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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 load_string(char *dir, char *file)
30 char *full = cc_fullname(dir, file);
40 r = fgets(line, sizeof (line), f);
45 if (r[rlen-1] == '\n')
51 load_hex(char *dir, char *file)
57 line = load_string(dir, file);
60 i = strtol(line, &end, 16);
68 dir_filter_tty_colon(const struct dirent *d)
70 return strncmp(d->d_name, "tty:", 4) == 0;
74 dir_filter_tty(const struct dirent *d)
76 return strncmp(d->d_name, "tty", 3) == 0;
85 struct dirent **namelist;
88 char endpoint_base[20];
94 base = cc_basename(sys);
95 num_configs = load_hex(sys, "bNumConfigurations");
96 num_interfaces = load_hex(sys, "bNumInterfaces");
97 for (config = 1; config <= num_configs; config++) {
98 for (interface = 0; interface < num_interfaces; interface++) {
99 sprintf(endpoint_base, "%s:%d.%d",
100 base, config, interface);
101 endpoint_full = cc_fullname(sys, endpoint_base);
103 /* Check for tty:ttyACMx style names
105 ntty = scandir(endpoint_full, &namelist,
106 dir_filter_tty_colon,
110 tty = cc_fullname("/dev", namelist[0]->d_name + 4);
115 /* Check for tty/ttyACMx style names
117 tty_dir = cc_fullname(endpoint_full, "tty");
119 ntty = scandir(tty_dir, &namelist,
124 tty = cc_fullname("/dev", namelist[0]->d_name);
133 static struct cc_usbdev *
134 usb_scan_device(char *sys)
136 struct cc_usbdev *usbdev;
138 usbdev = calloc(1, sizeof (struct cc_usbdev));
141 usbdev->sys = strdup(sys);
142 usbdev->manufacturer = load_string(sys, "manufacturer");
143 usbdev->product = load_string(sys, "product");
144 usbdev->serial = load_string(sys, "serial");
145 usbdev->idProduct = load_hex(sys, "idProduct");
146 usbdev->idVendor = load_hex(sys, "idVendor");
147 usbdev->tty = usb_tty(sys);
152 usbdev_free(struct cc_usbdev *usbdev)
155 free(usbdev->manufacturer);
156 free(usbdev->product);
157 free(usbdev->serial);
162 #define USB_DEVICES "/sys/bus/usb/devices"
165 dir_filter_dev(const struct dirent *d)
167 const char *n = d->d_name;
175 if (c == '.' && n != d->d_name + 1)
183 cc_usbdevs_scan(void)
186 struct dirent **ents;
188 struct cc_usbdev *dev;
189 struct cc_usbdevs *devs;
192 devs = calloc(1, sizeof (struct cc_usbdevs));
196 n = scandir (USB_DEVICES, &ents,
201 for (e = 0; e < n; e++) {
202 dir = cc_fullname(USB_DEVICES, ents[e]->d_name);
203 dev = usb_scan_device(dir);
205 if (dev->idVendor == 0xfffe && dev->tty) {
207 devs->dev = realloc(devs->dev,
208 devs->ndev + 1 * sizeof (struct usbdev *));
210 devs->dev = malloc (sizeof (struct usbdev *));
211 devs->dev[devs->ndev++] = dev;
219 cc_usbdevs_free(struct cc_usbdevs *usbdevs)
225 for (i = 0; i < usbdevs->ndev; i++)
226 usbdev_free(usbdevs->dev[i]);