22 #include <linux/slab.h>
23 #include <linux/pci.h>
26 #include <linux/ctype.h>
27 #include <linux/string.h>
28 #include <linux/export.h>
52 (verb >> 8) & 0xffff, verb & 0xff);
65 res = get_wcaps(codec, verb >> 24);
75 unsigned int cmd,
unsigned long arg)
84 return verb_write_ioctl(codec, argp);
86 return get_wcap_ioctl(codec, argp);
92 static int hda_hwdep_ioctl_compat(
struct snd_hwdep *hw,
struct file *file,
93 unsigned int cmd,
unsigned long arg)
95 return hda_hwdep_ioctl(hw, file, cmd, (
unsigned long)compat_ptr(arg));
99 static int hda_hwdep_open(
struct snd_hwdep *hw,
struct file *file)
101 #ifndef CONFIG_SND_DEBUG_VERBOSE
108 static void clear_hwdep_elements(
struct hda_codec *codec)
115 for (i = 0; i < codec->hints.used; i++) {
116 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
123 static void hwdep_free(
struct snd_hwdep *hwdep)
138 codec->hwdep = hwdep;
145 hwdep->
ops.open = hda_hwdep_open;
146 hwdep->
ops.ioctl = hda_hwdep_ioctl;
148 hwdep->
ops.ioctl_compat = hda_hwdep_ioctl_compat;
151 snd_array_init(&codec->init_verbs,
sizeof(
struct hda_verb), 32);
152 snd_array_init(&codec->hints,
sizeof(
struct hda_hint), 32);
153 snd_array_init(&codec->user_pins,
sizeof(
struct hda_pincfg), 16);
165 snd_hda_update_power_acct(codec);
175 snd_hda_update_power_acct(codec);
184 int snd_hda_hwdep_add_power_sysfs(
struct hda_codec *codec)
191 hwdep->
device, &power_attrs[i]);
196 #ifdef CONFIG_SND_HDA_RECONFIG
202 static int clear_codec(
struct hda_codec *codec)
211 clear_hwdep_elements(codec);
215 static int reconfig_codec(
struct hda_codec *codec)
219 snd_hda_power_up(codec);
224 "The codec is being used, can't reconfigure.\n");
240 snd_hda_power_down(codec);
247 static char *kstrndup_noeol(
const char *
src,
size_t len)
259 #define CODEC_INFO_SHOW(type) \
260 static ssize_t type##_show(struct device *dev, \
261 struct device_attribute *attr, \
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); \
269 #define CODEC_INFO_STR_SHOW(type) \
270 static ssize_t type##_show(struct device *dev, \
271 struct device_attribute *attr, \
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 : ""); \
281 CODEC_INFO_SHOW(subsystem_id);
282 CODEC_INFO_SHOW(revision_id);
283 CODEC_INFO_SHOW(afg);
284 CODEC_INFO_SHOW(mfg);
285 CODEC_INFO_STR_SHOW(vendor_name);
286 CODEC_INFO_STR_SHOW(chip_name);
287 CODEC_INFO_STR_SHOW(modelname);
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) \
294 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
295 struct hda_codec *codec = hwdep->private_data; \
297 int err = strict_strtoul(buf, 0, &val); \
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) \
309 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
310 struct hda_codec *codec = hwdep->private_data; \
311 char *s = kstrndup_noeol(buf, 64); \
314 kfree(codec->type); \
320 CODEC_INFO_STORE(subsystem_id);
321 CODEC_INFO_STORE(revision_id);
322 CODEC_INFO_STR_STORE(vendor_name);
323 CODEC_INFO_STR_STORE(chip_name);
324 CODEC_INFO_STR_STORE(modelname);
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) \
331 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
332 struct hda_codec *codec = hwdep->private_data; \
335 err = type##_codec(codec); \
336 return err < 0 ? err : count; \
339 CODEC_ACTION_STORE(reconfig);
340 CODEC_ACTION_STORE(
clear);
349 for (i = 0; i < codec->init_verbs.used; i++) {
350 struct hda_verb *
v = snd_array_elem(&codec->init_verbs, i);
352 "0x%02x 0x%03x 0x%04x\n",
358 static int parse_init_verbs(
struct hda_codec *codec,
const char *buf)
363 if (
sscanf(buf,
"%i %i %i", &nid, &verb, ¶m) != 3)
378 const char *buf,
size_t count)
382 int err = parse_init_verbs(codec, buf);
395 for (i = 0; i < codec->hints.used; i++) {
396 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
398 "%s = %s\n", hint->
key, hint->
val);
407 for (i = 0; i < codec->hints.used; i++) {
408 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
415 static void remove_trail_spaces(
char *
str)
420 p = str +
strlen(str) - 1;
428 #define MAX_HINTS 1024
430 static int parse_hints(
struct hda_codec *codec,
const char *buf)
436 if (!*buf || *buf ==
'#' || *buf ==
'\n')
440 key = kstrndup_noeol(buf, 1024);
451 remove_trail_spaces(key);
452 remove_trail_spaces(val);
453 hint = get_hint(codec, key);
462 if (codec->hints.used >= MAX_HINTS)
477 const char *buf,
size_t count)
481 int err = parse_hints(codec, buf);
492 for (i = 0; i < list->
used; i++) {
494 len +=
sprintf(buf + len,
"0x%02x 0x%08x\n",
506 return pin_configs_show(codec, &codec->
init_pins, buf);
515 return pin_configs_show(codec, &codec->user_pins, buf);
524 return pin_configs_show(codec, &codec->
driver_pins, buf);
527 #define MAX_PIN_CONFIGS 32
529 static int parse_user_pin_configs(
struct hda_codec *codec,
const char *buf)
533 if (
sscanf(buf,
"%i %i", &nid, &cfg) != 2)
542 const char *buf,
size_t count)
546 int err = parse_user_pin_configs(codec, buf);
552 #define CODEC_ATTR_RW(type) \
553 __ATTR(type, 0644, type##_show, type##_store)
554 #define CODEC_ATTR_RO(type) \
556 #define CODEC_ATTR_WO(type) \
557 __ATTR(type, 0200, NULL, type##_store)
561 CODEC_ATTR_RW(subsystem_id),
562 CODEC_ATTR_RW(revision_id),
565 CODEC_ATTR_RW(vendor_name),
566 CODEC_ATTR_RW(chip_name),
567 CODEC_ATTR_RW(modelname),
568 CODEC_ATTR_RW(init_verbs),
569 CODEC_ATTR_RW(hints),
570 CODEC_ATTR_RO(init_pin_configs),
571 CODEC_ATTR_RW(user_pin_configs),
572 CODEC_ATTR_RO(driver_pin_configs),
573 CODEC_ATTR_WO(reconfig),
574 CODEC_ATTR_WO(
clear),
580 int snd_hda_hwdep_add_sysfs(
struct hda_codec *codec)
587 hwdep->
device, &codec_attrs[i]);
594 const char *snd_hda_get_hint(
struct hda_codec *codec,
const char *key)
596 struct hda_hint *hint = get_hint(codec, key);
597 return hint ? hint->
val :
NULL;
601 int snd_hda_get_bool_hint(
struct hda_codec *codec,
const char *key)
603 const char *p = snd_hda_get_hint(codec, key);
618 #ifdef CONFIG_SND_HDA_PATCH_LOADER
629 LINE_MODE_SUBSYSTEM_ID,
630 LINE_MODE_REVISION_ID,
635 static inline int strmatch(
const char *
a,
const char *
b)
643 static void parse_codec_mode(
char *buf,
struct hda_bus *
bus,
646 int vendorid, subid, caddr;
650 if (
sscanf(buf,
"%i %i %i", &vendorid, &subid, &caddr) == 3) {
652 if ((vendorid <= 0 || codec->
vendor_id == vendorid) &&
654 codec->
addr == caddr) {
666 static void parse_pincfg_mode(
char *buf,
struct hda_bus *bus,
669 parse_user_pin_configs(*codecp, buf);
672 static void parse_verb_mode(
char *buf,
struct hda_bus *bus,
675 parse_init_verbs(*codecp, buf);
678 static void parse_hint_mode(
char *buf,
struct hda_bus *bus,
681 parse_hints(*codecp, buf);
684 static void parse_model_mode(
char *buf,
struct hda_bus *bus,
687 kfree((*codecp)->modelname);
691 static void parse_chip_name_mode(
char *buf,
struct hda_bus *bus,
694 kfree((*codecp)->chip_name);
698 #define DEFINE_PARSE_ID_MODE(name) \
699 static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
700 struct hda_codec **codecp) \
703 if (!strict_strtoul(buf, 0, &val)) \
704 (*codecp)->name = val; \
712 struct hda_patch_item {
718 static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
719 [LINE_MODE_CODEC] = {
"[codec]", parse_codec_mode, 0 },
720 [LINE_MODE_MODEL] = {
"[model]", parse_model_mode, 1 },
721 [LINE_MODE_VERB] = {
"[verb]", parse_verb_mode, 1 },
722 [LINE_MODE_PINCFG] = {
"[pincfg]", parse_pincfg_mode, 1 },
723 [LINE_MODE_HINT] = {
"[hint]", parse_hint_mode, 1 },
724 [LINE_MODE_VENDOR_ID] = {
"[vendor_id]", parse_vendor_id_mode, 1 },
725 [LINE_MODE_SUBSYSTEM_ID] = {
"[subsystem_id]", parse_subsystem_id_mode, 1 },
726 [LINE_MODE_REVISION_ID] = {
"[revision_id]", parse_revision_id_mode, 1 },
727 [LINE_MODE_CHIP_NAME] = {
"[chip_name]", parse_chip_name_mode, 1 },
731 static int parse_line_mode(
char *buf,
struct hda_bus *bus)
734 for (i = 0; i <
ARRAY_SIZE(patch_items); i++) {
735 if (!patch_items[i].
tag)
737 if (strmatch(buf, patch_items[i].tag))
740 return LINE_MODE_NONE;
749 static int get_line_from_fw(
char *buf,
int size,
size_t *fw_size_p,
750 const void **fw_data_p)
753 size_t fw_size = *fw_size_p;
754 const char *p = *fw_data_p;
756 while (
isspace(*p) && fw_size) {
763 for (len = 0; len < fw_size; len++) {
775 *fw_size_p = fw_size - len;
777 remove_trail_spaces(buf);
784 int snd_hda_load_patch(
struct hda_bus *bus,
size_t fw_size,
const void *fw_buf)
790 line_mode = LINE_MODE_NONE;
792 while (get_line_from_fw(buf,
sizeof(buf) - 1, &fw_size, &fw_buf)) {
793 if (!*buf || *buf ==
'#' || *buf ==
'\n')
796 line_mode = parse_line_mode(buf, bus);
797 else if (patch_items[line_mode].
parser &&
798 (codec || !patch_items[line_mode].need_codec))
799 patch_items[line_mode].parser(buf, bus, &codec);