source: mainline/uspace/drv/hid/usbhid/generic/hiddev.c

Last change on this file was d1582b50, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Fix spacing in single-line comments using latest ccheck

This found incorrectly formatted section comments (with blocks of
asterisks or dashes). I strongly believe against using section comments
but I am not simply removing them since that would probably be
controversial.

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