2 * Hardware dependent layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
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>
33 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
34 MODULE_DESCRIPTION("Hardware dependent layer");
35 MODULE_LICENSE("GPL");
37 snd_hwdep_t *snd_hwdep_devices[SNDRV_CARDS * SNDRV_MINOR_HWDEPS];
39 static DECLARE_MUTEX(register_mutex);
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);
50 static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
52 snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
54 return hw->ops.llseek(hw, file, offset, orig);
58 static ssize_t snd_hwdep_read(struct file * file, char *buf, size_t count, loff_t *offset)
60 snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
62 return hw->ops.read(hw, buf, count, offset);
66 static ssize_t snd_hwdep_write(struct file * file, const char *buf, size_t count, loff_t *offset)
68 snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
70 return hw->ops.write(hw, buf, count, offset);
74 static int snd_hwdep_open(struct inode *inode, struct file * file)
76 int major = imajor(inode);
84 case CONFIG_SND_MAJOR:
85 cardnum = SNDRV_MINOR_CARD(iminor(inode));
86 device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_HWDEP;
88 #ifdef CONFIG_SND_OSSEMUL
90 cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
97 cardnum %= SNDRV_CARDS;
98 device %= SNDRV_MINOR_HWDEPS;
99 hw = snd_hwdep_devices[(cardnum * SNDRV_MINOR_HWDEPS) + device];
105 #ifdef CONFIG_SND_OSSEMUL
106 if (major == SOUND_MAJOR && hw->oss_type < 0)
110 if (!try_module_get(hw->card->module))
113 init_waitqueue_entry(&wait, current);
114 add_wait_queue(&hw->open_wait, &wait);
115 down(&hw->open_mutex);
117 if (hw->exclusive && hw->used > 0) {
121 err = hw->ops.open(hw, file);
124 if (err == -EAGAIN) {
125 if (file->f_flags & O_NONBLOCK) {
131 set_current_state(TASK_INTERRUPTIBLE);
134 down(&hw->open_mutex);
135 if (signal_pending(current)) {
140 remove_wait_queue(&hw->open_wait, &wait);
142 err = snd_card_file_add(hw->card, file);
144 file->private_data = hw;
148 hw->ops.release(hw, file);
153 module_put(hw->card->module);
157 static int snd_hwdep_release(struct inode *inode, struct file * file)
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);
168 snd_card_file_remove(hw->card, file);
170 module_put(hw->card->module);
174 static unsigned int snd_hwdep_poll(struct file * file, poll_table * wait)
176 snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return 0);
178 return hw->ops.poll(hw, file, wait);
182 static int snd_hwdep_info(snd_hwdep_t *hw, snd_hwdep_info_t *_info)
184 snd_hwdep_info_t info;
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)))
196 static int snd_hwdep_dsp_status(snd_hwdep_t *hw, snd_hwdep_dsp_status_t *_info)
198 snd_hwdep_dsp_status_t info;
201 if (! hw->ops.dsp_status)
203 memset(&info, 0, sizeof(info));
204 info.dsp_loaded = hw->dsp_loaded;
205 if ((err = hw->ops.dsp_status(hw, &info)) < 0)
207 if (copy_to_user(_info, &info, sizeof(info)))
212 static int snd_hwdep_dsp_load(snd_hwdep_t *hw, snd_hwdep_dsp_image_t *_info)
214 snd_hwdep_dsp_image_t info;
217 if (! hw->ops.dsp_load)
219 memset(&info, 0, sizeof(info));
220 if (copy_from_user(&info, _info, sizeof(info)))
222 /* check whether the dsp was already loaded */
223 if (hw->dsp_loaded & (1 << info.index))
225 if (verify_area(VERIFY_READ, info.image, info.length))
227 err = hw->ops.dsp_load(hw, &info);
230 hw->dsp_loaded |= (1 << info.index);
234 static int snd_hwdep_ioctl(struct inode *inode, struct file * file,
235 unsigned int cmd, unsigned long arg)
237 snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
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);
249 return hw->ops.ioctl(hw, file, cmd, arg);
253 static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
255 snd_hwdep_t *hw = snd_magic_cast(snd_hwdep_t, file->private_data, return -ENXIO);
257 return hw->ops.mmap(hw, file, vma);
261 static int snd_hwdep_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
262 unsigned int cmd, unsigned long arg)
266 tmp = card->number * SNDRV_MINOR_HWDEPS;
268 case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
272 if (get_user(device, (int *)arg))
274 device = device < 0 ? 0 : device + 1;
275 while (device < SNDRV_MINOR_HWDEPS) {
276 if (snd_hwdep_devices[tmp + device])
280 if (device >= SNDRV_MINOR_HWDEPS)
282 if (put_user(device, (int *)arg))
286 case SNDRV_CTL_IOCTL_HWDEP_INFO:
288 snd_hwdep_info_t *info = (snd_hwdep_info_t *)arg;
292 if (get_user(device, &info->device))
294 if (device < 0 || device >= SNDRV_MINOR_HWDEPS)
296 hwdep = snd_hwdep_devices[tmp + device];
299 return snd_hwdep_info(hwdep, info);
309 static struct file_operations snd_hwdep_f_ops =
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,
322 static snd_minor_t snd_hwdep_reg =
324 .comment = "hardware dependent",
325 .f_ops = &snd_hwdep_f_ops,
329 * snd_hwdep_new - create a new hwdep instance
330 * @card: the card instance
332 * @device: the device index (zero-based)
333 * @rhwdep: the pointer to store the new hwdep instance
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.
339 * Returns zero if successful, or a negative error code on failure.
341 int snd_hwdep_new(snd_card_t * card, char *id, int device, snd_hwdep_t ** rhwdep)
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
351 snd_assert(rhwdep != NULL, return -EINVAL);
353 snd_assert(card != NULL, return -ENXIO);
354 hwdep = snd_magic_kcalloc(snd_hwdep_t, 0, GFP_KERNEL);
358 hwdep->device = device;
360 strlcpy(hwdep->id, id, sizeof(hwdep->id));
362 #ifdef CONFIG_SND_OSSEMUL
363 hwdep->oss_type = -1;
365 if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
366 snd_hwdep_free(hwdep);
369 init_waitqueue_head(&hwdep->open_wait);
370 init_MUTEX(&hwdep->open_mutex);
375 static int snd_hwdep_free(snd_hwdep_t *hwdep)
377 snd_assert(hwdep != NULL, return -ENXIO);
378 if (hwdep->private_free)
379 hwdep->private_free(hwdep);
380 snd_magic_kfree(hwdep);
384 static int snd_hwdep_dev_free(snd_device_t *device)
386 snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
387 return snd_hwdep_free(hwdep);
390 static int snd_hwdep_dev_register(snd_device_t *device)
392 snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
396 down(®ister_mutex);
397 idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
398 if (snd_hwdep_devices[idx]) {
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;
413 #ifdef CONFIG_SND_OSSEMUL
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");
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);
433 static int snd_hwdep_dev_unregister(snd_device_t *device)
435 snd_hwdep_t *hwdep = snd_magic_cast(snd_hwdep_t, device->device_data, return -ENXIO);
438 snd_assert(hwdep != NULL, return -ENXIO);
439 down(®ister_mutex);
440 idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
441 if (snd_hwdep_devices[idx] != hwdep) {
445 #ifdef CONFIG_SND_OSSEMUL
447 snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
449 snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
450 snd_hwdep_devices[idx] = NULL;
452 return snd_hwdep_free(hwdep);
459 static void snd_hwdep_proc_read(snd_info_entry_t *entry,
460 snd_info_buffer_t * buffer)
465 down(®ister_mutex);
466 for (idx = 0; idx < SNDRV_CARDS * SNDRV_MINOR_HWDEPS; idx++) {
467 hwdep = snd_hwdep_devices[idx];
470 snd_iprintf(buffer, "%02i-%02i: %s\n",
471 idx / SNDRV_MINOR_HWDEPS,
472 idx % SNDRV_MINOR_HWDEPS,
482 static snd_info_entry_t *snd_hwdep_proc_entry = NULL;
484 static int __init alsa_hwdep_init(void)
486 snd_info_entry_t *entry;
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);
497 snd_hwdep_proc_entry = entry;
498 snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
502 static void __exit alsa_hwdep_exit(void)
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;
511 module_init(alsa_hwdep_init)
512 module_exit(alsa_hwdep_exit)
514 EXPORT_SYMBOL(snd_hwdep_new);