source: mainline/uspace/srv/hid/input/ctl/kbdev.c@ edb3cf2

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

hidsrv: Destroy kbdev on hangup.

Fixes phone leak.
Fix possible session leak in error path.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
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 kbd_ctl
30 * @ingroup input
31 * @{
32 */
33/**
34 * @file
35 * @brief Keyboard device connector controller driver.
36 */
37
38#include <async.h>
39#include <bool.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <gsp.h>
43#include <io/console.h>
44#include <io/keycode.h>
45#include <ipc/kbdev.h>
46#include <input.h>
47#include <kbd.h>
48#include <kbd_ctl.h>
49#include <kbd_port.h>
50#include <loc.h>
51#include <stdlib.h>
52#include <vfs/vfs_sess.h>
53#include <sys/typefmt.h>
54
55static int kbdev_ctl_init(kbd_dev_t *);
56static void kbdev_ctl_set_ind(kbd_dev_t *, unsigned int);
57
58static void kbdev_callback_conn(ipc_callid_t, ipc_call_t *, void *arg);
59
60kbd_ctl_ops_t kbdev_ctl = {
61 .parse = NULL,
62 .init = kbdev_ctl_init,
63 .set_ind = kbdev_ctl_set_ind
64};
65
66/** Kbdev softstate */
67typedef struct {
68 /** Link to generic keyboard device */
69 kbd_dev_t *kbd_dev;
70
71 /** Session with kbdev device */
72 async_sess_t *sess;
73} kbdev_t;
74
75static kbdev_t *kbdev_new(kbd_dev_t *kdev)
76{
77 kbdev_t *kbdev;
78
79 kbdev = calloc(1, sizeof(kbdev_t));
80 if (kbdev == NULL)
81 return NULL;
82
83 kbdev->kbd_dev = kdev;
84
85 return kbdev;
86}
87
88static void kbdev_destroy(kbdev_t *kbdev)
89{
90 if (kbdev->sess != NULL)
91 async_hangup(kbdev->sess);
92 free(kbdev);
93}
94
95static int kbdev_ctl_init(kbd_dev_t *kdev)
96{
97 async_sess_t *sess;
98 async_exch_t *exch;
99 kbdev_t *kbdev;
100 int rc;
101
102 sess = loc_service_connect(EXCHANGE_SERIALIZE, kdev->svc_id, 0);
103 if (sess == NULL) {
104 printf("%s: Failed starting session with '%s.'\n", NAME,
105 kdev->svc_name);
106 return -1;
107 }
108
109 kbdev = kbdev_new(kdev);
110 if (kbdev == NULL) {
111 printf("%s: Failed allocating device structure for '%s'.\n",
112 NAME, kdev->svc_name);
113 async_hangup(sess);
114 return -1;
115 }
116
117 kbdev->sess = sess;
118
119 exch = async_exchange_begin(sess);
120 if (exch == NULL) {
121 printf("%s: Failed starting exchange with '%s'.\n", NAME,
122 kdev->svc_name);
123 kbdev_destroy(kbdev);
124 return -1;
125 }
126
127 rc = async_connect_to_me(exch, 0, 0, 0, kbdev_callback_conn, kbdev);
128 if (rc != EOK) {
129 printf("%s: Failed creating callback connection from '%s'.\n",
130 NAME, kdev->svc_name);
131 async_exchange_end(exch);
132 kbdev_destroy(kbdev);
133 return -1;
134 }
135
136 async_exchange_end(exch);
137
138 kdev->ctl_private = (void *) kbdev;
139 return 0;
140}
141
142static void kbdev_ctl_set_ind(kbd_dev_t *kdev, unsigned mods)
143{
144 async_sess_t *sess;
145 async_exch_t *exch;
146
147 sess = ((kbdev_t *) kdev->ctl_private)->sess;
148
149 exch = async_exchange_begin(sess);
150 if (!exch)
151 return;
152
153 async_msg_1(exch, KBDEV_SET_IND, mods);
154 async_exchange_end(exch);
155}
156
157static void kbdev_callback_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
158{
159 kbdev_t *kbdev;
160 int retval;
161 int type, key;
162
163 /* Kbdev device structure */
164 kbdev = arg;
165
166 while (true) {
167 ipc_call_t call;
168 ipc_callid_t callid;
169
170 callid = async_get_call(&call);
171 if (!IPC_GET_IMETHOD(call)) {
172 kbdev_destroy(kbdev);
173 return;
174 }
175
176 switch (IPC_GET_IMETHOD(call)) {
177 case KBDEV_EVENT:
178 /* Got event from keyboard device */
179 retval = 0;
180 type = IPC_GET_ARG1(call);
181 key = IPC_GET_ARG2(call);
182 kbd_push_event(kbdev->kbd_dev, type, key);
183 break;
184 default:
185 retval = ENOTSUP;
186 break;
187 }
188
189 async_answer_0(callid, retval);
190 }
191}
192
193/**
194 * @}
195 */
Note: See TracBrowser for help on using the repository browser.