f_midi.c 31 KB

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