staging: usbip: vhci: give back URBs from in-flight unlink requests
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / usbip / vhci_hcd.c
blob3a22f65b66d08494d9378ee0734ffd3cbfef8947
1 /*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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.
20 #include <linux/slab.h>
22 #include "usbip_common.h"
23 #include "vhci.h"
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"
29 MODULE_AUTHOR(DRIVER_AUTHOR);
30 MODULE_DESCRIPTION(DRIVER_DESC);
31 MODULE_LICENSE(DRIVER_LICENCE);
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
46 /* See usb gadget dummy hcd */
49 static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
50 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
51 u16 wIndex, char *buff, u16 wLength);
52 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
53 gfp_t mem_flags);
54 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
55 static int vhci_start(struct usb_hcd *vhci_hcd);
56 static void vhci_stop(struct usb_hcd *hcd);
57 static int vhci_get_frame_number(struct usb_hcd *hcd);
59 static const char driver_name[] = "vhci_hcd";
60 static const char driver_desc[] = "USB/IP Virtual Host Controller";
62 struct vhci_hcd *the_controller;
64 static 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*/
100 static void dump_port_status(u32 status)
102 int i = 0;
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]);
110 printk("\n");
115 void rh_port_connect(int rhport, enum usb_device_speed speed)
117 unsigned long flags;
119 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
121 spin_lock_irqsave(&the_controller->lock, flags);
123 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
124 | (1 << USB_PORT_FEAT_C_CONNECTION);
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;
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); */
141 the_controller->pending_port = rhport;
143 spin_unlock_irqrestore(&the_controller->lock, flags);
145 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
148 void rh_port_disconnect(int rhport)
150 unsigned long flags;
152 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
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);
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); */
166 spin_unlock_irqrestore(&the_controller->lock, flags);
168 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
173 /*----------------------------------------------------------------------*/
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)
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.
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.
196 * So, the maximum number of ports is 31 ( port 0 to port 30) ?
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.
203 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
205 struct vhci_hcd *vhci;
206 unsigned long flags;
207 int retval = 0;
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;
215 *event_bits = 0;
217 vhci = hcd_to_vhci(hcd);
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;
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);
231 *event_bits |= 1 << (rhport + 1);
232 changed = 1;
236 usbip_uinfo("changed %d\n", changed);
238 if (hcd->state == HC_STATE_SUSPENDED)
239 usb_hcd_resume_root_hub(hcd);
241 if (changed)
242 retval = 1 + (VHCI_NPORTS / 8);
243 else
244 retval = 0;
246 done:
247 spin_unlock_irqrestore(&vhci->lock, flags);
248 return retval;
251 /* See hub_configure in hub.c */
252 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
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;
264 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
265 u16 wIndex, char *buf, u16 wLength)
267 struct vhci_hcd *dum;
268 int retval = 0;
269 unsigned long flags;
270 int rhport;
272 u32 prev_port_status[VHCI_NPORTS];
274 if (!HCD_HW_ACCESSIBLE(hcd))
275 return -ETIMEDOUT;
278 * NOTE:
279 * wIndex shows the port number and begins from 1.
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;
288 dum = hcd_to_vhci(hcd);
290 spin_lock_irqsave(&dum->lock, flags);
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];
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);
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;
336 default:
337 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
338 wValue);
339 dum->port_status[rhport] &= ~(1 << wValue);
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;
358 /* we do no care of resume. */
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 * } */
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;
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;
395 #if 0
396 if (dum->driver) {
398 dum->port_status[rhport] |=
399 USB_PORT_STAT_ENABLE;
400 /* give it the best speed we agree on */
401 dum->gadget.speed = dum->driver->speed;
402 dum->gadget.ep0->maxpacket = 64;
403 switch (dum->gadget.speed) {
404 case USB_SPEED_HIGH:
405 dum->port_status[rhport] |=
406 USB_PORT_STAT_HIGH_SPEED;
407 break;
408 case USB_SPEED_LOW:
409 dum->gadget.ep0->maxpacket = 8;
410 dum->port_status[rhport] |=
411 USB_PORT_STAT_LOW_SPEED;
412 break;
413 default:
414 dum->gadget.speed = USB_SPEED_FULL;
415 break;
418 #endif
421 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
422 ((u16 *) buf)[1] =
423 cpu_to_le16(dum->port_status[rhport] >> 16);
425 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
426 ((u16 *)buf)[1]);
427 break;
428 case SetHubFeature:
429 usbip_dbg_vhci_rh(" SetHubFeature\n");
430 retval = -EPIPE;
431 break;
432 case SetPortFeature:
433 switch (wValue) {
434 case USB_PORT_FEAT_SUSPEND:
435 usbip_dbg_vhci_rh(" SetPortFeature: "
436 "USB_PORT_FEAT_SUSPEND\n");
437 printk(KERN_ERR "%s: not yet\n", __func__);
438 #if 0
439 dum->port_status[rhport] |=
440 (1 << USB_PORT_FEAT_SUSPEND);
441 if (dum->driver->suspend) {
442 spin_unlock(&dum->lock);
443 dum->driver->suspend(&dum->gadget);
444 spin_lock(&dum->lock);
446 #endif
447 break;
448 case USB_PORT_FEAT_RESET:
449 usbip_dbg_vhci_rh(" SetPortFeature: "
450 "USB_PORT_FEAT_RESET\n");
451 /* if it's already running, disconnect first */
452 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
453 dum->port_status[rhport] &=
454 ~(USB_PORT_STAT_ENABLE |
455 USB_PORT_STAT_LOW_SPEED |
456 USB_PORT_STAT_HIGH_SPEED);
457 #if 0
458 if (dum->driver) {
459 dev_dbg(hardware, "disconnect\n");
460 stop_activity(dum, dum->driver);
462 #endif
464 /* FIXME test that code path! */
466 /* 50msec reset signaling */
467 dum->re_timeout = jiffies + msecs_to_jiffies(50);
469 /* FALLTHROUGH */
470 default:
471 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
472 wValue);
473 dum->port_status[rhport] |= (1 << wValue);
475 break;
477 default:
478 printk(KERN_ERR "%s: default: no such request\n", __func__);
479 /* dev_dbg (hardware,
480 * "hub control req%04x v%04x i%04x l%d\n",
481 * typeReq, wValue, wIndex, wLength); */
483 /* "protocol stall" on error */
484 retval = -EPIPE;
487 if (usbip_dbg_flag_vhci_rh) {
488 printk(KERN_DEBUG "port %d\n", rhport);
489 dump_port_status(prev_port_status[rhport]);
490 dump_port_status(dum->port_status[rhport]);
492 usbip_dbg_vhci_rh(" bye\n");
494 spin_unlock_irqrestore(&dum->lock, flags);
496 return retval;
501 /*----------------------------------------------------------------------*/
503 static struct vhci_device *get_vdev(struct usb_device *udev)
505 int i;
507 if (!udev)
508 return NULL;
510 for (i = 0; i < VHCI_NPORTS; i++)
511 if (the_controller->vdev[i].udev == udev)
512 return port_to_vdev(i);
514 return NULL;
517 static void vhci_tx_urb(struct urb *urb)
519 struct vhci_device *vdev = get_vdev(urb->dev);
520 struct vhci_priv *priv;
521 unsigned long flag;
523 if (!vdev) {
524 err("could not get virtual device");
525 /* BUG(); */
526 return;
529 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
531 spin_lock_irqsave(&vdev->priv_lock, flag);
533 if (!priv) {
534 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
535 spin_unlock_irqrestore(&vdev->priv_lock, flag);
536 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
537 return;
540 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
541 if (priv->seqnum == 0xffff)
542 usbip_uinfo("seqnum max\n");
544 priv->vdev = vdev;
545 priv->urb = urb;
547 urb->hcpriv = (void *) priv;
550 list_add_tail(&priv->list, &vdev->priv_tx);
552 wake_up(&vdev->waitq_tx);
553 spin_unlock_irqrestore(&vdev->priv_lock, flag);
556 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
557 gfp_t mem_flags)
559 struct device *dev = &urb->dev->dev;
560 int ret = 0;
561 unsigned long flags;
563 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
564 hcd, urb, mem_flags);
566 /* patch to usb_sg_init() is in 2.5.60 */
567 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
569 spin_lock_irqsave(&the_controller->lock, flags);
571 if (urb->status != -EINPROGRESS) {
572 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
573 spin_unlock_irqrestore(&the_controller->lock, flags);
574 return urb->status;
577 ret = usb_hcd_link_urb_to_ep(hcd, urb);
578 if (ret)
579 goto no_need_unlink;
582 * The enumeration process is as follows;
584 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
585 * to get max packet length of default pipe
587 * 2. Set_Address request to DevAddr(0) EndPoint(0)
591 if (usb_pipedevice(urb->pipe) == 0) {
592 __u8 type = usb_pipetype(urb->pipe);
593 struct usb_ctrlrequest *ctrlreq =
594 (struct usb_ctrlrequest *) urb->setup_packet;
595 struct vhci_device *vdev =
596 port_to_vdev(the_controller->pending_port);
598 if (type != PIPE_CONTROL || !ctrlreq) {
599 dev_err(dev, "invalid request to devnum 0\n");
600 ret = -EINVAL;
601 goto no_need_xmit;
604 switch (ctrlreq->bRequest) {
605 case USB_REQ_SET_ADDRESS:
606 /* set_address may come when a device is reset */
607 dev_info(dev, "SetAddress Request (%d) to port %d\n",
608 ctrlreq->wValue, vdev->rhport);
610 if (vdev->udev)
611 usb_put_dev(vdev->udev);
612 vdev->udev = usb_get_dev(urb->dev);
614 spin_lock(&vdev->ud.lock);
615 vdev->ud.status = VDEV_ST_USED;
616 spin_unlock(&vdev->ud.lock);
618 if (urb->status == -EINPROGRESS) {
619 /* This request is successfully completed. */
620 /* If not -EINPROGRESS, possibly unlinked. */
621 urb->status = 0;
624 goto no_need_xmit;
626 case USB_REQ_GET_DESCRIPTOR:
627 if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
628 usbip_dbg_vhci_hc("Not yet?: "
629 "Get_Descriptor to device 0 "
630 "(get max pipe size)\n");
632 if (vdev->udev)
633 usb_put_dev(vdev->udev);
634 vdev->udev = usb_get_dev(urb->dev);
635 goto out;
637 default:
638 /* NOT REACHED */
639 dev_err(dev, "invalid request to devnum 0 bRequest %u, "
640 "wValue %u\n", ctrlreq->bRequest,
641 ctrlreq->wValue);
642 ret = -EINVAL;
643 goto no_need_xmit;
648 out:
649 vhci_tx_urb(urb);
651 spin_unlock_irqrestore(&the_controller->lock, flags);
653 return 0;
655 no_need_xmit:
656 usb_hcd_unlink_urb_from_ep(hcd, urb);
657 no_need_unlink:
658 spin_unlock_irqrestore(&the_controller->lock, flags);
660 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
662 return ret;
666 * vhci_rx gives back the urb after receiving the reply of the urb. If an
667 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
668 * back its urb. For the driver unlinking the urb, the content of the urb is
669 * not important, but the calling to its completion handler is important; the
670 * completion of unlinking is notified by the completion handler.
673 * CLIENT SIDE
675 * - When vhci_hcd receives RET_SUBMIT,
677 * - case 1a). the urb of the pdu is not unlinking.
678 * - normal case
679 * => just give back the urb
681 * - case 1b). the urb of the pdu is unlinking.
682 * - usbip.ko will return a reply of the unlinking request.
683 * => give back the urb now and go to case 2b).
685 * - When vhci_hcd receives RET_UNLINK,
687 * - case 2a). a submit request is still pending in vhci_hcd.
688 * - urb was really pending in usbip.ko and urb_unlink_urb() was
689 * completed there.
690 * => free a pending submit request
691 * => notify unlink completeness by giving back the urb
693 * - case 2b). a submit request is *not* pending in vhci_hcd.
694 * - urb was already given back to the core driver.
695 * => do not give back the urb
698 * SERVER SIDE
700 * - When usbip receives CMD_UNLINK,
702 * - case 3a). the urb of the unlink request is now in submission.
703 * => do usb_unlink_urb().
704 * => after the unlink is completed, send RET_UNLINK.
706 * - case 3b). the urb of the unlink request is not in submission.
707 * - may be already completed or never be received
708 * => send RET_UNLINK
711 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
713 unsigned long flags;
714 struct vhci_priv *priv;
715 struct vhci_device *vdev;
717 usbip_uinfo("vhci_hcd: dequeue a urb %p\n", urb);
720 spin_lock_irqsave(&the_controller->lock, flags);
722 priv = urb->hcpriv;
723 if (!priv) {
724 /* URB was never linked! or will be soon given back by
725 * vhci_rx. */
726 spin_unlock_irqrestore(&the_controller->lock, flags);
727 return 0;
731 int ret = 0;
732 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
733 if (ret) {
734 spin_unlock_irqrestore(&the_controller->lock, flags);
735 return ret;
739 /* send unlink request here? */
740 vdev = priv->vdev;
742 if (!vdev->ud.tcp_socket) {
743 /* tcp connection is closed */
744 unsigned long flags2;
746 spin_lock_irqsave(&vdev->priv_lock, flags2);
748 usbip_uinfo("vhci_hcd: device %p seems to be disconnected\n",
749 vdev);
750 list_del(&priv->list);
751 kfree(priv);
752 urb->hcpriv = NULL;
754 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
757 * If tcp connection is alive, we have sent CMD_UNLINK.
758 * vhci_rx will receive RET_UNLINK and give back the URB.
759 * Otherwise, we give back it here.
761 usbip_uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n",
762 urb);
764 usb_hcd_unlink_urb_from_ep(hcd, urb);
766 spin_unlock_irqrestore(&the_controller->lock, flags);
767 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
768 urb->status);
769 spin_lock_irqsave(&the_controller->lock, flags);
771 } else {
772 /* tcp connection is alive */
773 unsigned long flags2;
774 struct vhci_unlink *unlink;
776 spin_lock_irqsave(&vdev->priv_lock, flags2);
778 /* setup CMD_UNLINK pdu */
779 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
780 if (!unlink) {
781 usbip_uerr("malloc vhci_unlink\n");
782 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
783 spin_unlock_irqrestore(&the_controller->lock, flags);
784 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
785 return -ENOMEM;
788 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
789 if (unlink->seqnum == 0xffff)
790 usbip_uinfo("seqnum max\n");
792 unlink->unlink_seqnum = priv->seqnum;
794 usbip_uinfo("vhci_hcd: device %p seems to be still connected\n",
795 vdev);
797 /* send cmd_unlink and try to cancel the pending URB in the
798 * peer */
799 list_add_tail(&unlink->list, &vdev->unlink_tx);
800 wake_up(&vdev->waitq_tx);
802 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
805 spin_unlock_irqrestore(&the_controller->lock, flags);
807 usbip_dbg_vhci_hc("leave\n");
808 return 0;
811 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
813 struct vhci_unlink *unlink, *tmp;
815 spin_lock(&vdev->priv_lock);
817 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
818 usbip_uinfo("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
819 list_del(&unlink->list);
820 kfree(unlink);
823 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
824 struct urb *urb;
826 /* give back URB of unanswered unlink request */
827 usbip_uinfo("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
829 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
830 if (!urb) {
831 usbip_uinfo("the urb (seqnum %lu) was already given back\n",
832 unlink->unlink_seqnum);
833 list_del(&unlink->list);
834 kfree(unlink);
835 continue;
838 urb->status = -ENODEV;
840 spin_lock(&the_controller->lock);
841 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
842 spin_unlock(&the_controller->lock);
844 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
846 list_del(&unlink->list);
847 kfree(unlink);
850 spin_unlock(&vdev->priv_lock);
854 * The important thing is that only one context begins cleanup.
855 * This is why error handling and cleanup become simple.
856 * We do not want to consider race condition as possible.
858 static void vhci_shutdown_connection(struct usbip_device *ud)
860 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
862 /* need this? see stub_dev.c */
863 if (ud->tcp_socket) {
864 usbip_udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
865 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
868 usbip_stop_threads(&vdev->ud);
869 usbip_uinfo("stop threads\n");
871 /* active connection is closed */
872 if (vdev->ud.tcp_socket != NULL) {
873 sock_release(vdev->ud.tcp_socket);
874 vdev->ud.tcp_socket = NULL;
876 usbip_uinfo("release socket\n");
878 vhci_device_unlink_cleanup(vdev);
881 * rh_port_disconnect() is a trigger of ...
882 * usb_disable_device():
883 * disable all the endpoints for a USB device.
884 * usb_disable_endpoint():
885 * disable endpoints. pending urbs are unlinked(dequeued).
887 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
888 * deteched device should release used urbs in a cleanup function(i.e.
889 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
890 * pushed urbs and their private data in this function.
892 * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
893 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
894 * gives back pushed urbs and frees their private data by request of
895 * the cleanup function of a USB driver. When unlinking a urb with an
896 * active connection, vhci_dequeue() does not give back the urb which
897 * is actually given back by vhci_rx after receiving its return pdu.
900 rh_port_disconnect(vdev->rhport);
902 usbip_uinfo("disconnect device\n");
906 static void vhci_device_reset(struct usbip_device *ud)
908 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
910 spin_lock(&ud->lock);
912 vdev->speed = 0;
913 vdev->devid = 0;
915 if (vdev->udev)
916 usb_put_dev(vdev->udev);
917 vdev->udev = NULL;
919 ud->tcp_socket = NULL;
921 ud->status = VDEV_ST_NULL;
923 spin_unlock(&ud->lock);
926 static void vhci_device_unusable(struct usbip_device *ud)
928 spin_lock(&ud->lock);
930 ud->status = VDEV_ST_ERROR;
932 spin_unlock(&ud->lock);
935 static void vhci_device_init(struct vhci_device *vdev)
937 memset(vdev, 0, sizeof(*vdev));
939 usbip_task_init(&vdev->ud.tcp_rx, "vhci_rx", vhci_rx_loop);
940 usbip_task_init(&vdev->ud.tcp_tx, "vhci_tx", vhci_tx_loop);
942 vdev->ud.side = USBIP_VHCI;
943 vdev->ud.status = VDEV_ST_NULL;
944 /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
945 spin_lock_init(&vdev->ud.lock);
947 INIT_LIST_HEAD(&vdev->priv_rx);
948 INIT_LIST_HEAD(&vdev->priv_tx);
949 INIT_LIST_HEAD(&vdev->unlink_tx);
950 INIT_LIST_HEAD(&vdev->unlink_rx);
951 /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
952 spin_lock_init(&vdev->priv_lock);
954 init_waitqueue_head(&vdev->waitq_tx);
956 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
957 vdev->ud.eh_ops.reset = vhci_device_reset;
958 vdev->ud.eh_ops.unusable = vhci_device_unusable;
960 usbip_start_eh(&vdev->ud);
964 /*----------------------------------------------------------------------*/
966 static int vhci_start(struct usb_hcd *hcd)
968 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
969 int rhport;
970 int err = 0;
972 usbip_dbg_vhci_hc("enter vhci_start\n");
975 /* initialize private data of usb_hcd */
977 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
978 struct vhci_device *vdev = &vhci->vdev[rhport];
979 vhci_device_init(vdev);
980 vdev->rhport = rhport;
983 atomic_set(&vhci->seqnum, 0);
984 spin_lock_init(&vhci->lock);
988 hcd->power_budget = 0; /* no limit */
989 hcd->state = HC_STATE_RUNNING;
990 hcd->uses_new_polling = 1;
993 /* vhci_hcd is now ready to be controlled through sysfs */
994 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
995 if (err) {
996 usbip_uerr("create sysfs files\n");
997 return err;
1000 return 0;
1003 static void vhci_stop(struct usb_hcd *hcd)
1005 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1006 int rhport = 0;
1008 usbip_dbg_vhci_hc("stop VHCI controller\n");
1011 /* 1. remove the userland interface of vhci_hcd */
1012 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
1014 /* 2. shutdown all the ports of vhci_hcd */
1015 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
1016 struct vhci_device *vdev = &vhci->vdev[rhport];
1018 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1019 usbip_stop_eh(&vdev->ud);
1023 usbip_uinfo("vhci_stop done\n");
1026 /*----------------------------------------------------------------------*/
1028 static int vhci_get_frame_number(struct usb_hcd *hcd)
1030 usbip_uerr("Not yet implemented\n");
1031 return 0;
1035 #ifdef CONFIG_PM
1037 /* FIXME: suspend/resume */
1038 static int vhci_bus_suspend(struct usb_hcd *hcd)
1040 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1042 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1044 spin_lock_irq(&vhci->lock);
1045 /* vhci->rh_state = DUMMY_RH_SUSPENDED;
1046 * set_link_state(vhci); */
1047 hcd->state = HC_STATE_SUSPENDED;
1048 spin_unlock_irq(&vhci->lock);
1050 return 0;
1053 static int vhci_bus_resume(struct usb_hcd *hcd)
1055 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1056 int rc = 0;
1058 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1060 spin_lock_irq(&vhci->lock);
1061 if (!HCD_HW_ACCESSIBLE(hcd)) {
1062 rc = -ESHUTDOWN;
1063 } else {
1064 /* vhci->rh_state = DUMMY_RH_RUNNING;
1065 * set_link_state(vhci);
1066 * if (!list_empty(&vhci->urbp_list))
1067 * mod_timer(&vhci->timer, jiffies); */
1068 hcd->state = HC_STATE_RUNNING;
1070 spin_unlock_irq(&vhci->lock);
1071 return rc;
1073 return 0;
1076 #else
1078 #define vhci_bus_suspend NULL
1079 #define vhci_bus_resume NULL
1080 #endif
1084 static struct hc_driver vhci_hc_driver = {
1085 .description = driver_name,
1086 .product_desc = driver_desc,
1087 .hcd_priv_size = sizeof(struct vhci_hcd),
1089 .flags = HCD_USB2,
1091 .start = vhci_start,
1092 .stop = vhci_stop,
1094 .urb_enqueue = vhci_urb_enqueue,
1095 .urb_dequeue = vhci_urb_dequeue,
1097 .get_frame_number = vhci_get_frame_number,
1099 .hub_status_data = vhci_hub_status,
1100 .hub_control = vhci_hub_control,
1101 .bus_suspend = vhci_bus_suspend,
1102 .bus_resume = vhci_bus_resume,
1105 static int vhci_hcd_probe(struct platform_device *pdev)
1107 struct usb_hcd *hcd;
1108 int ret;
1110 usbip_uinfo("proving...\n");
1112 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1114 /* will be removed */
1115 if (pdev->dev.dma_mask) {
1116 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1117 return -EINVAL;
1121 * Allocate and initialize hcd.
1122 * Our private data is also allocated automatically.
1124 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1125 if (!hcd) {
1126 usbip_uerr("create hcd failed\n");
1127 return -ENOMEM;
1131 /* this is private data for vhci_hcd */
1132 the_controller = hcd_to_vhci(hcd);
1135 * Finish generic HCD structure initialization and register.
1136 * Call the driver's reset() and start() routines.
1138 ret = usb_add_hcd(hcd, 0, 0);
1139 if (ret != 0) {
1140 usbip_uerr("usb_add_hcd failed %d\n", ret);
1141 usb_put_hcd(hcd);
1142 the_controller = NULL;
1143 return ret;
1147 usbip_dbg_vhci_hc("bye\n");
1148 return 0;
1152 static int vhci_hcd_remove(struct platform_device *pdev)
1154 struct usb_hcd *hcd;
1156 hcd = platform_get_drvdata(pdev);
1157 if (!hcd)
1158 return 0;
1161 * Disconnects the root hub,
1162 * then reverses the effects of usb_add_hcd(),
1163 * invoking the HCD's stop() methods.
1165 usb_remove_hcd(hcd);
1166 usb_put_hcd(hcd);
1167 the_controller = NULL;
1170 return 0;
1175 #ifdef CONFIG_PM
1177 /* what should happen for USB/IP under suspend/resume? */
1178 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1180 struct usb_hcd *hcd;
1181 int rhport = 0;
1182 int connected = 0;
1183 int ret = 0;
1185 dev_dbg(&pdev->dev, "%s\n", __func__);
1187 hcd = platform_get_drvdata(pdev);
1189 spin_lock(&the_controller->lock);
1191 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1192 if (the_controller->port_status[rhport] &
1193 USB_PORT_STAT_CONNECTION)
1194 connected += 1;
1196 spin_unlock(&the_controller->lock);
1198 if (connected > 0) {
1199 usbip_uinfo("We have %d active connection%s. Do not suspend.\n",
1200 connected, (connected == 1 ? "" : "s"));
1201 ret = -EBUSY;
1202 } else {
1203 usbip_uinfo("suspend vhci_hcd");
1204 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1207 return ret;
1210 static int vhci_hcd_resume(struct platform_device *pdev)
1212 struct usb_hcd *hcd;
1214 dev_dbg(&pdev->dev, "%s\n", __func__);
1216 hcd = platform_get_drvdata(pdev);
1217 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1218 usb_hcd_poll_rh_status(hcd);
1220 return 0;
1223 #else
1225 #define vhci_hcd_suspend NULL
1226 #define vhci_hcd_resume NULL
1228 #endif
1231 static struct platform_driver vhci_driver = {
1232 .probe = vhci_hcd_probe,
1233 .remove = __devexit_p(vhci_hcd_remove),
1234 .suspend = vhci_hcd_suspend,
1235 .resume = vhci_hcd_resume,
1236 .driver = {
1237 .name = (char *) driver_name,
1238 .owner = THIS_MODULE,
1242 /*----------------------------------------------------------------------*/
1245 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1246 * We need to add this virtual device as a platform device arbitrarily:
1247 * 1. platform_device_register()
1249 static void the_pdev_release(struct device *dev)
1251 return;
1254 static struct platform_device the_pdev = {
1255 /* should be the same name as driver_name */
1256 .name = (char *) driver_name,
1257 .id = -1,
1258 .dev = {
1259 /* .driver = &vhci_driver, */
1260 .release = the_pdev_release,
1264 static int __init vhci_init(void)
1266 int ret;
1268 usbip_dbg_vhci_hc("enter\n");
1269 if (usb_disabled())
1270 return -ENODEV;
1272 printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name,
1273 DRIVER_VERSION);
1275 ret = platform_driver_register(&vhci_driver);
1276 if (ret < 0)
1277 goto err_driver_register;
1279 ret = platform_device_register(&the_pdev);
1280 if (ret < 0)
1281 goto err_platform_device_register;
1283 usbip_dbg_vhci_hc("bye\n");
1284 return ret;
1286 /* error occurred */
1287 err_platform_device_register:
1288 platform_driver_unregister(&vhci_driver);
1290 err_driver_register:
1291 usbip_dbg_vhci_hc("bye\n");
1292 return ret;
1294 module_init(vhci_init);
1296 static void __exit vhci_cleanup(void)
1298 usbip_dbg_vhci_hc("enter\n");
1300 platform_device_unregister(&the_pdev);
1301 platform_driver_unregister(&vhci_driver);
1303 usbip_dbg_vhci_hc("bye\n");
1305 module_exit(vhci_cleanup);