Add linux-next specific files for 20110525
[linux-2.6/next.git] / sound / oss / sb_midi.c
blobf139028e85c0dda9f30f9b3406f0e945f5d1bc70
1 /*
2 * sound/oss/sb_midi.c
4 * The low level driver for the Sound Blaster DS chips.
7 * Copyright (C) by Hannu Savolainen 1993-1997
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.
14 #include <linux/spinlock.h>
15 #include <linux/slab.h>
17 #include "sound_config.h"
19 #include "sb.h"
20 #undef SB_TEST_IRQ
23 * The DSP channel can be used either for input or output. Variable
24 * 'sb_irq_mode' will be set when the program calls read or write first time
25 * after open. Current version doesn't support mode changes without closing
26 * and reopening the device. Support for this feature may be implemented in a
27 * future version of this driver.
31 static int sb_midi_open(int dev, int mode,
32 void (*input) (int dev, unsigned char data),
33 void (*output) (int dev)
36 sb_devc *devc = midi_devs[dev]->devc;
37 unsigned long flags;
39 if (devc == NULL)
40 return -ENXIO;
42 spin_lock_irqsave(&devc->lock, flags);
43 if (devc->opened)
45 spin_unlock_irqrestore(&devc->lock, flags);
46 return -EBUSY;
48 devc->opened = 1;
49 spin_unlock_irqrestore(&devc->lock, flags);
51 devc->irq_mode = IMODE_MIDI;
52 devc->midi_broken = 0;
54 sb_dsp_reset(devc);
56 if (!sb_dsp_command(devc, 0x35)) /* Start MIDI UART mode */
58 devc->opened = 0;
59 return -EIO;
61 devc->intr_active = 1;
63 if (mode & OPEN_READ)
65 devc->input_opened = 1;
66 devc->midi_input_intr = input;
68 return 0;
71 static void sb_midi_close(int dev)
73 sb_devc *devc = midi_devs[dev]->devc;
74 unsigned long flags;
76 if (devc == NULL)
77 return;
79 spin_lock_irqsave(&devc->lock, flags);
80 sb_dsp_reset(devc);
81 devc->intr_active = 0;
82 devc->input_opened = 0;
83 devc->opened = 0;
84 spin_unlock_irqrestore(&devc->lock, flags);
87 static int sb_midi_out(int dev, unsigned char midi_byte)
89 sb_devc *devc = midi_devs[dev]->devc;
91 if (devc == NULL)
92 return 1;
94 if (devc->midi_broken)
95 return 1;
97 if (!sb_dsp_command(devc, midi_byte))
99 devc->midi_broken = 1;
100 return 1;
102 return 1;
105 static int sb_midi_start_read(int dev)
107 return 0;
110 static int sb_midi_end_read(int dev)
112 sb_devc *devc = midi_devs[dev]->devc;
114 if (devc == NULL)
115 return -ENXIO;
117 sb_dsp_reset(devc);
118 devc->intr_active = 0;
119 return 0;
122 static int sb_midi_ioctl(int dev, unsigned cmd, void __user *arg)
124 return -EINVAL;
127 void sb_midi_interrupt(sb_devc * devc)
129 unsigned long flags;
130 unsigned char data;
132 if (devc == NULL)
133 return;
135 spin_lock_irqsave(&devc->lock, flags);
137 data = inb(DSP_READ);
138 if (devc->input_opened)
139 devc->midi_input_intr(devc->my_mididev, data);
141 spin_unlock_irqrestore(&devc->lock, flags);
144 #define MIDI_SYNTH_NAME "Sound Blaster Midi"
145 #define MIDI_SYNTH_CAPS 0
146 #include "midi_synth.h"
148 static struct midi_operations sb_midi_operations =
150 .owner = THIS_MODULE,
151 .info = {"Sound Blaster", 0, 0, SNDCARD_SB},
152 .converter = &std_midi_synth,
153 .in_info = {0},
154 .open = sb_midi_open,
155 .close = sb_midi_close,
156 .ioctl = sb_midi_ioctl,
157 .outputc = sb_midi_out,
158 .start_read = sb_midi_start_read,
159 .end_read = sb_midi_end_read,
162 void sb_dsp_midi_init(sb_devc * devc, struct module *owner)
164 int dev;
166 if (devc->model < 2) /* No MIDI support for SB 1.x */
167 return;
169 dev = sound_alloc_mididev();
171 if (dev == -1)
173 printk(KERN_ERR "sb_midi: too many MIDI devices detected\n");
174 return;
176 std_midi_synth.midi_dev = devc->my_mididev = dev;
177 midi_devs[dev] = kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
178 if (midi_devs[dev] == NULL)
180 printk(KERN_WARNING "Sound Blaster: failed to allocate MIDI memory.\n");
181 sound_unload_mididev(dev);
182 return;
184 memcpy((char *) midi_devs[dev], (char *) &sb_midi_operations,
185 sizeof(struct midi_operations));
187 if (owner)
188 midi_devs[dev]->owner = owner;
190 midi_devs[dev]->devc = devc;
193 midi_devs[dev]->converter = kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
194 if (midi_devs[dev]->converter == NULL)
196 printk(KERN_WARNING "Sound Blaster: failed to allocate MIDI memory.\n");
197 kfree(midi_devs[dev]);
198 sound_unload_mididev(dev);
199 return;
201 memcpy((char *) midi_devs[dev]->converter, (char *) &std_midi_synth,
202 sizeof(struct synth_operations));
204 midi_devs[dev]->converter->id = "SBMIDI";
205 sequencer_init();