source: mainline/uspace/drv/char/xtkbd/xtkbd.c@ 65ceb4b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 65ceb4b was 65ceb4b, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

xtkbd: Add default connection handler and add device to keyboard category.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
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/** @addtogroup drvkbd
29 * @{
30 */
31/** @file
32 * @brief XT keyboard driver;
33 */
34
35#include <errno.h>
36#include <devman.h>
37#include <device/char_dev.h>
38#include <ddf/log.h>
39#include <ipc/kbdev.h>
40#include <abi/ipc/methods.h>
41
42#include "xtkbd.h"
43
44static int polling(void *);
45static void default_connection_handler(ddf_fun_t *fun,
46 ipc_callid_t icallid, ipc_call_t *icall);
47
48static ddf_dev_ops_t kbd_ops = {
49 .default_handler = default_connection_handler
50};
51
52int xt_kbd_init(xt_kbd_t *kbd, ddf_dev_t *dev)
53{
54 assert(kbd);
55 assert(dev);
56 kbd->input_sess = NULL;
57 kbd->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
58 dev->handle, IPC_FLAG_BLOCKING);
59 if (!kbd->parent_sess)
60 return ENOMEM;
61
62 kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
63 if (!kbd->kbd_fun) {
64 async_hangup(kbd->parent_sess);
65 return ENOMEM;
66 }
67 kbd->kbd_fun->ops = &kbd_ops;
68 kbd->kbd_fun->driver_data = kbd;
69
70 int ret = ddf_fun_bind(kbd->kbd_fun);
71 if (ret != EOK) {
72 async_hangup(kbd->parent_sess);
73 kbd->kbd_fun->driver_data = NULL;
74 ddf_fun_destroy(kbd->kbd_fun);
75 return ENOMEM;
76 }
77
78 ret = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
79 if (ret != EOK) {
80 async_hangup(kbd->parent_sess);
81 ddf_fun_unbind(kbd->kbd_fun);
82 kbd->kbd_fun->driver_data = NULL;
83 ddf_fun_destroy(kbd->kbd_fun);
84 return ENOMEM;
85 }
86
87 kbd->polling_fibril = fibril_create(polling, kbd);
88 if (!kbd->polling_fibril) {
89 async_hangup(kbd->parent_sess);
90 ddf_fun_unbind(kbd->kbd_fun);
91 kbd->kbd_fun->driver_data = NULL;
92 ddf_fun_destroy(kbd->kbd_fun);
93 return ENOMEM;
94 }
95 fibril_add_ready(kbd->polling_fibril);
96 return EOK;
97}
98/*----------------------------------------------------------------------------*/
99int polling(void *arg)
100{
101 assert(arg);
102 xt_kbd_t *kbd = arg;
103
104 assert(kbd->parent_sess);
105 while (1) {
106 uint8_t code = 0;
107 ssize_t size = char_dev_read(kbd->parent_sess, &code, 1);
108 ddf_msg(LVL_DEBUG, "Got scancode: %hhx, size: %zd", code, size);
109 }
110}
111/*----------------------------------------------------------------------------*/
112void default_connection_handler(ddf_fun_t *fun,
113 ipc_callid_t icallid, ipc_call_t *icall)
114{
115 if (fun == NULL || fun->driver_data == NULL) {
116 ddf_msg(LVL_ERROR, "%s: Missing parameter.\n", __FUNCTION__);
117 async_answer_0(icallid, EINVAL);
118 return;
119 }
120
121 const sysarg_t method = IPC_GET_IMETHOD(*icall);
122 xt_kbd_t *kbd = fun->driver_data;
123
124 switch (method) {
125 case KBDEV_SET_IND:
126 /* XT keyboards do not support setting mods */
127 async_answer_0(icallid, ENOTSUP);
128 break;
129 /* This might be ugly but async_callback_receive_start makes no
130 * difference for incorrect call and malloc failure. */
131 case IPC_M_CONNECT_TO_ME: {
132 async_sess_t *sess =
133 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
134 /* Probably ENOMEM error, try again. */
135 if (sess == NULL) {
136 ddf_msg(LVL_WARN,
137 "Failed to create start input session");
138 async_answer_0(icallid, EAGAIN);
139 break;
140 }
141 if (kbd->input_sess == NULL) {
142 kbd->input_sess = sess;
143 ddf_msg(LVL_DEBUG, "Set input session");
144 async_answer_0(icallid, EOK);
145 } else {
146 ddf_msg(LVL_ERROR, "Input session already set");
147 async_answer_0(icallid, ELIMIT);
148 }
149 break;
150 }
151 default:
152 ddf_msg(LVL_ERROR,
153 "Unknown method: %d.\n", (int)method);
154 async_answer_0(icallid, EINVAL);
155 break;
156 }
157}
Note: See TracBrowser for help on using the repository browser.