]>
Commit | Line | Data |
---|---|---|
04679b34 TH |
1 | /* |
2 | * Copyright (C) 2003-2008 Takahiro Hirofuchi | |
0775a9cb | 3 | * Copyright (C) 2015-2016 Nobuo Iwata |
04679b34 TH |
4 | * |
5 | * This is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
18 | * USA. | |
19 | */ | |
20 | ||
7aaacb43 | 21 | #include <linux/init.h> |
3d0a2a22 | 22 | #include <linux/file.h> |
7aaacb43 | 23 | #include <linux/kernel.h> |
9720b4bc | 24 | #include <linux/kthread.h> |
7aaacb43 | 25 | #include <linux/module.h> |
26 | #include <linux/platform_device.h> | |
27 | #include <linux/slab.h> | |
04679b34 TH |
28 | |
29 | #include "usbip_common.h" | |
30 | #include "vhci.h" | |
31 | ||
04679b34 | 32 | #define DRIVER_AUTHOR "Takahiro Hirofuchi" |
64e62426 | 33 | #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver" |
04679b34 TH |
34 | |
35 | /* | |
36 | * TODO | |
37 | * - update root hub emulation | |
38 | * - move the emulation code to userland ? | |
39 | * porting to other operating systems | |
40 | * minimize kernel code | |
41 | * - add suspend/resume code | |
42 | * - clean up everything | |
43 | */ | |
44 | ||
04679b34 TH |
45 | /* See usb gadget dummy hcd */ |
46 | ||
04679b34 TH |
47 | static int vhci_hub_status(struct usb_hcd *hcd, char *buff); |
48 | static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, | |
071d1d47 | 49 | u16 wIndex, char *buff, u16 wLength); |
04679b34 | 50 | static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, |
071d1d47 | 51 | gfp_t mem_flags); |
04679b34 TH |
52 | static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status); |
53 | static int vhci_start(struct usb_hcd *vhci_hcd); | |
54 | static void vhci_stop(struct usb_hcd *hcd); | |
55 | static int vhci_get_frame_number(struct usb_hcd *hcd); | |
56 | ||
57 | static const char driver_name[] = "vhci_hcd"; | |
f5e08ca7 | 58 | static const char driver_desc[] = "USB/IP Virtual Host Controller"; |
04679b34 | 59 | |
0775a9cb | 60 | int vhci_num_controllers = VHCI_NR_HCS; |
89a73d28 | 61 | struct vhci *vhcis; |
04679b34 | 62 | |
071d1d47 | 63 | static const char * const bit_desc[] = { |
04679b34 TH |
64 | "CONNECTION", /*0*/ |
65 | "ENABLE", /*1*/ | |
66 | "SUSPEND", /*2*/ | |
67 | "OVER_CURRENT", /*3*/ | |
68 | "RESET", /*4*/ | |
a5c7f019 | 69 | "L1", /*5*/ |
071d1d47 | 70 | "R6", /*6*/ |
71 | "R7", /*7*/ | |
04679b34 TH |
72 | "POWER", /*8*/ |
73 | "LOWSPEED", /*9*/ | |
74 | "HIGHSPEED", /*10*/ | |
75 | "PORT_TEST", /*11*/ | |
76 | "INDICATOR", /*12*/ | |
071d1d47 | 77 | "R13", /*13*/ |
78 | "R14", /*14*/ | |
79 | "R15", /*15*/ | |
04679b34 TH |
80 | "C_CONNECTION", /*16*/ |
81 | "C_ENABLE", /*17*/ | |
82 | "C_SUSPEND", /*18*/ | |
83 | "C_OVER_CURRENT", /*19*/ | |
84 | "C_RESET", /*20*/ | |
a5c7f019 | 85 | "C_L1", /*21*/ |
071d1d47 | 86 | "R22", /*22*/ |
87 | "R23", /*23*/ | |
88 | "R24", /*24*/ | |
89 | "R25", /*25*/ | |
90 | "R26", /*26*/ | |
91 | "R27", /*27*/ | |
92 | "R28", /*28*/ | |
93 | "R29", /*29*/ | |
94 | "R30", /*30*/ | |
95 | "R31", /*31*/ | |
04679b34 TH |
96 | }; |
97 | ||
a5c7f019 YD |
98 | static const char * const bit_desc_ss[] = { |
99 | "CONNECTION", /*0*/ | |
100 | "ENABLE", /*1*/ | |
101 | "SUSPEND", /*2*/ | |
102 | "OVER_CURRENT", /*3*/ | |
103 | "RESET", /*4*/ | |
104 | "L1", /*5*/ | |
105 | "R6", /*6*/ | |
106 | "R7", /*7*/ | |
107 | "R8", /*8*/ | |
108 | "POWER", /*9*/ | |
109 | "HIGHSPEED", /*10*/ | |
110 | "PORT_TEST", /*11*/ | |
111 | "INDICATOR", /*12*/ | |
112 | "R13", /*13*/ | |
113 | "R14", /*14*/ | |
114 | "R15", /*15*/ | |
115 | "C_CONNECTION", /*16*/ | |
116 | "C_ENABLE", /*17*/ | |
117 | "C_SUSPEND", /*18*/ | |
118 | "C_OVER_CURRENT", /*19*/ | |
119 | "C_RESET", /*20*/ | |
120 | "C_BH_RESET", /*21*/ | |
121 | "C_LINK_STATE", /*22*/ | |
122 | "C_CONFIG_ERROR", /*23*/ | |
123 | "R24", /*24*/ | |
124 | "R25", /*25*/ | |
125 | "R26", /*26*/ | |
126 | "R27", /*27*/ | |
127 | "R28", /*28*/ | |
128 | "R29", /*29*/ | |
129 | "R30", /*30*/ | |
130 | "R31", /*31*/ | |
131 | }; | |
132 | ||
133 | static void dump_port_status_diff(u32 prev_status, u32 new_status, bool usb3) | |
04679b34 TH |
134 | { |
135 | int i = 0; | |
4a3ca2be | 136 | u32 bit = 1; |
a5c7f019 YD |
137 | const char * const *desc = bit_desc; |
138 | ||
139 | if (usb3) | |
140 | desc = bit_desc_ss; | |
4a3ca2be MN |
141 | |
142 | pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status); | |
143 | while (bit) { | |
144 | u32 prev = prev_status & bit; | |
145 | u32 new = new_status & bit; | |
146 | char change; | |
147 | ||
148 | if (!prev && new) | |
149 | change = '+'; | |
150 | else if (prev && !new) | |
151 | change = '-'; | |
152 | else | |
153 | change = ' '; | |
154 | ||
a5c7f019 YD |
155 | if (prev || new) { |
156 | pr_debug(" %c%s\n", change, desc[i]); | |
157 | ||
158 | if (bit == 1) /* USB_PORT_STAT_CONNECTION */ | |
159 | pr_debug(" %c%s\n", change, "USB_PORT_STAT_SPEED_5GBPS"); | |
160 | } | |
4a3ca2be MN |
161 | bit <<= 1; |
162 | i++; | |
04679b34 | 163 | } |
1a4b6f66 | 164 | pr_debug("\n"); |
04679b34 TH |
165 | } |
166 | ||
0775a9cb | 167 | void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed) |
04679b34 | 168 | { |
03cd00d5 YD |
169 | struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev); |
170 | struct vhci *vhci = vhci_hcd->vhci; | |
0775a9cb NI |
171 | int rhport = vdev->rhport; |
172 | u32 status; | |
21619792 AG |
173 | unsigned long flags; |
174 | ||
b8868e45 | 175 | usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport); |
04679b34 | 176 | |
0775a9cb NI |
177 | spin_lock_irqsave(&vhci->lock, flags); |
178 | ||
03cd00d5 | 179 | status = vhci_hcd->port_status[rhport]; |
04679b34 | 180 | |
0775a9cb | 181 | status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION); |
04679b34 TH |
182 | |
183 | switch (speed) { | |
184 | case USB_SPEED_HIGH: | |
0775a9cb | 185 | status |= USB_PORT_STAT_HIGH_SPEED; |
04679b34 TH |
186 | break; |
187 | case USB_SPEED_LOW: | |
0775a9cb | 188 | status |= USB_PORT_STAT_LOW_SPEED; |
04679b34 TH |
189 | break; |
190 | default: | |
191 | break; | |
192 | } | |
193 | ||
03cd00d5 | 194 | vhci_hcd->port_status[rhport] = status; |
0775a9cb NI |
195 | |
196 | spin_unlock_irqrestore(&vhci->lock, flags); | |
04679b34 | 197 | |
03cd00d5 | 198 | usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd)); |
04679b34 TH |
199 | } |
200 | ||
0775a9cb | 201 | static void rh_port_disconnect(struct vhci_device *vdev) |
04679b34 | 202 | { |
03cd00d5 YD |
203 | struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev); |
204 | struct vhci *vhci = vhci_hcd->vhci; | |
0775a9cb NI |
205 | int rhport = vdev->rhport; |
206 | u32 status; | |
21619792 AG |
207 | unsigned long flags; |
208 | ||
b8868e45 | 209 | usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport); |
04679b34 | 210 | |
0775a9cb NI |
211 | spin_lock_irqsave(&vhci->lock, flags); |
212 | ||
03cd00d5 | 213 | status = vhci_hcd->port_status[rhport]; |
0775a9cb NI |
214 | |
215 | status &= ~USB_PORT_STAT_CONNECTION; | |
216 | status |= (1 << USB_PORT_FEAT_C_CONNECTION); | |
c7f00899 | 217 | |
03cd00d5 | 218 | vhci_hcd->port_status[rhport] = status; |
04679b34 | 219 | |
0775a9cb | 220 | spin_unlock_irqrestore(&vhci->lock, flags); |
03cd00d5 | 221 | usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd)); |
04679b34 TH |
222 | } |
223 | ||
071d1d47 | 224 | #define PORT_C_MASK \ |
225 | ((USB_PORT_STAT_C_CONNECTION \ | |
226 | | USB_PORT_STAT_C_ENABLE \ | |
227 | | USB_PORT_STAT_C_SUSPEND \ | |
228 | | USB_PORT_STAT_C_OVERCURRENT \ | |
04679b34 TH |
229 | | USB_PORT_STAT_C_RESET) << 16) |
230 | ||
231 | /* | |
7489301a BW |
232 | * Returns 0 if the status hasn't changed, or the number of bytes in buf. |
233 | * Ports are 0-indexed from the HCD point of view, | |
234 | * and 1-indexed from the USB core pointer of view. | |
04679b34 TH |
235 | * |
236 | * @buf: a bitmap to show which port status has been changed. | |
7489301a | 237 | * bit 0: reserved |
04679b34 TH |
238 | * bit 1: the status of port 0 has been changed. |
239 | * bit 2: the status of port 1 has been changed. | |
240 | * ... | |
04679b34 TH |
241 | */ |
242 | static int vhci_hub_status(struct usb_hcd *hcd, char *buf) | |
243 | { | |
03cd00d5 YD |
244 | struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd); |
245 | struct vhci *vhci = vhci_hcd->vhci; | |
246 | int retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8); | |
04679b34 TH |
247 | int rhport; |
248 | int changed = 0; | |
21619792 | 249 | unsigned long flags; |
04679b34 | 250 | |
7489301a | 251 | memset(buf, 0, retval); |
04679b34 | 252 | |
21619792 | 253 | spin_lock_irqsave(&vhci->lock, flags); |
541c7d43 | 254 | if (!HCD_HW_ACCESSIBLE(hcd)) { |
7489301a | 255 | usbip_dbg_vhci_rh("hw accessible flag not on?\n"); |
04679b34 TH |
256 | goto done; |
257 | } | |
258 | ||
259 | /* check pseudo status register for each port */ | |
0775a9cb | 260 | for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) { |
03cd00d5 | 261 | if ((vhci_hcd->port_status[rhport] & PORT_C_MASK)) { |
04679b34 | 262 | /* The status of a port has been changed, */ |
7489301a | 263 | usbip_dbg_vhci_rh("port %d status changed\n", rhport); |
04679b34 | 264 | |
7489301a | 265 | buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8; |
04679b34 TH |
266 | changed = 1; |
267 | } | |
268 | } | |
269 | ||
107f04bb | 270 | if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1)) |
04679b34 TH |
271 | usb_hcd_resume_root_hub(hcd); |
272 | ||
04679b34 | 273 | done: |
21619792 | 274 | spin_unlock_irqrestore(&vhci->lock, flags); |
7489301a | 275 | return changed ? retval : 0; |
04679b34 TH |
276 | } |
277 | ||
1c9de5bf YD |
278 | /* usb 3.0 root hub device descriptor */ |
279 | static struct { | |
280 | struct usb_bos_descriptor bos; | |
281 | struct usb_ss_cap_descriptor ss_cap; | |
282 | } __packed usb3_bos_desc = { | |
283 | ||
284 | .bos = { | |
285 | .bLength = USB_DT_BOS_SIZE, | |
286 | .bDescriptorType = USB_DT_BOS, | |
287 | .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)), | |
288 | .bNumDeviceCaps = 1, | |
289 | }, | |
290 | .ss_cap = { | |
291 | .bLength = USB_DT_USB_SS_CAP_SIZE, | |
292 | .bDescriptorType = USB_DT_DEVICE_CAPABILITY, | |
293 | .bDevCapabilityType = USB_SS_CAP_TYPE, | |
294 | .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION), | |
295 | .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION), | |
296 | }, | |
297 | }; | |
298 | ||
299 | static inline void | |
300 | ss_hub_descriptor(struct usb_hub_descriptor *desc) | |
301 | { | |
302 | memset(desc, 0, sizeof *desc); | |
303 | desc->bDescriptorType = USB_DT_SS_HUB; | |
304 | desc->bDescLength = 12; | |
305 | desc->wHubCharacteristics = cpu_to_le16( | |
306 | HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM); | |
307 | desc->bNbrPorts = VHCI_HC_PORTS; | |
308 | desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/ | |
309 | desc->u.ss.DeviceRemovable = 0xffff; | |
310 | } | |
311 | ||
04679b34 TH |
312 | static inline void hub_descriptor(struct usb_hub_descriptor *desc) |
313 | { | |
ec963b41 JH |
314 | int width; |
315 | ||
04679b34 | 316 | memset(desc, 0, sizeof(*desc)); |
8f413d80 | 317 | desc->bDescriptorType = USB_DT_HUB; |
0c596336 | 318 | desc->wHubCharacteristics = cpu_to_le16( |
1cc9af87 | 319 | HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM); |
ec963b41 | 320 | |
0775a9cb | 321 | desc->bNbrPorts = VHCI_HC_PORTS; |
ec963b41 JH |
322 | BUILD_BUG_ON(VHCI_HC_PORTS > USB_MAXCHILDREN); |
323 | width = desc->bNbrPorts / 8 + 1; | |
324 | desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * width; | |
325 | memset(&desc->u.hs.DeviceRemovable[0], 0, width); | |
326 | memset(&desc->u.hs.DeviceRemovable[width], 0xff, width); | |
04679b34 TH |
327 | } |
328 | ||
329 | static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, | |
330 | u16 wIndex, char *buf, u16 wLength) | |
331 | { | |
03cd00d5 YD |
332 | struct vhci_hcd *vhci_hcd; |
333 | struct vhci *vhci; | |
04679b34 | 334 | int retval = 0; |
04679b34 | 335 | int rhport; |
21619792 | 336 | unsigned long flags; |
04679b34 | 337 | |
0775a9cb | 338 | u32 prev_port_status[VHCI_HC_PORTS]; |
04679b34 | 339 | |
541c7d43 | 340 | if (!HCD_HW_ACCESSIBLE(hcd)) |
04679b34 TH |
341 | return -ETIMEDOUT; |
342 | ||
343 | /* | |
344 | * NOTE: | |
1c9de5bf | 345 | * wIndex (bits 0-7) shows the port number and begins from 1? |
04679b34 | 346 | */ |
1c9de5bf | 347 | wIndex = ((__u8)(wIndex & 0x00ff)); |
b8868e45 | 348 | usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue, |
071d1d47 | 349 | wIndex); |
1c9de5bf | 350 | |
0775a9cb | 351 | if (wIndex > VHCI_HC_PORTS) |
1a4b6f66 | 352 | pr_err("invalid port number %d\n", wIndex); |
1c9de5bf | 353 | rhport = wIndex - 1; |
04679b34 | 354 | |
03cd00d5 YD |
355 | vhci_hcd = hcd_to_vhci_hcd(hcd); |
356 | vhci = vhci_hcd->vhci; | |
04679b34 | 357 | |
03cd00d5 | 358 | spin_lock_irqsave(&vhci->lock, flags); |
04679b34 TH |
359 | |
360 | /* store old status and compare now and old later */ | |
b8868e45 | 361 | if (usbip_dbg_flag_vhci_rh) { |
03cd00d5 | 362 | memcpy(prev_port_status, vhci_hcd->port_status, |
6f480bf9 | 363 | sizeof(prev_port_status)); |
04679b34 TH |
364 | } |
365 | ||
366 | switch (typeReq) { | |
367 | case ClearHubFeature: | |
b8868e45 | 368 | usbip_dbg_vhci_rh(" ClearHubFeature\n"); |
04679b34 TH |
369 | break; |
370 | case ClearPortFeature: | |
371 | switch (wValue) { | |
372 | case USB_PORT_FEAT_SUSPEND: | |
1c9de5bf YD |
373 | if (hcd->speed == HCD_USB3) { |
374 | pr_err(" ClearPortFeature: USB_PORT_FEAT_SUSPEND req not " | |
375 | "supported for USB 3.0 roothub\n"); | |
376 | goto error; | |
377 | } | |
378 | usbip_dbg_vhci_rh( | |
379 | " ClearPortFeature: USB_PORT_FEAT_SUSPEND\n"); | |
03cd00d5 | 380 | if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_SUSPEND) { |
04679b34 | 381 | /* 20msec signaling */ |
03cd00d5 | 382 | vhci_hcd->resuming = 1; |
1c9de5bf | 383 | vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(20); |
04679b34 TH |
384 | } |
385 | break; | |
386 | case USB_PORT_FEAT_POWER: | |
7923a655 CC |
387 | usbip_dbg_vhci_rh( |
388 | " ClearPortFeature: USB_PORT_FEAT_POWER\n"); | |
1c9de5bf YD |
389 | if (hcd->speed == HCD_USB3) |
390 | vhci_hcd->port_status[rhport] &= ~USB_SS_PORT_STAT_POWER; | |
391 | else | |
392 | vhci_hcd->port_status[rhport] &= ~USB_PORT_STAT_POWER; | |
7c92e5fb | 393 | break; |
04679b34 | 394 | default: |
b8868e45 | 395 | usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n", |
071d1d47 | 396 | wValue); |
03cd00d5 | 397 | vhci_hcd->port_status[rhport] &= ~(1 << wValue); |
49aecefc | 398 | break; |
04679b34 TH |
399 | } |
400 | break; | |
401 | case GetHubDescriptor: | |
b8868e45 | 402 | usbip_dbg_vhci_rh(" GetHubDescriptor\n"); |
1c9de5bf YD |
403 | if (hcd->speed == HCD_USB3 && |
404 | (wLength < USB_DT_SS_HUB_SIZE || | |
405 | wValue != (USB_DT_SS_HUB << 8))) { | |
406 | pr_err("Wrong hub descriptor type for USB 3.0 roothub.\n"); | |
407 | goto error; | |
408 | } | |
409 | if (hcd->speed == HCD_USB3) | |
410 | ss_hub_descriptor((struct usb_hub_descriptor *) buf); | |
411 | else | |
412 | hub_descriptor((struct usb_hub_descriptor *) buf); | |
413 | break; | |
414 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: | |
415 | if (hcd->speed != HCD_USB3) | |
416 | goto error; | |
417 | ||
418 | if ((wValue >> 8) != USB_DT_BOS) | |
419 | goto error; | |
420 | ||
421 | memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc)); | |
422 | retval = sizeof(usb3_bos_desc); | |
04679b34 TH |
423 | break; |
424 | case GetHubStatus: | |
b8868e45 | 425 | usbip_dbg_vhci_rh(" GetHubStatus\n"); |
a06a24d2 | 426 | *(__le32 *) buf = cpu_to_le32(0); |
04679b34 TH |
427 | break; |
428 | case GetPortStatus: | |
b8868e45 | 429 | usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex); |
1c9de5bf | 430 | if (wIndex < 1) { |
1a4b6f66 | 431 | pr_err("invalid port number %d\n", wIndex); |
04679b34 TH |
432 | retval = -EPIPE; |
433 | } | |
434 | ||
c7f00899 | 435 | /* we do not care about resume. */ |
04679b34 TH |
436 | |
437 | /* whoever resets or resumes must GetPortStatus to | |
438 | * complete it!! | |
c7f00899 | 439 | */ |
03cd00d5 | 440 | if (vhci_hcd->resuming && time_after(jiffies, vhci_hcd->re_timeout)) { |
1c9de5bf YD |
441 | vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_SUSPEND); |
442 | vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_SUSPEND); | |
03cd00d5 YD |
443 | vhci_hcd->resuming = 0; |
444 | vhci_hcd->re_timeout = 0; | |
04679b34 TH |
445 | } |
446 | ||
03cd00d5 YD |
447 | if ((vhci_hcd->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) != |
448 | 0 && time_after(jiffies, vhci_hcd->re_timeout)) { | |
1c9de5bf YD |
449 | vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_RESET); |
450 | vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_RESET); | |
03cd00d5 | 451 | vhci_hcd->re_timeout = 0; |
04679b34 | 452 | |
03cd00d5 | 453 | if (vhci_hcd->vdev[rhport].ud.status == |
071d1d47 | 454 | VDEV_ST_NOTASSIGNED) { |
7923a655 CC |
455 | usbip_dbg_vhci_rh( |
456 | " enable rhport %d (status %u)\n", | |
457 | rhport, | |
03cd00d5 YD |
458 | vhci_hcd->vdev[rhport].ud.status); |
459 | vhci_hcd->port_status[rhport] |= | |
071d1d47 | 460 | USB_PORT_STAT_ENABLE; |
04679b34 | 461 | } |
1c9de5bf YD |
462 | |
463 | if (hcd->speed < HCD_USB3) { | |
464 | switch (vhci_hcd->vdev[rhport].speed) { | |
465 | case USB_SPEED_HIGH: | |
466 | vhci_hcd->port_status[rhport] |= | |
467 | USB_PORT_STAT_HIGH_SPEED; | |
468 | break; | |
469 | case USB_SPEED_LOW: | |
470 | vhci_hcd->port_status[rhport] |= | |
471 | USB_PORT_STAT_LOW_SPEED; | |
472 | break; | |
473 | default: | |
474 | pr_err("vhci_device speed not set\n"); | |
475 | break; | |
476 | } | |
477 | } | |
04679b34 | 478 | } |
03cd00d5 | 479 | ((__le16 *) buf)[0] = cpu_to_le16(vhci_hcd->port_status[rhport]); |
7923a655 | 480 | ((__le16 *) buf)[1] = |
03cd00d5 | 481 | cpu_to_le16(vhci_hcd->port_status[rhport] >> 16); |
04679b34 | 482 | |
b8868e45 | 483 | usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0], |
071d1d47 | 484 | ((u16 *)buf)[1]); |
04679b34 TH |
485 | break; |
486 | case SetHubFeature: | |
b8868e45 | 487 | usbip_dbg_vhci_rh(" SetHubFeature\n"); |
04679b34 TH |
488 | retval = -EPIPE; |
489 | break; | |
490 | case SetPortFeature: | |
491 | switch (wValue) { | |
1c9de5bf YD |
492 | case USB_PORT_FEAT_LINK_STATE: |
493 | usbip_dbg_vhci_rh( | |
494 | " SetPortFeature: USB_PORT_FEAT_LINK_STATE\n"); | |
495 | if (hcd->speed != HCD_USB3) { | |
496 | pr_err("USB_PORT_FEAT_LINK_STATE req not " | |
497 | "supported for USB 2.0 roothub\n"); | |
498 | goto error; | |
499 | } | |
500 | /* | |
501 | * Since this is dummy we don't have an actual link so | |
502 | * there is nothing to do for the SET_LINK_STATE cmd | |
503 | */ | |
504 | break; | |
505 | case USB_PORT_FEAT_U1_TIMEOUT: | |
506 | usbip_dbg_vhci_rh( | |
507 | " SetPortFeature: USB_PORT_FEAT_U1_TIMEOUT\n"); | |
508 | case USB_PORT_FEAT_U2_TIMEOUT: | |
509 | usbip_dbg_vhci_rh( | |
510 | " SetPortFeature: USB_PORT_FEAT_U2_TIMEOUT\n"); | |
511 | /* TODO: add suspend/resume support! */ | |
512 | if (hcd->speed != HCD_USB3) { | |
513 | pr_err("USB_PORT_FEAT_U1/2_TIMEOUT req not " | |
514 | "supported for USB 2.0 roothub\n"); | |
515 | goto error; | |
516 | } | |
517 | break; | |
04679b34 | 518 | case USB_PORT_FEAT_SUSPEND: |
7923a655 CC |
519 | usbip_dbg_vhci_rh( |
520 | " SetPortFeature: USB_PORT_FEAT_SUSPEND\n"); | |
1c9de5bf YD |
521 | /* Applicable only for USB2.0 hub */ |
522 | if (hcd->speed == HCD_USB3) { | |
523 | pr_err("USB_PORT_FEAT_SUSPEND req not " | |
524 | "supported for USB 3.0 roothub\n"); | |
525 | goto error; | |
526 | } | |
527 | ||
528 | vhci_hcd->port_status[rhport] |= USB_PORT_STAT_SUSPEND; | |
04679b34 | 529 | break; |
1c9de5bf YD |
530 | case USB_PORT_FEAT_POWER: |
531 | usbip_dbg_vhci_rh( | |
532 | " SetPortFeature: USB_PORT_FEAT_POWER\n"); | |
533 | if (hcd->speed == HCD_USB3) | |
534 | vhci_hcd->port_status[rhport] |= USB_SS_PORT_STAT_POWER; | |
535 | else | |
536 | vhci_hcd->port_status[rhport] |= USB_PORT_STAT_POWER; | |
537 | break; | |
538 | case USB_PORT_FEAT_BH_PORT_RESET: | |
539 | usbip_dbg_vhci_rh( | |
540 | " SetPortFeature: USB_PORT_FEAT_BH_PORT_RESET\n"); | |
541 | /* Applicable only for USB3.0 hub */ | |
542 | if (hcd->speed != HCD_USB3) { | |
543 | pr_err("USB_PORT_FEAT_BH_PORT_RESET req not " | |
544 | "supported for USB 2.0 roothub\n"); | |
545 | goto error; | |
546 | } | |
547 | /* FALLS THROUGH */ | |
04679b34 | 548 | case USB_PORT_FEAT_RESET: |
7923a655 CC |
549 | usbip_dbg_vhci_rh( |
550 | " SetPortFeature: USB_PORT_FEAT_RESET\n"); | |
1c9de5bf YD |
551 | /* if it's already enabled, disable */ |
552 | if (hcd->speed == HCD_USB3) { | |
553 | vhci_hcd->port_status[rhport] = 0; | |
554 | vhci_hcd->port_status[rhport] = | |
555 | (USB_SS_PORT_STAT_POWER | | |
556 | USB_PORT_STAT_CONNECTION | | |
557 | USB_PORT_STAT_RESET); | |
558 | } else if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_ENABLE) { | |
559 | vhci_hcd->port_status[rhport] &= ~(USB_PORT_STAT_ENABLE | |
560 | | USB_PORT_STAT_LOW_SPEED | |
561 | | USB_PORT_STAT_HIGH_SPEED); | |
04679b34 | 562 | } |
1c9de5bf | 563 | |
04679b34 | 564 | /* 50msec reset signaling */ |
03cd00d5 | 565 | vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(50); |
04679b34 | 566 | |
1c9de5bf | 567 | /* FALLS THROUGH */ |
04679b34 | 568 | default: |
b8868e45 | 569 | usbip_dbg_vhci_rh(" SetPortFeature: default %d\n", |
071d1d47 | 570 | wValue); |
1c9de5bf YD |
571 | if (hcd->speed == HCD_USB3) { |
572 | if ((vhci_hcd->port_status[rhport] & | |
573 | USB_SS_PORT_STAT_POWER) != 0) { | |
574 | vhci_hcd->port_status[rhport] |= (1 << wValue); | |
575 | } | |
576 | } else | |
577 | if ((vhci_hcd->port_status[rhport] & | |
578 | USB_PORT_STAT_POWER) != 0) { | |
579 | vhci_hcd->port_status[rhport] |= (1 << wValue); | |
580 | } | |
581 | } | |
582 | break; | |
583 | case GetPortErrorCount: | |
584 | usbip_dbg_vhci_rh(" GetPortErrorCount\n"); | |
585 | if (hcd->speed != HCD_USB3) { | |
586 | pr_err("GetPortErrorCount req not " | |
587 | "supported for USB 2.0 roothub\n"); | |
588 | goto error; | |
589 | } | |
590 | /* We'll always return 0 since this is a dummy hub */ | |
591 | *(__le32 *) buf = cpu_to_le32(0); | |
592 | break; | |
593 | case SetHubDepth: | |
594 | usbip_dbg_vhci_rh(" SetHubDepth\n"); | |
595 | if (hcd->speed != HCD_USB3) { | |
596 | pr_err("SetHubDepth req not supported for " | |
597 | "USB 2.0 roothub\n"); | |
598 | goto error; | |
04679b34 TH |
599 | } |
600 | break; | |
04679b34 | 601 | default: |
1c9de5bf YD |
602 | pr_err("default hub control req: %04x v%04x i%04x l%d\n", |
603 | typeReq, wValue, wIndex, wLength); | |
604 | error: | |
04679b34 TH |
605 | /* "protocol stall" on error */ |
606 | retval = -EPIPE; | |
607 | } | |
608 | ||
b8868e45 | 609 | if (usbip_dbg_flag_vhci_rh) { |
1a4b6f66 | 610 | pr_debug("port %d\n", rhport); |
bfb4ce20 MN |
611 | /* Only dump valid port status */ |
612 | if (rhport >= 0) { | |
4a3ca2be | 613 | dump_port_status_diff(prev_port_status[rhport], |
a5c7f019 YD |
614 | vhci_hcd->port_status[rhport], |
615 | hcd->speed == HCD_USB3); | |
bfb4ce20 | 616 | } |
04679b34 | 617 | } |
b8868e45 | 618 | usbip_dbg_vhci_rh(" bye\n"); |
04679b34 | 619 | |
03cd00d5 | 620 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 | 621 | |
1c9de5bf YD |
622 | if ((vhci_hcd->port_status[rhport] & PORT_C_MASK) != 0) |
623 | usb_hcd_poll_rh_status(hcd); | |
624 | ||
04679b34 TH |
625 | return retval; |
626 | } | |
627 | ||
4eebed9a | 628 | static void vhci_tx_urb(struct urb *urb, struct vhci_device *vdev) |
04679b34 | 629 | { |
04679b34 | 630 | struct vhci_priv *priv; |
03cd00d5 | 631 | struct vhci_hcd *vhci_hcd; |
21619792 | 632 | unsigned long flags; |
04679b34 TH |
633 | |
634 | if (!vdev) { | |
1a4b6f66 | 635 | pr_err("could not get virtual device"); |
04679b34 TH |
636 | return; |
637 | } | |
03cd00d5 | 638 | vhci_hcd = vdev_to_vhci_hcd(vdev); |
04679b34 | 639 | |
b8868e45 | 640 | priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC); |
04679b34 | 641 | if (!priv) { |
04679b34 TH |
642 | usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC); |
643 | return; | |
644 | } | |
645 | ||
21619792 | 646 | spin_lock_irqsave(&vdev->priv_lock, flags); |
78110bb8 | 647 | |
03cd00d5 | 648 | priv->seqnum = atomic_inc_return(&vhci_hcd->seqnum); |
04679b34 | 649 | if (priv->seqnum == 0xffff) |
1a4b6f66 | 650 | dev_info(&urb->dev->dev, "seqnum max\n"); |
04679b34 TH |
651 | |
652 | priv->vdev = vdev; | |
653 | priv->urb = urb; | |
654 | ||
655 | urb->hcpriv = (void *) priv; | |
656 | ||
04679b34 TH |
657 | list_add_tail(&priv->list, &vdev->priv_tx); |
658 | ||
659 | wake_up(&vdev->waitq_tx); | |
21619792 | 660 | spin_unlock_irqrestore(&vdev->priv_lock, flags); |
04679b34 TH |
661 | } |
662 | ||
1c9de5bf | 663 | static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags) |
04679b34 | 664 | { |
03cd00d5 YD |
665 | struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd); |
666 | struct vhci *vhci = vhci_hcd->vhci; | |
04679b34 | 667 | struct device *dev = &urb->dev->dev; |
0775a9cb | 668 | u8 portnum = urb->dev->portnum; |
04679b34 | 669 | int ret = 0; |
6d212153 | 670 | struct vhci_device *vdev; |
21619792 | 671 | unsigned long flags; |
04679b34 | 672 | |
b8868e45 | 673 | usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n", |
071d1d47 | 674 | hcd, urb, mem_flags); |
04679b34 | 675 | |
0775a9cb NI |
676 | if (portnum > VHCI_HC_PORTS) { |
677 | pr_err("invalid port number %d\n", portnum); | |
678 | return -ENODEV; | |
679 | } | |
03cd00d5 | 680 | vdev = &vhci_hcd->vdev[portnum-1]; |
0775a9cb | 681 | |
04679b34 TH |
682 | /* patch to usb_sg_init() is in 2.5.60 */ |
683 | BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length); | |
684 | ||
0775a9cb | 685 | spin_lock_irqsave(&vhci->lock, flags); |
04679b34 | 686 | |
04679b34 TH |
687 | if (urb->status != -EINPROGRESS) { |
688 | dev_err(dev, "URB already unlinked!, status %d\n", urb->status); | |
0775a9cb | 689 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 TH |
690 | return urb->status; |
691 | } | |
692 | ||
6d212153 MV |
693 | /* refuse enqueue for dead connection */ |
694 | spin_lock(&vdev->ud.lock); | |
071d1d47 | 695 | if (vdev->ud.status == VDEV_ST_NULL || |
696 | vdev->ud.status == VDEV_ST_ERROR) { | |
1a4b6f66 | 697 | dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport); |
6d212153 | 698 | spin_unlock(&vdev->ud.lock); |
0775a9cb | 699 | spin_unlock_irqrestore(&vhci->lock, flags); |
6d212153 MV |
700 | return -ENODEV; |
701 | } | |
702 | spin_unlock(&vdev->ud.lock); | |
703 | ||
04679b34 TH |
704 | ret = usb_hcd_link_urb_to_ep(hcd, urb); |
705 | if (ret) | |
706 | goto no_need_unlink; | |
707 | ||
708 | /* | |
b8868e45 | 709 | * The enumeration process is as follows; |
04679b34 TH |
710 | * |
711 | * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0) | |
712 | * to get max packet length of default pipe | |
713 | * | |
714 | * 2. Set_Address request to DevAddr(0) EndPoint(0) | |
715 | * | |
716 | */ | |
04679b34 TH |
717 | if (usb_pipedevice(urb->pipe) == 0) { |
718 | __u8 type = usb_pipetype(urb->pipe); | |
719 | struct usb_ctrlrequest *ctrlreq = | |
071d1d47 | 720 | (struct usb_ctrlrequest *) urb->setup_packet; |
04679b34 TH |
721 | |
722 | if (type != PIPE_CONTROL || !ctrlreq) { | |
723 | dev_err(dev, "invalid request to devnum 0\n"); | |
a7cd5829 | 724 | ret = -EINVAL; |
04679b34 TH |
725 | goto no_need_xmit; |
726 | } | |
727 | ||
728 | switch (ctrlreq->bRequest) { | |
729 | case USB_REQ_SET_ADDRESS: | |
730 | /* set_address may come when a device is reset */ | |
731 | dev_info(dev, "SetAddress Request (%d) to port %d\n", | |
732 | ctrlreq->wValue, vdev->rhport); | |
733 | ||
03f3df81 | 734 | usb_put_dev(vdev->udev); |
7606ee8a | 735 | vdev->udev = usb_get_dev(urb->dev); |
04679b34 TH |
736 | |
737 | spin_lock(&vdev->ud.lock); | |
738 | vdev->ud.status = VDEV_ST_USED; | |
739 | spin_unlock(&vdev->ud.lock); | |
740 | ||
741 | if (urb->status == -EINPROGRESS) { | |
742 | /* This request is successfully completed. */ | |
743 | /* If not -EINPROGRESS, possibly unlinked. */ | |
744 | urb->status = 0; | |
745 | } | |
746 | ||
747 | goto no_need_xmit; | |
748 | ||
749 | case USB_REQ_GET_DESCRIPTOR: | |
d0306a51 | 750 | if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8)) |
7923a655 CC |
751 | usbip_dbg_vhci_hc( |
752 | "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n"); | |
04679b34 | 753 | |
03f3df81 | 754 | usb_put_dev(vdev->udev); |
7606ee8a | 755 | vdev->udev = usb_get_dev(urb->dev); |
04679b34 TH |
756 | goto out; |
757 | ||
758 | default: | |
759 | /* NOT REACHED */ | |
7923a655 CC |
760 | dev_err(dev, |
761 | "invalid request to devnum 0 bRequest %u, wValue %u\n", | |
762 | ctrlreq->bRequest, | |
04679b34 TH |
763 | ctrlreq->wValue); |
764 | ret = -EINVAL; | |
765 | goto no_need_xmit; | |
766 | } | |
767 | ||
768 | } | |
769 | ||
770 | out: | |
4eebed9a | 771 | vhci_tx_urb(urb, vdev); |
0775a9cb | 772 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 TH |
773 | |
774 | return 0; | |
775 | ||
776 | no_need_xmit: | |
777 | usb_hcd_unlink_urb_from_ep(hcd, urb); | |
778 | no_need_unlink: | |
0775a9cb | 779 | spin_unlock_irqrestore(&vhci->lock, flags); |
03b0a528 | 780 | if (!ret) |
0775a9cb | 781 | usb_hcd_giveback_urb(hcd, urb, urb->status); |
a7cd5829 | 782 | return ret; |
04679b34 TH |
783 | } |
784 | ||
785 | /* | |
786 | * vhci_rx gives back the urb after receiving the reply of the urb. If an | |
787 | * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives | |
788 | * back its urb. For the driver unlinking the urb, the content of the urb is | |
789 | * not important, but the calling to its completion handler is important; the | |
790 | * completion of unlinking is notified by the completion handler. | |
791 | * | |
792 | * | |
793 | * CLIENT SIDE | |
794 | * | |
795 | * - When vhci_hcd receives RET_SUBMIT, | |
796 | * | |
797 | * - case 1a). the urb of the pdu is not unlinking. | |
798 | * - normal case | |
799 | * => just give back the urb | |
800 | * | |
801 | * - case 1b). the urb of the pdu is unlinking. | |
802 | * - usbip.ko will return a reply of the unlinking request. | |
803 | * => give back the urb now and go to case 2b). | |
804 | * | |
805 | * - When vhci_hcd receives RET_UNLINK, | |
806 | * | |
807 | * - case 2a). a submit request is still pending in vhci_hcd. | |
808 | * - urb was really pending in usbip.ko and urb_unlink_urb() was | |
809 | * completed there. | |
810 | * => free a pending submit request | |
811 | * => notify unlink completeness by giving back the urb | |
812 | * | |
813 | * - case 2b). a submit request is *not* pending in vhci_hcd. | |
814 | * - urb was already given back to the core driver. | |
815 | * => do not give back the urb | |
816 | * | |
817 | * | |
818 | * SERVER SIDE | |
819 | * | |
820 | * - When usbip receives CMD_UNLINK, | |
821 | * | |
822 | * - case 3a). the urb of the unlink request is now in submission. | |
823 | * => do usb_unlink_urb(). | |
824 | * => after the unlink is completed, send RET_UNLINK. | |
825 | * | |
826 | * - case 3b). the urb of the unlink request is not in submission. | |
827 | * - may be already completed or never be received | |
828 | * => send RET_UNLINK | |
829 | * | |
830 | */ | |
831 | static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) | |
832 | { | |
03cd00d5 YD |
833 | struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd); |
834 | struct vhci *vhci = vhci_hcd->vhci; | |
04679b34 TH |
835 | struct vhci_priv *priv; |
836 | struct vhci_device *vdev; | |
21619792 | 837 | unsigned long flags; |
04679b34 | 838 | |
1a4b6f66 | 839 | pr_info("dequeue a urb %p\n", urb); |
04679b34 | 840 | |
0775a9cb | 841 | spin_lock_irqsave(&vhci->lock, flags); |
04679b34 TH |
842 | |
843 | priv = urb->hcpriv; | |
844 | if (!priv) { | |
845 | /* URB was never linked! or will be soon given back by | |
846 | * vhci_rx. */ | |
0775a9cb | 847 | spin_unlock_irqrestore(&vhci->lock, flags); |
635e664a | 848 | return -EIDRM; |
04679b34 TH |
849 | } |
850 | ||
851 | { | |
852 | int ret = 0; | |
3eed8c03 | 853 | |
04679b34 TH |
854 | ret = usb_hcd_check_unlink_urb(hcd, urb, status); |
855 | if (ret) { | |
0775a9cb | 856 | spin_unlock_irqrestore(&vhci->lock, flags); |
b8868e45 | 857 | return ret; |
04679b34 TH |
858 | } |
859 | } | |
860 | ||
861 | /* send unlink request here? */ | |
862 | vdev = priv->vdev; | |
863 | ||
864 | if (!vdev->ud.tcp_socket) { | |
865 | /* tcp connection is closed */ | |
50b66b5c | 866 | spin_lock(&vdev->priv_lock); |
04679b34 | 867 | |
1a4b6f66 | 868 | pr_info("device %p seems to be disconnected\n", vdev); |
04679b34 TH |
869 | list_del(&priv->list); |
870 | kfree(priv); | |
871 | urb->hcpriv = NULL; | |
872 | ||
50b66b5c | 873 | spin_unlock(&vdev->priv_lock); |
04679b34 | 874 | |
b8868e45 BM |
875 | /* |
876 | * If tcp connection is alive, we have sent CMD_UNLINK. | |
877 | * vhci_rx will receive RET_UNLINK and give back the URB. | |
878 | * Otherwise, we give back it here. | |
879 | */ | |
1a4b6f66 | 880 | pr_info("gives back urb %p\n", urb); |
b8868e45 BM |
881 | |
882 | usb_hcd_unlink_urb_from_ep(hcd, urb); | |
883 | ||
0775a9cb | 884 | spin_unlock_irqrestore(&vhci->lock, flags); |
03cd00d5 | 885 | usb_hcd_giveback_urb(hcd, urb, urb->status); |
0775a9cb | 886 | spin_lock_irqsave(&vhci->lock, flags); |
b8868e45 | 887 | |
04679b34 TH |
888 | } else { |
889 | /* tcp connection is alive */ | |
04679b34 TH |
890 | struct vhci_unlink *unlink; |
891 | ||
50b66b5c | 892 | spin_lock(&vdev->priv_lock); |
04679b34 TH |
893 | |
894 | /* setup CMD_UNLINK pdu */ | |
895 | unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC); | |
896 | if (!unlink) { | |
50b66b5c | 897 | spin_unlock(&vdev->priv_lock); |
0775a9cb | 898 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 TH |
899 | usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC); |
900 | return -ENOMEM; | |
901 | } | |
902 | ||
03cd00d5 | 903 | unlink->seqnum = atomic_inc_return(&vhci_hcd->seqnum); |
04679b34 | 904 | if (unlink->seqnum == 0xffff) |
1a4b6f66 | 905 | pr_info("seqnum max\n"); |
04679b34 TH |
906 | |
907 | unlink->unlink_seqnum = priv->seqnum; | |
908 | ||
1a4b6f66 | 909 | pr_info("device %p seems to be still connected\n", vdev); |
04679b34 TH |
910 | |
911 | /* send cmd_unlink and try to cancel the pending URB in the | |
912 | * peer */ | |
913 | list_add_tail(&unlink->list, &vdev->unlink_tx); | |
914 | wake_up(&vdev->waitq_tx); | |
915 | ||
50b66b5c | 916 | spin_unlock(&vdev->priv_lock); |
04679b34 TH |
917 | } |
918 | ||
0775a9cb | 919 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 | 920 | |
b8868e45 | 921 | usbip_dbg_vhci_hc("leave\n"); |
04679b34 TH |
922 | return 0; |
923 | } | |
924 | ||
04679b34 TH |
925 | static void vhci_device_unlink_cleanup(struct vhci_device *vdev) |
926 | { | |
03cd00d5 YD |
927 | struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev); |
928 | struct usb_hcd *hcd = vhci_hcd_to_hcd(vhci_hcd); | |
929 | struct vhci *vhci = vhci_hcd->vhci; | |
04679b34 | 930 | struct vhci_unlink *unlink, *tmp; |
21619792 | 931 | unsigned long flags; |
04679b34 | 932 | |
0775a9cb | 933 | spin_lock_irqsave(&vhci->lock, flags); |
04679b34 TH |
934 | spin_lock(&vdev->priv_lock); |
935 | ||
936 | list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) { | |
1a4b6f66 | 937 | pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum); |
04679b34 TH |
938 | list_del(&unlink->list); |
939 | kfree(unlink); | |
940 | } | |
941 | ||
236742de | 942 | while (!list_empty(&vdev->unlink_rx)) { |
b92a5e23 MV |
943 | struct urb *urb; |
944 | ||
236742de BB |
945 | unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink, |
946 | list); | |
947 | ||
b92a5e23 | 948 | /* give back URB of unanswered unlink request */ |
1a4b6f66 | 949 | pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum); |
b92a5e23 MV |
950 | |
951 | urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum); | |
952 | if (!urb) { | |
1a4b6f66 | 953 | pr_info("the urb (seqnum %lu) was already given back\n", |
954 | unlink->unlink_seqnum); | |
b92a5e23 MV |
955 | list_del(&unlink->list); |
956 | kfree(unlink); | |
957 | continue; | |
958 | } | |
959 | ||
960 | urb->status = -ENODEV; | |
961 | ||
0775a9cb | 962 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
236742de BB |
963 | |
964 | list_del(&unlink->list); | |
965 | ||
966 | spin_unlock(&vdev->priv_lock); | |
0775a9cb | 967 | spin_unlock_irqrestore(&vhci->lock, flags); |
b92a5e23 | 968 | |
0775a9cb | 969 | usb_hcd_giveback_urb(hcd, urb, urb->status); |
b92a5e23 | 970 | |
0775a9cb | 971 | spin_lock_irqsave(&vhci->lock, flags); |
236742de BB |
972 | spin_lock(&vdev->priv_lock); |
973 | ||
04679b34 TH |
974 | kfree(unlink); |
975 | } | |
976 | ||
977 | spin_unlock(&vdev->priv_lock); | |
0775a9cb | 978 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 TH |
979 | } |
980 | ||
981 | /* | |
982 | * The important thing is that only one context begins cleanup. | |
983 | * This is why error handling and cleanup become simple. | |
984 | * We do not want to consider race condition as possible. | |
985 | */ | |
986 | static void vhci_shutdown_connection(struct usbip_device *ud) | |
987 | { | |
988 | struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); | |
989 | ||
990 | /* need this? see stub_dev.c */ | |
991 | if (ud->tcp_socket) { | |
1a4b6f66 | 992 | pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket); |
04679b34 TH |
993 | kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR); |
994 | } | |
995 | ||
c7f00899 | 996 | /* kill threads related to this sdev */ |
cbf7d122 | 997 | if (vdev->ud.tcp_rx) { |
ba46ce30 | 998 | kthread_stop_put(vdev->ud.tcp_rx); |
cbf7d122 | 999 | vdev->ud.tcp_rx = NULL; |
1000 | } | |
1001 | if (vdev->ud.tcp_tx) { | |
ba46ce30 | 1002 | kthread_stop_put(vdev->ud.tcp_tx); |
cbf7d122 | 1003 | vdev->ud.tcp_tx = NULL; |
1004 | } | |
1a4b6f66 | 1005 | pr_info("stop threads\n"); |
04679b34 TH |
1006 | |
1007 | /* active connection is closed */ | |
3d0a2a22 | 1008 | if (vdev->ud.tcp_socket) { |
964ea96e | 1009 | sockfd_put(vdev->ud.tcp_socket); |
04679b34 TH |
1010 | vdev->ud.tcp_socket = NULL; |
1011 | } | |
1a4b6f66 | 1012 | pr_info("release socket\n"); |
04679b34 TH |
1013 | |
1014 | vhci_device_unlink_cleanup(vdev); | |
1015 | ||
1016 | /* | |
1017 | * rh_port_disconnect() is a trigger of ... | |
1018 | * usb_disable_device(): | |
1019 | * disable all the endpoints for a USB device. | |
1020 | * usb_disable_endpoint(): | |
1021 | * disable endpoints. pending urbs are unlinked(dequeued). | |
1022 | * | |
1023 | * NOTE: After calling rh_port_disconnect(), the USB device drivers of a | |
4ec2601f | 1024 | * detached device should release used urbs in a cleanup function (i.e. |
04679b34 TH |
1025 | * xxx_disconnect()). Therefore, vhci_hcd does not need to release |
1026 | * pushed urbs and their private data in this function. | |
1027 | * | |
4ec2601f | 1028 | * NOTE: vhci_dequeue() must be considered carefully. When shutting down |
04679b34 TH |
1029 | * a connection, vhci_shutdown_connection() expects vhci_dequeue() |
1030 | * gives back pushed urbs and frees their private data by request of | |
1031 | * the cleanup function of a USB driver. When unlinking a urb with an | |
1032 | * active connection, vhci_dequeue() does not give back the urb which | |
1033 | * is actually given back by vhci_rx after receiving its return pdu. | |
1034 | * | |
1035 | */ | |
0775a9cb | 1036 | rh_port_disconnect(vdev); |
04679b34 | 1037 | |
1a4b6f66 | 1038 | pr_info("disconnect device\n"); |
04679b34 TH |
1039 | } |
1040 | ||
04679b34 TH |
1041 | static void vhci_device_reset(struct usbip_device *ud) |
1042 | { | |
1043 | struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); | |
21619792 | 1044 | unsigned long flags; |
04679b34 | 1045 | |
21619792 | 1046 | spin_lock_irqsave(&ud->lock, flags); |
04679b34 TH |
1047 | |
1048 | vdev->speed = 0; | |
1049 | vdev->devid = 0; | |
1050 | ||
03f3df81 | 1051 | usb_put_dev(vdev->udev); |
7606ee8a MV |
1052 | vdev->udev = NULL; |
1053 | ||
3d0a2a22 | 1054 | if (ud->tcp_socket) { |
964ea96e | 1055 | sockfd_put(ud->tcp_socket); |
3d0a2a22 BB |
1056 | ud->tcp_socket = NULL; |
1057 | } | |
04679b34 TH |
1058 | ud->status = VDEV_ST_NULL; |
1059 | ||
21619792 | 1060 | spin_unlock_irqrestore(&ud->lock, flags); |
04679b34 TH |
1061 | } |
1062 | ||
1063 | static void vhci_device_unusable(struct usbip_device *ud) | |
1064 | { | |
21619792 AG |
1065 | unsigned long flags; |
1066 | ||
1067 | spin_lock_irqsave(&ud->lock, flags); | |
04679b34 | 1068 | ud->status = VDEV_ST_ERROR; |
21619792 | 1069 | spin_unlock_irqrestore(&ud->lock, flags); |
04679b34 TH |
1070 | } |
1071 | ||
1072 | static void vhci_device_init(struct vhci_device *vdev) | |
1073 | { | |
0775a9cb | 1074 | memset(vdev, 0, sizeof(struct vhci_device)); |
04679b34 | 1075 | |
04679b34 TH |
1076 | vdev->ud.side = USBIP_VHCI; |
1077 | vdev->ud.status = VDEV_ST_NULL; | |
04679b34 TH |
1078 | spin_lock_init(&vdev->ud.lock); |
1079 | ||
1080 | INIT_LIST_HEAD(&vdev->priv_rx); | |
1081 | INIT_LIST_HEAD(&vdev->priv_tx); | |
1082 | INIT_LIST_HEAD(&vdev->unlink_tx); | |
1083 | INIT_LIST_HEAD(&vdev->unlink_rx); | |
04679b34 TH |
1084 | spin_lock_init(&vdev->priv_lock); |
1085 | ||
1086 | init_waitqueue_head(&vdev->waitq_tx); | |
1087 | ||
1088 | vdev->ud.eh_ops.shutdown = vhci_shutdown_connection; | |
1089 | vdev->ud.eh_ops.reset = vhci_device_reset; | |
1090 | vdev->ud.eh_ops.unusable = vhci_device_unusable; | |
1091 | ||
1092 | usbip_start_eh(&vdev->ud); | |
1093 | } | |
1094 | ||
0775a9cb NI |
1095 | static int hcd_name_to_id(const char *name) |
1096 | { | |
1097 | char *c; | |
1098 | long val; | |
1099 | int ret; | |
1100 | ||
1101 | c = strchr(name, '.'); | |
1102 | if (c == NULL) | |
1103 | return 0; | |
1104 | ||
1105 | ret = kstrtol(c+1, 10, &val); | |
1106 | if (ret < 0) | |
1107 | return ret; | |
1108 | ||
1109 | return val; | |
1110 | } | |
1111 | ||
03cd00d5 YD |
1112 | static int vhci_setup(struct usb_hcd *hcd) |
1113 | { | |
1114 | struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller)); | |
1115 | hcd->self.sg_tablesize = ~0; | |
1c9de5bf YD |
1116 | if (usb_hcd_is_primary_hcd(hcd)) { |
1117 | vhci->vhci_hcd_hs = hcd_to_vhci_hcd(hcd); | |
1118 | vhci->vhci_hcd_hs->vhci = vhci; | |
1119 | /* | |
1120 | * Mark the first roothub as being USB 2.0. | |
1121 | * The USB 3.0 roothub will be registered later by | |
1122 | * vhci_hcd_probe() | |
1123 | */ | |
1124 | hcd->speed = HCD_USB2; | |
1125 | hcd->self.root_hub->speed = USB_SPEED_HIGH; | |
1126 | } else { | |
1127 | vhci->vhci_hcd_ss = hcd_to_vhci_hcd(hcd); | |
1128 | vhci->vhci_hcd_ss->vhci = vhci; | |
1129 | hcd->speed = HCD_USB3; | |
1130 | hcd->self.root_hub->speed = USB_SPEED_SUPER; | |
1131 | } | |
03cd00d5 YD |
1132 | return 0; |
1133 | } | |
1134 | ||
04679b34 TH |
1135 | static int vhci_start(struct usb_hcd *hcd) |
1136 | { | |
03cd00d5 | 1137 | struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd); |
0775a9cb | 1138 | int id, rhport; |
03cd00d5 | 1139 | int err; |
04679b34 | 1140 | |
b8868e45 | 1141 | usbip_dbg_vhci_hc("enter vhci_start\n"); |
04679b34 | 1142 | |
1c9de5bf YD |
1143 | if (usb_hcd_is_primary_hcd(hcd)) |
1144 | spin_lock_init(&vhci_hcd->vhci->lock); | |
03cd00d5 | 1145 | |
04679b34 TH |
1146 | /* initialize private data of usb_hcd */ |
1147 | ||
0775a9cb | 1148 | for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) { |
03cd00d5 | 1149 | struct vhci_device *vdev = &vhci_hcd->vdev[rhport]; |
3eed8c03 | 1150 | |
04679b34 TH |
1151 | vhci_device_init(vdev); |
1152 | vdev->rhport = rhport; | |
1153 | } | |
1154 | ||
03cd00d5 | 1155 | atomic_set(&vhci_hcd->seqnum, 0); |
04679b34 | 1156 | |
04679b34 | 1157 | hcd->power_budget = 0; /* no limit */ |
04679b34 TH |
1158 | hcd->uses_new_polling = 1; |
1159 | ||
03cd00d5 YD |
1160 | #ifdef CONFIG_USB_OTG |
1161 | hcd->self.otg_port = 1; | |
1162 | #endif | |
1163 | ||
0775a9cb NI |
1164 | id = hcd_name_to_id(hcd_name(hcd)); |
1165 | if (id < 0) { | |
1166 | pr_err("invalid vhci name %s\n", hcd_name(hcd)); | |
1167 | return -EINVAL; | |
1168 | } | |
1169 | ||
04679b34 | 1170 | /* vhci_hcd is now ready to be controlled through sysfs */ |
1c9de5bf | 1171 | if (id == 0 && usb_hcd_is_primary_hcd(hcd)) { |
0775a9cb NI |
1172 | err = vhci_init_attr_group(); |
1173 | if (err) { | |
1174 | pr_err("init attr group\n"); | |
1175 | return err; | |
1176 | } | |
1177 | err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group); | |
1178 | if (err) { | |
1179 | pr_err("create sysfs files\n"); | |
1180 | vhci_finish_attr_group(); | |
1181 | return err; | |
1182 | } | |
1183 | pr_info("created sysfs %s\n", hcd_name(hcd)); | |
04679b34 TH |
1184 | } |
1185 | ||
1186 | return 0; | |
1187 | } | |
1188 | ||
1189 | static void vhci_stop(struct usb_hcd *hcd) | |
1190 | { | |
03cd00d5 | 1191 | struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd); |
0775a9cb | 1192 | int id, rhport; |
04679b34 | 1193 | |
b8868e45 | 1194 | usbip_dbg_vhci_hc("stop VHCI controller\n"); |
04679b34 | 1195 | |
04679b34 | 1196 | /* 1. remove the userland interface of vhci_hcd */ |
0775a9cb | 1197 | id = hcd_name_to_id(hcd_name(hcd)); |
1c9de5bf | 1198 | if (id == 0 && usb_hcd_is_primary_hcd(hcd)) { |
0775a9cb NI |
1199 | sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group); |
1200 | vhci_finish_attr_group(); | |
1201 | } | |
04679b34 TH |
1202 | |
1203 | /* 2. shutdown all the ports of vhci_hcd */ | |
0775a9cb | 1204 | for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) { |
03cd00d5 | 1205 | struct vhci_device *vdev = &vhci_hcd->vdev[rhport]; |
04679b34 TH |
1206 | |
1207 | usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED); | |
1208 | usbip_stop_eh(&vdev->ud); | |
1209 | } | |
04679b34 TH |
1210 | } |
1211 | ||
04679b34 TH |
1212 | static int vhci_get_frame_number(struct usb_hcd *hcd) |
1213 | { | |
083d5ad1 | 1214 | dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n"); |
04679b34 TH |
1215 | return 0; |
1216 | } | |
1217 | ||
04679b34 TH |
1218 | #ifdef CONFIG_PM |
1219 | ||
1220 | /* FIXME: suspend/resume */ | |
1221 | static int vhci_bus_suspend(struct usb_hcd *hcd) | |
1222 | { | |
03cd00d5 | 1223 | struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller)); |
21619792 | 1224 | unsigned long flags; |
04679b34 TH |
1225 | |
1226 | dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); | |
1227 | ||
21619792 | 1228 | spin_lock_irqsave(&vhci->lock, flags); |
04679b34 | 1229 | hcd->state = HC_STATE_SUSPENDED; |
21619792 | 1230 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 TH |
1231 | |
1232 | return 0; | |
1233 | } | |
1234 | ||
1235 | static int vhci_bus_resume(struct usb_hcd *hcd) | |
1236 | { | |
03cd00d5 | 1237 | struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller)); |
04679b34 | 1238 | int rc = 0; |
21619792 | 1239 | unsigned long flags; |
04679b34 TH |
1240 | |
1241 | dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); | |
1242 | ||
21619792 | 1243 | spin_lock_irqsave(&vhci->lock, flags); |
c46cb54d | 1244 | if (!HCD_HW_ACCESSIBLE(hcd)) |
04679b34 | 1245 | rc = -ESHUTDOWN; |
c46cb54d | 1246 | else |
04679b34 | 1247 | hcd->state = HC_STATE_RUNNING; |
21619792 | 1248 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 | 1249 | |
87352760 | 1250 | return rc; |
04679b34 TH |
1251 | } |
1252 | ||
1253 | #else | |
1254 | ||
1255 | #define vhci_bus_suspend NULL | |
1256 | #define vhci_bus_resume NULL | |
1257 | #endif | |
1258 | ||
1c9de5bf YD |
1259 | /* Change a group of bulk endpoints to support multiple stream IDs */ |
1260 | static int vhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, | |
1261 | struct usb_host_endpoint **eps, unsigned int num_eps, | |
1262 | unsigned int num_streams, gfp_t mem_flags) | |
1263 | { | |
1264 | dev_dbg(&hcd->self.root_hub->dev, "vhci_alloc_streams not implemented\n"); | |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | /* Reverts a group of bulk endpoints back to not using stream IDs. */ | |
1269 | static int vhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev, | |
1270 | struct usb_host_endpoint **eps, unsigned int num_eps, | |
1271 | gfp_t mem_flags) | |
1272 | { | |
1273 | dev_dbg(&hcd->self.root_hub->dev, "vhci_free_streams not implemented\n"); | |
1274 | return 0; | |
1275 | } | |
1276 | ||
04679b34 TH |
1277 | static struct hc_driver vhci_hc_driver = { |
1278 | .description = driver_name, | |
1279 | .product_desc = driver_desc, | |
1280 | .hcd_priv_size = sizeof(struct vhci_hcd), | |
1281 | ||
1c9de5bf | 1282 | .flags = HCD_USB3 | HCD_SHARED, |
04679b34 | 1283 | |
03cd00d5 | 1284 | .reset = vhci_setup, |
04679b34 | 1285 | .start = vhci_start, |
00512687 | 1286 | .stop = vhci_stop, |
04679b34 TH |
1287 | |
1288 | .urb_enqueue = vhci_urb_enqueue, | |
1289 | .urb_dequeue = vhci_urb_dequeue, | |
1290 | ||
1291 | .get_frame_number = vhci_get_frame_number, | |
1292 | ||
1293 | .hub_status_data = vhci_hub_status, | |
1294 | .hub_control = vhci_hub_control, | |
1295 | .bus_suspend = vhci_bus_suspend, | |
1296 | .bus_resume = vhci_bus_resume, | |
1c9de5bf YD |
1297 | |
1298 | .alloc_streams = vhci_alloc_streams, | |
1299 | .free_streams = vhci_free_streams, | |
04679b34 TH |
1300 | }; |
1301 | ||
1302 | static int vhci_hcd_probe(struct platform_device *pdev) | |
1303 | { | |
f0d657e8 | 1304 | struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev)); |
03cd00d5 | 1305 | struct usb_hcd *hcd_hs; |
1c9de5bf | 1306 | struct usb_hcd *hcd_ss; |
04679b34 TH |
1307 | int ret; |
1308 | ||
b8868e45 | 1309 | usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id); |
04679b34 | 1310 | |
04679b34 TH |
1311 | /* |
1312 | * Allocate and initialize hcd. | |
1313 | * Our private data is also allocated automatically. | |
1314 | */ | |
03cd00d5 YD |
1315 | hcd_hs = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev)); |
1316 | if (!hcd_hs) { | |
1c9de5bf | 1317 | pr_err("create primary hcd failed\n"); |
04679b34 TH |
1318 | return -ENOMEM; |
1319 | } | |
03cd00d5 | 1320 | hcd_hs->has_tt = 1; |
04679b34 | 1321 | |
04679b34 TH |
1322 | /* |
1323 | * Finish generic HCD structure initialization and register. | |
1324 | * Call the driver's reset() and start() routines. | |
1325 | */ | |
03cd00d5 | 1326 | ret = usb_add_hcd(hcd_hs, 0, 0); |
04679b34 | 1327 | if (ret != 0) { |
03cd00d5 YD |
1328 | pr_err("usb_add_hcd hs failed %d\n", ret); |
1329 | goto put_usb2_hcd; | |
04679b34 TH |
1330 | } |
1331 | ||
1c9de5bf YD |
1332 | hcd_ss = usb_create_shared_hcd(&vhci_hc_driver, &pdev->dev, |
1333 | dev_name(&pdev->dev), hcd_hs); | |
1334 | if (!hcd_ss) { | |
1335 | ret = -ENOMEM; | |
1336 | pr_err("create shared hcd failed\n"); | |
1337 | goto remove_usb2_hcd; | |
1338 | } | |
1339 | ||
1340 | ret = usb_add_hcd(hcd_ss, 0, 0); | |
1341 | if (ret) { | |
1342 | pr_err("usb_add_hcd ss failed %d\n", ret); | |
1343 | goto put_usb3_hcd; | |
1344 | } | |
1345 | ||
b8868e45 | 1346 | usbip_dbg_vhci_hc("bye\n"); |
04679b34 | 1347 | return 0; |
03cd00d5 | 1348 | |
1c9de5bf YD |
1349 | put_usb3_hcd: |
1350 | usb_put_hcd(hcd_ss); | |
1351 | remove_usb2_hcd: | |
1352 | usb_remove_hcd(hcd_hs); | |
03cd00d5 YD |
1353 | put_usb2_hcd: |
1354 | usb_put_hcd(hcd_hs); | |
1355 | vhci->vhci_hcd_hs = NULL; | |
1c9de5bf | 1356 | vhci->vhci_hcd_ss = NULL; |
03cd00d5 | 1357 | return ret; |
04679b34 TH |
1358 | } |
1359 | ||
04679b34 TH |
1360 | static int vhci_hcd_remove(struct platform_device *pdev) |
1361 | { | |
03cd00d5 | 1362 | struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev)); |
04679b34 TH |
1363 | |
1364 | /* | |
1365 | * Disconnects the root hub, | |
1366 | * then reverses the effects of usb_add_hcd(), | |
1367 | * invoking the HCD's stop() methods. | |
1368 | */ | |
1c9de5bf YD |
1369 | usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss)); |
1370 | usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss)); | |
1371 | ||
03cd00d5 YD |
1372 | usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs)); |
1373 | usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs)); | |
1374 | ||
1375 | vhci->vhci_hcd_hs = NULL; | |
1c9de5bf | 1376 | vhci->vhci_hcd_ss = NULL; |
04679b34 | 1377 | |
04679b34 TH |
1378 | return 0; |
1379 | } | |
1380 | ||
04679b34 TH |
1381 | #ifdef CONFIG_PM |
1382 | ||
1383 | /* what should happen for USB/IP under suspend/resume? */ | |
1384 | static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state) | |
1385 | { | |
1386 | struct usb_hcd *hcd; | |
03cd00d5 | 1387 | struct vhci *vhci; |
1c9de5bf | 1388 | int rhport; |
04679b34 TH |
1389 | int connected = 0; |
1390 | int ret = 0; | |
21619792 | 1391 | unsigned long flags; |
04679b34 | 1392 | |
03cd00d5 YD |
1393 | dev_dbg(&pdev->dev, "%s\n", __func__); |
1394 | ||
04679b34 | 1395 | hcd = platform_get_drvdata(pdev); |
0775a9cb NI |
1396 | if (!hcd) |
1397 | return 0; | |
03cd00d5 YD |
1398 | |
1399 | vhci = *((void **)dev_get_platdata(hcd->self.controller)); | |
04679b34 | 1400 | |
0775a9cb | 1401 | spin_lock_irqsave(&vhci->lock, flags); |
04679b34 | 1402 | |
03cd00d5 YD |
1403 | for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) { |
1404 | if (vhci->vhci_hcd_hs->port_status[rhport] & | |
1405 | USB_PORT_STAT_CONNECTION) | |
04679b34 | 1406 | connected += 1; |
1c9de5bf YD |
1407 | |
1408 | if (vhci->vhci_hcd_ss->port_status[rhport] & | |
1409 | USB_PORT_STAT_CONNECTION) | |
1410 | connected += 1; | |
03cd00d5 | 1411 | } |
04679b34 | 1412 | |
0775a9cb | 1413 | spin_unlock_irqrestore(&vhci->lock, flags); |
04679b34 TH |
1414 | |
1415 | if (connected > 0) { | |
7923a655 CC |
1416 | dev_info(&pdev->dev, |
1417 | "We have %d active connection%s. Do not suspend.\n", | |
1418 | connected, (connected == 1 ? "" : "s")); | |
04679b34 TH |
1419 | ret = -EBUSY; |
1420 | } else { | |
1a4b6f66 | 1421 | dev_info(&pdev->dev, "suspend vhci_hcd"); |
04679b34 TH |
1422 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
1423 | } | |
1424 | ||
1425 | return ret; | |
1426 | } | |
1427 | ||
1428 | static int vhci_hcd_resume(struct platform_device *pdev) | |
1429 | { | |
1430 | struct usb_hcd *hcd; | |
1431 | ||
1432 | dev_dbg(&pdev->dev, "%s\n", __func__); | |
1433 | ||
1434 | hcd = platform_get_drvdata(pdev); | |
0775a9cb NI |
1435 | if (!hcd) |
1436 | return 0; | |
04679b34 TH |
1437 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
1438 | usb_hcd_poll_rh_status(hcd); | |
1439 | ||
1440 | return 0; | |
1441 | } | |
1442 | ||
1443 | #else | |
1444 | ||
1445 | #define vhci_hcd_suspend NULL | |
1446 | #define vhci_hcd_resume NULL | |
1447 | ||
1448 | #endif | |
1449 | ||
04679b34 TH |
1450 | static struct platform_driver vhci_driver = { |
1451 | .probe = vhci_hcd_probe, | |
f307da94 | 1452 | .remove = vhci_hcd_remove, |
04679b34 TH |
1453 | .suspend = vhci_hcd_suspend, |
1454 | .resume = vhci_hcd_resume, | |
1455 | .driver = { | |
1c126bc6 | 1456 | .name = driver_name, |
04679b34 TH |
1457 | }, |
1458 | }; | |
1459 | ||
0775a9cb NI |
1460 | static void del_platform_devices(void) |
1461 | { | |
1462 | struct platform_device *pdev; | |
1463 | int i; | |
1464 | ||
1465 | for (i = 0; i < vhci_num_controllers; i++) { | |
89a73d28 | 1466 | pdev = vhcis[i].pdev; |
0775a9cb NI |
1467 | if (pdev != NULL) |
1468 | platform_device_unregister(pdev); | |
89a73d28 | 1469 | vhcis[i].pdev = NULL; |
0775a9cb NI |
1470 | } |
1471 | sysfs_remove_link(&platform_bus.kobj, driver_name); | |
1472 | } | |
04679b34 | 1473 | |
0392bbb6 | 1474 | static int __init vhci_hcd_init(void) |
04679b34 | 1475 | { |
0775a9cb | 1476 | int i, ret; |
04679b34 | 1477 | |
04679b34 TH |
1478 | if (usb_disabled()) |
1479 | return -ENODEV; | |
1480 | ||
0775a9cb NI |
1481 | if (vhci_num_controllers < 1) |
1482 | vhci_num_controllers = 1; | |
1483 | ||
89a73d28 YD |
1484 | vhcis = kcalloc(vhci_num_controllers, sizeof(struct vhci), GFP_KERNEL); |
1485 | if (vhcis == NULL) | |
0775a9cb NI |
1486 | return -ENOMEM; |
1487 | ||
dff3565b YD |
1488 | for (i = 0; i < vhci_num_controllers; i++) { |
1489 | vhcis[i].pdev = platform_device_alloc(driver_name, i); | |
1490 | if (!vhcis[i].pdev) { | |
1491 | i--; | |
1492 | while (i >= 0) | |
1493 | platform_device_put(vhcis[i--].pdev); | |
1494 | ret = -ENOMEM; | |
1495 | goto err_device_alloc; | |
1496 | } | |
1497 | } | |
1498 | for (i = 0; i < vhci_num_controllers; i++) { | |
1499 | void *vhci = &vhcis[i]; | |
1500 | ret = platform_device_add_data(vhcis[i].pdev, &vhci, sizeof(void *)); | |
1501 | if (ret) | |
1502 | goto err_driver_register; | |
1503 | } | |
1504 | ||
04679b34 | 1505 | ret = platform_driver_register(&vhci_driver); |
4d421950 | 1506 | if (ret) |
04679b34 TH |
1507 | goto err_driver_register; |
1508 | ||
0775a9cb | 1509 | for (i = 0; i < vhci_num_controllers; i++) { |
dff3565b YD |
1510 | ret = platform_device_add(vhcis[i].pdev); |
1511 | if (ret < 0) { | |
1512 | i--; | |
1513 | while (i >= 0) | |
1514 | platform_device_del(vhcis[i--].pdev); | |
1515 | goto err_add_hcd; | |
1516 | } | |
0775a9cb | 1517 | } |
04679b34 | 1518 | |
1a4b6f66 | 1519 | pr_info(DRIVER_DESC " v" USBIP_VERSION "\n"); |
04679b34 TH |
1520 | return ret; |
1521 | ||
dff3565b | 1522 | err_add_hcd: |
04679b34 | 1523 | platform_driver_unregister(&vhci_driver); |
04679b34 | 1524 | err_driver_register: |
dff3565b YD |
1525 | for (i = 0; i < vhci_num_controllers; i++) |
1526 | platform_device_put(vhcis[i].pdev); | |
1527 | err_device_alloc: | |
89a73d28 | 1528 | kfree(vhcis); |
04679b34 TH |
1529 | return ret; |
1530 | } | |
04679b34 | 1531 | |
0392bbb6 | 1532 | static void __exit vhci_hcd_exit(void) |
04679b34 | 1533 | { |
0775a9cb | 1534 | del_platform_devices(); |
04679b34 | 1535 | platform_driver_unregister(&vhci_driver); |
89a73d28 | 1536 | kfree(vhcis); |
04679b34 | 1537 | } |
071d1d47 | 1538 | |
0392bbb6 | 1539 | module_init(vhci_hcd_init); |
1540 | module_exit(vhci_hcd_exit); | |
071d1d47 | 1541 | |
1542 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
1543 | MODULE_DESCRIPTION(DRIVER_DESC); | |
4ce0a41f | 1544 | MODULE_LICENSE("GPL"); |
6973c6f2 | 1545 | MODULE_VERSION(USBIP_VERSION); |