xref: /linux/include/sound/ump.h (revision 81fd444aa371261cd33f31d4ffd80faeeeab0cc9)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Universal MIDI Packet (UMP) Support
4  */
5 #ifndef __SOUND_UMP_H
6 #define __SOUND_UMP_H
7 
8 #include <sound/rawmidi.h>
9 
10 struct snd_ump_endpoint;
11 struct snd_ump_block;
12 struct snd_ump_ops;
13 struct ump_cvt_to_ump;
14 struct snd_seq_ump_ops;
15 
16 struct snd_ump_endpoint {
17 	struct snd_rawmidi core;	/* raw UMP access */
18 
19 	struct snd_ump_endpoint_info info;
20 
21 	const struct snd_ump_ops *ops;	/* UMP ops set by the driver */
22 	struct snd_rawmidi_substream *substreams[2];	/* opened substreams */
23 
24 	void *private_data;
25 	void (*private_free)(struct snd_ump_endpoint *ump);
26 
27 	struct list_head block_list;	/* list of snd_ump_block objects */
28 
29 	/* intermediate buffer for UMP input */
30 	u32 input_buf[4];
31 	int input_buf_head;
32 	int input_pending;
33 
34 	struct mutex open_mutex;
35 
36 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI)
37 	spinlock_t legacy_locks[2];
38 	struct snd_rawmidi *legacy_rmidi;
39 	struct snd_rawmidi_substream *legacy_substreams[2][SNDRV_UMP_MAX_GROUPS];
40 
41 	/* for legacy output; need to open the actual substream unlike input */
42 	int legacy_out_opens;
43 	struct snd_rawmidi_file legacy_out_rfile;
44 	struct ump_cvt_to_ump *out_cvts;
45 #endif
46 
47 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
48 	struct snd_seq_device *seq_dev;
49 	const struct snd_seq_ump_ops *seq_ops;
50 	void *seq_client;
51 #endif
52 };
53 
54 /* ops filled by UMP drivers */
55 struct snd_ump_ops {
56 	int (*open)(struct snd_ump_endpoint *ump, int dir);
57 	void (*close)(struct snd_ump_endpoint *ump, int dir);
58 	void (*trigger)(struct snd_ump_endpoint *ump, int dir, int up);
59 	void (*drain)(struct snd_ump_endpoint *ump, int dir);
60 };
61 
62 /* ops filled by sequencer binding */
63 struct snd_seq_ump_ops {
64 	void (*input_receive)(struct snd_ump_endpoint *ump,
65 			      const u32 *data, int words);
66 };
67 
68 struct snd_ump_block {
69 	struct snd_ump_block_info info;
70 	struct snd_ump_endpoint *ump;
71 
72 	void *private_data;
73 	void (*private_free)(struct snd_ump_block *blk);
74 
75 	struct list_head list;
76 };
77 
78 #define rawmidi_to_ump(rmidi)	container_of(rmidi, struct snd_ump_endpoint, core)
79 
80 int snd_ump_endpoint_new(struct snd_card *card, char *id, int device,
81 			 int output, int input,
82 			 struct snd_ump_endpoint **ump_ret);
83 int snd_ump_block_new(struct snd_ump_endpoint *ump, unsigned int blk,
84 		      unsigned int direction, unsigned int first_group,
85 		      unsigned int num_groups, struct snd_ump_block **blk_ret);
86 int snd_ump_receive(struct snd_ump_endpoint *ump, const u32 *buffer, int count);
87 int snd_ump_transmit(struct snd_ump_endpoint *ump, u32 *buffer, int count);
88 
89 #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI)
90 int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump,
91 				  char *id, int device);
92 #else
93 static inline int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump,
94 						char *id, int device)
95 {
96 	return 0;
97 }
98 #endif
99 
100 /*
101  * Some definitions for UMP
102  */
103 
104 /* MIDI 2.0 Message Type */
105 enum {
106 	UMP_MSG_TYPE_UTILITY			= 0x00,
107 	UMP_MSG_TYPE_SYSTEM			= 0x01,
108 	UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE	= 0x02,
109 	UMP_MSG_TYPE_DATA			= 0x03,
110 	UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE	= 0x04,
111 	UMP_MSG_TYPE_EXTENDED_DATA		= 0x05,
112 };
113 
114 /* MIDI 2.0 SysEx / Data Status; same values for both 7-bit and 8-bit SysEx */
115 enum {
116 	UMP_SYSEX_STATUS_SINGLE			= 0,
117 	UMP_SYSEX_STATUS_START			= 1,
118 	UMP_SYSEX_STATUS_CONTINUE		= 2,
119 	UMP_SYSEX_STATUS_END			= 3,
120 };
121 
122 /*
123  * Helpers for retrieving / filling bits from UMP
124  */
125 /* get the message type (4bit) from a UMP packet (header) */
126 static inline unsigned char ump_message_type(u32 data)
127 {
128 	return data >> 28;
129 }
130 
131 /* get the group number (0-based, 4bit) from a UMP packet (header) */
132 static inline unsigned char ump_message_group(u32 data)
133 {
134 	return (data >> 24) & 0x0f;
135 }
136 
137 /* get the MIDI status code (4bit) from a UMP packet (header) */
138 static inline unsigned char ump_message_status_code(u32 data)
139 {
140 	return (data >> 20) & 0x0f;
141 }
142 
143 /* get the MIDI channel number (0-based, 4bit) from a UMP packet (header) */
144 static inline unsigned char ump_message_channel(u32 data)
145 {
146 	return (data >> 16) & 0x0f;
147 }
148 
149 /* get the MIDI status + channel combo byte (8bit) from a UMP packet (header) */
150 static inline unsigned char ump_message_status_channel(u32 data)
151 {
152 	return (data >> 16) & 0xff;
153 }
154 
155 /* compose a UMP packet (header) from type, group and status values */
156 static inline u32 ump_compose(unsigned char type, unsigned char group,
157 			      unsigned char status, unsigned char channel)
158 {
159 	return ((u32)type << 28) | ((u32)group << 24) | ((u32)status << 20) |
160 		((u32)channel << 16);
161 }
162 
163 /* get SysEx message status (for both 7 and 8bits) from a UMP packet (header) */
164 static inline unsigned char ump_sysex_message_status(u32 data)
165 {
166 	return (data >> 20) & 0xf;
167 }
168 
169 /* get SysEx message length (for both 7 and 8bits) from a UMP packet (header) */
170 static inline unsigned char ump_sysex_message_length(u32 data)
171 {
172 	return (data >> 16) & 0xf;
173 }
174 
175 #endif /* __SOUND_UMP_H */
176