1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <ctype.h>
20#include <limits.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <getopt.h>
27#include <unistd.h>
28
29#include "vhci_driver.h"
30#include "usbip_common.h"
31#include "usbip_network.h"
32#include "usbip.h"
33
34static const char usbip_detach_usage_string[] =
35 "usbip detach <args>\n"
36 " -p, --port=<port> " USBIP_VHCI_DRV_NAME
37 " port the device is on\n";
38
39void usbip_detach_usage(void)
40{
41 printf("usage: %s", usbip_detach_usage_string);
42}
43
44static int detach_port(char *port)
45{
46 int ret;
47 uint8_t portnum;
48 char path[PATH_MAX+1];
49
50 unsigned int port_len = strlen(port);
51
52 for (unsigned int i = 0; i < port_len; i++)
53 if (!isdigit(port[i])) {
54 err("invalid port %s", port);
55 return -1;
56 }
57
58
59
60 portnum = atoi(port);
61
62
63
64 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum);
65
66 remove(path);
67 rmdir(VHCI_STATE_PATH);
68
69 ret = usbip_vhci_driver_open();
70 if (ret < 0) {
71 err("open vhci_driver");
72 return -1;
73 }
74
75 ret = usbip_vhci_detach_device(portnum);
76 if (ret < 0)
77 return -1;
78
79 usbip_vhci_driver_close();
80
81 return ret;
82}
83
84int usbip_detach(int argc, char *argv[])
85{
86 static const struct option opts[] = {
87 { "port", required_argument, NULL, 'p' },
88 { NULL, 0, NULL, 0 }
89 };
90 int opt;
91 int ret = -1;
92
93 for (;;) {
94 opt = getopt_long(argc, argv, "p:", opts, NULL);
95
96 if (opt == -1)
97 break;
98
99 switch (opt) {
100 case 'p':
101 ret = detach_port(optarg);
102 goto out;
103 default:
104 goto err_out;
105 }
106 }
107
108err_out:
109 usbip_detach_usage();
110out:
111 return ret;
112}
113