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
RevLine 
[966acede]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
[7d521e24]43#include <usb/dev/driver.h>
44#include <usb/dev/poll.h>
[966acede]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.
[fbe148ee]67 * @return Error code.
[966acede]68 */
69static int usb_hid_try_add_device(usb_device_t *dev)
70{
[56f8a04]71 assert(dev != NULL);
[cc29622]72
[065064e6]73 /* Initialize device (get and process descriptors, get address, etc.) */
[966acede]74 usb_log_debug("Initializing USB/HID device...\n");
[cc29622]75
[065064e6]76 usb_hid_dev_t *hid_dev =
77 usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
[61257f4]78 if (hid_dev == NULL) {
79 usb_log_error("Error while creating USB/HID device "
80 "structure.\n");
81 return ENOMEM;
82 }
[cc29622]83
[61257f4]84 int rc = usb_hid_init(hid_dev, dev);
[cc29622]85
[61257f4]86 if (rc != EOK) {
87 usb_log_error("Failed to initialize USB/HID device.\n");
[a8c4e871]88 usb_hid_deinit(hid_dev);
[61257f4]89 return rc;
[98604cc]90 }
[cc29622]91
[61257f4]92 usb_log_debug("USB/HID device structure initialized.\n");
[cc29622]93
[b20de1d]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 */
[cc29622]104
[966acede]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. */
[61257f4]111 hid_dev->poll_pipe_index,
[966acede]112 /* Callback when data arrives. */
[60c0573]113 usb_hid_polling_callback,
[966acede]114 /* How much data to request. */
[b77931d]115 dev->pipes[hid_dev->poll_pipe_index].pipe.max_packet_size,
[966acede]116 /* Callback when the polling ends. */
117 usb_hid_polling_ended_callback,
118 /* Custom argument. */
[61257f4]119 hid_dev);
[cc29622]120
[966acede]121 if (rc != EOK) {
122 usb_log_error("Failed to start polling fibril for `%s'.\n",
123 dev->ddf_dev->name);
[a8c4e871]124 usb_hid_deinit(hid_dev);
[966acede]125 return rc;
126 }
[68dbe3e]127 hid_dev->running = true;
[966acede]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.
[fbe148ee]141 * @return Error code.
[966acede]142 */
[1a4ea01d]143static int usb_hid_device_add(usb_device_t *dev)
[966acede]144{
[1a4ea01d]145 usb_log_debug("usb_hid_device_add()\n");
[cc29622]146
[8f8f34ad]147 if (dev == NULL) {
[56f8a04]148 usb_log_warning("Wrong parameter given for add_device().\n");
149 return EINVAL;
150 }
[cc29622]151
[966acede]152 if (dev->interface_no < 0) {
153 usb_log_warning("Device is not a supported HID device.\n");
[61257f4]154 usb_log_error("Failed to add HID device: endpoints not found."
[966acede]155 "\n");
156 return ENOTSUP;
157 }
[cc29622]158
[966acede]159 int rc = usb_hid_try_add_device(dev);
[cc29622]160
[966acede]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 }
[cc29622]167
[966acede]168 usb_log_info("HID device `%s' ready to use.\n", dev->ddf_dev->name);
169
170 return EOK;
171}
172/*----------------------------------------------------------------------------*/
[98604cc]173/**
[fbe148ee]174 * Callback for a device about to be removed from the driver.
[98604cc]175 *
176 * @param dev Structure representing the device.
[fbe148ee]177 * @return Error code.
178 */
179static int usb_hid_device_rem(usb_device_t *dev)
180{
[dcc10b8d]181 // TODO: Stop device polling fibril
182 // TODO: Stop autorepeat fibril
183 // TODO: Call deinit
184 return ENOTSUP;
[fbe148ee]185}
186/*----------------------------------------------------------------------------*/
187/**
188 * Callback for removing a device from the driver.
[98604cc]189 *
[fbe148ee]190 * @param dev Structure representing the device.
191 * @return Error code.
[98604cc]192 */
193static int usb_hid_device_gone(usb_device_t *dev)
194{
195 usb_hid_dev_t *hid_dev = dev->driver_data;
[68dbe3e]196 unsigned tries = 10;
197 while (hid_dev->running) {
198 async_usleep(100000);
199 if (!tries--) {
[dcc10b8d]200 usb_log_error("Can't remove hid, still running.\n");
[fbe148ee]201 return EBUSY;
[68dbe3e]202 }
203 }
204
205 assert(!hid_dev->running);
[a8c4e871]206 usb_hid_deinit(hid_dev);
[2a5b62b]207 usb_log_debug2("%s destruction complete.\n", dev->ddf_dev->name);
[98604cc]208 return EOK;
209}
[fbe148ee]210/*----------------------------------------------------------------------------*/
[98604cc]211/** USB generic driver callbacks */
[fbe148ee]212static const usb_driver_ops_t usb_hid_driver_ops = {
[98604cc]213 .device_add = usb_hid_device_add,
[fbe148ee]214 .device_rem = usb_hid_device_rem,
[98604cc]215 .device_gone = usb_hid_device_gone,
[966acede]216};
[fbe148ee]217/*----------------------------------------------------------------------------*/
[98604cc]218/** The driver itself. */
[fbe148ee]219static const usb_driver_t usb_hid_driver = {
[966acede]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
[7eecbb8b]229 usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
[966acede]230
231 return usb_driver_main(&usb_hid_driver);
232}
233/**
234 * @}
235 */
Note: See TracBrowser for help on using the repository browser.