Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   Driver for Midiman Portman2x4 parallel port midi interface
0004  *
0005  *   Copyright (c) by Levent Guendogdu <levon@feature-it.com>
0006  *
0007  * ChangeLog
0008  * Jan 24 2007 Matthias Koenig <mkoenig@suse.de>
0009  *      - cleanup and rewrite
0010  * Sep 30 2004 Tobias Gehrig <tobias@gehrig.tk>
0011  *      - source code cleanup
0012  * Sep 03 2004 Tobias Gehrig <tobias@gehrig.tk>
0013  *      - fixed compilation problem with alsa 1.0.6a (removed MODULE_CLASSES,
0014  *        MODULE_PARM_SYNTAX and changed MODULE_DEVICES to
0015  *        MODULE_SUPPORTED_DEVICE)
0016  * Mar 24 2004 Tobias Gehrig <tobias@gehrig.tk>
0017  *      - added 2.6 kernel support
0018  * Mar 18 2004 Tobias Gehrig <tobias@gehrig.tk>
0019  *      - added parport_unregister_driver to the startup routine if the driver fails to detect a portman
0020  *      - added support for all 4 output ports in portman_putmidi
0021  * Mar 17 2004 Tobias Gehrig <tobias@gehrig.tk>
0022  *      - added checks for opened input device in interrupt handler
0023  * Feb 20 2004 Tobias Gehrig <tobias@gehrig.tk>
0024  *      - ported from alsa 0.5 to 1.0
0025  */
0026 
0027 #include <linux/init.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/parport.h>
0030 #include <linux/spinlock.h>
0031 #include <linux/delay.h>
0032 #include <linux/slab.h>
0033 #include <linux/module.h>
0034 #include <sound/core.h>
0035 #include <sound/initval.h>
0036 #include <sound/rawmidi.h>
0037 #include <sound/control.h>
0038 
0039 #define CARD_NAME "Portman 2x4"
0040 #define DRIVER_NAME "portman"
0041 #define PLATFORM_DRIVER "snd_portman2x4"
0042 
0043 static int index[SNDRV_CARDS]  = SNDRV_DEFAULT_IDX;
0044 static char *id[SNDRV_CARDS]   = SNDRV_DEFAULT_STR;
0045 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0046 
0047 static struct platform_device *platform_devices[SNDRV_CARDS]; 
0048 static int device_count;
0049 
0050 module_param_array(index, int, NULL, 0444);
0051 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
0052 module_param_array(id, charp, NULL, 0444);
0053 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
0054 module_param_array(enable, bool, NULL, 0444);
0055 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
0056 
0057 MODULE_AUTHOR("Levent Guendogdu, Tobias Gehrig, Matthias Koenig");
0058 MODULE_DESCRIPTION("Midiman Portman2x4");
0059 MODULE_LICENSE("GPL");
0060 
0061 /*********************************************************************
0062  * Chip specific
0063  *********************************************************************/
0064 #define PORTMAN_NUM_INPUT_PORTS 2
0065 #define PORTMAN_NUM_OUTPUT_PORTS 4
0066 
0067 struct portman {
0068     spinlock_t reg_lock;
0069     struct snd_card *card;
0070     struct snd_rawmidi *rmidi;
0071     struct pardevice *pardev;
0072     int open_count;
0073     int mode[PORTMAN_NUM_INPUT_PORTS];
0074     struct snd_rawmidi_substream *midi_input[PORTMAN_NUM_INPUT_PORTS];
0075 };
0076 
0077 static int portman_free(struct portman *pm)
0078 {
0079     kfree(pm);
0080     return 0;
0081 }
0082 
0083 static int portman_create(struct snd_card *card,
0084               struct pardevice *pardev,
0085               struct portman **rchip)
0086 {
0087     struct portman *pm;
0088 
0089     *rchip = NULL;
0090 
0091     pm = kzalloc(sizeof(struct portman), GFP_KERNEL);
0092     if (pm == NULL) 
0093         return -ENOMEM;
0094 
0095     /* Init chip specific data */
0096     spin_lock_init(&pm->reg_lock);
0097     pm->card = card;
0098     pm->pardev = pardev;
0099 
0100     *rchip = pm;
0101 
0102     return 0;
0103 }
0104 
0105 /*********************************************************************
0106  * HW related constants
0107  *********************************************************************/
0108 
0109 /* Standard PC parallel port status register equates. */
0110 #define PP_STAT_BSY     0x80    /* Busy status.  Inverted. */
0111 #define PP_STAT_ACK     0x40    /* Acknowledge.  Non-Inverted. */
0112 #define PP_STAT_POUT    0x20    /* Paper Out.    Non-Inverted. */
0113 #define PP_STAT_SEL     0x10    /* Select.       Non-Inverted. */
0114 #define PP_STAT_ERR     0x08    /* Error.        Non-Inverted. */
0115 
0116 /* Standard PC parallel port command register equates. */
0117 #define PP_CMD_IEN      0x10    /* IRQ Enable.   Non-Inverted. */
0118 #define PP_CMD_SELI     0x08    /* Select Input. Inverted. */
0119 #define PP_CMD_INIT     0x04    /* Init Printer. Non-Inverted. */
0120 #define PP_CMD_FEED     0x02    /* Auto Feed.    Inverted. */
0121 #define PP_CMD_STB      0x01    /* Strobe.       Inverted. */
0122 
0123 /* Parallel Port Command Register as implemented by PCP2x4. */
0124 #define INT_EN      PP_CMD_IEN  /* Interrupt enable. */
0125 #define STROBE          PP_CMD_STB  /* Command strobe. */
0126 
0127 /* The parallel port command register field (b1..b3) selects the 
0128  * various "registers" within the PC/P 2x4.  These are the internal
0129  * address of these "registers" that must be written to the parallel
0130  * port command register.
0131  */
0132 #define RXDATA0     (0 << 1)    /* PCP RxData channel 0. */
0133 #define RXDATA1     (1 << 1)    /* PCP RxData channel 1. */
0134 #define GEN_CTL     (2 << 1)    /* PCP General Control Register. */
0135 #define SYNC_CTL    (3 << 1)    /* PCP Sync Control Register. */
0136 #define TXDATA0     (4 << 1)    /* PCP TxData channel 0. */
0137 #define TXDATA1     (5 << 1)    /* PCP TxData channel 1. */
0138 #define TXDATA2     (6 << 1)    /* PCP TxData channel 2. */
0139 #define TXDATA3     (7 << 1)    /* PCP TxData channel 3. */
0140 
0141 /* Parallel Port Status Register as implemented by PCP2x4. */
0142 #define ESTB        PP_STAT_POUT    /* Echoed strobe. */
0143 #define INT_REQ         PP_STAT_ACK /* Input data int request. */
0144 #define BUSY            PP_STAT_ERR /* Interface Busy. */
0145 
0146 /* Parallel Port Status Register BUSY and SELECT lines are multiplexed
0147  * between several functions.  Depending on which 2x4 "register" is
0148  * currently selected (b1..b3), the BUSY and SELECT lines are
0149  * assigned as follows:
0150  *
0151  *   SELECT LINE:                                                    A3 A2 A1
0152  *                                                                   --------
0153  */
0154 #define RXAVAIL     PP_STAT_SEL /* Rx Available, channel 0.   0 0 0 */
0155 //  RXAVAIL1    PP_STAT_SEL             /* Rx Available, channel 1.   0 0 1 */
0156 #define SYNC_STAT   PP_STAT_SEL /* Reserved - Sync Status.    0 1 0 */
0157 //                                      /* Reserved.                  0 1 1 */
0158 #define TXEMPTY     PP_STAT_SEL /* Tx Empty, channel 0.       1 0 0 */
0159 //      TXEMPTY1        PP_STAT_SEL     /* Tx Empty, channel 1.       1 0 1 */
0160 //  TXEMPTY2    PP_STAT_SEL             /* Tx Empty, channel 2.       1 1 0 */
0161 //  TXEMPTY3    PP_STAT_SEL             /* Tx Empty, channel 3.       1 1 1 */
0162 
0163 /*   BUSY LINE:                                                      A3 A2 A1
0164  *                                                                   --------
0165  */
0166 #define RXDATA      PP_STAT_BSY /* Rx Input Data, channel 0.  0 0 0 */
0167 //      RXDATA1         PP_STAT_BSY     /* Rx Input Data, channel 1.  0 0 1 */
0168 #define SYNC_DATA       PP_STAT_BSY /* Reserved - Sync Data.      0 1 0 */
0169                     /* Reserved.                  0 1 1 */
0170 #define DATA_ECHO       PP_STAT_BSY /* Parallel Port Data Echo.   1 0 0 */
0171 #define A0_ECHO         PP_STAT_BSY /* Address 0 Echo.            1 0 1 */
0172 #define A1_ECHO         PP_STAT_BSY /* Address 1 Echo.            1 1 0 */
0173 #define A2_ECHO         PP_STAT_BSY /* Address 2 Echo.            1 1 1 */
0174 
0175 #define PORTMAN2X4_MODE_INPUT_TRIGGERED  0x01
0176 
0177 /*********************************************************************
0178  * Hardware specific functions
0179  *********************************************************************/
0180 static inline void portman_write_command(struct portman *pm, u8 value)
0181 {
0182     parport_write_control(pm->pardev->port, value);
0183 }
0184 
0185 static inline u8 portman_read_command(struct portman *pm)
0186 {
0187     return parport_read_control(pm->pardev->port);
0188 }
0189 
0190 static inline u8 portman_read_status(struct portman *pm)
0191 {
0192     return parport_read_status(pm->pardev->port);
0193 }
0194 
0195 static inline u8 portman_read_data(struct portman *pm)
0196 {
0197     return parport_read_data(pm->pardev->port);
0198 }
0199 
0200 static inline void portman_write_data(struct portman *pm, u8 value)
0201 {
0202     parport_write_data(pm->pardev->port, value);
0203 }
0204 
0205 static void portman_write_midi(struct portman *pm, 
0206                    int port, u8 mididata)
0207 {
0208     int command = ((port + 4) << 1);
0209 
0210     /* Get entering data byte and port number in BL and BH respectively.
0211      * Set up Tx Channel address field for use with PP Cmd Register.
0212      * Store address field in BH register.
0213      * Inputs:      AH = Output port number (0..3).
0214      *              AL = Data byte.
0215      *    command = TXDATA0 | INT_EN;
0216      * Align port num with address field (b1...b3),
0217      * set address for TXDatax, Strobe=0
0218      */
0219     command |= INT_EN;
0220 
0221     /* Disable interrupts so that the process is not interrupted, then 
0222      * write the address associated with the current Tx channel to the 
0223      * PP Command Reg.  Do not set the Strobe signal yet.
0224      */
0225 
0226     do {
0227         portman_write_command(pm, command);
0228 
0229         /* While the address lines settle, write parallel output data to 
0230          * PP Data Reg.  This has no effect until Strobe signal is asserted.
0231          */
0232 
0233         portman_write_data(pm, mididata);
0234         
0235         /* If PCP channel's TxEmpty is set (TxEmpty is read through the PP
0236          * Status Register), then go write data.  Else go back and wait.
0237          */
0238     } while ((portman_read_status(pm) & TXEMPTY) != TXEMPTY);
0239 
0240     /* TxEmpty is set.  Maintain PC/P destination address and assert
0241      * Strobe through the PP Command Reg.  This will Strobe data into
0242      * the PC/P transmitter and set the PC/P BUSY signal.
0243      */
0244 
0245     portman_write_command(pm, command | STROBE);
0246 
0247     /* Wait for strobe line to settle and echo back through hardware.
0248      * Once it has echoed back, assume that the address and data lines
0249      * have settled!
0250      */
0251 
0252     while ((portman_read_status(pm) & ESTB) == 0)
0253         cpu_relax();
0254 
0255     /* Release strobe and immediately re-allow interrupts. */
0256     portman_write_command(pm, command);
0257 
0258     while ((portman_read_status(pm) & ESTB) == ESTB)
0259         cpu_relax();
0260 
0261     /* PC/P BUSY is now set.  We must wait until BUSY resets itself.
0262      * We'll reenable ints while we're waiting.
0263      */
0264 
0265     while ((portman_read_status(pm) & BUSY) == BUSY)
0266         cpu_relax();
0267 
0268     /* Data sent. */
0269 }
0270 
0271 
0272 /*
0273  *  Read MIDI byte from port
0274  *  Attempt to read input byte from specified hardware input port (0..).
0275  *  Return -1 if no data
0276  */
0277 static int portman_read_midi(struct portman *pm, int port)
0278 {
0279     unsigned char midi_data = 0;
0280     unsigned char cmdout;   /* Saved address+IE bit. */
0281 
0282     /* Make sure clocking edge is down before starting... */
0283     portman_write_data(pm, 0);  /* Make sure edge is down. */
0284 
0285     /* Set destination address to PCP. */
0286     cmdout = (port << 1) | INT_EN;  /* Address + IE + No Strobe. */
0287     portman_write_command(pm, cmdout);
0288 
0289     while ((portman_read_status(pm) & ESTB) == ESTB)
0290         cpu_relax();    /* Wait for strobe echo. */
0291 
0292     /* After the address lines settle, check multiplexed RxAvail signal.
0293      * If data is available, read it.
0294      */
0295     if ((portman_read_status(pm) & RXAVAIL) == 0)
0296         return -1;  /* No data. */
0297 
0298     /* Set the Strobe signal to enable the Rx clocking circuitry. */
0299     portman_write_command(pm, cmdout | STROBE); /* Write address+IE+Strobe. */
0300 
0301     while ((portman_read_status(pm) & ESTB) == 0)
0302         cpu_relax(); /* Wait for strobe echo. */
0303 
0304     /* The first data bit (msb) is already sitting on the input line. */
0305     midi_data = (portman_read_status(pm) & 128);
0306     portman_write_data(pm, 1);  /* Cause rising edge, which shifts data. */
0307 
0308     /* Data bit 6. */
0309     portman_write_data(pm, 0);  /* Cause falling edge while data settles. */
0310     midi_data |= (portman_read_status(pm) >> 1) & 64;
0311     portman_write_data(pm, 1);  /* Cause rising edge, which shifts data. */
0312 
0313     /* Data bit 5. */
0314     portman_write_data(pm, 0);  /* Cause falling edge while data settles. */
0315     midi_data |= (portman_read_status(pm) >> 2) & 32;
0316     portman_write_data(pm, 1);  /* Cause rising edge, which shifts data. */
0317 
0318     /* Data bit 4. */
0319     portman_write_data(pm, 0);  /* Cause falling edge while data settles. */
0320     midi_data |= (portman_read_status(pm) >> 3) & 16;
0321     portman_write_data(pm, 1);  /* Cause rising edge, which shifts data. */
0322 
0323     /* Data bit 3. */
0324     portman_write_data(pm, 0);  /* Cause falling edge while data settles. */
0325     midi_data |= (portman_read_status(pm) >> 4) & 8;
0326     portman_write_data(pm, 1);  /* Cause rising edge, which shifts data. */
0327 
0328     /* Data bit 2. */
0329     portman_write_data(pm, 0);  /* Cause falling edge while data settles. */
0330     midi_data |= (portman_read_status(pm) >> 5) & 4;
0331     portman_write_data(pm, 1);  /* Cause rising edge, which shifts data. */
0332 
0333     /* Data bit 1. */
0334     portman_write_data(pm, 0);  /* Cause falling edge while data settles. */
0335     midi_data |= (portman_read_status(pm) >> 6) & 2;
0336     portman_write_data(pm, 1);  /* Cause rising edge, which shifts data. */
0337 
0338     /* Data bit 0. */
0339     portman_write_data(pm, 0);  /* Cause falling edge while data settles. */
0340     midi_data |= (portman_read_status(pm) >> 7) & 1;
0341     portman_write_data(pm, 1);  /* Cause rising edge, which shifts data. */
0342     portman_write_data(pm, 0);  /* Return data clock low. */
0343 
0344 
0345     /* De-assert Strobe and return data. */
0346     portman_write_command(pm, cmdout);  /* Output saved address+IE. */
0347 
0348     /* Wait for strobe echo. */
0349     while ((portman_read_status(pm) & ESTB) == ESTB)
0350         cpu_relax();
0351 
0352     return (midi_data & 255);   /* Shift back and return value. */
0353 }
0354 
0355 /*
0356  *  Checks if any input data on the given channel is available
0357  *  Checks RxAvail 
0358  */
0359 static int portman_data_avail(struct portman *pm, int channel)
0360 {
0361     int command = INT_EN;
0362     switch (channel) {
0363     case 0:
0364         command |= RXDATA0;
0365         break;
0366     case 1:
0367         command |= RXDATA1;
0368         break;
0369     }
0370     /* Write hardware (assumme STROBE=0) */
0371     portman_write_command(pm, command);
0372     /* Check multiplexed RxAvail signal */
0373     if ((portman_read_status(pm) & RXAVAIL) == RXAVAIL)
0374         return 1;   /* Data available */
0375 
0376     /* No Data available */
0377     return 0;
0378 }
0379 
0380 
0381 /*
0382  *  Flushes any input
0383  */
0384 static void portman_flush_input(struct portman *pm, unsigned char port)
0385 {
0386     /* Local variable for counting things */
0387     unsigned int i = 0;
0388     unsigned char command = 0;
0389 
0390     switch (port) {
0391     case 0:
0392         command = RXDATA0;
0393         break;
0394     case 1:
0395         command = RXDATA1;
0396         break;
0397     default:
0398         snd_printk(KERN_WARNING
0399                "portman_flush_input() Won't flush port %i\n",
0400                port);
0401         return;
0402     }
0403 
0404     /* Set address for specified channel in port and allow to settle. */
0405     portman_write_command(pm, command);
0406 
0407     /* Assert the Strobe and wait for echo back. */
0408     portman_write_command(pm, command | STROBE);
0409 
0410     /* Wait for ESTB */
0411     while ((portman_read_status(pm) & ESTB) == 0)
0412         cpu_relax();
0413 
0414     /* Output clock cycles to the Rx circuitry. */
0415     portman_write_data(pm, 0);
0416 
0417     /* Flush 250 bits... */
0418     for (i = 0; i < 250; i++) {
0419         portman_write_data(pm, 1);
0420         portman_write_data(pm, 0);
0421     }
0422 
0423     /* Deassert the Strobe signal of the port and wait for it to settle. */
0424     portman_write_command(pm, command | INT_EN);
0425 
0426     /* Wait for settling */
0427     while ((portman_read_status(pm) & ESTB) == ESTB)
0428         cpu_relax();
0429 }
0430 
0431 static int portman_probe(struct parport *p)
0432 {
0433     /* Initialize the parallel port data register.  Will set Rx clocks
0434      * low in case we happen to be addressing the Rx ports at this time.
0435      */
0436     /* 1 */
0437     parport_write_data(p, 0);
0438 
0439     /* Initialize the parallel port command register, thus initializing
0440      * hardware handshake lines to midi box:
0441      *
0442      *                                  Strobe = 0
0443      *                                  Interrupt Enable = 0            
0444      */
0445     /* 2 */
0446     parport_write_control(p, 0);
0447 
0448     /* Check if Portman PC/P 2x4 is out there. */
0449     /* 3 */
0450     parport_write_control(p, RXDATA0);  /* Write Strobe=0 to command reg. */
0451 
0452     /* Check for ESTB to be clear */
0453     /* 4 */
0454     if ((parport_read_status(p) & ESTB) == ESTB)
0455         return 1;   /* CODE 1 - Strobe Failure. */
0456 
0457     /* Set for RXDATA0 where no damage will be done. */
0458     /* 5 */
0459     parport_write_control(p, RXDATA0 | STROBE); /* Write Strobe=1 to command reg. */
0460 
0461     /* 6 */
0462     if ((parport_read_status(p) & ESTB) != ESTB)
0463         return 1;   /* CODE 1 - Strobe Failure. */
0464 
0465     /* 7 */
0466     parport_write_control(p, 0);    /* Reset Strobe=0. */
0467 
0468     /* Check if Tx circuitry is functioning properly.  If initialized 
0469      * unit TxEmpty is false, send out char and see if it goes true.
0470      */
0471     /* 8 */
0472     parport_write_control(p, TXDATA0);  /* Tx channel 0, strobe off. */
0473 
0474     /* If PCP channel's TxEmpty is set (TxEmpty is read through the PP
0475      * Status Register), then go write data.  Else go back and wait.
0476      */
0477     /* 9 */
0478     if ((parport_read_status(p) & TXEMPTY) == 0)
0479         return 2;
0480 
0481     /* Return OK status. */
0482     return 0;
0483 }
0484 
0485 static int portman_device_init(struct portman *pm)
0486 {
0487     portman_flush_input(pm, 0);
0488     portman_flush_input(pm, 1);
0489 
0490     return 0;
0491 }
0492 
0493 /*********************************************************************
0494  * Rawmidi
0495  *********************************************************************/
0496 static int snd_portman_midi_open(struct snd_rawmidi_substream *substream)
0497 {
0498     return 0;
0499 }
0500 
0501 static int snd_portman_midi_close(struct snd_rawmidi_substream *substream)
0502 {
0503     return 0;
0504 }
0505 
0506 static void snd_portman_midi_input_trigger(struct snd_rawmidi_substream *substream,
0507                        int up)
0508 {
0509     struct portman *pm = substream->rmidi->private_data;
0510     unsigned long flags;
0511 
0512     spin_lock_irqsave(&pm->reg_lock, flags);
0513     if (up)
0514         pm->mode[substream->number] |= PORTMAN2X4_MODE_INPUT_TRIGGERED;
0515     else
0516         pm->mode[substream->number] &= ~PORTMAN2X4_MODE_INPUT_TRIGGERED;
0517     spin_unlock_irqrestore(&pm->reg_lock, flags);
0518 }
0519 
0520 static void snd_portman_midi_output_trigger(struct snd_rawmidi_substream *substream,
0521                         int up)
0522 {
0523     struct portman *pm = substream->rmidi->private_data;
0524     unsigned long flags;
0525     unsigned char byte;
0526 
0527     spin_lock_irqsave(&pm->reg_lock, flags);
0528     if (up) {
0529         while ((snd_rawmidi_transmit(substream, &byte, 1) == 1))
0530             portman_write_midi(pm, substream->number, byte);
0531     }
0532     spin_unlock_irqrestore(&pm->reg_lock, flags);
0533 }
0534 
0535 static const struct snd_rawmidi_ops snd_portman_midi_output = {
0536     .open =     snd_portman_midi_open,
0537     .close =    snd_portman_midi_close,
0538     .trigger =  snd_portman_midi_output_trigger,
0539 };
0540 
0541 static const struct snd_rawmidi_ops snd_portman_midi_input = {
0542     .open =     snd_portman_midi_open,
0543     .close =    snd_portman_midi_close,
0544     .trigger =  snd_portman_midi_input_trigger,
0545 };
0546 
0547 /* Create and initialize the rawmidi component */
0548 static int snd_portman_rawmidi_create(struct snd_card *card)
0549 {
0550     struct portman *pm = card->private_data;
0551     struct snd_rawmidi *rmidi;
0552     struct snd_rawmidi_substream *substream;
0553     int err;
0554     
0555     err = snd_rawmidi_new(card, CARD_NAME, 0, 
0556                   PORTMAN_NUM_OUTPUT_PORTS, 
0557                   PORTMAN_NUM_INPUT_PORTS, 
0558                   &rmidi);
0559     if (err < 0) 
0560         return err;
0561 
0562     rmidi->private_data = pm;
0563     strcpy(rmidi->name, CARD_NAME);
0564     rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
0565                     SNDRV_RAWMIDI_INFO_INPUT |
0566                             SNDRV_RAWMIDI_INFO_DUPLEX;
0567 
0568     pm->rmidi = rmidi;
0569 
0570     /* register rawmidi ops */
0571     snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 
0572                 &snd_portman_midi_output);
0573     snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, 
0574                 &snd_portman_midi_input);
0575 
0576     /* name substreams */
0577     /* output */
0578     list_for_each_entry(substream,
0579                 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
0580                 list) {
0581         sprintf(substream->name,
0582             "Portman2x4 %d", substream->number+1);
0583     }
0584     /* input */
0585     list_for_each_entry(substream,
0586                 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
0587                 list) {
0588         pm->midi_input[substream->number] = substream;
0589         sprintf(substream->name,
0590             "Portman2x4 %d", substream->number+1);
0591     }
0592 
0593     return err;
0594 }
0595 
0596 /*********************************************************************
0597  * parport stuff
0598  *********************************************************************/
0599 static void snd_portman_interrupt(void *userdata)
0600 {
0601     unsigned char midivalue = 0;
0602     struct portman *pm = ((struct snd_card*)userdata)->private_data;
0603 
0604     spin_lock(&pm->reg_lock);
0605 
0606     /* While any input data is waiting */
0607     while ((portman_read_status(pm) & INT_REQ) == INT_REQ) {
0608         /* If data available on channel 0, 
0609            read it and stuff it into the queue. */
0610         if (portman_data_avail(pm, 0)) {
0611             /* Read Midi */
0612             midivalue = portman_read_midi(pm, 0);
0613             /* put midi into queue... */
0614             if (pm->mode[0] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
0615                 snd_rawmidi_receive(pm->midi_input[0],
0616                             &midivalue, 1);
0617 
0618         }
0619         /* If data available on channel 1, 
0620            read it and stuff it into the queue. */
0621         if (portman_data_avail(pm, 1)) {
0622             /* Read Midi */
0623             midivalue = portman_read_midi(pm, 1);
0624             /* put midi into queue... */
0625             if (pm->mode[1] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
0626                 snd_rawmidi_receive(pm->midi_input[1],
0627                             &midivalue, 1);
0628         }
0629 
0630     }
0631 
0632     spin_unlock(&pm->reg_lock);
0633 }
0634 
0635 static void snd_portman_attach(struct parport *p)
0636 {
0637     struct platform_device *device;
0638 
0639     device = platform_device_alloc(PLATFORM_DRIVER, device_count);
0640     if (!device)
0641         return;
0642 
0643     /* Temporary assignment to forward the parport */
0644     platform_set_drvdata(device, p);
0645 
0646     if (platform_device_add(device) < 0) {
0647         platform_device_put(device);
0648         return;
0649     }
0650 
0651     /* Since we dont get the return value of probe
0652      * We need to check if device probing succeeded or not */
0653     if (!platform_get_drvdata(device)) {
0654         platform_device_unregister(device);
0655         return;
0656     }
0657 
0658     /* register device in global table */
0659     platform_devices[device_count] = device;
0660     device_count++;
0661 }
0662 
0663 static void snd_portman_detach(struct parport *p)
0664 {
0665     /* nothing to do here */
0666 }
0667 
0668 static int snd_portman_dev_probe(struct pardevice *pardev)
0669 {
0670     if (strcmp(pardev->name, DRIVER_NAME))
0671         return -ENODEV;
0672 
0673     return 0;
0674 }
0675 
0676 static struct parport_driver portman_parport_driver = {
0677     .name       = "portman2x4",
0678     .probe      = snd_portman_dev_probe,
0679     .match_port = snd_portman_attach,
0680     .detach     = snd_portman_detach,
0681     .devmodel   = true,
0682 };
0683 
0684 /*********************************************************************
0685  * platform stuff
0686  *********************************************************************/
0687 static void snd_portman_card_private_free(struct snd_card *card)
0688 {
0689     struct portman *pm = card->private_data;
0690     struct pardevice *pardev = pm->pardev;
0691 
0692     if (pardev) {
0693         parport_release(pardev);
0694         parport_unregister_device(pardev);
0695     }
0696 
0697     portman_free(pm);
0698 }
0699 
0700 static int snd_portman_probe(struct platform_device *pdev)
0701 {
0702     struct pardevice *pardev;
0703     struct parport *p;
0704     int dev = pdev->id;
0705     struct snd_card *card = NULL;
0706     struct portman *pm = NULL;
0707     int err;
0708     struct pardev_cb portman_cb = {
0709         .preempt = NULL,
0710         .wakeup = NULL,
0711         .irq_func = snd_portman_interrupt,  /* ISR */
0712         .flags = PARPORT_DEV_EXCL,      /* flags */
0713     };
0714 
0715     p = platform_get_drvdata(pdev);
0716     platform_set_drvdata(pdev, NULL);
0717 
0718     if (dev >= SNDRV_CARDS)
0719         return -ENODEV;
0720     if (!enable[dev]) 
0721         return -ENOENT;
0722 
0723     err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
0724                0, &card);
0725     if (err < 0) {
0726         snd_printd("Cannot create card\n");
0727         return err;
0728     }
0729     strcpy(card->driver, DRIVER_NAME);
0730     strcpy(card->shortname, CARD_NAME);
0731     sprintf(card->longname,  "%s at 0x%lx, irq %i", 
0732         card->shortname, p->base, p->irq);
0733 
0734     portman_cb.private = card;             /* private */
0735     pardev = parport_register_dev_model(p,         /* port */
0736                         DRIVER_NAME,   /* name */
0737                         &portman_cb,   /* callbacks */
0738                         pdev->id);     /* device number */
0739     if (pardev == NULL) {
0740         snd_printd("Cannot register pardevice\n");
0741         err = -EIO;
0742         goto __err;
0743     }
0744 
0745     /* claim parport */
0746     if (parport_claim(pardev)) {
0747         snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
0748         err = -EIO;
0749         goto free_pardev;
0750     }
0751 
0752     err = portman_create(card, pardev, &pm);
0753     if (err < 0) {
0754         snd_printd("Cannot create main component\n");
0755         goto release_pardev;
0756     }
0757     card->private_data = pm;
0758     card->private_free = snd_portman_card_private_free;
0759 
0760     err = portman_probe(p);
0761     if (err) {
0762         err = -EIO;
0763         goto __err;
0764     }
0765     
0766     err = snd_portman_rawmidi_create(card);
0767     if (err < 0) {
0768         snd_printd("Creating Rawmidi component failed\n");
0769         goto __err;
0770     }
0771 
0772     /* init device */
0773     err = portman_device_init(pm);
0774     if (err < 0)
0775         goto __err;
0776 
0777     platform_set_drvdata(pdev, card);
0778 
0779     /* At this point card will be usable */
0780     err = snd_card_register(card);
0781     if (err < 0) {
0782         snd_printd("Cannot register card\n");
0783         goto __err;
0784     }
0785 
0786     snd_printk(KERN_INFO "Portman 2x4 on 0x%lx\n", p->base);
0787     return 0;
0788 
0789 release_pardev:
0790     parport_release(pardev);
0791 free_pardev:
0792     parport_unregister_device(pardev);
0793 __err:
0794     snd_card_free(card);
0795     return err;
0796 }
0797 
0798 static int snd_portman_remove(struct platform_device *pdev)
0799 {
0800     struct snd_card *card = platform_get_drvdata(pdev);
0801 
0802     if (card)
0803         snd_card_free(card);
0804 
0805     return 0;
0806 }
0807 
0808 
0809 static struct platform_driver snd_portman_driver = {
0810     .probe  = snd_portman_probe,
0811     .remove = snd_portman_remove,
0812     .driver = {
0813         .name = PLATFORM_DRIVER,
0814     }
0815 };
0816 
0817 /*********************************************************************
0818  * module init stuff
0819  *********************************************************************/
0820 static void snd_portman_unregister_all(void)
0821 {
0822     int i;
0823 
0824     for (i = 0; i < SNDRV_CARDS; ++i) {
0825         if (platform_devices[i]) {
0826             platform_device_unregister(platform_devices[i]);
0827             platform_devices[i] = NULL;
0828         }
0829     }       
0830     platform_driver_unregister(&snd_portman_driver);
0831     parport_unregister_driver(&portman_parport_driver);
0832 }
0833 
0834 static int __init snd_portman_module_init(void)
0835 {
0836     int err;
0837 
0838     err = platform_driver_register(&snd_portman_driver);
0839     if (err < 0)
0840         return err;
0841 
0842     if (parport_register_driver(&portman_parport_driver) != 0) {
0843         platform_driver_unregister(&snd_portman_driver);
0844         return -EIO;
0845     }
0846 
0847     if (device_count == 0) {
0848         snd_portman_unregister_all();
0849         return -ENODEV;
0850     }
0851 
0852     return 0;
0853 }
0854 
0855 static void __exit snd_portman_module_exit(void)
0856 {
0857     snd_portman_unregister_all();
0858 }
0859 
0860 module_init(snd_portman_module_init);
0861 module_exit(snd_portman_module_exit);