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

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

usbhid: unbind generic hid DDF function.

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[61257f4]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>
[3facf63a]39#include <errno.h>
40#include <str_error.h>
[dd3eda2]41#include <bool.h>
[3facf63a]42
43#include <usbhid_iface.h>
[61257f4]44
[dd10e07]45#include "hiddev.h"
[60c0573]46#include "usbhid.h"
[61257f4]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
[3facf63a]62static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
63
[d7c72db]64static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
[266fcd8]65 size_t size, size_t *act_size, int *event_nr, unsigned int flags);
[3facf63a]66
[dd3eda2]67static int usb_generic_hid_client_connected(ddf_fun_t *fun);
68
[d7c72db]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
[3facf63a]74/*----------------------------------------------------------------------------*/
75
76static usbhid_iface_t usb_generic_iface = {
77 .get_event = usb_generic_hid_get_event,
[d7c72db]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
[3facf63a]81};
82
83static ddf_dev_ops_t usb_generic_hid_ops = {
[dd3eda2]84 .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
85 .open = usb_generic_hid_client_connected
[3facf63a]86};
87
88/*----------------------------------------------------------------------------*/
89
90static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
91{
[ff41576d]92 usb_log_debug2("Generic HID: Get event length (fun: %p, "
[4e78236]93 "fun->driver_data: %p.\n", fun, fun->driver_data);
[cc29622]94
[4e78236]95 if (fun == NULL || fun->driver_data == NULL) {
[3facf63a]96 return 0;
97 }
98
99 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
[cc29622]100
[0d59d0e9]101 usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
[4e78236]102 hid_dev, hid_dev->max_input_report_size);
[cc29622]103
[4e78236]104 return hid_dev->max_input_report_size;
[3facf63a]105}
106
107/*----------------------------------------------------------------------------*/
108
[d7c72db]109static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
[266fcd8]110 size_t size, size_t *act_size, int *event_nr, unsigned int flags)
[3facf63a]111{
[ff41576d]112 usb_log_debug2("Generic HID: Get event.\n");
[cc29622]113
[266fcd8]114 if (fun == NULL || fun->driver_data == NULL || buffer == NULL
115 || act_size == NULL || event_nr == NULL) {
[4e78236]116 usb_log_debug("No function");
[3facf63a]117 return EINVAL;
118 }
119
120 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
[cc29622]121
[3facf63a]122 if (hid_dev->input_report_size > size) {
[da56be2]123 usb_log_debug("input_report_size > size (%zu, %zu)\n",
124 hid_dev->input_report_size, size);
[3facf63a]125 return EINVAL; // TODO: other error code
126 }
[cc29622]127
[19e0560e]128 /*! @todo This should probably be somehow atomic. */
[ff41576d]129 memcpy(buffer, hid_dev->input_report,
130 hid_dev->input_report_size);
131 *act_size = hid_dev->input_report_size;
[266fcd8]132 *event_nr = usb_hid_report_number(hid_dev);
[cc29622]133
[ff41576d]134 usb_log_debug2("OK\n");
[cc29622]135
[3facf63a]136 return EOK;
137}
138
139/*----------------------------------------------------------------------------*/
140
[d7c72db]141static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
142{
143 usb_log_debug("Generic HID: Get report descriptor length.\n");
[cc29622]144
[d7c72db]145 if (fun == NULL || fun->driver_data == NULL) {
146 usb_log_debug("No function");
147 return EINVAL;
148 }
[cc29622]149
[d7c72db]150 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
[cc29622]151
[ff41576d]152 usb_log_debug2("hid_dev->report_desc_size = %zu\n",
153 hid_dev->report_desc_size);
[cc29622]154
[d7c72db]155 return hid_dev->report_desc_size;
156}
157
158/*----------------------------------------------------------------------------*/
159
160static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
161 size_t size, size_t *actual_size)
162{
[ff41576d]163 usb_log_debug2("Generic HID: Get report descriptor.\n");
[cc29622]164
[d7c72db]165 if (fun == NULL || fun->driver_data == NULL) {
166 usb_log_debug("No function");
167 return EINVAL;
168 }
[cc29622]169
[d7c72db]170 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
[cc29622]171
[d7c72db]172 if (hid_dev->report_desc_size > size) {
[19e0560e]173 return EINVAL;
[d7c72db]174 }
[cc29622]175
[d7c72db]176 memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
177 *actual_size = hid_dev->report_desc_size;
[cc29622]178
[d7c72db]179 return EOK;
180}
181
182/*----------------------------------------------------------------------------*/
183
[dd3eda2]184static int usb_generic_hid_client_connected(ddf_fun_t *fun)
185{
[4939490]186 usb_log_debug("Generic HID: Client connected.\n");
[dd3eda2]187 return EOK;
188}
189
190/*----------------------------------------------------------------------------*/
191
[c5b6db53]192void usb_generic_hid_deinit(usb_hid_dev_t *hid_dev, void *data)
193{
194 ddf_fun_t *fun = data;
195 const int ret = ddf_fun_unbind(fun);
196 if (ret != EOK) {
197 usb_log_error("ailed to unbind generic hid fun.\n");
198 return;
199 }
200 ddf_fun_destroy(fun);
201}
202
203/*----------------------------------------------------------------------------*/
204
205int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
206{
207 if (hid_dev == NULL) {
208 return EINVAL;
209 }
210
[15f3c3f]211 /* Create the exposed function. */
[3facf63a]212 /** @todo Generate numbers for the devices? */
213 usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
214 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
215 HID_GENERIC_FUN_NAME);
216 if (fun == NULL) {
217 usb_log_error("Could not create DDF function node.\n");
218 return ENOMEM;
219 }
[cc29622]220
[4939490]221 fun->ops = &usb_generic_hid_ops;
222 fun->driver_data = hid_dev;
[3facf63a]223
224 int rc = ddf_fun_bind(fun);
225 if (rc != EOK) {
226 usb_log_error("Could not bind DDF function: %s.\n",
227 str_error(rc));
228 ddf_fun_destroy(fun);
229 return rc;
230 }
[cc29622]231
[0d59d0e9]232 usb_log_debug("HID function created. Handle: %" PRIun "\n", fun->handle);
[c5b6db53]233 *data = fun;
[cc29622]234
[3facf63a]235 return EOK;
236}
237
238/*----------------------------------------------------------------------------*/
239
[4d3c13e]240bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data)
[61257f4]241{
242 return true;
243}
244
245/**
246 * @}
247 */
Note: See TracBrowser for help on using the repository browser.