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/slab.h>
25 #include <linux/utsname.h>
26 #include <linux/module.h>
27 #include <linux/device.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>
38 #include "gadget_chips.h"
42 * Kbuild is not very cooperative with respect to linking separately
43 * compiled library objects into one module. So for now we won't use
44 * separate compilation ... ensuring init/exit sections work to shrink
45 * the runtime footprint, and giving us at least some parts of what
46 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
48 #include "usbstring.c"
50 #include "epautoconf.c"
52 /*-------------------------------------------------------------------------*/
55 MODULE_AUTHOR("Ben Williamson");
56 MODULE_LICENSE("GPL v2");
58 #define DRIVER_VERSION "25 Jul 2006"
60 static const char shortname
[] = "g_midi";
61 static const char longname
[] = "MIDI Gadget";
63 static int index
= SNDRV_DEFAULT_IDX1
;
64 static char *id
= SNDRV_DEFAULT_STR1
;
66 module_param(index
, int, 0444);
67 MODULE_PARM_DESC(index
, "Index value for the USB MIDI Gadget adapter.");
68 module_param(id
, charp
, 0444);
69 MODULE_PARM_DESC(id
, "ID string for the USB MIDI Gadget adapter.");
71 /* Some systems will want different product identifiers published in the
72 * device descriptor, either numbers or strings or both. These string
73 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
76 static ushort idVendor
;
77 module_param(idVendor
, ushort
, S_IRUGO
);
78 MODULE_PARM_DESC(idVendor
, "USB Vendor ID");
80 static ushort idProduct
;
81 module_param(idProduct
, ushort
, S_IRUGO
);
82 MODULE_PARM_DESC(idProduct
, "USB Product ID");
84 static ushort bcdDevice
;
85 module_param(bcdDevice
, ushort
, S_IRUGO
);
86 MODULE_PARM_DESC(bcdDevice
, "USB Device version (BCD)");
88 static char *iManufacturer
;
89 module_param(iManufacturer
, charp
, S_IRUGO
);
90 MODULE_PARM_DESC(iManufacturer
, "USB Manufacturer string");
92 static char *iProduct
;
93 module_param(iProduct
, charp
, S_IRUGO
);
94 MODULE_PARM_DESC(iProduct
, "USB Product string");
96 static char *iSerialNumber
;
97 module_param(iSerialNumber
, charp
, S_IRUGO
);
98 MODULE_PARM_DESC(iSerialNumber
, "SerialNumber");
101 * this version autoconfigures as much as possible,
102 * which is reasonable for most "bulk-only" drivers.
104 static const char *EP_IN_NAME
;
105 static const char *EP_OUT_NAME
;
108 /* big enough to hold our biggest descriptor */
109 #define USB_BUFSIZ 256
112 /* This is a gadget, and the IN/OUT naming is from the host's perspective.
113 USB -> OUT endpoint -> rawmidi
114 USB <- IN endpoint <- rawmidi */
115 struct gmidi_in_port
{
116 struct gmidi_device
* dev
;
118 uint8_t cable
; /* cable number << 4 */
120 #define STATE_UNKNOWN 0
121 #define STATE_1PARAM 1
122 #define STATE_2PARAM_1 2
123 #define STATE_2PARAM_2 3
124 #define STATE_SYSEX_0 4
125 #define STATE_SYSEX_1 5
126 #define STATE_SYSEX_2 6
130 struct gmidi_device
{
132 struct usb_gadget
*gadget
;
133 struct usb_request
*req
; /* for control responses */
135 struct usb_ep
*in_ep
, *out_ep
;
136 struct snd_card
*card
;
137 struct snd_rawmidi
*rmidi
;
138 struct snd_rawmidi_substream
*in_substream
;
139 struct snd_rawmidi_substream
*out_substream
;
141 /* For the moment we only support one port in
142 each direction, but in_port is kept as a
143 separate struct so we can have more later. */
144 struct gmidi_in_port in_port
;
145 unsigned long out_triggered
;
146 struct tasklet_struct tasklet
;
149 static void gmidi_transmit(struct gmidi_device
* dev
, struct usb_request
* req
);
152 #define DBG(d, fmt, args...) \
153 dev_dbg(&(d)->gadget->dev , fmt , ## args)
154 #define VDBG(d, fmt, args...) \
155 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
156 #define ERROR(d, fmt, args...) \
157 dev_err(&(d)->gadget->dev , fmt , ## args)
158 #define INFO(d, fmt, args...) \
159 dev_info(&(d)->gadget->dev , fmt , ## args)
162 static unsigned buflen
= 256;
163 static unsigned qlen
= 32;
165 module_param(buflen
, uint
, S_IRUGO
);
166 module_param(qlen
, uint
, S_IRUGO
);
169 /* Thanks to Grey Innovation for donating this product ID.
171 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
172 * Instead: allocate your own, using normal USB-IF procedures.
174 #define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
175 #define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
179 * DESCRIPTORS ... most are static, but strings and (full)
180 * configuration descriptors are built on demand.
183 #define STRING_MANUFACTURER 25
184 #define STRING_PRODUCT 42
185 #define STRING_SERIAL 101
186 #define STRING_MIDI_GADGET 250
188 /* We only have the one configuration, it's number 1. */
189 #define GMIDI_CONFIG 1
191 /* We have two interfaces- AudioControl and MIDIStreaming */
192 #define GMIDI_AC_INTERFACE 0
193 #define GMIDI_MS_INTERFACE 1
194 #define GMIDI_NUM_INTERFACES 2
196 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
197 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
198 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1);
200 /* B.1 Device Descriptor */
201 static struct usb_device_descriptor device_desc
= {
202 .bLength
= USB_DT_DEVICE_SIZE
,
203 .bDescriptorType
= USB_DT_DEVICE
,
204 .bcdUSB
= cpu_to_le16(0x0200),
205 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
206 .idVendor
= cpu_to_le16(DRIVER_VENDOR_NUM
),
207 .idProduct
= cpu_to_le16(DRIVER_PRODUCT_NUM
),
208 .iManufacturer
= STRING_MANUFACTURER
,
209 .iProduct
= STRING_PRODUCT
,
210 .bNumConfigurations
= 1,
213 /* B.2 Configuration Descriptor */
214 static struct usb_config_descriptor config_desc
= {
215 .bLength
= USB_DT_CONFIG_SIZE
,
216 .bDescriptorType
= USB_DT_CONFIG
,
217 /* compute wTotalLength on the fly */
218 .bNumInterfaces
= GMIDI_NUM_INTERFACES
,
219 .bConfigurationValue
= GMIDI_CONFIG
,
220 .iConfiguration
= STRING_MIDI_GADGET
,
222 * FIXME: When embedding this driver in a device,
223 * these need to be set to reflect the actual
224 * power properties of the device. Is it selfpowered?
226 .bmAttributes
= USB_CONFIG_ATT_ONE
,
227 .bMaxPower
= CONFIG_USB_GADGET_VBUS_DRAW
/ 2,
230 /* B.3.1 Standard AC Interface Descriptor */
231 static const struct usb_interface_descriptor ac_interface_desc
= {
232 .bLength
= USB_DT_INTERFACE_SIZE
,
233 .bDescriptorType
= USB_DT_INTERFACE
,
234 .bInterfaceNumber
= GMIDI_AC_INTERFACE
,
236 .bInterfaceClass
= USB_CLASS_AUDIO
,
237 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
238 .iInterface
= STRING_MIDI_GADGET
,
241 /* B.3.2 Class-Specific AC Interface Descriptor */
242 static const struct uac1_ac_header_descriptor_1 ac_header_desc
= {
243 .bLength
= UAC_DT_AC_HEADER_SIZE(1),
244 .bDescriptorType
= USB_DT_CS_INTERFACE
,
245 .bDescriptorSubtype
= USB_MS_HEADER
,
246 .bcdADC
= cpu_to_le16(0x0100),
247 .wTotalLength
= cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
250 [0] = GMIDI_MS_INTERFACE
,
254 /* B.4.1 Standard MS Interface Descriptor */
255 static const struct usb_interface_descriptor ms_interface_desc
= {
256 .bLength
= USB_DT_INTERFACE_SIZE
,
257 .bDescriptorType
= USB_DT_INTERFACE
,
258 .bInterfaceNumber
= GMIDI_MS_INTERFACE
,
260 .bInterfaceClass
= USB_CLASS_AUDIO
,
261 .bInterfaceSubClass
= USB_SUBCLASS_MIDISTREAMING
,
262 .iInterface
= STRING_MIDI_GADGET
,
265 /* B.4.2 Class-Specific MS Interface Descriptor */
266 static const struct usb_ms_header_descriptor ms_header_desc
= {
267 .bLength
= USB_DT_MS_HEADER_SIZE
,
268 .bDescriptorType
= USB_DT_CS_INTERFACE
,
269 .bDescriptorSubtype
= USB_MS_HEADER
,
270 .bcdMSC
= cpu_to_le16(0x0100),
271 .wTotalLength
= cpu_to_le16(USB_DT_MS_HEADER_SIZE
272 + 2*USB_DT_MIDI_IN_SIZE
273 + 2*USB_DT_MIDI_OUT_SIZE(1)),
276 #define JACK_IN_EMB 1
277 #define JACK_IN_EXT 2
278 #define JACK_OUT_EMB 3
279 #define JACK_OUT_EXT 4
281 /* B.4.3 MIDI IN Jack Descriptors */
282 static const struct usb_midi_in_jack_descriptor jack_in_emb_desc
= {
283 .bLength
= USB_DT_MIDI_IN_SIZE
,
284 .bDescriptorType
= USB_DT_CS_INTERFACE
,
285 .bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
,
286 .bJackType
= USB_MS_EMBEDDED
,
287 .bJackID
= JACK_IN_EMB
,
290 static const struct usb_midi_in_jack_descriptor jack_in_ext_desc
= {
291 .bLength
= USB_DT_MIDI_IN_SIZE
,
292 .bDescriptorType
= USB_DT_CS_INTERFACE
,
293 .bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
,
294 .bJackType
= USB_MS_EXTERNAL
,
295 .bJackID
= JACK_IN_EXT
,
298 /* B.4.4 MIDI OUT Jack Descriptors */
299 static const struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc
= {
300 .bLength
= USB_DT_MIDI_OUT_SIZE(1),
301 .bDescriptorType
= USB_DT_CS_INTERFACE
,
302 .bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
,
303 .bJackType
= USB_MS_EMBEDDED
,
304 .bJackID
= JACK_OUT_EMB
,
308 .baSourceID
= JACK_IN_EXT
,
314 static const struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc
= {
315 .bLength
= USB_DT_MIDI_OUT_SIZE(1),
316 .bDescriptorType
= USB_DT_CS_INTERFACE
,
317 .bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
,
318 .bJackType
= USB_MS_EXTERNAL
,
319 .bJackID
= JACK_OUT_EXT
,
323 .baSourceID
= JACK_IN_EMB
,
329 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
330 static struct usb_endpoint_descriptor bulk_out_desc
= {
331 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
332 .bDescriptorType
= USB_DT_ENDPOINT
,
333 .bEndpointAddress
= USB_DIR_OUT
,
334 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
337 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
338 static const struct usb_ms_endpoint_descriptor_1 ms_out_desc
= {
339 .bLength
= USB_DT_MS_ENDPOINT_SIZE(1),
340 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
341 .bDescriptorSubtype
= USB_MS_GENERAL
,
342 .bNumEmbMIDIJack
= 1,
348 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
349 static struct usb_endpoint_descriptor bulk_in_desc
= {
350 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
351 .bDescriptorType
= USB_DT_ENDPOINT
,
352 .bEndpointAddress
= USB_DIR_IN
,
353 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
356 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
357 static const struct usb_ms_endpoint_descriptor_1 ms_in_desc
= {
358 .bLength
= USB_DT_MS_ENDPOINT_SIZE(1),
359 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
360 .bDescriptorSubtype
= USB_MS_GENERAL
,
361 .bNumEmbMIDIJack
= 1,
367 static const struct usb_descriptor_header
*gmidi_function
[] = {
368 (struct usb_descriptor_header
*)&ac_interface_desc
,
369 (struct usb_descriptor_header
*)&ac_header_desc
,
370 (struct usb_descriptor_header
*)&ms_interface_desc
,
372 (struct usb_descriptor_header
*)&ms_header_desc
,
373 (struct usb_descriptor_header
*)&jack_in_emb_desc
,
374 (struct usb_descriptor_header
*)&jack_in_ext_desc
,
375 (struct usb_descriptor_header
*)&jack_out_emb_desc
,
376 (struct usb_descriptor_header
*)&jack_out_ext_desc
,
377 /* If you add more jacks, update ms_header_desc.wTotalLength */
379 (struct usb_descriptor_header
*)&bulk_out_desc
,
380 (struct usb_descriptor_header
*)&ms_out_desc
,
381 (struct usb_descriptor_header
*)&bulk_in_desc
,
382 (struct usb_descriptor_header
*)&ms_in_desc
,
386 static char manufacturer
[50];
387 static char product_desc
[40] = "MIDI Gadget";
388 static char serial_number
[20];
390 /* static strings, in UTF-8 */
391 static struct usb_string strings
[] = {
392 { STRING_MANUFACTURER
, manufacturer
, },
393 { STRING_PRODUCT
, product_desc
, },
394 { STRING_SERIAL
, serial_number
, },
395 { STRING_MIDI_GADGET
, longname
, },
396 { } /* end of list */
399 static struct usb_gadget_strings stringtab
= {
400 .language
= 0x0409, /* en-us */
404 static int config_buf(struct usb_gadget
*gadget
,
405 u8
*buf
, u8 type
, unsigned index
)
409 /* only one configuration */
413 len
= usb_gadget_config_buf(&config_desc
,
414 buf
, USB_BUFSIZ
, gmidi_function
);
418 ((struct usb_config_descriptor
*)buf
)->bDescriptorType
= type
;
422 static struct usb_request
*alloc_ep_req(struct usb_ep
*ep
, unsigned length
)
424 struct usb_request
*req
;
426 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
428 req
->length
= length
;
429 req
->buf
= kmalloc(length
, GFP_ATOMIC
);
431 usb_ep_free_request(ep
, req
);
438 static void free_ep_req(struct usb_ep
*ep
, struct usb_request
*req
)
441 usb_ep_free_request(ep
, req
);
444 static const uint8_t gmidi_cin_length
[] = {
445 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
449 * Receives a chunk of MIDI data.
451 static void gmidi_read_data(struct usb_ep
*ep
, int cable
,
452 uint8_t *data
, int length
)
454 struct gmidi_device
*dev
= ep
->driver_data
;
455 /* cable is ignored, because for now we only have one. */
457 if (!dev
->out_substream
) {
458 /* Nobody is listening - throw it on the floor. */
461 if (!test_bit(dev
->out_substream
->number
, &dev
->out_triggered
)) {
464 snd_rawmidi_receive(dev
->out_substream
, data
, length
);
467 static void gmidi_handle_out_data(struct usb_ep
*ep
, struct usb_request
*req
)
472 for (i
= 0; i
+ 3 < req
->actual
; i
+= 4) {
474 int cable
= buf
[i
] >> 4;
475 int length
= gmidi_cin_length
[buf
[i
] & 0x0f];
476 gmidi_read_data(ep
, cable
, &buf
[i
+ 1], length
);
481 static void gmidi_complete(struct usb_ep
*ep
, struct usb_request
*req
)
483 struct gmidi_device
*dev
= ep
->driver_data
;
484 int status
= req
->status
;
487 case 0: /* normal completion */
488 if (ep
== dev
->out_ep
) {
489 /* we received stuff.
490 req is queued again, below */
491 gmidi_handle_out_data(ep
, req
);
492 } else if (ep
== dev
->in_ep
) {
493 /* our transmit completed.
494 see if there's more to go.
495 gmidi_transmit eats req, don't queue it again. */
496 gmidi_transmit(dev
, req
);
501 /* this endpoint is normally active while we're configured */
502 case -ECONNABORTED
: /* hardware forced ep reset */
503 case -ECONNRESET
: /* request dequeued */
504 case -ESHUTDOWN
: /* disconnect from host */
505 VDBG(dev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
506 req
->actual
, req
->length
);
507 if (ep
== dev
->out_ep
) {
508 gmidi_handle_out_data(ep
, req
);
510 free_ep_req(ep
, req
);
513 case -EOVERFLOW
: /* buffer overrun on read means that
514 * we didn't provide a big enough
518 DBG(dev
, "%s complete --> %d, %d/%d\n", ep
->name
,
519 status
, req
->actual
, req
->length
);
521 case -EREMOTEIO
: /* short read */
525 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
527 ERROR(dev
, "kill %s: resubmit %d bytes --> %d\n",
528 ep
->name
, req
->length
, status
);
530 /* FIXME recover later ... somehow */
534 static int set_gmidi_config(struct gmidi_device
*dev
, gfp_t gfp_flags
)
537 struct usb_request
*req
;
541 dev
->in_ep
->desc
= &bulk_in_desc
;
542 err
= usb_ep_enable(dev
->in_ep
);
544 ERROR(dev
, "can't start %s: %d\n", dev
->in_ep
->name
, err
);
547 dev
->in_ep
->driver_data
= dev
;
549 dev
->out_ep
->desc
= &bulk_out_desc
;
550 err
= usb_ep_enable(dev
->out_ep
);
552 ERROR(dev
, "can't start %s: %d\n", dev
->out_ep
->name
, err
);
555 dev
->out_ep
->driver_data
= dev
;
557 /* allocate a bunch of read buffers and queue them all at once. */
559 for (i
= 0; i
< qlen
&& err
== 0; i
++) {
560 req
= alloc_ep_req(ep
, buflen
);
562 req
->complete
= gmidi_complete
;
563 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
565 DBG(dev
, "%s queue req: %d\n", ep
->name
, err
);
572 /* caller is responsible for cleanup on error */
577 static void gmidi_reset_config(struct gmidi_device
*dev
)
579 if (dev
->config
== 0) {
583 DBG(dev
, "reset config\n");
585 /* just disable endpoints, forcing completion of pending i/o.
586 * all our completion handlers free their requests in this case.
588 usb_ep_disable(dev
->in_ep
);
589 usb_ep_disable(dev
->out_ep
);
593 /* change our operational config. this code must agree with the code
594 * that returns config descriptors, and altsetting code.
596 * it's also responsible for power management interactions. some
597 * configurations might not work with our current power sources.
599 * note that some device controller hardware will constrain what this
600 * code can do, perhaps by disallowing more than one configuration or
601 * by limiting configuration choices (like the pxa2xx).
604 gmidi_set_config(struct gmidi_device
*dev
, unsigned number
, gfp_t gfp_flags
)
607 struct usb_gadget
*gadget
= dev
->gadget
;
611 /* Hacking this bit out fixes a bug where on receipt of two
612 USB_REQ_SET_CONFIGURATION messages, we end up with no
613 buffered OUT requests waiting for data. This is clearly
614 hiding a bug elsewhere, because if the config didn't
615 change then we really shouldn't do anything. */
616 /* Having said that, when we do "change" from config 1
617 to config 1, we at least gmidi_reset_config() which
618 clears out any requests on endpoints, so it's not like
619 we leak or anything. */
620 if (number
== dev
->config
) {
625 gmidi_reset_config(dev
);
629 result
= set_gmidi_config(dev
, gfp_flags
);
638 if (!result
&& (!dev
->in_ep
|| !dev
->out_ep
)) {
642 gmidi_reset_config(dev
);
646 switch (gadget
->speed
) {
647 case USB_SPEED_LOW
: speed
= "low"; break;
648 case USB_SPEED_FULL
: speed
= "full"; break;
649 case USB_SPEED_HIGH
: speed
= "high"; break;
650 default: speed
= "?"; break;
653 dev
->config
= number
;
654 INFO(dev
, "%s speed\n", speed
);
660 static void gmidi_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
662 if (req
->status
|| req
->actual
!= req
->length
) {
663 DBG((struct gmidi_device
*) ep
->driver_data
,
664 "setup complete --> %d, %d/%d\n",
665 req
->status
, req
->actual
, req
->length
);
670 * The setup() callback implements all the ep0 functionality that's
671 * not handled lower down, in hardware or the hardware driver (like
672 * device and endpoint feature flags, and their status). It's all
673 * housekeeping for the gadget function we're implementing. Most of
674 * the work is in config-specific setup.
676 static int gmidi_setup(struct usb_gadget
*gadget
,
677 const struct usb_ctrlrequest
*ctrl
)
679 struct gmidi_device
*dev
= get_gadget_data(gadget
);
680 struct usb_request
*req
= dev
->req
;
681 int value
= -EOPNOTSUPP
;
682 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
683 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
684 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
686 /* usually this stores reply data in the pre-allocated ep0 buffer,
687 * but config change events will reconfigure hardware.
690 switch (ctrl
->bRequest
) {
692 case USB_REQ_GET_DESCRIPTOR
:
693 if (ctrl
->bRequestType
!= USB_DIR_IN
) {
696 switch (w_value
>> 8) {
699 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
700 value
= min(w_length
, (u16
) sizeof(device_desc
));
701 memcpy(req
->buf
, &device_desc
, value
);
704 value
= config_buf(gadget
, req
->buf
,
708 value
= min(w_length
, (u16
)value
);
713 /* wIndex == language code.
714 * this driver only handles one language, you can
715 * add string tables for other languages, using
716 * any UTF-8 characters
718 value
= usb_gadget_get_string(&stringtab
,
719 w_value
& 0xff, req
->buf
);
721 value
= min(w_length
, (u16
)value
);
727 /* currently two configs, two speeds */
728 case USB_REQ_SET_CONFIGURATION
:
729 if (ctrl
->bRequestType
!= 0) {
732 if (gadget
->a_hnp_support
) {
733 DBG(dev
, "HNP available\n");
734 } else if (gadget
->a_alt_hnp_support
) {
735 DBG(dev
, "HNP needs a different root port\n");
737 VDBG(dev
, "HNP inactive\n");
739 spin_lock(&dev
->lock
);
740 value
= gmidi_set_config(dev
, w_value
, GFP_ATOMIC
);
741 spin_unlock(&dev
->lock
);
743 case USB_REQ_GET_CONFIGURATION
:
744 if (ctrl
->bRequestType
!= USB_DIR_IN
) {
747 *(u8
*)req
->buf
= dev
->config
;
748 value
= min(w_length
, (u16
)1);
751 /* until we add altsetting support, or other interfaces,
752 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
753 * and already killed pending endpoint I/O.
755 case USB_REQ_SET_INTERFACE
:
756 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
) {
759 spin_lock(&dev
->lock
);
760 if (dev
->config
&& w_index
< GMIDI_NUM_INTERFACES
763 u8 config
= dev
->config
;
765 /* resets interface configuration, forgets about
766 * previous transaction state (queued bufs, etc)
767 * and re-inits endpoint state (toggle etc)
768 * no response queued, just zero status == success.
769 * if we had more than one interface we couldn't
770 * use this "reset the config" shortcut.
772 gmidi_reset_config(dev
);
773 gmidi_set_config(dev
, config
, GFP_ATOMIC
);
776 spin_unlock(&dev
->lock
);
778 case USB_REQ_GET_INTERFACE
:
779 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
)) {
785 if (w_index
>= GMIDI_NUM_INTERFACES
) {
790 value
= min(w_length
, (u16
)1);
795 VDBG(dev
, "unknown control req%02x.%02x v%04x i%04x l%d\n",
796 ctrl
->bRequestType
, ctrl
->bRequest
,
797 w_value
, w_index
, w_length
);
800 /* respond with data transfer before status phase? */
803 req
->zero
= value
< w_length
;
804 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
806 DBG(dev
, "ep_queue --> %d\n", value
);
808 gmidi_setup_complete(gadget
->ep0
, req
);
812 /* device either stalls (value < 0) or reports success */
816 static void gmidi_disconnect(struct usb_gadget
*gadget
)
818 struct gmidi_device
*dev
= get_gadget_data(gadget
);
821 spin_lock_irqsave(&dev
->lock
, flags
);
822 gmidi_reset_config(dev
);
824 /* a more significant application might have some non-usb
825 * activities to quiesce here, saving resources like power
826 * or pushing the notification up a network stack.
828 spin_unlock_irqrestore(&dev
->lock
, flags
);
830 /* next we may get setup() calls to enumerate new connections;
831 * or an unbind() during shutdown (including removing module).
835 static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget
*gadget
)
837 struct gmidi_device
*dev
= get_gadget_data(gadget
);
838 struct snd_card
*card
;
840 DBG(dev
, "unbind\n");
848 /* we've already been disconnected ... no i/o is active */
850 dev
->req
->length
= USB_BUFSIZ
;
851 free_ep_req(gadget
->ep0
, dev
->req
);
854 set_gadget_data(gadget
, NULL
);
857 static int gmidi_snd_free(struct snd_device
*device
)
862 static void gmidi_transmit_packet(struct usb_request
*req
, uint8_t p0
,
863 uint8_t p1
, uint8_t p2
, uint8_t p3
)
865 unsigned length
= req
->length
;
866 u8
*buf
= (u8
*)req
->buf
+ length
;
872 req
->length
= length
+ 4;
876 * Converts MIDI commands to USB MIDI packets.
878 static void gmidi_transmit_byte(struct usb_request
*req
,
879 struct gmidi_in_port
*port
, uint8_t b
)
881 uint8_t p0
= port
->cable
;
884 gmidi_transmit_packet(req
, p0
| 0x0f, b
, 0, 0);
885 } else if (b
>= 0xf0) {
889 port
->state
= STATE_SYSEX_1
;
894 port
->state
= STATE_1PARAM
;
898 port
->state
= STATE_2PARAM_1
;
902 port
->state
= STATE_UNKNOWN
;
905 gmidi_transmit_packet(req
, p0
| 0x05, 0xf6, 0, 0);
906 port
->state
= STATE_UNKNOWN
;
909 switch (port
->state
) {
911 gmidi_transmit_packet(req
,
912 p0
| 0x05, 0xf7, 0, 0);
915 gmidi_transmit_packet(req
,
916 p0
| 0x06, port
->data
[0], 0xf7, 0);
919 gmidi_transmit_packet(req
,
920 p0
| 0x07, port
->data
[0],
921 port
->data
[1], 0xf7);
924 port
->state
= STATE_UNKNOWN
;
927 } else if (b
>= 0x80) {
929 if (b
>= 0xc0 && b
<= 0xdf)
930 port
->state
= STATE_1PARAM
;
932 port
->state
= STATE_2PARAM_1
;
933 } else { /* b < 0x80 */
934 switch (port
->state
) {
936 if (port
->data
[0] < 0xf0) {
937 p0
|= port
->data
[0] >> 4;
940 port
->state
= STATE_UNKNOWN
;
942 gmidi_transmit_packet(req
, p0
, port
->data
[0], b
, 0);
946 port
->state
= STATE_2PARAM_2
;
949 if (port
->data
[0] < 0xf0) {
950 p0
|= port
->data
[0] >> 4;
951 port
->state
= STATE_2PARAM_1
;
954 port
->state
= STATE_UNKNOWN
;
956 gmidi_transmit_packet(req
,
957 p0
, port
->data
[0], port
->data
[1], b
);
961 port
->state
= STATE_SYSEX_1
;
965 port
->state
= STATE_SYSEX_2
;
968 gmidi_transmit_packet(req
,
969 p0
| 0x04, port
->data
[0], port
->data
[1], b
);
970 port
->state
= STATE_SYSEX_0
;
976 static void gmidi_transmit(struct gmidi_device
*dev
, struct usb_request
*req
)
978 struct usb_ep
*ep
= dev
->in_ep
;
979 struct gmidi_in_port
*port
= &dev
->in_port
;
985 req
= alloc_ep_req(ep
, buflen
);
988 ERROR(dev
, "gmidi_transmit: alloc_ep_request failed\n");
992 req
->complete
= gmidi_complete
;
995 while (req
->length
+ 3 < buflen
) {
997 if (snd_rawmidi_transmit(dev
->in_substream
, &b
, 1)
1003 gmidi_transmit_byte(req
, port
, b
);
1006 if (req
->length
> 0) {
1007 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
1009 free_ep_req(ep
, req
);
1013 static void gmidi_in_tasklet(unsigned long data
)
1015 struct gmidi_device
*dev
= (struct gmidi_device
*)data
;
1017 gmidi_transmit(dev
, NULL
);
1020 static int gmidi_in_open(struct snd_rawmidi_substream
*substream
)
1022 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1024 VDBG(dev
, "gmidi_in_open\n");
1025 dev
->in_substream
= substream
;
1026 dev
->in_port
.state
= STATE_UNKNOWN
;
1030 static int gmidi_in_close(struct snd_rawmidi_substream
*substream
)
1032 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1034 VDBG(dev
, "gmidi_in_close\n");
1038 static void gmidi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1040 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1042 VDBG(dev
, "gmidi_in_trigger %d\n", up
);
1043 dev
->in_port
.active
= up
;
1045 tasklet_hi_schedule(&dev
->tasklet
);
1049 static int gmidi_out_open(struct snd_rawmidi_substream
*substream
)
1051 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1053 VDBG(dev
, "gmidi_out_open\n");
1054 dev
->out_substream
= substream
;
1058 static int gmidi_out_close(struct snd_rawmidi_substream
*substream
)
1060 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1062 VDBG(dev
, "gmidi_out_close\n");
1066 static void gmidi_out_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1068 struct gmidi_device
*dev
= substream
->rmidi
->private_data
;
1070 VDBG(dev
, "gmidi_out_trigger %d\n", up
);
1072 set_bit(substream
->number
, &dev
->out_triggered
);
1074 clear_bit(substream
->number
, &dev
->out_triggered
);
1078 static struct snd_rawmidi_ops gmidi_in_ops
= {
1079 .open
= gmidi_in_open
,
1080 .close
= gmidi_in_close
,
1081 .trigger
= gmidi_in_trigger
,
1084 static struct snd_rawmidi_ops gmidi_out_ops
= {
1085 .open
= gmidi_out_open
,
1086 .close
= gmidi_out_close
,
1087 .trigger
= gmidi_out_trigger
1090 /* register as a sound "card" */
1091 static int gmidi_register_card(struct gmidi_device
*dev
)
1093 struct snd_card
*card
;
1094 struct snd_rawmidi
*rmidi
;
1098 static struct snd_device_ops ops
= {
1099 .dev_free
= gmidi_snd_free
,
1102 err
= snd_card_create(index
, id
, THIS_MODULE
, 0, &card
);
1104 ERROR(dev
, "snd_card_create failed\n");
1109 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, dev
, &ops
);
1111 ERROR(dev
, "snd_device_new failed: error %d\n", err
);
1115 strcpy(card
->driver
, longname
);
1116 strcpy(card
->longname
, longname
);
1117 strcpy(card
->shortname
, shortname
);
1119 /* Set up rawmidi */
1120 dev
->in_port
.dev
= dev
;
1121 dev
->in_port
.active
= 0;
1122 snd_component_add(card
, "MIDI");
1123 err
= snd_rawmidi_new(card
, "USB MIDI Gadget", 0,
1124 out_ports
, in_ports
, &rmidi
);
1126 ERROR(dev
, "snd_rawmidi_new failed: error %d\n", err
);
1130 strcpy(rmidi
->name
, card
->shortname
);
1131 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
1132 SNDRV_RAWMIDI_INFO_INPUT
|
1133 SNDRV_RAWMIDI_INFO_DUPLEX
;
1134 rmidi
->private_data
= dev
;
1136 /* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
1137 It's an upside-down world being a gadget. */
1138 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &gmidi_in_ops
);
1139 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &gmidi_out_ops
);
1141 snd_card_set_dev(card
, &dev
->gadget
->dev
);
1143 /* register it - we're ready to go */
1144 err
= snd_card_register(card
);
1146 ERROR(dev
, "snd_card_register failed\n");
1150 VDBG(dev
, "gmidi_register_card finished ok\n");
1155 snd_card_free(dev
->card
);
1162 * Creates an output endpoint, and initializes output ports.
1164 static int __init
gmidi_bind(struct usb_gadget
*gadget
)
1166 struct gmidi_device
*dev
;
1167 struct usb_ep
*in_ep
, *out_ep
;
1170 /* support optional vendor/distro customization */
1173 pr_err("idVendor needs idProduct!\n");
1176 device_desc
.idVendor
= cpu_to_le16(idVendor
);
1177 device_desc
.idProduct
= cpu_to_le16(idProduct
);
1179 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
1182 if (iManufacturer
) {
1183 strlcpy(manufacturer
, iManufacturer
, sizeof(manufacturer
));
1185 snprintf(manufacturer
, sizeof(manufacturer
), "%s %s with %s",
1186 init_utsname()->sysname
, init_utsname()->release
,
1190 strlcpy(product_desc
, iProduct
, sizeof(product_desc
));
1192 if (iSerialNumber
) {
1193 device_desc
.iSerialNumber
= STRING_SERIAL
,
1194 strlcpy(serial_number
, iSerialNumber
, sizeof(serial_number
));
1197 /* Bulk-only drivers like this one SHOULD be able to
1198 * autoconfigure on any sane usb controller driver,
1199 * but there may also be important quirks to address.
1201 usb_ep_autoconfig_reset(gadget
);
1202 in_ep
= usb_ep_autoconfig(gadget
, &bulk_in_desc
);
1205 pr_err("%s: can't autoconfigure on %s\n",
1206 shortname
, gadget
->name
);
1209 EP_IN_NAME
= in_ep
->name
;
1210 in_ep
->driver_data
= in_ep
; /* claim */
1212 out_ep
= usb_ep_autoconfig(gadget
, &bulk_out_desc
);
1216 EP_OUT_NAME
= out_ep
->name
;
1217 out_ep
->driver_data
= out_ep
; /* claim */
1219 gcnum
= usb_gadget_controller_number(gadget
);
1221 device_desc
.bcdDevice
= cpu_to_le16(0x0200 + gcnum
);
1223 /* gmidi is so simple (no altsettings) that
1224 * it SHOULD NOT have problems with bulk-capable hardware.
1225 * so warn about unrecognized controllers, don't panic.
1227 pr_warning("%s: controller '%s' not recognized\n",
1228 shortname
, gadget
->name
);
1229 device_desc
.bcdDevice
= cpu_to_le16(0x9999);
1233 /* ok, we made sense of the hardware ... */
1234 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1238 spin_lock_init(&dev
->lock
);
1239 dev
->gadget
= gadget
;
1241 dev
->out_ep
= out_ep
;
1242 set_gadget_data(gadget
, dev
);
1243 tasklet_init(&dev
->tasklet
, gmidi_in_tasklet
, (unsigned long)dev
);
1245 /* preallocate control response and buffer */
1246 dev
->req
= alloc_ep_req(gadget
->ep0
, USB_BUFSIZ
);
1252 dev
->req
->complete
= gmidi_setup_complete
;
1254 gadget
->ep0
->driver_data
= dev
;
1256 INFO(dev
, "%s, version: " DRIVER_VERSION
"\n", longname
);
1257 INFO(dev
, "using %s, OUT %s IN %s\n", gadget
->name
,
1258 EP_OUT_NAME
, EP_IN_NAME
);
1260 /* register as an ALSA sound card */
1261 err
= gmidi_register_card(dev
);
1266 VDBG(dev
, "gmidi_bind finished ok\n");
1270 gmidi_unbind(gadget
);
1275 static void gmidi_suspend(struct usb_gadget
*gadget
)
1277 struct gmidi_device
*dev
= get_gadget_data(gadget
);
1279 if (gadget
->speed
== USB_SPEED_UNKNOWN
) {
1283 DBG(dev
, "suspend\n");
1286 static void gmidi_resume(struct usb_gadget
*gadget
)
1288 struct gmidi_device
*dev
= get_gadget_data(gadget
);
1290 DBG(dev
, "resume\n");
1294 static struct usb_gadget_driver gmidi_driver
= {
1295 .speed
= USB_SPEED_FULL
,
1296 .function
= (char *)longname
,
1297 .unbind
= gmidi_unbind
,
1299 .setup
= gmidi_setup
,
1300 .disconnect
= gmidi_disconnect
,
1302 .suspend
= gmidi_suspend
,
1303 .resume
= gmidi_resume
,
1306 .name
= (char *)shortname
,
1307 .owner
= THIS_MODULE
,
1311 static int __init
gmidi_init(void)
1313 return usb_gadget_probe_driver(&gmidi_driver
, gmidi_bind
);
1315 module_init(gmidi_init
);
1317 static void __exit
gmidi_cleanup(void)
1319 usb_gadget_unregister_driver(&gmidi_driver
);
1321 module_exit(gmidi_cleanup
);