ecm.c 18.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * File      : ecm.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006-2013, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author            Notes
 * 2017-11-19     ZYH               first version
 */

#include <rtdevice.h>
#include "cdc.h"


#ifdef  ECM_DEBUG
#define ECM_PRINTF                rt_kprintf("[ECM] "); rt_kprintf
#else
#define ECM_PRINTF(...)
#endif /* ECM_DEBUG */

/* RT-Thread LWIP ethernet interface */
#include <netif/ethernetif.h>

#ifndef USB_ETH_MTU
#define USB_ETH_MTU     1514
#endif
#define MAX_ADDR_LEN    6

struct rt_ecm_eth
{
    /* inherit from ethernet device */
    struct eth_device       parent;
    struct ufunction *      func;
    struct cdc_eps          eps;
    /* interface address info */
    rt_uint8_t              host_addr[MAX_ADDR_LEN];
    rt_uint8_t              dev_addr[MAX_ADDR_LEN];

    ALIGN(4)
44
    rt_uint8_t              rx_pool[512];
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    ALIGN(4)
    rt_size_t               rx_size;
    ALIGN(4)
    rt_size_t               rx_offset;
    ALIGN(4)
    char                    rx_buffer[USB_ETH_MTU];
    char                    tx_buffer[USB_ETH_MTU];
    
    struct rt_semaphore     tx_buffer_free;
    
};
typedef struct rt_ecm_eth * rt_ecm_eth_t;

ALIGN(4)
static struct udevice_descriptor _dev_desc =
{
    USB_DESC_LENGTH_DEVICE,     /* bLength */
    USB_DESC_TYPE_DEVICE,       /* type */
    USB_BCD_VERSION,            /* bcdUSB */
    USB_CLASS_CDC,              /* bDeviceClass */
    USB_CDC_SUBCLASS_ETH,       /* bDeviceSubClass */
    USB_CDC_PROTOCOL_NONE,      /* bDeviceProtocol */
    0x40,                       /* bMaxPacketSize0 */
    _VENDOR_ID,                 /* idVendor */
    _PRODUCT_ID,                /* idProduct */
    USB_BCD_DEVICE,             /* bcdDevice */
    USB_STRING_MANU_INDEX,      /* iManufacturer */
    USB_STRING_PRODUCT_INDEX,   /* iProduct */
    USB_STRING_SERIAL_INDEX,    /* iSerialNumber */
    USB_DYNAMIC                 /* bNumConfigurations */
};

/* communcation interface descriptor */
ALIGN(4)
const static struct ucdc_eth_descriptor _comm_desc =
{
#ifdef RT_USB_DEVICE_COMPOSITE
    /* Interface Association Descriptor */
    USB_DESC_LENGTH_IAD,
    USB_DESC_TYPE_IAD,
    USB_DYNAMIC,
    0x02,
    USB_CDC_CLASS_COMM,
    USB_CDC_SUBCLASS_ETH,
    USB_CDC_PROTOCOL_NONE,
    0x00,
#endif
    /* Interface Descriptor */
    USB_DESC_LENGTH_INTERFACE,
    USB_DESC_TYPE_INTERFACE,
    USB_DYNAMIC,
    0x00,
    0x01,
    USB_CDC_CLASS_COMM,
    USB_CDC_SUBCLASS_ETH,
    USB_CDC_PROTOCOL_NONE,
    0x00,
    /* Header Functional Descriptor */
    sizeof(struct ucdc_header_descriptor),
    USB_CDC_CS_INTERFACE,
    USB_CDC_SCS_HEADER,
    0x0110,
    /* Union Functional Descriptor */
    sizeof(struct ucdc_union_descriptor),
    USB_CDC_CS_INTERFACE,
    USB_CDC_SCS_UNION,
    USB_DYNAMIC,
    USB_DYNAMIC,
    /* Abstract Control Management Functional Descriptor */
    sizeof(struct ucdc_enet_descriptor),
    USB_CDC_CS_INTERFACE,
    USB_CDC_SCS_ETH,
    USB_STRING_SERIAL_INDEX,
    {0,0,0,0},
    USB_ETH_MTU,
    0x00,
    0x00,
    /* Endpoint Descriptor */
    USB_DESC_LENGTH_ENDPOINT,
    USB_DESC_TYPE_ENDPOINT,
    USB_DIR_IN | USB_DYNAMIC,
    USB_EP_ATTR_INT,
    0x08,
    0xFF,
};

/* data interface descriptor */
ALIGN(4)
const static struct ucdc_data_descriptor _data_desc =
{
    /* interface descriptor */
    USB_DESC_LENGTH_INTERFACE,
    USB_DESC_TYPE_INTERFACE,
    USB_DYNAMIC,
    0x00,
    0x02,
    USB_CDC_CLASS_DATA,
    USB_CDC_SUBCLASS_ETH,
    0x00,
    0x00,
    /* endpoint, bulk out */
    USB_DESC_LENGTH_ENDPOINT,
    USB_DESC_TYPE_ENDPOINT,
    USB_DIR_OUT | USB_DYNAMIC,
    USB_EP_ATTR_BULK,
150
    USB_DYNAMIC,
151 152 153 154 155 156
    0x00,
    /* endpoint, bulk in */
    USB_DESC_LENGTH_ENDPOINT,
    USB_DESC_TYPE_ENDPOINT,
    USB_DYNAMIC | USB_DIR_IN,
    USB_EP_ATTR_BULK,
157
    USB_DYNAMIC,
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    0x00,
};

ALIGN(4)
const static char* _ustring[] =
{
    "Language",                 /* LANGID */
    "RT-Thread Team.",          /* MANU */
    "RT-Thread ECM device",     /* PRODUCT */
    "3497F694ECAB",             /* SERIAL (MAC)*/
    "Configuration",            /* CONFIG */
    "Interface",                /* INTERFACE */
};

ALIGN(4)
173
//FS and HS needed
174 175
static struct usb_qualifier_descriptor dev_qualifier =
{
176 177 178 179 180 181 182 183
    sizeof(dev_qualifier),          //bLength
    USB_DESC_TYPE_DEVICEQUALIFIER,  //bDescriptorType
    0x0200,                         //bcdUSB
    USB_CLASS_CDC,                  //bDeviceClass
    USB_CDC_SUBCLASS_ETH,           //bDeviceSubClass
    USB_CDC_PROTOCOL_NONE,          //bDeviceProtocol
    64,                             //bMaxPacketSize0
    0x01,                           //bNumConfigurations
184 185
    0,
};
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static rt_err_t _cdc_send_notifi(ufunction_t func,ucdc_notification_code_t notifi,rt_uint16_t wValue,rt_uint16_t wLength)
{
    static struct ucdc_management_element_notifications _notifi;
    cdc_eps_t eps;
    RT_ASSERT(func!=RT_NULL)
    eps = &((rt_ecm_eth_t)func->user_data)->eps;
    _notifi.bmRequestType = 0xA1;
    _notifi.bNotificatinCode = notifi;
    _notifi.wValue = wValue;
    _notifi.wLength = wLength;
    
    eps->ep_cmd->request.buffer = (void *)&_notifi;
    eps->ep_cmd->request.size = 8;
    eps->ep_cmd->request.req_type = UIO_REQUEST_WRITE;
    rt_usbd_io_request(func->device, eps->ep_cmd, &eps->ep_cmd->request);
    return RT_EOK;
}

static rt_err_t _ecm_set_eth_packet_filter(ufunction_t func, ureq_t setup)
{
    rt_ecm_eth_t _ecm_eth = (rt_ecm_eth_t)func->user_data;
    dcd_ep0_send_status(func->device->dcd);
    

    /* send link up. */
    eth_device_linkchange(&_ecm_eth->parent, RT_TRUE);
    _cdc_send_notifi(func,UCDC_NOTIFI_NETWORK_CONNECTION,1,0);
    return RT_EOK;
}
/**
 * This function will handle rndis interface request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
{
    RT_ASSERT(func != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    switch(setup->bRequest)
    {
    case CDC_SET_ETH_PACKET_FILTER:
        _ecm_set_eth_packet_filter(func, setup);
        break;
    default:
        rt_kprintf("setup->bRequest:0x%02X",setup->bRequest);
        break;
    }
    return RT_EOK;
}

/**
 * This function will handle rndis bulk in endpoint request.
 *
 * @param device the usb device object.
 * @param size request size.
 *
 * @return RT_EOK.
 */

static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
{
    rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
    rt_sem_release(&ecm_device->tx_buffer_free);
    return RT_EOK;
}

/**
 * This function will handle RNDIS bulk out endpoint request.
 *
 * @param device the usb device object.
 * @param size request size.
 *
 * @return RT_EOK.
 */
static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
{
    rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
    rt_memcpy((void *)(ecm_device->rx_buffer + ecm_device->rx_offset),ecm_device->rx_pool,size);
    ecm_device->rx_offset += size;
    if(size < EP_MAXPACKET(ecm_device->eps.ep_out))
    {
        ecm_device->rx_size = ecm_device->rx_offset;
        ecm_device->rx_offset = 0;
        eth_device_ready(&ecm_device->parent);
        
    }else
    {
        ecm_device->eps.ep_out->request.buffer = ecm_device->eps.ep_out->buffer;
        ecm_device->eps.ep_out->request.size = EP_MAXPACKET(ecm_device->eps.ep_out);
        ecm_device->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
        rt_usbd_io_request(ecm_device->func->device, ecm_device->eps.ep_out, &ecm_device->eps.ep_out->request);
    }


    return RT_EOK;
}
static rt_err_t rt_ecm_eth_init(rt_device_t dev)
{
    return RT_EOK;
}

static rt_err_t rt_ecm_eth_open(rt_device_t dev, rt_uint16_t oflag)
{
    return RT_EOK;
}

static rt_err_t rt_ecm_eth_close(rt_device_t dev)
{
    return RT_EOK;
}

static rt_size_t rt_ecm_eth_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
    rt_set_errno(-RT_ENOSYS);
    return 0;
}

static rt_size_t rt_ecm_eth_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
    rt_set_errno(-RT_ENOSYS);
    return 0;
}
static rt_err_t rt_ecm_eth_control(rt_device_t dev, int cmd, void *args)
{
    rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
    switch(cmd)
    {
    case NIOCTL_GADDR:
        /* get mac address */
        if(args) rt_memcpy(args, ecm_eth_dev->dev_addr, MAX_ADDR_LEN);
        else return -RT_ERROR;
        break;

    default :
        break;
    }

    return RT_EOK;
}
struct pbuf *rt_ecm_eth_rx(rt_device_t dev)
{
    struct pbuf* p = RT_NULL;
    rt_uint32_t offset = 0;
    rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
    if(ecm_eth_dev->rx_size != 0)
    {
        /* allocate buffer */
        p = pbuf_alloc(PBUF_RAW, ecm_eth_dev->rx_size, PBUF_RAM);
        if (p != RT_NULL)
        {
            struct pbuf* q;

            for (q = p; q != RT_NULL; q= q->next)
            {
                /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
                rt_memcpy(q->payload,
                        (rt_uint8_t *)((ecm_eth_dev->rx_buffer) + offset),
                        q->len);
                offset += q->len;
            }
        }
    }

    {
        if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
        {
            ecm_eth_dev->rx_size = 0;
            ecm_eth_dev->rx_offset = 0;
            ecm_eth_dev->eps.ep_out->request.buffer = ecm_eth_dev->eps.ep_out->buffer;
            ecm_eth_dev->eps.ep_out->request.size = EP_MAXPACKET(ecm_eth_dev->eps.ep_out);
            ecm_eth_dev->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
            rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_out, &ecm_eth_dev->eps.ep_out->request);
        }
    }

    return p;
}

rt_err_t rt_ecm_eth_tx(rt_device_t dev, struct pbuf* p)
{
    struct pbuf* q;
    char * pbuffer;
    rt_err_t result = RT_EOK;
    rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;

    if(!ecm_eth_dev->parent.link_status)
    {
        ECM_PRINTF("linkdown, drop pkg\r\n");
        return RT_EOK;
    }

    // RT_ASSERT(p->tot_len < USB_ETH_MTU);
    if(p->tot_len > USB_ETH_MTU)
    {
        ECM_PRINTF("RNDIS MTU is:%d, but the send packet size is %d\r\n",
                     USB_ETH_MTU, p->tot_len);
        p->tot_len = USB_ETH_MTU;
    }
    result = rt_sem_take(&ecm_eth_dev->tx_buffer_free, RT_WAITING_FOREVER);
    if(result != RT_EOK)
    {
        return result;
    }
    pbuffer = (char *)&ecm_eth_dev->tx_buffer;
    for (q = p; q != NULL; q = q->next)
    {
        rt_memcpy(pbuffer, q->payload, q->len);
        pbuffer += q->len;
    }

    {
        if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
        {
            ecm_eth_dev->eps.ep_in->request.buffer = (void *)&ecm_eth_dev->tx_buffer;
            ecm_eth_dev->eps.ep_in->request.size = p->tot_len;
            ecm_eth_dev->eps.ep_in->request.req_type = UIO_REQUEST_WRITE;
            rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_in, &ecm_eth_dev->eps.ep_in->request);
        }
    }

    return result;
}
/**
 * This function will handle RNDIS interrupt in endpoint request.
 *
 * @param device the usb device object.
 * @param size request size.
 *
 * @return RT_EOK.
 */
static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
{
    return RT_EOK;
}

/**
 * This function will run cdc class, it will be called on handle set configuration request.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _function_enable(ufunction_t func)
{
    cdc_eps_t eps;
    rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
    eps = (cdc_eps_t)&ecm_device->eps;
    eps->ep_out->buffer = ecm_device->rx_pool;
    ecm_device->rx_size = 0;
    ecm_device->rx_offset = 0;
    
    eps->ep_out->request.buffer = (void *)eps->ep_out->buffer;
    eps->ep_out->request.size = EP_MAXPACKET(eps->ep_out);
    eps->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
    rt_usbd_io_request(func->device, eps->ep_out, &eps->ep_out->request);
    return RT_EOK;
}

/**
 * This function will stop cdc class, it will be called on handle set configuration request.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _function_disable(ufunction_t func)
{
    eth_device_linkchange(&((rt_ecm_eth_t)func->user_data)->parent, RT_FALSE);
    return RT_EOK;
}


static struct ufunction_ops ops =
{
    _function_enable,
    _function_disable,
    RT_NULL,
};

/**
 * This function will configure cdc descriptor.
 *
 * @param comm the communication interface number.
 * @param data the data interface number.
 *
 * @return RT_EOK on successful.
 */
478
static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm, rt_uint8_t cintf_nr, ucdc_data_desc_t data, rt_uint8_t dintf_nr, rt_uint8_t device_is_hs)
479 480 481 482 483 484 485
{
    comm->call_mgmt_desc.data_interface = dintf_nr;
    comm->union_desc.master_interface = cintf_nr;
    comm->union_desc.slave_interface0 = dintf_nr;
#ifdef RT_USB_DEVICE_COMPOSITE
    comm->iad_desc.bFirstInterface = cintf_nr;
#endif
486 487
    data->ep_out_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
    data->ep_in_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    return RT_EOK;
}



/**
 * This function will create a cdc ecm class instance.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
ufunction_t rt_usbd_function_ecm_create(udevice_t device)
{
    ufunction_t cdc;
    rt_ecm_eth_t _ecm_eth;
    cdc_eps_t eps;
    uintf_t intf_comm, intf_data;
    ualtsetting_t comm_setting, data_setting;
    ucdc_data_desc_t data_desc;
    ucdc_eth_desc_t comm_desc;
    
    /* parameter check */
    RT_ASSERT(device != RT_NULL);

    /* set usb device string description */
    rt_usbd_device_set_string(device, _ustring);

    /* create a cdc class */
    cdc = rt_usbd_function_new(device, &_dev_desc, &ops);
    rt_usbd_device_set_qualifier(device, &dev_qualifier);
    _ecm_eth= rt_malloc(sizeof(struct rt_ecm_eth)); 
    rt_memset(_ecm_eth, 0, sizeof(struct rt_ecm_eth));
    cdc->user_data = _ecm_eth;

    _ecm_eth->func = cdc;
    /* create a cdc class endpoints collection */
    eps = &_ecm_eth->eps;
    /* create a cdc communication interface and a cdc data interface */
    intf_comm = rt_usbd_interface_new(device, _interface_handler);
    intf_data = rt_usbd_interface_new(device, _interface_handler);

    /* create a communication alternate setting and a data alternate setting */
    comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_eth_descriptor));
    data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));

    /* config desc in alternate setting */
    rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
                                         (rt_off_t)&((ucdc_eth_desc_t)0)->intf_desc);
    rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
    /* configure the cdc interface descriptor */
539
    _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num, device->dcd->device_is_hs);
540 541 542 543 544 545 546 547 548 549 550 551 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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

    /* create a command endpoint */
    comm_desc = (ucdc_eth_desc_t)comm_setting->desc;
    eps->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
    /* add the command endpoint to the cdc communication interface */
    rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);

    /* add the communication alternate setting to the communication interface,
       then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
    rt_usbd_set_altsetting(intf_comm, 0);
    /* add the communication interface to the cdc class */
    rt_usbd_function_add_interface(cdc, intf_comm);
    
    /* create a bulk in and a bulk out endpoint */
    data_desc = (ucdc_data_desc_t)data_setting->desc;
    eps->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
    eps->ep_in = rt_usbd_endpoint_new(&data_desc->ep_in_desc, _ep_in_handler);

    /* add the bulk out and bulk in endpoints to the data alternate setting */
    rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
    rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);

    /* add the data alternate setting to the data interface
            then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_data, data_setting);
    rt_usbd_set_altsetting(intf_data, 0);

    /* add the cdc data interface to cdc class */
    rt_usbd_function_add_interface(cdc, intf_data);

    rt_sem_init(&_ecm_eth->tx_buffer_free, "ue_tx", 1, RT_IPC_FLAG_FIFO);
    /* OUI 00-00-00, only for test. */
    _ecm_eth->dev_addr[0] = 0x34;
    _ecm_eth->dev_addr[1] = 0x97;
    _ecm_eth->dev_addr[2] = 0xF6;
    /* generate random MAC. */
    _ecm_eth->dev_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
    _ecm_eth->dev_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
    _ecm_eth->dev_addr[5] = 0xAC;//(const rt_uint8_t *)(0x1fff7a18);
    /* OUI 00-00-00, only for test. */
    _ecm_eth->host_addr[0] = 0x34;
    _ecm_eth->host_addr[1] = 0x97;
    _ecm_eth->host_addr[2] = 0xF6;
    /* generate random MAC. */
    _ecm_eth->host_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
    _ecm_eth->host_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
    _ecm_eth->host_addr[5] = 0xAB;//*(const rt_uint8_t *)(0x1fff7a18);

    _ecm_eth->parent.parent.init       = rt_ecm_eth_init;
    _ecm_eth->parent.parent.open       = rt_ecm_eth_open;
    _ecm_eth->parent.parent.close      = rt_ecm_eth_close;
    _ecm_eth->parent.parent.read       = rt_ecm_eth_read;
    _ecm_eth->parent.parent.write      = rt_ecm_eth_write;
    _ecm_eth->parent.parent.control    = rt_ecm_eth_control;
    _ecm_eth->parent.parent.user_data  = device;

    _ecm_eth->parent.eth_rx     = rt_ecm_eth_rx;
    _ecm_eth->parent.eth_tx     = rt_ecm_eth_tx;
    /* register eth device */
    eth_device_init(&_ecm_eth->parent, "u0");
    
    /* send link up. */
    eth_device_linkchange(&_ecm_eth->parent, RT_FALSE);
   
    return cdc;
}