[EMAIL PROTECTED] wrote:

Hi,


Can you please tell me, is there any way to map between actual USB port and device files. Its something like port 1 ->devicefile1 port 2 ->devicefile2 port 3 ->devicefile3 etc....

can you please give us some guidelines on this thru which we can able to
do this.
Thanks.




You can use the information in sysfs /sys/bus/devices/x-a/devnum to find out the usb device
address on bus x port a(.b.c...) and then find this by using the usual usbfs methods.
Have a look at the attached fragment for an example.


/Brian
usb_dev_handle *find_terminal(unsigned int busnum,
		char* device_node)
{
    struct usb_bus *bus;
    usb_dev_handle *terminal = NULL;
    int err;
    struct sysfs_device *dev;
    struct sysfs_attribute *attr;
    unsigned int idProduct;
    unsigned int idVendor;
    unsigned int devnum;
    char device_id[SYSFS_PATH_MAX];

    snprintf(device_id, SYSFS_PATH_MAX, "%d-%s", busnum, device_node);
    
    dev = sysfs_open_device("usb", device_id);
    if (dev == NULL) {
        syslog(LOG_ERR, "no such device bus %d device %s.",
			busnum, device_node);
        return 0;
    }

    attr = sysfs_get_device_attr(dev, "idProduct");
    if (attr == 0) {
        syslog(LOG_ERR, "attribute has disappeared");
        return 0;
    }
    idProduct = strtoul(attr->value, 0, 16);
    attr = sysfs_get_device_attr(dev, "idVendor");
    if (attr == 0) {
        syslog(LOG_ERR, "attribute has disappeared");
        return 0;
    }
    idVendor = strtoul(attr->value, 0, 16);
    attr = sysfs_get_device_attr(dev, "devnum");
    if (attr == 0) {
        syslog(LOG_ERR, "attribute has disappeared");
        return 0;
    }
    devnum = strtoul(attr->value, 0, 10);

    sysfs_close_device(dev);

    syslog(LOG_DEBUG, "usb bus id %04x:%04x device no %d",
		    idVendor, idProduct, devnum);

    if (idVendor != VENDOR_ID || idProduct != PRODUCT_ID) {
        syslog(LOG_ERR, "wrong device id %04x:%04x found at position %s.",
                idVendor, idProduct, device_id);
        return 0;
    }

    syslog(LOG_DEBUG, "usb_find_busses");
    usb_find_busses();
    syslog(LOG_DEBUG, "usb_find_devices");
    usb_find_devices();
    syslog(LOG_DEBUG, "usb_find_devices done");

    for (bus = usb_busses; bus; bus = bus->next) {
        struct usb_device *dev;
	int current_busnum = strtoul(bus->dirname, 0, 10);

	if (busnum != current_busnum)
	    continue;

        for (dev = bus->devices; dev; dev = dev->next) {
	    unsigned int current_devnum = strtoul(dev->filename, 0, 10);
            syslog(LOG_DEBUG, "bus %s device %s id %04x %04x\n",
                    bus->dirname,
                    dev->filename,
                    dev->descriptor.idVendor,
                    dev->descriptor.idProduct);
            if (devnum == current_devnum) {
                syslog(LOG_INFO, "found device");
                terminal = usb_open(dev);
                if (terminal == NULL) {
                    syslog(LOG_ERR, "usb_open error: %m");
                    return 0;
                }
                break;
            }
        }
    }

    if (!terminal) {
        syslog(LOG_ERR, "could not find USB device. Check cabling.");
        return 0;
    }

    syslog(LOG_INFO, "claim interface\n");

    if ( ( err = usb_claim_interface(terminal, 0) ) < 0) {
        syslog(LOG_ERR, "could not claim interface: %m");
        return 0;
    }

    return terminal;
}

Reply via email to