mts64.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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 <sound/driver.h>
  21. #include <linux/init.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/parport.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/delay.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. spin_lock_irq(&mts->lock);
  391. if (mts->smpte_switch == uctl->value.integer.value[0])
  392. goto __out;
  393. changed = 1;
  394. mts->smpte_switch = uctl->value.integer.value[0];
  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. int changed = 0;
  461. spin_lock_irq(&mts->lock);
  462. if (mts->time[idx] != uctl->value.integer.value[0]) {
  463. changed = 1;
  464. mts->time[idx] = uctl->value.integer.value[0];
  465. }
  466. spin_unlock_irq(&mts->lock);
  467. return changed;
  468. }
  469. static struct snd_kcontrol_new mts64_ctl_smpte_time_hours __devinitdata = {
  470. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  471. .name = "SMPTE Time Hours",
  472. .index = 0,
  473. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  474. .private_value = 0,
  475. .info = snd_mts64_ctl_smpte_time_h_info,
  476. .get = snd_mts64_ctl_smpte_time_get,
  477. .put = snd_mts64_ctl_smpte_time_put
  478. };
  479. static struct snd_kcontrol_new mts64_ctl_smpte_time_minutes __devinitdata = {
  480. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  481. .name = "SMPTE Time Minutes",
  482. .index = 0,
  483. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  484. .private_value = 1,
  485. .info = snd_mts64_ctl_smpte_time_info,
  486. .get = snd_mts64_ctl_smpte_time_get,
  487. .put = snd_mts64_ctl_smpte_time_put
  488. };
  489. static struct snd_kcontrol_new mts64_ctl_smpte_time_seconds __devinitdata = {
  490. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  491. .name = "SMPTE Time Seconds",
  492. .index = 0,
  493. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  494. .private_value = 2,
  495. .info = snd_mts64_ctl_smpte_time_info,
  496. .get = snd_mts64_ctl_smpte_time_get,
  497. .put = snd_mts64_ctl_smpte_time_put
  498. };
  499. static struct snd_kcontrol_new mts64_ctl_smpte_time_frames __devinitdata = {
  500. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  501. .name = "SMPTE Time Frames",
  502. .index = 0,
  503. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  504. .private_value = 3,
  505. .info = snd_mts64_ctl_smpte_time_f_info,
  506. .get = snd_mts64_ctl_smpte_time_get,
  507. .put = snd_mts64_ctl_smpte_time_put
  508. };
  509. /* FPS */
  510. static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
  511. struct snd_ctl_elem_info *uinfo)
  512. {
  513. static char *texts[5] = { "24",
  514. "25",
  515. "29.97",
  516. "30D",
  517. "30" };
  518. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  519. uinfo->count = 1;
  520. uinfo->value.enumerated.items = 5;
  521. if (uinfo->value.enumerated.item > 4)
  522. uinfo->value.enumerated.item = 4;
  523. strcpy(uinfo->value.enumerated.name,
  524. texts[uinfo->value.enumerated.item]);
  525. return 0;
  526. }
  527. static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
  528. struct snd_ctl_elem_value *uctl)
  529. {
  530. struct mts64 *mts = snd_kcontrol_chip(kctl);
  531. spin_lock_irq(&mts->lock);
  532. uctl->value.enumerated.item[0] = mts->fps;
  533. spin_unlock_irq(&mts->lock);
  534. return 0;
  535. }
  536. static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
  537. struct snd_ctl_elem_value *uctl)
  538. {
  539. struct mts64 *mts = snd_kcontrol_chip(kctl);
  540. int changed = 0;
  541. spin_lock_irq(&mts->lock);
  542. if (mts->fps != uctl->value.enumerated.item[0]) {
  543. changed = 1;
  544. mts->fps = uctl->value.enumerated.item[0];
  545. }
  546. spin_unlock_irq(&mts->lock);
  547. return changed;
  548. }
  549. static struct snd_kcontrol_new mts64_ctl_smpte_fps __devinitdata = {
  550. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  551. .name = "SMPTE Fps",
  552. .index = 0,
  553. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  554. .private_value = 0,
  555. .info = snd_mts64_ctl_smpte_fps_info,
  556. .get = snd_mts64_ctl_smpte_fps_get,
  557. .put = snd_mts64_ctl_smpte_fps_put
  558. };
  559. static int __devinit snd_mts64_ctl_create(struct snd_card *card,
  560. struct mts64 *mts)
  561. {
  562. int err, i;
  563. static struct snd_kcontrol_new *control[] = {
  564. &mts64_ctl_smpte_switch,
  565. &mts64_ctl_smpte_time_hours,
  566. &mts64_ctl_smpte_time_minutes,
  567. &mts64_ctl_smpte_time_seconds,
  568. &mts64_ctl_smpte_time_frames,
  569. &mts64_ctl_smpte_fps,
  570. NULL };
  571. for (i = 0; control[i]; ++i) {
  572. err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
  573. if (err < 0) {
  574. snd_printd("Cannot create control: %s\n",
  575. control[i]->name);
  576. return err;
  577. }
  578. }
  579. return 0;
  580. }
  581. /*********************************************************************
  582. * Rawmidi
  583. *********************************************************************/
  584. #define MTS64_MODE_INPUT_TRIGGERED 0x01
  585. static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
  586. {
  587. struct mts64 *mts = substream->rmidi->private_data;
  588. if (mts->open_count == 0) {
  589. /* We don't need a spinlock here, because this is just called
  590. if the device has not been opened before.
  591. So there aren't any IRQs from the device */
  592. mts64_device_open(mts);
  593. msleep(50);
  594. }
  595. ++(mts->open_count);
  596. return 0;
  597. }
  598. static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
  599. {
  600. struct mts64 *mts = substream->rmidi->private_data;
  601. unsigned long flags;
  602. --(mts->open_count);
  603. if (mts->open_count == 0) {
  604. /* We need the spinlock_irqsave here because we can still
  605. have IRQs at this point */
  606. spin_lock_irqsave(&mts->lock, flags);
  607. mts64_device_close(mts);
  608. spin_unlock_irqrestore(&mts->lock, flags);
  609. msleep(500);
  610. } else if (mts->open_count < 0)
  611. mts->open_count = 0;
  612. return 0;
  613. }
  614. static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
  615. int up)
  616. {
  617. struct mts64 *mts = substream->rmidi->private_data;
  618. u8 data;
  619. unsigned long flags;
  620. spin_lock_irqsave(&mts->lock, flags);
  621. while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
  622. mts64_write_midi(mts, data, substream->number+1);
  623. snd_rawmidi_transmit_ack(substream, 1);
  624. }
  625. spin_unlock_irqrestore(&mts->lock, flags);
  626. }
  627. static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
  628. int up)
  629. {
  630. struct mts64 *mts = substream->rmidi->private_data;
  631. unsigned long flags;
  632. spin_lock_irqsave(&mts->lock, flags);
  633. if (up)
  634. mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
  635. else
  636. mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
  637. spin_unlock_irqrestore(&mts->lock, flags);
  638. }
  639. static struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
  640. .open = snd_mts64_rawmidi_open,
  641. .close = snd_mts64_rawmidi_close,
  642. .trigger = snd_mts64_rawmidi_output_trigger
  643. };
  644. static struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
  645. .open = snd_mts64_rawmidi_open,
  646. .close = snd_mts64_rawmidi_close,
  647. .trigger = snd_mts64_rawmidi_input_trigger
  648. };
  649. /* Create and initialize the rawmidi component */
  650. static int __devinit snd_mts64_rawmidi_create(struct snd_card *card)
  651. {
  652. struct mts64 *mts = card->private_data;
  653. struct snd_rawmidi *rmidi;
  654. struct snd_rawmidi_substream *substream;
  655. struct list_head *list;
  656. int err;
  657. err = snd_rawmidi_new(card, CARD_NAME, 0,
  658. MTS64_NUM_OUTPUT_PORTS,
  659. MTS64_NUM_INPUT_PORTS,
  660. &rmidi);
  661. if (err < 0)
  662. return err;
  663. rmidi->private_data = mts;
  664. strcpy(rmidi->name, CARD_NAME);
  665. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  666. SNDRV_RAWMIDI_INFO_INPUT |
  667. SNDRV_RAWMIDI_INFO_DUPLEX;
  668. mts->rmidi = rmidi;
  669. /* register rawmidi ops */
  670. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  671. &snd_mts64_rawmidi_output_ops);
  672. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  673. &snd_mts64_rawmidi_input_ops);
  674. /* name substreams */
  675. /* output */
  676. list_for_each(list,
  677. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
  678. substream = list_entry(list, struct snd_rawmidi_substream, list);
  679. sprintf(substream->name,
  680. "Miditerminal %d", substream->number+1);
  681. }
  682. /* input */
  683. list_for_each(list,
  684. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
  685. substream = list_entry(list, struct snd_rawmidi_substream, list);
  686. mts->midi_input_substream[substream->number] = substream;
  687. switch(substream->number) {
  688. case MTS64_SMPTE_SUBSTREAM:
  689. strcpy(substream->name, "Miditerminal SMPTE");
  690. break;
  691. default:
  692. sprintf(substream->name,
  693. "Miditerminal %d", substream->number+1);
  694. }
  695. }
  696. /* controls */
  697. err = snd_mts64_ctl_create(card, mts);
  698. return err;
  699. }
  700. /*********************************************************************
  701. * parport stuff
  702. *********************************************************************/
  703. static void snd_mts64_interrupt(int irq, void *private)
  704. {
  705. struct mts64 *mts = ((struct snd_card*)private)->private_data;
  706. u16 ret;
  707. u8 status, data;
  708. struct snd_rawmidi_substream *substream;
  709. spin_lock(&mts->lock);
  710. ret = mts64_read(mts->pardev->port);
  711. data = ret & 0x00ff;
  712. status = ret >> 8;
  713. if (status & MTS64_STAT_PORT) {
  714. mts->current_midi_input_port = mts64_map_midi_input(data);
  715. } else {
  716. if (mts->current_midi_input_port == -1)
  717. goto __out;
  718. substream = mts->midi_input_substream[mts->current_midi_input_port];
  719. if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
  720. snd_rawmidi_receive(substream, &data, 1);
  721. }
  722. __out:
  723. spin_unlock(&mts->lock);
  724. }
  725. static int __devinit snd_mts64_probe_port(struct parport *p)
  726. {
  727. struct pardevice *pardev;
  728. int res;
  729. pardev = parport_register_device(p, DRIVER_NAME,
  730. NULL, NULL, NULL,
  731. 0, NULL);
  732. if (!pardev)
  733. return -EIO;
  734. if (parport_claim(pardev)) {
  735. parport_unregister_device(pardev);
  736. return -EIO;
  737. }
  738. res = mts64_probe(p);
  739. parport_release(pardev);
  740. parport_unregister_device(pardev);
  741. return res;
  742. }
  743. static void __devinit snd_mts64_attach(struct parport *p)
  744. {
  745. struct platform_device *device;
  746. device = platform_device_alloc(PLATFORM_DRIVER, device_count);
  747. if (!device)
  748. return;
  749. /* Temporary assignment to forward the parport */
  750. platform_set_drvdata(device, p);
  751. if (platform_device_add(device) < 0) {
  752. platform_device_put(device);
  753. return;
  754. }
  755. /* Since we dont get the return value of probe
  756. * We need to check if device probing succeeded or not */
  757. if (!platform_get_drvdata(device)) {
  758. platform_device_unregister(device);
  759. return;
  760. }
  761. /* register device in global table */
  762. platform_devices[device_count] = device;
  763. device_count++;
  764. }
  765. static void snd_mts64_detach(struct parport *p)
  766. {
  767. /* nothing to do here */
  768. }
  769. static struct parport_driver mts64_parport_driver = {
  770. .name = "mts64",
  771. .attach = snd_mts64_attach,
  772. .detach = snd_mts64_detach
  773. };
  774. /*********************************************************************
  775. * platform stuff
  776. *********************************************************************/
  777. static void snd_mts64_card_private_free(struct snd_card *card)
  778. {
  779. struct mts64 *mts = card->private_data;
  780. struct pardevice *pardev = mts->pardev;
  781. if (pardev) {
  782. if (mts->pardev_claimed)
  783. parport_release(pardev);
  784. parport_unregister_device(pardev);
  785. }
  786. snd_mts64_free(mts);
  787. }
  788. static int __devinit snd_mts64_probe(struct platform_device *pdev)
  789. {
  790. struct pardevice *pardev;
  791. struct parport *p;
  792. int dev = pdev->id;
  793. struct snd_card *card = NULL;
  794. struct mts64 *mts = NULL;
  795. int err;
  796. p = platform_get_drvdata(pdev);
  797. platform_set_drvdata(pdev, NULL);
  798. if (dev >= SNDRV_CARDS)
  799. return -ENODEV;
  800. if (!enable[dev])
  801. return -ENOENT;
  802. if ((err = snd_mts64_probe_port(p)) < 0)
  803. return err;
  804. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  805. if (card == NULL) {
  806. snd_printd("Cannot create card\n");
  807. return -ENOMEM;
  808. }
  809. strcpy(card->driver, DRIVER_NAME);
  810. strcpy(card->shortname, "ESI " CARD_NAME);
  811. sprintf(card->longname, "%s at 0x%lx, irq %i",
  812. card->shortname, p->base, p->irq);
  813. pardev = parport_register_device(p, /* port */
  814. DRIVER_NAME, /* name */
  815. NULL, /* preempt */
  816. NULL, /* wakeup */
  817. snd_mts64_interrupt, /* ISR */
  818. PARPORT_DEV_EXCL, /* flags */
  819. (void *)card); /* private */
  820. if (pardev == NULL) {
  821. snd_printd("Cannot register pardevice\n");
  822. err = -EIO;
  823. goto __err;
  824. }
  825. if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
  826. snd_printd("Cannot create main component\n");
  827. parport_unregister_device(pardev);
  828. goto __err;
  829. }
  830. card->private_data = mts;
  831. card->private_free = snd_mts64_card_private_free;
  832. if ((err = snd_mts64_rawmidi_create(card)) < 0) {
  833. snd_printd("Creating Rawmidi component failed\n");
  834. goto __err;
  835. }
  836. /* claim parport */
  837. if (parport_claim(pardev)) {
  838. snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
  839. err = -EIO;
  840. goto __err;
  841. }
  842. mts->pardev_claimed = 1;
  843. /* init device */
  844. if ((err = mts64_device_init(p)) < 0)
  845. goto __err;
  846. platform_set_drvdata(pdev, card);
  847. /* At this point card will be usable */
  848. if ((err = snd_card_register(card)) < 0) {
  849. snd_printd("Cannot register card\n");
  850. goto __err;
  851. }
  852. snd_printk("ESI Miditerminal 4140 on 0x%lx\n", p->base);
  853. return 0;
  854. __err:
  855. snd_card_free(card);
  856. return err;
  857. }
  858. static int __devexit snd_mts64_remove(struct platform_device *pdev)
  859. {
  860. struct snd_card *card = platform_get_drvdata(pdev);
  861. if (card)
  862. snd_card_free(card);
  863. return 0;
  864. }
  865. static struct platform_driver snd_mts64_driver = {
  866. .probe = snd_mts64_probe,
  867. .remove = __devexit_p(snd_mts64_remove),
  868. .driver = {
  869. .name = PLATFORM_DRIVER
  870. }
  871. };
  872. /*********************************************************************
  873. * module init stuff
  874. *********************************************************************/
  875. static void snd_mts64_unregister_all(void)
  876. {
  877. int i;
  878. for (i = 0; i < SNDRV_CARDS; ++i) {
  879. if (platform_devices[i]) {
  880. platform_device_unregister(platform_devices[i]);
  881. platform_devices[i] = NULL;
  882. }
  883. }
  884. platform_driver_unregister(&snd_mts64_driver);
  885. parport_unregister_driver(&mts64_parport_driver);
  886. }
  887. static int __init snd_mts64_module_init(void)
  888. {
  889. int err;
  890. if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
  891. return err;
  892. if (parport_register_driver(&mts64_parport_driver) != 0) {
  893. platform_driver_unregister(&snd_mts64_driver);
  894. return -EIO;
  895. }
  896. if (device_count == 0) {
  897. snd_mts64_unregister_all();
  898. return -ENODEV;
  899. }
  900. return 0;
  901. }
  902. static void __exit snd_mts64_module_exit(void)
  903. {
  904. snd_mts64_unregister_all();
  905. }
  906. module_init(snd_mts64_module_init);
  907. module_exit(snd_mts64_module_exit);