]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * usbmidi.c - ALSA USB MIDI driver | |
3 | * | |
96f61d9a | 4 | * Copyright (c) 2002-2009 Clemens Ladisch |
1da177e4 LT |
5 | * All rights reserved. |
6 | * | |
7 | * Based on the OSS usb-midi driver by NAGANO Daisuke, | |
8 | * NetBSD's umidi driver by Takuya SHIOZAKI, | |
9 | * the "USB Device Class Definition for MIDI Devices" by Roland | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or without | |
12 | * modification, are permitted provided that the following conditions | |
13 | * are met: | |
14 | * 1. Redistributions of source code must retain the above copyright | |
15 | * notice, this list of conditions, and the following disclaimer, | |
16 | * without modification. | |
17 | * 2. The name of the author may not be used to endorse or promote products | |
18 | * derived from this software without specific prior written permission. | |
19 | * | |
20 | * Alternatively, this software may be distributed and/or modified under the | |
21 | * terms of the GNU General Public License as published by the Free Software | |
22 | * Foundation; either version 2 of the License, or (at your option) any later | |
23 | * version. | |
24 | * | |
25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR | |
29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
35 | * SUCH DAMAGE. | |
36 | */ | |
37 | ||
1da177e4 LT |
38 | #include <linux/kernel.h> |
39 | #include <linux/types.h> | |
40 | #include <linux/bitops.h> | |
41 | #include <linux/interrupt.h> | |
42 | #include <linux/spinlock.h> | |
43 | #include <linux/string.h> | |
44 | #include <linux/init.h> | |
45 | #include <linux/slab.h> | |
c8846970 | 46 | #include <linux/timer.h> |
1da177e4 | 47 | #include <linux/usb.h> |
a65dd997 | 48 | #include <linux/wait.h> |
de48c7bc DM |
49 | #include <linux/usb/audio.h> |
50 | ||
1da177e4 | 51 | #include <sound/core.h> |
96f61d9a | 52 | #include <sound/control.h> |
1da177e4 | 53 | #include <sound/rawmidi.h> |
a7b928ac | 54 | #include <sound/asequencer.h> |
1da177e4 | 55 | #include "usbaudio.h" |
3e1aebef | 56 | #include "usbmidi.h" |
1da177e4 LT |
57 | |
58 | ||
59 | /* | |
60 | * define this to log all USB packets | |
61 | */ | |
62 | /* #define DUMP_PACKETS */ | |
63 | ||
c8846970 CL |
64 | /* |
65 | * how long to wait after some USB errors, so that khubd can disconnect() us | |
66 | * without too many spurious errors | |
67 | */ | |
68 | #define ERROR_DELAY_JIFFIES (HZ / 10) | |
69 | ||
ed4affa5 | 70 | #define OUTPUT_URBS 7 |
4773d1fb CL |
71 | #define INPUT_URBS 7 |
72 | ||
1da177e4 LT |
73 | |
74 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); | |
75 | MODULE_DESCRIPTION("USB Audio/MIDI helper module"); | |
76 | MODULE_LICENSE("Dual BSD/GPL"); | |
77 | ||
78 | ||
79 | struct usb_ms_header_descriptor { | |
80 | __u8 bLength; | |
81 | __u8 bDescriptorType; | |
82 | __u8 bDescriptorSubtype; | |
83 | __u8 bcdMSC[2]; | |
84 | __le16 wTotalLength; | |
85 | } __attribute__ ((packed)); | |
86 | ||
87 | struct usb_ms_endpoint_descriptor { | |
88 | __u8 bLength; | |
89 | __u8 bDescriptorType; | |
90 | __u8 bDescriptorSubtype; | |
91 | __u8 bNumEmbMIDIJack; | |
92 | __u8 baAssocJackID[0]; | |
93 | } __attribute__ ((packed)); | |
94 | ||
86e07d34 TI |
95 | struct snd_usb_midi_in_endpoint; |
96 | struct snd_usb_midi_out_endpoint; | |
97 | struct snd_usb_midi_endpoint; | |
1da177e4 LT |
98 | |
99 | struct usb_protocol_ops { | |
86e07d34 | 100 | void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int); |
ed4affa5 | 101 | void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb); |
1da177e4 | 102 | void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t); |
86e07d34 TI |
103 | void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*); |
104 | void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*); | |
1da177e4 LT |
105 | }; |
106 | ||
107 | struct snd_usb_midi { | |
d82af9f9 CL |
108 | struct usb_device *dev; |
109 | struct snd_card *card; | |
1da177e4 | 110 | struct usb_interface *iface; |
86e07d34 TI |
111 | const struct snd_usb_audio_quirk *quirk; |
112 | struct snd_rawmidi *rmidi; | |
1da177e4 LT |
113 | struct usb_protocol_ops* usb_protocol_ops; |
114 | struct list_head list; | |
c8846970 | 115 | struct timer_list error_timer; |
c0792e00 | 116 | spinlock_t disc_lock; |
96f61d9a | 117 | struct mutex mutex; |
d82af9f9 CL |
118 | u32 usb_id; |
119 | int next_midi_device; | |
1da177e4 LT |
120 | |
121 | struct snd_usb_midi_endpoint { | |
86e07d34 TI |
122 | struct snd_usb_midi_out_endpoint *out; |
123 | struct snd_usb_midi_in_endpoint *in; | |
1da177e4 LT |
124 | } endpoints[MIDI_MAX_ENDPOINTS]; |
125 | unsigned long input_triggered; | |
96f61d9a | 126 | unsigned int opened; |
c0792e00 | 127 | unsigned char disconnected; |
96f61d9a CL |
128 | |
129 | struct snd_kcontrol *roland_load_ctl; | |
1da177e4 LT |
130 | }; |
131 | ||
132 | struct snd_usb_midi_out_endpoint { | |
86e07d34 | 133 | struct snd_usb_midi* umidi; |
ed4affa5 CL |
134 | struct out_urb_context { |
135 | struct urb *urb; | |
136 | struct snd_usb_midi_out_endpoint *ep; | |
137 | } urbs[OUTPUT_URBS]; | |
138 | unsigned int active_urbs; | |
a65dd997 | 139 | unsigned int drain_urbs; |
1da177e4 LT |
140 | int max_transfer; /* size of urb buffer */ |
141 | struct tasklet_struct tasklet; | |
a65dd997 | 142 | unsigned int next_urb; |
1da177e4 LT |
143 | spinlock_t buffer_lock; |
144 | ||
145 | struct usbmidi_out_port { | |
86e07d34 TI |
146 | struct snd_usb_midi_out_endpoint* ep; |
147 | struct snd_rawmidi_substream *substream; | |
1da177e4 LT |
148 | int active; |
149 | uint8_t cable; /* cable number << 4 */ | |
150 | uint8_t state; | |
151 | #define STATE_UNKNOWN 0 | |
152 | #define STATE_1PARAM 1 | |
153 | #define STATE_2PARAM_1 2 | |
154 | #define STATE_2PARAM_2 3 | |
155 | #define STATE_SYSEX_0 4 | |
156 | #define STATE_SYSEX_1 5 | |
157 | #define STATE_SYSEX_2 6 | |
158 | uint8_t data[2]; | |
159 | } ports[0x10]; | |
160 | int current_port; | |
a65dd997 CL |
161 | |
162 | wait_queue_head_t drain_wait; | |
1da177e4 LT |
163 | }; |
164 | ||
165 | struct snd_usb_midi_in_endpoint { | |
86e07d34 | 166 | struct snd_usb_midi* umidi; |
4773d1fb | 167 | struct urb* urbs[INPUT_URBS]; |
1da177e4 | 168 | struct usbmidi_in_port { |
86e07d34 | 169 | struct snd_rawmidi_substream *substream; |
d05cc104 | 170 | u8 running_status_length; |
1da177e4 | 171 | } ports[0x10]; |
c8846970 CL |
172 | u8 seen_f5; |
173 | u8 error_resubmit; | |
1da177e4 LT |
174 | int current_port; |
175 | }; | |
176 | ||
86e07d34 | 177 | static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep); |
1da177e4 LT |
178 | |
179 | static const uint8_t snd_usbmidi_cin_length[] = { | |
180 | 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1 | |
181 | }; | |
182 | ||
183 | /* | |
184 | * Submits the URB, with error handling. | |
185 | */ | |
55016f10 | 186 | static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags) |
1da177e4 LT |
187 | { |
188 | int err = usb_submit_urb(urb, flags); | |
189 | if (err < 0 && err != -ENODEV) | |
190 | snd_printk(KERN_ERR "usb_submit_urb: %d\n", err); | |
191 | return err; | |
192 | } | |
193 | ||
194 | /* | |
195 | * Error handling for URB completion functions. | |
196 | */ | |
197 | static int snd_usbmidi_urb_error(int status) | |
198 | { | |
c8846970 CL |
199 | switch (status) { |
200 | /* manually unlinked, or device gone */ | |
201 | case -ENOENT: | |
202 | case -ECONNRESET: | |
203 | case -ESHUTDOWN: | |
204 | case -ENODEV: | |
205 | return -ENODEV; | |
206 | /* errors that might occur during unplugging */ | |
38e2bfc9 PZ |
207 | case -EPROTO: |
208 | case -ETIME: | |
209 | case -EILSEQ: | |
c8846970 CL |
210 | return -EIO; |
211 | default: | |
212 | snd_printk(KERN_ERR "urb status %d\n", status); | |
213 | return 0; /* continue */ | |
214 | } | |
1da177e4 LT |
215 | } |
216 | ||
217 | /* | |
218 | * Receives a chunk of MIDI data. | |
219 | */ | |
86e07d34 | 220 | static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx, |
1da177e4 LT |
221 | uint8_t* data, int length) |
222 | { | |
86e07d34 | 223 | struct usbmidi_in_port* port = &ep->ports[portidx]; |
1da177e4 LT |
224 | |
225 | if (!port->substream) { | |
226 | snd_printd("unexpected port %d!\n", portidx); | |
227 | return; | |
228 | } | |
229 | if (!test_bit(port->substream->number, &ep->umidi->input_triggered)) | |
230 | return; | |
231 | snd_rawmidi_receive(port->substream, data, length); | |
232 | } | |
233 | ||
234 | #ifdef DUMP_PACKETS | |
235 | static void dump_urb(const char *type, const u8 *data, int length) | |
236 | { | |
237 | snd_printk(KERN_DEBUG "%s packet: [", type); | |
238 | for (; length > 0; ++data, --length) | |
239 | printk(" %02x", *data); | |
240 | printk(" ]\n"); | |
241 | } | |
242 | #else | |
243 | #define dump_urb(type, data, length) /* nothing */ | |
244 | #endif | |
245 | ||
246 | /* | |
247 | * Processes the data read from the device. | |
248 | */ | |
7d12e780 | 249 | static void snd_usbmidi_in_urb_complete(struct urb* urb) |
1da177e4 | 250 | { |
86e07d34 | 251 | struct snd_usb_midi_in_endpoint* ep = urb->context; |
1da177e4 LT |
252 | |
253 | if (urb->status == 0) { | |
254 | dump_urb("received", urb->transfer_buffer, urb->actual_length); | |
255 | ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer, | |
256 | urb->actual_length); | |
257 | } else { | |
c8846970 CL |
258 | int err = snd_usbmidi_urb_error(urb->status); |
259 | if (err < 0) { | |
260 | if (err != -ENODEV) { | |
261 | ep->error_resubmit = 1; | |
262 | mod_timer(&ep->umidi->error_timer, | |
263 | jiffies + ERROR_DELAY_JIFFIES); | |
264 | } | |
1da177e4 | 265 | return; |
c8846970 | 266 | } |
1da177e4 LT |
267 | } |
268 | ||
d82af9f9 | 269 | urb->dev = ep->umidi->dev; |
3cfc1eb1 | 270 | snd_usbmidi_submit_urb(urb, GFP_ATOMIC); |
1da177e4 LT |
271 | } |
272 | ||
7d12e780 | 273 | static void snd_usbmidi_out_urb_complete(struct urb* urb) |
1da177e4 | 274 | { |
ed4affa5 CL |
275 | struct out_urb_context *context = urb->context; |
276 | struct snd_usb_midi_out_endpoint* ep = context->ep; | |
a65dd997 | 277 | unsigned int urb_index; |
1da177e4 LT |
278 | |
279 | spin_lock(&ep->buffer_lock); | |
a65dd997 CL |
280 | urb_index = context - ep->urbs; |
281 | ep->active_urbs &= ~(1 << urb_index); | |
282 | if (unlikely(ep->drain_urbs)) { | |
283 | ep->drain_urbs &= ~(1 << urb_index); | |
284 | wake_up(&ep->drain_wait); | |
285 | } | |
1da177e4 LT |
286 | spin_unlock(&ep->buffer_lock); |
287 | if (urb->status < 0) { | |
c8846970 CL |
288 | int err = snd_usbmidi_urb_error(urb->status); |
289 | if (err < 0) { | |
290 | if (err != -ENODEV) | |
291 | mod_timer(&ep->umidi->error_timer, | |
292 | jiffies + ERROR_DELAY_JIFFIES); | |
1da177e4 | 293 | return; |
c8846970 | 294 | } |
1da177e4 LT |
295 | } |
296 | snd_usbmidi_do_output(ep); | |
297 | } | |
298 | ||
299 | /* | |
300 | * This is called when some data should be transferred to the device | |
301 | * (from one or more substreams). | |
302 | */ | |
86e07d34 | 303 | static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep) |
1da177e4 | 304 | { |
ed4affa5 CL |
305 | unsigned int urb_index; |
306 | struct urb* urb; | |
1da177e4 LT |
307 | unsigned long flags; |
308 | ||
309 | spin_lock_irqsave(&ep->buffer_lock, flags); | |
d82af9f9 | 310 | if (ep->umidi->disconnected) { |
1da177e4 LT |
311 | spin_unlock_irqrestore(&ep->buffer_lock, flags); |
312 | return; | |
313 | } | |
314 | ||
a65dd997 | 315 | urb_index = ep->next_urb; |
ed4affa5 | 316 | for (;;) { |
a65dd997 CL |
317 | if (!(ep->active_urbs & (1 << urb_index))) { |
318 | urb = ep->urbs[urb_index].urb; | |
319 | urb->transfer_buffer_length = 0; | |
320 | ep->umidi->usb_protocol_ops->output(ep, urb); | |
321 | if (urb->transfer_buffer_length == 0) | |
ed4affa5 | 322 | break; |
1da177e4 | 323 | |
a65dd997 CL |
324 | dump_urb("sending", urb->transfer_buffer, |
325 | urb->transfer_buffer_length); | |
d82af9f9 | 326 | urb->dev = ep->umidi->dev; |
a65dd997 CL |
327 | if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0) |
328 | break; | |
329 | ep->active_urbs |= 1 << urb_index; | |
330 | } | |
331 | if (++urb_index >= OUTPUT_URBS) | |
332 | urb_index = 0; | |
333 | if (urb_index == ep->next_urb) | |
ed4affa5 | 334 | break; |
1da177e4 | 335 | } |
a65dd997 | 336 | ep->next_urb = urb_index; |
1da177e4 LT |
337 | spin_unlock_irqrestore(&ep->buffer_lock, flags); |
338 | } | |
339 | ||
340 | static void snd_usbmidi_out_tasklet(unsigned long data) | |
341 | { | |
86e07d34 | 342 | struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data; |
1da177e4 LT |
343 | |
344 | snd_usbmidi_do_output(ep); | |
345 | } | |
346 | ||
c8846970 CL |
347 | /* called after transfers had been interrupted due to some USB error */ |
348 | static void snd_usbmidi_error_timer(unsigned long data) | |
349 | { | |
86e07d34 | 350 | struct snd_usb_midi *umidi = (struct snd_usb_midi *)data; |
4773d1fb | 351 | unsigned int i, j; |
c8846970 | 352 | |
c0792e00 TI |
353 | spin_lock(&umidi->disc_lock); |
354 | if (umidi->disconnected) { | |
355 | spin_unlock(&umidi->disc_lock); | |
356 | return; | |
357 | } | |
c8846970 | 358 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
86e07d34 | 359 | struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in; |
c8846970 CL |
360 | if (in && in->error_resubmit) { |
361 | in->error_resubmit = 0; | |
4773d1fb | 362 | for (j = 0; j < INPUT_URBS; ++j) { |
d82af9f9 | 363 | in->urbs[j]->dev = umidi->dev; |
4773d1fb CL |
364 | snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC); |
365 | } | |
c8846970 CL |
366 | } |
367 | if (umidi->endpoints[i].out) | |
368 | snd_usbmidi_do_output(umidi->endpoints[i].out); | |
369 | } | |
c0792e00 | 370 | spin_unlock(&umidi->disc_lock); |
c8846970 CL |
371 | } |
372 | ||
1da177e4 | 373 | /* helper function to send static data that may not DMA-able */ |
86e07d34 | 374 | static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep, |
1da177e4 LT |
375 | const void *data, int len) |
376 | { | |
ed4affa5 | 377 | int err = 0; |
52978be6 | 378 | void *buf = kmemdup(data, len, GFP_KERNEL); |
1da177e4 LT |
379 | if (!buf) |
380 | return -ENOMEM; | |
1da177e4 | 381 | dump_urb("sending", buf, len); |
ed4affa5 | 382 | if (ep->urbs[0].urb) |
d82af9f9 | 383 | err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe, |
ed4affa5 | 384 | buf, len, NULL, 250); |
1da177e4 LT |
385 | kfree(buf); |
386 | return err; | |
387 | } | |
388 | ||
389 | /* | |
390 | * Standard USB MIDI protocol: see the spec. | |
391 | * Midiman protocol: like the standard protocol, but the control byte is the | |
392 | * fourth byte in each packet, and uses length instead of CIN. | |
393 | */ | |
394 | ||
86e07d34 | 395 | static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep, |
1da177e4 LT |
396 | uint8_t* buffer, int buffer_length) |
397 | { | |
398 | int i; | |
399 | ||
400 | for (i = 0; i + 3 < buffer_length; i += 4) | |
401 | if (buffer[i] != 0) { | |
402 | int cable = buffer[i] >> 4; | |
403 | int length = snd_usbmidi_cin_length[buffer[i] & 0x0f]; | |
404 | snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length); | |
405 | } | |
406 | } | |
407 | ||
86e07d34 | 408 | static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep, |
1da177e4 LT |
409 | uint8_t* buffer, int buffer_length) |
410 | { | |
411 | int i; | |
412 | ||
413 | for (i = 0; i + 3 < buffer_length; i += 4) | |
414 | if (buffer[i + 3] != 0) { | |
415 | int port = buffer[i + 3] >> 4; | |
416 | int length = buffer[i + 3] & 3; | |
417 | snd_usbmidi_input_data(ep, port, &buffer[i], length); | |
418 | } | |
419 | } | |
420 | ||
d05cc104 CL |
421 | /* |
422 | * Buggy M-Audio device: running status on input results in a packet that has | |
423 | * the data bytes but not the status byte and that is marked with CIN 4. | |
424 | */ | |
425 | static void snd_usbmidi_maudio_broken_running_status_input( | |
426 | struct snd_usb_midi_in_endpoint* ep, | |
427 | uint8_t* buffer, int buffer_length) | |
428 | { | |
429 | int i; | |
430 | ||
431 | for (i = 0; i + 3 < buffer_length; i += 4) | |
432 | if (buffer[i] != 0) { | |
433 | int cable = buffer[i] >> 4; | |
434 | u8 cin = buffer[i] & 0x0f; | |
435 | struct usbmidi_in_port *port = &ep->ports[cable]; | |
436 | int length; | |
437 | ||
438 | length = snd_usbmidi_cin_length[cin]; | |
439 | if (cin == 0xf && buffer[i + 1] >= 0xf8) | |
440 | ; /* realtime msg: no running status change */ | |
441 | else if (cin >= 0x8 && cin <= 0xe) | |
442 | /* channel msg */ | |
443 | port->running_status_length = length - 1; | |
444 | else if (cin == 0x4 && | |
445 | port->running_status_length != 0 && | |
446 | buffer[i + 1] < 0x80) | |
447 | /* CIN 4 that is not a SysEx */ | |
448 | length = port->running_status_length; | |
449 | else | |
450 | /* | |
451 | * All other msgs cannot begin running status. | |
452 | * (A channel msg sent as two or three CIN 0xF | |
453 | * packets could in theory, but this device | |
454 | * doesn't use this format.) | |
455 | */ | |
456 | port->running_status_length = 0; | |
457 | snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length); | |
458 | } | |
459 | } | |
460 | ||
61870aed CL |
461 | /* |
462 | * CME protocol: like the standard protocol, but SysEx commands are sent as a | |
463 | * single USB packet preceded by a 0x0F byte. | |
464 | */ | |
465 | static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep, | |
466 | uint8_t *buffer, int buffer_length) | |
467 | { | |
468 | if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f) | |
469 | snd_usbmidi_standard_input(ep, buffer, buffer_length); | |
470 | else | |
471 | snd_usbmidi_input_data(ep, buffer[0] >> 4, | |
472 | &buffer[1], buffer_length - 1); | |
473 | } | |
474 | ||
1da177e4 LT |
475 | /* |
476 | * Adds one USB MIDI packet to the output buffer. | |
477 | */ | |
478 | static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0, | |
479 | uint8_t p1, uint8_t p2, uint8_t p3) | |
480 | { | |
481 | ||
482 | uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length; | |
483 | buf[0] = p0; | |
484 | buf[1] = p1; | |
485 | buf[2] = p2; | |
486 | buf[3] = p3; | |
487 | urb->transfer_buffer_length += 4; | |
488 | } | |
489 | ||
490 | /* | |
491 | * Adds one Midiman packet to the output buffer. | |
492 | */ | |
493 | static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0, | |
494 | uint8_t p1, uint8_t p2, uint8_t p3) | |
495 | { | |
496 | ||
497 | uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length; | |
498 | buf[0] = p1; | |
499 | buf[1] = p2; | |
500 | buf[2] = p3; | |
501 | buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f]; | |
502 | urb->transfer_buffer_length += 4; | |
503 | } | |
504 | ||
505 | /* | |
506 | * Converts MIDI commands to USB MIDI packets. | |
507 | */ | |
86e07d34 | 508 | static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port, |
1da177e4 LT |
509 | uint8_t b, struct urb* urb) |
510 | { | |
511 | uint8_t p0 = port->cable; | |
512 | void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) = | |
513 | port->ep->umidi->usb_protocol_ops->output_packet; | |
514 | ||
515 | if (b >= 0xf8) { | |
516 | output_packet(urb, p0 | 0x0f, b, 0, 0); | |
517 | } else if (b >= 0xf0) { | |
518 | switch (b) { | |
519 | case 0xf0: | |
520 | port->data[0] = b; | |
521 | port->state = STATE_SYSEX_1; | |
522 | break; | |
523 | case 0xf1: | |
524 | case 0xf3: | |
525 | port->data[0] = b; | |
526 | port->state = STATE_1PARAM; | |
527 | break; | |
528 | case 0xf2: | |
529 | port->data[0] = b; | |
530 | port->state = STATE_2PARAM_1; | |
531 | break; | |
532 | case 0xf4: | |
533 | case 0xf5: | |
534 | port->state = STATE_UNKNOWN; | |
535 | break; | |
536 | case 0xf6: | |
537 | output_packet(urb, p0 | 0x05, 0xf6, 0, 0); | |
538 | port->state = STATE_UNKNOWN; | |
539 | break; | |
540 | case 0xf7: | |
541 | switch (port->state) { | |
542 | case STATE_SYSEX_0: | |
543 | output_packet(urb, p0 | 0x05, 0xf7, 0, 0); | |
544 | break; | |
545 | case STATE_SYSEX_1: | |
546 | output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0); | |
547 | break; | |
548 | case STATE_SYSEX_2: | |
549 | output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7); | |
550 | break; | |
551 | } | |
552 | port->state = STATE_UNKNOWN; | |
553 | break; | |
554 | } | |
555 | } else if (b >= 0x80) { | |
556 | port->data[0] = b; | |
557 | if (b >= 0xc0 && b <= 0xdf) | |
558 | port->state = STATE_1PARAM; | |
559 | else | |
560 | port->state = STATE_2PARAM_1; | |
561 | } else { /* b < 0x80 */ | |
562 | switch (port->state) { | |
563 | case STATE_1PARAM: | |
564 | if (port->data[0] < 0xf0) { | |
565 | p0 |= port->data[0] >> 4; | |
566 | } else { | |
567 | p0 |= 0x02; | |
568 | port->state = STATE_UNKNOWN; | |
569 | } | |
570 | output_packet(urb, p0, port->data[0], b, 0); | |
571 | break; | |
572 | case STATE_2PARAM_1: | |
573 | port->data[1] = b; | |
574 | port->state = STATE_2PARAM_2; | |
575 | break; | |
576 | case STATE_2PARAM_2: | |
577 | if (port->data[0] < 0xf0) { | |
578 | p0 |= port->data[0] >> 4; | |
579 | port->state = STATE_2PARAM_1; | |
580 | } else { | |
581 | p0 |= 0x03; | |
582 | port->state = STATE_UNKNOWN; | |
583 | } | |
584 | output_packet(urb, p0, port->data[0], port->data[1], b); | |
585 | break; | |
586 | case STATE_SYSEX_0: | |
587 | port->data[0] = b; | |
588 | port->state = STATE_SYSEX_1; | |
589 | break; | |
590 | case STATE_SYSEX_1: | |
591 | port->data[1] = b; | |
592 | port->state = STATE_SYSEX_2; | |
593 | break; | |
594 | case STATE_SYSEX_2: | |
595 | output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b); | |
596 | port->state = STATE_SYSEX_0; | |
597 | break; | |
598 | } | |
599 | } | |
600 | } | |
601 | ||
ed4affa5 CL |
602 | static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep, |
603 | struct urb *urb) | |
1da177e4 | 604 | { |
1da177e4 LT |
605 | int p; |
606 | ||
607 | /* FIXME: lower-numbered ports can starve higher-numbered ports */ | |
608 | for (p = 0; p < 0x10; ++p) { | |
86e07d34 | 609 | struct usbmidi_out_port* port = &ep->ports[p]; |
1da177e4 LT |
610 | if (!port->active) |
611 | continue; | |
612 | while (urb->transfer_buffer_length + 3 < ep->max_transfer) { | |
613 | uint8_t b; | |
614 | if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) { | |
615 | port->active = 0; | |
616 | break; | |
617 | } | |
618 | snd_usbmidi_transmit_byte(port, b, urb); | |
619 | } | |
620 | } | |
621 | } | |
622 | ||
623 | static struct usb_protocol_ops snd_usbmidi_standard_ops = { | |
624 | .input = snd_usbmidi_standard_input, | |
625 | .output = snd_usbmidi_standard_output, | |
626 | .output_packet = snd_usbmidi_output_standard_packet, | |
627 | }; | |
628 | ||
629 | static struct usb_protocol_ops snd_usbmidi_midiman_ops = { | |
630 | .input = snd_usbmidi_midiman_input, | |
631 | .output = snd_usbmidi_standard_output, | |
632 | .output_packet = snd_usbmidi_output_midiman_packet, | |
633 | }; | |
634 | ||
d05cc104 CL |
635 | static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = { |
636 | .input = snd_usbmidi_maudio_broken_running_status_input, | |
637 | .output = snd_usbmidi_standard_output, | |
638 | .output_packet = snd_usbmidi_output_standard_packet, | |
639 | }; | |
640 | ||
61870aed CL |
641 | static struct usb_protocol_ops snd_usbmidi_cme_ops = { |
642 | .input = snd_usbmidi_cme_input, | |
643 | .output = snd_usbmidi_standard_output, | |
644 | .output_packet = snd_usbmidi_output_standard_packet, | |
645 | }; | |
646 | ||
1da177e4 LT |
647 | /* |
648 | * Novation USB MIDI protocol: number of data bytes is in the first byte | |
649 | * (when receiving) (+1!) or in the second byte (when sending); data begins | |
650 | * at the third byte. | |
651 | */ | |
652 | ||
86e07d34 | 653 | static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep, |
1da177e4 LT |
654 | uint8_t* buffer, int buffer_length) |
655 | { | |
656 | if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1) | |
657 | return; | |
658 | snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1); | |
659 | } | |
660 | ||
ed4affa5 CL |
661 | static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep, |
662 | struct urb *urb) | |
1da177e4 LT |
663 | { |
664 | uint8_t* transfer_buffer; | |
665 | int count; | |
666 | ||
667 | if (!ep->ports[0].active) | |
668 | return; | |
ed4affa5 | 669 | transfer_buffer = urb->transfer_buffer; |
1da177e4 LT |
670 | count = snd_rawmidi_transmit(ep->ports[0].substream, |
671 | &transfer_buffer[2], | |
672 | ep->max_transfer - 2); | |
673 | if (count < 1) { | |
674 | ep->ports[0].active = 0; | |
675 | return; | |
676 | } | |
677 | transfer_buffer[0] = 0; | |
678 | transfer_buffer[1] = count; | |
ed4affa5 | 679 | urb->transfer_buffer_length = 2 + count; |
1da177e4 LT |
680 | } |
681 | ||
682 | static struct usb_protocol_ops snd_usbmidi_novation_ops = { | |
683 | .input = snd_usbmidi_novation_input, | |
684 | .output = snd_usbmidi_novation_output, | |
685 | }; | |
686 | ||
687 | /* | |
6155aff8 | 688 | * "raw" protocol: used by the MOTU FastLane. |
1da177e4 LT |
689 | */ |
690 | ||
86e07d34 | 691 | static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep, |
6155aff8 | 692 | uint8_t* buffer, int buffer_length) |
1da177e4 LT |
693 | { |
694 | snd_usbmidi_input_data(ep, 0, buffer, buffer_length); | |
695 | } | |
696 | ||
ed4affa5 CL |
697 | static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep, |
698 | struct urb *urb) | |
1da177e4 LT |
699 | { |
700 | int count; | |
701 | ||
702 | if (!ep->ports[0].active) | |
703 | return; | |
704 | count = snd_rawmidi_transmit(ep->ports[0].substream, | |
ed4affa5 | 705 | urb->transfer_buffer, |
1da177e4 LT |
706 | ep->max_transfer); |
707 | if (count < 1) { | |
708 | ep->ports[0].active = 0; | |
709 | return; | |
710 | } | |
ed4affa5 | 711 | urb->transfer_buffer_length = count; |
1da177e4 LT |
712 | } |
713 | ||
6155aff8 CL |
714 | static struct usb_protocol_ops snd_usbmidi_raw_ops = { |
715 | .input = snd_usbmidi_raw_input, | |
716 | .output = snd_usbmidi_raw_output, | |
1da177e4 LT |
717 | }; |
718 | ||
030a07e4 KW |
719 | static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep, |
720 | uint8_t *buffer, int buffer_length) | |
721 | { | |
722 | if (buffer_length != 9) | |
723 | return; | |
724 | buffer_length = 8; | |
725 | while (buffer_length && buffer[buffer_length - 1] == 0xFD) | |
726 | buffer_length--; | |
727 | if (buffer_length) | |
728 | snd_usbmidi_input_data(ep, 0, buffer, buffer_length); | |
729 | } | |
730 | ||
ed4affa5 CL |
731 | static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep, |
732 | struct urb *urb) | |
030a07e4 KW |
733 | { |
734 | int count; | |
735 | ||
736 | if (!ep->ports[0].active) | |
737 | return; | |
d82af9f9 | 738 | count = snd_usb_get_speed(ep->umidi->dev) == USB_SPEED_HIGH ? 1 : 2; |
030a07e4 | 739 | count = snd_rawmidi_transmit(ep->ports[0].substream, |
ed4affa5 | 740 | urb->transfer_buffer, |
030a07e4 KW |
741 | count); |
742 | if (count < 1) { | |
743 | ep->ports[0].active = 0; | |
744 | return; | |
745 | } | |
746 | ||
ed4affa5 CL |
747 | memset(urb->transfer_buffer + count, 0xFD, 9 - count); |
748 | urb->transfer_buffer_length = count; | |
030a07e4 KW |
749 | } |
750 | ||
751 | static struct usb_protocol_ops snd_usbmidi_122l_ops = { | |
752 | .input = snd_usbmidi_us122l_input, | |
753 | .output = snd_usbmidi_us122l_output, | |
754 | }; | |
755 | ||
1da177e4 LT |
756 | /* |
757 | * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching. | |
758 | */ | |
759 | ||
86e07d34 | 760 | static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep) |
1da177e4 LT |
761 | { |
762 | static const u8 init_data[] = { | |
763 | /* initialization magic: "get version" */ | |
764 | 0xf0, | |
765 | 0x00, 0x20, 0x31, /* Emagic */ | |
766 | 0x64, /* Unitor8 */ | |
767 | 0x0b, /* version number request */ | |
768 | 0x00, /* command version */ | |
769 | 0x00, /* EEPROM, box 0 */ | |
770 | 0xf7 | |
771 | }; | |
772 | send_bulk_static_data(ep, init_data, sizeof(init_data)); | |
773 | /* while we're at it, pour on more magic */ | |
774 | send_bulk_static_data(ep, init_data, sizeof(init_data)); | |
775 | } | |
776 | ||
86e07d34 | 777 | static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep) |
1da177e4 LT |
778 | { |
779 | static const u8 finish_data[] = { | |
780 | /* switch to patch mode with last preset */ | |
781 | 0xf0, | |
782 | 0x00, 0x20, 0x31, /* Emagic */ | |
783 | 0x64, /* Unitor8 */ | |
784 | 0x10, /* patch switch command */ | |
785 | 0x00, /* command version */ | |
786 | 0x7f, /* to all boxes */ | |
787 | 0x40, /* last preset in EEPROM */ | |
788 | 0xf7 | |
789 | }; | |
790 | send_bulk_static_data(ep, finish_data, sizeof(finish_data)); | |
791 | } | |
792 | ||
86e07d34 | 793 | static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep, |
1da177e4 LT |
794 | uint8_t* buffer, int buffer_length) |
795 | { | |
c347e9fc CL |
796 | int i; |
797 | ||
798 | /* FF indicates end of valid data */ | |
799 | for (i = 0; i < buffer_length; ++i) | |
800 | if (buffer[i] == 0xff) { | |
801 | buffer_length = i; | |
802 | break; | |
803 | } | |
1da177e4 LT |
804 | |
805 | /* handle F5 at end of last buffer */ | |
806 | if (ep->seen_f5) | |
807 | goto switch_port; | |
808 | ||
809 | while (buffer_length > 0) { | |
1da177e4 LT |
810 | /* determine size of data until next F5 */ |
811 | for (i = 0; i < buffer_length; ++i) | |
812 | if (buffer[i] == 0xf5) | |
813 | break; | |
814 | snd_usbmidi_input_data(ep, ep->current_port, buffer, i); | |
815 | buffer += i; | |
816 | buffer_length -= i; | |
817 | ||
818 | if (buffer_length <= 0) | |
819 | break; | |
820 | /* assert(buffer[0] == 0xf5); */ | |
821 | ep->seen_f5 = 1; | |
822 | ++buffer; | |
823 | --buffer_length; | |
824 | ||
825 | switch_port: | |
826 | if (buffer_length <= 0) | |
827 | break; | |
828 | if (buffer[0] < 0x80) { | |
829 | ep->current_port = (buffer[0] - 1) & 15; | |
830 | ++buffer; | |
831 | --buffer_length; | |
832 | } | |
833 | ep->seen_f5 = 0; | |
834 | } | |
835 | } | |
836 | ||
ed4affa5 CL |
837 | static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep, |
838 | struct urb *urb) | |
1da177e4 LT |
839 | { |
840 | int port0 = ep->current_port; | |
ed4affa5 | 841 | uint8_t* buf = urb->transfer_buffer; |
1da177e4 LT |
842 | int buf_free = ep->max_transfer; |
843 | int length, i; | |
844 | ||
845 | for (i = 0; i < 0x10; ++i) { | |
846 | /* round-robin, starting at the last current port */ | |
847 | int portnum = (port0 + i) & 15; | |
86e07d34 | 848 | struct usbmidi_out_port* port = &ep->ports[portnum]; |
1da177e4 LT |
849 | |
850 | if (!port->active) | |
851 | continue; | |
852 | if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) { | |
853 | port->active = 0; | |
854 | continue; | |
855 | } | |
856 | ||
857 | if (portnum != ep->current_port) { | |
858 | if (buf_free < 2) | |
859 | break; | |
860 | ep->current_port = portnum; | |
861 | buf[0] = 0xf5; | |
862 | buf[1] = (portnum + 1) & 15; | |
863 | buf += 2; | |
864 | buf_free -= 2; | |
865 | } | |
866 | ||
867 | if (buf_free < 1) | |
868 | break; | |
869 | length = snd_rawmidi_transmit(port->substream, buf, buf_free); | |
870 | if (length > 0) { | |
871 | buf += length; | |
872 | buf_free -= length; | |
873 | if (buf_free < 1) | |
874 | break; | |
875 | } | |
876 | } | |
c347e9fc CL |
877 | if (buf_free < ep->max_transfer && buf_free > 0) { |
878 | *buf = 0xff; | |
879 | --buf_free; | |
880 | } | |
ed4affa5 | 881 | urb->transfer_buffer_length = ep->max_transfer - buf_free; |
1da177e4 LT |
882 | } |
883 | ||
884 | static struct usb_protocol_ops snd_usbmidi_emagic_ops = { | |
885 | .input = snd_usbmidi_emagic_input, | |
886 | .output = snd_usbmidi_emagic_output, | |
887 | .init_out_endpoint = snd_usbmidi_emagic_init_out, | |
888 | .finish_out_endpoint = snd_usbmidi_emagic_finish_out, | |
889 | }; | |
890 | ||
891 | ||
96f61d9a CL |
892 | static void update_roland_altsetting(struct snd_usb_midi* umidi) |
893 | { | |
894 | struct usb_interface *intf; | |
895 | struct usb_host_interface *hostif; | |
896 | struct usb_interface_descriptor *intfd; | |
897 | int is_light_load; | |
898 | ||
899 | intf = umidi->iface; | |
900 | is_light_load = intf->cur_altsetting != intf->altsetting; | |
901 | if (umidi->roland_load_ctl->private_value == is_light_load) | |
902 | return; | |
903 | hostif = &intf->altsetting[umidi->roland_load_ctl->private_value]; | |
904 | intfd = get_iface_desc(hostif); | |
905 | snd_usbmidi_input_stop(&umidi->list); | |
d82af9f9 | 906 | usb_set_interface(umidi->dev, intfd->bInterfaceNumber, |
96f61d9a CL |
907 | intfd->bAlternateSetting); |
908 | snd_usbmidi_input_start(&umidi->list); | |
909 | } | |
910 | ||
911 | static void substream_open(struct snd_rawmidi_substream *substream, int open) | |
912 | { | |
913 | struct snd_usb_midi* umidi = substream->rmidi->private_data; | |
914 | struct snd_kcontrol *ctl; | |
915 | ||
916 | mutex_lock(&umidi->mutex); | |
917 | if (open) { | |
918 | if (umidi->opened++ == 0 && umidi->roland_load_ctl) { | |
919 | ctl = umidi->roland_load_ctl; | |
920 | ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
d82af9f9 | 921 | snd_ctl_notify(umidi->card, |
96f61d9a CL |
922 | SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); |
923 | update_roland_altsetting(umidi); | |
924 | } | |
925 | } else { | |
926 | if (--umidi->opened == 0 && umidi->roland_load_ctl) { | |
927 | ctl = umidi->roland_load_ctl; | |
928 | ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
d82af9f9 | 929 | snd_ctl_notify(umidi->card, |
96f61d9a CL |
930 | SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); |
931 | } | |
932 | } | |
933 | mutex_unlock(&umidi->mutex); | |
934 | } | |
935 | ||
86e07d34 | 936 | static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream) |
1da177e4 | 937 | { |
86e07d34 TI |
938 | struct snd_usb_midi* umidi = substream->rmidi->private_data; |
939 | struct usbmidi_out_port* port = NULL; | |
1da177e4 LT |
940 | int i, j; |
941 | ||
942 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) | |
943 | if (umidi->endpoints[i].out) | |
944 | for (j = 0; j < 0x10; ++j) | |
945 | if (umidi->endpoints[i].out->ports[j].substream == substream) { | |
946 | port = &umidi->endpoints[i].out->ports[j]; | |
947 | break; | |
948 | } | |
949 | if (!port) { | |
950 | snd_BUG(); | |
951 | return -ENXIO; | |
952 | } | |
953 | substream->runtime->private_data = port; | |
954 | port->state = STATE_UNKNOWN; | |
96f61d9a | 955 | substream_open(substream, 1); |
1da177e4 LT |
956 | return 0; |
957 | } | |
958 | ||
86e07d34 | 959 | static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream) |
1da177e4 | 960 | { |
96f61d9a | 961 | substream_open(substream, 0); |
1da177e4 LT |
962 | return 0; |
963 | } | |
964 | ||
86e07d34 | 965 | static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up) |
1da177e4 | 966 | { |
86e07d34 | 967 | struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data; |
1da177e4 LT |
968 | |
969 | port->active = up; | |
970 | if (up) { | |
d82af9f9 | 971 | if (port->ep->umidi->disconnected) { |
1da177e4 LT |
972 | /* gobble up remaining bytes to prevent wait in |
973 | * snd_rawmidi_drain_output */ | |
974 | while (!snd_rawmidi_transmit_empty(substream)) | |
975 | snd_rawmidi_transmit_ack(substream, 1); | |
976 | return; | |
977 | } | |
1f04128a | 978 | tasklet_schedule(&port->ep->tasklet); |
1da177e4 LT |
979 | } |
980 | } | |
981 | ||
a65dd997 CL |
982 | static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream) |
983 | { | |
984 | struct usbmidi_out_port* port = substream->runtime->private_data; | |
985 | struct snd_usb_midi_out_endpoint *ep = port->ep; | |
986 | unsigned int drain_urbs; | |
987 | DEFINE_WAIT(wait); | |
988 | long timeout = msecs_to_jiffies(50); | |
989 | ||
990 | /* | |
991 | * The substream buffer is empty, but some data might still be in the | |
992 | * currently active URBs, so we have to wait for those to complete. | |
993 | */ | |
994 | spin_lock_irq(&ep->buffer_lock); | |
995 | drain_urbs = ep->active_urbs; | |
996 | if (drain_urbs) { | |
997 | ep->drain_urbs |= drain_urbs; | |
998 | do { | |
999 | prepare_to_wait(&ep->drain_wait, &wait, | |
1000 | TASK_UNINTERRUPTIBLE); | |
1001 | spin_unlock_irq(&ep->buffer_lock); | |
1002 | timeout = schedule_timeout(timeout); | |
1003 | spin_lock_irq(&ep->buffer_lock); | |
1004 | drain_urbs &= ep->drain_urbs; | |
1005 | } while (drain_urbs && timeout); | |
1006 | finish_wait(&ep->drain_wait, &wait); | |
1007 | } | |
1008 | spin_unlock_irq(&ep->buffer_lock); | |
1009 | } | |
1010 | ||
86e07d34 | 1011 | static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream) |
1da177e4 | 1012 | { |
96f61d9a | 1013 | substream_open(substream, 1); |
1da177e4 LT |
1014 | return 0; |
1015 | } | |
1016 | ||
86e07d34 | 1017 | static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream) |
1da177e4 | 1018 | { |
96f61d9a | 1019 | substream_open(substream, 0); |
1da177e4 LT |
1020 | return 0; |
1021 | } | |
1022 | ||
86e07d34 | 1023 | static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) |
1da177e4 | 1024 | { |
86e07d34 | 1025 | struct snd_usb_midi* umidi = substream->rmidi->private_data; |
1da177e4 LT |
1026 | |
1027 | if (up) | |
1028 | set_bit(substream->number, &umidi->input_triggered); | |
1029 | else | |
1030 | clear_bit(substream->number, &umidi->input_triggered); | |
1031 | } | |
1032 | ||
86e07d34 | 1033 | static struct snd_rawmidi_ops snd_usbmidi_output_ops = { |
1da177e4 LT |
1034 | .open = snd_usbmidi_output_open, |
1035 | .close = snd_usbmidi_output_close, | |
1036 | .trigger = snd_usbmidi_output_trigger, | |
a65dd997 | 1037 | .drain = snd_usbmidi_output_drain, |
1da177e4 LT |
1038 | }; |
1039 | ||
86e07d34 | 1040 | static struct snd_rawmidi_ops snd_usbmidi_input_ops = { |
1da177e4 LT |
1041 | .open = snd_usbmidi_input_open, |
1042 | .close = snd_usbmidi_input_close, | |
1043 | .trigger = snd_usbmidi_input_trigger | |
1044 | }; | |
1045 | ||
ed4affa5 CL |
1046 | static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb, |
1047 | unsigned int buffer_length) | |
1048 | { | |
d82af9f9 | 1049 | usb_buffer_free(umidi->dev, buffer_length, |
ed4affa5 CL |
1050 | urb->transfer_buffer, urb->transfer_dma); |
1051 | usb_free_urb(urb); | |
1052 | } | |
1053 | ||
1da177e4 LT |
1054 | /* |
1055 | * Frees an input endpoint. | |
1056 | * May be called when ep hasn't been initialized completely. | |
1057 | */ | |
86e07d34 | 1058 | static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep) |
1da177e4 | 1059 | { |
4773d1fb CL |
1060 | unsigned int i; |
1061 | ||
ed4affa5 CL |
1062 | for (i = 0; i < INPUT_URBS; ++i) |
1063 | if (ep->urbs[i]) | |
1064 | free_urb_and_buffer(ep->umidi, ep->urbs[i], | |
1065 | ep->urbs[i]->transfer_buffer_length); | |
1da177e4 LT |
1066 | kfree(ep); |
1067 | } | |
1068 | ||
1069 | /* | |
1070 | * Creates an input endpoint. | |
1071 | */ | |
86e07d34 TI |
1072 | static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi, |
1073 | struct snd_usb_midi_endpoint_info* ep_info, | |
1074 | struct snd_usb_midi_endpoint* rep) | |
1da177e4 | 1075 | { |
86e07d34 | 1076 | struct snd_usb_midi_in_endpoint* ep; |
1da177e4 LT |
1077 | void* buffer; |
1078 | unsigned int pipe; | |
1079 | int length; | |
4773d1fb | 1080 | unsigned int i; |
1da177e4 LT |
1081 | |
1082 | rep->in = NULL; | |
561b220a | 1083 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
1da177e4 LT |
1084 | if (!ep) |
1085 | return -ENOMEM; | |
1086 | ep->umidi = umidi; | |
1087 | ||
4773d1fb CL |
1088 | for (i = 0; i < INPUT_URBS; ++i) { |
1089 | ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL); | |
1090 | if (!ep->urbs[i]) { | |
1091 | snd_usbmidi_in_endpoint_delete(ep); | |
1092 | return -ENOMEM; | |
1093 | } | |
1da177e4 LT |
1094 | } |
1095 | if (ep_info->in_interval) | |
d82af9f9 | 1096 | pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep); |
1da177e4 | 1097 | else |
d82af9f9 CL |
1098 | pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep); |
1099 | length = usb_maxpacket(umidi->dev, pipe, 0); | |
4773d1fb | 1100 | for (i = 0; i < INPUT_URBS; ++i) { |
d82af9f9 | 1101 | buffer = usb_buffer_alloc(umidi->dev, length, GFP_KERNEL, |
4773d1fb CL |
1102 | &ep->urbs[i]->transfer_dma); |
1103 | if (!buffer) { | |
1104 | snd_usbmidi_in_endpoint_delete(ep); | |
1105 | return -ENOMEM; | |
1106 | } | |
1107 | if (ep_info->in_interval) | |
d82af9f9 | 1108 | usb_fill_int_urb(ep->urbs[i], umidi->dev, |
4773d1fb CL |
1109 | pipe, buffer, length, |
1110 | snd_usbmidi_in_urb_complete, | |
1111 | ep, ep_info->in_interval); | |
1112 | else | |
d82af9f9 | 1113 | usb_fill_bulk_urb(ep->urbs[i], umidi->dev, |
4773d1fb CL |
1114 | pipe, buffer, length, |
1115 | snd_usbmidi_in_urb_complete, ep); | |
1116 | ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | |
1da177e4 | 1117 | } |
1da177e4 LT |
1118 | |
1119 | rep->in = ep; | |
1120 | return 0; | |
1121 | } | |
1122 | ||
1da177e4 LT |
1123 | /* |
1124 | * Frees an output endpoint. | |
1125 | * May be called when ep hasn't been initialized completely. | |
1126 | */ | |
86e07d34 | 1127 | static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep) |
1da177e4 | 1128 | { |
ed4affa5 CL |
1129 | unsigned int i; |
1130 | ||
1131 | for (i = 0; i < OUTPUT_URBS; ++i) | |
1132 | if (ep->urbs[i].urb) | |
1133 | free_urb_and_buffer(ep->umidi, ep->urbs[i].urb, | |
1134 | ep->max_transfer); | |
1da177e4 LT |
1135 | kfree(ep); |
1136 | } | |
1137 | ||
1138 | /* | |
1139 | * Creates an output endpoint, and initializes output ports. | |
1140 | */ | |
86e07d34 TI |
1141 | static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi, |
1142 | struct snd_usb_midi_endpoint_info* ep_info, | |
1143 | struct snd_usb_midi_endpoint* rep) | |
1da177e4 | 1144 | { |
86e07d34 | 1145 | struct snd_usb_midi_out_endpoint* ep; |
ed4affa5 | 1146 | unsigned int i; |
1da177e4 LT |
1147 | unsigned int pipe; |
1148 | void* buffer; | |
1149 | ||
1150 | rep->out = NULL; | |
561b220a | 1151 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
1da177e4 LT |
1152 | if (!ep) |
1153 | return -ENOMEM; | |
1154 | ep->umidi = umidi; | |
1155 | ||
ed4affa5 CL |
1156 | for (i = 0; i < OUTPUT_URBS; ++i) { |
1157 | ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL); | |
1158 | if (!ep->urbs[i].urb) { | |
1159 | snd_usbmidi_out_endpoint_delete(ep); | |
1160 | return -ENOMEM; | |
1161 | } | |
1162 | ep->urbs[i].ep = ep; | |
1da177e4 | 1163 | } |
a6a712ae | 1164 | if (ep_info->out_interval) |
d82af9f9 | 1165 | pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep); |
a6a712ae | 1166 | else |
d82af9f9 | 1167 | pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep); |
f167e1d0 CL |
1168 | switch (umidi->usb_id) { |
1169 | default: | |
d82af9f9 | 1170 | ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1); |
f167e1d0 CL |
1171 | break; |
1172 | /* | |
1173 | * Various chips declare a packet size larger than 4 bytes, but | |
1174 | * do not actually work with larger packets: | |
1175 | */ | |
1176 | case USB_ID(0x0a92, 0x1020): /* ESI M4U */ | |
1177 | case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */ | |
1178 | case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */ | |
1179 | case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */ | |
1180 | case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */ | |
1181 | ep->max_transfer = 4; | |
1182 | break; | |
1183 | } | |
ed4affa5 | 1184 | for (i = 0; i < OUTPUT_URBS; ++i) { |
d82af9f9 | 1185 | buffer = usb_buffer_alloc(umidi->dev, |
ed4affa5 CL |
1186 | ep->max_transfer, GFP_KERNEL, |
1187 | &ep->urbs[i].urb->transfer_dma); | |
1188 | if (!buffer) { | |
1189 | snd_usbmidi_out_endpoint_delete(ep); | |
1190 | return -ENOMEM; | |
1191 | } | |
1192 | if (ep_info->out_interval) | |
d82af9f9 | 1193 | usb_fill_int_urb(ep->urbs[i].urb, umidi->dev, |
ed4affa5 CL |
1194 | pipe, buffer, ep->max_transfer, |
1195 | snd_usbmidi_out_urb_complete, | |
1196 | &ep->urbs[i], ep_info->out_interval); | |
1197 | else | |
d82af9f9 | 1198 | usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev, |
ed4affa5 CL |
1199 | pipe, buffer, ep->max_transfer, |
1200 | snd_usbmidi_out_urb_complete, | |
1201 | &ep->urbs[i]); | |
1202 | ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | |
1da177e4 | 1203 | } |
1da177e4 LT |
1204 | |
1205 | spin_lock_init(&ep->buffer_lock); | |
1206 | tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep); | |
a65dd997 | 1207 | init_waitqueue_head(&ep->drain_wait); |
1da177e4 LT |
1208 | |
1209 | for (i = 0; i < 0x10; ++i) | |
1210 | if (ep_info->out_cables & (1 << i)) { | |
1211 | ep->ports[i].ep = ep; | |
1212 | ep->ports[i].cable = i << 4; | |
1213 | } | |
1214 | ||
1215 | if (umidi->usb_protocol_ops->init_out_endpoint) | |
1216 | umidi->usb_protocol_ops->init_out_endpoint(ep); | |
1217 | ||
1218 | rep->out = ep; | |
1219 | return 0; | |
1220 | } | |
1221 | ||
1222 | /* | |
1223 | * Frees everything. | |
1224 | */ | |
86e07d34 | 1225 | static void snd_usbmidi_free(struct snd_usb_midi* umidi) |
1da177e4 LT |
1226 | { |
1227 | int i; | |
1228 | ||
1229 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
86e07d34 | 1230 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
1da177e4 LT |
1231 | if (ep->out) |
1232 | snd_usbmidi_out_endpoint_delete(ep->out); | |
1233 | if (ep->in) | |
1234 | snd_usbmidi_in_endpoint_delete(ep->in); | |
1235 | } | |
96f61d9a | 1236 | mutex_destroy(&umidi->mutex); |
1da177e4 LT |
1237 | kfree(umidi); |
1238 | } | |
1239 | ||
1240 | /* | |
1241 | * Unlinks all URBs (must be done before the usb_device is deleted). | |
1242 | */ | |
ee733397 | 1243 | void snd_usbmidi_disconnect(struct list_head* p) |
1da177e4 | 1244 | { |
86e07d34 | 1245 | struct snd_usb_midi* umidi; |
4773d1fb | 1246 | unsigned int i, j; |
1da177e4 | 1247 | |
86e07d34 | 1248 | umidi = list_entry(p, struct snd_usb_midi, list); |
c0792e00 TI |
1249 | /* |
1250 | * an URB's completion handler may start the timer and | |
1251 | * a timer may submit an URB. To reliably break the cycle | |
1252 | * a flag under lock must be used | |
1253 | */ | |
1254 | spin_lock_irq(&umidi->disc_lock); | |
1255 | umidi->disconnected = 1; | |
1256 | spin_unlock_irq(&umidi->disc_lock); | |
1da177e4 | 1257 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
86e07d34 | 1258 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
c8846970 CL |
1259 | if (ep->out) |
1260 | tasklet_kill(&ep->out->tasklet); | |
ed4affa5 CL |
1261 | if (ep->out) { |
1262 | for (j = 0; j < OUTPUT_URBS; ++j) | |
1263 | usb_kill_urb(ep->out->urbs[j].urb); | |
1da177e4 LT |
1264 | if (umidi->usb_protocol_ops->finish_out_endpoint) |
1265 | umidi->usb_protocol_ops->finish_out_endpoint(ep->out); | |
1266 | } | |
f5e135af | 1267 | if (ep->in) |
4773d1fb CL |
1268 | for (j = 0; j < INPUT_URBS; ++j) |
1269 | usb_kill_urb(ep->in->urbs[j]); | |
7a17daae TI |
1270 | /* free endpoints here; later call can result in Oops */ |
1271 | if (ep->out) { | |
1272 | snd_usbmidi_out_endpoint_delete(ep->out); | |
1273 | ep->out = NULL; | |
1274 | } | |
1275 | if (ep->in) { | |
1276 | snd_usbmidi_in_endpoint_delete(ep->in); | |
1277 | ep->in = NULL; | |
1278 | } | |
1da177e4 | 1279 | } |
c0792e00 | 1280 | del_timer_sync(&umidi->error_timer); |
1da177e4 LT |
1281 | } |
1282 | ||
86e07d34 | 1283 | static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi) |
1da177e4 | 1284 | { |
86e07d34 | 1285 | struct snd_usb_midi* umidi = rmidi->private_data; |
1da177e4 LT |
1286 | snd_usbmidi_free(umidi); |
1287 | } | |
1288 | ||
86e07d34 | 1289 | static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi, |
1da177e4 LT |
1290 | int stream, int number) |
1291 | { | |
1292 | struct list_head* list; | |
1293 | ||
1294 | list_for_each(list, &umidi->rmidi->streams[stream].substreams) { | |
86e07d34 | 1295 | struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list); |
1da177e4 LT |
1296 | if (substream->number == number) |
1297 | return substream; | |
1298 | } | |
1299 | return NULL; | |
1300 | } | |
1301 | ||
1302 | /* | |
1303 | * This list specifies names for ports that do not fit into the standard | |
1304 | * "(product) MIDI (n)" schema because they aren't external MIDI ports, | |
1305 | * such as internal control or synthesizer ports. | |
1306 | */ | |
a7b928ac | 1307 | static struct port_info { |
27d10f56 | 1308 | u32 id; |
a7b928ac CL |
1309 | short int port; |
1310 | short int voices; | |
1311 | const char *name; | |
1312 | unsigned int seq_flags; | |
1313 | } snd_usbmidi_port_info[] = { | |
1314 | #define PORT_INFO(vendor, product, num, name_, voices_, flags) \ | |
1315 | { .id = USB_ID(vendor, product), \ | |
1316 | .port = num, .voices = voices_, \ | |
1317 | .name = name_, .seq_flags = flags } | |
1318 | #define EXTERNAL_PORT(vendor, product, num, name) \ | |
1319 | PORT_INFO(vendor, product, num, name, 0, \ | |
1320 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ | |
1321 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ | |
1322 | SNDRV_SEQ_PORT_TYPE_PORT) | |
1323 | #define CONTROL_PORT(vendor, product, num, name) \ | |
1324 | PORT_INFO(vendor, product, num, name, 0, \ | |
1325 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ | |
1326 | SNDRV_SEQ_PORT_TYPE_HARDWARE) | |
1327 | #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \ | |
1328 | PORT_INFO(vendor, product, num, name, voices, \ | |
1329 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ | |
1330 | SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ | |
1331 | SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ | |
1332 | SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ | |
1333 | SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ | |
1334 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ | |
1335 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) | |
1336 | #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \ | |
1337 | PORT_INFO(vendor, product, num, name, voices, \ | |
1338 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ | |
1339 | SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ | |
1340 | SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ | |
1341 | SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ | |
1342 | SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ | |
1343 | SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \ | |
1344 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ | |
1345 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) | |
1da177e4 | 1346 | /* Roland UA-100 */ |
a7b928ac | 1347 | CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"), |
1da177e4 | 1348 | /* Roland SC-8850 */ |
a7b928ac CL |
1349 | SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128), |
1350 | SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128), | |
1351 | SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128), | |
1352 | SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128), | |
1353 | EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"), | |
1354 | EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"), | |
1da177e4 | 1355 | /* Roland U-8 */ |
a7b928ac CL |
1356 | EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"), |
1357 | CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"), | |
1da177e4 | 1358 | /* Roland SC-8820 */ |
a7b928ac CL |
1359 | SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64), |
1360 | SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64), | |
1361 | EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"), | |
1da177e4 | 1362 | /* Roland SK-500 */ |
a7b928ac CL |
1363 | SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64), |
1364 | SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64), | |
1365 | EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"), | |
1da177e4 | 1366 | /* Roland SC-D70 */ |
a7b928ac CL |
1367 | SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64), |
1368 | SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64), | |
1369 | EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"), | |
1da177e4 | 1370 | /* Edirol UM-880 */ |
a7b928ac | 1371 | CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"), |
1da177e4 | 1372 | /* Edirol SD-90 */ |
a7b928ac CL |
1373 | ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128), |
1374 | ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128), | |
1375 | EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"), | |
1376 | EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"), | |
1da177e4 | 1377 | /* Edirol UM-550 */ |
a7b928ac | 1378 | CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"), |
1da177e4 | 1379 | /* Edirol SD-20 */ |
a7b928ac CL |
1380 | ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64), |
1381 | ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64), | |
1382 | EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"), | |
1da177e4 | 1383 | /* Edirol SD-80 */ |
a7b928ac CL |
1384 | ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128), |
1385 | ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128), | |
1386 | EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"), | |
1387 | EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"), | |
1da177e4 | 1388 | /* Edirol UA-700 */ |
a7b928ac CL |
1389 | EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"), |
1390 | CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"), | |
1da177e4 | 1391 | /* Roland VariOS */ |
a7b928ac CL |
1392 | EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"), |
1393 | EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"), | |
1394 | EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"), | |
1da177e4 | 1395 | /* Edirol PCR */ |
a7b928ac CL |
1396 | EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"), |
1397 | EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"), | |
1398 | EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"), | |
1da177e4 | 1399 | /* BOSS GS-10 */ |
a7b928ac CL |
1400 | EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"), |
1401 | CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"), | |
1da177e4 | 1402 | /* Edirol UA-1000 */ |
a7b928ac CL |
1403 | EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"), |
1404 | CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"), | |
1da177e4 | 1405 | /* Edirol UR-80 */ |
a7b928ac CL |
1406 | EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"), |
1407 | EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"), | |
1408 | EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"), | |
1da177e4 | 1409 | /* Edirol PCR-A */ |
a7b928ac CL |
1410 | EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"), |
1411 | EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"), | |
1412 | EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"), | |
7c79b768 | 1413 | /* Edirol UM-3EX */ |
a7b928ac | 1414 | CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"), |
1da177e4 | 1415 | /* M-Audio MidiSport 8x8 */ |
a7b928ac CL |
1416 | CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"), |
1417 | CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"), | |
1da177e4 | 1418 | /* MOTU Fastlane */ |
a7b928ac CL |
1419 | EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"), |
1420 | EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"), | |
1da177e4 | 1421 | /* Emagic Unitor8/AMT8/MT4 */ |
a7b928ac CL |
1422 | EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"), |
1423 | EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"), | |
1424 | EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"), | |
d39e82db SA |
1425 | /* Access Music Virus TI */ |
1426 | EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"), | |
1427 | PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0, | |
1428 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | | |
1429 | SNDRV_SEQ_PORT_TYPE_HARDWARE | | |
1430 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER), | |
1da177e4 LT |
1431 | }; |
1432 | ||
a7b928ac CL |
1433 | static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number) |
1434 | { | |
1435 | int i; | |
1436 | ||
1437 | for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) { | |
d82af9f9 | 1438 | if (snd_usbmidi_port_info[i].id == umidi->usb_id && |
a7b928ac CL |
1439 | snd_usbmidi_port_info[i].port == number) |
1440 | return &snd_usbmidi_port_info[i]; | |
1441 | } | |
1442 | return NULL; | |
1443 | } | |
1444 | ||
1445 | static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number, | |
1446 | struct snd_seq_port_info *seq_port_info) | |
1447 | { | |
1448 | struct snd_usb_midi *umidi = rmidi->private_data; | |
1449 | struct port_info *port_info; | |
1450 | ||
1451 | /* TODO: read port flags from descriptors */ | |
1452 | port_info = find_port_info(umidi, number); | |
1453 | if (port_info) { | |
1454 | seq_port_info->type = port_info->seq_flags; | |
1455 | seq_port_info->midi_voices = port_info->voices; | |
1456 | } | |
1457 | } | |
1458 | ||
86e07d34 | 1459 | static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi, |
1da177e4 | 1460 | int stream, int number, |
86e07d34 | 1461 | struct snd_rawmidi_substream ** rsubstream) |
1da177e4 | 1462 | { |
a7b928ac | 1463 | struct port_info *port_info; |
1da177e4 LT |
1464 | const char *name_format; |
1465 | ||
86e07d34 | 1466 | struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number); |
1da177e4 LT |
1467 | if (!substream) { |
1468 | snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number); | |
1469 | return; | |
1470 | } | |
1471 | ||
1472 | /* TODO: read port name from jack descriptor */ | |
a7b928ac CL |
1473 | port_info = find_port_info(umidi, number); |
1474 | name_format = port_info ? port_info->name : "%s MIDI %d"; | |
1da177e4 | 1475 | snprintf(substream->name, sizeof(substream->name), |
d82af9f9 | 1476 | name_format, umidi->card->shortname, number + 1); |
1da177e4 LT |
1477 | |
1478 | *rsubstream = substream; | |
1479 | } | |
1480 | ||
1481 | /* | |
1482 | * Creates the endpoints and their ports. | |
1483 | */ | |
86e07d34 TI |
1484 | static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi, |
1485 | struct snd_usb_midi_endpoint_info* endpoints) | |
1da177e4 LT |
1486 | { |
1487 | int i, j, err; | |
1488 | int out_ports = 0, in_ports = 0; | |
1489 | ||
1490 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
1491 | if (endpoints[i].out_cables) { | |
1492 | err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i], | |
1493 | &umidi->endpoints[i]); | |
1494 | if (err < 0) | |
1495 | return err; | |
1496 | } | |
1497 | if (endpoints[i].in_cables) { | |
1498 | err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i], | |
1499 | &umidi->endpoints[i]); | |
1500 | if (err < 0) | |
1501 | return err; | |
1502 | } | |
1503 | ||
1504 | for (j = 0; j < 0x10; ++j) { | |
1505 | if (endpoints[i].out_cables & (1 << j)) { | |
1506 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports, | |
1507 | &umidi->endpoints[i].out->ports[j].substream); | |
1508 | ++out_ports; | |
1509 | } | |
1510 | if (endpoints[i].in_cables & (1 << j)) { | |
1511 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports, | |
1512 | &umidi->endpoints[i].in->ports[j].substream); | |
1513 | ++in_ports; | |
1514 | } | |
1515 | } | |
1516 | } | |
1517 | snd_printdd(KERN_INFO "created %d output and %d input ports\n", | |
1518 | out_ports, in_ports); | |
1519 | return 0; | |
1520 | } | |
1521 | ||
1522 | /* | |
1523 | * Returns MIDIStreaming device capabilities. | |
1524 | */ | |
86e07d34 TI |
1525 | static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi, |
1526 | struct snd_usb_midi_endpoint_info* endpoints) | |
1da177e4 LT |
1527 | { |
1528 | struct usb_interface* intf; | |
1529 | struct usb_host_interface *hostif; | |
1530 | struct usb_interface_descriptor* intfd; | |
1531 | struct usb_ms_header_descriptor* ms_header; | |
1532 | struct usb_host_endpoint *hostep; | |
1533 | struct usb_endpoint_descriptor* ep; | |
1534 | struct usb_ms_endpoint_descriptor* ms_ep; | |
1535 | int i, epidx; | |
1536 | ||
1537 | intf = umidi->iface; | |
1538 | if (!intf) | |
1539 | return -ENXIO; | |
1540 | hostif = &intf->altsetting[0]; | |
1541 | intfd = get_iface_desc(hostif); | |
1542 | ms_header = (struct usb_ms_header_descriptor*)hostif->extra; | |
1543 | if (hostif->extralen >= 7 && | |
1544 | ms_header->bLength >= 7 && | |
1545 | ms_header->bDescriptorType == USB_DT_CS_INTERFACE && | |
de48c7bc | 1546 | ms_header->bDescriptorSubtype == UAC_HEADER) |
1da177e4 LT |
1547 | snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n", |
1548 | ms_header->bcdMSC[1], ms_header->bcdMSC[0]); | |
1549 | else | |
1550 | snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n"); | |
1551 | ||
1552 | epidx = 0; | |
1553 | for (i = 0; i < intfd->bNumEndpoints; ++i) { | |
1554 | hostep = &hostif->endpoint[i]; | |
1555 | ep = get_ep_desc(hostep); | |
913ae5a2 | 1556 | if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep)) |
1da177e4 LT |
1557 | continue; |
1558 | ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra; | |
1559 | if (hostep->extralen < 4 || | |
1560 | ms_ep->bLength < 4 || | |
1561 | ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT || | |
de48c7bc | 1562 | ms_ep->bDescriptorSubtype != UAC_MS_GENERAL) |
1da177e4 | 1563 | continue; |
42a6e66f | 1564 | if (usb_endpoint_dir_out(ep)) { |
1da177e4 LT |
1565 | if (endpoints[epidx].out_ep) { |
1566 | if (++epidx >= MIDI_MAX_ENDPOINTS) { | |
1567 | snd_printk(KERN_WARNING "too many endpoints\n"); | |
1568 | break; | |
1569 | } | |
1570 | } | |
42a6e66f JL |
1571 | endpoints[epidx].out_ep = usb_endpoint_num(ep); |
1572 | if (usb_endpoint_xfer_int(ep)) | |
1da177e4 | 1573 | endpoints[epidx].out_interval = ep->bInterval; |
d82af9f9 | 1574 | else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) |
56162aab CL |
1575 | /* |
1576 | * Low speed bulk transfers don't exist, so | |
1577 | * force interrupt transfers for devices like | |
1578 | * ESI MIDI Mate that try to use them anyway. | |
1579 | */ | |
1580 | endpoints[epidx].out_interval = 1; | |
1da177e4 LT |
1581 | endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; |
1582 | snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", | |
1583 | ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); | |
1584 | } else { | |
1585 | if (endpoints[epidx].in_ep) { | |
1586 | if (++epidx >= MIDI_MAX_ENDPOINTS) { | |
1587 | snd_printk(KERN_WARNING "too many endpoints\n"); | |
1588 | break; | |
1589 | } | |
1590 | } | |
42a6e66f JL |
1591 | endpoints[epidx].in_ep = usb_endpoint_num(ep); |
1592 | if (usb_endpoint_xfer_int(ep)) | |
1da177e4 | 1593 | endpoints[epidx].in_interval = ep->bInterval; |
d82af9f9 | 1594 | else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) |
56162aab | 1595 | endpoints[epidx].in_interval = 1; |
1da177e4 LT |
1596 | endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; |
1597 | snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", | |
1598 | ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); | |
1599 | } | |
1600 | } | |
1601 | return 0; | |
1602 | } | |
1603 | ||
96f61d9a CL |
1604 | static int roland_load_info(struct snd_kcontrol *kcontrol, |
1605 | struct snd_ctl_elem_info *info) | |
1606 | { | |
1607 | static const char *const names[] = { "High Load", "Light Load" }; | |
1608 | ||
1609 | info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1610 | info->count = 1; | |
1611 | info->value.enumerated.items = 2; | |
1612 | if (info->value.enumerated.item > 1) | |
1613 | info->value.enumerated.item = 1; | |
1614 | strcpy(info->value.enumerated.name, names[info->value.enumerated.item]); | |
1615 | return 0; | |
1616 | } | |
1617 | ||
1618 | static int roland_load_get(struct snd_kcontrol *kcontrol, | |
1619 | struct snd_ctl_elem_value *value) | |
1620 | { | |
1621 | value->value.enumerated.item[0] = kcontrol->private_value; | |
1622 | return 0; | |
1623 | } | |
1624 | ||
1625 | static int roland_load_put(struct snd_kcontrol *kcontrol, | |
1626 | struct snd_ctl_elem_value *value) | |
1627 | { | |
1628 | struct snd_usb_midi* umidi = kcontrol->private_data; | |
1629 | int changed; | |
1630 | ||
1631 | if (value->value.enumerated.item[0] > 1) | |
1632 | return -EINVAL; | |
1633 | mutex_lock(&umidi->mutex); | |
1634 | changed = value->value.enumerated.item[0] != kcontrol->private_value; | |
1635 | if (changed) | |
1636 | kcontrol->private_value = value->value.enumerated.item[0]; | |
1637 | mutex_unlock(&umidi->mutex); | |
1638 | return changed; | |
1639 | } | |
1640 | ||
1641 | static struct snd_kcontrol_new roland_load_ctl = { | |
1642 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1643 | .name = "MIDI Input Mode", | |
1644 | .info = roland_load_info, | |
1645 | .get = roland_load_get, | |
1646 | .put = roland_load_put, | |
1647 | .private_value = 1, | |
1648 | }; | |
1649 | ||
1da177e4 LT |
1650 | /* |
1651 | * On Roland devices, use the second alternate setting to be able to use | |
1652 | * the interrupt input endpoint. | |
1653 | */ | |
86e07d34 | 1654 | static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi) |
1da177e4 LT |
1655 | { |
1656 | struct usb_interface* intf; | |
1657 | struct usb_host_interface *hostif; | |
1658 | struct usb_interface_descriptor* intfd; | |
1659 | ||
1660 | intf = umidi->iface; | |
1661 | if (!intf || intf->num_altsetting != 2) | |
1662 | return; | |
1663 | ||
1664 | hostif = &intf->altsetting[1]; | |
1665 | intfd = get_iface_desc(hostif); | |
1666 | if (intfd->bNumEndpoints != 2 || | |
1667 | (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK || | |
1668 | (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) | |
1669 | return; | |
1670 | ||
1671 | snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n", | |
1672 | intfd->bAlternateSetting); | |
d82af9f9 | 1673 | usb_set_interface(umidi->dev, intfd->bInterfaceNumber, |
1da177e4 | 1674 | intfd->bAlternateSetting); |
96f61d9a CL |
1675 | |
1676 | umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi); | |
d82af9f9 | 1677 | if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0) |
96f61d9a | 1678 | umidi->roland_load_ctl = NULL; |
1da177e4 LT |
1679 | } |
1680 | ||
1681 | /* | |
1682 | * Try to find any usable endpoints in the interface. | |
1683 | */ | |
86e07d34 TI |
1684 | static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi, |
1685 | struct snd_usb_midi_endpoint_info* endpoint, | |
1da177e4 LT |
1686 | int max_endpoints) |
1687 | { | |
1688 | struct usb_interface* intf; | |
1689 | struct usb_host_interface *hostif; | |
1690 | struct usb_interface_descriptor* intfd; | |
1691 | struct usb_endpoint_descriptor* epd; | |
1692 | int i, out_eps = 0, in_eps = 0; | |
1693 | ||
d82af9f9 | 1694 | if (USB_ID_VENDOR(umidi->usb_id) == 0x0582) |
1da177e4 LT |
1695 | snd_usbmidi_switch_roland_altsetting(umidi); |
1696 | ||
c1ab5d59 CL |
1697 | if (endpoint[0].out_ep || endpoint[0].in_ep) |
1698 | return 0; | |
1699 | ||
1da177e4 LT |
1700 | intf = umidi->iface; |
1701 | if (!intf || intf->num_altsetting < 1) | |
1702 | return -ENOENT; | |
1703 | hostif = intf->cur_altsetting; | |
1704 | intfd = get_iface_desc(hostif); | |
1705 | ||
1706 | for (i = 0; i < intfd->bNumEndpoints; ++i) { | |
1707 | epd = get_endpoint(hostif, i); | |
913ae5a2 JL |
1708 | if (!usb_endpoint_xfer_bulk(epd) && |
1709 | !usb_endpoint_xfer_int(epd)) | |
1da177e4 LT |
1710 | continue; |
1711 | if (out_eps < max_endpoints && | |
42a6e66f JL |
1712 | usb_endpoint_dir_out(epd)) { |
1713 | endpoint[out_eps].out_ep = usb_endpoint_num(epd); | |
1714 | if (usb_endpoint_xfer_int(epd)) | |
1da177e4 LT |
1715 | endpoint[out_eps].out_interval = epd->bInterval; |
1716 | ++out_eps; | |
1717 | } | |
1718 | if (in_eps < max_endpoints && | |
42a6e66f JL |
1719 | usb_endpoint_dir_in(epd)) { |
1720 | endpoint[in_eps].in_ep = usb_endpoint_num(epd); | |
1721 | if (usb_endpoint_xfer_int(epd)) | |
1da177e4 LT |
1722 | endpoint[in_eps].in_interval = epd->bInterval; |
1723 | ++in_eps; | |
1724 | } | |
1725 | } | |
1726 | return (out_eps || in_eps) ? 0 : -ENOENT; | |
1727 | } | |
1728 | ||
1729 | /* | |
1730 | * Detects the endpoints for one-port-per-endpoint protocols. | |
1731 | */ | |
86e07d34 TI |
1732 | static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi, |
1733 | struct snd_usb_midi_endpoint_info* endpoints) | |
1da177e4 LT |
1734 | { |
1735 | int err, i; | |
1736 | ||
1737 | err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS); | |
1738 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
1739 | if (endpoints[i].out_ep) | |
1740 | endpoints[i].out_cables = 0x0001; | |
1741 | if (endpoints[i].in_ep) | |
1742 | endpoints[i].in_cables = 0x0001; | |
1743 | } | |
1744 | return err; | |
1745 | } | |
1746 | ||
1747 | /* | |
1748 | * Detects the endpoints and ports of Yamaha devices. | |
1749 | */ | |
86e07d34 TI |
1750 | static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi, |
1751 | struct snd_usb_midi_endpoint_info* endpoint) | |
1da177e4 LT |
1752 | { |
1753 | struct usb_interface* intf; | |
1754 | struct usb_host_interface *hostif; | |
1755 | struct usb_interface_descriptor* intfd; | |
1756 | uint8_t* cs_desc; | |
1757 | ||
1758 | intf = umidi->iface; | |
1759 | if (!intf) | |
1760 | return -ENOENT; | |
1761 | hostif = intf->altsetting; | |
1762 | intfd = get_iface_desc(hostif); | |
1763 | if (intfd->bNumEndpoints < 1) | |
1764 | return -ENOENT; | |
1765 | ||
1766 | /* | |
1767 | * For each port there is one MIDI_IN/OUT_JACK descriptor, not | |
1768 | * necessarily with any useful contents. So simply count 'em. | |
1769 | */ | |
1770 | for (cs_desc = hostif->extra; | |
1771 | cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; | |
1772 | cs_desc += cs_desc[0]) { | |
c4a87ef4 | 1773 | if (cs_desc[1] == USB_DT_CS_INTERFACE) { |
de48c7bc | 1774 | if (cs_desc[2] == UAC_MIDI_IN_JACK) |
1da177e4 | 1775 | endpoint->in_cables = (endpoint->in_cables << 1) | 1; |
de48c7bc | 1776 | else if (cs_desc[2] == UAC_MIDI_OUT_JACK) |
1da177e4 LT |
1777 | endpoint->out_cables = (endpoint->out_cables << 1) | 1; |
1778 | } | |
1779 | } | |
1780 | if (!endpoint->in_cables && !endpoint->out_cables) | |
1781 | return -ENOENT; | |
1782 | ||
1783 | return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); | |
1784 | } | |
1785 | ||
1786 | /* | |
1787 | * Creates the endpoints and their ports for Midiman devices. | |
1788 | */ | |
86e07d34 TI |
1789 | static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi, |
1790 | struct snd_usb_midi_endpoint_info* endpoint) | |
1da177e4 | 1791 | { |
86e07d34 | 1792 | struct snd_usb_midi_endpoint_info ep_info; |
1da177e4 LT |
1793 | struct usb_interface* intf; |
1794 | struct usb_host_interface *hostif; | |
1795 | struct usb_interface_descriptor* intfd; | |
1796 | struct usb_endpoint_descriptor* epd; | |
1797 | int cable, err; | |
1798 | ||
1799 | intf = umidi->iface; | |
1800 | if (!intf) | |
1801 | return -ENOENT; | |
1802 | hostif = intf->altsetting; | |
1803 | intfd = get_iface_desc(hostif); | |
1804 | /* | |
1805 | * The various MidiSport devices have more or less random endpoint | |
1806 | * numbers, so we have to identify the endpoints by their index in | |
1807 | * the descriptor array, like the driver for that other OS does. | |
1808 | * | |
1809 | * There is one interrupt input endpoint for all input ports, one | |
1810 | * bulk output endpoint for even-numbered ports, and one for odd- | |
1811 | * numbered ports. Both bulk output endpoints have corresponding | |
1812 | * input bulk endpoints (at indices 1 and 3) which aren't used. | |
1813 | */ | |
1814 | if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) { | |
1815 | snd_printdd(KERN_ERR "not enough endpoints\n"); | |
1816 | return -ENOENT; | |
1817 | } | |
1818 | ||
1819 | epd = get_endpoint(hostif, 0); | |
913ae5a2 | 1820 | if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) { |
1da177e4 LT |
1821 | snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n"); |
1822 | return -ENXIO; | |
1823 | } | |
1824 | epd = get_endpoint(hostif, 2); | |
913ae5a2 | 1825 | if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) { |
1da177e4 LT |
1826 | snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n"); |
1827 | return -ENXIO; | |
1828 | } | |
1829 | if (endpoint->out_cables > 0x0001) { | |
1830 | epd = get_endpoint(hostif, 4); | |
913ae5a2 JL |
1831 | if (!usb_endpoint_dir_out(epd) || |
1832 | !usb_endpoint_xfer_bulk(epd)) { | |
1da177e4 LT |
1833 | snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n"); |
1834 | return -ENXIO; | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
e156ac4c | 1839 | ep_info.out_interval = 0; |
1da177e4 LT |
1840 | ep_info.out_cables = endpoint->out_cables & 0x5555; |
1841 | err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); | |
1842 | if (err < 0) | |
1843 | return err; | |
1844 | ||
1845 | ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1846 | ep_info.in_interval = get_endpoint(hostif, 0)->bInterval; | |
1847 | ep_info.in_cables = endpoint->in_cables; | |
1848 | err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); | |
1849 | if (err < 0) | |
1850 | return err; | |
1851 | ||
1852 | if (endpoint->out_cables > 0x0001) { | |
1853 | ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1854 | ep_info.out_cables = endpoint->out_cables & 0xaaaa; | |
1855 | err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]); | |
1856 | if (err < 0) | |
1857 | return err; | |
1858 | } | |
1859 | ||
1860 | for (cable = 0; cable < 0x10; ++cable) { | |
1861 | if (endpoint->out_cables & (1 << cable)) | |
1862 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable, | |
1863 | &umidi->endpoints[cable & 1].out->ports[cable].substream); | |
1864 | if (endpoint->in_cables & (1 << cable)) | |
1865 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable, | |
1866 | &umidi->endpoints[0].in->ports[cable].substream); | |
1867 | } | |
1868 | return 0; | |
1869 | } | |
1870 | ||
a7b928ac CL |
1871 | static struct snd_rawmidi_global_ops snd_usbmidi_ops = { |
1872 | .get_port_info = snd_usbmidi_get_port_info, | |
1873 | }; | |
1874 | ||
86e07d34 | 1875 | static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi, |
1da177e4 LT |
1876 | int out_ports, int in_ports) |
1877 | { | |
86e07d34 | 1878 | struct snd_rawmidi *rmidi; |
1da177e4 LT |
1879 | int err; |
1880 | ||
d82af9f9 CL |
1881 | err = snd_rawmidi_new(umidi->card, "USB MIDI", |
1882 | umidi->next_midi_device++, | |
1da177e4 LT |
1883 | out_ports, in_ports, &rmidi); |
1884 | if (err < 0) | |
1885 | return err; | |
d82af9f9 | 1886 | strcpy(rmidi->name, umidi->card->shortname); |
1da177e4 LT |
1887 | rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | |
1888 | SNDRV_RAWMIDI_INFO_INPUT | | |
1889 | SNDRV_RAWMIDI_INFO_DUPLEX; | |
a7b928ac | 1890 | rmidi->ops = &snd_usbmidi_ops; |
1da177e4 LT |
1891 | rmidi->private_data = umidi; |
1892 | rmidi->private_free = snd_usbmidi_rawmidi_free; | |
1893 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops); | |
1894 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops); | |
1895 | ||
1896 | umidi->rmidi = rmidi; | |
1897 | return 0; | |
1898 | } | |
1899 | ||
1900 | /* | |
1901 | * Temporarily stop input. | |
1902 | */ | |
1903 | void snd_usbmidi_input_stop(struct list_head* p) | |
1904 | { | |
86e07d34 | 1905 | struct snd_usb_midi* umidi; |
4773d1fb | 1906 | unsigned int i, j; |
1da177e4 | 1907 | |
86e07d34 | 1908 | umidi = list_entry(p, struct snd_usb_midi, list); |
1da177e4 | 1909 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
86e07d34 | 1910 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
1da177e4 | 1911 | if (ep->in) |
4773d1fb CL |
1912 | for (j = 0; j < INPUT_URBS; ++j) |
1913 | usb_kill_urb(ep->in->urbs[j]); | |
1da177e4 LT |
1914 | } |
1915 | } | |
1916 | ||
86e07d34 | 1917 | static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep) |
1da177e4 | 1918 | { |
4773d1fb CL |
1919 | unsigned int i; |
1920 | ||
1921 | if (!ep) | |
1922 | return; | |
1923 | for (i = 0; i < INPUT_URBS; ++i) { | |
1924 | struct urb* urb = ep->urbs[i]; | |
d82af9f9 | 1925 | urb->dev = ep->umidi->dev; |
1da177e4 LT |
1926 | snd_usbmidi_submit_urb(urb, GFP_KERNEL); |
1927 | } | |
1928 | } | |
1929 | ||
1930 | /* | |
1931 | * Resume input after a call to snd_usbmidi_input_stop(). | |
1932 | */ | |
1933 | void snd_usbmidi_input_start(struct list_head* p) | |
1934 | { | |
86e07d34 | 1935 | struct snd_usb_midi* umidi; |
1da177e4 LT |
1936 | int i; |
1937 | ||
86e07d34 | 1938 | umidi = list_entry(p, struct snd_usb_midi, list); |
1da177e4 LT |
1939 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) |
1940 | snd_usbmidi_input_start_ep(umidi->endpoints[i].in); | |
1941 | } | |
1942 | ||
1943 | /* | |
1944 | * Creates and registers everything needed for a MIDI streaming interface. | |
1945 | */ | |
d82af9f9 CL |
1946 | int snd_usbmidi_create(struct snd_card *card, |
1947 | struct usb_interface* iface, | |
1948 | struct list_head *midi_list, | |
1949 | const struct snd_usb_audio_quirk* quirk) | |
1da177e4 | 1950 | { |
86e07d34 TI |
1951 | struct snd_usb_midi* umidi; |
1952 | struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS]; | |
1da177e4 LT |
1953 | int out_ports, in_ports; |
1954 | int i, err; | |
1955 | ||
561b220a | 1956 | umidi = kzalloc(sizeof(*umidi), GFP_KERNEL); |
1da177e4 LT |
1957 | if (!umidi) |
1958 | return -ENOMEM; | |
d82af9f9 CL |
1959 | umidi->dev = interface_to_usbdev(iface); |
1960 | umidi->card = card; | |
1da177e4 LT |
1961 | umidi->iface = iface; |
1962 | umidi->quirk = quirk; | |
1963 | umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; | |
c8846970 | 1964 | init_timer(&umidi->error_timer); |
c0792e00 | 1965 | spin_lock_init(&umidi->disc_lock); |
96f61d9a | 1966 | mutex_init(&umidi->mutex); |
d82af9f9 CL |
1967 | umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor), |
1968 | le16_to_cpu(umidi->dev->descriptor.idProduct)); | |
c8846970 CL |
1969 | umidi->error_timer.function = snd_usbmidi_error_timer; |
1970 | umidi->error_timer.data = (unsigned long)umidi; | |
1da177e4 LT |
1971 | |
1972 | /* detect the endpoint(s) to use */ | |
1973 | memset(endpoints, 0, sizeof(endpoints)); | |
d1bda045 CL |
1974 | switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) { |
1975 | case QUIRK_MIDI_STANDARD_INTERFACE: | |
1da177e4 | 1976 | err = snd_usbmidi_get_ms_info(umidi, endpoints); |
d82af9f9 | 1977 | if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */ |
d05cc104 CL |
1978 | umidi->usb_protocol_ops = |
1979 | &snd_usbmidi_maudio_broken_running_status_ops; | |
d1bda045 | 1980 | break; |
030a07e4 KW |
1981 | case QUIRK_MIDI_US122L: |
1982 | umidi->usb_protocol_ops = &snd_usbmidi_122l_ops; | |
1983 | /* fall through */ | |
d1bda045 CL |
1984 | case QUIRK_MIDI_FIXED_ENDPOINT: |
1985 | memcpy(&endpoints[0], quirk->data, | |
86e07d34 | 1986 | sizeof(struct snd_usb_midi_endpoint_info)); |
d1bda045 CL |
1987 | err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); |
1988 | break; | |
1989 | case QUIRK_MIDI_YAMAHA: | |
1990 | err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]); | |
1991 | break; | |
1992 | case QUIRK_MIDI_MIDIMAN: | |
1993 | umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops; | |
1994 | memcpy(&endpoints[0], quirk->data, | |
86e07d34 | 1995 | sizeof(struct snd_usb_midi_endpoint_info)); |
d1bda045 CL |
1996 | err = 0; |
1997 | break; | |
1998 | case QUIRK_MIDI_NOVATION: | |
1999 | umidi->usb_protocol_ops = &snd_usbmidi_novation_ops; | |
2000 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); | |
2001 | break; | |
55de5ef9 | 2002 | case QUIRK_MIDI_FASTLANE: |
d1bda045 | 2003 | umidi->usb_protocol_ops = &snd_usbmidi_raw_ops; |
55de5ef9 CL |
2004 | /* |
2005 | * Interface 1 contains isochronous endpoints, but with the same | |
2006 | * numbers as in interface 0. Since it is interface 1 that the | |
2007 | * USB core has most recently seen, these descriptors are now | |
2008 | * associated with the endpoint numbers. This will foul up our | |
2009 | * attempts to submit bulk/interrupt URBs to the endpoints in | |
2010 | * interface 0, so we have to make sure that the USB core looks | |
2011 | * again at interface 0 by calling usb_set_interface() on it. | |
2012 | */ | |
d82af9f9 | 2013 | usb_set_interface(umidi->dev, 0, 0); |
d1bda045 CL |
2014 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); |
2015 | break; | |
2016 | case QUIRK_MIDI_EMAGIC: | |
2017 | umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops; | |
2018 | memcpy(&endpoints[0], quirk->data, | |
86e07d34 | 2019 | sizeof(struct snd_usb_midi_endpoint_info)); |
d1bda045 CL |
2020 | err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); |
2021 | break; | |
cc7a59bd | 2022 | case QUIRK_MIDI_CME: |
61870aed | 2023 | umidi->usb_protocol_ops = &snd_usbmidi_cme_ops; |
d1bda045 CL |
2024 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); |
2025 | break; | |
2026 | default: | |
2027 | snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); | |
2028 | err = -ENXIO; | |
2029 | break; | |
1da177e4 LT |
2030 | } |
2031 | if (err < 0) { | |
2032 | kfree(umidi); | |
2033 | return err; | |
2034 | } | |
2035 | ||
2036 | /* create rawmidi device */ | |
2037 | out_ports = 0; | |
2038 | in_ports = 0; | |
2039 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
fbc54391 AM |
2040 | out_ports += hweight16(endpoints[i].out_cables); |
2041 | in_ports += hweight16(endpoints[i].in_cables); | |
1da177e4 LT |
2042 | } |
2043 | err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports); | |
2044 | if (err < 0) { | |
2045 | kfree(umidi); | |
2046 | return err; | |
2047 | } | |
2048 | ||
2049 | /* create endpoint/port structures */ | |
2050 | if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN) | |
2051 | err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]); | |
2052 | else | |
2053 | err = snd_usbmidi_create_endpoints(umidi, endpoints); | |
2054 | if (err < 0) { | |
2055 | snd_usbmidi_free(umidi); | |
2056 | return err; | |
2057 | } | |
2058 | ||
d82af9f9 | 2059 | list_add_tail(&umidi->list, midi_list); |
1da177e4 LT |
2060 | |
2061 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) | |
2062 | snd_usbmidi_input_start_ep(umidi->endpoints[i].in); | |
2063 | return 0; | |
2064 | } | |
2065 | ||
d82af9f9 | 2066 | EXPORT_SYMBOL(snd_usbmidi_create); |
1da177e4 LT |
2067 | EXPORT_SYMBOL(snd_usbmidi_input_stop); |
2068 | EXPORT_SYMBOL(snd_usbmidi_input_start); | |
2069 | EXPORT_SYMBOL(snd_usbmidi_disconnect); |