mts64.c 27 KB

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