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