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/module.h>
24 #include <linux/slab.h>
25 #include <linux/device.h>
26 #include <linux/kfifo.h>
28 #include <sound/core.h>
29 #include <sound/initval.h>
30 #include <sound/rawmidi.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/audio.h>
35 #include <linux/usb/midi.h>
40 MODULE_AUTHOR("Ben Williamson");
41 MODULE_LICENSE("GPL v2");
43 static const char f_midi_shortname[] = "f_midi";
44 static const char f_midi_longname[] = "MIDI Gadget";
47 * We can only handle 16 cables on one single endpoint, as cable numbers are
48 * stored in 4-bit fields. And as the interface currently only holds one
49 * single endpoint, this is the maximum number of ports we can allow.
54 * This is a gadget, and the IN/OUT naming is from the host's perspective.
55 * USB -> OUT endpoint -> rawmidi
56 * USB <- IN endpoint <- rawmidi
58 struct gmidi_in_port {
59 struct snd_rawmidi_substream *substream;
63 #define STATE_UNKNOWN 0
64 #define STATE_1PARAM 1
65 #define STATE_2PARAM_1 2
66 #define STATE_2PARAM_2 3
67 #define STATE_SYSEX_0 4
68 #define STATE_SYSEX_1 5
69 #define STATE_SYSEX_2 6
74 struct usb_function func;
75 struct usb_gadget *gadget;
76 struct usb_ep *in_ep, *out_ep;
77 struct snd_card *card;
78 struct snd_rawmidi *rmidi;
81 struct snd_rawmidi_substream *out_substream[MAX_PORTS];
83 unsigned long out_triggered;
84 struct tasklet_struct tasklet;
85 unsigned int in_ports;
86 unsigned int out_ports;
89 unsigned int buflen, qlen;
90 /* This fifo is used as a buffer ring for pre-allocated IN usb_requests */
91 DECLARE_KFIFO_PTR(in_req_fifo, struct usb_request *);
92 unsigned int in_last_port;
94 struct gmidi_in_port in_ports_array[/* in_ports */];
97 static inline struct f_midi *func_to_midi(struct usb_function *f)
99 return container_of(f, struct f_midi, func);
102 static void f_midi_transmit(struct f_midi *midi);
104 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
105 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
106 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
108 /* B.3.1 Standard AC Interface Descriptor */
109 static struct usb_interface_descriptor ac_interface_desc = {
110 .bLength = USB_DT_INTERFACE_SIZE,
111 .bDescriptorType = USB_DT_INTERFACE,
112 /* .bInterfaceNumber = DYNAMIC */
113 /* .bNumEndpoints = DYNAMIC */
114 .bInterfaceClass = USB_CLASS_AUDIO,
115 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
116 /* .iInterface = DYNAMIC */
119 /* B.3.2 Class-Specific AC Interface Descriptor */
120 static struct uac1_ac_header_descriptor_1 ac_header_desc = {
121 .bLength = UAC_DT_AC_HEADER_SIZE(1),
122 .bDescriptorType = USB_DT_CS_INTERFACE,
123 .bDescriptorSubtype = USB_MS_HEADER,
124 .bcdADC = cpu_to_le16(0x0100),
125 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
127 /* .baInterfaceNr = DYNAMIC */
130 /* B.4.1 Standard MS Interface Descriptor */
131 static struct usb_interface_descriptor ms_interface_desc = {
132 .bLength = USB_DT_INTERFACE_SIZE,
133 .bDescriptorType = USB_DT_INTERFACE,
134 /* .bInterfaceNumber = DYNAMIC */
136 .bInterfaceClass = USB_CLASS_AUDIO,
137 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
138 /* .iInterface = DYNAMIC */
141 /* B.4.2 Class-Specific MS Interface Descriptor */
142 static struct usb_ms_header_descriptor ms_header_desc = {
143 .bLength = USB_DT_MS_HEADER_SIZE,
144 .bDescriptorType = USB_DT_CS_INTERFACE,
145 .bDescriptorSubtype = USB_MS_HEADER,
146 .bcdMSC = cpu_to_le16(0x0100),
147 /* .wTotalLength = DYNAMIC */
150 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
151 static struct usb_endpoint_descriptor bulk_out_desc = {
152 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = USB_DIR_OUT,
155 .bmAttributes = USB_ENDPOINT_XFER_BULK,
158 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
159 static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
160 /* .bLength = DYNAMIC */
161 .bDescriptorType = USB_DT_CS_ENDPOINT,
162 .bDescriptorSubtype = USB_MS_GENERAL,
163 /* .bNumEmbMIDIJack = DYNAMIC */
164 /* .baAssocJackID = DYNAMIC */
167 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
168 static struct usb_endpoint_descriptor bulk_in_desc = {
169 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
170 .bDescriptorType = USB_DT_ENDPOINT,
171 .bEndpointAddress = USB_DIR_IN,
172 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
176 static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
177 /* .bLength = DYNAMIC */
178 .bDescriptorType = USB_DT_CS_ENDPOINT,
179 .bDescriptorSubtype = USB_MS_GENERAL,
180 /* .bNumEmbMIDIJack = DYNAMIC */
181 /* .baAssocJackID = DYNAMIC */
184 /* string IDs are assigned dynamically */
186 #define STRING_FUNC_IDX 0
188 static struct usb_string midi_string_defs[] = {
189 [STRING_FUNC_IDX].s = "MIDI function",
190 { } /* end of list */
193 static struct usb_gadget_strings midi_stringtab = {
194 .language = 0x0409, /* en-us */
195 .strings = midi_string_defs,
198 static struct usb_gadget_strings *midi_strings[] = {
203 static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
206 return alloc_ep_req(ep, length, length);
209 static const uint8_t f_midi_cin_length[] = {
210 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
214 * Receives a chunk of MIDI data.
216 static void f_midi_read_data(struct usb_ep *ep, int cable,
217 uint8_t *data, int length)
219 struct f_midi *midi = ep->driver_data;
220 struct snd_rawmidi_substream *substream = midi->out_substream[cable];
223 /* Nobody is listening - throw it on the floor. */
226 if (!test_bit(cable, &midi->out_triggered))
229 snd_rawmidi_receive(substream, data, length);
232 static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
237 for (i = 0; i + 3 < req->actual; i += 4)
239 int cable = buf[i] >> 4;
240 int length = f_midi_cin_length[buf[i] & 0x0f];
241 f_midi_read_data(ep, cable, &buf[i + 1], length);
246 f_midi_complete(struct usb_ep *ep, struct usb_request *req)
248 struct f_midi *midi = ep->driver_data;
249 struct usb_composite_dev *cdev = midi->func.config->cdev;
250 int status = req->status;
253 case 0: /* normal completion */
254 if (ep == midi->out_ep) {
255 /* We received stuff. req is queued again, below */
256 f_midi_handle_out_data(ep, req);
257 } else if (ep == midi->in_ep) {
258 /* Our transmit completed. See if there's more to go.
259 * f_midi_transmit eats req, don't queue it again. */
261 f_midi_transmit(midi);
266 /* this endpoint is normally active while we're configured */
267 case -ECONNABORTED: /* hardware forced ep reset */
268 case -ECONNRESET: /* request dequeued */
269 case -ESHUTDOWN: /* disconnect from host */
270 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
271 req->actual, req->length);
272 if (ep == midi->out_ep) {
273 f_midi_handle_out_data(ep, req);
274 /* We don't need to free IN requests because it's handled
275 * by the midi->in_req_fifo. */
276 free_ep_req(ep, req);
280 case -EOVERFLOW: /* buffer overrun on read means that
281 * we didn't provide a big enough buffer.
284 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
285 status, req->actual, req->length);
287 case -EREMOTEIO: /* short read */
291 status = usb_ep_queue(ep, req, GFP_ATOMIC);
293 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
294 ep->name, req->length, status);
296 /* FIXME recover later ... somehow */
300 static int f_midi_start_ep(struct f_midi *midi,
301 struct usb_function *f,
305 struct usb_composite_dev *cdev = f->config->cdev;
309 err = config_ep_by_speed(midi->gadget, f, ep);
311 ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
315 err = usb_ep_enable(ep);
317 ERROR(cdev, "can't start %s: %d\n", ep->name, err);
321 ep->driver_data = midi;
326 static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
328 struct f_midi *midi = func_to_midi(f);
332 /* we only set alt for MIDIStreaming interface */
333 if (intf != midi->ms_id)
336 err = f_midi_start_ep(midi, f, midi->in_ep);
340 err = f_midi_start_ep(midi, f, midi->out_ep);
344 /* pre-allocate write usb requests to use on f_midi_transmit. */
345 while (kfifo_avail(&midi->in_req_fifo)) {
346 struct usb_request *req =
347 midi_alloc_ep_req(midi->in_ep, midi->buflen);
353 req->complete = f_midi_complete;
355 kfifo_put(&midi->in_req_fifo, req);
358 /* allocate a bunch of read buffers and queue them all at once. */
359 for (i = 0; i < midi->qlen && err == 0; i++) {
360 struct usb_request *req =
361 midi_alloc_ep_req(midi->out_ep, midi->buflen);
365 req->complete = f_midi_complete;
366 err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
368 ERROR(midi, "%s: couldn't enqueue request: %d\n",
369 midi->out_ep->name, err);
370 free_ep_req(midi->out_ep, req);
378 static void f_midi_disable(struct usb_function *f)
380 struct f_midi *midi = func_to_midi(f);
381 struct usb_composite_dev *cdev = f->config->cdev;
382 struct usb_request *req = NULL;
384 DBG(cdev, "disable\n");
387 * just disable endpoints, forcing completion of pending i/o.
388 * all our completion handlers free their requests in this case.
390 usb_ep_disable(midi->in_ep);
391 usb_ep_disable(midi->out_ep);
393 /* release IN requests */
394 while (kfifo_get(&midi->in_req_fifo, &req))
395 free_ep_req(midi->in_ep, req);
398 static int f_midi_snd_free(struct snd_device *device)
403 static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
404 uint8_t p1, uint8_t p2, uint8_t p3)
406 unsigned length = req->length;
407 u8 *buf = (u8 *)req->buf + length;
413 req->length = length + 4;
417 * Converts MIDI commands to USB MIDI packets.
419 static void f_midi_transmit_byte(struct usb_request *req,
420 struct gmidi_in_port *port, uint8_t b)
422 uint8_t p0 = port->cable << 4;
425 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
426 } else if (b >= 0xf0) {
430 port->state = STATE_SYSEX_1;
435 port->state = STATE_1PARAM;
439 port->state = STATE_2PARAM_1;
443 port->state = STATE_UNKNOWN;
446 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
447 port->state = STATE_UNKNOWN;
450 switch (port->state) {
452 f_midi_transmit_packet(req,
453 p0 | 0x05, 0xf7, 0, 0);
456 f_midi_transmit_packet(req,
457 p0 | 0x06, port->data[0], 0xf7, 0);
460 f_midi_transmit_packet(req,
461 p0 | 0x07, port->data[0],
462 port->data[1], 0xf7);
465 port->state = STATE_UNKNOWN;
468 } else if (b >= 0x80) {
470 if (b >= 0xc0 && b <= 0xdf)
471 port->state = STATE_1PARAM;
473 port->state = STATE_2PARAM_1;
474 } else { /* b < 0x80 */
475 switch (port->state) {
477 if (port->data[0] < 0xf0) {
478 p0 |= port->data[0] >> 4;
481 port->state = STATE_UNKNOWN;
483 f_midi_transmit_packet(req, p0, port->data[0], b, 0);
487 port->state = STATE_2PARAM_2;
490 if (port->data[0] < 0xf0) {
491 p0 |= port->data[0] >> 4;
492 port->state = STATE_2PARAM_1;
495 port->state = STATE_UNKNOWN;
497 f_midi_transmit_packet(req,
498 p0, port->data[0], port->data[1], b);
502 port->state = STATE_SYSEX_1;
506 port->state = STATE_SYSEX_2;
509 f_midi_transmit_packet(req,
510 p0 | 0x04, port->data[0], port->data[1], b);
511 port->state = STATE_SYSEX_0;
517 static void f_midi_drop_out_substreams(struct f_midi *midi)
521 for (i = 0; i < midi->in_ports; i++) {
522 struct gmidi_in_port *port = midi->in_ports_array + i;
523 struct snd_rawmidi_substream *substream = port->substream;
524 if (port->active && substream)
525 snd_rawmidi_drop_output(substream);
529 static int f_midi_do_transmit(struct f_midi *midi, struct usb_ep *ep)
531 struct usb_request *req = NULL;
537 * We peek the request in order to reuse it if it fails to enqueue on
540 len = kfifo_peek(&midi->in_req_fifo, &req);
542 ERROR(midi, "%s: Couldn't get usb request\n", __func__);
547 * If buffer overrun, then we ignore this transmission.
548 * IMPORTANT: This will cause the user-space rawmidi device to block
549 * until a) usb requests have been completed or b) snd_rawmidi_write()
555 for (i = midi->in_last_port; i < midi->in_ports; ++i) {
556 struct gmidi_in_port *port = midi->in_ports_array + i;
557 struct snd_rawmidi_substream *substream = port->substream;
559 if (!port->active || !substream)
562 while (req->length + 3 < midi->buflen) {
565 if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
569 f_midi_transmit_byte(req, port, b);
572 active = !!port->active;
576 midi->in_last_port = active ? i : 0;
578 if (req->length <= 0)
581 err = usb_ep_queue(ep, req, GFP_ATOMIC);
583 ERROR(midi, "%s failed to queue req: %d\n",
584 midi->in_ep->name, err);
585 req->length = 0; /* Re-use request next time. */
587 /* Upon success, put request at the back of the queue. */
588 kfifo_skip(&midi->in_req_fifo);
589 kfifo_put(&midi->in_req_fifo, req);
596 static void f_midi_transmit(struct f_midi *midi)
598 struct usb_ep *ep = midi->in_ep;
601 /* We only care about USB requests if IN endpoint is enabled */
602 if (!ep || !ep->enabled)
606 ret = f_midi_do_transmit(midi, ep);
614 f_midi_drop_out_substreams(midi);
617 static void f_midi_in_tasklet(unsigned long data)
619 struct f_midi *midi = (struct f_midi *) data;
620 f_midi_transmit(midi);
623 static int f_midi_in_open(struct snd_rawmidi_substream *substream)
625 struct f_midi *midi = substream->rmidi->private_data;
626 struct gmidi_in_port *port;
628 if (substream->number >= midi->in_ports)
631 VDBG(midi, "%s()\n", __func__);
632 port = midi->in_ports_array + substream->number;
633 port->substream = substream;
634 port->state = STATE_UNKNOWN;
638 static int f_midi_in_close(struct snd_rawmidi_substream *substream)
640 struct f_midi *midi = substream->rmidi->private_data;
642 VDBG(midi, "%s()\n", __func__);
646 static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
648 struct f_midi *midi = substream->rmidi->private_data;
650 if (substream->number >= midi->in_ports)
653 VDBG(midi, "%s() %d\n", __func__, up);
654 midi->in_ports_array[substream->number].active = up;
656 tasklet_hi_schedule(&midi->tasklet);
659 static int f_midi_out_open(struct snd_rawmidi_substream *substream)
661 struct f_midi *midi = substream->rmidi->private_data;
663 if (substream->number >= MAX_PORTS)
666 VDBG(midi, "%s()\n", __func__);
667 midi->out_substream[substream->number] = substream;
671 static int f_midi_out_close(struct snd_rawmidi_substream *substream)
673 struct f_midi *midi = substream->rmidi->private_data;
675 VDBG(midi, "%s()\n", __func__);
679 static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
681 struct f_midi *midi = substream->rmidi->private_data;
683 VDBG(midi, "%s()\n", __func__);
686 set_bit(substream->number, &midi->out_triggered);
688 clear_bit(substream->number, &midi->out_triggered);
691 static struct snd_rawmidi_ops gmidi_in_ops = {
692 .open = f_midi_in_open,
693 .close = f_midi_in_close,
694 .trigger = f_midi_in_trigger,
697 static struct snd_rawmidi_ops gmidi_out_ops = {
698 .open = f_midi_out_open,
699 .close = f_midi_out_close,
700 .trigger = f_midi_out_trigger
703 static inline void f_midi_unregister_card(struct f_midi *midi)
706 snd_card_free(midi->card);
711 /* register as a sound "card" */
712 static int f_midi_register_card(struct f_midi *midi)
714 struct snd_card *card;
715 struct snd_rawmidi *rmidi;
717 static struct snd_device_ops ops = {
718 .dev_free = f_midi_snd_free,
721 err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
722 THIS_MODULE, 0, &card);
724 ERROR(midi, "snd_card_new() failed\n");
729 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
731 ERROR(midi, "snd_device_new() failed: error %d\n", err);
735 strcpy(card->driver, f_midi_longname);
736 strcpy(card->longname, f_midi_longname);
737 strcpy(card->shortname, f_midi_shortname);
740 snd_component_add(card, "MIDI");
741 err = snd_rawmidi_new(card, card->longname, 0,
742 midi->out_ports, midi->in_ports, &rmidi);
744 ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
748 midi->in_last_port = 0;
749 strcpy(rmidi->name, card->shortname);
750 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
751 SNDRV_RAWMIDI_INFO_INPUT |
752 SNDRV_RAWMIDI_INFO_DUPLEX;
753 rmidi->private_data = midi;
756 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
757 * It's an upside-down world being a gadget.
759 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
760 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
762 /* register it - we're ready to go */
763 err = snd_card_register(card);
765 ERROR(midi, "snd_card_register() failed\n");
769 VDBG(midi, "%s() finished ok\n", __func__);
773 f_midi_unregister_card(midi);
777 /* MIDI function driver setup/binding */
779 static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
781 struct usb_descriptor_header **midi_function;
782 struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
783 struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
784 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
785 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
786 struct usb_composite_dev *cdev = c->cdev;
787 struct f_midi *midi = func_to_midi(f);
788 struct usb_string *us;
789 int status, n, jack = 1, i = 0;
791 midi->gadget = cdev->gadget;
792 tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
793 status = f_midi_register_card(midi);
797 /* maybe allocate device-global string ID */
798 us = usb_gstrings_attach(c->cdev, midi_strings,
799 ARRAY_SIZE(midi_string_defs));
801 status = PTR_ERR(us);
804 ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id;
806 /* We have two interfaces, AudioControl and MIDIStreaming */
807 status = usb_interface_id(c, f);
810 ac_interface_desc.bInterfaceNumber = status;
812 status = usb_interface_id(c, f);
815 ms_interface_desc.bInterfaceNumber = status;
816 ac_header_desc.baInterfaceNr[0] = status;
817 midi->ms_id = status;
821 /* allocate instance-specific endpoints */
822 midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
826 midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
830 /* allocate temporary function list */
831 midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
833 if (!midi_function) {
839 * construct the function's descriptor set. As the number of
840 * input and output MIDI ports is configurable, we have to do
844 /* add the headers - these are always the same */
845 midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
846 midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
847 midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
849 /* calculate the header's wTotalLength */
850 n = USB_DT_MS_HEADER_SIZE
851 + (midi->in_ports + midi->out_ports) *
852 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
853 ms_header_desc.wTotalLength = cpu_to_le16(n);
855 midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
857 /* configure the external IN jacks, each linked to an embedded OUT jack */
858 for (n = 0; n < midi->in_ports; n++) {
859 struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
860 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
862 in_ext->bLength = USB_DT_MIDI_IN_SIZE;
863 in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
864 in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
865 in_ext->bJackType = USB_MS_EXTERNAL;
866 in_ext->bJackID = jack++;
868 midi_function[i++] = (struct usb_descriptor_header *) in_ext;
870 out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
871 out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
872 out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
873 out_emb->bJackType = USB_MS_EMBEDDED;
874 out_emb->bJackID = jack++;
875 out_emb->bNrInputPins = 1;
876 out_emb->pins[0].baSourcePin = 1;
877 out_emb->pins[0].baSourceID = in_ext->bJackID;
879 midi_function[i++] = (struct usb_descriptor_header *) out_emb;
881 /* link it to the endpoint */
882 ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
885 /* configure the external OUT jacks, each linked to an embedded IN jack */
886 for (n = 0; n < midi->out_ports; n++) {
887 struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
888 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
890 in_emb->bLength = USB_DT_MIDI_IN_SIZE;
891 in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
892 in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
893 in_emb->bJackType = USB_MS_EMBEDDED;
894 in_emb->bJackID = jack++;
896 midi_function[i++] = (struct usb_descriptor_header *) in_emb;
898 out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
899 out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
900 out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
901 out_ext->bJackType = USB_MS_EXTERNAL;
902 out_ext->bJackID = jack++;
903 out_ext->bNrInputPins = 1;
905 out_ext->pins[0].baSourceID = in_emb->bJackID;
906 out_ext->pins[0].baSourcePin = 1;
907 midi_function[i++] = (struct usb_descriptor_header *) out_ext;
909 /* link it to the endpoint */
910 ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
913 /* configure the endpoint descriptors ... */
914 ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
915 ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
917 ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
918 ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
920 /* ... and add them to the list */
921 midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
922 midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
923 midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
924 midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
925 midi_function[i++] = NULL;
928 * support all relevant hardware speeds... we expect that when
929 * hardware is dual speed, all bulk-capable endpoints work at
932 /* copy descriptors, and track endpoint copies */
933 f->fs_descriptors = usb_copy_descriptors(midi_function);
934 if (!f->fs_descriptors)
937 if (gadget_is_dualspeed(c->cdev->gadget)) {
938 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
939 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
940 f->hs_descriptors = usb_copy_descriptors(midi_function);
941 if (!f->hs_descriptors)
945 kfree(midi_function);
950 kfree(midi_function);
951 usb_free_descriptors(f->hs_descriptors);
953 f_midi_unregister_card(midi);
955 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
960 static inline struct f_midi_opts *to_f_midi_opts(struct config_item *item)
962 return container_of(to_config_group(item), struct f_midi_opts,
966 static void midi_attr_release(struct config_item *item)
968 struct f_midi_opts *opts = to_f_midi_opts(item);
970 usb_put_function_instance(&opts->func_inst);
973 static struct configfs_item_operations midi_item_ops = {
974 .release = midi_attr_release,
977 #define F_MIDI_OPT(name, test_limit, limit) \
978 static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
980 struct f_midi_opts *opts = to_f_midi_opts(item); \
983 mutex_lock(&opts->lock); \
984 result = sprintf(page, "%d\n", opts->name); \
985 mutex_unlock(&opts->lock); \
990 static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
991 const char *page, size_t len) \
993 struct f_midi_opts *opts = to_f_midi_opts(item); \
997 mutex_lock(&opts->lock); \
998 if (opts->refcnt) { \
1003 ret = kstrtou32(page, 0, &num); \
1007 if (test_limit && num > limit) { \
1015 mutex_unlock(&opts->lock); \
1019 CONFIGFS_ATTR(f_midi_opts_, name);
1021 F_MIDI_OPT(index, true, SNDRV_CARDS);
1022 F_MIDI_OPT(buflen, false, 0);
1023 F_MIDI_OPT(qlen, false, 0);
1024 F_MIDI_OPT(in_ports, true, MAX_PORTS);
1025 F_MIDI_OPT(out_ports, true, MAX_PORTS);
1027 static ssize_t f_midi_opts_id_show(struct config_item *item, char *page)
1029 struct f_midi_opts *opts = to_f_midi_opts(item);
1032 mutex_lock(&opts->lock);
1034 result = strlcpy(page, opts->id, PAGE_SIZE);
1040 mutex_unlock(&opts->lock);
1045 static ssize_t f_midi_opts_id_store(struct config_item *item,
1046 const char *page, size_t len)
1048 struct f_midi_opts *opts = to_f_midi_opts(item);
1052 mutex_lock(&opts->lock);
1058 c = kstrndup(page, len, GFP_KERNEL);
1063 if (opts->id_allocated)
1066 opts->id_allocated = true;
1069 mutex_unlock(&opts->lock);
1073 CONFIGFS_ATTR(f_midi_opts_, id);
1075 static struct configfs_attribute *midi_attrs[] = {
1076 &f_midi_opts_attr_index,
1077 &f_midi_opts_attr_buflen,
1078 &f_midi_opts_attr_qlen,
1079 &f_midi_opts_attr_in_ports,
1080 &f_midi_opts_attr_out_ports,
1081 &f_midi_opts_attr_id,
1085 static struct config_item_type midi_func_type = {
1086 .ct_item_ops = &midi_item_ops,
1087 .ct_attrs = midi_attrs,
1088 .ct_owner = THIS_MODULE,
1091 static void f_midi_free_inst(struct usb_function_instance *f)
1093 struct f_midi_opts *opts;
1095 opts = container_of(f, struct f_midi_opts, func_inst);
1097 if (opts->id_allocated)
1103 static struct usb_function_instance *f_midi_alloc_inst(void)
1105 struct f_midi_opts *opts;
1107 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1109 return ERR_PTR(-ENOMEM);
1111 mutex_init(&opts->lock);
1112 opts->func_inst.free_func_inst = f_midi_free_inst;
1113 opts->index = SNDRV_DEFAULT_IDX1;
1114 opts->id = SNDRV_DEFAULT_STR1;
1118 opts->out_ports = 1;
1120 config_group_init_type_name(&opts->func_inst.group, "",
1123 return &opts->func_inst;
1126 static void f_midi_free(struct usb_function *f)
1128 struct f_midi *midi;
1129 struct f_midi_opts *opts;
1131 midi = func_to_midi(f);
1132 opts = container_of(f->fi, struct f_midi_opts, func_inst);
1134 mutex_lock(&opts->lock);
1135 kfifo_free(&midi->in_req_fifo);
1138 mutex_unlock(&opts->lock);
1141 static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
1143 struct usb_composite_dev *cdev = f->config->cdev;
1144 struct f_midi *midi = func_to_midi(f);
1145 struct snd_card *card;
1147 DBG(cdev, "unbind\n");
1149 /* just to be sure */
1155 snd_card_free(card);
1157 usb_free_all_descriptors(f);
1160 static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
1162 struct f_midi *midi = NULL;
1163 struct f_midi_opts *opts;
1166 opts = container_of(fi, struct f_midi_opts, func_inst);
1168 mutex_lock(&opts->lock);
1170 if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
1175 /* allocate and initialize one new instance */
1177 sizeof(*midi) + opts->in_ports * sizeof(*midi->in_ports_array),
1184 for (i = 0; i < opts->in_ports; i++)
1185 midi->in_ports_array[i].cable = i;
1187 /* set up ALSA midi devices */
1188 midi->id = kstrdup(opts->id, GFP_KERNEL);
1189 if (opts->id && !midi->id) {
1193 midi->in_ports = opts->in_ports;
1194 midi->out_ports = opts->out_ports;
1195 midi->index = opts->index;
1196 midi->buflen = opts->buflen;
1197 midi->qlen = opts->qlen;
1198 midi->in_last_port = 0;
1200 status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL);
1205 mutex_unlock(&opts->lock);
1207 midi->func.name = "gmidi function";
1208 midi->func.bind = f_midi_bind;
1209 midi->func.unbind = f_midi_unbind;
1210 midi->func.set_alt = f_midi_set_alt;
1211 midi->func.disable = f_midi_disable;
1212 midi->func.free_func = f_midi_free;
1217 mutex_unlock(&opts->lock);
1219 return ERR_PTR(status);
1222 DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);