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