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