rfkill: yet more minor kernel-doc fixes for rfkill_toggle_radio
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / gmidi.c
blob7f4d4828e3aabaca7db67bd512017f094b419992
1 /*
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;
100 int active;
101 uint8_t cable; /* cable number << 4 */
102 uint8_t state;
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
110 uint8_t data[2];
113 struct gmidi_device {
114 spinlock_t lock;
115 struct usb_gadget *gadget;
116 struct usb_request *req; /* for control responses */
117 u8 config;
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 WARN(d, fmt, args...) \
142 dev_warn(&(d)->gadget->dev , fmt , ## args)
143 #define INFO(d, fmt, args...) \
144 dev_info(&(d)->gadget->dev , fmt , ## args)
147 static unsigned buflen = 256;
148 static unsigned qlen = 32;
150 module_param(buflen, uint, S_IRUGO);
151 module_param(qlen, uint, S_IRUGO);
154 /* Thanks to Grey Innovation for donating this product ID.
156 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
157 * Instead: allocate your own, using normal USB-IF procedures.
159 #define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
160 #define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
164 * DESCRIPTORS ... most are static, but strings and (full)
165 * configuration descriptors are built on demand.
168 #define STRING_MANUFACTURER 25
169 #define STRING_PRODUCT 42
170 #define STRING_SERIAL 101
171 #define STRING_MIDI_GADGET 250
173 /* We only have the one configuration, it's number 1. */
174 #define GMIDI_CONFIG 1
176 /* We have two interfaces- AudioControl and MIDIStreaming */
177 #define GMIDI_AC_INTERFACE 0
178 #define GMIDI_MS_INTERFACE 1
179 #define GMIDI_NUM_INTERFACES 2
181 DECLARE_USB_AC_HEADER_DESCRIPTOR(1);
182 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
183 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1);
185 /* B.1 Device Descriptor */
186 static struct usb_device_descriptor device_desc = {
187 .bLength = USB_DT_DEVICE_SIZE,
188 .bDescriptorType = USB_DT_DEVICE,
189 .bcdUSB = __constant_cpu_to_le16(0x0200),
190 .bDeviceClass = USB_CLASS_PER_INTERFACE,
191 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM),
192 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
193 .iManufacturer = STRING_MANUFACTURER,
194 .iProduct = STRING_PRODUCT,
195 .bNumConfigurations = 1,
198 /* B.2 Configuration Descriptor */
199 static struct usb_config_descriptor config_desc = {
200 .bLength = USB_DT_CONFIG_SIZE,
201 .bDescriptorType = USB_DT_CONFIG,
202 /* compute wTotalLength on the fly */
203 .bNumInterfaces = GMIDI_NUM_INTERFACES,
204 .bConfigurationValue = GMIDI_CONFIG,
205 .iConfiguration = STRING_MIDI_GADGET,
207 * FIXME: When embedding this driver in a device,
208 * these need to be set to reflect the actual
209 * power properties of the device. Is it selfpowered?
211 .bmAttributes = USB_CONFIG_ATT_ONE,
212 .bMaxPower = 1,
215 /* B.3.1 Standard AC Interface Descriptor */
216 static const struct usb_interface_descriptor ac_interface_desc = {
217 .bLength = USB_DT_INTERFACE_SIZE,
218 .bDescriptorType = USB_DT_INTERFACE,
219 .bInterfaceNumber = GMIDI_AC_INTERFACE,
220 .bNumEndpoints = 0,
221 .bInterfaceClass = USB_CLASS_AUDIO,
222 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
223 .iInterface = STRING_MIDI_GADGET,
226 /* B.3.2 Class-Specific AC Interface Descriptor */
227 static const struct usb_ac_header_descriptor_1 ac_header_desc = {
228 .bLength = USB_DT_AC_HEADER_SIZE(1),
229 .bDescriptorType = USB_DT_CS_INTERFACE,
230 .bDescriptorSubtype = USB_MS_HEADER,
231 .bcdADC = __constant_cpu_to_le16(0x0100),
232 .wTotalLength = __constant_cpu_to_le16(USB_DT_AC_HEADER_SIZE(1)),
233 .bInCollection = 1,
234 .baInterfaceNr = {
235 [0] = GMIDI_MS_INTERFACE,
239 /* B.4.1 Standard MS Interface Descriptor */
240 static const struct usb_interface_descriptor ms_interface_desc = {
241 .bLength = USB_DT_INTERFACE_SIZE,
242 .bDescriptorType = USB_DT_INTERFACE,
243 .bInterfaceNumber = GMIDI_MS_INTERFACE,
244 .bNumEndpoints = 2,
245 .bInterfaceClass = USB_CLASS_AUDIO,
246 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
247 .iInterface = STRING_MIDI_GADGET,
250 /* B.4.2 Class-Specific MS Interface Descriptor */
251 static const struct usb_ms_header_descriptor ms_header_desc = {
252 .bLength = USB_DT_MS_HEADER_SIZE,
253 .bDescriptorType = USB_DT_CS_INTERFACE,
254 .bDescriptorSubtype = USB_MS_HEADER,
255 .bcdMSC = __constant_cpu_to_le16(0x0100),
256 .wTotalLength = __constant_cpu_to_le16(USB_DT_MS_HEADER_SIZE
257 + 2*USB_DT_MIDI_IN_SIZE
258 + 2*USB_DT_MIDI_OUT_SIZE(1)),
261 #define JACK_IN_EMB 1
262 #define JACK_IN_EXT 2
263 #define JACK_OUT_EMB 3
264 #define JACK_OUT_EXT 4
266 /* B.4.3 MIDI IN Jack Descriptors */
267 static const struct usb_midi_in_jack_descriptor jack_in_emb_desc = {
268 .bLength = USB_DT_MIDI_IN_SIZE,
269 .bDescriptorType = USB_DT_CS_INTERFACE,
270 .bDescriptorSubtype = USB_MS_MIDI_IN_JACK,
271 .bJackType = USB_MS_EMBEDDED,
272 .bJackID = JACK_IN_EMB,
275 static const struct usb_midi_in_jack_descriptor jack_in_ext_desc = {
276 .bLength = USB_DT_MIDI_IN_SIZE,
277 .bDescriptorType = USB_DT_CS_INTERFACE,
278 .bDescriptorSubtype = USB_MS_MIDI_IN_JACK,
279 .bJackType = USB_MS_EXTERNAL,
280 .bJackID = JACK_IN_EXT,
283 /* B.4.4 MIDI OUT Jack Descriptors */
284 static const struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc = {
285 .bLength = USB_DT_MIDI_OUT_SIZE(1),
286 .bDescriptorType = USB_DT_CS_INTERFACE,
287 .bDescriptorSubtype = USB_MS_MIDI_OUT_JACK,
288 .bJackType = USB_MS_EMBEDDED,
289 .bJackID = JACK_OUT_EMB,
290 .bNrInputPins = 1,
291 .pins = {
292 [0] = {
293 .baSourceID = JACK_IN_EXT,
294 .baSourcePin = 1,
299 static const struct usb_midi_out_jack_descriptor_1 jack_out_ext_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_EXTERNAL,
304 .bJackID = JACK_OUT_EXT,
305 .bNrInputPins = 1,
306 .pins = {
307 [0] = {
308 .baSourceID = JACK_IN_EMB,
309 .baSourcePin = 1,
314 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
315 static struct usb_endpoint_descriptor bulk_out_desc = {
316 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
317 .bDescriptorType = USB_DT_ENDPOINT,
318 .bEndpointAddress = USB_DIR_OUT,
319 .bmAttributes = USB_ENDPOINT_XFER_BULK,
322 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
323 static const struct usb_ms_endpoint_descriptor_1 ms_out_desc = {
324 .bLength = USB_DT_MS_ENDPOINT_SIZE(1),
325 .bDescriptorType = USB_DT_CS_ENDPOINT,
326 .bDescriptorSubtype = USB_MS_GENERAL,
327 .bNumEmbMIDIJack = 1,
328 .baAssocJackID = {
329 [0] = JACK_IN_EMB,
333 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
334 static struct usb_endpoint_descriptor bulk_in_desc = {
335 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
336 .bDescriptorType = USB_DT_ENDPOINT,
337 .bEndpointAddress = USB_DIR_IN,
338 .bmAttributes = USB_ENDPOINT_XFER_BULK,
341 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
342 static const struct usb_ms_endpoint_descriptor_1 ms_in_desc = {
343 .bLength = USB_DT_MS_ENDPOINT_SIZE(1),
344 .bDescriptorType = USB_DT_CS_ENDPOINT,
345 .bDescriptorSubtype = USB_MS_GENERAL,
346 .bNumEmbMIDIJack = 1,
347 .baAssocJackID = {
348 [0] = JACK_OUT_EMB,
352 static const struct usb_descriptor_header *gmidi_function [] = {
353 (struct usb_descriptor_header *)&ac_interface_desc,
354 (struct usb_descriptor_header *)&ac_header_desc,
355 (struct usb_descriptor_header *)&ms_interface_desc,
357 (struct usb_descriptor_header *)&ms_header_desc,
358 (struct usb_descriptor_header *)&jack_in_emb_desc,
359 (struct usb_descriptor_header *)&jack_in_ext_desc,
360 (struct usb_descriptor_header *)&jack_out_emb_desc,
361 (struct usb_descriptor_header *)&jack_out_ext_desc,
362 /* If you add more jacks, update ms_header_desc.wTotalLength */
364 (struct usb_descriptor_header *)&bulk_out_desc,
365 (struct usb_descriptor_header *)&ms_out_desc,
366 (struct usb_descriptor_header *)&bulk_in_desc,
367 (struct usb_descriptor_header *)&ms_in_desc,
368 NULL,
371 static char manufacturer[50];
372 static char product_desc[40] = "MIDI Gadget";
373 static char serial_number[20];
375 /* static strings, in UTF-8 */
376 static struct usb_string strings [] = {
377 { STRING_MANUFACTURER, manufacturer, },
378 { STRING_PRODUCT, product_desc, },
379 { STRING_SERIAL, serial_number, },
380 { STRING_MIDI_GADGET, longname, },
381 { } /* end of list */
384 static struct usb_gadget_strings stringtab = {
385 .language = 0x0409, /* en-us */
386 .strings = strings,
389 static int config_buf(struct usb_gadget *gadget,
390 u8 *buf, u8 type, unsigned index)
392 int len;
394 /* only one configuration */
395 if (index != 0) {
396 return -EINVAL;
398 len = usb_gadget_config_buf(&config_desc,
399 buf, USB_BUFSIZ, gmidi_function);
400 if (len < 0) {
401 return len;
403 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
404 return len;
407 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
409 struct usb_request *req;
411 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
412 if (req) {
413 req->length = length;
414 req->buf = kmalloc(length, GFP_ATOMIC);
415 if (!req->buf) {
416 usb_ep_free_request(ep, req);
417 req = NULL;
420 return req;
423 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
425 kfree(req->buf);
426 usb_ep_free_request(ep, req);
429 static const uint8_t gmidi_cin_length[] = {
430 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
434 * Receives a chunk of MIDI data.
436 static void gmidi_read_data(struct usb_ep *ep, int cable,
437 uint8_t *data, int length)
439 struct gmidi_device *dev = ep->driver_data;
440 /* cable is ignored, because for now we only have one. */
442 if (!dev->out_substream) {
443 /* Nobody is listening - throw it on the floor. */
444 return;
446 if (!test_bit(dev->out_substream->number, &dev->out_triggered)) {
447 return;
449 snd_rawmidi_receive(dev->out_substream, data, length);
452 static void gmidi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
454 unsigned i;
455 u8 *buf = req->buf;
457 for (i = 0; i + 3 < req->actual; i += 4) {
458 if (buf[i] != 0) {
459 int cable = buf[i] >> 4;
460 int length = gmidi_cin_length[buf[i] & 0x0f];
461 gmidi_read_data(ep, cable, &buf[i + 1], length);
466 static void gmidi_complete(struct usb_ep *ep, struct usb_request *req)
468 struct gmidi_device *dev = ep->driver_data;
469 int status = req->status;
471 switch (status) {
472 case 0: /* normal completion */
473 if (ep == dev->out_ep) {
474 /* we received stuff.
475 req is queued again, below */
476 gmidi_handle_out_data(ep, req);
477 } else if (ep == dev->in_ep) {
478 /* our transmit completed.
479 see if there's more to go.
480 gmidi_transmit eats req, don't queue it again. */
481 gmidi_transmit(dev, req);
482 return;
484 break;
486 /* this endpoint is normally active while we're configured */
487 case -ECONNABORTED: /* hardware forced ep reset */
488 case -ECONNRESET: /* request dequeued */
489 case -ESHUTDOWN: /* disconnect from host */
490 VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
491 req->actual, req->length);
492 if (ep == dev->out_ep) {
493 gmidi_handle_out_data(ep, req);
495 free_ep_req(ep, req);
496 return;
498 case -EOVERFLOW: /* buffer overrun on read means that
499 * we didn't provide a big enough
500 * buffer.
502 default:
503 DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
504 status, req->actual, req->length);
505 break;
506 case -EREMOTEIO: /* short read */
507 break;
510 status = usb_ep_queue(ep, req, GFP_ATOMIC);
511 if (status) {
512 ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
513 ep->name, req->length, status);
514 usb_ep_set_halt(ep);
515 /* FIXME recover later ... somehow */
519 static int set_gmidi_config(struct gmidi_device *dev, gfp_t gfp_flags)
521 int err = 0;
522 struct usb_request *req;
523 struct usb_ep *ep;
524 unsigned i;
526 err = usb_ep_enable(dev->in_ep, &bulk_in_desc);
527 if (err) {
528 ERROR(dev, "can't start %s: %d\n", dev->in_ep->name, err);
529 goto fail;
531 dev->in_ep->driver_data = dev;
533 err = usb_ep_enable(dev->out_ep, &bulk_out_desc);
534 if (err) {
535 ERROR(dev, "can't start %s: %d\n", dev->out_ep->name, err);
536 goto fail;
538 dev->out_ep->driver_data = dev;
540 /* allocate a bunch of read buffers and queue them all at once. */
541 ep = dev->out_ep;
542 for (i = 0; i < qlen && err == 0; i++) {
543 req = alloc_ep_req(ep, buflen);
544 if (req) {
545 req->complete = gmidi_complete;
546 err = usb_ep_queue(ep, req, GFP_ATOMIC);
547 if (err) {
548 DBG(dev, "%s queue req: %d\n", ep->name, err);
550 } else {
551 err = -ENOMEM;
554 fail:
555 /* caller is responsible for cleanup on error */
556 return err;
560 static void gmidi_reset_config(struct gmidi_device *dev)
562 if (dev->config == 0) {
563 return;
566 DBG(dev, "reset config\n");
568 /* just disable endpoints, forcing completion of pending i/o.
569 * all our completion handlers free their requests in this case.
571 usb_ep_disable(dev->in_ep);
572 usb_ep_disable(dev->out_ep);
573 dev->config = 0;
576 /* change our operational config. this code must agree with the code
577 * that returns config descriptors, and altsetting code.
579 * it's also responsible for power management interactions. some
580 * configurations might not work with our current power sources.
582 * note that some device controller hardware will constrain what this
583 * code can do, perhaps by disallowing more than one configuration or
584 * by limiting configuration choices (like the pxa2xx).
586 static int
587 gmidi_set_config(struct gmidi_device *dev, unsigned number, gfp_t gfp_flags)
589 int result = 0;
590 struct usb_gadget *gadget = dev->gadget;
592 #if 0
593 /* FIXME */
594 /* Hacking this bit out fixes a bug where on receipt of two
595 USB_REQ_SET_CONFIGURATION messages, we end up with no
596 buffered OUT requests waiting for data. This is clearly
597 hiding a bug elsewhere, because if the config didn't
598 change then we really shouldn't do anything. */
599 /* Having said that, when we do "change" from config 1
600 to config 1, we at least gmidi_reset_config() which
601 clears out any requests on endpoints, so it's not like
602 we leak or anything. */
603 if (number == dev->config) {
604 return 0;
606 #endif
608 if (gadget_is_sa1100(gadget) && dev->config) {
609 /* tx fifo is full, but we can't clear it...*/
610 ERROR(dev, "can't change configurations\n");
611 return -ESPIPE;
613 gmidi_reset_config(dev);
615 switch (number) {
616 case GMIDI_CONFIG:
617 result = set_gmidi_config(dev, gfp_flags);
618 break;
619 default:
620 result = -EINVAL;
621 /* FALL THROUGH */
622 case 0:
623 return result;
626 if (!result && (!dev->in_ep || !dev->out_ep)) {
627 result = -ENODEV;
629 if (result) {
630 gmidi_reset_config(dev);
631 } else {
632 char *speed;
634 switch (gadget->speed) {
635 case USB_SPEED_LOW: speed = "low"; break;
636 case USB_SPEED_FULL: speed = "full"; break;
637 case USB_SPEED_HIGH: speed = "high"; break;
638 default: speed = "?"; break;
641 dev->config = number;
642 INFO(dev, "%s speed\n", speed);
644 return result;
648 static void gmidi_setup_complete(struct usb_ep *ep, struct usb_request *req)
650 if (req->status || req->actual != req->length) {
651 DBG((struct gmidi_device *) ep->driver_data,
652 "setup complete --> %d, %d/%d\n",
653 req->status, req->actual, req->length);
658 * The setup() callback implements all the ep0 functionality that's
659 * not handled lower down, in hardware or the hardware driver (like
660 * device and endpoint feature flags, and their status). It's all
661 * housekeeping for the gadget function we're implementing. Most of
662 * the work is in config-specific setup.
664 static int gmidi_setup(struct usb_gadget *gadget,
665 const struct usb_ctrlrequest *ctrl)
667 struct gmidi_device *dev = get_gadget_data(gadget);
668 struct usb_request *req = dev->req;
669 int value = -EOPNOTSUPP;
670 u16 w_index = le16_to_cpu(ctrl->wIndex);
671 u16 w_value = le16_to_cpu(ctrl->wValue);
672 u16 w_length = le16_to_cpu(ctrl->wLength);
674 /* usually this stores reply data in the pre-allocated ep0 buffer,
675 * but config change events will reconfigure hardware.
677 req->zero = 0;
678 switch (ctrl->bRequest) {
680 case USB_REQ_GET_DESCRIPTOR:
681 if (ctrl->bRequestType != USB_DIR_IN) {
682 goto unknown;
684 switch (w_value >> 8) {
686 case USB_DT_DEVICE:
687 value = min(w_length, (u16) sizeof(device_desc));
688 memcpy(req->buf, &device_desc, value);
689 break;
690 case USB_DT_CONFIG:
691 value = config_buf(gadget, req->buf,
692 w_value >> 8,
693 w_value & 0xff);
694 if (value >= 0) {
695 value = min(w_length, (u16)value);
697 break;
699 case USB_DT_STRING:
700 /* wIndex == language code.
701 * this driver only handles one language, you can
702 * add string tables for other languages, using
703 * any UTF-8 characters
705 value = usb_gadget_get_string(&stringtab,
706 w_value & 0xff, req->buf);
707 if (value >= 0) {
708 value = min(w_length, (u16)value);
710 break;
712 break;
714 /* currently two configs, two speeds */
715 case USB_REQ_SET_CONFIGURATION:
716 if (ctrl->bRequestType != 0) {
717 goto unknown;
719 if (gadget->a_hnp_support) {
720 DBG(dev, "HNP available\n");
721 } else if (gadget->a_alt_hnp_support) {
722 DBG(dev, "HNP needs a different root port\n");
723 } else {
724 VDBG(dev, "HNP inactive\n");
726 spin_lock(&dev->lock);
727 value = gmidi_set_config(dev, w_value, GFP_ATOMIC);
728 spin_unlock(&dev->lock);
729 break;
730 case USB_REQ_GET_CONFIGURATION:
731 if (ctrl->bRequestType != USB_DIR_IN) {
732 goto unknown;
734 *(u8 *)req->buf = dev->config;
735 value = min(w_length, (u16)1);
736 break;
738 /* until we add altsetting support, or other interfaces,
739 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
740 * and already killed pending endpoint I/O.
742 case USB_REQ_SET_INTERFACE:
743 if (ctrl->bRequestType != USB_RECIP_INTERFACE) {
744 goto unknown;
746 spin_lock(&dev->lock);
747 if (dev->config && w_index < GMIDI_NUM_INTERFACES
748 && w_value == 0)
750 u8 config = dev->config;
752 /* resets interface configuration, forgets about
753 * previous transaction state (queued bufs, etc)
754 * and re-inits endpoint state (toggle etc)
755 * no response queued, just zero status == success.
756 * if we had more than one interface we couldn't
757 * use this "reset the config" shortcut.
759 gmidi_reset_config(dev);
760 gmidi_set_config(dev, config, GFP_ATOMIC);
761 value = 0;
763 spin_unlock(&dev->lock);
764 break;
765 case USB_REQ_GET_INTERFACE:
766 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) {
767 goto unknown;
769 if (!dev->config) {
770 break;
772 if (w_index >= GMIDI_NUM_INTERFACES) {
773 value = -EDOM;
774 break;
776 *(u8 *)req->buf = 0;
777 value = min(w_length, (u16)1);
778 break;
780 default:
781 unknown:
782 VDBG(dev, "unknown control req%02x.%02x v%04x i%04x l%d\n",
783 ctrl->bRequestType, ctrl->bRequest,
784 w_value, w_index, w_length);
787 /* respond with data transfer before status phase? */
788 if (value >= 0) {
789 req->length = value;
790 req->zero = value < w_length;
791 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
792 if (value < 0) {
793 DBG(dev, "ep_queue --> %d\n", value);
794 req->status = 0;
795 gmidi_setup_complete(gadget->ep0, req);
799 /* device either stalls (value < 0) or reports success */
800 return value;
803 static void gmidi_disconnect(struct usb_gadget *gadget)
805 struct gmidi_device *dev = get_gadget_data(gadget);
806 unsigned long flags;
808 spin_lock_irqsave(&dev->lock, flags);
809 gmidi_reset_config(dev);
811 /* a more significant application might have some non-usb
812 * activities to quiesce here, saving resources like power
813 * or pushing the notification up a network stack.
815 spin_unlock_irqrestore(&dev->lock, flags);
817 /* next we may get setup() calls to enumerate new connections;
818 * or an unbind() during shutdown (including removing module).
822 static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget *gadget)
824 struct gmidi_device *dev = get_gadget_data(gadget);
825 struct snd_card *card;
827 DBG(dev, "unbind\n");
829 card = dev->card;
830 dev->card = NULL;
831 if (card) {
832 snd_card_free(card);
835 /* we've already been disconnected ... no i/o is active */
836 if (dev->req) {
837 dev->req->length = USB_BUFSIZ;
838 free_ep_req(gadget->ep0, dev->req);
840 kfree(dev);
841 set_gadget_data(gadget, NULL);
844 static int gmidi_snd_free(struct snd_device *device)
846 return 0;
849 static void gmidi_transmit_packet(struct usb_request *req, uint8_t p0,
850 uint8_t p1, uint8_t p2, uint8_t p3)
852 unsigned length = req->length;
853 u8 *buf = (u8 *)req->buf + length;
855 buf[0] = p0;
856 buf[1] = p1;
857 buf[2] = p2;
858 buf[3] = p3;
859 req->length = length + 4;
863 * Converts MIDI commands to USB MIDI packets.
865 static void gmidi_transmit_byte(struct usb_request *req,
866 struct gmidi_in_port *port, uint8_t b)
868 uint8_t p0 = port->cable;
870 if (b >= 0xf8) {
871 gmidi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
872 } else if (b >= 0xf0) {
873 switch (b) {
874 case 0xf0:
875 port->data[0] = b;
876 port->state = STATE_SYSEX_1;
877 break;
878 case 0xf1:
879 case 0xf3:
880 port->data[0] = b;
881 port->state = STATE_1PARAM;
882 break;
883 case 0xf2:
884 port->data[0] = b;
885 port->state = STATE_2PARAM_1;
886 break;
887 case 0xf4:
888 case 0xf5:
889 port->state = STATE_UNKNOWN;
890 break;
891 case 0xf6:
892 gmidi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
893 port->state = STATE_UNKNOWN;
894 break;
895 case 0xf7:
896 switch (port->state) {
897 case STATE_SYSEX_0:
898 gmidi_transmit_packet(req,
899 p0 | 0x05, 0xf7, 0, 0);
900 break;
901 case STATE_SYSEX_1:
902 gmidi_transmit_packet(req,
903 p0 | 0x06, port->data[0], 0xf7, 0);
904 break;
905 case STATE_SYSEX_2:
906 gmidi_transmit_packet(req,
907 p0 | 0x07, port->data[0],
908 port->data[1], 0xf7);
909 break;
911 port->state = STATE_UNKNOWN;
912 break;
914 } else if (b >= 0x80) {
915 port->data[0] = b;
916 if (b >= 0xc0 && b <= 0xdf)
917 port->state = STATE_1PARAM;
918 else
919 port->state = STATE_2PARAM_1;
920 } else { /* b < 0x80 */
921 switch (port->state) {
922 case STATE_1PARAM:
923 if (port->data[0] < 0xf0) {
924 p0 |= port->data[0] >> 4;
925 } else {
926 p0 |= 0x02;
927 port->state = STATE_UNKNOWN;
929 gmidi_transmit_packet(req, p0, port->data[0], b, 0);
930 break;
931 case STATE_2PARAM_1:
932 port->data[1] = b;
933 port->state = STATE_2PARAM_2;
934 break;
935 case STATE_2PARAM_2:
936 if (port->data[0] < 0xf0) {
937 p0 |= port->data[0] >> 4;
938 port->state = STATE_2PARAM_1;
939 } else {
940 p0 |= 0x03;
941 port->state = STATE_UNKNOWN;
943 gmidi_transmit_packet(req,
944 p0, port->data[0], port->data[1], b);
945 break;
946 case STATE_SYSEX_0:
947 port->data[0] = b;
948 port->state = STATE_SYSEX_1;
949 break;
950 case STATE_SYSEX_1:
951 port->data[1] = b;
952 port->state = STATE_SYSEX_2;
953 break;
954 case STATE_SYSEX_2:
955 gmidi_transmit_packet(req,
956 p0 | 0x04, port->data[0], port->data[1], b);
957 port->state = STATE_SYSEX_0;
958 break;
963 static void gmidi_transmit(struct gmidi_device *dev, struct usb_request *req)
965 struct usb_ep *ep = dev->in_ep;
966 struct gmidi_in_port *port = &dev->in_port;
968 if (!ep) {
969 return;
971 if (!req) {
972 req = alloc_ep_req(ep, buflen);
974 if (!req) {
975 ERROR(dev, "gmidi_transmit: alloc_ep_request failed\n");
976 return;
978 req->length = 0;
979 req->complete = gmidi_complete;
981 if (port->active) {
982 while (req->length + 3 < buflen) {
983 uint8_t b;
984 if (snd_rawmidi_transmit(dev->in_substream, &b, 1)
985 != 1)
987 port->active = 0;
988 break;
990 gmidi_transmit_byte(req, port, b);
993 if (req->length > 0) {
994 usb_ep_queue(ep, req, GFP_ATOMIC);
995 } else {
996 free_ep_req(ep, req);
1000 static void gmidi_in_tasklet(unsigned long data)
1002 struct gmidi_device *dev = (struct gmidi_device *)data;
1004 gmidi_transmit(dev, NULL);
1007 static int gmidi_in_open(struct snd_rawmidi_substream *substream)
1009 struct gmidi_device *dev = substream->rmidi->private_data;
1011 VDBG(dev, "gmidi_in_open\n");
1012 dev->in_substream = substream;
1013 dev->in_port.state = STATE_UNKNOWN;
1014 return 0;
1017 static int gmidi_in_close(struct snd_rawmidi_substream *substream)
1019 struct gmidi_device *dev = substream->rmidi->private_data;
1021 VDBG(dev, "gmidi_in_close\n");
1022 return 0;
1025 static void gmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
1027 struct gmidi_device *dev = substream->rmidi->private_data;
1029 VDBG(dev, "gmidi_in_trigger %d\n", up);
1030 dev->in_port.active = up;
1031 if (up) {
1032 tasklet_hi_schedule(&dev->tasklet);
1036 static int gmidi_out_open(struct snd_rawmidi_substream *substream)
1038 struct gmidi_device *dev = substream->rmidi->private_data;
1040 VDBG(dev, "gmidi_out_open\n");
1041 dev->out_substream = substream;
1042 return 0;
1045 static int gmidi_out_close(struct snd_rawmidi_substream *substream)
1047 struct gmidi_device *dev = substream->rmidi->private_data;
1049 VDBG(dev, "gmidi_out_close\n");
1050 return 0;
1053 static void gmidi_out_trigger(struct snd_rawmidi_substream *substream, int up)
1055 struct gmidi_device *dev = substream->rmidi->private_data;
1057 VDBG(dev, "gmidi_out_trigger %d\n", up);
1058 if (up) {
1059 set_bit(substream->number, &dev->out_triggered);
1060 } else {
1061 clear_bit(substream->number, &dev->out_triggered);
1065 static struct snd_rawmidi_ops gmidi_in_ops = {
1066 .open = gmidi_in_open,
1067 .close = gmidi_in_close,
1068 .trigger = gmidi_in_trigger,
1071 static struct snd_rawmidi_ops gmidi_out_ops = {
1072 .open = gmidi_out_open,
1073 .close = gmidi_out_close,
1074 .trigger = gmidi_out_trigger
1077 /* register as a sound "card" */
1078 static int gmidi_register_card(struct gmidi_device *dev)
1080 struct snd_card *card;
1081 struct snd_rawmidi *rmidi;
1082 int err;
1083 int out_ports = 1;
1084 int in_ports = 1;
1085 static struct snd_device_ops ops = {
1086 .dev_free = gmidi_snd_free,
1089 card = snd_card_new(index, id, THIS_MODULE, 0);
1090 if (!card) {
1091 ERROR(dev, "snd_card_new failed\n");
1092 err = -ENOMEM;
1093 goto fail;
1095 dev->card = card;
1097 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, dev, &ops);
1098 if (err < 0) {
1099 ERROR(dev, "snd_device_new failed: error %d\n", err);
1100 goto fail;
1103 strcpy(card->driver, longname);
1104 strcpy(card->longname, longname);
1105 strcpy(card->shortname, shortname);
1107 /* Set up rawmidi */
1108 dev->in_port.dev = dev;
1109 dev->in_port.active = 0;
1110 snd_component_add(card, "MIDI");
1111 err = snd_rawmidi_new(card, "USB MIDI Gadget", 0,
1112 out_ports, in_ports, &rmidi);
1113 if (err < 0) {
1114 ERROR(dev, "snd_rawmidi_new failed: error %d\n", err);
1115 goto fail;
1117 dev->rmidi = rmidi;
1118 strcpy(rmidi->name, card->shortname);
1119 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1120 SNDRV_RAWMIDI_INFO_INPUT |
1121 SNDRV_RAWMIDI_INFO_DUPLEX;
1122 rmidi->private_data = dev;
1124 /* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
1125 It's an upside-down world being a gadget. */
1126 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
1127 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
1129 snd_card_set_dev(card, &dev->gadget->dev);
1131 /* register it - we're ready to go */
1132 err = snd_card_register(card);
1133 if (err < 0) {
1134 ERROR(dev, "snd_card_register failed\n");
1135 goto fail;
1138 VDBG(dev, "gmidi_register_card finished ok\n");
1139 return 0;
1141 fail:
1142 if (dev->card) {
1143 snd_card_free(dev->card);
1144 dev->card = NULL;
1146 return err;
1150 * Creates an output endpoint, and initializes output ports.
1152 static int __init gmidi_bind(struct usb_gadget *gadget)
1154 struct gmidi_device *dev;
1155 struct usb_ep *in_ep, *out_ep;
1156 int gcnum, err = 0;
1158 /* support optional vendor/distro customization */
1159 if (idVendor) {
1160 if (!idProduct) {
1161 pr_err("idVendor needs idProduct!\n");
1162 return -ENODEV;
1164 device_desc.idVendor = cpu_to_le16(idVendor);
1165 device_desc.idProduct = cpu_to_le16(idProduct);
1166 if (bcdDevice) {
1167 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1170 if (iManufacturer) {
1171 strlcpy(manufacturer, iManufacturer, sizeof(manufacturer));
1172 } else {
1173 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1174 init_utsname()->sysname, init_utsname()->release,
1175 gadget->name);
1177 if (iProduct) {
1178 strlcpy(product_desc, iProduct, sizeof(product_desc));
1180 if (iSerialNumber) {
1181 device_desc.iSerialNumber = STRING_SERIAL,
1182 strlcpy(serial_number, iSerialNumber, sizeof(serial_number));
1185 /* Bulk-only drivers like this one SHOULD be able to
1186 * autoconfigure on any sane usb controller driver,
1187 * but there may also be important quirks to address.
1189 usb_ep_autoconfig_reset(gadget);
1190 in_ep = usb_ep_autoconfig(gadget, &bulk_in_desc);
1191 if (!in_ep) {
1192 autoconf_fail:
1193 pr_err("%s: can't autoconfigure on %s\n",
1194 shortname, gadget->name);
1195 return -ENODEV;
1197 EP_IN_NAME = in_ep->name;
1198 in_ep->driver_data = in_ep; /* claim */
1200 out_ep = usb_ep_autoconfig(gadget, &bulk_out_desc);
1201 if (!out_ep) {
1202 goto autoconf_fail;
1204 EP_OUT_NAME = out_ep->name;
1205 out_ep->driver_data = out_ep; /* claim */
1207 gcnum = usb_gadget_controller_number(gadget);
1208 if (gcnum >= 0) {
1209 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1210 } else {
1211 /* gmidi is so simple (no altsettings) that
1212 * it SHOULD NOT have problems with bulk-capable hardware.
1213 * so warn about unrecognized controllers, don't panic.
1215 pr_warning("%s: controller '%s' not recognized\n",
1216 shortname, gadget->name);
1217 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1221 /* ok, we made sense of the hardware ... */
1222 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1223 if (!dev) {
1224 return -ENOMEM;
1226 spin_lock_init(&dev->lock);
1227 dev->gadget = gadget;
1228 dev->in_ep = in_ep;
1229 dev->out_ep = out_ep;
1230 set_gadget_data(gadget, dev);
1231 tasklet_init(&dev->tasklet, gmidi_in_tasklet, (unsigned long)dev);
1233 /* preallocate control response and buffer */
1234 dev->req = alloc_ep_req(gadget->ep0, USB_BUFSIZ);
1235 if (!dev->req) {
1236 err = -ENOMEM;
1237 goto fail;
1240 dev->req->complete = gmidi_setup_complete;
1242 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1244 gadget->ep0->driver_data = dev;
1246 INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
1247 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
1248 EP_OUT_NAME, EP_IN_NAME);
1250 /* register as an ALSA sound card */
1251 err = gmidi_register_card(dev);
1252 if (err < 0) {
1253 goto fail;
1256 VDBG(dev, "gmidi_bind finished ok\n");
1257 return 0;
1259 fail:
1260 gmidi_unbind(gadget);
1261 return err;
1265 static void gmidi_suspend(struct usb_gadget *gadget)
1267 struct gmidi_device *dev = get_gadget_data(gadget);
1269 if (gadget->speed == USB_SPEED_UNKNOWN) {
1270 return;
1273 DBG(dev, "suspend\n");
1276 static void gmidi_resume(struct usb_gadget *gadget)
1278 struct gmidi_device *dev = get_gadget_data(gadget);
1280 DBG(dev, "resume\n");
1284 static struct usb_gadget_driver gmidi_driver = {
1285 .speed = USB_SPEED_FULL,
1286 .function = (char *)longname,
1287 .bind = gmidi_bind,
1288 .unbind = gmidi_unbind,
1290 .setup = gmidi_setup,
1291 .disconnect = gmidi_disconnect,
1293 .suspend = gmidi_suspend,
1294 .resume = gmidi_resume,
1296 .driver = {
1297 .name = (char *)shortname,
1298 .owner = THIS_MODULE,
1302 static int __init gmidi_init(void)
1304 return usb_gadget_register_driver(&gmidi_driver);
1306 module_init(gmidi_init);
1308 static void __exit gmidi_cleanup(void)
1310 usb_gadget_unregister_driver(&gmidi_driver);
1312 module_exit(gmidi_cleanup);