summaryrefslogtreecommitdiffstats
path: root/src/modules/udev-util.c
blob: 026493be0adade40f6bf8819ae0ed125962b789b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/***
  This file is part of PulseAudio.

  Copyright 2009 Lennart Poettering

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libudev.h>

#include <pulse/xmalloc.h>
#include <pulse/proplist.h>

#include <pulsecore/log.h>
#include <pulsecore/core-util.h>

#include "udev-util.h"

static int read_id(struct udev_device *d, const char *n) {
    const char *v;
    unsigned u;

    pa_assert(d);
    pa_assert(n);

    if (!(v = udev_device_get_property_value(d, n)))
        return -1;

    if (pa_startswith(v, "0x"))
        v += 2;

    if (!*v)
        return -1;

    if (sscanf(v, "%04x", &u) != 1)
        return -1;

    if (u > 0xFFFFU)
        return -1;

    return u;
}

static int dehex(char x) {
    if (x >= '0' && x <= '9')
        return x - '0';

    if (x >= 'A' && x <= 'F')
        return x - 'A' + 10;

    if (x >= 'a' && x <= 'f')
        return x - 'a' + 10;

    return -1;
}

static void proplist_sets_unescape(pa_proplist *p, const char *prop, const char *s) {
    const char *f;
    char *t, *r;
    int c = 0;

    enum {
        TEXT,
        BACKSLASH,
        EX,
        FIRST
    } state = TEXT;

    /* The resulting string is definitely shorter than the source string */
    r = pa_xnew(char, strlen(s)+1);

    for (f = s, t = r; *f; f++) {

        switch (state) {

            case TEXT:
                if (*f == '\\')
                    state = BACKSLASH;
                else
                    *(t++) = *f;
                break;

            case BACKSLASH:
                if (*f == 'x')
                    state = EX;
                else {
                    *(t++) = '\\';
                    *(t++) = *f;
                    state = TEXT;
                }
                break;

            case EX:
                c = dehex(*f);

                if (c < 0) {
                    *(t++) = '\\';
                    *(t++) = 'x';
                    *(t++) = *f;
                    state = TEXT;
                } else
                    state = FIRST;

                break;

            case FIRST: {
                int d = dehex(*f);

                if (d < 0) {
                    *(t++) = '\\';
                    *(t++) = 'x';
                    *(t++) = *(f-1);
                    *(t++) = *f;
                } else
                    *(t++) = (char) (c << 4) | d;

                state = TEXT;
                break;
            }
        }
    }

    switch (state) {

        case TEXT:
            break;

        case BACKSLASH:
            *(t++) = '\\';
            break;

        case EX:
            *(t++) = '\\';
            *(t++) = 'x';
            break;

        case FIRST:
            *(t++) = '\\';
            *(t++) = 'x';
            *(t++) = *(f-1);
            break;
    }

    *t = 0;

    pa_proplist_sets(p, prop, r);
    pa_xfree(r);
}

int pa_udev_get_info(int card_idx, pa_proplist *p) {
    int r = -1;
    struct udev *udev;
    struct udev_device *card = NULL;
    char *t;
    const char *v;
    const char *bus = NULL;
    int id;

    pa_assert(p);
    pa_assert(card_idx >= 0);

    if (!(udev = udev_new())) {
        pa_log_error("Failed to allocate udev context.");
        goto finish;
    }

    t = pa_sprintf_malloc("/sys/class/sound/card%i", card_idx);
    card = udev_device_new_from_syspath(udev, t);
    pa_xfree(t);

    if (!card) {
        pa_log_error("Failed to get card object.");
        goto finish;
    }

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS_PATH))
        if (((v = udev_device_get_property_value(card, "ID_PATH")) && *v) ||
            (v = udev_device_get_devpath(card)))
            pa_proplist_sets(p, PA_PROP_DEVICE_BUS_PATH, v);

    if (!pa_proplist_contains(p, "sysfs.path"))
        if ((v = udev_device_get_devpath(card)))
            pa_proplist_sets(p, "sysfs.path", v);

    if (!pa_proplist_contains(p, "udev.id"))
        if ((v = udev_device_get_property_value(card, "ID_ID")) && *v)
            pa_proplist_sets(p, "udev.id", v);

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS))
        if ((bus = udev_device_get_property_value(card, "ID_BUS")) && *bus)
            pa_proplist_sets(p, PA_PROP_DEVICE_BUS, bus);

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_ID))
        if ((id = read_id(card, "ID_VENDOR_ID")) > 0)
            pa_proplist_setf(p, PA_PROP_DEVICE_VENDOR_ID, "%04x", id);

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_NAME)) {
        /* ID_VENDOR_FROM_DATABASE returns the name of IEEE 1394 Phy/Link chipset for FireWire devices */
        if (!pa_safe_streq(bus, "firewire") && (v = udev_device_get_property_value(card, "ID_VENDOR_FROM_DATABASE")) && *v)
            pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
        else if ((v = udev_device_get_property_value(card, "ID_VENDOR_ENC")) && *v)
            proplist_sets_unescape(p, PA_PROP_DEVICE_VENDOR_NAME, v);
        else if ((v = udev_device_get_property_value(card, "ID_VENDOR")) && *v)
            pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
    }

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_ID))
        if ((id = read_id(card, "ID_MODEL_ID")) >= 0)
            pa_proplist_setf(p, PA_PROP_DEVICE_PRODUCT_ID, "%04x", id);

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_NAME)) {
        /* ID_MODEL_FROM_DATABASE returns the name of IEEE 1394 Phy/Link chipset for FireWire devices */
        if (!pa_safe_streq(bus, "firewire") && (v = udev_device_get_property_value(card, "ID_MODEL_FROM_DATABASE")) && *v)
            pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
        else if ((v = udev_device_get_property_value(card, "ID_MODEL_ENC")) && *v)
            proplist_sets_unescape(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
        else if ((v = udev_device_get_property_value(card, "ID_MODEL")) && *v)
            pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
    }

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_SERIAL))
        if ((v = udev_device_get_property_value(card, "ID_SERIAL")) && *v)
            pa_proplist_sets(p, PA_PROP_DEVICE_SERIAL, v);

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_CLASS))
        if ((v = udev_device_get_property_value(card, "SOUND_CLASS")) && *v)
            pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, v);

    if (!pa_proplist_contains(p, PA_PROP_DEVICE_FORM_FACTOR))
        if ((v = udev_device_get_property_value(card, "SOUND_FORM_FACTOR")) && *v)
            pa_proplist_sets(p, PA_PROP_DEVICE_FORM_FACTOR, v);

    /* This is normally not set by the udev rules but may be useful to
     * allow administrators to overwrite the device description.*/
    if (!pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
        if ((v = udev_device_get_property_value(card, "SOUND_DESCRIPTION")) && *v)
            pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, v);

    r = 0;

finish:

    if (card)
        udev_device_unref(card);

    if (udev)
        udev_unref(udev);

    return r;
}

char* pa_udev_get_property(int card_idx, const char *name) {
    struct udev *udev;
    struct udev_device *card = NULL;
    char *t, *r = NULL;
    const char *v;

    pa_assert(card_idx >= 0);
    pa_assert(name);

    if (!(udev = udev_new())) {
        pa_log_error("Failed to allocate udev context.");
        goto finish;
    }

    t = pa_sprintf_malloc("/sys/class/sound/card%i", card_idx);
    card = udev_device_new_from_syspath(udev, t);
    pa_xfree(t);

    if (!card) {
        pa_log_error("Failed to get card object.");
        goto finish;
    }

    if ((v = udev_device_get_property_value(card, name)) && *v)
        r = pa_xstrdup(v);

finish:

    if (card)
        udev_device_unref(card);

    if (udev)
        udev_unref(udev);

    return r;
}