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
);
144 err
= snd_card_register(codec
->card
);
146 snd_hda_power_down(codec
);
151 * allocate a string at most len chars, and remove the trailing EOL
153 static char *kstrndup_noeol(const char *src
, size_t len
)
155 char *s
= kstrndup(src
, len
, GFP_KERNEL
);
165 #define CODEC_INFO_STORE(type, field) \
166 static ssize_t type##_store(struct device *dev, \
167 struct device_attribute *attr, \
168 const char *buf, size_t count) \
170 struct hda_codec *codec = dev_get_drvdata(dev); \
172 int err = kstrtoul(buf, 0, &val); \
175 codec->field = val; \
179 #define CODEC_INFO_STR_STORE(type, field) \
180 static ssize_t type##_store(struct device *dev, \
181 struct device_attribute *attr, \
182 const char *buf, size_t count) \
184 struct hda_codec *codec = dev_get_drvdata(dev); \
185 char *s = kstrndup_noeol(buf, 64); \
188 kfree(codec->field); \
193 CODEC_INFO_STORE(vendor_id
, core
.vendor_id
);
194 CODEC_INFO_STORE(subsystem_id
, core
.subsystem_id
);
195 CODEC_INFO_STORE(revision_id
, core
.revision_id
);
196 CODEC_INFO_STR_STORE(vendor_name
, core
.vendor_name
);
197 CODEC_INFO_STR_STORE(chip_name
, core
.chip_name
);
198 CODEC_INFO_STR_STORE(modelname
, modelname
);
200 #define CODEC_ACTION_STORE(type) \
201 static ssize_t type##_store(struct device *dev, \
202 struct device_attribute *attr, \
203 const char *buf, size_t count) \
205 struct hda_codec *codec = dev_get_drvdata(dev); \
208 err = type##_codec(codec); \
209 return err < 0 ? err : count; \
212 CODEC_ACTION_STORE(reconfig
);
213 CODEC_ACTION_STORE(clear
);
215 static ssize_t
init_verbs_show(struct device
*dev
,
216 struct device_attribute
*attr
,
219 struct hda_codec
*codec
= dev_get_drvdata(dev
);
221 mutex_lock(&codec
->user_mutex
);
222 for (i
= 0; i
< codec
->init_verbs
.used
; i
++) {
223 struct hda_verb
*v
= snd_array_elem(&codec
->init_verbs
, i
);
224 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
,
225 "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
);
271 mutex_lock(&codec
->user_mutex
);
272 for (i
= 0; i
< codec
->hints
.used
; i
++) {
273 struct hda_hint
*hint
= snd_array_elem(&codec
->hints
, i
);
274 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
,
275 "%s = %s\n", hint
->key
, hint
->val
);
277 mutex_unlock(&codec
->user_mutex
);
281 static struct hda_hint
*get_hint(struct hda_codec
*codec
, const char *key
)
285 for (i
= 0; i
< codec
->hints
.used
; i
++) {
286 struct hda_hint
*hint
= snd_array_elem(&codec
->hints
, i
);
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 #define MAX_PIN_CONFIGS 32
380 static int parse_user_pin_configs(struct hda_codec
*codec
, const char *buf
)
384 if (sscanf(buf
, "%i %i", &nid
, &cfg
) != 2)
388 mutex_lock(&codec
->user_mutex
);
389 err
= snd_hda_add_pincfg(codec
, &codec
->user_pins
, nid
, cfg
);
390 mutex_unlock(&codec
->user_mutex
);
394 static ssize_t
user_pin_configs_store(struct device
*dev
,
395 struct device_attribute
*attr
,
396 const char *buf
, size_t count
)
398 struct hda_codec
*codec
= dev_get_drvdata(dev
);
399 int err
= parse_user_pin_configs(codec
, buf
);
405 /* sysfs attributes exposed only when CONFIG_SND_HDA_RECONFIG=y */
406 static DEVICE_ATTR_RW(init_verbs
);
407 static DEVICE_ATTR_RW(hints
);
408 static DEVICE_ATTR_RW(user_pin_configs
);
409 static DEVICE_ATTR_WO(reconfig
);
410 static DEVICE_ATTR_WO(clear
);
413 * snd_hda_get_hint - Look for hint string
414 * @codec: the HDA codec
415 * @key: the hint key string
417 * Look for a hint key/value pair matching with the given key string
418 * and returns the value string. If nothing found, returns NULL.
420 const char *snd_hda_get_hint(struct hda_codec
*codec
, const char *key
)
422 struct hda_hint
*hint
= get_hint(codec
, key
);
423 return hint
? hint
->val
: NULL
;
425 EXPORT_SYMBOL_GPL(snd_hda_get_hint
);
428 * snd_hda_get_bool_hint - Get a boolean hint value
429 * @codec: the HDA codec
430 * @key: the hint key string
432 * Look for a hint key/value pair matching with the given key string
433 * and returns a boolean value parsed from the value. If no matching
434 * key is found, return a negative value.
436 int snd_hda_get_bool_hint(struct hda_codec
*codec
, const char *key
)
441 mutex_lock(&codec
->user_mutex
);
442 p
= snd_hda_get_hint(codec
, key
);
446 switch (toupper(*p
)) {
457 mutex_unlock(&codec
->user_mutex
);
460 EXPORT_SYMBOL_GPL(snd_hda_get_bool_hint
);
463 * snd_hda_get_int_hint - Get an integer hint value
464 * @codec: the HDA codec
465 * @key: the hint key string
466 * @valp: pointer to store a value
468 * Look for a hint key/value pair matching with the given key string
469 * and stores the integer value to @valp. If no matching key is found,
470 * return a negative error code. Otherwise it returns zero.
472 int snd_hda_get_int_hint(struct hda_codec
*codec
, const char *key
, int *valp
)
478 mutex_lock(&codec
->user_mutex
);
479 p
= snd_hda_get_hint(codec
, key
);
482 else if (kstrtoul(p
, 0, &val
))
488 mutex_unlock(&codec
->user_mutex
);
491 EXPORT_SYMBOL_GPL(snd_hda_get_int_hint
);
492 #endif /* CONFIG_SND_HDA_RECONFIG */
495 * common sysfs attributes
497 #ifdef CONFIG_SND_HDA_RECONFIG
498 #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RW(name)
500 #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RO(name)
502 static RECONFIG_DEVICE_ATTR(vendor_id
);
503 static RECONFIG_DEVICE_ATTR(subsystem_id
);
504 static RECONFIG_DEVICE_ATTR(revision_id
);
505 static DEVICE_ATTR_RO(afg
);
506 static DEVICE_ATTR_RO(mfg
);
507 static RECONFIG_DEVICE_ATTR(vendor_name
);
508 static RECONFIG_DEVICE_ATTR(chip_name
);
509 static RECONFIG_DEVICE_ATTR(modelname
);
510 static DEVICE_ATTR_RO(init_pin_configs
);
511 static DEVICE_ATTR_RO(driver_pin_configs
);
514 #ifdef CONFIG_SND_HDA_PATCH_LOADER
525 LINE_MODE_SUBSYSTEM_ID
,
526 LINE_MODE_REVISION_ID
,
531 static inline int strmatch(const char *a
, const char *b
)
533 return strncasecmp(a
, b
, strlen(b
)) == 0;
536 /* parse the contents after the line "[codec]"
537 * accept only the line with three numbers, and assign the current codec
539 static void parse_codec_mode(char *buf
, struct hda_bus
*bus
,
540 struct hda_codec
**codecp
)
542 int vendorid
, subid
, caddr
;
543 struct hda_codec
*codec
;
546 if (sscanf(buf
, "%i %i %i", &vendorid
, &subid
, &caddr
) == 3) {
547 list_for_each_codec(codec
, bus
) {
548 if ((vendorid
<= 0 || codec
->core
.vendor_id
== vendorid
) &&
549 (subid
<= 0 || codec
->core
.subsystem_id
== subid
) &&
550 codec
->core
.addr
== caddr
) {
558 /* parse the contents after the other command tags, [pincfg], [verb],
559 * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model]
560 * just pass to the sysfs helper (only when any codec was specified)
562 static void parse_pincfg_mode(char *buf
, struct hda_bus
*bus
,
563 struct hda_codec
**codecp
)
565 parse_user_pin_configs(*codecp
, buf
);
568 static void parse_verb_mode(char *buf
, struct hda_bus
*bus
,
569 struct hda_codec
**codecp
)
571 parse_init_verbs(*codecp
, buf
);
574 static void parse_hint_mode(char *buf
, struct hda_bus
*bus
,
575 struct hda_codec
**codecp
)
577 parse_hints(*codecp
, buf
);
580 static void parse_model_mode(char *buf
, struct hda_bus
*bus
,
581 struct hda_codec
**codecp
)
583 kfree((*codecp
)->modelname
);
584 (*codecp
)->modelname
= kstrdup(buf
, GFP_KERNEL
);
587 static void parse_chip_name_mode(char *buf
, struct hda_bus
*bus
,
588 struct hda_codec
**codecp
)
590 snd_hda_codec_set_name(*codecp
, buf
);
593 #define DEFINE_PARSE_ID_MODE(name) \
594 static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
595 struct hda_codec **codecp) \
598 if (!kstrtoul(buf, 0, &val)) \
599 (*codecp)->core.name = val; \
602 DEFINE_PARSE_ID_MODE(vendor_id
);
603 DEFINE_PARSE_ID_MODE(subsystem_id
);
604 DEFINE_PARSE_ID_MODE(revision_id
);
607 struct hda_patch_item
{
610 void (*parser
)(char *buf
, struct hda_bus
*bus
, struct hda_codec
**retc
);
613 static struct hda_patch_item patch_items
[NUM_LINE_MODES
] = {
614 [LINE_MODE_CODEC
] = {
616 .parser
= parse_codec_mode
,
618 [LINE_MODE_MODEL
] = {
620 .parser
= parse_model_mode
,
624 .alias
= "[init_verbs]",
625 .parser
= parse_verb_mode
,
627 [LINE_MODE_PINCFG
] = {
629 .alias
= "[user_pin_configs]",
630 .parser
= parse_pincfg_mode
,
635 .parser
= parse_hint_mode
637 [LINE_MODE_VENDOR_ID
] = {
638 .tag
= "[vendor_id]",
639 .parser
= parse_vendor_id_mode
,
641 [LINE_MODE_SUBSYSTEM_ID
] = {
642 .tag
= "[subsystem_id]",
643 .parser
= parse_subsystem_id_mode
,
645 [LINE_MODE_REVISION_ID
] = {
646 .tag
= "[revision_id]",
647 .parser
= parse_revision_id_mode
,
649 [LINE_MODE_CHIP_NAME
] = {
650 .tag
= "[chip_name]",
651 .parser
= parse_chip_name_mode
,
655 /* check the line starting with '[' -- change the parser mode accodingly */
656 static int parse_line_mode(char *buf
, struct hda_bus
*bus
)
659 for (i
= 0; i
< ARRAY_SIZE(patch_items
); i
++) {
660 if (!patch_items
[i
].tag
)
662 if (strmatch(buf
, patch_items
[i
].tag
))
664 if (patch_items
[i
].alias
&& strmatch(buf
, patch_items
[i
].alias
))
667 return LINE_MODE_NONE
;
670 /* copy one line from the buffer in fw, and update the fields in fw
671 * return zero if it reaches to the end of the buffer, or non-zero
672 * if successfully copied a line
674 * the spaces at the beginning and the end of the line are stripped
676 static int get_line_from_fw(char *buf
, int size
, size_t *fw_size_p
,
677 const void **fw_data_p
)
680 size_t fw_size
= *fw_size_p
;
681 const char *p
= *fw_data_p
;
683 while (isspace(*p
) && fw_size
) {
690 for (len
= 0; len
< fw_size
; len
++) {
702 *fw_size_p
= fw_size
- len
;
704 remove_trail_spaces(buf
);
709 * snd_hda_load_patch - load a "patch" firmware file and parse it
711 * @fw_size: the firmware byte size
712 * @fw_buf: the firmware data
714 int snd_hda_load_patch(struct hda_bus
*bus
, size_t fw_size
, const void *fw_buf
)
717 struct hda_codec
*codec
;
720 line_mode
= LINE_MODE_NONE
;
722 while (get_line_from_fw(buf
, sizeof(buf
) - 1, &fw_size
, &fw_buf
)) {
723 if (!*buf
|| *buf
== '#' || *buf
== '\n')
726 line_mode
= parse_line_mode(buf
, bus
);
727 else if (patch_items
[line_mode
].parser
&&
728 (codec
|| line_mode
<= LINE_MODE_CODEC
))
729 patch_items
[line_mode
].parser(buf
, bus
, &codec
);
733 EXPORT_SYMBOL_GPL(snd_hda_load_patch
);
734 #endif /* CONFIG_SND_HDA_PATCH_LOADER */
739 static struct attribute
*hda_dev_attrs
[] = {
740 &dev_attr_vendor_id
.attr
,
741 &dev_attr_subsystem_id
.attr
,
742 &dev_attr_revision_id
.attr
,
745 &dev_attr_vendor_name
.attr
,
746 &dev_attr_chip_name
.attr
,
747 &dev_attr_modelname
.attr
,
748 &dev_attr_init_pin_configs
.attr
,
749 &dev_attr_driver_pin_configs
.attr
,
751 &dev_attr_power_on_acct
.attr
,
752 &dev_attr_power_off_acct
.attr
,
754 #ifdef CONFIG_SND_HDA_RECONFIG
755 &dev_attr_init_verbs
.attr
,
756 &dev_attr_hints
.attr
,
757 &dev_attr_user_pin_configs
.attr
,
758 &dev_attr_reconfig
.attr
,
759 &dev_attr_clear
.attr
,
764 static const struct attribute_group hda_dev_attr_group
= {
765 .attrs
= hda_dev_attrs
,
768 const struct attribute_group
*snd_hda_dev_attr_groups
[] = {
773 void snd_hda_sysfs_init(struct hda_codec
*codec
)
775 mutex_init(&codec
->user_mutex
);
776 #ifdef CONFIG_SND_HDA_RECONFIG
777 snd_array_init(&codec
->init_verbs
, sizeof(struct hda_verb
), 32);
778 snd_array_init(&codec
->hints
, sizeof(struct hda_hint
), 32);
779 snd_array_init(&codec
->user_pins
, sizeof(struct hda_pincfg
), 16);
783 void snd_hda_sysfs_clear(struct hda_codec
*codec
)
785 #ifdef CONFIG_SND_HDA_RECONFIG
788 /* clear init verbs */
789 snd_array_free(&codec
->init_verbs
);
791 for (i
= 0; i
< codec
->hints
.used
; i
++) {
792 struct hda_hint
*hint
= snd_array_elem(&codec
->hints
, i
);
793 kfree(hint
->key
); /* we don't need to free hint->val */
795 snd_array_free(&codec
->hints
);
796 snd_array_free(&codec
->user_pins
);