source: mainline/uspace/drv/bus/usb/usbhid/main.c@ 555499da

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

usbhid: Merge usb_hid_device_add and usb_hid_try_add_device.

  • Property mode set to 100644
File size: 5.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/dev/driver.h>
44#include <usb/dev/poll.h>
45
46#include "usbhid.h"
47
48#define NAME "usbhid"
49
50/**
51 * Callback for passing a new device to the driver.
52 *
53 * @note Currently, only boot-protocol keyboards are supported by this driver.
54 *
55 * @param dev Structure representing the new device.
56 * @return Error code.
57 */
58static int usb_hid_device_add(usb_device_t *dev)
59{
60 usb_log_debug("%s\n", __FUNCTION__);
61
62 if (dev == NULL) {
63 usb_log_error("Wrong parameter given for add_device().\n");
64 return EINVAL;
65 }
66
67 if (dev->interface_no < 0) {
68 usb_log_error("Failed to add HID device: endpoints not found."
69 "\n");
70 return ENOTSUP;
71 }
72 usb_hid_dev_t *hid_dev =
73 usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
74 if (hid_dev == NULL) {
75 usb_log_error("Error while creating USB/HID device "
76 "structure.\n");
77 return ENOMEM;
78 }
79
80 int rc = usb_hid_init(hid_dev, dev);
81 if (rc != EOK) {
82 usb_log_error("Failed to initialize USB/HID device.\n");
83 usb_hid_deinit(hid_dev);
84 return rc;
85 }
86
87 usb_log_debug("USB/HID device structure initialized.\n");
88
89 /*
90 * 1) subdriver vytvori vlastnu ddf_fun, vlastne ddf_dev_ops, ktore da
91 * do nej.
92 * 2) do tych ops do .interfaces[DEV_IFACE_USBHID (asi)] priradi
93 * vyplnenu strukturu usbhid_iface_t.
94 * 3) klientska aplikacia - musi si rucne vytvorit telefon
95 * (devman_device_connect() - cesta k zariadeniu (/hw/pci0/...) az
96 * k tej fcii.
97 * pouzit usb/classes/hid/iface.h - prvy int je telefon
98 */
99
100 /* Start automated polling function.
101 * This will create a separate fibril that will query the device
102 * for the data continuously.
103 */
104 rc = usb_device_auto_poll(dev,
105 /* Index of the polling pipe. */
106 hid_dev->poll_pipe_index,
107 /* Callback when data arrives. */
108 usb_hid_polling_callback,
109 /* How much data to request. */
110 dev->pipes[hid_dev->poll_pipe_index].pipe.max_packet_size,
111 /* Callback when the polling ends. */
112 usb_hid_polling_ended_callback,
113 /* Custom argument. */
114 hid_dev);
115
116 if (rc != EOK) {
117 usb_log_error("Failed to start polling fibril for `%s'.\n",
118 dev->ddf_dev->name);
119 usb_hid_deinit(hid_dev);
120 return rc;
121 }
122 hid_dev->running = true;
123
124 usb_log_info("HID device `%s' ready to use.\n", dev->ddf_dev->name);
125
126 return EOK;
127}
128/*----------------------------------------------------------------------------*/
129/**
130 * Callback for a device about to be removed from the driver.
131 *
132 * @param dev Structure representing the device.
133 * @return Error code.
134 */
135static int usb_hid_device_rem(usb_device_t *dev)
136{
137 // TODO: Stop device polling fibril
138 // TODO: Stop autorepeat fibril
139 // TODO: Call deinit
140 return ENOTSUP;
141}
142/*----------------------------------------------------------------------------*/
143/**
144 * Callback for removing a device from the driver.
145 *
146 * @param dev Structure representing the device.
147 * @return Error code.
148 */
149static int usb_hid_device_gone(usb_device_t *dev)
150{
151 assert(dev);
152 assert(dev->driver_data);
153 usb_hid_dev_t *hid_dev = dev->driver_data;
154 unsigned tries = 100;
155 /* Wait for fail. */
156 while (hid_dev->running && tries--) {
157 async_usleep(100000);
158 }
159 if (hid_dev->running) {
160 usb_log_error("Can't remove hid, still running.\n");
161 return EBUSY;
162 }
163
164 usb_hid_deinit(hid_dev);
165 usb_log_debug2("%s destruction complete.\n", dev->ddf_dev->name);
166 return EOK;
167}
168/*----------------------------------------------------------------------------*/
169/** USB generic driver callbacks */
170static const usb_driver_ops_t usb_hid_driver_ops = {
171 .device_add = usb_hid_device_add,
172 .device_rem = usb_hid_device_rem,
173 .device_gone = usb_hid_device_gone,
174};
175/*----------------------------------------------------------------------------*/
176/** The driver itself. */
177static const usb_driver_t usb_hid_driver = {
178 .name = NAME,
179 .ops = &usb_hid_driver_ops,
180 .endpoints = usb_hid_endpoints
181};
182/*----------------------------------------------------------------------------*/
183int main(int argc, char *argv[])
184{
185 printf(NAME ": HelenOS USB HID driver.\n");
186
187 usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
188
189 return usb_driver_main(&usb_hid_driver);
190}
191/**
192 * @}
193 */
Note: See TracBrowser for help on using the repository browser.