usbd_hub.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file usbd_hub.c
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #include "usbd_core.h"
  24. #include "usbd_hub.h"
  25. static struct usb_hub_descriptor hub_desc = {
  26. .bLength = 0x09,
  27. .bDescriptorType = HUB_DESCRIPTOR_TYPE_HUB,
  28. .bNbrPorts = 4,
  29. .wHubCharacteristics = HUB_CHAR_PORTIND | HUB_CHAR_TTTT_32_BITS,
  30. .bPwrOn2PwrGood = 0x32,
  31. .bHubContrCurrent = 0x64,
  32. .DeviceRemovable = 0x00,
  33. .PortPwrCtrlMask = 0xff
  34. };
  35. static int hub_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  36. {
  37. USBD_LOG_DBG("HUB Class Custom request: "
  38. "bRequest 0x%02x\r\n",
  39. setup->bRequest);
  40. if (((setup->bmRequestType & USB_REQUEST_TYPE_MASK) == USB_REQUEST_CLASS) &&
  41. ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_DEVICE) &&
  42. (setup->bRequest == HUB_REQUEST_GET_DESCRIPTOR)) {
  43. uint8_t value = (uint8_t)(setup->wValue >> 8);
  44. uint8_t intf_num = (uint8_t)setup->wIndex;
  45. switch (value) {
  46. case HUB_DESCRIPTOR_TYPE_HUB:
  47. *data = (uint8_t *)&hub_desc;
  48. *len = hub_desc.bLength;
  49. break;
  50. default:
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. else if (((setup->bmRequestType & USB_REQUEST_TYPE_MASK) == USB_REQUEST_CLASS) &&
  56. ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_OTHER)) {
  57. uint8_t hub_port_feature = (uint8_t)(setup->wValue);
  58. uint8_t hub_port = (uint8_t)setup->wIndex;
  59. switch (setup->bRequest) {
  60. case HUB_REQUEST_GET_STATUS:
  61. break;
  62. case HUB_REQUEST_CLEAR_FEATURE:
  63. break;
  64. case HUB_REQUEST_SET_FEATURE:
  65. break;
  66. default:
  67. USBD_LOG_WRN("Unhandled HUB Class Custom bRequest 0x%02x\r\n", setup->bRequest);
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. return -1;
  73. }
  74. static void hub_notify_handler(uint8_t event, void *arg)
  75. {
  76. switch (event) {
  77. case USBD_EVENT_RESET:
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. void usbd_hub_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
  84. {
  85. static usbd_class_t *last_class = NULL;
  86. if (last_class != devclass) {
  87. last_class = devclass;
  88. usbd_class_register(devclass);
  89. }
  90. intf->class_handler = NULL;
  91. intf->custom_handler = hub_custom_request_handler;
  92. intf->vendor_handler = NULL;
  93. intf->notify_handler = hub_notify_handler;
  94. usbd_class_add_interface(devclass, intf);
  95. }