xref: /openbmc/linux/sound/drivers/portman2x4.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2757e119bSMatthias Koenig /*
3757e119bSMatthias Koenig  *   Driver for Midiman Portman2x4 parallel port midi interface
4757e119bSMatthias Koenig  *
5757e119bSMatthias Koenig  *   Copyright (c) by Levent Guendogdu <levon@feature-it.com>
6757e119bSMatthias Koenig  *
7757e119bSMatthias Koenig  * ChangeLog
8757e119bSMatthias Koenig  * Jan 24 2007 Matthias Koenig <mkoenig@suse.de>
9757e119bSMatthias Koenig  *      - cleanup and rewrite
10757e119bSMatthias Koenig  * Sep 30 2004 Tobias Gehrig <tobias@gehrig.tk>
11757e119bSMatthias Koenig  *      - source code cleanup
12757e119bSMatthias Koenig  * Sep 03 2004 Tobias Gehrig <tobias@gehrig.tk>
13757e119bSMatthias Koenig  *      - fixed compilation problem with alsa 1.0.6a (removed MODULE_CLASSES,
14757e119bSMatthias Koenig  *        MODULE_PARM_SYNTAX and changed MODULE_DEVICES to
15757e119bSMatthias Koenig  *        MODULE_SUPPORTED_DEVICE)
16757e119bSMatthias Koenig  * Mar 24 2004 Tobias Gehrig <tobias@gehrig.tk>
17757e119bSMatthias Koenig  *      - added 2.6 kernel support
18757e119bSMatthias Koenig  * Mar 18 2004 Tobias Gehrig <tobias@gehrig.tk>
19757e119bSMatthias Koenig  *      - added parport_unregister_driver to the startup routine if the driver fails to detect a portman
20757e119bSMatthias Koenig  *      - added support for all 4 output ports in portman_putmidi
21757e119bSMatthias Koenig  * Mar 17 2004 Tobias Gehrig <tobias@gehrig.tk>
22757e119bSMatthias Koenig  *      - added checks for opened input device in interrupt handler
23757e119bSMatthias Koenig  * Feb 20 2004 Tobias Gehrig <tobias@gehrig.tk>
24757e119bSMatthias Koenig  *      - ported from alsa 0.5 to 1.0
25757e119bSMatthias Koenig  */
26757e119bSMatthias Koenig 
27757e119bSMatthias Koenig #include <linux/init.h>
28757e119bSMatthias Koenig #include <linux/platform_device.h>
29757e119bSMatthias Koenig #include <linux/parport.h>
30757e119bSMatthias Koenig #include <linux/spinlock.h>
31757e119bSMatthias Koenig #include <linux/delay.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33da155d5bSPaul Gortmaker #include <linux/module.h>
34757e119bSMatthias Koenig #include <sound/core.h>
35757e119bSMatthias Koenig #include <sound/initval.h>
36757e119bSMatthias Koenig #include <sound/rawmidi.h>
37757e119bSMatthias Koenig #include <sound/control.h>
38757e119bSMatthias Koenig 
39757e119bSMatthias Koenig #define CARD_NAME "Portman 2x4"
40757e119bSMatthias Koenig #define DRIVER_NAME "portman"
41757e119bSMatthias Koenig #define PLATFORM_DRIVER "snd_portman2x4"
42757e119bSMatthias Koenig 
43757e119bSMatthias Koenig static int index[SNDRV_CARDS]  = SNDRV_DEFAULT_IDX;
44757e119bSMatthias Koenig static char *id[SNDRV_CARDS]   = SNDRV_DEFAULT_STR;
45a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
46757e119bSMatthias Koenig 
47757e119bSMatthias Koenig static struct platform_device *platform_devices[SNDRV_CARDS];
48757e119bSMatthias Koenig static int device_count;
49757e119bSMatthias Koenig 
506a73cf46SJoe Perches module_param_array(index, int, NULL, 0444);
51757e119bSMatthias Koenig MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
526a73cf46SJoe Perches module_param_array(id, charp, NULL, 0444);
53757e119bSMatthias Koenig MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
546a73cf46SJoe Perches module_param_array(enable, bool, NULL, 0444);
55757e119bSMatthias Koenig MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
56757e119bSMatthias Koenig 
57757e119bSMatthias Koenig MODULE_AUTHOR("Levent Guendogdu, Tobias Gehrig, Matthias Koenig");
58757e119bSMatthias Koenig MODULE_DESCRIPTION("Midiman Portman2x4");
59757e119bSMatthias Koenig MODULE_LICENSE("GPL");
60757e119bSMatthias Koenig 
61757e119bSMatthias Koenig /*********************************************************************
62757e119bSMatthias Koenig  * Chip specific
63757e119bSMatthias Koenig  *********************************************************************/
64757e119bSMatthias Koenig #define PORTMAN_NUM_INPUT_PORTS 2
65757e119bSMatthias Koenig #define PORTMAN_NUM_OUTPUT_PORTS 4
66757e119bSMatthias Koenig 
67757e119bSMatthias Koenig struct portman {
68757e119bSMatthias Koenig 	spinlock_t reg_lock;
69757e119bSMatthias Koenig 	struct snd_card *card;
70757e119bSMatthias Koenig 	struct snd_rawmidi *rmidi;
71757e119bSMatthias Koenig 	struct pardevice *pardev;
72757e119bSMatthias Koenig 	int open_count;
73757e119bSMatthias Koenig 	int mode[PORTMAN_NUM_INPUT_PORTS];
74757e119bSMatthias Koenig 	struct snd_rawmidi_substream *midi_input[PORTMAN_NUM_INPUT_PORTS];
75757e119bSMatthias Koenig };
76757e119bSMatthias Koenig 
portman_free(struct portman * pm)77757e119bSMatthias Koenig static int portman_free(struct portman *pm)
78757e119bSMatthias Koenig {
79757e119bSMatthias Koenig 	kfree(pm);
80757e119bSMatthias Koenig 	return 0;
81757e119bSMatthias Koenig }
82757e119bSMatthias Koenig 
portman_create(struct snd_card * card,struct pardevice * pardev,struct portman ** rchip)83fbbb01a1SBill Pemberton static int portman_create(struct snd_card *card,
84757e119bSMatthias Koenig 			  struct pardevice *pardev,
85757e119bSMatthias Koenig 			  struct portman **rchip)
86757e119bSMatthias Koenig {
87757e119bSMatthias Koenig 	struct portman *pm;
88757e119bSMatthias Koenig 
89757e119bSMatthias Koenig 	*rchip = NULL;
90757e119bSMatthias Koenig 
91757e119bSMatthias Koenig 	pm = kzalloc(sizeof(struct portman), GFP_KERNEL);
92757e119bSMatthias Koenig 	if (pm == NULL)
93757e119bSMatthias Koenig 		return -ENOMEM;
94757e119bSMatthias Koenig 
95757e119bSMatthias Koenig 	/* Init chip specific data */
96757e119bSMatthias Koenig 	spin_lock_init(&pm->reg_lock);
97757e119bSMatthias Koenig 	pm->card = card;
98757e119bSMatthias Koenig 	pm->pardev = pardev;
99757e119bSMatthias Koenig 
100757e119bSMatthias Koenig 	*rchip = pm;
101757e119bSMatthias Koenig 
102757e119bSMatthias Koenig 	return 0;
103757e119bSMatthias Koenig }
104757e119bSMatthias Koenig 
105757e119bSMatthias Koenig /*********************************************************************
106757e119bSMatthias Koenig  * HW related constants
107757e119bSMatthias Koenig  *********************************************************************/
108757e119bSMatthias Koenig 
109757e119bSMatthias Koenig /* Standard PC parallel port status register equates. */
110757e119bSMatthias Koenig #define	PP_STAT_BSY   	0x80	/* Busy status.  Inverted. */
111757e119bSMatthias Koenig #define	PP_STAT_ACK   	0x40	/* Acknowledge.  Non-Inverted. */
112757e119bSMatthias Koenig #define	PP_STAT_POUT  	0x20	/* Paper Out.    Non-Inverted. */
113757e119bSMatthias Koenig #define	PP_STAT_SEL   	0x10	/* Select.       Non-Inverted. */
114757e119bSMatthias Koenig #define	PP_STAT_ERR   	0x08	/* Error.        Non-Inverted. */
115757e119bSMatthias Koenig 
116757e119bSMatthias Koenig /* Standard PC parallel port command register equates. */
117757e119bSMatthias Koenig #define	PP_CMD_IEN  	0x10	/* IRQ Enable.   Non-Inverted. */
118757e119bSMatthias Koenig #define	PP_CMD_SELI 	0x08	/* Select Input. Inverted. */
119757e119bSMatthias Koenig #define	PP_CMD_INIT 	0x04	/* Init Printer. Non-Inverted. */
120757e119bSMatthias Koenig #define	PP_CMD_FEED 	0x02	/* Auto Feed.    Inverted. */
121757e119bSMatthias Koenig #define	PP_CMD_STB      0x01	/* Strobe.       Inverted. */
122757e119bSMatthias Koenig 
123757e119bSMatthias Koenig /* Parallel Port Command Register as implemented by PCP2x4. */
124757e119bSMatthias Koenig #define	INT_EN	 	PP_CMD_IEN	/* Interrupt enable. */
125757e119bSMatthias Koenig #define	STROBE	        PP_CMD_STB	/* Command strobe. */
126757e119bSMatthias Koenig 
127757e119bSMatthias Koenig /* The parallel port command register field (b1..b3) selects the
128757e119bSMatthias Koenig  * various "registers" within the PC/P 2x4.  These are the internal
129757e119bSMatthias Koenig  * address of these "registers" that must be written to the parallel
130757e119bSMatthias Koenig  * port command register.
131757e119bSMatthias Koenig  */
132757e119bSMatthias Koenig #define	RXDATA0		(0 << 1)	/* PCP RxData channel 0. */
133757e119bSMatthias Koenig #define	RXDATA1		(1 << 1)	/* PCP RxData channel 1. */
134757e119bSMatthias Koenig #define	GEN_CTL		(2 << 1)	/* PCP General Control Register. */
135757e119bSMatthias Koenig #define	SYNC_CTL 	(3 << 1)	/* PCP Sync Control Register. */
136757e119bSMatthias Koenig #define	TXDATA0		(4 << 1)	/* PCP TxData channel 0. */
137757e119bSMatthias Koenig #define	TXDATA1		(5 << 1)	/* PCP TxData channel 1. */
138757e119bSMatthias Koenig #define	TXDATA2		(6 << 1)	/* PCP TxData channel 2. */
139757e119bSMatthias Koenig #define	TXDATA3		(7 << 1)	/* PCP TxData channel 3. */
140757e119bSMatthias Koenig 
141757e119bSMatthias Koenig /* Parallel Port Status Register as implemented by PCP2x4. */
142757e119bSMatthias Koenig #define	ESTB		PP_STAT_POUT	/* Echoed strobe. */
143757e119bSMatthias Koenig #define	INT_REQ         PP_STAT_ACK	/* Input data int request. */
144757e119bSMatthias Koenig #define	BUSY            PP_STAT_ERR	/* Interface Busy. */
145757e119bSMatthias Koenig 
146757e119bSMatthias Koenig /* Parallel Port Status Register BUSY and SELECT lines are multiplexed
147757e119bSMatthias Koenig  * between several functions.  Depending on which 2x4 "register" is
148757e119bSMatthias Koenig  * currently selected (b1..b3), the BUSY and SELECT lines are
149757e119bSMatthias Koenig  * assigned as follows:
150757e119bSMatthias Koenig  *
151757e119bSMatthias Koenig  *   SELECT LINE:                                                    A3 A2 A1
152757e119bSMatthias Koenig  *                                                                   --------
153757e119bSMatthias Koenig  */
154757e119bSMatthias Koenig #define	RXAVAIL		PP_STAT_SEL	/* Rx Available, channel 0.   0 0 0 */
155757e119bSMatthias Koenig //  RXAVAIL1    PP_STAT_SEL             /* Rx Available, channel 1.   0 0 1 */
156757e119bSMatthias Koenig #define	SYNC_STAT	PP_STAT_SEL	/* Reserved - Sync Status.    0 1 0 */
157757e119bSMatthias Koenig //                                      /* Reserved.                  0 1 1 */
158757e119bSMatthias Koenig #define	TXEMPTY		PP_STAT_SEL	/* Tx Empty, channel 0.       1 0 0 */
159757e119bSMatthias Koenig //      TXEMPTY1        PP_STAT_SEL     /* Tx Empty, channel 1.       1 0 1 */
160757e119bSMatthias Koenig //  TXEMPTY2    PP_STAT_SEL             /* Tx Empty, channel 2.       1 1 0 */
161757e119bSMatthias Koenig //  TXEMPTY3    PP_STAT_SEL             /* Tx Empty, channel 3.       1 1 1 */
162757e119bSMatthias Koenig 
163757e119bSMatthias Koenig /*   BUSY LINE:                                                      A3 A2 A1
164757e119bSMatthias Koenig  *                                                                   --------
165757e119bSMatthias Koenig  */
166757e119bSMatthias Koenig #define	RXDATA		PP_STAT_BSY	/* Rx Input Data, channel 0.  0 0 0 */
167757e119bSMatthias Koenig //      RXDATA1         PP_STAT_BSY     /* Rx Input Data, channel 1.  0 0 1 */
168757e119bSMatthias Koenig #define	SYNC_DATA       PP_STAT_BSY	/* Reserved - Sync Data.      0 1 0 */
169757e119bSMatthias Koenig 					/* Reserved.                  0 1 1 */
170757e119bSMatthias Koenig #define	DATA_ECHO       PP_STAT_BSY	/* Parallel Port Data Echo.   1 0 0 */
171757e119bSMatthias Koenig #define	A0_ECHO         PP_STAT_BSY	/* Address 0 Echo.            1 0 1 */
172757e119bSMatthias Koenig #define	A1_ECHO         PP_STAT_BSY	/* Address 1 Echo.            1 1 0 */
173757e119bSMatthias Koenig #define	A2_ECHO         PP_STAT_BSY	/* Address 2 Echo.            1 1 1 */
174757e119bSMatthias Koenig 
175757e119bSMatthias Koenig #define PORTMAN2X4_MODE_INPUT_TRIGGERED	 0x01
176757e119bSMatthias Koenig 
177757e119bSMatthias Koenig /*********************************************************************
178757e119bSMatthias Koenig  * Hardware specific functions
179757e119bSMatthias Koenig  *********************************************************************/
portman_write_command(struct portman * pm,u8 value)180757e119bSMatthias Koenig static inline void portman_write_command(struct portman *pm, u8 value)
181757e119bSMatthias Koenig {
182757e119bSMatthias Koenig 	parport_write_control(pm->pardev->port, value);
183757e119bSMatthias Koenig }
184757e119bSMatthias Koenig 
portman_read_status(struct portman * pm)185757e119bSMatthias Koenig static inline u8 portman_read_status(struct portman *pm)
186757e119bSMatthias Koenig {
187757e119bSMatthias Koenig 	return parport_read_status(pm->pardev->port);
188757e119bSMatthias Koenig }
189757e119bSMatthias Koenig 
portman_write_data(struct portman * pm,u8 value)190757e119bSMatthias Koenig static inline void portman_write_data(struct portman *pm, u8 value)
191757e119bSMatthias Koenig {
192757e119bSMatthias Koenig 	parport_write_data(pm->pardev->port, value);
193757e119bSMatthias Koenig }
194757e119bSMatthias Koenig 
portman_write_midi(struct portman * pm,int port,u8 mididata)195757e119bSMatthias Koenig static void portman_write_midi(struct portman *pm,
196757e119bSMatthias Koenig 			       int port, u8 mididata)
197757e119bSMatthias Koenig {
198757e119bSMatthias Koenig 	int command = ((port + 4) << 1);
199757e119bSMatthias Koenig 
200757e119bSMatthias Koenig 	/* Get entering data byte and port number in BL and BH respectively.
201757e119bSMatthias Koenig 	 * Set up Tx Channel address field for use with PP Cmd Register.
202757e119bSMatthias Koenig 	 * Store address field in BH register.
203757e119bSMatthias Koenig 	 * Inputs:      AH = Output port number (0..3).
204757e119bSMatthias Koenig 	 *              AL = Data byte.
205757e119bSMatthias Koenig 	 *    command = TXDATA0 | INT_EN;
206757e119bSMatthias Koenig 	 * Align port num with address field (b1...b3),
207757e119bSMatthias Koenig 	 * set address for TXDatax, Strobe=0
208757e119bSMatthias Koenig 	 */
209757e119bSMatthias Koenig 	command |= INT_EN;
210757e119bSMatthias Koenig 
211757e119bSMatthias Koenig 	/* Disable interrupts so that the process is not interrupted, then
212757e119bSMatthias Koenig 	 * write the address associated with the current Tx channel to the
213757e119bSMatthias Koenig 	 * PP Command Reg.  Do not set the Strobe signal yet.
214757e119bSMatthias Koenig 	 */
215757e119bSMatthias Koenig 
216757e119bSMatthias Koenig 	do {
217757e119bSMatthias Koenig 		portman_write_command(pm, command);
218757e119bSMatthias Koenig 
219757e119bSMatthias Koenig 		/* While the address lines settle, write parallel output data to
220757e119bSMatthias Koenig 		 * PP Data Reg.  This has no effect until Strobe signal is asserted.
221757e119bSMatthias Koenig 		 */
222757e119bSMatthias Koenig 
223757e119bSMatthias Koenig 		portman_write_data(pm, mididata);
224757e119bSMatthias Koenig 
225757e119bSMatthias Koenig 		/* If PCP channel's TxEmpty is set (TxEmpty is read through the PP
226757e119bSMatthias Koenig 		 * Status Register), then go write data.  Else go back and wait.
227757e119bSMatthias Koenig 		 */
228757e119bSMatthias Koenig 	} while ((portman_read_status(pm) & TXEMPTY) != TXEMPTY);
229757e119bSMatthias Koenig 
230757e119bSMatthias Koenig 	/* TxEmpty is set.  Maintain PC/P destination address and assert
231757e119bSMatthias Koenig 	 * Strobe through the PP Command Reg.  This will Strobe data into
232757e119bSMatthias Koenig 	 * the PC/P transmitter and set the PC/P BUSY signal.
233757e119bSMatthias Koenig 	 */
234757e119bSMatthias Koenig 
235757e119bSMatthias Koenig 	portman_write_command(pm, command | STROBE);
236757e119bSMatthias Koenig 
237757e119bSMatthias Koenig 	/* Wait for strobe line to settle and echo back through hardware.
238757e119bSMatthias Koenig 	 * Once it has echoed back, assume that the address and data lines
239757e119bSMatthias Koenig 	 * have settled!
240757e119bSMatthias Koenig 	 */
241757e119bSMatthias Koenig 
242757e119bSMatthias Koenig 	while ((portman_read_status(pm) & ESTB) == 0)
243757e119bSMatthias Koenig 		cpu_relax();
244757e119bSMatthias Koenig 
245757e119bSMatthias Koenig 	/* Release strobe and immediately re-allow interrupts. */
246757e119bSMatthias Koenig 	portman_write_command(pm, command);
247757e119bSMatthias Koenig 
248757e119bSMatthias Koenig 	while ((portman_read_status(pm) & ESTB) == ESTB)
249757e119bSMatthias Koenig 		cpu_relax();
250757e119bSMatthias Koenig 
251757e119bSMatthias Koenig 	/* PC/P BUSY is now set.  We must wait until BUSY resets itself.
252757e119bSMatthias Koenig 	 * We'll reenable ints while we're waiting.
253757e119bSMatthias Koenig 	 */
254757e119bSMatthias Koenig 
255757e119bSMatthias Koenig 	while ((portman_read_status(pm) & BUSY) == BUSY)
256757e119bSMatthias Koenig 		cpu_relax();
257757e119bSMatthias Koenig 
258757e119bSMatthias Koenig 	/* Data sent. */
259757e119bSMatthias Koenig }
260757e119bSMatthias Koenig 
261757e119bSMatthias Koenig 
262757e119bSMatthias Koenig /*
263757e119bSMatthias Koenig  *  Read MIDI byte from port
264757e119bSMatthias Koenig  *  Attempt to read input byte from specified hardware input port (0..).
265757e119bSMatthias Koenig  *  Return -1 if no data
266757e119bSMatthias Koenig  */
portman_read_midi(struct portman * pm,int port)267757e119bSMatthias Koenig static int portman_read_midi(struct portman *pm, int port)
268757e119bSMatthias Koenig {
269757e119bSMatthias Koenig 	unsigned char midi_data = 0;
270757e119bSMatthias Koenig 	unsigned char cmdout;	/* Saved address+IE bit. */
271757e119bSMatthias Koenig 
272757e119bSMatthias Koenig 	/* Make sure clocking edge is down before starting... */
273757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Make sure edge is down. */
274757e119bSMatthias Koenig 
275757e119bSMatthias Koenig 	/* Set destination address to PCP. */
276757e119bSMatthias Koenig 	cmdout = (port << 1) | INT_EN;	/* Address + IE + No Strobe. */
277757e119bSMatthias Koenig 	portman_write_command(pm, cmdout);
278757e119bSMatthias Koenig 
279757e119bSMatthias Koenig 	while ((portman_read_status(pm) & ESTB) == ESTB)
280757e119bSMatthias Koenig 		cpu_relax();	/* Wait for strobe echo. */
281757e119bSMatthias Koenig 
282757e119bSMatthias Koenig 	/* After the address lines settle, check multiplexed RxAvail signal.
283757e119bSMatthias Koenig 	 * If data is available, read it.
284757e119bSMatthias Koenig 	 */
285757e119bSMatthias Koenig 	if ((portman_read_status(pm) & RXAVAIL) == 0)
286757e119bSMatthias Koenig 		return -1;	/* No data. */
287757e119bSMatthias Koenig 
288757e119bSMatthias Koenig 	/* Set the Strobe signal to enable the Rx clocking circuitry. */
289757e119bSMatthias Koenig 	portman_write_command(pm, cmdout | STROBE);	/* Write address+IE+Strobe. */
290757e119bSMatthias Koenig 
291757e119bSMatthias Koenig 	while ((portman_read_status(pm) & ESTB) == 0)
292757e119bSMatthias Koenig 		cpu_relax(); /* Wait for strobe echo. */
293757e119bSMatthias Koenig 
294757e119bSMatthias Koenig 	/* The first data bit (msb) is already sitting on the input line. */
295757e119bSMatthias Koenig 	midi_data = (portman_read_status(pm) & 128);
296757e119bSMatthias Koenig 	portman_write_data(pm, 1);	/* Cause rising edge, which shifts data. */
297757e119bSMatthias Koenig 
298757e119bSMatthias Koenig 	/* Data bit 6. */
299757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Cause falling edge while data settles. */
300757e119bSMatthias Koenig 	midi_data |= (portman_read_status(pm) >> 1) & 64;
301757e119bSMatthias Koenig 	portman_write_data(pm, 1);	/* Cause rising edge, which shifts data. */
302757e119bSMatthias Koenig 
303757e119bSMatthias Koenig 	/* Data bit 5. */
304757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Cause falling edge while data settles. */
305757e119bSMatthias Koenig 	midi_data |= (portman_read_status(pm) >> 2) & 32;
306757e119bSMatthias Koenig 	portman_write_data(pm, 1);	/* Cause rising edge, which shifts data. */
307757e119bSMatthias Koenig 
308757e119bSMatthias Koenig 	/* Data bit 4. */
309757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Cause falling edge while data settles. */
310757e119bSMatthias Koenig 	midi_data |= (portman_read_status(pm) >> 3) & 16;
311757e119bSMatthias Koenig 	portman_write_data(pm, 1);	/* Cause rising edge, which shifts data. */
312757e119bSMatthias Koenig 
313757e119bSMatthias Koenig 	/* Data bit 3. */
314757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Cause falling edge while data settles. */
315757e119bSMatthias Koenig 	midi_data |= (portman_read_status(pm) >> 4) & 8;
316757e119bSMatthias Koenig 	portman_write_data(pm, 1);	/* Cause rising edge, which shifts data. */
317757e119bSMatthias Koenig 
318757e119bSMatthias Koenig 	/* Data bit 2. */
319757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Cause falling edge while data settles. */
320757e119bSMatthias Koenig 	midi_data |= (portman_read_status(pm) >> 5) & 4;
321757e119bSMatthias Koenig 	portman_write_data(pm, 1);	/* Cause rising edge, which shifts data. */
322757e119bSMatthias Koenig 
323757e119bSMatthias Koenig 	/* Data bit 1. */
324757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Cause falling edge while data settles. */
325757e119bSMatthias Koenig 	midi_data |= (portman_read_status(pm) >> 6) & 2;
326757e119bSMatthias Koenig 	portman_write_data(pm, 1);	/* Cause rising edge, which shifts data. */
327757e119bSMatthias Koenig 
328757e119bSMatthias Koenig 	/* Data bit 0. */
329757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Cause falling edge while data settles. */
330757e119bSMatthias Koenig 	midi_data |= (portman_read_status(pm) >> 7) & 1;
331757e119bSMatthias Koenig 	portman_write_data(pm, 1);	/* Cause rising edge, which shifts data. */
332757e119bSMatthias Koenig 	portman_write_data(pm, 0);	/* Return data clock low. */
333757e119bSMatthias Koenig 
334757e119bSMatthias Koenig 
335757e119bSMatthias Koenig 	/* De-assert Strobe and return data. */
336757e119bSMatthias Koenig 	portman_write_command(pm, cmdout);	/* Output saved address+IE. */
337757e119bSMatthias Koenig 
338757e119bSMatthias Koenig 	/* Wait for strobe echo. */
339757e119bSMatthias Koenig 	while ((portman_read_status(pm) & ESTB) == ESTB)
340757e119bSMatthias Koenig 		cpu_relax();
341757e119bSMatthias Koenig 
342757e119bSMatthias Koenig 	return (midi_data & 255);	/* Shift back and return value. */
343757e119bSMatthias Koenig }
344757e119bSMatthias Koenig 
345757e119bSMatthias Koenig /*
346757e119bSMatthias Koenig  *  Checks if any input data on the given channel is available
347757e119bSMatthias Koenig  *  Checks RxAvail
348757e119bSMatthias Koenig  */
portman_data_avail(struct portman * pm,int channel)349757e119bSMatthias Koenig static int portman_data_avail(struct portman *pm, int channel)
350757e119bSMatthias Koenig {
351757e119bSMatthias Koenig 	int command = INT_EN;
352757e119bSMatthias Koenig 	switch (channel) {
353757e119bSMatthias Koenig 	case 0:
354757e119bSMatthias Koenig 		command |= RXDATA0;
355757e119bSMatthias Koenig 		break;
356757e119bSMatthias Koenig 	case 1:
357757e119bSMatthias Koenig 		command |= RXDATA1;
358757e119bSMatthias Koenig 		break;
359757e119bSMatthias Koenig 	}
360757e119bSMatthias Koenig 	/* Write hardware (assumme STROBE=0) */
361757e119bSMatthias Koenig 	portman_write_command(pm, command);
362757e119bSMatthias Koenig 	/* Check multiplexed RxAvail signal */
363757e119bSMatthias Koenig 	if ((portman_read_status(pm) & RXAVAIL) == RXAVAIL)
364757e119bSMatthias Koenig 		return 1;	/* Data available */
365757e119bSMatthias Koenig 
366757e119bSMatthias Koenig 	/* No Data available */
367757e119bSMatthias Koenig 	return 0;
368757e119bSMatthias Koenig }
369757e119bSMatthias Koenig 
370757e119bSMatthias Koenig 
371757e119bSMatthias Koenig /*
372757e119bSMatthias Koenig  *  Flushes any input
373757e119bSMatthias Koenig  */
portman_flush_input(struct portman * pm,unsigned char port)374757e119bSMatthias Koenig static void portman_flush_input(struct portman *pm, unsigned char port)
375757e119bSMatthias Koenig {
376757e119bSMatthias Koenig 	/* Local variable for counting things */
377757e119bSMatthias Koenig 	unsigned int i = 0;
378757e119bSMatthias Koenig 	unsigned char command = 0;
379757e119bSMatthias Koenig 
380757e119bSMatthias Koenig 	switch (port) {
381757e119bSMatthias Koenig 	case 0:
382757e119bSMatthias Koenig 		command = RXDATA0;
383757e119bSMatthias Koenig 		break;
384757e119bSMatthias Koenig 	case 1:
385757e119bSMatthias Koenig 		command = RXDATA1;
386757e119bSMatthias Koenig 		break;
387757e119bSMatthias Koenig 	default:
388757e119bSMatthias Koenig 		snd_printk(KERN_WARNING
389757e119bSMatthias Koenig 			   "portman_flush_input() Won't flush port %i\n",
390757e119bSMatthias Koenig 			   port);
391757e119bSMatthias Koenig 		return;
392757e119bSMatthias Koenig 	}
393757e119bSMatthias Koenig 
394757e119bSMatthias Koenig 	/* Set address for specified channel in port and allow to settle. */
395757e119bSMatthias Koenig 	portman_write_command(pm, command);
396757e119bSMatthias Koenig 
397757e119bSMatthias Koenig 	/* Assert the Strobe and wait for echo back. */
398757e119bSMatthias Koenig 	portman_write_command(pm, command | STROBE);
399757e119bSMatthias Koenig 
400757e119bSMatthias Koenig 	/* Wait for ESTB */
401757e119bSMatthias Koenig 	while ((portman_read_status(pm) & ESTB) == 0)
402757e119bSMatthias Koenig 		cpu_relax();
403757e119bSMatthias Koenig 
404757e119bSMatthias Koenig 	/* Output clock cycles to the Rx circuitry. */
405757e119bSMatthias Koenig 	portman_write_data(pm, 0);
406757e119bSMatthias Koenig 
407757e119bSMatthias Koenig 	/* Flush 250 bits... */
408757e119bSMatthias Koenig 	for (i = 0; i < 250; i++) {
409757e119bSMatthias Koenig 		portman_write_data(pm, 1);
410757e119bSMatthias Koenig 		portman_write_data(pm, 0);
411757e119bSMatthias Koenig 	}
412757e119bSMatthias Koenig 
413757e119bSMatthias Koenig 	/* Deassert the Strobe signal of the port and wait for it to settle. */
414757e119bSMatthias Koenig 	portman_write_command(pm, command | INT_EN);
415757e119bSMatthias Koenig 
416757e119bSMatthias Koenig 	/* Wait for settling */
417757e119bSMatthias Koenig 	while ((portman_read_status(pm) & ESTB) == ESTB)
418757e119bSMatthias Koenig 		cpu_relax();
419757e119bSMatthias Koenig }
420757e119bSMatthias Koenig 
portman_probe(struct parport * p)421757e119bSMatthias Koenig static int portman_probe(struct parport *p)
422757e119bSMatthias Koenig {
423757e119bSMatthias Koenig 	/* Initialize the parallel port data register.  Will set Rx clocks
424757e119bSMatthias Koenig 	 * low in case we happen to be addressing the Rx ports at this time.
425757e119bSMatthias Koenig 	 */
426757e119bSMatthias Koenig 	/* 1 */
427757e119bSMatthias Koenig 	parport_write_data(p, 0);
428757e119bSMatthias Koenig 
429757e119bSMatthias Koenig 	/* Initialize the parallel port command register, thus initializing
430757e119bSMatthias Koenig 	 * hardware handshake lines to midi box:
431757e119bSMatthias Koenig 	 *
432757e119bSMatthias Koenig 	 *                                  Strobe = 0
433757e119bSMatthias Koenig 	 *                                  Interrupt Enable = 0
434757e119bSMatthias Koenig 	 */
435757e119bSMatthias Koenig 	/* 2 */
436757e119bSMatthias Koenig 	parport_write_control(p, 0);
437757e119bSMatthias Koenig 
438757e119bSMatthias Koenig 	/* Check if Portman PC/P 2x4 is out there. */
439757e119bSMatthias Koenig 	/* 3 */
440757e119bSMatthias Koenig 	parport_write_control(p, RXDATA0);	/* Write Strobe=0 to command reg. */
441757e119bSMatthias Koenig 
442757e119bSMatthias Koenig 	/* Check for ESTB to be clear */
443757e119bSMatthias Koenig 	/* 4 */
444757e119bSMatthias Koenig 	if ((parport_read_status(p) & ESTB) == ESTB)
445757e119bSMatthias Koenig 		return 1;	/* CODE 1 - Strobe Failure. */
446757e119bSMatthias Koenig 
447757e119bSMatthias Koenig 	/* Set for RXDATA0 where no damage will be done. */
448757e119bSMatthias Koenig 	/* 5 */
44928a04aa3SSamuel Zou 	parport_write_control(p, RXDATA0 | STROBE);	/* Write Strobe=1 to command reg. */
450757e119bSMatthias Koenig 
451757e119bSMatthias Koenig 	/* 6 */
452757e119bSMatthias Koenig 	if ((parport_read_status(p) & ESTB) != ESTB)
453757e119bSMatthias Koenig 		return 1;	/* CODE 1 - Strobe Failure. */
454757e119bSMatthias Koenig 
455757e119bSMatthias Koenig 	/* 7 */
456757e119bSMatthias Koenig 	parport_write_control(p, 0);	/* Reset Strobe=0. */
457757e119bSMatthias Koenig 
458757e119bSMatthias Koenig 	/* Check if Tx circuitry is functioning properly.  If initialized
45986b9c4cdSRandy Dunlap 	 * unit TxEmpty is false, send out char and see if it goes true.
460757e119bSMatthias Koenig 	 */
461757e119bSMatthias Koenig 	/* 8 */
462757e119bSMatthias Koenig 	parport_write_control(p, TXDATA0);	/* Tx channel 0, strobe off. */
463757e119bSMatthias Koenig 
464757e119bSMatthias Koenig 	/* If PCP channel's TxEmpty is set (TxEmpty is read through the PP
465757e119bSMatthias Koenig 	 * Status Register), then go write data.  Else go back and wait.
466757e119bSMatthias Koenig 	 */
467757e119bSMatthias Koenig 	/* 9 */
468757e119bSMatthias Koenig 	if ((parport_read_status(p) & TXEMPTY) == 0)
469757e119bSMatthias Koenig 		return 2;
470757e119bSMatthias Koenig 
471757e119bSMatthias Koenig 	/* Return OK status. */
472757e119bSMatthias Koenig 	return 0;
473757e119bSMatthias Koenig }
474757e119bSMatthias Koenig 
portman_device_init(struct portman * pm)475757e119bSMatthias Koenig static int portman_device_init(struct portman *pm)
476757e119bSMatthias Koenig {
477757e119bSMatthias Koenig 	portman_flush_input(pm, 0);
478757e119bSMatthias Koenig 	portman_flush_input(pm, 1);
479757e119bSMatthias Koenig 
480757e119bSMatthias Koenig 	return 0;
481757e119bSMatthias Koenig }
482757e119bSMatthias Koenig 
483757e119bSMatthias Koenig /*********************************************************************
484757e119bSMatthias Koenig  * Rawmidi
485757e119bSMatthias Koenig  *********************************************************************/
snd_portman_midi_open(struct snd_rawmidi_substream * substream)486757e119bSMatthias Koenig static int snd_portman_midi_open(struct snd_rawmidi_substream *substream)
487757e119bSMatthias Koenig {
488757e119bSMatthias Koenig 	return 0;
489757e119bSMatthias Koenig }
490757e119bSMatthias Koenig 
snd_portman_midi_close(struct snd_rawmidi_substream * substream)491757e119bSMatthias Koenig static int snd_portman_midi_close(struct snd_rawmidi_substream *substream)
492757e119bSMatthias Koenig {
493757e119bSMatthias Koenig 	return 0;
494757e119bSMatthias Koenig }
495757e119bSMatthias Koenig 
snd_portman_midi_input_trigger(struct snd_rawmidi_substream * substream,int up)496757e119bSMatthias Koenig static void snd_portman_midi_input_trigger(struct snd_rawmidi_substream *substream,
497757e119bSMatthias Koenig 					   int up)
498757e119bSMatthias Koenig {
499757e119bSMatthias Koenig 	struct portman *pm = substream->rmidi->private_data;
500757e119bSMatthias Koenig 	unsigned long flags;
501757e119bSMatthias Koenig 
502757e119bSMatthias Koenig 	spin_lock_irqsave(&pm->reg_lock, flags);
503757e119bSMatthias Koenig 	if (up)
504757e119bSMatthias Koenig 		pm->mode[substream->number] |= PORTMAN2X4_MODE_INPUT_TRIGGERED;
505757e119bSMatthias Koenig 	else
506757e119bSMatthias Koenig 		pm->mode[substream->number] &= ~PORTMAN2X4_MODE_INPUT_TRIGGERED;
507757e119bSMatthias Koenig 	spin_unlock_irqrestore(&pm->reg_lock, flags);
508757e119bSMatthias Koenig }
509757e119bSMatthias Koenig 
snd_portman_midi_output_trigger(struct snd_rawmidi_substream * substream,int up)510757e119bSMatthias Koenig static void snd_portman_midi_output_trigger(struct snd_rawmidi_substream *substream,
511757e119bSMatthias Koenig 					    int up)
512757e119bSMatthias Koenig {
513757e119bSMatthias Koenig 	struct portman *pm = substream->rmidi->private_data;
514757e119bSMatthias Koenig 	unsigned long flags;
515757e119bSMatthias Koenig 	unsigned char byte;
516757e119bSMatthias Koenig 
517757e119bSMatthias Koenig 	spin_lock_irqsave(&pm->reg_lock, flags);
518757e119bSMatthias Koenig 	if (up) {
519757e119bSMatthias Koenig 		while ((snd_rawmidi_transmit(substream, &byte, 1) == 1))
520757e119bSMatthias Koenig 			portman_write_midi(pm, substream->number, byte);
521757e119bSMatthias Koenig 	}
522757e119bSMatthias Koenig 	spin_unlock_irqrestore(&pm->reg_lock, flags);
523757e119bSMatthias Koenig }
524757e119bSMatthias Koenig 
525c36f486dSTakashi Iwai static const struct snd_rawmidi_ops snd_portman_midi_output = {
526757e119bSMatthias Koenig 	.open =		snd_portman_midi_open,
527757e119bSMatthias Koenig 	.close =	snd_portman_midi_close,
528757e119bSMatthias Koenig 	.trigger =	snd_portman_midi_output_trigger,
529757e119bSMatthias Koenig };
530757e119bSMatthias Koenig 
531c36f486dSTakashi Iwai static const struct snd_rawmidi_ops snd_portman_midi_input = {
532757e119bSMatthias Koenig 	.open =		snd_portman_midi_open,
533757e119bSMatthias Koenig 	.close =	snd_portman_midi_close,
534757e119bSMatthias Koenig 	.trigger =	snd_portman_midi_input_trigger,
535757e119bSMatthias Koenig };
536757e119bSMatthias Koenig 
537757e119bSMatthias Koenig /* Create and initialize the rawmidi component */
snd_portman_rawmidi_create(struct snd_card * card)538fbbb01a1SBill Pemberton static int snd_portman_rawmidi_create(struct snd_card *card)
539757e119bSMatthias Koenig {
540757e119bSMatthias Koenig 	struct portman *pm = card->private_data;
541757e119bSMatthias Koenig 	struct snd_rawmidi *rmidi;
542757e119bSMatthias Koenig 	struct snd_rawmidi_substream *substream;
543757e119bSMatthias Koenig 	int err;
544757e119bSMatthias Koenig 
545757e119bSMatthias Koenig 	err = snd_rawmidi_new(card, CARD_NAME, 0,
546757e119bSMatthias Koenig 			      PORTMAN_NUM_OUTPUT_PORTS,
547757e119bSMatthias Koenig 			      PORTMAN_NUM_INPUT_PORTS,
548757e119bSMatthias Koenig 			      &rmidi);
549757e119bSMatthias Koenig 	if (err < 0)
550757e119bSMatthias Koenig 		return err;
551757e119bSMatthias Koenig 
552757e119bSMatthias Koenig 	rmidi->private_data = pm;
553757e119bSMatthias Koenig 	strcpy(rmidi->name, CARD_NAME);
554757e119bSMatthias Koenig 	rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
555757e119bSMatthias Koenig 		            SNDRV_RAWMIDI_INFO_INPUT |
556757e119bSMatthias Koenig                             SNDRV_RAWMIDI_INFO_DUPLEX;
557757e119bSMatthias Koenig 
558757e119bSMatthias Koenig 	pm->rmidi = rmidi;
559757e119bSMatthias Koenig 
560757e119bSMatthias Koenig 	/* register rawmidi ops */
561757e119bSMatthias Koenig 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
562757e119bSMatthias Koenig 			    &snd_portman_midi_output);
563757e119bSMatthias Koenig 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
564757e119bSMatthias Koenig 			    &snd_portman_midi_input);
565757e119bSMatthias Koenig 
566757e119bSMatthias Koenig 	/* name substreams */
567757e119bSMatthias Koenig 	/* output */
568757e119bSMatthias Koenig 	list_for_each_entry(substream,
569757e119bSMatthias Koenig 			    &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
570757e119bSMatthias Koenig 			    list) {
571757e119bSMatthias Koenig 		sprintf(substream->name,
572757e119bSMatthias Koenig 			"Portman2x4 %d", substream->number+1);
573757e119bSMatthias Koenig 	}
574757e119bSMatthias Koenig 	/* input */
575757e119bSMatthias Koenig 	list_for_each_entry(substream,
576757e119bSMatthias Koenig 			    &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
577757e119bSMatthias Koenig 			    list) {
578757e119bSMatthias Koenig 		pm->midi_input[substream->number] = substream;
579757e119bSMatthias Koenig 		sprintf(substream->name,
580757e119bSMatthias Koenig 			"Portman2x4 %d", substream->number+1);
581757e119bSMatthias Koenig 	}
582757e119bSMatthias Koenig 
583757e119bSMatthias Koenig 	return err;
584757e119bSMatthias Koenig }
585757e119bSMatthias Koenig 
586757e119bSMatthias Koenig /*********************************************************************
587757e119bSMatthias Koenig  * parport stuff
588757e119bSMatthias Koenig  *********************************************************************/
snd_portman_interrupt(void * userdata)5895712cb3dSJeff Garzik static void snd_portman_interrupt(void *userdata)
590757e119bSMatthias Koenig {
591757e119bSMatthias Koenig 	unsigned char midivalue = 0;
592757e119bSMatthias Koenig 	struct portman *pm = ((struct snd_card*)userdata)->private_data;
593757e119bSMatthias Koenig 
594757e119bSMatthias Koenig 	spin_lock(&pm->reg_lock);
595757e119bSMatthias Koenig 
596757e119bSMatthias Koenig 	/* While any input data is waiting */
597757e119bSMatthias Koenig 	while ((portman_read_status(pm) & INT_REQ) == INT_REQ) {
598757e119bSMatthias Koenig 		/* If data available on channel 0,
599757e119bSMatthias Koenig 		   read it and stuff it into the queue. */
600757e119bSMatthias Koenig 		if (portman_data_avail(pm, 0)) {
601757e119bSMatthias Koenig 			/* Read Midi */
602757e119bSMatthias Koenig 			midivalue = portman_read_midi(pm, 0);
603757e119bSMatthias Koenig 			/* put midi into queue... */
604757e119bSMatthias Koenig 			if (pm->mode[0] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
605757e119bSMatthias Koenig 				snd_rawmidi_receive(pm->midi_input[0],
606757e119bSMatthias Koenig 						    &midivalue, 1);
607757e119bSMatthias Koenig 
608757e119bSMatthias Koenig 		}
609757e119bSMatthias Koenig 		/* If data available on channel 1,
610757e119bSMatthias Koenig 		   read it and stuff it into the queue. */
611757e119bSMatthias Koenig 		if (portman_data_avail(pm, 1)) {
612757e119bSMatthias Koenig 			/* Read Midi */
613757e119bSMatthias Koenig 			midivalue = portman_read_midi(pm, 1);
614757e119bSMatthias Koenig 			/* put midi into queue... */
615757e119bSMatthias Koenig 			if (pm->mode[1] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
616757e119bSMatthias Koenig 				snd_rawmidi_receive(pm->midi_input[1],
617757e119bSMatthias Koenig 						    &midivalue, 1);
618757e119bSMatthias Koenig 		}
619757e119bSMatthias Koenig 
620757e119bSMatthias Koenig 	}
621757e119bSMatthias Koenig 
622757e119bSMatthias Koenig 	spin_unlock(&pm->reg_lock);
623757e119bSMatthias Koenig }
624757e119bSMatthias Koenig 
snd_portman_attach(struct parport * p)625fbbb01a1SBill Pemberton static void snd_portman_attach(struct parport *p)
626757e119bSMatthias Koenig {
627757e119bSMatthias Koenig 	struct platform_device *device;
628757e119bSMatthias Koenig 
629757e119bSMatthias Koenig 	device = platform_device_alloc(PLATFORM_DRIVER, device_count);
630757e119bSMatthias Koenig 	if (!device)
631757e119bSMatthias Koenig 		return;
632757e119bSMatthias Koenig 
633757e119bSMatthias Koenig 	/* Temporary assignment to forward the parport */
634757e119bSMatthias Koenig 	platform_set_drvdata(device, p);
635757e119bSMatthias Koenig 
636479ef436SAkinobu Mita 	if (platform_device_add(device) < 0) {
637757e119bSMatthias Koenig 		platform_device_put(device);
638757e119bSMatthias Koenig 		return;
639757e119bSMatthias Koenig 	}
640757e119bSMatthias Koenig 
641757e119bSMatthias Koenig 	/* Since we dont get the return value of probe
642757e119bSMatthias Koenig 	 * We need to check if device probing succeeded or not */
643757e119bSMatthias Koenig 	if (!platform_get_drvdata(device)) {
644757e119bSMatthias Koenig 		platform_device_unregister(device);
645757e119bSMatthias Koenig 		return;
646757e119bSMatthias Koenig 	}
647757e119bSMatthias Koenig 
648757e119bSMatthias Koenig 	/* register device in global table */
649757e119bSMatthias Koenig 	platform_devices[device_count] = device;
650757e119bSMatthias Koenig 	device_count++;
651757e119bSMatthias Koenig }
652757e119bSMatthias Koenig 
snd_portman_detach(struct parport * p)653757e119bSMatthias Koenig static void snd_portman_detach(struct parport *p)
654757e119bSMatthias Koenig {
655757e119bSMatthias Koenig 	/* nothing to do here */
656757e119bSMatthias Koenig }
657757e119bSMatthias Koenig 
snd_portman_dev_probe(struct pardevice * pardev)658e6a1b7e8SSudip Mukherjee static int snd_portman_dev_probe(struct pardevice *pardev)
659e6a1b7e8SSudip Mukherjee {
660e6a1b7e8SSudip Mukherjee 	if (strcmp(pardev->name, DRIVER_NAME))
661e6a1b7e8SSudip Mukherjee 		return -ENODEV;
662e6a1b7e8SSudip Mukherjee 
663e6a1b7e8SSudip Mukherjee 	return 0;
664e6a1b7e8SSudip Mukherjee }
665e6a1b7e8SSudip Mukherjee 
666757e119bSMatthias Koenig static struct parport_driver portman_parport_driver = {
667757e119bSMatthias Koenig 	.name		= "portman2x4",
668e6a1b7e8SSudip Mukherjee 	.probe		= snd_portman_dev_probe,
669e6a1b7e8SSudip Mukherjee 	.match_port	= snd_portman_attach,
670e6a1b7e8SSudip Mukherjee 	.detach		= snd_portman_detach,
671e6a1b7e8SSudip Mukherjee 	.devmodel	= true,
672757e119bSMatthias Koenig };
673757e119bSMatthias Koenig 
674757e119bSMatthias Koenig /*********************************************************************
675757e119bSMatthias Koenig  * platform stuff
676757e119bSMatthias Koenig  *********************************************************************/
snd_portman_card_private_free(struct snd_card * card)677757e119bSMatthias Koenig static void snd_portman_card_private_free(struct snd_card *card)
678757e119bSMatthias Koenig {
679757e119bSMatthias Koenig 	struct portman *pm = card->private_data;
680757e119bSMatthias Koenig 	struct pardevice *pardev = pm->pardev;
681757e119bSMatthias Koenig 
682757e119bSMatthias Koenig 	if (pardev) {
683757e119bSMatthias Koenig 		parport_release(pardev);
684757e119bSMatthias Koenig 		parport_unregister_device(pardev);
685757e119bSMatthias Koenig 	}
686757e119bSMatthias Koenig 
687757e119bSMatthias Koenig 	portman_free(pm);
688757e119bSMatthias Koenig }
689757e119bSMatthias Koenig 
snd_portman_probe(struct platform_device * pdev)690fbbb01a1SBill Pemberton static int snd_portman_probe(struct platform_device *pdev)
691757e119bSMatthias Koenig {
692757e119bSMatthias Koenig 	struct pardevice *pardev;
693757e119bSMatthias Koenig 	struct parport *p;
694757e119bSMatthias Koenig 	int dev = pdev->id;
695757e119bSMatthias Koenig 	struct snd_card *card = NULL;
696757e119bSMatthias Koenig 	struct portman *pm = NULL;
697757e119bSMatthias Koenig 	int err;
698e6a1b7e8SSudip Mukherjee 	struct pardev_cb portman_cb = {
699e6a1b7e8SSudip Mukherjee 		.preempt = NULL,
700e6a1b7e8SSudip Mukherjee 		.wakeup = NULL,
701e6a1b7e8SSudip Mukherjee 		.irq_func = snd_portman_interrupt,	/* ISR */
702e6a1b7e8SSudip Mukherjee 		.flags = PARPORT_DEV_EXCL,		/* flags */
703e6a1b7e8SSudip Mukherjee 	};
704757e119bSMatthias Koenig 
705757e119bSMatthias Koenig 	p = platform_get_drvdata(pdev);
706757e119bSMatthias Koenig 	platform_set_drvdata(pdev, NULL);
707757e119bSMatthias Koenig 
708757e119bSMatthias Koenig 	if (dev >= SNDRV_CARDS)
709757e119bSMatthias Koenig 		return -ENODEV;
710757e119bSMatthias Koenig 	if (!enable[dev])
711757e119bSMatthias Koenig 		return -ENOENT;
712757e119bSMatthias Koenig 
7135872f3f6STakashi Iwai 	err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
7145872f3f6STakashi Iwai 			   0, &card);
715bd7dd77cSTakashi Iwai 	if (err < 0) {
716757e119bSMatthias Koenig 		snd_printd("Cannot create card\n");
717bd7dd77cSTakashi Iwai 		return err;
718757e119bSMatthias Koenig 	}
719757e119bSMatthias Koenig 	strcpy(card->driver, DRIVER_NAME);
720757e119bSMatthias Koenig 	strcpy(card->shortname, CARD_NAME);
721757e119bSMatthias Koenig 	sprintf(card->longname,  "%s at 0x%lx, irq %i",
722757e119bSMatthias Koenig 		card->shortname, p->base, p->irq);
723757e119bSMatthias Koenig 
724e6a1b7e8SSudip Mukherjee 	portman_cb.private = card;			   /* private */
725e6a1b7e8SSudip Mukherjee 	pardev = parport_register_dev_model(p,		   /* port */
726757e119bSMatthias Koenig 					    DRIVER_NAME,   /* name */
727e6a1b7e8SSudip Mukherjee 					    &portman_cb,   /* callbacks */
728e6a1b7e8SSudip Mukherjee 					    pdev->id);	   /* device number */
729757e119bSMatthias Koenig 	if (pardev == NULL) {
730757e119bSMatthias Koenig 		snd_printd("Cannot register pardevice\n");
731757e119bSMatthias Koenig 		err = -EIO;
732757e119bSMatthias Koenig 		goto __err;
733757e119bSMatthias Koenig 	}
734757e119bSMatthias Koenig 
735e6a1b7e8SSudip Mukherjee 	/* claim parport */
736e6a1b7e8SSudip Mukherjee 	if (parport_claim(pardev)) {
737e6a1b7e8SSudip Mukherjee 		snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
738e6a1b7e8SSudip Mukherjee 		err = -EIO;
739e6a1b7e8SSudip Mukherjee 		goto free_pardev;
740e6a1b7e8SSudip Mukherjee 	}
741e6a1b7e8SSudip Mukherjee 
742d0ad13efSTakashi Iwai 	err = portman_create(card, pardev, &pm);
743d0ad13efSTakashi Iwai 	if (err < 0) {
744757e119bSMatthias Koenig 		snd_printd("Cannot create main component\n");
745e6a1b7e8SSudip Mukherjee 		goto release_pardev;
746757e119bSMatthias Koenig 	}
747757e119bSMatthias Koenig 	card->private_data = pm;
748757e119bSMatthias Koenig 	card->private_free = snd_portman_card_private_free;
749757e119bSMatthias Koenig 
75003367bf7SSudip Mukherjee 	err = portman_probe(p);
75103367bf7SSudip Mukherjee 	if (err) {
75203367bf7SSudip Mukherjee 		err = -EIO;
75303367bf7SSudip Mukherjee 		goto __err;
75403367bf7SSudip Mukherjee 	}
75503367bf7SSudip Mukherjee 
756d0ad13efSTakashi Iwai 	err = snd_portman_rawmidi_create(card);
757d0ad13efSTakashi Iwai 	if (err < 0) {
758757e119bSMatthias Koenig 		snd_printd("Creating Rawmidi component failed\n");
759757e119bSMatthias Koenig 		goto __err;
760757e119bSMatthias Koenig 	}
761757e119bSMatthias Koenig 
762757e119bSMatthias Koenig 	/* init device */
763d0ad13efSTakashi Iwai 	err = portman_device_init(pm);
764d0ad13efSTakashi Iwai 	if (err < 0)
765757e119bSMatthias Koenig 		goto __err;
766757e119bSMatthias Koenig 
767757e119bSMatthias Koenig 	platform_set_drvdata(pdev, card);
768757e119bSMatthias Koenig 
769757e119bSMatthias Koenig 	/* At this point card will be usable */
770d0ad13efSTakashi Iwai 	err = snd_card_register(card);
771d0ad13efSTakashi Iwai 	if (err < 0) {
772757e119bSMatthias Koenig 		snd_printd("Cannot register card\n");
773757e119bSMatthias Koenig 		goto __err;
774757e119bSMatthias Koenig 	}
775757e119bSMatthias Koenig 
776757e119bSMatthias Koenig 	snd_printk(KERN_INFO "Portman 2x4 on 0x%lx\n", p->base);
777757e119bSMatthias Koenig 	return 0;
778757e119bSMatthias Koenig 
779e6a1b7e8SSudip Mukherjee release_pardev:
780e6a1b7e8SSudip Mukherjee 	parport_release(pardev);
781e6a1b7e8SSudip Mukherjee free_pardev:
782e6a1b7e8SSudip Mukherjee 	parport_unregister_device(pardev);
783757e119bSMatthias Koenig __err:
784757e119bSMatthias Koenig 	snd_card_free(card);
785757e119bSMatthias Koenig 	return err;
786757e119bSMatthias Koenig }
787757e119bSMatthias Koenig 
snd_portman_remove(struct platform_device * pdev)788*962bdc96SUwe Kleine-König static void snd_portman_remove(struct platform_device *pdev)
789757e119bSMatthias Koenig {
790757e119bSMatthias Koenig 	struct snd_card *card = platform_get_drvdata(pdev);
791757e119bSMatthias Koenig 
792757e119bSMatthias Koenig 	if (card)
793757e119bSMatthias Koenig 		snd_card_free(card);
794757e119bSMatthias Koenig }
795757e119bSMatthias Koenig 
796757e119bSMatthias Koenig 
797757e119bSMatthias Koenig static struct platform_driver snd_portman_driver = {
798757e119bSMatthias Koenig 	.probe  = snd_portman_probe,
799*962bdc96SUwe Kleine-König 	.remove_new = snd_portman_remove,
800757e119bSMatthias Koenig 	.driver = {
8018bf01d8aSTakashi Iwai 		.name = PLATFORM_DRIVER,
802757e119bSMatthias Koenig 	}
803757e119bSMatthias Koenig };
804757e119bSMatthias Koenig 
805757e119bSMatthias Koenig /*********************************************************************
806757e119bSMatthias Koenig  * module init stuff
807757e119bSMatthias Koenig  *********************************************************************/
snd_portman_unregister_all(void)8083c2b576dSRandy Dunlap static void snd_portman_unregister_all(void)
809757e119bSMatthias Koenig {
810757e119bSMatthias Koenig 	int i;
811757e119bSMatthias Koenig 
812757e119bSMatthias Koenig 	for (i = 0; i < SNDRV_CARDS; ++i) {
813757e119bSMatthias Koenig 		if (platform_devices[i]) {
814757e119bSMatthias Koenig 			platform_device_unregister(platform_devices[i]);
815757e119bSMatthias Koenig 			platform_devices[i] = NULL;
816757e119bSMatthias Koenig 		}
817757e119bSMatthias Koenig 	}
818757e119bSMatthias Koenig 	platform_driver_unregister(&snd_portman_driver);
819757e119bSMatthias Koenig 	parport_unregister_driver(&portman_parport_driver);
820757e119bSMatthias Koenig }
821757e119bSMatthias Koenig 
snd_portman_module_init(void)822757e119bSMatthias Koenig static int __init snd_portman_module_init(void)
823757e119bSMatthias Koenig {
824757e119bSMatthias Koenig 	int err;
825757e119bSMatthias Koenig 
826d0ad13efSTakashi Iwai 	err = platform_driver_register(&snd_portman_driver);
827d0ad13efSTakashi Iwai 	if (err < 0)
828757e119bSMatthias Koenig 		return err;
829757e119bSMatthias Koenig 
830757e119bSMatthias Koenig 	if (parport_register_driver(&portman_parport_driver) != 0) {
831757e119bSMatthias Koenig 		platform_driver_unregister(&snd_portman_driver);
832757e119bSMatthias Koenig 		return -EIO;
833757e119bSMatthias Koenig 	}
834757e119bSMatthias Koenig 
835757e119bSMatthias Koenig 	if (device_count == 0) {
836757e119bSMatthias Koenig 		snd_portman_unregister_all();
837757e119bSMatthias Koenig 		return -ENODEV;
838757e119bSMatthias Koenig 	}
839757e119bSMatthias Koenig 
840757e119bSMatthias Koenig 	return 0;
841757e119bSMatthias Koenig }
842757e119bSMatthias Koenig 
snd_portman_module_exit(void)843757e119bSMatthias Koenig static void __exit snd_portman_module_exit(void)
844757e119bSMatthias Koenig {
845757e119bSMatthias Koenig 	snd_portman_unregister_all();
846757e119bSMatthias Koenig }
847757e119bSMatthias Koenig 
848757e119bSMatthias Koenig module_init(snd_portman_module_init);
849757e119bSMatthias Koenig module_exit(snd_portman_module_exit);
850