Example #1
1
ftdi_device_t* ftdi_context_match_name(const ftdi_context_t* context,
    const char* name) {
  struct stat stat_buffer;
  struct udev* udev = 0;
  struct udev_device* dev = 0;
  int bus = 0;
  int address = 0;
  int i;
  
  if (!stat(name, &stat_buffer) && S_ISCHR(stat_buffer.st_mode)) {
    udev = udev_new();
    dev = udev_device_new_from_devnum(udev, 'c', stat_buffer.st_rdev);

    if (dev) {
      string_scanf(udev_device_get_sysattr_value(dev, "busnum"),
        "%d", &bus);
      string_scanf(udev_device_get_sysattr_value(dev, "devnum"),
        "%d", &address);
    }

    udev_unref(udev);
  }

  for (i = 0; i < context->num_devices; ++i) {
    if ((context->devices[i].bus == bus) &&
        (context->devices[i].address == address))
      return &context->devices[i];
  }
  
  return 0;
}
Example #2
0
File: usb.c Project: seec/TL866
int get_device_count()
{
    struct udev *udev = udev_new();
    if (!udev)
    {
        return -1;
    }

    struct udev_enumerate *enumerate;
    struct udev_list_entry *devices, *dev_list_entry;
    struct udev_device *dev;

    enumerate = udev_enumerate_new(udev);
    udev_enumerate_add_match_subsystem(enumerate, "usb");
    udev_enumerate_scan_devices(enumerate);
    devices = udev_enumerate_get_list_entry(enumerate);
    const char *path;
    int count = 0;
    udev_list_entry_foreach(dev_list_entry, devices)
    {

        path = udev_list_entry_get_name(dev_list_entry);
        dev = udev_device_new_from_syspath(udev, path);
        if(!dev)
            return -1;

        const char * vid = udev_device_get_sysattr_value(dev,"idVendor");
        const char * pid = udev_device_get_sysattr_value(dev,"idProduct");
        if((vid && pid) && (!strcmp(vid, "04d8") && (!strcmp(pid, "e11c"))))
            count++;
        udev_device_unref(dev);
    }
Example #3
0
static int names_mac(struct udev_device *dev, struct netnames *names) {
        const char *s;
        unsigned int i;
        unsigned int a1, a2, a3, a4, a5, a6;

        /* check for NET_ADDR_PERM, skip random MAC addresses */
        s = udev_device_get_sysattr_value(dev, "addr_assign_type");
        if (!s)
                return EXIT_FAILURE;
        i = strtoul(s, NULL, 0);
        if (i != 0)
                return 0;

        s = udev_device_get_sysattr_value(dev, "address");
        if (!s)
                return -ENOENT;
        if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
                return -EINVAL;

        /* skip empty MAC addresses */
        if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
                return -EINVAL;

        names->mac[0] = a1;
        names->mac[1] = a2;
        names->mac[2] = a3;
        names->mac[3] = a4;
        names->mac[4] = a5;
        names->mac[5] = a6;
        names->mac_valid = true;
        return 0;
}
Example #4
0
static int find_device(struct udev_device *udev_dev)
{
	char path[PATH_MAX];
	const char *busnum, *devnum;
	int fd;

	busnum = udev_device_get_sysattr_value(udev_dev, "busnum");
	if (!busnum)
		return -1;

	devnum = udev_device_get_sysattr_value(udev_dev, "devnum");
	if (!devnum)
		return -1;

	snprintf(path, sizeof(path), "/dev/bus/usb/%s/%s", busnum, devnum);

	fd = open(path, O_RDWR, O_CLOEXEC);
	if (fd < 0) {
		fprintf(stderr, "Can't open device: %s (%d)\n",
						strerror(errno), errno);
		return -1;
	}

	return fd;
}
Example #5
0
QUdevDeviceList QUdevPrivate::getUDevDevicesForSubsystem(const QString &strSubSystem, const QString &strDeviceType, const QString &strParentSubSystem, const QString &strParentDeviceType)
{
    struct udev_enumerate *enumerate = udev_enumerate_new(m_pUdev);
    struct udev_list_entry *devices = 0;
    struct udev_list_entry *dev_list_entry = 0;
    struct udev_device *dev = 0;

    QList<QUdevDevice> lDevices;

    if(strSubSystem.isEmpty()) return lDevices;

    //get subsystem enumerator
    udev_enumerate_add_match_subsystem(enumerate, strSubSystem.toLatin1().constData());
    //perform sysfs scanning
    udev_enumerate_scan_devices(enumerate);
    devices = udev_enumerate_get_list_entry(enumerate);

    //iterate over all devices in the enumeration list
    udev_list_entry_foreach(dev_list_entry, devices)
    {
        QUdevDevice udDev;

        //create udev device for the sysfs path returned
        udDev.m_strSysfsPath = QString::fromLatin1(udev_list_entry_get_name(dev_list_entry));        
        dev = udev_device_new_from_syspath(m_pUdev, udDev.m_strSysfsPath.toLatin1().constData());

        //filter the correct device types, ignored if empty device type is specified
        if(strDeviceType.isEmpty() || (QString::fromLatin1(udev_device_get_devtype(dev)) == strDeviceType))
        {
            //get the path inside /dev
            udDev.m_strDevPath = QString::fromLatin1(udev_device_get_devnode(dev));
            udDev.m_strSubsystem = strSubSystem;
            udDev.m_strDeviceType = strDeviceType;

            //if the caller wants a specific parent subsystem/devtype query the sysfs tree here
            if(!strParentSubSystem.isEmpty() && !strParentDeviceType.isEmpty())
            {
                /*
                 * retrieve the parent device with the subsystem/devtype pair of m_strParentSubSystem/m_strParentDeviceType.
                 *
                 * udev_device_get_parent_with_subsystem_devtype() will walk up the complete tree if needed
                 */
                dev = udev_device_get_parent_with_subsystem_devtype(dev, strParentSubSystem.toLatin1().constData(), strParentDeviceType.toLatin1().constData());
            }

            if(dev)
            {
                //fill the device information
                udDev.m_strVendorID = QString::fromLatin1(udev_device_get_sysattr_value(dev,"idVendor"));
                udDev.m_strProductID = QString::fromLatin1(udev_device_get_sysattr_value(dev, "idProduct"));
                udDev.m_strManufacturer = QString::fromLatin1(udev_device_get_sysattr_value(dev,"manufacturer"));
                udDev.m_strProduct = QString::fromLatin1(udev_device_get_sysattr_value(dev,"product"));
                udDev.m_strSerial = QString::fromLatin1(udev_device_get_sysattr_value(dev, "serial"));
                lDevices.append(udDev);
            }

            udev_device_unref(dev);
        }
    }
Example #6
0
  LinuxDevice::LinuxDevice(struct udev_device* dev)
  {
    log->debug("Creating a new LinuxDevice instance");
    const char *name = udev_device_get_sysattr_value(dev, "product");
    if (name) {
      log->debug("DeviceName={}", name);
      setDeviceName(name);
    }
    
    const char *id_vendor = udev_device_get_sysattr_value(dev, "idVendor");
    if (id_vendor) {
      log->debug("VendorID={}", id_vendor);
      setVendorID(id_vendor);
    }

    const char *id_product = udev_device_get_sysattr_value(dev, "idProduct");
    if (id_product) {
      log->debug("ProductID={}", id_product);
      setProductID(id_product);
    }

    const char *serial = udev_device_get_sysattr_value(dev, "serial");
    if (serial) {
      log->debug("Serial={}", serial);
      setSerialNumber(serial);
    }

    const char *syspath = udev_device_get_syspath(dev);
    if (syspath) {
      log->debug("Syspath={}", syspath);
      _syspath = syspath;
    } else {
      throw std::runtime_error("device wihtout syspath");
    }

    const char *sysname = udev_device_get_sysname(dev);
    if (sysname) {
      log->debug("Sysname={}", sysname);
      setDevicePort(sysname);
    } else {
      throw std::runtime_error("device wihtout sysname");
    }

    log->debug("DeviceHash={}", getDeviceHash());

    setTarget(Rule::Target::Unknown);

    std::ifstream descriptor_stream(_syspath + "/descriptors", std::ifstream::binary);
    if (!descriptor_stream.good()) {
      throw std::runtime_error("cannot load USB descriptors");
    }
    else {
      readDescriptors(descriptor_stream);
    }

    return;
  }
  /* 
     This implementation uses udev to retrieve capture devices.
     We support both udev and default v4l2 ways to retrieve 
     devices. Udev is not supported on all systems.

   */
  std::vector<V4L2_Device> v4l2_get_devices() {
    
    std::vector<V4L2_Device> result;
    struct udev* udev;
    struct udev_enumerate* enumerate;
    struct udev_list_entry* devices;
    struct udev_list_entry* dev_list_entry;
    struct udev_device* dev;
 
    udev = udev_new();
    if(!udev) {
      printf("Error: Cannot udev_new()\n");
      return result;
    }
 
    enumerate = udev_enumerate_new(udev);
    udev_enumerate_add_match_subsystem(enumerate, "video4linux");
    udev_enumerate_scan_devices(enumerate);
    devices = udev_enumerate_get_list_entry(enumerate);

    udev_list_entry_foreach(dev_list_entry, devices) {

      /* Get the device by syspath. */
      const char* syspath = udev_list_entry_get_name(dev_list_entry);
      dev = udev_device_new_from_syspath(udev, syspath);

      if(!dev) {
        printf("Error: cannot get the device using the syspath: %s\n", syspath);
        continue;
      }

      V4L2_Device v4l2_device;
      v4l2_device.path = udev_device_get_devnode(dev);

      if(v4l2_device.path.size() == 0) {
        printf("Error: Cannot find devpath.\n");
        continue;
      }

      dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");

      if(!dev) {
        printf("Error:Cannot find related usb device.\n");
        continue;
      }

      v4l2_device.id_vendor = udev_device_get_sysattr_value(dev, "idVendor");
      v4l2_device.id_product = udev_device_get_sysattr_value(dev, "idProduct");

      result.push_back(v4l2_device);
    }

    udev_enumerate_unref(enumerate);
    udev_unref(udev);
    
    return result;
  }
Example #8
0
static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path)
{
	struct udev *udev  = udev_device_get_udev(parent);
	struct udev_device *transportdev;
	struct udev_device *sessiondev = NULL;
	const char *target;
	char *connname;
	struct udev_device *conndev = NULL;
	const char *addr;
	const char *port;

	/* find iscsi session */
	transportdev = parent;
	while (1) {
		transportdev = udev_device_get_parent(transportdev);
		if (transportdev == NULL)
			return NULL;
		if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0)
			break;
	}
	if (transportdev == NULL)
		return NULL;

	/* find iscsi session device */
	sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
	if (sessiondev == NULL)
		return NULL;
	target = udev_device_get_sysattr_value(sessiondev, "targetname");
	if (target == NULL) {
		parent = NULL;
		goto out;
	}

	if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
		parent = NULL;
		goto out;
	}
	conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
	free(connname);
	if (conndev == NULL) {
		parent = NULL;
		goto out;
	}
	addr = udev_device_get_sysattr_value(conndev, "persistent_address");
	port = udev_device_get_sysattr_value(conndev, "persistent_port");
	if (addr == NULL || port == NULL) {
		parent = NULL;
		goto out;
	}

	path_prepend(path, "ip-%s:%s-iscsi-%s-lun-%s", addr, port, target, udev_device_get_sysnum(parent));
out:
	udev_device_unref(sessiondev);
	udev_device_unref(conndev);
	return parent;
}
Example #9
0
int get_usbinfo(int bus, int dev, usbinfo_t *ui)
{
  struct udev *udev;
  struct udev_enumerate *enumerate;
  struct udev_list_entry *devices, *dev_list_entry;
  struct udev_device *udev_dev;
  char bus_str[16], dev_str[16];
  int found = 0;

  memset(ui, 0, sizeof(usbinfo_t));

  /* construct xenstore dev id */
  if (dev > 0xFFF) {
    xd_log(LOG_ERR, "bad device id %d", dev);
    return -EINVAL;
  }

  ui->usb_virtid = bus << 12 | (dev & 0xFFF);
  ui->usb_bus = bus;
  ui->usb_device = dev;

  /* udev scan */
  udev = udev_new();
  if (!udev) {
    xd_log(LOG_ERR, "Can't create udev");
    return -ENOMEM;
  }
  enumerate = udev_enumerate_new(udev);
  if (!enumerate) {
    xd_log(LOG_ERR, "Can't create enumeration");
    return -ENOMEM;
  }

  snprintf(bus_str, sizeof(bus_str), "%d", bus);
  snprintf(dev_str, sizeof(dev_str), "%d", dev);

  udev_enumerate_add_match_subsystem(enumerate, "usb");
  udev_enumerate_add_match_sysattr(enumerate, "busnum", bus_str);
  udev_enumerate_add_match_sysattr(enumerate, "devnum", dev_str);
  udev_enumerate_scan_devices(enumerate);
  devices = udev_enumerate_get_list_entry(enumerate);
  udev_list_entry_foreach(dev_list_entry, devices) {
    const char *path;
    path = udev_list_entry_get_name(dev_list_entry);
    udev_dev = udev_device_new_from_syspath(udev, path);
    sscanf(udev_device_get_sysattr_value(udev_dev, "idVendor"), "%x", &ui->usb_vendor);
    sscanf(udev_device_get_sysattr_value(udev_dev, "idProduct"), "%x", &ui->usb_product);
    udev_device_unref(udev_dev);
    udev_enumerate_unref(enumerate);
    udev_unref(udev);
    return 0;
  }
  udev_enumerate_unref(enumerate);
  udev_unref(udev);
  return -ENOENT;
}
Example #10
0
/*@[email protected]*/ static struct _driverTracker *get_driver(struct udev_device *dev,
	const char *devpath, struct _driverTracker **classdriver)
{
	int i;
	unsigned int idVendor, idProduct;
	static struct _driverTracker *driver;
	const char *str;

	str = udev_device_get_sysattr_value(dev, "idVendor");
	if (!str)
	{
		Log1(PCSC_LOG_ERROR, "udev_device_get_sysattr_value() failed");
		return NULL;
	}
	sscanf(str, "%X", &idVendor);

	str = udev_device_get_sysattr_value(dev, "idProduct");
	if (!str)
	{
		Log1(PCSC_LOG_ERROR, "udev_device_get_sysattr_value() failed");
		return NULL;
	}
	sscanf(str, "%X", &idProduct);

	Log4(PCSC_LOG_DEBUG,
		"Looking for a driver for VID: 0x%04X, PID: 0x%04X, path: %s",
		idVendor, idProduct, devpath);

	*classdriver = NULL;
	driver = NULL;
	/* check if the device is supported by one driver */
	for (i=0; i<driverSize; i++)
	{
		if (driverTracker[i].libraryPath != NULL &&
			idVendor == driverTracker[i].manuID &&
			idProduct == driverTracker[i].productID)
		{
			if ((driverTracker[i].CFBundleName != NULL)
				&& (0 == strcmp(driverTracker[i].CFBundleName, "CCIDCLASSDRIVER")))
				*classdriver = &driverTracker[i];
			else
				/* it is not a CCID Class driver */
				driver = &driverTracker[i];
		}
	}

	/* if we found a specific driver */
	if (driver)
		return driver;

	/* else return the Class driver (if any) */
	return *classdriver;
}
Example #11
0
static void
do_list_local ()
{
  struct udev *udev;
  struct udev_enumerate *enumerate;
  struct udev_list_entry *devices, *dev_list_entry;
  struct udev_device *dev;
  const gchar *path;
  const gchar *idVendor;
  const gchar *idProduct;
  const gchar *bConfValue;
  const gchar *bNumIntfs;
  const gchar *busid;

  /* Create libudev context. */
  udev = udev_new ();

  /* Create libudev device enumeration. */
  enumerate = udev_enumerate_new (udev);

  /* Take only USB devices that are not hubs and do not have
    * the bInterfaceNumber attribute, i.e. are not interfaces.
    */
  udev_enumerate_add_match_subsystem (enumerate, "usb");
  udev_enumerate_add_nomatch_sysattr (enumerate, "bDeviceClass", "09");
  udev_enumerate_add_nomatch_sysattr (enumerate, "bInterfaceNumber", NULL);
  udev_enumerate_scan_devices (enumerate);

  devices = udev_enumerate_get_list_entry (enumerate);

  /* Show information about each device. */
  udev_list_entry_foreach (dev_list_entry, devices) {
    path = udev_list_entry_get_name (dev_list_entry);
    dev = udev_device_new_from_syspath (udev, path);

    /* Get device information. */
    idVendor = udev_device_get_sysattr_value (dev, "idVendor");
    idProduct = udev_device_get_sysattr_value (dev, "idProduct");
    bConfValue = udev_device_get_sysattr_value (dev, "bConfigurationValue");
    bNumIntfs = udev_device_get_sysattr_value (dev, "bNumInterfaces");
    busid = udev_device_get_sysname (dev);
    if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) {
      g_printerr ("problem getting device attributes: %s\n",
                  g_strerror (errno));
      break;
    }

    g_print (" - busid %s (%.4s:%.4s)\n\n", busid, idVendor, idProduct);

    udev_device_unref (dev);
  }
Example #12
0
// Add a udev device. Returns 0 if device was recognized/added.
static int usb_add_device(struct udev_device* dev){
    const char* vendor = udev_device_get_sysattr_value(dev, "idVendor");
    if(vendor && !strcmp(vendor, V_CORSAIR_STR)){
        const char* product = udev_device_get_sysattr_value(dev, "idProduct");
        if(product){
            for(_model* model = models; model < models + N_MODELS; model++){
                if(!strcmp(product, model->name)){
                    return usbadd(dev, V_CORSAIR, model->number);
                }
            }
        }
    }
    return 1;
}
Example #13
0
/* retrieve on-board index number and label from firmware */
static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
        unsigned dev_port = 0;
        size_t l;
        char *s;
        const char *attr, *port_name;
        int idx;

        /* ACPI _DSM  — device specific method for naming a PCI or PCI Express device */
        attr = udev_device_get_sysattr_value(names->pcidev, "acpi_index");
        /* SMBIOS type 41 — Onboard Devices Extended Information */
        if (!attr)
                attr = udev_device_get_sysattr_value(names->pcidev, "index");
        if (!attr)
                return -ENOENT;

        idx = strtoul(attr, NULL, 0);
        if (idx <= 0)
                return -EINVAL;

        /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
         * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
         * cut-off, which is somewhere beyond the realistic number of physical network interface a system might
         * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
        if (idx > ONBOARD_INDEX_MAX)
                return -ENOENT;

        /* kernel provided port index for multiple ports on a single PCI function */
        attr = udev_device_get_sysattr_value(dev, "dev_port");
        if (attr)
                dev_port = strtol(attr, NULL, 10);

        /* kernel provided front panel port name for multiple port PCI device */
        port_name = udev_device_get_sysattr_value(dev, "phys_port_name");

        s = names->pci_onboard;
        l = sizeof(names->pci_onboard);
        l = strpcpyf(&s, l, "o%d", idx);
        if (port_name)
                l = strpcpyf(&s, l, "n%s", port_name);
        else if (dev_port > 0)
                l = strpcpyf(&s, l, "d%d", dev_port);
        if (l == 0)
                names->pci_onboard[0] = '\0';

        names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label");

        return 0;
}
Example #14
0
/* This function allocates memory from the heap for the property
 * value.  That memory must be later freed by some other code. */
static int udevGetDeviceSysfsAttr(struct udev_device *udev_device,
                                  const char *attr_name,
                                  char **attr_value)
{
    const char *udev_value = NULL;
    int ret = PROPERTY_FOUND;

    udev_value = udev_device_get_sysattr_value(udev_device, attr_name);
    if (udev_value == NULL) {
        VIR_DEBUG("udev reports device '%s' does not have sysfs attr '%s'",
                  udev_device_get_sysname(udev_device), attr_name);
        ret = PROPERTY_MISSING;
        goto out;
    }

    /* If this allocation is changed, the comment at the beginning
     * of the function must also be changed. */
    *attr_value = strdup(udev_value);
    if (*attr_value == NULL) {
        VIR_ERROR(_("Failed to allocate memory for sysfs attribute value for "
                    "sysfs attribute '%s' on device with sysname '%s'"),
                  attr_name, udev_device_get_sysname(udev_device));
        virReportOOMError();
        ret = PROPERTY_ERROR;
        goto out;
    }

    VIR_DEBUG("Found sysfs attribute '%s' value '%s' "
              "for device with sysname '%s'",
              attr_name, *attr_value,
              udev_device_get_sysname(udev_device));

out:
    return ret;
}
static int install_force_release(struct udev_device *dev, const unsigned int *release, unsigned int release_count) {
        struct udev_device *atkbd;
        const char *cur;
        char codes[4096];
        char *s;
        size_t l;
        unsigned int i;
        int ret;

        atkbd = udev_device_get_parent_with_subsystem_devtype(dev, "serio", NULL);
        if (!atkbd)
                return -ENODEV;

        cur = udev_device_get_sysattr_value(atkbd, "force_release");
        if (!cur)
                return -ENODEV;

        s = codes;
        l = sizeof(codes);

        /* copy current content */
        l = strpcpy(&s, l, cur);

        /* append new codes */
        for (i = 0; i < release_count; i++)
                l = strpcpyf(&s, l, ",%d", release[i]);

        log_debug("keyboard: updating force-release list with '%s'", codes);
        ret = udev_device_set_sysattr_value(atkbd, "force_release", codes);
        if (ret < 0)
                log_error("Error writing force-release attribute: %s", strerror(-ret));
        return ret;
}
Example #16
0
static int refresh_imported_device_list(void)
{
	const char *attr_status;
	char status[MAX_STATUS_NAME+1] = "status";
	int i, ret;

	for (i = 0; i < vhci_driver->ncontrollers; i++) {
		if (i > 0)
			snprintf(status, sizeof(status), "status.%d", i);

		attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
							    status);
		if (!attr_status) {
			err("udev_device_get_sysattr_value failed");
			return -1;
		}

		dbg("controller %d", i);

		ret = parse_status(attr_status);
		if (ret != 0)
			return ret;
	}

	return 0;
}
Example #17
0
static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path)
{
	struct udev *udev  = udev_device_get_udev(parent);
	struct udev_device *targetdev;
	struct udev_device *fcdev = NULL;
	const char *port;
	unsigned int lun;

	targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
	if (targetdev == NULL)
		return NULL;

	fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
	if (fcdev == NULL)
		return NULL;
	port = udev_device_get_sysattr_value(fcdev, "port_name");
	if (port == NULL) {
		parent = NULL;
		goto out;
	}

	lun = strtoul(udev_device_get_sysnum(parent), NULL, 10);
	path_prepend(path, "fc-%s:0x%04x%04x00000000", port, lun & 0xffff, (lun >> 16) & 0xffff);
out:
	udev_device_unref(fcdev);
	return parent;
}
Example #18
0
void udevenum(){
    struct udev_enumerate* enumerator = udev_enumerate_new(udev);
    udev_enumerate_add_match_subsystem(enumerator, "usb");
    udev_enumerate_add_match_sysattr(enumerator, "idVendor", V_CORSAIR_STR);
    udev_enumerate_scan_devices(enumerator);
    struct udev_list_entry* devices, *dev_list_entry;
    devices = udev_enumerate_get_list_entry(enumerator);

    udev_list_entry_foreach(dev_list_entry, devices){
        const char* path = udev_list_entry_get_name(dev_list_entry);
        struct udev_device* dev = udev_device_new_from_syspath(udev, path);
        // If the device matches a recognized device ID, open it
        const char* product = udev_device_get_sysattr_value(dev, "idProduct");
        if(!strcmp(product, P_K70_STR)){
            pthread_mutex_lock(&kblistmutex);
            openusb(dev, 70);
            pthread_mutex_unlock(&kblistmutex);
            continue;
        }
        if(!strcmp(product, P_K95_STR)){
            pthread_mutex_lock(&kblistmutex);
            openusb(dev, 95);
            pthread_mutex_unlock(&kblistmutex);
            continue;
        }
        // Free the device if it wasn't used
        udev_device_unref(dev);
    }
    udev_enumerate_unref(enumerator);
}
Example #19
0
/**
 * Walks up the device chain starting at @p syspath,
 * checking each device for @p sysattr, and returns the value if found.
 *
 * @param syspath The /sys/ path of the device to start at, with or without the /sys/
 * @param sysattr The attribute to find
 *
 * @return The stringshared value of @p sysattr if found, or NULL
 */
EAPI const char *
eeze_udev_walk_get_sysattr(const char *syspath,
                           const char *sysattr)
{
   _udev_device *device, *child, *parent;
   const char *test = NULL;

   if (!syspath)
     return NULL;

   if (!(device = _new_device(syspath)))
     return NULL;

   for (parent = device; parent;
        child = parent, parent = udev_device_get_parent(child))
     {
        if ((test = udev_device_get_sysattr_value(parent, sysattr)))
          {
             test = eina_stringshare_add(test);
             udev_device_unref(device);
             return test;
          }
     }

   udev_device_unref(device);
   return NULL;
}
Example #20
0
/**
 * @brief Get the MAC address of a net object
 * @param net The net object
 * @return The MAC address, NULL on failure
 * Use this function to retrieve the non-stringshared MAC address of @p net.
 */
const char *
eeze_net_mac_get(Eeze_Net *net)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(net, NULL);

   return udev_device_get_sysattr_value(net->device, "address");
}
Example #21
0
static gboolean setup_isi_serial(struct modem_info* modem)
{
	struct serial_device_info* info;
	const char *value;

	info = modem->serial;

	if (g_strcmp0(udev_device_get_subsystem(info->dev), "net") != 0)
		return FALSE;

	value = udev_device_get_sysattr_value(info->dev, "type");
	if (g_strcmp0(value, "820") != 0)
		return FALSE;

	/* OK, we want this device to be a modem */
	value = udev_device_get_sysname(info->dev);
	if (value)
		ofono_modem_set_string(modem->modem, "Interface", value);

	value = udev_device_get_property_value(info->dev, "OFONO_ISI_ADDRESS");
	if (value)
		ofono_modem_set_integer(modem->modem, "Address", atoi(value));

	ofono_modem_set_string(modem->modem, "Device", info->devnode);

	return TRUE;
}
Example #22
0
static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test)
{
        struct udev_device *pdev;
        unsigned long bitmask_ev[NBITS(EV_MAX)];
        unsigned long bitmask_abs[NBITS(ABS_MAX)];
        unsigned long bitmask_key[NBITS(KEY_MAX)];
        unsigned long bitmask_rel[NBITS(REL_MAX)];

        /* walk up the parental chain until we find the real input device; the
         * argument is very likely a subdevice of this, like eventN */
        pdev = dev;
        while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL)
                pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL);

        /* not an "input" class device */
        if (pdev == NULL)
                return EXIT_SUCCESS;

        /* Use this as a flag that input devices were detected, so that this
         * program doesn't need to be called more than once per device */
        udev_builtin_add_property(dev, test, "ID_INPUT", "1");
        get_cap_mask(dev, pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test);
        get_cap_mask(dev, pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test);
        get_cap_mask(dev, pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test);
        get_cap_mask(dev, pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test);
        test_pointers(dev, bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel, test);
        test_key(dev, bitmask_ev, bitmask_key, test);
        return EXIT_SUCCESS;
}
Example #23
0
static int get_nports(void)
{
	char *c;
	int nports = 0;
	const char *attr_status;

	attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
					       "status");
	if (!attr_status) {
		err("udev_device_get_sysattr_value failed");
		return -1;
	}

	/* skip a header line */
	c = strchr(attr_status, '\n');
	if (!c)
		return 0;
	c++;

	while (*c != '\0') {
		/* go to the next line */
		c = strchr(c, '\n');
		if (!c)
			return nports;
		c++;
		nports += 1;
	}

	return nports;
}
Example #24
0
int fs_on_read_only(const char *p) {
        struct stat st;
        struct udev *udev = NULL;
        struct udev_device *udev_device = NULL;
        bool b = false;
        const char *read_only;

        assert(p);

        if (stat(p, &st) < 0)
                return -errno;

        if (major(st.st_dev) == 0)
                return false;

        if (!(udev = udev_new()))
                return -ENOMEM;

        if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
                goto finish;

        if ((read_only = udev_device_get_sysattr_value(udev_device, "ro")))
                if ((b = streq(read_only, "1")))
                        goto finish;

finish:
        if (udev_device)
                udev_device_unref(udev_device);

        if (udev)
                udev_unref(udev);

        return b;
}
Example #25
0
/**
 * get_rfkill_device_by_index:
 **/
struct udev_device *
get_rfkill_device_by_index (struct udev *udev,
			    guint        index)
{
	struct udev_enumerate *enumerate;
	struct udev_list_entry *devices;
	struct udev_list_entry *dev_list_entry;
	struct udev_device *dev;

	enumerate = udev_enumerate_new(udev);
	udev_enumerate_add_match_subsystem(enumerate, "rfkill");
	udev_enumerate_scan_devices(enumerate);
	devices = udev_enumerate_get_list_entry(enumerate);

	udev_list_entry_foreach(dev_list_entry, devices) {
		const char *path, *index_c;
		path = udev_list_entry_get_name(dev_list_entry);
		dev = udev_device_new_from_syspath(udev, path);

		index_c = udev_device_get_sysattr_value (dev, "index");
		if (index_c && atoi(index_c) == index)
			break;

		udev_device_unref (dev);
		dev = NULL;
	}

	return dev;
}
Example #26
0
static int _udev_check_pv_min_size(struct device *dev)
{
	struct dev_ext *ext;
	const char *size_str;
	char *endp;
	uint64_t size;

	if (!(ext = dev_ext_get(dev)))
		return_0;

	if (!(size_str = udev_device_get_sysattr_value((struct udev_device *)ext->handle, DEV_EXT_UDEV_SYSFS_ATTR_SIZE))) {
		log_debug_devs("%s: Skipping: failed to get size from sysfs [%s:%p]",
				dev_name(dev), dev_ext_name(dev), dev->ext.handle);
		return 0;
	}

	errno = 0;
	size = strtoull(size_str, &endp, 10);
	if (errno || !endp || *endp) {
		log_debug_devs("%s: Skipping: failed to parse size from sysfs [%s:%p]",
				dev_name(dev), dev_ext_name(dev), dev->ext.handle);
		return 0;
	}

	if (size < pv_min_size()) {
		log_debug_devs("%s: Skipping: %s [%s:%p]", dev_name(dev),
				_too_small_to_hold_pv_msg,
				dev_ext_name(dev), dev->ext.handle);
		return 0;
	}

	return 1;
}
Example #27
0
int fs_on_read_only(const char *p) {
        struct stat st;
        _cleanup_udev_unref_ struct udev *udev = NULL;
        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
        const char *read_only;

        assert(p);

        if (stat(p, &st) < 0)
                return -errno;

        if (major(st.st_dev) == 0)
                return false;

        udev = udev_new();
        if (!udev)
                return -ENOMEM;

        udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
        if (!udev_device)
                return false;

        read_only = udev_device_get_sysattr_value(udev_device, "ro");
        if (read_only)
                return streq(read_only, "1");

        return false;
}
Example #28
0
/**
 * Walks up the device chain starting at @p syspath,
 * checking each device for @p sysattr with (optional) @p value.
 *
 * @param syspath The /sys/ path of the device to start at, with or without the /sys/
 * @param sysattr The attribute to find
 * @param value OPTIONAL: The value that @p sysattr should have, or NULL
 *
 * @return If the sysattr (with value) is found, returns TRUE.  Else, false.
 */
EAPI Eina_Bool
eeze_udev_walk_check_sysattr(const char *syspath,
                             const char *sysattr,
                             const char *value)
{
   _udev_device *device, *child, *parent;
   Eina_Bool ret = EINA_FALSE;
   const char *test = NULL;

   if (!udev)
     return EINA_FALSE;

   if (!(device = _new_device(syspath)))
     return EINA_FALSE;

   for (parent = device; parent;
        child = parent, parent = udev_device_get_parent(child))
     {
        if (!(test = udev_device_get_sysattr_value(parent, sysattr)))
          continue;
        if ((value && (!strcmp(test, value))) || (!value))
          {
             ret = EINA_TRUE;
             break;
          }
     }

   udev_device_unref(device);
   return ret;
}
Example #29
0
static void print_all_attributes(struct udev_device *device, const char *key) {
        struct udev_list_entry *sysattr;

        udev_list_entry_foreach(sysattr, udev_device_get_sysattr_list_entry(device)) {
                const char *name;
                const char *value;
                size_t len;

                name = udev_list_entry_get_name(sysattr);
                if (skip_attribute(name))
                        continue;

                value = udev_device_get_sysattr_value(device, name);
                if (value == NULL)
                        continue;

                /* skip any values that look like a path */
                if (value[0] == '/')
                        continue;

                /* skip nonprintable attributes */
                len = strlen(value);
                while (len > 0 && isprint(value[len-1]))
                        len--;
                if (len > 0)
                        continue;

                printf("    %s{%s}==\"%s\"\n", key, name, value);
        }
        printf("\n");
}
Example #30
0
static virInterfacePtr
udevInterfaceLookupByName(virConnectPtr conn, const char *name)
{
    struct udev_iface_driver *driverState = conn->interfacePrivateData;
    struct udev *udev = udev_ref(driverState->udev);
    struct udev_device *dev;
    const char *macaddr;
    virInterfacePtr ret = NULL;

    /* get a device reference based on the device name */
    dev = udev_device_new_from_subsystem_sysname(udev, "net", name);
    if (!dev) {
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("couldn't find interface named '%s'"),
                       name);
        goto err;
    }

    macaddr = udev_device_get_sysattr_value(dev, "address");
    ret = virGetInterface(conn, name, macaddr);
    udev_device_unref(dev);

err:
    udev_unref(udev);

    return ret;
}