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