gmidi.c 33 KB

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