f_midi.c 29 KB

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