GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / usb / usbip / vhci_hcd.c
1 /*
2  * Copyright (C) 2003-2008 Takahiro Hirofuchi
3  * Copyright (C) 2015-2016 Nobuo Iwata
4  *
5  * This is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18  * USA.
19  */
20
21 #include <linux/init.h>
22 #include <linux/file.h>
23 #include <linux/kernel.h>
24 #include <linux/kthread.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28
29 #include "usbip_common.h"
30 #include "vhci.h"
31
32 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
33 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
34
35 /*
36  * TODO
37  *      - update root hub emulation
38  *      - move the emulation code to userland ?
39  *              porting to other operating systems
40  *              minimize kernel code
41  *      - add suspend/resume code
42  *      - clean up everything
43  */
44
45 /* See usb gadget dummy hcd */
46
47 static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
48 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
49                             u16 wIndex, char *buff, u16 wLength);
50 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
51                             gfp_t mem_flags);
52 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
53 static int vhci_start(struct usb_hcd *vhci_hcd);
54 static void vhci_stop(struct usb_hcd *hcd);
55 static int vhci_get_frame_number(struct usb_hcd *hcd);
56
57 static const char driver_name[] = "vhci_hcd";
58 static const char driver_desc[] = "USB/IP Virtual Host Controller";
59
60 int vhci_num_controllers = VHCI_NR_HCS;
61 struct vhci *vhcis;
62
63 static const char * const bit_desc[] = {
64         "CONNECTION",           /*0*/
65         "ENABLE",               /*1*/
66         "SUSPEND",              /*2*/
67         "OVER_CURRENT",         /*3*/
68         "RESET",                /*4*/
69         "L1",                   /*5*/
70         "R6",                   /*6*/
71         "R7",                   /*7*/
72         "POWER",                /*8*/
73         "LOWSPEED",             /*9*/
74         "HIGHSPEED",            /*10*/
75         "PORT_TEST",            /*11*/
76         "INDICATOR",            /*12*/
77         "R13",                  /*13*/
78         "R14",                  /*14*/
79         "R15",                  /*15*/
80         "C_CONNECTION",         /*16*/
81         "C_ENABLE",             /*17*/
82         "C_SUSPEND",            /*18*/
83         "C_OVER_CURRENT",       /*19*/
84         "C_RESET",              /*20*/
85         "C_L1",                 /*21*/
86         "R22",                  /*22*/
87         "R23",                  /*23*/
88         "R24",                  /*24*/
89         "R25",                  /*25*/
90         "R26",                  /*26*/
91         "R27",                  /*27*/
92         "R28",                  /*28*/
93         "R29",                  /*29*/
94         "R30",                  /*30*/
95         "R31",                  /*31*/
96 };
97
98 static const char * const bit_desc_ss[] = {
99         "CONNECTION",           /*0*/
100         "ENABLE",               /*1*/
101         "SUSPEND",              /*2*/
102         "OVER_CURRENT",         /*3*/
103         "RESET",                /*4*/
104         "L1",                   /*5*/
105         "R6",                   /*6*/
106         "R7",                   /*7*/
107         "R8",                   /*8*/
108         "POWER",                /*9*/
109         "HIGHSPEED",            /*10*/
110         "PORT_TEST",            /*11*/
111         "INDICATOR",            /*12*/
112         "R13",                  /*13*/
113         "R14",                  /*14*/
114         "R15",                  /*15*/
115         "C_CONNECTION",         /*16*/
116         "C_ENABLE",             /*17*/
117         "C_SUSPEND",            /*18*/
118         "C_OVER_CURRENT",       /*19*/
119         "C_RESET",              /*20*/
120         "C_BH_RESET",           /*21*/
121         "C_LINK_STATE",         /*22*/
122         "C_CONFIG_ERROR",       /*23*/
123         "R24",                  /*24*/
124         "R25",                  /*25*/
125         "R26",                  /*26*/
126         "R27",                  /*27*/
127         "R28",                  /*28*/
128         "R29",                  /*29*/
129         "R30",                  /*30*/
130         "R31",                  /*31*/
131 };
132
133 static void dump_port_status_diff(u32 prev_status, u32 new_status, bool usb3)
134 {
135         int i = 0;
136         u32 bit = 1;
137         const char * const *desc = bit_desc;
138
139         if (usb3)
140                 desc = bit_desc_ss;
141
142         pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
143         while (bit) {
144                 u32 prev = prev_status & bit;
145                 u32 new = new_status & bit;
146                 char change;
147
148                 if (!prev && new)
149                         change = '+';
150                 else if (prev && !new)
151                         change = '-';
152                 else
153                         change = ' ';
154
155                 if (prev || new) {
156                         pr_debug(" %c%s\n", change, desc[i]);
157
158                         if (bit == 1) /* USB_PORT_STAT_CONNECTION */
159                                 pr_debug(" %c%s\n", change, "USB_PORT_STAT_SPEED_5GBPS");
160                 }
161                 bit <<= 1;
162                 i++;
163         }
164         pr_debug("\n");
165 }
166
167 void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed)
168 {
169         struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
170         struct vhci *vhci = vhci_hcd->vhci;
171         int             rhport = vdev->rhport;
172         u32             status;
173         unsigned long   flags;
174
175         usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
176
177         spin_lock_irqsave(&vhci->lock, flags);
178
179         status = vhci_hcd->port_status[rhport];
180
181         status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION);
182
183         switch (speed) {
184         case USB_SPEED_HIGH:
185                 status |= USB_PORT_STAT_HIGH_SPEED;
186                 break;
187         case USB_SPEED_LOW:
188                 status |= USB_PORT_STAT_LOW_SPEED;
189                 break;
190         default:
191                 break;
192         }
193
194         vhci_hcd->port_status[rhport] = status;
195
196         spin_unlock_irqrestore(&vhci->lock, flags);
197
198         usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
199 }
200
201 static void rh_port_disconnect(struct vhci_device *vdev)
202 {
203         struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
204         struct vhci *vhci = vhci_hcd->vhci;
205         int             rhport = vdev->rhport;
206         u32             status;
207         unsigned long   flags;
208
209         usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
210
211         spin_lock_irqsave(&vhci->lock, flags);
212
213         status = vhci_hcd->port_status[rhport];
214
215         status &= ~USB_PORT_STAT_CONNECTION;
216         status |= (1 << USB_PORT_FEAT_C_CONNECTION);
217
218         vhci_hcd->port_status[rhport] = status;
219
220         spin_unlock_irqrestore(&vhci->lock, flags);
221         usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
222 }
223
224 #define PORT_C_MASK                             \
225         ((USB_PORT_STAT_C_CONNECTION            \
226           | USB_PORT_STAT_C_ENABLE              \
227           | USB_PORT_STAT_C_SUSPEND             \
228           | USB_PORT_STAT_C_OVERCURRENT         \
229           | USB_PORT_STAT_C_RESET) << 16)
230
231 /*
232  * Returns 0 if the status hasn't changed, or the number of bytes in buf.
233  * Ports are 0-indexed from the HCD point of view,
234  * and 1-indexed from the USB core pointer of view.
235  *
236  * @buf: a bitmap to show which port status has been changed.
237  *  bit  0: reserved
238  *  bit  1: the status of port 0 has been changed.
239  *  bit  2: the status of port 1 has been changed.
240  *  ...
241  */
242 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
243 {
244         struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
245         struct vhci *vhci = vhci_hcd->vhci;
246         int             retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8);
247         int             rhport;
248         int             changed = 0;
249         unsigned long   flags;
250
251         memset(buf, 0, retval);
252
253         spin_lock_irqsave(&vhci->lock, flags);
254         if (!HCD_HW_ACCESSIBLE(hcd)) {
255                 usbip_dbg_vhci_rh("hw accessible flag not on?\n");
256                 goto done;
257         }
258
259         /* check pseudo status register for each port */
260         for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
261                 if ((vhci_hcd->port_status[rhport] & PORT_C_MASK)) {
262                         /* The status of a port has been changed, */
263                         usbip_dbg_vhci_rh("port %d status changed\n", rhport);
264
265                         buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8;
266                         changed = 1;
267                 }
268         }
269
270         if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
271                 usb_hcd_resume_root_hub(hcd);
272
273 done:
274         spin_unlock_irqrestore(&vhci->lock, flags);
275         return changed ? retval : 0;
276 }
277
278 /* usb 3.0 root hub device descriptor */
279 static struct {
280         struct usb_bos_descriptor bos;
281         struct usb_ss_cap_descriptor ss_cap;
282 } __packed usb3_bos_desc = {
283
284         .bos = {
285                 .bLength                = USB_DT_BOS_SIZE,
286                 .bDescriptorType        = USB_DT_BOS,
287                 .wTotalLength           = cpu_to_le16(sizeof(usb3_bos_desc)),
288                 .bNumDeviceCaps         = 1,
289         },
290         .ss_cap = {
291                 .bLength                = USB_DT_USB_SS_CAP_SIZE,
292                 .bDescriptorType        = USB_DT_DEVICE_CAPABILITY,
293                 .bDevCapabilityType     = USB_SS_CAP_TYPE,
294                 .wSpeedSupported        = cpu_to_le16(USB_5GBPS_OPERATION),
295                 .bFunctionalitySupport  = ilog2(USB_5GBPS_OPERATION),
296         },
297 };
298
299 static inline void
300 ss_hub_descriptor(struct usb_hub_descriptor *desc)
301 {
302         memset(desc, 0, sizeof *desc);
303         desc->bDescriptorType = USB_DT_SS_HUB;
304         desc->bDescLength = 12;
305         desc->wHubCharacteristics = cpu_to_le16(
306                 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
307         desc->bNbrPorts = VHCI_HC_PORTS;
308         desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
309         desc->u.ss.DeviceRemovable = 0xffff;
310 }
311
312 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
313 {
314         int width;
315
316         memset(desc, 0, sizeof(*desc));
317         desc->bDescriptorType = USB_DT_HUB;
318         desc->wHubCharacteristics = cpu_to_le16(
319                 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
320
321         desc->bNbrPorts = VHCI_HC_PORTS;
322         BUILD_BUG_ON(VHCI_HC_PORTS > USB_MAXCHILDREN);
323         width = desc->bNbrPorts / 8 + 1;
324         desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * width;
325         memset(&desc->u.hs.DeviceRemovable[0], 0, width);
326         memset(&desc->u.hs.DeviceRemovable[width], 0xff, width);
327 }
328
329 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
330                             u16 wIndex, char *buf, u16 wLength)
331 {
332         struct vhci_hcd *vhci_hcd;
333         struct vhci     *vhci;
334         int             retval = 0;
335         int             rhport = -1;
336         unsigned long   flags;
337         bool invalid_rhport = false;
338
339         u32 prev_port_status[VHCI_HC_PORTS];
340
341         if (!HCD_HW_ACCESSIBLE(hcd))
342                 return -ETIMEDOUT;
343
344         /*
345          * NOTE:
346          * wIndex (bits 0-7) shows the port number and begins from 1?
347          */
348         wIndex = ((__u8)(wIndex & 0x00ff));
349         usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
350                           wIndex);
351
352         /*
353          * wIndex can be 0 for some request types (typeReq). rhport is
354          * in valid range when wIndex >= 1 and < VHCI_HC_PORTS.
355          *
356          * Reference port_status[] only with valid rhport when
357          * invalid_rhport is false.
358          */
359         if (wIndex < 1 || wIndex > VHCI_HC_PORTS) {
360                 invalid_rhport = true;
361                 if (wIndex > VHCI_HC_PORTS)
362                         pr_err("invalid port number %d\n", wIndex);
363         } else
364                 rhport = wIndex - 1;
365
366         vhci_hcd = hcd_to_vhci_hcd(hcd);
367         vhci = vhci_hcd->vhci;
368
369         spin_lock_irqsave(&vhci->lock, flags);
370
371         /* store old status and compare now and old later */
372         if (usbip_dbg_flag_vhci_rh) {
373                 if (!invalid_rhport)
374                         memcpy(prev_port_status, vhci_hcd->port_status,
375                                 sizeof(prev_port_status));
376         }
377
378         switch (typeReq) {
379         case ClearHubFeature:
380                 usbip_dbg_vhci_rh(" ClearHubFeature\n");
381                 break;
382         case ClearPortFeature:
383                 if (invalid_rhport) {
384                         pr_err("invalid port number %d\n", wIndex);
385                         goto error;
386                 }
387                 switch (wValue) {
388                 case USB_PORT_FEAT_SUSPEND:
389                         if (hcd->speed == HCD_USB3) {
390                                 pr_err(" ClearPortFeature: USB_PORT_FEAT_SUSPEND req not "
391                                        "supported for USB 3.0 roothub\n");
392                                 goto error;
393                         }
394                         usbip_dbg_vhci_rh(
395                                 " ClearPortFeature: USB_PORT_FEAT_SUSPEND\n");
396                         if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
397                                 /* 20msec signaling */
398                                 vhci_hcd->resuming = 1;
399                                 vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
400                         }
401                         break;
402                 case USB_PORT_FEAT_POWER:
403                         usbip_dbg_vhci_rh(
404                                 " ClearPortFeature: USB_PORT_FEAT_POWER\n");
405                         if (hcd->speed == HCD_USB3)
406                                 vhci_hcd->port_status[rhport] &= ~USB_SS_PORT_STAT_POWER;
407                         else
408                                 vhci_hcd->port_status[rhport] &= ~USB_PORT_STAT_POWER;
409                         break;
410                 default:
411                         usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
412                                           wValue);
413                         if (wValue >= 32)
414                                 goto error;
415                         vhci_hcd->port_status[rhport] &= ~(1 << wValue);
416                         break;
417                 }
418                 break;
419         case GetHubDescriptor:
420                 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
421                 if (hcd->speed == HCD_USB3 &&
422                                 (wLength < USB_DT_SS_HUB_SIZE ||
423                                  wValue != (USB_DT_SS_HUB << 8))) {
424                         pr_err("Wrong hub descriptor type for USB 3.0 roothub.\n");
425                         goto error;
426                 }
427                 if (hcd->speed == HCD_USB3)
428                         ss_hub_descriptor((struct usb_hub_descriptor *) buf);
429                 else
430                         hub_descriptor((struct usb_hub_descriptor *) buf);
431                 break;
432         case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
433                 if (hcd->speed != HCD_USB3)
434                         goto error;
435
436                 if ((wValue >> 8) != USB_DT_BOS)
437                         goto error;
438
439                 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
440                 retval = sizeof(usb3_bos_desc);
441                 break;
442         case GetHubStatus:
443                 usbip_dbg_vhci_rh(" GetHubStatus\n");
444                 *(__le32 *) buf = cpu_to_le32(0);
445                 break;
446         case GetPortStatus:
447                 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
448                 if (invalid_rhport) {
449                         pr_err("invalid port number %d\n", wIndex);
450                         retval = -EPIPE;
451                         goto error;
452                 }
453
454                 /* we do not care about resume. */
455
456                 /* whoever resets or resumes must GetPortStatus to
457                  * complete it!!
458                  */
459                 if (vhci_hcd->resuming && time_after(jiffies, vhci_hcd->re_timeout)) {
460                         vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_SUSPEND);
461                         vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_SUSPEND);
462                         vhci_hcd->resuming = 0;
463                         vhci_hcd->re_timeout = 0;
464                 }
465
466                 if ((vhci_hcd->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
467                     0 && time_after(jiffies, vhci_hcd->re_timeout)) {
468                         vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_RESET);
469                         vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_RESET);
470                         vhci_hcd->re_timeout = 0;
471
472                         /*
473                          * A few drivers do usb reset during probe when
474                          * the device could be in VDEV_ST_USED state
475                          */
476                         if (vhci_hcd->vdev[rhport].ud.status ==
477                                 VDEV_ST_NOTASSIGNED ||
478                             vhci_hcd->vdev[rhport].ud.status ==
479                                 VDEV_ST_USED) {
480                                 usbip_dbg_vhci_rh(
481                                         " enable rhport %d (status %u)\n",
482                                         rhport,
483                                         vhci_hcd->vdev[rhport].ud.status);
484                                 vhci_hcd->port_status[rhport] |=
485                                         USB_PORT_STAT_ENABLE;
486                         }
487
488                         if (hcd->speed < HCD_USB3) {
489                                 switch (vhci_hcd->vdev[rhport].speed) {
490                                 case USB_SPEED_HIGH:
491                                         vhci_hcd->port_status[rhport] |=
492                                               USB_PORT_STAT_HIGH_SPEED;
493                                         break;
494                                 case USB_SPEED_LOW:
495                                         vhci_hcd->port_status[rhport] |=
496                                                 USB_PORT_STAT_LOW_SPEED;
497                                         break;
498                                 default:
499                                         pr_err("vhci_device speed not set\n");
500                                         break;
501                                 }
502                         }
503                 }
504                 ((__le16 *) buf)[0] = cpu_to_le16(vhci_hcd->port_status[rhport]);
505                 ((__le16 *) buf)[1] =
506                         cpu_to_le16(vhci_hcd->port_status[rhport] >> 16);
507
508                 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
509                                   ((u16 *)buf)[1]);
510                 break;
511         case SetHubFeature:
512                 usbip_dbg_vhci_rh(" SetHubFeature\n");
513                 retval = -EPIPE;
514                 break;
515         case SetPortFeature:
516                 switch (wValue) {
517                 case USB_PORT_FEAT_LINK_STATE:
518                         usbip_dbg_vhci_rh(
519                                 " SetPortFeature: USB_PORT_FEAT_LINK_STATE\n");
520                         if (hcd->speed != HCD_USB3) {
521                                 pr_err("USB_PORT_FEAT_LINK_STATE req not "
522                                        "supported for USB 2.0 roothub\n");
523                                 goto error;
524                         }
525                         /*
526                          * Since this is dummy we don't have an actual link so
527                          * there is nothing to do for the SET_LINK_STATE cmd
528                          */
529                         break;
530                 case USB_PORT_FEAT_U1_TIMEOUT:
531                         usbip_dbg_vhci_rh(
532                                 " SetPortFeature: USB_PORT_FEAT_U1_TIMEOUT\n");
533                 case USB_PORT_FEAT_U2_TIMEOUT:
534                         usbip_dbg_vhci_rh(
535                                 " SetPortFeature: USB_PORT_FEAT_U2_TIMEOUT\n");
536                         /* TODO: add suspend/resume support! */
537                         if (hcd->speed != HCD_USB3) {
538                                 pr_err("USB_PORT_FEAT_U1/2_TIMEOUT req not "
539                                        "supported for USB 2.0 roothub\n");
540                                 goto error;
541                         }
542                         break;
543                 case USB_PORT_FEAT_SUSPEND:
544                         usbip_dbg_vhci_rh(
545                                 " SetPortFeature: USB_PORT_FEAT_SUSPEND\n");
546                         /* Applicable only for USB2.0 hub */
547                         if (hcd->speed == HCD_USB3) {
548                                 pr_err("USB_PORT_FEAT_SUSPEND req not "
549                                        "supported for USB 3.0 roothub\n");
550                                 goto error;
551                         }
552
553                         if (invalid_rhport) {
554                                 pr_err("invalid port number %d\n", wIndex);
555                                 goto error;
556                         }
557
558                         vhci_hcd->port_status[rhport] |= USB_PORT_STAT_SUSPEND;
559                         break;
560                 case USB_PORT_FEAT_POWER:
561                         usbip_dbg_vhci_rh(
562                                 " SetPortFeature: USB_PORT_FEAT_POWER\n");
563                         if (invalid_rhport) {
564                                 pr_err("invalid port number %d\n", wIndex);
565                                 goto error;
566                         }
567                         if (hcd->speed == HCD_USB3)
568                                 vhci_hcd->port_status[rhport] |= USB_SS_PORT_STAT_POWER;
569                         else
570                                 vhci_hcd->port_status[rhport] |= USB_PORT_STAT_POWER;
571                         break;
572                 case USB_PORT_FEAT_BH_PORT_RESET:
573                         usbip_dbg_vhci_rh(
574                                 " SetPortFeature: USB_PORT_FEAT_BH_PORT_RESET\n");
575                         if (invalid_rhport) {
576                                 pr_err("invalid port number %d\n", wIndex);
577                                 goto error;
578                         }
579                         /* Applicable only for USB3.0 hub */
580                         if (hcd->speed != HCD_USB3) {
581                                 pr_err("USB_PORT_FEAT_BH_PORT_RESET req not "
582                                        "supported for USB 2.0 roothub\n");
583                                 goto error;
584                         }
585                         /* FALLS THROUGH */
586                 case USB_PORT_FEAT_RESET:
587                         usbip_dbg_vhci_rh(
588                                 " SetPortFeature: USB_PORT_FEAT_RESET\n");
589                         if (invalid_rhport) {
590                                 pr_err("invalid port number %d\n", wIndex);
591                                 goto error;
592                         }
593                         /* if it's already enabled, disable */
594                         if (hcd->speed == HCD_USB3) {
595                                 vhci_hcd->port_status[rhport] = 0;
596                                 vhci_hcd->port_status[rhport] =
597                                         (USB_SS_PORT_STAT_POWER |
598                                          USB_PORT_STAT_CONNECTION |
599                                          USB_PORT_STAT_RESET);
600                         } else if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_ENABLE) {
601                                 vhci_hcd->port_status[rhport] &= ~(USB_PORT_STAT_ENABLE
602                                         | USB_PORT_STAT_LOW_SPEED
603                                         | USB_PORT_STAT_HIGH_SPEED);
604                         }
605
606                         /* 50msec reset signaling */
607                         vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
608
609                         /* FALLS THROUGH */
610                 default:
611                         usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
612                                           wValue);
613                         if (invalid_rhport) {
614                                 pr_err("invalid port number %d\n", wIndex);
615                                 goto error;
616                         }
617                         if (wValue >= 32)
618                                 goto error;
619                         if (hcd->speed == HCD_USB3) {
620                                 if ((vhci_hcd->port_status[rhport] &
621                                      USB_SS_PORT_STAT_POWER) != 0) {
622                                         vhci_hcd->port_status[rhport] |= (1 << wValue);
623                                 }
624                         } else
625                                 if ((vhci_hcd->port_status[rhport] &
626                                      USB_PORT_STAT_POWER) != 0) {
627                                         vhci_hcd->port_status[rhport] |= (1 << wValue);
628                                 }
629                 }
630                 break;
631         case GetPortErrorCount:
632                 usbip_dbg_vhci_rh(" GetPortErrorCount\n");
633                 if (hcd->speed != HCD_USB3) {
634                         pr_err("GetPortErrorCount req not "
635                                "supported for USB 2.0 roothub\n");
636                         goto error;
637                 }
638                 /* We'll always return 0 since this is a dummy hub */
639                 *(__le32 *) buf = cpu_to_le32(0);
640                 break;
641         case SetHubDepth:
642                 usbip_dbg_vhci_rh(" SetHubDepth\n");
643                 if (hcd->speed != HCD_USB3) {
644                         pr_err("SetHubDepth req not supported for "
645                                "USB 2.0 roothub\n");
646                         goto error;
647                 }
648                 break;
649         default:
650                 pr_err("default hub control req: %04x v%04x i%04x l%d\n",
651                         typeReq, wValue, wIndex, wLength);
652 error:
653                 /* "protocol stall" on error */
654                 retval = -EPIPE;
655         }
656
657         if (usbip_dbg_flag_vhci_rh) {
658                 pr_debug("port %d\n", rhport);
659                 /* Only dump valid port status */
660                 if (!invalid_rhport) {
661                         dump_port_status_diff(prev_port_status[rhport],
662                                               vhci_hcd->port_status[rhport],
663                                               hcd->speed == HCD_USB3);
664                 }
665         }
666         usbip_dbg_vhci_rh(" bye\n");
667
668         spin_unlock_irqrestore(&vhci->lock, flags);
669
670         if (!invalid_rhport &&
671             (vhci_hcd->port_status[rhport] & PORT_C_MASK) != 0) {
672                 usb_hcd_poll_rh_status(hcd);
673         }
674
675         return retval;
676 }
677
678 static void vhci_tx_urb(struct urb *urb, struct vhci_device *vdev)
679 {
680         struct vhci_priv *priv;
681         struct vhci_hcd *vhci_hcd;
682         unsigned long flags;
683
684         if (!vdev) {
685                 pr_err("could not get virtual device");
686                 return;
687         }
688         vhci_hcd = vdev_to_vhci_hcd(vdev);
689
690         priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
691         if (!priv) {
692                 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
693                 return;
694         }
695
696         spin_lock_irqsave(&vdev->priv_lock, flags);
697
698         priv->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
699         if (priv->seqnum == 0xffff)
700                 dev_info(&urb->dev->dev, "seqnum max\n");
701
702         priv->vdev = vdev;
703         priv->urb = urb;
704
705         urb->hcpriv = (void *) priv;
706
707         list_add_tail(&priv->list, &vdev->priv_tx);
708
709         wake_up(&vdev->waitq_tx);
710         spin_unlock_irqrestore(&vdev->priv_lock, flags);
711 }
712
713 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
714 {
715         struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
716         struct vhci *vhci = vhci_hcd->vhci;
717         struct device *dev = &urb->dev->dev;
718         u8 portnum = urb->dev->portnum;
719         int ret = 0;
720         struct vhci_device *vdev;
721         unsigned long flags;
722
723         if (portnum > VHCI_HC_PORTS) {
724                 pr_err("invalid port number %d\n", portnum);
725                 return -ENODEV;
726         }
727         vdev = &vhci_hcd->vdev[portnum-1];
728
729         if (!urb->transfer_buffer && !urb->num_sgs &&
730              urb->transfer_buffer_length) {
731                 dev_dbg(dev, "Null URB transfer buffer\n");
732                 return -EINVAL;
733         }
734
735         spin_lock_irqsave(&vhci->lock, flags);
736
737         if (urb->status != -EINPROGRESS) {
738                 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
739                 spin_unlock_irqrestore(&vhci->lock, flags);
740                 return urb->status;
741         }
742
743         /* refuse enqueue for dead connection */
744         spin_lock(&vdev->ud.lock);
745         if (vdev->ud.status == VDEV_ST_NULL ||
746             vdev->ud.status == VDEV_ST_ERROR) {
747                 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
748                 spin_unlock(&vdev->ud.lock);
749                 spin_unlock_irqrestore(&vhci->lock, flags);
750                 return -ENODEV;
751         }
752         spin_unlock(&vdev->ud.lock);
753
754         ret = usb_hcd_link_urb_to_ep(hcd, urb);
755         if (ret)
756                 goto no_need_unlink;
757
758         /*
759          * The enumeration process is as follows;
760          *
761          *  1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
762          *     to get max packet length of default pipe
763          *
764          *  2. Set_Address request to DevAddr(0) EndPoint(0)
765          *
766          */
767         if (usb_pipedevice(urb->pipe) == 0) {
768                 __u8 type = usb_pipetype(urb->pipe);
769                 struct usb_ctrlrequest *ctrlreq =
770                         (struct usb_ctrlrequest *) urb->setup_packet;
771
772                 if (type != PIPE_CONTROL || !ctrlreq) {
773                         dev_err(dev, "invalid request to devnum 0\n");
774                         ret = -EINVAL;
775                         goto no_need_xmit;
776                 }
777
778                 switch (ctrlreq->bRequest) {
779                 case USB_REQ_SET_ADDRESS:
780                         /* set_address may come when a device is reset */
781                         dev_info(dev, "SetAddress Request (%d) to port %d\n",
782                                  ctrlreq->wValue, vdev->rhport);
783
784                         usb_put_dev(vdev->udev);
785                         vdev->udev = usb_get_dev(urb->dev);
786
787                         spin_lock(&vdev->ud.lock);
788                         vdev->ud.status = VDEV_ST_USED;
789                         spin_unlock(&vdev->ud.lock);
790
791                         if (urb->status == -EINPROGRESS) {
792                                 /* This request is successfully completed. */
793                                 /* If not -EINPROGRESS, possibly unlinked. */
794                                 urb->status = 0;
795                         }
796
797                         goto no_need_xmit;
798
799                 case USB_REQ_GET_DESCRIPTOR:
800                         if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
801                                 usbip_dbg_vhci_hc(
802                                         "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
803
804                         usb_put_dev(vdev->udev);
805                         vdev->udev = usb_get_dev(urb->dev);
806                         goto out;
807
808                 default:
809                         /* NOT REACHED */
810                         dev_err(dev,
811                                 "invalid request to devnum 0 bRequest %u, wValue %u\n",
812                                 ctrlreq->bRequest,
813                                 ctrlreq->wValue);
814                         ret =  -EINVAL;
815                         goto no_need_xmit;
816                 }
817
818         }
819
820 out:
821         vhci_tx_urb(urb, vdev);
822         spin_unlock_irqrestore(&vhci->lock, flags);
823
824         return 0;
825
826 no_need_xmit:
827         usb_hcd_unlink_urb_from_ep(hcd, urb);
828 no_need_unlink:
829         spin_unlock_irqrestore(&vhci->lock, flags);
830         if (!ret)
831                 usb_hcd_giveback_urb(hcd, urb, urb->status);
832         return ret;
833 }
834
835 /*
836  * vhci_rx gives back the urb after receiving the reply of the urb.  If an
837  * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
838  * back its urb. For the driver unlinking the urb, the content of the urb is
839  * not important, but the calling to its completion handler is important; the
840  * completion of unlinking is notified by the completion handler.
841  *
842  *
843  * CLIENT SIDE
844  *
845  * - When vhci_hcd receives RET_SUBMIT,
846  *
847  *      - case 1a). the urb of the pdu is not unlinking.
848  *              - normal case
849  *              => just give back the urb
850  *
851  *      - case 1b). the urb of the pdu is unlinking.
852  *              - usbip.ko will return a reply of the unlinking request.
853  *              => give back the urb now and go to case 2b).
854  *
855  * - When vhci_hcd receives RET_UNLINK,
856  *
857  *      - case 2a). a submit request is still pending in vhci_hcd.
858  *              - urb was really pending in usbip.ko and urb_unlink_urb() was
859  *                completed there.
860  *              => free a pending submit request
861  *              => notify unlink completeness by giving back the urb
862  *
863  *      - case 2b). a submit request is *not* pending in vhci_hcd.
864  *              - urb was already given back to the core driver.
865  *              => do not give back the urb
866  *
867  *
868  * SERVER SIDE
869  *
870  * - When usbip receives CMD_UNLINK,
871  *
872  *      - case 3a). the urb of the unlink request is now in submission.
873  *              => do usb_unlink_urb().
874  *              => after the unlink is completed, send RET_UNLINK.
875  *
876  *      - case 3b). the urb of the unlink request is not in submission.
877  *              - may be already completed or never be received
878  *              => send RET_UNLINK
879  *
880  */
881 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
882 {
883         struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
884         struct vhci *vhci = vhci_hcd->vhci;
885         struct vhci_priv *priv;
886         struct vhci_device *vdev;
887         unsigned long flags;
888
889         spin_lock_irqsave(&vhci->lock, flags);
890
891         priv = urb->hcpriv;
892         if (!priv) {
893                 /* URB was never linked! or will be soon given back by
894                  * vhci_rx. */
895                 spin_unlock_irqrestore(&vhci->lock, flags);
896                 return -EIDRM;
897         }
898
899         {
900                 int ret = 0;
901
902                 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
903                 if (ret) {
904                         spin_unlock_irqrestore(&vhci->lock, flags);
905                         return ret;
906                 }
907         }
908
909          /* send unlink request here? */
910         vdev = priv->vdev;
911
912         if (!vdev->ud.tcp_socket) {
913                 /* tcp connection is closed */
914                 spin_lock(&vdev->priv_lock);
915
916                 list_del(&priv->list);
917                 kfree(priv);
918                 urb->hcpriv = NULL;
919
920                 spin_unlock(&vdev->priv_lock);
921
922                 /*
923                  * If tcp connection is alive, we have sent CMD_UNLINK.
924                  * vhci_rx will receive RET_UNLINK and give back the URB.
925                  * Otherwise, we give back it here.
926                  */
927                 usb_hcd_unlink_urb_from_ep(hcd, urb);
928
929                 spin_unlock_irqrestore(&vhci->lock, flags);
930                 usb_hcd_giveback_urb(hcd, urb, urb->status);
931                 spin_lock_irqsave(&vhci->lock, flags);
932
933         } else {
934                 /* tcp connection is alive */
935                 struct vhci_unlink *unlink;
936
937                 spin_lock(&vdev->priv_lock);
938
939                 /* setup CMD_UNLINK pdu */
940                 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
941                 if (!unlink) {
942                         spin_unlock(&vdev->priv_lock);
943                         spin_unlock_irqrestore(&vhci->lock, flags);
944                         usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
945                         return -ENOMEM;
946                 }
947
948                 unlink->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
949                 if (unlink->seqnum == 0xffff)
950                         pr_info("seqnum max\n");
951
952                 unlink->unlink_seqnum = priv->seqnum;
953
954                 /* send cmd_unlink and try to cancel the pending URB in the
955                  * peer */
956                 list_add_tail(&unlink->list, &vdev->unlink_tx);
957                 wake_up(&vdev->waitq_tx);
958
959                 spin_unlock(&vdev->priv_lock);
960         }
961
962         spin_unlock_irqrestore(&vhci->lock, flags);
963
964         usbip_dbg_vhci_hc("leave\n");
965         return 0;
966 }
967
968 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
969 {
970         struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
971         struct usb_hcd *hcd = vhci_hcd_to_hcd(vhci_hcd);
972         struct vhci *vhci = vhci_hcd->vhci;
973         struct vhci_unlink *unlink, *tmp;
974         unsigned long flags;
975
976         spin_lock_irqsave(&vhci->lock, flags);
977         spin_lock(&vdev->priv_lock);
978
979         list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
980                 struct urb *urb;
981
982                 /* give back urb of unsent unlink request */
983                 pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
984
985                 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
986                 if (!urb) {
987                         list_del(&unlink->list);
988                         kfree(unlink);
989                         continue;
990                 }
991
992                 urb->status = -ENODEV;
993
994                 usb_hcd_unlink_urb_from_ep(hcd, urb);
995
996                 list_del(&unlink->list);
997
998                 spin_unlock(&vdev->priv_lock);
999                 spin_unlock_irqrestore(&vhci->lock, flags);
1000
1001                 usb_hcd_giveback_urb(hcd, urb, urb->status);
1002
1003                 spin_lock_irqsave(&vhci->lock, flags);
1004                 spin_lock(&vdev->priv_lock);
1005
1006                 kfree(unlink);
1007         }
1008
1009         while (!list_empty(&vdev->unlink_rx)) {
1010                 struct urb *urb;
1011
1012                 unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
1013                         list);
1014
1015                 /* give back URB of unanswered unlink request */
1016                 pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
1017
1018                 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
1019                 if (!urb) {
1020                         pr_info("the urb (seqnum %lu) was already given back\n",
1021                                 unlink->unlink_seqnum);
1022                         list_del(&unlink->list);
1023                         kfree(unlink);
1024                         continue;
1025                 }
1026
1027                 urb->status = -ENODEV;
1028
1029                 usb_hcd_unlink_urb_from_ep(hcd, urb);
1030
1031                 list_del(&unlink->list);
1032
1033                 spin_unlock(&vdev->priv_lock);
1034                 spin_unlock_irqrestore(&vhci->lock, flags);
1035
1036                 usb_hcd_giveback_urb(hcd, urb, urb->status);
1037
1038                 spin_lock_irqsave(&vhci->lock, flags);
1039                 spin_lock(&vdev->priv_lock);
1040
1041                 kfree(unlink);
1042         }
1043
1044         spin_unlock(&vdev->priv_lock);
1045         spin_unlock_irqrestore(&vhci->lock, flags);
1046 }
1047
1048 /*
1049  * The important thing is that only one context begins cleanup.
1050  * This is why error handling and cleanup become simple.
1051  * We do not want to consider race condition as possible.
1052  */
1053 static void vhci_shutdown_connection(struct usbip_device *ud)
1054 {
1055         struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1056
1057         /* need this? see stub_dev.c */
1058         if (ud->tcp_socket) {
1059                 pr_debug("shutdown tcp_socket %d\n", ud->sockfd);
1060                 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
1061         }
1062
1063         /* kill threads related to this sdev */
1064         if (vdev->ud.tcp_rx) {
1065                 kthread_stop_put(vdev->ud.tcp_rx);
1066                 vdev->ud.tcp_rx = NULL;
1067         }
1068         if (vdev->ud.tcp_tx) {
1069                 kthread_stop_put(vdev->ud.tcp_tx);
1070                 vdev->ud.tcp_tx = NULL;
1071         }
1072         pr_info("stop threads\n");
1073
1074         /* active connection is closed */
1075         if (vdev->ud.tcp_socket) {
1076                 sockfd_put(vdev->ud.tcp_socket);
1077                 vdev->ud.tcp_socket = NULL;
1078                 vdev->ud.sockfd = -1;
1079         }
1080         pr_info("release socket\n");
1081
1082         vhci_device_unlink_cleanup(vdev);
1083
1084         /*
1085          * rh_port_disconnect() is a trigger of ...
1086          *   usb_disable_device():
1087          *      disable all the endpoints for a USB device.
1088          *   usb_disable_endpoint():
1089          *      disable endpoints. pending urbs are unlinked(dequeued).
1090          *
1091          * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
1092          * detached device should release used urbs in a cleanup function (i.e.
1093          * xxx_disconnect()). Therefore, vhci_hcd does not need to release
1094          * pushed urbs and their private data in this function.
1095          *
1096          * NOTE: vhci_dequeue() must be considered carefully. When shutting down
1097          * a connection, vhci_shutdown_connection() expects vhci_dequeue()
1098          * gives back pushed urbs and frees their private data by request of
1099          * the cleanup function of a USB driver. When unlinking a urb with an
1100          * active connection, vhci_dequeue() does not give back the urb which
1101          * is actually given back by vhci_rx after receiving its return pdu.
1102          *
1103          */
1104         rh_port_disconnect(vdev);
1105
1106         pr_info("disconnect device\n");
1107 }
1108
1109 static void vhci_device_reset(struct usbip_device *ud)
1110 {
1111         struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1112         unsigned long flags;
1113
1114         spin_lock_irqsave(&ud->lock, flags);
1115
1116         vdev->speed  = 0;
1117         vdev->devid  = 0;
1118
1119         usb_put_dev(vdev->udev);
1120         vdev->udev = NULL;
1121
1122         if (ud->tcp_socket) {
1123                 sockfd_put(ud->tcp_socket);
1124                 ud->tcp_socket = NULL;
1125                 ud->sockfd = -1;
1126         }
1127         ud->status = VDEV_ST_NULL;
1128
1129         spin_unlock_irqrestore(&ud->lock, flags);
1130 }
1131
1132 static void vhci_device_unusable(struct usbip_device *ud)
1133 {
1134         unsigned long flags;
1135
1136         spin_lock_irqsave(&ud->lock, flags);
1137         ud->status = VDEV_ST_ERROR;
1138         spin_unlock_irqrestore(&ud->lock, flags);
1139 }
1140
1141 static void vhci_device_init(struct vhci_device *vdev)
1142 {
1143         memset(vdev, 0, sizeof(struct vhci_device));
1144
1145         vdev->ud.side   = USBIP_VHCI;
1146         vdev->ud.status = VDEV_ST_NULL;
1147         spin_lock_init(&vdev->ud.lock);
1148         mutex_init(&vdev->ud.sysfs_lock);
1149
1150         INIT_LIST_HEAD(&vdev->priv_rx);
1151         INIT_LIST_HEAD(&vdev->priv_tx);
1152         INIT_LIST_HEAD(&vdev->unlink_tx);
1153         INIT_LIST_HEAD(&vdev->unlink_rx);
1154         spin_lock_init(&vdev->priv_lock);
1155
1156         init_waitqueue_head(&vdev->waitq_tx);
1157
1158         vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
1159         vdev->ud.eh_ops.reset = vhci_device_reset;
1160         vdev->ud.eh_ops.unusable = vhci_device_unusable;
1161
1162         usbip_start_eh(&vdev->ud);
1163 }
1164
1165 static int hcd_name_to_id(const char *name)
1166 {
1167         char *c;
1168         long val;
1169         int ret;
1170
1171         c = strchr(name, '.');
1172         if (c == NULL)
1173                 return 0;
1174
1175         ret = kstrtol(c+1, 10, &val);
1176         if (ret < 0)
1177                 return ret;
1178
1179         return val;
1180 }
1181
1182 static int vhci_setup(struct usb_hcd *hcd)
1183 {
1184         struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1185         if (usb_hcd_is_primary_hcd(hcd)) {
1186                 vhci->vhci_hcd_hs = hcd_to_vhci_hcd(hcd);
1187                 vhci->vhci_hcd_hs->vhci = vhci;
1188                 /*
1189                  * Mark the first roothub as being USB 2.0.
1190                  * The USB 3.0 roothub will be registered later by
1191                  * vhci_hcd_probe()
1192                  */
1193                 hcd->speed = HCD_USB2;
1194                 hcd->self.root_hub->speed = USB_SPEED_HIGH;
1195         } else {
1196                 vhci->vhci_hcd_ss = hcd_to_vhci_hcd(hcd);
1197                 vhci->vhci_hcd_ss->vhci = vhci;
1198                 hcd->speed = HCD_USB3;
1199                 hcd->self.root_hub->speed = USB_SPEED_SUPER;
1200         }
1201
1202         /*
1203          * Support SG.
1204          * sg_tablesize is an arbitrary value to alleviate memory pressure
1205          * on the host.
1206          */
1207         hcd->self.sg_tablesize = 32;
1208         hcd->self.no_sg_constraint = 1;
1209
1210         return 0;
1211 }
1212
1213 static int vhci_start(struct usb_hcd *hcd)
1214 {
1215         struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
1216         int id, rhport;
1217         int err;
1218
1219         usbip_dbg_vhci_hc("enter vhci_start\n");
1220
1221         if (usb_hcd_is_primary_hcd(hcd))
1222                 spin_lock_init(&vhci_hcd->vhci->lock);
1223
1224         /* initialize private data of usb_hcd */
1225
1226         for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1227                 struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
1228
1229                 vhci_device_init(vdev);
1230                 vdev->rhport = rhport;
1231         }
1232
1233         atomic_set(&vhci_hcd->seqnum, 0);
1234
1235         hcd->power_budget = 0; /* no limit */
1236         hcd->uses_new_polling = 1;
1237
1238 #ifdef CONFIG_USB_OTG
1239         hcd->self.otg_port = 1;
1240 #endif
1241
1242         id = hcd_name_to_id(hcd_name(hcd));
1243         if (id < 0) {
1244                 pr_err("invalid vhci name %s\n", hcd_name(hcd));
1245                 return -EINVAL;
1246         }
1247
1248         /* vhci_hcd is now ready to be controlled through sysfs */
1249         if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
1250                 err = vhci_init_attr_group();
1251                 if (err) {
1252                         pr_err("init attr group\n");
1253                         return err;
1254                 }
1255                 err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1256                 if (err) {
1257                         pr_err("create sysfs files\n");
1258                         vhci_finish_attr_group();
1259                         return err;
1260                 }
1261                 pr_info("created sysfs %s\n", hcd_name(hcd));
1262         }
1263
1264         return 0;
1265 }
1266
1267 static void vhci_stop(struct usb_hcd *hcd)
1268 {
1269         struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
1270         int id, rhport;
1271
1272         usbip_dbg_vhci_hc("stop VHCI controller\n");
1273
1274         /* 1. remove the userland interface of vhci_hcd */
1275         id = hcd_name_to_id(hcd_name(hcd));
1276         if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
1277                 sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1278                 vhci_finish_attr_group();
1279         }
1280
1281         /* 2. shutdown all the ports of vhci_hcd */
1282         for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1283                 struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
1284
1285                 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1286                 usbip_stop_eh(&vdev->ud);
1287         }
1288 }
1289
1290 static int vhci_get_frame_number(struct usb_hcd *hcd)
1291 {
1292         dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
1293         return 0;
1294 }
1295
1296 #ifdef CONFIG_PM
1297
1298 /* FIXME: suspend/resume */
1299 static int vhci_bus_suspend(struct usb_hcd *hcd)
1300 {
1301         struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1302         unsigned long flags;
1303
1304         dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1305
1306         spin_lock_irqsave(&vhci->lock, flags);
1307         hcd->state = HC_STATE_SUSPENDED;
1308         spin_unlock_irqrestore(&vhci->lock, flags);
1309
1310         return 0;
1311 }
1312
1313 static int vhci_bus_resume(struct usb_hcd *hcd)
1314 {
1315         struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1316         int rc = 0;
1317         unsigned long flags;
1318
1319         dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1320
1321         spin_lock_irqsave(&vhci->lock, flags);
1322         if (!HCD_HW_ACCESSIBLE(hcd))
1323                 rc = -ESHUTDOWN;
1324         else
1325                 hcd->state = HC_STATE_RUNNING;
1326         spin_unlock_irqrestore(&vhci->lock, flags);
1327
1328         return rc;
1329 }
1330
1331 #else
1332
1333 #define vhci_bus_suspend      NULL
1334 #define vhci_bus_resume       NULL
1335 #endif
1336
1337 /* Change a group of bulk endpoints to support multiple stream IDs */
1338 static int vhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1339         struct usb_host_endpoint **eps, unsigned int num_eps,
1340         unsigned int num_streams, gfp_t mem_flags)
1341 {
1342         dev_dbg(&hcd->self.root_hub->dev, "vhci_alloc_streams not implemented\n");
1343         return 0;
1344 }
1345
1346 /* Reverts a group of bulk endpoints back to not using stream IDs. */
1347 static int vhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1348         struct usb_host_endpoint **eps, unsigned int num_eps,
1349         gfp_t mem_flags)
1350 {
1351         dev_dbg(&hcd->self.root_hub->dev, "vhci_free_streams not implemented\n");
1352         return 0;
1353 }
1354
1355 static const struct hc_driver vhci_hc_driver = {
1356         .description    = driver_name,
1357         .product_desc   = driver_desc,
1358         .hcd_priv_size  = sizeof(struct vhci_hcd),
1359
1360         .flags          = HCD_USB3 | HCD_SHARED,
1361
1362         .reset          = vhci_setup,
1363         .start          = vhci_start,
1364         .stop           = vhci_stop,
1365
1366         .urb_enqueue    = vhci_urb_enqueue,
1367         .urb_dequeue    = vhci_urb_dequeue,
1368
1369         .get_frame_number = vhci_get_frame_number,
1370
1371         .hub_status_data = vhci_hub_status,
1372         .hub_control    = vhci_hub_control,
1373         .bus_suspend    = vhci_bus_suspend,
1374         .bus_resume     = vhci_bus_resume,
1375
1376         .alloc_streams  = vhci_alloc_streams,
1377         .free_streams   = vhci_free_streams,
1378 };
1379
1380 static int vhci_hcd_probe(struct platform_device *pdev)
1381 {
1382         struct vhci             *vhci = *((void **)dev_get_platdata(&pdev->dev));
1383         struct usb_hcd          *hcd_hs;
1384         struct usb_hcd          *hcd_ss;
1385         int                     ret;
1386
1387         usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1388
1389         /*
1390          * Allocate and initialize hcd.
1391          * Our private data is also allocated automatically.
1392          */
1393         hcd_hs = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1394         if (!hcd_hs) {
1395                 pr_err("create primary hcd failed\n");
1396                 return -ENOMEM;
1397         }
1398         hcd_hs->has_tt = 1;
1399
1400         /*
1401          * Finish generic HCD structure initialization and register.
1402          * Call the driver's reset() and start() routines.
1403          */
1404         ret = usb_add_hcd(hcd_hs, 0, 0);
1405         if (ret != 0) {
1406                 pr_err("usb_add_hcd hs failed %d\n", ret);
1407                 goto put_usb2_hcd;
1408         }
1409
1410         hcd_ss = usb_create_shared_hcd(&vhci_hc_driver, &pdev->dev,
1411                                        dev_name(&pdev->dev), hcd_hs);
1412         if (!hcd_ss) {
1413                 ret = -ENOMEM;
1414                 pr_err("create shared hcd failed\n");
1415                 goto remove_usb2_hcd;
1416         }
1417
1418         ret = usb_add_hcd(hcd_ss, 0, 0);
1419         if (ret) {
1420                 pr_err("usb_add_hcd ss failed %d\n", ret);
1421                 goto put_usb3_hcd;
1422         }
1423
1424         usbip_dbg_vhci_hc("bye\n");
1425         return 0;
1426
1427 put_usb3_hcd:
1428         usb_put_hcd(hcd_ss);
1429 remove_usb2_hcd:
1430         usb_remove_hcd(hcd_hs);
1431 put_usb2_hcd:
1432         usb_put_hcd(hcd_hs);
1433         vhci->vhci_hcd_hs = NULL;
1434         vhci->vhci_hcd_ss = NULL;
1435         return ret;
1436 }
1437
1438 static int vhci_hcd_remove(struct platform_device *pdev)
1439 {
1440         struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
1441
1442         /*
1443          * Disconnects the root hub,
1444          * then reverses the effects of usb_add_hcd(),
1445          * invoking the HCD's stop() methods.
1446          */
1447         usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
1448         usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
1449
1450         usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
1451         usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
1452
1453         vhci->vhci_hcd_hs = NULL;
1454         vhci->vhci_hcd_ss = NULL;
1455
1456         return 0;
1457 }
1458
1459 #ifdef CONFIG_PM
1460
1461 /* what should happen for USB/IP under suspend/resume? */
1462 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1463 {
1464         struct usb_hcd *hcd;
1465         struct vhci *vhci;
1466         int rhport;
1467         int connected = 0;
1468         int ret = 0;
1469         unsigned long flags;
1470
1471         dev_dbg(&pdev->dev, "%s\n", __func__);
1472
1473         hcd = platform_get_drvdata(pdev);
1474         if (!hcd)
1475                 return 0;
1476
1477         vhci = *((void **)dev_get_platdata(hcd->self.controller));
1478
1479         spin_lock_irqsave(&vhci->lock, flags);
1480
1481         for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1482                 if (vhci->vhci_hcd_hs->port_status[rhport] &
1483                     USB_PORT_STAT_CONNECTION)
1484                         connected += 1;
1485
1486                 if (vhci->vhci_hcd_ss->port_status[rhport] &
1487                     USB_PORT_STAT_CONNECTION)
1488                         connected += 1;
1489         }
1490
1491         spin_unlock_irqrestore(&vhci->lock, flags);
1492
1493         if (connected > 0) {
1494                 dev_info(&pdev->dev,
1495                          "We have %d active connection%s. Do not suspend.\n",
1496                          connected, (connected == 1 ? "" : "s"));
1497                 ret =  -EBUSY;
1498         } else {
1499                 dev_info(&pdev->dev, "suspend vhci_hcd");
1500                 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1501         }
1502
1503         return ret;
1504 }
1505
1506 static int vhci_hcd_resume(struct platform_device *pdev)
1507 {
1508         struct usb_hcd *hcd;
1509
1510         dev_dbg(&pdev->dev, "%s\n", __func__);
1511
1512         hcd = platform_get_drvdata(pdev);
1513         if (!hcd)
1514                 return 0;
1515         set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1516         usb_hcd_poll_rh_status(hcd);
1517
1518         return 0;
1519 }
1520
1521 #else
1522
1523 #define vhci_hcd_suspend        NULL
1524 #define vhci_hcd_resume         NULL
1525
1526 #endif
1527
1528 static struct platform_driver vhci_driver = {
1529         .probe  = vhci_hcd_probe,
1530         .remove = vhci_hcd_remove,
1531         .suspend = vhci_hcd_suspend,
1532         .resume = vhci_hcd_resume,
1533         .driver = {
1534                 .name = driver_name,
1535         },
1536 };
1537
1538 static void del_platform_devices(void)
1539 {
1540         struct platform_device *pdev;
1541         int i;
1542
1543         for (i = 0; i < vhci_num_controllers; i++) {
1544                 pdev = vhcis[i].pdev;
1545                 if (pdev != NULL)
1546                         platform_device_unregister(pdev);
1547                 vhcis[i].pdev = NULL;
1548         }
1549         sysfs_remove_link(&platform_bus.kobj, driver_name);
1550 }
1551
1552 static int __init vhci_hcd_init(void)
1553 {
1554         int i, ret;
1555
1556         if (usb_disabled())
1557                 return -ENODEV;
1558
1559         if (vhci_num_controllers < 1)
1560                 vhci_num_controllers = 1;
1561
1562         vhcis = kcalloc(vhci_num_controllers, sizeof(struct vhci), GFP_KERNEL);
1563         if (vhcis == NULL)
1564                 return -ENOMEM;
1565
1566         for (i = 0; i < vhci_num_controllers; i++) {
1567                 vhcis[i].pdev = platform_device_alloc(driver_name, i);
1568                 if (!vhcis[i].pdev) {
1569                         i--;
1570                         while (i >= 0)
1571                                 platform_device_put(vhcis[i--].pdev);
1572                         ret = -ENOMEM;
1573                         goto err_device_alloc;
1574                 }
1575         }
1576         for (i = 0; i < vhci_num_controllers; i++) {
1577                 void *vhci = &vhcis[i];
1578                 ret = platform_device_add_data(vhcis[i].pdev, &vhci, sizeof(void *));
1579                 if (ret)
1580                         goto err_driver_register;
1581         }
1582
1583         ret = platform_driver_register(&vhci_driver);
1584         if (ret)
1585                 goto err_driver_register;
1586
1587         for (i = 0; i < vhci_num_controllers; i++) {
1588                 ret = platform_device_add(vhcis[i].pdev);
1589                 if (ret < 0) {
1590                         i--;
1591                         while (i >= 0)
1592                                 platform_device_del(vhcis[i--].pdev);
1593                         goto err_add_hcd;
1594                 }
1595         }
1596
1597         return ret;
1598
1599 err_add_hcd:
1600         platform_driver_unregister(&vhci_driver);
1601 err_driver_register:
1602         for (i = 0; i < vhci_num_controllers; i++)
1603                 platform_device_put(vhcis[i].pdev);
1604 err_device_alloc:
1605         kfree(vhcis);
1606         return ret;
1607 }
1608
1609 static void __exit vhci_hcd_exit(void)
1610 {
1611         del_platform_devices();
1612         platform_driver_unregister(&vhci_driver);
1613         kfree(vhcis);
1614 }
1615
1616 module_init(vhci_hcd_init);
1617 module_exit(vhci_hcd_exit);
1618
1619 MODULE_AUTHOR(DRIVER_AUTHOR);
1620 MODULE_DESCRIPTION(DRIVER_DESC);
1621 MODULE_LICENSE("GPL");