gmidi.c 33 KB

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