source: mainline/uspace/drv/usbkbd/main.c@ 85fa1e1

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

Huge refactoring of KBD driver code.

  • Separated code for retrieval of Report descriptor into hidreport.h/c (will be common for all HID drivers).
  • Removed unused files hiddev.h/c.
  • Changed prefix of usbkbd functions to usb_kbd_*.
  • Removed unused code.
  • Parsing of Report descriptor with fallback to boot protocol in KBD driver (not finished (boot report descriptor missing).
  • Property mode set to 100644
File size: 7.4 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 "kbddev.h"
46#include "kbdrepeat.h"
47
48/*----------------------------------------------------------------------------*/
49
50#define NAME "usbkbd"
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 *
75 * @sa usb_kbd_fibril(), usb_kbd_repeat_fibril()
76 */
77static int usbhid_try_add_device(usb_device_t *dev)
78{
79 /* Create the function exposed under /dev/devices. */
80 ddf_fun_t *kbd_fun = ddf_fun_create(dev->ddf_dev, fun_exposed,
81 "keyboard");
82 if (kbd_fun == NULL) {
83 usb_log_error("Could not create DDF function node.\n");
84 return ENOMEM;
85 }
86
87 /*
88 * Initialize device (get and process descriptors, get address, etc.)
89 */
90 usb_log_debug("Initializing USB/HID KBD device...\n");
91
92 usb_kbd_t *kbd_dev = usb_kbd_new();
93 if (kbd_dev == NULL) {
94 usb_log_error("Error while creating USB/HID KBD device "
95 "structure.\n");
96 ddf_fun_destroy(kbd_fun);
97 return ENOMEM; // TODO: some other code??
98 }
99
100 int rc = usb_kbd_init(kbd_dev, dev);
101
102 if (rc != EOK) {
103 usb_log_error("Failed to initialize USB/HID KBD device.\n");
104 ddf_fun_destroy(kbd_fun);
105 usb_kbd_free(&kbd_dev);
106 return rc;
107 }
108
109 usb_log_debug("USB/HID KBD device structure initialized.\n");
110
111 /*
112 * Store the initialized keyboard device and keyboard ops
113 * to the DDF function.
114 */
115 kbd_fun->driver_data = kbd_dev;
116 kbd_fun->ops = &keyboard_ops;
117
118 rc = ddf_fun_bind(kbd_fun);
119 if (rc != EOK) {
120 usb_log_error("Could not bind DDF function: %s.\n",
121 str_error(rc));
122 // TODO: Can / should I destroy the DDF function?
123 ddf_fun_destroy(kbd_fun);
124 usb_kbd_free(&kbd_dev);
125 return rc;
126 }
127
128 rc = ddf_fun_add_to_class(kbd_fun, "keyboard");
129 if (rc != EOK) {
130 usb_log_error(
131 "Could not add DDF function to class 'keyboard': %s.\n",
132 str_error(rc));
133 // TODO: Can / should I destroy the DDF function?
134 ddf_fun_destroy(kbd_fun);
135 usb_kbd_free(&kbd_dev);
136 return rc;
137 }
138
139 /*
140 * Create new fibril for handling this keyboard
141 */
142 //fid_t fid = fibril_create(usb_kbd_fibril, kbd_dev);
143
144 /* Start automated polling function.
145 * This will create a separate fibril that will query the device
146 * for the data continuously
147 */
148 rc = usb_device_auto_poll(dev,
149 /* Index of the polling pipe. */
150 USB_KBD_POLL_EP_NO,
151 /* Callback when data arrives. */
152 usb_kbd_polling_callback,
153 /* How much data to request. */
154 dev->pipes[USB_KBD_POLL_EP_NO].pipe->max_packet_size,
155 /* Callback when the polling ends. */
156 usb_kbd_polling_ended_callback,
157 /* Custom argument. */
158 kbd_dev);
159
160
161 if (rc != EOK) {
162 usb_log_error("Failed to start polling fibril for `%s'.\n",
163 dev->ddf_dev->name);
164 return rc;
165 }
166 //fibril_add_ready(fid);
167
168 /*
169 * Create new fibril for auto-repeat
170 */
171 fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
172 if (fid == 0) {
173 usb_log_error("Failed to start fibril for KBD auto-repeat");
174 return ENOMEM;
175 }
176 fibril_add_ready(fid);
177
178 (void)keyboard_ops;
179
180 /*
181 * Hurrah, device is initialized.
182 */
183 return EOK;
184}
185
186/*----------------------------------------------------------------------------*/
187/**
188 * Callback for passing a new device to the driver.
189 *
190 * @note Currently, only boot-protocol keyboards are supported by this driver.
191 *
192 * @param dev Structure representing the new device.
193 *
194 * @retval EOK if successful.
195 * @retval EREFUSED if the device is not supported.
196 */
197static int usbhid_add_device(usb_device_t *dev)
198{
199 usb_log_debug("usbhid_add_device()\n");
200
201 if (dev->interface_no < 0) {
202 usb_log_warning("Device is not a supported keyboard.\n");
203 usb_log_error("Failed to add HID device: endpoint not found."
204 "\n");
205 return ENOTSUP;
206 }
207
208 int rc = usbhid_try_add_device(dev);
209
210 if (rc != EOK) {
211 usb_log_warning("Device is not a supported keyboard.\n");
212 usb_log_error("Failed to add HID device: %s.\n",
213 str_error(rc));
214 return rc;
215 }
216
217 usb_log_info("Keyboard `%s' ready to use.\n", dev->ddf_dev->name);
218
219 return EOK;
220}
221
222/*----------------------------------------------------------------------------*/
223
224/* Currently, the framework supports only device adding. Once the framework
225 * supports unplug, more callbacks will be added. */
226static usb_driver_ops_t usbhid_driver_ops = {
227 .add_device = usbhid_add_device,
228};
229
230
231/* The driver itself. */
232static usb_driver_t usbhid_driver = {
233 .name = NAME,
234 .ops = &usbhid_driver_ops,
235 .endpoints = usb_kbd_endpoints
236};
237
238/*----------------------------------------------------------------------------*/
239
240//static driver_ops_t kbd_driver_ops = {
241// .add_device = usbhid_add_device,
242//};
243
244///*----------------------------------------------------------------------------*/
245
246//static driver_t kbd_driver = {
247// .name = NAME,
248// .driver_ops = &kbd_driver_ops
249//};
250
251/*----------------------------------------------------------------------------*/
252
253int main(int argc, char *argv[])
254{
255 printf(NAME ": HelenOS USB HID driver.\n");
256
257 usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
258
259 return usb_driver_main(&usbhid_driver);
260}
261
262/**
263 * @}
264 */
Note: See TracBrowser for help on using the repository browser.