PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / usb / gadget / f_midi.c
blob36d4bb23087f4743a9309783ab80b7d3c4d31d18
1 /*
2 * f_midi.c -- USB MIDI class function driver
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
8 * Rewritten for the composite framework
9 * Copyright (C) 2011 Daniel Mack <zonque@gmail.com>
11 * Based on drivers/usb/gadget/f_audio.c,
12 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
13 * Copyright (C) 2008 Analog Devices, Inc
15 * and drivers/usb/gadget/midi.c,
16 * Copyright (C) 2006 Thumtronics Pty Ltd.
17 * Ben Williamson <ben.williamson@greyinnovation.com>
19 * Licensed under the GPL-2 or later.
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/device.h>
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/rawmidi.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <linux/usb/audio.h>
33 #include <linux/usb/midi.h>
35 #include "u_f.h"
37 MODULE_AUTHOR("Ben Williamson");
38 MODULE_LICENSE("GPL v2");
40 static const char f_midi_shortname[] = "f_midi";
41 static const char f_midi_longname[] = "MIDI Gadget";
44 * We can only handle 16 cables on one single endpoint, as cable numbers are
45 * stored in 4-bit fields. And as the interface currently only holds one
46 * single endpoint, this is the maximum number of ports we can allow.
48 #define MAX_PORTS 16
51 * This is a gadget, and the IN/OUT naming is from the host's perspective.
52 * USB -> OUT endpoint -> rawmidi
53 * USB <- IN endpoint <- rawmidi
55 struct gmidi_in_port {
56 struct f_midi *midi;
57 int active;
58 uint8_t cable;
59 uint8_t state;
60 #define STATE_UNKNOWN 0
61 #define STATE_1PARAM 1
62 #define STATE_2PARAM_1 2
63 #define STATE_2PARAM_2 3
64 #define STATE_SYSEX_0 4
65 #define STATE_SYSEX_1 5
66 #define STATE_SYSEX_2 6
67 uint8_t data[2];
70 struct f_midi {
71 struct usb_function func;
72 struct usb_gadget *gadget;
73 struct usb_ep *in_ep, *out_ep;
74 struct snd_card *card;
75 struct snd_rawmidi *rmidi;
77 struct snd_rawmidi_substream *in_substream[MAX_PORTS];
78 struct snd_rawmidi_substream *out_substream[MAX_PORTS];
79 struct gmidi_in_port *in_port[MAX_PORTS];
81 unsigned long out_triggered;
82 struct tasklet_struct tasklet;
83 unsigned int in_ports;
84 unsigned int out_ports;
85 int index;
86 char *id;
87 unsigned int buflen, qlen;
90 static inline struct f_midi *func_to_midi(struct usb_function *f)
92 return container_of(f, struct f_midi, func);
95 static void f_midi_transmit(struct f_midi *midi, struct usb_request *req);
97 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
98 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
99 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
101 /* B.3.1 Standard AC Interface Descriptor */
102 static struct usb_interface_descriptor ac_interface_desc __initdata = {
103 .bLength = USB_DT_INTERFACE_SIZE,
104 .bDescriptorType = USB_DT_INTERFACE,
105 /* .bInterfaceNumber = DYNAMIC */
106 /* .bNumEndpoints = DYNAMIC */
107 .bInterfaceClass = USB_CLASS_AUDIO,
108 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
109 /* .iInterface = DYNAMIC */
112 /* B.3.2 Class-Specific AC Interface Descriptor */
113 static struct uac1_ac_header_descriptor_1 ac_header_desc __initdata = {
114 .bLength = UAC_DT_AC_HEADER_SIZE(1),
115 .bDescriptorType = USB_DT_CS_INTERFACE,
116 .bDescriptorSubtype = USB_MS_HEADER,
117 .bcdADC = cpu_to_le16(0x0100),
118 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
119 .bInCollection = 1,
120 /* .baInterfaceNr = DYNAMIC */
123 /* B.4.1 Standard MS Interface Descriptor */
124 static struct usb_interface_descriptor ms_interface_desc __initdata = {
125 .bLength = USB_DT_INTERFACE_SIZE,
126 .bDescriptorType = USB_DT_INTERFACE,
127 /* .bInterfaceNumber = DYNAMIC */
128 .bNumEndpoints = 2,
129 .bInterfaceClass = USB_CLASS_AUDIO,
130 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
131 /* .iInterface = DYNAMIC */
134 /* B.4.2 Class-Specific MS Interface Descriptor */
135 static struct usb_ms_header_descriptor ms_header_desc __initdata = {
136 .bLength = USB_DT_MS_HEADER_SIZE,
137 .bDescriptorType = USB_DT_CS_INTERFACE,
138 .bDescriptorSubtype = USB_MS_HEADER,
139 .bcdMSC = cpu_to_le16(0x0100),
140 /* .wTotalLength = DYNAMIC */
143 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
144 static struct usb_endpoint_descriptor bulk_out_desc = {
145 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
146 .bDescriptorType = USB_DT_ENDPOINT,
147 .bEndpointAddress = USB_DIR_OUT,
148 .bmAttributes = USB_ENDPOINT_XFER_BULK,
151 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
152 static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
153 /* .bLength = DYNAMIC */
154 .bDescriptorType = USB_DT_CS_ENDPOINT,
155 .bDescriptorSubtype = USB_MS_GENERAL,
156 /* .bNumEmbMIDIJack = DYNAMIC */
157 /* .baAssocJackID = DYNAMIC */
160 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
161 static struct usb_endpoint_descriptor bulk_in_desc = {
162 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
163 .bDescriptorType = USB_DT_ENDPOINT,
164 .bEndpointAddress = USB_DIR_IN,
165 .bmAttributes = USB_ENDPOINT_XFER_BULK,
168 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
169 static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
170 /* .bLength = DYNAMIC */
171 .bDescriptorType = USB_DT_CS_ENDPOINT,
172 .bDescriptorSubtype = USB_MS_GENERAL,
173 /* .bNumEmbMIDIJack = DYNAMIC */
174 /* .baAssocJackID = DYNAMIC */
177 /* string IDs are assigned dynamically */
179 #define STRING_FUNC_IDX 0
181 static struct usb_string midi_string_defs[] = {
182 [STRING_FUNC_IDX].s = "MIDI function",
183 { } /* end of list */
186 static struct usb_gadget_strings midi_stringtab = {
187 .language = 0x0409, /* en-us */
188 .strings = midi_string_defs,
191 static struct usb_gadget_strings *midi_strings[] = {
192 &midi_stringtab,
193 NULL,
196 static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
197 unsigned length)
199 return alloc_ep_req(ep, length, length);
202 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
204 kfree(req->buf);
205 usb_ep_free_request(ep, req);
208 static const uint8_t f_midi_cin_length[] = {
209 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
213 * Receives a chunk of MIDI data.
215 static void f_midi_read_data(struct usb_ep *ep, int cable,
216 uint8_t *data, int length)
218 struct f_midi *midi = ep->driver_data;
219 struct snd_rawmidi_substream *substream = midi->out_substream[cable];
221 if (!substream)
222 /* Nobody is listening - throw it on the floor. */
223 return;
225 if (!test_bit(cable, &midi->out_triggered))
226 return;
228 snd_rawmidi_receive(substream, data, length);
231 static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
233 unsigned int i;
234 u8 *buf = req->buf;
236 for (i = 0; i + 3 < req->actual; i += 4)
237 if (buf[i] != 0) {
238 int cable = buf[i] >> 4;
239 int length = f_midi_cin_length[buf[i] & 0x0f];
240 f_midi_read_data(ep, cable, &buf[i + 1], length);
244 static void
245 f_midi_complete(struct usb_ep *ep, struct usb_request *req)
247 struct f_midi *midi = ep->driver_data;
248 struct usb_composite_dev *cdev = midi->func.config->cdev;
249 int status = req->status;
251 switch (status) {
252 case 0: /* normal completion */
253 if (ep == midi->out_ep) {
254 /* We received stuff. req is queued again, below */
255 f_midi_handle_out_data(ep, req);
256 } else if (ep == midi->in_ep) {
257 /* Our transmit completed. See if there's more to go.
258 * f_midi_transmit eats req, don't queue it again. */
259 f_midi_transmit(midi, req);
260 return;
262 break;
264 /* this endpoint is normally active while we're configured */
265 case -ECONNABORTED: /* hardware forced ep reset */
266 case -ECONNRESET: /* request dequeued */
267 case -ESHUTDOWN: /* disconnect from host */
268 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
269 req->actual, req->length);
270 if (ep == midi->out_ep)
271 f_midi_handle_out_data(ep, req);
273 free_ep_req(ep, req);
274 return;
276 case -EOVERFLOW: /* buffer overrun on read means that
277 * we didn't provide a big enough buffer.
279 default:
280 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
281 status, req->actual, req->length);
282 break;
283 case -EREMOTEIO: /* short read */
284 break;
287 status = usb_ep_queue(ep, req, GFP_ATOMIC);
288 if (status) {
289 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
290 ep->name, req->length, status);
291 usb_ep_set_halt(ep);
292 /* FIXME recover later ... somehow */
296 static int f_midi_start_ep(struct f_midi *midi,
297 struct usb_function *f,
298 struct usb_ep *ep)
300 int err;
301 struct usb_composite_dev *cdev = f->config->cdev;
303 if (ep->driver_data)
304 usb_ep_disable(ep);
306 err = config_ep_by_speed(midi->gadget, f, ep);
307 if (err) {
308 ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
309 return err;
312 err = usb_ep_enable(ep);
313 if (err) {
314 ERROR(cdev, "can't start %s: %d\n", ep->name, err);
315 return err;
318 ep->driver_data = midi;
320 return 0;
323 static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
325 struct f_midi *midi = func_to_midi(f);
326 struct usb_composite_dev *cdev = f->config->cdev;
327 unsigned i;
328 int err;
330 err = f_midi_start_ep(midi, f, midi->in_ep);
331 if (err)
332 return err;
334 err = f_midi_start_ep(midi, f, midi->out_ep);
335 if (err)
336 return err;
338 if (midi->out_ep->driver_data)
339 usb_ep_disable(midi->out_ep);
341 err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
342 if (err) {
343 ERROR(cdev, "can't configure %s: %d\n",
344 midi->out_ep->name, err);
345 return err;
348 err = usb_ep_enable(midi->out_ep);
349 if (err) {
350 ERROR(cdev, "can't start %s: %d\n",
351 midi->out_ep->name, err);
352 return err;
355 midi->out_ep->driver_data = midi;
357 /* allocate a bunch of read buffers and queue them all at once. */
358 for (i = 0; i < midi->qlen && err == 0; i++) {
359 struct usb_request *req =
360 midi_alloc_ep_req(midi->out_ep, midi->buflen);
361 if (req == NULL)
362 return -ENOMEM;
364 req->complete = f_midi_complete;
365 err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
366 if (err) {
367 ERROR(midi, "%s queue req: %d\n",
368 midi->out_ep->name, err);
372 return 0;
375 static void f_midi_disable(struct usb_function *f)
377 struct f_midi *midi = func_to_midi(f);
378 struct usb_composite_dev *cdev = f->config->cdev;
380 DBG(cdev, "disable\n");
383 * just disable endpoints, forcing completion of pending i/o.
384 * all our completion handlers free their requests in this case.
386 usb_ep_disable(midi->in_ep);
387 usb_ep_disable(midi->out_ep);
390 static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
392 struct usb_composite_dev *cdev = f->config->cdev;
393 struct f_midi *midi = func_to_midi(f);
394 struct snd_card *card;
396 DBG(cdev, "unbind\n");
398 /* just to be sure */
399 f_midi_disable(f);
401 card = midi->card;
402 midi->card = NULL;
403 if (card)
404 snd_card_free(card);
406 kfree(midi->id);
407 midi->id = NULL;
409 usb_free_all_descriptors(f);
410 kfree(midi);
413 static int f_midi_snd_free(struct snd_device *device)
415 return 0;
418 static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
419 uint8_t p1, uint8_t p2, uint8_t p3)
421 unsigned length = req->length;
422 u8 *buf = (u8 *)req->buf + length;
424 buf[0] = p0;
425 buf[1] = p1;
426 buf[2] = p2;
427 buf[3] = p3;
428 req->length = length + 4;
432 * Converts MIDI commands to USB MIDI packets.
434 static void f_midi_transmit_byte(struct usb_request *req,
435 struct gmidi_in_port *port, uint8_t b)
437 uint8_t p0 = port->cable << 4;
439 if (b >= 0xf8) {
440 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
441 } else if (b >= 0xf0) {
442 switch (b) {
443 case 0xf0:
444 port->data[0] = b;
445 port->state = STATE_SYSEX_1;
446 break;
447 case 0xf1:
448 case 0xf3:
449 port->data[0] = b;
450 port->state = STATE_1PARAM;
451 break;
452 case 0xf2:
453 port->data[0] = b;
454 port->state = STATE_2PARAM_1;
455 break;
456 case 0xf4:
457 case 0xf5:
458 port->state = STATE_UNKNOWN;
459 break;
460 case 0xf6:
461 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
462 port->state = STATE_UNKNOWN;
463 break;
464 case 0xf7:
465 switch (port->state) {
466 case STATE_SYSEX_0:
467 f_midi_transmit_packet(req,
468 p0 | 0x05, 0xf7, 0, 0);
469 break;
470 case STATE_SYSEX_1:
471 f_midi_transmit_packet(req,
472 p0 | 0x06, port->data[0], 0xf7, 0);
473 break;
474 case STATE_SYSEX_2:
475 f_midi_transmit_packet(req,
476 p0 | 0x07, port->data[0],
477 port->data[1], 0xf7);
478 break;
480 port->state = STATE_UNKNOWN;
481 break;
483 } else if (b >= 0x80) {
484 port->data[0] = b;
485 if (b >= 0xc0 && b <= 0xdf)
486 port->state = STATE_1PARAM;
487 else
488 port->state = STATE_2PARAM_1;
489 } else { /* b < 0x80 */
490 switch (port->state) {
491 case STATE_1PARAM:
492 if (port->data[0] < 0xf0) {
493 p0 |= port->data[0] >> 4;
494 } else {
495 p0 |= 0x02;
496 port->state = STATE_UNKNOWN;
498 f_midi_transmit_packet(req, p0, port->data[0], b, 0);
499 break;
500 case STATE_2PARAM_1:
501 port->data[1] = b;
502 port->state = STATE_2PARAM_2;
503 break;
504 case STATE_2PARAM_2:
505 if (port->data[0] < 0xf0) {
506 p0 |= port->data[0] >> 4;
507 port->state = STATE_2PARAM_1;
508 } else {
509 p0 |= 0x03;
510 port->state = STATE_UNKNOWN;
512 f_midi_transmit_packet(req,
513 p0, port->data[0], port->data[1], b);
514 break;
515 case STATE_SYSEX_0:
516 port->data[0] = b;
517 port->state = STATE_SYSEX_1;
518 break;
519 case STATE_SYSEX_1:
520 port->data[1] = b;
521 port->state = STATE_SYSEX_2;
522 break;
523 case STATE_SYSEX_2:
524 f_midi_transmit_packet(req,
525 p0 | 0x04, port->data[0], port->data[1], b);
526 port->state = STATE_SYSEX_0;
527 break;
532 static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
534 struct usb_ep *ep = midi->in_ep;
535 int i;
537 if (!ep)
538 return;
540 if (!req)
541 req = midi_alloc_ep_req(ep, midi->buflen);
543 if (!req) {
544 ERROR(midi, "gmidi_transmit: alloc_ep_request failed\n");
545 return;
547 req->length = 0;
548 req->complete = f_midi_complete;
550 for (i = 0; i < MAX_PORTS; i++) {
551 struct gmidi_in_port *port = midi->in_port[i];
552 struct snd_rawmidi_substream *substream = midi->in_substream[i];
554 if (!port || !port->active || !substream)
555 continue;
557 while (req->length + 3 < midi->buflen) {
558 uint8_t b;
559 if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
560 port->active = 0;
561 break;
563 f_midi_transmit_byte(req, port, b);
567 if (req->length > 0)
568 usb_ep_queue(ep, req, GFP_ATOMIC);
569 else
570 free_ep_req(ep, req);
573 static void f_midi_in_tasklet(unsigned long data)
575 struct f_midi *midi = (struct f_midi *) data;
576 f_midi_transmit(midi, NULL);
579 static int f_midi_in_open(struct snd_rawmidi_substream *substream)
581 struct f_midi *midi = substream->rmidi->private_data;
583 if (!midi->in_port[substream->number])
584 return -EINVAL;
586 VDBG(midi, "%s()\n", __func__);
587 midi->in_substream[substream->number] = substream;
588 midi->in_port[substream->number]->state = STATE_UNKNOWN;
589 return 0;
592 static int f_midi_in_close(struct snd_rawmidi_substream *substream)
594 struct f_midi *midi = substream->rmidi->private_data;
596 VDBG(midi, "%s()\n", __func__);
597 return 0;
600 static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
602 struct f_midi *midi = substream->rmidi->private_data;
604 if (!midi->in_port[substream->number])
605 return;
607 VDBG(midi, "%s() %d\n", __func__, up);
608 midi->in_port[substream->number]->active = up;
609 if (up)
610 tasklet_hi_schedule(&midi->tasklet);
613 static int f_midi_out_open(struct snd_rawmidi_substream *substream)
615 struct f_midi *midi = substream->rmidi->private_data;
617 if (substream->number >= MAX_PORTS)
618 return -EINVAL;
620 VDBG(midi, "%s()\n", __func__);
621 midi->out_substream[substream->number] = substream;
622 return 0;
625 static int f_midi_out_close(struct snd_rawmidi_substream *substream)
627 struct f_midi *midi = substream->rmidi->private_data;
629 VDBG(midi, "%s()\n", __func__);
630 return 0;
633 static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
635 struct f_midi *midi = substream->rmidi->private_data;
637 VDBG(midi, "%s()\n", __func__);
639 if (up)
640 set_bit(substream->number, &midi->out_triggered);
641 else
642 clear_bit(substream->number, &midi->out_triggered);
645 static struct snd_rawmidi_ops gmidi_in_ops = {
646 .open = f_midi_in_open,
647 .close = f_midi_in_close,
648 .trigger = f_midi_in_trigger,
651 static struct snd_rawmidi_ops gmidi_out_ops = {
652 .open = f_midi_out_open,
653 .close = f_midi_out_close,
654 .trigger = f_midi_out_trigger
657 /* register as a sound "card" */
658 static int f_midi_register_card(struct f_midi *midi)
660 struct snd_card *card;
661 struct snd_rawmidi *rmidi;
662 int err;
663 static struct snd_device_ops ops = {
664 .dev_free = f_midi_snd_free,
667 err = snd_card_create(midi->index, midi->id, THIS_MODULE, 0, &card);
668 if (err < 0) {
669 ERROR(midi, "snd_card_create() failed\n");
670 goto fail;
672 midi->card = card;
674 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
675 if (err < 0) {
676 ERROR(midi, "snd_device_new() failed: error %d\n", err);
677 goto fail;
680 strcpy(card->driver, f_midi_longname);
681 strcpy(card->longname, f_midi_longname);
682 strcpy(card->shortname, f_midi_shortname);
684 /* Set up rawmidi */
685 snd_component_add(card, "MIDI");
686 err = snd_rawmidi_new(card, card->longname, 0,
687 midi->out_ports, midi->in_ports, &rmidi);
688 if (err < 0) {
689 ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
690 goto fail;
692 midi->rmidi = rmidi;
693 strcpy(rmidi->name, card->shortname);
694 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
695 SNDRV_RAWMIDI_INFO_INPUT |
696 SNDRV_RAWMIDI_INFO_DUPLEX;
697 rmidi->private_data = midi;
700 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
701 * It's an upside-down world being a gadget.
703 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
704 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
706 snd_card_set_dev(card, &midi->gadget->dev);
708 /* register it - we're ready to go */
709 err = snd_card_register(card);
710 if (err < 0) {
711 ERROR(midi, "snd_card_register() failed\n");
712 goto fail;
715 VDBG(midi, "%s() finished ok\n", __func__);
716 return 0;
718 fail:
719 if (midi->card) {
720 snd_card_free(midi->card);
721 midi->card = NULL;
723 return err;
726 /* MIDI function driver setup/binding */
728 static int __init
729 f_midi_bind(struct usb_configuration *c, struct usb_function *f)
731 struct usb_descriptor_header **midi_function;
732 struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
733 struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
734 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
735 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
736 struct usb_composite_dev *cdev = c->cdev;
737 struct f_midi *midi = func_to_midi(f);
738 int status, n, jack = 1, i = 0;
740 /* maybe allocate device-global string ID */
741 if (midi_string_defs[0].id == 0) {
742 status = usb_string_id(c->cdev);
743 if (status < 0)
744 goto fail;
745 midi_string_defs[0].id = status;
748 /* We have two interfaces, AudioControl and MIDIStreaming */
749 status = usb_interface_id(c, f);
750 if (status < 0)
751 goto fail;
752 ac_interface_desc.bInterfaceNumber = status;
754 status = usb_interface_id(c, f);
755 if (status < 0)
756 goto fail;
757 ms_interface_desc.bInterfaceNumber = status;
758 ac_header_desc.baInterfaceNr[0] = status;
760 status = -ENODEV;
762 /* allocate instance-specific endpoints */
763 midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
764 if (!midi->in_ep)
765 goto fail;
766 midi->in_ep->driver_data = cdev; /* claim */
768 midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
769 if (!midi->out_ep)
770 goto fail;
771 midi->out_ep->driver_data = cdev; /* claim */
773 /* allocate temporary function list */
774 midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
775 GFP_KERNEL);
776 if (!midi_function) {
777 status = -ENOMEM;
778 goto fail;
782 * construct the function's descriptor set. As the number of
783 * input and output MIDI ports is configurable, we have to do
784 * it that way.
787 /* add the headers - these are always the same */
788 midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
789 midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
790 midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
792 /* calculate the header's wTotalLength */
793 n = USB_DT_MS_HEADER_SIZE
794 + (midi->in_ports + midi->out_ports) *
795 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
796 ms_header_desc.wTotalLength = cpu_to_le16(n);
798 midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
800 /* configure the external IN jacks, each linked to an embedded OUT jack */
801 for (n = 0; n < midi->in_ports; n++) {
802 struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
803 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
805 in_ext->bLength = USB_DT_MIDI_IN_SIZE;
806 in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
807 in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
808 in_ext->bJackType = USB_MS_EXTERNAL;
809 in_ext->bJackID = jack++;
810 in_ext->iJack = 0;
811 midi_function[i++] = (struct usb_descriptor_header *) in_ext;
813 out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
814 out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
815 out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
816 out_emb->bJackType = USB_MS_EMBEDDED;
817 out_emb->bJackID = jack++;
818 out_emb->bNrInputPins = 1;
819 out_emb->pins[0].baSourcePin = 1;
820 out_emb->pins[0].baSourceID = in_ext->bJackID;
821 out_emb->iJack = 0;
822 midi_function[i++] = (struct usb_descriptor_header *) out_emb;
824 /* link it to the endpoint */
825 ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
828 /* configure the external OUT jacks, each linked to an embedded IN jack */
829 for (n = 0; n < midi->out_ports; n++) {
830 struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
831 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
833 in_emb->bLength = USB_DT_MIDI_IN_SIZE;
834 in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
835 in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
836 in_emb->bJackType = USB_MS_EMBEDDED;
837 in_emb->bJackID = jack++;
838 in_emb->iJack = 0;
839 midi_function[i++] = (struct usb_descriptor_header *) in_emb;
841 out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
842 out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
843 out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
844 out_ext->bJackType = USB_MS_EXTERNAL;
845 out_ext->bJackID = jack++;
846 out_ext->bNrInputPins = 1;
847 out_ext->iJack = 0;
848 out_ext->pins[0].baSourceID = in_emb->bJackID;
849 out_ext->pins[0].baSourcePin = 1;
850 midi_function[i++] = (struct usb_descriptor_header *) out_ext;
852 /* link it to the endpoint */
853 ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
856 /* configure the endpoint descriptors ... */
857 ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
858 ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
860 ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
861 ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
863 /* ... and add them to the list */
864 midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
865 midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
866 midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
867 midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
868 midi_function[i++] = NULL;
871 * support all relevant hardware speeds... we expect that when
872 * hardware is dual speed, all bulk-capable endpoints work at
873 * both speeds
875 /* copy descriptors, and track endpoint copies */
876 f->fs_descriptors = usb_copy_descriptors(midi_function);
877 if (!f->fs_descriptors)
878 goto fail_f_midi;
880 if (gadget_is_dualspeed(c->cdev->gadget)) {
881 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
882 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
883 f->hs_descriptors = usb_copy_descriptors(midi_function);
884 if (!f->hs_descriptors)
885 goto fail_f_midi;
888 kfree(midi_function);
890 return 0;
892 fail_f_midi:
893 kfree(midi_function);
894 usb_free_descriptors(f->hs_descriptors);
895 fail:
896 /* we might as well release our claims on endpoints */
897 if (midi->out_ep)
898 midi->out_ep->driver_data = NULL;
899 if (midi->in_ep)
900 midi->in_ep->driver_data = NULL;
902 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
904 return status;
908 * f_midi_bind_config - add USB MIDI function to a configuration
909 * @c: the configuration to supcard the USB audio function
910 * @index: the soundcard index to use for the ALSA device creation
911 * @id: the soundcard id to use for the ALSA device creation
912 * @buflen: the buffer length to use
913 * @qlen the number of read requests to pre-allocate
914 * Context: single threaded during gadget setup
916 * Returns zero on success, else negative errno.
918 int __init f_midi_bind_config(struct usb_configuration *c,
919 int index, char *id,
920 unsigned int in_ports,
921 unsigned int out_ports,
922 unsigned int buflen,
923 unsigned int qlen)
925 struct f_midi *midi;
926 int status, i;
928 /* sanity check */
929 if (in_ports > MAX_PORTS || out_ports > MAX_PORTS)
930 return -EINVAL;
932 /* allocate and initialize one new instance */
933 midi = kzalloc(sizeof *midi, GFP_KERNEL);
934 if (!midi) {
935 status = -ENOMEM;
936 goto fail;
939 for (i = 0; i < in_ports; i++) {
940 struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
941 if (!port) {
942 status = -ENOMEM;
943 goto setup_fail;
946 port->midi = midi;
947 port->active = 0;
948 port->cable = i;
949 midi->in_port[i] = port;
952 midi->gadget = c->cdev->gadget;
953 tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
955 /* set up ALSA midi devices */
956 midi->in_ports = in_ports;
957 midi->out_ports = out_ports;
958 status = f_midi_register_card(midi);
959 if (status < 0)
960 goto setup_fail;
962 midi->func.name = "gmidi function";
963 midi->func.strings = midi_strings;
964 midi->func.bind = f_midi_bind;
965 midi->func.unbind = f_midi_unbind;
966 midi->func.set_alt = f_midi_set_alt;
967 midi->func.disable = f_midi_disable;
969 midi->id = kstrdup(id, GFP_KERNEL);
970 midi->index = index;
971 midi->buflen = buflen;
972 midi->qlen = qlen;
974 status = usb_add_function(c, &midi->func);
975 if (status)
976 goto setup_fail;
978 return 0;
980 setup_fail:
981 for (--i; i >= 0; i--)
982 kfree(midi->in_port[i]);
983 kfree(midi);
984 fail:
985 return status;