]> git.sur5r.net Git - armstart-ibdap/blob - inc/usbd/usbd_hiduser.h
initial commit
[armstart-ibdap] / inc / usbd / usbd_hiduser.h
1 /***********************************************************************\r
2 * $Id:: mw_usbd_hiduser.h 331 2012-08-09 18:54:34Z usb10131                   $\r
3 *\r
4 * Project: USB device ROM Stack\r
5 *\r
6 * Description:\r
7 *     HID Custom User Module Definitions.\r
8 *\r
9 ***********************************************************************\r
10 *   Copyright(C) 2011, NXP Semiconductor\r
11 *   All rights reserved.\r
12 *\r
13 * Software that is described herein is for illustrative purposes only\r
14 * which provides customers with programming information regarding the\r
15 * products. This software is supplied "AS IS" without any warranties.\r
16 * NXP Semiconductors assumes no responsibility or liability for the\r
17 * use of the software, conveys no license or title under any patent,\r
18 * copyright, or mask work right to the product. NXP Semiconductors\r
19 * reserves the right to make changes in the software without\r
20 * notification. NXP Semiconductors also make no representation or\r
21 * warranty that such application will be suitable for the specified\r
22 * use without further testing or modification.\r
23 **********************************************************************/\r
24 \r
25 #ifndef __HIDUSER_H__\r
26 #define __HIDUSER_H__\r
27 \r
28 #include "usbd.h"\r
29 #include "usbd_hid.h"\r
30 #include "usbd_core.h"\r
31 \r
32 /** \file\r
33  *  \brief Human Interface Device (HID) API structures and function prototypes.\r
34  *\r
35  *  Definition of functions exported by ROM based HID function driver.\r
36  *\r
37  */\r
38 \r
39 /** \ingroup Group_USBD\r
40  *  @defgroup USBD_HID HID Class Function Driver\r
41  *  \section Sec_HIDModDescription Module Description\r
42  *  HID Class Function Driver module. This module contains an internal implementation of the USB HID Class.\r
43  *  User applications can use this class driver instead of implementing the HID class manually\r
44  *  via the low-level HW and core APIs.\r
45  *\r
46  *  This module is designed to simplify the user code by exposing only the required interface needed to interface with\r
47  *  Devices using the USB HID Class.\r
48  */\r
49 \r
50 /** \brief HID report descriptor data structure. \r
51  *  \ingroup USBD_HID\r
52  *\r
53  *  \details  This structure is used as part of HID function driver initialization \r
54  *  parameter structure \ref USBD_HID_INIT_PARAM. This structure contains\r
55  *  details of a report type supported by the application. An application\r
56  *  can support multiple report types as a single HID device. The application\r
57  *  should define this report type data structure per report it supports and\r
58  *  the array of report types to USBD_HID_API::init() through \ref USBD_HID_INIT_PARAM\r
59  *  structure. \r
60  *\r
61  *  \note All descriptor pointers assigned in this structure should be on 4 byte\r
62  *  aligned address boundary. \r
63  *\r
64  */\r
65 typedef struct _HID_REPORT_T {\r
66   uint16_t len; /**< Size of the report descriptor in bytes. */ \r
67   uint8_t idle_time; /**< This value is used by stack to respond to Set_Idle & \r
68                      GET_Idle requests for the specified report ID. The value\r
69                      of this field specified the rate at which duplicate reports \r
70                      are generated for the specified Report ID. For example, a \r
71                      device with two input reports could specify an idle rate of \r
72                      20 milliseconds for report ID 1 and 500 milliseconds for \r
73                      report ID 2.\r
74                      */\r
75   uint8_t __pad; /**< Padding space. */\r
76   uint8_t* desc; /**< Report descriptor. */\r
77 } USB_HID_REPORT_T;\r
78 \r
79 /** \brief USB descriptors data structure. \r
80  *  \ingroup USBD_HID\r
81  *\r
82  *  \details  This module exposes functions which interact directly with USB device stack's core layer.\r
83  *  The application layer uses this component when it has to implement custom class function driver or \r
84  *  standard class function driver which is not part of the current USB device stack.\r
85  *  The functions exposed by this interface are to register class specific EP0 handlers and corresponding\r
86  *  utility functions to manipulate EP0 state machine of the stack. This interface also exposes\r
87  *  function to register custom endpoint interrupt handler.\r
88  *\r
89  */\r
90 typedef struct USBD_HID_INIT_PARAM\r
91 {\r
92   /* memory allocation params */\r
93   uint32_t mem_base;  /**< Base memory location from where the stack can allocate\r
94                       data and buffers. \note The memory address set in this field\r
95                       should be accessible by USB DMA controller. Also this value\r
96                       should be aligned on 4 byte boundary.\r
97                       */\r
98   uint32_t mem_size;  /**< The size of memory buffer which stack can use. \r
99                       \note The \em mem_size should be greater than the size \r
100                       returned by USBD_HID_API::GetMemSize() routine.*/\r
101   /* HID paramas */\r
102   uint8_t max_reports; /**< Number of HID reports supported by this instance\r
103                        of HID class driver. \r
104                        */\r
105   uint8_t pad[3];\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
109                       */\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 received for various HID report descriptor\r
115                                  information. \note This array should be of global scope.\r
116                                  */\r
117 \r
118   /* user defined functions */\r
119   /* required functions */\r
120   /** \r
121   *  HID get report callback function.\r
122   *\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
130   *   \r
131   *  \r
132   *  \param[in] hHid Handle to HID function driver. \r
133   *  \param[in] pSetup Pointer to setup packet received from host.\r
134   *  \param[in, out] 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   *                       See \ref USBD_ZeroCopy for more details on zero-copy concept.\r
137   *  \param[in] length  Amount of data copied to destination buffer.\r
138   *  \return The call back should returns \ref ErrorCode_t type to indicate success or error condition.\r
139   *          \retval LPC_OK On success.\r
140   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
141   *          \retval ERR_USBD_xxx  For other error conditions. \r
142   *                                             \r
143   */\r
144   ErrorCode_t (*HID_GetReport)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* length); \r
145   \r
146   /** \r
147   *  HID set report callback function.\r
148   *\r
149   *  This function is provided by the application software. This function gets called \r
150   *  when host sends a HID_REQUEST_SET_REPORT request. The setup packet data (\em pSetup)\r
151   *  is passed to the callback so that application can extract the report ID, report\r
152   *  type and other information need to modify the report. An application might choose \r
153   *  to ignore input Set_Report requests as meaningless. Alternatively these reports \r
154   *  could be used to reset the origin of a control (that is, current position should \r
155   *  report zero).\r
156   *  \r
157   *  \param[in] hHid Handle to HID function driver. \r
158   *  \param[in] pSetup Pointer to setup packet received from host.\r
159   *  \param[in, out] pBuffer  Pointer to a pointer of data buffer containing report data. \r
160   *                       Pointer-to-pointer is used to implement zero-copy buffers. \r
161   *                       See \ref USBD_ZeroCopy for more details on zero-copy concept.\r
162   *  \param[in] length  Amount of data copied to destination buffer.\r
163   *  \return The call back should returns \ref ErrorCode_t type to indicate success or error condition.\r
164   *          \retval LPC_OK On success.\r
165   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
166   *          \retval ERR_USBD_xxx  For other error conditions. \r
167   *                                             \r
168   */\r
169   ErrorCode_t (*HID_SetReport)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length);\r
170   \r
171   /* optional functions */\r
172   \r
173   /** \r
174   *  Optional callback function to handle HID_GetPhysDesc request.\r
175   *\r
176   *  The application software could provide this callback HID_GetPhysDesc handler to\r
177   *  handle get physical descriptor requests sent by the host. When host requests \r
178   *  Physical Descriptor set 0, application should return a special descriptor\r
179   *  identifying the number of descriptor sets and their sizes. A Get_Descriptor \r
180   *  request with the Physical Index equal to 1 should return the first Physical \r
181   *  Descriptor set. A device could possibly have alternate uses for its items. \r
182   *  These can be enumerated by issuing subsequent Get_Descriptor requests while \r
183   *  incrementing the Descriptor Index. A device should return the last descriptor\r
184   *  set to requests with an index greater than the last number defined in the HID \r
185   *  descriptor.\r
186   *  \note Applications which don't have physical descriptor should set this data member\r
187   *  to zero before calling the USBD_HID_API::Init().\r
188   *  \n\r
189   *  \r
190   *  \param[in] hHid Handle to HID function driver. \r
191   *  \param[in] pSetup Pointer to setup packet received from host.\r
192   *  \param[in] pBuf Pointer to a pointer of data buffer containing physical descriptor \r
193   *                   data. If the physical descriptor is in USB accessible memory area\r
194   *                   application could just update the pointer or else it should copy \r
195   *                   the descriptor to the address pointed by this pointer.\r
196   *  \param[in] length  Amount of data copied to destination buffer or descriptor length.\r
197   *  \return The call back should returns \ref ErrorCode_t type to indicate success or error condition.\r
198   *          \retval LPC_OK On success.\r
199   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
200   *          \retval ERR_USBD_xxx  For other error conditions. \r
201   *                                             \r
202   */\r
203   ErrorCode_t (*HID_GetPhysDesc)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuf, uint16_t* length);\r
204 \r
205   /** \r
206   *  Optional callback function to handle HID_REQUEST_SET_IDLE request.\r
207   *\r
208   *  The application software could provide this callback to handle HID_REQUEST_SET_IDLE\r
209   *  requests sent by the host. This callback is provided to applications to adjust\r
210   *  timers associated with various reports, which are sent to host over interrupt \r
211   *  endpoint. The setup packet data (\em pSetup) is passed to the callback so that\r
212   *  application can extract the report ID, report type and other information need \r
213   *  to modify the report's idle time.\r
214   *  \note Applications which don't send reports on Interrupt endpoint or don't\r
215   *  have idle time between reports should set this data member to zero before \r
216   *  calling the USBD_HID_API::Init().\r
217   *  \n\r
218   *  \r
219   *  \param[in] hHid Handle to HID function driver. \r
220   *  \param[in] pSetup Pointer to setup packet received from host.\r
221   *  \param[in] idleTime  Idle time to be set for the specified report.\r
222   *  \return The call back should returns \ref ErrorCode_t type to indicate success or error condition.\r
223   *          \retval LPC_OK On success.\r
224   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
225   *          \retval ERR_USBD_xxx  For other error conditions. \r
226   *                                             \r
227   */\r
228   ErrorCode_t (*HID_SetIdle)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t idleTime); \r
229  \r
230   /** \r
231   *  Optional callback function to handle HID_REQUEST_SET_PROTOCOL request.\r
232   *\r
233   *  The application software could provide this callback to handle HID_REQUEST_SET_PROTOCOL\r
234   *  requests sent by the host. This callback is provided to applications to adjust\r
235   *  modes of their code between boot mode and report mode. \r
236   *  \note Applications which don't support protocol modes should set this data member\r
237   *  to zero before calling the USBD_HID_API::Init().\r
238   *  \n\r
239   *  \r
240   *  \param[in] hHid Handle to HID function driver. \r
241   *  \param[in] pSetup Pointer to setup packet received from host.\r
242   *  \param[in] protocol  Protocol mode. \r
243   *                       0 = Boot Protocol\r
244   *                       1 = Report Protocol\r
245   *  \return The call back should returns \ref ErrorCode_t type to indicate success or error condition.\r
246   *          \retval LPC_OK On success.\r
247   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
248   *          \retval ERR_USBD_xxx  For other error conditions. \r
249   *                                             \r
250   */\r
251   ErrorCode_t (*HID_SetProtocol)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t protocol); \r
252   \r
253   /** \r
254   *  Optional Interrupt IN endpoint event handler.\r
255   *\r
256   *  The application software could provide Interrupt IN endpoint event handler. \r
257   *  Application which send reports to host on interrupt endpoint should provide\r
258   *  an endpoint event handler through this data member. This data member is\r
259   *  ignored if the interface descriptor \em intf_desc doesn't have any IN interrupt \r
260   *  endpoint descriptor associated. \r
261   *  \n\r
262   *  \r
263   *  \param[in] hUsb Handle to the USB device stack. \r
264   *  \param[in] data Handle to HID function driver. \r
265   *  \param[in] event  Type of endpoint event. See \ref USBD_EVENT_T for more details.\r
266   *  \return The call back should return \ref ErrorCode_t type to indicate success or error condition.\r
267   *          \retval LPC_OK On success.\r
268   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
269   *          \retval ERR_USBD_xxx  For other error conditions. \r
270   *                                             \r
271   */\r
272   ErrorCode_t (*HID_EpIn_Hdlr) (USBD_HANDLE_T hUsb, void* data, uint32_t event);\r
273   /** \r
274   *  Optional Interrupt OUT endpoint event handler.\r
275   *\r
276   *  The application software could provide Interrupt OUT endpoint event handler. \r
277   *  Application which receives reports from host on interrupt endpoint should provide\r
278   *  an endpoint event handler through this data member. This data member is\r
279   *  ignored if the interface descriptor \em intf_desc doesn't have any OUT interrupt \r
280   *  endpoint descriptor associated. \r
281   *  \n\r
282   *  \r
283   *  \param[in] hUsb Handle to the USB device stack. \r
284   *  \param[in] data Handle to HID function driver. \r
285   *  \param[in] event  Type of endpoint event. See \ref USBD_EVENT_T for more details.\r
286   *  \return The call back should return \ref ErrorCode_t type to indicate success or error condition.\r
287   *          \retval LPC_OK On success.\r
288   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
289   *          \retval ERR_USBD_xxx  For other error conditions. \r
290   *                                             \r
291   */\r
292   ErrorCode_t (*HID_EpOut_Hdlr) (USBD_HANDLE_T hUsb, void* data, uint32_t event);\r
293 \r
294   /* user override-able function */\r
295   /** \r
296   *  Optional user override-able function to replace the default HID_GetReportDesc handler.\r
297   *\r
298   *  The application software could override the default HID_GetReportDesc handler with their\r
299   *  own by providing the handler function address as this data member of the parameter\r
300   *  structure. Application which like the default handler should set this data member\r
301   *  to zero before calling the USBD_HID_API::Init() and also provide report data array\r
302   *  \em report_data field.\r
303   *  \n\r
304   *  \note \r
305   *  \r
306   *  \param[in] hUsb Handle to the USB device stack. \r
307   *  \param[in] data Pointer to the data which will be passed when callback function is called by the stack. \r
308   *  \param[in] event  Type of endpoint event. See \ref USBD_EVENT_T for more details.\r
309   *  \return The call back should returns \ref ErrorCode_t type to indicate success or error condition.\r
310   *          \retval LPC_OK On success.\r
311   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
312   *          \retval ERR_USBD_xxx  For other error conditions. \r
313   *                                             \r
314   */\r
315   ErrorCode_t (*HID_GetReportDesc)(USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuf, uint16_t* length);\r
316   /** \r
317   *  Optional user override-able function to replace the default HID class handler.\r
318   *\r
319   *  The application software could override the default EP0 class handler with their\r
320   *  own by providing the handler function address as this data member of the parameter\r
321   *  structure. Application which like the default handler should set this data member\r
322   *  to zero before calling the USBD_HID_API::Init().\r
323   *  \n\r
324   *  \note \r
325   *  \r
326   *  \param[in] hUsb Handle to the USB device stack. \r
327   *  \param[in] data Pointer to the data which will be passed when callback function is called by the stack. \r
328   *  \param[in] event  Type of endpoint event. See \ref USBD_EVENT_T for more details.\r
329   *  \return The call back should returns \ref ErrorCode_t type to indicate success or error condition.\r
330   *          \retval LPC_OK On success.\r
331   *          \retval ERR_USBD_UNHANDLED  Event is not handled hence pass the event to next in line. \r
332   *          \retval ERR_USBD_xxx  For other error conditions. \r
333   *                                             \r
334   */\r
335   ErrorCode_t (*HID_Ep0_Hdlr) (USBD_HANDLE_T hUsb, void* data, uint32_t event);\r
336 \r
337 } USBD_HID_INIT_PARAM_T;\r
338 \r
339 /** \brief HID class API functions structure.\r
340  *  \ingroup USBD_HID\r
341  *\r
342  *  This structure contains pointers to all the function exposed by HID function driver module.\r
343  *\r
344  */\r
345 typedef struct USBD_HID_API \r
346 {\r
347   /** \fn uint32_t GetMemSize(USBD_HID_INIT_PARAM_T* param)\r
348    *  Function to determine the memory required by the HID function driver module.\r
349    * \r
350    *  This function is called by application layer before calling pUsbApi->hid->Init(), to allocate memory used \r
351    *  by HID function driver module. The application should allocate the memory which is accessible by USB\r
352    *  controller/DMA controller. \r
353    *  \note Some memory areas are not accessible by all bus masters.\r
354    *\r
355    *  \param[in] param Structure containing HID function driver module initialization parameters.\r
356    *  \return Returns the required memory size in bytes.\r
357    */\r
358   uint32_t (*GetMemSize)(USBD_HID_INIT_PARAM_T* param);\r
359 \r
360   /** \fn ErrorCode_t init(USBD_HANDLE_T hUsb, USBD_HID_INIT_PARAM_T* param)\r
361    *  Function to initialize HID function driver module.\r
362    * \r
363    *  This function is called by application layer to initialize HID function driver  \r
364    *  module. On successful initialization the function returns a handle to HID \r
365    *  function driver module in passed param structure.  \r
366    *\r
367    *  \param[in] hUsb Handle to the USB device stack. \r
368    *  \param[in, out] param Structure containing HID function driver module \r
369    *      initialization parameters.\r
370    *  \return Returns \ref ErrorCode_t type to indicate success or error condition.\r
371    *          \retval LPC_OK On success\r
372    *          \retval ERR_USBD_BAD_MEM_BUF  Memory buffer passed is not 4-byte \r
373    *              aligned or smaller than required. \r
374    *          \retval ERR_API_INVALID_PARAM2 Either HID_GetReport() or HID_SetReport()\r
375    *              callback are not defined. \r
376    *          \retval ERR_USBD_BAD_DESC  HID_HID_DESCRIPTOR_TYPE is not defined \r
377    *              immediately after interface descriptor. \r
378    *          \retval ERR_USBD_BAD_INTF_DESC  Wrong interface descriptor is passed. \r
379    *          \retval ERR_USBD_BAD_EP_DESC  Wrong endpoint descriptor is passed. \r
380    */\r
381   ErrorCode_t (*init)(USBD_HANDLE_T hUsb, USBD_HID_INIT_PARAM_T* param);\r
382 \r
383 } USBD_HID_API_T;\r
384 \r
385 /*-----------------------------------------------------------------------------\r
386  *  Private functions & structures prototypes\r
387  *-----------------------------------------------------------------------------*/\r
388 /** @cond  ADVANCED_API */\r
389 \r
390 typedef struct _HID_CTRL_T {\r
391   /* pointer to controller */\r
392   USB_CORE_CTRL_T*  pUsbCtrl;\r
393   /* descriptor pointers */\r
394   uint8_t* hid_desc;\r
395   USB_HID_REPORT_T* report_data;\r
396 \r
397   uint8_t protocol;\r
398   uint8_t if_num;                  /* interface number */\r
399   uint8_t epin_adr;                /* IN interrupt endpoint */\r
400   uint8_t epout_adr;               /* OUT interrupt endpoint */\r
401 \r
402   /* user defined functions */\r
403   ErrorCode_t (*HID_GetReport)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* length); \r
404   ErrorCode_t (*HID_SetReport)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length);\r
405   ErrorCode_t (*HID_GetPhysDesc)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuf, uint16_t* length);\r
406   ErrorCode_t (*HID_SetIdle)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t idleTime); \r
407   ErrorCode_t (*HID_SetProtocol)( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t protocol); \r
408 \r
409   /* virtual overridable functions */ \r
410   ErrorCode_t (*HID_GetReportDesc)(USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuf, uint16_t* length);\r
411 \r
412 }USB_HID_CTRL_T;\r
413 \r
414 /** @cond  DIRECT_API */\r
415 extern uint32_t mwHID_GetMemSize(USBD_HID_INIT_PARAM_T* param);\r
416 extern ErrorCode_t mwHID_init(USBD_HANDLE_T hUsb, USBD_HID_INIT_PARAM_T* param);\r
417 /** @endcond */\r
418 \r
419 /** @endcond */\r
420 \r
421 #endif  /* __HIDUSER_H__ */\r