patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / core / seq / oss / seq_oss_init.c
1 /*
2  * OSS compatible sequencer driver
3  *
4  * open/close and reset interface
5  *
6  * Copyright (C) 1998-1999 Takashi Iwai <tiwai@suse.de>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include "seq_oss_device.h"
24 #include "seq_oss_synth.h"
25 #include "seq_oss_midi.h"
26 #include "seq_oss_writeq.h"
27 #include "seq_oss_readq.h"
28 #include "seq_oss_timer.h"
29 #include "seq_oss_event.h"
30 #include <linux/init.h>
31 #include <linux/moduleparam.h>
32
33 /*
34  * common variables
35  */
36 static int maxqlen = SNDRV_SEQ_OSS_MAX_QLEN;
37 module_param(maxqlen, int, 0444);
38 MODULE_PARM_DESC(maxqlen, "maximum queue length");
39
40 static int system_client = -1; /* ALSA sequencer client number */
41 static int system_port = -1;
42
43 static int num_clients;
44 static seq_oss_devinfo_t *client_table[SNDRV_SEQ_OSS_MAX_CLIENTS];
45
46
47 /*
48  * prototypes
49  */
50 static int receive_announce(snd_seq_event_t *ev, int direct, void *private, int atomic, int hop);
51 static int translate_mode(struct file *file);
52 static int create_port(seq_oss_devinfo_t *dp);
53 static int delete_port(seq_oss_devinfo_t *dp);
54 static int alloc_seq_queue(seq_oss_devinfo_t *dp);
55 static int delete_seq_queue(int queue);
56 static void free_devinfo(void *private);
57
58 #define call_ctl(type,rec) snd_seq_kernel_client_ctl(system_client, type, rec)
59
60
61 /*
62  * create sequencer client for OSS sequencer
63  */
64 int __init
65 snd_seq_oss_create_client(void)
66 {
67         int rc;
68         snd_seq_client_callback_t callback;
69         snd_seq_client_info_t info;
70         snd_seq_port_info_t port;
71         snd_seq_port_callback_t port_callback;
72
73         /* create ALSA client */
74         memset(&callback, 0, sizeof(callback));
75
76         callback.private_data = NULL;
77         callback.allow_input = 1;
78         callback.allow_output = 1;
79
80         rc = snd_seq_create_kernel_client(NULL, SNDRV_SEQ_CLIENT_OSS, &callback);
81         if (rc < 0)
82                 return rc;
83
84         system_client = rc;
85         debug_printk(("new client = %d\n", rc));
86
87         /* set client information */
88         memset(&info, 0, sizeof(info));
89         info.client = system_client;
90         info.type = KERNEL_CLIENT;
91         strcpy(info.name, "OSS sequencer");
92
93         rc = call_ctl(SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &info);
94
95         /* look up midi devices */
96         snd_seq_oss_midi_lookup_ports(system_client);
97
98         /* create annoucement receiver port */
99         memset(&port, 0, sizeof(port));
100         strcpy(port.name, "Receiver");
101         port.addr.client = system_client;
102         port.capability = SNDRV_SEQ_PORT_CAP_WRITE; /* receive only */
103         port.type = 0;
104
105         memset(&port_callback, 0, sizeof(port_callback));
106         /* don't set port_callback.owner here. otherwise the module counter
107          * is incremented and we can no longer release the module..
108          */
109         port_callback.event_input = receive_announce;
110         port.kernel = &port_callback;
111         
112         call_ctl(SNDRV_SEQ_IOCTL_CREATE_PORT, &port);
113         if ((system_port = port.addr.port) >= 0) {
114                 snd_seq_port_subscribe_t subs;
115
116                 memset(&subs, 0, sizeof(subs));
117                 subs.sender.client = SNDRV_SEQ_CLIENT_SYSTEM;
118                 subs.sender.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
119                 subs.dest.client = system_client;
120                 subs.dest.port = system_port;
121                 call_ctl(SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs);
122         }
123
124
125         return 0;
126 }
127
128
129 /*
130  * receive annoucement from system port, and check the midi device
131  */
132 static int
133 receive_announce(snd_seq_event_t *ev, int direct, void *private, int atomic, int hop)
134 {
135         snd_seq_port_info_t pinfo;
136
137         if (atomic)
138                 return 0; /* it must not happen */
139
140         switch (ev->type) {
141         case SNDRV_SEQ_EVENT_PORT_START:
142         case SNDRV_SEQ_EVENT_PORT_CHANGE:
143                 if (ev->data.addr.client == system_client)
144                         break; /* ignore myself */
145                 memset(&pinfo, 0, sizeof(pinfo));
146                 pinfo.addr = ev->data.addr;
147                 if (call_ctl(SNDRV_SEQ_IOCTL_GET_PORT_INFO, &pinfo) >= 0)
148                         snd_seq_oss_midi_check_new_port(&pinfo);
149                 break;
150
151         case SNDRV_SEQ_EVENT_PORT_EXIT:
152                 if (ev->data.addr.client == system_client)
153                         break; /* ignore myself */
154                 snd_seq_oss_midi_check_exit_port(ev->data.addr.client,
155                                                 ev->data.addr.port);
156                 break;
157         }
158         return 0;
159 }
160
161
162 /*
163  * delete OSS sequencer client
164  */
165 int
166 snd_seq_oss_delete_client(void)
167 {
168         if (system_client >= 0)
169                 snd_seq_delete_kernel_client(system_client);
170
171         snd_seq_oss_midi_clear_all();
172
173         return 0;
174 }
175
176
177 /*
178  * open sequencer device
179  */
180 int
181 snd_seq_oss_open(struct file *file, int level)
182 {
183         int i, rc;
184         seq_oss_devinfo_t *dp;
185
186         if ((dp = snd_kcalloc(sizeof(*dp), GFP_KERNEL)) == NULL) {
187                 snd_printk(KERN_ERR "can't malloc device info\n");
188                 return -ENOMEM;
189         }
190         debug_printk(("oss_open: dp = %p\n", dp));
191
192         for (i = 0; i < SNDRV_SEQ_OSS_MAX_CLIENTS; i++) {
193                 if (client_table[i] == NULL)
194                         break;
195         }
196         if (i >= SNDRV_SEQ_OSS_MAX_CLIENTS) {
197                 snd_printk(KERN_ERR "too many applications\n");
198                 kfree(dp);
199                 return -ENOMEM;
200         }
201
202         dp->index = i;
203         dp->cseq = system_client;
204         dp->port = -1;
205         dp->queue = -1;
206         dp->readq = NULL;
207         dp->writeq = NULL;
208
209         /* look up synth and midi devices */
210         snd_seq_oss_synth_setup(dp);
211         snd_seq_oss_midi_setup(dp);
212
213         if (dp->synth_opened == 0 && dp->max_mididev == 0) {
214                 snd_printk(KERN_ERR "no device found\n");
215                 rc = -ENODEV;
216                 goto _error;
217         }
218
219         /* create port */
220         debug_printk(("create new port\n"));
221         if ((rc = create_port(dp)) < 0) {
222                 snd_printk(KERN_ERR "can't create port\n");
223                 goto _error;
224         }
225
226         /* allocate queue */
227         debug_printk(("allocate queue\n"));
228         if ((rc = alloc_seq_queue(dp)) < 0)
229                 goto _error;
230
231         /* set address */
232         dp->addr.client = dp->cseq;
233         dp->addr.port = dp->port;
234         /*dp->addr.queue = dp->queue;*/
235         /*dp->addr.channel = 0;*/
236
237         dp->seq_mode = level;
238
239         /* set up file mode */
240         dp->file_mode = translate_mode(file);
241
242         /* initialize read queue */
243         debug_printk(("initialize read queue\n"));
244         if (is_read_mode(dp->file_mode)) {
245                 if ((dp->readq = snd_seq_oss_readq_new(dp, maxqlen)) == NULL) {
246                         rc = -ENOMEM;
247                         goto _error;
248                 }
249         }
250
251         /* initialize write queue */
252         debug_printk(("initialize write queue\n"));
253         if (is_write_mode(dp->file_mode)) {
254                 dp->writeq = snd_seq_oss_writeq_new(dp, maxqlen);
255                 if (dp->writeq == NULL) {
256                         rc = -ENOMEM;
257                         goto _error;
258                 }
259         }
260
261         /* initialize timer */
262         debug_printk(("initialize timer\n"));
263         if ((dp->timer = snd_seq_oss_timer_new(dp)) == NULL) {
264                 snd_printk(KERN_ERR "can't alloc timer\n");
265                 rc = -ENOMEM;
266                 goto _error;
267         }
268         debug_printk(("timer initialized\n"));
269
270         /* set private data pointer */
271         file->private_data = dp;
272
273         /* set up for mode2 */
274         if (level == SNDRV_SEQ_OSS_MODE_MUSIC)
275                 snd_seq_oss_synth_setup_midi(dp);
276         else if (is_read_mode(dp->file_mode))
277                 snd_seq_oss_midi_open_all(dp, SNDRV_SEQ_OSS_FILE_READ);
278
279         client_table[dp->index] = dp;
280         num_clients++;
281
282         debug_printk(("open done\n"));
283         return 0;
284
285  _error:
286         snd_seq_oss_synth_cleanup(dp);
287         snd_seq_oss_midi_cleanup(dp);
288         i = dp->queue;
289         delete_port(dp);
290         delete_seq_queue(i);
291
292         return rc;
293 }
294
295 /*
296  * translate file flags to private mode
297  */
298 static int
299 translate_mode(struct file *file)
300 {
301         int file_mode = 0;
302         if ((file->f_flags & O_ACCMODE) != O_RDONLY)
303                 file_mode |= SNDRV_SEQ_OSS_FILE_WRITE;
304         if ((file->f_flags & O_ACCMODE) != O_WRONLY)
305                 file_mode |= SNDRV_SEQ_OSS_FILE_READ;
306         if (file->f_flags & O_NONBLOCK)
307                 file_mode |= SNDRV_SEQ_OSS_FILE_NONBLOCK;
308         return file_mode;
309 }
310
311
312 /*
313  * create sequencer port
314  */
315 static int
316 create_port(seq_oss_devinfo_t *dp)
317 {
318         int rc;
319         snd_seq_port_info_t port;
320         snd_seq_port_callback_t callback;
321
322         memset(&port, 0, sizeof(port));
323         port.addr.client = dp->cseq;
324         sprintf(port.name, "Sequencer-%d", dp->index);
325         port.capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_WRITE; /* no subscription */
326         port.type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
327         port.midi_channels = 128;
328         port.synth_voices = 128;
329
330         memset(&callback, 0, sizeof(callback));
331         callback.owner = THIS_MODULE;
332         callback.private_data = dp;
333         callback.event_input = snd_seq_oss_event_input;
334         callback.private_free = free_devinfo;
335         port.kernel = &callback;
336
337         rc = call_ctl(SNDRV_SEQ_IOCTL_CREATE_PORT, &port);
338         if (rc < 0)
339                 return rc;
340
341         dp->port = port.addr.port;
342         debug_printk(("new port = %d\n", port.addr.port));
343
344         return 0;
345 }
346
347 /*
348  * delete ALSA port
349  */
350 static int
351 delete_port(seq_oss_devinfo_t *dp)
352 {
353         if (dp->port < 0)
354                 return 0;
355
356         debug_printk(("delete_port %i\n", dp->port));
357         return snd_seq_event_port_detach(dp->cseq, dp->port);
358 }
359
360 /*
361  * allocate a queue
362  */
363 static int
364 alloc_seq_queue(seq_oss_devinfo_t *dp)
365 {
366         snd_seq_queue_info_t qinfo;
367         int rc;
368
369         memset(&qinfo, 0, sizeof(qinfo));
370         qinfo.owner = system_client;
371         qinfo.locked = 1;
372         strcpy(qinfo.name, "OSS Sequencer Emulation");
373         if ((rc = call_ctl(SNDRV_SEQ_IOCTL_CREATE_QUEUE, &qinfo)) < 0)
374                 return rc;
375         dp->queue = qinfo.queue;
376         return 0;
377 }
378
379 /*
380  * release queue
381  */
382 static int
383 delete_seq_queue(int queue)
384 {
385         snd_seq_queue_info_t qinfo;
386         int rc;
387
388         if (queue < 0)
389                 return 0;
390         memset(&qinfo, 0, sizeof(qinfo));
391         qinfo.queue = queue;
392         rc = call_ctl(SNDRV_SEQ_IOCTL_DELETE_QUEUE, &qinfo);
393         if (rc < 0)
394                 printk(KERN_ERR "seq-oss: unable to delete queue %d (%d)\n", queue, rc);
395         return rc;
396 }
397
398
399 /*
400  * free device informations - private_free callback of port
401  */
402 static void
403 free_devinfo(void *private)
404 {
405         seq_oss_devinfo_t *dp = (seq_oss_devinfo_t *)private;
406
407         if (dp->timer)
408                 snd_seq_oss_timer_delete(dp->timer);
409                 
410         if (dp->writeq)
411                 snd_seq_oss_writeq_delete(dp->writeq);
412
413         if (dp->readq)
414                 snd_seq_oss_readq_delete(dp->readq);
415         
416         kfree(dp);
417 }
418
419
420 /*
421  * close sequencer device
422  */
423 void
424 snd_seq_oss_release(seq_oss_devinfo_t *dp)
425 {
426         int queue;
427
428         client_table[dp->index] = NULL;
429         num_clients--;
430
431         debug_printk(("resetting..\n"));
432         snd_seq_oss_reset(dp);
433
434         debug_printk(("cleaning up..\n"));
435         snd_seq_oss_synth_cleanup(dp);
436         snd_seq_oss_midi_cleanup(dp);
437
438         /* clear slot */
439         debug_printk(("releasing resource..\n"));
440         queue = dp->queue;
441         if (dp->port >= 0)
442                 delete_port(dp);
443         delete_seq_queue(queue);
444
445         debug_printk(("release done\n"));
446 }
447
448
449 /*
450  * Wait until the queue is empty (if we don't have nonblock)
451  */
452 void
453 snd_seq_oss_drain_write(seq_oss_devinfo_t *dp)
454 {
455         if (! dp->timer->running)
456                 return;
457         if (is_write_mode(dp->file_mode) && !is_nonblock_mode(dp->file_mode) &&
458             dp->writeq) {
459                 debug_printk(("syncing..\n"));
460                 while (snd_seq_oss_writeq_sync(dp->writeq))
461                         ;
462         }
463 }
464
465
466 /*
467  * reset sequencer devices
468  */
469 void
470 snd_seq_oss_reset(seq_oss_devinfo_t *dp)
471 {
472         int i;
473
474         /* reset all synth devices */
475         for (i = 0; i < dp->max_synthdev; i++)
476                 snd_seq_oss_synth_reset(dp, i);
477
478         /* reset all midi devices */
479         if (dp->seq_mode != SNDRV_SEQ_OSS_MODE_MUSIC) {
480                 for (i = 0; i < dp->max_mididev; i++)
481                         snd_seq_oss_midi_reset(dp, i);
482         }
483
484         /* remove queues */
485         if (dp->readq)
486                 snd_seq_oss_readq_clear(dp->readq);
487         if (dp->writeq)
488                 snd_seq_oss_writeq_clear(dp->writeq);
489
490         /* reset timer */
491         snd_seq_oss_timer_stop(dp->timer);
492 }
493
494 /*
495  * proc interface
496  */
497 void
498 snd_seq_oss_system_info_read(snd_info_buffer_t *buf)
499 {
500         int i;
501         seq_oss_devinfo_t *dp;
502
503         snd_iprintf(buf, "ALSA client number %d\n", system_client);
504         snd_iprintf(buf, "ALSA receiver port %d\n", system_port);
505
506         snd_iprintf(buf, "\nNumber of applications: %d\n", num_clients);
507         for (i = 0; i < num_clients; i++) {
508                 snd_iprintf(buf, "\nApplication %d: ", i);
509                 if ((dp = client_table[i]) == NULL) {
510                         snd_iprintf(buf, "*empty*\n");
511                         continue;
512                 }
513                 snd_iprintf(buf, "port %d : queue %d\n", dp->port, dp->queue);
514                 snd_iprintf(buf, "  sequencer mode = %s : file open mode = %s\n",
515                             (dp->seq_mode ? "music" : "synth"),
516                             filemode_str(dp->file_mode));
517                 if (dp->seq_mode)
518                         snd_iprintf(buf, "  timer tempo = %d, timebase = %d\n",
519                                     dp->timer->oss_tempo, dp->timer->oss_timebase);
520                 snd_iprintf(buf, "  max queue length %d\n", maxqlen);
521                 if (is_read_mode(dp->file_mode) && dp->readq)
522                         snd_seq_oss_readq_info_read(dp->readq, buf);
523         }
524 }
525
526 /*
527  * misc. functions for proc interface
528  */
529 char *
530 enabled_str(int bool)
531 {
532         return bool ? "enabled" : "disabled";
533 }
534
535 char *
536 filemode_str(int val)
537 {
538         static char *str[] = {
539                 "none", "read", "write", "read/write",
540         };
541         return str[val & SNDRV_SEQ_OSS_FILE_ACMODE];
542 }
543
544