1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget 4 * 5 * hub.c - virtual hub handling 6 * 7 * Copyright 2017 IBM Corporation 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/platform_device.h> 13 #include <linux/delay.h> 14 #include <linux/ioport.h> 15 #include <linux/slab.h> 16 #include <linux/errno.h> 17 #include <linux/list.h> 18 #include <linux/interrupt.h> 19 #include <linux/proc_fs.h> 20 #include <linux/prefetch.h> 21 #include <linux/clk.h> 22 #include <linux/usb/gadget.h> 23 #include <linux/of.h> 24 #include <linux/of_gpio.h> 25 #include <linux/regmap.h> 26 #include <linux/dma-mapping.h> 27 #include <linux/bcd.h> 28 #include <linux/version.h> 29 #include <linux/usb.h> 30 #include <linux/usb/hcd.h> 31 32 #include "vhub.h" 33 34 /* usb 2.0 hub device descriptor 35 * 36 * A few things we may want to improve here: 37 * 38 * - We may need to indicate TT support 39 * - We may need a device qualifier descriptor 40 * as devices can pretend to be usb1 or 2 41 * - Make vid/did overridable 42 * - make it look like usb1 if usb1 mode forced 43 */ 44 #define KERNEL_REL bin2bcd(LINUX_VERSION_MAJOR) 45 #define KERNEL_VER bin2bcd(LINUX_VERSION_PATCHLEVEL) 46 47 enum { 48 AST_VHUB_STR_INDEX_MAX = 4, 49 AST_VHUB_STR_MANUF = 3, 50 AST_VHUB_STR_PRODUCT = 2, 51 AST_VHUB_STR_SERIAL = 1, 52 }; 53 54 static const struct usb_device_descriptor ast_vhub_dev_desc = { 55 .bLength = USB_DT_DEVICE_SIZE, 56 .bDescriptorType = USB_DT_DEVICE, 57 .bcdUSB = cpu_to_le16(0x0200), 58 .bDeviceClass = USB_CLASS_HUB, 59 .bDeviceSubClass = 0, 60 .bDeviceProtocol = 1, 61 .bMaxPacketSize0 = 64, 62 .idVendor = cpu_to_le16(0x1d6b), 63 .idProduct = cpu_to_le16(0x0107), 64 .bcdDevice = cpu_to_le16(0x0100), 65 .iManufacturer = AST_VHUB_STR_MANUF, 66 .iProduct = AST_VHUB_STR_PRODUCT, 67 .iSerialNumber = AST_VHUB_STR_SERIAL, 68 .bNumConfigurations = 1, 69 }; 70 71 static const struct usb_qualifier_descriptor ast_vhub_qual_desc = { 72 .bLength = 0xA, 73 .bDescriptorType = USB_DT_DEVICE_QUALIFIER, 74 .bcdUSB = cpu_to_le16(0x0200), 75 .bDeviceClass = USB_CLASS_HUB, 76 .bDeviceSubClass = 0, 77 .bDeviceProtocol = 0, 78 .bMaxPacketSize0 = 64, 79 .bNumConfigurations = 1, 80 .bRESERVED = 0, 81 }; 82 83 /* 84 * Configuration descriptor: same comments as above 85 * regarding handling USB1 mode. 86 */ 87 88 /* 89 * We don't use sizeof() as Linux definition of 90 * struct usb_endpoint_descriptor contains 2 91 * extra bytes 92 */ 93 #define AST_VHUB_CONF_DESC_SIZE (USB_DT_CONFIG_SIZE + \ 94 USB_DT_INTERFACE_SIZE + \ 95 USB_DT_ENDPOINT_SIZE) 96 97 static const struct ast_vhub_full_cdesc ast_vhub_conf_desc = { 98 .cfg = { 99 .bLength = USB_DT_CONFIG_SIZE, 100 .bDescriptorType = USB_DT_CONFIG, 101 .wTotalLength = cpu_to_le16(AST_VHUB_CONF_DESC_SIZE), 102 .bNumInterfaces = 1, 103 .bConfigurationValue = 1, 104 .iConfiguration = 0, 105 .bmAttributes = USB_CONFIG_ATT_ONE | 106 USB_CONFIG_ATT_SELFPOWER | 107 USB_CONFIG_ATT_WAKEUP, 108 .bMaxPower = 0, 109 }, 110 .intf = { 111 .bLength = USB_DT_INTERFACE_SIZE, 112 .bDescriptorType = USB_DT_INTERFACE, 113 .bInterfaceNumber = 0, 114 .bAlternateSetting = 0, 115 .bNumEndpoints = 1, 116 .bInterfaceClass = USB_CLASS_HUB, 117 .bInterfaceSubClass = 0, 118 .bInterfaceProtocol = 0, 119 .iInterface = 0, 120 }, 121 .ep = { 122 .bLength = USB_DT_ENDPOINT_SIZE, 123 .bDescriptorType = USB_DT_ENDPOINT, 124 .bEndpointAddress = 0x81, 125 .bmAttributes = USB_ENDPOINT_XFER_INT, 126 .wMaxPacketSize = cpu_to_le16(1), 127 .bInterval = 0x0c, 128 }, 129 }; 130 131 #define AST_VHUB_HUB_DESC_SIZE (USB_DT_HUB_NONVAR_SIZE + 2) 132 133 static const struct usb_hub_descriptor ast_vhub_hub_desc = { 134 .bDescLength = AST_VHUB_HUB_DESC_SIZE, 135 .bDescriptorType = USB_DT_HUB, 136 .bNbrPorts = AST_VHUB_NUM_PORTS, 137 .wHubCharacteristics = cpu_to_le16(HUB_CHAR_NO_LPSM), 138 .bPwrOn2PwrGood = 10, 139 .bHubContrCurrent = 0, 140 .u.hs.DeviceRemovable[0] = 0, 141 .u.hs.DeviceRemovable[1] = 0xff, 142 }; 143 144 /* 145 * These strings converted to UTF-16 must be smaller than 146 * our EP0 buffer. 147 */ 148 static const struct usb_string ast_vhub_str_array[] = { 149 { 150 .id = AST_VHUB_STR_SERIAL, 151 .s = "00000000" 152 }, 153 { 154 .id = AST_VHUB_STR_PRODUCT, 155 .s = "USB Virtual Hub" 156 }, 157 { 158 .id = AST_VHUB_STR_MANUF, 159 .s = "Aspeed" 160 }, 161 { } 162 }; 163 164 static const struct usb_gadget_strings ast_vhub_strings = { 165 .language = 0x0409, 166 .strings = (struct usb_string *)ast_vhub_str_array 167 }; 168 169 static int ast_vhub_hub_dev_status(struct ast_vhub_ep *ep, 170 u16 wIndex, u16 wValue) 171 { 172 u8 st0; 173 174 EPDBG(ep, "GET_STATUS(dev)\n"); 175 176 /* 177 * Mark it as self-powered, I doubt the BMC is powered off 178 * the USB bus ... 179 */ 180 st0 = 1 << USB_DEVICE_SELF_POWERED; 181 182 /* 183 * Need to double check how remote wakeup actually works 184 * on that chip and what triggers it. 185 */ 186 if (ep->vhub->wakeup_en) 187 st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP; 188 189 return ast_vhub_simple_reply(ep, st0, 0); 190 } 191 192 static int ast_vhub_hub_ep_status(struct ast_vhub_ep *ep, 193 u16 wIndex, u16 wValue) 194 { 195 int ep_num; 196 u8 st0 = 0; 197 198 ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK; 199 EPDBG(ep, "GET_STATUS(ep%d)\n", ep_num); 200 201 /* On the hub we have only EP 0 and 1 */ 202 if (ep_num == 1) { 203 if (ep->vhub->ep1_stalled) 204 st0 |= 1 << USB_ENDPOINT_HALT; 205 } else if (ep_num != 0) 206 return std_req_stall; 207 208 return ast_vhub_simple_reply(ep, st0, 0); 209 } 210 211 static int ast_vhub_hub_dev_feature(struct ast_vhub_ep *ep, 212 u16 wIndex, u16 wValue, 213 bool is_set) 214 { 215 EPDBG(ep, "%s_FEATURE(dev val=%02x)\n", 216 is_set ? "SET" : "CLEAR", wValue); 217 218 if (wValue != USB_DEVICE_REMOTE_WAKEUP) 219 return std_req_stall; 220 221 ep->vhub->wakeup_en = is_set; 222 EPDBG(ep, "Hub remote wakeup %s\n", 223 is_set ? "enabled" : "disabled"); 224 225 return std_req_complete; 226 } 227 228 static int ast_vhub_hub_ep_feature(struct ast_vhub_ep *ep, 229 u16 wIndex, u16 wValue, 230 bool is_set) 231 { 232 int ep_num; 233 u32 reg; 234 235 ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK; 236 EPDBG(ep, "%s_FEATURE(ep%d val=%02x)\n", 237 is_set ? "SET" : "CLEAR", ep_num, wValue); 238 239 if (ep_num > 1) 240 return std_req_stall; 241 if (wValue != USB_ENDPOINT_HALT) 242 return std_req_stall; 243 if (ep_num == 0) 244 return std_req_complete; 245 246 EPDBG(ep, "%s stall on EP 1\n", 247 is_set ? "setting" : "clearing"); 248 249 ep->vhub->ep1_stalled = is_set; 250 reg = readl(ep->vhub->regs + AST_VHUB_EP1_CTRL); 251 if (is_set) { 252 reg |= VHUB_EP1_CTRL_STALL; 253 } else { 254 reg &= ~VHUB_EP1_CTRL_STALL; 255 reg |= VHUB_EP1_CTRL_RESET_TOGGLE; 256 } 257 writel(reg, ep->vhub->regs + AST_VHUB_EP1_CTRL); 258 259 return std_req_complete; 260 } 261 262 static int ast_vhub_rep_desc(struct ast_vhub_ep *ep, 263 u8 desc_type, u16 len) 264 { 265 size_t dsize; 266 struct ast_vhub *vhub = ep->vhub; 267 268 EPDBG(ep, "GET_DESCRIPTOR(type:%d)\n", desc_type); 269 270 /* 271 * Copy first to EP buffer and send from there, so 272 * we can do some in-place patching if needed. We know 273 * the EP buffer is big enough but ensure that doesn't 274 * change. We do that now rather than later after we 275 * have checked sizes etc... to avoid a gcc bug where 276 * it thinks len is constant and barfs about read 277 * overflows in memcpy. 278 */ 279 switch(desc_type) { 280 case USB_DT_DEVICE: 281 dsize = USB_DT_DEVICE_SIZE; 282 memcpy(ep->buf, &vhub->vhub_dev_desc, dsize); 283 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_dev_desc)); 284 BUILD_BUG_ON(USB_DT_DEVICE_SIZE >= AST_VHUB_EP0_MAX_PACKET); 285 break; 286 case USB_DT_OTHER_SPEED_CONFIG: 287 case USB_DT_CONFIG: 288 dsize = AST_VHUB_CONF_DESC_SIZE; 289 memcpy(ep->buf, &vhub->vhub_conf_desc, dsize); 290 ((u8 *)ep->buf)[1] = desc_type; 291 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_conf_desc)); 292 BUILD_BUG_ON(AST_VHUB_CONF_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET); 293 break; 294 case USB_DT_HUB: 295 dsize = AST_VHUB_HUB_DESC_SIZE; 296 memcpy(ep->buf, &vhub->vhub_hub_desc, dsize); 297 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_hub_desc)); 298 BUILD_BUG_ON(AST_VHUB_HUB_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET); 299 break; 300 case USB_DT_DEVICE_QUALIFIER: 301 dsize = sizeof(vhub->vhub_qual_desc); 302 memcpy(ep->buf, &vhub->vhub_qual_desc, dsize); 303 break; 304 default: 305 return std_req_stall; 306 } 307 308 /* Crop requested length */ 309 if (len > dsize) 310 len = dsize; 311 312 /* Shoot it from the EP buffer */ 313 return ast_vhub_reply(ep, NULL, len); 314 } 315 316 static struct usb_gadget_strings* 317 ast_vhub_str_of_container(struct usb_gadget_string_container *container) 318 { 319 return (struct usb_gadget_strings *)container->stash; 320 } 321 322 static int ast_vhub_collect_languages(struct ast_vhub *vhub, void *buf, 323 size_t size) 324 { 325 int rc, hdr_len, nlangs, max_langs; 326 struct usb_gadget_strings *lang_str; 327 struct usb_gadget_string_container *container; 328 struct usb_string_descriptor *sdesc = buf; 329 330 nlangs = 0; 331 hdr_len = sizeof(struct usb_descriptor_header); 332 max_langs = (size - hdr_len) / sizeof(sdesc->wData[0]); 333 list_for_each_entry(container, &vhub->vhub_str_desc, list) { 334 if (nlangs >= max_langs) 335 break; 336 337 lang_str = ast_vhub_str_of_container(container); 338 sdesc->wData[nlangs++] = cpu_to_le16(lang_str->language); 339 } 340 341 rc = hdr_len + nlangs * sizeof(sdesc->wData[0]); 342 sdesc->bLength = rc; 343 sdesc->bDescriptorType = USB_DT_STRING; 344 345 return rc; 346 } 347 348 static struct usb_gadget_strings *ast_vhub_lookup_string(struct ast_vhub *vhub, 349 u16 lang_id) 350 { 351 struct usb_gadget_strings *lang_str; 352 struct usb_gadget_string_container *container; 353 354 list_for_each_entry(container, &vhub->vhub_str_desc, list) { 355 lang_str = ast_vhub_str_of_container(container); 356 if (lang_str->language == lang_id) 357 return lang_str; 358 } 359 360 return NULL; 361 } 362 363 static int ast_vhub_rep_string(struct ast_vhub_ep *ep, 364 u8 string_id, u16 lang_id, 365 u16 len) 366 { 367 int rc; 368 u8 buf[256]; 369 struct ast_vhub *vhub = ep->vhub; 370 struct usb_gadget_strings *lang_str; 371 372 if (string_id == 0) { 373 rc = ast_vhub_collect_languages(vhub, buf, sizeof(buf)); 374 } else { 375 lang_str = ast_vhub_lookup_string(vhub, lang_id); 376 if (!lang_str) 377 return std_req_stall; 378 379 rc = usb_gadget_get_string(lang_str, string_id, buf); 380 } 381 382 if (rc < 0 || rc >= AST_VHUB_EP0_MAX_PACKET) 383 return std_req_stall; 384 385 /* Shoot it from the EP buffer */ 386 memcpy(ep->buf, buf, rc); 387 return ast_vhub_reply(ep, NULL, min_t(u16, rc, len)); 388 } 389 390 enum std_req_rc ast_vhub_std_hub_request(struct ast_vhub_ep *ep, 391 struct usb_ctrlrequest *crq) 392 { 393 struct ast_vhub *vhub = ep->vhub; 394 u16 wValue, wIndex, wLength; 395 396 wValue = le16_to_cpu(crq->wValue); 397 wIndex = le16_to_cpu(crq->wIndex); 398 wLength = le16_to_cpu(crq->wLength); 399 400 /* First packet, grab speed */ 401 if (vhub->speed == USB_SPEED_UNKNOWN) { 402 u32 ustat = readl(vhub->regs + AST_VHUB_USBSTS); 403 if (ustat & VHUB_USBSTS_HISPEED) 404 vhub->speed = USB_SPEED_HIGH; 405 else 406 vhub->speed = USB_SPEED_FULL; 407 UDCDBG(vhub, "USB status=%08x speed=%s\n", ustat, 408 vhub->speed == USB_SPEED_HIGH ? "high" : "full"); 409 } 410 411 switch ((crq->bRequestType << 8) | crq->bRequest) { 412 /* SET_ADDRESS */ 413 case DeviceOutRequest | USB_REQ_SET_ADDRESS: 414 EPDBG(ep, "SET_ADDRESS: Got address %x\n", wValue); 415 writel(wValue, vhub->regs + AST_VHUB_CONF); 416 return std_req_complete; 417 418 /* GET_STATUS */ 419 case DeviceRequest | USB_REQ_GET_STATUS: 420 return ast_vhub_hub_dev_status(ep, wIndex, wValue); 421 case InterfaceRequest | USB_REQ_GET_STATUS: 422 return ast_vhub_simple_reply(ep, 0, 0); 423 case EndpointRequest | USB_REQ_GET_STATUS: 424 return ast_vhub_hub_ep_status(ep, wIndex, wValue); 425 426 /* SET/CLEAR_FEATURE */ 427 case DeviceOutRequest | USB_REQ_SET_FEATURE: 428 return ast_vhub_hub_dev_feature(ep, wIndex, wValue, true); 429 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: 430 return ast_vhub_hub_dev_feature(ep, wIndex, wValue, false); 431 case EndpointOutRequest | USB_REQ_SET_FEATURE: 432 return ast_vhub_hub_ep_feature(ep, wIndex, wValue, true); 433 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: 434 return ast_vhub_hub_ep_feature(ep, wIndex, wValue, false); 435 436 /* GET/SET_CONFIGURATION */ 437 case DeviceRequest | USB_REQ_GET_CONFIGURATION: 438 return ast_vhub_simple_reply(ep, 1); 439 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: 440 if (wValue != 1) 441 return std_req_stall; 442 return std_req_complete; 443 444 /* GET_DESCRIPTOR */ 445 case DeviceRequest | USB_REQ_GET_DESCRIPTOR: 446 switch (wValue >> 8) { 447 case USB_DT_DEVICE: 448 case USB_DT_CONFIG: 449 case USB_DT_DEVICE_QUALIFIER: 450 case USB_DT_OTHER_SPEED_CONFIG: 451 return ast_vhub_rep_desc(ep, wValue >> 8, 452 wLength); 453 case USB_DT_STRING: 454 return ast_vhub_rep_string(ep, wValue & 0xff, 455 wIndex, wLength); 456 } 457 return std_req_stall; 458 459 /* GET/SET_INTERFACE */ 460 case DeviceRequest | USB_REQ_GET_INTERFACE: 461 return ast_vhub_simple_reply(ep, 0); 462 case DeviceOutRequest | USB_REQ_SET_INTERFACE: 463 if (wValue != 0 || wIndex != 0) 464 return std_req_stall; 465 return std_req_complete; 466 } 467 return std_req_stall; 468 } 469 470 static void ast_vhub_update_hub_ep1(struct ast_vhub *vhub, 471 unsigned int port) 472 { 473 /* Update HW EP1 response */ 474 u32 reg = readl(vhub->regs + AST_VHUB_EP1_STS_CHG); 475 u32 pmask = (1 << (port + 1)); 476 if (vhub->ports[port].change) 477 reg |= pmask; 478 else 479 reg &= ~pmask; 480 writel(reg, vhub->regs + AST_VHUB_EP1_STS_CHG); 481 } 482 483 static void ast_vhub_change_port_stat(struct ast_vhub *vhub, 484 unsigned int port, 485 u16 clr_flags, 486 u16 set_flags, 487 bool set_c) 488 { 489 struct ast_vhub_port *p = &vhub->ports[port]; 490 u16 prev; 491 492 /* Update port status */ 493 prev = p->status; 494 p->status = (prev & ~clr_flags) | set_flags; 495 DDBG(&p->dev, "port %d status %04x -> %04x (C=%d)\n", 496 port + 1, prev, p->status, set_c); 497 498 /* Update change bits if needed */ 499 if (set_c) { 500 u16 chg = p->status ^ prev; 501 502 /* Only these are relevant for change */ 503 chg &= USB_PORT_STAT_C_CONNECTION | 504 USB_PORT_STAT_C_ENABLE | 505 USB_PORT_STAT_C_SUSPEND | 506 USB_PORT_STAT_C_OVERCURRENT | 507 USB_PORT_STAT_C_RESET | 508 USB_PORT_STAT_C_L1; 509 510 /* 511 * We only set USB_PORT_STAT_C_ENABLE if we are disabling 512 * the port as per USB spec, otherwise MacOS gets upset 513 */ 514 if (p->status & USB_PORT_STAT_ENABLE) 515 chg &= ~USB_PORT_STAT_C_ENABLE; 516 517 p->change = chg; 518 ast_vhub_update_hub_ep1(vhub, port); 519 } 520 } 521 522 static void ast_vhub_send_host_wakeup(struct ast_vhub *vhub) 523 { 524 u32 reg = readl(vhub->regs + AST_VHUB_CTRL); 525 UDCDBG(vhub, "Waking up host !\n"); 526 reg |= VHUB_CTRL_MANUAL_REMOTE_WAKEUP; 527 writel(reg, vhub->regs + AST_VHUB_CTRL); 528 } 529 530 void ast_vhub_device_connect(struct ast_vhub *vhub, 531 unsigned int port, bool on) 532 { 533 if (on) 534 ast_vhub_change_port_stat(vhub, port, 0, 535 USB_PORT_STAT_CONNECTION, true); 536 else 537 ast_vhub_change_port_stat(vhub, port, 538 USB_PORT_STAT_CONNECTION | 539 USB_PORT_STAT_ENABLE, 540 0, true); 541 542 /* 543 * If the hub is set to wakup the host on connection events 544 * then send a wakeup. 545 */ 546 if (vhub->wakeup_en) 547 ast_vhub_send_host_wakeup(vhub); 548 } 549 550 static void ast_vhub_wake_work(struct work_struct *work) 551 { 552 struct ast_vhub *vhub = container_of(work, 553 struct ast_vhub, 554 wake_work); 555 unsigned long flags; 556 unsigned int i; 557 558 /* 559 * Wake all sleeping ports. If a port is suspended by 560 * the host suspend (without explicit state suspend), 561 * we let the normal host wake path deal with it later. 562 */ 563 spin_lock_irqsave(&vhub->lock, flags); 564 for (i = 0; i < vhub->max_ports; i++) { 565 struct ast_vhub_port *p = &vhub->ports[i]; 566 567 if (!(p->status & USB_PORT_STAT_SUSPEND)) 568 continue; 569 ast_vhub_change_port_stat(vhub, i, 570 USB_PORT_STAT_SUSPEND, 571 0, true); 572 ast_vhub_dev_resume(&p->dev); 573 } 574 ast_vhub_send_host_wakeup(vhub); 575 spin_unlock_irqrestore(&vhub->lock, flags); 576 } 577 578 void ast_vhub_hub_wake_all(struct ast_vhub *vhub) 579 { 580 /* 581 * A device is trying to wake the world, because this 582 * can recurse into the device, we break the call chain 583 * using a work queue 584 */ 585 schedule_work(&vhub->wake_work); 586 } 587 588 static void ast_vhub_port_reset(struct ast_vhub *vhub, u8 port) 589 { 590 struct ast_vhub_port *p = &vhub->ports[port]; 591 u16 set, clr, speed; 592 593 /* First mark disabled */ 594 ast_vhub_change_port_stat(vhub, port, 595 USB_PORT_STAT_ENABLE | 596 USB_PORT_STAT_SUSPEND, 597 USB_PORT_STAT_RESET, 598 false); 599 600 if (!p->dev.driver) 601 return; 602 603 /* 604 * This will either "start" the port or reset the 605 * device if already started... 606 */ 607 ast_vhub_dev_reset(&p->dev); 608 609 /* Grab the right speed */ 610 speed = p->dev.driver->max_speed; 611 if (speed == USB_SPEED_UNKNOWN || speed > vhub->speed) 612 speed = vhub->speed; 613 614 switch (speed) { 615 case USB_SPEED_LOW: 616 set = USB_PORT_STAT_LOW_SPEED; 617 clr = USB_PORT_STAT_HIGH_SPEED; 618 break; 619 case USB_SPEED_FULL: 620 set = 0; 621 clr = USB_PORT_STAT_LOW_SPEED | 622 USB_PORT_STAT_HIGH_SPEED; 623 break; 624 case USB_SPEED_HIGH: 625 set = USB_PORT_STAT_HIGH_SPEED; 626 clr = USB_PORT_STAT_LOW_SPEED; 627 break; 628 default: 629 UDCDBG(vhub, "Unsupported speed %d when" 630 " connecting device\n", 631 speed); 632 return; 633 } 634 clr |= USB_PORT_STAT_RESET; 635 set |= USB_PORT_STAT_ENABLE; 636 637 /* This should ideally be delayed ... */ 638 ast_vhub_change_port_stat(vhub, port, clr, set, true); 639 } 640 641 static enum std_req_rc ast_vhub_set_port_feature(struct ast_vhub_ep *ep, 642 u8 port, u16 feat) 643 { 644 struct ast_vhub *vhub = ep->vhub; 645 struct ast_vhub_port *p; 646 647 if (port == 0 || port > vhub->max_ports) 648 return std_req_stall; 649 port--; 650 p = &vhub->ports[port]; 651 652 switch(feat) { 653 case USB_PORT_FEAT_SUSPEND: 654 if (!(p->status & USB_PORT_STAT_ENABLE)) 655 return std_req_complete; 656 ast_vhub_change_port_stat(vhub, port, 657 0, USB_PORT_STAT_SUSPEND, 658 false); 659 ast_vhub_dev_suspend(&p->dev); 660 return std_req_complete; 661 case USB_PORT_FEAT_RESET: 662 EPDBG(ep, "Port reset !\n"); 663 ast_vhub_port_reset(vhub, port); 664 return std_req_complete; 665 case USB_PORT_FEAT_POWER: 666 /* 667 * On Power-on, we mark the connected flag changed, 668 * if there's a connected device, some hosts will 669 * otherwise fail to detect it. 670 */ 671 if (p->status & USB_PORT_STAT_CONNECTION) { 672 p->change |= USB_PORT_STAT_C_CONNECTION; 673 ast_vhub_update_hub_ep1(vhub, port); 674 } 675 return std_req_complete; 676 case USB_PORT_FEAT_TEST: 677 case USB_PORT_FEAT_INDICATOR: 678 /* We don't do anything with these */ 679 return std_req_complete; 680 } 681 return std_req_stall; 682 } 683 684 static enum std_req_rc ast_vhub_clr_port_feature(struct ast_vhub_ep *ep, 685 u8 port, u16 feat) 686 { 687 struct ast_vhub *vhub = ep->vhub; 688 struct ast_vhub_port *p; 689 690 if (port == 0 || port > vhub->max_ports) 691 return std_req_stall; 692 port--; 693 p = &vhub->ports[port]; 694 695 switch(feat) { 696 case USB_PORT_FEAT_ENABLE: 697 ast_vhub_change_port_stat(vhub, port, 698 USB_PORT_STAT_ENABLE | 699 USB_PORT_STAT_SUSPEND, 0, 700 false); 701 ast_vhub_dev_suspend(&p->dev); 702 return std_req_complete; 703 case USB_PORT_FEAT_SUSPEND: 704 if (!(p->status & USB_PORT_STAT_SUSPEND)) 705 return std_req_complete; 706 ast_vhub_change_port_stat(vhub, port, 707 USB_PORT_STAT_SUSPEND, 0, 708 false); 709 ast_vhub_dev_resume(&p->dev); 710 return std_req_complete; 711 case USB_PORT_FEAT_POWER: 712 /* We don't do power control */ 713 return std_req_complete; 714 case USB_PORT_FEAT_INDICATOR: 715 /* We don't have indicators */ 716 return std_req_complete; 717 case USB_PORT_FEAT_C_CONNECTION: 718 case USB_PORT_FEAT_C_ENABLE: 719 case USB_PORT_FEAT_C_SUSPEND: 720 case USB_PORT_FEAT_C_OVER_CURRENT: 721 case USB_PORT_FEAT_C_RESET: 722 /* Clear state-change feature */ 723 p->change &= ~(1u << (feat - 16)); 724 ast_vhub_update_hub_ep1(vhub, port); 725 return std_req_complete; 726 } 727 return std_req_stall; 728 } 729 730 static enum std_req_rc ast_vhub_get_port_stat(struct ast_vhub_ep *ep, 731 u8 port) 732 { 733 struct ast_vhub *vhub = ep->vhub; 734 u16 stat, chg; 735 736 if (port == 0 || port > vhub->max_ports) 737 return std_req_stall; 738 port--; 739 740 stat = vhub->ports[port].status; 741 chg = vhub->ports[port].change; 742 743 /* We always have power */ 744 stat |= USB_PORT_STAT_POWER; 745 746 EPDBG(ep, " port status=%04x change=%04x\n", stat, chg); 747 748 return ast_vhub_simple_reply(ep, 749 stat & 0xff, 750 stat >> 8, 751 chg & 0xff, 752 chg >> 8); 753 } 754 755 enum std_req_rc ast_vhub_class_hub_request(struct ast_vhub_ep *ep, 756 struct usb_ctrlrequest *crq) 757 { 758 u16 wValue, wIndex, wLength; 759 760 wValue = le16_to_cpu(crq->wValue); 761 wIndex = le16_to_cpu(crq->wIndex); 762 wLength = le16_to_cpu(crq->wLength); 763 764 switch ((crq->bRequestType << 8) | crq->bRequest) { 765 case GetHubStatus: 766 EPDBG(ep, "GetHubStatus\n"); 767 return ast_vhub_simple_reply(ep, 0, 0, 0, 0); 768 case GetPortStatus: 769 EPDBG(ep, "GetPortStatus(%d)\n", wIndex & 0xff); 770 return ast_vhub_get_port_stat(ep, wIndex & 0xf); 771 case GetHubDescriptor: 772 if (wValue != (USB_DT_HUB << 8)) 773 return std_req_stall; 774 EPDBG(ep, "GetHubDescriptor(%d)\n", wIndex & 0xff); 775 return ast_vhub_rep_desc(ep, USB_DT_HUB, wLength); 776 case SetHubFeature: 777 case ClearHubFeature: 778 EPDBG(ep, "Get/SetHubFeature(%d)\n", wValue); 779 /* No feature, just complete the requests */ 780 if (wValue == C_HUB_LOCAL_POWER || 781 wValue == C_HUB_OVER_CURRENT) 782 return std_req_complete; 783 return std_req_stall; 784 case SetPortFeature: 785 EPDBG(ep, "SetPortFeature(%d,%d)\n", wIndex & 0xf, wValue); 786 return ast_vhub_set_port_feature(ep, wIndex & 0xf, wValue); 787 case ClearPortFeature: 788 EPDBG(ep, "ClearPortFeature(%d,%d)\n", wIndex & 0xf, wValue); 789 return ast_vhub_clr_port_feature(ep, wIndex & 0xf, wValue); 790 case ClearTTBuffer: 791 case ResetTT: 792 case StopTT: 793 return std_req_complete; 794 case GetTTState: 795 return ast_vhub_simple_reply(ep, 0, 0, 0, 0); 796 default: 797 EPDBG(ep, "Unknown class request\n"); 798 } 799 return std_req_stall; 800 } 801 802 void ast_vhub_hub_suspend(struct ast_vhub *vhub) 803 { 804 unsigned int i; 805 806 UDCDBG(vhub, "USB bus suspend\n"); 807 808 if (vhub->suspended) 809 return; 810 811 vhub->suspended = true; 812 813 /* 814 * Forward to unsuspended ports without changing 815 * their connection status. 816 */ 817 for (i = 0; i < vhub->max_ports; i++) { 818 struct ast_vhub_port *p = &vhub->ports[i]; 819 820 if (!(p->status & USB_PORT_STAT_SUSPEND)) 821 ast_vhub_dev_suspend(&p->dev); 822 } 823 } 824 825 void ast_vhub_hub_resume(struct ast_vhub *vhub) 826 { 827 unsigned int i; 828 829 UDCDBG(vhub, "USB bus resume\n"); 830 831 if (!vhub->suspended) 832 return; 833 834 vhub->suspended = false; 835 836 /* 837 * Forward to unsuspended ports without changing 838 * their connection status. 839 */ 840 for (i = 0; i < vhub->max_ports; i++) { 841 struct ast_vhub_port *p = &vhub->ports[i]; 842 843 if (!(p->status & USB_PORT_STAT_SUSPEND)) 844 ast_vhub_dev_resume(&p->dev); 845 } 846 } 847 848 void ast_vhub_hub_reset(struct ast_vhub *vhub) 849 { 850 unsigned int i; 851 852 UDCDBG(vhub, "USB bus reset\n"); 853 854 /* 855 * Is the speed known ? If not we don't care, we aren't 856 * initialized yet and ports haven't been enabled. 857 */ 858 if (vhub->speed == USB_SPEED_UNKNOWN) 859 return; 860 861 /* We aren't suspended anymore obviously */ 862 vhub->suspended = false; 863 864 /* No speed set */ 865 vhub->speed = USB_SPEED_UNKNOWN; 866 867 /* Wakeup not enabled anymore */ 868 vhub->wakeup_en = false; 869 870 /* 871 * Clear all port status, disable gadgets and "suspend" 872 * them. They will be woken up by a port reset. 873 */ 874 for (i = 0; i < vhub->max_ports; i++) { 875 struct ast_vhub_port *p = &vhub->ports[i]; 876 877 /* Only keep the connected flag */ 878 p->status &= USB_PORT_STAT_CONNECTION; 879 p->change = 0; 880 881 /* Suspend the gadget if any */ 882 ast_vhub_dev_suspend(&p->dev); 883 } 884 885 /* Cleanup HW */ 886 writel(0, vhub->regs + AST_VHUB_CONF); 887 writel(0, vhub->regs + AST_VHUB_EP0_CTRL); 888 writel(VHUB_EP1_CTRL_RESET_TOGGLE | 889 VHUB_EP1_CTRL_ENABLE, 890 vhub->regs + AST_VHUB_EP1_CTRL); 891 writel(0, vhub->regs + AST_VHUB_EP1_STS_CHG); 892 } 893 894 static void ast_vhub_of_parse_dev_desc(struct ast_vhub *vhub, 895 const struct device_node *vhub_np) 896 { 897 u16 id; 898 u32 data; 899 900 if (!of_property_read_u32(vhub_np, "vhub-vendor-id", &data)) { 901 id = (u16)data; 902 vhub->vhub_dev_desc.idVendor = cpu_to_le16(id); 903 } 904 if (!of_property_read_u32(vhub_np, "vhub-product-id", &data)) { 905 id = (u16)data; 906 vhub->vhub_dev_desc.idProduct = cpu_to_le16(id); 907 } 908 if (!of_property_read_u32(vhub_np, "vhub-device-revision", &data)) { 909 id = (u16)data; 910 vhub->vhub_dev_desc.bcdDevice = cpu_to_le16(id); 911 } 912 } 913 914 static void ast_vhub_fixup_usb1_dev_desc(struct ast_vhub *vhub) 915 { 916 vhub->vhub_dev_desc.bcdUSB = cpu_to_le16(0x0100); 917 vhub->vhub_dev_desc.bDeviceProtocol = 0; 918 } 919 920 static struct usb_gadget_string_container* 921 ast_vhub_str_container_alloc(struct ast_vhub *vhub) 922 { 923 unsigned int size; 924 struct usb_string *str_array; 925 struct usb_gadget_strings *lang_str; 926 struct usb_gadget_string_container *container; 927 928 size = sizeof(*container); 929 size += sizeof(struct usb_gadget_strings); 930 size += sizeof(struct usb_string) * AST_VHUB_STR_INDEX_MAX; 931 container = devm_kzalloc(&vhub->pdev->dev, size, GFP_KERNEL); 932 if (!container) 933 return ERR_PTR(-ENOMEM); 934 935 lang_str = ast_vhub_str_of_container(container); 936 str_array = (struct usb_string *)(lang_str + 1); 937 lang_str->strings = str_array; 938 return container; 939 } 940 941 static void ast_vhub_str_deep_copy(struct usb_gadget_strings *dest, 942 const struct usb_gadget_strings *src) 943 { 944 struct usb_string *src_array = src->strings; 945 struct usb_string *dest_array = dest->strings; 946 947 dest->language = src->language; 948 if (src_array && dest_array) { 949 do { 950 *dest_array = *src_array; 951 dest_array++; 952 src_array++; 953 } while (src_array->s); 954 } 955 } 956 957 static int ast_vhub_str_alloc_add(struct ast_vhub *vhub, 958 const struct usb_gadget_strings *src_str) 959 { 960 struct usb_gadget_strings *dest_str; 961 struct usb_gadget_string_container *container; 962 963 container = ast_vhub_str_container_alloc(vhub); 964 if (IS_ERR(container)) 965 return PTR_ERR(container); 966 967 dest_str = ast_vhub_str_of_container(container); 968 ast_vhub_str_deep_copy(dest_str, src_str); 969 list_add_tail(&container->list, &vhub->vhub_str_desc); 970 971 return 0; 972 } 973 974 static const struct { 975 const char *name; 976 u8 id; 977 } str_id_map[] = { 978 {"manufacturer", AST_VHUB_STR_MANUF}, 979 {"product", AST_VHUB_STR_PRODUCT}, 980 {"serial-number", AST_VHUB_STR_SERIAL}, 981 {}, 982 }; 983 984 static int ast_vhub_of_parse_str_desc(struct ast_vhub *vhub, 985 const struct device_node *desc_np) 986 { 987 u32 langid; 988 int ret = 0; 989 int i, offset; 990 const char *str; 991 struct device_node *child; 992 struct usb_string str_array[AST_VHUB_STR_INDEX_MAX]; 993 struct usb_gadget_strings lang_str = { 994 .strings = (struct usb_string *)str_array, 995 }; 996 997 for_each_child_of_node(desc_np, child) { 998 if (of_property_read_u32(child, "reg", &langid)) 999 continue; /* no language identifier specified */ 1000 1001 if (!usb_validate_langid(langid)) 1002 continue; /* invalid language identifier */ 1003 1004 lang_str.language = langid; 1005 for (i = offset = 0; str_id_map[i].name; i++) { 1006 str = of_get_property(child, str_id_map[i].name, NULL); 1007 if (str) { 1008 str_array[offset].s = str; 1009 str_array[offset].id = str_id_map[i].id; 1010 offset++; 1011 } 1012 } 1013 str_array[offset].id = 0; 1014 str_array[offset].s = NULL; 1015 1016 ret = ast_vhub_str_alloc_add(vhub, &lang_str); 1017 if (ret) { 1018 of_node_put(child); 1019 break; 1020 } 1021 } 1022 1023 return ret; 1024 } 1025 1026 static int ast_vhub_init_desc(struct ast_vhub *vhub) 1027 { 1028 int ret; 1029 struct device_node *desc_np; 1030 const struct device_node *vhub_np = vhub->pdev->dev.of_node; 1031 1032 /* Initialize vhub Device Descriptor. */ 1033 memcpy(&vhub->vhub_dev_desc, &ast_vhub_dev_desc, 1034 sizeof(vhub->vhub_dev_desc)); 1035 ast_vhub_of_parse_dev_desc(vhub, vhub_np); 1036 if (vhub->force_usb1) 1037 ast_vhub_fixup_usb1_dev_desc(vhub); 1038 1039 /* Initialize vhub Configuration Descriptor. */ 1040 memcpy(&vhub->vhub_conf_desc, &ast_vhub_conf_desc, 1041 sizeof(vhub->vhub_conf_desc)); 1042 1043 /* Initialize vhub Hub Descriptor. */ 1044 memcpy(&vhub->vhub_hub_desc, &ast_vhub_hub_desc, 1045 sizeof(vhub->vhub_hub_desc)); 1046 vhub->vhub_hub_desc.bNbrPorts = vhub->max_ports; 1047 1048 /* Initialize vhub String Descriptors. */ 1049 INIT_LIST_HEAD(&vhub->vhub_str_desc); 1050 desc_np = of_get_child_by_name(vhub_np, "vhub-strings"); 1051 if (desc_np) 1052 ret = ast_vhub_of_parse_str_desc(vhub, desc_np); 1053 else 1054 ret = ast_vhub_str_alloc_add(vhub, &ast_vhub_strings); 1055 1056 /* Initialize vhub Qualifier Descriptor. */ 1057 memcpy(&vhub->vhub_qual_desc, &ast_vhub_qual_desc, 1058 sizeof(vhub->vhub_qual_desc)); 1059 1060 return ret; 1061 } 1062 1063 int ast_vhub_init_hub(struct ast_vhub *vhub) 1064 { 1065 vhub->speed = USB_SPEED_UNKNOWN; 1066 INIT_WORK(&vhub->wake_work, ast_vhub_wake_work); 1067 1068 return ast_vhub_init_desc(vhub); 1069 } 1070