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);
114 static void hwdep_free(struct snd_hwdep *hwdep)
116 clear_hwdep_elements(hwdep->private_data);
119 int __devinit snd_hda_create_hwdep(struct hda_codec *codec)
122 struct snd_hwdep *hwdep;
125 sprintf(hwname, "HDA Codec %d", codec->addr);
126 err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
129 codec->hwdep = hwdep;
130 sprintf(hwdep->name, "HDA Codec %d", codec->addr);
131 hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
132 hwdep->private_data = codec;
133 hwdep->private_free = hwdep_free;
134 hwdep->exclusive = 1;
136 hwdep->ops.open = hda_hwdep_open;
137 hwdep->ops.ioctl = hda_hwdep_ioctl;
139 hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
142 snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
143 snd_array_init(&codec->hints, sizeof(char *), 32);
152 static int clear_codec(struct hda_codec *codec)
154 snd_hda_codec_reset(codec);
155 clear_hwdep_elements(codec);
159 static int reconfig_codec(struct hda_codec *codec)
163 snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
164 snd_hda_codec_reset(codec);
165 err = snd_hda_codec_configure(codec);
169 err = snd_hda_build_pcms(codec->bus);
173 err = snd_hda_codec_build_controls(codec);
180 * allocate a string at most len chars, and remove the trailing EOL
182 static char *kstrndup_noeol(const char *src, size_t len)
184 char *s = kstrndup(src, len, GFP_KERNEL);
194 #define CODEC_INFO_SHOW(type) \
195 static ssize_t type##_show(struct device *dev, \
196 struct device_attribute *attr, \
199 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
200 struct hda_codec *codec = hwdep->private_data; \
201 return sprintf(buf, "0x%x\n", codec->type); \
204 #define CODEC_INFO_STR_SHOW(type) \
205 static ssize_t type##_show(struct device *dev, \
206 struct device_attribute *attr, \
209 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
210 struct hda_codec *codec = hwdep->private_data; \
211 return sprintf(buf, "%s\n", \
212 codec->type ? codec->type : ""); \
215 CODEC_INFO_SHOW(vendor_id);
216 CODEC_INFO_SHOW(subsystem_id);
217 CODEC_INFO_SHOW(revision_id);
218 CODEC_INFO_SHOW(afg);
219 CODEC_INFO_SHOW(mfg);
220 CODEC_INFO_STR_SHOW(name);
221 CODEC_INFO_STR_SHOW(modelname);
223 #define CODEC_INFO_STORE(type) \
224 static ssize_t type##_store(struct device *dev, \
225 struct device_attribute *attr, \
226 const char *buf, size_t count) \
228 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
229 struct hda_codec *codec = hwdep->private_data; \
231 codec->type = simple_strtoul(buf, &after, 0); \
235 #define CODEC_INFO_STR_STORE(type) \
236 static ssize_t type##_store(struct device *dev, \
237 struct device_attribute *attr, \
238 const char *buf, size_t count) \
240 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
241 struct hda_codec *codec = hwdep->private_data; \
242 char *s = kstrndup_noeol(buf, 64); \
245 kfree(codec->type); \
250 CODEC_INFO_STORE(vendor_id);
251 CODEC_INFO_STORE(subsystem_id);
252 CODEC_INFO_STORE(revision_id);
253 CODEC_INFO_STR_STORE(name);
254 CODEC_INFO_STR_STORE(modelname);
256 #define CODEC_ACTION_STORE(type) \
257 static ssize_t type##_store(struct device *dev, \
258 struct device_attribute *attr, \
259 const char *buf, size_t count) \
261 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
262 struct hda_codec *codec = hwdep->private_data; \
265 err = type##_codec(codec); \
266 return err < 0 ? err : count; \
269 CODEC_ACTION_STORE(reconfig);
270 CODEC_ACTION_STORE(clear);
272 static ssize_t init_verbs_store(struct device *dev,
273 struct device_attribute *attr,
274 const char *buf, size_t count)
276 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
277 struct hda_codec *codec = hwdep->private_data;
279 struct hda_verb verb, *v;
281 verb.nid = simple_strtoul(buf, &p, 0);
282 verb.verb = simple_strtoul(p, &p, 0);
283 verb.param = simple_strtoul(p, &p, 0);
284 if (!verb.nid || !verb.verb || !verb.param)
286 v = snd_array_new(&codec->init_verbs);
293 static ssize_t hints_store(struct device *dev,
294 struct device_attribute *attr,
295 const char *buf, size_t count)
297 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
298 struct hda_codec *codec = hwdep->private_data;
302 if (!*buf || isspace(*buf) || *buf == '#' || *buf == '\n')
304 p = kstrndup_noeol(buf, 1024);
307 hint = snd_array_new(&codec->hints);
316 #define CODEC_ATTR_RW(type) \
317 __ATTR(type, 0644, type##_show, type##_store)
318 #define CODEC_ATTR_RO(type) \
320 #define CODEC_ATTR_WO(type) \
321 __ATTR(type, 0200, NULL, type##_store)
323 static struct device_attribute codec_attrs[] = {
324 CODEC_ATTR_RW(vendor_id),
325 CODEC_ATTR_RW(subsystem_id),
326 CODEC_ATTR_RW(revision_id),
330 CODEC_ATTR_RW(modelname),
331 CODEC_ATTR_WO(init_verbs),
332 CODEC_ATTR_WO(hints),
333 CODEC_ATTR_WO(reconfig),
334 CODEC_ATTR_WO(clear),
338 * create sysfs files on hwdep directory
340 int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
342 struct snd_hwdep *hwdep = codec->hwdep;
345 for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
346 snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
347 hwdep->device, &codec_attrs[i]);