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
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/utsname.h>
31 #include <linux/device.h>
32 #include <linux/moduleparam.h>
34 #include <sound/driver.h>
35 #include <sound/core.h>
36 #include <sound/initval.h>
37 #include <sound/rawmidi.h>
39 #include <linux/usb_ch9.h>
40 #include <linux/usb_gadget.h>
41 #include <linux/usb/audio.h>
42 #include <linux/usb/midi.h>
44 #include "gadget_chips.h"
46 MODULE_AUTHOR("Ben Williamson");
47 MODULE_LICENSE("GPL v2");
49 #define DRIVER_VERSION "25 Jul 2006"
51 static const char shortname
[] = "g_midi";
52 static const char longname
[] = "MIDI Gadget";
54 static int index
= SNDRV_DEFAULT_IDX1
;
55 static char *id
= SNDRV_DEFAULT_STR1
;
57 module_param(index
, int, 0444);
58 MODULE_PARM_DESC(index
, "Index value for the USB MIDI Gadget adapter.");
59 module_param(id
, charp
, 0444);
60 MODULE_PARM_DESC(id
, "ID string for the USB MIDI Gadget adapter.");
62 /* Some systems will want different product identifers published in the
63 * device descriptor, either numbers or strings or both. These string
64 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
67 static ushort idVendor
;
68 module_param(idVendor
, ushort
, S_IRUGO
);
69 MODULE_PARM_DESC(idVendor
, "USB Vendor ID");
71 static ushort idProduct
;
72 module_param(idProduct
, ushort
, S_IRUGO
);
73 MODULE_PARM_DESC(idProduct
, "USB Product ID");
75 static ushort bcdDevice
;
76 module_param(bcdDevice
, ushort
, S_IRUGO
);
77 MODULE_PARM_DESC(bcdDevice
, "USB Device version (BCD)");
79 static char *iManufacturer
;
80 module_param(iManufacturer
, charp
, S_IRUGO
);
81 MODULE_PARM_DESC(iManufacturer
, "USB Manufacturer string");
83 static char *iProduct
;
84 module_param(iProduct
, charp
, S_IRUGO
);
85 MODULE_PARM_DESC(iProduct
, "USB Product string");
87 static char *iSerialNumber
;
88 module_param(iSerialNumber
, charp
, S_IRUGO
);
89 MODULE_PARM_DESC(iSerialNumber
, "SerialNumber");
92 * this version autoconfigures as much as possible,
93 * which is reasonable for most "bulk-only" drivers.
95 static const char *EP_IN_NAME
;
96 static const char *EP_OUT_NAME
;
99 /* big enough to hold our biggest descriptor */
100 #define USB_BUFSIZ 256
103 /* This is a gadget, and the IN/OUT naming is from the host's perspective.
104 USB -> OUT endpoint -> rawmidi
105 USB <- IN endpoint <- rawmidi */
106 struct gmidi_in_port
{
107 struct gmidi_device
* dev
;
109 uint8_t cable
; /* cable number << 4 */
111 #define STATE_UNKNOWN 0
112 #define STATE_1PARAM 1
113 #define STATE_2PARAM_1 2
114 #define STATE_2PARAM_2 3
115 #define STATE_SYSEX_0 4
116 #define STATE_SYSEX_1 5
117 #define STATE_SYSEX_2 6
121 struct gmidi_device
{
123 struct usb_gadget
*gadget
;
124 struct usb_request
*req
; /* for control responses */
126 struct usb_ep
*in_ep
, *out_ep
;
127 struct snd_card
*card
;
128 struct snd_rawmidi
*rmidi
;
129 struct snd_rawmidi_substream
*in_substream
;
130 struct snd_rawmidi_substream
*out_substream
;
132 /* For the moment we only support one port in
133 each direction, but in_port is kept as a
134 separate struct so we can have more later. */
135 struct gmidi_in_port in_port
;
136 unsigned long out_triggered
;
137 struct tasklet_struct tasklet
;
140 static void gmidi_transmit(struct gmidi_device
* dev
, struct usb_request
* req
);
143 #define xprintk(d,level,fmt,args...) \
144 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
147 #define DBG(dev,fmt,args...) \
148 xprintk(dev , KERN_DEBUG , fmt , ## args)
150 #define DBG(dev,fmt,args...) \
157 #define VDBG(dev,fmt,args...) \
161 #define ERROR(dev,fmt,args...) \
162 xprintk(dev , KERN_ERR , fmt , ## args)
163 #define WARN(dev,fmt,args...) \
164 xprintk(dev , KERN_WARNING , fmt , ## args)
165 #define INFO(dev,fmt,args...) \
166 xprintk(dev , KERN_INFO , fmt , ## args)
169 static unsigned buflen
= 256;
170 static unsigned qlen
= 32;
172 module_param(buflen
, uint
, S_IRUGO
);
173 module_param(qlen
, uint
, S_IRUGO
);
176 /* Thanks to Grey Innovation for donating this product ID.
178 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
179 * Instead: allocate your own, using normal USB-IF procedures.
181 #define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
182 #define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
186 * DESCRIPTORS ... most are static, but strings and (full)
187 * configuration descriptors are built on demand.
190 #define STRING_MANUFACTURER 25
191 #define STRING_PRODUCT 42
192 #define STRING_SERIAL 101
193 #define STRING_MIDI_GADGET 250
195 /* We only have the one configuration, it's number 1. */
196 #define GMIDI_CONFIG 1
198 /* We have two interfaces- AudioControl and MIDIStreaming */
199 #define GMIDI_AC_INTERFACE 0
200 #define GMIDI_MS_INTERFACE 1
201 #define GMIDI_NUM_INTERFACES 2
203 DECLARE_USB_AC_HEADER_DESCRIPTOR(1);
204 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
205 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1);
207 /* B.1 Device Descriptor */
208 static struct usb_device_descriptor device_desc
= {
209 .bLength
= USB_DT_DEVICE_SIZE
,
210 .bDescriptorType
= USB_DT_DEVICE
,
211 .bcdUSB
= __constant_cpu_to_le16(0x0200),
212 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
213 .idVendor
= __constant_cpu_to_le16(DRIVER_VENDOR_NUM
),
214 .idProduct
= __constant_cpu_to_le16(DRIVER_PRODUCT_NUM
),
215 .iManufacturer
= STRING_MANUFACTURER
,
216 .iProduct
= STRING_PRODUCT
,
217 .bNumConfigurations
= 1,
220 /* B.2 Configuration Descriptor */
221 static struct usb_config_descriptor config_desc
= {
222 .bLength
= USB_DT_CONFIG_SIZE
,
223 .bDescriptorType
= USB_DT_CONFIG
,
224 /* compute wTotalLength on the fly */
225 .bNumInterfaces
= GMIDI_NUM_INTERFACES
,
226 .bConfigurationValue
= GMIDI_CONFIG
,
227 .iConfiguration
= STRING_MIDI_GADGET
,
229 * FIXME: When embedding this driver in a device,
230 * these need to be set to reflect the actual
231 * power properties of the device. Is it selfpowered?
233 .bmAttributes
= USB_CONFIG_ATT_ONE
,
237 /* B.3.1 Standard AC Interface Descriptor */
238 static const struct usb_interface_descriptor ac_interface_desc
= {
239 .bLength
= USB_DT_INTERFACE_SIZE
,
240 .bDescriptorType
= USB_DT_INTERFACE
,
241 .bInterfaceNumber
= GMIDI_AC_INTERFACE
,
243 .bInterfaceClass
= USB_CLASS_AUDIO
,
244 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
245 .iInterface
= STRING_MIDI_GADGET
,
248 /* B.3.2 Class-Specific AC Interface Descriptor */
249 static const struct usb_ac_header_descriptor_1 ac_header_desc
= {
250 .bLength
= USB_DT_AC_HEADER_SIZE(1),
251 .bDescriptorType
= USB_DT_CS_INTERFACE
,
252 .bDescriptorSubtype
= USB_MS_HEADER
,
253 .bcdADC
= __constant_cpu_to_le16(0x0100),
254 .wTotalLength
= USB_DT_AC_HEADER_SIZE(1),
257 [0] = GMIDI_MS_INTERFACE
,
261 /* B.4.1 Standard MS Interface Descriptor */
262 static const struct usb_interface_descriptor ms_interface_desc
= {
263 .bLength
= USB_DT_INTERFACE_SIZE
,
264 .bDescriptorType
= USB_DT_INTERFACE
,
265 .bInterfaceNumber
= GMIDI_MS_INTERFACE
,
267 .bInterfaceClass
= USB_CLASS_AUDIO
,
268 .bInterfaceSubClass
= USB_SUBCLASS_MIDISTREAMING
,
269 .iInterface
= STRING_MIDI_GADGET
,
272 /* B.4.2 Class-Specific MS Interface Descriptor */
273 static const struct usb_ms_header_descriptor ms_header_desc
= {
274 .bLength
= USB_DT_MS_HEADER_SIZE
,
275 .bDescriptorType
= USB_DT_CS_INTERFACE
,
276 .bDescriptorSubtype
= USB_MS_HEADER
,
277 .bcdMSC
= __constant_cpu_to_le16(0x0100),
278 .wTotalLength
= USB_DT_MS_HEADER_SIZE
279 + 2*USB_DT_MIDI_IN_SIZE
280 + 2*USB_DT_MIDI_OUT_SIZE(1),
283 #define JACK_IN_EMB 1
284 #define JACK_IN_EXT 2
285 #define JACK_OUT_EMB 3
286 #define JACK_OUT_EXT 4
288 /* B.4.3 MIDI IN Jack Descriptors */
289 static const struct usb_midi_in_jack_descriptor jack_in_emb_desc
= {
290 .bLength
= USB_DT_MIDI_IN_SIZE
,
291 .bDescriptorType
= USB_DT_CS_INTERFACE
,
292 .bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
,
293 .bJackType
= USB_MS_EMBEDDED
,
294 .bJackID
= JACK_IN_EMB
,
297 static const struct usb_midi_in_jack_descriptor jack_in_ext_desc
= {
298 .bLength
= USB_DT_MIDI_IN_SIZE
,
299 .bDescriptorType
= USB_DT_CS_INTERFACE
,
300 .bDescriptorSubtype
= USB_MS_MIDI_IN_JACK
,
301 .bJackType
= USB_MS_EXTERNAL
,
302 .bJackID
= JACK_IN_EXT
,
305 /* B.4.4 MIDI OUT Jack Descriptors */
306 static const struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc
= {
307 .bLength
= USB_DT_MIDI_OUT_SIZE(1),
308 .bDescriptorType
= USB_DT_CS_INTERFACE
,
309 .bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
,
310 .bJackType
= USB_MS_EMBEDDED
,
311 .bJackID
= JACK_OUT_EMB
,
315 .baSourceID
= JACK_IN_EXT
,
321 static const struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc
= {
322 .bLength
= USB_DT_MIDI_OUT_SIZE(1),
323 .bDescriptorType
= USB_DT_CS_INTERFACE
,
324 .bDescriptorSubtype
= USB_MS_MIDI_OUT_JACK
,
325 .bJackType
= USB_MS_EXTERNAL
,
326 .bJackID
= JACK_OUT_EXT
,
330 .baSourceID
= JACK_IN_EMB
,
336 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
337 static struct usb_endpoint_descriptor bulk_out_desc
= {
338 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
339 .bDescriptorType
= USB_DT_ENDPOINT
,
340 .bEndpointAddress
= USB_DIR_OUT
,
341 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
344 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
345 static const struct usb_ms_endpoint_descriptor_1 ms_out_desc
= {
346 .bLength
= USB_DT_MS_ENDPOINT_SIZE(1),
347 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
348 .bDescriptorSubtype
= USB_MS_GENERAL
,
349 .bNumEmbMIDIJack
= 1,
355 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
356 static struct usb_endpoint_descriptor bulk_in_desc
= {
357 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
358 .bDescriptorType
= USB_DT_ENDPOINT
,
359 .bEndpointAddress
= USB_DIR_IN
,
360 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
363 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
364 static const struct usb_ms_endpoint_descriptor_1 ms_in_desc
= {
365 .bLength
= USB_DT_MS_ENDPOINT_SIZE(1),
366 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
367 .bDescriptorSubtype
= USB_MS_GENERAL
,
368 .bNumEmbMIDIJack
= 1,
374 static const struct usb_descriptor_header
*gmidi_function
[] = {
375 (struct usb_descriptor_header
*)&ac_interface_desc
,
376 (struct usb_descriptor_header
*)&ac_header_desc
,
377 (struct usb_descriptor_header
*)&ms_interface_desc
,
379 (struct usb_descriptor_header
*)&ms_header_desc
,
380 (struct usb_descriptor_header
*)&jack_in_emb_desc
,
381 (struct usb_descriptor_header
*)&jack_in_ext_desc
,
382 (struct usb_descriptor_header
*)&jack_out_emb_desc
,
383 (struct usb_descriptor_header
*)&jack_out_ext_desc
,
384 /* If you add more jacks, update ms_header_desc.wTotalLength */
386 (struct usb_descriptor_header
*)&bulk_out_desc
,
387 (struct usb_descriptor_header
*)&ms_out_desc
,
388 (struct usb_descriptor_header
*)&bulk_in_desc
,
389 (struct usb_descriptor_header
*)&ms_in_desc
,
393 static char manufacturer
[50];
394 static char product_desc
[40] = "MIDI Gadget";
395 static char serial_number
[20];
397 /* static strings, in UTF-8 */
398 static struct usb_string strings
[] = {
399 { STRING_MANUFACTURER
, manufacturer
, },
400 { STRING_PRODUCT
, product_desc
, },
401 { STRING_SERIAL
, serial_number
, },
402 { STRING_MIDI_GADGET
, longname
, },
403 { } /* end of list */
406 static struct usb_gadget_strings stringtab
= {
407 .language
= 0x0409, /* en-us */
411 static int config_buf(struct usb_gadget
*gadget
,
412 u8
*buf
, u8 type
, unsigned index
)
416 /* only one configuration */
420 len
= usb_gadget_config_buf(&config_desc
,
421 buf
, USB_BUFSIZ
, gmidi_function
);
425 ((struct usb_config_descriptor
*)buf
)->bDescriptorType
= type
;
429 static struct usb_request
* alloc_ep_req(struct usb_ep
*ep
, unsigned length
)
431 struct usb_request
*req
;
433 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
435 req
->length
= length
;
436 req
->buf
= kmalloc(length
, GFP_ATOMIC
);
438 usb_ep_free_request(ep
, req
);
445 static void free_ep_req(struct usb_ep
*ep
, struct usb_request
*req
)
448 usb_ep_free_request(ep
, req
);
451 static const uint8_t gmidi_cin_length
[] = {
452 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
456 * Receives a chunk of MIDI data.
458 static void gmidi_read_data(struct usb_ep
*ep
, int cable
,
459 uint8_t* data
, int length
)
461 struct gmidi_device
*dev
= ep
->driver_data
;
462 /* cable is ignored, because for now we only have one. */
464 if (!dev
->out_substream
) {
465 /* Nobody is listening - throw it on the floor. */
468 if (!test_bit(dev
->out_substream
->number
, &dev
->out_triggered
)) {
471 snd_rawmidi_receive(dev
->out_substream
, data
, length
);
474 static void gmidi_handle_out_data(struct usb_ep
*ep
, struct usb_request
*req
)
479 for (i
= 0; i
+ 3 < req
->actual
; i
+= 4) {
481 int cable
= buf
[i
] >> 4;
482 int length
= gmidi_cin_length
[buf
[i
] & 0x0f];
483 gmidi_read_data(ep
, cable
, &buf
[i
+ 1], length
);
488 static void gmidi_complete(struct usb_ep
*ep
, struct usb_request
*req
)
490 struct gmidi_device
*dev
= ep
->driver_data
;
491 int status
= req
->status
;
494 case 0: /* normal completion */
495 if (ep
== dev
->out_ep
) {
496 /* we received stuff.
497 req is queued again, below */
498 gmidi_handle_out_data(ep
, req
);
499 } else if (ep
== dev
->in_ep
) {
500 /* our transmit completed.
501 see if there's more to go.
502 gmidi_transmit eats req, don't queue it again. */
503 gmidi_transmit(dev
, req
);
508 /* this endpoint is normally active while we're configured */
509 case -ECONNABORTED
: /* hardware forced ep reset */
510 case -ECONNRESET
: /* request dequeued */
511 case -ESHUTDOWN
: /* disconnect from host */
512 VDBG(dev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
513 req
->actual
, req
->length
);
514 if (ep
== dev
->out_ep
) {
515 gmidi_handle_out_data(ep
, req
);
517 free_ep_req(ep
, req
);
520 case -EOVERFLOW
: /* buffer overrun on read means that
521 * we didn't provide a big enough
525 DBG(dev
, "%s complete --> %d, %d/%d\n", ep
->name
,
526 status
, req
->actual
, req
->length
);
528 case -EREMOTEIO
: /* short read */
532 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
534 ERROR(dev
, "kill %s: resubmit %d bytes --> %d\n",
535 ep
->name
, req
->length
, status
);
537 /* FIXME recover later ... somehow */
541 static int set_gmidi_config(struct gmidi_device
*dev
, gfp_t gfp_flags
)
544 struct usb_request
*req
;
548 err
= usb_ep_enable(dev
->in_ep
, &bulk_in_desc
);
550 ERROR(dev
, "can't start %s: %d\n", dev
->in_ep
->name
, err
);
553 dev
->in_ep
->driver_data
= dev
;
555 err
= usb_ep_enable(dev
->out_ep
, &bulk_out_desc
);
557 ERROR(dev
, "can't start %s: %d\n", dev
->out_ep
->name
, err
);
560 dev
->out_ep
->driver_data
= dev
;
562 /* allocate a bunch of read buffers and queue them all at once. */
564 for (i
= 0; i
< qlen
&& err
== 0; i
++) {
565 req
= alloc_ep_req(ep
, buflen
);
567 req
->complete
= gmidi_complete
;
568 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
570 DBG(dev
, "%s queue req: %d\n", ep
->name
, err
);
577 /* caller is responsible for cleanup on error */
582 static void gmidi_reset_config(struct gmidi_device
*dev
)
584 if (dev
->config
== 0) {
588 DBG(dev
, "reset config\n");
590 /* just disable endpoints, forcing completion of pending i/o.
591 * all our completion handlers free their requests in this case.
593 usb_ep_disable(dev
->in_ep
);
594 usb_ep_disable(dev
->out_ep
);
598 /* change our operational config. this code must agree with the code
599 * that returns config descriptors, and altsetting code.
601 * it's also responsible for power management interactions. some
602 * configurations might not work with our current power sources.
604 * note that some device controller hardware will constrain what this
605 * code can do, perhaps by disallowing more than one configuration or
606 * by limiting configuration choices (like the pxa2xx).
609 gmidi_set_config(struct gmidi_device
*dev
, unsigned number
, gfp_t gfp_flags
)
612 struct usb_gadget
*gadget
= dev
->gadget
;
616 /* Hacking this bit out fixes a bug where on receipt of two
617 USB_REQ_SET_CONFIGURATION messages, we end up with no
618 buffered OUT requests waiting for data. This is clearly
619 hiding a bug elsewhere, because if the config didn't
620 change then we really shouldn't do anything. */
621 /* Having said that, when we do "change" from config 1
622 to config 1, we at least gmidi_reset_config() which
623 clears out any requests on endpoints, so it's not like
624 we leak or anything. */
625 if (number
== dev
->config
) {
630 if (gadget_is_sa1100(gadget
) && dev
->config
) {
631 /* tx fifo is full, but we can't clear it...*/
632 INFO(dev
, "can't change configurations\n");
635 gmidi_reset_config(dev
);
639 result
= set_gmidi_config(dev
, gfp_flags
);
648 if (!result
&& (!dev
->in_ep
|| !dev
->out_ep
)) {
652 gmidi_reset_config(dev
);
656 switch (gadget
->speed
) {
657 case USB_SPEED_LOW
: speed
= "low"; break;
658 case USB_SPEED_FULL
: speed
= "full"; break;
659 case USB_SPEED_HIGH
: speed
= "high"; break;
660 default: speed
= "?"; break;
663 dev
->config
= number
;
664 INFO(dev
, "%s speed\n", speed
);
670 static void gmidi_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
672 if (req
->status
|| req
->actual
!= req
->length
) {
673 DBG((struct gmidi_device
*) ep
->driver_data
,
674 "setup complete --> %d, %d/%d\n",
675 req
->status
, req
->actual
, req
->length
);
680 * The setup() callback implements all the ep0 functionality that's
681 * not handled lower down, in hardware or the hardware driver (like
682 * device and endpoint feature flags, and their status). It's all
683 * housekeeping for the gadget function we're implementing. Most of
684 * the work is in config-specific setup.
686 static int gmidi_setup(struct usb_gadget
*gadget
,
687 const struct usb_ctrlrequest
*ctrl
)
689 struct gmidi_device
*dev
= get_gadget_data(gadget
);
690 struct usb_request
*req
= dev
->req
;
691 int value
= -EOPNOTSUPP
;
692 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
693 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
694 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
696 /* usually this stores reply data in the pre-allocated ep0 buffer,
697 * but config change events will reconfigure hardware.
700 switch (ctrl
->bRequest
) {
702 case USB_REQ_GET_DESCRIPTOR
:
703 if (ctrl
->bRequestType
!= USB_DIR_IN
) {
706 switch (w_value
>> 8) {
709 value
= min(w_length
, (u16
) sizeof(device_desc
));
710 memcpy(req
->buf
, &device_desc
, value
);
713 value
= config_buf(gadget
, req
->buf
,
717 value
= min(w_length
, (u16
)value
);
722 /* wIndex == language code.
723 * this driver only handles one language, you can
724 * add string tables for other languages, using
725 * any UTF-8 characters
727 value
= usb_gadget_get_string(&stringtab
,
728 w_value
& 0xff, req
->buf
);
730 value
= min(w_length
, (u16
)value
);
736 /* currently two configs, two speeds */
737 case USB_REQ_SET_CONFIGURATION
:
738 if (ctrl
->bRequestType
!= 0) {
741 if (gadget
->a_hnp_support
) {
742 DBG(dev
, "HNP available\n");
743 } else if (gadget
->a_alt_hnp_support
) {
744 DBG(dev
, "HNP needs a different root port\n");
746 VDBG(dev
, "HNP inactive\n");
748 spin_lock(&dev
->lock
);
749 value
= gmidi_set_config(dev
, w_value
, GFP_ATOMIC
);
750 spin_unlock(&dev
->lock
);
752 case USB_REQ_GET_CONFIGURATION
:
753 if (ctrl
->bRequestType
!= USB_DIR_IN
) {
756 *(u8
*)req
->buf
= dev
->config
;
757 value
= min(w_length
, (u16
)1);
760 /* until we add altsetting support, or other interfaces,
761 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
762 * and already killed pending endpoint I/O.
764 case USB_REQ_SET_INTERFACE
:
765 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
) {
768 spin_lock(&dev
->lock
);
769 if (dev
->config
&& w_index
< GMIDI_NUM_INTERFACES
772 u8 config
= dev
->config
;
774 /* resets interface configuration, forgets about
775 * previous transaction state (queued bufs, etc)
776 * and re-inits endpoint state (toggle etc)
777 * no response queued, just zero status == success.
778 * if we had more than one interface we couldn't
779 * use this "reset the config" shortcut.
781 gmidi_reset_config(dev
);
782 gmidi_set_config(dev
, config
, GFP_ATOMIC
);
785 spin_unlock(&dev
->lock
);
787 case USB_REQ_GET_INTERFACE
:
788 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
)) {
794 if (w_index
>= GMIDI_NUM_INTERFACES
) {
799 value
= min(w_length
, (u16
)1);
804 VDBG(dev
, "unknown control req%02x.%02x v%04x i%04x l%d\n",
805 ctrl
->bRequestType
, ctrl
->bRequest
,
806 w_value
, w_index
, w_length
);
809 /* respond with data transfer before status phase? */
812 req
->zero
= value
< w_length
;
813 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
815 DBG(dev
, "ep_queue --> %d\n", value
);
817 gmidi_setup_complete(gadget
->ep0
, req
);
821 /* device either stalls (value < 0) or reports success */
825 static void gmidi_disconnect(struct usb_gadget
*gadget
)
827 struct gmidi_device
*dev
= get_gadget_data(gadget
);
830 spin_lock_irqsave(&dev
->lock
, flags
);
831 gmidi_reset_config(dev
);
833 /* a more significant application might have some non-usb
834 * activities to quiesce here, saving resources like power
835 * or pushing the notification up a network stack.
837 spin_unlock_irqrestore(&dev
->lock
, flags
);
839 /* next we may get setup() calls to enumerate new connections;
840 * or an unbind() during shutdown (including removing module).
844 static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget
*gadget
)
846 struct gmidi_device
*dev
= get_gadget_data(gadget
);
847 struct snd_card
* card
;
849 DBG(dev
, "unbind\n");
857 /* we've already been disconnected ... no i/o is active */
859 dev
->req
->length
= USB_BUFSIZ
;
860 free_ep_req(gadget
->ep0
, dev
->req
);
863 set_gadget_data(gadget
, NULL
);
866 static int gmidi_snd_free(struct snd_device
*device
)
871 static void gmidi_transmit_packet(struct usb_request
* req
, uint8_t p0
,
872 uint8_t p1
, uint8_t p2
, uint8_t p3
)
874 unsigned length
= req
->length
;
876 uint8_t* buf
= (uint8_t*)req
->buf
+ length
;
881 req
->length
= length
+ 4;
885 * Converts MIDI commands to USB MIDI packets.
887 static void gmidi_transmit_byte(struct usb_request
* req
,
888 struct gmidi_in_port
* port
, uint8_t b
)
890 uint8_t p0
= port
->cable
;
893 gmidi_transmit_packet(req
, p0
| 0x0f, b
, 0, 0);
894 } else if (b
>= 0xf0) {
898 port
->state
= STATE_SYSEX_1
;
903 port
->state
= STATE_1PARAM
;
907 port
->state
= STATE_2PARAM_1
;
911 port
->state
= STATE_UNKNOWN
;
914 gmidi_transmit_packet(req
, p0
| 0x05, 0xf6, 0, 0);
915 port
->state
= STATE_UNKNOWN
;
918 switch (port
->state
) {
920 gmidi_transmit_packet(req
,
921 p0
| 0x05, 0xf7, 0, 0);
924 gmidi_transmit_packet(req
,
925 p0
| 0x06, port
->data
[0], 0xf7, 0);
928 gmidi_transmit_packet(req
,
929 p0
| 0x07, port
->data
[0],
930 port
->data
[1], 0xf7);
933 port
->state
= STATE_UNKNOWN
;
936 } else if (b
>= 0x80) {
938 if (b
>= 0xc0 && b
<= 0xdf)
939 port
->state
= STATE_1PARAM
;
941 port
->state
= STATE_2PARAM_1
;
942 } else { /* b < 0x80 */
943 switch (port
->state
) {
945 if (port
->data
[0] < 0xf0) {
946 p0
|= port
->data
[0] >> 4;
949 port
->state
= STATE_UNKNOWN
;
951 gmidi_transmit_packet(req
, p0
, port
->data
[0], b
, 0);
955 port
->state
= STATE_2PARAM_2
;
958 if (port
->data
[0] < 0xf0) {
959 p0
|= port
->data
[0] >> 4;
960 port
->state
= STATE_2PARAM_1
;
963 port
->state
= STATE_UNKNOWN
;
965 gmidi_transmit_packet(req
,
966 p0
, port
->data
[0], port
->data
[1], b
);
970 port
->state
= STATE_SYSEX_1
;
974 port
->state
= STATE_SYSEX_2
;
977 gmidi_transmit_packet(req
,
978 p0
| 0x04, port
->data
[0], port
->data
[1], b
);
979 port
->state
= STATE_SYSEX_0
;
985 static void gmidi_transmit(struct gmidi_device
* dev
, struct usb_request
* req
)
987 struct usb_ep
* ep
= dev
->in_ep
;
988 struct gmidi_in_port
* port
= &dev
->in_port
;
994 req
= alloc_ep_req(ep
, buflen
);
997 ERROR(dev
, "gmidi_transmit: alloc_ep_request failed\n");
1001 req
->complete
= gmidi_complete
;
1004 while (req
->length
+ 3 < buflen
) {
1006 if (snd_rawmidi_transmit(dev
->in_substream
, &b
, 1)
1012 gmidi_transmit_byte(req
, port
, b
);
1015 if (req
->length
> 0) {
1016 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
1018 free_ep_req(ep
, req
);
1022 static void gmidi_in_tasklet(unsigned long data
)
1024 struct gmidi_device
* dev
= (struct gmidi_device
*)data
;
1026 gmidi_transmit(dev
, NULL
);
1029 static int gmidi_in_open(struct snd_rawmidi_substream
*substream
)
1031 struct gmidi_device
* dev
= substream
->rmidi
->private_data
;
1033 VDBG(dev
, "gmidi_in_open\n");
1034 dev
->in_substream
= substream
;
1035 dev
->in_port
.state
= STATE_UNKNOWN
;
1039 static int gmidi_in_close(struct snd_rawmidi_substream
*substream
)
1041 VDBG(dev
, "gmidi_in_close\n");
1045 static void gmidi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1047 struct gmidi_device
* dev
= substream
->rmidi
->private_data
;
1049 VDBG(dev
, "gmidi_in_trigger %d\n", up
);
1050 dev
->in_port
.active
= up
;
1052 tasklet_hi_schedule(&dev
->tasklet
);
1056 static int gmidi_out_open(struct snd_rawmidi_substream
*substream
)
1058 struct gmidi_device
* dev
= substream
->rmidi
->private_data
;
1060 VDBG(dev
, "gmidi_out_open\n");
1061 dev
->out_substream
= substream
;
1065 static int gmidi_out_close(struct snd_rawmidi_substream
*substream
)
1067 VDBG(dev
, "gmidi_out_close\n");
1071 static void gmidi_out_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1073 struct gmidi_device
* dev
= substream
->rmidi
->private_data
;
1075 VDBG(dev
, "gmidi_out_trigger %d\n", up
);
1077 set_bit(substream
->number
, &dev
->out_triggered
);
1079 clear_bit(substream
->number
, &dev
->out_triggered
);
1083 static struct snd_rawmidi_ops gmidi_in_ops
= {
1084 .open
= gmidi_in_open
,
1085 .close
= gmidi_in_close
,
1086 .trigger
= gmidi_in_trigger
,
1089 static struct snd_rawmidi_ops gmidi_out_ops
= {
1090 .open
= gmidi_out_open
,
1091 .close
= gmidi_out_close
,
1092 .trigger
= gmidi_out_trigger
1095 /* register as a sound "card" */
1096 static int gmidi_register_card(struct gmidi_device
*dev
)
1098 struct snd_card
*card
;
1099 struct snd_rawmidi
*rmidi
;
1103 static struct snd_device_ops ops
= {
1104 .dev_free
= gmidi_snd_free
,
1107 card
= snd_card_new(index
, id
, THIS_MODULE
, 0);
1109 ERROR(dev
, "snd_card_new failed\n");
1115 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, dev
, &ops
);
1117 ERROR(dev
, "snd_device_new failed: error %d\n", err
);
1121 strcpy(card
->driver
, longname
);
1122 strcpy(card
->longname
, longname
);
1123 strcpy(card
->shortname
, shortname
);
1125 /* Set up rawmidi */
1126 dev
->in_port
.dev
= dev
;
1127 dev
->in_port
.active
= 0;
1128 snd_component_add(card
, "MIDI");
1129 err
= snd_rawmidi_new(card
, "USB MIDI Gadget", 0,
1130 out_ports
, in_ports
, &rmidi
);
1132 ERROR(dev
, "snd_rawmidi_new failed: error %d\n", err
);
1136 strcpy(rmidi
->name
, card
->shortname
);
1137 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
1138 SNDRV_RAWMIDI_INFO_INPUT
|
1139 SNDRV_RAWMIDI_INFO_DUPLEX
;
1140 rmidi
->private_data
= dev
;
1142 /* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
1143 It's an upside-down world being a gadget. */
1144 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &gmidi_in_ops
);
1145 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &gmidi_out_ops
);
1147 snd_card_set_dev(card
, &dev
->gadget
->dev
);
1149 /* register it - we're ready to go */
1150 err
= snd_card_register(card
);
1152 ERROR(dev
, "snd_card_register failed\n");
1156 VDBG(dev
, "gmidi_register_card finished ok\n");
1161 snd_card_free(dev
->card
);
1168 * Creates an output endpoint, and initializes output ports.
1170 static int __devinit
gmidi_bind(struct usb_gadget
*gadget
)
1172 struct gmidi_device
*dev
;
1173 struct usb_ep
*in_ep
, *out_ep
;
1176 /* support optional vendor/distro customization */
1179 printk(KERN_ERR
"idVendor needs idProduct!\n");
1182 device_desc
.idVendor
= cpu_to_le16(idVendor
);
1183 device_desc
.idProduct
= cpu_to_le16(idProduct
);
1185 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
1188 if (iManufacturer
) {
1189 strlcpy(manufacturer
, iManufacturer
, sizeof(manufacturer
));
1191 snprintf(manufacturer
, sizeof(manufacturer
), "%s %s with %s",
1192 system_utsname
.sysname
, system_utsname
.release
,
1196 strlcpy(product_desc
, iProduct
, sizeof(product_desc
));
1198 if (iSerialNumber
) {
1199 device_desc
.iSerialNumber
= STRING_SERIAL
,
1200 strlcpy(serial_number
, iSerialNumber
, sizeof(serial_number
));
1203 /* Bulk-only drivers like this one SHOULD be able to
1204 * autoconfigure on any sane usb controller driver,
1205 * but there may also be important quirks to address.
1207 usb_ep_autoconfig_reset(gadget
);
1208 in_ep
= usb_ep_autoconfig(gadget
, &bulk_in_desc
);
1211 printk(KERN_ERR
"%s: can't autoconfigure on %s\n",
1212 shortname
, gadget
->name
);
1215 EP_IN_NAME
= in_ep
->name
;
1216 in_ep
->driver_data
= in_ep
; /* claim */
1218 out_ep
= usb_ep_autoconfig(gadget
, &bulk_out_desc
);
1222 EP_OUT_NAME
= out_ep
->name
;
1223 out_ep
->driver_data
= out_ep
; /* claim */
1225 gcnum
= usb_gadget_controller_number(gadget
);
1227 device_desc
.bcdDevice
= cpu_to_le16(0x0200 + gcnum
);
1229 /* gmidi is so simple (no altsettings) that
1230 * it SHOULD NOT have problems with bulk-capable hardware.
1231 * so warn about unrecognized controllers, don't panic.
1233 printk(KERN_WARNING
"%s: controller '%s' not recognized\n",
1234 shortname
, gadget
->name
);
1235 device_desc
.bcdDevice
= __constant_cpu_to_le16(0x9999);
1239 /* ok, we made sense of the hardware ... */
1240 dev
= kzalloc(sizeof(*dev
), SLAB_KERNEL
);
1244 spin_lock_init(&dev
->lock
);
1245 dev
->gadget
= gadget
;
1247 dev
->out_ep
= out_ep
;
1248 set_gadget_data(gadget
, dev
);
1249 tasklet_init(&dev
->tasklet
, gmidi_in_tasklet
, (unsigned long)dev
);
1251 /* preallocate control response and buffer */
1252 dev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
1257 dev
->req
->buf
= usb_ep_alloc_buffer(gadget
->ep0
, USB_BUFSIZ
,
1258 &dev
->req
->dma
, GFP_KERNEL
);
1259 if (!dev
->req
->buf
) {
1264 dev
->req
->complete
= gmidi_setup_complete
;
1266 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
1268 gadget
->ep0
->driver_data
= dev
;
1270 INFO(dev
, "%s, version: " DRIVER_VERSION
"\n", longname
);
1271 INFO(dev
, "using %s, OUT %s IN %s\n", gadget
->name
,
1272 EP_OUT_NAME
, EP_IN_NAME
);
1274 /* register as an ALSA sound card */
1275 err
= gmidi_register_card(dev
);
1280 VDBG(dev
, "gmidi_bind finished ok\n");
1284 gmidi_unbind(gadget
);
1289 static void gmidi_suspend(struct usb_gadget
*gadget
)
1291 struct gmidi_device
*dev
= get_gadget_data(gadget
);
1293 if (gadget
->speed
== USB_SPEED_UNKNOWN
) {
1297 DBG(dev
, "suspend\n");
1300 static void gmidi_resume(struct usb_gadget
*gadget
)
1302 struct gmidi_device
*dev
= get_gadget_data(gadget
);
1304 DBG(dev
, "resume\n");
1308 static struct usb_gadget_driver gmidi_driver
= {
1309 .speed
= USB_SPEED_FULL
,
1310 .function
= (char *)longname
,
1312 .unbind
= __exit_p(gmidi_unbind
),
1314 .setup
= gmidi_setup
,
1315 .disconnect
= gmidi_disconnect
,
1317 .suspend
= gmidi_suspend
,
1318 .resume
= gmidi_resume
,
1321 .name
= (char *)shortname
,
1322 .owner
= THIS_MODULE
,
1326 static int __init
gmidi_init(void)
1328 return usb_gadget_register_driver(&gmidi_driver
);
1330 module_init(gmidi_init
);
1332 static void __exit
gmidi_cleanup(void)
1334 usb_gadget_unregister_driver(&gmidi_driver
);
1336 module_exit(gmidi_cleanup
);