2 * f_midi.c -- USB MIDI class function driver
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
8 * Rewritten for the composite framework
11 * Based on drivers/usb/gadget/f_audio.c,
13 * Copyright (C) 2008 Analog Devices, Inc
15 * and drivers/usb/gadget/midi.c,
16 * Copyright (C) 2006 Thumtronics Pty Ltd.
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>
27 #include <linux/spinlock.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/rawmidi.h>
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/gadget.h>
35 #include <linux/usb/audio.h>
36 #include <linux/usb/midi.h>
41 MODULE_AUTHOR("Ben Williamson");
42 MODULE_LICENSE("GPL v2");
44 static const char f_midi_shortname[] = "f_midi";
45 static const char f_midi_longname[] = "MIDI Gadget";
48 * We can only handle 16 cables on one single endpoint, as cable numbers are
49 * stored in 4-bit fields. And as the interface currently only holds one
50 * single endpoint, this is the maximum number of ports we can allow.
54 /* MIDI message states */
56 STATE_INITIAL = 0, /* pseudo state */
64 STATE_FINISHED, /* pseudo state */
68 * This is a gadget, and the IN/OUT naming is from the host's perspective.
69 * USB -> OUT endpoint -> rawmidi
70 * USB <- IN endpoint <- rawmidi
72 struct gmidi_in_port {
73 struct snd_rawmidi_substream *substream;
81 struct usb_function func;
82 struct usb_gadget *gadget;
83 struct usb_ep *in_ep, *out_ep;
84 struct snd_card *card;
85 struct snd_rawmidi *rmidi;
88 struct snd_rawmidi_substream *out_substream[MAX_PORTS];
90 unsigned long out_triggered;
91 struct tasklet_struct tasklet;
92 unsigned int in_ports;
93 unsigned int out_ports;
96 unsigned int buflen, qlen;
97 /* This fifo is used as a buffer ring for pre-allocated IN usb_requests */
98 DECLARE_KFIFO_PTR(in_req_fifo, struct usb_request *);
99 spinlock_t transmit_lock;
100 unsigned int in_last_port;
102 struct gmidi_in_port in_ports_array[/* in_ports */];
105 static inline struct f_midi *func_to_midi(struct usb_function *f)
107 return container_of(f, struct f_midi, func);
110 static void f_midi_transmit(struct f_midi *midi);
112 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
113 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
114 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
116 /* B.3.1 Standard AC Interface Descriptor */
117 static struct usb_interface_descriptor ac_interface_desc = {
118 .bLength = USB_DT_INTERFACE_SIZE,
119 .bDescriptorType = USB_DT_INTERFACE,
120 /* .bInterfaceNumber = DYNAMIC */
121 /* .bNumEndpoints = DYNAMIC */
122 .bInterfaceClass = USB_CLASS_AUDIO,
123 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
124 /* .iInterface = DYNAMIC */
127 /* B.3.2 Class-Specific AC Interface Descriptor */
128 static struct uac1_ac_header_descriptor_1 ac_header_desc = {
129 .bLength = UAC_DT_AC_HEADER_SIZE(1),
130 .bDescriptorType = USB_DT_CS_INTERFACE,
131 .bDescriptorSubtype = USB_MS_HEADER,
132 .bcdADC = cpu_to_le16(0x0100),
133 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
135 /* .baInterfaceNr = DYNAMIC */
138 /* B.4.1 Standard MS Interface Descriptor */
139 static struct usb_interface_descriptor ms_interface_desc = {
140 .bLength = USB_DT_INTERFACE_SIZE,
141 .bDescriptorType = USB_DT_INTERFACE,
142 /* .bInterfaceNumber = DYNAMIC */
144 .bInterfaceClass = USB_CLASS_AUDIO,
145 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
146 /* .iInterface = DYNAMIC */
149 /* B.4.2 Class-Specific MS Interface Descriptor */
150 static struct usb_ms_header_descriptor ms_header_desc = {
151 .bLength = USB_DT_MS_HEADER_SIZE,
152 .bDescriptorType = USB_DT_CS_INTERFACE,
153 .bDescriptorSubtype = USB_MS_HEADER,
154 .bcdMSC = cpu_to_le16(0x0100),
155 /* .wTotalLength = DYNAMIC */
158 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
159 static struct usb_endpoint_descriptor bulk_out_desc = {
160 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
161 .bDescriptorType = USB_DT_ENDPOINT,
162 .bEndpointAddress = USB_DIR_OUT,
163 .bmAttributes = USB_ENDPOINT_XFER_BULK,
166 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
167 static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
168 /* .bLength = DYNAMIC */
169 .bDescriptorType = USB_DT_CS_ENDPOINT,
170 .bDescriptorSubtype = USB_MS_GENERAL,
171 /* .bNumEmbMIDIJack = DYNAMIC */
172 /* .baAssocJackID = DYNAMIC */
175 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
176 static struct usb_endpoint_descriptor bulk_in_desc = {
177 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
178 .bDescriptorType = USB_DT_ENDPOINT,
179 .bEndpointAddress = USB_DIR_IN,
180 .bmAttributes = USB_ENDPOINT_XFER_BULK,
183 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
184 static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
185 /* .bLength = DYNAMIC */
186 .bDescriptorType = USB_DT_CS_ENDPOINT,
187 .bDescriptorSubtype = USB_MS_GENERAL,
188 /* .bNumEmbMIDIJack = DYNAMIC */
189 /* .baAssocJackID = DYNAMIC */
192 /* string IDs are assigned dynamically */
194 #define STRING_FUNC_IDX 0
196 static struct usb_string midi_string_defs[] = {
197 [STRING_FUNC_IDX].s = "MIDI function",
198 { } /* end of list */
201 static struct usb_gadget_strings midi_stringtab = {
202 .language = 0x0409, /* en-us */
203 .strings = midi_string_defs,
206 static struct usb_gadget_strings *midi_strings[] = {
211 static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
214 return alloc_ep_req(ep, length);
217 static const uint8_t f_midi_cin_length[] = {
218 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
222 * Receives a chunk of MIDI data.
224 static void f_midi_read_data(struct usb_ep *ep, int cable,
225 uint8_t *data, int length)
227 struct f_midi *midi = ep->driver_data;
228 struct snd_rawmidi_substream *substream = midi->out_substream[cable];
231 /* Nobody is listening - throw it on the floor. */
234 if (!test_bit(cable, &midi->out_triggered))
237 snd_rawmidi_receive(substream, data, length);
240 static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
245 for (i = 0; i + 3 < req->actual; i += 4)
247 int cable = buf[i] >> 4;
248 int length = f_midi_cin_length[buf[i] & 0x0f];
249 f_midi_read_data(ep, cable, &buf[i + 1], length);
254 f_midi_complete(struct usb_ep *ep, struct usb_request *req)
256 struct f_midi *midi = ep->driver_data;
257 struct usb_composite_dev *cdev = midi->func.config->cdev;
258 int status = req->status;
261 case 0: /* normal completion */
262 if (ep == midi->out_ep) {
263 /* We received stuff. req is queued again, below */
264 f_midi_handle_out_data(ep, req);
265 } else if (ep == midi->in_ep) {
266 /* Our transmit completed. See if there's more to go.
267 * f_midi_transmit eats req, don't queue it again. */
269 f_midi_transmit(midi);
274 /* this endpoint is normally active while we're configured */
275 case -ECONNABORTED: /* hardware forced ep reset */
276 case -ECONNRESET: /* request dequeued */
277 case -ESHUTDOWN: /* disconnect from host */
278 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
279 req->actual, req->length);
280 if (ep == midi->out_ep) {
281 f_midi_handle_out_data(ep, req);
282 /* We don't need to free IN requests because it's handled
283 * by the midi->in_req_fifo. */
284 free_ep_req(ep, req);
288 case -EOVERFLOW: /* buffer overrun on read means that
289 * we didn't provide a big enough buffer.
292 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
293 status, req->actual, req->length);
295 case -EREMOTEIO: /* short read */
299 status = usb_ep_queue(ep, req, GFP_ATOMIC);
301 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
302 ep->name, req->length, status);
304 /* FIXME recover later ... somehow */
308 static void f_midi_drop_out_substreams(struct f_midi *midi)
312 for (i = 0; i < midi->in_ports; i++) {
313 struct gmidi_in_port *port = midi->in_ports_array + i;
314 struct snd_rawmidi_substream *substream = port->substream;
316 if (port->active && substream)
317 snd_rawmidi_drop_output(substream);
321 static int f_midi_start_ep(struct f_midi *midi,
322 struct usb_function *f,
326 struct usb_composite_dev *cdev = f->config->cdev;
330 err = config_ep_by_speed(midi->gadget, f, ep);
332 ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
336 err = usb_ep_enable(ep);
338 ERROR(cdev, "can't start %s: %d\n", ep->name, err);
342 ep->driver_data = midi;
347 static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
349 struct f_midi *midi = func_to_midi(f);
353 /* we only set alt for MIDIStreaming interface */
354 if (intf != midi->ms_id)
357 err = f_midi_start_ep(midi, f, midi->in_ep);
361 err = f_midi_start_ep(midi, f, midi->out_ep);
365 /* pre-allocate write usb requests to use on f_midi_transmit. */
366 while (kfifo_avail(&midi->in_req_fifo)) {
367 struct usb_request *req =
368 midi_alloc_ep_req(midi->in_ep, midi->buflen);
374 req->complete = f_midi_complete;
376 kfifo_put(&midi->in_req_fifo, req);
379 /* allocate a bunch of read buffers and queue them all at once. */
380 for (i = 0; i < midi->qlen && err == 0; i++) {
381 struct usb_request *req =
382 midi_alloc_ep_req(midi->out_ep, midi->buflen);
387 req->complete = f_midi_complete;
388 err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
390 ERROR(midi, "%s: couldn't enqueue request: %d\n",
391 midi->out_ep->name, err);
392 free_ep_req(midi->out_ep, req);
400 static void f_midi_disable(struct usb_function *f)
402 struct f_midi *midi = func_to_midi(f);
403 struct usb_composite_dev *cdev = f->config->cdev;
404 struct usb_request *req = NULL;
406 DBG(cdev, "disable\n");
409 * just disable endpoints, forcing completion of pending i/o.
410 * all our completion handlers free their requests in this case.
412 usb_ep_disable(midi->in_ep);
413 usb_ep_disable(midi->out_ep);
415 /* release IN requests */
416 while (kfifo_get(&midi->in_req_fifo, &req))
417 free_ep_req(midi->in_ep, req);
419 f_midi_drop_out_substreams(midi);
422 static int f_midi_snd_free(struct snd_device *device)
428 * Converts MIDI commands to USB MIDI packets.
430 static void f_midi_transmit_byte(struct usb_request *req,
431 struct gmidi_in_port *port, uint8_t b)
433 uint8_t p[4] = { port->cable << 4, 0, 0, 0 };
434 uint8_t next_state = STATE_INITIAL;
438 /* System Real-Time Messages */
441 next_state = port->state;
442 port->state = STATE_REAL_TIME;
447 switch (port->state) {
451 next_state = STATE_FINISHED;
455 p[1] = port->data[0];
457 next_state = STATE_FINISHED;
461 p[1] = port->data[0];
462 p[2] = port->data[1];
464 next_state = STATE_FINISHED;
468 next_state = port->state;
469 port->state = STATE_INITIAL;
474 /* System Common Messages */
475 port->data[0] = port->data[1] = 0;
476 port->state = STATE_INITIAL;
481 next_state = STATE_SYSEX_1;
486 next_state = STATE_1PARAM;
490 next_state = STATE_2PARAM_1;
494 next_state = STATE_INITIAL;
499 next_state = STATE_FINISHED;
506 * Channel Voice Messages, Channel Mode Messages
507 * and Control Change Messages.
511 port->state = STATE_INITIAL;
512 if (b >= 0xc0 && b <= 0xdf)
513 next_state = STATE_1PARAM;
515 next_state = STATE_2PARAM_1;
519 /* Message parameters */
520 switch (port->state) {
522 if (port->data[0] < 0xf0)
523 p[0] |= port->data[0] >> 4;
527 p[1] = port->data[0];
529 /* This is to allow Running State Messages */
530 next_state = STATE_1PARAM;
534 next_state = STATE_2PARAM_2;
537 if (port->data[0] < 0xf0)
538 p[0] |= port->data[0] >> 4;
542 p[1] = port->data[0];
543 p[2] = port->data[1];
545 /* This is to allow Running State Messages */
546 next_state = STATE_2PARAM_1;
550 next_state = STATE_SYSEX_1;
554 next_state = STATE_SYSEX_2;
558 p[1] = port->data[0];
559 p[2] = port->data[1];
561 next_state = STATE_SYSEX_0;
567 /* States where we have to write into the USB request */
568 if (next_state == STATE_FINISHED ||
569 port->state == STATE_SYSEX_2 ||
570 port->state == STATE_1PARAM ||
571 port->state == STATE_2PARAM_2 ||
572 port->state == STATE_REAL_TIME) {
574 unsigned int length = req->length;
575 u8 *buf = (u8 *)req->buf + length;
577 memcpy(buf, p, sizeof(p));
578 req->length = length + sizeof(p);
580 if (next_state == STATE_FINISHED) {
581 next_state = STATE_INITIAL;
582 port->data[0] = port->data[1] = 0;
586 port->state = next_state;
589 static int f_midi_do_transmit(struct f_midi *midi, struct usb_ep *ep)
591 struct usb_request *req = NULL;
597 * We peek the request in order to reuse it if it fails to enqueue on
600 len = kfifo_peek(&midi->in_req_fifo, &req);
602 ERROR(midi, "%s: Couldn't get usb request\n", __func__);
607 * If buffer overrun, then we ignore this transmission.
608 * IMPORTANT: This will cause the user-space rawmidi device to block
609 * until a) usb requests have been completed or b) snd_rawmidi_write()
615 for (i = midi->in_last_port; i < midi->in_ports; ++i) {
616 struct gmidi_in_port *port = midi->in_ports_array + i;
617 struct snd_rawmidi_substream *substream = port->substream;
619 if (!port->active || !substream)
622 while (req->length + 3 < midi->buflen) {
625 if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
629 f_midi_transmit_byte(req, port, b);
632 active = !!port->active;
636 midi->in_last_port = active ? i : 0;
638 if (req->length <= 0)
641 err = usb_ep_queue(ep, req, GFP_ATOMIC);
643 ERROR(midi, "%s failed to queue req: %d\n",
644 midi->in_ep->name, err);
645 req->length = 0; /* Re-use request next time. */
647 /* Upon success, put request at the back of the queue. */
648 kfifo_skip(&midi->in_req_fifo);
649 kfifo_put(&midi->in_req_fifo, req);
656 static void f_midi_transmit(struct f_midi *midi)
658 struct usb_ep *ep = midi->in_ep;
662 /* We only care about USB requests if IN endpoint is enabled */
663 if (!ep || !ep->enabled)
666 spin_lock_irqsave(&midi->transmit_lock, flags);
669 ret = f_midi_do_transmit(midi, ep);
671 spin_unlock_irqrestore(&midi->transmit_lock, flags);
676 spin_unlock_irqrestore(&midi->transmit_lock, flags);
681 f_midi_drop_out_substreams(midi);
684 static void f_midi_in_tasklet(unsigned long data)
686 struct f_midi *midi = (struct f_midi *) data;
687 f_midi_transmit(midi);
690 static int f_midi_in_open(struct snd_rawmidi_substream *substream)
692 struct f_midi *midi = substream->rmidi->private_data;
693 struct gmidi_in_port *port;
695 if (substream->number >= midi->in_ports)
698 VDBG(midi, "%s()\n", __func__);
699 port = midi->in_ports_array + substream->number;
700 port->substream = substream;
701 port->state = STATE_INITIAL;
705 static int f_midi_in_close(struct snd_rawmidi_substream *substream)
707 struct f_midi *midi = substream->rmidi->private_data;
709 VDBG(midi, "%s()\n", __func__);
713 static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
715 struct f_midi *midi = substream->rmidi->private_data;
717 if (substream->number >= midi->in_ports)
720 VDBG(midi, "%s() %d\n", __func__, up);
721 midi->in_ports_array[substream->number].active = up;
723 tasklet_hi_schedule(&midi->tasklet);
726 static int f_midi_out_open(struct snd_rawmidi_substream *substream)
728 struct f_midi *midi = substream->rmidi->private_data;
730 if (substream->number >= MAX_PORTS)
733 VDBG(midi, "%s()\n", __func__);
734 midi->out_substream[substream->number] = substream;
738 static int f_midi_out_close(struct snd_rawmidi_substream *substream)
740 struct f_midi *midi = substream->rmidi->private_data;
742 VDBG(midi, "%s()\n", __func__);
746 static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
748 struct f_midi *midi = substream->rmidi->private_data;
750 VDBG(midi, "%s()\n", __func__);
753 set_bit(substream->number, &midi->out_triggered);
755 clear_bit(substream->number, &midi->out_triggered);
758 static struct snd_rawmidi_ops gmidi_in_ops = {
759 .open = f_midi_in_open,
760 .close = f_midi_in_close,
761 .trigger = f_midi_in_trigger,
764 static struct snd_rawmidi_ops gmidi_out_ops = {
765 .open = f_midi_out_open,
766 .close = f_midi_out_close,
767 .trigger = f_midi_out_trigger
770 static inline void f_midi_unregister_card(struct f_midi *midi)
773 snd_card_free(midi->card);
778 /* register as a sound "card" */
779 static int f_midi_register_card(struct f_midi *midi)
781 struct snd_card *card;
782 struct snd_rawmidi *rmidi;
784 static struct snd_device_ops ops = {
785 .dev_free = f_midi_snd_free,
788 err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
789 THIS_MODULE, 0, &card);
791 ERROR(midi, "snd_card_new() failed\n");
796 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
798 ERROR(midi, "snd_device_new() failed: error %d\n", err);
802 strcpy(card->driver, f_midi_longname);
803 strcpy(card->longname, f_midi_longname);
804 strcpy(card->shortname, f_midi_shortname);
807 snd_component_add(card, "MIDI");
808 err = snd_rawmidi_new(card, card->longname, 0,
809 midi->out_ports, midi->in_ports, &rmidi);
811 ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
815 midi->in_last_port = 0;
816 strcpy(rmidi->name, card->shortname);
817 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
818 SNDRV_RAWMIDI_INFO_INPUT |
819 SNDRV_RAWMIDI_INFO_DUPLEX;
820 rmidi->private_data = midi;
823 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
824 * It's an upside-down world being a gadget.
826 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
827 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
829 /* register it - we're ready to go */
830 err = snd_card_register(card);
832 ERROR(midi, "snd_card_register() failed\n");
836 VDBG(midi, "%s() finished ok\n", __func__);
840 f_midi_unregister_card(midi);
844 /* MIDI function driver setup/binding */
846 static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
848 struct usb_descriptor_header **midi_function;
849 struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
850 struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
851 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
852 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
853 struct usb_composite_dev *cdev = c->cdev;
854 struct f_midi *midi = func_to_midi(f);
855 struct usb_string *us;
856 int status, n, jack = 1, i = 0;
858 midi->gadget = cdev->gadget;
859 tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
860 status = f_midi_register_card(midi);
864 /* maybe allocate device-global string ID */
865 us = usb_gstrings_attach(c->cdev, midi_strings,
866 ARRAY_SIZE(midi_string_defs));
868 status = PTR_ERR(us);
871 ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id;
873 /* We have two interfaces, AudioControl and MIDIStreaming */
874 status = usb_interface_id(c, f);
877 ac_interface_desc.bInterfaceNumber = status;
879 status = usb_interface_id(c, f);
882 ms_interface_desc.bInterfaceNumber = status;
883 ac_header_desc.baInterfaceNr[0] = status;
884 midi->ms_id = status;
888 /* allocate instance-specific endpoints */
889 midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
893 midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
897 /* allocate temporary function list */
898 midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
900 if (!midi_function) {
906 * construct the function's descriptor set. As the number of
907 * input and output MIDI ports is configurable, we have to do
911 /* add the headers - these are always the same */
912 midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
913 midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
914 midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
916 /* calculate the header's wTotalLength */
917 n = USB_DT_MS_HEADER_SIZE
918 + (midi->in_ports + midi->out_ports) *
919 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
920 ms_header_desc.wTotalLength = cpu_to_le16(n);
922 midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
924 /* configure the external IN jacks, each linked to an embedded OUT jack */
925 for (n = 0; n < midi->in_ports; n++) {
926 struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
927 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
929 in_ext->bLength = USB_DT_MIDI_IN_SIZE;
930 in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
931 in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
932 in_ext->bJackType = USB_MS_EXTERNAL;
933 in_ext->bJackID = jack++;
935 midi_function[i++] = (struct usb_descriptor_header *) in_ext;
937 out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
938 out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
939 out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
940 out_emb->bJackType = USB_MS_EMBEDDED;
941 out_emb->bJackID = jack++;
942 out_emb->bNrInputPins = 1;
943 out_emb->pins[0].baSourcePin = 1;
944 out_emb->pins[0].baSourceID = in_ext->bJackID;
946 midi_function[i++] = (struct usb_descriptor_header *) out_emb;
948 /* link it to the endpoint */
949 ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
952 /* configure the external OUT jacks, each linked to an embedded IN jack */
953 for (n = 0; n < midi->out_ports; n++) {
954 struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
955 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
957 in_emb->bLength = USB_DT_MIDI_IN_SIZE;
958 in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
959 in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
960 in_emb->bJackType = USB_MS_EMBEDDED;
961 in_emb->bJackID = jack++;
963 midi_function[i++] = (struct usb_descriptor_header *) in_emb;
965 out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
966 out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
967 out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
968 out_ext->bJackType = USB_MS_EXTERNAL;
969 out_ext->bJackID = jack++;
970 out_ext->bNrInputPins = 1;
972 out_ext->pins[0].baSourceID = in_emb->bJackID;
973 out_ext->pins[0].baSourcePin = 1;
974 midi_function[i++] = (struct usb_descriptor_header *) out_ext;
976 /* link it to the endpoint */
977 ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
980 /* configure the endpoint descriptors ... */
981 ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
982 ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
984 ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
985 ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
987 /* ... and add them to the list */
988 midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
989 midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
990 midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
991 midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
992 midi_function[i++] = NULL;
995 * support all relevant hardware speeds... we expect that when
996 * hardware is dual speed, all bulk-capable endpoints work at
999 /* copy descriptors, and track endpoint copies */
1000 f->fs_descriptors = usb_copy_descriptors(midi_function);
1001 if (!f->fs_descriptors)
1004 if (gadget_is_dualspeed(c->cdev->gadget)) {
1005 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
1006 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
1007 f->hs_descriptors = usb_copy_descriptors(midi_function);
1008 if (!f->hs_descriptors)
1012 kfree(midi_function);
1017 kfree(midi_function);
1018 usb_free_descriptors(f->hs_descriptors);
1020 f_midi_unregister_card(midi);
1022 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1027 static inline struct f_midi_opts *to_f_midi_opts(struct config_item *item)
1029 return container_of(to_config_group(item), struct f_midi_opts,
1033 static void midi_attr_release(struct config_item *item)
1035 struct f_midi_opts *opts = to_f_midi_opts(item);
1037 usb_put_function_instance(&opts->func_inst);
1040 static struct configfs_item_operations midi_item_ops = {
1041 .release = midi_attr_release,
1044 #define F_MIDI_OPT(name, test_limit, limit) \
1045 static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
1047 struct f_midi_opts *opts = to_f_midi_opts(item); \
1050 mutex_lock(&opts->lock); \
1051 result = sprintf(page, "%d\n", opts->name); \
1052 mutex_unlock(&opts->lock); \
1057 static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
1058 const char *page, size_t len) \
1060 struct f_midi_opts *opts = to_f_midi_opts(item); \
1064 mutex_lock(&opts->lock); \
1065 if (opts->refcnt) { \
1070 ret = kstrtou32(page, 0, &num); \
1074 if (test_limit && num > limit) { \
1082 mutex_unlock(&opts->lock); \
1086 CONFIGFS_ATTR(f_midi_opts_, name);
1088 F_MIDI_OPT(index, true, SNDRV_CARDS);
1089 F_MIDI_OPT(buflen, false, 0);
1090 F_MIDI_OPT(qlen, false, 0);
1091 F_MIDI_OPT(in_ports, true, MAX_PORTS);
1092 F_MIDI_OPT(out_ports, true, MAX_PORTS);
1094 static ssize_t f_midi_opts_id_show(struct config_item *item, char *page)
1096 struct f_midi_opts *opts = to_f_midi_opts(item);
1099 mutex_lock(&opts->lock);
1101 result = strlcpy(page, opts->id, PAGE_SIZE);
1107 mutex_unlock(&opts->lock);
1112 static ssize_t f_midi_opts_id_store(struct config_item *item,
1113 const char *page, size_t len)
1115 struct f_midi_opts *opts = to_f_midi_opts(item);
1119 mutex_lock(&opts->lock);
1125 c = kstrndup(page, len, GFP_KERNEL);
1130 if (opts->id_allocated)
1133 opts->id_allocated = true;
1136 mutex_unlock(&opts->lock);
1140 CONFIGFS_ATTR(f_midi_opts_, id);
1142 static struct configfs_attribute *midi_attrs[] = {
1143 &f_midi_opts_attr_index,
1144 &f_midi_opts_attr_buflen,
1145 &f_midi_opts_attr_qlen,
1146 &f_midi_opts_attr_in_ports,
1147 &f_midi_opts_attr_out_ports,
1148 &f_midi_opts_attr_id,
1152 static struct config_item_type midi_func_type = {
1153 .ct_item_ops = &midi_item_ops,
1154 .ct_attrs = midi_attrs,
1155 .ct_owner = THIS_MODULE,
1158 static void f_midi_free_inst(struct usb_function_instance *f)
1160 struct f_midi_opts *opts;
1162 opts = container_of(f, struct f_midi_opts, func_inst);
1164 if (opts->id_allocated)
1170 static struct usb_function_instance *f_midi_alloc_inst(void)
1172 struct f_midi_opts *opts;
1174 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1176 return ERR_PTR(-ENOMEM);
1178 mutex_init(&opts->lock);
1179 opts->func_inst.free_func_inst = f_midi_free_inst;
1180 opts->index = SNDRV_DEFAULT_IDX1;
1181 opts->id = SNDRV_DEFAULT_STR1;
1185 opts->out_ports = 1;
1187 config_group_init_type_name(&opts->func_inst.group, "",
1190 return &opts->func_inst;
1193 static void f_midi_free(struct usb_function *f)
1195 struct f_midi *midi;
1196 struct f_midi_opts *opts;
1198 midi = func_to_midi(f);
1199 opts = container_of(f->fi, struct f_midi_opts, func_inst);
1201 mutex_lock(&opts->lock);
1202 kfifo_free(&midi->in_req_fifo);
1205 mutex_unlock(&opts->lock);
1208 static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
1210 struct usb_composite_dev *cdev = f->config->cdev;
1211 struct f_midi *midi = func_to_midi(f);
1212 struct snd_card *card;
1214 DBG(cdev, "unbind\n");
1216 /* just to be sure */
1222 snd_card_free(card);
1224 usb_free_all_descriptors(f);
1227 static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
1229 struct f_midi *midi = NULL;
1230 struct f_midi_opts *opts;
1233 opts = container_of(fi, struct f_midi_opts, func_inst);
1235 mutex_lock(&opts->lock);
1237 if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
1242 /* allocate and initialize one new instance */
1244 sizeof(*midi) + opts->in_ports * sizeof(*midi->in_ports_array),
1251 for (i = 0; i < opts->in_ports; i++)
1252 midi->in_ports_array[i].cable = i;
1254 /* set up ALSA midi devices */
1255 midi->id = kstrdup(opts->id, GFP_KERNEL);
1256 if (opts->id && !midi->id) {
1260 midi->in_ports = opts->in_ports;
1261 midi->out_ports = opts->out_ports;
1262 midi->index = opts->index;
1263 midi->buflen = opts->buflen;
1264 midi->qlen = opts->qlen;
1265 midi->in_last_port = 0;
1267 status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL);
1271 spin_lock_init(&midi->transmit_lock);
1274 mutex_unlock(&opts->lock);
1276 midi->func.name = "gmidi function";
1277 midi->func.bind = f_midi_bind;
1278 midi->func.unbind = f_midi_unbind;
1279 midi->func.set_alt = f_midi_set_alt;
1280 midi->func.disable = f_midi_disable;
1281 midi->func.free_func = f_midi_free;
1286 mutex_unlock(&opts->lock);
1288 return ERR_PTR(status);
1291 DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);