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