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