~ubuntu-branches/ubuntu/saucy/libusbx/saucy-proposed

« back to all changes in this revision

Viewing changes to examples/xusb.c

  • Committer: Package Import Robot
  • Author(s): Aurelien Jarno
  • Date: 2013-05-06 17:31:43 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130506173143-wfbiltvcnn7pvdpa
Tags: 2:1.0.15-1
* New upstream version.
* Fix cross-build (closes: #694912, #705658).
* Add a -dbg package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#define msleep(msecs) usleep(1000*msecs)
34
34
#endif
35
35
 
36
 
#if !defined(_MSC_VER) || _MSC_VER<=1200
37
 
#define sscanf_s sscanf
38
 
#endif
39
 
 
40
36
#if !defined(bool)
41
37
#define bool int
42
38
#endif
47
43
#define false (!true)
48
44
#endif
49
45
 
50
 
 
51
46
// Future versions of libusbx will use usb_interface instead of interface
52
47
// in libusb_config_descriptor => catter for that
53
48
#define usb_interface interface
54
49
 
55
50
// Global variables
56
51
bool binary_dump = false;
57
 
char binary_name[64] = "raw.bin";
 
52
bool extra_info = false;
 
53
const char* binary_name = NULL;
58
54
 
59
55
static int perr(char const *format, ...)
60
56
{
692
688
        void* le_type_punning_IS_fine;
693
689
        struct {
694
690
                const char* desc;
 
691
                uint8_t recipient;
695
692
                uint16_t index;
696
693
                uint16_t header_size;
697
694
        } os_fd[2] = {
698
 
                {"Extended Compat ID", 0x0004, 0x10},
699
 
                {"Extended Properties", 0x0005, 0x0A}
 
695
                {"Extended Compat ID", LIBUSB_RECIPIENT_DEVICE, 0x0004, 0x10},
 
696
                {"Extended Properties", LIBUSB_RECIPIENT_INTERFACE, 0x0005, 0x0A}
700
697
        };
701
698
 
702
699
        if (iface_number < 0) return;
705
702
                printf("\nReading %s OS Feature Descriptor (wIndex = 0x%04d):\n", os_fd[i].desc, os_fd[i].index);
706
703
 
707
704
                // Read the header part
708
 
                r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|LIBUSB_RECIPIENT_DEVICE),
 
705
                r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|os_fd[i].recipient),
709
706
                        bRequest, (uint16_t)(((iface_number)<< 8)|0x00), os_fd[i].index, os_desc, os_fd[i].header_size, 1000);
710
707
                if (r < os_fd[i].header_size) {
711
708
                        perr("   Failed: %s", (r<0)?libusb_error_name((enum libusb_error)r):"header size is too small");
718
715
                }
719
716
 
720
717
                // Read the full feature descriptor
721
 
                r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|LIBUSB_RECIPIENT_DEVICE),
 
718
                r = libusb_control_transfer(handle, (uint8_t)(LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_VENDOR|os_fd[i].recipient),
722
719
                        bRequest, (uint16_t)(((iface_number)<< 8)|0x00), os_fd[i].index, os_desc, (uint16_t)length, 1000);
723
720
                if (r < 0) {
724
721
                        perr("   Failed: %s", libusb_error_name((enum libusb_error)r));
738
735
        const struct libusb_endpoint_descriptor *endpoint;
739
736
        int i, j, k, r;
740
737
        int iface, nb_ifaces, first_iface = -1;
741
 
#if defined(__linux__)
742
 
        // Attaching/detaching the kernel driver is only relevant for Linux
 
738
        // For attaching/detaching the kernel driver, if needed
743
739
        int iface_detached = -1;
744
 
#endif
745
740
        struct libusb_device_descriptor dev_desc;
746
741
        const char* speed_name[5] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)",
747
742
                "480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)"};
749
744
        uint8_t string_index[3];        // indexes of the string descriptors
750
745
        uint8_t endpoint_in = 0, endpoint_out = 0;      // default IN and OUT endpoints
751
746
 
752
 
        printf("Opening device...\n");
 
747
        printf("Opening device %04X:%04X...\n", vid, pid);
753
748
        handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
754
749
 
755
750
        if (handle == NULL) {
759
754
 
760
755
        dev = libusb_get_device(handle);
761
756
        bus = libusb_get_bus_number(dev);
762
 
        r = libusb_get_port_path(NULL, dev, port_path, sizeof(port_path));
763
 
        if (r > 0) {
764
 
                printf("bus: %d, port path from HCD: %d", bus, port_path[0]);
765
 
                for (i=1; i<r; i++) {
766
 
                        printf("->%d", port_path[i]);
 
757
        if (extra_info) {
 
758
                r = libusb_get_port_path(NULL, dev, port_path, sizeof(port_path));
 
759
                if (r > 0) {
 
760
                        printf("\nDevice properties:\n");
 
761
                        printf("        bus number: %d\n", bus);
 
762
                        printf("         port path: %d", port_path[0]);
 
763
                        for (i=1; i<r; i++) {
 
764
                                printf("->%d", port_path[i]);
 
765
                        }
 
766
                        printf(" (from root hub)\n");
767
767
                }
768
 
                printf("\n");
 
768
                r = libusb_get_device_speed(dev);
 
769
                if ((r<0) || (r>4)) r=0;
 
770
                printf("             speed: %s\n", speed_name[r]);
769
771
        }
770
 
        r = libusb_get_device_speed(dev);
771
 
        if ((r<0) || (r>4)) r=0;
772
 
        printf("speed: %s\n", speed_name[r]);
773
772
 
774
773
        printf("\nReading device descriptor:\n");
775
774
        CALL_CHECK(libusb_get_device_descriptor(dev, &dev_desc));
832
831
        {
833
832
                printf("\nClaiming interface %d...\n", iface);
834
833
                r = libusb_claim_interface(handle, iface);
835
 
#if defined(__linux__)
836
 
                if ((r != LIBUSB_SUCCESS) && (iface == 0)) {
837
 
                        // Maybe we need to detach the driver
838
 
                        perr("   Failed. Trying to detach driver...\n");
839
 
                        libusb_detach_kernel_driver(handle, iface);
840
 
                        iface_detached = iface;
841
 
                        printf("   Claiming interface again...\n");
842
 
                        r = libusb_claim_interface(handle, iface);
 
834
                if ((r != LIBUSB_SUCCESS) && libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)
 
835
                        && (libusb_kernel_driver_active(handle, iface) > 0)) {
 
836
                        // Try to detach the kernel driver
 
837
                        perr("   A kernel driver is active, trying to detach it...\n");
 
838
                        r = libusb_detach_kernel_driver(handle, iface);
 
839
                        if (r == LIBUSB_SUCCESS) {
 
840
                                iface_detached = iface;
 
841
                                printf("   Claiming interface again...\n");
 
842
                                r = libusb_claim_interface(handle, iface);
 
843
                        }
843
844
                }
844
 
#endif
845
845
                if (r != LIBUSB_SUCCESS) {
846
846
                        perr("   Failed.\n");
847
847
                }
890
890
                libusb_release_interface(handle, iface);
891
891
        }
892
892
 
893
 
#if defined(__linux__)
894
893
        if (iface_detached >= 0) {
895
894
                printf("Re-attaching kernel driver...\n");
896
895
                libusb_attach_kernel_driver(handle, iface_detached);
897
896
        }
898
 
#endif
899
897
 
900
898
        printf("Closing device...\n");
901
899
        libusb_close(handle);
933
931
                                case 'd':
934
932
                                        debug_mode = true;
935
933
                                        break;
 
934
                                case 'i':
 
935
                                        extra_info = true;
 
936
                                        break;
936
937
                                case 'b':
937
 
                                        if (j+1 < argc) {
938
 
                                                strncpy(binary_name, argv[j+1], 64);
939
 
                                                j++;
 
938
                                        if ((j+1 >= argc) || (argv[j+1][0] == '-') || (argv[j+1][0] == '/')) {
 
939
                                                printf("   Option -b requires a file name");
 
940
                                                return 1;
940
941
                                        }
 
942
                                        binary_name = argv[++j];
941
943
                                        binary_dump = true;
942
944
                                        break;
943
 
                                case 'g':
944
 
                                        break;
945
945
                                case 'j':
946
946
                                        // OLIMEX ARM-USB-TINY JTAG, 2 channel composite device - 2 interfaces
947
947
                                        if (!VID && !PID) {
985
985
                                                break;
986
986
                                }
987
987
                                if (i != arglen) {
988
 
                                        if (sscanf_s(argv[j], "%x:%x" , &tmp_vid, &tmp_pid) != 2) {
 
988
                                        if (sscanf(argv[j], "%x:%x" , &tmp_vid, &tmp_pid) != 2) {
989
989
                                                printf("   Please specify VID & PID as \"vid:pid\" in hexadecimal format\n");
990
990
                                                return 1;
991
991
                                        }
999
999
        }
1000
1000
 
1001
1001
        if ((show_help) || (argc == 1) || (argc > 7)) {
1002
 
                printf("usage: %s [-d] [-b [file]] [-h] [-i] [-j] [-k] [-x] [vid:pid]\n", argv[0]);
1003
 
                printf("   -h: display usage\n");
1004
 
                printf("   -d: enable debug output (if library was compiled with debug enabled)\n");
1005
 
                printf("   -b: dump Mass Storage first block to binary file\n");
1006
 
                printf("   -g: short generic test (default)\n");
1007
 
                printf("   -k: test generic Mass Storage USB device (using WinUSB)\n");
1008
 
                printf("   -j: test FTDI based JTAG device (using WinUSB)\n");
1009
 
                printf("   -p: test Sony PS3 SixAxis controller (using WinUSB)\n");
1010
 
                printf("   -s: test Microsoft Sidewinder Precision Pro (using HID)\n");
1011
 
                printf("   -x: test Microsoft XBox Controller Type S (using WinUSB)\n");
 
1002
                printf("usage: %s [-h] [-d] [-i] [-k] [-b file] [-j] [-x] [-s] [-p] [vid:pid]\n", argv[0]);
 
1003
                printf("   -h      : display usage\n");
 
1004
                printf("   -d      : enable debug output\n");
 
1005
                printf("   -i      : print topology and speed info\n");
 
1006
                printf("   -j      : test composite FTDI based JTAG device\n");
 
1007
                printf("   -k      : test Mass Storage device\n");
 
1008
                printf("   -b file : dump Mass Storage data to file 'file'\n");
 
1009
                printf("   -p      : test Sony PS3 SixAxis controller\n");
 
1010
                printf("   -s      : test Microsoft Sidewinder Precision Pro (HID)\n");
 
1011
                printf("   -x      : test Microsoft XBox Controller Type S\n");
 
1012
                printf("If only the vid:pid is provided, xusb attempts to run the most appropriate test\n");
1012
1013
                return 0;
1013
1014
        }
1014
1015