blob: a6d54595bb7b58f5006cbd4cdee33dc77169d8a7 [file] [log] [blame]
Takashi Iwai28073142007-07-27 18:58:06 +02001/*
2 * HWDEP Interface for HD-audio codec
3 *
4 * Copyright (c) 2007 Takashi Iwai <tiwai@suse.de>
5 *
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.
10 *
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.
15 *
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
19 */
20
Takashi Iwai28073142007-07-27 18:58:06 +020021#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/pci.h>
24#include <linux/compat.h>
25#include <linux/mutex.h>
Takashi Iwai1e1be432008-07-30 15:01:46 +020026#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080027#include <linux/string.h>
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +020028#include <linux/firmware.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040029#include <linux/export.h>
Takashi Iwai28073142007-07-27 18:58:06 +020030#include <sound/core.h>
31#include "hda_codec.h"
32#include "hda_local.h"
33#include <sound/hda_hwdep.h>
Takashi Iwaid7ffba12008-07-30 15:01:46 +020034#include <sound/minors.h>
Takashi Iwai28073142007-07-27 18:58:06 +020035
Takashi Iwai43b62712009-03-02 14:25:17 +010036/* hint string pair */
37struct hda_hint {
38 const char *key;
39 const char *val; /* contained in the same alloc as key */
40};
41
Takashi Iwai28073142007-07-27 18:58:06 +020042/*
43 * write/read an out-of-bound verb
44 */
45static int verb_write_ioctl(struct hda_codec *codec,
46 struct hda_verb_ioctl __user *arg)
47{
48 u32 verb, res;
49
50 if (get_user(verb, &arg->verb))
51 return -EFAULT;
52 res = snd_hda_codec_read(codec, verb >> 24, 0,
53 (verb >> 8) & 0xffff, verb & 0xff);
54 if (put_user(res, &arg->res))
55 return -EFAULT;
56 return 0;
57}
58
59static int get_wcap_ioctl(struct hda_codec *codec,
60 struct hda_verb_ioctl __user *arg)
61{
62 u32 verb, res;
63
64 if (get_user(verb, &arg->verb))
65 return -EFAULT;
66 res = get_wcaps(codec, verb >> 24);
67 if (put_user(res, &arg->res))
68 return -EFAULT;
69 return 0;
70}
71
72
73/*
74 */
75static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
76 unsigned int cmd, unsigned long arg)
77{
78 struct hda_codec *codec = hw->private_data;
79 void __user *argp = (void __user *)arg;
80
81 switch (cmd) {
82 case HDA_IOCTL_PVERSION:
83 return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
84 case HDA_IOCTL_VERB_WRITE:
85 return verb_write_ioctl(codec, argp);
86 case HDA_IOCTL_GET_WCAP:
87 return get_wcap_ioctl(codec, argp);
88 }
89 return -ENOIOCTLCMD;
90}
91
92#ifdef CONFIG_COMPAT
93static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
94 unsigned int cmd, unsigned long arg)
95{
Takashi Iwai312d0452007-07-31 11:08:10 +020096 return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
Takashi Iwai28073142007-07-27 18:58:06 +020097}
98#endif
99
100static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
101{
Takashi Iwai62cf8722008-05-20 12:15:15 +0200102#ifndef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwai28073142007-07-27 18:58:06 +0200103 if (!capable(CAP_SYS_RAWIO))
104 return -EACCES;
105#endif
106 return 0;
107}
108
Takashi Iwai11aeff02008-07-30 15:01:46 +0200109static void clear_hwdep_elements(struct hda_codec *codec)
110{
Takashi Iwai1e1be432008-07-30 15:01:46 +0200111 int i;
112
Takashi Iwai11aeff02008-07-30 15:01:46 +0200113 /* clear init verbs */
114 snd_array_free(&codec->init_verbs);
Takashi Iwai1e1be432008-07-30 15:01:46 +0200115 /* clear hints */
Takashi Iwai43b62712009-03-02 14:25:17 +0100116 for (i = 0; i < codec->hints.used; i++) {
117 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
118 kfree(hint->key); /* we don't need to free hint->val */
119 }
Takashi Iwai1e1be432008-07-30 15:01:46 +0200120 snd_array_free(&codec->hints);
Takashi Iwai346ff702009-02-23 09:42:57 +0100121 snd_array_free(&codec->user_pins);
Takashi Iwai11aeff02008-07-30 15:01:46 +0200122}
123
124static void hwdep_free(struct snd_hwdep *hwdep)
125{
126 clear_hwdep_elements(hwdep->private_data);
127}
128
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100129int /*__devinit*/ snd_hda_create_hwdep(struct hda_codec *codec)
Takashi Iwai28073142007-07-27 18:58:06 +0200130{
131 char hwname[16];
132 struct snd_hwdep *hwdep;
133 int err;
134
135 sprintf(hwname, "HDA Codec %d", codec->addr);
136 err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
137 if (err < 0)
138 return err;
139 codec->hwdep = hwdep;
140 sprintf(hwdep->name, "HDA Codec %d", codec->addr);
141 hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
142 hwdep->private_data = codec;
Takashi Iwai11aeff02008-07-30 15:01:46 +0200143 hwdep->private_free = hwdep_free;
Takashi Iwai28073142007-07-27 18:58:06 +0200144 hwdep->exclusive = 1;
145
146 hwdep->ops.open = hda_hwdep_open;
147 hwdep->ops.ioctl = hda_hwdep_ioctl;
148#ifdef CONFIG_COMPAT
149 hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
150#endif
151
Takashi Iwai11aeff02008-07-30 15:01:46 +0200152 snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
Takashi Iwai43b62712009-03-02 14:25:17 +0100153 snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
Takashi Iwai346ff702009-02-23 09:42:57 +0100154 snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai11aeff02008-07-30 15:01:46 +0200155
Takashi Iwai28073142007-07-27 18:58:06 +0200156 return 0;
157}
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200158
Takashi Iwaia2f63092009-11-11 09:34:25 +0100159#ifdef CONFIG_SND_HDA_POWER_SAVE
160static ssize_t power_on_acct_show(struct device *dev,
161 struct device_attribute *attr,
162 char *buf)
163{
164 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
165 struct hda_codec *codec = hwdep->private_data;
166 snd_hda_update_power_acct(codec);
167 return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_on_acct));
168}
169
170static ssize_t power_off_acct_show(struct device *dev,
171 struct device_attribute *attr,
172 char *buf)
173{
174 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
175 struct hda_codec *codec = hwdep->private_data;
176 snd_hda_update_power_acct(codec);
177 return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_off_acct));
178}
179
180static struct device_attribute power_attrs[] = {
181 __ATTR_RO(power_on_acct),
182 __ATTR_RO(power_off_acct),
183};
184
185int snd_hda_hwdep_add_power_sysfs(struct hda_codec *codec)
186{
187 struct snd_hwdep *hwdep = codec->hwdep;
188 int i;
189
190 for (i = 0; i < ARRAY_SIZE(power_attrs); i++)
191 snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
192 hwdep->device, &power_attrs[i]);
193 return 0;
194}
195#endif /* CONFIG_SND_HDA_POWER_SAVE */
196
Takashi Iwaie7ee0582008-11-21 09:26:20 +0100197#ifdef CONFIG_SND_HDA_RECONFIG
198
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200199/*
200 * sysfs interface
201 */
202
203static int clear_codec(struct hda_codec *codec)
204{
Takashi Iwaia65d6292009-02-23 16:57:04 +0100205 int err;
206
207 err = snd_hda_codec_reset(codec);
208 if (err < 0) {
209 snd_printk(KERN_ERR "The codec is being used, can't free.\n");
210 return err;
211 }
Takashi Iwai11aeff02008-07-30 15:01:46 +0200212 clear_hwdep_elements(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200213 return 0;
214}
215
216static int reconfig_codec(struct hda_codec *codec)
217{
218 int err;
219
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100220 snd_hda_power_up(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200221 snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
Takashi Iwaia65d6292009-02-23 16:57:04 +0100222 err = snd_hda_codec_reset(codec);
223 if (err < 0) {
224 snd_printk(KERN_ERR
225 "The codec is being used, can't reconfigure.\n");
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100226 goto error;
Takashi Iwaia65d6292009-02-23 16:57:04 +0100227 }
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200228 err = snd_hda_codec_configure(codec);
229 if (err < 0)
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100230 goto error;
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200231 /* rebuild PCMs */
Takashi Iwai529bd6c2008-11-27 14:17:01 +0100232 err = snd_hda_codec_build_pcms(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200233 if (err < 0)
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100234 goto error;
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200235 /* rebuild mixers */
236 err = snd_hda_codec_build_controls(codec);
237 if (err < 0)
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100238 goto error;
239 err = snd_card_register(codec->bus->card);
240 error:
241 snd_hda_power_down(codec);
242 return err;
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200243}
244
245/*
246 * allocate a string at most len chars, and remove the trailing EOL
247 */
248static char *kstrndup_noeol(const char *src, size_t len)
249{
250 char *s = kstrndup(src, len, GFP_KERNEL);
251 char *p;
252 if (!s)
253 return NULL;
254 p = strchr(s, '\n');
255 if (p)
256 *p = 0;
257 return s;
258}
259
260#define CODEC_INFO_SHOW(type) \
261static ssize_t type##_show(struct device *dev, \
262 struct device_attribute *attr, \
263 char *buf) \
264{ \
265 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
266 struct hda_codec *codec = hwdep->private_data; \
267 return sprintf(buf, "0x%x\n", codec->type); \
268}
269
270#define CODEC_INFO_STR_SHOW(type) \
271static ssize_t type##_show(struct device *dev, \
272 struct device_attribute *attr, \
273 char *buf) \
274{ \
275 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
276 struct hda_codec *codec = hwdep->private_data; \
277 return sprintf(buf, "%s\n", \
278 codec->type ? codec->type : ""); \
279}
280
281CODEC_INFO_SHOW(vendor_id);
282CODEC_INFO_SHOW(subsystem_id);
283CODEC_INFO_SHOW(revision_id);
284CODEC_INFO_SHOW(afg);
285CODEC_INFO_SHOW(mfg);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200286CODEC_INFO_STR_SHOW(vendor_name);
287CODEC_INFO_STR_SHOW(chip_name);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200288CODEC_INFO_STR_SHOW(modelname);
289
290#define CODEC_INFO_STORE(type) \
291static ssize_t type##_store(struct device *dev, \
292 struct device_attribute *attr, \
293 const char *buf, size_t count) \
294{ \
295 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
296 struct hda_codec *codec = hwdep->private_data; \
Takashi Iwai014c41f2009-12-27 13:53:24 +0100297 unsigned long val; \
298 int err = strict_strtoul(buf, 0, &val); \
299 if (err < 0) \
300 return err; \
301 codec->type = val; \
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200302 return count; \
303}
304
305#define CODEC_INFO_STR_STORE(type) \
306static ssize_t type##_store(struct device *dev, \
307 struct device_attribute *attr, \
308 const char *buf, size_t count) \
309{ \
310 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
311 struct hda_codec *codec = hwdep->private_data; \
312 char *s = kstrndup_noeol(buf, 64); \
313 if (!s) \
314 return -ENOMEM; \
315 kfree(codec->type); \
316 codec->type = s; \
317 return count; \
318}
319
320CODEC_INFO_STORE(vendor_id);
321CODEC_INFO_STORE(subsystem_id);
322CODEC_INFO_STORE(revision_id);
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200323CODEC_INFO_STR_STORE(vendor_name);
324CODEC_INFO_STR_STORE(chip_name);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200325CODEC_INFO_STR_STORE(modelname);
326
327#define CODEC_ACTION_STORE(type) \
328static ssize_t type##_store(struct device *dev, \
329 struct device_attribute *attr, \
330 const char *buf, size_t count) \
331{ \
332 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
333 struct hda_codec *codec = hwdep->private_data; \
334 int err = 0; \
335 if (*buf) \
336 err = type##_codec(codec); \
337 return err < 0 ? err : count; \
338}
339
340CODEC_ACTION_STORE(reconfig);
341CODEC_ACTION_STORE(clear);
342
Takashi Iwaiab1726f2009-03-02 17:09:25 +0100343static ssize_t init_verbs_show(struct device *dev,
344 struct device_attribute *attr,
345 char *buf)
346{
347 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
348 struct hda_codec *codec = hwdep->private_data;
349 int i, len = 0;
350 for (i = 0; i < codec->init_verbs.used; i++) {
351 struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
352 len += snprintf(buf + len, PAGE_SIZE - len,
353 "0x%02x 0x%03x 0x%04x\n",
354 v->nid, v->verb, v->param);
355 }
356 return len;
357}
358
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200359static int parse_init_verbs(struct hda_codec *codec, const char *buf)
Takashi Iwai11aeff02008-07-30 15:01:46 +0200360{
Takashi Iwai55290e12009-02-20 15:59:01 +0100361 struct hda_verb *v;
362 int nid, verb, param;
Takashi Iwai11aeff02008-07-30 15:01:46 +0200363
Takashi Iwai55290e12009-02-20 15:59:01 +0100364 if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
365 return -EINVAL;
366 if (!nid || !verb)
Takashi Iwai11aeff02008-07-30 15:01:46 +0200367 return -EINVAL;
368 v = snd_array_new(&codec->init_verbs);
369 if (!v)
370 return -ENOMEM;
Takashi Iwai55290e12009-02-20 15:59:01 +0100371 v->nid = nid;
372 v->verb = verb;
373 v->param = param;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200374 return 0;
375}
376
377static ssize_t init_verbs_store(struct device *dev,
378 struct device_attribute *attr,
379 const char *buf, size_t count)
380{
381 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
382 struct hda_codec *codec = hwdep->private_data;
383 int err = parse_init_verbs(codec, buf);
384 if (err < 0)
385 return err;
Takashi Iwai11aeff02008-07-30 15:01:46 +0200386 return count;
387}
388
Takashi Iwaiab1726f2009-03-02 17:09:25 +0100389static ssize_t hints_show(struct device *dev,
390 struct device_attribute *attr,
391 char *buf)
392{
393 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
394 struct hda_codec *codec = hwdep->private_data;
395 int i, len = 0;
396 for (i = 0; i < codec->hints.used; i++) {
397 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
398 len += snprintf(buf + len, PAGE_SIZE - len,
399 "%s = %s\n", hint->key, hint->val);
400 }
401 return len;
402}
403
Takashi Iwai43b62712009-03-02 14:25:17 +0100404static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
405{
406 int i;
407
408 for (i = 0; i < codec->hints.used; i++) {
409 struct hda_hint *hint = snd_array_elem(&codec->hints, i);
410 if (!strcmp(hint->key, key))
411 return hint;
412 }
413 return NULL;
414}
415
416static void remove_trail_spaces(char *str)
417{
418 char *p;
419 if (!*str)
420 return;
421 p = str + strlen(str) - 1;
422 for (; isspace(*p); p--) {
423 *p = 0;
424 if (p == str)
425 return;
426 }
427}
428
429#define MAX_HINTS 1024
430
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200431static int parse_hints(struct hda_codec *codec, const char *buf)
Takashi Iwai1e1be432008-07-30 15:01:46 +0200432{
Takashi Iwai43b62712009-03-02 14:25:17 +0100433 char *key, *val;
434 struct hda_hint *hint;
Takashi Iwai1e1be432008-07-30 15:01:46 +0200435
André Goddard Rosae7d28602009-12-14 18:01:06 -0800436 buf = skip_spaces(buf);
Takashi Iwai43b62712009-03-02 14:25:17 +0100437 if (!*buf || *buf == '#' || *buf == '\n')
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200438 return 0;
Takashi Iwai43b62712009-03-02 14:25:17 +0100439 if (*buf == '=')
440 return -EINVAL;
441 key = kstrndup_noeol(buf, 1024);
442 if (!key)
Takashi Iwai1e1be432008-07-30 15:01:46 +0200443 return -ENOMEM;
Takashi Iwai43b62712009-03-02 14:25:17 +0100444 /* extract key and val */
445 val = strchr(key, '=');
446 if (!val) {
447 kfree(key);
448 return -EINVAL;
449 }
450 *val++ = 0;
André Goddard Rosae7d28602009-12-14 18:01:06 -0800451 val = skip_spaces(val);
Takashi Iwai43b62712009-03-02 14:25:17 +0100452 remove_trail_spaces(key);
453 remove_trail_spaces(val);
454 hint = get_hint(codec, key);
455 if (hint) {
456 /* replace */
457 kfree(hint->key);
458 hint->key = key;
459 hint->val = val;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200460 return 0;
Takashi Iwai43b62712009-03-02 14:25:17 +0100461 }
462 /* allocate a new hint entry */
463 if (codec->hints.used >= MAX_HINTS)
464 hint = NULL;
465 else
466 hint = snd_array_new(&codec->hints);
Takashi Iwai1e1be432008-07-30 15:01:46 +0200467 if (!hint) {
Takashi Iwai43b62712009-03-02 14:25:17 +0100468 kfree(key);
Takashi Iwai1e1be432008-07-30 15:01:46 +0200469 return -ENOMEM;
470 }
Takashi Iwai43b62712009-03-02 14:25:17 +0100471 hint->key = key;
472 hint->val = val;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200473 return 0;
474}
475
476static ssize_t hints_store(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
479{
480 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
481 struct hda_codec *codec = hwdep->private_data;
482 int err = parse_hints(codec, buf);
483 if (err < 0)
484 return err;
Takashi Iwai1e1be432008-07-30 15:01:46 +0200485 return count;
486}
487
Takashi Iwai3be14142009-02-20 14:11:16 +0100488static ssize_t pin_configs_show(struct hda_codec *codec,
489 struct snd_array *list,
490 char *buf)
491{
492 int i, len = 0;
493 for (i = 0; i < list->used; i++) {
494 struct hda_pincfg *pin = snd_array_elem(list, i);
495 len += sprintf(buf + len, "0x%02x 0x%08x\n",
496 pin->nid, pin->cfg);
497 }
498 return len;
499}
500
501static ssize_t init_pin_configs_show(struct device *dev,
502 struct device_attribute *attr,
503 char *buf)
504{
505 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
506 struct hda_codec *codec = hwdep->private_data;
507 return pin_configs_show(codec, &codec->init_pins, buf);
508}
509
Takashi Iwai346ff702009-02-23 09:42:57 +0100510static ssize_t user_pin_configs_show(struct device *dev,
511 struct device_attribute *attr,
512 char *buf)
Takashi Iwai3be14142009-02-20 14:11:16 +0100513{
514 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
515 struct hda_codec *codec = hwdep->private_data;
Takashi Iwai346ff702009-02-23 09:42:57 +0100516 return pin_configs_show(codec, &codec->user_pins, buf);
Takashi Iwai3be14142009-02-20 14:11:16 +0100517}
518
Takashi Iwai346ff702009-02-23 09:42:57 +0100519static ssize_t driver_pin_configs_show(struct device *dev,
520 struct device_attribute *attr,
521 char *buf)
Takashi Iwai3be14142009-02-20 14:11:16 +0100522{
523 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
524 struct hda_codec *codec = hwdep->private_data;
Takashi Iwai346ff702009-02-23 09:42:57 +0100525 return pin_configs_show(codec, &codec->driver_pins, buf);
Takashi Iwai3be14142009-02-20 14:11:16 +0100526}
527
528#define MAX_PIN_CONFIGS 32
529
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200530static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
531{
532 int nid, cfg;
533
534 if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
535 return -EINVAL;
536 if (!nid)
537 return -EINVAL;
538 return snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
539}
540
Takashi Iwai346ff702009-02-23 09:42:57 +0100541static ssize_t user_pin_configs_store(struct device *dev,
542 struct device_attribute *attr,
543 const char *buf, size_t count)
Takashi Iwai3be14142009-02-20 14:11:16 +0100544{
545 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
546 struct hda_codec *codec = hwdep->private_data;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200547 int err = parse_user_pin_configs(codec, buf);
Takashi Iwai3be14142009-02-20 14:11:16 +0100548 if (err < 0)
549 return err;
550 return count;
551}
552
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200553#define CODEC_ATTR_RW(type) \
554 __ATTR(type, 0644, type##_show, type##_store)
555#define CODEC_ATTR_RO(type) \
556 __ATTR_RO(type)
557#define CODEC_ATTR_WO(type) \
558 __ATTR(type, 0200, NULL, type##_store)
559
560static struct device_attribute codec_attrs[] = {
561 CODEC_ATTR_RW(vendor_id),
562 CODEC_ATTR_RW(subsystem_id),
563 CODEC_ATTR_RW(revision_id),
564 CODEC_ATTR_RO(afg),
565 CODEC_ATTR_RO(mfg),
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200566 CODEC_ATTR_RW(vendor_name),
567 CODEC_ATTR_RW(chip_name),
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200568 CODEC_ATTR_RW(modelname),
Takashi Iwaiab1726f2009-03-02 17:09:25 +0100569 CODEC_ATTR_RW(init_verbs),
570 CODEC_ATTR_RW(hints),
Takashi Iwai3be14142009-02-20 14:11:16 +0100571 CODEC_ATTR_RO(init_pin_configs),
Takashi Iwai346ff702009-02-23 09:42:57 +0100572 CODEC_ATTR_RW(user_pin_configs),
573 CODEC_ATTR_RO(driver_pin_configs),
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200574 CODEC_ATTR_WO(reconfig),
575 CODEC_ATTR_WO(clear),
576};
577
578/*
579 * create sysfs files on hwdep directory
580 */
581int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
582{
583 struct snd_hwdep *hwdep = codec->hwdep;
584 int i;
585
586 for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
587 snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
588 hwdep->device, &codec_attrs[i]);
589 return 0;
590}
Takashi Iwaie7ee0582008-11-21 09:26:20 +0100591
Takashi Iwai43b62712009-03-02 14:25:17 +0100592/*
593 * Look for hint string
594 */
595const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
596{
597 struct hda_hint *hint = get_hint(codec, key);
598 return hint ? hint->val : NULL;
599}
600EXPORT_SYMBOL_HDA(snd_hda_get_hint);
601
602int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
603{
604 const char *p = snd_hda_get_hint(codec, key);
605 if (!p || !*p)
606 return -ENOENT;
607 switch (toupper(*p)) {
608 case 'T': /* true */
609 case 'Y': /* yes */
610 case '1':
611 return 1;
612 }
613 return 0;
614}
615EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
616
Takashi Iwaie7ee0582008-11-21 09:26:20 +0100617#endif /* CONFIG_SND_HDA_RECONFIG */
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200618
619#ifdef CONFIG_SND_HDA_PATCH_LOADER
620
621/* parser mode */
622enum {
623 LINE_MODE_NONE,
624 LINE_MODE_CODEC,
625 LINE_MODE_MODEL,
626 LINE_MODE_PINCFG,
627 LINE_MODE_VERB,
628 LINE_MODE_HINT,
Takashi Iwaib09f3e72010-01-28 00:01:53 +0100629 LINE_MODE_VENDOR_ID,
630 LINE_MODE_SUBSYSTEM_ID,
631 LINE_MODE_REVISION_ID,
632 LINE_MODE_CHIP_NAME,
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200633 NUM_LINE_MODES,
634};
635
636static inline int strmatch(const char *a, const char *b)
637{
638 return strnicmp(a, b, strlen(b)) == 0;
639}
640
641/* parse the contents after the line "[codec]"
642 * accept only the line with three numbers, and assign the current codec
643 */
644static void parse_codec_mode(char *buf, struct hda_bus *bus,
645 struct hda_codec **codecp)
646{
Takashi Iwaief940b02011-09-28 20:12:08 +0200647 int vendorid, subid, caddr;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200648 struct hda_codec *codec;
649
650 *codecp = NULL;
651 if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
652 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaief940b02011-09-28 20:12:08 +0200653 if ((vendorid <= 0 || codec->vendor_id == vendorid) &&
654 (subid <= 0 || codec->subsystem_id == subid) &&
David Henningsson2385b782010-06-02 16:56:41 +0200655 codec->addr == caddr) {
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200656 *codecp = codec;
657 break;
658 }
659 }
660 }
661}
662
663/* parse the contents after the other command tags, [pincfg], [verb],
Takashi Iwaib09f3e72010-01-28 00:01:53 +0100664 * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model]
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200665 * just pass to the sysfs helper (only when any codec was specified)
666 */
667static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
668 struct hda_codec **codecp)
669{
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200670 parse_user_pin_configs(*codecp, buf);
671}
672
673static void parse_verb_mode(char *buf, struct hda_bus *bus,
674 struct hda_codec **codecp)
675{
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200676 parse_init_verbs(*codecp, buf);
677}
678
679static void parse_hint_mode(char *buf, struct hda_bus *bus,
680 struct hda_codec **codecp)
681{
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200682 parse_hints(*codecp, buf);
683}
684
685static void parse_model_mode(char *buf, struct hda_bus *bus,
686 struct hda_codec **codecp)
687{
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200688 kfree((*codecp)->modelname);
689 (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
690}
691
Takashi Iwaib09f3e72010-01-28 00:01:53 +0100692static void parse_chip_name_mode(char *buf, struct hda_bus *bus,
693 struct hda_codec **codecp)
694{
695 kfree((*codecp)->chip_name);
696 (*codecp)->chip_name = kstrdup(buf, GFP_KERNEL);
697}
698
699#define DEFINE_PARSE_ID_MODE(name) \
700static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
701 struct hda_codec **codecp) \
702{ \
703 unsigned long val; \
704 if (!strict_strtoul(buf, 0, &val)) \
705 (*codecp)->name = val; \
706}
707
708DEFINE_PARSE_ID_MODE(vendor_id);
709DEFINE_PARSE_ID_MODE(subsystem_id);
710DEFINE_PARSE_ID_MODE(revision_id);
711
712
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200713struct hda_patch_item {
714 const char *tag;
715 void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
Takashi Iwaib09f3e72010-01-28 00:01:53 +0100716 int need_codec;
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200717};
718
719static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
Takashi Iwaib09f3e72010-01-28 00:01:53 +0100720 [LINE_MODE_CODEC] = { "[codec]", parse_codec_mode, 0 },
721 [LINE_MODE_MODEL] = { "[model]", parse_model_mode, 1 },
722 [LINE_MODE_VERB] = { "[verb]", parse_verb_mode, 1 },
723 [LINE_MODE_PINCFG] = { "[pincfg]", parse_pincfg_mode, 1 },
724 [LINE_MODE_HINT] = { "[hint]", parse_hint_mode, 1 },
725 [LINE_MODE_VENDOR_ID] = { "[vendor_id]", parse_vendor_id_mode, 1 },
726 [LINE_MODE_SUBSYSTEM_ID] = { "[subsystem_id]", parse_subsystem_id_mode, 1 },
727 [LINE_MODE_REVISION_ID] = { "[revision_id]", parse_revision_id_mode, 1 },
728 [LINE_MODE_CHIP_NAME] = { "[chip_name]", parse_chip_name_mode, 1 },
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200729};
730
731/* check the line starting with '[' -- change the parser mode accodingly */
732static int parse_line_mode(char *buf, struct hda_bus *bus)
733{
734 int i;
735 for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
736 if (!patch_items[i].tag)
737 continue;
738 if (strmatch(buf, patch_items[i].tag))
739 return i;
740 }
741 return LINE_MODE_NONE;
742}
743
744/* copy one line from the buffer in fw, and update the fields in fw
745 * return zero if it reaches to the end of the buffer, or non-zero
746 * if successfully copied a line
747 *
748 * the spaces at the beginning and the end of the line are stripped
749 */
750static int get_line_from_fw(char *buf, int size, struct firmware *fw)
751{
752 int len;
753 const char *p = fw->data;
754 while (isspace(*p) && fw->size) {
755 p++;
756 fw->size--;
757 }
758 if (!fw->size)
759 return 0;
760 if (size < fw->size)
761 size = fw->size;
762
763 for (len = 0; len < fw->size; len++) {
764 if (!*p)
765 break;
766 if (*p == '\n') {
767 p++;
768 len++;
769 break;
770 }
771 if (len < size)
772 *buf++ = *p++;
773 }
774 *buf = 0;
775 fw->size -= len;
776 fw->data = p;
777 remove_trail_spaces(buf);
778 return 1;
779}
780
781/*
782 * load a "patch" firmware file and parse it
783 */
784int snd_hda_load_patch(struct hda_bus *bus, const char *patch)
785{
786 int err;
787 const struct firmware *fw;
788 struct firmware tmp;
789 char buf[128];
790 struct hda_codec *codec;
791 int line_mode;
792 struct device *dev = bus->card->dev;
793
794 if (snd_BUG_ON(!dev))
795 return -ENODEV;
796 err = request_firmware(&fw, patch, dev);
797 if (err < 0) {
798 printk(KERN_ERR "hda-codec: Cannot load the patch '%s'\n",
799 patch);
800 return err;
801 }
802
803 tmp = *fw;
804 line_mode = LINE_MODE_NONE;
805 codec = NULL;
806 while (get_line_from_fw(buf, sizeof(buf) - 1, &tmp)) {
807 if (!*buf || *buf == '#' || *buf == '\n')
808 continue;
809 if (*buf == '[')
810 line_mode = parse_line_mode(buf, bus);
Takashi Iwaib09f3e72010-01-28 00:01:53 +0100811 else if (patch_items[line_mode].parser &&
812 (codec || !patch_items[line_mode].need_codec))
Takashi Iwai4ea6fbc2009-06-17 09:52:54 +0200813 patch_items[line_mode].parser(buf, bus, &codec);
814 }
815 release_firmware(fw);
816 return 0;
817}
818EXPORT_SYMBOL_HDA(snd_hda_load_patch);
819#endif /* CONFIG_SND_HDA_PATCH_LOADER */