source: mainline/uspace/drv/hid/usbhid/main.c@ 35c37fc

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

usbdev: refactoring

The device_rem driver callback was renamed to device_remove. Also,
a new device_removed callback was introduced. This callback will fire
after all device pipes (endpoints, internally) are unregistered from
the HC device after a call to device_remove. It is semantically
a better opportunity for the USB device driver to free resources and
join all fibrils, since endpoint unregistration will force abort all
active transfers, waking up their issuers.

  • Property mode set to 100644
File size: 5.1 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_device_auto_poll_desc(dev,
92 /* Index of the polling pipe. */
93 hid_dev->poll_pipe_mapping->description,
94 /* Callback when data arrives. */
95 usb_hid_polling_callback,
96 /* How much data to request. */
97 hid_dev->poll_pipe_mapping->pipe.desc.max_packet_size,
98 /* Delay */
99 -1,
100 /* Callback when the polling ends. */
101 usb_hid_polling_ended_callback,
102 /* Custom argument. */
103 hid_dev);
104
105 if (rc != EOK) {
106 usb_log_error("Failed to start polling fibril for `%s'.\n",
107 usb_device_get_name(dev));
108 usb_hid_deinit(hid_dev);
109 return rc;
110 }
111 hid_dev->running = true;
112
113 usb_log_info("HID device `%s' ready.\n", usb_device_get_name(dev));
114
115 return EOK;
116}
117
118/**
119 * Callback for a device about to be removed from the driver.
120 *
121 * @param dev Structure representing the device.
122 * @return Error code.
123 */
124static int usb_hid_device_remove(usb_device_t *dev)
125{
126 assert(dev);
127 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
128 assert(hid_dev);
129
130 /* TODO: Stop device polling prior to deinit. Now it fails on endpoint error. */
131
132 usb_hid_deinit(hid_dev);
133 usb_log_debug2("%s destruction complete.\n", usb_device_get_name(dev));
134 return EOK;
135}
136
137/**
138 * Callback for removing a device from the driver.
139 *
140 * @param dev Structure representing the device.
141 * @return Error code.
142 */
143static int usb_hid_device_gone(usb_device_t *dev)
144{
145 assert(dev);
146 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
147 assert(hid_dev);
148 unsigned tries = 100;
149 /* Wait for fail. */
150 while (hid_dev->running && tries--) {
151 async_usleep(100000);
152 }
153 if (hid_dev->running) {
154 usb_log_error("Can't remove hid, still running.\n");
155 return EBUSY;
156 }
157
158 usb_hid_deinit(hid_dev);
159 usb_log_debug2("%s destruction complete.\n", usb_device_get_name(dev));
160 return EOK;
161}
162
163/** USB generic driver callbacks */
164static const usb_driver_ops_t usb_hid_driver_ops = {
165 .device_add = usb_hid_device_add,
166 .device_remove = usb_hid_device_remove,
167 .device_gone = usb_hid_device_gone,
168};
169
170/** The driver itself. */
171static const usb_driver_t usb_hid_driver = {
172 .name = NAME,
173 .ops = &usb_hid_driver_ops,
174 .endpoints = usb_hid_endpoints
175};
176
177int main(int argc, char *argv[])
178{
179 printf(NAME ": HelenOS USB HID driver.\n");
180
181 log_init(NAME);
182
183 return usb_driver_main(&usb_hid_driver);
184}
185/**
186 * @}
187 */
Note: See TracBrowser for help on using the repository browser.