Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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  
102 int snd_gus_synth_event_input(snd_seq_event_t *ev, int direct, void *private_data, int atomic, int hop)
103 {
104         snd_gus_port_t * p = (snd_gus_port_t *) private_data;
105         
106         snd_assert(p != NULL, return -EINVAL);
107         if (ev->type >= SNDRV_SEQ_EVENT_SAMPLE &&
108             ev->type <= SNDRV_SEQ_EVENT_SAMPLE_PRIVATE1) {
109                 snd_gus_sample_event(ev, p);
110                 return 0;
111         }
112         if (ev->source.client == SNDRV_SEQ_CLIENT_SYSTEM &&
113             ev->source.port == SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE) {
114                 if (ev->type == SNDRV_SEQ_EVENT_CLIENT_EXIT) {
115                         snd_gus_synth_free_private_instruments(p, ev->data.addr.client);
116                         return 0;
117                 }
118         }
119         if (direct) {
120                 if (ev->type >= SNDRV_SEQ_EVENT_INSTR_BEGIN) {
121                         snd_seq_instr_event(&p->gus->gf1.iwffff_ops.kops,
122                                             p->gus->gf1.ilist,
123                                             ev,
124                                             p->gus->gf1.seq_client,
125                                             atomic, hop);
126                         return 0;
127                 }
128         }
129         return 0;
130 }
131 
132 static void snd_gus_synth_instr_notify(void *private_data,
133                                        snd_seq_kinstr_t *instr,
134                                        int what)
135 {
136         unsigned int idx;
137         snd_gus_card_t *gus = private_data;
138         snd_gus_voice_t *pvoice;
139         unsigned long flags;
140         
141         spin_lock_irqsave(&gus->event_lock, flags);
142         for (idx = 0; idx < 32; idx++) {
143                 pvoice = &gus->gf1.voices[idx];
144                 if (pvoice->use && !memcmp(&pvoice->instr, &instr->instr, sizeof(pvoice->instr))) {
145                         if (pvoice->sample_ops && pvoice->sample_ops->sample_stop) {
146                                 pvoice->sample_ops->sample_stop(gus, pvoice, SAMPLE_STOP_IMMEDIATELY);
147                         } else {
148                                 snd_gf1_stop_voice(gus, pvoice->number);
149                                 pvoice->flags &= ~SNDRV_GF1_VFLG_RUNNING;
150                         }
151                 }
152         }
153         spin_unlock_irqrestore(&gus->event_lock, flags);
154 }
155 
156 /*
157  *
158  */
159 
160 static void snd_gus_synth_free_port(void *private_data)
161 {
162         snd_gus_port_t * p = (snd_gus_port_t *)private_data;
163         
164         if (p)
165                 snd_midi_channel_free_set(p->chset);
166 }
167 
168 static int snd_gus_synth_create_port(snd_gus_card_t * gus, int idx)
169 {
170         snd_gus_port_t * p;
171         snd_seq_port_callback_t callbacks;
172         char name[32];
173         int result;
174         
175         p = &gus->gf1.seq_ports[idx];
176         p->chset = snd_midi_channel_alloc_set(16);
177         if (p->chset == NULL)
178                 return -ENOMEM;
179         p->chset->private_data = p;
180         p->gus = gus;
181         p->client = gus->gf1.seq_client;
182 
183         memset(&callbacks, 0, sizeof(callbacks));
184         callbacks.owner = THIS_MODULE;
185         callbacks.use = snd_gus_synth_use;
186         callbacks.unuse = snd_gus_synth_unuse;
187         callbacks.event_input = snd_gus_synth_event_input;
188         callbacks.private_free = snd_gus_synth_free_port;
189         callbacks.private_data = p;
190         
191         sprintf(name, "%s port %i", gus->interwave ? "AMD InterWave" : "GF1", idx);
192         p->chset->port = snd_seq_event_port_attach(gus->gf1.seq_client,
193                                                    &callbacks,
194                                                    SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
195                                                    SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
196                                                    SNDRV_SEQ_PORT_TYPE_SYNTH,
197                                                    16, 0,
198                                                    name);
199         if (p->chset->port < 0) {
200                 result = p->chset->port;
201                 snd_gus_synth_free_port(p);
202                 return result;
203         }
204         p->port = p->chset->port;
205         return 0;
206 }                                                
207 
208 /*
209  *
210  */
211 
212 static int snd_gus_synth_new_device(snd_seq_device_t *dev)
213 {
214         snd_gus_card_t *gus;
215         int client, i;
216         snd_seq_client_callback_t callbacks;
217         snd_seq_client_info_t cinfo;
218         snd_seq_port_subscribe_t sub;
219         snd_iwffff_ops_t *iwops;
220         snd_gf1_ops_t *gf1ops;
221         snd_simple_ops_t *simpleops;
222 
223         gus = *(snd_gus_card_t**)SNDRV_SEQ_DEVICE_ARGPTR(dev);
224         if (gus == NULL)
225                 return -EINVAL;
226 
227         init_MUTEX(&gus->register_mutex);
228         gus->gf1.seq_client = -1;
229         
230         /* allocate new client */
231         memset(&callbacks, 0, sizeof(callbacks));
232         callbacks.private_data = gus;
233         callbacks.allow_output = callbacks.allow_input = 1;
234         client = gus->gf1.seq_client =
235                         snd_seq_create_kernel_client(gus->card, 1, &callbacks);
236         if (client < 0)
237                 return client;
238 
239         /* change name of client */
240         memset(&cinfo, 0, sizeof(cinfo));
241         cinfo.client = client;
242         cinfo.type = KERNEL_CLIENT;
243         sprintf(cinfo.name, gus->interwave ? "AMD InterWave" : "GF1");
244         snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
245 
246         for (i = 0; i < 4; i++)
247                 snd_gus_synth_create_port(gus, i);
248                 
249         gus->gf1.ilist = snd_seq_instr_list_new();
250         if (gus->gf1.ilist == NULL) {
251                 snd_seq_delete_kernel_client(client);   
252                 gus->gf1.seq_client = -1;
253                 return -ENOMEM;
254         }
255         gus->gf1.ilist->flags = SNDRV_SEQ_INSTR_FLG_DIRECT;
256 
257         simpleops = &gus->gf1.simple_ops;
258         snd_seq_simple_init(simpleops, gus, NULL);
259         simpleops->put_sample = snd_gus_simple_put_sample;
260         simpleops->get_sample = snd_gus_simple_get_sample;
261         simpleops->remove_sample = snd_gus_simple_remove_sample;
262         simpleops->notify = snd_gus_synth_instr_notify;
263 
264         gf1ops = &gus->gf1.gf1_ops;
265         snd_seq_gf1_init(gf1ops, gus, &simpleops->kops);
266         gf1ops->put_sample = snd_gus_gf1_put_sample;
267         gf1ops->get_sample = snd_gus_gf1_get_sample;
268         gf1ops->remove_sample = snd_gus_gf1_remove_sample;
269         gf1ops->notify = snd_gus_synth_instr_notify;
270 
271         iwops = &gus->gf1.iwffff_ops;
272         snd_seq_iwffff_init(iwops, gus, &gf1ops->kops);
273         iwops->put_sample = snd_gus_iwffff_put_sample;
274         iwops->get_sample = snd_gus_iwffff_get_sample;
275         iwops->remove_sample = snd_gus_iwffff_remove_sample;
276         iwops->notify = snd_gus_synth_instr_notify;
277 
278         memset(&sub, 0, sizeof(sub));
279         sub.sender.client = SNDRV_SEQ_CLIENT_SYSTEM;
280         sub.sender.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
281         sub.dest.client = client;
282         sub.dest.port = 0;
283         snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &sub);
284 
285         return 0;
286 }
287 
288 static int snd_gus_synth_delete_device(snd_seq_device_t *dev)
289 {
290         snd_gus_card_t *gus;
291 
292         gus = *(snd_gus_card_t**)SNDRV_SEQ_DEVICE_ARGPTR(dev);
293         if (gus == NULL)
294                 return -EINVAL;
295 
296         if (gus->gf1.seq_client >= 0) {
297                 snd_seq_delete_kernel_client(gus->gf1.seq_client);      
298                 gus->gf1.seq_client = -1;
299         }
300         if (gus->gf1.ilist)
301                 snd_seq_instr_list_free(&gus->gf1.ilist);
302         return 0;
303 }
304 
305 static int __init alsa_gus_synth_init(void)
306 {
307         static snd_seq_dev_ops_t ops = {
308                 snd_gus_synth_new_device,
309                 snd_gus_synth_delete_device
310         };
311 
312         return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_GUS, &ops,
313                                               sizeof(snd_gus_card_t*));
314 }
315 
316 static void __exit alsa_gus_synth_exit(void)
317 {
318         snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_GUS);
319 }
320 
321 module_init(alsa_gus_synth_init)
322 module_exit(alsa_gus_synth_exit)
323 
  This page was automatically generated by the LXR engine.