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 load_string(char *dir, char *file)
25 char *full = aoview_fullname(dir, file);
35 r = fgets(line, sizeof (line), f);
40 if (r[rlen-1] == '\n')
46 load_hex(char *dir, char *file)
52 line = load_string(dir, file);
55 i = strtol(line, &end, 16);
63 dir_filter_tty(const struct dirent *d)
65 return strncmp(d->d_name, "tty:", 4) == 0;
74 struct dirent **namelist;
77 char endpoint_base[20];
82 base = aoview_basename(sys);
83 num_configs = load_hex(sys, "bNumConfigurations");
84 num_interfaces = load_hex(sys, "bNumInterfaces");
85 for (config = 1; config <= num_configs; config++) {
86 for (interface = 0; interface < num_interfaces; interface++) {
87 sprintf(endpoint_base, "%s:%d.%d",
88 base, config, interface);
89 endpoint_full = aoview_fullname(sys, endpoint_base);
90 ntty = scandir(endpoint_full, &namelist,
95 tty = aoview_fullname("/dev", namelist[0]->d_name + 4);
104 static struct usbdev *
105 usb_scan_device(char *sys)
107 struct usbdev *usbdev;
109 usbdev = calloc(1, sizeof (struct usbdev));
112 usbdev->sys = strdup(sys);
113 usbdev->manufacturer = load_string(sys, "manufacturer");
114 usbdev->product = load_string(sys, "product");
115 usbdev->serial = load_string(sys, "serial");
116 usbdev->idProduct = load_hex(sys, "idProduct");
117 usbdev->idVendor = load_hex(sys, "idVendor");
118 usbdev->tty = usb_tty(sys);
123 aoview_usbdev_free(struct usbdev *usbdev)
126 free(usbdev->manufacturer);
127 free(usbdev->product);
128 free(usbdev->serial);
133 #define USB_DEVICES "/sys/bus/usb/devices"
136 dir_filter_dev(const struct dirent *d)
138 const char *n = d->d_name;
152 aoview_usb_scan(struct usbdev ***devs_ret)
157 struct dirent **ents;
159 struct usbdev **devs = NULL;
162 n = scandir (USB_DEVICES, &ents,
167 for (e = 0; e < n; e++) {
168 dir = aoview_fullname(USB_DEVICES, ents[e]->d_name);
169 dev = usb_scan_device(dir);
171 if (dev->idVendor == 0xfffe && dev->tty) {
173 devs = realloc(devs, ndev + 1 * sizeof (struct usbdev *));
175 devs = malloc (sizeof (struct usbdev *));