2 * sysfs interface for HD-audio codec
4 * Copyright (c) 2014 Takashi Iwai <tiwai@suse.de>
6 * split from hda_hwdep.c
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/compat.h>
12 #include <linux/mutex.h>
13 #include <linux/ctype.h>
14 #include <linux/string.h>
15 #include <linux/export.h>
16 #include <sound/core.h>
17 #include "hda_codec.h"
18 #include "hda_local.h"
19 #include <sound/hda_hwdep.h>
20 #include <sound/minors.h>
22 /* hint string pair */
25 const char *val; /* contained in the same alloc as key */
29 static ssize_t power_on_acct_show(struct device *dev,
30 struct device_attribute *attr,
33 struct hda_codec *codec = dev_get_drvdata(dev);
34 snd_hda_update_power_acct(codec);
35 return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_on_acct));
38 static ssize_t power_off_acct_show(struct device *dev,
39 struct device_attribute *attr,
42 struct hda_codec *codec = dev_get_drvdata(dev);
43 snd_hda_update_power_acct(codec);
44 return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_off_acct));
47 static DEVICE_ATTR_RO(power_on_acct);
48 static DEVICE_ATTR_RO(power_off_acct);
49 #endif /* CONFIG_PM */
51 #define CODEC_INFO_SHOW(type, field) \
52 static ssize_t type##_show(struct device *dev, \
53 struct device_attribute *attr, \
56 struct hda_codec *codec = dev_get_drvdata(dev); \
57 return sprintf(buf, "0x%x\n", codec->field); \
60 #define CODEC_INFO_STR_SHOW(type, field) \
61 static ssize_t type##_show(struct device *dev, \
62 struct device_attribute *attr, \
65 struct hda_codec *codec = dev_get_drvdata(dev); \
66 return sprintf(buf, "%s\n", \
67 codec->field ? codec->field : ""); \
70 CODEC_INFO_SHOW(vendor_id, core.vendor_id);
71 CODEC_INFO_SHOW(subsystem_id, core.subsystem_id);
72 CODEC_INFO_SHOW(revision_id, core.revision_id);
73 CODEC_INFO_SHOW(afg, core.afg);
74 CODEC_INFO_SHOW(mfg, core.mfg);
75 CODEC_INFO_STR_SHOW(vendor_name, core.vendor_name);
76 CODEC_INFO_STR_SHOW(chip_name, core.chip_name);
77 CODEC_INFO_STR_SHOW(modelname, modelname);
79 static ssize_t pin_configs_show(struct hda_codec *codec,
80 struct snd_array *list,
84 mutex_lock(&codec->user_mutex);
85 for (i = 0; i < list->used; i++) {
86 struct hda_pincfg *pin = snd_array_elem(list, i);
87 len += sprintf(buf + len, "0x%02x 0x%08x\n",
90 mutex_unlock(&codec->user_mutex);
94 static ssize_t init_pin_configs_show(struct device *dev,
95 struct device_attribute *attr,
98 struct hda_codec *codec = dev_get_drvdata(dev);
99 return pin_configs_show(codec, &codec->init_pins, buf);
102 static ssize_t driver_pin_configs_show(struct device *dev,
103 struct device_attribute *attr,
106 struct hda_codec *codec = dev_get_drvdata(dev);
107 return pin_configs_show(codec, &codec->driver_pins, buf);
110 #ifdef CONFIG_SND_HDA_RECONFIG
116 static int clear_codec(struct hda_codec *codec)
120 err = snd_hda_codec_reset(codec);
122 codec_err(codec, "The codec is being used, can't free.\n");
125 snd_hda_sysfs_clear(codec);
129 static int reconfig_codec(struct hda_codec *codec)
133 snd_hda_power_up(codec);
134 codec_info(codec, "hda-codec: reconfiguring\n");
135 err = snd_hda_codec_reset(codec);
138 "The codec is being used, can't reconfigure.\n");
141 err = snd_hda_codec_configure(codec);
145 err = snd_hda_codec_build_pcms(codec);
149 err = snd_hda_codec_build_controls(codec);
152 err = snd_card_register(codec->card);
154 snd_hda_power_down(codec);
159 * allocate a string at most len chars, and remove the trailing EOL
161 static char *kstrndup_noeol(const char *src, size_t len)
163 char *s = kstrndup(src, len, GFP_KERNEL);
173 #define CODEC_INFO_STORE(type, field) \
174 static ssize_t type##_store(struct device *dev, \
175 struct device_attribute *attr, \
176 const char *buf, size_t count) \
178 struct hda_codec *codec = dev_get_drvdata(dev); \
180 int err = kstrtoul(buf, 0, &val); \
183 codec->field = val; \
187 #define CODEC_INFO_STR_STORE(type, field) \
188 static ssize_t type##_store(struct device *dev, \
189 struct device_attribute *attr, \
190 const char *buf, size_t count) \
192 struct hda_codec *codec = dev_get_drvdata(dev); \
193 char *s = kstrndup_noeol(buf, 64); \
196 kfree(codec->field); \
201 CODEC_INFO_STORE(vendor_id, core.vendor_id);
202 CODEC_INFO_STORE(subsystem_id, core.subsystem_id);
203 CODEC_INFO_STORE(revision_id, core.revision_id);
204 CODEC_INFO_STR_STORE(vendor_name, core.vendor_name);
205 CODEC_INFO_STR_STORE(chip_name, core.chip_name);
206 CODEC_INFO_STR_STORE(modelname, modelname);
208 #define CODEC_ACTION_STORE(type) \
209 static ssize_t type##_store(struct device *dev, \
210 struct device_attribute *attr, \
211 const char *buf, size_t count) \
213 struct hda_codec *codec = dev_get_drvdata(dev); \
216 err = type##_codec(codec); \
217 return err < 0 ? err : count; \
220 CODEC_ACTION_STORE(reconfig);
221 CODEC_ACTION_STORE(clear);
223 static ssize_t init_verbs_show(struct device *dev,
224 struct device_attribute *attr,
227 struct hda_codec *codec = dev_get_drvdata(dev);
229 mutex_lock(&codec->user_mutex);
230 for (i = 0; i < codec->init_verbs.used; i++) {
231 struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
232 len += snprintf(buf + len, PAGE_SIZE - len,
233 "0x%02x 0x%03x 0x%04x\n",
234 v->nid, v->verb, v->param);
236 mutex_unlock(&codec->user_mutex);
240 static int parse_init_verbs(struct hda_codec *codec, const char *buf)
243 int nid, verb, param;
245 if (sscanf(buf, "%i %i %i", &nid, &verb, ¶m) != 3)
249 mutex_lock(&codec->user_mutex);
250 v = snd_array_new(&codec->init_verbs);
252 mutex_unlock(&codec->user_mutex);
258 mutex_unlock(&codec->user_mutex);
262 static ssize_t init_verbs_store(struct device *dev,
263 struct device_attribute *attr,
264 const char *buf, size_t count)
266 struct hda_codec *codec = dev_get_drvdata(dev);
267 int err = parse_init_verbs(codec, buf);
273 static ssize_t hints_show(struct device *dev,
274 struct device_attribute *attr,
277 struct hda_codec *codec = dev_get_drvdata(dev);
279 mutex_lock(&codec->user_mutex);
280 for (i = 0; i < codec->hints.used; i++) {
281 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
282 len += snprintf(buf + len, PAGE_SIZE - len,
283 "%s = %s\n", hint->key, hint->val);
285 mutex_unlock(&codec->user_mutex);
289 static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
293 for (i = 0; i < codec->hints.used; i++) {
294 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
295 if (!strcmp(hint->key, key))
301 static void remove_trail_spaces(char *str)
306 p = str + strlen(str) - 1;
307 for (; isspace(*p); p--) {
314 #define MAX_HINTS 1024
316 static int parse_hints(struct hda_codec *codec, const char *buf)
319 struct hda_hint *hint;
322 buf = skip_spaces(buf);
323 if (!*buf || *buf == '#' || *buf == '\n')
327 key = kstrndup_noeol(buf, 1024);
330 /* extract key and val */
331 val = strchr(key, '=');
337 val = skip_spaces(val);
338 remove_trail_spaces(key);
339 remove_trail_spaces(val);
340 mutex_lock(&codec->user_mutex);
341 hint = get_hint(codec, key);
349 /* allocate a new hint entry */
350 if (codec->hints.used >= MAX_HINTS)
353 hint = snd_array_new(&codec->hints);
361 mutex_unlock(&codec->user_mutex);
367 static ssize_t hints_store(struct device *dev,
368 struct device_attribute *attr,
369 const char *buf, size_t count)
371 struct hda_codec *codec = dev_get_drvdata(dev);
372 int err = parse_hints(codec, buf);
378 static ssize_t user_pin_configs_show(struct device *dev,
379 struct device_attribute *attr,
382 struct hda_codec *codec = dev_get_drvdata(dev);
383 return pin_configs_show(codec, &codec->user_pins, buf);
386 #define MAX_PIN_CONFIGS 32
388 static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
392 if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
396 mutex_lock(&codec->user_mutex);
397 err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
398 mutex_unlock(&codec->user_mutex);
402 static ssize_t user_pin_configs_store(struct device *dev,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
406 struct hda_codec *codec = dev_get_drvdata(dev);
407 int err = parse_user_pin_configs(codec, buf);
413 /* sysfs attributes exposed only when CONFIG_SND_HDA_RECONFIG=y */
414 static DEVICE_ATTR_RW(init_verbs);
415 static DEVICE_ATTR_RW(hints);
416 static DEVICE_ATTR_RW(user_pin_configs);
417 static DEVICE_ATTR_WO(reconfig);
418 static DEVICE_ATTR_WO(clear);
421 * snd_hda_get_hint - Look for hint string
422 * @codec: the HDA codec
423 * @key: the hint key string
425 * Look for a hint key/value pair matching with the given key string
426 * and returns the value string. If nothing found, returns NULL.
428 const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
430 struct hda_hint *hint = get_hint(codec, key);
431 return hint ? hint->val : NULL;
433 EXPORT_SYMBOL_GPL(snd_hda_get_hint);
436 * snd_hda_get_bool_hint - Get a boolean hint value
437 * @codec: the HDA codec
438 * @key: the hint key string
440 * Look for a hint key/value pair matching with the given key string
441 * and returns a boolean value parsed from the value. If no matching
442 * key is found, return a negative value.
444 int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
449 mutex_lock(&codec->user_mutex);
450 p = snd_hda_get_hint(codec, key);
454 switch (toupper(*p)) {
465 mutex_unlock(&codec->user_mutex);
468 EXPORT_SYMBOL_GPL(snd_hda_get_bool_hint);
471 * snd_hda_get_int_hint - Get an integer hint value
472 * @codec: the HDA codec
473 * @key: the hint key string
474 * @valp: pointer to store a value
476 * Look for a hint key/value pair matching with the given key string
477 * and stores the integer value to @valp. If no matching key is found,
478 * return a negative error code. Otherwise it returns zero.
480 int snd_hda_get_int_hint(struct hda_codec *codec, const char *key, int *valp)
486 mutex_lock(&codec->user_mutex);
487 p = snd_hda_get_hint(codec, key);
490 else if (kstrtoul(p, 0, &val))
496 mutex_unlock(&codec->user_mutex);
499 EXPORT_SYMBOL_GPL(snd_hda_get_int_hint);
500 #endif /* CONFIG_SND_HDA_RECONFIG */
503 * common sysfs attributes
505 #ifdef CONFIG_SND_HDA_RECONFIG
506 #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RW(name)
508 #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RO(name)
510 static RECONFIG_DEVICE_ATTR(vendor_id);
511 static RECONFIG_DEVICE_ATTR(subsystem_id);
512 static RECONFIG_DEVICE_ATTR(revision_id);
513 static DEVICE_ATTR_RO(afg);
514 static DEVICE_ATTR_RO(mfg);
515 static RECONFIG_DEVICE_ATTR(vendor_name);
516 static RECONFIG_DEVICE_ATTR(chip_name);
517 static RECONFIG_DEVICE_ATTR(modelname);
518 static DEVICE_ATTR_RO(init_pin_configs);
519 static DEVICE_ATTR_RO(driver_pin_configs);
522 #ifdef CONFIG_SND_HDA_PATCH_LOADER
533 LINE_MODE_SUBSYSTEM_ID,
534 LINE_MODE_REVISION_ID,
539 static inline int strmatch(const char *a, const char *b)
541 return strncasecmp(a, b, strlen(b)) == 0;
544 /* parse the contents after the line "[codec]"
545 * accept only the line with three numbers, and assign the current codec
547 static void parse_codec_mode(char *buf, struct hda_bus *bus,
548 struct hda_codec **codecp)
550 int vendorid, subid, caddr;
551 struct hda_codec *codec;
554 if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
555 list_for_each_codec(codec, bus) {
556 if ((vendorid <= 0 || codec->core.vendor_id == vendorid) &&
557 (subid <= 0 || codec->core.subsystem_id == subid) &&
558 codec->core.addr == caddr) {
566 /* parse the contents after the other command tags, [pincfg], [verb],
567 * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model]
568 * just pass to the sysfs helper (only when any codec was specified)
570 static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
571 struct hda_codec **codecp)
573 parse_user_pin_configs(*codecp, buf);
576 static void parse_verb_mode(char *buf, struct hda_bus *bus,
577 struct hda_codec **codecp)
579 parse_init_verbs(*codecp, buf);
582 static void parse_hint_mode(char *buf, struct hda_bus *bus,
583 struct hda_codec **codecp)
585 parse_hints(*codecp, buf);
588 static void parse_model_mode(char *buf, struct hda_bus *bus,
589 struct hda_codec **codecp)
591 kfree((*codecp)->modelname);
592 (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
595 static void parse_chip_name_mode(char *buf, struct hda_bus *bus,
596 struct hda_codec **codecp)
598 snd_hda_codec_set_name(*codecp, buf);
601 #define DEFINE_PARSE_ID_MODE(name) \
602 static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
603 struct hda_codec **codecp) \
606 if (!kstrtoul(buf, 0, &val)) \
607 (*codecp)->core.name = val; \
610 DEFINE_PARSE_ID_MODE(vendor_id);
611 DEFINE_PARSE_ID_MODE(subsystem_id);
612 DEFINE_PARSE_ID_MODE(revision_id);
615 struct hda_patch_item {
618 void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
621 static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
622 [LINE_MODE_CODEC] = {
624 .parser = parse_codec_mode,
626 [LINE_MODE_MODEL] = {
628 .parser = parse_model_mode,
632 .alias = "[init_verbs]",
633 .parser = parse_verb_mode,
635 [LINE_MODE_PINCFG] = {
637 .alias = "[user_pin_configs]",
638 .parser = parse_pincfg_mode,
643 .parser = parse_hint_mode
645 [LINE_MODE_VENDOR_ID] = {
646 .tag = "[vendor_id]",
647 .parser = parse_vendor_id_mode,
649 [LINE_MODE_SUBSYSTEM_ID] = {
650 .tag = "[subsystem_id]",
651 .parser = parse_subsystem_id_mode,
653 [LINE_MODE_REVISION_ID] = {
654 .tag = "[revision_id]",
655 .parser = parse_revision_id_mode,
657 [LINE_MODE_CHIP_NAME] = {
658 .tag = "[chip_name]",
659 .parser = parse_chip_name_mode,
663 /* check the line starting with '[' -- change the parser mode accodingly */
664 static int parse_line_mode(char *buf, struct hda_bus *bus)
667 for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
668 if (!patch_items[i].tag)
670 if (strmatch(buf, patch_items[i].tag))
672 if (patch_items[i].alias && strmatch(buf, patch_items[i].alias))
675 return LINE_MODE_NONE;
678 /* copy one line from the buffer in fw, and update the fields in fw
679 * return zero if it reaches to the end of the buffer, or non-zero
680 * if successfully copied a line
682 * the spaces at the beginning and the end of the line are stripped
684 static int get_line_from_fw(char *buf, int size, size_t *fw_size_p,
685 const void **fw_data_p)
688 size_t fw_size = *fw_size_p;
689 const char *p = *fw_data_p;
691 while (isspace(*p) && fw_size) {
698 for (len = 0; len < fw_size; len++) {
710 *fw_size_p = fw_size - len;
712 remove_trail_spaces(buf);
717 * snd_hda_load_patch - load a "patch" firmware file and parse it
719 * @fw_size: the firmware byte size
720 * @fw_buf: the firmware data
722 int snd_hda_load_patch(struct hda_bus *bus, size_t fw_size, const void *fw_buf)
725 struct hda_codec *codec;
728 line_mode = LINE_MODE_NONE;
730 while (get_line_from_fw(buf, sizeof(buf) - 1, &fw_size, &fw_buf)) {
731 if (!*buf || *buf == '#' || *buf == '\n')
734 line_mode = parse_line_mode(buf, bus);
735 else if (patch_items[line_mode].parser &&
736 (codec || line_mode <= LINE_MODE_CODEC))
737 patch_items[line_mode].parser(buf, bus, &codec);
741 EXPORT_SYMBOL_GPL(snd_hda_load_patch);
742 #endif /* CONFIG_SND_HDA_PATCH_LOADER */
747 static struct attribute *hda_dev_attrs[] = {
748 &dev_attr_vendor_id.attr,
749 &dev_attr_subsystem_id.attr,
750 &dev_attr_revision_id.attr,
753 &dev_attr_vendor_name.attr,
754 &dev_attr_chip_name.attr,
755 &dev_attr_modelname.attr,
756 &dev_attr_init_pin_configs.attr,
757 &dev_attr_driver_pin_configs.attr,
759 &dev_attr_power_on_acct.attr,
760 &dev_attr_power_off_acct.attr,
762 #ifdef CONFIG_SND_HDA_RECONFIG
763 &dev_attr_init_verbs.attr,
764 &dev_attr_hints.attr,
765 &dev_attr_user_pin_configs.attr,
766 &dev_attr_reconfig.attr,
767 &dev_attr_clear.attr,
772 static struct attribute_group hda_dev_attr_group = {
773 .attrs = hda_dev_attrs,
776 const struct attribute_group *snd_hda_dev_attr_groups[] = {
781 void snd_hda_sysfs_init(struct hda_codec *codec)
783 mutex_init(&codec->user_mutex);
784 #ifdef CONFIG_SND_HDA_RECONFIG
785 snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
786 snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
787 snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
791 void snd_hda_sysfs_clear(struct hda_codec *codec)
793 #ifdef CONFIG_SND_HDA_RECONFIG
796 /* clear init verbs */
797 snd_array_free(&codec->init_verbs);
799 for (i = 0; i < codec->hints.used; i++) {
800 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
801 kfree(hint->key); /* we don't need to free hint->val */
803 snd_array_free(&codec->hints);
804 snd_array_free(&codec->user_pins);