xref: /openbmc/linux/sound/core/seq/seq_ump_convert.c (revision 6548d543)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ALSA sequencer event conversion between UMP and legacy clients
4  */
5 
6 #include <linux/init.h>
7 #include <linux/errno.h>
8 #include <linux/string.h>
9 #include <sound/core.h>
10 #include <sound/ump.h>
11 #include <sound/ump_msg.h>
12 #include "seq_ump_convert.h"
13 
14 /*
15  * Upgrade / downgrade value bits
16  */
17 static u8 downscale_32_to_7bit(u32 src)
18 {
19 	return src >> 25;
20 }
21 
22 static u16 downscale_32_to_14bit(u32 src)
23 {
24 	return src >> 18;
25 }
26 
27 static u8 downscale_16_to_7bit(u16 src)
28 {
29 	return src >> 9;
30 }
31 
32 static u16 upscale_7_to_16bit(u8 src)
33 {
34 	u16 val, repeat;
35 
36 	val = (u16)src << 9;
37 	if (src <= 0x40)
38 		return val;
39 	repeat = src & 0x3f;
40 	return val | (repeat << 3) | (repeat >> 3);
41 }
42 
43 static u32 upscale_7_to_32bit(u8 src)
44 {
45 	u32 val, repeat;
46 
47 	val = src << 25;
48 	if (src <= 0x40)
49 		return val;
50 	repeat = src & 0x3f;
51 	return val | (repeat << 19) | (repeat << 13) |
52 		(repeat << 7) | (repeat << 1) | (repeat >> 5);
53 }
54 
55 static u32 upscale_14_to_32bit(u16 src)
56 {
57 	u32 val, repeat;
58 
59 	val = src << 18;
60 	if (src <= 0x2000)
61 		return val;
62 	repeat = src & 0x1fff;
63 	return val | (repeat << 5) | (repeat >> 8);
64 }
65 
66 static unsigned char get_ump_group(struct snd_seq_client_port *port)
67 {
68 	return port->ump_group ? (port->ump_group - 1) : 0;
69 }
70 
71 /* create a UMP header */
72 #define make_raw_ump(port, type) \
73 	ump_compose(type, get_ump_group(port), 0, 0)
74 
75 /*
76  * UMP -> MIDI1 sequencer event
77  */
78 
79 /* MIDI 1.0 CVM */
80 
81 /* encode note event */
82 static void ump_midi1_to_note_ev(const union snd_ump_midi1_msg *val,
83 				 struct snd_seq_event *ev)
84 {
85 	ev->data.note.channel = val->note.channel;
86 	ev->data.note.note = val->note.note;
87 	ev->data.note.velocity = val->note.velocity;
88 }
89 
90 /* encode one parameter controls */
91 static void ump_midi1_to_ctrl_ev(const union snd_ump_midi1_msg *val,
92 				 struct snd_seq_event *ev)
93 {
94 	ev->data.control.channel = val->caf.channel;
95 	ev->data.control.value = val->caf.data;
96 }
97 
98 /* encode pitch wheel change */
99 static void ump_midi1_to_pitchbend_ev(const union snd_ump_midi1_msg *val,
100 				      struct snd_seq_event *ev)
101 {
102 	ev->data.control.channel = val->pb.channel;
103 	ev->data.control.value = (val->pb.data_msb << 7) | val->pb.data_lsb;
104 	ev->data.control.value -= 8192;
105 }
106 
107 /* encode midi control change */
108 static void ump_midi1_to_cc_ev(const union snd_ump_midi1_msg *val,
109 			       struct snd_seq_event *ev)
110 {
111 	ev->data.control.channel = val->cc.channel;
112 	ev->data.control.param = val->cc.index;
113 	ev->data.control.value = val->cc.data;
114 }
115 
116 /* Encoding MIDI 1.0 UMP packet */
117 struct seq_ump_midi1_to_ev {
118 	int seq_type;
119 	void (*encode)(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev);
120 };
121 
122 /* Encoders for MIDI1 status 0x80-0xe0 */
123 static struct seq_ump_midi1_to_ev midi1_msg_encoders[] = {
124 	{SNDRV_SEQ_EVENT_NOTEOFF,	ump_midi1_to_note_ev},	/* 0x80 */
125 	{SNDRV_SEQ_EVENT_NOTEON,	ump_midi1_to_note_ev},	/* 0x90 */
126 	{SNDRV_SEQ_EVENT_KEYPRESS,	ump_midi1_to_note_ev},	/* 0xa0 */
127 	{SNDRV_SEQ_EVENT_CONTROLLER,	ump_midi1_to_cc_ev},	/* 0xb0 */
128 	{SNDRV_SEQ_EVENT_PGMCHANGE,	ump_midi1_to_ctrl_ev},	/* 0xc0 */
129 	{SNDRV_SEQ_EVENT_CHANPRESS,	ump_midi1_to_ctrl_ev},	/* 0xd0 */
130 	{SNDRV_SEQ_EVENT_PITCHBEND,	ump_midi1_to_pitchbend_ev}, /* 0xe0 */
131 };
132 
133 static int cvt_ump_midi1_to_event(const union snd_ump_midi1_msg *val,
134 				  struct snd_seq_event *ev)
135 {
136 	unsigned char status = val->note.status;
137 
138 	if (status < 0x8 || status > 0xe)
139 		return 0; /* invalid - skip */
140 	status -= 8;
141 	ev->type = midi1_msg_encoders[status].seq_type;
142 	ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
143 	midi1_msg_encoders[status].encode(val, ev);
144 	return 1;
145 }
146 
147 /* MIDI System message */
148 
149 /* encode one parameter value*/
150 static void ump_system_to_one_param_ev(const union snd_ump_midi1_msg *val,
151 				       struct snd_seq_event *ev)
152 {
153 	ev->data.control.value = val->system.parm1;
154 }
155 
156 /* encode song position */
157 static void ump_system_to_songpos_ev(const union snd_ump_midi1_msg *val,
158 				     struct snd_seq_event *ev)
159 {
160 	ev->data.control.value = (val->system.parm2 << 7) | val->system.parm1;
161 }
162 
163 /* Encoders for 0xf0 - 0xff */
164 static struct seq_ump_midi1_to_ev system_msg_encoders[] = {
165 	{SNDRV_SEQ_EVENT_NONE,		NULL},	 /* 0xf0 */
166 	{SNDRV_SEQ_EVENT_QFRAME,	ump_system_to_one_param_ev}, /* 0xf1 */
167 	{SNDRV_SEQ_EVENT_SONGPOS,	ump_system_to_songpos_ev}, /* 0xf2 */
168 	{SNDRV_SEQ_EVENT_SONGSEL,	ump_system_to_one_param_ev}, /* 0xf3 */
169 	{SNDRV_SEQ_EVENT_NONE,		NULL}, /* 0xf4 */
170 	{SNDRV_SEQ_EVENT_NONE,		NULL}, /* 0xf5 */
171 	{SNDRV_SEQ_EVENT_TUNE_REQUEST,	NULL}, /* 0xf6 */
172 	{SNDRV_SEQ_EVENT_NONE,		NULL}, /* 0xf7 */
173 	{SNDRV_SEQ_EVENT_CLOCK,		NULL}, /* 0xf8 */
174 	{SNDRV_SEQ_EVENT_NONE,		NULL}, /* 0xf9 */
175 	{SNDRV_SEQ_EVENT_START,		NULL}, /* 0xfa */
176 	{SNDRV_SEQ_EVENT_CONTINUE,	NULL}, /* 0xfb */
177 	{SNDRV_SEQ_EVENT_STOP,		NULL}, /* 0xfc */
178 	{SNDRV_SEQ_EVENT_NONE,		NULL}, /* 0xfd */
179 	{SNDRV_SEQ_EVENT_SENSING,	NULL}, /* 0xfe */
180 	{SNDRV_SEQ_EVENT_RESET,		NULL}, /* 0xff */
181 };
182 
183 static int cvt_ump_system_to_event(const union snd_ump_midi1_msg *val,
184 				   struct snd_seq_event *ev)
185 {
186 	unsigned char status = val->system.status;
187 
188 	if ((status & 0xf0) != UMP_MIDI1_MSG_REALTIME)
189 		return 0; /* invalid status - skip */
190 	status &= 0x0f;
191 	ev->type = system_msg_encoders[status].seq_type;
192 	ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
193 	if (ev->type == SNDRV_SEQ_EVENT_NONE)
194 		return 0;
195 	if (system_msg_encoders[status].encode)
196 		system_msg_encoders[status].encode(val, ev);
197 	return 1;
198 }
199 
200 /* MIDI 2.0 CVM */
201 
202 /* encode note event */
203 static int ump_midi2_to_note_ev(const union snd_ump_midi2_msg *val,
204 				struct snd_seq_event *ev)
205 {
206 	ev->data.note.channel = val->note.channel;
207 	ev->data.note.note = val->note.note;
208 	ev->data.note.velocity = downscale_16_to_7bit(val->note.velocity);
209 	/* correct note-on velocity 0 to 1;
210 	 * it's no longer equivalent as not-off for MIDI 2.0
211 	 */
212 	if (ev->type == SNDRV_SEQ_EVENT_NOTEON &&
213 	    !ev->data.note.velocity)
214 		ev->data.note.velocity = 1;
215 	return 1;
216 }
217 
218 /* encode pitch wheel change */
219 static int ump_midi2_to_pitchbend_ev(const union snd_ump_midi2_msg *val,
220 				     struct snd_seq_event *ev)
221 {
222 	ev->data.control.channel = val->pb.channel;
223 	ev->data.control.value = downscale_32_to_14bit(val->pb.data);
224 	ev->data.control.value -= 8192;
225 	return 1;
226 }
227 
228 /* encode midi control change */
229 static int ump_midi2_to_cc_ev(const union snd_ump_midi2_msg *val,
230 			      struct snd_seq_event *ev)
231 {
232 	ev->data.control.channel = val->cc.channel;
233 	ev->data.control.param = val->cc.index;
234 	ev->data.control.value = downscale_32_to_7bit(val->cc.data);
235 	return 1;
236 }
237 
238 /* encode midi program change */
239 static int ump_midi2_to_pgm_ev(const union snd_ump_midi2_msg *val,
240 			       struct snd_seq_event *ev)
241 {
242 	int size = 1;
243 
244 	ev->data.control.channel = val->pg.channel;
245 	if (val->pg.bank_valid) {
246 		ev->type = SNDRV_SEQ_EVENT_CONTROL14;
247 		ev->data.control.param = UMP_CC_BANK_SELECT;
248 		ev->data.control.value = (val->pg.bank_msb << 7) | val->pg.bank_lsb;
249 		ev[1] = ev[0];
250 		ev++;
251 		ev->type = SNDRV_SEQ_EVENT_PGMCHANGE;
252 		size = 2;
253 	}
254 	ev->data.control.value = val->pg.program;
255 	return size;
256 }
257 
258 /* encode one parameter controls */
259 static int ump_midi2_to_ctrl_ev(const union snd_ump_midi2_msg *val,
260 				struct snd_seq_event *ev)
261 {
262 	ev->data.control.channel = val->caf.channel;
263 	ev->data.control.value = downscale_32_to_7bit(val->caf.data);
264 	return 1;
265 }
266 
267 /* encode RPN/NRPN */
268 static int ump_midi2_to_rpn_ev(const union snd_ump_midi2_msg *val,
269 			       struct snd_seq_event *ev)
270 {
271 	ev->data.control.channel = val->rpn.channel;
272 	ev->data.control.param = (val->rpn.bank << 7) | val->rpn.index;
273 	ev->data.control.value = downscale_32_to_14bit(val->rpn.data);
274 	return 1;
275 }
276 
277 /* Encoding MIDI 2.0 UMP Packet */
278 struct seq_ump_midi2_to_ev {
279 	int seq_type;
280 	int (*encode)(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev);
281 };
282 
283 /* Encoders for MIDI2 status 0x00-0xf0 */
284 static struct seq_ump_midi2_to_ev midi2_msg_encoders[] = {
285 	{SNDRV_SEQ_EVENT_NONE,		NULL},			/* 0x00 */
286 	{SNDRV_SEQ_EVENT_NONE,		NULL},			/* 0x10 */
287 	{SNDRV_SEQ_EVENT_REGPARAM,	ump_midi2_to_rpn_ev},	/* 0x20 */
288 	{SNDRV_SEQ_EVENT_NONREGPARAM,	ump_midi2_to_rpn_ev},	/* 0x30 */
289 	{SNDRV_SEQ_EVENT_NONE,		NULL},			/* 0x40 */
290 	{SNDRV_SEQ_EVENT_NONE,		NULL},			/* 0x50 */
291 	{SNDRV_SEQ_EVENT_NONE,		NULL},			/* 0x60 */
292 	{SNDRV_SEQ_EVENT_NONE,		NULL},			/* 0x70 */
293 	{SNDRV_SEQ_EVENT_NOTEOFF,	ump_midi2_to_note_ev},	/* 0x80 */
294 	{SNDRV_SEQ_EVENT_NOTEON,	ump_midi2_to_note_ev},	/* 0x90 */
295 	{SNDRV_SEQ_EVENT_KEYPRESS,	ump_midi2_to_note_ev},	/* 0xa0 */
296 	{SNDRV_SEQ_EVENT_CONTROLLER,	ump_midi2_to_cc_ev},	/* 0xb0 */
297 	{SNDRV_SEQ_EVENT_PGMCHANGE,	ump_midi2_to_pgm_ev},	/* 0xc0 */
298 	{SNDRV_SEQ_EVENT_CHANPRESS,	ump_midi2_to_ctrl_ev},	/* 0xd0 */
299 	{SNDRV_SEQ_EVENT_PITCHBEND,	ump_midi2_to_pitchbend_ev}, /* 0xe0 */
300 	{SNDRV_SEQ_EVENT_NONE,		NULL},			/* 0xf0 */
301 };
302 
303 static int cvt_ump_midi2_to_event(const union snd_ump_midi2_msg *val,
304 				  struct snd_seq_event *ev)
305 {
306 	unsigned char status = val->note.status;
307 
308 	ev->type = midi2_msg_encoders[status].seq_type;
309 	if (ev->type == SNDRV_SEQ_EVENT_NONE)
310 		return 0; /* skip */
311 	ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
312 	return midi2_msg_encoders[status].encode(val, ev);
313 }
314 
315 /* parse and compose for a sysex var-length event */
316 static int cvt_ump_sysex7_to_event(const u32 *data, unsigned char *buf,
317 				   struct snd_seq_event *ev)
318 {
319 	unsigned char status;
320 	unsigned char bytes;
321 	u32 val;
322 	int size = 0;
323 
324 	val = data[0];
325 	status = ump_sysex_message_status(val);
326 	bytes = ump_sysex_message_length(val);
327 	if (bytes > 6)
328 		return 0; // skip
329 
330 	if (status == UMP_SYSEX_STATUS_SINGLE ||
331 	    status == UMP_SYSEX_STATUS_START) {
332 		buf[0] = UMP_MIDI1_MSG_SYSEX_START;
333 		size = 1;
334 	}
335 
336 	if (bytes > 0)
337 		buf[size++] = (val >> 8) & 0x7f;
338 	if (bytes > 1)
339 		buf[size++] = val & 0x7f;
340 	val = data[1];
341 	if (bytes > 2)
342 		buf[size++] = (val >> 24) & 0x7f;
343 	if (bytes > 3)
344 		buf[size++] = (val >> 16) & 0x7f;
345 	if (bytes > 4)
346 		buf[size++] = (val >> 8) & 0x7f;
347 	if (bytes > 5)
348 		buf[size++] = val & 0x7f;
349 
350 	if (status == UMP_SYSEX_STATUS_SINGLE ||
351 	    status == UMP_SYSEX_STATUS_END)
352 		buf[size++] = UMP_MIDI1_MSG_SYSEX_END;
353 
354 	ev->type = SNDRV_SEQ_EVENT_SYSEX;
355 	ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
356 	ev->data.ext.len = size;
357 	ev->data.ext.ptr = buf;
358 	return 1;
359 }
360 
361 /* convert UMP packet from MIDI 1.0 to MIDI 2.0 and deliver it */
362 static int cvt_ump_midi1_to_midi2(struct snd_seq_client *dest,
363 				  struct snd_seq_client_port *dest_port,
364 				  struct snd_seq_event *__event,
365 				  int atomic, int hop)
366 {
367 	struct snd_seq_ump_event *event = (struct snd_seq_ump_event *)__event;
368 	struct snd_seq_ump_event ev_cvt;
369 	const union snd_ump_midi1_msg *midi1 = (const union snd_ump_midi1_msg *)event->ump;
370 	union snd_ump_midi2_msg *midi2 = (union snd_ump_midi2_msg *)ev_cvt.ump;
371 	struct snd_seq_ump_midi2_bank *cc;
372 
373 	ev_cvt = *event;
374 	memset(&ev_cvt.ump, 0, sizeof(ev_cvt.ump));
375 
376 	midi2->note.type = UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE;
377 	midi2->note.group = midi1->note.group;
378 	midi2->note.status = midi1->note.status;
379 	midi2->note.channel = midi1->note.channel;
380 	switch (midi1->note.status) {
381 	case UMP_MSG_STATUS_NOTE_ON:
382 	case UMP_MSG_STATUS_NOTE_OFF:
383 		midi2->note.note = midi1->note.note;
384 		midi2->note.velocity = upscale_7_to_16bit(midi1->note.velocity);
385 		break;
386 	case UMP_MSG_STATUS_POLY_PRESSURE:
387 		midi2->paf.note = midi1->paf.note;
388 		midi2->paf.data = upscale_7_to_32bit(midi1->paf.data);
389 		break;
390 	case UMP_MSG_STATUS_CC:
391 		cc = &dest_port->midi2_bank[midi1->note.channel];
392 		switch (midi1->cc.index) {
393 		case UMP_CC_BANK_SELECT:
394 			cc->bank_set = 1;
395 			cc->cc_bank_msb = midi1->cc.data;
396 			return 0; // skip
397 		case UMP_CC_BANK_SELECT_LSB:
398 			cc->bank_set = 1;
399 			cc->cc_bank_lsb = midi1->cc.data;
400 			return 0; // skip
401 		}
402 		midi2->cc.index = midi1->cc.index;
403 		midi2->cc.data = upscale_7_to_32bit(midi1->cc.data);
404 		break;
405 	case UMP_MSG_STATUS_PROGRAM:
406 		midi2->pg.program = midi1->pg.program;
407 		cc = &dest_port->midi2_bank[midi1->note.channel];
408 		if (cc->bank_set) {
409 			midi2->pg.bank_valid = 1;
410 			midi2->pg.bank_msb = cc->cc_bank_msb;
411 			midi2->pg.bank_lsb = cc->cc_bank_lsb;
412 			cc->bank_set = 0;
413 		}
414 		break;
415 	case UMP_MSG_STATUS_CHANNEL_PRESSURE:
416 		midi2->caf.data = upscale_7_to_32bit(midi1->caf.data);
417 		break;
418 	case UMP_MSG_STATUS_PITCH_BEND:
419 		midi2->pb.data = upscale_14_to_32bit((midi1->pb.data_msb << 7) |
420 						     midi1->pb.data_lsb);
421 		break;
422 	default:
423 		return 0;
424 	}
425 
426 	return __snd_seq_deliver_single_event(dest, dest_port,
427 					      (struct snd_seq_event *)&ev_cvt,
428 					      atomic, hop);
429 }
430 
431 /* convert UMP packet from MIDI 2.0 to MIDI 1.0 and deliver it */
432 static int cvt_ump_midi2_to_midi1(struct snd_seq_client *dest,
433 				  struct snd_seq_client_port *dest_port,
434 				  struct snd_seq_event *__event,
435 				  int atomic, int hop)
436 {
437 	struct snd_seq_ump_event *event = (struct snd_seq_ump_event *)__event;
438 	struct snd_seq_ump_event ev_cvt;
439 	union snd_ump_midi1_msg *midi1 = (union snd_ump_midi1_msg *)ev_cvt.ump;
440 	const union snd_ump_midi2_msg *midi2 = (const union snd_ump_midi2_msg *)event->ump;
441 	int err;
442 	u16 v;
443 
444 	ev_cvt = *event;
445 	memset(&ev_cvt.ump, 0, sizeof(ev_cvt.ump));
446 
447 	midi1->note.type = UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE;
448 	midi1->note.group = midi2->note.group;
449 	midi1->note.status = midi2->note.status;
450 	midi1->note.channel = midi2->note.channel;
451 	switch (midi2->note.status) {
452 	case UMP_MSG_STATUS_NOTE_ON:
453 	case UMP_MSG_STATUS_NOTE_OFF:
454 		midi1->note.note = midi2->note.note;
455 		midi1->note.velocity = downscale_16_to_7bit(midi2->note.velocity);
456 		break;
457 	case UMP_MSG_STATUS_POLY_PRESSURE:
458 		midi1->paf.note = midi2->paf.note;
459 		midi1->paf.data = downscale_32_to_7bit(midi2->paf.data);
460 		break;
461 	case UMP_MSG_STATUS_CC:
462 		midi1->cc.index = midi2->cc.index;
463 		midi1->cc.data = downscale_32_to_7bit(midi2->cc.data);
464 		break;
465 	case UMP_MSG_STATUS_PROGRAM:
466 		if (midi2->pg.bank_valid) {
467 			midi1->cc.status = UMP_MSG_STATUS_CC;
468 			midi1->cc.index = UMP_CC_BANK_SELECT;
469 			midi1->cc.data = midi2->pg.bank_msb;
470 			err = __snd_seq_deliver_single_event(dest, dest_port,
471 							     (struct snd_seq_event *)&ev_cvt,
472 							     atomic, hop);
473 			if (err < 0)
474 				return err;
475 			midi1->cc.index = UMP_CC_BANK_SELECT_LSB;
476 			midi1->cc.data = midi2->pg.bank_lsb;
477 			err = __snd_seq_deliver_single_event(dest, dest_port,
478 							     (struct snd_seq_event *)&ev_cvt,
479 							     atomic, hop);
480 			if (err < 0)
481 				return err;
482 			midi1->note.status = midi2->note.status;
483 		}
484 		midi1->pg.program = midi2->pg.program;
485 		break;
486 	case UMP_MSG_STATUS_CHANNEL_PRESSURE:
487 		midi1->caf.data = downscale_32_to_7bit(midi2->caf.data);
488 		break;
489 	case UMP_MSG_STATUS_PITCH_BEND:
490 		v = downscale_32_to_14bit(midi2->pb.data);
491 		midi1->pb.data_msb = v >> 7;
492 		midi1->pb.data_lsb = v & 0x7f;
493 		break;
494 	default:
495 		return 0;
496 	}
497 
498 	return __snd_seq_deliver_single_event(dest, dest_port,
499 					      (struct snd_seq_event *)&ev_cvt,
500 					      atomic, hop);
501 }
502 
503 /* convert UMP to a legacy ALSA seq event and deliver it */
504 static int cvt_ump_to_any(struct snd_seq_client *dest,
505 			  struct snd_seq_client_port *dest_port,
506 			  struct snd_seq_event *event,
507 			  unsigned char type,
508 			  int atomic, int hop)
509 {
510 	struct snd_seq_event ev_cvt[2]; /* up to two events */
511 	struct snd_seq_ump_event *ump_ev = (struct snd_seq_ump_event *)event;
512 	/* use the second event as a temp buffer for saving stack usage */
513 	unsigned char *sysex_buf = (unsigned char *)(ev_cvt + 1);
514 	unsigned char flags = event->flags & ~SNDRV_SEQ_EVENT_UMP;
515 	int i, len, err;
516 
517 	ev_cvt[0] = ev_cvt[1] = *event;
518 	ev_cvt[0].flags = flags;
519 	ev_cvt[1].flags = flags;
520 	switch (type) {
521 	case UMP_MSG_TYPE_SYSTEM:
522 		len = cvt_ump_system_to_event((union snd_ump_midi1_msg *)ump_ev->ump,
523 					      ev_cvt);
524 		break;
525 	case UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE:
526 		len = cvt_ump_midi1_to_event((union snd_ump_midi1_msg *)ump_ev->ump,
527 					     ev_cvt);
528 		break;
529 	case UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE:
530 		len = cvt_ump_midi2_to_event((union snd_ump_midi2_msg *)ump_ev->ump,
531 					     ev_cvt);
532 		break;
533 	case UMP_MSG_TYPE_DATA:
534 		len = cvt_ump_sysex7_to_event(ump_ev->ump, sysex_buf, ev_cvt);
535 		break;
536 	default:
537 		return 0;
538 	}
539 
540 	for (i = 0; i < len; i++) {
541 		err = __snd_seq_deliver_single_event(dest, dest_port,
542 						     &ev_cvt[i], atomic, hop);
543 		if (err < 0)
544 			return err;
545 	}
546 
547 	return 0;
548 }
549 
550 /* Replace UMP group field with the destination and deliver */
551 static int deliver_with_group_convert(struct snd_seq_client *dest,
552 				      struct snd_seq_client_port *dest_port,
553 				      struct snd_seq_ump_event *ump_ev,
554 				      int atomic, int hop)
555 {
556 	struct snd_seq_ump_event ev = *ump_ev;
557 
558 	/* rewrite the group to the destination port */
559 	ev.ump[0] &= ~(0xfU << 24);
560 	/* fill with the new group; the dest_port->ump_group field is 1-based */
561 	ev.ump[0] |= ((dest_port->ump_group - 1) << 24);
562 
563 	return __snd_seq_deliver_single_event(dest, dest_port,
564 					      (struct snd_seq_event *)&ev,
565 					      atomic, hop);
566 }
567 
568 /* apply the UMP event filter; return true to skip the event */
569 static bool ump_event_filtered(struct snd_seq_client *dest,
570 			       const struct snd_seq_ump_event *ev)
571 {
572 	unsigned char group;
573 
574 	group = ump_message_group(ev->ump[0]);
575 	if (ump_is_groupless_msg(ump_message_type(ev->ump[0])))
576 		return dest->group_filter & (1U << 0);
577 	/* check the bitmap for 1-based group number */
578 	return dest->group_filter & (1U << (group + 1));
579 }
580 
581 /* Convert from UMP packet and deliver */
582 int snd_seq_deliver_from_ump(struct snd_seq_client *source,
583 			     struct snd_seq_client *dest,
584 			     struct snd_seq_client_port *dest_port,
585 			     struct snd_seq_event *event,
586 			     int atomic, int hop)
587 {
588 	struct snd_seq_ump_event *ump_ev = (struct snd_seq_ump_event *)event;
589 	unsigned char type;
590 
591 	if (snd_seq_ev_is_variable(event))
592 		return 0; // skip, no variable event for UMP, so far
593 	if (ump_event_filtered(dest, ump_ev))
594 		return 0; // skip if group filter is set and matching
595 	type = ump_message_type(ump_ev->ump[0]);
596 
597 	if (snd_seq_client_is_ump(dest)) {
598 		if (snd_seq_client_is_midi2(dest) &&
599 		    type == UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE)
600 			return cvt_ump_midi1_to_midi2(dest, dest_port,
601 						      event, atomic, hop);
602 		else if (!snd_seq_client_is_midi2(dest) &&
603 			 type == UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE)
604 			return cvt_ump_midi2_to_midi1(dest, dest_port,
605 						      event, atomic, hop);
606 		/* non-EP port and different group is set? */
607 		if (dest_port->ump_group &&
608 		    !ump_is_groupless_msg(type) &&
609 		    ump_message_group(*ump_ev->ump) + 1 != dest_port->ump_group)
610 			return deliver_with_group_convert(dest, dest_port,
611 							  ump_ev, atomic, hop);
612 		/* copy as-is */
613 		return __snd_seq_deliver_single_event(dest, dest_port,
614 						      event, atomic, hop);
615 	}
616 
617 	return cvt_ump_to_any(dest, dest_port, event, type, atomic, hop);
618 }
619 
620 /*
621  * MIDI1 sequencer event -> UMP conversion
622  */
623 
624 /* Conversion to UMP MIDI 1.0 */
625 
626 /* convert note on/off event to MIDI 1.0 UMP */
627 static int note_ev_to_ump_midi1(const struct snd_seq_event *event,
628 				struct snd_seq_client_port *dest_port,
629 				union snd_ump_midi1_msg *data,
630 				unsigned char status)
631 {
632 	if (!event->data.note.velocity)
633 		status = UMP_MSG_STATUS_NOTE_OFF;
634 	data->note.status = status;
635 	data->note.channel = event->data.note.channel & 0x0f;
636 	data->note.velocity = event->data.note.velocity & 0x7f;
637 	data->note.note = event->data.note.note & 0x7f;
638 	return 1;
639 }
640 
641 /* convert CC event to MIDI 1.0 UMP */
642 static int cc_ev_to_ump_midi1(const struct snd_seq_event *event,
643 			      struct snd_seq_client_port *dest_port,
644 			      union snd_ump_midi1_msg *data,
645 			      unsigned char status)
646 {
647 	data->cc.status = status;
648 	data->cc.channel = event->data.control.channel & 0x0f;
649 	data->cc.index = event->data.control.param;
650 	data->cc.data = event->data.control.value;
651 	return 1;
652 }
653 
654 /* convert one-parameter control event to MIDI 1.0 UMP */
655 static int ctrl_ev_to_ump_midi1(const struct snd_seq_event *event,
656 				struct snd_seq_client_port *dest_port,
657 				union snd_ump_midi1_msg *data,
658 				unsigned char status)
659 {
660 	data->caf.status = status;
661 	data->caf.channel = event->data.control.channel & 0x0f;
662 	data->caf.data = event->data.control.value & 0x7f;
663 	return 1;
664 }
665 
666 /* convert pitchbend event to MIDI 1.0 UMP */
667 static int pitchbend_ev_to_ump_midi1(const struct snd_seq_event *event,
668 				     struct snd_seq_client_port *dest_port,
669 				     union snd_ump_midi1_msg *data,
670 				     unsigned char status)
671 {
672 	int val = event->data.control.value + 8192;
673 
674 	val = clamp(val, 0, 0x3fff);
675 	data->pb.status = status;
676 	data->pb.channel = event->data.control.channel & 0x0f;
677 	data->pb.data_msb = (val >> 7) & 0x7f;
678 	data->pb.data_lsb = val & 0x7f;
679 	return 1;
680 }
681 
682 /* convert 14bit control event to MIDI 1.0 UMP; split to two events */
683 static int ctrl14_ev_to_ump_midi1(const struct snd_seq_event *event,
684 				  struct snd_seq_client_port *dest_port,
685 				  union snd_ump_midi1_msg *data,
686 				  unsigned char status)
687 {
688 	data->cc.status = UMP_MSG_STATUS_CC;
689 	data->cc.channel = event->data.control.channel & 0x0f;
690 	data->cc.index = event->data.control.param & 0x7f;
691 	if (event->data.control.param < 0x20) {
692 		data->cc.data = (event->data.control.value >> 7) & 0x7f;
693 		data[1] = data[0];
694 		data[1].cc.index = event->data.control.param | 0x20;
695 		data[1].cc.data = event->data.control.value & 0x7f;
696 		return 2;
697 	}
698 
699 	data->cc.data = event->data.control.value & 0x7f;
700 	return 1;
701 }
702 
703 /* convert RPN/NRPN event to MIDI 1.0 UMP; split to four events */
704 static int rpn_ev_to_ump_midi1(const struct snd_seq_event *event,
705 			       struct snd_seq_client_port *dest_port,
706 			       union snd_ump_midi1_msg *data,
707 			       unsigned char status)
708 {
709 	bool is_rpn = (status == UMP_MSG_STATUS_RPN);
710 
711 	data->cc.status = UMP_MSG_STATUS_CC;
712 	data->cc.channel = event->data.control.channel & 0x0f;
713 	data[1] = data[2] = data[3] = data[0];
714 
715 	data[0].cc.index = is_rpn ? UMP_CC_RPN_MSB : UMP_CC_NRPN_MSB;
716 	data[0].cc.data = (event->data.control.param >> 7) & 0x7f;
717 	data[1].cc.index = is_rpn ? UMP_CC_RPN_LSB : UMP_CC_NRPN_LSB;
718 	data[1].cc.data = event->data.control.param & 0x7f;
719 	data[2].cc.index = UMP_CC_DATA;
720 	data[2].cc.data = (event->data.control.value >> 7) & 0x7f;
721 	data[3].cc.index = UMP_CC_DATA_LSB;
722 	data[3].cc.data = event->data.control.value & 0x7f;
723 	return 4;
724 }
725 
726 /* convert system / RT message to UMP */
727 static int system_ev_to_ump_midi1(const struct snd_seq_event *event,
728 				  struct snd_seq_client_port *dest_port,
729 				  union snd_ump_midi1_msg *data,
730 				  unsigned char status)
731 {
732 	data->system.type = UMP_MSG_TYPE_SYSTEM; // override
733 	data->system.status = status;
734 	return 1;
735 }
736 
737 /* convert system / RT message with 1 parameter to UMP */
738 static int system_1p_ev_to_ump_midi1(const struct snd_seq_event *event,
739 				     struct snd_seq_client_port *dest_port,
740 				     union snd_ump_midi1_msg *data,
741 				     unsigned char status)
742 {
743 	data->system.type = UMP_MSG_TYPE_SYSTEM; // override
744 	data->system.status = status;
745 	data->system.parm1 = event->data.control.value & 0x7f;
746 	return 1;
747 }
748 
749 /* convert system / RT message with two parameters to UMP */
750 static int system_2p_ev_to_ump_midi1(const struct snd_seq_event *event,
751 				     struct snd_seq_client_port *dest_port,
752 				     union snd_ump_midi1_msg *data,
753 				     unsigned char status)
754 {
755 	data->system.type = UMP_MSG_TYPE_SYSTEM; // override
756 	data->system.status = status;
757 	data->system.parm1 = event->data.control.value & 0x7f;
758 	data->system.parm2 = (event->data.control.value >> 7) & 0x7f;
759 	return 1;
760 }
761 
762 /* Conversion to UMP MIDI 2.0 */
763 
764 /* convert note on/off event to MIDI 2.0 UMP */
765 static int note_ev_to_ump_midi2(const struct snd_seq_event *event,
766 				struct snd_seq_client_port *dest_port,
767 				union snd_ump_midi2_msg *data,
768 				unsigned char status)
769 {
770 	if (!event->data.note.velocity)
771 		status = UMP_MSG_STATUS_NOTE_OFF;
772 	data->note.status = status;
773 	data->note.channel = event->data.note.channel & 0x0f;
774 	data->note.note = event->data.note.note & 0x7f;
775 	data->note.velocity = upscale_7_to_16bit(event->data.note.velocity & 0x7f);
776 	return 1;
777 }
778 
779 /* convert PAF event to MIDI 2.0 UMP */
780 static int paf_ev_to_ump_midi2(const struct snd_seq_event *event,
781 			       struct snd_seq_client_port *dest_port,
782 			       union snd_ump_midi2_msg *data,
783 			       unsigned char status)
784 {
785 	data->paf.status = status;
786 	data->paf.channel = event->data.note.channel & 0x0f;
787 	data->paf.note = event->data.note.note & 0x7f;
788 	data->paf.data = upscale_7_to_32bit(event->data.note.velocity & 0x7f);
789 	return 1;
790 }
791 
792 /* set up the MIDI2 RPN/NRPN packet data from the parsed info */
793 static void fill_rpn(struct snd_seq_ump_midi2_bank *cc,
794 		     union snd_ump_midi2_msg *data)
795 {
796 	if (cc->rpn_set) {
797 		data->rpn.status = UMP_MSG_STATUS_RPN;
798 		data->rpn.bank = cc->cc_rpn_msb;
799 		data->rpn.index = cc->cc_rpn_lsb;
800 		cc->rpn_set = 0;
801 		cc->cc_rpn_msb = cc->cc_rpn_lsb = 0;
802 	} else {
803 		data->rpn.status = UMP_MSG_STATUS_NRPN;
804 		data->rpn.bank = cc->cc_nrpn_msb;
805 		data->rpn.index = cc->cc_nrpn_lsb;
806 		cc->nrpn_set = 0;
807 		cc->cc_nrpn_msb = cc->cc_nrpn_lsb = 0;
808 	}
809 	data->rpn.data = upscale_14_to_32bit((cc->cc_data_msb << 7) |
810 					     cc->cc_data_lsb);
811 	cc->cc_data_msb = cc->cc_data_lsb = 0;
812 }
813 
814 /* convert CC event to MIDI 2.0 UMP */
815 static int cc_ev_to_ump_midi2(const struct snd_seq_event *event,
816 			      struct snd_seq_client_port *dest_port,
817 			      union snd_ump_midi2_msg *data,
818 			      unsigned char status)
819 {
820 	unsigned char channel = event->data.control.channel & 0x0f;
821 	unsigned char index = event->data.control.param & 0x7f;
822 	unsigned char val = event->data.control.value & 0x7f;
823 	struct snd_seq_ump_midi2_bank *cc = &dest_port->midi2_bank[channel];
824 
825 	/* process special CC's (bank/rpn/nrpn) */
826 	switch (index) {
827 	case UMP_CC_RPN_MSB:
828 		cc->rpn_set = 1;
829 		cc->cc_rpn_msb = val;
830 		return 0; // skip
831 	case UMP_CC_RPN_LSB:
832 		cc->rpn_set = 1;
833 		cc->cc_rpn_lsb = val;
834 		return 0; // skip
835 	case UMP_CC_NRPN_MSB:
836 		cc->nrpn_set = 1;
837 		cc->cc_nrpn_msb = val;
838 		return 0; // skip
839 	case UMP_CC_NRPN_LSB:
840 		cc->nrpn_set = 1;
841 		cc->cc_nrpn_lsb = val;
842 		return 0; // skip
843 	case UMP_CC_DATA:
844 		cc->cc_data_msb = val;
845 		return 0; // skip
846 	case UMP_CC_BANK_SELECT:
847 		cc->bank_set = 1;
848 		cc->cc_bank_msb = val;
849 		return 0; // skip
850 	case UMP_CC_BANK_SELECT_LSB:
851 		cc->bank_set = 1;
852 		cc->cc_bank_lsb = val;
853 		return 0; // skip
854 	case UMP_CC_DATA_LSB:
855 		cc->cc_data_lsb = val;
856 		if (!(cc->rpn_set || cc->nrpn_set))
857 			return 0; // skip
858 		fill_rpn(cc, data);
859 		return 1;
860 	}
861 
862 	data->cc.status = status;
863 	data->cc.channel = channel;
864 	data->cc.index = index;
865 	data->cc.data = upscale_7_to_32bit(event->data.control.value & 0x7f);
866 	return 1;
867 }
868 
869 /* convert one-parameter control event to MIDI 2.0 UMP */
870 static int ctrl_ev_to_ump_midi2(const struct snd_seq_event *event,
871 				struct snd_seq_client_port *dest_port,
872 				union snd_ump_midi2_msg *data,
873 				unsigned char status)
874 {
875 	data->caf.status = status;
876 	data->caf.channel = event->data.control.channel & 0x0f;
877 	data->caf.data = upscale_7_to_32bit(event->data.control.value & 0x7f);
878 	return 1;
879 }
880 
881 /* convert program change event to MIDI 2.0 UMP */
882 static int pgm_ev_to_ump_midi2(const struct snd_seq_event *event,
883 			       struct snd_seq_client_port *dest_port,
884 			       union snd_ump_midi2_msg *data,
885 			       unsigned char status)
886 {
887 	unsigned char channel = event->data.control.channel & 0x0f;
888 	struct snd_seq_ump_midi2_bank *cc = &dest_port->midi2_bank[channel];
889 
890 	data->pg.status = status;
891 	data->pg.channel = channel;
892 	data->pg.program = event->data.control.value & 0x7f;
893 	if (cc->bank_set) {
894 		data->pg.bank_valid = 1;
895 		data->pg.bank_msb = cc->cc_bank_msb;
896 		data->pg.bank_lsb = cc->cc_bank_lsb;
897 		cc->bank_set = 0;
898 	}
899 	return 1;
900 }
901 
902 /* convert pitchbend event to MIDI 2.0 UMP */
903 static int pitchbend_ev_to_ump_midi2(const struct snd_seq_event *event,
904 				     struct snd_seq_client_port *dest_port,
905 				     union snd_ump_midi2_msg *data,
906 				     unsigned char status)
907 {
908 	int val = event->data.control.value + 8192;
909 
910 	val = clamp(val, 0, 0x3fff);
911 	data->pb.status = status;
912 	data->pb.channel = event->data.control.channel & 0x0f;
913 	data->pb.data = upscale_14_to_32bit(val);
914 	return 1;
915 }
916 
917 /* convert 14bit control event to MIDI 2.0 UMP; split to two events */
918 static int ctrl14_ev_to_ump_midi2(const struct snd_seq_event *event,
919 				  struct snd_seq_client_port *dest_port,
920 				  union snd_ump_midi2_msg *data,
921 				  unsigned char status)
922 {
923 	unsigned char channel = event->data.control.channel & 0x0f;
924 	unsigned char index = event->data.control.param & 0x7f;
925 	struct snd_seq_ump_midi2_bank *cc = &dest_port->midi2_bank[channel];
926 	unsigned char msb, lsb;
927 
928 	msb = (event->data.control.value >> 7) & 0x7f;
929 	lsb = event->data.control.value & 0x7f;
930 	/* process special CC's (bank/rpn/nrpn) */
931 	switch (index) {
932 	case UMP_CC_BANK_SELECT:
933 		cc->cc_bank_msb = msb;
934 		fallthrough;
935 	case UMP_CC_BANK_SELECT_LSB:
936 		cc->bank_set = 1;
937 		cc->cc_bank_lsb = lsb;
938 		return 0; // skip
939 	case UMP_CC_RPN_MSB:
940 		cc->cc_rpn_msb = msb;
941 		fallthrough;
942 	case UMP_CC_RPN_LSB:
943 		cc->rpn_set = 1;
944 		cc->cc_rpn_lsb = lsb;
945 		return 0; // skip
946 	case UMP_CC_NRPN_MSB:
947 		cc->cc_nrpn_msb = msb;
948 		fallthrough;
949 	case UMP_CC_NRPN_LSB:
950 		cc->nrpn_set = 1;
951 		cc->cc_nrpn_lsb = lsb;
952 		return 0; // skip
953 	case UMP_CC_DATA:
954 		cc->cc_data_msb = msb;
955 		fallthrough;
956 	case UMP_CC_DATA_LSB:
957 		cc->cc_data_lsb = lsb;
958 		if (!(cc->rpn_set || cc->nrpn_set))
959 			return 0; // skip
960 		fill_rpn(cc, data);
961 		return 1;
962 	}
963 
964 	data->cc.status = UMP_MSG_STATUS_CC;
965 	data->cc.channel = channel;
966 	data->cc.index = index;
967 	if (event->data.control.param < 0x20) {
968 		data->cc.data = upscale_7_to_32bit(msb);
969 		data[1] = data[0];
970 		data[1].cc.index = event->data.control.param | 0x20;
971 		data[1].cc.data = upscale_7_to_32bit(lsb);
972 		return 2;
973 	}
974 
975 	data->cc.data = upscale_7_to_32bit(lsb);
976 	return 1;
977 }
978 
979 /* convert RPN/NRPN event to MIDI 2.0 UMP */
980 static int rpn_ev_to_ump_midi2(const struct snd_seq_event *event,
981 			       struct snd_seq_client_port *dest_port,
982 			       union snd_ump_midi2_msg *data,
983 			       unsigned char status)
984 {
985 	data->rpn.status = status;
986 	data->rpn.channel = event->data.control.channel;
987 	data->rpn.bank = (event->data.control.param >> 7) & 0x7f;
988 	data->rpn.index = event->data.control.param & 0x7f;
989 	data->rpn.data = upscale_14_to_32bit(event->data.control.value & 0x3fff);
990 	return 1;
991 }
992 
993 /* convert system / RT message to UMP */
994 static int system_ev_to_ump_midi2(const struct snd_seq_event *event,
995 				  struct snd_seq_client_port *dest_port,
996 				  union snd_ump_midi2_msg *data,
997 				  unsigned char status)
998 {
999 	return system_ev_to_ump_midi1(event, dest_port,
1000 				      (union snd_ump_midi1_msg *)data,
1001 				      status);
1002 }
1003 
1004 /* convert system / RT message with 1 parameter to UMP */
1005 static int system_1p_ev_to_ump_midi2(const struct snd_seq_event *event,
1006 				     struct snd_seq_client_port *dest_port,
1007 				     union snd_ump_midi2_msg *data,
1008 				     unsigned char status)
1009 {
1010 	return system_1p_ev_to_ump_midi1(event, dest_port,
1011 					 (union snd_ump_midi1_msg *)data,
1012 					 status);
1013 }
1014 
1015 /* convert system / RT message with two parameters to UMP */
1016 static int system_2p_ev_to_ump_midi2(const struct snd_seq_event *event,
1017 				     struct snd_seq_client_port *dest_port,
1018 				     union snd_ump_midi2_msg *data,
1019 				     unsigned char status)
1020 {
1021 	return system_1p_ev_to_ump_midi1(event, dest_port,
1022 					 (union snd_ump_midi1_msg *)data,
1023 					 status);
1024 }
1025 
1026 struct seq_ev_to_ump {
1027 	int seq_type;
1028 	unsigned char status;
1029 	int (*midi1_encode)(const struct snd_seq_event *event,
1030 			    struct snd_seq_client_port *dest_port,
1031 			    union snd_ump_midi1_msg *data,
1032 			    unsigned char status);
1033 	int (*midi2_encode)(const struct snd_seq_event *event,
1034 			    struct snd_seq_client_port *dest_port,
1035 			    union snd_ump_midi2_msg *data,
1036 			    unsigned char status);
1037 };
1038 
1039 static const struct seq_ev_to_ump seq_ev_ump_encoders[] = {
1040 	{ SNDRV_SEQ_EVENT_NOTEON, UMP_MSG_STATUS_NOTE_ON,
1041 	  note_ev_to_ump_midi1, note_ev_to_ump_midi2 },
1042 	{ SNDRV_SEQ_EVENT_NOTEOFF, UMP_MSG_STATUS_NOTE_OFF,
1043 	  note_ev_to_ump_midi1, note_ev_to_ump_midi2 },
1044 	{ SNDRV_SEQ_EVENT_KEYPRESS, UMP_MSG_STATUS_POLY_PRESSURE,
1045 	  note_ev_to_ump_midi1, paf_ev_to_ump_midi2 },
1046 	{ SNDRV_SEQ_EVENT_CONTROLLER, UMP_MSG_STATUS_CC,
1047 	  cc_ev_to_ump_midi1, cc_ev_to_ump_midi2 },
1048 	{ SNDRV_SEQ_EVENT_PGMCHANGE, UMP_MSG_STATUS_PROGRAM,
1049 	  ctrl_ev_to_ump_midi1, pgm_ev_to_ump_midi2 },
1050 	{ SNDRV_SEQ_EVENT_CHANPRESS, UMP_MSG_STATUS_CHANNEL_PRESSURE,
1051 	  ctrl_ev_to_ump_midi1, ctrl_ev_to_ump_midi2 },
1052 	{ SNDRV_SEQ_EVENT_PITCHBEND, UMP_MSG_STATUS_PITCH_BEND,
1053 	  pitchbend_ev_to_ump_midi1, pitchbend_ev_to_ump_midi2 },
1054 	{ SNDRV_SEQ_EVENT_CONTROL14, 0,
1055 	  ctrl14_ev_to_ump_midi1, ctrl14_ev_to_ump_midi2 },
1056 	{ SNDRV_SEQ_EVENT_NONREGPARAM, UMP_MSG_STATUS_NRPN,
1057 	  rpn_ev_to_ump_midi1, rpn_ev_to_ump_midi2 },
1058 	{ SNDRV_SEQ_EVENT_REGPARAM, UMP_MSG_STATUS_RPN,
1059 	  rpn_ev_to_ump_midi1, rpn_ev_to_ump_midi2 },
1060 	{ SNDRV_SEQ_EVENT_QFRAME, UMP_SYSTEM_STATUS_MIDI_TIME_CODE,
1061 	  system_1p_ev_to_ump_midi1, system_1p_ev_to_ump_midi2 },
1062 	{ SNDRV_SEQ_EVENT_SONGPOS, UMP_SYSTEM_STATUS_SONG_POSITION,
1063 	  system_2p_ev_to_ump_midi1, system_2p_ev_to_ump_midi2 },
1064 	{ SNDRV_SEQ_EVENT_SONGSEL, UMP_SYSTEM_STATUS_SONG_SELECT,
1065 	  system_1p_ev_to_ump_midi1, system_1p_ev_to_ump_midi2 },
1066 	{ SNDRV_SEQ_EVENT_TUNE_REQUEST, UMP_SYSTEM_STATUS_TUNE_REQUEST,
1067 	  system_ev_to_ump_midi1, system_ev_to_ump_midi2 },
1068 	{ SNDRV_SEQ_EVENT_CLOCK, UMP_SYSTEM_STATUS_TIMING_CLOCK,
1069 	  system_ev_to_ump_midi1, system_ev_to_ump_midi2 },
1070 	{ SNDRV_SEQ_EVENT_START, UMP_SYSTEM_STATUS_START,
1071 	  system_ev_to_ump_midi1, system_ev_to_ump_midi2 },
1072 	{ SNDRV_SEQ_EVENT_CONTINUE, UMP_SYSTEM_STATUS_CONTINUE,
1073 	  system_ev_to_ump_midi1, system_ev_to_ump_midi2 },
1074 	{ SNDRV_SEQ_EVENT_STOP, UMP_SYSTEM_STATUS_STOP,
1075 	  system_ev_to_ump_midi1, system_ev_to_ump_midi2 },
1076 	{ SNDRV_SEQ_EVENT_SENSING, UMP_SYSTEM_STATUS_ACTIVE_SENSING,
1077 	  system_ev_to_ump_midi1, system_ev_to_ump_midi2 },
1078 };
1079 
1080 static const struct seq_ev_to_ump *find_ump_encoder(int type)
1081 {
1082 	int i;
1083 
1084 	for (i = 0; i < ARRAY_SIZE(seq_ev_ump_encoders); i++)
1085 		if (seq_ev_ump_encoders[i].seq_type == type)
1086 			return &seq_ev_ump_encoders[i];
1087 
1088 	return NULL;
1089 }
1090 
1091 static void setup_ump_event(struct snd_seq_ump_event *dest,
1092 			    const struct snd_seq_event *src)
1093 {
1094 	memcpy(dest, src, sizeof(*src));
1095 	dest->type = 0;
1096 	dest->flags |= SNDRV_SEQ_EVENT_UMP;
1097 	dest->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
1098 	memset(dest->ump, 0, sizeof(dest->ump));
1099 }
1100 
1101 /* Convert ALSA seq event to UMP MIDI 1.0 and deliver it */
1102 static int cvt_to_ump_midi1(struct snd_seq_client *dest,
1103 			    struct snd_seq_client_port *dest_port,
1104 			    struct snd_seq_event *event,
1105 			    int atomic, int hop)
1106 {
1107 	const struct seq_ev_to_ump *encoder;
1108 	struct snd_seq_ump_event ev_cvt;
1109 	union snd_ump_midi1_msg data[4];
1110 	int i, n, err;
1111 
1112 	encoder = find_ump_encoder(event->type);
1113 	if (!encoder)
1114 		return __snd_seq_deliver_single_event(dest, dest_port,
1115 						      event, atomic, hop);
1116 
1117 	data->raw = make_raw_ump(dest_port, UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE);
1118 	n = encoder->midi1_encode(event, dest_port, data, encoder->status);
1119 	if (!n)
1120 		return 0;
1121 
1122 	setup_ump_event(&ev_cvt, event);
1123 	for (i = 0; i < n; i++) {
1124 		ev_cvt.ump[0] = data[i].raw;
1125 		err = __snd_seq_deliver_single_event(dest, dest_port,
1126 						     (struct snd_seq_event *)&ev_cvt,
1127 						     atomic, hop);
1128 		if (err < 0)
1129 			return err;
1130 	}
1131 
1132 	return 0;
1133 }
1134 
1135 /* Convert ALSA seq event to UMP MIDI 2.0 and deliver it */
1136 static int cvt_to_ump_midi2(struct snd_seq_client *dest,
1137 			    struct snd_seq_client_port *dest_port,
1138 			    struct snd_seq_event *event,
1139 			    int atomic, int hop)
1140 {
1141 	const struct seq_ev_to_ump *encoder;
1142 	struct snd_seq_ump_event ev_cvt;
1143 	union snd_ump_midi2_msg data[2];
1144 	int i, n, err;
1145 
1146 	encoder = find_ump_encoder(event->type);
1147 	if (!encoder)
1148 		return __snd_seq_deliver_single_event(dest, dest_port,
1149 						      event, atomic, hop);
1150 
1151 	data->raw[0] = make_raw_ump(dest_port, UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE);
1152 	data->raw[1] = 0;
1153 	n = encoder->midi2_encode(event, dest_port, data, encoder->status);
1154 	if (!n)
1155 		return 0;
1156 
1157 	setup_ump_event(&ev_cvt, event);
1158 	for (i = 0; i < n; i++) {
1159 		memcpy(ev_cvt.ump, &data[i], sizeof(data[i]));
1160 		err = __snd_seq_deliver_single_event(dest, dest_port,
1161 						     (struct snd_seq_event *)&ev_cvt,
1162 						     atomic, hop);
1163 		if (err < 0)
1164 			return err;
1165 	}
1166 
1167 	return 0;
1168 }
1169 
1170 /* Fill up a sysex7 UMP from the byte stream */
1171 static void fill_sysex7_ump(struct snd_seq_client_port *dest_port,
1172 			    u32 *val, u8 status, u8 *buf, int len)
1173 {
1174 	memset(val, 0, 8);
1175 	memcpy((u8 *)val + 2, buf, len);
1176 #ifdef __LITTLE_ENDIAN
1177 	swab32_array(val, 2);
1178 #endif
1179 	val[0] |= ump_compose(UMP_MSG_TYPE_DATA, get_ump_group(dest_port),
1180 			      status, len);
1181 }
1182 
1183 /* Convert sysex var event to UMP sysex7 packets and deliver them */
1184 static int cvt_sysex_to_ump(struct snd_seq_client *dest,
1185 			    struct snd_seq_client_port *dest_port,
1186 			    struct snd_seq_event *event,
1187 			    int atomic, int hop)
1188 {
1189 	struct snd_seq_ump_event ev_cvt;
1190 	unsigned char status;
1191 	u8 buf[6], *xbuf;
1192 	int offset = 0;
1193 	int len, err;
1194 
1195 	if (!snd_seq_ev_is_variable(event))
1196 		return 0;
1197 
1198 	setup_ump_event(&ev_cvt, event);
1199 	for (;;) {
1200 		len = snd_seq_expand_var_event_at(event, sizeof(buf), buf, offset);
1201 		if (len <= 0)
1202 			break;
1203 		if (WARN_ON(len > 6))
1204 			break;
1205 		offset += len;
1206 		xbuf = buf;
1207 		if (*xbuf == UMP_MIDI1_MSG_SYSEX_START) {
1208 			status = UMP_SYSEX_STATUS_START;
1209 			xbuf++;
1210 			len--;
1211 			if (len > 0 && xbuf[len - 1] == UMP_MIDI1_MSG_SYSEX_END) {
1212 				status = UMP_SYSEX_STATUS_SINGLE;
1213 				len--;
1214 			}
1215 		} else {
1216 			if (xbuf[len - 1] == UMP_MIDI1_MSG_SYSEX_END) {
1217 				status = UMP_SYSEX_STATUS_END;
1218 				len--;
1219 			} else {
1220 				status = UMP_SYSEX_STATUS_CONTINUE;
1221 			}
1222 		}
1223 		fill_sysex7_ump(dest_port, ev_cvt.ump, status, xbuf, len);
1224 		err = __snd_seq_deliver_single_event(dest, dest_port,
1225 						     (struct snd_seq_event *)&ev_cvt,
1226 						     atomic, hop);
1227 		if (err < 0)
1228 			return err;
1229 	}
1230 	return 0;
1231 }
1232 
1233 /* Convert to UMP packet and deliver */
1234 int snd_seq_deliver_to_ump(struct snd_seq_client *source,
1235 			   struct snd_seq_client *dest,
1236 			   struct snd_seq_client_port *dest_port,
1237 			   struct snd_seq_event *event,
1238 			   int atomic, int hop)
1239 {
1240 	if (dest->group_filter & (1U << dest_port->ump_group))
1241 		return 0; /* group filtered - skip the event */
1242 	if (event->type == SNDRV_SEQ_EVENT_SYSEX)
1243 		return cvt_sysex_to_ump(dest, dest_port, event, atomic, hop);
1244 	else if (snd_seq_client_is_midi2(dest))
1245 		return cvt_to_ump_midi2(dest, dest_port, event, atomic, hop);
1246 	else
1247 		return cvt_to_ump_midi1(dest, dest_port, event, atomic, hop);
1248 }
1249