-
Notifications
You must be signed in to change notification settings - Fork 4
/
devtree.c
338 lines (300 loc) · 8.74 KB
/
devtree.c
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*****************************************************************************/
/*
* devtree.c -- USB device tree.
*
* Copyright (C) 1999 Thomas Sailer, [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Please note that the GPL allows you to use the driver, NOT the radio.
* In order to use the radio, you need a license from the communications
* authority of your country.
*
*/
/*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "devtree.h"
/* ---------------------------------------------------------------------- */
LIST_HEAD(usbbuslist);
/* ---------------------------------------------------------------------- */
static void freedev(struct usbdevnode *dev)
{
free(dev);
}
static void freebus(struct usbbusnode *bus)
{
free(bus);
}
/* ---------------------------------------------------------------------- */
static void markdel(struct list_head *list)
{
struct usbdevnode *dev;
struct list_head *list2;
for (list2 = list->next; list2 != list; list2 = list2->next) {
dev = list_entry(list2, struct usbdevnode, list);
dev->flags |= USBFLG_DELETED;
markdel(&dev->childlist);
}
}
void devtree_markdeleted(void)
{
struct usbbusnode *bus;
struct list_head *list;
for (list = usbbuslist.next; list != &usbbuslist; list = list->next) {
bus = list_entry(list, struct usbbusnode, list);
markdel(&bus->childlist);
}
}
struct usbbusnode *devtree_findbus(unsigned int busn)
{
struct usbbusnode *bus;
struct list_head *list;
for (list = usbbuslist.next; list != &usbbuslist; list = list->next) {
bus = list_entry(list, struct usbbusnode, list);
if (bus->busnum == busn)
return bus;
}
return NULL;
}
static struct usbdevnode *findsubdevice(struct list_head *list, unsigned int devn)
{
struct usbdevnode *dev, *dev2;
struct list_head *list2;
for (list2 = list->next; list2 != list; list2 = list2->next) {
dev = list_entry(list2, struct usbdevnode, list);
if (dev->devnum == devn)
return dev;
dev2 = findsubdevice(&dev->childlist, devn);
if (dev2)
return dev2;
}
return NULL;
}
struct usbdevnode *devtree_finddevice(struct usbbusnode *bus, unsigned int devn)
{
return findsubdevice(&bus->childlist, devn);
}
/* ---------------------------------------------------------------------- */
void devtree_parsedevfile(int fd)
{
char buf[16384];
char *start, *end, *lineend, *cp;
int ret;
unsigned int devnum = 0, busnum = 0, parentdevnum = 0, level = 0;
unsigned int class = 0xff, vendor = 0xffff, prodid = 0xffff, speed = 0;
struct usbbusnode *bus;
struct usbdevnode *dev, *dev2;
devtree_markdeleted();
if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
lprintf(0, "lseek: %s (%d)\n", strerror(errno), errno);
ret = read(fd, buf, sizeof(buf)-1);
if (ret == -1)
lprintf(0, "read: %s (%d)\n", strerror(errno), errno);
end = buf + ret;
*end = 0;
start = buf;
while (start < end) {
lineend = strchr(start, '\n');
if (!lineend)
break;
*lineend = 0;
switch (start[0]) {
case 'T': /* topology line */
if ((cp = strstr(start, "Dev#=")))
devnum = strtoul(cp + 5, NULL, 0);
else
devnum = 0;
if ((cp = strstr(start, "Bus=")))
busnum = strtoul(cp + 4, NULL, 10);
else
busnum = 0;
if ((cp = strstr(start, "Prnt=")))
parentdevnum = strtoul(cp + 5, NULL, 10);
else
parentdevnum = 0;
if ((cp = strstr(start, "Lev=")))
level = strtoul(cp + 4, NULL, 10);
else
level = 0;
if (strstr(start, "Spd=1.5"))
speed = 1;
else if (strstr(start, "Spd=12"))
speed = 2;
else
speed = 0;
break;
case 'D':
if ((cp = strstr(start, "Cls=")))
class = strtoul(cp + 4, NULL, 16);
else
class = 0xff;
break;
case 'P':
if ((cp = strstr(start, "Vendor=")))
vendor = strtoul(cp + 7, NULL, 16);
else
vendor = 0xffff;
if ((cp = strstr(start, "ProdID=")))
prodid = strtoul(cp + 7, NULL, 16);
else
prodid = 0xffff;
/* print device */
#if 0
printf("Device %3d Vendor %04x Product ID %04x Class %02x Speed %s\n",
devnum, vendor, prodid, class, speed == 2 ? "12 MBPS" : speed == 1 ? "1.5 MBPS" : "unknown");
#endif
if (!(bus = devtree_findbus(busnum))) {
if (!(bus = malloc(sizeof(struct usbbusnode))))
lprintf(0, "Out of memory\n");
bus->busnum = busnum;
bus->flags = USBFLG_NEW;
INIT_LIST_HEAD(&bus->childlist);
list_add_tail(&bus->list, &usbbuslist);
} else {
bus->flags &= ~USBFLG_DELETED;
}
if (!(dev = devtree_finddevice(bus, devnum)) || dev->vendorid != vendor || dev->productid != prodid) {
if (!(dev = malloc(sizeof(struct usbdevnode))))
lprintf(0, "Out of memory\n");
dev->devnum = devnum;
dev->flags = USBFLG_NEW;
dev->bus = bus;
dev->vendorid = vendor;
dev->productid = prodid;
INIT_LIST_HEAD(&dev->childlist);
if (level == 0 && parentdevnum == 0) {
list_add_tail(&dev->list, &bus->childlist);
dev->parent = NULL;
} else {
if (!(dev2 = devtree_finddevice(bus, parentdevnum)))
lprintf(0, "Bus %d Device %d Parent Device %d not found\n", busnum, devnum, parentdevnum);
dev->parent = dev2;
list_add_tail(&dev->list, &dev2->childlist);
}
} else {
dev->flags &= ~USBFLG_DELETED;
}
break;
default:
break;
}
#if 0
printf("line: %s\n", start);
#endif
start = lineend + 1;
}
}
/* ---------------------------------------------------------------------- */
static void deletetree(struct list_head *list, unsigned int force)
{
struct usbdevnode *dev;
struct list_head *list2;
for (list2 = list->next; list2 != list;) {
dev = list_entry(list2, struct usbdevnode, list);
list2 = list2->next;
deletetree(&dev->childlist,
force || dev->flags & USBFLG_DELETED);
if (!force && !(dev->flags & USBFLG_DELETED))
continue;
list_del(&dev->list);
INIT_LIST_HEAD(&dev->list);
devtree_devdisconnect(dev);
freedev(dev);
}
}
static void newtree(struct list_head *list)
{
struct usbdevnode *dev;
struct list_head *list2;
for (list2 = list->next; list2 != list; list2 = list2->next) {
dev = list_entry(list2, struct usbdevnode, list);
if (dev->flags & USBFLG_NEW)
devtree_devconnect(dev);
dev->flags &= ~USBFLG_NEW;
newtree(&dev->childlist);
}
}
void devtree_processchanges(void)
{
struct list_head *list;
struct usbbusnode *bus;
for (list = usbbuslist.next; list != &usbbuslist;) {
bus = list_entry(list, struct usbbusnode, list);
list = list->next;
deletetree(&bus->childlist, bus->flags & USBFLG_DELETED);
if (!(bus->flags & USBFLG_DELETED))
continue;
list_del(&bus->list);
INIT_LIST_HEAD(&bus->list);
devtree_busdisconnect(bus);
freebus(bus);
}
for (list = usbbuslist.next; list != &usbbuslist; list = list->next) {
bus = list_entry(list, struct usbbusnode, list);
if (bus->flags & USBFLG_NEW)
devtree_busconnect(bus);
bus->flags &= ~USBFLG_NEW;
newtree(&bus->childlist);
}
}
/* ---------------------------------------------------------------------- */
static void dumpdevlist(struct list_head *list, unsigned int level,
unsigned int mask)
{
struct usbdevnode *dev;
struct list_head *list2;
char buf[512];
char *cp;
unsigned int i;
for (list2 = list->next; list2 != list; ) {
dev = list_entry(list2, struct usbdevnode, list);
list2 = list2->next;
for (cp = buf, i = 0; i < level; i++) {
*cp++ = (mask & (1 << i)) ? '|' : ' ';
*cp++ = ' ';
}
if (list2 != list) {
mask |= (1 << level);
*cp++ = '|';
} else {
mask &= ~(1 << level);
*cp++ = '`';
}
*cp++ = '-';
snprintf(cp, buf + sizeof(buf) - cp,
"Dev# %3d Vendor 0x%04x Product 0x%04x",
dev->devnum, dev->vendorid, dev->productid);
lprintf(1, "%s\n", buf);
dumpdevlist(&dev->childlist, level+1, mask);
}
}
void devtree_dump(void)
{
struct list_head *list;
struct usbbusnode *bus;
for (list = usbbuslist.next; list != &usbbuslist; list = list->next) {
bus = list_entry(list, struct usbbusnode, list);
lprintf(1, "Bus# %2d\n", bus->busnum);
dumpdevlist(&bus->childlist, 0, 0);
}
}