source: mainline/uspace/drv/bus/usb/usbhid/generic/hiddev.c@ 9c5fd7a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9c5fd7a was 065064e6, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

usb: Add and use usb_device_data_alloc.

Inspired by ddf_dev_data_alloc and ddf_fun_data_alloc.
Fix possible double free (hid dev and hid fun shared driver_data).

  • 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 .interface_subclass = -1,
55 .interface_protocol = -1,
56 .flags = 0
57};
58
59const char *HID_GENERIC_FUN_NAME = "hid";
60const char *HID_GENERIC_CLASS_NAME = "hid";
61
62/*----------------------------------------------------------------------------*/
63
64static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
65
66static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
67 size_t size, size_t *act_size, int *event_nr, unsigned int flags);
68
69static int usb_generic_hid_client_connected(ddf_fun_t *fun);
70
71static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun);
72
73static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
74 size_t size, size_t *actual_size);
75
76/*----------------------------------------------------------------------------*/
77
78static usbhid_iface_t usb_generic_iface = {
79 .get_event = usb_generic_hid_get_event,
80 .get_event_length = usb_generic_hid_get_event_length,
81 .get_report_descriptor_length = usb_generic_get_report_descriptor_length,
82 .get_report_descriptor = usb_generic_get_report_descriptor
83};
84
85static ddf_dev_ops_t usb_generic_hid_ops = {
86 .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
87 .open = usb_generic_hid_client_connected
88};
89
90/*----------------------------------------------------------------------------*/
91
92static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
93{
94 usb_log_debug2("Generic HID: Get event length (fun: %p, "
95 "fun->driver_data: %p.\n", fun, fun->driver_data);
96
97 if (fun == NULL || fun->driver_data == NULL) {
98 return 0;
99 }
100
101 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
102
103 usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
104 hid_dev, hid_dev->max_input_report_size);
105
106 return hid_dev->max_input_report_size;
107}
108
109/*----------------------------------------------------------------------------*/
110
111static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
112 size_t size, size_t *act_size, int *event_nr, unsigned int flags)
113{
114 usb_log_debug2("Generic HID: Get event.\n");
115
116 if (fun == NULL || fun->driver_data == NULL || buffer == NULL
117 || act_size == NULL || event_nr == NULL) {
118 usb_log_debug("No function");
119 return EINVAL;
120 }
121
122 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
123
124 if (hid_dev->input_report_size > size) {
125 usb_log_debug("input_report_size > size (%zu, %zu)\n",
126 hid_dev->input_report_size, size);
127 return EINVAL; // TODO: other error code
128 }
129
130 /*! @todo This should probably be somehow atomic. */
131 memcpy(buffer, hid_dev->input_report,
132 hid_dev->input_report_size);
133 *act_size = hid_dev->input_report_size;
134 *event_nr = usb_hid_report_number(hid_dev);
135
136 usb_log_debug2("OK\n");
137
138 return EOK;
139}
140
141/*----------------------------------------------------------------------------*/
142
143static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
144{
145 usb_log_debug("Generic HID: Get report descriptor length.\n");
146
147 if (fun == NULL || fun->driver_data == NULL) {
148 usb_log_debug("No function");
149 return EINVAL;
150 }
151
152 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
153
154 usb_log_debug2("hid_dev->report_desc_size = %zu\n",
155 hid_dev->report_desc_size);
156
157 return hid_dev->report_desc_size;
158}
159
160/*----------------------------------------------------------------------------*/
161
162static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
163 size_t size, size_t *actual_size)
164{
165 usb_log_debug2("Generic HID: Get report descriptor.\n");
166
167 if (fun == NULL || fun->driver_data == NULL) {
168 usb_log_debug("No function");
169 return EINVAL;
170 }
171
172 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
173
174 if (hid_dev->report_desc_size > size) {
175 return EINVAL;
176 }
177
178 memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
179 *actual_size = hid_dev->report_desc_size;
180
181 return EOK;
182}
183
184/*----------------------------------------------------------------------------*/
185
186static int usb_generic_hid_client_connected(ddf_fun_t *fun)
187{
188 usb_log_debug("Generic HID: Client connected.\n");
189 return EOK;
190}
191
192/*----------------------------------------------------------------------------*/
193
194void usb_generic_hid_deinit(usb_hid_dev_t *hid_dev, void *data)
195{
196 ddf_fun_t *fun = data;
197 const int ret = ddf_fun_unbind(fun);
198 if (ret != EOK) {
199 usb_log_error("Failed to unbind generic hid fun.\n");
200 return;
201 }
202 usb_log_debug2("%s unbound.\n", fun->name);
203 /* We did not allocate this, so leave this alone
204 * the device would take care of it */
205 fun->driver_data = NULL;
206 ddf_fun_destroy(fun);
207}
208
209/*----------------------------------------------------------------------------*/
210
211int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
212{
213 if (hid_dev == NULL) {
214 return EINVAL;
215 }
216
217 /* Create the exposed function. */
218 /** @todo Generate numbers for the devices? */
219 usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
220 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
221 HID_GENERIC_FUN_NAME);
222 if (fun == NULL) {
223 usb_log_error("Could not create DDF function node.\n");
224 return ENOMEM;
225 }
226
227 fun->ops = &usb_generic_hid_ops;
228
229 int rc = ddf_fun_bind(fun);
230 if (rc != EOK) {
231 usb_log_error("Could not bind DDF function: %s.\n",
232 str_error(rc));
233 ddf_fun_destroy(fun);
234 return rc;
235 }
236 /* This is nasty both device and this function have the same
237 * driver data, thus destruction would lead to double free */
238 fun->driver_data = hid_dev;
239
240 usb_log_debug("HID function created. Handle: %" PRIun "\n", fun->handle);
241 *data = fun;
242
243 return EOK;
244}
245
246/*----------------------------------------------------------------------------*/
247
248bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data)
249{
250 return true;
251}
252
253/**
254 * @}
255 */
Note: See TracBrowser for help on using the repository browser.