xref: /openbmc/linux/drivers/usb/usbip/vhci_hcd.c (revision 8730046c)
1 /*
2  * Copyright (C) 2003-2008 Takahiro Hirofuchi
3  * Copyright (C) 2015-2016 Nobuo Iwata
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 
21 #include <linux/init.h>
22 #include <linux/file.h>
23 #include <linux/kernel.h>
24 #include <linux/kthread.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 
29 #include "usbip_common.h"
30 #include "vhci.h"
31 
32 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
33 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
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 
45 /* See usb gadget dummy hcd */
46 
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,
49 			    u16 wIndex, char *buff, u16 wLength);
50 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
51 			    gfp_t mem_flags);
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";
58 static const char driver_desc[] = "USB/IP Virtual Host Controller";
59 
60 int vhci_num_controllers = VHCI_NR_HCS;
61 
62 struct platform_device **vhci_pdevs;
63 
64 static const char * const bit_desc[] = {
65 	"CONNECTION",		/*0*/
66 	"ENABLE",		/*1*/
67 	"SUSPEND",		/*2*/
68 	"OVER_CURRENT",		/*3*/
69 	"RESET",		/*4*/
70 	"R5",			/*5*/
71 	"R6",			/*6*/
72 	"R7",			/*7*/
73 	"POWER",		/*8*/
74 	"LOWSPEED",		/*9*/
75 	"HIGHSPEED",		/*10*/
76 	"PORT_TEST",		/*11*/
77 	"INDICATOR",		/*12*/
78 	"R13",			/*13*/
79 	"R14",			/*14*/
80 	"R15",			/*15*/
81 	"C_CONNECTION",		/*16*/
82 	"C_ENABLE",		/*17*/
83 	"C_SUSPEND",		/*18*/
84 	"C_OVER_CURRENT",	/*19*/
85 	"C_RESET",		/*20*/
86 	"R21",			/*21*/
87 	"R22",			/*22*/
88 	"R23",			/*23*/
89 	"R24",			/*24*/
90 	"R25",			/*25*/
91 	"R26",			/*26*/
92 	"R27",			/*27*/
93 	"R28",			/*28*/
94 	"R29",			/*29*/
95 	"R30",			/*30*/
96 	"R31",			/*31*/
97 };
98 
99 static void dump_port_status_diff(u32 prev_status, u32 new_status)
100 {
101 	int i = 0;
102 	u32 bit = 1;
103 
104 	pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
105 	while (bit) {
106 		u32 prev = prev_status & bit;
107 		u32 new = new_status & bit;
108 		char change;
109 
110 		if (!prev && new)
111 			change = '+';
112 		else if (prev && !new)
113 			change = '-';
114 		else
115 			change = ' ';
116 
117 		if (prev || new)
118 			pr_debug(" %c%s\n", change, bit_desc[i]);
119 		bit <<= 1;
120 		i++;
121 	}
122 	pr_debug("\n");
123 }
124 
125 void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed)
126 {
127 	struct vhci_hcd	*vhci = vdev_to_vhci(vdev);
128 	int		rhport = vdev->rhport;
129 	u32		status;
130 	unsigned long	flags;
131 
132 	usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
133 
134 	spin_lock_irqsave(&vhci->lock, flags);
135 
136 	status = vhci->port_status[rhport];
137 
138 	status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION);
139 
140 	switch (speed) {
141 	case USB_SPEED_HIGH:
142 		status |= USB_PORT_STAT_HIGH_SPEED;
143 		break;
144 	case USB_SPEED_LOW:
145 		status |= USB_PORT_STAT_LOW_SPEED;
146 		break;
147 	default:
148 		break;
149 	}
150 
151 	vhci->port_status[rhport] = status;
152 
153 	spin_unlock_irqrestore(&vhci->lock, flags);
154 
155 	usb_hcd_poll_rh_status(vhci_to_hcd(vhci));
156 }
157 
158 static void rh_port_disconnect(struct vhci_device *vdev)
159 {
160 	struct vhci_hcd	*vhci = vdev_to_vhci(vdev);
161 	int		rhport = vdev->rhport;
162 	u32		status;
163 	unsigned long	flags;
164 
165 	usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
166 
167 	spin_lock_irqsave(&vhci->lock, flags);
168 
169 	status = vhci->port_status[rhport];
170 
171 	status &= ~USB_PORT_STAT_CONNECTION;
172 	status |= (1 << USB_PORT_FEAT_C_CONNECTION);
173 
174 	vhci->port_status[rhport] = status;
175 
176 	spin_unlock_irqrestore(&vhci->lock, flags);
177 	usb_hcd_poll_rh_status(vhci_to_hcd(vhci));
178 }
179 
180 #define PORT_C_MASK				\
181 	((USB_PORT_STAT_C_CONNECTION		\
182 	  | USB_PORT_STAT_C_ENABLE		\
183 	  | USB_PORT_STAT_C_SUSPEND		\
184 	  | USB_PORT_STAT_C_OVERCURRENT		\
185 	  | USB_PORT_STAT_C_RESET) << 16)
186 
187 /*
188  * Returns 0 if the status hasn't changed, or the number of bytes in buf.
189  * Ports are 0-indexed from the HCD point of view,
190  * and 1-indexed from the USB core pointer of view.
191  *
192  * @buf: a bitmap to show which port status has been changed.
193  *  bit  0: reserved
194  *  bit  1: the status of port 0 has been changed.
195  *  bit  2: the status of port 1 has been changed.
196  *  ...
197  */
198 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
199 {
200 	struct vhci_hcd	*vhci;
201 	int		retval;
202 	int		rhport;
203 	int		changed = 0;
204 	unsigned long	flags;
205 
206 	retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8);
207 	memset(buf, 0, retval);
208 
209 	vhci = hcd_to_vhci(hcd);
210 
211 	spin_lock_irqsave(&vhci->lock, flags);
212 	if (!HCD_HW_ACCESSIBLE(hcd)) {
213 		usbip_dbg_vhci_rh("hw accessible flag not on?\n");
214 		goto done;
215 	}
216 
217 	/* check pseudo status register for each port */
218 	for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
219 		if ((vhci->port_status[rhport] & PORT_C_MASK)) {
220 			/* The status of a port has been changed, */
221 			usbip_dbg_vhci_rh("port %d status changed\n", rhport);
222 
223 			buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8;
224 			changed = 1;
225 		}
226 	}
227 
228 	if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
229 		usb_hcd_resume_root_hub(hcd);
230 
231 done:
232 	spin_unlock_irqrestore(&vhci->lock, flags);
233 	return changed ? retval : 0;
234 }
235 
236 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
237 {
238 	memset(desc, 0, sizeof(*desc));
239 	desc->bDescriptorType = USB_DT_HUB;
240 	desc->bDescLength = 9;
241 	desc->wHubCharacteristics = cpu_to_le16(
242 		HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
243 	desc->bNbrPorts = VHCI_HC_PORTS;
244 	desc->u.hs.DeviceRemovable[0] = 0xff;
245 	desc->u.hs.DeviceRemovable[1] = 0xff;
246 }
247 
248 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
249 			    u16 wIndex, char *buf, u16 wLength)
250 {
251 	struct vhci_hcd	*dum;
252 	int             retval = 0;
253 	int		rhport;
254 	unsigned long	flags;
255 
256 	u32 prev_port_status[VHCI_HC_PORTS];
257 
258 	if (!HCD_HW_ACCESSIBLE(hcd))
259 		return -ETIMEDOUT;
260 
261 	/*
262 	 * NOTE:
263 	 * wIndex shows the port number and begins from 1.
264 	 */
265 	usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
266 			  wIndex);
267 	if (wIndex > VHCI_HC_PORTS)
268 		pr_err("invalid port number %d\n", wIndex);
269 	rhport = ((__u8)(wIndex & 0x00ff)) - 1;
270 
271 	dum = hcd_to_vhci(hcd);
272 
273 	spin_lock_irqsave(&dum->lock, flags);
274 
275 	/* store old status and compare now and old later */
276 	if (usbip_dbg_flag_vhci_rh) {
277 		memcpy(prev_port_status, dum->port_status,
278 			sizeof(prev_port_status));
279 	}
280 
281 	switch (typeReq) {
282 	case ClearHubFeature:
283 		usbip_dbg_vhci_rh(" ClearHubFeature\n");
284 		break;
285 	case ClearPortFeature:
286 		switch (wValue) {
287 		case USB_PORT_FEAT_SUSPEND:
288 			if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
289 				/* 20msec signaling */
290 				dum->resuming = 1;
291 				dum->re_timeout =
292 					jiffies + msecs_to_jiffies(20);
293 			}
294 			break;
295 		case USB_PORT_FEAT_POWER:
296 			usbip_dbg_vhci_rh(
297 				" ClearPortFeature: USB_PORT_FEAT_POWER\n");
298 			dum->port_status[rhport] = 0;
299 			dum->resuming = 0;
300 			break;
301 		case USB_PORT_FEAT_C_RESET:
302 			usbip_dbg_vhci_rh(
303 				" ClearPortFeature: USB_PORT_FEAT_C_RESET\n");
304 			switch (dum->vdev[rhport].speed) {
305 			case USB_SPEED_HIGH:
306 				dum->port_status[rhport] |=
307 					USB_PORT_STAT_HIGH_SPEED;
308 				break;
309 			case USB_SPEED_LOW:
310 				dum->port_status[rhport] |=
311 					USB_PORT_STAT_LOW_SPEED;
312 				break;
313 			default:
314 				break;
315 			}
316 		default:
317 			usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
318 					  wValue);
319 			dum->port_status[rhport] &= ~(1 << wValue);
320 			break;
321 		}
322 		break;
323 	case GetHubDescriptor:
324 		usbip_dbg_vhci_rh(" GetHubDescriptor\n");
325 		hub_descriptor((struct usb_hub_descriptor *) buf);
326 		break;
327 	case GetHubStatus:
328 		usbip_dbg_vhci_rh(" GetHubStatus\n");
329 		*(__le32 *) buf = cpu_to_le32(0);
330 		break;
331 	case GetPortStatus:
332 		usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
333 		if (wIndex > VHCI_HC_PORTS || wIndex < 1) {
334 			pr_err("invalid port number %d\n", wIndex);
335 			retval = -EPIPE;
336 		}
337 
338 		/* we do not care about resume. */
339 
340 		/* whoever resets or resumes must GetPortStatus to
341 		 * complete it!!
342 		 */
343 		if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
344 			dum->port_status[rhport] |=
345 				(1 << USB_PORT_FEAT_C_SUSPEND);
346 			dum->port_status[rhport] &=
347 				~(1 << USB_PORT_FEAT_SUSPEND);
348 			dum->resuming = 0;
349 			dum->re_timeout = 0;
350 		}
351 
352 		if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
353 		    0 && time_after(jiffies, dum->re_timeout)) {
354 			dum->port_status[rhport] |=
355 				(1 << USB_PORT_FEAT_C_RESET);
356 			dum->port_status[rhport] &=
357 				~(1 << USB_PORT_FEAT_RESET);
358 			dum->re_timeout = 0;
359 
360 			if (dum->vdev[rhport].ud.status ==
361 			    VDEV_ST_NOTASSIGNED) {
362 				usbip_dbg_vhci_rh(
363 					" enable rhport %d (status %u)\n",
364 					rhport,
365 					dum->vdev[rhport].ud.status);
366 				dum->port_status[rhport] |=
367 					USB_PORT_STAT_ENABLE;
368 			}
369 		}
370 		((__le16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
371 		((__le16 *) buf)[1] =
372 			cpu_to_le16(dum->port_status[rhport] >> 16);
373 
374 		usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
375 				  ((u16 *)buf)[1]);
376 		break;
377 	case SetHubFeature:
378 		usbip_dbg_vhci_rh(" SetHubFeature\n");
379 		retval = -EPIPE;
380 		break;
381 	case SetPortFeature:
382 		switch (wValue) {
383 		case USB_PORT_FEAT_SUSPEND:
384 			usbip_dbg_vhci_rh(
385 				" SetPortFeature: USB_PORT_FEAT_SUSPEND\n");
386 			break;
387 		case USB_PORT_FEAT_RESET:
388 			usbip_dbg_vhci_rh(
389 				" SetPortFeature: USB_PORT_FEAT_RESET\n");
390 			/* if it's already running, disconnect first */
391 			if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
392 				dum->port_status[rhport] &=
393 					~(USB_PORT_STAT_ENABLE |
394 					  USB_PORT_STAT_LOW_SPEED |
395 					  USB_PORT_STAT_HIGH_SPEED);
396 				/* FIXME test that code path! */
397 			}
398 			/* 50msec reset signaling */
399 			dum->re_timeout = jiffies + msecs_to_jiffies(50);
400 
401 			/* FALLTHROUGH */
402 		default:
403 			usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
404 					  wValue);
405 			dum->port_status[rhport] |= (1 << wValue);
406 			break;
407 		}
408 		break;
409 
410 	default:
411 		pr_err("default: no such request\n");
412 
413 		/* "protocol stall" on error */
414 		retval = -EPIPE;
415 	}
416 
417 	if (usbip_dbg_flag_vhci_rh) {
418 		pr_debug("port %d\n", rhport);
419 		/* Only dump valid port status */
420 		if (rhport >= 0) {
421 			dump_port_status_diff(prev_port_status[rhport],
422 					      dum->port_status[rhport]);
423 		}
424 	}
425 	usbip_dbg_vhci_rh(" bye\n");
426 
427 	spin_unlock_irqrestore(&dum->lock, flags);
428 
429 	return retval;
430 }
431 
432 static struct vhci_device *get_vdev(struct usb_device *udev)
433 {
434 	struct platform_device *pdev;
435 	struct usb_hcd *hcd;
436 	struct vhci_hcd *vhci;
437 	int pdev_nr, rhport;
438 
439 	if (!udev)
440 		return NULL;
441 
442 	for (pdev_nr = 0; pdev_nr < vhci_num_controllers; pdev_nr++) {
443 		pdev = *(vhci_pdevs + pdev_nr);
444 		if (pdev == NULL)
445 			continue;
446 		hcd = platform_get_drvdata(pdev);
447 		if (hcd == NULL)
448 			continue;
449 		vhci = hcd_to_vhci(hcd);
450 		for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
451 			if (vhci->vdev[rhport].udev == udev)
452 				return &vhci->vdev[rhport];
453 		}
454 	}
455 
456 	return NULL;
457 }
458 
459 static void vhci_tx_urb(struct urb *urb)
460 {
461 	struct vhci_device *vdev = get_vdev(urb->dev);
462 	struct vhci_priv *priv;
463 	struct vhci_hcd *vhci;
464 	unsigned long flags;
465 
466 	if (!vdev) {
467 		pr_err("could not get virtual device");
468 		return;
469 	}
470 	vhci = vdev_to_vhci(vdev);
471 
472 	priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
473 	if (!priv) {
474 		usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
475 		return;
476 	}
477 
478 	spin_lock_irqsave(&vdev->priv_lock, flags);
479 
480 	priv->seqnum = atomic_inc_return(&vhci->seqnum);
481 	if (priv->seqnum == 0xffff)
482 		dev_info(&urb->dev->dev, "seqnum max\n");
483 
484 	priv->vdev = vdev;
485 	priv->urb = urb;
486 
487 	urb->hcpriv = (void *) priv;
488 
489 	list_add_tail(&priv->list, &vdev->priv_tx);
490 
491 	wake_up(&vdev->waitq_tx);
492 	spin_unlock_irqrestore(&vdev->priv_lock, flags);
493 }
494 
495 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
496 			    gfp_t mem_flags)
497 {
498 	struct vhci_hcd *vhci = hcd_to_vhci(hcd);
499 	struct device *dev = &urb->dev->dev;
500 	u8 portnum = urb->dev->portnum;
501 	int ret = 0;
502 	struct vhci_device *vdev;
503 	unsigned long flags;
504 
505 	usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
506 			  hcd, urb, mem_flags);
507 
508 	if (portnum > VHCI_HC_PORTS) {
509 		pr_err("invalid port number %d\n", portnum);
510 		return -ENODEV;
511 	}
512 	vdev = &vhci->vdev[portnum-1];
513 
514 	/* patch to usb_sg_init() is in 2.5.60 */
515 	BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
516 
517 	spin_lock_irqsave(&vhci->lock, flags);
518 
519 	if (urb->status != -EINPROGRESS) {
520 		dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
521 		spin_unlock_irqrestore(&vhci->lock, flags);
522 		return urb->status;
523 	}
524 
525 	/* refuse enqueue for dead connection */
526 	spin_lock(&vdev->ud.lock);
527 	if (vdev->ud.status == VDEV_ST_NULL ||
528 	    vdev->ud.status == VDEV_ST_ERROR) {
529 		dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
530 		spin_unlock(&vdev->ud.lock);
531 		spin_unlock_irqrestore(&vhci->lock, flags);
532 		return -ENODEV;
533 	}
534 	spin_unlock(&vdev->ud.lock);
535 
536 	ret = usb_hcd_link_urb_to_ep(hcd, urb);
537 	if (ret)
538 		goto no_need_unlink;
539 
540 	/*
541 	 * The enumeration process is as follows;
542 	 *
543 	 *  1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
544 	 *     to get max packet length of default pipe
545 	 *
546 	 *  2. Set_Address request to DevAddr(0) EndPoint(0)
547 	 *
548 	 */
549 	if (usb_pipedevice(urb->pipe) == 0) {
550 		__u8 type = usb_pipetype(urb->pipe);
551 		struct usb_ctrlrequest *ctrlreq =
552 			(struct usb_ctrlrequest *) urb->setup_packet;
553 
554 		if (type != PIPE_CONTROL || !ctrlreq) {
555 			dev_err(dev, "invalid request to devnum 0\n");
556 			ret = -EINVAL;
557 			goto no_need_xmit;
558 		}
559 
560 		switch (ctrlreq->bRequest) {
561 		case USB_REQ_SET_ADDRESS:
562 			/* set_address may come when a device is reset */
563 			dev_info(dev, "SetAddress Request (%d) to port %d\n",
564 				 ctrlreq->wValue, vdev->rhport);
565 
566 			usb_put_dev(vdev->udev);
567 			vdev->udev = usb_get_dev(urb->dev);
568 
569 			spin_lock(&vdev->ud.lock);
570 			vdev->ud.status = VDEV_ST_USED;
571 			spin_unlock(&vdev->ud.lock);
572 
573 			if (urb->status == -EINPROGRESS) {
574 				/* This request is successfully completed. */
575 				/* If not -EINPROGRESS, possibly unlinked. */
576 				urb->status = 0;
577 			}
578 
579 			goto no_need_xmit;
580 
581 		case USB_REQ_GET_DESCRIPTOR:
582 			if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
583 				usbip_dbg_vhci_hc(
584 					"Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
585 
586 			usb_put_dev(vdev->udev);
587 			vdev->udev = usb_get_dev(urb->dev);
588 			goto out;
589 
590 		default:
591 			/* NOT REACHED */
592 			dev_err(dev,
593 				"invalid request to devnum 0 bRequest %u, wValue %u\n",
594 				ctrlreq->bRequest,
595 				ctrlreq->wValue);
596 			ret =  -EINVAL;
597 			goto no_need_xmit;
598 		}
599 
600 	}
601 
602 out:
603 	vhci_tx_urb(urb);
604 	spin_unlock_irqrestore(&vhci->lock, flags);
605 
606 	return 0;
607 
608 no_need_xmit:
609 	usb_hcd_unlink_urb_from_ep(hcd, urb);
610 no_need_unlink:
611 	spin_unlock_irqrestore(&vhci->lock, flags);
612 	if (!ret)
613 		usb_hcd_giveback_urb(hcd, urb, urb->status);
614 	return ret;
615 }
616 
617 /*
618  * vhci_rx gives back the urb after receiving the reply of the urb.  If an
619  * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
620  * back its urb. For the driver unlinking the urb, the content of the urb is
621  * not important, but the calling to its completion handler is important; the
622  * completion of unlinking is notified by the completion handler.
623  *
624  *
625  * CLIENT SIDE
626  *
627  * - When vhci_hcd receives RET_SUBMIT,
628  *
629  *	- case 1a). the urb of the pdu is not unlinking.
630  *		- normal case
631  *		=> just give back the urb
632  *
633  *	- case 1b). the urb of the pdu is unlinking.
634  *		- usbip.ko will return a reply of the unlinking request.
635  *		=> give back the urb now and go to case 2b).
636  *
637  * - When vhci_hcd receives RET_UNLINK,
638  *
639  *	- case 2a). a submit request is still pending in vhci_hcd.
640  *		- urb was really pending in usbip.ko and urb_unlink_urb() was
641  *		  completed there.
642  *		=> free a pending submit request
643  *		=> notify unlink completeness by giving back the urb
644  *
645  *	- case 2b). a submit request is *not* pending in vhci_hcd.
646  *		- urb was already given back to the core driver.
647  *		=> do not give back the urb
648  *
649  *
650  * SERVER SIDE
651  *
652  * - When usbip receives CMD_UNLINK,
653  *
654  *	- case 3a). the urb of the unlink request is now in submission.
655  *		=> do usb_unlink_urb().
656  *		=> after the unlink is completed, send RET_UNLINK.
657  *
658  *	- case 3b). the urb of the unlink request is not in submission.
659  *		- may be already completed or never be received
660  *		=> send RET_UNLINK
661  *
662  */
663 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
664 {
665 	struct vhci_hcd *vhci = hcd_to_vhci(hcd);
666 	struct vhci_priv *priv;
667 	struct vhci_device *vdev;
668 	unsigned long flags;
669 
670 	pr_info("dequeue a urb %p\n", urb);
671 
672 	spin_lock_irqsave(&vhci->lock, flags);
673 
674 	priv = urb->hcpriv;
675 	if (!priv) {
676 		/* URB was never linked! or will be soon given back by
677 		 * vhci_rx. */
678 		spin_unlock_irqrestore(&vhci->lock, flags);
679 		return -EIDRM;
680 	}
681 
682 	{
683 		int ret = 0;
684 
685 		ret = usb_hcd_check_unlink_urb(hcd, urb, status);
686 		if (ret) {
687 			spin_unlock_irqrestore(&vhci->lock, flags);
688 			return ret;
689 		}
690 	}
691 
692 	 /* send unlink request here? */
693 	vdev = priv->vdev;
694 
695 	if (!vdev->ud.tcp_socket) {
696 		/* tcp connection is closed */
697 		spin_lock(&vdev->priv_lock);
698 
699 		pr_info("device %p seems to be disconnected\n", vdev);
700 		list_del(&priv->list);
701 		kfree(priv);
702 		urb->hcpriv = NULL;
703 
704 		spin_unlock(&vdev->priv_lock);
705 
706 		/*
707 		 * If tcp connection is alive, we have sent CMD_UNLINK.
708 		 * vhci_rx will receive RET_UNLINK and give back the URB.
709 		 * Otherwise, we give back it here.
710 		 */
711 		pr_info("gives back urb %p\n", urb);
712 
713 		usb_hcd_unlink_urb_from_ep(hcd, urb);
714 
715 		spin_unlock_irqrestore(&vhci->lock, flags);
716 		usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status);
717 		spin_lock_irqsave(&vhci->lock, flags);
718 
719 	} else {
720 		/* tcp connection is alive */
721 		struct vhci_unlink *unlink;
722 
723 		spin_lock(&vdev->priv_lock);
724 
725 		/* setup CMD_UNLINK pdu */
726 		unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
727 		if (!unlink) {
728 			spin_unlock(&vdev->priv_lock);
729 			spin_unlock_irqrestore(&vhci->lock, flags);
730 			usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
731 			return -ENOMEM;
732 		}
733 
734 		unlink->seqnum = atomic_inc_return(&vhci->seqnum);
735 		if (unlink->seqnum == 0xffff)
736 			pr_info("seqnum max\n");
737 
738 		unlink->unlink_seqnum = priv->seqnum;
739 
740 		pr_info("device %p seems to be still connected\n", vdev);
741 
742 		/* send cmd_unlink and try to cancel the pending URB in the
743 		 * peer */
744 		list_add_tail(&unlink->list, &vdev->unlink_tx);
745 		wake_up(&vdev->waitq_tx);
746 
747 		spin_unlock(&vdev->priv_lock);
748 	}
749 
750 	spin_unlock_irqrestore(&vhci->lock, flags);
751 
752 	usbip_dbg_vhci_hc("leave\n");
753 	return 0;
754 }
755 
756 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
757 {
758 	struct vhci_hcd *vhci = vdev_to_vhci(vdev);
759 	struct usb_hcd *hcd = vhci_to_hcd(vhci);
760 	struct vhci_unlink *unlink, *tmp;
761 	unsigned long flags;
762 
763 	spin_lock_irqsave(&vhci->lock, flags);
764 	spin_lock(&vdev->priv_lock);
765 
766 	list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
767 		pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
768 		list_del(&unlink->list);
769 		kfree(unlink);
770 	}
771 
772 	while (!list_empty(&vdev->unlink_rx)) {
773 		struct urb *urb;
774 
775 		unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
776 			list);
777 
778 		/* give back URB of unanswered unlink request */
779 		pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
780 
781 		urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
782 		if (!urb) {
783 			pr_info("the urb (seqnum %lu) was already given back\n",
784 				unlink->unlink_seqnum);
785 			list_del(&unlink->list);
786 			kfree(unlink);
787 			continue;
788 		}
789 
790 		urb->status = -ENODEV;
791 
792 		usb_hcd_unlink_urb_from_ep(hcd, urb);
793 
794 		list_del(&unlink->list);
795 
796 		spin_unlock(&vdev->priv_lock);
797 		spin_unlock_irqrestore(&vhci->lock, flags);
798 
799 		usb_hcd_giveback_urb(hcd, urb, urb->status);
800 
801 		spin_lock_irqsave(&vhci->lock, flags);
802 		spin_lock(&vdev->priv_lock);
803 
804 		kfree(unlink);
805 	}
806 
807 	spin_unlock(&vdev->priv_lock);
808 	spin_unlock_irqrestore(&vhci->lock, flags);
809 }
810 
811 /*
812  * The important thing is that only one context begins cleanup.
813  * This is why error handling and cleanup become simple.
814  * We do not want to consider race condition as possible.
815  */
816 static void vhci_shutdown_connection(struct usbip_device *ud)
817 {
818 	struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
819 
820 	/* need this? see stub_dev.c */
821 	if (ud->tcp_socket) {
822 		pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
823 		kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
824 	}
825 
826 	/* kill threads related to this sdev */
827 	if (vdev->ud.tcp_rx) {
828 		kthread_stop_put(vdev->ud.tcp_rx);
829 		vdev->ud.tcp_rx = NULL;
830 	}
831 	if (vdev->ud.tcp_tx) {
832 		kthread_stop_put(vdev->ud.tcp_tx);
833 		vdev->ud.tcp_tx = NULL;
834 	}
835 	pr_info("stop threads\n");
836 
837 	/* active connection is closed */
838 	if (vdev->ud.tcp_socket) {
839 		sockfd_put(vdev->ud.tcp_socket);
840 		vdev->ud.tcp_socket = NULL;
841 	}
842 	pr_info("release socket\n");
843 
844 	vhci_device_unlink_cleanup(vdev);
845 
846 	/*
847 	 * rh_port_disconnect() is a trigger of ...
848 	 *   usb_disable_device():
849 	 *	disable all the endpoints for a USB device.
850 	 *   usb_disable_endpoint():
851 	 *	disable endpoints. pending urbs are unlinked(dequeued).
852 	 *
853 	 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
854 	 * detached device should release used urbs in a cleanup function (i.e.
855 	 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
856 	 * pushed urbs and their private data in this function.
857 	 *
858 	 * NOTE: vhci_dequeue() must be considered carefully. When shutting down
859 	 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
860 	 * gives back pushed urbs and frees their private data by request of
861 	 * the cleanup function of a USB driver. When unlinking a urb with an
862 	 * active connection, vhci_dequeue() does not give back the urb which
863 	 * is actually given back by vhci_rx after receiving its return pdu.
864 	 *
865 	 */
866 	rh_port_disconnect(vdev);
867 
868 	pr_info("disconnect device\n");
869 }
870 
871 
872 static void vhci_device_reset(struct usbip_device *ud)
873 {
874 	struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
875 	unsigned long flags;
876 
877 	spin_lock_irqsave(&ud->lock, flags);
878 
879 	vdev->speed  = 0;
880 	vdev->devid  = 0;
881 
882 	usb_put_dev(vdev->udev);
883 	vdev->udev = NULL;
884 
885 	if (ud->tcp_socket) {
886 		sockfd_put(ud->tcp_socket);
887 		ud->tcp_socket = NULL;
888 	}
889 	ud->status = VDEV_ST_NULL;
890 
891 	spin_unlock_irqrestore(&ud->lock, flags);
892 }
893 
894 static void vhci_device_unusable(struct usbip_device *ud)
895 {
896 	unsigned long flags;
897 
898 	spin_lock_irqsave(&ud->lock, flags);
899 	ud->status = VDEV_ST_ERROR;
900 	spin_unlock_irqrestore(&ud->lock, flags);
901 }
902 
903 static void vhci_device_init(struct vhci_device *vdev)
904 {
905 	memset(vdev, 0, sizeof(struct vhci_device));
906 
907 	vdev->ud.side   = USBIP_VHCI;
908 	vdev->ud.status = VDEV_ST_NULL;
909 	spin_lock_init(&vdev->ud.lock);
910 
911 	INIT_LIST_HEAD(&vdev->priv_rx);
912 	INIT_LIST_HEAD(&vdev->priv_tx);
913 	INIT_LIST_HEAD(&vdev->unlink_tx);
914 	INIT_LIST_HEAD(&vdev->unlink_rx);
915 	spin_lock_init(&vdev->priv_lock);
916 
917 	init_waitqueue_head(&vdev->waitq_tx);
918 
919 	vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
920 	vdev->ud.eh_ops.reset = vhci_device_reset;
921 	vdev->ud.eh_ops.unusable = vhci_device_unusable;
922 
923 	usbip_start_eh(&vdev->ud);
924 }
925 
926 static int hcd_name_to_id(const char *name)
927 {
928 	char *c;
929 	long val;
930 	int ret;
931 
932 	c = strchr(name, '.');
933 	if (c == NULL)
934 		return 0;
935 
936 	ret = kstrtol(c+1, 10, &val);
937 	if (ret < 0)
938 		return ret;
939 
940 	return val;
941 }
942 
943 static int vhci_start(struct usb_hcd *hcd)
944 {
945 	struct vhci_hcd *vhci = hcd_to_vhci(hcd);
946 	int id, rhport;
947 	int err = 0;
948 
949 	usbip_dbg_vhci_hc("enter vhci_start\n");
950 
951 	/* initialize private data of usb_hcd */
952 
953 	for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
954 		struct vhci_device *vdev = &vhci->vdev[rhport];
955 
956 		vhci_device_init(vdev);
957 		vdev->rhport = rhport;
958 	}
959 
960 	atomic_set(&vhci->seqnum, 0);
961 	spin_lock_init(&vhci->lock);
962 
963 	hcd->power_budget = 0; /* no limit */
964 	hcd->uses_new_polling = 1;
965 
966 	id = hcd_name_to_id(hcd_name(hcd));
967 	if (id < 0) {
968 		pr_err("invalid vhci name %s\n", hcd_name(hcd));
969 		return -EINVAL;
970 	}
971 
972 	/* vhci_hcd is now ready to be controlled through sysfs */
973 	if (id == 0) {
974 		err = vhci_init_attr_group();
975 		if (err) {
976 			pr_err("init attr group\n");
977 			return err;
978 		}
979 		err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
980 		if (err) {
981 			pr_err("create sysfs files\n");
982 			vhci_finish_attr_group();
983 			return err;
984 		}
985 		pr_info("created sysfs %s\n", hcd_name(hcd));
986 	}
987 
988 	return 0;
989 }
990 
991 static void vhci_stop(struct usb_hcd *hcd)
992 {
993 	struct vhci_hcd *vhci = hcd_to_vhci(hcd);
994 	int id, rhport;
995 
996 	usbip_dbg_vhci_hc("stop VHCI controller\n");
997 
998 	/* 1. remove the userland interface of vhci_hcd */
999 	id = hcd_name_to_id(hcd_name(hcd));
1000 	if (id == 0) {
1001 		sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1002 		vhci_finish_attr_group();
1003 	}
1004 
1005 	/* 2. shutdown all the ports of vhci_hcd */
1006 	for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1007 		struct vhci_device *vdev = &vhci->vdev[rhport];
1008 
1009 		usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1010 		usbip_stop_eh(&vdev->ud);
1011 	}
1012 }
1013 
1014 static int vhci_get_frame_number(struct usb_hcd *hcd)
1015 {
1016 	dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
1017 	return 0;
1018 }
1019 
1020 #ifdef CONFIG_PM
1021 
1022 /* FIXME: suspend/resume */
1023 static int vhci_bus_suspend(struct usb_hcd *hcd)
1024 {
1025 	struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1026 	unsigned long flags;
1027 
1028 	dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1029 
1030 	spin_lock_irqsave(&vhci->lock, flags);
1031 	hcd->state = HC_STATE_SUSPENDED;
1032 	spin_unlock_irqrestore(&vhci->lock, flags);
1033 
1034 	return 0;
1035 }
1036 
1037 static int vhci_bus_resume(struct usb_hcd *hcd)
1038 {
1039 	struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1040 	int rc = 0;
1041 	unsigned long flags;
1042 
1043 	dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1044 
1045 	spin_lock_irqsave(&vhci->lock, flags);
1046 	if (!HCD_HW_ACCESSIBLE(hcd))
1047 		rc = -ESHUTDOWN;
1048 	else
1049 		hcd->state = HC_STATE_RUNNING;
1050 	spin_unlock_irqrestore(&vhci->lock, flags);
1051 
1052 	return rc;
1053 }
1054 
1055 #else
1056 
1057 #define vhci_bus_suspend      NULL
1058 #define vhci_bus_resume       NULL
1059 #endif
1060 
1061 static struct hc_driver vhci_hc_driver = {
1062 	.description	= driver_name,
1063 	.product_desc	= driver_desc,
1064 	.hcd_priv_size	= sizeof(struct vhci_hcd),
1065 
1066 	.flags		= HCD_USB2,
1067 
1068 	.start		= vhci_start,
1069 	.stop		= vhci_stop,
1070 
1071 	.urb_enqueue	= vhci_urb_enqueue,
1072 	.urb_dequeue	= vhci_urb_dequeue,
1073 
1074 	.get_frame_number = vhci_get_frame_number,
1075 
1076 	.hub_status_data = vhci_hub_status,
1077 	.hub_control    = vhci_hub_control,
1078 	.bus_suspend	= vhci_bus_suspend,
1079 	.bus_resume	= vhci_bus_resume,
1080 };
1081 
1082 static int vhci_hcd_probe(struct platform_device *pdev)
1083 {
1084 	struct usb_hcd		*hcd;
1085 	int			ret;
1086 
1087 	usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1088 
1089 	/*
1090 	 * Allocate and initialize hcd.
1091 	 * Our private data is also allocated automatically.
1092 	 */
1093 	hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1094 	if (!hcd) {
1095 		pr_err("create hcd failed\n");
1096 		return -ENOMEM;
1097 	}
1098 	hcd->has_tt = 1;
1099 
1100 	/*
1101 	 * Finish generic HCD structure initialization and register.
1102 	 * Call the driver's reset() and start() routines.
1103 	 */
1104 	ret = usb_add_hcd(hcd, 0, 0);
1105 	if (ret != 0) {
1106 		pr_err("usb_add_hcd failed %d\n", ret);
1107 		usb_put_hcd(hcd);
1108 		return ret;
1109 	}
1110 
1111 	usbip_dbg_vhci_hc("bye\n");
1112 	return 0;
1113 }
1114 
1115 static int vhci_hcd_remove(struct platform_device *pdev)
1116 {
1117 	struct usb_hcd	*hcd;
1118 
1119 	hcd = platform_get_drvdata(pdev);
1120 	if (!hcd)
1121 		return 0;
1122 
1123 	/*
1124 	 * Disconnects the root hub,
1125 	 * then reverses the effects of usb_add_hcd(),
1126 	 * invoking the HCD's stop() methods.
1127 	 */
1128 	usb_remove_hcd(hcd);
1129 	usb_put_hcd(hcd);
1130 
1131 	return 0;
1132 }
1133 
1134 #ifdef CONFIG_PM
1135 
1136 /* what should happen for USB/IP under suspend/resume? */
1137 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1138 {
1139 	struct usb_hcd *hcd;
1140 	struct vhci_hcd *vhci;
1141 	int rhport;
1142 	int connected = 0;
1143 	int ret = 0;
1144 	unsigned long flags;
1145 
1146 	hcd = platform_get_drvdata(pdev);
1147 	if (!hcd)
1148 		return 0;
1149 	vhci = hcd_to_vhci(hcd);
1150 
1151 	spin_lock_irqsave(&vhci->lock, flags);
1152 
1153 	for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++)
1154 		if (vhci->port_status[rhport] & USB_PORT_STAT_CONNECTION)
1155 			connected += 1;
1156 
1157 	spin_unlock_irqrestore(&vhci->lock, flags);
1158 
1159 	if (connected > 0) {
1160 		dev_info(&pdev->dev,
1161 			 "We have %d active connection%s. Do not suspend.\n",
1162 			 connected, (connected == 1 ? "" : "s"));
1163 		ret =  -EBUSY;
1164 	} else {
1165 		dev_info(&pdev->dev, "suspend vhci_hcd");
1166 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1167 	}
1168 
1169 	return ret;
1170 }
1171 
1172 static int vhci_hcd_resume(struct platform_device *pdev)
1173 {
1174 	struct usb_hcd *hcd;
1175 
1176 	dev_dbg(&pdev->dev, "%s\n", __func__);
1177 
1178 	hcd = platform_get_drvdata(pdev);
1179 	if (!hcd)
1180 		return 0;
1181 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1182 	usb_hcd_poll_rh_status(hcd);
1183 
1184 	return 0;
1185 }
1186 
1187 #else
1188 
1189 #define vhci_hcd_suspend	NULL
1190 #define vhci_hcd_resume		NULL
1191 
1192 #endif
1193 
1194 static struct platform_driver vhci_driver = {
1195 	.probe	= vhci_hcd_probe,
1196 	.remove	= vhci_hcd_remove,
1197 	.suspend = vhci_hcd_suspend,
1198 	.resume	= vhci_hcd_resume,
1199 	.driver	= {
1200 		.name = driver_name,
1201 	},
1202 };
1203 
1204 static int add_platform_device(int id)
1205 {
1206 	struct platform_device *pdev;
1207 	int dev_nr;
1208 
1209 	if (id == 0)
1210 		dev_nr = -1;
1211 	else
1212 		dev_nr = id;
1213 
1214 	pdev = platform_device_register_simple(driver_name, dev_nr, NULL, 0);
1215 	if (IS_ERR(pdev))
1216 		return PTR_ERR(pdev);
1217 
1218 	*(vhci_pdevs + id) = pdev;
1219 	return 0;
1220 }
1221 
1222 static void del_platform_devices(void)
1223 {
1224 	struct platform_device *pdev;
1225 	int i;
1226 
1227 	for (i = 0; i < vhci_num_controllers; i++) {
1228 		pdev = *(vhci_pdevs + i);
1229 		if (pdev != NULL)
1230 			platform_device_unregister(pdev);
1231 		*(vhci_pdevs + i) = NULL;
1232 	}
1233 	sysfs_remove_link(&platform_bus.kobj, driver_name);
1234 }
1235 
1236 static int __init vhci_hcd_init(void)
1237 {
1238 	int i, ret;
1239 
1240 	if (usb_disabled())
1241 		return -ENODEV;
1242 
1243 	if (vhci_num_controllers < 1)
1244 		vhci_num_controllers = 1;
1245 
1246 	vhci_pdevs = kcalloc(vhci_num_controllers, sizeof(void *), GFP_KERNEL);
1247 	if (vhci_pdevs == NULL)
1248 		return -ENOMEM;
1249 
1250 	ret = platform_driver_register(&vhci_driver);
1251 	if (ret)
1252 		goto err_driver_register;
1253 
1254 	for (i = 0; i < vhci_num_controllers; i++) {
1255 		ret = add_platform_device(i);
1256 		if (ret)
1257 			goto err_platform_device_register;
1258 	}
1259 
1260 	pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
1261 	return ret;
1262 
1263 err_platform_device_register:
1264 	del_platform_devices();
1265 	platform_driver_unregister(&vhci_driver);
1266 err_driver_register:
1267 	kfree(vhci_pdevs);
1268 	return ret;
1269 }
1270 
1271 static void __exit vhci_hcd_exit(void)
1272 {
1273 	del_platform_devices();
1274 	platform_driver_unregister(&vhci_driver);
1275 	kfree(vhci_pdevs);
1276 }
1277 
1278 module_init(vhci_hcd_init);
1279 module_exit(vhci_hcd_exit);
1280 
1281 MODULE_AUTHOR(DRIVER_AUTHOR);
1282 MODULE_DESCRIPTION(DRIVER_DESC);
1283 MODULE_LICENSE("GPL");
1284 MODULE_VERSION(USBIP_VERSION);
1285