source: mainline/uspace/drv/bus/usb/usbhid/main.c@ 5153b58

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5153b58 was b77931d, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

usb: usb_pipe_t was always allocated in usb_endpoint_mapping_t, embed it.

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