portman2x4.c 24 KB

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