usb_device.h 14.3 KB
Newer Older
M
Ming, Bai 已提交
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
M
Ming, Bai 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
M
Ming, Bai 已提交
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-10-01     Yi Qiu       first version
9 10
 * 2012-12-12     heyuanjie87  change endpoint and function handler
 * 2013-04-26     aozima       add DEVICEQUALIFIER support.
11
 * 2017-11-15     ZYH          fix ep0 transform error
M
Ming, Bai 已提交
12 13 14 15 16 17
 */

#ifndef  __USB_DEVICE_H__
#define  __USB_DEVICE_H__

#include <rtthread.h>
还_没_想_好's avatar
还_没_想_好 已提交
18
#include "drivers/usb_common.h"
M
Ming, Bai 已提交
19

20 21
/* Vendor ID */
#ifdef USB_VENDOR_ID
还_没_想_好's avatar
还_没_想_好 已提交
22
#define _VENDOR_ID                  USB_VENDOR_ID
23
#else
还_没_想_好's avatar
还_没_想_好 已提交
24
#define _VENDOR_ID                  0x0EFF
25 26 27
#endif
/* Product ID */
#ifdef USB_PRODUCT_ID
28
#define _PRODUCT_ID                 USB_PRODUCT_ID
H
heyuanjie87 已提交
29
#else
30
#define _PRODUCT_ID                 0x0001
31
#endif
M
Ming, Bai 已提交
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#define USB_BCD_DEVICE              0x0200   /* USB Specification Release Number in Binary-Coded Decimal */
#define USB_BCD_VERSION             0x0200   /* USB 2.0 */
#define EP0_IN_ADDR                 0x80
#define EP0_OUT_ADDR                0x00
#define EP_HANDLER(ep, func, size)  RT_ASSERT(ep != RT_NULL); ep->handler(func, size)
#define EP_ADDRESS(ep)              ep->ep_desc->bEndpointAddress
#define EP_MAXPACKET(ep)            ep->ep_desc->wMaxPacketSize
#define FUNC_ENABLE(func)           do{                                             \
                                        if(func->ops->enable != RT_NULL &&          \
                                            func->enabled == RT_FALSE)              \
                                        {                                           \
                                            if(func->ops->enable(func) == RT_EOK)   \
                                                func->enabled = RT_TRUE;            \
                                        }                                           \
                                    }while(0)
#define FUNC_DISABLE(func)          do{                                             \
                                        if(func->ops->disable != RT_NULL &&         \
                                            func->enabled == RT_TRUE)               \
                                        {                                           \
                                                func->enabled = RT_FALSE;           \
                                                func->ops->disable(func);           \
                                        }                                           \
                                    }while(0)

struct ufunction;
M
Ming, Bai 已提交
58 59 60
struct udevice;
struct uendpoint;

61 62 63 64 65
typedef enum 
{
    /* request to read full count */
    UIO_REQUEST_READ_FULL,
    /* request to read any count */
还_没_想_好's avatar
还_没_想_好 已提交
66
    UIO_REQUEST_READ_BEST,  
67 68 69 70
    /* request to write full count */
    UIO_REQUEST_WRITE,
}UIO_REQUEST_TYPE;

M
Ming, Bai 已提交
71 72
struct udcd_ops
{
73 74 75 76 77 78 79 80 81 82 83 84
    rt_err_t (*set_address)(rt_uint8_t address);
    rt_err_t (*set_config)(rt_uint8_t address);
    rt_err_t (*ep_set_stall)(rt_uint8_t address);
    rt_err_t (*ep_clear_stall)(rt_uint8_t address);
    rt_err_t (*ep_enable)(struct uendpoint* ep);
    rt_err_t (*ep_disable)(struct uendpoint* ep);
    rt_size_t (*ep_read_prepare)(rt_uint8_t address, void *buffer, rt_size_t size);
    rt_size_t (*ep_read)(rt_uint8_t address, void *buffer);
    rt_size_t (*ep_write)(rt_uint8_t address, void *buffer, rt_size_t size);
    rt_err_t (*ep0_send_status)(void);
    rt_err_t (*suspend)(void);
    rt_err_t (*wakeup)(void);    
M
Ming, Bai 已提交
85 86
};

87
struct ep_id
M
Ming, Bai 已提交
88
{
还_没_想_好's avatar
还_没_想_好 已提交
89 90 91 92 93
    rt_uint8_t  addr;
    rt_uint8_t  type;
    rt_uint8_t  dir;
    rt_uint16_t maxpacket;
    rt_uint8_t  status;
M
Ming, Bai 已提交
94 95
};

96
typedef rt_err_t (*udep_handler_t)(struct ufunction* func, rt_size_t size);
M
Ming, Bai 已提交
97

98
struct uio_request
M
Ming, Bai 已提交
99 100
{
    rt_list_t list;
101
    UIO_REQUEST_TYPE req_type;
M
Ming, Bai 已提交
102
    rt_uint8_t* buffer;
103 104 105 106 107 108 109 110
    rt_size_t size;
    rt_size_t remain_size;
};
typedef struct uio_request* uio_request_t;

struct uendpoint
{
    rt_list_t list;
M
Ming, Bai 已提交
111
    uep_desc_t ep_desc;
112 113 114 115 116
    rt_list_t request_list;
    struct uio_request request;
    rt_uint8_t* buffer;
    rt_bool_t stalled;
    struct ep_id* id;
M
Ming, Bai 已提交
117
    udep_handler_t handler;
118
    rt_err_t (*rx_indicate)(struct udevice* dev, rt_size_t size);
M
Ming, Bai 已提交
119 120
};
typedef struct uendpoint* uep_t;
H
heyuanjie87 已提交
121

122 123 124 125 126
struct udcd
{
    struct rt_device parent;
    const struct udcd_ops* ops;
    struct uendpoint ep0;
127
    uep0_stage_t stage;
128
    struct ep_id* ep_pool;
129
    rt_uint8_t device_is_hs;
130 131 132
};
typedef struct udcd* udcd_t;

M
Ming, Bai 已提交
133 134 135 136
struct ualtsetting
{
    rt_list_t list;
    uintf_desc_t intf_desc;
H
heyuanjie87 已提交
137
    void* desc;
M
Ming, Bai 已提交
138 139 140 141 142
    rt_size_t desc_size;
    rt_list_t ep_list;
};
typedef struct ualtsetting* ualtsetting_t;

143
typedef rt_err_t (*uintf_handler_t)(struct ufunction* func, ureq_t setup);
M
Ming, Bai 已提交
144 145 146 147 148 149 150 151 152 153 154

struct uinterface
{
    rt_list_t list;
    rt_uint8_t intf_num;
    ualtsetting_t curr_setting;
    rt_list_t setting_list;
    uintf_handler_t handler;
};
typedef struct uinterface* uintf_t;

155
struct ufunction_ops
M
Ming, Bai 已提交
156
{
157 158 159
    rt_err_t (*enable)(struct ufunction* func);
    rt_err_t (*disable)(struct ufunction* func);
    rt_err_t (*sof_handler)(struct ufunction* func);
M
Ming, Bai 已提交
160
};
161
typedef struct ufunction_ops* ufunction_ops_t;
M
Ming, Bai 已提交
162

163
struct ufunction
M
Ming, Bai 已提交
164 165
{
    rt_list_t list;
166
    ufunction_ops_t ops;
M
Ming, Bai 已提交
167 168 169
    struct udevice* device;
    udev_desc_t dev_desc;
    void* user_data;
170
    rt_bool_t enabled;
H
heyuanjie87 已提交
171

M
Ming, Bai 已提交
172 173
    rt_list_t intf_list;
};
174
typedef struct ufunction* ufunction_t;
M
Ming, Bai 已提交
175 176 177 178 179

struct uconfig
{
    rt_list_t list;
    struct uconfig_descriptor cfg_desc;
180
    rt_list_t func_list;
M
Ming, Bai 已提交
181 182 183 184 185 186 187
};
typedef struct uconfig* uconfig_t;

struct udevice
{
    rt_list_t list;
    struct udevice_descriptor dev_desc;
188 189

    struct usb_qualifier_descriptor * dev_qualifier;
190
    usb_os_comp_id_desc_t    os_comp_id_desc;
M
Ming, Bai 已提交
191 192
    const char** str;

H
heyuanjie87 已提交
193
    udevice_state_t state;
M
Ming, Bai 已提交
194 195 196 197 198 199 200 201
    rt_list_t cfg_list;
    uconfig_t curr_cfg;
    rt_uint8_t nr_intf;

    udcd_t dcd;
};
typedef struct udevice* udevice_t;

202 203 204 205 206 207 208
struct udclass
{
    rt_list_t list;
    ufunction_t (*rt_usbd_function_create)(udevice_t device);
};
typedef struct udclass* udclass_t;

M
Ming, Bai 已提交
209 210 211 212
enum udev_msg_type
{
    USB_MSG_SETUP_NOTIFY,
    USB_MSG_DATA_NOTIFY,
213 214
    USB_MSG_EP0_OUT,
    USB_MSG_EP_CLEAR_FEATURE,        
M
Ming, Bai 已提交
215
    USB_MSG_SOF,
216
    USB_MSG_RESET,
217
    USB_MSG_PLUG_IN,    
G
Grissiom 已提交
218 219 220 221 222
    /* we don't need to add a "PLUG_IN" event because after the cable is
     * plugged in(before any SETUP) the classed have nothing to do. If the host
     * is ready, it will send RESET and we will have USB_MSG_RESET. So, a RESET
     * should reset and run the class while plug_in is not. */
    USB_MSG_PLUG_OUT,
M
Ming, Bai 已提交
223 224 225
};
typedef enum udev_msg_type udev_msg_type;

226 227 228 229 230 231
struct ep_msg
{
    rt_size_t size;
    rt_uint8_t ep_addr;
};

M
Ming, Bai 已提交
232 233 234 235 236 237
struct udev_msg
{
    udev_msg_type type;
    udcd_t dcd;
    union
    {
还_没_想_好's avatar
还_没_想_好 已提交
238
        struct ep_msg   ep_msg;
qiuyiuestc's avatar
qiuyiuestc 已提交
239
        struct urequest setup;
M
Ming, Bai 已提交
240 241 242 243
    } content;
};
typedef struct udev_msg* udev_msg_t;

244
int rt_usbd_class_list_init(void);
245 246 247 248 249 250 251
udevice_t rt_usbd_device_new(void);
uconfig_t rt_usbd_config_new(void);
ufunction_t rt_usbd_function_new(udevice_t device, udev_desc_t dev_desc,
                              ufunction_ops_t ops);
uintf_t rt_usbd_interface_new(udevice_t device, uintf_handler_t handler);
uep_t rt_usbd_endpoint_new(uep_desc_t ep_desc, udep_handler_t handler);
ualtsetting_t rt_usbd_altsetting_new(rt_size_t desc_size);
M
Ming, Bai 已提交
252 253

rt_err_t rt_usbd_core_init(void);
254 255
rt_err_t rt_usb_device_init(void);
rt_err_t rt_usbd_event_signal(struct udev_msg* msg);
M
Ming, Bai 已提交
256 257
rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd);
rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc);
258
rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring);
259
rt_err_t rt_usbd_device_set_qualifier(udevice_t device, struct usb_qualifier_descriptor* qualifier);
260
rt_err_t rt_usbd_device_set_os_comp_id_desc(udevice_t device, usb_os_comp_id_desc_t os_comp_id_desc);
M
Ming, Bai 已提交
261
rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg);
262
rt_err_t rt_usbd_config_add_function(uconfig_t cfg, ufunction_t func);
263
rt_err_t rt_usbd_class_register(udclass_t udclass);
264
rt_err_t rt_usbd_function_add_interface(ufunction_t func, uintf_t intf);
M
Ming, Bai 已提交
265 266
rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting);
rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep);
267
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);
268
rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos);
M
Ming, Bai 已提交
269 270 271 272 273
rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value);
rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value);

udevice_t rt_usbd_find_device(udcd_t dcd);
uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value);
274 275 276 277 278 279 280
uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *pfunc);
uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_addr);
rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req);
rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size);
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));

281 282 283 284 285 286
int rt_usbd_vcom_class_register(void);
int rt_usbd_ecm_class_register(void);
int rt_usbd_hid_class_register(void);
int rt_usbd_msc_class_register(void);
int rt_usbd_rndis_class_register(void);
int rt_usbd_winusb_class_register(void);
M
Ming, Bai 已提交
287 288

#ifdef RT_USB_DEVICE_COMPOSITE
289
rt_err_t rt_usbd_function_set_iad(ufunction_t func, uiad_desc_t iad_desc);
M
Ming, Bai 已提交
290 291
#endif

292 293 294 295 296 297
rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index);
rt_err_t rt_usbd_clear_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index);
rt_err_t rt_usbd_ep_set_stall(udevice_t device, uep_t ep);
rt_err_t rt_usbd_ep_clear_stall(udevice_t device, uep_t ep);
rt_err_t rt_usbd_ep0_set_stall(udevice_t device);
rt_err_t rt_usbd_ep0_clear_stall(udevice_t device);
qiuyiuestc's avatar
qiuyiuestc 已提交
298
rt_err_t rt_usbd_ep0_setup_handler(udcd_t dcd, struct urequest* setup);
299 300
rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd);
rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size);
还_没_想_好's avatar
还_没_想_好 已提交
301
rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size);
302 303 304 305 306 307 308
rt_err_t rt_usbd_ep_out_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size);
rt_err_t rt_usbd_reset_handler(udcd_t dcd);
rt_err_t rt_usbd_connect_handler(udcd_t dcd);
rt_err_t rt_usbd_disconnect_handler(udcd_t dcd);
rt_err_t rt_usbd_sof_handler(udcd_t dcd);

rt_inline rt_err_t dcd_set_address(udcd_t dcd, rt_uint8_t address)
M
Ming, Bai 已提交
309 310
{
    RT_ASSERT(dcd != RT_NULL);
311 312
    RT_ASSERT(dcd->ops != RT_NULL);
    RT_ASSERT(dcd->ops->set_address != RT_NULL);
M
Ming, Bai 已提交
313

314
    return dcd->ops->set_address(address);
M
Ming, Bai 已提交
315 316
}

317
rt_inline rt_err_t dcd_set_config(udcd_t dcd, rt_uint8_t address)
M
Ming, Bai 已提交
318 319
{
    RT_ASSERT(dcd != RT_NULL);
320 321
    RT_ASSERT(dcd->ops != RT_NULL);
    RT_ASSERT(dcd->ops->set_config != RT_NULL);
M
Ming, Bai 已提交
322

323
    return dcd->ops->set_config(address);
M
Ming, Bai 已提交
324 325
}

326
rt_inline rt_err_t dcd_ep_enable(udcd_t dcd, uep_t ep)
M
Ming, Bai 已提交
327 328
{
    RT_ASSERT(dcd != RT_NULL);
329 330
    RT_ASSERT(dcd->ops != RT_NULL);
    RT_ASSERT(dcd->ops->ep_enable != RT_NULL);
M
Ming, Bai 已提交
331

332
    return dcd->ops->ep_enable(ep);
M
Ming, Bai 已提交
333 334
}

335
rt_inline rt_err_t dcd_ep_disable(udcd_t dcd, uep_t ep)
M
Ming, Bai 已提交
336 337
{
    RT_ASSERT(dcd != RT_NULL);
338 339
    RT_ASSERT(dcd->ops != RT_NULL);
    RT_ASSERT(dcd->ops->ep_disable != RT_NULL);
M
Ming, Bai 已提交
340

341
    return dcd->ops->ep_disable(ep);
M
Ming, Bai 已提交
342 343
}

344 345
rt_inline rt_size_t dcd_ep_read_prepare(udcd_t dcd, rt_uint8_t address, void *buffer,
                               rt_size_t size)
M
Ming, Bai 已提交
346 347
{
    RT_ASSERT(dcd != RT_NULL);
348
    RT_ASSERT(dcd->ops != RT_NULL);
M
Ming, Bai 已提交
349

350 351 352 353 354 355 356 357
    if(dcd->ops->ep_read_prepare != RT_NULL)
    {
        return dcd->ops->ep_read_prepare(address, buffer, size);
    }
    else
    {
        return 0;
    }
M
Ming, Bai 已提交
358 359
}

360
rt_inline rt_size_t dcd_ep_read(udcd_t dcd, rt_uint8_t address, void *buffer)
M
Ming, Bai 已提交
361 362
{
    RT_ASSERT(dcd != RT_NULL);
363
    RT_ASSERT(dcd->ops != RT_NULL);
M
Ming, Bai 已提交
364

365 366 367 368 369 370 371 372
    if(dcd->ops->ep_read != RT_NULL)
    {
        return dcd->ops->ep_read(address, buffer);
    }
    else
    {
        return 0;
    }
M
Ming, Bai 已提交
373 374
}

375 376
rt_inline rt_size_t dcd_ep_write(udcd_t dcd, rt_uint8_t address, void *buffer,
                                 rt_size_t size)
M
Ming, Bai 已提交
377 378
{
    RT_ASSERT(dcd != RT_NULL);
379 380
    RT_ASSERT(dcd->ops != RT_NULL);
    RT_ASSERT(dcd->ops->ep_write != RT_NULL);
M
Ming, Bai 已提交
381

382
    return dcd->ops->ep_write(address, buffer, size);
M
Ming, Bai 已提交
383 384
}

385
rt_inline rt_err_t dcd_ep0_send_status(udcd_t dcd)
M
Ming, Bai 已提交
386 387
{
    RT_ASSERT(dcd != RT_NULL);
388 389
    RT_ASSERT(dcd->ops != RT_NULL);
    RT_ASSERT(dcd->ops->ep0_send_status != RT_NULL);
M
Ming, Bai 已提交
390

391
    return dcd->ops->ep0_send_status();
M
Ming, Bai 已提交
392 393
}

394 395
rt_inline rt_err_t dcd_ep_set_stall(udcd_t dcd, rt_uint8_t address)
{    
M
Ming, Bai 已提交
396
    RT_ASSERT(dcd != RT_NULL);
397 398
    RT_ASSERT(dcd->ops != RT_NULL);
    RT_ASSERT(dcd->ops->ep_set_stall != RT_NULL);
M
Ming, Bai 已提交
399

400
    return dcd->ops->ep_set_stall(address);
M
Ming, Bai 已提交
401 402
}

403
rt_inline rt_err_t dcd_ep_clear_stall(udcd_t dcd, rt_uint8_t address)
404 405
{
    RT_ASSERT(dcd != RT_NULL);
406 407
    RT_ASSERT(dcd->ops != RT_NULL);
    RT_ASSERT(dcd->ops->ep_clear_stall != RT_NULL);
408

409
    return dcd->ops->ep_clear_stall(address);
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
rt_inline void usbd_os_proerty_descriptor_send(ufunction_t func, ureq_t setup, usb_os_proerty_t usb_os_proerty, rt_uint8_t number_of_proerty)
{
    struct usb_os_property_header header;
    static rt_uint8_t * data;
    rt_uint8_t * pdata;
    rt_uint8_t index,i;
    if(data == RT_NULL)
    {
        header.dwLength = sizeof(struct usb_os_property_header);
        header.bcdVersion = 0x0100;
        header.wIndex = 0x05;
        header.wCount = number_of_proerty;
        for(index = 0;index < number_of_proerty;index++)
        {
            header.dwLength += usb_os_proerty[index].dwSize;
        }
        data = (rt_uint8_t *)rt_malloc(header.dwLength);
        RT_ASSERT(data != RT_NULL);
        pdata = data;
        rt_memcpy((void *)pdata,(void *)&header,sizeof(struct usb_os_property_header));
        pdata += sizeof(struct usb_os_property_header);
        for(index = 0;index < number_of_proerty;index++)
        {
            rt_memcpy((void *)pdata,(void *)&usb_os_proerty[index],10);
            pdata += 10;
436
            for(i = 0;i < usb_os_proerty[index].wPropertyNameLength/2;i++)
437 438 439 440 441 442 443 444
            {
                *pdata = usb_os_proerty[index].bPropertyName[i];
                pdata++;
                *pdata = 0;
                pdata++;
            }
            *((rt_uint32_t *)pdata) = usb_os_proerty[index].dwPropertyDataLength;
            pdata += 4;
445
            for(i = 0;i < usb_os_proerty[index].dwPropertyDataLength/2;i++)
446 447 448 449 450 451 452 453 454 455
            {
                *pdata = usb_os_proerty[index].bPropertyData[i];
                pdata++;
                *pdata = 0;
                pdata++;
            }
        }
    }
    rt_usbd_ep0_write(func->device, data, setup->wLength);
}
M
Ming, Bai 已提交
456
#endif