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/module.h>
24 #include <linux/slab.h>
25 #include <linux/device.h>
26 #include <linux/kfifo.h>
27 #include <linux/spinlock.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/rawmidi.h>
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/gadget.h>
35 #include <linux/usb/audio.h>
36 #include <linux/usb/midi.h>
41 MODULE_AUTHOR("Ben Williamson");
42 MODULE_LICENSE("GPL v2");
44 static const char f_midi_shortname
[] = "f_midi";
45 static const char f_midi_longname
[] = "MIDI Gadget";
48 * We can only handle 16 cables on one single endpoint, as cable numbers are
49 * stored in 4-bit fields. And as the interface currently only holds one
50 * single endpoint, this is the maximum number of ports we can allow.
54 /* MIDI message states */
56 STATE_INITIAL
= 0, /* pseudo state */
64 STATE_FINISHED
, /* pseudo state */
68 * This is a gadget, and the IN/OUT naming is from the host's perspective.
69 * USB -> OUT endpoint -> rawmidi
70 * USB <- IN endpoint <- rawmidi
72 struct gmidi_in_port
{
73 struct snd_rawmidi_substream
*substream
;
81 struct usb_function func
;
82 struct usb_gadget
*gadget
;
83 struct usb_ep
*in_ep
, *out_ep
;
84 struct snd_card
*card
;
85 struct snd_rawmidi
*rmidi
;
88 struct snd_rawmidi_substream
*out_substream
[MAX_PORTS
];
90 unsigned long out_triggered
;
91 struct tasklet_struct tasklet
;
92 unsigned int in_ports
;
93 unsigned int out_ports
;
96 unsigned int buflen
, qlen
;
97 /* This fifo is used as a buffer ring for pre-allocated IN usb_requests */
98 DECLARE_KFIFO_PTR(in_req_fifo
, struct usb_request
*);
99 spinlock_t transmit_lock
;
100 unsigned int in_last_port
;
102 struct gmidi_in_port in_ports_array
[/* in_ports */];
105 static inline struct f_midi
*func_to_midi(struct usb_function
*f
)
107 return container_of(f
, struct f_midi
, func
);
110 static void f_midi_transmit(struct f_midi
*midi
);
112 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
113 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
114 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
116 /* B.3.1 Standard AC Interface Descriptor */
117 static struct usb_interface_descriptor ac_interface_desc
= {
118 .bLength
= USB_DT_INTERFACE_SIZE
,
119 .bDescriptorType
= USB_DT_INTERFACE
,
120 /* .bInterfaceNumber = DYNAMIC */
121 /* .bNumEndpoints = DYNAMIC */
122 .bInterfaceClass
= USB_CLASS_AUDIO
,
123 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
124 /* .iInterface = DYNAMIC */
127 /* B.3.2 Class-Specific AC Interface Descriptor */
128 static struct uac1_ac_header_descriptor_1 ac_header_desc
= {
129 .bLength
= UAC_DT_AC_HEADER_SIZE(1),
130 .bDescriptorType
= USB_DT_CS_INTERFACE
,
131 .bDescriptorSubtype
= USB_MS_HEADER
,
132 .bcdADC
= cpu_to_le16(0x0100),
133 .wTotalLength
= cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
135 /* .baInterfaceNr = DYNAMIC */
138 /* B.4.1 Standard MS Interface Descriptor */
139 static struct usb_interface_descriptor ms_interface_desc
= {
140 .bLength
= USB_DT_INTERFACE_SIZE
,
141 .bDescriptorType
= USB_DT_INTERFACE
,
142 /* .bInterfaceNumber = DYNAMIC */
144 .bInterfaceClass
= USB_CLASS_AUDIO
,
145 .bInterfaceSubClass
= USB_SUBCLASS_MIDISTREAMING
,
146 /* .iInterface = DYNAMIC */
149 /* B.4.2 Class-Specific MS Interface Descriptor */
150 static struct usb_ms_header_descriptor ms_header_desc
= {
151 .bLength
= USB_DT_MS_HEADER_SIZE
,
152 .bDescriptorType
= USB_DT_CS_INTERFACE
,
153 .bDescriptorSubtype
= USB_MS_HEADER
,
154 .bcdMSC
= cpu_to_le16(0x0100),
155 /* .wTotalLength = DYNAMIC */
158 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
159 static struct usb_endpoint_descriptor bulk_out_desc
= {
160 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
161 .bDescriptorType
= USB_DT_ENDPOINT
,
162 .bEndpointAddress
= USB_DIR_OUT
,
163 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
166 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
167 static struct usb_ms_endpoint_descriptor_16 ms_out_desc
= {
168 /* .bLength = DYNAMIC */
169 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
170 .bDescriptorSubtype
= USB_MS_GENERAL
,
171 /* .bNumEmbMIDIJack = DYNAMIC */
172 /* .baAssocJackID = DYNAMIC */
175 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
176 static struct usb_endpoint_descriptor bulk_in_desc
= {
177 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
178 .bDescriptorType
= USB_DT_ENDPOINT
,
179 .bEndpointAddress
= USB_DIR_IN
,
180 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
183 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
184 static struct usb_ms_endpoint_descriptor_16 ms_in_desc
= {
185 /* .bLength = DYNAMIC */
186 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
187 .bDescriptorSubtype
= USB_MS_GENERAL
,
188 /* .bNumEmbMIDIJack = DYNAMIC */
189 /* .baAssocJackID = DYNAMIC */
192 /* string IDs are assigned dynamically */
194 #define STRING_FUNC_IDX 0
196 static struct usb_string midi_string_defs
[] = {
197 [STRING_FUNC_IDX
].s
= "MIDI function",
198 { } /* end of list */
201 static struct usb_gadget_strings midi_stringtab
= {
202 .language
= 0x0409, /* en-us */
203 .strings
= midi_string_defs
,
206 static struct usb_gadget_strings
*midi_strings
[] = {
211 static inline struct usb_request
*midi_alloc_ep_req(struct usb_ep
*ep
,
214 return alloc_ep_req(ep
, length
);
217 static const uint8_t f_midi_cin_length
[] = {
218 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
222 * Receives a chunk of MIDI data.
224 static void f_midi_read_data(struct usb_ep
*ep
, int cable
,
225 uint8_t *data
, int length
)
227 struct f_midi
*midi
= ep
->driver_data
;
228 struct snd_rawmidi_substream
*substream
= midi
->out_substream
[cable
];
231 /* Nobody is listening - throw it on the floor. */
234 if (!test_bit(cable
, &midi
->out_triggered
))
237 snd_rawmidi_receive(substream
, data
, length
);
240 static void f_midi_handle_out_data(struct usb_ep
*ep
, struct usb_request
*req
)
245 for (i
= 0; i
+ 3 < req
->actual
; i
+= 4)
247 int cable
= buf
[i
] >> 4;
248 int length
= f_midi_cin_length
[buf
[i
] & 0x0f];
249 f_midi_read_data(ep
, cable
, &buf
[i
+ 1], length
);
254 f_midi_complete(struct usb_ep
*ep
, struct usb_request
*req
)
256 struct f_midi
*midi
= ep
->driver_data
;
257 struct usb_composite_dev
*cdev
= midi
->func
.config
->cdev
;
258 int status
= req
->status
;
261 case 0: /* normal completion */
262 if (ep
== midi
->out_ep
) {
263 /* We received stuff. req is queued again, below */
264 f_midi_handle_out_data(ep
, req
);
265 } else if (ep
== midi
->in_ep
) {
266 /* Our transmit completed. See if there's more to go.
267 * f_midi_transmit eats req, don't queue it again. */
269 f_midi_transmit(midi
);
274 /* this endpoint is normally active while we're configured */
275 case -ECONNABORTED
: /* hardware forced ep reset */
276 case -ECONNRESET
: /* request dequeued */
277 case -ESHUTDOWN
: /* disconnect from host */
278 VDBG(cdev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
279 req
->actual
, req
->length
);
280 if (ep
== midi
->out_ep
) {
281 f_midi_handle_out_data(ep
, req
);
282 /* We don't need to free IN requests because it's handled
283 * by the midi->in_req_fifo. */
284 free_ep_req(ep
, req
);
288 case -EOVERFLOW
: /* buffer overrun on read means that
289 * we didn't provide a big enough buffer.
292 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
293 status
, req
->actual
, req
->length
);
295 case -EREMOTEIO
: /* short read */
299 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
301 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
302 ep
->name
, req
->length
, status
);
304 /* FIXME recover later ... somehow */
308 static void f_midi_drop_out_substreams(struct f_midi
*midi
)
312 for (i
= 0; i
< midi
->in_ports
; i
++) {
313 struct gmidi_in_port
*port
= midi
->in_ports_array
+ i
;
314 struct snd_rawmidi_substream
*substream
= port
->substream
;
316 if (port
->active
&& substream
)
317 snd_rawmidi_drop_output(substream
);
321 static int f_midi_start_ep(struct f_midi
*midi
,
322 struct usb_function
*f
,
326 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
330 err
= config_ep_by_speed(midi
->gadget
, f
, ep
);
332 ERROR(cdev
, "can't configure %s: %d\n", ep
->name
, err
);
336 err
= usb_ep_enable(ep
);
338 ERROR(cdev
, "can't start %s: %d\n", ep
->name
, err
);
342 ep
->driver_data
= midi
;
347 static int f_midi_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
349 struct f_midi
*midi
= func_to_midi(f
);
353 /* we only set alt for MIDIStreaming interface */
354 if (intf
!= midi
->ms_id
)
357 err
= f_midi_start_ep(midi
, f
, midi
->in_ep
);
361 err
= f_midi_start_ep(midi
, f
, midi
->out_ep
);
365 /* pre-allocate write usb requests to use on f_midi_transmit. */
366 while (kfifo_avail(&midi
->in_req_fifo
)) {
367 struct usb_request
*req
=
368 midi_alloc_ep_req(midi
->in_ep
, midi
->buflen
);
374 req
->complete
= f_midi_complete
;
376 kfifo_put(&midi
->in_req_fifo
, req
);
379 /* allocate a bunch of read buffers and queue them all at once. */
380 for (i
= 0; i
< midi
->qlen
&& err
== 0; i
++) {
381 struct usb_request
*req
=
382 midi_alloc_ep_req(midi
->out_ep
, midi
->buflen
);
387 req
->complete
= f_midi_complete
;
388 err
= usb_ep_queue(midi
->out_ep
, req
, GFP_ATOMIC
);
390 ERROR(midi
, "%s: couldn't enqueue request: %d\n",
391 midi
->out_ep
->name
, err
);
392 if (req
->buf
!= NULL
)
393 free_ep_req(midi
->out_ep
, req
);
401 static void f_midi_disable(struct usb_function
*f
)
403 struct f_midi
*midi
= func_to_midi(f
);
404 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
405 struct usb_request
*req
= NULL
;
407 DBG(cdev
, "disable\n");
410 * just disable endpoints, forcing completion of pending i/o.
411 * all our completion handlers free their requests in this case.
413 usb_ep_disable(midi
->in_ep
);
414 usb_ep_disable(midi
->out_ep
);
416 /* release IN requests */
417 while (kfifo_get(&midi
->in_req_fifo
, &req
))
418 free_ep_req(midi
->in_ep
, req
);
420 f_midi_drop_out_substreams(midi
);
423 static int f_midi_snd_free(struct snd_device
*device
)
429 * Converts MIDI commands to USB MIDI packets.
431 static void f_midi_transmit_byte(struct usb_request
*req
,
432 struct gmidi_in_port
*port
, uint8_t b
)
434 uint8_t p
[4] = { port
->cable
<< 4, 0, 0, 0 };
435 uint8_t next_state
= STATE_INITIAL
;
439 /* System Real-Time Messages */
442 next_state
= port
->state
;
443 port
->state
= STATE_REAL_TIME
;
448 switch (port
->state
) {
452 next_state
= STATE_FINISHED
;
456 p
[1] = port
->data
[0];
458 next_state
= STATE_FINISHED
;
462 p
[1] = port
->data
[0];
463 p
[2] = port
->data
[1];
465 next_state
= STATE_FINISHED
;
469 next_state
= port
->state
;
470 port
->state
= STATE_INITIAL
;
475 /* System Common Messages */
476 port
->data
[0] = port
->data
[1] = 0;
477 port
->state
= STATE_INITIAL
;
482 next_state
= STATE_SYSEX_1
;
487 next_state
= STATE_1PARAM
;
491 next_state
= STATE_2PARAM_1
;
495 next_state
= STATE_INITIAL
;
500 next_state
= STATE_FINISHED
;
507 * Channel Voice Messages, Channel Mode Messages
508 * and Control Change Messages.
512 port
->state
= STATE_INITIAL
;
513 if (b
>= 0xc0 && b
<= 0xdf)
514 next_state
= STATE_1PARAM
;
516 next_state
= STATE_2PARAM_1
;
520 /* Message parameters */
521 switch (port
->state
) {
523 if (port
->data
[0] < 0xf0)
524 p
[0] |= port
->data
[0] >> 4;
528 p
[1] = port
->data
[0];
530 /* This is to allow Running State Messages */
531 next_state
= STATE_1PARAM
;
535 next_state
= STATE_2PARAM_2
;
538 if (port
->data
[0] < 0xf0)
539 p
[0] |= port
->data
[0] >> 4;
543 p
[1] = port
->data
[0];
544 p
[2] = port
->data
[1];
546 /* This is to allow Running State Messages */
547 next_state
= STATE_2PARAM_1
;
551 next_state
= STATE_SYSEX_1
;
555 next_state
= STATE_SYSEX_2
;
559 p
[1] = port
->data
[0];
560 p
[2] = port
->data
[1];
562 next_state
= STATE_SYSEX_0
;
568 /* States where we have to write into the USB request */
569 if (next_state
== STATE_FINISHED
||
570 port
->state
== STATE_SYSEX_2
||
571 port
->state
== STATE_1PARAM
||
572 port
->state
== STATE_2PARAM_2
||
573 port
->state
== STATE_REAL_TIME
) {
575 unsigned int length
= req
->length
;
576 u8
*buf
= (u8
*)req
->buf
+ length
;
578 memcpy(buf
, p
, sizeof(p
));
579 req
->length
= length
+ sizeof(p
);
581 if (next_state
== STATE_FINISHED
) {
582 next_state
= STATE_INITIAL
;
583 port
->data
[0] = port
->data
[1] = 0;
587 port
->state
= next_state
;
590 static int f_midi_do_transmit(struct f_midi
*midi
, struct usb_ep
*ep
)
592 struct usb_request
*req
= NULL
;
598 * We peek the request in order to reuse it if it fails to enqueue on
601 len
= kfifo_peek(&midi
->in_req_fifo
, &req
);
603 ERROR(midi
, "%s: Couldn't get usb request\n", __func__
);
608 * If buffer overrun, then we ignore this transmission.
609 * IMPORTANT: This will cause the user-space rawmidi device to block
610 * until a) usb requests have been completed or b) snd_rawmidi_write()
616 for (i
= midi
->in_last_port
; i
< midi
->in_ports
; ++i
) {
617 struct gmidi_in_port
*port
= midi
->in_ports_array
+ i
;
618 struct snd_rawmidi_substream
*substream
= port
->substream
;
620 if (!port
->active
|| !substream
)
623 while (req
->length
+ 3 < midi
->buflen
) {
626 if (snd_rawmidi_transmit(substream
, &b
, 1) != 1) {
630 f_midi_transmit_byte(req
, port
, b
);
633 active
= !!port
->active
;
637 midi
->in_last_port
= active
? i
: 0;
639 if (req
->length
<= 0)
642 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
644 ERROR(midi
, "%s failed to queue req: %d\n",
645 midi
->in_ep
->name
, err
);
646 req
->length
= 0; /* Re-use request next time. */
648 /* Upon success, put request at the back of the queue. */
649 kfifo_skip(&midi
->in_req_fifo
);
650 kfifo_put(&midi
->in_req_fifo
, req
);
657 static void f_midi_transmit(struct f_midi
*midi
)
659 struct usb_ep
*ep
= midi
->in_ep
;
663 /* We only care about USB requests if IN endpoint is enabled */
664 if (!ep
|| !ep
->enabled
)
667 spin_lock_irqsave(&midi
->transmit_lock
, flags
);
670 ret
= f_midi_do_transmit(midi
, ep
);
672 spin_unlock_irqrestore(&midi
->transmit_lock
, flags
);
677 spin_unlock_irqrestore(&midi
->transmit_lock
, flags
);
682 f_midi_drop_out_substreams(midi
);
685 static void f_midi_in_tasklet(unsigned long data
)
687 struct f_midi
*midi
= (struct f_midi
*) data
;
688 f_midi_transmit(midi
);
691 static int f_midi_in_open(struct snd_rawmidi_substream
*substream
)
693 struct f_midi
*midi
= substream
->rmidi
->private_data
;
694 struct gmidi_in_port
*port
;
696 if (substream
->number
>= midi
->in_ports
)
699 VDBG(midi
, "%s()\n", __func__
);
700 port
= midi
->in_ports_array
+ substream
->number
;
701 port
->substream
= substream
;
702 port
->state
= STATE_INITIAL
;
706 static int f_midi_in_close(struct snd_rawmidi_substream
*substream
)
708 struct f_midi
*midi
= substream
->rmidi
->private_data
;
710 VDBG(midi
, "%s()\n", __func__
);
714 static void f_midi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
716 struct f_midi
*midi
= substream
->rmidi
->private_data
;
718 if (substream
->number
>= midi
->in_ports
)
721 VDBG(midi
, "%s() %d\n", __func__
, up
);
722 midi
->in_ports_array
[substream
->number
].active
= up
;
724 tasklet_hi_schedule(&midi
->tasklet
);
727 static int f_midi_out_open(struct snd_rawmidi_substream
*substream
)
729 struct f_midi
*midi
= substream
->rmidi
->private_data
;
731 if (substream
->number
>= MAX_PORTS
)
734 VDBG(midi
, "%s()\n", __func__
);
735 midi
->out_substream
[substream
->number
] = substream
;
739 static int f_midi_out_close(struct snd_rawmidi_substream
*substream
)
741 struct f_midi
*midi
= substream
->rmidi
->private_data
;
743 VDBG(midi
, "%s()\n", __func__
);
747 static void f_midi_out_trigger(struct snd_rawmidi_substream
*substream
, int up
)
749 struct f_midi
*midi
= substream
->rmidi
->private_data
;
751 VDBG(midi
, "%s()\n", __func__
);
754 set_bit(substream
->number
, &midi
->out_triggered
);
756 clear_bit(substream
->number
, &midi
->out_triggered
);
759 static struct snd_rawmidi_ops gmidi_in_ops
= {
760 .open
= f_midi_in_open
,
761 .close
= f_midi_in_close
,
762 .trigger
= f_midi_in_trigger
,
765 static struct snd_rawmidi_ops gmidi_out_ops
= {
766 .open
= f_midi_out_open
,
767 .close
= f_midi_out_close
,
768 .trigger
= f_midi_out_trigger
771 static inline void f_midi_unregister_card(struct f_midi
*midi
)
774 snd_card_free(midi
->card
);
779 /* register as a sound "card" */
780 static int f_midi_register_card(struct f_midi
*midi
)
782 struct snd_card
*card
;
783 struct snd_rawmidi
*rmidi
;
785 static struct snd_device_ops ops
= {
786 .dev_free
= f_midi_snd_free
,
789 err
= snd_card_new(&midi
->gadget
->dev
, midi
->index
, midi
->id
,
790 THIS_MODULE
, 0, &card
);
792 ERROR(midi
, "snd_card_new() failed\n");
797 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, midi
, &ops
);
799 ERROR(midi
, "snd_device_new() failed: error %d\n", err
);
803 strcpy(card
->driver
, f_midi_longname
);
804 strcpy(card
->longname
, f_midi_longname
);
805 strcpy(card
->shortname
, f_midi_shortname
);
808 snd_component_add(card
, "MIDI");
809 err
= snd_rawmidi_new(card
, card
->longname
, 0,
810 midi
->out_ports
, midi
->in_ports
, &rmidi
);
812 ERROR(midi
, "snd_rawmidi_new() failed: error %d\n", err
);
816 midi
->in_last_port
= 0;
817 strcpy(rmidi
->name
, card
->shortname
);
818 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
819 SNDRV_RAWMIDI_INFO_INPUT
|
820 SNDRV_RAWMIDI_INFO_DUPLEX
;
821 rmidi
->private_data
= midi
;
824 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
825 * It's an upside-down world being a gadget.
827 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &gmidi_in_ops
);
828 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &gmidi_out_ops
);
830 /* register it - we're ready to go */
831 err
= snd_card_register(card
);
833 ERROR(midi
, "snd_card_register() failed\n");
837 VDBG(midi
, "%s() finished ok\n", __func__
);
841 f_midi_unregister_card(midi
);
845 /* MIDI function driver setup/binding */
847 static int f_midi_bind(struct usb_configuration
*c
, struct usb_function
*f
)
849 struct usb_descriptor_header
**midi_function
;
850 struct usb_midi_in_jack_descriptor jack_in_ext_desc
[MAX_PORTS
];
851 struct usb_midi_in_jack_descriptor jack_in_emb_desc
[MAX_PORTS
];
852 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc
[MAX_PORTS
];
853 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc
[MAX_PORTS
];
854 struct usb_composite_dev
*cdev
= c
->cdev
;
855 struct f_midi
*midi
= func_to_midi(f
);
856 struct usb_string
*us
;
857 int status
, n
, jack
= 1, i
= 0;
859 midi
->gadget
= cdev
->gadget
;
860 tasklet_init(&midi
->tasklet
, f_midi_in_tasklet
, (unsigned long) midi
);
861 status
= f_midi_register_card(midi
);
865 /* maybe allocate device-global string ID */
866 us
= usb_gstrings_attach(c
->cdev
, midi_strings
,
867 ARRAY_SIZE(midi_string_defs
));
869 status
= PTR_ERR(us
);
872 ac_interface_desc
.iInterface
= us
[STRING_FUNC_IDX
].id
;
874 /* We have two interfaces, AudioControl and MIDIStreaming */
875 status
= usb_interface_id(c
, f
);
878 ac_interface_desc
.bInterfaceNumber
= status
;
880 status
= usb_interface_id(c
, f
);
883 ms_interface_desc
.bInterfaceNumber
= status
;
884 ac_header_desc
.baInterfaceNr
[0] = status
;
885 midi
->ms_id
= status
;
889 /* allocate instance-specific endpoints */
890 midi
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_in_desc
);
894 midi
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &bulk_out_desc
);
898 /* allocate temporary function list */
899 midi_function
= kcalloc((MAX_PORTS
* 4) + 9, sizeof(*midi_function
),
901 if (!midi_function
) {
907 * construct the function's descriptor set. As the number of
908 * input and output MIDI ports is configurable, we have to do
912 /* add the headers - these are always the same */
913 midi_function
[i
++] = (struct usb_descriptor_header
*) &ac_interface_desc
;
914 midi_function
[i
++] = (struct usb_descriptor_header
*) &ac_header_desc
;
915 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_interface_desc
;
917 /* calculate the header's wTotalLength */
918 n
= USB_DT_MS_HEADER_SIZE
919 + (midi
->in_ports
+ midi
->out_ports
) *
920 (USB_DT_MIDI_IN_SIZE
+ USB_DT_MIDI_OUT_SIZE(1));
921 ms_header_desc
.wTotalLength
= cpu_to_le16(n
);
923 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_header_desc
;
925 /* configure the external IN jacks, each linked to an embedded OUT jack */
926 for (n
= 0; n
< midi
->in_ports
; n
++) {
927 struct usb_midi_in_jack_descriptor
*in_ext
= &jack_in_ext_desc
[n
];
928 struct usb_midi_out_jack_descriptor_1
*out_emb
= &jack_out_emb_desc
[n
];
930 in_ext
->bLength
= USB_DT_MIDI_IN_SIZE
;
931 in_ext
->bDescriptorType
= USB_DT_CS_INTERFACE
;
932 in_ext
->bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
;
933 in_ext
->bJackType
= USB_MS_EXTERNAL
;
934 in_ext
->bJackID
= jack
++;
936 midi_function
[i
++] = (struct usb_descriptor_header
*) in_ext
;
938 out_emb
->bLength
= USB_DT_MIDI_OUT_SIZE(1);
939 out_emb
->bDescriptorType
= USB_DT_CS_INTERFACE
;
940 out_emb
->bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
;
941 out_emb
->bJackType
= USB_MS_EMBEDDED
;
942 out_emb
->bJackID
= jack
++;
943 out_emb
->bNrInputPins
= 1;
944 out_emb
->pins
[0].baSourcePin
= 1;
945 out_emb
->pins
[0].baSourceID
= in_ext
->bJackID
;
947 midi_function
[i
++] = (struct usb_descriptor_header
*) out_emb
;
949 /* link it to the endpoint */
950 ms_in_desc
.baAssocJackID
[n
] = out_emb
->bJackID
;
953 /* configure the external OUT jacks, each linked to an embedded IN jack */
954 for (n
= 0; n
< midi
->out_ports
; n
++) {
955 struct usb_midi_in_jack_descriptor
*in_emb
= &jack_in_emb_desc
[n
];
956 struct usb_midi_out_jack_descriptor_1
*out_ext
= &jack_out_ext_desc
[n
];
958 in_emb
->bLength
= USB_DT_MIDI_IN_SIZE
;
959 in_emb
->bDescriptorType
= USB_DT_CS_INTERFACE
;
960 in_emb
->bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
;
961 in_emb
->bJackType
= USB_MS_EMBEDDED
;
962 in_emb
->bJackID
= jack
++;
964 midi_function
[i
++] = (struct usb_descriptor_header
*) in_emb
;
966 out_ext
->bLength
= USB_DT_MIDI_OUT_SIZE(1);
967 out_ext
->bDescriptorType
= USB_DT_CS_INTERFACE
;
968 out_ext
->bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
;
969 out_ext
->bJackType
= USB_MS_EXTERNAL
;
970 out_ext
->bJackID
= jack
++;
971 out_ext
->bNrInputPins
= 1;
973 out_ext
->pins
[0].baSourceID
= in_emb
->bJackID
;
974 out_ext
->pins
[0].baSourcePin
= 1;
975 midi_function
[i
++] = (struct usb_descriptor_header
*) out_ext
;
977 /* link it to the endpoint */
978 ms_out_desc
.baAssocJackID
[n
] = in_emb
->bJackID
;
981 /* configure the endpoint descriptors ... */
982 ms_out_desc
.bLength
= USB_DT_MS_ENDPOINT_SIZE(midi
->in_ports
);
983 ms_out_desc
.bNumEmbMIDIJack
= midi
->in_ports
;
985 ms_in_desc
.bLength
= USB_DT_MS_ENDPOINT_SIZE(midi
->out_ports
);
986 ms_in_desc
.bNumEmbMIDIJack
= midi
->out_ports
;
988 /* ... and add them to the list */
989 midi_function
[i
++] = (struct usb_descriptor_header
*) &bulk_out_desc
;
990 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_out_desc
;
991 midi_function
[i
++] = (struct usb_descriptor_header
*) &bulk_in_desc
;
992 midi_function
[i
++] = (struct usb_descriptor_header
*) &ms_in_desc
;
993 midi_function
[i
++] = NULL
;
996 * support all relevant hardware speeds... we expect that when
997 * hardware is dual speed, all bulk-capable endpoints work at
1000 /* copy descriptors, and track endpoint copies */
1001 f
->fs_descriptors
= usb_copy_descriptors(midi_function
);
1002 if (!f
->fs_descriptors
)
1005 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
1006 bulk_in_desc
.wMaxPacketSize
= cpu_to_le16(512);
1007 bulk_out_desc
.wMaxPacketSize
= cpu_to_le16(512);
1008 f
->hs_descriptors
= usb_copy_descriptors(midi_function
);
1009 if (!f
->hs_descriptors
)
1013 kfree(midi_function
);
1018 kfree(midi_function
);
1019 usb_free_descriptors(f
->hs_descriptors
);
1021 f_midi_unregister_card(midi
);
1023 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
1028 static inline struct f_midi_opts
*to_f_midi_opts(struct config_item
*item
)
1030 return container_of(to_config_group(item
), struct f_midi_opts
,
1034 static void midi_attr_release(struct config_item
*item
)
1036 struct f_midi_opts
*opts
= to_f_midi_opts(item
);
1038 usb_put_function_instance(&opts
->func_inst
);
1041 static struct configfs_item_operations midi_item_ops
= {
1042 .release
= midi_attr_release
,
1045 #define F_MIDI_OPT(name, test_limit, limit) \
1046 static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
1048 struct f_midi_opts *opts = to_f_midi_opts(item); \
1051 mutex_lock(&opts->lock); \
1052 result = sprintf(page, "%d\n", opts->name); \
1053 mutex_unlock(&opts->lock); \
1058 static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
1059 const char *page, size_t len) \
1061 struct f_midi_opts *opts = to_f_midi_opts(item); \
1065 mutex_lock(&opts->lock); \
1066 if (opts->refcnt) { \
1071 ret = kstrtou32(page, 0, &num); \
1075 if (test_limit && num > limit) { \
1083 mutex_unlock(&opts->lock); \
1087 CONFIGFS_ATTR(f_midi_opts_, name);
1089 F_MIDI_OPT(index
, true, SNDRV_CARDS
);
1090 F_MIDI_OPT(buflen
, false, 0);
1091 F_MIDI_OPT(qlen
, false, 0);
1092 F_MIDI_OPT(in_ports
, true, MAX_PORTS
);
1093 F_MIDI_OPT(out_ports
, true, MAX_PORTS
);
1095 static ssize_t
f_midi_opts_id_show(struct config_item
*item
, char *page
)
1097 struct f_midi_opts
*opts
= to_f_midi_opts(item
);
1100 mutex_lock(&opts
->lock
);
1102 result
= strlcpy(page
, opts
->id
, PAGE_SIZE
);
1108 mutex_unlock(&opts
->lock
);
1113 static ssize_t
f_midi_opts_id_store(struct config_item
*item
,
1114 const char *page
, size_t len
)
1116 struct f_midi_opts
*opts
= to_f_midi_opts(item
);
1120 mutex_lock(&opts
->lock
);
1126 c
= kstrndup(page
, len
, GFP_KERNEL
);
1131 if (opts
->id_allocated
)
1134 opts
->id_allocated
= true;
1137 mutex_unlock(&opts
->lock
);
1141 CONFIGFS_ATTR(f_midi_opts_
, id
);
1143 static struct configfs_attribute
*midi_attrs
[] = {
1144 &f_midi_opts_attr_index
,
1145 &f_midi_opts_attr_buflen
,
1146 &f_midi_opts_attr_qlen
,
1147 &f_midi_opts_attr_in_ports
,
1148 &f_midi_opts_attr_out_ports
,
1149 &f_midi_opts_attr_id
,
1153 static struct config_item_type midi_func_type
= {
1154 .ct_item_ops
= &midi_item_ops
,
1155 .ct_attrs
= midi_attrs
,
1156 .ct_owner
= THIS_MODULE
,
1159 static void f_midi_free_inst(struct usb_function_instance
*f
)
1161 struct f_midi_opts
*opts
;
1163 opts
= container_of(f
, struct f_midi_opts
, func_inst
);
1165 if (opts
->id_allocated
)
1171 static struct usb_function_instance
*f_midi_alloc_inst(void)
1173 struct f_midi_opts
*opts
;
1175 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
1177 return ERR_PTR(-ENOMEM
);
1179 mutex_init(&opts
->lock
);
1180 opts
->func_inst
.free_func_inst
= f_midi_free_inst
;
1181 opts
->index
= SNDRV_DEFAULT_IDX1
;
1182 opts
->id
= SNDRV_DEFAULT_STR1
;
1186 opts
->out_ports
= 1;
1188 config_group_init_type_name(&opts
->func_inst
.group
, "",
1191 return &opts
->func_inst
;
1194 static void f_midi_free(struct usb_function
*f
)
1196 struct f_midi
*midi
;
1197 struct f_midi_opts
*opts
;
1199 midi
= func_to_midi(f
);
1200 opts
= container_of(f
->fi
, struct f_midi_opts
, func_inst
);
1202 mutex_lock(&opts
->lock
);
1203 kfifo_free(&midi
->in_req_fifo
);
1206 mutex_unlock(&opts
->lock
);
1209 static void f_midi_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
1211 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
1212 struct f_midi
*midi
= func_to_midi(f
);
1213 struct snd_card
*card
;
1215 DBG(cdev
, "unbind\n");
1217 /* just to be sure */
1223 snd_card_free(card
);
1225 usb_free_all_descriptors(f
);
1228 static struct usb_function
*f_midi_alloc(struct usb_function_instance
*fi
)
1230 struct f_midi
*midi
= NULL
;
1231 struct f_midi_opts
*opts
;
1234 opts
= container_of(fi
, struct f_midi_opts
, func_inst
);
1236 mutex_lock(&opts
->lock
);
1238 if (opts
->in_ports
> MAX_PORTS
|| opts
->out_ports
> MAX_PORTS
) {
1243 /* allocate and initialize one new instance */
1245 sizeof(*midi
) + opts
->in_ports
* sizeof(*midi
->in_ports_array
),
1252 for (i
= 0; i
< opts
->in_ports
; i
++)
1253 midi
->in_ports_array
[i
].cable
= i
;
1255 /* set up ALSA midi devices */
1256 midi
->id
= kstrdup(opts
->id
, GFP_KERNEL
);
1257 if (opts
->id
&& !midi
->id
) {
1261 midi
->in_ports
= opts
->in_ports
;
1262 midi
->out_ports
= opts
->out_ports
;
1263 midi
->index
= opts
->index
;
1264 midi
->buflen
= opts
->buflen
;
1265 midi
->qlen
= opts
->qlen
;
1266 midi
->in_last_port
= 0;
1268 status
= kfifo_alloc(&midi
->in_req_fifo
, midi
->qlen
, GFP_KERNEL
);
1272 spin_lock_init(&midi
->transmit_lock
);
1275 mutex_unlock(&opts
->lock
);
1277 midi
->func
.name
= "gmidi function";
1278 midi
->func
.bind
= f_midi_bind
;
1279 midi
->func
.unbind
= f_midi_unbind
;
1280 midi
->func
.set_alt
= f_midi_set_alt
;
1281 midi
->func
.disable
= f_midi_disable
;
1282 midi
->func
.free_func
= f_midi_free
;
1287 mutex_unlock(&opts
->lock
);
1289 return ERR_PTR(status
);
1292 DECLARE_USB_FUNCTION_INIT(midi
, f_midi_alloc_inst
, f_midi_alloc
);