1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Universal MIDI Packet (UMP) support
6 #include <linux/list.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/export.h>
11 #include <sound/core.h>
12 #include <sound/rawmidi.h>
13 #include <sound/ump.h>
14 #include <sound/ump_convert.h>
16 #define ump_err(ump, fmt, args...) dev_err((ump)->core.dev, fmt, ##args)
17 #define ump_warn(ump, fmt, args...) dev_warn((ump)->core.dev, fmt, ##args)
18 #define ump_info(ump, fmt, args...) dev_info((ump)->core.dev, fmt, ##args)
19 #define ump_dbg(ump, fmt, args...) dev_dbg((ump)->core.dev, fmt, ##args)
21 static int snd_ump_dev_register(struct snd_rawmidi *rmidi);
22 static int snd_ump_dev_unregister(struct snd_rawmidi *rmidi);
23 static long snd_ump_ioctl(struct snd_rawmidi *rmidi, unsigned int cmd,
25 static void snd_ump_proc_read(struct snd_info_entry *entry,
26 struct snd_info_buffer *buffer);
27 static int snd_ump_rawmidi_open(struct snd_rawmidi_substream *substream);
28 static int snd_ump_rawmidi_close(struct snd_rawmidi_substream *substream);
29 static void snd_ump_rawmidi_trigger(struct snd_rawmidi_substream *substream,
31 static void snd_ump_rawmidi_drain(struct snd_rawmidi_substream *substream);
33 static void ump_handle_stream_msg(struct snd_ump_endpoint *ump,
34 const u32 *buf, int size);
35 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI)
36 static int process_legacy_output(struct snd_ump_endpoint *ump,
37 u32 *buffer, int count);
38 static void process_legacy_input(struct snd_ump_endpoint *ump, const u32 *src,
41 static inline int process_legacy_output(struct snd_ump_endpoint *ump,
42 u32 *buffer, int count)
46 static inline void process_legacy_input(struct snd_ump_endpoint *ump,
47 const u32 *src, int words)
52 static const struct snd_rawmidi_global_ops snd_ump_rawmidi_ops = {
53 .dev_register = snd_ump_dev_register,
54 .dev_unregister = snd_ump_dev_unregister,
55 .ioctl = snd_ump_ioctl,
56 .proc_read = snd_ump_proc_read,
59 static const struct snd_rawmidi_ops snd_ump_rawmidi_input_ops = {
60 .open = snd_ump_rawmidi_open,
61 .close = snd_ump_rawmidi_close,
62 .trigger = snd_ump_rawmidi_trigger,
65 static const struct snd_rawmidi_ops snd_ump_rawmidi_output_ops = {
66 .open = snd_ump_rawmidi_open,
67 .close = snd_ump_rawmidi_close,
68 .trigger = snd_ump_rawmidi_trigger,
69 .drain = snd_ump_rawmidi_drain,
72 static void snd_ump_endpoint_free(struct snd_rawmidi *rmidi)
74 struct snd_ump_endpoint *ump = rawmidi_to_ump(rmidi);
75 struct snd_ump_block *fb;
77 while (!list_empty(&ump->block_list)) {
78 fb = list_first_entry(&ump->block_list, struct snd_ump_block,
86 if (ump->private_free)
87 ump->private_free(ump);
89 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI)
95 * snd_ump_endpoint_new - create a UMP Endpoint object
96 * @card: the card instance
97 * @id: the id string for rawmidi
98 * @device: the device index for rawmidi
99 * @output: 1 for enabling output
100 * @input: 1 for enabling input
101 * @ump_ret: the pointer to store the new UMP instance
103 * Creates a new UMP Endpoint object. A UMP Endpoint is tied with one rawmidi
104 * instance with one input and/or one output rawmidi stream (either uni-
105 * or bi-directional). A UMP Endpoint may contain one or multiple UMP Blocks
106 * that consist of one or multiple UMP Groups.
108 * Use snd_rawmidi_set_ops() to set the operators to the new instance.
109 * Unlike snd_rawmidi_new(), this function sets up the info_flags by itself
110 * depending on the given @output and @input.
112 * The device has SNDRV_RAWMIDI_INFO_UMP flag set and a different device
113 * file ("umpCxDx") than a standard MIDI 1.x device ("midiCxDx") is
116 * Return: Zero if successful, or a negative error code on failure.
118 int snd_ump_endpoint_new(struct snd_card *card, char *id, int device,
119 int output, int input,
120 struct snd_ump_endpoint **ump_ret)
122 unsigned int info_flags = SNDRV_RAWMIDI_INFO_UMP;
123 struct snd_ump_endpoint *ump;
127 info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
129 info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
131 info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
133 ump = kzalloc(sizeof(*ump), GFP_KERNEL);
136 INIT_LIST_HEAD(&ump->block_list);
137 mutex_init(&ump->open_mutex);
138 init_waitqueue_head(&ump->stream_wait);
139 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI)
140 spin_lock_init(&ump->legacy_locks[0]);
141 spin_lock_init(&ump->legacy_locks[1]);
143 err = snd_rawmidi_init(&ump->core, card, id, device,
144 output, input, info_flags);
146 snd_rawmidi_free(&ump->core);
150 ump->info.card = card->number;
151 ump->info.device = device;
153 ump->core.private_free = snd_ump_endpoint_free;
154 ump->core.ops = &snd_ump_rawmidi_ops;
156 snd_rawmidi_set_ops(&ump->core, SNDRV_RAWMIDI_STREAM_INPUT,
157 &snd_ump_rawmidi_input_ops);
159 snd_rawmidi_set_ops(&ump->core, SNDRV_RAWMIDI_STREAM_OUTPUT,
160 &snd_ump_rawmidi_output_ops);
162 ump_dbg(ump, "Created a UMP EP #%d (%s)\n", device, id);
166 EXPORT_SYMBOL_GPL(snd_ump_endpoint_new);
169 * Device register / unregister hooks;
170 * do nothing, placeholders for avoiding the default rawmidi handling
173 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
174 static void snd_ump_dev_seq_free(struct snd_seq_device *device)
176 struct snd_ump_endpoint *ump = device->private_data;
182 static int snd_ump_dev_register(struct snd_rawmidi *rmidi)
184 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
185 struct snd_ump_endpoint *ump = rawmidi_to_ump(rmidi);
188 err = snd_seq_device_new(ump->core.card, ump->core.device,
189 SNDRV_SEQ_DEV_ID_UMP, 0, &ump->seq_dev);
192 ump->seq_dev->private_data = ump;
193 ump->seq_dev->private_free = snd_ump_dev_seq_free;
194 snd_device_register(ump->core.card, ump->seq_dev);
199 static int snd_ump_dev_unregister(struct snd_rawmidi *rmidi)
204 static struct snd_ump_block *
205 snd_ump_get_block(struct snd_ump_endpoint *ump, unsigned char id)
207 struct snd_ump_block *fb;
209 list_for_each_entry(fb, &ump->block_list, list) {
210 if (fb->info.block_id == id)
217 * rawmidi ops for UMP endpoint
219 static int snd_ump_rawmidi_open(struct snd_rawmidi_substream *substream)
221 struct snd_ump_endpoint *ump = rawmidi_to_ump(substream->rmidi);
222 int dir = substream->stream;
225 if (ump->substreams[dir])
227 err = ump->ops->open(ump, dir);
230 ump->substreams[dir] = substream;
234 static int snd_ump_rawmidi_close(struct snd_rawmidi_substream *substream)
236 struct snd_ump_endpoint *ump = rawmidi_to_ump(substream->rmidi);
237 int dir = substream->stream;
239 ump->substreams[dir] = NULL;
240 ump->ops->close(ump, dir);
244 static void snd_ump_rawmidi_trigger(struct snd_rawmidi_substream *substream,
247 struct snd_ump_endpoint *ump = rawmidi_to_ump(substream->rmidi);
248 int dir = substream->stream;
250 ump->ops->trigger(ump, dir, up);
253 static void snd_ump_rawmidi_drain(struct snd_rawmidi_substream *substream)
255 struct snd_ump_endpoint *ump = rawmidi_to_ump(substream->rmidi);
258 ump->ops->drain(ump, SNDRV_RAWMIDI_STREAM_OUTPUT);
261 /* number of 32bit words per message type */
262 static unsigned char ump_packet_words[0x10] = {
263 1, 1, 1, 2, 2, 4, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4
267 * snd_ump_receive_ump_val - parse the UMP packet data
269 * @val: UMP packet data
271 * The data is copied onto ump->input_buf[].
272 * When a full packet is completed, returns the number of words (from 1 to 4).
273 * OTOH, if the packet is incomplete, returns 0.
275 int snd_ump_receive_ump_val(struct snd_ump_endpoint *ump, u32 val)
279 if (!ump->input_pending)
280 ump->input_pending = ump_packet_words[ump_message_type(val)];
282 ump->input_buf[ump->input_buf_head++] = val;
283 ump->input_pending--;
284 if (!ump->input_pending) {
285 words = ump->input_buf_head;
286 ump->input_buf_head = 0;
291 EXPORT_SYMBOL_GPL(snd_ump_receive_ump_val);
294 * snd_ump_receive - transfer UMP packets from the device
295 * @ump: the UMP endpoint
296 * @buffer: the buffer pointer to transfer
297 * @count: byte size to transfer
299 * Called from the driver to submit the received UMP packets from the device
300 * to user-space. It's essentially a wrapper of rawmidi_receive().
301 * The data to receive is in CPU-native endianness.
303 int snd_ump_receive(struct snd_ump_endpoint *ump, const u32 *buffer, int count)
305 struct snd_rawmidi_substream *substream;
306 const u32 *p = buffer;
307 int n, words = count >> 2;
310 n = snd_ump_receive_ump_val(ump, *p++);
313 ump_handle_stream_msg(ump, ump->input_buf, n);
314 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
316 ump->seq_ops->input_receive(ump, ump->input_buf, n);
318 process_legacy_input(ump, ump->input_buf, n);
321 substream = ump->substreams[SNDRV_RAWMIDI_STREAM_INPUT];
324 return snd_rawmidi_receive(substream, (const char *)buffer, count);
326 EXPORT_SYMBOL_GPL(snd_ump_receive);
329 * snd_ump_transmit - transmit UMP packets
330 * @ump: the UMP endpoint
331 * @buffer: the buffer pointer to transfer
332 * @count: byte size to transfer
334 * Called from the driver to obtain the UMP packets from user-space to the
335 * device. It's essentially a wrapper of rawmidi_transmit().
336 * The data to transmit is in CPU-native endianness.
338 int snd_ump_transmit(struct snd_ump_endpoint *ump, u32 *buffer, int count)
340 struct snd_rawmidi_substream *substream =
341 ump->substreams[SNDRV_RAWMIDI_STREAM_OUTPUT];
346 err = snd_rawmidi_transmit(substream, (char *)buffer, count);
347 /* received either data or an error? */
350 return process_legacy_output(ump, buffer, count);
352 EXPORT_SYMBOL_GPL(snd_ump_transmit);
355 * snd_ump_block_new - Create a UMP block
357 * @blk: block ID number to create
358 * @direction: direction (in/out/bidirection)
359 * @first_group: the first group ID (0-based)
360 * @num_groups: the number of groups in this block
361 * @blk_ret: the pointer to store the resultant block object
363 int snd_ump_block_new(struct snd_ump_endpoint *ump, unsigned int blk,
364 unsigned int direction, unsigned int first_group,
365 unsigned int num_groups, struct snd_ump_block **blk_ret)
367 struct snd_ump_block *fb, *p;
369 if (blk < 0 || blk >= SNDRV_UMP_MAX_BLOCKS)
372 if (snd_ump_get_block(ump, blk))
375 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
380 fb->info.card = ump->info.card;
381 fb->info.device = ump->info.device;
382 fb->info.block_id = blk;
383 if (blk >= ump->info.num_blocks)
384 ump->info.num_blocks = blk + 1;
385 fb->info.direction = direction;
387 fb->info.first_group = first_group;
388 fb->info.num_groups = num_groups;
389 /* fill the default name, may be overwritten to a better name */
390 snprintf(fb->info.name, sizeof(fb->info.name), "Group %d-%d",
391 first_group + 1, first_group + num_groups);
393 /* put the entry in the ordered list */
394 list_for_each_entry(p, &ump->block_list, list) {
395 if (p->info.block_id > blk) {
396 list_add_tail(&fb->list, &p->list);
400 list_add_tail(&fb->list, &ump->block_list);
403 ump_dbg(ump, "Created a UMP Block #%d (%s)\n", blk, fb->info.name);
407 EXPORT_SYMBOL_GPL(snd_ump_block_new);
409 static int snd_ump_ioctl_block(struct snd_ump_endpoint *ump,
410 struct snd_ump_block_info __user *argp)
412 struct snd_ump_block *fb;
415 if (get_user(id, &argp->block_id))
417 fb = snd_ump_get_block(ump, id);
420 if (copy_to_user(argp, &fb->info, sizeof(fb->info)))
426 * Handle UMP-specific ioctls; called from snd_rawmidi_ioctl()
428 static long snd_ump_ioctl(struct snd_rawmidi *rmidi, unsigned int cmd,
431 struct snd_ump_endpoint *ump = rawmidi_to_ump(rmidi);
434 case SNDRV_UMP_IOCTL_ENDPOINT_INFO:
435 if (copy_to_user(argp, &ump->info, sizeof(ump->info)))
438 case SNDRV_UMP_IOCTL_BLOCK_INFO:
439 return snd_ump_ioctl_block(ump, argp);
441 ump_dbg(ump, "rawmidi: unknown command = 0x%x\n", cmd);
446 static const char *ump_direction_string(int dir)
449 case SNDRV_UMP_DIR_INPUT:
451 case SNDRV_UMP_DIR_OUTPUT:
453 case SNDRV_UMP_DIR_BIDIRECTION:
454 return "bidirection";
460 static const char *ump_ui_hint_string(int dir)
463 case SNDRV_UMP_BLOCK_UI_HINT_RECEIVER:
465 case SNDRV_UMP_BLOCK_UI_HINT_SENDER:
467 case SNDRV_UMP_BLOCK_UI_HINT_BOTH:
474 /* Additional proc file output */
475 static void snd_ump_proc_read(struct snd_info_entry *entry,
476 struct snd_info_buffer *buffer)
478 struct snd_rawmidi *rmidi = entry->private_data;
479 struct snd_ump_endpoint *ump = rawmidi_to_ump(rmidi);
480 struct snd_ump_block *fb;
482 snd_iprintf(buffer, "EP Name: %s\n", ump->info.name);
483 snd_iprintf(buffer, "EP Product ID: %s\n", ump->info.product_id);
484 snd_iprintf(buffer, "UMP Version: 0x%04x\n", ump->info.version);
485 snd_iprintf(buffer, "Protocol Caps: 0x%08x\n", ump->info.protocol_caps);
486 snd_iprintf(buffer, "Protocol: 0x%08x\n", ump->info.protocol);
487 if (ump->info.version) {
488 snd_iprintf(buffer, "Manufacturer ID: 0x%08x\n",
489 ump->info.manufacturer_id);
490 snd_iprintf(buffer, "Family ID: 0x%04x\n", ump->info.family_id);
491 snd_iprintf(buffer, "Model ID: 0x%04x\n", ump->info.model_id);
492 snd_iprintf(buffer, "SW Revision: 0x%4phN\n", ump->info.sw_revision);
494 snd_iprintf(buffer, "Static Blocks: %s\n",
495 (ump->info.flags & SNDRV_UMP_EP_INFO_STATIC_BLOCKS) ? "Yes" : "No");
496 snd_iprintf(buffer, "Num Blocks: %d\n\n", ump->info.num_blocks);
498 list_for_each_entry(fb, &ump->block_list, list) {
499 snd_iprintf(buffer, "Block %d (%s)\n", fb->info.block_id,
501 snd_iprintf(buffer, " Direction: %s\n",
502 ump_direction_string(fb->info.direction));
503 snd_iprintf(buffer, " Active: %s\n",
504 fb->info.active ? "Yes" : "No");
505 snd_iprintf(buffer, " Groups: %d-%d\n",
506 fb->info.first_group + 1,
507 fb->info.first_group + fb->info.num_groups);
508 snd_iprintf(buffer, " Is MIDI1: %s%s\n",
509 (fb->info.flags & SNDRV_UMP_BLOCK_IS_MIDI1) ? "Yes" : "No",
510 (fb->info.flags & SNDRV_UMP_BLOCK_IS_LOWSPEED) ? " (Low Speed)" : "");
511 if (ump->info.version) {
512 snd_iprintf(buffer, " MIDI-CI Version: %d\n",
513 fb->info.midi_ci_version);
514 snd_iprintf(buffer, " Sysex8 Streams: %d\n",
515 fb->info.sysex8_streams);
516 snd_iprintf(buffer, " UI Hint: %s\n",
517 ump_ui_hint_string(fb->info.ui_hint));
519 snd_iprintf(buffer, "\n");
523 /* update dir_bits and active flag for all groups in the client */
524 void snd_ump_update_group_attrs(struct snd_ump_endpoint *ump)
526 struct snd_ump_block *fb;
527 struct snd_ump_group *group;
530 for (i = 0; i < SNDRV_UMP_MAX_GROUPS; i++) {
531 group = &ump->groups[i];
536 group->valid = false;
537 group->is_midi1 = false;
540 list_for_each_entry(fb, &ump->block_list, list) {
541 if (fb->info.first_group + fb->info.num_groups > SNDRV_UMP_MAX_GROUPS)
543 group = &ump->groups[fb->info.first_group];
544 for (i = 0; i < fb->info.num_groups; i++, group++) {
548 if (fb->info.flags & SNDRV_UMP_BLOCK_IS_MIDI1)
549 group->is_midi1 = true;
550 switch (fb->info.direction) {
551 case SNDRV_UMP_DIR_INPUT:
552 group->dir_bits |= (1 << SNDRV_RAWMIDI_STREAM_INPUT);
554 case SNDRV_UMP_DIR_OUTPUT:
555 group->dir_bits |= (1 << SNDRV_RAWMIDI_STREAM_OUTPUT);
557 case SNDRV_UMP_DIR_BIDIRECTION:
558 group->dir_bits |= (1 << SNDRV_RAWMIDI_STREAM_INPUT) |
559 (1 << SNDRV_RAWMIDI_STREAM_OUTPUT);
565 /* store the first matching name */
566 strscpy(group->name, fb->info.name,
567 sizeof(group->name));
569 /* when overlapping, concat names */
570 strlcat(group->name, ", ", sizeof(group->name));
571 strlcat(group->name, fb->info.name,
572 sizeof(group->name));
577 EXPORT_SYMBOL_GPL(snd_ump_update_group_attrs);
580 * UMP endpoint and function block handling
583 /* open / close UMP streams for the internal stream msg communication */
584 static int ump_request_open(struct snd_ump_endpoint *ump)
586 return snd_rawmidi_kernel_open(&ump->core, 0,
587 SNDRV_RAWMIDI_LFLG_OUTPUT,
591 static void ump_request_close(struct snd_ump_endpoint *ump)
593 snd_rawmidi_kernel_release(&ump->stream_rfile);
596 /* request a command and wait for the given response;
597 * @req1 and @req2 are u32 commands
598 * @reply is the expected UMP stream status
600 static int ump_req_msg(struct snd_ump_endpoint *ump, u32 req1, u32 req2,
605 ump_dbg(ump, "%s: request %08x %08x, wait-for %08x\n",
606 __func__, req1, req2, reply);
607 memset(buf, 0, sizeof(buf));
610 ump->stream_finished = 0;
611 ump->stream_wait_for = reply;
612 snd_rawmidi_kernel_write(ump->stream_rfile.output,
613 (unsigned char *)&buf, 16);
614 wait_event_timeout(ump->stream_wait, ump->stream_finished,
615 msecs_to_jiffies(500));
616 if (!READ_ONCE(ump->stream_finished)) {
617 ump_dbg(ump, "%s: request timed out\n", __func__);
620 ump->stream_finished = 0;
621 ump_dbg(ump, "%s: reply: %08x %08x %08x %08x\n",
622 __func__, buf[0], buf[1], buf[2], buf[3]);
626 /* append the received letters via UMP packet to the given string buffer;
627 * return 1 if the full string is received or 0 to continue
629 static int ump_append_string(struct snd_ump_endpoint *ump, char *dest,
630 int maxsize, const u32 *buf, int offset)
632 unsigned char format;
635 format = ump_stream_message_format(buf[0]);
636 if (format == UMP_STREAM_MSG_FORMAT_SINGLE ||
637 format == UMP_STREAM_MSG_FORMAT_START) {
641 if (c >= maxsize - 1)
645 for (; offset < 16; offset++) {
646 dest[c] = buf[offset / 4] >> (3 - (offset % 4)) * 8;
649 if (++c >= maxsize - 1)
653 return (format == UMP_STREAM_MSG_FORMAT_SINGLE ||
654 format == UMP_STREAM_MSG_FORMAT_END);
657 /* Choose the default protocol */
658 static void choose_default_protocol(struct snd_ump_endpoint *ump)
660 if (ump->info.protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK)
662 if (ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI2)
663 ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI2;
665 ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI1;
668 /* handle EP info stream message; update the UMP attributes */
669 static int ump_handle_ep_info_msg(struct snd_ump_endpoint *ump,
670 const union snd_ump_stream_msg *buf)
672 ump->info.version = (buf->ep_info.ump_version_major << 8) |
673 buf->ep_info.ump_version_minor;
674 ump->info.num_blocks = buf->ep_info.num_function_blocks;
675 if (ump->info.num_blocks > SNDRV_UMP_MAX_BLOCKS) {
676 ump_info(ump, "Invalid function blocks %d, fallback to 1\n",
677 ump->info.num_blocks);
678 ump->info.num_blocks = 1;
681 if (buf->ep_info.static_function_block)
682 ump->info.flags |= SNDRV_UMP_EP_INFO_STATIC_BLOCKS;
684 ump->info.protocol_caps = (buf->ep_info.protocol << 8) |
687 ump_dbg(ump, "EP info: version=%x, num_blocks=%x, proto_caps=%x\n",
688 ump->info.version, ump->info.num_blocks, ump->info.protocol_caps);
690 ump->info.protocol &= ump->info.protocol_caps;
691 choose_default_protocol(ump);
693 return 1; /* finished */
696 /* handle EP device info stream message; update the UMP attributes */
697 static int ump_handle_device_info_msg(struct snd_ump_endpoint *ump,
698 const union snd_ump_stream_msg *buf)
700 ump->info.manufacturer_id = buf->device_info.manufacture_id & 0x7f7f7f;
701 ump->info.family_id = (buf->device_info.family_msb << 8) |
702 buf->device_info.family_lsb;
703 ump->info.model_id = (buf->device_info.model_msb << 8) |
704 buf->device_info.model_lsb;
705 ump->info.sw_revision[0] = (buf->device_info.sw_revision >> 24) & 0x7f;
706 ump->info.sw_revision[1] = (buf->device_info.sw_revision >> 16) & 0x7f;
707 ump->info.sw_revision[2] = (buf->device_info.sw_revision >> 8) & 0x7f;
708 ump->info.sw_revision[3] = buf->device_info.sw_revision & 0x7f;
709 ump_dbg(ump, "EP devinfo: manid=%08x, family=%04x, model=%04x, sw=%4phN\n",
710 ump->info.manufacturer_id,
713 ump->info.sw_revision);
714 return 1; /* finished */
717 /* handle EP name stream message; update the UMP name string */
718 static int ump_handle_ep_name_msg(struct snd_ump_endpoint *ump,
719 const union snd_ump_stream_msg *buf)
721 return ump_append_string(ump, ump->info.name, sizeof(ump->info.name),
725 /* handle EP product id stream message; update the UMP product_id string */
726 static int ump_handle_product_id_msg(struct snd_ump_endpoint *ump,
727 const union snd_ump_stream_msg *buf)
729 return ump_append_string(ump, ump->info.product_id,
730 sizeof(ump->info.product_id),
734 /* notify the protocol change to sequencer */
735 static void seq_notify_protocol(struct snd_ump_endpoint *ump)
737 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
738 if (ump->seq_ops && ump->seq_ops->switch_protocol)
739 ump->seq_ops->switch_protocol(ump);
740 #endif /* CONFIG_SND_SEQUENCER */
744 * snd_ump_switch_protocol - switch MIDI protocol
746 * @protocol: protocol to switch to
748 * Returns 1 if the protocol is actually switched, 0 if unchanged
750 int snd_ump_switch_protocol(struct snd_ump_endpoint *ump, unsigned int protocol)
754 protocol &= ump->info.protocol_caps;
755 if (protocol == ump->info.protocol)
758 type = protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK;
759 if (type != SNDRV_UMP_EP_INFO_PROTO_MIDI1 &&
760 type != SNDRV_UMP_EP_INFO_PROTO_MIDI2)
763 ump->info.protocol = protocol;
764 ump_dbg(ump, "New protocol = %x (caps = %x)\n",
765 protocol, ump->info.protocol_caps);
766 seq_notify_protocol(ump);
769 EXPORT_SYMBOL_GPL(snd_ump_switch_protocol);
771 /* handle EP stream config message; update the UMP protocol */
772 static int ump_handle_stream_cfg_msg(struct snd_ump_endpoint *ump,
773 const union snd_ump_stream_msg *buf)
775 unsigned int protocol =
776 (buf->stream_cfg.protocol << 8) | buf->stream_cfg.jrts;
778 snd_ump_switch_protocol(ump, protocol);
779 return 1; /* finished */
782 /* Extract Function Block info from UMP packet */
783 static void fill_fb_info(struct snd_ump_endpoint *ump,
784 struct snd_ump_block_info *info,
785 const union snd_ump_stream_msg *buf)
787 info->direction = buf->fb_info.direction;
788 info->ui_hint = buf->fb_info.ui_hint;
789 info->first_group = buf->fb_info.first_group;
790 info->num_groups = buf->fb_info.num_groups;
791 info->flags = buf->fb_info.midi_10;
792 info->active = buf->fb_info.active;
793 info->midi_ci_version = buf->fb_info.midi_ci_version;
794 info->sysex8_streams = buf->fb_info.sysex8_streams;
796 ump_dbg(ump, "FB %d: dir=%d, active=%d, first_gp=%d, num_gp=%d, midici=%d, sysex8=%d, flags=0x%x\n",
797 info->block_id, info->direction, info->active,
798 info->first_group, info->num_groups, info->midi_ci_version,
799 info->sysex8_streams, info->flags);
801 if ((info->flags & SNDRV_UMP_BLOCK_IS_MIDI1) && info->num_groups != 1) {
802 info->num_groups = 1;
803 ump_dbg(ump, "FB %d: corrected groups to 1 for MIDI1\n",
808 /* check whether the FB info gets updated by the current message */
809 static bool is_fb_info_updated(struct snd_ump_endpoint *ump,
810 struct snd_ump_block *fb,
811 const union snd_ump_stream_msg *buf)
813 char tmpbuf[offsetof(struct snd_ump_block_info, name)];
815 if (ump->info.flags & SNDRV_UMP_EP_INFO_STATIC_BLOCKS) {
816 ump_info(ump, "Skipping static FB info update (blk#%d)\n",
821 memcpy(tmpbuf, &fb->info, sizeof(tmpbuf));
822 fill_fb_info(ump, (struct snd_ump_block_info *)tmpbuf, buf);
823 return memcmp(&fb->info, tmpbuf, sizeof(tmpbuf)) != 0;
826 /* notify the FB info/name change to sequencer */
827 static void seq_notify_fb_change(struct snd_ump_endpoint *ump,
828 struct snd_ump_block *fb)
830 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
831 if (ump->seq_ops && ump->seq_ops->notify_fb_change)
832 ump->seq_ops->notify_fb_change(ump, fb);
836 /* handle FB info message; update FB info if the block is present */
837 static int ump_handle_fb_info_msg(struct snd_ump_endpoint *ump,
838 const union snd_ump_stream_msg *buf)
841 struct snd_ump_block *fb;
843 blk = buf->fb_info.function_block_id;
844 fb = snd_ump_get_block(ump, blk);
846 /* complain only if updated after parsing */
847 if (!fb && ump->parsed) {
848 ump_info(ump, "Function Block Info Update for non-existing block %d\n",
853 /* When updated after the initial parse, check the FB info update */
854 if (ump->parsed && !is_fb_info_updated(ump, fb, buf))
855 return 1; /* no content change */
858 fill_fb_info(ump, &fb->info, buf);
860 snd_ump_update_group_attrs(ump);
861 seq_notify_fb_change(ump, fb);
865 return 1; /* finished */
868 /* handle FB name message; update the FB name string */
869 static int ump_handle_fb_name_msg(struct snd_ump_endpoint *ump,
870 const union snd_ump_stream_msg *buf)
873 struct snd_ump_block *fb;
876 blk = buf->fb_name.function_block_id;
877 fb = snd_ump_get_block(ump, blk);
882 (ump->info.flags & SNDRV_UMP_EP_INFO_STATIC_BLOCKS)) {
883 ump_dbg(ump, "Skipping static FB name update (blk#%d)\n",
888 ret = ump_append_string(ump, fb->info.name, sizeof(fb->info.name),
890 /* notify the FB name update to sequencer, too */
891 if (ret > 0 && ump->parsed) {
892 snd_ump_update_group_attrs(ump);
893 seq_notify_fb_change(ump, fb);
898 static int create_block_from_fb_info(struct snd_ump_endpoint *ump, int blk)
900 struct snd_ump_block *fb;
901 unsigned char direction, first_group, num_groups;
902 const union snd_ump_stream_msg *buf =
903 (const union snd_ump_stream_msg *)ump->input_buf;
907 /* query the FB info once */
908 msg = ump_stream_compose(UMP_STREAM_MSG_STATUS_FB_DISCOVERY, 0) |
909 (blk << 8) | UMP_STREAM_MSG_REQUEST_FB_INFO;
910 err = ump_req_msg(ump, msg, 0, UMP_STREAM_MSG_STATUS_FB_INFO);
912 ump_dbg(ump, "Unable to get FB info for block %d\n", blk);
916 /* the last input must be the FB info */
917 if (buf->fb_info.status != UMP_STREAM_MSG_STATUS_FB_INFO) {
918 ump_dbg(ump, "Inconsistent input: 0x%x\n", *buf->raw);
922 direction = buf->fb_info.direction;
923 first_group = buf->fb_info.first_group;
924 num_groups = buf->fb_info.num_groups;
926 err = snd_ump_block_new(ump, blk, direction, first_group, num_groups,
931 fill_fb_info(ump, &fb->info, buf);
933 msg = ump_stream_compose(UMP_STREAM_MSG_STATUS_FB_DISCOVERY, 0) |
934 (blk << 8) | UMP_STREAM_MSG_REQUEST_FB_NAME;
935 err = ump_req_msg(ump, msg, 0, UMP_STREAM_MSG_STATUS_FB_NAME);
937 ump_dbg(ump, "Unable to get UMP FB name string #%d\n", blk);
942 /* handle stream messages, called from snd_ump_receive() */
943 static void ump_handle_stream_msg(struct snd_ump_endpoint *ump,
944 const u32 *buf, int size)
946 const union snd_ump_stream_msg *msg;
950 /* UMP stream message suppressed (for gadget UMP)? */
951 if (ump->no_process_stream)
954 BUILD_BUG_ON(sizeof(*msg) != 16);
955 ump_dbg(ump, "Stream msg: %08x %08x %08x %08x\n",
956 buf[0], buf[1], buf[2], buf[3]);
958 if (size != 4 || ump_message_type(*buf) != UMP_MSG_TYPE_STREAM)
961 msg = (const union snd_ump_stream_msg *)buf;
962 status = ump_stream_message_status(*buf);
964 case UMP_STREAM_MSG_STATUS_EP_INFO:
965 ret = ump_handle_ep_info_msg(ump, msg);
967 case UMP_STREAM_MSG_STATUS_DEVICE_INFO:
968 ret = ump_handle_device_info_msg(ump, msg);
970 case UMP_STREAM_MSG_STATUS_EP_NAME:
971 ret = ump_handle_ep_name_msg(ump, msg);
973 case UMP_STREAM_MSG_STATUS_PRODUCT_ID:
974 ret = ump_handle_product_id_msg(ump, msg);
976 case UMP_STREAM_MSG_STATUS_STREAM_CFG:
977 ret = ump_handle_stream_cfg_msg(ump, msg);
979 case UMP_STREAM_MSG_STATUS_FB_INFO:
980 ret = ump_handle_fb_info_msg(ump, msg);
982 case UMP_STREAM_MSG_STATUS_FB_NAME:
983 ret = ump_handle_fb_name_msg(ump, msg);
989 /* when the message has been processed fully, wake up */
990 if (ret > 0 && ump->stream_wait_for == status) {
991 WRITE_ONCE(ump->stream_finished, 1);
992 wake_up(&ump->stream_wait);
997 * snd_ump_parse_endpoint - parse endpoint and create function blocks
1000 * Returns 0 for successful parse, -ENODEV if device doesn't respond
1001 * (or the query is unsupported), or other error code for serious errors.
1003 int snd_ump_parse_endpoint(struct snd_ump_endpoint *ump)
1008 if (!(ump->core.info_flags & SNDRV_RAWMIDI_INFO_DUPLEX))
1011 err = ump_request_open(ump);
1013 ump_dbg(ump, "Unable to open rawmidi device: %d\n", err);
1017 /* Check Endpoint Information */
1018 msg = ump_stream_compose(UMP_STREAM_MSG_STATUS_EP_DISCOVERY, 0) |
1019 0x0101; /* UMP version 1.1 */
1020 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_EP_INFO,
1021 UMP_STREAM_MSG_STATUS_EP_INFO);
1023 ump_dbg(ump, "Unable to get UMP EP info\n");
1027 /* Request Endpoint Device Info */
1028 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_DEVICE_INFO,
1029 UMP_STREAM_MSG_STATUS_DEVICE_INFO);
1031 ump_dbg(ump, "Unable to get UMP EP device info\n");
1033 /* Request Endpoint Name */
1034 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_EP_NAME,
1035 UMP_STREAM_MSG_STATUS_EP_NAME);
1037 ump_dbg(ump, "Unable to get UMP EP name string\n");
1039 /* Request Endpoint Product ID */
1040 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_PRODUCT_ID,
1041 UMP_STREAM_MSG_STATUS_PRODUCT_ID);
1043 ump_dbg(ump, "Unable to get UMP EP product ID string\n");
1045 /* Get the current stream configuration */
1046 err = ump_req_msg(ump, msg, UMP_STREAM_MSG_REQUEST_STREAM_CFG,
1047 UMP_STREAM_MSG_STATUS_STREAM_CFG);
1049 ump_dbg(ump, "Unable to get UMP EP stream config\n");
1051 /* If no protocol is set by some reason, assume the valid one */
1052 choose_default_protocol(ump);
1054 /* Query and create blocks from Function Blocks */
1055 for (blk = 0; blk < ump->info.num_blocks; blk++) {
1056 err = create_block_from_fb_info(ump, blk);
1061 /* initialize group attributions */
1062 snd_ump_update_group_attrs(ump);
1066 ump_request_close(ump);
1067 if (err == -ETIMEDOUT)
1071 EXPORT_SYMBOL_GPL(snd_ump_parse_endpoint);
1073 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI)
1075 * Legacy rawmidi support
1077 static int snd_ump_legacy_open(struct snd_rawmidi_substream *substream)
1079 struct snd_ump_endpoint *ump = substream->rmidi->private_data;
1080 int dir = substream->stream;
1081 int group = ump->legacy_mapping[substream->number];
1084 guard(mutex)(&ump->open_mutex);
1085 if (ump->legacy_substreams[dir][group])
1087 if (dir == SNDRV_RAWMIDI_STREAM_OUTPUT) {
1088 if (!ump->legacy_out_opens) {
1089 err = snd_rawmidi_kernel_open(&ump->core, 0,
1090 SNDRV_RAWMIDI_LFLG_OUTPUT |
1091 SNDRV_RAWMIDI_LFLG_APPEND,
1092 &ump->legacy_out_rfile);
1096 ump->legacy_out_opens++;
1097 snd_ump_convert_reset(&ump->out_cvts[group]);
1099 guard(spinlock_irq)(&ump->legacy_locks[dir]);
1100 ump->legacy_substreams[dir][group] = substream;
1104 static int snd_ump_legacy_close(struct snd_rawmidi_substream *substream)
1106 struct snd_ump_endpoint *ump = substream->rmidi->private_data;
1107 int dir = substream->stream;
1108 int group = ump->legacy_mapping[substream->number];
1110 guard(mutex)(&ump->open_mutex);
1111 scoped_guard(spinlock_irq, &ump->legacy_locks[dir])
1112 ump->legacy_substreams[dir][group] = NULL;
1113 if (dir == SNDRV_RAWMIDI_STREAM_OUTPUT) {
1114 if (!--ump->legacy_out_opens)
1115 snd_rawmidi_kernel_release(&ump->legacy_out_rfile);
1120 static void snd_ump_legacy_trigger(struct snd_rawmidi_substream *substream,
1123 struct snd_ump_endpoint *ump = substream->rmidi->private_data;
1124 int dir = substream->stream;
1126 ump->ops->trigger(ump, dir, up);
1129 static void snd_ump_legacy_drain(struct snd_rawmidi_substream *substream)
1131 struct snd_ump_endpoint *ump = substream->rmidi->private_data;
1133 if (ump->ops->drain)
1134 ump->ops->drain(ump, SNDRV_RAWMIDI_STREAM_OUTPUT);
1137 static int snd_ump_legacy_dev_register(struct snd_rawmidi *rmidi)
1139 /* dummy, just for avoiding create superfluous seq clients */
1143 static const struct snd_rawmidi_ops snd_ump_legacy_input_ops = {
1144 .open = snd_ump_legacy_open,
1145 .close = snd_ump_legacy_close,
1146 .trigger = snd_ump_legacy_trigger,
1149 static const struct snd_rawmidi_ops snd_ump_legacy_output_ops = {
1150 .open = snd_ump_legacy_open,
1151 .close = snd_ump_legacy_close,
1152 .trigger = snd_ump_legacy_trigger,
1153 .drain = snd_ump_legacy_drain,
1156 static const struct snd_rawmidi_global_ops snd_ump_legacy_ops = {
1157 .dev_register = snd_ump_legacy_dev_register,
1160 static int process_legacy_output(struct snd_ump_endpoint *ump,
1161 u32 *buffer, int count)
1163 struct snd_rawmidi_substream *substream;
1164 struct ump_cvt_to_ump *ctx;
1165 const int dir = SNDRV_RAWMIDI_STREAM_OUTPUT;
1166 unsigned int protocol;
1168 int group, size = 0;
1170 if (!ump->out_cvts || !ump->legacy_out_opens)
1173 guard(spinlock_irqsave)(&ump->legacy_locks[dir]);
1174 for (group = 0; group < SNDRV_UMP_MAX_GROUPS; group++) {
1175 substream = ump->legacy_substreams[dir][group];
1178 ctx = &ump->out_cvts[group];
1179 protocol = ump->info.protocol;
1180 if ((protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI2) &&
1181 ump->groups[group].is_midi1)
1182 protocol = SNDRV_UMP_EP_INFO_PROTO_MIDI1;
1183 while (!ctx->ump_bytes &&
1184 snd_rawmidi_transmit(substream, &c, 1) > 0)
1185 snd_ump_convert_to_ump(ctx, group, protocol, c);
1186 if (ctx->ump_bytes && ctx->ump_bytes <= count) {
1187 size = ctx->ump_bytes;
1188 memcpy(buffer, ctx->ump, size);
1196 static void process_legacy_input(struct snd_ump_endpoint *ump, const u32 *src,
1199 struct snd_rawmidi_substream *substream;
1200 unsigned char buf[16];
1201 unsigned char group;
1202 const int dir = SNDRV_RAWMIDI_STREAM_INPUT;
1205 size = snd_ump_convert_from_ump(src, buf, &group);
1208 guard(spinlock_irqsave)(&ump->legacy_locks[dir]);
1209 substream = ump->legacy_substreams[dir][group];
1211 snd_rawmidi_receive(substream, buf, size);
1214 /* Fill ump->legacy_mapping[] for groups to be used for legacy rawmidi */
1215 static int fill_legacy_mapping(struct snd_ump_endpoint *ump)
1217 struct snd_ump_block *fb;
1218 unsigned int group_maps = 0;
1221 if (ump->info.flags & SNDRV_UMP_EP_INFO_STATIC_BLOCKS) {
1222 list_for_each_entry(fb, &ump->block_list, list) {
1223 for (i = 0; i < fb->info.num_groups; i++)
1224 group_maps |= 1U << (fb->info.first_group + i);
1227 ump_info(ump, "No UMP Group is found in FB\n");
1230 /* use all groups for non-static case */
1232 group_maps = (1U << SNDRV_UMP_MAX_GROUPS) - 1;
1235 for (i = 0; i < SNDRV_UMP_MAX_GROUPS; i++)
1236 if (group_maps & (1U << i))
1237 ump->legacy_mapping[num++] = i;
1242 static void fill_substream_names(struct snd_ump_endpoint *ump,
1243 struct snd_rawmidi *rmidi, int dir)
1245 struct snd_rawmidi_substream *s;
1249 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1250 idx = ump->legacy_mapping[s->number];
1251 name = ump->groups[idx].name;
1253 name = ump->info.name;
1254 snprintf(s->name, sizeof(s->name), "Group %d (%.16s)",
1259 int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump,
1260 char *id, int device)
1262 struct snd_rawmidi *rmidi;
1266 ump->out_cvts = kcalloc(SNDRV_UMP_MAX_GROUPS,
1267 sizeof(*ump->out_cvts), GFP_KERNEL);
1271 num = fill_legacy_mapping(ump);
1273 input = ump->core.info_flags & SNDRV_RAWMIDI_INFO_INPUT;
1274 output = ump->core.info_flags & SNDRV_RAWMIDI_INFO_OUTPUT;
1275 err = snd_rawmidi_new(ump->core.card, id, device,
1276 output ? num : 0, input ? num : 0,
1279 kfree(ump->out_cvts);
1284 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
1285 &snd_ump_legacy_input_ops);
1287 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
1288 &snd_ump_legacy_output_ops);
1289 snprintf(rmidi->name, sizeof(rmidi->name), "%.68s (MIDI 1.0)",
1291 rmidi->info_flags = ump->core.info_flags & ~SNDRV_RAWMIDI_INFO_UMP;
1292 rmidi->ops = &snd_ump_legacy_ops;
1293 rmidi->private_data = ump;
1294 ump->legacy_rmidi = rmidi;
1296 fill_substream_names(ump, rmidi, SNDRV_RAWMIDI_STREAM_INPUT);
1298 fill_substream_names(ump, rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT);
1300 ump_dbg(ump, "Created a legacy rawmidi #%d (%s)\n", device, id);
1303 EXPORT_SYMBOL_GPL(snd_ump_attach_legacy_rawmidi);
1304 #endif /* CONFIG_SND_UMP_LEGACY_RAWMIDI */
1306 MODULE_DESCRIPTION("Universal MIDI Packet (UMP) Core Driver");
1307 MODULE_LICENSE("GPL");