drv_usbhost.c 22.0 KB
Newer Older
W
Wayne Lin 已提交
1 2 3 4 5 6 7 8 9
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author           Notes
* 2020-5-4        CHChen           First version
10
* 2021-11-5       Wayne            Revise
W
Wayne Lin 已提交
11 12 13 14 15 16 17 18
*
******************************************************************************/
#include <rtconfig.h>

#if defined(BSP_USING_USBH)

#include <rtdevice.h>
#include <rthw.h>
W
Wayne Lin 已提交
19
#include "NuMicro.h"
W
Wayne Lin 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33

#include "usb.h"
#include "usbh_lib.h"

#if !defined(NU_USBHOST_HUB_POLLING_INTERVAL)
    #define NU_USBHOST_HUB_POLLING_INTERVAL    (100)
#endif

#define NU_MAX_USBH_PORT    1        //USB1.1 port
#define NU_MAX_USBH_PIPE    16
#define NU_USBH_THREAD_STACK_SIZE    2048

#define NU_MAX_USBH_HUB_PORT_DEV    USB_HUB_PORT_NUM

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#define NU_USBHOST_HUB_POLLING_LOCK
#if defined(NU_USBHOST_HUB_POLLING_LOCK)
#define NU_USBHOST_MUTEX_INIT()      { \
                                s_sUSBHDev.lock = rt_mutex_create("usbhost_lock", RT_IPC_FLAG_PRIO); \
                                RT_ASSERT(s_sUSBHDev.lock != RT_NULL); \
                            }

#define NU_USBHOST_LOCK()      { \
                                rt_err_t result = rt_mutex_take(s_sUSBHDev.lock, RT_WAITING_FOREVER); \
                                RT_ASSERT(result == RT_EOK); \
                            }

#define NU_USBHOST_UNLOCK()    { \
                                rt_err_t result = rt_mutex_release(s_sUSBHDev.lock); \
                                RT_ASSERT(result == RT_EOK); \
                            }
#else
#define NU_USBHOST_MUTEX_INIT()
#define NU_USBHOST_LOCK()
#define NU_USBHOST_UNLOCK()
#endif

W
Wayne Lin 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/* Private typedef --------------------------------------------------------------*/
typedef struct nu_port_dev
{
    rt_bool_t bRHParent;
    UDEV_T *pUDev;
    EP_INFO_T *apsEPInfo[NU_MAX_USBH_PIPE];
    struct urequest asSetupReq[NU_MAX_USBH_PIPE];
    struct rt_completion utr_completion;
    int port_num;
    rt_bool_t bEnumDone;
} S_NU_PORT_DEV;


typedef struct nu_port_ctrl
{
    S_NU_PORT_DEV sRHPortDev;
    S_NU_PORT_DEV asHubPortDev[NU_MAX_USBH_HUB_PORT_DEV];
} S_NU_RH_PORT_CTRL;


struct nu_usbh_dev
{
78
    struct uhcd uhcd;
W
Wayne Lin 已提交
79
    rt_thread_t polling_thread;
80
    rt_mutex_t  lock;
W
Wayne Lin 已提交
81 82 83 84
    S_NU_RH_PORT_CTRL asPortCtrl[NU_MAX_USBH_PORT];
};

/* Private variables ------------------------------------------------------------*/
85
static struct nu_usbh_dev s_sUSBHDev;
W
Wayne Lin 已提交
86 87 88

static S_NU_RH_PORT_CTRL *
GetRHPortControlFromPipe(
89
    upipe_t pipe)
W
Wayne Lin 已提交
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
{
    uinst_t inst;
    int port;
    if (pipe->inst->parent_hub->is_roothub)
    {
        //case: device ---> root hub
        inst = pipe->inst;
        port = inst->port;
    }
    else
    {
        //case: device ---> hub ---> root hub
        inst = pipe->inst->parent_hub->self;
        port = inst->port;
    }

    if (port > NU_MAX_USBH_PORT)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_open_pipe ERROR: port index over NU_MAX_USBH_PORT\n"));
        return RT_NULL;
    }

    return &s_sUSBHDev.asPortCtrl[port - 1];;
}

static S_NU_PORT_DEV *
GetPortDevFromPipe(
117
    upipe_t pipe)
W
Wayne Lin 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
    S_NU_RH_PORT_CTRL *psRHPortCtrl = GetRHPortControlFromPipe(pipe);
    int i;

    if (psRHPortCtrl == RT_NULL)
        return RT_NULL;

    if (pipe->inst->parent_hub->is_roothub)
    {
        //case: device ---> root hub
        return &psRHPortCtrl->sRHPortDev;
    }

    //case: device ---> hub ---> root hub
    for (i = 0 ; i < NU_MAX_USBH_HUB_PORT_DEV; i ++)
    {
        if (psRHPortCtrl->asHubPortDev[i].port_num == pipe->inst->port)
            break;
    }

    if (i >= NU_MAX_USBH_HUB_PORT_DEV)
        return RT_NULL;
140

W
Wayne Lin 已提交
141 142 143
    return &psRHPortCtrl->asHubPortDev[i];
}

144
static rt_err_t nu_reset_port(rt_uint8_t port)
W
Wayne Lin 已提交
145
{
146 147 148
    S_NU_RH_PORT_CTRL *psPortCtrl;

    if (port > NU_MAX_USBH_PORT)
W
Wayne Lin 已提交
149
    {
150 151
        RT_DEBUG_LOG(RT_DEBUG_USB, ("%s ERROR: port index over NU_MAX_USBH_PORT\n", __func__));
        return RT_EIO;
W
Wayne Lin 已提交
152 153
    }

154 155 156 157 158 159
    psPortCtrl = &s_sUSBHDev.asPortCtrl[port - 1];
    if (psPortCtrl->sRHPortDev.pUDev == NULL)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("%s ERROR: udev not found\n", __func__));
        return RT_EIO;
    }
W
Wayne Lin 已提交
160

161
    usbh_reset_port(psPortCtrl->sRHPortDev.pUDev);
W
Wayne Lin 已提交
162

163
    return RT_EOK;
W
Wayne Lin 已提交
164 165 166 167 168
}

static EP_INFO_T *GetFreePipe(
    S_NU_RH_PORT_CTRL *psPortCtrl,
    S_NU_PORT_DEV *psPortDev,
169
    rt_uint8_t *pu8PipeIndex)
W
Wayne Lin 已提交
170
{
171
    if (psPortCtrl != NULL)
W
Wayne Lin 已提交
172
    {
173 174 175 176 177 178 179
        int i;
        /* Find free Pipe */
        for (i = 0; i < NU_MAX_USBH_PIPE; i ++)
        {
            if (psPortDev->apsEPInfo[i] == NULL)
                break;
        }
W
Wayne Lin 已提交
180

181 182 183 184 185 186 187 188 189 190 191 192
        if (i < NU_MAX_USBH_PIPE)
        {
            EP_INFO_T *psEPInfo = rt_malloc(sizeof(EP_INFO_T));
            if (psEPInfo != RT_NULL)
            {
                psPortDev->apsEPInfo[i] = psEPInfo;
                *pu8PipeIndex = i;
                return psEPInfo;
            }
        }
    }
    return RT_NULL;
W
Wayne Lin 已提交
193 194 195 196 197
}

static void FreePipe(
    S_NU_RH_PORT_CTRL *psPortCtrl,
    S_NU_PORT_DEV *psPortDev,
198
    rt_uint8_t u8PipeIndex)
W
Wayne Lin 已提交
199
{
200 201 202
    if ((psPortCtrl != RT_NULL) &&
            (u8PipeIndex < NU_MAX_USBH_PIPE) &&
            (psPortDev->apsEPInfo[u8PipeIndex] != RT_NULL))
W
Wayne Lin 已提交
203 204 205 206 207 208
    {
        rt_free(psPortDev->apsEPInfo[u8PipeIndex]);
        psPortDev->apsEPInfo[u8PipeIndex] = RT_NULL;
    }
}

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
static  S_NU_PORT_DEV *
AllocateNewUDev(
    S_NU_RH_PORT_CTRL *psRHPortCtrl)
{
    if (psRHPortCtrl != RT_NULL)
    {
        int i;
        /* Find free Dev */
        for (i = 0 ; i < NU_MAX_USBH_HUB_PORT_DEV; i ++)
        {
            if (psRHPortCtrl->asHubPortDev[i].pUDev == NULL)
                break;
        }

        if (i < NU_MAX_USBH_HUB_PORT_DEV)
        {
            psRHPortCtrl->asHubPortDev[i].pUDev = alloc_device();
            if (psRHPortCtrl->asHubPortDev[i].pUDev == NULL)
            {
                return RT_NULL;
            }
            else
            {
                return &psRHPortCtrl->asHubPortDev[i];
            }
        }
    }
    return RT_NULL;
}

static rt_err_t nu_open_pipe(upipe_t pipe)
W
Wayne Lin 已提交
240 241
{
    S_NU_RH_PORT_CTRL *psPortCtrl;
242
    S_NU_PORT_DEV *psPortDev;
W
Wayne Lin 已提交
243

244 245
    psPortCtrl = GetRHPortControlFromPipe(pipe);
    if (psPortCtrl == RT_NULL)
W
Wayne Lin 已提交
246
    {
247 248
        RT_DEBUG_LOG(RT_DEBUG_USB, ("%s ERROR: RHPort not found\n", __func__));
        goto exit_nu_open_pipe;
W
Wayne Lin 已提交
249 250 251 252
    }

    if (psPortCtrl->sRHPortDev.pUDev == NULL)
    {
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
        RT_DEBUG_LOG(RT_DEBUG_USB, ("%s ERROR: udev not found\n", __func__));
        goto exit_nu_open_pipe;
    }

    psPortDev = GetPortDevFromPipe(pipe);

    if ((psPortDev == NULL) || (psPortDev->pUDev == NULL))
    {
        //allocate new dev for hub device
        psPortDev = AllocateNewUDev(psPortCtrl);

        if (psPortDev == RT_NULL)
        {
            RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_open_pipe ERROR: udev allocate failed\n"));
            goto exit_nu_open_pipe;
        }

        if (pipe->inst->speed)
        {
            psPortDev->pUDev->speed = SPEED_FULL;
        }
        else
        {
            psPortDev->pUDev->speed = SPEED_HIGH;
        }

        psPortDev->pUDev->parent = NULL;
        psPortDev->pUDev->hc_driver = psPortCtrl->sRHPortDev.pUDev->hc_driver;
        psPortDev->port_num = pipe->inst->port;
        psPortDev->pUDev->port_num = pipe->inst->port;
        psPortDev->bEnumDone = FALSE;
    }

    //For ep0 control transfer
    if ((pipe->ep.bEndpointAddress & 0x7F) == 0)
    {
        pipe->pipe_index = 0;
    }
    else
    {
        int  pksz;
        EP_INFO_T *psEPInfo = GetFreePipe(psPortCtrl, psPortDev, &pipe->pipe_index);
        if (psEPInfo == RT_NULL)
        {
            RT_DEBUG_LOG(RT_DEBUG_USB, ("%s ERROR: get free pipe failed\n", __func__));
            goto exit_nu_open_pipe;
        }

        psEPInfo->bEndpointAddress = pipe->ep.bEndpointAddress;
        psEPInfo->bmAttributes = pipe->ep.bmAttributes;

        pksz = pipe->ep.wMaxPacketSize;
        pksz = (pksz & 0x07ff) * (1 + ((pksz >> 11) & 3));
        psEPInfo->wMaxPacketSize = pksz;

        psEPInfo->bInterval = pipe->ep.bInterval;
        psEPInfo->hw_pipe = NULL;
        psEPInfo->bToggle = 0;
    }


    return RT_EOK;

exit_nu_open_pipe:

    return -RT_ERROR;
}

static rt_err_t nu_close_pipe(upipe_t pipe)
{
    S_NU_RH_PORT_CTRL *psPortCtrl;
    S_NU_PORT_DEV *psPortDev;

    psPortCtrl = GetRHPortControlFromPipe(pipe);
    if (psPortCtrl == RT_NULL)
    {
W
Wayne Lin 已提交
329 330 331
        return RT_EIO;
    }

332
    psPortDev = GetPortDevFromPipe(pipe);
W
Wayne Lin 已提交
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
    //For ep0 control transfer
    if ((pipe->ep.bEndpointAddress & 0x7F) == 0)
    {
        if ((psPortDev) && (psPortDev->bRHParent == FALSE) && (psPortDev->bEnumDone == TRUE))
        {
            if (psPortDev->pUDev)
            {
                int i;
                for (i = 0; i < NU_MAX_USBH_PIPE; i++)
                {
                    if (psPortDev->apsEPInfo[i] != NULL)
                    {
                        usbh_quit_xfer(psPortDev->pUDev, psPortDev->apsEPInfo[i]);
                    }
                }

                free_device(psPortDev->pUDev);
                psPortDev->pUDev = NULL;
            }
        }
    }

    if (psPortDev != NULL)
    {
        FreePipe(psPortCtrl, psPortDev, pipe->pipe_index);
    }
W
Wayne Lin 已提交
360 361 362 363 364 365 366
    return RT_EOK;
}

static int nu_ctrl_xfer(
    S_NU_PORT_DEV *psPortDev,
    struct urequest *psSetup,
    void *buffer,
367
    int timeouts)
W
Wayne Lin 已提交
368
{
369
    uint32_t  xfer_len = 0;
W
Wayne Lin 已提交
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
    int    ret;

    ret = usbh_ctrl_xfer(psPortDev->pUDev, psSetup->request_type, psSetup->bRequest, psSetup->wValue, psSetup->wIndex, psSetup->wLength, buffer, &xfer_len, timeouts * 10);
    if (ret < 0)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_ctrl_xfer ERROR: xfer failed %d\n", ret));
        return ret;
    }

    if (xfer_len != psSetup->wLength)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_ctrl_xfer ERROR: xfer length %d %d\n", psSetup->wLength, xfer_len));
    }

    if ((psSetup->bRequest == USB_REQ_SET_ADDRESS) && ((psSetup->request_type & 0x60) == REQ_TYPE_STD_DEV))
        psPortDev->pUDev->dev_num = psSetup->wValue;

    if ((psSetup->bRequest == USB_REQ_SET_CONFIGURATION) && ((psSetup->request_type & 0x60) == REQ_TYPE_STD_DEV))
    {
        psPortDev->pUDev->cur_conf = psSetup->wValue;
        psPortDev->bEnumDone = TRUE;
    }

    return xfer_len;
}

static int nu_bulk_xfer(
    S_NU_PORT_DEV *psPortDev,
    UTR_T *psUTR,
399
    int timeouts)
W
Wayne Lin 已提交
400
{
401
    int ret = usbh_bulk_xfer(psUTR);
W
Wayne Lin 已提交
402 403 404 405
    if (ret < 0)
        return ret;

    //wait transfer done
406 407 408 409 410 411 412 413 414 415 416 417 418
    if (rt_completion_wait(&(psPortDev->utr_completion), timeouts) < 0)
    {
        rt_kprintf("Request Timeout in %d ms!! (bulk_xfer)\n", timeouts);

        rt_kprintf("psUTR->buff: %08x\n", psUTR->buff);
        rt_kprintf("psUTR->data_len: %d\n", psUTR->data_len);
        rt_kprintf("psUTR->xfer_len: %d\n", psUTR->xfer_len);
        rt_kprintf("psUTR->ep: %08x\n", psUTR->ep);
        rt_kprintf("psUTR->bIsTransferDone: %08x\n", psUTR->bIsTransferDone);
        rt_kprintf("psUTR->status: %08x\n", psUTR->status);
        rt_kprintf("psUTR->td_cnt: %08x\n", psUTR->td_cnt);
        return -1;
    }
W
Wayne Lin 已提交
419 420 421 422 423 424 425
    return 0;
}

static int nu_int_xfer(
    upipe_t pipe,
    S_NU_PORT_DEV *psPortDev,
    UTR_T *psUTR,
426
    int timeouts)
W
Wayne Lin 已提交
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
{
    int ret;
    int retry = 3;

    while (retry > 0)
    {
        ret = usbh_int_xfer(psUTR);
        if (ret == 0)
            break;

        RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_int_xfer ERROR: failed to submit interrupt request\n"));
        rt_thread_delay((pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) > 0 ? (pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) : 1);
        retry --;
    }

    if (ret < 0)
        return ret;

    return 0;
}

static void xfer_done_cb(UTR_T *psUTR)
{
    S_NU_PORT_DEV *psPortDev = (S_NU_PORT_DEV *)psUTR->context;

    //transfer done, signal utr_completion
    rt_completion_done(&(psPortDev->utr_completion));
}

static void int_xfer_done_cb(UTR_T *psUTR)
{
    upipe_t pipe = (upipe_t)psUTR->context;

    if (psUTR->status != 0)
    {
462 463
        RT_DEBUG_LOG(RT_DEBUG_USB, ("Interrupt xfer failed %d\n", psUTR->status));
        goto exit_int_xfer_done_cb;
W
Wayne Lin 已提交
464 465 466 467 468 469 470 471 472 473 474
    }

    if (pipe->callback != RT_NULL)
    {
        struct uhost_msg msg;
        msg.type = USB_MSG_CALLBACK;
        msg.content.cb.function = pipe->callback;
        msg.content.cb.context = pipe;
        rt_usbh_event_signal(&msg);
    }

475 476
exit_int_xfer_done_cb:

W
Wayne Lin 已提交
477 478 479 480 481 482 483
    free_utr(psUTR);
}

static int nu_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeouts)
{
    S_NU_RH_PORT_CTRL *psPortCtrl;
    S_NU_PORT_DEV *psPortDev;
484 485 486 487 488 489
    UTR_T *psUTR = NULL;
    int i32XferLen = -1;

    void *buffer_nonch = buffer;

    NU_USBHOST_LOCK();
W
Wayne Lin 已提交
490 491 492 493

    psPortCtrl = GetRHPortControlFromPipe(pipe);
    if (psPortCtrl == RT_NULL)
    {
494
        goto exit_nu_pipe_xfer;
W
Wayne Lin 已提交
495 496 497 498 499 500
    }

    psPortDev = GetPortDevFromPipe(pipe);
    if (psPortDev->pUDev == NULL)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: udev not found\n"));
501
        goto exit_nu_pipe_xfer;
W
Wayne Lin 已提交
502 503 504 505 506
    }

    //ctrl xfer
    if (pipe->ep.bmAttributes == USB_EP_ATTR_CONTROL)
    {
507 508
        int ret;

W
Wayne Lin 已提交
509 510
        if (token == USBH_PID_SETUP)
        {
511 512
            struct urequest *psSetup = (struct urequest *)buffer_nonch;
            RT_ASSERT(buffer_nonch != RT_NULL);
W
Wayne Lin 已提交
513

514
            /* Read data from USB device. */
W
Wayne Lin 已提交
515 516 517 518 519 520 521
            if (psSetup->request_type & USB_REQ_TYPE_DIR_IN)
            {
                //Store setup request
                rt_memcpy(&psPortCtrl->asHubPortDev->asSetupReq[pipe->pipe_index], psSetup, sizeof(struct urequest));
            }
            else
            {
522 523 524 525 526
                /* Write data to USB device. */
                //Trigger USBHostLib Ctrl_Xfer
                ret = nu_ctrl_xfer(psPortDev, psSetup, NULL, timeouts);
                if (ret != psSetup->wLength)
                    goto exit_nu_pipe_xfer;
W
Wayne Lin 已提交
527 528 529 530 531
            }
        }
        else
        {
            //token == USBH_PID_DATA
532
            if (buffer_nonch && ((pipe->ep.bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN))
W
Wayne Lin 已提交
533
            {
534
                /* Read data from USB device. */
W
Wayne Lin 已提交
535
                //Trigger USBHostLib Ctril_Xfer
536 537 538
                ret = nu_ctrl_xfer(psPortDev, &psPortCtrl->asHubPortDev->asSetupReq[pipe->pipe_index], buffer_nonch, timeouts);
                if (ret != nbytes)
                    goto exit_nu_pipe_xfer;
W
Wayne Lin 已提交
539 540 541
            }
            else
            {
542
                RT_DEBUG_LOG(RT_DEBUG_USB, ("%d == USBH_PID_DATA, nil buf-%d \n", token, nbytes));
W
Wayne Lin 已提交
543 544
            }

545 546 547 548 549
        } //else
        i32XferLen = nbytes;
        goto exit_nu_pipe_xfer;
    } // if ( pipe->ep.bmAttributes == USB_EP_ATTR_CONTROL )
    else
W
Wayne Lin 已提交
550 551
    {

552
        psUTR = alloc_utr(psPortDev->pUDev);
W
Wayne Lin 已提交
553

554
        if (!psUTR)
W
Wayne Lin 已提交
555
        {
556 557
            RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: unable alloc UTR\n"));
            goto exit_nu_pipe_xfer;
W
Wayne Lin 已提交
558 559
        }

560 561 562 563 564 565 566 567 568 569 570
        psUTR->ep = psPortDev->apsEPInfo[pipe->pipe_index];
        psUTR->buff = buffer_nonch;
        psUTR->data_len = nbytes;
        psUTR->xfer_len = 0;
        psUTR->func = xfer_done_cb;
        psUTR->context = psPortDev;
        psUTR->bIsTransferDone = 0;
        psUTR->status = 0;

        //others xfer
        rt_completion_init(&(psPortDev->utr_completion));
W
Wayne Lin 已提交
571

572
        if (pipe->ep.bmAttributes == USB_EP_ATTR_BULK)
W
Wayne Lin 已提交
573
        {
574 575 576 577 578
            if (nu_bulk_xfer(psPortDev, psUTR, timeouts) < 0)
            {
                RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: bulk transfer failed\n"));
                goto failreport_nu_pipe_xfer;
            }
W
Wayne Lin 已提交
579
        }
580 581 582 583
        else if (pipe->ep.bmAttributes == USB_EP_ATTR_INT)
        {
            psUTR->func = int_xfer_done_cb;
            psUTR->context = pipe;
W
Wayne Lin 已提交
584

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
            if (nu_int_xfer(pipe, psPortDev, psUTR, timeouts) < 0)
            {
                RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: int transfer failed\n"));
                //goto exit_nu_pipe_xfer;
            }
            else
            {
                i32XferLen = nbytes;
            }
            goto exit2_nu_pipe_xfer;
        }
        else if (pipe->ep.bmAttributes == USB_EP_ATTR_ISOC)
        {
            //TODO: ISO transfer
            RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: isoc transfer not support\n"));
            goto exit_nu_pipe_xfer;
        }
W
Wayne Lin 已提交
602

603 604 605
    } //else

failreport_nu_pipe_xfer:
W
Wayne Lin 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

    if (psUTR->bIsTransferDone == 0)
    {
        //Timeout
        RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: timeout\n"));
        pipe->status = UPIPE_STATUS_ERROR;
        usbh_quit_utr(psUTR);
    }
    else
    {
        // Transfer Done. Get status
        if (psUTR->status == 0)
        {
            pipe->status = UPIPE_STATUS_OK;
        }
        else if (psUTR->status == USBH_ERR_STALL)
        {
            pipe->status = UPIPE_STATUS_STALL;
        }
        else
        {
            pipe->status = UPIPE_STATUS_ERROR;
        }
    }

    i32XferLen = psUTR->xfer_len;

    //Call callback
    if (pipe->callback != RT_NULL)
    {
636
        pipe->callback(pipe);
W
Wayne Lin 已提交
637 638
    }

639
exit_nu_pipe_xfer:
W
Wayne Lin 已提交
640

641 642
    if (psUTR)
        free_utr(psUTR);
W
Wayne Lin 已提交
643

644
exit2_nu_pipe_xfer:
W
Wayne Lin 已提交
645 646


647
    NU_USBHOST_UNLOCK();
W
Wayne Lin 已提交
648

649
    return i32XferLen;
W
Wayne Lin 已提交
650 651
}

652
/* Polling USB root hub status task */
W
Wayne Lin 已提交
653 654 655 656
static void nu_usbh_rh_thread_entry(void *parameter)
{
    while (1)
    {
657
        NU_USBHOST_LOCK();
W
Wayne Lin 已提交
658
        usbh_polling_root_hubs();
659 660
        NU_USBHOST_UNLOCK();

W
Wayne Lin 已提交
661 662 663 664 665 666
        rt_thread_mdelay(NU_USBHOST_HUB_POLLING_INTERVAL);
    }
}

static void nu_hcd_connect_callback(
    struct udev_t *udev,
667
    int param)
W
Wayne Lin 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
{
    int i;
    int port_index;
    S_NU_RH_PORT_CTRL *psPortCtrl;

    for (i = 0; i < NU_MAX_USBH_PORT; i++)
    {
        psPortCtrl = &s_sUSBHDev.asPortCtrl[i];
        if (psPortCtrl->sRHPortDev.pUDev == NULL)
            break;
    }

    if (i >= NU_MAX_USBH_PORT)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("ERROR: port connect slot is full\n"));
        return;
    }

    port_index = i + 1;
    psPortCtrl->sRHPortDev.pUDev = udev;
    psPortCtrl->sRHPortDev.bRHParent = TRUE;
689

W
Wayne Lin 已提交
690
    RT_DEBUG_LOG(RT_DEBUG_USB, ("usb connected\n"));
691 692 693 694 695

    if (udev->speed == SPEED_HIGH)
        rt_usbh_root_hub_connect_handler(&s_sUSBHDev.uhcd, port_index, RT_TRUE);
    else
        rt_usbh_root_hub_connect_handler(&s_sUSBHDev.uhcd, port_index, RT_FALSE);
W
Wayne Lin 已提交
696 697 698 699
}

static void nu_hcd_disconnect_callback(
    struct udev_t *udev,
700
    int param)
W
Wayne Lin 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
{
    int i;
    int port_index;
    S_NU_RH_PORT_CTRL *psPortCtrl;

    for (i = 0; i < NU_MAX_USBH_PORT; i++)
    {
        psPortCtrl = &s_sUSBHDev.asPortCtrl[i];
        if (psPortCtrl->sRHPortDev.pUDev == udev)
            break;
    }

    if (i >= NU_MAX_USBH_PORT)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("ERROR: udev not found\n"));
        return;
    }

    port_index = i + 1;

    for (i = 0; i < NU_MAX_USBH_PIPE; i++)
    {
        if (psPortCtrl->sRHPortDev.apsEPInfo[i] != NULL)
        {
            usbh_quit_xfer(psPortCtrl->sRHPortDev.pUDev, psPortCtrl->sRHPortDev.apsEPInfo[i]);
        }
    }

    psPortCtrl->sRHPortDev.pUDev = NULL;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("usb disconnect\n"));
732 733

    rt_usbh_root_hub_disconnect_handler(&s_sUSBHDev.uhcd, port_index);
W
Wayne Lin 已提交
734 735
}

736

W
Wayne Lin 已提交
737 738 739 740 741 742 743 744 745 746 747 748
/* USB host operations -----------------------------------------------------------*/
static struct uhcd_ops nu_uhcd_ops =
{
    nu_reset_port,
    nu_pipe_xfer,
    nu_open_pipe,
    nu_close_pipe,
};

static rt_err_t nu_hcd_init(rt_device_t device)
{
    struct nu_usbh_dev *pNuUSBHDev = (struct nu_usbh_dev *)device;
749

W
Wayne Lin 已提交
750 751 752 753 754 755 756 757 758
    usbh_core_init();

    //install connect/disconnect callback
    usbh_install_conn_callback(nu_hcd_connect_callback, nu_hcd_disconnect_callback);

    //create thread for polling usbh port status
    /* create usb hub thread */
    pNuUSBHDev->polling_thread = rt_thread_create("usbh_drv", nu_usbh_rh_thread_entry, RT_NULL,
                                 NU_USBH_THREAD_STACK_SIZE, 8, 20);
759 760 761 762
    RT_ASSERT(pNuUSBHDev->polling_thread != RT_NULL);

    /* startup usb host thread */
    rt_thread_startup(pNuUSBHDev->polling_thread);
W
Wayne Lin 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840

    return RT_EOK;
}

/* global function for USB host library -----------------------------*/
uint32_t usbh_get_ticks(void)
{
    return rt_tick_get();
}

void usbh_delay_ms(int msec)
{
    rt_thread_mdelay(msec);
}

uint32_t usbh_tick_from_millisecond(uint32_t msec)
{
    return rt_tick_from_millisecond(msec);
}

#if defined(RT_USING_PM)

/* device pm suspend() entry. */
static int usbhost_pm_suspend(const struct rt_device *device, rt_uint8_t mode)
{
    rt_err_t result;

    struct nu_usbh_dev *pNuUSBHDev = (struct nu_usbh_dev *)device;

    RT_ASSERT(pNuUSBHDev != RT_NULL);
    switch (mode)
    {
    case PM_SLEEP_MODE_LIGHT:
    case PM_SLEEP_MODE_DEEP:

        pNuUSBHDev->polling_thread->stat = RT_THREAD_READY;
        result = rt_thread_suspend(pNuUSBHDev->polling_thread);
        RT_ASSERT(result == RT_EOK);

        break;

    default:
        break;
    }

    return (int)RT_EOK;
}

/* device pm resume() entry. */
static void usbhost_pm_resume(const struct rt_device *device, rt_uint8_t mode)
{
    rt_err_t result;
    struct nu_usbh_dev *pNuUSBHDev = (struct nu_usbh_dev *)device;
    RT_ASSERT(pNuUSBHDev != RT_NULL);

    switch (mode)
    {
    case PM_SLEEP_MODE_LIGHT:
    case PM_SLEEP_MODE_DEEP:
        result = rt_thread_resume(pNuUSBHDev->polling_thread);
        RT_ASSERT(result == RT_EOK);
        break;

    default:
        break;
    }
}

static struct rt_device_pm_ops device_pm_ops =
{
    .suspend = usbhost_pm_suspend,
    .resume = usbhost_pm_resume,
    .frequency_change = RT_NULL
};
#endif

int nu_usbh_register(void)
{
841 842 843 844 845 846 847 848 849 850 851
    rt_err_t res;
    uhcd_t psUHCD;

    psUHCD = (uhcd_t)&s_sUSBHDev.uhcd;

    psUHCD->parent.type       = RT_Device_Class_USBHost;
    psUHCD->parent.init       = nu_hcd_init;
    psUHCD->parent.user_data  = &s_sUSBHDev;

    psUHCD->ops               = &nu_uhcd_ops;
    psUHCD->num_ports         = NU_MAX_USBH_PORT;
W
Wayne Lin 已提交
852 853 854 855 856 857 858 859 860 861 862

#if !defined(BSP_USING_OTG)
    SYS_UnlockReg();

    /* Set OTG as USB Host role */
    SYS->USBPHY = SYS_USBPHY_OTGPHYEN_Msk | SYS_USBPHY_SBO_Msk | (0x1 << SYS_USBPHY_USBROLE_Pos);

    SYS_LockReg();
#endif


863
    NU_USBHOST_MUTEX_INIT();
W
Wayne Lin 已提交
864

865
    res = rt_device_register(&psUHCD->parent, "usbh", RT_DEVICE_FLAG_DEACTIVATE);
W
Wayne Lin 已提交
866 867 868
    RT_ASSERT(res == RT_EOK);

    /*initialize the usb host function */
G
guozhanxin 已提交
869
    res = rt_usb_host_init("usbh");
W
Wayne Lin 已提交
870 871 872
    RT_ASSERT(res == RT_EOK);

#if defined(RT_USING_PM)
873
    rt_pm_device_register(&psUHCD->parent, &device_pm_ops);
W
Wayne Lin 已提交
874 875
#endif

876
    return 0;
W
Wayne Lin 已提交
877 878 879 880
}
INIT_DEVICE_EXPORT(nu_usbh_register);

#endif