aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorNobuo Iwata <nobuo.iwata@fujixerox.co.jp>2016-06-12 22:33:40 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2016-08-09 10:21:05 -0400
commit0775a9cbc694e8c7276688be3bbd2f386167ab54 (patch)
tree755007b95365a60e76f7a1a208840f5b57920654 /drivers/usb
parent0573f2c519e570d056989e57565e1ff1640dd794 (diff)
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 <nobuo.iwata@fujixerox.co.jp> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/usbip/Kconfig21
-rw-r--r--drivers/usb/usbip/vhci.h54
-rw-r--r--drivers/usb/usbip/vhci_hcd.c285
-rw-r--r--drivers/usb/usbip/vhci_rx.c21
-rw-r--r--drivers/usb/usbip/vhci_sysfs.c296
5 files changed, 497 insertions, 180 deletions
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
24 To compile this driver as a module, choose M here: the 24 To compile this driver as a module, choose M here: the
25 module will be called vhci-hcd. 25 module will be called vhci-hcd.
26 26
27config USBIP_VHCI_HC_PORTS
28 int "Number of ports per USB/IP virtual host controller"
29 range 1 31
30 default 8
31 depends on USBIP_VHCI_HCD
32 ---help---
33 To increase number of ports available for USB/IP virtual
34 host controller driver, this defines number of ports per
35 USB/IP virtual host controller.
36
37config USBIP_VHCI_NR_HCS
38 int "Number of USB/IP virtual host controllers"
39 range 1 128
40 default 1
41 depends on USBIP_VHCI_HCD
42 ---help---
43 To increase number of ports available for USB/IP virtual
44 host controller driver, this defines number of USB/IP
45 virtual host controllers as if adding physical host
46 controllers.
47
27config USBIP_HOST 48config USBIP_HOST
28 tristate "Host driver" 49 tristate "Host driver"
29 depends on USBIP_CORE && USB 50 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 @@
1/* 1/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi 2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015 Nobuo Iwata
3 * 4 *
4 * This is free software; you can redistribute it and/or modify 5 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
@@ -72,13 +73,25 @@ struct vhci_unlink {
72}; 73};
73 74
74/* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */ 75/* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */
75#define VHCI_NPORTS 8 76#ifdef CONFIG_USBIP_VHCI_HC_PORTS
77#define VHCI_HC_PORTS CONFIG_USBIP_VHCI_HC_PORTS
78#else
79#define VHCI_HC_PORTS 8
80#endif
81
82#ifdef CONFIG_USBIP_VHCI_NR_HCS
83#define VHCI_NR_HCS CONFIG_USBIP_VHCI_NR_HCS
84#else
85#define VHCI_NR_HCS 1
86#endif
87
88#define MAX_STATUS_NAME 16
76 89
77/* for usb_bus.hcpriv */ 90/* for usb_bus.hcpriv */
78struct vhci_hcd { 91struct vhci_hcd {
79 spinlock_t lock; 92 spinlock_t lock;
80 93
81 u32 port_status[VHCI_NPORTS]; 94 u32 port_status[VHCI_HC_PORTS];
82 95
83 unsigned resuming:1; 96 unsigned resuming:1;
84 unsigned long re_timeout; 97 unsigned long re_timeout;
@@ -90,14 +103,19 @@ struct vhci_hcd {
90 * wIndex shows the port number and begins from 1. 103 * wIndex shows the port number and begins from 1.
91 * But, the index of this array begins from 0. 104 * But, the index of this array begins from 0.
92 */ 105 */
93 struct vhci_device vdev[VHCI_NPORTS]; 106 struct vhci_device vdev[VHCI_HC_PORTS];
94}; 107};
95 108
96extern struct vhci_hcd *the_controller; 109extern int vhci_num_controllers;
97extern const struct attribute_group dev_attr_group; 110extern struct platform_device **vhci_pdevs;
111extern struct attribute_group vhci_attr_group;
98 112
99/* vhci_hcd.c */ 113/* vhci_hcd.c */
100void rh_port_connect(int rhport, enum usb_device_speed speed); 114void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed);
115
116/* vhci_sysfs.c */
117int vhci_init_attr_group(void);
118void vhci_finish_attr_group(void);
101 119
102/* vhci_rx.c */ 120/* vhci_rx.c */
103struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum); 121struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum);
@@ -106,9 +124,14 @@ int vhci_rx_loop(void *data);
106/* vhci_tx.c */ 124/* vhci_tx.c */
107int vhci_tx_loop(void *data); 125int vhci_tx_loop(void *data);
108 126
109static inline struct vhci_device *port_to_vdev(__u32 port) 127static inline __u32 port_to_rhport(__u32 port)
128{
129 return port % VHCI_HC_PORTS;
130}
131
132static inline int port_to_pdev_nr(__u32 port)
110{ 133{
111 return &the_controller->vdev[port]; 134 return port / VHCI_HC_PORTS;
112} 135}
113 136
114static inline struct vhci_hcd *hcd_to_vhci(struct usb_hcd *hcd) 137static 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)
116 return (struct vhci_hcd *) (hcd->hcd_priv); 139 return (struct vhci_hcd *) (hcd->hcd_priv);
117} 140}
118 141
142static inline struct device *hcd_dev(struct usb_hcd *hcd)
143{
144 return (hcd)->self.controller;
145}
146
147static inline const char *hcd_name(struct usb_hcd *hcd)
148{
149 return (hcd)->self.bus_name;
150}
151
119static inline struct usb_hcd *vhci_to_hcd(struct vhci_hcd *vhci) 152static inline struct usb_hcd *vhci_to_hcd(struct vhci_hcd *vhci)
120{ 153{
121 return container_of((void *) vhci, struct usb_hcd, hcd_priv); 154 return container_of((void *) vhci, struct usb_hcd, hcd_priv);
122} 155}
123 156
124static inline struct device *vhci_dev(struct vhci_hcd *vhci) 157static inline struct vhci_hcd *vdev_to_vhci(struct vhci_device *vdev)
125{ 158{
126 return vhci_to_hcd(vhci)->self.controller; 159 return container_of(
160 (void *)(vdev - vdev->rhport), struct vhci_hcd, vdev);
127} 161}
128 162
129#endif /* __USBIP_VHCI_H */ 163#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 @@
1/* 1/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi 2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015-2016 Nobuo Iwata
3 * 4 *
4 * This is free software; you can redistribute it and/or modify 5 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 6 * 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);
56static const char driver_name[] = "vhci_hcd"; 57static const char driver_name[] = "vhci_hcd";
57static const char driver_desc[] = "USB/IP Virtual Host Controller"; 58static const char driver_desc[] = "USB/IP Virtual Host Controller";
58 59
59struct vhci_hcd *the_controller; 60int vhci_num_controllers = VHCI_NR_HCS;
61
62struct platform_device **vhci_pdevs;
60 63
61static const char * const bit_desc[] = { 64static const char * const bit_desc[] = {
62 "CONNECTION", /*0*/ 65 "CONNECTION", /*0*/
@@ -119,47 +122,59 @@ static void dump_port_status_diff(u32 prev_status, u32 new_status)
119 pr_debug("\n"); 122 pr_debug("\n");
120} 123}
121 124
122void rh_port_connect(int rhport, enum usb_device_speed speed) 125void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed)
123{ 126{
127 struct vhci_hcd *vhci = vdev_to_vhci(vdev);
128 int rhport = vdev->rhport;
129 u32 status;
124 unsigned long flags; 130 unsigned long flags;
125 131
126 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport); 132 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
127 133
128 spin_lock_irqsave(&the_controller->lock, flags); 134 spin_lock_irqsave(&vhci->lock, flags);
135
136 status = vhci->port_status[rhport];
129 137
130 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION 138 status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION);
131 | (1 << USB_PORT_FEAT_C_CONNECTION);
132 139
133 switch (speed) { 140 switch (speed) {
134 case USB_SPEED_HIGH: 141 case USB_SPEED_HIGH:
135 the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED; 142 status |= USB_PORT_STAT_HIGH_SPEED;
136 break; 143 break;
137 case USB_SPEED_LOW: 144 case USB_SPEED_LOW:
138 the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED; 145 status |= USB_PORT_STAT_LOW_SPEED;
139 break; 146 break;
140 default: 147 default:
141 break; 148 break;
142 } 149 }
143 150
144 spin_unlock_irqrestore(&the_controller->lock, flags); 151 vhci->port_status[rhport] = status;
152
153 spin_unlock_irqrestore(&vhci->lock, flags);
145 154
146 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller)); 155 usb_hcd_poll_rh_status(vhci_to_hcd(vhci));
147} 156}
148 157
149static void rh_port_disconnect(int rhport) 158static void rh_port_disconnect(struct vhci_device *vdev)
150{ 159{
160 struct vhci_hcd *vhci = vdev_to_vhci(vdev);
161 int rhport = vdev->rhport;
162 u32 status;
151 unsigned long flags; 163 unsigned long flags;
152 164
153 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport); 165 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
154 166
155 spin_lock_irqsave(&the_controller->lock, flags); 167 spin_lock_irqsave(&vhci->lock, flags);
168
169 status = vhci->port_status[rhport];
170
171 status &= ~USB_PORT_STAT_CONNECTION;
172 status |= (1 << USB_PORT_FEAT_C_CONNECTION);
156 173
157 the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION; 174 vhci->port_status[rhport] = status;
158 the_controller->port_status[rhport] |=
159 (1 << USB_PORT_FEAT_C_CONNECTION);
160 175
161 spin_unlock_irqrestore(&the_controller->lock, flags); 176 spin_unlock_irqrestore(&vhci->lock, flags);
162 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller)); 177 usb_hcd_poll_rh_status(vhci_to_hcd(vhci));
163} 178}
164 179
165#define PORT_C_MASK \ 180#define PORT_C_MASK \
@@ -188,7 +203,7 @@ static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
188 int changed = 0; 203 int changed = 0;
189 unsigned long flags; 204 unsigned long flags;
190 205
191 retval = DIV_ROUND_UP(VHCI_NPORTS + 1, 8); 206 retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8);
192 memset(buf, 0, retval); 207 memset(buf, 0, retval);
193 208
194 vhci = hcd_to_vhci(hcd); 209 vhci = hcd_to_vhci(hcd);
@@ -200,7 +215,7 @@ static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
200 } 215 }
201 216
202 /* check pseudo status register for each port */ 217 /* check pseudo status register for each port */
203 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { 218 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
204 if ((vhci->port_status[rhport] & PORT_C_MASK)) { 219 if ((vhci->port_status[rhport] & PORT_C_MASK)) {
205 /* The status of a port has been changed, */ 220 /* The status of a port has been changed, */
206 usbip_dbg_vhci_rh("port %d status changed\n", rhport); 221 usbip_dbg_vhci_rh("port %d status changed\n", rhport);
@@ -225,7 +240,7 @@ static inline void hub_descriptor(struct usb_hub_descriptor *desc)
225 desc->bDescLength = 9; 240 desc->bDescLength = 9;
226 desc->wHubCharacteristics = cpu_to_le16( 241 desc->wHubCharacteristics = cpu_to_le16(
227 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM); 242 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
228 desc->bNbrPorts = VHCI_NPORTS; 243 desc->bNbrPorts = VHCI_HC_PORTS;
229 desc->u.hs.DeviceRemovable[0] = 0xff; 244 desc->u.hs.DeviceRemovable[0] = 0xff;
230 desc->u.hs.DeviceRemovable[1] = 0xff; 245 desc->u.hs.DeviceRemovable[1] = 0xff;
231} 246}
@@ -238,7 +253,7 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
238 int rhport; 253 int rhport;
239 unsigned long flags; 254 unsigned long flags;
240 255
241 u32 prev_port_status[VHCI_NPORTS]; 256 u32 prev_port_status[VHCI_HC_PORTS];
242 257
243 if (!HCD_HW_ACCESSIBLE(hcd)) 258 if (!HCD_HW_ACCESSIBLE(hcd))
244 return -ETIMEDOUT; 259 return -ETIMEDOUT;
@@ -249,7 +264,7 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
249 */ 264 */
250 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue, 265 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
251 wIndex); 266 wIndex);
252 if (wIndex > VHCI_NPORTS) 267 if (wIndex > VHCI_HC_PORTS)
253 pr_err("invalid port number %d\n", wIndex); 268 pr_err("invalid port number %d\n", wIndex);
254 rhport = ((__u8)(wIndex & 0x00ff)) - 1; 269 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
255 270
@@ -315,7 +330,7 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
315 break; 330 break;
316 case GetPortStatus: 331 case GetPortStatus:
317 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex); 332 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
318 if (wIndex > VHCI_NPORTS || wIndex < 1) { 333 if (wIndex > VHCI_HC_PORTS || wIndex < 1) {
319 pr_err("invalid port number %d\n", wIndex); 334 pr_err("invalid port number %d\n", wIndex);
320 retval = -EPIPE; 335 retval = -EPIPE;
321 } 336 }
@@ -416,14 +431,27 @@ static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
416 431
417static struct vhci_device *get_vdev(struct usb_device *udev) 432static struct vhci_device *get_vdev(struct usb_device *udev)
418{ 433{
419 int i; 434 struct platform_device *pdev;
435 struct usb_hcd *hcd;
436 struct vhci_hcd *vhci;
437 int pdev_nr, rhport;
420 438
421 if (!udev) 439 if (!udev)
422 return NULL; 440 return NULL;
423 441
424 for (i = 0; i < VHCI_NPORTS; i++) 442 for (pdev_nr = 0; pdev_nr < vhci_num_controllers; pdev_nr++) {
425 if (the_controller->vdev[i].udev == udev) 443 pdev = *(vhci_pdevs + pdev_nr);
426 return port_to_vdev(i); 444 if (pdev == NULL)
445 continue;
446 hcd = platform_get_drvdata(pdev);
447 if (hcd == NULL)
448 continue;
449 vhci = hcd_to_vhci(hcd);
450 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
451 if (vhci->vdev[rhport].udev == udev)
452 return &vhci->vdev[rhport];
453 }
454 }
427 455
428 return NULL; 456 return NULL;
429} 457}
@@ -432,6 +460,7 @@ static void vhci_tx_urb(struct urb *urb)
432{ 460{
433 struct vhci_device *vdev = get_vdev(urb->dev); 461 struct vhci_device *vdev = get_vdev(urb->dev);
434 struct vhci_priv *priv; 462 struct vhci_priv *priv;
463 struct vhci_hcd *vhci = vdev_to_vhci(vdev);
435 unsigned long flags; 464 unsigned long flags;
436 465
437 if (!vdev) { 466 if (!vdev) {
@@ -447,7 +476,7 @@ static void vhci_tx_urb(struct urb *urb)
447 476
448 spin_lock_irqsave(&vdev->priv_lock, flags); 477 spin_lock_irqsave(&vdev->priv_lock, flags);
449 478
450 priv->seqnum = atomic_inc_return(&the_controller->seqnum); 479 priv->seqnum = atomic_inc_return(&vhci->seqnum);
451 if (priv->seqnum == 0xffff) 480 if (priv->seqnum == 0xffff)
452 dev_info(&urb->dev->dev, "seqnum max\n"); 481 dev_info(&urb->dev->dev, "seqnum max\n");
453 482
@@ -465,7 +494,9 @@ static void vhci_tx_urb(struct urb *urb)
465static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, 494static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
466 gfp_t mem_flags) 495 gfp_t mem_flags)
467{ 496{
497 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
468 struct device *dev = &urb->dev->dev; 498 struct device *dev = &urb->dev->dev;
499 u8 portnum = urb->dev->portnum;
469 int ret = 0; 500 int ret = 0;
470 struct vhci_device *vdev; 501 struct vhci_device *vdev;
471 unsigned long flags; 502 unsigned long flags;
@@ -473,26 +504,30 @@ static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
473 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n", 504 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
474 hcd, urb, mem_flags); 505 hcd, urb, mem_flags);
475 506
507 if (portnum > VHCI_HC_PORTS) {
508 pr_err("invalid port number %d\n", portnum);
509 return -ENODEV;
510 }
511 vdev = &vhci->vdev[portnum-1];
512
476 /* patch to usb_sg_init() is in 2.5.60 */ 513 /* patch to usb_sg_init() is in 2.5.60 */
477 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length); 514 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
478 515
479 spin_lock_irqsave(&the_controller->lock, flags); 516 spin_lock_irqsave(&vhci->lock, flags);
480 517
481 if (urb->status != -EINPROGRESS) { 518 if (urb->status != -EINPROGRESS) {
482 dev_err(dev, "URB already unlinked!, status %d\n", urb->status); 519 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
483 spin_unlock_irqrestore(&the_controller->lock, flags); 520 spin_unlock_irqrestore(&vhci->lock, flags);
484 return urb->status; 521 return urb->status;
485 } 522 }
486 523
487 vdev = port_to_vdev(urb->dev->portnum-1);
488
489 /* refuse enqueue for dead connection */ 524 /* refuse enqueue for dead connection */
490 spin_lock(&vdev->ud.lock); 525 spin_lock(&vdev->ud.lock);
491 if (vdev->ud.status == VDEV_ST_NULL || 526 if (vdev->ud.status == VDEV_ST_NULL ||
492 vdev->ud.status == VDEV_ST_ERROR) { 527 vdev->ud.status == VDEV_ST_ERROR) {
493 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport); 528 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
494 spin_unlock(&vdev->ud.lock); 529 spin_unlock(&vdev->ud.lock);
495 spin_unlock_irqrestore(&the_controller->lock, flags); 530 spin_unlock_irqrestore(&vhci->lock, flags);
496 return -ENODEV; 531 return -ENODEV;
497 } 532 }
498 spin_unlock(&vdev->ud.lock); 533 spin_unlock(&vdev->ud.lock);
@@ -565,17 +600,16 @@ static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
565 600
566out: 601out:
567 vhci_tx_urb(urb); 602 vhci_tx_urb(urb);
568 spin_unlock_irqrestore(&the_controller->lock, flags); 603 spin_unlock_irqrestore(&vhci->lock, flags);
569 604
570 return 0; 605 return 0;
571 606
572no_need_xmit: 607no_need_xmit:
573 usb_hcd_unlink_urb_from_ep(hcd, urb); 608 usb_hcd_unlink_urb_from_ep(hcd, urb);
574no_need_unlink: 609no_need_unlink:
575 spin_unlock_irqrestore(&the_controller->lock, flags); 610 spin_unlock_irqrestore(&vhci->lock, flags);
576 if (!ret) 611 if (!ret)
577 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), 612 usb_hcd_giveback_urb(hcd, urb, urb->status);
578 urb, urb->status);
579 return ret; 613 return ret;
580} 614}
581 615
@@ -627,19 +661,20 @@ no_need_unlink:
627 */ 661 */
628static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 662static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
629{ 663{
664 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
630 struct vhci_priv *priv; 665 struct vhci_priv *priv;
631 struct vhci_device *vdev; 666 struct vhci_device *vdev;
632 unsigned long flags; 667 unsigned long flags;
633 668
634 pr_info("dequeue a urb %p\n", urb); 669 pr_info("dequeue a urb %p\n", urb);
635 670
636 spin_lock_irqsave(&the_controller->lock, flags); 671 spin_lock_irqsave(&vhci->lock, flags);
637 672
638 priv = urb->hcpriv; 673 priv = urb->hcpriv;
639 if (!priv) { 674 if (!priv) {
640 /* URB was never linked! or will be soon given back by 675 /* URB was never linked! or will be soon given back by
641 * vhci_rx. */ 676 * vhci_rx. */
642 spin_unlock_irqrestore(&the_controller->lock, flags); 677 spin_unlock_irqrestore(&vhci->lock, flags);
643 return -EIDRM; 678 return -EIDRM;
644 } 679 }
645 680
@@ -648,7 +683,7 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
648 683
649 ret = usb_hcd_check_unlink_urb(hcd, urb, status); 684 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
650 if (ret) { 685 if (ret) {
651 spin_unlock_irqrestore(&the_controller->lock, flags); 686 spin_unlock_irqrestore(&vhci->lock, flags);
652 return ret; 687 return ret;
653 } 688 }
654 } 689 }
@@ -676,10 +711,9 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
676 711
677 usb_hcd_unlink_urb_from_ep(hcd, urb); 712 usb_hcd_unlink_urb_from_ep(hcd, urb);
678 713
679 spin_unlock_irqrestore(&the_controller->lock, flags); 714 spin_unlock_irqrestore(&vhci->lock, flags);
680 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, 715 usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status);
681 urb->status); 716 spin_lock_irqsave(&vhci->lock, flags);
682 spin_lock_irqsave(&the_controller->lock, flags);
683 717
684 } else { 718 } else {
685 /* tcp connection is alive */ 719 /* tcp connection is alive */
@@ -691,12 +725,12 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
691 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC); 725 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
692 if (!unlink) { 726 if (!unlink) {
693 spin_unlock(&vdev->priv_lock); 727 spin_unlock(&vdev->priv_lock);
694 spin_unlock_irqrestore(&the_controller->lock, flags); 728 spin_unlock_irqrestore(&vhci->lock, flags);
695 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC); 729 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
696 return -ENOMEM; 730 return -ENOMEM;
697 } 731 }
698 732
699 unlink->seqnum = atomic_inc_return(&the_controller->seqnum); 733 unlink->seqnum = atomic_inc_return(&vhci->seqnum);
700 if (unlink->seqnum == 0xffff) 734 if (unlink->seqnum == 0xffff)
701 pr_info("seqnum max\n"); 735 pr_info("seqnum max\n");
702 736
@@ -712,7 +746,7 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
712 spin_unlock(&vdev->priv_lock); 746 spin_unlock(&vdev->priv_lock);
713 } 747 }
714 748
715 spin_unlock_irqrestore(&the_controller->lock, flags); 749 spin_unlock_irqrestore(&vhci->lock, flags);
716 750
717 usbip_dbg_vhci_hc("leave\n"); 751 usbip_dbg_vhci_hc("leave\n");
718 return 0; 752 return 0;
@@ -720,10 +754,12 @@ static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
720 754
721static void vhci_device_unlink_cleanup(struct vhci_device *vdev) 755static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
722{ 756{
757 struct vhci_hcd *vhci = vdev_to_vhci(vdev);
758 struct usb_hcd *hcd = vhci_to_hcd(vhci);
723 struct vhci_unlink *unlink, *tmp; 759 struct vhci_unlink *unlink, *tmp;
724 unsigned long flags; 760 unsigned long flags;
725 761
726 spin_lock_irqsave(&the_controller->lock, flags); 762 spin_lock_irqsave(&vhci->lock, flags);
727 spin_lock(&vdev->priv_lock); 763 spin_lock(&vdev->priv_lock);
728 764
729 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) { 765 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)
752 788
753 urb->status = -ENODEV; 789 urb->status = -ENODEV;
754 790
755 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); 791 usb_hcd_unlink_urb_from_ep(hcd, urb);
756 792
757 list_del(&unlink->list); 793 list_del(&unlink->list);
758 794
759 spin_unlock(&vdev->priv_lock); 795 spin_unlock(&vdev->priv_lock);
760 spin_unlock_irqrestore(&the_controller->lock, flags); 796 spin_unlock_irqrestore(&vhci->lock, flags);
761 797
762 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, 798 usb_hcd_giveback_urb(hcd, urb, urb->status);
763 urb->status);
764 799
765 spin_lock_irqsave(&the_controller->lock, flags); 800 spin_lock_irqsave(&vhci->lock, flags);
766 spin_lock(&vdev->priv_lock); 801 spin_lock(&vdev->priv_lock);
767 802
768 kfree(unlink); 803 kfree(unlink);
769 } 804 }
770 805
771 spin_unlock(&vdev->priv_lock); 806 spin_unlock(&vdev->priv_lock);
772 spin_unlock_irqrestore(&the_controller->lock, flags); 807 spin_unlock_irqrestore(&vhci->lock, flags);
773} 808}
774 809
775/* 810/*
@@ -827,7 +862,7 @@ static void vhci_shutdown_connection(struct usbip_device *ud)
827 * is actually given back by vhci_rx after receiving its return pdu. 862 * is actually given back by vhci_rx after receiving its return pdu.
828 * 863 *
829 */ 864 */
830 rh_port_disconnect(vdev->rhport); 865 rh_port_disconnect(vdev);
831 866
832 pr_info("disconnect device\n"); 867 pr_info("disconnect device\n");
833} 868}
@@ -866,7 +901,7 @@ static void vhci_device_unusable(struct usbip_device *ud)
866 901
867static void vhci_device_init(struct vhci_device *vdev) 902static void vhci_device_init(struct vhci_device *vdev)
868{ 903{
869 memset(vdev, 0, sizeof(*vdev)); 904 memset(vdev, 0, sizeof(struct vhci_device));
870 905
871 vdev->ud.side = USBIP_VHCI; 906 vdev->ud.side = USBIP_VHCI;
872 vdev->ud.status = VDEV_ST_NULL; 907 vdev->ud.status = VDEV_ST_NULL;
@@ -887,17 +922,34 @@ static void vhci_device_init(struct vhci_device *vdev)
887 usbip_start_eh(&vdev->ud); 922 usbip_start_eh(&vdev->ud);
888} 923}
889 924
925static int hcd_name_to_id(const char *name)
926{
927 char *c;
928 long val;
929 int ret;
930
931 c = strchr(name, '.');
932 if (c == NULL)
933 return 0;
934
935 ret = kstrtol(c+1, 10, &val);
936 if (ret < 0)
937 return ret;
938
939 return val;
940}
941
890static int vhci_start(struct usb_hcd *hcd) 942static int vhci_start(struct usb_hcd *hcd)
891{ 943{
892 struct vhci_hcd *vhci = hcd_to_vhci(hcd); 944 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
893 int rhport; 945 int id, rhport;
894 int err = 0; 946 int err = 0;
895 947
896 usbip_dbg_vhci_hc("enter vhci_start\n"); 948 usbip_dbg_vhci_hc("enter vhci_start\n");
897 949
898 /* initialize private data of usb_hcd */ 950 /* initialize private data of usb_hcd */
899 951
900 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { 952 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
901 struct vhci_device *vdev = &vhci->vdev[rhport]; 953 struct vhci_device *vdev = &vhci->vdev[rhport];
902 954
903 vhci_device_init(vdev); 955 vhci_device_init(vdev);
@@ -910,11 +962,26 @@ static int vhci_start(struct usb_hcd *hcd)
910 hcd->power_budget = 0; /* no limit */ 962 hcd->power_budget = 0; /* no limit */
911 hcd->uses_new_polling = 1; 963 hcd->uses_new_polling = 1;
912 964
965 id = hcd_name_to_id(hcd_name(hcd));
966 if (id < 0) {
967 pr_err("invalid vhci name %s\n", hcd_name(hcd));
968 return -EINVAL;
969 }
970
913 /* vhci_hcd is now ready to be controlled through sysfs */ 971 /* vhci_hcd is now ready to be controlled through sysfs */
914 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group); 972 if (id == 0) {
915 if (err) { 973 err = vhci_init_attr_group();
916 pr_err("create sysfs files\n"); 974 if (err) {
917 return err; 975 pr_err("init attr group\n");
976 return err;
977 }
978 err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
979 if (err) {
980 pr_err("create sysfs files\n");
981 vhci_finish_attr_group();
982 return err;
983 }
984 pr_info("created sysfs %s\n", hcd_name(hcd));
918 } 985 }
919 986
920 return 0; 987 return 0;
@@ -923,15 +990,19 @@ static int vhci_start(struct usb_hcd *hcd)
923static void vhci_stop(struct usb_hcd *hcd) 990static void vhci_stop(struct usb_hcd *hcd)
924{ 991{
925 struct vhci_hcd *vhci = hcd_to_vhci(hcd); 992 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
926 int rhport = 0; 993 int id, rhport;
927 994
928 usbip_dbg_vhci_hc("stop VHCI controller\n"); 995 usbip_dbg_vhci_hc("stop VHCI controller\n");
929 996
930 /* 1. remove the userland interface of vhci_hcd */ 997 /* 1. remove the userland interface of vhci_hcd */
931 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group); 998 id = hcd_name_to_id(hcd_name(hcd));
999 if (id == 0) {
1000 sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1001 vhci_finish_attr_group();
1002 }
932 1003
933 /* 2. shutdown all the ports of vhci_hcd */ 1004 /* 2. shutdown all the ports of vhci_hcd */
934 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { 1005 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
935 struct vhci_device *vdev = &vhci->vdev[rhport]; 1006 struct vhci_device *vdev = &vhci->vdev[rhport];
936 1007
937 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED); 1008 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
@@ -1025,9 +1096,6 @@ static int vhci_hcd_probe(struct platform_device *pdev)
1025 } 1096 }
1026 hcd->has_tt = 1; 1097 hcd->has_tt = 1;
1027 1098
1028 /* this is private data for vhci_hcd */
1029 the_controller = hcd_to_vhci(hcd);
1030
1031 /* 1099 /*
1032 * Finish generic HCD structure initialization and register. 1100 * Finish generic HCD structure initialization and register.
1033 * Call the driver's reset() and start() routines. 1101 * Call the driver's reset() and start() routines.
@@ -1036,7 +1104,6 @@ static int vhci_hcd_probe(struct platform_device *pdev)
1036 if (ret != 0) { 1104 if (ret != 0) {
1037 pr_err("usb_add_hcd failed %d\n", ret); 1105 pr_err("usb_add_hcd failed %d\n", ret);
1038 usb_put_hcd(hcd); 1106 usb_put_hcd(hcd);
1039 the_controller = NULL;
1040 return ret; 1107 return ret;
1041 } 1108 }
1042 1109
@@ -1059,7 +1126,6 @@ static int vhci_hcd_remove(struct platform_device *pdev)
1059 */ 1126 */
1060 usb_remove_hcd(hcd); 1127 usb_remove_hcd(hcd);
1061 usb_put_hcd(hcd); 1128 usb_put_hcd(hcd);
1062 the_controller = NULL;
1063 1129
1064 return 0; 1130 return 0;
1065} 1131}
@@ -1070,21 +1136,24 @@ static int vhci_hcd_remove(struct platform_device *pdev)
1070static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state) 1136static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1071{ 1137{
1072 struct usb_hcd *hcd; 1138 struct usb_hcd *hcd;
1073 int rhport = 0; 1139 struct vhci_hcd *vhci;
1140 int rhport;
1074 int connected = 0; 1141 int connected = 0;
1075 int ret = 0; 1142 int ret = 0;
1076 unsigned long flags; 1143 unsigned long flags;
1077 1144
1078 hcd = platform_get_drvdata(pdev); 1145 hcd = platform_get_drvdata(pdev);
1146 if (!hcd)
1147 return 0;
1148 vhci = hcd_to_vhci(hcd);
1079 1149
1080 spin_lock_irqsave(&the_controller->lock, flags); 1150 spin_lock_irqsave(&vhci->lock, flags);
1081 1151
1082 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) 1152 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++)
1083 if (the_controller->port_status[rhport] & 1153 if (vhci->port_status[rhport] & USB_PORT_STAT_CONNECTION)
1084 USB_PORT_STAT_CONNECTION)
1085 connected += 1; 1154 connected += 1;
1086 1155
1087 spin_unlock_irqrestore(&the_controller->lock, flags); 1156 spin_unlock_irqrestore(&vhci->lock, flags);
1088 1157
1089 if (connected > 0) { 1158 if (connected > 0) {
1090 dev_info(&pdev->dev, 1159 dev_info(&pdev->dev,
@@ -1106,6 +1175,8 @@ static int vhci_hcd_resume(struct platform_device *pdev)
1106 dev_dbg(&pdev->dev, "%s\n", __func__); 1175 dev_dbg(&pdev->dev, "%s\n", __func__);
1107 1176
1108 hcd = platform_get_drvdata(pdev); 1177 hcd = platform_get_drvdata(pdev);
1178 if (!hcd)
1179 return 0;
1109 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 1180 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1110 usb_hcd_poll_rh_status(hcd); 1181 usb_hcd_poll_rh_status(hcd);
1111 1182
@@ -1129,52 +1200,78 @@ static struct platform_driver vhci_driver = {
1129 }, 1200 },
1130}; 1201};
1131 1202
1132/* 1203static int add_platform_device(int id)
1133 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1134 * We need to add this virtual device as a platform device arbitrarily:
1135 * 1. platform_device_register()
1136 */
1137static void the_pdev_release(struct device *dev)
1138{ 1204{
1205 struct platform_device *pdev;
1206 int dev_nr;
1207
1208 if (id == 0)
1209 dev_nr = -1;
1210 else
1211 dev_nr = id;
1212
1213 pdev = platform_device_register_simple(driver_name, dev_nr, NULL, 0);
1214 if (pdev == NULL)
1215 return -ENODEV;
1216
1217 *(vhci_pdevs + id) = pdev;
1218 return 0;
1139} 1219}
1140 1220
1141static struct platform_device the_pdev = { 1221static void del_platform_devices(void)
1142 /* should be the same name as driver_name */ 1222{
1143 .name = driver_name, 1223 struct platform_device *pdev;
1144 .id = -1, 1224 int i;
1145 .dev = { 1225
1146 .release = the_pdev_release, 1226 for (i = 0; i < vhci_num_controllers; i++) {
1147 }, 1227 pdev = *(vhci_pdevs + i);
1148}; 1228 if (pdev != NULL)
1229 platform_device_unregister(pdev);
1230 *(vhci_pdevs + i) = NULL;
1231 }
1232 sysfs_remove_link(&platform_bus.kobj, driver_name);
1233}
1149 1234
1150static int __init vhci_hcd_init(void) 1235static int __init vhci_hcd_init(void)
1151{ 1236{
1152 int ret; 1237 int i, ret;
1153 1238
1154 if (usb_disabled()) 1239 if (usb_disabled())
1155 return -ENODEV; 1240 return -ENODEV;
1156 1241
1242 if (vhci_num_controllers < 1)
1243 vhci_num_controllers = 1;
1244
1245 vhci_pdevs = kcalloc(vhci_num_controllers, sizeof(void *), GFP_KERNEL);
1246 if (vhci_pdevs == NULL)
1247 return -ENOMEM;
1248
1157 ret = platform_driver_register(&vhci_driver); 1249 ret = platform_driver_register(&vhci_driver);
1158 if (ret) 1250 if (ret)
1159 goto err_driver_register; 1251 goto err_driver_register;
1160 1252
1161 ret = platform_device_register(&the_pdev); 1253 for (i = 0; i < vhci_num_controllers; i++) {
1162 if (ret) 1254 ret = add_platform_device(i);
1163 goto err_platform_device_register; 1255 if (ret)
1256 goto err_platform_device_register;
1257 }
1164 1258
1165 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n"); 1259 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
1166 return ret; 1260 return ret;
1167 1261
1168err_platform_device_register: 1262err_platform_device_register:
1263 del_platform_devices();
1169 platform_driver_unregister(&vhci_driver); 1264 platform_driver_unregister(&vhci_driver);
1170err_driver_register: 1265err_driver_register:
1266 kfree(vhci_pdevs);
1171 return ret; 1267 return ret;
1172} 1268}
1173 1269
1174static void __exit vhci_hcd_exit(void) 1270static void __exit vhci_hcd_exit(void)
1175{ 1271{
1176 platform_device_unregister(&the_pdev); 1272 del_platform_devices();
1177 platform_driver_unregister(&vhci_driver); 1273 platform_driver_unregister(&vhci_driver);
1274 kfree(vhci_pdevs);
1178} 1275}
1179 1276
1180module_init(vhci_hcd_init); 1277module_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)
70static void vhci_recv_ret_submit(struct vhci_device *vdev, 70static void vhci_recv_ret_submit(struct vhci_device *vdev,
71 struct usbip_header *pdu) 71 struct usbip_header *pdu)
72{ 72{
73 struct vhci_hcd *vhci = vdev_to_vhci(vdev);
73 struct usbip_device *ud = &vdev->ud; 74 struct usbip_device *ud = &vdev->ud;
74 struct urb *urb; 75 struct urb *urb;
75 unsigned long flags; 76 unsigned long flags;
@@ -81,7 +82,7 @@ static void vhci_recv_ret_submit(struct vhci_device *vdev,
81 if (!urb) { 82 if (!urb) {
82 pr_err("cannot find a urb of seqnum %u\n", pdu->base.seqnum); 83 pr_err("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
83 pr_info("max seqnum %d\n", 84 pr_info("max seqnum %d\n",
84 atomic_read(&the_controller->seqnum)); 85 atomic_read(&vhci->seqnum));
85 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); 86 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
86 return; 87 return;
87 } 88 }
@@ -105,11 +106,11 @@ static void vhci_recv_ret_submit(struct vhci_device *vdev,
105 106
106 usbip_dbg_vhci_rx("now giveback urb %p\n", urb); 107 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
107 108
108 spin_lock_irqsave(&the_controller->lock, flags); 109 spin_lock_irqsave(&vhci->lock, flags);
109 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); 110 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(vhci), urb);
110 spin_unlock_irqrestore(&the_controller->lock, flags); 111 spin_unlock_irqrestore(&vhci->lock, flags);
111 112
112 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status); 113 usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status);
113 114
114 usbip_dbg_vhci_rx("Leave\n"); 115 usbip_dbg_vhci_rx("Leave\n");
115} 116}
@@ -142,6 +143,7 @@ static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
142static void vhci_recv_ret_unlink(struct vhci_device *vdev, 143static void vhci_recv_ret_unlink(struct vhci_device *vdev,
143 struct usbip_header *pdu) 144 struct usbip_header *pdu)
144{ 145{
146 struct vhci_hcd *vhci = vdev_to_vhci(vdev);
145 struct vhci_unlink *unlink; 147 struct vhci_unlink *unlink;
146 struct urb *urb; 148 struct urb *urb;
147 unsigned long flags; 149 unsigned long flags;
@@ -174,12 +176,11 @@ static void vhci_recv_ret_unlink(struct vhci_device *vdev,
174 urb->status = pdu->u.ret_unlink.status; 176 urb->status = pdu->u.ret_unlink.status;
175 pr_info("urb->status %d\n", urb->status); 177 pr_info("urb->status %d\n", urb->status);
176 178
177 spin_lock_irqsave(&the_controller->lock, flags); 179 spin_lock_irqsave(&vhci->lock, flags);
178 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); 180 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(vhci), urb);
179 spin_unlock_irqrestore(&the_controller->lock, flags); 181 spin_unlock_irqrestore(&vhci->lock, flags);
180 182
181 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, 183 usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status);
182 urb->status);
183 } 184 }
184 185
185 kfree(unlink); 186 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 @@
1/* 1/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi 2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015-2016 Nobuo Iwata
3 * 4 *
4 * This is free software; you can redistribute it and/or modify 5 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
@@ -20,6 +21,8 @@
20#include <linux/kthread.h> 21#include <linux/kthread.h>
21#include <linux/file.h> 22#include <linux/file.h>
22#include <linux/net.h> 23#include <linux/net.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
23 26
24#include "usbip_common.h" 27#include "usbip_common.h"
25#include "vhci.h" 28#include "vhci.h"
@@ -27,106 +30,190 @@
27/* TODO: refine locking ?*/ 30/* TODO: refine locking ?*/
28 31
29/* Sysfs entry to show port status */ 32/* Sysfs entry to show port status */
30static ssize_t status_show(struct device *dev, struct device_attribute *attr, 33static ssize_t status_show_vhci(int pdev_nr, char *out)
31 char *out)
32{ 34{
35 struct platform_device *pdev = *(vhci_pdevs + pdev_nr);
36 struct vhci_hcd *vhci;
33 char *s = out; 37 char *s = out;
34 int i = 0; 38 int i = 0;
35 unsigned long flags; 39 unsigned long flags;
36 40
37 BUG_ON(!the_controller || !out); 41 if (!pdev || !out) {
42 usbip_dbg_vhci_sysfs("show status error\n");
43 return 0;
44 }
45
46 vhci = hcd_to_vhci(platform_get_drvdata(pdev));
38 47
39 spin_lock_irqsave(&the_controller->lock, flags); 48 spin_lock_irqsave(&vhci->lock, flags);
40 49
41 /* 50 /*
42 * output example: 51 * output example:
43 * prt sta spd dev socket local_busid 52 * port sta spd dev socket local_busid
44 * 000 004 000 000 c5a7bb80 1-2.3 53 * 0000 004 000 00000000 c5a7bb80 1-2.3
45 * 001 004 000 000 d8cee980 2-3.4 54 * 0001 004 000 00000000 d8cee980 2-3.4
46 * 55 *
47 * IP address can be retrieved from a socket pointer address by looking 56 * IP address can be retrieved from a socket pointer address by looking
48 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a 57 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
49 * port number and its peer IP address. 58 * port number and its peer IP address.
50 */ 59 */
51 out += sprintf(out, 60 for (i = 0; i < VHCI_HC_PORTS; i++) {
52 "prt sta spd bus dev socket local_busid\n"); 61 struct vhci_device *vdev = &vhci->vdev[i];
53
54 for (i = 0; i < VHCI_NPORTS; i++) {
55 struct vhci_device *vdev = port_to_vdev(i);
56 62
57 spin_lock(&vdev->ud.lock); 63 spin_lock(&vdev->ud.lock);
58 out += sprintf(out, "%03u %03u ", i, vdev->ud.status); 64 out += sprintf(out, "%04u %03u ",
65 (pdev_nr * VHCI_HC_PORTS) + i,
66 vdev->ud.status);
59 67
60 if (vdev->ud.status == VDEV_ST_USED) { 68 if (vdev->ud.status == VDEV_ST_USED) {
61 out += sprintf(out, "%03u %08x ", 69 out += sprintf(out, "%03u %08x ",
62 vdev->speed, vdev->devid); 70 vdev->speed, vdev->devid);
63 out += sprintf(out, "%16p ", vdev->ud.tcp_socket); 71 out += sprintf(out, "%16p %s",
64 out += sprintf(out, "%s", dev_name(&vdev->udev->dev)); 72 vdev->ud.tcp_socket,
73 dev_name(&vdev->udev->dev));
65 74
66 } else { 75 } else {
67 out += sprintf(out, "000 000 000 0000000000000000 0-0"); 76 out += sprintf(out, "000 00000000 ");
77 out += sprintf(out, "0000000000000000 0-0");
68 } 78 }
69 79
70 out += sprintf(out, "\n"); 80 out += sprintf(out, "\n");
71 spin_unlock(&vdev->ud.lock); 81 spin_unlock(&vdev->ud.lock);
72 } 82 }
73 83
74 spin_unlock_irqrestore(&the_controller->lock, flags); 84 spin_unlock_irqrestore(&vhci->lock, flags);
85
86 return out - s;
87}
88
89static ssize_t status_show_not_ready(int pdev_nr, char *out)
90{
91 char *s = out;
92 int i = 0;
93
94 for (i = 0; i < VHCI_HC_PORTS; i++) {
95 out += sprintf(out, "%04u %03u ",
96 (pdev_nr * VHCI_HC_PORTS) + i,
97 VDEV_ST_NOTASSIGNED);
98 out += sprintf(out, "000 00000000 0000000000000000 0-0");
99 out += sprintf(out, "\n");
100 }
101 return out - s;
102}
103
104static int status_name_to_id(const char *name)
105{
106 char *c;
107 long val;
108 int ret;
109
110 c = strchr(name, '.');
111 if (c == NULL)
112 return 0;
75 113
114 ret = kstrtol(c+1, 10, &val);
115 if (ret < 0)
116 return ret;
117
118 return val;
119}
120
121static ssize_t status_show(struct device *dev,
122 struct device_attribute *attr, char *out)
123{
124 char *s = out;
125 int pdev_nr;
126
127 out += sprintf(out,
128 "port sta spd dev socket local_busid\n");
129
130 pdev_nr = status_name_to_id(attr->attr.name);
131 if (pdev_nr < 0)
132 out += status_show_not_ready(pdev_nr, out);
133 else
134 out += status_show_vhci(pdev_nr, out);
135
136 return out - s;
137}
138
139static ssize_t nports_show(struct device *dev, struct device_attribute *attr,
140 char *out)
141{
142 char *s = out;
143
144 out += sprintf(out, "%d\n", VHCI_HC_PORTS * vhci_num_controllers);
76 return out - s; 145 return out - s;
77} 146}
78static DEVICE_ATTR_RO(status); 147static DEVICE_ATTR_RO(nports);
79 148
80/* Sysfs entry to shutdown a virtual connection */ 149/* Sysfs entry to shutdown a virtual connection */
81static int vhci_port_disconnect(__u32 rhport) 150static int vhci_port_disconnect(struct vhci_hcd *vhci, __u32 rhport)
82{ 151{
83 struct vhci_device *vdev; 152 struct vhci_device *vdev = &vhci->vdev[rhport];
84 unsigned long flags; 153 unsigned long flags;
85 154
86 usbip_dbg_vhci_sysfs("enter\n"); 155 usbip_dbg_vhci_sysfs("enter\n");
87 156
88 /* lock */ 157 /* lock */
89 spin_lock_irqsave(&the_controller->lock, flags); 158 spin_lock_irqsave(&vhci->lock, flags);
90
91 vdev = port_to_vdev(rhport);
92
93 spin_lock(&vdev->ud.lock); 159 spin_lock(&vdev->ud.lock);
160
94 if (vdev->ud.status == VDEV_ST_NULL) { 161 if (vdev->ud.status == VDEV_ST_NULL) {
95 pr_err("not connected %d\n", vdev->ud.status); 162 pr_err("not connected %d\n", vdev->ud.status);
96 163
97 /* unlock */ 164 /* unlock */
98 spin_unlock(&vdev->ud.lock); 165 spin_unlock(&vdev->ud.lock);
99 spin_unlock_irqrestore(&the_controller->lock, flags); 166 spin_unlock_irqrestore(&vhci->lock, flags);
100 167
101 return -EINVAL; 168 return -EINVAL;
102 } 169 }
103 170
104 /* unlock */ 171 /* unlock */
105 spin_unlock(&vdev->ud.lock); 172 spin_unlock(&vdev->ud.lock);
106 spin_unlock_irqrestore(&the_controller->lock, flags); 173 spin_unlock_irqrestore(&vhci->lock, flags);
107 174
108 usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); 175 usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
109 176
110 return 0; 177 return 0;
111} 178}
112 179
180static int valid_port(__u32 pdev_nr, __u32 rhport)
181{
182 if (pdev_nr >= vhci_num_controllers) {
183 pr_err("pdev %u\n", pdev_nr);
184 return 0;
185 }
186 if (rhport >= VHCI_HC_PORTS) {
187 pr_err("rhport %u\n", rhport);
188 return 0;
189 }
190 return 1;
191}
192
113static ssize_t store_detach(struct device *dev, struct device_attribute *attr, 193static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
114 const char *buf, size_t count) 194 const char *buf, size_t count)
115{ 195{
116 int err; 196 __u32 port = 0, pdev_nr = 0, rhport = 0;
117 __u32 rhport = 0; 197 struct usb_hcd *hcd;
198 int ret;
118 199
119 if (sscanf(buf, "%u", &rhport) != 1) 200 if (kstrtoint(buf, 10, &port) < 0)
120 return -EINVAL; 201 return -EINVAL;
121 202
122 /* check rhport */ 203 pdev_nr = port_to_pdev_nr(port);
123 if (rhport >= VHCI_NPORTS) { 204 rhport = port_to_rhport(port);
124 dev_err(dev, "invalid port %u\n", rhport); 205
206 if (!valid_port(pdev_nr, rhport))
125 return -EINVAL; 207 return -EINVAL;
208
209 hcd = platform_get_drvdata(*(vhci_pdevs + pdev_nr));
210 if (hcd == NULL) {
211 dev_err(dev, "port is not ready %u\n", port);
212 return -EAGAIN;
126 } 213 }
127 214
128 err = vhci_port_disconnect(rhport); 215 ret = vhci_port_disconnect(hcd_to_vhci(hcd), rhport);
129 if (err < 0) 216 if (ret < 0)
130 return -EINVAL; 217 return -EINVAL;
131 218
132 usbip_dbg_vhci_sysfs("Leave\n"); 219 usbip_dbg_vhci_sysfs("Leave\n");
@@ -135,16 +222,12 @@ static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
135} 222}
136static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach); 223static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
137 224
138/* Sysfs entry to establish a virtual connection */ 225static int valid_args(__u32 pdev_nr, __u32 rhport, enum usb_device_speed speed)
139static int valid_args(__u32 rhport, enum usb_device_speed speed)
140{ 226{
141 /* check rhport */ 227 if (!valid_port(pdev_nr, rhport)) {
142 if (rhport >= VHCI_NPORTS) { 228 return 0;
143 pr_err("port %u\n", rhport);
144 return -EINVAL;
145 } 229 }
146 230
147 /* check speed */
148 switch (speed) { 231 switch (speed) {
149 case USB_SPEED_LOW: 232 case USB_SPEED_LOW:
150 case USB_SPEED_FULL: 233 case USB_SPEED_FULL:
@@ -154,12 +237,13 @@ static int valid_args(__u32 rhport, enum usb_device_speed speed)
154 default: 237 default:
155 pr_err("Failed attach request for unsupported USB speed: %s\n", 238 pr_err("Failed attach request for unsupported USB speed: %s\n",
156 usb_speed_string(speed)); 239 usb_speed_string(speed));
157 return -EINVAL; 240 return 0;
158 } 241 }
159 242
160 return 0; 243 return 1;
161} 244}
162 245
246/* Sysfs entry to establish a virtual connection */
163/* 247/*
164 * To start a new USB/IP attachment, a userland program needs to setup a TCP 248 * To start a new USB/IP attachment, a userland program needs to setup a TCP
165 * connection and then write its socket descriptor with remote device 249 * 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)
174static ssize_t store_attach(struct device *dev, struct device_attribute *attr, 258static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t count) 259 const char *buf, size_t count)
176{ 260{
177 struct vhci_device *vdev;
178 struct socket *socket; 261 struct socket *socket;
179 int sockfd = 0; 262 int sockfd = 0;
180 __u32 rhport = 0, devid = 0, speed = 0; 263 __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0;
264 struct usb_hcd *hcd;
265 struct vhci_hcd *vhci;
266 struct vhci_device *vdev;
181 int err; 267 int err;
182 unsigned long flags; 268 unsigned long flags;
183 269
@@ -187,16 +273,28 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
187 * @devid: unique device identifier in a remote host 273 * @devid: unique device identifier in a remote host
188 * @speed: usb device speed in a remote host 274 * @speed: usb device speed in a remote host
189 */ 275 */
190 if (sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed) != 4) 276 if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4)
191 return -EINVAL; 277 return -EINVAL;
278 pdev_nr = port_to_pdev_nr(port);
279 rhport = port_to_rhport(port);
192 280
193 usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n", 281 usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
194 rhport, sockfd, devid, speed); 282 port, pdev_nr, rhport);
283 usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
284 sockfd, devid, speed);
195 285
196 /* check received parameters */ 286 /* check received parameters */
197 if (valid_args(rhport, speed) < 0) 287 if (!valid_args(pdev_nr, rhport, speed))
198 return -EINVAL; 288 return -EINVAL;
199 289
290 hcd = platform_get_drvdata(*(vhci_pdevs + pdev_nr));
291 if (hcd == NULL) {
292 dev_err(dev, "port %d is not ready\n", port);
293 return -EAGAIN;
294 }
295 vhci = hcd_to_vhci(hcd);
296 vdev = &vhci->vdev[rhport];
297
200 /* Extract socket from fd. */ 298 /* Extract socket from fd. */
201 socket = sockfd_lookup(sockfd, &err); 299 socket = sockfd_lookup(sockfd, &err);
202 if (!socket) 300 if (!socket)
@@ -205,14 +303,13 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
205 /* now need lock until setting vdev status as used */ 303 /* now need lock until setting vdev status as used */
206 304
207 /* begin a lock */ 305 /* begin a lock */
208 spin_lock_irqsave(&the_controller->lock, flags); 306 spin_lock_irqsave(&vhci->lock, flags);
209 vdev = port_to_vdev(rhport);
210 spin_lock(&vdev->ud.lock); 307 spin_lock(&vdev->ud.lock);
211 308
212 if (vdev->ud.status != VDEV_ST_NULL) { 309 if (vdev->ud.status != VDEV_ST_NULL) {
213 /* end of the lock */ 310 /* end of the lock */
214 spin_unlock(&vdev->ud.lock); 311 spin_unlock(&vdev->ud.lock);
215 spin_unlock_irqrestore(&the_controller->lock, flags); 312 spin_unlock_irqrestore(&vhci->lock, flags);
216 313
217 sockfd_put(socket); 314 sockfd_put(socket);
218 315
@@ -220,9 +317,10 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
220 return -EINVAL; 317 return -EINVAL;
221 } 318 }
222 319
223 dev_info(dev, 320 dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
224 "rhport(%u) sockfd(%d) devid(%u) speed(%u) speed_str(%s)\n", 321 pdev_nr, rhport, sockfd);
225 rhport, sockfd, devid, speed, usb_speed_string(speed)); 322 dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n",
323 devid, speed, usb_speed_string(speed));
226 324
227 vdev->devid = devid; 325 vdev->devid = devid;
228 vdev->speed = speed; 326 vdev->speed = speed;
@@ -230,26 +328,92 @@ static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
230 vdev->ud.status = VDEV_ST_NOTASSIGNED; 328 vdev->ud.status = VDEV_ST_NOTASSIGNED;
231 329
232 spin_unlock(&vdev->ud.lock); 330 spin_unlock(&vdev->ud.lock);
233 spin_unlock_irqrestore(&the_controller->lock, flags); 331 spin_unlock_irqrestore(&vhci->lock, flags);
234 /* end the lock */ 332 /* end the lock */
235 333
236 vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx"); 334 vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
237 vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx"); 335 vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
238 336
239 rh_port_connect(rhport, speed); 337 rh_port_connect(vdev, speed);
240 338
241 return count; 339 return count;
242} 340}
243static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach); 341static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
244 342
245static struct attribute *dev_attrs[] = { 343#define MAX_STATUS_NAME 16
246 &dev_attr_status.attr, 344
247 &dev_attr_detach.attr, 345struct status_attr {
248 &dev_attr_attach.attr, 346 struct device_attribute attr;
249 &dev_attr_usbip_debug.attr, 347 char name[MAX_STATUS_NAME+1];
250 NULL,
251}; 348};
252 349
253const struct attribute_group dev_attr_group = { 350static struct status_attr *status_attrs;
254 .attrs = dev_attrs, 351
352static void set_status_attr(int id)
353{
354 struct status_attr *status;
355
356 status = status_attrs + id;
357 if (id == 0)
358 strcpy(status->name, "status");
359 else
360 snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id);
361 status->attr.attr.name = status->name;
362 status->attr.attr.mode = S_IRUGO;
363 status->attr.show = status_show;
364}
365
366static int init_status_attrs(void)
367{
368 int id;
369
370 status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr),
371 GFP_KERNEL);
372 if (status_attrs == NULL)
373 return -ENOMEM;
374
375 for (id = 0; id < vhci_num_controllers; id++)
376 set_status_attr(id);
377
378 return 0;
379}
380
381static void finish_status_attrs(void)
382{
383 kfree(status_attrs);
384}
385
386struct attribute_group vhci_attr_group = {
387 .attrs = NULL,
255}; 388};
389
390int vhci_init_attr_group(void)
391{
392 struct attribute **attrs;
393 int ret, i;
394
395 attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *),
396 GFP_KERNEL);
397 if (attrs == NULL)
398 return -ENOMEM;
399
400 ret = init_status_attrs();
401 if (ret) {
402 kfree(attrs);
403 return ret;
404 }
405 *attrs = &dev_attr_nports.attr;
406 *(attrs + 1) = &dev_attr_detach.attr;
407 *(attrs + 2) = &dev_attr_attach.attr;
408 *(attrs + 3) = &dev_attr_usbip_debug.attr;
409 for (i = 0; i < vhci_num_controllers; i++)
410 *(attrs + i + 4) = &((status_attrs + i)->attr.attr);
411 vhci_attr_group.attrs = attrs;
412 return 0;
413}
414
415void vhci_finish_attr_group(void)
416{
417 finish_status_attrs();
418 kfree(vhci_attr_group.attrs);
419}