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