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_RECONFIG
163 static int clear_codec(struct hda_codec
*codec
)
167 err
= snd_hda_codec_reset(codec
);
169 snd_printk(KERN_ERR
"The codec is being used, can't free.\n");
172 clear_hwdep_elements(codec
);
176 static int reconfig_codec(struct hda_codec
*codec
)
180 snd_hda_power_up(codec
);
181 snd_printk(KERN_INFO
"hda-codec: reconfiguring\n");
182 err
= snd_hda_codec_reset(codec
);
185 "The codec is being used, can't reconfigure.\n");
188 err
= snd_hda_codec_configure(codec
);
192 err
= snd_hda_codec_build_pcms(codec
);
196 err
= snd_hda_codec_build_controls(codec
);
199 err
= snd_card_register(codec
->bus
->card
);
201 snd_hda_power_down(codec
);
206 * allocate a string at most len chars, and remove the trailing EOL
208 static char *kstrndup_noeol(const char *src
, size_t len
)
210 char *s
= kstrndup(src
, len
, GFP_KERNEL
);
220 #define CODEC_INFO_SHOW(type) \
221 static ssize_t type##_show(struct device *dev, \
222 struct device_attribute *attr, \
225 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
226 struct hda_codec *codec = hwdep->private_data; \
227 return sprintf(buf, "0x%x\n", codec->type); \
230 #define CODEC_INFO_STR_SHOW(type) \
231 static ssize_t type##_show(struct device *dev, \
232 struct device_attribute *attr, \
235 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
236 struct hda_codec *codec = hwdep->private_data; \
237 return sprintf(buf, "%s\n", \
238 codec->type ? codec->type : ""); \
241 CODEC_INFO_SHOW(vendor_id
);
242 CODEC_INFO_SHOW(subsystem_id
);
243 CODEC_INFO_SHOW(revision_id
);
244 CODEC_INFO_SHOW(afg
);
245 CODEC_INFO_SHOW(mfg
);
246 CODEC_INFO_STR_SHOW(vendor_name
);
247 CODEC_INFO_STR_SHOW(chip_name
);
248 CODEC_INFO_STR_SHOW(modelname
);
250 #define CODEC_INFO_STORE(type) \
251 static ssize_t type##_store(struct device *dev, \
252 struct device_attribute *attr, \
253 const char *buf, size_t count) \
255 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
256 struct hda_codec *codec = hwdep->private_data; \
258 codec->type = simple_strtoul(buf, &after, 0); \
262 #define CODEC_INFO_STR_STORE(type) \
263 static ssize_t type##_store(struct device *dev, \
264 struct device_attribute *attr, \
265 const char *buf, size_t count) \
267 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
268 struct hda_codec *codec = hwdep->private_data; \
269 char *s = kstrndup_noeol(buf, 64); \
272 kfree(codec->type); \
277 CODEC_INFO_STORE(vendor_id
);
278 CODEC_INFO_STORE(subsystem_id
);
279 CODEC_INFO_STORE(revision_id
);
280 CODEC_INFO_STR_STORE(vendor_name
);
281 CODEC_INFO_STR_STORE(chip_name
);
282 CODEC_INFO_STR_STORE(modelname
);
284 #define CODEC_ACTION_STORE(type) \
285 static ssize_t type##_store(struct device *dev, \
286 struct device_attribute *attr, \
287 const char *buf, size_t count) \
289 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
290 struct hda_codec *codec = hwdep->private_data; \
293 err = type##_codec(codec); \
294 return err < 0 ? err : count; \
297 CODEC_ACTION_STORE(reconfig
);
298 CODEC_ACTION_STORE(clear
);
300 static ssize_t
init_verbs_show(struct device
*dev
,
301 struct device_attribute
*attr
,
304 struct snd_hwdep
*hwdep
= dev_get_drvdata(dev
);
305 struct hda_codec
*codec
= hwdep
->private_data
;
307 for (i
= 0; i
< codec
->init_verbs
.used
; i
++) {
308 struct hda_verb
*v
= snd_array_elem(&codec
->init_verbs
, i
);
309 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
,
310 "0x%02x 0x%03x 0x%04x\n",
311 v
->nid
, v
->verb
, v
->param
);
316 static int parse_init_verbs(struct hda_codec
*codec
, const char *buf
)
319 int nid
, verb
, param
;
321 if (sscanf(buf
, "%i %i %i", &nid
, &verb
, ¶m
) != 3)
325 v
= snd_array_new(&codec
->init_verbs
);
334 static ssize_t
init_verbs_store(struct device
*dev
,
335 struct device_attribute
*attr
,
336 const char *buf
, size_t count
)
338 struct snd_hwdep
*hwdep
= dev_get_drvdata(dev
);
339 struct hda_codec
*codec
= hwdep
->private_data
;
340 int err
= parse_init_verbs(codec
, buf
);
346 static ssize_t
hints_show(struct device
*dev
,
347 struct device_attribute
*attr
,
350 struct snd_hwdep
*hwdep
= dev_get_drvdata(dev
);
351 struct hda_codec
*codec
= hwdep
->private_data
;
353 for (i
= 0; i
< codec
->hints
.used
; i
++) {
354 struct hda_hint
*hint
= snd_array_elem(&codec
->hints
, i
);
355 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
,
356 "%s = %s\n", hint
->key
, hint
->val
);
361 static struct hda_hint
*get_hint(struct hda_codec
*codec
, const char *key
)
365 for (i
= 0; i
< codec
->hints
.used
; i
++) {
366 struct hda_hint
*hint
= snd_array_elem(&codec
->hints
, i
);
367 if (!strcmp(hint
->key
, key
))
373 static void remove_trail_spaces(char *str
)
378 p
= str
+ strlen(str
) - 1;
379 for (; isspace(*p
); p
--) {
386 #define MAX_HINTS 1024
388 static int parse_hints(struct hda_codec
*codec
, const char *buf
)
391 struct hda_hint
*hint
;
393 while (isspace(*buf
))
395 if (!*buf
|| *buf
== '#' || *buf
== '\n')
399 key
= kstrndup_noeol(buf
, 1024);
402 /* extract key and val */
403 val
= strchr(key
, '=');
409 while (isspace(*val
))
411 remove_trail_spaces(key
);
412 remove_trail_spaces(val
);
413 hint
= get_hint(codec
, key
);
421 /* allocate a new hint entry */
422 if (codec
->hints
.used
>= MAX_HINTS
)
425 hint
= snd_array_new(&codec
->hints
);
435 static ssize_t
hints_store(struct device
*dev
,
436 struct device_attribute
*attr
,
437 const char *buf
, size_t count
)
439 struct snd_hwdep
*hwdep
= dev_get_drvdata(dev
);
440 struct hda_codec
*codec
= hwdep
->private_data
;
441 int err
= parse_hints(codec
, buf
);
447 static ssize_t
pin_configs_show(struct hda_codec
*codec
,
448 struct snd_array
*list
,
452 for (i
= 0; i
< list
->used
; i
++) {
453 struct hda_pincfg
*pin
= snd_array_elem(list
, i
);
454 len
+= sprintf(buf
+ len
, "0x%02x 0x%08x\n",
460 static ssize_t
init_pin_configs_show(struct device
*dev
,
461 struct device_attribute
*attr
,
464 struct snd_hwdep
*hwdep
= dev_get_drvdata(dev
);
465 struct hda_codec
*codec
= hwdep
->private_data
;
466 return pin_configs_show(codec
, &codec
->init_pins
, buf
);
469 static ssize_t
user_pin_configs_show(struct device
*dev
,
470 struct device_attribute
*attr
,
473 struct snd_hwdep
*hwdep
= dev_get_drvdata(dev
);
474 struct hda_codec
*codec
= hwdep
->private_data
;
475 return pin_configs_show(codec
, &codec
->user_pins
, buf
);
478 static ssize_t
driver_pin_configs_show(struct device
*dev
,
479 struct device_attribute
*attr
,
482 struct snd_hwdep
*hwdep
= dev_get_drvdata(dev
);
483 struct hda_codec
*codec
= hwdep
->private_data
;
484 return pin_configs_show(codec
, &codec
->driver_pins
, buf
);
487 #define MAX_PIN_CONFIGS 32
489 static int parse_user_pin_configs(struct hda_codec
*codec
, const char *buf
)
493 if (sscanf(buf
, "%i %i", &nid
, &cfg
) != 2)
497 return snd_hda_add_pincfg(codec
, &codec
->user_pins
, nid
, cfg
);
500 static ssize_t
user_pin_configs_store(struct device
*dev
,
501 struct device_attribute
*attr
,
502 const char *buf
, size_t count
)
504 struct snd_hwdep
*hwdep
= dev_get_drvdata(dev
);
505 struct hda_codec
*codec
= hwdep
->private_data
;
506 int err
= parse_user_pin_configs(codec
, buf
);
512 #define CODEC_ATTR_RW(type) \
513 __ATTR(type, 0644, type##_show, type##_store)
514 #define CODEC_ATTR_RO(type) \
516 #define CODEC_ATTR_WO(type) \
517 __ATTR(type, 0200, NULL, type##_store)
519 static struct device_attribute codec_attrs
[] = {
520 CODEC_ATTR_RW(vendor_id
),
521 CODEC_ATTR_RW(subsystem_id
),
522 CODEC_ATTR_RW(revision_id
),
525 CODEC_ATTR_RW(vendor_name
),
526 CODEC_ATTR_RW(chip_name
),
527 CODEC_ATTR_RW(modelname
),
528 CODEC_ATTR_RW(init_verbs
),
529 CODEC_ATTR_RW(hints
),
530 CODEC_ATTR_RO(init_pin_configs
),
531 CODEC_ATTR_RW(user_pin_configs
),
532 CODEC_ATTR_RO(driver_pin_configs
),
533 CODEC_ATTR_WO(reconfig
),
534 CODEC_ATTR_WO(clear
),
538 * create sysfs files on hwdep directory
540 int snd_hda_hwdep_add_sysfs(struct hda_codec
*codec
)
542 struct snd_hwdep
*hwdep
= codec
->hwdep
;
545 for (i
= 0; i
< ARRAY_SIZE(codec_attrs
); i
++)
546 snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP
, hwdep
->card
,
547 hwdep
->device
, &codec_attrs
[i
]);
552 * Look for hint string
554 const char *snd_hda_get_hint(struct hda_codec
*codec
, const char *key
)
556 struct hda_hint
*hint
= get_hint(codec
, key
);
557 return hint
? hint
->val
: NULL
;
559 EXPORT_SYMBOL_HDA(snd_hda_get_hint
);
561 int snd_hda_get_bool_hint(struct hda_codec
*codec
, const char *key
)
563 const char *p
= snd_hda_get_hint(codec
, key
);
566 switch (toupper(*p
)) {
574 EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint
);
576 #endif /* CONFIG_SND_HDA_RECONFIG */
578 #ifdef CONFIG_SND_HDA_PATCH_LOADER
591 static inline int strmatch(const char *a
, const char *b
)
593 return strnicmp(a
, b
, strlen(b
)) == 0;
596 /* parse the contents after the line "[codec]"
597 * accept only the line with three numbers, and assign the current codec
599 static void parse_codec_mode(char *buf
, struct hda_bus
*bus
,
600 struct hda_codec
**codecp
)
602 unsigned int vendorid
, subid
, caddr
;
603 struct hda_codec
*codec
;
606 if (sscanf(buf
, "%i %i %i", &vendorid
, &subid
, &caddr
) == 3) {
607 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
608 if (codec
->addr
== caddr
) {
616 /* parse the contents after the other command tags, [pincfg], [verb],
618 * just pass to the sysfs helper (only when any codec was specified)
620 static void parse_pincfg_mode(char *buf
, struct hda_bus
*bus
,
621 struct hda_codec
**codecp
)
625 parse_user_pin_configs(*codecp
, buf
);
628 static void parse_verb_mode(char *buf
, struct hda_bus
*bus
,
629 struct hda_codec
**codecp
)
633 parse_init_verbs(*codecp
, buf
);
636 static void parse_hint_mode(char *buf
, struct hda_bus
*bus
,
637 struct hda_codec
**codecp
)
641 parse_hints(*codecp
, buf
);
644 static void parse_model_mode(char *buf
, struct hda_bus
*bus
,
645 struct hda_codec
**codecp
)
649 kfree((*codecp
)->modelname
);
650 (*codecp
)->modelname
= kstrdup(buf
, GFP_KERNEL
);
653 struct hda_patch_item
{
655 void (*parser
)(char *buf
, struct hda_bus
*bus
, struct hda_codec
**retc
);
658 static struct hda_patch_item patch_items
[NUM_LINE_MODES
] = {
659 [LINE_MODE_CODEC
] = { "[codec]", parse_codec_mode
},
660 [LINE_MODE_MODEL
] = { "[model]", parse_model_mode
},
661 [LINE_MODE_VERB
] = { "[verb]", parse_verb_mode
},
662 [LINE_MODE_PINCFG
] = { "[pincfg]", parse_pincfg_mode
},
663 [LINE_MODE_HINT
] = { "[hint]", parse_hint_mode
},
666 /* check the line starting with '[' -- change the parser mode accodingly */
667 static int parse_line_mode(char *buf
, struct hda_bus
*bus
)
670 for (i
= 0; i
< ARRAY_SIZE(patch_items
); i
++) {
671 if (!patch_items
[i
].tag
)
673 if (strmatch(buf
, patch_items
[i
].tag
))
676 return LINE_MODE_NONE
;
679 /* copy one line from the buffer in fw, and update the fields in fw
680 * return zero if it reaches to the end of the buffer, or non-zero
681 * if successfully copied a line
683 * the spaces at the beginning and the end of the line are stripped
685 static int get_line_from_fw(char *buf
, int size
, struct firmware
*fw
)
688 const char *p
= fw
->data
;
689 while (isspace(*p
) && fw
->size
) {
698 for (len
= 0; len
< fw
->size
; len
++) {
712 remove_trail_spaces(buf
);
717 * load a "patch" firmware file and parse it
719 int snd_hda_load_patch(struct hda_bus
*bus
, const char *patch
)
722 const struct firmware
*fw
;
725 struct hda_codec
*codec
;
727 struct device
*dev
= bus
->card
->dev
;
729 if (snd_BUG_ON(!dev
))
731 err
= request_firmware(&fw
, patch
, dev
);
733 printk(KERN_ERR
"hda-codec: Cannot load the patch '%s'\n",
739 line_mode
= LINE_MODE_NONE
;
741 while (get_line_from_fw(buf
, sizeof(buf
) - 1, &tmp
)) {
742 if (!*buf
|| *buf
== '#' || *buf
== '\n')
745 line_mode
= parse_line_mode(buf
, bus
);
746 else if (patch_items
[line_mode
].parser
)
747 patch_items
[line_mode
].parser(buf
, bus
, &codec
);
749 release_firmware(fw
);
752 EXPORT_SYMBOL_HDA(snd_hda_load_patch
);
753 #endif /* CONFIG_SND_HDA_PATCH_LOADER */