usbh_core.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file usbh_core.h
  3. *
  4. * Copyright (c) 2022 sakumisu
  5. *
  6. * Licensed to the Apache Software Foundation (ASF) under one or more
  7. * contributor license agreements. See the NOTICE file distributed with
  8. * this work for additional information regarding copyright ownership. The
  9. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance with the
  11. * License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  18. * License for the specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. #ifndef _USBH_CORE_H
  23. #define _USBH_CORE_H
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include "usb_config.h"
  29. #include "usb_util.h"
  30. #include "usb_errno.h"
  31. #include "usb_def.h"
  32. #include "usb_list.h"
  33. #include "usb_mem.h"
  34. #include "usb_log.h"
  35. #include "usb_hc.h"
  36. #include "usb_osal.h"
  37. #include "usb_workq.h"
  38. #include "usbh_hub.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. #define USBH_ROOT_HUB_INDEX 1 /* roothub index*/
  43. #define USBH_EX_HUB_INDEX 2 /* external hub index */
  44. #define USBH_HUB_PORT_START_INDEX 1 /* first hub port index */
  45. #ifdef CONFIG_USBHOST_HUB
  46. #define ROOTHUB(hport) ((hport)->parent == NULL)
  47. #else
  48. #define ROOTHUB(hport) true
  49. #endif
  50. #define USB_CLASS_MATCH_VENDOR 0x0001
  51. #define USB_CLASS_MATCH_PRODUCT 0x0002
  52. #define USB_CLASS_MATCH_INTF_CLASS 0x0004
  53. #define USB_CLASS_MATCH_INTF_SUBCLASS 0x0008
  54. #define USB_CLASS_MATCH_INTF_PROTOCOL 0x0010
  55. #define CLASS_CONNECT(hport, i) ((hport)->config.intf[i].class_driver->connect(hport, i))
  56. #define CLASS_DISCONNECT(hport, i) ((hport)->config.intf[i].class_driver->disconnect(hport, i))
  57. #define CLASS_INFO_DEFINE __attribute__((section("usbh_class_info"))) __USED __ALIGNED(1)
  58. enum usbh_event_type {
  59. USBH_EVENT_CONNECTED = (1 << 0),
  60. USBH_EVENT_DISCONNECTED = (1 << 1),
  61. };
  62. struct usbh_class_info {
  63. uint8_t match_flags;/* Used for product specific matches; range is inclusive */
  64. uint8_t class; /* Base device class code */
  65. uint8_t subclass; /* Sub-class, depends on base class. Eg. */
  66. uint8_t protocol; /* Protocol, depends on base class. Eg. */
  67. uint16_t vid; /* Vendor ID (for vendor/product specific devices) */
  68. uint16_t pid; /* Product ID (for vendor/product specific devices) */
  69. const struct usbh_class_driver *class_driver;
  70. };
  71. struct usbh_hubport;
  72. struct usbh_class_driver {
  73. const char *driver_name;
  74. int (*connect)(struct usbh_hubport *hport, uint8_t intf);
  75. int (*disconnect)(struct usbh_hubport *hport, uint8_t intf);
  76. };
  77. typedef struct usbh_endpoint {
  78. struct usb_endpoint_descriptor ep_desc;
  79. } usbh_endpoint_t;
  80. typedef struct usbh_interface {
  81. struct usb_interface_descriptor intf_desc;
  82. struct usbh_endpoint ep[CONFIG_USBHOST_EP_NUM];
  83. char devname[CONFIG_USBHOST_DEV_NAMELEN];
  84. struct usbh_class_driver *class_driver;
  85. void *priv;
  86. } usbh_interface_t;
  87. typedef struct usbh_configuration {
  88. struct usb_configuration_descriptor config_desc;
  89. struct usbh_interface intf[CONFIG_USBHOST_INTF_NUM];
  90. } usbh_configuration_t;
  91. typedef struct usbh_hubport {
  92. bool connected; /* True: device connected; false: disconnected */
  93. bool port_change; /* True: port changed; false: port do not change */
  94. uint8_t port; /* Hub port index */
  95. uint8_t dev_addr; /* device address */
  96. uint8_t speed; /* device speed */
  97. usbh_epinfo_t ep0; /* control ep info */
  98. struct usb_device_descriptor device_desc;
  99. struct usbh_configuration config;
  100. #if 0
  101. uint8_t* config_desc;
  102. #endif
  103. struct usb_setup_packet *setup;
  104. struct usbh_hub *parent; /*if NULL, is roothub*/
  105. } usbh_hubport_t;
  106. typedef struct usbh_hub {
  107. usb_slist_t list;
  108. uint8_t index; /* Hub index */
  109. uint8_t nports; /* Hub port number */
  110. uint8_t dev_addr; /* Hub device address */
  111. usbh_epinfo_t intin;
  112. uint8_t *int_buffer;
  113. struct hub_port_status *port_status;
  114. struct usb_hub_descriptor hub_desc;
  115. struct usbh_hubport child[CONFIG_USBHOST_EHPORTS];
  116. struct usbh_hubport *parent; /* Parent hub port */
  117. struct usb_work work;
  118. } usbh_hub_t;
  119. void usbh_event_notify_handler(uint8_t event, uint8_t rhport);
  120. int usbh_initialize(void);
  121. int lsusb(int argc, char **argv);
  122. struct usbh_hubport *usbh_find_hubport(uint8_t dev_addr);
  123. void *usbh_find_class_instance(const char *devname);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif