mts64.c 26 KB

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