Exemple #1
0
int main(int argc, char *argv[])
{
	struct sysfs_bus *sf_bus;
	struct sysfs_device *sf_dev;
	struct dlist *sf_dev_list = NULL;

	struct sysfs_attribute *sf_attr;
	struct dlist *sf_attr_list = NULL;

	sf_bus = sysfs_open_bus("scsi");

	if (sf_bus != NULL)
	{
		sf_dev_list = sysfs_get_bus_devices(sf_bus);
		if (sf_dev_list != NULL)
		{
			dlist_for_each_data(sf_dev_list, sf_dev, struct sysfs_device)
			{
				printf("%s\n", sf_dev->name);

				sf_attr_list = sysfs_get_device_attributes(sf_dev);
				if (sf_attr_list != NULL)
				{
					dlist_for_each_data(sf_attr_list, sf_attr, struct sysfs_attribute)
					{
						printf("%s\n", sf_attr);
					}
Exemple #2
0
/**
 * extern struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus);
 *
 * flag:
 * 	0 	: bus -> valid
 * 	1 	: bus -> NULL
 */
int  test_sysfs_get_bus_devices(int flag)
{
	struct sysfs_bus *bus = NULL;
	struct dlist *list = NULL;
	char *bus_name = NULL;

	switch (flag) {
	case 0:
		bus_name = val_bus_name;
		bus = sysfs_open_bus(bus_name);
		if (bus == NULL) {
			dbg_print("%s: sysfs_open_bus() failed\n",__FUNCTION__);
			return 0;
		}
		break;
	case 1:
		bus = NULL;
		break;
	default:
		return -1;
	}
	list = sysfs_get_bus_devices(bus);

	switch (flag) {
	case 0:
		if (list == NULL) {
			if (errno == 0)
				dbg_print("%s: No devices registered with bus %s\n",
						__FUNCTION__, bus_name);
			else
				dbg_print("%s: FAILED with flag = %d errno = %d\n",
						__FUNCTION__, flag, errno);
		} else {
			dbg_print("%s: SUCCEEDED with flag = %d\n\n",
					__FUNCTION__, flag);
			show_device_list(list);
			dbg_print("\n");
		}
		break;
	case 1:
		if (list != NULL)
			dbg_print("%s: FAILED with flag = %d errno = %d\n",
						__FUNCTION__, flag, errno);
		else
			dbg_print("%s: SUCCEEDED with flag = %d\n",
					__FUNCTION__, flag);
		break;
	default:
		return 0;
	}
	if (bus != NULL)
		sysfs_close_bus(bus);

	return 0;
}
static int list_devices(bool parsable)
{
	char bus_type[] = "usb";
	char busid[SYSFS_BUS_ID_SIZE];
	char product_name[128];
	struct sysfs_bus *ubus;
	struct sysfs_device *dev;
	struct sysfs_device *intf;
	struct sysfs_attribute *idVendor;
	struct sysfs_attribute *idProduct;
	struct sysfs_attribute *bConfValue;
	struct sysfs_attribute *bNumIntfs;
	struct dlist *devlist;
	int i;
	int ret = -1;

	ubus = sysfs_open_bus(bus_type);
	if (!ubus) {
		err("could not open %s bus: %s", bus_type, strerror(errno));
		return -1;
	}

	devlist = sysfs_get_bus_devices(ubus);
	if (!devlist) {
		err("could not get %s bus devices: %s", bus_type,
		    strerror(errno));
		goto err_out;
	}

	/* remove interfaces and root hubs from device list */
	dlist_filter_sort(devlist, is_device, devcmp);

	if (!parsable) {
		printf("Local USB devices\n");
		printf("=================\n");
	}
	dlist_for_each_data(devlist, dev, struct sysfs_device) {
		idVendor   = sysfs_get_device_attr(dev, "idVendor");
		idProduct  = sysfs_get_device_attr(dev, "idProduct");
		bConfValue = sysfs_get_device_attr(dev, "bConfigurationValue");
		bNumIntfs  = sysfs_get_device_attr(dev, "bNumInterfaces");
		if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) {
			err("problem getting device attributes: %s",
			    strerror(errno));
			goto err_out;
		}

		/* get product name */
		usbip_names_get_product(product_name, sizeof(product_name),
					strtol(idVendor->value, NULL, 16),
					strtol(idProduct->value, NULL, 16));
		print_device(dev->bus_id, idVendor->value, idProduct->value,
			     parsable);
		print_product_name(product_name, parsable);

		for (i = 0; i < atoi(bNumIntfs->value); i++) {
			snprintf(busid, sizeof(busid), "%s:%.1s.%d",
				 dev->bus_id, bConfValue->value, i);
			intf = sysfs_open_device(bus_type, busid);
			if (!intf) {
				err("could not open device interface: %s",
				    strerror(errno));
				goto err_out;
			}
			print_interface(busid, intf->driver_name, parsable);
			sysfs_close_device(intf);
		}
		printf("\n");
	}
Exemple #4
0
void get_usb_devs(hd_data_t *hd_data)
{
    uint64_t ul0;
    unsigned u1, u2, u3;
    hd_t *hd, *hd1;
    usb_t *usb;
    str_list_t *sl, *usb_devs = NULL;
    char *s, *s1, *t;
    hd_res_t *res;
    size_t l;

    struct sysfs_bus *sf_bus;
    struct dlist *sf_dev_list;
    struct sysfs_device *sf_dev;
    struct sysfs_device *sf_dev_2;

    sf_bus = sysfs_open_bus("usb");

    if(!sf_bus) {
        ADD2LOG("sysfs: no such bus: usb\n");
        return;
    }

    sf_dev_list = sysfs_get_bus_devices(sf_bus);

    if(sf_dev_list) dlist_for_each_data(sf_dev_list, sf_dev, struct sysfs_device) {
        if(hd_attr_uint(sysfs_get_device_attr(sf_dev, "bNumInterfaces"), &ul0, 0)) {
            add_str_list(&usb_devs, sf_dev->path);
            ADD2LOG("  usb dev: %s\n", hd_sysfs_id(sf_dev->path));
        }
    }

    if(sf_dev_list) dlist_for_each_data(sf_dev_list, sf_dev, struct sysfs_device) {
        ADD2LOG(
            "  usb device: name = %s, bus_id = %s, bus = %s\n    path = %s\n",
            sf_dev->name,
            sf_dev->bus_id,
            sf_dev->bus,
            hd_sysfs_id(sf_dev->path)
        );

        if(
            hd_attr_uint(sysfs_get_device_attr(sf_dev, "bInterfaceNumber"), &ul0, 16)
        ) {
            hd = add_hd_entry(hd_data, __LINE__, 0);

            hd->detail = new_mem(sizeof *hd->detail);
            hd->detail->type = hd_detail_usb;
            hd->detail->usb.data = usb = new_mem(sizeof *usb);

            hd->sysfs_id = new_str(hd_sysfs_id(sf_dev->path));
            hd->sysfs_bus_id = new_str(sf_dev->bus_id);

            hd->bus.id = bus_usb;
            hd->func = ul0;

            usb->ifdescr = ul0;

            if((s = hd_attr_str(sysfs_get_device_attr(sf_dev, "modalias")))) {
                s = canon_str(s, strlen(s));
                ADD2LOG("    modalias = \"%s\"\n", s);
                if(s && *s) {
                    hd->modalias = s;
                    s = NULL;
                }
                s = free_mem(s);
            }

            ADD2LOG("    bInterfaceNumber = %u\n", hd->func);

            if(hd_attr_uint(sysfs_get_device_attr(sf_dev, "bInterfaceClass"), &ul0, 16)) {
                usb->i_cls = ul0;
                ADD2LOG("    bInterfaceClass = %u\n", usb->i_cls);
            }

            if(hd_attr_uint(sysfs_get_device_attr(sf_dev, "bInterfaceSubClass"), &ul0, 16)) {
                usb->i_sub = ul0;
                ADD2LOG("    bInterfaceSubClass = %u\n", usb->i_sub);
            }

            if(hd_attr_uint(sysfs_get_device_attr(sf_dev, "bInterfaceProtocol"), &ul0, 16)) {
                usb->i_prot = ul0;
                ADD2LOG("    bInterfaceProtocol = %u\n", usb->i_prot);
            }

            /* device has longest matching sysfs id */
            u2 = strlen(sf_dev->path);
            s = NULL;
            for(u3 = 0, sl = usb_devs; sl; sl = sl->next) {
                u1 = strlen(sl->str);
                if(u1 > u3 && u1 <= u2 && !strncmp(sf_dev->path, sl->str, u1)) {
                    u3 = u1;
                    s = sl->str;
                }
            }

            if(s) {
                ADD2LOG("    if: %s @ %s\n", hd->sysfs_bus_id, hd_sysfs_id(s));
                sf_dev_2 = sysfs_open_device_path(s);
                if(sf_dev_2) {

                    if(hd_attr_uint(sysfs_get_device_attr(sf_dev_2, "bDeviceClass"), &ul0, 16)) {
                        usb->d_cls = ul0;
                        ADD2LOG("    bDeviceClass = %u\n", usb->d_cls);
                    }

                    if(hd_attr_uint(sysfs_get_device_attr(sf_dev_2, "bDeviceSubClass"), &ul0, 16)) {
                        usb->d_sub = ul0;
                        ADD2LOG("    bDeviceSubClass = %u\n", usb->d_sub);
                    }

                    if(hd_attr_uint(sysfs_get_device_attr(sf_dev_2, "bDeviceProtocol"), &ul0, 16)) {
                        usb->d_prot = ul0;
                        ADD2LOG("    bDeviceProtocol = %u\n", usb->d_prot);
                    }

                    if(hd_attr_uint(sysfs_get_device_attr(sf_dev_2, "idVendor"), &ul0, 16)) {
                        usb->vendor = ul0;
                        ADD2LOG("    idVendor = 0x%04x\n", usb->vendor);
                    }

                    if(hd_attr_uint(sysfs_get_device_attr(sf_dev_2, "idProduct"), &ul0, 16)) {
                        usb->device = ul0;
                        ADD2LOG("    idProduct = 0x%04x\n", usb->device);
                    }

                    if((s = hd_attr_str(sysfs_get_device_attr(sf_dev_2, "manufacturer")))) {
                        usb->manufact = canon_str(s, strlen(s));
                        ADD2LOG("    manufacturer = \"%s\"\n", usb->manufact);
                    }

                    if((s = hd_attr_str(sysfs_get_device_attr(sf_dev_2, "product")))) {
                        usb->product = canon_str(s, strlen(s));
                        ADD2LOG("    product = \"%s\"\n", usb->product);
                    }

                    if((s = hd_attr_str(sysfs_get_device_attr(sf_dev_2, "serial")))) {
                        usb->serial = canon_str(s, strlen(s));
                        ADD2LOG("    serial = \"%s\"\n", usb->serial);
                    }

                    if(hd_attr_uint(sysfs_get_device_attr(sf_dev_2, "bcdDevice"), &ul0, 16)) {
                        usb->rev = ul0;
                        ADD2LOG("    bcdDevice = %04x\n", usb->rev);
                    }

                    if((s = hd_attr_str(sysfs_get_device_attr(sf_dev_2, "speed")))) {
                        s = canon_str(s, strlen(s));
                        if(!strcmp(s, "1.5")) usb->speed = 15*100000;
                        else if(!strcmp(s, "12")) usb->speed = 12*1000000;
                        else if(!strcmp(s, "480")) usb->speed = 480*1000000;
                        ADD2LOG("    speed = \"%s\"\n", s);
                        s = free_mem(s);
                    }

                    sysfs_close_device(sf_dev_2);
                }
            }

            if(usb->vendor || usb->device) {
                hd->vendor.id = MAKE_ID(TAG_USB, usb->vendor);
                hd->device.id = MAKE_ID(TAG_USB, usb->device);
            }

            if(usb->manufact) hd->vendor.name = new_str(usb->manufact);
            if(usb->product) hd->device.name = new_str(usb->product);
            if(usb->serial) hd->serial = new_str(usb->serial);

            if(usb->rev) str_printf(&hd->revision.name, 0, "%x.%02x", usb->rev >> 8, usb->rev & 0xff);

            if(usb->speed) {
                res = add_res_entry(&hd->res, new_mem(sizeof *res));
                res->baud.type = res_baud;
                res->baud.speed = usb->speed;
            }

            s = hd_sysfs_find_driver(hd_data, hd->sysfs_id, 1);
            if(s) add_str_list(&hd->drivers, s);

            set_class_entries(hd_data, hd, usb);

            if(!hd_data->scanner_db) {
                hd_data->scanner_db = hd_module_list(hd_data, 1);
            }

            if(
                hd->drivers &&
                search_str_list(hd_data->scanner_db, hd->drivers->str)
            ) {
                hd->base_class.id = bc_scanner;
            }

            // ###### FIXME
            if(hd->base_class.id == bc_modem) {
                hd->unix_dev_name = new_str("/dev/ttyACM0");
            }

        }