| NUC472_NUC442_BSP V3.04.000 The Board Support Package for NUC472/NUC442

hid_driver.c
Go to the documentation of this file.
1/**************************************************************************/
13#include <stdio.h>
14#include <string.h>
15
16#include "NUC472_442.h"
17#include "usbh_core.h"
18
19#include "usbh_hid.h"
20
21
35
36static HID_DEV_T g_hid_dev[CONFIG_HID_MAX_DEV];
37
38static HID_DEV_T *g_hdev_list = NULL;
39
40static HID_DEV_T *alloc_hid_device(void)
41{
42 int i;
43
44 for (i = 0; i < CONFIG_HID_MAX_DEV; i++)
45 {
46 if (g_hid_dev[i].udev == NULL)
47 {
48 memset((char *)&g_hid_dev[i], 0, sizeof(HID_DEV_T));
49 return &g_hid_dev[i];
50 }
51 }
52 return NULL;
53}
54
55void free_hid_device(HID_DEV_T *hid_dev)
56{
57 hid_dev->udev = NULL;
58 memset((char *)hid_dev, 0, sizeof(HID_DEV_T));
59}
60
61 HID_DEV_T *find_hid_deivce_by_urb(URB_T *urb)
62{
63 int i;
64
65 if (urb->dev == NULL)
66 return NULL;
67
68 for (i = 0; i < CONFIG_HID_MAX_DEV; i++)
69 {
70 if (g_hid_dev[i].udev == urb->dev)
71 {
72 if ((g_hid_dev[i].urbin == urb) || (g_hid_dev[i].urbout == urb))
73 return &g_hid_dev[i];
74 }
75 }
76 return NULL;
77}
78
79int find_hid_device(HID_DEV_T *hdev)
80{
81 int i;
82
83 for (i = 0; i < CONFIG_HID_MAX_DEV; i++)
84 {
85 if (&g_hid_dev[i] == hdev)
86 {
87 return TRUE;
88 }
89 }
90 return FALSE;
91}
92
93
94static int usbhid_probe(USB_DEV_T *dev, USB_IF_DESC_T *ifd, const USB_DEV_ID_T *id)
95{
96 EP_INFO_T *ep_info;
97 int ifnum;
98 HID_DEV_T *hid, *p;
99 int i;
100
101 HID_DBGMSG("usbhid_probe - dev=0x%x\n", (int)dev);
102
103 ifnum = ifd->bInterfaceNumber;
104
105 HID_DBGMSG("HID probe called for ifnum %d\n", ifnum);
106
107 ep_info = NULL;
108 for (i = 0; i < dev->ep_list_cnt; i++)
109 {
110 if ((dev->ep_list[i].ifnum == ifnum) &&
111 ((dev->ep_list[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
112 {
113 ep_info = &dev->ep_list[i];
114 HID_DBGMSG("Interrupt Endpoint 0x%x found.\n", ep_info->bEndpointAddress);
115 }
116 }
117 if (ep_info == NULL)
118 {
119 HID_DBGMSG("couldn't find an input interrupt endpoint\n");
120 return USB_ERR_NODEV;
121 }
122
123 hid = alloc_hid_device();
124 if (hid == NULL)
125 return USB_ERR_NODEV;
126
127 HID_DBGMSG("New HID device 0x%x.\n", (int)hid);
128
129 hid->ifnum = ifnum;
130 hid->udev = dev;
131 hid->next = NULL;
132 hid->bSubClassCode = ifd->bInterfaceSubClass;
133 hid->bProtocolCode = ifd->bInterfaceProtocol;
134
135 /*
136 * Chaining newly found HID device to end of HID device list.
137 */
138 if (g_hdev_list == NULL)
139 g_hdev_list = hid;
140 else
141 {
142 for (p = g_hdev_list; p->next != NULL; p = p->next)
143 ;
144 p->next = hid;
145 }
146
147 return 0;
148}
149
150
151static void hid_disconnect(USB_DEV_T *dev)
152{
153 HID_DEV_T *hid_dev, *p;
154 int i;
155
156 HID_DBGMSG("HID device disconnected!\n");
157
158 for (i = 0; i < CONFIG_HID_MAX_DEV; i++)
159 {
160 if (g_hid_dev[i].udev == dev)
161 {
162 hid_dev = &g_hid_dev[i];
163
164 if (hid_dev->urbin)
165 {
166 USBH_UnlinkUrb(hid_dev->urbin);
167 USBH_FreeUrb(hid_dev->urbin);
168 }
169 if (hid_dev->urbout)
170 {
171 USBH_UnlinkUrb(hid_dev->urbout);
172 USBH_FreeUrb(hid_dev->urbout);
173 }
174
175 /*
176 * Remove this HID device from HID device list.
177 */
178 if (hid_dev == g_hdev_list)
179 g_hdev_list = g_hdev_list->next;
180 else
181 {
182 for (p = g_hdev_list; p != NULL; p = p->next)
183 {
184 if (p->next == hid_dev)
185 {
186 p->next = hid_dev->next;
187 break;
188 }
189 }
190 }
191 free_hid_device(hid_dev);
192 }
193 }
194}
195
196
197#define USB_INTERFACE_CLASS_HID 3
198
199static USB_DEV_ID_T hid_id_table[] =
200{
201 USB_DEVICE_ID_MATCH_INT_CLASS, /* match_flags */
202 0, 0, 0, 0, 0, 0, 0,
203 USB_INTERFACE_CLASS_HID, /* bInterfaceClass */
204 0, 0, 0
205};
206
207
208static USB_DRIVER_T hid_driver =
209{
210 "hid driver",
211 usbhid_probe,
212 hid_disconnect,
213 hid_id_table,
214 NULL, /* suspend */
215 NULL, /* resume */
216 {NULL,NULL} /* driver_list */
217};
218
219
220EP_INFO_T *hid_get_ep_info(USB_DEV_T *dev, int ifnum, uint16_t dir)
221{
222 int i;
223
224 for (i = 0; i < dev->ep_list_cnt; i++)
225 {
226 if ((dev->ep_list[i].ifnum == ifnum) &&
227 ((dev->ep_list[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
228 {
229 if ((dev->ep_list[i].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir)
230 {
231 //printf("Endpoint found: 0x%x\n", dev->ep_list[i].bEndpointAddress);
232 return &dev->ep_list[i];
233 }
234 }
235 }
236 return NULL;
237}
238
240
241
246 void USBH_HidInit(void)
247{
248 memset((char *)&g_hid_dev[0], 0, sizeof(g_hid_dev));
249 g_hdev_list = NULL;
250 USBH_RegisterDriver(&hid_driver);
251}
252
253
262 HID_DEV_T * USBH_HidGetDeviceList(void)
263{
264 return g_hdev_list;
265}
266
267 /* end of group NUC472_442_USBH_HID_EXPORTED_FUNCTIONS */
269 /* end of group NUC472_442_USBH_HID_Driver */
271 /* end of group NUC472_442_Device_Driver */
273
274/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
275
NUC472_442.h
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
USB_ERR_NODEV
#define USB_ERR_NODEV
Definition: usbh_err_code.h:23
USBH_RegisterDriver
int32_t USBH_RegisterDriver(USB_DRIVER_T *new_driver)
Register a device driver to USB Host Core driver.
Definition: usbh_core.c:78
urb_t::dev
struct usb_device * dev
Definition: usbh_core.h:541
USBH_UnlinkUrb
int32_t USBH_UnlinkUrb(URB_T *urb)
Cancel an URB which has been submit to USB core.
Definition: usbh_core.c:236
USBH_FreeUrb
void USBH_FreeUrb(URB_T *)
Free the URB allocated from USBH_AllocUrb()
Definition: usbh_support.c:113
USB_IF_DESC_T
USB_IF_DESC_T
Definition: usbh_core.h:335
USB_DEV_T
HIDDEN_SYMBOLS struct usb_device USB_DEV_T
CONFIG_HID_MAX_DEV
#define CONFIG_HID_MAX_DEV
Definition: usbh_hid.h:35
usbhid_dev::udev
USB_DEV_T * udev
Definition: usbh_hid.h:93
usbhid_dev::urbin
URB_T * urbin
Definition: usbh_hid.h:97
usbhid_dev::next
struct usbhid_dev * next
Definition: usbh_hid.h:102
usbhid_dev::urbout
URB_T * urbout
Definition: usbh_hid.h:98
USBH_HidInit
void USBH_HidInit(void)
Init USB Host HID driver.
Definition: hid_driver.c:246
usbhid_dev::bProtocolCode
uint8_t bProtocolCode
Definition: usbh_hid.h:96
USBH_HidGetDeviceList
HID_DEV_T * USBH_HidGetDeviceList(void)
Get a list of currently connected USB Hid devices.
Definition: hid_driver.c:262
usbhid_dev::ifnum
int ifnum
Definition: usbh_hid.h:94
usbhid_dev::bSubClassCode
uint8_t bSubClassCode
Definition: usbh_hid.h:95
NULL
#define NULL
NULL pointer.
Definition: NUC472_442.h:29018
TRUE
#define TRUE
Boolean true, define to use in API parameters or return value.
Definition: NUC472_442.h:29021
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: NUC472_442.h:29022
urb_t
Definition: usbh_core.h:537
usbhid_dev
Definition: usbh_hid.h:92
usbh_core.h
USB Host core driver header file.
usbh_hid.h