mts64.c 27 KB

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