Merge commit 'origin/master-tx'
[platform/upstream/pulseaudio.git] / src / modules / udev-util.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2009 Lennart Poettering
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <libudev.h>
27
28 #include <pulse/xmalloc.h>
29 #include <pulse/proplist.h>
30
31 #include <pulsecore/log.h>
32 #include <pulsecore/core-util.h>
33
34 #include "udev-util.h"
35
36 static int read_id(struct udev_device *d, const char *n) {
37     const char *v;
38     unsigned u;
39
40     pa_assert(d);
41     pa_assert(n);
42
43     if (!(v = udev_device_get_property_value(d, n)))
44         return -1;
45
46     if (pa_startswith(v, "0x"))
47         v += 2;
48
49     if (!*v)
50         return -1;
51
52     if (sscanf(v, "%04x", &u) != 1)
53         return -1;
54
55     if (u > 0xFFFFU)
56         return -1;
57
58     return u;
59 }
60
61 int pa_udev_get_info(int card_idx, pa_proplist *p) {
62     int r = -1;
63     struct udev *udev;
64     struct udev_device *card = NULL;
65     char *t;
66     const char *v;
67     int id;
68
69     pa_assert(p);
70     pa_assert(card_idx >= 0);
71
72     if (!(udev = udev_new())) {
73         pa_log_error("Failed to allocate udev context.");
74         goto finish;
75     }
76
77     t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx);
78     card = udev_device_new_from_syspath(udev, t);
79     pa_xfree(t);
80
81     if (!card) {
82         pa_log_error("Failed to get card object.");
83         goto finish;
84     }
85
86     if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS_PATH))
87         if (((v = udev_device_get_property_value(card, "ID_PATH")) && *v) ||
88             (v = udev_device_get_devpath(card)))
89             pa_proplist_sets(p, PA_PROP_DEVICE_BUS_PATH, v);
90
91     if (!pa_proplist_contains(p, "sysfs.path"))
92         if ((v = udev_device_get_devpath(card)))
93             pa_proplist_sets(p, "sysfs.path", v);
94
95     if (!pa_proplist_contains(p, "udev.id"))
96         if ((v = udev_device_get_property_value(card, "ID_ID")) && *v)
97             pa_proplist_sets(p, "udev.id", v);
98
99     if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS))
100         if ((v = udev_device_get_property_value(card, "ID_BUS")) && *v)
101             pa_proplist_sets(p, PA_PROP_DEVICE_BUS, v);
102
103     if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_ID))
104         if ((id = read_id(card, "ID_VENDOR_ID")) > 0)
105             pa_proplist_setf(p, PA_PROP_DEVICE_VENDOR_ID, "%04x", id);
106
107     if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_NAME)) {
108         if ((v = udev_device_get_property_value(card, "ID_VENDOR_FROM_DATABASE")) && *v)
109             pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
110         else if ((v = udev_device_get_property_value(card, "ID_VENDOR")) && *v)
111             pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
112     }
113
114     if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_ID))
115         if ((id = read_id(card, "ID_MODEL_ID")) >= 0)
116             pa_proplist_setf(p, PA_PROP_DEVICE_PRODUCT_ID, "%04x", id);
117
118     if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_NAME)) {
119         if ((v = udev_device_get_property_value(card, "ID_MODEL_FROM_DATABASE")) && *v)
120             pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
121         else if ((v = udev_device_get_property_value(card, "ID_MODEL")) && *v)
122             pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
123     }
124
125     if (!pa_proplist_contains(p, PA_PROP_DEVICE_SERIAL))
126         if ((v = udev_device_get_property_value(card, "ID_SERIAL")) && *v)
127             pa_proplist_sets(p, PA_PROP_DEVICE_SERIAL, v);
128
129     if (!pa_proplist_contains(p, PA_PROP_DEVICE_CLASS))
130         if ((v = udev_device_get_property_value(card, "SOUND_CLASS")) && *v)
131             pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, v);
132
133     if (!pa_proplist_contains(p, PA_PROP_DEVICE_FORM_FACTOR))
134         if ((v = udev_device_get_property_value(card, "SOUND_FORM_FACTOR")) && *v)
135             pa_proplist_sets(p, PA_PROP_DEVICE_FORM_FACTOR, v);
136
137     /* This is normaly not set by the udev rules but may be useful to
138      * allow administrators to overwrite the device description.*/
139     if (!pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
140         if ((v = udev_device_get_property_value(card, "SOUND_DESCRIPTION")) && *v)
141             pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, v);
142
143     r = 0;
144
145 finish:
146
147     if (card)
148         udev_device_unref(card);
149
150     if (udev)
151         udev_unref(udev);
152
153     return r;
154 }
155
156 char* pa_udev_get_property(int card_idx, const char *name) {
157     struct udev *udev;
158     struct udev_device *card = NULL;
159     char *t, *r = NULL;
160     const char *v;
161
162     pa_assert(card_idx >= 0);
163     pa_assert(name);
164
165     if (!(udev = udev_new())) {
166         pa_log_error("Failed to allocate udev context.");
167         goto finish;
168     }
169
170     t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx);
171     card = udev_device_new_from_syspath(udev, t);
172     pa_xfree(t);
173
174     if (!card) {
175         pa_log_error("Failed to get card object.");
176         goto finish;
177     }
178
179     if ((v = udev_device_get_property_value(card, name)) && *v)
180         r = pa_xstrdup(v);
181
182 finish:
183
184     if (card)
185         udev_device_unref(card);
186
187     if (udev)
188         udev_unref(udev);
189
190     return r;
191 }