of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / tools / usb / usbip / libsrc / vhci_driver.c
blobad9204773533f4e3d6961dc2a9261720a39a1a44
1 /*
2 * Copyright (C) 2005-2007 Takahiro Hirofuchi
3 */
5 #include "usbip_common.h"
6 #include "vhci_driver.h"
7 #include <limits.h>
8 #include <netdb.h>
9 #include <libudev.h>
10 #include "sysfs_utils.h"
12 #undef PROGNAME
13 #define PROGNAME "libusbip"
15 struct usbip_vhci_driver *vhci_driver;
16 struct udev *udev_context;
18 static struct usbip_imported_device *
19 imported_device_init(struct usbip_imported_device *idev, char *busid)
21 struct udev_device *sudev;
23 sudev = udev_device_new_from_subsystem_sysname(udev_context,
24 "usb", busid);
25 if (!sudev) {
26 dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
27 goto err;
29 read_usb_device(sudev, &idev->udev);
30 udev_device_unref(sudev);
32 return idev;
34 err:
35 return NULL;
40 static int parse_status(const char *value)
42 int ret = 0;
43 char *c;
46 for (int i = 0; i < vhci_driver->nports; i++)
47 memset(&vhci_driver->idev[i], 0, sizeof(vhci_driver->idev[i]));
50 /* skip a header line */
51 c = strchr(value, '\n');
52 if (!c)
53 return -1;
54 c++;
56 while (*c != '\0') {
57 int port, status, speed, devid;
58 unsigned long socket;
59 char lbusid[SYSFS_BUS_ID_SIZE];
61 ret = sscanf(c, "%d %d %d %x %lx %31s\n",
62 &port, &status, &speed,
63 &devid, &socket, lbusid);
65 if (ret < 5) {
66 dbg("sscanf failed: %d", ret);
67 BUG();
70 dbg("port %d status %d speed %d devid %x",
71 port, status, speed, devid);
72 dbg("socket %lx lbusid %s", socket, lbusid);
75 /* if a device is connected, look at it */
77 struct usbip_imported_device *idev = &vhci_driver->idev[port];
79 idev->port = port;
80 idev->status = status;
82 idev->devid = devid;
84 idev->busnum = (devid >> 16);
85 idev->devnum = (devid & 0x0000ffff);
87 if (idev->status != VDEV_ST_NULL
88 && idev->status != VDEV_ST_NOTASSIGNED) {
89 idev = imported_device_init(idev, lbusid);
90 if (!idev) {
91 dbg("imported_device_init failed");
92 return -1;
98 /* go to the next line */
99 c = strchr(c, '\n');
100 if (!c)
101 break;
102 c++;
105 dbg("exit");
107 return 0;
110 static int refresh_imported_device_list(void)
112 const char *attr_status;
114 attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
115 "status");
116 if (!attr_status) {
117 err("udev_device_get_sysattr_value failed");
118 return -1;
121 return parse_status(attr_status);
124 static int get_nports(void)
126 char *c;
127 int nports = 0;
128 const char *attr_status;
130 attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
131 "status");
132 if (!attr_status) {
133 err("udev_device_get_sysattr_value failed");
134 return -1;
137 /* skip a header line */
138 c = strchr(attr_status, '\n');
139 if (!c)
140 return 0;
141 c++;
143 while (*c != '\0') {
144 /* go to the next line */
145 c = strchr(c, '\n');
146 if (!c)
147 return nports;
148 c++;
149 nports += 1;
152 return nports;
156 * Read the given port's record.
158 * To avoid buffer overflow we will read the entire line and
159 * validate each part's size. The initial buffer is padded by 4 to
160 * accommodate the 2 spaces, 1 newline and an additional character
161 * which is needed to properly validate the 3rd part without it being
162 * truncated to an acceptable length.
164 static int read_record(int rhport, char *host, unsigned long host_len,
165 char *port, unsigned long port_len, char *busid)
167 int part;
168 FILE *file;
169 char path[PATH_MAX+1];
170 char *buffer, *start, *end;
171 char delim[] = {' ', ' ', '\n'};
172 int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE};
173 size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4;
175 buffer = malloc(buffer_len);
176 if (!buffer)
177 return -1;
179 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
181 file = fopen(path, "r");
182 if (!file) {
183 err("fopen");
184 free(buffer);
185 return -1;
188 if (fgets(buffer, buffer_len, file) == NULL) {
189 err("fgets");
190 free(buffer);
191 fclose(file);
192 return -1;
194 fclose(file);
196 /* validate the length of each of the 3 parts */
197 start = buffer;
198 for (part = 0; part < 3; part++) {
199 end = strchr(start, delim[part]);
200 if (end == NULL || (end - start) > max_len[part]) {
201 free(buffer);
202 return -1;
204 start = end + 1;
207 if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) {
208 err("sscanf");
209 free(buffer);
210 return -1;
213 free(buffer);
215 return 0;
218 /* ---------------------------------------------------------------------- */
220 int usbip_vhci_driver_open(void)
222 udev_context = udev_new();
223 if (!udev_context) {
224 err("udev_new failed");
225 return -1;
228 vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
230 /* will be freed in usbip_driver_close() */
231 vhci_driver->hc_device =
232 udev_device_new_from_subsystem_sysname(udev_context,
233 USBIP_VHCI_BUS_TYPE,
234 USBIP_VHCI_DRV_NAME);
235 if (!vhci_driver->hc_device) {
236 err("udev_device_new_from_subsystem_sysname failed");
237 goto err;
240 vhci_driver->nports = get_nports();
242 dbg("available ports: %d", vhci_driver->nports);
244 if (refresh_imported_device_list())
245 goto err;
247 return 0;
249 err:
250 udev_device_unref(vhci_driver->hc_device);
252 if (vhci_driver)
253 free(vhci_driver);
255 vhci_driver = NULL;
257 udev_unref(udev_context);
259 return -1;
263 void usbip_vhci_driver_close(void)
265 if (!vhci_driver)
266 return;
268 udev_device_unref(vhci_driver->hc_device);
270 free(vhci_driver);
272 vhci_driver = NULL;
274 udev_unref(udev_context);
278 int usbip_vhci_refresh_device_list(void)
281 if (refresh_imported_device_list())
282 goto err;
284 return 0;
285 err:
286 dbg("failed to refresh device list");
287 return -1;
291 int usbip_vhci_get_free_port(void)
293 for (int i = 0; i < vhci_driver->nports; i++) {
294 if (vhci_driver->idev[i].status == VDEV_ST_NULL)
295 return i;
298 return -1;
301 int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
302 uint32_t speed) {
303 char buff[200]; /* what size should be ? */
304 char attach_attr_path[SYSFS_PATH_MAX];
305 char attr_attach[] = "attach";
306 const char *path;
307 int ret;
309 snprintf(buff, sizeof(buff), "%u %d %u %u",
310 port, sockfd, devid, speed);
311 dbg("writing: %s", buff);
313 path = udev_device_get_syspath(vhci_driver->hc_device);
314 snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s",
315 path, attr_attach);
316 dbg("attach attribute path: %s", attach_attr_path);
318 ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff));
319 if (ret < 0) {
320 dbg("write_sysfs_attribute failed");
321 return -1;
324 dbg("attached port: %d", port);
326 return 0;
329 static unsigned long get_devid(uint8_t busnum, uint8_t devnum)
331 return (busnum << 16) | devnum;
334 /* will be removed */
335 int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
336 uint8_t devnum, uint32_t speed)
338 int devid = get_devid(busnum, devnum);
340 return usbip_vhci_attach_device2(port, sockfd, devid, speed);
343 int usbip_vhci_detach_device(uint8_t port)
345 char detach_attr_path[SYSFS_PATH_MAX];
346 char attr_detach[] = "detach";
347 char buff[200]; /* what size should be ? */
348 const char *path;
349 int ret;
351 snprintf(buff, sizeof(buff), "%u", port);
352 dbg("writing: %s", buff);
354 path = udev_device_get_syspath(vhci_driver->hc_device);
355 snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s",
356 path, attr_detach);
357 dbg("detach attribute path: %s", detach_attr_path);
359 ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff));
360 if (ret < 0) {
361 dbg("write_sysfs_attribute failed");
362 return -1;
365 dbg("detached port: %d", port);
367 return 0;
370 int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
372 char product_name[100];
373 char host[NI_MAXHOST] = "unknown host";
374 char serv[NI_MAXSERV] = "unknown port";
375 char remote_busid[SYSFS_BUS_ID_SIZE];
376 int ret;
377 int read_record_error = 0;
379 if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED)
380 return 0;
382 ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv),
383 remote_busid);
384 if (ret) {
385 err("read_record");
386 read_record_error = 1;
389 printf("Port %02d: <%s> at %s\n", idev->port,
390 usbip_status_string(idev->status),
391 usbip_speed_string(idev->udev.speed));
393 usbip_names_get_product(product_name, sizeof(product_name),
394 idev->udev.idVendor, idev->udev.idProduct);
396 printf(" %s\n", product_name);
398 if (!read_record_error) {
399 printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid,
400 host, serv, remote_busid);
401 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
402 idev->busnum, idev->devnum);
403 } else {
404 printf("%10s -> unknown host, remote port and remote busid\n",
405 idev->udev.busid);
406 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
407 idev->busnum, idev->devnum);
410 return 0;