gmidi.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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 = cpu_to_le16(0x0200),
  160. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  161. .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
  162. .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
  163. .iManufacturer = STRING_MANUFACTURER,
  164. .iProduct = STRING_PRODUCT,
  165. .bNumConfigurations = 1,
  166. };
  167. /* B.2 Configuration Descriptor */
  168. static struct usb_config_descriptor config_desc = {
  169. .bLength = USB_DT_CONFIG_SIZE,
  170. .bDescriptorType = USB_DT_CONFIG,
  171. /* compute wTotalLength on the fly */
  172. .bNumInterfaces = GMIDI_NUM_INTERFACES,
  173. .bConfigurationValue = GMIDI_CONFIG,
  174. .iConfiguration = STRING_MIDI_GADGET,
  175. /*
  176. * FIXME: When embedding this driver in a device,
  177. * these need to be set to reflect the actual
  178. * power properties of the device. Is it selfpowered?
  179. */
  180. .bmAttributes = USB_CONFIG_ATT_ONE,
  181. .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  182. };
  183. /* B.3.1 Standard AC Interface Descriptor */
  184. static const struct usb_interface_descriptor ac_interface_desc = {
  185. .bLength = USB_DT_INTERFACE_SIZE,
  186. .bDescriptorType = USB_DT_INTERFACE,
  187. .bInterfaceNumber = GMIDI_AC_INTERFACE,
  188. .bNumEndpoints = 0,
  189. .bInterfaceClass = USB_CLASS_AUDIO,
  190. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  191. .iInterface = STRING_MIDI_GADGET,
  192. };
  193. /* B.3.2 Class-Specific AC Interface Descriptor */
  194. static const struct 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 = cpu_to_le16(0x0100),
  199. .wTotalLength = 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 = cpu_to_le16(0x0100),
  221. .wTotalLength = cpu_to_le16(USB_DT_MS_HEADER_SIZE
  222. + 2*USB_DT_MIDI_IN_SIZE
  223. + 2*USB_DT_MIDI_OUT_SIZE(1)),
  224. };
  225. #define JACK_IN_EMB 1
  226. #define JACK_IN_EXT 2
  227. #define JACK_OUT_EMB 3
  228. #define JACK_OUT_EXT 4
  229. /* B.4.3 MIDI IN Jack Descriptors */
  230. static const struct usb_midi_in_jack_descriptor jack_in_emb_desc = {
  231. .bLength = USB_DT_MIDI_IN_SIZE,
  232. .bDescriptorType = USB_DT_CS_INTERFACE,
  233. .bDescriptorSubtype = USB_MS_MIDI_IN_JACK,
  234. .bJackType = USB_MS_EMBEDDED,
  235. .bJackID = JACK_IN_EMB,
  236. };
  237. static const struct usb_midi_in_jack_descriptor jack_in_ext_desc = {
  238. .bLength = USB_DT_MIDI_IN_SIZE,
  239. .bDescriptorType = USB_DT_CS_INTERFACE,
  240. .bDescriptorSubtype = USB_MS_MIDI_IN_JACK,
  241. .bJackType = USB_MS_EXTERNAL,
  242. .bJackID = JACK_IN_EXT,
  243. };
  244. /* B.4.4 MIDI OUT Jack Descriptors */
  245. static const struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc = {
  246. .bLength = USB_DT_MIDI_OUT_SIZE(1),
  247. .bDescriptorType = USB_DT_CS_INTERFACE,
  248. .bDescriptorSubtype = USB_MS_MIDI_OUT_JACK,
  249. .bJackType = USB_MS_EMBEDDED,
  250. .bJackID = JACK_OUT_EMB,
  251. .bNrInputPins = 1,
  252. .pins = {
  253. [0] = {
  254. .baSourceID = JACK_IN_EXT,
  255. .baSourcePin = 1,
  256. }
  257. }
  258. };
  259. static const struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc = {
  260. .bLength = USB_DT_MIDI_OUT_SIZE(1),
  261. .bDescriptorType = USB_DT_CS_INTERFACE,
  262. .bDescriptorSubtype = USB_MS_MIDI_OUT_JACK,
  263. .bJackType = USB_MS_EXTERNAL,
  264. .bJackID = JACK_OUT_EXT,
  265. .bNrInputPins = 1,
  266. .pins = {
  267. [0] = {
  268. .baSourceID = JACK_IN_EMB,
  269. .baSourcePin = 1,
  270. }
  271. }
  272. };
  273. /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
  274. static struct usb_endpoint_descriptor bulk_out_desc = {
  275. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  276. .bDescriptorType = USB_DT_ENDPOINT,
  277. .bEndpointAddress = USB_DIR_OUT,
  278. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  279. };
  280. /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
  281. static const struct usb_ms_endpoint_descriptor_1 ms_out_desc = {
  282. .bLength = USB_DT_MS_ENDPOINT_SIZE(1),
  283. .bDescriptorType = USB_DT_CS_ENDPOINT,
  284. .bDescriptorSubtype = USB_MS_GENERAL,
  285. .bNumEmbMIDIJack = 1,
  286. .baAssocJackID = {
  287. [0] = JACK_IN_EMB,
  288. }
  289. };
  290. /* B.6.1 Standard Bulk IN Endpoint Descriptor */
  291. static struct usb_endpoint_descriptor bulk_in_desc = {
  292. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  293. .bDescriptorType = USB_DT_ENDPOINT,
  294. .bEndpointAddress = USB_DIR_IN,
  295. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  296. };
  297. /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
  298. static const struct usb_ms_endpoint_descriptor_1 ms_in_desc = {
  299. .bLength = USB_DT_MS_ENDPOINT_SIZE(1),
  300. .bDescriptorType = USB_DT_CS_ENDPOINT,
  301. .bDescriptorSubtype = USB_MS_GENERAL,
  302. .bNumEmbMIDIJack = 1,
  303. .baAssocJackID = {
  304. [0] = JACK_OUT_EMB,
  305. }
  306. };
  307. static const struct usb_descriptor_header *gmidi_function [] = {
  308. (struct usb_descriptor_header *)&ac_interface_desc,
  309. (struct usb_descriptor_header *)&ac_header_desc,
  310. (struct usb_descriptor_header *)&ms_interface_desc,
  311. (struct usb_descriptor_header *)&ms_header_desc,
  312. (struct usb_descriptor_header *)&jack_in_emb_desc,
  313. (struct usb_descriptor_header *)&jack_in_ext_desc,
  314. (struct usb_descriptor_header *)&jack_out_emb_desc,
  315. (struct usb_descriptor_header *)&jack_out_ext_desc,
  316. /* If you add more jacks, update ms_header_desc.wTotalLength */
  317. (struct usb_descriptor_header *)&bulk_out_desc,
  318. (struct usb_descriptor_header *)&ms_out_desc,
  319. (struct usb_descriptor_header *)&bulk_in_desc,
  320. (struct usb_descriptor_header *)&ms_in_desc,
  321. NULL,
  322. };
  323. static char manufacturer[50];
  324. static char product_desc[40] = "MIDI Gadget";
  325. static char serial_number[20];
  326. /* static strings, in UTF-8 */
  327. static struct usb_string strings [] = {
  328. { STRING_MANUFACTURER, manufacturer, },
  329. { STRING_PRODUCT, product_desc, },
  330. { STRING_SERIAL, serial_number, },
  331. { STRING_MIDI_GADGET, longname, },
  332. { } /* end of list */
  333. };
  334. static struct usb_gadget_strings stringtab = {
  335. .language = 0x0409, /* en-us */
  336. .strings = strings,
  337. };
  338. static int config_buf(struct usb_gadget *gadget,
  339. u8 *buf, u8 type, unsigned index)
  340. {
  341. int len;
  342. /* only one configuration */
  343. if (index != 0) {
  344. return -EINVAL;
  345. }
  346. len = usb_gadget_config_buf(&config_desc,
  347. buf, USB_BUFSIZ, gmidi_function);
  348. if (len < 0) {
  349. return len;
  350. }
  351. ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
  352. return len;
  353. }
  354. static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
  355. {
  356. struct usb_request *req;
  357. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  358. if (req) {
  359. req->length = length;
  360. req->buf = kmalloc(length, GFP_ATOMIC);
  361. if (!req->buf) {
  362. usb_ep_free_request(ep, req);
  363. req = NULL;
  364. }
  365. }
  366. return req;
  367. }
  368. static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  369. {
  370. kfree(req->buf);
  371. usb_ep_free_request(ep, req);
  372. }
  373. static const uint8_t gmidi_cin_length[] = {
  374. 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
  375. };
  376. /*
  377. * Receives a chunk of MIDI data.
  378. */
  379. static void gmidi_read_data(struct usb_ep *ep, int cable,
  380. uint8_t *data, int length)
  381. {
  382. struct gmidi_device *dev = ep->driver_data;
  383. /* cable is ignored, because for now we only have one. */
  384. if (!dev->out_substream) {
  385. /* Nobody is listening - throw it on the floor. */
  386. return;
  387. }
  388. if (!test_bit(dev->out_substream->number, &dev->out_triggered)) {
  389. return;
  390. }
  391. snd_rawmidi_receive(dev->out_substream, data, length);
  392. }
  393. static void gmidi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
  394. {
  395. unsigned i;
  396. u8 *buf = req->buf;
  397. for (i = 0; i + 3 < req->actual; i += 4) {
  398. if (buf[i] != 0) {
  399. int cable = buf[i] >> 4;
  400. int length = gmidi_cin_length[buf[i] & 0x0f];
  401. gmidi_read_data(ep, cable, &buf[i + 1], length);
  402. }
  403. }
  404. }
  405. static void gmidi_complete(struct usb_ep *ep, struct usb_request *req)
  406. {
  407. struct gmidi_device *dev = ep->driver_data;
  408. int status = req->status;
  409. switch (status) {
  410. case 0: /* normal completion */
  411. if (ep == dev->out_ep) {
  412. /* we received stuff.
  413. req is queued again, below */
  414. gmidi_handle_out_data(ep, req);
  415. } else if (ep == dev->in_ep) {
  416. /* our transmit completed.
  417. see if there's more to go.
  418. gmidi_transmit eats req, don't queue it again. */
  419. gmidi_transmit(dev, req);
  420. return;
  421. }
  422. break;
  423. /* this endpoint is normally active while we're configured */
  424. case -ECONNABORTED: /* hardware forced ep reset */
  425. case -ECONNRESET: /* request dequeued */
  426. case -ESHUTDOWN: /* disconnect from host */
  427. VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
  428. req->actual, req->length);
  429. if (ep == dev->out_ep) {
  430. gmidi_handle_out_data(ep, req);
  431. }
  432. free_ep_req(ep, req);
  433. return;
  434. case -EOVERFLOW: /* buffer overrun on read means that
  435. * we didn't provide a big enough
  436. * buffer.
  437. */
  438. default:
  439. DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
  440. status, req->actual, req->length);
  441. break;
  442. case -EREMOTEIO: /* short read */
  443. break;
  444. }
  445. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  446. if (status) {
  447. ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
  448. ep->name, req->length, status);
  449. usb_ep_set_halt(ep);
  450. /* FIXME recover later ... somehow */
  451. }
  452. }
  453. static int set_gmidi_config(struct gmidi_device *dev, gfp_t gfp_flags)
  454. {
  455. int err = 0;
  456. struct usb_request *req;
  457. struct usb_ep *ep;
  458. unsigned i;
  459. err = usb_ep_enable(dev->in_ep, &bulk_in_desc);
  460. if (err) {
  461. ERROR(dev, "can't start %s: %d\n", dev->in_ep->name, err);
  462. goto fail;
  463. }
  464. dev->in_ep->driver_data = dev;
  465. err = usb_ep_enable(dev->out_ep, &bulk_out_desc);
  466. if (err) {
  467. ERROR(dev, "can't start %s: %d\n", dev->out_ep->name, err);
  468. goto fail;
  469. }
  470. dev->out_ep->driver_data = dev;
  471. /* allocate a bunch of read buffers and queue them all at once. */
  472. ep = dev->out_ep;
  473. for (i = 0; i < qlen && err == 0; i++) {
  474. req = alloc_ep_req(ep, buflen);
  475. if (req) {
  476. req->complete = gmidi_complete;
  477. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  478. if (err) {
  479. DBG(dev, "%s queue req: %d\n", ep->name, err);
  480. }
  481. } else {
  482. err = -ENOMEM;
  483. }
  484. }
  485. fail:
  486. /* caller is responsible for cleanup on error */
  487. return err;
  488. }
  489. static void gmidi_reset_config(struct gmidi_device *dev)
  490. {
  491. if (dev->config == 0) {
  492. return;
  493. }
  494. DBG(dev, "reset config\n");
  495. /* just disable endpoints, forcing completion of pending i/o.
  496. * all our completion handlers free their requests in this case.
  497. */
  498. usb_ep_disable(dev->in_ep);
  499. usb_ep_disable(dev->out_ep);
  500. dev->config = 0;
  501. }
  502. /* change our operational config. this code must agree with the code
  503. * that returns config descriptors, and altsetting code.
  504. *
  505. * it's also responsible for power management interactions. some
  506. * configurations might not work with our current power sources.
  507. *
  508. * note that some device controller hardware will constrain what this
  509. * code can do, perhaps by disallowing more than one configuration or
  510. * by limiting configuration choices (like the pxa2xx).
  511. */
  512. static int
  513. gmidi_set_config(struct gmidi_device *dev, unsigned number, gfp_t gfp_flags)
  514. {
  515. int result = 0;
  516. struct usb_gadget *gadget = dev->gadget;
  517. #if 0
  518. /* FIXME */
  519. /* Hacking this bit out fixes a bug where on receipt of two
  520. USB_REQ_SET_CONFIGURATION messages, we end up with no
  521. buffered OUT requests waiting for data. This is clearly
  522. hiding a bug elsewhere, because if the config didn't
  523. change then we really shouldn't do anything. */
  524. /* Having said that, when we do "change" from config 1
  525. to config 1, we at least gmidi_reset_config() which
  526. clears out any requests on endpoints, so it's not like
  527. we leak or anything. */
  528. if (number == dev->config) {
  529. return 0;
  530. }
  531. #endif
  532. 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. err = snd_card_create(index, id, THIS_MODULE, 0, &card);
  963. if (err < 0) {
  964. ERROR(dev, "snd_card_create failed\n");
  965. goto fail;
  966. }
  967. dev->card = card;
  968. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, dev, &ops);
  969. if (err < 0) {
  970. ERROR(dev, "snd_device_new failed: error %d\n", err);
  971. goto fail;
  972. }
  973. strcpy(card->driver, longname);
  974. strcpy(card->longname, longname);
  975. strcpy(card->shortname, shortname);
  976. /* Set up rawmidi */
  977. dev->in_port.dev = dev;
  978. dev->in_port.active = 0;
  979. snd_component_add(card, "MIDI");
  980. err = snd_rawmidi_new(card, "USB MIDI Gadget", 0,
  981. out_ports, in_ports, &rmidi);
  982. if (err < 0) {
  983. ERROR(dev, "snd_rawmidi_new failed: error %d\n", err);
  984. goto fail;
  985. }
  986. dev->rmidi = rmidi;
  987. strcpy(rmidi->name, card->shortname);
  988. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  989. SNDRV_RAWMIDI_INFO_INPUT |
  990. SNDRV_RAWMIDI_INFO_DUPLEX;
  991. rmidi->private_data = dev;
  992. /* Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
  993. It's an upside-down world being a gadget. */
  994. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
  995. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
  996. snd_card_set_dev(card, &dev->gadget->dev);
  997. /* register it - we're ready to go */
  998. err = snd_card_register(card);
  999. if (err < 0) {
  1000. ERROR(dev, "snd_card_register failed\n");
  1001. goto fail;
  1002. }
  1003. VDBG(dev, "gmidi_register_card finished ok\n");
  1004. return 0;
  1005. fail:
  1006. if (dev->card) {
  1007. snd_card_free(dev->card);
  1008. dev->card = NULL;
  1009. }
  1010. return err;
  1011. }
  1012. /*
  1013. * Creates an output endpoint, and initializes output ports.
  1014. */
  1015. static int __init gmidi_bind(struct usb_gadget *gadget)
  1016. {
  1017. struct gmidi_device *dev;
  1018. struct usb_ep *in_ep, *out_ep;
  1019. int gcnum, err = 0;
  1020. /* support optional vendor/distro customization */
  1021. if (idVendor) {
  1022. if (!idProduct) {
  1023. pr_err("idVendor needs idProduct!\n");
  1024. return -ENODEV;
  1025. }
  1026. device_desc.idVendor = cpu_to_le16(idVendor);
  1027. device_desc.idProduct = cpu_to_le16(idProduct);
  1028. if (bcdDevice) {
  1029. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1030. }
  1031. }
  1032. if (iManufacturer) {
  1033. strlcpy(manufacturer, iManufacturer, sizeof(manufacturer));
  1034. } else {
  1035. snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
  1036. init_utsname()->sysname, init_utsname()->release,
  1037. gadget->name);
  1038. }
  1039. if (iProduct) {
  1040. strlcpy(product_desc, iProduct, sizeof(product_desc));
  1041. }
  1042. if (iSerialNumber) {
  1043. device_desc.iSerialNumber = STRING_SERIAL,
  1044. strlcpy(serial_number, iSerialNumber, sizeof(serial_number));
  1045. }
  1046. /* Bulk-only drivers like this one SHOULD be able to
  1047. * autoconfigure on any sane usb controller driver,
  1048. * but there may also be important quirks to address.
  1049. */
  1050. usb_ep_autoconfig_reset(gadget);
  1051. in_ep = usb_ep_autoconfig(gadget, &bulk_in_desc);
  1052. if (!in_ep) {
  1053. autoconf_fail:
  1054. pr_err("%s: can't autoconfigure on %s\n",
  1055. shortname, gadget->name);
  1056. return -ENODEV;
  1057. }
  1058. EP_IN_NAME = in_ep->name;
  1059. in_ep->driver_data = in_ep; /* claim */
  1060. out_ep = usb_ep_autoconfig(gadget, &bulk_out_desc);
  1061. if (!out_ep) {
  1062. goto autoconf_fail;
  1063. }
  1064. EP_OUT_NAME = out_ep->name;
  1065. out_ep->driver_data = out_ep; /* claim */
  1066. gcnum = usb_gadget_controller_number(gadget);
  1067. if (gcnum >= 0) {
  1068. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  1069. } else {
  1070. /* gmidi is so simple (no altsettings) that
  1071. * it SHOULD NOT have problems with bulk-capable hardware.
  1072. * so warn about unrecognized controllers, don't panic.
  1073. */
  1074. pr_warning("%s: controller '%s' not recognized\n",
  1075. shortname, gadget->name);
  1076. device_desc.bcdDevice = cpu_to_le16(0x9999);
  1077. }
  1078. /* ok, we made sense of the hardware ... */
  1079. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1080. if (!dev) {
  1081. return -ENOMEM;
  1082. }
  1083. spin_lock_init(&dev->lock);
  1084. dev->gadget = gadget;
  1085. dev->in_ep = in_ep;
  1086. dev->out_ep = out_ep;
  1087. set_gadget_data(gadget, dev);
  1088. tasklet_init(&dev->tasklet, gmidi_in_tasklet, (unsigned long)dev);
  1089. /* preallocate control response and buffer */
  1090. dev->req = alloc_ep_req(gadget->ep0, USB_BUFSIZ);
  1091. if (!dev->req) {
  1092. err = -ENOMEM;
  1093. goto fail;
  1094. }
  1095. dev->req->complete = gmidi_setup_complete;
  1096. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1097. gadget->ep0->driver_data = dev;
  1098. INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
  1099. INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
  1100. EP_OUT_NAME, EP_IN_NAME);
  1101. /* register as an ALSA sound card */
  1102. err = gmidi_register_card(dev);
  1103. if (err < 0) {
  1104. goto fail;
  1105. }
  1106. VDBG(dev, "gmidi_bind finished ok\n");
  1107. return 0;
  1108. fail:
  1109. gmidi_unbind(gadget);
  1110. return err;
  1111. }
  1112. static void gmidi_suspend(struct usb_gadget *gadget)
  1113. {
  1114. struct gmidi_device *dev = get_gadget_data(gadget);
  1115. if (gadget->speed == USB_SPEED_UNKNOWN) {
  1116. return;
  1117. }
  1118. DBG(dev, "suspend\n");
  1119. }
  1120. static void gmidi_resume(struct usb_gadget *gadget)
  1121. {
  1122. struct gmidi_device *dev = get_gadget_data(gadget);
  1123. DBG(dev, "resume\n");
  1124. }
  1125. static struct usb_gadget_driver gmidi_driver = {
  1126. .speed = USB_SPEED_FULL,
  1127. .function = (char *)longname,
  1128. .bind = gmidi_bind,
  1129. .unbind = gmidi_unbind,
  1130. .setup = gmidi_setup,
  1131. .disconnect = gmidi_disconnect,
  1132. .suspend = gmidi_suspend,
  1133. .resume = gmidi_resume,
  1134. .driver = {
  1135. .name = (char *)shortname,
  1136. .owner = THIS_MODULE,
  1137. },
  1138. };
  1139. static int __init gmidi_init(void)
  1140. {
  1141. return usb_gadget_register_driver(&gmidi_driver);
  1142. }
  1143. module_init(gmidi_init);
  1144. static void __exit gmidi_cleanup(void)
  1145. {
  1146. usb_gadget_unregister_driver(&gmidi_driver);
  1147. }
  1148. module_exit(gmidi_cleanup);