ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / hwdep.c
1 /*
2  *  Hardware dependent layer
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/major.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <sound/core.h>
28 #include <sound/control.h>
29 #include <sound/minors.h>
30 #include <sound/hwdep.h>
31 #include <sound/info.h>
32
33 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
34 MODULE_DESCRIPTION("Hardware dependent layer");
35 MODULE_LICENSE("GPL");
36
37 snd_hwdep_t *snd_hwdep_devices[SNDRV_CARDS * SNDRV_MINOR_HWDEPS];
38
39 static DECLARE_MUTEX(register_mutex);
40
41 static int snd_hwdep_free(snd_hwdep_t *hwdep);
42 static int snd_hwdep_dev_free(snd_device_t *device);
43 static int snd_hwdep_dev_register(snd_device_t *device);
44 static int snd_hwdep_dev_unregister(snd_device_t *device);
45
46 /*
47
48  */
49
50 static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
51 {
52         snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
53         if (hw->ops.llseek)
54                 return hw->ops.llseek(hw, file, offset, orig);
55         return -ENXIO;
56 }
57
58 static ssize_t snd_hwdep_read(struct file * file, char *buf, size_t count, loff_t *offset)
59 {
60         snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
61         if (hw->ops.read)
62                 return hw->ops.read(hw, buf, count, offset);
63         return -ENXIO;  
64 }
65
66 static ssize_t snd_hwdep_write(struct file * file, const char *buf, size_t count, loff_t *offset)
67 {
68         snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
69         if (hw->ops.write)
70                 return hw->ops.write(hw, buf, count, offset);
71         return -ENXIO;  
72 }
73
74 static int snd_hwdep_open(struct inode *inode, struct file * file)
75 {
76         int major = imajor(inode);
77         int cardnum;
78         int device;
79         snd_hwdep_t *hw;
80         int err;
81         wait_queue_t wait;
82
83         switch (major) {
84         case CONFIG_SND_MAJOR:
85                 cardnum = SNDRV_MINOR_CARD(iminor(inode));
86                 device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_HWDEP;
87                 break;
88 #ifdef CONFIG_SND_OSSEMUL
89         case SOUND_MAJOR:
90                 cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
91                 device = 0;
92                 break;
93 #endif
94         default:
95                 return -ENXIO;
96         }
97         cardnum %= SNDRV_CARDS;
98         device %= SNDRV_MINOR_HWDEPS;
99         hw = snd_hwdep_devices[(cardnum * SNDRV_MINOR_HWDEPS) + device];
100         if (hw == NULL)
101                 return -ENODEV;
102
103         if (!hw->ops.open)
104                 return -ENXIO;
105 #ifdef CONFIG_SND_OSSEMUL
106         if (major == SOUND_MAJOR && hw->oss_type < 0)
107                 return -ENXIO;
108 #endif
109
110         if (!try_module_get(hw->card->module))
111                 return -EFAULT;
112
113         init_waitqueue_entry(&wait, current);
114         add_wait_queue(&hw->open_wait, &wait);
115         down(&hw->open_mutex);
116         while (1) {
117                 if (hw->exclusive && hw->used > 0) {
118                         err = -EBUSY;
119                         break;
120                 }
121                 err = hw->ops.open(hw, file);
122                 if (err >= 0)
123                         break;
124                 if (err == -EAGAIN) {
125                         if (file->f_flags & O_NONBLOCK) {
126                                 err = -EBUSY;
127                                 break;
128                         }
129                 } else
130                         break;
131                 set_current_state(TASK_INTERRUPTIBLE);
132                 up(&hw->open_mutex);
133                 schedule();
134                 down(&hw->open_mutex);
135                 if (signal_pending(current)) {
136                         err = -ERESTARTSYS;
137                         break;
138                 }
139         }
140         remove_wait_queue(&hw->open_wait, &wait);
141         if (err >= 0) {
142                 err = snd_card_file_add(hw->card, file);
143                 if (err >= 0) {
144                         file->private_data = hw;
145                         hw->used++;
146                 } else {
147                         if (hw->ops.release)
148                                 hw->ops.release(hw, file);
149                 }
150         }
151         up(&hw->open_mutex);
152         if (err < 0)
153                 module_put(hw->card->module);
154         return err;
155 }
156
157 static int snd_hwdep_release(struct inode *inode, struct file * file)
158 {
159         int err = -ENXIO;
160         snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
161         down(&hw->open_mutex);
162         if (hw->ops.release) {
163                 err = hw->ops.release(hw, file);
164                 wake_up(&hw->open_wait);
165         }
166         if (hw->used > 0)
167                 hw->used--;
168         snd_card_file_remove(hw->card, file);
169         up(&hw->open_mutex);
170         module_put(hw->card->module);
171         return err;
172 }
173
174 static unsigned int snd_hwdep_poll(struct file * file, poll_table * wait)
175 {
176         snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return 0);
177         if (hw->ops.poll)
178                 return hw->ops.poll(hw, file, wait);
179         return 0;
180 }
181
182 static int snd_hwdep_info(snd_hwdep_t *hw, snd_hwdep_info_t *_info)
183 {
184         snd_hwdep_info_t info;
185         
186         memset(&info, 0, sizeof(info));
187         info.card = hw->card->number;
188         strlcpy(info.id, hw->id, sizeof(info.id));      
189         strlcpy(info.name, hw->name, sizeof(info.name));
190         info.iface = hw->iface;
191         if (copy_to_user(_info, &info, sizeof(info)))
192                 return -EFAULT;
193         return 0;
194 }
195
196 static int snd_hwdep_dsp_status(snd_hwdep_t *hw, snd_hwdep_dsp_status_t *_info)
197 {
198         snd_hwdep_dsp_status_t info;
199         int err;
200         
201         if (! hw->ops.dsp_status)
202                 return -ENXIO;
203         memset(&info, 0, sizeof(info));
204         info.dsp_loaded = hw->dsp_loaded;
205         if ((err = hw->ops.dsp_status(hw, &info)) < 0)
206                 return err;
207         if (copy_to_user(_info, &info, sizeof(info)))
208                 return -EFAULT;
209         return 0;
210 }
211
212 static int snd_hwdep_dsp_load(snd_hwdep_t *hw, snd_hwdep_dsp_image_t *_info)
213 {
214         snd_hwdep_dsp_image_t info;
215         int err;
216         
217         if (! hw->ops.dsp_load)
218                 return -ENXIO;
219         memset(&info, 0, sizeof(info));
220         if (copy_from_user(&info, _info, sizeof(info)))
221                 return -EFAULT;
222         /* check whether the dsp was already loaded */
223         if (hw->dsp_loaded & (1 << info.index))
224                 return -EBUSY;
225         if (verify_area(VERIFY_READ, info.image, info.length))
226                 return -EFAULT;
227         err = hw->ops.dsp_load(hw, &info);
228         if (err < 0)
229                 return err;
230         hw->dsp_loaded |= (1 << info.index);
231         return 0;
232 }
233
234 static int snd_hwdep_ioctl(struct inode *inode, struct file * file,
235                            unsigned int cmd, unsigned long arg)
236 {
237         snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
238         switch (cmd) {
239         case SNDRV_HWDEP_IOCTL_PVERSION:
240                 return put_user(SNDRV_HWDEP_VERSION, (int *)arg);
241         case SNDRV_HWDEP_IOCTL_INFO:
242                 return snd_hwdep_info(hw, (snd_hwdep_info_t *)arg);
243         case SNDRV_HWDEP_IOCTL_DSP_STATUS:
244                 return snd_hwdep_dsp_status(hw, (snd_hwdep_dsp_status_t *)arg);
245         case SNDRV_HWDEP_IOCTL_DSP_LOAD:
246                 return snd_hwdep_dsp_load(hw, (snd_hwdep_dsp_image_t *)arg);
247         }
248         if (hw->ops.ioctl)
249                 return hw->ops.ioctl(hw, file, cmd, arg);
250         return -ENOTTY;
251 }
252
253 static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
254 {
255         snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
256         if (hw->ops.mmap)
257                 return hw->ops.mmap(hw, file, vma);
258         return -ENXIO;
259 }
260
261 static int snd_hwdep_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
262                                    unsigned int cmd, unsigned long arg)
263 {
264         unsigned int tmp;
265         
266         tmp = card->number * SNDRV_MINOR_HWDEPS;
267         switch (cmd) {
268         case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
269                 {
270                         int device;
271
272                         if (get_user(device, (int *)arg))
273                                 return -EFAULT;
274                         device = device < 0 ? 0 : device + 1;
275                         while (device < SNDRV_MINOR_HWDEPS) {
276                                 if (snd_hwdep_devices[tmp + device])
277                                         break;
278                                 device++;
279                         }
280                         if (device >= SNDRV_MINOR_HWDEPS)
281                                 device = -1;
282                         if (put_user(device, (int *)arg))
283                                 return -EFAULT;
284                         return 0;
285                 }
286         case SNDRV_CTL_IOCTL_HWDEP_INFO:
287                 {
288                         snd_hwdep_info_t *info = (snd_hwdep_info_t *)arg;
289                         int device;
290                         snd_hwdep_t *hwdep;
291
292                         if (get_user(device, &info->device))
293                                 return -EFAULT;
294                         if (device < 0 || device >= SNDRV_MINOR_HWDEPS)
295                                 return -ENXIO;
296                         hwdep = snd_hwdep_devices[tmp + device];
297                         if (hwdep == NULL)
298                                 return -ENXIO;
299                         return snd_hwdep_info(hwdep, info);
300                 }
301         }
302         return -ENOIOCTLCMD;
303 }
304
305 /*
306
307  */
308
309 static struct file_operations snd_hwdep_f_ops =
310 {
311         .owner =        THIS_MODULE,
312         .llseek =       snd_hwdep_llseek,
313         .read =         snd_hwdep_read,
314         .write =        snd_hwdep_write,
315         .open =         snd_hwdep_open,
316         .release =      snd_hwdep_release,
317         .poll =         snd_hwdep_poll,
318         .ioctl =        snd_hwdep_ioctl,
319         .mmap =         snd_hwdep_mmap,
320 };
321
322 static snd_minor_t snd_hwdep_reg =
323 {
324         .comment =      "hardware dependent",
325         .f_ops =        &snd_hwdep_f_ops,
326 };
327
328 /**
329  * snd_hwdep_new - create a new hwdep instance
330  * @card: the card instance
331  * @id: the id string
332  * @device: the device index (zero-based)
333  * @rhwdep: the pointer to store the new hwdep instance
334  *
335  * Creates a new hwdep instance with the given index on the card.
336  * The callbacks (hwdep->ops) must be set on the returned instance
337  * after this call manually by the caller.
338  *
339  * Returns zero if successful, or a negative error code on failure.
340  */
341 int snd_hwdep_new(snd_card_t * card, char *id, int device, snd_hwdep_t ** rhwdep)
342 {
343         snd_hwdep_t *hwdep;
344         int err;
345         static snd_device_ops_t ops = {
346                 .dev_free = snd_hwdep_dev_free,
347                 .dev_register = snd_hwdep_dev_register,
348                 .dev_unregister = snd_hwdep_dev_unregister
349         };
350
351         snd_assert(rhwdep != NULL, return -EINVAL);
352         *rhwdep = NULL;
353         snd_assert(card != NULL, return -ENXIO);
354         hwdep = snd_magic_kcalloc(snd_hwdep_t, 0, GFP_KERNEL);
355         if (hwdep == NULL)
356                 return -ENOMEM;
357         hwdep->card = card;
358         hwdep->device = device;
359         if (id) {
360                 strlcpy(hwdep->id, id, sizeof(hwdep->id));
361         }
362 #ifdef CONFIG_SND_OSSEMUL
363         hwdep->oss_type = -1;
364 #endif
365         if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
366                 snd_hwdep_free(hwdep);
367                 return err;
368         }
369         init_waitqueue_head(&hwdep->open_wait);
370         init_MUTEX(&hwdep->open_mutex);
371         *rhwdep = hwdep;
372         return 0;
373 }
374
375 static int snd_hwdep_free(snd_hwdep_t *hwdep)
376 {
377         snd_assert(hwdep != NULL, return -ENXIO);
378         if (hwdep->private_free)
379                 hwdep->private_free(hwdep);
380         snd_magic_kfree(hwdep);
381         return 0;
382 }
383
384 static int snd_hwdep_dev_free(snd_device_t *device)
385 {
386         snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
387         return snd_hwdep_free(hwdep);
388 }
389
390 static int snd_hwdep_dev_register(snd_device_t *device)
391 {
392         snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
393         int idx, err;
394         char name[32];
395
396         down(&register_mutex);
397         idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
398         if (snd_hwdep_devices[idx]) {
399                 up(&register_mutex);
400                 return -EBUSY;
401         }
402         snd_hwdep_devices[idx] = hwdep;
403         sprintf(name, "hwC%iD%i", hwdep->card->number, hwdep->device);
404         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
405                                        hwdep->card, hwdep->device,
406                                        &snd_hwdep_reg, name)) < 0) {
407                 snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
408                            hwdep->card->number, hwdep->device);
409                 snd_hwdep_devices[idx] = NULL;
410                 up(&register_mutex);
411                 return err;
412         }
413 #ifdef CONFIG_SND_OSSEMUL
414         hwdep->ossreg = 0;
415         if (hwdep->oss_type >= 0) {
416                 if ((hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM) && (hwdep->device != 0)) {
417                         snd_printk (KERN_WARNING "only hwdep device 0 can be registered as OSS direct FM device!\n");
418                 } else {
419                         if (snd_register_oss_device(hwdep->oss_type,
420                                                     hwdep->card, hwdep->device,
421                                                     &snd_hwdep_reg, hwdep->oss_dev) < 0) {
422                                 snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
423                                            hwdep->card->number, hwdep->device);
424                         } else
425                                 hwdep->ossreg = 1;
426                 }
427         }
428 #endif
429         up(&register_mutex);
430         return 0;
431 }
432
433 static int snd_hwdep_dev_unregister(snd_device_t *device)
434 {
435         snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
436         int idx;
437
438         snd_assert(hwdep != NULL, return -ENXIO);
439         down(&register_mutex);
440         idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
441         if (snd_hwdep_devices[idx] != hwdep) {
442                 up(&register_mutex);
443                 return -EINVAL;
444         }
445 #ifdef CONFIG_SND_OSSEMUL
446         if (hwdep->ossreg)
447                 snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
448 #endif
449         snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
450         snd_hwdep_devices[idx] = NULL;
451         up(&register_mutex);
452         return snd_hwdep_free(hwdep);
453 }
454
455 /*
456  *  Info interface
457  */
458
459 static void snd_hwdep_proc_read(snd_info_entry_t *entry,
460                                 snd_info_buffer_t * buffer)
461 {
462         int idx;
463         snd_hwdep_t *hwdep;
464
465         down(&register_mutex);
466         for (idx = 0; idx < SNDRV_CARDS * SNDRV_MINOR_HWDEPS; idx++) {
467                 hwdep = snd_hwdep_devices[idx];
468                 if (hwdep == NULL)
469                         continue;
470                 snd_iprintf(buffer, "%02i-%02i: %s\n",
471                                         idx / SNDRV_MINOR_HWDEPS,
472                                         idx % SNDRV_MINOR_HWDEPS,
473                                         hwdep->name);
474         }
475         up(&register_mutex);
476 }
477
478 /*
479  *  ENTRY functions
480  */
481
482 static snd_info_entry_t *snd_hwdep_proc_entry = NULL;
483
484 static int __init alsa_hwdep_init(void)
485 {
486         snd_info_entry_t *entry;
487
488         memset(snd_hwdep_devices, 0, sizeof(snd_hwdep_devices));
489         if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
490                 entry->c.text.read_size = 512;
491                 entry->c.text.read = snd_hwdep_proc_read;
492                 if (snd_info_register(entry) < 0) {
493                         snd_info_free_entry(entry);
494                         entry = NULL;
495                 }
496         }
497         snd_hwdep_proc_entry = entry;
498         snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
499         return 0;
500 }
501
502 static void __exit alsa_hwdep_exit(void)
503 {
504         snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
505         if (snd_hwdep_proc_entry) {
506                 snd_info_unregister(snd_hwdep_proc_entry);
507                 snd_hwdep_proc_entry = NULL;
508         }
509 }
510
511 module_init(alsa_hwdep_init)
512 module_exit(alsa_hwdep_exit)
513
514 EXPORT_SYMBOL(snd_hwdep_new);