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 <sound/core.h>
27 #include "hda_codec.h"
28 #include "hda_local.h"
29 #include <sound/hda_hwdep.h>
30 #include <sound/minors.h>
33 * write/read an out-of-bound verb
35 static int verb_write_ioctl(struct hda_codec *codec,
36 struct hda_verb_ioctl __user *arg)
40 if (get_user(verb, &arg->verb))
42 res = snd_hda_codec_read(codec, verb >> 24, 0,
43 (verb >> 8) & 0xffff, verb & 0xff);
44 if (put_user(res, &arg->res))
49 static int get_wcap_ioctl(struct hda_codec *codec,
50 struct hda_verb_ioctl __user *arg)
54 if (get_user(verb, &arg->verb))
56 res = get_wcaps(codec, verb >> 24);
57 if (put_user(res, &arg->res))
65 static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
66 unsigned int cmd, unsigned long arg)
68 struct hda_codec *codec = hw->private_data;
69 void __user *argp = (void __user *)arg;
72 case HDA_IOCTL_PVERSION:
73 return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
74 case HDA_IOCTL_VERB_WRITE:
75 return verb_write_ioctl(codec, argp);
76 case HDA_IOCTL_GET_WCAP:
77 return get_wcap_ioctl(codec, argp);
83 static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
84 unsigned int cmd, unsigned long arg)
86 return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
90 static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
92 #ifndef CONFIG_SND_DEBUG_VERBOSE
93 if (!capable(CAP_SYS_RAWIO))
99 static void clear_hwdep_elements(struct hda_codec *codec)
101 /* clear init verbs */
102 snd_array_free(&codec->init_verbs);
105 static void hwdep_free(struct snd_hwdep *hwdep)
107 clear_hwdep_elements(hwdep->private_data);
110 int __devinit snd_hda_create_hwdep(struct hda_codec *codec)
113 struct snd_hwdep *hwdep;
116 sprintf(hwname, "HDA Codec %d", codec->addr);
117 err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
120 codec->hwdep = hwdep;
121 sprintf(hwdep->name, "HDA Codec %d", codec->addr);
122 hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
123 hwdep->private_data = codec;
124 hwdep->private_free = hwdep_free;
125 hwdep->exclusive = 1;
127 hwdep->ops.open = hda_hwdep_open;
128 hwdep->ops.ioctl = hda_hwdep_ioctl;
130 hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
133 snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
142 static int clear_codec(struct hda_codec *codec)
144 snd_hda_codec_reset(codec);
145 clear_hwdep_elements(codec);
149 static int reconfig_codec(struct hda_codec *codec)
153 snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
154 snd_hda_codec_reset(codec);
155 err = snd_hda_codec_configure(codec);
159 err = snd_hda_build_pcms(codec->bus);
163 err = snd_hda_codec_build_controls(codec);
170 * allocate a string at most len chars, and remove the trailing EOL
172 static char *kstrndup_noeol(const char *src, size_t len)
174 char *s = kstrndup(src, len, GFP_KERNEL);
184 #define CODEC_INFO_SHOW(type) \
185 static ssize_t type##_show(struct device *dev, \
186 struct device_attribute *attr, \
189 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
190 struct hda_codec *codec = hwdep->private_data; \
191 return sprintf(buf, "0x%x\n", codec->type); \
194 #define CODEC_INFO_STR_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, "%s\n", \
202 codec->type ? codec->type : ""); \
205 CODEC_INFO_SHOW(vendor_id);
206 CODEC_INFO_SHOW(subsystem_id);
207 CODEC_INFO_SHOW(revision_id);
208 CODEC_INFO_SHOW(afg);
209 CODEC_INFO_SHOW(mfg);
210 CODEC_INFO_STR_SHOW(name);
211 CODEC_INFO_STR_SHOW(modelname);
213 #define CODEC_INFO_STORE(type) \
214 static ssize_t type##_store(struct device *dev, \
215 struct device_attribute *attr, \
216 const char *buf, size_t count) \
218 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
219 struct hda_codec *codec = hwdep->private_data; \
221 codec->type = simple_strtoul(buf, &after, 0); \
225 #define CODEC_INFO_STR_STORE(type) \
226 static ssize_t type##_store(struct device *dev, \
227 struct device_attribute *attr, \
228 const char *buf, size_t count) \
230 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
231 struct hda_codec *codec = hwdep->private_data; \
232 char *s = kstrndup_noeol(buf, 64); \
235 kfree(codec->type); \
240 CODEC_INFO_STORE(vendor_id);
241 CODEC_INFO_STORE(subsystem_id);
242 CODEC_INFO_STORE(revision_id);
243 CODEC_INFO_STR_STORE(name);
244 CODEC_INFO_STR_STORE(modelname);
246 #define CODEC_ACTION_STORE(type) \
247 static ssize_t type##_store(struct device *dev, \
248 struct device_attribute *attr, \
249 const char *buf, size_t count) \
251 struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
252 struct hda_codec *codec = hwdep->private_data; \
255 err = type##_codec(codec); \
256 return err < 0 ? err : count; \
259 CODEC_ACTION_STORE(reconfig);
260 CODEC_ACTION_STORE(clear);
262 static ssize_t init_verbs_store(struct device *dev,
263 struct device_attribute *attr,
264 const char *buf, size_t count)
266 struct snd_hwdep *hwdep = dev_get_drvdata(dev);
267 struct hda_codec *codec = hwdep->private_data;
269 struct hda_verb verb, *v;
271 verb.nid = simple_strtoul(buf, &p, 0);
272 verb.verb = simple_strtoul(p, &p, 0);
273 verb.param = simple_strtoul(p, &p, 0);
274 if (!verb.nid || !verb.verb || !verb.param)
276 v = snd_array_new(&codec->init_verbs);
283 #define CODEC_ATTR_RW(type) \
284 __ATTR(type, 0644, type##_show, type##_store)
285 #define CODEC_ATTR_RO(type) \
287 #define CODEC_ATTR_WO(type) \
288 __ATTR(type, 0200, NULL, type##_store)
290 static struct device_attribute codec_attrs[] = {
291 CODEC_ATTR_RW(vendor_id),
292 CODEC_ATTR_RW(subsystem_id),
293 CODEC_ATTR_RW(revision_id),
297 CODEC_ATTR_RW(modelname),
298 CODEC_ATTR_WO(init_verbs),
299 CODEC_ATTR_WO(reconfig),
300 CODEC_ATTR_WO(clear),
304 * create sysfs files on hwdep directory
306 int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
308 struct snd_hwdep *hwdep = codec->hwdep;
311 for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
312 snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
313 hwdep->device, &codec_attrs[i]);