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>
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.
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
{
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
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
;
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)),
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 */
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
[] = {
196 static inline struct usb_request
*midi_alloc_ep_req(struct usb_ep
*ep
,
199 return alloc_ep_req(ep
, length
, length
);
202 static void free_ep_req(struct usb_ep
*ep
, struct usb_request
*req
)
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
];
222 /* Nobody is listening - throw it on the floor. */
225 if (!test_bit(cable
, &midi
->out_triggered
))
228 snd_rawmidi_receive(substream
, data
, length
);
231 static void f_midi_handle_out_data(struct usb_ep
*ep
, struct usb_request
*req
)
236 for (i
= 0; i
+ 3 < req
->actual
; i
+= 4)
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
);
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
;
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
);
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
);
276 case -EOVERFLOW
: /* buffer overrun on read means that
277 * we didn't provide a big enough buffer.
280 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
281 status
, req
->actual
, req
->length
);
283 case -EREMOTEIO
: /* short read */
287 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
289 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
290 ep
->name
, req
->length
, status
);
292 /* FIXME recover later ... somehow */
296 static int f_midi_start_ep(struct f_midi
*midi
,
297 struct usb_function
*f
,
301 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
306 err
= config_ep_by_speed(midi
->gadget
, f
, ep
);
308 ERROR(cdev
, "can't configure %s: %d\n", ep
->name
, err
);
312 err
= usb_ep_enable(ep
);
314 ERROR(cdev
, "can't start %s: %d\n", ep
->name
, err
);
318 ep
->driver_data
= midi
;
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
;
330 err
= f_midi_start_ep(midi
, f
, midi
->in_ep
);
334 err
= f_midi_start_ep(midi
, f
, midi
->out_ep
);
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
);
343 ERROR(cdev
, "can't configure %s: %d\n",
344 midi
->out_ep
->name
, err
);
348 err
= usb_ep_enable(midi
->out_ep
);
350 ERROR(cdev
, "can't start %s: %d\n",
351 midi
->out_ep
->name
, 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
);
364 req
->complete
= f_midi_complete
;
365 err
= usb_ep_queue(midi
->out_ep
, req
, GFP_ATOMIC
);
367 ERROR(midi
, "%s queue req: %d\n",
368 midi
->out_ep
->name
, err
);
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 */
409 usb_free_all_descriptors(f
);
413 static int f_midi_snd_free(struct snd_device
*device
)
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
;
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;
440 f_midi_transmit_packet(req
, p0
| 0x0f, b
, 0, 0);
441 } else if (b
>= 0xf0) {
445 port
->state
= STATE_SYSEX_1
;
450 port
->state
= STATE_1PARAM
;
454 port
->state
= STATE_2PARAM_1
;
458 port
->state
= STATE_UNKNOWN
;
461 f_midi_transmit_packet(req
, p0
| 0x05, 0xf6, 0, 0);
462 port
->state
= STATE_UNKNOWN
;
465 switch (port
->state
) {
467 f_midi_transmit_packet(req
,
468 p0
| 0x05, 0xf7, 0, 0);
471 f_midi_transmit_packet(req
,
472 p0
| 0x06, port
->data
[0], 0xf7, 0);
475 f_midi_transmit_packet(req
,
476 p0
| 0x07, port
->data
[0],
477 port
->data
[1], 0xf7);
480 port
->state
= STATE_UNKNOWN
;
483 } else if (b
>= 0x80) {
485 if (b
>= 0xc0 && b
<= 0xdf)
486 port
->state
= STATE_1PARAM
;
488 port
->state
= STATE_2PARAM_1
;
489 } else { /* b < 0x80 */
490 switch (port
->state
) {
492 if (port
->data
[0] < 0xf0) {
493 p0
|= port
->data
[0] >> 4;
496 port
->state
= STATE_UNKNOWN
;
498 f_midi_transmit_packet(req
, p0
, port
->data
[0], b
, 0);
502 port
->state
= STATE_2PARAM_2
;
505 if (port
->data
[0] < 0xf0) {
506 p0
|= port
->data
[0] >> 4;
507 port
->state
= STATE_2PARAM_1
;
510 port
->state
= STATE_UNKNOWN
;
512 f_midi_transmit_packet(req
,
513 p0
, port
->data
[0], port
->data
[1], b
);
517 port
->state
= STATE_SYSEX_1
;
521 port
->state
= 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
;
532 static void f_midi_transmit(struct f_midi
*midi
, struct usb_request
*req
)
534 struct usb_ep
*ep
= midi
->in_ep
;
541 req
= midi_alloc_ep_req(ep
, midi
->buflen
);
544 ERROR(midi
, "gmidi_transmit: alloc_ep_request failed\n");
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
)
557 while (req
->length
+ 3 < midi
->buflen
) {
559 if (snd_rawmidi_transmit(substream
, &b
, 1) != 1) {
563 f_midi_transmit_byte(req
, port
, b
);
568 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
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
])
586 VDBG(midi
, "%s()\n", __func__
);
587 midi
->in_substream
[substream
->number
] = substream
;
588 midi
->in_port
[substream
->number
]->state
= STATE_UNKNOWN
;
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__
);
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
])
607 VDBG(midi
, "%s() %d\n", __func__
, up
);
608 midi
->in_port
[substream
->number
]->active
= 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
)
620 VDBG(midi
, "%s()\n", __func__
);
621 midi
->out_substream
[substream
->number
] = substream
;
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__
);
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__
);
640 set_bit(substream
->number
, &midi
->out_triggered
);
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
;
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
);
669 ERROR(midi
, "snd_card_create() failed\n");
674 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, midi
, &ops
);
676 ERROR(midi
, "snd_device_new() failed: error %d\n", err
);
680 strcpy(card
->driver
, f_midi_longname
);
681 strcpy(card
->longname
, f_midi_longname
);
682 strcpy(card
->shortname
, f_midi_shortname
);
685 snd_component_add(card
, "MIDI");
686 err
= snd_rawmidi_new(card
, card
->longname
, 0,
687 midi
->out_ports
, midi
->in_ports
, &rmidi
);
689 ERROR(midi
, "snd_rawmidi_new() failed: error %d\n", err
);
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
);
711 ERROR(midi
, "snd_card_register() failed\n");
715 VDBG(midi
, "%s() finished ok\n", __func__
);
720 snd_card_free(midi
->card
);
726 /* MIDI function driver setup/binding */
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
);
745 midi_string_defs
[0].id
= status
;
748 /* We have two interfaces, AudioControl and MIDIStreaming */
749 status
= usb_interface_id(c
, f
);
752 ac_interface_desc
.bInterfaceNumber
= status
;
754 status
= usb_interface_id(c
, f
);
757 ms_interface_desc
.bInterfaceNumber
= status
;
758 ac_header_desc
.baInterfaceNr
[0] = status
;
762 /* allocate instance-specific endpoints */
763 midi
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_in_desc
);
766 midi
->in_ep
->driver_data
= cdev
; /* claim */
768 midi
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_out_desc
);
771 midi
->out_ep
->driver_data
= cdev
; /* claim */
773 /* allocate temporary function list */
774 midi_function
= kcalloc((MAX_PORTS
* 4) + 9, sizeof(*midi_function
),
776 if (!midi_function
) {
782 * construct the function's descriptor set. As the number of
783 * input and output MIDI ports is configurable, we have to do
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
++;
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
;
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
++;
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;
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
875 /* copy descriptors, and track endpoint copies */
876 f
->fs_descriptors
= usb_copy_descriptors(midi_function
);
877 if (!f
->fs_descriptors
)
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
)
888 kfree(midi_function
);
893 kfree(midi_function
);
894 usb_free_descriptors(f
->hs_descriptors
);
896 /* we might as well release our claims on endpoints */
898 midi
->out_ep
->driver_data
= NULL
;
900 midi
->in_ep
->driver_data
= NULL
;
902 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, 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
,
920 unsigned int in_ports
,
921 unsigned int out_ports
,
929 if (in_ports
> MAX_PORTS
|| out_ports
> MAX_PORTS
)
932 /* allocate and initialize one new instance */
933 midi
= kzalloc(sizeof *midi
, GFP_KERNEL
);
939 for (i
= 0; i
< in_ports
; i
++) {
940 struct gmidi_in_port
*port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
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
);
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
);
971 midi
->buflen
= buflen
;
974 status
= usb_add_function(c
, &midi
->func
);
981 for (--i
; i
>= 0; i
--)
982 kfree(midi
->in_port
[i
]);