1 | /*
|
---|
2 | * Copyright (c) 2011 Lubos Slovak
|
---|
3 | * Copyright (c) 2018 Petr Manek
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup drvusbhid
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * USB HID driver API.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef USB_HID_USBHID_H_
|
---|
38 | #define USB_HID_USBHID_H_
|
---|
39 |
|
---|
40 | #include <stdint.h>
|
---|
41 |
|
---|
42 | #include <usb/hid/hidparser.h>
|
---|
43 | #include <ddf/driver.h>
|
---|
44 | #include <usb/dev/pipes.h>
|
---|
45 | #include <usb/dev/driver.h>
|
---|
46 | #include <usb/dev/poll.h>
|
---|
47 | #include <usb/hid/hid.h>
|
---|
48 | #include <stdbool.h>
|
---|
49 |
|
---|
50 | typedef struct usb_hid_dev usb_hid_dev_t;
|
---|
51 | typedef struct usb_hid_subdriver usb_hid_subdriver_t;
|
---|
52 |
|
---|
53 | /** Subdriver initialization callback.
|
---|
54 | *
|
---|
55 | * @param dev Backing USB HID device.
|
---|
56 | * @param data Custom subdriver data (pointer where to store them).
|
---|
57 | * @return Error code.
|
---|
58 | */
|
---|
59 | typedef errno_t (*usb_hid_driver_init_t)(usb_hid_dev_t *dev, void **data);
|
---|
60 |
|
---|
61 | /** Subdriver deinitialization callback.
|
---|
62 | *
|
---|
63 | * @param dev Backing USB HID device.
|
---|
64 | * @param data Custom subdriver data.
|
---|
65 | */
|
---|
66 | typedef void (*usb_hid_driver_deinit_t)(usb_hid_dev_t *dev, void *data);
|
---|
67 |
|
---|
68 | /** Subdriver callback on data from device.
|
---|
69 | *
|
---|
70 | * @param dev Backing USB HID device.
|
---|
71 | * @param data Custom subdriver data.
|
---|
72 | * @return Whether to continue polling (typically true always).
|
---|
73 | */
|
---|
74 | typedef bool (*usb_hid_driver_poll_t)(usb_hid_dev_t *dev, void *data);
|
---|
75 |
|
---|
76 | /** Subdriver callback after communication with the device ceased.
|
---|
77 | *
|
---|
78 | * @param dev Backing USB HID device.
|
---|
79 | * @param data Custom subdriver data.
|
---|
80 | * @param ended_due_to_errors Whether communication ended due to errors in
|
---|
81 | * communication (true) or deliberately by driver (false).
|
---|
82 | */
|
---|
83 | typedef void (*usb_hid_driver_poll_ended_t)(usb_hid_dev_t *dev, void *data,
|
---|
84 | bool ended_due_to_errors);
|
---|
85 |
|
---|
86 | struct usb_hid_subdriver {
|
---|
87 | /** Function to be called when initializing HID device. */
|
---|
88 | usb_hid_driver_init_t init;
|
---|
89 | /** Function to be called when destroying the HID device structure. */
|
---|
90 | usb_hid_driver_deinit_t deinit;
|
---|
91 | /** Function to be called when data arrives from the device. */
|
---|
92 | usb_hid_driver_poll_t poll;
|
---|
93 | /** Function to be called when polling ends. */
|
---|
94 | usb_hid_driver_poll_ended_t poll_end;
|
---|
95 | /** Arbitrary data needed by the subdriver. */
|
---|
96 | void *data;
|
---|
97 | };
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Structure for holding general HID device data.
|
---|
101 | */
|
---|
102 | struct usb_hid_dev {
|
---|
103 | /** Structure holding generic USB device information. */
|
---|
104 | usb_device_t *usb_dev;
|
---|
105 |
|
---|
106 | /** Endpont mapping of the polling pipe. */
|
---|
107 | usb_endpoint_mapping_t *poll_pipe_mapping;
|
---|
108 |
|
---|
109 | /** Device polling structure. */
|
---|
110 | usb_polling_t polling;
|
---|
111 |
|
---|
112 | /** Subdrivers. */
|
---|
113 | usb_hid_subdriver_t *subdrivers;
|
---|
114 |
|
---|
115 | /** Number of subdrivers. */
|
---|
116 | unsigned subdriver_count;
|
---|
117 |
|
---|
118 | /** Report descriptor. */
|
---|
119 | uint8_t *report_desc;
|
---|
120 |
|
---|
121 | /** Report descriptor size. */
|
---|
122 | size_t report_desc_size;
|
---|
123 |
|
---|
124 | /** HID Report parser. */
|
---|
125 | usb_hid_report_t report;
|
---|
126 |
|
---|
127 | uint8_t report_id;
|
---|
128 |
|
---|
129 | uint8_t *input_report;
|
---|
130 |
|
---|
131 | size_t input_report_size;
|
---|
132 | size_t max_input_report_size;
|
---|
133 |
|
---|
134 | int report_nr;
|
---|
135 | volatile bool running;
|
---|
136 | };
|
---|
137 |
|
---|
138 | extern const usb_endpoint_description_t *usb_hid_endpoints[];
|
---|
139 |
|
---|
140 | errno_t usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev);
|
---|
141 |
|
---|
142 | void usb_hid_deinit(usb_hid_dev_t *hid_dev);
|
---|
143 |
|
---|
144 | void usb_hid_new_report(usb_hid_dev_t *hid_dev);
|
---|
145 |
|
---|
146 | int usb_hid_report_number(const usb_hid_dev_t *hid_dev);
|
---|
147 |
|
---|
148 | #endif /* USB_HID_USBHID_H_ */
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * @}
|
---|
152 | */
|
---|