1 /*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
4 * Copyright (c) 2002-2004 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/interrupt.h>
42 #include <linux/spinlock.h>
43 #include <linux/string.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/usb.h>
47 #include <sound/core.h>
48 #include <sound/minors.h>
49 #include <sound/rawmidi.h>
50 #include "usbaudio.h"
51
52 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
53 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
54 MODULE_LICENSE("Dual BSD/GPL");
55
56
57 struct usb_ms_header_descriptor {
58 __u8 bLength;
59 __u8 bDescriptorType;
60 __u8 bDescriptorSubtype;
61 __u8 bcdMSC[2];
62 __le16 wTotalLength;
63 } __attribute__ ((packed));
64
65 struct usb_ms_endpoint_descriptor {
66 __u8 bLength;
67 __u8 bDescriptorType;
68 __u8 bDescriptorSubtype;
69 __u8 bNumEmbMIDIJack;
70 __u8 baAssocJackID[0];
71 } __attribute__ ((packed));
72
73 typedef struct snd_usb_midi snd_usb_midi_t;
74 typedef struct snd_usb_midi_endpoint snd_usb_midi_endpoint_t;
75 typedef struct snd_usb_midi_out_endpoint snd_usb_midi_out_endpoint_t;
76 typedef struct snd_usb_midi_in_endpoint snd_usb_midi_in_endpoint_t;
77 typedef struct usbmidi_out_port usbmidi_out_port_t;
78 typedef struct usbmidi_in_port usbmidi_in_port_t;
79
80 struct snd_usb_midi {
81 snd_usb_audio_t *chip;
82 struct usb_interface *iface;
83 const snd_usb_audio_quirk_t *quirk;
84 snd_rawmidi_t* rmidi;
85 struct list_head list;
86
87 struct snd_usb_midi_endpoint {
88 snd_usb_midi_out_endpoint_t *out;
89 snd_usb_midi_in_endpoint_t *in;
90 } endpoints[MIDI_MAX_ENDPOINTS];
91 };
92
93 struct snd_usb_midi_out_endpoint {
94 snd_usb_midi_t* umidi;
95 struct urb* urb;
96 int max_transfer; /* size of urb buffer */
97 struct tasklet_struct tasklet;
98
99 spinlock_t buffer_lock;
100
101 struct usbmidi_out_port {
102 snd_usb_midi_out_endpoint_t* ep;
103 snd_rawmidi_substream_t* substream;
104 int active;
105 uint8_t cable; /* cable number << 4 */
106 uint8_t state;
107 #define STATE_UNKNOWN 0
108 #define STATE_1PARAM 1
109 #define STATE_2PARAM_1 2
110 #define STATE_2PARAM_2 3
111 #define STATE_SYSEX_0 4
112 #define STATE_SYSEX_1 5
113 #define STATE_SYSEX_2 6
114 uint8_t data[2];
115 } ports[0x10];
116 };
117
118 struct snd_usb_midi_in_endpoint {
119 snd_usb_midi_t* umidi;
120 struct urb* urb;
121 struct usbmidi_in_port {
122 snd_rawmidi_substream_t* substream;
123 } ports[0x10];
124 };
125
126 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep);
127
128 static const uint8_t snd_usbmidi_cin_length[] = {
129 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
130 };
131
132 /*
133 * Submits the URB, with error handling.
134 */
135 static int snd_usbmidi_submit_urb(struct urb* urb, int flags)
136 {
137 int err = usb_submit_urb(urb, flags);
138 if (err < 0 && err != -ENODEV)
139 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
140 return err;
141 }
142
143 /*
144 * Error handling for URB completion functions.
145 */
146 static int snd_usbmidi_urb_error(int status)
147 {
148 if (status == -ENOENT)
149 return status; /* killed */
150 if (status == -EILSEQ ||
151 status == -ECONNRESET ||
152 status == -ETIMEDOUT)
153 return -ENODEV; /* device removed/shutdown */
154 snd_printk(KERN_ERR "urb status %d\n", status);
155 return 0; /* continue */
156 }
157
158 /*
159 * Receives a USB MIDI packet.
160 */
161 static void snd_usbmidi_input_packet(snd_usb_midi_in_endpoint_t* ep,
162 uint8_t packet[4])
163 {
164 int cable = packet[0] >> 4;
165 usbmidi_in_port_t* port = &ep->ports[cable];
166
167 if (!port->substream) {
168 snd_printd("unexpected port %d!\n", cable);
169 return;
170 }
171 if (!port->substream->runtime ||
172 !port->substream->runtime->trigger)
173 return;
174 snd_rawmidi_receive(port->substream, &packet[1],
175 snd_usbmidi_cin_length[packet[0] & 0x0f]);
176 }
177
178 /*
179 * Processes the data read from the device.
180 */
181 static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs)
182 {
183 snd_usb_midi_in_endpoint_t* ep = urb->context;
184
185 if (urb->status == 0) {
186 uint8_t* buffer = (uint8_t*)ep->urb->transfer_buffer;
187 int i;
188
189 for (i = 0; i + 4 <= urb->actual_length; i += 4)
190 if (buffer[i] != 0)
191 snd_usbmidi_input_packet(ep, &buffer[i]);
192 } else {
193 if (snd_usbmidi_urb_error(urb->status) < 0)
194 return;
195 }
196
197 if (usb_pipe_needs_resubmit(urb->pipe)) {
198 urb->dev = ep->umidi->chip->dev;
199 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
200 }
201 }
202
203 /*
204 * Converts the data read from a Midiman device to standard USB MIDI packets.
205 */
206 static void snd_usbmidi_in_midiman_complete(struct urb* urb, struct pt_regs *regs)
207 {
208 if (urb->status == 0) {
209 uint8_t* buffer = (uint8_t*)urb->transfer_buffer;
210 int i;
211
212 for (i = 0; i + 4 <= urb->actual_length; i += 4) {
213 if (buffer[i + 3] != 0) {
214 /*
215 * snd_usbmidi_input_packet() doesn't check the
216 * contents of the message, so we simply use
217 * some random CIN with the desired length.
218 */
219 static const uint8_t cin[4] = {
220 0x0, 0xf, 0x2, 0x3
221 };
222 uint8_t ctl = buffer[i + 3];
223 buffer[i + 3] = buffer[i + 2];
224 buffer[i + 2] = buffer[i + 1];
225 buffer[i + 1] = buffer[i + 0];
226 buffer[i + 0] = (ctl & 0xf0) | cin[ctl & 3];
227 } else {
228 buffer[i + 0] = 0;
229 }
230 }
231 }
232 snd_usbmidi_in_urb_complete(urb, regs);
233 }
234
235 static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs)
236 {
237 snd_usb_midi_out_endpoint_t* ep = urb->context;
238
239 if (urb->status < 0) {
240 if (snd_usbmidi_urb_error(urb->status) < 0)
241 return;
242 }
243 snd_usbmidi_do_output(ep);
244 }
245
246 /*
247 * Converts standard USB MIDI packets to what Midman devices expect.
248 */
249 static void snd_usbmidi_convert_to_midiman(struct urb* urb)
250 {
251 uint8_t* buffer = (uint8_t*)urb->transfer_buffer;
252 int i;
253
254 for (i = 0; i + 4 <= urb->transfer_buffer_length; i += 4) {
255 uint8_t cin = buffer[i];
256 buffer[i + 0] = buffer[i + 1];
257 buffer[i + 1] = buffer[i + 2];
258 buffer[i + 2] = buffer[i + 3];
259 buffer[i + 3] = (cin & 0xf0) | snd_usbmidi_cin_length[cin & 0x0f];
260 }
261 }
262
263 /*
264 * Adds one USB MIDI packet to the output buffer.
265 */
266 static inline void output_packet(struct urb* urb,
267 uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3)
268 {
269
270 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
271 buf[0] = p0;
272 buf[1] = p1;
273 buf[2] = p2;
274 buf[3] = p3;
275 urb->transfer_buffer_length += 4;
276 }
277
278 /*
279 * Converts MIDI commands to USB MIDI packets.
280 */
281 static void snd_usbmidi_transmit_byte(usbmidi_out_port_t* port,
282 uint8_t b, struct urb* urb)
283 {
284 uint8_t p0 = port->cable;
285
286 if (b >= 0xf8) {
287 output_packet(urb, p0 | 0x0f, b, 0, 0);
288 } else if (b >= 0xf0) {
289 switch (b) {
290 case 0xf0:
291 port->data[0] = b;
292 port->state = STATE_SYSEX_1;
293 break;
294 case 0xf1:
295 case 0xf3:
296 port->data[0] = b;
297 port->state = STATE_1PARAM;
298 break;
299 case 0xf2:
300 port->data[0] = b;
301 port->state = STATE_2PARAM_1;
302 break;
303 case 0xf4:
304 case 0xf5:
305 port->state = STATE_UNKNOWN;
306 break;
307 case 0xf6:
308 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
309 port->state = STATE_UNKNOWN;
310 break;
311 case 0xf7:
312 switch (port->state) {
313 case STATE_SYSEX_0:
314 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
315 break;
316 case STATE_SYSEX_1:
317 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
318 break;
319 case STATE_SYSEX_2:
320 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
321 break;
322 }
323 port->state = STATE_UNKNOWN;
324 break;
325 }
326 } else if (b >= 0x80) {
327 port->data[0] = b;
328 if (b >= 0xc0 && b <= 0xdf)
329 port->state = STATE_1PARAM;
330 else
331 port->state = STATE_2PARAM_1;
332 } else { /* b < 0x80 */
333 switch (port->state) {
334 case STATE_1PARAM:
335 if (port->data[0] < 0xf0) {
336 p0 |= port->data[0] >> 4;
337 } else {
338 p0 |= 0x02;
339 port->state = STATE_UNKNOWN;
340 }
341 output_packet(urb, p0, port->data[0], b, 0);
342 break;
343 case STATE_2PARAM_1:
344 port->data[1] = b;
345 port->state = STATE_2PARAM_2;
346 break;
347 case STATE_2PARAM_2:
348 if (port->data[0] < 0xf0) {
349 p0 |= port->data[0] >> 4;
350 port->state = STATE_2PARAM_1;
351 } else {
352 p0 |= 0x03;
353 port->state = STATE_UNKNOWN;
354 }
355 output_packet(urb, p0, port->data[0], port->data[1], b);
356 break;
357 case STATE_SYSEX_0:
358 port->data[0] = b;
359 port->state = STATE_SYSEX_1;
360 break;
361 case STATE_SYSEX_1:
362 port->data[1] = b;
363 port->state = STATE_SYSEX_2;
364 break;
365 case STATE_SYSEX_2:
366 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
367 port->state = STATE_SYSEX_0;
368 break;
369 }
370 }
371 }
372
373 /*
374 * Moves data from one substream buffer to the URB transfer buffer.
375 */
376 static void snd_usbmidi_transmit(snd_usb_midi_out_endpoint_t* ep, int port_idx)
377 {
378 struct urb* urb = ep->urb;
379 usbmidi_out_port_t* port = &ep->ports[port_idx];
380
381 while (urb->transfer_buffer_length < ep->max_transfer) {
382 uint8_t b;
383 if (snd_rawmidi_transmit_peek(port->substream, &b, 1) != 1) {
384 port->active = 0;
385 break;
386 }
387 snd_usbmidi_transmit_byte(port, b, urb);
388 snd_rawmidi_transmit_ack(port->substream, 1);
389 }
390 }
391
392 /*
393 * This is called when some data should be transferred to the device
394 * (from one or more substreams).
395 */
396 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep)
397 {
398 int p;
399 struct urb* urb = ep->urb;
400 unsigned long flags;
401
402 spin_lock_irqsave(&ep->buffer_lock, flags);
403 if (urb->status == -EINPROGRESS || ep->umidi->chip->shutdown) {
404 spin_unlock_irqrestore(&ep->buffer_lock, flags);
405 return;
406 }
407
408 urb->transfer_buffer_length = 0;
409 for (p= 0; p < 0x10; ++p)
410 if (ep->ports[p].active)
411 snd_usbmidi_transmit(ep, p);
412
413 if (urb->transfer_buffer_length > 0) {
414 if (ep->umidi->quirk && ep->umidi->quirk->type == QUIRK_MIDI_MIDIMAN)
415 snd_usbmidi_convert_to_midiman(urb);
416
417 urb->dev = ep->umidi->chip->dev;
418 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
419 }
420 spin_unlock_irqrestore(&ep->buffer_lock, flags);
421 }
422
423 static void snd_usbmidi_out_tasklet(unsigned long data)
424 {
425 snd_usb_midi_out_endpoint_t* ep = (snd_usb_midi_out_endpoint_t *) data;
426
427 snd_usbmidi_do_output(ep);
428 }
429
430 static int snd_usbmidi_output_open(snd_rawmidi_substream_t* substream)
431 {
432 snd_usb_midi_t* umidi = substream->rmidi->private_data;
433 usbmidi_out_port_t* port = NULL;
434 int i, j;
435
436 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
437 if (umidi->endpoints[i].out)
438 for (j = 0; j < 0x10; ++j)
439 if (umidi->endpoints[i].out->ports[j].substream == substream) {
440 port = &umidi->endpoints[i].out->ports[j];
441 break;
442 }
443 if (!port) {
444 snd_BUG();
445 return -ENXIO;
446 }
447 substream->runtime->private_data = port;
448 port->state = STATE_UNKNOWN;
449 return 0;
450 }
451
452 static int snd_usbmidi_output_close(snd_rawmidi_substream_t* substream)
453 {
454 return 0;
455 }
456
457 static void snd_usbmidi_output_trigger(snd_rawmidi_substream_t* substream, int up)
458 {
459 usbmidi_out_port_t* port = (usbmidi_out_port_t*)substream->runtime->private_data;
460
461 port->active = up;
462 if (up) {
463 if (port->ep->umidi->chip->shutdown) {
464 /* gobble up remaining bytes to prevent wait in
465 * snd_rawmidi_drain_output */
466 while (!snd_rawmidi_transmit_empty(substream))
467 snd_rawmidi_transmit_ack(substream, 1);
468 return;
469 }
470 tasklet_hi_schedule(&port->ep->tasklet);
471 }
472 }
473
474 static int snd_usbmidi_input_open(snd_rawmidi_substream_t* substream)
475 {
476 return 0;
477 }
478
479 static int snd_usbmidi_input_close(snd_rawmidi_substream_t* substream)
480 {
481 return 0;
482 }
483
484 static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t* substream, int up)
485 {
486 }
487
488 static snd_rawmidi_ops_t snd_usbmidi_output_ops = {
489 .open = snd_usbmidi_output_open,
490 .close = snd_usbmidi_output_close,
491 .trigger = snd_usbmidi_output_trigger,
492 };
493
494 static snd_rawmidi_ops_t snd_usbmidi_input_ops = {
495 .open = snd_usbmidi_input_open,
496 .close = snd_usbmidi_input_close,
497 .trigger = snd_usbmidi_input_trigger
498 };
499
500 /*
501 * Frees an input endpoint.
502 * May be called when ep hasn't been initialized completely.
503 */
504 static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t* ep)
505 {
506 if (ep->urb) {
507 kfree(ep->urb->transfer_buffer);
508 usb_free_urb(ep->urb);
509 }
510 kfree(ep);
511 }
512
513 /*
514 * For Roland devices, use the alternate setting which uses interrupt
515 * transfers for input.
516 */
517 static struct usb_endpoint_descriptor* snd_usbmidi_get_int_epd(snd_usb_midi_t* umidi)
518 {
519 struct usb_interface* intf;
520 struct usb_host_interface *hostif;
521 struct usb_interface_descriptor* intfd;
522
523 if (le16_to_cpu(umidi->chip->dev->descriptor.idVendor) != 0x0582)
524 return NULL;
525 intf = umidi->iface;
526 if (!intf || intf->num_altsetting != 2)
527 return NULL;
528
529 hostif = &intf->altsetting[0];
530 intfd = get_iface_desc(hostif);
531 if (intfd->bNumEndpoints != 2 ||
532 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
533 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
534 return NULL;
535
536 hostif = &intf->altsetting[1];
537 intfd = get_iface_desc(hostif);
538 if (intfd->bNumEndpoints != 2 ||
539 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
540 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
541 return NULL;
542
543 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
544 intfd->bAlternateSetting);
545 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
546 intfd->bAlternateSetting);
547 return get_endpoint(hostif, 1);
548 }
549
550 static struct usb_endpoint_descriptor* snd_usbmidi_get_midiman_int_epd(snd_usb_midi_t* umidi)
551 {
552 struct usb_interface* intf = umidi->iface;
553 struct usb_host_interface *hostif;
554 struct usb_interface_descriptor *intfd;
555 if (!intf)
556 return NULL;
557 hostif = &intf->altsetting[0];
558 intfd = get_iface_desc(hostif);
559 if (intfd->bNumEndpoints < 1)
560 return NULL;
561 return get_endpoint(hostif, 0);
562 }
563
564 /*
565 * Creates an input endpoint.
566 */
567 static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t* umidi,
568 snd_usb_midi_endpoint_info_t* ep_info,
569 snd_usb_midi_endpoint_t* rep)
570 {
571 snd_usb_midi_in_endpoint_t* ep;
572 struct usb_endpoint_descriptor* int_epd;
573 void* buffer;
574 unsigned int pipe;
575 int length;
576
577 rep->in = NULL;
578 ep = kcalloc(1, sizeof(*ep), GFP_KERNEL);
579 if (!ep)
580 return -ENOMEM;
581 ep->umidi = umidi;
582
583 if (umidi->quirk && umidi->quirk->type == QUIRK_MIDI_MIDIMAN)
584 int_epd = snd_usbmidi_get_midiman_int_epd(umidi);
585 else
586 int_epd = snd_usbmidi_get_int_epd(umidi);
587
588 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
589 if (!ep->urb) {
590 snd_usbmidi_in_endpoint_delete(ep);
591 return -ENOMEM;
592 }
593 if (int_epd)
594 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
595 else
596 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
597 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
598 buffer = kmalloc(length, GFP_KERNEL);
599 if (!buffer) {
600 snd_usbmidi_in_endpoint_delete(ep);
601 return -ENOMEM;
602 }
603 if (int_epd)
604 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer, length,
605 snd_usb_complete_callback(snd_usbmidi_in_urb_complete),
606 ep, int_epd->bInterval);
607 else
608 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, length,
609 snd_usb_complete_callback(snd_usbmidi_in_urb_complete),
610 ep);
611
612 rep->in = ep;
613 return 0;
614 }
615
616 static int snd_usbmidi_count_bits(uint16_t x)
617 {
618 int i, bits = 0;
619
620 for (i = 0; i < 16; ++i)
621 bits += (x & (1 << i)) != 0;
622 return bits;
623 }
624
625 /*
626 * Frees an output endpoint.
627 * May be called when ep hasn't been initialized completely.
628 */
629 static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t* ep)
630 {
631 if (ep->tasklet.func)
632 tasklet_kill(&ep->tasklet);
633 if (ep->urb) {
634 kfree(ep->urb->transfer_buffer);
635 usb_free_urb(ep->urb);
636 }
637 kfree(ep);
638 }
639
640 /*
641 * Creates an output endpoint, and initializes output ports.
642 */
643 static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t* umidi,
644 snd_usb_midi_endpoint_info_t* ep_info,
645 snd_usb_midi_endpoint_t* rep)
646 {
647 snd_usb_midi_out_endpoint_t* ep;
648 int i;
649 unsigned int pipe;
650 void* buffer;
651
652 rep->out = NULL;
653 ep = kcalloc(1, sizeof(*ep), GFP_KERNEL);
654 if (!ep)
655 return -ENOMEM;
656 ep->umidi = umidi;
657
658 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
659 if (!ep->urb) {
660 snd_usbmidi_out_endpoint_delete(ep);
661 return -ENOMEM;
662 }
663 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
664 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1) & ~3;
665 buffer = kmalloc(ep->max_transfer, GFP_KERNEL);
666 if (!buffer) {
667 snd_usbmidi_out_endpoint_delete(ep);
668 return -ENOMEM;
669 }
670 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
671 ep->max_transfer,
672 snd_usb_complete_callback(snd_usbmidi_out_urb_complete), ep);
673
674 spin_lock_init(&ep->buffer_lock);
675 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
676
677 for (i = 0; i < 0x10; ++i)
678 if (ep_info->out_cables & (1 << i)) {
679 ep->ports[i].ep = ep;
680 ep->ports[i].cable = i << 4;
681 }
682
683 rep->out = ep;
684 return 0;
685 }
686
687 /*
688 * Frees everything.
689 */
690 static void snd_usbmidi_free(snd_usb_midi_t* umidi)
691 {
692 int i;
693
694 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
695 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
696 if (ep->out)
697 snd_usbmidi_out_endpoint_delete(ep->out);
698 if (ep->in)
699 snd_usbmidi_in_endpoint_delete(ep->in);
700 }
701 kfree(umidi);
702 }
703
704 /*
705 * Unlinks all URBs (must be done before the usb_device is deleted).
706 */
707 void snd_usbmidi_disconnect(struct list_head* p, struct usb_driver *driver)
708 {
709 snd_usb_midi_t* umidi;
710 int i;
711
712 umidi = list_entry(p, snd_usb_midi_t, list);
713 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
714 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
715 if (ep->out && ep->out->urb)
716 usb_kill_urb(ep->out->urb);
717 if (ep->in && ep->in->urb)
718 usb_kill_urb(ep->in->urb);
719 }
720 }
721
722 static void snd_usbmidi_rawmidi_free(snd_rawmidi_t* rmidi)
723 {
724 snd_usb_midi_t* umidi = rmidi->private_data;
725 snd_usbmidi_free(umidi);
726 }
727
728 static snd_rawmidi_substream_t* snd_usbmidi_find_substream(snd_usb_midi_t* umidi,
729 int stream, int number)
730 {
731 struct list_head* list;
732
733 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
734 snd_rawmidi_substream_t* substream = list_entry(list, snd_rawmidi_substream_t, list);
735 if (substream->number == number)
736 return substream;
737 }
738 return NULL;
739 }
740
741 /*
742 * This list specifies names for ports that do not fit into the standard
743 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
744 * such as internal control or synthesizer ports.
745 */
746 static struct {
747 __u16 vendor;
748 __u16 product;
749 int port;
750 const char *name_format;
751 } snd_usbmidi_port_names[] = {
752 /* Roland UA-100 */
753 {0x0582, 0x0000, 2, "%s Control"},
754 /* Roland SC-8850 */
755 {0x0582, 0x0003, 0, "%s Part A"},
756 {0x0582, 0x0003, 1, "%s Part B"},
757 {0x0582, 0x0003, 2, "%s Part C"},
758 {0x0582, 0x0003, 3, "%s Part D"},
759 {0x0582, 0x0003, 4, "%s MIDI 1"},
760 {0x0582, 0x0003, 5, "%s MIDI 2"},
761 /* Roland U-8 */
762 {0x0582, 0x0004, 0, "%s MIDI"},
763 {0x0582, 0x0004, 1, "%s Control"},
764 /* Roland SC-8820 */
765 {0x0582, 0x0007, 0, "%s Part A"},
766 {0x0582, 0x0007, 1, "%s Part B"},
767 {0x0582, 0x0007, 2, "%s MIDI"},
768 /* Roland SK-500 */
769 {0x0582, 0x000b, 0, "%s Part A"},
770 {0x0582, 0x000b, 1, "%s Part B"},
771 {0x0582, 0x000b, 2, "%s MIDI"},
772 /* Roland SC-D70 */
773 {0x0582, 0x000c, 0, "%s Part A"},
774 {0x0582, 0x000c, 1, "%s Part B"},
775 {0x0582, 0x000c, 2, "%s MIDI"},
776 /* Edirol UM-880 */
777 {0x0582, 0x0014, 8, "%s Control"},
778 /* Edirol SD-90 */
779 {0x0582, 0x0016, 0, "%s Part A"},
780 {0x0582, 0x0016, 1, "%s Part B"},
781 {0x0582, 0x0016, 2, "%s MIDI 1"},
782 {0x0582, 0x0016, 3, "%s MIDI 2"},
783 /* Edirol UM-550 */
784 {0x0582, 0x0023, 5, "%s Control"},
785 /* Edirol SD-20 */
786 {0x0582, 0x0027, 0, "%s Part A"},
787 {0x0582, 0x0027, 1, "%s Part B"},
788 {0x0582, 0x0027, 2, "%s MIDI"},
789 /* Edirol SD-80 */
790 {0x0582, 0x0029, 0, "%s Part A"},
791 {0x0582, 0x0029, 1, "%s Part B"},
792 {0x0582, 0x0029, 2, "%s MIDI 1"},
793 {0x0582, 0x0029, 3, "%s MIDI 2"},
794 /* Edirol UA-700 */
795 {0x0582, 0x002b, 0, "%s MIDI"},
796 {0x0582, 0x002b, 1, "%s Control"},
797 /* Roland VariOS */
798 {0x0582, 0x002f, 0, "%s MIDI"},
799 {0x0582, 0x002f, 1, "%s External MIDI"},
800 {0x0582, 0x002f, 2, "%s Sync"},
801 /* Edirol PCR */
802 {0x0582, 0x0033, 0, "%s MIDI"},
803 {0x0582, 0x0033, 1, "%s 1"},
804 {0x0582, 0x0033, 2, "%s 2"},
805 /* BOSS GS-10 */
806 {0x0582, 0x003b, 0, "%s MIDI"},
807 {0x0582, 0x003b, 1, "%s Control"},
808 /* Edirol UA-1000 */
809 {0x0582, 0x0044, 0, "%s MIDI"},
810 {0x0582, 0x0044, 1, "%s Control"},
811 /* Edirol UR-80 */
812 {0x0582, 0x0048, 0, "%s MIDI"},
813 {0x0582, 0x0048, 1, "%s 1"},
814 {0x0582, 0x0048, 2, "%s 2"},
815 /* Edirol PCR-A */
816 {0x0582, 0x004d, 0, "%s MIDI"},
817 {0x0582, 0x004d, 1, "%s 1"},
818 {0x0582, 0x004d, 2, "%s 2"},
819 /* M-Audio MidiSport 8x8 */
820 {0x0763, 0x1031, 8, "%s Control"},
821 {0x0763, 0x1033, 8, "%s Control"},
822 };
823
824 static void snd_usbmidi_init_substream(snd_usb_midi_t* umidi,
825 int stream, int number,
826 snd_rawmidi_substream_t** rsubstream)
827 {
828 int i;
829 __u16 vendor, product;
830 const char *name_format;
831
832 snd_rawmidi_substream_t* substream = snd_usbmidi_find_substream(umidi, stream, number);
833 if (!substream) {
834 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
835 return;
836 }
837
838 /* TODO: read port name from jack descriptor */
839 name_format = "%s MIDI %d";
840 vendor = le16_to_cpu(umidi->chip->dev->descriptor.idVendor);
841 product = le16_to_cpu(umidi->chip->dev->descriptor.idProduct);
842 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_names); ++i) {
843 if (snd_usbmidi_port_names[i].vendor == vendor &&
844 snd_usbmidi_port_names[i].product == product &&
845 snd_usbmidi_port_names[i].port == number) {
846 name_format = snd_usbmidi_port_names[i].name_format;
847 break;
848 }
849 }
850 snprintf(substream->name, sizeof(substream->name),
851 name_format, umidi->chip->card->shortname, number + 1);
852
853 *rsubstream = substream;
854 }
855
856 /*
857 * Creates the endpoints and their ports.
858 */
859 static int snd_usbmidi_create_endpoints(snd_usb_midi_t* umidi,
860 snd_usb_midi_endpoint_info_t* endpoints)
861 {
862 int i, j, err;
863 int out_ports = 0, in_ports = 0;
864
865 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
866 if (endpoints[i].out_cables) {
867 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
868 &umidi->endpoints[i]);
869 if (err < 0)
870 return err;
871 }
872 if (endpoints[i].in_cables) {
873 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
874 &umidi->endpoints[i]);
875 if (err < 0)
876 return err;
877 }
878
879 for (j = 0; j < 0x10; ++j) {
880 if (endpoints[i].out_cables & (1 << j)) {
881 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
882 &umidi->endpoints[i].out->ports[j].substream);
883 ++out_ports;
884 }
885 if (endpoints[i].in_cables & (1 << j)) {
886 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
887 &umidi->endpoints[i].in->ports[j].substream);
888 ++in_ports;
889 }
890 }
891 }
892 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
893 out_ports, in_ports);
894 return 0;
895 }
896
897 /*
898 * Returns MIDIStreaming device capabilities.
899 */
900 static int snd_usbmidi_get_ms_info(snd_usb_midi_t* umidi,
901 snd_usb_midi_endpoint_info_t* endpoints)
902 {
903 struct usb_interface* intf;
904 struct usb_host_interface *hostif;
905 struct usb_interface_descriptor* intfd;
906 struct usb_ms_header_descriptor* ms_header;
907 struct usb_host_endpoint *hostep;
908 struct usb_endpoint_descriptor* ep;
909 struct usb_ms_endpoint_descriptor* ms_ep;
910 int i, epidx;
911
912 intf = umidi->iface;
913 if (!intf)
914 return -ENXIO;
915 hostif = &intf->altsetting[0];
916 intfd = get_iface_desc(hostif);
917 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
918 if (hostif->extralen >= 7 &&
919 ms_header->bLength >= 7 &&
920 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
921 ms_header->bDescriptorSubtype == HEADER)
922 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
923 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
924 else
925 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
926
927 epidx = 0;
928 for (i = 0; i < intfd->bNumEndpoints; ++i) {
929 hostep = &hostif->endpoint[i];
930 ep = get_ep_desc(hostep);
931 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
932 continue;
933 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
934 if (hostep->extralen < 4 ||
935 ms_ep->bLength < 4 ||
936 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
937 ms_ep->bDescriptorSubtype != MS_GENERAL)
938 continue;
939 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
940 if (endpoints[epidx].out_ep) {
941 if (++epidx >= MIDI_MAX_ENDPOINTS) {
942 snd_printk(KERN_WARNING "too many endpoints\n");
943 break;
944 }
945 }
946 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
947 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
948 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
949 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
950 } else {
951 if (endpoints[epidx].in_ep) {
952 if (++epidx >= MIDI_MAX_ENDPOINTS) {
953 snd_printk(KERN_WARNING "too many endpoints\n");
954 break;
955 }
956 }
957 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
958 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
959 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
960 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
961 }
962 }
963 return 0;
964 }
965
966 /*
967 * If the endpoints aren't specified, use the first bulk endpoints in the
968 * first alternate setting of the interface.
969 */
970 static int snd_usbmidi_detect_endpoint(snd_usb_midi_t* umidi,
971 snd_usb_midi_endpoint_info_t* endpoint)
972 {
973 struct usb_interface* intf;
974 struct usb_host_interface *hostif;
975 struct usb_interface_descriptor* intfd;
976 struct usb_endpoint_descriptor* epd;
977 int i;
978
979 intf = umidi->iface;
980 if (!intf || intf->num_altsetting < 1)
981 return -ENOENT;
982 hostif = intf->altsetting;
983 intfd = get_iface_desc(hostif);
984 if (intfd->bNumEndpoints < 1)
985 return -ENOENT;
986
987 for (i = 0; i < intfd->bNumEndpoints; ++i) {
988 epd = get_endpoint(hostif, i);
989 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
990 continue;
991 if (!endpoint->out_ep && endpoint->out_cables &&
992 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
993 endpoint->out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
994 if (!endpoint->in_ep && endpoint->in_cables &&
995 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
996 endpoint->in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
997 }
998 return 0;
999 }
1000
1001 /*
1002 * Detects the endpoints and ports of Yamaha devices.
1003 */
1004 static int snd_usbmidi_detect_yamaha(snd_usb_midi_t* umidi,
1005 snd_usb_midi_endpoint_info_t* endpoint)
1006 {
1007 struct usb_interface* intf;
1008 struct usb_host_interface *hostif;
1009 struct usb_interface_descriptor* intfd;
1010 uint8_t* cs_desc;
1011
1012 intf = umidi->iface;
1013 if (!intf)
1014 return -ENOENT;
1015 hostif = intf->altsetting;
1016 intfd = get_iface_desc(hostif);
1017 if (intfd->bNumEndpoints < 1)
1018 return -ENOENT;
1019
1020 /*
1021 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1022 * necessarily with any useful contents. So simply count 'em.
1023 */
1024 for (cs_desc = hostif->extra;
1025 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1026 cs_desc += cs_desc[0]) {
1027 if (cs_desc[1] == CS_AUDIO_INTERFACE) {
1028 if (cs_desc[2] == MIDI_IN_JACK)
1029 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1030 else if (cs_desc[2] == MIDI_OUT_JACK)
1031 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1032 }
1033 }
1034 if (!endpoint->in_cables && !endpoint->out_cables)
1035 return -ENOENT;
1036
1037 return snd_usbmidi_detect_endpoint(umidi, endpoint);
1038 }
1039
1040 /*
1041 * Creates the endpoints and their ports for Midiman devices.
1042 */
1043 static int snd_usbmidi_create_endpoints_midiman(snd_usb_midi_t* umidi,
1044 snd_usb_midi_endpoint_info_t* endpoint)
1045 {
1046 snd_usb_midi_endpoint_info_t ep_info;
1047 struct usb_interface* intf;
1048 struct usb_host_interface *hostif;
1049 struct usb_interface_descriptor* intfd;
1050 struct usb_endpoint_descriptor* epd;
1051 int cable, err;
1052
1053 intf = umidi->iface;
1054 if (!intf)
1055 return -ENOENT;
1056 hostif = intf->altsetting;
1057 intfd = get_iface_desc(hostif);
1058 /*
1059 * The various MidiSport devices have more or less random endpoint
1060 * numbers, so we have to identify the endpoints by their index in
1061 * the descriptor array, like the driver for that other OS does.
1062 *
1063 * There is one interrupt input endpoint for all input ports, one
1064 * bulk output endpoint for even-numbered ports, and one for odd-
1065 * numbered ports. Both bulk output endpoints have corresponding
1066 * input bulk endpoints (at indices 1 and 3) which aren't used.
1067 */
1068 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1069 snd_printdd(KERN_ERR "not enough endpoints\n");
1070 return -ENOENT;
1071 }
1072
1073 epd = get_endpoint(hostif, 0);
1074 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1075 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1076 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1077 return -ENXIO;
1078 }
1079 epd = get_endpoint(hostif, 2);
1080 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1081 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1082 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1083 return -ENXIO;
1084 }
1085 if (endpoint->out_cables > 0x0001) {
1086 epd = get_endpoint(hostif, 4);
1087 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1088 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1089 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1090 return -ENXIO;
1091 }
1092 }
1093
1094 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1095 ep_info.out_cables = endpoint->out_cables & 0x5555;
1096 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1097 if (err < 0)
1098 return err;
1099
1100 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1101 ep_info.in_cables = endpoint->in_cables;
1102 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1103 if (err < 0)
1104 return err;
1105 umidi->endpoints[0].in->urb->complete = snd_usb_complete_callback(snd_usbmidi_in_midiman_complete);
1106
1107 if (endpoint->out_cables > 0x0001) {
1108 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1109 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1110 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1111 if (err < 0)
1112 return err;
1113 }
1114
1115 for (cable = 0; cable < 0x10; ++cable) {
1116 if (endpoint->out_cables & (1 << cable))
1117 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1118 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1119 if (endpoint->in_cables & (1 << cable))
1120 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1121 &umidi->endpoints[0].in->ports[cable].substream);
1122 }
1123 return 0;
1124 }
1125
1126 static int snd_usbmidi_create_rawmidi(snd_usb_midi_t* umidi,
1127 int out_ports, int in_ports)
1128 {
1129 snd_rawmidi_t* rmidi;
1130 int err;
1131
1132 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1133 umidi->chip->next_midi_device++,
1134 out_ports, in_ports, &rmidi);
1135 if (err < 0)
1136 return err;
1137 strcpy(rmidi->name, umidi->chip->card->shortname);
1138 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1139 SNDRV_RAWMIDI_INFO_INPUT |
1140 SNDRV_RAWMIDI_INFO_DUPLEX;
1141 rmidi->private_data = umidi;
1142 rmidi->private_free = snd_usbmidi_rawmidi_free;
1143 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1144 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1145
1146 umidi->rmidi = rmidi;
1147 return 0;
1148 }
1149
1150 /*
1151 * Temporarily stop input.
1152 */
1153 void snd_usbmidi_input_stop(struct list_head* p)
1154 {
1155 snd_usb_midi_t* umidi;
1156 int i;
1157
1158 umidi = list_entry(p, snd_usb_midi_t, list);
1159 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1160 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
1161 if (ep->in)
1162 usb_kill_urb(ep->in->urb);
1163 }
1164 }
1165
1166 static void snd_usbmidi_input_start_ep(snd_usb_midi_in_endpoint_t* ep)
1167 {
1168 if (ep) {
1169 struct urb* urb = ep->urb;
1170 urb->dev = ep->umidi->chip->dev;
1171 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1172 }
1173 }
1174
1175 /*
1176 * Resume input after a call to snd_usbmidi_input_stop().
1177 */
1178 void snd_usbmidi_input_start(struct list_head* p)
1179 {
1180 snd_usb_midi_t* umidi;
1181 int i;
1182
1183 umidi = list_entry(p, snd_usb_midi_t, list);
1184 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1185 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1186 }
1187
1188 /*
1189 * Creates and registers everything needed for a MIDI streaming interface.
1190 */
1191 int snd_usb_create_midi_interface(snd_usb_audio_t* chip,
1192 struct usb_interface* iface,
1193 const snd_usb_audio_quirk_t* quirk)
1194 {
1195 snd_usb_midi_t* umidi;
1196 snd_usb_midi_endpoint_info_t endpoints[MIDI_MAX_ENDPOINTS];
1197 int out_ports, in_ports;
1198 int i, err;
1199
1200 umidi = kcalloc(1, sizeof(*umidi), GFP_KERNEL);
1201 if (!umidi)
1202 return -ENOMEM;
1203 umidi->chip = chip;
1204 umidi->iface = iface;
1205 umidi->quirk = quirk;
1206
1207 /* detect the endpoint(s) to use */
1208 memset(endpoints, 0, sizeof(endpoints));
1209 if (!quirk) {
1210 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1211 } else {
1212 switch (quirk->type) {
1213 case QUIRK_MIDI_FIXED_ENDPOINT:
1214 memcpy(&endpoints[0], quirk->data,
1215 sizeof(snd_usb_midi_endpoint_info_t));
1216 err = snd_usbmidi_detect_endpoint(umidi, &endpoints[0]);
1217 break;
1218 case QUIRK_MIDI_YAMAHA:
1219 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1220 break;
1221 case QUIRK_MIDI_MIDIMAN:
1222 memcpy(&endpoints[0], quirk->data,
1223 sizeof(snd_usb_midi_endpoint_info_t));
1224 err = 0;
1225 break;
1226 default:
1227 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1228 err = -ENXIO;
1229 break;
1230 }
1231 }
1232 if (err < 0) {
1233 kfree(umidi);
1234 return err;
1235 }
1236
1237 /* create rawmidi device */
1238 out_ports = 0;
1239 in_ports = 0;
1240 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1241 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1242 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1243 }
1244 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1245 if (err < 0) {
1246 kfree(umidi);
1247 return err;
1248 }
1249
1250 /* create endpoint/port structures */
1251 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1252 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1253 else
1254 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1255 if (err < 0) {
1256 snd_usbmidi_free(umidi);
1257 return err;
1258 }
1259
1260 list_add(&umidi->list, &umidi->chip->midi_list);
1261
1262 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1263 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1264 return 0;
1265 }
1266
1267 EXPORT_SYMBOL(snd_usb_create_midi_interface);
1268 EXPORT_SYMBOL(snd_usbmidi_input_stop);
1269 EXPORT_SYMBOL(snd_usbmidi_input_start);
1270 EXPORT_SYMBOL(snd_usbmidi_disconnect);
1271
|
This page was automatically generated by the
LXR engine.
|