2 * HWDEP Interface for HD-audio codec
4 * Copyright (c) 2007 Takashi Iwai <tiwai@suse.de>
6 * This driver is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This driver is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/pci.h>
24 #include <linux/compat.h>
25 #include <linux/mutex.h>
26 #include <linux/ctype.h>
27 #include <linux/firmware.h>
28 #include <sound/core.h>
29 #include "hda_codec.h"
30 #include "hda_local.h"
31 #include <sound/hda_hwdep.h>
32 #include <sound/minors.h>
34 /* hint string pair */
37 const char *val; /* contained in the same alloc as key */
41 * write/read an out-of-bound verb
43 static int verb_write_ioctl(struct hda_codec *codec,
44 struct hda_verb_ioctl __user *arg)
48 if (get_user(verb, &arg->verb))
50 res = snd_hda_codec_read(codec, verb >> 24, 0,
51 (verb >> 8) & 0xffff, verb & 0xff);
52 if (put_user(res, &arg->res))
57 static int get_wcap_ioctl(struct hda_codec *codec,
58 struct hda_verb_ioctl __user *arg)
62 if (get_user(verb, &arg->verb))
64 res = get_wcaps(codec, verb >> 24);
65 if (put_user(res, &arg->res))
73 static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
74 unsigned int cmd, unsigned long arg)
76 struct hda_codec *codec = hw->private_data;
77 void __user *argp = (void __user *)arg;
80 case HDA_IOCTL_PVERSION:
81 return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
82 case HDA_IOCTL_VERB_WRITE:
83 return verb_write_ioctl(codec, argp);
84 case HDA_IOCTL_GET_WCAP:
85 return get_wcap_ioctl(codec, argp);
91 static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
92 unsigned int cmd, unsigned long arg)
94 return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
98 static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
100 #ifndef CONFIG_SND_DEBUG_VERBOSE
101 if (!capable(CAP_SYS_RAWIO))
107 static void clear_hwdep_elements(struct hda_codec *codec)
111 /* clear init verbs */
112 snd_array_free(&codec->init_verbs);
114 for (i = 0; i < codec->hints.used; i++) {
115 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
116 kfree(hint->key); /* we don't need to free hint->val */
118 snd_array_free(&codec->hints);
119 snd_array_free(&codec->user_pins);
122 static void hwdep_free(struct snd_hwdep *hwdep)
124 clear_hwdep_elements(hwdep->private_data);
127 int /*__devinit*/ snd_hda_create_hwdep(struct hda_codec *codec)
130 struct snd_hwdep *hwdep;
133 sprintf(hwname, "HDA Codec %d", codec->addr);
134 err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
137 codec->hwdep = hwdep;
138 sprintf(hwdep->name, "HDA Codec %d", codec->addr);
139 hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
140 hwdep->private_data = codec;
141 hwdep->private_free = hwdep_free;
142 hwdep->exclusive = 1;
144 hwdep->ops.open = hda_hwdep_open;
145 hwdep->ops.ioctl = hda_hwdep_ioctl;
147 hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
150 snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
151 snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
152 snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
157 #ifdef CONFIG_SND_HDA_POWER_SAVE
158 static ssize_t power_on_acct_show(struct device *dev,
159 struct device_attribute *attr,
162 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
163 struct hda_codec *codec = hwdep->private_data;
164 snd_hda_update_power_acct(codec);
165 return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_on_acct));
168 static ssize_t power_off_acct_show(struct device *dev,
169 struct device_attribute *attr,
172 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
173 struct hda_codec *codec = hwdep->private_data;
174 snd_hda_update_power_acct(codec);
175 return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_off_acct));
178 static struct device_attribute power_attrs[] = {
179 __ATTR_RO(power_on_acct),
180 __ATTR_RO(power_off_acct),
183 int snd_hda_hwdep_add_power_sysfs(struct hda_codec *codec)
185 struct snd_hwdep *hwdep = codec->hwdep;
188 for (i = 0; i < ARRAY_SIZE(power_attrs); i++)
189 snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
190 hwdep->device, &power_attrs[i]);
193 #endif /* CONFIG_SND_HDA_POWER_SAVE */
195 #ifdef CONFIG_SND_HDA_RECONFIG
201 static int clear_codec(struct hda_codec *codec)
205 err = snd_hda_codec_reset(codec);
207 snd_printk(KERN_ERR "The codec is being used, can't free.\n");
210 clear_hwdep_elements(codec);
214 static int reconfig_codec(struct hda_codec *codec)
218 snd_hda_power_up(codec);
219 snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
220 err = snd_hda_codec_reset(codec);
223 "The codec is being used, can't reconfigure.\n");
226 err = snd_hda_codec_configure(codec);
230 err = snd_hda_codec_build_pcms(codec);
234 err = snd_hda_codec_build_controls(codec);
237 err = snd_card_register(codec->bus->card);
239 snd_hda_power_down(codec);
244 * allocate a string at most len chars, and remove the trailing EOL
246 static char *kstrndup_noeol(const char *src, size_t len)
248 char *s = kstrndup(src, len, GFP_KERNEL);
258 #define CODEC_INFO_SHOW(type) \
259 static ssize_t type##_show(struct device *dev, \
260 struct device_attribute *attr, \
263 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
264 struct hda_codec *codec = hwdep->private_data; \
265 return sprintf(buf, "0x%x\n", codec->type); \
268 #define CODEC_INFO_STR_SHOW(type) \
269 static ssize_t type##_show(struct device *dev, \
270 struct device_attribute *attr, \
273 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
274 struct hda_codec *codec = hwdep->private_data; \
275 return sprintf(buf, "%s\n", \
276 codec->type ? codec->type : ""); \
279 CODEC_INFO_SHOW(vendor_id);
280 CODEC_INFO_SHOW(subsystem_id);
281 CODEC_INFO_SHOW(revision_id);
282 CODEC_INFO_SHOW(afg);
283 CODEC_INFO_SHOW(mfg);
284 CODEC_INFO_STR_SHOW(vendor_name);
285 CODEC_INFO_STR_SHOW(chip_name);
286 CODEC_INFO_STR_SHOW(modelname);
288 #define CODEC_INFO_STORE(type) \
289 static ssize_t type##_store(struct device *dev, \
290 struct device_attribute *attr, \
291 const char *buf, size_t count) \
293 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
294 struct hda_codec *codec = hwdep->private_data; \
296 codec->type = simple_strtoul(buf, &after, 0); \
300 #define CODEC_INFO_STR_STORE(type) \
301 static ssize_t type##_store(struct device *dev, \
302 struct device_attribute *attr, \
303 const char *buf, size_t count) \
305 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
306 struct hda_codec *codec = hwdep->private_data; \
307 char *s = kstrndup_noeol(buf, 64); \
310 kfree(codec->type); \
315 CODEC_INFO_STORE(vendor_id);
316 CODEC_INFO_STORE(subsystem_id);
317 CODEC_INFO_STORE(revision_id);
318 CODEC_INFO_STR_STORE(vendor_name);
319 CODEC_INFO_STR_STORE(chip_name);
320 CODEC_INFO_STR_STORE(modelname);
322 #define CODEC_ACTION_STORE(type) \
323 static ssize_t type##_store(struct device *dev, \
324 struct device_attribute *attr, \
325 const char *buf, size_t count) \
327 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
328 struct hda_codec *codec = hwdep->private_data; \
331 err = type##_codec(codec); \
332 return err < 0 ? err : count; \
335 CODEC_ACTION_STORE(reconfig);
336 CODEC_ACTION_STORE(clear);
338 static ssize_t init_verbs_show(struct device *dev,
339 struct device_attribute *attr,
342 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
343 struct hda_codec *codec = hwdep->private_data;
345 for (i = 0; i < codec->init_verbs.used; i++) {
346 struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
347 len += snprintf(buf + len, PAGE_SIZE - len,
348 "0x%02x 0x%03x 0x%04x\n",
349 v->nid, v->verb, v->param);
354 static int parse_init_verbs(struct hda_codec *codec, const char *buf)
357 int nid, verb, param;
359 if (sscanf(buf, "%i %i %i", &nid, &verb, ¶m) != 3)
363 v = snd_array_new(&codec->init_verbs);
372 static ssize_t init_verbs_store(struct device *dev,
373 struct device_attribute *attr,
374 const char *buf, size_t count)
376 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
377 struct hda_codec *codec = hwdep->private_data;
378 int err = parse_init_verbs(codec, buf);
384 static ssize_t hints_show(struct device *dev,
385 struct device_attribute *attr,
388 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
389 struct hda_codec *codec = hwdep->private_data;
391 for (i = 0; i < codec->hints.used; i++) {
392 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
393 len += snprintf(buf + len, PAGE_SIZE - len,
394 "%s = %s\n", hint->key, hint->val);
399 static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
403 for (i = 0; i < codec->hints.used; i++) {
404 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
405 if (!strcmp(hint->key, key))
411 static void remove_trail_spaces(char *str)
416 p = str + strlen(str) - 1;
417 for (; isspace(*p); p--) {
424 #define MAX_HINTS 1024
426 static int parse_hints(struct hda_codec *codec, const char *buf)
429 struct hda_hint *hint;
431 while (isspace(*buf))
433 if (!*buf || *buf == '#' || *buf == '\n')
437 key = kstrndup_noeol(buf, 1024);
440 /* extract key and val */
441 val = strchr(key, '=');
447 while (isspace(*val))
449 remove_trail_spaces(key);
450 remove_trail_spaces(val);
451 hint = get_hint(codec, key);
459 /* allocate a new hint entry */
460 if (codec->hints.used >= MAX_HINTS)
463 hint = snd_array_new(&codec->hints);
473 static ssize_t hints_store(struct device *dev,
474 struct device_attribute *attr,
475 const char *buf, size_t count)
477 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
478 struct hda_codec *codec = hwdep->private_data;
479 int err = parse_hints(codec, buf);
485 static ssize_t pin_configs_show(struct hda_codec *codec,
486 struct snd_array *list,
490 for (i = 0; i < list->used; i++) {
491 struct hda_pincfg *pin = snd_array_elem(list, i);
492 len += sprintf(buf + len, "0x%02x 0x%08x\n",
498 static ssize_t init_pin_configs_show(struct device *dev,
499 struct device_attribute *attr,
502 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
503 struct hda_codec *codec = hwdep->private_data;
504 return pin_configs_show(codec, &codec->init_pins, buf);
507 static ssize_t user_pin_configs_show(struct device *dev,
508 struct device_attribute *attr,
511 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
512 struct hda_codec *codec = hwdep->private_data;
513 return pin_configs_show(codec, &codec->user_pins, buf);
516 static ssize_t driver_pin_configs_show(struct device *dev,
517 struct device_attribute *attr,
520 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
521 struct hda_codec *codec = hwdep->private_data;
522 return pin_configs_show(codec, &codec->driver_pins, buf);
525 #define MAX_PIN_CONFIGS 32
527 static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
531 if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
535 return snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
538 static ssize_t user_pin_configs_store(struct device *dev,
539 struct device_attribute *attr,
540 const char *buf, size_t count)
542 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
543 struct hda_codec *codec = hwdep->private_data;
544 int err = parse_user_pin_configs(codec, buf);
550 #define CODEC_ATTR_RW(type) \
551 __ATTR(type, 0644, type##_show, type##_store)
552 #define CODEC_ATTR_RO(type) \
554 #define CODEC_ATTR_WO(type) \
555 __ATTR(type, 0200, NULL, type##_store)
557 static struct device_attribute codec_attrs[] = {
558 CODEC_ATTR_RW(vendor_id),
559 CODEC_ATTR_RW(subsystem_id),
560 CODEC_ATTR_RW(revision_id),
563 CODEC_ATTR_RW(vendor_name),
564 CODEC_ATTR_RW(chip_name),
565 CODEC_ATTR_RW(modelname),
566 CODEC_ATTR_RW(init_verbs),
567 CODEC_ATTR_RW(hints),
568 CODEC_ATTR_RO(init_pin_configs),
569 CODEC_ATTR_RW(user_pin_configs),
570 CODEC_ATTR_RO(driver_pin_configs),
571 CODEC_ATTR_WO(reconfig),
572 CODEC_ATTR_WO(clear),
576 * create sysfs files on hwdep directory
578 int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
580 struct snd_hwdep *hwdep = codec->hwdep;
583 for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
584 snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
585 hwdep->device, &codec_attrs[i]);
590 * Look for hint string
592 const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
594 struct hda_hint *hint = get_hint(codec, key);
595 return hint ? hint->val : NULL;
597 EXPORT_SYMBOL_HDA(snd_hda_get_hint);
599 int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
601 const char *p = snd_hda_get_hint(codec, key);
604 switch (toupper(*p)) {
612 EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
614 #endif /* CONFIG_SND_HDA_RECONFIG */
616 #ifdef CONFIG_SND_HDA_PATCH_LOADER
629 static inline int strmatch(const char *a, const char *b)
631 return strnicmp(a, b, strlen(b)) == 0;
634 /* parse the contents after the line "[codec]"
635 * accept only the line with three numbers, and assign the current codec
637 static void parse_codec_mode(char *buf, struct hda_bus *bus,
638 struct hda_codec **codecp)
640 unsigned int vendorid, subid, caddr;
641 struct hda_codec *codec;
644 if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
645 list_for_each_entry(codec, &bus->codec_list, list) {
646 if (codec->addr == caddr) {
654 /* parse the contents after the other command tags, [pincfg], [verb],
656 * just pass to the sysfs helper (only when any codec was specified)
658 static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
659 struct hda_codec **codecp)
663 parse_user_pin_configs(*codecp, buf);
666 static void parse_verb_mode(char *buf, struct hda_bus *bus,
667 struct hda_codec **codecp)
671 parse_init_verbs(*codecp, buf);
674 static void parse_hint_mode(char *buf, struct hda_bus *bus,
675 struct hda_codec **codecp)
679 parse_hints(*codecp, buf);
682 static void parse_model_mode(char *buf, struct hda_bus *bus,
683 struct hda_codec **codecp)
687 kfree((*codecp)->modelname);
688 (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
691 struct hda_patch_item {
693 void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
696 static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
697 [LINE_MODE_CODEC] = { "[codec]", parse_codec_mode },
698 [LINE_MODE_MODEL] = { "[model]", parse_model_mode },
699 [LINE_MODE_VERB] = { "[verb]", parse_verb_mode },
700 [LINE_MODE_PINCFG] = { "[pincfg]", parse_pincfg_mode },
701 [LINE_MODE_HINT] = { "[hint]", parse_hint_mode },
704 /* check the line starting with '[' -- change the parser mode accodingly */
705 static int parse_line_mode(char *buf, struct hda_bus *bus)
708 for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
709 if (!patch_items[i].tag)
711 if (strmatch(buf, patch_items[i].tag))
714 return LINE_MODE_NONE;
717 /* copy one line from the buffer in fw, and update the fields in fw
718 * return zero if it reaches to the end of the buffer, or non-zero
719 * if successfully copied a line
721 * the spaces at the beginning and the end of the line are stripped
723 static int get_line_from_fw(char *buf, int size, struct firmware *fw)
726 const char *p = fw->data;
727 while (isspace(*p) && fw->size) {
736 for (len = 0; len < fw->size; len++) {
750 remove_trail_spaces(buf);
755 * load a "patch" firmware file and parse it
757 int snd_hda_load_patch(struct hda_bus *bus, const char *patch)
760 const struct firmware *fw;
763 struct hda_codec *codec;
765 struct device *dev = bus->card->dev;
767 if (snd_BUG_ON(!dev))
769 err = request_firmware(&fw, patch, dev);
771 printk(KERN_ERR "hda-codec: Cannot load the patch '%s'\n",
777 line_mode = LINE_MODE_NONE;
779 while (get_line_from_fw(buf, sizeof(buf) - 1, &tmp)) {
780 if (!*buf || *buf == '#' || *buf == '\n')
783 line_mode = parse_line_mode(buf, bus);
784 else if (patch_items[line_mode].parser)
785 patch_items[line_mode].parser(buf, bus, &codec);
787 release_firmware(fw);
790 EXPORT_SYMBOL_HDA(snd_hda_load_patch);
791 #endif /* CONFIG_SND_HDA_PATCH_LOADER */