blob: e121cfb1746a4c7c4bf6d4e501d119fbee4bc60e [file] [log] [blame]
matt mooneye9837bb2011-05-26 06:17:11 -07001/*
2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
Valentina Manea1e940312014-03-08 14:53:19 +020019#include <libudev.h>
matt mooneye9837bb2011-05-26 06:17:11 -070020
matt mooney06c465f2011-06-19 22:44:42 -070021#include <errno.h>
matt mooneye9837bb2011-05-26 06:17:11 -070022#include <stdio.h>
matt mooney06c465f2011-06-19 22:44:42 -070023#include <stdlib.h>
matt mooneye9837bb2011-05-26 06:17:11 -070024#include <string.h>
25
matt mooney4cbab522011-05-27 01:44:11 -070026#include <getopt.h>
matt mooneye9837bb2011-05-26 06:17:11 -070027
28#include "usbip_common.h"
29#include "utils.h"
30#include "usbip.h"
Valentina Manea1e940312014-03-08 14:53:19 +020031#include "sysfs_utils.h"
matt mooneye9837bb2011-05-26 06:17:11 -070032
matt mooney06c465f2011-06-19 22:44:42 -070033enum unbind_status {
34 UNBIND_ST_OK,
35 UNBIND_ST_USBIP_HOST,
36 UNBIND_ST_FAILED
37};
38
matt mooneye9837bb2011-05-26 06:17:11 -070039static const char usbip_bind_usage_string[] =
40 "usbip bind <args>\n"
41 " -b, --busid=<busid> Bind " USBIP_HOST_DRV_NAME ".ko to device "
42 "on <busid>\n";
43
44void usbip_bind_usage(void)
45{
46 printf("usage: %s", usbip_bind_usage_string);
47}
48
matt mooney06c465f2011-06-19 22:44:42 -070049/* call at unbound state */
50static int bind_usbip(char *busid)
51{
matt mooney06c465f2011-06-19 22:44:42 -070052 char attr_name[] = "bind";
matt mooney06c465f2011-06-19 22:44:42 -070053 char bind_attr_path[SYSFS_PATH_MAX];
Valentina Manea1e940312014-03-08 14:53:19 +020054 int rc = -1;
matt mooney06c465f2011-06-19 22:44:42 -070055
56 snprintf(bind_attr_path, sizeof(bind_attr_path), "%s/%s/%s/%s/%s/%s",
Valentina Manea1e940312014-03-08 14:53:19 +020057 SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE,
58 SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME, attr_name);
matt mooney06c465f2011-06-19 22:44:42 -070059
Valentina Manea1e940312014-03-08 14:53:19 +020060 rc = write_sysfs_attribute(bind_attr_path, busid, strlen(busid));
61 if (rc < 0) {
62 err("error binding device %s to driver: %s", busid,
63 strerror(errno));
matt mooney06c465f2011-06-19 22:44:42 -070064 return -1;
65 }
66
Valentina Manea1e940312014-03-08 14:53:19 +020067 return 0;
matt mooney06c465f2011-06-19 22:44:42 -070068}
matt mooneye9837bb2011-05-26 06:17:11 -070069
70/* buggy driver may cause dead lock */
matt mooney06c465f2011-06-19 22:44:42 -070071static int unbind_other(char *busid)
matt mooneye9837bb2011-05-26 06:17:11 -070072{
matt mooney06c465f2011-06-19 22:44:42 -070073 enum unbind_status status = UNBIND_ST_OK;
matt mooneye9837bb2011-05-26 06:17:11 -070074
Valentina Manea1e940312014-03-08 14:53:19 +020075 char attr_name[] = "unbind";
76 char unbind_attr_path[SYSFS_PATH_MAX];
77 int rc = -1;
matt mooneye9837bb2011-05-26 06:17:11 -070078
Valentina Manea1e940312014-03-08 14:53:19 +020079 struct udev *udev;
80 struct udev_device *dev;
81 const char *driver;
82 const char *bDevClass;
83
84 /* Create libudev context. */
85 udev = udev_new();
86
87 /* Get the device. */
88 dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
89 if (!dev) {
90 dbg("unable to find device with bus ID %s", busid);
matt mooney06c465f2011-06-19 22:44:42 -070091 goto err_close_busid_dev;
matt mooneye9837bb2011-05-26 06:17:11 -070092 }
93
Valentina Manea1e940312014-03-08 14:53:19 +020094 /* Check what kind of device it is. */
95 bDevClass = udev_device_get_sysattr_value(dev, "bDeviceClass");
96 if (!bDevClass) {
97 dbg("unable to get bDevClass device attribute");
98 goto err_close_busid_dev;
99 }
100
101 if (!strncmp(bDevClass, "09", strlen(bDevClass))) {
matt mooneye9837bb2011-05-26 06:17:11 -0700102 dbg("skip unbinding of hub");
matt mooney06c465f2011-06-19 22:44:42 -0700103 goto err_close_busid_dev;
matt mooneye9837bb2011-05-26 06:17:11 -0700104 }
105
Valentina Manea1e940312014-03-08 14:53:19 +0200106 /* Get the device driver. */
107 driver = udev_device_get_driver(dev);
108 if (!driver) {
109 /* No driver bound to this device. */
110 goto out;
111 }
112
113 if (!strncmp(USBIP_HOST_DRV_NAME, driver,
114 strlen(USBIP_HOST_DRV_NAME))) {
115 /* Already bound to usbip-host. */
116 status = UNBIND_ST_USBIP_HOST;
117 goto out;
118 }
119
120 /* Unbind device from driver. */
121 snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
122 SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE,
123 SYSFS_DRIVERS_NAME, driver, attr_name);
124
125 rc = write_sysfs_attribute(unbind_attr_path, busid, strlen(busid));
126 if (rc < 0) {
127 err("error unbinding device %s from driver", busid);
Valentina Maneab7945b72014-01-23 23:12:29 +0200128 goto err_close_busid_dev;
matt mooneye9837bb2011-05-26 06:17:11 -0700129 }
130
matt mooney06c465f2011-06-19 22:44:42 -0700131 goto out;
132
matt mooney06c465f2011-06-19 22:44:42 -0700133err_close_busid_dev:
134 status = UNBIND_ST_FAILED;
135out:
Valentina Manea1e940312014-03-08 14:53:19 +0200136 udev_device_unref(dev);
137 udev_unref(udev);
matt mooney06c465f2011-06-19 22:44:42 -0700138
139 return status;
matt mooneye9837bb2011-05-26 06:17:11 -0700140}
141
matt mooney06c465f2011-06-19 22:44:42 -0700142static int bind_device(char *busid)
matt mooneye9837bb2011-05-26 06:17:11 -0700143{
matt mooney06c465f2011-06-19 22:44:42 -0700144 int rc;
Valentina Manea1e940312014-03-08 14:53:19 +0200145 struct udev *udev;
146 struct udev_device *dev;
Shuah Khan4c6fcc32018-01-17 12:07:30 -0700147 const char *devpath;
Valentina Manea1e940312014-03-08 14:53:19 +0200148
149 /* Check whether the device with this bus ID exists. */
150 udev = udev_new();
151 dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
152 if (!dev) {
153 err("device with the specified bus ID does not exist");
154 return -1;
155 }
Shuah Khan4c6fcc32018-01-17 12:07:30 -0700156 devpath = udev_device_get_devpath(dev);
Valentina Manea1e940312014-03-08 14:53:19 +0200157 udev_unref(udev);
matt mooneye9837bb2011-05-26 06:17:11 -0700158
Shuah Khan4c6fcc32018-01-17 12:07:30 -0700159 /* If the device is already attached to vhci_hcd - bail out */
160 if (strstr(devpath, USBIP_VHCI_DRV_NAME)) {
161 err("bind loop detected: device: %s is attached to %s\n",
162 devpath, USBIP_VHCI_DRV_NAME);
163 return -1;
164 }
165
matt mooney06c465f2011-06-19 22:44:42 -0700166 rc = unbind_other(busid);
167 if (rc == UNBIND_ST_FAILED) {
168 err("could not unbind driver from device on busid %s", busid);
169 return -1;
170 } else if (rc == UNBIND_ST_USBIP_HOST) {
171 err("device on busid %s is already bound to %s", busid,
172 USBIP_HOST_DRV_NAME);
matt mooneye9837bb2011-05-26 06:17:11 -0700173 return -1;
174 }
175
matt mooney06c465f2011-06-19 22:44:42 -0700176 rc = modify_match_busid(busid, 1);
177 if (rc < 0) {
178 err("unable to bind device on %s", busid);
matt mooneye9837bb2011-05-26 06:17:11 -0700179 return -1;
180 }
181
matt mooney06c465f2011-06-19 22:44:42 -0700182 rc = bind_usbip(busid);
183 if (rc < 0) {
184 err("could not bind device to %s", USBIP_HOST_DRV_NAME);
matt mooneye9837bb2011-05-26 06:17:11 -0700185 modify_match_busid(busid, 0);
186 return -1;
187 }
188
Valentina Manea1e940312014-03-08 14:53:19 +0200189 info("bind device on busid %s: complete", busid);
matt mooneye9837bb2011-05-26 06:17:11 -0700190
191 return 0;
192}
193
194int usbip_bind(int argc, char *argv[])
195{
196 static const struct option opts[] = {
197 { "busid", required_argument, NULL, 'b' },
matt mooney06c465f2011-06-19 22:44:42 -0700198 { NULL, 0, NULL, 0 }
matt mooneye9837bb2011-05-26 06:17:11 -0700199 };
matt mooney06c465f2011-06-19 22:44:42 -0700200
matt mooneye9837bb2011-05-26 06:17:11 -0700201 int opt;
202 int ret = -1;
203
204 for (;;) {
205 opt = getopt_long(argc, argv, "b:", opts, NULL);
206
207 if (opt == -1)
208 break;
209
210 switch (opt) {
211 case 'b':
matt mooney06c465f2011-06-19 22:44:42 -0700212 ret = bind_device(optarg);
matt mooneye9837bb2011-05-26 06:17:11 -0700213 goto out;
214 default:
215 goto err_out;
216 }
217 }
218
219err_out:
220 usbip_bind_usage();
221out:
222 return ret;
223}