gmidi.c 33 KB

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