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