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