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