f_midi.c 25 KB

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