| 1 | |
| 2 | |
| 3 | * f_midi2.c -- USB MIDI 2.0 class function driver
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/device.h>
|
| 7 | #include <linux/kernel.h>
|
| 8 | #include <linux/module.h>
|
| 9 | #include <linux/slab.h>
|
| 10 |
|
| 11 | #include <sound/core.h>
|
| 12 | #include <sound/control.h>
|
| 13 | #include <sound/ump.h>
|
| 14 | #include <sound/ump_msg.h>
|
| 15 | #include <sound/ump_convert.h>
|
| 16 |
|
| 17 | #include <linux/usb/ch9.h>
|
| 18 | #include <linux/usb/func_utils.h>
|
| 19 | #include <linux/usb/gadget.h>
|
| 20 | #include <linux/usb/audio.h>
|
| 21 | #include <linux/usb/midi-v2.h>
|
| 22 |
|
| 23 | #include "u_midi2.h"
|
| 24 |
|
| 25 | struct f_midi2;
|
| 26 | struct f_midi2_ep;
|
| 27 | struct f_midi2_usb_ep;
|
| 28 |
|
| 29 |
|
| 30 | struct f_midi2_req_ctx {
|
| 31 | struct f_midi2_usb_ep *usb_ep;
|
| 32 | unsigned int index;
|
| 33 | struct usb_request *req;
|
| 34 | };
|
| 35 |
|
| 36 |
|
| 37 | struct f_midi2_usb_ep {
|
| 38 | struct f_midi2 *card;
|
| 39 | struct f_midi2_ep *ep;
|
| 40 | struct usb_ep *usb_ep;
|
| 41 | void (*complete)(struct usb_ep *usb_ep, struct usb_request *req);
|
| 42 | unsigned long free_reqs;
|
| 43 | unsigned int num_reqs;
|
| 44 | struct f_midi2_req_ctx *reqs;
|
| 45 | };
|
| 46 |
|
| 47 |
|
| 48 | struct f_midi2_block {
|
| 49 | struct f_midi2_block_info info;
|
| 50 | struct snd_ump_block *fb;
|
| 51 | unsigned int gtb_id;
|
| 52 | unsigned int string_id;
|
| 53 | };
|
| 54 |
|
| 55 |
|
| 56 | struct f_midi2_midi1_port {
|
| 57 | unsigned int pending;
|
| 58 | u8 buf[32];
|
| 59 | u8 state;
|
| 60 | u8 data[2];
|
| 61 | };
|
| 62 |
|
| 63 |
|
| 64 | enum {
|
| 65 | STATE_INITIAL = 0,
|
| 66 | STATE_1PARAM,
|
| 67 | STATE_2PARAM_1,
|
| 68 | STATE_2PARAM_2,
|
| 69 | STATE_SYSEX_0,
|
| 70 | STATE_SYSEX_1,
|
| 71 | STATE_SYSEX_2,
|
| 72 | STATE_REAL_TIME,
|
| 73 | STATE_FINISHED,
|
| 74 | };
|
| 75 |
|
| 76 |
|
| 77 | struct f_midi2_ep {
|
| 78 | struct snd_ump_endpoint *ump;
|
| 79 | struct f_midi2 *card;
|
| 80 |
|
| 81 | struct f_midi2_ep_info info;
|
| 82 | unsigned int num_blks;
|
| 83 | struct f_midi2_block blks[SNDRV_UMP_MAX_BLOCKS];
|
| 84 |
|
| 85 | struct f_midi2_usb_ep ep_in;
|
| 86 | struct f_midi2_usb_ep ep_out;
|
| 87 |
|
| 88 | u8 in_group_to_cable[SNDRV_UMP_MAX_GROUPS];
|
| 89 | };
|
| 90 |
|
| 91 |
|
| 92 | enum {
|
| 93 | STR_IFACE = 0,
|
| 94 | STR_GTB1 = 1,
|
| 95 | };
|
| 96 |
|
| 97 |
|
| 98 | #define gtb_to_str_id(id) (STR_GTB1 + (id) - 1)
|
| 99 |
|
| 100 |
|
| 101 | struct midi1_cable_mapping {
|
| 102 | struct f_midi2_ep *ep;
|
| 103 | unsigned char block;
|
| 104 | unsigned char group;
|
| 105 | };
|
| 106 |
|
| 107 |
|
| 108 | enum {
|
| 109 | MIDI_OP_MODE_UNSET,
|
| 110 | MIDI_OP_MODE_MIDI1,
|
| 111 | MIDI_OP_MODE_MIDI2,
|
| 112 | };
|
| 113 |
|
| 114 |
|
| 115 | struct f_midi2 {
|
| 116 | struct usb_function func;
|
| 117 | struct usb_gadget *gadget;
|
| 118 | struct snd_card *card;
|
| 119 |
|
| 120 |
|
| 121 | struct f_midi2_usb_ep midi1_ep_in;
|
| 122 | struct f_midi2_usb_ep midi1_ep_out;
|
| 123 |
|
| 124 |
|
| 125 | unsigned int num_midi1_in;
|
| 126 | unsigned int num_midi1_out;
|
| 127 |
|
| 128 |
|
| 129 | struct f_midi2_midi1_port midi1_port[MAX_CABLES];
|
| 130 |
|
| 131 | struct ump_cvt_to_ump midi1_ump_cvt;
|
| 132 |
|
| 133 | struct midi1_cable_mapping in_cable_mapping[MAX_CABLES];
|
| 134 | struct midi1_cable_mapping out_cable_mapping[MAX_CABLES];
|
| 135 |
|
| 136 | int midi_if;
|
| 137 | int operation_mode;
|
| 138 |
|
| 139 | spinlock_t queue_lock;
|
| 140 |
|
| 141 | struct f_midi2_card_info info;
|
| 142 |
|
| 143 | unsigned int num_eps;
|
| 144 | struct f_midi2_ep midi2_eps[MAX_UMP_EPS];
|
| 145 |
|
| 146 | unsigned int total_blocks;
|
| 147 | struct usb_string *string_defs;
|
| 148 | struct usb_string *strings;
|
| 149 | };
|
| 150 |
|
| 151 | #define func_to_midi2(f) container_of(f, struct f_midi2, func)
|
| 152 |
|
| 153 |
|
| 154 | #define to_ump_protocol(v) (((v) & 3) << 8)
|
| 155 |
|
| 156 |
|
| 157 | static const char *ump_ep_name(const struct f_midi2_ep *ep)
|
| 158 | {
|
| 159 | return ep->info.ep_name ? ep->info.ep_name : "MIDI 2.0 Gadget";
|
| 160 | }
|
| 161 |
|
| 162 |
|
| 163 | static const char *ump_product_id(const struct f_midi2_ep *ep)
|
| 164 | {
|
| 165 | return ep->info.product_id ? ep->info.product_id : "Unique Product ID";
|
| 166 | }
|
| 167 |
|
| 168 |
|
| 169 | static const char *ump_fb_name(const struct f_midi2_block_info *info)
|
| 170 | {
|
| 171 | return info->name ? info->name : "MIDI 2.0 Gadget I/O";
|
| 172 | }
|
| 173 |
|
| 174 | |
| 175 | * USB Descriptor Definitions
|
| 176 |
|
| 177 |
|
| 178 | static struct usb_ms20_gr_trm_block_header_descriptor gtb_header_desc = {
|
| 179 | .bLength = sizeof(gtb_header_desc),
|
| 180 | .bDescriptorType = USB_DT_CS_GR_TRM_BLOCK,
|
| 181 | .bDescriptorSubtype = USB_MS_GR_TRM_BLOCK_HEADER,
|
| 182 | .wTotalLength = __cpu_to_le16(0x12), |
| 183 | };
|
| 184 |
|
| 185 |
|
| 186 | static struct usb_ms20_gr_trm_block_descriptor gtb_desc = {
|
| 187 | .bLength = sizeof(gtb_desc),
|
| 188 | .bDescriptorType = USB_DT_CS_GR_TRM_BLOCK,
|
| 189 | .bDescriptorSubtype = USB_MS_GR_TRM_BLOCK,
|
| 190 | .bGrpTrmBlkID = 0x01,
|
| 191 | .bGrpTrmBlkType = USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL,
|
| 192 | .nGroupTrm = 0x00,
|
| 193 | .nNumGroupTrm = 1,
|
| 194 | .iBlockItem = 0,
|
| 195 | .bMIDIProtocol = USB_MS_MIDI_PROTO_1_0_64,
|
| 196 | .wMaxInputBandwidth = 0,
|
| 197 | .wMaxOutputBandwidth = 0,
|
| 198 | };
|
| 199 |
|
| 200 | DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
|
| 201 | DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
|
| 202 | DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
|
| 203 | DECLARE_USB_MS20_ENDPOINT_DESCRIPTOR(32);
|
| 204 |
|
| 205 | #define EP_MAX_PACKET_INT 8
|
| 206 |
|
| 207 |
|
| 208 | static struct usb_interface_descriptor midi2_audio_if_desc = {
|
| 209 | .bLength = USB_DT_INTERFACE_SIZE,
|
| 210 | .bDescriptorType = USB_DT_INTERFACE,
|
| 211 | .bInterfaceNumber = 0, |
| 212 | .bNumEndpoints = 0,
|
| 213 | .bInterfaceClass = USB_CLASS_AUDIO,
|
| 214 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
|
| 215 | .bInterfaceProtocol = 0,
|
| 216 | .iInterface = 0,
|
| 217 | };
|
| 218 |
|
| 219 | static struct uac1_ac_header_descriptor_1 midi2_audio_class_desc = {
|
| 220 | .bLength = 0x09,
|
| 221 | .bDescriptorType = USB_DT_CS_INTERFACE,
|
| 222 | .bDescriptorSubtype = 0x01,
|
| 223 | .bcdADC = __cpu_to_le16(0x0100),
|
| 224 | .wTotalLength = __cpu_to_le16(0x0009),
|
| 225 | .bInCollection = 0x01,
|
| 226 | .baInterfaceNr = { 0x01 }, |
| 227 | };
|
| 228 |
|
| 229 |
|
| 230 | static struct usb_interface_descriptor midi2_midi1_if_desc = {
|
| 231 | .bLength = USB_DT_INTERFACE_SIZE,
|
| 232 | .bDescriptorType = USB_DT_INTERFACE,
|
| 233 | .bInterfaceNumber = 0, |
| 234 | .bAlternateSetting = 0,
|
| 235 | .bNumEndpoints = 2, |
| 236 | .bInterfaceClass = USB_CLASS_AUDIO,
|
| 237 | .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
|
| 238 | .bInterfaceProtocol = 0,
|
| 239 | .iInterface = 0, |
| 240 | };
|
| 241 |
|
| 242 | static struct usb_ms_header_descriptor midi2_midi1_class_desc = {
|
| 243 | .bLength = 0x07,
|
| 244 | .bDescriptorType = USB_DT_CS_INTERFACE,
|
| 245 | .bDescriptorSubtype = USB_MS_HEADER,
|
| 246 | .bcdMSC = __cpu_to_le16(0x0100),
|
| 247 | .wTotalLength = __cpu_to_le16(0x41), |
| 248 | };
|
| 249 |
|
| 250 |
|
| 251 | static struct usb_endpoint_descriptor midi2_midi1_ep_out_desc = {
|
| 252 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
|
| 253 | .bDescriptorType = USB_DT_ENDPOINT,
|
| 254 | .bEndpointAddress = USB_DIR_OUT | 0, |
| 255 | .bmAttributes = USB_ENDPOINT_XFER_BULK,
|
| 256 | };
|
| 257 |
|
| 258 | static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_out_ss_comp_desc = {
|
| 259 | .bLength = sizeof(midi2_midi1_ep_out_ss_comp_desc),
|
| 260 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
| 261 | };
|
| 262 |
|
| 263 | static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_out_class_desc = {
|
| 264 | .bLength = 0x05, |
| 265 | .bDescriptorType = USB_DT_CS_ENDPOINT,
|
| 266 | .bDescriptorSubtype = USB_MS_GENERAL,
|
| 267 | .bNumEmbMIDIJack = 1,
|
| 268 | .baAssocJackID = { 0x01 },
|
| 269 | };
|
| 270 |
|
| 271 |
|
| 272 | static struct usb_endpoint_descriptor midi2_midi1_ep_in_desc = {
|
| 273 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
|
| 274 | .bDescriptorType = USB_DT_ENDPOINT,
|
| 275 | .bEndpointAddress = USB_DIR_IN | 0, |
| 276 | .bmAttributes = USB_ENDPOINT_XFER_BULK,
|
| 277 | };
|
| 278 |
|
| 279 | static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_in_ss_comp_desc = {
|
| 280 | .bLength = sizeof(midi2_midi1_ep_in_ss_comp_desc),
|
| 281 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
| 282 | };
|
| 283 |
|
| 284 | static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_in_class_desc = {
|
| 285 | .bLength = 0x05, |
| 286 | .bDescriptorType = USB_DT_CS_ENDPOINT,
|
| 287 | .bDescriptorSubtype = USB_MS_GENERAL,
|
| 288 | .bNumEmbMIDIJack = 1,
|
| 289 | .baAssocJackID = { 0x03 },
|
| 290 | };
|
| 291 |
|
| 292 |
|
| 293 | static struct usb_interface_descriptor midi2_midi2_if_desc = {
|
| 294 | .bLength = USB_DT_INTERFACE_SIZE,
|
| 295 | .bDescriptorType = USB_DT_INTERFACE,
|
| 296 | .bInterfaceNumber = 0, |
| 297 | .bAlternateSetting = 1,
|
| 298 | .bNumEndpoints = 2, |
| 299 | .bInterfaceClass = USB_CLASS_AUDIO,
|
| 300 | .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
|
| 301 | .bInterfaceProtocol = 0,
|
| 302 | .iInterface = 0, |
| 303 | };
|
| 304 |
|
| 305 | static struct usb_ms_header_descriptor midi2_midi2_class_desc = {
|
| 306 | .bLength = 0x07,
|
| 307 | .bDescriptorType = USB_DT_CS_INTERFACE,
|
| 308 | .bDescriptorSubtype = USB_MS_HEADER,
|
| 309 | .bcdMSC = __cpu_to_le16(0x0200),
|
| 310 | .wTotalLength = __cpu_to_le16(0x07),
|
| 311 | };
|
| 312 |
|
| 313 |
|
| 314 | static struct usb_endpoint_descriptor midi2_midi2_ep_out_desc[MAX_UMP_EPS];
|
| 315 |
|
| 316 | static struct usb_ss_ep_comp_descriptor midi2_midi2_ep_out_ss_comp_desc = {
|
| 317 | .bLength = sizeof(midi2_midi1_ep_out_ss_comp_desc),
|
| 318 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
| 319 | };
|
| 320 |
|
| 321 | static struct usb_ms20_endpoint_descriptor_32 midi2_midi2_ep_out_class_desc[MAX_UMP_EPS];
|
| 322 |
|
| 323 |
|
| 324 | static struct usb_endpoint_descriptor midi2_midi2_ep_in_desc[MAX_UMP_EPS];
|
| 325 |
|
| 326 | static struct usb_ss_ep_comp_descriptor midi2_midi2_ep_in_ss_comp_desc = {
|
| 327 | .bLength = sizeof(midi2_midi2_ep_in_ss_comp_desc),
|
| 328 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
| 329 | };
|
| 330 |
|
| 331 | static struct usb_ms20_endpoint_descriptor_32 midi2_midi2_ep_in_class_desc[MAX_UMP_EPS];
|
| 332 |
|
| 333 |
|
| 334 | static void *midi2_audio_descs[] = {
|
| 335 | &midi2_audio_if_desc,
|
| 336 | &midi2_audio_class_desc,
|
| 337 | NULL
|
| 338 | };
|
| 339 |
|
| 340 | static void *midi2_midi1_descs[] = {
|
| 341 | &midi2_midi1_if_desc,
|
| 342 | &midi2_midi1_class_desc,
|
| 343 | NULL
|
| 344 | };
|
| 345 |
|
| 346 | static void *midi2_midi1_ep_out_descs[] = {
|
| 347 | &midi2_midi1_ep_out_desc,
|
| 348 | &midi2_midi1_ep_out_class_desc,
|
| 349 | NULL
|
| 350 | };
|
| 351 |
|
| 352 | static void *midi2_midi1_ep_in_descs[] = {
|
| 353 | &midi2_midi1_ep_in_desc,
|
| 354 | &midi2_midi1_ep_in_class_desc,
|
| 355 | NULL
|
| 356 | };
|
| 357 |
|
| 358 | static void *midi2_midi1_ep_out_ss_descs[] = {
|
| 359 | &midi2_midi1_ep_out_desc,
|
| 360 | &midi2_midi1_ep_out_ss_comp_desc,
|
| 361 | &midi2_midi1_ep_out_class_desc,
|
| 362 | NULL
|
| 363 | };
|
| 364 |
|
| 365 | static void *midi2_midi1_ep_in_ss_descs[] = {
|
| 366 | &midi2_midi1_ep_in_desc,
|
| 367 | &midi2_midi1_ep_in_ss_comp_desc,
|
| 368 | &midi2_midi1_ep_in_class_desc,
|
| 369 | NULL
|
| 370 | };
|
| 371 |
|
| 372 | static void *midi2_midi2_descs[] = {
|
| 373 | &midi2_midi2_if_desc,
|
| 374 | &midi2_midi2_class_desc,
|
| 375 | NULL
|
| 376 | };
|
| 377 |
|
| 378 | |
| 379 | * USB request handling
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 | static struct usb_request *get_empty_request(struct f_midi2_usb_ep *usb_ep)
|
| 384 | {
|
| 385 | struct usb_request *req = NULL;
|
| 386 | unsigned long flags;
|
| 387 | int index;
|
| 388 |
|
| 389 | spin_lock_irqsave(&usb_ep->card->queue_lock, flags);
|
| 390 | if (!usb_ep->free_reqs)
|
| 391 | goto unlock;
|
| 392 | index = find_first_bit(&usb_ep->free_reqs, usb_ep->num_reqs);
|
| 393 | if (index >= usb_ep->num_reqs)
|
| 394 | goto unlock;
|
| 395 | req = usb_ep->reqs[index].req;
|
| 396 | if (!req)
|
| 397 | goto unlock;
|
| 398 | clear_bit(index, &usb_ep->free_reqs);
|
| 399 | req->length = 0;
|
| 400 | unlock:
|
| 401 | spin_unlock_irqrestore(&usb_ep->card->queue_lock, flags);
|
| 402 | return req;
|
| 403 | }
|
| 404 |
|
| 405 |
|
| 406 | static void put_empty_request(struct usb_request *req)
|
| 407 | {
|
| 408 | struct f_midi2_req_ctx *ctx = req->context;
|
| 409 | unsigned long flags;
|
| 410 |
|
| 411 | spin_lock_irqsave(&ctx->usb_ep->card->queue_lock, flags);
|
| 412 | set_bit(ctx->index, &ctx->usb_ep->free_reqs);
|
| 413 | spin_unlock_irqrestore(&ctx->usb_ep->card->queue_lock, flags);
|
| 414 | }
|
| 415 |
|
| 416 | |
| 417 | * UMP v1.1 Stream message handling
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 | static int queue_request_ep_raw(struct usb_request *req)
|
| 422 | {
|
| 423 | struct f_midi2_req_ctx *ctx = req->context;
|
| 424 | int err;
|
| 425 |
|
| 426 | req->complete = ctx->usb_ep->complete;
|
| 427 | err = usb_ep_queue(ctx->usb_ep->usb_ep, req, GFP_ATOMIC);
|
| 428 | if (err) {
|
| 429 | put_empty_request(req);
|
| 430 | return err;
|
| 431 | }
|
| 432 | return 0;
|
| 433 | }
|
| 434 |
|
| 435 |
|
| 436 | static int queue_request_ep_in(struct usb_request *req)
|
| 437 | {
|
| 438 |
|
| 439 | cpu_to_le32_array((u32 *)req->buf, req->length >> 2);
|
| 440 | return queue_request_ep_raw(req);
|
| 441 | }
|
| 442 |
|
| 443 |
|
| 444 | static int reply_ep_in(struct f_midi2_ep *ep, const void *buf, int len)
|
| 445 | {
|
| 446 | struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
|
| 447 | struct usb_request *req;
|
| 448 |
|
| 449 | req = get_empty_request(usb_ep);
|
| 450 | if (!req)
|
| 451 | return -ENOSPC;
|
| 452 |
|
| 453 | req->length = len;
|
| 454 | memcpy(req->buf, buf, len);
|
| 455 | return queue_request_ep_in(req);
|
| 456 | }
|
| 457 |
|
| 458 |
|
| 459 | static void reply_ump_stream_ep_info(struct f_midi2_ep *ep)
|
| 460 | {
|
| 461 | struct snd_ump_stream_msg_ep_info rep = {
|
| 462 | .type = UMP_MSG_TYPE_STREAM,
|
| 463 | .status = UMP_STREAM_MSG_STATUS_EP_INFO,
|
| 464 | .ump_version_major = 0x01,
|
| 465 | .ump_version_minor = 0x01,
|
| 466 | .num_function_blocks = ep->num_blks,
|
| 467 | .static_function_block = !!ep->card->info.static_block,
|
| 468 | .protocol = (UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 |
|
| 469 | UMP_STREAM_MSG_EP_INFO_CAP_MIDI2) >> 8,
|
| 470 | };
|
| 471 |
|
| 472 | reply_ep_in(ep, &rep, sizeof(rep));
|
| 473 | }
|
| 474 |
|
| 475 |
|
| 476 | static void reply_ump_stream_ep_device(struct f_midi2_ep *ep)
|
| 477 | {
|
| 478 | struct snd_ump_stream_msg_device_info rep = {
|
| 479 | .type = UMP_MSG_TYPE_STREAM,
|
| 480 | .status = UMP_STREAM_MSG_STATUS_DEVICE_INFO,
|
| 481 | .manufacture_id = ep->info.manufacturer,
|
| 482 | .family_lsb = ep->info.family & 0xff,
|
| 483 | .family_msb = (ep->info.family >> 8) & 0xff,
|
| 484 | .model_lsb = ep->info.model & 0xff,
|
| 485 | .model_msb = (ep->info.model >> 8) & 0xff,
|
| 486 | .sw_revision = ep->info.sw_revision,
|
| 487 | };
|
| 488 |
|
| 489 | reply_ep_in(ep, &rep, sizeof(rep));
|
| 490 | }
|
| 491 |
|
| 492 | #define UMP_STREAM_PKT_BYTES 16
|
| 493 | #define UMP_STREAM_EP_STR_OFF 2
|
| 494 | #define UMP_STREAM_FB_STR_OFF 3
|
| 495 |
|
| 496 |
|
| 497 | static void reply_ump_stream_string(struct f_midi2_ep *ep, const u8 *name,
|
| 498 | unsigned int type, unsigned int extra,
|
| 499 | unsigned int start_ofs)
|
| 500 | {
|
| 501 | struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
|
| 502 | struct f_midi2 *midi2 = ep->card;
|
| 503 | struct usb_request *req;
|
| 504 | unsigned int pos;
|
| 505 | u32 *buf;
|
| 506 |
|
| 507 | if (!*name)
|
| 508 | return;
|
| 509 | req = get_empty_request(usb_ep);
|
| 510 | if (!req)
|
| 511 | return;
|
| 512 |
|
| 513 | buf = (u32 *)req->buf;
|
| 514 | pos = start_ofs;
|
| 515 | for (;;) {
|
| 516 | if (pos == start_ofs) {
|
| 517 | memset(buf, 0, UMP_STREAM_PKT_BYTES);
|
| 518 | buf[0] = ump_stream_compose(type, 0) | extra;
|
| 519 | }
|
| 520 | buf[pos / 4] |= *name++ << ((3 - (pos % 4)) * 8);
|
| 521 | if (!*name) {
|
| 522 | if (req->length)
|
| 523 | buf[0] |= UMP_STREAM_MSG_FORMAT_END << 26;
|
| 524 | req->length += UMP_STREAM_PKT_BYTES;
|
| 525 | break;
|
| 526 | }
|
| 527 | if (++pos == UMP_STREAM_PKT_BYTES) {
|
| 528 | if (!req->length)
|
| 529 | buf[0] |= UMP_STREAM_MSG_FORMAT_START << 26;
|
| 530 | else
|
| 531 | buf[0] |= UMP_STREAM_MSG_FORMAT_CONTINUE << 26;
|
| 532 | req->length += UMP_STREAM_PKT_BYTES;
|
| 533 | if (midi2->info.req_buf_size - req->length < UMP_STREAM_PKT_BYTES)
|
| 534 | break;
|
| 535 | buf += 4;
|
| 536 | pos = start_ofs;
|
| 537 | }
|
| 538 | }
|
| 539 |
|
| 540 | if (req->length)
|
| 541 | queue_request_ep_in(req);
|
| 542 | else
|
| 543 | put_empty_request(req);
|
| 544 | }
|
| 545 |
|
| 546 |
|
| 547 | static void reply_ump_stream_ep_name(struct f_midi2_ep *ep)
|
| 548 | {
|
| 549 | reply_ump_stream_string(ep, ump_ep_name(ep),
|
| 550 | UMP_STREAM_MSG_STATUS_EP_NAME, 0,
|
| 551 | UMP_STREAM_EP_STR_OFF);
|
| 552 | }
|
| 553 |
|
| 554 |
|
| 555 | static void reply_ump_stream_ep_pid(struct f_midi2_ep *ep)
|
| 556 | {
|
| 557 | reply_ump_stream_string(ep, ump_product_id(ep),
|
| 558 | UMP_STREAM_MSG_STATUS_PRODUCT_ID, 0,
|
| 559 | UMP_STREAM_EP_STR_OFF);
|
| 560 | }
|
| 561 |
|
| 562 |
|
| 563 | static void reply_ump_stream_ep_config(struct f_midi2_ep *ep)
|
| 564 | {
|
| 565 | struct snd_ump_stream_msg_stream_cfg rep = {
|
| 566 | .type = UMP_MSG_TYPE_STREAM,
|
| 567 | .status = UMP_STREAM_MSG_STATUS_STREAM_CFG,
|
| 568 | };
|
| 569 |
|
| 570 | if (ep->info.protocol == 2)
|
| 571 | rep.protocol = UMP_STREAM_MSG_EP_INFO_CAP_MIDI2 >> 8;
|
| 572 | else
|
| 573 | rep.protocol = UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 >> 8;
|
| 574 |
|
| 575 | reply_ep_in(ep, &rep, sizeof(rep));
|
| 576 | }
|
| 577 |
|
| 578 |
|
| 579 | static void reply_ump_stream_fb_info(struct f_midi2_ep *ep, int blk)
|
| 580 | {
|
| 581 | struct f_midi2_block_info *b = &ep->blks[blk].info;
|
| 582 | struct snd_ump_stream_msg_fb_info rep = {
|
| 583 | .type = UMP_MSG_TYPE_STREAM,
|
| 584 | .status = UMP_STREAM_MSG_STATUS_FB_INFO,
|
| 585 | .active = !!b->active,
|
| 586 | .function_block_id = blk,
|
| 587 | .ui_hint = b->ui_hint,
|
| 588 | .midi_10 = b->is_midi1,
|
| 589 | .direction = b->direction,
|
| 590 | .first_group = b->first_group,
|
| 591 | .num_groups = b->num_groups,
|
| 592 | .midi_ci_version = b->midi_ci_version,
|
| 593 | .sysex8_streams = b->sysex8_streams,
|
| 594 | };
|
| 595 |
|
| 596 | reply_ep_in(ep, &rep, sizeof(rep));
|
| 597 | }
|
| 598 |
|
| 599 |
|
| 600 | static void reply_ump_stream_fb_name(struct f_midi2_ep *ep, unsigned int blk)
|
| 601 | {
|
| 602 | reply_ump_stream_string(ep, ump_fb_name(&ep->blks[blk].info),
|
| 603 | UMP_STREAM_MSG_STATUS_FB_NAME, blk << 8,
|
| 604 | UMP_STREAM_FB_STR_OFF);
|
| 605 | }
|
| 606 |
|
| 607 |
|
| 608 | static void process_ump_stream_msg(struct f_midi2_ep *ep, const u32 *data)
|
| 609 | {
|
| 610 | struct f_midi2 *midi2 = ep->card;
|
| 611 | unsigned int format, status, blk;
|
| 612 |
|
| 613 | format = ump_stream_message_format(*data);
|
| 614 | status = ump_stream_message_status(*data);
|
| 615 | switch (status) {
|
| 616 | case UMP_STREAM_MSG_STATUS_EP_DISCOVERY:
|
| 617 | if (format)
|
| 618 | return; |
| 619 | if (data[1] & UMP_STREAM_MSG_REQUEST_EP_INFO)
|
| 620 | reply_ump_stream_ep_info(ep);
|
| 621 | if (data[1] & UMP_STREAM_MSG_REQUEST_DEVICE_INFO)
|
| 622 | reply_ump_stream_ep_device(ep);
|
| 623 | if (data[1] & UMP_STREAM_MSG_REQUEST_EP_NAME)
|
| 624 | reply_ump_stream_ep_name(ep);
|
| 625 | if (data[1] & UMP_STREAM_MSG_REQUEST_PRODUCT_ID)
|
| 626 | reply_ump_stream_ep_pid(ep);
|
| 627 | if (data[1] & UMP_STREAM_MSG_REQUEST_STREAM_CFG)
|
| 628 | reply_ump_stream_ep_config(ep);
|
| 629 | return;
|
| 630 | case UMP_STREAM_MSG_STATUS_STREAM_CFG_REQUEST:
|
| 631 | if (*data & UMP_STREAM_MSG_EP_INFO_CAP_MIDI2) {
|
| 632 | ep->info.protocol = 2;
|
| 633 | DBG(midi2, "Switching Protocol to MIDI2\n");
|
| 634 | } else {
|
| 635 | ep->info.protocol = 1;
|
| 636 | DBG(midi2, "Switching Protocol to MIDI1\n");
|
| 637 | }
|
| 638 | snd_ump_switch_protocol(ep->ump, to_ump_protocol(ep->info.protocol));
|
| 639 | reply_ump_stream_ep_config(ep);
|
| 640 | return;
|
| 641 | case UMP_STREAM_MSG_STATUS_FB_DISCOVERY:
|
| 642 | if (format)
|
| 643 | return; |
| 644 | blk = (*data >> 8) & 0xff;
|
| 645 | if (blk == 0xff) {
|
| 646 |
|
| 647 | for (blk = 0; blk < ep->num_blks; blk++) {
|
| 648 | if (*data & UMP_STREAM_MSG_REQUEST_FB_INFO)
|
| 649 | reply_ump_stream_fb_info(ep, blk);
|
| 650 | if (*data & UMP_STREAM_MSG_REQUEST_FB_NAME)
|
| 651 | reply_ump_stream_fb_name(ep, blk);
|
| 652 | }
|
| 653 | } else if (blk < ep->num_blks) {
|
| 654 |
|
| 655 | if (*data & UMP_STREAM_MSG_REQUEST_FB_INFO)
|
| 656 | reply_ump_stream_fb_info(ep, blk);
|
| 657 | if (*data & UMP_STREAM_MSG_REQUEST_FB_NAME)
|
| 658 | reply_ump_stream_fb_name(ep, blk);
|
| 659 | }
|
| 660 | return;
|
| 661 | }
|
| 662 | }
|
| 663 |
|
| 664 |
|
| 665 | static void process_ump(struct f_midi2_ep *ep, const struct usb_request *req)
|
| 666 | {
|
| 667 | const u32 *data = (u32 *)req->buf;
|
| 668 | int len = req->actual >> 2;
|
| 669 | const u32 *in_buf = ep->ump->input_buf;
|
| 670 |
|
| 671 | for (; len > 0; len--, data++) {
|
| 672 | if (snd_ump_receive_ump_val(ep->ump, *data) <= 0)
|
| 673 | continue;
|
| 674 | if (ump_message_type(*in_buf) == UMP_MSG_TYPE_STREAM)
|
| 675 | process_ump_stream_msg(ep, in_buf);
|
| 676 | }
|
| 677 | }
|
| 678 |
|
| 679 | |
| 680 | * MIDI 2.0 UMP USB request handling
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 | static void f_midi2_ep_out_complete(struct usb_ep *usb_ep,
|
| 685 | struct usb_request *req)
|
| 686 | {
|
| 687 | struct f_midi2_req_ctx *ctx = req->context;
|
| 688 | struct f_midi2_ep *ep = ctx->usb_ep->ep;
|
| 689 | struct f_midi2 *midi2 = ep->card;
|
| 690 | int status = req->status;
|
| 691 |
|
| 692 | if (status) {
|
| 693 | DBG(midi2, "%s complete error %d: %d/%d\n",
|
| 694 | usb_ep->name, status, req->actual, req->length);
|
| 695 | goto error;
|
| 696 | }
|
| 697 |
|
| 698 |
|
| 699 | le32_to_cpu_array((u32 *)req->buf, req->actual >> 2);
|
| 700 |
|
| 701 | if (midi2->info.process_ump)
|
| 702 | process_ump(ep, req);
|
| 703 |
|
| 704 | snd_ump_receive(ep->ump, req->buf, req->actual & ~3);
|
| 705 |
|
| 706 | if (midi2->operation_mode != MIDI_OP_MODE_MIDI2)
|
| 707 | goto error;
|
| 708 |
|
| 709 | if (queue_request_ep_raw(req))
|
| 710 | goto error;
|
| 711 | return;
|
| 712 |
|
| 713 | error:
|
| 714 | put_empty_request(req);
|
| 715 | }
|
| 716 |
|
| 717 |
|
| 718 | static void process_ump_transmit(struct f_midi2_ep *ep)
|
| 719 | {
|
| 720 | struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
|
| 721 | struct f_midi2 *midi2 = ep->card;
|
| 722 | struct usb_request *req;
|
| 723 | int len;
|
| 724 |
|
| 725 | if (!usb_ep->usb_ep->enabled)
|
| 726 | return;
|
| 727 |
|
| 728 | for (;;) {
|
| 729 | req = get_empty_request(usb_ep);
|
| 730 | if (!req)
|
| 731 | break;
|
| 732 | len = snd_ump_transmit(ep->ump, (u32 *)req->buf,
|
| 733 | midi2->info.req_buf_size);
|
| 734 | if (len <= 0) {
|
| 735 | put_empty_request(req);
|
| 736 | break;
|
| 737 | }
|
| 738 |
|
| 739 | req->length = len;
|
| 740 | if (queue_request_ep_in(req) < 0)
|
| 741 | break;
|
| 742 | }
|
| 743 | }
|
| 744 |
|
| 745 |
|
| 746 | static void f_midi2_ep_in_complete(struct usb_ep *usb_ep,
|
| 747 | struct usb_request *req)
|
| 748 | {
|
| 749 | struct f_midi2_req_ctx *ctx = req->context;
|
| 750 | struct f_midi2_ep *ep = ctx->usb_ep->ep;
|
| 751 | struct f_midi2 *midi2 = ep->card;
|
| 752 | int status = req->status;
|
| 753 |
|
| 754 | put_empty_request(req);
|
| 755 |
|
| 756 | if (status) {
|
| 757 | DBG(midi2, "%s complete error %d: %d/%d\n",
|
| 758 | usb_ep->name, status, req->actual, req->length);
|
| 759 | return;
|
| 760 | }
|
| 761 |
|
| 762 | process_ump_transmit(ep);
|
| 763 | }
|
| 764 |
|
| 765 | |
| 766 | * MIDI1 (altset 0) USB request handling
|
| 767 |
|
| 768 |
|
| 769 | |
| 770 | *
|
| 771 | * fill the packet or request if needed
|
| 772 | * returns true if the request became empty (queued)
|
| 773 |
|
| 774 | static bool process_midi1_byte(struct f_midi2 *midi2, u8 cable, u8 b,
|
| 775 | struct usb_request **req_p)
|
| 776 | {
|
| 777 | struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
|
| 778 | u8 p[4] = { cable << 4, 0, 0, 0 };
|
| 779 | int next_state = STATE_INITIAL;
|
| 780 | struct usb_request *req = *req_p;
|
| 781 |
|
| 782 | switch (b) {
|
| 783 | case 0xf8 ... 0xff:
|
| 784 |
|
| 785 | p[0] |= 0x0f;
|
| 786 | p[1] = b;
|
| 787 | next_state = port->state;
|
| 788 | port->state = STATE_REAL_TIME;
|
| 789 | break;
|
| 790 |
|
| 791 | case 0xf7:
|
| 792 |
|
| 793 | switch (port->state) {
|
| 794 | case STATE_SYSEX_0:
|
| 795 | p[0] |= 0x05;
|
| 796 | p[1] = 0xf7;
|
| 797 | next_state = STATE_FINISHED;
|
| 798 | break;
|
| 799 | case STATE_SYSEX_1:
|
| 800 | p[0] |= 0x06;
|
| 801 | p[1] = port->data[0];
|
| 802 | p[2] = 0xf7;
|
| 803 | next_state = STATE_FINISHED;
|
| 804 | break;
|
| 805 | case STATE_SYSEX_2:
|
| 806 | p[0] |= 0x07;
|
| 807 | p[1] = port->data[0];
|
| 808 | p[2] = port->data[1];
|
| 809 | p[3] = 0xf7;
|
| 810 | next_state = STATE_FINISHED;
|
| 811 | break;
|
| 812 | default:
|
| 813 |
|
| 814 | next_state = port->state;
|
| 815 | port->state = STATE_INITIAL;
|
| 816 | }
|
| 817 | break;
|
| 818 |
|
| 819 | case 0xf0 ... 0xf6:
|
| 820 |
|
| 821 | port->data[0] = port->data[1] = 0;
|
| 822 | port->state = STATE_INITIAL;
|
| 823 | switch (b) {
|
| 824 | case 0xf0:
|
| 825 | port->data[0] = b;
|
| 826 | port->data[1] = 0;
|
| 827 | next_state = STATE_SYSEX_1;
|
| 828 | break;
|
| 829 | case 0xf1:
|
| 830 | case 0xf3:
|
| 831 | port->data[0] = b;
|
| 832 | next_state = STATE_1PARAM;
|
| 833 | break;
|
| 834 | case 0xf2:
|
| 835 | port->data[0] = b;
|
| 836 | next_state = STATE_2PARAM_1;
|
| 837 | break;
|
| 838 | case 0xf4:
|
| 839 | case 0xf5:
|
| 840 | next_state = STATE_INITIAL;
|
| 841 | break;
|
| 842 | case 0xf6:
|
| 843 | p[0] |= 0x05;
|
| 844 | p[1] = 0xf6;
|
| 845 | next_state = STATE_FINISHED;
|
| 846 | break;
|
| 847 | }
|
| 848 | break;
|
| 849 |
|
| 850 | case 0x80 ... 0xef:
|
| 851 | |
| 852 | * Channel Voice Messages, Channel Mode Messages
|
| 853 | * and Control Change Messages.
|
| 854 |
|
| 855 | port->data[0] = b;
|
| 856 | port->data[1] = 0;
|
| 857 | port->state = STATE_INITIAL;
|
| 858 | if (b >= 0xc0 && b <= 0xdf)
|
| 859 | next_state = STATE_1PARAM;
|
| 860 | else
|
| 861 | next_state = STATE_2PARAM_1;
|
| 862 | break;
|
| 863 |
|
| 864 | case 0x00 ... 0x7f:
|
| 865 |
|
| 866 | switch (port->state) {
|
| 867 | case STATE_1PARAM:
|
| 868 | if (port->data[0] < 0xf0)
|
| 869 | p[0] |= port->data[0] >> 4;
|
| 870 | else
|
| 871 | p[0] |= 0x02;
|
| 872 |
|
| 873 | p[1] = port->data[0];
|
| 874 | p[2] = b;
|
| 875 |
|
| 876 | next_state = STATE_1PARAM;
|
| 877 | break;
|
| 878 | case STATE_2PARAM_1:
|
| 879 | port->data[1] = b;
|
| 880 | next_state = STATE_2PARAM_2;
|
| 881 | break;
|
| 882 | case STATE_2PARAM_2:
|
| 883 | if (port->data[0] < 0xf0)
|
| 884 | p[0] |= port->data[0] >> 4;
|
| 885 | else
|
| 886 | p[0] |= 0x03;
|
| 887 |
|
| 888 | p[1] = port->data[0];
|
| 889 | p[2] = port->data[1];
|
| 890 | p[3] = b;
|
| 891 |
|
| 892 | next_state = STATE_2PARAM_1;
|
| 893 | break;
|
| 894 | case STATE_SYSEX_0:
|
| 895 | port->data[0] = b;
|
| 896 | next_state = STATE_SYSEX_1;
|
| 897 | break;
|
| 898 | case STATE_SYSEX_1:
|
| 899 | port->data[1] = b;
|
| 900 | next_state = STATE_SYSEX_2;
|
| 901 | break;
|
| 902 | case STATE_SYSEX_2:
|
| 903 | p[0] |= 0x04;
|
| 904 | p[1] = port->data[0];
|
| 905 | p[2] = port->data[1];
|
| 906 | p[3] = b;
|
| 907 | next_state = STATE_SYSEX_0;
|
| 908 | break;
|
| 909 | }
|
| 910 | break;
|
| 911 | }
|
| 912 |
|
| 913 |
|
| 914 | if (next_state == STATE_FINISHED ||
|
| 915 | port->state == STATE_SYSEX_2 ||
|
| 916 | port->state == STATE_1PARAM ||
|
| 917 | port->state == STATE_2PARAM_2 ||
|
| 918 | port->state == STATE_REAL_TIME) {
|
| 919 | memcpy(req->buf + req->length, p, sizeof(p));
|
| 920 | req->length += sizeof(p);
|
| 921 |
|
| 922 | if (next_state == STATE_FINISHED) {
|
| 923 | next_state = STATE_INITIAL;
|
| 924 | port->data[0] = port->data[1] = 0;
|
| 925 | }
|
| 926 |
|
| 927 | if (midi2->info.req_buf_size - req->length <= 4) {
|
| 928 | queue_request_ep_raw(req);
|
| 929 | *req_p = NULL;
|
| 930 | return true;
|
| 931 | }
|
| 932 | }
|
| 933 |
|
| 934 | port->state = next_state;
|
| 935 | return false;
|
| 936 | }
|
| 937 |
|
| 938 | |
| 939 | * returns true if the request gets empty
|
| 940 | * returns false if all have been processed
|
| 941 |
|
| 942 | static bool process_midi1_pending_buf(struct f_midi2 *midi2,
|
| 943 | struct usb_request **req_p)
|
| 944 | {
|
| 945 | unsigned int cable, c;
|
| 946 |
|
| 947 | for (cable = 0; cable < midi2->num_midi1_in; cable++) {
|
| 948 | struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
|
| 949 |
|
| 950 | if (!port->pending)
|
| 951 | continue;
|
| 952 | for (c = 0; c < port->pending; c++) {
|
| 953 | if (process_midi1_byte(midi2, cable, port->buf[c],
|
| 954 | req_p)) {
|
| 955 | port->pending -= c;
|
| 956 | if (port->pending)
|
| 957 | memmove(port->buf, port->buf + c,
|
| 958 | port->pending);
|
| 959 | return true;
|
| 960 | }
|
| 961 | }
|
| 962 | port->pending = 0;
|
| 963 | }
|
| 964 |
|
| 965 | return false;
|
| 966 | }
|
| 967 |
|
| 968 | |
| 969 |
|
| 970 | static void fill_midi1_pending_buf(struct f_midi2 *midi2, u8 cable, u8 *buf,
|
| 971 | unsigned int size)
|
| 972 | {
|
| 973 | struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
|
| 974 |
|
| 975 | if (port->pending + size > sizeof(port->buf))
|
| 976 | return;
|
| 977 | memcpy(port->buf + port->pending, buf, size);
|
| 978 | port->pending += size;
|
| 979 | }
|
| 980 |
|
| 981 |
|
| 982 | static void process_midi1_transmit(struct f_midi2 *midi2)
|
| 983 | {
|
| 984 | struct f_midi2_usb_ep *usb_ep = &midi2->midi1_ep_in;
|
| 985 | struct f_midi2_ep *ep = &midi2->midi2_eps[0];
|
| 986 | struct usb_request *req = NULL;
|
| 987 |
|
| 988 | unsigned char outbuf[12];
|
| 989 | unsigned char group, cable;
|
| 990 | int len, size;
|
| 991 | u32 ump;
|
| 992 |
|
| 993 | if (!usb_ep->usb_ep || !usb_ep->usb_ep->enabled)
|
| 994 | return;
|
| 995 |
|
| 996 | for (;;) {
|
| 997 | if (!req) {
|
| 998 | req = get_empty_request(usb_ep);
|
| 999 | if (!req)
|
| 1000 | break;
|
| 1001 | }
|
| 1002 |
|
| 1003 | if (process_midi1_pending_buf(midi2, &req))
|
| 1004 | continue;
|
| 1005 |
|
| 1006 | len = snd_ump_transmit(ep->ump, &ump, 4);
|
| 1007 | if (len <= 0)
|
| 1008 | break;
|
| 1009 | if (snd_ump_receive_ump_val(ep->ump, ump) <= 0)
|
| 1010 | continue;
|
| 1011 | size = snd_ump_convert_from_ump(ep->ump->input_buf, outbuf,
|
| 1012 | &group);
|
| 1013 | if (size <= 0)
|
| 1014 | continue;
|
| 1015 | cable = ep->in_group_to_cable[group];
|
| 1016 | if (!cable)
|
| 1017 | continue;
|
| 1018 | cable--;
|
| 1019 | fill_midi1_pending_buf(midi2, cable, outbuf, size);
|
| 1020 | }
|
| 1021 |
|
| 1022 | if (req) {
|
| 1023 | if (req->length)
|
| 1024 | queue_request_ep_raw(req);
|
| 1025 | else
|
| 1026 | put_empty_request(req);
|
| 1027 | }
|
| 1028 | }
|
| 1029 |
|
| 1030 |
|
| 1031 | static void f_midi2_midi1_ep_in_complete(struct usb_ep *usb_ep,
|
| 1032 | struct usb_request *req)
|
| 1033 | {
|
| 1034 | struct f_midi2_req_ctx *ctx = req->context;
|
| 1035 | struct f_midi2 *midi2 = ctx->usb_ep->card;
|
| 1036 | int status = req->status;
|
| 1037 |
|
| 1038 | put_empty_request(req);
|
| 1039 |
|
| 1040 | if (status) {
|
| 1041 | DBG(midi2, "%s complete error %d: %d/%d\n",
|
| 1042 | usb_ep->name, status, req->actual, req->length);
|
| 1043 | return;
|
| 1044 | }
|
| 1045 |
|
| 1046 | process_midi1_transmit(midi2);
|
| 1047 | }
|
| 1048 |
|
| 1049 |
|
| 1050 | static void f_midi2_midi1_ep_out_complete(struct usb_ep *usb_ep,
|
| 1051 | struct usb_request *req)
|
| 1052 | {
|
| 1053 | struct f_midi2_req_ctx *ctx = req->context;
|
| 1054 | struct f_midi2 *midi2 = ctx->usb_ep->card;
|
| 1055 | struct f_midi2_ep *ep;
|
| 1056 | struct ump_cvt_to_ump *cvt = &midi2->midi1_ump_cvt;
|
| 1057 | static const u8 midi1_packet_bytes[16] = {
|
| 1058 | 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
|
| 1059 | };
|
| 1060 | unsigned int group, cable, bytes, c, len;
|
| 1061 | int status = req->status;
|
| 1062 | const u8 *buf = req->buf;
|
| 1063 |
|
| 1064 | if (status) {
|
| 1065 | DBG(midi2, "%s complete error %d: %d/%d\n",
|
| 1066 | usb_ep->name, status, req->actual, req->length);
|
| 1067 | goto error;
|
| 1068 | }
|
| 1069 |
|
| 1070 | len = req->actual >> 2;
|
| 1071 | for (; len; len--, buf += 4) {
|
| 1072 | cable = *buf >> 4;
|
| 1073 | ep = midi2->out_cable_mapping[cable].ep;
|
| 1074 | if (!ep)
|
| 1075 | continue;
|
| 1076 | group = midi2->out_cable_mapping[cable].group;
|
| 1077 | bytes = midi1_packet_bytes[*buf & 0x0f];
|
| 1078 | for (c = 0; c < bytes; c++) {
|
| 1079 | snd_ump_convert_to_ump(cvt, group,
|
| 1080 | to_ump_protocol(ep->info.protocol),
|
| 1081 | buf[c + 1]);
|
| 1082 | if (cvt->ump_bytes) {
|
| 1083 | snd_ump_receive(ep->ump, cvt->ump,
|
| 1084 | cvt->ump_bytes);
|
| 1085 | cvt->ump_bytes = 0;
|
| 1086 | }
|
| 1087 | }
|
| 1088 | }
|
| 1089 |
|
| 1090 | if (midi2->operation_mode != MIDI_OP_MODE_MIDI1)
|
| 1091 | goto error;
|
| 1092 |
|
| 1093 | if (queue_request_ep_raw(req))
|
| 1094 | goto error;
|
| 1095 | return;
|
| 1096 |
|
| 1097 | error:
|
| 1098 | put_empty_request(req);
|
| 1099 | }
|
| 1100 |
|
| 1101 | |
| 1102 | * Common EP handling helpers
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 | static int f_midi2_start_ep(struct f_midi2_usb_ep *usb_ep,
|
| 1107 | struct usb_function *fn)
|
| 1108 | {
|
| 1109 | int err;
|
| 1110 |
|
| 1111 | if (!usb_ep->usb_ep)
|
| 1112 | return 0;
|
| 1113 |
|
| 1114 | usb_ep_disable(usb_ep->usb_ep);
|
| 1115 | err = config_ep_by_speed(usb_ep->card->gadget, fn, usb_ep->usb_ep);
|
| 1116 | if (err)
|
| 1117 | return err;
|
| 1118 | return usb_ep_enable(usb_ep->usb_ep);
|
| 1119 | }
|
| 1120 |
|
| 1121 |
|
| 1122 | static void f_midi2_drop_reqs(struct f_midi2_usb_ep *usb_ep)
|
| 1123 | {
|
| 1124 | int i;
|
| 1125 |
|
| 1126 | if (!usb_ep->usb_ep || !usb_ep->num_reqs)
|
| 1127 | return;
|
| 1128 |
|
| 1129 | for (i = 0; i < usb_ep->num_reqs; i++) {
|
| 1130 | if (!test_bit(i, &usb_ep->free_reqs) && usb_ep->reqs[i].req) {
|
| 1131 | usb_ep_dequeue(usb_ep->usb_ep, usb_ep->reqs[i].req);
|
| 1132 | set_bit(i, &usb_ep->free_reqs);
|
| 1133 | }
|
| 1134 | }
|
| 1135 | }
|
| 1136 |
|
| 1137 |
|
| 1138 | static int f_midi2_alloc_ep_reqs(struct f_midi2_usb_ep *usb_ep)
|
| 1139 | {
|
| 1140 | struct f_midi2 *midi2 = usb_ep->card;
|
| 1141 | int i;
|
| 1142 |
|
| 1143 | if (!usb_ep->usb_ep)
|
| 1144 | return 0;
|
| 1145 | if (!usb_ep->reqs)
|
| 1146 | return -EINVAL;
|
| 1147 |
|
| 1148 | for (i = 0; i < usb_ep->num_reqs; i++) {
|
| 1149 | if (usb_ep->reqs[i].req)
|
| 1150 | continue;
|
| 1151 | usb_ep->reqs[i].req = alloc_ep_req(usb_ep->usb_ep,
|
| 1152 | midi2->info.req_buf_size);
|
| 1153 | if (!usb_ep->reqs[i].req)
|
| 1154 | return -ENOMEM;
|
| 1155 | usb_ep->reqs[i].req->context = &usb_ep->reqs[i];
|
| 1156 | }
|
| 1157 | return 0;
|
| 1158 | }
|
| 1159 |
|
| 1160 |
|
| 1161 | static void f_midi2_free_ep_reqs(struct f_midi2_usb_ep *usb_ep)
|
| 1162 | {
|
| 1163 | int i;
|
| 1164 |
|
| 1165 | for (i = 0; i < usb_ep->num_reqs; i++) {
|
| 1166 | if (!usb_ep->reqs[i].req)
|
| 1167 | continue;
|
| 1168 | free_ep_req(usb_ep->usb_ep, usb_ep->reqs[i].req);
|
| 1169 | usb_ep->reqs[i].req = NULL;
|
| 1170 | }
|
| 1171 | }
|
| 1172 |
|
| 1173 |
|
| 1174 | static int f_midi2_init_ep(struct f_midi2 *midi2, struct f_midi2_ep *ep,
|
| 1175 | struct f_midi2_usb_ep *usb_ep,
|
| 1176 | void *desc,
|
| 1177 | void (*complete)(struct usb_ep *usb_ep,
|
| 1178 | struct usb_request *req))
|
| 1179 | {
|
| 1180 | int i;
|
| 1181 |
|
| 1182 | usb_ep->card = midi2;
|
| 1183 | usb_ep->ep = ep;
|
| 1184 | usb_ep->usb_ep = usb_ep_autoconfig(midi2->gadget, desc);
|
| 1185 | if (!usb_ep->usb_ep)
|
| 1186 | return -ENODEV;
|
| 1187 | usb_ep->complete = complete;
|
| 1188 |
|
| 1189 | usb_ep->reqs = kzalloc_objs(*usb_ep->reqs, midi2->info.num_reqs);
|
| 1190 | if (!usb_ep->reqs)
|
| 1191 | return -ENOMEM;
|
| 1192 | for (i = 0; i < midi2->info.num_reqs; i++) {
|
| 1193 | usb_ep->reqs[i].index = i;
|
| 1194 | usb_ep->reqs[i].usb_ep = usb_ep;
|
| 1195 | set_bit(i, &usb_ep->free_reqs);
|
| 1196 | usb_ep->num_reqs++;
|
| 1197 | }
|
| 1198 |
|
| 1199 | return 0;
|
| 1200 | }
|
| 1201 |
|
| 1202 |
|
| 1203 | static void f_midi2_free_ep(struct f_midi2_usb_ep *usb_ep)
|
| 1204 | {
|
| 1205 | f_midi2_drop_reqs(usb_ep);
|
| 1206 |
|
| 1207 | f_midi2_free_ep_reqs(usb_ep);
|
| 1208 |
|
| 1209 | kfree(usb_ep->reqs);
|
| 1210 | usb_ep->num_reqs = 0;
|
| 1211 | usb_ep->free_reqs = 0;
|
| 1212 | usb_ep->reqs = NULL;
|
| 1213 | }
|
| 1214 |
|
| 1215 |
|
| 1216 | static void f_midi2_queue_out_reqs(struct f_midi2_usb_ep *usb_ep)
|
| 1217 | {
|
| 1218 | int i, err;
|
| 1219 |
|
| 1220 | if (!usb_ep->usb_ep)
|
| 1221 | return;
|
| 1222 |
|
| 1223 | for (i = 0; i < usb_ep->num_reqs; i++) {
|
| 1224 | if (!test_bit(i, &usb_ep->free_reqs) || !usb_ep->reqs[i].req)
|
| 1225 | continue;
|
| 1226 | usb_ep->reqs[i].req->complete = usb_ep->complete;
|
| 1227 | err = usb_ep_queue(usb_ep->usb_ep, usb_ep->reqs[i].req,
|
| 1228 | GFP_ATOMIC);
|
| 1229 | if (!err)
|
| 1230 | clear_bit(i, &usb_ep->free_reqs);
|
| 1231 | }
|
| 1232 | }
|
| 1233 |
|
| 1234 | |
| 1235 | * Gadget Function callbacks
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 | static void f_midi2_stop_eps(struct f_midi2_usb_ep *ep_in,
|
| 1240 | struct f_midi2_usb_ep *ep_out)
|
| 1241 | {
|
| 1242 | f_midi2_drop_reqs(ep_in);
|
| 1243 | f_midi2_drop_reqs(ep_out);
|
| 1244 | f_midi2_free_ep_reqs(ep_in);
|
| 1245 | f_midi2_free_ep_reqs(ep_out);
|
| 1246 | }
|
| 1247 |
|
| 1248 |
|
| 1249 | static int f_midi2_start_eps(struct f_midi2_usb_ep *ep_in,
|
| 1250 | struct f_midi2_usb_ep *ep_out,
|
| 1251 | struct usb_function *fn)
|
| 1252 | {
|
| 1253 | int err;
|
| 1254 |
|
| 1255 | err = f_midi2_start_ep(ep_in, fn);
|
| 1256 | if (err)
|
| 1257 | return err;
|
| 1258 | err = f_midi2_start_ep(ep_out, fn);
|
| 1259 | if (err)
|
| 1260 | return err;
|
| 1261 |
|
| 1262 | err = f_midi2_alloc_ep_reqs(ep_in);
|
| 1263 | if (err)
|
| 1264 | return err;
|
| 1265 | err = f_midi2_alloc_ep_reqs(ep_out);
|
| 1266 | if (err)
|
| 1267 | return err;
|
| 1268 |
|
| 1269 | f_midi2_queue_out_reqs(ep_out);
|
| 1270 | return 0;
|
| 1271 | }
|
| 1272 |
|
| 1273 |
|
| 1274 | static int f_midi2_set_alt(struct usb_function *fn, unsigned int intf,
|
| 1275 | unsigned int alt)
|
| 1276 | {
|
| 1277 | struct f_midi2 *midi2 = func_to_midi2(fn);
|
| 1278 | struct f_midi2_ep *ep;
|
| 1279 | int i, op_mode, err;
|
| 1280 |
|
| 1281 | if (intf != midi2->midi_if || alt > 1)
|
| 1282 | return 0;
|
| 1283 |
|
| 1284 | if (alt == 0)
|
| 1285 | op_mode = MIDI_OP_MODE_MIDI1;
|
| 1286 | else
|
| 1287 | op_mode = MIDI_OP_MODE_MIDI2;
|
| 1288 |
|
| 1289 | if (midi2->operation_mode == op_mode)
|
| 1290 | return 0;
|
| 1291 |
|
| 1292 | midi2->operation_mode = op_mode;
|
| 1293 |
|
| 1294 | if (op_mode != MIDI_OP_MODE_MIDI1)
|
| 1295 | f_midi2_stop_eps(&midi2->midi1_ep_in, &midi2->midi1_ep_out);
|
| 1296 |
|
| 1297 | if (op_mode != MIDI_OP_MODE_MIDI2) {
|
| 1298 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1299 | ep = &midi2->midi2_eps[i];
|
| 1300 | f_midi2_stop_eps(&ep->ep_in, &ep->ep_out);
|
| 1301 | }
|
| 1302 | }
|
| 1303 |
|
| 1304 | if (op_mode == MIDI_OP_MODE_MIDI1)
|
| 1305 | return f_midi2_start_eps(&midi2->midi1_ep_in,
|
| 1306 | &midi2->midi1_ep_out, fn);
|
| 1307 |
|
| 1308 | if (op_mode == MIDI_OP_MODE_MIDI2) {
|
| 1309 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1310 | ep = &midi2->midi2_eps[i];
|
| 1311 |
|
| 1312 | err = f_midi2_start_eps(&ep->ep_in, &ep->ep_out, fn);
|
| 1313 | if (err)
|
| 1314 | return err;
|
| 1315 | }
|
| 1316 | }
|
| 1317 |
|
| 1318 | return 0;
|
| 1319 | }
|
| 1320 |
|
| 1321 |
|
| 1322 | static int f_midi2_get_alt(struct usb_function *fn, unsigned int intf)
|
| 1323 | {
|
| 1324 | struct f_midi2 *midi2 = func_to_midi2(fn);
|
| 1325 |
|
| 1326 | if (intf == midi2->midi_if &&
|
| 1327 | midi2->operation_mode == MIDI_OP_MODE_MIDI2)
|
| 1328 | return 1;
|
| 1329 | return 0;
|
| 1330 | }
|
| 1331 |
|
| 1332 |
|
| 1333 | static unsigned int ump_to_usb_dir(unsigned int ump_dir)
|
| 1334 | {
|
| 1335 | switch (ump_dir) {
|
| 1336 | case SNDRV_UMP_DIR_INPUT:
|
| 1337 | return USB_MS_GR_TRM_BLOCK_TYPE_INPUT_ONLY;
|
| 1338 | case SNDRV_UMP_DIR_OUTPUT:
|
| 1339 | return USB_MS_GR_TRM_BLOCK_TYPE_OUTPUT_ONLY;
|
| 1340 | default:
|
| 1341 | return USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL;
|
| 1342 | }
|
| 1343 | }
|
| 1344 |
|
| 1345 |
|
| 1346 | static void assign_block_descriptors(struct f_midi2 *midi2,
|
| 1347 | struct usb_request *req,
|
| 1348 | int max_len)
|
| 1349 | {
|
| 1350 | struct usb_ms20_gr_trm_block_header_descriptor header;
|
| 1351 | struct usb_ms20_gr_trm_block_descriptor *desc;
|
| 1352 | struct f_midi2_block_info *b;
|
| 1353 | struct f_midi2_ep *ep;
|
| 1354 | int i, blk, len;
|
| 1355 | char *data;
|
| 1356 |
|
| 1357 | len = sizeof(gtb_header_desc) + sizeof(gtb_desc) * midi2->total_blocks;
|
| 1358 | if (WARN_ON(len > midi2->info.req_buf_size))
|
| 1359 | return;
|
| 1360 |
|
| 1361 | header = gtb_header_desc;
|
| 1362 | header.wTotalLength = cpu_to_le16(len);
|
| 1363 | if (max_len < len) {
|
| 1364 | len = min_t(int, len, sizeof(header));
|
| 1365 | memcpy(req->buf, &header, len);
|
| 1366 | req->length = len;
|
| 1367 | req->zero = len < max_len;
|
| 1368 | return;
|
| 1369 | }
|
| 1370 |
|
| 1371 | memcpy(req->buf, &header, sizeof(header));
|
| 1372 | data = req->buf + sizeof(header);
|
| 1373 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1374 | ep = &midi2->midi2_eps[i];
|
| 1375 | for (blk = 0; blk < ep->num_blks; blk++) {
|
| 1376 | b = &ep->blks[blk].info;
|
| 1377 | desc = (struct usb_ms20_gr_trm_block_descriptor *)data;
|
| 1378 |
|
| 1379 | *desc = gtb_desc;
|
| 1380 | desc->bGrpTrmBlkID = ep->blks[blk].gtb_id;
|
| 1381 | desc->bGrpTrmBlkType = ump_to_usb_dir(b->direction);
|
| 1382 | desc->nGroupTrm = b->first_group;
|
| 1383 | desc->nNumGroupTrm = b->num_groups;
|
| 1384 | desc->iBlockItem = ep->blks[blk].string_id;
|
| 1385 |
|
| 1386 | if (ep->info.protocol == 2)
|
| 1387 | desc->bMIDIProtocol = USB_MS_MIDI_PROTO_2_0;
|
| 1388 | else
|
| 1389 | desc->bMIDIProtocol = USB_MS_MIDI_PROTO_1_0_128;
|
| 1390 |
|
| 1391 | if (b->is_midi1 == 2) {
|
| 1392 | desc->wMaxInputBandwidth = cpu_to_le16(1);
|
| 1393 | desc->wMaxOutputBandwidth = cpu_to_le16(1);
|
| 1394 | }
|
| 1395 |
|
| 1396 | data += sizeof(*desc);
|
| 1397 | }
|
| 1398 | }
|
| 1399 |
|
| 1400 | req->length = len;
|
| 1401 | req->zero = len < max_len;
|
| 1402 | }
|
| 1403 |
|
| 1404 |
|
| 1405 | static int f_midi2_setup(struct usb_function *fn,
|
| 1406 | const struct usb_ctrlrequest *ctrl)
|
| 1407 | {
|
| 1408 | struct f_midi2 *midi2 = func_to_midi2(fn);
|
| 1409 | struct usb_composite_dev *cdev = fn->config->cdev;
|
| 1410 | struct usb_request *req = cdev->req;
|
| 1411 | u16 value, length;
|
| 1412 |
|
| 1413 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD ||
|
| 1414 | ctrl->bRequest != USB_REQ_GET_DESCRIPTOR)
|
| 1415 | return -EOPNOTSUPP;
|
| 1416 |
|
| 1417 | value = le16_to_cpu(ctrl->wValue);
|
| 1418 | length = le16_to_cpu(ctrl->wLength);
|
| 1419 |
|
| 1420 | if ((value >> 8) != USB_DT_CS_GR_TRM_BLOCK)
|
| 1421 | return -EOPNOTSUPP;
|
| 1422 |
|
| 1423 |
|
| 1424 | if ((value & 0xff) != 1)
|
| 1425 | return -EOPNOTSUPP;
|
| 1426 |
|
| 1427 | assign_block_descriptors(midi2, req, length);
|
| 1428 | return usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
|
| 1429 | }
|
| 1430 |
|
| 1431 |
|
| 1432 | static void f_midi2_disable(struct usb_function *fn)
|
| 1433 | {
|
| 1434 | struct f_midi2 *midi2 = func_to_midi2(fn);
|
| 1435 |
|
| 1436 | midi2->operation_mode = MIDI_OP_MODE_UNSET;
|
| 1437 | }
|
| 1438 |
|
| 1439 | |
| 1440 | * ALSA UMP ops: most of them are NOPs, only trigger for write is needed
|
| 1441 |
|
| 1442 | static int f_midi2_ump_open(struct snd_ump_endpoint *ump, int dir)
|
| 1443 | {
|
| 1444 | return 0;
|
| 1445 | }
|
| 1446 |
|
| 1447 | static void f_midi2_ump_close(struct snd_ump_endpoint *ump, int dir)
|
| 1448 | {
|
| 1449 | }
|
| 1450 |
|
| 1451 | static void f_midi2_ump_trigger(struct snd_ump_endpoint *ump, int dir, int up)
|
| 1452 | {
|
| 1453 | struct f_midi2_ep *ep = ump->private_data;
|
| 1454 | struct f_midi2 *midi2 = ep->card;
|
| 1455 |
|
| 1456 | if (up && dir == SNDRV_RAWMIDI_STREAM_OUTPUT) {
|
| 1457 | switch (midi2->operation_mode) {
|
| 1458 | case MIDI_OP_MODE_MIDI1:
|
| 1459 | process_midi1_transmit(midi2);
|
| 1460 | break;
|
| 1461 | case MIDI_OP_MODE_MIDI2:
|
| 1462 | process_ump_transmit(ep);
|
| 1463 | break;
|
| 1464 | }
|
| 1465 | }
|
| 1466 | }
|
| 1467 |
|
| 1468 | static void f_midi2_ump_drain(struct snd_ump_endpoint *ump, int dir)
|
| 1469 | {
|
| 1470 | }
|
| 1471 |
|
| 1472 | static const struct snd_ump_ops f_midi2_ump_ops = {
|
| 1473 | .open = f_midi2_ump_open,
|
| 1474 | .close = f_midi2_ump_close,
|
| 1475 | .trigger = f_midi2_ump_trigger,
|
| 1476 | .drain = f_midi2_ump_drain,
|
| 1477 | };
|
| 1478 |
|
| 1479 | |
| 1480 | * "Operation Mode" control element
|
| 1481 |
|
| 1482 | static int f_midi2_operation_mode_info(struct snd_kcontrol *kcontrol,
|
| 1483 | struct snd_ctl_elem_info *uinfo)
|
| 1484 | {
|
| 1485 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
|
| 1486 | uinfo->count = 1;
|
| 1487 | uinfo->value.integer.min = MIDI_OP_MODE_UNSET;
|
| 1488 | uinfo->value.integer.max = MIDI_OP_MODE_MIDI2;
|
| 1489 | return 0;
|
| 1490 | }
|
| 1491 |
|
| 1492 | static int f_midi2_operation_mode_get(struct snd_kcontrol *kcontrol,
|
| 1493 | struct snd_ctl_elem_value *ucontrol)
|
| 1494 | {
|
| 1495 | struct f_midi2 *midi2 = snd_kcontrol_chip(kcontrol);
|
| 1496 |
|
| 1497 | ucontrol->value.integer.value[0] = midi2->operation_mode;
|
| 1498 | return 0;
|
| 1499 | }
|
| 1500 |
|
| 1501 | static const struct snd_kcontrol_new operation_mode_ctl = {
|
| 1502 | .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
|
| 1503 | .name = "Operation Mode",
|
| 1504 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
|
| 1505 | .info = f_midi2_operation_mode_info,
|
| 1506 | .get = f_midi2_operation_mode_get,
|
| 1507 | };
|
| 1508 |
|
| 1509 | |
| 1510 | * ALSA UMP instance creation / deletion
|
| 1511 |
|
| 1512 | static void f_midi2_free_card(struct f_midi2 *midi2)
|
| 1513 | {
|
| 1514 | if (midi2->card) {
|
| 1515 | snd_card_free_when_closed(midi2->card);
|
| 1516 | midi2->card = NULL;
|
| 1517 | }
|
| 1518 | }
|
| 1519 |
|
| 1520 |
|
| 1521 | static int reverse_dir(int dir)
|
| 1522 | {
|
| 1523 | if (!dir || dir == SNDRV_UMP_DIR_BIDIRECTION)
|
| 1524 | return dir;
|
| 1525 | return (dir == SNDRV_UMP_DIR_OUTPUT) ?
|
| 1526 | SNDRV_UMP_DIR_INPUT : SNDRV_UMP_DIR_OUTPUT;
|
| 1527 | }
|
| 1528 |
|
| 1529 | static int f_midi2_create_card(struct f_midi2 *midi2)
|
| 1530 | {
|
| 1531 | struct snd_card *card;
|
| 1532 | struct snd_ump_endpoint *ump;
|
| 1533 | struct f_midi2_ep *ep;
|
| 1534 | int i, id, blk, err;
|
| 1535 | __be32 sw;
|
| 1536 |
|
| 1537 | err = snd_card_new(&midi2->gadget->dev, -1, NULL, THIS_MODULE, 0,
|
| 1538 | &card);
|
| 1539 | if (err < 0)
|
| 1540 | return err;
|
| 1541 | midi2->card = card;
|
| 1542 |
|
| 1543 | strscpy(card->driver, "f_midi2");
|
| 1544 | strscpy(card->shortname, "MIDI 2.0 Gadget");
|
| 1545 | strscpy(card->longname, "MIDI 2.0 Gadget");
|
| 1546 |
|
| 1547 | id = 0;
|
| 1548 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1549 | ep = &midi2->midi2_eps[i];
|
| 1550 | err = snd_ump_endpoint_new(card, "MIDI 2.0 Gadget", id,
|
| 1551 | 1, 1, &ump);
|
| 1552 | if (err < 0)
|
| 1553 | goto error;
|
| 1554 | id++;
|
| 1555 |
|
| 1556 | ep->ump = ump;
|
| 1557 | ump->no_process_stream = true;
|
| 1558 | ump->private_data = ep;
|
| 1559 | ump->ops = &f_midi2_ump_ops;
|
| 1560 | if (midi2->info.static_block)
|
| 1561 | ump->info.flags |= SNDRV_UMP_EP_INFO_STATIC_BLOCKS;
|
| 1562 | ump->info.protocol_caps = (ep->info.protocol_caps & 3) << 8;
|
| 1563 | ump->info.protocol = to_ump_protocol(ep->info.protocol);
|
| 1564 | ump->info.version = 0x0101;
|
| 1565 | ump->info.family_id = ep->info.family;
|
| 1566 | ump->info.model_id = ep->info.model;
|
| 1567 | ump->info.manufacturer_id = ep->info.manufacturer & 0xffffff;
|
| 1568 | sw = cpu_to_be32(ep->info.sw_revision);
|
| 1569 | memcpy(ump->info.sw_revision, &sw, 4);
|
| 1570 |
|
| 1571 | strscpy(ump->info.name, ump_ep_name(ep),
|
| 1572 | sizeof(ump->info.name));
|
| 1573 | strscpy(ump->info.product_id, ump_product_id(ep),
|
| 1574 | sizeof(ump->info.product_id));
|
| 1575 | strscpy(ump->core.name, ump->info.name, sizeof(ump->core.name));
|
| 1576 |
|
| 1577 | for (blk = 0; blk < ep->num_blks; blk++) {
|
| 1578 | const struct f_midi2_block_info *b = &ep->blks[blk].info;
|
| 1579 | struct snd_ump_block *fb;
|
| 1580 |
|
| 1581 | err = snd_ump_block_new(ump, blk,
|
| 1582 | reverse_dir(b->direction),
|
| 1583 | b->first_group, b->num_groups,
|
| 1584 | &ep->blks[blk].fb);
|
| 1585 | if (err < 0)
|
| 1586 | goto error;
|
| 1587 | fb = ep->blks[blk].fb;
|
| 1588 | fb->info.active = !!b->active;
|
| 1589 | fb->info.midi_ci_version = b->midi_ci_version;
|
| 1590 | fb->info.ui_hint = reverse_dir(b->ui_hint);
|
| 1591 | fb->info.sysex8_streams = b->sysex8_streams;
|
| 1592 | if (b->is_midi1 < 2)
|
| 1593 | fb->info.flags |= b->is_midi1;
|
| 1594 | else
|
| 1595 | fb->info.flags |= SNDRV_UMP_BLOCK_IS_MIDI1 |
|
| 1596 | SNDRV_UMP_BLOCK_IS_LOWSPEED;
|
| 1597 | strscpy(fb->info.name, ump_fb_name(b),
|
| 1598 | sizeof(fb->info.name));
|
| 1599 | }
|
| 1600 | snd_ump_update_group_attrs(ump);
|
| 1601 | }
|
| 1602 |
|
| 1603 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1604 | err = snd_ump_attach_legacy_rawmidi(midi2->midi2_eps[i].ump,
|
| 1605 | "Legacy MIDI", id);
|
| 1606 | if (err < 0)
|
| 1607 | goto error;
|
| 1608 | id++;
|
| 1609 | }
|
| 1610 |
|
| 1611 | err = snd_ctl_add(card, snd_ctl_new1(&operation_mode_ctl, midi2));
|
| 1612 | if (err < 0)
|
| 1613 | goto error;
|
| 1614 |
|
| 1615 | err = snd_card_register(card);
|
| 1616 | if (err < 0)
|
| 1617 | goto error;
|
| 1618 |
|
| 1619 | return 0;
|
| 1620 |
|
| 1621 | error:
|
| 1622 | f_midi2_free_card(midi2);
|
| 1623 | return err;
|
| 1624 | }
|
| 1625 |
|
| 1626 | |
| 1627 | * Creation of USB descriptors
|
| 1628 |
|
| 1629 | struct f_midi2_usb_config {
|
| 1630 | struct usb_descriptor_header **list;
|
| 1631 | unsigned int size;
|
| 1632 | unsigned int alloc;
|
| 1633 |
|
| 1634 |
|
| 1635 | unsigned char jack_in, jack_out, jack_id;
|
| 1636 | struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES * 2];
|
| 1637 | struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES * 2];
|
| 1638 | };
|
| 1639 |
|
| 1640 | static int append_config(struct f_midi2_usb_config *config, void *d)
|
| 1641 | {
|
| 1642 | unsigned int size;
|
| 1643 | void *buf;
|
| 1644 |
|
| 1645 | if (config->size + 2 >= config->alloc) {
|
| 1646 | size = config->size + 16;
|
| 1647 | buf = krealloc(config->list, size * sizeof(void *), GFP_KERNEL);
|
| 1648 | if (!buf)
|
| 1649 | return -ENOMEM;
|
| 1650 | config->list = buf;
|
| 1651 | config->alloc = size;
|
| 1652 | }
|
| 1653 |
|
| 1654 | config->list[config->size] = d;
|
| 1655 | config->size++;
|
| 1656 | config->list[config->size] = NULL;
|
| 1657 | return 0;
|
| 1658 | }
|
| 1659 |
|
| 1660 | static int append_configs(struct f_midi2_usb_config *config, void **d)
|
| 1661 | {
|
| 1662 | int err;
|
| 1663 |
|
| 1664 | for (; *d; d++) {
|
| 1665 | err = append_config(config, *d);
|
| 1666 | if (err)
|
| 1667 | return err;
|
| 1668 | }
|
| 1669 | return 0;
|
| 1670 | }
|
| 1671 |
|
| 1672 | static int append_midi1_in_jack(struct f_midi2 *midi2,
|
| 1673 | struct f_midi2_usb_config *config,
|
| 1674 | struct midi1_cable_mapping *map,
|
| 1675 | unsigned int type)
|
| 1676 | {
|
| 1677 | struct usb_midi_in_jack_descriptor *jack =
|
| 1678 | &config->jack_ins[config->jack_in++];
|
| 1679 | int id = ++config->jack_id;
|
| 1680 | int err;
|
| 1681 |
|
| 1682 | jack->bLength = 0x06;
|
| 1683 | jack->bDescriptorType = USB_DT_CS_INTERFACE;
|
| 1684 | jack->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
|
| 1685 | jack->bJackType = type;
|
| 1686 | jack->bJackID = id;
|
| 1687 |
|
| 1688 | if (map->ep)
|
| 1689 | jack->iJack = map->ep->blks[map->block].string_id;
|
| 1690 |
|
| 1691 | err = append_config(config, jack);
|
| 1692 | if (err < 0)
|
| 1693 | return err;
|
| 1694 | return id;
|
| 1695 | }
|
| 1696 |
|
| 1697 | static int append_midi1_out_jack(struct f_midi2 *midi2,
|
| 1698 | struct f_midi2_usb_config *config,
|
| 1699 | struct midi1_cable_mapping *map,
|
| 1700 | unsigned int type, unsigned int source)
|
| 1701 | {
|
| 1702 | struct usb_midi_out_jack_descriptor_1 *jack =
|
| 1703 | &config->jack_outs[config->jack_out++];
|
| 1704 | int id = ++config->jack_id;
|
| 1705 | int err;
|
| 1706 |
|
| 1707 | jack->bLength = 0x09;
|
| 1708 | jack->bDescriptorType = USB_DT_CS_INTERFACE;
|
| 1709 | jack->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
|
| 1710 | jack->bJackType = type;
|
| 1711 | jack->bJackID = id;
|
| 1712 | jack->bNrInputPins = 1;
|
| 1713 | jack->pins[0].baSourceID = source;
|
| 1714 | jack->pins[0].baSourcePin = 0x01;
|
| 1715 |
|
| 1716 | if (map->ep)
|
| 1717 | jack->iJack = map->ep->blks[map->block].string_id;
|
| 1718 |
|
| 1719 | err = append_config(config, jack);
|
| 1720 | if (err < 0)
|
| 1721 | return err;
|
| 1722 | return id;
|
| 1723 | }
|
| 1724 |
|
| 1725 | static int f_midi2_create_usb_configs(struct f_midi2 *midi2,
|
| 1726 | struct f_midi2_usb_config *config,
|
| 1727 | int speed)
|
| 1728 | {
|
| 1729 | void **midi1_in_eps, **midi1_out_eps;
|
| 1730 | int i, jack, total;
|
| 1731 | int err;
|
| 1732 |
|
| 1733 | switch (speed) {
|
| 1734 | default:
|
| 1735 | case USB_SPEED_HIGH:
|
| 1736 | midi2_midi1_ep_out_desc.wMaxPacketSize = cpu_to_le16(512);
|
| 1737 | midi2_midi1_ep_in_desc.wMaxPacketSize = cpu_to_le16(512);
|
| 1738 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1739 | midi2_midi2_ep_out_desc[i].wMaxPacketSize =
|
| 1740 | cpu_to_le16(512);
|
| 1741 | midi2_midi2_ep_in_desc[i].wMaxPacketSize =
|
| 1742 | cpu_to_le16(512);
|
| 1743 | }
|
| 1744 | fallthrough;
|
| 1745 | case USB_SPEED_FULL:
|
| 1746 | midi1_in_eps = midi2_midi1_ep_in_descs;
|
| 1747 | midi1_out_eps = midi2_midi1_ep_out_descs;
|
| 1748 | break;
|
| 1749 | case USB_SPEED_SUPER:
|
| 1750 | midi2_midi1_ep_out_desc.wMaxPacketSize = cpu_to_le16(1024);
|
| 1751 | midi2_midi1_ep_in_desc.wMaxPacketSize = cpu_to_le16(1024);
|
| 1752 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1753 | midi2_midi2_ep_out_desc[i].wMaxPacketSize =
|
| 1754 | cpu_to_le16(1024);
|
| 1755 | midi2_midi2_ep_in_desc[i].wMaxPacketSize =
|
| 1756 | cpu_to_le16(1024);
|
| 1757 | }
|
| 1758 | midi1_in_eps = midi2_midi1_ep_in_ss_descs;
|
| 1759 | midi1_out_eps = midi2_midi1_ep_out_ss_descs;
|
| 1760 | break;
|
| 1761 | }
|
| 1762 |
|
| 1763 | err = append_configs(config, midi2_audio_descs);
|
| 1764 | if (err < 0)
|
| 1765 | return err;
|
| 1766 |
|
| 1767 | if (midi2->num_midi1_in && midi2->num_midi1_out)
|
| 1768 | midi2_midi1_if_desc.bNumEndpoints = 2;
|
| 1769 | else
|
| 1770 | midi2_midi1_if_desc.bNumEndpoints = 1;
|
| 1771 |
|
| 1772 | err = append_configs(config, midi2_midi1_descs);
|
| 1773 | if (err < 0)
|
| 1774 | return err;
|
| 1775 |
|
| 1776 | total = USB_DT_MS_HEADER_SIZE;
|
| 1777 | if (midi2->num_midi1_out) {
|
| 1778 | midi2_midi1_ep_out_class_desc.bLength =
|
| 1779 | USB_DT_MS_ENDPOINT_SIZE(midi2->num_midi1_out);
|
| 1780 | total += midi2_midi1_ep_out_class_desc.bLength;
|
| 1781 | midi2_midi1_ep_out_class_desc.bNumEmbMIDIJack =
|
| 1782 | midi2->num_midi1_out;
|
| 1783 | total += midi2->num_midi1_out *
|
| 1784 | (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
|
| 1785 | for (i = 0; i < midi2->num_midi1_out; i++) {
|
| 1786 | jack = append_midi1_in_jack(midi2, config,
|
| 1787 | &midi2->in_cable_mapping[i],
|
| 1788 | USB_MS_EMBEDDED);
|
| 1789 | if (jack < 0)
|
| 1790 | return jack;
|
| 1791 | midi2_midi1_ep_out_class_desc.baAssocJackID[i] = jack;
|
| 1792 | jack = append_midi1_out_jack(midi2, config,
|
| 1793 | &midi2->in_cable_mapping[i],
|
| 1794 | USB_MS_EXTERNAL, jack);
|
| 1795 | if (jack < 0)
|
| 1796 | return jack;
|
| 1797 | }
|
| 1798 | }
|
| 1799 |
|
| 1800 | if (midi2->num_midi1_in) {
|
| 1801 | midi2_midi1_ep_in_class_desc.bLength =
|
| 1802 | USB_DT_MS_ENDPOINT_SIZE(midi2->num_midi1_in);
|
| 1803 | total += midi2_midi1_ep_in_class_desc.bLength;
|
| 1804 | midi2_midi1_ep_in_class_desc.bNumEmbMIDIJack =
|
| 1805 | midi2->num_midi1_in;
|
| 1806 | total += midi2->num_midi1_in *
|
| 1807 | (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
|
| 1808 | for (i = 0; i < midi2->num_midi1_in; i++) {
|
| 1809 | jack = append_midi1_in_jack(midi2, config,
|
| 1810 | &midi2->out_cable_mapping[i],
|
| 1811 | USB_MS_EXTERNAL);
|
| 1812 | if (jack < 0)
|
| 1813 | return jack;
|
| 1814 | jack = append_midi1_out_jack(midi2, config,
|
| 1815 | &midi2->out_cable_mapping[i],
|
| 1816 | USB_MS_EMBEDDED, jack);
|
| 1817 | if (jack < 0)
|
| 1818 | return jack;
|
| 1819 | midi2_midi1_ep_in_class_desc.baAssocJackID[i] = jack;
|
| 1820 | }
|
| 1821 | }
|
| 1822 |
|
| 1823 | midi2_midi1_class_desc.wTotalLength = cpu_to_le16(total);
|
| 1824 |
|
| 1825 | if (midi2->num_midi1_out) {
|
| 1826 | err = append_configs(config, midi1_out_eps);
|
| 1827 | if (err < 0)
|
| 1828 | return err;
|
| 1829 | }
|
| 1830 | if (midi2->num_midi1_in) {
|
| 1831 | err = append_configs(config, midi1_in_eps);
|
| 1832 | if (err < 0)
|
| 1833 | return err;
|
| 1834 | }
|
| 1835 |
|
| 1836 | err = append_configs(config, midi2_midi2_descs);
|
| 1837 | if (err < 0)
|
| 1838 | return err;
|
| 1839 |
|
| 1840 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1841 | err = append_config(config, &midi2_midi2_ep_out_desc[i]);
|
| 1842 | if (err < 0)
|
| 1843 | return err;
|
| 1844 | if (speed == USB_SPEED_SUPER || speed == USB_SPEED_SUPER_PLUS) {
|
| 1845 | err = append_config(config, &midi2_midi2_ep_out_ss_comp_desc);
|
| 1846 | if (err < 0)
|
| 1847 | return err;
|
| 1848 | }
|
| 1849 | err = append_config(config, &midi2_midi2_ep_out_class_desc[i]);
|
| 1850 | if (err < 0)
|
| 1851 | return err;
|
| 1852 | err = append_config(config, &midi2_midi2_ep_in_desc[i]);
|
| 1853 | if (err < 0)
|
| 1854 | return err;
|
| 1855 | if (speed == USB_SPEED_SUPER || speed == USB_SPEED_SUPER_PLUS) {
|
| 1856 | err = append_config(config, &midi2_midi2_ep_in_ss_comp_desc);
|
| 1857 | if (err < 0)
|
| 1858 | return err;
|
| 1859 | }
|
| 1860 | err = append_config(config, &midi2_midi2_ep_in_class_desc[i]);
|
| 1861 | if (err < 0)
|
| 1862 | return err;
|
| 1863 | }
|
| 1864 |
|
| 1865 | return 0;
|
| 1866 | }
|
| 1867 |
|
| 1868 | static void f_midi2_free_usb_configs(struct f_midi2_usb_config *config)
|
| 1869 | {
|
| 1870 | kfree(config->list);
|
| 1871 | memset(config, 0, sizeof(*config));
|
| 1872 | }
|
| 1873 |
|
| 1874 |
|
| 1875 | static DEFINE_MUTEX(f_midi2_desc_mutex);
|
| 1876 |
|
| 1877 |
|
| 1878 | static void fill_midi2_class_desc(struct f_midi2_ep *ep,
|
| 1879 | struct usb_ms20_endpoint_descriptor_32 *cdesc)
|
| 1880 | {
|
| 1881 | int blk;
|
| 1882 |
|
| 1883 | cdesc->bLength = USB_DT_MS20_ENDPOINT_SIZE(ep->num_blks);
|
| 1884 | cdesc->bDescriptorType = USB_DT_CS_ENDPOINT;
|
| 1885 | cdesc->bDescriptorSubtype = USB_MS_GENERAL_2_0;
|
| 1886 | cdesc->bNumGrpTrmBlock = ep->num_blks;
|
| 1887 | for (blk = 0; blk < ep->num_blks; blk++)
|
| 1888 | cdesc->baAssoGrpTrmBlkID[blk] = ep->blks[blk].gtb_id;
|
| 1889 | }
|
| 1890 |
|
| 1891 |
|
| 1892 | static int f_midi2_init_midi2_ep_in(struct f_midi2 *midi2, int index)
|
| 1893 | {
|
| 1894 | struct f_midi2_ep *ep = &midi2->midi2_eps[index];
|
| 1895 | struct usb_endpoint_descriptor *desc = &midi2_midi2_ep_in_desc[index];
|
| 1896 |
|
| 1897 | desc->bLength = USB_DT_ENDPOINT_SIZE;
|
| 1898 | desc->bDescriptorType = USB_DT_ENDPOINT;
|
| 1899 | desc->bEndpointAddress = USB_DIR_IN;
|
| 1900 | desc->bmAttributes = USB_ENDPOINT_XFER_INT;
|
| 1901 | desc->wMaxPacketSize = cpu_to_le16(EP_MAX_PACKET_INT);
|
| 1902 | desc->bInterval = 1;
|
| 1903 |
|
| 1904 | fill_midi2_class_desc(ep, &midi2_midi2_ep_in_class_desc[index]);
|
| 1905 |
|
| 1906 | return f_midi2_init_ep(midi2, ep, &ep->ep_in, desc,
|
| 1907 | f_midi2_ep_in_complete);
|
| 1908 | }
|
| 1909 |
|
| 1910 |
|
| 1911 | static int f_midi2_init_midi2_ep_out(struct f_midi2 *midi2, int index)
|
| 1912 | {
|
| 1913 | struct f_midi2_ep *ep = &midi2->midi2_eps[index];
|
| 1914 | struct usb_endpoint_descriptor *desc = &midi2_midi2_ep_out_desc[index];
|
| 1915 |
|
| 1916 | desc->bLength = USB_DT_ENDPOINT_SIZE;
|
| 1917 | desc->bDescriptorType = USB_DT_ENDPOINT;
|
| 1918 | desc->bEndpointAddress = USB_DIR_OUT;
|
| 1919 | desc->bmAttributes = USB_ENDPOINT_XFER_BULK;
|
| 1920 |
|
| 1921 | fill_midi2_class_desc(ep, &midi2_midi2_ep_out_class_desc[index]);
|
| 1922 |
|
| 1923 | return f_midi2_init_ep(midi2, ep, &ep->ep_out, desc,
|
| 1924 | f_midi2_ep_out_complete);
|
| 1925 | }
|
| 1926 |
|
| 1927 |
|
| 1928 | static int f_midi2_bind(struct usb_configuration *c, struct usb_function *f)
|
| 1929 | {
|
| 1930 | struct usb_composite_dev *cdev = c->cdev;
|
| 1931 | struct f_midi2 *midi2 = func_to_midi2(f);
|
| 1932 | struct f_midi2_ep *ep;
|
| 1933 | struct f_midi2_usb_config config = {};
|
| 1934 | struct usb_gadget_strings string_fn = {
|
| 1935 | .language = 0x0409,
|
| 1936 | .strings = midi2->string_defs,
|
| 1937 | };
|
| 1938 | struct usb_gadget_strings *strings[] = {
|
| 1939 | &string_fn,
|
| 1940 | NULL,
|
| 1941 | };
|
| 1942 | int i, blk, status;
|
| 1943 |
|
| 1944 | midi2->gadget = cdev->gadget;
|
| 1945 | midi2->operation_mode = MIDI_OP_MODE_UNSET;
|
| 1946 |
|
| 1947 | status = f_midi2_create_card(midi2);
|
| 1948 | if (status < 0)
|
| 1949 | goto fail_register;
|
| 1950 |
|
| 1951 |
|
| 1952 | midi2->strings = usb_gstrings_attach(c->cdev, strings,
|
| 1953 | midi2->total_blocks + 1);
|
| 1954 | if (IS_ERR(midi2->strings)) {
|
| 1955 | status = PTR_ERR(midi2->strings);
|
| 1956 | goto fail_string;
|
| 1957 | }
|
| 1958 |
|
| 1959 | mutex_lock(&f_midi2_desc_mutex);
|
| 1960 | midi2_midi1_if_desc.iInterface = midi2->strings[STR_IFACE].id;
|
| 1961 | midi2_midi2_if_desc.iInterface = midi2->strings[STR_IFACE].id;
|
| 1962 | for (i = 0; i < midi2->num_eps; i++) {
|
| 1963 | ep = &midi2->midi2_eps[i];
|
| 1964 | for (blk = 0; blk < ep->num_blks; blk++)
|
| 1965 | ep->blks[blk].string_id =
|
| 1966 | midi2->strings[gtb_to_str_id(ep->blks[blk].gtb_id)].id;
|
| 1967 | }
|
| 1968 |
|
| 1969 | midi2_midi2_if_desc.bNumEndpoints = midi2->num_eps * 2;
|
| 1970 |
|
| 1971 |
|
| 1972 | status = usb_interface_id(c, f);
|
| 1973 | if (status < 0)
|
| 1974 | goto fail;
|
| 1975 | midi2_audio_if_desc.bInterfaceNumber = status;
|
| 1976 |
|
| 1977 |
|
| 1978 | status = usb_interface_id(c, f);
|
| 1979 | if (status < 0)
|
| 1980 | goto fail;
|
| 1981 | midi2->midi_if = status;
|
| 1982 | midi2_midi1_if_desc.bInterfaceNumber = status;
|
| 1983 | midi2_midi2_if_desc.bInterfaceNumber = status;
|
| 1984 | midi2_audio_class_desc.baInterfaceNr[0] = status;
|
| 1985 |
|
| 1986 |
|
| 1987 | if (midi2->midi2_eps[0].blks[0].info.direction != SNDRV_UMP_DIR_OUTPUT) {
|
| 1988 | status = f_midi2_init_ep(midi2, NULL, &midi2->midi1_ep_in,
|
| 1989 | &midi2_midi1_ep_in_desc,
|
| 1990 | f_midi2_midi1_ep_in_complete);
|
| 1991 | if (status)
|
| 1992 | goto fail;
|
| 1993 | }
|
| 1994 |
|
| 1995 | if (midi2->midi2_eps[0].blks[0].info.direction != SNDRV_UMP_DIR_INPUT) {
|
| 1996 | status = f_midi2_init_ep(midi2, NULL, &midi2->midi1_ep_out,
|
| 1997 | &midi2_midi1_ep_out_desc,
|
| 1998 | f_midi2_midi1_ep_out_complete);
|
| 1999 | if (status)
|
| 2000 | goto fail;
|
| 2001 | }
|
| 2002 |
|
| 2003 | for (i = 0; i < midi2->num_eps; i++) {
|
| 2004 | status = f_midi2_init_midi2_ep_in(midi2, i);
|
| 2005 | if (status)
|
| 2006 | goto fail;
|
| 2007 | status = f_midi2_init_midi2_ep_out(midi2, i);
|
| 2008 | if (status)
|
| 2009 | goto fail;
|
| 2010 | }
|
| 2011 |
|
| 2012 | status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_FULL);
|
| 2013 | if (status < 0)
|
| 2014 | goto fail;
|
| 2015 | f->fs_descriptors = usb_copy_descriptors(config.list);
|
| 2016 | if (!f->fs_descriptors) {
|
| 2017 | status = -ENOMEM;
|
| 2018 | goto fail;
|
| 2019 | }
|
| 2020 | f_midi2_free_usb_configs(&config);
|
| 2021 |
|
| 2022 | status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_HIGH);
|
| 2023 | if (status < 0)
|
| 2024 | goto fail;
|
| 2025 | f->hs_descriptors = usb_copy_descriptors(config.list);
|
| 2026 | if (!f->hs_descriptors) {
|
| 2027 | status = -ENOMEM;
|
| 2028 | goto fail;
|
| 2029 | }
|
| 2030 | f_midi2_free_usb_configs(&config);
|
| 2031 |
|
| 2032 | status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_SUPER);
|
| 2033 | if (status < 0)
|
| 2034 | goto fail;
|
| 2035 | f->ss_descriptors = usb_copy_descriptors(config.list);
|
| 2036 | if (!f->ss_descriptors) {
|
| 2037 | status = -ENOMEM;
|
| 2038 | goto fail;
|
| 2039 | }
|
| 2040 | f_midi2_free_usb_configs(&config);
|
| 2041 |
|
| 2042 | mutex_unlock(&f_midi2_desc_mutex);
|
| 2043 | return 0;
|
| 2044 |
|
| 2045 | fail:
|
| 2046 | f_midi2_free_usb_configs(&config);
|
| 2047 | mutex_unlock(&f_midi2_desc_mutex);
|
| 2048 | usb_free_all_descriptors(f);
|
| 2049 | fail_string:
|
| 2050 | f_midi2_free_card(midi2);
|
| 2051 | fail_register:
|
| 2052 | ERROR(midi2, "%s: can't bind, err %d\n", f->name, status);
|
| 2053 | return status;
|
| 2054 | }
|
| 2055 |
|
| 2056 |
|
| 2057 | static void f_midi2_unbind(struct usb_configuration *c, struct usb_function *f)
|
| 2058 | {
|
| 2059 | struct f_midi2 *midi2 = func_to_midi2(f);
|
| 2060 | int i;
|
| 2061 |
|
| 2062 | f_midi2_free_card(midi2);
|
| 2063 |
|
| 2064 | f_midi2_free_ep(&midi2->midi1_ep_in);
|
| 2065 | f_midi2_free_ep(&midi2->midi1_ep_out);
|
| 2066 | for (i = 0; i < midi2->num_eps; i++) {
|
| 2067 | f_midi2_free_ep(&midi2->midi2_eps[i].ep_in);
|
| 2068 | f_midi2_free_ep(&midi2->midi2_eps[i].ep_out);
|
| 2069 | }
|
| 2070 |
|
| 2071 | usb_free_all_descriptors(f);
|
| 2072 | }
|
| 2073 |
|
| 2074 | |
| 2075 | * ConfigFS interface
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 | static inline struct f_midi2_opts *to_f_midi2_opts(struct config_item *item)
|
| 2080 | {
|
| 2081 | return container_of(to_config_group(item), struct f_midi2_opts,
|
| 2082 | func_inst.group);
|
| 2083 | }
|
| 2084 |
|
| 2085 | static inline struct f_midi2_ep_opts *
|
| 2086 | to_f_midi2_ep_opts(struct config_item *item)
|
| 2087 | {
|
| 2088 | return container_of(to_config_group(item), struct f_midi2_ep_opts,
|
| 2089 | group);
|
| 2090 | }
|
| 2091 |
|
| 2092 | static inline struct f_midi2_block_opts *
|
| 2093 | to_f_midi2_block_opts(struct config_item *item)
|
| 2094 | {
|
| 2095 | return container_of(to_config_group(item), struct f_midi2_block_opts,
|
| 2096 | group);
|
| 2097 | }
|
| 2098 |
|
| 2099 |
|
| 2100 | static void make_name_string(char *s)
|
| 2101 | {
|
| 2102 | char *p;
|
| 2103 |
|
| 2104 | p = strchr(s, '\n');
|
| 2105 | if (p)
|
| 2106 | *p = 0;
|
| 2107 |
|
| 2108 | p = s + strlen(s);
|
| 2109 | for (; p > s && isspace(*p); p--)
|
| 2110 | *p = 0;
|
| 2111 | }
|
| 2112 |
|
| 2113 |
|
| 2114 | static ssize_t f_midi2_opts_uint_show(struct f_midi2_opts *opts,
|
| 2115 | u32 val, const char *format, char *page)
|
| 2116 | {
|
| 2117 | int result;
|
| 2118 |
|
| 2119 | mutex_lock(&opts->lock);
|
| 2120 | result = sprintf(page, format, val);
|
| 2121 | mutex_unlock(&opts->lock);
|
| 2122 | return result;
|
| 2123 | }
|
| 2124 |
|
| 2125 | static ssize_t f_midi2_opts_uint_store(struct f_midi2_opts *opts,
|
| 2126 | u32 *valp, u32 minval, u32 maxval,
|
| 2127 | const char *page, size_t len)
|
| 2128 | {
|
| 2129 | int ret;
|
| 2130 | u32 val;
|
| 2131 |
|
| 2132 | mutex_lock(&opts->lock);
|
| 2133 | if (opts->refcnt) {
|
| 2134 | ret = -EBUSY;
|
| 2135 | goto end;
|
| 2136 | }
|
| 2137 |
|
| 2138 | ret = kstrtou32(page, 0, &val);
|
| 2139 | if (ret)
|
| 2140 | goto end;
|
| 2141 | if (val < minval || val > maxval) {
|
| 2142 | ret = -EINVAL;
|
| 2143 | goto end;
|
| 2144 | }
|
| 2145 |
|
| 2146 | *valp = val;
|
| 2147 | ret = len;
|
| 2148 |
|
| 2149 | end:
|
| 2150 | mutex_unlock(&opts->lock);
|
| 2151 | return ret;
|
| 2152 | }
|
| 2153 |
|
| 2154 |
|
| 2155 | static ssize_t f_midi2_opts_bool_store(struct f_midi2_opts *opts,
|
| 2156 | bool *valp, const char *page, size_t len)
|
| 2157 | {
|
| 2158 | int ret;
|
| 2159 | bool val;
|
| 2160 |
|
| 2161 | mutex_lock(&opts->lock);
|
| 2162 | if (opts->refcnt) {
|
| 2163 | ret = -EBUSY;
|
| 2164 | goto end;
|
| 2165 | }
|
| 2166 |
|
| 2167 | ret = kstrtobool(page, &val);
|
| 2168 | if (ret)
|
| 2169 | goto end;
|
| 2170 | *valp = val;
|
| 2171 | ret = len;
|
| 2172 |
|
| 2173 | end:
|
| 2174 | mutex_unlock(&opts->lock);
|
| 2175 | return ret;
|
| 2176 | }
|
| 2177 |
|
| 2178 |
|
| 2179 | static ssize_t f_midi2_opts_str_show(struct f_midi2_opts *opts,
|
| 2180 | const char **strp, char *page)
|
| 2181 | {
|
| 2182 | int result = 0;
|
| 2183 |
|
| 2184 | mutex_lock(&opts->lock);
|
| 2185 | if (*strp)
|
| 2186 | result = scnprintf(page, PAGE_SIZE, "%s\n", *strp);
|
| 2187 | mutex_unlock(&opts->lock);
|
| 2188 | return result;
|
| 2189 | }
|
| 2190 |
|
| 2191 | static ssize_t f_midi2_opts_str_store(struct f_midi2_opts *opts,
|
| 2192 | const char **strp, size_t maxlen,
|
| 2193 | const char *page, size_t len)
|
| 2194 | {
|
| 2195 | char *c;
|
| 2196 | int ret;
|
| 2197 |
|
| 2198 | mutex_lock(&opts->lock);
|
| 2199 | if (opts->refcnt) {
|
| 2200 | ret = -EBUSY;
|
| 2201 | goto end;
|
| 2202 | }
|
| 2203 |
|
| 2204 | c = kstrndup(page, min(len, maxlen), GFP_KERNEL);
|
| 2205 | if (!c) {
|
| 2206 | ret = -ENOMEM;
|
| 2207 | goto end;
|
| 2208 | }
|
| 2209 |
|
| 2210 | kfree(*strp);
|
| 2211 | make_name_string(c);
|
| 2212 | *strp = c;
|
| 2213 | ret = len;
|
| 2214 |
|
| 2215 | end:
|
| 2216 | mutex_unlock(&opts->lock);
|
| 2217 | return ret;
|
| 2218 | }
|
| 2219 |
|
| 2220 | |
| 2221 | * Definitions for UMP Block config
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 | #define F_MIDI2_BLOCK_OPT(name, format, minval, maxval) \
|
| 2226 | static ssize_t f_midi2_block_opts_##name##_show(struct config_item *item,\
|
| 2227 | char *page) \
|
| 2228 | { \
|
| 2229 | struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
|
| 2230 | return f_midi2_opts_uint_show(opts->ep->opts, opts->info.name, \
|
| 2231 | format "\n", page); \
|
| 2232 | } \
|
| 2233 | \
|
| 2234 | static ssize_t f_midi2_block_opts_##name##_store(struct config_item *item,\
|
| 2235 | const char *page, size_t len) \
|
| 2236 | { \
|
| 2237 | struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
|
| 2238 | return f_midi2_opts_uint_store(opts->ep->opts, &opts->info.name,\
|
| 2239 | minval, maxval, page, len); \
|
| 2240 | } \
|
| 2241 | \
|
| 2242 | CONFIGFS_ATTR(f_midi2_block_opts_, name)
|
| 2243 |
|
| 2244 |
|
| 2245 | #define F_MIDI2_BLOCK_BOOL_OPT(name) \
|
| 2246 | static ssize_t f_midi2_block_opts_##name##_show(struct config_item *item,\
|
| 2247 | char *page) \
|
| 2248 | { \
|
| 2249 | struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
|
| 2250 | return f_midi2_opts_uint_show(opts->ep->opts, opts->info.name, \
|
| 2251 | "%u\n", page); \
|
| 2252 | } \
|
| 2253 | \
|
| 2254 | static ssize_t f_midi2_block_opts_##name##_store(struct config_item *item,\
|
| 2255 | const char *page, size_t len) \
|
| 2256 | { \
|
| 2257 | struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
|
| 2258 | return f_midi2_opts_bool_store(opts->ep->opts, &opts->info.name,\
|
| 2259 | page, len); \
|
| 2260 | } \
|
| 2261 | \
|
| 2262 | CONFIGFS_ATTR(f_midi2_block_opts_, name)
|
| 2263 |
|
| 2264 | F_MIDI2_BLOCK_OPT(direction, "0x%x", 1, 3);
|
| 2265 | F_MIDI2_BLOCK_OPT(first_group, "0x%x", 0, 15);
|
| 2266 | F_MIDI2_BLOCK_OPT(num_groups, "0x%x", 1, 16);
|
| 2267 | F_MIDI2_BLOCK_OPT(midi1_first_group, "0x%x", 0, 15);
|
| 2268 | F_MIDI2_BLOCK_OPT(midi1_num_groups, "0x%x", 0, 16);
|
| 2269 | F_MIDI2_BLOCK_OPT(ui_hint, "0x%x", 0, 3);
|
| 2270 | F_MIDI2_BLOCK_OPT(midi_ci_version, "%u", 0, 1);
|
| 2271 | F_MIDI2_BLOCK_OPT(sysex8_streams, "%u", 0, 255);
|
| 2272 | F_MIDI2_BLOCK_OPT(is_midi1, "%u", 0, 2);
|
| 2273 | F_MIDI2_BLOCK_BOOL_OPT(active);
|
| 2274 |
|
| 2275 | static ssize_t f_midi2_block_opts_name_show(struct config_item *item,
|
| 2276 | char *page)
|
| 2277 | {
|
| 2278 | struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
|
| 2279 |
|
| 2280 | return f_midi2_opts_str_show(opts->ep->opts, &opts->info.name, page);
|
| 2281 | }
|
| 2282 |
|
| 2283 | static ssize_t f_midi2_block_opts_name_store(struct config_item *item,
|
| 2284 | const char *page, size_t len)
|
| 2285 | {
|
| 2286 | struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
|
| 2287 |
|
| 2288 | return f_midi2_opts_str_store(opts->ep->opts, &opts->info.name, 128,
|
| 2289 | page, len);
|
| 2290 | }
|
| 2291 |
|
| 2292 | CONFIGFS_ATTR(f_midi2_block_opts_, name);
|
| 2293 |
|
| 2294 | static struct configfs_attribute *f_midi2_block_attrs[] = {
|
| 2295 | &f_midi2_block_opts_attr_direction,
|
| 2296 | &f_midi2_block_opts_attr_first_group,
|
| 2297 | &f_midi2_block_opts_attr_num_groups,
|
| 2298 | &f_midi2_block_opts_attr_midi1_first_group,
|
| 2299 | &f_midi2_block_opts_attr_midi1_num_groups,
|
| 2300 | &f_midi2_block_opts_attr_ui_hint,
|
| 2301 | &f_midi2_block_opts_attr_midi_ci_version,
|
| 2302 | &f_midi2_block_opts_attr_sysex8_streams,
|
| 2303 | &f_midi2_block_opts_attr_is_midi1,
|
| 2304 | &f_midi2_block_opts_attr_active,
|
| 2305 | &f_midi2_block_opts_attr_name,
|
| 2306 | NULL,
|
| 2307 | };
|
| 2308 |
|
| 2309 | static void f_midi2_block_opts_release(struct config_item *item)
|
| 2310 | {
|
| 2311 | struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
|
| 2312 |
|
| 2313 | kfree(opts->info.name);
|
| 2314 | kfree(opts);
|
| 2315 | }
|
| 2316 |
|
| 2317 | static const struct configfs_item_operations f_midi2_block_item_ops = {
|
| 2318 | .release = f_midi2_block_opts_release,
|
| 2319 | };
|
| 2320 |
|
| 2321 | static const struct config_item_type f_midi2_block_type = {
|
| 2322 | .ct_item_ops = &f_midi2_block_item_ops,
|
| 2323 | .ct_attrs = f_midi2_block_attrs,
|
| 2324 | .ct_owner = THIS_MODULE,
|
| 2325 | };
|
| 2326 |
|
| 2327 |
|
| 2328 | static int f_midi2_block_opts_create(struct f_midi2_ep_opts *ep_opts,
|
| 2329 | unsigned int blk,
|
| 2330 | struct f_midi2_block_opts **block_p)
|
| 2331 | {
|
| 2332 | struct f_midi2_block_opts *block_opts;
|
| 2333 | int ret = 0;
|
| 2334 |
|
| 2335 | mutex_lock(&ep_opts->opts->lock);
|
| 2336 | if (ep_opts->opts->refcnt || ep_opts->blks[blk]) {
|
| 2337 | ret = -EBUSY;
|
| 2338 | goto out;
|
| 2339 | }
|
| 2340 |
|
| 2341 | block_opts = kzalloc_obj(*block_opts);
|
| 2342 | if (!block_opts) {
|
| 2343 | ret = -ENOMEM;
|
| 2344 | goto out;
|
| 2345 | }
|
| 2346 |
|
| 2347 | block_opts->ep = ep_opts;
|
| 2348 | block_opts->id = blk;
|
| 2349 |
|
| 2350 |
|
| 2351 | block_opts->info.direction = SNDRV_UMP_DIR_BIDIRECTION;
|
| 2352 | block_opts->info.first_group = 0;
|
| 2353 | block_opts->info.num_groups = 1;
|
| 2354 | block_opts->info.ui_hint = SNDRV_UMP_BLOCK_UI_HINT_BOTH;
|
| 2355 | block_opts->info.active = 1;
|
| 2356 |
|
| 2357 | ep_opts->blks[blk] = block_opts;
|
| 2358 | *block_p = block_opts;
|
| 2359 |
|
| 2360 | out:
|
| 2361 | mutex_unlock(&ep_opts->opts->lock);
|
| 2362 | return ret;
|
| 2363 | }
|
| 2364 |
|
| 2365 |
|
| 2366 | static struct config_group *
|
| 2367 | f_midi2_opts_block_make(struct config_group *group, const char *name)
|
| 2368 | {
|
| 2369 | struct f_midi2_ep_opts *ep_opts;
|
| 2370 | struct f_midi2_block_opts *block_opts;
|
| 2371 | unsigned int blk;
|
| 2372 | int ret;
|
| 2373 |
|
| 2374 | if (strncmp(name, "block.", 6))
|
| 2375 | return ERR_PTR(-EINVAL);
|
| 2376 | ret = kstrtouint(name + 6, 10, &blk);
|
| 2377 | if (ret)
|
| 2378 | return ERR_PTR(ret);
|
| 2379 |
|
| 2380 | ep_opts = to_f_midi2_ep_opts(&group->cg_item);
|
| 2381 |
|
| 2382 | if (blk >= SNDRV_UMP_MAX_BLOCKS)
|
| 2383 | return ERR_PTR(-EINVAL);
|
| 2384 | if (ep_opts->blks[blk])
|
| 2385 | return ERR_PTR(-EBUSY);
|
| 2386 | ret = f_midi2_block_opts_create(ep_opts, blk, &block_opts);
|
| 2387 | if (ret)
|
| 2388 | return ERR_PTR(ret);
|
| 2389 |
|
| 2390 | config_group_init_type_name(&block_opts->group, name,
|
| 2391 | &f_midi2_block_type);
|
| 2392 | return &block_opts->group;
|
| 2393 | }
|
| 2394 |
|
| 2395 |
|
| 2396 | static void
|
| 2397 | f_midi2_opts_block_drop(struct config_group *group, struct config_item *item)
|
| 2398 | {
|
| 2399 | struct f_midi2_block_opts *block_opts = to_f_midi2_block_opts(item);
|
| 2400 |
|
| 2401 | mutex_lock(&block_opts->ep->opts->lock);
|
| 2402 | block_opts->ep->blks[block_opts->id] = NULL;
|
| 2403 | mutex_unlock(&block_opts->ep->opts->lock);
|
| 2404 | config_item_put(item);
|
| 2405 | }
|
| 2406 |
|
| 2407 | |
| 2408 | * Definitions for UMP Endpoint config
|
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 | #define F_MIDI2_EP_OPT(name, format, minval, maxval) \
|
| 2413 | static ssize_t f_midi2_ep_opts_##name##_show(struct config_item *item, \
|
| 2414 | char *page) \
|
| 2415 | { \
|
| 2416 | struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
|
| 2417 | return f_midi2_opts_uint_show(opts->opts, opts->info.name, \
|
| 2418 | format "\n", page); \
|
| 2419 | } \
|
| 2420 | \
|
| 2421 | static ssize_t f_midi2_ep_opts_##name##_store(struct config_item *item, \
|
| 2422 | const char *page, size_t len)\
|
| 2423 | { \
|
| 2424 | struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
|
| 2425 | return f_midi2_opts_uint_store(opts->opts, &opts->info.name, \
|
| 2426 | minval, maxval, page, len); \
|
| 2427 | } \
|
| 2428 | \
|
| 2429 | CONFIGFS_ATTR(f_midi2_ep_opts_, name)
|
| 2430 |
|
| 2431 |
|
| 2432 | #define F_MIDI2_EP_STR_OPT(name, maxlen) \
|
| 2433 | static ssize_t f_midi2_ep_opts_##name##_show(struct config_item *item, \
|
| 2434 | char *page) \
|
| 2435 | { \
|
| 2436 | struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
|
| 2437 | return f_midi2_opts_str_show(opts->opts, &opts->info.name, page);\
|
| 2438 | } \
|
| 2439 | \
|
| 2440 | static ssize_t f_midi2_ep_opts_##name##_store(struct config_item *item, \
|
| 2441 | const char *page, size_t len) \
|
| 2442 | { \
|
| 2443 | struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
|
| 2444 | return f_midi2_opts_str_store(opts->opts, &opts->info.name, maxlen,\
|
| 2445 | page, len); \
|
| 2446 | } \
|
| 2447 | \
|
| 2448 | CONFIGFS_ATTR(f_midi2_ep_opts_, name)
|
| 2449 |
|
| 2450 | F_MIDI2_EP_OPT(protocol, "0x%x", 1, 2);
|
| 2451 | F_MIDI2_EP_OPT(protocol_caps, "0x%x", 1, 3);
|
| 2452 | F_MIDI2_EP_OPT(manufacturer, "0x%x", 0, 0xffffff);
|
| 2453 | F_MIDI2_EP_OPT(family, "0x%x", 0, 0xffff);
|
| 2454 | F_MIDI2_EP_OPT(model, "0x%x", 0, 0xffff);
|
| 2455 | F_MIDI2_EP_OPT(sw_revision, "0x%x", 0, 0xffffffff);
|
| 2456 | F_MIDI2_EP_STR_OPT(ep_name, 128);
|
| 2457 | F_MIDI2_EP_STR_OPT(product_id, 128);
|
| 2458 |
|
| 2459 | static struct configfs_attribute *f_midi2_ep_attrs[] = {
|
| 2460 | &f_midi2_ep_opts_attr_protocol,
|
| 2461 | &f_midi2_ep_opts_attr_protocol_caps,
|
| 2462 | &f_midi2_ep_opts_attr_ep_name,
|
| 2463 | &f_midi2_ep_opts_attr_product_id,
|
| 2464 | &f_midi2_ep_opts_attr_manufacturer,
|
| 2465 | &f_midi2_ep_opts_attr_family,
|
| 2466 | &f_midi2_ep_opts_attr_model,
|
| 2467 | &f_midi2_ep_opts_attr_sw_revision,
|
| 2468 | NULL,
|
| 2469 | };
|
| 2470 |
|
| 2471 | static void f_midi2_ep_opts_release(struct config_item *item)
|
| 2472 | {
|
| 2473 | struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item);
|
| 2474 |
|
| 2475 | configfs_remove_default_groups(&opts->group);
|
| 2476 | kfree(opts->info.ep_name);
|
| 2477 | kfree(opts->info.product_id);
|
| 2478 | kfree(opts);
|
| 2479 | }
|
| 2480 |
|
| 2481 | static const struct configfs_item_operations f_midi2_ep_item_ops = {
|
| 2482 | .release = f_midi2_ep_opts_release,
|
| 2483 | };
|
| 2484 |
|
| 2485 | static const struct configfs_group_operations f_midi2_ep_group_ops = {
|
| 2486 | .make_group = f_midi2_opts_block_make,
|
| 2487 | .drop_item = f_midi2_opts_block_drop,
|
| 2488 | };
|
| 2489 |
|
| 2490 | static const struct config_item_type f_midi2_ep_type = {
|
| 2491 | .ct_item_ops = &f_midi2_ep_item_ops,
|
| 2492 | .ct_group_ops = &f_midi2_ep_group_ops,
|
| 2493 | .ct_attrs = f_midi2_ep_attrs,
|
| 2494 | .ct_owner = THIS_MODULE,
|
| 2495 | };
|
| 2496 |
|
| 2497 |
|
| 2498 | static int f_midi2_ep_opts_create(struct f_midi2_opts *opts,
|
| 2499 | unsigned int index,
|
| 2500 | struct f_midi2_ep_opts **ep_p)
|
| 2501 | {
|
| 2502 | struct f_midi2_ep_opts *ep_opts;
|
| 2503 |
|
| 2504 | ep_opts = kzalloc_obj(*ep_opts);
|
| 2505 | if (!ep_opts)
|
| 2506 | return -ENOMEM;
|
| 2507 |
|
| 2508 | ep_opts->opts = opts;
|
| 2509 | ep_opts->index = index;
|
| 2510 |
|
| 2511 |
|
| 2512 | ep_opts->info.protocol = 2;
|
| 2513 | ep_opts->info.protocol_caps = 3;
|
| 2514 |
|
| 2515 | opts->eps[index] = ep_opts;
|
| 2516 | *ep_p = ep_opts;
|
| 2517 | return 0;
|
| 2518 | }
|
| 2519 |
|
| 2520 |
|
| 2521 | static struct config_group *
|
| 2522 | f_midi2_opts_ep_make(struct config_group *group, const char *name)
|
| 2523 | {
|
| 2524 | struct f_midi2_opts *opts;
|
| 2525 | struct f_midi2_ep_opts *ep_opts;
|
| 2526 | unsigned int index;
|
| 2527 | int ret;
|
| 2528 |
|
| 2529 | if (strncmp(name, "ep.", 3))
|
| 2530 | return ERR_PTR(-EINVAL);
|
| 2531 | ret = kstrtouint(name + 3, 10, &index);
|
| 2532 | if (ret)
|
| 2533 | return ERR_PTR(ret);
|
| 2534 |
|
| 2535 | opts = to_f_midi2_opts(&group->cg_item);
|
| 2536 | if (index >= MAX_UMP_EPS)
|
| 2537 | return ERR_PTR(-EINVAL);
|
| 2538 | if (opts->eps[index])
|
| 2539 | return ERR_PTR(-EBUSY);
|
| 2540 | ret = f_midi2_ep_opts_create(opts, index, &ep_opts);
|
| 2541 | if (ret)
|
| 2542 | return ERR_PTR(ret);
|
| 2543 |
|
| 2544 | config_group_init_type_name(&ep_opts->group, name, &f_midi2_ep_type);
|
| 2545 | return &ep_opts->group;
|
| 2546 | }
|
| 2547 |
|
| 2548 |
|
| 2549 | static void
|
| 2550 | f_midi2_opts_ep_drop(struct config_group *group, struct config_item *item)
|
| 2551 | {
|
| 2552 | struct f_midi2_ep_opts *ep_opts = to_f_midi2_ep_opts(item);
|
| 2553 |
|
| 2554 | mutex_lock(&ep_opts->opts->lock);
|
| 2555 | ep_opts->opts->eps[ep_opts->index] = NULL;
|
| 2556 | mutex_unlock(&ep_opts->opts->lock);
|
| 2557 | config_item_put(item);
|
| 2558 | }
|
| 2559 |
|
| 2560 | |
| 2561 | * Definitions for card config
|
| 2562 |
|
| 2563 |
|
| 2564 |
|
| 2565 | #define F_MIDI2_BOOL_OPT(name) \
|
| 2566 | static ssize_t f_midi2_opts_##name##_show(struct config_item *item, \
|
| 2567 | char *page) \
|
| 2568 | { \
|
| 2569 | struct f_midi2_opts *opts = to_f_midi2_opts(item); \
|
| 2570 | return f_midi2_opts_uint_show(opts, opts->info.name, \
|
| 2571 | "%u\n", page); \
|
| 2572 | } \
|
| 2573 | \
|
| 2574 | static ssize_t f_midi2_opts_##name##_store(struct config_item *item, \
|
| 2575 | const char *page, size_t len) \
|
| 2576 | { \
|
| 2577 | struct f_midi2_opts *opts = to_f_midi2_opts(item); \
|
| 2578 | return f_midi2_opts_bool_store(opts, &opts->info.name, \
|
| 2579 | page, len); \
|
| 2580 | } \
|
| 2581 | \
|
| 2582 | CONFIGFS_ATTR(f_midi2_opts_, name)
|
| 2583 |
|
| 2584 | F_MIDI2_BOOL_OPT(process_ump);
|
| 2585 | F_MIDI2_BOOL_OPT(static_block);
|
| 2586 |
|
| 2587 | static ssize_t f_midi2_opts_iface_name_show(struct config_item *item,
|
| 2588 | char *page)
|
| 2589 | {
|
| 2590 | struct f_midi2_opts *opts = to_f_midi2_opts(item);
|
| 2591 |
|
| 2592 | return f_midi2_opts_str_show(opts, &opts->info.iface_name, page);
|
| 2593 | }
|
| 2594 |
|
| 2595 | static ssize_t f_midi2_opts_iface_name_store(struct config_item *item,
|
| 2596 | const char *page, size_t len)
|
| 2597 | {
|
| 2598 | struct f_midi2_opts *opts = to_f_midi2_opts(item);
|
| 2599 |
|
| 2600 | return f_midi2_opts_str_store(opts, &opts->info.iface_name, 128,
|
| 2601 | page, len);
|
| 2602 | }
|
| 2603 |
|
| 2604 | CONFIGFS_ATTR(f_midi2_opts_, iface_name);
|
| 2605 |
|
| 2606 | static struct configfs_attribute *f_midi2_attrs[] = {
|
| 2607 | &f_midi2_opts_attr_process_ump,
|
| 2608 | &f_midi2_opts_attr_static_block,
|
| 2609 | &f_midi2_opts_attr_iface_name,
|
| 2610 | NULL
|
| 2611 | };
|
| 2612 |
|
| 2613 | static void f_midi2_opts_release(struct config_item *item)
|
| 2614 | {
|
| 2615 | struct f_midi2_opts *opts = to_f_midi2_opts(item);
|
| 2616 |
|
| 2617 | usb_put_function_instance(&opts->func_inst);
|
| 2618 | }
|
| 2619 |
|
| 2620 | static const struct configfs_item_operations f_midi2_item_ops = {
|
| 2621 | .release = f_midi2_opts_release,
|
| 2622 | };
|
| 2623 |
|
| 2624 | static const struct configfs_group_operations f_midi2_group_ops = {
|
| 2625 | .make_group = f_midi2_opts_ep_make,
|
| 2626 | .drop_item = f_midi2_opts_ep_drop,
|
| 2627 | };
|
| 2628 |
|
| 2629 | static const struct config_item_type f_midi2_func_type = {
|
| 2630 | .ct_item_ops = &f_midi2_item_ops,
|
| 2631 | .ct_group_ops = &f_midi2_group_ops,
|
| 2632 | .ct_attrs = f_midi2_attrs,
|
| 2633 | .ct_owner = THIS_MODULE,
|
| 2634 | };
|
| 2635 |
|
| 2636 | static void f_midi2_free_inst(struct usb_function_instance *f)
|
| 2637 | {
|
| 2638 | struct f_midi2_opts *opts;
|
| 2639 |
|
| 2640 | opts = container_of(f, struct f_midi2_opts, func_inst);
|
| 2641 |
|
| 2642 | configfs_remove_default_groups(&opts->func_inst.group);
|
| 2643 | kfree(opts->info.iface_name);
|
| 2644 | kfree(opts);
|
| 2645 | }
|
| 2646 |
|
| 2647 |
|
| 2648 | static struct usb_function_instance *f_midi2_alloc_inst(void)
|
| 2649 | {
|
| 2650 | struct f_midi2_opts *opts;
|
| 2651 | struct f_midi2_ep_opts *ep_opts;
|
| 2652 | struct f_midi2_block_opts *block_opts;
|
| 2653 | int ret;
|
| 2654 |
|
| 2655 | opts = kzalloc_obj(*opts);
|
| 2656 | if (!opts)
|
| 2657 | return ERR_PTR(-ENOMEM);
|
| 2658 |
|
| 2659 | mutex_init(&opts->lock);
|
| 2660 | opts->func_inst.free_func_inst = f_midi2_free_inst;
|
| 2661 | opts->info.process_ump = true;
|
| 2662 | opts->info.static_block = true;
|
| 2663 | opts->info.num_reqs = 32;
|
| 2664 | opts->info.req_buf_size = 512;
|
| 2665 |
|
| 2666 |
|
| 2667 | ret = f_midi2_ep_opts_create(opts, 0, &ep_opts);
|
| 2668 | if (ret) {
|
| 2669 | kfree(opts);
|
| 2670 | return ERR_PTR(ret);
|
| 2671 | }
|
| 2672 |
|
| 2673 |
|
| 2674 | ret = f_midi2_block_opts_create(ep_opts, 0, &block_opts);
|
| 2675 | if (ret) {
|
| 2676 | kfree(ep_opts);
|
| 2677 | kfree(opts);
|
| 2678 | return ERR_PTR(ret);
|
| 2679 | }
|
| 2680 |
|
| 2681 |
|
| 2682 | block_opts->info.midi1_num_groups = 1;
|
| 2683 |
|
| 2684 | config_group_init_type_name(&opts->func_inst.group, "",
|
| 2685 | &f_midi2_func_type);
|
| 2686 |
|
| 2687 | config_group_init_type_name(&ep_opts->group, "ep.0",
|
| 2688 | &f_midi2_ep_type);
|
| 2689 | configfs_add_default_group(&ep_opts->group, &opts->func_inst.group);
|
| 2690 |
|
| 2691 | config_group_init_type_name(&block_opts->group, "block.0",
|
| 2692 | &f_midi2_block_type);
|
| 2693 | configfs_add_default_group(&block_opts->group, &ep_opts->group);
|
| 2694 |
|
| 2695 | return &opts->func_inst;
|
| 2696 | }
|
| 2697 |
|
| 2698 | static void do_f_midi2_free(struct f_midi2 *midi2, struct f_midi2_opts *opts)
|
| 2699 | {
|
| 2700 | mutex_lock(&opts->lock);
|
| 2701 | --opts->refcnt;
|
| 2702 | mutex_unlock(&opts->lock);
|
| 2703 | kfree(midi2->string_defs);
|
| 2704 | kfree(midi2);
|
| 2705 | }
|
| 2706 |
|
| 2707 | static void f_midi2_free(struct usb_function *f)
|
| 2708 | {
|
| 2709 | do_f_midi2_free(func_to_midi2(f),
|
| 2710 | container_of(f->fi, struct f_midi2_opts, func_inst));
|
| 2711 | }
|
| 2712 |
|
| 2713 | |
| 2714 | * return the number of EPs or a negative error
|
| 2715 |
|
| 2716 | static int verify_parameters(struct f_midi2_opts *opts)
|
| 2717 | {
|
| 2718 | int i, j, num_eps, num_blks;
|
| 2719 | struct f_midi2_ep_info *ep;
|
| 2720 | struct f_midi2_block_info *bp;
|
| 2721 |
|
| 2722 | for (num_eps = 0; num_eps < MAX_UMP_EPS && opts->eps[num_eps];
|
| 2723 | num_eps++)
|
| 2724 | ;
|
| 2725 | if (!num_eps) {
|
| 2726 | pr_err("f_midi2: No EP is defined\n");
|
| 2727 | return -EINVAL;
|
| 2728 | }
|
| 2729 |
|
| 2730 | num_blks = 0;
|
| 2731 | for (i = 0; i < num_eps; i++) {
|
| 2732 | ep = &opts->eps[i]->info;
|
| 2733 | if (!(ep->protocol_caps & ep->protocol)) {
|
| 2734 | pr_err("f_midi2: Invalid protocol 0x%x (caps 0x%x) for EP %d\n",
|
| 2735 | ep->protocol, ep->protocol_caps, i);
|
| 2736 | return -EINVAL;
|
| 2737 | }
|
| 2738 |
|
| 2739 | for (j = 0; j < SNDRV_UMP_MAX_BLOCKS && opts->eps[i]->blks[j];
|
| 2740 | j++, num_blks++) {
|
| 2741 | bp = &opts->eps[i]->blks[j]->info;
|
| 2742 | if (bp->first_group + bp->num_groups > SNDRV_UMP_MAX_GROUPS) {
|
| 2743 | pr_err("f_midi2: Invalid group definitions for block %d:%d\n",
|
| 2744 | i, j);
|
| 2745 | return -EINVAL;
|
| 2746 | }
|
| 2747 |
|
| 2748 | if (bp->midi1_num_groups) {
|
| 2749 | if (bp->midi1_first_group < bp->first_group ||
|
| 2750 | bp->midi1_first_group + bp->midi1_num_groups >
|
| 2751 | bp->first_group + bp->num_groups) {
|
| 2752 | pr_err("f_midi2: Invalid MIDI1 group definitions for block %d:%d\n",
|
| 2753 | i, j);
|
| 2754 | return -EINVAL;
|
| 2755 | }
|
| 2756 | }
|
| 2757 | }
|
| 2758 | }
|
| 2759 | if (!num_blks) {
|
| 2760 | pr_err("f_midi2: No block is defined\n");
|
| 2761 | return -EINVAL;
|
| 2762 | }
|
| 2763 |
|
| 2764 | return num_eps;
|
| 2765 | }
|
| 2766 |
|
| 2767 |
|
| 2768 | static void fill_midi1_cable_mapping(struct f_midi2 *midi2,
|
| 2769 | struct f_midi2_ep *ep,
|
| 2770 | int blk)
|
| 2771 | {
|
| 2772 | const struct f_midi2_block_info *binfo = &ep->blks[blk].info;
|
| 2773 | struct midi1_cable_mapping *map;
|
| 2774 | int i, group;
|
| 2775 |
|
| 2776 | if (!binfo->midi1_num_groups)
|
| 2777 | return;
|
| 2778 | if (binfo->direction != SNDRV_UMP_DIR_OUTPUT) {
|
| 2779 | group = binfo->midi1_first_group;
|
| 2780 | map = midi2->in_cable_mapping + midi2->num_midi1_in;
|
| 2781 | for (i = 0; i < binfo->midi1_num_groups; i++, group++, map++) {
|
| 2782 | if (midi2->num_midi1_in >= MAX_CABLES)
|
| 2783 | break;
|
| 2784 | map->ep = ep;
|
| 2785 | map->block = blk;
|
| 2786 | map->group = group;
|
| 2787 | midi2->num_midi1_in++;
|
| 2788 |
|
| 2789 | ep->in_group_to_cable[group] = midi2->num_midi1_in;
|
| 2790 | }
|
| 2791 | }
|
| 2792 |
|
| 2793 | if (binfo->direction != SNDRV_UMP_DIR_INPUT) {
|
| 2794 | group = binfo->midi1_first_group;
|
| 2795 | map = midi2->out_cable_mapping + midi2->num_midi1_out;
|
| 2796 | for (i = 0; i < binfo->midi1_num_groups; i++, group++, map++) {
|
| 2797 | if (midi2->num_midi1_out >= MAX_CABLES)
|
| 2798 | break;
|
| 2799 | map->ep = ep;
|
| 2800 | map->block = blk;
|
| 2801 | map->group = group;
|
| 2802 | midi2->num_midi1_out++;
|
| 2803 | }
|
| 2804 | }
|
| 2805 | }
|
| 2806 |
|
| 2807 |
|
| 2808 | static struct usb_function *f_midi2_alloc(struct usb_function_instance *fi)
|
| 2809 | {
|
| 2810 | struct f_midi2 *midi2;
|
| 2811 | struct f_midi2_opts *opts;
|
| 2812 | struct f_midi2_ep *ep;
|
| 2813 | struct f_midi2_block *bp;
|
| 2814 | int i, num_eps, blk;
|
| 2815 |
|
| 2816 | midi2 = kzalloc_obj(*midi2);
|
| 2817 | if (!midi2)
|
| 2818 | return ERR_PTR(-ENOMEM);
|
| 2819 |
|
| 2820 | opts = container_of(fi, struct f_midi2_opts, func_inst);
|
| 2821 | mutex_lock(&opts->lock);
|
| 2822 | num_eps = verify_parameters(opts);
|
| 2823 | if (num_eps < 0) {
|
| 2824 | mutex_unlock(&opts->lock);
|
| 2825 | kfree(midi2);
|
| 2826 | return ERR_PTR(num_eps);
|
| 2827 | }
|
| 2828 | ++opts->refcnt;
|
| 2829 | mutex_unlock(&opts->lock);
|
| 2830 |
|
| 2831 | spin_lock_init(&midi2->queue_lock);
|
| 2832 |
|
| 2833 | midi2->func.name = "midi2_func";
|
| 2834 | midi2->func.bind = f_midi2_bind;
|
| 2835 | midi2->func.unbind = f_midi2_unbind;
|
| 2836 | midi2->func.get_alt = f_midi2_get_alt;
|
| 2837 | midi2->func.set_alt = f_midi2_set_alt;
|
| 2838 | midi2->func.setup = f_midi2_setup;
|
| 2839 | midi2->func.disable = f_midi2_disable;
|
| 2840 | midi2->func.free_func = f_midi2_free;
|
| 2841 |
|
| 2842 | midi2->info = opts->info;
|
| 2843 | midi2->num_eps = num_eps;
|
| 2844 |
|
| 2845 | for (i = 0; i < num_eps; i++) {
|
| 2846 | ep = &midi2->midi2_eps[i];
|
| 2847 | ep->info = opts->eps[i]->info;
|
| 2848 | ep->card = midi2;
|
| 2849 | for (blk = 0; blk < SNDRV_UMP_MAX_BLOCKS &&
|
| 2850 | opts->eps[i]->blks[blk]; blk++) {
|
| 2851 | bp = &ep->blks[blk];
|
| 2852 | ep->num_blks++;
|
| 2853 | bp->info = opts->eps[i]->blks[blk]->info;
|
| 2854 | bp->gtb_id = ++midi2->total_blocks;
|
| 2855 | }
|
| 2856 | }
|
| 2857 |
|
| 2858 | midi2->string_defs = kzalloc_objs(*midi2->string_defs,
|
| 2859 | midi2->total_blocks + 1);
|
| 2860 | if (!midi2->string_defs) {
|
| 2861 | do_f_midi2_free(midi2, opts);
|
| 2862 | return ERR_PTR(-ENOMEM);
|
| 2863 | }
|
| 2864 |
|
| 2865 | if (opts->info.iface_name && *opts->info.iface_name)
|
| 2866 | midi2->string_defs[STR_IFACE].s = opts->info.iface_name;
|
| 2867 | else
|
| 2868 | midi2->string_defs[STR_IFACE].s = ump_ep_name(&midi2->midi2_eps[0]);
|
| 2869 |
|
| 2870 | for (i = 0; i < midi2->num_eps; i++) {
|
| 2871 | ep = &midi2->midi2_eps[i];
|
| 2872 | for (blk = 0; blk < ep->num_blks; blk++) {
|
| 2873 | bp = &ep->blks[blk];
|
| 2874 | midi2->string_defs[gtb_to_str_id(bp->gtb_id)].s =
|
| 2875 | ump_fb_name(&bp->info);
|
| 2876 |
|
| 2877 | fill_midi1_cable_mapping(midi2, ep, blk);
|
| 2878 | }
|
| 2879 | }
|
| 2880 |
|
| 2881 | if (!midi2->num_midi1_in && !midi2->num_midi1_out) {
|
| 2882 | pr_err("f_midi2: MIDI1 definition is missing\n");
|
| 2883 | do_f_midi2_free(midi2, opts);
|
| 2884 | return ERR_PTR(-EINVAL);
|
| 2885 | }
|
| 2886 |
|
| 2887 | return &midi2->func;
|
| 2888 | }
|
| 2889 |
|
| 2890 | DECLARE_USB_FUNCTION_INIT(midi2, f_midi2_alloc_inst, f_midi2_alloc);
|
| 2891 |
|
| 2892 | MODULE_DESCRIPTION("USB MIDI 2.0 class function driver");
|
| 2893 | MODULE_LICENSE("GPL");
|