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