diff options
| author | Bernard Xiong <[email protected]> | 2017-11-13 14:25:51 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-11-13 14:25:51 +0800 |
| commit | d3e72e74980c4aaf95dcbddd71a1be5ecf32b441 (patch) | |
| tree | 4cd4e58754f4acab3c76f31d98ea6711abbe0be1 /components/drivers/usb/usbdevice | |
| parent | f0bc0f8fd409736f40d85a718bf759cd64ef727c (diff) | |
| parent | cc54e0a74ef1d6a4b13802915f80be213cf74a7c (diff) | |
Merge pull request #981 from enkiller/modify_usb_stack_
[usb]update usb stack
Diffstat (limited to 'components/drivers/usb/usbdevice')
| -rw-r--r-- | components/drivers/usb/usbdevice/SConscript | 5 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/class/cdc_vcom.c | 146 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/class/hid.c | 552 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/class/hid.h | 55 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/class/mstorage.c | 4 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/core/core.c | 242 | ||||
| -rw-r--r-- | components/drivers/usb/usbdevice/core/usbdevice.c | 45 |
7 files changed, 868 insertions, 181 deletions
diff --git a/components/drivers/usb/usbdevice/SConscript b/components/drivers/usb/usbdevice/SConscript index 1ee9467410..760000a887 100644 --- a/components/drivers/usb/usbdevice/SConscript +++ b/components/drivers/usb/usbdevice/SConscript @@ -9,12 +9,15 @@ core/usbdevice.c if GetDepend('RT_USB_DEVICE_CDC'): src += Glob('class/cdc_vcom.c') + +if GetDepend('RT_USB_DEVICE_HID'): + src += Glob('class/hid.c') if GetDepend('RT_USB_DEVICE_MSTORAGE'): src += Glob('class/mstorage.c') CPPPATH = [cwd] -group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_USB_DEVICE'], CPPPATH = CPPPATH) +group = DefineGroup('rt_usbd', src, depend = ['RT_USING_USB_DEVICE'], CPPPATH = CPPPATH) Return('group') diff --git a/components/drivers/usb/usbdevice/class/cdc_vcom.c b/components/drivers/usb/usbdevice/class/cdc_vcom.c index 8916f3a084..62f3bdcb28 100644 --- a/components/drivers/usb/usbdevice/class/cdc_vcom.c +++ b/components/drivers/usb/usbdevice/class/cdc_vcom.c @@ -22,24 +22,31 @@ * 2012-10-02 Yi Qiu first version * 2012-12-12 heyuanjie87 change endpoints and function handler * 2013-06-25 heyuanjie87 remove SOF mechinism - * 2013-07-20 Yi Qiu do more test + * 2013-07-20 Yi Qiu do more test + * 2016-02-01 Urey Fix some error */ +#include <rthw.h> #include <rtthread.h> #include <rtservice.h> #include <rtdevice.h> -#include <rthw.h> +#include <drivers/serial.h> +#include "drivers/usb_device.h" #include "cdc.h" #ifdef RT_USB_DEVICE_CDC -#define TX_TIMEOUT 100 +#define TX_TIMEOUT 1000 #define CDC_RX_BUFSIZE 2048 #define CDC_MAX_PACKET_SIZE 64 #define VCOM_DEVICE "vcom" +#define VCOM_TASK_STK_SIZE 2048 + +//#define VCOM_TX_USE_DMA + ALIGN(RT_ALIGN_SIZE) -static rt_uint8_t vcom_thread_stack[512]; +static rt_uint8_t vcom_thread_stack[VCOM_TASK_STK_SIZE]; static struct rt_thread vcom_thread; #define VCOM_MQ_MSG_SZ 16 #define VCOM_MQ_MAX_MSG 4 @@ -52,7 +59,7 @@ static struct ucdc_line_coding line_coding; struct vcom { - struct rt_serial_device serial; + struct rt_serial_device serial; uep_t ep_out; uep_t ep_in; uep_t ep_cmd; @@ -61,7 +68,6 @@ struct vcom struct rt_completion wait; rt_uint8_t rx_rbp[CDC_RX_BUFSIZE]; struct rt_ringbuffer rx_ringbuffer; - struct serial_ringbuffer vcom_int_rx; }; struct vcom_tx_msg @@ -227,7 +233,7 @@ static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size) RT_ASSERT(func != RT_NULL); RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_in_handler %d\n", size)); - + rt_kprintf("%s size = %d\n",__func__,size); data = (struct vcom*)func->user_data; if ((size != 0) && (size % CDC_MAX_PACKET_SIZE == 0)) { @@ -276,11 +282,11 @@ static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size) rt_hw_interrupt_enable(level); /* notify receive data */ - rt_hw_serial_isr(&data->serial); + rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_RX_IND); data->ep_out->request.buffer = data->ep_out->buffer; data->ep_out->request.size = EP_MAXPACKET(data->ep_out); - data->ep_out->request.req_type = UIO_REQUEST_READ_MOST; + data->ep_out->request.req_type = UIO_REQUEST_READ_BEST; rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request); return RT_EOK; @@ -325,7 +331,7 @@ static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup) data.bCharFormat = 0; data.bDataBits = 8; data.bParityType = 0; - size = setup->length > 7 ? 7 : setup->length; + size = setup->wLength > 7 ? 7 : setup->wLength; rt_usbd_ep0_write(device, (void*)&data, size); @@ -380,7 +386,7 @@ static rt_err_t _interface_handler(ufunction_t func, ureq_t setup) data = (struct vcom*)func->user_data; - switch(setup->request) + switch(setup->bRequest) { case CDC_SEND_ENCAPSULATED_COMMAND: break; @@ -435,7 +441,7 @@ static rt_err_t _function_enable(ufunction_t func) data->ep_out->request.buffer = data->ep_out->buffer; data->ep_out->request.size = EP_MAXPACKET(data->ep_out); - data->ep_out->request.req_type = UIO_REQUEST_READ_MOST; + data->ep_out->request.req_type = UIO_REQUEST_READ_BEST; rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request); return RT_EOK; @@ -630,69 +636,111 @@ static int _vcom_getc(struct rt_serial_device *serial) return result; } -static rt_size_t _vcom_tx(struct rt_serial_device *serial, - const char *buf, rt_size_t size) +#ifdef VCOM_TX_USE_DMA +static rt_size_t _vcom_tx(struct rt_serial_device *serial, const char *buf, rt_size_t size,int direction) { - struct vcom_tx_msg msg; + static struct vcom_tx_msg msg; RT_ASSERT(serial != RT_NULL); RT_ASSERT(buf != RT_NULL); - msg.buf = buf; - msg.serial = serial; - msg.size = size; + rt_kprintf("%s\n",__func__); - if (rt_mq_send(&vcom_tx_thread_mq, (void*)&msg, - sizeof(struct vcom_tx_msg)) != RT_EOK) + msg.buf = buf; + msg.serial = serial; + msg.size = size; + + if (rt_mq_send(&vcom_tx_thread_mq, (void*)&msg, sizeof(struct vcom_tx_msg)) != RT_EOK) { rt_kprintf("vcom send msg fail\n"); return 0; } - + return size; } +#else +static int _vcom_putc(struct rt_serial_device *serial, char c) +{ + static struct vcom_tx_msg msg; + + RT_ASSERT(serial != RT_NULL); + + msg.buf = (void *)((rt_uint32_t)c); + msg.serial = serial; + msg.size = 1; + if (rt_mq_send(&vcom_tx_thread_mq, (void*)&msg, sizeof(struct vcom_tx_msg)) != RT_EOK) + { +// rt_kprintf("vcom send msg fail\n"); + return -1; + } + + return 1; +} +#endif static const struct rt_uart_ops usb_vcom_ops = { _vcom_configure, _vcom_control, - RT_NULL, +#ifndef VCOM_TX_USE_DMA + _vcom_putc, _vcom_getc, + RT_NULL +#else RT_NULL, - //_vcom_tx, + _vcom_getc, + _vcom_tx +#endif + }; /* Vcom Tx Thread */ static void vcom_tx_thread_entry(void* parameter) { struct vcom_tx_msg msg; - + rt_uint8_t ch; while (1) { - if (rt_mq_recv(&vcom_tx_thread_mq, (void*)&msg, - sizeof(struct vcom_tx_msg), RT_WAITING_FOREVER) == RT_EOK) + if (rt_mq_recv(&vcom_tx_thread_mq, (void*)&msg, sizeof(struct vcom_tx_msg), RT_WAITING_FOREVER) == RT_EOK) { struct ufunction *func; struct vcom *data; func = (struct ufunction*)msg.serial->parent.user_data; data = (struct vcom*)func->user_data; + if (!data->connected) { + /* drop msg */ +#ifndef VCOM_TX_USE_DMA + rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DONE); +#else + rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE); +#endif continue; } rt_completion_init(&data->wait); +#ifndef VCOM_TX_USE_DMA + ch = (rt_uint8_t)((rt_uint32_t)msg.buf); + data->ep_in->request.buffer = (rt_uint8_t*)&ch; +#else + data->ep_in->request.buffer = (rt_uint8_t*)msg.buf; +#endif + data->ep_in->request.size = msg.size; + data->ep_in->request.req_type = UIO_REQUEST_WRITE; - data->ep_in->request.buffer = (void*)msg.buf; - data->ep_in->request.size = msg.size; - data->ep_in->request.req_type = UIO_REQUEST_WRITE; rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request); if (rt_completion_wait(&data->wait, TX_TIMEOUT) != RT_EOK) { rt_kprintf("vcom tx timeout\n"); - } + } +#ifndef VCOM_TX_USE_DMA + rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DONE); +#else + rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE); +#endif } } } @@ -706,29 +754,41 @@ static void rt_usb_vcom_init(struct ufunction *func) /* initialize ring buffer */ rt_ringbuffer_init(&data->rx_ringbuffer, data->rx_rbp, CDC_RX_BUFSIZE); - config.baud_rate = BAUD_RATE_115200; - config.bit_order = BIT_ORDER_LSB; - config.data_bits = DATA_BITS_8; - config.parity = PARITY_NONE; - config.stop_bits = STOP_BITS_1; - config.invert = NRZ_NORMAL; + config.baud_rate = BAUD_RATE_115200; + config.data_bits = DATA_BITS_8; + config.stop_bits = STOP_BITS_1; + config.parity = PARITY_NONE; + config.bit_order = BIT_ORDER_LSB; + config.invert = NRZ_NORMAL; + config.bufsz = CDC_RX_BUFSIZE; - data->serial.ops = &usb_vcom_ops; - data->serial.int_rx = &data->vcom_int_rx; - data->serial.config = config; + data->serial.ops = &usb_vcom_ops; + data->serial.serial_rx = RT_NULL; + data->serial.config = config; /* register vcom device */ rt_hw_serial_register(&data->serial, VCOM_DEVICE, +#ifndef VCOM_TX_USE_DMA RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX, +#else + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX, +#endif func); /* create an vcom message queue */ - rt_mq_init(&vcom_tx_thread_mq, "vcomq", vcom_tx_thread_mq_pool, VCOM_MQ_MSG_SZ, - sizeof(vcom_tx_thread_mq_pool), RT_IPC_FLAG_FIFO); + rt_mq_init(&vcom_tx_thread_mq, + "vcomq", + vcom_tx_thread_mq_pool, + VCOM_MQ_MSG_SZ, + sizeof(vcom_tx_thread_mq_pool), + RT_IPC_FLAG_FIFO); + /* init usb device thread */ - rt_thread_init(&vcom_thread, "vcom", vcom_tx_thread_entry, RT_NULL, - vcom_thread_stack, 512, 8, 20); + rt_thread_init(&vcom_thread, "vcom", + vcom_tx_thread_entry, RT_NULL, + vcom_thread_stack, VCOM_TASK_STK_SIZE, + 8, 20); result = rt_thread_startup(&vcom_thread); RT_ASSERT(result == RT_EOK); } diff --git a/components/drivers/usb/usbdevice/class/hid.c b/components/drivers/usb/usbdevice/class/hid.c new file mode 100644 index 0000000000..97d9d8ac4b --- /dev/null +++ b/components/drivers/usb/usbdevice/class/hid.c @@ -0,0 +1,552 @@ +/* + * File : hid.c + * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team + * + * Change Logs: + * Date Author Notes + * 2017��3��13�� Urey the first version + */ + +#include <rthw.h> +#include <rtthread.h> +#include <rtservice.h> +#include <rtdevice.h> + +#include "drivers/usb_common.h" +#include "drivers/usb_device.h" + +#include "hid.h" + +#ifdef RT_USB_DEVICE_HID + +struct hid_s +{ + uep_t ep_in; + uep_t ep_out; + int status; + + rt_uint16_t protocol; + + rt_uint8_t report_buf[MAX_REPORT_SIZE]; +}; + +/* CustomHID_ConfigDescriptor */ +const rt_uint8_t _report_desc[] __attribute__ ((packed))= +{ + 0x06, 0xFF, 0x00, /* USAGE_PAGE (Vendor Page: 0xFF00) */ + 0x09, 0x01, /* USAGE (Demo Kit) */ + 0xa1, 0x01, /* COLLECTION (Application) */ + /* 6 */ + + /* Led 1 */ + 0x85, 0x01, /* REPORT_ID (1) */ + 0x09, 0x01, /* USAGE (LED 1) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ + + 0x85, 0x01, /* REPORT_ID (1) */ + 0x09, 0x01, /* USAGE (LED 1) */ + 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */ + /* 26 */ + + /* Led 2 */ + 0x85, 0x02, /* REPORT_ID 2 */ + 0x09, 0x02, /* USAGE (LED 2) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ + + 0x85, 0x02, /* REPORT_ID (2) */ + 0x09, 0x02, /* USAGE (LED 2) */ + 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */ + /* 46 */ + + /* Led 3 */ + 0x85, 0x03, /* REPORT_ID (3) */ + 0x09, 0x03, /* USAGE (LED 3) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ + + 0x85, 0x03, /* REPORT_ID (3) */ + 0x09, 0x03, /* USAGE (LED 3) */ + 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */ + /* 66 */ + + /* Led 4 */ + 0x85, 0x04, /* REPORT_ID 4) */ + 0x09, 0x04, /* USAGE (LED 4) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ + + 0x85, 0x04, /* REPORT_ID (4) */ + 0x09, 0x04, /* USAGE (LED 4) */ + 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */ + /* 86 */ + + /* key Push Button */ + 0x85, 0x05, /* REPORT_ID (5) */ + 0x09, 0x05, /* USAGE (Push Button) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ + 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */ + + 0x09, 0x05, /* USAGE (Push Button) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ + 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ + + 0x75, 0x07, /* REPORT_SIZE (7) */ + 0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */ + 0x85, 0x05, /* REPORT_ID (2) */ + + 0x75, 0x07, /* REPORT_SIZE (7) */ + 0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */ + /* 114 */ + + /* Tamper Push Button */ + 0x85, 0x06, /* REPORT_ID (6) */ + 0x09, 0x06, /* USAGE (Tamper Push Button) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ + 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */ + + 0x09, 0x06, /* USAGE (Tamper Push Button) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ + 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ + + 0x75, 0x07, /* REPORT_SIZE (7) */ + 0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */ + 0x85, 0x06, /* REPORT_ID (6) */ + + 0x75, 0x07, /* REPORT_SIZE (7) */ + 0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */ + /* 142 */ + + /* ADC IN */ + 0x85, 0x07, /* REPORT_ID (7) */ + 0x09, 0x07, /* USAGE (ADC IN) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */ + 0x85, 0x07, /* REPORT_ID (7) */ + 0x09, 0x07, /* USAGE (ADC in) */ + 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ + /* 161 */ + + 0xc0 /* END_COLLECTION */ +}; /* CustomHID_ReportDescriptor */ + +static struct udevice_descriptor _dev_desc = +{ + USB_DESC_LENGTH_DEVICE, //bLength; + USB_DESC_TYPE_DEVICE, //type; + USB_BCD_VERSION, //bcdUSB; + USB_CLASS_HID, //bDeviceClass; + 0x00, //bDeviceSubClass; + 0x00, //bDeviceProtocol; + 64, //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; +}; + +static struct usb_qualifier_descriptor _dev_qualifier = +{ + sizeof(_dev_qualifier), + USB_DESC_TYPE_DEVICEQUALIFIER, + 0x0200, + USB_CLASS_HID, + 0x00, + 64, + 0x01, + 0, +}; + +/* hid interface descriptor */ +const static struct uhid_comm_descriptor _hid_comm_desc = +{ +#ifdef RT_USB_DEVICE_COMPOSITE + /* Interface Association Descriptor */ + USB_DESC_LENGTH_IAD, + USB_DESC_TYPE_IAD, + USB_DYNAMIC, + 0x02, + 0x03, /* bInterfaceClass: HID */ + USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + 0x00, +#endif + + /* Interface Descriptor */ + USB_DESC_LENGTH_INTERFACE, + USB_DESC_TYPE_INTERFACE, + USB_DYNAMIC, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints */ + 0x03, /* bInterfaceClass: HID */ + USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + 0, /* iInterface: Index of string descriptor */ + + /* HID Descriptor */ + HID_DESCRIPTOR_SIZE, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x0110, /* bcdHID: HID Class Spec release number */ + 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + sizeof(_report_desc), /* wItemLength: Total length of Report descriptor */ + + /* Endpoint Descriptor IN */ + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_IN, + USB_EP_ATTR_INT, + 0x08, + 0x20, + + /* Endpoint Descriptor OUT */ + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_OUT, + USB_EP_ATTR_INT, + 0x08, + 0x20, +}; + + +const static char* _ustring[] = +{ + "Language", + "RT-Thread Team.", + "RTT HID-Device", + "32021919830108", + "Configuration", + "Interface", +}; + +#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ') +static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen) +{ + unsigned char *buf = (unsigned char*)ptr; + int i, j; + for (i=0; i<buflen; i+=16) + { + rt_kprintf("%06x: ", i); + for (j=0; j<16; j++) + if (i+j < buflen) + rt_kprintf("%02x ", buf[i+j]); + else + rt_kprintf(" "); + rt_kprintf(" "); + for (j=0; j<16; j++) + if (i+j < buflen) + rt_kprintf("%c", __is_print(buf[i+j]) ? buf[i+j] : '.'); + rt_kprintf("\n"); + } +} + +static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size) +{ + struct hid_s *data; + RT_ASSERT(func != RT_NULL); + RT_ASSERT(func->device != RT_NULL); + + rt_kprintf("%s size = %d\n",__func__,size); + data = (struct hid_s *) func->user_data; + + if(size != 0) + { + dump_hex(data->ep_out->buffer,size); + } + + data->ep_out->request.buffer = data->ep_out->buffer; + data->ep_out->request.size = EP_MAXPACKET(data->ep_out); + data->ep_out->request.req_type = UIO_REQUEST_READ_BEST; + rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request); + return RT_EOK; +} + +static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size) +{ + struct hid_s *data; + RT_ASSERT(func != RT_NULL); + RT_ASSERT(func->device != RT_NULL); + + data = (struct hid_s *) func->user_data; + + return RT_EOK; +} + +static rt_err_t _hid_set_report_callback(udevice_t device, rt_size_t size) +{ + RT_DEBUG_LOG(RT_DEBUG_USB, ("_hid_set_report_callback\n")); + + if(size != 0) + { + rt_kprintf("HID set report callback:\n"); + dump_hex(device->dcd->ep0.request.buffer - size,size); + } + + dcd_ep0_send_status(device->dcd); + + return RT_EOK; +} + +/** + * This function will handle hid interface bRequest. + * + * @param device the usb device object. + * @param setup the setup bRequest. + * + * @return RT_EOK on successful. + */ +static rt_err_t _interface_handler(ufunction_t func, ureq_t setup) +{ + RT_ASSERT(func != RT_NULL); + RT_ASSERT(func->device != RT_NULL); + RT_ASSERT(setup != RT_NULL); + + struct hid_s *data = (struct hid_s *) func->user_data; + + if(setup->wIndex != 0) + return -RT_EIO; + + switch (setup->bRequest) + { + case USB_REQ_GET_DESCRIPTOR: + if((setup->wValue >> 8) == USB_DESC_TYPE_REPORT) + { + rt_kprintf("HID get report descriptor\n"); + rt_kprintf("sizeof _report_desc = %d bytes\n",sizeof(_report_desc)); + rt_usbd_ep0_write(func->device, (void *)(&_report_desc[0]), sizeof(_report_desc)); + } + else if((setup->wValue >> 8) == USB_DESC_TYPE_HID) + { + rt_kprintf("HID get hid descriptor\n"); + + rt_usbd_ep0_write(func->device, (void *)(&_hid_comm_desc.hid_desc), sizeof(struct uhid_descriptor)); + } + break; + case USB_HID_REQ_GET_REPORT: + rt_kprintf("HID get report \n"); + rt_kprintf("wLength = %d\n",setup->wLength); + if(setup->wLength == 0) + { + rt_usbd_ep0_set_stall(func->device); + break; + } + if((setup->wLength == 0) || (setup->wLength > MAX_REPORT_SIZE)) + setup->wLength = MAX_REPORT_SIZE; + rt_usbd_ep0_write(func->device, data->report_buf,setup->wLength); + break; + case USB_HID_REQ_GET_IDLE: + rt_kprintf("HID get idle \n"); + + dcd_ep0_send_status(func->device->dcd); + break; + case USB_HID_REQ_GET_PROTOCOL: + rt_kprintf("HID get protocol \n"); + rt_usbd_ep0_write(func->device, &data->protocol,2); + break; + case USB_HID_REQ_SET_REPORT: + rt_kprintf("HID set report \n"); + rt_kprintf("set report size = %d\n",setup->wLength); + + if((setup->wLength == 0) || (setup->wLength > MAX_REPORT_SIZE)) + rt_usbd_ep0_set_stall(func->device); + + rt_usbd_ep0_read(func->device, data->report_buf, setup->wLength, _hid_set_report_callback); + break; + case USB_HID_REQ_SET_IDLE: + { + int duration = (setup->wValue >> 8); + int report_id = (setup->wValue & 0xFF); + rt_kprintf("HID set idle \n"); + rt_kprintf("duration = %d,report_id = %d\n",duration,report_id); + + dcd_ep0_send_status(func->device->dcd); + } + break; + case USB_HID_REQ_SET_PROTOCOL: + rt_kprintf("HID set protocol \n"); + data->protocol = setup->wValue; + + dcd_ep0_send_status(func->device->dcd); + break; + } + + return RT_EOK; +} + + +/** + * This function will run cdc function, it will be called on handle set configuration bRequest. + * + * @param func the usb function object. + * + * @return RT_EOK on successful. + */ +static rt_err_t _function_enable(ufunction_t func) +{ + struct hid_s *data; + + RT_ASSERT(func != RT_NULL); + RT_ASSERT(func->device != RT_NULL); + data = (struct hid_s *) func->user_data; + + RT_DEBUG_LOG(RT_DEBUG_USB, ("hid function enable\n")); +// +// _vcom_reset_state(func); +// + data->ep_out->buffer = rt_malloc(HID_RX_BUFSIZE); + data->ep_out->request.buffer = data->ep_out->buffer; + data->ep_out->request.size = EP_MAXPACKET(data->ep_out); + data->ep_out->request.req_type = UIO_REQUEST_READ_BEST; + + rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request); + + return RT_EOK; +} + +/** + * This function will stop cdc function, it will be called on handle set configuration bRequest. + * + * @param func the usb function object. + * + * @return RT_EOK on successful. + */ +static rt_err_t _function_disable(ufunction_t func) +{ + struct hid_s *data; + + RT_ASSERT(func != RT_NULL); + RT_ASSERT(func->device != RT_NULL); + data = (struct hid_s *) func->user_data; + + RT_DEBUG_LOG(RT_DEBUG_USB, ("hid function disable\n")); + + if(data->ep_out->buffer != RT_NULL) + { + rt_free(data->ep_out->buffer); + data->ep_out->buffer = RT_NULL; + } + + return RT_EOK; +} + +static struct ufunction_ops ops = +{ + _function_enable, + _function_disable, + RT_NULL, +}; + + + + +/** + * This function will configure hid descriptor. + * + * @param comm the communication interface number. + * @param data the data interface number. + * + * @return RT_EOK on successful. + */ +static rt_err_t _hid_descriptor_config(uhid_comm_desc_t hid, rt_uint8_t cintf_nr) +{ +#ifdef RT_USB_DEVICE_COMPOSITE + hid->iad_desc.bFirstInterface = cintf_nr; +#endif + + return RT_EOK; +} + +static void rt_usb_hid_init(struct ufunction *func) +{ + +} + + +/** + * This function will create a hid function instance. + * + * @param device the usb device object. + * + * @return RT_EOK on successful. + */ +ufunction_t rt_usbd_function_hid_create(udevice_t device) +{ + ufunction_t func; + struct hid_s *data; + + uintf_t hid_intf; + ualtsetting_t hid_setting; + uhid_comm_desc_t hid_desc; + + /* parameter check */ + RT_ASSERT(device != RT_NULL); + + /* set usb device string description */ + rt_usbd_device_set_string(device, _ustring); + + /* create a cdc function */ + func = rt_usbd_function_new(device, &_dev_desc, &ops); + rt_usbd_device_set_qualifier(device, &_dev_qualifier); + + /* allocate memory for cdc vcom data */ + data = (struct hid_s*)rt_malloc(sizeof(struct hid_s)); + rt_memset(data, 0, sizeof(struct hid_s)); + func->user_data = (void*)data; + + /* initilize hid */ + rt_usb_hid_init(func); + + /* create an interface object */ + hid_intf = rt_usbd_interface_new(device, _interface_handler); + + /* create an alternate setting object */ + hid_setting = rt_usbd_altsetting_new(sizeof(struct uhid_comm_descriptor)); + + /* config desc in alternate setting */ + rt_usbd_altsetting_config_descriptor(hid_setting, &_hid_comm_desc, (rt_off_t)&((uhid_comm_desc_t)0)->intf_desc); + + /* configure the hid interface descriptor */ + _hid_descriptor_config(hid_setting->desc, hid_intf->intf_num); + + /* create endpoint */ + hid_desc = (uhid_comm_desc_t)hid_setting->desc; + data->ep_out = rt_usbd_endpoint_new(&hid_desc->ep_out_desc, _ep_out_handler); + data->ep_in = rt_usbd_endpoint_new(&hid_desc->ep_in_desc, _ep_in_handler); + + /* add the int out and int in endpoint to the alternate setting */ + rt_usbd_altsetting_add_endpoint(hid_setting, data->ep_out); + rt_usbd_altsetting_add_endpoint(hid_setting, data->ep_in); + + /* add the alternate setting to the interface, then set default setting */ + rt_usbd_interface_add_altsetting(hid_intf, hid_setting); + rt_usbd_set_altsetting(hid_intf, 0); + + /* add the interface to the mass storage function */ + rt_usbd_function_add_interface(func, hid_intf); + return func; +} + +#endif /* RT_USB_DEVICE_HID */ diff --git a/components/drivers/usb/usbdevice/class/hid.h b/components/drivers/usb/usbdevice/class/hid.h new file mode 100644 index 0000000000..70e14422b1 --- /dev/null +++ b/components/drivers/usb/usbdevice/class/hid.h @@ -0,0 +1,55 @@ +/* + * File : hid.h + * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team + * + * Change Logs: + * Date Author Notes + * 2017��3��13�� Urey the first version + */ +#ifndef _USBDEVICE_CLASS_HID_H_ +#define _USBDEVICE_CLASS_HID_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define HID_DESCRIPTOR_TYPE 0x21 +#define HID_DESCRIPTOR_SIZE 0x09 +#define HID_OFF_HID_DESC 0x12 + +#define USB_HID_SUBCLASS_BOOT 0x01 +#define USB_HID_SUBCLASS_NOBOOT 0x00 + +#define USB_HID_PROTOCOL_NONE 0x00 +#define USB_HID_PROTOCOL_KEYBOARD 0x01 +#define USB_HID_PROTOCOL_MOUSE 0x02 + + +#define USB_HID_REQ_GET_REPORT 0x01 +#define USB_HID_REQ_GET_IDLE 0x02 +#define USB_HID_REQ_GET_PROTOCOL 0x03 +#define USB_HID_REQ_SET_REPORT 0x09 +#define USB_HID_REQ_SET_IDLE 0x0a +#define USB_HID_REQ_SET_PROTOCOL 0x0b + +#define MAX_REPORT_SIZE 8 +#define HID_RX_BUFSIZE 64 + +struct uhid_comm_descriptor +{ +#ifdef RT_USB_DEVICE_COMPOSITE + struct uiad_descriptor iad_desc; +#endif + struct uinterface_descriptor intf_desc; + struct uhid_descriptor hid_desc; + struct uendpoint_descriptor ep_in_desc; + struct uendpoint_descriptor ep_out_desc; +}; +typedef struct uhid_comm_descriptor* uhid_comm_desc_t; + + +#ifdef __cplusplus +} +#endif + +#endif /* _USBDEVICE_CLASS_HID_H_ */ diff --git a/components/drivers/usb/usbdevice/class/mstorage.c b/components/drivers/usb/usbdevice/class/mstorage.c index 4ee45189f8..1bde46fffa 100644 --- a/components/drivers/usb/usbdevice/class/mstorage.c +++ b/components/drivers/usb/usbdevice/class/mstorage.c @@ -27,7 +27,7 @@ #include <rtthread.h> #include <rtservice.h> -#include <rtdevice.h> +#include "drivers/usb_device.h" #include "mstorage.h" #ifdef RT_USB_DEVICE_MSTORAGE @@ -112,6 +112,8 @@ static struct usb_qualifier_descriptor dev_qualifier = 0, }; + + const static struct umass_descriptor _mass_desc = { USB_DESC_LENGTH_INTERFACE, //bLength; diff --git a/components/drivers/usb/usbdevice/core/core.c b/components/drivers/usb/usbdevice/core/core.c index b845b27c4c..6880f931f0 100644 --- a/components/drivers/usb/usbdevice/core/core.c +++ b/components/drivers/usb/usbdevice/core/core.c @@ -27,7 +27,8 @@ */ #include <rtthread.h> -#include <rtdevice.h> +#include "drivers/usb_common.h" +#include "drivers/usb_device.h" static rt_list_t device_list; @@ -37,10 +38,10 @@ 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); /** - * This function will handle get_device_descriptor request. + * This function will handle get_device_descriptor bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -54,22 +55,21 @@ static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup) RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n")); - /* device descriptor length should less than USB_DESC_LENGTH_DEVICE*/ - size = (setup->length > USB_DESC_LENGTH_DEVICE) ? - USB_DESC_LENGTH_DEVICE : setup->length; + /* device descriptor wLength should less than USB_DESC_LENGTH_DEVICE*/ + size = (setup->wLength > USB_DESC_LENGTH_DEVICE) ? + USB_DESC_LENGTH_DEVICE : setup->wLength; /* send device descriptor to endpoint 0 */ - rt_usbd_ep0_write(device, (rt_uint8_t*)&device->dev_desc, - size); + rt_usbd_ep0_write(device, (rt_uint8_t*) &device->dev_desc, size); return RT_EOK; } /** - * This function will handle get_config_descriptor request. + * This function will handle get_config_descriptor bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -85,8 +85,8 @@ static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup) RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n")); cfg_desc = &device->curr_cfg->cfg_desc; - size = (setup->length > cfg_desc->wTotalLength) ? - cfg_desc->wTotalLength : setup->length; + size = (setup->wLength > cfg_desc->wTotalLength) ? + cfg_desc->wTotalLength : setup->wLength; /* send configuration descriptor to endpoint 0 */ rt_usbd_ep0_write(device, (rt_uint8_t*)cfg_desc, size); @@ -95,12 +95,12 @@ static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup) } /** - * This function will handle get_string_descriptor request. + * This function will handle get_string_descriptor bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * - * @return RT_EOK on successful, -RT_ERROR on invalid request. + * @return RT_EOK on successful, -RT_ERROR on invalid bRequest. */ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup) { @@ -115,7 +115,7 @@ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup) RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n")); str_desc.type = USB_DESC_TYPE_STRING; - index = setup->value & 0xFF; + index = setup->wValue & 0xFF; if(index > USB_STRING_INTERFACE_INDEX) { @@ -141,10 +141,10 @@ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup) } } - if (setup->length > str_desc.bLength) + if (setup->wLength > str_desc.bLength) len = str_desc.bLength; else - len = setup->length; + len = setup->wLength; /* send string descriptor to endpoint 0 */ rt_usbd_ep0_write(device, (rt_uint8_t*)&str_desc, len); @@ -175,10 +175,10 @@ static rt_err_t _get_qualifier_descriptor(struct udevice* device, ureq_t setup) } /** - * This function will handle get_descriptor request. + * This function will handle get_descriptor bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -190,7 +190,7 @@ static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup) if(setup->request_type == USB_REQ_TYPE_DIR_IN) { - switch(setup->value >> 8) + switch(setup->wValue >> 8) { case USB_DESC_TYPE_DEVICE: _get_device_descriptor(device, setup); @@ -220,10 +220,10 @@ static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup) } /** - * This function will handle get_interface request. + * This function will handle get_interface bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -245,7 +245,7 @@ static rt_err_t _get_interface(struct udevice* device, ureq_t setup) } /* find the specified interface and its alternate setting */ - intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL); + intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL); value = intf->curr_setting->intf_desc->bAlternateSetting; /* send the interface alternate setting to endpoint 0*/ @@ -255,10 +255,10 @@ static rt_err_t _get_interface(struct udevice* device, ureq_t setup) } /** - * This function will handle set_interface request. + * This function will handle set_interface bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -282,10 +282,10 @@ static rt_err_t _set_interface(struct udevice* device, ureq_t setup) } /* find the specified interface */ - intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL); + intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL); /* set alternate setting to the interface */ - rt_usbd_set_altsetting(intf, setup->value & 0xFF); + rt_usbd_set_altsetting(intf, setup->wValue & 0xFF); setting = intf->curr_setting; /* start all endpoints of the interface alternate setting */ @@ -301,10 +301,10 @@ static rt_err_t _set_interface(struct udevice* device, ureq_t setup) } /** - * This function will handle get_config request. + * This function will handle get_config bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -335,10 +335,10 @@ static rt_err_t _get_config(struct udevice* device, ureq_t setup) } /** - * This function will handle set_config request. + * This function will handle set_config bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -356,13 +356,13 @@ static rt_err_t _set_config(struct udevice* device, ureq_t setup) RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n")); - if (setup->value > device->dev_desc.bNumConfigurations) + if (setup->wValue > device->dev_desc.bNumConfigurations) { rt_usbd_ep0_set_stall(device); return -RT_ERROR; } - if (setup->value == 0) + if (setup->wValue == 0) { RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n")); device->state = USB_STATE_ADDRESS; @@ -371,8 +371,7 @@ static rt_err_t _set_config(struct udevice* device, ureq_t setup) } /* set current configuration */ - rt_usbd_set_config(device, setup->value); - dcd_set_config(device->dcd, setup->value); + rt_usbd_set_config(device, setup->wValue); cfg = device->curr_cfg; for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next) @@ -406,10 +405,10 @@ _exit: } /** - * This function will handle set_address request. + * This function will handle set_address bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -419,12 +418,12 @@ static rt_err_t _set_address(struct udevice* device, ureq_t setup) RT_ASSERT(device != RT_NULL); RT_ASSERT(setup != RT_NULL); + /* set address in device control driver */ + dcd_set_address(device->dcd, setup->wValue); + /* issue status stage */ dcd_ep0_send_status(device->dcd); - /* set address in device control driver */ - dcd_set_address(device->dcd, setup->value); - RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n")); device->state = USB_STATE_ADDRESS; @@ -433,11 +432,11 @@ static rt_err_t _set_address(struct udevice* device, ureq_t setup) } /** - * This function will handle standard request to + * This function will handle standard bRequest to * interface that defined in function-specifics * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -453,7 +452,7 @@ static rt_err_t _request_interface(struct udevice* device, ureq_t setup) RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n")); - intf = rt_usbd_find_interface(device, setup->index & 0xFF, &func); + intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func); if (intf != RT_NULL) { ret = intf->handler(func, setup); @@ -467,10 +466,10 @@ static rt_err_t _request_interface(struct udevice* device, ureq_t setup) } /** - * This function will handle standard request. + * This function will handle standard bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * * @return RT_EOK on successful. */ @@ -488,17 +487,17 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup) switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK) { case USB_REQ_TYPE_DEVICE: - switch(setup->request) + switch(setup->bRequest) { case USB_REQ_GET_STATUS: rt_usbd_ep0_write(device, &value, 2); break; case USB_REQ_CLEAR_FEATURE: - rt_usbd_clear_feature(device, setup->value, setup->index); + rt_usbd_clear_feature(device, setup->wValue, setup->wIndex); dcd_ep0_send_status(dcd); break; case USB_REQ_SET_FEATURE: - rt_usbd_set_feature(device, setup->value, setup->index); + rt_usbd_set_feature(device, setup->wValue, setup->wIndex); break; case USB_REQ_SET_ADDRESS: _set_address(device, setup); @@ -522,7 +521,7 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup) } break; case USB_REQ_TYPE_INTERFACE: - switch(setup->request) + switch(setup->bRequest) { case USB_REQ_GET_INTERFACE: _get_interface(device, setup); @@ -542,13 +541,13 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup) } break; case USB_REQ_TYPE_ENDPOINT: - switch(setup->request) + switch(setup->bRequest) { case USB_REQ_GET_STATUS: { uep_t ep; - ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index); + ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex); value = ep->stalled; rt_usbd_ep0_write(device, &value, 2); } @@ -559,10 +558,10 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup) uio_request_t req; struct rt_list_node *node; - ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index); - if(USB_EP_HALT == setup->value && ep->stalled == RT_TRUE) + ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex); + if(USB_EP_HALT == setup->wValue && ep->stalled == RT_TRUE) { - rt_usbd_clear_feature(device, setup->value, setup->index); + rt_usbd_clear_feature(device, setup->wValue, setup->wIndex); dcd_ep0_send_status(dcd); ep->stalled = RT_FALSE; @@ -581,11 +580,11 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup) { uep_t ep; - if(USB_EP_HALT == setup->value) + if(USB_EP_HALT == setup->wValue) { - ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index); + ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex); ep->stalled = RT_TRUE; - rt_usbd_set_feature(device, setup->value, setup->index); + rt_usbd_set_feature(device, setup->wValue, setup->wIndex); dcd_ep0_send_status(dcd); } } @@ -612,12 +611,12 @@ static rt_err_t _standard_request(struct udevice* device, ureq_t setup) } /** - * This function will handle function request. + * This function will handle function bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * - * @return RT_EOK on successful, -RT_ERROR on invalid request. + * @return RT_EOK on successful, -RT_ERROR on invalid bRequest. */ static rt_err_t _function_request(udevice_t device, ureq_t setup) { @@ -628,8 +627,8 @@ static rt_err_t _function_request(udevice_t device, ureq_t setup) RT_ASSERT(device != RT_NULL); RT_ASSERT(setup != RT_NULL); - /* verify request value */ - if(setup->index > device->curr_cfg->cfg_desc.bNumInterfaces) + /* verify bRequest wValue */ + if(setup->wIndex > device->curr_cfg->cfg_desc.bNumInterfaces) { rt_usbd_ep0_set_stall(device); return -RT_ERROR; @@ -638,7 +637,7 @@ static rt_err_t _function_request(udevice_t device, ureq_t setup) switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK) { case USB_REQ_TYPE_INTERFACE: - intf = rt_usbd_find_interface(device, setup->index & 0xFF, &func); + intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func); if(intf == RT_NULL) { rt_kprintf("unkwown interface request\n"); @@ -663,24 +662,24 @@ static rt_err_t _function_request(udevice_t device, ureq_t setup) static rt_err_t _dump_setup_packet(ureq_t setup) { RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n")); - RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request 0x%x\n", + RT_DEBUG_LOG(RT_DEBUG_USB, (" setup_request : 0x%x\n", setup->request_type)); - RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value)); - RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length)); - RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index)); - RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request)); + RT_DEBUG_LOG(RT_DEBUG_USB, (" 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)); RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n")); return RT_EOK; } /** - * This function will handle setup request. + * This function will handle setup bRequest. * * @param device the usb device object. - * @param setup the setup request. + * @param setup the setup bRequest. * - * @return RT_EOK on successful, -RT_ERROR on invalid request. + * @return RT_EOK on successful, -RT_ERROR on invalid bRequest. */ static rt_err_t _setup_request(udevice_t device, ureq_t setup) { @@ -741,17 +740,16 @@ static rt_err_t _data_notify(udevice_t device, struct ep_msg* ep_msg) if(EP_ADDRESS(ep) & USB_DIR_IN) { + size = ep_msg->size; if(ep->request.remain_size >= EP_MAXPACKET(ep)) { - dcd_ep_write(device->dcd, EP_ADDRESS(ep), - ep->request.buffer, EP_MAXPACKET(ep)); + dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, EP_MAXPACKET(ep)); ep->request.remain_size -= EP_MAXPACKET(ep); ep->request.buffer += EP_MAXPACKET(ep); } else if(ep->request.remain_size > 0) { - dcd_ep_write(device->dcd, EP_ADDRESS(ep), - ep->request.buffer, ep->request.remain_size); + dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, ep->request.remain_size); ep->request.remain_size = 0; } else @@ -769,21 +767,12 @@ static rt_err_t _data_notify(udevice_t device, struct ep_msg* ep_msg) if(size == 0) { - size = dcd_ep_read(device->dcd, EP_ADDRESS(ep), - ep->request.buffer); - } - - if(size > ep->request.remain_size) - { - ep->request.remain_size = 0; - } - else - { - ep->request.remain_size -= size; - ep->request.buffer += size; + size = dcd_ep_read(device->dcd, EP_ADDRESS(ep), ep->request.buffer); } + ep->request.remain_size -= size; + ep->request.buffer += size; - if(ep->request.req_type == UIO_REQUEST_READ_MOST) + if(ep->request.req_type == UIO_REQUEST_READ_BEST) { EP_HANDLER(ep, func, size); } @@ -807,6 +796,7 @@ static rt_err_t _ep0_out_notify(udevice_t device, struct ep_msg* ep_msg) ep0 = &device->dcd->ep0; size = ep_msg->size; + if(ep0->request.remain_size == 0) { return RT_EOK; @@ -889,7 +879,6 @@ static rt_err_t _stop_notify(udevice_t device) static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size) { rt_uint16_t maxpacket; - rt_size_t sent_size; RT_ASSERT(device != RT_NULL); RT_ASSERT(device->dcd != RT_NULL); @@ -898,18 +887,18 @@ static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_s maxpacket = EP_MAXPACKET(ep); if(ep->request.remain_size >= maxpacket) { - sent_size = dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, maxpacket); - ep->request.remain_size -= sent_size; + dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, maxpacket); + ep->request.remain_size -= maxpacket; ep->request.buffer += maxpacket; } else { - sent_size = dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, + dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, ep->request.remain_size); - ep->request.remain_size -= sent_size; + ep->request.remain_size = 0; } - return sent_size; + return size; } static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size) @@ -1047,7 +1036,7 @@ uconfig_t rt_usbd_config_new(void) } rt_memset(cfg, 0, sizeof(struct uconfig)); - /* set default value */ + /* set default wValue */ cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG; cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION; cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG; @@ -1258,7 +1247,7 @@ udevice_t rt_usbd_find_device(udcd_t dcd) * This function will find an usb configuration object. * * @param device the usb device object. - * @param value the configuration number. + * @param wValue the configuration number. * * @return an usb configuration object on found or RT_NULL on not found. */ @@ -1291,7 +1280,7 @@ uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value) * This function will find an usb interface object. * * @param device the usb device object. - * @param value the interface number. + * @param wValue the interface number. * * @return an usb configuration object on found or RT_NULL on not found. */ @@ -1332,7 +1321,7 @@ uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t * * This function will find an usb interface alternate setting object. * * @param device the usb device object. - * @param value the alternate setting number. + * @param wValue the alternate setting number. * * @return an usb interface alternate setting object on found or RT_NULL on not found. */ @@ -1348,7 +1337,7 @@ ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value) if(intf->curr_setting != RT_NULL) { - /* if the value equal to the current alternate setting, then do not search */ + /* if the wValue equal to the current alternate setting, then do not search */ if(intf->curr_setting->intf_desc->bAlternateSetting == value) return intf->curr_setting; } @@ -1384,8 +1373,7 @@ uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_ RT_ASSERT(device != RT_NULL); /* search a endpoint in the current configuration */ - for (i=device->curr_cfg->func_list.next; - i!=&device->curr_cfg->func_list; i=i->next) + for (i=device->curr_cfg->func_list.next; i!=&device->curr_cfg->func_list; i=i->next) { func = (ufunction_t)rt_list_entry(i, struct ufunction, list); for(j=func->intf_list.next; j!=&func->intf_list; j=j->next) @@ -1563,7 +1551,7 @@ rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep) * This function will set an alternate setting for an interface. * * @param intf_desc the interface descriptor. - * @param value the alternate setting number. + * @param wValue the alternate setting number. * * @return RT_EOK. */ @@ -1589,7 +1577,7 @@ rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value) * This function will set a configuration for an usb device. * * @param device the usb device object. - * @param value the configuration number. + * @param wValue the configuration number. * * @return RT_EOK. */ @@ -1608,16 +1596,18 @@ rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value) /* set as current configuration */ device->curr_cfg = cfg; + + dcd_set_config(device->dcd, value); return RT_TRUE; } /** - * This function will request an IO transaction. + * This function will bRequest an IO transaction. * * @param device the usb device object. * @param ep the endpoint object. - * @param req IO request. + * @param req IO bRequest. * * @return RT_EOK. */ @@ -1632,7 +1622,7 @@ rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req) { switch(req->req_type) { - case UIO_REQUEST_READ_MOST: + case UIO_REQUEST_READ_BEST: case UIO_REQUEST_READ_FULL: ep->request.remain_size = ep->request.size; size = rt_usbd_ep_read_prepare(device, ep, req->buffer, req->size); @@ -1659,7 +1649,7 @@ rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req) * This function will set feature for an usb device. * * @param device the usb device object. - * @param value the configuration number. + * @param wValue the configuration number. * * @return RT_EOK. */ @@ -1684,7 +1674,7 @@ rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t in * This function will clear feature for an usb device. * * @param device the usb device object. - * @param value the configuration number. + * @param wValue the configuration number. * * @return RT_EOK. */ @@ -1766,8 +1756,7 @@ static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep) while(device->dcd->ep_pool[i].addr != 0xFF) { if(device->dcd->ep_pool[i].status == ID_UNASSIGNED && - ep->ep_desc->bmAttributes == device->dcd->ep_pool[i].type && - ep->ep_desc->bEndpointAddress == device->dcd->ep_pool[i].dir) + ep->ep_desc->bmAttributes == device->dcd->ep_pool[i].type) { EP_ADDRESS(ep) |= device->dcd->ep_pool[i].addr; ep->id = &device->dcd->ep_pool[i]; @@ -1832,6 +1821,7 @@ rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd) { dcd_ep_write(dcd, EP0_IN_ADDR, dcd->ep0.request.buffer, dcd->ep0.id->maxpacket); dcd->ep0.request.remain_size -= dcd->ep0.id->maxpacket; + dcd->ep0.request.buffer += dcd->ep0.id->maxpacket; } else if(dcd->ep0.request.remain_size > 0) { @@ -1860,7 +1850,7 @@ rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size) return RT_EOK; } -rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address) +rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size) { struct udev_msg msg; @@ -1869,7 +1859,7 @@ rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address) msg.type = USB_MSG_DATA_NOTIFY; msg.dcd = dcd; msg.content.ep_msg.ep_addr = address; - msg.content.ep_msg.size = 0; + msg.content.ep_msg.size = size; rt_usbd_event_signal(&msg); return RT_EOK; @@ -1945,7 +1935,6 @@ rt_err_t rt_usbd_sof_handler(udcd_t dcd) rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size) { uep_t ep0; - rt_size_t sent_size = 0; RT_ASSERT(device != RT_NULL); RT_ASSERT(device->dcd != RT_NULL); @@ -1956,19 +1945,20 @@ rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size) ep0->request.size = size; ep0->request.buffer = buffer; ep0->request.remain_size = size; + if(ep0->request.remain_size >= ep0->id->maxpacket) { - sent_size = dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->id->maxpacket); - ep0->request.remain_size -= sent_size; + dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->id->maxpacket); + ep0->request.remain_size -= ep0->id->maxpacket; ep0->request.buffer += ep0->id->maxpacket; } else { - sent_size = dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->request.remain_size); - ep0->request.remain_size -= sent_size; + dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->request.remain_size); + ep0->request.remain_size = 0; } - return sent_size; + return size; } rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size, @@ -2094,12 +2084,18 @@ rt_err_t rt_usbd_core_init(void) rt_list_init(&device_list); /* create an usb message queue */ - rt_mq_init(&usb_mq, "usbd", usb_mq_pool, USBD_MQ_MSG_SZ, - sizeof(usb_mq_pool), RT_IPC_FLAG_FIFO); + rt_mq_init(&usb_mq, + "usbd", + usb_mq_pool, USBD_MQ_MSG_SZ, + sizeof(usb_mq_pool), + RT_IPC_FLAG_FIFO); /* init usb device thread */ - 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); + 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); /* rt_thread_init should always be OK, so start the thread without further * checking. */ return rt_thread_startup(&usb_thread); diff --git a/components/drivers/usb/usbdevice/core/usbdevice.c b/components/drivers/usb/usbdevice/core/usbdevice.c index adef1db2a0..2c7bd39e16 100644 --- a/components/drivers/usb/usbdevice/core/usbdevice.c +++ b/components/drivers/usb/usbdevice/core/usbdevice.c @@ -89,27 +89,47 @@ rt_err_t rt_usb_device_init(void) cfg = rt_usbd_config_new(); #ifdef RT_USB_DEVICE_MSTORAGE - /* create a mass storage function object */ - func = rt_usbd_function_mstorage_create(udevice); + { + extern ufunction_t rt_usbd_function_mstorage_create(udevice_t device); + /* create a mass storage function object */ + func = rt_usbd_function_mstorage_create(udevice); - /* add the function to the configuration */ - rt_usbd_config_add_function(cfg, func); + /* add the function to the configuration */ + rt_usbd_config_add_function(cfg, func); + } #endif #ifdef RT_USB_DEVICE_CDC - /* create a cdc function object */ - func = rt_usbd_function_cdc_create(udevice); + { + 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 - /* add the function to the configuration */ - rt_usbd_config_add_function(cfg, func); +#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); + } #endif #ifdef RT_USB_DEVICE_RNDIS - /* create a rndis function object */ - func = rt_usbd_function_rndis_create(udevice); + { + extern ufunction_t rt_usbd_function_rndis_create(udevice_t device); + /* create a rndis function object */ + func = rt_usbd_function_rndis_create(udevice); - /* add the function to the configuration */ - rt_usbd_config_add_function(cfg, func); + /* add the function to the configuration */ + rt_usbd_config_add_function(cfg, func); + } #endif /* set device descriptor to the device */ @@ -131,5 +151,4 @@ rt_err_t rt_usb_device_init(void) return RT_EOK; } - #endif |
