2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 fullname (char *dir, char *file)
26 int dlen = strlen (dir);
27 int flen = strlen (file);
30 if (dir[dlen-1] != '/')
32 new = malloc (dlen + slen + flen + 1);
43 load_string(char *dir, char *file)
45 char *full = fullname(dir, file);
55 r = fgets(line, sizeof (line), f);
60 if (r[rlen-1] == '\n')
66 load_hex(char *dir, char *file)
72 line = load_string(dir, file);
75 i = strtol(line, &end, 16);
87 b = strrchr(file, '/');
94 dir_filter_tty(const struct dirent *d)
96 return strncmp(d->d_name, "tty:", 4) == 0;
105 struct dirent **namelist;
108 char endpoint_base[20];
113 base = basename(sys);
114 num_configs = load_hex(sys, "bNumConfigurations");
115 num_interfaces = load_hex(sys, "bNumInterfaces");
116 for (config = 1; config <= num_configs; config++) {
117 for (interface = 0; interface < num_interfaces; interface++) {
118 sprintf(endpoint_base, "%s:%d.%d",
119 base, config, interface);
120 endpoint_full = fullname(sys, endpoint_base);
121 ntty = scandir(endpoint_full, &namelist,
126 tty = fullname("/dev", namelist[0]->d_name + 4);
135 static struct usbdev *
136 usb_scan_device(char *sys)
138 struct usbdev *usbdev;
140 usbdev = calloc(1, sizeof (struct usbdev));
143 usbdev->sys = strdup(sys);
144 usbdev->manufacturer = load_string(sys, "manufacturer");
145 usbdev->product = load_string(sys, "product");
146 usbdev->serial = load_string(sys, "serial");
147 usbdev->idProduct = load_hex(sys, "idProduct");
148 usbdev->idVendor = load_hex(sys, "idVendor");
149 usbdev->tty = usb_tty(sys);
154 aoview_usbdev_free(struct usbdev *usbdev)
157 free(usbdev->manufacturer);
158 free(usbdev->product);
159 free(usbdev->serial);
164 #define USB_DEVICES "/sys/bus/usb/devices"
167 dir_filter_dev(const struct dirent *d)
169 const char *n = d->d_name;
183 aoview_usb_scan(struct usbdev ***devs_ret)
188 struct dirent **ents;
190 struct usbdev **devs = NULL;
193 n = scandir (USB_DEVICES, &ents,
198 for (e = 0; e < n; e++) {
199 dir = fullname(USB_DEVICES, ents[e]->d_name);
200 dev = usb_scan_device(dir);
202 if (dev->idVendor == 0xfffe && dev->tty) {
204 devs = realloc(devs, ndev + 1 * sizeof (struct usbdev *));
206 devs = malloc (sizeof (struct usbdev *));