0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #include <linux/init.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/parport.h>
0030 #include <linux/spinlock.h>
0031 #include <linux/delay.h>
0032 #include <linux/slab.h>
0033 #include <linux/module.h>
0034 #include <sound/core.h>
0035 #include <sound/initval.h>
0036 #include <sound/rawmidi.h>
0037 #include <sound/control.h>
0038
0039 #define CARD_NAME "Portman 2x4"
0040 #define DRIVER_NAME "portman"
0041 #define PLATFORM_DRIVER "snd_portman2x4"
0042
0043 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0044 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0045 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0046
0047 static struct platform_device *platform_devices[SNDRV_CARDS];
0048 static int device_count;
0049
0050 module_param_array(index, int, NULL, 0444);
0051 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
0052 module_param_array(id, charp, NULL, 0444);
0053 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
0054 module_param_array(enable, bool, NULL, 0444);
0055 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
0056
0057 MODULE_AUTHOR("Levent Guendogdu, Tobias Gehrig, Matthias Koenig");
0058 MODULE_DESCRIPTION("Midiman Portman2x4");
0059 MODULE_LICENSE("GPL");
0060
0061
0062
0063
0064 #define PORTMAN_NUM_INPUT_PORTS 2
0065 #define PORTMAN_NUM_OUTPUT_PORTS 4
0066
0067 struct portman {
0068 spinlock_t reg_lock;
0069 struct snd_card *card;
0070 struct snd_rawmidi *rmidi;
0071 struct pardevice *pardev;
0072 int open_count;
0073 int mode[PORTMAN_NUM_INPUT_PORTS];
0074 struct snd_rawmidi_substream *midi_input[PORTMAN_NUM_INPUT_PORTS];
0075 };
0076
0077 static int portman_free(struct portman *pm)
0078 {
0079 kfree(pm);
0080 return 0;
0081 }
0082
0083 static int portman_create(struct snd_card *card,
0084 struct pardevice *pardev,
0085 struct portman **rchip)
0086 {
0087 struct portman *pm;
0088
0089 *rchip = NULL;
0090
0091 pm = kzalloc(sizeof(struct portman), GFP_KERNEL);
0092 if (pm == NULL)
0093 return -ENOMEM;
0094
0095
0096 spin_lock_init(&pm->reg_lock);
0097 pm->card = card;
0098 pm->pardev = pardev;
0099
0100 *rchip = pm;
0101
0102 return 0;
0103 }
0104
0105
0106
0107
0108
0109
0110 #define PP_STAT_BSY 0x80
0111 #define PP_STAT_ACK 0x40
0112 #define PP_STAT_POUT 0x20
0113 #define PP_STAT_SEL 0x10
0114 #define PP_STAT_ERR 0x08
0115
0116
0117 #define PP_CMD_IEN 0x10
0118 #define PP_CMD_SELI 0x08
0119 #define PP_CMD_INIT 0x04
0120 #define PP_CMD_FEED 0x02
0121 #define PP_CMD_STB 0x01
0122
0123
0124 #define INT_EN PP_CMD_IEN
0125 #define STROBE PP_CMD_STB
0126
0127
0128
0129
0130
0131
0132 #define RXDATA0 (0 << 1)
0133 #define RXDATA1 (1 << 1)
0134 #define GEN_CTL (2 << 1)
0135 #define SYNC_CTL (3 << 1)
0136 #define TXDATA0 (4 << 1)
0137 #define TXDATA1 (5 << 1)
0138 #define TXDATA2 (6 << 1)
0139 #define TXDATA3 (7 << 1)
0140
0141
0142 #define ESTB PP_STAT_POUT
0143 #define INT_REQ PP_STAT_ACK
0144 #define BUSY PP_STAT_ERR
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 #define RXAVAIL PP_STAT_SEL
0155
0156 #define SYNC_STAT PP_STAT_SEL
0157
0158 #define TXEMPTY PP_STAT_SEL
0159
0160
0161
0162
0163
0164
0165
0166 #define RXDATA PP_STAT_BSY
0167
0168 #define SYNC_DATA PP_STAT_BSY
0169
0170 #define DATA_ECHO PP_STAT_BSY
0171 #define A0_ECHO PP_STAT_BSY
0172 #define A1_ECHO PP_STAT_BSY
0173 #define A2_ECHO PP_STAT_BSY
0174
0175 #define PORTMAN2X4_MODE_INPUT_TRIGGERED 0x01
0176
0177
0178
0179
0180 static inline void portman_write_command(struct portman *pm, u8 value)
0181 {
0182 parport_write_control(pm->pardev->port, value);
0183 }
0184
0185 static inline u8 portman_read_command(struct portman *pm)
0186 {
0187 return parport_read_control(pm->pardev->port);
0188 }
0189
0190 static inline u8 portman_read_status(struct portman *pm)
0191 {
0192 return parport_read_status(pm->pardev->port);
0193 }
0194
0195 static inline u8 portman_read_data(struct portman *pm)
0196 {
0197 return parport_read_data(pm->pardev->port);
0198 }
0199
0200 static inline void portman_write_data(struct portman *pm, u8 value)
0201 {
0202 parport_write_data(pm->pardev->port, value);
0203 }
0204
0205 static void portman_write_midi(struct portman *pm,
0206 int port, u8 mididata)
0207 {
0208 int command = ((port + 4) << 1);
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219 command |= INT_EN;
0220
0221
0222
0223
0224
0225
0226 do {
0227 portman_write_command(pm, command);
0228
0229
0230
0231
0232
0233 portman_write_data(pm, mididata);
0234
0235
0236
0237
0238 } while ((portman_read_status(pm) & TXEMPTY) != TXEMPTY);
0239
0240
0241
0242
0243
0244
0245 portman_write_command(pm, command | STROBE);
0246
0247
0248
0249
0250
0251
0252 while ((portman_read_status(pm) & ESTB) == 0)
0253 cpu_relax();
0254
0255
0256 portman_write_command(pm, command);
0257
0258 while ((portman_read_status(pm) & ESTB) == ESTB)
0259 cpu_relax();
0260
0261
0262
0263
0264
0265 while ((portman_read_status(pm) & BUSY) == BUSY)
0266 cpu_relax();
0267
0268
0269 }
0270
0271
0272
0273
0274
0275
0276
0277 static int portman_read_midi(struct portman *pm, int port)
0278 {
0279 unsigned char midi_data = 0;
0280 unsigned char cmdout;
0281
0282
0283 portman_write_data(pm, 0);
0284
0285
0286 cmdout = (port << 1) | INT_EN;
0287 portman_write_command(pm, cmdout);
0288
0289 while ((portman_read_status(pm) & ESTB) == ESTB)
0290 cpu_relax();
0291
0292
0293
0294
0295 if ((portman_read_status(pm) & RXAVAIL) == 0)
0296 return -1;
0297
0298
0299 portman_write_command(pm, cmdout | STROBE);
0300
0301 while ((portman_read_status(pm) & ESTB) == 0)
0302 cpu_relax();
0303
0304
0305 midi_data = (portman_read_status(pm) & 128);
0306 portman_write_data(pm, 1);
0307
0308
0309 portman_write_data(pm, 0);
0310 midi_data |= (portman_read_status(pm) >> 1) & 64;
0311 portman_write_data(pm, 1);
0312
0313
0314 portman_write_data(pm, 0);
0315 midi_data |= (portman_read_status(pm) >> 2) & 32;
0316 portman_write_data(pm, 1);
0317
0318
0319 portman_write_data(pm, 0);
0320 midi_data |= (portman_read_status(pm) >> 3) & 16;
0321 portman_write_data(pm, 1);
0322
0323
0324 portman_write_data(pm, 0);
0325 midi_data |= (portman_read_status(pm) >> 4) & 8;
0326 portman_write_data(pm, 1);
0327
0328
0329 portman_write_data(pm, 0);
0330 midi_data |= (portman_read_status(pm) >> 5) & 4;
0331 portman_write_data(pm, 1);
0332
0333
0334 portman_write_data(pm, 0);
0335 midi_data |= (portman_read_status(pm) >> 6) & 2;
0336 portman_write_data(pm, 1);
0337
0338
0339 portman_write_data(pm, 0);
0340 midi_data |= (portman_read_status(pm) >> 7) & 1;
0341 portman_write_data(pm, 1);
0342 portman_write_data(pm, 0);
0343
0344
0345
0346 portman_write_command(pm, cmdout);
0347
0348
0349 while ((portman_read_status(pm) & ESTB) == ESTB)
0350 cpu_relax();
0351
0352 return (midi_data & 255);
0353 }
0354
0355
0356
0357
0358
0359 static int portman_data_avail(struct portman *pm, int channel)
0360 {
0361 int command = INT_EN;
0362 switch (channel) {
0363 case 0:
0364 command |= RXDATA0;
0365 break;
0366 case 1:
0367 command |= RXDATA1;
0368 break;
0369 }
0370
0371 portman_write_command(pm, command);
0372
0373 if ((portman_read_status(pm) & RXAVAIL) == RXAVAIL)
0374 return 1;
0375
0376
0377 return 0;
0378 }
0379
0380
0381
0382
0383
0384 static void portman_flush_input(struct portman *pm, unsigned char port)
0385 {
0386
0387 unsigned int i = 0;
0388 unsigned char command = 0;
0389
0390 switch (port) {
0391 case 0:
0392 command = RXDATA0;
0393 break;
0394 case 1:
0395 command = RXDATA1;
0396 break;
0397 default:
0398 snd_printk(KERN_WARNING
0399 "portman_flush_input() Won't flush port %i\n",
0400 port);
0401 return;
0402 }
0403
0404
0405 portman_write_command(pm, command);
0406
0407
0408 portman_write_command(pm, command | STROBE);
0409
0410
0411 while ((portman_read_status(pm) & ESTB) == 0)
0412 cpu_relax();
0413
0414
0415 portman_write_data(pm, 0);
0416
0417
0418 for (i = 0; i < 250; i++) {
0419 portman_write_data(pm, 1);
0420 portman_write_data(pm, 0);
0421 }
0422
0423
0424 portman_write_command(pm, command | INT_EN);
0425
0426
0427 while ((portman_read_status(pm) & ESTB) == ESTB)
0428 cpu_relax();
0429 }
0430
0431 static int portman_probe(struct parport *p)
0432 {
0433
0434
0435
0436
0437 parport_write_data(p, 0);
0438
0439
0440
0441
0442
0443
0444
0445
0446 parport_write_control(p, 0);
0447
0448
0449
0450 parport_write_control(p, RXDATA0);
0451
0452
0453
0454 if ((parport_read_status(p) & ESTB) == ESTB)
0455 return 1;
0456
0457
0458
0459 parport_write_control(p, RXDATA0 | STROBE);
0460
0461
0462 if ((parport_read_status(p) & ESTB) != ESTB)
0463 return 1;
0464
0465
0466 parport_write_control(p, 0);
0467
0468
0469
0470
0471
0472 parport_write_control(p, TXDATA0);
0473
0474
0475
0476
0477
0478 if ((parport_read_status(p) & TXEMPTY) == 0)
0479 return 2;
0480
0481
0482 return 0;
0483 }
0484
0485 static int portman_device_init(struct portman *pm)
0486 {
0487 portman_flush_input(pm, 0);
0488 portman_flush_input(pm, 1);
0489
0490 return 0;
0491 }
0492
0493
0494
0495
0496 static int snd_portman_midi_open(struct snd_rawmidi_substream *substream)
0497 {
0498 return 0;
0499 }
0500
0501 static int snd_portman_midi_close(struct snd_rawmidi_substream *substream)
0502 {
0503 return 0;
0504 }
0505
0506 static void snd_portman_midi_input_trigger(struct snd_rawmidi_substream *substream,
0507 int up)
0508 {
0509 struct portman *pm = substream->rmidi->private_data;
0510 unsigned long flags;
0511
0512 spin_lock_irqsave(&pm->reg_lock, flags);
0513 if (up)
0514 pm->mode[substream->number] |= PORTMAN2X4_MODE_INPUT_TRIGGERED;
0515 else
0516 pm->mode[substream->number] &= ~PORTMAN2X4_MODE_INPUT_TRIGGERED;
0517 spin_unlock_irqrestore(&pm->reg_lock, flags);
0518 }
0519
0520 static void snd_portman_midi_output_trigger(struct snd_rawmidi_substream *substream,
0521 int up)
0522 {
0523 struct portman *pm = substream->rmidi->private_data;
0524 unsigned long flags;
0525 unsigned char byte;
0526
0527 spin_lock_irqsave(&pm->reg_lock, flags);
0528 if (up) {
0529 while ((snd_rawmidi_transmit(substream, &byte, 1) == 1))
0530 portman_write_midi(pm, substream->number, byte);
0531 }
0532 spin_unlock_irqrestore(&pm->reg_lock, flags);
0533 }
0534
0535 static const struct snd_rawmidi_ops snd_portman_midi_output = {
0536 .open = snd_portman_midi_open,
0537 .close = snd_portman_midi_close,
0538 .trigger = snd_portman_midi_output_trigger,
0539 };
0540
0541 static const struct snd_rawmidi_ops snd_portman_midi_input = {
0542 .open = snd_portman_midi_open,
0543 .close = snd_portman_midi_close,
0544 .trigger = snd_portman_midi_input_trigger,
0545 };
0546
0547
0548 static int snd_portman_rawmidi_create(struct snd_card *card)
0549 {
0550 struct portman *pm = card->private_data;
0551 struct snd_rawmidi *rmidi;
0552 struct snd_rawmidi_substream *substream;
0553 int err;
0554
0555 err = snd_rawmidi_new(card, CARD_NAME, 0,
0556 PORTMAN_NUM_OUTPUT_PORTS,
0557 PORTMAN_NUM_INPUT_PORTS,
0558 &rmidi);
0559 if (err < 0)
0560 return err;
0561
0562 rmidi->private_data = pm;
0563 strcpy(rmidi->name, CARD_NAME);
0564 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
0565 SNDRV_RAWMIDI_INFO_INPUT |
0566 SNDRV_RAWMIDI_INFO_DUPLEX;
0567
0568 pm->rmidi = rmidi;
0569
0570
0571 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
0572 &snd_portman_midi_output);
0573 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
0574 &snd_portman_midi_input);
0575
0576
0577
0578 list_for_each_entry(substream,
0579 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
0580 list) {
0581 sprintf(substream->name,
0582 "Portman2x4 %d", substream->number+1);
0583 }
0584
0585 list_for_each_entry(substream,
0586 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
0587 list) {
0588 pm->midi_input[substream->number] = substream;
0589 sprintf(substream->name,
0590 "Portman2x4 %d", substream->number+1);
0591 }
0592
0593 return err;
0594 }
0595
0596
0597
0598
0599 static void snd_portman_interrupt(void *userdata)
0600 {
0601 unsigned char midivalue = 0;
0602 struct portman *pm = ((struct snd_card*)userdata)->private_data;
0603
0604 spin_lock(&pm->reg_lock);
0605
0606
0607 while ((portman_read_status(pm) & INT_REQ) == INT_REQ) {
0608
0609
0610 if (portman_data_avail(pm, 0)) {
0611
0612 midivalue = portman_read_midi(pm, 0);
0613
0614 if (pm->mode[0] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
0615 snd_rawmidi_receive(pm->midi_input[0],
0616 &midivalue, 1);
0617
0618 }
0619
0620
0621 if (portman_data_avail(pm, 1)) {
0622
0623 midivalue = portman_read_midi(pm, 1);
0624
0625 if (pm->mode[1] & PORTMAN2X4_MODE_INPUT_TRIGGERED)
0626 snd_rawmidi_receive(pm->midi_input[1],
0627 &midivalue, 1);
0628 }
0629
0630 }
0631
0632 spin_unlock(&pm->reg_lock);
0633 }
0634
0635 static void snd_portman_attach(struct parport *p)
0636 {
0637 struct platform_device *device;
0638
0639 device = platform_device_alloc(PLATFORM_DRIVER, device_count);
0640 if (!device)
0641 return;
0642
0643
0644 platform_set_drvdata(device, p);
0645
0646 if (platform_device_add(device) < 0) {
0647 platform_device_put(device);
0648 return;
0649 }
0650
0651
0652
0653 if (!platform_get_drvdata(device)) {
0654 platform_device_unregister(device);
0655 return;
0656 }
0657
0658
0659 platform_devices[device_count] = device;
0660 device_count++;
0661 }
0662
0663 static void snd_portman_detach(struct parport *p)
0664 {
0665
0666 }
0667
0668 static int snd_portman_dev_probe(struct pardevice *pardev)
0669 {
0670 if (strcmp(pardev->name, DRIVER_NAME))
0671 return -ENODEV;
0672
0673 return 0;
0674 }
0675
0676 static struct parport_driver portman_parport_driver = {
0677 .name = "portman2x4",
0678 .probe = snd_portman_dev_probe,
0679 .match_port = snd_portman_attach,
0680 .detach = snd_portman_detach,
0681 .devmodel = true,
0682 };
0683
0684
0685
0686
0687 static void snd_portman_card_private_free(struct snd_card *card)
0688 {
0689 struct portman *pm = card->private_data;
0690 struct pardevice *pardev = pm->pardev;
0691
0692 if (pardev) {
0693 parport_release(pardev);
0694 parport_unregister_device(pardev);
0695 }
0696
0697 portman_free(pm);
0698 }
0699
0700 static int snd_portman_probe(struct platform_device *pdev)
0701 {
0702 struct pardevice *pardev;
0703 struct parport *p;
0704 int dev = pdev->id;
0705 struct snd_card *card = NULL;
0706 struct portman *pm = NULL;
0707 int err;
0708 struct pardev_cb portman_cb = {
0709 .preempt = NULL,
0710 .wakeup = NULL,
0711 .irq_func = snd_portman_interrupt,
0712 .flags = PARPORT_DEV_EXCL,
0713 };
0714
0715 p = platform_get_drvdata(pdev);
0716 platform_set_drvdata(pdev, NULL);
0717
0718 if (dev >= SNDRV_CARDS)
0719 return -ENODEV;
0720 if (!enable[dev])
0721 return -ENOENT;
0722
0723 err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
0724 0, &card);
0725 if (err < 0) {
0726 snd_printd("Cannot create card\n");
0727 return err;
0728 }
0729 strcpy(card->driver, DRIVER_NAME);
0730 strcpy(card->shortname, CARD_NAME);
0731 sprintf(card->longname, "%s at 0x%lx, irq %i",
0732 card->shortname, p->base, p->irq);
0733
0734 portman_cb.private = card;
0735 pardev = parport_register_dev_model(p,
0736 DRIVER_NAME,
0737 &portman_cb,
0738 pdev->id);
0739 if (pardev == NULL) {
0740 snd_printd("Cannot register pardevice\n");
0741 err = -EIO;
0742 goto __err;
0743 }
0744
0745
0746 if (parport_claim(pardev)) {
0747 snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
0748 err = -EIO;
0749 goto free_pardev;
0750 }
0751
0752 err = portman_create(card, pardev, &pm);
0753 if (err < 0) {
0754 snd_printd("Cannot create main component\n");
0755 goto release_pardev;
0756 }
0757 card->private_data = pm;
0758 card->private_free = snd_portman_card_private_free;
0759
0760 err = portman_probe(p);
0761 if (err) {
0762 err = -EIO;
0763 goto __err;
0764 }
0765
0766 err = snd_portman_rawmidi_create(card);
0767 if (err < 0) {
0768 snd_printd("Creating Rawmidi component failed\n");
0769 goto __err;
0770 }
0771
0772
0773 err = portman_device_init(pm);
0774 if (err < 0)
0775 goto __err;
0776
0777 platform_set_drvdata(pdev, card);
0778
0779
0780 err = snd_card_register(card);
0781 if (err < 0) {
0782 snd_printd("Cannot register card\n");
0783 goto __err;
0784 }
0785
0786 snd_printk(KERN_INFO "Portman 2x4 on 0x%lx\n", p->base);
0787 return 0;
0788
0789 release_pardev:
0790 parport_release(pardev);
0791 free_pardev:
0792 parport_unregister_device(pardev);
0793 __err:
0794 snd_card_free(card);
0795 return err;
0796 }
0797
0798 static int snd_portman_remove(struct platform_device *pdev)
0799 {
0800 struct snd_card *card = platform_get_drvdata(pdev);
0801
0802 if (card)
0803 snd_card_free(card);
0804
0805 return 0;
0806 }
0807
0808
0809 static struct platform_driver snd_portman_driver = {
0810 .probe = snd_portman_probe,
0811 .remove = snd_portman_remove,
0812 .driver = {
0813 .name = PLATFORM_DRIVER,
0814 }
0815 };
0816
0817
0818
0819
0820 static void snd_portman_unregister_all(void)
0821 {
0822 int i;
0823
0824 for (i = 0; i < SNDRV_CARDS; ++i) {
0825 if (platform_devices[i]) {
0826 platform_device_unregister(platform_devices[i]);
0827 platform_devices[i] = NULL;
0828 }
0829 }
0830 platform_driver_unregister(&snd_portman_driver);
0831 parport_unregister_driver(&portman_parport_driver);
0832 }
0833
0834 static int __init snd_portman_module_init(void)
0835 {
0836 int err;
0837
0838 err = platform_driver_register(&snd_portman_driver);
0839 if (err < 0)
0840 return err;
0841
0842 if (parport_register_driver(&portman_parport_driver) != 0) {
0843 platform_driver_unregister(&snd_portman_driver);
0844 return -EIO;
0845 }
0846
0847 if (device_count == 0) {
0848 snd_portman_unregister_all();
0849 return -ENODEV;
0850 }
0851
0852 return 0;
0853 }
0854
0855 static void __exit snd_portman_module_exit(void)
0856 {
0857 snd_portman_unregister_all();
0858 }
0859
0860 module_init(snd_portman_module_init);
0861 module_exit(snd_portman_module_exit);