usb_hid.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2019, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*- Includes ----------------------------------------------------------------*/
  29. #include <stdbool.h>
  30. #include <stdalign.h>
  31. #include <string.h>
  32. #include "utils.h"
  33. #include "usb.h"
  34. #include "usb_std.h"
  35. #include "usb_hid.h"
  36. #include "usb_descriptors.h"
  37. /*- Prototypes --------------------------------------------------------------*/
  38. static void usb_hid_ep_send_callback(int size);
  39. static void usb_hid_ep_recv_callback(int size);
  40. /*- Implementations ---------------------------------------------------------*/
  41. //-----------------------------------------------------------------------------
  42. void usb_hid_init(void)
  43. {
  44. usb_set_callback(USB_HID_EP_SEND, usb_hid_ep_send_callback);
  45. usb_set_callback(USB_HID_EP_RECV, usb_hid_ep_recv_callback);
  46. }
  47. //-----------------------------------------------------------------------------
  48. void usb_hid_send(uint8_t *data, int size)
  49. {
  50. usb_send(USB_HID_EP_SEND, data, size);
  51. }
  52. //-----------------------------------------------------------------------------
  53. void usb_hid_recv(uint8_t *data, int size)
  54. {
  55. usb_recv(USB_HID_EP_RECV, data, size);
  56. }
  57. //-----------------------------------------------------------------------------
  58. static void usb_hid_ep_send_callback(int size)
  59. {
  60. usb_hid_send_callback();
  61. (void)size;
  62. }
  63. //-----------------------------------------------------------------------------
  64. static void usb_hid_ep_recv_callback(int size)
  65. {
  66. usb_hid_recv_callback(size);
  67. }
  68. //-----------------------------------------------------------------------------
  69. bool usb_hid_handle_request(usb_request_t *request)
  70. {
  71. int length = request->wLength;
  72. switch ((request->bRequest << 8) | request->bmRequestType)
  73. {
  74. case USB_CMD(IN, INTERFACE, STANDARD, GET_DESCRIPTOR):
  75. {
  76. length = LIMIT(length, sizeof(usb_hid_report_descriptor));
  77. usb_control_send((uint8_t *)usb_hid_report_descriptor, length);
  78. } break;
  79. default:
  80. return false;
  81. }
  82. return true;
  83. }