Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HWDEP Interface for HD-audio codec |
| 3 | * |
| 4 | * Copyright (c) 2007 Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * This driver 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 driver 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 | |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/slab.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 23 | #include <linux/compat.h> |
Takashi Iwai | 69bd3f0 | 2018-04-24 07:50:50 +0200 | [diff] [blame] | 24 | #include <linux/nospec.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 25 | #include <sound/core.h> |
| 26 | #include "hda_codec.h" |
| 27 | #include "hda_local.h" |
| 28 | #include <sound/hda_hwdep.h> |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 29 | #include <sound/minors.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * write/read an out-of-bound verb |
| 33 | */ |
| 34 | static int verb_write_ioctl(struct hda_codec *codec, |
| 35 | struct hda_verb_ioctl __user *arg) |
| 36 | { |
| 37 | u32 verb, res; |
| 38 | |
| 39 | if (get_user(verb, &arg->verb)) |
| 40 | return -EFAULT; |
| 41 | res = snd_hda_codec_read(codec, verb >> 24, 0, |
| 42 | (verb >> 8) & 0xffff, verb & 0xff); |
| 43 | if (put_user(res, &arg->res)) |
| 44 | return -EFAULT; |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static int get_wcap_ioctl(struct hda_codec *codec, |
| 49 | struct hda_verb_ioctl __user *arg) |
| 50 | { |
| 51 | u32 verb, res; |
| 52 | |
| 53 | if (get_user(verb, &arg->verb)) |
| 54 | return -EFAULT; |
Takashi Iwai | 69bd3f0 | 2018-04-24 07:50:50 +0200 | [diff] [blame] | 55 | /* open-code get_wcaps(verb>>24) with nospec */ |
| 56 | verb >>= 24; |
| 57 | if (verb < codec->core.start_nid || |
| 58 | verb >= codec->core.start_nid + codec->core.num_nodes) { |
| 59 | res = 0; |
| 60 | } else { |
| 61 | verb -= codec->core.start_nid; |
| 62 | verb = array_index_nospec(verb, codec->core.num_nodes); |
| 63 | res = codec->wcaps[verb]; |
| 64 | } |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 65 | if (put_user(res, &arg->res)) |
| 66 | return -EFAULT; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | */ |
| 73 | static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, |
| 74 | unsigned int cmd, unsigned long arg) |
| 75 | { |
| 76 | struct hda_codec *codec = hw->private_data; |
| 77 | void __user *argp = (void __user *)arg; |
| 78 | |
| 79 | switch (cmd) { |
| 80 | case HDA_IOCTL_PVERSION: |
| 81 | return put_user(HDA_HWDEP_VERSION, (int __user *)argp); |
| 82 | case HDA_IOCTL_VERB_WRITE: |
| 83 | return verb_write_ioctl(codec, argp); |
| 84 | case HDA_IOCTL_GET_WCAP: |
| 85 | return get_wcap_ioctl(codec, argp); |
| 86 | } |
| 87 | return -ENOIOCTLCMD; |
| 88 | } |
| 89 | |
| 90 | #ifdef CONFIG_COMPAT |
| 91 | static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file, |
| 92 | unsigned int cmd, unsigned long arg) |
| 93 | { |
Takashi Iwai | 312d045 | 2007-07-31 11:08:10 +0200 | [diff] [blame] | 94 | return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg)); |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 95 | } |
| 96 | #endif |
| 97 | |
| 98 | static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file) |
| 99 | { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 100 | #ifndef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 101 | if (!capable(CAP_SYS_RAWIO)) |
| 102 | return -EACCES; |
| 103 | #endif |
| 104 | return 0; |
| 105 | } |
| 106 | |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 107 | int snd_hda_create_hwdep(struct hda_codec *codec) |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 108 | { |
| 109 | char hwname[16]; |
| 110 | struct snd_hwdep *hwdep; |
| 111 | int err; |
| 112 | |
| 113 | sprintf(hwname, "HDA Codec %d", codec->addr); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 114 | err = snd_hwdep_new(codec->card, hwname, codec->addr, &hwdep); |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 115 | if (err < 0) |
| 116 | return err; |
| 117 | codec->hwdep = hwdep; |
| 118 | sprintf(hwdep->name, "HDA Codec %d", codec->addr); |
| 119 | hwdep->iface = SNDRV_HWDEP_IFACE_HDA; |
| 120 | hwdep->private_data = codec; |
| 121 | hwdep->exclusive = 1; |
| 122 | |
| 123 | hwdep->ops.open = hda_hwdep_open; |
| 124 | hwdep->ops.ioctl = hda_hwdep_ioctl; |
| 125 | #ifdef CONFIG_COMPAT |
| 126 | hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat; |
| 127 | #endif |
| 128 | |
Takashi Iwai | 7b46160 | 2015-01-29 17:13:32 +0100 | [diff] [blame] | 129 | /* for sysfs */ |
| 130 | hwdep->dev.groups = snd_hda_dev_attr_groups; |
| 131 | dev_set_drvdata(&hwdep->dev, codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 132 | |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 133 | return 0; |
| 134 | } |