[966acede] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Lubos Slovak
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Petr Manek
|
---|
[966acede] | 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 | /** @file
|
---|
| 34 | * USB HID driver API.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[ba358ed] | 37 | #ifndef USB_HID_USBHID_H_
|
---|
| 38 | #define USB_HID_USBHID_H_
|
---|
[966acede] | 39 |
|
---|
| 40 | #include <stdint.h>
|
---|
| 41 |
|
---|
[faa44e58] | 42 | #include <usb/hid/hidparser.h>
|
---|
[966acede] | 43 | #include <ddf/driver.h>
|
---|
[7d521e24] | 44 | #include <usb/dev/pipes.h>
|
---|
| 45 | #include <usb/dev/driver.h>
|
---|
[71f211f] | 46 | #include <usb/dev/poll.h>
|
---|
[faa44e58] | 47 | #include <usb/hid/hid.h>
|
---|
[3e6a98c5] | 48 | #include <stdbool.h>
|
---|
[966acede] | 49 |
|
---|
[d022500] | 50 | typedef struct usb_hid_dev usb_hid_dev_t;
|
---|
| 51 | typedef struct usb_hid_subdriver usb_hid_subdriver_t;
|
---|
[60c0573] | 52 |
|
---|
[a22004f] | 53 | /** Subdriver initialization callback.
|
---|
| 54 | *
|
---|
| 55 | * @param dev Backing USB HID device.
|
---|
| 56 | * @param data Custom subdriver data (pointer where to store them).
|
---|
| 57 | * @return Error code.
|
---|
| 58 | */
|
---|
[5a6cc679] | 59 | typedef errno_t (*usb_hid_driver_init_t)(usb_hid_dev_t *dev, void **data);
|
---|
[a22004f] | 60 |
|
---|
| 61 | /** Subdriver deinitialization callback.
|
---|
| 62 | *
|
---|
| 63 | * @param dev Backing USB HID device.
|
---|
| 64 | * @param data Custom subdriver data.
|
---|
| 65 | */
|
---|
[d022500] | 66 | typedef void (*usb_hid_driver_deinit_t)(usb_hid_dev_t *dev, void *data);
|
---|
[a22004f] | 67 |
|
---|
| 68 | /** Subdriver callback on data from device.
|
---|
| 69 | *
|
---|
| 70 | * @param dev Backing USB HID device.
|
---|
| 71 | * @param data Custom subdriver data.
|
---|
| 72 | * @return Whether to continue polling (typically true always).
|
---|
| 73 | */
|
---|
[d022500] | 74 | typedef bool (*usb_hid_driver_poll_t)(usb_hid_dev_t *dev, void *data);
|
---|
[a22004f] | 75 |
|
---|
| 76 | /** Subdriver callback after communication with the device ceased.
|
---|
| 77 | *
|
---|
| 78 | * @param dev Backing USB HID device.
|
---|
| 79 | * @param data Custom subdriver data.
|
---|
| 80 | * @param ended_due_to_errors Whether communication ended due to errors in
|
---|
| 81 | * communication (true) or deliberately by driver (false).
|
---|
| 82 | */
|
---|
| 83 | typedef void (*usb_hid_driver_poll_ended_t)(usb_hid_dev_t *dev, void *data,
|
---|
| 84 | bool ended_due_to_errors);
|
---|
[60c0573] | 85 |
|
---|
[d022500] | 86 | struct usb_hid_subdriver {
|
---|
[60c0573] | 87 | /** Function to be called when initializing HID device. */
|
---|
| 88 | usb_hid_driver_init_t init;
|
---|
| 89 | /** Function to be called when destroying the HID device structure. */
|
---|
| 90 | usb_hid_driver_deinit_t deinit;
|
---|
| 91 | /** Function to be called when data arrives from the device. */
|
---|
[65c3794] | 92 | usb_hid_driver_poll_t poll;
|
---|
[60c0573] | 93 | /** Function to be called when polling ends. */
|
---|
[65c3794] | 94 | usb_hid_driver_poll_ended_t poll_end;
|
---|
[65b458c4] | 95 | /** Arbitrary data needed by the subdriver. */
|
---|
| 96 | void *data;
|
---|
[d022500] | 97 | };
|
---|
[60c0573] | 98 |
|
---|
[61257f4] | 99 | /**
|
---|
| 100 | * Structure for holding general HID device data.
|
---|
| 101 | */
|
---|
[d022500] | 102 | struct usb_hid_dev {
|
---|
[61257f4] | 103 | /** Structure holding generic USB device information. */
|
---|
| 104 | usb_device_t *usb_dev;
|
---|
[cc29622] | 105 |
|
---|
[bb70637] | 106 | /** Endpont mapping of the polling pipe. */
|
---|
| 107 | usb_endpoint_mapping_t *poll_pipe_mapping;
|
---|
[cc29622] | 108 |
|
---|
[8b71f3e] | 109 | /** Device polling structure. */
|
---|
| 110 | usb_polling_t polling;
|
---|
[71f211f] | 111 |
|
---|
[60c0573] | 112 | /** Subdrivers. */
|
---|
| 113 | usb_hid_subdriver_t *subdrivers;
|
---|
[cc29622] | 114 |
|
---|
[60c0573] | 115 | /** Number of subdrivers. */
|
---|
[f317490] | 116 | unsigned subdriver_count;
|
---|
[cc29622] | 117 |
|
---|
[61257f4] | 118 | /** Report descriptor. */
|
---|
| 119 | uint8_t *report_desc;
|
---|
[966acede] | 120 |
|
---|
[61257f4] | 121 | /** Report descriptor size. */
|
---|
| 122 | size_t report_desc_size;
|
---|
[cc29622] | 123 |
|
---|
[61257f4] | 124 | /** HID Report parser. */
|
---|
[a8c4e871] | 125 | usb_hid_report_t report;
|
---|
[cc29622] | 126 |
|
---|
[65c3794] | 127 | uint8_t report_id;
|
---|
[cc29622] | 128 |
|
---|
[31cfee16] | 129 | uint8_t *input_report;
|
---|
[cc29622] | 130 |
|
---|
[31cfee16] | 131 | size_t input_report_size;
|
---|
[d1fb591] | 132 | size_t max_input_report_size;
|
---|
[cc29622] | 133 |
|
---|
[8fb45e08] | 134 | int report_nr;
|
---|
[2a5b62b] | 135 | volatile bool running;
|
---|
[d022500] | 136 | };
|
---|
[966acede] | 137 |
|
---|
[b803845] | 138 | extern const usb_endpoint_description_t *usb_hid_endpoints[];
|
---|
[966acede] | 139 |
|
---|
[5a6cc679] | 140 | errno_t usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev);
|
---|
[3f8f09f] | 141 |
|
---|
[a8c4e871] | 142 | void usb_hid_deinit(usb_hid_dev_t *hid_dev);
|
---|
[966acede] | 143 |
|
---|
[8fb45e08] | 144 | void usb_hid_new_report(usb_hid_dev_t *hid_dev);
|
---|
| 145 |
|
---|
[3f8f09f] | 146 | int usb_hid_report_number(const usb_hid_dev_t *hid_dev);
|
---|
[dd3eda2] | 147 |
|
---|
[ba358ed] | 148 | #endif /* USB_HID_USBHID_H_ */
|
---|
[966acede] | 149 |
|
---|
| 150 | /**
|
---|
| 151 | * @}
|
---|
| 152 | */
|
---|