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