source: mainline/uspace/drv/hid/usbhid/main.c

Last change on this file was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[966acede]1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * Copyright (c) 2011 Lubos Slovak
[e0a5d4c]4 * Copyright (c) 2018 Petr Manek, Ondrej Hlavaty
[966acede]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup drvusbhid
32 * @{
33 */
34/**
35 * @file
36 * Main routines of USB HID driver.
37 */
38
39#include <ddf/driver.h>
40#include <usb/debug.h>
41#include <errno.h>
42#include <str_error.h>
43
[7d521e24]44#include <usb/dev/driver.h>
45#include <usb/dev/poll.h>
[966acede]46
47#include "usbhid.h"
48
49#define NAME "usbhid"
50
51/**
[555499da]52 * Callback for passing a new device to the driver.
[966acede]53 *
[555499da]54 * @note Currently, only boot-protocol keyboards are supported by this driver.
[966acede]55 *
[555499da]56 * @param dev Structure representing the new device.
[fbe148ee]57 * @return Error code.
[966acede]58 */
[5a6cc679]59static errno_t usb_hid_device_add(usb_device_t *dev)
[966acede]60{
[a1732929]61 usb_log_debug("%s", __FUNCTION__);
[cc29622]62
[555499da]63 if (dev == NULL) {
[a1732929]64 usb_log_error("Wrong parameter given for add_device().");
[555499da]65 return EINVAL;
66 }
[cc29622]67
[8e10ef4]68 if (usb_device_get_iface_number(dev) < 0) {
[555499da]69 usb_log_error("Failed to add HID device: endpoints not found."
70 "\n");
71 return ENOTSUP;
72 }
[065064e6]73 usb_hid_dev_t *hid_dev =
74 usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
[61257f4]75 if (hid_dev == NULL) {
[a1732929]76 usb_log_error("Failed to create USB/HID device structure.");
[61257f4]77 return ENOMEM;
78 }
[cc29622]79
[5a6cc679]80 errno_t rc = usb_hid_init(hid_dev, dev);
[61257f4]81 if (rc != EOK) {
[a1732929]82 usb_log_error("Failed to initialize USB/HID device.");
[a8c4e871]83 usb_hid_deinit(hid_dev);
[61257f4]84 return rc;
[98604cc]85 }
[cc29622]86
[a1732929]87 usb_log_debug("USB/HID device structure initialized.");
[cc29622]88
[7c3fb9b]89 /*
90 * Start automated polling function.
[966acede]91 * This will create a separate fibril that will query the device
[7c3fb9b]92 * for the data continuously.
93 */
[8b71f3e]94 rc = usb_polling_start(&hid_dev->polling);
[cc29622]95
[966acede]96 if (rc != EOK) {
[a1732929]97 usb_log_error("Failed to start polling fibril for `%s'.",
[c39e9fb]98 usb_device_get_name(dev));
[a8c4e871]99 usb_hid_deinit(hid_dev);
[966acede]100 return rc;
101 }
[68dbe3e]102 hid_dev->running = true;
[966acede]103
[a1732929]104 usb_log_info("HID device `%s' ready.", usb_device_get_name(dev));
[966acede]105
106 return EOK;
107}
[76fbd9a]108
[be01eb3]109static errno_t join_and_clean(usb_device_t *dev)
[970f6e1]110{
111 assert(dev);
112 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
113 assert(hid_dev);
114
[8b71f3e]115 /* Join polling fibril (ignoring error code). */
116 usb_polling_join(&hid_dev->polling);
[970f6e1]117
118 /* Clean up. */
[d46ceb2b]119 usb_hid_deinit(hid_dev);
[a1732929]120 usb_log_info("%s destruction complete.", usb_device_get_name(dev));
[970f6e1]121
[d46ceb2b]122 return EOK;
[fbe148ee]123}
[76fbd9a]124
[fcdab1e]125/**
[91173333]126 * Callback for a device about to be removed from the driver.
[fcdab1e]127 *
128 * @param dev Structure representing the device.
129 * @return Error code.
130 */
[5a6cc679]131static errno_t usb_hid_device_remove(usb_device_t *dev)
[fcdab1e]132{
133 assert(dev);
134 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
135 assert(hid_dev);
136
[a1732929]137 usb_log_info("Device %s removed.", usb_device_get_name(dev));
[fcdab1e]138 return join_and_clean(dev);
139}
140
[fbe148ee]141/**
142 * Callback for removing a device from the driver.
[98604cc]143 *
[fbe148ee]144 * @param dev Structure representing the device.
145 * @return Error code.
[98604cc]146 */
[5a6cc679]147static errno_t usb_hid_device_gone(usb_device_t *dev)
[98604cc]148{
[f750345]149 assert(dev);
[c39e9fb]150 usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
151 assert(hid_dev);
[9d170a1]152
[a1732929]153 usb_log_info("Device %s gone.", usb_device_get_name(dev));
[fcdab1e]154 return join_and_clean(dev);
[98604cc]155}
[76fbd9a]156
[98604cc]157/** USB generic driver callbacks */
[fbe148ee]158static const usb_driver_ops_t usb_hid_driver_ops = {
[98604cc]159 .device_add = usb_hid_device_add,
[c54b898]160 .device_remove = usb_hid_device_remove,
[98604cc]161 .device_gone = usb_hid_device_gone,
[966acede]162};
[76fbd9a]163
[98604cc]164/** The driver itself. */
[fbe148ee]165static const usb_driver_t usb_hid_driver = {
[1433ecda]166 .name = NAME,
167 .ops = &usb_hid_driver_ops,
168 .endpoints = usb_hid_endpoints
[966acede]169};
[76fbd9a]170
[966acede]171int main(int argc, char *argv[])
172{
173 printf(NAME ": HelenOS USB HID driver.\n");
174
[920d0fc]175 log_init(NAME);
[966acede]176
177 return usb_driver_main(&usb_hid_driver);
178}
179/**
180 * @}
181 */
Note: See TracBrowser for help on using the repository browser.