2 * @brief usbhid-dump - interface list
4 * Copyright (C) 2010 Nikolai Kondrashov
6 * This file is part of usbhid-dump.
8 * Usbhid-dump is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Usbhid-dump is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with usbhid-dump; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * @author Nikolai Kondrashov <spbnick@gmail.com>
29 #include "uhd/iface_list.h"
35 uhd_iface_list_valid(const uhd_iface *list)
37 UHD_IFACE_LIST_FOR_EACH(list, list)
38 if (!uhd_iface_valid(list))
46 uhd_iface_list_len(const uhd_iface *list)
50 UHD_IFACE_LIST_FOR_EACH(list, list)
58 uhd_iface_list_free(uhd_iface *list)
62 for (; list != NULL; list = next)
71 uhd_iface_list_new(uhd_dev *dev_list,
74 enum libusb_error err = LIBUSB_ERROR_OTHER;
77 struct libusb_config_descriptor *config = NULL;
78 const struct libusb_interface *lusb_iface;
79 const struct libusb_endpoint_descriptor *ep_list;
81 const struct libusb_endpoint_descriptor *ep;
82 uhd_iface *list = NULL;
85 assert(uhd_dev_list_valid(dev_list));
87 UHD_DEV_LIST_FOR_EACH(dev, dev_list)
89 /* Retrieve active configuration descriptor */
90 err = libusb_get_active_config_descriptor(libusb_get_device(dev->handle),
92 if (err != LIBUSB_SUCCESS)
96 * Build the matching interface list
99 /* For each interface */
100 for (lusb_iface = config->interface;
101 lusb_iface - config->interface < config->bNumInterfaces;
104 /* Skip interfaces with altsettings and non-HID interfaces */
105 if (lusb_iface->num_altsetting != 1 ||
106 lusb_iface->altsetting->bInterfaceClass != LIBUSB_CLASS_HID)
109 /* Retrieve endpoint list */
110 ep_list = lusb_iface->altsetting->endpoint;
111 ep_num = lusb_iface->altsetting->bNumEndpoints;
113 /* For each endpoint */
114 for (ep = ep_list; (ep - ep_list) < ep_num; ep++)
116 /* Skip non-interrupt and non-in endpoints */
117 if ((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) !=
118 LIBUSB_TRANSFER_TYPE_INTERRUPT ||
119 (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) !=
123 /* Create the interface */
124 iface = uhd_iface_new(
126 lusb_iface->altsetting->bInterfaceNumber,
127 ep->bEndpointAddress, ep->wMaxPacketSize);
130 err = LIBUSB_ERROR_NO_MEM;
134 /* Add the interface */
142 /* Free the config descriptor */
143 libusb_free_config_descriptor(config);
147 /* Output the resulting list, if requested */
148 assert(uhd_iface_list_valid(list));
156 err = LIBUSB_SUCCESS;
160 libusb_free_config_descriptor(config);
161 uhd_iface_list_free(list);
168 uhd_iface_list_fltr_by_num(uhd_iface *list,
175 assert(uhd_iface_list_valid(list));
176 assert(number < UINT8_MAX);
178 for (prev = NULL, iface = list; iface != NULL; iface = next)
181 if (iface->number == number)
189 uhd_iface_free(iface);