portman2x4.c 24 KB

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