summaryrefslogtreecommitdiff
path: root/lib/libusb.c
blob: f56c88e7535cb21cb0829f00a72aa822a2cef815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/** @file
 * @brief hid-dump - libusb API extensions
 *
 * Copyright (C) 2010 Nikolai Kondrashov
 *
 * This file is part of hid-dump.
 *
 * Hid-dump is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Hid-dump is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with hid-dump; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @author Nikolai Kondrashov <spbnick@gmail.com>
 *
 * @(#) $Id$
 */

#include "hid_dump/libusb.h"
#include <stdbool.h>

const char *
libusb_strerror(enum libusb_error err)
{
    switch (err)
    {
        case LIBUSB_SUCCESS:
            return "Success";
#define MAP(_name, _desc) \
    case LIBUSB_ERROR_##_name:          \
        return _desc " (" #_name ")"
	    MAP(IO,
            "Input/output error");
	    MAP(INVALID_PARAM,
            "Invalid parameter");
	    MAP(ACCESS,
            "Access denied (insufficient permissions)");
	    MAP(NO_DEVICE,
            "No such device (it may have been disconnected)");
	    MAP(NOT_FOUND,
            "Entity not found");
	    MAP(BUSY,
            "Resource busy");
	    MAP(TIMEOUT,
            "Operation timed out");
	    MAP(OVERFLOW,
            "Overflow");
	    MAP(PIPE,
            "Pipe error");
	    MAP(INTERRUPTED,
            "System call interrupted (perhaps due to signal)");
	    MAP(NO_MEM,
            "Insufficient memory");
	    MAP(NOT_SUPPORTED,
            "Operation not supported or unimplemented on this platform");
	    MAP(OTHER, "Other error");
#undef MAP
        default:
            return "Unknown error code";
    }
}


enum libusb_error
libusb_open_device_with_bus_dev(libusb_context         *ctx,
                                uint8_t                 bus_num,
                                uint8_t                 dev_addr,
                                libusb_device_handle  **phandle)
{
    enum libusb_error       err     = LIBUSB_ERROR_OTHER;
    libusb_device         **list    = NULL;
    ssize_t                 num;
    ssize_t                 idx;
    libusb_device          *dev;
    libusb_device_handle   *handle  = NULL;

    /* Retrieve device list */
    num = libusb_get_device_list(ctx, &list);
    if (num == LIBUSB_ERROR_NO_MEM)
    {
        err = num;
        goto cleanup;
    }

    /* Find and open the device */
    for (idx = 0; idx < num; idx++)
    {
        dev = list[idx];

        if (libusb_get_bus_number(dev) == bus_num &&
            libusb_get_device_address(dev) == dev_addr)
        {
            err = libusb_open(dev, &handle);
            if (err != LIBUSB_SUCCESS)
                goto cleanup;
            break;
        }
    }

    /* Free the device list freeing unused devices */
    libusb_free_device_list(list, true);
    list = NULL;

    /* Check if the device is found */
    if (handle == NULL)
    {
        err = LIBUSB_ERROR_NO_DEVICE;
        goto cleanup;
    }

    /* Output the device handle */
    if (phandle != NULL)
    {
        *phandle = handle;
        handle = NULL;
    }

    err = LIBUSB_SUCCESS;

cleanup:

    /* Free the device */
    if (handle != NULL)
        libusb_close(handle);

    /* Free device list along with devices */
    if (list != NULL)
        libusb_free_device_list(list, true);

    return err;
}