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> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/compat.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <sound/core.h> |
| 27 | #include "hda_codec.h" |
| 28 | #include "hda_local.h" |
| 29 | #include <sound/hda_hwdep.h> |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame^] | 30 | #include <sound/minors.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * write/read an out-of-bound verb |
| 34 | */ |
| 35 | static int verb_write_ioctl(struct hda_codec *codec, |
| 36 | struct hda_verb_ioctl __user *arg) |
| 37 | { |
| 38 | u32 verb, res; |
| 39 | |
| 40 | if (get_user(verb, &arg->verb)) |
| 41 | return -EFAULT; |
| 42 | res = snd_hda_codec_read(codec, verb >> 24, 0, |
| 43 | (verb >> 8) & 0xffff, verb & 0xff); |
| 44 | if (put_user(res, &arg->res)) |
| 45 | return -EFAULT; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int get_wcap_ioctl(struct hda_codec *codec, |
| 50 | struct hda_verb_ioctl __user *arg) |
| 51 | { |
| 52 | u32 verb, res; |
| 53 | |
| 54 | if (get_user(verb, &arg->verb)) |
| 55 | return -EFAULT; |
| 56 | res = get_wcaps(codec, verb >> 24); |
| 57 | if (put_user(res, &arg->res)) |
| 58 | return -EFAULT; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /* |
| 64 | */ |
| 65 | static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, |
| 66 | unsigned int cmd, unsigned long arg) |
| 67 | { |
| 68 | struct hda_codec *codec = hw->private_data; |
| 69 | void __user *argp = (void __user *)arg; |
| 70 | |
| 71 | switch (cmd) { |
| 72 | case HDA_IOCTL_PVERSION: |
| 73 | return put_user(HDA_HWDEP_VERSION, (int __user *)argp); |
| 74 | case HDA_IOCTL_VERB_WRITE: |
| 75 | return verb_write_ioctl(codec, argp); |
| 76 | case HDA_IOCTL_GET_WCAP: |
| 77 | return get_wcap_ioctl(codec, argp); |
| 78 | } |
| 79 | return -ENOIOCTLCMD; |
| 80 | } |
| 81 | |
| 82 | #ifdef CONFIG_COMPAT |
| 83 | static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file, |
| 84 | unsigned int cmd, unsigned long arg) |
| 85 | { |
Takashi Iwai | 312d045 | 2007-07-31 11:08:10 +0200 | [diff] [blame] | 86 | return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg)); |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 87 | } |
| 88 | #endif |
| 89 | |
| 90 | static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file) |
| 91 | { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 92 | #ifndef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 93 | if (!capable(CAP_SYS_RAWIO)) |
| 94 | return -EACCES; |
| 95 | #endif |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | int __devinit snd_hda_create_hwdep(struct hda_codec *codec) |
| 100 | { |
| 101 | char hwname[16]; |
| 102 | struct snd_hwdep *hwdep; |
| 103 | int err; |
| 104 | |
| 105 | sprintf(hwname, "HDA Codec %d", codec->addr); |
| 106 | err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep); |
| 107 | if (err < 0) |
| 108 | return err; |
| 109 | codec->hwdep = hwdep; |
| 110 | sprintf(hwdep->name, "HDA Codec %d", codec->addr); |
| 111 | hwdep->iface = SNDRV_HWDEP_IFACE_HDA; |
| 112 | hwdep->private_data = codec; |
| 113 | hwdep->exclusive = 1; |
| 114 | |
| 115 | hwdep->ops.open = hda_hwdep_open; |
| 116 | hwdep->ops.ioctl = hda_hwdep_ioctl; |
| 117 | #ifdef CONFIG_COMPAT |
| 118 | hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat; |
| 119 | #endif |
| 120 | |
| 121 | return 0; |
| 122 | } |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame^] | 123 | |
| 124 | /* |
| 125 | * sysfs interface |
| 126 | */ |
| 127 | |
| 128 | static int clear_codec(struct hda_codec *codec) |
| 129 | { |
| 130 | snd_hda_codec_reset(codec); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int reconfig_codec(struct hda_codec *codec) |
| 135 | { |
| 136 | int err; |
| 137 | |
| 138 | snd_printk(KERN_INFO "hda-codec: reconfiguring\n"); |
| 139 | snd_hda_codec_reset(codec); |
| 140 | err = snd_hda_codec_configure(codec); |
| 141 | if (err < 0) |
| 142 | return err; |
| 143 | /* rebuild PCMs */ |
| 144 | err = snd_hda_build_pcms(codec->bus); |
| 145 | if (err < 0) |
| 146 | return err; |
| 147 | /* rebuild mixers */ |
| 148 | err = snd_hda_codec_build_controls(codec); |
| 149 | if (err < 0) |
| 150 | return err; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * allocate a string at most len chars, and remove the trailing EOL |
| 156 | */ |
| 157 | static char *kstrndup_noeol(const char *src, size_t len) |
| 158 | { |
| 159 | char *s = kstrndup(src, len, GFP_KERNEL); |
| 160 | char *p; |
| 161 | if (!s) |
| 162 | return NULL; |
| 163 | p = strchr(s, '\n'); |
| 164 | if (p) |
| 165 | *p = 0; |
| 166 | return s; |
| 167 | } |
| 168 | |
| 169 | #define CODEC_INFO_SHOW(type) \ |
| 170 | static ssize_t type##_show(struct device *dev, \ |
| 171 | struct device_attribute *attr, \ |
| 172 | char *buf) \ |
| 173 | { \ |
| 174 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 175 | struct hda_codec *codec = hwdep->private_data; \ |
| 176 | return sprintf(buf, "0x%x\n", codec->type); \ |
| 177 | } |
| 178 | |
| 179 | #define CODEC_INFO_STR_SHOW(type) \ |
| 180 | static ssize_t type##_show(struct device *dev, \ |
| 181 | struct device_attribute *attr, \ |
| 182 | char *buf) \ |
| 183 | { \ |
| 184 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 185 | struct hda_codec *codec = hwdep->private_data; \ |
| 186 | return sprintf(buf, "%s\n", \ |
| 187 | codec->type ? codec->type : ""); \ |
| 188 | } |
| 189 | |
| 190 | CODEC_INFO_SHOW(vendor_id); |
| 191 | CODEC_INFO_SHOW(subsystem_id); |
| 192 | CODEC_INFO_SHOW(revision_id); |
| 193 | CODEC_INFO_SHOW(afg); |
| 194 | CODEC_INFO_SHOW(mfg); |
| 195 | CODEC_INFO_STR_SHOW(name); |
| 196 | CODEC_INFO_STR_SHOW(modelname); |
| 197 | |
| 198 | #define CODEC_INFO_STORE(type) \ |
| 199 | static ssize_t type##_store(struct device *dev, \ |
| 200 | struct device_attribute *attr, \ |
| 201 | const char *buf, size_t count) \ |
| 202 | { \ |
| 203 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 204 | struct hda_codec *codec = hwdep->private_data; \ |
| 205 | char *after; \ |
| 206 | codec->type = simple_strtoul(buf, &after, 0); \ |
| 207 | return count; \ |
| 208 | } |
| 209 | |
| 210 | #define CODEC_INFO_STR_STORE(type) \ |
| 211 | static ssize_t type##_store(struct device *dev, \ |
| 212 | struct device_attribute *attr, \ |
| 213 | const char *buf, size_t count) \ |
| 214 | { \ |
| 215 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 216 | struct hda_codec *codec = hwdep->private_data; \ |
| 217 | char *s = kstrndup_noeol(buf, 64); \ |
| 218 | if (!s) \ |
| 219 | return -ENOMEM; \ |
| 220 | kfree(codec->type); \ |
| 221 | codec->type = s; \ |
| 222 | return count; \ |
| 223 | } |
| 224 | |
| 225 | CODEC_INFO_STORE(vendor_id); |
| 226 | CODEC_INFO_STORE(subsystem_id); |
| 227 | CODEC_INFO_STORE(revision_id); |
| 228 | CODEC_INFO_STR_STORE(name); |
| 229 | CODEC_INFO_STR_STORE(modelname); |
| 230 | |
| 231 | #define CODEC_ACTION_STORE(type) \ |
| 232 | static ssize_t type##_store(struct device *dev, \ |
| 233 | struct device_attribute *attr, \ |
| 234 | const char *buf, size_t count) \ |
| 235 | { \ |
| 236 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 237 | struct hda_codec *codec = hwdep->private_data; \ |
| 238 | int err = 0; \ |
| 239 | if (*buf) \ |
| 240 | err = type##_codec(codec); \ |
| 241 | return err < 0 ? err : count; \ |
| 242 | } |
| 243 | |
| 244 | CODEC_ACTION_STORE(reconfig); |
| 245 | CODEC_ACTION_STORE(clear); |
| 246 | |
| 247 | #define CODEC_ATTR_RW(type) \ |
| 248 | __ATTR(type, 0644, type##_show, type##_store) |
| 249 | #define CODEC_ATTR_RO(type) \ |
| 250 | __ATTR_RO(type) |
| 251 | #define CODEC_ATTR_WO(type) \ |
| 252 | __ATTR(type, 0200, NULL, type##_store) |
| 253 | |
| 254 | static struct device_attribute codec_attrs[] = { |
| 255 | CODEC_ATTR_RW(vendor_id), |
| 256 | CODEC_ATTR_RW(subsystem_id), |
| 257 | CODEC_ATTR_RW(revision_id), |
| 258 | CODEC_ATTR_RO(afg), |
| 259 | CODEC_ATTR_RO(mfg), |
| 260 | CODEC_ATTR_RW(name), |
| 261 | CODEC_ATTR_RW(modelname), |
| 262 | CODEC_ATTR_WO(reconfig), |
| 263 | CODEC_ATTR_WO(clear), |
| 264 | }; |
| 265 | |
| 266 | /* |
| 267 | * create sysfs files on hwdep directory |
| 268 | */ |
| 269 | int snd_hda_hwdep_add_sysfs(struct hda_codec *codec) |
| 270 | { |
| 271 | struct snd_hwdep *hwdep = codec->hwdep; |
| 272 | int i; |
| 273 | |
| 274 | for (i = 0; i < ARRAY_SIZE(codec_attrs); i++) |
| 275 | snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, |
| 276 | hwdep->device, &codec_attrs[i]); |
| 277 | return 0; |
| 278 | } |