f_midi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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/device.h>
  24. #include <sound/core.h>
  25. #include <sound/initval.h>
  26. #include <sound/rawmidi.h>
  27. #include <linux/usb/ch9.h>
  28. #include <linux/usb/gadget.h>
  29. #include <linux/usb/audio.h>
  30. #include <linux/usb/midi.h>
  31. #include "u_f.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 inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
  169. unsigned length)
  170. {
  171. return alloc_ep_req(ep, length, length);
  172. }
  173. static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  174. {
  175. kfree(req->buf);
  176. usb_ep_free_request(ep, req);
  177. }
  178. static const uint8_t f_midi_cin_length[] = {
  179. 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
  180. };
  181. /*
  182. * Receives a chunk of MIDI data.
  183. */
  184. static void f_midi_read_data(struct usb_ep *ep, int cable,
  185. uint8_t *data, int length)
  186. {
  187. struct f_midi *midi = ep->driver_data;
  188. struct snd_rawmidi_substream *substream = midi->out_substream[cable];
  189. if (!substream)
  190. /* Nobody is listening - throw it on the floor. */
  191. return;
  192. if (!test_bit(cable, &midi->out_triggered))
  193. return;
  194. snd_rawmidi_receive(substream, data, length);
  195. }
  196. static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
  197. {
  198. unsigned int i;
  199. u8 *buf = req->buf;
  200. for (i = 0; i + 3 < req->actual; i += 4)
  201. if (buf[i] != 0) {
  202. int cable = buf[i] >> 4;
  203. int length = f_midi_cin_length[buf[i] & 0x0f];
  204. f_midi_read_data(ep, cable, &buf[i + 1], length);
  205. }
  206. }
  207. static void
  208. f_midi_complete(struct usb_ep *ep, struct usb_request *req)
  209. {
  210. struct f_midi *midi = ep->driver_data;
  211. struct usb_composite_dev *cdev = midi->func.config->cdev;
  212. int status = req->status;
  213. switch (status) {
  214. case 0: /* normal completion */
  215. if (ep == midi->out_ep) {
  216. /* We received stuff. req is queued again, below */
  217. f_midi_handle_out_data(ep, req);
  218. } else if (ep == midi->in_ep) {
  219. /* Our transmit completed. See if there's more to go.
  220. * f_midi_transmit eats req, don't queue it again. */
  221. f_midi_transmit(midi, req);
  222. return;
  223. }
  224. break;
  225. /* this endpoint is normally active while we're configured */
  226. case -ECONNABORTED: /* hardware forced ep reset */
  227. case -ECONNRESET: /* request dequeued */
  228. case -ESHUTDOWN: /* disconnect from host */
  229. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  230. req->actual, req->length);
  231. if (ep == midi->out_ep)
  232. f_midi_handle_out_data(ep, req);
  233. free_ep_req(ep, req);
  234. return;
  235. case -EOVERFLOW: /* buffer overrun on read means that
  236. * we didn't provide a big enough buffer.
  237. */
  238. default:
  239. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  240. status, req->actual, req->length);
  241. break;
  242. case -EREMOTEIO: /* short read */
  243. break;
  244. }
  245. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  246. if (status) {
  247. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  248. ep->name, req->length, status);
  249. usb_ep_set_halt(ep);
  250. /* FIXME recover later ... somehow */
  251. }
  252. }
  253. static int f_midi_start_ep(struct f_midi *midi,
  254. struct usb_function *f,
  255. struct usb_ep *ep)
  256. {
  257. int err;
  258. struct usb_composite_dev *cdev = f->config->cdev;
  259. if (ep->driver_data)
  260. usb_ep_disable(ep);
  261. err = config_ep_by_speed(midi->gadget, f, ep);
  262. if (err) {
  263. ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
  264. return err;
  265. }
  266. err = usb_ep_enable(ep);
  267. if (err) {
  268. ERROR(cdev, "can't start %s: %d\n", ep->name, err);
  269. return err;
  270. }
  271. ep->driver_data = midi;
  272. return 0;
  273. }
  274. static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  275. {
  276. struct f_midi *midi = func_to_midi(f);
  277. struct usb_composite_dev *cdev = f->config->cdev;
  278. unsigned i;
  279. int err;
  280. err = f_midi_start_ep(midi, f, midi->in_ep);
  281. if (err)
  282. return err;
  283. err = f_midi_start_ep(midi, f, midi->out_ep);
  284. if (err)
  285. return err;
  286. if (midi->out_ep->driver_data)
  287. usb_ep_disable(midi->out_ep);
  288. err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
  289. if (err) {
  290. ERROR(cdev, "can't configure %s: %d\n",
  291. midi->out_ep->name, err);
  292. return err;
  293. }
  294. err = usb_ep_enable(midi->out_ep);
  295. if (err) {
  296. ERROR(cdev, "can't start %s: %d\n",
  297. midi->out_ep->name, err);
  298. return err;
  299. }
  300. midi->out_ep->driver_data = midi;
  301. /* allocate a bunch of read buffers and queue them all at once. */
  302. for (i = 0; i < midi->qlen && err == 0; i++) {
  303. struct usb_request *req =
  304. midi_alloc_ep_req(midi->out_ep, midi->buflen);
  305. if (req == NULL)
  306. return -ENOMEM;
  307. req->complete = f_midi_complete;
  308. err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
  309. if (err) {
  310. ERROR(midi, "%s queue req: %d\n",
  311. midi->out_ep->name, err);
  312. }
  313. }
  314. return 0;
  315. }
  316. static void f_midi_disable(struct usb_function *f)
  317. {
  318. struct f_midi *midi = func_to_midi(f);
  319. struct usb_composite_dev *cdev = f->config->cdev;
  320. DBG(cdev, "disable\n");
  321. /*
  322. * just disable endpoints, forcing completion of pending i/o.
  323. * all our completion handlers free their requests in this case.
  324. */
  325. usb_ep_disable(midi->in_ep);
  326. usb_ep_disable(midi->out_ep);
  327. }
  328. static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
  329. {
  330. struct usb_composite_dev *cdev = f->config->cdev;
  331. struct f_midi *midi = func_to_midi(f);
  332. struct snd_card *card;
  333. DBG(cdev, "unbind\n");
  334. /* just to be sure */
  335. f_midi_disable(f);
  336. card = midi->card;
  337. midi->card = NULL;
  338. if (card)
  339. snd_card_free(card);
  340. kfree(midi->id);
  341. midi->id = NULL;
  342. usb_free_all_descriptors(f);
  343. kfree(midi);
  344. }
  345. static int f_midi_snd_free(struct snd_device *device)
  346. {
  347. return 0;
  348. }
  349. static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
  350. uint8_t p1, uint8_t p2, uint8_t p3)
  351. {
  352. unsigned length = req->length;
  353. u8 *buf = (u8 *)req->buf + length;
  354. buf[0] = p0;
  355. buf[1] = p1;
  356. buf[2] = p2;
  357. buf[3] = p3;
  358. req->length = length + 4;
  359. }
  360. /*
  361. * Converts MIDI commands to USB MIDI packets.
  362. */
  363. static void f_midi_transmit_byte(struct usb_request *req,
  364. struct gmidi_in_port *port, uint8_t b)
  365. {
  366. uint8_t p0 = port->cable << 4;
  367. if (b >= 0xf8) {
  368. f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
  369. } else if (b >= 0xf0) {
  370. switch (b) {
  371. case 0xf0:
  372. port->data[0] = b;
  373. port->state = STATE_SYSEX_1;
  374. break;
  375. case 0xf1:
  376. case 0xf3:
  377. port->data[0] = b;
  378. port->state = STATE_1PARAM;
  379. break;
  380. case 0xf2:
  381. port->data[0] = b;
  382. port->state = STATE_2PARAM_1;
  383. break;
  384. case 0xf4:
  385. case 0xf5:
  386. port->state = STATE_UNKNOWN;
  387. break;
  388. case 0xf6:
  389. f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
  390. port->state = STATE_UNKNOWN;
  391. break;
  392. case 0xf7:
  393. switch (port->state) {
  394. case STATE_SYSEX_0:
  395. f_midi_transmit_packet(req,
  396. p0 | 0x05, 0xf7, 0, 0);
  397. break;
  398. case STATE_SYSEX_1:
  399. f_midi_transmit_packet(req,
  400. p0 | 0x06, port->data[0], 0xf7, 0);
  401. break;
  402. case STATE_SYSEX_2:
  403. f_midi_transmit_packet(req,
  404. p0 | 0x07, port->data[0],
  405. port->data[1], 0xf7);
  406. break;
  407. }
  408. port->state = STATE_UNKNOWN;
  409. break;
  410. }
  411. } else if (b >= 0x80) {
  412. port->data[0] = b;
  413. if (b >= 0xc0 && b <= 0xdf)
  414. port->state = STATE_1PARAM;
  415. else
  416. port->state = STATE_2PARAM_1;
  417. } else { /* b < 0x80 */
  418. switch (port->state) {
  419. case STATE_1PARAM:
  420. if (port->data[0] < 0xf0) {
  421. p0 |= port->data[0] >> 4;
  422. } else {
  423. p0 |= 0x02;
  424. port->state = STATE_UNKNOWN;
  425. }
  426. f_midi_transmit_packet(req, p0, port->data[0], b, 0);
  427. break;
  428. case STATE_2PARAM_1:
  429. port->data[1] = b;
  430. port->state = STATE_2PARAM_2;
  431. break;
  432. case STATE_2PARAM_2:
  433. if (port->data[0] < 0xf0) {
  434. p0 |= port->data[0] >> 4;
  435. port->state = STATE_2PARAM_1;
  436. } else {
  437. p0 |= 0x03;
  438. port->state = STATE_UNKNOWN;
  439. }
  440. f_midi_transmit_packet(req,
  441. p0, port->data[0], port->data[1], b);
  442. break;
  443. case STATE_SYSEX_0:
  444. port->data[0] = b;
  445. port->state = STATE_SYSEX_1;
  446. break;
  447. case STATE_SYSEX_1:
  448. port->data[1] = b;
  449. port->state = STATE_SYSEX_2;
  450. break;
  451. case STATE_SYSEX_2:
  452. f_midi_transmit_packet(req,
  453. p0 | 0x04, port->data[0], port->data[1], b);
  454. port->state = STATE_SYSEX_0;
  455. break;
  456. }
  457. }
  458. }
  459. static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
  460. {
  461. struct usb_ep *ep = midi->in_ep;
  462. int i;
  463. if (!ep)
  464. return;
  465. if (!req)
  466. req = midi_alloc_ep_req(ep, midi->buflen);
  467. if (!req) {
  468. ERROR(midi, "gmidi_transmit: alloc_ep_request failed\n");
  469. return;
  470. }
  471. req->length = 0;
  472. req->complete = f_midi_complete;
  473. for (i = 0; i < MAX_PORTS; i++) {
  474. struct gmidi_in_port *port = midi->in_port[i];
  475. struct snd_rawmidi_substream *substream = midi->in_substream[i];
  476. if (!port || !port->active || !substream)
  477. continue;
  478. while (req->length + 3 < midi->buflen) {
  479. uint8_t b;
  480. if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
  481. port->active = 0;
  482. break;
  483. }
  484. f_midi_transmit_byte(req, port, b);
  485. }
  486. }
  487. if (req->length > 0)
  488. usb_ep_queue(ep, req, GFP_ATOMIC);
  489. else
  490. free_ep_req(ep, req);
  491. }
  492. static void f_midi_in_tasklet(unsigned long data)
  493. {
  494. struct f_midi *midi = (struct f_midi *) data;
  495. f_midi_transmit(midi, NULL);
  496. }
  497. static int f_midi_in_open(struct snd_rawmidi_substream *substream)
  498. {
  499. struct f_midi *midi = substream->rmidi->private_data;
  500. if (!midi->in_port[substream->number])
  501. return -EINVAL;
  502. VDBG(midi, "%s()\n", __func__);
  503. midi->in_substream[substream->number] = substream;
  504. midi->in_port[substream->number]->state = STATE_UNKNOWN;
  505. return 0;
  506. }
  507. static int f_midi_in_close(struct snd_rawmidi_substream *substream)
  508. {
  509. struct f_midi *midi = substream->rmidi->private_data;
  510. VDBG(midi, "%s()\n", __func__);
  511. return 0;
  512. }
  513. static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
  514. {
  515. struct f_midi *midi = substream->rmidi->private_data;
  516. if (!midi->in_port[substream->number])
  517. return;
  518. VDBG(midi, "%s() %d\n", __func__, up);
  519. midi->in_port[substream->number]->active = up;
  520. if (up)
  521. tasklet_hi_schedule(&midi->tasklet);
  522. }
  523. static int f_midi_out_open(struct snd_rawmidi_substream *substream)
  524. {
  525. struct f_midi *midi = substream->rmidi->private_data;
  526. if (substream->number >= MAX_PORTS)
  527. return -EINVAL;
  528. VDBG(midi, "%s()\n", __func__);
  529. midi->out_substream[substream->number] = substream;
  530. return 0;
  531. }
  532. static int f_midi_out_close(struct snd_rawmidi_substream *substream)
  533. {
  534. struct f_midi *midi = substream->rmidi->private_data;
  535. VDBG(midi, "%s()\n", __func__);
  536. return 0;
  537. }
  538. static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
  539. {
  540. struct f_midi *midi = substream->rmidi->private_data;
  541. VDBG(midi, "%s()\n", __func__);
  542. if (up)
  543. set_bit(substream->number, &midi->out_triggered);
  544. else
  545. clear_bit(substream->number, &midi->out_triggered);
  546. }
  547. static struct snd_rawmidi_ops gmidi_in_ops = {
  548. .open = f_midi_in_open,
  549. .close = f_midi_in_close,
  550. .trigger = f_midi_in_trigger,
  551. };
  552. static struct snd_rawmidi_ops gmidi_out_ops = {
  553. .open = f_midi_out_open,
  554. .close = f_midi_out_close,
  555. .trigger = f_midi_out_trigger
  556. };
  557. /* register as a sound "card" */
  558. static int f_midi_register_card(struct f_midi *midi)
  559. {
  560. struct snd_card *card;
  561. struct snd_rawmidi *rmidi;
  562. int err;
  563. static struct snd_device_ops ops = {
  564. .dev_free = f_midi_snd_free,
  565. };
  566. err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
  567. THIS_MODULE, 0, &card);
  568. if (err < 0) {
  569. ERROR(midi, "snd_card_new() failed\n");
  570. goto fail;
  571. }
  572. midi->card = card;
  573. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
  574. if (err < 0) {
  575. ERROR(midi, "snd_device_new() failed: error %d\n", err);
  576. goto fail;
  577. }
  578. strcpy(card->driver, f_midi_longname);
  579. strcpy(card->longname, f_midi_longname);
  580. strcpy(card->shortname, f_midi_shortname);
  581. /* Set up rawmidi */
  582. snd_component_add(card, "MIDI");
  583. err = snd_rawmidi_new(card, card->longname, 0,
  584. midi->out_ports, midi->in_ports, &rmidi);
  585. if (err < 0) {
  586. ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
  587. goto fail;
  588. }
  589. midi->rmidi = rmidi;
  590. strcpy(rmidi->name, card->shortname);
  591. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  592. SNDRV_RAWMIDI_INFO_INPUT |
  593. SNDRV_RAWMIDI_INFO_DUPLEX;
  594. rmidi->private_data = midi;
  595. /*
  596. * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
  597. * It's an upside-down world being a gadget.
  598. */
  599. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
  600. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
  601. /* register it - we're ready to go */
  602. err = snd_card_register(card);
  603. if (err < 0) {
  604. ERROR(midi, "snd_card_register() failed\n");
  605. goto fail;
  606. }
  607. VDBG(midi, "%s() finished ok\n", __func__);
  608. return 0;
  609. fail:
  610. if (midi->card) {
  611. snd_card_free(midi->card);
  612. midi->card = NULL;
  613. }
  614. return err;
  615. }
  616. /* MIDI function driver setup/binding */
  617. static int __init
  618. f_midi_bind(struct usb_configuration *c, struct usb_function *f)
  619. {
  620. struct usb_descriptor_header **midi_function;
  621. struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
  622. struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
  623. struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
  624. struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
  625. struct usb_composite_dev *cdev = c->cdev;
  626. struct f_midi *midi = func_to_midi(f);
  627. int status, n, jack = 1, i = 0;
  628. /* maybe allocate device-global string ID */
  629. if (midi_string_defs[0].id == 0) {
  630. status = usb_string_id(c->cdev);
  631. if (status < 0)
  632. goto fail;
  633. midi_string_defs[0].id = status;
  634. }
  635. /* We have two interfaces, AudioControl and MIDIStreaming */
  636. status = usb_interface_id(c, f);
  637. if (status < 0)
  638. goto fail;
  639. ac_interface_desc.bInterfaceNumber = status;
  640. status = usb_interface_id(c, f);
  641. if (status < 0)
  642. goto fail;
  643. ms_interface_desc.bInterfaceNumber = status;
  644. ac_header_desc.baInterfaceNr[0] = status;
  645. status = -ENODEV;
  646. /* allocate instance-specific endpoints */
  647. midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
  648. if (!midi->in_ep)
  649. goto fail;
  650. midi->in_ep->driver_data = cdev; /* claim */
  651. midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
  652. if (!midi->out_ep)
  653. goto fail;
  654. midi->out_ep->driver_data = cdev; /* claim */
  655. /* allocate temporary function list */
  656. midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
  657. GFP_KERNEL);
  658. if (!midi_function) {
  659. status = -ENOMEM;
  660. goto fail;
  661. }
  662. /*
  663. * construct the function's descriptor set. As the number of
  664. * input and output MIDI ports is configurable, we have to do
  665. * it that way.
  666. */
  667. /* add the headers - these are always the same */
  668. midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
  669. midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
  670. midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
  671. /* calculate the header's wTotalLength */
  672. n = USB_DT_MS_HEADER_SIZE
  673. + (midi->in_ports + midi->out_ports) *
  674. (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
  675. ms_header_desc.wTotalLength = cpu_to_le16(n);
  676. midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
  677. /* configure the external IN jacks, each linked to an embedded OUT jack */
  678. for (n = 0; n < midi->in_ports; n++) {
  679. struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
  680. struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
  681. in_ext->bLength = USB_DT_MIDI_IN_SIZE;
  682. in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  683. in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  684. in_ext->bJackType = USB_MS_EXTERNAL;
  685. in_ext->bJackID = jack++;
  686. in_ext->iJack = 0;
  687. midi_function[i++] = (struct usb_descriptor_header *) in_ext;
  688. out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
  689. out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  690. out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  691. out_emb->bJackType = USB_MS_EMBEDDED;
  692. out_emb->bJackID = jack++;
  693. out_emb->bNrInputPins = 1;
  694. out_emb->pins[0].baSourcePin = 1;
  695. out_emb->pins[0].baSourceID = in_ext->bJackID;
  696. out_emb->iJack = 0;
  697. midi_function[i++] = (struct usb_descriptor_header *) out_emb;
  698. /* link it to the endpoint */
  699. ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
  700. }
  701. /* configure the external OUT jacks, each linked to an embedded IN jack */
  702. for (n = 0; n < midi->out_ports; n++) {
  703. struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
  704. struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
  705. in_emb->bLength = USB_DT_MIDI_IN_SIZE;
  706. in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  707. in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  708. in_emb->bJackType = USB_MS_EMBEDDED;
  709. in_emb->bJackID = jack++;
  710. in_emb->iJack = 0;
  711. midi_function[i++] = (struct usb_descriptor_header *) in_emb;
  712. out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
  713. out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  714. out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  715. out_ext->bJackType = USB_MS_EXTERNAL;
  716. out_ext->bJackID = jack++;
  717. out_ext->bNrInputPins = 1;
  718. out_ext->iJack = 0;
  719. out_ext->pins[0].baSourceID = in_emb->bJackID;
  720. out_ext->pins[0].baSourcePin = 1;
  721. midi_function[i++] = (struct usb_descriptor_header *) out_ext;
  722. /* link it to the endpoint */
  723. ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
  724. }
  725. /* configure the endpoint descriptors ... */
  726. ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
  727. ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
  728. ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
  729. ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
  730. /* ... and add them to the list */
  731. midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
  732. midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
  733. midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
  734. midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
  735. midi_function[i++] = NULL;
  736. /*
  737. * support all relevant hardware speeds... we expect that when
  738. * hardware is dual speed, all bulk-capable endpoints work at
  739. * both speeds
  740. */
  741. /* copy descriptors, and track endpoint copies */
  742. f->fs_descriptors = usb_copy_descriptors(midi_function);
  743. if (!f->fs_descriptors)
  744. goto fail_f_midi;
  745. if (gadget_is_dualspeed(c->cdev->gadget)) {
  746. bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
  747. bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
  748. f->hs_descriptors = usb_copy_descriptors(midi_function);
  749. if (!f->hs_descriptors)
  750. goto fail_f_midi;
  751. }
  752. kfree(midi_function);
  753. return 0;
  754. fail_f_midi:
  755. kfree(midi_function);
  756. usb_free_descriptors(f->hs_descriptors);
  757. fail:
  758. /* we might as well release our claims on endpoints */
  759. if (midi->out_ep)
  760. midi->out_ep->driver_data = NULL;
  761. if (midi->in_ep)
  762. midi->in_ep->driver_data = NULL;
  763. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  764. return status;
  765. }
  766. /**
  767. * f_midi_bind_config - add USB MIDI function to a configuration
  768. * @c: the configuration to supcard the USB audio function
  769. * @index: the soundcard index to use for the ALSA device creation
  770. * @id: the soundcard id to use for the ALSA device creation
  771. * @buflen: the buffer length to use
  772. * @qlen the number of read requests to pre-allocate
  773. * Context: single threaded during gadget setup
  774. *
  775. * Returns zero on success, else negative errno.
  776. */
  777. int __init f_midi_bind_config(struct usb_configuration *c,
  778. int index, char *id,
  779. unsigned int in_ports,
  780. unsigned int out_ports,
  781. unsigned int buflen,
  782. unsigned int qlen)
  783. {
  784. struct f_midi *midi;
  785. int status, i;
  786. /* sanity check */
  787. if (in_ports > MAX_PORTS || out_ports > MAX_PORTS)
  788. return -EINVAL;
  789. /* allocate and initialize one new instance */
  790. midi = kzalloc(sizeof *midi, GFP_KERNEL);
  791. if (!midi) {
  792. status = -ENOMEM;
  793. goto fail;
  794. }
  795. for (i = 0; i < in_ports; i++) {
  796. struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
  797. if (!port) {
  798. status = -ENOMEM;
  799. goto setup_fail;
  800. }
  801. port->midi = midi;
  802. port->active = 0;
  803. port->cable = i;
  804. midi->in_port[i] = port;
  805. }
  806. midi->gadget = c->cdev->gadget;
  807. tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
  808. /* set up ALSA midi devices */
  809. midi->in_ports = in_ports;
  810. midi->out_ports = out_ports;
  811. status = f_midi_register_card(midi);
  812. if (status < 0)
  813. goto setup_fail;
  814. midi->func.name = "gmidi function";
  815. midi->func.strings = midi_strings;
  816. midi->func.bind = f_midi_bind;
  817. midi->func.unbind = f_midi_unbind;
  818. midi->func.set_alt = f_midi_set_alt;
  819. midi->func.disable = f_midi_disable;
  820. midi->id = kstrdup(id, GFP_KERNEL);
  821. midi->index = index;
  822. midi->buflen = buflen;
  823. midi->qlen = qlen;
  824. status = usb_add_function(c, &midi->func);
  825. if (status)
  826. goto setup_fail;
  827. return 0;
  828. setup_fail:
  829. for (--i; i >= 0; i--)
  830. kfree(midi->in_port[i]);
  831. kfree(midi);
  832. fail:
  833. return status;
  834. }