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