Linux Audio

Check our new training course

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