1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libusb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * Client functions for accessing USB HID interface (implementation).
|
---|
34 | */
|
---|
35 | #include <dev_iface.h>
|
---|
36 | #include <usbhid_iface.h>
|
---|
37 | #include <usb/classes/hid/iface.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <str_error.h>
|
---|
40 | #include <async.h>
|
---|
41 | #include <assert.h>
|
---|
42 |
|
---|
43 | /** Ask for event array length.
|
---|
44 | *
|
---|
45 | * @param dev_phone Opened phone to DDF device providing USB HID interface.
|
---|
46 | * @return Number of usages returned or negative error code.
|
---|
47 | */
|
---|
48 | int usbhid_dev_get_event_length(int dev_phone)
|
---|
49 | {
|
---|
50 | if (dev_phone < 0) {
|
---|
51 | return EINVAL;
|
---|
52 | }
|
---|
53 |
|
---|
54 | sysarg_t len;
|
---|
55 | int rc = async_req_1_1(dev_phone, DEV_IFACE_ID(USBHID_DEV_IFACE),
|
---|
56 | IPC_M_USBHID_GET_EVENT_LENGTH, &len);
|
---|
57 | if (rc == EOK) {
|
---|
58 | return (int) len;
|
---|
59 | } else {
|
---|
60 | return rc;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Request for next event from HID device.
|
---|
65 | *
|
---|
66 | * @param[in] dev_phone Opened phone to DDF device providing USB HID interface.
|
---|
67 | * @param[out] usage_pages Where to store usage pages.
|
---|
68 | * @param[out] usages Where to store usages (actual data).
|
---|
69 | * @param[in] usage_count Length of @p usage_pages and @p usages buffer
|
---|
70 | * (in items, not bytes).
|
---|
71 | * @param[out] actual_usage_count Number of usages actually returned by the
|
---|
72 | * device driver.
|
---|
73 | * @param[in] flags Flags (see USBHID_IFACE_FLAG_*).
|
---|
74 | * @return Error code.
|
---|
75 | */
|
---|
76 | int usbhid_dev_get_event(int dev_phone, uint16_t *usage_pages, uint16_t *usages,
|
---|
77 | size_t usage_count, size_t *actual_usage_count, unsigned int flags)
|
---|
78 | {
|
---|
79 | if (dev_phone < 0) {
|
---|
80 | return EINVAL;
|
---|
81 | }
|
---|
82 | if ((usage_pages == NULL) || (usages == NULL)) {
|
---|
83 | return ENOMEM;
|
---|
84 | }
|
---|
85 | if (usage_count == 0) {
|
---|
86 | return EINVAL;
|
---|
87 | }
|
---|
88 |
|
---|
89 | size_t buffer_size = sizeof(uint16_t) * usage_count * 2;
|
---|
90 | uint16_t *buffer = malloc(buffer_size);
|
---|
91 | if (buffer == NULL) {
|
---|
92 | return ENOMEM;
|
---|
93 | }
|
---|
94 |
|
---|
95 | aid_t opening_request = async_send_2(dev_phone,
|
---|
96 | DEV_IFACE_ID(USBHID_DEV_IFACE), IPC_M_USBHID_GET_EVENT,
|
---|
97 | flags, NULL);
|
---|
98 | if (opening_request == 0) {
|
---|
99 | free(buffer);
|
---|
100 | return ENOMEM;
|
---|
101 | }
|
---|
102 |
|
---|
103 | ipc_call_t data_request_call;
|
---|
104 | aid_t data_request = async_data_read(dev_phone, buffer, buffer_size,
|
---|
105 | &data_request_call);
|
---|
106 | if (data_request == 0) {
|
---|
107 | async_wait_for(opening_request, NULL);
|
---|
108 | free(buffer);
|
---|
109 | return ENOMEM;
|
---|
110 | }
|
---|
111 |
|
---|
112 | sysarg_t data_request_rc;
|
---|
113 | sysarg_t opening_request_rc;
|
---|
114 | async_wait_for(data_request, &data_request_rc);
|
---|
115 | async_wait_for(opening_request, &opening_request_rc);
|
---|
116 |
|
---|
117 | if (data_request_rc != EOK) {
|
---|
118 | /* Prefer return code of the opening request. */
|
---|
119 | if (opening_request_rc != EOK) {
|
---|
120 | return (int) opening_request_rc;
|
---|
121 | } else {
|
---|
122 | return (int) data_request_rc;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (opening_request_rc != EOK) {
|
---|
127 | return (int) opening_request_rc;
|
---|
128 | }
|
---|
129 |
|
---|
130 | size_t actual_size = IPC_GET_ARG2(data_request_call);
|
---|
131 | size_t items = actual_size / 2;
|
---|
132 |
|
---|
133 | /* Copy the individual items. */
|
---|
134 | memcpy(usage_pages, buffer, items * sizeof(uint16_t));
|
---|
135 | memcpy(usages, buffer + items, items * sizeof(uint16_t));
|
---|
136 |
|
---|
137 | if (actual_usage_count != NULL) {
|
---|
138 | *actual_usage_count = items;
|
---|
139 | }
|
---|
140 |
|
---|
141 | return EOK;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * @}
|
---|
146 | */
|
---|