Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Routines for Gravis UltraSound soundcards - Synthesizer |
| 3 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> |
| 4 | * |
| 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 | #include <sound/driver.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/time.h> |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/gus.h> |
| 27 | #include <sound/seq_device.h> |
| 28 | |
| 29 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); |
| 30 | MODULE_DESCRIPTION("Routines for Gravis UltraSound soundcards - Synthesizer"); |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | |
| 33 | /* |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | static void snd_gus_synth_free_voices(snd_gus_card_t * gus, int client, int port) |
| 38 | { |
| 39 | int idx; |
| 40 | snd_gus_voice_t * voice; |
| 41 | |
| 42 | for (idx = 0; idx < 32; idx++) { |
| 43 | voice = &gus->gf1.voices[idx]; |
| 44 | if (voice->use && voice->client == client && voice->port == port) |
| 45 | snd_gf1_free_voice(gus, voice); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static int snd_gus_synth_use(void *private_data, snd_seq_port_subscribe_t *info) |
| 50 | { |
| 51 | snd_gus_port_t * port = (snd_gus_port_t *)private_data; |
| 52 | snd_gus_card_t * gus = port->gus; |
| 53 | snd_gus_voice_t * voice; |
| 54 | unsigned int idx; |
| 55 | |
| 56 | if (info->voices > 32) |
| 57 | return -EINVAL; |
| 58 | down(&gus->register_mutex); |
| 59 | if (!snd_gus_use_inc(gus)) { |
| 60 | up(&gus->register_mutex); |
| 61 | return -EFAULT; |
| 62 | } |
| 63 | for (idx = 0; idx < info->voices; idx++) { |
| 64 | voice = snd_gf1_alloc_voice(gus, SNDRV_GF1_VOICE_TYPE_SYNTH, info->sender.client, info->sender.port); |
| 65 | if (voice == NULL) { |
| 66 | snd_gus_synth_free_voices(gus, info->sender.client, info->sender.port); |
| 67 | snd_gus_use_dec(gus); |
| 68 | up(&gus->register_mutex); |
| 69 | return -EBUSY; |
| 70 | } |
| 71 | voice->index = idx; |
| 72 | } |
| 73 | up(&gus->register_mutex); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int snd_gus_synth_unuse(void *private_data, snd_seq_port_subscribe_t *info) |
| 78 | { |
| 79 | snd_gus_port_t * port = (snd_gus_port_t *)private_data; |
| 80 | snd_gus_card_t * gus = port->gus; |
| 81 | |
| 82 | down(&gus->register_mutex); |
| 83 | snd_gus_synth_free_voices(gus, info->sender.client, info->sender.port); |
| 84 | snd_gus_use_dec(gus); |
| 85 | up(&gus->register_mutex); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * |
| 91 | */ |
| 92 | |
| 93 | static void snd_gus_synth_free_private_instruments(snd_gus_port_t *p, int client) |
| 94 | { |
| 95 | snd_seq_instr_header_t ifree; |
| 96 | |
| 97 | memset(&ifree, 0, sizeof(ifree)); |
| 98 | ifree.cmd = SNDRV_SEQ_INSTR_FREE_CMD_PRIVATE; |
| 99 | snd_seq_instr_list_free_cond(p->gus->gf1.ilist, &ifree, client, 0); |
| 100 | } |
| 101 | |
Adrian Bunk | 209ac85 | 2005-05-23 10:29:53 +0200 | [diff] [blame] | 102 | static int snd_gus_synth_event_input(snd_seq_event_t *ev, int direct, |
| 103 | void *private_data, int atomic, int hop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | snd_gus_port_t * p = (snd_gus_port_t *) private_data; |
| 106 | |
| 107 | snd_assert(p != NULL, return -EINVAL); |
| 108 | if (ev->type >= SNDRV_SEQ_EVENT_SAMPLE && |
| 109 | ev->type <= SNDRV_SEQ_EVENT_SAMPLE_PRIVATE1) { |
| 110 | snd_gus_sample_event(ev, p); |
| 111 | return 0; |
| 112 | } |
| 113 | if (ev->source.client == SNDRV_SEQ_CLIENT_SYSTEM && |
| 114 | ev->source.port == SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE) { |
| 115 | if (ev->type == SNDRV_SEQ_EVENT_CLIENT_EXIT) { |
| 116 | snd_gus_synth_free_private_instruments(p, ev->data.addr.client); |
| 117 | return 0; |
| 118 | } |
| 119 | } |
| 120 | if (direct) { |
| 121 | if (ev->type >= SNDRV_SEQ_EVENT_INSTR_BEGIN) { |
| 122 | snd_seq_instr_event(&p->gus->gf1.iwffff_ops.kops, |
| 123 | p->gus->gf1.ilist, |
| 124 | ev, |
| 125 | p->gus->gf1.seq_client, |
| 126 | atomic, hop); |
| 127 | return 0; |
| 128 | } |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static void snd_gus_synth_instr_notify(void *private_data, |
| 134 | snd_seq_kinstr_t *instr, |
| 135 | int what) |
| 136 | { |
| 137 | unsigned int idx; |
| 138 | snd_gus_card_t *gus = private_data; |
| 139 | snd_gus_voice_t *pvoice; |
| 140 | unsigned long flags; |
| 141 | |
| 142 | spin_lock_irqsave(&gus->event_lock, flags); |
| 143 | for (idx = 0; idx < 32; idx++) { |
| 144 | pvoice = &gus->gf1.voices[idx]; |
| 145 | if (pvoice->use && !memcmp(&pvoice->instr, &instr->instr, sizeof(pvoice->instr))) { |
| 146 | if (pvoice->sample_ops && pvoice->sample_ops->sample_stop) { |
| 147 | pvoice->sample_ops->sample_stop(gus, pvoice, SAMPLE_STOP_IMMEDIATELY); |
| 148 | } else { |
| 149 | snd_gf1_stop_voice(gus, pvoice->number); |
| 150 | pvoice->flags &= ~SNDRV_GF1_VFLG_RUNNING; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | spin_unlock_irqrestore(&gus->event_lock, flags); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * |
| 159 | */ |
| 160 | |
| 161 | static void snd_gus_synth_free_port(void *private_data) |
| 162 | { |
| 163 | snd_gus_port_t * p = (snd_gus_port_t *)private_data; |
| 164 | |
| 165 | if (p) |
| 166 | snd_midi_channel_free_set(p->chset); |
| 167 | } |
| 168 | |
| 169 | static int snd_gus_synth_create_port(snd_gus_card_t * gus, int idx) |
| 170 | { |
| 171 | snd_gus_port_t * p; |
| 172 | snd_seq_port_callback_t callbacks; |
| 173 | char name[32]; |
| 174 | int result; |
| 175 | |
| 176 | p = &gus->gf1.seq_ports[idx]; |
| 177 | p->chset = snd_midi_channel_alloc_set(16); |
| 178 | if (p->chset == NULL) |
| 179 | return -ENOMEM; |
| 180 | p->chset->private_data = p; |
| 181 | p->gus = gus; |
| 182 | p->client = gus->gf1.seq_client; |
| 183 | |
| 184 | memset(&callbacks, 0, sizeof(callbacks)); |
| 185 | callbacks.owner = THIS_MODULE; |
| 186 | callbacks.use = snd_gus_synth_use; |
| 187 | callbacks.unuse = snd_gus_synth_unuse; |
| 188 | callbacks.event_input = snd_gus_synth_event_input; |
| 189 | callbacks.private_free = snd_gus_synth_free_port; |
| 190 | callbacks.private_data = p; |
| 191 | |
| 192 | sprintf(name, "%s port %i", gus->interwave ? "AMD InterWave" : "GF1", idx); |
| 193 | p->chset->port = snd_seq_event_port_attach(gus->gf1.seq_client, |
| 194 | &callbacks, |
| 195 | SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE, |
| 196 | SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE | |
| 197 | SNDRV_SEQ_PORT_TYPE_SYNTH, |
| 198 | 16, 0, |
| 199 | name); |
| 200 | if (p->chset->port < 0) { |
| 201 | result = p->chset->port; |
| 202 | snd_gus_synth_free_port(p); |
| 203 | return result; |
| 204 | } |
| 205 | p->port = p->chset->port; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * |
| 211 | */ |
| 212 | |
| 213 | static int snd_gus_synth_new_device(snd_seq_device_t *dev) |
| 214 | { |
| 215 | snd_gus_card_t *gus; |
| 216 | int client, i; |
| 217 | snd_seq_client_callback_t callbacks; |
| 218 | snd_seq_client_info_t *cinfo; |
| 219 | snd_seq_port_subscribe_t sub; |
| 220 | snd_iwffff_ops_t *iwops; |
| 221 | snd_gf1_ops_t *gf1ops; |
| 222 | snd_simple_ops_t *simpleops; |
| 223 | |
| 224 | gus = *(snd_gus_card_t**)SNDRV_SEQ_DEVICE_ARGPTR(dev); |
| 225 | if (gus == NULL) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | init_MUTEX(&gus->register_mutex); |
| 229 | gus->gf1.seq_client = -1; |
| 230 | |
| 231 | cinfo = kmalloc(sizeof(*cinfo), GFP_KERNEL); |
| 232 | if (! cinfo) |
| 233 | return -ENOMEM; |
| 234 | |
| 235 | /* allocate new client */ |
| 236 | memset(&callbacks, 0, sizeof(callbacks)); |
| 237 | callbacks.private_data = gus; |
| 238 | callbacks.allow_output = callbacks.allow_input = 1; |
| 239 | client = gus->gf1.seq_client = |
| 240 | snd_seq_create_kernel_client(gus->card, 1, &callbacks); |
| 241 | if (client < 0) { |
| 242 | kfree(cinfo); |
| 243 | return client; |
| 244 | } |
| 245 | |
| 246 | /* change name of client */ |
| 247 | memset(cinfo, 0, sizeof(*cinfo)); |
| 248 | cinfo->client = client; |
| 249 | cinfo->type = KERNEL_CLIENT; |
| 250 | sprintf(cinfo->name, gus->interwave ? "AMD InterWave" : "GF1"); |
| 251 | snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, cinfo); |
| 252 | kfree(cinfo); |
| 253 | |
| 254 | for (i = 0; i < 4; i++) |
| 255 | snd_gus_synth_create_port(gus, i); |
| 256 | |
| 257 | gus->gf1.ilist = snd_seq_instr_list_new(); |
| 258 | if (gus->gf1.ilist == NULL) { |
| 259 | snd_seq_delete_kernel_client(client); |
| 260 | gus->gf1.seq_client = -1; |
| 261 | return -ENOMEM; |
| 262 | } |
| 263 | gus->gf1.ilist->flags = SNDRV_SEQ_INSTR_FLG_DIRECT; |
| 264 | |
| 265 | simpleops = &gus->gf1.simple_ops; |
| 266 | snd_seq_simple_init(simpleops, gus, NULL); |
| 267 | simpleops->put_sample = snd_gus_simple_put_sample; |
| 268 | simpleops->get_sample = snd_gus_simple_get_sample; |
| 269 | simpleops->remove_sample = snd_gus_simple_remove_sample; |
| 270 | simpleops->notify = snd_gus_synth_instr_notify; |
| 271 | |
| 272 | gf1ops = &gus->gf1.gf1_ops; |
| 273 | snd_seq_gf1_init(gf1ops, gus, &simpleops->kops); |
| 274 | gf1ops->put_sample = snd_gus_gf1_put_sample; |
| 275 | gf1ops->get_sample = snd_gus_gf1_get_sample; |
| 276 | gf1ops->remove_sample = snd_gus_gf1_remove_sample; |
| 277 | gf1ops->notify = snd_gus_synth_instr_notify; |
| 278 | |
| 279 | iwops = &gus->gf1.iwffff_ops; |
| 280 | snd_seq_iwffff_init(iwops, gus, &gf1ops->kops); |
| 281 | iwops->put_sample = snd_gus_iwffff_put_sample; |
| 282 | iwops->get_sample = snd_gus_iwffff_get_sample; |
| 283 | iwops->remove_sample = snd_gus_iwffff_remove_sample; |
| 284 | iwops->notify = snd_gus_synth_instr_notify; |
| 285 | |
| 286 | memset(&sub, 0, sizeof(sub)); |
| 287 | sub.sender.client = SNDRV_SEQ_CLIENT_SYSTEM; |
| 288 | sub.sender.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE; |
| 289 | sub.dest.client = client; |
| 290 | sub.dest.port = 0; |
| 291 | snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &sub); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int snd_gus_synth_delete_device(snd_seq_device_t *dev) |
| 297 | { |
| 298 | snd_gus_card_t *gus; |
| 299 | |
| 300 | gus = *(snd_gus_card_t**)SNDRV_SEQ_DEVICE_ARGPTR(dev); |
| 301 | if (gus == NULL) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | if (gus->gf1.seq_client >= 0) { |
| 305 | snd_seq_delete_kernel_client(gus->gf1.seq_client); |
| 306 | gus->gf1.seq_client = -1; |
| 307 | } |
| 308 | if (gus->gf1.ilist) |
| 309 | snd_seq_instr_list_free(&gus->gf1.ilist); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int __init alsa_gus_synth_init(void) |
| 314 | { |
| 315 | static snd_seq_dev_ops_t ops = { |
| 316 | snd_gus_synth_new_device, |
| 317 | snd_gus_synth_delete_device |
| 318 | }; |
| 319 | |
| 320 | return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_GUS, &ops, |
| 321 | sizeof(snd_gus_card_t*)); |
| 322 | } |
| 323 | |
| 324 | static void __exit alsa_gus_synth_exit(void) |
| 325 | { |
| 326 | snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_GUS); |
| 327 | } |
| 328 | |
| 329 | module_init(alsa_gus_synth_init) |
| 330 | module_exit(alsa_gus_synth_exit) |