blob: 1517a232ab18798ba748d2c9bf16a4a3f6fa9ef5 [file] [log] [blame]
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -07001/*
2 * Copyright (C) 2005-2007 Takahiro Hirofuchi
3 */
4
Valentina Manea021aed82014-03-08 14:53:26 +02005#include <libudev.h>
matt mooney099f79f2011-06-19 22:44:37 -07006#include "usbip_common.h"
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -07007#include "names.h"
8
matt mooneyf2fb62b2011-06-19 22:44:35 -07009#undef PROGNAME
10#define PROGNAME "libusbip"
11
Kurt Kanzenbach5af7746f2013-02-22 12:13:26 +010012int usbip_use_syslog;
13int usbip_use_stderr;
14int usbip_use_debug;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070015
Valentina Manea021aed82014-03-08 14:53:26 +020016extern struct udev *udev_context;
17
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070018struct speed_string {
19 int num;
20 char *speed;
21 char *desc;
22};
23
24static const struct speed_string speed_strings[] = {
25 { USB_SPEED_UNKNOWN, "unknown", "Unknown Speed"},
26 { USB_SPEED_LOW, "1.5", "Low Speed(1.5Mbps)" },
27 { USB_SPEED_FULL, "12", "Full Speed(12Mbps)" },
28 { USB_SPEED_HIGH, "480", "High Speed(480Mbps)" },
Shuah Khand1fd43d2014-01-24 11:34:13 -070029 { USB_SPEED_WIRELESS, "53.3-480", "Wireless"},
30 { USB_SPEED_SUPER, "5000", "Super Speed(5000Mbps)" },
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070031 { 0, NULL, NULL }
32};
33
34struct portst_string {
35 int num;
36 char *desc;
37};
38
39static struct portst_string portst_strings[] = {
40 { SDEV_ST_AVAILABLE, "Device Available" },
41 { SDEV_ST_USED, "Device in Use" },
42 { SDEV_ST_ERROR, "Device Error"},
43 { VDEV_ST_NULL, "Port Available"},
44 { VDEV_ST_NOTASSIGNED, "Port Initializing"},
45 { VDEV_ST_USED, "Port in Use"},
46 { VDEV_ST_ERROR, "Port Error"},
47 { 0, NULL}
48};
49
50const char *usbip_status_string(int32_t status)
51{
Kurt Kanzenbachb8ab0f22013-02-22 12:13:27 +010052 for (int i = 0; portst_strings[i].desc != NULL; i++)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070053 if (portst_strings[i].num == status)
54 return portst_strings[i].desc;
55
56 return "Unknown Status";
57}
58
59const char *usbip_speed_string(int num)
60{
Kurt Kanzenbachb8ab0f22013-02-22 12:13:27 +010061 for (int i = 0; speed_strings[i].speed != NULL; i++)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070062 if (speed_strings[i].num == num)
63 return speed_strings[i].desc;
64
65 return "Unknown Speed";
66}
67
68
69#define DBG_UDEV_INTEGER(name)\
70 dbg("%-20s = %x", to_string(name), (int) udev->name)
71
72#define DBG_UINF_INTEGER(name)\
73 dbg("%-20s = %x", to_string(name), (int) uinf->name)
74
matt mooney35dd0c22011-05-27 01:44:14 -070075void dump_usb_interface(struct usbip_usb_interface *uinf)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070076{
77 char buff[100];
Pawel Lebioda3eed8c02014-05-14 19:20:27 +020078
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070079 usbip_names_get_class(buff, sizeof(buff),
80 uinf->bInterfaceClass,
81 uinf->bInterfaceSubClass,
82 uinf->bInterfaceProtocol);
83 dbg("%-20s = %s", "Interface(C/SC/P)", buff);
84}
85
matt mooney35dd0c22011-05-27 01:44:14 -070086void dump_usb_device(struct usbip_usb_device *udev)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070087{
88 char buff[100];
89
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -070090 dbg("%-20s = %s", "path", udev->path);
91 dbg("%-20s = %s", "busid", udev->busid);
92
93 usbip_names_get_class(buff, sizeof(buff),
94 udev->bDeviceClass,
95 udev->bDeviceSubClass,
96 udev->bDeviceProtocol);
97 dbg("%-20s = %s", "Device(C/SC/P)", buff);
98
99 DBG_UDEV_INTEGER(bcdDevice);
100
101 usbip_names_get_product(buff, sizeof(buff),
102 udev->idVendor,
103 udev->idProduct);
104 dbg("%-20s = %s", "Vendor/Product", buff);
105
106 DBG_UDEV_INTEGER(bNumConfigurations);
107 DBG_UDEV_INTEGER(bNumInterfaces);
108
109 dbg("%-20s = %s", "speed",
110 usbip_speed_string(udev->speed));
111
112 DBG_UDEV_INTEGER(busnum);
113 DBG_UDEV_INTEGER(devnum);
114}
115
116
Valentina Manea021aed82014-03-08 14:53:26 +0200117int read_attr_value(struct udev_device *dev, const char *name,
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +0100118 const char *format)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700119{
Valentina Manea021aed82014-03-08 14:53:26 +0200120 const char *attr;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700121 int num = 0;
122 int ret;
123
Valentina Manea021aed82014-03-08 14:53:26 +0200124 attr = udev_device_get_sysattr_value(dev, name);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700125 if (!attr) {
Valentina Manea021aed82014-03-08 14:53:26 +0200126 err("udev_device_get_sysattr_value failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700127 goto err;
128 }
129
Valentina Manea87f627f2014-03-08 14:53:35 +0200130 /* The client chooses the device configuration
131 * when attaching it so right after being bound
132 * to usbip-host on the server the device will
133 * have no configuration.
134 * Therefore, attributes such as bConfigurationValue
135 * and bNumInterfaces will not exist and sscanf will
136 * fail. Check for these cases and don't treat them
137 * as errors.
138 */
139
Valentina Manea021aed82014-03-08 14:53:26 +0200140 ret = sscanf(attr, format, &num);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700141 if (ret < 1) {
Valentina Manea87f627f2014-03-08 14:53:35 +0200142 if (strcmp(name, "bConfigurationValue") &&
143 strcmp(name, "bNumInterfaces")) {
144 err("sscanf failed for attribute %s", name);
145 goto err;
146 }
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700147 }
148
149err:
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700150
151 return num;
152}
153
154
Valentina Manea021aed82014-03-08 14:53:26 +0200155int read_attr_speed(struct udev_device *dev)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700156{
Valentina Manea021aed82014-03-08 14:53:26 +0200157 const char *speed;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700158
Valentina Manea021aed82014-03-08 14:53:26 +0200159 speed = udev_device_get_sysattr_value(dev, "speed");
160 if (!speed) {
161 err("udev_device_get_sysattr_value failed");
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700162 goto err;
163 }
164
Kurt Kanzenbachb8ab0f22013-02-22 12:13:27 +0100165 for (int i = 0; speed_strings[i].speed != NULL; i++) {
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700166 if (!strcmp(speed, speed_strings[i].speed))
167 return speed_strings[i].num;
168 }
169
Valentina Manea021aed82014-03-08 14:53:26 +0200170err:
171
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700172 return USB_SPEED_UNKNOWN;
173}
174
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +0100175#define READ_ATTR(object, type, dev, name, format) \
176 do { \
177 (object)->name = (type) read_attr_value(dev, to_string(name), \
178 format); \
179 } while (0)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700180
181
Valentina Manea021aed82014-03-08 14:53:26 +0200182int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700183{
184 uint32_t busnum, devnum;
Valentina Manea021aed82014-03-08 14:53:26 +0200185 const char *path, *name;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700186
187 READ_ATTR(udev, uint8_t, sdev, bDeviceClass, "%02x\n");
188 READ_ATTR(udev, uint8_t, sdev, bDeviceSubClass, "%02x\n");
189 READ_ATTR(udev, uint8_t, sdev, bDeviceProtocol, "%02x\n");
190
191 READ_ATTR(udev, uint16_t, sdev, idVendor, "%04x\n");
192 READ_ATTR(udev, uint16_t, sdev, idProduct, "%04x\n");
193 READ_ATTR(udev, uint16_t, sdev, bcdDevice, "%04x\n");
194
195 READ_ATTR(udev, uint8_t, sdev, bConfigurationValue, "%02x\n");
196 READ_ATTR(udev, uint8_t, sdev, bNumConfigurations, "%02x\n");
197 READ_ATTR(udev, uint8_t, sdev, bNumInterfaces, "%02x\n");
198
199 READ_ATTR(udev, uint8_t, sdev, devnum, "%d\n");
200 udev->speed = read_attr_speed(sdev);
201
Valentina Manea021aed82014-03-08 14:53:26 +0200202 path = udev_device_get_syspath(sdev);
203 name = udev_device_get_sysname(sdev);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700204
Valentina Manea021aed82014-03-08 14:53:26 +0200205 strncpy(udev->path, path, SYSFS_PATH_MAX);
206 strncpy(udev->busid, name, SYSFS_BUS_ID_SIZE);
207
208 sscanf(name, "%u-%u", &busnum, &devnum);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700209 udev->busnum = busnum;
210
211 return 0;
212}
213
matt mooney35dd0c22011-05-27 01:44:14 -0700214int read_usb_interface(struct usbip_usb_device *udev, int i,
215 struct usbip_usb_interface *uinf)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700216{
217 char busid[SYSFS_BUS_ID_SIZE];
Jonathan Dietere5dfa3f2017-02-27 10:31:03 +0200218 int size;
Valentina Manea021aed82014-03-08 14:53:26 +0200219 struct udev_device *sif;
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700220
Jonathan Dietere5dfa3f2017-02-27 10:31:03 +0200221 size = snprintf(busid, sizeof(busid), "%s:%d.%d",
222 udev->busid, udev->bConfigurationValue, i);
223 if (size < 0 || (unsigned int)size >= sizeof(busid)) {
224 err("busid length %i >= %lu or < 0", size,
225 (long unsigned)sizeof(busid));
226 return -1;
227 }
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700228
Valentina Manea021aed82014-03-08 14:53:26 +0200229 sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700230 if (!sif) {
Valentina Manea021aed82014-03-08 14:53:26 +0200231 err("udev_device_new_from_subsystem_sysname %s failed", busid);
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700232 return -1;
233 }
234
235 READ_ATTR(uinf, uint8_t, sif, bInterfaceClass, "%02x\n");
236 READ_ATTR(uinf, uint8_t, sif, bInterfaceSubClass, "%02x\n");
237 READ_ATTR(uinf, uint8_t, sif, bInterfaceProtocol, "%02x\n");
238
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700239 return 0;
240}
241
242int usbip_names_init(char *f)
243{
244 return names_init(f);
245}
246
Pawel Lebioda19495512014-05-14 21:28:27 +0200247void usbip_names_free(void)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700248{
249 names_free();
250}
251
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +0100252void usbip_names_get_product(char *buff, size_t size, uint16_t vendor,
253 uint16_t product)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700254{
255 const char *prod, *vend;
256
257 prod = names_product(vendor, product);
258 if (!prod)
259 prod = "unknown product";
260
261
262 vend = names_vendor(vendor);
263 if (!vend)
264 vend = "unknown vendor";
265
266 snprintf(buff, size, "%s : %s (%04x:%04x)", vend, prod, vendor, product);
267}
268
Kurt Kanzenbach9db91e12013-02-22 12:13:29 +0100269void usbip_names_get_class(char *buff, size_t size, uint8_t class,
270 uint8_t subclass, uint8_t protocol)
Takahiro Hirofuchi0945b4f2011-05-14 03:55:07 -0700271{
272 const char *c, *s, *p;
273
274 if (class == 0 && subclass == 0 && protocol == 0) {
275 snprintf(buff, size, "(Defined at Interface level) (%02x/%02x/%02x)", class, subclass, protocol);
276 return;
277 }
278
279 p = names_protocol(class, subclass, protocol);
280 if (!p)
281 p = "unknown protocol";
282
283 s = names_subclass(class, subclass);
284 if (!s)
285 s = "unknown subclass";
286
287 c = names_class(class);
288 if (!c)
289 c = "unknown class";
290
291 snprintf(buff, size, "%s / %s / %s (%02x/%02x/%02x)", c, s, p, class, subclass, protocol);
292}