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