source: mainline/uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c@ 01c3bb4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 01c3bb4 was 01c3bb4, checked in by Jakub Jermar <jakub@…>, 8 years ago

Convert call-handling syscalls to capabilities

This commit modifies the behavior of sys_ipc_wait_for_call() to return a
capability handle for requests. This capability handle can be used
either by sys_ipc_answer*() to answer the call or by sys_ipc_forward*()
to forward it further along. Answering or forwarding the call results in
destruction of the respective capability. For requests and
notifications, sys_ipc_wait_for_call() returns CAP_NIL and sets call
flags accordingly.

  • Property mode set to 100644
File size: 10.1 KB
Line 
1/*
2 * Copyright (c) 2010 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 mouse
30 * @{
31 */
32/**
33 * @file
34 * @brief Samsung Samsung S3C24xx on-chip ADC and touch-screen interface driver.
35 *
36 * This interface is present on the Samsung S3C24xx CPU (on the gta02 platform).
37 */
38
39#include <ddi.h>
40#include <loc.h>
41#include <io/console.h>
42#include <vfs/vfs.h>
43#include <ipc/mouseev.h>
44#include <async.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <sysinfo.h>
48#include <errno.h>
49#include <inttypes.h>
50#include "s3c24xx_ts.h"
51
52#define NAME "s3c24xx_ts"
53#define NAMESPACE "hid"
54
55static irq_cmd_t ts_irq_cmds[] = {
56 {
57 .cmd = CMD_ACCEPT
58 }
59};
60
61static irq_code_t ts_irq_code = {
62 0,
63 NULL,
64 sizeof(ts_irq_cmds) / sizeof(irq_cmd_t),
65 ts_irq_cmds
66};
67
68/** S3C24xx touchscreen instance structure */
69static s3c24xx_ts_t *ts;
70
71static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall,
72 void *arg);
73static void s3c24xx_ts_irq_handler(ipc_call_t *call, void *);
74static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts);
75static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts);
76static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts);
77static int s3c24xx_ts_init(s3c24xx_ts_t *ts);
78static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn);
79static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y);
80static int lin_map_range(int v, int i0, int i1, int o0, int o1);
81
82int main(int argc, char *argv[])
83{
84 printf("%s: S3C24xx touchscreen driver\n", NAME);
85
86 async_set_fallback_port_handler(s3c24xx_ts_connection, NULL);
87 int rc = loc_server_register(NAME);
88 if (rc != EOK) {
89 printf("%s: Unable to register driver.\n", NAME);
90 return rc;
91 }
92
93 ts = malloc(sizeof(s3c24xx_ts_t));
94 if (ts == NULL)
95 return -1;
96
97 if (s3c24xx_ts_init(ts) != EOK)
98 return -1;
99
100 rc = loc_service_register(NAMESPACE "/mouse", &ts->service_id);
101 if (rc != EOK) {
102 printf(NAME ": Unable to register device %s.\n",
103 NAMESPACE "/mouse");
104 return -1;
105 }
106
107 printf(NAME ": Registered device %s.\n", NAMESPACE "/mouse");
108
109 printf(NAME ": Accepting connections\n");
110 task_retval(0);
111 async_manager();
112
113 /* Not reached */
114 return 0;
115}
116
117/** Initialize S3C24xx touchscreen interface. */
118static int s3c24xx_ts_init(s3c24xx_ts_t *ts)
119{
120 void *vaddr;
121 sysarg_t inr;
122
123 inr = S3C24XX_TS_INR;
124 ts->paddr = S3C24XX_TS_ADDR;
125
126 if (pio_enable((void *) ts->paddr, sizeof(s3c24xx_adc_io_t),
127 &vaddr) != 0)
128 return -1;
129
130 ts->io = vaddr;
131 ts->client_sess = NULL;
132 ts->state = ts_wait_pendown;
133 ts->last_x = 0;
134 ts->last_y = 0;
135
136 printf(NAME ": device at physical address %p, inr %" PRIun ".\n",
137 (void *) ts->paddr, inr);
138
139 async_irq_subscribe(inr, s3c24xx_ts_irq_handler, NULL, &ts_irq_code);
140
141 s3c24xx_ts_wait_for_int_mode(ts, updn_down);
142
143 return EOK;
144}
145
146/** Switch interface to wait for interrupt mode.
147 *
148 * In this mode we receive an interrupt when pen goes up/down, depending
149 * on @a updn.
150 *
151 * @param ts Touchscreen instance
152 * @param updn @c updn_up to wait for pen up, @c updn_down to wait for pen
153 * down.
154 */
155static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn)
156{
157 uint32_t con, tsc;
158
159 /*
160 * Configure ADCCON register
161 */
162
163 con = pio_read_32(&ts->io->con);
164
165 /* Disable standby, disable start-by-read, clear manual start bit */
166 con = con & ~(ADCCON_STDBM | ADCCON_READ_START | ADCCON_ENABLE_START);
167
168 /* Set prescaler value 0xff, XP for input. */
169 con = con | (ADCCON_PRSCVL(0xff) << 6) | ADCCON_SEL_MUX(SMUX_XP);
170
171 /* Enable prescaler. */
172 con = con | ADCCON_PRSCEN;
173
174 pio_write_32(&ts->io->con, con);
175
176 /*
177 * Configure ADCTSC register
178 */
179
180 tsc = pio_read_32(&ts->io->tsc);
181
182 /* Select whether waiting for pen up or pen down. */
183 if (updn == updn_up)
184 tsc |= ADCTSC_DSUD_UP;
185 else
186 tsc &= ~ADCTSC_DSUD_UP;
187
188 /*
189 * Enable XP pull-up and disable all drivers except YM. This is
190 * according to the manual. This gives us L on XP input when touching
191 * and (pulled up to) H when not touching.
192 */
193 tsc = tsc & ~(ADCTSC_XM_ENABLE | ADCTSC_AUTO_PST |
194 ADCTSC_PULLUP_DISABLE);
195 tsc = tsc | ADCTSC_YP_DISABLE | ADCTSC_XP_DISABLE | ADCTSC_YM_ENABLE;
196
197 /* Select wait-for-interrupt mode. */
198 tsc = (tsc & ~ADCTSC_XY_PST_MASK) | ADCTSC_XY_PST_WAITINT;
199
200 pio_write_32(&ts->io->tsc, tsc);
201}
202
203/** Handle touchscreen interrupt */
204static void s3c24xx_ts_irq_handler(ipc_call_t *call, void *arg)
205{
206 ts_updn_t updn;
207
208 (void) call;
209
210 /* Read up/down interrupt flags. */
211 updn = pio_read_32(&ts->io->updn);
212
213 if (updn & (ADCUPDN_TSC_DN | ADCUPDN_TSC_UP)) {
214 /* Clear up/down interrupt flags. */
215 pio_write_32(&ts->io->updn, updn &
216 ~(ADCUPDN_TSC_DN | ADCUPDN_TSC_UP));
217 }
218
219 if (updn & ADCUPDN_TSC_DN) {
220 /* Pen-down interrupt */
221 s3c24xx_ts_pen_down(ts);
222 } else if (updn & ADCUPDN_TSC_UP) {
223 /* Pen-up interrupt */
224 s3c24xx_ts_pen_up(ts);
225 } else {
226 /* Presumably end-of-conversion interrupt */
227
228 /* Check end-of-conversion flag. */
229 if ((pio_read_32(&ts->io->con) & ADCCON_ECFLG) == 0) {
230 printf(NAME ": Unrecognized ts int.\n");
231 return;
232 }
233
234 if (ts->state != ts_sample_pos) {
235 /*
236 * We got an extra interrupt ater switching to
237 * wait for interrupt mode.
238 */
239 return;
240 }
241
242 /* End-of-conversion interrupt */
243 s3c24xx_ts_eoc(ts);
244 }
245}
246
247/** Handle pen-down interrupt.
248 *
249 * @param ts Touchscreen instance
250 */
251static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts)
252{
253 /* Pen-down interrupt */
254
255 ts->state = ts_sample_pos;
256
257 /* Enable auto xy-conversion mode */
258 pio_write_32(&ts->io->tsc, (pio_read_32(&ts->io->tsc)
259 & ~3) | 4);
260
261 /* Start the conversion. */
262 pio_write_32(&ts->io->con, pio_read_32(&ts->io->con)
263 | ADCCON_ENABLE_START);
264}
265
266/** Handle pen-up interrupt.
267 *
268 * @param ts Touchscreen instance
269 */
270static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts)
271{
272 int button, press;
273
274 /* Pen-up interrupt */
275
276 ts->state = ts_wait_pendown;
277
278 button = 1;
279 press = 0;
280
281 async_exch_t *exch = async_exchange_begin(ts->client_sess);
282 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, button, press);
283 async_exchange_end(exch);
284
285 s3c24xx_ts_wait_for_int_mode(ts, updn_down);
286}
287
288/** Handle end-of-conversion interrupt.
289 *
290 * @param ts Touchscreen instance
291 */
292static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts)
293{
294 uint32_t data;
295 int button, press;
296 int smp0, smp1;
297 int x_pos, y_pos;
298 int dx, dy;
299
300 ts->state = ts_wait_penup;
301
302 /* Read in sampled data. */
303
304 data = pio_read_32(&ts->io->dat0);
305 smp0 = data & 0x3ff;
306
307 data = pio_read_32(&ts->io->dat1);
308 smp1 = data & 0x3ff;
309
310 /* Convert to screen coordinates. */
311 s3c24xx_ts_convert_samples(smp0, smp1, &x_pos, &y_pos);
312
313 printf("s0: 0x%03x, s1:0x%03x -> x:%d,y:%d\n", smp0, smp1,
314 x_pos, y_pos);
315
316 /* Get differences. */
317 dx = x_pos - ts->last_x;
318 dy = y_pos - ts->last_y;
319
320 button = 1;
321 press = 1;
322
323 /* Send notifications to client. */
324 async_exch_t *exch = async_exchange_begin(ts->client_sess);
325 async_msg_2(exch, MOUSEEV_MOVE_EVENT, dx, dy);
326 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, button, press);
327 async_exchange_end(exch);
328
329 ts->last_x = x_pos;
330 ts->last_y = y_pos;
331
332 s3c24xx_ts_wait_for_int_mode(ts, updn_up);
333}
334
335/** Convert sampled data to screen coordinates. */
336static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y)
337{
338 /*
339 * The orientation and display dimensions are GTA02-specific and the
340 * calibration values might even specific to the individual piece
341 * of hardware.
342 *
343 * The calibration values can be obtained by touching corners
344 * of the screen with the stylus and noting the sampled values.
345 */
346 *x = lin_map_range(smp1, 0xa1, 0x396, 0, 479);
347 *y = lin_map_range(smp0, 0x69, 0x38a, 639, 0);
348}
349
350/** Map integer from one range to another range in a linear fashion.
351 *
352 * i0 < i1 is required. i0 is mapped to o0, i1 to o1. If o1 < o0, then the
353 * mapping will be descending. If v is outside of [i0, i1], it is clamped.
354 *
355 * @param v Value to map.
356 * @param i0 Lower bound of input range.
357 * @param i1 Upper bound of input range.
358 * @param o0 First bound of output range.
359 * @param o1 Second bound of output range.
360 *
361 * @return Mapped value ov, o0 <= ov <= o1.
362 */
363static int lin_map_range(int v, int i0, int i1, int o0, int o1)
364{
365 if (v < i0)
366 v = i0;
367
368 if (v > i1)
369 v = i1;
370
371 return o0 + (o1 - o0) * (v - i0) / (i1 - i0);
372}
373
374/** Handle mouse client connection. */
375static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall,
376 void *arg)
377{
378 async_answer_0(iid, EOK);
379
380 while (true) {
381 ipc_call_t call;
382 ipc_callid_t callid = async_get_call(&call);
383
384 if (!IPC_GET_IMETHOD(call)) {
385 if (ts->client_sess != NULL) {
386 async_hangup(ts->client_sess);
387 ts->client_sess = NULL;
388 }
389
390 async_answer_0(callid, EOK);
391 return;
392 }
393
394 async_sess_t *sess =
395 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
396 if (sess != NULL) {
397 if (ts->client_sess == NULL) {
398 ts->client_sess = sess;
399 async_answer_0(callid, EOK);
400 } else
401 async_answer_0(callid, ELIMIT);
402 } else
403 async_answer_0(callid, EINVAL);
404 }
405}
406
407/** @}
408 */
Note: See TracBrowser for help on using the repository browser.