source: mainline/uspace/drv/hid/usbhid/main.c@ 17c1d9db

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 17c1d9db was 8b71f3e, checked in by Petr Manek <petr.manek@…>, 8 years ago

usbdev: refactor polling more

For clarity, the opaque usb_device_polling_t and its complementary
configuration data structure usb_device_polling_config_t have been
merged into usb_polling_t. All related methods have dropped the
"device" from their prefix as well.

The usage semantics have transitioned to malloc-free model, where the
user is entirely responsible for (de)allocation of the polling structure
and its data buffer, and (de)initialization happens in designated
functions during its lifetime in the system.

In addition, the distinction between mandatory / optional / internal
parameters has been documented. Optional parameters now have default
values, which are set to sensible constants in order to allow dropping
some lines in USB driver implementations.

The drivers usbhid and usbhub were refactored to match the API
changes.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * Copyright (c) 2011 Lubos Slovak
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 * Main routines of USB HID driver.
36 */
37
38#include <ddf/driver.h>
39#include <usb/debug.h>
40#include <errno.h>
41#include <str_error.h>
42
43#include <usb/dev/driver.h>
44#include <usb/dev/poll.h>
45
46#include "usbhid.h"
47
48#define NAME "usbhid"
49
50/**
51 * Callback for passing a new device to the driver.
52 *
53 * @note Currently, only boot-protocol keyboards are supported by this driver.
54 *
55 * @param dev Structure representing the new device.
56 * @return Error code.
57 */
58static int usb_hid_device_add(usb_device_t *dev)
59{
60 usb_log_debug("%s\n", __FUNCTION__);
61
62 if (dev == NULL) {
63 usb_log_error("Wrong parameter given for add_device().\n");
64 return EINVAL;
65 }
66
67 if (usb_device_get_iface_number(dev) < 0) {
68 usb_log_error("Failed to add HID device: endpoints not found."
69 "\n");
70 return ENOTSUP;
71 }
72 usb_hid_dev_t *hid_dev =
73 usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
74 if (hid_dev == NULL) {
75 usb_log_error("Failed to create USB/HID device structure.\n");
76 return ENOMEM;
77 }
78
79 int rc = usb_hid_init(hid_dev, dev);
80 if (rc != EOK) {
81 usb_log_error("Failed to initialize USB/HID device.\n");
82 usb_hid_deinit(hid_dev);
83 return rc;
84 }
85
86 usb_log_debug("USB/HID device structure initialized.\n");
87
88 /* Start automated polling function.
89 * This will create a separate fibril that will query the device
90 * for the data continuously. */
91 rc = usb_polling_start(&hid_dev->polling);
92
93 if (rc != EOK) {
94 usb_log_error("Failed to start polling fibril for `%s'.\n",
95 usb_device_get_name(dev));
96 usb_hid_deinit(hid_dev);
97 return rc;
98 }
99 hid_dev->running = true;
100
101 usb_log_info("HID device `%s' ready.\n", usb_device_get_name(dev));
102
103 return EOK;
104}
105
106static int join_and_clean(usb_device_t *dev)
107{
108 assert(dev);
109 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
110 assert(hid_dev);
111
112 /* Join polling fibril (ignoring error code). */
113 usb_polling_join(&hid_dev->polling);
114
115 /* Clean up. */
116 usb_hid_deinit(hid_dev);
117 usb_log_info("%s destruction complete.\n", usb_device_get_name(dev));
118
119 return EOK;
120}
121
122/**
123 * Callback for a device about to be removed from the driver.
124 *
125 * @param dev Structure representing the device.
126 * @return Error code.
127 */
128static int usb_hid_device_remove(usb_device_t *dev)
129{
130 assert(dev);
131 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
132 assert(hid_dev);
133
134 usb_log_info("Device %s removed.\n", usb_device_get_name(dev));
135 return join_and_clean(dev);
136}
137
138/**
139 * Callback for removing a device from the driver.
140 *
141 * @param dev Structure representing the device.
142 * @return Error code.
143 */
144static int usb_hid_device_gone(usb_device_t *dev)
145{
146 assert(dev);
147 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
148 assert(hid_dev);
149
150 usb_log_info("Device %s gone.\n", usb_device_get_name(dev));
151 return join_and_clean(dev);
152}
153
154/** USB generic driver callbacks */
155static const usb_driver_ops_t usb_hid_driver_ops = {
156 .device_add = usb_hid_device_add,
157 .device_remove = usb_hid_device_remove,
158 .device_gone = usb_hid_device_gone,
159};
160
161/** The driver itself. */
162static const usb_driver_t usb_hid_driver = {
163 .name = NAME,
164 .ops = &usb_hid_driver_ops,
165 .endpoints = usb_hid_endpoints
166};
167
168int main(int argc, char *argv[])
169{
170 printf(NAME ": HelenOS USB HID driver.\n");
171
172 log_init(NAME);
173
174 return usb_driver_main(&usb_hid_driver);
175}
176/**
177 * @}
178 */
Note: See TracBrowser for help on using the repository browser.