1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Universal MIDI Packet (UMP) support 4 */ 5 6 #include <linux/list.h> 7 #include <linux/slab.h> 8 #include <linux/module.h> 9 #include <linux/export.h> 10 #include <linux/mm.h> 11 #include <linux/srcu.h> 12 #include <sound/core.h> 13 #include <sound/rawmidi.h> 14 #include <sound/ump.h> 15 #include <sound/ump_convert.h> 16 17 #define ump_err(ump, fmt, args...) dev_err((ump)->core.dev, fmt, ##args) 18 #define ump_warn(ump, fmt, args...) dev_warn((ump)->core.dev, fmt, ##args) 19 #define ump_info(ump, fmt, args...) dev_info((ump)->core.dev, fmt, ##args) 20 #define ump_dbg(ump, fmt, args...) dev_dbg((ump)->core.dev, fmt, ##args) 21 22 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 23 DEFINE_STATIC_SRCU(ump_seq_srcu); 24 25 static const struct snd_seq_ump_ops *seq_ops_load(struct snd_ump_endpoint *ump) 26 { 27 /* Pair with publication after seq_client has been initialized. */ 28 return smp_load_acquire(&ump->seq_ops); 29 } 30 31 /** 32 * snd_ump_detach_seq - stop callbacks to a sequencer binding 33 * @ump: UMP endpoint 34 * 35 * Withdraw the binding and wait for all callbacks already in progress. 36 * The caller can then drain binding work and free the sequencer client. 37 * Must not be called from one of the binding callbacks. 38 */ 39 void snd_ump_detach_seq(struct snd_ump_endpoint *ump) 40 { 41 WRITE_ONCE(ump->seq_ops, NULL); 42 synchronize_srcu(&ump_seq_srcu); 43 } 44 EXPORT_SYMBOL_GPL(snd_ump_detach_seq); 45 #endif 46 47 static int snd_ump_dev_register(struct snd_rawmidi *rmidi); 48 static int snd_ump_dev_unregister(struct snd_rawmidi *rmidi); 49 static long snd_ump_ioctl(struct snd_rawmidi *rmidi, unsigned int cmd, 50 void __user *argp); 51 static void snd_ump_proc_read(struct snd_info_entry *entry, 52 struct snd_info_buffer *buffer); 53 static int snd_ump_rawmidi_open(struct snd_rawmidi_substream *substream); 54 static int snd_ump_rawmidi_close(struct snd_rawmidi_substream *substream); 55 static void snd_ump_rawmidi_trigger(struct snd_rawmidi_substream *substream, 56 int up); 57 static void snd_ump_rawmidi_drain(struct snd_rawmidi_substream *substream); 58 59 static void ump_handle_stream_msg(struct snd_ump_endpoint *ump, 60 const u32 *buf, int size); 61 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI) 62 static int process_legacy_output(struct snd_ump_endpoint *ump, 63 u32 *buffer, int count); 64 static void process_legacy_input(struct snd_ump_endpoint *ump, const u32 *src, 65 int words); 66 static void ump_legacy_set_rawmidi_name(struct snd_ump_endpoint *ump); 67 static void update_legacy_names(struct snd_ump_endpoint *ump); 68 #else 69 static inline int process_legacy_output(struct snd_ump_endpoint *ump, 70 u32 *buffer, int count) 71 { 72 return 0; 73 } 74 static inline void process_legacy_input(struct snd_ump_endpoint *ump, 75 const u32 *src, int words) 76 { 77 } 78 static inline void ump_legacy_set_rawmidi_name(struct snd_ump_endpoint *ump) 79 { 80 } 81 static inline void update_legacy_names(struct snd_ump_endpoint *ump) 82 { 83 } 84 #endif 85 86 /* copy a string safely with stripping non-printable letters */ 87 static void safe_copy_string(void *dst, size_t max_dst_size, 88 const void *src, size_t max_src_size) 89 { 90 const unsigned char *s = src; 91 unsigned char *d = dst; 92 93 if (!max_dst_size--) 94 return; 95 for (s = src; max_dst_size && max_src_size-- && *s; s++) { 96 if (!isascii(*s) || !isprint(*s)) 97 continue; 98 *d++ = *s; 99 max_dst_size--; 100 } 101 *d = 0; 102 } 103 104 /* append a string safely with stripping non-printable letters */ 105 static void safe_append_string(void *dst, size_t max_dst_size, 106 const void *src, size_t max_src_size) 107 { 108 unsigned char *d = dst; 109 size_t len = strlen(d); 110 111 safe_copy_string(d + len, max_dst_size - len, src, max_src_size); 112 } 113 114 static const struct snd_rawmidi_global_ops snd_ump_rawmidi_ops = { 115 .dev_register = snd_ump_dev_register, 116 .dev_unregister = snd_ump_dev_unregister, 117 .ioctl = snd_ump_ioctl, 118 .proc_read = snd_ump_proc_read, 119 }; 120 121 static const struct snd_rawmidi_ops snd_ump_rawmidi_input_ops = { 122 .open = snd_ump_rawmidi_open, 123 .close = snd_ump_rawmidi_close, 124 .trigger = snd_ump_rawmidi_trigger, 125 }; 126 127 static const struct snd_rawmidi_ops snd_ump_rawmidi_output_ops = { 128 .open = snd_ump_rawmidi_open, 129 .close = snd_ump_rawmidi_close, 130 .trigger = snd_ump_rawmidi_trigger, 131 .drain = snd_ump_rawmidi_drain, 132 }; 133 134 static void snd_ump_endpoint_free(struct snd_rawmidi *rmidi) 135 { 136 struct snd_ump_endpoint *ump = rawmidi_to_ump(rmidi); 137 struct snd_ump_block *fb; 138 139 while (!list_empty(&ump->block_list)) { 140 fb = list_first_entry(&ump->block_list, struct snd_ump_block, 141 list); 142 list_del(&fb->list); 143 if (fb->private_free) 144 fb->private_free(fb); 145 kfree(fb); 146 } 147 148 if (ump->private_free) 149 ump->private_free(ump); 150 151 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI) 152 kfree(ump->out_cvts); 153 #endif 154 } 155 156 /** 157 * snd_ump_endpoint_new - create a UMP Endpoint object 158 * @card: the card instance 159 * @id: the id string for rawmidi 160 * @device: the device index for rawmidi 161 * @output: 1 for enabling output 162 * @input: 1 for enabling input 163 * @ump_ret: the pointer to store the new UMP instance 164 * 165 * Creates a new UMP Endpoint object. A UMP Endpoint is tied with one rawmidi 166 * instance with one input and/or one output rawmidi stream (either uni- 167 * or bi-directional). A UMP Endpoint may contain one or multiple UMP Blocks 168 * that consist of one or multiple UMP Groups. 169 * 170 * Use snd_rawmidi_set_ops() to set the operators to the new instance. 171 * Unlike snd_rawmidi_new(), this function sets up the info_flags by itself 172 * depending on the given @output and @input. 173 * 174 * The device has SNDRV_RAWMIDI_INFO_UMP flag set and a different device 175 * file ("umpCxDx") than a standard MIDI 1.x device ("midiCxDx") is 176 * created. 177 * 178 * Return: Zero if successful, or a negative error code on failure. 179 */ 180 int snd_ump_endpoint_new(struct snd_card *card, char *id, int device, 181 int output, int input, 182 struct snd_ump_endpoint **ump_ret) 183 { 184 unsigned int info_flags = SNDRV_RAWMIDI_INFO_UMP; 185 struct snd_ump_endpoint *ump; 186 int err; 187 188 if (input) 189 info_flags |= SNDRV_RAWMIDI_INFO_INPUT; 190 if (output) 191 info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT; 192 if (input && output) 193 info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX; 194 195 ump = kzalloc_obj(*ump); 196 if (!ump) 197 return -ENOMEM; 198 INIT_LIST_HEAD(&ump->block_list); 199 mutex_init(&ump->open_mutex); 200 init_waitqueue_head(&ump->stream_wait); 201 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI) 202 spin_lock_init(&ump->legacy_locks[0]); 203 spin_lock_init(&ump->legacy_locks[1]); 204 #endif 205 err = snd_rawmidi_init(&ump->core, card, id, device, 206 output, input, info_flags); 207 if (err < 0) { 208 snd_rawmidi_free(&ump->core); 209 return err; 210 } 211 212 ump->info.card = card->number; 213 ump->info.device = device; 214 215 ump->core.private_free = snd_ump_endpoint_free; 216 ump->core.ops = &snd_ump_rawmidi_ops; 217 if (input) 218 snd_rawmidi_set_ops(&ump->core, SNDRV_RAWMIDI_STREAM_INPUT, 219 &snd_ump_rawmidi_input_ops); 220 if (output) 221 snd_rawmidi_set_ops(&ump->core, SNDRV_RAWMIDI_STREAM_OUTPUT, 222 &snd_ump_rawmidi_output_ops); 223 224 ump_dbg(ump, "Created a UMP EP #%d (%s)\n", device, id); 225 *ump_ret = ump; 226 return 0; 227 } 228 EXPORT_SYMBOL_GPL(snd_ump_endpoint_new); 229 230 /* 231 * Device register / unregister hooks; 232 * do nothing, placeholders for avoiding the default rawmidi handling 233 */ 234 235 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 236 static void snd_ump_dev_seq_free(struct snd_seq_device *device) 237 { 238 struct snd_ump_endpoint *ump = device->private_data; 239 240 ump->seq_dev = NULL; 241 } 242 #endif 243 244 static int snd_ump_dev_register(struct snd_rawmidi *rmidi) 245 { 246 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 247 struct snd_ump_endpoint *ump = rawmidi_to_ump(rmidi); 248 int err; 249 250 err = snd_seq_device_new(ump->core.card, ump->core.device, 251 SNDRV_SEQ_DEV_ID_UMP, 0, &ump->seq_dev); 252 if (err < 0) 253 return err; 254 ump->seq_dev->private_data = ump; 255 ump->seq_dev->private_free = snd_ump_dev_seq_free; 256 snd_device_register(ump->core.card, ump->seq_dev); 257 #endif 258 return 0; 259 } 260 261 static int snd_ump_dev_unregister(struct snd_rawmidi *rmidi) 262 { 263 return 0; 264 } 265 266 static struct snd_ump_block * 267 snd_ump_get_block(struct snd_ump_endpoint *ump, unsigned char id) 268 { 269 struct snd_ump_block *fb; 270 271 list_for_each_entry(fb, &ump->block_list, list) { 272 if (fb->info.block_id == id) 273 return fb; 274 } 275 return NULL; 276 } 277 278 /* 279 * rawmidi ops for UMP endpoint 280 */ 281 static int snd_ump_rawmidi_open(struct snd_rawmidi_substream *substream) 282 { 283 struct snd_ump_endpoint *ump = rawmidi_to_ump(substream->rmidi); 284 int dir = substream->stream; 285 int err; 286 287 if (ump->substreams[dir]) 288 return -EBUSY; 289 err = ump->ops->open(ump, dir); 290 if (err < 0) 291 return err; 292 ump->substreams[dir] = substream; 293 return 0; 294 } 295 296 static int snd_ump_rawmidi_close(struct snd_rawmidi_substream *substream) 297 { 298 struct snd_ump_endpoint *ump = rawmidi_to_ump(substream->rmidi); 299 int dir = substream->stream; 300 301 ump->substreams[dir] = NULL; 302 ump->ops->close(ump, dir); 303 return 0; 304 } 305 306 static void snd_ump_rawmidi_trigger(struct snd_rawmidi_substream *substream, 307 int up) 308 { 309 struct snd_ump_endpoint *ump = rawmidi_to_ump(substream->rmidi); 310 int dir = substream->stream; 311 312 ump->ops->trigger(ump, dir, up); 313 } 314 315 static void snd_ump_rawmidi_drain(struct snd_rawmidi_substream *substream) 316 { 317 struct snd_ump_endpoint *ump = rawmidi_to_ump(substream->rmidi); 318 319 if (ump->ops->drain) 320 ump->ops->drain(ump, SNDRV_RAWMIDI_STREAM_OUTPUT); 321 } 322 323 /* number of 32bit words per message type */ 324 static unsigned char ump_packet_words[0x10] = { 325 1, 1, 1, 2, 2, 4, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4 326 }; 327 328 /** 329 * snd_ump_receive_ump_val - parse the UMP packet data 330 * @ump: UMP endpoint 331 * @val: UMP packet data 332 * 333 * The data is copied onto ump->input_buf[]. 334 * When a full packet is completed, returns the number of words (from 1 to 4). 335 * OTOH, if the packet is incomplete, returns 0. 336 */ 337 int snd_ump_receive_ump_val(struct snd_ump_endpoint *ump, u32 val) 338 { 339 int words; 340 341 if (!ump->input_pending) 342 ump->input_pending = ump_packet_words[ump_message_type(val)]; 343 344 ump->input_buf[ump->input_buf_head++] = val; 345 ump->input_pending--; 346 if (!ump->input_pending) { 347 words = ump->input_buf_head; 348 ump->input_buf_head = 0; 349 return words; 350 } 351 return 0; 352 } 353 EXPORT_SYMBOL_GPL(snd_ump_receive_ump_val); 354 355 /** 356 * snd_ump_receive - transfer UMP packets from the device 357 * @ump: the UMP endpoint 358 * @buffer: the buffer pointer to transfer 359 * @count: byte size to transfer 360 * 361 * Called from the driver to submit the received UMP packets from the device 362 * to user-space. It's essentially a wrapper of rawmidi_receive(). 363 * The data to receive is in CPU-native endianness. 364 */ 365 int snd_ump_receive(struct snd_ump_endpoint *ump, const u32 *buffer, int count) 366 { 367 struct snd_rawmidi_substream *substream; 368 const u32 *p = buffer; 369 int n, words = count >> 2; 370 371 while (words--) { 372 n = snd_ump_receive_ump_val(ump, *p++); 373 if (!n) 374 continue; 375 ump_handle_stream_msg(ump, ump->input_buf, n); 376 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 377 scoped_guard(srcu, &ump_seq_srcu) { 378 const struct snd_seq_ump_ops *ops; 379 380 ops = seq_ops_load(ump); 381 if (ops) 382 ops->input_receive(ump, ump->input_buf, n); 383 } 384 #endif 385 process_legacy_input(ump, ump->input_buf, n); 386 } 387 388 substream = ump->substreams[SNDRV_RAWMIDI_STREAM_INPUT]; 389 if (!substream) 390 return 0; 391 return snd_rawmidi_receive(substream, (const char *)buffer, count); 392 } 393 EXPORT_SYMBOL_GPL(snd_ump_receive); 394 395 /** 396 * snd_ump_transmit - transmit UMP packets 397 * @ump: the UMP endpoint 398 * @buffer: the buffer pointer to transfer 399 * @count: byte size to transfer 400 * 401 * Called from the driver to obtain the UMP packets from user-space to the 402 * device. It's essentially a wrapper of rawmidi_transmit(). 403 * The data to transmit is in CPU-native endianness. 404 */ 405 int snd_ump_transmit(struct snd_ump_endpoint *ump, u32 *buffer, int count) 406 { 407 struct snd_rawmidi_substream *substream = 408 ump->substreams[SNDRV_RAWMIDI_STREAM_OUTPUT]; 409 int err; 410 411 if (!substream) 412 return -ENODEV; 413 err = snd_rawmidi_transmit(substream, (char *)buffer, count); 414 /* received either data or an error? */ 415 if (err) 416 return err; 417 return process_legacy_output(ump, buffer, count); 418 } 419 EXPORT_SYMBOL_GPL(snd_ump_transmit); 420 421 /** 422 * snd_ump_block_new - Create a UMP block 423 * @ump: UMP object 424 * @blk: block ID number to create 425 * @direction: direction (in/out/bidirection) 426 * @first_group: the first group ID (0-based) 427 * @num_groups: the number of groups in this block 428 * @blk_ret: the pointer to store the resultant block object 429 */ 430 int snd_ump_block_new(struct snd_ump_endpoint *ump, unsigned int blk, 431 unsigned int direction, unsigned int first_group, 432 unsigned int num_groups, struct snd_ump_block **blk_ret) 433 { 434 struct snd_ump_block *fb, *p; 435 436 if (blk >= SNDRV_UMP_MAX_BLOCKS) 437 return -EINVAL; 438 439 if (snd_ump_get_block(ump, blk)) 440 return -EBUSY; 441 442 fb = kzalloc_obj(*fb); 443 if (!fb) 444 return -ENOMEM; 445 446 fb->ump = ump; 447 fb->info.card = ump->info.card; 448 fb->info.device = ump->info.device; 449 fb->info.block_id = blk; 450 if (blk >= ump->info.num_blocks) 451 ump->info.num_blocks = blk + 1; 452 fb->info.direction = direction; 453 fb->info.active = 1; 454 fb->info.first_group = first_group; 455 fb->info.num_groups = num_groups; 456 /* fill the default name, may be overwritten to a better name */ 457 snprintf(fb->info.name, sizeof(fb->info.name), "Group %u-%u", 458 first_group + 1, first_group + num_groups); 459 460 /* put the entry in the ordered list */ 461 list_for_each_entry(p, &ump->block_list, list) { 462 if (p->info.block_id > blk) { 463 list_add_tail(&fb->list, &p->list); 464 goto added; 465 } 466 } 467 list_add_tail(&fb->list, &ump->block_list); 468 469 added: 470 ump_dbg(ump, "Created a UMP Block #%d (%s)\n", blk, fb->info.name); 471 *blk_ret = fb; 472 return 0; 473 } 474 EXPORT_SYMBOL_GPL(snd_ump_block_new); 475 476 static int snd_ump_ioctl_block(struct snd_ump_endpoint *ump, 477 struct snd_ump_block_info __user *argp) 478 { 479 struct snd_ump_block *fb; 480 unsigned char id; 481 482 if (get_user(id, &argp->block_id)) 483 return -EFAULT; 484 fb = snd_ump_get_block(ump, id); 485 if (!fb) 486 return -ENOENT; 487 if (copy_to_user(argp, &fb->info, sizeof(fb->info))) 488 return -EFAULT; 489 return 0; 490 } 491 492 /* 493 * Handle UMP-specific ioctls; called from snd_rawmidi_ioctl() 494 */ 495 static long snd_ump_ioctl(struct snd_rawmidi *rmidi, unsigned int cmd, 496 void __user *argp) 497 { 498 struct snd_ump_endpoint *ump = rawmidi_to_ump(rmidi); 499 500 switch (cmd) { 501 case SNDRV_UMP_IOCTL_ENDPOINT_INFO: 502 if (copy_to_user(argp, &ump->info, sizeof(ump->info))) 503 return -EFAULT; 504 return 0; 505 case SNDRV_UMP_IOCTL_BLOCK_INFO: 506 return snd_ump_ioctl_block(ump, argp); 507 default: 508 ump_dbg(ump, "rawmidi: unknown command = 0x%x\n", cmd); 509 return -ENOTTY; 510 } 511 } 512 513 static const char *ump_direction_string(int dir) 514 { 515 switch (dir) { 516 case SNDRV_UMP_DIR_INPUT: 517 return "input"; 518 case SNDRV_UMP_DIR_OUTPUT: 519 return "output"; 520 case SNDRV_UMP_DIR_BIDIRECTION: 521 return "bidirection"; 522 default: 523 return "unknown"; 524 } 525 } 526 527 static const char *ump_ui_hint_string(int dir) 528 { 529 switch (dir) { 530 case SNDRV_UMP_BLOCK_UI_HINT_RECEIVER: 531 return "receiver"; 532 case SNDRV_UMP_BLOCK_UI_HINT_SENDER: 533 return "sender"; 534 case SNDRV_UMP_BLOCK_UI_HINT_BOTH: 535 return "both"; 536 default: 537 return "unknown"; 538 } 539 } 540 541 /* Additional proc file output */ 542 static void snd_ump_proc_read(struct snd_info_entry *entry, 543 struct snd_info_buffer *buffer) 544 { 545 struct snd_rawmidi *rmidi = entry->private_data; 546 struct snd_ump_endpoint *ump = rawmidi_to_ump(rmidi); 547 struct snd_ump_block *fb; 548 549 snd_iprintf(buffer, "EP Name: %s\n", ump->info.name); 550 snd_iprintf(buffer, "EP Product ID: %s\n", ump->info.product_id); 551 snd_iprintf(buffer, "UMP Version: 0x%04x\n", ump->info.version); 552 snd_iprintf(buffer, "Protocol Caps: 0x%08x\n", ump->info.protocol_caps); 553 snd_iprintf(buffer, "Protocol: 0x%08x\n", ump->info.protocol); 554 if (ump->info.version) { 555 snd_iprintf(buffer, "Manufacturer ID: 0x%08x\n", 556 ump->info.manufacturer_id); 557 snd_iprintf(buffer, "Family ID: 0x%04x\n", ump->info.family_id); 558 snd_iprintf(buffer, "Model ID: 0x%04x\n", ump->info.model_id); 559 snd_iprintf(buffer, "SW Revision: 0x%4phN\n", ump->info.sw_revision); 560 } 561 snd_iprintf(buffer, "Static Blocks: %s\n", 562 (ump->info.flags & SNDRV_UMP_EP_INFO_STATIC_BLOCKS) ? "Yes" : "No"); 563 snd_iprintf(buffer, "Num Blocks: %d\n\n", ump->info.num_blocks); 564 565 list_for_each_entry(fb, &ump->block_list, list) { 566 snd_iprintf(buffer, "Block %d (%s)\n", fb->info.block_id, 567 fb->info.name); 568 snd_iprintf(buffer, " Direction: %s\n", 569 ump_direction_string(fb->info.direction)); 570 snd_iprintf(buffer, " Active: %s\n", 571 fb->info.active ? "Yes" : "No"); 572 snd_iprintf(buffer, " Groups: %d-%d\n", 573 fb->info.first_group + 1, 574 fb->info.first_group + fb->info.num_groups); 575 snd_iprintf(buffer, " Is MIDI1: %s%s\n", 576 (fb->info.flags & SNDRV_UMP_BLOCK_IS_MIDI1) ? "Yes" : "No", 577 (fb->info.flags & SNDRV_UMP_BLOCK_IS_LOWSPEED) ? " (Low Speed)" : ""); 578 if (ump->info.version) { 579 snd_iprintf(buffer, " MIDI-CI Version: %d\n", 580 fb->info.midi_ci_version); 581 snd_iprintf(buffer, " Sysex8 Streams: %d\n", 582 fb->info.sysex8_streams); 583 snd_iprintf(buffer, " UI Hint: %s\n", 584 ump_ui_hint_string(fb->info.ui_hint)); 585 } 586 snd_iprintf(buffer, "\n"); 587 } 588 } 589 590 /* update dir_bits and active flag for all groups in the client */ 591 void snd_ump_update_group_attrs(struct snd_ump_endpoint *ump) 592 { 593 static const char separator[] = ", "; 594 struct snd_ump_block *fb; 595 struct snd_ump_group *group; 596 int i; 597 598 for (i = 0; i < SNDRV_UMP_MAX_GROUPS; i++) { 599 group = &ump->groups[i]; 600 *group->name = 0; 601 group->dir_bits = 0; 602 group->active = 0; 603 group->group = i; 604 group->valid = false; 605 group->is_midi1 = false; 606 } 607 608 list_for_each_entry(fb, &ump->block_list, list) { 609 if (fb->info.first_group + fb->info.num_groups > SNDRV_UMP_MAX_GROUPS) 610 break; 611 group = &ump->groups[fb->info.first_group]; 612 for (i = 0; i < fb->info.num_groups; i++, group++) { 613 group->valid = true; 614 if (fb->info.active) 615 group->active = 1; 616 if (fb->info.flags & SNDRV_UMP_BLOCK_IS_MIDI1) 617 group->is_midi1 = true; 618 switch (fb->info.direction) { 619 case SNDRV_UMP_DIR_INPUT: 620 group->dir_bits |= (1 << SNDRV_RAWMIDI_STREAM_INPUT); 621 break; 622 case SNDRV_UMP_DIR_OUTPUT: 623 group->dir_bits |= (1 << SNDRV_RAWMIDI_STREAM_OUTPUT); 624 break; 625 case SNDRV_UMP_DIR_BIDIRECTION: 626 group->dir_bits |= (1 << SNDRV_RAWMIDI_STREAM_INPUT) | 627 (1 << SNDRV_RAWMIDI_STREAM_OUTPUT); 628 break; 629 } 630 if (!*fb->info.name) 631 continue; 632 if (*group->name) 633 safe_append_string(group->name, sizeof(group->name), 634 separator, sizeof(separator)); 635 safe_append_string(group->name, sizeof(group->name), 636 fb->info.name, sizeof(fb->info.name)); 637 } 638 } 639 } 640 EXPORT_SYMBOL_GPL(snd_ump_update_group_attrs); 641 642 /* 643 * UMP endpoint and function block handling 644 */ 645 646 /* open / close UMP streams for the internal stream msg communication */ 647 static int ump_request_open(struct snd_ump_endpoint *ump) 648 { 649 return snd_rawmidi_kernel_open(&ump->core, 0, 650 SNDRV_RAWMIDI_LFLG_OUTPUT, 651 &ump->stream_rfile); 652 } 653 654 static void ump_request_close(struct snd_ump_endpoint *ump) 655 { 656 snd_rawmidi_kernel_release(&ump->stream_rfile); 657 } 658 659 /* request a command and wait for the given response; 660 * @req1 and @req2 are u32 commands 661 * @reply is the expected UMP stream status 662 */ 663 static int ump_req_msg(struct snd_ump_endpoint *ump, u32 req1, u32 req2, 664 u32 reply) 665 { 666 u32 buf[4]; 667 668 ump_dbg(ump, "%s: request %08x %08x, wait-for %08x\n", 669 __func__, req1, req2, reply); 670 memset(buf, 0, sizeof(buf)); 671 buf[0] = req1; 672 buf[1] = req2; 673 ump->stream_finished = 0; 674 ump->stream_wait_for = reply; 675 snd_rawmidi_kernel_write(ump->stream_rfile.output, 676 (unsigned char *)&buf, 16); 677 wait_event_timeout(ump->stream_wait, ump->stream_finished, 678 msecs_to_jiffies(500)); 679 if (!READ_ONCE(ump->stream_finished)) { 680 ump_dbg(ump, "%s: request timed out\n", __func__); 681 return -ETIMEDOUT; 682 } 683 ump->stream_finished = 0; 684 ump_dbg(ump, "%s: reply: %08x %08x %08x %08x\n", 685 __func__, buf[0], buf[1], buf[2], buf[3]); 686 return 0; 687 } 688 689 /* append the received letters via UMP packet to the given string buffer; 690 * return 1 if the full string is received or 0 to continue 691 */ 692 static int ump_append_string(struct snd_ump_endpoint *ump, char *dest, 693 int maxsize, const u32 *buf, int offset) 694 { 695 unsigned char format; 696 int c; 697 698 format = ump_stream_message_format(buf[0]); 699 if (format == UMP_STREAM_MSG_FORMAT_SINGLE || 700 format == UMP_STREAM_MSG_FORMAT_START) { 701 c = 0; 702 } else { 703 c = strlen(dest); 704 if (c >= maxsize - 1) 705 return 1; 706 } 707 708 for (; offset < 16; offset++) { 709 dest[c] = buf[offset / 4] >> (3 - (offset % 4)) * 8; 710 if (!dest[c]) 711 break; 712 if (++c >= maxsize - 1) 713 break; 714 } 715 dest[c] = 0; 716 return (format == UMP_STREAM_MSG_FORMAT_SINGLE || 717 format == UMP_STREAM_MSG_FORMAT_END); 718 } 719 720 /* Choose the default protocol */ 721 static void choose_default_protocol(struct snd_ump_endpoint *ump) 722 { 723 if (ump->info.protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK) 724 return; 725 if (ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI2) 726 ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI2; 727 else 728 ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI1; 729 } 730 731 /* notify the EP info/name change to sequencer */ 732 static void seq_notify_ep_change(struct snd_ump_endpoint *ump) 733 { 734 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 735 const struct snd_seq_ump_ops *ops; 736 737 guard(srcu)(&ump_seq_srcu); 738 ops = seq_ops_load(ump); 739 if (ump->parsed && ops && ops->notify_ep_change) 740 ops->notify_ep_change(ump); 741 #endif 742 } 743 744 /* handle EP info stream message; update the UMP attributes */ 745 static int ump_handle_ep_info_msg(struct snd_ump_endpoint *ump, 746 const union snd_ump_stream_msg *buf) 747 { 748 ump->info.version = (buf->ep_info.ump_version_major << 8) | 749 buf->ep_info.ump_version_minor; 750 ump->info.num_blocks = buf->ep_info.num_function_blocks; 751 if (ump->info.num_blocks > SNDRV_UMP_MAX_BLOCKS) { 752 ump_info(ump, "Invalid function blocks %d, fallback to 1\n", 753 ump->info.num_blocks); 754 ump->info.num_blocks = 1; 755 } 756 757 if (buf->ep_info.static_function_block) 758 ump->info.flags |= SNDRV_UMP_EP_INFO_STATIC_BLOCKS; 759 760 ump->info.protocol_caps = (buf->ep_info.protocol << 8) | 761 buf->ep_info.jrts; 762 763 ump_dbg(ump, "EP info: version=%x, num_blocks=%x, proto_caps=%x\n", 764 ump->info.version, ump->info.num_blocks, ump->info.protocol_caps); 765 766 ump->info.protocol &= ump->info.protocol_caps; 767 choose_default_protocol(ump); 768 seq_notify_ep_change(ump); 769 770 return 1; /* finished */ 771 } 772 773 /* handle EP device info stream message; update the UMP attributes */ 774 static int ump_handle_device_info_msg(struct snd_ump_endpoint *ump, 775 const union snd_ump_stream_msg *buf) 776 { 777 ump->info.manufacturer_id = buf->device_info.manufacture_id & 0x7f7f7f; 778 ump->info.family_id = (buf->device_info.family_msb << 8) | 779 buf->device_info.family_lsb; 780 ump->info.model_id = (buf->device_info.model_msb << 8) | 781 buf->device_info.model_lsb; 782 ump->info.sw_revision[0] = (buf->device_info.sw_revision >> 24) & 0x7f; 783 ump->info.sw_revision[1] = (buf->device_info.sw_revision >> 16) & 0x7f; 784 ump->info.sw_revision[2] = (buf->device_info.sw_revision >> 8) & 0x7f; 785 ump->info.sw_revision[3] = buf->device_info.sw_revision & 0x7f; 786 ump_dbg(ump, "EP devinfo: manid=%08x, family=%04x, model=%04x, sw=%4phN\n", 787 ump->info.manufacturer_id, 788 ump->info.family_id, 789 ump->info.model_id, 790 ump->info.sw_revision); 791 seq_notify_ep_change(ump); 792 return 1; /* finished */ 793 } 794 795 /* set up the core rawmidi name from UMP EP name string */ 796 static void ump_set_rawmidi_name(struct snd_ump_endpoint *ump) 797 { 798 safe_copy_string(ump->core.name, sizeof(ump->core.name), 799 ump->info.name, sizeof(ump->info.name)); 800 } 801 802 /* handle EP name stream message; update the UMP name string */ 803 static int ump_handle_ep_name_msg(struct snd_ump_endpoint *ump, 804 const union snd_ump_stream_msg *buf) 805 { 806 int ret; 807 808 ret = ump_append_string(ump, ump->info.name, sizeof(ump->info.name), 809 buf->raw, 2); 810 if (ret && ump->parsed) { 811 ump_set_rawmidi_name(ump); 812 ump_legacy_set_rawmidi_name(ump); 813 seq_notify_ep_change(ump); 814 } 815 816 return ret; 817 } 818 819 /* handle EP product id stream message; update the UMP product_id string */ 820 static int ump_handle_product_id_msg(struct snd_ump_endpoint *ump, 821 const union snd_ump_stream_msg *buf) 822 { 823 int ret; 824 825 ret = ump_append_string(ump, ump->info.product_id, 826 sizeof(ump->info.product_id), 827 buf->raw, 2); 828 if (ret) 829 seq_notify_ep_change(ump); 830 return ret; 831 } 832 833 /* notify the protocol change to sequencer */ 834 static void seq_notify_protocol(struct snd_ump_endpoint *ump) 835 { 836 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 837 const struct snd_seq_ump_ops *ops; 838 839 guard(srcu)(&ump_seq_srcu); 840 ops = seq_ops_load(ump); 841 if (ops && ops->switch_protocol) 842 ops->switch_protocol(ump); 843 #endif /* CONFIG_SND_SEQUENCER */ 844 } 845 846 /** 847 * snd_ump_switch_protocol - switch MIDI protocol 848 * @ump: UMP endpoint 849 * @protocol: protocol to switch to 850 * 851 * Returns 1 if the protocol is actually switched, 0 if unchanged 852 */ 853 int snd_ump_switch_protocol(struct snd_ump_endpoint *ump, unsigned int protocol) 854 { 855 unsigned int type; 856 857 protocol &= ump->info.protocol_caps; 858 if (protocol == ump->info.protocol) 859 return 0; 860 861 type = protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK; 862 if (type != SNDRV_UMP_EP_INFO_PROTO_MIDI1 && 863 type != SNDRV_UMP_EP_INFO_PROTO_MIDI2) 864 return 0; 865 866 ump->info.protocol = protocol; 867 ump_dbg(ump, "New protocol = %x (caps = %x)\n", 868 protocol, ump->info.protocol_caps); 869 seq_notify_protocol(ump); 870 return 1; 871 } 872 EXPORT_SYMBOL_GPL(snd_ump_switch_protocol); 873 874 /* handle EP stream config message; update the UMP protocol */ 875 static int ump_handle_stream_cfg_msg(struct snd_ump_endpoint *ump, 876 const union snd_ump_stream_msg *buf) 877 { 878 unsigned int protocol = 879 (buf->stream_cfg.protocol << 8) | buf->stream_cfg.jrts; 880 881 snd_ump_switch_protocol(ump, protocol); 882 return 1; /* finished */ 883 } 884 885 /* Extract Function Block info from UMP packet */ 886 static void fill_fb_info(struct snd_ump_endpoint *ump, 887 struct snd_ump_block_info *info, 888 const union snd_ump_stream_msg *buf) 889 { 890 info->direction = buf->fb_info.direction; 891 info->ui_hint = buf->fb_info.ui_hint; 892 info->first_group = buf->fb_info.first_group; 893 info->num_groups = buf->fb_info.num_groups; 894 if (buf->fb_info.midi_10 < 2) 895 info->flags = buf->fb_info.midi_10; 896 else 897 info->flags = SNDRV_UMP_BLOCK_IS_MIDI1 | SNDRV_UMP_BLOCK_IS_LOWSPEED; 898 info->active = buf->fb_info.active; 899 info->midi_ci_version = buf->fb_info.midi_ci_version; 900 info->sysex8_streams = buf->fb_info.sysex8_streams; 901 902 ump_dbg(ump, "FB %d: dir=%d, active=%d, first_gp=%d, num_gp=%d, midici=%d, sysex8=%d, flags=0x%x\n", 903 info->block_id, info->direction, info->active, 904 info->first_group, info->num_groups, info->midi_ci_version, 905 info->sysex8_streams, info->flags); 906 907 if ((info->flags & SNDRV_UMP_BLOCK_IS_MIDI1) && info->num_groups != 1) { 908 info->num_groups = 1; 909 ump_dbg(ump, "FB %d: corrected groups to 1 for MIDI1\n", 910 info->block_id); 911 } 912 } 913 914 /* check whether the FB info gets updated by the current message */ 915 static bool is_fb_info_updated(struct snd_ump_endpoint *ump, 916 struct snd_ump_block *fb, 917 const union snd_ump_stream_msg *buf) 918 { 919 char tmpbuf[offsetof(struct snd_ump_block_info, name)]; 920 921 if (ump->info.flags & SNDRV_UMP_EP_INFO_STATIC_BLOCKS) { 922 ump_info(ump, "Skipping static FB info update (blk#%d)\n", 923 fb->info.block_id); 924 return 0; 925 } 926 927 memcpy(tmpbuf, &fb->info, sizeof(tmpbuf)); 928 fill_fb_info(ump, (struct snd_ump_block_info *)tmpbuf, buf); 929 return memcmp(&fb->info, tmpbuf, sizeof(tmpbuf)) != 0; 930 } 931 932 /* notify the FB info/name change to sequencer */ 933 static void seq_notify_fb_change(struct snd_ump_endpoint *ump, 934 struct snd_ump_block *fb) 935 { 936 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 937 const struct snd_seq_ump_ops *ops; 938 939 guard(srcu)(&ump_seq_srcu); 940 ops = seq_ops_load(ump); 941 if (ops && ops->notify_fb_change) 942 ops->notify_fb_change(ump, fb); 943 #endif 944 } 945 946 /* handle FB info message; update FB info if the block is present */ 947 static int ump_handle_fb_info_msg(struct snd_ump_endpoint *ump, 948 const union snd_ump_stream_msg *buf) 949 { 950 unsigned char blk; 951 struct snd_ump_block *fb; 952 953 blk = buf->fb_info.function_block_id; 954 fb = snd_ump_get_block(ump, blk); 955 956 /* complain only if updated after parsing */ 957 if (!fb && ump->parsed) { 958 ump_info(ump, "Function Block Info Update for non-existing block %d\n", 959 blk); 960 return -ENODEV; 961 } 962 963 /* When updated after the initial parse, check the FB info update */ 964 if (ump->parsed && !is_fb_info_updated(ump, fb, buf)) 965 return 1; /* no content change */ 966 967 if (fb) { 968 fill_fb_info(ump, &fb->info, buf); 969 if (ump->parsed) { 970 snd_ump_update_group_attrs(ump); 971 update_legacy_names(ump); 972 seq_notify_fb_change(ump, fb); 973 } 974 } 975 976 return 1; /* finished */ 977 } 978 979 /* handle FB name message; update the FB name string */ 980 static int ump_handle_fb_name_msg(struct snd_ump_endpoint *ump, 981 const union snd_ump_stream_msg *buf) 982 { 983 unsigned char blk; 984 struct snd_ump_block *fb; 985 int ret; 986 987 blk = buf->fb_name.function_block_id; 988 fb = snd_ump_get_block(ump, blk); 989 if (!fb) 990 return -ENODEV; 991 992 if (ump->parsed && 993 (ump->info.flags & SNDRV_UMP_EP_INFO_STATIC_BLOCKS)) { 994 ump_dbg(ump, "Skipping static FB name update (blk#%d)\n", 995 fb->info.block_id); 996 return 0; 997 } 998 999 ret = ump_append_string(ump, fb->info.name, sizeof(fb->info.name), 1000 buf->raw, 3); 1001 /* notify the FB name update to sequencer, too */ 1002 if (ret > 0 && ump->parsed) { 1003 snd_ump_update_group_attrs(ump); 1004 update_legacy_names(ump); 1005 seq_notify_fb_change(ump, fb); 1006 } 1007 return ret; 1008 } 1009 1010 static int create_block_from_fb_info(struct snd_ump_endpoint *ump, int blk) 1011 { 1012 struct snd_ump_block *fb; 1013 unsigned char direction, first_group, num_groups; 1014 const union snd_ump_stream_msg *buf = 1015 (const union snd_ump_stream_msg *)ump->input_buf; 1016 u32 msg; 1017 int err; 1018 1019 /* query the FB info once */ 1020 msg = ump_stream_compose(UMP_STREAM_MSG_STATUS_FB_DISCOVERY, 0) | 1021 (blk << 8) | UMP_STREAM_MSG_REQUEST_FB_INFO; 1022 err = ump_req_msg(ump, msg, 0, UMP_STREAM_MSG_STATUS_FB_INFO); 1023 if (err < 0) { 1024 ump_dbg(ump, "Unable to get FB info for block %d\n", blk); 1025 return err; 1026 } 1027 1028 /* the last input must be the FB info */ 1029 if (buf->fb_info.status != UMP_STREAM_MSG_STATUS_FB_INFO) { 1030 ump_dbg(ump, "Inconsistent input: 0x%x\n", *buf->raw); 1031 return -EINVAL; 1032 } 1033 1034 direction = buf->fb_info.direction; 1035 first_group = buf->fb_info.first_group; 1036 num_groups = buf->fb_info.num_groups; 1037 1038 err = snd_ump_block_new(ump, blk, direction, first_group, num_groups, 1039 &fb); 1040 if (err < 0) 1041 return err; 1042 1043 fill_fb_info(ump, &fb->info, buf); 1044 1045 msg = ump_stream_compose(UMP_STREAM_MSG_STATUS_FB_DISCOVERY, 0) | 1046 (blk << 8) | UMP_STREAM_MSG_REQUEST_FB_NAME; 1047 err = ump_req_msg(ump, msg, 0, UMP_STREAM_MSG_STATUS_FB_NAME); 1048 if (err) 1049 ump_dbg(ump, "Unable to get UMP FB name string #%d\n", blk); 1050 1051 return 0; 1052 } 1053 1054 /* handle stream messages, called from snd_ump_receive() */ 1055 static void ump_handle_stream_msg(struct snd_ump_endpoint *ump, 1056 const u32 *buf, int size) 1057 { 1058 const union snd_ump_stream_msg *msg; 1059 unsigned int status; 1060 int ret; 1061 1062 /* UMP stream message suppressed (for gadget UMP)? */ 1063 if (ump->no_process_stream) 1064 return; 1065 1066 BUILD_BUG_ON(sizeof(*msg) != 16); 1067 ump_dbg(ump, "Stream msg: %08x %08x %08x %08x\n", 1068 buf[0], buf[1], buf[2], buf[3]); 1069 1070 if (size != 4 || ump_message_type(*buf) != UMP_MSG_TYPE_STREAM) 1071 return; 1072 1073 msg = (const union snd_ump_stream_msg *)buf; 1074 status = ump_stream_message_status(*buf); 1075 switch (status) { 1076 case UMP_STREAM_MSG_STATUS_EP_INFO: 1077 ret = ump_handle_ep_info_msg(ump, msg); 1078 break; 1079 case UMP_STREAM_MSG_STATUS_DEVICE_INFO: 1080 ret = ump_handle_device_info_msg(ump, msg); 1081 break; 1082 case UMP_STREAM_MSG_STATUS_EP_NAME: 1083 ret = ump_handle_ep_name_msg(ump, msg); 1084 break; 1085 case UMP_STREAM_MSG_STATUS_PRODUCT_ID: 1086 ret = ump_handle_product_id_msg(ump, msg); 1087 break; 1088 case UMP_STREAM_MSG_STATUS_STREAM_CFG: 1089 ret = ump_handle_stream_cfg_msg(ump, msg); 1090 break; 1091 case UMP_STREAM_MSG_STATUS_FB_INFO: 1092 ret = ump_handle_fb_info_msg(ump, msg); 1093 break; 1094 case UMP_STREAM_MSG_STATUS_FB_NAME: 1095 ret = ump_handle_fb_name_msg(ump, msg); 1096 break; 1097 default: 1098 return; 1099 } 1100 1101 /* when the message has been processed fully, wake up */ 1102 if (ret > 0 && ump->stream_wait_for == status) { 1103 WRITE_ONCE(ump->stream_finished, 1); 1104 wake_up(&ump->stream_wait); 1105 } 1106 } 1107 1108 /** 1109 * snd_ump_parse_endpoint - parse endpoint and create function blocks 1110 * @ump: UMP object 1111 * 1112 * Returns 0 for successful parse, -ENODEV if device doesn't respond 1113 * (or the query is unsupported), or other error code for serious errors. 1114 */ 1115 int snd_ump_parse_endpoint(struct snd_ump_endpoint *ump) 1116 { 1117 int blk, err; 1118 u32 msg; 1119 1120 if (!(ump->core.info_flags & SNDRV_RAWMIDI_INFO_DUPLEX)) 1121 return -ENODEV; 1122 1123 err = ump_request_open(ump); 1124 if (err < 0) { 1125 ump_dbg(ump, "Unable to open rawmidi device: %d\n", err); 1126 return err; 1127 } 1128 1129 /* Check Endpoint Information */ 1130 msg = ump_stream_compose(UMP_STREAM_MSG_STATUS_EP_DISCOVERY, 0) | 1131 0x0101; /* UMP version 1.1 */ 1132 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_EP_INFO, 1133 UMP_STREAM_MSG_STATUS_EP_INFO); 1134 if (err < 0) { 1135 ump_dbg(ump, "Unable to get UMP EP info\n"); 1136 goto error; 1137 } 1138 1139 /* Request Endpoint Device Info */ 1140 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_DEVICE_INFO, 1141 UMP_STREAM_MSG_STATUS_DEVICE_INFO); 1142 if (err < 0) 1143 ump_dbg(ump, "Unable to get UMP EP device info\n"); 1144 1145 /* Request Endpoint Name */ 1146 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_EP_NAME, 1147 UMP_STREAM_MSG_STATUS_EP_NAME); 1148 if (err < 0) 1149 ump_dbg(ump, "Unable to get UMP EP name string\n"); 1150 1151 ump_set_rawmidi_name(ump); 1152 1153 /* Request Endpoint Product ID */ 1154 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_PRODUCT_ID, 1155 UMP_STREAM_MSG_STATUS_PRODUCT_ID); 1156 if (err < 0) 1157 ump_dbg(ump, "Unable to get UMP EP product ID string\n"); 1158 1159 /* Get the current stream configuration */ 1160 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_STREAM_CFG, 1161 UMP_STREAM_MSG_STATUS_STREAM_CFG); 1162 if (err < 0) 1163 ump_dbg(ump, "Unable to get UMP EP stream config\n"); 1164 1165 /* If no protocol is set by some reason, assume the valid one */ 1166 choose_default_protocol(ump); 1167 1168 /* Query and create blocks from Function Blocks */ 1169 for (blk = 0; blk < ump->info.num_blocks; blk++) { 1170 err = create_block_from_fb_info(ump, blk); 1171 if (err < 0) 1172 continue; 1173 } 1174 1175 /* initialize group attributions */ 1176 snd_ump_update_group_attrs(ump); 1177 1178 error: 1179 ump->parsed = true; 1180 ump_request_close(ump); 1181 if (err == -ETIMEDOUT) 1182 err = -ENODEV; 1183 return err; 1184 } 1185 EXPORT_SYMBOL_GPL(snd_ump_parse_endpoint); 1186 1187 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI) 1188 /* 1189 * Legacy rawmidi support 1190 */ 1191 static int snd_ump_legacy_open(struct snd_rawmidi_substream *substream) 1192 { 1193 struct snd_ump_endpoint *ump = substream->rmidi->private_data; 1194 int dir = substream->stream; 1195 int group = ump->legacy_mapping[substream->number]; 1196 int err; 1197 1198 guard(mutex)(&ump->open_mutex); 1199 if (ump->legacy_substreams[dir][group]) 1200 return -EBUSY; 1201 if (!ump->groups[group].active) 1202 return -ENODEV; 1203 if (dir == SNDRV_RAWMIDI_STREAM_OUTPUT) { 1204 if (!ump->legacy_out_opens) { 1205 err = snd_rawmidi_kernel_open_nested(&ump->core, 0, 1206 SNDRV_RAWMIDI_LFLG_OUTPUT | 1207 SNDRV_RAWMIDI_LFLG_APPEND, 1208 &ump->legacy_out_rfile, 1209 SINGLE_DEPTH_NESTING); 1210 if (err < 0) 1211 return err; 1212 } 1213 ump->legacy_out_opens++; 1214 snd_ump_convert_reset(&ump->out_cvts[group]); 1215 } 1216 guard(spinlock_irq)(&ump->legacy_locks[dir]); 1217 ump->legacy_substreams[dir][group] = substream; 1218 return 0; 1219 } 1220 1221 static int snd_ump_legacy_close(struct snd_rawmidi_substream *substream) 1222 { 1223 struct snd_ump_endpoint *ump = substream->rmidi->private_data; 1224 int dir = substream->stream; 1225 int group = ump->legacy_mapping[substream->number]; 1226 1227 guard(mutex)(&ump->open_mutex); 1228 scoped_guard(spinlock_irq, &ump->legacy_locks[dir]) 1229 ump->legacy_substreams[dir][group] = NULL; 1230 if (dir == SNDRV_RAWMIDI_STREAM_OUTPUT) { 1231 if (!--ump->legacy_out_opens) 1232 snd_rawmidi_kernel_release_nested(&ump->legacy_out_rfile, 1233 SINGLE_DEPTH_NESTING); 1234 } 1235 return 0; 1236 } 1237 1238 static void snd_ump_legacy_trigger(struct snd_rawmidi_substream *substream, 1239 int up) 1240 { 1241 struct snd_ump_endpoint *ump = substream->rmidi->private_data; 1242 int dir = substream->stream; 1243 1244 ump->ops->trigger(ump, dir, up); 1245 } 1246 1247 static void snd_ump_legacy_drain(struct snd_rawmidi_substream *substream) 1248 { 1249 struct snd_ump_endpoint *ump = substream->rmidi->private_data; 1250 1251 if (ump->ops->drain) 1252 ump->ops->drain(ump, SNDRV_RAWMIDI_STREAM_OUTPUT); 1253 } 1254 1255 static int snd_ump_legacy_dev_register(struct snd_rawmidi *rmidi) 1256 { 1257 /* dummy, just for avoiding create superfluous seq clients */ 1258 return 0; 1259 } 1260 1261 static const struct snd_rawmidi_ops snd_ump_legacy_input_ops = { 1262 .open = snd_ump_legacy_open, 1263 .close = snd_ump_legacy_close, 1264 .trigger = snd_ump_legacy_trigger, 1265 }; 1266 1267 static const struct snd_rawmidi_ops snd_ump_legacy_output_ops = { 1268 .open = snd_ump_legacy_open, 1269 .close = snd_ump_legacy_close, 1270 .trigger = snd_ump_legacy_trigger, 1271 .drain = snd_ump_legacy_drain, 1272 }; 1273 1274 static const struct snd_rawmidi_global_ops snd_ump_legacy_ops = { 1275 .dev_register = snd_ump_legacy_dev_register, 1276 }; 1277 1278 static int process_legacy_output(struct snd_ump_endpoint *ump, 1279 u32 *buffer, int count) 1280 { 1281 struct snd_rawmidi_substream *substream; 1282 struct ump_cvt_to_ump *ctx; 1283 const int dir = SNDRV_RAWMIDI_STREAM_OUTPUT; 1284 unsigned int protocol; 1285 unsigned char c; 1286 int group, size = 0; 1287 1288 if (!ump->out_cvts || !ump->legacy_out_opens) 1289 return 0; 1290 1291 guard(spinlock_irqsave)(&ump->legacy_locks[dir]); 1292 for (group = 0; group < SNDRV_UMP_MAX_GROUPS; group++) { 1293 substream = ump->legacy_substreams[dir][group]; 1294 if (!substream) 1295 continue; 1296 ctx = &ump->out_cvts[group]; 1297 protocol = ump->info.protocol; 1298 if ((protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI2) && 1299 ump->groups[group].is_midi1) 1300 protocol = SNDRV_UMP_EP_INFO_PROTO_MIDI1; 1301 while (!ctx->ump_bytes && 1302 snd_rawmidi_transmit(substream, &c, 1) > 0) 1303 snd_ump_convert_to_ump(ctx, group, protocol, c); 1304 if (ctx->ump_bytes && ctx->ump_bytes <= count) { 1305 size = ctx->ump_bytes; 1306 memcpy(buffer, ctx->ump, size); 1307 ctx->ump_bytes = 0; 1308 break; 1309 } 1310 } 1311 return size; 1312 } 1313 1314 static void process_legacy_input(struct snd_ump_endpoint *ump, const u32 *src, 1315 int words) 1316 { 1317 struct snd_rawmidi_substream *substream; 1318 unsigned char buf[16]; 1319 unsigned char group; 1320 const int dir = SNDRV_RAWMIDI_STREAM_INPUT; 1321 int size; 1322 1323 size = snd_ump_convert_from_ump(src, buf, &group); 1324 if (size <= 0) 1325 return; 1326 guard(spinlock_irqsave)(&ump->legacy_locks[dir]); 1327 substream = ump->legacy_substreams[dir][group]; 1328 if (substream) 1329 snd_rawmidi_receive(substream, buf, size); 1330 } 1331 1332 /* Fill ump->legacy_mapping[] for groups to be used for legacy rawmidi */ 1333 static int fill_legacy_mapping(struct snd_ump_endpoint *ump) 1334 { 1335 struct snd_ump_block *fb; 1336 unsigned int group_maps = 0; 1337 int i, num; 1338 1339 if (ump->info.flags & SNDRV_UMP_EP_INFO_STATIC_BLOCKS) { 1340 list_for_each_entry(fb, &ump->block_list, list) { 1341 for (i = 0; i < fb->info.num_groups; i++) 1342 group_maps |= 1U << (fb->info.first_group + i); 1343 } 1344 if (!group_maps) 1345 ump_info(ump, "No UMP Group is found in FB\n"); 1346 } 1347 1348 /* use all groups for non-static case */ 1349 if (!group_maps) 1350 group_maps = (1U << SNDRV_UMP_MAX_GROUPS) - 1; 1351 1352 num = 0; 1353 for (i = 0; i < SNDRV_UMP_MAX_GROUPS; i++) 1354 if (group_maps & (1U << i)) 1355 ump->legacy_mapping[num++] = i; 1356 1357 return num; 1358 } 1359 1360 static void update_legacy_substreams(struct snd_ump_endpoint *ump, 1361 struct snd_rawmidi *rmidi, int dir) 1362 { 1363 struct snd_rawmidi_substream *s; 1364 const char *name; 1365 int idx; 1366 1367 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) { 1368 idx = ump->legacy_mapping[s->number]; 1369 name = ump->groups[idx].name; 1370 if (!*name) 1371 name = ump->core.name; 1372 scnprintf(s->name, sizeof(s->name), "Group %d (%.16s)%s", 1373 idx + 1, name, 1374 ump->groups[idx].active ? "" : " [Inactive]"); 1375 s->inactive = !ump->groups[idx].active; 1376 } 1377 } 1378 1379 static void update_legacy_names(struct snd_ump_endpoint *ump) 1380 { 1381 struct snd_rawmidi *rmidi = ump->legacy_rmidi; 1382 1383 if (!rmidi) 1384 return; 1385 update_legacy_substreams(ump, rmidi, SNDRV_RAWMIDI_STREAM_INPUT); 1386 update_legacy_substreams(ump, rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT); 1387 } 1388 1389 static void ump_legacy_set_rawmidi_name(struct snd_ump_endpoint *ump) 1390 { 1391 struct snd_rawmidi *rmidi = ump->legacy_rmidi; 1392 1393 if (!rmidi) 1394 return; 1395 snprintf(rmidi->name, sizeof(rmidi->name), "%.68s (MIDI 1.0)", 1396 ump->core.name); 1397 } 1398 1399 int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump, 1400 char *id, int device) 1401 { 1402 struct snd_rawmidi *rmidi; 1403 bool input, output; 1404 int err, num; 1405 1406 ump->out_cvts = kzalloc_objs(*ump->out_cvts, SNDRV_UMP_MAX_GROUPS); 1407 if (!ump->out_cvts) 1408 return -ENOMEM; 1409 1410 num = fill_legacy_mapping(ump); 1411 1412 input = ump->core.info_flags & SNDRV_RAWMIDI_INFO_INPUT; 1413 output = ump->core.info_flags & SNDRV_RAWMIDI_INFO_OUTPUT; 1414 err = snd_rawmidi_new(ump->core.card, id, device, 1415 output ? num : 0, input ? num : 0, 1416 &rmidi); 1417 if (err < 0) { 1418 kfree(ump->out_cvts); 1419 ump->out_cvts = NULL; 1420 return err; 1421 } 1422 1423 if (input) 1424 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, 1425 &snd_ump_legacy_input_ops); 1426 if (output) 1427 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 1428 &snd_ump_legacy_output_ops); 1429 rmidi->info_flags = ump->core.info_flags & ~SNDRV_RAWMIDI_INFO_UMP; 1430 rmidi->ops = &snd_ump_legacy_ops; 1431 rmidi->private_data = ump; 1432 ump->legacy_rmidi = rmidi; 1433 ump_legacy_set_rawmidi_name(ump); 1434 update_legacy_names(ump); 1435 1436 snd_rawmidi_tie_devices(rmidi, &ump->core); 1437 1438 ump_dbg(ump, "Created a legacy rawmidi #%d (%s)\n", device, id); 1439 return 0; 1440 } 1441 EXPORT_SYMBOL_GPL(snd_ump_attach_legacy_rawmidi); 1442 #endif /* CONFIG_SND_UMP_LEGACY_RAWMIDI */ 1443 1444 MODULE_DESCRIPTION("Universal MIDI Packet (UMP) Core Driver"); 1445 MODULE_LICENSE("GPL"); 1446