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 int pa_udev_get_info(pa_core
*core
, pa_proplist
*p
, int card_idx
) {
64 struct udev_device
*card
= NULL
;
71 pa_assert(card_idx
>= 0);
73 if (!(udev
= udev_new())) {
74 pa_log_error("Failed to allocate udev context.");
78 t
= pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev
), card_idx
);
79 card
= udev_device_new_from_syspath(udev
, t
);
83 pa_log_error("Failed to get card object.");
87 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_BUS_PATH
))
88 if (((v
= udev_device_get_property_value(card
, "ID_PATH")) && *v
) ||
89 (v
= udev_device_get_devpath(card
)))
90 pa_proplist_sets(p
, PA_PROP_DEVICE_BUS_PATH
, v
);
92 if (!pa_proplist_contains(p
, "sysfs.path"))
93 if ((v
= udev_device_get_devpath(card
)))
94 pa_proplist_sets(p
, "sysfs.path", v
);
96 if (!pa_proplist_contains(p
, "udev.id"))
97 if ((v
= udev_device_get_property_value(card
, "ID_ID")) && *v
)
98 pa_proplist_sets(p
, "udev.id", v
);
100 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_BUS
))
101 if ((v
= udev_device_get_property_value(card
, "ID_BUS")) && *v
)
102 pa_proplist_sets(p
, PA_PROP_DEVICE_BUS
, v
);
104 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_VENDOR_ID
))
105 if ((id
= read_id(card
, "ID_VENDOR_ID")) > 0)
106 pa_proplist_setf(p
, PA_PROP_DEVICE_VENDOR_ID
, "%04x", id
);
108 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_VENDOR_NAME
)) {
109 if ((v
= udev_device_get_property_value(card
, "ID_VENDOR_FROM_DATABASE")) && *v
)
110 pa_proplist_sets(p
, PA_PROP_DEVICE_VENDOR_NAME
, v
);
111 else if ((v
= udev_device_get_property_value(card
, "ID_VENDOR")) && *v
)
112 pa_proplist_sets(p
, PA_PROP_DEVICE_VENDOR_NAME
, v
);
115 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_PRODUCT_ID
))
116 if ((id
= read_id(card
, "ID_MODEL_ID")) >= 0)
117 pa_proplist_setf(p
, PA_PROP_DEVICE_PRODUCT_ID
, "%04x", id
);
119 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_PRODUCT_NAME
)) {
120 if ((v
= udev_device_get_property_value(card
, "ID_MODEL_FROM_DATABASE")) && *v
)
121 pa_proplist_sets(p
, PA_PROP_DEVICE_PRODUCT_NAME
, v
);
122 else if ((v
= udev_device_get_property_value(card
, "ID_MODEL")) && *v
)
123 pa_proplist_sets(p
, PA_PROP_DEVICE_PRODUCT_NAME
, v
);
126 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_SERIAL
))
127 if ((v
= udev_device_get_property_value(card
, "ID_SERIAL")) && *v
)
128 pa_proplist_sets(p
, PA_PROP_DEVICE_SERIAL
, v
);
130 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_CLASS
))
131 if ((v
= udev_device_get_property_value(card
, "SOUND_CLASS")) && *v
)
132 pa_proplist_sets(p
, PA_PROP_DEVICE_CLASS
, v
);
134 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_FORM_FACTOR
))
135 if ((v
= udev_device_get_property_value(card
, "SOUND_FORM_FACTOR")) && *v
)
136 pa_proplist_sets(p
, PA_PROP_DEVICE_FORM_FACTOR
, v
);
138 /* This is normaly not set by the udev rules but may be useful to
139 * allow administrators to overwrite the device description.*/
140 if (!pa_proplist_contains(p
, PA_PROP_DEVICE_DESCRIPTION
))
141 if ((v
= udev_device_get_property_value(card
, "SOUND_DESCRIPTION")) && *v
)
142 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, v
);
149 udev_device_unref(card
);