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" |
e5779998 DM |
56 | #include "midi.h" |
57 | #include "helper.h" | |
1da177e4 LT |
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; | |
21af7d8c | 437 | |
d05cc104 CL |
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, | |
21af7d8c | 631 | .output = snd_usbmidi_standard_output, |
1da177e4 LT |
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, | |
21af7d8c | 637 | .output = snd_usbmidi_standard_output, |
d05cc104 CL |
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 | ||
4434ade8 KF |
647 | /* |
648 | * AKAI MPD16 protocol: | |
649 | * | |
650 | * For control port (endpoint 1): | |
651 | * ============================== | |
652 | * One or more chunks consisting of first byte of (0x10 | msg_len) and then a | |
653 | * SysEx message (msg_len=9 bytes long). | |
654 | * | |
655 | * For data port (endpoint 2): | |
656 | * =========================== | |
657 | * One or more chunks consisting of first byte of (0x20 | msg_len) and then a | |
658 | * MIDI message (msg_len bytes long) | |
659 | * | |
660 | * Messages sent: Active Sense, Note On, Poly Pressure, Control Change. | |
661 | */ | |
662 | static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep, | |
663 | uint8_t *buffer, int buffer_length) | |
664 | { | |
665 | unsigned int pos = 0; | |
666 | unsigned int len = (unsigned int)buffer_length; | |
667 | while (pos < len) { | |
668 | unsigned int port = (buffer[pos] >> 4) - 1; | |
669 | unsigned int msg_len = buffer[pos] & 0x0f; | |
670 | pos++; | |
671 | if (pos + msg_len <= len && port < 2) | |
672 | snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len); | |
673 | pos += msg_len; | |
674 | } | |
675 | } | |
676 | ||
677 | #define MAX_AKAI_SYSEX_LEN 9 | |
678 | ||
679 | static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep, | |
680 | struct urb *urb) | |
681 | { | |
682 | uint8_t *msg; | |
683 | int pos, end, count, buf_end; | |
684 | uint8_t tmp[MAX_AKAI_SYSEX_LEN]; | |
685 | struct snd_rawmidi_substream *substream = ep->ports[0].substream; | |
686 | ||
687 | if (!ep->ports[0].active) | |
688 | return; | |
689 | ||
690 | msg = urb->transfer_buffer + urb->transfer_buffer_length; | |
691 | buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1; | |
692 | ||
693 | /* only try adding more data when there's space for at least 1 SysEx */ | |
694 | while (urb->transfer_buffer_length < buf_end) { | |
695 | count = snd_rawmidi_transmit_peek(substream, | |
696 | tmp, MAX_AKAI_SYSEX_LEN); | |
697 | if (!count) { | |
698 | ep->ports[0].active = 0; | |
699 | return; | |
700 | } | |
701 | /* try to skip non-SysEx data */ | |
702 | for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++) | |
703 | ; | |
704 | ||
705 | if (pos > 0) { | |
706 | snd_rawmidi_transmit_ack(substream, pos); | |
707 | continue; | |
708 | } | |
709 | ||
710 | /* look for the start or end marker */ | |
711 | for (end = 1; end < count && tmp[end] < 0xF0; end++) | |
712 | ; | |
713 | ||
714 | /* next SysEx started before the end of current one */ | |
715 | if (end < count && tmp[end] == 0xF0) { | |
716 | /* it's incomplete - drop it */ | |
717 | snd_rawmidi_transmit_ack(substream, end); | |
718 | continue; | |
719 | } | |
720 | /* SysEx complete */ | |
721 | if (end < count && tmp[end] == 0xF7) { | |
722 | /* queue it, ack it, and get the next one */ | |
723 | count = end + 1; | |
724 | msg[0] = 0x10 | count; | |
725 | memcpy(&msg[1], tmp, count); | |
726 | snd_rawmidi_transmit_ack(substream, count); | |
727 | urb->transfer_buffer_length += count + 1; | |
728 | msg += count + 1; | |
729 | continue; | |
730 | } | |
731 | /* less than 9 bytes and no end byte - wait for more */ | |
732 | if (count < MAX_AKAI_SYSEX_LEN) { | |
733 | ep->ports[0].active = 0; | |
734 | return; | |
735 | } | |
736 | /* 9 bytes and no end marker in sight - malformed, skip it */ | |
737 | snd_rawmidi_transmit_ack(substream, count); | |
738 | } | |
739 | } | |
740 | ||
741 | static struct usb_protocol_ops snd_usbmidi_akai_ops = { | |
742 | .input = snd_usbmidi_akai_input, | |
743 | .output = snd_usbmidi_akai_output, | |
744 | }; | |
745 | ||
1da177e4 LT |
746 | /* |
747 | * Novation USB MIDI protocol: number of data bytes is in the first byte | |
748 | * (when receiving) (+1!) or in the second byte (when sending); data begins | |
749 | * at the third byte. | |
750 | */ | |
751 | ||
86e07d34 | 752 | static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep, |
1da177e4 LT |
753 | uint8_t* buffer, int buffer_length) |
754 | { | |
755 | if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1) | |
756 | return; | |
757 | snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1); | |
758 | } | |
759 | ||
ed4affa5 CL |
760 | static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep, |
761 | struct urb *urb) | |
1da177e4 LT |
762 | { |
763 | uint8_t* transfer_buffer; | |
764 | int count; | |
765 | ||
766 | if (!ep->ports[0].active) | |
767 | return; | |
ed4affa5 | 768 | transfer_buffer = urb->transfer_buffer; |
1da177e4 LT |
769 | count = snd_rawmidi_transmit(ep->ports[0].substream, |
770 | &transfer_buffer[2], | |
771 | ep->max_transfer - 2); | |
772 | if (count < 1) { | |
773 | ep->ports[0].active = 0; | |
774 | return; | |
775 | } | |
776 | transfer_buffer[0] = 0; | |
777 | transfer_buffer[1] = count; | |
ed4affa5 | 778 | urb->transfer_buffer_length = 2 + count; |
1da177e4 LT |
779 | } |
780 | ||
781 | static struct usb_protocol_ops snd_usbmidi_novation_ops = { | |
782 | .input = snd_usbmidi_novation_input, | |
783 | .output = snd_usbmidi_novation_output, | |
784 | }; | |
785 | ||
786 | /* | |
c7f57216 | 787 | * "raw" protocol: just move raw MIDI bytes from/to the endpoint |
1da177e4 LT |
788 | */ |
789 | ||
86e07d34 | 790 | static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep, |
6155aff8 | 791 | uint8_t* buffer, int buffer_length) |
1da177e4 LT |
792 | { |
793 | snd_usbmidi_input_data(ep, 0, buffer, buffer_length); | |
794 | } | |
795 | ||
ed4affa5 CL |
796 | static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep, |
797 | struct urb *urb) | |
1da177e4 LT |
798 | { |
799 | int count; | |
800 | ||
801 | if (!ep->ports[0].active) | |
802 | return; | |
803 | count = snd_rawmidi_transmit(ep->ports[0].substream, | |
ed4affa5 | 804 | urb->transfer_buffer, |
1da177e4 LT |
805 | ep->max_transfer); |
806 | if (count < 1) { | |
807 | ep->ports[0].active = 0; | |
808 | return; | |
809 | } | |
ed4affa5 | 810 | urb->transfer_buffer_length = count; |
1da177e4 LT |
811 | } |
812 | ||
6155aff8 CL |
813 | static struct usb_protocol_ops snd_usbmidi_raw_ops = { |
814 | .input = snd_usbmidi_raw_input, | |
815 | .output = snd_usbmidi_raw_output, | |
1da177e4 LT |
816 | }; |
817 | ||
030a07e4 KW |
818 | static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep, |
819 | uint8_t *buffer, int buffer_length) | |
820 | { | |
821 | if (buffer_length != 9) | |
822 | return; | |
823 | buffer_length = 8; | |
824 | while (buffer_length && buffer[buffer_length - 1] == 0xFD) | |
825 | buffer_length--; | |
826 | if (buffer_length) | |
827 | snd_usbmidi_input_data(ep, 0, buffer, buffer_length); | |
828 | } | |
829 | ||
ed4affa5 CL |
830 | static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep, |
831 | struct urb *urb) | |
030a07e4 KW |
832 | { |
833 | int count; | |
834 | ||
835 | if (!ep->ports[0].active) | |
836 | return; | |
4f4e8f69 PZ |
837 | switch (snd_usb_get_speed(ep->umidi->dev)) { |
838 | case USB_SPEED_HIGH: | |
839 | case USB_SPEED_SUPER: | |
840 | count = 1; | |
841 | break; | |
842 | default: | |
843 | count = 2; | |
844 | } | |
030a07e4 | 845 | count = snd_rawmidi_transmit(ep->ports[0].substream, |
ed4affa5 | 846 | urb->transfer_buffer, |
030a07e4 KW |
847 | count); |
848 | if (count < 1) { | |
849 | ep->ports[0].active = 0; | |
850 | return; | |
851 | } | |
852 | ||
921eebdc KW |
853 | memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count); |
854 | urb->transfer_buffer_length = ep->max_transfer; | |
030a07e4 KW |
855 | } |
856 | ||
857 | static struct usb_protocol_ops snd_usbmidi_122l_ops = { | |
858 | .input = snd_usbmidi_us122l_input, | |
859 | .output = snd_usbmidi_us122l_output, | |
860 | }; | |
861 | ||
1da177e4 LT |
862 | /* |
863 | * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching. | |
864 | */ | |
865 | ||
86e07d34 | 866 | static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep) |
1da177e4 LT |
867 | { |
868 | static const u8 init_data[] = { | |
869 | /* initialization magic: "get version" */ | |
870 | 0xf0, | |
871 | 0x00, 0x20, 0x31, /* Emagic */ | |
872 | 0x64, /* Unitor8 */ | |
873 | 0x0b, /* version number request */ | |
874 | 0x00, /* command version */ | |
875 | 0x00, /* EEPROM, box 0 */ | |
876 | 0xf7 | |
877 | }; | |
878 | send_bulk_static_data(ep, init_data, sizeof(init_data)); | |
879 | /* while we're at it, pour on more magic */ | |
880 | send_bulk_static_data(ep, init_data, sizeof(init_data)); | |
881 | } | |
882 | ||
86e07d34 | 883 | static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep) |
1da177e4 LT |
884 | { |
885 | static const u8 finish_data[] = { | |
886 | /* switch to patch mode with last preset */ | |
887 | 0xf0, | |
888 | 0x00, 0x20, 0x31, /* Emagic */ | |
889 | 0x64, /* Unitor8 */ | |
890 | 0x10, /* patch switch command */ | |
891 | 0x00, /* command version */ | |
892 | 0x7f, /* to all boxes */ | |
893 | 0x40, /* last preset in EEPROM */ | |
894 | 0xf7 | |
895 | }; | |
896 | send_bulk_static_data(ep, finish_data, sizeof(finish_data)); | |
897 | } | |
898 | ||
86e07d34 | 899 | static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep, |
1da177e4 LT |
900 | uint8_t* buffer, int buffer_length) |
901 | { | |
c347e9fc CL |
902 | int i; |
903 | ||
904 | /* FF indicates end of valid data */ | |
905 | for (i = 0; i < buffer_length; ++i) | |
906 | if (buffer[i] == 0xff) { | |
907 | buffer_length = i; | |
908 | break; | |
909 | } | |
1da177e4 LT |
910 | |
911 | /* handle F5 at end of last buffer */ | |
912 | if (ep->seen_f5) | |
913 | goto switch_port; | |
914 | ||
915 | while (buffer_length > 0) { | |
1da177e4 LT |
916 | /* determine size of data until next F5 */ |
917 | for (i = 0; i < buffer_length; ++i) | |
918 | if (buffer[i] == 0xf5) | |
919 | break; | |
920 | snd_usbmidi_input_data(ep, ep->current_port, buffer, i); | |
921 | buffer += i; | |
922 | buffer_length -= i; | |
923 | ||
924 | if (buffer_length <= 0) | |
925 | break; | |
926 | /* assert(buffer[0] == 0xf5); */ | |
927 | ep->seen_f5 = 1; | |
928 | ++buffer; | |
929 | --buffer_length; | |
930 | ||
931 | switch_port: | |
932 | if (buffer_length <= 0) | |
933 | break; | |
934 | if (buffer[0] < 0x80) { | |
935 | ep->current_port = (buffer[0] - 1) & 15; | |
936 | ++buffer; | |
937 | --buffer_length; | |
938 | } | |
939 | ep->seen_f5 = 0; | |
940 | } | |
941 | } | |
942 | ||
ed4affa5 CL |
943 | static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep, |
944 | struct urb *urb) | |
1da177e4 LT |
945 | { |
946 | int port0 = ep->current_port; | |
ed4affa5 | 947 | uint8_t* buf = urb->transfer_buffer; |
1da177e4 LT |
948 | int buf_free = ep->max_transfer; |
949 | int length, i; | |
950 | ||
951 | for (i = 0; i < 0x10; ++i) { | |
952 | /* round-robin, starting at the last current port */ | |
953 | int portnum = (port0 + i) & 15; | |
86e07d34 | 954 | struct usbmidi_out_port* port = &ep->ports[portnum]; |
1da177e4 LT |
955 | |
956 | if (!port->active) | |
957 | continue; | |
958 | if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) { | |
959 | port->active = 0; | |
960 | continue; | |
961 | } | |
962 | ||
963 | if (portnum != ep->current_port) { | |
964 | if (buf_free < 2) | |
965 | break; | |
966 | ep->current_port = portnum; | |
967 | buf[0] = 0xf5; | |
968 | buf[1] = (portnum + 1) & 15; | |
969 | buf += 2; | |
970 | buf_free -= 2; | |
971 | } | |
972 | ||
973 | if (buf_free < 1) | |
974 | break; | |
975 | length = snd_rawmidi_transmit(port->substream, buf, buf_free); | |
976 | if (length > 0) { | |
977 | buf += length; | |
978 | buf_free -= length; | |
979 | if (buf_free < 1) | |
980 | break; | |
981 | } | |
982 | } | |
c347e9fc CL |
983 | if (buf_free < ep->max_transfer && buf_free > 0) { |
984 | *buf = 0xff; | |
985 | --buf_free; | |
986 | } | |
ed4affa5 | 987 | urb->transfer_buffer_length = ep->max_transfer - buf_free; |
1da177e4 LT |
988 | } |
989 | ||
990 | static struct usb_protocol_ops snd_usbmidi_emagic_ops = { | |
991 | .input = snd_usbmidi_emagic_input, | |
992 | .output = snd_usbmidi_emagic_output, | |
993 | .init_out_endpoint = snd_usbmidi_emagic_init_out, | |
994 | .finish_out_endpoint = snd_usbmidi_emagic_finish_out, | |
995 | }; | |
996 | ||
997 | ||
96f61d9a CL |
998 | static void update_roland_altsetting(struct snd_usb_midi* umidi) |
999 | { | |
1000 | struct usb_interface *intf; | |
1001 | struct usb_host_interface *hostif; | |
1002 | struct usb_interface_descriptor *intfd; | |
1003 | int is_light_load; | |
1004 | ||
1005 | intf = umidi->iface; | |
1006 | is_light_load = intf->cur_altsetting != intf->altsetting; | |
1007 | if (umidi->roland_load_ctl->private_value == is_light_load) | |
1008 | return; | |
1009 | hostif = &intf->altsetting[umidi->roland_load_ctl->private_value]; | |
1010 | intfd = get_iface_desc(hostif); | |
1011 | snd_usbmidi_input_stop(&umidi->list); | |
d82af9f9 | 1012 | usb_set_interface(umidi->dev, intfd->bInterfaceNumber, |
96f61d9a CL |
1013 | intfd->bAlternateSetting); |
1014 | snd_usbmidi_input_start(&umidi->list); | |
1015 | } | |
1016 | ||
1017 | static void substream_open(struct snd_rawmidi_substream *substream, int open) | |
1018 | { | |
1019 | struct snd_usb_midi* umidi = substream->rmidi->private_data; | |
1020 | struct snd_kcontrol *ctl; | |
1021 | ||
1022 | mutex_lock(&umidi->mutex); | |
1023 | if (open) { | |
1024 | if (umidi->opened++ == 0 && umidi->roland_load_ctl) { | |
1025 | ctl = umidi->roland_load_ctl; | |
1026 | ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
d82af9f9 | 1027 | snd_ctl_notify(umidi->card, |
96f61d9a CL |
1028 | SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); |
1029 | update_roland_altsetting(umidi); | |
1030 | } | |
1031 | } else { | |
1032 | if (--umidi->opened == 0 && umidi->roland_load_ctl) { | |
1033 | ctl = umidi->roland_load_ctl; | |
1034 | ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
d82af9f9 | 1035 | snd_ctl_notify(umidi->card, |
96f61d9a CL |
1036 | SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); |
1037 | } | |
1038 | } | |
1039 | mutex_unlock(&umidi->mutex); | |
1040 | } | |
1041 | ||
86e07d34 | 1042 | static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream) |
1da177e4 | 1043 | { |
86e07d34 TI |
1044 | struct snd_usb_midi* umidi = substream->rmidi->private_data; |
1045 | struct usbmidi_out_port* port = NULL; | |
1da177e4 LT |
1046 | int i, j; |
1047 | ||
1048 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) | |
1049 | if (umidi->endpoints[i].out) | |
1050 | for (j = 0; j < 0x10; ++j) | |
1051 | if (umidi->endpoints[i].out->ports[j].substream == substream) { | |
1052 | port = &umidi->endpoints[i].out->ports[j]; | |
1053 | break; | |
1054 | } | |
1055 | if (!port) { | |
1056 | snd_BUG(); | |
1057 | return -ENXIO; | |
1058 | } | |
1059 | substream->runtime->private_data = port; | |
1060 | port->state = STATE_UNKNOWN; | |
96f61d9a | 1061 | substream_open(substream, 1); |
1da177e4 LT |
1062 | return 0; |
1063 | } | |
1064 | ||
86e07d34 | 1065 | static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream) |
1da177e4 | 1066 | { |
96f61d9a | 1067 | substream_open(substream, 0); |
1da177e4 LT |
1068 | return 0; |
1069 | } | |
1070 | ||
86e07d34 | 1071 | static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up) |
1da177e4 | 1072 | { |
86e07d34 | 1073 | struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data; |
1da177e4 LT |
1074 | |
1075 | port->active = up; | |
1076 | if (up) { | |
d82af9f9 | 1077 | if (port->ep->umidi->disconnected) { |
1da177e4 LT |
1078 | /* gobble up remaining bytes to prevent wait in |
1079 | * snd_rawmidi_drain_output */ | |
1080 | while (!snd_rawmidi_transmit_empty(substream)) | |
1081 | snd_rawmidi_transmit_ack(substream, 1); | |
1082 | return; | |
1083 | } | |
1f04128a | 1084 | tasklet_schedule(&port->ep->tasklet); |
1da177e4 LT |
1085 | } |
1086 | } | |
1087 | ||
a65dd997 CL |
1088 | static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream) |
1089 | { | |
1090 | struct usbmidi_out_port* port = substream->runtime->private_data; | |
1091 | struct snd_usb_midi_out_endpoint *ep = port->ep; | |
1092 | unsigned int drain_urbs; | |
1093 | DEFINE_WAIT(wait); | |
1094 | long timeout = msecs_to_jiffies(50); | |
1095 | ||
29aac005 TI |
1096 | if (ep->umidi->disconnected) |
1097 | return; | |
a65dd997 CL |
1098 | /* |
1099 | * The substream buffer is empty, but some data might still be in the | |
1100 | * currently active URBs, so we have to wait for those to complete. | |
1101 | */ | |
1102 | spin_lock_irq(&ep->buffer_lock); | |
1103 | drain_urbs = ep->active_urbs; | |
1104 | if (drain_urbs) { | |
1105 | ep->drain_urbs |= drain_urbs; | |
1106 | do { | |
1107 | prepare_to_wait(&ep->drain_wait, &wait, | |
1108 | TASK_UNINTERRUPTIBLE); | |
1109 | spin_unlock_irq(&ep->buffer_lock); | |
1110 | timeout = schedule_timeout(timeout); | |
1111 | spin_lock_irq(&ep->buffer_lock); | |
1112 | drain_urbs &= ep->drain_urbs; | |
1113 | } while (drain_urbs && timeout); | |
1114 | finish_wait(&ep->drain_wait, &wait); | |
1115 | } | |
1116 | spin_unlock_irq(&ep->buffer_lock); | |
1117 | } | |
1118 | ||
86e07d34 | 1119 | static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream) |
1da177e4 | 1120 | { |
96f61d9a | 1121 | substream_open(substream, 1); |
1da177e4 LT |
1122 | return 0; |
1123 | } | |
1124 | ||
86e07d34 | 1125 | static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream) |
1da177e4 | 1126 | { |
96f61d9a | 1127 | substream_open(substream, 0); |
1da177e4 LT |
1128 | return 0; |
1129 | } | |
1130 | ||
86e07d34 | 1131 | static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) |
1da177e4 | 1132 | { |
86e07d34 | 1133 | struct snd_usb_midi* umidi = substream->rmidi->private_data; |
1da177e4 LT |
1134 | |
1135 | if (up) | |
1136 | set_bit(substream->number, &umidi->input_triggered); | |
1137 | else | |
1138 | clear_bit(substream->number, &umidi->input_triggered); | |
1139 | } | |
1140 | ||
86e07d34 | 1141 | static struct snd_rawmidi_ops snd_usbmidi_output_ops = { |
1da177e4 LT |
1142 | .open = snd_usbmidi_output_open, |
1143 | .close = snd_usbmidi_output_close, | |
1144 | .trigger = snd_usbmidi_output_trigger, | |
a65dd997 | 1145 | .drain = snd_usbmidi_output_drain, |
1da177e4 LT |
1146 | }; |
1147 | ||
86e07d34 | 1148 | static struct snd_rawmidi_ops snd_usbmidi_input_ops = { |
1da177e4 LT |
1149 | .open = snd_usbmidi_input_open, |
1150 | .close = snd_usbmidi_input_close, | |
1151 | .trigger = snd_usbmidi_input_trigger | |
1152 | }; | |
1153 | ||
ed4affa5 CL |
1154 | static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb, |
1155 | unsigned int buffer_length) | |
1156 | { | |
997ea58e DM |
1157 | usb_free_coherent(umidi->dev, buffer_length, |
1158 | urb->transfer_buffer, urb->transfer_dma); | |
ed4affa5 CL |
1159 | usb_free_urb(urb); |
1160 | } | |
1161 | ||
1da177e4 LT |
1162 | /* |
1163 | * Frees an input endpoint. | |
1164 | * May be called when ep hasn't been initialized completely. | |
1165 | */ | |
86e07d34 | 1166 | static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep) |
1da177e4 | 1167 | { |
4773d1fb CL |
1168 | unsigned int i; |
1169 | ||
ed4affa5 CL |
1170 | for (i = 0; i < INPUT_URBS; ++i) |
1171 | if (ep->urbs[i]) | |
1172 | free_urb_and_buffer(ep->umidi, ep->urbs[i], | |
1173 | ep->urbs[i]->transfer_buffer_length); | |
1da177e4 LT |
1174 | kfree(ep); |
1175 | } | |
1176 | ||
1177 | /* | |
1178 | * Creates an input endpoint. | |
1179 | */ | |
86e07d34 TI |
1180 | static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi, |
1181 | struct snd_usb_midi_endpoint_info* ep_info, | |
1182 | struct snd_usb_midi_endpoint* rep) | |
1da177e4 | 1183 | { |
86e07d34 | 1184 | struct snd_usb_midi_in_endpoint* ep; |
1da177e4 LT |
1185 | void* buffer; |
1186 | unsigned int pipe; | |
1187 | int length; | |
4773d1fb | 1188 | unsigned int i; |
1da177e4 LT |
1189 | |
1190 | rep->in = NULL; | |
561b220a | 1191 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
1da177e4 LT |
1192 | if (!ep) |
1193 | return -ENOMEM; | |
1194 | ep->umidi = umidi; | |
1195 | ||
4773d1fb CL |
1196 | for (i = 0; i < INPUT_URBS; ++i) { |
1197 | ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL); | |
1198 | if (!ep->urbs[i]) { | |
1199 | snd_usbmidi_in_endpoint_delete(ep); | |
1200 | return -ENOMEM; | |
1201 | } | |
1da177e4 LT |
1202 | } |
1203 | if (ep_info->in_interval) | |
d82af9f9 | 1204 | pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep); |
1da177e4 | 1205 | else |
d82af9f9 CL |
1206 | pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep); |
1207 | length = usb_maxpacket(umidi->dev, pipe, 0); | |
4773d1fb | 1208 | for (i = 0; i < INPUT_URBS; ++i) { |
997ea58e DM |
1209 | buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL, |
1210 | &ep->urbs[i]->transfer_dma); | |
4773d1fb CL |
1211 | if (!buffer) { |
1212 | snd_usbmidi_in_endpoint_delete(ep); | |
1213 | return -ENOMEM; | |
1214 | } | |
1215 | if (ep_info->in_interval) | |
d82af9f9 | 1216 | usb_fill_int_urb(ep->urbs[i], umidi->dev, |
4773d1fb CL |
1217 | pipe, buffer, length, |
1218 | snd_usbmidi_in_urb_complete, | |
1219 | ep, ep_info->in_interval); | |
1220 | else | |
d82af9f9 | 1221 | usb_fill_bulk_urb(ep->urbs[i], umidi->dev, |
4773d1fb CL |
1222 | pipe, buffer, length, |
1223 | snd_usbmidi_in_urb_complete, ep); | |
1224 | ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | |
1da177e4 | 1225 | } |
1da177e4 LT |
1226 | |
1227 | rep->in = ep; | |
1228 | return 0; | |
1229 | } | |
1230 | ||
1da177e4 LT |
1231 | /* |
1232 | * Frees an output endpoint. | |
1233 | * May be called when ep hasn't been initialized completely. | |
1234 | */ | |
29aac005 | 1235 | static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep) |
1da177e4 | 1236 | { |
ed4affa5 CL |
1237 | unsigned int i; |
1238 | ||
1239 | for (i = 0; i < OUTPUT_URBS; ++i) | |
29aac005 | 1240 | if (ep->urbs[i].urb) { |
ed4affa5 CL |
1241 | free_urb_and_buffer(ep->umidi, ep->urbs[i].urb, |
1242 | ep->max_transfer); | |
29aac005 TI |
1243 | ep->urbs[i].urb = NULL; |
1244 | } | |
1245 | } | |
1246 | ||
1247 | static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep) | |
1248 | { | |
1249 | snd_usbmidi_out_endpoint_clear(ep); | |
1da177e4 LT |
1250 | kfree(ep); |
1251 | } | |
1252 | ||
1253 | /* | |
1254 | * Creates an output endpoint, and initializes output ports. | |
1255 | */ | |
86e07d34 TI |
1256 | static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi, |
1257 | struct snd_usb_midi_endpoint_info* ep_info, | |
21af7d8c | 1258 | struct snd_usb_midi_endpoint* rep) |
1da177e4 | 1259 | { |
86e07d34 | 1260 | struct snd_usb_midi_out_endpoint* ep; |
ed4affa5 | 1261 | unsigned int i; |
1da177e4 LT |
1262 | unsigned int pipe; |
1263 | void* buffer; | |
1264 | ||
1265 | rep->out = NULL; | |
561b220a | 1266 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
1da177e4 LT |
1267 | if (!ep) |
1268 | return -ENOMEM; | |
1269 | ep->umidi = umidi; | |
1270 | ||
ed4affa5 CL |
1271 | for (i = 0; i < OUTPUT_URBS; ++i) { |
1272 | ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL); | |
1273 | if (!ep->urbs[i].urb) { | |
1274 | snd_usbmidi_out_endpoint_delete(ep); | |
1275 | return -ENOMEM; | |
1276 | } | |
1277 | ep->urbs[i].ep = ep; | |
1da177e4 | 1278 | } |
a6a712ae | 1279 | if (ep_info->out_interval) |
d82af9f9 | 1280 | pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep); |
a6a712ae | 1281 | else |
d82af9f9 | 1282 | pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep); |
f167e1d0 CL |
1283 | switch (umidi->usb_id) { |
1284 | default: | |
d82af9f9 | 1285 | ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1); |
f167e1d0 CL |
1286 | break; |
1287 | /* | |
1288 | * Various chips declare a packet size larger than 4 bytes, but | |
1289 | * do not actually work with larger packets: | |
1290 | */ | |
1291 | case USB_ID(0x0a92, 0x1020): /* ESI M4U */ | |
1292 | case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */ | |
1293 | case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */ | |
1294 | case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */ | |
1295 | case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */ | |
1296 | ep->max_transfer = 4; | |
1297 | break; | |
921eebdc KW |
1298 | /* |
1299 | * Some devices only work with 9 bytes packet size: | |
1300 | */ | |
1301 | case USB_ID(0x0644, 0x800E): /* Tascam US-122L */ | |
1302 | case USB_ID(0x0644, 0x800F): /* Tascam US-144 */ | |
1303 | ep->max_transfer = 9; | |
1304 | break; | |
f167e1d0 | 1305 | } |
ed4affa5 | 1306 | for (i = 0; i < OUTPUT_URBS; ++i) { |
997ea58e DM |
1307 | buffer = usb_alloc_coherent(umidi->dev, |
1308 | ep->max_transfer, GFP_KERNEL, | |
1309 | &ep->urbs[i].urb->transfer_dma); | |
ed4affa5 CL |
1310 | if (!buffer) { |
1311 | snd_usbmidi_out_endpoint_delete(ep); | |
1312 | return -ENOMEM; | |
1313 | } | |
1314 | if (ep_info->out_interval) | |
d82af9f9 | 1315 | usb_fill_int_urb(ep->urbs[i].urb, umidi->dev, |
ed4affa5 CL |
1316 | pipe, buffer, ep->max_transfer, |
1317 | snd_usbmidi_out_urb_complete, | |
1318 | &ep->urbs[i], ep_info->out_interval); | |
1319 | else | |
d82af9f9 | 1320 | usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev, |
ed4affa5 CL |
1321 | pipe, buffer, ep->max_transfer, |
1322 | snd_usbmidi_out_urb_complete, | |
1323 | &ep->urbs[i]); | |
1324 | ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | |
1da177e4 | 1325 | } |
1da177e4 LT |
1326 | |
1327 | spin_lock_init(&ep->buffer_lock); | |
1328 | tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep); | |
a65dd997 | 1329 | init_waitqueue_head(&ep->drain_wait); |
1da177e4 LT |
1330 | |
1331 | for (i = 0; i < 0x10; ++i) | |
1332 | if (ep_info->out_cables & (1 << i)) { | |
1333 | ep->ports[i].ep = ep; | |
1334 | ep->ports[i].cable = i << 4; | |
1335 | } | |
1336 | ||
1337 | if (umidi->usb_protocol_ops->init_out_endpoint) | |
1338 | umidi->usb_protocol_ops->init_out_endpoint(ep); | |
1339 | ||
1340 | rep->out = ep; | |
1341 | return 0; | |
1342 | } | |
1343 | ||
1344 | /* | |
1345 | * Frees everything. | |
1346 | */ | |
86e07d34 | 1347 | static void snd_usbmidi_free(struct snd_usb_midi* umidi) |
1da177e4 LT |
1348 | { |
1349 | int i; | |
1350 | ||
1351 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
86e07d34 | 1352 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
1da177e4 LT |
1353 | if (ep->out) |
1354 | snd_usbmidi_out_endpoint_delete(ep->out); | |
1355 | if (ep->in) | |
1356 | snd_usbmidi_in_endpoint_delete(ep->in); | |
1357 | } | |
96f61d9a | 1358 | mutex_destroy(&umidi->mutex); |
1da177e4 LT |
1359 | kfree(umidi); |
1360 | } | |
1361 | ||
1362 | /* | |
1363 | * Unlinks all URBs (must be done before the usb_device is deleted). | |
1364 | */ | |
ee733397 | 1365 | void snd_usbmidi_disconnect(struct list_head* p) |
1da177e4 | 1366 | { |
86e07d34 | 1367 | struct snd_usb_midi* umidi; |
4773d1fb | 1368 | unsigned int i, j; |
1da177e4 | 1369 | |
86e07d34 | 1370 | umidi = list_entry(p, struct snd_usb_midi, list); |
c0792e00 TI |
1371 | /* |
1372 | * an URB's completion handler may start the timer and | |
1373 | * a timer may submit an URB. To reliably break the cycle | |
1374 | * a flag under lock must be used | |
1375 | */ | |
1376 | spin_lock_irq(&umidi->disc_lock); | |
1377 | umidi->disconnected = 1; | |
1378 | spin_unlock_irq(&umidi->disc_lock); | |
1da177e4 | 1379 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
86e07d34 | 1380 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
c8846970 CL |
1381 | if (ep->out) |
1382 | tasklet_kill(&ep->out->tasklet); | |
ed4affa5 CL |
1383 | if (ep->out) { |
1384 | for (j = 0; j < OUTPUT_URBS; ++j) | |
1385 | usb_kill_urb(ep->out->urbs[j].urb); | |
1da177e4 LT |
1386 | if (umidi->usb_protocol_ops->finish_out_endpoint) |
1387 | umidi->usb_protocol_ops->finish_out_endpoint(ep->out); | |
29aac005 TI |
1388 | ep->out->active_urbs = 0; |
1389 | if (ep->out->drain_urbs) { | |
1390 | ep->out->drain_urbs = 0; | |
1391 | wake_up(&ep->out->drain_wait); | |
1392 | } | |
1da177e4 | 1393 | } |
f5e135af | 1394 | if (ep->in) |
4773d1fb CL |
1395 | for (j = 0; j < INPUT_URBS; ++j) |
1396 | usb_kill_urb(ep->in->urbs[j]); | |
7a17daae | 1397 | /* free endpoints here; later call can result in Oops */ |
29aac005 TI |
1398 | if (ep->out) |
1399 | snd_usbmidi_out_endpoint_clear(ep->out); | |
7a17daae TI |
1400 | if (ep->in) { |
1401 | snd_usbmidi_in_endpoint_delete(ep->in); | |
1402 | ep->in = NULL; | |
1403 | } | |
1da177e4 | 1404 | } |
c0792e00 | 1405 | del_timer_sync(&umidi->error_timer); |
1da177e4 LT |
1406 | } |
1407 | ||
86e07d34 | 1408 | static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi) |
1da177e4 | 1409 | { |
86e07d34 | 1410 | struct snd_usb_midi* umidi = rmidi->private_data; |
1da177e4 LT |
1411 | snd_usbmidi_free(umidi); |
1412 | } | |
1413 | ||
86e07d34 | 1414 | static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi, |
21af7d8c | 1415 | int stream, int number) |
1da177e4 LT |
1416 | { |
1417 | struct list_head* list; | |
1418 | ||
1419 | list_for_each(list, &umidi->rmidi->streams[stream].substreams) { | |
86e07d34 | 1420 | struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list); |
1da177e4 LT |
1421 | if (substream->number == number) |
1422 | return substream; | |
1423 | } | |
1424 | return NULL; | |
1425 | } | |
1426 | ||
1427 | /* | |
1428 | * This list specifies names for ports that do not fit into the standard | |
1429 | * "(product) MIDI (n)" schema because they aren't external MIDI ports, | |
1430 | * such as internal control or synthesizer ports. | |
1431 | */ | |
a7b928ac | 1432 | static struct port_info { |
27d10f56 | 1433 | u32 id; |
a7b928ac CL |
1434 | short int port; |
1435 | short int voices; | |
1436 | const char *name; | |
1437 | unsigned int seq_flags; | |
1438 | } snd_usbmidi_port_info[] = { | |
1439 | #define PORT_INFO(vendor, product, num, name_, voices_, flags) \ | |
1440 | { .id = USB_ID(vendor, product), \ | |
1441 | .port = num, .voices = voices_, \ | |
1442 | .name = name_, .seq_flags = flags } | |
1443 | #define EXTERNAL_PORT(vendor, product, num, name) \ | |
1444 | PORT_INFO(vendor, product, num, name, 0, \ | |
1445 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ | |
1446 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ | |
1447 | SNDRV_SEQ_PORT_TYPE_PORT) | |
1448 | #define CONTROL_PORT(vendor, product, num, name) \ | |
1449 | PORT_INFO(vendor, product, num, name, 0, \ | |
1450 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ | |
1451 | SNDRV_SEQ_PORT_TYPE_HARDWARE) | |
1452 | #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \ | |
1453 | PORT_INFO(vendor, product, num, name, voices, \ | |
1454 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ | |
1455 | SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ | |
1456 | SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ | |
1457 | SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ | |
1458 | SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ | |
1459 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ | |
1460 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) | |
1461 | #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \ | |
1462 | PORT_INFO(vendor, product, num, name, voices, \ | |
1463 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ | |
1464 | SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ | |
1465 | SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ | |
1466 | SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ | |
1467 | SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ | |
1468 | SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \ | |
1469 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ | |
1470 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) | |
1da177e4 | 1471 | /* Roland UA-100 */ |
a7b928ac | 1472 | CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"), |
1da177e4 | 1473 | /* Roland SC-8850 */ |
a7b928ac CL |
1474 | SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128), |
1475 | SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128), | |
1476 | SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128), | |
1477 | SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128), | |
1478 | EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"), | |
1479 | EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"), | |
1da177e4 | 1480 | /* Roland U-8 */ |
a7b928ac CL |
1481 | EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"), |
1482 | CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"), | |
1da177e4 | 1483 | /* Roland SC-8820 */ |
a7b928ac CL |
1484 | SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64), |
1485 | SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64), | |
1486 | EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"), | |
1da177e4 | 1487 | /* Roland SK-500 */ |
a7b928ac CL |
1488 | SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64), |
1489 | SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64), | |
1490 | EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"), | |
1da177e4 | 1491 | /* Roland SC-D70 */ |
a7b928ac CL |
1492 | SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64), |
1493 | SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64), | |
1494 | EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"), | |
1da177e4 | 1495 | /* Edirol UM-880 */ |
a7b928ac | 1496 | CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"), |
1da177e4 | 1497 | /* Edirol SD-90 */ |
a7b928ac CL |
1498 | ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128), |
1499 | ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128), | |
1500 | EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"), | |
1501 | EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"), | |
1da177e4 | 1502 | /* Edirol UM-550 */ |
a7b928ac | 1503 | CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"), |
1da177e4 | 1504 | /* Edirol SD-20 */ |
a7b928ac CL |
1505 | ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64), |
1506 | ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64), | |
1507 | EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"), | |
1da177e4 | 1508 | /* Edirol SD-80 */ |
a7b928ac CL |
1509 | ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128), |
1510 | ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128), | |
1511 | EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"), | |
1512 | EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"), | |
1da177e4 | 1513 | /* Edirol UA-700 */ |
a7b928ac CL |
1514 | EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"), |
1515 | CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"), | |
1da177e4 | 1516 | /* Roland VariOS */ |
a7b928ac CL |
1517 | EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"), |
1518 | EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"), | |
1519 | EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"), | |
1da177e4 | 1520 | /* Edirol PCR */ |
a7b928ac CL |
1521 | EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"), |
1522 | EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"), | |
1523 | EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"), | |
1da177e4 | 1524 | /* BOSS GS-10 */ |
a7b928ac CL |
1525 | EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"), |
1526 | CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"), | |
1da177e4 | 1527 | /* Edirol UA-1000 */ |
a7b928ac CL |
1528 | EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"), |
1529 | CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"), | |
1da177e4 | 1530 | /* Edirol UR-80 */ |
a7b928ac CL |
1531 | EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"), |
1532 | EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"), | |
1533 | EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"), | |
1da177e4 | 1534 | /* Edirol PCR-A */ |
a7b928ac CL |
1535 | EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"), |
1536 | EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"), | |
1537 | EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"), | |
7c79b768 | 1538 | /* Edirol UM-3EX */ |
a7b928ac | 1539 | CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"), |
1da177e4 | 1540 | /* M-Audio MidiSport 8x8 */ |
a7b928ac CL |
1541 | CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"), |
1542 | CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"), | |
1da177e4 | 1543 | /* MOTU Fastlane */ |
a7b928ac CL |
1544 | EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"), |
1545 | EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"), | |
1da177e4 | 1546 | /* Emagic Unitor8/AMT8/MT4 */ |
a7b928ac CL |
1547 | EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"), |
1548 | EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"), | |
1549 | EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"), | |
4434ade8 KF |
1550 | /* Akai MPD16 */ |
1551 | CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"), | |
1552 | PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0, | |
1553 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | | |
1554 | SNDRV_SEQ_PORT_TYPE_HARDWARE), | |
d39e82db SA |
1555 | /* Access Music Virus TI */ |
1556 | EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"), | |
1557 | PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0, | |
1558 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | | |
1559 | SNDRV_SEQ_PORT_TYPE_HARDWARE | | |
1560 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER), | |
1da177e4 LT |
1561 | }; |
1562 | ||
a7b928ac CL |
1563 | static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number) |
1564 | { | |
1565 | int i; | |
1566 | ||
1567 | for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) { | |
d82af9f9 | 1568 | if (snd_usbmidi_port_info[i].id == umidi->usb_id && |
a7b928ac CL |
1569 | snd_usbmidi_port_info[i].port == number) |
1570 | return &snd_usbmidi_port_info[i]; | |
1571 | } | |
1572 | return NULL; | |
1573 | } | |
1574 | ||
1575 | static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number, | |
1576 | struct snd_seq_port_info *seq_port_info) | |
1577 | { | |
1578 | struct snd_usb_midi *umidi = rmidi->private_data; | |
1579 | struct port_info *port_info; | |
1580 | ||
1581 | /* TODO: read port flags from descriptors */ | |
1582 | port_info = find_port_info(umidi, number); | |
1583 | if (port_info) { | |
1584 | seq_port_info->type = port_info->seq_flags; | |
1585 | seq_port_info->midi_voices = port_info->voices; | |
1586 | } | |
1587 | } | |
1588 | ||
86e07d34 | 1589 | static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi, |
1da177e4 | 1590 | int stream, int number, |
86e07d34 | 1591 | struct snd_rawmidi_substream ** rsubstream) |
1da177e4 | 1592 | { |
a7b928ac | 1593 | struct port_info *port_info; |
1da177e4 LT |
1594 | const char *name_format; |
1595 | ||
86e07d34 | 1596 | struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number); |
1da177e4 LT |
1597 | if (!substream) { |
1598 | snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number); | |
1599 | return; | |
1600 | } | |
1601 | ||
1602 | /* TODO: read port name from jack descriptor */ | |
a7b928ac CL |
1603 | port_info = find_port_info(umidi, number); |
1604 | name_format = port_info ? port_info->name : "%s MIDI %d"; | |
1da177e4 | 1605 | snprintf(substream->name, sizeof(substream->name), |
d82af9f9 | 1606 | name_format, umidi->card->shortname, number + 1); |
1da177e4 LT |
1607 | |
1608 | *rsubstream = substream; | |
1609 | } | |
1610 | ||
1611 | /* | |
1612 | * Creates the endpoints and their ports. | |
1613 | */ | |
86e07d34 TI |
1614 | static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi, |
1615 | struct snd_usb_midi_endpoint_info* endpoints) | |
1da177e4 LT |
1616 | { |
1617 | int i, j, err; | |
1618 | int out_ports = 0, in_ports = 0; | |
1619 | ||
1620 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
1621 | if (endpoints[i].out_cables) { | |
1622 | err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i], | |
1623 | &umidi->endpoints[i]); | |
1624 | if (err < 0) | |
1625 | return err; | |
1626 | } | |
1627 | if (endpoints[i].in_cables) { | |
1628 | err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i], | |
1629 | &umidi->endpoints[i]); | |
1630 | if (err < 0) | |
1631 | return err; | |
1632 | } | |
1633 | ||
1634 | for (j = 0; j < 0x10; ++j) { | |
1635 | if (endpoints[i].out_cables & (1 << j)) { | |
1636 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports, | |
1637 | &umidi->endpoints[i].out->ports[j].substream); | |
1638 | ++out_ports; | |
1639 | } | |
1640 | if (endpoints[i].in_cables & (1 << j)) { | |
1641 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports, | |
1642 | &umidi->endpoints[i].in->ports[j].substream); | |
1643 | ++in_ports; | |
1644 | } | |
1645 | } | |
1646 | } | |
1647 | snd_printdd(KERN_INFO "created %d output and %d input ports\n", | |
1648 | out_ports, in_ports); | |
1649 | return 0; | |
1650 | } | |
1651 | ||
1652 | /* | |
1653 | * Returns MIDIStreaming device capabilities. | |
1654 | */ | |
86e07d34 TI |
1655 | static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi, |
1656 | struct snd_usb_midi_endpoint_info* endpoints) | |
1da177e4 LT |
1657 | { |
1658 | struct usb_interface* intf; | |
1659 | struct usb_host_interface *hostif; | |
1660 | struct usb_interface_descriptor* intfd; | |
1661 | struct usb_ms_header_descriptor* ms_header; | |
1662 | struct usb_host_endpoint *hostep; | |
1663 | struct usb_endpoint_descriptor* ep; | |
1664 | struct usb_ms_endpoint_descriptor* ms_ep; | |
1665 | int i, epidx; | |
1666 | ||
1667 | intf = umidi->iface; | |
1668 | if (!intf) | |
1669 | return -ENXIO; | |
1670 | hostif = &intf->altsetting[0]; | |
1671 | intfd = get_iface_desc(hostif); | |
1672 | ms_header = (struct usb_ms_header_descriptor*)hostif->extra; | |
1673 | if (hostif->extralen >= 7 && | |
1674 | ms_header->bLength >= 7 && | |
1675 | ms_header->bDescriptorType == USB_DT_CS_INTERFACE && | |
de48c7bc | 1676 | ms_header->bDescriptorSubtype == UAC_HEADER) |
1da177e4 LT |
1677 | snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n", |
1678 | ms_header->bcdMSC[1], ms_header->bcdMSC[0]); | |
1679 | else | |
1680 | snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n"); | |
1681 | ||
1682 | epidx = 0; | |
1683 | for (i = 0; i < intfd->bNumEndpoints; ++i) { | |
1684 | hostep = &hostif->endpoint[i]; | |
1685 | ep = get_ep_desc(hostep); | |
913ae5a2 | 1686 | if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep)) |
1da177e4 LT |
1687 | continue; |
1688 | ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra; | |
1689 | if (hostep->extralen < 4 || | |
1690 | ms_ep->bLength < 4 || | |
1691 | ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT || | |
de48c7bc | 1692 | ms_ep->bDescriptorSubtype != UAC_MS_GENERAL) |
1da177e4 | 1693 | continue; |
42a6e66f | 1694 | if (usb_endpoint_dir_out(ep)) { |
1da177e4 LT |
1695 | if (endpoints[epidx].out_ep) { |
1696 | if (++epidx >= MIDI_MAX_ENDPOINTS) { | |
1697 | snd_printk(KERN_WARNING "too many endpoints\n"); | |
1698 | break; | |
1699 | } | |
1700 | } | |
42a6e66f JL |
1701 | endpoints[epidx].out_ep = usb_endpoint_num(ep); |
1702 | if (usb_endpoint_xfer_int(ep)) | |
1da177e4 | 1703 | endpoints[epidx].out_interval = ep->bInterval; |
d82af9f9 | 1704 | else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) |
56162aab CL |
1705 | /* |
1706 | * Low speed bulk transfers don't exist, so | |
1707 | * force interrupt transfers for devices like | |
1708 | * ESI MIDI Mate that try to use them anyway. | |
1709 | */ | |
1710 | endpoints[epidx].out_interval = 1; | |
1da177e4 LT |
1711 | endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; |
1712 | snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", | |
1713 | ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); | |
1714 | } else { | |
1715 | if (endpoints[epidx].in_ep) { | |
1716 | if (++epidx >= MIDI_MAX_ENDPOINTS) { | |
1717 | snd_printk(KERN_WARNING "too many endpoints\n"); | |
1718 | break; | |
1719 | } | |
1720 | } | |
42a6e66f JL |
1721 | endpoints[epidx].in_ep = usb_endpoint_num(ep); |
1722 | if (usb_endpoint_xfer_int(ep)) | |
1da177e4 | 1723 | endpoints[epidx].in_interval = ep->bInterval; |
d82af9f9 | 1724 | else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) |
56162aab | 1725 | endpoints[epidx].in_interval = 1; |
1da177e4 LT |
1726 | endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; |
1727 | snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", | |
1728 | ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); | |
1729 | } | |
1730 | } | |
1731 | return 0; | |
1732 | } | |
1733 | ||
96f61d9a CL |
1734 | static int roland_load_info(struct snd_kcontrol *kcontrol, |
1735 | struct snd_ctl_elem_info *info) | |
1736 | { | |
1737 | static const char *const names[] = { "High Load", "Light Load" }; | |
1738 | ||
2a1803a7 | 1739 | return snd_ctl_enum_info(info, 1, 2, names); |
96f61d9a CL |
1740 | } |
1741 | ||
1742 | static int roland_load_get(struct snd_kcontrol *kcontrol, | |
1743 | struct snd_ctl_elem_value *value) | |
1744 | { | |
1745 | value->value.enumerated.item[0] = kcontrol->private_value; | |
1746 | return 0; | |
1747 | } | |
1748 | ||
1749 | static int roland_load_put(struct snd_kcontrol *kcontrol, | |
1750 | struct snd_ctl_elem_value *value) | |
1751 | { | |
1752 | struct snd_usb_midi* umidi = kcontrol->private_data; | |
1753 | int changed; | |
1754 | ||
1755 | if (value->value.enumerated.item[0] > 1) | |
1756 | return -EINVAL; | |
1757 | mutex_lock(&umidi->mutex); | |
1758 | changed = value->value.enumerated.item[0] != kcontrol->private_value; | |
1759 | if (changed) | |
1760 | kcontrol->private_value = value->value.enumerated.item[0]; | |
1761 | mutex_unlock(&umidi->mutex); | |
1762 | return changed; | |
1763 | } | |
1764 | ||
1765 | static struct snd_kcontrol_new roland_load_ctl = { | |
1766 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1767 | .name = "MIDI Input Mode", | |
1768 | .info = roland_load_info, | |
1769 | .get = roland_load_get, | |
1770 | .put = roland_load_put, | |
1771 | .private_value = 1, | |
1772 | }; | |
1773 | ||
1da177e4 LT |
1774 | /* |
1775 | * On Roland devices, use the second alternate setting to be able to use | |
1776 | * the interrupt input endpoint. | |
1777 | */ | |
86e07d34 | 1778 | static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi) |
1da177e4 LT |
1779 | { |
1780 | struct usb_interface* intf; | |
1781 | struct usb_host_interface *hostif; | |
1782 | struct usb_interface_descriptor* intfd; | |
1783 | ||
1784 | intf = umidi->iface; | |
1785 | if (!intf || intf->num_altsetting != 2) | |
1786 | return; | |
1787 | ||
1788 | hostif = &intf->altsetting[1]; | |
1789 | intfd = get_iface_desc(hostif); | |
1790 | if (intfd->bNumEndpoints != 2 || | |
1791 | (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK || | |
1792 | (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) | |
1793 | return; | |
1794 | ||
1795 | snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n", | |
1796 | intfd->bAlternateSetting); | |
d82af9f9 | 1797 | usb_set_interface(umidi->dev, intfd->bInterfaceNumber, |
1da177e4 | 1798 | intfd->bAlternateSetting); |
96f61d9a CL |
1799 | |
1800 | umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi); | |
d82af9f9 | 1801 | if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0) |
96f61d9a | 1802 | umidi->roland_load_ctl = NULL; |
1da177e4 LT |
1803 | } |
1804 | ||
1805 | /* | |
1806 | * Try to find any usable endpoints in the interface. | |
1807 | */ | |
86e07d34 TI |
1808 | static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi, |
1809 | struct snd_usb_midi_endpoint_info* endpoint, | |
1da177e4 LT |
1810 | int max_endpoints) |
1811 | { | |
1812 | struct usb_interface* intf; | |
1813 | struct usb_host_interface *hostif; | |
1814 | struct usb_interface_descriptor* intfd; | |
1815 | struct usb_endpoint_descriptor* epd; | |
1816 | int i, out_eps = 0, in_eps = 0; | |
1817 | ||
d82af9f9 | 1818 | if (USB_ID_VENDOR(umidi->usb_id) == 0x0582) |
1da177e4 LT |
1819 | snd_usbmidi_switch_roland_altsetting(umidi); |
1820 | ||
c1ab5d59 | 1821 | if (endpoint[0].out_ep || endpoint[0].in_ep) |
21af7d8c | 1822 | return 0; |
c1ab5d59 | 1823 | |
1da177e4 LT |
1824 | intf = umidi->iface; |
1825 | if (!intf || intf->num_altsetting < 1) | |
1826 | return -ENOENT; | |
1827 | hostif = intf->cur_altsetting; | |
1828 | intfd = get_iface_desc(hostif); | |
1829 | ||
1830 | for (i = 0; i < intfd->bNumEndpoints; ++i) { | |
1831 | epd = get_endpoint(hostif, i); | |
913ae5a2 JL |
1832 | if (!usb_endpoint_xfer_bulk(epd) && |
1833 | !usb_endpoint_xfer_int(epd)) | |
1da177e4 LT |
1834 | continue; |
1835 | if (out_eps < max_endpoints && | |
42a6e66f JL |
1836 | usb_endpoint_dir_out(epd)) { |
1837 | endpoint[out_eps].out_ep = usb_endpoint_num(epd); | |
1838 | if (usb_endpoint_xfer_int(epd)) | |
1da177e4 LT |
1839 | endpoint[out_eps].out_interval = epd->bInterval; |
1840 | ++out_eps; | |
1841 | } | |
1842 | if (in_eps < max_endpoints && | |
42a6e66f JL |
1843 | usb_endpoint_dir_in(epd)) { |
1844 | endpoint[in_eps].in_ep = usb_endpoint_num(epd); | |
1845 | if (usb_endpoint_xfer_int(epd)) | |
1da177e4 LT |
1846 | endpoint[in_eps].in_interval = epd->bInterval; |
1847 | ++in_eps; | |
1848 | } | |
1849 | } | |
1850 | return (out_eps || in_eps) ? 0 : -ENOENT; | |
1851 | } | |
1852 | ||
1853 | /* | |
1854 | * Detects the endpoints for one-port-per-endpoint protocols. | |
1855 | */ | |
86e07d34 TI |
1856 | static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi, |
1857 | struct snd_usb_midi_endpoint_info* endpoints) | |
1da177e4 LT |
1858 | { |
1859 | int err, i; | |
21af7d8c | 1860 | |
1da177e4 LT |
1861 | err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS); |
1862 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
1863 | if (endpoints[i].out_ep) | |
1864 | endpoints[i].out_cables = 0x0001; | |
1865 | if (endpoints[i].in_ep) | |
1866 | endpoints[i].in_cables = 0x0001; | |
1867 | } | |
1868 | return err; | |
1869 | } | |
1870 | ||
1871 | /* | |
1872 | * Detects the endpoints and ports of Yamaha devices. | |
1873 | */ | |
86e07d34 TI |
1874 | static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi, |
1875 | struct snd_usb_midi_endpoint_info* endpoint) | |
1da177e4 LT |
1876 | { |
1877 | struct usb_interface* intf; | |
1878 | struct usb_host_interface *hostif; | |
1879 | struct usb_interface_descriptor* intfd; | |
1880 | uint8_t* cs_desc; | |
1881 | ||
1882 | intf = umidi->iface; | |
1883 | if (!intf) | |
1884 | return -ENOENT; | |
1885 | hostif = intf->altsetting; | |
1886 | intfd = get_iface_desc(hostif); | |
1887 | if (intfd->bNumEndpoints < 1) | |
1888 | return -ENOENT; | |
1889 | ||
1890 | /* | |
1891 | * For each port there is one MIDI_IN/OUT_JACK descriptor, not | |
1892 | * necessarily with any useful contents. So simply count 'em. | |
1893 | */ | |
1894 | for (cs_desc = hostif->extra; | |
1895 | cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; | |
1896 | cs_desc += cs_desc[0]) { | |
c4a87ef4 | 1897 | if (cs_desc[1] == USB_DT_CS_INTERFACE) { |
de48c7bc | 1898 | if (cs_desc[2] == UAC_MIDI_IN_JACK) |
1da177e4 | 1899 | endpoint->in_cables = (endpoint->in_cables << 1) | 1; |
de48c7bc | 1900 | else if (cs_desc[2] == UAC_MIDI_OUT_JACK) |
1da177e4 LT |
1901 | endpoint->out_cables = (endpoint->out_cables << 1) | 1; |
1902 | } | |
1903 | } | |
1904 | if (!endpoint->in_cables && !endpoint->out_cables) | |
1905 | return -ENOENT; | |
1906 | ||
1907 | return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); | |
1908 | } | |
1909 | ||
1910 | /* | |
1911 | * Creates the endpoints and their ports for Midiman devices. | |
1912 | */ | |
86e07d34 TI |
1913 | static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi, |
1914 | struct snd_usb_midi_endpoint_info* endpoint) | |
1da177e4 | 1915 | { |
86e07d34 | 1916 | struct snd_usb_midi_endpoint_info ep_info; |
1da177e4 LT |
1917 | struct usb_interface* intf; |
1918 | struct usb_host_interface *hostif; | |
1919 | struct usb_interface_descriptor* intfd; | |
1920 | struct usb_endpoint_descriptor* epd; | |
1921 | int cable, err; | |
1922 | ||
1923 | intf = umidi->iface; | |
1924 | if (!intf) | |
1925 | return -ENOENT; | |
1926 | hostif = intf->altsetting; | |
1927 | intfd = get_iface_desc(hostif); | |
1928 | /* | |
1929 | * The various MidiSport devices have more or less random endpoint | |
1930 | * numbers, so we have to identify the endpoints by their index in | |
1931 | * the descriptor array, like the driver for that other OS does. | |
1932 | * | |
1933 | * There is one interrupt input endpoint for all input ports, one | |
1934 | * bulk output endpoint for even-numbered ports, and one for odd- | |
1935 | * numbered ports. Both bulk output endpoints have corresponding | |
1936 | * input bulk endpoints (at indices 1 and 3) which aren't used. | |
1937 | */ | |
1938 | if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) { | |
1939 | snd_printdd(KERN_ERR "not enough endpoints\n"); | |
1940 | return -ENOENT; | |
1941 | } | |
1942 | ||
1943 | epd = get_endpoint(hostif, 0); | |
913ae5a2 | 1944 | if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) { |
1da177e4 LT |
1945 | snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n"); |
1946 | return -ENXIO; | |
1947 | } | |
1948 | epd = get_endpoint(hostif, 2); | |
913ae5a2 | 1949 | if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) { |
1da177e4 LT |
1950 | snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n"); |
1951 | return -ENXIO; | |
1952 | } | |
1953 | if (endpoint->out_cables > 0x0001) { | |
1954 | epd = get_endpoint(hostif, 4); | |
913ae5a2 JL |
1955 | if (!usb_endpoint_dir_out(epd) || |
1956 | !usb_endpoint_xfer_bulk(epd)) { | |
1da177e4 LT |
1957 | snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n"); |
1958 | return -ENXIO; | |
1959 | } | |
1960 | } | |
1961 | ||
1962 | ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
e156ac4c | 1963 | ep_info.out_interval = 0; |
1da177e4 LT |
1964 | ep_info.out_cables = endpoint->out_cables & 0x5555; |
1965 | err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); | |
1966 | if (err < 0) | |
1967 | return err; | |
1968 | ||
1969 | ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1970 | ep_info.in_interval = get_endpoint(hostif, 0)->bInterval; | |
1971 | ep_info.in_cables = endpoint->in_cables; | |
1972 | err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); | |
1973 | if (err < 0) | |
1974 | return err; | |
1975 | ||
1976 | if (endpoint->out_cables > 0x0001) { | |
1977 | ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1978 | ep_info.out_cables = endpoint->out_cables & 0xaaaa; | |
1979 | err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]); | |
1980 | if (err < 0) | |
1981 | return err; | |
1982 | } | |
1983 | ||
1984 | for (cable = 0; cable < 0x10; ++cable) { | |
1985 | if (endpoint->out_cables & (1 << cable)) | |
1986 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable, | |
1987 | &umidi->endpoints[cable & 1].out->ports[cable].substream); | |
1988 | if (endpoint->in_cables & (1 << cable)) | |
1989 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable, | |
1990 | &umidi->endpoints[0].in->ports[cable].substream); | |
1991 | } | |
1992 | return 0; | |
1993 | } | |
1994 | ||
a7b928ac CL |
1995 | static struct snd_rawmidi_global_ops snd_usbmidi_ops = { |
1996 | .get_port_info = snd_usbmidi_get_port_info, | |
1997 | }; | |
1998 | ||
86e07d34 | 1999 | static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi, |
1da177e4 LT |
2000 | int out_ports, int in_ports) |
2001 | { | |
86e07d34 | 2002 | struct snd_rawmidi *rmidi; |
1da177e4 LT |
2003 | int err; |
2004 | ||
d82af9f9 CL |
2005 | err = snd_rawmidi_new(umidi->card, "USB MIDI", |
2006 | umidi->next_midi_device++, | |
1da177e4 LT |
2007 | out_ports, in_ports, &rmidi); |
2008 | if (err < 0) | |
2009 | return err; | |
d82af9f9 | 2010 | strcpy(rmidi->name, umidi->card->shortname); |
1da177e4 LT |
2011 | rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | |
2012 | SNDRV_RAWMIDI_INFO_INPUT | | |
2013 | SNDRV_RAWMIDI_INFO_DUPLEX; | |
a7b928ac | 2014 | rmidi->ops = &snd_usbmidi_ops; |
1da177e4 LT |
2015 | rmidi->private_data = umidi; |
2016 | rmidi->private_free = snd_usbmidi_rawmidi_free; | |
2017 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops); | |
2018 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops); | |
2019 | ||
2020 | umidi->rmidi = rmidi; | |
2021 | return 0; | |
2022 | } | |
2023 | ||
2024 | /* | |
2025 | * Temporarily stop input. | |
2026 | */ | |
2027 | void snd_usbmidi_input_stop(struct list_head* p) | |
2028 | { | |
86e07d34 | 2029 | struct snd_usb_midi* umidi; |
4773d1fb | 2030 | unsigned int i, j; |
1da177e4 | 2031 | |
86e07d34 | 2032 | umidi = list_entry(p, struct snd_usb_midi, list); |
1da177e4 | 2033 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
86e07d34 | 2034 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
1da177e4 | 2035 | if (ep->in) |
4773d1fb CL |
2036 | for (j = 0; j < INPUT_URBS; ++j) |
2037 | usb_kill_urb(ep->in->urbs[j]); | |
1da177e4 LT |
2038 | } |
2039 | } | |
2040 | ||
86e07d34 | 2041 | static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep) |
1da177e4 | 2042 | { |
4773d1fb CL |
2043 | unsigned int i; |
2044 | ||
2045 | if (!ep) | |
2046 | return; | |
2047 | for (i = 0; i < INPUT_URBS; ++i) { | |
2048 | struct urb* urb = ep->urbs[i]; | |
d82af9f9 | 2049 | urb->dev = ep->umidi->dev; |
1da177e4 LT |
2050 | snd_usbmidi_submit_urb(urb, GFP_KERNEL); |
2051 | } | |
2052 | } | |
2053 | ||
2054 | /* | |
2055 | * Resume input after a call to snd_usbmidi_input_stop(). | |
2056 | */ | |
2057 | void snd_usbmidi_input_start(struct list_head* p) | |
2058 | { | |
86e07d34 | 2059 | struct snd_usb_midi* umidi; |
1da177e4 LT |
2060 | int i; |
2061 | ||
86e07d34 | 2062 | umidi = list_entry(p, struct snd_usb_midi, list); |
1da177e4 LT |
2063 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) |
2064 | snd_usbmidi_input_start_ep(umidi->endpoints[i].in); | |
2065 | } | |
2066 | ||
2067 | /* | |
2068 | * Creates and registers everything needed for a MIDI streaming interface. | |
2069 | */ | |
d82af9f9 CL |
2070 | int snd_usbmidi_create(struct snd_card *card, |
2071 | struct usb_interface* iface, | |
2072 | struct list_head *midi_list, | |
2073 | const struct snd_usb_audio_quirk* quirk) | |
1da177e4 | 2074 | { |
86e07d34 TI |
2075 | struct snd_usb_midi* umidi; |
2076 | struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS]; | |
1da177e4 LT |
2077 | int out_ports, in_ports; |
2078 | int i, err; | |
2079 | ||
561b220a | 2080 | umidi = kzalloc(sizeof(*umidi), GFP_KERNEL); |
1da177e4 LT |
2081 | if (!umidi) |
2082 | return -ENOMEM; | |
d82af9f9 CL |
2083 | umidi->dev = interface_to_usbdev(iface); |
2084 | umidi->card = card; | |
1da177e4 LT |
2085 | umidi->iface = iface; |
2086 | umidi->quirk = quirk; | |
2087 | umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; | |
c8846970 | 2088 | init_timer(&umidi->error_timer); |
c0792e00 | 2089 | spin_lock_init(&umidi->disc_lock); |
96f61d9a | 2090 | mutex_init(&umidi->mutex); |
d82af9f9 CL |
2091 | umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor), |
2092 | le16_to_cpu(umidi->dev->descriptor.idProduct)); | |
c8846970 CL |
2093 | umidi->error_timer.function = snd_usbmidi_error_timer; |
2094 | umidi->error_timer.data = (unsigned long)umidi; | |
1da177e4 LT |
2095 | |
2096 | /* detect the endpoint(s) to use */ | |
2097 | memset(endpoints, 0, sizeof(endpoints)); | |
d1bda045 CL |
2098 | switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) { |
2099 | case QUIRK_MIDI_STANDARD_INTERFACE: | |
1da177e4 | 2100 | err = snd_usbmidi_get_ms_info(umidi, endpoints); |
d82af9f9 | 2101 | if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */ |
d05cc104 CL |
2102 | umidi->usb_protocol_ops = |
2103 | &snd_usbmidi_maudio_broken_running_status_ops; | |
d1bda045 | 2104 | break; |
030a07e4 KW |
2105 | case QUIRK_MIDI_US122L: |
2106 | umidi->usb_protocol_ops = &snd_usbmidi_122l_ops; | |
2107 | /* fall through */ | |
d1bda045 CL |
2108 | case QUIRK_MIDI_FIXED_ENDPOINT: |
2109 | memcpy(&endpoints[0], quirk->data, | |
86e07d34 | 2110 | sizeof(struct snd_usb_midi_endpoint_info)); |
d1bda045 CL |
2111 | err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); |
2112 | break; | |
2113 | case QUIRK_MIDI_YAMAHA: | |
2114 | err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]); | |
2115 | break; | |
2116 | case QUIRK_MIDI_MIDIMAN: | |
2117 | umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops; | |
2118 | memcpy(&endpoints[0], quirk->data, | |
86e07d34 | 2119 | sizeof(struct snd_usb_midi_endpoint_info)); |
d1bda045 CL |
2120 | err = 0; |
2121 | break; | |
2122 | case QUIRK_MIDI_NOVATION: | |
2123 | umidi->usb_protocol_ops = &snd_usbmidi_novation_ops; | |
2124 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); | |
2125 | break; | |
c7f57216 | 2126 | case QUIRK_MIDI_RAW_BYTES: |
d1bda045 | 2127 | umidi->usb_protocol_ops = &snd_usbmidi_raw_ops; |
55de5ef9 CL |
2128 | /* |
2129 | * Interface 1 contains isochronous endpoints, but with the same | |
2130 | * numbers as in interface 0. Since it is interface 1 that the | |
2131 | * USB core has most recently seen, these descriptors are now | |
2132 | * associated with the endpoint numbers. This will foul up our | |
2133 | * attempts to submit bulk/interrupt URBs to the endpoints in | |
2134 | * interface 0, so we have to make sure that the USB core looks | |
2135 | * again at interface 0 by calling usb_set_interface() on it. | |
2136 | */ | |
c7f57216 CL |
2137 | if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */ |
2138 | usb_set_interface(umidi->dev, 0, 0); | |
d1bda045 CL |
2139 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); |
2140 | break; | |
2141 | case QUIRK_MIDI_EMAGIC: | |
2142 | umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops; | |
2143 | memcpy(&endpoints[0], quirk->data, | |
86e07d34 | 2144 | sizeof(struct snd_usb_midi_endpoint_info)); |
d1bda045 CL |
2145 | err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); |
2146 | break; | |
cc7a59bd | 2147 | case QUIRK_MIDI_CME: |
61870aed | 2148 | umidi->usb_protocol_ops = &snd_usbmidi_cme_ops; |
d1bda045 CL |
2149 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); |
2150 | break; | |
4434ade8 KF |
2151 | case QUIRK_MIDI_AKAI: |
2152 | umidi->usb_protocol_ops = &snd_usbmidi_akai_ops; | |
2153 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); | |
2154 | /* endpoint 1 is input-only */ | |
2155 | endpoints[1].out_cables = 0; | |
2156 | break; | |
d1bda045 CL |
2157 | default: |
2158 | snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); | |
2159 | err = -ENXIO; | |
2160 | break; | |
1da177e4 LT |
2161 | } |
2162 | if (err < 0) { | |
2163 | kfree(umidi); | |
2164 | return err; | |
2165 | } | |
2166 | ||
2167 | /* create rawmidi device */ | |
2168 | out_ports = 0; | |
2169 | in_ports = 0; | |
2170 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
fbc54391 AM |
2171 | out_ports += hweight16(endpoints[i].out_cables); |
2172 | in_ports += hweight16(endpoints[i].in_cables); | |
1da177e4 LT |
2173 | } |
2174 | err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports); | |
2175 | if (err < 0) { | |
2176 | kfree(umidi); | |
2177 | return err; | |
2178 | } | |
2179 | ||
2180 | /* create endpoint/port structures */ | |
2181 | if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN) | |
2182 | err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]); | |
2183 | else | |
2184 | err = snd_usbmidi_create_endpoints(umidi, endpoints); | |
2185 | if (err < 0) { | |
2186 | snd_usbmidi_free(umidi); | |
2187 | return err; | |
2188 | } | |
2189 | ||
d82af9f9 | 2190 | list_add_tail(&umidi->list, midi_list); |
1da177e4 LT |
2191 | |
2192 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) | |
2193 | snd_usbmidi_input_start_ep(umidi->endpoints[i].in); | |
2194 | return 0; | |
2195 | } | |
2196 | ||
d82af9f9 | 2197 | EXPORT_SYMBOL(snd_usbmidi_create); |
1da177e4 LT |
2198 | EXPORT_SYMBOL(snd_usbmidi_input_stop); |
2199 | EXPORT_SYMBOL(snd_usbmidi_input_start); | |
2200 | EXPORT_SYMBOL(snd_usbmidi_disconnect); |