PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/drivers/portman2x4.c

https://github.com/mstsirkin/kvm
C | 878 lines | 513 code | 157 blank | 208 comment | 70 complexity | 10d27f3de68e5ab4eeff56c0647e0491 MD5 | raw file
  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 <sound/core.h>
  46. #include <sound/initval.h>
  47. #include <sound/rawmidi.h>
  48. #include <sound/control.h>
  49. #define CARD_NAME "Portman 2x4"
  50. #define DRIVER_NAME "portman"
  51. #define PLATFORM_DRIVER "snd_portman2x4"
  52. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  53. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  54. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  55. static struct platform_device *platform_devices[SNDRV_CARDS];
  56. static int device_count;
  57. module_param_array(index, int, NULL, S_IRUGO);
  58. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  59. module_param_array(id, charp, NULL, S_IRUGO);
  60. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  61. module_param_array(enable, bool, NULL, S_IRUGO);
  62. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  63. MODULE_AUTHOR("Levent Guendogdu, Tobias Gehrig, Matthias Koenig");
  64. MODULE_DESCRIPTION("Midiman Portman2x4");
  65. MODULE_LICENSE("GPL");
  66. MODULE_SUPPORTED_DEVICE("{{Midiman,Portman2x4}}");
  67. /*********************************************************************
  68. * Chip specific
  69. *********************************************************************/
  70. #define PORTMAN_NUM_INPUT_PORTS 2
  71. #define PORTMAN_NUM_OUTPUT_PORTS 4
  72. struct portman {
  73. spinlock_t reg_lock;
  74. struct snd_card *card;
  75. struct snd_rawmidi *rmidi;
  76. struct pardevice *pardev;
  77. int pardev_claimed;
  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 __devinit 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 __devinit 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 int __devinit snd_portman_probe_port(struct parport *p)
  539. {
  540. struct pardevice *pardev;
  541. int res;
  542. pardev = parport_register_device(p, DRIVER_NAME,
  543. NULL, NULL, NULL,
  544. 0, NULL);
  545. if (!pardev)
  546. return -EIO;
  547. if (parport_claim(pardev)) {
  548. parport_unregister_device(pardev);
  549. return -EIO;
  550. }
  551. res = portman_probe(p);
  552. parport_release(pardev);
  553. parport_unregister_device(pardev);
  554. return res ? -EIO : 0;
  555. }
  556. static void __devinit snd_portman_attach(struct parport *p)
  557. {
  558. struct platform_device *device;
  559. device = platform_device_alloc(PLATFORM_DRIVER, device_count);
  560. if (!device)
  561. return;
  562. /* Temporary assignment to forward the parport */
  563. platform_set_drvdata(device, p);
  564. if (platform_device_add(device) < 0) {
  565. platform_device_put(device);
  566. return;
  567. }
  568. /* Since we dont get the return value of probe
  569. * We need to check if device probing succeeded or not */
  570. if (!platform_get_drvdata(device)) {
  571. platform_device_unregister(device);
  572. return;
  573. }
  574. /* register device in global table */
  575. platform_devices[device_count] = device;
  576. device_count++;
  577. }
  578. static void snd_portman_detach(struct parport *p)
  579. {
  580. /* nothing to do here */
  581. }
  582. static struct parport_driver portman_parport_driver = {
  583. .name = "portman2x4",
  584. .attach = snd_portman_attach,
  585. .detach = snd_portman_detach
  586. };
  587. /*********************************************************************
  588. * platform stuff
  589. *********************************************************************/
  590. static void snd_portman_card_private_free(struct snd_card *card)
  591. {
  592. struct portman *pm = card->private_data;
  593. struct pardevice *pardev = pm->pardev;
  594. if (pardev) {
  595. if (pm->pardev_claimed)
  596. parport_release(pardev);
  597. parport_unregister_device(pardev);
  598. }
  599. portman_free(pm);
  600. }
  601. static int __devinit snd_portman_probe(struct platform_device *pdev)
  602. {
  603. struct pardevice *pardev;
  604. struct parport *p;
  605. int dev = pdev->id;
  606. struct snd_card *card = NULL;
  607. struct portman *pm = NULL;
  608. int err;
  609. p = platform_get_drvdata(pdev);
  610. platform_set_drvdata(pdev, NULL);
  611. if (dev >= SNDRV_CARDS)
  612. return -ENODEV;
  613. if (!enable[dev])
  614. return -ENOENT;
  615. if ((err = snd_portman_probe_port(p)) < 0)
  616. return err;
  617. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  618. if (err < 0) {
  619. snd_printd("Cannot create card\n");
  620. return err;
  621. }
  622. strcpy(card->driver, DRIVER_NAME);
  623. strcpy(card->shortname, CARD_NAME);
  624. sprintf(card->longname, "%s at 0x%lx, irq %i",
  625. card->shortname, p->base, p->irq);
  626. pardev = parport_register_device(p, /* port */
  627. DRIVER_NAME, /* name */
  628. NULL, /* preempt */
  629. NULL, /* wakeup */
  630. snd_portman_interrupt, /* ISR */
  631. PARPORT_DEV_EXCL, /* flags */
  632. (void *)card); /* private */
  633. if (pardev == NULL) {
  634. snd_printd("Cannot register pardevice\n");
  635. err = -EIO;
  636. goto __err;
  637. }
  638. if ((err = portman_create(card, pardev, &pm)) < 0) {
  639. snd_printd("Cannot create main component\n");
  640. parport_unregister_device(pardev);
  641. goto __err;
  642. }
  643. card->private_data = pm;
  644. card->private_free = snd_portman_card_private_free;
  645. if ((err = snd_portman_rawmidi_create(card)) < 0) {
  646. snd_printd("Creating Rawmidi component failed\n");
  647. goto __err;
  648. }
  649. /* claim parport */
  650. if (parport_claim(pardev)) {
  651. snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
  652. err = -EIO;
  653. goto __err;
  654. }
  655. pm->pardev_claimed = 1;
  656. /* init device */
  657. if ((err = portman_device_init(pm)) < 0)
  658. goto __err;
  659. platform_set_drvdata(pdev, card);
  660. snd_card_set_dev(card, &pdev->dev);
  661. /* At this point card will be usable */
  662. if ((err = snd_card_register(card)) < 0) {
  663. snd_printd("Cannot register card\n");
  664. goto __err;
  665. }
  666. snd_printk(KERN_INFO "Portman 2x4 on 0x%lx\n", p->base);
  667. return 0;
  668. __err:
  669. snd_card_free(card);
  670. return err;
  671. }
  672. static int __devexit snd_portman_remove(struct platform_device *pdev)
  673. {
  674. struct snd_card *card = platform_get_drvdata(pdev);
  675. if (card)
  676. snd_card_free(card);
  677. return 0;
  678. }
  679. static struct platform_driver snd_portman_driver = {
  680. .probe = snd_portman_probe,
  681. .remove = __devexit_p(snd_portman_remove),
  682. .driver = {
  683. .name = PLATFORM_DRIVER
  684. }
  685. };
  686. /*********************************************************************
  687. * module init stuff
  688. *********************************************************************/
  689. static void snd_portman_unregister_all(void)
  690. {
  691. int i;
  692. for (i = 0; i < SNDRV_CARDS; ++i) {
  693. if (platform_devices[i]) {
  694. platform_device_unregister(platform_devices[i]);
  695. platform_devices[i] = NULL;
  696. }
  697. }
  698. platform_driver_unregister(&snd_portman_driver);
  699. parport_unregister_driver(&portman_parport_driver);
  700. }
  701. static int __init snd_portman_module_init(void)
  702. {
  703. int err;
  704. if ((err = platform_driver_register(&snd_portman_driver)) < 0)
  705. return err;
  706. if (parport_register_driver(&portman_parport_driver) != 0) {
  707. platform_driver_unregister(&snd_portman_driver);
  708. return -EIO;
  709. }
  710. if (device_count == 0) {
  711. snd_portman_unregister_all();
  712. return -ENODEV;
  713. }
  714. return 0;
  715. }
  716. static void __exit snd_portman_module_exit(void)
  717. {
  718. snd_portman_unregister_all();
  719. }
  720. module_init(snd_portman_module_init);
  721. module_exit(snd_portman_module_exit);