xref: /linux/sound/synth/emux/emux_seq.c (revision 9611c0ce215a66770ccbe5c126bf57ba8c31bcad)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Midi Sequencer interface routines.
4  *
5  *  Copyright (C) 1999 Steve Ratcliffe
6  *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
7  */
8 
9 #include "emux_voice.h"
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 
13 /* Prototypes for static functions */
14 static void free_port(void *private);
15 static void snd_emux_init_port(struct snd_emux_port *p);
16 static int snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info);
17 static int snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info);
18 
19 /*
20  * MIDI emulation operators
21  */
22 static const struct snd_midi_op emux_ops = {
23 	.note_on = snd_emux_note_on,
24 	.note_off = snd_emux_note_off,
25 	.key_press = snd_emux_key_press,
26 	.note_terminate = snd_emux_terminate_note,
27 	.control = snd_emux_control,
28 	.nrpn = snd_emux_nrpn,
29 	.sysex = snd_emux_sysex,
30 };
31 
32 
33 /*
34  * number of MIDI channels
35  */
36 #define MIDI_CHANNELS		16
37 
38 /*
39  * type flags for MIDI sequencer port
40  */
41 #define DEFAULT_MIDI_TYPE	(SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |\
42 				 SNDRV_SEQ_PORT_TYPE_MIDI_GM |\
43 				 SNDRV_SEQ_PORT_TYPE_MIDI_GS |\
44 				 SNDRV_SEQ_PORT_TYPE_MIDI_XG |\
45 				 SNDRV_SEQ_PORT_TYPE_HARDWARE |\
46 				 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
47 
48 /*
49  * Initialise the EMUX Synth by creating a client and registering
50  * a series of ports.
51  * Each of the ports will contain the 16 midi channels.  Applications
52  * can connect to these ports to play midi data.
53  */
54 int
55 snd_emux_init_seq(struct snd_emux *emu, struct snd_card *card, int index)
56 {
57 	int  i;
58 	struct snd_seq_port_callback pinfo;
59 	char tmpname[64];
60 
61 	emu->client = snd_seq_create_kernel_client(card, index,
62 						   "%s WaveTable", emu->name);
63 	if (emu->client < 0) {
64 		dev_err(card->dev, "can't create client\n");
65 		return -ENODEV;
66 	}
67 
68 	if (emu->num_ports <= 0) {
69 		dev_warn(card->dev, "seqports must be greater than zero\n");
70 		emu->num_ports = 1;
71 	} else if (emu->num_ports > SNDRV_EMUX_MAX_PORTS) {
72 		dev_warn(card->dev,
73 			 "too many ports. limited max. ports %d\n",
74 			 SNDRV_EMUX_MAX_PORTS);
75 		emu->num_ports = SNDRV_EMUX_MAX_PORTS;
76 	}
77 
78 	memset(&pinfo, 0, sizeof(pinfo));
79 	pinfo.owner = THIS_MODULE;
80 	pinfo.use = snd_emux_use;
81 	pinfo.unuse = snd_emux_unuse;
82 	pinfo.event_input = snd_emux_event_input;
83 
84 	for (i = 0; i < emu->num_ports; i++) {
85 		struct snd_emux_port *p;
86 
87 		sprintf(tmpname, "%s Port %d", emu->name, i);
88 		p = snd_emux_create_port(emu, tmpname, MIDI_CHANNELS,
89 					 0, &pinfo);
90 		if (!p) {
91 			dev_err(card->dev, "can't create port\n");
92 			return -ENOMEM;
93 		}
94 
95 		p->port_mode =  SNDRV_EMUX_PORT_MODE_MIDI;
96 		snd_emux_init_port(p);
97 		emu->ports[i] = p->chset.port;
98 		emu->portptrs[i] = p;
99 	}
100 
101 	return 0;
102 }
103 
104 
105 /*
106  * Detach from the ports that were set up for this synthesizer and
107  * destroy the kernel client.
108  */
109 void
110 snd_emux_detach_seq(struct snd_emux *emu)
111 {
112 	if (emu->voices)
113 		snd_emux_terminate_all(emu);
114 
115 	if (emu->client >= 0) {
116 		snd_seq_delete_kernel_client(emu->client);
117 		emu->client = -1;
118 	}
119 }
120 
121 
122 /*
123  * create a sequencer port and channel_set
124  */
125 
126 struct snd_emux_port *
127 snd_emux_create_port(struct snd_emux *emu, char *name,
128 		     int max_channels, int oss_port,
129 		     struct snd_seq_port_callback *callback)
130 {
131 	struct snd_emux_port *p;
132 	int i, type, cap;
133 
134 	/* Allocate structures for this channel */
135 	p = kzalloc_flex(*p, chset.channels, max_channels);
136 	if (!p)
137 		return NULL;
138 
139 	p->chset.max_channels = max_channels;
140 
141 	for (i = 0; i < max_channels; i++)
142 		p->chset.channels[i].number = i;
143 	p->chset.private_data = p;
144 	p->emu = emu;
145 	p->chset.client = emu->client;
146 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
147 	snd_emux_create_effect(p);
148 #endif
149 	callback->private_free = free_port;
150 	callback->private_data = p;
151 
152 	cap = SNDRV_SEQ_PORT_CAP_WRITE;
153 	if (oss_port) {
154 		type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
155 	} else {
156 		type = DEFAULT_MIDI_TYPE;
157 		cap |= SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
158 	}
159 
160 	p->chset.port = snd_seq_event_port_attach(emu->client, callback,
161 						  cap, type, max_channels,
162 						  emu->max_voices, name);
163 
164 	return p;
165 }
166 
167 
168 /*
169  * release memory block for port
170  */
171 static void
172 free_port(void *private_data)
173 {
174 	struct snd_emux_port *p;
175 
176 	p = private_data;
177 	if (p) {
178 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
179 		snd_emux_delete_effect(p);
180 #endif
181 		kfree(p);
182 	}
183 }
184 
185 
186 #define DEFAULT_DRUM_FLAGS	(1<<9)
187 
188 /*
189  * initialize the port specific parameters
190  */
191 static void
192 snd_emux_init_port(struct snd_emux_port *p)
193 {
194 	p->drum_flags = DEFAULT_DRUM_FLAGS;
195 	p->volume_atten = 0;
196 
197 	snd_emux_reset_port(p);
198 }
199 
200 
201 /*
202  * reset port
203  */
204 void
205 snd_emux_reset_port(struct snd_emux_port *port)
206 {
207 	int i;
208 
209 	/* stop all sounds */
210 	snd_emux_sounds_off_all(port);
211 
212 	snd_midi_channel_set_clear(&port->chset);
213 
214 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
215 	snd_emux_clear_effect(port);
216 #endif
217 
218 	/* set port specific control parameters */
219 	port->ctrls[EMUX_MD_DEF_BANK] = 0;
220 	port->ctrls[EMUX_MD_DEF_DRUM] = 0;
221 	port->ctrls[EMUX_MD_REALTIME_PAN] = 1;
222 
223 	for (i = 0; i < port->chset.max_channels; i++) {
224 		struct snd_midi_channel *chan = port->chset.channels + i;
225 		chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
226 	}
227 }
228 
229 
230 /*
231  * input sequencer event
232  */
233 int
234 snd_emux_event_input(struct snd_seq_event *ev, int direct, void *private_data,
235 		     int atomic, int hop)
236 {
237 	struct snd_emux_port *port;
238 
239 	port = private_data;
240 	if (snd_BUG_ON(!port || !ev))
241 		return -EINVAL;
242 
243 	snd_midi_process_event(&emux_ops, ev, &port->chset);
244 
245 	return 0;
246 }
247 
248 
249 /*
250  * increment usage count
251  */
252 static int
253 __snd_emux_inc_count(struct snd_emux *emu)
254 {
255 	emu->used++;
256 	if (!try_module_get(emu->ops.owner))
257 		goto __error;
258 	if (!try_module_get(emu->card->module)) {
259 		module_put(emu->ops.owner);
260 	      __error:
261 		emu->used--;
262 		return 0;
263 	}
264 	return 1;
265 }
266 
267 int snd_emux_inc_count(struct snd_emux *emu)
268 {
269 	guard(mutex)(&emu->register_mutex);
270 	return __snd_emux_inc_count(emu);
271 }
272 
273 /*
274  * decrease usage count
275  */
276 static void
277 __snd_emux_dec_count(struct snd_emux *emu)
278 {
279 	module_put(emu->card->module);
280 	emu->used--;
281 	if (emu->used <= 0)
282 		snd_emux_terminate_all(emu);
283 	module_put(emu->ops.owner);
284 }
285 
286 void snd_emux_dec_count(struct snd_emux *emu)
287 {
288 	guard(mutex)(&emu->register_mutex);
289 	__snd_emux_dec_count(emu);
290 }
291 
292 /*
293  * Routine that is called upon a first use of a particular port
294  */
295 static int
296 snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info)
297 {
298 	struct snd_emux_port *p;
299 	struct snd_emux *emu;
300 
301 	p = private_data;
302 	if (snd_BUG_ON(!p))
303 		return -EINVAL;
304 	emu = p->emu;
305 	if (snd_BUG_ON(!emu))
306 		return -EINVAL;
307 
308 	guard(mutex)(&emu->register_mutex);
309 	snd_emux_init_port(p);
310 	__snd_emux_inc_count(emu);
311 	return 0;
312 }
313 
314 /*
315  * Routine that is called upon the last unuse() of a particular port.
316  */
317 static int
318 snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info)
319 {
320 	struct snd_emux_port *p;
321 	struct snd_emux *emu;
322 
323 	p = private_data;
324 	if (snd_BUG_ON(!p))
325 		return -EINVAL;
326 	emu = p->emu;
327 	if (snd_BUG_ON(!emu))
328 		return -EINVAL;
329 
330 	guard(mutex)(&emu->register_mutex);
331 	snd_emux_sounds_off_all(p);
332 	__snd_emux_dec_count(emu);
333 	return 0;
334 }
335 
336 
337 /*
338  * attach virtual rawmidi devices
339  */
340 int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card)
341 {
342 	int i;
343 
344 	emu->vmidi = NULL;
345 	if (emu->midi_ports <= 0)
346 		return 0;
347 
348 	emu->vmidi = kzalloc_objs(*emu->vmidi, emu->midi_ports);
349 	if (!emu->vmidi)
350 		return -ENOMEM;
351 
352 	for (i = 0; i < emu->midi_ports; i++) {
353 		struct snd_rawmidi *rmidi;
354 		struct snd_virmidi_dev *rdev;
355 		if (snd_virmidi_new(card, emu->midi_devidx + i, &rmidi) < 0)
356 			goto __error;
357 		rdev = rmidi->private_data;
358 		sprintf(rmidi->name, "%s Synth MIDI", emu->name);
359 		rdev->seq_mode = SNDRV_VIRMIDI_SEQ_ATTACH;
360 		rdev->client = emu->client;
361 		rdev->port = emu->ports[i];
362 		if (snd_device_register(card, rmidi) < 0) {
363 			snd_device_free(card, rmidi);
364 			goto __error;
365 		}
366 		emu->vmidi[i] = rmidi;
367 	}
368 	return 0;
369 
370 __error:
371 	snd_emux_delete_virmidi(emu);
372 	return -ENOMEM;
373 }
374 
375 int snd_emux_delete_virmidi(struct snd_emux *emu)
376 {
377 	int i;
378 
379 	if (!emu->vmidi)
380 		return 0;
381 
382 	for (i = 0; i < emu->midi_ports; i++) {
383 		if (emu->vmidi[i])
384 			snd_device_free(emu->card, emu->vmidi[i]);
385 	}
386 	kfree(emu->vmidi);
387 	emu->vmidi = NULL;
388 	return 0;
389 }
390