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 <sound/core.h>
28 #include "hda_codec.h"
29 #include "hda_local.h"
30 #include <sound/hda_hwdep.h>
31 #include <sound/minors.h>
34 * write/read an out-of-bound verb
36 static int verb_write_ioctl(struct hda_codec *codec,
37 struct hda_verb_ioctl __user *arg)
41 if (get_user(verb, &arg->verb))
43 res = snd_hda_codec_read(codec, verb >> 24, 0,
44 (verb >> 8) & 0xffff, verb & 0xff);
45 if (put_user(res, &arg->res))
50 static int get_wcap_ioctl(struct hda_codec *codec,
51 struct hda_verb_ioctl __user *arg)
55 if (get_user(verb, &arg->verb))
57 res = get_wcaps(codec, verb >> 24);
58 if (put_user(res, &arg->res))
66 static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
67 unsigned int cmd, unsigned long arg)
69 struct hda_codec *codec = hw->private_data;
70 void __user *argp = (void __user *)arg;
73 case HDA_IOCTL_PVERSION:
74 return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
75 case HDA_IOCTL_VERB_WRITE:
76 return verb_write_ioctl(codec, argp);
77 case HDA_IOCTL_GET_WCAP:
78 return get_wcap_ioctl(codec, argp);
84 static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
85 unsigned int cmd, unsigned long arg)
87 return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
91 static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
93 #ifndef CONFIG_SND_DEBUG_VERBOSE
94 if (!capable(CAP_SYS_RAWIO))
100 static void clear_hwdep_elements(struct hda_codec *codec)
105 /* clear init verbs */
106 snd_array_free(&codec->init_verbs);
108 head = codec->hints.list;
109 for (i = 0; i < codec->hints.used; i++, head++)
111 snd_array_free(&codec->hints);
112 snd_array_free(&codec->override_pins);
115 static void hwdep_free(struct snd_hwdep *hwdep)
117 clear_hwdep_elements(hwdep->private_data);
120 int /*__devinit*/ snd_hda_create_hwdep(struct hda_codec *codec)
123 struct snd_hwdep *hwdep;
126 sprintf(hwname, "HDA Codec %d", codec->addr);
127 err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
130 codec->hwdep = hwdep;
131 sprintf(hwdep->name, "HDA Codec %d", codec->addr);
132 hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
133 hwdep->private_data = codec;
134 hwdep->private_free = hwdep_free;
135 hwdep->exclusive = 1;
137 hwdep->ops.open = hda_hwdep_open;
138 hwdep->ops.ioctl = hda_hwdep_ioctl;
140 hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
143 snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
144 snd_array_init(&codec->hints, sizeof(char *), 32);
145 snd_array_init(&codec->override_pins, sizeof(struct hda_pincfg), 16);
150 #ifdef CONFIG_SND_HDA_RECONFIG
156 static int clear_codec(struct hda_codec *codec)
158 snd_hda_codec_reset(codec);
159 clear_hwdep_elements(codec);
163 static int reconfig_codec(struct hda_codec *codec)
167 snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
168 snd_hda_codec_reset(codec);
169 err = snd_hda_codec_configure(codec);
173 err = snd_hda_codec_build_pcms(codec);
177 err = snd_hda_codec_build_controls(codec);
180 return snd_card_register(codec->bus->card);
184 * allocate a string at most len chars, and remove the trailing EOL
186 static char *kstrndup_noeol(const char *src, size_t len)
188 char *s = kstrndup(src, len, GFP_KERNEL);
198 #define CODEC_INFO_SHOW(type) \
199 static ssize_t type##_show(struct device *dev, \
200 struct device_attribute *attr, \
203 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
204 struct hda_codec *codec = hwdep->private_data; \
205 return sprintf(buf, "0x%x\n", codec->type); \
208 #define CODEC_INFO_STR_SHOW(type) \
209 static ssize_t type##_show(struct device *dev, \
210 struct device_attribute *attr, \
213 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
214 struct hda_codec *codec = hwdep->private_data; \
215 return sprintf(buf, "%s\n", \
216 codec->type ? codec->type : ""); \
219 CODEC_INFO_SHOW(vendor_id);
220 CODEC_INFO_SHOW(subsystem_id);
221 CODEC_INFO_SHOW(revision_id);
222 CODEC_INFO_SHOW(afg);
223 CODEC_INFO_SHOW(mfg);
224 CODEC_INFO_STR_SHOW(name);
225 CODEC_INFO_STR_SHOW(modelname);
227 #define CODEC_INFO_STORE(type) \
228 static ssize_t type##_store(struct device *dev, \
229 struct device_attribute *attr, \
230 const char *buf, size_t count) \
232 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
233 struct hda_codec *codec = hwdep->private_data; \
235 codec->type = simple_strtoul(buf, &after, 0); \
239 #define CODEC_INFO_STR_STORE(type) \
240 static ssize_t type##_store(struct device *dev, \
241 struct device_attribute *attr, \
242 const char *buf, size_t count) \
244 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
245 struct hda_codec *codec = hwdep->private_data; \
246 char *s = kstrndup_noeol(buf, 64); \
249 kfree(codec->type); \
254 CODEC_INFO_STORE(vendor_id);
255 CODEC_INFO_STORE(subsystem_id);
256 CODEC_INFO_STORE(revision_id);
257 CODEC_INFO_STR_STORE(name);
258 CODEC_INFO_STR_STORE(modelname);
260 #define CODEC_ACTION_STORE(type) \
261 static ssize_t type##_store(struct device *dev, \
262 struct device_attribute *attr, \
263 const char *buf, size_t count) \
265 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
266 struct hda_codec *codec = hwdep->private_data; \
269 err = type##_codec(codec); \
270 return err < 0 ? err : count; \
273 CODEC_ACTION_STORE(reconfig);
274 CODEC_ACTION_STORE(clear);
276 static ssize_t init_verbs_store(struct device *dev,
277 struct device_attribute *attr,
278 const char *buf, size_t count)
280 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
281 struct hda_codec *codec = hwdep->private_data;
283 int nid, verb, param;
285 if (sscanf(buf, "%i %i %i", &nid, &verb, ¶m) != 3)
289 v = snd_array_new(&codec->init_verbs);
298 static ssize_t hints_store(struct device *dev,
299 struct device_attribute *attr,
300 const char *buf, size_t count)
302 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
303 struct hda_codec *codec = hwdep->private_data;
307 if (!*buf || isspace(*buf) || *buf == '#' || *buf == '\n')
309 p = kstrndup_noeol(buf, 1024);
312 hint = snd_array_new(&codec->hints);
321 static ssize_t pin_configs_show(struct hda_codec *codec,
322 struct snd_array *list,
326 for (i = 0; i < list->used; i++) {
327 struct hda_pincfg *pin = snd_array_elem(list, i);
328 len += sprintf(buf + len, "0x%02x 0x%08x\n",
334 static ssize_t init_pin_configs_show(struct device *dev,
335 struct device_attribute *attr,
338 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
339 struct hda_codec *codec = hwdep->private_data;
340 return pin_configs_show(codec, &codec->init_pins, buf);
343 static ssize_t override_pin_configs_show(struct device *dev,
344 struct device_attribute *attr,
347 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
348 struct hda_codec *codec = hwdep->private_data;
349 return pin_configs_show(codec, &codec->override_pins, buf);
352 static ssize_t cur_pin_configs_show(struct device *dev,
353 struct device_attribute *attr,
356 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
357 struct hda_codec *codec = hwdep->private_data;
358 return pin_configs_show(codec, &codec->cur_pins, buf);
361 #define MAX_PIN_CONFIGS 32
363 static ssize_t override_pin_configs_store(struct device *dev,
364 struct device_attribute *attr,
365 const char *buf, size_t count)
367 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
368 struct hda_codec *codec = hwdep->private_data;
372 if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
376 err = snd_hda_add_pincfg(codec, &codec->override_pins, nid, cfg);
382 #define CODEC_ATTR_RW(type) \
383 __ATTR(type, 0644, type##_show, type##_store)
384 #define CODEC_ATTR_RO(type) \
386 #define CODEC_ATTR_WO(type) \
387 __ATTR(type, 0200, NULL, type##_store)
389 static struct device_attribute codec_attrs[] = {
390 CODEC_ATTR_RW(vendor_id),
391 CODEC_ATTR_RW(subsystem_id),
392 CODEC_ATTR_RW(revision_id),
396 CODEC_ATTR_RW(modelname),
397 CODEC_ATTR_WO(init_verbs),
398 CODEC_ATTR_WO(hints),
399 CODEC_ATTR_RO(init_pin_configs),
400 CODEC_ATTR_RW(override_pin_configs),
401 CODEC_ATTR_RO(cur_pin_configs),
402 CODEC_ATTR_WO(reconfig),
403 CODEC_ATTR_WO(clear),
407 * create sysfs files on hwdep directory
409 int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
411 struct snd_hwdep *hwdep = codec->hwdep;
414 for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
415 snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
416 hwdep->device, &codec_attrs[i]);
420 #endif /* CONFIG_SND_HDA_RECONFIG */