18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
48c2ecf20Sopenharmony_ci *               2005-2007 Takahiro Hirofuchi
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <libudev.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <errno.h>
108c2ecf20Sopenharmony_ci#include <stdio.h>
118c2ecf20Sopenharmony_ci#include <stdlib.h>
128c2ecf20Sopenharmony_ci#include <string.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <getopt.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "usbip_common.h"
178c2ecf20Sopenharmony_ci#include "utils.h"
188c2ecf20Sopenharmony_ci#include "usbip.h"
198c2ecf20Sopenharmony_ci#include "sysfs_utils.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cienum unbind_status {
228c2ecf20Sopenharmony_ci	UNBIND_ST_OK,
238c2ecf20Sopenharmony_ci	UNBIND_ST_USBIP_HOST,
248c2ecf20Sopenharmony_ci	UNBIND_ST_FAILED
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic const char usbip_bind_usage_string[] =
288c2ecf20Sopenharmony_ci	"usbip bind <args>\n"
298c2ecf20Sopenharmony_ci	"    -b, --busid=<busid>    Bind " USBIP_HOST_DRV_NAME ".ko to device "
308c2ecf20Sopenharmony_ci	"on <busid>\n";
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_civoid usbip_bind_usage(void)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	printf("usage: %s", usbip_bind_usage_string);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* call at unbound state */
388c2ecf20Sopenharmony_cistatic int bind_usbip(char *busid)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	char attr_name[] = "bind";
418c2ecf20Sopenharmony_ci	char bind_attr_path[SYSFS_PATH_MAX];
428c2ecf20Sopenharmony_ci	int rc = -1;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	snprintf(bind_attr_path, sizeof(bind_attr_path), "%s/%s/%s/%s/%s/%s",
458c2ecf20Sopenharmony_ci		 SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE,
468c2ecf20Sopenharmony_ci		 SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME, attr_name);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	rc = write_sysfs_attribute(bind_attr_path, busid, strlen(busid));
498c2ecf20Sopenharmony_ci	if (rc < 0) {
508c2ecf20Sopenharmony_ci		err("error binding device %s to driver: %s", busid,
518c2ecf20Sopenharmony_ci		    strerror(errno));
528c2ecf20Sopenharmony_ci		return -1;
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return 0;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* buggy driver may cause dead lock */
598c2ecf20Sopenharmony_cistatic int unbind_other(char *busid)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	enum unbind_status status = UNBIND_ST_OK;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	char attr_name[] = "unbind";
648c2ecf20Sopenharmony_ci	char unbind_attr_path[SYSFS_PATH_MAX];
658c2ecf20Sopenharmony_ci	int rc = -1;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	struct udev *udev;
688c2ecf20Sopenharmony_ci	struct udev_device *dev;
698c2ecf20Sopenharmony_ci	const char *driver;
708c2ecf20Sopenharmony_ci	const char *bDevClass;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* Create libudev context. */
738c2ecf20Sopenharmony_ci	udev = udev_new();
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* Get the device. */
768c2ecf20Sopenharmony_ci	dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
778c2ecf20Sopenharmony_ci	if (!dev) {
788c2ecf20Sopenharmony_ci		dbg("unable to find device with bus ID %s", busid);
798c2ecf20Sopenharmony_ci		goto err_close_busid_dev;
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/* Check what kind of device it is. */
838c2ecf20Sopenharmony_ci	bDevClass  = udev_device_get_sysattr_value(dev, "bDeviceClass");
848c2ecf20Sopenharmony_ci	if (!bDevClass) {
858c2ecf20Sopenharmony_ci		dbg("unable to get bDevClass device attribute");
868c2ecf20Sopenharmony_ci		goto err_close_busid_dev;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (!strncmp(bDevClass, "09", strlen(bDevClass))) {
908c2ecf20Sopenharmony_ci		dbg("skip unbinding of hub");
918c2ecf20Sopenharmony_ci		goto err_close_busid_dev;
928c2ecf20Sopenharmony_ci	}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* Get the device driver. */
958c2ecf20Sopenharmony_ci	driver = udev_device_get_driver(dev);
968c2ecf20Sopenharmony_ci	if (!driver) {
978c2ecf20Sopenharmony_ci		/* No driver bound to this device. */
988c2ecf20Sopenharmony_ci		goto out;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (!strncmp(USBIP_HOST_DRV_NAME, driver,
1028c2ecf20Sopenharmony_ci				strlen(USBIP_HOST_DRV_NAME))) {
1038c2ecf20Sopenharmony_ci		/* Already bound to usbip-host. */
1048c2ecf20Sopenharmony_ci		status = UNBIND_ST_USBIP_HOST;
1058c2ecf20Sopenharmony_ci		goto out;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* Unbind device from driver. */
1098c2ecf20Sopenharmony_ci	snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
1108c2ecf20Sopenharmony_ci		 SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE,
1118c2ecf20Sopenharmony_ci		 SYSFS_DRIVERS_NAME, driver, attr_name);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	rc = write_sysfs_attribute(unbind_attr_path, busid, strlen(busid));
1148c2ecf20Sopenharmony_ci	if (rc < 0) {
1158c2ecf20Sopenharmony_ci		err("error unbinding device %s from driver", busid);
1168c2ecf20Sopenharmony_ci		goto err_close_busid_dev;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	goto out;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cierr_close_busid_dev:
1228c2ecf20Sopenharmony_ci	status = UNBIND_ST_FAILED;
1238c2ecf20Sopenharmony_ciout:
1248c2ecf20Sopenharmony_ci	udev_device_unref(dev);
1258c2ecf20Sopenharmony_ci	udev_unref(udev);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	return status;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic int bind_device(char *busid)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	int rc;
1338c2ecf20Sopenharmony_ci	struct udev *udev;
1348c2ecf20Sopenharmony_ci	struct udev_device *dev;
1358c2ecf20Sopenharmony_ci	const char *devpath;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/* Check whether the device with this bus ID exists. */
1388c2ecf20Sopenharmony_ci	udev = udev_new();
1398c2ecf20Sopenharmony_ci	dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
1408c2ecf20Sopenharmony_ci	if (!dev) {
1418c2ecf20Sopenharmony_ci		err("device with the specified bus ID does not exist");
1428c2ecf20Sopenharmony_ci		return -1;
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci	devpath = udev_device_get_devpath(dev);
1458c2ecf20Sopenharmony_ci	udev_unref(udev);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* If the device is already attached to vhci_hcd - bail out */
1488c2ecf20Sopenharmony_ci	if (strstr(devpath, USBIP_VHCI_DRV_NAME)) {
1498c2ecf20Sopenharmony_ci		err("bind loop detected: device: %s is attached to %s\n",
1508c2ecf20Sopenharmony_ci		    devpath, USBIP_VHCI_DRV_NAME);
1518c2ecf20Sopenharmony_ci		return -1;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	rc = unbind_other(busid);
1558c2ecf20Sopenharmony_ci	if (rc == UNBIND_ST_FAILED) {
1568c2ecf20Sopenharmony_ci		err("could not unbind driver from device on busid %s", busid);
1578c2ecf20Sopenharmony_ci		return -1;
1588c2ecf20Sopenharmony_ci	} else if (rc == UNBIND_ST_USBIP_HOST) {
1598c2ecf20Sopenharmony_ci		err("device on busid %s is already bound to %s", busid,
1608c2ecf20Sopenharmony_ci		    USBIP_HOST_DRV_NAME);
1618c2ecf20Sopenharmony_ci		return -1;
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	rc = modify_match_busid(busid, 1);
1658c2ecf20Sopenharmony_ci	if (rc < 0) {
1668c2ecf20Sopenharmony_ci		err("unable to bind device on %s", busid);
1678c2ecf20Sopenharmony_ci		return -1;
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	rc = bind_usbip(busid);
1718c2ecf20Sopenharmony_ci	if (rc < 0) {
1728c2ecf20Sopenharmony_ci		err("could not bind device to %s", USBIP_HOST_DRV_NAME);
1738c2ecf20Sopenharmony_ci		modify_match_busid(busid, 0);
1748c2ecf20Sopenharmony_ci		return -1;
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	info("bind device on busid %s: complete", busid);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return 0;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ciint usbip_bind(int argc, char *argv[])
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	static const struct option opts[] = {
1858c2ecf20Sopenharmony_ci		{ "busid", required_argument, NULL, 'b' },
1868c2ecf20Sopenharmony_ci		{ NULL,    0,                 NULL,  0  }
1878c2ecf20Sopenharmony_ci	};
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	int opt;
1908c2ecf20Sopenharmony_ci	int ret = -1;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	for (;;) {
1938c2ecf20Sopenharmony_ci		opt = getopt_long(argc, argv, "b:", opts, NULL);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		if (opt == -1)
1968c2ecf20Sopenharmony_ci			break;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci		switch (opt) {
1998c2ecf20Sopenharmony_ci		case 'b':
2008c2ecf20Sopenharmony_ci			ret = bind_device(optarg);
2018c2ecf20Sopenharmony_ci			goto out;
2028c2ecf20Sopenharmony_ci		default:
2038c2ecf20Sopenharmony_ci			goto err_out;
2048c2ecf20Sopenharmony_ci		}
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cierr_out:
2088c2ecf20Sopenharmony_ci	usbip_bind_usage();
2098c2ecf20Sopenharmony_ciout:
2108c2ecf20Sopenharmony_ci	return ret;
2118c2ecf20Sopenharmony_ci}
212