File Index
Files and directories
Sign in
FILE

portman2x4.c

File preview with syntax highlighting.

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