/
usbtree.c
404 lines (322 loc) · 12.1 KB
/
usbtree.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*************************************************************************
** usbtree.c for USBView - a USB device viewer
** Copyright (c) 1999, 2000 by Greg Kroah-Hartman, <greg@kroah.com>
**
** 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.
**
** 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.
**
** (See the included file COPYING)
*************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <gtk/gtk.h>
#include "usbtree.h"
#include "usbparse.h"
#define MAX_LINE_SIZE 1000
static void Init (void)
{
GtkTextIter begin;
GtkTextIter end;
/* blow away the tree if there is one */
if (rootDevice != NULL) {
gtk_ctree_remove_node (GTK_CTREE(treeUSB), GTK_CTREE_NODE(rootDevice->leaf));
}
/* clean out the text box */
gtk_text_buffer_get_start_iter(textDescriptionBuffer,&begin);
gtk_text_buffer_get_end_iter(textDescriptionBuffer,&end);
gtk_text_buffer_delete (textDescriptionBuffer, &begin, &end);
return;
}
static void PopulateListBox (int deviceId)
{
Device *device;
char *string;
char *tempString;
int configNum;
int interfaceNum;
int endpointNum;
int deviceNumber = (deviceId >> 8);
int busNumber = (deviceId & 0x00ff);
GtkTextIter begin;
GtkTextIter end;
device = usb_find_device (deviceNumber, busNumber);
if (device == NULL) {
printf ("Can't seem to find device info to display\n");
return;
}
/* clear the textbox */
gtk_text_buffer_get_start_iter(textDescriptionBuffer,&begin);
gtk_text_buffer_get_end_iter(textDescriptionBuffer,&end);
gtk_text_buffer_delete (textDescriptionBuffer, &begin, &end);
/* freeze the display */
/* this keeps the annoying scroll from happening */
gtk_widget_freeze_child_notify(textDescriptionView);
string = (char *)g_malloc (1000);
/* add the name to the textbox if we have one*/
if (device->name != NULL) {
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, device->name,strlen(device->name));
}
/* add the manufacturer if we have one */
if (device->manufacturer != NULL) {
sprintf (string, "\nManufacturer: %s", device->manufacturer);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
}
/* add the serial number if we have one */
if (device->serialNumber != NULL) {
sprintf (string, "\nSerial Number: %s", device->serialNumber);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
}
/* add speed */
switch (device->speed) {
case 1 : tempString = "1.5Mb/s (low)"; break;
case 12 : tempString = "12Mb/s (full)"; break;
case 480 : tempString = "480Mb/s (high)"; break; /* planning ahead... */
default : tempString = "unknown"; break;
}
sprintf (string, "\nSpeed: %s", tempString);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
/* add ports if available */
if (device->maxChildren) {
sprintf (string, "\nNumber of Ports: %i", device->maxChildren);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
}
/* add the bandwidth info if available */
if (device->bandwidth != NULL) {
sprintf (string, "\nBandwidth allocated: %i / %i (%i%%)", device->bandwidth->allocated, device->bandwidth->total, device->bandwidth->percent);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
sprintf (string, "\nTotal number of interrupt requests: %i", device->bandwidth->numInterruptRequests);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
sprintf (string, "\nTotal number of isochronous requests: %i", device->bandwidth->numIsocRequests);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
}
/* add the USB version, device class, subclass, protocol, max packet size, and the number of configurations (if it is there) */
if (device->version) {
sprintf (string, "\nUSB Version: %s\nDevice Class: %s\nDevice Subclass: %s\nDevice Protocol: %s\n"
"Maximum Default Endpoint Size: %i\nNumber of Configurations: %i",
device->version, device->class, device->subClass, device->protocol,
device->maxPacketSize, device->numConfigs);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
}
/* add the vendor id, product id, and revision number (if it is there) */
if (device->vendorId) {
sprintf (string, "\nVendor Id: %.4x\nProduct Id: %.4x\nRevision Number: %s",
device->vendorId, device->productId, device->revisionNumber);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
}
/* display all the info for the configs */
for (configNum = 0; configNum < MAX_CONFIGS; ++configNum) {
if (device->config[configNum]) {
DeviceConfig *config = device->config[configNum];
/* show this config */
sprintf (string, "\n\nConfig Number: %i\n\tNumber of Interfaces: %i\n\t"
"Attributes: %.2x\n\tMaxPower Needed: %s",
config->configNumber, config->numInterfaces,
config->attributes, config->maxPower);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
/* show all of the interfaces for this config */
for (interfaceNum = 0; interfaceNum < MAX_INTERFACES; ++interfaceNum) {
if (config->interface[interfaceNum]) {
DeviceInterface *interface = config->interface[interfaceNum];
sprintf (string, "\n\n\tInterface Number: %i", interface->interfaceNumber);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string, strlen(string));
if (interface->name != NULL) {
sprintf (string, "\n\t\tName: %s", interface->name);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string, strlen(string));
}
sprintf (string, "\n\t\tAlternate Number: %i\n\t\tClass: %s\n\t\t"
"Sub Class: %.2x\n\t\tProtocol: %.2x\n\t\tNumber of Endpoints: %i",
interface->alternateNumber, interface->class,
interface->subClass, interface->protocol, interface->numEndpoints);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string, strlen(string));
/* show all of the endpoints for this interface */
for (endpointNum = 0; endpointNum < MAX_ENDPOINTS; ++endpointNum) {
if (interface->endpoint[endpointNum]) {
DeviceEndpoint *endpoint = interface->endpoint[endpointNum];
sprintf (string, "\n\n\t\t\tEndpoint Address: %.2x\n\t\t\t"
"Direction: %s\n\t\t\tAttribute: %i\n\t\t\t"
"Type: %s\n\t\t\tMax Packet Size: %i\n\t\t\tInterval: %s",
endpoint->address,
endpoint->in ? "in" : "out", endpoint->attribute,
endpoint->type, endpoint->maxPacketSize, endpoint->interval);
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
}
}
}
}
}
}
/* thaw the display */
gtk_widget_thaw_child_notify(textDescriptionView);
/* clean up our string */
g_free (string);
return;
}
void SelectItem (GtkWidget *widget, GtkCTreeNode *node, gint column, gpointer userData)
{
int data;
data = (int) gtk_ctree_node_get_row_data (GTK_CTREE (widget), node);
PopulateListBox ((int)data);
return;
}
static void DisplayDevice (Device *parent, Device *device)
{
int i;
gchar *text[1];
int configNum;
int interfaceNum;
gboolean driverAttached = TRUE;
if (device == NULL)
return;
/* build this node */
text[0] = device->name;
device->leaf = gtk_ctree_insert_node (GTK_CTREE(treeUSB), parent->leaf, NULL, text, 1, NULL, NULL, NULL, NULL, FALSE, FALSE);
gtk_ctree_node_set_row_data (GTK_CTREE(treeUSB), device->leaf, (gpointer)((device->deviceNumber<<8) | (device->busNumber)));
/* determine if this device has drivers attached to all interfaces */
for (configNum = 0; configNum < MAX_CONFIGS; ++configNum) {
if (device->config[configNum]) {
DeviceConfig *config = device->config[configNum];
for (interfaceNum = 0; interfaceNum < MAX_INTERFACES; ++interfaceNum) {
if (config->interface[interfaceNum]) {
DeviceInterface *interface = config->interface[interfaceNum];
if (interface->driverAttached == FALSE) {
driverAttached = FALSE;
break;
}
}
}
}
}
/* change the color of this leaf if there are no drivers attached to it */
if (driverAttached == FALSE) {
GdkColor red;
red.red = 56000;
red.green = 0;
red.blue = 0;
red.pixel = 0;
gtk_ctree_node_set_foreground (GTK_CTREE(treeUSB), device->leaf, &red);
}
/* create all of the children's leafs */
for (i = 0; i < MAX_CHILDREN; ++i) {
DisplayDevice (device, device->child[i]);
}
return;
}
#define FILENAME_SIZE 1000;
gchar devicesFile[1000];
static gchar previousDevicesFile[1000];
static time_t previousChange;
const char *verifyMessage = " Verify that you have USB compiled into your kernel, \n"
" have the USB core modules loaded, and have the \n"
" usbdevfs filesystem mounted. ";
static void FileError (void)
{
gchar *tempString = g_malloc0(strlen (verifyMessage) + strlen (devicesFile) + 50);
sprintf (tempString, " Can not open the file %s \n\n%s", devicesFile, verifyMessage);
ShowMessage ("USBView Error", tempString, FALSE);
g_free (tempString);
return;
}
static int FileHasChanged (void)
{
struct stat file_info;
int result;
if (strcmp (previousDevicesFile, devicesFile) == 0) {
/* we've looked at this filename before, so check the file time of the file */
result = stat (devicesFile, &file_info);
if (result) {
/* something wrong in looking for this file */
return 0;
}
if (file_info.st_ctime == previousChange) {
/* no change */
return 0;
} else {
/* something changed */
previousChange = file_info.st_ctime;
return 1;
}
} else {
/* filenames are different, so save the name for the next time */
strcpy (previousDevicesFile, devicesFile);
return 1;
}
}
void LoadUSBTree (int refresh)
{
static gboolean signal_connected = FALSE;
FILE *usbFile;
char *dataLine;
int finished;
int i;
if (MessageShown() == TRUE) {
return;
}
/* if refresh is selected, then always do a refresh, otherwise look at the file first */
if (!refresh) {
if (!FileHasChanged()) {
return;
}
}
usbFile = fopen (devicesFile, "r");
if (usbFile == NULL) {
FileError();
return;
}
finished = 0;
Init();
usb_initialize_list ();
dataLine = (char *)g_malloc (MAX_LINE_SIZE);
while (!finished) {
/* read the line in from the file */
fgets (dataLine, MAX_LINE_SIZE-1, usbFile);
if (dataLine[strlen(dataLine)-1] == '\n')
usb_parse_line (dataLine);
if (feof (usbFile))
finished = 1;
}
fclose (usbFile);
g_free (dataLine);
usb_name_devices ();
/* set up our tree */
gtk_ctree_set_line_style (GTK_CTREE(treeUSB), GTK_CTREE_LINES_DOTTED);
gtk_ctree_set_expander_style (GTK_CTREE(treeUSB), GTK_CTREE_EXPANDER_SQUARE);
gtk_ctree_set_indent (GTK_CTREE(treeUSB),10);
gtk_clist_column_titles_passive (GTK_CLIST(treeUSB));
/* build our tree */
for (i = 0; i < rootDevice->maxChildren; ++i) {
DisplayDevice (rootDevice, rootDevice->child[i]);
}
gtk_widget_show (treeUSB);
gtk_ctree_expand_recursive (GTK_CTREE(treeUSB), NULL);
/* hook up our callback function to this tree if we haven't yet */
if (!signal_connected) {
gtk_signal_connect (GTK_OBJECT (treeUSB), "tree-select-row", GTK_SIGNAL_FUNC (SelectItem), NULL);
signal_connected = TRUE;
}
return;
}
void initialize_stuff (void)
{
strcpy (devicesFile, "/proc/bus/usb/devices");
memset (&previousDevicesFile[0], 0x00, sizeof(previousDevicesFile));
previousChange = 0;
return;
}