portman2x4.c 25 KB

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