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