gmidi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. dev->in_ep->desc = &bulk_in_desc;
  461. err = usb_ep_enable(dev->in_ep);
  462. if (err) {
  463. ERROR(dev, "can't start %s: %d\n", dev->in_ep->name, err);
  464. goto fail;
  465. }
  466. dev->in_ep->driver_data = dev;
  467. dev->out_ep->desc = &bulk_out_desc;
  468. err = usb_ep_enable(dev->out_ep);
  469. if (err) {
  470. ERROR(dev, "can't start %s: %d\n", dev->out_ep->name, err);
  471. goto fail;
  472. }
  473. dev->out_ep->driver_data = dev;
  474. /* allocate a bunch of read buffers and queue them all at once. */
  475. ep = dev->out_ep;
  476. for (i = 0; i < qlen && err == 0; i++) {
  477. req = alloc_ep_req(ep, buflen);
  478. if (req) {
  479. req->complete = gmidi_complete;
  480. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  481. if (err) {
  482. DBG(dev, "%s queue req: %d\n", ep->name, err);
  483. }
  484. } else {
  485. err = -ENOMEM;
  486. }
  487. }
  488. fail:
  489. /* caller is responsible for cleanup on error */
  490. return err;
  491. }
  492. static void gmidi_reset_config(struct gmidi_device *dev)
  493. {
  494. if (dev->config == 0) {
  495. return;
  496. }
  497. DBG(dev, "reset config\n");
  498. /* just disable endpoints, forcing completion of pending i/o.
  499. * all our completion handlers free their requests in this case.
  500. */
  501. usb_ep_disable(dev->in_ep);
  502. usb_ep_disable(dev->out_ep);
  503. dev->config = 0;
  504. }
  505. /* change our operational config. this code must agree with the code
  506. * that returns config descriptors, and altsetting code.
  507. *
  508. * it's also responsible for power management interactions. some
  509. * configurations might not work with our current power sources.
  510. *
  511. * note that some device controller hardware will constrain what this
  512. * code can do, perhaps by disallowing more than one configuration or
  513. * by limiting configuration choices (like the pxa2xx).
  514. */
  515. static int
  516. gmidi_set_config(struct gmidi_device *dev, unsigned number, gfp_t gfp_flags)
  517. {
  518. int result = 0;
  519. struct usb_gadget *gadget = dev->gadget;
  520. #if 0
  521. /* FIXME */
  522. /* Hacking this bit out fixes a bug where on receipt of two
  523. USB_REQ_SET_CONFIGURATION messages, we end up with no
  524. buffered OUT requests waiting for data. This is clearly
  525. hiding a bug elsewhere, because if the config didn't
  526. change then we really shouldn't do anything. */
  527. /* Having said that, when we do "change" from config 1
  528. to config 1, we at least gmidi_reset_config() which
  529. clears out any requests on endpoints, so it's not like
  530. we leak or anything. */
  531. if (number == dev->config) {
  532. return 0;
  533. }
  534. #endif
  535. gmidi_reset_config(dev);
  536. switch (number) {
  537. case GMIDI_CONFIG:
  538. result = set_gmidi_config(dev, gfp_flags);
  539. break;
  540. default:
  541. result = -EINVAL;
  542. /* FALL THROUGH */
  543. case 0:
  544. return result;
  545. }
  546. if (!result && (!dev->in_ep || !dev->out_ep)) {
  547. result = -ENODEV;
  548. }
  549. if (result) {
  550. gmidi_reset_config(dev);
  551. } else {
  552. dev->config = number;
  553. INFO(dev, "%s speed\n", usb_speed_string(gadget->speed));
  554. }
  555. return result;
  556. }
  557. static void gmidi_setup_complete(struct usb_ep *ep, struct usb_request *req)
  558. {
  559. if (req->status || req->actual != req->length) {
  560. DBG((struct gmidi_device *) ep->driver_data,
  561. "setup complete --> %d, %d/%d\n",
  562. req->status, req->actual, req->length);
  563. }
  564. }
  565. /*
  566. * The setup() callback implements all the ep0 functionality that's
  567. * not handled lower down, in hardware or the hardware driver (like
  568. * device and endpoint feature flags, and their status). It's all
  569. * housekeeping for the gadget function we're implementing. Most of
  570. * the work is in config-specific setup.
  571. */
  572. static int gmidi_setup(struct usb_gadget *gadget,
  573. const struct usb_ctrlrequest *ctrl)
  574. {
  575. struct gmidi_device *dev = get_gadget_data(gadget);
  576. struct usb_request *req = dev->req;
  577. int value = -EOPNOTSUPP;
  578. u16 w_index = le16_to_cpu(ctrl->wIndex);
  579. u16 w_value = le16_to_cpu(ctrl->wValue);
  580. u16 w_length = le16_to_cpu(ctrl->wLength);
  581. /* usually this stores reply data in the pre-allocated ep0 buffer,
  582. * but config change events will reconfigure hardware.
  583. */
  584. req->zero = 0;
  585. switch (ctrl->bRequest) {
  586. case USB_REQ_GET_DESCRIPTOR:
  587. if (ctrl->bRequestType != USB_DIR_IN) {
  588. goto unknown;
  589. }
  590. switch (w_value >> 8) {
  591. case USB_DT_DEVICE:
  592. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  593. value = min(w_length, (u16) sizeof(device_desc));
  594. memcpy(req->buf, &device_desc, value);
  595. break;
  596. case USB_DT_CONFIG:
  597. value = config_buf(gadget, req->buf,
  598. w_value >> 8,
  599. w_value & 0xff);
  600. if (value >= 0) {
  601. value = min(w_length, (u16)value);
  602. }
  603. break;
  604. case USB_DT_STRING:
  605. /* wIndex == language code.
  606. * this driver only handles one language, you can
  607. * add string tables for other languages, using
  608. * any UTF-8 characters
  609. */
  610. value = usb_gadget_get_string(&stringtab,
  611. w_value & 0xff, req->buf);
  612. if (value >= 0) {
  613. value = min(w_length, (u16)value);
  614. }
  615. break;
  616. }
  617. break;
  618. /* currently two configs, two speeds */
  619. case USB_REQ_SET_CONFIGURATION:
  620. if (ctrl->bRequestType != 0) {
  621. goto unknown;
  622. }
  623. if (gadget->a_hnp_support) {
  624. DBG(dev, "HNP available\n");
  625. } else if (gadget->a_alt_hnp_support) {
  626. DBG(dev, "HNP needs a different root port\n");
  627. } else {
  628. VDBG(dev, "HNP inactive\n");
  629. }
  630. spin_lock(&dev->lock);
  631. value = gmidi_set_config(dev, w_value, GFP_ATOMIC);
  632. spin_unlock(&dev->lock);
  633. break;
  634. case USB_REQ_GET_CONFIGURATION:
  635. if (ctrl->bRequestType != USB_DIR_IN) {
  636. goto unknown;
  637. }
  638. *(u8 *)req->buf = dev->config;
  639. value = min(w_length, (u16)1);
  640. break;
  641. /* until we add altsetting support, or other interfaces,
  642. * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  643. * and already killed pending endpoint I/O.
  644. */
  645. case USB_REQ_SET_INTERFACE:
  646. if (ctrl->bRequestType != USB_RECIP_INTERFACE) {
  647. goto unknown;
  648. }
  649. spin_lock(&dev->lock);
  650. if (dev->config && w_index < GMIDI_NUM_INTERFACES
  651. && w_value == 0)
  652. {
  653. u8 config = dev->config;
  654. /* resets interface configuration, forgets about
  655. * previous transaction state (queued bufs, etc)
  656. * and re-inits endpoint state (toggle etc)
  657. * no response queued, just zero status == success.
  658. * if we had more than one interface we couldn't
  659. * use this "reset the config" shortcut.
  660. */
  661. gmidi_reset_config(dev);
  662. gmidi_set_config(dev, config, GFP_ATOMIC);
  663. value = 0;
  664. }
  665. spin_unlock(&dev->lock);
  666. break;
  667. case USB_REQ_GET_INTERFACE:
  668. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) {
  669. goto unknown;
  670. }
  671. if (!dev->config) {
  672. break;
  673. }
  674. if (w_index >= GMIDI_NUM_INTERFACES) {
  675. value = -EDOM;
  676. break;
  677. }
  678. *(u8 *)req->buf = 0;
  679. value = min(w_length, (u16)1);
  680. break;
  681. default:
  682. unknown:
  683. VDBG(dev, "unknown control req%02x.%02x v%04x i%04x l%d\n",
  684. ctrl->bRequestType, ctrl->bRequest,
  685. w_value, w_index, w_length);
  686. }
  687. /* respond with data transfer before status phase? */
  688. if (value >= 0) {
  689. req->length = value;
  690. req->zero = value < w_length;
  691. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  692. if (value < 0) {
  693. DBG(dev, "ep_queue --> %d\n", value);
  694. req->status = 0;
  695. gmidi_setup_complete(gadget->ep0, req);
  696. }
  697. }
  698. /* device either stalls (value < 0) or reports success */
  699. return value;
  700. }
  701. static void gmidi_disconnect(struct usb_gadget *gadget)
  702. {
  703. struct gmidi_device *dev = get_gadget_data(gadget);
  704. unsigned long flags;
  705. spin_lock_irqsave(&dev->lock, flags);
  706. gmidi_reset_config(dev);
  707. /* a more significant application might have some non-usb
  708. * activities to quiesce here, saving resources like power
  709. * or pushing the notification up a network stack.
  710. */
  711. spin_unlock_irqrestore(&dev->lock, flags);
  712. /* next we may get setup() calls to enumerate new connections;
  713. * or an unbind() during shutdown (including removing module).
  714. */
  715. }
  716. static void /* __init_or_exit */ gmidi_unbind(struct usb_gadget *gadget)
  717. {
  718. struct gmidi_device *dev = get_gadget_data(gadget);
  719. struct snd_card *card;
  720. DBG(dev, "unbind\n");
  721. card = dev->card;
  722. dev->card = NULL;
  723. if (card) {
  724. snd_card_free(card);
  725. }
  726. /* we've already been disconnected ... no i/o is active */
  727. if (dev->req) {
  728. dev->req->length = USB_BUFSIZ;
  729. free_ep_req(gadget->ep0, dev->req);
  730. }
  731. kfree(dev);
  732. set_gadget_data(gadget, NULL);
  733. }
  734. static int gmidi_snd_free(struct snd_device *device)
  735. {
  736. return 0;
  737. }
  738. static void gmidi_transmit_packet(struct usb_request *req, uint8_t p0,
  739. uint8_t p1, uint8_t p2, uint8_t p3)
  740. {
  741. unsigned length = req->length;
  742. u8 *buf = (u8 *)req->buf + length;
  743. buf[0] = p0;
  744. buf[1] = p1;
  745. buf[2] = p2;
  746. buf[3] = p3;
  747. req->length = length + 4;
  748. }
  749. /*
  750. * Converts MIDI commands to USB MIDI packets.
  751. */
  752. static void gmidi_transmit_byte(struct usb_request *req,
  753. struct gmidi_in_port *port, uint8_t b)
  754. {
  755. uint8_t p0 = port->cable;
  756. if (b >= 0xf8) {
  757. gmidi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
  758. } else if (b >= 0xf0) {
  759. switch (b) {
  760. case 0xf0:
  761. port->data[0] = b;
  762. port->state = STATE_SYSEX_1;
  763. break;
  764. case 0xf1:
  765. case 0xf3:
  766. port->data[0] = b;
  767. port->state = STATE_1PARAM;
  768. break;
  769. case 0xf2:
  770. port->data[0] = b;
  771. port->state = STATE_2PARAM_1;
  772. break;
  773. case 0xf4:
  774. case 0xf5:
  775. port->state = STATE_UNKNOWN;
  776. break;
  777. case 0xf6:
  778. gmidi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
  779. port->state = STATE_UNKNOWN;
  780. break;
  781. case 0xf7:
  782. switch (port->state) {
  783. case STATE_SYSEX_0:
  784. gmidi_transmit_packet(req,
  785. p0 | 0x05, 0xf7, 0, 0);
  786. break;
  787. case STATE_SYSEX_1:
  788. gmidi_transmit_packet(req,
  789. p0 | 0x06, port->data[0], 0xf7, 0);
  790. break;
  791. case STATE_SYSEX_2:
  792. gmidi_transmit_packet(req,
  793. p0 | 0x07, port->data[0],
  794. port->data[1], 0xf7);
  795. break;
  796. }
  797. port->state = STATE_UNKNOWN;
  798. break;
  799. }
  800. } else if (b >= 0x80) {
  801. port->data[0] = b;
  802. if (b >= 0xc0 && b <= 0xdf)
  803. port->state = STATE_1PARAM;
  804. else
  805. port->state = STATE_2PARAM_1;
  806. } else { /* b < 0x80 */
  807. switch (port->state) {
  808. case STATE_1PARAM:
  809. if (port->data[0] < 0xf0) {
  810. p0 |= port->data[0] >> 4;
  811. } else {
  812. p0 |= 0x02;
  813. port->state = STATE_UNKNOWN;
  814. }
  815. gmidi_transmit_packet(req, p0, port->data[0], b, 0);
  816. break;
  817. case STATE_2PARAM_1:
  818. port->data[1] = b;
  819. port->state = STATE_2PARAM_2;
  820. break;
  821. case STATE_2PARAM_2:
  822. if (port->data[0] < 0xf0) {
  823. p0 |= port->data[0] >> 4;
  824. port->state = STATE_2PARAM_1;
  825. } else {
  826. p0 |= 0x03;
  827. port->state = STATE_UNKNOWN;
  828. }
  829. gmidi_transmit_packet(req,
  830. p0, port->data[0], port->data[1], b);
  831. break;
  832. case STATE_SYSEX_0:
  833. port->data[0] = b;
  834. port->state = STATE_SYSEX_1;
  835. break;
  836. case STATE_SYSEX_1:
  837. port->data[1] = b;
  838. port->state = STATE_SYSEX_2;
  839. break;
  840. case STATE_SYSEX_2:
  841. gmidi_transmit_packet(req,
  842. p0 | 0x04, port->data[0], port->data[1], b);
  843. port->state = STATE_SYSEX_0;
  844. break;
  845. }
  846. }
  847. }
  848. static void gmidi_transmit(struct gmidi_device *dev, struct usb_request *req)
  849. {
  850. struct usb_ep *ep = dev->in_ep;
  851. struct gmidi_in_port *port = &dev->in_port;
  852. if (!ep) {
  853. return;
  854. }
  855. if (!req) {
  856. req = alloc_ep_req(ep, buflen);
  857. }
  858. if (!req) {
  859. ERROR(dev, "gmidi_transmit: alloc_ep_request failed\n");
  860. return;
  861. }
  862. req->length = 0;
  863. req->complete = gmidi_complete;
  864. if (port->active) {
  865. while (req->length + 3 < buflen) {
  866. uint8_t b;
  867. if (snd_rawmidi_transmit(dev->in_substream, &b, 1)
  868. != 1)
  869. {
  870. port->active = 0;
  871. break;
  872. }
  873. gmidi_transmit_byte(req, port, b);
  874. }
  875. }
  876. if (req->length > 0) {
  877. usb_ep_queue(ep, req, GFP_ATOMIC);
  878. } else {
  879. free_ep_req(ep, req);
  880. }
  881. }
  882. static void gmidi_in_tasklet(unsigned long data)
  883. {
  884. struct gmidi_device *dev = (struct gmidi_device *)data;
  885. gmidi_transmit(dev, NULL);
  886. }
  887. static int gmidi_in_open(struct snd_rawmidi_substream *substream)
  888. {
  889. struct gmidi_device *dev = substream->rmidi->private_data;
  890. VDBG(dev, "gmidi_in_open\n");
  891. dev->in_substream = substream;
  892. dev->in_port.state = STATE_UNKNOWN;
  893. return 0;
  894. }
  895. static int gmidi_in_close(struct snd_rawmidi_substream *substream)
  896. {
  897. struct gmidi_device *dev = substream->rmidi->private_data;
  898. VDBG(dev, "gmidi_in_close\n");
  899. return 0;
  900. }
  901. static void gmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
  902. {
  903. struct gmidi_device *dev = substream->rmidi->private_data;
  904. VDBG(dev, "gmidi_in_trigger %d\n", up);
  905. dev->in_port.active = up;
  906. if (up) {
  907. tasklet_hi_schedule(&dev->tasklet);
  908. }
  909. }
  910. static int gmidi_out_open(struct snd_rawmidi_substream *substream)
  911. {
  912. struct gmidi_device *dev = substream->rmidi->private_data;
  913. VDBG(dev, "gmidi_out_open\n");
  914. dev->out_substream = substream;
  915. return 0;
  916. }
  917. static int gmidi_out_close(struct snd_rawmidi_substream *substream)
  918. {
  919. struct gmidi_device *dev = substream->rmidi->private_data;
  920. VDBG(dev, "gmidi_out_close\n");
  921. return 0;
  922. }
  923. static void gmidi_out_trigger(struct snd_rawmidi_substream *substream, int up)
  924. {
  925. struct gmidi_device *dev = substream->rmidi->private_data;
  926. VDBG(dev, "gmidi_out_trigger %d\n", up);
  927. if (up) {
  928. set_bit(substream->number, &dev->out_triggered);
  929. } else {
  930. clear_bit(substream->number, &dev->out_triggered);
  931. }
  932. }
  933. static struct snd_rawmidi_ops gmidi_in_ops = {
  934. .open = gmidi_in_open,
  935. .close = gmidi_in_close,
  936. .trigger = gmidi_in_trigger,
  937. };
  938. static struct snd_rawmidi_ops gmidi_out_ops = {
  939. .open = gmidi_out_open,
  940. .close = gmidi_out_close,
  941. .trigger = gmidi_out_trigger
  942. };
  943. /* register as a sound "card" */
  944. static int gmidi_register_card(struct gmidi_device *dev)
  945. {
  946. struct snd_card *card;
  947. struct snd_rawmidi *rmidi;
  948. int err;
  949. int out_ports = 1;
  950. int in_ports = 1;
  951. static struct snd_device_ops ops = {
  952. .dev_free = gmidi_snd_free,
  953. };
  954. err = snd_card_create(index, id, THIS_MODULE, 0, &card);
  955. if (err < 0) {
  956. ERROR(dev, "snd_card_create failed\n");
  957. goto fail;
  958. }
  959. dev->card = card;
  960. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, dev, &ops);
  961. if (err < 0) {
  962. ERROR(dev, "snd_device_new failed: error %d\n", err);
  963. goto fail;
  964. }
  965. strcpy(card->driver, longname);
  966. strcpy(card->longname, longname);
  967. strcpy(card->shortname, shortname);
  968. /* Set up rawmidi */
  969. dev->in_port.dev = dev;
  970. dev->in_port.active = 0;
  971. snd_component_add(card, "MIDI");
  972. err = snd_rawmidi_new(card, "USB MIDI Gadget", 0,
  973. out_ports, in_ports, &rmidi);
  974. if (err < 0) {
  975. ERROR(dev, "snd_rawmidi_new failed: error %d\n", err);
  976. goto fail;
  977. }
  978. dev->rmidi = rmidi;
  979. strcpy(rmidi->name, card->shortname);
  980. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  981. SNDRV_RAWMIDI_INFO_INPUT |
  982. SNDRV_RAWMIDI_INFO_DUPLEX;
  983. rmidi->private_data = dev;
  984. /* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
  985. It's an upside-down world being a gadget. */
  986. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
  987. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
  988. snd_card_set_dev(card, &dev->gadget->dev);
  989. /* register it - we're ready to go */
  990. err = snd_card_register(card);
  991. if (err < 0) {
  992. ERROR(dev, "snd_card_register failed\n");
  993. goto fail;
  994. }
  995. VDBG(dev, "gmidi_register_card finished ok\n");
  996. return 0;
  997. fail:
  998. if (dev->card) {
  999. snd_card_free(dev->card);
  1000. dev->card = NULL;
  1001. }
  1002. return err;
  1003. }
  1004. /*
  1005. * Creates an output endpoint, and initializes output ports.
  1006. */
  1007. static int __init gmidi_bind(struct usb_gadget *gadget)
  1008. {
  1009. struct gmidi_device *dev;
  1010. struct usb_ep *in_ep, *out_ep;
  1011. int gcnum, err = 0;
  1012. /* support optional vendor/distro customization */
  1013. if (idVendor) {
  1014. if (!idProduct) {
  1015. pr_err("idVendor needs idProduct!\n");
  1016. return -ENODEV;
  1017. }
  1018. device_desc.idVendor = cpu_to_le16(idVendor);
  1019. device_desc.idProduct = cpu_to_le16(idProduct);
  1020. if (bcdDevice) {
  1021. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1022. }
  1023. }
  1024. if (iManufacturer) {
  1025. strlcpy(manufacturer, iManufacturer, sizeof(manufacturer));
  1026. } else {
  1027. snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
  1028. init_utsname()->sysname, init_utsname()->release,
  1029. gadget->name);
  1030. }
  1031. if (iProduct) {
  1032. strlcpy(product_desc, iProduct, sizeof(product_desc));
  1033. }
  1034. if (iSerialNumber) {
  1035. device_desc.iSerialNumber = STRING_SERIAL,
  1036. strlcpy(serial_number, iSerialNumber, sizeof(serial_number));
  1037. }
  1038. /* Bulk-only drivers like this one SHOULD be able to
  1039. * autoconfigure on any sane usb controller driver,
  1040. * but there may also be important quirks to address.
  1041. */
  1042. usb_ep_autoconfig_reset(gadget);
  1043. in_ep = usb_ep_autoconfig(gadget, &bulk_in_desc);
  1044. if (!in_ep) {
  1045. autoconf_fail:
  1046. pr_err("%s: can't autoconfigure on %s\n",
  1047. shortname, gadget->name);
  1048. return -ENODEV;
  1049. }
  1050. EP_IN_NAME = in_ep->name;
  1051. in_ep->driver_data = in_ep; /* claim */
  1052. out_ep = usb_ep_autoconfig(gadget, &bulk_out_desc);
  1053. if (!out_ep) {
  1054. goto autoconf_fail;
  1055. }
  1056. EP_OUT_NAME = out_ep->name;
  1057. out_ep->driver_data = out_ep; /* claim */
  1058. gcnum = usb_gadget_controller_number(gadget);
  1059. if (gcnum >= 0) {
  1060. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  1061. } else {
  1062. /* gmidi is so simple (no altsettings) that
  1063. * it SHOULD NOT have problems with bulk-capable hardware.
  1064. * so warn about unrecognized controllers, don't panic.
  1065. */
  1066. pr_warning("%s: controller '%s' not recognized\n",
  1067. shortname, gadget->name);
  1068. device_desc.bcdDevice = cpu_to_le16(0x9999);
  1069. }
  1070. /* ok, we made sense of the hardware ... */
  1071. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1072. if (!dev) {
  1073. return -ENOMEM;
  1074. }
  1075. spin_lock_init(&dev->lock);
  1076. dev->gadget = gadget;
  1077. dev->in_ep = in_ep;
  1078. dev->out_ep = out_ep;
  1079. set_gadget_data(gadget, dev);
  1080. tasklet_init(&dev->tasklet, gmidi_in_tasklet, (unsigned long)dev);
  1081. /* preallocate control response and buffer */
  1082. dev->req = alloc_ep_req(gadget->ep0, USB_BUFSIZ);
  1083. if (!dev->req) {
  1084. err = -ENOMEM;
  1085. goto fail;
  1086. }
  1087. dev->req->complete = gmidi_setup_complete;
  1088. gadget->ep0->driver_data = dev;
  1089. INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
  1090. INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
  1091. EP_OUT_NAME, EP_IN_NAME);
  1092. /* register as an ALSA sound card */
  1093. err = gmidi_register_card(dev);
  1094. if (err < 0) {
  1095. goto fail;
  1096. }
  1097. VDBG(dev, "gmidi_bind finished ok\n");
  1098. return 0;
  1099. fail:
  1100. gmidi_unbind(gadget);
  1101. return err;
  1102. }
  1103. static void gmidi_suspend(struct usb_gadget *gadget)
  1104. {
  1105. struct gmidi_device *dev = get_gadget_data(gadget);
  1106. if (gadget->speed == USB_SPEED_UNKNOWN) {
  1107. return;
  1108. }
  1109. DBG(dev, "suspend\n");
  1110. }
  1111. static void gmidi_resume(struct usb_gadget *gadget)
  1112. {
  1113. struct gmidi_device *dev = get_gadget_data(gadget);
  1114. DBG(dev, "resume\n");
  1115. }
  1116. static struct usb_gadget_driver gmidi_driver = {
  1117. .speed = USB_SPEED_FULL,
  1118. .function = (char *)longname,
  1119. .unbind = gmidi_unbind,
  1120. .setup = gmidi_setup,
  1121. .disconnect = gmidi_disconnect,
  1122. .suspend = gmidi_suspend,
  1123. .resume = gmidi_resume,
  1124. .driver = {
  1125. .name = (char *)shortname,
  1126. .owner = THIS_MODULE,
  1127. },
  1128. };
  1129. static int __init gmidi_init(void)
  1130. {
  1131. return usb_gadget_probe_driver(&gmidi_driver, gmidi_bind);
  1132. }
  1133. module_init(gmidi_init);
  1134. static void __exit gmidi_cleanup(void)
  1135. {
  1136. usb_gadget_unregister_driver(&gmidi_driver);
  1137. }
  1138. module_exit(gmidi_cleanup);