usbip: updates from upstream
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / usb / usbip / vhci_hcd.c
blob364a686e87f9c8d73477ececa96c0edfdda5e291
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>
21 #include <linux/kthread.h>
23 #include "usbip_common.h"
24 #include "vhci.h"
26 #define DRIVER_VERSION "1.0"
27 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
28 #define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP"
29 #define DRIVER_LICENCE "GPL"
30 MODULE_AUTHOR(DRIVER_AUTHOR);
31 MODULE_DESCRIPTION(DRIVER_DESC);
32 MODULE_LICENSE(DRIVER_LICENCE);
37 * TODO
38 * - update root hub emulation
39 * - move the emulation code to userland ?
40 * porting to other operating systems
41 * minimize kernel code
42 * - add suspend/resume code
43 * - clean up everything
47 /* See usb gadget dummy hcd */
50 static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
51 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
52 u16 wIndex, char *buff, u16 wLength);
53 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
54 gfp_t mem_flags);
55 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
56 static int vhci_start(struct usb_hcd *vhci_hcd);
57 static void vhci_stop(struct usb_hcd *hcd);
58 static int vhci_get_frame_number(struct usb_hcd *hcd);
60 static const char driver_name[] = "vhci_hcd";
61 static const char driver_desc[] = "USB/IP Virtual Host Controller";
63 struct vhci_hcd *the_controller;
65 static const char *bit_desc[] = {
66 "CONNECTION", /*0*/
67 "ENABLE", /*1*/
68 "SUSPEND", /*2*/
69 "OVER_CURRENT", /*3*/
70 "RESET", /*4*/
71 "R5", /*5*/
72 "R6", /*6*/
73 "R7", /*7*/
74 "POWER", /*8*/
75 "LOWSPEED", /*9*/
76 "HIGHSPEED", /*10*/
77 "PORT_TEST", /*11*/
78 "INDICATOR", /*12*/
79 "R13", /*13*/
80 "R14", /*14*/
81 "R15", /*15*/
82 "C_CONNECTION", /*16*/
83 "C_ENABLE", /*17*/
84 "C_SUSPEND", /*18*/
85 "C_OVER_CURRENT", /*19*/
86 "C_RESET", /*20*/
87 "R21", /*21*/
88 "R22", /*22*/
89 "R23", /*23*/
90 "R24", /*24*/
91 "R25", /*25*/
92 "R26", /*26*/
93 "R27", /*27*/
94 "R28", /*28*/
95 "R29", /*29*/
96 "R30", /*30*/
97 "R31", /*31*/
101 static void dump_port_status(u32 status)
103 int i = 0;
105 printk(KERN_DEBUG "status %08x:", status);
106 for (i = 0; i < 32; i++) {
107 if (status & (1 << i))
108 printk(" %s", bit_desc[i]);
111 printk("\n");
116 void rh_port_connect(int rhport, enum usb_device_speed speed)
118 unsigned long flags;
120 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
122 spin_lock_irqsave(&the_controller->lock, flags);
124 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
125 | (1 << USB_PORT_FEAT_C_CONNECTION);
127 switch (speed) {
128 case USB_SPEED_HIGH:
129 the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
130 break;
131 case USB_SPEED_LOW:
132 the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
133 break;
134 default:
135 break;
138 /* spin_lock(&the_controller->vdev[rhport].ud.lock);
139 * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
140 * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
142 spin_unlock_irqrestore(&the_controller->lock, flags);
144 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
147 void rh_port_disconnect(int rhport)
149 unsigned long flags;
151 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
153 spin_lock_irqsave(&the_controller->lock, flags);
154 /* stop_activity(dum, driver); */
155 the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
156 the_controller->port_status[rhport] |=
157 (1 << USB_PORT_FEAT_C_CONNECTION);
160 /* not yet complete the disconnection
161 * spin_lock(&vdev->ud.lock);
162 * vdev->ud.status = VHC_ST_DISCONNECT;
163 * spin_unlock(&vdev->ud.lock); */
165 spin_unlock_irqrestore(&the_controller->lock, flags);
167 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
172 /*----------------------------------------------------------------------*/
174 #define PORT_C_MASK \
175 ((USB_PORT_STAT_C_CONNECTION \
176 | USB_PORT_STAT_C_ENABLE \
177 | USB_PORT_STAT_C_SUSPEND \
178 | USB_PORT_STAT_C_OVERCURRENT \
179 | USB_PORT_STAT_C_RESET) << 16)
182 * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
183 * suspend/resume support. But, it is modified to provide multiple ports.
185 * @buf: a bitmap to show which port status has been changed.
186 * bit 0: reserved or used for another purpose?
187 * bit 1: the status of port 0 has been changed.
188 * bit 2: the status of port 1 has been changed.
189 * ...
190 * bit 7: the status of port 6 has been changed.
191 * bit 8: the status of port 7 has been changed.
192 * ...
193 * bit 15: the status of port 14 has been changed.
195 * So, the maximum number of ports is 31 ( port 0 to port 30) ?
197 * The return value is the actual transfered length in byte. If nothing has
198 * been changed, return 0. In the case that the number of ports is less than or
199 * equal to 6 (VHCI_NPORTS==7), return 1.
202 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
204 struct vhci_hcd *vhci;
205 unsigned long flags;
206 int retval = 0;
208 /* the enough buffer is allocated according to USB_MAXCHILDREN */
209 unsigned long *event_bits = (unsigned long *) buf;
210 int rhport;
211 int changed = 0;
214 *event_bits = 0;
216 vhci = hcd_to_vhci(hcd);
218 spin_lock_irqsave(&vhci->lock, flags);
219 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
220 usbip_dbg_vhci_rh("hw accessible flag in on?\n");
221 goto done;
224 /* check pseudo status register for each port */
225 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
226 if ((vhci->port_status[rhport] & PORT_C_MASK)) {
227 /* The status of a port has been changed, */
228 usbip_dbg_vhci_rh("port %d is changed\n", rhport);
230 *event_bits |= 1 << (rhport + 1);
231 changed = 1;
235 usbip_uinfo("changed %d\n", changed);
237 if (hcd->state == HC_STATE_SUSPENDED)
238 usb_hcd_resume_root_hub(hcd);
240 if (changed)
241 retval = 1 + (VHCI_NPORTS / 8);
242 else
243 retval = 0;
245 done:
246 spin_unlock_irqrestore(&vhci->lock, flags);
247 return retval;
250 /* See hub_configure in hub.c */
251 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
253 memset(desc, 0, sizeof(*desc));
254 desc->bDescriptorType = 0x29;
255 desc->bDescLength = 9;
256 desc->wHubCharacteristics = (__force __u16)
257 (__constant_cpu_to_le16(0x0001));
258 desc->bNbrPorts = VHCI_NPORTS;
259 desc->bitmap[0] = 0xff;
260 desc->bitmap[1] = 0xff;
263 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
264 u16 wIndex, char *buf, u16 wLength)
266 struct vhci_hcd *dum;
267 int retval = 0;
268 unsigned long flags;
269 int rhport;
271 u32 prev_port_status[VHCI_NPORTS];
273 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
274 return -ETIMEDOUT;
277 * NOTE:
278 * wIndex shows the port number and begins from 1.
280 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
281 wIndex);
282 if (wIndex > VHCI_NPORTS)
283 printk(KERN_ERR "%s: invalid port number %d\n", __func__,
284 wIndex);
285 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
287 dum = hcd_to_vhci(hcd);
289 spin_lock_irqsave(&dum->lock, flags);
291 /* store old status and compare now and old later */
292 if (usbip_dbg_flag_vhci_rh) {
293 memcpy(prev_port_status, dum->port_status,
294 sizeof(prev_port_status));
297 switch (typeReq) {
298 case ClearHubFeature:
299 usbip_dbg_vhci_rh(" ClearHubFeature\n");
300 break;
301 case ClearPortFeature:
302 switch (wValue) {
303 case USB_PORT_FEAT_SUSPEND:
304 if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
305 /* 20msec signaling */
306 dum->resuming = 1;
307 dum->re_timeout =
308 jiffies + msecs_to_jiffies(20);
310 break;
311 case USB_PORT_FEAT_POWER:
312 usbip_dbg_vhci_rh(" ClearPortFeature: "
313 "USB_PORT_FEAT_POWER\n");
314 dum->port_status[rhport] = 0;
315 /* dum->address = 0; */
316 /* dum->hdev = 0; */
317 dum->resuming = 0;
318 break;
319 case USB_PORT_FEAT_C_RESET:
320 usbip_dbg_vhci_rh(" ClearPortFeature: "
321 "USB_PORT_FEAT_C_RESET\n");
322 switch (dum->vdev[rhport].speed) {
323 case USB_SPEED_HIGH:
324 dum->port_status[rhport] |=
325 USB_PORT_STAT_HIGH_SPEED;
326 break;
327 case USB_SPEED_LOW:
328 dum->port_status[rhport] |=
329 USB_PORT_STAT_LOW_SPEED;
330 break;
331 default:
332 break;
334 default:
335 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
336 wValue);
337 dum->port_status[rhport] &= ~(1 << wValue);
339 break;
340 case GetHubDescriptor:
341 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
342 hub_descriptor((struct usb_hub_descriptor *) buf);
343 break;
344 case GetHubStatus:
345 usbip_dbg_vhci_rh(" GetHubStatus\n");
346 *(__le32 *) buf = __constant_cpu_to_le32(0);
347 break;
348 case GetPortStatus:
349 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
350 if (wIndex > VHCI_NPORTS || wIndex < 1) {
351 printk(KERN_ERR "%s: invalid port number %d\n",
352 __func__, wIndex);
353 retval = -EPIPE;
356 /* we do no care of resume. */
358 /* whoever resets or resumes must GetPortStatus to
359 * complete it!!
360 * */
361 if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
362 printk(KERN_ERR "%s: not yet\n", __func__);
363 dum->port_status[rhport] |=
364 (1 << USB_PORT_FEAT_C_SUSPEND);
365 dum->port_status[rhport] &=
366 ~(1 << USB_PORT_FEAT_SUSPEND);
367 dum->resuming = 0;
368 dum->re_timeout = 0;
369 /* if (dum->driver && dum->driver->resume) {
370 * spin_unlock (&dum->lock);
371 * dum->driver->resume (&dum->gadget);
372 * spin_lock (&dum->lock);
373 * } */
376 if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
377 0 && time_after(jiffies, dum->re_timeout)) {
378 dum->port_status[rhport] |=
379 (1 << USB_PORT_FEAT_C_RESET);
380 dum->port_status[rhport] &=
381 ~(1 << USB_PORT_FEAT_RESET);
382 dum->re_timeout = 0;
384 if (dum->vdev[rhport].ud.status ==
385 VDEV_ST_NOTASSIGNED) {
386 usbip_dbg_vhci_rh(" enable rhport %d "
387 "(status %u)\n",
388 rhport,
389 dum->vdev[rhport].ud.status);
390 dum->port_status[rhport] |=
391 USB_PORT_STAT_ENABLE;
393 #if 0
394 if (dum->driver) {
396 dum->port_status[rhport] |=
397 USB_PORT_STAT_ENABLE;
398 /* give it the best speed we agree on */
399 dum->gadget.speed = dum->driver->speed;
400 dum->gadget.ep0->maxpacket = 64;
401 switch (dum->gadget.speed) {
402 case USB_SPEED_HIGH:
403 dum->port_status[rhport] |=
404 USB_PORT_STAT_HIGH_SPEED;
405 break;
406 case USB_SPEED_LOW:
407 dum->gadget.ep0->maxpacket = 8;
408 dum->port_status[rhport] |=
409 USB_PORT_STAT_LOW_SPEED;
410 break;
411 default:
412 dum->gadget.speed = USB_SPEED_FULL;
413 break;
416 #endif
419 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
420 ((u16 *) buf)[1] =
421 cpu_to_le16(dum->port_status[rhport] >> 16);
423 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
424 ((u16 *)buf)[1]);
425 break;
426 case SetHubFeature:
427 usbip_dbg_vhci_rh(" SetHubFeature\n");
428 retval = -EPIPE;
429 break;
430 case SetPortFeature:
431 switch (wValue) {
432 case USB_PORT_FEAT_SUSPEND:
433 usbip_dbg_vhci_rh(" SetPortFeature: "
434 "USB_PORT_FEAT_SUSPEND\n");
435 printk(KERN_ERR "%s: not yet\n", __func__);
436 #if 0
437 dum->port_status[rhport] |=
438 (1 << USB_PORT_FEAT_SUSPEND);
439 if (dum->driver->suspend) {
440 spin_unlock(&dum->lock);
441 dum->driver->suspend(&dum->gadget);
442 spin_lock(&dum->lock);
444 #endif
445 break;
446 case USB_PORT_FEAT_RESET:
447 usbip_dbg_vhci_rh(" SetPortFeature: "
448 "USB_PORT_FEAT_RESET\n");
449 /* if it's already running, disconnect first */
450 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
451 dum->port_status[rhport] &=
452 ~(USB_PORT_STAT_ENABLE |
453 USB_PORT_STAT_LOW_SPEED |
454 USB_PORT_STAT_HIGH_SPEED);
455 #if 0
456 if (dum->driver) {
457 dev_dbg(hardware, "disconnect\n");
458 stop_activity(dum, dum->driver);
460 #endif
462 /* FIXME test that code path! */
464 /* 50msec reset signaling */
465 dum->re_timeout = jiffies + msecs_to_jiffies(50);
467 /* FALLTHROUGH */
468 default:
469 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
470 wValue);
471 dum->port_status[rhport] |= (1 << wValue);
473 break;
475 default:
476 printk(KERN_ERR "%s: default: no such request\n", __func__);
477 /* dev_dbg (hardware,
478 * "hub control req%04x v%04x i%04x l%d\n",
479 * typeReq, wValue, wIndex, wLength); */
481 /* "protocol stall" on error */
482 retval = -EPIPE;
485 if (usbip_dbg_flag_vhci_rh) {
486 printk(KERN_DEBUG "port %d\n", rhport);
487 dump_port_status(prev_port_status[rhport]);
488 dump_port_status(dum->port_status[rhport]);
490 usbip_dbg_vhci_rh(" bye\n");
492 spin_unlock_irqrestore(&dum->lock, flags);
494 return retval;
499 /*----------------------------------------------------------------------*/
501 static struct vhci_device *get_vdev(struct usb_device *udev)
503 int i;
505 if (!udev)
506 return NULL;
508 for (i = 0; i < VHCI_NPORTS; i++)
509 if (the_controller->vdev[i].udev == udev)
510 return port_to_vdev(i);
512 return NULL;
515 static void vhci_tx_urb(struct urb *urb)
517 struct vhci_device *vdev = get_vdev(urb->dev);
518 struct vhci_priv *priv;
519 unsigned long flag;
521 if (!vdev) {
522 err("could not get virtual device");
523 /* BUG(); */
524 return;
527 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
529 spin_lock_irqsave(&vdev->priv_lock, flag);
531 if (!priv) {
532 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
533 spin_unlock_irqrestore(&vdev->priv_lock, flag);
534 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
535 return;
538 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
539 if (priv->seqnum == 0xffff)
540 usbip_uinfo("seqnum max\n");
542 priv->vdev = vdev;
543 priv->urb = urb;
545 urb->hcpriv = (void *) priv;
548 list_add_tail(&priv->list, &vdev->priv_tx);
550 wake_up(&vdev->waitq_tx);
551 spin_unlock_irqrestore(&vdev->priv_lock, flag);
554 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
555 gfp_t mem_flags)
557 struct device *dev = &urb->dev->dev;
558 int ret = 0;
559 unsigned long flags;
560 struct vhci_device *vdev;
562 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
563 hcd, urb, mem_flags);
565 /* patch to usb_sg_init() is in 2.5.60 */
566 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
568 spin_lock_irqsave(&the_controller->lock, flags);
570 if (urb->status != -EINPROGRESS) {
571 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
572 spin_unlock_irqrestore(&the_controller->lock, flags);
573 return urb->status;
576 vdev = port_to_vdev(urb->dev->portnum-1);
578 /* refuse enqueue for dead connection */
579 spin_lock(&vdev->ud.lock);
580 if (vdev->ud.status == VDEV_ST_NULL || vdev->ud.status == VDEV_ST_ERROR) {
581 usbip_uerr("enqueue for inactive port %d\n", vdev->rhport);
582 spin_unlock(&vdev->ud.lock);
583 spin_unlock_irqrestore(&the_controller->lock, flags);
584 return -ENODEV;
586 spin_unlock(&vdev->ud.lock);
588 ret = usb_hcd_link_urb_to_ep(hcd, urb);
589 if (ret)
590 goto no_need_unlink;
593 * The enumeration process is as follows;
595 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
596 * to get max packet length of default pipe
598 * 2. Set_Address request to DevAddr(0) EndPoint(0)
602 if (usb_pipedevice(urb->pipe) == 0) {
603 __u8 type = usb_pipetype(urb->pipe);
604 struct usb_ctrlrequest *ctrlreq =
605 (struct usb_ctrlrequest *) urb->setup_packet;
607 if (type != PIPE_CONTROL || !ctrlreq) {
608 dev_err(dev, "invalid request to devnum 0\n");
609 ret = -EINVAL;
610 goto no_need_xmit;
613 switch (ctrlreq->bRequest) {
614 case USB_REQ_SET_ADDRESS:
615 /* set_address may come when a device is reset */
616 dev_info(dev, "SetAddress Request (%d) to port %d\n",
617 ctrlreq->wValue, vdev->rhport);
619 if (vdev->udev)
620 usb_put_dev(vdev->udev);
621 vdev->udev = usb_get_dev(urb->dev);
623 spin_lock(&vdev->ud.lock);
624 vdev->ud.status = VDEV_ST_USED;
625 spin_unlock(&vdev->ud.lock);
627 if (urb->status == -EINPROGRESS) {
628 /* This request is successfully completed. */
629 /* If not -EINPROGRESS, possibly unlinked. */
630 urb->status = 0;
633 goto no_need_xmit;
635 case USB_REQ_GET_DESCRIPTOR:
636 if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
637 usbip_dbg_vhci_hc("Not yet?: "
638 "Get_Descriptor to device 0 "
639 "(get max pipe size)\n");
641 if (vdev->udev)
642 usb_put_dev(vdev->udev);
643 vdev->udev = usb_get_dev(urb->dev);
644 goto out;
646 default:
647 /* NOT REACHED */
648 dev_err(dev, "invalid request to devnum 0 bRequest %u, "
649 "wValue %u\n", ctrlreq->bRequest,
650 ctrlreq->wValue);
651 ret = -EINVAL;
652 goto no_need_xmit;
657 out:
658 vhci_tx_urb(urb);
660 spin_unlock_irqrestore(&the_controller->lock, flags);
662 return 0;
664 no_need_xmit:
665 usb_hcd_unlink_urb_from_ep(hcd, urb);
666 no_need_unlink:
667 spin_unlock_irqrestore(&the_controller->lock, flags);
669 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
671 return ret;
675 * vhci_rx gives back the urb after receiving the reply of the urb. If an
676 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
677 * back its urb. For the driver unlinking the urb, the content of the urb is
678 * not important, but the calling to its completion handler is important; the
679 * completion of unlinking is notified by the completion handler.
682 * CLIENT SIDE
684 * - When vhci_hcd receives RET_SUBMIT,
686 * - case 1a). the urb of the pdu is not unlinking.
687 * - normal case
688 * => just give back the urb
690 * - case 1b). the urb of the pdu is unlinking.
691 * - usbip.ko will return a reply of the unlinking request.
692 * => give back the urb now and go to case 2b).
694 * - When vhci_hcd receives RET_UNLINK,
696 * - case 2a). a submit request is still pending in vhci_hcd.
697 * - urb was really pending in usbip.ko and urb_unlink_urb() was
698 * completed there.
699 * => free a pending submit request
700 * => notify unlink completeness by giving back the urb
702 * - case 2b). a submit request is *not* pending in vhci_hcd.
703 * - urb was already given back to the core driver.
704 * => do not give back the urb
707 * SERVER SIDE
709 * - When usbip receives CMD_UNLINK,
711 * - case 3a). the urb of the unlink request is now in submission.
712 * => do usb_unlink_urb().
713 * => after the unlink is completed, send RET_UNLINK.
715 * - case 3b). the urb of the unlink request is not in submission.
716 * - may be already completed or never be received
717 * => send RET_UNLINK
720 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
722 unsigned long flags;
723 struct vhci_priv *priv;
724 struct vhci_device *vdev;
726 usbip_uinfo("vhci_hcd: dequeue a urb %p\n", urb);
729 spin_lock_irqsave(&the_controller->lock, flags);
731 priv = urb->hcpriv;
732 if (!priv) {
733 /* URB was never linked! or will be soon given back by
734 * vhci_rx. */
735 spin_unlock_irqrestore(&the_controller->lock, flags);
736 return 0;
740 int ret = 0;
741 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
742 if (ret) {
743 spin_unlock_irqrestore(&the_controller->lock, flags);
744 return ret;
748 /* send unlink request here? */
749 vdev = priv->vdev;
751 if (!vdev->ud.tcp_socket) {
752 /* tcp connection is closed */
753 unsigned long flags2;
755 spin_lock_irqsave(&vdev->priv_lock, flags2);
757 usbip_uinfo("vhci_hcd: device %p seems to be disconnected\n",
758 vdev);
759 list_del(&priv->list);
760 kfree(priv);
761 urb->hcpriv = NULL;
763 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
766 * If tcp connection is alive, we have sent CMD_UNLINK.
767 * vhci_rx will receive RET_UNLINK and give back the URB.
768 * Otherwise, we give back it here.
770 usbip_uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n",
771 urb);
773 usb_hcd_unlink_urb_from_ep(hcd, urb);
775 spin_unlock_irqrestore(&the_controller->lock, flags);
776 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
777 urb->status);
778 spin_lock_irqsave(&the_controller->lock, flags);
780 } else {
781 /* tcp connection is alive */
782 unsigned long flags2;
783 struct vhci_unlink *unlink;
785 spin_lock_irqsave(&vdev->priv_lock, flags2);
787 /* setup CMD_UNLINK pdu */
788 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
789 if (!unlink) {
790 usbip_uerr("malloc vhci_unlink\n");
791 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
792 spin_unlock_irqrestore(&the_controller->lock, flags);
793 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
794 return -ENOMEM;
797 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
798 if (unlink->seqnum == 0xffff)
799 usbip_uinfo("seqnum max\n");
801 unlink->unlink_seqnum = priv->seqnum;
803 usbip_uinfo("vhci_hcd: device %p seems to be still connected\n",
804 vdev);
806 /* send cmd_unlink and try to cancel the pending URB in the
807 * peer */
808 list_add_tail(&unlink->list, &vdev->unlink_tx);
809 wake_up(&vdev->waitq_tx);
811 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
814 spin_unlock_irqrestore(&the_controller->lock, flags);
816 usbip_dbg_vhci_hc("leave\n");
817 return 0;
820 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
822 struct vhci_unlink *unlink, *tmp;
824 spin_lock(&vdev->priv_lock);
826 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
827 usbip_uinfo("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
828 list_del(&unlink->list);
829 kfree(unlink);
832 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
833 struct urb *urb;
835 /* give back URB of unanswered unlink request */
836 usbip_uinfo("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
838 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
839 if (!urb) {
840 usbip_uinfo("the urb (seqnum %lu) was already given back\n",
841 unlink->unlink_seqnum);
842 list_del(&unlink->list);
843 kfree(unlink);
844 continue;
847 urb->status = -ENODEV;
849 spin_lock(&the_controller->lock);
850 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
851 spin_unlock(&the_controller->lock);
853 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
855 list_del(&unlink->list);
856 kfree(unlink);
859 spin_unlock(&vdev->priv_lock);
863 * The important thing is that only one context begins cleanup.
864 * This is why error handling and cleanup become simple.
865 * We do not want to consider race condition as possible.
867 static void vhci_shutdown_connection(struct usbip_device *ud)
869 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
871 /* need this? see stub_dev.c */
872 if (ud->tcp_socket) {
873 usbip_udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
874 ud->tcp_socket->ops->shutdown(ud->tcp_socket, SEND_SHUTDOWN|RCV_SHUTDOWN);
877 /* kill threads related to this sdev, if v.c. exists */
878 if (vdev->ud.tcp_rx && !task_is_dead(vdev->ud.tcp_rx))
879 kthread_stop(vdev->ud.tcp_rx);
880 if (vdev->ud.tcp_tx && !task_is_dead(vdev->ud.tcp_tx))
881 kthread_stop(vdev->ud.tcp_tx);
883 usbip_uinfo("stop threads\n");
885 /* active connection is closed */
886 if (vdev->ud.tcp_socket != NULL) {
887 sock_release(vdev->ud.tcp_socket);
888 vdev->ud.tcp_socket = NULL;
890 usbip_uinfo("release socket\n");
892 vhci_device_unlink_cleanup(vdev);
895 * rh_port_disconnect() is a trigger of ...
896 * usb_disable_device():
897 * disable all the endpoints for a USB device.
898 * usb_disable_endpoint():
899 * disable endpoints. pending urbs are unlinked(dequeued).
901 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
902 * deteched device should release used urbs in a cleanup function(i.e.
903 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
904 * pushed urbs and their private data in this function.
906 * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
907 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
908 * gives back pushed urbs and frees their private data by request of
909 * the cleanup function of a USB driver. When unlinking a urb with an
910 * active connection, vhci_dequeue() does not give back the urb which
911 * is actually given back by vhci_rx after receiving its return pdu.
914 rh_port_disconnect(vdev->rhport);
916 usbip_uinfo("disconnect device\n");
920 static void vhci_device_reset(struct usbip_device *ud)
922 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
924 spin_lock(&ud->lock);
926 vdev->speed = 0;
927 vdev->devid = 0;
929 if (vdev->udev)
930 usb_put_dev(vdev->udev);
931 vdev->udev = NULL;
933 ud->tcp_socket = NULL;
935 ud->status = VDEV_ST_NULL;
937 spin_unlock(&ud->lock);
940 static void vhci_device_unusable(struct usbip_device *ud)
942 spin_lock(&ud->lock);
944 ud->status = VDEV_ST_ERROR;
946 spin_unlock(&ud->lock);
949 static void vhci_device_init(struct vhci_device *vdev)
951 memset(vdev, 0, sizeof(*vdev));
953 vdev->ud.side = USBIP_VHCI;
954 vdev->ud.status = VDEV_ST_NULL;
955 /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
956 spin_lock_init(&vdev->ud.lock);
958 INIT_LIST_HEAD(&vdev->priv_rx);
959 INIT_LIST_HEAD(&vdev->priv_tx);
960 INIT_LIST_HEAD(&vdev->unlink_tx);
961 INIT_LIST_HEAD(&vdev->unlink_rx);
962 /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
963 spin_lock_init(&vdev->priv_lock);
965 init_waitqueue_head(&vdev->waitq_tx);
967 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
968 vdev->ud.eh_ops.reset = vhci_device_reset;
969 vdev->ud.eh_ops.unusable = vhci_device_unusable;
971 usbip_start_eh(&vdev->ud);
975 /*----------------------------------------------------------------------*/
977 static int vhci_start(struct usb_hcd *hcd)
979 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
980 int rhport;
981 int err = 0;
983 usbip_dbg_vhci_hc("enter vhci_start\n");
986 /* initialize private data of usb_hcd */
988 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
989 struct vhci_device *vdev = &vhci->vdev[rhport];
990 vhci_device_init(vdev);
991 vdev->rhport = rhport;
994 atomic_set(&vhci->seqnum, 0);
995 spin_lock_init(&vhci->lock);
999 hcd->power_budget = 0; /* no limit */
1000 hcd->state = HC_STATE_RUNNING;
1001 hcd->uses_new_polling = 1;
1004 /* vhci_hcd is now ready to be controlled through sysfs */
1005 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
1006 if (err) {
1007 usbip_uerr("create sysfs files\n");
1008 return err;
1011 return 0;
1014 static void vhci_stop(struct usb_hcd *hcd)
1016 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1017 int rhport = 0;
1019 usbip_dbg_vhci_hc("stop VHCI controller\n");
1022 /* 1. remove the userland interface of vhci_hcd */
1023 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
1025 /* 2. shutdown all the ports of vhci_hcd */
1026 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
1027 struct vhci_device *vdev = &vhci->vdev[rhport];
1029 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1030 usbip_stop_eh(&vdev->ud);
1034 usbip_uinfo("vhci_stop done\n");
1037 /*----------------------------------------------------------------------*/
1039 static int vhci_get_frame_number(struct usb_hcd *hcd)
1041 usbip_uerr("Not yet implemented\n");
1042 return 0;
1046 #ifdef CONFIG_PM
1048 /* FIXME: suspend/resume */
1049 static int vhci_bus_suspend(struct usb_hcd *hcd)
1051 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1053 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1055 spin_lock_irq(&vhci->lock);
1056 /* vhci->rh_state = DUMMY_RH_SUSPENDED;
1057 * set_link_state(vhci); */
1058 hcd->state = HC_STATE_SUSPENDED;
1059 spin_unlock_irq(&vhci->lock);
1061 return 0;
1064 static int vhci_bus_resume(struct usb_hcd *hcd)
1066 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1067 int rc = 0;
1069 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1071 spin_lock_irq(&vhci->lock);
1072 if (!HCD_HW_ACCESSIBLE(hcd)) {
1073 rc = -ESHUTDOWN;
1074 } else {
1075 /* vhci->rh_state = DUMMY_RH_RUNNING;
1076 * set_link_state(vhci);
1077 * if (!list_empty(&vhci->urbp_list))
1078 * mod_timer(&vhci->timer, jiffies); */
1079 hcd->state = HC_STATE_RUNNING;
1081 spin_unlock_irq(&vhci->lock);
1082 return rc;
1084 return 0;
1087 #else
1089 #define vhci_bus_suspend NULL
1090 #define vhci_bus_resume NULL
1091 #endif
1095 static struct hc_driver vhci_hc_driver = {
1096 .description = driver_name,
1097 .product_desc = driver_desc,
1098 .hcd_priv_size = sizeof(struct vhci_hcd),
1100 .flags = HCD_USB2,
1102 .start = vhci_start,
1103 .stop = vhci_stop,
1105 .urb_enqueue = vhci_urb_enqueue,
1106 .urb_dequeue = vhci_urb_dequeue,
1108 .get_frame_number = vhci_get_frame_number,
1110 .hub_status_data = vhci_hub_status,
1111 .hub_control = vhci_hub_control,
1112 .bus_suspend = vhci_bus_suspend,
1113 .bus_resume = vhci_bus_resume,
1116 static int vhci_hcd_probe(struct platform_device *pdev)
1118 struct usb_hcd *hcd;
1119 int ret;
1121 usbip_uinfo("proving...\n");
1123 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1125 /* will be removed */
1126 if (pdev->dev.dma_mask) {
1127 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1128 return -EINVAL;
1132 * Allocate and initialize hcd.
1133 * Our private data is also allocated automatically.
1135 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, pdev->dev.bus_id);
1136 if (!hcd) {
1137 usbip_uerr("create hcd failed\n");
1138 return -ENOMEM;
1140 hcd->has_tt = 1;
1142 /* this is private data for vhci_hcd */
1143 the_controller = hcd_to_vhci(hcd);
1146 * Finish generic HCD structure initialization and register.
1147 * Call the driver's reset() and start() routines.
1149 ret = usb_add_hcd(hcd, 0, 0);
1150 if (ret != 0) {
1151 usbip_uerr("usb_add_hcd failed %d\n", ret);
1152 usb_put_hcd(hcd);
1153 the_controller = NULL;
1154 return ret;
1158 usbip_dbg_vhci_hc("bye\n");
1159 return 0;
1163 static int vhci_hcd_remove(struct platform_device *pdev)
1165 struct usb_hcd *hcd;
1167 hcd = platform_get_drvdata(pdev);
1168 if (!hcd)
1169 return 0;
1172 * Disconnects the root hub,
1173 * then reverses the effects of usb_add_hcd(),
1174 * invoking the HCD's stop() methods.
1176 usb_remove_hcd(hcd);
1177 usb_put_hcd(hcd);
1178 the_controller = NULL;
1181 return 0;
1186 #ifdef CONFIG_PM
1188 /* what should happen for USB/IP under suspend/resume? */
1189 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1191 struct usb_hcd *hcd;
1192 int rhport = 0;
1193 int connected = 0;
1194 int ret = 0;
1196 dev_dbg(&pdev->dev, "%s\n", __func__);
1198 hcd = platform_get_drvdata(pdev);
1200 spin_lock(&the_controller->lock);
1202 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1203 if (the_controller->port_status[rhport] &
1204 USB_PORT_STAT_CONNECTION)
1205 connected += 1;
1207 spin_unlock(&the_controller->lock);
1209 if (connected > 0) {
1210 usbip_uinfo("We have %d active connection%s. Do not suspend.\n",
1211 connected, (connected == 1 ? "" : "s"));
1212 ret = -EBUSY;
1213 } else {
1214 usbip_uinfo("suspend vhci_hcd");
1215 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1218 return ret;
1221 static int vhci_hcd_resume(struct platform_device *pdev)
1223 struct usb_hcd *hcd;
1225 dev_dbg(&pdev->dev, "%s\n", __func__);
1227 hcd = platform_get_drvdata(pdev);
1228 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1229 usb_hcd_poll_rh_status(hcd);
1231 return 0;
1234 #else
1236 #define vhci_hcd_suspend NULL
1237 #define vhci_hcd_resume NULL
1239 #endif
1242 static struct platform_driver vhci_driver = {
1243 .probe = vhci_hcd_probe,
1244 .remove = __devexit_p(vhci_hcd_remove),
1245 .suspend = vhci_hcd_suspend,
1246 .resume = vhci_hcd_resume,
1247 .driver = {
1248 .name = (char *) driver_name,
1249 .owner = THIS_MODULE,
1253 /*----------------------------------------------------------------------*/
1256 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1257 * We need to add this virtual device as a platform device arbitrarily:
1258 * 1. platform_device_register()
1260 static void the_pdev_release(struct device *dev)
1262 return;
1265 static struct platform_device the_pdev = {
1266 /* should be the same name as driver_name */
1267 .name = (char *) driver_name,
1268 .id = -1,
1269 .dev = {
1270 /* .driver = &vhci_driver, */
1271 .release = the_pdev_release,
1275 static int __init vhci_init(void)
1277 int ret;
1279 usbip_dbg_vhci_hc("enter\n");
1280 if (usb_disabled())
1281 return -ENODEV;
1283 printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name,
1284 DRIVER_VERSION);
1286 ret = platform_driver_register(&vhci_driver);
1287 if (ret < 0)
1288 goto err_driver_register;
1290 ret = platform_device_register(&the_pdev);
1291 if (ret < 0)
1292 goto err_platform_device_register;
1294 usbip_dbg_vhci_hc("bye\n");
1295 return ret;
1297 /* error occurred */
1298 err_platform_device_register:
1299 platform_driver_unregister(&vhci_driver);
1301 err_driver_register:
1302 usbip_dbg_vhci_hc("bye\n");
1303 return ret;
1305 module_init(vhci_init);
1307 static void __exit vhci_cleanup(void)
1309 usbip_dbg_vhci_hc("enter\n");
1311 platform_device_unregister(&the_pdev);
1312 platform_driver_unregister(&vhci_driver);
1314 usbip_dbg_vhci_hc("bye\n");
1316 module_exit(vhci_cleanup);