blob: 5c34ba127f8ae5210667ef73e20067ada8369b80 [file] [log] [blame]
Daniel Thompson1d507a02018-07-12 11:02:28 +01001/*
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 <string.h>
19#include <libusb.h>
20#include "programmer.h"
21
22struct libusb_device_handle *usb_dev_get_by_vid_pid_serial(
23 struct libusb_context *usb_ctx, uint16_t vid, uint16_t pid, const char *serialno)
24{
25 struct libusb_device **list;
26 ssize_t count = libusb_get_device_list(usb_ctx, &list);
27 if (count < 0) {
28 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
29 return NULL;
30 }
31
32 ssize_t i = 0;
33 for (i = 0; i < count; i++) {
34 struct libusb_device *dev = list[i];
35 struct libusb_device_descriptor desc;
36 struct libusb_device_handle *handle;
37
38 int res = libusb_get_device_descriptor(dev, &desc);
39 if (res != 0) {
40 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(res));
41 continue;
42 }
43
44 if ((desc.idVendor != vid) && (desc.idProduct != pid))
45 continue;
46
47 msg_pdbg("Found USB device %04"PRIx16":%04"PRIx16" at address %d-%d.\n",
48 desc.idVendor, desc.idProduct,
49 libusb_get_bus_number(dev), libusb_get_device_address(dev));
50
51 res = libusb_open(dev, &handle);
52 if (res != 0) {
53 msg_perr("Opening the USB device failed (%s)!\n", libusb_error_name(res));
54 continue;
55 }
56
57 if (serialno) {
58 unsigned char myserial[64];
59 res = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, myserial,
60 sizeof(myserial));
61 if (res < 0) {
62 msg_perr("Reading the USB serialno failed (%s)!\n", libusb_error_name(res));
63 libusb_close(handle);
64 continue;
65 }
66 msg_pdbg("Serial number is %s\n", myserial);
67
68 /* Reject any serial number that does not commence with serialno */
69 if (0 != strncmp(serialno, (char *)myserial, strlen(serialno))) {
70 libusb_close(handle);
71 continue;
72 }
73 }
74
75 libusb_free_device_list(list, 1);
76 return handle;
77 }
78
79 libusb_free_device_list(list, 1);
80 return NULL;
81}
82
83/*
84 * This function allows different devices to be targeted based on enumeration order. Different
85 * hotplug sequencing (or simply a reboot) may change the enumeration order. This function should
86 * only be used if a programmers does not provide an alternative way to identify itself uniquely
87 * (such as a unique serial number).
88 */
89struct libusb_device_handle *usb_dev_get_by_vid_pid_number(
90 struct libusb_context *usb_ctx, uint16_t vid, uint16_t pid, unsigned int num)
91{
92 struct libusb_device **list;
93 ssize_t count = libusb_get_device_list(usb_ctx, &list);
94 if (count < 0) {
95 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
96 return NULL;
97 }
98
99 struct libusb_device_handle *handle = NULL;
100 ssize_t i = 0;
101 for (i = 0; i < count; i++) {
102 struct libusb_device *dev = list[i];
103 struct libusb_device_descriptor desc;
104 int err = libusb_get_device_descriptor(dev, &desc);
105 if (err != 0) {
106 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
107 libusb_free_device_list(list, 1);
108 return NULL;
109 }
110 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
111 msg_pdbg("Found USB device %04"PRIx16":%04"PRIx16" at address %d-%d.\n",
112 desc.idVendor, desc.idProduct,
113 libusb_get_bus_number(dev), libusb_get_device_address(dev));
114 if (num == 0) {
115 err = libusb_open(dev, &handle);
116 if (err != 0) {
117 msg_perr("Opening the USB device failed (%s)!\n",
118 libusb_error_name(err));
119 libusb_free_device_list(list, 1);
120 return NULL;
121 }
122 break;
123 }
124 num--;
125 }
126 }
127 libusb_free_device_list(list, 1);
128
129 return handle;
130}