"Fossies" - the Fresh Open Source Software Archive

Member "usbutils-014/usbhid-dump/src/dev_list.c" (22 Feb 2021, 3050 Bytes) of package /linux/misc/usbutils-014.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dev_list.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 013_vs_014.

    1 // SPDX-License-Identifier: GPL-2.0-or-later
    2 /*
    3  * usbhid-dump - device list
    4  *
    5  * Copyright (C) 2010 Nikolai Kondrashov <spbnick@gmail.com>
    6  */
    7 
    8 #include "config.h"
    9 
   10 #include "misc.h"
   11 #include "dev_list.h"
   12 #include <assert.h>
   13 #include <stdlib.h>
   14 #include <stdio.h>
   15 
   16 bool
   17 uhd_dev_list_valid(const uhd_dev *list)
   18 {
   19     UHD_DEV_LIST_FOR_EACH(list, list)
   20         if (!uhd_dev_valid(list))
   21             return false;
   22 
   23     return true;
   24 }
   25 
   26 
   27 void
   28 uhd_dev_list_close(uhd_dev *list)
   29 {
   30     uhd_dev *next;
   31 
   32     for (; list != NULL; list = next)
   33     {
   34         next = list->next;
   35         uhd_dev_close(list);
   36     }
   37 }
   38 
   39 
   40 enum libusb_error
   41 uhd_dev_list_open(libusb_context *ctx,
   42                   uint8_t bus_num, uint8_t dev_addr,
   43                   uint16_t vid, uint16_t pid,
   44                   uhd_dev **plist)
   45 {
   46     enum libusb_error                   err         = LIBUSB_ERROR_OTHER;
   47     libusb_device                     **lusb_list   = NULL;
   48     ssize_t                             num;
   49     ssize_t                             idx;
   50     libusb_device                      *lusb_dev;
   51     struct libusb_device_descriptor     desc;
   52     uhd_dev                            *list        = NULL;
   53     uhd_dev                            *dev;
   54 
   55     assert(ctx != NULL);
   56 
   57     /* Retrieve libusb device list */
   58     num = libusb_get_device_list(ctx, &lusb_list);
   59     if (num == LIBUSB_ERROR_NO_MEM)
   60     {
   61         err = num;
   62         goto cleanup;
   63     }
   64 
   65     /* Find and open the devices */
   66     for (idx = 0; idx < num; idx++)
   67     {
   68         lusb_dev = lusb_list[idx];
   69 
   70         /* Skip devices not matching bus_num/dev_addr mask */
   71         if ((bus_num != UHD_BUS_NUM_ANY &&
   72              libusb_get_bus_number(lusb_dev) != bus_num) ||
   73             (dev_addr != UHD_DEV_ADDR_ANY &&
   74              libusb_get_device_address(lusb_dev) != dev_addr))
   75             continue;
   76 
   77         /* Skip devices not matching vendor/product mask */
   78         if (vid != UHD_VID_ANY || pid != UHD_PID_ANY)
   79         {
   80             err = libusb_get_device_descriptor(lusb_dev, &desc);
   81             if (err != LIBUSB_SUCCESS)
   82                 goto cleanup;
   83 
   84             if ((vid != UHD_VID_ANY && vid != desc.idVendor) ||
   85                 (pid != UHD_PID_ANY && pid != desc.idProduct))
   86                 continue;
   87         }
   88 
   89         /* Open and append the device to the list */
   90         err = uhd_dev_open(lusb_dev, &dev);
   91         if (err != LIBUSB_SUCCESS)
   92             goto cleanup;
   93 
   94         dev->next = list;
   95         list = dev;
   96     }
   97 
   98     /* Free the libusb device list freeing unused devices */
   99     libusb_free_device_list(lusb_list, true);
  100     lusb_list = NULL;
  101 
  102     /* Output device list, if requested */
  103     assert(uhd_dev_list_valid(list));
  104     if (plist != NULL)
  105     {
  106         *plist = list;
  107         list = NULL;
  108     }
  109 
  110     /* Done! */
  111     err = LIBUSB_SUCCESS;
  112 
  113 cleanup:
  114 
  115     /* Close the device list if not output */
  116     uhd_dev_list_close(list);
  117 
  118     /* Free the libusb device list along with devices */
  119     if (lusb_list != NULL)
  120         libusb_free_device_list(lusb_list, true);
  121 
  122     return err;
  123 }
  124 
  125