portman2x4.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * Driver for Midiman Portman2x4 parallel port midi interface
  3. *
  4. * Copyright (c) by Levent Guendogdu <levon@feature-it.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * ChangeLog
  21. * Jan 24 2007 Matthias Koenig <mkoenig@suse.de>
  22. * - cleanup and rewrite
  23. * Sep 30 2004 Tobias Gehrig <tobias@gehrig.tk>
  24. * - source code cleanup
  25. * Sep 03 2004 Tobias Gehrig <tobias@gehrig.tk>
  26. * - fixed compilation problem with alsa 1.0.6a (removed MODULE_CLASSES,
  27. * MODULE_PARM_SYNTAX and changed MODULE_DEVICES to
  28. * MODULE_SUPPORTED_DEVICE)
  29. * Mar 24 2004 Tobias Gehrig <tobias@gehrig.tk>
  30. * - added 2.6 kernel support
  31. * Mar 18 2004 Tobias Gehrig <tobias@gehrig.tk>
  32. * - added parport_unregister_driver to the startup routine if the driver fails to detect a portman
  33. * - added support for all 4 output ports in portman_putmidi
  34. * Mar 17 2004 Tobias Gehrig <tobias@gehrig.tk>
  35. * - added checks for opened input device in interrupt handler
  36. * Feb 20 2004 Tobias Gehrig <tobias@gehrig.tk>
  37. * - ported from alsa 0.5 to 1.0
  38. */
  39. #include <linux/init.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/parport.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/delay.h>
  44. #include <linux/slab.h>
  45. #include <linux/module.h>
  46. #include <sound/core.h>
  47. #include <sound/initval.h>
  48. #include <sound/rawmidi.h>
  49. #include <sound/control.h>
  50. #define CARD_NAME "Portman 2x4"
  51. #define DRIVER_NAME "portman"
  52. #define PLATFORM_DRIVER "snd_portman2x4"
  53. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  54. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  55. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  56. static struct platform_device *platform_devices[SNDRV_CARDS];
  57. static int device_count;
  58. module_param_array(index, int, NULL, S_IRUGO);
  59. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  60. module_param_array(id, charp, NULL, S_IRUGO);
  61. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  62. module_param_array(enable, bool, NULL, S_IRUGO);
  63. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  64. MODULE_AUTHOR("Levent Guendogdu, Tobias Gehrig, Matthias Koenig");
  65. MODULE_DESCRIPTION("Midiman Portman2x4");
  66. MODULE_LICENSE("GPL");
  67. MODULE_SUPPORTED_DEVICE("{{Midiman,Portman2x4}}");
  68. /*********************************************************************
  69. * Chip specific
  70. *********************************************************************/
  71. #define PORTMAN_NUM_INPUT_PORTS 2
  72. #define PORTMAN_NUM_OUTPUT_PORTS 4
  73. struct portman {
  74. spinlock_t reg_lock;
  75. struct snd_card *card;
  76. struct snd_rawmidi *rmidi;
  77. struct pardevice *pardev;
  78. int pardev_claimed;
  79. int open_count;
  80. int mode[PORTMAN_NUM_INPUT_PORTS];
  81. struct snd_rawmidi_substream *midi_input[PORTMAN_NUM_INPUT_PORTS];
  82. };
  83. static int portman_free(struct portman *pm)
  84. {
  85. kfree(pm);
  86. return 0;
  87. }
  88. static int portman_create(struct snd_card *card,
  89. struct pardevice *pardev,
  90. struct portman **rchip)
  91. {
  92. struct portman *pm;
  93. *rchip = NULL;
  94. pm = kzalloc(sizeof(struct portman), GFP_KERNEL);
  95. if (pm == NULL)
  96. return -ENOMEM;
  97. /* Init chip specific data */
  98. spin_lock_init(&pm->reg_lock);
  99. pm->card = card;
  100. pm->pardev = pardev;
  101. *rchip = pm;
  102. return 0;
  103. }
  104. /*********************************************************************
  105. * HW related constants
  106. *********************************************************************/
  107. /* Standard PC parallel port status register equates. */
  108. #define PP_STAT_BSY 0x80 /* Busy status. Inverted. */
  109. #define PP_STAT_ACK 0x40 /* Acknowledge. Non-Inverted. */
  110. #define PP_STAT_POUT 0x20 /* Paper Out. Non-Inverted. */
  111. #define PP_STAT_SEL 0x10 /* Select. Non-Inverted. */
  112. #define PP_STAT_ERR 0x08 /* Error. Non-Inverted. */
  113. /* Standard PC parallel port command register equates. */
  114. #define PP_CMD_IEN 0x10 /* IRQ Enable. Non-Inverted. */
  115. #define PP_CMD_SELI 0x08 /* Select Input. Inverted. */
  116. #define PP_CMD_INIT 0x04 /* Init Printer. Non-Inverted. */
  117. #define PP_CMD_FEED 0x02 /* Auto Feed. Inverted. */
  118. #define PP_CMD_STB 0x01 /* Strobe. Inverted. */
  119. /* Parallel Port Command Register as implemented by PCP2x4. */
  120. #define INT_EN PP_CMD_IEN /* Interrupt enable. */
  121. #define STROBE PP_CMD_STB /* Command strobe. */
  122. /* The parallel port command register field (b1..b3) selects the
  123. * various "registers" within the PC/P 2x4. These are the internal
  124. * address of these "registers" that must be written to the parallel
  125. * port command register.
  126. */
  127. #define RXDATA0 (0 << 1) /* PCP RxData channel 0. */
  128. #define RXDATA1 (1 << 1) /* PCP RxData channel 1. */
  129. #define GEN_CTL (2 << 1) /* PCP General Control Register. */
  130. #define SYNC_CTL (3 << 1) /* PCP Sync Control Register. */
  131. #define TXDATA0 (4 << 1) /* PCP TxData channel 0. */
  132. #define TXDATA1 (5 << 1) /* PCP TxData channel 1. */
  133. #define TXDATA2 (6 << 1) /* PCP TxData channel 2. */
  134. #define TXDATA3 (7 << 1) /* PCP TxData channel 3. */
  135. /* Parallel Port Status Register as implemented by PCP2x4. */
  136. #define ESTB PP_STAT_POUT /* Echoed strobe. */
  137. #define INT_REQ PP_STAT_ACK /* Input data int request. */
  138. #define BUSY PP_STAT_ERR /* Interface Busy. */
  139. /* Parallel Port Status Register BUSY and SELECT lines are multiplexed
  140. * between several functions. Depending on which 2x4 "register" is
  141. * currently selected (b1..b3), the BUSY and SELECT lines are
  142. * assigned as follows:
  143. *
  144. * SELECT LINE: A3 A2 A1
  145. * --------
  146. */
  147. #define RXAVAIL PP_STAT_SEL /* Rx Available, channel 0. 0 0 0 */
  148. // RXAVAIL1 PP_STAT_SEL /* Rx Available, channel 1. 0 0 1 */
  149. #define SYNC_STAT PP_STAT_SEL /* Reserved - Sync Status. 0 1 0 */
  150. // /* Reserved. 0 1 1 */
  151. #define TXEMPTY PP_STAT_SEL /* Tx Empty, channel 0. 1 0 0 */
  152. // TXEMPTY1 PP_STAT_SEL /* Tx Empty, channel 1. 1 0 1 */
  153. // TXEMPTY2 PP_STAT_SEL /* Tx Empty, channel 2. 1 1 0 */
  154. // TXEMPTY3 PP_STAT_SEL /* Tx Empty, channel 3. 1 1 1 */
  155. /* BUSY LINE: A3 A2 A1
  156. * --------
  157. */
  158. #define RXDATA PP_STAT_BSY /* Rx Input Data, channel 0. 0 0 0 */
  159. // RXDATA1 PP_STAT_BSY /* Rx Input Data, channel 1. 0 0 1 */
  160. #define SYNC_DATA PP_STAT_BSY /* Reserved - Sync Data. 0 1 0 */
  161. /* Reserved. 0 1 1 */
  162. #define DATA_ECHO PP_STAT_BSY /* Parallel Port Data Echo. 1 0 0 */
  163. #define A0_ECHO PP_STAT_BSY /* Address 0 Echo. 1 0 1 */
  164. #define A1_ECHO PP_STAT_BSY /* Address 1 Echo. 1 1 0 */
  165. #define A2_ECHO PP_STAT_BSY /* Address 2 Echo. 1 1 1 */
  166. #define PORTMAN2X4_MODE_INPUT_TRIGGERED 0x01
  167. /*********************************************************************
  168. * Hardware specific functions
  169. *********************************************************************/
  170. static inline void portman_write_command(struct portman *pm, u8 value)
  171. {
  172. parport_write_control(pm->pardev->port, value);
  173. }
  174. static inline u8 portman_read_command(struct portman *pm)
  175. {
  176. return parport_read_control(pm->pardev->port);
  177. }
  178. static inline u8 portman_read_status(struct portman *pm)
  179. {
  180. return parport_read_status(pm->pardev->port);
  181. }
  182. static inline u8 portman_read_data(struct portman *pm)
  183. {
  184. return parport_read_data(pm->pardev->port);
  185. }
  186. static inline void portman_write_data(struct portman *pm, u8 value)
  187. {
  188. parport_write_data(pm->pardev->port, value);
  189. }
  190. static void portman_write_midi(struct portman *pm,
  191. int port, u8 mididata)
  192. {
  193. int command = ((port + 4) << 1);
  194. /* Get entering data byte and port number in BL and BH respectively.
  195. * Set up Tx Channel address field for use with PP Cmd Register.
  196. * Store address field in BH register.
  197. * Inputs: AH = Output port number (0..3).
  198. * AL = Data byte.
  199. * command = TXDATA0 | INT_EN;
  200. * Align port num with address field (b1...b3),
  201. * set address for TXDatax, Strobe=0
  202. */
  203. command |= INT_EN;
  204. /* Disable interrupts so that the process is not interrupted, then
  205. * write the address associated with the current Tx channel to the
  206. * PP Command Reg. Do not set the Strobe signal yet.
  207. */
  208. do {
  209. portman_write_command(pm, command);
  210. /* While the address lines settle, write parallel output data to
  211. * PP Data Reg. This has no effect until Strobe signal is asserted.
  212. */
  213. portman_write_data(pm, mididata);
  214. /* If PCP channel's TxEmpty is set (TxEmpty is read through the PP
  215. * Status Register), then go write data. Else go back and wait.
  216. */
  217. } while ((portman_read_status(pm) & TXEMPTY) != TXEMPTY);
  218. /* TxEmpty is set. Maintain PC/P destination address and assert
  219. * Strobe through the PP Command Reg. This will Strobe data into
  220. * the PC/P transmitter and set the PC/P BUSY signal.
  221. */
  222. portman_write_command(pm, command | STROBE);
  223. /* Wait for strobe line to settle and echo back through hardware.
  224. * Once it has echoed back, assume that the address and data lines
  225. * have settled!
  226. */
  227. while ((portman_read_status(pm) & ESTB) == 0)
  228. cpu_relax();
  229. /* Release strobe and immediately re-allow interrupts. */
  230. portman_write_command(pm, command);
  231. while ((portman_read_status(pm) & ESTB) == ESTB)
  232. cpu_relax();
  233. /* PC/P BUSY is now set. We must wait until BUSY resets itself.
  234. * We'll reenable ints while we're waiting.
  235. */
  236. while ((portman_read_status(pm) & BUSY) == BUSY)
  237. cpu_relax();
  238. /* Data sent. */
  239. }
  240. /*
  241. * Read MIDI byte from port
  242. * Attempt to read input byte from specified hardware input port (0..).
  243. * Return -1 if no data
  244. */
  245. static int portman_read_midi(struct portman *pm, int port)
  246. {
  247. unsigned char midi_data = 0;
  248. unsigned char cmdout; /* Saved address+IE bit. */
  249. /* Make sure clocking edge is down before starting... */
  250. portman_write_data(pm, 0); /* Make sure edge is down. */
  251. /* Set destination address to PCP. */
  252. cmdout = (port << 1) | INT_EN; /* Address + IE + No Strobe. */
  253. portman_write_command(pm, cmdout);
  254. while ((portman_read_status(pm) & ESTB) == ESTB)
  255. cpu_relax(); /* Wait for strobe echo. */
  256. /* After the address lines settle, check multiplexed RxAvail signal.
  257. * If data is available, read it.
  258. */
  259. if ((portman_read_status(pm) & RXAVAIL) == 0)
  260. return -1; /* No data. */
  261. /* Set the Strobe signal to enable the Rx clocking circuitry. */
  262. portman_write_command(pm, cmdout | STROBE); /* Write address+IE+Strobe. */
  263. while ((portman_read_status(pm) & ESTB) == 0)
  264. cpu_relax(); /* Wait for strobe echo. */
  265. /* The first data bit (msb) is already sitting on the input line. */
  266. midi_data = (portman_read_status(pm) & 128);
  267. portman_write_data(pm, 1); /* Cause rising edge, which shifts data. */
  268. /* Data bit 6. */
  269. portman_write_data(pm, 0); /* Cause falling edge while data settles. */
  270. midi_data |= (portman_read_status(pm) >> 1) & 64;
  271. portman_write_data(pm, 1); /* Cause rising edge, which shifts data. */
  272. /* Data bit 5. */
  273. portman_write_data(pm, 0); /* Cause falling edge while data settles. */
  274. midi_data |= (portman_read_status(pm) >> 2) & 32;
  275. portman_write_data(pm, 1); /* Cause rising edge, which shifts data. */
  276. /* Data bit 4. */
  277. portman_write_data(pm, 0); /* Cause falling edge while data settles. */
  278. midi_data |= (portman_read_status(pm) >> 3) & 16;
  279. portman_write_data(pm, 1); /* Cause rising edge, which shifts data. */
  280. /* Data bit 3. */
  281. portman_write_data(pm, 0); /* Cause falling edge while data settles. */
  282. midi_data |= (portman_read_status(pm) >> 4) & 8;
  283. portman_write_data(pm, 1); /* Cause rising edge, which shifts data. */
  284. /* Data bit 2. */
  285. portman_write_data(pm, 0); /* Cause falling edge while data settles. */
  286. midi_data |= (portman_read_status(pm) >> 5) & 4;
  287. portman_write_data(pm, 1); /* Cause rising edge, which shifts data. */
  288. /* Data bit 1. */
  289. portman_write_data(pm, 0); /* Cause falling edge while data settles. */
  290. midi_data |= (portman_read_status(pm) >> 6) & 2;
  291. portman_write_data(pm, 1); /* Cause rising edge, which shifts data. */
  292. /* Data bit 0. */
  293. portman_write_data(pm, 0); /* Cause falling edge while data settles. */
  294. midi_data |= (portman_read_status(pm) >> 7) & 1;
  295. portman_write_data(pm, 1); /* Cause rising edge, which shifts data. */
  296. portman_write_data(pm, 0); /* Return data clock low. */
  297. /* De-assert Strobe and return data. */
  298. portman_write_command(pm, cmdout); /* Output saved address+IE. */
  299. /* Wait for strobe echo. */
  300. while ((portman_read_status(pm) & ESTB) == ESTB)
  301. cpu_relax();
  302. return (midi_data & 255); /* Shift back and return value. */
  303. }
  304. /*
  305. * Checks if any input data on the given channel is available
  306. * Checks RxAvail
  307. */
  308. static int portman_data_avail(struct portman *pm, int channel)
  309. {
  310. int command = INT_EN;
  311. switch (channel) {
  312. case 0:
  313. command |= RXDATA0;
  314. break;
  315. case 1:
  316. command |= RXDATA1;
  317. break;
  318. }
  319. /* Write hardware (assumme STROBE=0) */
  320. portman_write_command(pm, command);
  321. /* Check multiplexed RxAvail signal */
  322. if ((portman_read_status(pm) & RXAVAIL) == RXAVAIL)
  323. return 1; /* Data available */
  324. /* No Data available */
  325. return 0;
  326. }
  327. /*
  328. * Flushes any input
  329. */
  330. static void portman_flush_input(struct portman *pm, unsigned char port)
  331. {
  332. /* Local variable for counting things */
  333. unsigned int i = 0;
  334. unsigned char command = 0;
  335. switch (port) {
  336. case 0:
  337. command = RXDATA0;
  338. break;
  339. case 1:
  340. command = RXDATA1;
  341. break;
  342. default:
  343. snd_printk(KERN_WARNING
  344. "portman_flush_input() Won't flush port %i\n",
  345. port);
  346. return;
  347. }
  348. /* Set address for specified channel in port and allow to settle. */
  349. portman_write_command(pm, command);
  350. /* Assert the Strobe and wait for echo back. */
  351. portman_write_command(pm, command | STROBE);
  352. /* Wait for ESTB */
  353. while ((portman_read_status(pm) & ESTB) == 0)
  354. cpu_relax();
  355. /* Output clock cycles to the Rx circuitry. */
  356. portman_write_data(pm, 0);
  357. /* Flush 250 bits... */
  358. for (i = 0; i < 250; i++) {
  359. portman_write_data(pm, 1);
  360. portman_write_data(pm, 0);
  361. }
  362. /* Deassert the Strobe signal of the port and wait for it to settle. */
  363. portman_write_command(pm, command | INT_EN);
  364. /* Wait for settling */
  365. while ((portman_read_status(pm) & ESTB) == ESTB)
  366. cpu_relax();
  367. }
  368. static int portman_probe(struct parport *p)
  369. {
  370. /* Initialize the parallel port data register. Will set Rx clocks
  371. * low in case we happen to be addressing the Rx ports at this time.
  372. */
  373. /* 1 */
  374. parport_write_data(p, 0);
  375. /* Initialize the parallel port command register, thus initializing
  376. * hardware handshake lines to midi box:
  377. *
  378. * Strobe = 0
  379. * Interrupt Enable = 0
  380. */
  381. /* 2 */
  382. parport_write_control(p, 0);
  383. /* Check if Portman PC/P 2x4 is out there. */
  384. /* 3 */
  385. parport_write_control(p, RXDATA0); /* Write Strobe=0 to command reg. */
  386. /* Check for ESTB to be clear */
  387. /* 4 */
  388. if ((parport_read_status(p) & ESTB) == ESTB)
  389. return 1; /* CODE 1 - Strobe Failure. */
  390. /* Set for RXDATA0 where no damage will be done. */
  391. /* 5 */
  392. parport_write_control(p, RXDATA0 + STROBE); /* Write Strobe=1 to command reg. */
  393. /* 6 */
  394. if ((parport_read_status(p) & ESTB) != ESTB)
  395. return 1; /* CODE 1 - Strobe Failure. */
  396. /* 7 */
  397. parport_write_control(p, 0); /* Reset Strobe=0. */
  398. /* Check if Tx circuitry is functioning properly. If initialized
  399. * unit TxEmpty is false, send out char and see if if goes true.
  400. */
  401. /* 8 */
  402. parport_write_control(p, TXDATA0); /* Tx channel 0, strobe off. */
  403. /* If PCP channel's TxEmpty is set (TxEmpty is read through the PP
  404. * Status Register), then go write data. Else go back and wait.
  405. */
  406. /* 9 */
  407. if ((parport_read_status(p) & TXEMPTY) == 0)
  408. return 2;
  409. /* Return OK status. */
  410. return 0;
  411. }
  412. static int portman_device_init(struct portman *pm)
  413. {
  414. portman_flush_input(pm, 0);
  415. portman_flush_input(pm, 1);
  416. return 0;
  417. }
  418. /*********************************************************************
  419. * Rawmidi
  420. *********************************************************************/
  421. static int snd_portman_midi_open(struct snd_rawmidi_substream *substream)
  422. {
  423. return 0;
  424. }
  425. static int snd_portman_midi_close(struct snd_rawmidi_substream *substream)
  426. {
  427. return 0;
  428. }
  429. static void snd_portman_midi_input_trigger(struct snd_rawmidi_substream *substream,
  430. int up)
  431. {
  432. struct portman *pm = substream->rmidi->private_data;
  433. unsigned long flags;
  434. spin_lock_irqsave(&pm->reg_lock, flags);
  435. if (up)
  436. pm->mode[substream->number] |= PORTMAN2X4_MODE_INPUT_TRIGGERED;
  437. else
  438. pm->mode[substream->number] &= ~PORTMAN2X4_MODE_INPUT_TRIGGERED;
  439. spin_unlock_irqrestore(&pm->reg_lock, flags);
  440. }
  441. static void snd_portman_midi_output_trigger(struct snd_rawmidi_substream *substream,
  442. int up)
  443. {
  444. struct portman *pm = substream->rmidi->private_data;
  445. unsigned long flags;
  446. unsigned char byte;
  447. spin_lock_irqsave(&pm->reg_lock, flags);
  448. if (up) {
  449. while ((snd_rawmidi_transmit(substream, &byte, 1) == 1))
  450. portman_write_midi(pm, substream->number, byte);
  451. }
  452. spin_unlock_irqrestore(&pm->reg_lock, flags);
  453. }
  454. static struct snd_rawmidi_ops snd_portman_midi_output = {
  455. .open = snd_portman_midi_open,
  456. .close = snd_portman_midi_close,
  457. .trigger = snd_portman_midi_output_trigger,
  458. };
  459. static struct snd_rawmidi_ops snd_portman_midi_input = {
  460. .open = snd_portman_midi_open,
  461. .close = snd_portman_midi_close,
  462. .trigger = snd_portman_midi_input_trigger,
  463. };
  464. /* Create and initialize the rawmidi component */
  465. static int snd_portman_rawmidi_create(struct snd_card *card)
  466. {
  467. struct portman *pm = card->private_data;
  468. struct snd_rawmidi *rmidi;
  469. struct snd_rawmidi_substream *substream;
  470. int err;
  471. err = snd_rawmidi_new(card, CARD_NAME, 0,
  472. PORTMAN_NUM_OUTPUT_PORTS,
  473. PORTMAN_NUM_INPUT_PORTS,
  474. &rmidi);
  475. if (err < 0)
  476. return err;
  477. rmidi->private_data = pm;
  478. strcpy(rmidi->name, CARD_NAME);
  479. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  480. SNDRV_RAWMIDI_INFO_INPUT |
  481. SNDRV_RAWMIDI_INFO_DUPLEX;
  482. pm->rmidi = rmidi;
  483. /* register rawmidi ops */
  484. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  485. &snd_portman_midi_output);
  486. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  487. &snd_portman_midi_input);
  488. /* name substreams */
  489. /* output */
  490. list_for_each_entry(substream,
  491. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
  492. list) {
  493. sprintf(substream->name,
  494. "Portman2x4 %d", substream->number+1);
  495. }
  496. /* input */
  497. list_for_each_entry(substream,
  498. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
  499. list) {
  500. pm->midi_input[substream->number] = substream;
  501. sprintf(substream->name,
  502. "Portman2x4 %d", substream->number+1);
  503. }
  504. return err;
  505. }
  506. /*********************************************************************
  507. * parport stuff
  508. *********************************************************************/
  509. static void snd_portman_interrupt(void *userdata)
  510. {
  511. unsigned char midivalue = 0;
  512. struct portman *pm = ((struct snd_card*)userdata)->private_data;
  513. spin_lock(&pm->reg_lock);
  514. /* While any input data is waiting */
  515. while ((portman_read_status(pm) & INT_REQ) == INT_REQ) {
  516. /* If data available on channel 0,
  517. read it and stuff it into the queue. */
  518. if (portman_data_avail(pm, 0)) {
  519. /* Read Midi */
  520. midivalue = portman_read_midi(pm, 0);
  521. /* put midi into queue... */
  522. if (pm->mode[0] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
  523. snd_rawmidi_receive(pm->midi_input[0],
  524. &midivalue, 1);
  525. }
  526. /* If data available on channel 1,
  527. read it and stuff it into the queue. */
  528. if (portman_data_avail(pm, 1)) {
  529. /* Read Midi */
  530. midivalue = portman_read_midi(pm, 1);
  531. /* put midi into queue... */
  532. if (pm->mode[1] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
  533. snd_rawmidi_receive(pm->midi_input[1],
  534. &midivalue, 1);
  535. }
  536. }
  537. spin_unlock(&pm->reg_lock);
  538. }
  539. static int snd_portman_probe_port(struct parport *p)
  540. {
  541. struct pardevice *pardev;
  542. int res;
  543. pardev = parport_register_device(p, DRIVER_NAME,
  544. NULL, NULL, NULL,
  545. 0, NULL);
  546. if (!pardev)
  547. return -EIO;
  548. if (parport_claim(pardev)) {
  549. parport_unregister_device(pardev);
  550. return -EIO;
  551. }
  552. res = portman_probe(p);
  553. parport_release(pardev);
  554. parport_unregister_device(pardev);
  555. return res ? -EIO : 0;
  556. }
  557. static void snd_portman_attach(struct parport *p)
  558. {
  559. struct platform_device *device;
  560. device = platform_device_alloc(PLATFORM_DRIVER, device_count);
  561. if (!device)
  562. return;
  563. /* Temporary assignment to forward the parport */
  564. platform_set_drvdata(device, p);
  565. if (platform_device_add(device) < 0) {
  566. platform_device_put(device);
  567. return;
  568. }
  569. /* Since we dont get the return value of probe
  570. * We need to check if device probing succeeded or not */
  571. if (!platform_get_drvdata(device)) {
  572. platform_device_unregister(device);
  573. return;
  574. }
  575. /* register device in global table */
  576. platform_devices[device_count] = device;
  577. device_count++;
  578. }
  579. static void snd_portman_detach(struct parport *p)
  580. {
  581. /* nothing to do here */
  582. }
  583. static struct parport_driver portman_parport_driver = {
  584. .name = "portman2x4",
  585. .attach = snd_portman_attach,
  586. .detach = snd_portman_detach
  587. };
  588. /*********************************************************************
  589. * platform stuff
  590. *********************************************************************/
  591. static void snd_portman_card_private_free(struct snd_card *card)
  592. {
  593. struct portman *pm = card->private_data;
  594. struct pardevice *pardev = pm->pardev;
  595. if (pardev) {
  596. if (pm->pardev_claimed)
  597. parport_release(pardev);
  598. parport_unregister_device(pardev);
  599. }
  600. portman_free(pm);
  601. }
  602. static int snd_portman_probe(struct platform_device *pdev)
  603. {
  604. struct pardevice *pardev;
  605. struct parport *p;
  606. int dev = pdev->id;
  607. struct snd_card *card = NULL;
  608. struct portman *pm = NULL;
  609. int err;
  610. p = platform_get_drvdata(pdev);
  611. platform_set_drvdata(pdev, NULL);
  612. if (dev >= SNDRV_CARDS)
  613. return -ENODEV;
  614. if (!enable[dev])
  615. return -ENOENT;
  616. if ((err = snd_portman_probe_port(p)) < 0)
  617. return err;
  618. err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
  619. 0, &card);
  620. if (err < 0) {
  621. snd_printd("Cannot create card\n");
  622. return err;
  623. }
  624. strcpy(card->driver, DRIVER_NAME);
  625. strcpy(card->shortname, CARD_NAME);
  626. sprintf(card->longname, "%s at 0x%lx, irq %i",
  627. card->shortname, p->base, p->irq);
  628. pardev = parport_register_device(p, /* port */
  629. DRIVER_NAME, /* name */
  630. NULL, /* preempt */
  631. NULL, /* wakeup */
  632. snd_portman_interrupt, /* ISR */
  633. PARPORT_DEV_EXCL, /* flags */
  634. (void *)card); /* private */
  635. if (pardev == NULL) {
  636. snd_printd("Cannot register pardevice\n");
  637. err = -EIO;
  638. goto __err;
  639. }
  640. if ((err = portman_create(card, pardev, &pm)) < 0) {
  641. snd_printd("Cannot create main component\n");
  642. parport_unregister_device(pardev);
  643. goto __err;
  644. }
  645. card->private_data = pm;
  646. card->private_free = snd_portman_card_private_free;
  647. if ((err = snd_portman_rawmidi_create(card)) < 0) {
  648. snd_printd("Creating Rawmidi component failed\n");
  649. goto __err;
  650. }
  651. /* claim parport */
  652. if (parport_claim(pardev)) {
  653. snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
  654. err = -EIO;
  655. goto __err;
  656. }
  657. pm->pardev_claimed = 1;
  658. /* init device */
  659. if ((err = portman_device_init(pm)) < 0)
  660. goto __err;
  661. platform_set_drvdata(pdev, card);
  662. /* At this point card will be usable */
  663. if ((err = snd_card_register(card)) < 0) {
  664. snd_printd("Cannot register card\n");
  665. goto __err;
  666. }
  667. snd_printk(KERN_INFO "Portman 2x4 on 0x%lx\n", p->base);
  668. return 0;
  669. __err:
  670. snd_card_free(card);
  671. return err;
  672. }
  673. static int snd_portman_remove(struct platform_device *pdev)
  674. {
  675. struct snd_card *card = platform_get_drvdata(pdev);
  676. if (card)
  677. snd_card_free(card);
  678. return 0;
  679. }
  680. static struct platform_driver snd_portman_driver = {
  681. .probe = snd_portman_probe,
  682. .remove = snd_portman_remove,
  683. .driver = {
  684. .name = PLATFORM_DRIVER,
  685. }
  686. };
  687. /*********************************************************************
  688. * module init stuff
  689. *********************************************************************/
  690. static void snd_portman_unregister_all(void)
  691. {
  692. int i;
  693. for (i = 0; i < SNDRV_CARDS; ++i) {
  694. if (platform_devices[i]) {
  695. platform_device_unregister(platform_devices[i]);
  696. platform_devices[i] = NULL;
  697. }
  698. }
  699. platform_driver_unregister(&snd_portman_driver);
  700. parport_unregister_driver(&portman_parport_driver);
  701. }
  702. static int __init snd_portman_module_init(void)
  703. {
  704. int err;
  705. if ((err = platform_driver_register(&snd_portman_driver)) < 0)
  706. return err;
  707. if (parport_register_driver(&portman_parport_driver) != 0) {
  708. platform_driver_unregister(&snd_portman_driver);
  709. return -EIO;
  710. }
  711. if (device_count == 0) {
  712. snd_portman_unregister_all();
  713. return -ENODEV;
  714. }
  715. return 0;
  716. }
  717. static void __exit snd_portman_module_exit(void)
  718. {
  719. snd_portman_unregister_all();
  720. }
  721. module_init(snd_portman_module_init);
  722. module_exit(snd_portman_module_exit);