Linux Audio

Check our new training course

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