source: mainline/uspace/drv/usbhid/main.c@ b83edb93

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b83edb93 was 966acede, checked in by Lubos Slovak <lubos.slovak@…>, 14 years ago

Added generic HID driver skeleton.

  • Property mode set to 100644
File size: 6.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/devdrv.h>
44
45#include "usbhid.h"
46
47/*----------------------------------------------------------------------------*/
48
49#define NAME "usbhid"
50
51/**
52 * Function for adding a new device of type USB/HID/keyboard.
53 *
54 * This functions initializes required structures from the device's descriptors
55 * and starts new fibril for polling the keyboard for events and another one for
56 * handling auto-repeat of keys.
57 *
58 * During initialization, the keyboard is switched into boot protocol, the idle
59 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
60 * when a key is pressed or released. Finally, the LED lights are turned on
61 * according to the default setup of lock keys.
62 *
63 * @note By default, the keyboards is initialized with Num Lock turned on and
64 * other locks turned off.
65 * @note Currently supports only boot-protocol keyboards.
66 *
67 * @param dev Device to add.
68 *
69 * @retval EOK if successful.
70 * @retval ENOMEM if there
71 * @return Other error code inherited from one of functions usb_kbd_init(),
72 * ddf_fun_bind() and ddf_fun_add_to_class().
73 *
74 * @sa usb_kbd_fibril(), usb_kbd_repeat_fibril()
75 */
76static int usb_hid_try_add_device(usb_device_t *dev)
77{
78 /* Create the function exposed under /dev/devices. */
79 ddf_fun_t *hid_fun = ddf_fun_create(dev->ddf_dev, fun_exposed,
80 "hid");
81 if (hid_fun == NULL) {
82 usb_log_error("Could not create DDF function node.\n");
83 return ENOMEM;
84 }
85
86 /*
87 * Initialize device (get and process descriptors, get address, etc.)
88 */
89 usb_log_debug("Initializing USB/HID device...\n");
90
91// usb_kbd_t *kbd_dev = usb_kbd_new();
92// if (kbd_dev == NULL) {
93// usb_log_error("Error while creating USB/HID KBD device "
94// "structure.\n");
95// ddf_fun_destroy(hid_fun);
96// return ENOMEM; // TODO: some other code??
97// }
98
99// int rc = usb_kbd_init(kbd_dev, dev);
100
101// if (rc != EOK) {
102// usb_log_error("Failed to initialize USB/HID KBD device.\n");
103// ddf_fun_destroy(hid_fun);
104// usb_kbd_free(&kbd_dev);
105// return rc;
106// }
107
108// usb_log_debug("USB/HID KBD device structure initialized.\n");
109
110 /*
111 * Store the initialized keyboard device and keyboard ops
112 * to the DDF function.
113 */
114 //kbd_fun->driver_data = kbd_dev;
115 hid_fun->ops = &hid_ops;
116
117 int rc = ddf_fun_bind(hid_fun);
118 if (rc != EOK) {
119 usb_log_error("Could not bind DDF function: %s.\n",
120 str_error(rc));
121 // TODO: Can / should I destroy the DDF function?
122 ddf_fun_destroy(hid_fun);
123 return rc;
124 }
125
126 rc = ddf_fun_add_to_class(hid_fun, "hid");
127 if (rc != EOK) {
128 usb_log_error(
129 "Could not add DDF function to class 'hid': %s.\n",
130 str_error(rc));
131 // TODO: Can / should I destroy the DDF function?
132 ddf_fun_destroy(hid_fun);
133 return rc;
134 }
135
136
137 /* Start automated polling function.
138 * This will create a separate fibril that will query the device
139 * for the data continuously
140 */
141 rc = usb_device_auto_poll(dev,
142 /* Index of the polling pipe. */
143 USB_HID_POLL_EP_NO,
144 /* Callback when data arrives. */
145 usb_hid_polling_callback,
146 /* How much data to request. */
147 dev->pipes[USB_HID_POLL_EP_NO].pipe->max_packet_size,
148 /* Callback when the polling ends. */
149 usb_hid_polling_ended_callback,
150 /* Custom argument. */
151 NULL);
152
153
154 if (rc != EOK) {
155 usb_log_error("Failed to start polling fibril for `%s'.\n",
156 dev->ddf_dev->name);
157 return rc;
158 }
159
160 (void)hid_ops;
161
162 /*
163 * Hurrah, device is initialized.
164 */
165 return EOK;
166}
167
168/*----------------------------------------------------------------------------*/
169/**
170 * Callback for passing a new device to the driver.
171 *
172 * @note Currently, only boot-protocol keyboards are supported by this driver.
173 *
174 * @param dev Structure representing the new device.
175 *
176 * @retval EOK if successful.
177 * @retval EREFUSED if the device is not supported.
178 */
179static int usb_hid_add_device(usb_device_t *dev)
180{
181 usb_log_debug("usb_hid_add_device()\n");
182
183 if (dev->interface_no < 0) {
184 usb_log_warning("Device is not a supported HID device.\n");
185 usb_log_error("Failed to add HID device: endpoint not found."
186 "\n");
187 return ENOTSUP;
188 }
189
190 int rc = usb_hid_try_add_device(dev);
191
192 if (rc != EOK) {
193 usb_log_warning("Device is not a supported HID device.\n");
194 usb_log_error("Failed to add HID device: %s.\n",
195 str_error(rc));
196 return rc;
197 }
198
199 usb_log_info("HID device `%s' ready to use.\n", dev->ddf_dev->name);
200
201 return EOK;
202}
203
204/*----------------------------------------------------------------------------*/
205
206/* Currently, the framework supports only device adding. Once the framework
207 * supports unplug, more callbacks will be added. */
208static usb_driver_ops_t usb_hid_driver_ops = {
209 .add_device = usb_hid_add_device,
210};
211
212
213/* The driver itself. */
214static usb_driver_t usb_hid_driver = {
215 .name = NAME,
216 .ops = &usb_hid_driver_ops,
217 .endpoints = usb_hid_endpoints
218};
219
220/*----------------------------------------------------------------------------*/
221
222int main(int argc, char *argv[])
223{
224 printf(NAME ": HelenOS USB HID driver.\n");
225
226 usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
227
228 return usb_driver_main(&usb_hid_driver);
229}
230
231/**
232 * @}
233 */
Note: See TracBrowser for help on using the repository browser.