2 * Copyright © 2010 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.
24 match_dev(char *product, int serial, struct altos_device *device)
26 struct altos_list *list;
29 list = altos_list_start();
32 while ((i = altos_list_next(list, device)) != 0) {
33 if (product && strncmp (product, device->product, strlen(product)) != 0)
35 if (serial && serial != device->serial)
39 altos_list_finish(list);
44 altos_find_by_arg(char *arg, char *default_product, struct altos_device *device)
54 /* check for <serial> */
55 serial = strtol(arg, &end, 0);
61 /* check for <product>:<serial> */
62 colon = strchr(arg, ':');
64 product = strndup(arg, colon - arg);
65 serial = strtol(colon + 1, &end, 0);
77 if (!product && default_product)
78 ret = match_dev(default_product, serial, device);
80 ret = match_dev(product, serial, device);
81 if (product && product != arg)
96 cc_fullname (char *dir, char *file)
99 int dlen = strlen (dir);
100 int flen = strlen (file);
103 if (dir[dlen-1] != '/')
105 new = malloc (dlen + slen + flen + 1);
116 cc_basename(char *file)
120 b = strrchr(file, '/');
127 load_string(char *dir, char *file)
129 char *full = cc_fullname(dir, file);
135 f = fopen(full, "r");
139 r = fgets(line, sizeof (line), f);
144 if (r[rlen-1] == '\n')
150 load_hex(char *dir, char *file)
156 line = load_string(dir, file);
159 i = strtol(line, &end, 16);
167 load_dec(char *dir, char *file)
173 line = load_string(dir, file);
176 i = strtol(line, &end, 10);
184 dir_filter_tty_colon(const struct dirent *d)
186 return strncmp(d->d_name, "tty:", 4) == 0;
190 dir_filter_tty(const struct dirent *d)
192 return strncmp(d->d_name, "tty", 3) == 0;
195 struct altos_usbdev {
200 int serial; /* AltOS always uses simple integer serial numbers */
211 struct dirent **namelist;
214 char endpoint_base[20];
220 base = cc_basename(sys);
221 num_configs = load_hex(sys, "bNumConfigurations");
222 num_interfaces = load_hex(sys, "bNumInterfaces");
223 for (config = 1; config <= num_configs; config++) {
224 for (interface = 0; interface < num_interfaces; interface++) {
225 sprintf(endpoint_base, "%s:%d.%d",
226 base, config, interface);
227 endpoint_full = cc_fullname(sys, endpoint_base);
229 /* Check for tty:ttyACMx style names
231 ntty = scandir(endpoint_full, &namelist,
232 dir_filter_tty_colon,
236 tty = cc_fullname("/dev", namelist[0]->d_name + 4);
241 /* Check for tty/ttyACMx style names
243 tty_dir = cc_fullname(endpoint_full, "tty");
245 ntty = scandir(tty_dir, &namelist,
250 tty = cc_fullname("/dev", namelist[0]->d_name);
259 static struct altos_usbdev *
260 usb_scan_device(char *sys)
262 struct altos_usbdev *usbdev;
264 usbdev = calloc(1, sizeof (struct altos_usbdev));
267 usbdev->sys = strdup(sys);
268 usbdev->manufacturer = load_string(sys, "manufacturer");
269 usbdev->product = load_string(sys, "product");
270 usbdev->serial = load_dec(sys, "serial");
271 usbdev->idProduct = load_hex(sys, "idProduct");
272 usbdev->idVendor = load_hex(sys, "idVendor");
273 usbdev->tty = usb_tty(sys);
278 usbdev_free(struct altos_usbdev *usbdev)
281 free(usbdev->manufacturer);
282 free(usbdev->product);
283 /* this can get used as a return value */
289 #define USB_DEVICES "/sys/bus/usb/devices"
292 dir_filter_dev(const struct dirent *d)
294 const char *n = d->d_name;
302 if (c == '.' && n != d->d_name + 1)
310 struct altos_usbdev **dev;
327 altos_list_start(void)
330 struct dirent **ents;
332 struct altos_usbdev *dev;
333 struct altos_list *devs;
336 devs = calloc(1, sizeof (struct altos_list));
340 n = scandir (USB_DEVICES, &ents,
345 for (e = 0; e < n; e++) {
346 dir = cc_fullname(USB_DEVICES, ents[e]->d_name);
347 dev = usb_scan_device(dir);
349 if (dev->idVendor == 0xfffe && dev->tty) {
351 devs->dev = realloc(devs->dev,
352 devs->ndev + 1 * sizeof (struct usbdev *));
354 devs->dev = malloc (sizeof (struct usbdev *));
355 devs->dev[devs->ndev++] = dev;
364 altos_list_next(struct altos_list *list, struct altos_device *device)
366 struct altos_usbdev *dev;
367 if (list->current >= list->ndev)
369 dev = list->dev[list->current];
370 strcpy(device->product, dev->product);
371 strcpy(device->path, dev->tty);
372 device->serial = dev->serial;
378 altos_list_finish(struct altos_list *usbdevs)
384 for (i = 0; i < usbdevs->ndev; i++)
385 usbdev_free(usbdevs->dev[i]);
393 #include <IOKitLib.h>
394 #include <IOKit/usb/USBspec.h>
395 #include <sys/param.h>
397 #include <CFNumber.h>
404 io_iterator_t iterator;
408 get_string(io_object_t object, CFStringRef entry, char *result, int result_len)
410 CFTypeRef entry_as_string;
413 entry_as_string = IORegistryEntrySearchCFProperty (object,
417 kIORegistryIterateRecursively);
418 if (entry_as_string) {
419 got_string = CFStringGetCString(entry_as_string,
421 kCFStringEncodingASCII);
423 CFRelease(entry_as_string);
442 altos_list_start(void)
444 struct altos_list *list = calloc (sizeof (struct altos_list), 1);
445 CFMutableDictionaryRef matching_dictionary = IOServiceMatching("IOUSBDevice");
446 UInt32 vendor = 0xfffe, product = 0x000a;
447 CFNumberRef vendor_ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendor);
448 CFNumberRef product_ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &product);
449 io_iterator_t tdIterator;
450 io_object_t tdObject;
452 CFDictionaryAddValue(matching_dictionary, CFSTR(kUSBVendorID), vendor_ref);
453 CFDictionaryAddValue(matching_dictionary, CFSTR(kUSBProductID), product_ref);
455 IOServiceGetMatchingServices(kIOMasterPortDefault, matching_dictionary, &list->iterator);
457 CFRelease(vendor_ref);
458 CFRelease(product_ref);
463 altos_list_next(struct altos_list *list, struct altos_device *device)
466 char serial_string[128];
469 object = IOIteratorNext(list->iterator);
473 if (get_string (object, CFSTR("IOCalloutDevice"), device->path, sizeof (device->path)) &&
474 get_string (object, CFSTR("USB Product Name"), device->product, sizeof (device->product)) &&
475 get_string (object, CFSTR("USB Serial Number"), serial_string, sizeof (serial_string))) {
476 device->serial = atoi(serial_string);
483 altos_list_finish(struct altos_list *list)
485 IOObjectRelease (list->iterator);
499 #define USB_BUF_SIZE 64
503 unsigned char out_data[USB_BUF_SIZE];
505 unsigned char in_data[USB_BUF_SIZE];
511 altos_open(struct altos_device *device)
513 struct altos_file *file = calloc (sizeof (struct altos_file), 1);
520 file->fd = open(device->path, O_RDWR | O_NOCTTY);
522 perror(device->path);
526 ret = tcgetattr(file->fd, &term);
535 term.c_cc[VTIME] = 1;
536 ret = tcsetattr(file->fd, TCSAFLUSH, &term);
547 altos_close(struct altos_file *file)
554 altos_putchar(struct altos_file *file, char c)
558 if (file->out_used == USB_BUF_SIZE) {
559 ret = altos_flush(file);
563 file->out_data[file->out_used++] = c;
564 if (file->out_used == USB_BUF_SIZE)
565 return altos_flush(file);
570 altos_flush(struct altos_file *file)
572 while (file->out_used) {
575 ret = write (file->fd, file->out_data, file->out_used);
579 memmove(file->out_data, file->out_data + ret,
580 file->out_used - ret);
581 file->out_used -= ret;
587 altos_getchar(struct altos_file *file, int timeout)
589 while (file->in_read == file->in_used) {
593 ret = read(file->fd, file->in_data, USB_BUF_SIZE);
599 return file->in_data[file->in_read++];
602 #endif /* POSIX_TTY */
610 libusb_context *usb_context;
615 ret = libusb_init(&usb_context);
618 libusb_set_debug(usb_context, 3);
622 void altos_fini(void)
624 libusb_exit(usb_context);
628 static libusb_device **list;
629 static ssize_t num, current;
631 int altos_list_start(void)
636 num = libusb_get_device_list(usb_context, &list);
645 int altos_list_next(struct altos_device *device)
647 while (current < num) {
648 struct libusb_device_descriptor descriptor;
649 libusb_device *usb_device = list[current++];
651 if (libusb_get_device_descriptor(usb_device, &descriptor) == 0) {
652 if (descriptor.idVendor == 0xfffe)
654 libusb_device_handle *handle;
655 if (libusb_open(usb_device, &handle) == 0) {
656 char serial_number[256];
657 libusb_get_string_descriptor_ascii(handle, descriptor.iProduct,
659 sizeof(device->product));
660 libusb_get_string_descriptor_ascii(handle, descriptor.iSerialNumber,
662 sizeof (serial_number));
663 libusb_close(handle);
664 device->serial = atoi(serial_number);
665 device->device = usb_device;
674 void altos_list_finish(void)
677 libusb_free_device_list(list, 1);
682 #define USB_BUF_SIZE 64
685 struct libusb_device *device;
686 struct libusb_device_handle *handle;
691 unsigned char out_data[USB_BUF_SIZE];
693 unsigned char in_data[USB_BUF_SIZE];
699 altos_open(struct altos_device *device)
701 struct altos_file *file;
702 struct libusb_device_handle *handle;
703 if (libusb_open(device->device, &handle) == 0) {
706 ret = libusb_claim_interface(handle, 1);
709 libusb_close(handle);
713 ret = libusb_detach_kernel_driver(handle, 1);
716 libusb_close(handle);
721 file = calloc(sizeof (struct altos_file), 1);
722 file->device = libusb_ref_device(device->device);
723 file->handle = handle;
724 /* XXX should get these from the endpoint descriptors */
725 file->out_ep = 4 | LIBUSB_ENDPOINT_OUT;
727 file->in_ep = 5 | LIBUSB_ENDPOINT_IN;
736 altos_close(struct altos_file *file)
738 libusb_close(file->handle);
739 libusb_unref_device(file->device);
745 altos_putchar(struct altos_file *file, char c)
749 if (file->out_used == file->out_size) {
750 ret = altos_flush(file);
754 file->out_data[file->out_used++] = c;
755 if (file->out_used == file->out_size)
756 return altos_flush(file);
761 altos_flush(struct altos_file *file)
763 while (file->out_used) {
767 ret = libusb_bulk_transfer(file->handle,
776 memmove(file->out_data, file->out_data + transferred,
777 file->out_used - transferred);
778 file->out_used -= transferred;
784 altos_getchar(struct altos_file *file, int timeout)
786 while (file->in_read == file->in_used) {
791 ret = libusb_bulk_transfer(file->handle,
796 (unsigned int) timeout);
800 file->in_used = transferred;
802 return file->in_data[file->in_read++];
805 #endif /* USE_LIBUSB */