gmidi.c 33 KB

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