1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _USB_HOST_DEV_MNG_H_
10 #define _USB_HOST_DEV_MNG_H_
11 
12 #include "usb_host.h"
13 
14 /*******************************************************************************
15  * Definitions
16  ******************************************************************************/
17 /*!
18  * @addtogroup usb_host_drv
19  * @{
20  */
21 /*! @brief States of device instances enumeration */
22 typedef enum _usb_host_device_enumeration_status
23 {
24     kStatus_DEV_Notinit = 0, /*!< Device is invalid */
25     kStatus_DEV_Initial,     /*!< Device has been processed by host driver */
26     kStatus_DEV_GetDes8,     /*!< Enumeration process: get 8 bytes' device descriptor */
27     kStatus_DEV_SetAddress,  /*!< Enumeration process: set device address */
28     kStatus_DEV_GetDes,      /*!< Enumeration process: get device descriptor */
29     kStatus_DEV_GetCfg9,     /*!< Enumeration process: get 9 bytes' configuration descriptor */
30     kStatus_DEV_GetCfg,      /*!< Enumeration process: get configuration descriptor */
31     kStatus_DEV_SetCfg,      /*!< Enumeration process: set configuration */
32     kStatus_DEV_EnumDone,    /*!< Enumeration is done */
33     kStatus_DEV_AppUsed,     /*!< This device has been used by application */
34 } usb_host_device_enumeration_status_t;
35 
36 /*! @brief States of device's interface */
37 typedef enum _usb_host_interface_state
38 {
39     kStatus_interface_Attached = 1, /*!< Interface's default status */
40     kStatus_interface_Opened,       /*!< Interface is used by application */
41     kStatus_interface_Detached,     /*!< Interface is not used by application */
42 } usb_host_interface_state_t;
43 
44 /*! @brief States of device */
45 typedef enum _usb_host_device_state
46 {
47     kStatus_device_Detached = 0, /*!< Device is used by application */
48     kStatus_device_Attached,     /*!< Device's default status */
49 } usb_host_device_state_t;
50 
51 /*! @brief Device instance */
52 typedef struct _usb_host_device_instance
53 {
54     struct _usb_host_device_instance *next;    /*!< Next device, or NULL */
55     usb_host_handle hostHandle;                /*!< Host handle */
56     usb_host_configuration_t configuration;    /*!< Parsed configuration information for the device */
57     usb_descriptor_device_t *deviceDescriptor; /*!< Standard device descriptor */
58     usb_host_pipe_handle controlPipe;          /*!< Device's control pipe */
59     uint8_t *configurationDesc;                /*!< Configuration descriptor pointer */
60     uint8_t *enumBuffer;                       /*!< Buffer for enumeration */
61     uint16_t configurationLen;                 /*!< Configuration descriptor length */
62     uint8_t interfaceStatus[USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE]; /*!< Interfaces' status, please reference to
63                                                                              #usb_host_interface_state_t */
64     uint8_t configurationValue;                                           /*!< Configuration index */
65     uint8_t state;                                                        /*!< Device state for enumeration */
66     uint8_t enumRetries;       /*!< Re-enumeration when error in control transfer */
67     uint8_t stallRetries;      /*!< Re-transfer when stall */
68     uint8_t speed;             /*!< Device speed */
69     uint8_t allocatedAddress;  /*!< Temporary address for the device. When set address request succeeds, setAddress is
70                                   a value, 1 - 127 */
71     uint8_t setAddress;        /*!< The address has been set to the device successfully, 1 - 127 */
72     uint8_t deviceAttachState; /*!< See the usb_host_device_state_t */
73 #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
74     /* hub related */
75     uint8_t hubNumber;   /*!< Device's first connected hub address (root hub = 0) */
76     uint8_t portNumber;  /*!< Device's first connected hub's port no (1 - 8) */
77     uint8_t hsHubNumber; /*!< Device's first connected high-speed hub's address (1 - 8) */
78     uint8_t hsHubPort;   /*!< Device's first connected high-speed hub's port no (1 - 8) */
79     uint8_t level;       /*!< Device's level (root device = 0) */
80 #endif
81 } usb_host_device_instance_t;
82 
83 typedef struct _usb_host_enum_process_entry
84 {
85     usb_host_device_enumeration_status_t successState; /*!< When the last step is successful, the next state value */
86     usb_host_device_enumeration_status_t retryState;   /*!< When the last step need retry, the next state value */
87     /*! When the last step transfer is done, the function is used to process the transfer data */
88     usb_status_t (*process)(usb_host_device_instance_t *deviceInstance, uint32_t dataLength);
89 } usb_host_enum_process_entry_t;
90 
91 /*******************************************************************************
92  * API
93  ******************************************************************************/
94 
95 /*!
96  * @brief Calls this function when device attach.
97  *
98  * @param hostHandle    Host instance handle.
99  * @param speed         Device speed.
100  * @param hubNumber     Device hub no. root device's hub no. is 0.
101  * @param portNumber    Device port no. root device's port no. is 0.
102  * @param level         Device level. root device's level is 1.
103  * @param deviceHandle  Return device handle.
104  *
105  * @return kStatus_USB_Success or error codes.
106  */
107 extern usb_status_t USB_HostAttachDevice(usb_host_handle hostHandle,
108                                          uint8_t speed,
109                                          uint8_t hubNumber,
110                                          uint8_t portNumber,
111                                          uint8_t level,
112                                          usb_device_handle *deviceHandle);
113 
114 /*!
115  * @brief Call this function when device detaches.
116  *
117  * @param hostHandle   Host instance handle.
118  * @param hubNumber    Device hub no. root device's hub no. is 0.
119  * @param portNumber   Device port no. root device's port no. is 0.
120  *
121  * @return kStatus_USB_Success or error codes.
122  */
123 extern usb_status_t USB_HostDetachDevice(usb_host_handle hostHandle, uint8_t hubNumber, uint8_t portNumber);
124 
125 /*!
126  * @brief Call this function when device detaches.
127  *
128  * @param hostHandle    Host instance handle.
129  * @param deviceHandle  Device handle.
130  *
131  * @return kStatus_USB_Success or error codes.
132  */
133 extern usb_status_t USB_HostDetachDeviceInternal(usb_host_handle hostHandle, usb_device_handle deviceHandle);
134 
135 /*!
136  * @brief Gets the device attach/detach state.
137  *
138  * @param deviceHandle    Device handle.
139  *
140  * @return 0x01 - attached; 0x00 - detached.
141  */
142 extern uint8_t USB_HostGetDeviceAttachState(usb_device_handle deviceHandle);
143 
144 /*!
145  * @brief Determine whether the device is attached.
146  *
147  * @param hostHandle    Host instance pointer.
148  * @param deviceHandle    Device handle.
149  *
150  * @return kStatus_USB_Success or error codes.
151  */
152 extern usb_status_t USB_HostValidateDevice(usb_host_handle hostHandle, usb_device_handle deviceHandle);
153 
154 /*! @}*/
155 #endif /* _USB_HOST_DEV_MNG_H_ */
156