1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
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
107
108
109
110#define PP_STAT_BSY 0x80
111#define PP_STAT_ACK 0x40
112#define PP_STAT_POUT 0x20
113#define PP_STAT_SEL 0x10
114#define PP_STAT_ERR 0x08
115
116
117#define PP_CMD_IEN 0x10
118#define PP_CMD_SELI 0x08
119#define PP_CMD_INIT 0x04
120#define PP_CMD_FEED 0x02
121#define PP_CMD_STB 0x01
122
123
124#define INT_EN PP_CMD_IEN
125#define STROBE PP_CMD_STB
126
127
128
129
130
131
132#define RXDATA0 (0 << 1)
133#define RXDATA1 (1 << 1)
134#define GEN_CTL (2 << 1)
135#define SYNC_CTL (3 << 1)
136#define TXDATA0 (4 << 1)
137#define TXDATA1 (5 << 1)
138#define TXDATA2 (6 << 1)
139#define TXDATA3 (7 << 1)
140
141
142#define ESTB PP_STAT_POUT
143#define INT_REQ PP_STAT_ACK
144#define BUSY PP_STAT_ERR
145
146
147
148
149
150
151
152
153
154#define RXAVAIL PP_STAT_SEL
155
156#define SYNC_STAT PP_STAT_SEL
157
158#define TXEMPTY PP_STAT_SEL
159
160
161
162
163
164
165
166#define RXDATA PP_STAT_BSY
167
168#define SYNC_DATA PP_STAT_BSY
169
170#define DATA_ECHO PP_STAT_BSY
171#define A0_ECHO PP_STAT_BSY
172#define A1_ECHO PP_STAT_BSY
173#define A2_ECHO PP_STAT_BSY
174
175#define PORTMAN2X4_MODE_INPUT_TRIGGERED 0x01
176
177
178
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
211
212
213
214
215
216
217
218
219 command |= INT_EN;
220
221
222
223
224
225
226 do {
227 portman_write_command(pm, command);
228
229
230
231
232
233 portman_write_data(pm, mididata);
234
235
236
237
238 } while ((portman_read_status(pm) & TXEMPTY) != TXEMPTY);
239
240
241
242
243
244
245 portman_write_command(pm, command | STROBE);
246
247
248
249
250
251
252 while ((portman_read_status(pm) & ESTB) == 0)
253 cpu_relax();
254
255
256 portman_write_command(pm, command);
257
258 while ((portman_read_status(pm) & ESTB) == ESTB)
259 cpu_relax();
260
261
262
263
264
265 while ((portman_read_status(pm) & BUSY) == BUSY)
266 cpu_relax();
267
268
269}
270
271
272
273
274
275
276
277static int portman_read_midi(struct portman *pm, int port)
278{
279 unsigned char midi_data = 0;
280 unsigned char cmdout;
281
282
283 portman_write_data(pm, 0);
284
285
286 cmdout = (port << 1) | INT_EN;
287 portman_write_command(pm, cmdout);
288
289 while ((portman_read_status(pm) & ESTB) == ESTB)
290 cpu_relax();
291
292
293
294
295 if ((portman_read_status(pm) & RXAVAIL) == 0)
296 return -1;
297
298
299 portman_write_command(pm, cmdout | STROBE);
300
301 while ((portman_read_status(pm) & ESTB) == 0)
302 cpu_relax();
303
304
305 midi_data = (portman_read_status(pm) & 128);
306 portman_write_data(pm, 1);
307
308
309 portman_write_data(pm, 0);
310 midi_data |= (portman_read_status(pm) >> 1) & 64;
311 portman_write_data(pm, 1);
312
313
314 portman_write_data(pm, 0);
315 midi_data |= (portman_read_status(pm) >> 2) & 32;
316 portman_write_data(pm, 1);
317
318
319 portman_write_data(pm, 0);
320 midi_data |= (portman_read_status(pm) >> 3) & 16;
321 portman_write_data(pm, 1);
322
323
324 portman_write_data(pm, 0);
325 midi_data |= (portman_read_status(pm) >> 4) & 8;
326 portman_write_data(pm, 1);
327
328
329 portman_write_data(pm, 0);
330 midi_data |= (portman_read_status(pm) >> 5) & 4;
331 portman_write_data(pm, 1);
332
333
334 portman_write_data(pm, 0);
335 midi_data |= (portman_read_status(pm) >> 6) & 2;
336 portman_write_data(pm, 1);
337
338
339 portman_write_data(pm, 0);
340 midi_data |= (portman_read_status(pm) >> 7) & 1;
341 portman_write_data(pm, 1);
342 portman_write_data(pm, 0);
343
344
345
346 portman_write_command(pm, cmdout);
347
348
349 while ((portman_read_status(pm) & ESTB) == ESTB)
350 cpu_relax();
351
352 return (midi_data & 255);
353}
354
355
356
357
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
371 portman_write_command(pm, command);
372
373 if ((portman_read_status(pm) & RXAVAIL) == RXAVAIL)
374 return 1;
375
376
377 return 0;
378}
379
380
381
382
383
384static void portman_flush_input(struct portman *pm, unsigned char port)
385{
386
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
405 portman_write_command(pm, command);
406
407
408 portman_write_command(pm, command | STROBE);
409
410
411 while ((portman_read_status(pm) & ESTB) == 0)
412 cpu_relax();
413
414
415 portman_write_data(pm, 0);
416
417
418 for (i = 0; i < 250; i++) {
419 portman_write_data(pm, 1);
420 portman_write_data(pm, 0);
421 }
422
423
424 portman_write_command(pm, command | INT_EN);
425
426
427 while ((portman_read_status(pm) & ESTB) == ESTB)
428 cpu_relax();
429}
430
431static int portman_probe(struct parport *p)
432{
433
434
435
436
437 parport_write_data(p, 0);
438
439
440
441
442
443
444
445
446 parport_write_control(p, 0);
447
448
449
450 parport_write_control(p, RXDATA0);
451
452
453
454 if ((parport_read_status(p) & ESTB) == ESTB)
455 return 1;
456
457
458
459 parport_write_control(p, RXDATA0 | STROBE);
460
461
462 if ((parport_read_status(p) & ESTB) != ESTB)
463 return 1;
464
465
466 parport_write_control(p, 0);
467
468
469
470
471
472 parport_write_control(p, TXDATA0);
473
474
475
476
477
478 if ((parport_read_status(p) & TXEMPTY) == 0)
479 return 2;
480
481
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
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
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
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
577
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
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
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
607 while ((portman_read_status(pm) & INT_REQ) == INT_REQ) {
608
609
610 if (portman_data_avail(pm, 0)) {
611
612 midivalue = portman_read_midi(pm, 0);
613
614 if (pm->mode[0] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
615 snd_rawmidi_receive(pm->midi_input[0],
616 &midivalue, 1);
617
618 }
619
620
621 if (portman_data_avail(pm, 1)) {
622
623 midivalue = portman_read_midi(pm, 1);
624
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
644 platform_set_drvdata(device, p);
645
646 if (platform_device_add(device) < 0) {
647 platform_device_put(device);
648 return;
649 }
650
651
652
653 if (!platform_get_drvdata(device)) {
654 platform_device_unregister(device);
655 return;
656 }
657
658
659 platform_devices[device_count] = device;
660 device_count++;
661}
662
663static void snd_portman_detach(struct parport *p)
664{
665
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
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,
712 .flags = PARPORT_DEV_EXCL,
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;
735 pardev = parport_register_dev_model(p,
736 DRIVER_NAME,
737 &portman_cb,
738 pdev->id);
739 if (pardev == NULL) {
740 snd_printd("Cannot register pardevice\n");
741 err = -EIO;
742 goto __err;
743 }
744
745
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
773 err = portman_device_init(pm);
774 if (err < 0)
775 goto __err;
776
777 platform_set_drvdata(pdev, card);
778
779
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
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);
862