diff --git a/.gitignore b/.gitignore index fb38dfce..f3e4c365 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*~ *.vcxproj.user /.vs /Debug diff --git a/userspace/src/usbip/usbip_bind.c~ b/userspace/src/usbip/usbip_bind.c~ deleted file mode 100644 index fa46141a..00000000 --- a/userspace/src/usbip/usbip_bind.c~ +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (C) 2011 matt mooney - * 2005-2007 Takahiro Hirofuchi - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include - -#include -#include -#include -#include - -#include - -#include "usbip_common.h" -#include "utils.h" -#include "usbip.h" -#include "sysfs_utils.h" - -enum unbind_status { - UNBIND_ST_OK, - UNBIND_ST_USBIP_HOST, - UNBIND_ST_FAILED -}; - -static const char usbip_bind_usage_string[] = - "usbip bind \n" - " -b, --busid= Bind " USBIP_HOST_DRV_NAME ".ko to device " - "on \n"; - -void usbip_bind_usage(void) -{ - printf("usage: %s", usbip_bind_usage_string); -} - -/* call at unbound state */ -static int bind_usbip(char *busid) -{ - char attr_name[] = "bind"; - char bind_attr_path[SYSFS_PATH_MAX]; - int rc = -1; - - snprintf(bind_attr_path, sizeof(bind_attr_path), "%s/%s/%s/%s/%s/%s", - SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE, - SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME, attr_name); - - rc = write_sysfs_attribute(bind_attr_path, busid, strlen(busid)); - if (rc < 0) { - err("error binding device %s to driver: %s", busid, - strerror(errno)); - return -1; - } - - return 0; -} - -/* buggy driver may cause dead lock */ -static int unbind_other(char *busid) -{ - enum unbind_status status = UNBIND_ST_OK; - - char attr_name[] = "unbind"; - char unbind_attr_path[SYSFS_PATH_MAX]; - int rc = -1; - - struct udev *udev; - struct udev_device *dev; - const char *driver; - const char *bDevClass; - - /* Create libudev context. */ - udev = udev_new(); - - /* Get the device. */ - dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid); - if (!dev) { - dbg("unable to find device with bus ID %s", busid); - goto err_close_busid_dev; - } - - /* Check what kind of device it is. */ - bDevClass = udev_device_get_sysattr_value(dev, "bDeviceClass"); - if (!bDevClass) { - dbg("unable to get bDevClass device attribute"); - goto err_close_busid_dev; - } - - if (!strncmp(bDevClass, "09", strlen(bDevClass))) { - dbg("skip unbinding of hub"); - goto err_close_busid_dev; - } - - /* Get the device driver. */ - driver = udev_device_get_driver(dev); - if (!driver) { - /* No driver bound to this device. */ - goto out; - } - - if (!strncmp(USBIP_HOST_DRV_NAME, driver, - strlen(USBIP_HOST_DRV_NAME))) { - /* Already bound to usbip-host. */ - status = UNBIND_ST_USBIP_HOST; - goto out; - } - - /* Unbind device from driver. */ - snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s", - SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE, - SYSFS_DRIVERS_NAME, driver, attr_name); - - rc = write_sysfs_attribute(unbind_attr_path, busid, strlen(busid)); - if (rc < 0) { - err("error unbinding device %s from driver", busid); - goto err_close_busid_dev; - } - - goto out; - -err_close_busid_dev: - status = UNBIND_ST_FAILED; -out: - udev_device_unref(dev); - udev_unref(udev); - - return status; -} - -static int bind_device(char *busid) -{ - int rc; - struct udev *udev; - struct udev_device *dev; - - /* Check whether the device with this bus ID exists. */ - udev = udev_new(); - dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid); - if (!dev) { - err("device with the specified bus ID does not exist"); - return -1; - } - udev_unref(udev); - - rc = unbind_other(busid); - if (rc == UNBIND_ST_FAILED) { - err("could not unbind driver from device on busid %s", busid); - return -1; - } else if (rc == UNBIND_ST_USBIP_HOST) { - err("device on busid %s is already bound to %s", busid, - USBIP_HOST_DRV_NAME); - return -1; - } - - rc = modify_match_busid(busid, 1); - if (rc < 0) { - err("unable to bind device on %s", busid); - return -1; - } - - rc = bind_usbip(busid); - if (rc < 0) { - err("could not bind device to %s", USBIP_HOST_DRV_NAME); - modify_match_busid(busid, 0); - return -1; - } - - info("bind device on busid %s: complete", busid); - - return 0; -} - -int usbip_bind(int argc, char *argv[]) -{ - static const struct option opts[] = { - { "busid", required_argument, NULL, 'b' }, - { NULL, 0, NULL, 0 } - }; - - int opt; - int ret = -1; - - for (;;) { - opt = getopt_long(argc, argv, "b:", opts, NULL); - - if (opt == -1) - break; - - switch (opt) { - case 'b': - ret = bind_device(optarg); - goto out; - default: - goto err_out; - } - } - -err_out: - usbip_bind_usage(); -out: - return ret; -} diff --git a/userspace/src/usbip/usbip_stub.c~ b/userspace/src/usbip/usbip_stub.c~ deleted file mode 100644 index bd18be52..00000000 --- a/userspace/src/usbip/usbip_stub.c~ +++ /dev/null @@ -1,265 +0,0 @@ -#define INITGUID - -#include "usbip_common.h" -#include "usbip_windows.h" - -#include -#include - -#include "usbip_stub_api.h" - -static int -find_vhci_index(HDEVINFO dev_info) -{ - SP_DEVINFO_DATA dev_info_data; - int idx; - - dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); - - // Loop reading information from the devices until we get some error. - for (idx = 0;; idx++) { - char hardwareID[256]; - - // Get device info data. - if (!SetupDiEnumDeviceInfo(dev_info, idx, &dev_info_data)) { - DWORD err = GetLastError(); - - if (err != ERROR_NO_MORE_ITEMS) { - err("failed to get device information: err: %d\n", err); - } - return -1; - } - - // Get hardware ID. - if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, - SPDRP_HARDWAREID, 0L, (PBYTE)hardwareID, sizeof(hardwareID), 0L)) { - // We got some error reading the hardware id. I'm pretty sure this isn't supposed - // to happen, but let's continue anyway. - continue; - } - - // Check if we got the correct device. - if (strcmp(hardwareID, "root\\usbip_vhci") == 0) - return idx; - } -} - -static BOOL -get_vhci_intf(HDEVINFO dev_info, PSP_DEVICE_INTERFACE_DATA pdev_interface_data) -{ - int idx; - - idx = find_vhci_index(dev_info); - if (idx < 0) - return FALSE; - - pdev_interface_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); - // Get device interfaces. - if (!SetupDiEnumDeviceInterfaces(dev_info, NULL, (LPGUID)&GUID_DEVINTERFACE_VHCI_USBIP, - idx, pdev_interface_data)) { - DWORD err; - - err = GetLastError(); - // No more items here isn't supposed to happen since we checked that on the - // SetupDiEnumDeviceInfo, but there's no harm checking again. - if (err == ERROR_NO_MORE_ITEMS) { - err("usbip vhci interface is not registered\n"); - } - else { - err("unknown error when get interface_data: err: %d\n", err); - } - return FALSE; - } - return TRUE; -} - -static PSP_DEVICE_INTERFACE_DETAIL_DATA -get_vhci_intf_detail(HDEVINFO dev_info, PSP_DEVICE_INTERFACE_DATA pdev_interface_data) -{ - PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_interface_detail; - unsigned long len = 0; - DWORD err; - - // Get required length for dev_interface_detail. - SetupDiGetDeviceInterfaceDetail(dev_info, pdev_interface_data, NULL, 0, &len, NULL); - err = GetLastError(); - if (err != ERROR_INSUFFICIENT_BUFFER) { - err("SetupDiGetDeviceInterfaceDetail failed: err: %ld\n", err); - return NULL; - } - - // Allocate the required memory and set the cbSize. - pdev_interface_detail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(len); - if (pdev_interface_detail == NULL) { - err("can't malloc %lu size memory", len); - return NULL; - } - - pdev_interface_detail->cbSize = sizeof(PSP_DEVICE_INTERFACE_DETAIL_DATA); - - // Try to get device details. - if (!SetupDiGetDeviceInterfaceDetail(dev_info, pdev_interface_data, - pdev_interface_detail, len, &len, NULL)) { - // Errors. - err("SetupDiGetDeviceInterfaceDetail failed: err: %ld", GetLastError()); - free(pdev_interface_detail); - return NULL; - } - - return pdev_interface_detail; -} - -static char * -get_vhci_devpath(void) -{ - HDEVINFO dev_info; - SP_DEVICE_INTERFACE_DATA dev_interface_data; - PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_interface_detail; - char *devpath; - - // Get devices info. - dev_info = SetupDiGetClassDevs((LPGUID) &GUID_DEVINTERFACE_VHCI_USBIP, NULL, NULL, - DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); - if (dev_info == INVALID_HANDLE_VALUE) { - err("SetupDiGetClassDevs failed: %ld\n", GetLastError()); - return FALSE; - } - - if (!get_vhci_intf(dev_info, &dev_interface_data)) { - SetupDiDestroyDeviceInfoList(dev_info); - return FALSE; - } - - pdev_interface_detail = get_vhci_intf_detail(dev_info, &dev_interface_data); - if (pdev_interface_detail == NULL) { - SetupDiDestroyDeviceInfoList(dev_info); - return FALSE; - } - - devpath = _strdup(pdev_interface_detail->DevicePath); - free(pdev_interface_detail); - - SetupDiDestroyDeviceInfoList(dev_info); - - return devpath; -} - -HANDLE -usbip_vhci_driver_open(void) -{ - HANDLE hdev; - char *devpath; - - devpath = get_vhci_devpath(); - if (devpath == NULL) { - return INVALID_HANDLE_VALUE; - } - dbg("device path: %s", devpath); - hdev = CreateFile(devpath, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); - free(devpath); - return hdev; -} - -void -usbip_vhci_driver_close(HANDLE hdev) -{ - CloseHandle(hdev); -} - -static int -usbip_vhci_get_ports_status(HANDLE hdev, char *buf, int l) -{ - ioctl_usbip_vhci_get_ports_status *st; - unsigned long len; - - st = (ioctl_usbip_vhci_get_ports_status *)buf; - - if (l != sizeof(ioctl_usbip_vhci_get_ports_status)) - return -1; - - if (DeviceIoControl(hdev, IOCTL_USBIP_VHCI_GET_PORTS_STATUS, - NULL, 0, st, sizeof(ioctl_usbip_vhci_get_ports_status), &len, NULL)) { - if (len == sizeof(ioctl_usbip_vhci_get_ports_status)) - return 0; - } - return -1; -} - -int -usbip_vhci_get_free_port(HANDLE hdev) -{ - char buf[128]; - int i; - - if (usbip_vhci_get_ports_status(hdev, buf, sizeof(buf))) - return -1; - for(i = 1;i < sizeof(buf); i++) { - if (!buf[i]) - return i; - } - return -1; -} - -int -usbip_vhci_attach_device(HANDLE hdev, int port, struct usbip_usb_device *udev) -{ - ioctl_usbip_vhci_plugin plugin; - unsigned long unused; - - plugin.devid = ((udev->busnum << 16) | udev->devnum); - plugin.vendor = udev->idVendor; - plugin.product = udev->idProduct; - plugin.version = udev->bcdDevice; - plugin.speed = udev->speed; - plugin.inum = udev->bNumInterfaces; - plugin.class = udev->bDeviceClass; - plugin.subclass = udev->bDeviceSubClass; - plugin.protocol = udev->bDeviceProtocol; - - plugin.addr = port; - - if (DeviceIoControl(hdev, IOCTL_USBIP_VHCI_PLUGIN_HARDWARE, - &plugin, sizeof(plugin), NULL, 0, &unused, NULL)) - return 0; - return -1; -} - -int -usbip_vhci_detach_device(HANDLE hdev, int port) -{ - ioctl_usbip_vhci_unplug unplug; - unsigned long unused; - - unplug.addr = port; - if (DeviceIoControl(hdev, IOCTL_USBIP_VHCI_UNPLUG_HARDWARE, - &unplug, sizeof(unplug), NULL, 0, &unused, NULL)) - return 0; - return -1; -} - -int -show_port_status(void) -{ - HANDLE fd; - int i; - char buf[128]; - - fd = usbip_vhci_driver_open(); - if (INVALID_HANDLE_VALUE == fd) { - err("open vhci driver"); - return -1; - } - if (usbip_vhci_get_ports_status(fd, buf, sizeof(buf))) { - err("get port status"); - return -1; - } - info("max used port:%d\n", buf[0]); - for (i = 1; i <= buf[0]; i++) { - if (buf[i]) - info("port %d: used\n", i); - else - info("port %d: idle\n", i); - } - CloseHandle(fd); - return 0; -} diff --git a/userspace/src/usbip/usbip_stub.h~ b/userspace/src/usbip/usbip_stub.h~ deleted file mode 100644 index c9668f30..00000000 --- a/userspace/src/usbip/usbip_stub.h~ +++ /dev/null @@ -1,12 +0,0 @@ -#pragma - -#define WIN32_LEAN_AND_MEAN -#include -#include - -HANDLE usbip_vhci_driver_open(void); -void usbip_vhci_driver_close(HANDLE hdev); -int usbip_vhci_get_free_port(HANDLE hdev); -int usbip_vhci_attach_device(HANDLE hdev, int port, struct usbip_usb_device *udev); -int usbip_vhci_detach_device(HANDLE hdev, int port); -void usbip_vhci_forward(SOCKET sockfd, HANDLE hdev);