f_midi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /*
  2. * f_midi.c -- USB MIDI class function 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. * Rewritten for the composite framework
  9. * Copyright (C) 2011 Daniel Mack <zonque@gmail.com>
  10. *
  11. * Based on drivers/usb/gadget/f_audio.c,
  12. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  13. * Copyright (C) 2008 Analog Devices, Inc
  14. *
  15. * and drivers/usb/gadget/midi.c,
  16. * Copyright (C) 2006 Thumtronics Pty Ltd.
  17. * Ben Williamson <ben.williamson@greyinnovation.com>
  18. *
  19. * Licensed under the GPL-2 or later.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/utsname.h>
  24. #include <linux/device.h>
  25. #include <sound/core.h>
  26. #include <sound/initval.h>
  27. #include <sound/rawmidi.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include <linux/usb/audio.h>
  31. #include <linux/usb/midi.h>
  32. MODULE_AUTHOR("Ben Williamson");
  33. MODULE_LICENSE("GPL v2");
  34. static const char f_midi_shortname[] = "f_midi";
  35. static const char f_midi_longname[] = "MIDI Gadget";
  36. /*
  37. * We can only handle 16 cables on one single endpoint, as cable numbers are
  38. * stored in 4-bit fields. And as the interface currently only holds one
  39. * single endpoint, this is the maximum number of ports we can allow.
  40. */
  41. #define MAX_PORTS 16
  42. /*
  43. * This is a gadget, and the IN/OUT naming is from the host's perspective.
  44. * USB -> OUT endpoint -> rawmidi
  45. * USB <- IN endpoint <- rawmidi
  46. */
  47. struct gmidi_in_port {
  48. struct f_midi *midi;
  49. int active;
  50. uint8_t cable;
  51. uint8_t state;
  52. #define STATE_UNKNOWN 0
  53. #define STATE_1PARAM 1
  54. #define STATE_2PARAM_1 2
  55. #define STATE_2PARAM_2 3
  56. #define STATE_SYSEX_0 4
  57. #define STATE_SYSEX_1 5
  58. #define STATE_SYSEX_2 6
  59. uint8_t data[2];
  60. };
  61. struct f_midi {
  62. struct usb_function func;
  63. struct usb_gadget *gadget;
  64. struct usb_ep *in_ep, *out_ep;
  65. struct snd_card *card;
  66. struct snd_rawmidi *rmidi;
  67. struct snd_rawmidi_substream *in_substream[MAX_PORTS];
  68. struct snd_rawmidi_substream *out_substream[MAX_PORTS];
  69. struct gmidi_in_port *in_port[MAX_PORTS];
  70. unsigned long out_triggered;
  71. struct tasklet_struct tasklet;
  72. unsigned int in_ports;
  73. unsigned int out_ports;
  74. int index;
  75. char *id;
  76. unsigned int buflen, qlen;
  77. };
  78. static inline struct f_midi *func_to_midi(struct usb_function *f)
  79. {
  80. return container_of(f, struct f_midi, func);
  81. }
  82. static void f_midi_transmit(struct f_midi *midi, struct usb_request *req);
  83. DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
  84. DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
  85. DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
  86. /* B.3.1 Standard AC Interface Descriptor */
  87. static struct usb_interface_descriptor ac_interface_desc __initdata = {
  88. .bLength = USB_DT_INTERFACE_SIZE,
  89. .bDescriptorType = USB_DT_INTERFACE,
  90. /* .bInterfaceNumber = DYNAMIC */
  91. /* .bNumEndpoints = DYNAMIC */
  92. .bInterfaceClass = USB_CLASS_AUDIO,
  93. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  94. /* .iInterface = DYNAMIC */
  95. };
  96. /* B.3.2 Class-Specific AC Interface Descriptor */
  97. static struct uac1_ac_header_descriptor_1 ac_header_desc __initdata = {
  98. .bLength = UAC_DT_AC_HEADER_SIZE(1),
  99. .bDescriptorType = USB_DT_CS_INTERFACE,
  100. .bDescriptorSubtype = USB_MS_HEADER,
  101. .bcdADC = cpu_to_le16(0x0100),
  102. .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
  103. .bInCollection = 1,
  104. /* .baInterfaceNr = DYNAMIC */
  105. };
  106. /* B.4.1 Standard MS Interface Descriptor */
  107. static struct usb_interface_descriptor ms_interface_desc __initdata = {
  108. .bLength = USB_DT_INTERFACE_SIZE,
  109. .bDescriptorType = USB_DT_INTERFACE,
  110. /* .bInterfaceNumber = DYNAMIC */
  111. .bNumEndpoints = 2,
  112. .bInterfaceClass = USB_CLASS_AUDIO,
  113. .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
  114. /* .iInterface = DYNAMIC */
  115. };
  116. /* B.4.2 Class-Specific MS Interface Descriptor */
  117. static struct usb_ms_header_descriptor ms_header_desc __initdata = {
  118. .bLength = USB_DT_MS_HEADER_SIZE,
  119. .bDescriptorType = USB_DT_CS_INTERFACE,
  120. .bDescriptorSubtype = USB_MS_HEADER,
  121. .bcdMSC = cpu_to_le16(0x0100),
  122. /* .wTotalLength = DYNAMIC */
  123. };
  124. /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
  125. static struct usb_endpoint_descriptor bulk_out_desc = {
  126. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  127. .bDescriptorType = USB_DT_ENDPOINT,
  128. .bEndpointAddress = USB_DIR_OUT,
  129. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  130. };
  131. /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
  132. static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
  133. /* .bLength = DYNAMIC */
  134. .bDescriptorType = USB_DT_CS_ENDPOINT,
  135. .bDescriptorSubtype = USB_MS_GENERAL,
  136. /* .bNumEmbMIDIJack = DYNAMIC */
  137. /* .baAssocJackID = DYNAMIC */
  138. };
  139. /* B.6.1 Standard Bulk IN Endpoint Descriptor */
  140. static struct usb_endpoint_descriptor bulk_in_desc = {
  141. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  142. .bDescriptorType = USB_DT_ENDPOINT,
  143. .bEndpointAddress = USB_DIR_IN,
  144. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  145. };
  146. /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
  147. static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
  148. /* .bLength = DYNAMIC */
  149. .bDescriptorType = USB_DT_CS_ENDPOINT,
  150. .bDescriptorSubtype = USB_MS_GENERAL,
  151. /* .bNumEmbMIDIJack = DYNAMIC */
  152. /* .baAssocJackID = DYNAMIC */
  153. };
  154. /* string IDs are assigned dynamically */
  155. #define STRING_FUNC_IDX 0
  156. static struct usb_string midi_string_defs[] = {
  157. [STRING_FUNC_IDX].s = "MIDI function",
  158. { } /* end of list */
  159. };
  160. static struct usb_gadget_strings midi_stringtab = {
  161. .language = 0x0409, /* en-us */
  162. .strings = midi_string_defs,
  163. };
  164. static struct usb_gadget_strings *midi_strings[] = {
  165. &midi_stringtab,
  166. NULL,
  167. };
  168. static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
  169. {
  170. struct usb_request *req;
  171. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  172. if (req) {
  173. req->length = length;
  174. req->buf = kmalloc(length, GFP_ATOMIC);
  175. if (!req->buf) {
  176. usb_ep_free_request(ep, req);
  177. req = NULL;
  178. }
  179. }
  180. return req;
  181. }
  182. static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  183. {
  184. kfree(req->buf);
  185. usb_ep_free_request(ep, req);
  186. }
  187. static const uint8_t f_midi_cin_length[] = {
  188. 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
  189. };
  190. /*
  191. * Receives a chunk of MIDI data.
  192. */
  193. static void f_midi_read_data(struct usb_ep *ep, int cable,
  194. uint8_t *data, int length)
  195. {
  196. struct f_midi *midi = ep->driver_data;
  197. struct snd_rawmidi_substream *substream = midi->out_substream[cable];
  198. if (!substream)
  199. /* Nobody is listening - throw it on the floor. */
  200. return;
  201. if (!test_bit(cable, &midi->out_triggered))
  202. return;
  203. snd_rawmidi_receive(substream, data, length);
  204. }
  205. static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
  206. {
  207. unsigned int i;
  208. u8 *buf = req->buf;
  209. for (i = 0; i + 3 < req->actual; i += 4)
  210. if (buf[i] != 0) {
  211. int cable = buf[i] >> 4;
  212. int length = f_midi_cin_length[buf[i] & 0x0f];
  213. f_midi_read_data(ep, cable, &buf[i + 1], length);
  214. }
  215. }
  216. static void
  217. f_midi_complete(struct usb_ep *ep, struct usb_request *req)
  218. {
  219. struct f_midi *midi = ep->driver_data;
  220. struct usb_composite_dev *cdev = midi->func.config->cdev;
  221. int status = req->status;
  222. switch (status) {
  223. case 0: /* normal completion */
  224. if (ep == midi->out_ep) {
  225. /* We received stuff. req is queued again, below */
  226. f_midi_handle_out_data(ep, req);
  227. } else if (ep == midi->in_ep) {
  228. /* Our transmit completed. See if there's more to go.
  229. * f_midi_transmit eats req, don't queue it again. */
  230. f_midi_transmit(midi, req);
  231. return;
  232. }
  233. break;
  234. /* this endpoint is normally active while we're configured */
  235. case -ECONNABORTED: /* hardware forced ep reset */
  236. case -ECONNRESET: /* request dequeued */
  237. case -ESHUTDOWN: /* disconnect from host */
  238. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  239. req->actual, req->length);
  240. if (ep == midi->out_ep)
  241. f_midi_handle_out_data(ep, req);
  242. free_ep_req(ep, req);
  243. return;
  244. case -EOVERFLOW: /* buffer overrun on read means that
  245. * we didn't provide a big enough buffer.
  246. */
  247. default:
  248. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  249. status, req->actual, req->length);
  250. break;
  251. case -EREMOTEIO: /* short read */
  252. break;
  253. }
  254. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  255. if (status) {
  256. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  257. ep->name, req->length, status);
  258. usb_ep_set_halt(ep);
  259. /* FIXME recover later ... somehow */
  260. }
  261. }
  262. static int f_midi_start_ep(struct f_midi *midi,
  263. struct usb_function *f,
  264. struct usb_ep *ep)
  265. {
  266. int err;
  267. struct usb_composite_dev *cdev = f->config->cdev;
  268. if (ep->driver_data)
  269. usb_ep_disable(ep);
  270. err = config_ep_by_speed(midi->gadget, f, ep);
  271. if (err) {
  272. ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
  273. return err;
  274. }
  275. err = usb_ep_enable(ep);
  276. if (err) {
  277. ERROR(cdev, "can't start %s: %d\n", ep->name, err);
  278. return err;
  279. }
  280. ep->driver_data = midi;
  281. return 0;
  282. }
  283. static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  284. {
  285. struct f_midi *midi = func_to_midi(f);
  286. struct usb_composite_dev *cdev = f->config->cdev;
  287. unsigned i;
  288. int err;
  289. err = f_midi_start_ep(midi, f, midi->in_ep);
  290. if (err)
  291. return err;
  292. err = f_midi_start_ep(midi, f, midi->out_ep);
  293. if (err)
  294. return err;
  295. if (midi->out_ep->driver_data)
  296. usb_ep_disable(midi->out_ep);
  297. err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
  298. if (err) {
  299. ERROR(cdev, "can't configure %s: %d\n",
  300. midi->out_ep->name, err);
  301. return err;
  302. }
  303. err = usb_ep_enable(midi->out_ep);
  304. if (err) {
  305. ERROR(cdev, "can't start %s: %d\n",
  306. midi->out_ep->name, err);
  307. return err;
  308. }
  309. midi->out_ep->driver_data = midi;
  310. /* allocate a bunch of read buffers and queue them all at once. */
  311. for (i = 0; i < midi->qlen && err == 0; i++) {
  312. struct usb_request *req =
  313. alloc_ep_req(midi->out_ep, midi->buflen);
  314. if (req == NULL)
  315. return -ENOMEM;
  316. req->complete = f_midi_complete;
  317. err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
  318. if (err) {
  319. ERROR(midi, "%s queue req: %d\n",
  320. midi->out_ep->name, err);
  321. }
  322. }
  323. return 0;
  324. }
  325. static void f_midi_disable(struct usb_function *f)
  326. {
  327. struct f_midi *midi = func_to_midi(f);
  328. struct usb_composite_dev *cdev = f->config->cdev;
  329. DBG(cdev, "disable\n");
  330. /*
  331. * just disable endpoints, forcing completion of pending i/o.
  332. * all our completion handlers free their requests in this case.
  333. */
  334. usb_ep_disable(midi->in_ep);
  335. usb_ep_disable(midi->out_ep);
  336. }
  337. static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
  338. {
  339. struct usb_composite_dev *cdev = f->config->cdev;
  340. struct f_midi *midi = func_to_midi(f);
  341. struct snd_card *card;
  342. DBG(cdev, "unbind\n");
  343. /* just to be sure */
  344. f_midi_disable(f);
  345. card = midi->card;
  346. midi->card = NULL;
  347. if (card)
  348. snd_card_free(card);
  349. kfree(midi->id);
  350. midi->id = NULL;
  351. usb_free_descriptors(f->descriptors);
  352. kfree(midi);
  353. }
  354. static int f_midi_snd_free(struct snd_device *device)
  355. {
  356. return 0;
  357. }
  358. static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
  359. uint8_t p1, uint8_t p2, uint8_t p3)
  360. {
  361. unsigned length = req->length;
  362. u8 *buf = (u8 *)req->buf + length;
  363. buf[0] = p0;
  364. buf[1] = p1;
  365. buf[2] = p2;
  366. buf[3] = p3;
  367. req->length = length + 4;
  368. }
  369. /*
  370. * Converts MIDI commands to USB MIDI packets.
  371. */
  372. static void f_midi_transmit_byte(struct usb_request *req,
  373. struct gmidi_in_port *port, uint8_t b)
  374. {
  375. uint8_t p0 = port->cable << 4;
  376. if (b >= 0xf8) {
  377. f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
  378. } else if (b >= 0xf0) {
  379. switch (b) {
  380. case 0xf0:
  381. port->data[0] = b;
  382. port->state = STATE_SYSEX_1;
  383. break;
  384. case 0xf1:
  385. case 0xf3:
  386. port->data[0] = b;
  387. port->state = STATE_1PARAM;
  388. break;
  389. case 0xf2:
  390. port->data[0] = b;
  391. port->state = STATE_2PARAM_1;
  392. break;
  393. case 0xf4:
  394. case 0xf5:
  395. port->state = STATE_UNKNOWN;
  396. break;
  397. case 0xf6:
  398. f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
  399. port->state = STATE_UNKNOWN;
  400. break;
  401. case 0xf7:
  402. switch (port->state) {
  403. case STATE_SYSEX_0:
  404. f_midi_transmit_packet(req,
  405. p0 | 0x05, 0xf7, 0, 0);
  406. break;
  407. case STATE_SYSEX_1:
  408. f_midi_transmit_packet(req,
  409. p0 | 0x06, port->data[0], 0xf7, 0);
  410. break;
  411. case STATE_SYSEX_2:
  412. f_midi_transmit_packet(req,
  413. p0 | 0x07, port->data[0],
  414. port->data[1], 0xf7);
  415. break;
  416. }
  417. port->state = STATE_UNKNOWN;
  418. break;
  419. }
  420. } else if (b >= 0x80) {
  421. port->data[0] = b;
  422. if (b >= 0xc0 && b <= 0xdf)
  423. port->state = STATE_1PARAM;
  424. else
  425. port->state = STATE_2PARAM_1;
  426. } else { /* b < 0x80 */
  427. switch (port->state) {
  428. case STATE_1PARAM:
  429. if (port->data[0] < 0xf0) {
  430. p0 |= port->data[0] >> 4;
  431. } else {
  432. p0 |= 0x02;
  433. port->state = STATE_UNKNOWN;
  434. }
  435. f_midi_transmit_packet(req, p0, port->data[0], b, 0);
  436. break;
  437. case STATE_2PARAM_1:
  438. port->data[1] = b;
  439. port->state = STATE_2PARAM_2;
  440. break;
  441. case STATE_2PARAM_2:
  442. if (port->data[0] < 0xf0) {
  443. p0 |= port->data[0] >> 4;
  444. port->state = STATE_2PARAM_1;
  445. } else {
  446. p0 |= 0x03;
  447. port->state = STATE_UNKNOWN;
  448. }
  449. f_midi_transmit_packet(req,
  450. p0, port->data[0], port->data[1], b);
  451. break;
  452. case STATE_SYSEX_0:
  453. port->data[0] = b;
  454. port->state = STATE_SYSEX_1;
  455. break;
  456. case STATE_SYSEX_1:
  457. port->data[1] = b;
  458. port->state = STATE_SYSEX_2;
  459. break;
  460. case STATE_SYSEX_2:
  461. f_midi_transmit_packet(req,
  462. p0 | 0x04, port->data[0], port->data[1], b);
  463. port->state = STATE_SYSEX_0;
  464. break;
  465. }
  466. }
  467. }
  468. static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
  469. {
  470. struct usb_ep *ep = midi->in_ep;
  471. int i;
  472. if (!ep)
  473. return;
  474. if (!req)
  475. req = alloc_ep_req(ep, midi->buflen);
  476. if (!req) {
  477. ERROR(midi, "gmidi_transmit: alloc_ep_request failed\n");
  478. return;
  479. }
  480. req->length = 0;
  481. req->complete = f_midi_complete;
  482. for (i = 0; i < MAX_PORTS; i++) {
  483. struct gmidi_in_port *port = midi->in_port[i];
  484. struct snd_rawmidi_substream *substream = midi->in_substream[i];
  485. if (!port || !port->active || !substream)
  486. continue;
  487. while (req->length + 3 < midi->buflen) {
  488. uint8_t b;
  489. if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
  490. port->active = 0;
  491. break;
  492. }
  493. f_midi_transmit_byte(req, port, b);
  494. }
  495. }
  496. if (req->length > 0)
  497. usb_ep_queue(ep, req, GFP_ATOMIC);
  498. else
  499. free_ep_req(ep, req);
  500. }
  501. static void f_midi_in_tasklet(unsigned long data)
  502. {
  503. struct f_midi *midi = (struct f_midi *) data;
  504. f_midi_transmit(midi, NULL);
  505. }
  506. static int f_midi_in_open(struct snd_rawmidi_substream *substream)
  507. {
  508. struct f_midi *midi = substream->rmidi->private_data;
  509. if (!midi->in_port[substream->number])
  510. return -EINVAL;
  511. VDBG(midi, "%s()\n", __func__);
  512. midi->in_substream[substream->number] = substream;
  513. midi->in_port[substream->number]->state = STATE_UNKNOWN;
  514. return 0;
  515. }
  516. static int f_midi_in_close(struct snd_rawmidi_substream *substream)
  517. {
  518. struct f_midi *midi = substream->rmidi->private_data;
  519. VDBG(midi, "%s()\n", __func__);
  520. return 0;
  521. }
  522. static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
  523. {
  524. struct f_midi *midi = substream->rmidi->private_data;
  525. if (!midi->in_port[substream->number])
  526. return;
  527. VDBG(midi, "%s() %d\n", __func__, up);
  528. midi->in_port[substream->number]->active = up;
  529. if (up)
  530. tasklet_hi_schedule(&midi->tasklet);
  531. }
  532. static int f_midi_out_open(struct snd_rawmidi_substream *substream)
  533. {
  534. struct f_midi *midi = substream->rmidi->private_data;
  535. if (substream->number >= MAX_PORTS)
  536. return -EINVAL;
  537. VDBG(midi, "%s()\n", __func__);
  538. midi->out_substream[substream->number] = substream;
  539. return 0;
  540. }
  541. static int f_midi_out_close(struct snd_rawmidi_substream *substream)
  542. {
  543. struct f_midi *midi = substream->rmidi->private_data;
  544. VDBG(midi, "%s()\n", __func__);
  545. return 0;
  546. }
  547. static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
  548. {
  549. struct f_midi *midi = substream->rmidi->private_data;
  550. VDBG(midi, "%s()\n", __func__);
  551. if (up)
  552. set_bit(substream->number, &midi->out_triggered);
  553. else
  554. clear_bit(substream->number, &midi->out_triggered);
  555. }
  556. static struct snd_rawmidi_ops gmidi_in_ops = {
  557. .open = f_midi_in_open,
  558. .close = f_midi_in_close,
  559. .trigger = f_midi_in_trigger,
  560. };
  561. static struct snd_rawmidi_ops gmidi_out_ops = {
  562. .open = f_midi_out_open,
  563. .close = f_midi_out_close,
  564. .trigger = f_midi_out_trigger
  565. };
  566. /* register as a sound "card" */
  567. static int f_midi_register_card(struct f_midi *midi)
  568. {
  569. struct snd_card *card;
  570. struct snd_rawmidi *rmidi;
  571. int err;
  572. static struct snd_device_ops ops = {
  573. .dev_free = f_midi_snd_free,
  574. };
  575. err = snd_card_create(midi->index, midi->id, THIS_MODULE, 0, &card);
  576. if (err < 0) {
  577. ERROR(midi, "snd_card_create() failed\n");
  578. goto fail;
  579. }
  580. midi->card = card;
  581. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
  582. if (err < 0) {
  583. ERROR(midi, "snd_device_new() failed: error %d\n", err);
  584. goto fail;
  585. }
  586. strcpy(card->driver, f_midi_longname);
  587. strcpy(card->longname, f_midi_longname);
  588. strcpy(card->shortname, f_midi_shortname);
  589. /* Set up rawmidi */
  590. snd_component_add(card, "MIDI");
  591. err = snd_rawmidi_new(card, card->longname, 0,
  592. midi->out_ports, midi->in_ports, &rmidi);
  593. if (err < 0) {
  594. ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
  595. goto fail;
  596. }
  597. midi->rmidi = rmidi;
  598. strcpy(rmidi->name, card->shortname);
  599. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  600. SNDRV_RAWMIDI_INFO_INPUT |
  601. SNDRV_RAWMIDI_INFO_DUPLEX;
  602. rmidi->private_data = midi;
  603. /*
  604. * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
  605. * It's an upside-down world being a gadget.
  606. */
  607. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
  608. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
  609. snd_card_set_dev(card, &midi->gadget->dev);
  610. /* register it - we're ready to go */
  611. err = snd_card_register(card);
  612. if (err < 0) {
  613. ERROR(midi, "snd_card_register() failed\n");
  614. goto fail;
  615. }
  616. VDBG(midi, "%s() finished ok\n", __func__);
  617. return 0;
  618. fail:
  619. if (midi->card) {
  620. snd_card_free(midi->card);
  621. midi->card = NULL;
  622. }
  623. return err;
  624. }
  625. /* MIDI function driver setup/binding */
  626. static int __init
  627. f_midi_bind(struct usb_configuration *c, struct usb_function *f)
  628. {
  629. struct usb_descriptor_header **midi_function;
  630. struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
  631. struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
  632. struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
  633. struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
  634. struct usb_composite_dev *cdev = c->cdev;
  635. struct f_midi *midi = func_to_midi(f);
  636. int status, n, jack = 1, i = 0;
  637. /* maybe allocate device-global string ID */
  638. if (midi_string_defs[0].id == 0) {
  639. status = usb_string_id(c->cdev);
  640. if (status < 0)
  641. goto fail;
  642. midi_string_defs[0].id = status;
  643. }
  644. /* We have two interfaces, AudioControl and MIDIStreaming */
  645. status = usb_interface_id(c, f);
  646. if (status < 0)
  647. goto fail;
  648. ac_interface_desc.bInterfaceNumber = status;
  649. status = usb_interface_id(c, f);
  650. if (status < 0)
  651. goto fail;
  652. ms_interface_desc.bInterfaceNumber = status;
  653. ac_header_desc.baInterfaceNr[0] = status;
  654. status = -ENODEV;
  655. /* allocate instance-specific endpoints */
  656. midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
  657. if (!midi->in_ep)
  658. goto fail;
  659. midi->in_ep->driver_data = cdev; /* claim */
  660. midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
  661. if (!midi->out_ep)
  662. goto fail;
  663. midi->out_ep->driver_data = cdev; /* claim */
  664. /* allocate temporary function list */
  665. midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(midi_function),
  666. GFP_KERNEL);
  667. if (!midi_function) {
  668. status = -ENOMEM;
  669. goto fail;
  670. }
  671. /*
  672. * construct the function's descriptor set. As the number of
  673. * input and output MIDI ports is configurable, we have to do
  674. * it that way.
  675. */
  676. /* add the headers - these are always the same */
  677. midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
  678. midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
  679. midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
  680. /* calculate the header's wTotalLength */
  681. n = USB_DT_MS_HEADER_SIZE
  682. + (midi->in_ports + midi->out_ports) *
  683. (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
  684. ms_header_desc.wTotalLength = cpu_to_le16(n);
  685. midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
  686. /* configure the external IN jacks, each linked to an embedded OUT jack */
  687. for (n = 0; n < midi->in_ports; n++) {
  688. struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
  689. struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
  690. in_ext->bLength = USB_DT_MIDI_IN_SIZE;
  691. in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  692. in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  693. in_ext->bJackType = USB_MS_EXTERNAL;
  694. in_ext->bJackID = jack++;
  695. in_ext->iJack = 0;
  696. midi_function[i++] = (struct usb_descriptor_header *) in_ext;
  697. out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
  698. out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  699. out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  700. out_emb->bJackType = USB_MS_EMBEDDED;
  701. out_emb->bJackID = jack++;
  702. out_emb->bNrInputPins = 1;
  703. out_emb->pins[0].baSourcePin = 1;
  704. out_emb->pins[0].baSourceID = in_ext->bJackID;
  705. out_emb->iJack = 0;
  706. midi_function[i++] = (struct usb_descriptor_header *) out_emb;
  707. /* link it to the endpoint */
  708. ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
  709. }
  710. /* configure the external OUT jacks, each linked to an embedded IN jack */
  711. for (n = 0; n < midi->out_ports; n++) {
  712. struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
  713. struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
  714. in_emb->bLength = USB_DT_MIDI_IN_SIZE;
  715. in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  716. in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  717. in_emb->bJackType = USB_MS_EMBEDDED;
  718. in_emb->bJackID = jack++;
  719. in_emb->iJack = 0;
  720. midi_function[i++] = (struct usb_descriptor_header *) in_emb;
  721. out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
  722. out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  723. out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  724. out_ext->bJackType = USB_MS_EXTERNAL;
  725. out_ext->bJackID = jack++;
  726. out_ext->bNrInputPins = 1;
  727. out_ext->iJack = 0;
  728. out_ext->pins[0].baSourceID = in_emb->bJackID;
  729. out_ext->pins[0].baSourcePin = 1;
  730. midi_function[i++] = (struct usb_descriptor_header *) out_ext;
  731. /* link it to the endpoint */
  732. ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
  733. }
  734. /* configure the endpoint descriptors ... */
  735. ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
  736. ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
  737. ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
  738. ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
  739. /* ... and add them to the list */
  740. midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
  741. midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
  742. midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
  743. midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
  744. midi_function[i++] = NULL;
  745. /*
  746. * support all relevant hardware speeds... we expect that when
  747. * hardware is dual speed, all bulk-capable endpoints work at
  748. * both speeds
  749. */
  750. /* copy descriptors, and track endpoint copies */
  751. if (gadget_is_dualspeed(c->cdev->gadget)) {
  752. c->highspeed = true;
  753. bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
  754. bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
  755. f->hs_descriptors = usb_copy_descriptors(midi_function);
  756. } else {
  757. f->descriptors = usb_copy_descriptors(midi_function);
  758. }
  759. kfree(midi_function);
  760. return 0;
  761. fail:
  762. /* we might as well release our claims on endpoints */
  763. if (midi->out_ep)
  764. midi->out_ep->driver_data = NULL;
  765. if (midi->in_ep)
  766. midi->in_ep->driver_data = NULL;
  767. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  768. return status;
  769. }
  770. /**
  771. * f_midi_bind_config - add USB MIDI function to a configuration
  772. * @c: the configuration to supcard the USB audio function
  773. * @index: the soundcard index to use for the ALSA device creation
  774. * @id: the soundcard id to use for the ALSA device creation
  775. * @buflen: the buffer length to use
  776. * @qlen the number of read requests to pre-allocate
  777. * Context: single threaded during gadget setup
  778. *
  779. * Returns zero on success, else negative errno.
  780. */
  781. int __init f_midi_bind_config(struct usb_configuration *c,
  782. int index, char *id,
  783. unsigned int in_ports,
  784. unsigned int out_ports,
  785. unsigned int buflen,
  786. unsigned int qlen)
  787. {
  788. struct f_midi *midi;
  789. int status, i;
  790. /* sanity check */
  791. if (in_ports > MAX_PORTS || out_ports > MAX_PORTS)
  792. return -EINVAL;
  793. /* allocate and initialize one new instance */
  794. midi = kzalloc(sizeof *midi, GFP_KERNEL);
  795. if (!midi) {
  796. status = -ENOMEM;
  797. goto fail;
  798. }
  799. for (i = 0; i < in_ports; i++) {
  800. struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
  801. if (!port) {
  802. status = -ENOMEM;
  803. goto setup_fail;
  804. }
  805. port->midi = midi;
  806. port->active = 0;
  807. port->cable = i;
  808. midi->in_port[i] = port;
  809. }
  810. midi->gadget = c->cdev->gadget;
  811. tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
  812. /* set up ALSA midi devices */
  813. midi->in_ports = in_ports;
  814. midi->out_ports = out_ports;
  815. status = f_midi_register_card(midi);
  816. if (status < 0)
  817. goto setup_fail;
  818. midi->func.name = "gmidi function";
  819. midi->func.strings = midi_strings;
  820. midi->func.bind = f_midi_bind;
  821. midi->func.unbind = f_midi_unbind;
  822. midi->func.set_alt = f_midi_set_alt;
  823. midi->func.disable = f_midi_disable;
  824. midi->id = kstrdup(id, GFP_KERNEL);
  825. midi->index = index;
  826. midi->buflen = buflen;
  827. midi->qlen = qlen;
  828. status = usb_add_function(c, &midi->func);
  829. if (status)
  830. goto setup_fail;
  831. return 0;
  832. setup_fail:
  833. for (--i; i >= 0; i--)
  834. kfree(midi->in_port[i]);
  835. kfree(midi);
  836. fail:
  837. return status;
  838. }