Skip to content

Commit 7628f36

Browse files
authored
Merge pull request #4376 from LeeChunHei/hid_fix
修復usb host hid和umouse驅動
2 parents d718f64 + e03eaec commit 7628f36

File tree

4 files changed

+114
-72
lines changed

4 files changed

+114
-72
lines changed

components/drivers/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@ menu "Using USB"
642642
string "Udisk mount dir"
643643
default "/"
644644
endif
645+
config RT_USBH_HID
646+
bool "Enable HID Drivers"
647+
default n
648+
if RT_USBH_HID
649+
config RT_USBH_HID_MOUSE
650+
bool "Enable HID mouse protocol"
651+
default n
652+
endif
645653
endif
646654
config RT_USING_USB_DEVICE
647655
bool "Using USB device"

components/drivers/usb/usbhost/class/hid.c

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2011-12-12 Yi Qiu first version
9+
* 2021-02-23 Leslie Lee update with current usb api
910
*/
1011

1112
#include <rtthread.h>
@@ -26,7 +27,7 @@ static rt_list_t _protocal_list;
2627
*
2728
* @return the error code, RT_EOK on successfully.
2829
*/
29-
rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id)
30+
rt_err_t rt_usbh_hid_set_idle(struct uhintf* intf, int duration, int report_id)
3031
{
3132
struct urequest setup;
3233
struct uinstance* device;
@@ -40,14 +41,15 @@ rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id)
4041

4142
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
4243
USB_REQ_TYPE_INTERFACE;
43-
setup.request = USB_REQ_SET_IDLE;
44-
setup.index = 0;
45-
setup.length = 0;
46-
setup.value = (duration << 8 )| report_id;
47-
48-
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0,
49-
timeout) == 0) return RT_EOK;
50-
else return -RT_FALSE;
44+
setup.bRequest = USB_REQ_SET_IDLE;
45+
setup.wIndex = 0;
46+
setup.wLength = 0;
47+
setup.wValue = (duration << 8 )| report_id;
48+
49+
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
50+
return RT_EOK;
51+
else
52+
return -RT_FALSE;
5153
}
5254

5355
/**
@@ -59,7 +61,7 @@ rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id)
5961
*
6062
* @return the error code, RT_EOK on successfully.
6163
*/
62-
rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type,
64+
rt_err_t rt_usbh_hid_get_report(struct uhintf* intf, rt_uint8_t type,
6365
rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size)
6466
{
6567
struct urequest setup;
@@ -74,14 +76,24 @@ rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type,
7476

7577
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
7678
USB_REQ_TYPE_INTERFACE;
77-
setup.request = USB_REQ_GET_REPORT;
78-
setup.index = intf->intf_desc->bInterfaceNumber;
79-
setup.length = size;
80-
setup.value = (type << 8 ) + id;
81-
82-
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size,
83-
timeout) == size) return RT_EOK;
84-
else return -RT_FALSE;
79+
setup.bRequest = USB_REQ_GET_REPORT;
80+
setup.wIndex = intf->intf_desc->bInterfaceNumber;
81+
setup.wLength = size;
82+
setup.wValue = (type << 8 ) + id;
83+
84+
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
85+
{
86+
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, size, timeout) == size)
87+
{
88+
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
89+
{
90+
return RT_EOK;
91+
}
92+
}
93+
}
94+
else
95+
return -RT_FALSE;
96+
return -RT_FALSE;
8597
}
8698

8799
/**
@@ -93,7 +105,7 @@ rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type,
93105
*
94106
* @return the error code, RT_EOK on successfully.
95107
*/
96-
rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_t size)
108+
rt_err_t rt_usbh_hid_set_report(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size)
97109
{
98110
struct urequest setup;
99111
struct uinstance* device;
@@ -107,14 +119,15 @@ rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_
107119

108120
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
109121
USB_REQ_TYPE_INTERFACE;
110-
setup.request = USB_REQ_SET_REPORT;
111-
setup.index = intf->intf_desc->bInterfaceNumber;
112-
setup.length = size;
113-
setup.value = 0x02 << 8;
114-
115-
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size,
116-
timeout) == size) return RT_EOK;
117-
else return -RT_FALSE;
122+
setup.bRequest = USB_REQ_SET_REPORT;
123+
setup.wIndex = intf->intf_desc->bInterfaceNumber;
124+
setup.wLength = size;
125+
setup.wValue = 0x02 << 8;
126+
127+
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
128+
return RT_EOK;
129+
else
130+
return -RT_FALSE;
118131
}
119132

120133
/**
@@ -125,7 +138,7 @@ rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_
125138
*
126139
* @return the error code, RT_EOK on successfully.
127140
*/
128-
rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol)
141+
rt_err_t rt_usbh_hid_set_protocal(struct uhintf* intf, int protocol)
129142
{
130143
struct urequest setup;
131144
struct uinstance* device;
@@ -139,14 +152,15 @@ rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol)
139152

140153
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
141154
USB_REQ_TYPE_INTERFACE;
142-
setup.request = USB_REQ_SET_PROTOCOL;
143-
setup.index = 0;
144-
setup.length = 0;
145-
setup.value = protocol;
146-
147-
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0,
148-
timeout) == 0) return RT_EOK;
149-
else return -RT_FALSE;
155+
setup.bRequest = USB_REQ_SET_PROTOCOL;
156+
setup.wIndex = 0;
157+
setup.wLength = 0;
158+
setup.wValue = protocol;
159+
160+
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
161+
return RT_EOK;
162+
else
163+
return -RT_FALSE;
150164
}
151165

152166
/**
@@ -159,7 +173,7 @@ rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol)
159173
*
160174
* @return the error code, RT_EOK on successfully.
161175
*/
162-
rt_err_t rt_usbh_hid_get_report_descriptor(struct uintf* intf,
176+
rt_err_t rt_usbh_hid_get_report_descriptor(struct uhintf* intf,
163177
rt_uint8_t *buffer, rt_size_t size)
164178
{
165179
struct urequest setup;
@@ -174,14 +188,24 @@ rt_err_t rt_usbh_hid_get_report_descriptor(struct uintf* intf,
174188

175189
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD|
176190
USB_REQ_TYPE_INTERFACE;
177-
setup.request = USB_REQ_GET_DESCRIPTOR;
178-
setup.index = 0;
179-
setup.length = size;
180-
setup.value = USB_DESC_TYPE_REPORT << 8;
181-
182-
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size,
183-
timeout) == size) return RT_EOK;
184-
else return -RT_FALSE;
191+
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
192+
setup.wIndex = 0;
193+
setup.wLength = size;
194+
setup.wValue = USB_DESC_TYPE_REPORT << 8;
195+
196+
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
197+
{
198+
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, size, timeout) == size)
199+
{
200+
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
201+
{
202+
return RT_EOK;
203+
}
204+
}
205+
}
206+
else
207+
return -RT_FALSE;
208+
return -RT_FALSE;
185209
}
186210

187211
/**
@@ -220,16 +244,16 @@ static void rt_usbh_hid_callback(void* context)
220244
RT_ASSERT(context != RT_NULL);
221245

222246
pipe = (upipe_t)context;
223-
hid = (struct uhid*)pipe->intf->user_data;
247+
hid = (struct uhid*)((struct uhintf*)pipe->inst)->user_data;
224248

225249
/* invoke protocal callback function */
226250
hid->protocal->callback((void*)hid);
227251

228252
/* parameter check */
229-
RT_ASSERT(pipe->intf->device->hcd != RT_NULL);
253+
RT_ASSERT(((struct uhintf*)pipe->inst)->device->hcd != RT_NULL);
230254

231-
rt_usb_hcd_int_xfer(pipe->intf->device->hcd, pipe, hid->buffer,
232-
pipe->ep.wMaxPacketSize, timeout);
255+
rt_usb_hcd_pipe_xfer(((struct uhintf*)pipe->inst)->device->hcd, pipe,
256+
hid->buffer, pipe->ep.wMaxPacketSize, timeout);
233257
}
234258

235259
/**
@@ -268,9 +292,7 @@ static rt_err_t rt_usbh_hid_enable(void* arg)
268292
int i = 0, pro_id;
269293
uprotocal_t protocal;
270294
struct uhid* hid;
271-
struct uintf* intf = (struct uintf*)arg;
272-
int timeout = USB_TIMEOUT_BASIC;
273-
upipe_t pipe;
295+
struct uhintf* intf = (struct uhintf*)arg;
274296

275297
/* parameter check */
276298
if(intf == RT_NULL)
@@ -319,19 +341,13 @@ static rt_err_t rt_usbh_hid_enable(void* arg)
319341
if(!(ep_desc->bEndpointAddress & USB_DIR_IN)) continue;
320342

321343
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &hid->pipe_in,
322-
intf, ep_desc, rt_usbh_hid_callback);
344+
intf, ep_desc);
323345
if(ret != RT_EOK) return ret;
324346
}
325347

326348
/* initialize hid protocal */
327-
hid->protocal->init((void*)intf);
328-
pipe = hid->pipe_in;
349+
hid->protocal->init((void*)intf);
329350

330-
/* parameter check */
331-
RT_ASSERT(pipe->intf->device->hcd != RT_NULL);
332-
333-
rt_usb_hcd_int_xfer(pipe->intf->device->hcd, hid->pipe_in,
334-
hid->buffer, hid->pipe_in->ep.wMaxPacketSize, timeout);
335351
return RT_EOK;
336352
}
337353

@@ -346,7 +362,7 @@ static rt_err_t rt_usbh_hid_enable(void* arg)
346362
static rt_err_t rt_usbh_hid_disable(void* arg)
347363
{
348364
struct uhid* hid;
349-
struct uintf* intf = (struct uintf*)arg;
365+
struct uhintf* intf = (struct uhintf*)arg;
350366

351367
RT_ASSERT(intf != RT_NULL);
352368

@@ -364,9 +380,6 @@ static rt_err_t rt_usbh_hid_disable(void* arg)
364380
/* free the hid instance */
365381
rt_free(hid);
366382
}
367-
368-
/* free the instance */
369-
rt_free(intf);
370383

371384
return RT_EOK;
372385
}

components/drivers/usb/usbhost/class/hid.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ typedef struct uhid uhid_t;
3131
#define USB_HID_KEYBOARD 1
3232
#define USB_HID_MOUSE 2
3333

34-
rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id);
35-
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);
36-
rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_t size);
37-
rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol);
38-
rt_err_t rt_usbh_hid_get_report_descriptor(struct uintf* intf, rt_uint8_t *buffer, rt_size_t size);
34+
rt_err_t rt_usbh_hid_set_idle(struct uhintf* intf, int duration, int report_id);
35+
rt_err_t rt_usbh_hid_get_report(struct uhintf* intf, rt_uint8_t type, rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size);
36+
rt_err_t rt_usbh_hid_set_report(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size);
37+
rt_err_t rt_usbh_hid_set_protocal(struct uhintf* intf, int protocol);
38+
rt_err_t rt_usbh_hid_get_report_descriptor(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size);
3939
rt_err_t rt_usbh_hid_protocal_register(uprotocal_t protocal);
4040

41-
#endif
41+
#endif

components/drivers/usb/usbhost/class/umouse.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,36 @@ static rt_err_t rt_usbh_hid_mouse_callback(void* arg)
126126
return RT_EOK;
127127
}
128128

129+
rt_thread_t mouse_thread;
130+
void mouse_task(void* param)
131+
{
132+
struct uhintf* intf = (struct uhintf*)param;
133+
while (1)
134+
{
135+
if (rt_usb_hcd_pipe_xfer(intf->device->hcd, ((struct uhid*)intf->user_data)->pipe_in,
136+
((struct uhid*)intf->user_data)->buffer, ((struct uhid*)intf->user_data)->pipe_in->ep.wMaxPacketSize,
137+
USB_TIMEOUT_BASIC) == 0)
138+
{
139+
break;
140+
}
141+
142+
rt_usbh_hid_mouse_callback(intf->user_data);
143+
}
144+
}
145+
146+
129147
static rt_err_t rt_usbh_hid_mouse_init(void* arg)
130148
{
131-
struct uintf* intf = (struct uintf*)arg;
149+
struct uhintf* intf = (struct uhintf*)arg;
132150

133151
RT_ASSERT(intf != RT_NULL);
134152

135153
rt_usbh_hid_set_protocal(intf, 0);
136154

137-
rt_usbh_hid_set_idle(intf, 10, 0);
155+
rt_usbh_hid_set_idle(intf, 0, 0);
156+
157+
mouse_thread = rt_thread_create("mouse0", mouse_task, intf, 500, 8, 100);
158+
rt_thread_startup(mouse_thread);
138159

139160
RT_DEBUG_LOG(RT_DEBUG_USB, ("start usb mouse\n"));
140161
#ifdef RT_USING_RTGUI

0 commit comments

Comments
 (0)