Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Generic MIDI synth driver for ALSA sequencer | |
4 | * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl> | |
c1017a4c | 5 | * Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
6 | */ |
7 | ||
8 | /* | |
9 | Possible options for midisynth module: | |
10 | - automatic opening of midi ports on first received event or subscription | |
11 | (close will be performed when client leaves) | |
12 | */ | |
13 | ||
14 | ||
1da177e4 LT |
15 | #include <linux/init.h> |
16 | #include <linux/slab.h> | |
17 | #include <linux/errno.h> | |
18 | #include <linux/string.h> | |
65a77217 | 19 | #include <linux/module.h> |
1a60d4c5 | 20 | #include <linux/mutex.h> |
1da177e4 LT |
21 | #include <sound/core.h> |
22 | #include <sound/rawmidi.h> | |
23 | #include <sound/seq_kernel.h> | |
24 | #include <sound/seq_device.h> | |
25 | #include <sound/seq_midi_event.h> | |
26 | #include <sound/initval.h> | |
27 | ||
c1017a4c | 28 | MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@perex.cz>"); |
1da177e4 LT |
29 | MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth."); |
30 | MODULE_LICENSE("GPL"); | |
31 | static int output_buffer_size = PAGE_SIZE; | |
32 | module_param(output_buffer_size, int, 0644); | |
33 | MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes."); | |
34 | static int input_buffer_size = PAGE_SIZE; | |
35 | module_param(input_buffer_size, int, 0644); | |
36 | MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes."); | |
37 | ||
38 | /* data for this midi synth driver */ | |
c7e0b5bf TI |
39 | struct seq_midisynth { |
40 | struct snd_card *card; | |
1da177e4 LT |
41 | int device; |
42 | int subdevice; | |
c7e0b5bf TI |
43 | struct snd_rawmidi_file input_rfile; |
44 | struct snd_rawmidi_file output_rfile; | |
1da177e4 LT |
45 | int seq_client; |
46 | int seq_port; | |
c7e0b5bf TI |
47 | struct snd_midi_event *parser; |
48 | }; | |
1da177e4 | 49 | |
c7e0b5bf | 50 | struct seq_midisynth_client { |
1da177e4 LT |
51 | int seq_client; |
52 | int num_ports; | |
53 | int ports_per_device[SNDRV_RAWMIDI_DEVICES]; | |
c7e0b5bf TI |
54 | struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES]; |
55 | }; | |
1da177e4 | 56 | |
c7e0b5bf | 57 | static struct seq_midisynth_client *synths[SNDRV_CARDS]; |
1a60d4c5 | 58 | static DEFINE_MUTEX(register_mutex); |
1da177e4 LT |
59 | |
60 | /* handle rawmidi input event (MIDI v1.0 stream) */ | |
c7e0b5bf | 61 | static void snd_midi_input_event(struct snd_rawmidi_substream *substream) |
1da177e4 | 62 | { |
c7e0b5bf TI |
63 | struct snd_rawmidi_runtime *runtime; |
64 | struct seq_midisynth *msynth; | |
65 | struct snd_seq_event ev; | |
1da177e4 | 66 | char buf[16], *pbuf; |
ef965ad5 | 67 | long res; |
1da177e4 LT |
68 | |
69 | if (substream == NULL) | |
70 | return; | |
71 | runtime = substream->runtime; | |
c7e0b5bf | 72 | msynth = runtime->private_data; |
1da177e4 LT |
73 | if (msynth == NULL) |
74 | return; | |
75 | memset(&ev, 0, sizeof(ev)); | |
76 | while (runtime->avail > 0) { | |
77 | res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf)); | |
78 | if (res <= 0) | |
79 | continue; | |
80 | if (msynth->parser == NULL) | |
81 | continue; | |
82 | pbuf = buf; | |
ef965ad5 TI |
83 | while (res-- > 0) { |
84 | if (!snd_midi_event_encode_byte(msynth->parser, | |
85 | *pbuf++, &ev)) | |
86 | continue; | |
87 | ev.source.port = msynth->seq_port; | |
88 | ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; | |
89 | snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0); | |
90 | /* clear event and reset header */ | |
91 | memset(&ev, 0, sizeof(ev)); | |
1da177e4 LT |
92 | } |
93 | } | |
94 | } | |
95 | ||
c7e0b5bf | 96 | static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count) |
1da177e4 | 97 | { |
c7e0b5bf | 98 | struct snd_rawmidi_runtime *runtime; |
1da177e4 LT |
99 | int tmp; |
100 | ||
7eaa943c TI |
101 | if (snd_BUG_ON(!substream || !buf)) |
102 | return -EINVAL; | |
1da177e4 LT |
103 | runtime = substream->runtime; |
104 | if ((tmp = runtime->avail) < count) { | |
f907ed94 | 105 | if (printk_ratelimit()) |
04cc79a0 | 106 | pr_err("ALSA: seq_midi: MIDI output buffer overrun\n"); |
1da177e4 LT |
107 | return -ENOMEM; |
108 | } | |
109 | if (snd_rawmidi_kernel_write(substream, buf, count) < count) | |
110 | return -EINVAL; | |
111 | return 0; | |
112 | } | |
113 | ||
c7e0b5bf | 114 | static int event_process_midi(struct snd_seq_event *ev, int direct, |
1da177e4 LT |
115 | void *private_data, int atomic, int hop) |
116 | { | |
c7e0b5bf | 117 | struct seq_midisynth *msynth = private_data; |
1da177e4 | 118 | unsigned char msg[10]; /* buffer for constructing midi messages */ |
c7e0b5bf | 119 | struct snd_rawmidi_substream *substream; |
d06e4c40 | 120 | int len; |
1da177e4 | 121 | |
7eaa943c TI |
122 | if (snd_BUG_ON(!msynth)) |
123 | return -EINVAL; | |
1da177e4 LT |
124 | substream = msynth->output_rfile.output; |
125 | if (substream == NULL) | |
126 | return -ENODEV; | |
127 | if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */ | |
128 | if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) { | |
129 | /* invalid event */ | |
04cc79a0 | 130 | pr_debug("ALSA: seq_midi: invalid sysex event flags = 0x%x\n", ev->flags); |
1da177e4 LT |
131 | return 0; |
132 | } | |
d06e4c40 | 133 | snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream); |
1da177e4 | 134 | snd_midi_event_reset_decode(msynth->parser); |
1da177e4 LT |
135 | } else { |
136 | if (msynth->parser == NULL) | |
137 | return -EIO; | |
d06e4c40 CL |
138 | len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev); |
139 | if (len < 0) | |
140 | return 0; | |
141 | if (dump_midi(substream, msg, len) < 0) | |
1da177e4 | 142 | snd_midi_event_reset_decode(msynth->parser); |
1da177e4 LT |
143 | } |
144 | return 0; | |
145 | } | |
146 | ||
147 | ||
c7e0b5bf TI |
148 | static int snd_seq_midisynth_new(struct seq_midisynth *msynth, |
149 | struct snd_card *card, | |
1da177e4 LT |
150 | int device, |
151 | int subdevice) | |
152 | { | |
153 | if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0) | |
154 | return -ENOMEM; | |
155 | msynth->card = card; | |
156 | msynth->device = device; | |
157 | msynth->subdevice = subdevice; | |
158 | return 0; | |
159 | } | |
160 | ||
161 | /* open associated midi device for input */ | |
c7e0b5bf | 162 | static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info) |
1da177e4 LT |
163 | { |
164 | int err; | |
c7e0b5bf TI |
165 | struct seq_midisynth *msynth = private_data; |
166 | struct snd_rawmidi_runtime *runtime; | |
167 | struct snd_rawmidi_params params; | |
1da177e4 LT |
168 | |
169 | /* open midi port */ | |
f87135f5 CL |
170 | if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device, |
171 | msynth->subdevice, | |
172 | SNDRV_RAWMIDI_LFLG_INPUT, | |
173 | &msynth->input_rfile)) < 0) { | |
04cc79a0 | 174 | pr_debug("ALSA: seq_midi: midi input open failed!!!\n"); |
1da177e4 LT |
175 | return err; |
176 | } | |
177 | runtime = msynth->input_rfile.input->runtime; | |
178 | memset(¶ms, 0, sizeof(params)); | |
179 | params.avail_min = 1; | |
180 | params.buffer_size = input_buffer_size; | |
181 | if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, ¶ms)) < 0) { | |
182 | snd_rawmidi_kernel_release(&msynth->input_rfile); | |
183 | return err; | |
184 | } | |
185 | snd_midi_event_reset_encode(msynth->parser); | |
186 | runtime->event = snd_midi_input_event; | |
187 | runtime->private_data = msynth; | |
188 | snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0); | |
189 | return 0; | |
190 | } | |
191 | ||
192 | /* close associated midi device for input */ | |
c7e0b5bf | 193 | static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info) |
1da177e4 LT |
194 | { |
195 | int err; | |
c7e0b5bf | 196 | struct seq_midisynth *msynth = private_data; |
1da177e4 | 197 | |
7eaa943c TI |
198 | if (snd_BUG_ON(!msynth->input_rfile.input)) |
199 | return -EINVAL; | |
1da177e4 LT |
200 | err = snd_rawmidi_kernel_release(&msynth->input_rfile); |
201 | return err; | |
202 | } | |
203 | ||
204 | /* open associated midi device for output */ | |
c7e0b5bf | 205 | static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info) |
1da177e4 LT |
206 | { |
207 | int err; | |
c7e0b5bf TI |
208 | struct seq_midisynth *msynth = private_data; |
209 | struct snd_rawmidi_params params; | |
1da177e4 LT |
210 | |
211 | /* open midi port */ | |
f87135f5 CL |
212 | if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device, |
213 | msynth->subdevice, | |
214 | SNDRV_RAWMIDI_LFLG_OUTPUT, | |
215 | &msynth->output_rfile)) < 0) { | |
04cc79a0 | 216 | pr_debug("ALSA: seq_midi: midi output open failed!!!\n"); |
1da177e4 LT |
217 | return err; |
218 | } | |
219 | memset(¶ms, 0, sizeof(params)); | |
220 | params.avail_min = 1; | |
221 | params.buffer_size = output_buffer_size; | |
2d4b8420 | 222 | params.no_active_sensing = 1; |
1da177e4 LT |
223 | if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, ¶ms)) < 0) { |
224 | snd_rawmidi_kernel_release(&msynth->output_rfile); | |
225 | return err; | |
226 | } | |
227 | snd_midi_event_reset_decode(msynth->parser); | |
228 | return 0; | |
229 | } | |
230 | ||
231 | /* close associated midi device for output */ | |
c7e0b5bf | 232 | static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info) |
1da177e4 | 233 | { |
c7e0b5bf | 234 | struct seq_midisynth *msynth = private_data; |
1da177e4 | 235 | |
7eaa943c TI |
236 | if (snd_BUG_ON(!msynth->output_rfile.output)) |
237 | return -EINVAL; | |
1da177e4 LT |
238 | snd_rawmidi_drain_output(msynth->output_rfile.output); |
239 | return snd_rawmidi_kernel_release(&msynth->output_rfile); | |
240 | } | |
241 | ||
242 | /* delete given midi synth port */ | |
c7e0b5bf | 243 | static void snd_seq_midisynth_delete(struct seq_midisynth *msynth) |
1da177e4 LT |
244 | { |
245 | if (msynth == NULL) | |
246 | return; | |
247 | ||
248 | if (msynth->seq_client > 0) { | |
249 | /* delete port */ | |
250 | snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port); | |
251 | } | |
252 | ||
57dca36e | 253 | snd_midi_event_free(msynth->parser); |
1da177e4 LT |
254 | } |
255 | ||
1da177e4 LT |
256 | /* register new midi synth port */ |
257 | static int | |
05662205 | 258 | snd_seq_midisynth_probe(struct device *_dev) |
1da177e4 | 259 | { |
05662205 | 260 | struct snd_seq_device *dev = to_seq_dev(_dev); |
c7e0b5bf TI |
261 | struct seq_midisynth_client *client; |
262 | struct seq_midisynth *msynth, *ms; | |
263 | struct snd_seq_port_info *port; | |
264 | struct snd_rawmidi_info *info; | |
a7b928ac | 265 | struct snd_rawmidi *rmidi = dev->private_data; |
1da177e4 LT |
266 | int newclient = 0; |
267 | unsigned int p, ports; | |
c7e0b5bf TI |
268 | struct snd_seq_port_callback pcallbacks; |
269 | struct snd_card *card = dev->card; | |
1da177e4 LT |
270 | int device = dev->device; |
271 | unsigned int input_count = 0, output_count = 0; | |
272 | ||
7eaa943c TI |
273 | if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES)) |
274 | return -EINVAL; | |
1da177e4 LT |
275 | info = kmalloc(sizeof(*info), GFP_KERNEL); |
276 | if (! info) | |
277 | return -ENOMEM; | |
278 | info->device = device; | |
279 | info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT; | |
280 | info->subdevice = 0; | |
281 | if (snd_rawmidi_info_select(card, info) >= 0) | |
282 | output_count = info->subdevices_count; | |
283 | info->stream = SNDRV_RAWMIDI_STREAM_INPUT; | |
284 | if (snd_rawmidi_info_select(card, info) >= 0) { | |
285 | input_count = info->subdevices_count; | |
286 | } | |
287 | ports = output_count; | |
288 | if (ports < input_count) | |
289 | ports = input_count; | |
290 | if (ports == 0) { | |
291 | kfree(info); | |
292 | return -ENODEV; | |
293 | } | |
294 | if (ports > (256 / SNDRV_RAWMIDI_DEVICES)) | |
295 | ports = 256 / SNDRV_RAWMIDI_DEVICES; | |
296 | ||
1a60d4c5 | 297 | mutex_lock(®ister_mutex); |
1da177e4 LT |
298 | client = synths[card->number]; |
299 | if (client == NULL) { | |
300 | newclient = 1; | |
ecca82b4 | 301 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
1da177e4 | 302 | if (client == NULL) { |
1a60d4c5 | 303 | mutex_unlock(®ister_mutex); |
1da177e4 LT |
304 | kfree(info); |
305 | return -ENOMEM; | |
306 | } | |
7b6d9245 CL |
307 | client->seq_client = |
308 | snd_seq_create_kernel_client( | |
78fc030b AH |
309 | card, 0, "%s", card->shortname[0] ? |
310 | (const char *)card->shortname : "External MIDI"); | |
1da177e4 LT |
311 | if (client->seq_client < 0) { |
312 | kfree(client); | |
1a60d4c5 | 313 | mutex_unlock(®ister_mutex); |
1da177e4 LT |
314 | kfree(info); |
315 | return -ENOMEM; | |
316 | } | |
7b6d9245 | 317 | } |
1da177e4 | 318 | |
c7e0b5bf | 319 | msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL); |
1da177e4 LT |
320 | port = kmalloc(sizeof(*port), GFP_KERNEL); |
321 | if (msynth == NULL || port == NULL) | |
322 | goto __nomem; | |
323 | ||
324 | for (p = 0; p < ports; p++) { | |
325 | ms = &msynth[p]; | |
326 | ||
327 | if (snd_seq_midisynth_new(ms, card, device, p) < 0) | |
328 | goto __nomem; | |
329 | ||
330 | /* declare port */ | |
331 | memset(port, 0, sizeof(*port)); | |
332 | port->addr.client = client->seq_client; | |
333 | port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p; | |
334 | port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; | |
335 | memset(info, 0, sizeof(*info)); | |
336 | info->device = device; | |
337 | if (p < output_count) | |
338 | info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT; | |
339 | else | |
340 | info->stream = SNDRV_RAWMIDI_STREAM_INPUT; | |
341 | info->subdevice = p; | |
342 | if (snd_rawmidi_info_select(card, info) >= 0) | |
343 | strcpy(port->name, info->subname); | |
344 | if (! port->name[0]) { | |
345 | if (info->name[0]) { | |
346 | if (ports > 1) | |
53403a80 | 347 | snprintf(port->name, sizeof(port->name), "%s-%u", info->name, p); |
1da177e4 LT |
348 | else |
349 | snprintf(port->name, sizeof(port->name), "%s", info->name); | |
350 | } else { | |
351 | /* last resort */ | |
352 | if (ports > 1) | |
53403a80 | 353 | sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p); |
1da177e4 LT |
354 | else |
355 | sprintf(port->name, "MIDI %d-%d", card->number, device); | |
356 | } | |
357 | } | |
358 | if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count) | |
359 | port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE; | |
360 | if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count) | |
361 | port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ; | |
362 | if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) && | |
363 | info->flags & SNDRV_RAWMIDI_INFO_DUPLEX) | |
364 | port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX; | |
450047a7 CL |
365 | port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
366 | | SNDRV_SEQ_PORT_TYPE_HARDWARE | |
367 | | SNDRV_SEQ_PORT_TYPE_PORT; | |
1da177e4 LT |
368 | port->midi_channels = 16; |
369 | memset(&pcallbacks, 0, sizeof(pcallbacks)); | |
370 | pcallbacks.owner = THIS_MODULE; | |
371 | pcallbacks.private_data = ms; | |
372 | pcallbacks.subscribe = midisynth_subscribe; | |
373 | pcallbacks.unsubscribe = midisynth_unsubscribe; | |
374 | pcallbacks.use = midisynth_use; | |
375 | pcallbacks.unuse = midisynth_unuse; | |
376 | pcallbacks.event_input = event_process_midi; | |
377 | port->kernel = &pcallbacks; | |
a7b928ac CL |
378 | if (rmidi->ops && rmidi->ops->get_port_info) |
379 | rmidi->ops->get_port_info(rmidi, p, port); | |
1da177e4 LT |
380 | if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0) |
381 | goto __nomem; | |
382 | ms->seq_client = client->seq_client; | |
383 | ms->seq_port = port->addr.port; | |
384 | } | |
385 | client->ports_per_device[device] = ports; | |
386 | client->ports[device] = msynth; | |
387 | client->num_ports++; | |
388 | if (newclient) | |
389 | synths[card->number] = client; | |
1a60d4c5 | 390 | mutex_unlock(®ister_mutex); |
51f633da TI |
391 | kfree(info); |
392 | kfree(port); | |
1da177e4 LT |
393 | return 0; /* success */ |
394 | ||
395 | __nomem: | |
396 | if (msynth != NULL) { | |
397 | for (p = 0; p < ports; p++) | |
398 | snd_seq_midisynth_delete(&msynth[p]); | |
399 | kfree(msynth); | |
400 | } | |
401 | if (newclient) { | |
402 | snd_seq_delete_kernel_client(client->seq_client); | |
403 | kfree(client); | |
404 | } | |
405 | kfree(info); | |
406 | kfree(port); | |
1a60d4c5 | 407 | mutex_unlock(®ister_mutex); |
1da177e4 LT |
408 | return -ENOMEM; |
409 | } | |
410 | ||
411 | /* release midi synth port */ | |
412 | static int | |
05662205 | 413 | snd_seq_midisynth_remove(struct device *_dev) |
1da177e4 | 414 | { |
05662205 | 415 | struct snd_seq_device *dev = to_seq_dev(_dev); |
c7e0b5bf TI |
416 | struct seq_midisynth_client *client; |
417 | struct seq_midisynth *msynth; | |
418 | struct snd_card *card = dev->card; | |
1da177e4 LT |
419 | int device = dev->device, p, ports; |
420 | ||
1a60d4c5 | 421 | mutex_lock(®ister_mutex); |
1da177e4 LT |
422 | client = synths[card->number]; |
423 | if (client == NULL || client->ports[device] == NULL) { | |
1a60d4c5 | 424 | mutex_unlock(®ister_mutex); |
1da177e4 LT |
425 | return -ENODEV; |
426 | } | |
427 | ports = client->ports_per_device[device]; | |
428 | client->ports_per_device[device] = 0; | |
429 | msynth = client->ports[device]; | |
430 | client->ports[device] = NULL; | |
1da177e4 LT |
431 | for (p = 0; p < ports; p++) |
432 | snd_seq_midisynth_delete(&msynth[p]); | |
433 | kfree(msynth); | |
1da177e4 LT |
434 | client->num_ports--; |
435 | if (client->num_ports <= 0) { | |
436 | snd_seq_delete_kernel_client(client->seq_client); | |
437 | synths[card->number] = NULL; | |
438 | kfree(client); | |
439 | } | |
1a60d4c5 | 440 | mutex_unlock(®ister_mutex); |
1da177e4 LT |
441 | return 0; |
442 | } | |
443 | ||
05662205 TI |
444 | static struct snd_seq_driver seq_midisynth_driver = { |
445 | .driver = { | |
446 | .name = KBUILD_MODNAME, | |
447 | .probe = snd_seq_midisynth_probe, | |
448 | .remove = snd_seq_midisynth_remove, | |
449 | }, | |
450 | .id = SNDRV_SEQ_DEV_ID_MIDISYNTH, | |
451 | .argsize = 0, | |
452 | }; | |
1da177e4 | 453 | |
54a721ab | 454 | module_snd_seq_driver(seq_midisynth_driver); |