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