source: mainline/uspace/drv/hid/usbhid/main.c@ 61e27e80

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 61e27e80 was a1732929, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usb: unified logging

Use logger instead of printf. Logger adds newlines automatically.

  • Property mode set to 100644
File size: 4.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", __FUNCTION__);
61
62 if (dev == NULL) {
63 usb_log_error("Wrong parameter given for add_device().");
64 return EINVAL;
65 }
66
67 if (usb_device_get_iface_number(dev) < 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("Failed to create USB/HID device structure.");
76 return ENOMEM;
77 }
78
79 int rc = usb_hid_init(hid_dev, dev);
80 if (rc != EOK) {
81 usb_log_error("Failed to initialize USB/HID device.");
82 usb_hid_deinit(hid_dev);
83 return rc;
84 }
85
86 usb_log_debug("USB/HID device structure initialized.");
87
88 /* Start automated polling function.
89 * This will create a separate fibril that will query the device
90 * for the data continuously. */
91 rc = usb_polling_start(&hid_dev->polling);
92
93 if (rc != EOK) {
94 usb_log_error("Failed to start polling fibril for `%s'.",
95 usb_device_get_name(dev));
96 usb_hid_deinit(hid_dev);
97 return rc;
98 }
99 hid_dev->running = true;
100
101 usb_log_info("HID device `%s' ready.", usb_device_get_name(dev));
102
103 return EOK;
104}
105
106static int join_and_clean(usb_device_t *dev)
107{
108 assert(dev);
109 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
110 assert(hid_dev);
111
112 /* Join polling fibril (ignoring error code). */
113 usb_polling_join(&hid_dev->polling);
114
115 /* Clean up. */
116 usb_hid_deinit(hid_dev);
117 usb_log_info("%s destruction complete.", usb_device_get_name(dev));
118
119 return EOK;
120}
121
122/**
123 * Callback for a device about to be removed from the driver.
124 *
125 * @param dev Structure representing the device.
126 * @return Error code.
127 */
128static int usb_hid_device_remove(usb_device_t *dev)
129{
130 assert(dev);
131 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
132 assert(hid_dev);
133
134 usb_log_info("Device %s removed.", usb_device_get_name(dev));
135 return join_and_clean(dev);
136}
137
138/**
139 * Callback for removing a device from the driver.
140 *
141 * @param dev Structure representing the device.
142 * @return Error code.
143 */
144static int usb_hid_device_gone(usb_device_t *dev)
145{
146 assert(dev);
147 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
148 assert(hid_dev);
149
150 usb_log_info("Device %s gone.", usb_device_get_name(dev));
151 return join_and_clean(dev);
152}
153
154/** USB generic driver callbacks */
155static const usb_driver_ops_t usb_hid_driver_ops = {
156 .device_add = usb_hid_device_add,
157 .device_remove = usb_hid_device_remove,
158 .device_gone = usb_hid_device_gone,
159};
160
161/** The driver itself. */
162static const usb_driver_t usb_hid_driver = {
163 .name = NAME,
164 .ops = &usb_hid_driver_ops,
165 .endpoints = usb_hid_endpoints
166};
167
168int main(int argc, char *argv[])
169{
170 printf(NAME ": HelenOS USB HID driver.\n");
171
172 log_init(NAME);
173
174 return usb_driver_main(&usb_hid_driver);
175}
176/**
177 * @}
178 */
Note: See TracBrowser for help on using the repository browser.