Greetings,

I'm interested in controlling the port power on my USB hub based on
the serial number of the device plugged into the respective port.
I've read the archives and purchased a hub with the appropriate port
power control circuitry (namely the Targus, Inc.'s PAUH212) .  Using
the source available from http://www.gniibe.org/log/2006/01/14 I have
been to successfully turn the port power on and off.

I've experimented a bit and have been able to identify the bus number
and device number of the hub to which the device I'm interesting in
powering on/off is connected.  What I've not been able to figure out,
however, is the number of the port to which the device is connected.

One alternative I have been able to find is that instead of searching
for the hub's bus/device information, I can search for the device's
bus/device information, use that information to find an entry in
/proc/bus/usb/devices (which lists both the parent device number and
port), then carry on from there.  However, I'd really rather stay away
from parsing out that information.

I've attached a copy of the code I've been working with so far.  Any
suggestions you might have would be greatly appreciated.

Thanks in advance.

Andy
/*
 * Copyright (C) 2006 Free Software Initiative of Japan
 *
 * Author:      NIIBE Yutaka <gniibe at fsij.org>
 * Modified by: Andrew R. Dalton <andy.dalton at gmail.com>
 *
 * This file can be distributed under the terms and conditions of the
 * GNU General Public License version 2 (or later).
 *
 */

#include <usb.h>
#include <stdio.h>

#define SERIAL_MAX_LEN 128

static void usage(const char* progname) {
    fprintf(stderr, "Usage: %s -s <SERIAL>\n", progname);
}


int main(int argc, const char* argv[]) {
    char            serial[SERIAL_MAX_LEN] = { '\0' };
    struct usb_bus* busses;
    struct usb_bus* bus;
    int             i;

    for (i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
            case 's':
                if (++i >= argc) {
                    usage(argv[0]);
                    exit(1);
                }
                strncpy(serial, argv[i], SERIAL_MAX_LEN);
                serial[SERIAL_MAX_LEN - 1] = '\0';
                break;

            default:
                usage(argv[0]);
                exit(1);
            }
        }
    }

    if (serial[0] == '\0') {
        usage(argv[0]);
        exit(1);
    }

    usb_init();
    usb_find_busses();
    usb_find_devices();

    busses = usb_get_busses();
    if (busses == NULL) {
        perror("Failed to access USB");
        exit(1);
    }


    for (bus = busses; bus != NULL; bus = bus->next) {
        struct usb_device* dev;

        for (dev = bus->devices; dev != NULL; dev = dev->next) {

            if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
                continue;
            }

            /* We've found a hub, loop over each connected device */
            for (i = 0; i < dev->num_children; ++i) {
                char               devSerial[SERIAL_MAX_LEN] = { '\0' };
                struct usb_device* child;
                usb_dev_handle*    udh;

                child = dev->children[i];
                if (child == NULL) {
                    /* It's not clear this would ever happen */
                    continue;
                }

                udh = usb_open(child);
                if (udh == NULL) {
                    perror("Failed to open child usb device");
                    continue;
                }

                usb_claim_interface(udh, 0);
                usb_get_string_simple(udh, child->descriptor.iSerialNumber,
                                      devSerial, SERIAL_MAX_LEN);

                if (!strncmp(serial, devSerial, SERIAL_MAX_LEN)) {
                    int busNum = atoi(bus->dirname);
                    int devNum = atoi(dev->filename);
                    int port   = 0; /* How do I find this? */

                    printf("Device:  bus: %d, device: %d, port: %d\n",
                           busNum, devNum, port);

                    exit(0);
                }

                usb_release_interface(udh, 0);
                usb_close(udh);
            }
        }
    }

    fprintf(stderr, "Device not found.\n");
    return 1;
}
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Linux-usb-users@lists.sourceforge.net
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-users

Reply via email to