static int usbdk_get_device_list(struct libusb_context *ctx, struct discovered_devs **_discdevs) { int r = LIBUSB_SUCCESS; ULONG i; struct discovered_devs *discdevs = NULL; ULONG dev_number; PUSB_DK_DEVICE_INFO devices; if(!usbdk_helper.GetDevicesList(&devices, &dev_number)) return LIBUSB_ERROR_OTHER; for (i = 0; i < dev_number; i++) { unsigned long session_id; struct libusb_device *dev = NULL; if (usbdk_get_session_id_for_device(ctx, &devices[i].ID, &session_id)) continue; dev = usbi_get_device_by_session_id(ctx, session_id); if (dev == NULL) { dev = usbi_alloc_device(ctx, session_id); if (dev == NULL) { usbi_err(ctx, "failed to allocate a new device structure"); continue; } usbdk_device_init(dev, &devices[i]); if (usbdk_device_priv_init(ctx, dev, &devices[i]) != LIBUSB_SUCCESS) { libusb_unref_device(dev); continue; } } discdevs = discovered_devs_append(*_discdevs, dev); libusb_unref_device(dev); if (!discdevs) { usbi_err(ctx, "cannot append new device to list"); r = LIBUSB_ERROR_NO_MEM; goto func_exit; } *_discdevs = discdevs; } func_exit: usbdk_helper.ReleaseDevicesList(devices); return r; }
int obsd_get_device_list(struct libusb_context * ctx, struct discovered_devs **discdevs) { struct libusb_device *dev; struct device_priv *dpriv; struct usb_device_info di; unsigned long session_id; char devnode[16]; int fd, err, i; usbi_dbg(""); for (i = 0; i < USB_MAX_DEVICES; i++) { snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i); if ((fd = open(devnode, O_RDONLY)) < 0) { if (errno != ENOENT && errno != ENXIO) usbi_err(ctx, "could not open %s", devnode); continue; } if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0) continue; session_id = (di.udi_bus << 8 | di.udi_addr); dev = usbi_get_device_by_session_id(ctx, session_id); if (dev == NULL) { dev = usbi_alloc_device(ctx, session_id); if (dev == NULL) return (LIBUSB_ERROR_NO_MEM); dev->bus_number = di.udi_bus; dev->device_address = di.udi_addr; dev->speed = di.udi_speed; dpriv = (struct device_priv *)dev->os_priv; strlcpy(dpriv->devnode, devnode, sizeof(devnode)); dpriv->fd = -1; if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) { err = errno; goto error; } dpriv->cdesc = NULL; if (_cache_active_config_descriptor(dev, fd)) { err = errno; goto error; } if ((err = usbi_sanitize_device(dev))) goto error; } close(fd); if (discovered_devs_append(*discdevs, dev) == NULL) return (LIBUSB_ERROR_NO_MEM); } return (LIBUSB_SUCCESS); error: close(fd); libusb_unref_device(dev); return _errno_to_libusb(err); }
static int sunos_add_devices(di_devlink_t link, void *arg) { struct devlink_cbarg *largs = (struct devlink_cbarg *)arg; struct node_args *nargs; di_node_t myself, pnode; uint64_t session_id = 0; uint16_t bdf = 0; struct libusb_device *dev; sunos_dev_priv_t *devpriv; const char *path, *newpath; int n, i; int *addr_prop; uint8_t bus_number = 0; nargs = (struct node_args *)largs->nargs; myself = largs->myself; if (nargs->last_ugenpath) { /* the same node's links */ return (DI_WALK_CONTINUE); } /* * Construct session ID. * session ID = ...parent hub addr|hub addr|dev addr. */ pnode = myself; i = 0; while (pnode != DI_NODE_NIL) { if (di_prop_exists(DDI_DEV_T_ANY, pnode, "root-hub") == 1) { /* walk to root */ uint32_t *regbuf = NULL; uint32_t reg; n = di_prop_lookup_ints(DDI_DEV_T_ANY, pnode, "reg", (int **)®buf); reg = regbuf[0]; bdf = (PCI_REG_BUS_G(reg) << 8) | (PCI_REG_DEV_G(reg) << 3) | PCI_REG_FUNC_G(reg); session_id |= (bdf << i * 8); /* same as 'unit-address' property */ bus_number = (PCI_REG_DEV_G(reg) << 3) | PCI_REG_FUNC_G(reg); usbi_dbg("device bus address=%s:%x", di_bus_addr(pnode), bus_number); break; } /* usb_addr */ n = di_prop_lookup_ints(DDI_DEV_T_ANY, pnode, "assigned-address", &addr_prop); if ((n != 1) || (addr_prop[0] == 0)) { usbi_dbg("cannot get valid usb_addr"); return (DI_WALK_CONTINUE); } session_id |= ((addr_prop[0] & 0xff) << i * 8); if (++i > 7) break; pnode = di_parent_node(pnode); } path = di_devlink_path(link); dev = usbi_get_device_by_session_id(nargs->ctx, session_id); if (dev == NULL) { dev = usbi_alloc_device(nargs->ctx, session_id); if (dev == NULL) { usbi_dbg("can't alloc device"); return (DI_WALK_TERMINATE); } devpriv = (sunos_dev_priv_t *)dev->os_priv; if ((newpath = strrchr(path, '/')) == NULL) { libusb_unref_device(dev); return (DI_WALK_TERMINATE); } devpriv->ugenpath = strndup(path, strlen(path) - strlen(newpath)); dev->bus_number = bus_number; if (sunos_fill_in_dev_info(myself, dev) != LIBUSB_SUCCESS) { libusb_unref_device(dev); return (DI_WALK_TERMINATE); } if (usbi_sanitize_device(dev) < 0) { libusb_unref_device(dev); usbi_dbg("sanatize failed: "); return (DI_WALK_TERMINATE); } } else { usbi_dbg("Dev %s exists", path); } devpriv = (sunos_dev_priv_t *)dev->os_priv; if (nargs->last_ugenpath == NULL) { /* first device */ nargs->last_ugenpath = devpriv->ugenpath; if (discovered_devs_append(*(nargs->discdevs), dev) == NULL) { usbi_dbg("cannot append device"); } /* * we alloc and hence ref this dev. We don't need to ref it * hereafter. Front end or app should take care of their ref. */ libusb_unref_device(dev); } usbi_dbg("Device %s %s id=0x%llx, devcount:%d, bdf=%x", devpriv->ugenpath, path, (uint64_t)session_id, (*nargs->discdevs)->len, bdf); return (DI_WALK_CONTINUE); }
int obsd_get_device_list(struct libusb_context * ctx, struct discovered_devs **discdevs) { struct discovered_devs *ddd; struct libusb_device *dev; struct device_priv *dpriv; struct usb_device_info di; struct usb_device_ddesc dd; unsigned long session_id; char devices[USB_MAX_DEVICES]; char busnode[16]; char *udevname; int fd, addr, i, j; usbi_dbg(""); for (i = 0; i < 8; i++) { snprintf(busnode, sizeof(busnode), USBDEV "%d", i); if ((fd = open(busnode, O_RDWR)) < 0) { if (errno != ENOENT && errno != ENXIO) usbi_err(ctx, "could not open %s", busnode); continue; } bzero(devices, sizeof(devices)); for (addr = 1; addr < USB_MAX_DEVICES; addr++) { if (devices[addr]) continue; di.udi_addr = addr; if (ioctl(fd, USB_DEVICEINFO, &di) < 0) continue; /* * XXX If ugen(4) is attached to the USB device * it will be used. */ udevname = NULL; for (j = 0; j < USB_MAX_DEVNAMES; j++) if (!strncmp("ugen", di.udi_devnames[j], 4)) { udevname = strdup(di.udi_devnames[j]); break; } session_id = (di.udi_bus << 8 | di.udi_addr); dev = usbi_get_device_by_session_id(ctx, session_id); if (dev == NULL) { dev = usbi_alloc_device(ctx, session_id); if (dev == NULL) { close(fd); return (LIBUSB_ERROR_NO_MEM); } dev->bus_number = di.udi_bus; dev->device_address = di.udi_addr; dev->speed = di.udi_speed; dpriv = (struct device_priv *)dev->os_priv; dpriv->fd = -1; dpriv->cdesc = NULL; dpriv->devname = udevname; dd.udd_bus = di.udi_bus; dd.udd_addr = di.udi_addr; if (ioctl(fd, USB_DEVICE_GET_DDESC, &dd) < 0) { libusb_unref_device(dev); continue; } dpriv->ddesc = dd.udd_desc; if (_cache_active_config_descriptor(dev)) { libusb_unref_device(dev); continue; } if (usbi_sanitize_device(dev)) { libusb_unref_device(dev); continue; } } ddd = discovered_devs_append(*discdevs, dev); if (ddd == NULL) { close(fd); return (LIBUSB_ERROR_NO_MEM); } libusb_unref_device(dev); *discdevs = ddd; devices[addr] = 1; } close(fd); } return (LIBUSB_SUCCESS); }
static int wince_get_device_list( struct libusb_context *ctx, struct discovered_devs **discdevs) { UKW_DEVICE devices[MAX_DEVICE_COUNT]; struct discovered_devs * new_devices = *discdevs; DWORD count = 0, i; struct libusb_device *dev = NULL; unsigned char bus_addr, dev_addr; unsigned long session_id; BOOL success; DWORD release_list_offset = 0; int r = LIBUSB_SUCCESS; success = UkwGetDeviceList(driver_handle, devices, MAX_DEVICE_COUNT, &count); if (!success) { int libusbErr = translate_driver_error(GetLastError()); usbi_err(ctx, "could not get devices: %s", windows_error_str(0)); return libusbErr; } for(i = 0; i < count; ++i) { release_list_offset = i; success = UkwGetDeviceAddress(devices[i], &bus_addr, &dev_addr, &session_id); if (!success) { r = translate_driver_error(GetLastError()); usbi_err(ctx, "could not get device address for %d: %s", i, windows_error_str(0)); goto err_out; } dev = usbi_get_device_by_session_id(ctx, session_id); if (dev) { usbi_dbg("using existing device for %d/%d (session %ld)", bus_addr, dev_addr, session_id); libusb_ref_device(dev); // Release just this element in the device list (as we already hold a // reference to it). UkwReleaseDeviceList(driver_handle, &devices[i], 1); release_list_offset++; } else { usbi_dbg("allocating new device for %d/%d (session %ld)", bus_addr, dev_addr, session_id); dev = usbi_alloc_device(ctx, session_id); if (!dev) { r = LIBUSB_ERROR_NO_MEM; goto err_out; } r = init_device(dev, devices[i], bus_addr, dev_addr); if (r < 0) goto err_out; r = usbi_sanitize_device(dev); if (r < 0) goto err_out; } new_devices = discovered_devs_append(new_devices, dev); if (!discdevs) { r = LIBUSB_ERROR_NO_MEM; goto err_out; } safe_unref_device(dev); } *discdevs = new_devices; return r; err_out: *discdevs = new_devices; safe_unref_device(dev); // Release the remainder of the unprocessed device list. // The devices added to new_devices already will still be passed up to libusb, // which can dispose of them at its leisure. UkwReleaseDeviceList(driver_handle, &devices[release_list_offset], count - release_list_offset); return r; }
WatchedEntry::WatchedEntry(BMessenger *messenger, entry_ref *ref) : fMessenger(messenger), fIsDirectory(false), fDevice(NULL), fEntries(NULL), fLink(NULL), fInitCheck(false) { BEntry entry(ref); entry.GetNodeRef(&fNode); BDirectory directory; if (entry.IsDirectory() && directory.SetTo(ref) >= B_OK) { fIsDirectory = true; while (directory.GetNextEntry(&entry) >= B_OK) { if (entry.GetRef(ref) < B_OK) continue; WatchedEntry *child = new(std::nothrow) WatchedEntry(fMessenger, ref); if (child == NULL) continue; if (child->InitCheck() == false) { delete child; continue; } child->fLink = fEntries; fEntries = child; } watch_node(&fNode, B_WATCH_DIRECTORY, *fMessenger); } else { if (strncmp(ref->name, "raw", 3) == 0) return; BPath path, parent_path; entry.GetPath(&path); fDevice = new(std::nothrow) USBDevice(path.Path()); if (fDevice != NULL && fDevice->InitCheck() == true) { // Add this new device to each active context's device list struct libusb_context *ctx; unsigned long session_id = (unsigned long)&fDevice; usbi_mutex_lock(&active_contexts_lock); list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) { struct libusb_device *dev = usbi_get_device_by_session_id(ctx, session_id); if (dev) { usbi_dbg("using previously allocated device with location %lu", session_id); libusb_unref_device(dev); continue; } usbi_dbg("allocating new device with location %lu", session_id); dev = usbi_alloc_device(ctx, session_id); if (!dev) { usbi_dbg("device allocation failed"); continue; } *((USBDevice **)dev->os_priv) = fDevice; // Calculate pseudo-device-address int addr, tmp; if (strcmp(path.Leaf(), "hub") == 0) tmp = 100; //Random Number else sscanf(path.Leaf(), "%d", &tmp); addr = tmp + 1; path.GetParent(&parent_path); while (strcmp(parent_path.Leaf(), "usb") != 0) { sscanf(parent_path.Leaf(), "%d", &tmp); addr += tmp + 1; parent_path.GetParent(&parent_path); } sscanf(path.Path(), "/dev/bus/usb/%d", &dev->bus_number); dev->device_address = addr - (dev->bus_number + 1); if (usbi_sanitize_device(dev) < 0) { usbi_dbg("device sanitization failed"); libusb_unref_device(dev); continue; } usbi_connect_device(dev); } usbi_mutex_unlock(&active_contexts_lock); } else if (fDevice) { delete fDevice; fDevice = NULL; return; } }