1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of the copyright holder nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _USB_HOST_DEV_MNG_H_
32 #define _USB_HOST_DEV_MNG_H_
33 
34 #include "usb_host.h"
35 
36 /*******************************************************************************
37  * Definitions
38  ******************************************************************************/
39 /*!
40  * @addtogroup usb_host_drv
41  * @{
42  */
43 /*! @brief States of device instances enumeration */
44 typedef enum _usb_host_device_enumeration_status
45 {
46     kStatus_DEV_Notinit = 0, /*!< Device is invalid */
47     kStatus_DEV_Initial,     /*!< Device has been processed by host driver */
48     kStatus_DEV_GetDes8,     /*!< Enumeration process: get 8 bytes' device descriptor */
49     kStatus_DEV_SetAddress,  /*!< Enumeration process: set device address */
50     kStatus_DEV_GetDes,      /*!< Enumeration process: get device descriptor */
51     kStatus_DEV_GetCfg9,     /*!< Enumeration process: get 9 bytes' configuration descriptor */
52     kStatus_DEV_GetCfg,      /*!< Enumeration process: get configuration descriptor */
53     kStatus_DEV_SetCfg,      /*!< Enumeration process: set configuration */
54     kStatus_DEV_EnumDone,    /*!< Enumeration is done */
55     kStatus_DEV_AppUsed,     /*!< This device has been used by application */
56 } usb_host_device_enumeration_status_t;
57 
58 /*! @brief States of device's interface */
59 typedef enum _usb_host_interface_state
60 {
61     kStatus_interface_Attached = 1, /*!< Interface's default status */
62     kStatus_interface_Opened,       /*!< Interface is used by application */
63     kStatus_interface_Detached,     /*!< Interface is not used by application */
64 } usb_host_interface_state_t;
65 
66 /*! @brief States of device */
67 typedef enum _usb_host_device_state
68 {
69     kStatus_device_Detached = 0, /*!< Device is used by application */
70     kStatus_device_Attached,     /*!< Device's default status */
71 } usb_host_device_state_t;
72 
73 /*! @brief Device instance */
74 typedef struct _usb_host_device_instance
75 {
76     struct _usb_host_device_instance *next;    /*!< Next device, or NULL */
77     usb_host_handle hostHandle;                /*!< Host handle */
78     usb_host_configuration_t configuration;    /*!< Parsed configuration information for the device */
79     usb_descriptor_device_t *deviceDescriptor; /*!< Standard device descriptor */
80     usb_host_pipe_handle controlPipe;          /*!< Device's control pipe */
81     uint8_t *configurationDesc;                /*!< Configuration descriptor pointer */
82     uint16_t configurationLen;                 /*!< Configuration descriptor length */
83     uint16_t configurationValue;               /*!< Configuration index */
84     uint8_t interfaceStatus[USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE]; /*!< Interfaces' status, please reference to
85                                                                              #usb_host_interface_state_t */
86     uint8_t *enumBuffer;                                                  /*!< Buffer for enumeration */
87     uint8_t state;                                                        /*!< Device state for enumeration */
88     uint8_t enumRetries;       /*!< Re-enumeration when error in control transfer */
89     uint8_t stallRetries;      /*!< Re-transfer when stall */
90     uint8_t speed;             /*!< Device speed */
91     uint8_t allocatedAddress;  /*!< Temporary address for the device. When set address request succeeds, setAddress is
92                                   a value, 1 - 127 */
93     uint8_t setAddress;        /*!< The address has been set to the device successfully, 1 - 127 */
94     uint8_t deviceAttachState; /*!< See the usb_host_device_state_t */
95 #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
96     /* hub related */
97     uint8_t hubNumber;   /*!< Device's first connected hub address (root hub = 0) */
98     uint8_t portNumber;  /*!< Device's first connected hub's port no (1 - 8) */
99     uint8_t hsHubNumber; /*!< Device's first connected high-speed hub's address (1 - 8) */
100     uint8_t hsHubPort;   /*!< Device's first connected high-speed hub's port no (1 - 8) */
101     uint8_t level;       /*!< Device's level (root device = 0) */
102 #endif
103 } usb_host_device_instance_t;
104 
105 typedef struct _usb_host_enum_process_entry
106 {
107     uint8_t successState; /*!< When the last step is successful, the next state value */
108     uint8_t retryState;   /*!< When the last step need retry, the next state value */
109     usb_status_t (*process)(usb_host_device_instance_t *deviceInstance); /*!< When the last step transfer is done, the
110                                                                             function is used to process the transfer
111                                                                             data */
112 } usb_host_enum_process_entry_t;
113 
114 /*******************************************************************************
115  * API
116  ******************************************************************************/
117 
118 /*!
119  * @brief Calls this function when device attach.
120  *
121  * @param hostHandle    Host instance handle.
122  * @param speed         Device speed.
123  * @param hubNumber     Device hub no. root device's hub no. is 0.
124  * @param portNumber    Device port no. root device's port no. is 0.
125  * @param level         Device level. root device's level is 1.
126  * @param deviceHandle  Return device handle.
127  *
128  * @return kStatus_USB_Success or error codes.
129  */
130 extern usb_status_t USB_HostAttachDevice(usb_host_handle hostHandle,
131                                          uint8_t speed,
132                                          uint8_t hubNumber,
133                                          uint8_t portNumber,
134                                          uint8_t level,
135                                          usb_device_handle *deviceHandle);
136 
137 /*!
138  * @brief Call this function when device detaches.
139  *
140  * @param hostHandle   Host instance handle.
141  * @param hubNumber    Device hub no. root device's hub no. is 0.
142  * @param portNumber   Device port no. root device's port no. is 0.
143  *
144  * @return kStatus_USB_Success or error codes.
145  */
146 extern usb_status_t USB_HostDetachDevice(usb_host_handle hostHandle, uint8_t hubNumber, uint8_t portNumber);
147 
148 /*!
149  * @brief Call this function when device detaches.
150  *
151  * @param hostHandle    Host instance handle.
152  * @param deviceHandle  Device handle.
153  *
154  * @return kStatus_USB_Success or error codes.
155  */
156 extern usb_status_t USB_HostDetachDeviceInternal(usb_host_handle hostHandle, usb_device_handle deviceHandle);
157 
158 /*!
159  * @brief Gets the device attach/detach state.
160  *
161  * @param deviceHandle    Device handle.
162  *
163  * @return 0x01 - attached; 0x00 - detached.
164  */
165 extern uint8_t USB_HostGetDeviceAttachState(usb_device_handle deviceHandle);
166 
167 /*!
168  * @brief Determine whether the device is attached.
169  *
170  * @param hostHandle    Host instance pointer.
171  * @param deviceHandle    Device handle.
172  *
173  * @return kStatus_USB_Success or error codes.
174  */
175 extern usb_status_t USB_HostValidateDevice(usb_host_handle hostHandle, usb_device_handle deviceHandle);
176 
177 /*! @}*/
178 #endif /* _USB_HOST_DEV_MNG_H_ */
179