0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/io.h>
0017 #include <linux/delay.h>
0018 #include <linux/init.h>
0019 #include <linux/slab.h>
0020 #include <linux/ioport.h>
0021 #include <linux/module.h>
0022 #include <linux/interrupt.h>
0023 #include <linux/errno.h>
0024 #include <sound/core.h>
0025 #include <sound/mpu401.h>
0026
0027 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0028 MODULE_DESCRIPTION("Routines for control of MPU-401 in UART mode");
0029 MODULE_LICENSE("GPL");
0030
0031 static void snd_mpu401_uart_input_read(struct snd_mpu401 * mpu);
0032 static void snd_mpu401_uart_output_write(struct snd_mpu401 * mpu);
0033
0034
0035
0036
0037
0038 #define snd_mpu401_input_avail(mpu) \
0039 (!(mpu->read(mpu, MPU401C(mpu)) & MPU401_RX_EMPTY))
0040 #define snd_mpu401_output_ready(mpu) \
0041 (!(mpu->read(mpu, MPU401C(mpu)) & MPU401_TX_FULL))
0042
0043
0044 static void mpu401_write_port(struct snd_mpu401 *mpu, unsigned char data,
0045 unsigned long addr)
0046 {
0047 outb(data, addr);
0048 }
0049
0050 static unsigned char mpu401_read_port(struct snd_mpu401 *mpu,
0051 unsigned long addr)
0052 {
0053 return inb(addr);
0054 }
0055
0056 static void mpu401_write_mmio(struct snd_mpu401 *mpu, unsigned char data,
0057 unsigned long addr)
0058 {
0059 writeb(data, (void __iomem *)addr);
0060 }
0061
0062 static unsigned char mpu401_read_mmio(struct snd_mpu401 *mpu,
0063 unsigned long addr)
0064 {
0065 return readb((void __iomem *)addr);
0066 }
0067
0068
0069 static void snd_mpu401_uart_clear_rx(struct snd_mpu401 *mpu)
0070 {
0071 int timeout = 100000;
0072 for (; timeout > 0 && snd_mpu401_input_avail(mpu); timeout--)
0073 mpu->read(mpu, MPU401D(mpu));
0074 #ifdef CONFIG_SND_DEBUG
0075 if (timeout <= 0)
0076 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n",
0077 mpu->read(mpu, MPU401C(mpu)));
0078 #endif
0079 }
0080
0081 static void uart_interrupt_tx(struct snd_mpu401 *mpu)
0082 {
0083 unsigned long flags;
0084
0085 if (test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode) &&
0086 test_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode)) {
0087 spin_lock_irqsave(&mpu->output_lock, flags);
0088 snd_mpu401_uart_output_write(mpu);
0089 spin_unlock_irqrestore(&mpu->output_lock, flags);
0090 }
0091 }
0092
0093 static void _snd_mpu401_uart_interrupt(struct snd_mpu401 *mpu)
0094 {
0095 unsigned long flags;
0096
0097 if (mpu->info_flags & MPU401_INFO_INPUT) {
0098 spin_lock_irqsave(&mpu->input_lock, flags);
0099 if (test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode))
0100 snd_mpu401_uart_input_read(mpu);
0101 else
0102 snd_mpu401_uart_clear_rx(mpu);
0103 spin_unlock_irqrestore(&mpu->input_lock, flags);
0104 }
0105 if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
0106
0107
0108 uart_interrupt_tx(mpu);
0109 }
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 irqreturn_t snd_mpu401_uart_interrupt(int irq, void *dev_id)
0121 {
0122 struct snd_mpu401 *mpu = dev_id;
0123
0124 if (!mpu)
0125 return IRQ_NONE;
0126 _snd_mpu401_uart_interrupt(mpu);
0127 return IRQ_HANDLED;
0128 }
0129
0130 EXPORT_SYMBOL(snd_mpu401_uart_interrupt);
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 irqreturn_t snd_mpu401_uart_interrupt_tx(int irq, void *dev_id)
0142 {
0143 struct snd_mpu401 *mpu = dev_id;
0144
0145 if (!mpu)
0146 return IRQ_NONE;
0147 uart_interrupt_tx(mpu);
0148 return IRQ_HANDLED;
0149 }
0150
0151 EXPORT_SYMBOL(snd_mpu401_uart_interrupt_tx);
0152
0153
0154
0155
0156
0157 static void snd_mpu401_uart_timer(struct timer_list *t)
0158 {
0159 struct snd_mpu401 *mpu = from_timer(mpu, t, timer);
0160 unsigned long flags;
0161
0162 spin_lock_irqsave(&mpu->timer_lock, flags);
0163
0164 mod_timer(&mpu->timer, 1 + jiffies);
0165 spin_unlock_irqrestore(&mpu->timer_lock, flags);
0166 if (mpu->rmidi)
0167 _snd_mpu401_uart_interrupt(mpu);
0168 }
0169
0170
0171
0172
0173 static void snd_mpu401_uart_add_timer (struct snd_mpu401 *mpu, int input)
0174 {
0175 unsigned long flags;
0176
0177 spin_lock_irqsave (&mpu->timer_lock, flags);
0178 if (mpu->timer_invoked == 0) {
0179 timer_setup(&mpu->timer, snd_mpu401_uart_timer, 0);
0180 mod_timer(&mpu->timer, 1 + jiffies);
0181 }
0182 mpu->timer_invoked |= input ? MPU401_MODE_INPUT_TIMER :
0183 MPU401_MODE_OUTPUT_TIMER;
0184 spin_unlock_irqrestore (&mpu->timer_lock, flags);
0185 }
0186
0187
0188
0189
0190 static void snd_mpu401_uart_remove_timer (struct snd_mpu401 *mpu, int input)
0191 {
0192 unsigned long flags;
0193
0194 spin_lock_irqsave (&mpu->timer_lock, flags);
0195 if (mpu->timer_invoked) {
0196 mpu->timer_invoked &= input ? ~MPU401_MODE_INPUT_TIMER :
0197 ~MPU401_MODE_OUTPUT_TIMER;
0198 if (! mpu->timer_invoked)
0199 del_timer(&mpu->timer);
0200 }
0201 spin_unlock_irqrestore (&mpu->timer_lock, flags);
0202 }
0203
0204
0205
0206
0207
0208
0209 static int snd_mpu401_uart_cmd(struct snd_mpu401 * mpu, unsigned char cmd,
0210 int ack)
0211 {
0212 unsigned long flags;
0213 int timeout, ok;
0214
0215 spin_lock_irqsave(&mpu->input_lock, flags);
0216 if (mpu->hardware != MPU401_HW_TRID4DWAVE) {
0217 mpu->write(mpu, 0x00, MPU401D(mpu));
0218
0219 }
0220
0221 if (mpu->hardware != MPU401_HW_SB) {
0222 for (timeout = 1000; timeout > 0 &&
0223 !snd_mpu401_output_ready(mpu); timeout--)
0224 udelay(10);
0225 #ifdef CONFIG_SND_DEBUG
0226 if (!timeout)
0227 snd_printk(KERN_ERR "cmd: tx timeout (status = 0x%x)\n",
0228 mpu->read(mpu, MPU401C(mpu)));
0229 #endif
0230 }
0231 mpu->write(mpu, cmd, MPU401C(mpu));
0232 if (ack && !(mpu->info_flags & MPU401_INFO_NO_ACK)) {
0233 ok = 0;
0234 timeout = 10000;
0235 while (!ok && timeout-- > 0) {
0236 if (snd_mpu401_input_avail(mpu)) {
0237 if (mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
0238 ok = 1;
0239 }
0240 }
0241 if (!ok && mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
0242 ok = 1;
0243 } else
0244 ok = 1;
0245 spin_unlock_irqrestore(&mpu->input_lock, flags);
0246 if (!ok) {
0247 snd_printk(KERN_ERR "cmd: 0x%x failed at 0x%lx "
0248 "(status = 0x%x, data = 0x%x)\n", cmd, mpu->port,
0249 mpu->read(mpu, MPU401C(mpu)),
0250 mpu->read(mpu, MPU401D(mpu)));
0251 return 1;
0252 }
0253 return 0;
0254 }
0255
0256 static int snd_mpu401_do_reset(struct snd_mpu401 *mpu)
0257 {
0258 if (snd_mpu401_uart_cmd(mpu, MPU401_RESET, 1))
0259 return -EIO;
0260 if (snd_mpu401_uart_cmd(mpu, MPU401_ENTER_UART, 0))
0261 return -EIO;
0262 return 0;
0263 }
0264
0265
0266
0267
0268 static int snd_mpu401_uart_input_open(struct snd_rawmidi_substream *substream)
0269 {
0270 struct snd_mpu401 *mpu;
0271 int err;
0272
0273 mpu = substream->rmidi->private_data;
0274 if (mpu->open_input) {
0275 err = mpu->open_input(mpu);
0276 if (err < 0)
0277 return err;
0278 }
0279 if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode)) {
0280 if (snd_mpu401_do_reset(mpu) < 0)
0281 goto error_out;
0282 }
0283 mpu->substream_input = substream;
0284 set_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
0285 return 0;
0286
0287 error_out:
0288 if (mpu->open_input && mpu->close_input)
0289 mpu->close_input(mpu);
0290 return -EIO;
0291 }
0292
0293 static int snd_mpu401_uart_output_open(struct snd_rawmidi_substream *substream)
0294 {
0295 struct snd_mpu401 *mpu;
0296 int err;
0297
0298 mpu = substream->rmidi->private_data;
0299 if (mpu->open_output) {
0300 err = mpu->open_output(mpu);
0301 if (err < 0)
0302 return err;
0303 }
0304 if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode)) {
0305 if (snd_mpu401_do_reset(mpu) < 0)
0306 goto error_out;
0307 }
0308 mpu->substream_output = substream;
0309 set_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
0310 return 0;
0311
0312 error_out:
0313 if (mpu->open_output && mpu->close_output)
0314 mpu->close_output(mpu);
0315 return -EIO;
0316 }
0317
0318 static int snd_mpu401_uart_input_close(struct snd_rawmidi_substream *substream)
0319 {
0320 struct snd_mpu401 *mpu;
0321 int err = 0;
0322
0323 mpu = substream->rmidi->private_data;
0324 clear_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
0325 mpu->substream_input = NULL;
0326 if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode))
0327 err = snd_mpu401_uart_cmd(mpu, MPU401_RESET, 0);
0328 if (mpu->close_input)
0329 mpu->close_input(mpu);
0330 if (err)
0331 return -EIO;
0332 return 0;
0333 }
0334
0335 static int snd_mpu401_uart_output_close(struct snd_rawmidi_substream *substream)
0336 {
0337 struct snd_mpu401 *mpu;
0338 int err = 0;
0339
0340 mpu = substream->rmidi->private_data;
0341 clear_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
0342 mpu->substream_output = NULL;
0343 if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode))
0344 err = snd_mpu401_uart_cmd(mpu, MPU401_RESET, 0);
0345 if (mpu->close_output)
0346 mpu->close_output(mpu);
0347 if (err)
0348 return -EIO;
0349 return 0;
0350 }
0351
0352
0353
0354
0355 static void
0356 snd_mpu401_uart_input_trigger(struct snd_rawmidi_substream *substream, int up)
0357 {
0358 unsigned long flags;
0359 struct snd_mpu401 *mpu;
0360 int max = 64;
0361
0362 mpu = substream->rmidi->private_data;
0363 if (up) {
0364 if (! test_and_set_bit(MPU401_MODE_BIT_INPUT_TRIGGER,
0365 &mpu->mode)) {
0366
0367 while (max-- > 0)
0368 mpu->read(mpu, MPU401D(mpu));
0369 if (mpu->info_flags & MPU401_INFO_USE_TIMER)
0370 snd_mpu401_uart_add_timer(mpu, 1);
0371 }
0372
0373
0374 spin_lock_irqsave(&mpu->input_lock, flags);
0375 snd_mpu401_uart_input_read(mpu);
0376 spin_unlock_irqrestore(&mpu->input_lock, flags);
0377 } else {
0378 if (mpu->info_flags & MPU401_INFO_USE_TIMER)
0379 snd_mpu401_uart_remove_timer(mpu, 1);
0380 clear_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
0381 }
0382
0383 }
0384
0385
0386
0387
0388
0389 static void snd_mpu401_uart_input_read(struct snd_mpu401 * mpu)
0390 {
0391 int max = 128;
0392 unsigned char byte;
0393
0394 while (max-- > 0) {
0395 if (! snd_mpu401_input_avail(mpu))
0396 break;
0397 byte = mpu->read(mpu, MPU401D(mpu));
0398 if (test_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode))
0399 snd_rawmidi_receive(mpu->substream_input, &byte, 1);
0400 }
0401 }
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415 static void snd_mpu401_uart_output_write(struct snd_mpu401 * mpu)
0416 {
0417 unsigned char byte;
0418 int max = 256;
0419
0420 do {
0421 if (snd_rawmidi_transmit_peek(mpu->substream_output,
0422 &byte, 1) == 1) {
0423
0424
0425
0426
0427 if (!snd_mpu401_output_ready(mpu) &&
0428 !snd_mpu401_output_ready(mpu))
0429 break;
0430 mpu->write(mpu, byte, MPU401D(mpu));
0431 snd_rawmidi_transmit_ack(mpu->substream_output, 1);
0432 } else {
0433 snd_mpu401_uart_remove_timer (mpu, 0);
0434 break;
0435 }
0436 } while (--max > 0);
0437 }
0438
0439
0440
0441
0442 static void
0443 snd_mpu401_uart_output_trigger(struct snd_rawmidi_substream *substream, int up)
0444 {
0445 unsigned long flags;
0446 struct snd_mpu401 *mpu;
0447
0448 mpu = substream->rmidi->private_data;
0449 if (up) {
0450 set_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
0451
0452
0453
0454
0455
0456 if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
0457 snd_mpu401_uart_add_timer(mpu, 0);
0458
0459
0460 spin_lock_irqsave(&mpu->output_lock, flags);
0461 snd_mpu401_uart_output_write(mpu);
0462 spin_unlock_irqrestore(&mpu->output_lock, flags);
0463 } else {
0464 if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
0465 snd_mpu401_uart_remove_timer(mpu, 0);
0466 clear_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
0467 }
0468 }
0469
0470
0471
0472
0473
0474 static const struct snd_rawmidi_ops snd_mpu401_uart_output =
0475 {
0476 .open = snd_mpu401_uart_output_open,
0477 .close = snd_mpu401_uart_output_close,
0478 .trigger = snd_mpu401_uart_output_trigger,
0479 };
0480
0481 static const struct snd_rawmidi_ops snd_mpu401_uart_input =
0482 {
0483 .open = snd_mpu401_uart_input_open,
0484 .close = snd_mpu401_uart_input_close,
0485 .trigger = snd_mpu401_uart_input_trigger,
0486 };
0487
0488 static void snd_mpu401_uart_free(struct snd_rawmidi *rmidi)
0489 {
0490 struct snd_mpu401 *mpu = rmidi->private_data;
0491 if (mpu->irq >= 0)
0492 free_irq(mpu->irq, (void *) mpu);
0493 release_and_free_resource(mpu->res);
0494 kfree(mpu);
0495 }
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515 int snd_mpu401_uart_new(struct snd_card *card, int device,
0516 unsigned short hardware,
0517 unsigned long port,
0518 unsigned int info_flags,
0519 int irq,
0520 struct snd_rawmidi ** rrawmidi)
0521 {
0522 struct snd_mpu401 *mpu;
0523 struct snd_rawmidi *rmidi;
0524 int in_enable, out_enable;
0525 int err;
0526
0527 if (rrawmidi)
0528 *rrawmidi = NULL;
0529 if (! (info_flags & (MPU401_INFO_INPUT | MPU401_INFO_OUTPUT)))
0530 info_flags |= MPU401_INFO_INPUT | MPU401_INFO_OUTPUT;
0531 in_enable = (info_flags & MPU401_INFO_INPUT) ? 1 : 0;
0532 out_enable = (info_flags & MPU401_INFO_OUTPUT) ? 1 : 0;
0533 err = snd_rawmidi_new(card, "MPU-401U", device,
0534 out_enable, in_enable, &rmidi);
0535 if (err < 0)
0536 return err;
0537 mpu = kzalloc(sizeof(*mpu), GFP_KERNEL);
0538 if (!mpu) {
0539 err = -ENOMEM;
0540 goto free_device;
0541 }
0542 rmidi->private_data = mpu;
0543 rmidi->private_free = snd_mpu401_uart_free;
0544 spin_lock_init(&mpu->input_lock);
0545 spin_lock_init(&mpu->output_lock);
0546 spin_lock_init(&mpu->timer_lock);
0547 mpu->hardware = hardware;
0548 mpu->irq = -1;
0549 if (! (info_flags & MPU401_INFO_INTEGRATED)) {
0550 int res_size = hardware == MPU401_HW_PC98II ? 4 : 2;
0551 mpu->res = request_region(port, res_size, "MPU401 UART");
0552 if (!mpu->res) {
0553 snd_printk(KERN_ERR "mpu401_uart: "
0554 "unable to grab port 0x%lx size %d\n",
0555 port, res_size);
0556 err = -EBUSY;
0557 goto free_device;
0558 }
0559 }
0560 if (info_flags & MPU401_INFO_MMIO) {
0561 mpu->write = mpu401_write_mmio;
0562 mpu->read = mpu401_read_mmio;
0563 } else {
0564 mpu->write = mpu401_write_port;
0565 mpu->read = mpu401_read_port;
0566 }
0567 mpu->port = port;
0568 if (hardware == MPU401_HW_PC98II)
0569 mpu->cport = port + 2;
0570 else
0571 mpu->cport = port + 1;
0572 if (irq >= 0) {
0573 if (request_irq(irq, snd_mpu401_uart_interrupt, 0,
0574 "MPU401 UART", (void *) mpu)) {
0575 snd_printk(KERN_ERR "mpu401_uart: "
0576 "unable to grab IRQ %d\n", irq);
0577 err = -EBUSY;
0578 goto free_device;
0579 }
0580 }
0581 if (irq < 0 && !(info_flags & MPU401_INFO_IRQ_HOOK))
0582 info_flags |= MPU401_INFO_USE_TIMER;
0583 mpu->info_flags = info_flags;
0584 mpu->irq = irq;
0585 if (card->shortname[0])
0586 snprintf(rmidi->name, sizeof(rmidi->name), "%s MIDI",
0587 card->shortname);
0588 else
0589 sprintf(rmidi->name, "MPU-401 MIDI %d-%d",card->number, device);
0590 if (out_enable) {
0591 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
0592 &snd_mpu401_uart_output);
0593 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
0594 }
0595 if (in_enable) {
0596 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
0597 &snd_mpu401_uart_input);
0598 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
0599 if (out_enable)
0600 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
0601 }
0602 mpu->rmidi = rmidi;
0603 if (rrawmidi)
0604 *rrawmidi = rmidi;
0605 return 0;
0606 free_device:
0607 snd_device_free(card, rmidi);
0608 return err;
0609 }
0610
0611 EXPORT_SYMBOL(snd_mpu401_uart_new);