2 * f_midi.c -- USB MIDI class function driver
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
8 * Rewritten for the composite framework
9 * Copyright (C) 2011 Daniel Mack <zonque@gmail.com>
11 * Based on drivers/usb/gadget/f_audio.c,
12 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
13 * Copyright (C) 2008 Analog Devices, Inc
15 * and drivers/usb/gadget/midi.c,
16 * Copyright (C) 2006 Thumtronics Pty Ltd.
17 * Ben Williamson <ben.williamson@greyinnovation.com>
19 * Licensed under the GPL-2 or later.
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/device.h>
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/rawmidi.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <linux/usb/audio.h>
33 #include <linux/usb/midi.h>
35 MODULE_AUTHOR("Ben Williamson");
36 MODULE_LICENSE("GPL v2");
38 static const char f_midi_shortname
[] = "f_midi";
39 static const char f_midi_longname
[] = "MIDI Gadget";
42 * We can only handle 16 cables on one single endpoint, as cable numbers are
43 * stored in 4-bit fields. And as the interface currently only holds one
44 * single endpoint, this is the maximum number of ports we can allow.
49 * This is a gadget, and the IN/OUT naming is from the host's perspective.
50 * USB -> OUT endpoint -> rawmidi
51 * USB <- IN endpoint <- rawmidi
53 struct gmidi_in_port
{
58 #define STATE_UNKNOWN 0
59 #define STATE_1PARAM 1
60 #define STATE_2PARAM_1 2
61 #define STATE_2PARAM_2 3
62 #define STATE_SYSEX_0 4
63 #define STATE_SYSEX_1 5
64 #define STATE_SYSEX_2 6
69 struct usb_function func
;
70 struct usb_gadget
*gadget
;
71 struct usb_ep
*in_ep
, *out_ep
;
72 struct snd_card
*card
;
73 struct snd_rawmidi
*rmidi
;
75 struct snd_rawmidi_substream
*in_substream
[MAX_PORTS
];
76 struct snd_rawmidi_substream
*out_substream
[MAX_PORTS
];
77 struct gmidi_in_port
*in_port
[MAX_PORTS
];
79 unsigned long out_triggered
;
80 struct tasklet_struct tasklet
;
81 unsigned int in_ports
;
82 unsigned int out_ports
;
85 unsigned int buflen
, qlen
;
88 static inline struct f_midi
*func_to_midi(struct usb_function
*f
)
90 return container_of(f
, struct f_midi
, func
);
93 static void f_midi_transmit(struct f_midi
*midi
, struct usb_request
*req
);
95 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
96 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
97 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
99 /* B.3.1 Standard AC Interface Descriptor */
100 static struct usb_interface_descriptor ac_interface_desc __initdata
= {
101 .bLength
= USB_DT_INTERFACE_SIZE
,
102 .bDescriptorType
= USB_DT_INTERFACE
,
103 /* .bInterfaceNumber = DYNAMIC */
104 /* .bNumEndpoints = DYNAMIC */
105 .bInterfaceClass
= USB_CLASS_AUDIO
,
106 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
107 /* .iInterface = DYNAMIC */
110 /* B.3.2 Class-Specific AC Interface Descriptor */
111 static struct uac1_ac_header_descriptor_1 ac_header_desc __initdata
= {
112 .bLength
= UAC_DT_AC_HEADER_SIZE(1),
113 .bDescriptorType
= USB_DT_CS_INTERFACE
,
114 .bDescriptorSubtype
= USB_MS_HEADER
,
115 .bcdADC
= cpu_to_le16(0x0100),
116 .wTotalLength
= cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
118 /* .baInterfaceNr = DYNAMIC */
121 /* B.4.1 Standard MS Interface Descriptor */
122 static struct usb_interface_descriptor ms_interface_desc __initdata
= {
123 .bLength
= USB_DT_INTERFACE_SIZE
,
124 .bDescriptorType
= USB_DT_INTERFACE
,
125 /* .bInterfaceNumber = DYNAMIC */
127 .bInterfaceClass
= USB_CLASS_AUDIO
,
128 .bInterfaceSubClass
= USB_SUBCLASS_MIDISTREAMING
,
129 /* .iInterface = DYNAMIC */
132 /* B.4.2 Class-Specific MS Interface Descriptor */
133 static struct usb_ms_header_descriptor ms_header_desc __initdata
= {
134 .bLength
= USB_DT_MS_HEADER_SIZE
,
135 .bDescriptorType
= USB_DT_CS_INTERFACE
,
136 .bDescriptorSubtype
= USB_MS_HEADER
,
137 .bcdMSC
= cpu_to_le16(0x0100),
138 /* .wTotalLength = DYNAMIC */
141 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
142 static struct usb_endpoint_descriptor bulk_out_desc
= {
143 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
144 .bDescriptorType
= USB_DT_ENDPOINT
,
145 .bEndpointAddress
= USB_DIR_OUT
,
146 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
149 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
150 static struct usb_ms_endpoint_descriptor_16 ms_out_desc
= {
151 /* .bLength = DYNAMIC */
152 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
153 .bDescriptorSubtype
= USB_MS_GENERAL
,
154 /* .bNumEmbMIDIJack = DYNAMIC */
155 /* .baAssocJackID = DYNAMIC */
158 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
159 static struct usb_endpoint_descriptor bulk_in_desc
= {
160 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
161 .bDescriptorType
= USB_DT_ENDPOINT
,
162 .bEndpointAddress
= USB_DIR_IN
,
163 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
166 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
167 static struct usb_ms_endpoint_descriptor_16 ms_in_desc
= {
168 /* .bLength = DYNAMIC */
169 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
170 .bDescriptorSubtype
= USB_MS_GENERAL
,
171 /* .bNumEmbMIDIJack = DYNAMIC */
172 /* .baAssocJackID = DYNAMIC */
175 /* string IDs are assigned dynamically */
177 #define STRING_FUNC_IDX 0
179 static struct usb_string midi_string_defs
[] = {
180 [STRING_FUNC_IDX
].s
= "MIDI function",
181 { } /* end of list */
184 static struct usb_gadget_strings midi_stringtab
= {
185 .language
= 0x0409, /* en-us */
186 .strings
= midi_string_defs
,
189 static struct usb_gadget_strings
*midi_strings
[] = {
194 static struct usb_request
*alloc_ep_req(struct usb_ep
*ep
, unsigned length
)
196 struct usb_request
*req
;
198 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
200 req
->length
= length
;
201 req
->buf
= kmalloc(length
, GFP_ATOMIC
);
203 usb_ep_free_request(ep
, req
);
210 static void free_ep_req(struct usb_ep
*ep
, struct usb_request
*req
)
213 usb_ep_free_request(ep
, req
);
216 static const uint8_t f_midi_cin_length
[] = {
217 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
221 * Receives a chunk of MIDI data.
223 static void f_midi_read_data(struct usb_ep
*ep
, int cable
,
224 uint8_t *data
, int length
)
226 struct f_midi
*midi
= ep
->driver_data
;
227 struct snd_rawmidi_substream
*substream
= midi
->out_substream
[cable
];
230 /* Nobody is listening - throw it on the floor. */
233 if (!test_bit(cable
, &midi
->out_triggered
))
236 snd_rawmidi_receive(substream
, data
, length
);
239 static void f_midi_handle_out_data(struct usb_ep
*ep
, struct usb_request
*req
)
244 for (i
= 0; i
+ 3 < req
->actual
; i
+= 4)
246 int cable
= buf
[i
] >> 4;
247 int length
= f_midi_cin_length
[buf
[i
] & 0x0f];
248 f_midi_read_data(ep
, cable
, &buf
[i
+ 1], length
);
253 f_midi_complete(struct usb_ep
*ep
, struct usb_request
*req
)
255 struct f_midi
*midi
= ep
->driver_data
;
256 struct usb_composite_dev
*cdev
= midi
->func
.config
->cdev
;
257 int status
= req
->status
;
260 case 0: /* normal completion */
261 if (ep
== midi
->out_ep
) {
262 /* We received stuff. req is queued again, below */
263 f_midi_handle_out_data(ep
, req
);
264 } else if (ep
== midi
->in_ep
) {
265 /* Our transmit completed. See if there's more to go.
266 * f_midi_transmit eats req, don't queue it again. */
267 f_midi_transmit(midi
, req
);
272 /* this endpoint is normally active while we're configured */
273 case -ECONNABORTED
: /* hardware forced ep reset */
274 case -ECONNRESET
: /* request dequeued */
275 case -ESHUTDOWN
: /* disconnect from host */
276 VDBG(cdev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
277 req
->actual
, req
->length
);
278 if (ep
== midi
->out_ep
)
279 f_midi_handle_out_data(ep
, req
);
281 free_ep_req(ep
, req
);
284 case -EOVERFLOW
: /* buffer overrun on read means that
285 * we didn't provide a big enough buffer.
288 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
289 status
, req
->actual
, req
->length
);
291 case -EREMOTEIO
: /* short read */
295 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
297 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
298 ep
->name
, req
->length
, status
);
300 /* FIXME recover later ... somehow */
304 static int f_midi_start_ep(struct f_midi
*midi
,
305 struct usb_function
*f
,
309 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
314 err
= config_ep_by_speed(midi
->gadget
, f
, ep
);
316 ERROR(cdev
, "can't configure %s: %d\n", ep
->name
, err
);
320 err
= usb_ep_enable(ep
);
322 ERROR(cdev
, "can't start %s: %d\n", ep
->name
, err
);
326 ep
->driver_data
= midi
;
331 static int f_midi_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
333 struct f_midi
*midi
= func_to_midi(f
);
334 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
338 err
= f_midi_start_ep(midi
, f
, midi
->in_ep
);
342 err
= f_midi_start_ep(midi
, f
, midi
->out_ep
);
346 if (midi
->out_ep
->driver_data
)
347 usb_ep_disable(midi
->out_ep
);
349 err
= config_ep_by_speed(midi
->gadget
, f
, midi
->out_ep
);
351 ERROR(cdev
, "can't configure %s: %d\n",
352 midi
->out_ep
->name
, err
);
356 err
= usb_ep_enable(midi
->out_ep
);
358 ERROR(cdev
, "can't start %s: %d\n",
359 midi
->out_ep
->name
, err
);
363 midi
->out_ep
->driver_data
= midi
;
365 /* allocate a bunch of read buffers and queue them all at once. */
366 for (i
= 0; i
< midi
->qlen
&& err
== 0; i
++) {
367 struct usb_request
*req
=
368 alloc_ep_req(midi
->out_ep
, midi
->buflen
);
372 req
->complete
= f_midi_complete
;
373 err
= usb_ep_queue(midi
->out_ep
, req
, GFP_ATOMIC
);
375 ERROR(midi
, "%s queue req: %d\n",
376 midi
->out_ep
->name
, err
);
383 static void f_midi_disable(struct usb_function
*f
)
385 struct f_midi
*midi
= func_to_midi(f
);
386 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
388 DBG(cdev
, "disable\n");
391 * just disable endpoints, forcing completion of pending i/o.
392 * all our completion handlers free their requests in this case.
394 usb_ep_disable(midi
->in_ep
);
395 usb_ep_disable(midi
->out_ep
);
398 static void f_midi_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
400 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
401 struct f_midi
*midi
= func_to_midi(f
);
402 struct snd_card
*card
;
404 DBG(cdev
, "unbind\n");
406 /* just to be sure */
417 usb_free_all_descriptors(f
);
421 static int f_midi_snd_free(struct snd_device
*device
)
426 static void f_midi_transmit_packet(struct usb_request
*req
, uint8_t p0
,
427 uint8_t p1
, uint8_t p2
, uint8_t p3
)
429 unsigned length
= req
->length
;
430 u8
*buf
= (u8
*)req
->buf
+ length
;
436 req
->length
= length
+ 4;
440 * Converts MIDI commands to USB MIDI packets.
442 static void f_midi_transmit_byte(struct usb_request
*req
,
443 struct gmidi_in_port
*port
, uint8_t b
)
445 uint8_t p0
= port
->cable
<< 4;
448 f_midi_transmit_packet(req
, p0
| 0x0f, b
, 0, 0);
449 } else if (b
>= 0xf0) {
453 port
->state
= STATE_SYSEX_1
;
458 port
->state
= STATE_1PARAM
;
462 port
->state
= STATE_2PARAM_1
;
466 port
->state
= STATE_UNKNOWN
;
469 f_midi_transmit_packet(req
, p0
| 0x05, 0xf6, 0, 0);
470 port
->state
= STATE_UNKNOWN
;
473 switch (port
->state
) {
475 f_midi_transmit_packet(req
,
476 p0
| 0x05, 0xf7, 0, 0);
479 f_midi_transmit_packet(req
,
480 p0
| 0x06, port
->data
[0], 0xf7, 0);
483 f_midi_transmit_packet(req
,
484 p0
| 0x07, port
->data
[0],
485 port
->data
[1], 0xf7);
488 port
->state
= STATE_UNKNOWN
;
491 } else if (b
>= 0x80) {
493 if (b
>= 0xc0 && b
<= 0xdf)
494 port
->state
= STATE_1PARAM
;
496 port
->state
= STATE_2PARAM_1
;
497 } else { /* b < 0x80 */
498 switch (port
->state
) {
500 if (port
->data
[0] < 0xf0) {
501 p0
|= port
->data
[0] >> 4;
504 port
->state
= STATE_UNKNOWN
;
506 f_midi_transmit_packet(req
, p0
, port
->data
[0], b
, 0);
510 port
->state
= STATE_2PARAM_2
;
513 if (port
->data
[0] < 0xf0) {
514 p0
|= port
->data
[0] >> 4;
515 port
->state
= STATE_2PARAM_1
;
518 port
->state
= STATE_UNKNOWN
;
520 f_midi_transmit_packet(req
,
521 p0
, port
->data
[0], port
->data
[1], b
);
525 port
->state
= STATE_SYSEX_1
;
529 port
->state
= STATE_SYSEX_2
;
532 f_midi_transmit_packet(req
,
533 p0
| 0x04, port
->data
[0], port
->data
[1], b
);
534 port
->state
= STATE_SYSEX_0
;
540 static void f_midi_transmit(struct f_midi
*midi
, struct usb_request
*req
)
542 struct usb_ep
*ep
= midi
->in_ep
;
549 req
= alloc_ep_req(ep
, midi
->buflen
);
552 ERROR(midi
, "gmidi_transmit: alloc_ep_request failed\n");
556 req
->complete
= f_midi_complete
;
558 for (i
= 0; i
< MAX_PORTS
; i
++) {
559 struct gmidi_in_port
*port
= midi
->in_port
[i
];
560 struct snd_rawmidi_substream
*substream
= midi
->in_substream
[i
];
562 if (!port
|| !port
->active
|| !substream
)
565 while (req
->length
+ 3 < midi
->buflen
) {
567 if (snd_rawmidi_transmit(substream
, &b
, 1) != 1) {
571 f_midi_transmit_byte(req
, port
, b
);
576 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
578 free_ep_req(ep
, req
);
581 static void f_midi_in_tasklet(unsigned long data
)
583 struct f_midi
*midi
= (struct f_midi
*) data
;
584 f_midi_transmit(midi
, NULL
);
587 static int f_midi_in_open(struct snd_rawmidi_substream
*substream
)
589 struct f_midi
*midi
= substream
->rmidi
->private_data
;
591 if (!midi
->in_port
[substream
->number
])
594 VDBG(midi
, "%s()\n", __func__
);
595 midi
->in_substream
[substream
->number
] = substream
;
596 midi
->in_port
[substream
->number
]->state
= STATE_UNKNOWN
;
600 static int f_midi_in_close(struct snd_rawmidi_substream
*substream
)
602 struct f_midi
*midi
= substream
->rmidi
->private_data
;
604 VDBG(midi
, "%s()\n", __func__
);
608 static void f_midi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
610 struct f_midi
*midi
= substream
->rmidi
->private_data
;
612 if (!midi
->in_port
[substream
->number
])
615 VDBG(midi
, "%s() %d\n", __func__
, up
);
616 midi
->in_port
[substream
->number
]->active
= up
;
618 tasklet_hi_schedule(&midi
->tasklet
);
621 static int f_midi_out_open(struct snd_rawmidi_substream
*substream
)
623 struct f_midi
*midi
= substream
->rmidi
->private_data
;
625 if (substream
->number
>= MAX_PORTS
)
628 VDBG(midi
, "%s()\n", __func__
);
629 midi
->out_substream
[substream
->number
] = substream
;
633 static int f_midi_out_close(struct snd_rawmidi_substream
*substream
)
635 struct f_midi
*midi
= substream
->rmidi
->private_data
;
637 VDBG(midi
, "%s()\n", __func__
);
641 static void f_midi_out_trigger(struct snd_rawmidi_substream
*substream
, int up
)
643 struct f_midi
*midi
= substream
->rmidi
->private_data
;
645 VDBG(midi
, "%s()\n", __func__
);
648 set_bit(substream
->number
, &midi
->out_triggered
);
650 clear_bit(substream
->number
, &midi
->out_triggered
);
653 static struct snd_rawmidi_ops gmidi_in_ops
= {
654 .open
= f_midi_in_open
,
655 .close
= f_midi_in_close
,
656 .trigger
= f_midi_in_trigger
,
659 static struct snd_rawmidi_ops gmidi_out_ops
= {
660 .open
= f_midi_out_open
,
661 .close
= f_midi_out_close
,
662 .trigger
= f_midi_out_trigger
665 /* register as a sound "card" */
666 static int f_midi_register_card(struct f_midi
*midi
)
668 struct snd_card
*card
;
669 struct snd_rawmidi
*rmidi
;
671 static struct snd_device_ops ops
= {
672 .dev_free
= f_midi_snd_free
,
675 err
= snd_card_create(midi
->index
, midi
->id
, THIS_MODULE
, 0, &card
);
677 ERROR(midi
, "snd_card_create() failed\n");
682 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, midi
, &ops
);
684 ERROR(midi
, "snd_device_new() failed: error %d\n", err
);
688 strcpy(card
->driver
, f_midi_longname
);
689 strcpy(card
->longname
, f_midi_longname
);
690 strcpy(card
->shortname
, f_midi_shortname
);
693 snd_component_add(card
, "MIDI");
694 err
= snd_rawmidi_new(card
, card
->longname
, 0,
695 midi
->out_ports
, midi
->in_ports
, &rmidi
);
697 ERROR(midi
, "snd_rawmidi_new() failed: error %d\n", err
);
701 strcpy(rmidi
->name
, card
->shortname
);
702 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
703 SNDRV_RAWMIDI_INFO_INPUT
|
704 SNDRV_RAWMIDI_INFO_DUPLEX
;
705 rmidi
->private_data
= midi
;
708 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
709 * It's an upside-down world being a gadget.
711 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &gmidi_in_ops
);
712 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &gmidi_out_ops
);
714 snd_card_set_dev(card
, &midi
->gadget
->dev
);
716 /* register it - we're ready to go */
717 err
= snd_card_register(card
);
719 ERROR(midi
, "snd_card_register() failed\n");
723 VDBG(midi
, "%s() finished ok\n", __func__
);
728 snd_card_free(midi
->card
);
734 /* MIDI function driver setup/binding */
737 f_midi_bind(struct usb_configuration
*c
, struct usb_function
*f
)
739 struct usb_descriptor_header
**midi_function
;
740 struct usb_midi_in_jack_descriptor jack_in_ext_desc
[MAX_PORTS
];
741 struct usb_midi_in_jack_descriptor jack_in_emb_desc
[MAX_PORTS
];
742 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc
[MAX_PORTS
];
743 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc
[MAX_PORTS
];
744 struct usb_composite_dev
*cdev
= c
->cdev
;
745 struct f_midi
*midi
= func_to_midi(f
);
746 int status
, n
, jack
= 1, i
= 0;
748 /* maybe allocate device-global string ID */
749 if (midi_string_defs
[0].id
== 0) {
750 status
= usb_string_id(c
->cdev
);
753 midi_string_defs
[0].id
= status
;
756 /* We have two interfaces, AudioControl and MIDIStreaming */
757 status
= usb_interface_id(c
, f
);
760 ac_interface_desc
.bInterfaceNumber
= status
;
762 status
= usb_interface_id(c
, f
);
765 ms_interface_desc
.bInterfaceNumber
= status
;
766 ac_header_desc
.baInterfaceNr
[0] = status
;
770 /* allocate instance-specific endpoints */
771 midi
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_in_desc
);
774 midi
->in_ep
->driver_data
= cdev
; /* claim */
776 midi
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_out_desc
);
779 midi
->out_ep
->driver_data
= cdev
; /* claim */
781 /* allocate temporary function list */
782 midi_function
= kcalloc((MAX_PORTS
* 4) + 9, sizeof(*midi_function
),
784 if (!midi_function
) {
790 * construct the function's descriptor set. As the number of
791 * input and output MIDI ports is configurable, we have to do
795 /* add the headers - these are always the same */
796 midi_function
[i
++] = (struct usb_descriptor_header
*) &ac_interface_desc
;
797 midi_function
[i
++] = (struct usb_descriptor_header
*) &ac_header_desc
;
798 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_interface_desc
;
800 /* calculate the header's wTotalLength */
801 n
= USB_DT_MS_HEADER_SIZE
802 + (midi
->in_ports
+ midi
->out_ports
) *
803 (USB_DT_MIDI_IN_SIZE
+ USB_DT_MIDI_OUT_SIZE(1));
804 ms_header_desc
.wTotalLength
= cpu_to_le16(n
);
806 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_header_desc
;
808 /* configure the external IN jacks, each linked to an embedded OUT jack */
809 for (n
= 0; n
< midi
->in_ports
; n
++) {
810 struct usb_midi_in_jack_descriptor
*in_ext
= &jack_in_ext_desc
[n
];
811 struct usb_midi_out_jack_descriptor_1
*out_emb
= &jack_out_emb_desc
[n
];
813 in_ext
->bLength
= USB_DT_MIDI_IN_SIZE
;
814 in_ext
->bDescriptorType
= USB_DT_CS_INTERFACE
;
815 in_ext
->bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
;
816 in_ext
->bJackType
= USB_MS_EXTERNAL
;
817 in_ext
->bJackID
= jack
++;
819 midi_function
[i
++] = (struct usb_descriptor_header
*) in_ext
;
821 out_emb
->bLength
= USB_DT_MIDI_OUT_SIZE(1);
822 out_emb
->bDescriptorType
= USB_DT_CS_INTERFACE
;
823 out_emb
->bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
;
824 out_emb
->bJackType
= USB_MS_EMBEDDED
;
825 out_emb
->bJackID
= jack
++;
826 out_emb
->bNrInputPins
= 1;
827 out_emb
->pins
[0].baSourcePin
= 1;
828 out_emb
->pins
[0].baSourceID
= in_ext
->bJackID
;
830 midi_function
[i
++] = (struct usb_descriptor_header
*) out_emb
;
832 /* link it to the endpoint */
833 ms_in_desc
.baAssocJackID
[n
] = out_emb
->bJackID
;
836 /* configure the external OUT jacks, each linked to an embedded IN jack */
837 for (n
= 0; n
< midi
->out_ports
; n
++) {
838 struct usb_midi_in_jack_descriptor
*in_emb
= &jack_in_emb_desc
[n
];
839 struct usb_midi_out_jack_descriptor_1
*out_ext
= &jack_out_ext_desc
[n
];
841 in_emb
->bLength
= USB_DT_MIDI_IN_SIZE
;
842 in_emb
->bDescriptorType
= USB_DT_CS_INTERFACE
;
843 in_emb
->bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
;
844 in_emb
->bJackType
= USB_MS_EMBEDDED
;
845 in_emb
->bJackID
= jack
++;
847 midi_function
[i
++] = (struct usb_descriptor_header
*) in_emb
;
849 out_ext
->bLength
= USB_DT_MIDI_OUT_SIZE(1);
850 out_ext
->bDescriptorType
= USB_DT_CS_INTERFACE
;
851 out_ext
->bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
;
852 out_ext
->bJackType
= USB_MS_EXTERNAL
;
853 out_ext
->bJackID
= jack
++;
854 out_ext
->bNrInputPins
= 1;
856 out_ext
->pins
[0].baSourceID
= in_emb
->bJackID
;
857 out_ext
->pins
[0].baSourcePin
= 1;
858 midi_function
[i
++] = (struct usb_descriptor_header
*) out_ext
;
860 /* link it to the endpoint */
861 ms_out_desc
.baAssocJackID
[n
] = in_emb
->bJackID
;
864 /* configure the endpoint descriptors ... */
865 ms_out_desc
.bLength
= USB_DT_MS_ENDPOINT_SIZE(midi
->in_ports
);
866 ms_out_desc
.bNumEmbMIDIJack
= midi
->in_ports
;
868 ms_in_desc
.bLength
= USB_DT_MS_ENDPOINT_SIZE(midi
->out_ports
);
869 ms_in_desc
.bNumEmbMIDIJack
= midi
->out_ports
;
871 /* ... and add them to the list */
872 midi_function
[i
++] = (struct usb_descriptor_header
*) &bulk_out_desc
;
873 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_out_desc
;
874 midi_function
[i
++] = (struct usb_descriptor_header
*) &bulk_in_desc
;
875 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_in_desc
;
876 midi_function
[i
++] = NULL
;
879 * support all relevant hardware speeds... we expect that when
880 * hardware is dual speed, all bulk-capable endpoints work at
883 /* copy descriptors, and track endpoint copies */
884 f
->fs_descriptors
= usb_copy_descriptors(midi_function
);
885 if (!f
->fs_descriptors
)
888 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
889 bulk_in_desc
.wMaxPacketSize
= cpu_to_le16(512);
890 bulk_out_desc
.wMaxPacketSize
= cpu_to_le16(512);
891 f
->hs_descriptors
= usb_copy_descriptors(midi_function
);
892 if (!f
->hs_descriptors
)
896 kfree(midi_function
);
901 kfree(midi_function
);
902 usb_free_descriptors(f
->hs_descriptors
);
904 /* we might as well release our claims on endpoints */
906 midi
->out_ep
->driver_data
= NULL
;
908 midi
->in_ep
->driver_data
= NULL
;
910 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
916 * f_midi_bind_config - add USB MIDI function to a configuration
917 * @c: the configuration to supcard the USB audio function
918 * @index: the soundcard index to use for the ALSA device creation
919 * @id: the soundcard id to use for the ALSA device creation
920 * @buflen: the buffer length to use
921 * @qlen the number of read requests to pre-allocate
922 * Context: single threaded during gadget setup
924 * Returns zero on success, else negative errno.
926 int __init
f_midi_bind_config(struct usb_configuration
*c
,
928 unsigned int in_ports
,
929 unsigned int out_ports
,
937 if (in_ports
> MAX_PORTS
|| out_ports
> MAX_PORTS
)
940 /* allocate and initialize one new instance */
941 midi
= kzalloc(sizeof *midi
, GFP_KERNEL
);
947 for (i
= 0; i
< in_ports
; i
++) {
948 struct gmidi_in_port
*port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
957 midi
->in_port
[i
] = port
;
960 midi
->gadget
= c
->cdev
->gadget
;
961 tasklet_init(&midi
->tasklet
, f_midi_in_tasklet
, (unsigned long) midi
);
963 /* set up ALSA midi devices */
964 midi
->in_ports
= in_ports
;
965 midi
->out_ports
= out_ports
;
966 status
= f_midi_register_card(midi
);
970 midi
->func
.name
= "gmidi function";
971 midi
->func
.strings
= midi_strings
;
972 midi
->func
.bind
= f_midi_bind
;
973 midi
->func
.unbind
= f_midi_unbind
;
974 midi
->func
.set_alt
= f_midi_set_alt
;
975 midi
->func
.disable
= f_midi_disable
;
977 midi
->id
= kstrdup(id
, GFP_KERNEL
);
979 midi
->buflen
= buflen
;
982 status
= usb_add_function(c
, &midi
->func
);
989 for (--i
; i
>= 0; i
--)
990 kfree(midi
->in_port
[i
]);