1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2011-12-12     Yi Qiu      first version
9  * 2021-02-23     Leslie Lee  update with current usb api
10  */
11 
12 #include <rtthread.h>
13 #include <drivers/usb_host.h>
14 #include "hid.h"
15 
16 #ifdef RT_USBH_HID
17 
18 #define DBG_TAG           "usbhost.hid"
19 #define DBG_LVL           DBG_INFO
20 #include <rtdbg.h>
21 
22 static struct uclass_driver hid_driver;
23 static rt_list_t _protocal_list;
24 
25 /**
26  * This function will do USB_REQ_SET_IDLE request to set idle period to the usb hid device
27  *
28  * @param intf the interface instance.
29  * @duration the idle period of requesting data.
30  * @report_id the report id
31  *
32  * @return the error code, RT_EOK on successfully.
33 */
rt_usbh_hid_set_idle(struct uhintf * intf,int duration,int report_id)34 rt_err_t rt_usbh_hid_set_idle(struct uhintf* intf, int duration, int report_id)
35 {
36     struct urequest setup;
37     struct uinstance* device;
38     int timeout = USB_TIMEOUT_BASIC;
39 
40     /* parameter check */
41     RT_ASSERT(intf != RT_NULL);
42     RT_ASSERT(intf->device != RT_NULL);
43 
44     device = intf->device;
45 
46     setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
47         USB_REQ_TYPE_INTERFACE;
48     setup.bRequest = USB_REQ_SET_IDLE;
49     setup.wIndex = 0;
50     setup.wLength = 0;
51     setup.wValue = (duration << 8 )| report_id;
52 
53     if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
54         if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
55             return RT_EOK;
56 
57     return -RT_FALSE;
58 }
59 
60 /**
61  * This function will do USB_REQ_GET_REPORT request to get report from the usb hid device
62  *
63  * @param intf the interface instance.
64  * @buffer the data buffer to save usb report descriptor.
65  * @param nbytes the size of buffer
66  *
67  * @return the error code, RT_EOK on successfully.
68 */
rt_usbh_hid_get_report(struct uhintf * intf,rt_uint8_t type,rt_uint8_t id,rt_uint8_t * buffer,rt_size_t size)69 rt_err_t rt_usbh_hid_get_report(struct uhintf* intf, rt_uint8_t type,
70     rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size)
71 {
72     struct urequest setup;
73     struct uinstance* device;
74     int timeout = USB_TIMEOUT_BASIC;
75 
76     /* parameter check */
77     RT_ASSERT(intf != RT_NULL);
78     RT_ASSERT(intf->device != RT_NULL);
79 
80     device = intf->device;
81 
82     setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
83         USB_REQ_TYPE_INTERFACE;
84     setup.bRequest = USB_REQ_GET_REPORT;
85     setup.wIndex = intf->intf_desc->bInterfaceNumber;
86     setup.wLength = size;
87     setup.wValue = (type << 8 ) + id;
88 
89     if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
90     {
91         if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, size, timeout) == size)
92         {
93             if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
94             {
95                 return RT_EOK;
96             }
97         }
98     }
99     else
100         return -RT_FALSE;
101     return -RT_FALSE;
102 }
103 
104 /**
105  * This function will do USB_REQ_SET_REPORT request to set report to the usb hid device
106  *
107  * @param intf the interface instance.
108  * @buffer the data buffer to save usb report descriptor.
109  * @param nbytes the size of buffer
110  *
111  * @return the error code, RT_EOK on successfully.
112 */
rt_usbh_hid_set_report(struct uhintf * intf,rt_uint8_t * buffer,rt_size_t size)113 rt_err_t rt_usbh_hid_set_report(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size)
114 {
115     struct urequest setup;
116     struct uinstance* device;
117     int timeout = USB_TIMEOUT_BASIC;
118 
119     /* parameter check */
120     RT_ASSERT(intf != RT_NULL);
121     RT_ASSERT(intf->device != RT_NULL);
122 
123     device = intf->device;
124 
125     setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
126         USB_REQ_TYPE_INTERFACE;
127     setup.bRequest = USB_REQ_SET_REPORT;
128     setup.wIndex = intf->intf_desc->bInterfaceNumber;
129     setup.wLength = size;
130     setup.wValue = 0x02 << 8;
131 
132     if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
133         return RT_EOK;
134     else
135         return -RT_FALSE;
136 }
137 
138 /**
139  * This function will do USB_REQ_SET_PROTOCOL request to set protocal to the usb hid device.
140  *
141  * @param intf the interface instance.
142  * @param protocol the protocol id.
143  *
144  * @return the error code, RT_EOK on successfully.
145  */
rt_usbh_hid_set_protocal(struct uhintf * intf,int protocol)146 rt_err_t rt_usbh_hid_set_protocal(struct uhintf* intf, int protocol)
147 {
148     struct urequest setup;
149     struct uinstance* device;
150     int timeout = USB_TIMEOUT_BASIC;
151 
152     /* parameter check */
153     RT_ASSERT(intf != RT_NULL);
154     RT_ASSERT(intf->device != RT_NULL);
155 
156     device = intf->device;
157 
158     setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
159         USB_REQ_TYPE_INTERFACE;
160     setup.bRequest = USB_REQ_SET_PROTOCOL;
161     setup.wIndex = 0;
162     setup.wLength = 0;
163     setup.wValue = protocol;
164 
165     if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
166         return RT_EOK;
167     else
168         return -RT_FALSE;
169 }
170 
171 /**
172  * This function will do USB_REQ_GET_DESCRIPTOR request for the device instance
173  * to set feature of the hub port.
174  *
175  * @param intf the interface instance.
176  * @buffer the data buffer to save usb report descriptor.
177  * @param nbytes the size of buffer
178  *
179  * @return the error code, RT_EOK on successfully.
180  */
rt_usbh_hid_get_report_descriptor(struct uhintf * intf,rt_uint8_t * buffer,rt_size_t size)181 rt_err_t rt_usbh_hid_get_report_descriptor(struct uhintf* intf,
182     rt_uint8_t *buffer, rt_size_t size)
183 {
184     struct urequest setup;
185     struct uinstance* device;
186     int timeout = USB_TIMEOUT_BASIC;
187 
188     /* parameter check */
189     RT_ASSERT(intf != RT_NULL);
190     RT_ASSERT(intf->device != RT_NULL);
191 
192     device = intf->device;
193 
194     setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD|
195         USB_REQ_TYPE_INTERFACE;
196     setup.bRequest = USB_REQ_GET_DESCRIPTOR;
197     setup.wIndex = 0;
198     setup.wLength = size;
199     setup.wValue = USB_DESC_TYPE_REPORT << 8;
200 
201     if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
202     {
203         if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, size, timeout) == size)
204         {
205             if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
206             {
207                 return RT_EOK;
208             }
209         }
210     }
211     else
212         return -RT_FALSE;
213     return -RT_FALSE;
214 }
215 
216 /**
217  * This function will register specified hid protocal to protocal list
218  *
219  * @param protocal the specified protocal.
220  *
221  * @return the error code, RT_EOK on successfully.
222  */
rt_usbh_hid_protocal_register(uprotocal_t protocal)223 rt_err_t rt_usbh_hid_protocal_register(uprotocal_t protocal)
224 {
225     RT_ASSERT(protocal != RT_NULL);
226 
227     if (protocal == RT_NULL) return -RT_ERROR;
228 
229     /* insert class driver into driver list */
230     rt_list_insert_after(&_protocal_list, &(protocal->list));
231 
232     return RT_EOK;
233 }
234 
235 /**
236  * This function is the callback function of hid's int endpoint, it is invoked when data comes.
237  *
238  * @param context the context of the callback function.
239  *
240  * @return none.
241  */
rt_usbh_hid_callback(void * context)242 static void rt_usbh_hid_callback(void* context)
243 {
244     upipe_t pipe;
245     struct uhid* hid;
246     int timeout = USB_TIMEOUT_LONG;
247 
248     /* parameter check */
249     RT_ASSERT(context != RT_NULL);
250 
251     pipe = (upipe_t)context;
252     hid = (struct uhid*)((struct uhintf*)pipe->inst)->user_data;
253 
254     /* invoke protocal callback function */
255     hid->protocal->callback((void*)hid);
256 
257     /* parameter check */
258      RT_ASSERT(((struct uhintf*)pipe->inst)->device->hcd != RT_NULL);
259 
260     rt_usb_hcd_pipe_xfer(((struct uhintf*)pipe->inst)->device->hcd, pipe,
261         hid->buffer, pipe->ep.wMaxPacketSize, timeout);
262 }
263 
264 /**
265  * This function will find specified hid protocal from protocal list
266  *
267  * @param pro_id the protocal id.
268  *
269  * @return the found protocal or RT_NULL if there is no this protocal.
270  */
rt_usbh_hid_protocal_find(int pro_id)271 static uprotocal_t rt_usbh_hid_protocal_find(int pro_id)
272 {
273     struct rt_list_node *node;
274 
275     /* try to find protocal object */
276     for (node = _protocal_list.next; node != &_protocal_list; node = node->next)
277     {
278         uprotocal_t protocal =
279             (uprotocal_t)rt_list_entry(node, struct uprotocal, list);
280         if (protocal->pro_id == pro_id) return protocal;
281     }
282 
283     /* not found */
284     return RT_NULL;
285 }
286 
287 /**
288  * This function will run hid class driver when usb device is detected and identified
289  *  as a hid class device, it will continue the enumulate process.
290  *
291  * @param arg the argument.
292  *
293  * @return the error code, RT_EOK on successfully.
294  */
rt_usbh_hid_enable(void * arg)295 static rt_err_t rt_usbh_hid_enable(void* arg)
296 {
297     int i = 0, pro_id;
298     uprotocal_t protocal;
299     struct uhid* hid;
300     struct uhintf* intf = (struct uhintf*)arg;
301 
302     /* parameter check */
303     if(intf == RT_NULL)
304     {
305         rt_kprintf("the interface is not available\n");
306         return -RT_EIO;
307     }
308 
309     pro_id = intf->intf_desc->bInterfaceProtocol;
310 
311     LOG_D("HID device enable, protocal id %d", pro_id);
312 
313     protocal = rt_usbh_hid_protocal_find(pro_id);
314     if(protocal == RT_NULL)
315     {
316         rt_kprintf("can't find hid protocal %d\n", pro_id);
317         intf->user_data = RT_NULL;
318         return -RT_ERROR;
319     }
320 
321     hid = rt_malloc(sizeof(struct uhid));
322     RT_ASSERT(hid != RT_NULL);
323 
324     /* initilize the data structure */
325     rt_memset(hid, 0, sizeof(struct uhid));
326     intf->user_data = (void*)hid;
327     hid->protocal = protocal;
328 
329     for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
330     {
331         rt_err_t ret;
332         uep_desc_t ep_desc;
333 
334         /* get endpoint descriptor */
335         rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
336         if(ep_desc == RT_NULL)
337         {
338             rt_kprintf("rt_usbh_get_endpoint_descriptor error\n");
339             return -RT_ERROR;
340         }
341 
342         if(USB_EP_ATTR(ep_desc->bmAttributes) != USB_EP_ATTR_INT)
343             continue;
344 
345         if(!(ep_desc->bEndpointAddress & USB_DIR_IN)) continue;
346 
347         ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &hid->pipe_in,
348             intf->device, ep_desc);
349         if(ret != RT_EOK) return ret;
350     }
351 
352     /* initialize hid protocal */
353     hid->protocal->init((void*)intf);
354 
355     return RT_EOK;
356 }
357 
358 /**
359  * This function will be invoked when usb device plug out is detected and it would clean
360  * and release all hub class related resources.
361  *
362  * @param arg the argument.
363  *
364  * @return the error code, RT_EOK on successfully.
365  */
rt_usbh_hid_disable(void * arg)366 static rt_err_t rt_usbh_hid_disable(void* arg)
367 {
368     struct uhid* hid;
369     struct uhintf* intf = (struct uhintf*)arg;
370 
371     RT_ASSERT(intf != RT_NULL);
372 
373     LOG_D("rt_usbh_hid_disable");
374 
375     hid = (struct uhid*)intf->user_data;
376     if(hid != RT_NULL)
377     {
378         if(hid->pipe_in != RT_NULL)
379         {
380             /* free the HID in pipe */
381             rt_usb_hcd_free_pipe(intf->device->hcd, hid->pipe_in);
382         }
383 
384         /* free the hid instance */
385         rt_free(hid);
386     }
387 
388     return RT_EOK;
389 }
390 
391 /**
392  * This function will register hid class driver to the usb class driver manager.
393  * and it should be invoked in the usb system initialization.
394  *
395  * @return the error code, RT_EOK on successfully.
396  */
rt_usbh_class_driver_hid(void)397 ucd_t rt_usbh_class_driver_hid(void)
398 {
399     rt_list_init(&_protocal_list);
400 
401     hid_driver.class_code = USB_CLASS_HID;
402 
403     hid_driver.enable = rt_usbh_hid_enable;
404     hid_driver.disable = rt_usbh_hid_disable;
405 
406     return &hid_driver;
407 }
408 
409 #endif
410 
411