source: mainline/uspace/drv/usbhid/generic/hiddev.c@ ff41576d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ff41576d was ff41576d, checked in by Lubos Slovak <lubos.slovak@…>, 14 years ago

Edited log messages + changed way of getting reports (get_event()).

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2011 Lubos Slovak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup drvusbhid
30 * @{
31 */
32/**
33 * @file
34 * USB HID driver API.
35 */
36
37#include <usb/debug.h>
38#include <usb/classes/classes.h>
39#include <errno.h>
40#include <str_error.h>
41#include <bool.h>
42
43#include <usbhid_iface.h>
44
45#include "hiddev.h"
46#include "usbhid.h"
47
48/*----------------------------------------------------------------------------*/
49
50usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
51 .transfer_type = USB_TRANSFER_INTERRUPT,
52 .direction = USB_DIRECTION_IN,
53 .interface_class = USB_CLASS_HID,
54 .flags = 0
55};
56
57const char *HID_GENERIC_FUN_NAME = "hid";
58const char *HID_GENERIC_CLASS_NAME = "hid";
59
60/*----------------------------------------------------------------------------*/
61
62static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
63
64static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
65 size_t size, size_t *act_size, unsigned int flags);
66
67static int usb_generic_hid_client_connected(ddf_fun_t *fun);
68
69static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun);
70
71static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
72 size_t size, size_t *actual_size);
73
74/*----------------------------------------------------------------------------*/
75
76static usbhid_iface_t usb_generic_iface = {
77 .get_event = usb_generic_hid_get_event,
78 .get_event_length = usb_generic_hid_get_event_length,
79 .get_report_descriptor_length = usb_generic_get_report_descriptor_length,
80 .get_report_descriptor = usb_generic_get_report_descriptor
81};
82
83static ddf_dev_ops_t usb_generic_hid_ops = {
84 .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
85 .open = usb_generic_hid_client_connected
86};
87
88/*----------------------------------------------------------------------------*/
89
90static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
91{
92 usb_log_debug2("Generic HID: Get event length (fun: %p, "
93 "fun->driver_data: %p.\n", fun, fun->driver_data);
94
95 if (fun == NULL || fun->driver_data == NULL) {
96 return 0;
97 }
98
99 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
100
101 usb_log_debug2("hid_dev: %p, Max input report size (%d).\n",
102 hid_dev, hid_dev->max_input_report_size);
103
104 return hid_dev->max_input_report_size;
105}
106
107/*----------------------------------------------------------------------------*/
108
109static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
110 size_t size, size_t *act_size, unsigned int flags)
111{
112 usb_log_debug2("Generic HID: Get event.\n");
113
114 if (fun == NULL || fun->driver_data == NULL) {
115 usb_log_debug("No function");
116 return EINVAL;
117 }
118
119 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
120
121 if (hid_dev->input_report_size > size) {
122 usb_log_debug("input_report_size > size (%zu, %zu)\n",
123 hid_dev->input_report_size, size);
124 return EINVAL; // TODO: other error code
125 }
126
127 /*! @todo This should probably be atomic. */
128// if (usb_hid_report_ready()) {
129// usb_log_debug2("Report ready, size: %zu\n",
130// hid_dev->input_report_size);
131
132// usb_hid_report_received();
133// } else {
134// memset(buffer, 0, hid_dev->input_report_size);
135// }
136 memcpy(buffer, hid_dev->input_report,
137 hid_dev->input_report_size);
138 *act_size = hid_dev->input_report_size;
139
140 // clear the buffer so that it will not be received twice
141 //memset(hid_dev->input_report, 0, hid_dev->input_report_size);
142
143 // note that we already received this report
144// report_received = true;
145 usb_log_debug2("OK\n");
146
147 return EOK;
148}
149
150/*----------------------------------------------------------------------------*/
151
152static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
153{
154 usb_log_debug("Generic HID: Get report descriptor length.\n");
155
156 if (fun == NULL || fun->driver_data == NULL) {
157 usb_log_debug("No function");
158 return EINVAL;
159 }
160
161 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
162
163 usb_log_debug2("hid_dev->report_desc_size = %zu\n",
164 hid_dev->report_desc_size);
165
166 return hid_dev->report_desc_size;
167}
168
169/*----------------------------------------------------------------------------*/
170
171static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
172 size_t size, size_t *actual_size)
173{
174 usb_log_debug2("Generic HID: Get report descriptor.\n");
175
176 if (fun == NULL || fun->driver_data == NULL) {
177 usb_log_debug("No function");
178 return EINVAL;
179 }
180
181 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
182
183 if (hid_dev->report_desc_size > size) {
184 return EINVAL; // TODO: other error code
185 }
186
187 memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
188 *actual_size = hid_dev->report_desc_size;
189
190 return EOK;
191}
192
193/*----------------------------------------------------------------------------*/
194
195static int usb_generic_hid_client_connected(ddf_fun_t *fun)
196{
197 usb_log_debug("Generic HID: Client connected.\n");
198 return EOK;
199}
200
201/*----------------------------------------------------------------------------*/
202
203static int usb_generic_hid_create_function(usb_hid_dev_t *hid_dev)
204{
205 /* Create the function exposed under /dev/devices. */
206 /** @todo Generate numbers for the devices? */
207 usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
208 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
209 HID_GENERIC_FUN_NAME);
210 if (fun == NULL) {
211 usb_log_error("Could not create DDF function node.\n");
212 return ENOMEM;
213 }
214
215 fun->ops = &usb_generic_hid_ops;
216 fun->driver_data = hid_dev;
217
218 int rc = ddf_fun_bind(fun);
219 if (rc != EOK) {
220 usb_log_error("Could not bind DDF function: %s.\n",
221 str_error(rc));
222 ddf_fun_destroy(fun);
223 return rc;
224 }
225
226 usb_log_debug("HID function created. Handle: %d\n", fun->handle);
227
228 return EOK;
229}
230
231/*----------------------------------------------------------------------------*/
232
233int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
234{
235 if (hid_dev == NULL) {
236 return EINVAL;
237 }
238
239 return usb_generic_hid_create_function(hid_dev);
240}
241
242/*----------------------------------------------------------------------------*/
243
244bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data,
245 uint8_t *buffer, size_t buffer_size)
246{
247 usb_log_debug2("usb_hid_polling_callback(%p, %p, %zu)\n",
248 hid_dev, buffer, buffer_size);
249 usb_debug_str_buffer(buffer, buffer_size, 0);
250 return true;
251}
252
253/**
254 * @}
255 */
Note: See TracBrowser for help on using the repository browser.