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> |
| 24 | #include <linux/mutex.h> |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 25 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 26 | #include <linux/string.h> |
Paul Gortmaker | d81a6d7 | 2011-09-22 09:34:58 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 28 | #include <sound/core.h> |
| 29 | #include "hda_codec.h" |
| 30 | #include "hda_local.h" |
| 31 | #include <sound/hda_hwdep.h> |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 32 | #include <sound/minors.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 33 | |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 34 | /* hint string pair */ |
| 35 | struct hda_hint { |
| 36 | const char *key; |
| 37 | const char *val; /* contained in the same alloc as key */ |
| 38 | }; |
| 39 | |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 40 | /* |
| 41 | * write/read an out-of-bound verb |
| 42 | */ |
| 43 | static int verb_write_ioctl(struct hda_codec *codec, |
| 44 | struct hda_verb_ioctl __user *arg) |
| 45 | { |
| 46 | u32 verb, res; |
| 47 | |
| 48 | if (get_user(verb, &arg->verb)) |
| 49 | return -EFAULT; |
| 50 | res = snd_hda_codec_read(codec, verb >> 24, 0, |
| 51 | (verb >> 8) & 0xffff, verb & 0xff); |
| 52 | if (put_user(res, &arg->res)) |
| 53 | return -EFAULT; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int get_wcap_ioctl(struct hda_codec *codec, |
| 58 | struct hda_verb_ioctl __user *arg) |
| 59 | { |
| 60 | u32 verb, res; |
| 61 | |
| 62 | if (get_user(verb, &arg->verb)) |
| 63 | return -EFAULT; |
| 64 | res = get_wcaps(codec, verb >> 24); |
| 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 | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 107 | static void clear_hwdep_elements(struct hda_codec *codec) |
| 108 | { |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 109 | int i; |
| 110 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 111 | /* clear init verbs */ |
| 112 | snd_array_free(&codec->init_verbs); |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 113 | /* clear hints */ |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 114 | for (i = 0; i < codec->hints.used; i++) { |
| 115 | struct hda_hint *hint = snd_array_elem(&codec->hints, i); |
| 116 | kfree(hint->key); /* we don't need to free hint->val */ |
| 117 | } |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 118 | snd_array_free(&codec->hints); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 119 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void hwdep_free(struct snd_hwdep *hwdep) |
| 123 | { |
| 124 | clear_hwdep_elements(hwdep->private_data); |
| 125 | } |
| 126 | |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 127 | int snd_hda_create_hwdep(struct hda_codec *codec) |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 128 | { |
| 129 | char hwname[16]; |
| 130 | struct snd_hwdep *hwdep; |
| 131 | int err; |
| 132 | |
| 133 | sprintf(hwname, "HDA Codec %d", codec->addr); |
| 134 | err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep); |
| 135 | if (err < 0) |
| 136 | return err; |
| 137 | codec->hwdep = hwdep; |
| 138 | sprintf(hwdep->name, "HDA Codec %d", codec->addr); |
| 139 | hwdep->iface = SNDRV_HWDEP_IFACE_HDA; |
| 140 | hwdep->private_data = codec; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 141 | hwdep->private_free = hwdep_free; |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 142 | hwdep->exclusive = 1; |
| 143 | |
| 144 | hwdep->ops.open = hda_hwdep_open; |
| 145 | hwdep->ops.ioctl = hda_hwdep_ioctl; |
| 146 | #ifdef CONFIG_COMPAT |
| 147 | hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat; |
| 148 | #endif |
| 149 | |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 150 | mutex_init(&codec->user_mutex); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 151 | snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32); |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 152 | snd_array_init(&codec->hints, sizeof(struct hda_hint), 32); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 153 | snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 154 | |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | } |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 157 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 158 | #ifdef CONFIG_PM |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 159 | static ssize_t power_on_acct_show(struct device *dev, |
| 160 | struct device_attribute *attr, |
| 161 | char *buf) |
| 162 | { |
| 163 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 164 | struct hda_codec *codec = hwdep->private_data; |
| 165 | snd_hda_update_power_acct(codec); |
| 166 | return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_on_acct)); |
| 167 | } |
| 168 | |
| 169 | static ssize_t power_off_acct_show(struct device *dev, |
| 170 | struct device_attribute *attr, |
| 171 | char *buf) |
| 172 | { |
| 173 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 174 | struct hda_codec *codec = hwdep->private_data; |
| 175 | snd_hda_update_power_acct(codec); |
| 176 | return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_off_acct)); |
| 177 | } |
| 178 | |
| 179 | static struct device_attribute power_attrs[] = { |
| 180 | __ATTR_RO(power_on_acct), |
| 181 | __ATTR_RO(power_off_acct), |
| 182 | }; |
| 183 | |
| 184 | int snd_hda_hwdep_add_power_sysfs(struct hda_codec *codec) |
| 185 | { |
| 186 | struct snd_hwdep *hwdep = codec->hwdep; |
| 187 | int i; |
| 188 | |
| 189 | for (i = 0; i < ARRAY_SIZE(power_attrs); i++) |
| 190 | snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, |
| 191 | hwdep->device, &power_attrs[i]); |
| 192 | return 0; |
| 193 | } |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 194 | #endif /* CONFIG_PM */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 195 | |
Takashi Iwai | e7ee058 | 2008-11-21 09:26:20 +0100 | [diff] [blame] | 196 | #ifdef CONFIG_SND_HDA_RECONFIG |
| 197 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 198 | /* |
| 199 | * sysfs interface |
| 200 | */ |
| 201 | |
| 202 | static int clear_codec(struct hda_codec *codec) |
| 203 | { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 204 | int err; |
| 205 | |
| 206 | err = snd_hda_codec_reset(codec); |
| 207 | if (err < 0) { |
| 208 | snd_printk(KERN_ERR "The codec is being used, can't free.\n"); |
| 209 | return err; |
| 210 | } |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 211 | clear_hwdep_elements(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int reconfig_codec(struct hda_codec *codec) |
| 216 | { |
| 217 | int err; |
| 218 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 219 | snd_hda_power_up(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 220 | snd_printk(KERN_INFO "hda-codec: reconfiguring\n"); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 221 | err = snd_hda_codec_reset(codec); |
| 222 | if (err < 0) { |
| 223 | snd_printk(KERN_ERR |
| 224 | "The codec is being used, can't reconfigure.\n"); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 225 | goto error; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 226 | } |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 227 | err = snd_hda_codec_configure(codec); |
| 228 | if (err < 0) |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 229 | goto error; |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 230 | /* rebuild PCMs */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 231 | err = snd_hda_codec_build_pcms(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 232 | if (err < 0) |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 233 | goto error; |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 234 | /* rebuild mixers */ |
| 235 | err = snd_hda_codec_build_controls(codec); |
| 236 | if (err < 0) |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 237 | goto error; |
| 238 | err = snd_card_register(codec->bus->card); |
| 239 | error: |
| 240 | snd_hda_power_down(codec); |
| 241 | return err; |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* |
| 245 | * allocate a string at most len chars, and remove the trailing EOL |
| 246 | */ |
| 247 | static char *kstrndup_noeol(const char *src, size_t len) |
| 248 | { |
| 249 | char *s = kstrndup(src, len, GFP_KERNEL); |
| 250 | char *p; |
| 251 | if (!s) |
| 252 | return NULL; |
| 253 | p = strchr(s, '\n'); |
| 254 | if (p) |
| 255 | *p = 0; |
| 256 | return s; |
| 257 | } |
| 258 | |
| 259 | #define CODEC_INFO_SHOW(type) \ |
| 260 | static ssize_t type##_show(struct device *dev, \ |
| 261 | struct device_attribute *attr, \ |
| 262 | char *buf) \ |
| 263 | { \ |
| 264 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 265 | struct hda_codec *codec = hwdep->private_data; \ |
| 266 | return sprintf(buf, "0x%x\n", codec->type); \ |
| 267 | } |
| 268 | |
| 269 | #define CODEC_INFO_STR_SHOW(type) \ |
| 270 | static ssize_t type##_show(struct device *dev, \ |
| 271 | struct device_attribute *attr, \ |
| 272 | char *buf) \ |
| 273 | { \ |
| 274 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 275 | struct hda_codec *codec = hwdep->private_data; \ |
| 276 | return sprintf(buf, "%s\n", \ |
| 277 | codec->type ? codec->type : ""); \ |
| 278 | } |
| 279 | |
| 280 | CODEC_INFO_SHOW(vendor_id); |
| 281 | CODEC_INFO_SHOW(subsystem_id); |
| 282 | CODEC_INFO_SHOW(revision_id); |
| 283 | CODEC_INFO_SHOW(afg); |
| 284 | CODEC_INFO_SHOW(mfg); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 285 | CODEC_INFO_STR_SHOW(vendor_name); |
| 286 | CODEC_INFO_STR_SHOW(chip_name); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 287 | CODEC_INFO_STR_SHOW(modelname); |
| 288 | |
| 289 | #define CODEC_INFO_STORE(type) \ |
| 290 | static ssize_t type##_store(struct device *dev, \ |
| 291 | struct device_attribute *attr, \ |
| 292 | const char *buf, size_t count) \ |
| 293 | { \ |
| 294 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 295 | struct hda_codec *codec = hwdep->private_data; \ |
Takashi Iwai | 014c41f | 2009-12-27 13:53:24 +0100 | [diff] [blame] | 296 | unsigned long val; \ |
Jingoo Han | b785a49 | 2013-07-19 16:24:59 +0900 | [diff] [blame] | 297 | int err = kstrtoul(buf, 0, &val); \ |
Takashi Iwai | 014c41f | 2009-12-27 13:53:24 +0100 | [diff] [blame] | 298 | if (err < 0) \ |
| 299 | return err; \ |
| 300 | codec->type = val; \ |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 301 | return count; \ |
| 302 | } |
| 303 | |
| 304 | #define CODEC_INFO_STR_STORE(type) \ |
| 305 | static ssize_t type##_store(struct device *dev, \ |
| 306 | struct device_attribute *attr, \ |
| 307 | const char *buf, size_t count) \ |
| 308 | { \ |
| 309 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 310 | struct hda_codec *codec = hwdep->private_data; \ |
| 311 | char *s = kstrndup_noeol(buf, 64); \ |
| 312 | if (!s) \ |
| 313 | return -ENOMEM; \ |
| 314 | kfree(codec->type); \ |
| 315 | codec->type = s; \ |
| 316 | return count; \ |
| 317 | } |
| 318 | |
| 319 | CODEC_INFO_STORE(vendor_id); |
| 320 | CODEC_INFO_STORE(subsystem_id); |
| 321 | CODEC_INFO_STORE(revision_id); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 322 | CODEC_INFO_STR_STORE(vendor_name); |
| 323 | CODEC_INFO_STR_STORE(chip_name); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 324 | CODEC_INFO_STR_STORE(modelname); |
| 325 | |
| 326 | #define CODEC_ACTION_STORE(type) \ |
| 327 | static ssize_t type##_store(struct device *dev, \ |
| 328 | struct device_attribute *attr, \ |
| 329 | const char *buf, size_t count) \ |
| 330 | { \ |
| 331 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); \ |
| 332 | struct hda_codec *codec = hwdep->private_data; \ |
| 333 | int err = 0; \ |
| 334 | if (*buf) \ |
| 335 | err = type##_codec(codec); \ |
| 336 | return err < 0 ? err : count; \ |
| 337 | } |
| 338 | |
| 339 | CODEC_ACTION_STORE(reconfig); |
| 340 | CODEC_ACTION_STORE(clear); |
| 341 | |
Takashi Iwai | ab1726f | 2009-03-02 17:09:25 +0100 | [diff] [blame] | 342 | static ssize_t init_verbs_show(struct device *dev, |
| 343 | struct device_attribute *attr, |
| 344 | char *buf) |
| 345 | { |
| 346 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 347 | struct hda_codec *codec = hwdep->private_data; |
| 348 | int i, len = 0; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 349 | mutex_lock(&codec->user_mutex); |
Takashi Iwai | ab1726f | 2009-03-02 17:09:25 +0100 | [diff] [blame] | 350 | for (i = 0; i < codec->init_verbs.used; i++) { |
| 351 | struct hda_verb *v = snd_array_elem(&codec->init_verbs, i); |
| 352 | len += snprintf(buf + len, PAGE_SIZE - len, |
| 353 | "0x%02x 0x%03x 0x%04x\n", |
| 354 | v->nid, v->verb, v->param); |
| 355 | } |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 356 | mutex_unlock(&codec->user_mutex); |
Takashi Iwai | ab1726f | 2009-03-02 17:09:25 +0100 | [diff] [blame] | 357 | return len; |
| 358 | } |
| 359 | |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 360 | static int parse_init_verbs(struct hda_codec *codec, const char *buf) |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 361 | { |
Takashi Iwai | 55290e1 | 2009-02-20 15:59:01 +0100 | [diff] [blame] | 362 | struct hda_verb *v; |
| 363 | int nid, verb, param; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 364 | |
Takashi Iwai | 55290e1 | 2009-02-20 15:59:01 +0100 | [diff] [blame] | 365 | if (sscanf(buf, "%i %i %i", &nid, &verb, ¶m) != 3) |
| 366 | return -EINVAL; |
| 367 | if (!nid || !verb) |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 368 | return -EINVAL; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 369 | mutex_lock(&codec->user_mutex); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 370 | v = snd_array_new(&codec->init_verbs); |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 371 | if (!v) { |
| 372 | mutex_unlock(&codec->user_mutex); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 373 | return -ENOMEM; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 374 | } |
Takashi Iwai | 55290e1 | 2009-02-20 15:59:01 +0100 | [diff] [blame] | 375 | v->nid = nid; |
| 376 | v->verb = verb; |
| 377 | v->param = param; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 378 | mutex_unlock(&codec->user_mutex); |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static ssize_t init_verbs_store(struct device *dev, |
| 383 | struct device_attribute *attr, |
| 384 | const char *buf, size_t count) |
| 385 | { |
| 386 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 387 | struct hda_codec *codec = hwdep->private_data; |
| 388 | int err = parse_init_verbs(codec, buf); |
| 389 | if (err < 0) |
| 390 | return err; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 391 | return count; |
| 392 | } |
| 393 | |
Takashi Iwai | ab1726f | 2009-03-02 17:09:25 +0100 | [diff] [blame] | 394 | static ssize_t hints_show(struct device *dev, |
| 395 | struct device_attribute *attr, |
| 396 | char *buf) |
| 397 | { |
| 398 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 399 | struct hda_codec *codec = hwdep->private_data; |
| 400 | int i, len = 0; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 401 | mutex_lock(&codec->user_mutex); |
Takashi Iwai | ab1726f | 2009-03-02 17:09:25 +0100 | [diff] [blame] | 402 | for (i = 0; i < codec->hints.used; i++) { |
| 403 | struct hda_hint *hint = snd_array_elem(&codec->hints, i); |
| 404 | len += snprintf(buf + len, PAGE_SIZE - len, |
| 405 | "%s = %s\n", hint->key, hint->val); |
| 406 | } |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 407 | mutex_unlock(&codec->user_mutex); |
Takashi Iwai | ab1726f | 2009-03-02 17:09:25 +0100 | [diff] [blame] | 408 | return len; |
| 409 | } |
| 410 | |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 411 | static struct hda_hint *get_hint(struct hda_codec *codec, const char *key) |
| 412 | { |
| 413 | int i; |
| 414 | |
| 415 | for (i = 0; i < codec->hints.used; i++) { |
| 416 | struct hda_hint *hint = snd_array_elem(&codec->hints, i); |
| 417 | if (!strcmp(hint->key, key)) |
| 418 | return hint; |
| 419 | } |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | static void remove_trail_spaces(char *str) |
| 424 | { |
| 425 | char *p; |
| 426 | if (!*str) |
| 427 | return; |
| 428 | p = str + strlen(str) - 1; |
| 429 | for (; isspace(*p); p--) { |
| 430 | *p = 0; |
| 431 | if (p == str) |
| 432 | return; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | #define MAX_HINTS 1024 |
| 437 | |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 438 | static int parse_hints(struct hda_codec *codec, const char *buf) |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 439 | { |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 440 | char *key, *val; |
| 441 | struct hda_hint *hint; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 442 | int err = 0; |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 443 | |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 444 | buf = skip_spaces(buf); |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 445 | if (!*buf || *buf == '#' || *buf == '\n') |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 446 | return 0; |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 447 | if (*buf == '=') |
| 448 | return -EINVAL; |
| 449 | key = kstrndup_noeol(buf, 1024); |
| 450 | if (!key) |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 451 | return -ENOMEM; |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 452 | /* extract key and val */ |
| 453 | val = strchr(key, '='); |
| 454 | if (!val) { |
| 455 | kfree(key); |
| 456 | return -EINVAL; |
| 457 | } |
| 458 | *val++ = 0; |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 459 | val = skip_spaces(val); |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 460 | remove_trail_spaces(key); |
| 461 | remove_trail_spaces(val); |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 462 | mutex_lock(&codec->user_mutex); |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 463 | hint = get_hint(codec, key); |
| 464 | if (hint) { |
| 465 | /* replace */ |
| 466 | kfree(hint->key); |
| 467 | hint->key = key; |
| 468 | hint->val = val; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 469 | goto unlock; |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 470 | } |
| 471 | /* allocate a new hint entry */ |
| 472 | if (codec->hints.used >= MAX_HINTS) |
| 473 | hint = NULL; |
| 474 | else |
| 475 | hint = snd_array_new(&codec->hints); |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 476 | if (hint) { |
| 477 | hint->key = key; |
| 478 | hint->val = val; |
| 479 | } else { |
| 480 | err = -ENOMEM; |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 481 | } |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 482 | unlock: |
| 483 | mutex_unlock(&codec->user_mutex); |
| 484 | if (err) |
| 485 | kfree(key); |
| 486 | return err; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static ssize_t hints_store(struct device *dev, |
| 490 | struct device_attribute *attr, |
| 491 | const char *buf, size_t count) |
| 492 | { |
| 493 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 494 | struct hda_codec *codec = hwdep->private_data; |
| 495 | int err = parse_hints(codec, buf); |
| 496 | if (err < 0) |
| 497 | return err; |
Takashi Iwai | 1e1be43 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 498 | return count; |
| 499 | } |
| 500 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 501 | static ssize_t pin_configs_show(struct hda_codec *codec, |
| 502 | struct snd_array *list, |
| 503 | char *buf) |
| 504 | { |
| 505 | int i, len = 0; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 506 | mutex_lock(&codec->user_mutex); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 507 | for (i = 0; i < list->used; i++) { |
| 508 | struct hda_pincfg *pin = snd_array_elem(list, i); |
| 509 | len += sprintf(buf + len, "0x%02x 0x%08x\n", |
| 510 | pin->nid, pin->cfg); |
| 511 | } |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 512 | mutex_unlock(&codec->user_mutex); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 513 | return len; |
| 514 | } |
| 515 | |
| 516 | static ssize_t init_pin_configs_show(struct device *dev, |
| 517 | struct device_attribute *attr, |
| 518 | char *buf) |
| 519 | { |
| 520 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 521 | struct hda_codec *codec = hwdep->private_data; |
| 522 | return pin_configs_show(codec, &codec->init_pins, buf); |
| 523 | } |
| 524 | |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 525 | static ssize_t user_pin_configs_show(struct device *dev, |
| 526 | struct device_attribute *attr, |
| 527 | char *buf) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 528 | { |
| 529 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 530 | struct hda_codec *codec = hwdep->private_data; |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 531 | return pin_configs_show(codec, &codec->user_pins, buf); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 534 | static ssize_t driver_pin_configs_show(struct device *dev, |
| 535 | struct device_attribute *attr, |
| 536 | char *buf) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 537 | { |
| 538 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 539 | struct hda_codec *codec = hwdep->private_data; |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 540 | return pin_configs_show(codec, &codec->driver_pins, buf); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | #define MAX_PIN_CONFIGS 32 |
| 544 | |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 545 | static int parse_user_pin_configs(struct hda_codec *codec, const char *buf) |
| 546 | { |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 547 | int nid, cfg, err; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 548 | |
| 549 | if (sscanf(buf, "%i %i", &nid, &cfg) != 2) |
| 550 | return -EINVAL; |
| 551 | if (!nid) |
| 552 | return -EINVAL; |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 553 | mutex_lock(&codec->user_mutex); |
| 554 | err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg); |
| 555 | mutex_unlock(&codec->user_mutex); |
| 556 | return err; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 559 | static ssize_t user_pin_configs_store(struct device *dev, |
| 560 | struct device_attribute *attr, |
| 561 | const char *buf, size_t count) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 562 | { |
| 563 | struct snd_hwdep *hwdep = dev_get_drvdata(dev); |
| 564 | struct hda_codec *codec = hwdep->private_data; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 565 | int err = parse_user_pin_configs(codec, buf); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 566 | if (err < 0) |
| 567 | return err; |
| 568 | return count; |
| 569 | } |
| 570 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 571 | #define CODEC_ATTR_RW(type) \ |
| 572 | __ATTR(type, 0644, type##_show, type##_store) |
| 573 | #define CODEC_ATTR_RO(type) \ |
| 574 | __ATTR_RO(type) |
| 575 | #define CODEC_ATTR_WO(type) \ |
| 576 | __ATTR(type, 0200, NULL, type##_store) |
| 577 | |
| 578 | static struct device_attribute codec_attrs[] = { |
| 579 | CODEC_ATTR_RW(vendor_id), |
| 580 | CODEC_ATTR_RW(subsystem_id), |
| 581 | CODEC_ATTR_RW(revision_id), |
| 582 | CODEC_ATTR_RO(afg), |
| 583 | CODEC_ATTR_RO(mfg), |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 584 | CODEC_ATTR_RW(vendor_name), |
| 585 | CODEC_ATTR_RW(chip_name), |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 586 | CODEC_ATTR_RW(modelname), |
Takashi Iwai | ab1726f | 2009-03-02 17:09:25 +0100 | [diff] [blame] | 587 | CODEC_ATTR_RW(init_verbs), |
| 588 | CODEC_ATTR_RW(hints), |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 589 | CODEC_ATTR_RO(init_pin_configs), |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 590 | CODEC_ATTR_RW(user_pin_configs), |
| 591 | CODEC_ATTR_RO(driver_pin_configs), |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 592 | CODEC_ATTR_WO(reconfig), |
| 593 | CODEC_ATTR_WO(clear), |
| 594 | }; |
| 595 | |
| 596 | /* |
| 597 | * create sysfs files on hwdep directory |
| 598 | */ |
| 599 | int snd_hda_hwdep_add_sysfs(struct hda_codec *codec) |
| 600 | { |
| 601 | struct snd_hwdep *hwdep = codec->hwdep; |
| 602 | int i; |
| 603 | |
| 604 | for (i = 0; i < ARRAY_SIZE(codec_attrs); i++) |
| 605 | snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, |
| 606 | hwdep->device, &codec_attrs[i]); |
| 607 | return 0; |
| 608 | } |
Takashi Iwai | e7ee058 | 2008-11-21 09:26:20 +0100 | [diff] [blame] | 609 | |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 610 | /* |
| 611 | * Look for hint string |
| 612 | */ |
| 613 | const char *snd_hda_get_hint(struct hda_codec *codec, const char *key) |
| 614 | { |
| 615 | struct hda_hint *hint = get_hint(codec, key); |
| 616 | return hint ? hint->val : NULL; |
| 617 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 618 | EXPORT_SYMBOL_GPL(snd_hda_get_hint); |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 619 | |
| 620 | int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key) |
| 621 | { |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 622 | const char *p; |
| 623 | int ret; |
| 624 | |
| 625 | mutex_lock(&codec->user_mutex); |
| 626 | p = snd_hda_get_hint(codec, key); |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 627 | if (!p || !*p) |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 628 | ret = -ENOENT; |
| 629 | else { |
| 630 | switch (toupper(*p)) { |
| 631 | case 'T': /* true */ |
| 632 | case 'Y': /* yes */ |
| 633 | case '1': |
| 634 | ret = 1; |
| 635 | break; |
| 636 | default: |
| 637 | ret = 0; |
| 638 | break; |
| 639 | } |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 640 | } |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 641 | mutex_unlock(&codec->user_mutex); |
| 642 | return ret; |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 643 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 644 | EXPORT_SYMBOL_GPL(snd_hda_get_bool_hint); |
Takashi Iwai | 43b6271 | 2009-03-02 14:25:17 +0100 | [diff] [blame] | 645 | |
Takashi Iwai | bc75972 | 2013-01-11 17:40:31 +0100 | [diff] [blame] | 646 | int snd_hda_get_int_hint(struct hda_codec *codec, const char *key, int *valp) |
| 647 | { |
| 648 | const char *p; |
| 649 | unsigned long val; |
| 650 | int ret; |
| 651 | |
| 652 | mutex_lock(&codec->user_mutex); |
| 653 | p = snd_hda_get_hint(codec, key); |
| 654 | if (!p) |
| 655 | ret = -ENOENT; |
Jingoo Han | b785a49 | 2013-07-19 16:24:59 +0900 | [diff] [blame] | 656 | else if (kstrtoul(p, 0, &val)) |
Takashi Iwai | bc75972 | 2013-01-11 17:40:31 +0100 | [diff] [blame] | 657 | ret = -EINVAL; |
| 658 | else { |
| 659 | *valp = val; |
| 660 | ret = 0; |
| 661 | } |
| 662 | mutex_unlock(&codec->user_mutex); |
| 663 | return ret; |
| 664 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 665 | EXPORT_SYMBOL_GPL(snd_hda_get_int_hint); |
Takashi Iwai | e7ee058 | 2008-11-21 09:26:20 +0100 | [diff] [blame] | 666 | #endif /* CONFIG_SND_HDA_RECONFIG */ |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 667 | |
| 668 | #ifdef CONFIG_SND_HDA_PATCH_LOADER |
| 669 | |
| 670 | /* parser mode */ |
| 671 | enum { |
| 672 | LINE_MODE_NONE, |
| 673 | LINE_MODE_CODEC, |
| 674 | LINE_MODE_MODEL, |
| 675 | LINE_MODE_PINCFG, |
| 676 | LINE_MODE_VERB, |
| 677 | LINE_MODE_HINT, |
Takashi Iwai | b09f3e7 | 2010-01-28 00:01:53 +0100 | [diff] [blame] | 678 | LINE_MODE_VENDOR_ID, |
| 679 | LINE_MODE_SUBSYSTEM_ID, |
| 680 | LINE_MODE_REVISION_ID, |
| 681 | LINE_MODE_CHIP_NAME, |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 682 | NUM_LINE_MODES, |
| 683 | }; |
| 684 | |
| 685 | static inline int strmatch(const char *a, const char *b) |
| 686 | { |
| 687 | return strnicmp(a, b, strlen(b)) == 0; |
| 688 | } |
| 689 | |
| 690 | /* parse the contents after the line "[codec]" |
| 691 | * accept only the line with three numbers, and assign the current codec |
| 692 | */ |
| 693 | static void parse_codec_mode(char *buf, struct hda_bus *bus, |
| 694 | struct hda_codec **codecp) |
| 695 | { |
Takashi Iwai | ef940b0 | 2011-09-28 20:12:08 +0200 | [diff] [blame] | 696 | int vendorid, subid, caddr; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 697 | struct hda_codec *codec; |
| 698 | |
| 699 | *codecp = NULL; |
| 700 | if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) { |
| 701 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | ef940b0 | 2011-09-28 20:12:08 +0200 | [diff] [blame] | 702 | if ((vendorid <= 0 || codec->vendor_id == vendorid) && |
| 703 | (subid <= 0 || codec->subsystem_id == subid) && |
David Henningsson | 2385b78 | 2010-06-02 16:56:41 +0200 | [diff] [blame] | 704 | codec->addr == caddr) { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 705 | *codecp = codec; |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* parse the contents after the other command tags, [pincfg], [verb], |
Takashi Iwai | b09f3e7 | 2010-01-28 00:01:53 +0100 | [diff] [blame] | 713 | * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model] |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 714 | * just pass to the sysfs helper (only when any codec was specified) |
| 715 | */ |
| 716 | static void parse_pincfg_mode(char *buf, struct hda_bus *bus, |
| 717 | struct hda_codec **codecp) |
| 718 | { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 719 | parse_user_pin_configs(*codecp, buf); |
| 720 | } |
| 721 | |
| 722 | static void parse_verb_mode(char *buf, struct hda_bus *bus, |
| 723 | struct hda_codec **codecp) |
| 724 | { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 725 | parse_init_verbs(*codecp, buf); |
| 726 | } |
| 727 | |
| 728 | static void parse_hint_mode(char *buf, struct hda_bus *bus, |
| 729 | struct hda_codec **codecp) |
| 730 | { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 731 | parse_hints(*codecp, buf); |
| 732 | } |
| 733 | |
| 734 | static void parse_model_mode(char *buf, struct hda_bus *bus, |
| 735 | struct hda_codec **codecp) |
| 736 | { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 737 | kfree((*codecp)->modelname); |
| 738 | (*codecp)->modelname = kstrdup(buf, GFP_KERNEL); |
| 739 | } |
| 740 | |
Takashi Iwai | b09f3e7 | 2010-01-28 00:01:53 +0100 | [diff] [blame] | 741 | static void parse_chip_name_mode(char *buf, struct hda_bus *bus, |
| 742 | struct hda_codec **codecp) |
| 743 | { |
| 744 | kfree((*codecp)->chip_name); |
| 745 | (*codecp)->chip_name = kstrdup(buf, GFP_KERNEL); |
| 746 | } |
| 747 | |
| 748 | #define DEFINE_PARSE_ID_MODE(name) \ |
| 749 | static void parse_##name##_mode(char *buf, struct hda_bus *bus, \ |
| 750 | struct hda_codec **codecp) \ |
| 751 | { \ |
| 752 | unsigned long val; \ |
Jingoo Han | b785a49 | 2013-07-19 16:24:59 +0900 | [diff] [blame] | 753 | if (!kstrtoul(buf, 0, &val)) \ |
Takashi Iwai | b09f3e7 | 2010-01-28 00:01:53 +0100 | [diff] [blame] | 754 | (*codecp)->name = val; \ |
| 755 | } |
| 756 | |
| 757 | DEFINE_PARSE_ID_MODE(vendor_id); |
| 758 | DEFINE_PARSE_ID_MODE(subsystem_id); |
| 759 | DEFINE_PARSE_ID_MODE(revision_id); |
| 760 | |
| 761 | |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 762 | struct hda_patch_item { |
| 763 | const char *tag; |
Takashi Iwai | 8e3ae6f | 2014-01-07 15:28:51 +0100 | [diff] [blame] | 764 | const char *alias; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 765 | void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc); |
| 766 | }; |
| 767 | |
| 768 | static struct hda_patch_item patch_items[NUM_LINE_MODES] = { |
Takashi Iwai | 8e3ae6f | 2014-01-07 15:28:51 +0100 | [diff] [blame] | 769 | [LINE_MODE_CODEC] = { |
| 770 | .tag = "[codec]", |
| 771 | .parser = parse_codec_mode, |
| 772 | }, |
| 773 | [LINE_MODE_MODEL] = { |
| 774 | .tag = "[model]", |
| 775 | .parser = parse_model_mode, |
| 776 | }, |
| 777 | [LINE_MODE_VERB] = { |
| 778 | .tag = "[verb]", |
| 779 | .alias = "[init_verbs]", |
| 780 | .parser = parse_verb_mode, |
| 781 | }, |
| 782 | [LINE_MODE_PINCFG] = { |
| 783 | .tag = "[pincfg]", |
| 784 | .alias = "[user_pin_configs]", |
| 785 | .parser = parse_pincfg_mode, |
| 786 | }, |
| 787 | [LINE_MODE_HINT] = { |
| 788 | .tag = "[hint]", |
| 789 | .alias = "[hints]", |
| 790 | .parser = parse_hint_mode |
| 791 | }, |
| 792 | [LINE_MODE_VENDOR_ID] = { |
| 793 | .tag = "[vendor_id]", |
| 794 | .parser = parse_vendor_id_mode, |
| 795 | }, |
| 796 | [LINE_MODE_SUBSYSTEM_ID] = { |
| 797 | .tag = "[subsystem_id]", |
| 798 | .parser = parse_subsystem_id_mode, |
| 799 | }, |
| 800 | [LINE_MODE_REVISION_ID] = { |
| 801 | .tag = "[revision_id]", |
| 802 | .parser = parse_revision_id_mode, |
| 803 | }, |
| 804 | [LINE_MODE_CHIP_NAME] = { |
| 805 | .tag = "[chip_name]", |
| 806 | .parser = parse_chip_name_mode, |
| 807 | }, |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 808 | }; |
| 809 | |
| 810 | /* check the line starting with '[' -- change the parser mode accodingly */ |
| 811 | static int parse_line_mode(char *buf, struct hda_bus *bus) |
| 812 | { |
| 813 | int i; |
| 814 | for (i = 0; i < ARRAY_SIZE(patch_items); i++) { |
| 815 | if (!patch_items[i].tag) |
| 816 | continue; |
| 817 | if (strmatch(buf, patch_items[i].tag)) |
| 818 | return i; |
Takashi Iwai | 8e3ae6f | 2014-01-07 15:28:51 +0100 | [diff] [blame] | 819 | if (patch_items[i].alias && strmatch(buf, patch_items[i].alias)) |
| 820 | return i; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 821 | } |
| 822 | return LINE_MODE_NONE; |
| 823 | } |
| 824 | |
| 825 | /* copy one line from the buffer in fw, and update the fields in fw |
| 826 | * return zero if it reaches to the end of the buffer, or non-zero |
| 827 | * if successfully copied a line |
| 828 | * |
| 829 | * the spaces at the beginning and the end of the line are stripped |
| 830 | */ |
Takashi Iwai | 4918cda | 2012-08-09 12:33:28 +0200 | [diff] [blame] | 831 | static int get_line_from_fw(char *buf, int size, size_t *fw_size_p, |
| 832 | const void **fw_data_p) |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 833 | { |
| 834 | int len; |
Takashi Iwai | 4918cda | 2012-08-09 12:33:28 +0200 | [diff] [blame] | 835 | size_t fw_size = *fw_size_p; |
| 836 | const char *p = *fw_data_p; |
| 837 | |
| 838 | while (isspace(*p) && fw_size) { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 839 | p++; |
Takashi Iwai | 4918cda | 2012-08-09 12:33:28 +0200 | [diff] [blame] | 840 | fw_size--; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 841 | } |
Takashi Iwai | 4918cda | 2012-08-09 12:33:28 +0200 | [diff] [blame] | 842 | if (!fw_size) |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 843 | return 0; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 844 | |
Takashi Iwai | 4918cda | 2012-08-09 12:33:28 +0200 | [diff] [blame] | 845 | for (len = 0; len < fw_size; len++) { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 846 | if (!*p) |
| 847 | break; |
| 848 | if (*p == '\n') { |
| 849 | p++; |
| 850 | len++; |
| 851 | break; |
| 852 | } |
| 853 | if (len < size) |
| 854 | *buf++ = *p++; |
| 855 | } |
| 856 | *buf = 0; |
Takashi Iwai | 4918cda | 2012-08-09 12:33:28 +0200 | [diff] [blame] | 857 | *fw_size_p = fw_size - len; |
| 858 | *fw_data_p = p; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 859 | remove_trail_spaces(buf); |
| 860 | return 1; |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * load a "patch" firmware file and parse it |
| 865 | */ |
Takashi Iwai | 4918cda | 2012-08-09 12:33:28 +0200 | [diff] [blame] | 866 | int snd_hda_load_patch(struct hda_bus *bus, size_t fw_size, const void *fw_buf) |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 867 | { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 868 | char buf[128]; |
| 869 | struct hda_codec *codec; |
| 870 | int line_mode; |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 871 | |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 872 | line_mode = LINE_MODE_NONE; |
| 873 | codec = NULL; |
Takashi Iwai | 4918cda | 2012-08-09 12:33:28 +0200 | [diff] [blame] | 874 | while (get_line_from_fw(buf, sizeof(buf) - 1, &fw_size, &fw_buf)) { |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 875 | if (!*buf || *buf == '#' || *buf == '\n') |
| 876 | continue; |
| 877 | if (*buf == '[') |
| 878 | line_mode = parse_line_mode(buf, bus); |
Takashi Iwai | b09f3e7 | 2010-01-28 00:01:53 +0100 | [diff] [blame] | 879 | else if (patch_items[line_mode].parser && |
Takashi Iwai | d81e397 | 2014-01-07 15:23:44 +0100 | [diff] [blame] | 880 | (codec || line_mode <= LINE_MODE_CODEC)) |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 881 | patch_items[line_mode].parser(buf, bus, &codec); |
| 882 | } |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 883 | return 0; |
| 884 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 885 | EXPORT_SYMBOL_GPL(snd_hda_load_patch); |
Takashi Iwai | 4ea6fbc | 2009-06-17 09:52:54 +0200 | [diff] [blame] | 886 | #endif /* CONFIG_SND_HDA_PATCH_LOADER */ |