2 * gmidi.c -- USB MIDI Gadget Driver
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
8 * This software is distributed under the terms of the GNU General Public
9 * License ("GPL") version 2, as published by the Free Software Foundation.
11 * This code is based in part on:
13 * Gadget Zero driver, Copyright (C) 2003-2004 David Brownell.
14 * USB Audio driver, Copyright (C) 2002 by Takashi Iwai.
15 * USB MIDI driver, Copyright (C) 2002-2005 Clemens Ladisch.
17 * Refer to the USB Device Class Definition for MIDI Devices:
18 * http://www.usb.org/developers/devclass_docs/midi10.pdf
21 /* #define VERBOSE_DEBUG */
23 #include <linux/kernel.h>
24 #include <linux/utsname.h>
25 #include <linux/device.h>
27 #include <sound/core.h>
28 #include <sound/initval.h>
29 #include <sound/rawmidi.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb/audio.h>
34 #include <linux/usb/midi.h>
36 #include "gadget_chips.h"
38 MODULE_AUTHOR("Ben Williamson");
39 MODULE_LICENSE("GPL v2");
41 #define DRIVER_VERSION "25 Jul 2006"
43 static const char shortname
[] = "g_midi";
44 static const char longname
[] = "MIDI Gadget";
46 static int index
= SNDRV_DEFAULT_IDX1
;
47 static char *id
= SNDRV_DEFAULT_STR1
;
49 module_param(index
, int, 0444);
50 MODULE_PARM_DESC(index
, "Index value for the USB MIDI Gadget adapter.");
51 module_param(id
, charp
, 0444);
52 MODULE_PARM_DESC(id
, "ID string for the USB MIDI Gadget adapter.");
54 /* Some systems will want different product identifers published in the
55 * device descriptor, either numbers or strings or both. These string
56 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
59 static ushort idVendor
;
60 module_param(idVendor
, ushort
, S_IRUGO
);
61 MODULE_PARM_DESC(idVendor
, "USB Vendor ID");
63 static ushort idProduct
;
64 module_param(idProduct
, ushort
, S_IRUGO
);
65 MODULE_PARM_DESC(idProduct
, "USB Product ID");
67 static ushort bcdDevice
;
68 module_param(bcdDevice
, ushort
, S_IRUGO
);
69 MODULE_PARM_DESC(bcdDevice
, "USB Device version (BCD)");
71 static char *iManufacturer
;
72 module_param(iManufacturer
, charp
, S_IRUGO
);
73 MODULE_PARM_DESC(iManufacturer
, "USB Manufacturer string");
75 static char *iProduct
;
76 module_param(iProduct
, charp
, S_IRUGO
);
77 MODULE_PARM_DESC(iProduct
, "USB Product string");
79 static char *iSerialNumber
;
80 module_param(iSerialNumber
, charp
, S_IRUGO
);
81 MODULE_PARM_DESC(iSerialNumber
, "SerialNumber");
84 * this version autoconfigures as much as possible,
85 * which is reasonable for most "bulk-only" drivers.
87 static const char *EP_IN_NAME
;
88 static const char *EP_OUT_NAME
;
91 /* big enough to hold our biggest descriptor */
92 #define USB_BUFSIZ 256
95 /* This is a gadget, and the IN/OUT naming is from the host's perspective.
96 USB -> OUT endpoint -> rawmidi
97 USB <- IN endpoint <- rawmidi */
98 struct gmidi_in_port
{
99 struct gmidi_device
* dev
;
101 uint8_t cable
; /* cable number << 4 */
103 #define STATE_UNKNOWN 0
104 #define STATE_1PARAM 1
105 #define STATE_2PARAM_1 2
106 #define STATE_2PARAM_2 3
107 #define STATE_SYSEX_0 4
108 #define STATE_SYSEX_1 5
109 #define STATE_SYSEX_2 6
113 struct gmidi_device
{
115 struct usb_gadget
*gadget
;
116 struct usb_request
*req
; /* for control responses */
118 struct usb_ep
*in_ep
, *out_ep
;
119 struct snd_card
*card
;
120 struct snd_rawmidi
*rmidi
;
121 struct snd_rawmidi_substream
*in_substream
;
122 struct snd_rawmidi_substream
*out_substream
;
124 /* For the moment we only support one port in
125 each direction, but in_port is kept as a
126 separate struct so we can have more later. */
127 struct gmidi_in_port in_port
;
128 unsigned long out_triggered
;
129 struct tasklet_struct tasklet
;
132 static void gmidi_transmit(struct gmidi_device
* dev
, struct usb_request
* req
);
135 #define DBG(d, fmt, args...) \
136 dev_dbg(&(d)->gadget->dev , fmt , ## args)
137 #define VDBG(d, fmt, args...) \
138 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
139 #define ERROR(d, fmt, args...) \
140 dev_err(&(d)->gadget->dev , fmt , ## args)
141 #define INFO(d, fmt, args...) \
142 dev_info(&(d)->gadget->dev , fmt , ## args)
145 static unsigned buflen
= 256;
146 static unsigned qlen
= 32;
148 module_param(buflen
, uint
, S_IRUGO
);
149 module_param(qlen
, uint
, S_IRUGO
);
152 /* Thanks to Grey Innovation for donating this product ID.
154 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
155 * Instead: allocate your own, using normal USB-IF procedures.
157 #define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
158 #define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
162 * DESCRIPTORS ... most are static, but strings and (full)
163 * configuration descriptors are built on demand.
166 #define STRING_MANUFACTURER 25
167 #define STRING_PRODUCT 42
168 #define STRING_SERIAL 101
169 #define STRING_MIDI_GADGET 250
171 /* We only have the one configuration, it's number 1. */
172 #define GMIDI_CONFIG 1
174 /* We have two interfaces- AudioControl and MIDIStreaming */
175 #define GMIDI_AC_INTERFACE 0
176 #define GMIDI_MS_INTERFACE 1
177 #define GMIDI_NUM_INTERFACES 2
179 DECLARE_USB_AC_HEADER_DESCRIPTOR(1);
180 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
181 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1);
183 /* B.1 Device Descriptor */
184 static struct usb_device_descriptor device_desc
= {
185 .bLength
= USB_DT_DEVICE_SIZE
,
186 .bDescriptorType
= USB_DT_DEVICE
,
187 .bcdUSB
= __constant_cpu_to_le16(0x0200),
188 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
189 .idVendor
= __constant_cpu_to_le16(DRIVER_VENDOR_NUM
),
190 .idProduct
= __constant_cpu_to_le16(DRIVER_PRODUCT_NUM
),
191 .iManufacturer
= STRING_MANUFACTURER
,
192 .iProduct
= STRING_PRODUCT
,
193 .bNumConfigurations
= 1,
196 /* B.2 Configuration Descriptor */
197 static struct usb_config_descriptor config_desc
= {
198 .bLength
= USB_DT_CONFIG_SIZE
,
199 .bDescriptorType
= USB_DT_CONFIG
,
200 /* compute wTotalLength on the fly */
201 .bNumInterfaces
= GMIDI_NUM_INTERFACES
,
202 .bConfigurationValue
= GMIDI_CONFIG
,
203 .iConfiguration
= STRING_MIDI_GADGET
,
205 * FIXME: When embedding this driver in a device,
206 * these need to be set to reflect the actual
207 * power properties of the device. Is it selfpowered?
209 .bmAttributes
= USB_CONFIG_ATT_ONE
,
213 /* B.3.1 Standard AC Interface Descriptor */
214 static const struct usb_interface_descriptor ac_interface_desc
= {
215 .bLength
= USB_DT_INTERFACE_SIZE
,
216 .bDescriptorType
= USB_DT_INTERFACE
,
217 .bInterfaceNumber
= GMIDI_AC_INTERFACE
,
219 .bInterfaceClass
= USB_CLASS_AUDIO
,
220 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
221 .iInterface
= STRING_MIDI_GADGET
,
224 /* B.3.2 Class-Specific AC Interface Descriptor */
225 static const struct usb_ac_header_descriptor_1 ac_header_desc
= {
226 .bLength
= USB_DT_AC_HEADER_SIZE(1),
227 .bDescriptorType
= USB_DT_CS_INTERFACE
,
228 .bDescriptorSubtype
= USB_MS_HEADER
,
229 .bcdADC
= __constant_cpu_to_le16(0x0100),
230 .wTotalLength
= __constant_cpu_to_le16(USB_DT_AC_HEADER_SIZE(1)),
233 [0] = GMIDI_MS_INTERFACE
,
237 /* B.4.1 Standard MS Interface Descriptor */
238 static const struct usb_interface_descriptor ms_interface_desc
= {
239 .bLength
= USB_DT_INTERFACE_SIZE
,
240 .bDescriptorType
= USB_DT_INTERFACE
,
241 .bInterfaceNumber
= GMIDI_MS_INTERFACE
,
243 .bInterfaceClass
= USB_CLASS_AUDIO
,
244 .bInterfaceSubClass
= USB_SUBCLASS_MIDISTREAMING
,
245 .iInterface
= STRING_MIDI_GADGET
,
248 /* B.4.2 Class-Specific MS Interface Descriptor */
249 static const struct usb_ms_header_descriptor ms_header_desc
= {
250 .bLength
= USB_DT_MS_HEADER_SIZE
,
251 .bDescriptorType
= USB_DT_CS_INTERFACE
,
252 .bDescriptorSubtype
= USB_MS_HEADER
,
253 .bcdMSC
= __constant_cpu_to_le16(0x0100),
254 .wTotalLength
= __constant_cpu_to_le16(USB_DT_MS_HEADER_SIZE
255 + 2*USB_DT_MIDI_IN_SIZE
256 + 2*USB_DT_MIDI_OUT_SIZE(1)),
259 #define JACK_IN_EMB 1
260 #define JACK_IN_EXT 2
261 #define JACK_OUT_EMB 3
262 #define JACK_OUT_EXT 4
264 /* B.4.3 MIDI IN Jack Descriptors */
265 static const struct usb_midi_in_jack_descriptor jack_in_emb_desc
= {
266 .bLength
= USB_DT_MIDI_IN_SIZE
,
267 .bDescriptorType
= USB_DT_CS_INTERFACE
,
268 .bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
,
269 .bJackType
= USB_MS_EMBEDDED
,
270 .bJackID
= JACK_IN_EMB
,
273 static const struct usb_midi_in_jack_descriptor jack_in_ext_desc
= {
274 .bLength
= USB_DT_MIDI_IN_SIZE
,
275 .bDescriptorType
= USB_DT_CS_INTERFACE
,
276 .bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
,
277 .bJackType
= USB_MS_EXTERNAL
,
278 .bJackID
= JACK_IN_EXT
,
281 /* B.4.4 MIDI OUT Jack Descriptors */
282 static const struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc
= {
283 .bLength
= USB_DT_MIDI_OUT_SIZE(1),
284 .bDescriptorType
= USB_DT_CS_INTERFACE
,
285 .bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
,
286 .bJackType
= USB_MS_EMBEDDED
,
287 .bJackID
= JACK_OUT_EMB
,
291 .baSourceID
= JACK_IN_EXT
,
297 static const struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc
= {
298 .bLength
= USB_DT_MIDI_OUT_SIZE(1),
299 .bDescriptorType
= USB_DT_CS_INTERFACE
,
300 .bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
,
301 .bJackType
= USB_MS_EXTERNAL
,
302 .bJackID
= JACK_OUT_EXT
,
306 .baSourceID
= JACK_IN_EMB
,
312 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
313 static struct usb_endpoint_descriptor bulk_out_desc
= {
314 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
315 .bDescriptorType
= USB_DT_ENDPOINT
,
316 .bEndpointAddress
= USB_DIR_OUT
,
317 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
320 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
321 static const struct usb_ms_endpoint_descriptor_1 ms_out_desc
= {
322 .bLength
= USB_DT_MS_ENDPOINT_SIZE(1),
323 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
324 .bDescriptorSubtype
= USB_MS_GENERAL
,
325 .bNumEmbMIDIJack
= 1,
331 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
332 static struct usb_endpoint_descriptor bulk_in_desc
= {
333 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
334 .bDescriptorType
= USB_DT_ENDPOINT
,
335 .bEndpointAddress
= USB_DIR_IN
,
336 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
339 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
340 static const struct usb_ms_endpoint_descriptor_1 ms_in_desc
= {
341 .bLength
= USB_DT_MS_ENDPOINT_SIZE(1),
342 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
343 .bDescriptorSubtype
= USB_MS_GENERAL
,
344 .bNumEmbMIDIJack
= 1,
350 static const struct usb_descriptor_header
*gmidi_function
[] = {
351 (struct usb_descriptor_header
*)&ac_interface_desc
,
352 (struct usb_descriptor_header
*)&ac_header_desc
,
353 (struct usb_descriptor_header
*)&ms_interface_desc
,
355 (struct usb_descriptor_header
*)&ms_header_desc
,
356 (struct usb_descriptor_header
*)&jack_in_emb_desc
,
357 (struct usb_descriptor_header
*)&jack_in_ext_desc
,
358 (struct usb_descriptor_header
*)&jack_out_emb_desc
,
359 (struct usb_descriptor_header
*)&jack_out_ext_desc
,
360 /* If you add more jacks, update ms_header_desc.wTotalLength */
362 (struct usb_descriptor_header
*)&bulk_out_desc
,
363 (struct usb_descriptor_header
*)&ms_out_desc
,
364 (struct usb_descriptor_header
*)&bulk_in_desc
,
365 (struct usb_descriptor_header
*)&ms_in_desc
,
369 static char manufacturer
[50];
370 static char product_desc
[40] = "MIDI Gadget";
371 static char serial_number
[20];
373 /* static strings, in UTF-8 */
374 static struct usb_string strings
[] = {
375 { STRING_MANUFACTURER
, manufacturer
, },
376 { STRING_PRODUCT
, product_desc
, },
377 { STRING_SERIAL
, serial_number
, },
378 { STRING_MIDI_GADGET
, longname
, },
379 { } /* end of list */
382 static struct usb_gadget_strings stringtab
= {
383 .language
= 0x0409, /* en-us */
387 static int config_buf(struct usb_gadget
*gadget
,
388 u8
*buf
, u8 type
, unsigned index
)
392 /* only one configuration */
396 len
= usb_gadget_config_buf(&config_desc
,
397 buf
, USB_BUFSIZ
, gmidi_function
);
401 ((struct usb_config_descriptor
*)buf
)->bDescriptorType
= type
;
405 static struct usb_request
*alloc_ep_req(struct usb_ep
*ep
, unsigned length
)
407 struct usb_request
*req
;
409 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
411 req
->length
= length
;
412 req
->buf
= kmalloc(length
, GFP_ATOMIC
);
414 usb_ep_free_request(ep
, req
);
421 static void free_ep_req(struct usb_ep
*ep
, struct usb_request
*req
)
424 usb_ep_free_request(ep
, req
);
427 static const uint8_t gmidi_cin_length
[] = {
428 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
432 * Receives a chunk of MIDI data.
434 static void gmidi_read_data(struct usb_ep
*ep
, int cable
,
435 uint8_t *data
, int length
)
437 struct gmidi_device
*dev
= ep
->driver_data
;
438 /* cable is ignored, because for now we only have one. */
440 if (!dev
->out_substream
) {
441 /* Nobody is listening - throw it on the floor. */
444 if (!test_bit(dev
->out_substream
->number
, &dev
->out_triggered
)) {
447 snd_rawmidi_receive(dev
->out_substream
, data
, length
);
450 static void gmidi_handle_out_data(struct usb_ep
*ep
, struct usb_request
*req
)
455 for (i
= 0; i
+ 3 < req
->actual
; i
+= 4) {
457 int cable
= buf
[i
] >> 4;
458 int length
= gmidi_cin_length
[buf
[i
] & 0x0f];
459 gmidi_read_data(ep
, cable
, &buf
[i
+ 1], length
);
464 static void gmidi_complete(struct usb_ep
*ep
, struct usb_request
*req
)
466 struct gmidi_device
*dev
= ep
->driver_data
;
467 int status
= req
->status
;
470 case 0: /* normal completion */
471 if (ep
== dev
->out_ep
) {
472 /* we received stuff.
473 req is queued again, below */
474 gmidi_handle_out_data(ep
, req
);
475 } else if (ep
== dev
->in_ep
) {
476 /* our transmit completed.
477 see if there's more to go.
478 gmidi_transmit eats req, don't queue it again. */
479 gmidi_transmit(dev
, req
);
484 /* this endpoint is normally active while we're configured */
485 case -ECONNABORTED
: /* hardware forced ep reset */
486 case -ECONNRESET
: /* request dequeued */
487 case -ESHUTDOWN
: /* disconnect from host */
488 VDBG(dev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
489 req
->actual
, req
->length
);
490 if (ep
== dev
->out_ep
) {
491 gmidi_handle_out_data(ep
, req
);
493 free_ep_req(ep
, req
);
496 case -EOVERFLOW
: /* buffer overrun on read means that
497 * we didn't provide a big enough
501 DBG(dev
, "%s complete --> %d, %d/%d\n", ep
->name
,
502 status
, req
->actual
, req
->length
);
504 case -EREMOTEIO
: /* short read */
508 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
510 ERROR(dev
, "kill %s: resubmit %d bytes --> %d\n",
511 ep
->name
, req
->length
, status
);
513 /* FIXME recover later ... somehow */
517 static int set_gmidi_config(struct gmidi_device
*dev
, gfp_t gfp_flags
)
520 struct usb_request
*req
;
524 err
= usb_ep_enable(dev
->in_ep
, &bulk_in_desc
);
526 ERROR(dev
, "can't start %s: %d\n", dev
->in_ep
->name
, err
);
529 dev
->in_ep
->driver_data
= dev
;
531 err
= usb_ep_enable(dev
->out_ep
, &bulk_out_desc
);
533 ERROR(dev
, "can't start %s: %d\n", dev
->out_ep
->name
, err
);
536 dev
->out_ep
->driver_data
= dev
;
538 /* allocate a bunch of read buffers and queue them all at once. */
540 for (i
= 0; i
< qlen
&& err
== 0; i
++) {
541 req
= alloc_ep_req(ep
, buflen
);
543 req
->complete
= gmidi_complete
;
544 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
546 DBG(dev
, "%s queue req: %d\n", ep
->name
, err
);
553 /* caller is responsible for cleanup on error */
558 static void gmidi_reset_config(struct gmidi_device
*dev
)
560 if (dev
->config
== 0) {
564 DBG(dev
, "reset config\n");
566 /* just disable endpoints, forcing completion of pending i/o.
567 * all our completion handlers free their requests in this case.
569 usb_ep_disable(dev
->in_ep
);
570 usb_ep_disable(dev
->out_ep
);
574 /* change our operational config. this code must agree with the code
575 * that returns config descriptors, and altsetting code.
577 * it's also responsible for power management interactions. some
578 * configurations might not work with our current power sources.
580 * note that some device controller hardware will constrain what this
581 * code can do, perhaps by disallowing more than one configuration or
582 * by limiting configuration choices (like the pxa2xx).
585 gmidi_set_config(struct gmidi_device
*dev
, unsigned number
, gfp_t gfp_flags
)
588 struct usb_gadget
*gadget
= dev
->gadget
;
592 /* Hacking this bit out fixes a bug where on receipt of two
593 USB_REQ_SET_CONFIGURATION messages, we end up with no
594 buffered OUT requests waiting for data. This is clearly
595 hiding a bug elsewhere, because if the config didn't
596 change then we really shouldn't do anything. */
597 /* Having said that, when we do "change" from config 1
598 to config 1, we at least gmidi_reset_config() which
599 clears out any requests on endpoints, so it's not like
600 we leak or anything. */
601 if (number
== dev
->config
) {
606 if (gadget_is_sa1100(gadget
) && dev
->config
) {
607 /* tx fifo is full, but we can't clear it...*/
608 ERROR(dev
, "can't change configurations\n");
611 gmidi_reset_config(dev
);
615 result
= set_gmidi_config(dev
, gfp_flags
);
624 if (!result
&& (!dev
->in_ep
|| !dev
->out_ep
)) {
628 gmidi_reset_config(dev
);
632 switch (gadget
->speed
) {
633 case USB_SPEED_LOW
: speed
= "low"; break;
634 case USB_SPEED_FULL
: speed
= "full"; break;
635 case USB_SPEED_HIGH
: speed
= "high"; break;
636 default: speed
= "?"; break;
639 dev
->config
= number
;
640 INFO(dev
, "%s speed\n", speed
);
646 static void gmidi_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
648 if (req
->status
|| req
->actual
!= req
->length
) {
649 DBG((struct gmidi_device
*) ep
->driver_data
,
650 "setup complete --> %d, %d/%d\n",
651 req
->status
, req
->actual
, req
->length
);
656 * The setup() callback implements all the ep0 functionality that's
657 * not handled lower down, in hardware or the hardware driver (like
658 * device and endpoint feature flags, and their status). It's all
659 * housekeeping for the gadget function we're implementing. Most of
660 * the work is in config-specific setup.
662 static int gmidi_setup(struct usb_gadget
*gadget
,
663 const struct usb_ctrlrequest
*ctrl
)
665 struct gmidi_device
*dev
= get_gadget_data(gadget
);
666 struct usb_request
*req
= dev
->req
;
667 int value
= -EOPNOTSUPP
;
668 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
669 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
670 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
672 /* usually this stores reply data in the pre-allocated ep0 buffer,
673 * but config change events will reconfigure hardware.
676 switch (ctrl
->bRequest
) {
678 case USB_REQ_GET_DESCRIPTOR
:
679 if (ctrl
->bRequestType
!= USB_DIR_IN
) {
682 switch (w_value
>> 8) {
685 value
= min(w_length
, (u16
) sizeof(device_desc
));
686 memcpy(req
->buf
, &device_desc
, value
);
689 value
= config_buf(gadget
, req
->buf
,
693 value
= min(w_length
, (u16
)value
);
698 /* wIndex == language code.
699 * this driver only handles one language, you can
700 * add string tables for other languages, using
701 * any UTF-8 characters
703 value
= usb_gadget_get_string(&stringtab
,
704 w_value
& 0xff, req
->buf
);
706 value
= min(w_length
, (u16
)value
);
712 /* currently two configs, two speeds */
713 case USB_REQ_SET_CONFIGURATION
:
714 if (ctrl
->bRequestType
!= 0) {
717 if (gadget
->a_hnp_support
) {
718 DBG(dev
, "HNP available\n");
719 } else if (gadget
->a_alt_hnp_support
) {
720 DBG(dev
, "HNP needs a different root port\n");
722 VDBG(dev
, "HNP inactive\n");
724 spin_lock(&dev
->lock
);
725 value
= gmidi_set_config(dev
, w_value
, GFP_ATOMIC
);
726 spin_unlock(&dev
->lock
);
728 case USB_REQ_GET_CONFIGURATION
:
729 if (ctrl
->bRequestType
!= USB_DIR_IN
) {
732 *(u8
*)req
->buf
= dev
->config
;
733 value
= min(w_length
, (u16
)1);
736 /* until we add altsetting support, or other interfaces,
737 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
738 * and already killed pending endpoint I/O.
740 case USB_REQ_SET_INTERFACE
:
741 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
) {
744 spin_lock(&dev
->lock
);
745 if (dev
->config
&& w_index
< GMIDI_NUM_INTERFACES
748 u8 config
= dev
->config
;
750 /* resets interface configuration, forgets about
751 * previous transaction state (queued bufs, etc)
752 * and re-inits endpoint state (toggle etc)
753 * no response queued, just zero status == success.
754 * if we had more than one interface we couldn't
755 * use this "reset the config" shortcut.
757 gmidi_reset_config(dev
);
758 gmidi_set_config(dev
, config
, GFP_ATOMIC
);
761 spin_unlock(&dev
->lock
);
763 case USB_REQ_GET_INTERFACE
:
764 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
)) {
770 if (w_index
>= GMIDI_NUM_INTERFACES
) {
775 value
= min(w_length
, (u16
)1);
780 VDBG(dev
, "unknown control req%02x.%02x v%04x i%04x l%d\n",
781 ctrl
->bRequestType
, ctrl
->bRequest
,
782 w_value
, w_index
, w_length
);
785 /* respond with data transfer before status phase? */
788 req
->zero
= value
< w_length
;
789 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
791 DBG(dev
, "ep_queue --> %d\n", value
);
793 gmidi_setup_complete(gadget
->ep0
, req
);
797 /* device either stalls (value < 0) or reports success */
801 static void gmidi_disconnect(struct usb_gadget
*gadget
)
803 struct gmidi_device
*dev
= get_gadget_data(gadget
);
806 spin_lock_irqsave(&dev
->lock
, flags
);
807 gmidi_reset_config(dev
);
809 /* a more significant application might have some non-usb
810 * activities to quiesce here, saving resources like power
811 * or pushing the notification up a network stack.
813 spin_unlock_irqrestore(&dev
->lock
, flags
);
815 /* next we may get setup() calls to enumerate new connections;
816 * or an unbind() during shutdown (including removing module).
820 static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget
*gadget
)
822 struct gmidi_device
*dev
= get_gadget_data(gadget
);
823 struct snd_card
*card
;
825 DBG(dev
, "unbind\n");
833 /* we've already been disconnected ... no i/o is active */
835 dev
->req
->length
= USB_BUFSIZ
;
836 free_ep_req(gadget
->ep0
, dev
->req
);
839 set_gadget_data(gadget
, NULL
);
842 static int gmidi_snd_free(struct snd_device
*device
)
847 static void gmidi_transmit_packet(struct usb_request
*req
, uint8_t p0
,
848 uint8_t p1
, uint8_t p2
, uint8_t p3
)
850 unsigned length
= req
->length
;
851 u8
*buf
= (u8
*)req
->buf
+ length
;
857 req
->length
= length
+ 4;
861 * Converts MIDI commands to USB MIDI packets.
863 static void gmidi_transmit_byte(struct usb_request
*req
,
864 struct gmidi_in_port
*port
, uint8_t b
)
866 uint8_t p0
= port
->cable
;
869 gmidi_transmit_packet(req
, p0
| 0x0f, b
, 0, 0);
870 } else if (b
>= 0xf0) {
874 port
->state
= STATE_SYSEX_1
;
879 port
->state
= STATE_1PARAM
;
883 port
->state
= STATE_2PARAM_1
;
887 port
->state
= STATE_UNKNOWN
;
890 gmidi_transmit_packet(req
, p0
| 0x05, 0xf6, 0, 0);
891 port
->state
= STATE_UNKNOWN
;
894 switch (port
->state
) {
896 gmidi_transmit_packet(req
,
897 p0
| 0x05, 0xf7, 0, 0);
900 gmidi_transmit_packet(req
,
901 p0
| 0x06, port
->data
[0], 0xf7, 0);
904 gmidi_transmit_packet(req
,
905 p0
| 0x07, port
->data
[0],
906 port
->data
[1], 0xf7);
909 port
->state
= STATE_UNKNOWN
;
912 } else if (b
>= 0x80) {
914 if (b
>= 0xc0 && b
<= 0xdf)
915 port
->state
= STATE_1PARAM
;
917 port
->state
= STATE_2PARAM_1
;
918 } else { /* b < 0x80 */
919 switch (port
->state
) {
921 if (port
->data
[0] < 0xf0) {
922 p0
|= port
->data
[0] >> 4;
925 port
->state
= STATE_UNKNOWN
;
927 gmidi_transmit_packet(req
, p0
, port
->data
[0], b
, 0);
931 port
->state
= STATE_2PARAM_2
;
934 if (port
->data
[0] < 0xf0) {
935 p0
|= port
->data
[0] >> 4;
936 port
->state
= STATE_2PARAM_1
;
939 port
->state
= STATE_UNKNOWN
;
941 gmidi_transmit_packet(req
,
942 p0
, port
->data
[0], port
->data
[1], b
);
946 port
->state
= STATE_SYSEX_1
;
950 port
->state
= STATE_SYSEX_2
;
953 gmidi_transmit_packet(req
,
954 p0
| 0x04, port
->data
[0], port
->data
[1], b
);
955 port
->state
= STATE_SYSEX_0
;
961 static void gmidi_transmit(struct gmidi_device
*dev
, struct usb_request
*req
)
963 struct usb_ep
*ep
= dev
->in_ep
;
964 struct gmidi_in_port
*port
= &dev
->in_port
;
970 req
= alloc_ep_req(ep
, buflen
);
973 ERROR(dev
, "gmidi_transmit: alloc_ep_request failed\n");
977 req
->complete
= gmidi_complete
;
980 while (req
->length
+ 3 < buflen
) {
982 if (snd_rawmidi_transmit(dev
->in_substream
, &b
, 1)
988 gmidi_transmit_byte(req
, port
, b
);
991 if (req
->length
> 0) {
992 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
994 free_ep_req(ep
, req
);
998 static void gmidi_in_tasklet(unsigned long data
)
1000 struct gmidi_device
*dev
= (struct gmidi_device
*)data
;
1002 gmidi_transmit(dev
, NULL
);
1005 static int gmidi_in_open(struct snd_rawmidi_substream
*substream
)
1007 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1009 VDBG(dev
, "gmidi_in_open\n");
1010 dev
->in_substream
= substream
;
1011 dev
->in_port
.state
= STATE_UNKNOWN
;
1015 static int gmidi_in_close(struct snd_rawmidi_substream
*substream
)
1017 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1019 VDBG(dev
, "gmidi_in_close\n");
1023 static void gmidi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1025 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1027 VDBG(dev
, "gmidi_in_trigger %d\n", up
);
1028 dev
->in_port
.active
= up
;
1030 tasklet_hi_schedule(&dev
->tasklet
);
1034 static int gmidi_out_open(struct snd_rawmidi_substream
*substream
)
1036 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1038 VDBG(dev
, "gmidi_out_open\n");
1039 dev
->out_substream
= substream
;
1043 static int gmidi_out_close(struct snd_rawmidi_substream
*substream
)
1045 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1047 VDBG(dev
, "gmidi_out_close\n");
1051 static void gmidi_out_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1053 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1055 VDBG(dev
, "gmidi_out_trigger %d\n", up
);
1057 set_bit(substream
->number
, &dev
->out_triggered
);
1059 clear_bit(substream
->number
, &dev
->out_triggered
);
1063 static struct snd_rawmidi_ops gmidi_in_ops
= {
1064 .open
= gmidi_in_open
,
1065 .close
= gmidi_in_close
,
1066 .trigger
= gmidi_in_trigger
,
1069 static struct snd_rawmidi_ops gmidi_out_ops
= {
1070 .open
= gmidi_out_open
,
1071 .close
= gmidi_out_close
,
1072 .trigger
= gmidi_out_trigger
1075 /* register as a sound "card" */
1076 static int gmidi_register_card(struct gmidi_device
*dev
)
1078 struct snd_card
*card
;
1079 struct snd_rawmidi
*rmidi
;
1083 static struct snd_device_ops ops
= {
1084 .dev_free
= gmidi_snd_free
,
1087 card
= snd_card_new(index
, id
, THIS_MODULE
, 0);
1089 ERROR(dev
, "snd_card_new failed\n");
1095 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, dev
, &ops
);
1097 ERROR(dev
, "snd_device_new failed: error %d\n", err
);
1101 strcpy(card
->driver
, longname
);
1102 strcpy(card
->longname
, longname
);
1103 strcpy(card
->shortname
, shortname
);
1105 /* Set up rawmidi */
1106 dev
->in_port
.dev
= dev
;
1107 dev
->in_port
.active
= 0;
1108 snd_component_add(card
, "MIDI");
1109 err
= snd_rawmidi_new(card
, "USB MIDI Gadget", 0,
1110 out_ports
, in_ports
, &rmidi
);
1112 ERROR(dev
, "snd_rawmidi_new failed: error %d\n", err
);
1116 strcpy(rmidi
->name
, card
->shortname
);
1117 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
1118 SNDRV_RAWMIDI_INFO_INPUT
|
1119 SNDRV_RAWMIDI_INFO_DUPLEX
;
1120 rmidi
->private_data
= dev
;
1122 /* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
1123 It's an upside-down world being a gadget. */
1124 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &gmidi_in_ops
);
1125 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &gmidi_out_ops
);
1127 snd_card_set_dev(card
, &dev
->gadget
->dev
);
1129 /* register it - we're ready to go */
1130 err
= snd_card_register(card
);
1132 ERROR(dev
, "snd_card_register failed\n");
1136 VDBG(dev
, "gmidi_register_card finished ok\n");
1141 snd_card_free(dev
->card
);
1148 * Creates an output endpoint, and initializes output ports.
1150 static int __init
gmidi_bind(struct usb_gadget
*gadget
)
1152 struct gmidi_device
*dev
;
1153 struct usb_ep
*in_ep
, *out_ep
;
1156 /* support optional vendor/distro customization */
1159 pr_err("idVendor needs idProduct!\n");
1162 device_desc
.idVendor
= cpu_to_le16(idVendor
);
1163 device_desc
.idProduct
= cpu_to_le16(idProduct
);
1165 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
1168 if (iManufacturer
) {
1169 strlcpy(manufacturer
, iManufacturer
, sizeof(manufacturer
));
1171 snprintf(manufacturer
, sizeof(manufacturer
), "%s %s with %s",
1172 init_utsname()->sysname
, init_utsname()->release
,
1176 strlcpy(product_desc
, iProduct
, sizeof(product_desc
));
1178 if (iSerialNumber
) {
1179 device_desc
.iSerialNumber
= STRING_SERIAL
,
1180 strlcpy(serial_number
, iSerialNumber
, sizeof(serial_number
));
1183 /* Bulk-only drivers like this one SHOULD be able to
1184 * autoconfigure on any sane usb controller driver,
1185 * but there may also be important quirks to address.
1187 usb_ep_autoconfig_reset(gadget
);
1188 in_ep
= usb_ep_autoconfig(gadget
, &bulk_in_desc
);
1191 pr_err("%s: can't autoconfigure on %s\n",
1192 shortname
, gadget
->name
);
1195 EP_IN_NAME
= in_ep
->name
;
1196 in_ep
->driver_data
= in_ep
; /* claim */
1198 out_ep
= usb_ep_autoconfig(gadget
, &bulk_out_desc
);
1202 EP_OUT_NAME
= out_ep
->name
;
1203 out_ep
->driver_data
= out_ep
; /* claim */
1205 gcnum
= usb_gadget_controller_number(gadget
);
1207 device_desc
.bcdDevice
= cpu_to_le16(0x0200 + gcnum
);
1209 /* gmidi is so simple (no altsettings) that
1210 * it SHOULD NOT have problems with bulk-capable hardware.
1211 * so warn about unrecognized controllers, don't panic.
1213 pr_warning("%s: controller '%s' not recognized\n",
1214 shortname
, gadget
->name
);
1215 device_desc
.bcdDevice
= __constant_cpu_to_le16(0x9999);
1219 /* ok, we made sense of the hardware ... */
1220 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1224 spin_lock_init(&dev
->lock
);
1225 dev
->gadget
= gadget
;
1227 dev
->out_ep
= out_ep
;
1228 set_gadget_data(gadget
, dev
);
1229 tasklet_init(&dev
->tasklet
, gmidi_in_tasklet
, (unsigned long)dev
);
1231 /* preallocate control response and buffer */
1232 dev
->req
= alloc_ep_req(gadget
->ep0
, USB_BUFSIZ
);
1238 dev
->req
->complete
= gmidi_setup_complete
;
1240 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
1242 gadget
->ep0
->driver_data
= dev
;
1244 INFO(dev
, "%s, version: " DRIVER_VERSION
"\n", longname
);
1245 INFO(dev
, "using %s, OUT %s IN %s\n", gadget
->name
,
1246 EP_OUT_NAME
, EP_IN_NAME
);
1248 /* register as an ALSA sound card */
1249 err
= gmidi_register_card(dev
);
1254 VDBG(dev
, "gmidi_bind finished ok\n");
1258 gmidi_unbind(gadget
);
1263 static void gmidi_suspend(struct usb_gadget
*gadget
)
1265 struct gmidi_device
*dev
= get_gadget_data(gadget
);
1267 if (gadget
->speed
== USB_SPEED_UNKNOWN
) {
1271 DBG(dev
, "suspend\n");
1274 static void gmidi_resume(struct usb_gadget
*gadget
)
1276 struct gmidi_device
*dev
= get_gadget_data(gadget
);
1278 DBG(dev
, "resume\n");
1282 static struct usb_gadget_driver gmidi_driver
= {
1283 .speed
= USB_SPEED_FULL
,
1284 .function
= (char *)longname
,
1286 .unbind
= gmidi_unbind
,
1288 .setup
= gmidi_setup
,
1289 .disconnect
= gmidi_disconnect
,
1291 .suspend
= gmidi_suspend
,
1292 .resume
= gmidi_resume
,
1295 .name
= (char *)shortname
,
1296 .owner
= THIS_MODULE
,
1300 static int __init
gmidi_init(void)
1302 return usb_gadget_register_driver(&gmidi_driver
);
1304 module_init(gmidi_init
);
1306 static void __exit
gmidi_cleanup(void)
1308 usb_gadget_unregister_driver(&gmidi_driver
);
1310 module_exit(gmidi_cleanup
);