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 sprintf(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 sprintf(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 sprintf(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 sprintf(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 += sprintf(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 = snd_hda_codec_configure(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 += snprintf(buf + len, PAGE_SIZE - len,
226 "0x%02x 0x%03x 0x%04x\n",
227 v->nid, v->verb, v->param);
229 mutex_unlock(&codec->user_mutex);
233 static int parse_init_verbs(struct hda_codec *codec, const char *buf)
236 int nid, verb, param;
238 if (sscanf(buf, "%i %i %i", &nid, &verb, ¶m) != 3)
242 mutex_lock(&codec->user_mutex);
243 v = snd_array_new(&codec->init_verbs);
245 mutex_unlock(&codec->user_mutex);
251 mutex_unlock(&codec->user_mutex);
255 static ssize_t init_verbs_store(struct device *dev,
256 struct device_attribute *attr,
257 const char *buf, size_t count)
259 struct hda_codec *codec = dev_get_drvdata(dev);
260 int err = parse_init_verbs(codec, buf);
266 static ssize_t hints_show(struct device *dev,
267 struct device_attribute *attr,
270 struct hda_codec *codec = dev_get_drvdata(dev);
271 const struct hda_hint *hint;
273 mutex_lock(&codec->user_mutex);
274 snd_array_for_each(&codec->hints, i, hint) {
275 len += snprintf(buf + len, PAGE_SIZE - len,
276 "%s = %s\n", hint->key, hint->val);
278 mutex_unlock(&codec->user_mutex);
282 static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
284 struct hda_hint *hint;
287 snd_array_for_each(&codec->hints, i, hint) {
288 if (!strcmp(hint->key, key))
294 static void remove_trail_spaces(char *str)
299 p = str + strlen(str) - 1;
300 for (; isspace(*p); p--) {
307 #define MAX_HINTS 1024
309 static int parse_hints(struct hda_codec *codec, const char *buf)
312 struct hda_hint *hint;
315 buf = skip_spaces(buf);
316 if (!*buf || *buf == '#' || *buf == '\n')
320 key = kstrndup_noeol(buf, 1024);
323 /* extract key and val */
324 val = strchr(key, '=');
330 val = skip_spaces(val);
331 remove_trail_spaces(key);
332 remove_trail_spaces(val);
333 mutex_lock(&codec->user_mutex);
334 hint = get_hint(codec, key);
342 /* allocate a new hint entry */
343 if (codec->hints.used >= MAX_HINTS)
346 hint = snd_array_new(&codec->hints);
354 mutex_unlock(&codec->user_mutex);
360 static ssize_t hints_store(struct device *dev,
361 struct device_attribute *attr,
362 const char *buf, size_t count)
364 struct hda_codec *codec = dev_get_drvdata(dev);
365 int err = parse_hints(codec, buf);
371 static ssize_t user_pin_configs_show(struct device *dev,
372 struct device_attribute *attr,
375 struct hda_codec *codec = dev_get_drvdata(dev);
376 return pin_configs_show(codec, &codec->user_pins, buf);
379 #define MAX_PIN_CONFIGS 32
381 static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
385 if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
389 mutex_lock(&codec->user_mutex);
390 err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
391 mutex_unlock(&codec->user_mutex);
395 static ssize_t user_pin_configs_store(struct device *dev,
396 struct device_attribute *attr,
397 const char *buf, size_t count)
399 struct hda_codec *codec = dev_get_drvdata(dev);
400 int err = parse_user_pin_configs(codec, buf);
406 /* sysfs attributes exposed only when CONFIG_SND_HDA_RECONFIG=y */
407 static DEVICE_ATTR_RW(init_verbs);
408 static DEVICE_ATTR_RW(hints);
409 static DEVICE_ATTR_RW(user_pin_configs);
410 static DEVICE_ATTR_WO(reconfig);
411 static DEVICE_ATTR_WO(clear);
414 * snd_hda_get_hint - Look for hint string
415 * @codec: the HDA codec
416 * @key: the hint key string
418 * Look for a hint key/value pair matching with the given key string
419 * and returns the value string. If nothing found, returns NULL.
421 const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
423 struct hda_hint *hint = get_hint(codec, key);
424 return hint ? hint->val : NULL;
426 EXPORT_SYMBOL_GPL(snd_hda_get_hint);
429 * snd_hda_get_bool_hint - Get a boolean hint value
430 * @codec: the HDA codec
431 * @key: the hint key string
433 * Look for a hint key/value pair matching with the given key string
434 * and returns a boolean value parsed from the value. If no matching
435 * key is found, return a negative value.
437 int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
442 mutex_lock(&codec->user_mutex);
443 p = snd_hda_get_hint(codec, key);
447 switch (toupper(*p)) {
458 mutex_unlock(&codec->user_mutex);
461 EXPORT_SYMBOL_GPL(snd_hda_get_bool_hint);
464 * snd_hda_get_int_hint - Get an integer hint value
465 * @codec: the HDA codec
466 * @key: the hint key string
467 * @valp: pointer to store a value
469 * Look for a hint key/value pair matching with the given key string
470 * and stores the integer value to @valp. If no matching key is found,
471 * return a negative error code. Otherwise it returns zero.
473 int snd_hda_get_int_hint(struct hda_codec *codec, const char *key, int *valp)
479 mutex_lock(&codec->user_mutex);
480 p = snd_hda_get_hint(codec, key);
483 else if (kstrtoul(p, 0, &val))
489 mutex_unlock(&codec->user_mutex);
492 EXPORT_SYMBOL_GPL(snd_hda_get_int_hint);
493 #endif /* CONFIG_SND_HDA_RECONFIG */
496 * common sysfs attributes
498 #ifdef CONFIG_SND_HDA_RECONFIG
499 #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RW(name)
501 #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RO(name)
503 static RECONFIG_DEVICE_ATTR(vendor_id);
504 static RECONFIG_DEVICE_ATTR(subsystem_id);
505 static RECONFIG_DEVICE_ATTR(revision_id);
506 static DEVICE_ATTR_RO(afg);
507 static DEVICE_ATTR_RO(mfg);
508 static RECONFIG_DEVICE_ATTR(vendor_name);
509 static RECONFIG_DEVICE_ATTR(chip_name);
510 static RECONFIG_DEVICE_ATTR(modelname);
511 static DEVICE_ATTR_RO(init_pin_configs);
512 static DEVICE_ATTR_RO(driver_pin_configs);
515 #ifdef CONFIG_SND_HDA_PATCH_LOADER
526 LINE_MODE_SUBSYSTEM_ID,
527 LINE_MODE_REVISION_ID,
532 static inline int strmatch(const char *a, const char *b)
534 return strncasecmp(a, b, strlen(b)) == 0;
537 /* parse the contents after the line "[codec]"
538 * accept only the line with three numbers, and assign the current codec
540 static void parse_codec_mode(char *buf, struct hda_bus *bus,
541 struct hda_codec **codecp)
543 int vendorid, subid, caddr;
544 struct hda_codec *codec;
547 if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
548 list_for_each_codec(codec, bus) {
549 if ((vendorid <= 0 || codec->core.vendor_id == vendorid) &&
550 (subid <= 0 || codec->core.subsystem_id == subid) &&
551 codec->core.addr == caddr) {
559 /* parse the contents after the other command tags, [pincfg], [verb],
560 * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model]
561 * just pass to the sysfs helper (only when any codec was specified)
563 static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
564 struct hda_codec **codecp)
566 parse_user_pin_configs(*codecp, buf);
569 static void parse_verb_mode(char *buf, struct hda_bus *bus,
570 struct hda_codec **codecp)
572 parse_init_verbs(*codecp, buf);
575 static void parse_hint_mode(char *buf, struct hda_bus *bus,
576 struct hda_codec **codecp)
578 parse_hints(*codecp, buf);
581 static void parse_model_mode(char *buf, struct hda_bus *bus,
582 struct hda_codec **codecp)
584 kfree((*codecp)->modelname);
585 (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
588 static void parse_chip_name_mode(char *buf, struct hda_bus *bus,
589 struct hda_codec **codecp)
591 snd_hda_codec_set_name(*codecp, buf);
594 #define DEFINE_PARSE_ID_MODE(name) \
595 static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
596 struct hda_codec **codecp) \
599 if (!kstrtoul(buf, 0, &val)) \
600 (*codecp)->core.name = val; \
603 DEFINE_PARSE_ID_MODE(vendor_id);
604 DEFINE_PARSE_ID_MODE(subsystem_id);
605 DEFINE_PARSE_ID_MODE(revision_id);
608 struct hda_patch_item {
611 void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
614 static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
615 [LINE_MODE_CODEC] = {
617 .parser = parse_codec_mode,
619 [LINE_MODE_MODEL] = {
621 .parser = parse_model_mode,
625 .alias = "[init_verbs]",
626 .parser = parse_verb_mode,
628 [LINE_MODE_PINCFG] = {
630 .alias = "[user_pin_configs]",
631 .parser = parse_pincfg_mode,
636 .parser = parse_hint_mode
638 [LINE_MODE_VENDOR_ID] = {
639 .tag = "[vendor_id]",
640 .parser = parse_vendor_id_mode,
642 [LINE_MODE_SUBSYSTEM_ID] = {
643 .tag = "[subsystem_id]",
644 .parser = parse_subsystem_id_mode,
646 [LINE_MODE_REVISION_ID] = {
647 .tag = "[revision_id]",
648 .parser = parse_revision_id_mode,
650 [LINE_MODE_CHIP_NAME] = {
651 .tag = "[chip_name]",
652 .parser = parse_chip_name_mode,
656 /* check the line starting with '[' -- change the parser mode accodingly */
657 static int parse_line_mode(char *buf, struct hda_bus *bus)
660 for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
661 if (!patch_items[i].tag)
663 if (strmatch(buf, patch_items[i].tag))
665 if (patch_items[i].alias && strmatch(buf, patch_items[i].alias))
668 return LINE_MODE_NONE;
671 /* copy one line from the buffer in fw, and update the fields in fw
672 * return zero if it reaches to the end of the buffer, or non-zero
673 * if successfully copied a line
675 * the spaces at the beginning and the end of the line are stripped
677 static int get_line_from_fw(char *buf, int size, size_t *fw_size_p,
678 const void **fw_data_p)
681 size_t fw_size = *fw_size_p;
682 const char *p = *fw_data_p;
684 while (isspace(*p) && fw_size) {
691 for (len = 0; len < fw_size; len++) {
703 *fw_size_p = fw_size - len;
705 remove_trail_spaces(buf);
710 * snd_hda_load_patch - load a "patch" firmware file and parse it
712 * @fw_size: the firmware byte size
713 * @fw_buf: the firmware data
715 int snd_hda_load_patch(struct hda_bus *bus, size_t fw_size, const void *fw_buf)
718 struct hda_codec *codec;
721 line_mode = LINE_MODE_NONE;
723 while (get_line_from_fw(buf, sizeof(buf) - 1, &fw_size, &fw_buf)) {
724 if (!*buf || *buf == '#' || *buf == '\n')
727 line_mode = parse_line_mode(buf, bus);
728 else if (patch_items[line_mode].parser &&
729 (codec || line_mode <= LINE_MODE_CODEC))
730 patch_items[line_mode].parser(buf, bus, &codec);
734 EXPORT_SYMBOL_GPL(snd_hda_load_patch);
735 #endif /* CONFIG_SND_HDA_PATCH_LOADER */
740 static struct attribute *hda_dev_attrs[] = {
741 &dev_attr_vendor_id.attr,
742 &dev_attr_subsystem_id.attr,
743 &dev_attr_revision_id.attr,
746 &dev_attr_vendor_name.attr,
747 &dev_attr_chip_name.attr,
748 &dev_attr_modelname.attr,
749 &dev_attr_init_pin_configs.attr,
750 &dev_attr_driver_pin_configs.attr,
752 &dev_attr_power_on_acct.attr,
753 &dev_attr_power_off_acct.attr,
755 #ifdef CONFIG_SND_HDA_RECONFIG
756 &dev_attr_init_verbs.attr,
757 &dev_attr_hints.attr,
758 &dev_attr_user_pin_configs.attr,
759 &dev_attr_reconfig.attr,
760 &dev_attr_clear.attr,
765 static const struct attribute_group hda_dev_attr_group = {
766 .attrs = hda_dev_attrs,
769 const struct attribute_group *snd_hda_dev_attr_groups[] = {
774 void snd_hda_sysfs_init(struct hda_codec *codec)
776 mutex_init(&codec->user_mutex);
777 #ifdef CONFIG_SND_HDA_RECONFIG
778 snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
779 snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
780 snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
784 void snd_hda_sysfs_clear(struct hda_codec *codec)
786 #ifdef CONFIG_SND_HDA_RECONFIG
787 struct hda_hint *hint;
790 /* clear init verbs */
791 snd_array_free(&codec->init_verbs);
793 snd_array_for_each(&codec->hints, i, hint) {
794 kfree(hint->key); /* we don't need to free hint->val */
796 snd_array_free(&codec->hints);
797 snd_array_free(&codec->user_pins);