aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/usb/gadget/Kconfig14
-rw-r--r--drivers/usb/gadget/Makefile2
-rw-r--r--drivers/usb/gadget/gmidi.c1337
-rw-r--r--include/linux/usb/audio.h53
-rw-r--r--include/linux/usb/midi.h112
5 files changed, 1518 insertions, 0 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 1a32d96774b4..4301e96c417b 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -404,6 +404,20 @@ config USB_G_SERIAL
404 which includes instructions and a "driver info file" needed to 404 which includes instructions and a "driver info file" needed to
405 make MS-Windows work with this driver. 405 make MS-Windows work with this driver.
406 406
407config USB_MIDI_GADGET
408 tristate "MIDI Gadget (EXPERIMENTAL)"
409 depends on SND && EXPERIMENTAL
410 select SND_RAWMIDI
411 help
412 The MIDI Gadget acts as a USB Audio device, with one MIDI
413 input and one MIDI output. These MIDI jacks appear as
414 a sound "card" in the ALSA sound system. Other MIDI
415 connections can then be made on the gadget system, using
416 ALSA's aconnect utility etc.
417
418 Say "y" to link the driver statically, or "m" to build a
419 dynamically linked module called "g_midi".
420
407 421
408# put drivers that need isochronous transfer support (for audio 422# put drivers that need isochronous transfer support (for audio
409# or video class gadget drivers), or specific hardware, here. 423# or video class gadget drivers), or specific hardware, here.
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 5a28e61392ec..e71e086a1cfa 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_USB_AT91) += at91_udc.o
15g_zero-objs := zero.o usbstring.o config.o epautoconf.o 15g_zero-objs := zero.o usbstring.o config.o epautoconf.o
16g_ether-objs := ether.o usbstring.o config.o epautoconf.o 16g_ether-objs := ether.o usbstring.o config.o epautoconf.o
17g_serial-objs := serial.o usbstring.o config.o epautoconf.o 17g_serial-objs := serial.o usbstring.o config.o epautoconf.o
18g_midi-objs := gmidi.o usbstring.o config.o epautoconf.o
18gadgetfs-objs := inode.o 19gadgetfs-objs := inode.o
19g_file_storage-objs := file_storage.o usbstring.o config.o \ 20g_file_storage-objs := file_storage.o usbstring.o config.o \
20 epautoconf.o 21 epautoconf.o
@@ -28,4 +29,5 @@ obj-$(CONFIG_USB_ETH) += g_ether.o
28obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o 29obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
29obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o 30obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
30obj-$(CONFIG_USB_G_SERIAL) += g_serial.o 31obj-$(CONFIG_USB_G_SERIAL) += g_serial.o
32obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o
31 33
diff --git a/drivers/usb/gadget/gmidi.c b/drivers/usb/gadget/gmidi.c
new file mode 100644
index 000000000000..b68cecd57411
--- /dev/null
+++ b/drivers/usb/gadget/gmidi.c
@@ -0,0 +1,1337 @@
1/*
2 * gmidi.c -- USB MIDI Gadget Driver
3 *
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
7 *
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.
10 *
11 * This code is based in part on:
12 *
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.
16 *
17 * Refer to the USB Device Class Definition for MIDI Devices:
18 * http://www.usb.org/developers/devclass_docs/midi10.pdf
19 */
20
21#define DEBUG 1
22// #define VERBOSE
23
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>
33
34#include <sound/driver.h>
35#include <sound/core.h>
36#include <sound/initval.h>
37#include <sound/rawmidi.h>
38
39#include <linux/usb_ch9.h>
40#include <linux/usb_gadget.h>
41#include <linux/usb/audio.h>
42#include <linux/usb/midi.h>
43
44#include "gadget_chips.h"
45
46MODULE_AUTHOR("Ben Williamson");
47MODULE_LICENSE("GPL v2");
48
49#define DRIVER_VERSION "25 Jul 2006"
50
51static const char shortname[] = "g_midi";
52static const char longname[] = "MIDI Gadget";
53
54static int index = SNDRV_DEFAULT_IDX1;
55static char *id = SNDRV_DEFAULT_STR1;
56
57module_param(index, int, 0444);
58MODULE_PARM_DESC(index, "Index value for the USB MIDI Gadget adapter.");
59module_param(id, charp, 0444);
60MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter.");
61
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).
65 */
66
67static ushort idVendor;
68module_param(idVendor, ushort, S_IRUGO);
69MODULE_PARM_DESC(idVendor, "USB Vendor ID");
70
71static ushort idProduct;
72module_param(idProduct, ushort, S_IRUGO);
73MODULE_PARM_DESC(idProduct, "USB Product ID");
74
75static ushort bcdDevice;
76module_param(bcdDevice, ushort, S_IRUGO);
77MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
78
79static char *iManufacturer;
80module_param(iManufacturer, charp, S_IRUGO);
81MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
82
83static char *iProduct;
84module_param(iProduct, charp, S_IRUGO);
85MODULE_PARM_DESC(iProduct, "USB Product string");
86
87static char *iSerialNumber;
88module_param(iSerialNumber, charp, S_IRUGO);
89MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
90
91/*
92 * this version autoconfigures as much as possible,
93 * which is reasonable for most "bulk-only" drivers.
94 */
95static const char *EP_IN_NAME;
96static const char *EP_OUT_NAME;
97
98
99/* big enough to hold our biggest descriptor */
100#define USB_BUFSIZ 256
101
102
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 */
106struct gmidi_in_port {
107 struct gmidi_device* dev;
108 int active;
109 uint8_t cable; /* cable number << 4 */
110 uint8_t state;
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
118 uint8_t data[2];
119};
120
121struct gmidi_device {
122 spinlock_t lock;
123 struct usb_gadget *gadget;
124 struct usb_request *req; /* for control responses */
125 u8 config;
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;
131
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;
138};
139
140static void gmidi_transmit(struct gmidi_device* dev, struct usb_request* req);
141
142
143#define xprintk(d,level,fmt,args...) \
144 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
145
146#ifdef DEBUG
147#define DBG(dev,fmt,args...) \
148 xprintk(dev , KERN_DEBUG , fmt , ## args)
149#else
150#define DBG(dev,fmt,args...) \
151 do { } while (0)
152#endif /* DEBUG */
153
154#ifdef VERBOSE
155#define VDBG DBG
156#else
157#define VDBG(dev,fmt,args...) \
158 do { } while (0)
159#endif /* VERBOSE */
160
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)
167
168
169static unsigned buflen = 256;
170static unsigned qlen = 32;
171
172module_param(buflen, uint, S_IRUGO);
173module_param(qlen, uint, S_IRUGO);
174
175
176/* Thanks to Grey Innovation for donating this product ID.
177 *
178 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
179 * Instead: allocate your own, using normal USB-IF procedures.
180 */
181#define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
182#define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
183
184
185/*
186 * DESCRIPTORS ... most are static, but strings and (full)
187 * configuration descriptors are built on demand.
188 */
189
190#define STRING_MANUFACTURER 25
191#define STRING_PRODUCT 42
192#define STRING_SERIAL 101
193#define STRING_MIDI_GADGET 250
194
195/* We only have the one configuration, it's number 1. */
196#define GMIDI_CONFIG 1
197
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
202
203DECLARE_USB_AC_HEADER_DESCRIPTOR(1);
204DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
205DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1);
206
207/* B.1 Device Descriptor */
208static 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,
218};
219
220/* B.2 Configuration Descriptor */
221static 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,
228 /*
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?
232 */
233 .bmAttributes = USB_CONFIG_ATT_ONE,
234 .bMaxPower = 1,
235};
236
237/* B.3.1 Standard AC Interface Descriptor */
238static const struct usb_interface_descriptor ac_interface_desc = {
239 .bLength = USB_DT_INTERFACE_SIZE,
240 .bDescriptorType = USB_DT_INTERFACE,
241 .bInterfaceNumber = GMIDI_AC_INTERFACE,
242 .bNumEndpoints = 0,
243 .bInterfaceClass = USB_CLASS_AUDIO,
244 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
245 .iInterface = STRING_MIDI_GADGET,
246};
247
248/* B.3.2 Class-Specific AC Interface Descriptor */
249static 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),
255 .bInCollection = 1,
256 .baInterfaceNr = {
257 [0] = GMIDI_MS_INTERFACE,
258 }
259};
260
261/* B.4.1 Standard MS Interface Descriptor */
262static const struct usb_interface_descriptor ms_interface_desc = {
263 .bLength = USB_DT_INTERFACE_SIZE,
264 .bDescriptorType = USB_DT_INTERFACE,
265 .bInterfaceNumber = GMIDI_MS_INTERFACE,
266 .bNumEndpoints = 2,
267 .bInterfaceClass = USB_CLASS_AUDIO,
268 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
269 .iInterface = STRING_MIDI_GADGET,
270};
271
272/* B.4.2 Class-Specific MS Interface Descriptor */
273static 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),
281};
282
283#define JACK_IN_EMB 1
284#define JACK_IN_EXT 2
285#define JACK_OUT_EMB 3
286#define JACK_OUT_EXT 4
287
288/* B.4.3 MIDI IN Jack Descriptors */
289static 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,
295};
296
297static 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,
303};
304
305/* B.4.4 MIDI OUT Jack Descriptors */
306static 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,
312 .bNrInputPins = 1,
313 .pins = {
314 [0] = {
315 .baSourceID = JACK_IN_EXT,
316 .baSourcePin = 1,
317 }
318 }
319};
320
321static 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,
327 .bNrInputPins = 1,
328 .pins = {
329 [0] = {
330 .baSourceID = JACK_IN_EMB,
331 .baSourcePin = 1,
332 }
333 }
334};
335
336/* B.5.1 Standard Bulk OUT Endpoint Descriptor */
337static 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,
342};
343
344/* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
345static 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,
350 .baAssocJackID = {
351 [0] = JACK_IN_EMB,
352 }
353};
354
355/* B.6.1 Standard Bulk IN Endpoint Descriptor */
356static 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,
361};
362
363/* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
364static 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,
369 .baAssocJackID = {
370 [0] = JACK_OUT_EMB,
371 }
372};
373
374static 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,
378
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 */
385
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,
390 NULL,
391};
392
393static char manufacturer[50];
394static char product_desc[40] = "MIDI Gadget";
395static char serial_number[20];
396
397/* static strings, in UTF-8 */
398static 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 */
404};
405
406static struct usb_gadget_strings stringtab = {
407 .language = 0x0409, /* en-us */
408 .strings = strings,
409};
410
411static int config_buf(struct usb_gadget *gadget,
412 u8 *buf, u8 type, unsigned index)
413{
414 int len;
415
416 /* only one configuration */
417 if (index != 0) {
418 return -EINVAL;
419 }
420 len = usb_gadget_config_buf(&config_desc,
421 buf, USB_BUFSIZ, gmidi_function);
422 if (len < 0) {
423 return len;
424 }
425 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
426 return len;
427}
428
429static struct usb_request* alloc_ep_req(struct usb_ep *ep, unsigned length)
430{
431 struct usb_request *req;
432
433 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
434 if (req) {
435 req->length = length;
436 req->buf = kmalloc(length, GFP_ATOMIC);
437 if (!req->buf) {
438 usb_ep_free_request(ep, req);
439 req = NULL;
440 }
441 }
442 return req;
443}
444
445static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
446{
447 kfree(req->buf);
448 usb_ep_free_request(ep, req);
449}
450
451static const uint8_t gmidi_cin_length[] = {
452 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
453};
454
455/*
456 * Receives a chunk of MIDI data.
457 */
458static void gmidi_read_data(struct usb_ep *ep, int cable,
459 uint8_t* data, int length)
460{
461 struct gmidi_device *dev = ep->driver_data;
462 /* cable is ignored, because for now we only have one. */
463
464 if (!dev->out_substream) {
465 /* Nobody is listening - throw it on the floor. */
466 return;
467 }
468 if (!test_bit(dev->out_substream->number, &dev->out_triggered)) {
469 return;
470 }
471 snd_rawmidi_receive(dev->out_substream, data, length);
472}
473
474static void gmidi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
475{
476 unsigned i;
477 u8 *buf = req->buf;
478
479 for (i = 0; i + 3 < req->actual; i += 4) {
480 if (buf[i] != 0) {
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);
484 }
485 }
486}
487
488static void gmidi_complete(struct usb_ep *ep, struct usb_request *req)
489{
490 struct gmidi_device *dev = ep->driver_data;
491 int status = req->status;
492
493 switch (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);
504 return;
505 }
506 break;
507
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);
516 }
517 free_ep_req(ep, req);
518 return;
519
520 case -EOVERFLOW: /* buffer overrun on read means that
521 * we didn't provide a big enough
522 * buffer.
523 */
524 default:
525 DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
526 status, req->actual, req->length);
527 break;
528 case -EREMOTEIO: /* short read */
529 break;
530 }
531
532 status = usb_ep_queue(ep, req, GFP_ATOMIC);
533 if (status) {
534 ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
535 ep->name, req->length, status);
536 usb_ep_set_halt(ep);
537 /* FIXME recover later ... somehow */
538 }
539}
540
541static int set_gmidi_config(struct gmidi_device *dev, gfp_t gfp_flags)
542{
543 int err = 0;
544 struct usb_request *req;
545 struct usb_ep* ep;
546 unsigned i;
547
548 err = usb_ep_enable(dev->in_ep, &bulk_in_desc);
549 if (err) {
550 ERROR(dev, "can't start %s: %d\n", dev->in_ep->name, err);
551 goto fail;
552 }
553 dev->in_ep->driver_data = dev;
554
555 err = usb_ep_enable(dev->out_ep, &bulk_out_desc);
556 if (err) {
557 ERROR(dev, "can't start %s: %d\n", dev->out_ep->name, err);
558 goto fail;
559 }
560 dev->out_ep->driver_data = dev;
561
562 /* allocate a bunch of read buffers and queue them all at once. */
563 ep = dev->out_ep;
564 for (i = 0; i < qlen && err == 0; i++) {
565 req = alloc_ep_req(ep, buflen);
566 if (req) {
567 req->complete = gmidi_complete;
568 err = usb_ep_queue(ep, req, GFP_ATOMIC);
569 if (err) {
570 DBG(dev, "%s queue req: %d\n", ep->name, err);
571 }
572 } else {
573 err = -ENOMEM;
574 }
575 }
576fail:
577 /* caller is responsible for cleanup on error */
578 return err;
579}
580
581
582static void gmidi_reset_config(struct gmidi_device *dev)
583{
584 if (dev->config == 0) {
585 return;
586 }
587
588 DBG(dev, "reset config\n");
589
590 /* just disable endpoints, forcing completion of pending i/o.
591 * all our completion handlers free their requests in this case.
592 */
593 usb_ep_disable(dev->in_ep);
594 usb_ep_disable(dev->out_ep);
595 dev->config = 0;
596}
597
598/* change our operational config. this code must agree with the code
599 * that returns config descriptors, and altsetting code.
600 *
601 * it's also responsible for power management interactions. some
602 * configurations might not work with our current power sources.
603 *
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).
607 */
608static int
609gmidi_set_config(struct gmidi_device *dev, unsigned number, gfp_t gfp_flags)
610{
611 int result = 0;
612 struct usb_gadget *gadget = dev->gadget;
613
614#if 0
615 /* FIXME */
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) {
626 return 0;
627 }
628#endif
629
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");
633 return -ESPIPE;
634 }
635 gmidi_reset_config(dev);
636
637 switch (number) {
638 case GMIDI_CONFIG:
639 result = set_gmidi_config(dev, gfp_flags);
640 break;
641 default:
642 result = -EINVAL;
643 /* FALL THROUGH */
644 case 0:
645 return result;
646 }
647
648 if (!result && (!dev->in_ep || !dev->out_ep)) {
649 result = -ENODEV;
650 }
651 if (result) {
652 gmidi_reset_config(dev);
653 } else {
654 char *speed;
655
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;
661 }
662
663 dev->config = number;
664 INFO(dev, "%s speed\n", speed);
665 }
666 return result;
667}
668
669
670static void gmidi_setup_complete(struct usb_ep *ep, struct usb_request *req)
671{
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);
676 }
677}
678
679/*
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.
685 */
686static int gmidi_setup(struct usb_gadget *gadget,
687 const struct usb_ctrlrequest *ctrl)
688{
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);
695
696 /* usually this stores reply data in the pre-allocated ep0 buffer,
697 * but config change events will reconfigure hardware.
698 */
699 req->zero = 0;
700 switch (ctrl->bRequest) {
701
702 case USB_REQ_GET_DESCRIPTOR:
703 if (ctrl->bRequestType != USB_DIR_IN) {
704 goto unknown;
705 }
706 switch (w_value >> 8) {
707
708 case USB_DT_DEVICE:
709 value = min(w_length, (u16) sizeof(device_desc));
710 memcpy(req->buf, &device_desc, value);
711 break;
712 case USB_DT_CONFIG:
713 value = config_buf(gadget, req->buf,
714 w_value >> 8,
715 w_value & 0xff);
716 if (value >= 0) {
717 value = min(w_length, (u16)value);
718 }
719 break;
720
721 case USB_DT_STRING:
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
726 */
727 value = usb_gadget_get_string(&stringtab,
728 w_value & 0xff, req->buf);
729 if (value >= 0) {
730 value = min(w_length, (u16)value);
731 }
732 break;
733 }
734 break;
735
736 /* currently two configs, two speeds */
737 case USB_REQ_SET_CONFIGURATION:
738 if (ctrl->bRequestType != 0) {
739 goto unknown;
740 }
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");
745 } else {
746 VDBG(dev, "HNP inactive\n");
747 }
748 spin_lock(&dev->lock);
749 value = gmidi_set_config(dev, w_value, GFP_ATOMIC);
750 spin_unlock(&dev->lock);
751 break;
752 case USB_REQ_GET_CONFIGURATION:
753 if (ctrl->bRequestType != USB_DIR_IN) {
754 goto unknown;
755 }
756 *(u8 *)req->buf = dev->config;
757 value = min(w_length, (u16)1);
758 break;
759
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.
763 */
764 case USB_REQ_SET_INTERFACE:
765 if (ctrl->bRequestType != USB_RECIP_INTERFACE) {
766 goto unknown;
767 }
768 spin_lock(&dev->lock);
769 if (dev->config && w_index < GMIDI_NUM_INTERFACES
770 && w_value == 0)
771 {
772 u8 config = dev->config;
773
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.
780 */
781 gmidi_reset_config(dev);
782 gmidi_set_config(dev, config, GFP_ATOMIC);
783 value = 0;
784 }
785 spin_unlock(&dev->lock);
786 break;
787 case USB_REQ_GET_INTERFACE:
788 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) {
789 goto unknown;
790 }
791 if (!dev->config) {
792 break;
793 }
794 if (w_index >= GMIDI_NUM_INTERFACES) {
795 value = -EDOM;
796 break;
797 }
798 *(u8 *)req->buf = 0;
799 value = min(w_length, (u16)1);
800 break;
801
802 default:
803unknown:
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);
807 }
808
809 /* respond with data transfer before status phase? */
810 if (value >= 0) {
811 req->length = value;
812 req->zero = value < w_length;
813 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
814 if (value < 0) {
815 DBG(dev, "ep_queue --> %d\n", value);
816 req->status = 0;
817 gmidi_setup_complete(gadget->ep0, req);
818 }
819 }
820
821 /* device either stalls (value < 0) or reports success */
822 return value;
823}
824
825static void gmidi_disconnect(struct usb_gadget *gadget)
826{
827 struct gmidi_device *dev = get_gadget_data(gadget);
828 unsigned long flags;
829
830 spin_lock_irqsave(&dev->lock, flags);
831 gmidi_reset_config(dev);
832
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.
836 */
837 spin_unlock_irqrestore(&dev->lock, flags);
838
839 /* next we may get setup() calls to enumerate new connections;
840 * or an unbind() during shutdown (including removing module).
841 */
842}
843
844static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget *gadget)
845{
846 struct gmidi_device *dev = get_gadget_data(gadget);
847 struct snd_card* card;
848
849 DBG(dev, "unbind\n");
850
851 card = dev->card;
852 dev->card = NULL;
853 if (card) {
854 snd_card_free(card);
855 }
856
857 /* we've already been disconnected ... no i/o is active */
858 if (dev->req) {
859 dev->req->length = USB_BUFSIZ;
860 free_ep_req(gadget->ep0, dev->req);
861 }
862 kfree(dev);
863 set_gadget_data(gadget, NULL);
864}
865
866static int gmidi_snd_free(struct snd_device *device)
867{
868 return 0;
869}
870
871static void gmidi_transmit_packet(struct usb_request* req, uint8_t p0,
872 uint8_t p1, uint8_t p2, uint8_t p3)
873{
874 unsigned length = req->length;
875
876 uint8_t* buf = (uint8_t*)req->buf + length;
877 buf[0] = p0;
878 buf[1] = p1;
879 buf[2] = p2;
880 buf[3] = p3;
881 req->length = length + 4;
882}
883
884/*
885 * Converts MIDI commands to USB MIDI packets.
886 */
887static void gmidi_transmit_byte(struct usb_request* req,
888 struct gmidi_in_port* port, uint8_t b)
889{
890 uint8_t p0 = port->cable;
891
892 if (b >= 0xf8) {
893 gmidi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
894 } else if (b >= 0xf0) {
895 switch (b) {
896 case 0xf0:
897 port->data[0] = b;
898 port->state = STATE_SYSEX_1;
899 break;
900 case 0xf1:
901 case 0xf3:
902 port->data[0] = b;
903 port->state = STATE_1PARAM;
904 break;
905 case 0xf2:
906 port->data[0] = b;
907 port->state = STATE_2PARAM_1;
908 break;
909 case 0xf4:
910 case 0xf5:
911 port->state = STATE_UNKNOWN;
912 break;
913 case 0xf6:
914 gmidi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
915 port->state = STATE_UNKNOWN;
916 break;
917 case 0xf7:
918 switch (port->state) {
919 case STATE_SYSEX_0:
920 gmidi_transmit_packet(req,
921 p0 | 0x05, 0xf7, 0, 0);
922 break;
923 case STATE_SYSEX_1:
924 gmidi_transmit_packet(req,
925 p0 | 0x06, port->data[0], 0xf7, 0);
926 break;
927 case STATE_SYSEX_2:
928 gmidi_transmit_packet(req,
929 p0 | 0x07, port->data[0],
930 port->data[1], 0xf7);
931 break;
932 }
933 port->state = STATE_UNKNOWN;
934 break;
935 }
936 } else if (b >= 0x80) {
937 port->data[0] = b;
938 if (b >= 0xc0 && b <= 0xdf)
939 port->state = STATE_1PARAM;
940 else
941 port->state = STATE_2PARAM_1;
942 } else { /* b < 0x80 */
943 switch (port->state) {
944 case STATE_1PARAM:
945 if (port->data[0] < 0xf0) {
946 p0 |= port->data[0] >> 4;
947 } else {
948 p0 |= 0x02;
949 port->state = STATE_UNKNOWN;
950 }
951 gmidi_transmit_packet(req, p0, port->data[0], b, 0);
952 break;
953 case STATE_2PARAM_1:
954 port->data[1] = b;
955 port->state = STATE_2PARAM_2;
956 break;
957 case STATE_2PARAM_2:
958 if (port->data[0] < 0xf0) {
959 p0 |= port->data[0] >> 4;
960 port->state = STATE_2PARAM_1;
961 } else {
962 p0 |= 0x03;
963 port->state = STATE_UNKNOWN;
964 }
965 gmidi_transmit_packet(req,
966 p0, port->data[0], port->data[1], b);
967 break;
968 case STATE_SYSEX_0:
969 port->data[0] = b;
970 port->state = STATE_SYSEX_1;
971 break;
972 case STATE_SYSEX_1:
973 port->data[1] = b;
974 port->state = STATE_SYSEX_2;
975 break;
976 case STATE_SYSEX_2:
977 gmidi_transmit_packet(req,
978 p0 | 0x04, port->data[0], port->data[1], b);
979 port->state = STATE_SYSEX_0;
980 break;
981 }
982 }
983}
984
985static void gmidi_transmit(struct gmidi_device* dev, struct usb_request* req)
986{
987 struct usb_ep* ep = dev->in_ep;
988 struct gmidi_in_port* port = &dev->in_port;
989
990 if (!ep) {
991 return;
992 }
993 if (!req) {
994 req = alloc_ep_req(ep, buflen);
995 }
996 if (!req) {
997 ERROR(dev, "gmidi_transmit: alloc_ep_request failed\n");
998 return;
999 }
1000 req->length = 0;
1001 req->complete = gmidi_complete;
1002
1003 if (port->active) {
1004 while (req->length + 3 < buflen) {
1005 uint8_t b;
1006 if (snd_rawmidi_transmit(dev->in_substream, &b, 1)
1007 != 1)
1008 {
1009 port->active = 0;
1010 break;
1011 }
1012 gmidi_transmit_byte(req, port, b);
1013 }
1014 }
1015 if (req->length > 0) {
1016 usb_ep_queue(ep, req, GFP_ATOMIC);
1017 } else {
1018 free_ep_req(ep, req);
1019 }
1020}
1021
1022static void gmidi_in_tasklet(unsigned long data)
1023{
1024 struct gmidi_device* dev = (struct gmidi_device*)data;
1025
1026 gmidi_transmit(dev, NULL);
1027}
1028
1029static int gmidi_in_open(struct snd_rawmidi_substream *substream)
1030{
1031 struct gmidi_device* dev = substream->rmidi->private_data;
1032
1033 VDBG(dev, "gmidi_in_open\n");
1034 dev->in_substream = substream;
1035 dev->in_port.state = STATE_UNKNOWN;
1036 return 0;
1037}
1038
1039static int gmidi_in_close(struct snd_rawmidi_substream *substream)
1040{
1041 VDBG(dev, "gmidi_in_close\n");
1042 return 0;
1043}
1044
1045static void gmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
1046{
1047 struct gmidi_device* dev = substream->rmidi->private_data;
1048
1049 VDBG(dev, "gmidi_in_trigger %d\n", up);
1050 dev->in_port.active = up;
1051 if (up) {
1052 tasklet_hi_schedule(&dev->tasklet);
1053 }
1054}
1055
1056static int gmidi_out_open(struct snd_rawmidi_substream *substream)
1057{
1058 struct gmidi_device* dev = substream->rmidi->private_data;
1059
1060 VDBG(dev, "gmidi_out_open\n");
1061 dev->out_substream = substream;
1062 return 0;
1063}
1064
1065static int gmidi_out_close(struct snd_rawmidi_substream *substream)
1066{
1067 VDBG(dev, "gmidi_out_close\n");
1068 return 0;
1069}
1070
1071static void gmidi_out_trigger(struct snd_rawmidi_substream *substream, int up)
1072{
1073 struct gmidi_device* dev = substream->rmidi->private_data;
1074
1075 VDBG(dev, "gmidi_out_trigger %d\n", up);
1076 if (up) {
1077 set_bit(substream->number, &dev->out_triggered);
1078 } else {
1079 clear_bit(substream->number, &dev->out_triggered);
1080 }
1081}
1082
1083static struct snd_rawmidi_ops gmidi_in_ops = {
1084 .open = gmidi_in_open,
1085 .close = gmidi_in_close,
1086 .trigger = gmidi_in_trigger,
1087};
1088
1089static struct snd_rawmidi_ops gmidi_out_ops = {
1090 .open = gmidi_out_open,
1091 .close = gmidi_out_close,
1092 .trigger = gmidi_out_trigger
1093};
1094
1095/* register as a sound "card" */
1096static int gmidi_register_card(struct gmidi_device *dev)
1097{
1098 struct snd_card *card;
1099 struct snd_rawmidi *rmidi;
1100 int err;
1101 int out_ports = 1;
1102 int in_ports = 1;
1103 static struct snd_device_ops ops = {
1104 .dev_free = gmidi_snd_free,
1105 };
1106
1107 card = snd_card_new(index, id, THIS_MODULE, 0);
1108 if (!card) {
1109 ERROR(dev, "snd_card_new failed\n");
1110 err = -ENOMEM;
1111 goto fail;
1112 }
1113 dev->card = card;
1114
1115 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, dev, &ops);
1116 if (err < 0) {
1117 ERROR(dev, "snd_device_new failed: error %d\n", err);
1118 goto fail;
1119 }
1120
1121 strcpy(card->driver, longname);
1122 strcpy(card->longname, longname);
1123 strcpy(card->shortname, shortname);
1124
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);
1131 if (err < 0) {
1132 ERROR(dev, "snd_rawmidi_new failed: error %d\n", err);
1133 goto fail;
1134 }
1135 dev->rmidi = rmidi;
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;
1141
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);
1146
1147 snd_card_set_dev(card, &dev->gadget->dev);
1148
1149 /* register it - we're ready to go */
1150 err = snd_card_register(card);
1151 if (err < 0) {
1152 ERROR(dev, "snd_card_register failed\n");
1153 goto fail;
1154 }
1155
1156 VDBG(dev, "gmidi_register_card finished ok\n");
1157 return 0;
1158
1159fail:
1160 if (dev->card) {
1161 snd_card_free(dev->card);
1162 dev->card = NULL;
1163 }
1164 return err;
1165}
1166
1167/*
1168 * Creates an output endpoint, and initializes output ports.
1169 */
1170static int __devinit gmidi_bind(struct usb_gadget *gadget)
1171{
1172 struct gmidi_device *dev;
1173 struct usb_ep *in_ep, *out_ep;
1174 int gcnum, err = 0;
1175
1176 /* support optional vendor/distro customization */
1177 if (idVendor) {
1178 if (!idProduct) {
1179 printk(KERN_ERR "idVendor needs idProduct!\n");
1180 return -ENODEV;
1181 }
1182 device_desc.idVendor = cpu_to_le16(idVendor);
1183 device_desc.idProduct = cpu_to_le16(idProduct);
1184 if (bcdDevice) {
1185 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1186 }
1187 }
1188 if (iManufacturer) {
1189 strlcpy(manufacturer, iManufacturer, sizeof(manufacturer));
1190 } else {
1191 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1192 system_utsname.sysname, system_utsname.release,
1193 gadget->name);
1194 }
1195 if (iProduct) {
1196 strlcpy(product_desc, iProduct, sizeof(product_desc));
1197 }
1198 if (iSerialNumber) {
1199 device_desc.iSerialNumber = STRING_SERIAL,
1200 strlcpy(serial_number, iSerialNumber, sizeof(serial_number));
1201 }
1202
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.
1206 */
1207 usb_ep_autoconfig_reset(gadget);
1208 in_ep = usb_ep_autoconfig(gadget, &bulk_in_desc);
1209 if (!in_ep) {
1210autoconf_fail:
1211 printk(KERN_ERR "%s: can't autoconfigure on %s\n",
1212 shortname, gadget->name);
1213 return -ENODEV;
1214 }
1215 EP_IN_NAME = in_ep->name;
1216 in_ep->driver_data = in_ep; /* claim */
1217
1218 out_ep = usb_ep_autoconfig(gadget, &bulk_out_desc);
1219 if (!out_ep) {
1220 goto autoconf_fail;
1221 }
1222 EP_OUT_NAME = out_ep->name;
1223 out_ep->driver_data = out_ep; /* claim */
1224
1225 gcnum = usb_gadget_controller_number(gadget);
1226 if (gcnum >= 0) {
1227 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1228 } else {
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.
1232 */
1233 printk(KERN_WARNING "%s: controller '%s' not recognized\n",
1234 shortname, gadget->name);
1235 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1236 }
1237
1238
1239 /* ok, we made sense of the hardware ... */
1240 dev = kzalloc(sizeof(*dev), SLAB_KERNEL);
1241 if (!dev) {
1242 return -ENOMEM;
1243 }
1244 spin_lock_init(&dev->lock);
1245 dev->gadget = gadget;
1246 dev->in_ep = in_ep;
1247 dev->out_ep = out_ep;
1248 set_gadget_data(gadget, dev);
1249 tasklet_init(&dev->tasklet, gmidi_in_tasklet, (unsigned long)dev);
1250
1251 /* preallocate control response and buffer */
1252 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1253 if (!dev->req) {
1254 err = -ENOMEM;
1255 goto fail;
1256 }
1257 dev->req->buf = usb_ep_alloc_buffer(gadget->ep0, USB_BUFSIZ,
1258 &dev->req->dma, GFP_KERNEL);
1259 if (!dev->req->buf) {
1260 err = -ENOMEM;
1261 goto fail;
1262 }
1263
1264 dev->req->complete = gmidi_setup_complete;
1265
1266 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1267
1268 gadget->ep0->driver_data = dev;
1269
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);
1273
1274 /* register as an ALSA sound card */
1275 err = gmidi_register_card(dev);
1276 if (err < 0) {
1277 goto fail;
1278 }
1279
1280 VDBG(dev, "gmidi_bind finished ok\n");
1281 return 0;
1282
1283fail:
1284 gmidi_unbind(gadget);
1285 return err;
1286}
1287
1288
1289static void gmidi_suspend(struct usb_gadget *gadget)
1290{
1291 struct gmidi_device *dev = get_gadget_data(gadget);
1292
1293 if (gadget->speed == USB_SPEED_UNKNOWN) {
1294 return;
1295 }
1296
1297 DBG(dev, "suspend\n");
1298}
1299
1300static void gmidi_resume(struct usb_gadget *gadget)
1301{
1302 struct gmidi_device *dev = get_gadget_data(gadget);
1303
1304 DBG(dev, "resume\n");
1305}
1306
1307
1308static struct usb_gadget_driver gmidi_driver = {
1309 .speed = USB_SPEED_FULL,
1310 .function = (char *)longname,
1311 .bind = gmidi_bind,
1312 .unbind = __exit_p(gmidi_unbind),
1313
1314 .setup = gmidi_setup,
1315 .disconnect = gmidi_disconnect,
1316
1317 .suspend = gmidi_suspend,
1318 .resume = gmidi_resume,
1319
1320 .driver = {
1321 .name = (char *)shortname,
1322 .owner = THIS_MODULE,
1323 },
1324};
1325
1326static int __init gmidi_init(void)
1327{
1328 return usb_gadget_register_driver(&gmidi_driver);
1329}
1330module_init(gmidi_init);
1331
1332static void __exit gmidi_cleanup(void)
1333{
1334 usb_gadget_unregister_driver(&gmidi_driver);
1335}
1336module_exit(gmidi_cleanup);
1337
diff --git a/include/linux/usb/audio.h b/include/linux/usb/audio.h
new file mode 100644
index 000000000000..6bd235994dc2
--- /dev/null
+++ b/include/linux/usb/audio.h
@@ -0,0 +1,53 @@
1/*
2 * <linux/usb/audio.h> -- USB Audio definitions.
3 *
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
7 *
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.
10 *
11 * This file holds USB constants and structures defined
12 * by the USB Device Class Definition for Audio Devices.
13 * Comments below reference relevant sections of that document:
14 *
15 * http://www.usb.org/developers/devclass_docs/audio10.pdf
16 */
17
18#ifndef __LINUX_USB_AUDIO_H
19#define __LINUX_USB_AUDIO_H
20
21#include <linux/types.h>
22
23/* A.2 Audio Interface Subclass Codes */
24#define USB_SUBCLASS_AUDIOCONTROL 0x01
25#define USB_SUBCLASS_AUDIOSTREAMING 0x02
26#define USB_SUBCLASS_MIDISTREAMING 0x03
27
28/* 4.3.2 Class-Specific AC Interface Descriptor */
29struct usb_ac_header_descriptor {
30 __u8 bLength; // 8+n
31 __u8 bDescriptorType; // USB_DT_CS_INTERFACE
32 __u8 bDescriptorSubtype; // USB_MS_HEADER
33 __le16 bcdADC; // 0x0100
34 __le16 wTotalLength; // includes Unit and Terminal desc.
35 __u8 bInCollection; // n
36 __u8 baInterfaceNr[]; // [n]
37} __attribute__ ((packed));
38
39#define USB_DT_AC_HEADER_SIZE(n) (8+(n))
40
41/* As above, but more useful for defining your own descriptors: */
42#define DECLARE_USB_AC_HEADER_DESCRIPTOR(n) \
43struct usb_ac_header_descriptor_##n { \
44 __u8 bLength; \
45 __u8 bDescriptorType; \
46 __u8 bDescriptorSubtype; \
47 __le16 bcdADC; \
48 __le16 wTotalLength; \
49 __u8 bInCollection; \
50 __u8 baInterfaceNr[n]; \
51} __attribute__ ((packed))
52
53#endif
diff --git a/include/linux/usb/midi.h b/include/linux/usb/midi.h
new file mode 100644
index 000000000000..11a97d5ffd34
--- /dev/null
+++ b/include/linux/usb/midi.h
@@ -0,0 +1,112 @@
1/*
2 * <linux/usb/midi.h> -- USB MIDI definitions.
3 *
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
7 *
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.
10 *
11 * This file holds USB constants and structures defined
12 * by the USB Device Class Definition for MIDI Devices.
13 * Comments below reference relevant sections of that document:
14 *
15 * http://www.usb.org/developers/devclass_docs/midi10.pdf
16 */
17
18#ifndef __LINUX_USB_MIDI_H
19#define __LINUX_USB_MIDI_H
20
21#include <linux/types.h>
22
23/* A.1 MS Class-Specific Interface Descriptor Subtypes */
24#define USB_MS_HEADER 0x01
25#define USB_MS_MIDI_IN_JACK 0x02
26#define USB_MS_MIDI_OUT_JACK 0x03
27#define USB_MS_ELEMENT 0x04
28
29/* A.2 MS Class-Specific Endpoint Descriptor Subtypes */
30#define USB_MS_GENERAL 0x01
31
32/* A.3 MS MIDI IN and OUT Jack Types */
33#define USB_MS_EMBEDDED 0x01
34#define USB_MS_EXTERNAL 0x02
35
36/* 6.1.2.1 Class-Specific MS Interface Header Descriptor */
37struct usb_ms_header_descriptor {
38 __u8 bLength;
39 __u8 bDescriptorType;
40 __u8 bDescriptorSubtype;
41 __le16 bcdMSC;
42 __le16 wTotalLength;
43} __attribute__ ((packed));
44
45#define USB_DT_MS_HEADER_SIZE 7
46
47/* 6.1.2.2 MIDI IN Jack Descriptor */
48struct usb_midi_in_jack_descriptor {
49 __u8 bLength;
50 __u8 bDescriptorType; // USB_DT_CS_INTERFACE
51 __u8 bDescriptorSubtype; // USB_MS_MIDI_IN_JACK
52 __u8 bJackType; // USB_MS_EMBEDDED/EXTERNAL
53 __u8 bJackID;
54 __u8 iJack;
55} __attribute__ ((packed));
56
57#define USB_DT_MIDI_IN_SIZE 6
58
59struct usb_midi_source_pin {
60 __u8 baSourceID;
61 __u8 baSourcePin;
62} __attribute__ ((packed));
63
64/* 6.1.2.3 MIDI OUT Jack Descriptor */
65struct usb_midi_out_jack_descriptor {
66 __u8 bLength;
67 __u8 bDescriptorType; // USB_DT_CS_INTERFACE
68 __u8 bDescriptorSubtype; // USB_MS_MIDI_OUT_JACK
69 __u8 bJackType; // USB_MS_EMBEDDED/EXTERNAL
70 __u8 bJackID;
71 __u8 bNrInputPins; // p
72 struct usb_midi_source_pin pins[]; // [p]
73 /*__u8 iJack; -- ommitted due to variable-sized pins[] */
74} __attribute__ ((packed));
75
76#define USB_DT_MIDI_OUT_SIZE(p) (7 + 2 * (p))
77
78/* As above, but more useful for defining your own descriptors: */
79#define DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(p) \
80struct usb_midi_out_jack_descriptor_##p { \
81 __u8 bLength; \
82 __u8 bDescriptorType; \
83 __u8 bDescriptorSubtype; \
84 __u8 bJackType; \
85 __u8 bJackID; \
86 __u8 bNrInputPins; \
87 struct usb_midi_source_pin pins[p]; \
88 __u8 iJack; \
89} __attribute__ ((packed))
90
91/* 6.2.2 Class-Specific MS Bulk Data Endpoint Descriptor */
92struct usb_ms_endpoint_descriptor {
93 __u8 bLength; // 4+n
94 __u8 bDescriptorType; // USB_DT_CS_ENDPOINT
95 __u8 bDescriptorSubtype; // USB_MS_GENERAL
96 __u8 bNumEmbMIDIJack; // n
97 __u8 baAssocJackID[]; // [n]
98} __attribute__ ((packed));
99
100#define USB_DT_MS_ENDPOINT_SIZE(n) (4 + (n))
101
102/* As above, but more useful for defining your own descriptors: */
103#define DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(n) \
104struct usb_ms_endpoint_descriptor_##n { \
105 __u8 bLength; \
106 __u8 bDescriptorType; \
107 __u8 bDescriptorSubtype; \
108 __u8 bNumEmbMIDIJack; \
109 __u8 baAssocJackID[n]; \
110} __attribute__ ((packed))
111
112#endif