gmidi.c 33 KB

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