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/utsname.h>
25 #include <linux/device.h>
27 #include <sound/core.h>
28 #include <sound/initval.h>
29 #include <sound/rawmidi.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb/audio.h>
34 #include <linux/usb/midi.h>
36 MODULE_AUTHOR("Ben Williamson");
37 MODULE_LICENSE("GPL v2");
39 static const char f_midi_shortname
[] = "f_midi";
40 static const char f_midi_longname
[] = "MIDI Gadget";
43 * This is a gadget, and the IN/OUT naming is from the host's perspective.
44 * USB -> OUT endpoint -> rawmidi
45 * USB <- IN endpoint <- rawmidi
47 struct gmidi_in_port
{
50 uint8_t cable
; /* cable number << 4 */
52 #define STATE_UNKNOWN 0
53 #define STATE_1PARAM 1
54 #define STATE_2PARAM_1 2
55 #define STATE_2PARAM_2 3
56 #define STATE_SYSEX_0 4
57 #define STATE_SYSEX_1 5
58 #define STATE_SYSEX_2 6
63 struct usb_function func
;
64 struct usb_gadget
*gadget
;
65 struct usb_ep
*in_ep
, *out_ep
;
66 struct snd_card
*card
;
67 struct snd_rawmidi
*rmidi
;
68 struct snd_rawmidi_substream
*in_substream
;
69 struct snd_rawmidi_substream
*out_substream
;
71 /* For the moment we only support one port in
72 each direction, but in_port is kept as a
73 separate struct so we can have more later. */
74 struct gmidi_in_port in_port
;
75 unsigned long out_triggered
;
76 struct tasklet_struct tasklet
;
79 unsigned int buflen
, qlen
;
82 static inline struct f_midi
*func_to_midi(struct usb_function
*f
)
84 return container_of(f
, struct f_midi
, func
);
87 static void f_midi_transmit(struct f_midi
*midi
, struct usb_request
*req
);
89 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
90 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
91 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1);
93 /* B.3.1 Standard AC Interface Descriptor */
94 static struct usb_interface_descriptor ac_interface_desc __initdata
= {
95 .bLength
= USB_DT_INTERFACE_SIZE
,
96 .bDescriptorType
= USB_DT_INTERFACE
,
97 /* .bInterfaceNumber = DYNAMIC */
98 /* .bNumEndpoints = DYNAMIC */
99 .bInterfaceClass
= USB_CLASS_AUDIO
,
100 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
101 /* .iInterface = DYNAMIC */
104 /* B.3.2 Class-Specific AC Interface Descriptor */
105 static struct uac1_ac_header_descriptor_1 ac_header_desc __initdata
= {
106 .bLength
= UAC_DT_AC_HEADER_SIZE(1),
107 .bDescriptorType
= USB_DT_CS_INTERFACE
,
108 .bDescriptorSubtype
= USB_MS_HEADER
,
109 .bcdADC
= cpu_to_le16(0x0100),
110 .wTotalLength
= cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
112 /* .baInterfaceNr = DYNAMIC */
115 /* B.4.1 Standard MS Interface Descriptor */
116 static struct usb_interface_descriptor ms_interface_desc __initdata
= {
117 .bLength
= USB_DT_INTERFACE_SIZE
,
118 .bDescriptorType
= USB_DT_INTERFACE
,
119 /* .bInterfaceNumber = DYNAMIC */
121 .bInterfaceClass
= USB_CLASS_AUDIO
,
122 .bInterfaceSubClass
= USB_SUBCLASS_MIDISTREAMING
,
123 /* .iInterface = DYNAMIC */
126 /* B.4.2 Class-Specific MS Interface Descriptor */
127 static struct usb_ms_header_descriptor ms_header_desc __initdata
= {
128 .bLength
= USB_DT_MS_HEADER_SIZE
,
129 .bDescriptorType
= USB_DT_CS_INTERFACE
,
130 .bDescriptorSubtype
= USB_MS_HEADER
,
131 .bcdMSC
= cpu_to_le16(0x0100),
132 .wTotalLength
= cpu_to_le16(USB_DT_MS_HEADER_SIZE
133 + 2*USB_DT_MIDI_IN_SIZE
134 + 2*USB_DT_MIDI_OUT_SIZE(1)),
137 #define JACK_IN_EMB 1
138 #define JACK_IN_EXT 2
139 #define JACK_OUT_EMB 3
140 #define JACK_OUT_EXT 4
142 /* B.4.3 MIDI IN Jack Descriptors */
143 static const struct usb_midi_in_jack_descriptor jack_in_emb_desc
= {
144 .bLength
= USB_DT_MIDI_IN_SIZE
,
145 .bDescriptorType
= USB_DT_CS_INTERFACE
,
146 .bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
,
147 .bJackType
= USB_MS_EMBEDDED
,
148 .bJackID
= JACK_IN_EMB
,
151 static const struct usb_midi_in_jack_descriptor jack_in_ext_desc
= {
152 .bLength
= USB_DT_MIDI_IN_SIZE
,
153 .bDescriptorType
= USB_DT_CS_INTERFACE
,
154 .bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
,
155 .bJackType
= USB_MS_EXTERNAL
,
156 .bJackID
= JACK_IN_EXT
,
159 /* B.4.4 MIDI OUT Jack Descriptors */
160 static const struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc
= {
161 .bLength
= USB_DT_MIDI_OUT_SIZE(1),
162 .bDescriptorType
= USB_DT_CS_INTERFACE
,
163 .bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
,
164 .bJackType
= USB_MS_EMBEDDED
,
165 .bJackID
= JACK_OUT_EMB
,
169 .baSourceID
= JACK_IN_EXT
,
175 static const struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc
= {
176 .bLength
= USB_DT_MIDI_OUT_SIZE(1),
177 .bDescriptorType
= USB_DT_CS_INTERFACE
,
178 .bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
,
179 .bJackType
= USB_MS_EXTERNAL
,
180 .bJackID
= JACK_OUT_EXT
,
184 .baSourceID
= JACK_IN_EMB
,
190 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
191 static struct usb_endpoint_descriptor bulk_out_desc
= {
192 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
193 .bDescriptorType
= USB_DT_ENDPOINT
,
194 .bEndpointAddress
= USB_DIR_OUT
,
195 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
198 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
199 static const struct usb_ms_endpoint_descriptor_1 ms_out_desc
= {
200 .bLength
= USB_DT_MS_ENDPOINT_SIZE(1),
201 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
202 .bDescriptorSubtype
= USB_MS_GENERAL
,
203 .bNumEmbMIDIJack
= 1,
209 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
210 static struct usb_endpoint_descriptor bulk_in_desc
= {
211 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
212 .bDescriptorType
= USB_DT_ENDPOINT
,
213 .bEndpointAddress
= USB_DIR_IN
,
214 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
217 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
218 static const struct usb_ms_endpoint_descriptor_1 ms_in_desc
= {
219 .bLength
= USB_DT_MS_ENDPOINT_SIZE(1),
220 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
221 .bDescriptorSubtype
= USB_MS_GENERAL
,
222 .bNumEmbMIDIJack
= 1,
228 static struct usb_descriptor_header
*midi_function
[] __initdata
= {
229 (struct usb_descriptor_header
*)&ac_interface_desc
,
230 (struct usb_descriptor_header
*)&ac_header_desc
,
231 (struct usb_descriptor_header
*)&ms_interface_desc
,
233 (struct usb_descriptor_header
*)&ms_header_desc
,
234 (struct usb_descriptor_header
*)&jack_in_emb_desc
,
235 (struct usb_descriptor_header
*)&jack_in_ext_desc
,
236 (struct usb_descriptor_header
*)&jack_out_emb_desc
,
237 (struct usb_descriptor_header
*)&jack_out_ext_desc
,
238 /* If you add more jacks, update ms_header_desc.wTotalLength */
240 (struct usb_descriptor_header
*)&bulk_out_desc
,
241 (struct usb_descriptor_header
*)&ms_out_desc
,
242 (struct usb_descriptor_header
*)&bulk_in_desc
,
243 (struct usb_descriptor_header
*)&ms_in_desc
,
247 /* string IDs are assigned dynamically */
249 #define STRING_FUNC_IDX 0
251 static struct usb_string midi_string_defs
[] = {
252 [STRING_FUNC_IDX
].s
= "MIDI function",
253 { } /* end of list */
256 static struct usb_gadget_strings midi_stringtab
= {
257 .language
= 0x0409, /* en-us */
258 .strings
= midi_string_defs
,
261 static struct usb_gadget_strings
*midi_strings
[] = {
266 static struct usb_request
*alloc_ep_req(struct usb_ep
*ep
, unsigned length
)
268 struct usb_request
*req
;
270 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
272 req
->length
= length
;
273 req
->buf
= kmalloc(length
, GFP_ATOMIC
);
275 usb_ep_free_request(ep
, req
);
282 static void free_ep_req(struct usb_ep
*ep
, struct usb_request
*req
)
285 usb_ep_free_request(ep
, req
);
288 static const uint8_t f_midi_cin_length
[] = {
289 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
293 * Receives a chunk of MIDI data.
295 static void f_midi_read_data(struct usb_ep
*ep
, int cable
,
296 uint8_t *data
, int length
)
298 struct f_midi
*midi
= ep
->driver_data
;
299 /* cable is ignored, because for now we only have one. */
301 if (!midi
->out_substream
)
302 /* Nobody is listening - throw it on the floor. */
305 if (!test_bit(midi
->out_substream
->number
, &midi
->out_triggered
))
308 snd_rawmidi_receive(midi
->out_substream
, data
, length
);
311 static void f_midi_handle_out_data(struct usb_ep
*ep
, struct usb_request
*req
)
316 for (i
= 0; i
+ 3 < req
->actual
; i
+= 4)
318 int cable
= buf
[i
] >> 4;
319 int length
= f_midi_cin_length
[buf
[i
] & 0x0f];
320 f_midi_read_data(ep
, cable
, &buf
[i
+ 1], length
);
325 f_midi_complete(struct usb_ep
*ep
, struct usb_request
*req
)
327 struct f_midi
*midi
= ep
->driver_data
;
328 struct usb_composite_dev
*cdev
= midi
->func
.config
->cdev
;
329 int status
= req
->status
;
332 case 0: /* normal completion */
333 if (ep
== midi
->out_ep
) {
334 /* We received stuff. req is queued again, below */
335 f_midi_handle_out_data(ep
, req
);
336 } else if (ep
== midi
->in_ep
) {
337 /* Our transmit completed. See if there's more to go.
338 * f_midi_transmit eats req, don't queue it again. */
339 f_midi_transmit(midi
, req
);
344 /* this endpoint is normally active while we're configured */
345 case -ECONNABORTED
: /* hardware forced ep reset */
346 case -ECONNRESET
: /* request dequeued */
347 case -ESHUTDOWN
: /* disconnect from host */
348 VDBG(cdev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
349 req
->actual
, req
->length
);
350 if (ep
== midi
->out_ep
)
351 f_midi_handle_out_data(ep
, req
);
353 free_ep_req(ep
, req
);
356 case -EOVERFLOW
: /* buffer overrun on read means that
357 * we didn't provide a big enough buffer.
360 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
361 status
, req
->actual
, req
->length
);
363 case -EREMOTEIO
: /* short read */
367 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
369 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
370 ep
->name
, req
->length
, status
);
372 /* FIXME recover later ... somehow */
376 static int f_midi_start_ep(struct f_midi
*midi
,
377 struct usb_function
*f
,
381 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
386 err
= config_ep_by_speed(midi
->gadget
, f
, ep
);
388 ERROR(cdev
, "can't configure %s: %d\n", ep
->name
, err
);
392 err
= usb_ep_enable(ep
);
394 ERROR(cdev
, "can't start %s: %d\n", ep
->name
, err
);
398 ep
->driver_data
= midi
;
403 static int f_midi_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
405 struct f_midi
*midi
= func_to_midi(f
);
406 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
410 err
= f_midi_start_ep(midi
, f
, midi
->in_ep
);
414 err
= f_midi_start_ep(midi
, f
, midi
->out_ep
);
418 if (midi
->out_ep
->driver_data
)
419 usb_ep_disable(midi
->out_ep
);
421 err
= config_ep_by_speed(midi
->gadget
, f
, midi
->out_ep
);
423 ERROR(cdev
, "can't configure %s: %d\n",
424 midi
->out_ep
->name
, err
);
428 err
= usb_ep_enable(midi
->out_ep
);
430 ERROR(cdev
, "can't start %s: %d\n",
431 midi
->out_ep
->name
, err
);
435 midi
->out_ep
->driver_data
= midi
;
437 /* allocate a bunch of read buffers and queue them all at once. */
438 for (i
= 0; i
< midi
->qlen
&& err
== 0; i
++) {
439 struct usb_request
*req
=
440 alloc_ep_req(midi
->out_ep
, midi
->buflen
);
444 req
->complete
= f_midi_complete
;
445 err
= usb_ep_queue(midi
->out_ep
, req
, GFP_ATOMIC
);
447 ERROR(midi
, "%s queue req: %d\n",
448 midi
->out_ep
->name
, err
);
455 static void f_midi_disable(struct usb_function
*f
)
457 struct f_midi
*midi
= func_to_midi(f
);
458 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
460 DBG(cdev
, "disable\n");
463 * just disable endpoints, forcing completion of pending i/o.
464 * all our completion handlers free their requests in this case.
466 usb_ep_disable(midi
->in_ep
);
467 usb_ep_disable(midi
->out_ep
);
470 static void f_midi_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
472 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
473 struct f_midi
*midi
= func_to_midi(f
);
474 struct snd_card
*card
;
476 DBG(cdev
, "unbind\n");
478 /* just to be sure */
489 usb_free_descriptors(f
->descriptors
);
493 static int f_midi_snd_free(struct snd_device
*device
)
498 static void f_midi_transmit_packet(struct usb_request
*req
, uint8_t p0
,
499 uint8_t p1
, uint8_t p2
, uint8_t p3
)
501 unsigned length
= req
->length
;
502 u8
*buf
= (u8
*)req
->buf
+ length
;
508 req
->length
= length
+ 4;
512 * Converts MIDI commands to USB MIDI packets.
514 static void f_midi_transmit_byte(struct usb_request
*req
,
515 struct gmidi_in_port
*port
, uint8_t b
)
517 uint8_t p0
= port
->cable
;
520 f_midi_transmit_packet(req
, p0
| 0x0f, b
, 0, 0);
521 } else if (b
>= 0xf0) {
525 port
->state
= STATE_SYSEX_1
;
530 port
->state
= STATE_1PARAM
;
534 port
->state
= STATE_2PARAM_1
;
538 port
->state
= STATE_UNKNOWN
;
541 f_midi_transmit_packet(req
, p0
| 0x05, 0xf6, 0, 0);
542 port
->state
= STATE_UNKNOWN
;
545 switch (port
->state
) {
547 f_midi_transmit_packet(req
,
548 p0
| 0x05, 0xf7, 0, 0);
551 f_midi_transmit_packet(req
,
552 p0
| 0x06, port
->data
[0], 0xf7, 0);
555 f_midi_transmit_packet(req
,
556 p0
| 0x07, port
->data
[0],
557 port
->data
[1], 0xf7);
560 port
->state
= STATE_UNKNOWN
;
563 } else if (b
>= 0x80) {
565 if (b
>= 0xc0 && b
<= 0xdf)
566 port
->state
= STATE_1PARAM
;
568 port
->state
= STATE_2PARAM_1
;
569 } else { /* b < 0x80 */
570 switch (port
->state
) {
572 if (port
->data
[0] < 0xf0) {
573 p0
|= port
->data
[0] >> 4;
576 port
->state
= STATE_UNKNOWN
;
578 f_midi_transmit_packet(req
, p0
, port
->data
[0], b
, 0);
582 port
->state
= STATE_2PARAM_2
;
585 if (port
->data
[0] < 0xf0) {
586 p0
|= port
->data
[0] >> 4;
587 port
->state
= STATE_2PARAM_1
;
590 port
->state
= STATE_UNKNOWN
;
592 f_midi_transmit_packet(req
,
593 p0
, port
->data
[0], port
->data
[1], b
);
597 port
->state
= STATE_SYSEX_1
;
601 port
->state
= STATE_SYSEX_2
;
604 f_midi_transmit_packet(req
,
605 p0
| 0x04, port
->data
[0], port
->data
[1], b
);
606 port
->state
= STATE_SYSEX_0
;
612 static void f_midi_transmit(struct f_midi
*midi
, struct usb_request
*req
)
614 struct usb_ep
*ep
= midi
->in_ep
;
615 struct gmidi_in_port
*port
= &midi
->in_port
;
621 req
= alloc_ep_req(ep
, midi
->buflen
);
624 ERROR(midi
, "gmidi_transmit: alloc_ep_request failed\n");
628 req
->complete
= f_midi_complete
;
631 while (req
->length
+ 3 < midi
->buflen
) {
633 if (snd_rawmidi_transmit(midi
->in_substream
, &b
, 1) != 1) {
637 f_midi_transmit_byte(req
, port
, b
);
642 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
644 free_ep_req(ep
, req
);
647 static void f_midi_in_tasklet(unsigned long data
)
649 struct f_midi
*midi
= (struct f_midi
*) data
;
650 f_midi_transmit(midi
, NULL
);
653 static int f_midi_in_open(struct snd_rawmidi_substream
*substream
)
655 struct f_midi
*midi
= substream
->rmidi
->private_data
;
657 VDBG(midi
, "%s()\n", __func__
);
658 midi
->in_substream
= substream
;
659 midi
->in_port
.state
= STATE_UNKNOWN
;
663 static int f_midi_in_close(struct snd_rawmidi_substream
*substream
)
665 struct f_midi
*midi
= substream
->rmidi
->private_data
;
667 VDBG(midi
, "%s()\n", __func__
);
671 static void f_midi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
673 struct f_midi
*midi
= substream
->rmidi
->private_data
;
675 VDBG(midi
, "%s() %d\n", __func__
, up
);
676 midi
->in_port
.active
= up
;
678 tasklet_hi_schedule(&midi
->tasklet
);
681 static int f_midi_out_open(struct snd_rawmidi_substream
*substream
)
683 struct f_midi
*midi
= substream
->rmidi
->private_data
;
685 VDBG(midi
, "%s()\n", __func__
);
686 midi
->out_substream
= substream
;
690 static int f_midi_out_close(struct snd_rawmidi_substream
*substream
)
692 struct f_midi
*midi
= substream
->rmidi
->private_data
;
694 VDBG(midi
, "%s()\n", __func__
);
698 static void f_midi_out_trigger(struct snd_rawmidi_substream
*substream
, int up
)
700 struct f_midi
*midi
= substream
->rmidi
->private_data
;
702 VDBG(midi
, "%s()\n", __func__
);
705 set_bit(substream
->number
, &midi
->out_triggered
);
707 clear_bit(substream
->number
, &midi
->out_triggered
);
710 static struct snd_rawmidi_ops gmidi_in_ops
= {
711 .open
= f_midi_in_open
,
712 .close
= f_midi_in_close
,
713 .trigger
= f_midi_in_trigger
,
716 static struct snd_rawmidi_ops gmidi_out_ops
= {
717 .open
= f_midi_out_open
,
718 .close
= f_midi_out_close
,
719 .trigger
= f_midi_out_trigger
722 /* register as a sound "card" */
723 static int f_midi_register_card(struct f_midi
*midi
)
725 struct snd_card
*card
;
726 struct snd_rawmidi
*rmidi
;
730 static struct snd_device_ops ops
= {
731 .dev_free
= f_midi_snd_free
,
734 err
= snd_card_create(midi
->index
, midi
->id
, THIS_MODULE
, 0, &card
);
736 ERROR(midi
, "snd_card_create() failed\n");
741 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, midi
, &ops
);
743 ERROR(midi
, "snd_device_new() failed: error %d\n", err
);
747 strcpy(card
->driver
, f_midi_longname
);
748 strcpy(card
->longname
, f_midi_longname
);
749 strcpy(card
->shortname
, f_midi_shortname
);
752 midi
->in_port
.midi
= midi
;
753 midi
->in_port
.active
= 0;
754 snd_component_add(card
, "MIDI");
755 err
= snd_rawmidi_new(card
, card
->longname
, 0,
756 out_ports
, in_ports
, &rmidi
);
758 ERROR(midi
, "snd_rawmidi_new() failed: error %d\n", err
);
762 strcpy(rmidi
->name
, card
->shortname
);
763 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
764 SNDRV_RAWMIDI_INFO_INPUT
|
765 SNDRV_RAWMIDI_INFO_DUPLEX
;
766 rmidi
->private_data
= midi
;
769 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
770 * It's an upside-down world being a gadget.
772 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &gmidi_in_ops
);
773 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &gmidi_out_ops
);
775 snd_card_set_dev(card
, &midi
->gadget
->dev
);
777 /* register it - we're ready to go */
778 err
= snd_card_register(card
);
780 ERROR(midi
, "snd_card_register() failed\n");
784 VDBG(midi
, "%s() finished ok\n", __func__
);
789 snd_card_free(midi
->card
);
795 /* MIDI function driver setup/binding */
798 f_midi_bind(struct usb_configuration
*c
, struct usb_function
*f
)
800 struct usb_composite_dev
*cdev
= c
->cdev
;
801 struct f_midi
*midi
= func_to_midi(f
);
804 /* maybe allocate device-global string ID */
805 if (midi_string_defs
[0].id
== 0) {
806 status
= usb_string_id(c
->cdev
);
809 midi_string_defs
[0].id
= status
;
812 /* We have two interfaces, AudioControl and MIDIStreaming */
813 status
= usb_interface_id(c
, f
);
816 ac_interface_desc
.bInterfaceNumber
= status
;
818 status
= usb_interface_id(c
, f
);
821 ms_interface_desc
.bInterfaceNumber
= status
;
822 ac_header_desc
.baInterfaceNr
[0] = status
;
826 /* allocate instance-specific endpoints */
827 midi
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_in_desc
);
830 midi
->in_ep
->driver_data
= cdev
; /* claim */
832 midi
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_out_desc
);
835 midi
->out_ep
->driver_data
= cdev
; /* claim */
838 * support all relevant hardware speeds... we expect that when
839 * hardware is dual speed, all bulk-capable endpoints work at
842 /* copy descriptors, and track endpoint copies */
843 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
845 bulk_in_desc
.wMaxPacketSize
= cpu_to_le16(512);
846 bulk_out_desc
.wMaxPacketSize
= cpu_to_le16(512);
847 f
->hs_descriptors
= usb_copy_descriptors(midi_function
);
849 f
->descriptors
= usb_copy_descriptors(midi_function
);
855 /* we might as well release our claims on endpoints */
857 midi
->out_ep
->driver_data
= NULL
;
859 midi
->in_ep
->driver_data
= NULL
;
861 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
867 * f_midi_bind_config - add USB MIDI function to a configuration
868 * @c: the configuration to supcard the USB audio function
869 * @index: the soundcard index to use for the ALSA device creation
870 * @id: the soundcard id to use for the ALSA device creation
871 * @buflen: the buffer length to use
872 * @qlen the number of read requests to pre-allocate
873 * Context: single threaded during gadget setup
875 * Returns zero on success, else negative errno.
877 int __init
f_midi_bind_config(struct usb_configuration
*c
,
885 /* allocate and initialize one new instance */
886 midi
= kzalloc(sizeof *midi
, GFP_KERNEL
);
892 midi
->gadget
= c
->cdev
->gadget
;
893 tasklet_init(&midi
->tasklet
, f_midi_in_tasklet
, (unsigned long) midi
);
895 /* set up ALSA midi devices */
896 status
= f_midi_register_card(midi
);
900 midi
->func
.name
= "gmidi function";
901 midi
->func
.strings
= midi_strings
;
902 midi
->func
.bind
= f_midi_bind
;
903 midi
->func
.unbind
= f_midi_unbind
;
904 midi
->func
.set_alt
= f_midi_set_alt
;
905 midi
->func
.disable
= f_midi_disable
;
907 midi
->id
= kstrdup(id
, GFP_KERNEL
);
909 midi
->buflen
= buflen
;
912 status
= usb_add_function(c
, &midi
->func
);