mts64.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * ALSA Driver for Ego Systems Inc. (ESI) Miditerminal 4140
  3. * Copyright (c) 2006 by Matthias König <mk@phasorlab.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/parport.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/delay.h>
  25. #include <sound/core.h>
  26. #include <sound/initval.h>
  27. #include <sound/rawmidi.h>
  28. #include <sound/control.h>
  29. #define CARD_NAME "Miditerminal 4140"
  30. #define DRIVER_NAME "MTS64"
  31. #define PLATFORM_DRIVER "snd_mts64"
  32. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  33. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  34. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  35. static struct platform_device *platform_devices[SNDRV_CARDS];
  36. static int device_count;
  37. module_param_array(index, int, NULL, S_IRUGO);
  38. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  39. module_param_array(id, charp, NULL, S_IRUGO);
  40. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  41. module_param_array(enable, bool, NULL, S_IRUGO);
  42. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  43. MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
  44. MODULE_DESCRIPTION("ESI Miditerminal 4140");
  45. MODULE_LICENSE("GPL");
  46. MODULE_SUPPORTED_DEVICE("{{ESI,Miditerminal 4140}}");
  47. /*********************************************************************
  48. * Chip specific
  49. *********************************************************************/
  50. #define MTS64_NUM_INPUT_PORTS 5
  51. #define MTS64_NUM_OUTPUT_PORTS 4
  52. #define MTS64_SMPTE_SUBSTREAM 4
  53. struct mts64 {
  54. spinlock_t lock;
  55. struct snd_card *card;
  56. struct snd_rawmidi *rmidi;
  57. struct pardevice *pardev;
  58. int pardev_claimed;
  59. int open_count;
  60. int current_midi_output_port;
  61. int current_midi_input_port;
  62. u8 mode[MTS64_NUM_INPUT_PORTS];
  63. struct snd_rawmidi_substream *midi_input_substream[MTS64_NUM_INPUT_PORTS];
  64. int smpte_switch;
  65. u8 time[4]; /* [0]=hh, [1]=mm, [2]=ss, [3]=ff */
  66. u8 fps;
  67. };
  68. static int snd_mts64_free(struct mts64 *mts)
  69. {
  70. kfree(mts);
  71. return 0;
  72. }
  73. static int __devinit snd_mts64_create(struct snd_card *card,
  74. struct pardevice *pardev,
  75. struct mts64 **rchip)
  76. {
  77. struct mts64 *mts;
  78. *rchip = NULL;
  79. mts = kzalloc(sizeof(struct mts64), GFP_KERNEL);
  80. if (mts == NULL)
  81. return -ENOMEM;
  82. /* Init chip specific data */
  83. spin_lock_init(&mts->lock);
  84. mts->card = card;
  85. mts->pardev = pardev;
  86. mts->current_midi_output_port = -1;
  87. mts->current_midi_input_port = -1;
  88. *rchip = mts;
  89. return 0;
  90. }
  91. /*********************************************************************
  92. * HW register related constants
  93. *********************************************************************/
  94. /* Status Bits */
  95. #define MTS64_STAT_BSY 0x80
  96. #define MTS64_STAT_BIT_SET 0x20 /* readout process, bit is set */
  97. #define MTS64_STAT_PORT 0x10 /* read byte is a port number */
  98. /* Control Bits */
  99. #define MTS64_CTL_READOUT 0x08 /* enable readout */
  100. #define MTS64_CTL_WRITE_CMD 0x06
  101. #define MTS64_CTL_WRITE_DATA 0x02
  102. #define MTS64_CTL_STROBE 0x01
  103. /* Command */
  104. #define MTS64_CMD_RESET 0xfe
  105. #define MTS64_CMD_PROBE 0x8f /* Used in probing procedure */
  106. #define MTS64_CMD_SMPTE_SET_TIME 0xe8
  107. #define MTS64_CMD_SMPTE_SET_FPS 0xee
  108. #define MTS64_CMD_SMPTE_STOP 0xef
  109. #define MTS64_CMD_SMPTE_FPS_24 0xe3
  110. #define MTS64_CMD_SMPTE_FPS_25 0xe2
  111. #define MTS64_CMD_SMPTE_FPS_2997 0xe4
  112. #define MTS64_CMD_SMPTE_FPS_30D 0xe1
  113. #define MTS64_CMD_SMPTE_FPS_30 0xe0
  114. #define MTS64_CMD_COM_OPEN 0xf8 /* setting the communication mode */
  115. #define MTS64_CMD_COM_CLOSE1 0xff /* clearing communication mode */
  116. #define MTS64_CMD_COM_CLOSE2 0xf5
  117. /*********************************************************************
  118. * Hardware specific functions
  119. *********************************************************************/
  120. static void mts64_enable_readout(struct parport *p);
  121. static void mts64_disable_readout(struct parport *p);
  122. static int mts64_device_ready(struct parport *p);
  123. static int mts64_device_init(struct parport *p);
  124. static int mts64_device_open(struct mts64 *mts);
  125. static int mts64_device_close(struct mts64 *mts);
  126. static u8 mts64_map_midi_input(u8 c);
  127. static int mts64_probe(struct parport *p);
  128. static u16 mts64_read(struct parport *p);
  129. static u8 mts64_read_char(struct parport *p);
  130. static void mts64_smpte_start(struct parport *p,
  131. u8 hours, u8 minutes,
  132. u8 seconds, u8 frames,
  133. u8 idx);
  134. static void mts64_smpte_stop(struct parport *p);
  135. static void mts64_write_command(struct parport *p, u8 c);
  136. static void mts64_write_data(struct parport *p, u8 c);
  137. static void mts64_write_midi(struct mts64 *mts, u8 c, int midiport);
  138. /* Enables the readout procedure
  139. *
  140. * Before we can read a midi byte from the device, we have to set
  141. * bit 3 of control port.
  142. */
  143. static void mts64_enable_readout(struct parport *p)
  144. {
  145. u8 c;
  146. c = parport_read_control(p);
  147. c |= MTS64_CTL_READOUT;
  148. parport_write_control(p, c);
  149. }
  150. /* Disables readout
  151. *
  152. * Readout is disabled by clearing bit 3 of control
  153. */
  154. static void mts64_disable_readout(struct parport *p)
  155. {
  156. u8 c;
  157. c = parport_read_control(p);
  158. c &= ~MTS64_CTL_READOUT;
  159. parport_write_control(p, c);
  160. }
  161. /* waits for device ready
  162. *
  163. * Checks if BUSY (Bit 7 of status) is clear
  164. * 1 device ready
  165. * 0 failure
  166. */
  167. static int mts64_device_ready(struct parport *p)
  168. {
  169. int i;
  170. u8 c;
  171. for (i = 0; i < 0xffff; ++i) {
  172. c = parport_read_status(p);
  173. c &= MTS64_STAT_BSY;
  174. if (c != 0)
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. /* Init device (LED blinking startup magic)
  180. *
  181. * Returns:
  182. * 0 init ok
  183. * -EIO failure
  184. */
  185. static int __devinit mts64_device_init(struct parport *p)
  186. {
  187. int i;
  188. mts64_write_command(p, MTS64_CMD_RESET);
  189. for (i = 0; i < 64; ++i) {
  190. msleep(100);
  191. if (mts64_probe(p) == 0) {
  192. /* success */
  193. mts64_disable_readout(p);
  194. return 0;
  195. }
  196. }
  197. mts64_disable_readout(p);
  198. return -EIO;
  199. }
  200. /*
  201. * Opens the device (set communication mode)
  202. */
  203. static int mts64_device_open(struct mts64 *mts)
  204. {
  205. int i;
  206. struct parport *p = mts->pardev->port;
  207. for (i = 0; i < 5; ++i)
  208. mts64_write_command(p, MTS64_CMD_COM_OPEN);
  209. return 0;
  210. }
  211. /*
  212. * Close device (clear communication mode)
  213. */
  214. static int mts64_device_close(struct mts64 *mts)
  215. {
  216. int i;
  217. struct parport *p = mts->pardev->port;
  218. for (i = 0; i < 5; ++i) {
  219. mts64_write_command(p, MTS64_CMD_COM_CLOSE1);
  220. mts64_write_command(p, MTS64_CMD_COM_CLOSE2);
  221. }
  222. return 0;
  223. }
  224. /* map hardware port to substream number
  225. *
  226. * When reading a byte from the device, the device tells us
  227. * on what port the byte is. This HW port has to be mapped to
  228. * the midiport (substream number).
  229. * substream 0-3 are Midiports 1-4
  230. * substream 4 is SMPTE Timecode
  231. * The mapping is done by the table:
  232. * HW | 0 | 1 | 2 | 3 | 4
  233. * SW | 0 | 1 | 4 | 2 | 3
  234. */
  235. static u8 mts64_map_midi_input(u8 c)
  236. {
  237. static u8 map[] = { 0, 1, 4, 2, 3 };
  238. return map[c];
  239. }
  240. /* Probe parport for device
  241. *
  242. * Do we have a Miditerminal 4140 on parport?
  243. * Returns:
  244. * 0 device found
  245. * -ENODEV no device
  246. */
  247. static int __devinit mts64_probe(struct parport *p)
  248. {
  249. u8 c;
  250. mts64_smpte_stop(p);
  251. mts64_write_command(p, MTS64_CMD_PROBE);
  252. msleep(50);
  253. c = mts64_read(p);
  254. c &= 0x00ff;
  255. if (c != MTS64_CMD_PROBE)
  256. return -ENODEV;
  257. else
  258. return 0;
  259. }
  260. /* Read byte incl. status from device
  261. *
  262. * Returns:
  263. * data in lower 8 bits and status in upper 8 bits
  264. */
  265. static u16 mts64_read(struct parport *p)
  266. {
  267. u8 data, status;
  268. mts64_device_ready(p);
  269. mts64_enable_readout(p);
  270. status = parport_read_status(p);
  271. data = mts64_read_char(p);
  272. mts64_disable_readout(p);
  273. return (status << 8) | data;
  274. }
  275. /* Read a byte from device
  276. *
  277. * Note, that readout mode has to be enabled.
  278. * readout procedure is as follows:
  279. * - Write number of the Bit to read to DATA
  280. * - Read STATUS
  281. * - Bit 5 of STATUS indicates if Bit is set
  282. *
  283. * Returns:
  284. * Byte read from device
  285. */
  286. static u8 mts64_read_char(struct parport *p)
  287. {
  288. u8 c = 0;
  289. u8 status;
  290. u8 i;
  291. for (i = 0; i < 8; ++i) {
  292. parport_write_data(p, i);
  293. c >>= 1;
  294. status = parport_read_status(p);
  295. if (status & MTS64_STAT_BIT_SET)
  296. c |= 0x80;
  297. }
  298. return c;
  299. }
  300. /* Starts SMPTE Timecode generation
  301. *
  302. * The device creates SMPTE Timecode by hardware.
  303. * 0 24 fps
  304. * 1 25 fps
  305. * 2 29.97 fps
  306. * 3 30 fps (Drop-frame)
  307. * 4 30 fps
  308. */
  309. static void mts64_smpte_start(struct parport *p,
  310. u8 hours, u8 minutes,
  311. u8 seconds, u8 frames,
  312. u8 idx)
  313. {
  314. static u8 fps[5] = { MTS64_CMD_SMPTE_FPS_24,
  315. MTS64_CMD_SMPTE_FPS_25,
  316. MTS64_CMD_SMPTE_FPS_2997,
  317. MTS64_CMD_SMPTE_FPS_30D,
  318. MTS64_CMD_SMPTE_FPS_30 };
  319. mts64_write_command(p, MTS64_CMD_SMPTE_SET_TIME);
  320. mts64_write_command(p, frames);
  321. mts64_write_command(p, seconds);
  322. mts64_write_command(p, minutes);
  323. mts64_write_command(p, hours);
  324. mts64_write_command(p, MTS64_CMD_SMPTE_SET_FPS);
  325. mts64_write_command(p, fps[idx]);
  326. }
  327. /* Stops SMPTE Timecode generation
  328. */
  329. static void mts64_smpte_stop(struct parport *p)
  330. {
  331. mts64_write_command(p, MTS64_CMD_SMPTE_STOP);
  332. }
  333. /* Write a command byte to device
  334. */
  335. static void mts64_write_command(struct parport *p, u8 c)
  336. {
  337. mts64_device_ready(p);
  338. parport_write_data(p, c);
  339. parport_write_control(p, MTS64_CTL_WRITE_CMD);
  340. parport_write_control(p, MTS64_CTL_WRITE_CMD | MTS64_CTL_STROBE);
  341. parport_write_control(p, MTS64_CTL_WRITE_CMD);
  342. }
  343. /* Write a data byte to device
  344. */
  345. static void mts64_write_data(struct parport *p, u8 c)
  346. {
  347. mts64_device_ready(p);
  348. parport_write_data(p, c);
  349. parport_write_control(p, MTS64_CTL_WRITE_DATA);
  350. parport_write_control(p, MTS64_CTL_WRITE_DATA | MTS64_CTL_STROBE);
  351. parport_write_control(p, MTS64_CTL_WRITE_DATA);
  352. }
  353. /* Write a MIDI byte to midiport
  354. *
  355. * midiport ranges from 0-3 and maps to Ports 1-4
  356. * assumptions: communication mode is on
  357. */
  358. static void mts64_write_midi(struct mts64 *mts, u8 c,
  359. int midiport)
  360. {
  361. struct parport *p = mts->pardev->port;
  362. /* check current midiport */
  363. if (mts->current_midi_output_port != midiport)
  364. mts64_write_command(p, midiport);
  365. /* write midi byte */
  366. mts64_write_data(p, c);
  367. }
  368. /*********************************************************************
  369. * Control elements
  370. *********************************************************************/
  371. /* SMPTE Switch */
  372. #define snd_mts64_ctl_smpte_switch_info snd_ctl_boolean_mono_info
  373. static int snd_mts64_ctl_smpte_switch_get(struct snd_kcontrol* kctl,
  374. struct snd_ctl_elem_value *uctl)
  375. {
  376. struct mts64 *mts = snd_kcontrol_chip(kctl);
  377. spin_lock_irq(&mts->lock);
  378. uctl->value.integer.value[0] = mts->smpte_switch;
  379. spin_unlock_irq(&mts->lock);
  380. return 0;
  381. }
  382. /* smpte_switch is not accessed from IRQ handler, so we just need
  383. to protect the HW access */
  384. static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
  385. struct snd_ctl_elem_value *uctl)
  386. {
  387. struct mts64 *mts = snd_kcontrol_chip(kctl);
  388. int changed = 0;
  389. int val = !!uctl->value.integer.value[0];
  390. spin_lock_irq(&mts->lock);
  391. if (mts->smpte_switch == val)
  392. goto __out;
  393. changed = 1;
  394. mts->smpte_switch = val;
  395. if (mts->smpte_switch) {
  396. mts64_smpte_start(mts->pardev->port,
  397. mts->time[0], mts->time[1],
  398. mts->time[2], mts->time[3],
  399. mts->fps);
  400. } else {
  401. mts64_smpte_stop(mts->pardev->port);
  402. }
  403. __out:
  404. spin_unlock_irq(&mts->lock);
  405. return changed;
  406. }
  407. static struct snd_kcontrol_new mts64_ctl_smpte_switch __devinitdata = {
  408. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  409. .name = "SMPTE Playback Switch",
  410. .index = 0,
  411. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  412. .private_value = 0,
  413. .info = snd_mts64_ctl_smpte_switch_info,
  414. .get = snd_mts64_ctl_smpte_switch_get,
  415. .put = snd_mts64_ctl_smpte_switch_put
  416. };
  417. /* Time */
  418. static int snd_mts64_ctl_smpte_time_h_info(struct snd_kcontrol *kctl,
  419. struct snd_ctl_elem_info *uinfo)
  420. {
  421. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  422. uinfo->count = 1;
  423. uinfo->value.integer.min = 0;
  424. uinfo->value.integer.max = 23;
  425. return 0;
  426. }
  427. static int snd_mts64_ctl_smpte_time_f_info(struct snd_kcontrol *kctl,
  428. struct snd_ctl_elem_info *uinfo)
  429. {
  430. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  431. uinfo->count = 1;
  432. uinfo->value.integer.min = 0;
  433. uinfo->value.integer.max = 99;
  434. return 0;
  435. }
  436. static int snd_mts64_ctl_smpte_time_info(struct snd_kcontrol *kctl,
  437. struct snd_ctl_elem_info *uinfo)
  438. {
  439. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  440. uinfo->count = 1;
  441. uinfo->value.integer.min = 0;
  442. uinfo->value.integer.max = 59;
  443. return 0;
  444. }
  445. static int snd_mts64_ctl_smpte_time_get(struct snd_kcontrol *kctl,
  446. struct snd_ctl_elem_value *uctl)
  447. {
  448. struct mts64 *mts = snd_kcontrol_chip(kctl);
  449. int idx = kctl->private_value;
  450. spin_lock_irq(&mts->lock);
  451. uctl->value.integer.value[0] = mts->time[idx];
  452. spin_unlock_irq(&mts->lock);
  453. return 0;
  454. }
  455. static int snd_mts64_ctl_smpte_time_put(struct snd_kcontrol *kctl,
  456. struct snd_ctl_elem_value *uctl)
  457. {
  458. struct mts64 *mts = snd_kcontrol_chip(kctl);
  459. int idx = kctl->private_value;
  460. unsigned int time = uctl->value.integer.value[0] % 60;
  461. int changed = 0;
  462. spin_lock_irq(&mts->lock);
  463. if (mts->time[idx] != time) {
  464. changed = 1;
  465. mts->time[idx] = time;
  466. }
  467. spin_unlock_irq(&mts->lock);
  468. return changed;
  469. }
  470. static struct snd_kcontrol_new mts64_ctl_smpte_time_hours __devinitdata = {
  471. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  472. .name = "SMPTE Time Hours",
  473. .index = 0,
  474. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  475. .private_value = 0,
  476. .info = snd_mts64_ctl_smpte_time_h_info,
  477. .get = snd_mts64_ctl_smpte_time_get,
  478. .put = snd_mts64_ctl_smpte_time_put
  479. };
  480. static struct snd_kcontrol_new mts64_ctl_smpte_time_minutes __devinitdata = {
  481. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  482. .name = "SMPTE Time Minutes",
  483. .index = 0,
  484. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  485. .private_value = 1,
  486. .info = snd_mts64_ctl_smpte_time_info,
  487. .get = snd_mts64_ctl_smpte_time_get,
  488. .put = snd_mts64_ctl_smpte_time_put
  489. };
  490. static struct snd_kcontrol_new mts64_ctl_smpte_time_seconds __devinitdata = {
  491. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  492. .name = "SMPTE Time Seconds",
  493. .index = 0,
  494. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  495. .private_value = 2,
  496. .info = snd_mts64_ctl_smpte_time_info,
  497. .get = snd_mts64_ctl_smpte_time_get,
  498. .put = snd_mts64_ctl_smpte_time_put
  499. };
  500. static struct snd_kcontrol_new mts64_ctl_smpte_time_frames __devinitdata = {
  501. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  502. .name = "SMPTE Time Frames",
  503. .index = 0,
  504. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  505. .private_value = 3,
  506. .info = snd_mts64_ctl_smpte_time_f_info,
  507. .get = snd_mts64_ctl_smpte_time_get,
  508. .put = snd_mts64_ctl_smpte_time_put
  509. };
  510. /* FPS */
  511. static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
  512. struct snd_ctl_elem_info *uinfo)
  513. {
  514. static char *texts[5] = { "24",
  515. "25",
  516. "29.97",
  517. "30D",
  518. "30" };
  519. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  520. uinfo->count = 1;
  521. uinfo->value.enumerated.items = 5;
  522. if (uinfo->value.enumerated.item > 4)
  523. uinfo->value.enumerated.item = 4;
  524. strcpy(uinfo->value.enumerated.name,
  525. texts[uinfo->value.enumerated.item]);
  526. return 0;
  527. }
  528. static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
  529. struct snd_ctl_elem_value *uctl)
  530. {
  531. struct mts64 *mts = snd_kcontrol_chip(kctl);
  532. spin_lock_irq(&mts->lock);
  533. uctl->value.enumerated.item[0] = mts->fps;
  534. spin_unlock_irq(&mts->lock);
  535. return 0;
  536. }
  537. static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
  538. struct snd_ctl_elem_value *uctl)
  539. {
  540. struct mts64 *mts = snd_kcontrol_chip(kctl);
  541. int changed = 0;
  542. if (uctl->value.enumerated.item[0] >= 5)
  543. return -EINVAL;
  544. spin_lock_irq(&mts->lock);
  545. if (mts->fps != uctl->value.enumerated.item[0]) {
  546. changed = 1;
  547. mts->fps = uctl->value.enumerated.item[0];
  548. }
  549. spin_unlock_irq(&mts->lock);
  550. return changed;
  551. }
  552. static struct snd_kcontrol_new mts64_ctl_smpte_fps __devinitdata = {
  553. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  554. .name = "SMPTE Fps",
  555. .index = 0,
  556. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  557. .private_value = 0,
  558. .info = snd_mts64_ctl_smpte_fps_info,
  559. .get = snd_mts64_ctl_smpte_fps_get,
  560. .put = snd_mts64_ctl_smpte_fps_put
  561. };
  562. static int __devinit snd_mts64_ctl_create(struct snd_card *card,
  563. struct mts64 *mts)
  564. {
  565. int err, i;
  566. static struct snd_kcontrol_new *control[] __devinitdata = {
  567. &mts64_ctl_smpte_switch,
  568. &mts64_ctl_smpte_time_hours,
  569. &mts64_ctl_smpte_time_minutes,
  570. &mts64_ctl_smpte_time_seconds,
  571. &mts64_ctl_smpte_time_frames,
  572. &mts64_ctl_smpte_fps,
  573. NULL };
  574. for (i = 0; control[i]; ++i) {
  575. err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
  576. if (err < 0) {
  577. snd_printd("Cannot create control: %s\n",
  578. control[i]->name);
  579. return err;
  580. }
  581. }
  582. return 0;
  583. }
  584. /*********************************************************************
  585. * Rawmidi
  586. *********************************************************************/
  587. #define MTS64_MODE_INPUT_TRIGGERED 0x01
  588. static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
  589. {
  590. struct mts64 *mts = substream->rmidi->private_data;
  591. if (mts->open_count == 0) {
  592. /* We don't need a spinlock here, because this is just called
  593. if the device has not been opened before.
  594. So there aren't any IRQs from the device */
  595. mts64_device_open(mts);
  596. msleep(50);
  597. }
  598. ++(mts->open_count);
  599. return 0;
  600. }
  601. static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
  602. {
  603. struct mts64 *mts = substream->rmidi->private_data;
  604. unsigned long flags;
  605. --(mts->open_count);
  606. if (mts->open_count == 0) {
  607. /* We need the spinlock_irqsave here because we can still
  608. have IRQs at this point */
  609. spin_lock_irqsave(&mts->lock, flags);
  610. mts64_device_close(mts);
  611. spin_unlock_irqrestore(&mts->lock, flags);
  612. msleep(500);
  613. } else if (mts->open_count < 0)
  614. mts->open_count = 0;
  615. return 0;
  616. }
  617. static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
  618. int up)
  619. {
  620. struct mts64 *mts = substream->rmidi->private_data;
  621. u8 data;
  622. unsigned long flags;
  623. spin_lock_irqsave(&mts->lock, flags);
  624. while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
  625. mts64_write_midi(mts, data, substream->number+1);
  626. snd_rawmidi_transmit_ack(substream, 1);
  627. }
  628. spin_unlock_irqrestore(&mts->lock, flags);
  629. }
  630. static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
  631. int up)
  632. {
  633. struct mts64 *mts = substream->rmidi->private_data;
  634. unsigned long flags;
  635. spin_lock_irqsave(&mts->lock, flags);
  636. if (up)
  637. mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
  638. else
  639. mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
  640. spin_unlock_irqrestore(&mts->lock, flags);
  641. }
  642. static struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
  643. .open = snd_mts64_rawmidi_open,
  644. .close = snd_mts64_rawmidi_close,
  645. .trigger = snd_mts64_rawmidi_output_trigger
  646. };
  647. static struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
  648. .open = snd_mts64_rawmidi_open,
  649. .close = snd_mts64_rawmidi_close,
  650. .trigger = snd_mts64_rawmidi_input_trigger
  651. };
  652. /* Create and initialize the rawmidi component */
  653. static int __devinit snd_mts64_rawmidi_create(struct snd_card *card)
  654. {
  655. struct mts64 *mts = card->private_data;
  656. struct snd_rawmidi *rmidi;
  657. struct snd_rawmidi_substream *substream;
  658. struct list_head *list;
  659. int err;
  660. err = snd_rawmidi_new(card, CARD_NAME, 0,
  661. MTS64_NUM_OUTPUT_PORTS,
  662. MTS64_NUM_INPUT_PORTS,
  663. &rmidi);
  664. if (err < 0)
  665. return err;
  666. rmidi->private_data = mts;
  667. strcpy(rmidi->name, CARD_NAME);
  668. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  669. SNDRV_RAWMIDI_INFO_INPUT |
  670. SNDRV_RAWMIDI_INFO_DUPLEX;
  671. mts->rmidi = rmidi;
  672. /* register rawmidi ops */
  673. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  674. &snd_mts64_rawmidi_output_ops);
  675. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  676. &snd_mts64_rawmidi_input_ops);
  677. /* name substreams */
  678. /* output */
  679. list_for_each(list,
  680. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
  681. substream = list_entry(list, struct snd_rawmidi_substream, list);
  682. sprintf(substream->name,
  683. "Miditerminal %d", substream->number+1);
  684. }
  685. /* input */
  686. list_for_each(list,
  687. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
  688. substream = list_entry(list, struct snd_rawmidi_substream, list);
  689. mts->midi_input_substream[substream->number] = substream;
  690. switch(substream->number) {
  691. case MTS64_SMPTE_SUBSTREAM:
  692. strcpy(substream->name, "Miditerminal SMPTE");
  693. break;
  694. default:
  695. sprintf(substream->name,
  696. "Miditerminal %d", substream->number+1);
  697. }
  698. }
  699. /* controls */
  700. err = snd_mts64_ctl_create(card, mts);
  701. return err;
  702. }
  703. /*********************************************************************
  704. * parport stuff
  705. *********************************************************************/
  706. static void snd_mts64_interrupt(void *private)
  707. {
  708. struct mts64 *mts = ((struct snd_card*)private)->private_data;
  709. u16 ret;
  710. u8 status, data;
  711. struct snd_rawmidi_substream *substream;
  712. spin_lock(&mts->lock);
  713. ret = mts64_read(mts->pardev->port);
  714. data = ret & 0x00ff;
  715. status = ret >> 8;
  716. if (status & MTS64_STAT_PORT) {
  717. mts->current_midi_input_port = mts64_map_midi_input(data);
  718. } else {
  719. if (mts->current_midi_input_port == -1)
  720. goto __out;
  721. substream = mts->midi_input_substream[mts->current_midi_input_port];
  722. if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
  723. snd_rawmidi_receive(substream, &data, 1);
  724. }
  725. __out:
  726. spin_unlock(&mts->lock);
  727. }
  728. static int __devinit snd_mts64_probe_port(struct parport *p)
  729. {
  730. struct pardevice *pardev;
  731. int res;
  732. pardev = parport_register_device(p, DRIVER_NAME,
  733. NULL, NULL, NULL,
  734. 0, NULL);
  735. if (!pardev)
  736. return -EIO;
  737. if (parport_claim(pardev)) {
  738. parport_unregister_device(pardev);
  739. return -EIO;
  740. }
  741. res = mts64_probe(p);
  742. parport_release(pardev);
  743. parport_unregister_device(pardev);
  744. return res;
  745. }
  746. static void __devinit snd_mts64_attach(struct parport *p)
  747. {
  748. struct platform_device *device;
  749. device = platform_device_alloc(PLATFORM_DRIVER, device_count);
  750. if (!device)
  751. return;
  752. /* Temporary assignment to forward the parport */
  753. platform_set_drvdata(device, p);
  754. if (platform_device_add(device) < 0) {
  755. platform_device_put(device);
  756. return;
  757. }
  758. /* Since we dont get the return value of probe
  759. * We need to check if device probing succeeded or not */
  760. if (!platform_get_drvdata(device)) {
  761. platform_device_unregister(device);
  762. return;
  763. }
  764. /* register device in global table */
  765. platform_devices[device_count] = device;
  766. device_count++;
  767. }
  768. static void snd_mts64_detach(struct parport *p)
  769. {
  770. /* nothing to do here */
  771. }
  772. static struct parport_driver mts64_parport_driver = {
  773. .name = "mts64",
  774. .attach = snd_mts64_attach,
  775. .detach = snd_mts64_detach
  776. };
  777. /*********************************************************************
  778. * platform stuff
  779. *********************************************************************/
  780. static void snd_mts64_card_private_free(struct snd_card *card)
  781. {
  782. struct mts64 *mts = card->private_data;
  783. struct pardevice *pardev = mts->pardev;
  784. if (pardev) {
  785. if (mts->pardev_claimed)
  786. parport_release(pardev);
  787. parport_unregister_device(pardev);
  788. }
  789. snd_mts64_free(mts);
  790. }
  791. static int __devinit snd_mts64_probe(struct platform_device *pdev)
  792. {
  793. struct pardevice *pardev;
  794. struct parport *p;
  795. int dev = pdev->id;
  796. struct snd_card *card = NULL;
  797. struct mts64 *mts = NULL;
  798. int err;
  799. p = platform_get_drvdata(pdev);
  800. platform_set_drvdata(pdev, NULL);
  801. if (dev >= SNDRV_CARDS)
  802. return -ENODEV;
  803. if (!enable[dev])
  804. return -ENOENT;
  805. if ((err = snd_mts64_probe_port(p)) < 0)
  806. return err;
  807. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  808. if (card == NULL) {
  809. snd_printd("Cannot create card\n");
  810. return -ENOMEM;
  811. }
  812. strcpy(card->driver, DRIVER_NAME);
  813. strcpy(card->shortname, "ESI " CARD_NAME);
  814. sprintf(card->longname, "%s at 0x%lx, irq %i",
  815. card->shortname, p->base, p->irq);
  816. pardev = parport_register_device(p, /* port */
  817. DRIVER_NAME, /* name */
  818. NULL, /* preempt */
  819. NULL, /* wakeup */
  820. snd_mts64_interrupt, /* ISR */
  821. PARPORT_DEV_EXCL, /* flags */
  822. (void *)card); /* private */
  823. if (pardev == NULL) {
  824. snd_printd("Cannot register pardevice\n");
  825. err = -EIO;
  826. goto __err;
  827. }
  828. if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
  829. snd_printd("Cannot create main component\n");
  830. parport_unregister_device(pardev);
  831. goto __err;
  832. }
  833. card->private_data = mts;
  834. card->private_free = snd_mts64_card_private_free;
  835. if ((err = snd_mts64_rawmidi_create(card)) < 0) {
  836. snd_printd("Creating Rawmidi component failed\n");
  837. goto __err;
  838. }
  839. /* claim parport */
  840. if (parport_claim(pardev)) {
  841. snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
  842. err = -EIO;
  843. goto __err;
  844. }
  845. mts->pardev_claimed = 1;
  846. /* init device */
  847. if ((err = mts64_device_init(p)) < 0)
  848. goto __err;
  849. platform_set_drvdata(pdev, card);
  850. snd_card_set_dev(card, &pdev->dev);
  851. /* At this point card will be usable */
  852. if ((err = snd_card_register(card)) < 0) {
  853. snd_printd("Cannot register card\n");
  854. goto __err;
  855. }
  856. snd_printk("ESI Miditerminal 4140 on 0x%lx\n", p->base);
  857. return 0;
  858. __err:
  859. snd_card_free(card);
  860. return err;
  861. }
  862. static int __devexit snd_mts64_remove(struct platform_device *pdev)
  863. {
  864. struct snd_card *card = platform_get_drvdata(pdev);
  865. if (card)
  866. snd_card_free(card);
  867. return 0;
  868. }
  869. static struct platform_driver snd_mts64_driver = {
  870. .probe = snd_mts64_probe,
  871. .remove = __devexit_p(snd_mts64_remove),
  872. .driver = {
  873. .name = PLATFORM_DRIVER
  874. }
  875. };
  876. /*********************************************************************
  877. * module init stuff
  878. *********************************************************************/
  879. static void snd_mts64_unregister_all(void)
  880. {
  881. int i;
  882. for (i = 0; i < SNDRV_CARDS; ++i) {
  883. if (platform_devices[i]) {
  884. platform_device_unregister(platform_devices[i]);
  885. platform_devices[i] = NULL;
  886. }
  887. }
  888. platform_driver_unregister(&snd_mts64_driver);
  889. parport_unregister_driver(&mts64_parport_driver);
  890. }
  891. static int __init snd_mts64_module_init(void)
  892. {
  893. int err;
  894. if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
  895. return err;
  896. if (parport_register_driver(&mts64_parport_driver) != 0) {
  897. platform_driver_unregister(&snd_mts64_driver);
  898. return -EIO;
  899. }
  900. if (device_count == 0) {
  901. snd_mts64_unregister_all();
  902. return -ENODEV;
  903. }
  904. return 0;
  905. }
  906. static void __exit snd_mts64_module_exit(void)
  907. {
  908. snd_mts64_unregister_all();
  909. }
  910. module_init(snd_mts64_module_init);
  911. module_exit(snd_mts64_module_exit);