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