1 // SPDX-License-Identifier: GPL-2.0+
3 * f_midi.c -- USB MIDI class function driver
5 * Copyright (C) 2006 Thumtronics Pty Ltd.
6 * Developed for Thumtronics by Grey Innovation
7 * Ben Williamson <ben.williamson@greyinnovation.com>
9 * Rewritten for the composite framework
10 * Copyright (C) 2011 Daniel Mack <zonque@gmail.com>
12 * Based on drivers/usb/gadget/f_audio.c,
13 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
14 * Copyright (C) 2008 Analog Devices, Inc
16 * and drivers/usb/gadget/midi.c,
17 * Copyright (C) 2006 Thumtronics Pty Ltd.
18 * Ben Williamson <ben.williamson@greyinnovation.com>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/device.h>
25 #include <linux/kfifo.h>
26 #include <linux/spinlock.h>
28 #include <sound/core.h>
29 #include <sound/initval.h>
30 #include <sound/rawmidi.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/audio.h>
35 #include <linux/usb/midi.h>
40 MODULE_AUTHOR("Ben Williamson");
41 MODULE_LICENSE("GPL v2");
43 static const char f_midi_shortname
[] = "f_midi";
44 static const char f_midi_longname
[] = "MIDI Gadget";
47 * We can only handle 16 cables on one single endpoint, as cable numbers are
48 * stored in 4-bit fields. And as the interface currently only holds one
49 * single endpoint, this is the maximum number of ports we can allow.
53 /* MIDI message states */
55 STATE_INITIAL
= 0, /* pseudo state */
63 STATE_FINISHED
, /* pseudo state */
67 * This is a gadget, and the IN/OUT naming is from the host's perspective.
68 * USB -> OUT endpoint -> rawmidi
69 * USB <- IN endpoint <- rawmidi
71 struct gmidi_in_port
{
72 struct snd_rawmidi_substream
*substream
;
80 struct usb_function func
;
81 struct usb_gadget
*gadget
;
82 struct usb_ep
*in_ep
, *out_ep
;
83 struct snd_card
*card
;
84 struct snd_rawmidi
*rmidi
;
87 struct snd_rawmidi_substream
*out_substream
[MAX_PORTS
];
89 unsigned long out_triggered
;
90 struct tasklet_struct tasklet
;
91 unsigned int in_ports
;
92 unsigned int out_ports
;
95 unsigned int buflen
, qlen
;
96 /* This fifo is used as a buffer ring for pre-allocated IN usb_requests */
97 DECLARE_KFIFO_PTR(in_req_fifo
, struct usb_request
*);
98 spinlock_t transmit_lock
;
99 unsigned int in_last_port
;
100 unsigned char free_ref
;
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
);
111 static void f_midi_rmidi_free(struct snd_rawmidi
*rmidi
);
112 static void f_midi_free_inst(struct usb_function_instance
*f
);
114 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
115 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
116 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
118 /* B.3.1 Standard AC Interface Descriptor */
119 static struct usb_interface_descriptor ac_interface_desc
= {
120 .bLength
= USB_DT_INTERFACE_SIZE
,
121 .bDescriptorType
= USB_DT_INTERFACE
,
122 /* .bInterfaceNumber = DYNAMIC */
123 /* .bNumEndpoints = DYNAMIC */
124 .bInterfaceClass
= USB_CLASS_AUDIO
,
125 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
126 /* .iInterface = DYNAMIC */
129 /* B.3.2 Class-Specific AC Interface Descriptor */
130 static struct uac1_ac_header_descriptor_1 ac_header_desc
= {
131 .bLength
= UAC_DT_AC_HEADER_SIZE(1),
132 .bDescriptorType
= USB_DT_CS_INTERFACE
,
133 .bDescriptorSubtype
= USB_MS_HEADER
,
134 .bcdADC
= cpu_to_le16(0x0100),
135 .wTotalLength
= cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
137 /* .baInterfaceNr = DYNAMIC */
140 /* B.4.1 Standard MS Interface Descriptor */
141 static struct usb_interface_descriptor ms_interface_desc
= {
142 .bLength
= USB_DT_INTERFACE_SIZE
,
143 .bDescriptorType
= USB_DT_INTERFACE
,
144 /* .bInterfaceNumber = DYNAMIC */
146 .bInterfaceClass
= USB_CLASS_AUDIO
,
147 .bInterfaceSubClass
= USB_SUBCLASS_MIDISTREAMING
,
148 /* .iInterface = DYNAMIC */
151 /* B.4.2 Class-Specific MS Interface Descriptor */
152 static struct usb_ms_header_descriptor ms_header_desc
= {
153 .bLength
= USB_DT_MS_HEADER_SIZE
,
154 .bDescriptorType
= USB_DT_CS_INTERFACE
,
155 .bDescriptorSubtype
= USB_MS_HEADER
,
156 .bcdMSC
= cpu_to_le16(0x0100),
157 /* .wTotalLength = DYNAMIC */
160 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
161 static struct usb_endpoint_descriptor bulk_out_desc
= {
162 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
163 .bDescriptorType
= USB_DT_ENDPOINT
,
164 .bEndpointAddress
= USB_DIR_OUT
,
165 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
168 static struct usb_ss_ep_comp_descriptor bulk_out_ss_comp_desc
= {
169 .bLength
= sizeof(bulk_out_ss_comp_desc
),
170 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
171 /* .bMaxBurst = 0, */
172 /* .bmAttributes = 0, */
175 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
176 static struct usb_ms_endpoint_descriptor_16 ms_out_desc
= {
177 /* .bLength = DYNAMIC */
178 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
179 .bDescriptorSubtype
= USB_MS_GENERAL
,
180 /* .bNumEmbMIDIJack = DYNAMIC */
181 /* .baAssocJackID = DYNAMIC */
184 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
185 static struct usb_endpoint_descriptor bulk_in_desc
= {
186 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
187 .bDescriptorType
= USB_DT_ENDPOINT
,
188 .bEndpointAddress
= USB_DIR_IN
,
189 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
192 static struct usb_ss_ep_comp_descriptor bulk_in_ss_comp_desc
= {
193 .bLength
= sizeof(bulk_in_ss_comp_desc
),
194 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
195 /* .bMaxBurst = 0, */
196 /* .bmAttributes = 0, */
199 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
200 static struct usb_ms_endpoint_descriptor_16 ms_in_desc
= {
201 /* .bLength = DYNAMIC */
202 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
203 .bDescriptorSubtype
= USB_MS_GENERAL
,
204 /* .bNumEmbMIDIJack = DYNAMIC */
205 /* .baAssocJackID = DYNAMIC */
208 /* string IDs are assigned dynamically */
210 #define STRING_FUNC_IDX 0
212 static struct usb_string midi_string_defs
[] = {
213 [STRING_FUNC_IDX
].s
= "MIDI function",
214 { } /* end of list */
217 static struct usb_gadget_strings midi_stringtab
= {
218 .language
= 0x0409, /* en-us */
219 .strings
= midi_string_defs
,
222 static struct usb_gadget_strings
*midi_strings
[] = {
227 static inline struct usb_request
*midi_alloc_ep_req(struct usb_ep
*ep
,
230 return alloc_ep_req(ep
, length
);
233 static const uint8_t f_midi_cin_length
[] = {
234 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
238 * Receives a chunk of MIDI data.
240 static void f_midi_read_data(struct usb_ep
*ep
, int cable
,
241 uint8_t *data
, int length
)
243 struct f_midi
*midi
= ep
->driver_data
;
244 struct snd_rawmidi_substream
*substream
= midi
->out_substream
[cable
];
247 /* Nobody is listening - throw it on the floor. */
250 if (!test_bit(cable
, &midi
->out_triggered
))
253 snd_rawmidi_receive(substream
, data
, length
);
256 static void f_midi_handle_out_data(struct usb_ep
*ep
, struct usb_request
*req
)
261 for (i
= 0; i
+ 3 < req
->actual
; i
+= 4)
263 int cable
= buf
[i
] >> 4;
264 int length
= f_midi_cin_length
[buf
[i
] & 0x0f];
265 f_midi_read_data(ep
, cable
, &buf
[i
+ 1], length
);
270 f_midi_complete(struct usb_ep
*ep
, struct usb_request
*req
)
272 struct f_midi
*midi
= ep
->driver_data
;
273 struct usb_composite_dev
*cdev
= midi
->func
.config
->cdev
;
274 int status
= req
->status
;
277 case 0: /* normal completion */
278 if (ep
== midi
->out_ep
) {
279 /* We received stuff. req is queued again, below */
280 f_midi_handle_out_data(ep
, req
);
281 } else if (ep
== midi
->in_ep
) {
282 /* Our transmit completed. See if there's more to go.
283 * f_midi_transmit eats req, don't queue it again. */
285 f_midi_transmit(midi
);
290 /* this endpoint is normally active while we're configured */
291 case -ECONNABORTED
: /* hardware forced ep reset */
292 case -ECONNRESET
: /* request dequeued */
293 case -ESHUTDOWN
: /* disconnect from host */
294 VDBG(cdev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
295 req
->actual
, req
->length
);
296 if (ep
== midi
->out_ep
) {
297 f_midi_handle_out_data(ep
, req
);
298 /* We don't need to free IN requests because it's handled
299 * by the midi->in_req_fifo. */
300 free_ep_req(ep
, req
);
304 case -EOVERFLOW
: /* buffer overrun on read means that
305 * we didn't provide a big enough buffer.
308 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
309 status
, req
->actual
, req
->length
);
311 case -EREMOTEIO
: /* short read */
315 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
317 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
318 ep
->name
, req
->length
, status
);
320 /* FIXME recover later ... somehow */
324 static void f_midi_drop_out_substreams(struct f_midi
*midi
)
328 for (i
= 0; i
< midi
->in_ports
; i
++) {
329 struct gmidi_in_port
*port
= midi
->in_ports_array
+ i
;
330 struct snd_rawmidi_substream
*substream
= port
->substream
;
332 if (port
->active
&& substream
)
333 snd_rawmidi_drop_output(substream
);
337 static int f_midi_start_ep(struct f_midi
*midi
,
338 struct usb_function
*f
,
342 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
346 err
= config_ep_by_speed(midi
->gadget
, f
, ep
);
348 ERROR(cdev
, "can't configure %s: %d\n", ep
->name
, err
);
352 err
= usb_ep_enable(ep
);
354 ERROR(cdev
, "can't start %s: %d\n", ep
->name
, err
);
358 ep
->driver_data
= midi
;
363 static int f_midi_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
365 struct f_midi
*midi
= func_to_midi(f
);
369 /* we only set alt for MIDIStreaming interface */
370 if (intf
!= midi
->ms_id
)
373 err
= f_midi_start_ep(midi
, f
, midi
->in_ep
);
377 err
= f_midi_start_ep(midi
, f
, midi
->out_ep
);
381 /* pre-allocate write usb requests to use on f_midi_transmit. */
382 while (kfifo_avail(&midi
->in_req_fifo
)) {
383 struct usb_request
*req
=
384 midi_alloc_ep_req(midi
->in_ep
, midi
->buflen
);
390 req
->complete
= f_midi_complete
;
392 kfifo_put(&midi
->in_req_fifo
, req
);
395 /* allocate a bunch of read buffers and queue them all at once. */
396 for (i
= 0; i
< midi
->qlen
&& err
== 0; i
++) {
397 struct usb_request
*req
=
398 midi_alloc_ep_req(midi
->out_ep
, midi
->buflen
);
403 req
->complete
= f_midi_complete
;
404 err
= usb_ep_queue(midi
->out_ep
, req
, GFP_ATOMIC
);
406 ERROR(midi
, "%s: couldn't enqueue request: %d\n",
407 midi
->out_ep
->name
, err
);
408 if (req
->buf
!= NULL
)
409 free_ep_req(midi
->out_ep
, req
);
417 static void f_midi_disable(struct usb_function
*f
)
419 struct f_midi
*midi
= func_to_midi(f
);
420 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
421 struct usb_request
*req
= NULL
;
423 DBG(cdev
, "disable\n");
426 * just disable endpoints, forcing completion of pending i/o.
427 * all our completion handlers free their requests in this case.
429 usb_ep_disable(midi
->in_ep
);
430 usb_ep_disable(midi
->out_ep
);
432 /* release IN requests */
433 while (kfifo_get(&midi
->in_req_fifo
, &req
))
434 free_ep_req(midi
->in_ep
, req
);
436 f_midi_drop_out_substreams(midi
);
439 static int f_midi_snd_free(struct snd_device
*device
)
445 * Converts MIDI commands to USB MIDI packets.
447 static void f_midi_transmit_byte(struct usb_request
*req
,
448 struct gmidi_in_port
*port
, uint8_t b
)
450 uint8_t p
[4] = { port
->cable
<< 4, 0, 0, 0 };
451 uint8_t next_state
= STATE_INITIAL
;
455 /* System Real-Time Messages */
458 next_state
= port
->state
;
459 port
->state
= STATE_REAL_TIME
;
464 switch (port
->state
) {
468 next_state
= STATE_FINISHED
;
472 p
[1] = port
->data
[0];
474 next_state
= STATE_FINISHED
;
478 p
[1] = port
->data
[0];
479 p
[2] = port
->data
[1];
481 next_state
= STATE_FINISHED
;
485 next_state
= port
->state
;
486 port
->state
= STATE_INITIAL
;
491 /* System Common Messages */
492 port
->data
[0] = port
->data
[1] = 0;
493 port
->state
= STATE_INITIAL
;
498 next_state
= STATE_SYSEX_1
;
503 next_state
= STATE_1PARAM
;
507 next_state
= STATE_2PARAM_1
;
511 next_state
= STATE_INITIAL
;
516 next_state
= STATE_FINISHED
;
523 * Channel Voice Messages, Channel Mode Messages
524 * and Control Change Messages.
528 port
->state
= STATE_INITIAL
;
529 if (b
>= 0xc0 && b
<= 0xdf)
530 next_state
= STATE_1PARAM
;
532 next_state
= STATE_2PARAM_1
;
536 /* Message parameters */
537 switch (port
->state
) {
539 if (port
->data
[0] < 0xf0)
540 p
[0] |= port
->data
[0] >> 4;
544 p
[1] = port
->data
[0];
546 /* This is to allow Running State Messages */
547 next_state
= STATE_1PARAM
;
551 next_state
= STATE_2PARAM_2
;
554 if (port
->data
[0] < 0xf0)
555 p
[0] |= port
->data
[0] >> 4;
559 p
[1] = port
->data
[0];
560 p
[2] = port
->data
[1];
562 /* This is to allow Running State Messages */
563 next_state
= STATE_2PARAM_1
;
567 next_state
= STATE_SYSEX_1
;
571 next_state
= STATE_SYSEX_2
;
575 p
[1] = port
->data
[0];
576 p
[2] = port
->data
[1];
578 next_state
= STATE_SYSEX_0
;
584 /* States where we have to write into the USB request */
585 if (next_state
== STATE_FINISHED
||
586 port
->state
== STATE_SYSEX_2
||
587 port
->state
== STATE_1PARAM
||
588 port
->state
== STATE_2PARAM_2
||
589 port
->state
== STATE_REAL_TIME
) {
591 unsigned int length
= req
->length
;
592 u8
*buf
= (u8
*)req
->buf
+ length
;
594 memcpy(buf
, p
, sizeof(p
));
595 req
->length
= length
+ sizeof(p
);
597 if (next_state
== STATE_FINISHED
) {
598 next_state
= STATE_INITIAL
;
599 port
->data
[0] = port
->data
[1] = 0;
603 port
->state
= next_state
;
606 static int f_midi_do_transmit(struct f_midi
*midi
, struct usb_ep
*ep
)
608 struct usb_request
*req
= NULL
;
614 * We peek the request in order to reuse it if it fails to enqueue on
617 len
= kfifo_peek(&midi
->in_req_fifo
, &req
);
619 ERROR(midi
, "%s: Couldn't get usb request\n", __func__
);
624 * If buffer overrun, then we ignore this transmission.
625 * IMPORTANT: This will cause the user-space rawmidi device to block
626 * until a) usb requests have been completed or b) snd_rawmidi_write()
632 for (i
= midi
->in_last_port
; i
< midi
->in_ports
; ++i
) {
633 struct gmidi_in_port
*port
= midi
->in_ports_array
+ i
;
634 struct snd_rawmidi_substream
*substream
= port
->substream
;
636 if (!port
->active
|| !substream
)
639 while (req
->length
+ 3 < midi
->buflen
) {
642 if (snd_rawmidi_transmit(substream
, &b
, 1) != 1) {
646 f_midi_transmit_byte(req
, port
, b
);
649 active
= !!port
->active
;
653 midi
->in_last_port
= active
? i
: 0;
655 if (req
->length
<= 0)
658 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
660 ERROR(midi
, "%s failed to queue req: %d\n",
661 midi
->in_ep
->name
, err
);
662 req
->length
= 0; /* Re-use request next time. */
664 /* Upon success, put request at the back of the queue. */
665 kfifo_skip(&midi
->in_req_fifo
);
666 kfifo_put(&midi
->in_req_fifo
, req
);
673 static void f_midi_transmit(struct f_midi
*midi
)
675 struct usb_ep
*ep
= midi
->in_ep
;
679 /* We only care about USB requests if IN endpoint is enabled */
680 if (!ep
|| !ep
->enabled
)
683 spin_lock_irqsave(&midi
->transmit_lock
, flags
);
686 ret
= f_midi_do_transmit(midi
, ep
);
688 spin_unlock_irqrestore(&midi
->transmit_lock
, flags
);
693 spin_unlock_irqrestore(&midi
->transmit_lock
, flags
);
698 f_midi_drop_out_substreams(midi
);
701 static void f_midi_in_tasklet(unsigned long data
)
703 struct f_midi
*midi
= (struct f_midi
*) data
;
704 f_midi_transmit(midi
);
707 static int f_midi_in_open(struct snd_rawmidi_substream
*substream
)
709 struct f_midi
*midi
= substream
->rmidi
->private_data
;
710 struct gmidi_in_port
*port
;
712 if (substream
->number
>= midi
->in_ports
)
715 VDBG(midi
, "%s()\n", __func__
);
716 port
= midi
->in_ports_array
+ substream
->number
;
717 port
->substream
= substream
;
718 port
->state
= STATE_INITIAL
;
722 static int f_midi_in_close(struct snd_rawmidi_substream
*substream
)
724 struct f_midi
*midi
= substream
->rmidi
->private_data
;
726 VDBG(midi
, "%s()\n", __func__
);
730 static void f_midi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
732 struct f_midi
*midi
= substream
->rmidi
->private_data
;
734 if (substream
->number
>= midi
->in_ports
)
737 VDBG(midi
, "%s() %d\n", __func__
, up
);
738 midi
->in_ports_array
[substream
->number
].active
= up
;
740 tasklet_hi_schedule(&midi
->tasklet
);
743 static int f_midi_out_open(struct snd_rawmidi_substream
*substream
)
745 struct f_midi
*midi
= substream
->rmidi
->private_data
;
747 if (substream
->number
>= MAX_PORTS
)
750 VDBG(midi
, "%s()\n", __func__
);
751 midi
->out_substream
[substream
->number
] = substream
;
755 static int f_midi_out_close(struct snd_rawmidi_substream
*substream
)
757 struct f_midi
*midi
= substream
->rmidi
->private_data
;
759 VDBG(midi
, "%s()\n", __func__
);
763 static void f_midi_out_trigger(struct snd_rawmidi_substream
*substream
, int up
)
765 struct f_midi
*midi
= substream
->rmidi
->private_data
;
767 VDBG(midi
, "%s()\n", __func__
);
770 set_bit(substream
->number
, &midi
->out_triggered
);
772 clear_bit(substream
->number
, &midi
->out_triggered
);
775 static const struct snd_rawmidi_ops gmidi_in_ops
= {
776 .open
= f_midi_in_open
,
777 .close
= f_midi_in_close
,
778 .trigger
= f_midi_in_trigger
,
781 static const struct snd_rawmidi_ops gmidi_out_ops
= {
782 .open
= f_midi_out_open
,
783 .close
= f_midi_out_close
,
784 .trigger
= f_midi_out_trigger
787 static inline void f_midi_unregister_card(struct f_midi
*midi
)
790 snd_card_free(midi
->card
);
795 /* register as a sound "card" */
796 static int f_midi_register_card(struct f_midi
*midi
)
798 struct snd_card
*card
;
799 struct snd_rawmidi
*rmidi
;
801 static struct snd_device_ops ops
= {
802 .dev_free
= f_midi_snd_free
,
805 err
= snd_card_new(&midi
->gadget
->dev
, midi
->index
, midi
->id
,
806 THIS_MODULE
, 0, &card
);
808 ERROR(midi
, "snd_card_new() failed\n");
813 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, midi
, &ops
);
815 ERROR(midi
, "snd_device_new() failed: error %d\n", err
);
819 strcpy(card
->driver
, f_midi_longname
);
820 strcpy(card
->longname
, f_midi_longname
);
821 strcpy(card
->shortname
, f_midi_shortname
);
824 snd_component_add(card
, "MIDI");
825 err
= snd_rawmidi_new(card
, card
->longname
, 0,
826 midi
->out_ports
, midi
->in_ports
, &rmidi
);
828 ERROR(midi
, "snd_rawmidi_new() failed: error %d\n", err
);
832 midi
->in_last_port
= 0;
833 strcpy(rmidi
->name
, card
->shortname
);
834 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
835 SNDRV_RAWMIDI_INFO_INPUT
|
836 SNDRV_RAWMIDI_INFO_DUPLEX
;
837 rmidi
->private_data
= midi
;
838 rmidi
->private_free
= f_midi_rmidi_free
;
842 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
843 * It's an upside-down world being a gadget.
845 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &gmidi_in_ops
);
846 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &gmidi_out_ops
);
848 /* register it - we're ready to go */
849 err
= snd_card_register(card
);
851 ERROR(midi
, "snd_card_register() failed\n");
855 VDBG(midi
, "%s() finished ok\n", __func__
);
859 f_midi_unregister_card(midi
);
863 /* MIDI function driver setup/binding */
865 static int f_midi_bind(struct usb_configuration
*c
, struct usb_function
*f
)
867 struct usb_descriptor_header
**midi_function
;
868 struct usb_midi_in_jack_descriptor jack_in_ext_desc
[MAX_PORTS
];
869 struct usb_midi_in_jack_descriptor jack_in_emb_desc
[MAX_PORTS
];
870 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc
[MAX_PORTS
];
871 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc
[MAX_PORTS
];
872 struct usb_composite_dev
*cdev
= c
->cdev
;
873 struct f_midi
*midi
= func_to_midi(f
);
874 struct usb_string
*us
;
875 int status
, n
, jack
= 1, i
= 0, endpoint_descriptor_index
= 0;
877 midi
->gadget
= cdev
->gadget
;
878 tasklet_init(&midi
->tasklet
, f_midi_in_tasklet
, (unsigned long) midi
);
879 status
= f_midi_register_card(midi
);
883 /* maybe allocate device-global string ID */
884 us
= usb_gstrings_attach(c
->cdev
, midi_strings
,
885 ARRAY_SIZE(midi_string_defs
));
887 status
= PTR_ERR(us
);
890 ac_interface_desc
.iInterface
= us
[STRING_FUNC_IDX
].id
;
892 /* We have two interfaces, AudioControl and MIDIStreaming */
893 status
= usb_interface_id(c
, f
);
896 ac_interface_desc
.bInterfaceNumber
= status
;
898 status
= usb_interface_id(c
, f
);
901 ms_interface_desc
.bInterfaceNumber
= status
;
902 ac_header_desc
.baInterfaceNr
[0] = status
;
903 midi
->ms_id
= status
;
907 /* allocate instance-specific endpoints */
908 midi
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_in_desc
);
912 midi
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_out_desc
);
916 /* allocate temporary function list */
917 midi_function
= kcalloc((MAX_PORTS
* 4) + 11, sizeof(*midi_function
),
919 if (!midi_function
) {
925 * construct the function's descriptor set. As the number of
926 * input and output MIDI ports is configurable, we have to do
930 /* add the headers - these are always the same */
931 midi_function
[i
++] = (struct usb_descriptor_header
*) &ac_interface_desc
;
932 midi_function
[i
++] = (struct usb_descriptor_header
*) &ac_header_desc
;
933 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_interface_desc
;
935 /* calculate the header's wTotalLength */
936 n
= USB_DT_MS_HEADER_SIZE
937 + (midi
->in_ports
+ midi
->out_ports
) *
938 (USB_DT_MIDI_IN_SIZE
+ USB_DT_MIDI_OUT_SIZE(1));
939 ms_header_desc
.wTotalLength
= cpu_to_le16(n
);
941 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_header_desc
;
943 /* configure the external IN jacks, each linked to an embedded OUT jack */
944 for (n
= 0; n
< midi
->in_ports
; n
++) {
945 struct usb_midi_in_jack_descriptor
*in_ext
= &jack_in_ext_desc
[n
];
946 struct usb_midi_out_jack_descriptor_1
*out_emb
= &jack_out_emb_desc
[n
];
948 in_ext
->bLength
= USB_DT_MIDI_IN_SIZE
;
949 in_ext
->bDescriptorType
= USB_DT_CS_INTERFACE
;
950 in_ext
->bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
;
951 in_ext
->bJackType
= USB_MS_EXTERNAL
;
952 in_ext
->bJackID
= jack
++;
954 midi_function
[i
++] = (struct usb_descriptor_header
*) in_ext
;
956 out_emb
->bLength
= USB_DT_MIDI_OUT_SIZE(1);
957 out_emb
->bDescriptorType
= USB_DT_CS_INTERFACE
;
958 out_emb
->bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
;
959 out_emb
->bJackType
= USB_MS_EMBEDDED
;
960 out_emb
->bJackID
= jack
++;
961 out_emb
->bNrInputPins
= 1;
962 out_emb
->pins
[0].baSourcePin
= 1;
963 out_emb
->pins
[0].baSourceID
= in_ext
->bJackID
;
965 midi_function
[i
++] = (struct usb_descriptor_header
*) out_emb
;
967 /* link it to the endpoint */
968 ms_in_desc
.baAssocJackID
[n
] = out_emb
->bJackID
;
971 /* configure the external OUT jacks, each linked to an embedded IN jack */
972 for (n
= 0; n
< midi
->out_ports
; n
++) {
973 struct usb_midi_in_jack_descriptor
*in_emb
= &jack_in_emb_desc
[n
];
974 struct usb_midi_out_jack_descriptor_1
*out_ext
= &jack_out_ext_desc
[n
];
976 in_emb
->bLength
= USB_DT_MIDI_IN_SIZE
;
977 in_emb
->bDescriptorType
= USB_DT_CS_INTERFACE
;
978 in_emb
->bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
;
979 in_emb
->bJackType
= USB_MS_EMBEDDED
;
980 in_emb
->bJackID
= jack
++;
982 midi_function
[i
++] = (struct usb_descriptor_header
*) in_emb
;
984 out_ext
->bLength
= USB_DT_MIDI_OUT_SIZE(1);
985 out_ext
->bDescriptorType
= USB_DT_CS_INTERFACE
;
986 out_ext
->bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
;
987 out_ext
->bJackType
= USB_MS_EXTERNAL
;
988 out_ext
->bJackID
= jack
++;
989 out_ext
->bNrInputPins
= 1;
991 out_ext
->pins
[0].baSourceID
= in_emb
->bJackID
;
992 out_ext
->pins
[0].baSourcePin
= 1;
993 midi_function
[i
++] = (struct usb_descriptor_header
*) out_ext
;
995 /* link it to the endpoint */
996 ms_out_desc
.baAssocJackID
[n
] = in_emb
->bJackID
;
999 /* configure the endpoint descriptors ... */
1000 ms_out_desc
.bLength
= USB_DT_MS_ENDPOINT_SIZE(midi
->in_ports
);
1001 ms_out_desc
.bNumEmbMIDIJack
= midi
->in_ports
;
1003 ms_in_desc
.bLength
= USB_DT_MS_ENDPOINT_SIZE(midi
->out_ports
);
1004 ms_in_desc
.bNumEmbMIDIJack
= midi
->out_ports
;
1006 /* ... and add them to the list */
1007 endpoint_descriptor_index
= i
;
1008 midi_function
[i
++] = (struct usb_descriptor_header
*) &bulk_out_desc
;
1009 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_out_desc
;
1010 midi_function
[i
++] = (struct usb_descriptor_header
*) &bulk_in_desc
;
1011 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_in_desc
;
1012 midi_function
[i
++] = NULL
;
1015 * support all relevant hardware speeds... we expect that when
1016 * hardware is dual speed, all bulk-capable endpoints work at
1019 /* copy descriptors, and track endpoint copies */
1020 f
->fs_descriptors
= usb_copy_descriptors(midi_function
);
1021 if (!f
->fs_descriptors
)
1024 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
1025 bulk_in_desc
.wMaxPacketSize
= cpu_to_le16(512);
1026 bulk_out_desc
.wMaxPacketSize
= cpu_to_le16(512);
1027 f
->hs_descriptors
= usb_copy_descriptors(midi_function
);
1028 if (!f
->hs_descriptors
)
1032 if (gadget_is_superspeed(c
->cdev
->gadget
)) {
1033 bulk_in_desc
.wMaxPacketSize
= cpu_to_le16(1024);
1034 bulk_out_desc
.wMaxPacketSize
= cpu_to_le16(1024);
1035 i
= endpoint_descriptor_index
;
1036 midi_function
[i
++] = (struct usb_descriptor_header
*)
1038 midi_function
[i
++] = (struct usb_descriptor_header
*)
1039 &bulk_out_ss_comp_desc
;
1040 midi_function
[i
++] = (struct usb_descriptor_header
*)
1042 midi_function
[i
++] = (struct usb_descriptor_header
*)
1044 midi_function
[i
++] = (struct usb_descriptor_header
*)
1045 &bulk_in_ss_comp_desc
;
1046 midi_function
[i
++] = (struct usb_descriptor_header
*)
1048 f
->ss_descriptors
= usb_copy_descriptors(midi_function
);
1049 if (!f
->ss_descriptors
)
1053 kfree(midi_function
);
1058 kfree(midi_function
);
1059 usb_free_all_descriptors(f
);
1061 f_midi_unregister_card(midi
);
1063 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
1068 static inline struct f_midi_opts
*to_f_midi_opts(struct config_item
*item
)
1070 return container_of(to_config_group(item
), struct f_midi_opts
,
1074 static void midi_attr_release(struct config_item
*item
)
1076 struct f_midi_opts
*opts
= to_f_midi_opts(item
);
1078 usb_put_function_instance(&opts
->func_inst
);
1081 static struct configfs_item_operations midi_item_ops
= {
1082 .release
= midi_attr_release
,
1085 #define F_MIDI_OPT(name, test_limit, limit) \
1086 static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
1088 struct f_midi_opts *opts = to_f_midi_opts(item); \
1091 mutex_lock(&opts->lock); \
1092 result = sprintf(page, "%d\n", opts->name); \
1093 mutex_unlock(&opts->lock); \
1098 static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
1099 const char *page, size_t len) \
1101 struct f_midi_opts *opts = to_f_midi_opts(item); \
1105 mutex_lock(&opts->lock); \
1106 if (opts->refcnt > 1) { \
1111 ret = kstrtou32(page, 0, &num); \
1115 if (test_limit && num > limit) { \
1123 mutex_unlock(&opts->lock); \
1127 CONFIGFS_ATTR(f_midi_opts_, name);
1129 F_MIDI_OPT(index
, true, SNDRV_CARDS
);
1130 F_MIDI_OPT(buflen
, false, 0);
1131 F_MIDI_OPT(qlen
, false, 0);
1132 F_MIDI_OPT(in_ports
, true, MAX_PORTS
);
1133 F_MIDI_OPT(out_ports
, true, MAX_PORTS
);
1135 static ssize_t
f_midi_opts_id_show(struct config_item
*item
, char *page
)
1137 struct f_midi_opts
*opts
= to_f_midi_opts(item
);
1140 mutex_lock(&opts
->lock
);
1142 result
= strlcpy(page
, opts
->id
, PAGE_SIZE
);
1148 mutex_unlock(&opts
->lock
);
1153 static ssize_t
f_midi_opts_id_store(struct config_item
*item
,
1154 const char *page
, size_t len
)
1156 struct f_midi_opts
*opts
= to_f_midi_opts(item
);
1160 mutex_lock(&opts
->lock
);
1161 if (opts
->refcnt
> 1) {
1166 c
= kstrndup(page
, len
, GFP_KERNEL
);
1171 if (opts
->id_allocated
)
1174 opts
->id_allocated
= true;
1177 mutex_unlock(&opts
->lock
);
1181 CONFIGFS_ATTR(f_midi_opts_
, id
);
1183 static struct configfs_attribute
*midi_attrs
[] = {
1184 &f_midi_opts_attr_index
,
1185 &f_midi_opts_attr_buflen
,
1186 &f_midi_opts_attr_qlen
,
1187 &f_midi_opts_attr_in_ports
,
1188 &f_midi_opts_attr_out_ports
,
1189 &f_midi_opts_attr_id
,
1193 static const struct config_item_type midi_func_type
= {
1194 .ct_item_ops
= &midi_item_ops
,
1195 .ct_attrs
= midi_attrs
,
1196 .ct_owner
= THIS_MODULE
,
1199 static void f_midi_free_inst(struct usb_function_instance
*f
)
1201 struct f_midi_opts
*opts
;
1204 opts
= container_of(f
, struct f_midi_opts
, func_inst
);
1206 mutex_lock(&opts
->lock
);
1207 if (!--opts
->refcnt
) {
1210 mutex_unlock(&opts
->lock
);
1213 if (opts
->id_allocated
)
1219 static struct usb_function_instance
*f_midi_alloc_inst(void)
1221 struct f_midi_opts
*opts
;
1223 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
1225 return ERR_PTR(-ENOMEM
);
1227 mutex_init(&opts
->lock
);
1228 opts
->func_inst
.free_func_inst
= f_midi_free_inst
;
1229 opts
->index
= SNDRV_DEFAULT_IDX1
;
1230 opts
->id
= SNDRV_DEFAULT_STR1
;
1234 opts
->out_ports
= 1;
1237 config_group_init_type_name(&opts
->func_inst
.group
, "",
1240 return &opts
->func_inst
;
1243 static void f_midi_free(struct usb_function
*f
)
1245 struct f_midi
*midi
;
1246 struct f_midi_opts
*opts
;
1249 midi
= func_to_midi(f
);
1250 opts
= container_of(f
->fi
, struct f_midi_opts
, func_inst
);
1251 mutex_lock(&opts
->lock
);
1252 if (!--midi
->free_ref
) {
1254 kfifo_free(&midi
->in_req_fifo
);
1258 mutex_unlock(&opts
->lock
);
1261 f_midi_free_inst(&opts
->func_inst
);
1264 static void f_midi_rmidi_free(struct snd_rawmidi
*rmidi
)
1266 f_midi_free(rmidi
->private_data
);
1269 static void f_midi_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
1271 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
1272 struct f_midi
*midi
= func_to_midi(f
);
1273 struct snd_card
*card
;
1275 DBG(cdev
, "unbind\n");
1277 /* just to be sure */
1283 snd_card_free_when_closed(card
);
1285 usb_free_all_descriptors(f
);
1288 static struct usb_function
*f_midi_alloc(struct usb_function_instance
*fi
)
1290 struct f_midi
*midi
= NULL
;
1291 struct f_midi_opts
*opts
;
1294 opts
= container_of(fi
, struct f_midi_opts
, func_inst
);
1296 mutex_lock(&opts
->lock
);
1298 if (opts
->in_ports
> MAX_PORTS
|| opts
->out_ports
> MAX_PORTS
) {
1303 /* allocate and initialize one new instance */
1304 midi
= kzalloc(struct_size(midi
, in_ports_array
, opts
->in_ports
),
1311 for (i
= 0; i
< opts
->in_ports
; i
++)
1312 midi
->in_ports_array
[i
].cable
= i
;
1314 /* set up ALSA midi devices */
1315 midi
->id
= kstrdup(opts
->id
, GFP_KERNEL
);
1316 if (opts
->id
&& !midi
->id
) {
1320 midi
->in_ports
= opts
->in_ports
;
1321 midi
->out_ports
= opts
->out_ports
;
1322 midi
->index
= opts
->index
;
1323 midi
->buflen
= opts
->buflen
;
1324 midi
->qlen
= opts
->qlen
;
1325 midi
->in_last_port
= 0;
1328 status
= kfifo_alloc(&midi
->in_req_fifo
, midi
->qlen
, GFP_KERNEL
);
1332 spin_lock_init(&midi
->transmit_lock
);
1335 mutex_unlock(&opts
->lock
);
1337 midi
->func
.name
= "gmidi function";
1338 midi
->func
.bind
= f_midi_bind
;
1339 midi
->func
.unbind
= f_midi_unbind
;
1340 midi
->func
.set_alt
= f_midi_set_alt
;
1341 midi
->func
.disable
= f_midi_disable
;
1342 midi
->func
.free_func
= f_midi_free
;
1347 mutex_unlock(&opts
->lock
);
1349 return ERR_PTR(status
);
1352 DECLARE_USB_FUNCTION_INIT(midi
, f_midi_alloc_inst
, f_midi_alloc
);