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