patch-2_6_7-vs1_9_1_12
[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 __user *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 __user *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 __user *_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 __user *_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 __user *_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         void __user *argp = (void __user *)arg;
239         switch (cmd) {
240         case SNDRV_HWDEP_IOCTL_PVERSION:
241                 return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);
242         case SNDRV_HWDEP_IOCTL_INFO:
243                 return snd_hwdep_info(hw, argp);
244         case SNDRV_HWDEP_IOCTL_DSP_STATUS:
245                 return snd_hwdep_dsp_status(hw, argp);
246         case SNDRV_HWDEP_IOCTL_DSP_LOAD:
247                 return snd_hwdep_dsp_load(hw, argp);
248         }
249         if (hw->ops.ioctl)
250                 return hw->ops.ioctl(hw, file, cmd, arg);
251         return -ENOTTY;
252 }
253
254 static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
255 {
256         snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
257         if (hw->ops.mmap)
258                 return hw->ops.mmap(hw, file, vma);
259         return -ENXIO;
260 }
261
262 static int snd_hwdep_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
263                                    unsigned int cmd, unsigned long arg)
264 {
265         unsigned int tmp;
266         
267         tmp = card->number * SNDRV_MINOR_HWDEPS;
268         switch (cmd) {
269         case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
270                 {
271                         int device;
272
273                         if (get_user(device, (int __user *)arg))
274                                 return -EFAULT;
275                         device = device < 0 ? 0 : device + 1;
276                         while (device < SNDRV_MINOR_HWDEPS) {
277                                 if (snd_hwdep_devices[tmp + device])
278                                         break;
279                                 device++;
280                         }
281                         if (device >= SNDRV_MINOR_HWDEPS)
282                                 device = -1;
283                         if (put_user(device, (int __user *)arg))
284                                 return -EFAULT;
285                         return 0;
286                 }
287         case SNDRV_CTL_IOCTL_HWDEP_INFO:
288                 {
289                         snd_hwdep_info_t __user *info = (snd_hwdep_info_t __user *)arg;
290                         int device;
291                         snd_hwdep_t *hwdep;
292
293                         if (get_user(device, &info->device))
294                                 return -EFAULT;
295                         if (device < 0 || device >= SNDRV_MINOR_HWDEPS)
296                                 return -ENXIO;
297                         hwdep = snd_hwdep_devices[tmp + device];
298                         if (hwdep == NULL)
299                                 return -ENXIO;
300                         return snd_hwdep_info(hwdep, info);
301                 }
302         }
303         return -ENOIOCTLCMD;
304 }
305
306 /*
307
308  */
309
310 static struct file_operations snd_hwdep_f_ops =
311 {
312         .owner =        THIS_MODULE,
313         .llseek =       snd_hwdep_llseek,
314         .read =         snd_hwdep_read,
315         .write =        snd_hwdep_write,
316         .open =         snd_hwdep_open,
317         .release =      snd_hwdep_release,
318         .poll =         snd_hwdep_poll,
319         .ioctl =        snd_hwdep_ioctl,
320         .mmap =         snd_hwdep_mmap,
321 };
322
323 static snd_minor_t snd_hwdep_reg =
324 {
325         .comment =      "hardware dependent",
326         .f_ops =        &snd_hwdep_f_ops,
327 };
328
329 /**
330  * snd_hwdep_new - create a new hwdep instance
331  * @card: the card instance
332  * @id: the id string
333  * @device: the device index (zero-based)
334  * @rhwdep: the pointer to store the new hwdep instance
335  *
336  * Creates a new hwdep instance with the given index on the card.
337  * The callbacks (hwdep->ops) must be set on the returned instance
338  * after this call manually by the caller.
339  *
340  * Returns zero if successful, or a negative error code on failure.
341  */
342 int snd_hwdep_new(snd_card_t * card, char *id, int device, snd_hwdep_t ** rhwdep)
343 {
344         snd_hwdep_t *hwdep;
345         int err;
346         static snd_device_ops_t ops = {
347                 .dev_free = snd_hwdep_dev_free,
348                 .dev_register = snd_hwdep_dev_register,
349                 .dev_unregister = snd_hwdep_dev_unregister
350         };
351
352         snd_assert(rhwdep != NULL, return -EINVAL);
353         *rhwdep = NULL;
354         snd_assert(card != NULL, return -ENXIO);
355         hwdep = snd_magic_kcalloc(snd_hwdep_t, 0, GFP_KERNEL);
356         if (hwdep == NULL)
357                 return -ENOMEM;
358         hwdep->card = card;
359         hwdep->device = device;
360         if (id) {
361                 strlcpy(hwdep->id, id, sizeof(hwdep->id));
362         }
363 #ifdef CONFIG_SND_OSSEMUL
364         hwdep->oss_type = -1;
365 #endif
366         if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
367                 snd_hwdep_free(hwdep);
368                 return err;
369         }
370         init_waitqueue_head(&hwdep->open_wait);
371         init_MUTEX(&hwdep->open_mutex);
372         *rhwdep = hwdep;
373         return 0;
374 }
375
376 static int snd_hwdep_free(snd_hwdep_t *hwdep)
377 {
378         snd_assert(hwdep != NULL, return -ENXIO);
379         if (hwdep->private_free)
380                 hwdep->private_free(hwdep);
381         snd_magic_kfree(hwdep);
382         return 0;
383 }
384
385 static int snd_hwdep_dev_free(snd_device_t *device)
386 {
387         snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
388         return snd_hwdep_free(hwdep);
389 }
390
391 static int snd_hwdep_dev_register(snd_device_t *device)
392 {
393         snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
394         int idx, err;
395         char name[32];
396
397         down(&register_mutex);
398         idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
399         if (snd_hwdep_devices[idx]) {
400                 up(&register_mutex);
401                 return -EBUSY;
402         }
403         snd_hwdep_devices[idx] = hwdep;
404         sprintf(name, "hwC%iD%i", hwdep->card->number, hwdep->device);
405         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
406                                        hwdep->card, hwdep->device,
407                                        &snd_hwdep_reg, name)) < 0) {
408                 snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
409                            hwdep->card->number, hwdep->device);
410                 snd_hwdep_devices[idx] = NULL;
411                 up(&register_mutex);
412                 return err;
413         }
414 #ifdef CONFIG_SND_OSSEMUL
415         hwdep->ossreg = 0;
416         if (hwdep->oss_type >= 0) {
417                 if ((hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM) && (hwdep->device != 0)) {
418                         snd_printk (KERN_WARNING "only hwdep device 0 can be registered as OSS direct FM device!\n");
419                 } else {
420                         if (snd_register_oss_device(hwdep->oss_type,
421                                                     hwdep->card, hwdep->device,
422                                                     &snd_hwdep_reg, hwdep->oss_dev) < 0) {
423                                 snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
424                                            hwdep->card->number, hwdep->device);
425                         } else
426                                 hwdep->ossreg = 1;
427                 }
428         }
429 #endif
430         up(&register_mutex);
431         return 0;
432 }
433
434 static int snd_hwdep_dev_unregister(snd_device_t *device)
435 {
436         snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
437         int idx;
438
439         snd_assert(hwdep != NULL, return -ENXIO);
440         down(&register_mutex);
441         idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
442         if (snd_hwdep_devices[idx] != hwdep) {
443                 up(&register_mutex);
444                 return -EINVAL;
445         }
446 #ifdef CONFIG_SND_OSSEMUL
447         if (hwdep->ossreg)
448                 snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
449 #endif
450         snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
451         snd_hwdep_devices[idx] = NULL;
452         up(&register_mutex);
453         return snd_hwdep_free(hwdep);
454 }
455
456 /*
457  *  Info interface
458  */
459
460 static void snd_hwdep_proc_read(snd_info_entry_t *entry,
461                                 snd_info_buffer_t * buffer)
462 {
463         int idx;
464         snd_hwdep_t *hwdep;
465
466         down(&register_mutex);
467         for (idx = 0; idx < SNDRV_CARDS * SNDRV_MINOR_HWDEPS; idx++) {
468                 hwdep = snd_hwdep_devices[idx];
469                 if (hwdep == NULL)
470                         continue;
471                 snd_iprintf(buffer, "%02i-%02i: %s\n",
472                                         idx / SNDRV_MINOR_HWDEPS,
473                                         idx % SNDRV_MINOR_HWDEPS,
474                                         hwdep->name);
475         }
476         up(&register_mutex);
477 }
478
479 /*
480  *  ENTRY functions
481  */
482
483 static snd_info_entry_t *snd_hwdep_proc_entry = NULL;
484
485 static int __init alsa_hwdep_init(void)
486 {
487         snd_info_entry_t *entry;
488
489         memset(snd_hwdep_devices, 0, sizeof(snd_hwdep_devices));
490         if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
491                 entry->c.text.read_size = 512;
492                 entry->c.text.read = snd_hwdep_proc_read;
493                 if (snd_info_register(entry) < 0) {
494                         snd_info_free_entry(entry);
495                         entry = NULL;
496                 }
497         }
498         snd_hwdep_proc_entry = entry;
499         snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
500         return 0;
501 }
502
503 static void __exit alsa_hwdep_exit(void)
504 {
505         snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
506         if (snd_hwdep_proc_entry) {
507                 snd_info_unregister(snd_hwdep_proc_entry);
508                 snd_hwdep_proc_entry = NULL;
509         }
510 }
511
512 module_init(alsa_hwdep_init)
513 module_exit(alsa_hwdep_exit)
514
515 EXPORT_SYMBOL(snd_hwdep_new);