blob: ff3a8513e64dbbcdc8d15f5d944693317acdb459 [file] [log] [blame]
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001/*
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
David Brownell8c070212007-08-02 00:01:27 -070021/* #define VERBOSE_DEBUG */
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +100022
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +100023#include <linux/kernel.h>
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +100024#include <linux/utsname.h>
25#include <linux/device.h>
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +100026
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +100027#include <sound/core.h>
28#include <sound/initval.h>
29#include <sound/rawmidi.h>
30
David Brownell5f848132006-12-16 15:34:53 -080031#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070032#include <linux/usb/gadget.h>
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +100033#include <linux/usb/audio.h>
34#include <linux/usb/midi.h>
35
36#include "gadget_chips.h"
37
38MODULE_AUTHOR("Ben Williamson");
39MODULE_LICENSE("GPL v2");
40
41#define DRIVER_VERSION "25 Jul 2006"
42
43static const char shortname[] = "g_midi";
44static const char longname[] = "MIDI Gadget";
45
46static int index = SNDRV_DEFAULT_IDX1;
47static char *id = SNDRV_DEFAULT_STR1;
48
49module_param(index, int, 0444);
50MODULE_PARM_DESC(index, "Index value for the USB MIDI Gadget adapter.");
51module_param(id, charp, 0444);
52MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter.");
53
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).
57 */
58
59static ushort idVendor;
60module_param(idVendor, ushort, S_IRUGO);
61MODULE_PARM_DESC(idVendor, "USB Vendor ID");
62
63static ushort idProduct;
64module_param(idProduct, ushort, S_IRUGO);
65MODULE_PARM_DESC(idProduct, "USB Product ID");
66
67static ushort bcdDevice;
68module_param(bcdDevice, ushort, S_IRUGO);
69MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
70
71static char *iManufacturer;
72module_param(iManufacturer, charp, S_IRUGO);
73MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
74
75static char *iProduct;
76module_param(iProduct, charp, S_IRUGO);
77MODULE_PARM_DESC(iProduct, "USB Product string");
78
79static char *iSerialNumber;
80module_param(iSerialNumber, charp, S_IRUGO);
81MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
82
83/*
84 * this version autoconfigures as much as possible,
85 * which is reasonable for most "bulk-only" drivers.
86 */
87static const char *EP_IN_NAME;
88static const char *EP_OUT_NAME;
89
90
91/* big enough to hold our biggest descriptor */
92#define USB_BUFSIZ 256
93
94
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 */
98struct 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];
111};
112
113struct 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;
David Brownell6bea4762006-12-05 03:15:33 -0800119 struct snd_card *card;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000120 struct snd_rawmidi *rmidi;
121 struct snd_rawmidi_substream *in_substream;
122 struct snd_rawmidi_substream *out_substream;
123
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;
130};
131
132static void gmidi_transmit(struct gmidi_device* dev, struct usb_request* req);
133
134
David Brownell8c070212007-08-02 00:01:27 -0700135#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)
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000145
146
147static unsigned buflen = 256;
148static unsigned qlen = 32;
149
150module_param(buflen, uint, S_IRUGO);
151module_param(qlen, uint, S_IRUGO);
152
153
154/* Thanks to Grey Innovation for donating this product ID.
155 *
156 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
157 * Instead: allocate your own, using normal USB-IF procedures.
158 */
159#define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
160#define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
161
162
163/*
164 * DESCRIPTORS ... most are static, but strings and (full)
165 * configuration descriptors are built on demand.
166 */
167
168#define STRING_MANUFACTURER 25
169#define STRING_PRODUCT 42
170#define STRING_SERIAL 101
171#define STRING_MIDI_GADGET 250
172
173/* We only have the one configuration, it's number 1. */
174#define GMIDI_CONFIG 1
175
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
180
181DECLARE_USB_AC_HEADER_DESCRIPTOR(1);
182DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
183DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1);
184
185/* B.1 Device Descriptor */
186static 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,
196};
197
198/* B.2 Configuration Descriptor */
199static 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,
206 /*
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?
210 */
211 .bmAttributes = USB_CONFIG_ATT_ONE,
212 .bMaxPower = 1,
213};
214
215/* B.3.1 Standard AC Interface Descriptor */
216static 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,
224};
225
226/* B.3.2 Class-Specific AC Interface Descriptor */
227static 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 = USB_DT_AC_HEADER_SIZE(1),
233 .bInCollection = 1,
234 .baInterfaceNr = {
235 [0] = GMIDI_MS_INTERFACE,
236 }
237};
238
239/* B.4.1 Standard MS Interface Descriptor */
240static 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,
248};
249
250/* B.4.2 Class-Specific MS Interface Descriptor */
251static 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 = USB_DT_MS_HEADER_SIZE
257 + 2*USB_DT_MIDI_IN_SIZE
258 + 2*USB_DT_MIDI_OUT_SIZE(1),
259};
260
261#define JACK_IN_EMB 1
262#define JACK_IN_EXT 2
263#define JACK_OUT_EMB 3
264#define JACK_OUT_EXT 4
265
266/* B.4.3 MIDI IN Jack Descriptors */
267static 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,
273};
274
275static 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,
281};
282
283/* B.4.4 MIDI OUT Jack Descriptors */
284static 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,
295 }
296 }
297};
298
299static 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,
310 }
311 }
312};
313
314/* B.5.1 Standard Bulk OUT Endpoint Descriptor */
315static 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,
320};
321
322/* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
323static 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,
330 }
331};
332
333/* B.6.1 Standard Bulk IN Endpoint Descriptor */
334static 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,
339};
340
341/* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
342static 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,
349 }
350};
351
352static 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,
356
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 */
363
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,
369};
370
371static char manufacturer[50];
372static char product_desc[40] = "MIDI Gadget";
373static char serial_number[20];
374
375/* static strings, in UTF-8 */
376static 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 */
382};
383
384static struct usb_gadget_strings stringtab = {
385 .language = 0x0409, /* en-us */
386 .strings = strings,
387};
388
389static int config_buf(struct usb_gadget *gadget,
390 u8 *buf, u8 type, unsigned index)
391{
392 int len;
393
394 /* only one configuration */
395 if (index != 0) {
396 return -EINVAL;
397 }
398 len = usb_gadget_config_buf(&config_desc,
399 buf, USB_BUFSIZ, gmidi_function);
400 if (len < 0) {
401 return len;
402 }
403 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
404 return len;
405}
406
David Brownell8c070212007-08-02 00:01:27 -0700407static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000408{
409 struct usb_request *req;
410
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;
418 }
419 }
420 return req;
421}
422
423static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
424{
425 kfree(req->buf);
426 usb_ep_free_request(ep, req);
427}
428
429static const uint8_t gmidi_cin_length[] = {
430 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
431};
432
433/*
434 * Receives a chunk of MIDI data.
435 */
436static void gmidi_read_data(struct usb_ep *ep, int cable,
David Brownell8c070212007-08-02 00:01:27 -0700437 uint8_t *data, int length)
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000438{
439 struct gmidi_device *dev = ep->driver_data;
440 /* cable is ignored, because for now we only have one. */
441
442 if (!dev->out_substream) {
443 /* Nobody is listening - throw it on the floor. */
444 return;
445 }
446 if (!test_bit(dev->out_substream->number, &dev->out_triggered)) {
447 return;
448 }
449 snd_rawmidi_receive(dev->out_substream, data, length);
450}
451
452static void gmidi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
453{
454 unsigned i;
455 u8 *buf = req->buf;
456
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);
462 }
463 }
464}
465
466static void gmidi_complete(struct usb_ep *ep, struct usb_request *req)
467{
468 struct gmidi_device *dev = ep->driver_data;
469 int status = req->status;
470
471 switch (status) {
David Brownell6bea4762006-12-05 03:15:33 -0800472 case 0: /* normal completion */
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000473 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;
483 }
484 break;
485
486 /* this endpoint is normally active while we're configured */
David Brownell6bea4762006-12-05 03:15:33 -0800487 case -ECONNABORTED: /* hardware forced ep reset */
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000488 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);
494 }
495 free_ep_req(ep, req);
496 return;
497
498 case -EOVERFLOW: /* buffer overrun on read means that
499 * we didn't provide a big enough
500 * buffer.
501 */
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;
508 }
509
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 */
516 }
517}
518
519static int set_gmidi_config(struct gmidi_device *dev, gfp_t gfp_flags)
520{
521 int err = 0;
522 struct usb_request *req;
David Brownell8c070212007-08-02 00:01:27 -0700523 struct usb_ep *ep;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000524 unsigned i;
525
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;
530 }
531 dev->in_ep->driver_data = dev;
532
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;
537 }
538 dev->out_ep->driver_data = dev;
539
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);
549 }
550 } else {
551 err = -ENOMEM;
552 }
553 }
554fail:
555 /* caller is responsible for cleanup on error */
556 return err;
557}
558
559
560static void gmidi_reset_config(struct gmidi_device *dev)
561{
562 if (dev->config == 0) {
563 return;
564 }
565
566 DBG(dev, "reset config\n");
567
568 /* just disable endpoints, forcing completion of pending i/o.
569 * all our completion handlers free their requests in this case.
570 */
571 usb_ep_disable(dev->in_ep);
572 usb_ep_disable(dev->out_ep);
573 dev->config = 0;
574}
575
576/* change our operational config. this code must agree with the code
577 * that returns config descriptors, and altsetting code.
578 *
579 * it's also responsible for power management interactions. some
580 * configurations might not work with our current power sources.
581 *
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).
585 */
586static int
587gmidi_set_config(struct gmidi_device *dev, unsigned number, gfp_t gfp_flags)
588{
589 int result = 0;
590 struct usb_gadget *gadget = dev->gadget;
591
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;
605 }
606#endif
607
608 if (gadget_is_sa1100(gadget) && dev->config) {
609 /* tx fifo is full, but we can't clear it...*/
David Brownell8c070212007-08-02 00:01:27 -0700610 ERROR(dev, "can't change configurations\n");
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000611 return -ESPIPE;
612 }
613 gmidi_reset_config(dev);
614
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;
624 }
625
626 if (!result && (!dev->in_ep || !dev->out_ep)) {
627 result = -ENODEV;
628 }
629 if (result) {
630 gmidi_reset_config(dev);
631 } else {
632 char *speed;
633
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;
David Brownell6bea4762006-12-05 03:15:33 -0800638 default: speed = "?"; break;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000639 }
640
641 dev->config = number;
642 INFO(dev, "%s speed\n", speed);
643 }
644 return result;
645}
646
647
648static void gmidi_setup_complete(struct usb_ep *ep, struct usb_request *req)
649{
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);
654 }
655}
656
657/*
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.
663 */
664static int gmidi_setup(struct usb_gadget *gadget,
665 const struct usb_ctrlrequest *ctrl)
666{
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);
673
674 /* usually this stores reply data in the pre-allocated ep0 buffer,
675 * but config change events will reconfigure hardware.
676 */
677 req->zero = 0;
678 switch (ctrl->bRequest) {
679
680 case USB_REQ_GET_DESCRIPTOR:
681 if (ctrl->bRequestType != USB_DIR_IN) {
682 goto unknown;
683 }
684 switch (w_value >> 8) {
685
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);
696 }
697 break;
698
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
704 */
705 value = usb_gadget_get_string(&stringtab,
706 w_value & 0xff, req->buf);
707 if (value >= 0) {
708 value = min(w_length, (u16)value);
709 }
710 break;
711 }
712 break;
713
714 /* currently two configs, two speeds */
715 case USB_REQ_SET_CONFIGURATION:
716 if (ctrl->bRequestType != 0) {
717 goto unknown;
718 }
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");
725 }
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;
733 }
734 *(u8 *)req->buf = dev->config;
735 value = min(w_length, (u16)1);
736 break;
737
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.
741 */
742 case USB_REQ_SET_INTERFACE:
743 if (ctrl->bRequestType != USB_RECIP_INTERFACE) {
744 goto unknown;
745 }
746 spin_lock(&dev->lock);
747 if (dev->config && w_index < GMIDI_NUM_INTERFACES
748 && w_value == 0)
749 {
750 u8 config = dev->config;
751
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.
758 */
759 gmidi_reset_config(dev);
760 gmidi_set_config(dev, config, GFP_ATOMIC);
761 value = 0;
762 }
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;
768 }
769 if (!dev->config) {
770 break;
771 }
772 if (w_index >= GMIDI_NUM_INTERFACES) {
773 value = -EDOM;
774 break;
775 }
776 *(u8 *)req->buf = 0;
777 value = min(w_length, (u16)1);
778 break;
779
780 default:
781unknown:
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);
785 }
786
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);
796 }
797 }
798
799 /* device either stalls (value < 0) or reports success */
800 return value;
801}
802
803static void gmidi_disconnect(struct usb_gadget *gadget)
804{
805 struct gmidi_device *dev = get_gadget_data(gadget);
806 unsigned long flags;
807
808 spin_lock_irqsave(&dev->lock, flags);
809 gmidi_reset_config(dev);
810
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.
814 */
815 spin_unlock_irqrestore(&dev->lock, flags);
816
817 /* next we may get setup() calls to enumerate new connections;
818 * or an unbind() during shutdown (including removing module).
819 */
820}
821
822static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget *gadget)
823{
824 struct gmidi_device *dev = get_gadget_data(gadget);
David Brownell8c070212007-08-02 00:01:27 -0700825 struct snd_card *card;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000826
827 DBG(dev, "unbind\n");
828
829 card = dev->card;
830 dev->card = NULL;
831 if (card) {
832 snd_card_free(card);
833 }
834
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);
839 }
840 kfree(dev);
841 set_gadget_data(gadget, NULL);
842}
843
844static int gmidi_snd_free(struct snd_device *device)
845{
846 return 0;
847}
848
David Brownell8c070212007-08-02 00:01:27 -0700849static void gmidi_transmit_packet(struct usb_request *req, uint8_t p0,
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000850 uint8_t p1, uint8_t p2, uint8_t p3)
851{
852 unsigned length = req->length;
David Brownell8c070212007-08-02 00:01:27 -0700853 u8 *buf = (u8 *)req->buf + length;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000854
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000855 buf[0] = p0;
856 buf[1] = p1;
857 buf[2] = p2;
858 buf[3] = p3;
859 req->length = length + 4;
860}
861
862/*
863 * Converts MIDI commands to USB MIDI packets.
864 */
David Brownell8c070212007-08-02 00:01:27 -0700865static void gmidi_transmit_byte(struct usb_request *req,
866 struct gmidi_in_port *port, uint8_t b)
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000867{
868 uint8_t p0 = port->cable;
869
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;
910 }
911 port->state = STATE_UNKNOWN;
912 break;
913 }
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;
928 }
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;
942 }
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;
959 }
960 }
961}
962
David Brownell8c070212007-08-02 00:01:27 -0700963static void gmidi_transmit(struct gmidi_device *dev, struct usb_request *req)
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000964{
David Brownell8c070212007-08-02 00:01:27 -0700965 struct usb_ep *ep = dev->in_ep;
966 struct gmidi_in_port *port = &dev->in_port;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +1000967
968 if (!ep) {
969 return;
970 }
971 if (!req) {
972 req = alloc_ep_req(ep, buflen);
973 }
974 if (!req) {
975 ERROR(dev, "gmidi_transmit: alloc_ep_request failed\n");
976 return;
977 }
978 req->length = 0;
979 req->complete = gmidi_complete;
980
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)
986 {
987 port->active = 0;
988 break;
989 }
990 gmidi_transmit_byte(req, port, b);
991 }
992 }
993 if (req->length > 0) {
994 usb_ep_queue(ep, req, GFP_ATOMIC);
995 } else {
996 free_ep_req(ep, req);
997 }
998}
999
1000static void gmidi_in_tasklet(unsigned long data)
1001{
David Brownell8c070212007-08-02 00:01:27 -07001002 struct gmidi_device *dev = (struct gmidi_device *)data;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001003
1004 gmidi_transmit(dev, NULL);
1005}
1006
1007static int gmidi_in_open(struct snd_rawmidi_substream *substream)
1008{
David Brownell8c070212007-08-02 00:01:27 -07001009 struct gmidi_device *dev = substream->rmidi->private_data;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001010
1011 VDBG(dev, "gmidi_in_open\n");
1012 dev->in_substream = substream;
1013 dev->in_port.state = STATE_UNKNOWN;
1014 return 0;
1015}
1016
1017static int gmidi_in_close(struct snd_rawmidi_substream *substream)
1018{
David Brownell8c070212007-08-02 00:01:27 -07001019 struct gmidi_device *dev = substream->rmidi->private_data;
1020
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001021 VDBG(dev, "gmidi_in_close\n");
1022 return 0;
1023}
1024
1025static void gmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
1026{
David Brownell8c070212007-08-02 00:01:27 -07001027 struct gmidi_device *dev = substream->rmidi->private_data;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001028
1029 VDBG(dev, "gmidi_in_trigger %d\n", up);
1030 dev->in_port.active = up;
1031 if (up) {
1032 tasklet_hi_schedule(&dev->tasklet);
1033 }
1034}
1035
1036static int gmidi_out_open(struct snd_rawmidi_substream *substream)
1037{
David Brownell8c070212007-08-02 00:01:27 -07001038 struct gmidi_device *dev = substream->rmidi->private_data;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001039
1040 VDBG(dev, "gmidi_out_open\n");
1041 dev->out_substream = substream;
1042 return 0;
1043}
1044
1045static int gmidi_out_close(struct snd_rawmidi_substream *substream)
1046{
David Brownell8c070212007-08-02 00:01:27 -07001047 struct gmidi_device *dev = substream->rmidi->private_data;
1048
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001049 VDBG(dev, "gmidi_out_close\n");
1050 return 0;
1051}
1052
1053static void gmidi_out_trigger(struct snd_rawmidi_substream *substream, int up)
1054{
David Brownell8c070212007-08-02 00:01:27 -07001055 struct gmidi_device *dev = substream->rmidi->private_data;
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001056
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);
1062 }
1063}
1064
1065static struct snd_rawmidi_ops gmidi_in_ops = {
1066 .open = gmidi_in_open,
1067 .close = gmidi_in_close,
1068 .trigger = gmidi_in_trigger,
1069};
1070
1071static struct snd_rawmidi_ops gmidi_out_ops = {
1072 .open = gmidi_out_open,
1073 .close = gmidi_out_close,
1074 .trigger = gmidi_out_trigger
1075};
1076
1077/* register as a sound "card" */
1078static int gmidi_register_card(struct gmidi_device *dev)
1079{
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,
1087 };
1088
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;
1094 }
1095 dev->card = card;
1096
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;
1101 }
1102
1103 strcpy(card->driver, longname);
1104 strcpy(card->longname, longname);
1105 strcpy(card->shortname, shortname);
1106
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;
1116 }
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;
1123
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);
1128
1129 snd_card_set_dev(card, &dev->gadget->dev);
1130
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;
1136 }
1137
1138 VDBG(dev, "gmidi_register_card finished ok\n");
1139 return 0;
1140
1141fail:
1142 if (dev->card) {
1143 snd_card_free(dev->card);
1144 dev->card = NULL;
1145 }
1146 return err;
1147}
1148
1149/*
1150 * Creates an output endpoint, and initializes output ports.
1151 */
David Brownell0e530b42008-04-05 14:17:14 -07001152static int __init gmidi_bind(struct usb_gadget *gadget)
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001153{
1154 struct gmidi_device *dev;
1155 struct usb_ep *in_ep, *out_ep;
1156 int gcnum, err = 0;
1157
1158 /* support optional vendor/distro customization */
1159 if (idVendor) {
1160 if (!idProduct) {
David Brownell00274922007-11-19 12:58:36 -08001161 pr_err("idVendor needs idProduct!\n");
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001162 return -ENODEV;
1163 }
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);
1168 }
1169 }
1170 if (iManufacturer) {
1171 strlcpy(manufacturer, iManufacturer, sizeof(manufacturer));
1172 } else {
1173 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001174 init_utsname()->sysname, init_utsname()->release,
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001175 gadget->name);
1176 }
1177 if (iProduct) {
1178 strlcpy(product_desc, iProduct, sizeof(product_desc));
1179 }
1180 if (iSerialNumber) {
1181 device_desc.iSerialNumber = STRING_SERIAL,
1182 strlcpy(serial_number, iSerialNumber, sizeof(serial_number));
1183 }
1184
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.
1188 */
1189 usb_ep_autoconfig_reset(gadget);
1190 in_ep = usb_ep_autoconfig(gadget, &bulk_in_desc);
1191 if (!in_ep) {
1192autoconf_fail:
David Brownell00274922007-11-19 12:58:36 -08001193 pr_err("%s: can't autoconfigure on %s\n",
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001194 shortname, gadget->name);
1195 return -ENODEV;
1196 }
1197 EP_IN_NAME = in_ep->name;
1198 in_ep->driver_data = in_ep; /* claim */
1199
1200 out_ep = usb_ep_autoconfig(gadget, &bulk_out_desc);
1201 if (!out_ep) {
1202 goto autoconf_fail;
1203 }
1204 EP_OUT_NAME = out_ep->name;
1205 out_ep->driver_data = out_ep; /* claim */
1206
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.
1214 */
David Brownell00274922007-11-19 12:58:36 -08001215 pr_warning("%s: controller '%s' not recognized\n",
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001216 shortname, gadget->name);
1217 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1218 }
1219
1220
1221 /* ok, we made sense of the hardware ... */
Christoph Lametere94b1762006-12-06 20:33:17 -08001222 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001223 if (!dev) {
1224 return -ENOMEM;
1225 }
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);
1232
1233 /* preallocate control response and buffer */
David Brownell9d8bab52007-07-01 11:04:54 -07001234 dev->req = alloc_ep_req(gadget->ep0, USB_BUFSIZ);
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001235 if (!dev->req) {
1236 err = -ENOMEM;
1237 goto fail;
1238 }
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001239
1240 dev->req->complete = gmidi_setup_complete;
1241
1242 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1243
1244 gadget->ep0->driver_data = dev;
1245
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);
1249
1250 /* register as an ALSA sound card */
1251 err = gmidi_register_card(dev);
1252 if (err < 0) {
1253 goto fail;
1254 }
1255
1256 VDBG(dev, "gmidi_bind finished ok\n");
1257 return 0;
1258
1259fail:
1260 gmidi_unbind(gadget);
1261 return err;
1262}
1263
1264
1265static void gmidi_suspend(struct usb_gadget *gadget)
1266{
1267 struct gmidi_device *dev = get_gadget_data(gadget);
1268
1269 if (gadget->speed == USB_SPEED_UNKNOWN) {
1270 return;
1271 }
1272
1273 DBG(dev, "suspend\n");
1274}
1275
1276static void gmidi_resume(struct usb_gadget *gadget)
1277{
1278 struct gmidi_device *dev = get_gadget_data(gadget);
1279
1280 DBG(dev, "resume\n");
1281}
1282
1283
1284static struct usb_gadget_driver gmidi_driver = {
1285 .speed = USB_SPEED_FULL,
1286 .function = (char *)longname,
1287 .bind = gmidi_bind,
David Brownell6bea4762006-12-05 03:15:33 -08001288 .unbind = gmidi_unbind,
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001289
1290 .setup = gmidi_setup,
1291 .disconnect = gmidi_disconnect,
1292
1293 .suspend = gmidi_suspend,
1294 .resume = gmidi_resume,
1295
David Brownell6bea4762006-12-05 03:15:33 -08001296 .driver = {
Ben Williamsonf2ebf92c2006-08-01 11:28:16 +10001297 .name = (char *)shortname,
1298 .owner = THIS_MODULE,
1299 },
1300};
1301
1302static int __init gmidi_init(void)
1303{
1304 return usb_gadget_register_driver(&gmidi_driver);
1305}
1306module_init(gmidi_init);
1307
1308static void __exit gmidi_cleanup(void)
1309{
1310 usb_gadget_unregister_driver(&gmidi_driver);
1311}
1312module_exit(gmidi_cleanup);
1313