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