gmidi.c 33 KB

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