mts64.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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/module.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <sound/core.h>
  28. #include <sound/initval.h>
  29. #include <sound/rawmidi.h>
  30. #include <sound/control.h>
  31. #define CARD_NAME "Miditerminal 4140"
  32. #define DRIVER_NAME "MTS64"
  33. #define PLATFORM_DRIVER "snd_mts64"
  34. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  35. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  36. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  37. static struct platform_device *platform_devices[SNDRV_CARDS];
  38. static int device_count;
  39. module_param_array(index, int, NULL, S_IRUGO);
  40. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  41. module_param_array(id, charp, NULL, S_IRUGO);
  42. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  43. module_param_array(enable, bool, NULL, S_IRUGO);
  44. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  45. MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
  46. MODULE_DESCRIPTION("ESI Miditerminal 4140");
  47. MODULE_LICENSE("GPL");
  48. MODULE_SUPPORTED_DEVICE("{{ESI,Miditerminal 4140}}");
  49. /*********************************************************************
  50. * Chip specific
  51. *********************************************************************/
  52. #define MTS64_NUM_INPUT_PORTS 5
  53. #define MTS64_NUM_OUTPUT_PORTS 4
  54. #define MTS64_SMPTE_SUBSTREAM 4
  55. struct mts64 {
  56. spinlock_t lock;
  57. struct snd_card *card;
  58. struct snd_rawmidi *rmidi;
  59. struct pardevice *pardev;
  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 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 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 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 = {
  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 = {
  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 = {
  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 = {
  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 = {
  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 const char * const texts[5] = {
  516. "24", "25", "29.97", "30D", "30"
  517. };
  518. return snd_ctl_enum_info(uinfo, 1, 5, texts);
  519. }
  520. static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
  521. struct snd_ctl_elem_value *uctl)
  522. {
  523. struct mts64 *mts = snd_kcontrol_chip(kctl);
  524. spin_lock_irq(&mts->lock);
  525. uctl->value.enumerated.item[0] = mts->fps;
  526. spin_unlock_irq(&mts->lock);
  527. return 0;
  528. }
  529. static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
  530. struct snd_ctl_elem_value *uctl)
  531. {
  532. struct mts64 *mts = snd_kcontrol_chip(kctl);
  533. int changed = 0;
  534. if (uctl->value.enumerated.item[0] >= 5)
  535. return -EINVAL;
  536. spin_lock_irq(&mts->lock);
  537. if (mts->fps != uctl->value.enumerated.item[0]) {
  538. changed = 1;
  539. mts->fps = uctl->value.enumerated.item[0];
  540. }
  541. spin_unlock_irq(&mts->lock);
  542. return changed;
  543. }
  544. static struct snd_kcontrol_new mts64_ctl_smpte_fps = {
  545. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  546. .name = "SMPTE Fps",
  547. .index = 0,
  548. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  549. .private_value = 0,
  550. .info = snd_mts64_ctl_smpte_fps_info,
  551. .get = snd_mts64_ctl_smpte_fps_get,
  552. .put = snd_mts64_ctl_smpte_fps_put
  553. };
  554. static int snd_mts64_ctl_create(struct snd_card *card,
  555. struct mts64 *mts)
  556. {
  557. int err, i;
  558. static struct snd_kcontrol_new *control[] = {
  559. &mts64_ctl_smpte_switch,
  560. &mts64_ctl_smpte_time_hours,
  561. &mts64_ctl_smpte_time_minutes,
  562. &mts64_ctl_smpte_time_seconds,
  563. &mts64_ctl_smpte_time_frames,
  564. &mts64_ctl_smpte_fps,
  565. NULL };
  566. for (i = 0; control[i]; ++i) {
  567. err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
  568. if (err < 0) {
  569. snd_printd("Cannot create control: %s\n",
  570. control[i]->name);
  571. return err;
  572. }
  573. }
  574. return 0;
  575. }
  576. /*********************************************************************
  577. * Rawmidi
  578. *********************************************************************/
  579. #define MTS64_MODE_INPUT_TRIGGERED 0x01
  580. static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
  581. {
  582. struct mts64 *mts = substream->rmidi->private_data;
  583. if (mts->open_count == 0) {
  584. /* We don't need a spinlock here, because this is just called
  585. if the device has not been opened before.
  586. So there aren't any IRQs from the device */
  587. mts64_device_open(mts);
  588. msleep(50);
  589. }
  590. ++(mts->open_count);
  591. return 0;
  592. }
  593. static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
  594. {
  595. struct mts64 *mts = substream->rmidi->private_data;
  596. unsigned long flags;
  597. --(mts->open_count);
  598. if (mts->open_count == 0) {
  599. /* We need the spinlock_irqsave here because we can still
  600. have IRQs at this point */
  601. spin_lock_irqsave(&mts->lock, flags);
  602. mts64_device_close(mts);
  603. spin_unlock_irqrestore(&mts->lock, flags);
  604. msleep(500);
  605. } else if (mts->open_count < 0)
  606. mts->open_count = 0;
  607. return 0;
  608. }
  609. static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
  610. int up)
  611. {
  612. struct mts64 *mts = substream->rmidi->private_data;
  613. u8 data;
  614. unsigned long flags;
  615. spin_lock_irqsave(&mts->lock, flags);
  616. while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
  617. mts64_write_midi(mts, data, substream->number+1);
  618. snd_rawmidi_transmit_ack(substream, 1);
  619. }
  620. spin_unlock_irqrestore(&mts->lock, flags);
  621. }
  622. static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
  623. int up)
  624. {
  625. struct mts64 *mts = substream->rmidi->private_data;
  626. unsigned long flags;
  627. spin_lock_irqsave(&mts->lock, flags);
  628. if (up)
  629. mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
  630. else
  631. mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
  632. spin_unlock_irqrestore(&mts->lock, flags);
  633. }
  634. static struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
  635. .open = snd_mts64_rawmidi_open,
  636. .close = snd_mts64_rawmidi_close,
  637. .trigger = snd_mts64_rawmidi_output_trigger
  638. };
  639. static struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
  640. .open = snd_mts64_rawmidi_open,
  641. .close = snd_mts64_rawmidi_close,
  642. .trigger = snd_mts64_rawmidi_input_trigger
  643. };
  644. /* Create and initialize the rawmidi component */
  645. static int snd_mts64_rawmidi_create(struct snd_card *card)
  646. {
  647. struct mts64 *mts = card->private_data;
  648. struct snd_rawmidi *rmidi;
  649. struct snd_rawmidi_substream *substream;
  650. struct list_head *list;
  651. int err;
  652. err = snd_rawmidi_new(card, CARD_NAME, 0,
  653. MTS64_NUM_OUTPUT_PORTS,
  654. MTS64_NUM_INPUT_PORTS,
  655. &rmidi);
  656. if (err < 0)
  657. return err;
  658. rmidi->private_data = mts;
  659. strcpy(rmidi->name, CARD_NAME);
  660. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  661. SNDRV_RAWMIDI_INFO_INPUT |
  662. SNDRV_RAWMIDI_INFO_DUPLEX;
  663. mts->rmidi = rmidi;
  664. /* register rawmidi ops */
  665. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  666. &snd_mts64_rawmidi_output_ops);
  667. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  668. &snd_mts64_rawmidi_input_ops);
  669. /* name substreams */
  670. /* output */
  671. list_for_each(list,
  672. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
  673. substream = list_entry(list, struct snd_rawmidi_substream, list);
  674. sprintf(substream->name,
  675. "Miditerminal %d", substream->number+1);
  676. }
  677. /* input */
  678. list_for_each(list,
  679. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
  680. substream = list_entry(list, struct snd_rawmidi_substream, list);
  681. mts->midi_input_substream[substream->number] = substream;
  682. switch(substream->number) {
  683. case MTS64_SMPTE_SUBSTREAM:
  684. strcpy(substream->name, "Miditerminal SMPTE");
  685. break;
  686. default:
  687. sprintf(substream->name,
  688. "Miditerminal %d", substream->number+1);
  689. }
  690. }
  691. /* controls */
  692. err = snd_mts64_ctl_create(card, mts);
  693. return err;
  694. }
  695. /*********************************************************************
  696. * parport stuff
  697. *********************************************************************/
  698. static void snd_mts64_interrupt(void *private)
  699. {
  700. struct mts64 *mts = ((struct snd_card*)private)->private_data;
  701. u16 ret;
  702. u8 status, data;
  703. struct snd_rawmidi_substream *substream;
  704. spin_lock(&mts->lock);
  705. ret = mts64_read(mts->pardev->port);
  706. data = ret & 0x00ff;
  707. status = ret >> 8;
  708. if (status & MTS64_STAT_PORT) {
  709. mts->current_midi_input_port = mts64_map_midi_input(data);
  710. } else {
  711. if (mts->current_midi_input_port == -1)
  712. goto __out;
  713. substream = mts->midi_input_substream[mts->current_midi_input_port];
  714. if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
  715. snd_rawmidi_receive(substream, &data, 1);
  716. }
  717. __out:
  718. spin_unlock(&mts->lock);
  719. }
  720. static void snd_mts64_attach(struct parport *p)
  721. {
  722. struct platform_device *device;
  723. device = platform_device_alloc(PLATFORM_DRIVER, device_count);
  724. if (!device)
  725. return;
  726. /* Temporary assignment to forward the parport */
  727. platform_set_drvdata(device, p);
  728. if (platform_device_add(device) < 0) {
  729. platform_device_put(device);
  730. return;
  731. }
  732. /* Since we dont get the return value of probe
  733. * We need to check if device probing succeeded or not */
  734. if (!platform_get_drvdata(device)) {
  735. platform_device_unregister(device);
  736. return;
  737. }
  738. /* register device in global table */
  739. platform_devices[device_count] = device;
  740. device_count++;
  741. }
  742. static void snd_mts64_detach(struct parport *p)
  743. {
  744. /* nothing to do here */
  745. }
  746. static int snd_mts64_dev_probe(struct pardevice *pardev)
  747. {
  748. if (strcmp(pardev->name, DRIVER_NAME))
  749. return -ENODEV;
  750. return 0;
  751. }
  752. static struct parport_driver mts64_parport_driver = {
  753. .name = "mts64",
  754. .probe = snd_mts64_dev_probe,
  755. .match_port = snd_mts64_attach,
  756. .detach = snd_mts64_detach,
  757. .devmodel = true,
  758. };
  759. /*********************************************************************
  760. * platform stuff
  761. *********************************************************************/
  762. static void snd_mts64_card_private_free(struct snd_card *card)
  763. {
  764. struct mts64 *mts = card->private_data;
  765. struct pardevice *pardev = mts->pardev;
  766. if (pardev) {
  767. parport_release(pardev);
  768. parport_unregister_device(pardev);
  769. }
  770. snd_mts64_free(mts);
  771. }
  772. static int snd_mts64_probe(struct platform_device *pdev)
  773. {
  774. struct pardevice *pardev;
  775. struct parport *p;
  776. int dev = pdev->id;
  777. struct snd_card *card = NULL;
  778. struct mts64 *mts = NULL;
  779. int err;
  780. struct pardev_cb mts64_cb = {
  781. .preempt = NULL,
  782. .wakeup = NULL,
  783. .irq_func = snd_mts64_interrupt, /* ISR */
  784. .flags = PARPORT_DEV_EXCL, /* flags */
  785. };
  786. p = platform_get_drvdata(pdev);
  787. platform_set_drvdata(pdev, NULL);
  788. if (dev >= SNDRV_CARDS)
  789. return -ENODEV;
  790. if (!enable[dev])
  791. return -ENOENT;
  792. err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
  793. 0, &card);
  794. if (err < 0) {
  795. snd_printd("Cannot create card\n");
  796. return err;
  797. }
  798. strcpy(card->driver, DRIVER_NAME);
  799. strcpy(card->shortname, "ESI " CARD_NAME);
  800. sprintf(card->longname, "%s at 0x%lx, irq %i",
  801. card->shortname, p->base, p->irq);
  802. mts64_cb.private = card; /* private */
  803. pardev = parport_register_dev_model(p, /* port */
  804. DRIVER_NAME, /* name */
  805. &mts64_cb, /* callbacks */
  806. pdev->id); /* device number */
  807. if (!pardev) {
  808. snd_printd("Cannot register pardevice\n");
  809. err = -EIO;
  810. goto __err;
  811. }
  812. /* claim parport */
  813. if (parport_claim(pardev)) {
  814. snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
  815. err = -EIO;
  816. goto free_pardev;
  817. }
  818. if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
  819. snd_printd("Cannot create main component\n");
  820. goto release_pardev;
  821. }
  822. card->private_data = mts;
  823. card->private_free = snd_mts64_card_private_free;
  824. err = mts64_probe(p);
  825. if (err) {
  826. err = -EIO;
  827. goto __err;
  828. }
  829. if ((err = snd_mts64_rawmidi_create(card)) < 0) {
  830. snd_printd("Creating Rawmidi component failed\n");
  831. goto __err;
  832. }
  833. /* init device */
  834. if ((err = mts64_device_init(p)) < 0)
  835. goto __err;
  836. platform_set_drvdata(pdev, card);
  837. /* At this point card will be usable */
  838. if ((err = snd_card_register(card)) < 0) {
  839. snd_printd("Cannot register card\n");
  840. goto __err;
  841. }
  842. snd_printk(KERN_INFO "ESI Miditerminal 4140 on 0x%lx\n", p->base);
  843. return 0;
  844. release_pardev:
  845. parport_release(pardev);
  846. free_pardev:
  847. parport_unregister_device(pardev);
  848. __err:
  849. snd_card_free(card);
  850. return err;
  851. }
  852. static int snd_mts64_remove(struct platform_device *pdev)
  853. {
  854. struct snd_card *card = platform_get_drvdata(pdev);
  855. if (card)
  856. snd_card_free(card);
  857. return 0;
  858. }
  859. static struct platform_driver snd_mts64_driver = {
  860. .probe = snd_mts64_probe,
  861. .remove = snd_mts64_remove,
  862. .driver = {
  863. .name = PLATFORM_DRIVER,
  864. }
  865. };
  866. /*********************************************************************
  867. * module init stuff
  868. *********************************************************************/
  869. static void snd_mts64_unregister_all(void)
  870. {
  871. int i;
  872. for (i = 0; i < SNDRV_CARDS; ++i) {
  873. if (platform_devices[i]) {
  874. platform_device_unregister(platform_devices[i]);
  875. platform_devices[i] = NULL;
  876. }
  877. }
  878. platform_driver_unregister(&snd_mts64_driver);
  879. parport_unregister_driver(&mts64_parport_driver);
  880. }
  881. static int __init snd_mts64_module_init(void)
  882. {
  883. int err;
  884. if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
  885. return err;
  886. if (parport_register_driver(&mts64_parport_driver) != 0) {
  887. platform_driver_unregister(&snd_mts64_driver);
  888. return -EIO;
  889. }
  890. if (device_count == 0) {
  891. snd_mts64_unregister_all();
  892. return -ENODEV;
  893. }
  894. return 0;
  895. }
  896. static void __exit snd_mts64_module_exit(void)
  897. {
  898. snd_mts64_unregister_all();
  899. }
  900. module_init(snd_mts64_module_init);
  901. module_exit(snd_mts64_module_exit);