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