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