fusb_hub.c 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
/*
 * Copyright : (C) 2022 Phytium Information Technology, Inc.
 * All Rights Reserved.
 *
 * This program is OPEN SOURCE software: you can redistribute it and/or modify it
 * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
 * either version 1.0 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the Phytium Public License for more details.
 *
 *
 * FilePath: fusb_hub.c
 * Date: 2022-02-11 13:33:07
 * LastEditTime: 2022-02-17 17:48:52
 * Description:  This files is for implmentation of USB hub function
 *  you may refer to chapter 11 Hub specification for details
 *
 * Modify History:
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 * 1.0   Zhugengyu  2022/2/7    init commit
 */

#include "fkernel.h"
#include "fdebug.h"
#include "fassert.h"

#include "fusb_private.h"
#include "fusb_generic_hub.h"

#define FUSB_DEBUG_TAG "FUSB_HUB"
#define FUSB_ERROR(format, ...) FT_DEBUG_PRINT_E(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
#define FUSB_WARN(format, ...) FT_DEBUG_PRINT_W(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
#define FUSB_INFO(format, ...) FT_DEBUG_PRINT_I(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
#define FUSB_DEBUG(format, ...) FT_DEBUG_PRINT_D(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)

/* assume that FUSB_REQ_HOST_TO_DEVICE is overwritten if necessary */
#define DR_PORT FUsbGenerateReqType(FUSB_REQ_HOST_TO_DEVICE, FUSB_REQ_TYPE_CLASS, FUSB_REQ_RECP_OTHER) /* 10100011B */
#define DR_HUB FUsbGenerateReqType(FUSB_REQ_HOST_TO_DEVICE, FUSB_REQ_TYPE_CLASS, FUSB_REQ_RECP_DEV) /* 10100000B */

#define FUSB_HUB_PORT_STATUS        0
#define FUSB_HUB_PORT_CHANGE        1

#define FUSB_HUB_PORT_BUF_LEN       2

/* status (and status change) bits, refer to Table 10-10, Port Status Field in USB spec */
#define FUSB_HUB_STATUS_PORT_CONNECTION BIT(0) /* reflects if device is currently connected to this port */
#define FUSB_HUB_STATUS_PORT_ENABLE     BIT(1) /* reflects if this port is enabled */
#define FUSB_HUB_STATUS_PORT_SUSPEND    BIT(2) /* reflects if this port is suspend, only for USB2 */
#define FUSB_HUB_STATUS_PORT_OVER_CURRENT BIT(3) /* reports over-current conditions in this port */
#define FUSB_HUB_STATUS_PORT_RESET      BIT(4) /* reset signaling asserted */
#define FUSB_HUB_STATUS_BH_PORT_RESET   BIT(5) /* warm reset completed */
#define FUSB_HUB_STATUS_PORT_LINK_STATE BIT(6) /* link state changed */
#define FUSB_HUB_STATUS_PORT_PORT_CONFIG_ERROR BIT(7) /* port fails to config */

/* feature selectors (for setting / clearing features), refer to USB spec. Table 10-17. Hub Class Feature Selectors for details */
#define FUSB_HUB_SEL_PORT_RESET         4
#define FUSB_HUB_SEL_PORT_POWER         8
#define FUSB_HUB_SEL_C_PORT_CONNECTION  16
#define FUSB_HUB_SEL_C_PORT_ENABLE      17  /* USB2 only */
#define FUSB_HUB_SEL_C_PORT_SUSPEND     18  /* USB2 only */
#define FUSB_HUB_SEL_C_PORT_OVER_CURRENT 19
#define FUSB_HUB_SEL_C_PORT_RESET       20
#define FUSB_HUB_SEL_C_PORT_LINK_STATE  25
#define FUSB_HUB_SEL_C_PORT_CONFIG_ERROR 26
#define FUSB_HUB_SEL_C_BH_PORT_RESET     27

/* request type (USB 3.0 hubs only) */
#define SET_HUB_DEPTH 12

/**
 * @name: FUsbHubIntrEp
 * @msg: 获取USB Hub的中断端点
 * @return {FUsbEndpoint *} 中断类型的功能端点
 * @param {FUsbDev} *dev, Hub实例
 */
static FUsbEndpoint *FUsbHubIntrEp(FUsbDev *const dev)
{
    FASSERT(dev);
    int i;

    for (i = 0; i < dev->num_endp; ++i)
    {
        if (dev->endpoints[i].type == FUSB_INTERRUPT_EP &&
                dev->endpoints[i].direction == FUSB_IN)
            return &dev->endpoints[i];
    }

    return NULL;
}

/**
 * @name: FUsbHubPortStatusChange
 * @msg: Usb Hub的Port状态变化回调函数
 * @return {FUsbTransCode} USB请求返回值
 * @param {FUsbDev} *dev, Hub实例
 * @param {int} port, Port号
 */
static FUsbTransCode FUsbHubPortStatusChange(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    unsigned short buf[FUSB_HUB_PORT_BUF_LEN]; /* Hub Status and Change Status */
    FUsbTransCode ret = FUsbGetStatus(dev, port, DR_PORT, sizeof(buf), buf);

    if (ret >= FUSB_CC_ZERO_BYTES)
    {
        ret = buf[FUSB_HUB_PORT_CHANGE] & FUSB_HUB_STATUS_PORT_CONNECTION;
        if (ret)
        {
            ret = FUsbClearFeature(dev, port, FUSB_HUB_SEL_C_PORT_CONNECTION, DR_PORT);
        }
    }

    return ret;
}

/**
 * @name: FUsbHubPortConnected
 * @msg: Usb Hub的Port连接回调函数
 * @return {FUsbTransCode} USB请求返回值
 * @param {FUsbDev} *dev, Hub实例
 * @param {int} port, Port号
 */
static FUsbTransCode FUsbHubPortConnected(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    unsigned short buf[FUSB_HUB_PORT_BUF_LEN]; /* Hub Status and Change Status */
    FUsbTransCode ret = FUsbGetStatus(dev, port, DR_PORT, sizeof(buf), buf);

    if (ret >= FUSB_CC_ZERO_BYTES)
    {
        ret = buf[FUSB_HUB_PORT_STATUS] & FUSB_HUB_STATUS_PORT_CONNECTION;
    }

    return ret;
}

/**
 * @name: FUsbHubPortInReset
 * @msg: 检查Hub port是否处于Reset状态
 * @return {FUsbTransCode} USB请求返回值
 * @param {FUsbDev} *dev, Hub实例
 * @param {int} port, Port号
 */
static FUsbTransCode FUsbHubPortInReset(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    unsigned short buf[FUSB_HUB_PORT_BUF_LEN]; /* Hub Status and Change Status */
    FUsbTransCode ret = FUsbGetStatus(dev, port, DR_PORT, sizeof(buf), buf);

    if (ret >= FUSB_CC_ZERO_BYTES)
        ret = buf[FUSB_HUB_PORT_STATUS] & FUSB_HUB_STATUS_PORT_RESET;

    return ret;
}

/**
 * @name: FUsbHubPortEnabled
 * @msg: 检查Hub port是否已使能
 * @return {FUsbTransCode} USB请求返回值
 * @param {FUsbDev} *dev, Hub实例
 * @param {int} port, Port号
 */
static FUsbTransCode FUsbHubPortEnabled(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    unsigned short buf[FUSB_HUB_PORT_BUF_LEN]; /* Hub Status and Change Status */
    FUsbTransCode ret = FUsbGetStatus(dev, port, DR_PORT, sizeof(buf), buf);

    if (ret >= FUSB_CC_ZERO_BYTES)
        ret = buf[FUSB_HUB_PORT_STATUS] & FUSB_HUB_STATUS_PORT_ENABLE;

    return ret;
}

/**
 * @name: FUsbHubPortSpeed
 * @msg: 获取Hub port的速度类型
 * @return {FUsbSpeed} Port的速度类型,支持SuperSpeed和HighSpeed
 * @param {FUsbDev} *dev, Hub实例
 * @param {int} port, Port号
 */
static FUsbSpeed FUsbHubPortSpeed(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    unsigned short buf[FUSB_HUB_PORT_BUF_LEN]; /* Hub Status and Change Status */
    FUsbTransCode ret = FUsbGetStatus(dev, port, DR_PORT, sizeof(buf), buf);
    int speed;

    if (ret >= FUSB_CC_ZERO_BYTES && (buf[FUSB_HUB_PORT_STATUS] & FUSB_HUB_STATUS_PORT_ENABLE))
    {
        /* SuperSpeed hubs can only have SuperSpeed devices. */
        if (FUsbIsSuperSpeed(dev->speed))
            return dev->speed;

        /*[bit] 10  9  (USB 2.0 port status word)
         *      0   0  full speed
         *      0   1  low speed
         *      1   0  high speed
         *      1   1  invalid
         */
        speed = (buf[FUSB_HUB_PORT_STATUS] >> 9) & 0x3;
        if (speed != 0x3) /* high-speed device */
            return speed;
    }

    return FUSB_UNKNOWN_SPEED;
}

/**
 * @name: FUsbHubEnablePort
 * @msg: 使能Hub port
 * @return {FUsbTransCode} USB请求返回值
 * @param {FUsbDev} *dev, Hub实例
 * @param {int} port, port号
 */
static FUsbTransCode FUsbHubEnablePort(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    return FUsbSetFeature(dev, port, FUSB_HUB_SEL_PORT_POWER, DR_PORT);
}

/**
 * @name: FUsbHubStartPortReset
 * @msg: 开始Reset Hub port
 * @return {FUsbTransCode} USB请求返回值
 * @param {FUsbDev} *dev, Hub实例
 * @param {int} port, port号
 */
static FUsbTransCode FUsbHubStartPortReset(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    return FUsbSetFeature(dev, port, FUSB_HUB_SEL_PORT_RESET, DR_PORT);
}

static void FUsbHubSetHubDepth(FUsbDev *const dev)
{
    FASSERT(dev);
    FUsbDevReq dr =
    {
        .bmRequestType = FUsbGenerateReqType(FUSB_REQ_HOST_TO_DEVICE,
                                             FUSB_REQ_TYPE_CLASS, FUSB_REQ_RECP_DEV),
        .bRequest = SET_HUB_DEPTH,
        .wValue = 0,
        .wIndex = 0,
        .wLength = 0,
    };
    FUsbDev *parent = dev;

    FASSERT(dev->controller);
    while (parent->hub > 0)
    {
        FASSERT(dev->controller->devices[parent->hub]);
        parent = dev->controller->devices[parent->hub];
        dr.wValue++;
    }

    FUsbTransCode ret = dev->controller->control(dev, FUSB_OUT, sizeof(dr), &dr, 0, NULL);
    if (ret < FUSB_CC_ZERO_BYTES)
    {
        FUSB_ERROR("Failed SET_HUB_DEPTH(%d) on hub %d: %d ",
                   dr.wValue, dev->address, ret);
    }

    return;
}

static const FUsbGenericHubOps FUSB_HUB_OPS =
{
    .hub_status_changed = NULL,
    .port_status_changed = FUsbHubPortStatusChange,
    .port_connected = FUsbHubPortConnected,
    .port_in_reset = FUsbHubPortInReset,
    .port_enabled = FUsbHubPortEnabled,
    .port_speed = FUsbHubPortSpeed,
    .enable_port = FUsbHubEnablePort,
    .disable_port = NULL,
    .start_port_reset = FUsbHubStartPortReset,
    .reset_port = FUsbGenericHubResetPort,
};

/* Clear CSC if set and enumerate port if it's connected regardless of change
   bits. Some broken hubs don't set CSC if already connected during reset. */
static void FUsbHubPortInit(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    unsigned short buf[FUSB_HUB_PORT_BUF_LEN]; /* Hub Status and Change Status */
    FUsbTransCode ret = FUsbGetStatus(dev, port, DR_PORT, sizeof(buf), buf);

    if (ret < FUSB_CC_ZERO_BYTES)
        return;

    if (buf[FUSB_HUB_PORT_CHANGE] & FUSB_HUB_STATUS_PORT_CONNECTION)
        FUsbClearFeature(dev, port, FUSB_HUB_SEL_C_PORT_CONNECTION, DR_PORT);

    if (buf[FUSB_HUB_PORT_STATUS] & FUSB_HUB_STATUS_PORT_CONNECTION)
    {
        FUSB_INFO("usbhub: Port coldplug at %d ", port);
        FUsbGenericHubScanPort(dev, port);
    }

    return;
}

/**
 * @name: FUsbHubHandlePortChange
 * @msg: Hub端口状态变化的处理回调函数
 * @return {*}
 * @param {FUsbDev} *dev
 * @param {int} port
 */
static FUsbTransCode FUsbHubHandlePortChange(FUsbDev *const dev, const int port)
{
    FASSERT(dev);
    static const struct
    {
        unsigned short change_bit;
        unsigned short clear_sel;
    } change_bits[] =
    {
        {FUSB_HUB_STATUS_PORT_CONNECTION, FUSB_HUB_SEL_C_PORT_CONNECTION},
        {FUSB_HUB_STATUS_PORT_ENABLE, FUSB_HUB_SEL_C_PORT_ENABLE},
        {FUSB_HUB_STATUS_PORT_SUSPEND, FUSB_HUB_SEL_C_PORT_SUSPEND},
        {FUSB_HUB_STATUS_PORT_OVER_CURRENT, FUSB_HUB_SEL_C_PORT_OVER_CURRENT},
        {FUSB_HUB_STATUS_PORT_RESET, FUSB_HUB_SEL_C_PORT_RESET},
        {FUSB_HUB_STATUS_BH_PORT_RESET, FUSB_HUB_SEL_C_BH_PORT_RESET},
        {FUSB_HUB_STATUS_PORT_LINK_STATE, FUSB_HUB_SEL_C_PORT_LINK_STATE},
        {FUSB_HUB_STATUS_PORT_PORT_CONFIG_ERROR, FUSB_HUB_SEL_C_PORT_CONFIG_ERROR},
    };

    FUsbTransCode ret = 0;
    unsigned int i;
    unsigned short checked_bits = 0;
    unsigned short buf[FUSB_HUB_PORT_BUF_LEN] = {0, 0}; /* Hub Status and Change Status */

    ret = FUsbGetStatus(dev, port, DR_PORT, sizeof(buf), buf);
    if (ret < FUSB_CC_ZERO_BYTES)
        return ret;

    /*
     * Second word holds the change bits. The interrupt transfer shows
     * a logical or of these bits, so we have to clear them all.
     */
    for (i = 0; i < ARRAY_SIZE(change_bits); ++i)
    {
        if (buf[FUSB_HUB_PORT_CHANGE] & change_bits[i].change_bit)
        {
            /* clear feature if specific change bit = 1 */
            FUsbClearFeature(dev, port, change_bits[i].clear_sel, DR_PORT);
        }

        checked_bits |= change_bits[i].change_bit;
    }

    if (buf[FUSB_HUB_PORT_CHANGE] & ~checked_bits)
        FUSB_DEBUG("Spurious change bit at port %d ", port);

    /* Now, handle connection changes. */
    if (buf[FUSB_HUB_PORT_CHANGE] & FUSB_HUB_STATUS_PORT_CONNECTION)
    {
        FUSB_DEBUG("Port change at %d ", port);
        ret = FUsbGenericHubScanPort(dev, port);
    }

    return ret;
}

/**
 * @name: FUsbHubPoll
 * @msg: 轮询Hub的所有端口,检查端口状态变化
 * @return {*}
 * @param {FUsbDev} *dev, Hub设备实例
 */
static void FUsbHubPoll(FUsbDev *const dev)
{
    FASSERT(dev);
    int port, i;
    u8 buf[32] = {0};
    const u8 *ibuf;

    /* First, gather all change bits from finished interrupt transfers. */
    const size_t port_bytes = min(ARRAY_SIZE(buf),
                                  (size_t)DIV_ROUND_UP(FUSB_GEN_HUB_GET(dev)->num_ports + 1, 8));

    while (NULL != (ibuf = dev->controller->poll_intr_queue(FUSB_GEN_HUB_GET(dev)->data)))
    {
        for (i = 0; (size_t)i < port_bytes; ++i)
            buf[i] |= ibuf[i];
    }

    for (port = 1; port <= FUSB_GEN_HUB_GET(dev)->num_ports; ++port)
    {
        /* ports start at bit1; bit0 is hub status change */
        if (buf[port / 8] & (1 << (port % 8)))
        {
            if (FUsbHubHandlePortChange(dev, port) < 0)
                return;
        }
    }

    return;
}

/**
 * @name: FUsbHubDestory
 * @msg: USB Hub的去初始化函数
 * @return {*}
 * @param {FUsbDev} *dev, Hub设备实例
 */
static void FUsbHubDestory(FUsbDev *const dev)
{
    FASSERT(dev);
    FUsbEndpoint *const intr_ep = FUsbHubIntrEp(dev);
    FASSERT(intr_ep); /* interrupt ep must exists */
    dev->controller->destroy_intr_queue(intr_ep, FUSB_GEN_HUB_GET(dev)->data);
    FUsbGenericHubDestory(dev);
}

/**
 * @name: FUsbHubInit
 * @msg: USB Hub的初始化函数,由应用程序注册到FUSB框架中
 * @return {*}
 * @param {FUsbDev} *dev, Hub设备实例
 */
void FUsbHubInit(FUsbDev *dev)
{
    FASSERT(dev);
    FUsbEndpoint *const intr_ep = FUsbHubIntrEp(dev); /* get the first intrrupt ep found */
    if (NULL == intr_ep)
    {
        FUSB_ERROR("No interrupt-in endpoint found ");
        return;
    }

    FASSERT(dev->controller);

    /* Get number of ports from hub descriptor */
    int type = FUsbIsSuperSpeed(dev->speed) ? FUSB_DESC_TYPE_SUPER_SPEED_HUB : FUSB_DESC_TYPE_HUB; /* similar enough */
    FUsbHubDescriptor desc;                                /* won't fit the whole thing, we don't care */
    if (FUsbGetDescriptor(dev, FUsbGenerateReqType(FUSB_REQ_DEVICE_TO_HOST, FUSB_REQ_TYPE_CLASS, FUSB_REQ_RECP_DEV), type, 0, &desc, sizeof(desc)) != sizeof(desc))
    {
        FUSB_ERROR("FUsbGetDescriptor(HUB) failed ");
        FUsbDetachDev(dev->controller, dev->address);
        return;
    }

    if (FUsbIsSuperSpeed(dev->speed))
    {
        FUsbHubSetHubDepth(dev);
    }

    /*
     * Register interrupt transfer:
     *   one bit per port + one bit for the hub,
     *   20 transfers in the queue, like our HID driver,
     *   one transfer per 256ms
     */
    void *const intrq = dev->controller->create_intr_queue(
                            intr_ep, intr_ep->maxpacketsize, 20, 256);
    if (NULL == intrq)
    {
        FUsbDetachDev(dev->controller, dev->address);
        return;
    }

    /*
     * Limit the number of ports by the max packet size of
     * the interrupt endpoint. This shouldn't be necessary
     * but prevents a potential overflow in FUsbHubPoll().
     */
    const unsigned int num_ports =
        min((int)desc.bNbrPorts, intr_ep->maxpacketsize * 8 - 1);
    if (FUsbGenericHubInit(dev, num_ports, &FUSB_HUB_OPS))
    {
        dev->controller->destroy_intr_queue(intr_ep, intrq);
        FUsbDetachDev(dev->controller, dev->address);
        return;
    }

    unsigned int port;
    for (port = 1; port <= num_ports; ++port)
    {
        FUsbHubPortInit(dev, port);
    }

    FUSB_GEN_HUB_GET(dev)->data = intrq;
    dev->poll = FUsbHubPoll;
    dev->destroy = FUsbHubDestory;

    return;
}