Commit 3391ba0e authored by Krzysztof Opasiak's avatar Krzysztof Opasiak Committed by Greg Kroah-Hartman

usbip: tools: Extract generic code to be shared with vudc backend

Extract the code from current stub driver backend and a common
interface for both stub driver and vudc. This allows to share most
of the usbipd code for both of them.

Based on code created in cooperation with Open Operating Systems
Student Society at University of Warsaw (O2S3@UW) consisting of:

    Igor Kotrasinski <ikotrasinsk@gmail.com>
    Karol Kosik <karo9@interia.eu>
    Ewelina Kosmider <3w3lfin@gmail.com>
    Dawid Lazarczyk <lazarczyk.dawid@gmail.com>
    Piotr Szulc <ps347277@students.mimuw.edu.pl>

Tutor and project owner:
    Krzysztof Opasiak <k.opasiak@samsung.com>
Signed-off-by: 's avatarKrzysztof Opasiak <k.opasiak@samsung.com>
Signed-off-by: 's avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent ea6873a4
......@@ -4,5 +4,6 @@ libusbip_la_LDFLAGS = -version-info @LIBUSBIP_VERSION@
lib_LTLIBRARIES := libusbip.la
libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h \
usbip_common.c usbip_common.h vhci_driver.c vhci_driver.h \
usbip_common.c usbip_common.h usbip_host_common.h \
usbip_host_common.c vhci_driver.c vhci_driver.h \
sysfs_utils.c sysfs_utils.h
/*
* Copyright (C) 2015-2016 Samsung Electronics
* Igor Kotrasinski <i.kotrasinsk@samsung.com>
* Krzysztof Opasiak <k.opasiak@samsung.com>
*
* Refactored from usbip_host_driver.c, which is:
* Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
* 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 <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <libudev.h>
#include "usbip_common.h"
#include "usbip_host_common.h"
#include "list.h"
#include "sysfs_utils.h"
struct udev *udev_context;
static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
{
char status_attr_path[SYSFS_PATH_MAX];
int fd;
int length;
char status;
int value = 0;
snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
udev->path);
fd = open(status_attr_path, O_RDONLY);
if (fd < 0) {
err("error opening attribute %s", status_attr_path);
return -1;
}
length = read(fd, &status, 1);
if (length < 0) {
err("error reading attribute %s", status_attr_path);
close(fd);
return -1;
}
value = atoi(&status);
return value;
}
static
struct usbip_exported_device *usbip_exported_device_new(
struct usbip_host_driver *hdriver, const char *sdevpath)
{
struct usbip_exported_device *edev = NULL;
struct usbip_exported_device *edev_old;
size_t size;
int i;
edev = calloc(1, sizeof(struct usbip_exported_device));
edev->sudev =
udev_device_new_from_syspath(udev_context, sdevpath);
if (!edev->sudev) {
err("udev_device_new_from_syspath: %s", sdevpath);
goto err;
}
if (hdriver->ops.read_device(edev->sudev, &edev->udev) < 0)
goto err;
edev->status = read_attr_usbip_status(&edev->udev);
if (edev->status < 0)
goto err;
/* reallocate buffer to include usb interface data */
size = sizeof(struct usbip_exported_device) +
edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface);
edev_old = edev;
edev = realloc(edev, size);
if (!edev) {
edev = edev_old;
dbg("realloc failed");
goto err;
}
for (i = 0; i < edev->udev.bNumInterfaces; i++) {
/* vudc does not support reading interfaces */
if (!hdriver->ops.read_interface)
break;
hdriver->ops.read_interface(&edev->udev, i, &edev->uinf[i]);
}
return edev;
err:
if (edev->sudev)
udev_device_unref(edev->sudev);
if (edev)
free(edev);
return NULL;
}
static int refresh_exported_devices(struct usbip_host_driver *hdriver)
{
struct usbip_exported_device *edev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev;
const char *path;
enumerate = udev_enumerate_new(udev_context);
udev_enumerate_add_match_subsystem(enumerate, hdriver->udev_subsystem);
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices) {
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev_context,
path);
if (dev == NULL)
continue;
/* Check whether device uses usbip driver. */
if (hdriver->ops.is_my_device(dev)) {
edev = usbip_exported_device_new(hdriver, path);
if (!edev) {
dbg("usbip_exported_device_new failed");
continue;
}
list_add(&edev->node, &hdriver->edev_list);
hdriver->ndevs++;
}
}
return 0;
}
static void usbip_exported_device_destroy(struct list_head *devs)
{
struct list_head *i, *tmp;
struct usbip_exported_device *edev;
list_for_each_safe(i, tmp, devs) {
edev = list_entry(i, struct usbip_exported_device, node);
list_del(i);
free(edev);
}
}
int usbip_generic_driver_open(struct usbip_host_driver *hdriver)
{
int rc;
udev_context = udev_new();
if (!udev_context) {
err("udev_new failed");
return -1;
}
rc = refresh_exported_devices(hdriver);
if (rc < 0)
goto err;
return 0;
err:
udev_unref(udev_context);
return -1;
}
void usbip_generic_driver_close(struct usbip_host_driver *hdriver)
{
if (!hdriver)
return;
usbip_exported_device_destroy(&hdriver->edev_list);
udev_unref(udev_context);
}
int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver)
{
int rc;
usbip_exported_device_destroy(&hdriver->edev_list);
hdriver->ndevs = 0;
INIT_LIST_HEAD(&hdriver->edev_list);
rc = refresh_exported_devices(hdriver);
if (rc < 0)
return -1;
return 0;
}
int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
{
char attr_name[] = "usbip_sockfd";
char sockfd_attr_path[SYSFS_PATH_MAX];
char sockfd_buff[30];
int ret;
if (edev->status != SDEV_ST_AVAILABLE) {
dbg("device not available: %s", edev->udev.busid);
switch (edev->status) {
case SDEV_ST_ERROR:
dbg("status SDEV_ST_ERROR");
break;
case SDEV_ST_USED:
dbg("status SDEV_ST_USED");
break;
default:
dbg("status unknown: 0x%x", edev->status);
}
return -1;
}
/* only the first interface is true */
snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
edev->udev.path, attr_name);
snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
strlen(sockfd_buff));
if (ret < 0) {
err("write_sysfs_attribute failed: sockfd %s to %s",
sockfd_buff, sockfd_attr_path);
return ret;
}
info("connect: %s", edev->udev.busid);
return ret;
}
struct usbip_exported_device *usbip_generic_get_device(
struct usbip_host_driver *hdriver, int num)
{
struct list_head *i;
struct usbip_exported_device *edev;
int cnt = 0;
list_for_each(i, &hdriver->edev_list) {
edev = list_entry(i, struct usbip_exported_device, node);
if (num == cnt)
return edev;
cnt++;
}
return NULL;
}
/*
* Copyright (C) 2015-2016 Samsung Electronics
* Igor Kotrasinski <i.kotrasinsk@samsung.com>
* Krzysztof Opasiak <k.opasiak@samsung.com>
*
* Refactored from usbip_host_driver.c, which is:
* Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __USBIP_HOST_COMMON_H
#define __USBIP_HOST_COMMON_H
#include <stdint.h>
#include <libudev.h>
#include <errno.h>
#include "list.h"
#include "usbip_common.h"
#include "sysfs_utils.h"
struct usbip_host_driver;
struct usbip_host_driver_ops {
int (*open)(struct usbip_host_driver *hdriver);
void (*close)(struct usbip_host_driver *hdriver);
int (*refresh_device_list)(struct usbip_host_driver *hdriver);
struct usbip_exported_device * (*get_device)(
struct usbip_host_driver *hdriver, int num);
int (*read_device)(struct udev_device *sdev,
struct usbip_usb_device *dev);
int (*read_interface)(struct usbip_usb_device *udev, int i,
struct usbip_usb_interface *uinf);
int (*is_my_device)(struct udev_device *udev);
};
struct usbip_host_driver {
int ndevs;
/* list of exported device */
struct list_head edev_list;
const char *udev_subsystem;
struct usbip_host_driver_ops ops;
};
struct usbip_exported_device {
struct udev_device *sudev;
int32_t status;
struct usbip_usb_device udev;
struct list_head node;
struct usbip_usb_interface uinf[];
};
/* External API to access the driver */
static inline int usbip_driver_open(struct usbip_host_driver *hdriver)
{
if (!hdriver->ops.open)
return -EOPNOTSUPP;
return hdriver->ops.open(hdriver);
}
static inline void usbip_driver_close(struct usbip_host_driver *hdriver)
{
if (!hdriver->ops.close)
return;
hdriver->ops.close(hdriver);
}
static inline int usbip_refresh_device_list(struct usbip_host_driver *hdriver)
{
if (!hdriver->ops.refresh_device_list)
return -EOPNOTSUPP;
return hdriver->ops.refresh_device_list(hdriver);
}
static inline struct usbip_exported_device *
usbip_get_device(struct usbip_host_driver *hdriver, int num)
{
if (!hdriver->ops.get_device)
return NULL;
return hdriver->ops.get_device(hdriver, num);
}
/* Helper functions for implementing driver backend */
int usbip_generic_driver_open(struct usbip_host_driver *hdriver);
void usbip_generic_driver_close(struct usbip_host_driver *hdriver);
int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver);
int usbip_export_device(struct usbip_exported_device *edev, int sockfd);
struct usbip_exported_device *usbip_generic_get_device(
struct usbip_host_driver *hdriver, int num);
#endif /* __USBIP_HOST_COMMON_H */
/*
* Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
* 2005-2007 Takahiro Hirofuchi
* Copyright (C) 2015-2016 Samsung Electronics
* Igor Kotrasinski <i.kotrasinsk@samsung.com>
* Krzysztof Opasiak <k.opasiak@samsung.com>
*
* 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
......@@ -16,265 +19,47 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <libudev.h>
#include "usbip_common.h"
#include "usbip_host_common.h"
#include "usbip_host_driver.h"
#include "list.h"
#include "sysfs_utils.h"
#undef PROGNAME
#define PROGNAME "libusbip"
struct usbip_host_driver *host_driver;
struct udev *udev_context;
static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
{
char status_attr_path[SYSFS_PATH_MAX];
int fd;
int length;
char status;
int value = 0;
snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
udev->path);
fd = open(status_attr_path, O_RDONLY);
if (fd < 0) {
err("error opening attribute %s", status_attr_path);
return -1;
}
length = read(fd, &status, 1);
if (length < 0) {
err("error reading attribute %s", status_attr_path);
close(fd);
return -1;
}
value = atoi(&status);
return value;
}
static
struct usbip_exported_device *usbip_exported_device_new(const char *sdevpath)
{
struct usbip_exported_device *edev = NULL;
struct usbip_exported_device *edev_old;
size_t size;
int i;
edev = calloc(1, sizeof(struct usbip_exported_device));
edev->sudev = udev_device_new_from_syspath(udev_context, sdevpath);
if (!edev->sudev) {
err("udev_device_new_from_syspath: %s", sdevpath);
goto err;
}
read_usb_device(edev->sudev, &edev->udev);
edev->status = read_attr_usbip_status(&edev->udev);
if (edev->status < 0)
goto err;
/* reallocate buffer to include usb interface data */
size = sizeof(struct usbip_exported_device) +
edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface);
edev_old = edev;
edev = realloc(edev, size);
if (!edev) {
edev = edev_old;
dbg("realloc failed");
goto err;
}
for (i = 0; i < edev->udev.bNumInterfaces; i++)
read_usb_interface(&edev->udev, i, &edev->uinf[i]);
return edev;
err:
if (edev->sudev)
udev_device_unref(edev->sudev);
if (edev)
free(edev);
return NULL;
}
static int refresh_exported_devices(void)
static int is_my_device(struct udev_device *dev)
{
struct usbip_exported_device *edev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev;
const char *path;
const char *driver;
enumerate = udev_enumerate_new(udev_context);
udev_enumerate_add_match_subsystem(enumerate, "usb");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices) {
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev_context, path);
if (dev == NULL)
continue;
/* Check whether device uses usbip-host driver. */
driver = udev_device_get_driver(dev);
if (driver != NULL && !strcmp(driver, USBIP_HOST_DRV_NAME)) {
edev = usbip_exported_device_new(path);
if (!edev) {
dbg("usbip_exported_device_new failed");
continue;
}
list_add(&edev->node, &host_driver->edev_list);
host_driver->ndevs++;
}
}
return 0;
}
static void usbip_exported_device_destroy(void)
{
struct list_head *i, *tmp;
struct usbip_exported_device *edev;
list_for_each_safe(i, tmp, &host_driver->edev_list) {
edev = list_entry(i, struct usbip_exported_device, node);
list_del(i);
free(edev);
}
driver = udev_device_get_driver(dev);
return driver != NULL && !strcmp(driver, USBIP_HOST_DRV_NAME);
}
int usbip_host_driver_open(void)
static int usbip_host_driver_open(struct usbip_host_driver *hdriver)
{
int rc;
udev_context = udev_new();
if (!udev_context) {
err("udev_new failed");
return -1;
}
host_driver = calloc(1, sizeof(*host_driver));
host_driver->ndevs = 0;
INIT_LIST_HEAD(&host_driver->edev_list);
rc = refresh_exported_devices();
if (rc < 0)
goto err_free_host_driver;
return 0;
err_free_host_driver:
free(host_driver);
host_driver = NULL;
udev_unref(udev_context);
return -1;
}
void usbip_host_driver_close(void)
{
if (!host_driver)
return;
usbip_exported_device_destroy();
free(host_driver);
host_driver = NULL;
udev_unref(udev_context);
}
int usbip_host_refresh_device_list(void)
{
int rc;
usbip_exported_device_destroy();
host_driver->ndevs = 0;
INIT_LIST_HEAD(&host_driver->edev_list);
rc = refresh_exported_devices();
if (rc < 0)
return -1;
return 0;
}
int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd)
{
char attr_name[] = "usbip_sockfd";
char sockfd_attr_path[SYSFS_PATH_MAX];
char sockfd_buff[30];
int ret;
if (edev->status != SDEV_ST_AVAILABLE) {
dbg("device not available: %s", edev->udev.busid);
switch (edev->status) {
case SDEV_ST_ERROR:
dbg("status SDEV_ST_ERROR");
break;
case SDEV_ST_USED:
dbg("status SDEV_ST_USED");
break;
default:
dbg("status unknown: 0x%x", edev->status);
}
return -1;
}
/* only the first interface is true */
snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
edev->udev.path, attr_name);
snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
strlen(sockfd_buff));
if (ret < 0) {
err("write_sysfs_attribute failed: sockfd %s to %s",
sockfd_buff, sockfd_attr_path);
return ret;
}
info("connect: %s", edev->udev.busid);
hdriver->ndevs = 0;
INIT_LIST_HEAD(&hdriver->edev_list);
ret = usbip_generic_driver_open(hdriver);
if (ret)
err("please load " USBIP_CORE_MOD_NAME ".ko and "
USBIP_HOST_DRV_NAME ".ko!");
return ret;
}
struct usbip_exported_device *usbip_host_get_device(int num)
{
struct list_head *i;
struct usbip_exported_device *edev;
int cnt = 0;
list_for_each(i, &host_driver->edev_list) {
edev = list_entry(i, struct usbip_exported_device, node);
if (num == cnt)
return edev;
else
cnt++;
}
return NULL;
}
struct usbip_host_driver host_driver = {
.edev_list = LIST_HEAD_INIT(host_driver.edev_list),
.udev_subsystem = "usb",
.ops = {
.open = usbip_host_driver_open,
.close = usbip_generic_driver_close,
.refresh_device_list = usbip_generic_refresh_device_list,
.get_device = usbip_generic_get_device,
.read_device = read_usb_device,
.read_interface = read_usb_interface,
.is_my_device = is_my_device,
},
};
/*
* Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
* 2005-2007 Takahiro Hirofuchi
* Copyright (C) 2015-2016 Samsung Electronics
* Igor Kotrasinski <i.kotrasinsk@samsung.com>
* Krzysztof Opasiak <k.opasiak@samsung.com>
*
* 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
......@@ -22,28 +25,8 @@
#include <stdint.h>
#include "usbip_common.h"
#include "list.h"
#include "usbip_host_common.h"
struct usbip_host_driver {
int ndevs;
/* list of exported device */
struct list_head edev_list;
};
struct usbip_exported_device {
struct udev_device *sudev;
int32_t status;
struct usbip_usb_device udev;
struct list_head node;
struct usbip_usb_interface uinf[];
};
extern struct usbip_host_driver *host_driver;
int usbip_host_driver_open(void);
void usbip_host_driver_close(void);
int usbip_host_refresh_device_list(void);
int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd);
struct usbip_exported_device *usbip_host_get_device(int num);
extern struct usbip_host_driver host_driver;
#endif /* __USBIP_HOST_DRIVER_H */