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_colon(const struct dirent *d)
65 return strncmp(d->d_name, "tty:", 4) == 0;
69 dir_filter_tty(const struct dirent *d)
71 return strncmp(d->d_name, "tty", 3) == 0;
80 struct dirent **namelist;
83 char endpoint_base[20];
89 base = aoview_basename(sys);
90 num_configs = load_hex(sys, "bNumConfigurations");
91 num_interfaces = load_hex(sys, "bNumInterfaces");
92 for (config = 1; config <= num_configs; config++) {
93 for (interface = 0; interface < num_interfaces; interface++) {
94 sprintf(endpoint_base, "%s:%d.%d",
95 base, config, interface);
96 endpoint_full = aoview_fullname(sys, endpoint_base);
98 /* Check for tty:ttyACMx style names
100 ntty = scandir(endpoint_full, &namelist,
101 dir_filter_tty_colon,
105 tty = aoview_fullname("/dev", namelist[0]->d_name + 4);
110 /* Check for tty/ttyACMx style names
112 tty_dir = aoview_fullname(endpoint_full, "tty");
114 ntty = scandir(tty_dir, &namelist,
119 tty = aoview_fullname("/dev", namelist[0]->d_name);
128 static struct usbdev *
129 usb_scan_device(char *sys)
131 struct usbdev *usbdev;
133 usbdev = calloc(1, sizeof (struct usbdev));
136 usbdev->sys = strdup(sys);
137 usbdev->manufacturer = load_string(sys, "manufacturer");
138 usbdev->product = load_string(sys, "product");
139 usbdev->serial = load_string(sys, "serial");
140 usbdev->idProduct = load_hex(sys, "idProduct");
141 usbdev->idVendor = load_hex(sys, "idVendor");
142 usbdev->tty = usb_tty(sys);
147 aoview_usbdev_free(struct usbdev *usbdev)
150 free(usbdev->manufacturer);
151 free(usbdev->product);
152 free(usbdev->serial);
157 #define USB_DEVICES "/sys/bus/usb/devices"
160 dir_filter_dev(const struct dirent *d)
162 const char *n = d->d_name;
170 if (c == '.' && n != d->d_name + 1)
178 aoview_usb_scan(struct usbdev ***devs_ret)
183 struct dirent **ents;
185 struct usbdev **devs = NULL;
188 n = scandir (USB_DEVICES, &ents,
193 for (e = 0; e < n; e++) {
194 dir = aoview_fullname(USB_DEVICES, ents[e]->d_name);
195 dev = usb_scan_device(dir);
197 if (dev->idVendor == 0xfffe && dev->tty) {
199 devs = realloc(devs, ndev + 1 * sizeof (struct usbdev *));
201 devs = malloc (sizeof (struct usbdev *));