Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / staging / usbip / vhci_hcd.c
blob2ee97e2095b0ad9bf6b4e07a8d27ff2b161c1e17
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/init.h>
21 #include <linux/kernel.h>
22 #include <linux/kthread.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #include "usbip_common.h"
28 #include "vhci.h"
30 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
31 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
34 * TODO
35 * - update root hub emulation
36 * - move the emulation code to userland ?
37 * porting to other operating systems
38 * minimize kernel code
39 * - add suspend/resume code
40 * - clean up everything
43 /* See usb gadget dummy hcd */
45 static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
46 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
47 u16 wIndex, char *buff, u16 wLength);
48 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
49 gfp_t mem_flags);
50 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
51 static int vhci_start(struct usb_hcd *vhci_hcd);
52 static void vhci_stop(struct usb_hcd *hcd);
53 static int vhci_get_frame_number(struct usb_hcd *hcd);
55 static const char driver_name[] = "vhci_hcd";
56 static const char driver_desc[] = "USB/IP Virtual Host Controller";
58 struct vhci_hcd *the_controller;
60 static const char * const bit_desc[] = {
61 "CONNECTION", /*0*/
62 "ENABLE", /*1*/
63 "SUSPEND", /*2*/
64 "OVER_CURRENT", /*3*/
65 "RESET", /*4*/
66 "R5", /*5*/
67 "R6", /*6*/
68 "R7", /*7*/
69 "POWER", /*8*/
70 "LOWSPEED", /*9*/
71 "HIGHSPEED", /*10*/
72 "PORT_TEST", /*11*/
73 "INDICATOR", /*12*/
74 "R13", /*13*/
75 "R14", /*14*/
76 "R15", /*15*/
77 "C_CONNECTION", /*16*/
78 "C_ENABLE", /*17*/
79 "C_SUSPEND", /*18*/
80 "C_OVER_CURRENT", /*19*/
81 "C_RESET", /*20*/
82 "R21", /*21*/
83 "R22", /*22*/
84 "R23", /*23*/
85 "R24", /*24*/
86 "R25", /*25*/
87 "R26", /*26*/
88 "R27", /*27*/
89 "R28", /*28*/
90 "R29", /*29*/
91 "R30", /*30*/
92 "R31", /*31*/
95 static void dump_port_status_diff(u32 prev_status, u32 new_status)
97 int i = 0;
98 u32 bit = 1;
100 pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
101 while (bit) {
102 u32 prev = prev_status & bit;
103 u32 new = new_status & bit;
104 char change;
106 if (!prev && new)
107 change = '+';
108 else if (prev && !new)
109 change = '-';
110 else
111 change = ' ';
113 if (prev || new)
114 pr_debug(" %c%s\n", change, bit_desc[i]);
115 bit <<= 1;
116 i++;
118 pr_debug("\n");
121 void rh_port_connect(int rhport, enum usb_device_speed speed)
123 unsigned long flags;
125 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
127 spin_lock_irqsave(&the_controller->lock, flags);
129 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
130 | (1 << USB_PORT_FEAT_C_CONNECTION);
132 switch (speed) {
133 case USB_SPEED_HIGH:
134 the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
135 break;
136 case USB_SPEED_LOW:
137 the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
138 break;
139 default:
140 break;
143 /* spin_lock(&the_controller->vdev[rhport].ud.lock);
144 * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
145 * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
147 spin_unlock_irqrestore(&the_controller->lock, flags);
149 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
152 void rh_port_disconnect(int rhport)
154 unsigned long flags;
156 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
158 spin_lock_irqsave(&the_controller->lock, flags);
159 /* stop_activity(dum, driver); */
160 the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
161 the_controller->port_status[rhport] |=
162 (1 << USB_PORT_FEAT_C_CONNECTION);
164 /* not yet complete the disconnection
165 * spin_lock(&vdev->ud.lock);
166 * vdev->ud.status = VHC_ST_DISCONNECT;
167 * spin_unlock(&vdev->ud.lock); */
169 spin_unlock_irqrestore(&the_controller->lock, flags);
170 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
173 #define PORT_C_MASK \
174 ((USB_PORT_STAT_C_CONNECTION \
175 | USB_PORT_STAT_C_ENABLE \
176 | USB_PORT_STAT_C_SUSPEND \
177 | USB_PORT_STAT_C_OVERCURRENT \
178 | USB_PORT_STAT_C_RESET) << 16)
181 * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
182 * suspend/resume support. But, it is modified to provide multiple ports.
184 * @buf: a bitmap to show which port status has been changed.
185 * bit 0: reserved or used for another purpose?
186 * bit 1: the status of port 0 has been changed.
187 * bit 2: the status of port 1 has been changed.
188 * ...
189 * bit 7: the status of port 6 has been changed.
190 * bit 8: the status of port 7 has been changed.
191 * ...
192 * bit 15: the status of port 14 has been changed.
194 * So, the maximum number of ports is 31 ( port 0 to port 30) ?
196 * The return value is the actual transferred length in byte. If nothing has
197 * been changed, return 0. In the case that the number of ports is less than or
198 * equal to 6 (VHCI_NPORTS==7), return 1.
201 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
203 struct vhci_hcd *vhci;
204 unsigned long flags;
205 int retval = 0;
207 /* the enough buffer is allocated according to USB_MAXCHILDREN */
208 unsigned long *event_bits = (unsigned long *) buf;
209 int rhport;
210 int changed = 0;
212 *event_bits = 0;
214 vhci = hcd_to_vhci(hcd);
216 spin_lock_irqsave(&vhci->lock, flags);
217 if (!HCD_HW_ACCESSIBLE(hcd)) {
218 usbip_dbg_vhci_rh("hw accessible flag in on?\n");
219 goto done;
222 /* check pseudo status register for each port */
223 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
224 if ((vhci->port_status[rhport] & PORT_C_MASK)) {
225 /* The status of a port has been changed, */
226 usbip_dbg_vhci_rh("port %d is changed\n", rhport);
228 *event_bits |= 1 << (rhport + 1);
229 changed = 1;
233 pr_info("changed %d\n", changed);
235 if (hcd->state == HC_STATE_SUSPENDED)
236 usb_hcd_resume_root_hub(hcd);
238 if (changed)
239 retval = 1 + (VHCI_NPORTS / 8);
240 else
241 retval = 0;
243 done:
244 spin_unlock_irqrestore(&vhci->lock, flags);
245 return retval;
248 /* See hub_configure in hub.c */
249 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
251 memset(desc, 0, sizeof(*desc));
252 desc->bDescriptorType = 0x29;
253 desc->bDescLength = 9;
254 desc->wHubCharacteristics = (__force __u16)
255 (__constant_cpu_to_le16(0x0001));
256 desc->bNbrPorts = VHCI_NPORTS;
257 desc->u.hs.DeviceRemovable[0] = 0xff;
258 desc->u.hs.DeviceRemovable[1] = 0xff;
261 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
262 u16 wIndex, char *buf, u16 wLength)
264 struct vhci_hcd *dum;
265 int retval = 0;
266 unsigned long flags;
267 int rhport;
269 u32 prev_port_status[VHCI_NPORTS];
271 if (!HCD_HW_ACCESSIBLE(hcd))
272 return -ETIMEDOUT;
275 * NOTE:
276 * wIndex shows the port number and begins from 1.
278 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
279 wIndex);
280 if (wIndex > VHCI_NPORTS)
281 pr_err("invalid port number %d\n", wIndex);
282 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
284 dum = hcd_to_vhci(hcd);
286 spin_lock_irqsave(&dum->lock, flags);
288 /* store old status and compare now and old later */
289 if (usbip_dbg_flag_vhci_rh) {
290 memcpy(prev_port_status, dum->port_status,
291 sizeof(prev_port_status));
294 switch (typeReq) {
295 case ClearHubFeature:
296 usbip_dbg_vhci_rh(" ClearHubFeature\n");
297 break;
298 case ClearPortFeature:
299 switch (wValue) {
300 case USB_PORT_FEAT_SUSPEND:
301 if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
302 /* 20msec signaling */
303 dum->resuming = 1;
304 dum->re_timeout =
305 jiffies + msecs_to_jiffies(20);
307 break;
308 case USB_PORT_FEAT_POWER:
309 usbip_dbg_vhci_rh(" ClearPortFeature: "
310 "USB_PORT_FEAT_POWER\n");
311 dum->port_status[rhport] = 0;
312 /* dum->address = 0; */
313 /* dum->hdev = 0; */
314 dum->resuming = 0;
315 break;
316 case USB_PORT_FEAT_C_RESET:
317 usbip_dbg_vhci_rh(" ClearPortFeature: "
318 "USB_PORT_FEAT_C_RESET\n");
319 switch (dum->vdev[rhport].speed) {
320 case USB_SPEED_HIGH:
321 dum->port_status[rhport] |=
322 USB_PORT_STAT_HIGH_SPEED;
323 break;
324 case USB_SPEED_LOW:
325 dum->port_status[rhport] |=
326 USB_PORT_STAT_LOW_SPEED;
327 break;
328 default:
329 break;
331 default:
332 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
333 wValue);
334 dum->port_status[rhport] &= ~(1 << wValue);
335 break;
337 break;
338 case GetHubDescriptor:
339 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
340 hub_descriptor((struct usb_hub_descriptor *) buf);
341 break;
342 case GetHubStatus:
343 usbip_dbg_vhci_rh(" GetHubStatus\n");
344 *(__le32 *) buf = __constant_cpu_to_le32(0);
345 break;
346 case GetPortStatus:
347 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
348 if (wIndex > VHCI_NPORTS || wIndex < 1) {
349 pr_err("invalid port number %d\n", wIndex);
350 retval = -EPIPE;
353 /* we do no care of resume. */
355 /* whoever resets or resumes must GetPortStatus to
356 * complete it!!
357 * */
358 if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
359 dum->port_status[rhport] |=
360 (1 << USB_PORT_FEAT_C_SUSPEND);
361 dum->port_status[rhport] &=
362 ~(1 << USB_PORT_FEAT_SUSPEND);
363 dum->resuming = 0;
364 dum->re_timeout = 0;
365 /* if (dum->driver && dum->driver->resume) {
366 * spin_unlock (&dum->lock);
367 * dum->driver->resume (&dum->gadget);
368 * spin_lock (&dum->lock);
369 * } */
372 if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
373 0 && time_after(jiffies, dum->re_timeout)) {
374 dum->port_status[rhport] |=
375 (1 << USB_PORT_FEAT_C_RESET);
376 dum->port_status[rhport] &=
377 ~(1 << USB_PORT_FEAT_RESET);
378 dum->re_timeout = 0;
380 if (dum->vdev[rhport].ud.status ==
381 VDEV_ST_NOTASSIGNED) {
382 usbip_dbg_vhci_rh(" enable rhport %d "
383 "(status %u)\n",
384 rhport,
385 dum->vdev[rhport].ud.status);
386 dum->port_status[rhport] |=
387 USB_PORT_STAT_ENABLE;
389 #if 0
390 if (dum->driver) {
391 dum->port_status[rhport] |=
392 USB_PORT_STAT_ENABLE;
393 /* give it the best speed we agree on */
394 dum->gadget.speed = dum->driver->speed;
395 dum->gadget.ep0->maxpacket = 64;
396 switch (dum->gadget.speed) {
397 case USB_SPEED_HIGH:
398 dum->port_status[rhport] |=
399 USB_PORT_STAT_HIGH_SPEED;
400 break;
401 case USB_SPEED_LOW:
402 dum->gadget.ep0->maxpacket = 8;
403 dum->port_status[rhport] |=
404 USB_PORT_STAT_LOW_SPEED;
405 break;
406 default:
407 dum->gadget.speed = USB_SPEED_FULL;
408 break;
411 #endif
413 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
414 ((u16 *) buf)[1] = cpu_to_le16(dum->port_status[rhport] >> 16);
416 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
417 ((u16 *)buf)[1]);
418 break;
419 case SetHubFeature:
420 usbip_dbg_vhci_rh(" SetHubFeature\n");
421 retval = -EPIPE;
422 break;
423 case SetPortFeature:
424 switch (wValue) {
425 case USB_PORT_FEAT_SUSPEND:
426 usbip_dbg_vhci_rh(" SetPortFeature: "
427 "USB_PORT_FEAT_SUSPEND\n");
428 #if 0
429 dum->port_status[rhport] |=
430 (1 << USB_PORT_FEAT_SUSPEND);
431 if (dum->driver->suspend) {
432 spin_unlock(&dum->lock);
433 dum->driver->suspend(&dum->gadget);
434 spin_lock(&dum->lock);
436 #endif
437 break;
438 case USB_PORT_FEAT_RESET:
439 usbip_dbg_vhci_rh(" SetPortFeature: "
440 "USB_PORT_FEAT_RESET\n");
441 /* if it's already running, disconnect first */
442 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
443 dum->port_status[rhport] &=
444 ~(USB_PORT_STAT_ENABLE |
445 USB_PORT_STAT_LOW_SPEED |
446 USB_PORT_STAT_HIGH_SPEED);
447 #if 0
448 if (dum->driver) {
449 dev_dbg(hardware, "disconnect\n");
450 stop_activity(dum, dum->driver);
452 #endif
454 /* FIXME test that code path! */
456 /* 50msec reset signaling */
457 dum->re_timeout = jiffies + msecs_to_jiffies(50);
459 /* FALLTHROUGH */
460 default:
461 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
462 wValue);
463 dum->port_status[rhport] |= (1 << wValue);
464 break;
466 break;
468 default:
469 pr_err("default: no such request\n");
470 /* dev_dbg (hardware,
471 * "hub control req%04x v%04x i%04x l%d\n",
472 * typeReq, wValue, wIndex, wLength); */
474 /* "protocol stall" on error */
475 retval = -EPIPE;
478 if (usbip_dbg_flag_vhci_rh) {
479 pr_debug("port %d\n", rhport);
480 /* Only dump valid port status */
481 if (rhport >= 0) {
482 dump_port_status_diff(prev_port_status[rhport],
483 dum->port_status[rhport]);
486 usbip_dbg_vhci_rh(" bye\n");
488 spin_unlock_irqrestore(&dum->lock, flags);
490 return retval;
493 static struct vhci_device *get_vdev(struct usb_device *udev)
495 int i;
497 if (!udev)
498 return NULL;
500 for (i = 0; i < VHCI_NPORTS; i++)
501 if (the_controller->vdev[i].udev == udev)
502 return port_to_vdev(i);
504 return NULL;
507 static void vhci_tx_urb(struct urb *urb)
509 struct vhci_device *vdev = get_vdev(urb->dev);
510 struct vhci_priv *priv;
511 unsigned long flag;
513 if (!vdev) {
514 pr_err("could not get virtual device");
515 /* BUG(); */
516 return;
519 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
521 spin_lock_irqsave(&vdev->priv_lock, flag);
523 if (!priv) {
524 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
525 spin_unlock_irqrestore(&vdev->priv_lock, flag);
526 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
527 return;
530 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
531 if (priv->seqnum == 0xffff)
532 dev_info(&urb->dev->dev, "seqnum max\n");
534 priv->vdev = vdev;
535 priv->urb = urb;
537 urb->hcpriv = (void *) priv;
539 list_add_tail(&priv->list, &vdev->priv_tx);
541 wake_up(&vdev->waitq_tx);
542 spin_unlock_irqrestore(&vdev->priv_lock, flag);
545 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
546 gfp_t mem_flags)
548 struct device *dev = &urb->dev->dev;
549 int ret = 0;
550 unsigned long flags;
551 struct vhci_device *vdev;
553 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
554 hcd, urb, mem_flags);
556 /* patch to usb_sg_init() is in 2.5.60 */
557 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
559 spin_lock_irqsave(&the_controller->lock, flags);
561 if (urb->status != -EINPROGRESS) {
562 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
563 spin_unlock_irqrestore(&the_controller->lock, flags);
564 return urb->status;
567 vdev = port_to_vdev(urb->dev->portnum-1);
569 /* refuse enqueue for dead connection */
570 spin_lock(&vdev->ud.lock);
571 if (vdev->ud.status == VDEV_ST_NULL ||
572 vdev->ud.status == VDEV_ST_ERROR) {
573 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
574 spin_unlock(&vdev->ud.lock);
575 spin_unlock_irqrestore(&the_controller->lock, flags);
576 return -ENODEV;
578 spin_unlock(&vdev->ud.lock);
580 ret = usb_hcd_link_urb_to_ep(hcd, urb);
581 if (ret)
582 goto no_need_unlink;
585 * The enumeration process is as follows;
587 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
588 * to get max packet length of default pipe
590 * 2. Set_Address request to DevAddr(0) EndPoint(0)
593 if (usb_pipedevice(urb->pipe) == 0) {
594 __u8 type = usb_pipetype(urb->pipe);
595 struct usb_ctrlrequest *ctrlreq =
596 (struct usb_ctrlrequest *) urb->setup_packet;
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);
650 spin_unlock_irqrestore(&the_controller->lock, flags);
652 return 0;
654 no_need_xmit:
655 usb_hcd_unlink_urb_from_ep(hcd, urb);
656 no_need_unlink:
657 spin_unlock_irqrestore(&the_controller->lock, flags);
658 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
659 return ret;
663 * vhci_rx gives back the urb after receiving the reply of the urb. If an
664 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
665 * back its urb. For the driver unlinking the urb, the content of the urb is
666 * not important, but the calling to its completion handler is important; the
667 * completion of unlinking is notified by the completion handler.
670 * CLIENT SIDE
672 * - When vhci_hcd receives RET_SUBMIT,
674 * - case 1a). the urb of the pdu is not unlinking.
675 * - normal case
676 * => just give back the urb
678 * - case 1b). the urb of the pdu is unlinking.
679 * - usbip.ko will return a reply of the unlinking request.
680 * => give back the urb now and go to case 2b).
682 * - When vhci_hcd receives RET_UNLINK,
684 * - case 2a). a submit request is still pending in vhci_hcd.
685 * - urb was really pending in usbip.ko and urb_unlink_urb() was
686 * completed there.
687 * => free a pending submit request
688 * => notify unlink completeness by giving back the urb
690 * - case 2b). a submit request is *not* pending in vhci_hcd.
691 * - urb was already given back to the core driver.
692 * => do not give back the urb
695 * SERVER SIDE
697 * - When usbip receives CMD_UNLINK,
699 * - case 3a). the urb of the unlink request is now in submission.
700 * => do usb_unlink_urb().
701 * => after the unlink is completed, send RET_UNLINK.
703 * - case 3b). the urb of the unlink request is not in submission.
704 * - may be already completed or never be received
705 * => send RET_UNLINK
708 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
710 unsigned long flags;
711 struct vhci_priv *priv;
712 struct vhci_device *vdev;
714 pr_info("dequeue a urb %p\n", urb);
716 spin_lock_irqsave(&the_controller->lock, flags);
718 priv = urb->hcpriv;
719 if (!priv) {
720 /* URB was never linked! or will be soon given back by
721 * vhci_rx. */
722 spin_unlock_irqrestore(&the_controller->lock, flags);
723 return 0;
727 int ret = 0;
728 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
729 if (ret) {
730 spin_unlock_irqrestore(&the_controller->lock, flags);
731 return ret;
735 /* send unlink request here? */
736 vdev = priv->vdev;
738 if (!vdev->ud.tcp_socket) {
739 /* tcp connection is closed */
740 unsigned long flags2;
742 spin_lock_irqsave(&vdev->priv_lock, flags2);
744 pr_info("device %p seems to be disconnected\n", vdev);
745 list_del(&priv->list);
746 kfree(priv);
747 urb->hcpriv = NULL;
749 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
752 * If tcp connection is alive, we have sent CMD_UNLINK.
753 * vhci_rx will receive RET_UNLINK and give back the URB.
754 * Otherwise, we give back it here.
756 pr_info("gives back urb %p\n", urb);
758 usb_hcd_unlink_urb_from_ep(hcd, urb);
760 spin_unlock_irqrestore(&the_controller->lock, flags);
761 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
762 urb->status);
763 spin_lock_irqsave(&the_controller->lock, flags);
765 } else {
766 /* tcp connection is alive */
767 unsigned long flags2;
768 struct vhci_unlink *unlink;
770 spin_lock_irqsave(&vdev->priv_lock, flags2);
772 /* setup CMD_UNLINK pdu */
773 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
774 if (!unlink) {
775 pr_err("malloc vhci_unlink\n");
776 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
777 spin_unlock_irqrestore(&the_controller->lock, flags);
778 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
779 return -ENOMEM;
782 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
783 if (unlink->seqnum == 0xffff)
784 pr_info("seqnum max\n");
786 unlink->unlink_seqnum = priv->seqnum;
788 pr_info("device %p seems to be still connected\n", vdev);
790 /* send cmd_unlink and try to cancel the pending URB in the
791 * peer */
792 list_add_tail(&unlink->list, &vdev->unlink_tx);
793 wake_up(&vdev->waitq_tx);
795 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
798 spin_unlock_irqrestore(&the_controller->lock, flags);
800 usbip_dbg_vhci_hc("leave\n");
801 return 0;
804 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
806 struct vhci_unlink *unlink, *tmp;
808 spin_lock(&vdev->priv_lock);
810 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
811 pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
812 list_del(&unlink->list);
813 kfree(unlink);
816 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
817 struct urb *urb;
819 /* give back URB of unanswered unlink request */
820 pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
822 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
823 if (!urb) {
824 pr_info("the urb (seqnum %lu) was already given back\n",
825 unlink->unlink_seqnum);
826 list_del(&unlink->list);
827 kfree(unlink);
828 continue;
831 urb->status = -ENODEV;
833 spin_lock(&the_controller->lock);
834 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
835 spin_unlock(&the_controller->lock);
837 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
838 urb->status);
840 list_del(&unlink->list);
841 kfree(unlink);
844 spin_unlock(&vdev->priv_lock);
848 * The important thing is that only one context begins cleanup.
849 * This is why error handling and cleanup become simple.
850 * We do not want to consider race condition as possible.
852 static void vhci_shutdown_connection(struct usbip_device *ud)
854 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
856 /* need this? see stub_dev.c */
857 if (ud->tcp_socket) {
858 pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
859 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
862 /* kill threads related to this sdev, if v.c. exists */
863 if (vdev->ud.tcp_rx && !task_is_dead(vdev->ud.tcp_rx))
864 kthread_stop(vdev->ud.tcp_rx);
865 if (vdev->ud.tcp_tx && !task_is_dead(vdev->ud.tcp_tx))
866 kthread_stop(vdev->ud.tcp_tx);
868 pr_info("stop threads\n");
870 /* active connection is closed */
871 if (vdev->ud.tcp_socket != NULL) {
872 sock_release(vdev->ud.tcp_socket);
873 vdev->ud.tcp_socket = NULL;
875 pr_info("release socket\n");
877 vhci_device_unlink_cleanup(vdev);
880 * rh_port_disconnect() is a trigger of ...
881 * usb_disable_device():
882 * disable all the endpoints for a USB device.
883 * usb_disable_endpoint():
884 * disable endpoints. pending urbs are unlinked(dequeued).
886 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
887 * deteched device should release used urbs in a cleanup function(i.e.
888 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
889 * pushed urbs and their private data in this function.
891 * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
892 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
893 * gives back pushed urbs and frees their private data by request of
894 * the cleanup function of a USB driver. When unlinking a urb with an
895 * active connection, vhci_dequeue() does not give back the urb which
896 * is actually given back by vhci_rx after receiving its return pdu.
899 rh_port_disconnect(vdev->rhport);
901 pr_info("disconnect device\n");
905 static void vhci_device_reset(struct usbip_device *ud)
907 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
909 spin_lock(&ud->lock);
911 vdev->speed = 0;
912 vdev->devid = 0;
914 if (vdev->udev)
915 usb_put_dev(vdev->udev);
916 vdev->udev = NULL;
918 ud->tcp_socket = NULL;
919 ud->status = VDEV_ST_NULL;
921 spin_unlock(&ud->lock);
924 static void vhci_device_unusable(struct usbip_device *ud)
926 spin_lock(&ud->lock);
927 ud->status = VDEV_ST_ERROR;
928 spin_unlock(&ud->lock);
931 static void vhci_device_init(struct vhci_device *vdev)
933 memset(vdev, 0, sizeof(*vdev));
935 vdev->ud.side = USBIP_VHCI;
936 vdev->ud.status = VDEV_ST_NULL;
937 /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
938 spin_lock_init(&vdev->ud.lock);
940 INIT_LIST_HEAD(&vdev->priv_rx);
941 INIT_LIST_HEAD(&vdev->priv_tx);
942 INIT_LIST_HEAD(&vdev->unlink_tx);
943 INIT_LIST_HEAD(&vdev->unlink_rx);
944 /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
945 spin_lock_init(&vdev->priv_lock);
947 init_waitqueue_head(&vdev->waitq_tx);
949 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
950 vdev->ud.eh_ops.reset = vhci_device_reset;
951 vdev->ud.eh_ops.unusable = vhci_device_unusable;
953 usbip_start_eh(&vdev->ud);
956 static int vhci_start(struct usb_hcd *hcd)
958 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
959 int rhport;
960 int err = 0;
962 usbip_dbg_vhci_hc("enter vhci_start\n");
964 /* initialize private data of usb_hcd */
966 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
967 struct vhci_device *vdev = &vhci->vdev[rhport];
968 vhci_device_init(vdev);
969 vdev->rhport = rhport;
972 atomic_set(&vhci->seqnum, 0);
973 spin_lock_init(&vhci->lock);
975 hcd->power_budget = 0; /* no limit */
976 hcd->state = HC_STATE_RUNNING;
977 hcd->uses_new_polling = 1;
979 /* vhci_hcd is now ready to be controlled through sysfs */
980 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
981 if (err) {
982 pr_err("create sysfs files\n");
983 return err;
986 return 0;
989 static void vhci_stop(struct usb_hcd *hcd)
991 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
992 int rhport = 0;
994 usbip_dbg_vhci_hc("stop VHCI controller\n");
996 /* 1. remove the userland interface of vhci_hcd */
997 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
999 /* 2. shutdown all the ports of vhci_hcd */
1000 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
1001 struct vhci_device *vdev = &vhci->vdev[rhport];
1003 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1004 usbip_stop_eh(&vdev->ud);
1008 static int vhci_get_frame_number(struct usb_hcd *hcd)
1010 pr_err("Not yet implemented\n");
1011 return 0;
1014 #ifdef CONFIG_PM
1016 /* FIXME: suspend/resume */
1017 static int vhci_bus_suspend(struct usb_hcd *hcd)
1019 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1021 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1023 spin_lock_irq(&vhci->lock);
1024 /* vhci->rh_state = DUMMY_RH_SUSPENDED;
1025 * set_link_state(vhci); */
1026 hcd->state = HC_STATE_SUSPENDED;
1027 spin_unlock_irq(&vhci->lock);
1029 return 0;
1032 static int vhci_bus_resume(struct usb_hcd *hcd)
1034 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1035 int rc = 0;
1037 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1039 spin_lock_irq(&vhci->lock);
1040 if (!HCD_HW_ACCESSIBLE(hcd)) {
1041 rc = -ESHUTDOWN;
1042 } else {
1043 /* vhci->rh_state = DUMMY_RH_RUNNING;
1044 * set_link_state(vhci);
1045 * if (!list_empty(&vhci->urbp_list))
1046 * mod_timer(&vhci->timer, jiffies); */
1047 hcd->state = HC_STATE_RUNNING;
1049 spin_unlock_irq(&vhci->lock);
1051 return rc;
1054 #else
1056 #define vhci_bus_suspend NULL
1057 #define vhci_bus_resume NULL
1058 #endif
1060 static struct hc_driver vhci_hc_driver = {
1061 .description = driver_name,
1062 .product_desc = driver_desc,
1063 .hcd_priv_size = sizeof(struct vhci_hcd),
1065 .flags = HCD_USB2,
1067 .start = vhci_start,
1068 .stop = vhci_stop,
1070 .urb_enqueue = vhci_urb_enqueue,
1071 .urb_dequeue = vhci_urb_dequeue,
1073 .get_frame_number = vhci_get_frame_number,
1075 .hub_status_data = vhci_hub_status,
1076 .hub_control = vhci_hub_control,
1077 .bus_suspend = vhci_bus_suspend,
1078 .bus_resume = vhci_bus_resume,
1081 static int vhci_hcd_probe(struct platform_device *pdev)
1083 struct usb_hcd *hcd;
1084 int ret;
1086 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1088 /* will be removed */
1089 if (pdev->dev.dma_mask) {
1090 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1091 return -EINVAL;
1095 * Allocate and initialize hcd.
1096 * Our private data is also allocated automatically.
1098 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1099 if (!hcd) {
1100 pr_err("create hcd failed\n");
1101 return -ENOMEM;
1103 hcd->has_tt = 1;
1105 /* this is private data for vhci_hcd */
1106 the_controller = hcd_to_vhci(hcd);
1109 * Finish generic HCD structure initialization and register.
1110 * Call the driver's reset() and start() routines.
1112 ret = usb_add_hcd(hcd, 0, 0);
1113 if (ret != 0) {
1114 pr_err("usb_add_hcd failed %d\n", ret);
1115 usb_put_hcd(hcd);
1116 the_controller = NULL;
1117 return ret;
1120 usbip_dbg_vhci_hc("bye\n");
1121 return 0;
1124 static int vhci_hcd_remove(struct platform_device *pdev)
1126 struct usb_hcd *hcd;
1128 hcd = platform_get_drvdata(pdev);
1129 if (!hcd)
1130 return 0;
1133 * Disconnects the root hub,
1134 * then reverses the effects of usb_add_hcd(),
1135 * invoking the HCD's stop() methods.
1137 usb_remove_hcd(hcd);
1138 usb_put_hcd(hcd);
1139 the_controller = NULL;
1141 return 0;
1144 #ifdef CONFIG_PM
1146 /* what should happen for USB/IP under suspend/resume? */
1147 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1149 struct usb_hcd *hcd;
1150 int rhport = 0;
1151 int connected = 0;
1152 int ret = 0;
1154 hcd = platform_get_drvdata(pdev);
1156 spin_lock(&the_controller->lock);
1158 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1159 if (the_controller->port_status[rhport] &
1160 USB_PORT_STAT_CONNECTION)
1161 connected += 1;
1163 spin_unlock(&the_controller->lock);
1165 if (connected > 0) {
1166 dev_info(&pdev->dev, "We have %d active connection%s. Do not "
1167 "suspend.\n", connected, (connected == 1 ? "" : "s"));
1168 ret = -EBUSY;
1169 } else {
1170 dev_info(&pdev->dev, "suspend vhci_hcd");
1171 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1174 return ret;
1177 static int vhci_hcd_resume(struct platform_device *pdev)
1179 struct usb_hcd *hcd;
1181 dev_dbg(&pdev->dev, "%s\n", __func__);
1183 hcd = platform_get_drvdata(pdev);
1184 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1185 usb_hcd_poll_rh_status(hcd);
1187 return 0;
1190 #else
1192 #define vhci_hcd_suspend NULL
1193 #define vhci_hcd_resume NULL
1195 #endif
1197 static struct platform_driver vhci_driver = {
1198 .probe = vhci_hcd_probe,
1199 .remove = __devexit_p(vhci_hcd_remove),
1200 .suspend = vhci_hcd_suspend,
1201 .resume = vhci_hcd_resume,
1202 .driver = {
1203 .name = (char *) driver_name,
1204 .owner = THIS_MODULE,
1209 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1210 * We need to add this virtual device as a platform device arbitrarily:
1211 * 1. platform_device_register()
1213 static void the_pdev_release(struct device *dev)
1215 return;
1218 static struct platform_device the_pdev = {
1219 /* should be the same name as driver_name */
1220 .name = (char *) driver_name,
1221 .id = -1,
1222 .dev = {
1223 /* .driver = &vhci_driver, */
1224 .release = the_pdev_release,
1228 static int __init vhci_hcd_init(void)
1230 int ret;
1232 if (usb_disabled())
1233 return -ENODEV;
1235 ret = platform_driver_register(&vhci_driver);
1236 if (ret < 0)
1237 goto err_driver_register;
1239 ret = platform_device_register(&the_pdev);
1240 if (ret < 0)
1241 goto err_platform_device_register;
1243 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
1244 return ret;
1246 err_platform_device_register:
1247 platform_driver_unregister(&vhci_driver);
1248 err_driver_register:
1249 return ret;
1252 static void __exit vhci_hcd_exit(void)
1254 platform_device_unregister(&the_pdev);
1255 platform_driver_unregister(&vhci_driver);
1258 module_init(vhci_hcd_init);
1259 module_exit(vhci_hcd_exit);
1261 MODULE_AUTHOR(DRIVER_AUTHOR);
1262 MODULE_DESCRIPTION(DRIVER_DESC);
1263 MODULE_LICENSE("GPL");
1264 MODULE_VERSION(USBIP_VERSION);