ALSA: ctl: fix to handle several elements added by one operation for userspace element
[deliverable/linux.git] / sound / hda / hdac_sysfs.c
1 /*
2 * sysfs support for HD-audio core device
3 */
4
5 #include <linux/slab.h>
6 #include <linux/sysfs.h>
7 #include <linux/device.h>
8 #include <sound/core.h>
9 #include <sound/hdaudio.h>
10 #include "local.h"
11
12 struct hdac_widget_tree {
13 struct kobject *root;
14 struct kobject *afg;
15 struct kobject **nodes;
16 };
17
18 #define CODEC_ATTR(type) \
19 static ssize_t type##_show(struct device *dev, \
20 struct device_attribute *attr, \
21 char *buf) \
22 { \
23 struct hdac_device *codec = dev_to_hdac_dev(dev); \
24 return sprintf(buf, "0x%x\n", codec->type); \
25 } \
26 static DEVICE_ATTR_RO(type)
27
28 #define CODEC_ATTR_STR(type) \
29 static ssize_t type##_show(struct device *dev, \
30 struct device_attribute *attr, \
31 char *buf) \
32 { \
33 struct hdac_device *codec = dev_to_hdac_dev(dev); \
34 return sprintf(buf, "%s\n", \
35 codec->type ? codec->type : ""); \
36 } \
37 static DEVICE_ATTR_RO(type)
38
39 CODEC_ATTR(vendor_id);
40 CODEC_ATTR(subsystem_id);
41 CODEC_ATTR(revision_id);
42 CODEC_ATTR(afg);
43 CODEC_ATTR(mfg);
44 CODEC_ATTR_STR(vendor_name);
45 CODEC_ATTR_STR(chip_name);
46
47 static struct attribute *hdac_dev_attrs[] = {
48 &dev_attr_vendor_id.attr,
49 &dev_attr_subsystem_id.attr,
50 &dev_attr_revision_id.attr,
51 &dev_attr_afg.attr,
52 &dev_attr_mfg.attr,
53 &dev_attr_vendor_name.attr,
54 &dev_attr_chip_name.attr,
55 NULL
56 };
57
58 static struct attribute_group hdac_dev_attr_group = {
59 .attrs = hdac_dev_attrs,
60 };
61
62 const struct attribute_group *hdac_dev_attr_groups[] = {
63 &hdac_dev_attr_group,
64 NULL
65 };
66
67 /*
68 * Widget tree sysfs
69 *
70 * This is a tree showing the attributes of each widget. It appears like
71 * /sys/bus/hdaudioC0D0/widgets/04/caps
72 */
73
74 struct widget_attribute;
75
76 struct widget_attribute {
77 struct attribute attr;
78 ssize_t (*show)(struct hdac_device *codec, hda_nid_t nid,
79 struct widget_attribute *attr, char *buf);
80 ssize_t (*store)(struct hdac_device *codec, hda_nid_t nid,
81 struct widget_attribute *attr,
82 const char *buf, size_t count);
83 };
84
85 static int get_codec_nid(struct kobject *kobj, struct hdac_device **codecp)
86 {
87 struct device *dev = kobj_to_dev(kobj->parent->parent);
88 int nid;
89 ssize_t ret;
90
91 ret = kstrtoint(kobj->name, 16, &nid);
92 if (ret < 0)
93 return ret;
94 *codecp = dev_to_hdac_dev(dev);
95 return nid;
96 }
97
98 static ssize_t widget_attr_show(struct kobject *kobj, struct attribute *attr,
99 char *buf)
100 {
101 struct widget_attribute *wid_attr =
102 container_of(attr, struct widget_attribute, attr);
103 struct hdac_device *codec;
104 int nid;
105
106 if (!wid_attr->show)
107 return -EIO;
108 nid = get_codec_nid(kobj, &codec);
109 if (nid < 0)
110 return nid;
111 return wid_attr->show(codec, nid, wid_attr, buf);
112 }
113
114 static ssize_t widget_attr_store(struct kobject *kobj, struct attribute *attr,
115 const char *buf, size_t count)
116 {
117 struct widget_attribute *wid_attr =
118 container_of(attr, struct widget_attribute, attr);
119 struct hdac_device *codec;
120 int nid;
121
122 if (!wid_attr->store)
123 return -EIO;
124 nid = get_codec_nid(kobj, &codec);
125 if (nid < 0)
126 return nid;
127 return wid_attr->store(codec, nid, wid_attr, buf, count);
128 }
129
130 static const struct sysfs_ops widget_sysfs_ops = {
131 .show = widget_attr_show,
132 .store = widget_attr_store,
133 };
134
135 static void widget_release(struct kobject *kobj)
136 {
137 kfree(kobj);
138 }
139
140 static struct kobj_type widget_ktype = {
141 .release = widget_release,
142 .sysfs_ops = &widget_sysfs_ops,
143 };
144
145 #define WIDGET_ATTR_RO(_name) \
146 struct widget_attribute wid_attr_##_name = __ATTR_RO(_name)
147 #define WIDGET_ATTR_RW(_name) \
148 struct widget_attribute wid_attr_##_name = __ATTR_RW(_name)
149
150 static ssize_t caps_show(struct hdac_device *codec, hda_nid_t nid,
151 struct widget_attribute *attr, char *buf)
152 {
153 return sprintf(buf, "0x%08x\n", get_wcaps(codec, nid));
154 }
155
156 static ssize_t pin_caps_show(struct hdac_device *codec, hda_nid_t nid,
157 struct widget_attribute *attr, char *buf)
158 {
159 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
160 return 0;
161 return sprintf(buf, "0x%08x\n",
162 snd_hdac_read_parm(codec, nid, AC_PAR_PIN_CAP));
163 }
164
165 static ssize_t pin_cfg_show(struct hdac_device *codec, hda_nid_t nid,
166 struct widget_attribute *attr, char *buf)
167 {
168 unsigned int val;
169
170 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
171 return 0;
172 if (snd_hdac_read(codec, nid, AC_VERB_GET_CONFIG_DEFAULT, 0, &val))
173 return 0;
174 return sprintf(buf, "0x%08x\n", val);
175 }
176
177 static bool has_pcm_cap(struct hdac_device *codec, hda_nid_t nid)
178 {
179 if (nid == codec->afg || nid == codec->mfg)
180 return true;
181 switch (get_wcaps_type(get_wcaps(codec, nid))) {
182 case AC_WID_AUD_OUT:
183 case AC_WID_AUD_IN:
184 return true;
185 default:
186 return false;
187 }
188 }
189
190 static ssize_t pcm_caps_show(struct hdac_device *codec, hda_nid_t nid,
191 struct widget_attribute *attr, char *buf)
192 {
193 if (!has_pcm_cap(codec, nid))
194 return 0;
195 return sprintf(buf, "0x%08x\n",
196 snd_hdac_read_parm(codec, nid, AC_PAR_PCM));
197 }
198
199 static ssize_t pcm_formats_show(struct hdac_device *codec, hda_nid_t nid,
200 struct widget_attribute *attr, char *buf)
201 {
202 if (!has_pcm_cap(codec, nid))
203 return 0;
204 return sprintf(buf, "0x%08x\n",
205 snd_hdac_read_parm(codec, nid, AC_PAR_STREAM));
206 }
207
208 static ssize_t amp_in_caps_show(struct hdac_device *codec, hda_nid_t nid,
209 struct widget_attribute *attr, char *buf)
210 {
211 if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_IN_AMP))
212 return 0;
213 return sprintf(buf, "0x%08x\n",
214 snd_hdac_read_parm(codec, nid, AC_PAR_AMP_IN_CAP));
215 }
216
217 static ssize_t amp_out_caps_show(struct hdac_device *codec, hda_nid_t nid,
218 struct widget_attribute *attr, char *buf)
219 {
220 if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_OUT_AMP))
221 return 0;
222 return sprintf(buf, "0x%08x\n",
223 snd_hdac_read_parm(codec, nid, AC_PAR_AMP_OUT_CAP));
224 }
225
226 static ssize_t power_caps_show(struct hdac_device *codec, hda_nid_t nid,
227 struct widget_attribute *attr, char *buf)
228 {
229 if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_POWER))
230 return 0;
231 return sprintf(buf, "0x%08x\n",
232 snd_hdac_read_parm(codec, nid, AC_PAR_POWER_STATE));
233 }
234
235 static ssize_t gpio_caps_show(struct hdac_device *codec, hda_nid_t nid,
236 struct widget_attribute *attr, char *buf)
237 {
238 return sprintf(buf, "0x%08x\n",
239 snd_hdac_read_parm(codec, nid, AC_PAR_GPIO_CAP));
240 }
241
242 static ssize_t connections_show(struct hdac_device *codec, hda_nid_t nid,
243 struct widget_attribute *attr, char *buf)
244 {
245 hda_nid_t list[32];
246 int i, nconns;
247 ssize_t ret = 0;
248
249 nconns = snd_hdac_get_connections(codec, nid, list, ARRAY_SIZE(list));
250 if (nconns <= 0)
251 return nconns;
252 for (i = 0; i < nconns; i++)
253 ret += sprintf(buf + ret, "%s0x%02x", i ? " " : "", list[i]);
254 ret += sprintf(buf + ret, "\n");
255 return ret;
256 }
257
258 static WIDGET_ATTR_RO(caps);
259 static WIDGET_ATTR_RO(pin_caps);
260 static WIDGET_ATTR_RO(pin_cfg);
261 static WIDGET_ATTR_RO(pcm_caps);
262 static WIDGET_ATTR_RO(pcm_formats);
263 static WIDGET_ATTR_RO(amp_in_caps);
264 static WIDGET_ATTR_RO(amp_out_caps);
265 static WIDGET_ATTR_RO(power_caps);
266 static WIDGET_ATTR_RO(gpio_caps);
267 static WIDGET_ATTR_RO(connections);
268
269 static struct attribute *widget_node_attrs[] = {
270 &wid_attr_caps.attr,
271 &wid_attr_pin_caps.attr,
272 &wid_attr_pin_cfg.attr,
273 &wid_attr_pcm_caps.attr,
274 &wid_attr_pcm_formats.attr,
275 &wid_attr_amp_in_caps.attr,
276 &wid_attr_amp_out_caps.attr,
277 &wid_attr_power_caps.attr,
278 &wid_attr_connections.attr,
279 NULL,
280 };
281
282 static struct attribute *widget_afg_attrs[] = {
283 &wid_attr_pcm_caps.attr,
284 &wid_attr_pcm_formats.attr,
285 &wid_attr_amp_in_caps.attr,
286 &wid_attr_amp_out_caps.attr,
287 &wid_attr_power_caps.attr,
288 &wid_attr_gpio_caps.attr,
289 NULL,
290 };
291
292 static const struct attribute_group widget_node_group = {
293 .attrs = widget_node_attrs,
294 };
295
296 static const struct attribute_group widget_afg_group = {
297 .attrs = widget_afg_attrs,
298 };
299
300 static void free_widget_node(struct kobject *kobj,
301 const struct attribute_group *group)
302 {
303 if (kobj) {
304 sysfs_remove_group(kobj, group);
305 kobject_put(kobj);
306 }
307 }
308
309 static void widget_tree_free(struct hdac_device *codec)
310 {
311 struct hdac_widget_tree *tree = codec->widgets;
312 struct kobject **p;
313
314 if (!tree)
315 return;
316 free_widget_node(tree->afg, &widget_afg_group);
317 if (tree->nodes) {
318 for (p = tree->nodes; *p; p++)
319 free_widget_node(*p, &widget_node_group);
320 kfree(tree->nodes);
321 }
322 if (tree->root)
323 kobject_put(tree->root);
324 kfree(tree);
325 codec->widgets = NULL;
326 }
327
328 static int add_widget_node(struct kobject *parent, hda_nid_t nid,
329 const struct attribute_group *group,
330 struct kobject **res)
331 {
332 struct kobject *kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
333 int err;
334
335 if (!kobj)
336 return -ENOMEM;
337 kobject_init(kobj, &widget_ktype);
338 err = kobject_add(kobj, parent, "%02x", nid);
339 if (err < 0)
340 return err;
341 err = sysfs_create_group(kobj, group);
342 if (err < 0) {
343 kobject_put(kobj);
344 return err;
345 }
346
347 *res = kobj;
348 return 0;
349 }
350
351 static int widget_tree_create(struct hdac_device *codec)
352 {
353 struct hdac_widget_tree *tree;
354 int i, err;
355 hda_nid_t nid;
356
357 tree = codec->widgets = kzalloc(sizeof(*tree), GFP_KERNEL);
358 if (!tree)
359 return -ENOMEM;
360
361 tree->root = kobject_create_and_add("widgets", &codec->dev.kobj);
362 if (!tree->root)
363 return -ENOMEM;
364
365 tree->nodes = kcalloc(codec->num_nodes + 1, sizeof(*tree->nodes),
366 GFP_KERNEL);
367 if (!tree->nodes)
368 return -ENOMEM;
369
370 for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
371 err = add_widget_node(tree->root, nid, &widget_node_group,
372 &tree->nodes[i]);
373 if (err < 0)
374 return err;
375 }
376
377 if (codec->afg) {
378 err = add_widget_node(tree->root, codec->afg,
379 &widget_afg_group, &tree->afg);
380 if (err < 0)
381 return err;
382 }
383
384 kobject_uevent(tree->root, KOBJ_CHANGE);
385 return 0;
386 }
387
388 int hda_widget_sysfs_init(struct hdac_device *codec)
389 {
390 int err;
391
392 err = widget_tree_create(codec);
393 if (err < 0) {
394 widget_tree_free(codec);
395 return err;
396 }
397
398 return 0;
399 }
400
401 void hda_widget_sysfs_exit(struct hdac_device *codec)
402 {
403 widget_tree_free(codec);
404 }
This page took 0.045957 seconds and 5 git commands to generate.