2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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.
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.
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
28 #include <pulse/xmalloc.h>
29 #include <pulse/proplist.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/core-util.h>
34 #include "udev-util.h"
36 static int read_id(struct udev_device
*d
, const char *n
) {
43 if (!(v
= udev_device_get_property_value(d
, n
)))
46 if (pa_startswith(v
, "0x"))
52 if (sscanf(v
, "%04x", &u
) != 1)
61 static int dehex(char x
) {
62 if (x
>= '0' && x
<= '9')
65 if (x
>= 'A' && x
<= 'F')
68 if (x
>= 'a' && x
<= 'f')
74 static void proplist_sets_unescape(pa_proplist
*p
, const char *prop
, const char *s
) {
86 /* The resulting string is definitely shorter than the source string */
87 r
= pa_xnew(char, strlen(s
)+1);
89 for (f
= s
, t
= r
; *f
; f
++) {
132 *(t
++) = (char) (c
<< 4) | d
;
163 pa_proplist_sets(p
, prop
, r
);
167 int pa_udev_get_info(int card_idx
, pa_proplist
*p
) {
170 struct udev_device
*card
= NULL
;
176 pa_assert(card_idx
>= 0);
178 if (!(udev
= udev_new())) {
179 pa_log_error("Failed to allocate udev context.");
183 t
= pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev
), card_idx
);
184 card
= udev_device_new_from_syspath(udev
, t
);
188 pa_log_error("Failed to get card object.");
192 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_BUS_PATH
))
193 if (((v
= udev_device_get_property_value(card
, "ID_PATH")) && *v
) ||
194 (v
= udev_device_get_devpath(card
)))
195 pa_proplist_sets(p
, PA_PROP_DEVICE_BUS_PATH
, v
);
197 if (!pa_proplist_contains(p
, "sysfs.path"))
198 if ((v
= udev_device_get_devpath(card
)))
199 pa_proplist_sets(p
, "sysfs.path", v
);
201 if (!pa_proplist_contains(p
, "udev.id"))
202 if ((v
= udev_device_get_property_value(card
, "ID_ID")) && *v
)
203 pa_proplist_sets(p
, "udev.id", v
);
205 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_BUS
))
206 if ((v
= udev_device_get_property_value(card
, "ID_BUS")) && *v
)
207 pa_proplist_sets(p
, PA_PROP_DEVICE_BUS
, v
);
209 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_VENDOR_ID
))
210 if ((id
= read_id(card
, "ID_VENDOR_ID")) > 0)
211 pa_proplist_setf(p
, PA_PROP_DEVICE_VENDOR_ID
, "%04x", id
);
213 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_VENDOR_NAME
)) {
214 if ((v
= udev_device_get_property_value(card
, "ID_VENDOR_FROM_DATABASE")) && *v
)
215 pa_proplist_sets(p
, PA_PROP_DEVICE_VENDOR_NAME
, v
);
216 else if ((v
= udev_device_get_property_value(card
, "ID_VENDOR_ENC")) && *v
)
217 proplist_sets_unescape(p
, PA_PROP_DEVICE_VENDOR_NAME
, v
);
218 else if ((v
= udev_device_get_property_value(card
, "ID_VENDOR")) && *v
)
219 pa_proplist_sets(p
, PA_PROP_DEVICE_VENDOR_NAME
, v
);
222 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_PRODUCT_ID
))
223 if ((id
= read_id(card
, "ID_MODEL_ID")) >= 0)
224 pa_proplist_setf(p
, PA_PROP_DEVICE_PRODUCT_ID
, "%04x", id
);
226 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_PRODUCT_NAME
)) {
227 if ((v
= udev_device_get_property_value(card
, "ID_MODEL_FROM_DATABASE")) && *v
)
228 pa_proplist_sets(p
, PA_PROP_DEVICE_PRODUCT_NAME
, v
);
229 else if ((v
= udev_device_get_property_value(card
, "ID_MODEL_ENC")) && *v
)
230 proplist_sets_unescape(p
, PA_PROP_DEVICE_PRODUCT_NAME
, v
);
231 else if ((v
= udev_device_get_property_value(card
, "ID_MODEL")) && *v
)
232 pa_proplist_sets(p
, PA_PROP_DEVICE_PRODUCT_NAME
, v
);
235 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_SERIAL
))
236 if ((v
= udev_device_get_property_value(card
, "ID_SERIAL")) && *v
)
237 pa_proplist_sets(p
, PA_PROP_DEVICE_SERIAL
, v
);
239 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_CLASS
))
240 if ((v
= udev_device_get_property_value(card
, "SOUND_CLASS")) && *v
)
241 pa_proplist_sets(p
, PA_PROP_DEVICE_CLASS
, v
);
243 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_FORM_FACTOR
))
244 if ((v
= udev_device_get_property_value(card
, "SOUND_FORM_FACTOR")) && *v
)
245 pa_proplist_sets(p
, PA_PROP_DEVICE_FORM_FACTOR
, v
);
247 /* This is normaly not set by the udev rules but may be useful to
248 * allow administrators to overwrite the device description.*/
249 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_DESCRIPTION
))
250 if ((v
= udev_device_get_property_value(card
, "SOUND_DESCRIPTION")) && *v
)
251 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, v
);
258 udev_device_unref(card
);
266 char* pa_udev_get_property(int card_idx
, const char *name
) {
268 struct udev_device
*card
= NULL
;
272 pa_assert(card_idx
>= 0);
275 if (!(udev
= udev_new())) {
276 pa_log_error("Failed to allocate udev context.");
280 t
= pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev
), card_idx
);
281 card
= udev_device_new_from_syspath(udev
, t
);
285 pa_log_error("Failed to get card object.");
289 if ((v
= udev_device_get_property_value(card
, name
)) && *v
)
295 udev_device_unref(card
);