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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since beb9336 was 56fd7cf, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

  • Property mode set to 100644
File size: 6.5 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
[56fd7cf]37/* XXX Fix this */
38#define _DDF_DATA_IMPLANT
39
[61257f4]40#include <usb/debug.h>
41#include <usb/classes/classes.h>
[3facf63a]42#include <errno.h>
43#include <str_error.h>
[dd3eda2]44#include <bool.h>
[3facf63a]45
46#include <usbhid_iface.h>
[61257f4]47
[dd10e07]48#include "hiddev.h"
[60c0573]49#include "usbhid.h"
[61257f4]50
[76fbd9a]51
[61257f4]52
[b803845]53const usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
[61257f4]54 .transfer_type = USB_TRANSFER_INTERRUPT,
55 .direction = USB_DIRECTION_IN,
56 .interface_class = USB_CLASS_HID,
[aaf835d]57 .interface_subclass = -1,
58 .interface_protocol = -1,
[61257f4]59 .flags = 0
60};
61
62const char *HID_GENERIC_FUN_NAME = "hid";
63const char *HID_GENERIC_CLASS_NAME = "hid";
64
[76fbd9a]65
[3facf63a]66static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
[5f047d0]67static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
[266fcd8]68 size_t size, size_t *act_size, int *event_nr, unsigned int flags);
[dd3eda2]69static int usb_generic_hid_client_connected(ddf_fun_t *fun);
[d7c72db]70static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun);
[5f047d0]71static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
[d7c72db]72 size_t size, size_t *actual_size);
[76fbd9a]73
[3facf63a]74static usbhid_iface_t usb_generic_iface = {
75 .get_event = usb_generic_hid_get_event,
[d7c72db]76 .get_event_length = usb_generic_hid_get_event_length,
77 .get_report_descriptor_length = usb_generic_get_report_descriptor_length,
78 .get_report_descriptor = usb_generic_get_report_descriptor
[3facf63a]79};
[76fbd9a]80
[3facf63a]81static ddf_dev_ops_t usb_generic_hid_ops = {
[dd3eda2]82 .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
83 .open = usb_generic_hid_client_connected
[3facf63a]84};
[76fbd9a]85
[56fd7cf]86/** Return hid_dev_t * for generic HID function node.
87 *
88 * For the generic HID subdriver the 'hid' function has usb_hid_gen_fun_t
89 * as soft state. Through that we can get to the usb_hid_dev_t.
90 */
91static usb_hid_dev_t *fun_hid_dev(ddf_fun_t *fun)
92{
93 return ((usb_hid_gen_fun_t *)ddf_fun_data_get(fun))->hid_dev;
94}
95
[3facf63a]96static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
97{
[ff41576d]98 usb_log_debug2("Generic HID: Get event length (fun: %p, "
[56fd7cf]99 "fun->driver_data: %p.\n", fun, ddf_fun_data_get(fun));
[3facf63a]100
[56fd7cf]101 const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
[cc29622]102
[0d59d0e9]103 usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
[4e78236]104 hid_dev, hid_dev->max_input_report_size);
[cc29622]105
[4e78236]106 return hid_dev->max_input_report_size;
[3facf63a]107}
[76fbd9a]108
[5f047d0]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
[56fd7cf]114 if (buffer == NULL || act_size == NULL || event_nr == NULL) {
[4e78236]115 usb_log_debug("No function");
[3facf63a]116 return EINVAL;
117 }
118
[56fd7cf]119 const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
[cc29622]120
[3facf63a]121 if (hid_dev->input_report_size > size) {
[5f047d0]122 usb_log_debug("input_report_size > size (%zu, %zu)\n",
[da56be2]123 hid_dev->input_report_size, size);
[3facf63a]124 return EINVAL; // TODO: other error code
125 }
[cc29622]126
[19e0560e]127 /*! @todo This should probably be somehow atomic. */
[5f047d0]128 memcpy(buffer, hid_dev->input_report,
[ff41576d]129 hid_dev->input_report_size);
130 *act_size = hid_dev->input_report_size;
[266fcd8]131 *event_nr = usb_hid_report_number(hid_dev);
[cc29622]132
[ff41576d]133 usb_log_debug2("OK\n");
[cc29622]134
[3facf63a]135 return EOK;
136}
[76fbd9a]137
[d7c72db]138static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
139{
140 usb_log_debug("Generic HID: Get report descriptor length.\n");
[cc29622]141
[56fd7cf]142 const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
[cc29622]143
[5f047d0]144 usb_log_debug2("hid_dev->report_desc_size = %zu\n",
[ff41576d]145 hid_dev->report_desc_size);
[cc29622]146
[d7c72db]147 return hid_dev->report_desc_size;
148}
[76fbd9a]149
[5f047d0]150static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
[d7c72db]151 size_t size, size_t *actual_size)
152{
[ff41576d]153 usb_log_debug2("Generic HID: Get report descriptor.\n");
[cc29622]154
[56fd7cf]155 const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
[cc29622]156
[d7c72db]157 if (hid_dev->report_desc_size > size) {
[19e0560e]158 return EINVAL;
[d7c72db]159 }
[cc29622]160
[d7c72db]161 memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
162 *actual_size = hid_dev->report_desc_size;
[cc29622]163
[d7c72db]164 return EOK;
165}
[76fbd9a]166
[dd3eda2]167static int usb_generic_hid_client_connected(ddf_fun_t *fun)
168{
[4939490]169 usb_log_debug("Generic HID: Client connected.\n");
[dd3eda2]170 return EOK;
171}
[76fbd9a]172
[c5b6db53]173void usb_generic_hid_deinit(usb_hid_dev_t *hid_dev, void *data)
174{
175 ddf_fun_t *fun = data;
[5f047d0]176 if (fun == NULL)
177 return;
178
[a0c05e7]179 if (ddf_fun_unbind(fun) != EOK) {
[2a5b62b]180 usb_log_error("Failed to unbind generic hid fun.\n");
[c5b6db53]181 return;
182 }
[56fd7cf]183 usb_log_debug2("%s unbound.\n", ddf_fun_get_name(fun));
[c5b6db53]184 ddf_fun_destroy(fun);
185}
[76fbd9a]186
[c5b6db53]187int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
188{
[56fd7cf]189 usb_hid_gen_fun_t *hid_fun;
190
[c5b6db53]191 if (hid_dev == NULL) {
192 return EINVAL;
193 }
194
[15f3c3f]195 /* Create the exposed function. */
[3facf63a]196 usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
197 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
198 HID_GENERIC_FUN_NAME);
199 if (fun == NULL) {
200 usb_log_error("Could not create DDF function node.\n");
201 return ENOMEM;
202 }
[cc29622]203
[56fd7cf]204 /* Create softstate */
205 hid_fun = ddf_fun_data_alloc(fun, sizeof(usb_hid_gen_fun_t));
206 hid_fun->hid_dev = hid_dev;
207 ddf_fun_set_ops(fun, &usb_generic_hid_ops);
[3facf63a]208
209 int rc = ddf_fun_bind(fun);
210 if (rc != EOK) {
211 usb_log_error("Could not bind DDF function: %s.\n",
212 str_error(rc));
213 ddf_fun_destroy(fun);
214 return rc;
215 }
[cc29622]216
[56fd7cf]217 usb_log_debug("HID function created. Handle: %" PRIun "\n",
218 ddf_fun_get_handle(fun));
[c5b6db53]219 *data = fun;
[cc29622]220
[3facf63a]221 return EOK;
222}
[76fbd9a]223
[4d3c13e]224bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data)
[61257f4]225{
226 return true;
227}
228/**
229 * @}
230 */
Note: See TracBrowser for help on using the repository browser.