source: mainline/uspace/drv/hid/usbhid/main.c@ 1433ecda

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • 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 * Copyright (c) 2018 Petr Manek, Ondrej Hlavaty
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup drvusbhid
32 * @{
33 */
34/**
35 * @file
36 * Main routines of USB HID driver.
37 */
38
39#include <ddf/driver.h>
40#include <usb/debug.h>
41#include <errno.h>
42#include <str_error.h>
43
44#include <usb/dev/driver.h>
45#include <usb/dev/poll.h>
46
47#include "usbhid.h"
48
49#define NAME "usbhid"
50
51/**
52 * Callback for passing a new device to the driver.
53 *
54 * @note Currently, only boot-protocol keyboards are supported by this driver.
55 *
56 * @param dev Structure representing the new device.
57 * @return Error code.
58 */
59static errno_t usb_hid_device_add(usb_device_t *dev)
60{
61 usb_log_debug("%s", __FUNCTION__);
62
63 if (dev == NULL) {
64 usb_log_error("Wrong parameter given for add_device().");
65 return EINVAL;
66 }
67
68 if (usb_device_get_iface_number(dev) < 0) {
69 usb_log_error("Failed to add HID device: endpoints not found."
70 "\n");
71 return ENOTSUP;
72 }
73 usb_hid_dev_t *hid_dev =
74 usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
75 if (hid_dev == NULL) {
76 usb_log_error("Failed to create USB/HID device structure.");
77 return ENOMEM;
78 }
79
80 errno_t rc = usb_hid_init(hid_dev, dev);
81 if (rc != EOK) {
82 usb_log_error("Failed to initialize USB/HID device.");
83 usb_hid_deinit(hid_dev);
84 return rc;
85 }
86
87 usb_log_debug("USB/HID device structure initialized.");
88
89 /* Start automated polling function.
90 * This will create a separate fibril that will query the device
91 * for the data continuously. */
92 rc = usb_polling_start(&hid_dev->polling);
93
94 if (rc != EOK) {
95 usb_log_error("Failed to start polling fibril for `%s'.",
96 usb_device_get_name(dev));
97 usb_hid_deinit(hid_dev);
98 return rc;
99 }
100 hid_dev->running = true;
101
102 usb_log_info("HID device `%s' ready.", usb_device_get_name(dev));
103
104 return EOK;
105}
106
107static errno_t join_and_clean(usb_device_t *dev)
108{
109 assert(dev);
110 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
111 assert(hid_dev);
112
113 /* Join polling fibril (ignoring error code). */
114 usb_polling_join(&hid_dev->polling);
115
116 /* Clean up. */
117 usb_hid_deinit(hid_dev);
118 usb_log_info("%s destruction complete.", usb_device_get_name(dev));
119
120 return EOK;
121}
122
123/**
124 * Callback for a device about to be removed from the driver.
125 *
126 * @param dev Structure representing the device.
127 * @return Error code.
128 */
129static errno_t usb_hid_device_remove(usb_device_t *dev)
130{
131 assert(dev);
132 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
133 assert(hid_dev);
134
135 usb_log_info("Device %s removed.", usb_device_get_name(dev));
136 return join_and_clean(dev);
137}
138
139/**
140 * Callback for removing a device from the driver.
141 *
142 * @param dev Structure representing the device.
143 * @return Error code.
144 */
145static errno_t usb_hid_device_gone(usb_device_t *dev)
146{
147 assert(dev);
148 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
149 assert(hid_dev);
150
151 usb_log_info("Device %s gone.", usb_device_get_name(dev));
152 return join_and_clean(dev);
153}
154
155/** USB generic driver callbacks */
156static const usb_driver_ops_t usb_hid_driver_ops = {
157 .device_add = usb_hid_device_add,
158 .device_remove = usb_hid_device_remove,
159 .device_gone = usb_hid_device_gone,
160};
161
162/** The driver itself. */
163static const usb_driver_t usb_hid_driver = {
164 .name = NAME,
165 .ops = &usb_hid_driver_ops,
166 .endpoints = usb_hid_endpoints
167};
168
169int main(int argc, char *argv[])
170{
171 printf(NAME ": HelenOS USB HID driver.\n");
172
173 log_init(NAME);
174
175 return usb_driver_main(&usb_hid_driver);
176}
177/**
178 * @}
179 */
Note: See TracBrowser for help on using the repository browser.