27#define USB_CTRL_TIMEOUT_MS 100
30static int get_free_utr_slot(
HID_DEV_T *hdev)
36 if(hdev->utr_list[i] ==
NULL)
64 if(!hdev || !hdev->iface)
67 iface = (IFACE_T *)hdev->iface;
69 ret = usbh_ctrl_xfer(iface->udev,
70 REQ_TYPE_IN | REQ_TYPE_STD_DEV | REQ_TYPE_TO_IFACE,
71 USB_REQ_GET_DESCRIPTOR,
72 (USB_DT_REPORT << 8) + 0,
75 desc_buf, &xfer_len, USB_CTRL_TIMEOUT_MS);
77 if((ret < 0) || (xfer_len == 0))
79 HID_DBGMSG(
"failed to get HID descriptor.\n");
102 uint8_t *data,
int len)
108 if(!hdev || !hdev->iface)
111 iface = (IFACE_T *)hdev->iface;
113 ret = usbh_ctrl_xfer(iface->udev,
114 REQ_TYPE_IN | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE,
116 rtp_id + (rtp_typ << 8),
119 data, &xfer_len, USB_CTRL_TIMEOUT_MS);
122 HID_DBGMSG(
"failed to get report!\n");
125 return (
int)xfer_len;
147 uint8_t *data,
int len)
153 if(!hdev || !hdev->iface)
156 iface = (IFACE_T *)hdev->iface;
158 ret = usbh_ctrl_xfer(iface->udev,
159 REQ_TYPE_OUT | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE,
161 rtp_id + (rtp_typ << 8),
164 data, &xfer_len, USB_CTRL_TIMEOUT_MS);
167 HID_DBGMSG(
"failed to set report!\n");
170 return (
int)xfer_len;
176static void led_ctrl_irq(UTR_T *utr)
179 utr->bIsTransferDone = 1;
182int32_t usbh_hid_set_report_non_blocking(
HID_DEV_T *hdev,
int rtp_typ,
int rtp_id,
183 uint8_t *data,
int len)
189 if(!hdev || !hdev->iface)
192 iface = (IFACE_T *)hdev->iface;
194 utr = hdev->rpd.utr_led;
197 utr = alloc_utr(iface->udev);
200 hdev->rpd.utr_led = utr;
204 if(utr->bIsTransferDone == 0)
208 utr->setup.bmRequestType = REQ_TYPE_OUT | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE;
210 utr->setup.wValue = rtp_id + (rtp_typ << 8);
211 utr->setup.wIndex = iface->if_num;
212 utr->setup.wLength = len;
216 utr->func = led_ctrl_irq;
217 utr->bIsTransferDone = 0;
219 status = iface->udev->hc_driver->ctrl_xfer(utr);
222 iface->udev->ep0.hw_pipe =
NULL;
248 if(!hdev || !hdev->iface)
251 iface = (IFACE_T *)hdev->iface;
253 ret = usbh_ctrl_xfer(iface->udev,
254 REQ_TYPE_IN | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE,
259 idle_rate, &xfer_len, USB_CTRL_TIMEOUT_MS);
261 if((ret < 0) || (xfer_len != 1))
263 HID_DBGMSG(
"failed to get idle rate! %d\n", ret);
286 uint16_t wValue = idle_rate;
289 if(!hdev || !hdev->iface)
292 iface = (IFACE_T *)hdev->iface;
294 ret = usbh_ctrl_xfer(iface->udev,
295 REQ_TYPE_OUT | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE,
297 rtp_id + (wValue << 8),
300 NULL, &xfer_len, USB_CTRL_TIMEOUT_MS);
304 HID_DBGMSG(
"failed to set idle rate! %d\n", ret);
328 if(!hdev || !hdev->iface)
331 iface = (IFACE_T *)hdev->iface;
333 ret = usbh_ctrl_xfer(iface->udev,
334 REQ_TYPE_IN | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE,
339 protocol, &xfer_len, USB_CTRL_TIMEOUT_MS);
341 if((ret < 0) || (xfer_len != 1))
343 HID_DBGMSG(
"failed to get idle rate! %d\n", ret);
367 if(!hdev || !hdev->iface)
370 iface = (IFACE_T *)hdev->iface;
372 ret = usbh_ctrl_xfer(iface->udev,
373 REQ_TYPE_OUT | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE,
378 NULL, &xfer_len, USB_CTRL_TIMEOUT_MS);
382 HID_DBGMSG(
"failed to set idle rate! %d\n", ret);
395static void hid_read_irq(UTR_T *utr)
406 HID_DBGMSG(
"hid_read_irq - has error: 0x%x\n", utr->status);
408 hdev->read_func(hdev, utr->ep->bEndpointAddress, utr->status, utr->buff, 0);
412 if(hdev->bSubClassCode == HID_SUBCLASS_BOOT_DEVICE)
414 if(hdev->bProtocolCode == HID_PROTOCOL_MOUSE)
415 hid_parse_mouse_reports(hdev, utr->buff, utr->xfer_len);
417 if(hdev->bProtocolCode == HID_PROTOCOL_KEYBOARD)
418 hid_parse_keyboard_reports(hdev, utr->buff, utr->xfer_len);
421 if(hdev->read_func && utr->xfer_len)
422 hdev->read_func(hdev, utr->ep->bEndpointAddress, utr->status, utr->buff, utr->xfer_len);
425 ret = usbh_int_xfer(utr);
428 HID_DBGMSG(
"hid_read_irq - failed to submit interrupt-in request (%d)", ret);
430 hdev->read_func(hdev, utr->ep->bEndpointAddress, ret, utr->buff, 0);
431 usbh_free_mem(utr->buff, utr->data_len);
439static void hid_write_irq(UTR_T *utr)
450 HID_DBGMSG(
"hid_write_irq - has error: 0x%x\n", utr->status);
451 hdev->write_func(hdev, utr->ep->bEndpointAddress, utr->status, utr->buff, &(utr->data_len));
457 utr->data_len = utr->ep->wMaxPacketSize;
458 hdev->write_func(hdev, utr->ep->bEndpointAddress, utr->status, utr->buff, &(utr->data_len));
462 ret = usbh_int_xfer(utr);
465 HID_DBGMSG(
"hid_write_irq - failed to submit interrupt-out request (%d)", ret);
466 hdev->write_func(hdev, utr->ep->bEndpointAddress, ret, utr->buff, &(utr->data_len));
486 IFACE_T *iface = (IFACE_T *)hdev->iface;
491 if((!iface) || (!iface->udev))
499 utr = hdev->utr_list[i];
500 if((utr !=
NULL) && (utr->ep !=
NULL) && (utr->ep->bEndpointAddress == ep_addr))
507 ep = usbh_iface_find_ep(iface, 0, EP_ADDR_DIR_IN | EP_ATTR_TT_INT);
509 ep = usbh_iface_find_ep(iface, ep_addr, 0);
514 utr = alloc_utr(iface->udev);
518 utr->buff = usbh_alloc_mem(ep->wMaxPacketSize);
519 if(utr->buff ==
NULL)
525 hdev->read_func = func;
529 utr->data_len = ep->wMaxPacketSize;
531 utr->func = hid_read_irq;
533 ret = usbh_int_xfer(utr);
536 HID_DBGMSG(
"Error - failed to submit interrupt read request (%d)", ret);
537 usbh_free_mem(utr->buff, utr->data_len);
542 i = get_free_utr_slot(hdev);
545 HID_DBGMSG(
"Error - No free HID slot!\n");
547 usbh_free_mem(utr->buff, utr->data_len);
553 hdev->utr_list[i] = utr;
570 IFACE_T *iface = (IFACE_T *)hdev->iface;
574 if((!iface) || (!iface->udev))
579 utr = hdev->utr_list[i];
583 if((utr !=
NULL) && (utr->ep !=
NULL) &&
584 ((utr->ep->bEndpointAddress & EP_ADDR_DIR_MASK) == EP_ADDR_DIR_IN))
592 if((utr !=
NULL) && (utr->ep !=
NULL) && (utr->ep->bEndpointAddress == ep_addr))
603 hdev->utr_list[i] =
NULL;
605 ret = usbh_quit_utr(utr);
607 usbh_free_mem(utr->buff, utr->ep->wMaxPacketSize);
625 IFACE_T *iface = (IFACE_T *)hdev->iface;
630 if((!iface) || (!iface->udev))
638 utr = hdev->utr_list[i];
639 if((utr !=
NULL) && (utr->ep !=
NULL) && (utr->ep->bEndpointAddress == ep_addr))
646 ep = usbh_iface_find_ep(iface, 0, EP_ADDR_DIR_OUT | EP_ATTR_TT_INT);
648 ep = usbh_iface_find_ep(iface, ep_addr, 0);
653 utr = alloc_utr(iface->udev);
657 utr->buff = usbh_alloc_mem(ep->wMaxPacketSize);
658 if(utr->buff ==
NULL)
664 hdev->write_func = func;
668 utr->data_len = ep->wMaxPacketSize;
670 utr->func = hid_write_irq;
673 func(hdev, ep->bEndpointAddress, 0, utr->buff, &(utr->data_len));
675 ret = usbh_int_xfer(utr);
678 HID_DBGMSG(
"Error - failed to submit interrupt read request (%d)", ret);
683 i = get_free_utr_slot(hdev);
686 HID_DBGMSG(
"Error - No free HID slot!\n");
688 usbh_free_mem(utr->buff, utr->data_len);
694 hdev->utr_list[i] = utr;
711 IFACE_T *iface = (IFACE_T *)hdev->iface;
715 if((!iface) || (!iface->udev))
720 utr = hdev->utr_list[i];
724 if((utr !=
NULL) && (utr->ep !=
NULL) &&
725 ((utr->ep->bEndpointAddress & EP_ADDR_DIR_MASK) == EP_ADDR_DIR_OUT))
733 if((utr !=
NULL) && (utr->ep !=
NULL) && (utr->ep->bEndpointAddress == ep_addr))
744 hdev->utr_list[i] =
NULL;
746 ret = usbh_quit_utr(utr);
748 usbh_free_mem(utr->buff, utr->ep->wMaxPacketSize);
763 _mouse_callback = func;
775 _keyboard_callback = func;
NuMicro peripheral access layer header file.
#define HID_RET_XFER_IS_RUNNING
#define USBH_ERR_EP_NOT_FOUND
#define HID_RET_DEV_NOT_FOUND
#define HID_RET_INVALID_PARAMETER
#define USBH_ERR_NOT_FOUND
#define CONFIG_HID_DEV_MAX_PIPE
#define USBH_ERR_MEMORY_OUT
void usbh_hid_regitser_mouse_callback(HID_MOUSE_FUNC *func)
Register the mouse event callback function to HID class driver. Any mouse reports will be sent to use...
void usbh_hid_regitser_keyboard_callback(HID_KEYBOARD_FUNC *func)
Register the keyboard event callback function to HID class driver. Any keyboard reports will be sent ...
void() HID_KEYBOARD_FUNC(struct usbhid_dev *hdev, KEYBOARD_EVENT_T *kbd)
void() HID_MOUSE_FUNC(struct usbhid_dev *hdev, MOUSE_EVENT_T *mouse)
HIDDEN_SYMBOLS struct usbhid_dev HID_DEV_T
void() HID_IR_FUNC(struct usbhid_dev *hdev, uint16_t ep_addr, int status, uint8_t *rdata, uint32_t data_len)
void() HID_IW_FUNC(struct usbhid_dev *hdev, uint16_t ep_addr, int status, uint8_t *wbuff, uint32_t *data_len)
int32_t usbh_hid_stop_int_write(HID_DEV_T *hdev, uint8_t ep_addr)
stop purge the USB interrupt out transfer.
int32_t usbh_hid_set_protocol(HID_DEV_T *hdev, uint8_t protocol)
Issue a HID class SET_PROTOCOL request. The SET_PROTOCOL switches between the boot protocol and the r...
int32_t usbh_hid_stop_int_read(HID_DEV_T *hdev, uint8_t ep_addr)
Stop purge the USB interrupt in transfer.
int32_t usbh_hid_set_report(HID_DEV_T *hdev, int rtp_typ, int rtp_id, uint8_t *data, int len)
Issue a HID class SET_REPORT request. The Set_Report request allows the host to send a report to the ...
int32_t usbh_hid_get_report(HID_DEV_T *hdev, int rtp_typ, int rtp_id, uint8_t *data, int len)
Issue a HID class GET_REPORT request.
HIDDEN_SYMBOLS int32_t usbh_hid_start_int_read(HID_DEV_T *hdev, uint8_t ep_addr, HID_IR_FUNC *func)
Start purge the USB interrupt in transfer.
int32_t usbh_hid_get_report_descriptor(HID_DEV_T *hdev, uint8_t *desc_buf, int buf_max_len)
Read report descriptor from HID device.
int32_t usbh_hid_set_idle(HID_DEV_T *hdev, int rtp_id, uint8_t idle_rate)
Issue a HID class SET_IDLE request. The SET_IDLE request silences a particular report on the Interrup...
int32_t usbh_hid_get_protocol(HID_DEV_T *hdev, uint8_t *protocol)
Issue a HID class GET_PROTOCOL request. The GET_PROTOCOL request reads which protocol is currently ac...
HIDDEN_SYMBOLS int32_t usbh_hid_get_idle(HID_DEV_T *hdev, int rtp_id, uint8_t *idle_rate)
Issue a HID class GET_IDLE request. The GET_IDLE request reads the current idle rate for a particular...
int32_t usbh_hid_start_int_write(HID_DEV_T *hdev, uint8_t ep_addr, HID_IW_FUNC *func)
Start purge the USB interrupt out transfer.
M471M/R1/S MCU USB Host HID report descriptor parser.
USB Host library header file.
USB Host HID class driver header file.
USB Host library exported header file.