source: mainline/uspace/drv/usbkbd/main.c@ 86b4ee0

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

Report IDs support + fix in kbd_set_led().

  • Property mode set to 100644
File size: 7.1 KB
RevLine 
[c7137738]1/*
2 * Copyright (c) 2010 Vojtech Horky
[1c13dac]3 * Copyright (c) 2011 Lubos Slovak
[c7137738]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 */
[1c13dac]29
[ba54451]30/** @addtogroup drvusbhid
31 * @{
32 */
[1c13dac]33/**
34 * @file
[966acede]35 * Main routines of USB KBD driver.
[1c13dac]36 */
37
[eb1a2f4]38#include <ddf/driver.h>
[b43bcf1]39#include <usb/debug.h>
[2391aaf]40#include <errno.h>
[fbefd0e]41#include <str_error.h>
[882f8b1]42
[f8e4cb6]43#include <usb/devdrv.h>
44
[2391aaf]45#include "kbddev.h"
[f8e4cb6]46#include "kbdrepeat.h"
[45019865]47
[2391aaf]48/*----------------------------------------------------------------------------*/
[45019865]49
[476b71ff]50#define NAME "usbkbd"
[45019865]51
[f8e4cb6]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
[252e30c]72 * @return Other error code inherited from one of functions usb_kbd_init(),
[f8e4cb6]73 * ddf_fun_bind() and ddf_fun_add_to_class().
74 *
[252e30c]75 * @sa usb_kbd_fibril(), usb_kbd_repeat_fibril()
[f8e4cb6]76 */
[966acede]77static int usb_kbd_try_add_device(usb_device_t *dev)
[f8e4cb6]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
[252e30c]92 usb_kbd_t *kbd_dev = usb_kbd_new();
[f8e4cb6]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
[252e30c]100 int rc = usb_kbd_init(kbd_dev, dev);
[f8e4cb6]101
102 if (rc != EOK) {
103 usb_log_error("Failed to initialize USB/HID KBD device.\n");
104 ddf_fun_destroy(kbd_fun);
[252e30c]105 usb_kbd_free(&kbd_dev);
[f8e4cb6]106 return rc;
[b83edb93]107 }
[f8e4cb6]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);
[252e30c]124 usb_kbd_free(&kbd_dev);
[f8e4cb6]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);
[252e30c]135 usb_kbd_free(&kbd_dev);
[f8e4cb6]136 return rc;
137 }
138
139 /*
140 * Create new fibril for handling this keyboard
141 */
[252e30c]142 //fid_t fid = fibril_create(usb_kbd_fibril, kbd_dev);
[f8e4cb6]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. */
[252e30c]150 USB_KBD_POLL_EP_NO,
[f8e4cb6]151 /* Callback when data arrives. */
[252e30c]152 usb_kbd_polling_callback,
[f8e4cb6]153 /* How much data to request. */
[252e30c]154 dev->pipes[USB_KBD_POLL_EP_NO].pipe->max_packet_size,
[f8e4cb6]155 /* Callback when the polling ends. */
[252e30c]156 usb_kbd_polling_ended_callback,
[f8e4cb6]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 */
[252e30c]171 fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
[f8e4cb6]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
[2391aaf]186/*----------------------------------------------------------------------------*/
[48d2765]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 */
[966acede]197static int usb_kbd_add_device(usb_device_t *dev)
[243cb86]198{
[966acede]199 usb_log_debug("usb_kbd_add_device()\n");
[1c6c4092]200
[f8e4cb6]201 if (dev->interface_no < 0) {
202 usb_log_warning("Device is not a supported keyboard.\n");
[b83edb93]203 usb_log_error("Failed to add USB KBD device: endpoint not "
204 "found.\n");
[f8e4cb6]205 return ENOTSUP;
206 }
207
[966acede]208 int rc = usb_kbd_try_add_device(dev);
[dafab9e0]209
210 if (rc != EOK) {
[fbefd0e]211 usb_log_warning("Device is not a supported keyboard.\n");
[966acede]212 usb_log_error("Failed to add KBD device: %s.\n",
[fbefd0e]213 str_error(rc));
214 return rc;
[b43bcf1]215 }
[882f8b1]216
[f8e4cb6]217 usb_log_info("Keyboard `%s' ready to use.\n", dev->ddf_dev->name);
[fbefd0e]218
[45019865]219 return EOK;
[243cb86]220}
[91db50ac]221
[2391aaf]222/*----------------------------------------------------------------------------*/
[c7137738]223
[f8e4cb6]224/* Currently, the framework supports only device adding. Once the framework
225 * supports unplug, more callbacks will be added. */
[966acede]226static usb_driver_ops_t usb_kbd_driver_ops = {
227 .add_device = usb_kbd_add_device,
[c7137738]228};
229
[2391aaf]230
[f8e4cb6]231/* The driver itself. */
[966acede]232static usb_driver_t usb_kbd_driver = {
[f8e4cb6]233 .name = NAME,
[966acede]234 .ops = &usb_kbd_driver_ops,
[252e30c]235 .endpoints = usb_kbd_endpoints
[c7137738]236};
237
[2391aaf]238/*----------------------------------------------------------------------------*/
239
[c7137738]240int main(int argc, char *argv[])
241{
[966acede]242 printf(NAME ": HelenOS USB KBD driver.\n");
[fbefd0e]243
[f8e4cb6]244 usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
[fbefd0e]245
[966acede]246 return usb_driver_main(&usb_kbd_driver);
[c7137738]247}
[ba54451]248
249/**
250 * @}
251 */
Note: See TracBrowser for help on using the repository browser.