core.c 58.2 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
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-10-01     Yi Qiu       first version
23
 * 2012-12-12     heyuanjie87  change endpoint and function handler
M
Ming, Bai 已提交
24
 * 2012-12-30     heyuanjie87  change inferface handler
25 26
 * 2013-04-26     aozima       add DEVICEQUALIFIER support.
 * 2013-07-25     Yi Qiu       update for USB CV test
27
 * 2017-11-15     ZYH          fix ep0 transform error
M
Ming, Bai 已提交
28 29 30
 */

#include <rtthread.h>
还_没_想_好's avatar
还_没_想_好 已提交
31 32
#include "drivers/usb_common.h"
#include "drivers/usb_device.h"
M
Ming, Bai 已提交
33 34 35

static rt_list_t device_list;

36 37 38 39 40
static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size);
static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size);
static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep);
static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep);

M
Ming, Bai 已提交
41
/**
还_没_想_好's avatar
还_没_想_好 已提交
42
 * This function will handle get_device_descriptor bRequest.
M
Ming, Bai 已提交
43 44
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
45
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
46 47 48
 *
 * @return RT_EOK on successful.
 */
49
static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
50 51
{
    rt_size_t size;
52

M
Ming, Bai 已提交
53 54 55 56 57 58
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

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

还_没_想_好's avatar
还_没_想_好 已提交
59 60 61
    /* device descriptor wLength should less than USB_DESC_LENGTH_DEVICE*/
    size = (setup->wLength > USB_DESC_LENGTH_DEVICE) ?
           USB_DESC_LENGTH_DEVICE : setup->wLength;
M
Ming, Bai 已提交
62 63

    /* send device descriptor to endpoint 0 */
还_没_想_好's avatar
还_没_想_好 已提交
64
    rt_usbd_ep0_write(device, (rt_uint8_t*) &device->dev_desc, size);
M
Ming, Bai 已提交
65 66 67 68 69

    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
70
 * This function will handle get_config_descriptor bRequest.
M
Ming, Bai 已提交
71 72
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
73
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
74 75 76
 *
 * @return RT_EOK on successful.
 */
77
static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
78 79 80 81 82 83 84 85 86 87 88
{
    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;
还_没_想_好's avatar
还_没_想_好 已提交
89 90
    size = (setup->wLength > cfg_desc->wTotalLength) ?
           cfg_desc->wTotalLength : setup->wLength;
M
Ming, Bai 已提交
91 92

    /* send configuration descriptor to endpoint 0 */
93
    rt_usbd_ep0_write(device, (rt_uint8_t*)cfg_desc, size);
94

M
Ming, Bai 已提交
95 96 97 98
    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
99
 * This function will handle get_string_descriptor bRequest.
M
Ming, Bai 已提交
100 101
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
102
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
103
 *
还_没_想_好's avatar
还_没_想_好 已提交
104
 * @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
M
Ming, Bai 已提交
105
 */
106
static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
107 108 109 110 111 112 113 114 115 116 117 118
{
    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;
还_没_想_好's avatar
还_没_想_好 已提交
119
    index = setup->wValue & 0xFF;
M
Ming, Bai 已提交
120

121 122 123 124 125 126
    if(index == 0xEE)
    {
        index = USB_STRING_OS_INDEX;
    }

    if(index > USB_STRING_MAX)
M
Ming, Bai 已提交
127 128
    {
        rt_kprintf("unknown string index\n");
129
        rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
130
        return -RT_ERROR;
131
    }
132
    else if(index == USB_STRING_LANGID_INDEX)
M
Ming, Bai 已提交
133 134 135
    {
        str_desc.bLength = 4;
        str_desc.String[0] = 0x09;
136
        str_desc.String[1] = 0x04;
M
Ming, Bai 已提交
137 138 139 140 141 142
    }
    else
    {
        len = rt_strlen(device->str[index]);
        str_desc.bLength = len*2 + 2;

143
        for(i=0; i<len; i++)
M
Ming, Bai 已提交
144 145 146 147 148 149
        {
            str_desc.String[i*2] = device->str[index][i];
            str_desc.String[i*2 + 1] = 0;
        }
    }

还_没_想_好's avatar
还_没_想_好 已提交
150
    if (setup->wLength > str_desc.bLength)
M
Ming, Bai 已提交
151 152
        len = str_desc.bLength;
    else
还_没_想_好's avatar
还_没_想_好 已提交
153
        len = setup->wLength;
M
Ming, Bai 已提交
154 155

    /* send string descriptor to endpoint 0 */
156 157 158 159 160 161 162 163 164 165 166 167 168
    rt_usbd_ep0_write(device, (rt_uint8_t*)&str_desc, len);

    return RT_EOK;
}

static rt_err_t _get_qualifier_descriptor(struct udevice* device, ureq_t setup)
{
    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_qualifier_descriptor\n"));

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

169
    if(device->dev_qualifier && device->dcd->device_is_hs)
170 171 172 173 174 175 176 177 178
    {
        /* send device qualifier descriptor to endpoint 0 */
        rt_usbd_ep0_write(device, (rt_uint8_t*)device->dev_qualifier,
                     sizeof(struct usb_qualifier_descriptor));
    }
    else
    {
        rt_usbd_ep0_set_stall(device);
    }
M
Ming, Bai 已提交
179 180 181 182 183

    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
184
 * This function will handle get_descriptor bRequest.
M
Ming, Bai 已提交
185 186
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
187
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
188 189 190
 *
 * @return RT_EOK on successful.
 */
191
static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
192 193 194 195
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);
196

197
    if(setup->request_type == USB_REQ_TYPE_DIR_IN)
M
Ming, Bai 已提交
198
    {
还_没_想_好's avatar
还_没_想_好 已提交
199
        switch(setup->wValue >> 8)
M
Ming, Bai 已提交
200 201 202 203
        {
        case USB_DESC_TYPE_DEVICE:
            _get_device_descriptor(device, setup);
            break;
204
        case USB_DESC_TYPE_CONFIGURATION:
M
Ming, Bai 已提交
205 206 207 208 209 210
            _get_config_descriptor(device, setup);
            break;
        case USB_DESC_TYPE_STRING:
            _get_string_descriptor(device, setup);
            break;
        case USB_DESC_TYPE_DEVICEQUALIFIER:
211
            _get_qualifier_descriptor(device, setup);
M
Ming, Bai 已提交
212
            break;
213 214 215
        case USB_DESC_TYPE_OTHERSPEED:
            _get_config_descriptor(device, setup);
            break;
M
Ming, Bai 已提交
216 217
        default:
            rt_kprintf("unsupported descriptor request\n");
218
            rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
219 220 221 222 223 224
            break;
        }
    }
    else
    {
        rt_kprintf("request direction error\n");
225
        rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
226 227 228 229 230 231
    }

    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
232
 * This function will handle get_interface bRequest.
M
Ming, Bai 已提交
233 234
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
235
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
236 237 238
 *
 * @return RT_EOK on successful.
 */
239
static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
240 241 242 243 244 245 246 247 248 249
{
    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 已提交
250 251
    if (device->state != USB_STATE_CONFIGURED)
    {
252
        rt_usbd_ep0_set_stall(device);
H
heyuanjie87 已提交
253 254 255
        return -RT_ERROR;
    }

M
Ming, Bai 已提交
256
    /* find the specified interface and its alternate setting */
还_没_想_好's avatar
还_没_想_好 已提交
257
    intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL);
M
Ming, Bai 已提交
258 259 260
    value = intf->curr_setting->intf_desc->bAlternateSetting;

    /* send the interface alternate setting to endpoint 0*/
261
    rt_usbd_ep0_write(device, &value, 1);
M
Ming, Bai 已提交
262 263 264 265 266

    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
267
 * This function will handle set_interface bRequest.
M
Ming, Bai 已提交
268 269
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
270
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
271 272 273
 *
 * @return RT_EOK on successful.
 */
274
static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
275 276 277
{
    uintf_t intf;
    uep_t ep;
278
    struct rt_list_node* i;
M
Ming, Bai 已提交
279 280 281 282 283 284 285 286
    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 已提交
287 288
    if (device->state != USB_STATE_CONFIGURED)
    {
289
        rt_usbd_ep0_set_stall(device);
H
heyuanjie87 已提交
290 291
        return -RT_ERROR;
    }
292
        
M
Ming, Bai 已提交
293
    /* find the specified interface */
还_没_想_好's avatar
还_没_想_好 已提交
294
    intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL);
M
Ming, Bai 已提交
295 296

    /* set alternate setting to the interface */
还_没_想_好's avatar
还_没_想_好 已提交
297
    rt_usbd_set_altsetting(intf, setup->wValue & 0xFF);
M
Ming, Bai 已提交
298 299 300
    setting = intf->curr_setting;

    /* start all endpoints of the interface alternate setting */
301
    for(i=setting->ep_list.next; i != &setting->ep_list; i=i->next)
M
Ming, Bai 已提交
302 303
    {
        ep = (uep_t)rt_list_entry(i, struct uendpoint, list);
304 305
        dcd_ep_disable(device->dcd, ep);
        dcd_ep_enable(device->dcd, ep);
M
Ming, Bai 已提交
306
    }
307
    dcd_ep0_send_status(device->dcd);
H
heyuanjie87 已提交
308
    
M
Ming, Bai 已提交
309 310 311 312
    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
313
 * This function will handle get_config bRequest.
M
Ming, Bai 已提交
314 315
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
316
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
317 318 319
 *
 * @return RT_EOK on successful.
 */
320
static rt_err_t _get_config(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
321 322 323 324 325 326 327 328 329
{
    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"));
330
    
H
heyuanjie87 已提交
331 332 333 334 335 336 337 338 339
    if (device->state == USB_STATE_CONFIGURED)
    {
        /* get current configuration */
        value = device->curr_cfg->cfg_desc.bConfigurationValue;
    }
    else
    {
        value = 0;
    }
M
Ming, Bai 已提交
340
    /* write the current configuration to endpoint 0 */
341
    rt_usbd_ep0_write(device, &value, 1);
M
Ming, Bai 已提交
342 343 344 345 346

    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
347
 * This function will handle set_config bRequest.
M
Ming, Bai 已提交
348 349
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
350
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
351 352 353
 *
 * @return RT_EOK on successful.
 */
354
static rt_err_t _set_config(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367
{
    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"));

还_没_想_好's avatar
还_没_想_好 已提交
368
    if (setup->wValue > device->dev_desc.bNumConfigurations)
H
heyuanjie87 已提交
369
    {
370
        rt_usbd_ep0_set_stall(device);
H
heyuanjie87 已提交
371 372 373
        return -RT_ERROR;
    }

还_没_想_好's avatar
还_没_想_好 已提交
374
    if (setup->wValue == 0)
H
heyuanjie87 已提交
375 376 377 378 379 380 381
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
        device->state = USB_STATE_ADDRESS;

        goto _exit;
    }

M
Ming, Bai 已提交
382
    /* set current configuration */
还_没_想_好's avatar
还_没_想_好 已提交
383
    rt_usbd_set_config(device, setup->wValue);
M
Ming, Bai 已提交
384 385
    cfg = device->curr_cfg;

386
    for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next)
M
Ming, Bai 已提交
387
    {
388 389 390
        /* run all functiones and their endpoints in the configuration */
        ufunction_t func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
        for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
M
Ming, Bai 已提交
391 392 393
        {
            intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
            setting = intf->curr_setting;
394
            for(k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
M
Ming, Bai 已提交
395 396 397
            {
                ep = (uep_t)rt_list_entry(k, struct uendpoint, list);

398 399 400
                /* first disable then enable an endpoint */
                dcd_ep_disable(device->dcd, ep);
                dcd_ep_enable(device->dcd, ep);
M
Ming, Bai 已提交
401 402
            }
        }
403 404
        /* after enabled endpoints, then enable function */
        FUNC_ENABLE(func);
M
Ming, Bai 已提交
405
    }
406

H
heyuanjie87 已提交
407 408 409
    device->state = USB_STATE_CONFIGURED;

_exit:
M
Ming, Bai 已提交
410
    /* issue status stage */
411
    dcd_ep0_send_status(device->dcd);
412

M
Ming, Bai 已提交
413 414 415 416
    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
417
 * This function will handle set_address bRequest.
M
Ming, Bai 已提交
418 419
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
420
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
421 422 423
 *
 * @return RT_EOK on successful.
 */
424
static rt_err_t _set_address(struct udevice* device, ureq_t setup)
M
Ming, Bai 已提交
425 426 427 428
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);
429

还_没_想_好's avatar
还_没_想_好 已提交
430 431 432
    /* set address in device control driver */
    dcd_set_address(device->dcd, setup->wValue);

433 434
    /* issue status stage */
    dcd_ep0_send_status(device->dcd);
M
Ming, Bai 已提交
435

436 437
    RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
    
H
heyuanjie87 已提交
438
    device->state = USB_STATE_ADDRESS;
439

M
Ming, Bai 已提交
440 441 442
    return RT_EOK;
}

443
/**
还_没_想_好's avatar
还_没_想_好 已提交
444
 * This function will handle standard bRequest to
445
 * interface that defined in function-specifics
446 447
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
448
 * @param setup the setup bRequest.
449
 *
450
 * @return RT_EOK on successful.
451
 */
452
static rt_err_t _request_interface(struct udevice* device, ureq_t setup)
453 454
{
    uintf_t intf;
455
    ufunction_t func;
456 457 458 459 460 461 462
    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"));
463

还_没_想_好's avatar
还_没_想_好 已提交
464
    intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func);
465 466
    if (intf != RT_NULL)
    {
467
        ret = intf->handler(func, setup);
468 469
    }
    else
H
heyuanjie87 已提交
470
    {
471
        ret = -RT_ERROR;
H
heyuanjie87 已提交
472
    }
473
    
474 475 476
    return ret;
}

M
Ming, Bai 已提交
477
/**
还_没_想_好's avatar
还_没_想_好 已提交
478
 * This function will handle standard bRequest.
M
Ming, Bai 已提交
479 480
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
481
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
482 483 484
 *
 * @return RT_EOK on successful.
 */
485
static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
486
{
M
Ming, Bai 已提交
487 488 489 490 491 492 493 494 495
    udcd_t dcd;
    rt_uint16_t value = 0;

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

    dcd = device->dcd;

496
    switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
M
Ming, Bai 已提交
497 498
    {
    case USB_REQ_TYPE_DEVICE:
还_没_想_好's avatar
还_没_想_好 已提交
499
        switch(setup->bRequest)
M
Ming, Bai 已提交
500 501
        {
        case USB_REQ_GET_STATUS:
502
            rt_usbd_ep0_write(device, &value, 2);
M
Ming, Bai 已提交
503 504
            break;
        case USB_REQ_CLEAR_FEATURE:
还_没_想_好's avatar
还_没_想_好 已提交
505
            rt_usbd_clear_feature(device, setup->wValue, setup->wIndex);
506
            dcd_ep0_send_status(dcd);
M
Ming, Bai 已提交
507 508
            break;
        case USB_REQ_SET_FEATURE:
还_没_想_好's avatar
还_没_想_好 已提交
509
            rt_usbd_set_feature(device, setup->wValue, setup->wIndex);
M
Ming, Bai 已提交
510 511 512 513 514 515 516 517
            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:
518
            rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
519 520 521 522 523 524 525 526 527
            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");
528
            rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
529 530 531 532
            break;
        }
        break;
    case USB_REQ_TYPE_INTERFACE:
还_没_想_好's avatar
还_没_想_好 已提交
533
        switch(setup->bRequest)
M
Ming, Bai 已提交
534 535 536 537 538 539 540 541
        {
        case USB_REQ_GET_INTERFACE:
            _get_interface(device, setup);
            break;
        case USB_REQ_SET_INTERFACE:
            _set_interface(device, setup);
            break;
        default:
542 543 544
            if (_request_interface(device, setup) != RT_EOK)
            {
                rt_kprintf("unknown interface request\n");
545
                rt_usbd_ep0_set_stall(device);
546 547 548 549
                return - RT_ERROR;
            }
            else
                break;
M
Ming, Bai 已提交
550 551 552
        }
        break;
    case USB_REQ_TYPE_ENDPOINT:
还_没_想_好's avatar
还_没_想_好 已提交
553
        switch(setup->bRequest)
M
Ming, Bai 已提交
554
        {
555
        case USB_REQ_GET_STATUS:
H
heyuanjie87 已提交
556 557 558
        {
            uep_t ep;
        
还_没_想_好's avatar
还_没_想_好 已提交
559
            ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
560 561
            value = ep->stalled;        
            rt_usbd_ep0_write(device, &value, 2);
H
heyuanjie87 已提交
562 563
        }
        break;
M
Ming, Bai 已提交
564
        case USB_REQ_CLEAR_FEATURE:
H
heyuanjie87 已提交
565 566
        {
            uep_t ep;
567 568 569
            uio_request_t req;
            struct rt_list_node *node;

还_没_想_好's avatar
还_没_想_好 已提交
570 571
            ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
            if(USB_EP_HALT == setup->wValue && ep->stalled == RT_TRUE)
572
            {
还_没_想_好's avatar
还_没_想_好 已提交
573
                rt_usbd_clear_feature(device, setup->wValue, setup->wIndex);
574 575 576 577 578 579 580 581 582 583 584 585
                dcd_ep0_send_status(dcd);
                ep->stalled = RT_FALSE;  

                for (node = ep->request_list.next; node != &ep->request_list; node = node->next)
                {
                    req = (uio_request_t)rt_list_entry(node, struct uio_request, list);                    
                    rt_usbd_io_request(device, ep, req);
                    RT_DEBUG_LOG(RT_DEBUG_USB, ("fired a request\n"));                    
                }

                rt_list_init(&ep->request_list);
            }
H
heyuanjie87 已提交
586 587
        }
        break;
M
Ming, Bai 已提交
588
        case USB_REQ_SET_FEATURE:
H
heyuanjie87 已提交
589 590
        {
            uep_t ep;
591

还_没_想_好's avatar
还_没_想_好 已提交
592
            if(USB_EP_HALT == setup->wValue)
593
            {
还_没_想_好's avatar
还_没_想_好 已提交
594
                ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
595
                ep->stalled = RT_TRUE;            
还_没_想_好's avatar
还_没_想_好 已提交
596
                rt_usbd_set_feature(device, setup->wValue, setup->wIndex);
597 598
                dcd_ep0_send_status(dcd);
            }    
H
heyuanjie87 已提交
599 600
        }
        break;
M
Ming, Bai 已提交
601 602 603 604
        case USB_REQ_SYNCH_FRAME:
            break;
        default:
            rt_kprintf("unknown endpoint request\n");
605
            rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
606 607 608 609 610
            break;
        }
        break;
    case USB_REQ_TYPE_OTHER:
        rt_kprintf("unknown other type request\n");
611
        rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
612 613 614
        break;
    default:
        rt_kprintf("unknown type request\n");
615
        rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
616 617 618 619 620 621 622
        break;
    }

    return RT_EOK;
}

/**
还_没_想_好's avatar
还_没_想_好 已提交
623
 * This function will handle function bRequest.
M
Ming, Bai 已提交
624 625
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
626
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
627
 *
还_没_想_好's avatar
还_没_想_好 已提交
628
 * @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
M
Ming, Bai 已提交
629
 */
630
static rt_err_t _function_request(udevice_t device, ureq_t setup)
M
Ming, Bai 已提交
631 632
{
    uintf_t intf;
633
    ufunction_t func;
634

M
Ming, Bai 已提交
635 636 637 638
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

还_没_想_好's avatar
还_没_想_好 已提交
639 640
    /* verify bRequest wValue */
    if(setup->wIndex > device->curr_cfg->cfg_desc.bNumInterfaces)
M
Ming, Bai 已提交
641
    {
642
        rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
643 644 645
        return -RT_ERROR;
    }

646
    switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
M
Ming, Bai 已提交
647 648
    {
    case USB_REQ_TYPE_INTERFACE:
还_没_想_好's avatar
还_没_想_好 已提交
649
        intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func);
650 651 652 653 654 655 656 657 658
        if(intf == RT_NULL)
        {
            rt_kprintf("unkwown interface request\n");
            rt_usbd_ep0_set_stall(device);
        }
        else
        {
            intf->handler(func, setup);
        }
M
Ming, Bai 已提交
659 660 661 662
        break;
    case USB_REQ_TYPE_ENDPOINT:
        break;
    default:
663 664
        rt_kprintf("unknown function request type\n");
        rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
665
        break;
666 667
    }

M
Ming, Bai 已提交
668 669
    return RT_EOK;
}
670 671 672 673 674
static rt_err_t _vendor_request(udevice_t device, ureq_t setup)
{
    static rt_uint8_t * usb_comp_id_desc = RT_NULL;
    static rt_uint32_t  usb_comp_id_desc_size = 0;
    usb_os_func_comp_id_desc_t func_comp_id_desc;
675 676
    uintf_t intf;
    ufunction_t func;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    switch(setup->bRequest)
    {
        case 'A':
        switch(setup->wIndex)
        {
            case 0x04:
                if(rt_list_len(&device->os_comp_id_desc->func_desc) == 0)
                {
                    rt_usbd_ep0_set_stall(device);
                    return RT_EOK;
                }
                if(usb_comp_id_desc == RT_NULL)
                {
                    rt_uint8_t * pusb_comp_id_desc;
                    rt_list_t *p;
                    usb_comp_id_desc_size = sizeof(struct usb_os_header_comp_id_descriptor) + 
                    (sizeof(struct usb_os_function_comp_id_descriptor)-sizeof(rt_list_t))*rt_list_len(&device->os_comp_id_desc->func_desc);

                    usb_comp_id_desc = (rt_uint8_t *)rt_malloc(usb_comp_id_desc_size);
                    RT_ASSERT(usb_comp_id_desc != RT_NULL);
                    device->os_comp_id_desc->head_desc.dwLength = usb_comp_id_desc_size;
                    pusb_comp_id_desc = usb_comp_id_desc;
                    rt_memcpy((void *)pusb_comp_id_desc,(void *)&device->os_comp_id_desc->head_desc,sizeof(struct usb_os_header_comp_id_descriptor));
                    pusb_comp_id_desc += sizeof(struct usb_os_header_comp_id_descriptor);
                    
                    for (p = device->os_comp_id_desc->func_desc.next; p != &device->os_comp_id_desc->func_desc; p = p->next)
                    {
                        func_comp_id_desc = rt_list_entry(p,struct usb_os_function_comp_id_descriptor,list);
                        rt_memcpy(pusb_comp_id_desc,(void *)&func_comp_id_desc->bFirstInterfaceNumber,
                        sizeof(struct usb_os_function_comp_id_descriptor)-sizeof(rt_list_t));
                        pusb_comp_id_desc += sizeof(struct usb_os_function_comp_id_descriptor)-sizeof(rt_list_t);
                    }
                }
                rt_usbd_ep0_write(device, (void*)usb_comp_id_desc, setup->wLength);
            break;
712 713 714 715 716 717 718
            case 0x05:
                intf = rt_usbd_find_interface(device, setup->wValue & 0xFF, &func);
                if(intf != RT_NULL)
                {
                    intf->handler(func, setup);
                }
                break;
719 720 721 722 723 724
        }
            
        break;
    }
    return RT_EOK;
}
725 726 727
static rt_err_t _dump_setup_packet(ureq_t setup)
{
    RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
还_没_想_好's avatar
还_没_想_好 已提交
728
    RT_DEBUG_LOG(RT_DEBUG_USB, ("  setup_request : 0x%x\n",
729
                                setup->request_type));
还_没_想_好's avatar
还_没_想_好 已提交
730 731 732 733
    RT_DEBUG_LOG(RT_DEBUG_USB, ("  value         : 0x%x\n", setup->wValue));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("  length        : 0x%x\n", setup->wLength));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("  index         : 0x%x\n", setup->wIndex));
    RT_DEBUG_LOG(RT_DEBUG_USB, ("  request       : 0x%x\n", setup->bRequest));
734 735 736 737 738
    RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));

    return RT_EOK;
}

M
Ming, Bai 已提交
739
/**
还_没_想_好's avatar
还_没_想_好 已提交
740
 * This function will handle setup bRequest.
M
Ming, Bai 已提交
741 742
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
743
 * @param setup the setup bRequest.
M
Ming, Bai 已提交
744
 *
还_没_想_好's avatar
还_没_想_好 已提交
745
 * @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
M
Ming, Bai 已提交
746 747 748 749 750 751 752
 */
static rt_err_t _setup_request(udevice_t device, ureq_t setup)
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

753
    _dump_setup_packet(setup);
754

755
    switch((setup->request_type & USB_REQ_TYPE_MASK))
M
Ming, Bai 已提交
756 757 758 759 760
    {
    case USB_REQ_TYPE_STANDARD:
        _standard_request(device, setup);
        break;
    case USB_REQ_TYPE_CLASS:
761
        _function_request(device, setup);
M
Ming, Bai 已提交
762 763
        break;
    case USB_REQ_TYPE_VENDOR:
764
        _vendor_request(device, setup);
M
Ming, Bai 已提交
765 766 767
        break;
    default:
        rt_kprintf("unknown setup request type\n");
768
        rt_usbd_ep0_set_stall(device);
M
Ming, Bai 已提交
769 770 771 772 773 774 775
        return -RT_ERROR;
    }

    return RT_EOK;
}

/**
776
 * This function will hanle data notify event.
M
Ming, Bai 已提交
777
 *
778 779
 * @param device the usb device object. 
 * @param ep_msg the endpoint message.
M
Ming, Bai 已提交
780 781 782
 *
 * @return RT_EOK.
 */
783
static rt_err_t _data_notify(udevice_t device, struct ep_msg* ep_msg)
M
Ming, Bai 已提交
784
{
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
    uep_t ep;
    ufunction_t func;
    rt_size_t size = 0;
    
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(ep_msg != RT_NULL);
    
    if (device->state != USB_STATE_CONFIGURED)
    {
        return -RT_ERROR;
    }
    
    ep = rt_usbd_find_endpoint(device, &func, ep_msg->ep_addr);
    if(ep == RT_NULL)
    {        
        rt_kprintf("invalid endpoint\n");
        return -RT_ERROR;
    }
803

804 805
    if(EP_ADDRESS(ep) & USB_DIR_IN)
    {
还_没_想_好's avatar
还_没_想_好 已提交
806
        size = ep_msg->size;
807 808
        if(ep->request.remain_size >= EP_MAXPACKET(ep))
        {
还_没_想_好's avatar
还_没_想_好 已提交
809
            dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, EP_MAXPACKET(ep));
810 811 812 813 814
            ep->request.remain_size -= EP_MAXPACKET(ep);
            ep->request.buffer += EP_MAXPACKET(ep);     
        }
        else if(ep->request.remain_size > 0)
        {
还_没_想_好's avatar
还_没_想_好 已提交
815
            dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, ep->request.remain_size);
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
            ep->request.remain_size = 0;
        }
        else
        {
            EP_HANDLER(ep, func, size);
        }
    }
    else
    {
        size = ep_msg->size;
        if(ep->request.remain_size == 0)
        {
            return RT_EOK;            
        }
            
        if(size == 0)
        {
还_没_想_好's avatar
还_没_想_好 已提交
833
            size = dcd_ep_read(device->dcd, EP_ADDRESS(ep), ep->request.buffer);
qiuyiuestc's avatar
qiuyiuestc 已提交
834
        }
还_没_想_好's avatar
还_没_想_好 已提交
835 836
        ep->request.remain_size -= size;
        ep->request.buffer += size;
qiuyiuestc's avatar
qiuyiuestc 已提交
837

还_没_想_好's avatar
还_没_想_好 已提交
838
        if(ep->request.req_type == UIO_REQUEST_READ_BEST)
839 840 841 842 843 844 845
        {
            EP_HANDLER(ep, func, size);
        }
        else if(ep->request.remain_size == 0)
        {
            EP_HANDLER(ep, func, ep->request.size);
        }
846 847 848 849
        else
        {
            dcd_ep_read_prepare(device->dcd, EP_ADDRESS(ep), ep->request.buffer, ep->request.remain_size > EP_MAXPACKET(ep) ? EP_MAXPACKET(ep) : ep->request.remain_size);
        }
850 851 852 853 854 855 856 857 858 859
    }

    return RT_EOK;
}

static rt_err_t _ep0_out_notify(udevice_t device, struct ep_msg* ep_msg)
{
    uep_t ep0;
    rt_size_t size;
    
M
Ming, Bai 已提交
860
    RT_ASSERT(device != RT_NULL);
861 862
    RT_ASSERT(ep_msg != RT_NULL);    
    RT_ASSERT(device->dcd != RT_NULL);
M
Ming, Bai 已提交
863

864 865
    ep0 = &device->dcd->ep0;
    size = ep_msg->size;
还_没_想_好's avatar
还_没_想_好 已提交
866

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    if(ep0->request.remain_size == 0)
    {
        return RT_EOK;            
    }    
    if(size == 0)
    {
        size = dcd_ep_read(device->dcd, EP0_OUT_ADDR, ep0->request.buffer);
        if(size == 0)
        {
            return RT_EOK;
        }        
    }

    ep0->request.remain_size -= size; 
    ep0->request.buffer += size;
    if(ep0->request.remain_size == 0)
M
Ming, Bai 已提交
883
    {
884 885 886 887 888
        /* invoke callback */
        if(ep0->rx_indicate != RT_NULL)
        {
            ep0->rx_indicate(device, size);
        }        
M
Ming, Bai 已提交
889
    }
lymzzyh's avatar
lymzzyh 已提交
890 891 892 893
    else
    {
        rt_usbd_ep0_read(device, ep0->request.buffer, ep0->request.remain_size,ep0->rx_indicate);
    }
M
Ming, Bai 已提交
894

895
    return RT_EOK;
M
Ming, Bai 已提交
896 897
}

898
/**
899
 * This function will notity sof event to all of function.
900 901 902 903 904
 *
 * @param device the usb device object.
 *
 * @return RT_EOK.
 */
905
static rt_err_t _sof_notify(udevice_t device)
906 907
{
    struct rt_list_node *i;
908
    ufunction_t func;
909 910 911

    RT_ASSERT(device != RT_NULL);

912 913 914
    /* to notity every function that sof event comes */
    for (i=device->curr_cfg->func_list.next;
            i!=&device->curr_cfg->func_list; i=i->next)
915
    {
916 917 918
        func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
        if(func->ops->sof_handler != RT_NULL)
            func->ops->sof_handler(func);
G
Grissiom 已提交
919
    }
920

G
Grissiom 已提交
921 922 923 924
    return RT_EOK;
}

/**
925
 * This function will disable all USB functions.
G
Grissiom 已提交
926 927 928 929 930
 *
 * @param device the usb device object.
 *
 * @return RT_EOK.
 */
931
static rt_err_t _stop_notify(udevice_t device)
G
Grissiom 已提交
932 933
{
    struct rt_list_node *i;
934
    ufunction_t func;
G
Grissiom 已提交
935 936 937

    RT_ASSERT(device != RT_NULL);

938 939 940
    /* to notity every function */
    for (i  = device->curr_cfg->func_list.next;
         i != &device->curr_cfg->func_list;
G
Grissiom 已提交
941 942
         i  = i->next)
    {
943 944
        func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
        FUNC_DISABLE(func);
945 946 947 948 949
    }

    return RT_EOK;
}

950
static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
G
Grissiom 已提交
951
{
952 953 954 955 956
    rt_uint16_t maxpacket;
        
    RT_ASSERT(device != RT_NULL);    
    RT_ASSERT(device->dcd != RT_NULL);
    RT_ASSERT(ep != RT_NULL);    
G
Grissiom 已提交
957

958
    rt_enter_critical();
959 960 961
    maxpacket = EP_MAXPACKET(ep);
    if(ep->request.remain_size >= maxpacket)
    {
还_没_想_好's avatar
还_没_想_好 已提交
962 963
        dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, maxpacket);
        ep->request.remain_size -= maxpacket;
964 965 966 967
        ep->request.buffer += maxpacket;    
    }
    else
    {
还_没_想_好's avatar
还_没_想_好 已提交
968
        dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, 
969
            ep->request.remain_size);
还_没_想_好's avatar
还_没_想_好 已提交
970
        ep->request.remain_size = 0;
971
    }
972
    rt_exit_critical();
还_没_想_好's avatar
还_没_想_好 已提交
973
    return size;
974
}
G
Grissiom 已提交
975

976 977 978 979 980 981 982 983
static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
{
    RT_ASSERT(device != RT_NULL);	
    RT_ASSERT(device->dcd != RT_NULL);
    RT_ASSERT(ep != RT_NULL);    
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);

984
    return dcd_ep_read_prepare(device->dcd, EP_ADDRESS(ep), buffer, size > EP_MAXPACKET(ep) ? EP_MAXPACKET(ep) : size);
G
Grissiom 已提交
985 986
}

M
Ming, Bai 已提交
987 988 989 990 991 992 993
/**
 * 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.
 */
994
udevice_t rt_usbd_device_new(void)
M
Ming, Bai 已提交
995 996 997
{
    udevice_t udevice;

998
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_new\n"));
M
Ming, Bai 已提交
999 1000 1001

    /* allocate memory for the object */
    udevice = rt_malloc(sizeof(struct udevice));
1002
    if(udevice == RT_NULL)
M
Ming, Bai 已提交
1003 1004 1005 1006 1007
    {
        rt_kprintf("alloc memery failed\n");
        return RT_NULL;
    }
    rt_memset(udevice, 0, sizeof(struct udevice));
1008

M
Ming, Bai 已提交
1009 1010 1011 1012
    /* to initialize configuration list */
    rt_list_init(&udevice->cfg_list);

    /* insert the device object to device list */
1013
    rt_list_insert_before(&device_list, &udevice->list);
M
Ming, Bai 已提交
1014 1015 1016 1017

    return udevice;
}

1018 1019 1020
/**
 * This function will set usb device string description.
 *
1021
 * @param device the usb device object.
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
 * @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;
}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
rt_err_t rt_usbd_device_set_os_comp_id_desc(udevice_t device, usb_os_comp_id_desc_t os_comp_id_desc)
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(os_comp_id_desc != RT_NULL);

    /* set string descriptor array to the device object */
    device->os_comp_id_desc = os_comp_id_desc;
    rt_list_init(&device->os_comp_id_desc->func_desc);
    return RT_EOK;
}

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
rt_err_t rt_usbd_device_set_qualifier(udevice_t device, struct usb_qualifier_descriptor* qualifier)
{
    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(qualifier != RT_NULL);

    device->dev_qualifier = qualifier;

    return RT_EOK;
}

M
Ming, Bai 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
/**
 * 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);
1094

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

M
Ming, Bai 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    return RT_EOK;
}

/**
 * This function will create an usb configuration object.
 *
 * @param none.
 *
 * @return an usb configuration object.
 */
1108
uconfig_t rt_usbd_config_new(void)
M
Ming, Bai 已提交
1109 1110 1111
{
    uconfig_t cfg;

1112
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_new\n"));
M
Ming, Bai 已提交
1113 1114 1115

    /* allocate memory for the object */
    cfg = rt_malloc(sizeof(struct uconfig));
1116
    if(cfg == RT_NULL)
M
Ming, Bai 已提交
1117 1118 1119 1120 1121 1122
    {
        rt_kprintf("alloc memery failed\n");
        return RT_NULL;
    }
    rt_memset(cfg, 0, sizeof(struct uconfig));

还_没_想_好's avatar
还_没_想_好 已提交
1123
    /* set default wValue */
1124 1125
    cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
    cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
M
Ming, Bai 已提交
1126 1127
    cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
    cfg->cfg_desc.bmAttributes = 0xC0;
1128
    cfg->cfg_desc.MaxPower = 0x32;
M
Ming, Bai 已提交
1129

1130 1131
    /* to initialize function object list */
    rt_list_init(&cfg->func_list);
M
Ming, Bai 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

    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.
 */
1144
uintf_t rt_usbd_interface_new(udevice_t device, uintf_handler_t handler)
M
Ming, Bai 已提交
1145 1146 1147
{
    uintf_t intf;

1148
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_new\n"));
M
Ming, Bai 已提交
1149 1150 1151

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

M
Ming, Bai 已提交
1153
    /* allocate memory for the object */
1154
    intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
1155
    if(intf == RT_NULL)
M
Ming, Bai 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164
    {
        rt_kprintf("alloc memery failed\n");
        return RT_NULL;
    }
    intf->intf_num = device->nr_intf;
    device->nr_intf++;
    intf->handler = handler;
    intf->curr_setting = RT_NULL;

1165
    /* to initialize the alternate setting object list */
M
Ming, Bai 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
    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.
 */
1179
ualtsetting_t rt_usbd_altsetting_new(rt_size_t desc_size)
M
Ming, Bai 已提交
1180 1181 1182
{
    ualtsetting_t setting;

1183
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_new\n"));
M
Ming, Bai 已提交
1184 1185 1186 1187 1188 1189

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

    /* allocate memory for the object */
    setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
1190
    if(setting == RT_NULL)
M
Ming, Bai 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
    {
        rt_kprintf("alloc memery failed\n");
        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);
1201
        return RT_NULL;
M
Ming, Bai 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
    }

    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.
 */
1222
rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
M
Ming, Bai 已提交
1223 1224 1225
{
    RT_ASSERT(setting != RT_NULL);
    RT_ASSERT(setting->desc !=RT_NULL);
1226

M
Ming, Bai 已提交
1227
    rt_memcpy(setting->desc, desc, setting->desc_size);
1228
    setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
1229 1230

    return RT_EOK;
M
Ming, Bai 已提交
1231 1232 1233
}

/**
1234
 * This function will create an usb function object.
M
Ming, Bai 已提交
1235 1236 1237 1238 1239
 *
 * @param device the usb device object.
 * @param dev_desc the device descriptor.
 * @param ops the operation set.
 *
1240
 * @return an usb function object on success, RT_NULL on fail.
M
Ming, Bai 已提交
1241
 */
1242 1243
ufunction_t rt_usbd_function_new(udevice_t device, udev_desc_t dev_desc,
                              ufunction_ops_t ops)
M
Ming, Bai 已提交
1244
{
1245
    ufunction_t func;
M
Ming, Bai 已提交
1246

1247
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_function_new\n"));
M
Ming, Bai 已提交
1248 1249 1250 1251 1252 1253

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

    /* allocate memory for the object */
1254 1255
    func = (ufunction_t)rt_malloc(sizeof(struct ufunction));
    if(func == RT_NULL)
M
Ming, Bai 已提交
1256 1257 1258 1259
    {
        rt_kprintf("alloc memery failed\n");
        return RT_NULL;
    }
1260 1261 1262 1263
    func->dev_desc = dev_desc;
    func->ops = ops;
    func->device = device;
    func->enabled = RT_FALSE;
M
Ming, Bai 已提交
1264 1265

    /* to initialize interface list */
1266
    rt_list_init(&func->intf_list);
M
Ming, Bai 已提交
1267

1268
    return func;
M
Ming, Bai 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
}

/**
 * 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.
 */
1279
uep_t rt_usbd_endpoint_new(uep_desc_t ep_desc, udep_handler_t handler)
M
Ming, Bai 已提交
1280 1281 1282
{
    uep_t ep;

1283
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_new\n"));
M
Ming, Bai 已提交
1284 1285 1286 1287 1288

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

    /* allocate memory for the object */
1289
    ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
1290
    if(ep == RT_NULL)
M
Ming, Bai 已提交
1291 1292 1293 1294 1295 1296 1297
    {
        rt_kprintf("alloc memery failed\n");
        return RT_NULL;
    }
    ep->ep_desc = ep_desc;
    ep->handler = handler;
    ep->buffer  = RT_NULL;
1298 1299
    ep->stalled = RT_FALSE;
    rt_list_init(&ep->request_list);
M
Ming, Bai 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312

    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)
{
1313
    struct rt_list_node* node;
M
Ming, Bai 已提交
1314
    udevice_t device;
1315

M
Ming, Bai 已提交
1316 1317
    /* parameter check */
    RT_ASSERT(dcd != RT_NULL);
1318

M
Ming, Bai 已提交
1319 1320 1321 1322
    /* 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);
1323
        if(device->dcd == dcd) return device;
M
Ming, Bai 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    }

    rt_kprintf("can't find device\n");
    return RT_NULL;
}

/**
 * This function will find an usb configuration object.
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
1334
 * @param wValue the configuration number.
M
Ming, Bai 已提交
1335 1336 1337 1338 1339
 *
 * @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)
{
1340
    struct rt_list_node* node;
M
Ming, Bai 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349
    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 */
1350
    for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
M
Ming, Bai 已提交
1351 1352
    {
        cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
1353 1354
        if(cfg->cfg_desc.bConfigurationValue == value)
        {
1355
            return cfg;
1356
        }
M
Ming, Bai 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
    }

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

/**
 * This function will find an usb interface object.
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
1367
 * @param wValue the interface number.
M
Ming, Bai 已提交
1368 1369 1370
 *
 * @return an usb configuration object on found or RT_NULL on not found.
 */
1371
uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *pfunc)
1372
{
M
Ming, Bai 已提交
1373
    struct rt_list_node *i, *j;
1374
    ufunction_t func;
M
Ming, Bai 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383
    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 */
1384 1385
    for (i=device->curr_cfg->func_list.next;
            i!=&device->curr_cfg->func_list; i=i->next)
M
Ming, Bai 已提交
1386
    {
1387 1388
        func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
        for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
M
Ming, Bai 已提交
1389 1390
        {
            intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
1391
            if(intf->intf_num == value)
M
Ming, Bai 已提交
1392
            {
1393 1394
                if (pfunc != RT_NULL)
                    *pfunc = func;
M
Ming, Bai 已提交
1395 1396 1397
                return intf;
            }
        }
1398
    }
M
Ming, Bai 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407

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

/**
 * This function will find an usb interface alternate setting object.
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
1408
 * @param wValue the alternate setting number.
M
Ming, Bai 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
 *
 * @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);

1422
    if(intf->curr_setting != RT_NULL)
M
Ming, Bai 已提交
1423
    {
还_没_想_好's avatar
还_没_想_好 已提交
1424
        /* if the wValue equal to the current alternate setting, then do not search */
1425
        if(intf->curr_setting->intf_desc->bAlternateSetting == value)
M
Ming, Bai 已提交
1426 1427
            return intf->curr_setting;
    }
1428

M
Ming, Bai 已提交
1429
    /* search a setting in the alternate setting list */
1430
    for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
M
Ming, Bai 已提交
1431 1432
    {
        setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
1433
        if(setting->intf_desc->bAlternateSetting == value)
M
Ming, Bai 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
            return setting;
    }

    rt_kprintf("can't find alternate setting %d\n", value);
    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.
 */
1449
uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_addr)
M
Ming, Bai 已提交
1450 1451 1452
{
    uep_t ep;
    struct rt_list_node *i, *j, *k;
1453
    ufunction_t func;
M
Ming, Bai 已提交
1454 1455 1456 1457
    uintf_t intf;

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

M
Ming, Bai 已提交
1459
    /* search a endpoint in the current configuration */
还_没_想_好's avatar
还_没_想_好 已提交
1460
    for (i=device->curr_cfg->func_list.next; i!=&device->curr_cfg->func_list; i=i->next)
M
Ming, Bai 已提交
1461
    {
1462 1463
        func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
        for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
M
Ming, Bai 已提交
1464 1465
        {
            intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
1466 1467
            for(k=intf->curr_setting->ep_list.next;
                    k!=&intf->curr_setting->ep_list; k=k->next)
M
Ming, Bai 已提交
1468 1469
            {
                ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
1470
                if(EP_ADDRESS(ep) == ep_addr)
M
Ming, Bai 已提交
1471
                {
1472 1473
                    if (pfunc != RT_NULL)
                        *pfunc = func;
M
Ming, Bai 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
                    return ep;
                }
            }
        }
    }

    rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
    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;
1495
    ufunction_t func;
M
Ming, Bai 已提交
1496 1497 1498 1499 1500 1501 1502
    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);
1503
    RT_ASSERT(cfg != RT_NULL);
M
Ming, Bai 已提交
1504 1505 1506 1507 1508

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

1509
    for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next)
M
Ming, Bai 已提交
1510
    {
1511
        func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
M
Ming, Bai 已提交
1512

1513
        for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
M
Ming, Bai 已提交
1514 1515 1516
        {
            intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
            cfg->cfg_desc.bNumInterfaces++;
1517

M
Ming, Bai 已提交
1518
            /* allocate address for every endpoint in the interface alternate setting */
1519 1520
            for(k=intf->curr_setting->ep_list.next;
                    k!=&intf->curr_setting->ep_list; k=k->next)
M
Ming, Bai 已提交
1521 1522
            {
                ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
1523 1524 1525 1526
                if(rt_usbd_ep_assign(device, ep) != RT_EOK)
                {
                    rt_kprintf("endpoint assign error\n");
                }
M
Ming, Bai 已提交
1527 1528 1529
            }

            /* construct complete configuration descriptor */
1530 1531 1532
            rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength - USB_DESC_LENGTH_CONFIG], 
                        (void*)intf->curr_setting->desc,
                        intf->curr_setting->desc_size);
M
Ming, Bai 已提交
1533 1534
            cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
        }
1535
    }
M
Ming, Bai 已提交
1536 1537

    /* insert the configuration to the list */
1538
    rt_list_insert_before(&device->cfg_list, &cfg->list);
M
Ming, Bai 已提交
1539 1540 1541 1542 1543

    return RT_EOK;
}

/**
1544
 * This function will add a function to a configuration.
M
Ming, Bai 已提交
1545 1546
 *
 * @param cfg the configuration object.
1547
 * @param func the function object.
M
Ming, Bai 已提交
1548 1549 1550
 *
 * @return RT_EOK.
 */
1551
rt_err_t rt_usbd_config_add_function(uconfig_t cfg, ufunction_t func)
M
Ming, Bai 已提交
1552
{
1553
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_function\n"));
M
Ming, Bai 已提交
1554 1555 1556

    /* parameter check */
    RT_ASSERT(cfg != RT_NULL);
1557
    RT_ASSERT(func != RT_NULL);
M
Ming, Bai 已提交
1558

1559 1560
    /* insert the function to the list */
    rt_list_insert_before(&cfg->func_list, &func->list);
1561

M
Ming, Bai 已提交
1562 1563 1564 1565
    return RT_EOK;
}

/**
1566
 * This function will add an interface to a function.
M
Ming, Bai 已提交
1567
 *
1568
 * @param func the function object.
M
Ming, Bai 已提交
1569 1570 1571 1572
 * @param intf the interface object.
 *
 * @return RT_EOK.
 */
1573
rt_err_t rt_usbd_function_add_interface(ufunction_t func, uintf_t intf)
M
Ming, Bai 已提交
1574 1575
{

1576
    RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_function_add_interface\n"));
M
Ming, Bai 已提交
1577 1578

    /* parameter check */
1579
    RT_ASSERT(func != RT_NULL);
M
Ming, Bai 已提交
1580 1581 1582
    RT_ASSERT(intf != RT_NULL);

    /* insert the interface to the list */
1583
    rt_list_insert_before(&func->intf_list, &intf->list);
M
Ming, Bai 已提交
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

    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 */
1607
    rt_list_insert_before(&intf->setting_list, &setting->list);
M
Ming, Bai 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628

    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 */
1629
    rt_list_insert_before(&setting->ep_list, &ep->list);
M
Ming, Bai 已提交
1630 1631 1632 1633

    return RT_EOK;
}

1634 1635 1636 1637 1638 1639 1640 1641 1642
rt_err_t rt_usbd_os_comp_id_desc_add_os_func_comp_id_desc(usb_os_comp_id_desc_t os_comp_id_desc, usb_os_func_comp_id_desc_t os_func_comp_id_desc)
{
    RT_ASSERT(os_comp_id_desc != RT_NULL);
    RT_ASSERT(os_func_comp_id_desc != RT_NULL);
    rt_list_insert_before(&os_comp_id_desc->func_desc, &os_func_comp_id_desc->list);
    os_comp_id_desc->head_desc.bCount++;
    return RT_EOK;
}

M
Ming, Bai 已提交
1643 1644 1645 1646
/**
 * This function will set an alternate setting for an interface.
 *
 * @param intf_desc the interface descriptor.
还_没_想_好's avatar
还_没_想_好 已提交
1647
 * @param wValue the alternate setting number.
M
Ming, Bai 已提交
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
 *
 * @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.
还_没_想_好's avatar
还_没_想_好 已提交
1673
 * @param wValue the configuration number.
M
Ming, Bai 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
 *
 * @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;
还_没_想_好's avatar
还_没_想_好 已提交
1692 1693

    dcd_set_config(device->dcd, value);
1694
    
M
Ming, Bai 已提交
1695 1696 1697
    return RT_TRUE;
}

1698
/**
还_没_想_好's avatar
还_没_想_好 已提交
1699
 * This function will bRequest an IO transaction.
1700 1701 1702
 *
 * @param device the usb device object.
 * @param ep the endpoint object. 
还_没_想_好's avatar
还_没_想_好 已提交
1703
 * @param req IO bRequest.
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
 *
 * @return RT_EOK.
 */
rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req)
{
    rt_size_t size = 0;
    
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(req != RT_NULL);

    if(ep->stalled == RT_FALSE)
    {
        switch(req->req_type)
        {
还_没_想_好's avatar
还_没_想_好 已提交
1718
        case UIO_REQUEST_READ_BEST:
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
        case UIO_REQUEST_READ_FULL:
            ep->request.remain_size = ep->request.size;
            size = rt_usbd_ep_read_prepare(device, ep, req->buffer, req->size);
            break;
        case UIO_REQUEST_WRITE:
            ep->request.remain_size = ep->request.size;
            size = rt_usbd_ep_write(device, ep, req->buffer, req->size);
            break;
        default:
            rt_kprintf("unknown request type\n");
            break;
        }
    }
    else
    {
        rt_list_insert_before(&ep->request_list, &req->list);
        RT_DEBUG_LOG(RT_DEBUG_USB, ("suspend a request\n"));
    }            

    return size;
}

/**
 * This function will set feature for an usb device.
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
1745
 * @param wValue the configuration number.
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
{
    RT_ASSERT(device != RT_NULL);

    if (value == USB_FEATURE_DEV_REMOTE_WAKEUP)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("set feature remote wakeup\n"));
    }
    else if (value == USB_FEATURE_ENDPOINT_HALT)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("set feature stall\n"));    
        dcd_ep_set_stall(device->dcd, (rt_uint32_t)(index & 0xFF));
    }
    
    return RT_EOK;
}

/**
 * This function will clear feature for an usb device.
 *
 * @param device the usb device object.
还_没_想_好's avatar
还_没_想_好 已提交
1770
 * @param wValue the configuration number.
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
 *
 * @return RT_EOK.
 */
rt_err_t rt_usbd_clear_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
{
    RT_ASSERT(device != RT_NULL);

    if (value == USB_FEATURE_DEV_REMOTE_WAKEUP)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("clear feature remote wakeup\n"));
    }
    else if (value == USB_FEATURE_ENDPOINT_HALT)
    {
        RT_DEBUG_LOG(RT_DEBUG_USB, ("clear feature stall\n"));
        dcd_ep_clear_stall(device->dcd, (rt_uint32_t)(index & 0xFF));
    }
    
    return RT_EOK;
}

rt_err_t rt_usbd_ep0_set_stall(udevice_t device)
{
    RT_ASSERT(device != RT_NULL);
    
    return dcd_ep_set_stall(device->dcd, 0);
}

rt_err_t rt_usbd_ep0_clear_stall(udevice_t device)
{
    RT_ASSERT(device != RT_NULL);
    
    return dcd_ep_clear_stall(device->dcd, 0);
}

rt_err_t rt_usbd_ep_set_stall(udevice_t device, uep_t ep)
{
    rt_err_t ret;
    
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);  

    ret = dcd_ep_set_stall(device->dcd, EP_ADDRESS(ep));
    if(ret == RT_EOK)
    {
        ep->stalled = RT_TRUE;
    }
    
    return ret;
}

rt_err_t rt_usbd_ep_clear_stall(udevice_t device, uep_t ep)
{
    rt_err_t ret;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);

    ret = dcd_ep_clear_stall(device->dcd, EP_ADDRESS(ep));
    if(ret == RT_EOK)
    {
        ep->stalled = RT_FALSE;
    }
    
    return ret;
}

static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep)
{
    int i = 0;
    
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->dcd != RT_NULL);    
    RT_ASSERT(device->dcd->ep_pool != RT_NULL);        
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);

    while(device->dcd->ep_pool[i].addr != 0xFF)
    {
        if(device->dcd->ep_pool[i].status == ID_UNASSIGNED && 
1852
            ep->ep_desc->bmAttributes == device->dcd->ep_pool[i].type && (EP_ADDRESS(ep) & 0x80) == device->dcd->ep_pool[i].dir)
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
        {
            EP_ADDRESS(ep) |= device->dcd->ep_pool[i].addr;
            ep->id = &device->dcd->ep_pool[i];
            device->dcd->ep_pool[i].status = ID_ASSIGNED;

            RT_DEBUG_LOG(RT_DEBUG_USB, ("assigned %d\n", device->dcd->ep_pool[i].addr));  
            return RT_EOK;
        }
        
        i++;
    }
    
    return -RT_ERROR;
}

static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep)
{
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->dcd != RT_NULL);    
    RT_ASSERT(device->dcd->ep_pool != RT_NULL);        
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);

    ep->id->status = ID_UNASSIGNED;

    return RT_EOK;
}

qiuyiuestc's avatar
qiuyiuestc 已提交
1881
rt_err_t rt_usbd_ep0_setup_handler(udcd_t dcd, struct urequest* setup)
1882 1883 1884 1885 1886 1887 1888 1889 1890
{
    struct udev_msg msg;
    rt_size_t size;

    RT_ASSERT(dcd != RT_NULL);

    if(setup == RT_NULL)
    {
        size = dcd_ep_read(dcd, EP0_OUT_ADDR, (void*)&msg.content.setup);
qiuyiuestc's avatar
qiuyiuestc 已提交
1891
        if(size != sizeof(struct urequest))
1892 1893 1894 1895 1896 1897 1898
        {
            rt_kprintf("read setup packet error\n");
            return -RT_ERROR;
        }
    }
    else
    {
qiuyiuestc's avatar
qiuyiuestc 已提交
1899
        rt_memcpy((void*)&msg.content.setup, (void*)setup, sizeof(struct urequest));
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
    }    
    
    msg.type = USB_MSG_SETUP_NOTIFY;
    msg.dcd = dcd;
    rt_usbd_event_signal(&msg);

    return RT_EOK;
}

rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd)
{
1911 1912
    rt_int32_t remain, mps;

1913 1914
    RT_ASSERT(dcd != RT_NULL);

1915 1916 1917 1918 1919 1920 1921 1922
    if (dcd->stage != STAGE_DIN)
        return RT_EOK;

    mps = dcd->ep0.id->maxpacket;
    dcd->ep0.request.remain_size -= mps;
    remain = dcd->ep0.request.remain_size;

    if (remain > 0)
1923
    {
1924 1925 1926 1927 1928 1929 1930
        if (remain >= mps)
        {
            remain = mps;
        }

        dcd->ep0.request.buffer += mps;
        dcd_ep_write(dcd, EP0_IN_ADDR, dcd->ep0.request.buffer, remain);
1931 1932 1933
    }
    else
    {
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
        /* last packet is MPS multiple, so send ZLP packet */
        if ((remain == 0) && (dcd->ep0.request.size > 0))
        {
            dcd->ep0.request.size = 0;
            dcd_ep_write(dcd, EP0_IN_ADDR, RT_NULL, 0);
        }
        else
        {
            /* receive status */
            dcd->stage = STAGE_STATUS_OUT;
            dcd_ep_read_prepare(dcd, EP0_OUT_ADDR, RT_NULL, 0);
        }
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
    }

    return RT_EOK;
}

rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size)
{
    struct udev_msg msg;

    RT_ASSERT(dcd != RT_NULL);

    msg.type = USB_MSG_EP0_OUT;
    msg.dcd = dcd;
    msg.content.ep_msg.size = size;
    rt_usbd_event_signal(&msg);

    return RT_EOK;
}

还_没_想_好's avatar
还_没_想_好 已提交
1965
rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
1966 1967 1968 1969 1970 1971 1972 1973
{
    struct udev_msg msg;

    RT_ASSERT(dcd != RT_NULL);

    msg.type = USB_MSG_DATA_NOTIFY;
    msg.dcd = dcd;
    msg.content.ep_msg.ep_addr = address;
还_没_想_好's avatar
还_没_想_好 已提交
1974
    msg.content.ep_msg.size = size;
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
    rt_usbd_event_signal(&msg);

    return RT_EOK;
}

rt_err_t rt_usbd_ep_out_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
{
    struct udev_msg msg;

    RT_ASSERT(dcd != RT_NULL);

    msg.type = USB_MSG_DATA_NOTIFY;
    msg.dcd = dcd;
    msg.content.ep_msg.ep_addr = address;
    msg.content.ep_msg.size = size;
    rt_usbd_event_signal(&msg);

    return RT_EOK;
}

rt_err_t rt_usbd_reset_handler(udcd_t dcd)
{
    struct udev_msg msg;

    RT_ASSERT(dcd != RT_NULL);
    
    msg.type = USB_MSG_RESET;
    msg.dcd = dcd;
    rt_usbd_event_signal(&msg);

    return RT_EOK;
}

rt_err_t rt_usbd_connect_handler(udcd_t dcd)
{
    struct udev_msg msg;

    RT_ASSERT(dcd != RT_NULL);
    
    msg.type = USB_MSG_PLUG_IN;
    msg.dcd = dcd;
    rt_usbd_event_signal(&msg);

    return RT_EOK;
}

rt_err_t rt_usbd_disconnect_handler(udcd_t dcd)
{
    struct udev_msg msg;

    RT_ASSERT(dcd != RT_NULL);
    
    msg.type = USB_MSG_PLUG_OUT;
    msg.dcd = dcd;
    rt_usbd_event_signal(&msg);

    return RT_EOK;
}

rt_err_t rt_usbd_sof_handler(udcd_t dcd)
{
    struct udev_msg msg;

    RT_ASSERT(dcd != RT_NULL);
    
    msg.type = USB_MSG_SOF;
    msg.dcd = dcd;
    rt_usbd_event_signal(&msg);

    return RT_EOK;
}

rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size)
{
    uep_t ep0;
2050
    rt_size_t sent_size = 0;
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060

    RT_ASSERT(device != RT_NULL);    
    RT_ASSERT(device->dcd != RT_NULL);
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size > 0);

    ep0 = &device->dcd->ep0;
    ep0->request.size = size;
    ep0->request.buffer = buffer;
    ep0->request.remain_size = size;
2061
    if(size >= ep0->id->maxpacket)
2062
    {
2063
        sent_size = ep0->id->maxpacket;
2064 2065 2066
    }
    else
    {
2067
        sent_size = size;
2068
    }
2069
    device->dcd->stage = STAGE_DIN;
2070

2071
    return dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, sent_size);
2072 2073 2074 2075 2076 2077
}

rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size, 
    rt_err_t (*rx_ind)(udevice_t device, rt_size_t size))
{
    uep_t ep0;
lymzzyh's avatar
lymzzyh 已提交
2078
    rt_size_t read_size = 0;
2079 2080 2081 2082 2083 2084 2085 2086 2087

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->dcd != RT_NULL);
    RT_ASSERT(buffer != RT_NULL);

    ep0 = &device->dcd->ep0;
    ep0->request.buffer = buffer;    
    ep0->request.remain_size = size;
    ep0->rx_indicate = rx_ind;
lymzzyh's avatar
lymzzyh 已提交
2088 2089 2090 2091 2092 2093 2094 2095
    if(size >= ep0->id->maxpacket)
    {
        read_size = ep0->id->maxpacket;
    }
    else
    {
        read_size = size;
    }
2096
    device->dcd->stage = STAGE_DOUT;
lymzzyh's avatar
lymzzyh 已提交
2097
    dcd_ep_read_prepare(device->dcd, EP0_OUT_ADDR, buffer, read_size);
2098 2099 2100 2101

    return size;
}

2102
static struct rt_messagequeue usb_mq;
M
Ming, Bai 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111

/**
 * 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.
 */
2112
static void rt_usbd_thread_entry(void* parameter)
2113
{
2114
    while(1)
2115 2116
    {
        struct udev_msg msg;
M
Ming, Bai 已提交
2117
        udevice_t device;
2118

M
Ming, Bai 已提交
2119
        /* receive message */
2120 2121
        if(rt_mq_recv(&usb_mq, &msg, sizeof(struct udev_msg),
                    RT_WAITING_FOREVER) != RT_EOK )
G
Grissiom 已提交
2122 2123 2124
            continue;

        device = rt_usbd_find_device(msg.dcd);
2125
        if(device == RT_NULL)
G
Grissiom 已提交
2126 2127 2128 2129
        {
            rt_kprintf("invalid usb device\n");
            continue;
        }
2130

2131 2132
        RT_DEBUG_LOG(RT_DEBUG_USB, ("message type %d\n", msg.type));
        
M
Ming, Bai 已提交
2133
        switch (msg.type)
2134
        {
2135 2136
        case USB_MSG_SOF:
            _sof_notify(device);
M
Ming, Bai 已提交
2137 2138
            break;
        case USB_MSG_DATA_NOTIFY:
2139 2140
            /* some buggy drivers will have USB_MSG_DATA_NOTIFY before the core
             * got configured. */
2141
            _data_notify(device, &msg.content.ep_msg);
M
Ming, Bai 已提交
2142
            break;
2143
        case USB_MSG_SETUP_NOTIFY:
2144 2145 2146 2147
            _setup_request(device, &msg.content.setup);
            break;
        case USB_MSG_EP0_OUT:
            _ep0_out_notify(device, &msg.content.ep_msg);
M
Ming, Bai 已提交
2148
            break;
2149 2150
        case USB_MSG_RESET:            
            RT_DEBUG_LOG(RT_DEBUG_USB, ("reset %d\n", device->state));
lymzzyh's avatar
lymzzyh 已提交
2151
            if (device->state == USB_STATE_ADDRESS || device->state == USB_STATE_CONFIGURED)
2152
                _stop_notify(device);
lymzzyh's avatar
lymzzyh 已提交
2153
            device->state = USB_STATE_NOTATTACHED;
2154 2155 2156
            break;
        case USB_MSG_PLUG_IN:
            device->state = USB_STATE_ATTACHED;
2157
            break;
G
Grissiom 已提交
2158
        case USB_MSG_PLUG_OUT:
2159
            device->state = USB_STATE_NOTATTACHED;    
G
Grissiom 已提交
2160 2161
            _stop_notify(device);
            break;
M
Ming, Bai 已提交
2162
        default:
2163
            rt_kprintf("unknown msg type %d\n", msg.type);
M
Ming, Bai 已提交
2164
            break;
2165
        }
M
Ming, Bai 已提交
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
    }
}

/**
 * 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.
 */
2177
rt_err_t rt_usbd_event_signal(struct udev_msg* msg)
M
Ming, Bai 已提交
2178 2179 2180 2181
{
    RT_ASSERT(msg != RT_NULL);

    /* send message to usb message queue */
2182
    return rt_mq_send(&usb_mq, (void*)msg, sizeof(struct udev_msg));
M
Ming, Bai 已提交
2183 2184
}

2185 2186 2187 2188

ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
static struct rt_thread usb_thread;
2189 2190 2191 2192 2193 2194
#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];
2195

M
Ming, Bai 已提交
2196 2197 2198 2199
/**
 * This function will initialize usb device thread.
 *
 * @return none.
2200
 *
M
Ming, Bai 已提交
2201 2202 2203 2204
 */
rt_err_t rt_usbd_core_init(void)
{
    rt_list_init(&device_list);
2205

M
Ming, Bai 已提交
2206
    /* create an usb message queue */
还_没_想_好's avatar
还_没_想_好 已提交
2207 2208 2209 2210 2211
    rt_mq_init(&usb_mq,
               "usbd",
               usb_mq_pool, USBD_MQ_MSG_SZ,
               sizeof(usb_mq_pool),
               RT_IPC_FLAG_FIFO);
2212

2213
    /* init usb device thread */
还_没_想_好's avatar
还_没_想_好 已提交
2214 2215 2216 2217 2218
    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);
2219 2220 2221
    /* rt_thread_init should always be OK, so start the thread without further
     * checking. */
    return rt_thread_startup(&usb_thread);
M
Ming, Bai 已提交
2222
}
2223