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
28
29
30
31
32
33
34
35
36
37
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
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
99static int portman_create(struct snd_card *card,
100 struct pardevice *pardev,
101 struct portman **rchip)
102{
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
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
123
124
125
126#define PP_STAT_BSY 0x80
127#define PP_STAT_ACK 0x40
128#define PP_STAT_POUT 0x20
129#define PP_STAT_SEL 0x10
130#define PP_STAT_ERR 0x08
131
132
133#define PP_CMD_IEN 0x10
134#define PP_CMD_SELI 0x08
135#define PP_CMD_INIT 0x04
136#define PP_CMD_FEED 0x02
137#define PP_CMD_STB 0x01
138
139
140#define INT_EN PP_CMD_IEN
141#define STROBE PP_CMD_STB
142
143
144
145
146
147
148#define RXDATA0 (0 << 1)
149#define RXDATA1 (1 << 1)
150#define GEN_CTL (2 << 1)
151#define SYNC_CTL (3 << 1)
152#define TXDATA0 (4 << 1)
153#define TXDATA1 (5 << 1)
154#define TXDATA2 (6 << 1)
155#define TXDATA3 (7 << 1)
156
157
158#define ESTB PP_STAT_POUT
159#define INT_REQ PP_STAT_ACK
160#define BUSY PP_STAT_ERR
161
162
163
164
165
166
167
168
169
170#define RXAVAIL PP_STAT_SEL
171
172#define SYNC_STAT PP_STAT_SEL
173
174#define TXEMPTY PP_STAT_SEL
175
176
177
178
179
180
181
182#define RXDATA PP_STAT_BSY
183
184#define SYNC_DATA PP_STAT_BSY
185
186#define DATA_ECHO PP_STAT_BSY
187#define A0_ECHO PP_STAT_BSY
188#define A1_ECHO PP_STAT_BSY
189#define A2_ECHO PP_STAT_BSY
190
191#define PORTMAN2X4_MODE_INPUT_TRIGGERED 0x01
192
193
194
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
227
228
229
230
231
232
233
234
235 command |= INT_EN;
236
237
238
239
240
241
242 do {
243 portman_write_command(pm, command);
244
245
246
247
248
249 portman_write_data(pm, mididata);
250
251
252
253
254 } while ((portman_read_status(pm) & TXEMPTY) != TXEMPTY);
255
256
257
258
259
260
261 portman_write_command(pm, command | STROBE);
262
263
264
265
266
267
268 while ((portman_read_status(pm) & ESTB) == 0)
269 cpu_relax();
270
271
272 portman_write_command(pm, command);
273
274 while ((portman_read_status(pm) & ESTB) == ESTB)
275 cpu_relax();
276
277
278
279
280
281 while ((portman_read_status(pm) & BUSY) == BUSY)
282 cpu_relax();
283
284
285}
286
287
288
289
290
291
292
293static int portman_read_midi(struct portman *pm, int port)
294{
295 unsigned char midi_data = 0;
296 unsigned char cmdout;
297
298
299 portman_write_data(pm, 0);
300
301
302 cmdout = (port << 1) | INT_EN;
303 portman_write_command(pm, cmdout);
304
305 while ((portman_read_status(pm) & ESTB) == ESTB)
306 cpu_relax();
307
308
309
310
311 if ((portman_read_status(pm) & RXAVAIL) == 0)
312 return -1;
313
314
315 portman_write_command(pm, cmdout | STROBE);
316
317 while ((portman_read_status(pm) & ESTB) == 0)
318 cpu_relax();
319
320
321 midi_data = (portman_read_status(pm) & 128);
322 portman_write_data(pm, 1);
323
324
325 portman_write_data(pm, 0);
326 midi_data |= (portman_read_status(pm) >> 1) & 64;
327 portman_write_data(pm, 1);
328
329
330 portman_write_data(pm, 0);
331 midi_data |= (portman_read_status(pm) >> 2) & 32;
332 portman_write_data(pm, 1);
333
334
335 portman_write_data(pm, 0);
336 midi_data |= (portman_read_status(pm) >> 3) & 16;
337 portman_write_data(pm, 1);
338
339
340 portman_write_data(pm, 0);
341 midi_data |= (portman_read_status(pm) >> 4) & 8;
342 portman_write_data(pm, 1);
343
344
345 portman_write_data(pm, 0);
346 midi_data |= (portman_read_status(pm) >> 5) & 4;
347 portman_write_data(pm, 1);
348
349
350 portman_write_data(pm, 0);
351 midi_data |= (portman_read_status(pm) >> 6) & 2;
352 portman_write_data(pm, 1);
353
354
355 portman_write_data(pm, 0);
356 midi_data |= (portman_read_status(pm) >> 7) & 1;
357 portman_write_data(pm, 1);
358 portman_write_data(pm, 0);
359
360
361
362 portman_write_command(pm, cmdout);
363
364
365 while ((portman_read_status(pm) & ESTB) == ESTB)
366 cpu_relax();
367
368 return (midi_data & 255);
369}
370
371
372
373
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
387 portman_write_command(pm, command);
388
389 if ((portman_read_status(pm) & RXAVAIL) == RXAVAIL)
390 return 1;
391
392
393 return 0;
394}
395
396
397
398
399
400static void portman_flush_input(struct portman *pm, unsigned char port)
401{
402
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
421 portman_write_command(pm, command);
422
423
424 portman_write_command(pm, command | STROBE);
425
426
427 while ((portman_read_status(pm) & ESTB) == 0)
428 cpu_relax();
429
430
431 portman_write_data(pm, 0);
432
433
434 for (i = 0; i < 250; i++) {
435 portman_write_data(pm, 1);
436 portman_write_data(pm, 0);
437 }
438
439
440 portman_write_command(pm, command | INT_EN);
441
442
443 while ((portman_read_status(pm) & ESTB) == ESTB)
444 cpu_relax();
445}
446
447static int portman_probe(struct parport *p)
448{
449
450
451
452
453 parport_write_data(p, 0);
454
455
456
457
458
459
460
461
462 parport_write_control(p, 0);
463
464
465
466 parport_write_control(p, RXDATA0);
467
468
469
470 if ((parport_read_status(p) & ESTB) == ESTB)
471 return 1;
472
473
474
475 parport_write_control(p, RXDATA0 + STROBE);
476
477
478 if ((parport_read_status(p) & ESTB) != ESTB)
479 return 1;
480
481
482 parport_write_control(p, 0);
483
484
485
486
487
488 parport_write_control(p, TXDATA0);
489
490
491
492
493
494 if ((parport_read_status(p) & TXEMPTY) == 0)
495 return 2;
496
497
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
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
564static int snd_portman_rawmidi_create(struct snd_card *card)
565{
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
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
593
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
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
614
615static void snd_portman_interrupt(void *userdata)
616{
617 unsigned char midivalue = 0;
618 struct portman *pm = ((struct snd_card*)userdata)->private_data;
619
620 spin_lock(&pm->reg_lock);
621
622
623 while ((portman_read_status(pm) & INT_REQ) == INT_REQ) {
624
625
626 if (portman_data_avail(pm, 0)) {
627
628 midivalue = portman_read_midi(pm, 0);
629
630 if (pm->mode[0] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
631 snd_rawmidi_receive(pm->midi_input[0],
632 &midivalue, 1);
633
634 }
635
636
637 if (portman_data_avail(pm, 1)) {
638
639 midivalue = portman_read_midi(pm, 1);
640
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
651static int snd_portman_probe_port(struct parport *p)
652{
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
672 return res ? -EIO : 0;
673}
674
675static void snd_portman_attach(struct parport *p)
676{
677 struct platform_device *device;
678
679 device = platform_device_alloc(PLATFORM_DRIVER, device_count);
680 if (!device)
681 return;
682
683
684 platform_set_drvdata(device, p);
685
686 if (platform_device_add(device) < 0) {
687 platform_device_put(device);
688 return;
689 }
690
691
692
693 if (!platform_get_drvdata(device)) {
694 platform_device_unregister(device);
695 return;
696 }
697
698
699 platform_devices[device_count] = device;
700 device_count++;
701}
702
703static void snd_portman_detach(struct parport *p)
704{
705
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
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
731static int snd_portman_probe(struct platform_device *pdev)
732{
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
751 err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
752 0, &card);
753 if (err < 0) {
754 snd_printd("Cannot create card\n");
755 return err;
756 }
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,
763 DRIVER_NAME,
764 NULL,
765 NULL,
766 snd_portman_interrupt,
767 PARPORT_DEV_EXCL,
768 (void *)card);
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
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
797 if ((err = portman_device_init(pm)) < 0)
798 goto __err;
799
800 platform_set_drvdata(pdev, card);
801
802
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
816static int snd_portman_remove(struct platform_device *pdev)
817{
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,
829 .remove = snd_portman_remove,
830 .driver = {
831 .name = PLATFORM_DRIVER,
832 }
833};
834
835
836
837
838static void snd_portman_unregister_all(void)
839{
840 int i;
841
842 for (i = 0; i < SNDRV_CARDS; ++i) {
843 if (platform_devices[i]) {
844 platform_device_unregister(platform_devices[i]);
845 platform_devices[i] = NULL;
846 }
847 }
848 platform_driver_unregister(&snd_portman_driver);
849 parport_unregister_driver(&portman_parport_driver);
850}
851
852static int __init snd_portman_module_init(void)
853{
854 int err;
855
856 if ((err = platform_driver_register(&snd_portman_driver)) < 0)
857 return err;
858
859 if (parport_register_driver(&portman_parport_driver) != 0) {
860 platform_driver_unregister(&snd_portman_driver);
861 return -EIO;
862 }
863
864 if (device_count == 0) {
865 snd_portman_unregister_all();
866 return -ENODEV;
867 }
868
869 return 0;
870}
871
872static void __exit snd_portman_module_exit(void)
873{
874 snd_portman_unregister_all();
875}
876
877module_init(snd_portman_module_init);
878module_exit(snd_portman_module_exit);
879