From 0775a9cbc694e8c7276688be3bbd2f386167ab54 Mon Sep 17 00:00:00 2001 From: Nobuo Iwata Date: Mon, 13 Jun 2016 11:33:40 +0900 Subject: usbip: vhci extension: modifications to vhci driver Modification to Kconfig, vhci_hc.c, vhci.h and vhci_sysfs.c. 1. kernel config Followings are added. USBIP_VHCI_HC_PORTS: Number of ports per USB/IP virtual host controller. The default is 8 - same as current VHCI_NPORTS. USBIP_VHCI_NR_HCS: Number of USB/IP virtual host controllers. The default is 1. This paratmeter is replaced with USBIP_VHCI_INIT_HCS and USBIP_VHCI_MAX_HCS included in succeeding dynamic extension patch. 2. the_controller to controllers the_controller is changed to vhci_pdevs: array of struct platform_device. 3. vhci_sysfs.c Sysfs structure is changed as following. BEFORE: /sys/devices/platform +-- vhci +-- status +-- attach +-- detach +-- usbip_debug AFTER: example for CONFIG_USBIP_NR_HCS=4 /sys/devices/platform +-- vhci | +-- nports | +-- status | +-- status.1 | +-- status.2 | +-- status.3 | +-- attach | +-- detach | +-- usbip_debug +-- vhci.1 +-- vhci.2 +-- vhci.3 vhci[.N] is shown for each host controller kobj. vhch.1, vhci.2, ... are shown only when CONFIG_USBIP_NR_HCS is more than 1. Only 'vhci' (without number) has user space interfaces. 'nports' is newly added to give ports-per-controller and number of controlles. Before that, number of ports is acquired by reading status lines. Status is divided for each controller to avoid page size (4KB) limitation. Old userspace tool binaries work with the first status within the first controller. Inconsistency between status header and content is fixed. 4th and 5th column are header: "dev bus" content(unused): "000 000" content(used): "%08x", devid Only 1st and 2nd column are used by program. In old version, sscanf() in parse_status expect no bus column. And bus_id string is shown in the last column. Then bus in the header is removed and unused content is replaced with 8 zeros. The sscanf() expects more than 5 columns and new has 6 columns so there's no compatibility issue in this change. Signed-off-by: Nobuo Iwata Signed-off-by: Greg Kroah-Hartman --- drivers/usb/usbip/Kconfig | 21 +++ drivers/usb/usbip/vhci.h | 54 ++++++-- drivers/usb/usbip/vhci_hcd.c | 285 ++++++++++++++++++++++++++------------- drivers/usb/usbip/vhci_rx.c | 21 +-- drivers/usb/usbip/vhci_sysfs.c | 296 ++++++++++++++++++++++++++++++++--------- 5 files changed, 497 insertions(+), 180 deletions(-) (limited to 'drivers/usb/usbip') diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig index 17646b25343f..29492c70e0e6 100644 --- a/drivers/usb/usbip/Kconfig +++ b/drivers/usb/usbip/Kconfig @@ -24,6 +24,27 @@ config USBIP_VHCI_HCD To compile this driver as a module, choose M here: the module will be called vhci-hcd. +config USBIP_VHCI_HC_PORTS + int "Number of ports per USB/IP virtual host controller" + range 1 31 + default 8 + depends on USBIP_VHCI_HCD + ---help--- + To increase number of ports available for USB/IP virtual + host controller driver, this defines number of ports per + USB/IP virtual host controller. + +config USBIP_VHCI_NR_HCS + int "Number of USB/IP virtual host controllers" + range 1 128 + default 1 + depends on USBIP_VHCI_HCD + ---help--- + To increase number of ports available for USB/IP virtual + host controller driver, this defines number of USB/IP + virtual host controllers as if adding physical host + controllers. + config USBIP_HOST tristate "Host driver" depends on USBIP_CORE && USB diff --git a/drivers/usb/usbip/vhci.h b/drivers/usb/usbip/vhci.h index a863a98a91ce..88b71c4e068f 100644 --- a/drivers/usb/usbip/vhci.h +++ b/drivers/usb/usbip/vhci.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2003-2008 Takahiro Hirofuchi + * Copyright (C) 2015 Nobuo Iwata * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -72,13 +73,25 @@ struct vhci_unlink { }; /* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */ -#define VHCI_NPORTS 8 +#ifdef CONFIG_USBIP_VHCI_HC_PORTS +#define VHCI_HC_PORTS CONFIG_USBIP_VHCI_HC_PORTS +#else +#define VHCI_HC_PORTS 8 +#endif + +#ifdef CONFIG_USBIP_VHCI_NR_HCS +#define VHCI_NR_HCS CONFIG_USBIP_VHCI_NR_HCS +#else +#define VHCI_NR_HCS 1 +#endif + +#define MAX_STATUS_NAME 16 /* for usb_bus.hcpriv */ struct vhci_hcd { spinlock_t lock; - u32 port_status[VHCI_NPORTS]; + u32 port_status[VHCI_HC_PORTS]; unsigned resuming:1; unsigned long re_timeout; @@ -90,14 +103,19 @@ struct vhci_hcd { * wIndex shows the port number and begins from 1. * But, the index of this array begins from 0. */ - struct vhci_device vdev[VHCI_NPORTS]; + struct vhci_device vdev[VHCI_HC_PORTS]; }; -extern struct vhci_hcd *the_controller; -extern const struct attribute_group dev_attr_group; +extern int vhci_num_controllers; +extern struct platform_device **vhci_pdevs; +extern struct attribute_group vhci_attr_group; /* vhci_hcd.c */ -void rh_port_connect(int rhport, enum usb_device_speed speed); +void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed); + +/* vhci_sysfs.c */ +int vhci_init_attr_group(void); +void vhci_finish_attr_group(void); /* vhci_rx.c */ struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum); @@ -106,9 +124,14 @@ int vhci_rx_loop(void *data); /* vhci_tx.c */ int vhci_tx_loop(void *data); -static inline struct vhci_device *port_to_vdev(__u32 port) +static inline __u32 port_to_rhport(__u32 port) +{ + return port % VHCI_HC_PORTS; +} + +static inline int port_to_pdev_nr(__u32 port) { - return &the_controller->vdev[port]; + return port / VHCI_HC_PORTS; } static inline struct vhci_hcd *hcd_to_vhci(struct usb_hcd *hcd) @@ -116,14 +139,25 @@ static inline struct vhci_hcd *hcd_to_vhci(struct usb_hcd *hcd) return (struct vhci_hcd *) (hcd->hcd_priv); } +static inline struct device *hcd_dev(struct usb_hcd *hcd) +{ + return (hcd)->self.controller; +} + +static inline const char *hcd_name(struct usb_hcd *hcd) +{ + return (hcd)->self.bus_name; +} + static inline struct usb_hcd *vhci_to_hcd(struct vhci_hcd *vhci) { return container_of((void *) vhci, struct usb_hcd, hcd_priv); } -static inline struct device *vhci_dev(struct vhci_hcd *vhci) +static inline struct vhci_hcd *vdev_to_vhci(struct vhci_device *vdev) { - return vhci_to_hcd(vhci)->self.controller; + return container_of( + (void *)(vdev - vdev->rhport), struct vhci_hcd, vdev); } #endif /* __USBIP_VHCI_H */ diff --git a/drivers/usb/usbip/vhci_hcd.c b/drivers/usb/usbip/vhci_hcd.c index 2e0450bec1b1..96f2dacb27fa 100644 --- a/drivers/usb/usbip/vhci_hcd.c +++ b/drivers/usb/usbip/vhci_hcd.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2003-2008 Takahiro Hirofuchi + * Copyright (C) 2015-2016 Nobuo Iwata * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -56,7 +57,9 @@ static int vhci_get_frame_number(struct usb_hcd *hcd); static const char driver_name[] = "vhci_hcd"; static const char driver_desc[] = "USB/IP Virtual Host Controller"; -struct vhci_hcd *the_controller; +int vhci_num_controllers = VHCI_NR_HCS; + +struct platform_device **vhci_pdevs; static const char * const bit_desc[] = { "CONNECTION", /*0*/ @@ -119,47 +122,59 @@ static void dump_port_status_diff(u32 prev_status, u32 new_status) pr_debug("\n"); } -void rh_port_connect(int rhport, enum usb_device_speed speed) +void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed) { + struct vhci_hcd *vhci = vdev_to_vhci(vdev); + int rhport = vdev->rhport; + u32 status; unsigned long flags; usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport); - spin_lock_irqsave(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); + + status = vhci->port_status[rhport]; - the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION - | (1 << USB_PORT_FEAT_C_CONNECTION); + status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION); switch (speed) { case USB_SPEED_HIGH: - the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED; + status |= USB_PORT_STAT_HIGH_SPEED; break; case USB_SPEED_LOW: - the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED; + status |= USB_PORT_STAT_LOW_SPEED; break; default: break; } - spin_unlock_irqrestore(&the_controller->lock, flags); + vhci->port_status[rhport] = status; + + spin_unlock_irqrestore(&vhci->lock, flags); - usb_hcd_poll_rh_status(vhci_to_hcd(the_controller)); + usb_hcd_poll_rh_status(vhci_to_hcd(vhci)); } -static void rh_port_disconnect(int rhport) +static void rh_port_disconnect(struct vhci_device *vdev) { + struct vhci_hcd *vhci = vdev_to_vhci(vdev); + int rhport = vdev->rhport; + u32 status; unsigned long flags; usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport); - spin_lock_irqsave(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); + + status = vhci->port_status[rhport]; + + status &= ~USB_PORT_STAT_CONNECTION; + status |= (1 << USB_PORT_FEAT_C_CONNECTION); - the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION; - the_controller->port_status[rhport] |= - (1 << USB_PORT_FEAT_C_CONNECTION); + vhci->port_status[rhport] = status; - spin_unlock_irqrestore(&the_controller->lock, flags); - usb_hcd_poll_rh_status(vhci_to_hcd(the_controller)); + spin_unlock_irqrestore(&vhci->lock, flags); + usb_hcd_poll_rh_status(vhci_to_hcd(vhci)); } #define PORT_C_MASK \ @@ -188,7 +203,7 @@ static int vhci_hub_status(struct usb_hcd *hcd, char *buf) int changed = 0; unsigned long flags; - retval = DIV_ROUND_UP(VHCI_NPORTS + 1, 8); + retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8); memset(buf, 0, retval); vhci = hcd_to_vhci(hcd); @@ -200,7 +215,7 @@ static int vhci_hub_status(struct usb_hcd *hcd, char *buf) } /* check pseudo status register for each port */ - for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { + for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) { if ((vhci->port_status[rhport] & PORT_C_MASK)) { /* The status of a port has been changed, */ usbip_dbg_vhci_rh("port %d status changed\n", rhport); @@ -225,7 +240,7 @@ static inline void hub_descriptor(struct usb_hub_descriptor *desc) desc->bDescLength = 9; desc->wHubCharacteristics = cpu_to_le16( HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM); - desc->bNbrPorts = VHCI_NPORTS; + desc->bNbrPorts = VHCI_HC_PORTS; desc->u.hs.DeviceRemovable[0] = 0xff; desc->u.hs.DeviceRemovable[1] = 0xff; } @@ -238,7 +253,7 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, int rhport; unsigned long flags; - u32 prev_port_status[VHCI_NPORTS]; + u32 prev_port_status[VHCI_HC_PORTS]; if (!HCD_HW_ACCESSIBLE(hcd)) return -ETIMEDOUT; @@ -249,7 +264,7 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, */ usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue, wIndex); - if (wIndex > VHCI_NPORTS) + if (wIndex > VHCI_HC_PORTS) pr_err("invalid port number %d\n", wIndex); rhport = ((__u8)(wIndex & 0x00ff)) - 1; @@ -315,7 +330,7 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, break; case GetPortStatus: usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex); - if (wIndex > VHCI_NPORTS || wIndex < 1) { + if (wIndex > VHCI_HC_PORTS || wIndex < 1) { pr_err("invalid port number %d\n", wIndex); retval = -EPIPE; } @@ -416,14 +431,27 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, static struct vhci_device *get_vdev(struct usb_device *udev) { - int i; + struct platform_device *pdev; + struct usb_hcd *hcd; + struct vhci_hcd *vhci; + int pdev_nr, rhport; if (!udev) return NULL; - for (i = 0; i < VHCI_NPORTS; i++) - if (the_controller->vdev[i].udev == udev) - return port_to_vdev(i); + for (pdev_nr = 0; pdev_nr < vhci_num_controllers; pdev_nr++) { + pdev = *(vhci_pdevs + pdev_nr); + if (pdev == NULL) + continue; + hcd = platform_get_drvdata(pdev); + if (hcd == NULL) + continue; + vhci = hcd_to_vhci(hcd); + for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) { + if (vhci->vdev[rhport].udev == udev) + return &vhci->vdev[rhport]; + } + } return NULL; } @@ -432,6 +460,7 @@ static void vhci_tx_urb(struct urb *urb) { struct vhci_device *vdev = get_vdev(urb->dev); struct vhci_priv *priv; + struct vhci_hcd *vhci = vdev_to_vhci(vdev); unsigned long flags; if (!vdev) { @@ -447,7 +476,7 @@ static void vhci_tx_urb(struct urb *urb) spin_lock_irqsave(&vdev->priv_lock, flags); - priv->seqnum = atomic_inc_return(&the_controller->seqnum); + priv->seqnum = atomic_inc_return(&vhci->seqnum); if (priv->seqnum == 0xffff) dev_info(&urb->dev->dev, "seqnum max\n"); @@ -465,7 +494,9 @@ static void vhci_tx_urb(struct urb *urb) static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags) { + struct vhci_hcd *vhci = hcd_to_vhci(hcd); struct device *dev = &urb->dev->dev; + u8 portnum = urb->dev->portnum; int ret = 0; struct vhci_device *vdev; unsigned long flags; @@ -473,26 +504,30 @@ static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n", hcd, urb, mem_flags); + if (portnum > VHCI_HC_PORTS) { + pr_err("invalid port number %d\n", portnum); + return -ENODEV; + } + vdev = &vhci->vdev[portnum-1]; + /* patch to usb_sg_init() is in 2.5.60 */ BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length); - spin_lock_irqsave(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); if (urb->status != -EINPROGRESS) { dev_err(dev, "URB already unlinked!, status %d\n", urb->status); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); return urb->status; } - vdev = port_to_vdev(urb->dev->portnum-1); - /* refuse enqueue for dead connection */ spin_lock(&vdev->ud.lock); if (vdev->ud.status == VDEV_ST_NULL || vdev->ud.status == VDEV_ST_ERROR) { dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport); spin_unlock(&vdev->ud.lock); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); return -ENODEV; } spin_unlock(&vdev->ud.lock); @@ -565,17 +600,16 @@ static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, out: vhci_tx_urb(urb); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); return 0; no_need_xmit: usb_hcd_unlink_urb_from_ep(hcd, urb); no_need_unlink: - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); if (!ret) - usb_hcd_giveback_urb(vhci_to_hcd(the_controller), - urb, urb->status); + usb_hcd_giveback_urb(hcd, urb, urb->status); return ret; } @@ -627,19 +661,20 @@ no_need_unlink: */ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) { + struct vhci_hcd *vhci = hcd_to_vhci(hcd); struct vhci_priv *priv; struct vhci_device *vdev; unsigned long flags; pr_info("dequeue a urb %p\n", urb); - spin_lock_irqsave(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); priv = urb->hcpriv; if (!priv) { /* URB was never linked! or will be soon given back by * vhci_rx. */ - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); return -EIDRM; } @@ -648,7 +683,7 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) ret = usb_hcd_check_unlink_urb(hcd, urb, status); if (ret) { - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); return ret; } } @@ -676,10 +711,9 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) usb_hcd_unlink_urb_from_ep(hcd, urb); - spin_unlock_irqrestore(&the_controller->lock, flags); - usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, - urb->status); - spin_lock_irqsave(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); + usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status); + spin_lock_irqsave(&vhci->lock, flags); } else { /* tcp connection is alive */ @@ -691,12 +725,12 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC); if (!unlink) { spin_unlock(&vdev->priv_lock); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC); return -ENOMEM; } - unlink->seqnum = atomic_inc_return(&the_controller->seqnum); + unlink->seqnum = atomic_inc_return(&vhci->seqnum); if (unlink->seqnum == 0xffff) pr_info("seqnum max\n"); @@ -712,7 +746,7 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) spin_unlock(&vdev->priv_lock); } - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); usbip_dbg_vhci_hc("leave\n"); return 0; @@ -720,10 +754,12 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) static void vhci_device_unlink_cleanup(struct vhci_device *vdev) { + struct vhci_hcd *vhci = vdev_to_vhci(vdev); + struct usb_hcd *hcd = vhci_to_hcd(vhci); struct vhci_unlink *unlink, *tmp; unsigned long flags; - spin_lock_irqsave(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); spin_lock(&vdev->priv_lock); list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) { @@ -752,24 +788,23 @@ static void vhci_device_unlink_cleanup(struct vhci_device *vdev) urb->status = -ENODEV; - usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); + usb_hcd_unlink_urb_from_ep(hcd, urb); list_del(&unlink->list); spin_unlock(&vdev->priv_lock); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); - usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, - urb->status); + usb_hcd_giveback_urb(hcd, urb, urb->status); - spin_lock_irqsave(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); spin_lock(&vdev->priv_lock); kfree(unlink); } spin_unlock(&vdev->priv_lock); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); } /* @@ -827,7 +862,7 @@ static void vhci_shutdown_connection(struct usbip_device *ud) * is actually given back by vhci_rx after receiving its return pdu. * */ - rh_port_disconnect(vdev->rhport); + rh_port_disconnect(vdev); pr_info("disconnect device\n"); } @@ -866,7 +901,7 @@ static void vhci_device_unusable(struct usbip_device *ud) static void vhci_device_init(struct vhci_device *vdev) { - memset(vdev, 0, sizeof(*vdev)); + memset(vdev, 0, sizeof(struct vhci_device)); vdev->ud.side = USBIP_VHCI; vdev->ud.status = VDEV_ST_NULL; @@ -887,17 +922,34 @@ static void vhci_device_init(struct vhci_device *vdev) usbip_start_eh(&vdev->ud); } +static int hcd_name_to_id(const char *name) +{ + char *c; + long val; + int ret; + + c = strchr(name, '.'); + if (c == NULL) + return 0; + + ret = kstrtol(c+1, 10, &val); + if (ret < 0) + return ret; + + return val; +} + static int vhci_start(struct usb_hcd *hcd) { struct vhci_hcd *vhci = hcd_to_vhci(hcd); - int rhport; + int id, rhport; int err = 0; usbip_dbg_vhci_hc("enter vhci_start\n"); /* initialize private data of usb_hcd */ - for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { + for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) { struct vhci_device *vdev = &vhci->vdev[rhport]; vhci_device_init(vdev); @@ -910,11 +962,26 @@ static int vhci_start(struct usb_hcd *hcd) hcd->power_budget = 0; /* no limit */ hcd->uses_new_polling = 1; + id = hcd_name_to_id(hcd_name(hcd)); + if (id < 0) { + pr_err("invalid vhci name %s\n", hcd_name(hcd)); + return -EINVAL; + } + /* vhci_hcd is now ready to be controlled through sysfs */ - err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group); - if (err) { - pr_err("create sysfs files\n"); - return err; + if (id == 0) { + err = vhci_init_attr_group(); + if (err) { + pr_err("init attr group\n"); + return err; + } + err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group); + if (err) { + pr_err("create sysfs files\n"); + vhci_finish_attr_group(); + return err; + } + pr_info("created sysfs %s\n", hcd_name(hcd)); } return 0; @@ -923,15 +990,19 @@ static int vhci_start(struct usb_hcd *hcd) static void vhci_stop(struct usb_hcd *hcd) { struct vhci_hcd *vhci = hcd_to_vhci(hcd); - int rhport = 0; + int id, rhport; usbip_dbg_vhci_hc("stop VHCI controller\n"); /* 1. remove the userland interface of vhci_hcd */ - sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group); + id = hcd_name_to_id(hcd_name(hcd)); + if (id == 0) { + sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group); + vhci_finish_attr_group(); + } /* 2. shutdown all the ports of vhci_hcd */ - for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { + for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) { struct vhci_device *vdev = &vhci->vdev[rhport]; usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED); @@ -1025,9 +1096,6 @@ static int vhci_hcd_probe(struct platform_device *pdev) } hcd->has_tt = 1; - /* this is private data for vhci_hcd */ - the_controller = hcd_to_vhci(hcd); - /* * Finish generic HCD structure initialization and register. * Call the driver's reset() and start() routines. @@ -1036,7 +1104,6 @@ static int vhci_hcd_probe(struct platform_device *pdev) if (ret != 0) { pr_err("usb_add_hcd failed %d\n", ret); usb_put_hcd(hcd); - the_controller = NULL; return ret; } @@ -1059,7 +1126,6 @@ static int vhci_hcd_remove(struct platform_device *pdev) */ usb_remove_hcd(hcd); usb_put_hcd(hcd); - the_controller = NULL; return 0; } @@ -1070,21 +1136,24 @@ static int vhci_hcd_remove(struct platform_device *pdev) static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state) { struct usb_hcd *hcd; - int rhport = 0; + struct vhci_hcd *vhci; + int rhport; int connected = 0; int ret = 0; unsigned long flags; hcd = platform_get_drvdata(pdev); + if (!hcd) + return 0; + vhci = hcd_to_vhci(hcd); - spin_lock_irqsave(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); - for (rhport = 0; rhport < VHCI_NPORTS; rhport++) - if (the_controller->port_status[rhport] & - USB_PORT_STAT_CONNECTION) + for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) + if (vhci->port_status[rhport] & USB_PORT_STAT_CONNECTION) connected += 1; - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); if (connected > 0) { dev_info(&pdev->dev, @@ -1106,6 +1175,8 @@ static int vhci_hcd_resume(struct platform_device *pdev) dev_dbg(&pdev->dev, "%s\n", __func__); hcd = platform_get_drvdata(pdev); + if (!hcd) + return 0; set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); usb_hcd_poll_rh_status(hcd); @@ -1129,52 +1200,78 @@ static struct platform_driver vhci_driver = { }, }; -/* - * The VHCI 'device' is 'virtual'; not a real plug&play hardware. - * We need to add this virtual device as a platform device arbitrarily: - * 1. platform_device_register() - */ -static void the_pdev_release(struct device *dev) +static int add_platform_device(int id) { + struct platform_device *pdev; + int dev_nr; + + if (id == 0) + dev_nr = -1; + else + dev_nr = id; + + pdev = platform_device_register_simple(driver_name, dev_nr, NULL, 0); + if (pdev == NULL) + return -ENODEV; + + *(vhci_pdevs + id) = pdev; + return 0; } -static struct platform_device the_pdev = { - /* should be the same name as driver_name */ - .name = driver_name, - .id = -1, - .dev = { - .release = the_pdev_release, - }, -}; +static void del_platform_devices(void) +{ + struct platform_device *pdev; + int i; + + for (i = 0; i < vhci_num_controllers; i++) { + pdev = *(vhci_pdevs + i); + if (pdev != NULL) + platform_device_unregister(pdev); + *(vhci_pdevs + i) = NULL; + } + sysfs_remove_link(&platform_bus.kobj, driver_name); +} static int __init vhci_hcd_init(void) { - int ret; + int i, ret; if (usb_disabled()) return -ENODEV; + if (vhci_num_controllers < 1) + vhci_num_controllers = 1; + + vhci_pdevs = kcalloc(vhci_num_controllers, sizeof(void *), GFP_KERNEL); + if (vhci_pdevs == NULL) + return -ENOMEM; + ret = platform_driver_register(&vhci_driver); if (ret) goto err_driver_register; - ret = platform_device_register(&the_pdev); - if (ret) - goto err_platform_device_register; + for (i = 0; i < vhci_num_controllers; i++) { + ret = add_platform_device(i); + if (ret) + goto err_platform_device_register; + } pr_info(DRIVER_DESC " v" USBIP_VERSION "\n"); return ret; err_platform_device_register: + del_platform_devices(); platform_driver_unregister(&vhci_driver); err_driver_register: + kfree(vhci_pdevs); return ret; } static void __exit vhci_hcd_exit(void) { - platform_device_unregister(&the_pdev); + del_platform_devices(); platform_driver_unregister(&vhci_driver); + kfree(vhci_pdevs); } module_init(vhci_hcd_init); diff --git a/drivers/usb/usbip/vhci_rx.c b/drivers/usb/usbip/vhci_rx.c index d656e0edc3d5..fc2d319e2360 100644 --- a/drivers/usb/usbip/vhci_rx.c +++ b/drivers/usb/usbip/vhci_rx.c @@ -70,6 +70,7 @@ struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum) static void vhci_recv_ret_submit(struct vhci_device *vdev, struct usbip_header *pdu) { + struct vhci_hcd *vhci = vdev_to_vhci(vdev); struct usbip_device *ud = &vdev->ud; struct urb *urb; unsigned long flags; @@ -81,7 +82,7 @@ static void vhci_recv_ret_submit(struct vhci_device *vdev, if (!urb) { pr_err("cannot find a urb of seqnum %u\n", pdu->base.seqnum); pr_info("max seqnum %d\n", - atomic_read(&the_controller->seqnum)); + atomic_read(&vhci->seqnum)); usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); return; } @@ -105,11 +106,11 @@ static void vhci_recv_ret_submit(struct vhci_device *vdev, usbip_dbg_vhci_rx("now giveback urb %p\n", urb); - spin_lock_irqsave(&the_controller->lock, flags); - usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(vhci), urb); + spin_unlock_irqrestore(&vhci->lock, flags); - usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status); + usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status); usbip_dbg_vhci_rx("Leave\n"); } @@ -142,6 +143,7 @@ static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev, static void vhci_recv_ret_unlink(struct vhci_device *vdev, struct usbip_header *pdu) { + struct vhci_hcd *vhci = vdev_to_vhci(vdev); struct vhci_unlink *unlink; struct urb *urb; unsigned long flags; @@ -174,12 +176,11 @@ static void vhci_recv_ret_unlink(struct vhci_device *vdev, urb->status = pdu->u.ret_unlink.status; pr_info("urb->status %d\n", urb->status); - spin_lock_irqsave(&the_controller->lock, flags); - usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(vhci), urb); + spin_unlock_irqrestore(&vhci->lock, flags); - usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, - urb->status); + usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status); } kfree(unlink); diff --git a/drivers/usb/usbip/vhci_sysfs.c b/drivers/usb/usbip/vhci_sysfs.c index 5b5462eb1665..c404017c1b5a 100644 --- a/drivers/usb/usbip/vhci_sysfs.c +++ b/drivers/usb/usbip/vhci_sysfs.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2003-2008 Takahiro Hirofuchi + * Copyright (C) 2015-2016 Nobuo Iwata * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +21,8 @@ #include #include #include +#include +#include #include "usbip_common.h" #include "vhci.h" @@ -27,106 +30,190 @@ /* TODO: refine locking ?*/ /* Sysfs entry to show port status */ -static ssize_t status_show(struct device *dev, struct device_attribute *attr, - char *out) +static ssize_t status_show_vhci(int pdev_nr, char *out) { + struct platform_device *pdev = *(vhci_pdevs + pdev_nr); + struct vhci_hcd *vhci; char *s = out; int i = 0; unsigned long flags; - BUG_ON(!the_controller || !out); + if (!pdev || !out) { + usbip_dbg_vhci_sysfs("show status error\n"); + return 0; + } + + vhci = hcd_to_vhci(platform_get_drvdata(pdev)); - spin_lock_irqsave(&the_controller->lock, flags); + spin_lock_irqsave(&vhci->lock, flags); /* * output example: - * prt sta spd dev socket local_busid - * 000 004 000 000 c5a7bb80 1-2.3 - * 001 004 000 000 d8cee980 2-3.4 + * port sta spd dev socket local_busid + * 0000 004 000 00000000 c5a7bb80 1-2.3 + * 0001 004 000 00000000 d8cee980 2-3.4 * * IP address can be retrieved from a socket pointer address by looking * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a * port number and its peer IP address. */ - out += sprintf(out, - "prt sta spd bus dev socket local_busid\n"); - - for (i = 0; i < VHCI_NPORTS; i++) { - struct vhci_device *vdev = port_to_vdev(i); + for (i = 0; i < VHCI_HC_PORTS; i++) { + struct vhci_device *vdev = &vhci->vdev[i]; spin_lock(&vdev->ud.lock); - out += sprintf(out, "%03u %03u ", i, vdev->ud.status); + out += sprintf(out, "%04u %03u ", + (pdev_nr * VHCI_HC_PORTS) + i, + vdev->ud.status); if (vdev->ud.status == VDEV_ST_USED) { out += sprintf(out, "%03u %08x ", - vdev->speed, vdev->devid); - out += sprintf(out, "%16p ", vdev->ud.tcp_socket); - out += sprintf(out, "%s", dev_name(&vdev->udev->dev)); + vdev->speed, vdev->devid); + out += sprintf(out, "%16p %s", + vdev->ud.tcp_socket, + dev_name(&vdev->udev->dev)); } else { - out += sprintf(out, "000 000 000 0000000000000000 0-0"); + out += sprintf(out, "000 00000000 "); + out += sprintf(out, "0000000000000000 0-0"); } out += sprintf(out, "\n"); spin_unlock(&vdev->ud.lock); } - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); + + return out - s; +} + +static ssize_t status_show_not_ready(int pdev_nr, char *out) +{ + char *s = out; + int i = 0; + + for (i = 0; i < VHCI_HC_PORTS; i++) { + out += sprintf(out, "%04u %03u ", + (pdev_nr * VHCI_HC_PORTS) + i, + VDEV_ST_NOTASSIGNED); + out += sprintf(out, "000 00000000 0000000000000000 0-0"); + out += sprintf(out, "\n"); + } + return out - s; +} + +static int status_name_to_id(const char *name) +{ + char *c; + long val; + int ret; + + c = strchr(name, '.'); + if (c == NULL) + return 0; + ret = kstrtol(c+1, 10, &val); + if (ret < 0) + return ret; + + return val; +} + +static ssize_t status_show(struct device *dev, + struct device_attribute *attr, char *out) +{ + char *s = out; + int pdev_nr; + + out += sprintf(out, + "port sta spd dev socket local_busid\n"); + + pdev_nr = status_name_to_id(attr->attr.name); + if (pdev_nr < 0) + out += status_show_not_ready(pdev_nr, out); + else + out += status_show_vhci(pdev_nr, out); + + return out - s; +} + +static ssize_t nports_show(struct device *dev, struct device_attribute *attr, + char *out) +{ + char *s = out; + + out += sprintf(out, "%d\n", VHCI_HC_PORTS * vhci_num_controllers); return out - s; } -static DEVICE_ATTR_RO(status); +static DEVICE_ATTR_RO(nports); /* Sysfs entry to shutdown a virtual connection */ -static int vhci_port_disconnect(__u32 rhport) +static int vhci_port_disconnect(struct vhci_hcd *vhci, __u32 rhport) { - struct vhci_device *vdev; + struct vhci_device *vdev = &vhci->vdev[rhport]; unsigned long flags; usbip_dbg_vhci_sysfs("enter\n"); /* lock */ - spin_lock_irqsave(&the_controller->lock, flags); - - vdev = port_to_vdev(rhport); - + spin_lock_irqsave(&vhci->lock, flags); spin_lock(&vdev->ud.lock); + if (vdev->ud.status == VDEV_ST_NULL) { pr_err("not connected %d\n", vdev->ud.status); /* unlock */ spin_unlock(&vdev->ud.lock); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); return -EINVAL; } /* unlock */ spin_unlock(&vdev->ud.lock); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); return 0; } +static int valid_port(__u32 pdev_nr, __u32 rhport) +{ + if (pdev_nr >= vhci_num_controllers) { + pr_err("pdev %u\n", pdev_nr); + return 0; + } + if (rhport >= VHCI_HC_PORTS) { + pr_err("rhport %u\n", rhport); + return 0; + } + return 1; +} + static ssize_t store_detach(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - int err; - __u32 rhport = 0; + __u32 port = 0, pdev_nr = 0, rhport = 0; + struct usb_hcd *hcd; + int ret; - if (sscanf(buf, "%u", &rhport) != 1) + if (kstrtoint(buf, 10, &port) < 0) return -EINVAL; - /* check rhport */ - if (rhport >= VHCI_NPORTS) { - dev_err(dev, "invalid port %u\n", rhport); + pdev_nr = port_to_pdev_nr(port); + rhport = port_to_rhport(port); + + if (!valid_port(pdev_nr, rhport)) return -EINVAL; + + hcd = platform_get_drvdata(*(vhci_pdevs + pdev_nr)); + if (hcd == NULL) { + dev_err(dev, "port is not ready %u\n", port); + return -EAGAIN; } - err = vhci_port_disconnect(rhport); - if (err < 0) + ret = vhci_port_disconnect(hcd_to_vhci(hcd), rhport); + if (ret < 0) return -EINVAL; usbip_dbg_vhci_sysfs("Leave\n"); @@ -135,16 +222,12 @@ static ssize_t store_detach(struct device *dev, struct device_attribute *attr, } static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach); -/* Sysfs entry to establish a virtual connection */ -static int valid_args(__u32 rhport, enum usb_device_speed speed) +static int valid_args(__u32 pdev_nr, __u32 rhport, enum usb_device_speed speed) { - /* check rhport */ - if (rhport >= VHCI_NPORTS) { - pr_err("port %u\n", rhport); - return -EINVAL; + if (!valid_port(pdev_nr, rhport)) { + return 0; } - /* check speed */ switch (speed) { case USB_SPEED_LOW: case USB_SPEED_FULL: @@ -154,12 +237,13 @@ static int valid_args(__u32 rhport, enum usb_device_speed speed) default: pr_err("Failed attach request for unsupported USB speed: %s\n", usb_speed_string(speed)); - return -EINVAL; + return 0; } - return 0; + return 1; } +/* Sysfs entry to establish a virtual connection */ /* * To start a new USB/IP attachment, a userland program needs to setup a TCP * connection and then write its socket descriptor with remote device @@ -174,10 +258,12 @@ static int valid_args(__u32 rhport, enum usb_device_speed speed) static ssize_t store_attach(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct vhci_device *vdev; struct socket *socket; int sockfd = 0; - __u32 rhport = 0, devid = 0, speed = 0; + __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0; + struct usb_hcd *hcd; + struct vhci_hcd *vhci; + struct vhci_device *vdev; int err; unsigned long flags; @@ -187,16 +273,28 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr, * @devid: unique device identifier in a remote host * @speed: usb device speed in a remote host */ - if (sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed) != 4) + if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4) return -EINVAL; + pdev_nr = port_to_pdev_nr(port); + rhport = port_to_rhport(port); - usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n", - rhport, sockfd, devid, speed); + usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n", + port, pdev_nr, rhport); + usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n", + sockfd, devid, speed); /* check received parameters */ - if (valid_args(rhport, speed) < 0) + if (!valid_args(pdev_nr, rhport, speed)) return -EINVAL; + hcd = platform_get_drvdata(*(vhci_pdevs + pdev_nr)); + if (hcd == NULL) { + dev_err(dev, "port %d is not ready\n", port); + return -EAGAIN; + } + vhci = hcd_to_vhci(hcd); + vdev = &vhci->vdev[rhport]; + /* Extract socket from fd. */ socket = sockfd_lookup(sockfd, &err); if (!socket) @@ -205,14 +303,13 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr, /* now need lock until setting vdev status as used */ /* begin a lock */ - spin_lock_irqsave(&the_controller->lock, flags); - vdev = port_to_vdev(rhport); + spin_lock_irqsave(&vhci->lock, flags); spin_lock(&vdev->ud.lock); if (vdev->ud.status != VDEV_ST_NULL) { /* end of the lock */ spin_unlock(&vdev->ud.lock); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); sockfd_put(socket); @@ -220,9 +317,10 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr, return -EINVAL; } - dev_info(dev, - "rhport(%u) sockfd(%d) devid(%u) speed(%u) speed_str(%s)\n", - rhport, sockfd, devid, speed, usb_speed_string(speed)); + dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n", + pdev_nr, rhport, sockfd); + dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n", + devid, speed, usb_speed_string(speed)); vdev->devid = devid; vdev->speed = speed; @@ -230,26 +328,92 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr, vdev->ud.status = VDEV_ST_NOTASSIGNED; spin_unlock(&vdev->ud.lock); - spin_unlock_irqrestore(&the_controller->lock, flags); + spin_unlock_irqrestore(&vhci->lock, flags); /* end the lock */ vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx"); vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx"); - rh_port_connect(rhport, speed); + rh_port_connect(vdev, speed); return count; } static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach); -static struct attribute *dev_attrs[] = { - &dev_attr_status.attr, - &dev_attr_detach.attr, - &dev_attr_attach.attr, - &dev_attr_usbip_debug.attr, - NULL, +#define MAX_STATUS_NAME 16 + +struct status_attr { + struct device_attribute attr; + char name[MAX_STATUS_NAME+1]; }; -const struct attribute_group dev_attr_group = { - .attrs = dev_attrs, +static struct status_attr *status_attrs; + +static void set_status_attr(int id) +{ + struct status_attr *status; + + status = status_attrs + id; + if (id == 0) + strcpy(status->name, "status"); + else + snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id); + status->attr.attr.name = status->name; + status->attr.attr.mode = S_IRUGO; + status->attr.show = status_show; +} + +static int init_status_attrs(void) +{ + int id; + + status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr), + GFP_KERNEL); + if (status_attrs == NULL) + return -ENOMEM; + + for (id = 0; id < vhci_num_controllers; id++) + set_status_attr(id); + + return 0; +} + +static void finish_status_attrs(void) +{ + kfree(status_attrs); +} + +struct attribute_group vhci_attr_group = { + .attrs = NULL, }; + +int vhci_init_attr_group(void) +{ + struct attribute **attrs; + int ret, i; + + attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *), + GFP_KERNEL); + if (attrs == NULL) + return -ENOMEM; + + ret = init_status_attrs(); + if (ret) { + kfree(attrs); + return ret; + } + *attrs = &dev_attr_nports.attr; + *(attrs + 1) = &dev_attr_detach.attr; + *(attrs + 2) = &dev_attr_attach.attr; + *(attrs + 3) = &dev_attr_usbip_debug.attr; + for (i = 0; i < vhci_num_controllers; i++) + *(attrs + i + 4) = &((status_attrs + i)->attr.attr); + vhci_attr_group.attrs = attrs; + return 0; +} + +void vhci_finish_attr_group(void) +{ + finish_status_attrs(); + kfree(vhci_attr_group.attrs); +} -- cgit v1.2.3