11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Hardware dependent layer
4c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds
71da177e4SLinus Torvalds #include <linux/major.h>
81da177e4SLinus Torvalds #include <linux/init.h>
91da177e4SLinus Torvalds #include <linux/slab.h>
101da177e4SLinus Torvalds #include <linux/time.h>
111a60d4c5SIngo Molnar #include <linux/mutex.h>
12da155d5bSPaul Gortmaker #include <linux/module.h>
13174cd4b1SIngo Molnar #include <linux/sched/signal.h>
141da177e4SLinus Torvalds #include <sound/core.h>
151da177e4SLinus Torvalds #include <sound/control.h>
161da177e4SLinus Torvalds #include <sound/minors.h>
171da177e4SLinus Torvalds #include <sound/hwdep.h>
181da177e4SLinus Torvalds #include <sound/info.h>
191da177e4SLinus Torvalds
20c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
211da177e4SLinus Torvalds MODULE_DESCRIPTION("Hardware dependent layer");
221da177e4SLinus Torvalds MODULE_LICENSE("GPL");
231da177e4SLinus Torvalds
24f87135f5SClemens Ladisch static LIST_HEAD(snd_hwdep_devices);
251a60d4c5SIngo Molnar static DEFINE_MUTEX(register_mutex);
261da177e4SLinus Torvalds
27d9a98de2STakashi Iwai static int snd_hwdep_dev_free(struct snd_device *device);
28d9a98de2STakashi Iwai static int snd_hwdep_dev_register(struct snd_device *device);
29c461482cSTakashi Iwai static int snd_hwdep_dev_disconnect(struct snd_device *device);
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds
snd_hwdep_search(struct snd_card * card,int device)32f87135f5SClemens Ladisch static struct snd_hwdep *snd_hwdep_search(struct snd_card *card, int device)
33f87135f5SClemens Ladisch {
34f87135f5SClemens Ladisch struct snd_hwdep *hwdep;
35f87135f5SClemens Ladisch
369244b2c3SJohannes Berg list_for_each_entry(hwdep, &snd_hwdep_devices, list)
37f87135f5SClemens Ladisch if (hwdep->card == card && hwdep->device == device)
38f87135f5SClemens Ladisch return hwdep;
39f87135f5SClemens Ladisch return NULL;
40f87135f5SClemens Ladisch }
411da177e4SLinus Torvalds
snd_hwdep_llseek(struct file * file,loff_t offset,int orig)421da177e4SLinus Torvalds static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
431da177e4SLinus Torvalds {
44d9a98de2STakashi Iwai struct snd_hwdep *hw = file->private_data;
451da177e4SLinus Torvalds if (hw->ops.llseek)
461da177e4SLinus Torvalds return hw->ops.llseek(hw, file, offset, orig);
471da177e4SLinus Torvalds return -ENXIO;
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds
snd_hwdep_read(struct file * file,char __user * buf,size_t count,loff_t * offset)50d9a98de2STakashi Iwai static ssize_t snd_hwdep_read(struct file * file, char __user *buf,
51d9a98de2STakashi Iwai size_t count, loff_t *offset)
521da177e4SLinus Torvalds {
53d9a98de2STakashi Iwai struct snd_hwdep *hw = file->private_data;
541da177e4SLinus Torvalds if (hw->ops.read)
551da177e4SLinus Torvalds return hw->ops.read(hw, buf, count, offset);
561da177e4SLinus Torvalds return -ENXIO;
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds
snd_hwdep_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)59d9a98de2STakashi Iwai static ssize_t snd_hwdep_write(struct file * file, const char __user *buf,
60d9a98de2STakashi Iwai size_t count, loff_t *offset)
611da177e4SLinus Torvalds {
62d9a98de2STakashi Iwai struct snd_hwdep *hw = file->private_data;
631da177e4SLinus Torvalds if (hw->ops.write)
641da177e4SLinus Torvalds return hw->ops.write(hw, buf, count, offset);
651da177e4SLinus Torvalds return -ENXIO;
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds
snd_hwdep_open(struct inode * inode,struct file * file)681da177e4SLinus Torvalds static int snd_hwdep_open(struct inode *inode, struct file * file)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds int major = imajor(inode);
71d9a98de2STakashi Iwai struct snd_hwdep *hw;
721da177e4SLinus Torvalds int err;
73ac6424b9SIngo Molnar wait_queue_entry_t wait;
741da177e4SLinus Torvalds
75f1902860SClemens Ladisch if (major == snd_major) {
76f87135f5SClemens Ladisch hw = snd_lookup_minor_data(iminor(inode),
77f87135f5SClemens Ladisch SNDRV_DEVICE_TYPE_HWDEP);
781da177e4SLinus Torvalds #ifdef CONFIG_SND_OSSEMUL
79f1902860SClemens Ladisch } else if (major == SOUND_MAJOR) {
80f87135f5SClemens Ladisch hw = snd_lookup_oss_minor_data(iminor(inode),
81f87135f5SClemens Ladisch SNDRV_OSS_DEVICE_TYPE_DMFM);
821da177e4SLinus Torvalds #endif
83f1902860SClemens Ladisch } else
841da177e4SLinus Torvalds return -ENXIO;
851da177e4SLinus Torvalds if (hw == NULL)
861da177e4SLinus Torvalds return -ENODEV;
871da177e4SLinus Torvalds
88a0830dbdSTakashi Iwai if (!try_module_get(hw->card->module)) {
89a0830dbdSTakashi Iwai snd_card_unref(hw->card);
901da177e4SLinus Torvalds return -EFAULT;
91a0830dbdSTakashi Iwai }
921da177e4SLinus Torvalds
931da177e4SLinus Torvalds init_waitqueue_entry(&wait, current);
941da177e4SLinus Torvalds add_wait_queue(&hw->open_wait, &wait);
951a60d4c5SIngo Molnar mutex_lock(&hw->open_mutex);
961da177e4SLinus Torvalds while (1) {
971da177e4SLinus Torvalds if (hw->exclusive && hw->used > 0) {
981da177e4SLinus Torvalds err = -EBUSY;
991da177e4SLinus Torvalds break;
1001da177e4SLinus Torvalds }
101345d0b19STakashi Iwai if (!hw->ops.open) {
102345d0b19STakashi Iwai err = 0;
103345d0b19STakashi Iwai break;
104345d0b19STakashi Iwai }
1051da177e4SLinus Torvalds err = hw->ops.open(hw, file);
1061da177e4SLinus Torvalds if (err >= 0)
1071da177e4SLinus Torvalds break;
1081da177e4SLinus Torvalds if (err == -EAGAIN) {
1091da177e4SLinus Torvalds if (file->f_flags & O_NONBLOCK) {
1101da177e4SLinus Torvalds err = -EBUSY;
1111da177e4SLinus Torvalds break;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds } else
1141da177e4SLinus Torvalds break;
1151da177e4SLinus Torvalds set_current_state(TASK_INTERRUPTIBLE);
1161a60d4c5SIngo Molnar mutex_unlock(&hw->open_mutex);
1171da177e4SLinus Torvalds schedule();
1181a60d4c5SIngo Molnar mutex_lock(&hw->open_mutex);
1190914f796STakashi Iwai if (hw->card->shutdown) {
1200914f796STakashi Iwai err = -ENODEV;
1210914f796STakashi Iwai break;
1220914f796STakashi Iwai }
1231da177e4SLinus Torvalds if (signal_pending(current)) {
1241da177e4SLinus Torvalds err = -ERESTARTSYS;
1251da177e4SLinus Torvalds break;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds remove_wait_queue(&hw->open_wait, &wait);
1291da177e4SLinus Torvalds if (err >= 0) {
1301da177e4SLinus Torvalds err = snd_card_file_add(hw->card, file);
1311da177e4SLinus Torvalds if (err >= 0) {
1321da177e4SLinus Torvalds file->private_data = hw;
1331da177e4SLinus Torvalds hw->used++;
1341da177e4SLinus Torvalds } else {
1351da177e4SLinus Torvalds if (hw->ops.release)
1361da177e4SLinus Torvalds hw->ops.release(hw, file);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds }
1391a60d4c5SIngo Molnar mutex_unlock(&hw->open_mutex);
1401da177e4SLinus Torvalds if (err < 0)
1411da177e4SLinus Torvalds module_put(hw->card->module);
142a0830dbdSTakashi Iwai snd_card_unref(hw->card);
1431da177e4SLinus Torvalds return err;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds
snd_hwdep_release(struct inode * inode,struct file * file)1461da177e4SLinus Torvalds static int snd_hwdep_release(struct inode *inode, struct file * file)
1471da177e4SLinus Torvalds {
148345d0b19STakashi Iwai int err = 0;
149d9a98de2STakashi Iwai struct snd_hwdep *hw = file->private_data;
150104326f8SFlorin Malita struct module *mod = hw->card->module;
1518fa58af7SKarsten Wiese
152e6684d08STakashi Iwai scoped_guard(mutex, &hw->open_mutex) {
1538fa58af7SKarsten Wiese if (hw->ops.release)
1541da177e4SLinus Torvalds err = hw->ops.release(hw, file);
1551da177e4SLinus Torvalds if (hw->used > 0)
1561da177e4SLinus Torvalds hw->used--;
157e6684d08STakashi Iwai }
1588fa58af7SKarsten Wiese wake_up(&hw->open_wait);
1598fa58af7SKarsten Wiese
1608fa58af7SKarsten Wiese snd_card_file_remove(hw->card, file);
161104326f8SFlorin Malita module_put(mod);
1621da177e4SLinus Torvalds return err;
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds
snd_hwdep_poll(struct file * file,poll_table * wait)165680ef72aSAl Viro static __poll_t snd_hwdep_poll(struct file * file, poll_table * wait)
1661da177e4SLinus Torvalds {
167d9a98de2STakashi Iwai struct snd_hwdep *hw = file->private_data;
1681da177e4SLinus Torvalds if (hw->ops.poll)
1691da177e4SLinus Torvalds return hw->ops.poll(hw, file, wait);
1701da177e4SLinus Torvalds return 0;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds
snd_hwdep_info(struct snd_hwdep * hw,struct snd_hwdep_info __user * _info)173d9a98de2STakashi Iwai static int snd_hwdep_info(struct snd_hwdep *hw,
174d9a98de2STakashi Iwai struct snd_hwdep_info __user *_info)
1751da177e4SLinus Torvalds {
176d9a98de2STakashi Iwai struct snd_hwdep_info info;
1771da177e4SLinus Torvalds
1781da177e4SLinus Torvalds memset(&info, 0, sizeof(info));
1791da177e4SLinus Torvalds info.card = hw->card->number;
18075b1a8f9SJoe Perches strscpy(info.id, hw->id, sizeof(info.id));
18175b1a8f9SJoe Perches strscpy(info.name, hw->name, sizeof(info.name));
1821da177e4SLinus Torvalds info.iface = hw->iface;
1831da177e4SLinus Torvalds if (copy_to_user(_info, &info, sizeof(info)))
1841da177e4SLinus Torvalds return -EFAULT;
1851da177e4SLinus Torvalds return 0;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds
snd_hwdep_dsp_status(struct snd_hwdep * hw,struct snd_hwdep_dsp_status __user * _info)188d9a98de2STakashi Iwai static int snd_hwdep_dsp_status(struct snd_hwdep *hw,
189d9a98de2STakashi Iwai struct snd_hwdep_dsp_status __user *_info)
1901da177e4SLinus Torvalds {
191d9a98de2STakashi Iwai struct snd_hwdep_dsp_status info;
1921da177e4SLinus Torvalds int err;
1931da177e4SLinus Torvalds
1941da177e4SLinus Torvalds if (! hw->ops.dsp_status)
1951da177e4SLinus Torvalds return -ENXIO;
1961da177e4SLinus Torvalds memset(&info, 0, sizeof(info));
1971da177e4SLinus Torvalds info.dsp_loaded = hw->dsp_loaded;
198e3ded899STakashi Iwai err = hw->ops.dsp_status(hw, &info);
199e3ded899STakashi Iwai if (err < 0)
2001da177e4SLinus Torvalds return err;
2011da177e4SLinus Torvalds if (copy_to_user(_info, &info, sizeof(info)))
2021da177e4SLinus Torvalds return -EFAULT;
2031da177e4SLinus Torvalds return 0;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds
snd_hwdep_dsp_load(struct snd_hwdep * hw,struct snd_hwdep_dsp_image * info)206d9a98de2STakashi Iwai static int snd_hwdep_dsp_load(struct snd_hwdep *hw,
20718d122c0SArnd Bergmann struct snd_hwdep_dsp_image *info)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds int err;
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds if (! hw->ops.dsp_load)
2121da177e4SLinus Torvalds return -ENXIO;
21318d122c0SArnd Bergmann if (info->index >= 32)
214c4fd4379SDan Carpenter return -EINVAL;
2151da177e4SLinus Torvalds /* check whether the dsp was already loaded */
21618d122c0SArnd Bergmann if (hw->dsp_loaded & (1u << info->index))
2171da177e4SLinus Torvalds return -EBUSY;
21818d122c0SArnd Bergmann err = hw->ops.dsp_load(hw, info);
2191da177e4SLinus Torvalds if (err < 0)
2201da177e4SLinus Torvalds return err;
22118d122c0SArnd Bergmann hw->dsp_loaded |= (1u << info->index);
2221da177e4SLinus Torvalds return 0;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds
snd_hwdep_dsp_load_user(struct snd_hwdep * hw,struct snd_hwdep_dsp_image __user * _info)22518d122c0SArnd Bergmann static int snd_hwdep_dsp_load_user(struct snd_hwdep *hw,
22618d122c0SArnd Bergmann struct snd_hwdep_dsp_image __user *_info)
22718d122c0SArnd Bergmann {
22818d122c0SArnd Bergmann struct snd_hwdep_dsp_image info = {};
22918d122c0SArnd Bergmann
23018d122c0SArnd Bergmann if (copy_from_user(&info, _info, sizeof(info)))
23118d122c0SArnd Bergmann return -EFAULT;
23218d122c0SArnd Bergmann return snd_hwdep_dsp_load(hw, &info);
23318d122c0SArnd Bergmann }
23418d122c0SArnd Bergmann
23518d122c0SArnd Bergmann
snd_hwdep_ioctl(struct file * file,unsigned int cmd,unsigned long arg)236d9a98de2STakashi Iwai static long snd_hwdep_ioctl(struct file * file, unsigned int cmd,
237d9a98de2STakashi Iwai unsigned long arg)
2381da177e4SLinus Torvalds {
239d9a98de2STakashi Iwai struct snd_hwdep *hw = file->private_data;
2401da177e4SLinus Torvalds void __user *argp = (void __user *)arg;
2411da177e4SLinus Torvalds switch (cmd) {
2421da177e4SLinus Torvalds case SNDRV_HWDEP_IOCTL_PVERSION:
2431da177e4SLinus Torvalds return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);
2441da177e4SLinus Torvalds case SNDRV_HWDEP_IOCTL_INFO:
2451da177e4SLinus Torvalds return snd_hwdep_info(hw, argp);
2461da177e4SLinus Torvalds case SNDRV_HWDEP_IOCTL_DSP_STATUS:
2471da177e4SLinus Torvalds return snd_hwdep_dsp_status(hw, argp);
2481da177e4SLinus Torvalds case SNDRV_HWDEP_IOCTL_DSP_LOAD:
24918d122c0SArnd Bergmann return snd_hwdep_dsp_load_user(hw, argp);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds if (hw->ops.ioctl)
2521da177e4SLinus Torvalds return hw->ops.ioctl(hw, file, cmd, arg);
2531da177e4SLinus Torvalds return -ENOTTY;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds
snd_hwdep_mmap(struct file * file,struct vm_area_struct * vma)2561da177e4SLinus Torvalds static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
2571da177e4SLinus Torvalds {
258d9a98de2STakashi Iwai struct snd_hwdep *hw = file->private_data;
2591da177e4SLinus Torvalds if (hw->ops.mmap)
2601da177e4SLinus Torvalds return hw->ops.mmap(hw, file, vma);
2611da177e4SLinus Torvalds return -ENXIO;
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds
snd_hwdep_control_ioctl(struct snd_card * card,struct snd_ctl_file * control,unsigned int cmd,unsigned long arg)264d9a98de2STakashi Iwai static int snd_hwdep_control_ioctl(struct snd_card *card,
265d9a98de2STakashi Iwai struct snd_ctl_file * control,
2661da177e4SLinus Torvalds unsigned int cmd, unsigned long arg)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds switch (cmd) {
2691da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds int device;
2721da177e4SLinus Torvalds
2731da177e4SLinus Torvalds if (get_user(device, (int __user *)arg))
2741da177e4SLinus Torvalds return -EFAULT;
275f7b2bb85SDan Carpenter
276e6684d08STakashi Iwai scoped_guard(mutex, ®ister_mutex) {
277f7b2bb85SDan Carpenter if (device < 0)
278f7b2bb85SDan Carpenter device = 0;
279f7b2bb85SDan Carpenter else if (device < SNDRV_MINOR_HWDEPS)
280f7b2bb85SDan Carpenter device++;
281f7b2bb85SDan Carpenter else
282f7b2bb85SDan Carpenter device = SNDRV_MINOR_HWDEPS;
283f7b2bb85SDan Carpenter
2841da177e4SLinus Torvalds while (device < SNDRV_MINOR_HWDEPS) {
285f87135f5SClemens Ladisch if (snd_hwdep_search(card, device))
2861da177e4SLinus Torvalds break;
2871da177e4SLinus Torvalds device++;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds if (device >= SNDRV_MINOR_HWDEPS)
2901da177e4SLinus Torvalds device = -1;
291*72165c86SNathan Chancellor }
2921da177e4SLinus Torvalds if (put_user(device, (int __user *)arg))
2931da177e4SLinus Torvalds return -EFAULT;
2941da177e4SLinus Torvalds return 0;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_HWDEP_INFO:
2971da177e4SLinus Torvalds {
298d9a98de2STakashi Iwai struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
299e6684d08STakashi Iwai int device;
300d9a98de2STakashi Iwai struct snd_hwdep *hwdep;
3011da177e4SLinus Torvalds
3021da177e4SLinus Torvalds if (get_user(device, &info->device))
3031da177e4SLinus Torvalds return -EFAULT;
304e6684d08STakashi Iwai scoped_guard(mutex, ®ister_mutex) {
305f87135f5SClemens Ladisch hwdep = snd_hwdep_search(card, device);
306e6684d08STakashi Iwai if (!hwdep)
307e6684d08STakashi Iwai return -ENXIO;
308e6684d08STakashi Iwai return snd_hwdep_info(hwdep, info);
309e6684d08STakashi Iwai }
310e6684d08STakashi Iwai break;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds return -ENOIOCTLCMD;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds
3161da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
3171da177e4SLinus Torvalds #include "hwdep_compat.c"
3181da177e4SLinus Torvalds #else
3191da177e4SLinus Torvalds #define snd_hwdep_ioctl_compat NULL
3201da177e4SLinus Torvalds #endif
3211da177e4SLinus Torvalds
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds
3241da177e4SLinus Torvalds */
3251da177e4SLinus Torvalds
3269c2e08c5SArjan van de Ven static const struct file_operations snd_hwdep_f_ops =
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds .owner = THIS_MODULE,
3291da177e4SLinus Torvalds .llseek = snd_hwdep_llseek,
3301da177e4SLinus Torvalds .read = snd_hwdep_read,
3311da177e4SLinus Torvalds .write = snd_hwdep_write,
3321da177e4SLinus Torvalds .open = snd_hwdep_open,
3331da177e4SLinus Torvalds .release = snd_hwdep_release,
3341da177e4SLinus Torvalds .poll = snd_hwdep_poll,
3351da177e4SLinus Torvalds .unlocked_ioctl = snd_hwdep_ioctl,
3361da177e4SLinus Torvalds .compat_ioctl = snd_hwdep_ioctl_compat,
3371da177e4SLinus Torvalds .mmap = snd_hwdep_mmap,
3381da177e4SLinus Torvalds };
3391da177e4SLinus Torvalds
snd_hwdep_free(struct snd_hwdep * hwdep)340897c8882STakashi Iwai static void snd_hwdep_free(struct snd_hwdep *hwdep)
3417b461600STakashi Iwai {
342897c8882STakashi Iwai if (!hwdep)
343897c8882STakashi Iwai return;
344897c8882STakashi Iwai if (hwdep->private_free)
345897c8882STakashi Iwai hwdep->private_free(hwdep);
346897c8882STakashi Iwai put_device(hwdep->dev);
347897c8882STakashi Iwai kfree(hwdep);
3487b461600STakashi Iwai }
3497b461600STakashi Iwai
3501da177e4SLinus Torvalds /**
3511da177e4SLinus Torvalds * snd_hwdep_new - create a new hwdep instance
3521da177e4SLinus Torvalds * @card: the card instance
3531da177e4SLinus Torvalds * @id: the id string
3541da177e4SLinus Torvalds * @device: the device index (zero-based)
3551da177e4SLinus Torvalds * @rhwdep: the pointer to store the new hwdep instance
3561da177e4SLinus Torvalds *
3571da177e4SLinus Torvalds * Creates a new hwdep instance with the given index on the card.
3581da177e4SLinus Torvalds * The callbacks (hwdep->ops) must be set on the returned instance
3591da177e4SLinus Torvalds * after this call manually by the caller.
3601da177e4SLinus Torvalds *
361eb7c06e8SYacine Belkadi * Return: Zero if successful, or a negative error code on failure.
3621da177e4SLinus Torvalds */
snd_hwdep_new(struct snd_card * card,char * id,int device,struct snd_hwdep ** rhwdep)363d9a98de2STakashi Iwai int snd_hwdep_new(struct snd_card *card, char *id, int device,
364d9a98de2STakashi Iwai struct snd_hwdep **rhwdep)
3651da177e4SLinus Torvalds {
366d9a98de2STakashi Iwai struct snd_hwdep *hwdep;
3671da177e4SLinus Torvalds int err;
368f15ee210STakashi Iwai static const struct snd_device_ops ops = {
3691da177e4SLinus Torvalds .dev_free = snd_hwdep_dev_free,
3701da177e4SLinus Torvalds .dev_register = snd_hwdep_dev_register,
371c461482cSTakashi Iwai .dev_disconnect = snd_hwdep_dev_disconnect,
3721da177e4SLinus Torvalds };
3731da177e4SLinus Torvalds
3747eaa943cSTakashi Iwai if (snd_BUG_ON(!card))
3757eaa943cSTakashi Iwai return -ENXIO;
3767eaa943cSTakashi Iwai if (rhwdep)
3771da177e4SLinus Torvalds *rhwdep = NULL;
378ca2c0966STakashi Iwai hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
379ec0e9937STakashi Iwai if (!hwdep)
3801da177e4SLinus Torvalds return -ENOMEM;
3817b461600STakashi Iwai
3827b461600STakashi Iwai init_waitqueue_head(&hwdep->open_wait);
3837b461600STakashi Iwai mutex_init(&hwdep->open_mutex);
3841da177e4SLinus Torvalds hwdep->card = card;
3851da177e4SLinus Torvalds hwdep->device = device;
38673e77ba0STakashi Iwai if (id)
38775b1a8f9SJoe Perches strscpy(hwdep->id, id, sizeof(hwdep->id));
3887b461600STakashi Iwai
389897c8882STakashi Iwai err = snd_device_alloc(&hwdep->dev, card);
390897c8882STakashi Iwai if (err < 0) {
391897c8882STakashi Iwai snd_hwdep_free(hwdep);
392897c8882STakashi Iwai return err;
393897c8882STakashi Iwai }
394897c8882STakashi Iwai
395897c8882STakashi Iwai dev_set_name(hwdep->dev, "hwC%iD%i", card->number, device);
3961da177e4SLinus Torvalds #ifdef CONFIG_SND_OSSEMUL
3971da177e4SLinus Torvalds hwdep->oss_type = -1;
3981da177e4SLinus Torvalds #endif
3997b461600STakashi Iwai
4007b461600STakashi Iwai err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops);
4017b461600STakashi Iwai if (err < 0) {
402897c8882STakashi Iwai snd_hwdep_free(hwdep);
4031da177e4SLinus Torvalds return err;
4041da177e4SLinus Torvalds }
4057b461600STakashi Iwai
4067eaa943cSTakashi Iwai if (rhwdep)
4071da177e4SLinus Torvalds *rhwdep = hwdep;
4081da177e4SLinus Torvalds return 0;
4091da177e4SLinus Torvalds }
4106776a5d7STakashi Iwai EXPORT_SYMBOL(snd_hwdep_new);
4111da177e4SLinus Torvalds
snd_hwdep_dev_free(struct snd_device * device)4127b461600STakashi Iwai static int snd_hwdep_dev_free(struct snd_device *device)
4131da177e4SLinus Torvalds {
414897c8882STakashi Iwai snd_hwdep_free(device->device_data);
4151da177e4SLinus Torvalds return 0;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds
snd_hwdep_dev_register(struct snd_device * device)418d9a98de2STakashi Iwai static int snd_hwdep_dev_register(struct snd_device *device)
4191da177e4SLinus Torvalds {
420d9a98de2STakashi Iwai struct snd_hwdep *hwdep = device->device_data;
4212ebef69fSTakashi Iwai struct snd_card *card = hwdep->card;
422f87135f5SClemens Ladisch int err;
4231da177e4SLinus Torvalds
424e6684d08STakashi Iwai guard(mutex)(®ister_mutex);
425e6684d08STakashi Iwai if (snd_hwdep_search(card, hwdep->device))
4261da177e4SLinus Torvalds return -EBUSY;
427f87135f5SClemens Ladisch list_add_tail(&hwdep->list, &snd_hwdep_devices);
42840a4b263STakashi Iwai err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
4291da177e4SLinus Torvalds hwdep->card, hwdep->device,
430897c8882STakashi Iwai &snd_hwdep_f_ops, hwdep, hwdep->dev);
43171e2e1c1STakashi Iwai if (err < 0) {
432897c8882STakashi Iwai dev_err(hwdep->dev, "unable to register\n");
433f87135f5SClemens Ladisch list_del(&hwdep->list);
4341da177e4SLinus Torvalds return err;
4351da177e4SLinus Torvalds }
436caa751baSTakashi Iwai
4371da177e4SLinus Torvalds #ifdef CONFIG_SND_OSSEMUL
4381da177e4SLinus Torvalds hwdep->ossreg = 0;
4391da177e4SLinus Torvalds if (hwdep->oss_type >= 0) {
4407b461600STakashi Iwai if (hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM &&
4417b461600STakashi Iwai hwdep->device)
442897c8882STakashi Iwai dev_warn(hwdep->dev,
4432ebef69fSTakashi Iwai "only hwdep device 0 can be registered as OSS direct FM device!\n");
4447b461600STakashi Iwai else if (snd_register_oss_device(hwdep->oss_type,
4452ebef69fSTakashi Iwai card, hwdep->device,
4467b461600STakashi Iwai &snd_hwdep_f_ops, hwdep) < 0)
447897c8882STakashi Iwai dev_warn(hwdep->dev,
4487b461600STakashi Iwai "unable to register OSS compatibility device\n");
4497b461600STakashi Iwai else
4501da177e4SLinus Torvalds hwdep->ossreg = 1;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds #endif
4531da177e4SLinus Torvalds return 0;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds
snd_hwdep_dev_disconnect(struct snd_device * device)456c461482cSTakashi Iwai static int snd_hwdep_dev_disconnect(struct snd_device *device)
4571da177e4SLinus Torvalds {
458d9a98de2STakashi Iwai struct snd_hwdep *hwdep = device->device_data;
4591da177e4SLinus Torvalds
4607eaa943cSTakashi Iwai if (snd_BUG_ON(!hwdep))
4617eaa943cSTakashi Iwai return -ENXIO;
462e6684d08STakashi Iwai guard(mutex)(®ister_mutex);
463e6684d08STakashi Iwai if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep)
4641da177e4SLinus Torvalds return -EINVAL;
465e6684d08STakashi Iwai guard(mutex)(&hwdep->open_mutex);
4660914f796STakashi Iwai wake_up(&hwdep->open_wait);
4671da177e4SLinus Torvalds #ifdef CONFIG_SND_OSSEMUL
4681da177e4SLinus Torvalds if (hwdep->ossreg)
4691da177e4SLinus Torvalds snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
4701da177e4SLinus Torvalds #endif
471897c8882STakashi Iwai snd_unregister_device(hwdep->dev);
472c461482cSTakashi Iwai list_del_init(&hwdep->list);
473c461482cSTakashi Iwai return 0;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds
476cd6a6503SJie Yang #ifdef CONFIG_SND_PROC_FS
4771da177e4SLinus Torvalds /*
4781da177e4SLinus Torvalds * Info interface
4791da177e4SLinus Torvalds */
4801da177e4SLinus Torvalds
snd_hwdep_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)481d9a98de2STakashi Iwai static void snd_hwdep_proc_read(struct snd_info_entry *entry,
482d9a98de2STakashi Iwai struct snd_info_buffer *buffer)
4831da177e4SLinus Torvalds {
484d9a98de2STakashi Iwai struct snd_hwdep *hwdep;
4851da177e4SLinus Torvalds
486e6684d08STakashi Iwai guard(mutex)(®ister_mutex);
4879244b2c3SJohannes Berg list_for_each_entry(hwdep, &snd_hwdep_devices, list)
4881da177e4SLinus Torvalds snd_iprintf(buffer, "%02i-%02i: %s\n",
489f87135f5SClemens Ladisch hwdep->card->number, hwdep->device, hwdep->name);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds
492e28563ccSTakashi Iwai static struct snd_info_entry *snd_hwdep_proc_entry;
4931da177e4SLinus Torvalds
snd_hwdep_proc_init(void)494e28563ccSTakashi Iwai static void __init snd_hwdep_proc_init(void)
4951da177e4SLinus Torvalds {
496d9a98de2STakashi Iwai struct snd_info_entry *entry;
4971da177e4SLinus Torvalds
498e3ded899STakashi Iwai entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL);
499e3ded899STakashi Iwai if (entry) {
5001da177e4SLinus Torvalds entry->c.text.read = snd_hwdep_proc_read;
5011da177e4SLinus Torvalds if (snd_info_register(entry) < 0) {
5021da177e4SLinus Torvalds snd_info_free_entry(entry);
5031da177e4SLinus Torvalds entry = NULL;
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds snd_hwdep_proc_entry = entry;
507e28563ccSTakashi Iwai }
508e28563ccSTakashi Iwai
snd_hwdep_proc_done(void)509e28563ccSTakashi Iwai static void __exit snd_hwdep_proc_done(void)
510e28563ccSTakashi Iwai {
511746d4a02STakashi Iwai snd_info_free_entry(snd_hwdep_proc_entry);
512e28563ccSTakashi Iwai }
513cd6a6503SJie Yang #else /* !CONFIG_SND_PROC_FS */
514e28563ccSTakashi Iwai #define snd_hwdep_proc_init()
515e28563ccSTakashi Iwai #define snd_hwdep_proc_done()
516cd6a6503SJie Yang #endif /* CONFIG_SND_PROC_FS */
517e28563ccSTakashi Iwai
518e28563ccSTakashi Iwai
519e28563ccSTakashi Iwai /*
520e28563ccSTakashi Iwai * ENTRY functions
521e28563ccSTakashi Iwai */
522e28563ccSTakashi Iwai
alsa_hwdep_init(void)523e28563ccSTakashi Iwai static int __init alsa_hwdep_init(void)
524e28563ccSTakashi Iwai {
525e28563ccSTakashi Iwai snd_hwdep_proc_init();
5261da177e4SLinus Torvalds snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
5271da177e4SLinus Torvalds snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
5281da177e4SLinus Torvalds return 0;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds
alsa_hwdep_exit(void)5311da177e4SLinus Torvalds static void __exit alsa_hwdep_exit(void)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
5341da177e4SLinus Torvalds snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
535e28563ccSTakashi Iwai snd_hwdep_proc_done();
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds
5381da177e4SLinus Torvalds module_init(alsa_hwdep_init)
5391da177e4SLinus Torvalds module_exit(alsa_hwdep_exit)
540