gmidi.c 33 KB

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