blob: a4a496c9cbaff8a4733c02c02ab4010eb642b263 [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 Manead561ef92014-03-08 14:53:21 +020019#include <libudev.h>
matt mooney9cda5702011-06-19 22:44:43 -070020
21#include <errno.h>
matt mooneye9837bb2011-05-26 06:17:11 -070022#include <stdio.h>
matt mooney9cda5702011-06-19 22:44:43 -070023#include <string.h>
matt mooneye9837bb2011-05-26 06:17:11 -070024
25#include <getopt.h>
matt mooneye9837bb2011-05-26 06:17:11 -070026
27#include "usbip_common.h"
28#include "utils.h"
29#include "usbip.h"
Valentina Manead561ef92014-03-08 14:53:21 +020030#include "sysfs_utils.h"
matt mooneye9837bb2011-05-26 06:17:11 -070031
32static const char usbip_unbind_usage_string[] =
33 "usbip unbind <args>\n"
34 " -b, --busid=<busid> Unbind " USBIP_HOST_DRV_NAME ".ko from "
35 "device on <busid>\n";
36
37void usbip_unbind_usage(void)
38{
39 printf("usage: %s", usbip_unbind_usage_string);
40}
41
matt mooney9cda5702011-06-19 22:44:43 -070042static int unbind_device(char *busid)
matt mooneye9837bb2011-05-26 06:17:11 -070043{
matt mooney9cda5702011-06-19 22:44:43 -070044 char bus_type[] = "usb";
matt mooney9cda5702011-06-19 22:44:43 -070045 int rc, ret = -1;
matt mooneye9837bb2011-05-26 06:17:11 -070046
Valentina Maneaa46034c2014-03-08 14:53:33 +020047 char unbind_attr_name[] = "unbind";
Valentina Maneab7945b72014-01-23 23:12:29 +020048 char unbind_attr_path[SYSFS_PATH_MAX];
Valentina Maneaa46034c2014-03-08 14:53:33 +020049 char rebind_attr_name[] = "rebind";
50 char rebind_attr_path[SYSFS_PATH_MAX];
matt mooney9cda5702011-06-19 22:44:43 -070051
Valentina Manead561ef92014-03-08 14:53:21 +020052 struct udev *udev;
53 struct udev_device *dev;
54 const char *driver;
55
56 /* Create libudev context. */
57 udev = udev_new();
58
59 /* Check whether the device with this bus ID exists. */
60 dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
61 if (!dev) {
62 err("device with the specified bus ID does not exist");
63 goto err_close_udev;
matt mooneye9837bb2011-05-26 06:17:11 -070064 }
65
Valentina Manead561ef92014-03-08 14:53:21 +020066 /* Check whether the device is using usbip-host driver. */
67 driver = udev_device_get_driver(dev);
68 if (!driver || strcmp(driver, "usbip-host")) {
69 err("device is not bound to usbip-host driver");
70 goto err_close_udev;
matt mooney9cda5702011-06-19 22:44:43 -070071 }
72
Valentina Manead561ef92014-03-08 14:53:21 +020073 /* Unbind device from driver. */
Valentina Maneab7945b72014-01-23 23:12:29 +020074 snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
Valentina Manead561ef92014-03-08 14:53:21 +020075 SYSFS_MNT_PATH, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
Valentina Maneaa46034c2014-03-08 14:53:33 +020076 USBIP_HOST_DRV_NAME, unbind_attr_name);
matt mooney9cda5702011-06-19 22:44:43 -070077
Valentina Manead561ef92014-03-08 14:53:21 +020078 rc = write_sysfs_attribute(unbind_attr_path, busid, strlen(busid));
79 if (rc < 0) {
80 err("error unbinding device %s from driver", busid);
81 goto err_close_udev;
matt mooney9cda5702011-06-19 22:44:43 -070082 }
83
Valentina Manead561ef92014-03-08 14:53:21 +020084 /* Notify driver of unbind. */
matt mooneye9837bb2011-05-26 06:17:11 -070085 rc = modify_match_busid(busid, 0);
86 if (rc < 0) {
matt mooney9cda5702011-06-19 22:44:43 -070087 err("unable to unbind device on %s", busid);
Valentina Manead561ef92014-03-08 14:53:21 +020088 goto err_close_udev;
matt mooney9cda5702011-06-19 22:44:43 -070089 }
90
Valentina Maneaa46034c2014-03-08 14:53:33 +020091 /* Trigger new probing. */
92 snprintf(rebind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
93 SYSFS_MNT_PATH, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
94 USBIP_HOST_DRV_NAME, rebind_attr_name);
95
96 rc = write_sysfs_attribute(rebind_attr_path, busid, strlen(busid));
97 if (rc < 0) {
98 err("error rebinding");
99 goto err_close_udev;
100 }
101
matt mooney9cda5702011-06-19 22:44:43 -0700102 ret = 0;
Valentina Manead561ef92014-03-08 14:53:21 +0200103 info("unbind device on busid %s: complete", busid);
matt mooneye9837bb2011-05-26 06:17:11 -0700104
Valentina Manead561ef92014-03-08 14:53:21 +0200105err_close_udev:
106 udev_device_unref(dev);
107 udev_unref(udev);
matt mooney9cda5702011-06-19 22:44:43 -0700108
109 return ret;
matt mooneye9837bb2011-05-26 06:17:11 -0700110}
111
112int usbip_unbind(int argc, char *argv[])
113{
114 static const struct option opts[] = {
115 { "busid", required_argument, NULL, 'b' },
matt mooney9cda5702011-06-19 22:44:43 -0700116 { NULL, 0, NULL, 0 }
matt mooneye9837bb2011-05-26 06:17:11 -0700117 };
matt mooney9cda5702011-06-19 22:44:43 -0700118
matt mooneye9837bb2011-05-26 06:17:11 -0700119 int opt;
120 int ret = -1;
121
122 for (;;) {
123 opt = getopt_long(argc, argv, "b:", opts, NULL);
124
125 if (opt == -1)
126 break;
127
128 switch (opt) {
129 case 'b':
matt mooney9cda5702011-06-19 22:44:43 -0700130 ret = unbind_device(optarg);
matt mooneye9837bb2011-05-26 06:17:11 -0700131 goto out;
132 default:
133 goto err_out;
134 }
135 }
136
137err_out:
138 usbip_unbind_usage();
139out:
140 return ret;
141}