ALSA: hda - Add init_verbs entries
[linux-2.6] / sound / pci / hda / hda_hwdep.c
1 /*
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
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>
31
32 /*
33  * write/read an out-of-bound verb
34  */
35 static int verb_write_ioctl(struct hda_codec *codec,
36                             struct hda_verb_ioctl __user *arg)
37 {
38         u32 verb, res;
39
40         if (get_user(verb, &arg->verb))
41                 return -EFAULT;
42         res = snd_hda_codec_read(codec, verb >> 24, 0,
43                                  (verb >> 8) & 0xffff, verb & 0xff);
44         if (put_user(res, &arg->res))
45                 return -EFAULT;
46         return 0;
47 }
48
49 static int get_wcap_ioctl(struct hda_codec *codec,
50                           struct hda_verb_ioctl __user *arg)
51 {
52         u32 verb, res;
53         
54         if (get_user(verb, &arg->verb))
55                 return -EFAULT;
56         res = get_wcaps(codec, verb >> 24);
57         if (put_user(res, &arg->res))
58                 return -EFAULT;
59         return 0;
60 }
61
62
63 /*
64  */
65 static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
66                            unsigned int cmd, unsigned long arg)
67 {
68         struct hda_codec *codec = hw->private_data;
69         void __user *argp = (void __user *)arg;
70
71         switch (cmd) {
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);
78         }
79         return -ENOIOCTLCMD;
80 }
81
82 #ifdef CONFIG_COMPAT
83 static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
84                                   unsigned int cmd, unsigned long arg)
85 {
86         return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
87 }
88 #endif
89
90 static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
91 {
92 #ifndef CONFIG_SND_DEBUG_VERBOSE
93         if (!capable(CAP_SYS_RAWIO))
94                 return -EACCES;
95 #endif
96         return 0;
97 }
98
99 static void clear_hwdep_elements(struct hda_codec *codec)
100 {
101         /* clear init verbs */
102         snd_array_free(&codec->init_verbs);
103 }
104
105 static void hwdep_free(struct snd_hwdep *hwdep)
106 {
107         clear_hwdep_elements(hwdep->private_data);
108 }
109
110 int __devinit snd_hda_create_hwdep(struct hda_codec *codec)
111 {
112         char hwname[16];
113         struct snd_hwdep *hwdep;
114         int err;
115
116         sprintf(hwname, "HDA Codec %d", codec->addr);
117         err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
118         if (err < 0)
119                 return err;
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;
126
127         hwdep->ops.open = hda_hwdep_open;
128         hwdep->ops.ioctl = hda_hwdep_ioctl;
129 #ifdef CONFIG_COMPAT
130         hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
131 #endif
132
133         snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
134
135         return 0;
136 }
137
138 /*
139  * sysfs interface
140  */
141
142 static int clear_codec(struct hda_codec *codec)
143 {
144         snd_hda_codec_reset(codec);
145         clear_hwdep_elements(codec);
146         return 0;
147 }
148
149 static int reconfig_codec(struct hda_codec *codec)
150 {
151         int err;
152
153         snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
154         snd_hda_codec_reset(codec);
155         err = snd_hda_codec_configure(codec);
156         if (err < 0)
157                 return err;
158         /* rebuild PCMs */
159         err = snd_hda_build_pcms(codec->bus);
160         if (err < 0)
161                 return err;
162         /* rebuild mixers */
163         err = snd_hda_codec_build_controls(codec);
164         if (err < 0)
165                 return err;
166         return 0;
167 }
168
169 /*
170  * allocate a string at most len chars, and remove the trailing EOL
171  */
172 static char *kstrndup_noeol(const char *src, size_t len)
173 {
174         char *s = kstrndup(src, len, GFP_KERNEL);
175         char *p;
176         if (!s)
177                 return NULL;
178         p = strchr(s, '\n');
179         if (p)
180                 *p = 0;
181         return s;
182 }
183
184 #define CODEC_INFO_SHOW(type)                                   \
185 static ssize_t type##_show(struct device *dev,                  \
186                            struct device_attribute *attr,       \
187                            char *buf)                           \
188 {                                                               \
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);             \
192 }
193
194 #define CODEC_INFO_STR_SHOW(type)                               \
195 static ssize_t type##_show(struct device *dev,                  \
196                              struct device_attribute *attr,     \
197                                         char *buf)              \
198 {                                                               \
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 : "");         \
203 }
204
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);
212
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)      \
217 {                                                               \
218         struct snd_hwdep *hwdep = dev_get_drvdata(dev);         \
219         struct hda_codec *codec = hwdep->private_data;          \
220         char *after;                                            \
221         codec->type = simple_strtoul(buf, &after, 0);           \
222         return count;                                           \
223 }
224
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)      \
229 {                                                               \
230         struct snd_hwdep *hwdep = dev_get_drvdata(dev);         \
231         struct hda_codec *codec = hwdep->private_data;          \
232         char *s = kstrndup_noeol(buf, 64);                      \
233         if (!s)                                                 \
234                 return -ENOMEM;                                 \
235         kfree(codec->type);                                     \
236         codec->type = s;                                        \
237         return count;                                           \
238 }
239
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);
245
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)      \
250 {                                                               \
251         struct snd_hwdep *hwdep = dev_get_drvdata(dev);         \
252         struct hda_codec *codec = hwdep->private_data;          \
253         int err = 0;                                            \
254         if (*buf)                                               \
255                 err = type##_codec(codec);                      \
256         return err < 0 ? err : count;                           \
257 }
258
259 CODEC_ACTION_STORE(reconfig);
260 CODEC_ACTION_STORE(clear);
261
262 static ssize_t init_verbs_store(struct device *dev,
263                                 struct device_attribute *attr,
264                                 const char *buf, size_t count)
265 {
266         struct snd_hwdep *hwdep = dev_get_drvdata(dev);
267         struct hda_codec *codec = hwdep->private_data;
268         char *p;
269         struct hda_verb verb, *v;
270
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)
275                 return -EINVAL;
276         v = snd_array_new(&codec->init_verbs);
277         if (!v)
278                 return -ENOMEM;
279         *v = verb;
280         return count;
281 }
282
283 #define CODEC_ATTR_RW(type) \
284         __ATTR(type, 0644, type##_show, type##_store)
285 #define CODEC_ATTR_RO(type) \
286         __ATTR_RO(type)
287 #define CODEC_ATTR_WO(type) \
288         __ATTR(type, 0200, NULL, type##_store)
289
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),
294         CODEC_ATTR_RO(afg),
295         CODEC_ATTR_RO(mfg),
296         CODEC_ATTR_RW(name),
297         CODEC_ATTR_RW(modelname),
298         CODEC_ATTR_WO(init_verbs),
299         CODEC_ATTR_WO(reconfig),
300         CODEC_ATTR_WO(clear),
301 };
302
303 /*
304  * create sysfs files on hwdep directory
305  */
306 int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
307 {
308         struct snd_hwdep *hwdep = codec->hwdep;
309         int i;
310
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]);
314         return 0;
315 }