source: mainline/uspace/drv/bus/usb/usbhid/main.c@ 065064e6

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 065064e6 was 065064e6, checked in by Jan Vesely <jano.vesely@…>, 15 years ago

usb: Add and use usb_device_data_alloc.

Inspired by ddf_dev_data_alloc and ddf_fun_data_alloc.
Fix possible double free (hid dev and hid fun shared driver_data).

  • Property mode set to 100644
File size: 6.8 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/*----------------------------------------------------------------------------*/
49
50#define NAME "usbhid"
51
52/**
53 * Function for adding a new device of type USB/HID/keyboard.
54 *
55 * This functions initializes required structures from the device's descriptors
56 * and starts new fibril for polling the keyboard for events and another one for
57 * handling auto-repeat of keys.
58 *
59 * During initialization, the keyboard is switched into boot protocol, the idle
60 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
61 * when a key is pressed or released. Finally, the LED lights are turned on
62 * according to the default setup of lock keys.
63 *
64 * @note By default, the keyboards is initialized with Num Lock turned on and
65 * other locks turned off.
66 * @note Currently supports only boot-protocol keyboards.
67 *
68 * @param dev Device to add.
69 *
70 * @retval EOK if successful.
71 * @retval ENOMEM if there
72 * @return Other error code inherited from one of functions usb_kbd_init(),
73 * ddf_fun_bind() and ddf_fun_add_to_class().
74 */
75static int usb_hid_try_add_device(usb_device_t *dev)
76{
77 assert(dev != NULL);
78
79 /* Initialize device (get and process descriptors, get address, etc.) */
80 usb_log_debug("Initializing USB/HID device...\n");
81
82 usb_hid_dev_t *hid_dev =
83 usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
84 if (hid_dev == NULL) {
85 usb_log_error("Error while creating USB/HID device "
86 "structure.\n");
87 return ENOMEM;
88 }
89
90 int rc = usb_hid_init(hid_dev, dev);
91
92 if (rc != EOK) {
93 usb_log_error("Failed to initialize USB/HID device.\n");
94 usb_hid_destroy(hid_dev);
95 return rc;
96 }
97
98 usb_log_debug("USB/HID device structure initialized.\n");
99
100 /*
101 * 1) subdriver vytvori vlastnu ddf_fun, vlastne ddf_dev_ops, ktore da
102 * do nej.
103 * 2) do tych ops do .interfaces[DEV_IFACE_USBHID (asi)] priradi
104 * vyplnenu strukturu usbhid_iface_t.
105 * 3) klientska aplikacia - musi si rucne vytvorit telefon
106 * (devman_device_connect() - cesta k zariadeniu (/hw/pci0/...) az
107 * k tej fcii.
108 * pouzit usb/classes/hid/iface.h - prvy int je telefon
109 */
110
111 /* Start automated polling function.
112 * This will create a separate fibril that will query the device
113 * for the data continuously
114 */
115 rc = usb_device_auto_poll(dev,
116 /* Index of the polling pipe. */
117 hid_dev->poll_pipe_index,
118 /* Callback when data arrives. */
119 usb_hid_polling_callback,
120 /* How much data to request. */
121 dev->pipes[hid_dev->poll_pipe_index].pipe->max_packet_size,
122 /* Callback when the polling ends. */
123 usb_hid_polling_ended_callback,
124 /* Custom argument. */
125 hid_dev);
126
127 if (rc != EOK) {
128 usb_log_error("Failed to start polling fibril for `%s'.\n",
129 dev->ddf_dev->name);
130 usb_hid_destroy(hid_dev);
131 return rc;
132 }
133 hid_dev->running = true;
134 dev->driver_data = hid_dev;
135
136 /*
137 * Hurrah, device is initialized.
138 */
139 return EOK;
140}
141
142/*----------------------------------------------------------------------------*/
143/**
144 * Callback for passing a new device to the driver.
145 *
146 * @note Currently, only boot-protocol keyboards are supported by this driver.
147 *
148 * @param dev Structure representing the new device.
149 *
150 * @retval EOK if successful.
151 * @retval EREFUSED if the device is not supported.
152 */
153static int usb_hid_device_add(usb_device_t *dev)
154{
155 usb_log_debug("usb_hid_device_add()\n");
156
157 if (dev == NULL) {
158 usb_log_warning("Wrong parameter given for add_device().\n");
159 return EINVAL;
160 }
161
162 if (dev->interface_no < 0) {
163 usb_log_warning("Device is not a supported HID device.\n");
164 usb_log_error("Failed to add HID device: endpoints not found."
165 "\n");
166 return ENOTSUP;
167 }
168
169 int rc = usb_hid_try_add_device(dev);
170
171 if (rc != EOK) {
172 usb_log_warning("Device is not a supported HID device.\n");
173 usb_log_error("Failed to add HID device: %s.\n",
174 str_error(rc));
175 return rc;
176 }
177
178 usb_log_info("HID device `%s' ready to use.\n", dev->ddf_dev->name);
179
180 return EOK;
181}
182
183/*----------------------------------------------------------------------------*/
184
185/**
186 * Callback for removing a device from the driver.
187 *
188 * @param dev Structure representing the device.
189 *
190 * @retval EOK if successful.
191 * @retval EREFUSED if the device is not supported.
192 */
193static int usb_hid_device_gone(usb_device_t *dev)
194{
195 usb_hid_dev_t *hid_dev = dev->driver_data;
196 unsigned tries = 10;
197 while (hid_dev->running) {
198 async_usleep(100000);
199 if (!tries--) {
200 usb_log_error("Can't remove hub, still running.\n");
201 return EINPROGRESS;
202 }
203 }
204
205 assert(!hid_dev->running);
206 usb_hid_destroy(hid_dev);
207 usb_log_debug2("%s destruction complete.\n", dev->ddf_dev->name);
208 return EOK;
209}
210
211/** USB generic driver callbacks */
212static usb_driver_ops_t usb_hid_driver_ops = {
213 .device_add = usb_hid_device_add,
214 .device_gone = usb_hid_device_gone,
215};
216
217
218/** The driver itself. */
219static usb_driver_t usb_hid_driver = {
220 .name = NAME,
221 .ops = &usb_hid_driver_ops,
222 .endpoints = usb_hid_endpoints
223};
224
225/*----------------------------------------------------------------------------*/
226
227int main(int argc, char *argv[])
228{
229 printf(NAME ": HelenOS USB HID driver.\n");
230
231 usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
232
233 return usb_driver_main(&usb_hid_driver);
234}
235
236/**
237 * @}
238 */
Note: See TracBrowser for help on using the repository browser.