Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic MIDI synth driver for ALSA sequencer |
| 3 | * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl> |
| 4 | * Jaroslav Kysela <perex@suse.cz> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | Possible options for midisynth module: |
| 24 | - automatic opening of midi ports on first received event or subscription |
| 25 | (close will be performed when client leaves) |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include <sound/driver.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/moduleparam.h> |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 35 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <sound/core.h> |
| 37 | #include <sound/rawmidi.h> |
| 38 | #include <sound/seq_kernel.h> |
| 39 | #include <sound/seq_device.h> |
| 40 | #include <sound/seq_midi_event.h> |
| 41 | #include <sound/initval.h> |
| 42 | |
| 43 | MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@suse.cz>"); |
| 44 | MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth."); |
| 45 | MODULE_LICENSE("GPL"); |
| 46 | static int output_buffer_size = PAGE_SIZE; |
| 47 | module_param(output_buffer_size, int, 0644); |
| 48 | MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes."); |
| 49 | static int input_buffer_size = PAGE_SIZE; |
| 50 | module_param(input_buffer_size, int, 0644); |
| 51 | MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes."); |
| 52 | |
| 53 | /* data for this midi synth driver */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 54 | struct seq_midisynth { |
| 55 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | int device; |
| 57 | int subdevice; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 58 | struct snd_rawmidi_file input_rfile; |
| 59 | struct snd_rawmidi_file output_rfile; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | int seq_client; |
| 61 | int seq_port; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 62 | struct snd_midi_event *parser; |
| 63 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 65 | struct seq_midisynth_client { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | int seq_client; |
| 67 | int num_ports; |
| 68 | int ports_per_device[SNDRV_RAWMIDI_DEVICES]; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 69 | struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES]; |
| 70 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 72 | static struct seq_midisynth_client *synths[SNDRV_CARDS]; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 73 | static DEFINE_MUTEX(register_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | /* handle rawmidi input event (MIDI v1.0 stream) */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 76 | static void snd_midi_input_event(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 78 | struct snd_rawmidi_runtime *runtime; |
| 79 | struct seq_midisynth *msynth; |
| 80 | struct snd_seq_event ev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | char buf[16], *pbuf; |
| 82 | long res, count; |
| 83 | |
| 84 | if (substream == NULL) |
| 85 | return; |
| 86 | runtime = substream->runtime; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 87 | msynth = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (msynth == NULL) |
| 89 | return; |
| 90 | memset(&ev, 0, sizeof(ev)); |
| 91 | while (runtime->avail > 0) { |
| 92 | res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf)); |
| 93 | if (res <= 0) |
| 94 | continue; |
| 95 | if (msynth->parser == NULL) |
| 96 | continue; |
| 97 | pbuf = buf; |
| 98 | while (res > 0) { |
| 99 | count = snd_midi_event_encode(msynth->parser, pbuf, res, &ev); |
| 100 | if (count < 0) |
| 101 | break; |
| 102 | pbuf += count; |
| 103 | res -= count; |
| 104 | if (ev.type != SNDRV_SEQ_EVENT_NONE) { |
| 105 | ev.source.port = msynth->seq_port; |
| 106 | ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; |
| 107 | snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0); |
| 108 | /* clear event and reset header */ |
| 109 | memset(&ev, 0, sizeof(ev)); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 115 | static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 117 | struct snd_rawmidi_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | int tmp; |
| 119 | |
| 120 | snd_assert(substream != NULL || buf != NULL, return -EINVAL); |
| 121 | runtime = substream->runtime; |
| 122 | if ((tmp = runtime->avail) < count) { |
| 123 | snd_printd("warning, output event was lost (count = %i, available = %i)\n", count, tmp); |
| 124 | return -ENOMEM; |
| 125 | } |
| 126 | if (snd_rawmidi_kernel_write(substream, buf, count) < count) |
| 127 | return -EINVAL; |
| 128 | return 0; |
| 129 | } |
| 130 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 131 | static int event_process_midi(struct snd_seq_event *ev, int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | void *private_data, int atomic, int hop) |
| 133 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 134 | struct seq_midisynth *msynth = private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | unsigned char msg[10]; /* buffer for constructing midi messages */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 136 | struct snd_rawmidi_substream *substream; |
Clemens Ladisch | d06e4c4 | 2005-07-21 08:01:22 +0200 | [diff] [blame] | 137 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | snd_assert(msynth != NULL, return -EINVAL); |
| 140 | substream = msynth->output_rfile.output; |
| 141 | if (substream == NULL) |
| 142 | return -ENODEV; |
| 143 | if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */ |
| 144 | if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) { |
| 145 | /* invalid event */ |
| 146 | snd_printd("seq_midi: invalid sysex event flags = 0x%x\n", ev->flags); |
| 147 | return 0; |
| 148 | } |
Clemens Ladisch | d06e4c4 | 2005-07-21 08:01:22 +0200 | [diff] [blame] | 149 | snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | snd_midi_event_reset_decode(msynth->parser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } else { |
| 152 | if (msynth->parser == NULL) |
| 153 | return -EIO; |
Clemens Ladisch | d06e4c4 | 2005-07-21 08:01:22 +0200 | [diff] [blame] | 154 | len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev); |
| 155 | if (len < 0) |
| 156 | return 0; |
| 157 | if (dump_midi(substream, msg, len) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | snd_midi_event_reset_decode(msynth->parser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 164 | static int snd_seq_midisynth_new(struct seq_midisynth *msynth, |
| 165 | struct snd_card *card, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | int device, |
| 167 | int subdevice) |
| 168 | { |
| 169 | if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0) |
| 170 | return -ENOMEM; |
| 171 | msynth->card = card; |
| 172 | msynth->device = device; |
| 173 | msynth->subdevice = subdevice; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* open associated midi device for input */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 178 | static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
| 180 | int err; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 181 | struct seq_midisynth *msynth = private_data; |
| 182 | struct snd_rawmidi_runtime *runtime; |
| 183 | struct snd_rawmidi_params params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | /* open midi port */ |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 186 | if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device, |
| 187 | msynth->subdevice, |
| 188 | SNDRV_RAWMIDI_LFLG_INPUT, |
| 189 | &msynth->input_rfile)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | snd_printd("midi input open failed!!!\n"); |
| 191 | return err; |
| 192 | } |
| 193 | runtime = msynth->input_rfile.input->runtime; |
| 194 | memset(¶ms, 0, sizeof(params)); |
| 195 | params.avail_min = 1; |
| 196 | params.buffer_size = input_buffer_size; |
| 197 | if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, ¶ms)) < 0) { |
| 198 | snd_rawmidi_kernel_release(&msynth->input_rfile); |
| 199 | return err; |
| 200 | } |
| 201 | snd_midi_event_reset_encode(msynth->parser); |
| 202 | runtime->event = snd_midi_input_event; |
| 203 | runtime->private_data = msynth; |
| 204 | snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0); |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /* close associated midi device for input */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 209 | static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | int err; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 212 | struct seq_midisynth *msynth = private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | snd_assert(msynth->input_rfile.input != NULL, return -EINVAL); |
| 215 | err = snd_rawmidi_kernel_release(&msynth->input_rfile); |
| 216 | return err; |
| 217 | } |
| 218 | |
| 219 | /* open associated midi device for output */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 220 | static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | int err; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 223 | struct seq_midisynth *msynth = private_data; |
| 224 | struct snd_rawmidi_params params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | /* open midi port */ |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 227 | if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device, |
| 228 | msynth->subdevice, |
| 229 | SNDRV_RAWMIDI_LFLG_OUTPUT, |
| 230 | &msynth->output_rfile)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | snd_printd("midi output open failed!!!\n"); |
| 232 | return err; |
| 233 | } |
| 234 | memset(¶ms, 0, sizeof(params)); |
| 235 | params.avail_min = 1; |
| 236 | params.buffer_size = output_buffer_size; |
| 237 | if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, ¶ms)) < 0) { |
| 238 | snd_rawmidi_kernel_release(&msynth->output_rfile); |
| 239 | return err; |
| 240 | } |
| 241 | snd_midi_event_reset_decode(msynth->parser); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* close associated midi device for output */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 246 | static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 248 | struct seq_midisynth *msynth = private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | unsigned char buf = 0xff; /* MIDI reset */ |
| 250 | |
| 251 | snd_assert(msynth->output_rfile.output != NULL, return -EINVAL); |
| 252 | /* sending single MIDI reset message to shut the device up */ |
| 253 | snd_rawmidi_kernel_write(msynth->output_rfile.output, &buf, 1); |
| 254 | snd_rawmidi_drain_output(msynth->output_rfile.output); |
| 255 | return snd_rawmidi_kernel_release(&msynth->output_rfile); |
| 256 | } |
| 257 | |
| 258 | /* delete given midi synth port */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 259 | static void snd_seq_midisynth_delete(struct seq_midisynth *msynth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
| 261 | if (msynth == NULL) |
| 262 | return; |
| 263 | |
| 264 | if (msynth->seq_client > 0) { |
| 265 | /* delete port */ |
| 266 | snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port); |
| 267 | } |
| 268 | |
| 269 | if (msynth->parser) |
| 270 | snd_midi_event_free(msynth->parser); |
| 271 | } |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /* register new midi synth port */ |
| 274 | static int |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 275 | snd_seq_midisynth_register_port(struct snd_seq_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 277 | struct seq_midisynth_client *client; |
| 278 | struct seq_midisynth *msynth, *ms; |
| 279 | struct snd_seq_port_info *port; |
| 280 | struct snd_rawmidi_info *info; |
Clemens Ladisch | a7b928ac5 | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 281 | struct snd_rawmidi *rmidi = dev->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | int newclient = 0; |
| 283 | unsigned int p, ports; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 284 | struct snd_seq_port_callback pcallbacks; |
| 285 | struct snd_card *card = dev->card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | int device = dev->device; |
| 287 | unsigned int input_count = 0, output_count = 0; |
| 288 | |
| 289 | snd_assert(card != NULL && device >= 0 && device < SNDRV_RAWMIDI_DEVICES, return -EINVAL); |
| 290 | info = kmalloc(sizeof(*info), GFP_KERNEL); |
| 291 | if (! info) |
| 292 | return -ENOMEM; |
| 293 | info->device = device; |
| 294 | info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT; |
| 295 | info->subdevice = 0; |
| 296 | if (snd_rawmidi_info_select(card, info) >= 0) |
| 297 | output_count = info->subdevices_count; |
| 298 | info->stream = SNDRV_RAWMIDI_STREAM_INPUT; |
| 299 | if (snd_rawmidi_info_select(card, info) >= 0) { |
| 300 | input_count = info->subdevices_count; |
| 301 | } |
| 302 | ports = output_count; |
| 303 | if (ports < input_count) |
| 304 | ports = input_count; |
| 305 | if (ports == 0) { |
| 306 | kfree(info); |
| 307 | return -ENODEV; |
| 308 | } |
| 309 | if (ports > (256 / SNDRV_RAWMIDI_DEVICES)) |
| 310 | ports = 256 / SNDRV_RAWMIDI_DEVICES; |
| 311 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 312 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | client = synths[card->number]; |
| 314 | if (client == NULL) { |
| 315 | newclient = 1; |
Takashi Iwai | ecca82b | 2005-09-09 14:20:49 +0200 | [diff] [blame] | 316 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | if (client == NULL) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 318 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | kfree(info); |
| 320 | return -ENOMEM; |
| 321 | } |
Clemens Ladisch | 7b6d924 | 2005-12-12 09:33:37 +0100 | [diff] [blame] | 322 | client->seq_client = |
| 323 | snd_seq_create_kernel_client( |
Alan Horstmann | 78fc030 | 2006-04-21 08:39:20 +0200 | [diff] [blame] | 324 | card, 0, "%s", card->shortname[0] ? |
| 325 | (const char *)card->shortname : "External MIDI"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | if (client->seq_client < 0) { |
| 327 | kfree(client); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 328 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | kfree(info); |
| 330 | return -ENOMEM; |
| 331 | } |
Clemens Ladisch | 7b6d924 | 2005-12-12 09:33:37 +0100 | [diff] [blame] | 332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 334 | msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | port = kmalloc(sizeof(*port), GFP_KERNEL); |
| 336 | if (msynth == NULL || port == NULL) |
| 337 | goto __nomem; |
| 338 | |
| 339 | for (p = 0; p < ports; p++) { |
| 340 | ms = &msynth[p]; |
| 341 | |
| 342 | if (snd_seq_midisynth_new(ms, card, device, p) < 0) |
| 343 | goto __nomem; |
| 344 | |
| 345 | /* declare port */ |
| 346 | memset(port, 0, sizeof(*port)); |
| 347 | port->addr.client = client->seq_client; |
| 348 | port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p; |
| 349 | port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; |
| 350 | memset(info, 0, sizeof(*info)); |
| 351 | info->device = device; |
| 352 | if (p < output_count) |
| 353 | info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT; |
| 354 | else |
| 355 | info->stream = SNDRV_RAWMIDI_STREAM_INPUT; |
| 356 | info->subdevice = p; |
| 357 | if (snd_rawmidi_info_select(card, info) >= 0) |
| 358 | strcpy(port->name, info->subname); |
| 359 | if (! port->name[0]) { |
| 360 | if (info->name[0]) { |
| 361 | if (ports > 1) |
| 362 | snprintf(port->name, sizeof(port->name), "%s-%d", info->name, p); |
| 363 | else |
| 364 | snprintf(port->name, sizeof(port->name), "%s", info->name); |
| 365 | } else { |
| 366 | /* last resort */ |
| 367 | if (ports > 1) |
| 368 | sprintf(port->name, "MIDI %d-%d-%d", card->number, device, p); |
| 369 | else |
| 370 | sprintf(port->name, "MIDI %d-%d", card->number, device); |
| 371 | } |
| 372 | } |
| 373 | if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count) |
| 374 | port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE; |
| 375 | if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count) |
| 376 | port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ; |
| 377 | if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) && |
| 378 | info->flags & SNDRV_RAWMIDI_INFO_DUPLEX) |
| 379 | port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX; |
Clemens Ladisch | 450047a | 2006-05-02 16:08:41 +0200 | [diff] [blame] | 380 | port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
| 381 | | SNDRV_SEQ_PORT_TYPE_HARDWARE |
| 382 | | SNDRV_SEQ_PORT_TYPE_PORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | port->midi_channels = 16; |
| 384 | memset(&pcallbacks, 0, sizeof(pcallbacks)); |
| 385 | pcallbacks.owner = THIS_MODULE; |
| 386 | pcallbacks.private_data = ms; |
| 387 | pcallbacks.subscribe = midisynth_subscribe; |
| 388 | pcallbacks.unsubscribe = midisynth_unsubscribe; |
| 389 | pcallbacks.use = midisynth_use; |
| 390 | pcallbacks.unuse = midisynth_unuse; |
| 391 | pcallbacks.event_input = event_process_midi; |
| 392 | port->kernel = &pcallbacks; |
Clemens Ladisch | a7b928ac5 | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 393 | if (rmidi->ops && rmidi->ops->get_port_info) |
| 394 | rmidi->ops->get_port_info(rmidi, p, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0) |
| 396 | goto __nomem; |
| 397 | ms->seq_client = client->seq_client; |
| 398 | ms->seq_port = port->addr.port; |
| 399 | } |
| 400 | client->ports_per_device[device] = ports; |
| 401 | client->ports[device] = msynth; |
| 402 | client->num_ports++; |
| 403 | if (newclient) |
| 404 | synths[card->number] = client; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 405 | mutex_unlock(®ister_mutex); |
Takashi Iwai | 51f633d | 2005-03-30 13:49:06 +0200 | [diff] [blame] | 406 | kfree(info); |
| 407 | kfree(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | return 0; /* success */ |
| 409 | |
| 410 | __nomem: |
| 411 | if (msynth != NULL) { |
| 412 | for (p = 0; p < ports; p++) |
| 413 | snd_seq_midisynth_delete(&msynth[p]); |
| 414 | kfree(msynth); |
| 415 | } |
| 416 | if (newclient) { |
| 417 | snd_seq_delete_kernel_client(client->seq_client); |
| 418 | kfree(client); |
| 419 | } |
| 420 | kfree(info); |
| 421 | kfree(port); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 422 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return -ENOMEM; |
| 424 | } |
| 425 | |
| 426 | /* release midi synth port */ |
| 427 | static int |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 428 | snd_seq_midisynth_unregister_port(struct snd_seq_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 430 | struct seq_midisynth_client *client; |
| 431 | struct seq_midisynth *msynth; |
| 432 | struct snd_card *card = dev->card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | int device = dev->device, p, ports; |
| 434 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 435 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | client = synths[card->number]; |
| 437 | if (client == NULL || client->ports[device] == NULL) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 438 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | return -ENODEV; |
| 440 | } |
| 441 | ports = client->ports_per_device[device]; |
| 442 | client->ports_per_device[device] = 0; |
| 443 | msynth = client->ports[device]; |
| 444 | client->ports[device] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | for (p = 0; p < ports; p++) |
| 446 | snd_seq_midisynth_delete(&msynth[p]); |
| 447 | kfree(msynth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | client->num_ports--; |
| 449 | if (client->num_ports <= 0) { |
| 450 | snd_seq_delete_kernel_client(client->seq_client); |
| 451 | synths[card->number] = NULL; |
| 452 | kfree(client); |
| 453 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 454 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | static int __init alsa_seq_midi_init(void) |
| 460 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 461 | static struct snd_seq_dev_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | snd_seq_midisynth_register_port, |
| 463 | snd_seq_midisynth_unregister_port, |
| 464 | }; |
| 465 | memset(&synths, 0, sizeof(synths)); |
| 466 | snd_seq_autoload_lock(); |
| 467 | snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH, &ops, 0); |
| 468 | snd_seq_autoload_unlock(); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static void __exit alsa_seq_midi_exit(void) |
| 473 | { |
| 474 | snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH); |
| 475 | } |
| 476 | |
| 477 | module_init(alsa_seq_midi_init) |
| 478 | module_exit(alsa_seq_midi_exit) |