gmidi.c 33 KB

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