core.c 40.0 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5
/*
 * File      : core.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2012, RT-Thread Development Team
 *
Y
yiyue.fang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
M
Ming, Bai 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-10-01     Yi Qiu       first version
 * 2012-12-12     heyuanjie87  change endpoint and class handler
 * 2012-12-30     heyuanjie87  change inferface handler
 */

#include <rtthread.h>
#include <rtdevice.h>

static rt_list_t device_list;

/**
 * This function will handle get_device_descriptor request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
40
static rt_err_t _get_device_descriptor(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
41 42
{
    rt_size_t size;
43

M
Ming, Bai 已提交
44 45 46 47 48 49 50
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));

    /* device descriptor length should less than USB_DESC_LENGTH_DEVICE*/
51 52
    size = (setup->length > USB_DESC_LENGTH_DEVICE) ?
           USB_DESC_LENGTH_DEVICE : setup->length;
M
Ming, Bai 已提交
53 54

    /* send device descriptor to endpoint 0 */
55
    dcd_ep_write(device->dcd, 0, (rt_uint8_t *)&device->dev_desc, size);
M
Ming, Bai 已提交
56 57 58 59 60 61 62 63 64 65 66 67

    return RT_EOK;
}

/**
 * This function will handle get_config_descriptor request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
68
static rt_err_t _get_config_descriptor(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
69 70 71 72 73 74 75 76 77 78 79
{
    rt_size_t size;
    ucfg_desc_t cfg_desc;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));

    cfg_desc = &device->curr_cfg->cfg_desc;
80 81
    size = (setup->length > cfg_desc->wTotalLength) ?
           cfg_desc->wTotalLength : setup->length;
M
Ming, Bai 已提交
82 83

    /* send configuration descriptor to endpoint 0 */
84
    dcd_ep_write(device->dcd, 0, (rt_uint8_t *)cfg_desc, size);
85

M
Ming, Bai 已提交
86 87 88 89 90 91 92 93 94 95 96
    return RT_EOK;
}

/**
 * This function will handle get_string_descriptor request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful, -RT_ERROR on invalid request.
 */
97
static rt_err_t _get_string_descriptor(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111
{
    struct ustring_descriptor str_desc;
    rt_uint8_t index, i;
    rt_uint32_t len;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));

    str_desc.type = USB_DESC_TYPE_STRING;
    index = setup->value & 0xFF;

112
    if (index > USB_STRING_INTERFACE_INDEX)
M
Ming, Bai 已提交
113 114 115
    {
        rt_kprintf("unknown string index\n");
        dcd_ep_stall(device->dcd, 0);
116

M
Ming, Bai 已提交
117
        return -RT_ERROR;
118
    }
119
    if (index == 0)
M
Ming, Bai 已提交
120 121 122
    {
        str_desc.bLength = 4;
        str_desc.String[0] = 0x09;
123
        str_desc.String[1] = 0x04;
M
Ming, Bai 已提交
124 125 126 127 128 129
    }
    else
    {
        len = rt_strlen(device->str[index]);
        str_desc.bLength = len*2 + 2;

130
        for (i=0; i<len; i++)
M
Ming, Bai 已提交
131 132 133 134 135 136
        {
            str_desc.String[i*2] = device->str[index][i];
            str_desc.String[i*2 + 1] = 0;
        }
    }

137
    if (setup->length > str_desc.bLength)
M
Ming, Bai 已提交
138 139 140 141 142
        len = str_desc.bLength;
    else
        len = setup->length;

    /* send string descriptor to endpoint 0 */
143
    dcd_ep_write(device->dcd, 0, (rt_uint8_t *)&str_desc, len);
M
Ming, Bai 已提交
144 145 146 147 148 149 150 151 152 153 154 155

    return RT_EOK;
}

/**
 * This function will handle get_descriptor request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
156
static rt_err_t _get_descriptor(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
157 158 159 160
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);
161

162
    if (setup->request_type == USB_REQ_TYPE_DIR_IN)
M
Ming, Bai 已提交
163
    {
164
        switch (setup->value >> 8)
M
Ming, Bai 已提交
165 166 167 168
        {
        case USB_DESC_TYPE_DEVICE:
            _get_device_descriptor(device, setup);
            break;
169
        case USB_DESC_TYPE_CONFIGURATION:
M
Ming, Bai 已提交
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
            _get_config_descriptor(device, setup);
            break;
        case USB_DESC_TYPE_STRING:
            _get_string_descriptor(device, setup);
            break;
        case USB_DESC_TYPE_DEVICEQUALIFIER:
            dcd_ep_stall(device->dcd, 0);
            break;
        default:
            rt_kprintf("unsupported descriptor request\n");
            dcd_ep_stall(device->dcd, 0);
            break;
        }
    }
    else
    {
        rt_kprintf("request direction error\n");
        dcd_ep_stall(device->dcd, 0);
    }

    return RT_EOK;
}

/**
 * This function will handle get_interface request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
201
static rt_err_t _get_interface(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
202 203 204 205 206 207 208 209 210 211
{
    rt_uint8_t value;
    uintf_t intf;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_interface\n"));

H
heyuanjie87 已提交
212 213 214
    if (device->state != USB_STATE_CONFIGURED)
    {
        dcd_ep_stall(device->dcd, 0);
215

H
heyuanjie87 已提交
216 217 218
        return -RT_ERROR;
    }

M
Ming, Bai 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    /* find the specified interface and its alternate setting */
    intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
    value = intf->curr_setting->intf_desc->bAlternateSetting;

    /* send the interface alternate setting to endpoint 0*/
    dcd_ep_write(device->dcd, 0, &value, 1);

    return RT_EOK;
}

/**
 * This function will handle set_interface request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
237
static rt_err_t _set_interface(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
238 239 240
{
    uintf_t intf;
    uep_t ep;
241
    struct rt_list_node *i;
M
Ming, Bai 已提交
242 243 244 245 246 247 248 249
    ualtsetting_t setting;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_interface\n"));

H
heyuanjie87 已提交
250 251 252
    if (device->state != USB_STATE_CONFIGURED)
    {
        dcd_ep_stall(device->dcd, 0);
253

H
heyuanjie87 已提交
254 255
        return -RT_ERROR;
    }
256

M
Ming, Bai 已提交
257 258 259 260 261 262 263 264
    /* find the specified interface */
    intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);

    /* set alternate setting to the interface */
    rt_usbd_set_altsetting(intf, setup->value & 0xFF);
    setting = intf->curr_setting;

    /* start all endpoints of the interface alternate setting */
265
    for (i=setting->ep_list.next; i != &setting->ep_list; i=i->next)
M
Ming, Bai 已提交
266 267 268 269 270
    {
        ep = (uep_t)rt_list_entry(i, struct uendpoint, list);
        dcd_ep_stop(device->dcd, ep);
        dcd_ep_run(device->dcd, ep);
    }
H
heyuanjie87 已提交
271 272
    dcd_send_status(device->dcd);
    
M
Ming, Bai 已提交
273 274 275 276 277 278 279 280 281 282 283
    return RT_EOK;
}

/**
 * This function will handle get_config request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
284
static rt_err_t _get_config(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
285 286 287 288 289 290 291 292 293
{
    rt_uint8_t value;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);
    RT_ASSERT(device->curr_cfg != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config\n"));
294

H
heyuanjie87 已提交
295 296 297 298 299 300 301 302 303
    if (device->state == USB_STATE_CONFIGURED)
    {
        /* get current configuration */
        value = device->curr_cfg->cfg_desc.bConfigurationValue;
    }
    else
    {
        value = 0;
    }
M
Ming, Bai 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317
    /* write the current configuration to endpoint 0 */
    dcd_ep_write(device->dcd, 0, &value, 1);

    return RT_EOK;
}

/**
 * This function will handle set_config request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
318
static rt_err_t _set_config(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331
{
    struct rt_list_node *i, *j, *k;
    uconfig_t cfg;
    uintf_t intf;
    ualtsetting_t setting;
    uep_t ep;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));

H
heyuanjie87 已提交
332 333
    if (setup->value > device->dev_desc.bNumConfigurations)
    {
334
        dcd_ep_stall(device->dcd, 0);
335

H
heyuanjie87 已提交
336 337 338 339 340 341 342 343 344 345 346
        return -RT_ERROR;
    }

    if (setup->value == 0)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
        device->state = USB_STATE_ADDRESS;

        goto _exit;
    }

M
Ming, Bai 已提交
347 348 349 350 351 352 353 354
    /* set current configuration */
    rt_usbd_set_config(device, setup->value);
    cfg = device->curr_cfg;

    for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
    {
        /* run all classes and their endpoints in the configuration */
        uclass_t cls = (uclass_t)rt_list_entry(i, struct uclass, list);
355
        for (j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
M
Ming, Bai 已提交
356 357 358
        {
            intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
            setting = intf->curr_setting;
359
            for (k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
M
Ming, Bai 已提交
360 361 362 363 364 365 366 367 368
            {
                ep = (uep_t)rt_list_entry(k, struct uendpoint, list);

                /* first stop then start endpoint */
                dcd_ep_stop(device->dcd, ep);
                dcd_ep_run(device->dcd, ep);
            }
        }
        /* after running all endpoints, then run class */
369
        if (cls->ops->run != RT_NULL)
M
Ming, Bai 已提交
370 371
            cls->ops->run(device, cls);
    }
372

H
heyuanjie87 已提交
373 374 375
    device->state = USB_STATE_CONFIGURED;

_exit:
M
Ming, Bai 已提交
376
    /* issue status stage */
377 378
    dcd_send_status(device->dcd);

M
Ming, Bai 已提交
379 380 381 382 383 384 385 386 387 388 389
    return RT_EOK;
}

/**
 * This function will handle set_address request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
390
static rt_err_t _set_address(struct udevice *device, ureq_t setup)
M
Ming, Bai 已提交
391 392 393 394
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);
395

M
Ming, Bai 已提交
396 397 398 399
    RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));

    /* set address in device control driver */
    dcd_set_address(device->dcd, setup->value);
400

H
heyuanjie87 已提交
401
    device->state = USB_STATE_ADDRESS;
402

M
Ming, Bai 已提交
403
    /* issue status stage */
404
    dcd_send_status(device->dcd);
M
Ming, Bai 已提交
405 406 407 408

    return RT_EOK;
}

409 410 411 412 413 414 415 416 417
/**
 * This function will handle standard request to 
 * interface that defined in class-specifics
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful. 
 */
418
static rt_err_t _request_interface(struct udevice *device, ureq_t setup)
419 420 421 422 423 424 425 426 427 428
{
    uintf_t intf;
    uclass_t cls;
    rt_err_t ret;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n"));
429

430 431 432 433 434 435
    intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
    if (intf != RT_NULL)
    {
        ret = intf->handler(device, cls, setup);
    }
    else
H
heyuanjie87 已提交
436
    {
437
        ret = -RT_ERROR;
H
heyuanjie87 已提交
438
    }
439

440 441 442
    return ret;
}

M
Ming, Bai 已提交
443 444 445 446 447 448 449 450
/**
 * This function will handle standard request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
451
static rt_err_t _standard_request(struct udevice *device, ureq_t setup)
452
{
M
Ming, Bai 已提交
453 454 455 456 457 458 459 460 461
    udcd_t dcd;
    rt_uint16_t value = 0;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    dcd = device->dcd;

462
    switch (setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
M
Ming, Bai 已提交
463 464
    {
    case USB_REQ_TYPE_DEVICE:
465
        switch (setup->request)
M
Ming, Bai 已提交
466 467 468 469 470
        {
        case USB_REQ_GET_STATUS:
            dcd_ep_write(device->dcd, 0, &value, 2);
            break;
        case USB_REQ_CLEAR_FEATURE:
H
heyuanjie87 已提交
471 472
            dcd_clear_feature(dcd, setup->value, setup->index);
            dcd_send_status(dcd);
M
Ming, Bai 已提交
473 474
            break;
        case USB_REQ_SET_FEATURE:
H
heyuanjie87 已提交
475
            dcd_set_feature(dcd, setup->value, setup->index);
M
Ming, Bai 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
            break;
        case USB_REQ_SET_ADDRESS:
            _set_address(device, setup);
            break;
        case USB_REQ_GET_DESCRIPTOR:
            _get_descriptor(device, setup);
            break;
        case USB_REQ_SET_DESCRIPTOR:
            dcd_ep_stall(dcd, 0);
            break;
        case USB_REQ_GET_CONFIGURATION:
            _get_config(device, setup);
            break;
        case USB_REQ_SET_CONFIGURATION:
            _set_config(device, setup);
            break;
        default:
            rt_kprintf("unknown device request\n");
            dcd_ep_stall(device->dcd, 0);
            break;
        }
        break;
    case USB_REQ_TYPE_INTERFACE:
499
        switch (setup->request)
M
Ming, Bai 已提交
500 501 502 503 504 505 506 507
        {
        case USB_REQ_GET_INTERFACE:
            _get_interface(device, setup);
            break;
        case USB_REQ_SET_INTERFACE:
            _set_interface(device, setup);
            break;
        default:
508 509 510 511
            if (_request_interface(device, setup) != RT_EOK)
            {
                rt_kprintf("unknown interface request\n");
                dcd_ep_stall(device->dcd, 0);
512

513 514 515 516
                return - RT_ERROR;
            }
            else
                break;
M
Ming, Bai 已提交
517 518 519
        }
        break;
    case USB_REQ_TYPE_ENDPOINT:
520
        switch (setup->request)
M
Ming, Bai 已提交
521
        {
522
        case USB_REQ_GET_STATUS:
H
heyuanjie87 已提交
523
        {
M
Ming, Bai 已提交
524
            /* TODO */
H
heyuanjie87 已提交
525 526 527 528
            uep_t ep;
        
            ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
            value = ep->is_stall;        
M
Ming, Bai 已提交
529
            dcd_ep_write(dcd, 0, &value, 2);
H
heyuanjie87 已提交
530 531
        }
        break;
M
Ming, Bai 已提交
532
        case USB_REQ_CLEAR_FEATURE:
H
heyuanjie87 已提交
533 534 535 536 537 538 539 540 541
        {
            uep_t ep;
        
            ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
            ep->is_stall = 0;
            dcd_clear_feature(dcd, setup->value, setup->index);
            dcd_send_status(dcd);
        }
        break;
M
Ming, Bai 已提交
542
        case USB_REQ_SET_FEATURE:
H
heyuanjie87 已提交
543 544 545 546 547 548 549 550 551
        {
            uep_t ep;
        
            ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
            ep->is_stall = 1;            
            dcd_set_feature(dcd, setup->value, setup->index);
            dcd_send_status(dcd);
        }
        break;
M
Ming, Bai 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        case USB_REQ_SYNCH_FRAME:
            break;
        default:
            rt_kprintf("unknown endpoint request\n");
            dcd_ep_stall(device->dcd, 0);
            break;
        }
        break;
    case USB_REQ_TYPE_OTHER:
        rt_kprintf("unknown other type request\n");
        dcd_ep_stall(device->dcd, 0);
        break;
    default:
        rt_kprintf("unknown type request\n");
        dcd_ep_stall(device->dcd, 0);
        break;
    }

    return RT_EOK;
}

/**
 * This function will handle class request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful, -RT_ERROR on invalid request.
 */
static rt_err_t _class_request(udevice_t device, ureq_t setup)
{
    uintf_t intf;
    uclass_t cls;
585

M
Ming, Bai 已提交
586 587 588 589 590
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    /* verify request value */
591
    if (setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
M
Ming, Bai 已提交
592 593
    {
        dcd_ep_stall(device->dcd, 0);
594

M
Ming, Bai 已提交
595 596 597
        return -RT_ERROR;
    }

598
    switch (setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
M
Ming, Bai 已提交
599 600 601
    {
    case USB_REQ_TYPE_INTERFACE:
        intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
602
        intf->handler(device, cls, setup);
M
Ming, Bai 已提交
603 604 605 606 607 608 609
        break;
    case USB_REQ_TYPE_ENDPOINT:
        break;
    default:
        rt_kprintf("unknown class request type\n");
        dcd_ep_stall(device->dcd, 0);
        break;
610 611
    }

M
Ming, Bai 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    return RT_EOK;
}

/**
 * This function will handle setup request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful, -RT_ERROR on invalid request.
 */
static rt_err_t _setup_request(udevice_t device, ureq_t setup)
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request_handler 0x%x\n",
                                setup->request_type));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
637

638
    switch ((setup->request_type & USB_REQ_TYPE_MASK))
M
Ming, Bai 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    {
    case USB_REQ_TYPE_STANDARD:
        _standard_request(device, setup);
        break;
    case USB_REQ_TYPE_CLASS:
        _class_request(device, setup);
        break;
    case USB_REQ_TYPE_VENDOR:
        rt_kprintf("vendor type request\n");
        break;
    default:
        rt_kprintf("unknown setup request type\n");
        dcd_ep_stall(device->dcd, 0);
        return -RT_ERROR;
    }

    return RT_EOK;
}

/**
 * This function will notity sof event to all of class.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK.
 */
rt_err_t _sof_notify(udevice_t device)
{
    struct rt_list_node *i;
    uclass_t cls;
669

M
Ming, Bai 已提交
670 671 672
    RT_ASSERT(device != RT_NULL);

    /* to notity every class that sof event comes */
673 674 675
    for (i  = device->curr_cfg->cls_list.next;
         i != &device->curr_cfg->cls_list;
         i  = i->next)
M
Ming, Bai 已提交
676
    {
677
        cls = (uclass_t)rt_list_entry(i, struct uclass, list);
678
        if (cls->ops->sof_handler != RT_NULL)
M
Ming, Bai 已提交
679 680 681
            cls->ops->sof_handler(device, cls);
    }

682
    return RT_EOK;
M
Ming, Bai 已提交
683 684
}

685
/**
G
Grissiom 已提交
686
 * This function will stop all class.
687 688 689 690 691
 *
 * @param device the usb device object.
 *
 * @return RT_EOK.
 */
G
Grissiom 已提交
692
rt_err_t _stop_notify(udevice_t device)
693 694 695 696 697 698 699
{
    struct rt_list_node *i;
    uclass_t cls;

    RT_ASSERT(device != RT_NULL);

    /* to notity every class that sof event comes */
G
Grissiom 已提交
700 701 702
    for (i  = device->curr_cfg->cls_list.next;
         i != &device->curr_cfg->cls_list;
         i  = i->next)
703 704
    {
        cls = (uclass_t)rt_list_entry(i, struct uclass, list);
705
        if (cls->ops->stop != RT_NULL)
706
            cls->ops->stop(device, cls);
G
Grissiom 已提交
707
    }
708

G
Grissiom 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    return RT_EOK;
}

/**
 * This function will run all class.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK.
 */
rt_err_t _run_notify(udevice_t device)
{
    struct rt_list_node *i;
    uclass_t cls;

    RT_ASSERT(device != RT_NULL);

    /* to notity every class that sof event comes */
    for (i  = device->curr_cfg->cls_list.next;
         i != &device->curr_cfg->cls_list;
         i  = i->next)
    {
        cls = (uclass_t)rt_list_entry(i, struct uclass, list);
732
        if (cls->ops->run != RT_NULL)
733 734 735 736 737 738
            cls->ops->run(device, cls);
    }

    return RT_EOK;
}

G
Grissiom 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751 752
/**
 * This function will reset all class.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK.
 */
rt_err_t _reset_notify(udevice_t device)
{
    struct rt_list_node *i;
    uclass_t cls;

    RT_ASSERT(device != RT_NULL);

H
heyuanjie87 已提交
753 754
    _stop_notify(device);
    _run_notify(device);
G
Grissiom 已提交
755 756 757 758

    return RT_EOK;
}

M
Ming, Bai 已提交
759 760 761 762 763 764 765
/**
 * This function will create an usb device object.
 *
 * @param ustring the usb string array to contain string descriptor.
 *
 * @return an usb device object on success, RT_NULL on fail.
 */
766
udevice_t rt_usbd_device_create(void)
M
Ming, Bai 已提交
767 768 769 770 771 772 773
{
    udevice_t udevice;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_create\n"));

    /* allocate memory for the object */
    udevice = rt_malloc(sizeof(struct udevice));
774
    if (udevice == RT_NULL)
M
Ming, Bai 已提交
775 776
    {
        rt_kprintf("alloc memery failed\n");
777

M
Ming, Bai 已提交
778 779 780
        return RT_NULL;
    }
    rt_memset(udevice, 0, sizeof(struct udevice));
781

M
Ming, Bai 已提交
782 783 784 785
    /* to initialize configuration list */
    rt_list_init(&udevice->cfg_list);

    /* insert the device object to device list */
786
    rt_list_insert_after(&device_list, &udevice->list);
M
Ming, Bai 已提交
787 788 789 790

    return udevice;
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
/**
 * This function will set usb device string description.
 *
 * @param device the usb device object. 
 * @param ustring pointer to string pointer array.
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring)
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(ustring != RT_NULL);

    /* set string descriptor array to the device object */
    device->str = ustring;

    return RT_EOK;
}

M
Ming, Bai 已提交
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 841 842 843
/**
 * This function will set an usb controller driver to a device.
 *
 * @param device the usb device object.
 * @param dcd the usb device controller driver.
 *
 * @return RT_EOK on successful.
 */
rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(dcd != RT_NULL);

    /* set usb device controller driver to the device */
    device->dcd = dcd;

    return RT_EOK;
}

/**
 * This function will set an usb device descriptor to a device.
 *
 * @param device the usb device object.
 * @param dev_desc the usb device descriptor.
 *
 * @return RT_EOK on successful.
 */
rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(dev_desc != RT_NULL);
844

M
Ming, Bai 已提交
845 846
    /* copy the usb device descriptor to the device */
    rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
847

M
Ming, Bai 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
    return RT_EOK;
}

/**
 * This function will create an usb configuration object.
 *
 * @param none.
 *
 * @return an usb configuration object.
 */
uconfig_t rt_usbd_config_create(void)
{
    uconfig_t cfg;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_create\n"));

    /* allocate memory for the object */
    cfg = rt_malloc(sizeof(struct uconfig));
866
    if (cfg == RT_NULL)
M
Ming, Bai 已提交
867 868
    {
        rt_kprintf("alloc memery failed\n");
869

M
Ming, Bai 已提交
870 871 872 873 874
        return RT_NULL;
    }
    rt_memset(cfg, 0, sizeof(struct uconfig));

    /* set default value */
875 876
    cfg->cfg_desc.bLength      = USB_DESC_LENGTH_CONFIG;
    cfg->cfg_desc.type         = USB_DESC_TYPE_CONFIGURATION;
M
Ming, Bai 已提交
877 878
    cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
    cfg->cfg_desc.bmAttributes = 0xC0;
879
    cfg->cfg_desc.MaxPower     = 0x32;
M
Ming, Bai 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

    /* to initialize class object list */
    rt_list_init(&cfg->cls_list);

    return cfg;
}

/**
 * This function will create an usb interface object.
 *
 * @param device the usb device object.
 * @handler the callback handler of object
 *
 * @return an usb interface object on success, RT_NULL on fail.
 */
895
uintf_t rt_usbd_interface_create(udevice_t device, uintf_handler_t handler)
M
Ming, Bai 已提交
896 897 898 899 900 901 902
{
    uintf_t intf;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_create\n"));

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
903

M
Ming, Bai 已提交
904
    /* allocate memory for the object */
905
    intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
906
    if (intf == RT_NULL)
M
Ming, Bai 已提交
907 908
    {
        rt_kprintf("alloc memery failed\n");
909

M
Ming, Bai 已提交
910 911 912 913 914 915 916
        return RT_NULL;
    }
    intf->intf_num = device->nr_intf;
    device->nr_intf++;
    intf->handler = handler;
    intf->curr_setting = RT_NULL;

917
    /* to initialize the alternate setting object list */
M
Ming, Bai 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    rt_list_init(&intf->setting_list);

    return intf;
}

/**
 * This function will create an usb alternate setting object.
 *
 * @param intf_desc the interface descriptor.
 * @desc_size the size of the interface descriptor.
 *
 * @return an usb alternate setting object on success, RT_NULL on fail.
 */
ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size)
{
    ualtsetting_t setting;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_create\n"));

    /* parameter check */
    RT_ASSERT(desc_size > 0);

    /* allocate memory for the object */
    setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
942
    if (setting == RT_NULL)
M
Ming, Bai 已提交
943 944
    {
        rt_kprintf("alloc memery failed\n");
945

M
Ming, Bai 已提交
946 947 948 949 950 951 952 953
        return RT_NULL;
    }
    /* allocate memory for the desc */
    setting->desc = rt_malloc(desc_size);
    if (setting->desc == RT_NULL)
    {
        rt_kprintf("alloc desc memery failed\n");
        rt_free(setting);
954

955
        return RT_NULL;
M
Ming, Bai 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    }

    setting->desc_size = desc_size;
    setting->intf_desc = RT_NULL;

    /* to initialize endpoint list */
    rt_list_init(&setting->ep_list);

    return setting;
}

/**
 * This function will config an desc in alternate setting object.
 *
 * @param setting the altsetting to be config.
 * @param desc use it to init desc in setting.
 * @param intf_pos the offset of interface descriptor in desc.
 *
 * @return RT_EOK.
 */
976 977 978
rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting,
                                              const void   *desc,
                                              rt_off_t      intf_pos)
M
Ming, Bai 已提交
979 980 981
{
    RT_ASSERT(setting != RT_NULL);
    RT_ASSERT(setting->desc !=RT_NULL);
982

M
Ming, Bai 已提交
983
    rt_memcpy(setting->desc, desc, setting->desc_size);
984
    setting->intf_desc = (uintf_desc_t)((char *)setting->desc + intf_pos);
985 986

    return RT_EOK;
M
Ming, Bai 已提交
987 988 989 990 991 992 993 994 995 996 997
}

/**
 * This function will create an usb class object.
 *
 * @param device the usb device object.
 * @param dev_desc the device descriptor.
 * @param ops the operation set.
 *
 * @return an usb class object on success, RT_NULL on fail.
 */
998 999
uclass_t rt_usbd_class_create(udevice_t    device,
                              udev_desc_t  dev_desc,
1000
                              uclass_ops_t ops)
M
Ming, Bai 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
{
    uclass_t cls;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_create\n"));

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(dev_desc != RT_NULL);

    /* allocate memory for the object */
    cls = (uclass_t)rt_malloc(sizeof(struct uclass));
1012
    if (cls == RT_NULL)
M
Ming, Bai 已提交
1013 1014
    {
        rt_kprintf("alloc memery failed\n");
1015

M
Ming, Bai 已提交
1016 1017 1018
        return RT_NULL;
    }
    cls->dev_desc = dev_desc;
1019 1020
    cls->ops      = ops;
    cls->device   = device;
M
Ming, Bai 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045

    /* to initialize interface list */
    rt_list_init(&cls->intf_list);

    return cls;
}

/**
 * This function will create an usb endpoint object.
 *
 * @param ep_desc the endpoint descriptor.
 * @handler the callback handler of object
 *
 * @return an usb endpoint object on success, RT_NULL on fail.
 */
uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler)
{
    uep_t ep;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_create\n"));

    /* parameter check */
    RT_ASSERT(ep_desc != RT_NULL);

    /* allocate memory for the object */
1046
    ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
1047
    if (ep == RT_NULL)
M
Ming, Bai 已提交
1048 1049
    {
        rt_kprintf("alloc memery failed\n");
1050

M
Ming, Bai 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
        return RT_NULL;
    }
    ep->ep_desc = ep_desc;
    ep->handler = handler;
    ep->buffer  = RT_NULL;

    return ep;
}

/**
 * This function will find an usb device object.
 *
 * @dcd usd device controller driver.
 *
 * @return an usb device object on found or RT_NULL on not found.
 */
udevice_t rt_usbd_find_device(udcd_t dcd)
{
1069
    struct rt_list_node *node;
M
Ming, Bai 已提交
1070
    udevice_t device;
1071

M
Ming, Bai 已提交
1072 1073
    /* parameter check */
    RT_ASSERT(dcd != RT_NULL);
1074

M
Ming, Bai 已提交
1075 1076 1077 1078
    /* search a device in the the device list */
    for (node = device_list.next; node != &device_list; node = node->next)
    {
        device = (udevice_t)rt_list_entry(node, struct udevice, list);
1079 1080
        if (device->dcd == dcd)
            return device;
M
Ming, Bai 已提交
1081 1082 1083
    }

    rt_kprintf("can't find device\n");
1084

M
Ming, Bai 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    return RT_NULL;
}

/**
 * This function will find an usb configuration object.
 *
 * @param device the usb device object.
 * @param value the configuration number.
 *
 * @return an usb configuration object on found or RT_NULL on not found.
 */
uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
{
1098
    struct rt_list_node *node;
M
Ming, Bai 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107
    uconfig_t cfg = RT_NULL;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(value <= device->dev_desc.bNumConfigurations);

    /* search a configration in the the device */
1108 1109 1110
    for (node  = device->cfg_list.next;
         node != &device->cfg_list;
         node  = node->next)
M
Ming, Bai 已提交
1111 1112
    {
        cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
1113 1114
        if (cfg->cfg_desc.bConfigurationValue == value)
            return cfg;
M
Ming, Bai 已提交
1115 1116 1117
    }

    rt_kprintf("can't find configuration %d\n", value);
1118

M
Ming, Bai 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    return RT_NULL;
}

/**
 * This function will find an usb interface object.
 *
 * @param device the usb device object.
 * @param value the interface number.
 *
 * @return an usb configuration object on found or RT_NULL on not found.
 */
1130 1131 1132
uintf_t rt_usbd_find_interface(udevice_t  device,
                               rt_uint8_t value,
                               uclass_t  *pcls)
1133
{
M
Ming, Bai 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    struct rt_list_node *i, *j;
    uclass_t cls;
    uintf_t intf;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(value < device->nr_intf);

    /* search an interface in the current configuration */
1145 1146 1147
    for (i  = device->curr_cfg->cls_list.next;
         i != &device->curr_cfg->cls_list;
         i  = i->next)
M
Ming, Bai 已提交
1148
    {
1149
        cls = (uclass_t)rt_list_entry(i, struct uclass, list);
1150
        for (j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
M
Ming, Bai 已提交
1151 1152
        {
            intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
1153
            if (intf->intf_num == value)
M
Ming, Bai 已提交
1154 1155 1156
            {
                if (pcls != RT_NULL)
                    *pcls = cls;
1157

M
Ming, Bai 已提交
1158 1159 1160
                return intf;
            }
        }
1161
    }
M
Ming, Bai 已提交
1162 1163

    rt_kprintf("can't find interface %d\n", value);
1164

M
Ming, Bai 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
    return RT_NULL;
}

/**
 * This function will find an usb interface alternate setting object.
 *
 * @param device the usb device object.
 * @param value the alternate setting number.
 *
 * @return an usb interface alternate setting object on found or RT_NULL on not found.
 */
ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
{
    struct rt_list_node *i;
    ualtsetting_t setting;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));

    /* parameter check */
    RT_ASSERT(intf != RT_NULL);

1186
    if (intf->curr_setting != RT_NULL)
M
Ming, Bai 已提交
1187 1188
    {
        /* if the value equal to the current alternate setting, then do not search */
1189
        if (intf->curr_setting->intf_desc->bAlternateSetting == value)
M
Ming, Bai 已提交
1190 1191
            return intf->curr_setting;
    }
1192

M
Ming, Bai 已提交
1193
    /* search a setting in the alternate setting list */
1194
    for (i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
M
Ming, Bai 已提交
1195 1196
    {
        setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
1197
        if (setting->intf_desc->bAlternateSetting == value)
M
Ming, Bai 已提交
1198 1199 1200 1201
            return setting;
    }

    rt_kprintf("can't find alternate setting %d\n", value);
1202

M
Ming, Bai 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    return RT_NULL;
}

/**
 * This function will find an usb endpoint object.
 *
 * @param device the usb device object.
 * @param ep_addr endpoint address.
 *
 * @return an usb endpoint object on found or RT_NULL on not found.
 */
1214 1215 1216
uep_t rt_usbd_find_endpoint(udevice_t  device,
                            uclass_t  *pcls,
                            rt_uint8_t ep_addr)
M
Ming, Bai 已提交
1217 1218 1219 1220 1221 1222 1223 1224
{
    uep_t ep;
    struct rt_list_node *i, *j, *k;
    uclass_t cls;
    uintf_t intf;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
1225

M
Ming, Bai 已提交
1226
    /* search a endpoint in the current configuration */
1227 1228 1229
    for (i  = device->curr_cfg->cls_list.next;
         i != &device->curr_cfg->cls_list;
         i  = i->next)
M
Ming, Bai 已提交
1230
    {
1231
        cls = (uclass_t)rt_list_entry(i, struct uclass, list);
1232
        for (j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
M
Ming, Bai 已提交
1233 1234
        {
            intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
1235 1236 1237
            for (k  = intf->curr_setting->ep_list.next;
                 k != &intf->curr_setting->ep_list;
                 k  = k->next)
M
Ming, Bai 已提交
1238 1239
            {
                ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
1240
                if (ep->ep_desc->bEndpointAddress == ep_addr)
M
Ming, Bai 已提交
1241 1242 1243
                {
                    if (pcls != RT_NULL)
                        *pcls = cls;
1244

M
Ming, Bai 已提交
1245 1246 1247 1248 1249 1250 1251
                    return ep;
                }
            }
        }
    }

    rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
1252

M
Ming, Bai 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
    return RT_NULL;
}

/**
 * This function will add a configuration to an usb device.
 *
 * @param device the usb device object.
 * @param cfg the configuration object.
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
{
    struct rt_list_node *i, *j, *k;
    uclass_t cls;
    uintf_t intf;
    uep_t ep;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
1275
    RT_ASSERT(cfg != RT_NULL);
M
Ming, Bai 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284

    /* set configuration number to the configuration descriptor */
    cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
    device->dev_desc.bNumConfigurations++;

    for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
    {
        cls = (uclass_t)rt_list_entry(i, struct uclass, list);

1285
        for (j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
M
Ming, Bai 已提交
1286 1287 1288
        {
            intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
            cfg->cfg_desc.bNumInterfaces++;
1289

M
Ming, Bai 已提交
1290
            /* allocate address for every endpoint in the interface alternate setting */
1291 1292 1293
            for (k  = intf->curr_setting->ep_list.next;
                 k != &intf->curr_setting->ep_list;
                 k  = k->next)
M
Ming, Bai 已提交
1294 1295
            {
                ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
1296
                dcd_ep_alloc(device->dcd, ep);
M
Ming, Bai 已提交
1297 1298 1299
            }

            /* construct complete configuration descriptor */
1300 1301
            rt_memcpy((void *)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength - USB_DESC_LENGTH_CONFIG],
                      (void *)intf->curr_setting->desc,
1302
                      intf->curr_setting->desc_size);
M
Ming, Bai 已提交
1303 1304
            cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
        }
1305
    }
M
Ming, Bai 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330

    /* insert the configuration to the list */
    rt_list_insert_after(&device->cfg_list, &cfg->list);

    return RT_EOK;
}

/**
 * This function will add a class to a configuration.
 *
 * @param cfg the configuration object.
 * @param cls the class object.
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls)
{
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_class\n"));

    /* parameter check */
    RT_ASSERT(cfg != RT_NULL);
    RT_ASSERT(cls != RT_NULL);

    /* insert the class to the list */
    rt_list_insert_after(&cfg->cls_list, &cls->list);
1331

M
Ming, Bai 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
    return RT_EOK;
}

/**
 * This function will add an interface to a class.
 *
 * @param cls the class object.
 * @param intf the interface object.
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf)
{

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_add_interface\n"));

    /* parameter check */
    RT_ASSERT(cls != RT_NULL);
    RT_ASSERT(intf != RT_NULL);

    /* insert the interface to the list */
    rt_list_insert_after(&cls->intf_list, &intf->list);

    return RT_EOK;
}

/**
 * This function will add an alternate setting to an interface.
 *
 * @param intf the interface object.
 * @param setting the alternate setting object.
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
{
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));

    /* parameter check */
    RT_ASSERT(intf != RT_NULL);
    RT_ASSERT(setting != RT_NULL);

    setting->intf_desc->bInterfaceNumber = intf->intf_num;

    /* insert the alternate setting to the list */
    rt_list_insert_after(&intf->setting_list, &setting->list);

    return RT_EOK;
}

/**
 * This function will add an endpoint to an alternate setting.
 *
 * @param setting the alternate setting object.
 * @param ep the endpoint object.
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
{
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));

    /* parameter check */
    RT_ASSERT(setting != RT_NULL);
    RT_ASSERT(ep != RT_NULL);

    /* insert the endpoint to the list */
    rt_list_insert_after(&setting->ep_list, &ep->list);

    return RT_EOK;
}

/**
 * This function will set an alternate setting for an interface.
 *
 * @param intf_desc the interface descriptor.
 * @param value the alternate setting number.
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
{
    ualtsetting_t setting;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));

    /* parameter check */
    RT_ASSERT(intf != RT_NULL);

    /* find an alternate setting */
    setting = rt_usbd_find_altsetting(intf, value);

    /* set as current alternate setting */
    intf->curr_setting = setting;

    return RT_EOK;
}

/**
 * This function will set a configuration for an usb device.
 *
 * @param device the usb device object.
 * @param value the configuration number.
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
{
    uconfig_t cfg;

    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(value <= device->dev_desc.bNumConfigurations);

    /* find a configuration */
    cfg = rt_usbd_find_config(device, value);

    /* set as current configuration */
    device->curr_cfg = cfg;

    return RT_TRUE;
}

1457
static struct rt_messagequeue usb_mq;
M
Ming, Bai 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466

/**
 * This function is the main entry of usb device thread, it is in charge of
 * processing all messages received from the usb message buffer.
 *
 * @param parameter the parameter of the usb device thread.
 *
 * @return none.
 */
1467
static void rt_usbd_thread_entry(void *parameter)
1468
{
1469
    while (1)
1470 1471
    {
        struct udev_msg msg;
M
Ming, Bai 已提交
1472 1473 1474
        udevice_t device;
        uclass_t cls;
        uep_t ep;
1475

M
Ming, Bai 已提交
1476
        /* receive message */
1477
        if (rt_mq_recv(&usb_mq, &msg, sizeof(struct udev_msg), RT_WAITING_FOREVER) != RT_EOK)
G
Grissiom 已提交
1478 1479 1480
            continue;

        device = rt_usbd_find_device(msg.dcd);
1481
        if (device == RT_NULL)
G
Grissiom 已提交
1482 1483 1484 1485
        {
            rt_kprintf("invalid usb device\n");
            continue;
        }
1486

M
Ming, Bai 已提交
1487
        switch (msg.type)
1488
        {
1489 1490
        case USB_MSG_SOF:
            _sof_notify(device);
M
Ming, Bai 已提交
1491 1492
            break;
        case USB_MSG_DATA_NOTIFY:
1493 1494 1495 1496
            /* some buggy drivers will have USB_MSG_DATA_NOTIFY before the core
             * got configured. */
            if (device->state != USB_STATE_CONFIGURED)
                break;
M
Ming, Bai 已提交
1497
            ep = rt_usbd_find_endpoint(device, &cls, msg.content.ep_msg.ep_addr);
1498
            if (ep != RT_NULL)
M
Ming, Bai 已提交
1499
                ep->handler(device, cls, msg.content.ep_msg.size);
1500
            else
M
Ming, Bai 已提交
1501 1502
                rt_kprintf("invalid endpoint\n");
            break;
1503 1504
        case USB_MSG_SETUP_NOTIFY:
            _setup_request(device, (ureq_t)msg.content.setup_msg.packet);
M
Ming, Bai 已提交
1505
            break;
1506 1507 1508 1509
        case USB_MSG_RESET:
            if (device->state == USB_STATE_ADDRESS)
                _reset_notify(device);
            break;
G
Grissiom 已提交
1510 1511 1512
        case USB_MSG_PLUG_OUT:
            _stop_notify(device);
            break;
M
Ming, Bai 已提交
1513
        default:
G
Grissiom 已提交
1514
            rt_kprintf("unknown msg type\n");
M
Ming, Bai 已提交
1515
            break;
1516
        }
M
Ming, Bai 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
    }
}

/**
 * This function will post an message to usb message queue,
 *
 * @param msg the message to be posted
 * @param size the size of the message .
 *
 * @return the error code, RT_EOK on successfully.
 */
1528
rt_err_t rt_usbd_post_event(struct udev_msg *msg, rt_size_t size)
M
Ming, Bai 已提交
1529 1530 1531 1532
{
    RT_ASSERT(msg != RT_NULL);

    /* send message to usb message queue */
1533
    return rt_mq_send(&usb_mq, (void *)msg, size);
M
Ming, Bai 已提交
1534 1535
}

1536 1537 1538 1539

ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
static struct rt_thread usb_thread;
1540 1541 1542 1543 1544 1545
#define USBD_MQ_MSG_SZ  32
#define USBD_MQ_MAX_MSG 16
/* internal of the message queue: every message is associated with a pointer,
 * so in order to recveive USBD_MQ_MAX_MSG messages, we have to allocate more
 * than USBD_MQ_MSG_SZ*USBD_MQ_MAX_MSG memery. */
static rt_uint8_t usb_mq_pool[(USBD_MQ_MSG_SZ+sizeof(void*))*USBD_MQ_MAX_MSG];
1546

M
Ming, Bai 已提交
1547 1548 1549 1550 1551 1552 1553 1554
/**
 * This function will initialize usb device thread.
 *
 * @return none.
 */
rt_err_t rt_usbd_core_init(void)
{
    rt_list_init(&device_list);
1555

M
Ming, Bai 已提交
1556
    /* create an usb message queue */
1557 1558 1559 1560 1561 1562
    rt_mq_init(&usb_mq,
               "usbd",
               usb_mq_pool,
               USBD_MQ_MSG_SZ,
               sizeof(usb_mq_pool),
               RT_IPC_FLAG_FIFO);
1563

1564
    /* init usb device thread */
1565 1566 1567 1568 1569 1570 1571 1572
    rt_thread_init(&usb_thread,
                   "usbd",
                   rt_usbd_thread_entry,
                   RT_NULL,
                   usb_thread_stack,
                   RT_USBD_THREAD_STACK_SZ,
                   RT_USBD_THREAD_PRIO,
                   20);
1573 1574
    /* rt_thread_init should always be OK, so start the thread without further
     * checking. */
1575

1576
    return rt_thread_startup(&usb_thread);
M
Ming, Bai 已提交
1577
}