1 // SPDX-License-Identifier: GPL-2.0-only
3 * sysfs interface for HD-audio codec
5 * Copyright (c) 2014 Takashi Iwai <tiwai@suse.de>
7 * split from hda_hwdep.c
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/compat.h>
13 #include <linux/mutex.h>
14 #include <linux/ctype.h>
15 #include <linux/string.h>
16 #include <linux/export.h>
17 #include <sound/core.h>
18 #include <sound/hda_codec.h>
19 #include "hda_local.h"
20 #include <sound/hda_hwdep.h>
21 #include <sound/minors.h>
23 /* hint string pair */
26 const char *val; /* contained in the same alloc as key */
30 static ssize_t power_on_acct_show(struct device *dev,
31 struct device_attribute *attr,
34 struct hda_codec *codec = dev_get_drvdata(dev);
35 snd_hda_update_power_acct(codec);
36 return sysfs_emit(buf, "%u\n", jiffies_to_msecs(codec->power_on_acct));
39 static ssize_t power_off_acct_show(struct device *dev,
40 struct device_attribute *attr,
43 struct hda_codec *codec = dev_get_drvdata(dev);
44 snd_hda_update_power_acct(codec);
45 return sysfs_emit(buf, "%u\n", jiffies_to_msecs(codec->power_off_acct));
48 static DEVICE_ATTR_RO(power_on_acct);
49 static DEVICE_ATTR_RO(power_off_acct);
50 #endif /* CONFIG_PM */
52 #define CODEC_INFO_SHOW(type, field) \
53 static ssize_t type##_show(struct device *dev, \
54 struct device_attribute *attr, \
57 struct hda_codec *codec = dev_get_drvdata(dev); \
58 return sysfs_emit(buf, "0x%x\n", codec->field); \
61 #define CODEC_INFO_STR_SHOW(type, field) \
62 static ssize_t type##_show(struct device *dev, \
63 struct device_attribute *attr, \
66 struct hda_codec *codec = dev_get_drvdata(dev); \
67 return sysfs_emit(buf, "%s\n", \
68 codec->field ? codec->field : ""); \
71 CODEC_INFO_SHOW(vendor_id, core.vendor_id);
72 CODEC_INFO_SHOW(subsystem_id, core.subsystem_id);
73 CODEC_INFO_SHOW(revision_id, core.revision_id);
74 CODEC_INFO_SHOW(afg, core.afg);
75 CODEC_INFO_SHOW(mfg, core.mfg);
76 CODEC_INFO_STR_SHOW(vendor_name, core.vendor_name);
77 CODEC_INFO_STR_SHOW(chip_name, core.chip_name);
78 CODEC_INFO_STR_SHOW(modelname, modelname);
80 static ssize_t pin_configs_show(struct hda_codec *codec,
81 struct snd_array *list,
84 const struct hda_pincfg *pin;
86 mutex_lock(&codec->user_mutex);
87 snd_array_for_each(list, i, pin) {
88 len += sysfs_emit_at(buf, len, "0x%02x 0x%08x\n",
91 mutex_unlock(&codec->user_mutex);
95 static ssize_t init_pin_configs_show(struct device *dev,
96 struct device_attribute *attr,
99 struct hda_codec *codec = dev_get_drvdata(dev);
100 return pin_configs_show(codec, &codec->init_pins, buf);
103 static ssize_t driver_pin_configs_show(struct device *dev,
104 struct device_attribute *attr,
107 struct hda_codec *codec = dev_get_drvdata(dev);
108 return pin_configs_show(codec, &codec->driver_pins, buf);
111 #ifdef CONFIG_SND_HDA_RECONFIG
117 static int clear_codec(struct hda_codec *codec)
121 err = snd_hda_codec_reset(codec);
123 codec_err(codec, "The codec is being used, can't free.\n");
126 snd_hda_sysfs_clear(codec);
130 static int reconfig_codec(struct hda_codec *codec)
134 snd_hda_power_up(codec);
135 codec_info(codec, "hda-codec: reconfiguring\n");
136 err = snd_hda_codec_reset(codec);
139 "The codec is being used, can't reconfigure.\n");
142 err = device_reprobe(hda_codec_dev(codec));
145 err = snd_card_register(codec->card);
147 snd_hda_power_down(codec);
152 * allocate a string at most len chars, and remove the trailing EOL
154 static char *kstrndup_noeol(const char *src, size_t len)
156 char *s = kstrndup(src, len, GFP_KERNEL);
166 #define CODEC_INFO_STORE(type, field) \
167 static ssize_t type##_store(struct device *dev, \
168 struct device_attribute *attr, \
169 const char *buf, size_t count) \
171 struct hda_codec *codec = dev_get_drvdata(dev); \
173 int err = kstrtoul(buf, 0, &val); \
176 codec->field = val; \
180 #define CODEC_INFO_STR_STORE(type, field) \
181 static ssize_t type##_store(struct device *dev, \
182 struct device_attribute *attr, \
183 const char *buf, size_t count) \
185 struct hda_codec *codec = dev_get_drvdata(dev); \
186 char *s = kstrndup_noeol(buf, 64); \
189 kfree(codec->field); \
194 CODEC_INFO_STORE(vendor_id, core.vendor_id);
195 CODEC_INFO_STORE(subsystem_id, core.subsystem_id);
196 CODEC_INFO_STORE(revision_id, core.revision_id);
197 CODEC_INFO_STR_STORE(vendor_name, core.vendor_name);
198 CODEC_INFO_STR_STORE(chip_name, core.chip_name);
199 CODEC_INFO_STR_STORE(modelname, modelname);
201 #define CODEC_ACTION_STORE(type) \
202 static ssize_t type##_store(struct device *dev, \
203 struct device_attribute *attr, \
204 const char *buf, size_t count) \
206 struct hda_codec *codec = dev_get_drvdata(dev); \
209 err = type##_codec(codec); \
210 return err < 0 ? err : count; \
213 CODEC_ACTION_STORE(reconfig);
214 CODEC_ACTION_STORE(clear);
216 static ssize_t init_verbs_show(struct device *dev,
217 struct device_attribute *attr,
220 struct hda_codec *codec = dev_get_drvdata(dev);
221 const struct hda_verb *v;
223 mutex_lock(&codec->user_mutex);
224 snd_array_for_each(&codec->init_verbs, i, v) {
225 len += sysfs_emit_at(buf, len, "0x%02x 0x%03x 0x%04x\n",
226 v->nid, v->verb, v->param);
228 mutex_unlock(&codec->user_mutex);
232 static int parse_init_verbs(struct hda_codec *codec, const char *buf)
235 int nid, verb, param;
237 if (sscanf(buf, "%i %i %i", &nid, &verb, ¶m) != 3)
241 mutex_lock(&codec->user_mutex);
242 v = snd_array_new(&codec->init_verbs);
244 mutex_unlock(&codec->user_mutex);
250 mutex_unlock(&codec->user_mutex);
254 static ssize_t init_verbs_store(struct device *dev,
255 struct device_attribute *attr,
256 const char *buf, size_t count)
258 struct hda_codec *codec = dev_get_drvdata(dev);
259 int err = parse_init_verbs(codec, buf);
265 static ssize_t hints_show(struct device *dev,
266 struct device_attribute *attr,
269 struct hda_codec *codec = dev_get_drvdata(dev);
270 const struct hda_hint *hint;
272 mutex_lock(&codec->user_mutex);
273 snd_array_for_each(&codec->hints, i, hint) {
274 len += sysfs_emit_at(buf, len, "%s = %s\n",
275 hint->key, hint->val);
277 mutex_unlock(&codec->user_mutex);
281 static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
283 struct hda_hint *hint;
286 snd_array_for_each(&codec->hints, i, hint) {
287 if (!strcmp(hint->key, key))
293 static void remove_trail_spaces(char *str)
298 p = str + strlen(str) - 1;
299 for (; isspace(*p); p--) {
306 #define MAX_HINTS 1024
308 static int parse_hints(struct hda_codec *codec, const char *buf)
311 struct hda_hint *hint;
314 buf = skip_spaces(buf);
315 if (!*buf || *buf == '#' || *buf == '\n')
319 key = kstrndup_noeol(buf, 1024);
322 /* extract key and val */
323 val = strchr(key, '=');
329 val = skip_spaces(val);
330 remove_trail_spaces(key);
331 remove_trail_spaces(val);
332 mutex_lock(&codec->user_mutex);
333 hint = get_hint(codec, key);
341 /* allocate a new hint entry */
342 if (codec->hints.used >= MAX_HINTS)
345 hint = snd_array_new(&codec->hints);
353 mutex_unlock(&codec->user_mutex);
359 static ssize_t hints_store(struct device *dev,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
363 struct hda_codec *codec = dev_get_drvdata(dev);
364 int err = parse_hints(codec, buf);
370 static ssize_t user_pin_configs_show(struct device *dev,
371 struct device_attribute *attr,
374 struct hda_codec *codec = dev_get_drvdata(dev);
375 return pin_configs_show(codec, &codec->user_pins, buf);
378 static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
382 if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
386 mutex_lock(&codec->user_mutex);
387 err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
388 mutex_unlock(&codec->user_mutex);
392 static ssize_t user_pin_configs_store(struct device *dev,
393 struct device_attribute *attr,
394 const char *buf, size_t count)
396 struct hda_codec *codec = dev_get_drvdata(dev);
397 int err = parse_user_pin_configs(codec, buf);
403 /* sysfs attributes exposed only when CONFIG_SND_HDA_RECONFIG=y */
404 static DEVICE_ATTR_RW(init_verbs);
405 static DEVICE_ATTR_RW(hints);
406 static DEVICE_ATTR_RW(user_pin_configs);
407 static DEVICE_ATTR_WO(reconfig);
408 static DEVICE_ATTR_WO(clear);
411 * snd_hda_get_hint - Look for hint string
412 * @codec: the HDA codec
413 * @key: the hint key string
415 * Look for a hint key/value pair matching with the given key string
416 * and returns the value string. If nothing found, returns NULL.
418 const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
420 struct hda_hint *hint = get_hint(codec, key);
421 return hint ? hint->val : NULL;
423 EXPORT_SYMBOL_GPL(snd_hda_get_hint);
426 * snd_hda_get_bool_hint - Get a boolean hint value
427 * @codec: the HDA codec
428 * @key: the hint key string
430 * Look for a hint key/value pair matching with the given key string
431 * and returns a boolean value parsed from the value. If no matching
432 * key is found, return a negative value.
434 int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
439 mutex_lock(&codec->user_mutex);
440 p = snd_hda_get_hint(codec, key);
444 switch (toupper(*p)) {
455 mutex_unlock(&codec->user_mutex);
458 EXPORT_SYMBOL_GPL(snd_hda_get_bool_hint);
461 * snd_hda_get_int_hint - Get an integer hint value
462 * @codec: the HDA codec
463 * @key: the hint key string
464 * @valp: pointer to store a value
466 * Look for a hint key/value pair matching with the given key string
467 * and stores the integer value to @valp. If no matching key is found,
468 * return a negative error code. Otherwise it returns zero.
470 int snd_hda_get_int_hint(struct hda_codec *codec, const char *key, int *valp)
476 mutex_lock(&codec->user_mutex);
477 p = snd_hda_get_hint(codec, key);
480 else if (kstrtoul(p, 0, &val))
486 mutex_unlock(&codec->user_mutex);
489 EXPORT_SYMBOL_GPL(snd_hda_get_int_hint);
490 #endif /* CONFIG_SND_HDA_RECONFIG */
493 * common sysfs attributes
495 #ifdef CONFIG_SND_HDA_RECONFIG
496 #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RW(name)
498 #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RO(name)
500 static RECONFIG_DEVICE_ATTR(vendor_id);
501 static RECONFIG_DEVICE_ATTR(subsystem_id);
502 static RECONFIG_DEVICE_ATTR(revision_id);
503 static DEVICE_ATTR_RO(afg);
504 static DEVICE_ATTR_RO(mfg);
505 static RECONFIG_DEVICE_ATTR(vendor_name);
506 static RECONFIG_DEVICE_ATTR(chip_name);
507 static RECONFIG_DEVICE_ATTR(modelname);
508 static DEVICE_ATTR_RO(init_pin_configs);
509 static DEVICE_ATTR_RO(driver_pin_configs);
512 #ifdef CONFIG_SND_HDA_PATCH_LOADER
523 LINE_MODE_SUBSYSTEM_ID,
524 LINE_MODE_REVISION_ID,
529 static inline int strmatch(const char *a, const char *b)
531 return strncasecmp(a, b, strlen(b)) == 0;
534 /* parse the contents after the line "[codec]"
535 * accept only the line with three numbers, and assign the current codec
537 static void parse_codec_mode(char *buf, struct hda_bus *bus,
538 struct hda_codec **codecp)
540 int vendorid, subid, caddr;
541 struct hda_codec *codec;
544 if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
545 list_for_each_codec(codec, bus) {
546 if ((vendorid <= 0 || codec->core.vendor_id == vendorid) &&
547 (subid <= 0 || codec->core.subsystem_id == subid) &&
548 codec->core.addr == caddr) {
556 /* parse the contents after the other command tags, [pincfg], [verb],
557 * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model]
558 * just pass to the sysfs helper (only when any codec was specified)
560 static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
561 struct hda_codec **codecp)
563 parse_user_pin_configs(*codecp, buf);
566 static void parse_verb_mode(char *buf, struct hda_bus *bus,
567 struct hda_codec **codecp)
569 parse_init_verbs(*codecp, buf);
572 static void parse_hint_mode(char *buf, struct hda_bus *bus,
573 struct hda_codec **codecp)
575 parse_hints(*codecp, buf);
578 static void parse_model_mode(char *buf, struct hda_bus *bus,
579 struct hda_codec **codecp)
581 kfree((*codecp)->modelname);
582 (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
585 static void parse_chip_name_mode(char *buf, struct hda_bus *bus,
586 struct hda_codec **codecp)
588 snd_hda_codec_set_name(*codecp, buf);
591 #define DEFINE_PARSE_ID_MODE(name) \
592 static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
593 struct hda_codec **codecp) \
596 if (!kstrtoul(buf, 0, &val)) \
597 (*codecp)->core.name = val; \
600 DEFINE_PARSE_ID_MODE(vendor_id);
601 DEFINE_PARSE_ID_MODE(subsystem_id);
602 DEFINE_PARSE_ID_MODE(revision_id);
605 struct hda_patch_item {
608 void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
611 static const struct hda_patch_item patch_items[NUM_LINE_MODES] = {
612 [LINE_MODE_CODEC] = {
614 .parser = parse_codec_mode,
616 [LINE_MODE_MODEL] = {
618 .parser = parse_model_mode,
622 .alias = "[init_verbs]",
623 .parser = parse_verb_mode,
625 [LINE_MODE_PINCFG] = {
627 .alias = "[user_pin_configs]",
628 .parser = parse_pincfg_mode,
633 .parser = parse_hint_mode
635 [LINE_MODE_VENDOR_ID] = {
636 .tag = "[vendor_id]",
637 .parser = parse_vendor_id_mode,
639 [LINE_MODE_SUBSYSTEM_ID] = {
640 .tag = "[subsystem_id]",
641 .parser = parse_subsystem_id_mode,
643 [LINE_MODE_REVISION_ID] = {
644 .tag = "[revision_id]",
645 .parser = parse_revision_id_mode,
647 [LINE_MODE_CHIP_NAME] = {
648 .tag = "[chip_name]",
649 .parser = parse_chip_name_mode,
653 /* check the line starting with '[' -- change the parser mode accodingly */
654 static int parse_line_mode(char *buf, struct hda_bus *bus)
657 for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
658 if (!patch_items[i].tag)
660 if (strmatch(buf, patch_items[i].tag))
662 if (patch_items[i].alias && strmatch(buf, patch_items[i].alias))
665 return LINE_MODE_NONE;
668 /* copy one line from the buffer in fw, and update the fields in fw
669 * return zero if it reaches to the end of the buffer, or non-zero
670 * if successfully copied a line
672 * the spaces at the beginning and the end of the line are stripped
674 static int get_line_from_fw(char *buf, int size, size_t *fw_size_p,
675 const void **fw_data_p)
678 size_t fw_size = *fw_size_p;
679 const char *p = *fw_data_p;
681 while (isspace(*p) && fw_size) {
688 for (len = 0; len < fw_size; len++) {
700 *fw_size_p = fw_size - len;
702 remove_trail_spaces(buf);
707 * snd_hda_load_patch - load a "patch" firmware file and parse it
709 * @fw_size: the firmware byte size
710 * @fw_buf: the firmware data
712 int snd_hda_load_patch(struct hda_bus *bus, size_t fw_size, const void *fw_buf)
715 struct hda_codec *codec;
718 line_mode = LINE_MODE_NONE;
720 while (get_line_from_fw(buf, sizeof(buf) - 1, &fw_size, &fw_buf)) {
721 if (!*buf || *buf == '#' || *buf == '\n')
724 line_mode = parse_line_mode(buf, bus);
725 else if (patch_items[line_mode].parser &&
726 (codec || line_mode <= LINE_MODE_CODEC))
727 patch_items[line_mode].parser(buf, bus, &codec);
731 EXPORT_SYMBOL_GPL(snd_hda_load_patch);
732 #endif /* CONFIG_SND_HDA_PATCH_LOADER */
737 static struct attribute *hda_dev_attrs[] = {
738 &dev_attr_vendor_id.attr,
739 &dev_attr_subsystem_id.attr,
740 &dev_attr_revision_id.attr,
743 &dev_attr_vendor_name.attr,
744 &dev_attr_chip_name.attr,
745 &dev_attr_modelname.attr,
746 &dev_attr_init_pin_configs.attr,
747 &dev_attr_driver_pin_configs.attr,
749 &dev_attr_power_on_acct.attr,
750 &dev_attr_power_off_acct.attr,
752 #ifdef CONFIG_SND_HDA_RECONFIG
753 &dev_attr_init_verbs.attr,
754 &dev_attr_hints.attr,
755 &dev_attr_user_pin_configs.attr,
756 &dev_attr_reconfig.attr,
757 &dev_attr_clear.attr,
762 static const struct attribute_group hda_dev_attr_group = {
763 .attrs = hda_dev_attrs,
766 const struct attribute_group *snd_hda_dev_attr_groups[] = {
771 void snd_hda_sysfs_init(struct hda_codec *codec)
773 mutex_init(&codec->user_mutex);
774 #ifdef CONFIG_SND_HDA_RECONFIG
775 snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
776 snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
777 snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
781 void snd_hda_sysfs_clear(struct hda_codec *codec)
783 #ifdef CONFIG_SND_HDA_RECONFIG
784 struct hda_hint *hint;
787 /* clear init verbs */
788 snd_array_free(&codec->init_verbs);
790 snd_array_for_each(&codec->hints, i, hint) {
791 kfree(hint->key); /* we don't need to free hint->val */
793 snd_array_free(&codec->hints);
794 snd_array_free(&codec->user_pins);