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