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