source: mainline/uspace/lib/drv/generic/remote_usbhid.c@ 27b85d9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 27b85d9 was 27b85d9, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Add USB HID interface

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2010-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 libdrv
30 * @{
31 */
32/** @file
33 */
34
35#include <async.h>
36#include <errno.h>
37#include <assert.h>
38
39#include "usbhid_iface.h"
40#include "ddf/driver.h"
41
42static void remote_usbhid_get_event_length(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
43static void remote_usbhid_get_event(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
44// static void remote_usbhid_(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
45
46/** Remote USB HID interface operations. */
47static remote_iface_func_ptr_t remote_usbhid_iface_ops [] = {
48 remote_usbhid_get_event_length,
49 remote_usbhid_get_event
50};
51
52/** Remote USB HID interface structure.
53 */
54remote_iface_t remote_usbhid_iface = {
55 .method_count = sizeof(remote_usbhid_iface_ops) /
56 sizeof(remote_usbhid_iface_ops[0]),
57 .methods = remote_usbhid_iface_ops
58};
59
60usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
61
62
63void remote_usbhid_get_event_length(ddf_fun_t *fun, void *iface,
64 ipc_callid_t callid, ipc_call_t *call)
65{
66 usbhid_iface_t *hid_iface = (usbhid_iface_t *) iface;
67
68 if (!hid_iface->get_event_length) {
69 async_answer_0(callid, ENOTSUP);
70 return;
71 }
72
73 int len = hid_iface->get_event_length(fun);
74 if (len == 0) {
75 len = EEMPTY;
76 }
77 if (len < 0) {
78 async_answer_0(callid, len);
79 } else {
80 async_answer_1(callid, EOK, len);
81 }
82}
83
84void remote_usbhid_get_event(ddf_fun_t *fun, void *iface,
85 ipc_callid_t callid, ipc_call_t *call)
86{
87 usbhid_iface_t *hid_iface = (usbhid_iface_t *) iface;
88
89 if (!hid_iface->get_event) {
90 async_answer_0(callid, ENOTSUP);
91 return;
92 }
93
94 unsigned int flags = DEV_IPC_GET_ARG1(*call);
95
96 size_t len;
97 ipc_callid_t data_callid;
98 if (!async_data_read_receive(&data_callid, &len)) {
99 async_answer_0(callid, EPARTY);
100 return;
101 }
102 /* Check that length is even number. Truncate otherwise. */
103 if ((len % 2) == 1) {
104 len--;
105 }
106 if (len == 0) {
107 async_answer_0(data_callid, EINVAL);
108 async_answer_0(callid, EINVAL);
109 }
110
111 int rc;
112
113 size_t items = len / 2;
114 uint16_t *usage_pages_and_usages = malloc(sizeof(uint16_t) * len);
115 if (usage_pages_and_usages == NULL) {
116 async_answer_0(data_callid, ENOMEM);
117 async_answer_0(callid, ENOMEM);
118 }
119
120 size_t act_items;
121 int rc = hid_iface->get_event(fun, usage_pages_and_usages,
122 usage_pages_and_usages + items, items, &act_items, flags);
123 if (rc != EOK) {
124 free(usage_pages_and_usages);
125 async_answer_0(data_callid, rc);
126 async_answer_0(callid, rc);
127 }
128 if (act_items >= items) {
129 /* This shall not happen. */
130 // FIXME: how about an assert here?
131 act_items = items;
132 }
133
134 async_data_read_finalize(data_callid, usage_pages_and_usages,
135 act_items * 2 * sizeof(uint16_t));
136
137 free(usage_pages_and_usages);
138
139 async_answer_0(callid, EOK);
140}
141
142/**
143 * @}
144 */
Note: See TracBrowser for help on using the repository browser.