vxlan: check return value of gro_cells_init()
[linux/fpc-iii.git] / tools / usb / usbip / libsrc / usbip_device_driver.c
blob67ae6c1557b8cf9a31acf28a59dfe80a1be58add
1 /*
2 * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
3 * 2015 Samsung Electronics
4 * Author: Igor Kotrasinski <i.kotrasinsk@samsung.com>
6 * Based on tools/usb/usbip/libsrc/usbip_host_driver.c, which is:
7 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
8 * 2005-2007 Takahiro Hirofuchi
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <fcntl.h>
25 #include <string.h>
26 #include <linux/usb/ch9.h>
28 #include <unistd.h>
30 #include "usbip_host_common.h"
31 #include "usbip_device_driver.h"
33 #undef PROGNAME
34 #define PROGNAME "libusbip"
36 #define copy_descr_attr16(dev, descr, attr) \
37 ((dev)->attr = le16toh((descr)->attr)) \
39 #define copy_descr_attr(dev, descr, attr) \
40 ((dev)->attr = (descr)->attr) \
42 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
44 static struct {
45 enum usb_device_speed speed;
46 const char *name;
47 } speed_names[] = {
49 .speed = USB_SPEED_UNKNOWN,
50 .name = "UNKNOWN",
53 .speed = USB_SPEED_LOW,
54 .name = "low-speed",
57 .speed = USB_SPEED_FULL,
58 .name = "full-speed",
61 .speed = USB_SPEED_HIGH,
62 .name = "high-speed",
65 .speed = USB_SPEED_WIRELESS,
66 .name = "wireless",
69 .speed = USB_SPEED_SUPER,
70 .name = "super-speed",
74 static
75 int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
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 size_t ret;
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 != 1) {
95 err("Cannot read vudc device descr file: %s", strerror(errno));
96 goto err;
98 fclose(fd);
100 copy_descr_attr(dev, &descr, bDeviceClass);
101 copy_descr_attr(dev, &descr, bDeviceSubClass);
102 copy_descr_attr(dev, &descr, bDeviceProtocol);
103 copy_descr_attr(dev, &descr, bNumConfigurations);
104 copy_descr_attr16(dev, &descr, idVendor);
105 copy_descr_attr16(dev, &descr, idProduct);
106 copy_descr_attr16(dev, &descr, bcdDevice);
108 strncpy(dev->path, path, SYSFS_PATH_MAX);
110 dev->speed = USB_SPEED_UNKNOWN;
111 speed = udev_device_get_sysattr_value(sdev, "current_speed");
112 if (speed) {
113 for (i = 0; i < ARRAY_SIZE(speed_names); i++) {
114 if (!strcmp(speed_names[i].name, speed)) {
115 dev->speed = speed_names[i].speed;
116 break;
121 /* Only used for user output, little sense to output them in general */
122 dev->bNumInterfaces = 0;
123 dev->bConfigurationValue = 0;
124 dev->busnum = 0;
126 name = udev_device_get_sysname(plat);
127 strncpy(dev->busid, name, SYSFS_BUS_ID_SIZE);
128 return 0;
129 err:
130 fclose(fd);
131 return -1;
134 static int is_my_device(struct udev_device *dev)
136 const char *driver;
138 driver = udev_device_get_property_value(dev, "USB_UDC_NAME");
139 return driver != NULL && !strcmp(driver, USBIP_DEVICE_DRV_NAME);
142 static int usbip_device_driver_open(struct usbip_host_driver *hdriver)
144 int ret;
146 hdriver->ndevs = 0;
147 INIT_LIST_HEAD(&hdriver->edev_list);
149 ret = usbip_generic_driver_open(hdriver);
150 if (ret)
151 err("please load " USBIP_CORE_MOD_NAME ".ko and "
152 USBIP_DEVICE_DRV_NAME ".ko!");
154 return ret;
157 struct usbip_host_driver device_driver = {
158 .edev_list = LIST_HEAD_INIT(device_driver.edev_list),
159 .udev_subsystem = "udc",
160 .ops = {
161 .open = usbip_device_driver_open,
162 .close = usbip_generic_driver_close,
163 .refresh_device_list = usbip_generic_refresh_device_list,
164 .get_device = usbip_generic_get_device,
165 .read_device = read_usb_vudc_device,
166 .is_my_device = is_my_device,