portman2x4.c 25 KB

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