]>
Commit | Line | Data |
---|---|---|
f2ebf92c BW |
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 | ||
f2ebf92c BW |
24 | #include <linux/module.h> |
25 | #include <linux/kernel.h> | |
26 | #include <linux/delay.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/utsname.h> | |
30 | #include <linux/device.h> | |
31 | #include <linux/moduleparam.h> | |
32 | ||
33 | #include <sound/driver.h> | |
34 | #include <sound/core.h> | |
35 | #include <sound/initval.h> | |
36 | #include <sound/rawmidi.h> | |
37 | ||
5f848137 | 38 | #include <linux/usb/ch9.h> |
f2ebf92c BW |
39 | #include <linux/usb_gadget.h> |
40 | #include <linux/usb/audio.h> | |
41 | #include <linux/usb/midi.h> | |
42 | ||
43 | #include "gadget_chips.h" | |
44 | ||
45 | MODULE_AUTHOR("Ben Williamson"); | |
46 | MODULE_LICENSE("GPL v2"); | |
47 | ||
48 | #define DRIVER_VERSION "25 Jul 2006" | |
49 | ||
50 | static const char shortname[] = "g_midi"; | |
51 | static const char longname[] = "MIDI Gadget"; | |
52 | ||
53 | static int index = SNDRV_DEFAULT_IDX1; | |
54 | static char *id = SNDRV_DEFAULT_STR1; | |
55 | ||
56 | module_param(index, int, 0444); | |
57 | MODULE_PARM_DESC(index, "Index value for the USB MIDI Gadget adapter."); | |
58 | module_param(id, charp, 0444); | |
59 | MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter."); | |
60 | ||
61 | /* Some systems will want different product identifers published in the | |
62 | * device descriptor, either numbers or strings or both. These string | |
63 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). | |
64 | */ | |
65 | ||
66 | static ushort idVendor; | |
67 | module_param(idVendor, ushort, S_IRUGO); | |
68 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); | |
69 | ||
70 | static ushort idProduct; | |
71 | module_param(idProduct, ushort, S_IRUGO); | |
72 | MODULE_PARM_DESC(idProduct, "USB Product ID"); | |
73 | ||
74 | static ushort bcdDevice; | |
75 | module_param(bcdDevice, ushort, S_IRUGO); | |
76 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); | |
77 | ||
78 | static char *iManufacturer; | |
79 | module_param(iManufacturer, charp, S_IRUGO); | |
80 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); | |
81 | ||
82 | static char *iProduct; | |
83 | module_param(iProduct, charp, S_IRUGO); | |
84 | MODULE_PARM_DESC(iProduct, "USB Product string"); | |
85 | ||
86 | static char *iSerialNumber; | |
87 | module_param(iSerialNumber, charp, S_IRUGO); | |
88 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber"); | |
89 | ||
90 | /* | |
91 | * this version autoconfigures as much as possible, | |
92 | * which is reasonable for most "bulk-only" drivers. | |
93 | */ | |
94 | static const char *EP_IN_NAME; | |
95 | static const char *EP_OUT_NAME; | |
96 | ||
97 | ||
98 | /* big enough to hold our biggest descriptor */ | |
99 | #define USB_BUFSIZ 256 | |
100 | ||
101 | ||
102 | /* This is a gadget, and the IN/OUT naming is from the host's perspective. | |
103 | USB -> OUT endpoint -> rawmidi | |
104 | USB <- IN endpoint <- rawmidi */ | |
105 | struct gmidi_in_port { | |
106 | struct gmidi_device* dev; | |
107 | int active; | |
108 | uint8_t cable; /* cable number << 4 */ | |
109 | uint8_t state; | |
110 | #define STATE_UNKNOWN 0 | |
111 | #define STATE_1PARAM 1 | |
112 | #define STATE_2PARAM_1 2 | |
113 | #define STATE_2PARAM_2 3 | |
114 | #define STATE_SYSEX_0 4 | |
115 | #define STATE_SYSEX_1 5 | |
116 | #define STATE_SYSEX_2 6 | |
117 | uint8_t data[2]; | |
118 | }; | |
119 | ||
120 | struct gmidi_device { | |
121 | spinlock_t lock; | |
122 | struct usb_gadget *gadget; | |
123 | struct usb_request *req; /* for control responses */ | |
124 | u8 config; | |
125 | struct usb_ep *in_ep, *out_ep; | |
6bea476c | 126 | struct snd_card *card; |
f2ebf92c BW |
127 | struct snd_rawmidi *rmidi; |
128 | struct snd_rawmidi_substream *in_substream; | |
129 | struct snd_rawmidi_substream *out_substream; | |
130 | ||
131 | /* For the moment we only support one port in | |
132 | each direction, but in_port is kept as a | |
133 | separate struct so we can have more later. */ | |
134 | struct gmidi_in_port in_port; | |
135 | unsigned long out_triggered; | |
136 | struct tasklet_struct tasklet; | |
137 | }; | |
138 | ||
139 | static void gmidi_transmit(struct gmidi_device* dev, struct usb_request* req); | |
140 | ||
141 | ||
142 | #define xprintk(d,level,fmt,args...) \ | |
143 | dev_printk(level , &(d)->gadget->dev , fmt , ## args) | |
144 | ||
145 | #ifdef DEBUG | |
146 | #define DBG(dev,fmt,args...) \ | |
147 | xprintk(dev , KERN_DEBUG , fmt , ## args) | |
148 | #else | |
149 | #define DBG(dev,fmt,args...) \ | |
150 | do { } while (0) | |
151 | #endif /* DEBUG */ | |
152 | ||
153 | #ifdef VERBOSE | |
154 | #define VDBG DBG | |
155 | #else | |
156 | #define VDBG(dev,fmt,args...) \ | |
157 | do { } while (0) | |
158 | #endif /* VERBOSE */ | |
159 | ||
160 | #define ERROR(dev,fmt,args...) \ | |
161 | xprintk(dev , KERN_ERR , fmt , ## args) | |
162 | #define WARN(dev,fmt,args...) \ | |
163 | xprintk(dev , KERN_WARNING , fmt , ## args) | |
164 | #define INFO(dev,fmt,args...) \ | |
165 | xprintk(dev , KERN_INFO , fmt , ## args) | |
166 | ||
167 | ||
168 | static unsigned buflen = 256; | |
169 | static unsigned qlen = 32; | |
170 | ||
171 | module_param(buflen, uint, S_IRUGO); | |
172 | module_param(qlen, uint, S_IRUGO); | |
173 | ||
174 | ||
175 | /* Thanks to Grey Innovation for donating this product ID. | |
176 | * | |
177 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
178 | * Instead: allocate your own, using normal USB-IF procedures. | |
179 | */ | |
180 | #define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */ | |
181 | #define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */ | |
182 | ||
183 | ||
184 | /* | |
185 | * DESCRIPTORS ... most are static, but strings and (full) | |
186 | * configuration descriptors are built on demand. | |
187 | */ | |
188 | ||
189 | #define STRING_MANUFACTURER 25 | |
190 | #define STRING_PRODUCT 42 | |
191 | #define STRING_SERIAL 101 | |
192 | #define STRING_MIDI_GADGET 250 | |
193 | ||
194 | /* We only have the one configuration, it's number 1. */ | |
195 | #define GMIDI_CONFIG 1 | |
196 | ||
197 | /* We have two interfaces- AudioControl and MIDIStreaming */ | |
198 | #define GMIDI_AC_INTERFACE 0 | |
199 | #define GMIDI_MS_INTERFACE 1 | |
200 | #define GMIDI_NUM_INTERFACES 2 | |
201 | ||
202 | DECLARE_USB_AC_HEADER_DESCRIPTOR(1); | |
203 | DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1); | |
204 | DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(1); | |
205 | ||
206 | /* B.1 Device Descriptor */ | |
207 | static struct usb_device_descriptor device_desc = { | |
208 | .bLength = USB_DT_DEVICE_SIZE, | |
209 | .bDescriptorType = USB_DT_DEVICE, | |
210 | .bcdUSB = __constant_cpu_to_le16(0x0200), | |
211 | .bDeviceClass = USB_CLASS_PER_INTERFACE, | |
212 | .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM), | |
213 | .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM), | |
214 | .iManufacturer = STRING_MANUFACTURER, | |
215 | .iProduct = STRING_PRODUCT, | |
216 | .bNumConfigurations = 1, | |
217 | }; | |
218 | ||
219 | /* B.2 Configuration Descriptor */ | |
220 | static struct usb_config_descriptor config_desc = { | |
221 | .bLength = USB_DT_CONFIG_SIZE, | |
222 | .bDescriptorType = USB_DT_CONFIG, | |
223 | /* compute wTotalLength on the fly */ | |
224 | .bNumInterfaces = GMIDI_NUM_INTERFACES, | |
225 | .bConfigurationValue = GMIDI_CONFIG, | |
226 | .iConfiguration = STRING_MIDI_GADGET, | |
227 | /* | |
228 | * FIXME: When embedding this driver in a device, | |
229 | * these need to be set to reflect the actual | |
230 | * power properties of the device. Is it selfpowered? | |
231 | */ | |
232 | .bmAttributes = USB_CONFIG_ATT_ONE, | |
233 | .bMaxPower = 1, | |
234 | }; | |
235 | ||
236 | /* B.3.1 Standard AC Interface Descriptor */ | |
237 | static const struct usb_interface_descriptor ac_interface_desc = { | |
238 | .bLength = USB_DT_INTERFACE_SIZE, | |
239 | .bDescriptorType = USB_DT_INTERFACE, | |
240 | .bInterfaceNumber = GMIDI_AC_INTERFACE, | |
241 | .bNumEndpoints = 0, | |
242 | .bInterfaceClass = USB_CLASS_AUDIO, | |
243 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, | |
244 | .iInterface = STRING_MIDI_GADGET, | |
245 | }; | |
246 | ||
247 | /* B.3.2 Class-Specific AC Interface Descriptor */ | |
248 | static const struct usb_ac_header_descriptor_1 ac_header_desc = { | |
249 | .bLength = USB_DT_AC_HEADER_SIZE(1), | |
250 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
251 | .bDescriptorSubtype = USB_MS_HEADER, | |
252 | .bcdADC = __constant_cpu_to_le16(0x0100), | |
253 | .wTotalLength = USB_DT_AC_HEADER_SIZE(1), | |
254 | .bInCollection = 1, | |
255 | .baInterfaceNr = { | |
256 | [0] = GMIDI_MS_INTERFACE, | |
257 | } | |
258 | }; | |
259 | ||
260 | /* B.4.1 Standard MS Interface Descriptor */ | |
261 | static const struct usb_interface_descriptor ms_interface_desc = { | |
262 | .bLength = USB_DT_INTERFACE_SIZE, | |
263 | .bDescriptorType = USB_DT_INTERFACE, | |
264 | .bInterfaceNumber = GMIDI_MS_INTERFACE, | |
265 | .bNumEndpoints = 2, | |
266 | .bInterfaceClass = USB_CLASS_AUDIO, | |
267 | .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING, | |
268 | .iInterface = STRING_MIDI_GADGET, | |
269 | }; | |
270 | ||
271 | /* B.4.2 Class-Specific MS Interface Descriptor */ | |
272 | static const struct usb_ms_header_descriptor ms_header_desc = { | |
273 | .bLength = USB_DT_MS_HEADER_SIZE, | |
274 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
275 | .bDescriptorSubtype = USB_MS_HEADER, | |
276 | .bcdMSC = __constant_cpu_to_le16(0x0100), | |
277 | .wTotalLength = USB_DT_MS_HEADER_SIZE | |
278 | + 2*USB_DT_MIDI_IN_SIZE | |
279 | + 2*USB_DT_MIDI_OUT_SIZE(1), | |
280 | }; | |
281 | ||
282 | #define JACK_IN_EMB 1 | |
283 | #define JACK_IN_EXT 2 | |
284 | #define JACK_OUT_EMB 3 | |
285 | #define JACK_OUT_EXT 4 | |
286 | ||
287 | /* B.4.3 MIDI IN Jack Descriptors */ | |
288 | static const struct usb_midi_in_jack_descriptor jack_in_emb_desc = { | |
289 | .bLength = USB_DT_MIDI_IN_SIZE, | |
290 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
291 | .bDescriptorSubtype = USB_MS_MIDI_IN_JACK, | |
292 | .bJackType = USB_MS_EMBEDDED, | |
293 | .bJackID = JACK_IN_EMB, | |
294 | }; | |
295 | ||
296 | static const struct usb_midi_in_jack_descriptor jack_in_ext_desc = { | |
297 | .bLength = USB_DT_MIDI_IN_SIZE, | |
298 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
299 | .bDescriptorSubtype = USB_MS_MIDI_IN_JACK, | |
300 | .bJackType = USB_MS_EXTERNAL, | |
301 | .bJackID = JACK_IN_EXT, | |
302 | }; | |
303 | ||
304 | /* B.4.4 MIDI OUT Jack Descriptors */ | |
305 | static const struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc = { | |
306 | .bLength = USB_DT_MIDI_OUT_SIZE(1), | |
307 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
308 | .bDescriptorSubtype = USB_MS_MIDI_OUT_JACK, | |
309 | .bJackType = USB_MS_EMBEDDED, | |
310 | .bJackID = JACK_OUT_EMB, | |
311 | .bNrInputPins = 1, | |
312 | .pins = { | |
313 | [0] = { | |
314 | .baSourceID = JACK_IN_EXT, | |
315 | .baSourcePin = 1, | |
316 | } | |
317 | } | |
318 | }; | |
319 | ||
320 | static const struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc = { | |
321 | .bLength = USB_DT_MIDI_OUT_SIZE(1), | |
322 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
323 | .bDescriptorSubtype = USB_MS_MIDI_OUT_JACK, | |
324 | .bJackType = USB_MS_EXTERNAL, | |
325 | .bJackID = JACK_OUT_EXT, | |
326 | .bNrInputPins = 1, | |
327 | .pins = { | |
328 | [0] = { | |
329 | .baSourceID = JACK_IN_EMB, | |
330 | .baSourcePin = 1, | |
331 | } | |
332 | } | |
333 | }; | |
334 | ||
335 | /* B.5.1 Standard Bulk OUT Endpoint Descriptor */ | |
336 | static struct usb_endpoint_descriptor bulk_out_desc = { | |
337 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, | |
338 | .bDescriptorType = USB_DT_ENDPOINT, | |
339 | .bEndpointAddress = USB_DIR_OUT, | |
340 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
341 | }; | |
342 | ||
343 | /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */ | |
344 | static const struct usb_ms_endpoint_descriptor_1 ms_out_desc = { | |
345 | .bLength = USB_DT_MS_ENDPOINT_SIZE(1), | |
346 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
347 | .bDescriptorSubtype = USB_MS_GENERAL, | |
348 | .bNumEmbMIDIJack = 1, | |
349 | .baAssocJackID = { | |
350 | [0] = JACK_IN_EMB, | |
351 | } | |
352 | }; | |
353 | ||
354 | /* B.6.1 Standard Bulk IN Endpoint Descriptor */ | |
355 | static struct usb_endpoint_descriptor bulk_in_desc = { | |
356 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, | |
357 | .bDescriptorType = USB_DT_ENDPOINT, | |
358 | .bEndpointAddress = USB_DIR_IN, | |
359 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
360 | }; | |
361 | ||
362 | /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */ | |
363 | static const struct usb_ms_endpoint_descriptor_1 ms_in_desc = { | |
364 | .bLength = USB_DT_MS_ENDPOINT_SIZE(1), | |
365 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
366 | .bDescriptorSubtype = USB_MS_GENERAL, | |
367 | .bNumEmbMIDIJack = 1, | |
368 | .baAssocJackID = { | |
369 | [0] = JACK_OUT_EMB, | |
370 | } | |
371 | }; | |
372 | ||
373 | static const struct usb_descriptor_header *gmidi_function [] = { | |
374 | (struct usb_descriptor_header *)&ac_interface_desc, | |
375 | (struct usb_descriptor_header *)&ac_header_desc, | |
376 | (struct usb_descriptor_header *)&ms_interface_desc, | |
377 | ||
378 | (struct usb_descriptor_header *)&ms_header_desc, | |
379 | (struct usb_descriptor_header *)&jack_in_emb_desc, | |
380 | (struct usb_descriptor_header *)&jack_in_ext_desc, | |
381 | (struct usb_descriptor_header *)&jack_out_emb_desc, | |
382 | (struct usb_descriptor_header *)&jack_out_ext_desc, | |
383 | /* If you add more jacks, update ms_header_desc.wTotalLength */ | |
384 | ||
385 | (struct usb_descriptor_header *)&bulk_out_desc, | |
386 | (struct usb_descriptor_header *)&ms_out_desc, | |
387 | (struct usb_descriptor_header *)&bulk_in_desc, | |
388 | (struct usb_descriptor_header *)&ms_in_desc, | |
389 | NULL, | |
390 | }; | |
391 | ||
392 | static char manufacturer[50]; | |
393 | static char product_desc[40] = "MIDI Gadget"; | |
394 | static char serial_number[20]; | |
395 | ||
396 | /* static strings, in UTF-8 */ | |
397 | static struct usb_string strings [] = { | |
398 | { STRING_MANUFACTURER, manufacturer, }, | |
399 | { STRING_PRODUCT, product_desc, }, | |
400 | { STRING_SERIAL, serial_number, }, | |
401 | { STRING_MIDI_GADGET, longname, }, | |
402 | { } /* end of list */ | |
403 | }; | |
404 | ||
405 | static struct usb_gadget_strings stringtab = { | |
406 | .language = 0x0409, /* en-us */ | |
407 | .strings = strings, | |
408 | }; | |
409 | ||
410 | static int config_buf(struct usb_gadget *gadget, | |
411 | u8 *buf, u8 type, unsigned index) | |
412 | { | |
413 | int len; | |
414 | ||
415 | /* only one configuration */ | |
416 | if (index != 0) { | |
417 | return -EINVAL; | |
418 | } | |
419 | len = usb_gadget_config_buf(&config_desc, | |
420 | buf, USB_BUFSIZ, gmidi_function); | |
421 | if (len < 0) { | |
422 | return len; | |
423 | } | |
424 | ((struct usb_config_descriptor *)buf)->bDescriptorType = type; | |
425 | return len; | |
426 | } | |
427 | ||
428 | static struct usb_request* alloc_ep_req(struct usb_ep *ep, unsigned length) | |
429 | { | |
430 | struct usb_request *req; | |
431 | ||
432 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); | |
433 | if (req) { | |
434 | req->length = length; | |
435 | req->buf = kmalloc(length, GFP_ATOMIC); | |
436 | if (!req->buf) { | |
437 | usb_ep_free_request(ep, req); | |
438 | req = NULL; | |
439 | } | |
440 | } | |
441 | return req; | |
442 | } | |
443 | ||
444 | static void free_ep_req(struct usb_ep *ep, struct usb_request *req) | |
445 | { | |
446 | kfree(req->buf); | |
447 | usb_ep_free_request(ep, req); | |
448 | } | |
449 | ||
450 | static const uint8_t gmidi_cin_length[] = { | |
451 | 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1 | |
452 | }; | |
453 | ||
454 | /* | |
455 | * Receives a chunk of MIDI data. | |
456 | */ | |
457 | static void gmidi_read_data(struct usb_ep *ep, int cable, | |
458 | uint8_t* data, int length) | |
459 | { | |
460 | struct gmidi_device *dev = ep->driver_data; | |
461 | /* cable is ignored, because for now we only have one. */ | |
462 | ||
463 | if (!dev->out_substream) { | |
464 | /* Nobody is listening - throw it on the floor. */ | |
465 | return; | |
466 | } | |
467 | if (!test_bit(dev->out_substream->number, &dev->out_triggered)) { | |
468 | return; | |
469 | } | |
470 | snd_rawmidi_receive(dev->out_substream, data, length); | |
471 | } | |
472 | ||
473 | static void gmidi_handle_out_data(struct usb_ep *ep, struct usb_request *req) | |
474 | { | |
475 | unsigned i; | |
476 | u8 *buf = req->buf; | |
477 | ||
478 | for (i = 0; i + 3 < req->actual; i += 4) { | |
479 | if (buf[i] != 0) { | |
480 | int cable = buf[i] >> 4; | |
481 | int length = gmidi_cin_length[buf[i] & 0x0f]; | |
482 | gmidi_read_data(ep, cable, &buf[i + 1], length); | |
483 | } | |
484 | } | |
485 | } | |
486 | ||
487 | static void gmidi_complete(struct usb_ep *ep, struct usb_request *req) | |
488 | { | |
489 | struct gmidi_device *dev = ep->driver_data; | |
490 | int status = req->status; | |
491 | ||
492 | switch (status) { | |
6bea476c | 493 | case 0: /* normal completion */ |
f2ebf92c BW |
494 | if (ep == dev->out_ep) { |
495 | /* we received stuff. | |
496 | req is queued again, below */ | |
497 | gmidi_handle_out_data(ep, req); | |
498 | } else if (ep == dev->in_ep) { | |
499 | /* our transmit completed. | |
500 | see if there's more to go. | |
501 | gmidi_transmit eats req, don't queue it again. */ | |
502 | gmidi_transmit(dev, req); | |
503 | return; | |
504 | } | |
505 | break; | |
506 | ||
507 | /* this endpoint is normally active while we're configured */ | |
6bea476c | 508 | case -ECONNABORTED: /* hardware forced ep reset */ |
f2ebf92c BW |
509 | case -ECONNRESET: /* request dequeued */ |
510 | case -ESHUTDOWN: /* disconnect from host */ | |
511 | VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status, | |
512 | req->actual, req->length); | |
513 | if (ep == dev->out_ep) { | |
514 | gmidi_handle_out_data(ep, req); | |
515 | } | |
516 | free_ep_req(ep, req); | |
517 | return; | |
518 | ||
519 | case -EOVERFLOW: /* buffer overrun on read means that | |
520 | * we didn't provide a big enough | |
521 | * buffer. | |
522 | */ | |
523 | default: | |
524 | DBG(dev, "%s complete --> %d, %d/%d\n", ep->name, | |
525 | status, req->actual, req->length); | |
526 | break; | |
527 | case -EREMOTEIO: /* short read */ | |
528 | break; | |
529 | } | |
530 | ||
531 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | |
532 | if (status) { | |
533 | ERROR(dev, "kill %s: resubmit %d bytes --> %d\n", | |
534 | ep->name, req->length, status); | |
535 | usb_ep_set_halt(ep); | |
536 | /* FIXME recover later ... somehow */ | |
537 | } | |
538 | } | |
539 | ||
540 | static int set_gmidi_config(struct gmidi_device *dev, gfp_t gfp_flags) | |
541 | { | |
542 | int err = 0; | |
543 | struct usb_request *req; | |
544 | struct usb_ep* ep; | |
545 | unsigned i; | |
546 | ||
547 | err = usb_ep_enable(dev->in_ep, &bulk_in_desc); | |
548 | if (err) { | |
549 | ERROR(dev, "can't start %s: %d\n", dev->in_ep->name, err); | |
550 | goto fail; | |
551 | } | |
552 | dev->in_ep->driver_data = dev; | |
553 | ||
554 | err = usb_ep_enable(dev->out_ep, &bulk_out_desc); | |
555 | if (err) { | |
556 | ERROR(dev, "can't start %s: %d\n", dev->out_ep->name, err); | |
557 | goto fail; | |
558 | } | |
559 | dev->out_ep->driver_data = dev; | |
560 | ||
561 | /* allocate a bunch of read buffers and queue them all at once. */ | |
562 | ep = dev->out_ep; | |
563 | for (i = 0; i < qlen && err == 0; i++) { | |
564 | req = alloc_ep_req(ep, buflen); | |
565 | if (req) { | |
566 | req->complete = gmidi_complete; | |
567 | err = usb_ep_queue(ep, req, GFP_ATOMIC); | |
568 | if (err) { | |
569 | DBG(dev, "%s queue req: %d\n", ep->name, err); | |
570 | } | |
571 | } else { | |
572 | err = -ENOMEM; | |
573 | } | |
574 | } | |
575 | fail: | |
576 | /* caller is responsible for cleanup on error */ | |
577 | return err; | |
578 | } | |
579 | ||
580 | ||
581 | static void gmidi_reset_config(struct gmidi_device *dev) | |
582 | { | |
583 | if (dev->config == 0) { | |
584 | return; | |
585 | } | |
586 | ||
587 | DBG(dev, "reset config\n"); | |
588 | ||
589 | /* just disable endpoints, forcing completion of pending i/o. | |
590 | * all our completion handlers free their requests in this case. | |
591 | */ | |
592 | usb_ep_disable(dev->in_ep); | |
593 | usb_ep_disable(dev->out_ep); | |
594 | dev->config = 0; | |
595 | } | |
596 | ||
597 | /* change our operational config. this code must agree with the code | |
598 | * that returns config descriptors, and altsetting code. | |
599 | * | |
600 | * it's also responsible for power management interactions. some | |
601 | * configurations might not work with our current power sources. | |
602 | * | |
603 | * note that some device controller hardware will constrain what this | |
604 | * code can do, perhaps by disallowing more than one configuration or | |
605 | * by limiting configuration choices (like the pxa2xx). | |
606 | */ | |
607 | static int | |
608 | gmidi_set_config(struct gmidi_device *dev, unsigned number, gfp_t gfp_flags) | |
609 | { | |
610 | int result = 0; | |
611 | struct usb_gadget *gadget = dev->gadget; | |
612 | ||
613 | #if 0 | |
614 | /* FIXME */ | |
615 | /* Hacking this bit out fixes a bug where on receipt of two | |
616 | USB_REQ_SET_CONFIGURATION messages, we end up with no | |
617 | buffered OUT requests waiting for data. This is clearly | |
618 | hiding a bug elsewhere, because if the config didn't | |
619 | change then we really shouldn't do anything. */ | |
620 | /* Having said that, when we do "change" from config 1 | |
621 | to config 1, we at least gmidi_reset_config() which | |
622 | clears out any requests on endpoints, so it's not like | |
623 | we leak or anything. */ | |
624 | if (number == dev->config) { | |
625 | return 0; | |
626 | } | |
627 | #endif | |
628 | ||
629 | if (gadget_is_sa1100(gadget) && dev->config) { | |
630 | /* tx fifo is full, but we can't clear it...*/ | |
631 | INFO(dev, "can't change configurations\n"); | |
632 | return -ESPIPE; | |
633 | } | |
634 | gmidi_reset_config(dev); | |
635 | ||
636 | switch (number) { | |
637 | case GMIDI_CONFIG: | |
638 | result = set_gmidi_config(dev, gfp_flags); | |
639 | break; | |
640 | default: | |
641 | result = -EINVAL; | |
642 | /* FALL THROUGH */ | |
643 | case 0: | |
644 | return result; | |
645 | } | |
646 | ||
647 | if (!result && (!dev->in_ep || !dev->out_ep)) { | |
648 | result = -ENODEV; | |
649 | } | |
650 | if (result) { | |
651 | gmidi_reset_config(dev); | |
652 | } else { | |
653 | char *speed; | |
654 | ||
655 | switch (gadget->speed) { | |
656 | case USB_SPEED_LOW: speed = "low"; break; | |
657 | case USB_SPEED_FULL: speed = "full"; break; | |
658 | case USB_SPEED_HIGH: speed = "high"; break; | |
6bea476c | 659 | default: speed = "?"; break; |
f2ebf92c BW |
660 | } |
661 | ||
662 | dev->config = number; | |
663 | INFO(dev, "%s speed\n", speed); | |
664 | } | |
665 | return result; | |
666 | } | |
667 | ||
668 | ||
669 | static void gmidi_setup_complete(struct usb_ep *ep, struct usb_request *req) | |
670 | { | |
671 | if (req->status || req->actual != req->length) { | |
672 | DBG((struct gmidi_device *) ep->driver_data, | |
673 | "setup complete --> %d, %d/%d\n", | |
674 | req->status, req->actual, req->length); | |
675 | } | |
676 | } | |
677 | ||
678 | /* | |
679 | * The setup() callback implements all the ep0 functionality that's | |
680 | * not handled lower down, in hardware or the hardware driver (like | |
681 | * device and endpoint feature flags, and their status). It's all | |
682 | * housekeeping for the gadget function we're implementing. Most of | |
683 | * the work is in config-specific setup. | |
684 | */ | |
685 | static int gmidi_setup(struct usb_gadget *gadget, | |
686 | const struct usb_ctrlrequest *ctrl) | |
687 | { | |
688 | struct gmidi_device *dev = get_gadget_data(gadget); | |
689 | struct usb_request *req = dev->req; | |
690 | int value = -EOPNOTSUPP; | |
691 | u16 w_index = le16_to_cpu(ctrl->wIndex); | |
692 | u16 w_value = le16_to_cpu(ctrl->wValue); | |
693 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
694 | ||
695 | /* usually this stores reply data in the pre-allocated ep0 buffer, | |
696 | * but config change events will reconfigure hardware. | |
697 | */ | |
698 | req->zero = 0; | |
699 | switch (ctrl->bRequest) { | |
700 | ||
701 | case USB_REQ_GET_DESCRIPTOR: | |
702 | if (ctrl->bRequestType != USB_DIR_IN) { | |
703 | goto unknown; | |
704 | } | |
705 | switch (w_value >> 8) { | |
706 | ||
707 | case USB_DT_DEVICE: | |
708 | value = min(w_length, (u16) sizeof(device_desc)); | |
709 | memcpy(req->buf, &device_desc, value); | |
710 | break; | |
711 | case USB_DT_CONFIG: | |
712 | value = config_buf(gadget, req->buf, | |
713 | w_value >> 8, | |
714 | w_value & 0xff); | |
715 | if (value >= 0) { | |
716 | value = min(w_length, (u16)value); | |
717 | } | |
718 | break; | |
719 | ||
720 | case USB_DT_STRING: | |
721 | /* wIndex == language code. | |
722 | * this driver only handles one language, you can | |
723 | * add string tables for other languages, using | |
724 | * any UTF-8 characters | |
725 | */ | |
726 | value = usb_gadget_get_string(&stringtab, | |
727 | w_value & 0xff, req->buf); | |
728 | if (value >= 0) { | |
729 | value = min(w_length, (u16)value); | |
730 | } | |
731 | break; | |
732 | } | |
733 | break; | |
734 | ||
735 | /* currently two configs, two speeds */ | |
736 | case USB_REQ_SET_CONFIGURATION: | |
737 | if (ctrl->bRequestType != 0) { | |
738 | goto unknown; | |
739 | } | |
740 | if (gadget->a_hnp_support) { | |
741 | DBG(dev, "HNP available\n"); | |
742 | } else if (gadget->a_alt_hnp_support) { | |
743 | DBG(dev, "HNP needs a different root port\n"); | |
744 | } else { | |
745 | VDBG(dev, "HNP inactive\n"); | |
746 | } | |
747 | spin_lock(&dev->lock); | |
748 | value = gmidi_set_config(dev, w_value, GFP_ATOMIC); | |
749 | spin_unlock(&dev->lock); | |
750 | break; | |
751 | case USB_REQ_GET_CONFIGURATION: | |
752 | if (ctrl->bRequestType != USB_DIR_IN) { | |
753 | goto unknown; | |
754 | } | |
755 | *(u8 *)req->buf = dev->config; | |
756 | value = min(w_length, (u16)1); | |
757 | break; | |
758 | ||
759 | /* until we add altsetting support, or other interfaces, | |
760 | * only 0/0 are possible. pxa2xx only supports 0/0 (poorly) | |
761 | * and already killed pending endpoint I/O. | |
762 | */ | |
763 | case USB_REQ_SET_INTERFACE: | |
764 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) { | |
765 | goto unknown; | |
766 | } | |
767 | spin_lock(&dev->lock); | |
768 | if (dev->config && w_index < GMIDI_NUM_INTERFACES | |
769 | && w_value == 0) | |
770 | { | |
771 | u8 config = dev->config; | |
772 | ||
773 | /* resets interface configuration, forgets about | |
774 | * previous transaction state (queued bufs, etc) | |
775 | * and re-inits endpoint state (toggle etc) | |
776 | * no response queued, just zero status == success. | |
777 | * if we had more than one interface we couldn't | |
778 | * use this "reset the config" shortcut. | |
779 | */ | |
780 | gmidi_reset_config(dev); | |
781 | gmidi_set_config(dev, config, GFP_ATOMIC); | |
782 | value = 0; | |
783 | } | |
784 | spin_unlock(&dev->lock); | |
785 | break; | |
786 | case USB_REQ_GET_INTERFACE: | |
787 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) { | |
788 | goto unknown; | |
789 | } | |
790 | if (!dev->config) { | |
791 | break; | |
792 | } | |
793 | if (w_index >= GMIDI_NUM_INTERFACES) { | |
794 | value = -EDOM; | |
795 | break; | |
796 | } | |
797 | *(u8 *)req->buf = 0; | |
798 | value = min(w_length, (u16)1); | |
799 | break; | |
800 | ||
801 | default: | |
802 | unknown: | |
803 | VDBG(dev, "unknown control req%02x.%02x v%04x i%04x l%d\n", | |
804 | ctrl->bRequestType, ctrl->bRequest, | |
805 | w_value, w_index, w_length); | |
806 | } | |
807 | ||
808 | /* respond with data transfer before status phase? */ | |
809 | if (value >= 0) { | |
810 | req->length = value; | |
811 | req->zero = value < w_length; | |
812 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); | |
813 | if (value < 0) { | |
814 | DBG(dev, "ep_queue --> %d\n", value); | |
815 | req->status = 0; | |
816 | gmidi_setup_complete(gadget->ep0, req); | |
817 | } | |
818 | } | |
819 | ||
820 | /* device either stalls (value < 0) or reports success */ | |
821 | return value; | |
822 | } | |
823 | ||
824 | static void gmidi_disconnect(struct usb_gadget *gadget) | |
825 | { | |
826 | struct gmidi_device *dev = get_gadget_data(gadget); | |
827 | unsigned long flags; | |
828 | ||
829 | spin_lock_irqsave(&dev->lock, flags); | |
830 | gmidi_reset_config(dev); | |
831 | ||
832 | /* a more significant application might have some non-usb | |
833 | * activities to quiesce here, saving resources like power | |
834 | * or pushing the notification up a network stack. | |
835 | */ | |
836 | spin_unlock_irqrestore(&dev->lock, flags); | |
837 | ||
838 | /* next we may get setup() calls to enumerate new connections; | |
839 | * or an unbind() during shutdown (including removing module). | |
840 | */ | |
841 | } | |
842 | ||
843 | static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget *gadget) | |
844 | { | |
845 | struct gmidi_device *dev = get_gadget_data(gadget); | |
846 | struct snd_card* card; | |
847 | ||
848 | DBG(dev, "unbind\n"); | |
849 | ||
850 | card = dev->card; | |
851 | dev->card = NULL; | |
852 | if (card) { | |
853 | snd_card_free(card); | |
854 | } | |
855 | ||
856 | /* we've already been disconnected ... no i/o is active */ | |
857 | if (dev->req) { | |
858 | dev->req->length = USB_BUFSIZ; | |
859 | free_ep_req(gadget->ep0, dev->req); | |
860 | } | |
861 | kfree(dev); | |
862 | set_gadget_data(gadget, NULL); | |
863 | } | |
864 | ||
865 | static int gmidi_snd_free(struct snd_device *device) | |
866 | { | |
867 | return 0; | |
868 | } | |
869 | ||
870 | static void gmidi_transmit_packet(struct usb_request* req, uint8_t p0, | |
871 | uint8_t p1, uint8_t p2, uint8_t p3) | |
872 | { | |
873 | unsigned length = req->length; | |
874 | ||
875 | uint8_t* buf = (uint8_t*)req->buf + length; | |
876 | buf[0] = p0; | |
877 | buf[1] = p1; | |
878 | buf[2] = p2; | |
879 | buf[3] = p3; | |
880 | req->length = length + 4; | |
881 | } | |
882 | ||
883 | /* | |
884 | * Converts MIDI commands to USB MIDI packets. | |
885 | */ | |
886 | static void gmidi_transmit_byte(struct usb_request* req, | |
887 | struct gmidi_in_port* port, uint8_t b) | |
888 | { | |
889 | uint8_t p0 = port->cable; | |
890 | ||
891 | if (b >= 0xf8) { | |
892 | gmidi_transmit_packet(req, p0 | 0x0f, b, 0, 0); | |
893 | } else if (b >= 0xf0) { | |
894 | switch (b) { | |
895 | case 0xf0: | |
896 | port->data[0] = b; | |
897 | port->state = STATE_SYSEX_1; | |
898 | break; | |
899 | case 0xf1: | |
900 | case 0xf3: | |
901 | port->data[0] = b; | |
902 | port->state = STATE_1PARAM; | |
903 | break; | |
904 | case 0xf2: | |
905 | port->data[0] = b; | |
906 | port->state = STATE_2PARAM_1; | |
907 | break; | |
908 | case 0xf4: | |
909 | case 0xf5: | |
910 | port->state = STATE_UNKNOWN; | |
911 | break; | |
912 | case 0xf6: | |
913 | gmidi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0); | |
914 | port->state = STATE_UNKNOWN; | |
915 | break; | |
916 | case 0xf7: | |
917 | switch (port->state) { | |
918 | case STATE_SYSEX_0: | |
919 | gmidi_transmit_packet(req, | |
920 | p0 | 0x05, 0xf7, 0, 0); | |
921 | break; | |
922 | case STATE_SYSEX_1: | |
923 | gmidi_transmit_packet(req, | |
924 | p0 | 0x06, port->data[0], 0xf7, 0); | |
925 | break; | |
926 | case STATE_SYSEX_2: | |
927 | gmidi_transmit_packet(req, | |
928 | p0 | 0x07, port->data[0], | |
929 | port->data[1], 0xf7); | |
930 | break; | |
931 | } | |
932 | port->state = STATE_UNKNOWN; | |
933 | break; | |
934 | } | |
935 | } else if (b >= 0x80) { | |
936 | port->data[0] = b; | |
937 | if (b >= 0xc0 && b <= 0xdf) | |
938 | port->state = STATE_1PARAM; | |
939 | else | |
940 | port->state = STATE_2PARAM_1; | |
941 | } else { /* b < 0x80 */ | |
942 | switch (port->state) { | |
943 | case STATE_1PARAM: | |
944 | if (port->data[0] < 0xf0) { | |
945 | p0 |= port->data[0] >> 4; | |
946 | } else { | |
947 | p0 |= 0x02; | |
948 | port->state = STATE_UNKNOWN; | |
949 | } | |
950 | gmidi_transmit_packet(req, p0, port->data[0], b, 0); | |
951 | break; | |
952 | case STATE_2PARAM_1: | |
953 | port->data[1] = b; | |
954 | port->state = STATE_2PARAM_2; | |
955 | break; | |
956 | case STATE_2PARAM_2: | |
957 | if (port->data[0] < 0xf0) { | |
958 | p0 |= port->data[0] >> 4; | |
959 | port->state = STATE_2PARAM_1; | |
960 | } else { | |
961 | p0 |= 0x03; | |
962 | port->state = STATE_UNKNOWN; | |
963 | } | |
964 | gmidi_transmit_packet(req, | |
965 | p0, port->data[0], port->data[1], b); | |
966 | break; | |
967 | case STATE_SYSEX_0: | |
968 | port->data[0] = b; | |
969 | port->state = STATE_SYSEX_1; | |
970 | break; | |
971 | case STATE_SYSEX_1: | |
972 | port->data[1] = b; | |
973 | port->state = STATE_SYSEX_2; | |
974 | break; | |
975 | case STATE_SYSEX_2: | |
976 | gmidi_transmit_packet(req, | |
977 | p0 | 0x04, port->data[0], port->data[1], b); | |
978 | port->state = STATE_SYSEX_0; | |
979 | break; | |
980 | } | |
981 | } | |
982 | } | |
983 | ||
984 | static void gmidi_transmit(struct gmidi_device* dev, struct usb_request* req) | |
985 | { | |
986 | struct usb_ep* ep = dev->in_ep; | |
987 | struct gmidi_in_port* port = &dev->in_port; | |
988 | ||
989 | if (!ep) { | |
990 | return; | |
991 | } | |
992 | if (!req) { | |
993 | req = alloc_ep_req(ep, buflen); | |
994 | } | |
995 | if (!req) { | |
996 | ERROR(dev, "gmidi_transmit: alloc_ep_request failed\n"); | |
997 | return; | |
998 | } | |
999 | req->length = 0; | |
1000 | req->complete = gmidi_complete; | |
1001 | ||
1002 | if (port->active) { | |
1003 | while (req->length + 3 < buflen) { | |
1004 | uint8_t b; | |
1005 | if (snd_rawmidi_transmit(dev->in_substream, &b, 1) | |
1006 | != 1) | |
1007 | { | |
1008 | port->active = 0; | |
1009 | break; | |
1010 | } | |
1011 | gmidi_transmit_byte(req, port, b); | |
1012 | } | |
1013 | } | |
1014 | if (req->length > 0) { | |
1015 | usb_ep_queue(ep, req, GFP_ATOMIC); | |
1016 | } else { | |
1017 | free_ep_req(ep, req); | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | static void gmidi_in_tasklet(unsigned long data) | |
1022 | { | |
1023 | struct gmidi_device* dev = (struct gmidi_device*)data; | |
1024 | ||
1025 | gmidi_transmit(dev, NULL); | |
1026 | } | |
1027 | ||
1028 | static int gmidi_in_open(struct snd_rawmidi_substream *substream) | |
1029 | { | |
1030 | struct gmidi_device* dev = substream->rmidi->private_data; | |
1031 | ||
1032 | VDBG(dev, "gmidi_in_open\n"); | |
1033 | dev->in_substream = substream; | |
1034 | dev->in_port.state = STATE_UNKNOWN; | |
1035 | return 0; | |
1036 | } | |
1037 | ||
1038 | static int gmidi_in_close(struct snd_rawmidi_substream *substream) | |
1039 | { | |
1040 | VDBG(dev, "gmidi_in_close\n"); | |
1041 | return 0; | |
1042 | } | |
1043 | ||
1044 | static void gmidi_in_trigger(struct snd_rawmidi_substream *substream, int up) | |
1045 | { | |
1046 | struct gmidi_device* dev = substream->rmidi->private_data; | |
1047 | ||
1048 | VDBG(dev, "gmidi_in_trigger %d\n", up); | |
1049 | dev->in_port.active = up; | |
1050 | if (up) { | |
1051 | tasklet_hi_schedule(&dev->tasklet); | |
1052 | } | |
1053 | } | |
1054 | ||
1055 | static int gmidi_out_open(struct snd_rawmidi_substream *substream) | |
1056 | { | |
1057 | struct gmidi_device* dev = substream->rmidi->private_data; | |
1058 | ||
1059 | VDBG(dev, "gmidi_out_open\n"); | |
1060 | dev->out_substream = substream; | |
1061 | return 0; | |
1062 | } | |
1063 | ||
1064 | static int gmidi_out_close(struct snd_rawmidi_substream *substream) | |
1065 | { | |
1066 | VDBG(dev, "gmidi_out_close\n"); | |
1067 | return 0; | |
1068 | } | |
1069 | ||
1070 | static void gmidi_out_trigger(struct snd_rawmidi_substream *substream, int up) | |
1071 | { | |
1072 | struct gmidi_device* dev = substream->rmidi->private_data; | |
1073 | ||
1074 | VDBG(dev, "gmidi_out_trigger %d\n", up); | |
1075 | if (up) { | |
1076 | set_bit(substream->number, &dev->out_triggered); | |
1077 | } else { | |
1078 | clear_bit(substream->number, &dev->out_triggered); | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | static struct snd_rawmidi_ops gmidi_in_ops = { | |
1083 | .open = gmidi_in_open, | |
1084 | .close = gmidi_in_close, | |
1085 | .trigger = gmidi_in_trigger, | |
1086 | }; | |
1087 | ||
1088 | static struct snd_rawmidi_ops gmidi_out_ops = { | |
1089 | .open = gmidi_out_open, | |
1090 | .close = gmidi_out_close, | |
1091 | .trigger = gmidi_out_trigger | |
1092 | }; | |
1093 | ||
1094 | /* register as a sound "card" */ | |
1095 | static int gmidi_register_card(struct gmidi_device *dev) | |
1096 | { | |
1097 | struct snd_card *card; | |
1098 | struct snd_rawmidi *rmidi; | |
1099 | int err; | |
1100 | int out_ports = 1; | |
1101 | int in_ports = 1; | |
1102 | static struct snd_device_ops ops = { | |
1103 | .dev_free = gmidi_snd_free, | |
1104 | }; | |
1105 | ||
1106 | card = snd_card_new(index, id, THIS_MODULE, 0); | |
1107 | if (!card) { | |
1108 | ERROR(dev, "snd_card_new failed\n"); | |
1109 | err = -ENOMEM; | |
1110 | goto fail; | |
1111 | } | |
1112 | dev->card = card; | |
1113 | ||
1114 | err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, dev, &ops); | |
1115 | if (err < 0) { | |
1116 | ERROR(dev, "snd_device_new failed: error %d\n", err); | |
1117 | goto fail; | |
1118 | } | |
1119 | ||
1120 | strcpy(card->driver, longname); | |
1121 | strcpy(card->longname, longname); | |
1122 | strcpy(card->shortname, shortname); | |
1123 | ||
1124 | /* Set up rawmidi */ | |
1125 | dev->in_port.dev = dev; | |
1126 | dev->in_port.active = 0; | |
1127 | snd_component_add(card, "MIDI"); | |
1128 | err = snd_rawmidi_new(card, "USB MIDI Gadget", 0, | |
1129 | out_ports, in_ports, &rmidi); | |
1130 | if (err < 0) { | |
1131 | ERROR(dev, "snd_rawmidi_new failed: error %d\n", err); | |
1132 | goto fail; | |
1133 | } | |
1134 | dev->rmidi = rmidi; | |
1135 | strcpy(rmidi->name, card->shortname); | |
1136 | rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | | |
1137 | SNDRV_RAWMIDI_INFO_INPUT | | |
1138 | SNDRV_RAWMIDI_INFO_DUPLEX; | |
1139 | rmidi->private_data = dev; | |
1140 | ||
1141 | /* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT. | |
1142 | It's an upside-down world being a gadget. */ | |
1143 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops); | |
1144 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops); | |
1145 | ||
1146 | snd_card_set_dev(card, &dev->gadget->dev); | |
1147 | ||
1148 | /* register it - we're ready to go */ | |
1149 | err = snd_card_register(card); | |
1150 | if (err < 0) { | |
1151 | ERROR(dev, "snd_card_register failed\n"); | |
1152 | goto fail; | |
1153 | } | |
1154 | ||
1155 | VDBG(dev, "gmidi_register_card finished ok\n"); | |
1156 | return 0; | |
1157 | ||
1158 | fail: | |
1159 | if (dev->card) { | |
1160 | snd_card_free(dev->card); | |
1161 | dev->card = NULL; | |
1162 | } | |
1163 | return err; | |
1164 | } | |
1165 | ||
1166 | /* | |
1167 | * Creates an output endpoint, and initializes output ports. | |
1168 | */ | |
1169 | static int __devinit gmidi_bind(struct usb_gadget *gadget) | |
1170 | { | |
1171 | struct gmidi_device *dev; | |
1172 | struct usb_ep *in_ep, *out_ep; | |
1173 | int gcnum, err = 0; | |
1174 | ||
1175 | /* support optional vendor/distro customization */ | |
1176 | if (idVendor) { | |
1177 | if (!idProduct) { | |
1178 | printk(KERN_ERR "idVendor needs idProduct!\n"); | |
1179 | return -ENODEV; | |
1180 | } | |
1181 | device_desc.idVendor = cpu_to_le16(idVendor); | |
1182 | device_desc.idProduct = cpu_to_le16(idProduct); | |
1183 | if (bcdDevice) { | |
1184 | device_desc.bcdDevice = cpu_to_le16(bcdDevice); | |
1185 | } | |
1186 | } | |
1187 | if (iManufacturer) { | |
1188 | strlcpy(manufacturer, iManufacturer, sizeof(manufacturer)); | |
1189 | } else { | |
1190 | snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", | |
96b644bd | 1191 | init_utsname()->sysname, init_utsname()->release, |
f2ebf92c BW |
1192 | gadget->name); |
1193 | } | |
1194 | if (iProduct) { | |
1195 | strlcpy(product_desc, iProduct, sizeof(product_desc)); | |
1196 | } | |
1197 | if (iSerialNumber) { | |
1198 | device_desc.iSerialNumber = STRING_SERIAL, | |
1199 | strlcpy(serial_number, iSerialNumber, sizeof(serial_number)); | |
1200 | } | |
1201 | ||
1202 | /* Bulk-only drivers like this one SHOULD be able to | |
1203 | * autoconfigure on any sane usb controller driver, | |
1204 | * but there may also be important quirks to address. | |
1205 | */ | |
1206 | usb_ep_autoconfig_reset(gadget); | |
1207 | in_ep = usb_ep_autoconfig(gadget, &bulk_in_desc); | |
1208 | if (!in_ep) { | |
1209 | autoconf_fail: | |
1210 | printk(KERN_ERR "%s: can't autoconfigure on %s\n", | |
1211 | shortname, gadget->name); | |
1212 | return -ENODEV; | |
1213 | } | |
1214 | EP_IN_NAME = in_ep->name; | |
1215 | in_ep->driver_data = in_ep; /* claim */ | |
1216 | ||
1217 | out_ep = usb_ep_autoconfig(gadget, &bulk_out_desc); | |
1218 | if (!out_ep) { | |
1219 | goto autoconf_fail; | |
1220 | } | |
1221 | EP_OUT_NAME = out_ep->name; | |
1222 | out_ep->driver_data = out_ep; /* claim */ | |
1223 | ||
1224 | gcnum = usb_gadget_controller_number(gadget); | |
1225 | if (gcnum >= 0) { | |
1226 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); | |
1227 | } else { | |
1228 | /* gmidi is so simple (no altsettings) that | |
1229 | * it SHOULD NOT have problems with bulk-capable hardware. | |
1230 | * so warn about unrecognized controllers, don't panic. | |
1231 | */ | |
1232 | printk(KERN_WARNING "%s: controller '%s' not recognized\n", | |
1233 | shortname, gadget->name); | |
1234 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); | |
1235 | } | |
1236 | ||
1237 | ||
1238 | /* ok, we made sense of the hardware ... */ | |
e94b1766 | 1239 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
f2ebf92c BW |
1240 | if (!dev) { |
1241 | return -ENOMEM; | |
1242 | } | |
1243 | spin_lock_init(&dev->lock); | |
1244 | dev->gadget = gadget; | |
1245 | dev->in_ep = in_ep; | |
1246 | dev->out_ep = out_ep; | |
1247 | set_gadget_data(gadget, dev); | |
1248 | tasklet_init(&dev->tasklet, gmidi_in_tasklet, (unsigned long)dev); | |
1249 | ||
1250 | /* preallocate control response and buffer */ | |
9d8bab58 | 1251 | dev->req = alloc_ep_req(gadget->ep0, USB_BUFSIZ); |
f2ebf92c BW |
1252 | if (!dev->req) { |
1253 | err = -ENOMEM; | |
1254 | goto fail; | |
1255 | } | |
f2ebf92c BW |
1256 | |
1257 | dev->req->complete = gmidi_setup_complete; | |
1258 | ||
1259 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | |
1260 | ||
1261 | gadget->ep0->driver_data = dev; | |
1262 | ||
1263 | INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname); | |
1264 | INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, | |
1265 | EP_OUT_NAME, EP_IN_NAME); | |
1266 | ||
1267 | /* register as an ALSA sound card */ | |
1268 | err = gmidi_register_card(dev); | |
1269 | if (err < 0) { | |
1270 | goto fail; | |
1271 | } | |
1272 | ||
1273 | VDBG(dev, "gmidi_bind finished ok\n"); | |
1274 | return 0; | |
1275 | ||
1276 | fail: | |
1277 | gmidi_unbind(gadget); | |
1278 | return err; | |
1279 | } | |
1280 | ||
1281 | ||
1282 | static void gmidi_suspend(struct usb_gadget *gadget) | |
1283 | { | |
1284 | struct gmidi_device *dev = get_gadget_data(gadget); | |
1285 | ||
1286 | if (gadget->speed == USB_SPEED_UNKNOWN) { | |
1287 | return; | |
1288 | } | |
1289 | ||
1290 | DBG(dev, "suspend\n"); | |
1291 | } | |
1292 | ||
1293 | static void gmidi_resume(struct usb_gadget *gadget) | |
1294 | { | |
1295 | struct gmidi_device *dev = get_gadget_data(gadget); | |
1296 | ||
1297 | DBG(dev, "resume\n"); | |
1298 | } | |
1299 | ||
1300 | ||
1301 | static struct usb_gadget_driver gmidi_driver = { | |
1302 | .speed = USB_SPEED_FULL, | |
1303 | .function = (char *)longname, | |
1304 | .bind = gmidi_bind, | |
6bea476c | 1305 | .unbind = gmidi_unbind, |
f2ebf92c BW |
1306 | |
1307 | .setup = gmidi_setup, | |
1308 | .disconnect = gmidi_disconnect, | |
1309 | ||
1310 | .suspend = gmidi_suspend, | |
1311 | .resume = gmidi_resume, | |
1312 | ||
6bea476c | 1313 | .driver = { |
f2ebf92c BW |
1314 | .name = (char *)shortname, |
1315 | .owner = THIS_MODULE, | |
1316 | }, | |
1317 | }; | |
1318 | ||
1319 | static int __init gmidi_init(void) | |
1320 | { | |
1321 | return usb_gadget_register_driver(&gmidi_driver); | |
1322 | } | |
1323 | module_init(gmidi_init); | |
1324 | ||
1325 | static void __exit gmidi_cleanup(void) | |
1326 | { | |
1327 | usb_gadget_unregister_driver(&gmidi_driver); | |
1328 | } | |
1329 | module_exit(gmidi_cleanup); | |
1330 |