blob: 695cf75428e3481b3808382bbbbe3f32cb1ca261 [file] [log] [blame]
Daniel Mackd5daf492011-09-28 16:41:32 +02001/*
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>
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +020023#include <linux/module.h>
Daniel Mackd5daf492011-09-28 16:41:32 +020024#include <linux/slab.h>
Daniel Mackd5daf492011-09-28 16:41:32 +020025#include <linux/device.h>
26
27#include <sound/core.h>
28#include <sound/initval.h>
29#include <sound/rawmidi.h>
30
31#include <linux/usb/ch9.h>
32#include <linux/usb/gadget.h>
33#include <linux/usb/audio.h>
34#include <linux/usb/midi.h>
35
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010036#include "u_f.h"
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +020037#include "u_midi.h"
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010038
Daniel Mackd5daf492011-09-28 16:41:32 +020039MODULE_AUTHOR("Ben Williamson");
40MODULE_LICENSE("GPL v2");
41
42static const char f_midi_shortname[] = "f_midi";
43static const char f_midi_longname[] = "MIDI Gadget";
44
45/*
Daniel Mackc8933c32011-09-28 16:41:34 +020046 * We can only handle 16 cables on one single endpoint, as cable numbers are
47 * stored in 4-bit fields. And as the interface currently only holds one
48 * single endpoint, this is the maximum number of ports we can allow.
49 */
50#define MAX_PORTS 16
51
52/*
Daniel Mackd5daf492011-09-28 16:41:32 +020053 * This is a gadget, and the IN/OUT naming is from the host's perspective.
54 * USB -> OUT endpoint -> rawmidi
55 * USB <- IN endpoint <- rawmidi
56 */
57struct gmidi_in_port {
58 struct f_midi *midi;
59 int active;
Daniel Mackc8933c32011-09-28 16:41:34 +020060 uint8_t cable;
Daniel Mackd5daf492011-09-28 16:41:32 +020061 uint8_t state;
62#define STATE_UNKNOWN 0
63#define STATE_1PARAM 1
64#define STATE_2PARAM_1 2
65#define STATE_2PARAM_2 3
66#define STATE_SYSEX_0 4
67#define STATE_SYSEX_1 5
68#define STATE_SYSEX_2 6
69 uint8_t data[2];
70};
71
72struct f_midi {
73 struct usb_function func;
74 struct usb_gadget *gadget;
75 struct usb_ep *in_ep, *out_ep;
76 struct snd_card *card;
77 struct snd_rawmidi *rmidi;
Daniel Mackd5daf492011-09-28 16:41:32 +020078
Daniel Mackc8933c32011-09-28 16:41:34 +020079 struct snd_rawmidi_substream *in_substream[MAX_PORTS];
80 struct snd_rawmidi_substream *out_substream[MAX_PORTS];
81 struct gmidi_in_port *in_port[MAX_PORTS];
82
Daniel Mackd5daf492011-09-28 16:41:32 +020083 unsigned long out_triggered;
84 struct tasklet_struct tasklet;
Daniel Mackc8933c32011-09-28 16:41:34 +020085 unsigned int in_ports;
86 unsigned int out_ports;
Daniel Mackd5daf492011-09-28 16:41:32 +020087 int index;
88 char *id;
89 unsigned int buflen, qlen;
90};
91
92static inline struct f_midi *func_to_midi(struct usb_function *f)
93{
94 return container_of(f, struct f_midi, func);
95}
96
97static void f_midi_transmit(struct f_midi *midi, struct usb_request *req);
98
99DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
100DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
Daniel Mackc8933c32011-09-28 16:41:34 +0200101DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
Daniel Mackd5daf492011-09-28 16:41:32 +0200102
103/* B.3.1 Standard AC Interface Descriptor */
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +0200104static struct usb_interface_descriptor ac_interface_desc = {
Daniel Mackd5daf492011-09-28 16:41:32 +0200105 .bLength = USB_DT_INTERFACE_SIZE,
106 .bDescriptorType = USB_DT_INTERFACE,
107 /* .bInterfaceNumber = DYNAMIC */
108 /* .bNumEndpoints = DYNAMIC */
109 .bInterfaceClass = USB_CLASS_AUDIO,
110 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
111 /* .iInterface = DYNAMIC */
112};
113
114/* B.3.2 Class-Specific AC Interface Descriptor */
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +0200115static struct uac1_ac_header_descriptor_1 ac_header_desc = {
Daniel Mackd5daf492011-09-28 16:41:32 +0200116 .bLength = UAC_DT_AC_HEADER_SIZE(1),
117 .bDescriptorType = USB_DT_CS_INTERFACE,
118 .bDescriptorSubtype = USB_MS_HEADER,
119 .bcdADC = cpu_to_le16(0x0100),
120 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
121 .bInCollection = 1,
122 /* .baInterfaceNr = DYNAMIC */
123};
124
125/* B.4.1 Standard MS Interface Descriptor */
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +0200126static struct usb_interface_descriptor ms_interface_desc = {
Daniel Mackd5daf492011-09-28 16:41:32 +0200127 .bLength = USB_DT_INTERFACE_SIZE,
128 .bDescriptorType = USB_DT_INTERFACE,
129 /* .bInterfaceNumber = DYNAMIC */
130 .bNumEndpoints = 2,
131 .bInterfaceClass = USB_CLASS_AUDIO,
132 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
133 /* .iInterface = DYNAMIC */
134};
135
136/* B.4.2 Class-Specific MS Interface Descriptor */
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +0200137static struct usb_ms_header_descriptor ms_header_desc = {
Daniel Mackd5daf492011-09-28 16:41:32 +0200138 .bLength = USB_DT_MS_HEADER_SIZE,
139 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubtype = USB_MS_HEADER,
141 .bcdMSC = cpu_to_le16(0x0100),
Daniel Mackc8933c32011-09-28 16:41:34 +0200142 /* .wTotalLength = DYNAMIC */
Daniel Mackd5daf492011-09-28 16:41:32 +0200143};
144
Daniel Mackd5daf492011-09-28 16:41:32 +0200145/* B.5.1 Standard Bulk OUT Endpoint Descriptor */
146static struct usb_endpoint_descriptor bulk_out_desc = {
147 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
148 .bDescriptorType = USB_DT_ENDPOINT,
149 .bEndpointAddress = USB_DIR_OUT,
150 .bmAttributes = USB_ENDPOINT_XFER_BULK,
151};
152
153/* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
Daniel Mackc8933c32011-09-28 16:41:34 +0200154static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
155 /* .bLength = DYNAMIC */
Daniel Mackd5daf492011-09-28 16:41:32 +0200156 .bDescriptorType = USB_DT_CS_ENDPOINT,
157 .bDescriptorSubtype = USB_MS_GENERAL,
Daniel Mackc8933c32011-09-28 16:41:34 +0200158 /* .bNumEmbMIDIJack = DYNAMIC */
159 /* .baAssocJackID = DYNAMIC */
Daniel Mackd5daf492011-09-28 16:41:32 +0200160};
161
162/* B.6.1 Standard Bulk IN Endpoint Descriptor */
163static struct usb_endpoint_descriptor bulk_in_desc = {
164 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = USB_DIR_IN,
167 .bmAttributes = USB_ENDPOINT_XFER_BULK,
168};
169
170/* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
Daniel Mackc8933c32011-09-28 16:41:34 +0200171static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
172 /* .bLength = DYNAMIC */
Daniel Mackd5daf492011-09-28 16:41:32 +0200173 .bDescriptorType = USB_DT_CS_ENDPOINT,
174 .bDescriptorSubtype = USB_MS_GENERAL,
Daniel Mackc8933c32011-09-28 16:41:34 +0200175 /* .bNumEmbMIDIJack = DYNAMIC */
176 /* .baAssocJackID = DYNAMIC */
Daniel Mackd5daf492011-09-28 16:41:32 +0200177};
178
179/* string IDs are assigned dynamically */
180
181#define STRING_FUNC_IDX 0
182
183static struct usb_string midi_string_defs[] = {
184 [STRING_FUNC_IDX].s = "MIDI function",
185 { } /* end of list */
186};
187
188static struct usb_gadget_strings midi_stringtab = {
189 .language = 0x0409, /* en-us */
190 .strings = midi_string_defs,
191};
192
193static struct usb_gadget_strings *midi_strings[] = {
194 &midi_stringtab,
195 NULL,
196};
197
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100198static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
199 unsigned length)
Daniel Mackd5daf492011-09-28 16:41:32 +0200200{
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100201 return alloc_ep_req(ep, length, length);
Daniel Mackd5daf492011-09-28 16:41:32 +0200202}
203
204static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
205{
206 kfree(req->buf);
207 usb_ep_free_request(ep, req);
208}
209
210static const uint8_t f_midi_cin_length[] = {
211 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
212};
213
214/*
215 * Receives a chunk of MIDI data.
216 */
217static void f_midi_read_data(struct usb_ep *ep, int cable,
218 uint8_t *data, int length)
219{
220 struct f_midi *midi = ep->driver_data;
Daniel Mackc8933c32011-09-28 16:41:34 +0200221 struct snd_rawmidi_substream *substream = midi->out_substream[cable];
Daniel Mackd5daf492011-09-28 16:41:32 +0200222
Daniel Mackc8933c32011-09-28 16:41:34 +0200223 if (!substream)
Daniel Mackd5daf492011-09-28 16:41:32 +0200224 /* Nobody is listening - throw it on the floor. */
225 return;
226
Daniel Mackc8933c32011-09-28 16:41:34 +0200227 if (!test_bit(cable, &midi->out_triggered))
Daniel Mackd5daf492011-09-28 16:41:32 +0200228 return;
229
Daniel Mackc8933c32011-09-28 16:41:34 +0200230 snd_rawmidi_receive(substream, data, length);
Daniel Mackd5daf492011-09-28 16:41:32 +0200231}
232
233static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
234{
235 unsigned int i;
236 u8 *buf = req->buf;
237
238 for (i = 0; i + 3 < req->actual; i += 4)
239 if (buf[i] != 0) {
240 int cable = buf[i] >> 4;
241 int length = f_midi_cin_length[buf[i] & 0x0f];
242 f_midi_read_data(ep, cable, &buf[i + 1], length);
243 }
244}
245
246static void
247f_midi_complete(struct usb_ep *ep, struct usb_request *req)
248{
249 struct f_midi *midi = ep->driver_data;
250 struct usb_composite_dev *cdev = midi->func.config->cdev;
251 int status = req->status;
252
253 switch (status) {
254 case 0: /* normal completion */
255 if (ep == midi->out_ep) {
256 /* We received stuff. req is queued again, below */
257 f_midi_handle_out_data(ep, req);
258 } else if (ep == midi->in_ep) {
259 /* Our transmit completed. See if there's more to go.
260 * f_midi_transmit eats req, don't queue it again. */
261 f_midi_transmit(midi, req);
262 return;
263 }
264 break;
265
266 /* this endpoint is normally active while we're configured */
267 case -ECONNABORTED: /* hardware forced ep reset */
268 case -ECONNRESET: /* request dequeued */
269 case -ESHUTDOWN: /* disconnect from host */
270 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
271 req->actual, req->length);
272 if (ep == midi->out_ep)
273 f_midi_handle_out_data(ep, req);
274
275 free_ep_req(ep, req);
276 return;
277
278 case -EOVERFLOW: /* buffer overrun on read means that
279 * we didn't provide a big enough buffer.
280 */
281 default:
282 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
283 status, req->actual, req->length);
284 break;
285 case -EREMOTEIO: /* short read */
286 break;
287 }
288
289 status = usb_ep_queue(ep, req, GFP_ATOMIC);
290 if (status) {
291 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
292 ep->name, req->length, status);
293 usb_ep_set_halt(ep);
294 /* FIXME recover later ... somehow */
295 }
296}
297
298static int f_midi_start_ep(struct f_midi *midi,
299 struct usb_function *f,
300 struct usb_ep *ep)
301{
302 int err;
303 struct usb_composite_dev *cdev = f->config->cdev;
304
Robert Baldygace723952015-09-16 12:10:49 +0200305 usb_ep_disable(ep);
Daniel Mackd5daf492011-09-28 16:41:32 +0200306
307 err = config_ep_by_speed(midi->gadget, f, ep);
308 if (err) {
309 ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
310 return err;
311 }
312
313 err = usb_ep_enable(ep);
314 if (err) {
315 ERROR(cdev, "can't start %s: %d\n", ep->name, err);
316 return err;
317 }
318
319 ep->driver_data = midi;
320
321 return 0;
322}
323
324static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
325{
326 struct f_midi *midi = func_to_midi(f);
Daniel Mackd5daf492011-09-28 16:41:32 +0200327 unsigned i;
328 int err;
329
Robert Baldyga4ef7a4a2015-07-13 11:03:51 +0200330 /* For Control Device interface we do nothing */
331 if (intf == 0)
332 return 0;
333
Daniel Mackd5daf492011-09-28 16:41:32 +0200334 err = f_midi_start_ep(midi, f, midi->in_ep);
335 if (err)
336 return err;
337
338 err = f_midi_start_ep(midi, f, midi->out_ep);
339 if (err)
340 return err;
341
Daniel Mackd5daf492011-09-28 16:41:32 +0200342 /* allocate a bunch of read buffers and queue them all at once. */
343 for (i = 0; i < midi->qlen && err == 0; i++) {
344 struct usb_request *req =
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100345 midi_alloc_ep_req(midi->out_ep, midi->buflen);
Daniel Mackd5daf492011-09-28 16:41:32 +0200346 if (req == NULL)
347 return -ENOMEM;
348
349 req->complete = f_midi_complete;
350 err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
351 if (err) {
352 ERROR(midi, "%s queue req: %d\n",
353 midi->out_ep->name, err);
Felipe F. Tonelload0d1a02015-11-10 17:52:06 +0000354 free_ep_req(midi->out_ep, req);
Daniel Mackd5daf492011-09-28 16:41:32 +0200355 }
356 }
357
358 return 0;
359}
360
361static void f_midi_disable(struct usb_function *f)
362{
363 struct f_midi *midi = func_to_midi(f);
364 struct usb_composite_dev *cdev = f->config->cdev;
365
366 DBG(cdev, "disable\n");
367
368 /*
369 * just disable endpoints, forcing completion of pending i/o.
370 * all our completion handlers free their requests in this case.
371 */
372 usb_ep_disable(midi->in_ep);
373 usb_ep_disable(midi->out_ep);
374}
375
Daniel Mackd5daf492011-09-28 16:41:32 +0200376static int f_midi_snd_free(struct snd_device *device)
377{
378 return 0;
379}
380
381static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
382 uint8_t p1, uint8_t p2, uint8_t p3)
383{
384 unsigned length = req->length;
385 u8 *buf = (u8 *)req->buf + length;
386
387 buf[0] = p0;
388 buf[1] = p1;
389 buf[2] = p2;
390 buf[3] = p3;
391 req->length = length + 4;
392}
393
394/*
395 * Converts MIDI commands to USB MIDI packets.
396 */
397static void f_midi_transmit_byte(struct usb_request *req,
398 struct gmidi_in_port *port, uint8_t b)
399{
Daniel Mackc8933c32011-09-28 16:41:34 +0200400 uint8_t p0 = port->cable << 4;
Daniel Mackd5daf492011-09-28 16:41:32 +0200401
402 if (b >= 0xf8) {
403 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
404 } else if (b >= 0xf0) {
405 switch (b) {
406 case 0xf0:
407 port->data[0] = b;
408 port->state = STATE_SYSEX_1;
409 break;
410 case 0xf1:
411 case 0xf3:
412 port->data[0] = b;
413 port->state = STATE_1PARAM;
414 break;
415 case 0xf2:
416 port->data[0] = b;
417 port->state = STATE_2PARAM_1;
418 break;
419 case 0xf4:
420 case 0xf5:
421 port->state = STATE_UNKNOWN;
422 break;
423 case 0xf6:
424 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
425 port->state = STATE_UNKNOWN;
426 break;
427 case 0xf7:
428 switch (port->state) {
429 case STATE_SYSEX_0:
430 f_midi_transmit_packet(req,
431 p0 | 0x05, 0xf7, 0, 0);
432 break;
433 case STATE_SYSEX_1:
434 f_midi_transmit_packet(req,
435 p0 | 0x06, port->data[0], 0xf7, 0);
436 break;
437 case STATE_SYSEX_2:
438 f_midi_transmit_packet(req,
439 p0 | 0x07, port->data[0],
440 port->data[1], 0xf7);
441 break;
442 }
443 port->state = STATE_UNKNOWN;
444 break;
445 }
446 } else if (b >= 0x80) {
447 port->data[0] = b;
448 if (b >= 0xc0 && b <= 0xdf)
449 port->state = STATE_1PARAM;
450 else
451 port->state = STATE_2PARAM_1;
452 } else { /* b < 0x80 */
453 switch (port->state) {
454 case STATE_1PARAM:
455 if (port->data[0] < 0xf0) {
456 p0 |= port->data[0] >> 4;
457 } else {
458 p0 |= 0x02;
459 port->state = STATE_UNKNOWN;
460 }
461 f_midi_transmit_packet(req, p0, port->data[0], b, 0);
462 break;
463 case STATE_2PARAM_1:
464 port->data[1] = b;
465 port->state = STATE_2PARAM_2;
466 break;
467 case STATE_2PARAM_2:
468 if (port->data[0] < 0xf0) {
469 p0 |= port->data[0] >> 4;
470 port->state = STATE_2PARAM_1;
471 } else {
472 p0 |= 0x03;
473 port->state = STATE_UNKNOWN;
474 }
475 f_midi_transmit_packet(req,
476 p0, port->data[0], port->data[1], b);
477 break;
478 case STATE_SYSEX_0:
479 port->data[0] = b;
480 port->state = STATE_SYSEX_1;
481 break;
482 case STATE_SYSEX_1:
483 port->data[1] = b;
484 port->state = STATE_SYSEX_2;
485 break;
486 case STATE_SYSEX_2:
487 f_midi_transmit_packet(req,
488 p0 | 0x04, port->data[0], port->data[1], b);
489 port->state = STATE_SYSEX_0;
490 break;
491 }
492 }
493}
494
495static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
496{
497 struct usb_ep *ep = midi->in_ep;
Daniel Mackc8933c32011-09-28 16:41:34 +0200498 int i;
Daniel Mackd5daf492011-09-28 16:41:32 +0200499
500 if (!ep)
501 return;
502
503 if (!req)
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100504 req = midi_alloc_ep_req(ep, midi->buflen);
Daniel Mackd5daf492011-09-28 16:41:32 +0200505
506 if (!req) {
Julia Lawallc9b3bde2014-12-07 20:21:00 +0100507 ERROR(midi, "%s: alloc_ep_request failed\n", __func__);
Daniel Mackd5daf492011-09-28 16:41:32 +0200508 return;
509 }
510 req->length = 0;
511 req->complete = f_midi_complete;
512
Daniel Mackc8933c32011-09-28 16:41:34 +0200513 for (i = 0; i < MAX_PORTS; i++) {
514 struct gmidi_in_port *port = midi->in_port[i];
515 struct snd_rawmidi_substream *substream = midi->in_substream[i];
516
517 if (!port || !port->active || !substream)
518 continue;
519
Daniel Mackd5daf492011-09-28 16:41:32 +0200520 while (req->length + 3 < midi->buflen) {
521 uint8_t b;
Daniel Mackc8933c32011-09-28 16:41:34 +0200522 if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
Daniel Mackd5daf492011-09-28 16:41:32 +0200523 port->active = 0;
524 break;
525 }
526 f_midi_transmit_byte(req, port, b);
527 }
528 }
529
Felipe F. Tonelloe9ca7e42015-11-10 17:52:03 +0000530 if (req->length > 0 && ep->enabled) {
Felipe F. Tonellof35fe4b2015-09-18 18:36:28 +0100531 int err;
532
533 err = usb_ep_queue(ep, req, GFP_ATOMIC);
534 if (err < 0)
535 ERROR(midi, "%s queue req: %d\n",
536 midi->in_ep->name, err);
537 } else {
Daniel Mackd5daf492011-09-28 16:41:32 +0200538 free_ep_req(ep, req);
Felipe F. Tonellof35fe4b2015-09-18 18:36:28 +0100539 }
Daniel Mackd5daf492011-09-28 16:41:32 +0200540}
541
542static void f_midi_in_tasklet(unsigned long data)
543{
544 struct f_midi *midi = (struct f_midi *) data;
545 f_midi_transmit(midi, NULL);
546}
547
548static int f_midi_in_open(struct snd_rawmidi_substream *substream)
549{
550 struct f_midi *midi = substream->rmidi->private_data;
551
Daniel Mackc8933c32011-09-28 16:41:34 +0200552 if (!midi->in_port[substream->number])
553 return -EINVAL;
554
Daniel Mackd5daf492011-09-28 16:41:32 +0200555 VDBG(midi, "%s()\n", __func__);
Daniel Mackc8933c32011-09-28 16:41:34 +0200556 midi->in_substream[substream->number] = substream;
557 midi->in_port[substream->number]->state = STATE_UNKNOWN;
Daniel Mackd5daf492011-09-28 16:41:32 +0200558 return 0;
559}
560
561static int f_midi_in_close(struct snd_rawmidi_substream *substream)
562{
563 struct f_midi *midi = substream->rmidi->private_data;
564
565 VDBG(midi, "%s()\n", __func__);
566 return 0;
567}
568
569static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
570{
571 struct f_midi *midi = substream->rmidi->private_data;
572
Daniel Mackc8933c32011-09-28 16:41:34 +0200573 if (!midi->in_port[substream->number])
574 return;
575
Daniel Mackd5daf492011-09-28 16:41:32 +0200576 VDBG(midi, "%s() %d\n", __func__, up);
Daniel Mackc8933c32011-09-28 16:41:34 +0200577 midi->in_port[substream->number]->active = up;
Daniel Mackd5daf492011-09-28 16:41:32 +0200578 if (up)
579 tasklet_hi_schedule(&midi->tasklet);
580}
581
582static int f_midi_out_open(struct snd_rawmidi_substream *substream)
583{
584 struct f_midi *midi = substream->rmidi->private_data;
585
Dan Carpenter08895512011-10-18 09:24:36 +0300586 if (substream->number >= MAX_PORTS)
Daniel Mackc8933c32011-09-28 16:41:34 +0200587 return -EINVAL;
588
Daniel Mackd5daf492011-09-28 16:41:32 +0200589 VDBG(midi, "%s()\n", __func__);
Daniel Mackc8933c32011-09-28 16:41:34 +0200590 midi->out_substream[substream->number] = substream;
Daniel Mackd5daf492011-09-28 16:41:32 +0200591 return 0;
592}
593
594static int f_midi_out_close(struct snd_rawmidi_substream *substream)
595{
596 struct f_midi *midi = substream->rmidi->private_data;
597
598 VDBG(midi, "%s()\n", __func__);
599 return 0;
600}
601
602static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
603{
604 struct f_midi *midi = substream->rmidi->private_data;
605
606 VDBG(midi, "%s()\n", __func__);
607
608 if (up)
609 set_bit(substream->number, &midi->out_triggered);
610 else
611 clear_bit(substream->number, &midi->out_triggered);
612}
613
614static struct snd_rawmidi_ops gmidi_in_ops = {
615 .open = f_midi_in_open,
616 .close = f_midi_in_close,
617 .trigger = f_midi_in_trigger,
618};
619
620static struct snd_rawmidi_ops gmidi_out_ops = {
621 .open = f_midi_out_open,
622 .close = f_midi_out_close,
623 .trigger = f_midi_out_trigger
624};
625
Andrzej Pietrasiewiczd23b4c32014-10-16 13:33:26 +0200626static inline void f_midi_unregister_card(struct f_midi *midi)
627{
628 if (midi->card) {
629 snd_card_free(midi->card);
630 midi->card = NULL;
631 }
632}
633
Daniel Mackd5daf492011-09-28 16:41:32 +0200634/* register as a sound "card" */
635static int f_midi_register_card(struct f_midi *midi)
636{
637 struct snd_card *card;
638 struct snd_rawmidi *rmidi;
639 int err;
Daniel Mackd5daf492011-09-28 16:41:32 +0200640 static struct snd_device_ops ops = {
641 .dev_free = f_midi_snd_free,
642 };
643
Takashi Iwai13345092014-01-29 15:04:31 +0100644 err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
645 THIS_MODULE, 0, &card);
Daniel Mackd5daf492011-09-28 16:41:32 +0200646 if (err < 0) {
Takashi Iwai13345092014-01-29 15:04:31 +0100647 ERROR(midi, "snd_card_new() failed\n");
Daniel Mackd5daf492011-09-28 16:41:32 +0200648 goto fail;
649 }
650 midi->card = card;
651
652 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
653 if (err < 0) {
654 ERROR(midi, "snd_device_new() failed: error %d\n", err);
655 goto fail;
656 }
657
658 strcpy(card->driver, f_midi_longname);
659 strcpy(card->longname, f_midi_longname);
660 strcpy(card->shortname, f_midi_shortname);
661
662 /* Set up rawmidi */
Daniel Mackd5daf492011-09-28 16:41:32 +0200663 snd_component_add(card, "MIDI");
664 err = snd_rawmidi_new(card, card->longname, 0,
Daniel Mackc8933c32011-09-28 16:41:34 +0200665 midi->out_ports, midi->in_ports, &rmidi);
Daniel Mackd5daf492011-09-28 16:41:32 +0200666 if (err < 0) {
667 ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
668 goto fail;
669 }
670 midi->rmidi = rmidi;
671 strcpy(rmidi->name, card->shortname);
672 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
673 SNDRV_RAWMIDI_INFO_INPUT |
674 SNDRV_RAWMIDI_INFO_DUPLEX;
675 rmidi->private_data = midi;
676
677 /*
678 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
679 * It's an upside-down world being a gadget.
680 */
681 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
682 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
683
Daniel Mackd5daf492011-09-28 16:41:32 +0200684 /* register it - we're ready to go */
685 err = snd_card_register(card);
686 if (err < 0) {
687 ERROR(midi, "snd_card_register() failed\n");
688 goto fail;
689 }
690
691 VDBG(midi, "%s() finished ok\n", __func__);
692 return 0;
693
694fail:
Andrzej Pietrasiewiczd23b4c32014-10-16 13:33:26 +0200695 f_midi_unregister_card(midi);
Daniel Mackd5daf492011-09-28 16:41:32 +0200696 return err;
697}
698
699/* MIDI function driver setup/binding */
700
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +0200701static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
Daniel Mackd5daf492011-09-28 16:41:32 +0200702{
Daniel Mack74203de2011-10-15 13:45:05 +0200703 struct usb_descriptor_header **midi_function;
Daniel Mackc8933c32011-09-28 16:41:34 +0200704 struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
Daniel Mack74203de2011-10-15 13:45:05 +0200705 struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
Daniel Mackc8933c32011-09-28 16:41:34 +0200706 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
Daniel Mack74203de2011-10-15 13:45:05 +0200707 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
Daniel Mackd5daf492011-09-28 16:41:32 +0200708 struct usb_composite_dev *cdev = c->cdev;
709 struct f_midi *midi = func_to_midi(f);
Andrzej Pietrasiewicz9caa0d72014-10-16 13:33:30 +0200710 struct usb_string *us;
Daniel Mackc8933c32011-09-28 16:41:34 +0200711 int status, n, jack = 1, i = 0;
Daniel Mackd5daf492011-09-28 16:41:32 +0200712
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +0200713 midi->gadget = cdev->gadget;
714 tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
715 status = f_midi_register_card(midi);
716 if (status < 0)
717 goto fail_register;
718
Daniel Mackd5daf492011-09-28 16:41:32 +0200719 /* maybe allocate device-global string ID */
Andrzej Pietrasiewicz9caa0d72014-10-16 13:33:30 +0200720 us = usb_gstrings_attach(c->cdev, midi_strings,
721 ARRAY_SIZE(midi_string_defs));
722 if (IS_ERR(us)) {
723 status = PTR_ERR(us);
724 goto fail;
Daniel Mackd5daf492011-09-28 16:41:32 +0200725 }
Andrzej Pietrasiewicz9caa0d72014-10-16 13:33:30 +0200726 ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id;
Daniel Mackd5daf492011-09-28 16:41:32 +0200727
728 /* We have two interfaces, AudioControl and MIDIStreaming */
729 status = usb_interface_id(c, f);
730 if (status < 0)
731 goto fail;
732 ac_interface_desc.bInterfaceNumber = status;
733
734 status = usb_interface_id(c, f);
735 if (status < 0)
736 goto fail;
737 ms_interface_desc.bInterfaceNumber = status;
738 ac_header_desc.baInterfaceNr[0] = status;
739
740 status = -ENODEV;
741
742 /* allocate instance-specific endpoints */
743 midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
744 if (!midi->in_ep)
745 goto fail;
Daniel Mackd5daf492011-09-28 16:41:32 +0200746
747 midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
748 if (!midi->out_ep)
749 goto fail;
Daniel Mackd5daf492011-09-28 16:41:32 +0200750
Daniel Mack74203de2011-10-15 13:45:05 +0200751 /* allocate temporary function list */
Jesper Juhl2a5be872012-03-01 23:01:19 +0100752 midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
Daniel Mack74203de2011-10-15 13:45:05 +0200753 GFP_KERNEL);
754 if (!midi_function) {
755 status = -ENOMEM;
756 goto fail;
757 }
758
Daniel Mackd5daf492011-09-28 16:41:32 +0200759 /*
Daniel Mackc8933c32011-09-28 16:41:34 +0200760 * construct the function's descriptor set. As the number of
761 * input and output MIDI ports is configurable, we have to do
762 * it that way.
763 */
764
765 /* add the headers - these are always the same */
766 midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
767 midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
768 midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
769
770 /* calculate the header's wTotalLength */
771 n = USB_DT_MS_HEADER_SIZE
Daniel Mack74203de2011-10-15 13:45:05 +0200772 + (midi->in_ports + midi->out_ports) *
773 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
Daniel Mackc8933c32011-09-28 16:41:34 +0200774 ms_header_desc.wTotalLength = cpu_to_le16(n);
775
776 midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
777
Daniel Mack74203de2011-10-15 13:45:05 +0200778 /* configure the external IN jacks, each linked to an embedded OUT jack */
Daniel Mackc8933c32011-09-28 16:41:34 +0200779 for (n = 0; n < midi->in_ports; n++) {
Daniel Mack74203de2011-10-15 13:45:05 +0200780 struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
781 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
Daniel Mackc8933c32011-09-28 16:41:34 +0200782
Daniel Mack74203de2011-10-15 13:45:05 +0200783 in_ext->bLength = USB_DT_MIDI_IN_SIZE;
784 in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
785 in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
786 in_ext->bJackType = USB_MS_EXTERNAL;
787 in_ext->bJackID = jack++;
788 in_ext->iJack = 0;
789 midi_function[i++] = (struct usb_descriptor_header *) in_ext;
Daniel Mackc8933c32011-09-28 16:41:34 +0200790
Daniel Mack74203de2011-10-15 13:45:05 +0200791 out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
792 out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
793 out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
794 out_emb->bJackType = USB_MS_EMBEDDED;
795 out_emb->bJackID = jack++;
796 out_emb->bNrInputPins = 1;
797 out_emb->pins[0].baSourcePin = 1;
798 out_emb->pins[0].baSourceID = in_ext->bJackID;
799 out_emb->iJack = 0;
800 midi_function[i++] = (struct usb_descriptor_header *) out_emb;
801
802 /* link it to the endpoint */
803 ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
Daniel Mackc8933c32011-09-28 16:41:34 +0200804 }
805
Daniel Mack74203de2011-10-15 13:45:05 +0200806 /* configure the external OUT jacks, each linked to an embedded IN jack */
Daniel Mackc8933c32011-09-28 16:41:34 +0200807 for (n = 0; n < midi->out_ports; n++) {
Daniel Mack74203de2011-10-15 13:45:05 +0200808 struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
809 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
Daniel Mackc8933c32011-09-28 16:41:34 +0200810
Daniel Mack74203de2011-10-15 13:45:05 +0200811 in_emb->bLength = USB_DT_MIDI_IN_SIZE;
812 in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
813 in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
814 in_emb->bJackType = USB_MS_EMBEDDED;
815 in_emb->bJackID = jack++;
816 in_emb->iJack = 0;
817 midi_function[i++] = (struct usb_descriptor_header *) in_emb;
Daniel Mackc8933c32011-09-28 16:41:34 +0200818
Daniel Mack74203de2011-10-15 13:45:05 +0200819 out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
820 out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
821 out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
822 out_ext->bJackType = USB_MS_EXTERNAL;
823 out_ext->bJackID = jack++;
824 out_ext->bNrInputPins = 1;
825 out_ext->iJack = 0;
826 out_ext->pins[0].baSourceID = in_emb->bJackID;
827 out_ext->pins[0].baSourcePin = 1;
828 midi_function[i++] = (struct usb_descriptor_header *) out_ext;
829
830 /* link it to the endpoint */
831 ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
Daniel Mackc8933c32011-09-28 16:41:34 +0200832 }
833
834 /* configure the endpoint descriptors ... */
835 ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
836 ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
Daniel Mackc8933c32011-09-28 16:41:34 +0200837
838 ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
839 ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
Daniel Mackc8933c32011-09-28 16:41:34 +0200840
841 /* ... and add them to the list */
842 midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
843 midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
844 midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
845 midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
846 midi_function[i++] = NULL;
847
848 /*
Daniel Mackd5daf492011-09-28 16:41:32 +0200849 * support all relevant hardware speeds... we expect that when
850 * hardware is dual speed, all bulk-capable endpoints work at
851 * both speeds
852 */
853 /* copy descriptors, and track endpoint copies */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200854 f->fs_descriptors = usb_copy_descriptors(midi_function);
855 if (!f->fs_descriptors)
Sebastian Andrzej Siewior7f2a9262012-10-22 22:15:03 +0200856 goto fail_f_midi;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200857
Daniel Mackd5daf492011-09-28 16:41:32 +0200858 if (gadget_is_dualspeed(c->cdev->gadget)) {
Daniel Mackd5daf492011-09-28 16:41:32 +0200859 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
860 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
861 f->hs_descriptors = usb_copy_descriptors(midi_function);
Sebastian Andrzej Siewior7f2a9262012-10-22 22:15:03 +0200862 if (!f->hs_descriptors)
863 goto fail_f_midi;
Daniel Mackd5daf492011-09-28 16:41:32 +0200864 }
865
Daniel Mack74203de2011-10-15 13:45:05 +0200866 kfree(midi_function);
867
Daniel Mackd5daf492011-09-28 16:41:32 +0200868 return 0;
869
Sebastian Andrzej Siewior7f2a9262012-10-22 22:15:03 +0200870fail_f_midi:
871 kfree(midi_function);
872 usb_free_descriptors(f->hs_descriptors);
Daniel Mackd5daf492011-09-28 16:41:32 +0200873fail:
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +0200874 f_midi_unregister_card(midi);
875fail_register:
Daniel Mackd5daf492011-09-28 16:41:32 +0200876 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
877
878 return status;
879}
880
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200881static inline struct f_midi_opts *to_f_midi_opts(struct config_item *item)
882{
883 return container_of(to_config_group(item), struct f_midi_opts,
884 func_inst.group);
885}
886
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200887static void midi_attr_release(struct config_item *item)
888{
889 struct f_midi_opts *opts = to_f_midi_opts(item);
890
891 usb_put_function_instance(&opts->func_inst);
892}
893
894static struct configfs_item_operations midi_item_ops = {
895 .release = midi_attr_release,
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200896};
897
898#define F_MIDI_OPT(name, test_limit, limit) \
Christoph Hellwig3755a272015-10-03 15:32:44 +0200899static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200900{ \
Christoph Hellwig3755a272015-10-03 15:32:44 +0200901 struct f_midi_opts *opts = to_f_midi_opts(item); \
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200902 int result; \
903 \
904 mutex_lock(&opts->lock); \
905 result = sprintf(page, "%d\n", opts->name); \
906 mutex_unlock(&opts->lock); \
907 \
908 return result; \
909} \
910 \
Christoph Hellwig3755a272015-10-03 15:32:44 +0200911static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200912 const char *page, size_t len) \
913{ \
Christoph Hellwig3755a272015-10-03 15:32:44 +0200914 struct f_midi_opts *opts = to_f_midi_opts(item); \
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200915 int ret; \
916 u32 num; \
917 \
918 mutex_lock(&opts->lock); \
919 if (opts->refcnt) { \
920 ret = -EBUSY; \
921 goto end; \
922 } \
923 \
924 ret = kstrtou32(page, 0, &num); \
925 if (ret) \
926 goto end; \
927 \
928 if (test_limit && num > limit) { \
929 ret = -EINVAL; \
930 goto end; \
931 } \
932 opts->name = num; \
933 ret = len; \
934 \
935end: \
936 mutex_unlock(&opts->lock); \
937 return ret; \
938} \
939 \
Christoph Hellwig3755a272015-10-03 15:32:44 +0200940CONFIGFS_ATTR(f_midi_opts_, name);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200941
942F_MIDI_OPT(index, true, SNDRV_CARDS);
943F_MIDI_OPT(buflen, false, 0);
944F_MIDI_OPT(qlen, false, 0);
945F_MIDI_OPT(in_ports, true, MAX_PORTS);
946F_MIDI_OPT(out_ports, true, MAX_PORTS);
947
Christoph Hellwig3755a272015-10-03 15:32:44 +0200948static ssize_t f_midi_opts_id_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200949{
Christoph Hellwig3755a272015-10-03 15:32:44 +0200950 struct f_midi_opts *opts = to_f_midi_opts(item);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200951 int result;
952
953 mutex_lock(&opts->lock);
Pawel Szewczyka25a23c2015-05-14 14:14:11 +0200954 if (opts->id) {
955 result = strlcpy(page, opts->id, PAGE_SIZE);
956 } else {
957 page[0] = 0;
958 result = 0;
959 }
960
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200961 mutex_unlock(&opts->lock);
962
963 return result;
964}
965
Christoph Hellwig3755a272015-10-03 15:32:44 +0200966static ssize_t f_midi_opts_id_store(struct config_item *item,
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200967 const char *page, size_t len)
968{
Christoph Hellwig3755a272015-10-03 15:32:44 +0200969 struct f_midi_opts *opts = to_f_midi_opts(item);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200970 int ret;
971 char *c;
972
973 mutex_lock(&opts->lock);
974 if (opts->refcnt) {
975 ret = -EBUSY;
976 goto end;
977 }
978
979 c = kstrndup(page, len, GFP_KERNEL);
980 if (!c) {
981 ret = -ENOMEM;
982 goto end;
983 }
984 if (opts->id_allocated)
985 kfree(opts->id);
986 opts->id = c;
987 opts->id_allocated = true;
988 ret = len;
989end:
990 mutex_unlock(&opts->lock);
991 return ret;
992}
993
Christoph Hellwig3755a272015-10-03 15:32:44 +0200994CONFIGFS_ATTR(f_midi_opts_, id);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +0200995
996static struct configfs_attribute *midi_attrs[] = {
Christoph Hellwig3755a272015-10-03 15:32:44 +0200997 &f_midi_opts_attr_index,
998 &f_midi_opts_attr_buflen,
999 &f_midi_opts_attr_qlen,
1000 &f_midi_opts_attr_in_ports,
1001 &f_midi_opts_attr_out_ports,
1002 &f_midi_opts_attr_id,
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001003 NULL,
1004};
1005
1006static struct config_item_type midi_func_type = {
1007 .ct_item_ops = &midi_item_ops,
1008 .ct_attrs = midi_attrs,
1009 .ct_owner = THIS_MODULE,
1010};
1011
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001012static void f_midi_free_inst(struct usb_function_instance *f)
1013{
1014 struct f_midi_opts *opts;
1015
1016 opts = container_of(f, struct f_midi_opts, func_inst);
1017
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001018 if (opts->id_allocated)
1019 kfree(opts->id);
1020
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001021 kfree(opts);
1022}
1023
1024static struct usb_function_instance *f_midi_alloc_inst(void)
1025{
1026 struct f_midi_opts *opts;
1027
1028 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1029 if (!opts)
1030 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001031
1032 mutex_init(&opts->lock);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001033 opts->func_inst.free_func_inst = f_midi_free_inst;
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001034 opts->index = SNDRV_DEFAULT_IDX1;
1035 opts->id = SNDRV_DEFAULT_STR1;
1036 opts->buflen = 256;
1037 opts->qlen = 32;
1038 opts->in_ports = 1;
1039 opts->out_ports = 1;
1040
1041 config_group_init_type_name(&opts->func_inst.group, "",
1042 &midi_func_type);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001043
1044 return &opts->func_inst;
1045}
1046
1047static void f_midi_free(struct usb_function *f)
1048{
1049 struct f_midi *midi;
1050 struct f_midi_opts *opts;
1051 int i;
1052
1053 midi = func_to_midi(f);
1054 opts = container_of(f->fi, struct f_midi_opts, func_inst);
1055 kfree(midi->id);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001056 mutex_lock(&opts->lock);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001057 for (i = opts->in_ports - 1; i >= 0; --i)
1058 kfree(midi->in_port[i]);
1059 kfree(midi);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001060 --opts->refcnt;
1061 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001062}
1063
1064static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
1065{
1066 struct usb_composite_dev *cdev = f->config->cdev;
1067 struct f_midi *midi = func_to_midi(f);
1068 struct snd_card *card;
1069
1070 DBG(cdev, "unbind\n");
1071
1072 /* just to be sure */
1073 f_midi_disable(f);
1074
1075 card = midi->card;
1076 midi->card = NULL;
1077 if (card)
1078 snd_card_free(card);
1079
1080 usb_free_all_descriptors(f);
1081}
1082
Fengguang Wuf509fee2014-11-12 21:28:24 +08001083static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001084{
1085 struct f_midi *midi;
1086 struct f_midi_opts *opts;
1087 int status, i;
1088
1089 opts = container_of(fi, struct f_midi_opts, func_inst);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001090
1091 mutex_lock(&opts->lock);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001092 /* sanity check */
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001093 if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
1094 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001095 return ERR_PTR(-EINVAL);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001096 }
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001097
1098 /* allocate and initialize one new instance */
1099 midi = kzalloc(sizeof(*midi), GFP_KERNEL);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001100 if (!midi) {
1101 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001102 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001103 }
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001104
1105 for (i = 0; i < opts->in_ports; i++) {
1106 struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
1107
1108 if (!port) {
1109 status = -ENOMEM;
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001110 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001111 goto setup_fail;
1112 }
1113
1114 port->midi = midi;
1115 port->active = 0;
1116 port->cable = i;
1117 midi->in_port[i] = port;
1118 }
1119
1120 /* set up ALSA midi devices */
1121 midi->id = kstrdup(opts->id, GFP_KERNEL);
1122 if (opts->id && !midi->id) {
1123 status = -ENOMEM;
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001124 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczb2e2c942015-07-03 14:02:29 +02001125 goto setup_fail;
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001126 }
1127 midi->in_ports = opts->in_ports;
1128 midi->out_ports = opts->out_ports;
1129 midi->index = opts->index;
1130 midi->buflen = opts->buflen;
1131 midi->qlen = opts->qlen;
Andrzej Pietrasiewicz6f1de342014-10-16 13:33:31 +02001132 ++opts->refcnt;
1133 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001134
1135 midi->func.name = "gmidi function";
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001136 midi->func.bind = f_midi_bind;
1137 midi->func.unbind = f_midi_unbind;
1138 midi->func.set_alt = f_midi_set_alt;
1139 midi->func.disable = f_midi_disable;
1140 midi->func.free_func = f_midi_free;
1141
1142 return &midi->func;
1143
Andrzej Pietrasiewiczb85e9de2014-10-16 13:33:27 +02001144setup_fail:
1145 for (--i; i >= 0; i--)
1146 kfree(midi->in_port[i]);
1147 kfree(midi);
1148 return ERR_PTR(status);
1149}
1150
1151DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);