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 load_dec(char *dir, char *file)
74 line = load_string(dir, file);
77 i = strtol(line, &end, 10);
85 dir_filter_tty_colon(const struct dirent *d)
87 return strncmp(d->d_name, "tty:", 4) == 0;
91 dir_filter_tty(const struct dirent *d)
93 return strncmp(d->d_name, "tty", 3) == 0;
102 struct dirent **namelist;
105 char endpoint_base[20];
111 base = cc_basename(sys);
112 num_configs = load_hex(sys, "bNumConfigurations");
113 num_interfaces = load_hex(sys, "bNumInterfaces");
114 for (config = 1; config <= num_configs; config++) {
115 for (interface = 0; interface < num_interfaces; interface++) {
116 sprintf(endpoint_base, "%s:%d.%d",
117 base, config, interface);
118 endpoint_full = cc_fullname(sys, endpoint_base);
120 /* Check for tty:ttyACMx style names
122 ntty = scandir(endpoint_full, &namelist,
123 dir_filter_tty_colon,
127 tty = cc_fullname("/dev", namelist[0]->d_name + 4);
132 /* Check for tty/ttyACMx style names
134 tty_dir = cc_fullname(endpoint_full, "tty");
136 ntty = scandir(tty_dir, &namelist,
141 tty = cc_fullname("/dev", namelist[0]->d_name);
150 static struct cc_usbdev *
151 usb_scan_device(char *sys)
153 struct cc_usbdev *usbdev;
155 usbdev = calloc(1, sizeof (struct cc_usbdev));
158 usbdev->sys = strdup(sys);
159 usbdev->manufacturer = load_string(sys, "manufacturer");
160 usbdev->product = load_string(sys, "product");
161 usbdev->serial = load_dec(sys, "serial");
162 usbdev->idProduct = load_hex(sys, "idProduct");
163 usbdev->idVendor = load_hex(sys, "idVendor");
164 usbdev->tty = usb_tty(sys);
169 usbdev_free(struct cc_usbdev *usbdev)
172 free(usbdev->manufacturer);
173 free(usbdev->product);
174 /* this can get used as a return value */
180 #define USB_DEVICES "/sys/bus/usb/devices"
183 dir_filter_dev(const struct dirent *d)
185 const char *n = d->d_name;
193 if (c == '.' && n != d->d_name + 1)
201 cc_usbdevs_scan(void)
204 struct dirent **ents;
206 struct cc_usbdev *dev;
207 struct cc_usbdevs *devs;
210 devs = calloc(1, sizeof (struct cc_usbdevs));
214 n = scandir (USB_DEVICES, &ents,
219 for (e = 0; e < n; e++) {
220 dir = cc_fullname(USB_DEVICES, ents[e]->d_name);
221 dev = usb_scan_device(dir);
223 if (dev->idVendor == 0xfffe && dev->tty) {
225 devs->dev = realloc(devs->dev,
226 (devs->ndev + 1) * sizeof (struct usbdev *));
228 devs->dev = malloc (sizeof (struct usbdev *));
229 devs->dev[devs->ndev++] = dev;
237 cc_usbdevs_free(struct cc_usbdevs *usbdevs)
243 for (i = 0; i < usbdevs->ndev; i++)
244 usbdev_free(usbdevs->dev[i]);
249 match_dev(char *product, int serial)
251 struct cc_usbdevs *devs;
252 struct cc_usbdev *dev;
256 devs = cc_usbdevs_scan();
259 for (i = 0; i < devs->ndev; i++) {
261 if (product && strncmp (product, dev->product, strlen(product)) != 0)
263 if (serial && serial != dev->serial)
267 if (i < devs->ndev) {
268 tty = devs->dev[i]->tty;
269 devs->dev[i]->tty = NULL;
271 cc_usbdevs_free(devs);
276 cc_usbdevs_find_by_arg(char *arg, char *default_product)
286 /* check for <serial> */
287 serial = strtol(arg, &end, 0);
293 /* check for <product>:<serial> */
294 colon = strchr(arg, ':');
296 product = strndup(arg, colon - arg);
297 serial = strtol(colon + 1, &end, 0);
310 if (!product && default_product)
311 tty = match_dev(default_product, serial);
313 tty = match_dev(product, serial);
314 if (product && product != arg)