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