usbdevice.c 5.8 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5
/*
 * File      : usbdevice.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.
19
 * 
M
Ming, Bai 已提交
20 21
 * Change Logs:
 * Date           Author       Notes
H
heyuanjie87 已提交
22
 * 2012-10-02     Yi Qiu       first version
M
Ming, Bai 已提交
23 24 25 26 27 28
 */

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

H
heyuanjie87 已提交
29 30
#ifdef RT_USING_USB_DEVICE

31 32
#define USB_DEVICE_CONTROLLER_NAME      "usbd"

33 34
#ifdef RT_USB_DEVICE_COMPOSITE
const static char* ustring[] =
M
Ming, Bai 已提交
35 36 37
{
    "Language",
    "RT-Thread Team.",
38
    "RTT Composite Device",
39
    "320219198301",
M
Ming, Bai 已提交
40 41
    "Configuration",
    "Interface",
42
    USB_STRING_OS
M
Ming, Bai 已提交
43 44 45 46 47 48 49 50 51 52 53
};

static struct udevice_descriptor compsit_desc =
{
    USB_DESC_LENGTH_DEVICE,     //bLength;
    USB_DESC_TYPE_DEVICE,       //type;
    USB_BCD_VERSION,            //bcdUSB;
    USB_CLASS_MISC,             //bDeviceClass;
    0x02,                       //bDeviceSubClass;
    0x01,                       //bDeviceProtocol;
    0x40,                       //bMaxPacketSize0;
H
heyuanjie87 已提交
54 55
    _VENDOR_ID,                 //idVendor;
    _PRODUCT_ID,                //idProduct;
M
Ming, Bai 已提交
56 57 58 59
    USB_BCD_DEVICE,             //bcdDevice;
    USB_STRING_MANU_INDEX,      //iManufacturer;
    USB_STRING_PRODUCT_INDEX,   //iProduct;
    USB_STRING_SERIAL_INDEX,    //iSerialNumber;
60
    USB_DYNAMIC,                //bNumConfigurations;
M
Ming, Bai 已提交
61
};
62 63 64 65 66 67 68 69 70 71 72 73 74 75

//FS and HS needed
static struct usb_qualifier_descriptor dev_qualifier =
{
    sizeof(dev_qualifier),          //bLength
    USB_DESC_TYPE_DEVICEQUALIFIER,  //bDescriptorType
    0x0200,                         //bcdUSB
    USB_CLASS_MISC,                 //bDeviceClass
    0x02,                           //bDeviceSubClass
    0x01,                           //bDeviceProtocol
    64,                             //bMaxPacketSize0
    0x01,                           //bNumConfigurations
    0,
};
M
Ming, Bai 已提交
76 77
#endif

78 79 80 81 82 83 84 85 86 87
struct usb_os_comp_id_descriptor usb_comp_id_desc = 
{
    //head section
    USB_DYNAMIC,
    0x0100,
    0x04,
    USB_DYNAMIC,
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00},
};

88
rt_err_t rt_usb_device_init(void)
M
Ming, Bai 已提交
89 90 91 92
{
    rt_device_t udc;
    udevice_t udevice;
    uconfig_t cfg;
93 94 95 96
    ufunction_t func;

    /* create and startup usb device thread */
    rt_usbd_core_init();
M
Ming, Bai 已提交
97

98 99
    /* create a device object */
    udevice = rt_usbd_device_new();
M
Ming, Bai 已提交
100

101
    udc = rt_device_find(USB_DEVICE_CONTROLLER_NAME);
M
Ming, Bai 已提交
102 103
    if(udc == RT_NULL)
    {
104
        rt_kprintf("can't find usb device controller %s\n", USB_DEVICE_CONTROLLER_NAME);
M
Ming, Bai 已提交
105 106 107 108 109 110 111
        return -RT_ERROR;
    }

    /* set usb controller driver to the device */
    rt_usbd_device_set_controller(udevice, (udcd_t)udc);

    /* create a configuration object */
112
    cfg = rt_usbd_config_new();
M
Ming, Bai 已提交
113

114 115
    rt_usbd_device_set_os_comp_id_desc(udevice,&usb_comp_id_desc);

M
Ming, Bai 已提交
116
#ifdef RT_USB_DEVICE_MSTORAGE
还_没_想_好's avatar
还_没_想_好 已提交
117 118 119 120
    {
        extern ufunction_t rt_usbd_function_mstorage_create(udevice_t device);
        /* create a mass storage function object */
        func = rt_usbd_function_mstorage_create(udevice);
M
Ming, Bai 已提交
121

还_没_想_好's avatar
还_没_想_好 已提交
122 123 124
        /* add the function to the configuration */
        rt_usbd_config_add_function(cfg, func);
    }
M
Ming, Bai 已提交
125
#endif
126

M
Ming, Bai 已提交
127
#ifdef RT_USB_DEVICE_CDC
还_没_想_好's avatar
还_没_想_好 已提交
128 129 130 131 132 133 134 135 136
    {
        extern ufunction_t rt_usbd_function_cdc_create(udevice_t device);
        /* create a cdc function object */
        func = rt_usbd_function_cdc_create(udevice);

        /* add the function to the configuration */
        rt_usbd_config_add_function(cfg, func);
    }
#endif
M
Ming, Bai 已提交
137

还_没_想_好's avatar
还_没_想_好 已提交
138 139 140 141 142 143 144 145 146
#ifdef RT_USB_DEVICE_HID
    {
        extern ufunction_t rt_usbd_function_hid_create(udevice_t device);
        /* create a cdc function object */
        func = rt_usbd_function_hid_create(udevice);

        /* add the function to the configuration */
        rt_usbd_config_add_function(cfg, func);
    }
M
Ming, Bai 已提交
147
#endif
148

M
Ming, Bai 已提交
149
#ifdef RT_USB_DEVICE_RNDIS
还_没_想_好's avatar
还_没_想_好 已提交
150 151 152 153
    {
        extern ufunction_t rt_usbd_function_rndis_create(udevice_t device);
        /* create a rndis function object */
        func = rt_usbd_function_rndis_create(udevice);
M
Ming, Bai 已提交
154

还_没_想_好's avatar
还_没_想_好 已提交
155 156 157
        /* add the function to the configuration */
        rt_usbd_config_add_function(cfg, func);
    }
M
Ming, Bai 已提交
158 159
#endif

160 161 162 163 164 165 166 167 168 169
#ifdef RT_USB_DEVICE_ECM
    {
        extern ufunction_t rt_usbd_function_ecm_create(udevice_t device);
        /* create a rndis function object */
        func = rt_usbd_function_ecm_create(udevice);

        /* add the function to the configuration */
        rt_usbd_config_add_function(cfg, func);
    }
#endif
170 171 172 173 174 175 176 177 178 179 180
    
#ifdef RT_USB_DEVICE_WINUSB
    {
        extern ufunction_t rt_usbd_function_winusb_create(udevice_t device);
        /* create a rndis function object */
        func = rt_usbd_function_winusb_create(udevice);

        /* add the function to the configuration */
        rt_usbd_config_add_function(cfg, func);
    }
#endif
181

M
Ming, Bai 已提交
182 183 184
    /* set device descriptor to the device */
#ifdef RT_USB_DEVICE_COMPOSITE
    rt_usbd_device_set_descriptor(udevice, &compsit_desc);
185
    rt_usbd_device_set_string(udevice, ustring);
lymzzyh's avatar
lymzzyh 已提交
186
    rt_usbd_device_set_qualifier(udevice, &dev_qualifier);
M
Ming, Bai 已提交
187
#else
188
    rt_usbd_device_set_descriptor(udevice, func->dev_desc);
M
Ming, Bai 已提交
189 190 191 192
#endif

    /* add the configuration to the device */
    rt_usbd_device_add_config(udevice, cfg);
193

M
Ming, Bai 已提交
194 195 196
    /* initialize usb device controller */
    rt_device_init(udc);

197 198 199
    /* set default configuration to 1 */
    rt_usbd_set_config(udevice, 1);

M
Ming, Bai 已提交
200 201
    return RT_EOK;
}
H
heyuanjie87 已提交
202
#endif