1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <fcntl.h>
25#include <string.h>
26#include <linux/usb/ch9.h>
27
28#include <unistd.h>
29
30#include "usbip_host_common.h"
31#include "usbip_device_driver.h"
32
33#undef PROGNAME
34#define PROGNAME "libusbip"
35
36#define copy_descr_attr16(dev, descr, attr) \
37 ((dev)->attr = le16toh((descr)->attr)) \
38
39#define copy_descr_attr(dev, descr, attr) \
40 ((dev)->attr = (descr)->attr) \
41
42#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
43
44static struct {
45 enum usb_device_speed speed;
46 const char *name;
47} speed_names[] = {
48 {
49 .speed = USB_SPEED_UNKNOWN,
50 .name = "UNKNOWN",
51 },
52 {
53 .speed = USB_SPEED_LOW,
54 .name = "low-speed",
55 },
56 {
57 .speed = USB_SPEED_FULL,
58 .name = "full-speed",
59 },
60 {
61 .speed = USB_SPEED_HIGH,
62 .name = "high-speed",
63 },
64 {
65 .speed = USB_SPEED_WIRELESS,
66 .name = "wireless",
67 },
68 {
69 .speed = USB_SPEED_SUPER,
70 .name = "super-speed",
71 },
72};
73
74static
75int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
76{
77 const char *path, *name;
78 char filepath[SYSFS_PATH_MAX];
79 struct usb_device_descriptor descr;
80 unsigned int i;
81 FILE *fd = NULL;
82 struct udev_device *plat;
83 const char *speed;
84 int ret = 0;
85
86 plat = udev_device_get_parent(sdev);
87 path = udev_device_get_syspath(plat);
88 snprintf(filepath, SYSFS_PATH_MAX, "%s/%s",
89 path, VUDC_DEVICE_DESCR_FILE);
90 fd = fopen(filepath, "r");
91 if (!fd)
92 return -1;
93 ret = fread((char *) &descr, sizeof(descr), 1, fd);
94 if (ret < 0)
95 goto err;
96 fclose(fd);
97
98 copy_descr_attr(dev, &descr, bDeviceClass);
99 copy_descr_attr(dev, &descr, bDeviceSubClass);
100 copy_descr_attr(dev, &descr, bDeviceProtocol);
101 copy_descr_attr(dev, &descr, bNumConfigurations);
102 copy_descr_attr16(dev, &descr, idVendor);
103 copy_descr_attr16(dev, &descr, idProduct);
104 copy_descr_attr16(dev, &descr, bcdDevice);
105
106 strncpy(dev->path, path, SYSFS_PATH_MAX);
107
108 dev->speed = USB_SPEED_UNKNOWN;
109 speed = udev_device_get_sysattr_value(sdev, "current_speed");
110 if (speed) {
111 for (i = 0; i < ARRAY_SIZE(speed_names); i++) {
112 if (!strcmp(speed_names[i].name, speed)) {
113 dev->speed = speed_names[i].speed;
114 break;
115 }
116 }
117 }
118
119
120 dev->bNumInterfaces = 0;
121 dev->bConfigurationValue = 0;
122 dev->busnum = 0;
123
124 name = udev_device_get_sysname(plat);
125 strncpy(dev->busid, name, SYSFS_BUS_ID_SIZE);
126 return 0;
127err:
128 fclose(fd);
129 return -1;
130}
131
132static int is_my_device(struct udev_device *dev)
133{
134 const char *driver;
135
136 driver = udev_device_get_property_value(dev, "USB_UDC_NAME");
137 return driver != NULL && !strcmp(driver, USBIP_DEVICE_DRV_NAME);
138}
139
140static int usbip_device_driver_open(struct usbip_host_driver *hdriver)
141{
142 int ret;
143
144 hdriver->ndevs = 0;
145 INIT_LIST_HEAD(&hdriver->edev_list);
146
147 ret = usbip_generic_driver_open(hdriver);
148 if (ret)
149 err("please load " USBIP_CORE_MOD_NAME ".ko and "
150 USBIP_DEVICE_DRV_NAME ".ko!");
151
152 return ret;
153}
154
155struct usbip_host_driver device_driver = {
156 .edev_list = LIST_HEAD_INIT(device_driver.edev_list),
157 .udev_subsystem = "udc",
158 .ops = {
159 .open = usbip_device_driver_open,
160 .close = usbip_generic_driver_close,
161 .refresh_device_list = usbip_generic_refresh_device_list,
162 .get_device = usbip_generic_get_device,
163 .read_device = read_usb_vudc_device,
164 .is_my_device = is_my_device,
165 },
166};
167