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