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