This source file includes following definitions.
- portman_free
- portman_create
- portman_write_command
- portman_read_command
- portman_read_status
- portman_read_data
- portman_write_data
- portman_write_midi
- portman_read_midi
- portman_data_avail
- portman_flush_input
- portman_probe
- portman_device_init
- snd_portman_midi_open
- snd_portman_midi_close
- snd_portman_midi_input_trigger
- snd_portman_midi_output_trigger
- snd_portman_rawmidi_create
- snd_portman_interrupt
- snd_portman_attach
- snd_portman_detach
- snd_portman_dev_probe
- snd_portman_card_private_free
- snd_portman_probe
- snd_portman_remove
- snd_portman_unregister_all
- snd_portman_module_init
- snd_portman_module_exit
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
43 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
44 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
45 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
46
47 static struct platform_device *platform_devices[SNDRV_CARDS];
48 static int device_count;
49
50 module_param_array(index, int, NULL, 0444);
51 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
52 module_param_array(id, charp, NULL, 0444);
53 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
54 module_param_array(enable, bool, NULL, 0444);
55 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
56
57 MODULE_AUTHOR("Levent Guendogdu, Tobias Gehrig, Matthias Koenig");
58 MODULE_DESCRIPTION("Midiman Portman2x4");
59 MODULE_LICENSE("GPL");
60 MODULE_SUPPORTED_DEVICE("{{Midiman,Portman2x4}}");
61
62
63
64
65 #define PORTMAN_NUM_INPUT_PORTS 2
66 #define PORTMAN_NUM_OUTPUT_PORTS 4
67
68 struct portman {
69 spinlock_t reg_lock;
70 struct snd_card *card;
71 struct snd_rawmidi *rmidi;
72 struct pardevice *pardev;
73 int open_count;
74 int mode[PORTMAN_NUM_INPUT_PORTS];
75 struct snd_rawmidi_substream *midi_input[PORTMAN_NUM_INPUT_PORTS];
76 };
77
78 static int portman_free(struct portman *pm)
79 {
80 kfree(pm);
81 return 0;
82 }
83
84 static int portman_create(struct snd_card *card,
85 struct pardevice *pardev,
86 struct portman **rchip)
87 {
88 struct portman *pm;
89
90 *rchip = NULL;
91
92 pm = kzalloc(sizeof(struct portman), GFP_KERNEL);
93 if (pm == NULL)
94 return -ENOMEM;
95
96
97 spin_lock_init(&pm->reg_lock);
98 pm->card = card;
99 pm->pardev = pardev;
100
101 *rchip = pm;
102
103 return 0;
104 }
105
106
107
108
109
110
111 #define PP_STAT_BSY 0x80
112 #define PP_STAT_ACK 0x40
113 #define PP_STAT_POUT 0x20
114 #define PP_STAT_SEL 0x10
115 #define PP_STAT_ERR 0x08
116
117
118 #define PP_CMD_IEN 0x10
119 #define PP_CMD_SELI 0x08
120 #define PP_CMD_INIT 0x04
121 #define PP_CMD_FEED 0x02
122 #define PP_CMD_STB 0x01
123
124
125 #define INT_EN PP_CMD_IEN
126 #define STROBE PP_CMD_STB
127
128
129
130
131
132
133 #define RXDATA0 (0 << 1)
134 #define RXDATA1 (1 << 1)
135 #define GEN_CTL (2 << 1)
136 #define SYNC_CTL (3 << 1)
137 #define TXDATA0 (4 << 1)
138 #define TXDATA1 (5 << 1)
139 #define TXDATA2 (6 << 1)
140 #define TXDATA3 (7 << 1)
141
142
143 #define ESTB PP_STAT_POUT
144 #define INT_REQ PP_STAT_ACK
145 #define BUSY PP_STAT_ERR
146
147
148
149
150
151
152
153
154
155 #define RXAVAIL PP_STAT_SEL
156
157 #define SYNC_STAT PP_STAT_SEL
158
159 #define TXEMPTY PP_STAT_SEL
160
161
162
163
164
165
166
167 #define RXDATA PP_STAT_BSY
168
169 #define SYNC_DATA PP_STAT_BSY
170
171 #define DATA_ECHO PP_STAT_BSY
172 #define A0_ECHO PP_STAT_BSY
173 #define A1_ECHO PP_STAT_BSY
174 #define A2_ECHO PP_STAT_BSY
175
176 #define PORTMAN2X4_MODE_INPUT_TRIGGERED 0x01
177
178
179
180
181 static inline void portman_write_command(struct portman *pm, u8 value)
182 {
183 parport_write_control(pm->pardev->port, value);
184 }
185
186 static inline u8 portman_read_command(struct portman *pm)
187 {
188 return parport_read_control(pm->pardev->port);
189 }
190
191 static inline u8 portman_read_status(struct portman *pm)
192 {
193 return parport_read_status(pm->pardev->port);
194 }
195
196 static inline u8 portman_read_data(struct portman *pm)
197 {
198 return parport_read_data(pm->pardev->port);
199 }
200
201 static inline void portman_write_data(struct portman *pm, u8 value)
202 {
203 parport_write_data(pm->pardev->port, value);
204 }
205
206 static void portman_write_midi(struct portman *pm,
207 int port, u8 mididata)
208 {
209 int command = ((port + 4) << 1);
210
211
212
213
214
215
216
217
218
219
220 command |= INT_EN;
221
222
223
224
225
226
227 do {
228 portman_write_command(pm, command);
229
230
231
232
233
234 portman_write_data(pm, mididata);
235
236
237
238
239 } while ((portman_read_status(pm) & TXEMPTY) != TXEMPTY);
240
241
242
243
244
245
246 portman_write_command(pm, command | STROBE);
247
248
249
250
251
252
253 while ((portman_read_status(pm) & ESTB) == 0)
254 cpu_relax();
255
256
257 portman_write_command(pm, command);
258
259 while ((portman_read_status(pm) & ESTB) == ESTB)
260 cpu_relax();
261
262
263
264
265
266 while ((portman_read_status(pm) & BUSY) == BUSY)
267 cpu_relax();
268
269
270 }
271
272
273
274
275
276
277
278 static int portman_read_midi(struct portman *pm, int port)
279 {
280 unsigned char midi_data = 0;
281 unsigned char cmdout;
282
283
284 portman_write_data(pm, 0);
285
286
287 cmdout = (port << 1) | INT_EN;
288 portman_write_command(pm, cmdout);
289
290 while ((portman_read_status(pm) & ESTB) == ESTB)
291 cpu_relax();
292
293
294
295
296 if ((portman_read_status(pm) & RXAVAIL) == 0)
297 return -1;
298
299
300 portman_write_command(pm, cmdout | STROBE);
301
302 while ((portman_read_status(pm) & ESTB) == 0)
303 cpu_relax();
304
305
306 midi_data = (portman_read_status(pm) & 128);
307 portman_write_data(pm, 1);
308
309
310 portman_write_data(pm, 0);
311 midi_data |= (portman_read_status(pm) >> 1) & 64;
312 portman_write_data(pm, 1);
313
314
315 portman_write_data(pm, 0);
316 midi_data |= (portman_read_status(pm) >> 2) & 32;
317 portman_write_data(pm, 1);
318
319
320 portman_write_data(pm, 0);
321 midi_data |= (portman_read_status(pm) >> 3) & 16;
322 portman_write_data(pm, 1);
323
324
325 portman_write_data(pm, 0);
326 midi_data |= (portman_read_status(pm) >> 4) & 8;
327 portman_write_data(pm, 1);
328
329
330 portman_write_data(pm, 0);
331 midi_data |= (portman_read_status(pm) >> 5) & 4;
332 portman_write_data(pm, 1);
333
334
335 portman_write_data(pm, 0);
336 midi_data |= (portman_read_status(pm) >> 6) & 2;
337 portman_write_data(pm, 1);
338
339
340 portman_write_data(pm, 0);
341 midi_data |= (portman_read_status(pm) >> 7) & 1;
342 portman_write_data(pm, 1);
343 portman_write_data(pm, 0);
344
345
346
347 portman_write_command(pm, cmdout);
348
349
350 while ((portman_read_status(pm) & ESTB) == ESTB)
351 cpu_relax();
352
353 return (midi_data & 255);
354 }
355
356
357
358
359
360 static int portman_data_avail(struct portman *pm, int channel)
361 {
362 int command = INT_EN;
363 switch (channel) {
364 case 0:
365 command |= RXDATA0;
366 break;
367 case 1:
368 command |= RXDATA1;
369 break;
370 }
371
372 portman_write_command(pm, command);
373
374 if ((portman_read_status(pm) & RXAVAIL) == RXAVAIL)
375 return 1;
376
377
378 return 0;
379 }
380
381
382
383
384
385 static void portman_flush_input(struct portman *pm, unsigned char port)
386 {
387
388 unsigned int i = 0;
389 unsigned char command = 0;
390
391 switch (port) {
392 case 0:
393 command = RXDATA0;
394 break;
395 case 1:
396 command = RXDATA1;
397 break;
398 default:
399 snd_printk(KERN_WARNING
400 "portman_flush_input() Won't flush port %i\n",
401 port);
402 return;
403 }
404
405
406 portman_write_command(pm, command);
407
408
409 portman_write_command(pm, command | STROBE);
410
411
412 while ((portman_read_status(pm) & ESTB) == 0)
413 cpu_relax();
414
415
416 portman_write_data(pm, 0);
417
418
419 for (i = 0; i < 250; i++) {
420 portman_write_data(pm, 1);
421 portman_write_data(pm, 0);
422 }
423
424
425 portman_write_command(pm, command | INT_EN);
426
427
428 while ((portman_read_status(pm) & ESTB) == ESTB)
429 cpu_relax();
430 }
431
432 static int portman_probe(struct parport *p)
433 {
434
435
436
437
438 parport_write_data(p, 0);
439
440
441
442
443
444
445
446
447 parport_write_control(p, 0);
448
449
450
451 parport_write_control(p, RXDATA0);
452
453
454
455 if ((parport_read_status(p) & ESTB) == ESTB)
456 return 1;
457
458
459
460 parport_write_control(p, RXDATA0 + STROBE);
461
462
463 if ((parport_read_status(p) & ESTB) != ESTB)
464 return 1;
465
466
467 parport_write_control(p, 0);
468
469
470
471
472
473 parport_write_control(p, TXDATA0);
474
475
476
477
478
479 if ((parport_read_status(p) & TXEMPTY) == 0)
480 return 2;
481
482
483 return 0;
484 }
485
486 static int portman_device_init(struct portman *pm)
487 {
488 portman_flush_input(pm, 0);
489 portman_flush_input(pm, 1);
490
491 return 0;
492 }
493
494
495
496
497 static int snd_portman_midi_open(struct snd_rawmidi_substream *substream)
498 {
499 return 0;
500 }
501
502 static int snd_portman_midi_close(struct snd_rawmidi_substream *substream)
503 {
504 return 0;
505 }
506
507 static void snd_portman_midi_input_trigger(struct snd_rawmidi_substream *substream,
508 int up)
509 {
510 struct portman *pm = substream->rmidi->private_data;
511 unsigned long flags;
512
513 spin_lock_irqsave(&pm->reg_lock, flags);
514 if (up)
515 pm->mode[substream->number] |= PORTMAN2X4_MODE_INPUT_TRIGGERED;
516 else
517 pm->mode[substream->number] &= ~PORTMAN2X4_MODE_INPUT_TRIGGERED;
518 spin_unlock_irqrestore(&pm->reg_lock, flags);
519 }
520
521 static void snd_portman_midi_output_trigger(struct snd_rawmidi_substream *substream,
522 int up)
523 {
524 struct portman *pm = substream->rmidi->private_data;
525 unsigned long flags;
526 unsigned char byte;
527
528 spin_lock_irqsave(&pm->reg_lock, flags);
529 if (up) {
530 while ((snd_rawmidi_transmit(substream, &byte, 1) == 1))
531 portman_write_midi(pm, substream->number, byte);
532 }
533 spin_unlock_irqrestore(&pm->reg_lock, flags);
534 }
535
536 static const struct snd_rawmidi_ops snd_portman_midi_output = {
537 .open = snd_portman_midi_open,
538 .close = snd_portman_midi_close,
539 .trigger = snd_portman_midi_output_trigger,
540 };
541
542 static const struct snd_rawmidi_ops snd_portman_midi_input = {
543 .open = snd_portman_midi_open,
544 .close = snd_portman_midi_close,
545 .trigger = snd_portman_midi_input_trigger,
546 };
547
548
549 static int snd_portman_rawmidi_create(struct snd_card *card)
550 {
551 struct portman *pm = card->private_data;
552 struct snd_rawmidi *rmidi;
553 struct snd_rawmidi_substream *substream;
554 int err;
555
556 err = snd_rawmidi_new(card, CARD_NAME, 0,
557 PORTMAN_NUM_OUTPUT_PORTS,
558 PORTMAN_NUM_INPUT_PORTS,
559 &rmidi);
560 if (err < 0)
561 return err;
562
563 rmidi->private_data = pm;
564 strcpy(rmidi->name, CARD_NAME);
565 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
566 SNDRV_RAWMIDI_INFO_INPUT |
567 SNDRV_RAWMIDI_INFO_DUPLEX;
568
569 pm->rmidi = rmidi;
570
571
572 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
573 &snd_portman_midi_output);
574 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
575 &snd_portman_midi_input);
576
577
578
579 list_for_each_entry(substream,
580 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
581 list) {
582 sprintf(substream->name,
583 "Portman2x4 %d", substream->number+1);
584 }
585
586 list_for_each_entry(substream,
587 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
588 list) {
589 pm->midi_input[substream->number] = substream;
590 sprintf(substream->name,
591 "Portman2x4 %d", substream->number+1);
592 }
593
594 return err;
595 }
596
597
598
599
600 static void snd_portman_interrupt(void *userdata)
601 {
602 unsigned char midivalue = 0;
603 struct portman *pm = ((struct snd_card*)userdata)->private_data;
604
605 spin_lock(&pm->reg_lock);
606
607
608 while ((portman_read_status(pm) & INT_REQ) == INT_REQ) {
609
610
611 if (portman_data_avail(pm, 0)) {
612
613 midivalue = portman_read_midi(pm, 0);
614
615 if (pm->mode[0] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
616 snd_rawmidi_receive(pm->midi_input[0],
617 &midivalue, 1);
618
619 }
620
621
622 if (portman_data_avail(pm, 1)) {
623
624 midivalue = portman_read_midi(pm, 1);
625
626 if (pm->mode[1] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
627 snd_rawmidi_receive(pm->midi_input[1],
628 &midivalue, 1);
629 }
630
631 }
632
633 spin_unlock(&pm->reg_lock);
634 }
635
636 static void snd_portman_attach(struct parport *p)
637 {
638 struct platform_device *device;
639
640 device = platform_device_alloc(PLATFORM_DRIVER, device_count);
641 if (!device)
642 return;
643
644
645 platform_set_drvdata(device, p);
646
647 if (platform_device_add(device) < 0) {
648 platform_device_put(device);
649 return;
650 }
651
652
653
654 if (!platform_get_drvdata(device)) {
655 platform_device_unregister(device);
656 return;
657 }
658
659
660 platform_devices[device_count] = device;
661 device_count++;
662 }
663
664 static void snd_portman_detach(struct parport *p)
665 {
666
667 }
668
669 static int snd_portman_dev_probe(struct pardevice *pardev)
670 {
671 if (strcmp(pardev->name, DRIVER_NAME))
672 return -ENODEV;
673
674 return 0;
675 }
676
677 static struct parport_driver portman_parport_driver = {
678 .name = "portman2x4",
679 .probe = snd_portman_dev_probe,
680 .match_port = snd_portman_attach,
681 .detach = snd_portman_detach,
682 .devmodel = true,
683 };
684
685
686
687
688 static void snd_portman_card_private_free(struct snd_card *card)
689 {
690 struct portman *pm = card->private_data;
691 struct pardevice *pardev = pm->pardev;
692
693 if (pardev) {
694 parport_release(pardev);
695 parport_unregister_device(pardev);
696 }
697
698 portman_free(pm);
699 }
700
701 static int snd_portman_probe(struct platform_device *pdev)
702 {
703 struct pardevice *pardev;
704 struct parport *p;
705 int dev = pdev->id;
706 struct snd_card *card = NULL;
707 struct portman *pm = NULL;
708 int err;
709 struct pardev_cb portman_cb = {
710 .preempt = NULL,
711 .wakeup = NULL,
712 .irq_func = snd_portman_interrupt,
713 .flags = PARPORT_DEV_EXCL,
714 };
715
716 p = platform_get_drvdata(pdev);
717 platform_set_drvdata(pdev, NULL);
718
719 if (dev >= SNDRV_CARDS)
720 return -ENODEV;
721 if (!enable[dev])
722 return -ENOENT;
723
724 err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
725 0, &card);
726 if (err < 0) {
727 snd_printd("Cannot create card\n");
728 return err;
729 }
730 strcpy(card->driver, DRIVER_NAME);
731 strcpy(card->shortname, CARD_NAME);
732 sprintf(card->longname, "%s at 0x%lx, irq %i",
733 card->shortname, p->base, p->irq);
734
735 portman_cb.private = card;
736 pardev = parport_register_dev_model(p,
737 DRIVER_NAME,
738 &portman_cb,
739 pdev->id);
740 if (pardev == NULL) {
741 snd_printd("Cannot register pardevice\n");
742 err = -EIO;
743 goto __err;
744 }
745
746
747 if (parport_claim(pardev)) {
748 snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
749 err = -EIO;
750 goto free_pardev;
751 }
752
753 if ((err = portman_create(card, pardev, &pm)) < 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 if ((err = snd_portman_rawmidi_create(card)) < 0) {
767 snd_printd("Creating Rawmidi component failed\n");
768 goto __err;
769 }
770
771
772 if ((err = portman_device_init(pm)) < 0)
773 goto __err;
774
775 platform_set_drvdata(pdev, card);
776
777
778 if ((err = snd_card_register(card)) < 0) {
779 snd_printd("Cannot register card\n");
780 goto __err;
781 }
782
783 snd_printk(KERN_INFO "Portman 2x4 on 0x%lx\n", p->base);
784 return 0;
785
786 release_pardev:
787 parport_release(pardev);
788 free_pardev:
789 parport_unregister_device(pardev);
790 __err:
791 snd_card_free(card);
792 return err;
793 }
794
795 static int snd_portman_remove(struct platform_device *pdev)
796 {
797 struct snd_card *card = platform_get_drvdata(pdev);
798
799 if (card)
800 snd_card_free(card);
801
802 return 0;
803 }
804
805
806 static struct platform_driver snd_portman_driver = {
807 .probe = snd_portman_probe,
808 .remove = snd_portman_remove,
809 .driver = {
810 .name = PLATFORM_DRIVER,
811 }
812 };
813
814
815
816
817 static void snd_portman_unregister_all(void)
818 {
819 int i;
820
821 for (i = 0; i < SNDRV_CARDS; ++i) {
822 if (platform_devices[i]) {
823 platform_device_unregister(platform_devices[i]);
824 platform_devices[i] = NULL;
825 }
826 }
827 platform_driver_unregister(&snd_portman_driver);
828 parport_unregister_driver(&portman_parport_driver);
829 }
830
831 static int __init snd_portman_module_init(void)
832 {
833 int err;
834
835 if ((err = platform_driver_register(&snd_portman_driver)) < 0)
836 return err;
837
838 if (parport_register_driver(&portman_parport_driver) != 0) {
839 platform_driver_unregister(&snd_portman_driver);
840 return -EIO;
841 }
842
843 if (device_count == 0) {
844 snd_portman_unregister_all();
845 return -ENODEV;
846 }
847
848 return 0;
849 }
850
851 static void __exit snd_portman_module_exit(void)
852 {
853 snd_portman_unregister_all();
854 }
855
856 module_init(snd_portman_module_init);
857 module_exit(snd_portman_module_exit);