Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * usbmidi.c - ALSA USB MIDI driver | |
3 | * | |
4 | * Copyright (c) 2002-2005 Clemens Ladisch | |
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 | ||
38 | #include <sound/driver.h> | |
39 | #include <linux/kernel.h> | |
40 | #include <linux/types.h> | |
41 | #include <linux/bitops.h> | |
42 | #include <linux/interrupt.h> | |
43 | #include <linux/spinlock.h> | |
44 | #include <linux/string.h> | |
45 | #include <linux/init.h> | |
46 | #include <linux/slab.h> | |
c8846970 | 47 | #include <linux/timer.h> |
1da177e4 LT |
48 | #include <linux/usb.h> |
49 | #include <sound/core.h> | |
1da177e4 LT |
50 | #include <sound/rawmidi.h> |
51 | #include "usbaudio.h" | |
52 | ||
53 | ||
54 | /* | |
55 | * define this to log all USB packets | |
56 | */ | |
57 | /* #define DUMP_PACKETS */ | |
58 | ||
c8846970 CL |
59 | /* |
60 | * how long to wait after some USB errors, so that khubd can disconnect() us | |
61 | * without too many spurious errors | |
62 | */ | |
63 | #define ERROR_DELAY_JIFFIES (HZ / 10) | |
64 | ||
1da177e4 LT |
65 | |
66 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); | |
67 | MODULE_DESCRIPTION("USB Audio/MIDI helper module"); | |
68 | MODULE_LICENSE("Dual BSD/GPL"); | |
69 | ||
70 | ||
71 | struct usb_ms_header_descriptor { | |
72 | __u8 bLength; | |
73 | __u8 bDescriptorType; | |
74 | __u8 bDescriptorSubtype; | |
75 | __u8 bcdMSC[2]; | |
76 | __le16 wTotalLength; | |
77 | } __attribute__ ((packed)); | |
78 | ||
79 | struct usb_ms_endpoint_descriptor { | |
80 | __u8 bLength; | |
81 | __u8 bDescriptorType; | |
82 | __u8 bDescriptorSubtype; | |
83 | __u8 bNumEmbMIDIJack; | |
84 | __u8 baAssocJackID[0]; | |
85 | } __attribute__ ((packed)); | |
86 | ||
87 | typedef struct snd_usb_midi snd_usb_midi_t; | |
88 | typedef struct snd_usb_midi_endpoint snd_usb_midi_endpoint_t; | |
89 | typedef struct snd_usb_midi_out_endpoint snd_usb_midi_out_endpoint_t; | |
90 | typedef struct snd_usb_midi_in_endpoint snd_usb_midi_in_endpoint_t; | |
91 | typedef struct usbmidi_out_port usbmidi_out_port_t; | |
92 | typedef struct usbmidi_in_port usbmidi_in_port_t; | |
93 | ||
94 | struct usb_protocol_ops { | |
95 | void (*input)(snd_usb_midi_in_endpoint_t*, uint8_t*, int); | |
96 | void (*output)(snd_usb_midi_out_endpoint_t*); | |
97 | void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t); | |
98 | void (*init_out_endpoint)(snd_usb_midi_out_endpoint_t*); | |
99 | void (*finish_out_endpoint)(snd_usb_midi_out_endpoint_t*); | |
100 | }; | |
101 | ||
102 | struct snd_usb_midi { | |
103 | snd_usb_audio_t *chip; | |
104 | struct usb_interface *iface; | |
105 | const snd_usb_audio_quirk_t *quirk; | |
106 | snd_rawmidi_t* rmidi; | |
107 | struct usb_protocol_ops* usb_protocol_ops; | |
108 | struct list_head list; | |
c8846970 | 109 | struct timer_list error_timer; |
1da177e4 LT |
110 | |
111 | struct snd_usb_midi_endpoint { | |
112 | snd_usb_midi_out_endpoint_t *out; | |
113 | snd_usb_midi_in_endpoint_t *in; | |
114 | } endpoints[MIDI_MAX_ENDPOINTS]; | |
115 | unsigned long input_triggered; | |
116 | }; | |
117 | ||
118 | struct snd_usb_midi_out_endpoint { | |
119 | snd_usb_midi_t* umidi; | |
120 | struct urb* urb; | |
121 | int urb_active; | |
122 | int max_transfer; /* size of urb buffer */ | |
123 | struct tasklet_struct tasklet; | |
124 | ||
125 | spinlock_t buffer_lock; | |
126 | ||
127 | struct usbmidi_out_port { | |
128 | snd_usb_midi_out_endpoint_t* ep; | |
129 | snd_rawmidi_substream_t* substream; | |
130 | int active; | |
131 | uint8_t cable; /* cable number << 4 */ | |
132 | uint8_t state; | |
133 | #define STATE_UNKNOWN 0 | |
134 | #define STATE_1PARAM 1 | |
135 | #define STATE_2PARAM_1 2 | |
136 | #define STATE_2PARAM_2 3 | |
137 | #define STATE_SYSEX_0 4 | |
138 | #define STATE_SYSEX_1 5 | |
139 | #define STATE_SYSEX_2 6 | |
140 | uint8_t data[2]; | |
141 | } ports[0x10]; | |
142 | int current_port; | |
143 | }; | |
144 | ||
145 | struct snd_usb_midi_in_endpoint { | |
146 | snd_usb_midi_t* umidi; | |
147 | struct urb* urb; | |
148 | struct usbmidi_in_port { | |
149 | snd_rawmidi_substream_t* substream; | |
150 | } ports[0x10]; | |
c8846970 CL |
151 | u8 seen_f5; |
152 | u8 error_resubmit; | |
1da177e4 LT |
153 | int current_port; |
154 | }; | |
155 | ||
156 | static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep); | |
157 | ||
158 | static const uint8_t snd_usbmidi_cin_length[] = { | |
159 | 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1 | |
160 | }; | |
161 | ||
162 | /* | |
163 | * Submits the URB, with error handling. | |
164 | */ | |
55016f10 | 165 | static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags) |
1da177e4 LT |
166 | { |
167 | int err = usb_submit_urb(urb, flags); | |
168 | if (err < 0 && err != -ENODEV) | |
169 | snd_printk(KERN_ERR "usb_submit_urb: %d\n", err); | |
170 | return err; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Error handling for URB completion functions. | |
175 | */ | |
176 | static int snd_usbmidi_urb_error(int status) | |
177 | { | |
c8846970 CL |
178 | switch (status) { |
179 | /* manually unlinked, or device gone */ | |
180 | case -ENOENT: | |
181 | case -ECONNRESET: | |
182 | case -ESHUTDOWN: | |
183 | case -ENODEV: | |
184 | return -ENODEV; | |
185 | /* errors that might occur during unplugging */ | |
186 | case -EPROTO: /* EHCI */ | |
187 | case -ETIMEDOUT: /* OHCI */ | |
188 | case -EILSEQ: /* UHCI */ | |
189 | return -EIO; | |
190 | default: | |
191 | snd_printk(KERN_ERR "urb status %d\n", status); | |
192 | return 0; /* continue */ | |
193 | } | |
1da177e4 LT |
194 | } |
195 | ||
196 | /* | |
197 | * Receives a chunk of MIDI data. | |
198 | */ | |
199 | static void snd_usbmidi_input_data(snd_usb_midi_in_endpoint_t* ep, int portidx, | |
200 | uint8_t* data, int length) | |
201 | { | |
202 | usbmidi_in_port_t* port = &ep->ports[portidx]; | |
203 | ||
204 | if (!port->substream) { | |
205 | snd_printd("unexpected port %d!\n", portidx); | |
206 | return; | |
207 | } | |
208 | if (!test_bit(port->substream->number, &ep->umidi->input_triggered)) | |
209 | return; | |
210 | snd_rawmidi_receive(port->substream, data, length); | |
211 | } | |
212 | ||
213 | #ifdef DUMP_PACKETS | |
214 | static void dump_urb(const char *type, const u8 *data, int length) | |
215 | { | |
216 | snd_printk(KERN_DEBUG "%s packet: [", type); | |
217 | for (; length > 0; ++data, --length) | |
218 | printk(" %02x", *data); | |
219 | printk(" ]\n"); | |
220 | } | |
221 | #else | |
222 | #define dump_urb(type, data, length) /* nothing */ | |
223 | #endif | |
224 | ||
225 | /* | |
226 | * Processes the data read from the device. | |
227 | */ | |
228 | static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs) | |
229 | { | |
230 | snd_usb_midi_in_endpoint_t* ep = urb->context; | |
231 | ||
232 | if (urb->status == 0) { | |
233 | dump_urb("received", urb->transfer_buffer, urb->actual_length); | |
234 | ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer, | |
235 | urb->actual_length); | |
236 | } else { | |
c8846970 CL |
237 | int err = snd_usbmidi_urb_error(urb->status); |
238 | if (err < 0) { | |
239 | if (err != -ENODEV) { | |
240 | ep->error_resubmit = 1; | |
241 | mod_timer(&ep->umidi->error_timer, | |
242 | jiffies + ERROR_DELAY_JIFFIES); | |
243 | } | |
1da177e4 | 244 | return; |
c8846970 | 245 | } |
1da177e4 LT |
246 | } |
247 | ||
3cfc1eb1 CL |
248 | urb->dev = ep->umidi->chip->dev; |
249 | snd_usbmidi_submit_urb(urb, GFP_ATOMIC); | |
1da177e4 LT |
250 | } |
251 | ||
252 | static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs) | |
253 | { | |
254 | snd_usb_midi_out_endpoint_t* ep = urb->context; | |
255 | ||
256 | spin_lock(&ep->buffer_lock); | |
257 | ep->urb_active = 0; | |
258 | spin_unlock(&ep->buffer_lock); | |
259 | if (urb->status < 0) { | |
c8846970 CL |
260 | int err = snd_usbmidi_urb_error(urb->status); |
261 | if (err < 0) { | |
262 | if (err != -ENODEV) | |
263 | mod_timer(&ep->umidi->error_timer, | |
264 | jiffies + ERROR_DELAY_JIFFIES); | |
1da177e4 | 265 | return; |
c8846970 | 266 | } |
1da177e4 LT |
267 | } |
268 | snd_usbmidi_do_output(ep); | |
269 | } | |
270 | ||
271 | /* | |
272 | * This is called when some data should be transferred to the device | |
273 | * (from one or more substreams). | |
274 | */ | |
275 | static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep) | |
276 | { | |
277 | struct urb* urb = ep->urb; | |
278 | unsigned long flags; | |
279 | ||
280 | spin_lock_irqsave(&ep->buffer_lock, flags); | |
281 | if (ep->urb_active || ep->umidi->chip->shutdown) { | |
282 | spin_unlock_irqrestore(&ep->buffer_lock, flags); | |
283 | return; | |
284 | } | |
285 | ||
286 | urb->transfer_buffer_length = 0; | |
287 | ep->umidi->usb_protocol_ops->output(ep); | |
288 | ||
289 | if (urb->transfer_buffer_length > 0) { | |
290 | dump_urb("sending", urb->transfer_buffer, | |
291 | urb->transfer_buffer_length); | |
292 | urb->dev = ep->umidi->chip->dev; | |
293 | ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0; | |
294 | } | |
295 | spin_unlock_irqrestore(&ep->buffer_lock, flags); | |
296 | } | |
297 | ||
298 | static void snd_usbmidi_out_tasklet(unsigned long data) | |
299 | { | |
300 | snd_usb_midi_out_endpoint_t* ep = (snd_usb_midi_out_endpoint_t *) data; | |
301 | ||
302 | snd_usbmidi_do_output(ep); | |
303 | } | |
304 | ||
c8846970 CL |
305 | /* called after transfers had been interrupted due to some USB error */ |
306 | static void snd_usbmidi_error_timer(unsigned long data) | |
307 | { | |
308 | snd_usb_midi_t *umidi = (snd_usb_midi_t *)data; | |
309 | int i; | |
310 | ||
311 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
312 | snd_usb_midi_in_endpoint_t *in = umidi->endpoints[i].in; | |
313 | if (in && in->error_resubmit) { | |
314 | in->error_resubmit = 0; | |
315 | in->urb->dev = umidi->chip->dev; | |
316 | snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC); | |
317 | } | |
318 | if (umidi->endpoints[i].out) | |
319 | snd_usbmidi_do_output(umidi->endpoints[i].out); | |
320 | } | |
321 | } | |
322 | ||
1da177e4 LT |
323 | /* helper function to send static data that may not DMA-able */ |
324 | static int send_bulk_static_data(snd_usb_midi_out_endpoint_t* ep, | |
325 | const void *data, int len) | |
326 | { | |
327 | int err; | |
328 | void *buf = kmalloc(len, GFP_KERNEL); | |
329 | if (!buf) | |
330 | return -ENOMEM; | |
331 | memcpy(buf, data, len); | |
332 | dump_urb("sending", buf, len); | |
333 | err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len, | |
334 | NULL, 250); | |
335 | kfree(buf); | |
336 | return err; | |
337 | } | |
338 | ||
339 | /* | |
340 | * Standard USB MIDI protocol: see the spec. | |
341 | * Midiman protocol: like the standard protocol, but the control byte is the | |
342 | * fourth byte in each packet, and uses length instead of CIN. | |
343 | */ | |
344 | ||
345 | static void snd_usbmidi_standard_input(snd_usb_midi_in_endpoint_t* ep, | |
346 | uint8_t* buffer, int buffer_length) | |
347 | { | |
348 | int i; | |
349 | ||
350 | for (i = 0; i + 3 < buffer_length; i += 4) | |
351 | if (buffer[i] != 0) { | |
352 | int cable = buffer[i] >> 4; | |
353 | int length = snd_usbmidi_cin_length[buffer[i] & 0x0f]; | |
354 | snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length); | |
355 | } | |
356 | } | |
357 | ||
358 | static void snd_usbmidi_midiman_input(snd_usb_midi_in_endpoint_t* ep, | |
359 | uint8_t* buffer, int buffer_length) | |
360 | { | |
361 | int i; | |
362 | ||
363 | for (i = 0; i + 3 < buffer_length; i += 4) | |
364 | if (buffer[i + 3] != 0) { | |
365 | int port = buffer[i + 3] >> 4; | |
366 | int length = buffer[i + 3] & 3; | |
367 | snd_usbmidi_input_data(ep, port, &buffer[i], length); | |
368 | } | |
369 | } | |
370 | ||
371 | /* | |
372 | * Adds one USB MIDI packet to the output buffer. | |
373 | */ | |
374 | static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0, | |
375 | uint8_t p1, uint8_t p2, uint8_t p3) | |
376 | { | |
377 | ||
378 | uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length; | |
379 | buf[0] = p0; | |
380 | buf[1] = p1; | |
381 | buf[2] = p2; | |
382 | buf[3] = p3; | |
383 | urb->transfer_buffer_length += 4; | |
384 | } | |
385 | ||
386 | /* | |
387 | * Adds one Midiman packet to the output buffer. | |
388 | */ | |
389 | static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0, | |
390 | uint8_t p1, uint8_t p2, uint8_t p3) | |
391 | { | |
392 | ||
393 | uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length; | |
394 | buf[0] = p1; | |
395 | buf[1] = p2; | |
396 | buf[2] = p3; | |
397 | buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f]; | |
398 | urb->transfer_buffer_length += 4; | |
399 | } | |
400 | ||
401 | /* | |
402 | * Converts MIDI commands to USB MIDI packets. | |
403 | */ | |
404 | static void snd_usbmidi_transmit_byte(usbmidi_out_port_t* port, | |
405 | uint8_t b, struct urb* urb) | |
406 | { | |
407 | uint8_t p0 = port->cable; | |
408 | void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) = | |
409 | port->ep->umidi->usb_protocol_ops->output_packet; | |
410 | ||
411 | if (b >= 0xf8) { | |
412 | output_packet(urb, p0 | 0x0f, b, 0, 0); | |
413 | } else if (b >= 0xf0) { | |
414 | switch (b) { | |
415 | case 0xf0: | |
416 | port->data[0] = b; | |
417 | port->state = STATE_SYSEX_1; | |
418 | break; | |
419 | case 0xf1: | |
420 | case 0xf3: | |
421 | port->data[0] = b; | |
422 | port->state = STATE_1PARAM; | |
423 | break; | |
424 | case 0xf2: | |
425 | port->data[0] = b; | |
426 | port->state = STATE_2PARAM_1; | |
427 | break; | |
428 | case 0xf4: | |
429 | case 0xf5: | |
430 | port->state = STATE_UNKNOWN; | |
431 | break; | |
432 | case 0xf6: | |
433 | output_packet(urb, p0 | 0x05, 0xf6, 0, 0); | |
434 | port->state = STATE_UNKNOWN; | |
435 | break; | |
436 | case 0xf7: | |
437 | switch (port->state) { | |
438 | case STATE_SYSEX_0: | |
439 | output_packet(urb, p0 | 0x05, 0xf7, 0, 0); | |
440 | break; | |
441 | case STATE_SYSEX_1: | |
442 | output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0); | |
443 | break; | |
444 | case STATE_SYSEX_2: | |
445 | output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7); | |
446 | break; | |
447 | } | |
448 | port->state = STATE_UNKNOWN; | |
449 | break; | |
450 | } | |
451 | } else if (b >= 0x80) { | |
452 | port->data[0] = b; | |
453 | if (b >= 0xc0 && b <= 0xdf) | |
454 | port->state = STATE_1PARAM; | |
455 | else | |
456 | port->state = STATE_2PARAM_1; | |
457 | } else { /* b < 0x80 */ | |
458 | switch (port->state) { | |
459 | case STATE_1PARAM: | |
460 | if (port->data[0] < 0xf0) { | |
461 | p0 |= port->data[0] >> 4; | |
462 | } else { | |
463 | p0 |= 0x02; | |
464 | port->state = STATE_UNKNOWN; | |
465 | } | |
466 | output_packet(urb, p0, port->data[0], b, 0); | |
467 | break; | |
468 | case STATE_2PARAM_1: | |
469 | port->data[1] = b; | |
470 | port->state = STATE_2PARAM_2; | |
471 | break; | |
472 | case STATE_2PARAM_2: | |
473 | if (port->data[0] < 0xf0) { | |
474 | p0 |= port->data[0] >> 4; | |
475 | port->state = STATE_2PARAM_1; | |
476 | } else { | |
477 | p0 |= 0x03; | |
478 | port->state = STATE_UNKNOWN; | |
479 | } | |
480 | output_packet(urb, p0, port->data[0], port->data[1], b); | |
481 | break; | |
482 | case STATE_SYSEX_0: | |
483 | port->data[0] = b; | |
484 | port->state = STATE_SYSEX_1; | |
485 | break; | |
486 | case STATE_SYSEX_1: | |
487 | port->data[1] = b; | |
488 | port->state = STATE_SYSEX_2; | |
489 | break; | |
490 | case STATE_SYSEX_2: | |
491 | output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b); | |
492 | port->state = STATE_SYSEX_0; | |
493 | break; | |
494 | } | |
495 | } | |
496 | } | |
497 | ||
498 | static void snd_usbmidi_standard_output(snd_usb_midi_out_endpoint_t* ep) | |
499 | { | |
500 | struct urb* urb = ep->urb; | |
501 | int p; | |
502 | ||
503 | /* FIXME: lower-numbered ports can starve higher-numbered ports */ | |
504 | for (p = 0; p < 0x10; ++p) { | |
505 | usbmidi_out_port_t* port = &ep->ports[p]; | |
506 | if (!port->active) | |
507 | continue; | |
508 | while (urb->transfer_buffer_length + 3 < ep->max_transfer) { | |
509 | uint8_t b; | |
510 | if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) { | |
511 | port->active = 0; | |
512 | break; | |
513 | } | |
514 | snd_usbmidi_transmit_byte(port, b, urb); | |
515 | } | |
516 | } | |
517 | } | |
518 | ||
519 | static struct usb_protocol_ops snd_usbmidi_standard_ops = { | |
520 | .input = snd_usbmidi_standard_input, | |
521 | .output = snd_usbmidi_standard_output, | |
522 | .output_packet = snd_usbmidi_output_standard_packet, | |
523 | }; | |
524 | ||
525 | static struct usb_protocol_ops snd_usbmidi_midiman_ops = { | |
526 | .input = snd_usbmidi_midiman_input, | |
527 | .output = snd_usbmidi_standard_output, | |
528 | .output_packet = snd_usbmidi_output_midiman_packet, | |
529 | }; | |
530 | ||
531 | /* | |
532 | * Novation USB MIDI protocol: number of data bytes is in the first byte | |
533 | * (when receiving) (+1!) or in the second byte (when sending); data begins | |
534 | * at the third byte. | |
535 | */ | |
536 | ||
537 | static void snd_usbmidi_novation_input(snd_usb_midi_in_endpoint_t* ep, | |
538 | uint8_t* buffer, int buffer_length) | |
539 | { | |
540 | if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1) | |
541 | return; | |
542 | snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1); | |
543 | } | |
544 | ||
545 | static void snd_usbmidi_novation_output(snd_usb_midi_out_endpoint_t* ep) | |
546 | { | |
547 | uint8_t* transfer_buffer; | |
548 | int count; | |
549 | ||
550 | if (!ep->ports[0].active) | |
551 | return; | |
552 | transfer_buffer = ep->urb->transfer_buffer; | |
553 | count = snd_rawmidi_transmit(ep->ports[0].substream, | |
554 | &transfer_buffer[2], | |
555 | ep->max_transfer - 2); | |
556 | if (count < 1) { | |
557 | ep->ports[0].active = 0; | |
558 | return; | |
559 | } | |
560 | transfer_buffer[0] = 0; | |
561 | transfer_buffer[1] = count; | |
562 | ep->urb->transfer_buffer_length = 2 + count; | |
563 | } | |
564 | ||
565 | static struct usb_protocol_ops snd_usbmidi_novation_ops = { | |
566 | .input = snd_usbmidi_novation_input, | |
567 | .output = snd_usbmidi_novation_output, | |
568 | }; | |
569 | ||
570 | /* | |
6155aff8 | 571 | * "raw" protocol: used by the MOTU FastLane. |
1da177e4 LT |
572 | */ |
573 | ||
6155aff8 CL |
574 | static void snd_usbmidi_raw_input(snd_usb_midi_in_endpoint_t* ep, |
575 | uint8_t* buffer, int buffer_length) | |
1da177e4 LT |
576 | { |
577 | snd_usbmidi_input_data(ep, 0, buffer, buffer_length); | |
578 | } | |
579 | ||
6155aff8 | 580 | static void snd_usbmidi_raw_output(snd_usb_midi_out_endpoint_t* ep) |
1da177e4 LT |
581 | { |
582 | int count; | |
583 | ||
584 | if (!ep->ports[0].active) | |
585 | return; | |
586 | count = snd_rawmidi_transmit(ep->ports[0].substream, | |
587 | ep->urb->transfer_buffer, | |
588 | ep->max_transfer); | |
589 | if (count < 1) { | |
590 | ep->ports[0].active = 0; | |
591 | return; | |
592 | } | |
593 | ep->urb->transfer_buffer_length = count; | |
594 | } | |
595 | ||
6155aff8 CL |
596 | static struct usb_protocol_ops snd_usbmidi_raw_ops = { |
597 | .input = snd_usbmidi_raw_input, | |
598 | .output = snd_usbmidi_raw_output, | |
1da177e4 LT |
599 | }; |
600 | ||
601 | /* | |
602 | * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching. | |
603 | */ | |
604 | ||
605 | static void snd_usbmidi_emagic_init_out(snd_usb_midi_out_endpoint_t* ep) | |
606 | { | |
607 | static const u8 init_data[] = { | |
608 | /* initialization magic: "get version" */ | |
609 | 0xf0, | |
610 | 0x00, 0x20, 0x31, /* Emagic */ | |
611 | 0x64, /* Unitor8 */ | |
612 | 0x0b, /* version number request */ | |
613 | 0x00, /* command version */ | |
614 | 0x00, /* EEPROM, box 0 */ | |
615 | 0xf7 | |
616 | }; | |
617 | send_bulk_static_data(ep, init_data, sizeof(init_data)); | |
618 | /* while we're at it, pour on more magic */ | |
619 | send_bulk_static_data(ep, init_data, sizeof(init_data)); | |
620 | } | |
621 | ||
622 | static void snd_usbmidi_emagic_finish_out(snd_usb_midi_out_endpoint_t* ep) | |
623 | { | |
624 | static const u8 finish_data[] = { | |
625 | /* switch to patch mode with last preset */ | |
626 | 0xf0, | |
627 | 0x00, 0x20, 0x31, /* Emagic */ | |
628 | 0x64, /* Unitor8 */ | |
629 | 0x10, /* patch switch command */ | |
630 | 0x00, /* command version */ | |
631 | 0x7f, /* to all boxes */ | |
632 | 0x40, /* last preset in EEPROM */ | |
633 | 0xf7 | |
634 | }; | |
635 | send_bulk_static_data(ep, finish_data, sizeof(finish_data)); | |
636 | } | |
637 | ||
638 | static void snd_usbmidi_emagic_input(snd_usb_midi_in_endpoint_t* ep, | |
639 | uint8_t* buffer, int buffer_length) | |
640 | { | |
c347e9fc CL |
641 | int i; |
642 | ||
643 | /* FF indicates end of valid data */ | |
644 | for (i = 0; i < buffer_length; ++i) | |
645 | if (buffer[i] == 0xff) { | |
646 | buffer_length = i; | |
647 | break; | |
648 | } | |
1da177e4 LT |
649 | |
650 | /* handle F5 at end of last buffer */ | |
651 | if (ep->seen_f5) | |
652 | goto switch_port; | |
653 | ||
654 | while (buffer_length > 0) { | |
1da177e4 LT |
655 | /* determine size of data until next F5 */ |
656 | for (i = 0; i < buffer_length; ++i) | |
657 | if (buffer[i] == 0xf5) | |
658 | break; | |
659 | snd_usbmidi_input_data(ep, ep->current_port, buffer, i); | |
660 | buffer += i; | |
661 | buffer_length -= i; | |
662 | ||
663 | if (buffer_length <= 0) | |
664 | break; | |
665 | /* assert(buffer[0] == 0xf5); */ | |
666 | ep->seen_f5 = 1; | |
667 | ++buffer; | |
668 | --buffer_length; | |
669 | ||
670 | switch_port: | |
671 | if (buffer_length <= 0) | |
672 | break; | |
673 | if (buffer[0] < 0x80) { | |
674 | ep->current_port = (buffer[0] - 1) & 15; | |
675 | ++buffer; | |
676 | --buffer_length; | |
677 | } | |
678 | ep->seen_f5 = 0; | |
679 | } | |
680 | } | |
681 | ||
682 | static void snd_usbmidi_emagic_output(snd_usb_midi_out_endpoint_t* ep) | |
683 | { | |
684 | int port0 = ep->current_port; | |
685 | uint8_t* buf = ep->urb->transfer_buffer; | |
686 | int buf_free = ep->max_transfer; | |
687 | int length, i; | |
688 | ||
689 | for (i = 0; i < 0x10; ++i) { | |
690 | /* round-robin, starting at the last current port */ | |
691 | int portnum = (port0 + i) & 15; | |
692 | usbmidi_out_port_t* port = &ep->ports[portnum]; | |
693 | ||
694 | if (!port->active) | |
695 | continue; | |
696 | if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) { | |
697 | port->active = 0; | |
698 | continue; | |
699 | } | |
700 | ||
701 | if (portnum != ep->current_port) { | |
702 | if (buf_free < 2) | |
703 | break; | |
704 | ep->current_port = portnum; | |
705 | buf[0] = 0xf5; | |
706 | buf[1] = (portnum + 1) & 15; | |
707 | buf += 2; | |
708 | buf_free -= 2; | |
709 | } | |
710 | ||
711 | if (buf_free < 1) | |
712 | break; | |
713 | length = snd_rawmidi_transmit(port->substream, buf, buf_free); | |
714 | if (length > 0) { | |
715 | buf += length; | |
716 | buf_free -= length; | |
717 | if (buf_free < 1) | |
718 | break; | |
719 | } | |
720 | } | |
c347e9fc CL |
721 | if (buf_free < ep->max_transfer && buf_free > 0) { |
722 | *buf = 0xff; | |
723 | --buf_free; | |
724 | } | |
1da177e4 LT |
725 | ep->urb->transfer_buffer_length = ep->max_transfer - buf_free; |
726 | } | |
727 | ||
728 | static struct usb_protocol_ops snd_usbmidi_emagic_ops = { | |
729 | .input = snd_usbmidi_emagic_input, | |
730 | .output = snd_usbmidi_emagic_output, | |
731 | .init_out_endpoint = snd_usbmidi_emagic_init_out, | |
732 | .finish_out_endpoint = snd_usbmidi_emagic_finish_out, | |
733 | }; | |
734 | ||
735 | ||
736 | static int snd_usbmidi_output_open(snd_rawmidi_substream_t* substream) | |
737 | { | |
738 | snd_usb_midi_t* umidi = substream->rmidi->private_data; | |
739 | usbmidi_out_port_t* port = NULL; | |
740 | int i, j; | |
741 | ||
742 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) | |
743 | if (umidi->endpoints[i].out) | |
744 | for (j = 0; j < 0x10; ++j) | |
745 | if (umidi->endpoints[i].out->ports[j].substream == substream) { | |
746 | port = &umidi->endpoints[i].out->ports[j]; | |
747 | break; | |
748 | } | |
749 | if (!port) { | |
750 | snd_BUG(); | |
751 | return -ENXIO; | |
752 | } | |
753 | substream->runtime->private_data = port; | |
754 | port->state = STATE_UNKNOWN; | |
755 | return 0; | |
756 | } | |
757 | ||
758 | static int snd_usbmidi_output_close(snd_rawmidi_substream_t* substream) | |
759 | { | |
760 | return 0; | |
761 | } | |
762 | ||
763 | static void snd_usbmidi_output_trigger(snd_rawmidi_substream_t* substream, int up) | |
764 | { | |
765 | usbmidi_out_port_t* port = (usbmidi_out_port_t*)substream->runtime->private_data; | |
766 | ||
767 | port->active = up; | |
768 | if (up) { | |
769 | if (port->ep->umidi->chip->shutdown) { | |
770 | /* gobble up remaining bytes to prevent wait in | |
771 | * snd_rawmidi_drain_output */ | |
772 | while (!snd_rawmidi_transmit_empty(substream)) | |
773 | snd_rawmidi_transmit_ack(substream, 1); | |
774 | return; | |
775 | } | |
776 | tasklet_hi_schedule(&port->ep->tasklet); | |
777 | } | |
778 | } | |
779 | ||
780 | static int snd_usbmidi_input_open(snd_rawmidi_substream_t* substream) | |
781 | { | |
782 | return 0; | |
783 | } | |
784 | ||
785 | static int snd_usbmidi_input_close(snd_rawmidi_substream_t* substream) | |
786 | { | |
787 | return 0; | |
788 | } | |
789 | ||
790 | static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t* substream, int up) | |
791 | { | |
792 | snd_usb_midi_t* umidi = substream->rmidi->private_data; | |
793 | ||
794 | if (up) | |
795 | set_bit(substream->number, &umidi->input_triggered); | |
796 | else | |
797 | clear_bit(substream->number, &umidi->input_triggered); | |
798 | } | |
799 | ||
800 | static snd_rawmidi_ops_t snd_usbmidi_output_ops = { | |
801 | .open = snd_usbmidi_output_open, | |
802 | .close = snd_usbmidi_output_close, | |
803 | .trigger = snd_usbmidi_output_trigger, | |
804 | }; | |
805 | ||
806 | static snd_rawmidi_ops_t snd_usbmidi_input_ops = { | |
807 | .open = snd_usbmidi_input_open, | |
808 | .close = snd_usbmidi_input_close, | |
809 | .trigger = snd_usbmidi_input_trigger | |
810 | }; | |
811 | ||
812 | /* | |
813 | * Frees an input endpoint. | |
814 | * May be called when ep hasn't been initialized completely. | |
815 | */ | |
816 | static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t* ep) | |
817 | { | |
818 | if (ep->urb) { | |
55851f73 CL |
819 | usb_buffer_free(ep->umidi->chip->dev, |
820 | ep->urb->transfer_buffer_length, | |
821 | ep->urb->transfer_buffer, | |
822 | ep->urb->transfer_dma); | |
1da177e4 LT |
823 | usb_free_urb(ep->urb); |
824 | } | |
825 | kfree(ep); | |
826 | } | |
827 | ||
828 | /* | |
829 | * Creates an input endpoint. | |
830 | */ | |
831 | static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t* umidi, | |
832 | snd_usb_midi_endpoint_info_t* ep_info, | |
833 | snd_usb_midi_endpoint_t* rep) | |
834 | { | |
835 | snd_usb_midi_in_endpoint_t* ep; | |
836 | void* buffer; | |
837 | unsigned int pipe; | |
838 | int length; | |
839 | ||
840 | rep->in = NULL; | |
561b220a | 841 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
1da177e4 LT |
842 | if (!ep) |
843 | return -ENOMEM; | |
844 | ep->umidi = umidi; | |
845 | ||
846 | ep->urb = usb_alloc_urb(0, GFP_KERNEL); | |
847 | if (!ep->urb) { | |
848 | snd_usbmidi_in_endpoint_delete(ep); | |
849 | return -ENOMEM; | |
850 | } | |
851 | if (ep_info->in_interval) | |
852 | pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep); | |
853 | else | |
854 | pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep); | |
855 | length = usb_maxpacket(umidi->chip->dev, pipe, 0); | |
55851f73 CL |
856 | buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL, |
857 | &ep->urb->transfer_dma); | |
1da177e4 LT |
858 | if (!buffer) { |
859 | snd_usbmidi_in_endpoint_delete(ep); | |
860 | return -ENOMEM; | |
861 | } | |
862 | if (ep_info->in_interval) | |
3527a008 CL |
863 | usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer, |
864 | length, snd_usbmidi_in_urb_complete, ep, | |
865 | ep_info->in_interval); | |
1da177e4 | 866 | else |
3527a008 CL |
867 | usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, |
868 | length, snd_usbmidi_in_urb_complete, ep); | |
55851f73 | 869 | ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
1da177e4 LT |
870 | |
871 | rep->in = ep; | |
872 | return 0; | |
873 | } | |
874 | ||
875 | static unsigned int snd_usbmidi_count_bits(unsigned int x) | |
876 | { | |
877 | unsigned int bits = 0; | |
878 | ||
879 | for (; x; x >>= 1) | |
880 | bits += x & 1; | |
881 | return bits; | |
882 | } | |
883 | ||
884 | /* | |
885 | * Frees an output endpoint. | |
886 | * May be called when ep hasn't been initialized completely. | |
887 | */ | |
888 | static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t* ep) | |
889 | { | |
1da177e4 | 890 | if (ep->urb) { |
55851f73 CL |
891 | usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer, |
892 | ep->urb->transfer_buffer, | |
893 | ep->urb->transfer_dma); | |
1da177e4 LT |
894 | usb_free_urb(ep->urb); |
895 | } | |
896 | kfree(ep); | |
897 | } | |
898 | ||
899 | /* | |
900 | * Creates an output endpoint, and initializes output ports. | |
901 | */ | |
902 | static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t* umidi, | |
903 | snd_usb_midi_endpoint_info_t* ep_info, | |
904 | snd_usb_midi_endpoint_t* rep) | |
905 | { | |
906 | snd_usb_midi_out_endpoint_t* ep; | |
907 | int i; | |
908 | unsigned int pipe; | |
909 | void* buffer; | |
910 | ||
911 | rep->out = NULL; | |
561b220a | 912 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
1da177e4 LT |
913 | if (!ep) |
914 | return -ENOMEM; | |
915 | ep->umidi = umidi; | |
916 | ||
917 | ep->urb = usb_alloc_urb(0, GFP_KERNEL); | |
918 | if (!ep->urb) { | |
919 | snd_usbmidi_out_endpoint_delete(ep); | |
920 | return -ENOMEM; | |
921 | } | |
922 | /* we never use interrupt output pipes */ | |
923 | pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep); | |
924 | ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1); | |
55851f73 CL |
925 | buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer, |
926 | GFP_KERNEL, &ep->urb->transfer_dma); | |
1da177e4 LT |
927 | if (!buffer) { |
928 | snd_usbmidi_out_endpoint_delete(ep); | |
929 | return -ENOMEM; | |
930 | } | |
931 | usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, | |
3527a008 | 932 | ep->max_transfer, snd_usbmidi_out_urb_complete, ep); |
55851f73 | 933 | ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
1da177e4 LT |
934 | |
935 | spin_lock_init(&ep->buffer_lock); | |
936 | tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep); | |
937 | ||
938 | for (i = 0; i < 0x10; ++i) | |
939 | if (ep_info->out_cables & (1 << i)) { | |
940 | ep->ports[i].ep = ep; | |
941 | ep->ports[i].cable = i << 4; | |
942 | } | |
943 | ||
944 | if (umidi->usb_protocol_ops->init_out_endpoint) | |
945 | umidi->usb_protocol_ops->init_out_endpoint(ep); | |
946 | ||
947 | rep->out = ep; | |
948 | return 0; | |
949 | } | |
950 | ||
951 | /* | |
952 | * Frees everything. | |
953 | */ | |
954 | static void snd_usbmidi_free(snd_usb_midi_t* umidi) | |
955 | { | |
956 | int i; | |
957 | ||
958 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
959 | snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i]; | |
960 | if (ep->out) | |
961 | snd_usbmidi_out_endpoint_delete(ep->out); | |
962 | if (ep->in) | |
963 | snd_usbmidi_in_endpoint_delete(ep->in); | |
964 | } | |
965 | kfree(umidi); | |
966 | } | |
967 | ||
968 | /* | |
969 | * Unlinks all URBs (must be done before the usb_device is deleted). | |
970 | */ | |
ee733397 | 971 | void snd_usbmidi_disconnect(struct list_head* p) |
1da177e4 LT |
972 | { |
973 | snd_usb_midi_t* umidi; | |
974 | int i; | |
975 | ||
976 | umidi = list_entry(p, snd_usb_midi_t, list); | |
c8846970 | 977 | del_timer_sync(&umidi->error_timer); |
1da177e4 LT |
978 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
979 | snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i]; | |
c8846970 CL |
980 | if (ep->out) |
981 | tasklet_kill(&ep->out->tasklet); | |
1da177e4 LT |
982 | if (ep->out && ep->out->urb) { |
983 | usb_kill_urb(ep->out->urb); | |
984 | if (umidi->usb_protocol_ops->finish_out_endpoint) | |
985 | umidi->usb_protocol_ops->finish_out_endpoint(ep->out); | |
986 | } | |
987 | if (ep->in && ep->in->urb) | |
988 | usb_kill_urb(ep->in->urb); | |
989 | } | |
990 | } | |
991 | ||
992 | static void snd_usbmidi_rawmidi_free(snd_rawmidi_t* rmidi) | |
993 | { | |
994 | snd_usb_midi_t* umidi = rmidi->private_data; | |
995 | snd_usbmidi_free(umidi); | |
996 | } | |
997 | ||
998 | static snd_rawmidi_substream_t* snd_usbmidi_find_substream(snd_usb_midi_t* umidi, | |
999 | int stream, int number) | |
1000 | { | |
1001 | struct list_head* list; | |
1002 | ||
1003 | list_for_each(list, &umidi->rmidi->streams[stream].substreams) { | |
1004 | snd_rawmidi_substream_t* substream = list_entry(list, snd_rawmidi_substream_t, list); | |
1005 | if (substream->number == number) | |
1006 | return substream; | |
1007 | } | |
1008 | return NULL; | |
1009 | } | |
1010 | ||
1011 | /* | |
1012 | * This list specifies names for ports that do not fit into the standard | |
1013 | * "(product) MIDI (n)" schema because they aren't external MIDI ports, | |
1014 | * such as internal control or synthesizer ports. | |
1015 | */ | |
1016 | static struct { | |
27d10f56 | 1017 | u32 id; |
1da177e4 LT |
1018 | int port; |
1019 | const char *name_format; | |
1020 | } snd_usbmidi_port_names[] = { | |
1021 | /* Roland UA-100 */ | |
27d10f56 | 1022 | { USB_ID(0x0582, 0x0000), 2, "%s Control" }, |
1da177e4 | 1023 | /* Roland SC-8850 */ |
27d10f56 CL |
1024 | { USB_ID(0x0582, 0x0003), 0, "%s Part A" }, |
1025 | { USB_ID(0x0582, 0x0003), 1, "%s Part B" }, | |
1026 | { USB_ID(0x0582, 0x0003), 2, "%s Part C" }, | |
1027 | { USB_ID(0x0582, 0x0003), 3, "%s Part D" }, | |
1028 | { USB_ID(0x0582, 0x0003), 4, "%s MIDI 1" }, | |
1029 | { USB_ID(0x0582, 0x0003), 5, "%s MIDI 2" }, | |
1da177e4 | 1030 | /* Roland U-8 */ |
27d10f56 CL |
1031 | { USB_ID(0x0582, 0x0004), 0, "%s MIDI" }, |
1032 | { USB_ID(0x0582, 0x0004), 1, "%s Control" }, | |
1da177e4 | 1033 | /* Roland SC-8820 */ |
27d10f56 CL |
1034 | { USB_ID(0x0582, 0x0007), 0, "%s Part A" }, |
1035 | { USB_ID(0x0582, 0x0007), 1, "%s Part B" }, | |
1036 | { USB_ID(0x0582, 0x0007), 2, "%s MIDI" }, | |
1da177e4 | 1037 | /* Roland SK-500 */ |
27d10f56 CL |
1038 | { USB_ID(0x0582, 0x000b), 0, "%s Part A" }, |
1039 | { USB_ID(0x0582, 0x000b), 1, "%s Part B" }, | |
1040 | { USB_ID(0x0582, 0x000b), 2, "%s MIDI" }, | |
1da177e4 | 1041 | /* Roland SC-D70 */ |
27d10f56 CL |
1042 | { USB_ID(0x0582, 0x000c), 0, "%s Part A" }, |
1043 | { USB_ID(0x0582, 0x000c), 1, "%s Part B" }, | |
1044 | { USB_ID(0x0582, 0x000c), 2, "%s MIDI" }, | |
1da177e4 | 1045 | /* Edirol UM-880 */ |
27d10f56 | 1046 | { USB_ID(0x0582, 0x0014), 8, "%s Control" }, |
1da177e4 | 1047 | /* Edirol SD-90 */ |
27d10f56 CL |
1048 | { USB_ID(0x0582, 0x0016), 0, "%s Part A" }, |
1049 | { USB_ID(0x0582, 0x0016), 1, "%s Part B" }, | |
1050 | { USB_ID(0x0582, 0x0016), 2, "%s MIDI 1" }, | |
1051 | { USB_ID(0x0582, 0x0016), 3, "%s MIDI 2" }, | |
1da177e4 | 1052 | /* Edirol UM-550 */ |
27d10f56 | 1053 | { USB_ID(0x0582, 0x0023), 5, "%s Control" }, |
1da177e4 | 1054 | /* Edirol SD-20 */ |
27d10f56 CL |
1055 | { USB_ID(0x0582, 0x0027), 0, "%s Part A" }, |
1056 | { USB_ID(0x0582, 0x0027), 1, "%s Part B" }, | |
1057 | { USB_ID(0x0582, 0x0027), 2, "%s MIDI" }, | |
1da177e4 | 1058 | /* Edirol SD-80 */ |
27d10f56 CL |
1059 | { USB_ID(0x0582, 0x0029), 0, "%s Part A" }, |
1060 | { USB_ID(0x0582, 0x0029), 1, "%s Part B" }, | |
1061 | { USB_ID(0x0582, 0x0029), 2, "%s MIDI 1" }, | |
1062 | { USB_ID(0x0582, 0x0029), 3, "%s MIDI 2" }, | |
1da177e4 | 1063 | /* Edirol UA-700 */ |
27d10f56 CL |
1064 | { USB_ID(0x0582, 0x002b), 0, "%s MIDI" }, |
1065 | { USB_ID(0x0582, 0x002b), 1, "%s Control" }, | |
1da177e4 | 1066 | /* Roland VariOS */ |
27d10f56 CL |
1067 | { USB_ID(0x0582, 0x002f), 0, "%s MIDI" }, |
1068 | { USB_ID(0x0582, 0x002f), 1, "%s External MIDI" }, | |
1069 | { USB_ID(0x0582, 0x002f), 2, "%s Sync" }, | |
1da177e4 | 1070 | /* Edirol PCR */ |
27d10f56 CL |
1071 | { USB_ID(0x0582, 0x0033), 0, "%s MIDI" }, |
1072 | { USB_ID(0x0582, 0x0033), 1, "%s 1" }, | |
1073 | { USB_ID(0x0582, 0x0033), 2, "%s 2" }, | |
1da177e4 | 1074 | /* BOSS GS-10 */ |
27d10f56 CL |
1075 | { USB_ID(0x0582, 0x003b), 0, "%s MIDI" }, |
1076 | { USB_ID(0x0582, 0x003b), 1, "%s Control" }, | |
1da177e4 | 1077 | /* Edirol UA-1000 */ |
27d10f56 CL |
1078 | { USB_ID(0x0582, 0x0044), 0, "%s MIDI" }, |
1079 | { USB_ID(0x0582, 0x0044), 1, "%s Control" }, | |
1da177e4 | 1080 | /* Edirol UR-80 */ |
27d10f56 CL |
1081 | { USB_ID(0x0582, 0x0048), 0, "%s MIDI" }, |
1082 | { USB_ID(0x0582, 0x0048), 1, "%s 1" }, | |
1083 | { USB_ID(0x0582, 0x0048), 2, "%s 2" }, | |
1da177e4 | 1084 | /* Edirol PCR-A */ |
27d10f56 CL |
1085 | { USB_ID(0x0582, 0x004d), 0, "%s MIDI" }, |
1086 | { USB_ID(0x0582, 0x004d), 1, "%s 1" }, | |
1087 | { USB_ID(0x0582, 0x004d), 2, "%s 2" }, | |
1da177e4 | 1088 | /* M-Audio MidiSport 8x8 */ |
27d10f56 CL |
1089 | { USB_ID(0x0763, 0x1031), 8, "%s Control" }, |
1090 | { USB_ID(0x0763, 0x1033), 8, "%s Control" }, | |
1da177e4 | 1091 | /* MOTU Fastlane */ |
27d10f56 CL |
1092 | { USB_ID(0x07fd, 0x0001), 0, "%s MIDI A" }, |
1093 | { USB_ID(0x07fd, 0x0001), 1, "%s MIDI B" }, | |
1da177e4 | 1094 | /* Emagic Unitor8/AMT8/MT4 */ |
27d10f56 CL |
1095 | { USB_ID(0x086a, 0x0001), 8, "%s Broadcast" }, |
1096 | { USB_ID(0x086a, 0x0002), 8, "%s Broadcast" }, | |
1097 | { USB_ID(0x086a, 0x0003), 4, "%s Broadcast" }, | |
1da177e4 LT |
1098 | }; |
1099 | ||
1100 | static void snd_usbmidi_init_substream(snd_usb_midi_t* umidi, | |
1101 | int stream, int number, | |
1102 | snd_rawmidi_substream_t** rsubstream) | |
1103 | { | |
1104 | int i; | |
1da177e4 LT |
1105 | const char *name_format; |
1106 | ||
1107 | snd_rawmidi_substream_t* substream = snd_usbmidi_find_substream(umidi, stream, number); | |
1108 | if (!substream) { | |
1109 | snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number); | |
1110 | return; | |
1111 | } | |
1112 | ||
1113 | /* TODO: read port name from jack descriptor */ | |
1114 | name_format = "%s MIDI %d"; | |
1da177e4 | 1115 | for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_names); ++i) { |
27d10f56 | 1116 | if (snd_usbmidi_port_names[i].id == umidi->chip->usb_id && |
1da177e4 LT |
1117 | snd_usbmidi_port_names[i].port == number) { |
1118 | name_format = snd_usbmidi_port_names[i].name_format; | |
1119 | break; | |
1120 | } | |
1121 | } | |
1122 | snprintf(substream->name, sizeof(substream->name), | |
1123 | name_format, umidi->chip->card->shortname, number + 1); | |
1124 | ||
1125 | *rsubstream = substream; | |
1126 | } | |
1127 | ||
1128 | /* | |
1129 | * Creates the endpoints and their ports. | |
1130 | */ | |
1131 | static int snd_usbmidi_create_endpoints(snd_usb_midi_t* umidi, | |
1132 | snd_usb_midi_endpoint_info_t* endpoints) | |
1133 | { | |
1134 | int i, j, err; | |
1135 | int out_ports = 0, in_ports = 0; | |
1136 | ||
1137 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
1138 | if (endpoints[i].out_cables) { | |
1139 | err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i], | |
1140 | &umidi->endpoints[i]); | |
1141 | if (err < 0) | |
1142 | return err; | |
1143 | } | |
1144 | if (endpoints[i].in_cables) { | |
1145 | err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i], | |
1146 | &umidi->endpoints[i]); | |
1147 | if (err < 0) | |
1148 | return err; | |
1149 | } | |
1150 | ||
1151 | for (j = 0; j < 0x10; ++j) { | |
1152 | if (endpoints[i].out_cables & (1 << j)) { | |
1153 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports, | |
1154 | &umidi->endpoints[i].out->ports[j].substream); | |
1155 | ++out_ports; | |
1156 | } | |
1157 | if (endpoints[i].in_cables & (1 << j)) { | |
1158 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports, | |
1159 | &umidi->endpoints[i].in->ports[j].substream); | |
1160 | ++in_ports; | |
1161 | } | |
1162 | } | |
1163 | } | |
1164 | snd_printdd(KERN_INFO "created %d output and %d input ports\n", | |
1165 | out_ports, in_ports); | |
1166 | return 0; | |
1167 | } | |
1168 | ||
1169 | /* | |
1170 | * Returns MIDIStreaming device capabilities. | |
1171 | */ | |
1172 | static int snd_usbmidi_get_ms_info(snd_usb_midi_t* umidi, | |
1173 | snd_usb_midi_endpoint_info_t* endpoints) | |
1174 | { | |
1175 | struct usb_interface* intf; | |
1176 | struct usb_host_interface *hostif; | |
1177 | struct usb_interface_descriptor* intfd; | |
1178 | struct usb_ms_header_descriptor* ms_header; | |
1179 | struct usb_host_endpoint *hostep; | |
1180 | struct usb_endpoint_descriptor* ep; | |
1181 | struct usb_ms_endpoint_descriptor* ms_ep; | |
1182 | int i, epidx; | |
1183 | ||
1184 | intf = umidi->iface; | |
1185 | if (!intf) | |
1186 | return -ENXIO; | |
1187 | hostif = &intf->altsetting[0]; | |
1188 | intfd = get_iface_desc(hostif); | |
1189 | ms_header = (struct usb_ms_header_descriptor*)hostif->extra; | |
1190 | if (hostif->extralen >= 7 && | |
1191 | ms_header->bLength >= 7 && | |
1192 | ms_header->bDescriptorType == USB_DT_CS_INTERFACE && | |
1193 | ms_header->bDescriptorSubtype == HEADER) | |
1194 | snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n", | |
1195 | ms_header->bcdMSC[1], ms_header->bcdMSC[0]); | |
1196 | else | |
1197 | snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n"); | |
1198 | ||
1199 | epidx = 0; | |
1200 | for (i = 0; i < intfd->bNumEndpoints; ++i) { | |
1201 | hostep = &hostif->endpoint[i]; | |
1202 | ep = get_ep_desc(hostep); | |
1203 | if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK && | |
1204 | (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) | |
1205 | continue; | |
1206 | ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra; | |
1207 | if (hostep->extralen < 4 || | |
1208 | ms_ep->bLength < 4 || | |
1209 | ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT || | |
1210 | ms_ep->bDescriptorSubtype != MS_GENERAL) | |
1211 | continue; | |
1212 | if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { | |
1213 | if (endpoints[epidx].out_ep) { | |
1214 | if (++epidx >= MIDI_MAX_ENDPOINTS) { | |
1215 | snd_printk(KERN_WARNING "too many endpoints\n"); | |
1216 | break; | |
1217 | } | |
1218 | } | |
1219 | endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1220 | if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) | |
1221 | endpoints[epidx].out_interval = ep->bInterval; | |
1222 | endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; | |
1223 | snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", | |
1224 | ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); | |
1225 | } else { | |
1226 | if (endpoints[epidx].in_ep) { | |
1227 | if (++epidx >= MIDI_MAX_ENDPOINTS) { | |
1228 | snd_printk(KERN_WARNING "too many endpoints\n"); | |
1229 | break; | |
1230 | } | |
1231 | } | |
1232 | endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1233 | if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) | |
1234 | endpoints[epidx].in_interval = ep->bInterval; | |
1235 | endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; | |
1236 | snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", | |
1237 | ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); | |
1238 | } | |
1239 | } | |
1240 | return 0; | |
1241 | } | |
1242 | ||
1243 | /* | |
1244 | * On Roland devices, use the second alternate setting to be able to use | |
1245 | * the interrupt input endpoint. | |
1246 | */ | |
1247 | static void snd_usbmidi_switch_roland_altsetting(snd_usb_midi_t* umidi) | |
1248 | { | |
1249 | struct usb_interface* intf; | |
1250 | struct usb_host_interface *hostif; | |
1251 | struct usb_interface_descriptor* intfd; | |
1252 | ||
1253 | intf = umidi->iface; | |
1254 | if (!intf || intf->num_altsetting != 2) | |
1255 | return; | |
1256 | ||
1257 | hostif = &intf->altsetting[1]; | |
1258 | intfd = get_iface_desc(hostif); | |
1259 | if (intfd->bNumEndpoints != 2 || | |
1260 | (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK || | |
1261 | (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) | |
1262 | return; | |
1263 | ||
1264 | snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n", | |
1265 | intfd->bAlternateSetting); | |
1266 | usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber, | |
1267 | intfd->bAlternateSetting); | |
1268 | } | |
1269 | ||
1270 | /* | |
1271 | * Try to find any usable endpoints in the interface. | |
1272 | */ | |
1273 | static int snd_usbmidi_detect_endpoints(snd_usb_midi_t* umidi, | |
1274 | snd_usb_midi_endpoint_info_t* endpoint, | |
1275 | int max_endpoints) | |
1276 | { | |
1277 | struct usb_interface* intf; | |
1278 | struct usb_host_interface *hostif; | |
1279 | struct usb_interface_descriptor* intfd; | |
1280 | struct usb_endpoint_descriptor* epd; | |
1281 | int i, out_eps = 0, in_eps = 0; | |
1282 | ||
27d10f56 | 1283 | if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582) |
1da177e4 LT |
1284 | snd_usbmidi_switch_roland_altsetting(umidi); |
1285 | ||
c1ab5d59 CL |
1286 | if (endpoint[0].out_ep || endpoint[0].in_ep) |
1287 | return 0; | |
1288 | ||
1da177e4 LT |
1289 | intf = umidi->iface; |
1290 | if (!intf || intf->num_altsetting < 1) | |
1291 | return -ENOENT; | |
1292 | hostif = intf->cur_altsetting; | |
1293 | intfd = get_iface_desc(hostif); | |
1294 | ||
1295 | for (i = 0; i < intfd->bNumEndpoints; ++i) { | |
1296 | epd = get_endpoint(hostif, i); | |
1297 | if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK && | |
1298 | (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) | |
1299 | continue; | |
1300 | if (out_eps < max_endpoints && | |
1301 | (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { | |
1302 | endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1303 | if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) | |
1304 | endpoint[out_eps].out_interval = epd->bInterval; | |
1305 | ++out_eps; | |
1306 | } | |
1307 | if (in_eps < max_endpoints && | |
1308 | (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) { | |
1309 | endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1310 | if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) | |
1311 | endpoint[in_eps].in_interval = epd->bInterval; | |
1312 | ++in_eps; | |
1313 | } | |
1314 | } | |
1315 | return (out_eps || in_eps) ? 0 : -ENOENT; | |
1316 | } | |
1317 | ||
1318 | /* | |
1319 | * Detects the endpoints for one-port-per-endpoint protocols. | |
1320 | */ | |
1321 | static int snd_usbmidi_detect_per_port_endpoints(snd_usb_midi_t* umidi, | |
1322 | snd_usb_midi_endpoint_info_t* endpoints) | |
1323 | { | |
1324 | int err, i; | |
1325 | ||
1326 | err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS); | |
1327 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
1328 | if (endpoints[i].out_ep) | |
1329 | endpoints[i].out_cables = 0x0001; | |
1330 | if (endpoints[i].in_ep) | |
1331 | endpoints[i].in_cables = 0x0001; | |
1332 | } | |
1333 | return err; | |
1334 | } | |
1335 | ||
1336 | /* | |
1337 | * Detects the endpoints and ports of Yamaha devices. | |
1338 | */ | |
1339 | static int snd_usbmidi_detect_yamaha(snd_usb_midi_t* umidi, | |
1340 | snd_usb_midi_endpoint_info_t* endpoint) | |
1341 | { | |
1342 | struct usb_interface* intf; | |
1343 | struct usb_host_interface *hostif; | |
1344 | struct usb_interface_descriptor* intfd; | |
1345 | uint8_t* cs_desc; | |
1346 | ||
1347 | intf = umidi->iface; | |
1348 | if (!intf) | |
1349 | return -ENOENT; | |
1350 | hostif = intf->altsetting; | |
1351 | intfd = get_iface_desc(hostif); | |
1352 | if (intfd->bNumEndpoints < 1) | |
1353 | return -ENOENT; | |
1354 | ||
1355 | /* | |
1356 | * For each port there is one MIDI_IN/OUT_JACK descriptor, not | |
1357 | * necessarily with any useful contents. So simply count 'em. | |
1358 | */ | |
1359 | for (cs_desc = hostif->extra; | |
1360 | cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; | |
1361 | cs_desc += cs_desc[0]) { | |
1362 | if (cs_desc[1] == CS_AUDIO_INTERFACE) { | |
1363 | if (cs_desc[2] == MIDI_IN_JACK) | |
1364 | endpoint->in_cables = (endpoint->in_cables << 1) | 1; | |
1365 | else if (cs_desc[2] == MIDI_OUT_JACK) | |
1366 | endpoint->out_cables = (endpoint->out_cables << 1) | 1; | |
1367 | } | |
1368 | } | |
1369 | if (!endpoint->in_cables && !endpoint->out_cables) | |
1370 | return -ENOENT; | |
1371 | ||
1372 | return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); | |
1373 | } | |
1374 | ||
1375 | /* | |
1376 | * Creates the endpoints and their ports for Midiman devices. | |
1377 | */ | |
1378 | static int snd_usbmidi_create_endpoints_midiman(snd_usb_midi_t* umidi, | |
1379 | snd_usb_midi_endpoint_info_t* endpoint) | |
1380 | { | |
1381 | snd_usb_midi_endpoint_info_t ep_info; | |
1382 | struct usb_interface* intf; | |
1383 | struct usb_host_interface *hostif; | |
1384 | struct usb_interface_descriptor* intfd; | |
1385 | struct usb_endpoint_descriptor* epd; | |
1386 | int cable, err; | |
1387 | ||
1388 | intf = umidi->iface; | |
1389 | if (!intf) | |
1390 | return -ENOENT; | |
1391 | hostif = intf->altsetting; | |
1392 | intfd = get_iface_desc(hostif); | |
1393 | /* | |
1394 | * The various MidiSport devices have more or less random endpoint | |
1395 | * numbers, so we have to identify the endpoints by their index in | |
1396 | * the descriptor array, like the driver for that other OS does. | |
1397 | * | |
1398 | * There is one interrupt input endpoint for all input ports, one | |
1399 | * bulk output endpoint for even-numbered ports, and one for odd- | |
1400 | * numbered ports. Both bulk output endpoints have corresponding | |
1401 | * input bulk endpoints (at indices 1 and 3) which aren't used. | |
1402 | */ | |
1403 | if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) { | |
1404 | snd_printdd(KERN_ERR "not enough endpoints\n"); | |
1405 | return -ENOENT; | |
1406 | } | |
1407 | ||
1408 | epd = get_endpoint(hostif, 0); | |
1409 | if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN || | |
1410 | (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) { | |
1411 | snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n"); | |
1412 | return -ENXIO; | |
1413 | } | |
1414 | epd = get_endpoint(hostif, 2); | |
1415 | if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT || | |
1416 | (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) { | |
1417 | snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n"); | |
1418 | return -ENXIO; | |
1419 | } | |
1420 | if (endpoint->out_cables > 0x0001) { | |
1421 | epd = get_endpoint(hostif, 4); | |
1422 | if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT || | |
1423 | (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) { | |
1424 | snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n"); | |
1425 | return -ENXIO; | |
1426 | } | |
1427 | } | |
1428 | ||
1429 | ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1430 | ep_info.out_cables = endpoint->out_cables & 0x5555; | |
1431 | err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); | |
1432 | if (err < 0) | |
1433 | return err; | |
1434 | ||
1435 | ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1436 | ep_info.in_interval = get_endpoint(hostif, 0)->bInterval; | |
1437 | ep_info.in_cables = endpoint->in_cables; | |
1438 | err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); | |
1439 | if (err < 0) | |
1440 | return err; | |
1441 | ||
1442 | if (endpoint->out_cables > 0x0001) { | |
1443 | ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; | |
1444 | ep_info.out_cables = endpoint->out_cables & 0xaaaa; | |
1445 | err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]); | |
1446 | if (err < 0) | |
1447 | return err; | |
1448 | } | |
1449 | ||
1450 | for (cable = 0; cable < 0x10; ++cable) { | |
1451 | if (endpoint->out_cables & (1 << cable)) | |
1452 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable, | |
1453 | &umidi->endpoints[cable & 1].out->ports[cable].substream); | |
1454 | if (endpoint->in_cables & (1 << cable)) | |
1455 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable, | |
1456 | &umidi->endpoints[0].in->ports[cable].substream); | |
1457 | } | |
1458 | return 0; | |
1459 | } | |
1460 | ||
1461 | static int snd_usbmidi_create_rawmidi(snd_usb_midi_t* umidi, | |
1462 | int out_ports, int in_ports) | |
1463 | { | |
1464 | snd_rawmidi_t* rmidi; | |
1465 | int err; | |
1466 | ||
1467 | err = snd_rawmidi_new(umidi->chip->card, "USB MIDI", | |
1468 | umidi->chip->next_midi_device++, | |
1469 | out_ports, in_ports, &rmidi); | |
1470 | if (err < 0) | |
1471 | return err; | |
1472 | strcpy(rmidi->name, umidi->chip->card->shortname); | |
1473 | rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | | |
1474 | SNDRV_RAWMIDI_INFO_INPUT | | |
1475 | SNDRV_RAWMIDI_INFO_DUPLEX; | |
1476 | rmidi->private_data = umidi; | |
1477 | rmidi->private_free = snd_usbmidi_rawmidi_free; | |
1478 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops); | |
1479 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops); | |
1480 | ||
1481 | umidi->rmidi = rmidi; | |
1482 | return 0; | |
1483 | } | |
1484 | ||
1485 | /* | |
1486 | * Temporarily stop input. | |
1487 | */ | |
1488 | void snd_usbmidi_input_stop(struct list_head* p) | |
1489 | { | |
1490 | snd_usb_midi_t* umidi; | |
1491 | int i; | |
1492 | ||
1493 | umidi = list_entry(p, snd_usb_midi_t, list); | |
1494 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
1495 | snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i]; | |
1496 | if (ep->in) | |
1497 | usb_kill_urb(ep->in->urb); | |
1498 | } | |
1499 | } | |
1500 | ||
1501 | static void snd_usbmidi_input_start_ep(snd_usb_midi_in_endpoint_t* ep) | |
1502 | { | |
1503 | if (ep) { | |
1504 | struct urb* urb = ep->urb; | |
1505 | urb->dev = ep->umidi->chip->dev; | |
1506 | snd_usbmidi_submit_urb(urb, GFP_KERNEL); | |
1507 | } | |
1508 | } | |
1509 | ||
1510 | /* | |
1511 | * Resume input after a call to snd_usbmidi_input_stop(). | |
1512 | */ | |
1513 | void snd_usbmidi_input_start(struct list_head* p) | |
1514 | { | |
1515 | snd_usb_midi_t* umidi; | |
1516 | int i; | |
1517 | ||
1518 | umidi = list_entry(p, snd_usb_midi_t, list); | |
1519 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) | |
1520 | snd_usbmidi_input_start_ep(umidi->endpoints[i].in); | |
1521 | } | |
1522 | ||
1523 | /* | |
1524 | * Creates and registers everything needed for a MIDI streaming interface. | |
1525 | */ | |
1526 | int snd_usb_create_midi_interface(snd_usb_audio_t* chip, | |
1527 | struct usb_interface* iface, | |
1528 | const snd_usb_audio_quirk_t* quirk) | |
1529 | { | |
1530 | snd_usb_midi_t* umidi; | |
1531 | snd_usb_midi_endpoint_info_t endpoints[MIDI_MAX_ENDPOINTS]; | |
1532 | int out_ports, in_ports; | |
1533 | int i, err; | |
1534 | ||
561b220a | 1535 | umidi = kzalloc(sizeof(*umidi), GFP_KERNEL); |
1da177e4 LT |
1536 | if (!umidi) |
1537 | return -ENOMEM; | |
1538 | umidi->chip = chip; | |
1539 | umidi->iface = iface; | |
1540 | umidi->quirk = quirk; | |
1541 | umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; | |
c8846970 CL |
1542 | init_timer(&umidi->error_timer); |
1543 | umidi->error_timer.function = snd_usbmidi_error_timer; | |
1544 | umidi->error_timer.data = (unsigned long)umidi; | |
1da177e4 LT |
1545 | |
1546 | /* detect the endpoint(s) to use */ | |
1547 | memset(endpoints, 0, sizeof(endpoints)); | |
d1bda045 CL |
1548 | switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) { |
1549 | case QUIRK_MIDI_STANDARD_INTERFACE: | |
1da177e4 | 1550 | err = snd_usbmidi_get_ms_info(umidi, endpoints); |
d1bda045 CL |
1551 | break; |
1552 | case QUIRK_MIDI_FIXED_ENDPOINT: | |
1553 | memcpy(&endpoints[0], quirk->data, | |
1554 | sizeof(snd_usb_midi_endpoint_info_t)); | |
1555 | err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); | |
1556 | break; | |
1557 | case QUIRK_MIDI_YAMAHA: | |
1558 | err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]); | |
1559 | break; | |
1560 | case QUIRK_MIDI_MIDIMAN: | |
1561 | umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops; | |
1562 | memcpy(&endpoints[0], quirk->data, | |
1563 | sizeof(snd_usb_midi_endpoint_info_t)); | |
1564 | err = 0; | |
1565 | break; | |
1566 | case QUIRK_MIDI_NOVATION: | |
1567 | umidi->usb_protocol_ops = &snd_usbmidi_novation_ops; | |
1568 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); | |
1569 | break; | |
1570 | case QUIRK_MIDI_RAW: | |
1571 | umidi->usb_protocol_ops = &snd_usbmidi_raw_ops; | |
1572 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); | |
1573 | break; | |
1574 | case QUIRK_MIDI_EMAGIC: | |
1575 | umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops; | |
1576 | memcpy(&endpoints[0], quirk->data, | |
1577 | sizeof(snd_usb_midi_endpoint_info_t)); | |
1578 | err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); | |
1579 | break; | |
1580 | case QUIRK_MIDI_MIDITECH: | |
1581 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); | |
1582 | break; | |
1583 | default: | |
1584 | snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); | |
1585 | err = -ENXIO; | |
1586 | break; | |
1da177e4 LT |
1587 | } |
1588 | if (err < 0) { | |
1589 | kfree(umidi); | |
1590 | return err; | |
1591 | } | |
1592 | ||
1593 | /* create rawmidi device */ | |
1594 | out_ports = 0; | |
1595 | in_ports = 0; | |
1596 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | |
1597 | out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables); | |
1598 | in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables); | |
1599 | } | |
1600 | err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports); | |
1601 | if (err < 0) { | |
1602 | kfree(umidi); | |
1603 | return err; | |
1604 | } | |
1605 | ||
1606 | /* create endpoint/port structures */ | |
1607 | if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN) | |
1608 | err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]); | |
1609 | else | |
1610 | err = snd_usbmidi_create_endpoints(umidi, endpoints); | |
1611 | if (err < 0) { | |
1612 | snd_usbmidi_free(umidi); | |
1613 | return err; | |
1614 | } | |
1615 | ||
1616 | list_add(&umidi->list, &umidi->chip->midi_list); | |
1617 | ||
1618 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) | |
1619 | snd_usbmidi_input_start_ep(umidi->endpoints[i].in); | |
1620 | return 0; | |
1621 | } | |
1622 | ||
1623 | EXPORT_SYMBOL(snd_usb_create_midi_interface); | |
1624 | EXPORT_SYMBOL(snd_usbmidi_input_stop); | |
1625 | EXPORT_SYMBOL(snd_usbmidi_input_start); | |
1626 | EXPORT_SYMBOL(snd_usbmidi_disconnect); |