blob: f1c1dc3ada25d1ef61731e8f9a2145ff7c3e04ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * sound/oss/mpu401.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * The low level driver for Roland MPU-401 compatible Midi cards.
5 */
6/*
7 * Copyright (C) by Hannu Savolainen 1993-1997
8 *
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
12 *
13 *
14 * Thomas Sailer ioctl code reworked (vmalloc/vfree removed)
15 * Alan Cox modularisation, use normal request_irq, use dev_id
16 * Bartlomiej Zolnierkiewicz removed some __init to allow using many drivers
17 * Chris Rankin Update the module-usage counter for the coprocessor
18 * Zwane Mwaikambo Changed attach/unload resource freeing
19 */
20
21#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
26#define USE_SEQ_MACROS
27#define USE_SIMPLE_MACROS
28
29#include "sound_config.h"
30
31#include "coproc.h"
32#include "mpu401.h"
33
34static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
35
36struct mpu_config
37{
38 int base; /*
39 * I/O base
40 */
41 int irq;
42 int opened; /*
43 * Open mode
44 */
45 int devno;
46 int synthno;
47 int uart_mode;
48 int initialized;
49 int mode;
50#define MODE_MIDI 1
51#define MODE_SYNTH 2
52 unsigned char version, revision;
53 unsigned int capabilities;
54#define MPU_CAP_INTLG 0x10000000
55#define MPU_CAP_SYNC 0x00000010
56#define MPU_CAP_FSK 0x00000020
57#define MPU_CAP_CLS 0x00000040
58#define MPU_CAP_SMPTE 0x00000080
59#define MPU_CAP_2PORT 0x00000001
60 int timer_flag;
61
62#define MBUF_MAX 10
63#define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
64 {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
65 int m_busy;
66 unsigned char m_buf[MBUF_MAX];
67 int m_ptr;
68 int m_state;
69 int m_left;
70 unsigned char last_status;
71 void (*inputintr) (int dev, unsigned char data);
72 int shared_irq;
73 int *osp;
74 spinlock_t lock;
75 };
76
77#define DATAPORT(base) (base)
78#define COMDPORT(base) (base+1)
79#define STATPORT(base) (base+1)
80
81
82static void mpu401_close(int dev);
83
84static inline int mpu401_status(struct mpu_config *devc)
85{
86 return inb(STATPORT(devc->base));
87}
88
89#define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
90#define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
91
92static inline void write_command(struct mpu_config *devc, unsigned char cmd)
93{
94 outb(cmd, COMDPORT(devc->base));
95}
96
97static inline int read_data(struct mpu_config *devc)
98{
99 return inb(DATAPORT(devc->base));
100}
101
102static inline void write_data(struct mpu_config *devc, unsigned char byte)
103{
104 outb(byte, DATAPORT(devc->base));
105}
106
107#define OUTPUT_READY 0x40
108#define INPUT_AVAIL 0x80
109#define MPU_ACK 0xFE
110#define MPU_RESET 0xFF
111#define UART_MODE_ON 0x3F
112
113static struct mpu_config dev_conf[MAX_MIDI_DEV];
114
115static int n_mpu_devs;
116
117static int reset_mpu401(struct mpu_config *devc);
118static void set_uart_mode(int dev, struct mpu_config *devc, int arg);
119
120static int mpu_timer_init(int midi_dev);
121static void mpu_timer_interrupt(void);
122static void timer_ext_event(struct mpu_config *devc, int event, int parm);
123
124static struct synth_info mpu_synth_info_proto = {
125 "MPU-401 MIDI interface",
126 0,
127 SYNTH_TYPE_MIDI,
128 MIDI_TYPE_MPU401,
129 0, 128,
130 0, 128,
131 SYNTH_CAP_INPUT
132};
133
134static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
135
136/*
137 * States for the input scanner
138 */
139
140#define ST_INIT 0 /* Ready for timing byte or msg */
141#define ST_TIMED 1 /* Leading timing byte rcvd */
142#define ST_DATABYTE 2 /* Waiting for (nr_left) data bytes */
143
144#define ST_SYSMSG 100 /* System message (sysx etc). */
145#define ST_SYSEX 101 /* System exclusive msg */
146#define ST_MTC 102 /* Midi Time Code (MTC) qframe msg */
147#define ST_SONGSEL 103 /* Song select */
148#define ST_SONGPOS 104 /* Song position pointer */
149
150static unsigned char len_tab[] = /* # of data bytes following a status
151 */
152{
153 2, /* 8x */
154 2, /* 9x */
155 2, /* Ax */
156 2, /* Bx */
157 1, /* Cx */
158 1, /* Dx */
159 2, /* Ex */
160 0 /* Fx */
161};
162
163#define STORE(cmd) \
164{ \
165 int len; \
166 unsigned char obuf[8]; \
167 cmd; \
168 seq_input_event(obuf, len); \
169}
170
171#define _seqbuf obuf
172#define _seqbufptr 0
173#define _SEQ_ADVBUF(x) len=x
174
175static int mpu_input_scanner(struct mpu_config *devc, unsigned char midic)
176{
177
178 switch (devc->m_state)
179 {
180 case ST_INIT:
181 switch (midic)
182 {
183 case 0xf8:
184 /* Timer overflow */
185 break;
186
187 case 0xfc:
188 printk("<all end>");
189 break;
190
191 case 0xfd:
192 if (devc->timer_flag)
193 mpu_timer_interrupt();
194 break;
195
196 case 0xfe:
197 return MPU_ACK;
198
199 case 0xf0:
200 case 0xf1:
201 case 0xf2:
202 case 0xf3:
203 case 0xf4:
204 case 0xf5:
205 case 0xf6:
206 case 0xf7:
207 printk("<Trk data rq #%d>", midic & 0x0f);
208 break;
209
210 case 0xf9:
211 printk("<conductor rq>");
212 break;
213
214 case 0xff:
215 devc->m_state = ST_SYSMSG;
216 break;
217
218 default:
219 if (midic <= 0xef)
220 {
221 /* printk( "mpu time: %d ", midic); */
222 devc->m_state = ST_TIMED;
223 }
224 else
225 printk("<MPU: Unknown event %02x> ", midic);
226 }
227 break;
228
229 case ST_TIMED:
230 {
231 int msg = ((int) (midic & 0xf0) >> 4);
232
233 devc->m_state = ST_DATABYTE;
234
235 if (msg < 8) /* Data byte */
236 {
237 /* printk( "midi msg (running status) "); */
238 msg = ((int) (devc->last_status & 0xf0) >> 4);
239 msg -= 8;
240 devc->m_left = len_tab[msg] - 1;
241
242 devc->m_ptr = 2;
243 devc->m_buf[0] = devc->last_status;
244 devc->m_buf[1] = midic;
245
246 if (devc->m_left <= 0)
247 {
248 devc->m_state = ST_INIT;
249 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
250 devc->m_ptr = 0;
251 }
252 }
253 else if (msg == 0xf) /* MPU MARK */
254 {
255 devc->m_state = ST_INIT;
256
257 switch (midic)
258 {
259 case 0xf8:
260 /* printk( "NOP "); */
261 break;
262
263 case 0xf9:
264 /* printk( "meas end "); */
265 break;
266
267 case 0xfc:
268 /* printk( "data end "); */
269 break;
270
271 default:
272 printk("Unknown MPU mark %02x\n", midic);
273 }
274 }
275 else
276 {
277 devc->last_status = midic;
278 /* printk( "midi msg "); */
279 msg -= 8;
280 devc->m_left = len_tab[msg];
281
282 devc->m_ptr = 1;
283 devc->m_buf[0] = midic;
284
285 if (devc->m_left <= 0)
286 {
287 devc->m_state = ST_INIT;
288 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
289 devc->m_ptr = 0;
290 }
291 }
292 }
293 break;
294
295 case ST_SYSMSG:
296 switch (midic)
297 {
298 case 0xf0:
299 printk("<SYX>");
300 devc->m_state = ST_SYSEX;
301 break;
302
303 case 0xf1:
304 devc->m_state = ST_MTC;
305 break;
306
307 case 0xf2:
308 devc->m_state = ST_SONGPOS;
309 devc->m_ptr = 0;
310 break;
311
312 case 0xf3:
313 devc->m_state = ST_SONGSEL;
314 break;
315
316 case 0xf6:
317 /* printk( "tune_request\n"); */
318 devc->m_state = ST_INIT;
Rickard Strandqvist21d72162014-06-24 12:49:35 +0200319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /*
322 * Real time messages
323 */
324 case 0xf8:
325 /* midi clock */
326 devc->m_state = ST_INIT;
327 timer_ext_event(devc, TMR_CLOCK, 0);
328 break;
329
330 case 0xfA:
331 devc->m_state = ST_INIT;
332 timer_ext_event(devc, TMR_START, 0);
333 break;
334
335 case 0xFB:
336 devc->m_state = ST_INIT;
337 timer_ext_event(devc, TMR_CONTINUE, 0);
338 break;
339
340 case 0xFC:
341 devc->m_state = ST_INIT;
342 timer_ext_event(devc, TMR_STOP, 0);
343 break;
344
345 case 0xFE:
346 /* active sensing */
347 devc->m_state = ST_INIT;
348 break;
349
350 case 0xff:
351 /* printk( "midi hard reset"); */
352 devc->m_state = ST_INIT;
353 break;
354
355 default:
356 printk("unknown MIDI sysmsg %0x\n", midic);
357 devc->m_state = ST_INIT;
358 }
359 break;
360
361 case ST_MTC:
362 devc->m_state = ST_INIT;
363 printk("MTC frame %x02\n", midic);
364 break;
365
366 case ST_SYSEX:
367 if (midic == 0xf7)
368 {
369 printk("<EOX>");
370 devc->m_state = ST_INIT;
371 }
372 else
373 printk("%02x ", midic);
374 break;
375
376 case ST_SONGPOS:
377 BUFTEST(devc);
378 devc->m_buf[devc->m_ptr++] = midic;
379 if (devc->m_ptr == 2)
380 {
381 devc->m_state = ST_INIT;
382 devc->m_ptr = 0;
383 timer_ext_event(devc, TMR_SPP,
384 ((devc->m_buf[1] & 0x7f) << 7) |
385 (devc->m_buf[0] & 0x7f));
386 }
387 break;
388
389 case ST_DATABYTE:
390 BUFTEST(devc);
391 devc->m_buf[devc->m_ptr++] = midic;
392 if ((--devc->m_left) <= 0)
393 {
394 devc->m_state = ST_INIT;
395 do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
396 devc->m_ptr = 0;
397 }
398 break;
399
400 default:
401 printk("Bad state %d ", devc->m_state);
402 devc->m_state = ST_INIT;
403 }
404 return 1;
405}
406
407static void mpu401_input_loop(struct mpu_config *devc)
408{
409 unsigned long flags;
410 int busy;
411 int n;
412
413 spin_lock_irqsave(&devc->lock,flags);
414 busy = devc->m_busy;
415 devc->m_busy = 1;
416 spin_unlock_irqrestore(&devc->lock,flags);
417
418 if (busy) /* Already inside the scanner */
419 return;
420
421 n = 50;
422
423 while (input_avail(devc) && n-- > 0)
424 {
425 unsigned char c = read_data(devc);
426
427 if (devc->mode == MODE_SYNTH)
428 {
429 mpu_input_scanner(devc, c);
430 }
431 else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
432 devc->inputintr(devc->devno, c);
433 }
434 devc->m_busy = 0;
435}
436
David Howells7d12e782006-10-05 14:55:46 +0100437static irqreturn_t mpuintr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct mpu_config *devc;
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400440 int dev = (int)(unsigned long) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int handled = 0;
442
443 devc = &dev_conf[dev];
444
445 if (input_avail(devc))
446 {
447 handled = 1;
448 if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
449 mpu401_input_loop(devc);
450 else
451 {
452 /* Dummy read (just to acknowledge the interrupt) */
453 read_data(devc);
454 }
455 }
456 return IRQ_RETVAL(handled);
457}
458
459static int mpu401_open(int dev, int mode,
460 void (*input) (int dev, unsigned char data),
461 void (*output) (int dev)
462)
463{
464 int err;
465 struct mpu_config *devc;
466 struct coproc_operations *coprocessor;
467
468 if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
469 return -ENXIO;
470
471 devc = &dev_conf[dev];
472
473 if (devc->opened)
474 return -EBUSY;
475 /*
476 * Verify that the device is really running.
477 * Some devices (such as Ensoniq SoundScape don't
478 * work before the on board processor (OBP) is initialized
479 * by downloading its microcode.
480 */
481
482 if (!devc->initialized)
483 {
484 if (mpu401_status(devc) == 0xff) /* Bus float */
485 {
486 printk(KERN_ERR "mpu401: Device not initialized properly\n");
487 return -EIO;
488 }
489 reset_mpu401(devc);
490 }
491
492 if ( (coprocessor = midi_devs[dev]->coproc) != NULL )
493 {
494 if (!try_module_get(coprocessor->owner)) {
495 mpu401_close(dev);
496 return -ENODEV;
497 }
498
499 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
500 {
501 printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
502 mpu401_close(dev);
503 return err;
504 }
505 }
506
507 set_uart_mode(dev, devc, 1);
508 devc->mode = MODE_MIDI;
509 devc->synthno = 0;
510
511 mpu401_input_loop(devc);
512
513 devc->inputintr = input;
514 devc->opened = mode;
515
516 return 0;
517}
518
519static void mpu401_close(int dev)
520{
521 struct mpu_config *devc;
522 struct coproc_operations *coprocessor;
523
524 devc = &dev_conf[dev];
525 if (devc->uart_mode)
526 reset_mpu401(devc); /*
527 * This disables the UART mode
528 */
529 devc->mode = 0;
530 devc->inputintr = NULL;
531
532 coprocessor = midi_devs[dev]->coproc;
533 if (coprocessor) {
534 coprocessor->close(coprocessor->devc, COPR_MIDI);
535 module_put(coprocessor->owner);
536 }
537 devc->opened = 0;
538}
539
540static int mpu401_out(int dev, unsigned char midi_byte)
541{
542 int timeout;
543 unsigned long flags;
544
545 struct mpu_config *devc;
546
547 devc = &dev_conf[dev];
548
549 /*
550 * Sometimes it takes about 30000 loops before the output becomes ready
551 * (After reset). Normally it takes just about 10 loops.
552 */
553
554 for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
555
556 spin_lock_irqsave(&devc->lock,flags);
557 if (!output_ready(devc))
558 {
559 printk(KERN_WARNING "mpu401: Send data timeout\n");
560 spin_unlock_irqrestore(&devc->lock,flags);
561 return 0;
562 }
563 write_data(devc, midi_byte);
564 spin_unlock_irqrestore(&devc->lock,flags);
565 return 1;
566}
567
568static int mpu401_command(int dev, mpu_command_rec * cmd)
569{
570 int i, timeout, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned long flags;
572 struct mpu_config *devc;
573
574 devc = &dev_conf[dev];
575
576 if (devc->uart_mode) /*
577 * Not possible in UART mode
578 */
579 {
580 printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
581 return -EINVAL;
582 }
583 /*
584 * Test for input since pending input seems to block the output.
585 */
586 if (input_avail(devc))
587 mpu401_input_loop(devc);
588
589 /*
590 * Sometimes it takes about 50000 loops before the output becomes ready
591 * (After reset). Normally it takes just about 10 loops.
592 */
593
594 timeout = 50000;
595retry:
596 if (timeout-- <= 0)
597 {
598 printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
599 return -EIO;
600 }
601 spin_lock_irqsave(&devc->lock,flags);
602
603 if (!output_ready(devc))
604 {
605 spin_unlock_irqrestore(&devc->lock,flags);
606 goto retry;
607 }
608 write_command(devc, cmd->cmd);
609
610 ok = 0;
611 for (timeout = 50000; timeout > 0 && !ok; timeout--)
612 {
613 if (input_avail(devc))
614 {
615 if (devc->opened && devc->mode == MODE_SYNTH)
616 {
617 if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
618 ok = 1;
619 }
620 else
621 {
622 /* Device is not currently open. Use simpler method */
623 if (read_data(devc) == MPU_ACK)
624 ok = 1;
625 }
626 }
627 }
628 if (!ok)
629 {
630 spin_unlock_irqrestore(&devc->lock,flags);
631 return -EIO;
632 }
633 if (cmd->nr_args)
634 {
635 for (i = 0; i < cmd->nr_args; i++)
636 {
637 for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
638
639 if (!mpu401_out(dev, cmd->data[i]))
640 {
641 spin_unlock_irqrestore(&devc->lock,flags);
642 printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
643 return -EIO;
644 }
645 }
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 cmd->data[0] = 0;
648
649 if (cmd->nr_returns)
650 {
651 for (i = 0; i < cmd->nr_returns; i++)
652 {
653 ok = 0;
654 for (timeout = 5000; timeout > 0 && !ok; timeout--)
655 if (input_avail(devc))
656 {
657 cmd->data[i] = read_data(devc);
658 ok = 1;
659 }
660 if (!ok)
661 {
662 spin_unlock_irqrestore(&devc->lock,flags);
663 return -EIO;
664 }
665 }
666 }
667 spin_unlock_irqrestore(&devc->lock,flags);
Julia Lawall47c98072014-05-19 06:31:10 +0200668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671static int mpu_cmd(int dev, int cmd, int data)
672{
673 int ret;
674
675 static mpu_command_rec rec;
676
677 rec.cmd = cmd & 0xff;
678 rec.nr_args = ((cmd & 0xf0) == 0xE0);
679 rec.nr_returns = ((cmd & 0xf0) == 0xA0);
680 rec.data[0] = data & 0xff;
681
682 if ((ret = mpu401_command(dev, &rec)) < 0)
683 return ret;
684 return (unsigned char) rec.data[0];
685}
686
687static int mpu401_prefix_cmd(int dev, unsigned char status)
688{
689 struct mpu_config *devc = &dev_conf[dev];
690
691 if (devc->uart_mode)
692 return 1;
693
694 if (status < 0xf0)
695 {
696 if (mpu_cmd(dev, 0xD0, 0) < 0)
697 return 0;
698 return 1;
699 }
700 switch (status)
701 {
702 case 0xF0:
703 if (mpu_cmd(dev, 0xDF, 0) < 0)
704 return 0;
705 return 1;
706
707 default:
708 return 0;
709 }
710}
711
712static int mpu401_start_read(int dev)
713{
714 return 0;
715}
716
717static int mpu401_end_read(int dev)
718{
719 return 0;
720}
721
722static int mpu401_ioctl(int dev, unsigned cmd, void __user *arg)
723{
724 struct mpu_config *devc;
725 mpu_command_rec rec;
726 int val, ret;
727
728 devc = &dev_conf[dev];
729 switch (cmd)
730 {
731 case SNDCTL_MIDI_MPUMODE:
732 if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
733 printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
734 return -EINVAL;
735 }
736 if (get_user(val, (int __user *)arg))
737 return -EFAULT;
738 set_uart_mode(dev, devc, !val);
739 return 0;
740
741 case SNDCTL_MIDI_MPUCMD:
742 if (copy_from_user(&rec, arg, sizeof(rec)))
743 return -EFAULT;
744 if ((ret = mpu401_command(dev, &rec)) < 0)
745 return ret;
746 if (copy_to_user(arg, &rec, sizeof(rec)))
747 return -EFAULT;
748 return 0;
749
750 default:
751 return -EINVAL;
752 }
753}
754
755static void mpu401_kick(int dev)
756{
757}
758
759static int mpu401_buffer_status(int dev)
760{
761 return 0; /*
762 * No data in buffers
763 */
764}
765
766static int mpu_synth_ioctl(int dev, unsigned int cmd, void __user *arg)
767{
768 int midi_dev;
769 struct mpu_config *devc;
770
771 midi_dev = synth_devs[dev]->midi_dev;
772
Roel Kluin02bb57a2009-11-16 17:05:02 +0100773 if (midi_dev < 0 || midi_dev >= num_midis || midi_devs[midi_dev] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -ENXIO;
775
776 devc = &dev_conf[midi_dev];
777
778 switch (cmd)
779 {
780
781 case SNDCTL_SYNTH_INFO:
782 if (copy_to_user(arg, &mpu_synth_info[midi_dev],
783 sizeof(struct synth_info)))
784 return -EFAULT;
785 return 0;
786
787 case SNDCTL_SYNTH_MEMAVL:
788 return 0x7fffffff;
789
790 default:
791 return -EINVAL;
792 }
793}
794
795static int mpu_synth_open(int dev, int mode)
796{
797 int midi_dev, err;
798 struct mpu_config *devc;
799 struct coproc_operations *coprocessor;
800
801 midi_dev = synth_devs[dev]->midi_dev;
802
803 if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
804 return -ENXIO;
805
806 devc = &dev_conf[midi_dev];
807
808 /*
809 * Verify that the device is really running.
810 * Some devices (such as Ensoniq SoundScape don't
811 * work before the on board processor (OBP) is initialized
812 * by downloading its microcode.
813 */
814
815 if (!devc->initialized)
816 {
817 if (mpu401_status(devc) == 0xff) /* Bus float */
818 {
819 printk(KERN_ERR "mpu401: Device not initialized properly\n");
820 return -EIO;
821 }
822 reset_mpu401(devc);
823 }
824 if (devc->opened)
825 return -EBUSY;
826 devc->mode = MODE_SYNTH;
827 devc->synthno = dev;
828
829 devc->inputintr = NULL;
830
831 coprocessor = midi_devs[midi_dev]->coproc;
832 if (coprocessor) {
833 if (!try_module_get(coprocessor->owner))
834 return -ENODEV;
835
836 if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
837 {
838 printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
839 return err;
840 }
841 }
842 devc->opened = mode;
843 reset_mpu401(devc);
844
845 if (mode & OPEN_READ)
846 {
847 mpu_cmd(midi_dev, 0x8B, 0); /* Enable data in stop mode */
848 mpu_cmd(midi_dev, 0x34, 0); /* Return timing bytes in stop mode */
849 mpu_cmd(midi_dev, 0x87, 0); /* Enable pitch & controller */
850 }
851 return 0;
852}
853
854static void mpu_synth_close(int dev)
855{
856 int midi_dev;
857 struct mpu_config *devc;
858 struct coproc_operations *coprocessor;
859
860 midi_dev = synth_devs[dev]->midi_dev;
861
862 devc = &dev_conf[midi_dev];
863 mpu_cmd(midi_dev, 0x15, 0); /* Stop recording, playback and MIDI */
864 mpu_cmd(midi_dev, 0x8a, 0); /* Disable data in stopped mode */
865
866 devc->inputintr = NULL;
867
868 coprocessor = midi_devs[midi_dev]->coproc;
869 if (coprocessor) {
870 coprocessor->close(coprocessor->devc, COPR_MIDI);
871 module_put(coprocessor->owner);
872 }
873 devc->opened = 0;
874 devc->mode = 0;
875}
876
877#define MIDI_SYNTH_NAME "MPU-401 UART Midi"
878#define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
879#include "midi_synth.h"
880
881static struct synth_operations mpu401_synth_proto =
882{
883 .owner = THIS_MODULE,
884 .id = "MPU401",
885 .info = NULL,
886 .midi_dev = 0,
887 .synth_type = SYNTH_TYPE_MIDI,
888 .synth_subtype = 0,
889 .open = mpu_synth_open,
890 .close = mpu_synth_close,
891 .ioctl = mpu_synth_ioctl,
892 .kill_note = midi_synth_kill_note,
893 .start_note = midi_synth_start_note,
894 .set_instr = midi_synth_set_instr,
895 .reset = midi_synth_reset,
896 .hw_control = midi_synth_hw_control,
897 .load_patch = midi_synth_load_patch,
898 .aftertouch = midi_synth_aftertouch,
899 .controller = midi_synth_controller,
900 .panning = midi_synth_panning,
901 .bender = midi_synth_bender,
902 .setup_voice = midi_synth_setup_voice,
903 .send_sysex = midi_synth_send_sysex
904};
905
906static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
907
908static struct midi_operations mpu401_midi_proto =
909{
910 .owner = THIS_MODULE,
911 .info = {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
912 .in_info = {0},
913 .open = mpu401_open,
914 .close = mpu401_close,
915 .ioctl = mpu401_ioctl,
916 .outputc = mpu401_out,
917 .start_read = mpu401_start_read,
918 .end_read = mpu401_end_read,
919 .kick = mpu401_kick,
920 .buffer_status = mpu401_buffer_status,
921 .prefix_cmd = mpu401_prefix_cmd
922};
923
924static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
925
926static void mpu401_chk_version(int n, struct mpu_config *devc)
927{
928 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 devc->version = devc->revision = 0;
931
Jiri Slaby9ea5ca72009-06-29 18:03:34 +0200932 tmp = mpu_cmd(n, 0xAC, 0);
933 if (tmp < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 devc->version = tmp;
938
Jiri Slaby9ea5ca72009-06-29 18:03:34 +0200939 if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 devc->version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return;
942 }
943 devc->revision = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946int attach_mpu401(struct address_info *hw_config, struct module *owner)
947{
948 unsigned long flags;
949 char revision_char;
950
951 int m, ret;
952 struct mpu_config *devc;
953
954 hw_config->slots[1] = -1;
955 m = sound_alloc_mididev();
956 if (m == -1)
957 {
958 printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
959 ret = -ENOMEM;
960 goto out_err;
961 }
962 devc = &dev_conf[m];
963 devc->base = hw_config->io_base;
964 devc->osp = hw_config->osp;
965 devc->irq = hw_config->irq;
966 devc->opened = 0;
967 devc->uart_mode = 0;
968 devc->initialized = 0;
969 devc->version = 0;
970 devc->revision = 0;
971 devc->capabilities = 0;
972 devc->timer_flag = 0;
973 devc->m_busy = 0;
974 devc->m_state = ST_INIT;
975 devc->shared_irq = hw_config->always_detect;
976 devc->irq = hw_config->irq;
977 spin_lock_init(&devc->lock);
978
979 if (devc->irq < 0)
980 {
981 devc->irq *= -1;
982 devc->shared_irq = 1;
983 }
984
985 if (!hw_config->always_detect)
986 {
987 /* Verify the hardware again */
988 if (!reset_mpu401(devc))
989 {
990 printk(KERN_WARNING "mpu401: Device didn't respond\n");
991 ret = -ENODEV;
992 goto out_mididev;
993 }
994 if (!devc->shared_irq)
995 {
Andrew Morton69378382007-07-15 23:40:04 -0700996 if (request_irq(devc->irq, mpuintr, 0, "mpu401",
997 hw_config) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 {
999 printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
1000 ret = -ENOMEM;
1001 goto out_mididev;
1002 }
1003 }
1004 spin_lock_irqsave(&devc->lock,flags);
1005 mpu401_chk_version(m, devc);
1006 if (devc->version == 0)
1007 mpu401_chk_version(m, devc);
Ilpo Järvinenf329bdd2008-08-19 10:56:33 +03001008 spin_unlock_irqrestore(&devc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
1011 if (devc->version != 0)
1012 if (mpu_cmd(m, 0xC5, 0) >= 0) /* Set timebase OK */
1013 if (mpu_cmd(m, 0xE0, 120) >= 0) /* Set tempo OK */
1014 devc->capabilities |= MPU_CAP_INTLG; /* Supports intelligent mode */
1015
1016
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001017 mpu401_synth_operations[m] = kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (mpu401_synth_operations[m] == NULL)
1020 {
1021 printk(KERN_ERR "mpu401: Can't allocate memory\n");
1022 ret = -ENOMEM;
1023 goto out_irq;
1024 }
1025 if (!(devc->capabilities & MPU_CAP_INTLG)) /* No intelligent mode */
1026 {
1027 memcpy((char *) mpu401_synth_operations[m],
1028 (char *) &std_midi_synth,
1029 sizeof(struct synth_operations));
1030 }
1031 else
1032 {
1033 memcpy((char *) mpu401_synth_operations[m],
1034 (char *) &mpu401_synth_proto,
1035 sizeof(struct synth_operations));
1036 }
1037 if (owner)
1038 mpu401_synth_operations[m]->owner = owner;
1039
1040 memcpy((char *) &mpu401_midi_operations[m],
1041 (char *) &mpu401_midi_proto,
1042 sizeof(struct midi_operations));
1043
1044 mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
1045
1046 memcpy((char *) &mpu_synth_info[m],
1047 (char *) &mpu_synth_info_proto,
1048 sizeof(struct synth_info));
1049
1050 n_mpu_devs++;
1051
1052 if (devc->version == 0x20 && devc->revision >= 0x07) /* MusicQuest interface */
1053 {
1054 int ports = (devc->revision & 0x08) ? 32 : 16;
1055
1056 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1057 MPU_CAP_CLS | MPU_CAP_2PORT;
1058
1059 revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1060 sprintf(mpu_synth_info[m].name, "MQX-%d%c MIDI Interface #%d",
1061 ports,
1062 revision_char,
1063 n_mpu_devs);
1064 }
1065 else
1066 {
1067 revision_char = devc->revision ? devc->revision + '@' : ' ';
1068 if ((int) devc->revision > ('Z' - '@'))
1069 revision_char = '+';
1070
1071 devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1072
1073 if (hw_config->name)
1074 sprintf(mpu_synth_info[m].name, "%s (MPU401)", hw_config->name);
1075 else
1076 sprintf(mpu_synth_info[m].name,
Roel Kluina9870042009-07-29 12:12:09 +02001077 "MPU-401 %d.%d%c MIDI #%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 (int) (devc->version & 0xf0) >> 4,
1079 devc->version & 0x0f,
1080 revision_char,
1081 n_mpu_devs);
1082 }
1083
1084 strcpy(mpu401_midi_operations[m].info.name,
1085 mpu_synth_info[m].name);
1086
1087 conf_printf(mpu_synth_info[m].name, hw_config);
1088
1089 mpu401_synth_operations[m]->midi_dev = devc->devno = m;
1090 mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
1091
1092 if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
1093 hw_config->slots[2] = mpu_timer_init(m);
1094
1095 midi_devs[m] = &mpu401_midi_operations[devc->devno];
1096
1097 if (owner)
1098 midi_devs[m]->owner = owner;
1099
1100 hw_config->slots[1] = m;
1101 sequencer_init();
1102
1103 return 0;
1104
1105out_irq:
Andrew Morton69378382007-07-15 23:40:04 -07001106 free_irq(devc->irq, hw_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107out_mididev:
1108 sound_unload_mididev(m);
1109out_err:
1110 release_region(hw_config->io_base, 2);
1111 return ret;
1112}
1113
1114static int reset_mpu401(struct mpu_config *devc)
1115{
1116 unsigned long flags;
1117 int ok, timeout, n;
1118 int timeout_limit;
1119
1120 /*
1121 * Send the RESET command. Try again if no success at the first time.
1122 * (If the device is in the UART mode, it will not ack the reset cmd).
1123 */
1124
1125 ok = 0;
1126
1127 timeout_limit = devc->initialized ? 30000 : 100000;
1128 devc->initialized = 1;
1129
1130 for (n = 0; n < 2 && !ok; n++)
1131 {
1132 for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1133 ok = output_ready(devc);
1134
1135 write_command(devc, MPU_RESET); /*
1136 * Send MPU-401 RESET Command
1137 */
1138
1139 /*
1140 * Wait at least 25 msec. This method is not accurate so let's make the
1141 * loop bit longer. Cannot sleep since this is called during boot.
1142 */
1143
1144 for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1145 {
1146 spin_lock_irqsave(&devc->lock,flags);
1147 if (input_avail(devc))
1148 if (read_data(devc) == MPU_ACK)
1149 ok = 1;
1150 spin_unlock_irqrestore(&devc->lock,flags);
1151 }
1152
1153 }
1154
1155 devc->m_state = ST_INIT;
1156 devc->m_ptr = 0;
1157 devc->m_left = 0;
1158 devc->last_status = 0;
1159 devc->uart_mode = 0;
1160
1161 return ok;
1162}
1163
1164static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
1165{
1166 if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1167 return;
1168 if ((devc->uart_mode == 0) == (arg == 0))
1169 return; /* Already set */
1170 reset_mpu401(devc); /* This exits the uart mode */
1171
1172 if (arg)
1173 {
1174 if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
1175 {
1176 printk(KERN_ERR "mpu401: Can't enter UART mode\n");
1177 devc->uart_mode = 0;
1178 return;
1179 }
1180 }
1181 devc->uart_mode = arg;
1182
1183}
1184
1185int probe_mpu401(struct address_info *hw_config, struct resource *ports)
1186{
1187 int ok = 0;
1188 struct mpu_config tmp_devc;
1189
1190 tmp_devc.base = hw_config->io_base;
1191 tmp_devc.irq = hw_config->irq;
1192 tmp_devc.initialized = 0;
1193 tmp_devc.opened = 0;
1194 tmp_devc.osp = hw_config->osp;
1195
1196 if (hw_config->always_detect)
1197 return 1;
1198
1199 if (inb(hw_config->io_base + 1) == 0xff)
1200 {
1201 DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
1202 return 0; /* Just bus float? */
1203 }
1204 ok = reset_mpu401(&tmp_devc);
1205
1206 if (!ok)
1207 {
1208 DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
1209 }
1210 return ok;
1211}
1212
1213void unload_mpu401(struct address_info *hw_config)
1214{
1215 void *p;
1216 int n=hw_config->slots[1];
1217
1218 if (n != -1) {
1219 release_region(hw_config->io_base, 2);
1220 if (hw_config->always_detect == 0 && hw_config->irq > 0)
Andrew Morton69378382007-07-15 23:40:04 -07001221 free_irq(hw_config->irq, hw_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 p=mpu401_synth_operations[n];
1223 sound_unload_mididev(n);
1224 sound_unload_timerdev(hw_config->slots[2]);
Jesper Juhl09417372005-06-25 14:58:49 -07001225 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227}
1228
1229/*****************************************************
1230 * Timer stuff
1231 ****************************************************/
1232
1233static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1234static volatile int curr_tempo, curr_timebase, hw_timebase;
1235static int max_timebase = 8; /* 8*24=192 ppqn */
1236static volatile unsigned long next_event_time;
1237static volatile unsigned long curr_ticks, curr_clocks;
1238static unsigned long prev_event_time;
1239static int metronome_mode;
1240
1241static unsigned long clocks2ticks(unsigned long clocks)
1242{
1243 /*
1244 * The MPU-401 supports just a limited set of possible timebase values.
1245 * Since the applications require more choices, the driver has to
1246 * program the HW to do its best and to convert between the HW and
1247 * actual timebases.
1248 */
1249 return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1250}
1251
1252static void set_timebase(int midi_dev, int val)
1253{
1254 int hw_val;
1255
1256 if (val < 48)
1257 val = 48;
1258 if (val > 1000)
1259 val = 1000;
1260
1261 hw_val = val;
1262 hw_val = (hw_val + 12) / 24;
1263 if (hw_val > max_timebase)
1264 hw_val = max_timebase;
1265
1266 if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1267 {
1268 printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
1269 return;
1270 }
1271 hw_timebase = hw_val * 24;
1272 curr_timebase = val;
1273
1274}
1275
1276static void tmr_reset(struct mpu_config *devc)
1277{
1278 unsigned long flags;
1279
1280 spin_lock_irqsave(&devc->lock,flags);
1281 next_event_time = (unsigned long) -1;
1282 prev_event_time = 0;
1283 curr_ticks = curr_clocks = 0;
1284 spin_unlock_irqrestore(&devc->lock,flags);
1285}
1286
1287static void set_timer_mode(int midi_dev)
1288{
1289 if (timer_mode & TMR_MODE_CLS)
1290 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1291 else if (timer_mode & TMR_MODE_SMPTE)
1292 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1293
1294 if (timer_mode & TMR_INTERNAL)
1295 {
1296 mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
1297 }
1298 else
1299 {
1300 if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1301 {
1302 mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
1303 mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
1304 }
1305 else if (timer_mode & TMR_MODE_FSK)
1306 mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
1307 }
1308}
1309
1310static void stop_metronome(int midi_dev)
1311{
1312 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1313}
1314
1315static void setup_metronome(int midi_dev)
1316{
1317 int numerator, denominator;
1318 int clks_per_click, num_32nds_per_beat;
1319 int beats_per_measure;
1320
1321 numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1322 denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1323 clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1324 num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1325 beats_per_measure = (numerator * 4) >> denominator;
1326
1327 if (!metronome_mode)
1328 mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
1329 else
1330 {
1331 mpu_cmd(midi_dev, 0xE4, clks_per_click);
1332 mpu_cmd(midi_dev, 0xE6, beats_per_measure);
1333 mpu_cmd(midi_dev, 0x83, 0); /* Enable metronome without accents */
1334 }
1335}
1336
1337static int mpu_start_timer(int midi_dev)
1338{
1339 struct mpu_config *devc= &dev_conf[midi_dev];
1340
1341 tmr_reset(devc);
1342 set_timer_mode(midi_dev);
1343
1344 if (tmr_running)
1345 return TIMER_NOT_ARMED; /* Already running */
1346
1347 if (timer_mode & TMR_INTERNAL)
1348 {
1349 mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
1350 tmr_running = 1;
1351 return TIMER_NOT_ARMED;
1352 }
1353 else
1354 {
1355 mpu_cmd(midi_dev, 0x35, 0); /* Enable mode messages to PC */
1356 mpu_cmd(midi_dev, 0x38, 0); /* Enable sys common messages to PC */
1357 mpu_cmd(midi_dev, 0x39, 0); /* Enable real time messages to PC */
1358 mpu_cmd(midi_dev, 0x97, 0); /* Enable system exclusive messages to PC */
1359 }
1360 return TIMER_ARMED;
1361}
1362
1363static int mpu_timer_open(int dev, int mode)
1364{
1365 int midi_dev = sound_timer_devs[dev]->devlink;
1366 struct mpu_config *devc= &dev_conf[midi_dev];
1367
1368 if (timer_open)
1369 return -EBUSY;
1370
1371 tmr_reset(devc);
1372 curr_tempo = 50;
1373 mpu_cmd(midi_dev, 0xE0, 50);
1374 curr_timebase = hw_timebase = 120;
1375 set_timebase(midi_dev, 120);
1376 timer_open = 1;
1377 metronome_mode = 0;
1378 set_timer_mode(midi_dev);
1379
1380 mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
1381 mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
1382
1383 return 0;
1384}
1385
1386static void mpu_timer_close(int dev)
1387{
1388 int midi_dev = sound_timer_devs[dev]->devlink;
1389
1390 timer_open = tmr_running = 0;
1391 mpu_cmd(midi_dev, 0x15, 0); /* Stop all */
1392 mpu_cmd(midi_dev, 0x94, 0); /* Disable clock to host */
1393 mpu_cmd(midi_dev, 0x8c, 0); /* Disable measure end messages to host */
1394 stop_metronome(midi_dev);
1395}
1396
1397static int mpu_timer_event(int dev, unsigned char *event)
1398{
1399 unsigned char command = event[1];
1400 unsigned long parm = *(unsigned int *) &event[4];
1401 int midi_dev = sound_timer_devs[dev]->devlink;
1402
1403 switch (command)
1404 {
1405 case TMR_WAIT_REL:
1406 parm += prev_event_time;
1407 case TMR_WAIT_ABS:
1408 if (parm > 0)
1409 {
1410 long time;
1411
1412 if (parm <= curr_ticks) /* It's the time */
1413 return TIMER_NOT_ARMED;
1414 time = parm;
1415 next_event_time = prev_event_time = time;
1416
1417 return TIMER_ARMED;
1418 }
1419 break;
1420
1421 case TMR_START:
1422 if (tmr_running)
1423 break;
1424 return mpu_start_timer(midi_dev);
1425
1426 case TMR_STOP:
1427 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1428 stop_metronome(midi_dev);
1429 tmr_running = 0;
1430 break;
1431
1432 case TMR_CONTINUE:
1433 if (tmr_running)
1434 break;
1435 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1436 setup_metronome(midi_dev);
1437 tmr_running = 1;
1438 break;
1439
1440 case TMR_TEMPO:
1441 if (parm)
1442 {
1443 if (parm < 8)
1444 parm = 8;
1445 if (parm > 250)
1446 parm = 250;
1447 if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
1448 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
1449 curr_tempo = parm;
1450 }
1451 break;
1452
1453 case TMR_ECHO:
1454 seq_copy_to_input(event, 8);
1455 break;
1456
1457 case TMR_TIMESIG:
1458 if (metronome_mode) /* Metronome enabled */
1459 {
1460 metronome_mode = parm;
1461 setup_metronome(midi_dev);
1462 }
1463 break;
1464
1465 default:;
1466 }
1467 return TIMER_NOT_ARMED;
1468}
1469
1470static unsigned long mpu_timer_get_time(int dev)
1471{
1472 if (!timer_open)
1473 return 0;
1474
1475 return curr_ticks;
1476}
1477
1478static int mpu_timer_ioctl(int dev, unsigned int command, void __user *arg)
1479{
1480 int midi_dev = sound_timer_devs[dev]->devlink;
1481 int __user *p = (int __user *)arg;
1482
1483 switch (command)
1484 {
1485 case SNDCTL_TMR_SOURCE:
1486 {
1487 int parm;
1488
1489 if (get_user(parm, p))
1490 return -EFAULT;
1491 parm &= timer_caps;
1492
1493 if (parm != 0)
1494 {
1495 timer_mode = parm;
1496
1497 if (timer_mode & TMR_MODE_CLS)
1498 mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
1499 else if (timer_mode & TMR_MODE_SMPTE)
1500 mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
1501 }
1502 if (put_user(timer_mode, p))
1503 return -EFAULT;
1504 return timer_mode;
1505 }
1506 break;
1507
1508 case SNDCTL_TMR_START:
1509 mpu_start_timer(midi_dev);
1510 return 0;
1511
1512 case SNDCTL_TMR_STOP:
1513 tmr_running = 0;
1514 mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
1515 stop_metronome(midi_dev);
1516 return 0;
1517
1518 case SNDCTL_TMR_CONTINUE:
1519 if (tmr_running)
1520 return 0;
1521 tmr_running = 1;
1522 mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
1523 return 0;
1524
1525 case SNDCTL_TMR_TIMEBASE:
1526 {
1527 int val;
1528 if (get_user(val, p))
1529 return -EFAULT;
1530 if (val)
1531 set_timebase(midi_dev, val);
1532 if (put_user(curr_timebase, p))
1533 return -EFAULT;
1534 return curr_timebase;
1535 }
1536 break;
1537
1538 case SNDCTL_TMR_TEMPO:
1539 {
1540 int val;
1541 int ret;
1542
1543 if (get_user(val, p))
1544 return -EFAULT;
1545
1546 if (val)
1547 {
1548 if (val < 8)
1549 val = 8;
1550 if (val > 250)
1551 val = 250;
1552 if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
1553 {
1554 printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
1555 return ret;
1556 }
1557 curr_tempo = val;
1558 }
1559 if (put_user(curr_tempo, p))
1560 return -EFAULT;
1561 return curr_tempo;
1562 }
1563 break;
1564
1565 case SNDCTL_SEQ_CTRLRATE:
1566 {
1567 int val;
1568 if (get_user(val, p))
1569 return -EFAULT;
1570
1571 if (val != 0) /* Can't change */
1572 return -EINVAL;
1573 val = ((curr_tempo * curr_timebase) + 30)/60;
1574 if (put_user(val, p))
1575 return -EFAULT;
1576 return val;
1577 }
1578 break;
1579
1580 case SNDCTL_SEQ_GETTIME:
1581 if (put_user(curr_ticks, p))
1582 return -EFAULT;
1583 return curr_ticks;
1584
1585 case SNDCTL_TMR_METRONOME:
1586 if (get_user(metronome_mode, p))
1587 return -EFAULT;
1588 setup_metronome(midi_dev);
1589 return 0;
1590
1591 default:;
1592 }
1593 return -EINVAL;
1594}
1595
1596static void mpu_timer_arm(int dev, long time)
1597{
1598 if (time < 0)
1599 time = curr_ticks + 1;
1600 else if (time <= curr_ticks) /* It's the time */
1601 return;
1602 next_event_time = prev_event_time = time;
1603 return;
1604}
1605
1606static struct sound_timer_operations mpu_timer =
1607{
1608 .owner = THIS_MODULE,
1609 .info = {"MPU-401 Timer", 0},
1610 .priority = 10, /* Priority */
1611 .devlink = 0, /* Local device link */
1612 .open = mpu_timer_open,
1613 .close = mpu_timer_close,
1614 .event = mpu_timer_event,
1615 .get_time = mpu_timer_get_time,
1616 .ioctl = mpu_timer_ioctl,
1617 .arm_timer = mpu_timer_arm
1618};
1619
1620static void mpu_timer_interrupt(void)
1621{
1622 if (!timer_open)
1623 return;
1624
1625 if (!tmr_running)
1626 return;
1627
1628 curr_clocks++;
1629 curr_ticks = clocks2ticks(curr_clocks);
1630
1631 if (curr_ticks >= next_event_time)
1632 {
1633 next_event_time = (unsigned long) -1;
1634 sequencer_timer(0);
1635 }
1636}
1637
1638static void timer_ext_event(struct mpu_config *devc, int event, int parm)
1639{
1640 int midi_dev = devc->devno;
1641
1642 if (!devc->timer_flag)
1643 return;
1644
1645 switch (event)
1646 {
1647 case TMR_CLOCK:
1648 printk("<MIDI clk>");
1649 break;
1650
1651 case TMR_START:
1652 printk("Ext MIDI start\n");
1653 if (!tmr_running)
1654 {
1655 if (timer_mode & TMR_EXTERNAL)
1656 {
1657 tmr_running = 1;
1658 setup_metronome(midi_dev);
1659 next_event_time = 0;
1660 STORE(SEQ_START_TIMER());
1661 }
1662 }
1663 break;
1664
1665 case TMR_STOP:
1666 printk("Ext MIDI stop\n");
1667 if (timer_mode & TMR_EXTERNAL)
1668 {
1669 tmr_running = 0;
1670 stop_metronome(midi_dev);
1671 STORE(SEQ_STOP_TIMER());
1672 }
1673 break;
1674
1675 case TMR_CONTINUE:
1676 printk("Ext MIDI continue\n");
1677 if (timer_mode & TMR_EXTERNAL)
1678 {
1679 tmr_running = 1;
1680 setup_metronome(midi_dev);
1681 STORE(SEQ_CONTINUE_TIMER());
1682 }
1683 break;
1684
1685 case TMR_SPP:
1686 printk("Songpos: %d\n", parm);
1687 if (timer_mode & TMR_EXTERNAL)
1688 {
1689 STORE(SEQ_SONGPOS(parm));
1690 }
1691 break;
1692 }
1693}
1694
1695static int mpu_timer_init(int midi_dev)
1696{
1697 struct mpu_config *devc;
1698 int n;
1699
1700 devc = &dev_conf[midi_dev];
1701
1702 if (timer_initialized)
1703 return -1; /* There is already a similar timer */
1704
1705 timer_initialized = 1;
1706
1707 mpu_timer.devlink = midi_dev;
1708 dev_conf[midi_dev].timer_flag = 1;
1709
1710 n = sound_alloc_timerdev();
1711 if (n == -1)
1712 n = 0;
1713 sound_timer_devs[n] = &mpu_timer;
1714
1715 if (devc->version < 0x20) /* Original MPU-401 */
1716 timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1717 else
1718 {
1719 /*
1720 * The version number 2.0 is used (at least) by the
1721 * MusicQuest cards and the Roland Super-MPU.
1722 *
1723 * MusicQuest has given a special meaning to the bits of the
1724 * revision number. The Super-MPU returns 0.
1725 */
1726
1727 if (devc->revision)
1728 timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1729
1730 if (devc->revision & 0x02)
1731 timer_caps |= TMR_MODE_CLS;
1732
1733
1734 if (devc->revision & 0x40)
1735 max_timebase = 10; /* Has the 216 and 240 ppqn modes */
1736 }
1737
1738 timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1739 return n;
1740
1741}
1742
1743EXPORT_SYMBOL(probe_mpu401);
1744EXPORT_SYMBOL(attach_mpu401);
1745EXPORT_SYMBOL(unload_mpu401);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747static struct address_info cfg;
1748
1749static int io = -1;
1750static int irq = -1;
1751
1752module_param(irq, int, 0);
1753module_param(io, int, 0);
1754
1755static int __init init_mpu401(void)
1756{
1757 int ret;
1758 /* Can be loaded either for module use or to provide functions
1759 to others */
1760 if (io != -1 && irq != -1) {
1761 struct resource *ports;
1762 cfg.irq = irq;
1763 cfg.io_base = io;
1764 ports = request_region(io, 2, "mpu401");
1765 if (!ports)
1766 return -EBUSY;
1767 if (probe_mpu401(&cfg, ports) == 0) {
1768 release_region(io, 2);
1769 return -ENODEV;
1770 }
1771 if ((ret = attach_mpu401(&cfg, THIS_MODULE)))
1772 return ret;
1773 }
1774
1775 return 0;
1776}
1777
1778static void __exit cleanup_mpu401(void)
1779{
1780 if (io != -1 && irq != -1) {
1781 /* Check for use by, for example, sscape driver */
1782 unload_mpu401(&cfg);
1783 }
1784}
1785
1786module_init(init_mpu401);
1787module_exit(cleanup_mpu401);
1788
1789#ifndef MODULE
1790static int __init setup_mpu401(char *str)
1791{
1792 /* io, irq */
1793 int ints[3];
1794
1795 str = get_options(str, ARRAY_SIZE(ints), ints);
1796
1797 io = ints[1];
1798 irq = ints[2];
1799
1800 return 1;
1801}
1802
1803__setup("mpu401=", setup_mpu401);
1804#endif
1805MODULE_LICENSE("GPL");