Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
SummerGao.
rt-thread
提交
cc54e0a7
R
rt-thread
项目概览
SummerGao.
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
cc54e0a7
编写于
11月 11, 2017
作者:
还_没_想_好
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[usb]update usb stack
上级
f7a6078c
变更
21
展开全部
隐藏空白更改
内联
并排
Showing
21 changed file
with
2744 addition
and
261 deletion
+2744
-261
components/drivers/include/drivers/usb_common.h
components/drivers/include/drivers/usb_common.h
+20
-15
components/drivers/include/drivers/usb_device.h
components/drivers/include/drivers/usb_device.h
+11
-11
components/drivers/usb/SConscript
components/drivers/usb/SConscript
+3
-4
components/drivers/usb/usbdevice/SConscript
components/drivers/usb/usbdevice/SConscript
+4
-1
components/drivers/usb/usbdevice/class/cdc_vcom.c
components/drivers/usb/usbdevice/class/cdc_vcom.c
+103
-43
components/drivers/usb/usbdevice/class/hid.c
components/drivers/usb/usbdevice/class/hid.c
+552
-0
components/drivers/usb/usbdevice/class/hid.h
components/drivers/usb/usbdevice/class/hid.h
+55
-0
components/drivers/usb/usbdevice/class/mstorage.c
components/drivers/usb/usbdevice/class/mstorage.c
+3
-1
components/drivers/usb/usbdevice/core/core.c
components/drivers/usb/usbdevice/core/core.c
+119
-123
components/drivers/usb/usbdevice/core/usbdevice.c
components/drivers/usb/usbdevice/core/usbdevice.c
+32
-13
components/drivers/usb/usbhost/SConscript
components/drivers/usb/usbhost/SConscript
+1
-1
components/drivers/usb/usbhost/class/hid.c
components/drivers/usb/usbhost/class/hid.c
+397
-0
components/drivers/usb/usbhost/class/hid.h
components/drivers/usb/usbhost/class/hid.h
+45
-0
components/drivers/usb/usbhost/class/mass.c
components/drivers/usb/usbhost/class/mass.c
+648
-0
components/drivers/usb/usbhost/class/mass.h
components/drivers/usb/usbhost/class/mass.h
+54
-0
components/drivers/usb/usbhost/class/udisk.c
components/drivers/usb/usbhost/class/udisk.c
+419
-0
components/drivers/usb/usbhost/class/ukbd.c
components/drivers/usb/usbhost/class/ukbd.c
+72
-0
components/drivers/usb/usbhost/class/umouse.c
components/drivers/usb/usbhost/class/umouse.c
+157
-0
components/drivers/usb/usbhost/core/core.c
components/drivers/usb/usbhost/core/core.c
+22
-22
components/drivers/usb/usbhost/core/driver.c
components/drivers/usb/usbhost/core/driver.c
+1
-1
components/drivers/usb/usbhost/core/hub.c
components/drivers/usb/usbhost/core/hub.c
+26
-26
未找到文件。
components/drivers/include/drivers/usb_common.h
浏览文件 @
cc54e0a7
...
...
@@ -313,12 +313,12 @@ typedef struct uiad_descriptor* uiad_desc_t;
struct
uendpoint_descriptor
{
rt_uint8_t
bLength
;
rt_uint8_t
type
;
rt_uint8_t
bEndpointAddress
;
rt_uint8_t
bmAttributes
;
rt_uint8_t
bLength
;
rt_uint8_t
type
;
rt_uint8_t
bEndpointAddress
;
rt_uint8_t
bmAttributes
;
rt_uint16_t
wMaxPacketSize
;
rt_uint8_t
bInterval
;
rt_uint8_t
bInterval
;
};
typedef
struct
uendpoint_descriptor
*
uep_desc_t
;
...
...
@@ -358,33 +358,38 @@ struct usb_qualifier_descriptor
rt_uint8_t
bRESERVED
;
}
__attribute__
((
packed
));
#ifndef HID_SUB_DESCRIPTOR_MAX
#define HID_SUB_DESCRIPTOR_MAX 1
#endif
struct
uhid_descriptor
{
rt_uint8_t
bLength
;
rt_uint8_t
type
;
rt_uint8_t
bLength
;
rt_uint8_t
type
;
rt_uint16_t
bcdHID
;
rt_uint8_t
bCountryCode
;
rt_uint8_t
bNumDescriptors
;
rt_uint8_t
bCountryCode
;
rt_uint8_t
bNumDescriptors
;
struct
hid_descriptor_list
{
rt_uint8_t
type
;
rt_uint16_t
wLength
;
}
Descriptor
[
1
];
}
Descriptor
[
HID_SUB_DESCRIPTOR_MAX
];
};
typedef
struct
uhid_descriptor
*
uhid_desc_t
;
struct
urequest
{
rt_uint8_t
request_type
;
rt_uint8_t
r
equest
;
rt_uint16_t
v
alue
;
rt_uint16_t
i
ndex
;
rt_uint16_t
l
ength
;
rt_uint8_t
request_type
;
rt_uint8_t
bR
equest
;
rt_uint16_t
wV
alue
;
rt_uint16_t
wI
ndex
;
rt_uint16_t
wL
ength
;
};
typedef
struct
urequest
*
ureq_t
;
#ifndef MIN
#define MIN(a, b) (a < b ? a : b)
#endif
#ifndef MAX
#define MAX(a, b) (a > b ? a : b)
#endif
...
...
components/drivers/include/drivers/usb_device.h
浏览文件 @
cc54e0a7
...
...
@@ -28,13 +28,13 @@
#define __USB_DEVICE_H__
#include <rtthread.h>
#include "usb_common.h"
#include "
drivers/
usb_common.h"
/* Vendor ID */
#ifdef USB_VENDOR_ID
#define _VENDOR_ID USB_VENDOR_ID
#define _VENDOR_ID
USB_VENDOR_ID
#else
#define _VENDOR_ID 0x0EFF
#define _VENDOR_ID
0x0EFF
#endif
/* Product ID */
#ifdef USB_PRODUCT_ID
...
...
@@ -76,7 +76,7 @@ typedef enum
/* request to read full count */
UIO_REQUEST_READ_FULL
,
/* request to read any count */
UIO_REQUEST_READ_
MO
ST
,
UIO_REQUEST_READ_
BE
ST
,
/* request to write full count */
UIO_REQUEST_WRITE
,
}
UIO_REQUEST_TYPE
;
...
...
@@ -99,11 +99,11 @@ struct udcd_ops
struct
ep_id
{
rt_uint8_t
addr
;
rt_uint8_t
type
;
rt_uint8_t
dir
;
rt_uint
8
_t
maxpacket
;
rt_uint8_t
status
;
rt_uint8_t
addr
;
rt_uint8_t
type
;
rt_uint8_t
dir
;
rt_uint
16
_t
maxpacket
;
rt_uint8_t
status
;
};
typedef
rt_err_t
(
*
udep_handler_t
)(
struct
ufunction
*
func
,
rt_size_t
size
);
...
...
@@ -238,7 +238,7 @@ struct udev_msg
udcd_t
dcd
;
union
{
struct
ep_msg
ep_msg
;
struct
ep_msg
ep_msg
;
struct
urequest
setup
;
}
content
;
};
...
...
@@ -295,7 +295,7 @@ rt_err_t rt_usbd_ep0_clear_stall(udevice_t device);
rt_err_t
rt_usbd_ep0_setup_handler
(
udcd_t
dcd
,
struct
urequest
*
setup
);
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
);
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
);
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
);
...
...
components/drivers/usb/SConscript
浏览文件 @
cc54e0a7
...
...
@@ -2,13 +2,12 @@
import
os
from
building
import
*
cwd
=
GetCurrentDir
()
objs
=
[]
list
=
os
.
listdir
(
cwd
)
cwd
=
GetCurrentDir
()
objs
=
[]
list
=
os
.
listdir
(
cwd
)
for
d
in
list
:
path
=
os
.
path
.
join
(
cwd
,
d
)
if
os
.
path
.
isfile
(
os
.
path
.
join
(
path
,
'SConscript'
)):
objs
=
objs
+
SConscript
(
os
.
path
.
join
(
d
,
'SConscript'
))
Return
(
'objs'
)
components/drivers/usb/usbdevice/SConscript
浏览文件 @
cc54e0a7
...
...
@@ -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'
)
components/drivers/usb/usbdevice/class/cdc_vcom.c
浏览文件 @
cc54e0a7
...
...
@@ -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 100
0
#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_
MO
ST
;
data
->
ep_out
->
request
.
req_type
=
UIO_REQUEST_READ_
BE
ST
;
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
->
l
ength
;
size
=
setup
->
wLength
>
7
?
7
:
setup
->
wL
ength
;
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
->
r
equest
)
switch
(
setup
->
bR
equest
)
{
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_
MO
ST
;
data
->
ep_out
->
request
.
req_type
=
UIO_REQUEST_READ_
BE
ST
;
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
;
st
atic
st
ruct
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
);
}
...
...
components/drivers/usb/usbdevice/class/hid.c
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : hid.c
* COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
*
* Change Logs:
* Date Author Notes
* 2017313 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 */
components/drivers/usb/usbdevice/class/hid.h
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : hid.h
* COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
*
* Change Logs:
* Date Author Notes
* 2017313 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_ */
components/drivers/usb/usbdevice/class/mstorage.c
浏览文件 @
cc54e0a7
...
...
@@ -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;
...
...
components/drivers/usb/usbdevice/core/core.c
浏览文件 @
cc54e0a7
此差异已折叠。
点击以展开。
components/drivers/usb/usbdevice/core/usbdevice.c
浏览文件 @
cc54e0a7
...
...
@@ -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
components/drivers/usb/usbhost/SConscript
浏览文件 @
cc54e0a7
...
...
@@ -29,6 +29,6 @@ if GetDepend('RT_USBH_HID_KEYBOARD'):
CPPPATH
=
[
cwd
,
cwd
+
'/class'
,
cwd
+
'/core'
,
\
cwd
+
'/include'
,
cwd
+
'../../../include'
]
group
=
DefineGroup
(
'
DeviceDrivers'
,
src
,
depend
=
[
'RT_USING_USB_HOST
'
],
CPPPATH
=
CPPPATH
)
group
=
DefineGroup
(
'
rt_usbh'
,
src
,
depend
=
[
'RT_USING_USBH
'
],
CPPPATH
=
CPPPATH
)
Return
(
'group'
)
components/drivers/usb/usbhost/class/hid.c
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : hid.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, 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
* 2011-12-12 Yi Qiu first version
*/
#include <rtthread.h>
#include <drivers/usb_host.h>
#include "hid.h"
#ifdef RT_USBH_HID
static
struct
uclass_driver
hid_driver
;
static
rt_list_t
_protocal_list
;
/**
* This function will do USB_REQ_SET_IDLE request to set idle period to the usb hid device
*
* @param intf the interface instance.
* @duration the idle period of requesting data.
* @report_id the report id
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_hid_set_idle
(
struct
uintf
*
intf
,
int
duration
,
int
report_id
)
{
struct
urequest
setup
;
struct
uinstance
*
device
;
int
timeout
=
100
;
/* parameter check */
RT_ASSERT
(
intf
!=
RT_NULL
);
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
device
=
intf
->
device
;
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_INTERFACE
;
setup
.
request
=
USB_REQ_SET_IDLE
;
setup
.
index
=
0
;
setup
.
length
=
0
;
setup
.
value
=
(
duration
<<
8
)
|
report_id
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
RT_NULL
,
0
,
timeout
)
==
0
)
return
RT_EOK
;
else
return
-
RT_FALSE
;
}
/**
* This function will do USB_REQ_GET_REPORT request to get report from the usb hid device
*
* @param intf the interface instance.
* @buffer the data buffer to save usb report descriptor.
* @param nbytes the size of buffer
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_hid_get_report
(
struct
uintf
*
intf
,
rt_uint8_t
type
,
rt_uint8_t
id
,
rt_uint8_t
*
buffer
,
rt_size_t
size
)
{
struct
urequest
setup
;
struct
uinstance
*
device
;
int
timeout
=
100
;
/* parameter check */
RT_ASSERT
(
intf
!=
RT_NULL
);
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
device
=
intf
->
device
;
setup
.
request_type
=
USB_REQ_TYPE_DIR_IN
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_INTERFACE
;
setup
.
request
=
USB_REQ_GET_REPORT
;
setup
.
index
=
intf
->
intf_desc
->
bInterfaceNumber
;
setup
.
length
=
size
;
setup
.
value
=
(
type
<<
8
)
+
id
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
buffer
,
size
,
timeout
)
==
size
)
return
RT_EOK
;
else
return
-
RT_FALSE
;
}
/**
* This function will do USB_REQ_SET_REPORT request to set report to the usb hid device
*
* @param intf the interface instance.
* @buffer the data buffer to save usb report descriptor.
* @param nbytes the size of buffer
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_hid_set_report
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
,
rt_size_t
size
)
{
struct
urequest
setup
;
struct
uinstance
*
device
;
int
timeout
=
100
;
/* parameter check */
RT_ASSERT
(
intf
!=
RT_NULL
);
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
device
=
intf
->
device
;
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_INTERFACE
;
setup
.
request
=
USB_REQ_SET_REPORT
;
setup
.
index
=
intf
->
intf_desc
->
bInterfaceNumber
;
setup
.
length
=
size
;
setup
.
value
=
0x02
<<
8
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
buffer
,
size
,
timeout
)
==
size
)
return
RT_EOK
;
else
return
-
RT_FALSE
;
}
/**
* This function will do USB_REQ_SET_PROTOCOL request to set protocal to the usb hid device.
*
* @param intf the interface instance.
* @param protocol the protocol id.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_hid_set_protocal
(
struct
uintf
*
intf
,
int
protocol
)
{
struct
urequest
setup
;
struct
uinstance
*
device
;
int
timeout
=
100
;
/* parameter check */
RT_ASSERT
(
intf
!=
RT_NULL
);
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
device
=
intf
->
device
;
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_INTERFACE
;
setup
.
request
=
USB_REQ_SET_PROTOCOL
;
setup
.
index
=
0
;
setup
.
length
=
0
;
setup
.
value
=
protocol
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
RT_NULL
,
0
,
timeout
)
==
0
)
return
RT_EOK
;
else
return
-
RT_FALSE
;
}
/**
* This function will do USB_REQ_GET_DESCRIPTOR request for the device instance
* to set feature of the hub port.
*
* @param intf the interface instance.
* @buffer the data buffer to save usb report descriptor.
* @param nbytes the size of buffer
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_hid_get_report_descriptor
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
,
rt_size_t
size
)
{
struct
urequest
setup
;
struct
uinstance
*
device
;
int
timeout
=
100
;
/* parameter check */
RT_ASSERT
(
intf
!=
RT_NULL
);
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
device
=
intf
->
device
;
setup
.
request_type
=
USB_REQ_TYPE_DIR_IN
|
USB_REQ_TYPE_STANDARD
|
USB_REQ_TYPE_INTERFACE
;
setup
.
request
=
USB_REQ_GET_DESCRIPTOR
;
setup
.
index
=
0
;
setup
.
length
=
size
;
setup
.
value
=
USB_DESC_TYPE_REPORT
<<
8
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
buffer
,
size
,
timeout
)
==
size
)
return
RT_EOK
;
else
return
-
RT_FALSE
;
}
/**
* This function will register specified hid protocal to protocal list
*
* @param protocal the specified protocal.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_hid_protocal_register
(
uprotocal_t
protocal
)
{
RT_ASSERT
(
protocal
!=
RT_NULL
);
if
(
protocal
==
RT_NULL
)
return
-
RT_ERROR
;
/* insert class driver into driver list */
rt_list_insert_after
(
&
_protocal_list
,
&
(
protocal
->
list
));
return
RT_EOK
;
}
/**
* This function is the callback function of hid's int endpoint, it is invoked when data comes.
*
* @param context the context of the callback function.
*
* @return none.
*/
static
void
rt_usbh_hid_callback
(
void
*
context
)
{
upipe_t
pipe
;
struct
uhid
*
hid
;
int
timeout
=
300
;
/* parameter check */
RT_ASSERT
(
context
!=
RT_NULL
);
pipe
=
(
upipe_t
)
context
;
hid
=
(
struct
uhid
*
)
pipe
->
intf
->
user_data
;
/* invoke protocal callback function */
hid
->
protocal
->
callback
((
void
*
)
hid
);
/* parameter check */
RT_ASSERT
(
pipe
->
intf
->
device
->
hcd
!=
RT_NULL
);
rt_usb_hcd_int_xfer
(
pipe
->
intf
->
device
->
hcd
,
pipe
,
hid
->
buffer
,
pipe
->
ep
.
wMaxPacketSize
,
timeout
);
}
/**
* This function will find specified hid protocal from protocal list
*
* @param pro_id the protocal id.
*
* @return the found protocal or RT_NULL if there is no this protocal.
*/
static
uprotocal_t
rt_usbh_hid_protocal_find
(
int
pro_id
)
{
struct
rt_list_node
*
node
;
/* try to find protocal object */
for
(
node
=
_protocal_list
.
next
;
node
!=
&
_protocal_list
;
node
=
node
->
next
)
{
uprotocal_t
protocal
=
(
uprotocal_t
)
rt_list_entry
(
node
,
struct
uprotocal
,
list
);
if
(
protocal
->
pro_id
==
pro_id
)
return
protocal
;
}
/* not found */
return
RT_NULL
;
}
/**
* This function will run hid class driver when usb device is detected and identified
* as a hid class device, it will continue the enumulate process.
*
* @param arg the argument.
*
* @return the error code, RT_EOK on successfully.
*/
static
rt_err_t
rt_usbh_hid_enable
(
void
*
arg
)
{
int
i
=
0
,
pro_id
;
uprotocal_t
protocal
;
struct
uhid
*
hid
;
struct
uintf
*
intf
=
(
struct
uintf
*
)
arg
;
int
timeout
=
100
;
upipe_t
pipe
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
pro_id
=
intf
->
intf_desc
->
bInterfaceProtocol
;
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"HID device enable, protocal id %d
\n
"
,
pro_id
));
protocal
=
rt_usbh_hid_protocal_find
(
pro_id
);
if
(
protocal
==
RT_NULL
)
{
rt_kprintf
(
"can't find hid protocal %d
\n
"
,
pro_id
);
intf
->
user_data
=
RT_NULL
;
return
-
RT_ERROR
;
}
hid
=
rt_malloc
(
sizeof
(
struct
uhid
));
RT_ASSERT
(
hid
!=
RT_NULL
);
/* initilize the data structure */
rt_memset
(
hid
,
0
,
sizeof
(
struct
uhid
));
intf
->
user_data
=
(
void
*
)
hid
;
hid
->
protocal
=
protocal
;
for
(
i
=
0
;
i
<
intf
->
intf_desc
->
bNumEndpoints
;
i
++
)
{
rt_err_t
ret
;
uep_desc_t
ep_desc
;
/* get endpoint descriptor */
rt_usbh_get_endpoint_descriptor
(
intf
->
intf_desc
,
i
,
&
ep_desc
);
if
(
ep_desc
==
RT_NULL
)
{
rt_kprintf
(
"rt_usbh_get_endpoint_descriptor error
\n
"
);
return
-
RT_ERROR
;
}
if
(
USB_EP_ATTR
(
ep_desc
->
bmAttributes
)
!=
USB_EP_ATTR_INT
)
continue
;
if
(
!
(
ep_desc
->
bEndpointAddress
&
USB_DIR_IN
))
continue
;
ret
=
rt_usb_hcd_alloc_pipe
(
intf
->
device
->
hcd
,
&
hid
->
pipe_in
,
intf
,
ep_desc
,
rt_usbh_hid_callback
);
if
(
ret
!=
RT_EOK
)
return
ret
;
}
/* initialize hid protocal */
hid
->
protocal
->
init
((
void
*
)
intf
);
pipe
=
hid
->
pipe_in
;
/* parameter check */
RT_ASSERT
(
pipe
->
intf
->
device
->
hcd
!=
RT_NULL
);
rt_usb_hcd_int_xfer
(
pipe
->
intf
->
device
->
hcd
,
hid
->
pipe_in
,
hid
->
buffer
,
hid
->
pipe_in
->
ep
.
wMaxPacketSize
,
timeout
);
return
RT_EOK
;
}
/**
* This function will be invoked when usb device plug out is detected and it would clean
* and release all hub class related resources.
*
* @param arg the argument.
*
* @return the error code, RT_EOK on successfully.
*/
static
rt_err_t
rt_usbh_hid_disable
(
void
*
arg
)
{
struct
uhid
*
hid
;
struct
uintf
*
intf
=
(
struct
uintf
*
)
arg
;
RT_ASSERT
(
intf
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_hid_disable
\n
"
));
hid
=
(
struct
uhid
*
)
intf
->
user_data
;
if
(
hid
!=
RT_NULL
)
{
if
(
hid
->
pipe_in
!=
RT_NULL
)
{
/* free the HID in pipe */
rt_usb_hcd_free_pipe
(
intf
->
device
->
hcd
,
hid
->
pipe_in
);
}
/* free the hid instance */
rt_free
(
hid
);
}
/* free the instance */
rt_free
(
intf
);
return
RT_EOK
;
}
/**
* This function will register hid class driver to the usb class driver manager.
* and it should be invoked in the usb system initialization.
*
* @return the error code, RT_EOK on successfully.
*/
ucd_t
rt_usbh_class_driver_hid
(
void
)
{
rt_list_init
(
&
_protocal_list
);
hid_driver
.
class_code
=
USB_CLASS_HID
;
hid_driver
.
enable
=
rt_usbh_hid_enable
;
hid_driver
.
disable
=
rt_usbh_hid_disable
;
return
&
hid_driver
;
}
#endif
components/drivers/usb/usbhost/class/hid.h
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : hid.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, 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
* 2011-12-12 Yi Qiu first version
*/
#ifndef __HID_H__
#define __HID_H__
#include <rtthread.h>
struct
uhid
{
upipe_t
pipe_in
;
rt_uint8_t
buffer
[
8
];
uprotocal_t
protocal
;
};
typedef
struct
uhid
uhid_t
;
#define USB_REQ_GET_REPORT 0x01
#define USB_REQ_GET_IDLE 0x02
#define USB_REQ_GET_PROTOCOL 0x03
#define USB_REQ_SET_REPORT 0x09
#define USB_REQ_SET_IDLE 0x0a
#define USB_REQ_SET_PROTOCOL 0x0b
#define USB_HID_KEYBOARD 1
#define USB_HID_MOUSE 2
rt_err_t
rt_usbh_hid_set_idle
(
struct
uintf
*
intf
,
int
duration
,
int
report_id
);
rt_err_t
rt_usbh_hid_get_report
(
struct
uintf
*
intf
,
rt_uint8_t
type
,
rt_uint8_t
id
,
rt_uint8_t
*
buffer
,
rt_size_t
size
);
rt_err_t
rt_usbh_hid_set_report
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
,
rt_size_t
size
);
rt_err_t
rt_usbh_hid_set_protocal
(
struct
uintf
*
intf
,
int
protocol
);
rt_err_t
rt_usbh_hid_get_report_descriptor
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
,
rt_size_t
size
);
rt_err_t
rt_usbh_hid_protocal_register
(
uprotocal_t
protocal
);
#endif
components/drivers/usb/usbhost/class/mass.c
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : mass.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, 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
* 2011-12-12 Yi Qiu first version
*/
#include <rtthread.h>
#include <drivers/usb_host.h>
#include "mass.h"
#ifdef RT_USBH_MSTORAGE
extern
rt_err_t
rt_udisk_run
(
struct
uintf
*
intf
);
extern
rt_err_t
rt_udisk_stop
(
struct
uintf
*
intf
);
static
struct
uclass_driver
storage_driver
;
/**
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
*
* @param intf the interface instance.
* @param max_lun the buffer to save max_lun.
*
* @return the error code, RT_EOK on successfully.
*/
static
rt_err_t
_pipe_check
(
struct
uintf
*
intf
,
upipe_t
pipe
)
{
struct
uinstance
*
device
;
rt_err_t
ret
;
ustor_t
stor
;
int
size
=
0
;
struct
ustorage_csw
csw
;
if
(
intf
==
RT_NULL
||
pipe
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
/* get usb device instance from the interface instance */
device
=
intf
->
device
;
/* get storage instance from the interface instance */
stor
=
(
ustor_t
)
intf
->
user_data
;
/* check pipe status */
if
(
pipe
->
status
==
UPIPE_STATUS_OK
)
return
RT_EOK
;
if
(
pipe
->
status
==
UPIPE_STATUS_ERROR
)
{
rt_kprintf
(
"pipe status error
\n
"
);
return
-
RT_EIO
;
}
/* clear the pipe stall status */
ret
=
rt_usbh_clear_feature
(
device
,
pipe
->
ep
.
bEndpointAddress
,
USB_FEATURE_ENDPOINT_HALT
);
if
(
ret
!=
RT_EOK
)
return
ret
;
rt_thread_delay
(
50
);
rt_kprintf
(
"pipes1 0x%x, 0x%x
\n
"
,
stor
->
pipe_in
,
stor
->
pipe_out
);
stor
->
pipe_in
->
status
=
UPIPE_STATUS_OK
;
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"clean storage in pipe stall
\n
"
));
/* it should receive csw after clear the stall feature */
size
=
rt_usb_hcd_bulk_xfer
(
stor
->
pipe_in
->
intf
->
device
->
hcd
,
stor
->
pipe_in
,
&
csw
,
SIZEOF_CSW
,
100
);
if
(
size
!=
SIZEOF_CSW
)
{
rt_kprintf
(
"receive the csw after stall failed
\n
"
);
return
-
RT_EIO
;
}
return
-
RT_ERROR
;
}
/**
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
*
* @param intf the interface instance.
* @param max_lun the buffer to save max_lun.
*
* @return the error code, RT_EOK on successfully.
*/
static
rt_err_t
rt_usb_bulk_only_xfer
(
struct
uintf
*
intf
,
ustorage_cbw_t
cmd
,
rt_uint8_t
*
buffer
,
int
timeout
)
{
rt_size_t
size
;
rt_err_t
ret
;
upipe_t
pipe
;
struct
ustorage_csw
csw
;
ustor_t
stor
;
RT_ASSERT
(
cmd
!=
RT_NULL
);
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
/* get storage instance from the interface instance */
stor
=
(
ustor_t
)
intf
->
user_data
;
do
{
/* send the cbw */
size
=
rt_usb_hcd_bulk_xfer
(
intf
->
device
->
hcd
,
stor
->
pipe_out
,
cmd
,
SIZEOF_CBW
,
timeout
);
if
(
size
!=
SIZEOF_CBW
)
{
rt_kprintf
(
"CBW size error
\n
"
);
return
-
RT_EIO
;
}
if
(
cmd
->
xfer_len
!=
0
)
{
pipe
=
(
cmd
->
dflags
==
CBWFLAGS_DIR_IN
)
?
stor
->
pipe_in
:
stor
->
pipe_out
;
size
=
rt_usb_hcd_bulk_xfer
(
intf
->
device
->
hcd
,
pipe
,
(
void
*
)
buffer
,
cmd
->
xfer_len
,
timeout
);
if
(
size
!=
cmd
->
xfer_len
)
{
rt_kprintf
(
"request size %d, transfer size %d
\n
"
,
cmd
->
xfer_len
,
size
);
break
;
}
}
/* receive the csw */
size
=
rt_usb_hcd_bulk_xfer
(
intf
->
device
->
hcd
,
stor
->
pipe_in
,
&
csw
,
SIZEOF_CSW
,
timeout
);
if
(
size
!=
SIZEOF_CSW
)
{
rt_kprintf
(
"csw size error
\n
"
);
return
-
RT_EIO
;
}
}
while
(
0
);
/* check in pipes status */
ret
=
_pipe_check
(
intf
,
stor
->
pipe_in
);
if
(
ret
!=
RT_EOK
)
{
rt_kprintf
(
"in pipe error
\n
"
);
return
ret
;
}
/* check out pipes status */
ret
=
_pipe_check
(
intf
,
stor
->
pipe_out
);
if
(
ret
!=
RT_EOK
)
{
rt_kprintf
(
"out pipe error
\n
"
);
return
ret
;
}
/* check csw status */
if
(
csw
.
signature
!=
CSW_SIGNATURE
||
csw
.
tag
!=
CBW_TAG_VALUE
)
{
rt_kprintf
(
"csw signature error
\n
"
);
return
-
RT_EIO
;
}
if
(
csw
.
status
!=
0
)
{
rt_kprintf
(
"csw status error
\n
"
);
return
-
RT_ERROR
;
}
return
RT_EOK
;
}
/**
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
*
* @param intf the interface instance.
* @param max_lun the buffer to save max_lun.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_storage_get_max_lun
(
struct
uintf
*
intf
,
rt_uint8_t
*
max_lun
)
{
struct
uinstance
*
device
;
struct
urequest
setup
;
int
timeout
=
100
;
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
/* parameter check */
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_get_max_lun
\n
"
));
/* get usb device instance from the interface instance */
device
=
intf
->
device
;
/* construct the request */
setup
.
request_type
=
USB_REQ_TYPE_DIR_IN
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_INTERFACE
;
setup
.
request
=
USBREQ_GET_MAX_LUN
;
setup
.
index
=
intf
->
intf_desc
->
bInterfaceNumber
;
setup
.
length
=
1
;
setup
.
value
=
0
;
/* do control transfer request */
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
max_lun
,
1
,
timeout
)
!=
1
)
return
-
RT_EIO
;
return
RT_EOK
;
}
/**
* This function will do USBREQ_MASS_STORAGE_RESET request for the usb interface instance.
*
* @param intf the interface instance.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_storage_reset
(
struct
uintf
*
intf
)
{
struct
urequest
setup
;
struct
uinstance
*
device
;
int
timeout
=
100
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_reset
\n
"
));
/* get usb device instance from the interface instance */
device
=
intf
->
device
;
/* construct the request */
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_INTERFACE
;
setup
.
request
=
USBREQ_MASS_STORAGE_RESET
;
setup
.
index
=
intf
->
intf_desc
->
bInterfaceNumber
;
setup
.
length
=
0
;
setup
.
value
=
0
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
RT_NULL
,
0
,
timeout
)
!=
0
)
return
-
RT_EIO
;
return
RT_EOK
;
}
/**
* This function will execute SCSI_READ_10 command to read data from the usb device.
*
* @param intf the interface instance.
* @param buffer the data buffer to save read data
* @param sector the start sector address to read.
* @param sector the sector count to read.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_storage_read10
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
,
rt_uint32_t
sector
,
rt_size_t
count
,
int
timeout
)
{
struct
ustorage_cbw
cmd
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"interface is not available
\n
"
);
return
-
RT_EIO
;
}
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_read10
\n
"
));
/* construct the command block wrapper */
rt_memset
(
&
cmd
,
0
,
sizeof
(
struct
ustorage_cbw
));
cmd
.
signature
=
CBW_SIGNATURE
;
cmd
.
tag
=
CBW_TAG_VALUE
;
cmd
.
xfer_len
=
SECTOR_SIZE
*
count
;
cmd
.
dflags
=
CBWFLAGS_DIR_IN
;
cmd
.
lun
=
0
;
cmd
.
cb_len
=
10
;
cmd
.
cb
[
0
]
=
SCSI_READ_10
;
cmd
.
cb
[
1
]
=
0
;
cmd
.
cb
[
2
]
=
(
rt_uint8_t
)(
sector
>>
24
);
cmd
.
cb
[
3
]
=
(
rt_uint8_t
)(
sector
>>
16
);
cmd
.
cb
[
4
]
=
(
rt_uint8_t
)(
sector
>>
8
);
cmd
.
cb
[
5
]
=
(
rt_uint8_t
)
sector
;
cmd
.
cb
[
6
]
=
0
;
cmd
.
cb
[
7
]
=
(
count
&
0xff00
)
>>
8
;
cmd
.
cb
[
8
]
=
(
rt_uint8_t
)
count
&
0xff
;
return
rt_usb_bulk_only_xfer
(
intf
,
&
cmd
,
buffer
,
timeout
);
}
/**
* This function will execute SCSI_WRITE_10 command to write data to the usb device.
*
* @param intf the interface instance.
* @param buffer the data buffer to save write data
* @param sector the start sector address to write.
* @param sector the sector count to write.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_storage_write10
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
,
rt_uint32_t
sector
,
rt_size_t
count
,
int
timeout
)
{
struct
ustorage_cbw
cmd
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_write10
\n
"
));
/* construct the command block wrapper */
rt_memset
(
&
cmd
,
0
,
sizeof
(
struct
ustorage_cbw
));
cmd
.
signature
=
CBW_SIGNATURE
;
cmd
.
tag
=
CBW_TAG_VALUE
;
cmd
.
xfer_len
=
SECTOR_SIZE
*
count
;
cmd
.
dflags
=
CBWFLAGS_DIR_OUT
;
cmd
.
lun
=
0
;
cmd
.
cb_len
=
10
;
cmd
.
cb
[
0
]
=
SCSI_WRITE_10
;
cmd
.
cb
[
1
]
=
0
;
cmd
.
cb
[
2
]
=
(
rt_uint8_t
)(
sector
>>
24
);
cmd
.
cb
[
3
]
=
(
rt_uint8_t
)(
sector
>>
16
);
cmd
.
cb
[
4
]
=
(
rt_uint8_t
)(
sector
>>
8
);
cmd
.
cb
[
5
]
=
(
rt_uint8_t
)
sector
;
cmd
.
cb
[
6
]
=
0
;
cmd
.
cb
[
7
]
=
(
count
&
0xff00
)
>>
8
;
cmd
.
cb
[
8
]
=
(
rt_uint8_t
)
count
&
0xff
;
return
rt_usb_bulk_only_xfer
(
intf
,
&
cmd
,
buffer
,
timeout
);
}
/**
* This function will execute SCSI_REQUEST_SENSE command to get sense data.
*
* @param intf the interface instance.
* @param buffer the data buffer to save sense data
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_storage_request_sense
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
)
{
struct
ustorage_cbw
cmd
;
int
timeout
=
200
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_request_sense
\n
"
));
/* construct the command block wrapper */
rt_memset
(
&
cmd
,
0
,
sizeof
(
struct
ustorage_cbw
));
cmd
.
signature
=
CBW_SIGNATURE
;
cmd
.
tag
=
CBW_TAG_VALUE
;
cmd
.
xfer_len
=
18
;
cmd
.
dflags
=
CBWFLAGS_DIR_IN
;
cmd
.
lun
=
0
;
cmd
.
cb_len
=
6
;
cmd
.
cb
[
0
]
=
SCSI_REQUEST_SENSE
;
cmd
.
cb
[
4
]
=
18
;
return
rt_usb_bulk_only_xfer
(
intf
,
&
cmd
,
buffer
,
timeout
);
}
/**
* This function will execute SCSI_TEST_UNIT_READY command to get unit ready status.
*
* @param intf the interface instance.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_storage_test_unit_ready
(
struct
uintf
*
intf
)
{
struct
ustorage_cbw
cmd
;
int
timeout
=
200
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_test_unit_ready
\n
"
));
/* construct the command block wrapper */
rt_memset
(
&
cmd
,
0
,
sizeof
(
struct
ustorage_cbw
));
cmd
.
signature
=
CBW_SIGNATURE
;
cmd
.
tag
=
CBW_TAG_VALUE
;
cmd
.
xfer_len
=
0
;
cmd
.
dflags
=
CBWFLAGS_DIR_OUT
;
cmd
.
lun
=
0
;
cmd
.
cb_len
=
12
;
cmd
.
cb
[
0
]
=
SCSI_TEST_UNIT_READY
;
return
rt_usb_bulk_only_xfer
(
intf
,
&
cmd
,
RT_NULL
,
timeout
);
}
/**
* This function will execute SCSI_INQUIRY_CMD command to get inquiry data.
*
* @param intf the interface instance.
* @param buffer the data buffer to save inquiry data
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_storage_inquiry
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
)
{
struct
ustorage_cbw
cmd
;
int
timeout
=
200
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_inquiry
\n
"
));
/* construct the command block wrapper */
rt_memset
(
&
cmd
,
0
,
sizeof
(
struct
ustorage_cbw
));
cmd
.
signature
=
CBW_SIGNATURE
;
cmd
.
tag
=
CBW_TAG_VALUE
;
cmd
.
xfer_len
=
36
;
cmd
.
dflags
=
CBWFLAGS_DIR_IN
;
cmd
.
lun
=
0
;
cmd
.
cb_len
=
12
;
cmd
.
cb
[
0
]
=
SCSI_INQUIRY_CMD
;
cmd
.
cb
[
4
]
=
36
;
return
rt_usb_bulk_only_xfer
(
intf
,
&
cmd
,
buffer
,
timeout
);
}
/**
* This function will execute SCSI_READ_CAPACITY command to get capacity data.
*
* @param intf the interface instance.
* @param buffer the data buffer to save capacity data
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_usbh_storage_get_capacity
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
)
{
struct
ustorage_cbw
cmd
;
int
timeout
=
200
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_get_capacity
\n
"
));
/* construct the command block wrapper */
rt_memset
(
&
cmd
,
0
,
sizeof
(
struct
ustorage_cbw
));
cmd
.
signature
=
CBW_SIGNATURE
;
cmd
.
tag
=
CBW_TAG_VALUE
;
cmd
.
xfer_len
=
8
;
cmd
.
dflags
=
CBWFLAGS_DIR_IN
;
cmd
.
lun
=
0
;
cmd
.
cb_len
=
12
;
cmd
.
cb
[
0
]
=
SCSI_READ_CAPACITY
;
return
rt_usb_bulk_only_xfer
(
intf
,
&
cmd
,
buffer
,
timeout
);
}
/**
* This function will run mass storage class driver when usb device is detected
* and identified as a mass storage class device, it will continue to do the enumulate
* process.
*
* @param arg the argument.
*
* @return the error code, RT_EOK on successfully.
*/
static
rt_err_t
rt_usbh_storage_enable
(
void
*
arg
)
{
int
i
=
0
;
rt_err_t
ret
;
ustor_t
stor
;
struct
uintf
*
intf
=
(
struct
uintf
*
)
arg
;
/* parameter check */
if
(
intf
==
RT_NULL
)
{
rt_kprintf
(
"the interface is not available
\n
"
);
return
-
RT_EIO
;
}
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"subclass %d, protocal %d
\n
"
,
intf
->
intf_desc
->
bInterfaceSubClass
,
intf
->
intf_desc
->
bInterfaceProtocol
));
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_run
\n
"
));
/* only support SCSI subclass and bulk only protocal */
stor
=
rt_malloc
(
sizeof
(
struct
ustor
));
RT_ASSERT
(
stor
!=
RT_NULL
);
/* initilize the data structure */
rt_memset
(
stor
,
0
,
sizeof
(
struct
ustor
));
intf
->
user_data
=
(
void
*
)
stor
;
for
(
i
=
0
;
i
<
intf
->
intf_desc
->
bNumEndpoints
;
i
++
)
{
uep_desc_t
ep_desc
;
/* get endpoint descriptor from interface descriptor */
rt_usbh_get_endpoint_descriptor
(
intf
->
intf_desc
,
i
,
&
ep_desc
);
if
(
ep_desc
==
RT_NULL
)
{
rt_kprintf
(
"rt_usb_get_endpoint_descriptor error
\n
"
);
return
-
RT_ERROR
;
}
/* the endpoint type of mass storage class should be BULK */
if
((
ep_desc
->
bmAttributes
&
USB_EP_ATTR_TYPE_MASK
)
!=
USB_EP_ATTR_BULK
)
continue
;
/* allocate pipes according to the endpoint type */
if
(
ep_desc
->
bEndpointAddress
&
USB_DIR_IN
)
{
/* alloc an in pipe for the storage instance */
ret
=
rt_usb_hcd_alloc_pipe
(
intf
->
device
->
hcd
,
&
stor
->
pipe_in
,
intf
,
ep_desc
,
RT_NULL
);
if
(
ret
!=
RT_EOK
)
return
ret
;
}
else
{
/* alloc an output pipe for the storage instance */
ret
=
rt_usb_hcd_alloc_pipe
(
intf
->
device
->
hcd
,
&
stor
->
pipe_out
,
intf
,
ep_desc
,
RT_NULL
);
if
(
ret
!=
RT_EOK
)
return
ret
;
}
}
/* check pipes infomation */
if
(
stor
->
pipe_in
==
RT_NULL
||
stor
->
pipe_out
==
RT_NULL
)
{
rt_kprintf
(
"pipe error, unsupported device
\n
"
);
return
-
RT_ERROR
;
}
/* should implement as callback */
ret
=
rt_udisk_run
(
intf
);
if
(
ret
!=
RT_EOK
)
return
ret
;
return
RT_EOK
;
}
/**
* This function will be invoked when usb device plug out is detected and it would clean
* and release all mass storage class related resources.
*
* @param arg the argument.
*
* @return the error code, RT_EOK on successfully.
*/
static
rt_err_t
rt_usbh_storage_disable
(
void
*
arg
)
{
ustor_t
stor
;
struct
uintf
*
intf
=
(
struct
uintf
*
)
arg
;
/* parameter check */
RT_ASSERT
(
intf
!=
RT_NULL
);
RT_ASSERT
(
intf
->
user_data
!=
RT_NULL
);
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"rt_usbh_storage_stop
\n
"
));
/* get storage instance from interface instance */
stor
=
(
ustor_t
)
intf
->
user_data
;
rt_udisk_stop
(
intf
);
rt_kprintf
(
"in 0x%x, out 0x%x
\n
"
,
stor
->
pipe_in
,
stor
->
pipe_out
);
/* free in pipe */
if
(
stor
->
pipe_in
!=
RT_NULL
)
rt_usb_hcd_free_pipe
(
intf
->
device
->
hcd
,
stor
->
pipe_in
);
/* free out pipe */
if
(
stor
->
pipe_out
!=
RT_NULL
)
rt_usb_hcd_free_pipe
(
intf
->
device
->
hcd
,
stor
->
pipe_out
);
/* free storage instance */
if
(
stor
!=
RT_NULL
)
rt_free
(
stor
);
/* free interface instance */
if
(
intf
!=
RT_NULL
)
rt_free
(
intf
);
return
RT_EOK
;
}
/**
* This function will register mass storage class driver to the usb class driver manager.
* and it should be invoked in the usb system initialization.
*
* @return the error code, RT_EOK on successfully.
*/
ucd_t
rt_usbh_class_driver_storage
(
void
)
{
storage_driver
.
class_code
=
USB_CLASS_MASS_STORAGE
;
storage_driver
.
enable
=
rt_usbh_storage_enable
;
storage_driver
.
disable
=
rt_usbh_storage_disable
;
return
&
storage_driver
;
}
#endif
components/drivers/usb/usbhost/class/mass.h
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : mass.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, 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
* 2011-12-12 Yi Qiu first version
*/
#ifndef __MASS_H__
#define __MASS_H__
#include <rtthread.h>
#include "dfs_fs.h"
#define MAX_PARTITION_COUNT 4
#define SECTOR_SIZE 512
struct
ustor_data
{
struct
dfs_partition
part
;
struct
uintf
*
intf
;
int
udisk_id
;
const
char
path
;
};
struct
ustor
{
upipe_t
pipe_in
;
upipe_t
pipe_out
;
rt_uint32_t
capicity
[
2
];
struct
rt_device
dev
[
MAX_PARTITION_COUNT
];
rt_uint8_t
dev_cnt
;
};
typedef
struct
ustor
*
ustor_t
;
rt_err_t
rt_usbh_storage_get_max_lun
(
struct
uintf
*
intf
,
rt_uint8_t
*
max_lun
);
rt_err_t
rt_usbh_storage_reset
(
struct
uintf
*
intf
);
rt_err_t
rt_usbh_storage_read10
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
,
rt_uint32_t
sector
,
rt_size_t
count
,
int
timeout
);
rt_err_t
rt_usbh_storage_write10
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
,
rt_uint32_t
sector
,
rt_size_t
count
,
int
timeout
);
rt_err_t
rt_usbh_storage_request_sense
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
);
rt_err_t
rt_usbh_storage_test_unit_ready
(
struct
uintf
*
intf
);
rt_err_t
rt_usbh_storage_inquiry
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
);
rt_err_t
rt_usbh_storage_get_capacity
(
struct
uintf
*
intf
,
rt_uint8_t
*
buffer
);
#endif
components/drivers/usb/usbhost/class/udisk.c
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : udisk.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, 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
* 2011-12-12 Yi Qiu first version
*/
#include <rtthread.h>
#include <dfs_fs.h>
#include <drivers/usb_host.h>
#include "mass.h"
#ifdef RT_USBH_MSTORAGE
#define UDISK_MAX_COUNT 8
static
rt_uint8_t
_udisk_idset
=
0
;
static
int
udisk_get_id
(
void
)
{
int
i
;
for
(
i
=
0
;
i
<
UDISK_MAX_COUNT
;
i
++
)
{
if
((
_udisk_idset
&
(
1
<<
i
))
!=
0
)
continue
;
else
break
;
}
/* it should not happen */
if
(
i
==
UDISK_MAX_COUNT
)
RT_ASSERT
(
0
);
_udisk_idset
|=
(
1
<<
i
);
return
i
;
}
static
void
udisk_free_id
(
int
id
)
{
RT_ASSERT
(
id
<
UDISK_MAX_COUNT
)
_udisk_idset
&=
~
(
1
<<
id
);
}
/**
* This function will initialize the udisk device
*
* @param dev the pointer of device driver structure
*
* @return RT_EOK
*/
static
rt_err_t
rt_udisk_init
(
rt_device_t
dev
)
{
return
RT_EOK
;
}
/**
* This function will read some data from a device.
*
* @param dev the pointer of device driver structure
* @param pos the position of reading
* @param buffer the data buffer to save read data
* @param size the size of buffer
*
* @return the actually read size on successful, otherwise negative returned.
*/
static
rt_size_t
rt_udisk_read
(
rt_device_t
dev
,
rt_off_t
pos
,
void
*
buffer
,
rt_size_t
size
)
{
rt_err_t
ret
;
struct
uintf
*
intf
;
struct
ustor_data
*
data
;
int
timeout
=
500
;
/* check parameter */
RT_ASSERT
(
dev
!=
RT_NULL
);
RT_ASSERT
(
buffer
!=
RT_NULL
);
if
(
size
>
4096
)
timeout
=
800
;
data
=
(
struct
ustor_data
*
)
dev
->
user_data
;
intf
=
data
->
intf
;
ret
=
rt_usbh_storage_read10
(
intf
,
(
rt_uint8_t
*
)
buffer
,
pos
,
size
,
timeout
);
if
(
ret
!=
RT_EOK
)
{
rt_kprintf
(
"usb mass_storage read failed
\n
"
);
return
0
;
}
return
size
;
}
/**
* This function will write some data to a device.
*
* @param dev the pointer of device driver structure
* @param pos the position of written
* @param buffer the data buffer to be written to device
* @param size the size of buffer
*
* @return the actually written size on successful, otherwise negative returned.
*/
static
rt_size_t
rt_udisk_write
(
rt_device_t
dev
,
rt_off_t
pos
,
const
void
*
buffer
,
rt_size_t
size
)
{
rt_err_t
ret
;
struct
uintf
*
intf
;
struct
ustor_data
*
data
;
int
timeout
=
500
;
/* check parameter */
RT_ASSERT
(
dev
!=
RT_NULL
);
RT_ASSERT
(
buffer
!=
RT_NULL
);
if
(
size
*
SECTOR_SIZE
>
4096
)
timeout
=
800
;
data
=
(
struct
ustor_data
*
)
dev
->
user_data
;
intf
=
data
->
intf
;
ret
=
rt_usbh_storage_write10
(
intf
,
(
rt_uint8_t
*
)
buffer
,
pos
,
size
,
timeout
);
if
(
ret
!=
RT_EOK
)
{
rt_kprintf
(
"usb mass_storage write %d sector failed
\n
"
,
size
);
return
0
;
}
return
size
;
}
/**
* This function will execute SCSI_INQUIRY_CMD command to get inquiry data.
*
* @param intf the interface instance.
* @param buffer the data buffer to save inquiry data
*
* @return the error code, RT_EOK on successfully.
*/
static
rt_err_t
rt_udisk_control
(
rt_device_t
dev
,
rt_uint8_t
cmd
,
void
*
args
)
{
ustor_t
stor
;
struct
ustor_data
*
data
;
/* check parameter */
RT_ASSERT
(
dev
!=
RT_NULL
);
data
=
(
struct
ustor_data
*
)
dev
->
user_data
;
stor
=
(
ustor_t
)
data
->
intf
->
user_data
;
if
(
cmd
==
RT_DEVICE_CTRL_BLK_GETGEOME
)
{
struct
rt_device_blk_geometry
*
geometry
;
geometry
=
(
struct
rt_device_blk_geometry
*
)
args
;
if
(
geometry
==
RT_NULL
)
return
-
RT_ERROR
;
geometry
->
bytes_per_sector
=
SECTOR_SIZE
;
geometry
->
block_size
=
stor
->
capicity
[
1
];
geometry
->
sector_count
=
stor
->
capicity
[
0
];
}
return
RT_EOK
;
}
/**
* This function will run udisk driver when usb disk is detected.
*
* @param intf the usb interface instance.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_udisk_run
(
struct
uintf
*
intf
)
{
int
i
=
0
;
rt_err_t
ret
;
char
dname
[
4
];
char
sname
[
8
];
rt_uint8_t
max_lun
,
*
sector
,
sense
[
18
],
inquiry
[
36
];
struct
dfs_partition
part
[
MAX_PARTITION_COUNT
];
ustor_t
stor
;
/* check parameter */
RT_ASSERT
(
intf
!=
RT_NULL
);
/* set interface */
// ret = rt_usbh_set_interface(intf->device, intf->intf_desc->bInterfaceNumber);
// if(ret != RT_EOK)
// rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
/* reset mass storage class device */
ret
=
rt_usbh_storage_reset
(
intf
);
if
(
ret
!=
RT_EOK
)
return
ret
;
stor
=
(
ustor_t
)
intf
->
user_data
;
/* get max logic unit number */
ret
=
rt_usbh_storage_get_max_lun
(
intf
,
&
max_lun
);
if
(
ret
!=
RT_EOK
)
rt_usbh_clear_feature
(
intf
->
device
,
0
,
USB_FEATURE_ENDPOINT_HALT
);
/* reset pipe in endpoint */
ret
=
rt_usbh_clear_feature
(
intf
->
device
,
stor
->
pipe_in
->
ep
.
bEndpointAddress
,
USB_FEATURE_ENDPOINT_HALT
);
if
(
ret
!=
RT_EOK
)
return
ret
;
/* reset pipe out endpoint */
ret
=
rt_usbh_clear_feature
(
intf
->
device
,
stor
->
pipe_out
->
ep
.
bEndpointAddress
,
USB_FEATURE_ENDPOINT_HALT
);
if
(
ret
!=
RT_EOK
)
return
ret
;
while
((
ret
=
rt_usbh_storage_inquiry
(
intf
,
inquiry
))
!=
RT_EOK
)
{
if
(
ret
==
-
RT_EIO
)
return
ret
;
rt_thread_delay
(
5
);
if
(
i
++
<
10
)
continue
;
rt_kprintf
(
"rt_usbh_storage_inquiry error
\n
"
);
return
-
RT_ERROR
;
}
i
=
0
;
/* wait device ready */
while
((
ret
=
rt_usbh_storage_test_unit_ready
(
intf
))
!=
RT_EOK
)
{
if
(
ret
==
-
RT_EIO
)
return
ret
;
ret
=
rt_usbh_storage_request_sense
(
intf
,
sense
);
if
(
ret
==
-
RT_EIO
)
return
ret
;
rt_thread_delay
(
10
);
if
(
i
++
<
10
)
continue
;
rt_kprintf
(
"rt_usbh_storage_test_unit_ready error
\n
"
);
return
-
RT_ERROR
;
}
i
=
0
;
rt_memset
(
stor
->
capicity
,
0
,
sizeof
(
stor
->
capicity
));
/* get storage capacity */
while
((
ret
=
rt_usbh_storage_get_capacity
(
intf
,
(
rt_uint8_t
*
)
stor
->
capicity
))
!=
RT_EOK
)
{
if
(
ret
==
-
RT_EIO
)
return
ret
;
rt_thread_delay
(
50
);
if
(
i
++
<
10
)
continue
;
stor
->
capicity
[
0
]
=
2880
;
stor
->
capicity
[
1
]
=
0x200
;
rt_kprintf
(
"rt_usbh_storage_get_capacity error
\n
"
);
break
;
}
stor
->
capicity
[
0
]
=
uswap_32
(
stor
->
capicity
[
0
]);
stor
->
capicity
[
1
]
=
uswap_32
(
stor
->
capicity
[
1
]);
stor
->
capicity
[
0
]
+=
1
;
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"capicity %d, block size %d
\n
"
,
stor
->
capicity
[
0
],
stor
->
capicity
[
1
]));
/* get the first sector to read partition table */
sector
=
(
rt_uint8_t
*
)
rt_malloc
(
SECTOR_SIZE
);
if
(
sector
==
RT_NULL
)
{
rt_kprintf
(
"allocate partition sector buffer failed
\n
"
);
return
-
RT_ERROR
;
}
rt_memset
(
sector
,
0
,
SECTOR_SIZE
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"read partition table
\n
"
));
/* get the partition table */
ret
=
rt_usbh_storage_read10
(
intf
,
sector
,
0
,
1
,
500
);
if
(
ret
!=
RT_EOK
)
{
rt_kprintf
(
"read parition table error
\n
"
);
rt_free
(
sector
);
return
-
RT_ERROR
;
}
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"finished reading partition
\n
"
));
for
(
i
=
0
;
i
<
MAX_PARTITION_COUNT
;
i
++
)
{
/* get the first partition */
ret
=
dfs_filesystem_get_partition
(
&
part
[
i
],
sector
,
i
);
if
(
ret
==
RT_EOK
)
{
struct
ustor_data
*
data
=
rt_malloc
(
sizeof
(
struct
ustor_data
));
rt_memset
(
data
,
0
,
sizeof
(
struct
ustor_data
));
data
->
intf
=
intf
;
data
->
udisk_id
=
udisk_get_id
();
data
->
part
.
lock
=
rt_sem_create
(
sname
,
1
,
RT_IPC_FLAG_FIFO
);
rt_snprintf
(
dname
,
6
,
"ud%d-%d"
,
data
->
udisk_id
,
i
);
rt_snprintf
(
sname
,
8
,
"sem_ud%d"
,
i
);
/* register sdcard device */
stor
->
dev
[
i
].
type
=
RT_Device_Class_Block
;
stor
->
dev
[
i
].
init
=
rt_udisk_init
;
stor
->
dev
[
i
].
read
=
rt_udisk_read
;
stor
->
dev
[
i
].
write
=
rt_udisk_write
;
stor
->
dev
[
i
].
control
=
rt_udisk_control
;
stor
->
dev
[
i
].
user_data
=
(
void
*
)
data
;
rt_device_register
(
&
stor
->
dev
[
i
],
dname
,
RT_DEVICE_FLAG_RDWR
|
RT_DEVICE_FLAG_REMOVABLE
|
RT_DEVICE_FLAG_STANDALONE
);
stor
->
dev_cnt
++
;
if
(
dfs_mount
(
stor
->
dev
[
i
].
parent
.
name
,
UDISK_MOUNTPOINT
,
"elm"
,
0
,
0
)
==
0
)
{
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"udisk part %d mount successfully
\n
"
,
i
));
}
else
{
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"udisk part %d mount failed
\n
"
,
i
));
}
}
else
{
if
(
i
==
0
)
{
struct
ustor_data
*
data
=
rt_malloc
(
sizeof
(
struct
ustor_data
));
rt_memset
(
data
,
0
,
sizeof
(
struct
ustor_data
));
data
->
udisk_id
=
udisk_get_id
();
/* there is no partition table */
data
->
part
.
offset
=
0
;
data
->
part
.
size
=
0
;
data
->
intf
=
intf
;
data
->
part
.
lock
=
rt_sem_create
(
"sem_ud"
,
1
,
RT_IPC_FLAG_FIFO
);
rt_snprintf
(
dname
,
7
,
"udisk%d"
,
data
->
udisk_id
);
/* register sdcard device */
stor
->
dev
[
0
].
type
=
RT_Device_Class_Block
;
stor
->
dev
[
0
].
init
=
rt_udisk_init
;
stor
->
dev
[
0
].
read
=
rt_udisk_read
;
stor
->
dev
[
0
].
write
=
rt_udisk_write
;
stor
->
dev
[
0
].
control
=
rt_udisk_control
;
stor
->
dev
[
0
].
user_data
=
(
void
*
)
data
;
rt_device_register
(
&
stor
->
dev
[
0
],
dname
,
RT_DEVICE_FLAG_RDWR
|
RT_DEVICE_FLAG_REMOVABLE
|
RT_DEVICE_FLAG_STANDALONE
);
stor
->
dev_cnt
++
;
if
(
dfs_mount
(
stor
->
dev
[
0
].
parent
.
name
,
UDISK_MOUNTPOINT
,
"elm"
,
0
,
0
)
==
0
)
{
rt_kprintf
(
"Mount FAT on Udisk successful.
\n
"
);
}
else
{
rt_kprintf
(
"Mount FAT on Udisk failed.
\n
"
);
}
}
break
;
}
}
rt_free
(
sector
);
return
RT_EOK
;
}
/**
* This function will be invoked when usb disk plug out is detected and it would clean
* and release all udisk related resources.
*
* @param intf the usb interface instance.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t
rt_udisk_stop
(
struct
uintf
*
intf
)
{
int
i
;
ustor_t
stor
;
struct
ustor_data
*
data
;
/* check parameter */
RT_ASSERT
(
intf
!=
RT_NULL
);
RT_ASSERT
(
intf
->
device
!=
RT_NULL
);
stor
=
(
ustor_t
)
intf
->
user_data
;
RT_ASSERT
(
stor
!=
RT_NULL
);
for
(
i
=
0
;
i
<
stor
->
dev_cnt
;
i
++
)
{
rt_device_t
dev
=
&
stor
->
dev
[
i
];
data
=
(
struct
ustor_data
*
)
dev
->
user_data
;
/* unmount filesystem */
dfs_unmount
(
UDISK_MOUNTPOINT
);
/* delete semaphore */
rt_sem_delete
(
data
->
part
.
lock
);
udisk_free_id
(
data
->
udisk_id
);
rt_free
(
data
);
/* unregister device */
rt_device_unregister
(
&
stor
->
dev
[
i
]);
}
return
RT_EOK
;
}
#endif
components/drivers/usb/usbhost/class/ukbd.c
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : ukbd.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, 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
* 2012-01-03 Yi Qiu first version
*/
#include <rtthread.h>
#include <drivers/usb_host.h>
#include "hid.h"
#if defined(RT_USBH_HID) && defined(RT_USBH_HID_KEYBOARD)
static
struct
uprotocal
kbd_protocal
;
static
rt_err_t
rt_usbh_hid_kbd_callback
(
void
*
arg
)
{
int
int1
,
int2
;
struct
uhid
*
hid
;
hid
=
(
struct
uhid
*
)
arg
;
int1
=
*
(
rt_uint32_t
*
)
hid
->
buffer
;
int2
=
*
(
rt_uint32_t
*
)(
&
hid
->
buffer
[
4
]);
if
(
int1
!=
0
||
int2
!=
0
)
{
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"key down 0x%x, 0x%x
\n
"
,
int1
,
int2
));
}
return
RT_EOK
;
}
static
rt_err_t
rt_usbh_hid_kbd_init
(
void
*
arg
)
{
struct
uintf
*
intf
=
(
struct
uintf
*
)
arg
;
RT_ASSERT
(
intf
!=
RT_NULL
);
rt_usbh_hid_set_protocal
(
intf
,
0
);
rt_usbh_hid_set_idle
(
intf
,
10
,
0
);
//RT_DEBUG_LOG(RT_DEBUG_USB, ("start usb keyboard\n"));
return
RT_EOK
;
}
/**
* This function will define the hid keyboard protocal, it will be register to the protocal list.
*
* @return the keyboard protocal structure.
*/
uprotocal_t
rt_usbh_hid_protocal_kbd
(
void
)
{
kbd_protocal
.
pro_id
=
USB_HID_KEYBOARD
;
kbd_protocal
.
init
=
rt_usbh_hid_kbd_init
;
kbd_protocal
.
callback
=
rt_usbh_hid_kbd_callback
;
return
&
kbd_protocal
;
}
#endif
components/drivers/usb/usbhost/class/umouse.c
0 → 100644
浏览文件 @
cc54e0a7
/*
* File : umouse.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2011, 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
* 2012-01-03 Yi Qiu first version
*/
#include <rtthread.h>
#include <drivers/usb_host.h>
#include "hid.h"
#ifdef RT_USING_RTGUI
#include <rtgui/event.h>
#include <rtgui/rtgui_server.h>
#include "drv_lcd.h"
#endif
#if defined(RT_USBH_HID) && defined(RT_USBH_HID_MOUSE)
static
struct
uprotocal
mouse_protocal
;
#ifdef RT_USING_RTGUI
#define LKEY_PRESS 0x01
#define RKEY_PRESS 0x02
#define MKEY_PRESS 0x04
#define MOUSE_SCALING 0x02
static
rt_bool_t
lkey_down
=
RT_FALSE
;
//static rt_bool_t rkey_down=RT_FALSE;
//static rt_bool_t mkey_down=RT_FALSE;
static
struct
rtgui_event_mouse
emouse
;
#endif
static
rt_err_t
rt_usbh_hid_mouse_callback
(
void
*
arg
)
{
struct
uhid
*
hid
;
#ifdef RT_USING_RTGUI
rt_uint16_t
xoffset
=
0
;
rt_uint16_t
yoffset
=
0
;
#endif
hid
=
(
struct
uhid
*
)
arg
;
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"hid 0x%x 0x%x
\n
"
,
*
(
rt_uint32_t
*
)
hid
->
buffer
,
*
(
rt_uint32_t
*
)(
&
hid
->
buffer
[
4
])));
#ifdef RT_USING_RTGUI
if
(
hid
->
buffer
[
1
]
!=
0
)
{
if
(
hid
->
buffer
[
1
]
>
127
)
{
xoffset
=
(
256
-
hid
->
buffer
[
1
])
*
MOUSE_SCALING
;
if
(
emouse
.
x
>
xoffset
)
{
emouse
.
x
-=
xoffset
;
}
else
{
emouse
.
x
=
0
;
}
}
else
{
xoffset
=
(
hid
->
buffer
[
1
])
*
MOUSE_SCALING
;
if
((
emouse
.
x
+
xoffset
)
<
480
)
{
emouse
.
x
+=
xoffset
;
}
else
{
emouse
.
x
=
480
;
}
}
}
if
(
hid
->
buffer
[
2
]
!=
0
)
{
if
(
hid
->
buffer
[
2
]
>
127
)
{
yoffset
=
(
256
-
hid
->
buffer
[
2
])
*
MOUSE_SCALING
;
if
(
emouse
.
y
>
yoffset
)
{
emouse
.
y
-=
yoffset
;
}
else
{
emouse
.
y
=
0
;
}
}
else
{
yoffset
=
hid
->
buffer
[
2
]
*
MOUSE_SCALING
;
if
(
emouse
.
y
+
yoffset
<
272
)
{
emouse
.
y
+=
yoffset
;
}
else
{
emouse
.
y
=
272
;
}
}
}
if
(
xoffset
!=
0
||
yoffset
!=
0
)
{
cursor_set_position
(
emouse
.
x
,
emouse
.
y
);
}
if
(
hid
->
buffer
[
0
]
&
LKEY_PRESS
)
{
if
(
lkey_down
==
RT_FALSE
){
// rt_kprintf("mouse left key press down\n");
emouse
.
button
=
(
RTGUI_MOUSE_BUTTON_LEFT
|
RTGUI_MOUSE_BUTTON_DOWN
);
rtgui_server_post_event
(
&
emouse
.
parent
,
sizeof
(
struct
rtgui_event_mouse
));
lkey_down
=
RT_TRUE
;
}
}
else
if
(
lkey_down
==
RT_TRUE
)
{
// rt_kprintf("mouse left key press up\n");
emouse
.
button
=
(
RTGUI_MOUSE_BUTTON_LEFT
|
RTGUI_MOUSE_BUTTON_UP
);
rtgui_server_post_event
(
&
emouse
.
parent
,
sizeof
(
struct
rtgui_event_mouse
));
lkey_down
=
RT_FALSE
;
}
#endif
return
RT_EOK
;
}
static
rt_err_t
rt_usbh_hid_mouse_init
(
void
*
arg
)
{
struct
uintf
*
intf
=
(
struct
uintf
*
)
arg
;
RT_ASSERT
(
intf
!=
RT_NULL
);
rt_usbh_hid_set_protocal
(
intf
,
0
);
rt_usbh_hid_set_idle
(
intf
,
10
,
0
);
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"start usb mouse
\n
"
));
#ifdef RT_USING_RTGUI
RTGUI_EVENT_MOUSE_BUTTON_INIT
(
&
emouse
);
emouse
.
wid
=
RT_NULL
;
cursor_display
(
RT_TRUE
);
#endif
return
RT_EOK
;
}
/**
* This function will define the hid mouse protocal, it will be register to the protocal list.
*
* @return the keyboard protocal structure.
*/
uprotocal_t
rt_usbh_hid_protocal_mouse
(
void
)
{
mouse_protocal
.
pro_id
=
USB_HID_MOUSE
;
mouse_protocal
.
init
=
rt_usbh_hid_mouse_init
;
mouse_protocal
.
callback
=
rt_usbh_hid_mouse_callback
;
return
&
mouse_protocal
;
}
#endif
components/drivers/usb/usbhost/core/core.c
浏览文件 @
cc54e0a7
...
...
@@ -234,10 +234,10 @@ rt_err_t rt_usbh_detach_instance(uinst_t device)
}
/**
* This function will do USB_REQ_GET_DESCRIPTO'
r
equest for the usb device instance,
* This function will do USB_REQ_GET_DESCRIPTO'
bR
equest for the usb device instance,
*
* @param device the usb device instance.
* @param type the type of descriptor
r
equest.
* @param type the type of descriptor
bR
equest.
* @param buffer the data buffer to save requested data
* @param nbytes the size of buffer
*
...
...
@@ -253,10 +253,10 @@ rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer,
setup
.
request_type
=
USB_REQ_TYPE_DIR_IN
|
USB_REQ_TYPE_STANDARD
|
USB_REQ_TYPE_DEVICE
;
setup
.
r
equest
=
USB_REQ_GET_DESCRIPTOR
;
setup
.
i
ndex
=
0
;
setup
.
l
ength
=
nbytes
;
setup
.
v
alue
=
type
<<
8
;
setup
.
bR
equest
=
USB_REQ_GET_DESCRIPTOR
;
setup
.
wI
ndex
=
0
;
setup
.
wL
ength
=
nbytes
;
setup
.
wV
alue
=
type
<<
8
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
buffer
,
nbytes
,
timeout
)
!=
nbytes
)
return
-
RT_EIO
;
...
...
@@ -281,10 +281,10 @@ rt_err_t rt_usbh_set_address(uinst_t device)
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_STANDARD
|
USB_REQ_TYPE_DEVICE
;
setup
.
r
equest
=
USB_REQ_SET_ADDRESS
;
setup
.
i
ndex
=
0
;
setup
.
l
ength
=
0
;
setup
.
v
alue
=
device
->
index
;
setup
.
bR
equest
=
USB_REQ_SET_ADDRESS
;
setup
.
wI
ndex
=
0
;
setup
.
wL
ength
=
0
;
setup
.
wV
alue
=
device
->
index
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
RT_NULL
,
0
,
timeout
)
!=
0
)
return
-
RT_EIO
;
...
...
@@ -314,10 +314,10 @@ rt_err_t rt_usbh_set_configure(uinst_t device, int config)
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_STANDARD
|
USB_REQ_TYPE_DEVICE
;
setup
.
r
equest
=
USB_REQ_SET_CONFIGURATION
;
setup
.
i
ndex
=
0
;
setup
.
l
ength
=
0
;
setup
.
v
alue
=
config
;
setup
.
bR
equest
=
USB_REQ_SET_CONFIGURATION
;
setup
.
wI
ndex
=
0
;
setup
.
wL
ength
=
0
;
setup
.
wV
alue
=
config
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
RT_NULL
,
0
,
timeout
)
!=
0
)
return
-
RT_EIO
;
...
...
@@ -343,10 +343,10 @@ rt_err_t rt_usbh_set_interface(uinst_t device, int intf)
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_STANDARD
|
USB_REQ_TYPE_INTERFACE
;
setup
.
r
equest
=
USB_REQ_SET_INTERFACE
;
setup
.
i
ndex
=
0
;
setup
.
l
ength
=
0
;
setup
.
v
alue
=
intf
;
setup
.
bR
equest
=
USB_REQ_SET_INTERFACE
;
setup
.
wI
ndex
=
0
;
setup
.
wL
ength
=
0
;
setup
.
wV
alue
=
intf
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
RT_NULL
,
0
,
timeout
)
!=
0
)
return
-
RT_EIO
;
...
...
@@ -372,10 +372,10 @@ rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature)
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_STANDARD
|
USB_REQ_TYPE_ENDPOINT
;
setup
.
r
equest
=
USB_REQ_CLEAR_FEATURE
;
setup
.
i
ndex
=
endpoint
;
setup
.
l
ength
=
0
;
setup
.
v
alue
=
feature
;
setup
.
bR
equest
=
USB_REQ_CLEAR_FEATURE
;
setup
.
wI
ndex
=
endpoint
;
setup
.
wL
ength
=
0
;
setup
.
wV
alue
=
feature
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
RT_NULL
,
0
,
timeout
)
!=
0
)
return
-
RT_EIO
;
...
...
components/drivers/usb/usbhost/core/driver.c
浏览文件 @
cc54e0a7
...
...
@@ -24,7 +24,7 @@
#include <rtthread.h>
#include <rtservice.h>
#include <
rtdevice
.h>
#include <
drivers/usb_host
.h>
static
rt_list_t
_driver_list
;
...
...
components/drivers/usb/usbhost/core/hub.c
浏览文件 @
cc54e0a7
...
...
@@ -31,7 +31,7 @@ static struct rt_messagequeue *usb_mq;
static
struct
uclass_driver
hub_driver
;
/**
* This function will do USB_REQ_GET_DESCRIPTOR
r
equest for the device instance
* This function will do USB_REQ_GET_DESCRIPTOR
bR
equest for the device instance
* to get usb hub descriptor.
*
* @param intf the interface instance.
...
...
@@ -51,10 +51,10 @@ rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer
setup
.
request_type
=
USB_REQ_TYPE_DIR_IN
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_DEVICE
;
setup
.
r
equest
=
USB_REQ_GET_DESCRIPTOR
;
setup
.
i
ndex
=
0
;
setup
.
l
ength
=
nbytes
;
setup
.
v
alue
=
USB_DESC_TYPE_HUB
<<
8
;
setup
.
bR
equest
=
USB_REQ_GET_DESCRIPTOR
;
setup
.
wI
ndex
=
0
;
setup
.
wL
ength
=
nbytes
;
setup
.
wV
alue
=
USB_DESC_TYPE_HUB
<<
8
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
buffer
,
nbytes
,
timeout
)
==
nbytes
)
return
RT_EOK
;
...
...
@@ -62,7 +62,7 @@ rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer
}
/**
* This function will do USB_REQ_GET_STATUS
r
equest for the device instance
* This function will do USB_REQ_GET_STATUS
bR
equest for the device instance
* to get usb hub status.
*
* @param intf the interface instance.
...
...
@@ -81,10 +81,10 @@ rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint8_t* buffer)
setup
.
request_type
=
USB_REQ_TYPE_DIR_IN
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_DEVICE
;
setup
.
r
equest
=
USB_REQ_GET_STATUS
;
setup
.
i
ndex
=
0
;
setup
.
l
ength
=
length
;
setup
.
v
alue
=
0
;
setup
.
bR
equest
=
USB_REQ_GET_STATUS
;
setup
.
wI
ndex
=
0
;
setup
.
wL
ength
=
length
;
setup
.
wV
alue
=
0
;
if
(
rt_usb_hcd_control_xfer
(
device
->
hcd
,
device
,
&
setup
,
buffer
,
length
,
timeout
)
==
length
)
return
RT_EOK
;
...
...
@@ -92,7 +92,7 @@ rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint8_t* buffer)
}
/**
* This function will do USB_REQ_GET_STATUS
r
equest for the device instance
* This function will do USB_REQ_GET_STATUS
bR
equest for the device instance
* to get hub port status.
*
* @param intf the interface instance.
...
...
@@ -121,10 +121,10 @@ rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port,
setup
.
request_type
=
USB_REQ_TYPE_DIR_IN
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_OTHER
;
setup
.
r
equest
=
USB_REQ_GET_STATUS
;
setup
.
i
ndex
=
port
;
setup
.
l
ength
=
4
;
setup
.
v
alue
=
0
;
setup
.
bR
equest
=
USB_REQ_GET_STATUS
;
setup
.
wI
ndex
=
port
;
setup
.
wL
ength
=
4
;
setup
.
wV
alue
=
0
;
if
(
rt_usb_hcd_control_xfer
(
hub
->
hcd
,
hub
->
self
,
&
setup
,
buffer
,
length
,
timeout
)
==
timeout
)
return
RT_EOK
;
...
...
@@ -132,7 +132,7 @@ rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port,
}
/**
* This function will do USB_REQ_CLEAR_FEATURE
r
equest for the device instance
* This function will do USB_REQ_CLEAR_FEATURE
bR
equest for the device instance
* to clear feature of the hub port.
*
* @param intf the interface instance.
...
...
@@ -160,10 +160,10 @@ rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port,
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_OTHER
;
setup
.
r
equest
=
USB_REQ_CLEAR_FEATURE
;
setup
.
i
ndex
=
port
;
setup
.
l
ength
=
0
;
setup
.
v
alue
=
feature
;
setup
.
bR
equest
=
USB_REQ_CLEAR_FEATURE
;
setup
.
wI
ndex
=
port
;
setup
.
wL
ength
=
0
;
setup
.
wV
alue
=
feature
;
if
(
rt_usb_hcd_control_xfer
(
hub
->
hcd
,
hub
->
self
,
&
setup
,
RT_NULL
,
0
,
timeout
)
==
0
)
return
RT_EOK
;
...
...
@@ -171,7 +171,7 @@ rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port,
}
/**
* This function will do USB_REQ_SET_FEATURE
r
equest for the device instance
* This function will do USB_REQ_SET_FEATURE
bR
equest for the device instance
* to set feature of the hub port.
*
* @param intf the interface instance.
...
...
@@ -199,10 +199,10 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
setup
.
request_type
=
USB_REQ_TYPE_DIR_OUT
|
USB_REQ_TYPE_CLASS
|
USB_REQ_TYPE_OTHER
;
setup
.
r
equest
=
USB_REQ_SET_FEATURE
;
setup
.
i
ndex
=
port
;
setup
.
l
ength
=
0
;
setup
.
v
alue
=
feature
;
setup
.
bR
equest
=
USB_REQ_SET_FEATURE
;
setup
.
wI
ndex
=
port
;
setup
.
wL
ength
=
0
;
setup
.
wV
alue
=
feature
;
if
(
rt_usb_hcd_control_xfer
(
hub
->
hcd
,
hub
->
self
,
&
setup
,
RT_NULL
,
0
,
timeout
)
==
0
)
return
RT_EOK
;
...
...
@@ -561,7 +561,7 @@ static void rt_usbh_hub_thread_entry(void* parameter)
if
(
rt_mq_recv
(
usb_mq
,
&
msg
,
sizeof
(
struct
uhost_msg
),
RT_WAITING_FOREVER
)
!=
RT_EOK
)
continue
;
RT_DEBUG_LOG
(
RT_DEBUG_USB
,
(
"msg type %d
\n
"
,
msg
.
type
));
//
RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
switch
(
msg
.
type
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录