blob: 55b92466d58ead0bf68d83eb2e9d6d128bd28bfc [file] [log] [blame]
Edward O'Callaghana88395f2019-02-27 18:44:04 +11001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2016 secunet Security Networks AG
5 * Copyright (C) 2018 Linaro Limited
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <inttypes.h>
19#include <string.h>
20#include <libusb.h>
21#include "programmer.h"
22
23struct libusb_device_handle *usb_dev_get_by_vid_pid_serial(
24 struct libusb_context *usb_ctx, uint16_t vid, uint16_t pid, const char *serialno)
25{
26 struct libusb_device **list;
27 ssize_t count = libusb_get_device_list(usb_ctx, &list);
28 if (count < 0) {
29 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
30 return NULL;
31 }
32
33 ssize_t i = 0;
34 for (i = 0; i < count; i++) {
35 struct libusb_device *dev = list[i];
36 struct libusb_device_descriptor desc;
37 struct libusb_device_handle *handle;
38
39 int res = libusb_get_device_descriptor(dev, &desc);
40 if (res != 0) {
41 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(res));
42 continue;
43 }
44
Patrick Rudolphfaa6b392019-05-20 11:31:44 +020045 if ((desc.idVendor != vid) || (desc.idProduct != pid))
Edward O'Callaghana88395f2019-02-27 18:44:04 +110046 continue;
47
48 msg_pdbg("Found USB device %04"PRIx16":%04"PRIx16" at address %d-%d.\n",
49 desc.idVendor, desc.idProduct,
50 libusb_get_bus_number(dev), libusb_get_device_address(dev));
51
52 res = libusb_open(dev, &handle);
53 if (res != 0) {
54 msg_perr("Opening the USB device failed (%s)!\n", libusb_error_name(res));
55 continue;
56 }
57
58 if (serialno) {
59 unsigned char myserial[64];
60 res = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, myserial,
61 sizeof(myserial));
62 if (res < 0) {
63 msg_perr("Reading the USB serialno failed (%s)!\n", libusb_error_name(res));
64 libusb_close(handle);
65 continue;
66 }
67 msg_pdbg("Serial number is %s\n", myserial);
68
69 /* Reject any serial number that does not commence with serialno */
70 if (0 != strncmp(serialno, (char *)myserial, strlen(serialno))) {
71 libusb_close(handle);
72 continue;
73 }
74 }
75
76 libusb_free_device_list(list, 1);
77 return handle;
78 }
79
80 libusb_free_device_list(list, 1);
81 return NULL;
82}
83
84/*
85 * This function allows different devices to be targeted based on enumeration order. Different
86 * hotplug sequencing (or simply a reboot) may change the enumeration order. This function should
87 * only be used if a programmers does not provide an alternative way to identify itself uniquely
88 * (such as a unique serial number).
89 */
90struct libusb_device_handle *usb_dev_get_by_vid_pid_number(
91 struct libusb_context *usb_ctx, uint16_t vid, uint16_t pid, unsigned int num)
92{
93 struct libusb_device **list;
94 ssize_t count = libusb_get_device_list(usb_ctx, &list);
95 if (count < 0) {
96 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
97 return NULL;
98 }
99
100 struct libusb_device_handle *handle = NULL;
101 ssize_t i = 0;
102 for (i = 0; i < count; i++) {
103 struct libusb_device *dev = list[i];
104 struct libusb_device_descriptor desc;
105 int err = libusb_get_device_descriptor(dev, &desc);
106 if (err != 0) {
107 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
108 libusb_free_device_list(list, 1);
109 return NULL;
110 }
111 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
112 msg_pdbg("Found USB device %04"PRIx16":%04"PRIx16" at address %d-%d.\n",
113 desc.idVendor, desc.idProduct,
114 libusb_get_bus_number(dev), libusb_get_device_address(dev));
115 if (num == 0) {
116 err = libusb_open(dev, &handle);
117 if (err != 0) {
118 msg_perr("Opening the USB device failed (%s)!\n",
119 libusb_error_name(err));
120 libusb_free_device_list(list, 1);
121 return NULL;
122 }
123 break;
124 }
125 num--;
126 }
127 }
128 libusb_free_device_list(list, 1);
129
130 return handle;
131}