2 * @brief Definition of functions exported by ROM based HID function driver
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * All rights reserved.
\r
9 * Software that is described herein is for illustrative purposes only
\r
10 * which provides customers with programming information regarding the
\r
11 * LPC products. This software is supplied "AS IS" without any warranties of
\r
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
\r
13 * all warranties, express or implied, including all implied warranties of
\r
14 * merchantability, fitness for a particular purpose and non-infringement of
\r
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
\r
16 * or liability for the use of the software, conveys no license or rights under any
\r
17 * patent, copyright, mask work right, or any other intellectual property rights in
\r
18 * or to any products. NXP Semiconductors reserves the right to make changes
\r
19 * in the software without notification. NXP Semiconductors also makes no
\r
20 * representation or warranty that such application will be suitable for the
\r
21 * specified use without further testing or modification.
\r
24 * Permission to use, copy, modify, and distribute this software and its
\r
25 * documentation is hereby granted, under NXP Semiconductors' and its
\r
26 * licensor's relevant copyrights in the software, without fee, provided that it
\r
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
\r
28 * copyright, permission, and disclaimer notice must appear in all copies of
\r
32 #ifndef __HIDUSER_H__
\r
33 #define __HIDUSER_H__
\r
36 #include "usbd_hid.h"
\r
37 #include "usbd_core.h"
\r
40 /** @ingroup Group_USBD_Class
\r
41 * @defgroup USBD_HID HID Class Function Driver
\r
42 * @section Sec_HIDModDescription Module Description
\r
43 * HID Class Function Driver module. This module contains an internal implementation of the USB HID Class.
\r
44 * User applications can use this class driver instead of implementing the HID class manually
\r
45 * via the low-level HW and core APIs.
\r
47 * This module is designed to simplify the user code by exposing only the required interface needed to interface with
\r
48 * Devices using the USB HID Class.
\r
51 /** @brief HID report descriptor data structure.
\r
54 * @details This structure is used as part of HID function driver initialization
\r
55 * parameter structure @ref USBD_HID_INIT_PARAM_T. This structure contains
\r
56 * details of a report type supported by the application. An application
\r
57 * can support multiple report types as a single HID device. The application
\r
58 * should define this report type data struture per report it supports and
\r
59 * the array of reoprt types to USBD_HID_API::init() through @ref USBD_HID_INIT_PARAM_T
\r
62 * @note All descriptor pointers assigned in this structure should be on 4 byte
\r
63 * aligned address boundary.
\r
66 typedef struct _HID_REPORT_T {
\r
67 uint16_t len; /**< Size of the report descriptor in bytes. */
\r
68 uint8_t idle_time; /**< This value is used by stack to respond to Set_Idle &
\r
69 GET_Idle requests for the specified report ID. The value
\r
70 of this field specified the rate at which duplicate reports
\r
71 are generated for the specified Report ID. For example, a
\r
72 device with two input reports could specify an idle rate of
\r
73 20 milliseconds for report ID 1 and 500 milliseconds for
\r
76 uint8_t __pad; /**< Padding space. */
\r
77 uint8_t* desc; /**< Report descriptor. */
\r
80 /** @brief USB descriptors data structure.
\r
83 * @details This module exposes functions which interact directly with USB device stack's core layer.
\r
84 * The application layer uses this component when it has to implement custom class function driver or
\r
85 * standard class function driver which is not part of the current USB device stack.
\r
86 * The functions exposed by this interface are to register class specific EP0 handlers and corresponding
\r
87 * utility functions to manipulate EP0 state machine of the stack. This interface also exposes
\r
88 * function to register custom endpoint interrupt handler.
\r
91 typedef struct USBD_HID_INIT_PARAM
\r
93 /* memory allocation params */
\r
94 uint32_t mem_base; /**< Base memory location from where the stack can allocate
\r
95 data and buffers. @note The memory address set in this field
\r
96 should be accessible by USB DMA controller. Also this value
\r
97 should be aligned on 4 byte boundary.
\r
99 uint32_t mem_size; /**< The size of memory buffer which stack can use.
\r
100 @note The \em mem_size should be greater than the size
\r
101 returned by USBD_HID_API::GetMemSize() routine.*/
\r
103 uint8_t max_reports; /**< Number of HID reports supported by this instance
\r
104 of HID class driver.
\r
106 uint8_t* intf_desc; /**< Pointer to the HID interface descriptor within the
\r
107 descriptor array (\em high_speed_desc) passed to Init()
\r
108 through @ref USB_CORE_DESCS_T structure.
\r
110 USB_HID_REPORT_T* report_data; /**< Pointer to an array of HID report descriptor
\r
111 data structure (@ref USB_HID_REPORT_T). The number
\r
112 of elements in the array should be same a \em max_reports
\r
113 value. The stack uses this array to respond to
\r
114 requests recieved for various HID report descriptor
\r
115 information. @note This array should be of global scope.
\r
118 /* user defined functions */
\r
119 /* required functions */
\r
121 * HID get report callback function.
\r
123 * This function is provided by the application software. This function gets called
\r
124 * when host sends a HID_REQUEST_GET_REPORT request. The setup packet data (\em pSetup)
\r
125 * is passed to the callback so that application can extract the report ID, report
\r
126 * type and other information need to generate the report. @note HID reports are sent
\r
127 * via interrupt IN endpoint also. This function is called only when report request
\r
128 * is received on control endpoint. Application should implement \em HID_EpIn_Hdlr to
\r
129 * send reports to host via interrupt IN endpoint.
\r
132 * @param hHid Handle to HID function driver.
\r
133 * @param pSetup Pointer to setup packet recived from host.
\r
134 * @param pBuffer Pointer to a pointer of data buffer containing report data.
\r
135 * Pointer-to-pointer is used to implement zero-copy buffers.
\r
136 * @param length Amount of data copied to destination buffer.
\r
137 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
138 * @retval LPC_OK On success.
\r
139 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
140 * @retval ERR_USBD_xxx For other error conditions.
\r
143 ErrorCode_t (*HID_GetReport)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* length);
\r
146 * HID set report callback function.
\r
148 * This function is provided by the application software. This function gets called
\r
149 * when host sends a HID_REQUEST_SET_REPORT request. The setup packet data (\em pSetup)
\r
150 * is passed to the callback so that application can extract the report ID, report
\r
151 * type and other information need to modify the report. An application might choose
\r
152 * to ignore input Set_Report requests as meaningless. Alternatively these reports
\r
153 * could be used to reset the origin of a control (that is, current position should
\r
156 * @param hHid Handle to HID function driver.
\r
157 * @param pSetup Pointer to setup packet recived from host.
\r
158 * @param pBuffer Pointer to a pointer of data buffer containing report data.
\r
159 * Pointer-to-pointer is used to implement zero-copy buffers.
\r
160 * @param length Amount of data copied to destination buffer.
\r
161 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
162 * @retval LPC_OK On success.
\r
163 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
164 * @retval ERR_USBD_xxx For other error conditions.
\r
167 ErrorCode_t (*HID_SetReport)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length);
\r
169 /* optional functions */
\r
172 * Optional callback function to handle HID_GetPhysDesc request.
\r
174 * The application software could provide this callback HID_GetPhysDesc handler to
\r
175 * handle get physical descriptor requests sent by the host. When host requests
\r
176 * Physical Descriptor set 0, application should return a special descriptor
\r
177 * identifying the number of descriptor sets and their sizes. A Get_Descriptor
\r
178 * request with the Physical Index equal to 1 should return the first Physical
\r
179 * Descriptor set. A device could possibly have alternate uses for its items.
\r
180 * These can be enumerated by issuing subsequent Get_Descriptor requests while
\r
181 * incrementing the Descriptor Index. A device should return the last descriptor
\r
182 * set to requests with an index greater than the last number defined in the HID
\r
184 * @note Applications which don't have physical descriptor should set this data member
\r
185 * to zero before calling the USBD_HID_API::Init().
\r
188 * @param hHid Handle to HID function driver.
\r
189 * @param pSetup Pointer to setup packet recived from host.
\r
190 * @param pBuf Pointer to a pointer of data buffer containing physical descriptor
\r
191 * data. If the physical descriptor is in USB accessable memory area
\r
192 * application could just update the pointer or else it should copy
\r
193 * the descriptor to the address pointed by this pointer.
\r
194 * @param length Amount of data copied to destination buffer or descriptor length.
\r
195 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
196 * @retval LPC_OK On success.
\r
197 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
198 * @retval ERR_USBD_xxx For other error conditions.
\r
201 ErrorCode_t (*HID_GetPhysDesc)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuf, uint16_t* length);
\r
204 * Optional callback function to handle HID_REQUEST_SET_IDLE request.
\r
206 * The application software could provide this callback to handle HID_REQUEST_SET_IDLE
\r
207 * requests sent by the host. This callback is provided to applications to adjust
\r
208 * timers associated with various reports, which are sent to host over interrupt
\r
209 * endpoint. The setup packet data (\em pSetup) is passed to the callback so that
\r
210 * application can extract the report ID, report type and other information need
\r
211 * to modify the report's idle time.
\r
212 * @note Applications which don't send reports on Interrupt endpoint or don't
\r
213 * have idle time between reports should set this data member to zero before
\r
214 * calling the USBD_HID_API::Init().
\r
217 * @param hHid Handle to HID function driver.
\r
218 * @param pSetup Pointer to setup packet recived from host.
\r
219 * @param idleTime Idle time to be set for the specified report.
\r
220 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
221 * @retval LPC_OK On success.
\r
222 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
223 * @retval ERR_USBD_xxx For other error conditions.
\r
226 ErrorCode_t (*HID_SetIdle)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t idleTime);
\r
229 * Optional callback function to handle HID_REQUEST_SET_PROTOCOL request.
\r
231 * The application software could provide this callback to handle HID_REQUEST_SET_PROTOCOL
\r
232 * requests sent by the host. This callback is provided to applications to adjust
\r
233 * modes of their code between boot mode and report mode.
\r
234 * @note Applications which don't support protocol modes should set this data member
\r
235 * to zero before calling the USBD_HID_API::Init().
\r
238 * @param hHid Handle to HID function driver.
\r
239 * @param pSetup Pointer to setup packet recived from host.
\r
240 * @param protocol Protocol mode.
\r
241 * 0 = Boot Protocol
\r
242 * 1 = Report Protocol
\r
243 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
244 * @retval LPC_OK On success.
\r
245 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
246 * @retval ERR_USBD_xxx For other error conditions.
\r
249 ErrorCode_t (*HID_SetProtocol)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t protocol);
\r
252 * Optional Interrupt IN endpoint event handler.
\r
254 * The application software could provide Interrupt IN endpoint event handler.
\r
255 * Application which send reports to host on interrupt endpoint should provide
\r
256 * an endpoint event handler through this data member. This data memeber is
\r
257 * ignored if the interface descriptor \em intf_desc doesn't have any IN interrupt
\r
258 * endpoint descriptor associated.
\r
261 * @param hUsb Handle to the USB device stack.
\r
262 * @param data Handle to HID function driver.
\r
263 * @param event Type of endpoint event. See @ref USBD_EVENT_T for more details.
\r
264 * @return The call back should return @ref ErrorCode_t type to indicate success or error condition.
\r
265 * @retval LPC_OK On success.
\r
266 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
267 * @retval ERR_USBD_xxx For other error conditions.
\r
270 ErrorCode_t (*HID_EpIn_Hdlr) (USBD_HANDLE_T hUsb, void* data, uint32_t event);
\r
272 * Optional Interrupt OUT endpoint event handler.
\r
274 * The application software could provide Interrupt OUT endpoint event handler.
\r
275 * Application which recieves reports from host on interrupt endpoint should provide
\r
276 * an endpoint event handler through this data member. This data memeber is
\r
277 * ignored if the interface descriptor \em intf_desc doesn't have any OUT interrupt
\r
278 * endpoint descriptor associated.
\r
281 * @param hUsb Handle to the USB device stack.
\r
282 * @param data Handle to HID function driver.
\r
283 * @param event Type of endpoint event. See @ref USBD_EVENT_T for more details.
\r
284 * @return The call back should return @ref ErrorCode_t type to indicate success or error condition.
\r
285 * @retval LPC_OK On success.
\r
286 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
287 * @retval ERR_USBD_xxx For other error conditions.
\r
290 ErrorCode_t (*HID_EpOut_Hdlr) (USBD_HANDLE_T hUsb, void* data, uint32_t event);
\r
292 /* user overridable function */
\r
294 * Optional user overridable function to replace the default HID_GetReportDesc handler.
\r
296 * The application software could override the default HID_GetReportDesc handler with their
\r
297 * own by providing the handler function address as this data member of the parameter
\r
298 * structure. Application which like the default handler should set this data member
\r
299 * to zero before calling the USBD_HID_API::Init() and also provide report data array
\r
300 * \em report_data field.
\r
304 * @param hUsb Handle to the USB device stack.
\r
305 * @param data Pointer to the data which will be passed when callback function is called by the stack.
\r
306 * @param event Type of endpoint event. See @ref USBD_EVENT_T for more details.
\r
307 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
308 * @retval LPC_OK On success.
\r
309 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
310 * @retval ERR_USBD_xxx For other error conditions.
\r
313 ErrorCode_t (*HID_GetReportDesc)(USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuf, uint16_t* length);
\r
315 * Optional user overridable function to replace the default HID class handler.
\r
317 * The application software could override the default EP0 class handler with their
\r
318 * own by providing the handler function address as this data member of the parameter
\r
319 * structure. Application which like the default handler should set this data member
\r
320 * to zero before calling the USBD_HID_API::Init().
\r
324 * @param hUsb Handle to the USB device stack.
\r
325 * @param data Pointer to the data which will be passed when callback function is called by the stack.
\r
326 * @param event Type of endpoint event. See @ref USBD_EVENT_T for more details.
\r
327 * @return The call back should returns @ref ErrorCode_t type to indicate success or error condition.
\r
328 * @retval LPC_OK On success.
\r
329 * @retval ERR_USBD_UNHANDLED Event is not handled hence pass the event to next in line.
\r
330 * @retval ERR_USBD_xxx For other error conditions.
\r
333 ErrorCode_t (*HID_Ep0_Hdlr) (USBD_HANDLE_T hUsb, void* data, uint32_t event);
\r
335 } USBD_HID_INIT_PARAM_T;
\r
337 /** @brief HID class API functions structure.
\r
338 * @ingroup USBD_HID
\r
340 * This structure contains pointers to all the function exposed by HID function driver module.
\r
343 typedef struct USBD_HID_API
\r
345 /** @fn uint32_t GetMemSize(USBD_HID_INIT_PARAM_T* param)
\r
346 * Function to determine the memory required by the HID function driver module.
\r
348 * This function is called by application layer before calling pUsbApi->hid->Init(), to allocate memory used
\r
349 * by HID function driver module. The application should allocate the memory which is accessible by USB
\r
350 * controller/DMA controller.
\r
351 * @note Some memory areas are not accessible by all bus masters.
\r
353 * @param param Structure containing HID function driver module initialization parameters.
\r
354 * @return Returns the required memory size in bytes.
\r
356 uint32_t (*GetMemSize)(USBD_HID_INIT_PARAM_T* param);
\r
358 /** @fn ErrorCode_t init(USBD_HANDLE_T hUsb, USBD_HID_INIT_PARAM_T* param)
\r
359 * Function to initialize HID function driver module.
\r
361 * This function is called by application layer to initialize HID function driver
\r
362 * module. On successful initialization the function returns a handle to HID
\r
363 * function driver module in passed param structure.
\r
365 * @param hUsb Handle to the USB device stack.
\r
366 * @param param Structure containing HID function driver module
\r
367 * initialization parameters.
\r
368 * @return Returns @ref ErrorCode_t type to indicate success or error condition.
\r
369 * @retval LPC_OK On success
\r
370 * @retval ERR_USBD_BAD_MEM_BUF Memory buffer passed is not 4-byte
\r
371 * aligned or smaller than required.
\r
372 * @retval ERR_API_INVALID_PARAM2 Either HID_GetReport() or HID_SetReport()
\r
373 * callback are not defined.
\r
374 * @retval ERR_USBD_BAD_DESC HID_HID_DESCRIPTOR_TYPE is not defined
\r
375 * immediately after interface descriptor.
\r
376 * @retval ERR_USBD_BAD_INTF_DESC Wrong interface descriptor is passed.
\r
377 * @retval ERR_USBD_BAD_EP_DESC Wrong endpoint descriptor is passed.
\r
379 ErrorCode_t (*init)(USBD_HANDLE_T hUsb, USBD_HID_INIT_PARAM_T* param);
\r
383 /*-----------------------------------------------------------------------------
\r
384 * Private functions & structures prototypes
\r
385 *-----------------------------------------------------------------------------*/
\r
386 /** @cond ADVANCED_API */
\r
388 typedef struct _HID_CTRL_T {
\r
389 /* pointer to controller */
\r
390 USB_CORE_CTRL_T* pUsbCtrl;
\r
391 /* descriptor pointers */
\r
393 USB_HID_REPORT_T* report_data;
\r
396 uint8_t if_num; /* interface number */
\r
397 uint8_t epin_adr; /* IN interrupt endpoint */
\r
398 uint8_t epout_adr; /* OUT interrupt endpoint */
\r
400 /* user defined functions */
\r
401 ErrorCode_t (*HID_GetReport)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* length);
\r
402 ErrorCode_t (*HID_SetReport)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length);
\r
403 ErrorCode_t (*HID_GetPhysDesc)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuf, uint16_t* length);
\r
404 ErrorCode_t (*HID_SetIdle)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t idleTime);
\r
405 ErrorCode_t (*HID_SetProtocol)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t protocol);
\r
407 /* virtual overridable functions */
\r
408 ErrorCode_t (*HID_GetReportDesc)(USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuf, uint16_t* length);
\r
415 #endif /* __HIDUSER_H__ */
\r