2 * Copyright (C) 2005-2007 Takahiro Hirofuchi
5 #include "usbip_common.h"
6 #include "vhci_driver.h"
10 #include "sysfs_utils.h"
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
,
26 dbg("udev_device_new_from_subsystem_sysname failed: %s", busid
);
29 read_usb_device(sudev
, &idev
->udev
);
30 udev_device_unref(sudev
);
40 static int parse_status(const char *value
)
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');
57 int port
, status
, speed
, devid
;
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
);
66 dbg("sscanf failed: %d", ret
);
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
];
80 idev
->status
= status
;
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
);
91 dbg("imported_device_init failed");
98 /* go to the next line */
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
,
117 err("udev_device_get_sysattr_value failed");
121 return parse_status(attr_status
);
124 static int get_nports(void)
126 const char *attr_nports
;
128 attr_nports
= udev_device_get_sysattr_value(vhci_driver
->hc_device
, "nports");
130 err("udev_device_get_sysattr_value nports failed");
134 return (int)strtoul(attr_nports
, NULL
, 10);
138 * Read the given port's record.
140 * To avoid buffer overflow we will read the entire line and
141 * validate each part's size. The initial buffer is padded by 4 to
142 * accommodate the 2 spaces, 1 newline and an additional character
143 * which is needed to properly validate the 3rd part without it being
144 * truncated to an acceptable length.
146 static int read_record(int rhport
, char *host
, unsigned long host_len
,
147 char *port
, unsigned long port_len
, char *busid
)
151 char path
[PATH_MAX
+1];
152 char *buffer
, *start
, *end
;
153 char delim
[] = {' ', ' ', '\n'};
154 int max_len
[] = {(int)host_len
, (int)port_len
, SYSFS_BUS_ID_SIZE
};
155 size_t buffer_len
= host_len
+ port_len
+ SYSFS_BUS_ID_SIZE
+ 4;
157 buffer
= malloc(buffer_len
);
161 snprintf(path
, PATH_MAX
, VHCI_STATE_PATH
"/port%d", rhport
);
163 file
= fopen(path
, "r");
170 if (fgets(buffer
, buffer_len
, file
) == NULL
) {
178 /* validate the length of each of the 3 parts */
180 for (part
= 0; part
< 3; part
++) {
181 end
= strchr(start
, delim
[part
]);
182 if (end
== NULL
|| (end
- start
) > max_len
[part
]) {
189 if (sscanf(buffer
, "%s %s %s\n", host
, port
, busid
) != 3) {
200 /* ---------------------------------------------------------------------- */
202 int usbip_vhci_driver_open(void)
204 udev_context
= udev_new();
206 err("udev_new failed");
210 vhci_driver
= calloc(1, sizeof(struct usbip_vhci_driver
));
212 /* will be freed in usbip_driver_close() */
213 vhci_driver
->hc_device
=
214 udev_device_new_from_subsystem_sysname(udev_context
,
216 USBIP_VHCI_DRV_NAME
);
217 if (!vhci_driver
->hc_device
) {
218 err("udev_device_new_from_subsystem_sysname failed");
222 vhci_driver
->nports
= get_nports();
223 dbg("available ports: %d", vhci_driver
->nports
);
225 if (vhci_driver
->nports
<= 0) {
226 err("no available ports");
228 } else if (vhci_driver
->nports
> MAXNPORT
) {
229 err("port number exceeds %d", MAXNPORT
);
233 if (refresh_imported_device_list())
239 udev_device_unref(vhci_driver
->hc_device
);
246 udev_unref(udev_context
);
252 void usbip_vhci_driver_close(void)
257 udev_device_unref(vhci_driver
->hc_device
);
263 udev_unref(udev_context
);
267 int usbip_vhci_refresh_device_list(void)
270 if (refresh_imported_device_list())
275 dbg("failed to refresh device list");
280 int usbip_vhci_get_free_port(void)
282 for (int i
= 0; i
< vhci_driver
->nports
; i
++) {
283 if (vhci_driver
->idev
[i
].status
== VDEV_ST_NULL
)
290 int usbip_vhci_attach_device2(uint8_t port
, int sockfd
, uint32_t devid
,
292 char buff
[200]; /* what size should be ? */
293 char attach_attr_path
[SYSFS_PATH_MAX
];
294 char attr_attach
[] = "attach";
298 snprintf(buff
, sizeof(buff
), "%u %d %u %u",
299 port
, sockfd
, devid
, speed
);
300 dbg("writing: %s", buff
);
302 path
= udev_device_get_syspath(vhci_driver
->hc_device
);
303 snprintf(attach_attr_path
, sizeof(attach_attr_path
), "%s/%s",
305 dbg("attach attribute path: %s", attach_attr_path
);
307 ret
= write_sysfs_attribute(attach_attr_path
, buff
, strlen(buff
));
309 dbg("write_sysfs_attribute failed");
313 dbg("attached port: %d", port
);
318 static unsigned long get_devid(uint8_t busnum
, uint8_t devnum
)
320 return (busnum
<< 16) | devnum
;
323 /* will be removed */
324 int usbip_vhci_attach_device(uint8_t port
, int sockfd
, uint8_t busnum
,
325 uint8_t devnum
, uint32_t speed
)
327 int devid
= get_devid(busnum
, devnum
);
329 return usbip_vhci_attach_device2(port
, sockfd
, devid
, speed
);
332 int usbip_vhci_detach_device(uint8_t port
)
334 char detach_attr_path
[SYSFS_PATH_MAX
];
335 char attr_detach
[] = "detach";
336 char buff
[200]; /* what size should be ? */
340 snprintf(buff
, sizeof(buff
), "%u", port
);
341 dbg("writing: %s", buff
);
343 path
= udev_device_get_syspath(vhci_driver
->hc_device
);
344 snprintf(detach_attr_path
, sizeof(detach_attr_path
), "%s/%s",
346 dbg("detach attribute path: %s", detach_attr_path
);
348 ret
= write_sysfs_attribute(detach_attr_path
, buff
, strlen(buff
));
350 dbg("write_sysfs_attribute failed");
354 dbg("detached port: %d", port
);
359 int usbip_vhci_imported_device_dump(struct usbip_imported_device
*idev
)
361 char product_name
[100];
362 char host
[NI_MAXHOST
] = "unknown host";
363 char serv
[NI_MAXSERV
] = "unknown port";
364 char remote_busid
[SYSFS_BUS_ID_SIZE
];
366 int read_record_error
= 0;
368 if (idev
->status
== VDEV_ST_NULL
|| idev
->status
== VDEV_ST_NOTASSIGNED
)
371 ret
= read_record(idev
->port
, host
, sizeof(host
), serv
, sizeof(serv
),
375 read_record_error
= 1;
378 printf("Port %02d: <%s> at %s\n", idev
->port
,
379 usbip_status_string(idev
->status
),
380 usbip_speed_string(idev
->udev
.speed
));
382 usbip_names_get_product(product_name
, sizeof(product_name
),
383 idev
->udev
.idVendor
, idev
->udev
.idProduct
);
385 printf(" %s\n", product_name
);
387 if (!read_record_error
) {
388 printf("%10s -> usbip://%s:%s/%s\n", idev
->udev
.busid
,
389 host
, serv
, remote_busid
);
390 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
391 idev
->busnum
, idev
->devnum
);
393 printf("%10s -> unknown host, remote port and remote busid\n",
395 printf("%10s -> remote bus/dev %03d/%03d\n", " ",
396 idev
->busnum
, idev
->devnum
);