Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 | /*
*
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/
#include "utils.h"
#define _GNU_SOURCE
#include <getopt.h>
#include <glib.h>
static const struct option longopts[] = {
{"usbip", required_argument, NULL, 'u'},
{"other", required_argument, NULL, 'o'},
{"list", no_argument, NULL, 'l'},
{"list2", no_argument, NULL, 'L'},
{"help", no_argument, NULL, 'h'},
#if 0
{"allusbip", no_argument, NULL, 'a'},
{"export-to", required_argument, NULL, 'e'},
{"unexport", required_argument, NULL, 'x'},
{"busid", required_argument, NULL, 'b'},
#endif
{NULL, 0, NULL, 0}
};
static const char match_busid_path[] = "/sys/bus/usb/drivers/usbip/match_busid";
static void show_help(void)
{
printf("Usage: usbip_bind_driver [OPTION]\n");
printf("Change driver binding for USB/IP.\n");
printf(" --usbip busid make a device exportable\n");
printf(" --other busid use a device by a local driver\n");
printf(" --list print usb devices and their drivers\n");
printf(" --list2 print usb devices and their drivers in parseable mode\n");
#if 0
printf(" --allusbip make all devices exportable\n");
printf(" --export-to host export the device to 'host'\n");
printf(" --unexport host unexport a device previously exported to 'host'\n");
printf(" --busid busid the busid used for --export-to\n");
#endif
}
static int modify_match_busid(char *busid, int add)
{
int fd;
int ret;
char buff[BUS_ID_SIZE + 4];
/* BUS_IS_SIZE includes NULL termination? */
if (strnlen(busid, BUS_ID_SIZE) > BUS_ID_SIZE - 1) {
g_warning("too long busid");
return -1;
}
fd = open(match_busid_path, O_WRONLY);
if (fd < 0)
return -1;
if (add)
snprintf(buff, BUS_ID_SIZE + 4, "add %s", busid);
else
snprintf(buff, BUS_ID_SIZE + 4, "del %s", busid);
g_debug("write \"%s\" to %s", buff, match_busid_path);
ret = write(fd, buff, sizeof(buff));
if (ret < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
}
static const char unbind_path_format[] = "/sys/bus/usb/devices/%s/driver/unbind";
/* buggy driver may cause dead lock */
static int unbind_interface_busid(char *busid)
{
char unbind_path[PATH_MAX];
int fd;
int ret;
snprintf(unbind_path, sizeof(unbind_path), unbind_path_format, busid);
fd = open(unbind_path, O_WRONLY);
if (fd < 0) {
g_warning("opening unbind_path failed: %d", fd);
return -1;
}
ret = write(fd, busid, strnlen(busid, BUS_ID_SIZE));
if (ret < 0) {
g_warning("write to unbind_path failed: %d", ret);
close(fd);
return -1;
}
close(fd);
return 0;
}
static int unbind_interface(char *busid, int configvalue, int interface)
{
char inf_busid[BUS_ID_SIZE];
g_debug("unbinding interface");
snprintf(inf_busid, BUS_ID_SIZE, "%s:%d.%d", busid, configvalue, interface);
return unbind_interface_busid(inf_busid);
}
static const char bind_path_format[] = "/sys/bus/usb/drivers/%s/bind";
static int bind_interface_busid(char *busid, char *driver)
{
char bind_path[PATH_MAX];
int fd;
int ret;
snprintf(bind_path, sizeof(bind_path), bind_path_format, driver);
fd = open(bind_path, O_WRONLY);
if (fd < 0)
return -1;
ret = write(fd, busid, strnlen(busid, BUS_ID_SIZE));
if (ret < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
}
static int bind_interface(char *busid, int configvalue, int interface, char *driver)
{
char inf_busid[BUS_ID_SIZE];
snprintf(inf_busid, BUS_ID_SIZE, "%s:%d.%d", busid, configvalue, interface);
return bind_interface_busid(inf_busid, driver);
}
static int unbind(char *busid)
{
int configvalue = 0;
int ninterface = 0;
int devclass = 0;
int i;
int failed = 0;
configvalue = read_bConfigurationValue(busid);
ninterface = read_bNumInterfaces(busid);
devclass = read_bDeviceClass(busid);
if (configvalue < 0 || ninterface < 0 || devclass < 0) {
g_warning("read config and ninf value, removed?");
return -1;
}
if (devclass == 0x09) {
g_message("skip unbinding of hub");
return -1;
}
for (i = 0; i < ninterface; i++) {
char driver[PATH_MAX];
int ret;
bzero(&driver, sizeof(driver));
getdriver(busid, configvalue, i, driver, PATH_MAX-1);
g_debug(" %s:%d.%d -> %s ", busid, configvalue, i, driver);
if (!strncmp("none", driver, PATH_MAX))
continue; /* unbound interface */
#if 0
if (!strncmp("usbip", driver, PATH_MAX))
continue; /* already bound to usbip */
#endif
/* unbinding */
ret = unbind_interface(busid, configvalue, i);
if (ret < 0) {
g_warning("unbind driver at %s:%d.%d failed",
busid, configvalue, i);
failed = 1;
}
}
if (failed)
return -1;
else
return 0;
}
/* call at unbound state */
static int bind_to_usbip(char *busid)
{
int configvalue = 0;
int ninterface = 0;
int i;
int failed = 0;
configvalue = read_bConfigurationValue(busid);
ninterface = read_bNumInterfaces(busid);
if (configvalue < 0 || ninterface < 0) {
g_warning("read config and ninf value, removed?");
return -1;
}
for (i = 0; i < ninterface; i++) {
int ret;
ret = bind_interface(busid, configvalue, i, "usbip");
if (ret < 0) {
g_warning("bind usbip at %s:%d.%d, failed",
busid, configvalue, i);
failed = 1;
/* need to contine binding at other interfaces */
}
}
if (failed)
return -1;
else
return 0;
}
static int use_device_by_usbip(char *busid)
{
int ret;
ret = unbind(busid);
if (ret < 0) {
g_warning("unbind drivers of %s, failed", busid);
return -1;
}
ret = modify_match_busid(busid, 1);
if (ret < 0) {
g_warning("add %s to match_busid, failed", busid);
return -1;
}
ret = bind_to_usbip(busid);
if (ret < 0) {
g_warning("bind usbip to %s, failed", busid);
modify_match_busid(busid, 0);
return -1;
}
g_message("bind %s to usbip, complete!", busid);
return 0;
}
static int use_device_by_other(char *busid)
{
int ret;
int config;
/* read and write the same config value to kick probing */
config = read_bConfigurationValue(busid);
if (config < 0) {
g_warning("read bConfigurationValue of %s, failed", busid);
return -1;
}
ret = modify_match_busid(busid, 0);
if (ret < 0) {
g_warning("del %s to match_busid, failed", busid);
return -1;
}
ret = write_bConfigurationValue(busid, config);
if (ret < 0) {
g_warning("read bConfigurationValue of %s, failed", busid);
return -1;
}
g_message("bind %s to other drivers than usbip, complete!", busid);
return 0;
}
#include <sys/types.h>
#include <regex.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
static int is_usb_device(char *busid)
{
int ret;
regex_t regex;
regmatch_t pmatch[1];
ret = regcomp(®ex, "^[0-9]+-[0-9]+(\\.[0-9]+)*$", REG_NOSUB|REG_EXTENDED);
if (ret < 0)
g_error("regcomp: %s\n", strerror(errno));
ret = regexec(®ex, busid, 0, pmatch, 0);
if (ret)
return 0; /* not matched */
return 1;
}
#include <dirent.h>
static int show_devices(void)
{
DIR *dir;
dir = opendir("/sys/bus/usb/devices/");
if (!dir)
g_error("opendir: %s", strerror(errno));
printf("List USB devices\n");
for (;;) {
struct dirent *dirent;
char *busid;
dirent = readdir(dir);
if (!dirent)
break;
busid = dirent->d_name;
if (is_usb_device(busid)) {
char name[100] = {'\0'};
char driver[100] = {'\0'};
int conf, ninf = 0;
int i;
conf = read_bConfigurationValue(busid);
ninf = read_bNumInterfaces(busid);
getdevicename(busid, name, sizeof(name));
printf(" - busid %s (%s)\n", busid, name);
for (i = 0; i < ninf; i++) {
getdriver(busid, conf, i, driver, sizeof(driver));
printf(" %s:%d.%d -> %s\n", busid, conf, i, driver);
}
printf("\n");
}
}
closedir(dir);
return 0;
}
static int show_devices2(void)
{
DIR *dir;
dir = opendir("/sys/bus/usb/devices/");
if (!dir)
g_error("opendir: %s", strerror(errno));
for (;;) {
struct dirent *dirent;
char *busid;
dirent = readdir(dir);
if (!dirent)
break;
busid = dirent->d_name;
if (is_usb_device(busid)) {
char name[100] = {'\0'};
char driver[100] = {'\0'};
int conf, ninf = 0;
int i;
conf = read_bConfigurationValue(busid);
ninf = read_bNumInterfaces(busid);
getdevicename(busid, name, sizeof(name));
printf("busid=%s#usbid=%s#", busid, name);
for (i = 0; i < ninf; i++) {
getdriver(busid, conf, i, driver, sizeof(driver));
printf("%s:%d.%d=%s#", busid, conf, i, driver);
}
printf("\n");
}
}
closedir(dir);
return 0;
}
#if 0
static int export_to(char *host, char *busid) {
int ret;
if( host == NULL ) {
printf( "no host given\n\n");
show_help();
return -1;
}
if( busid == NULL ) {
/* XXX print device list and ask for busnumber, if none is
* given */
printf( "no busid given, use --busid switch\n\n");
show_help();
return -1;
}
ret = use_device_by_usbip(busid);
if( ret != 0 ) {
printf( "could not bind driver to usbip\n");
return -1;
}
printf( "DEBUG: exporting device '%s' to '%s'\n", busid, host );
ret = export_busid_to_host(host, busid); /* usbip_export.[ch] */
if( ret != 0 ) {
printf( "could not export device to host\n" );
printf( " host: %s, device: %s\n", host, busid );
use_device_by_other(busid);
return -1;
}
return 0;
}
static int unexport_from(char *host, char *busid) {
int ret;
if (!host || !busid)
g_error("no host or no busid\n");
g_message("unexport_from: host: '%s', busid: '%s'", host, busid);
ret = unexport_busid_from_host(host, busid); /* usbip_export.[ch] */
if( ret != 0 ) {
err( "could not unexport device from host\n" );
err( " host: %s, device: %s\n", host, busid );
}
ret = use_device_by_other(busid);
if (ret < 0)
g_error("could not unbind device from usbip\n");
return 0;
}
static int allusbip(void)
{
DIR *dir;
dir = opendir("/sys/bus/usb/devices/");
if (!dir)
g_error("opendir: %s", strerror(errno));
for (;;) {
struct dirent *dirent;
char *busid;
dirent = readdir(dir);
if (!dirent)
break;
busid = dirent->d_name;
if (!is_usb_device(busid))
continue;
{
char name[PATH_MAX];
int conf, ninf = 0;
int i;
int be_local = 0;
conf = read_bConfigurationValue(busid);
ninf = read_bNumInterfaces(busid);
getdevicename(busid, name, sizeof(name));
for (i = 0; i < ninf; i++) {
char driver[PATH_MAX];
getdriver(busid, conf, i, driver, sizeof(driver));
#if 0
if (strncmp(driver, "usbhid", 6) == 0 || strncmp(driver, "usb-storage", 11) == 0) {
be_local = 1;
break;
}
#endif
}
if (be_local == 0)
use_device_by_usbip(busid);
}
}
closedir(dir);
return 0;
}
#endif
int main(int argc, char **argv)
{
char *busid = NULL;
char *remote_host __attribute__((unused)) = NULL;
enum {
cmd_unknown = 0,
cmd_use_by_usbip,
cmd_use_by_other,
cmd_list,
cmd_list2,
cmd_allusbip,
cmd_export_to,
cmd_unexport,
cmd_help,
} cmd = cmd_unknown;
if (geteuid() != 0)
g_warning("running non-root?");
for (;;) {
int c;
int index = 0;
c = getopt_long(argc, argv, "u:o:hlLae:x:b:", longopts, &index);
if (c == -1)
break;
switch (c) {
case 'u':
cmd = cmd_use_by_usbip;
busid = optarg;
break;
case 'o' :
cmd = cmd_use_by_other;
busid = optarg;
break;
case 'l' :
cmd = cmd_list;
break;
case 'L' :
cmd = cmd_list2;
break;
case 'a' :
cmd = cmd_allusbip;
break;
case 'b':
busid = optarg;
break;
case 'e':
cmd = cmd_export_to;
remote_host = optarg;
break;
case 'x':
cmd = cmd_unexport;
remote_host = optarg;
break;
case 'h': /* fallthrough */
case '?':
cmd = cmd_help;
break;
default:
g_error("getopt");
}
//if (cmd)
// break;
}
switch (cmd) {
case cmd_use_by_usbip:
use_device_by_usbip(busid);
break;
case cmd_use_by_other:
use_device_by_other(busid);
break;
case cmd_list:
show_devices();
break;
case cmd_list2:
show_devices2();
break;
#if 0
case cmd_allusbip:
allusbip();
break;
case cmd_export_to:
export_to(remote_host, busid);
break;
case cmd_unexport:
unexport_from(remote_host, busid);
break;
#endif
case cmd_help: /* fallthrough */
case cmd_unknown:
show_help();
break;
default:
g_error("NOT REACHED");
}
return 0;
}
|