alps.c revision 1.1 1 /* $NetBSD: alps.c,v 1.1 2017/08/13 08:49:27 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Ryo ONODERA <ryo (at) tetera.org>
5 * Copyright (c) 2008 Jared D. McNeill <jmcneill (at) invisible.ca>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "opt_pms.h"
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: alps.c,v 1.1 2017/08/13 08:49:27 christos Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/bus.h>
41 #include <sys/bitops.h>
42
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/wscons/wsmousevar.h>
45
46 #include <dev/pckbport/pckbportvar.h>
47 #include <dev/pckbport/pmsreg.h>
48 #include <dev/pckbport/pmsvar.h>
49 #include <dev/pckbport/alpsreg.h>
50 #include <dev/pckbport/alpsvar.h>
51
52 /* #define ALPS_DEBUG */
53
54 static int alps_touchpad_xy_unprecision_nodenum;
55 static int alps_trackstick_xy_precision_nodenum;
56
57 static int alps_touchpad_xy_unprecision = 2;
58 static int alps_trackstick_xy_precision = 1;
59
60 static void pms_alps_input_v7(void *, int);
61 static void pms_alps_input_v2(void *, int);
62
63 static int
64 pms_sysctl_alps_verify(SYSCTLFN_ARGS)
65 {
66 int error, t;
67 struct sysctlnode node;
68
69 node = *rnode;
70 t = *(int *)rnode->sysctl_data;
71 node.sysctl_data = &t;
72 error = sysctl_lookup(SYSCTLFN_CALL(&node));
73 if (error || newp == NULL)
74 return error;
75
76 if (node.sysctl_num == alps_touchpad_xy_unprecision_nodenum ||
77 node.sysctl_num == alps_trackstick_xy_precision_nodenum) {
78 if (t < 0 || t > 7)
79 return EINVAL;
80 } else
81 return EINVAL;
82
83 *(int *)rnode->sysctl_data = t;
84
85 return 0;
86
87 }
88
89 static void
90 pms_sysctl_alps(struct sysctllog **clog)
91 {
92 const struct sysctlnode *node;
93 int rc, root_num;
94
95 if ((rc = sysctl_createv(clog, 0, NULL, &node,
96 CTLFLAG_PERMANENT, CTLTYPE_NODE, "alps",
97 SYSCTL_DESCR("ALPS touchpad controls"),
98 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
99 goto err;
100
101 root_num = node->sysctl_num;
102
103 if ((rc = sysctl_createv(clog, 0, NULL, &node,
104 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
105 CTLTYPE_INT, "touchpad_xy_precision_shift",
106 SYSCTL_DESCR("Touchpad X/Y-axis precision shift value"),
107 pms_sysctl_alps_verify, 0,
108 &alps_touchpad_xy_unprecision,
109 0, CTL_HW, root_num, CTL_CREATE,
110 CTL_EOL)) != 0)
111 goto err;
112 alps_touchpad_xy_unprecision_nodenum = node->sysctl_num;
113
114 if ((rc = sysctl_createv(clog, 0, NULL, &node,
115 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
116 CTLTYPE_INT, "tackstick_xy_precision_shift",
117 SYSCTL_DESCR("Trackstick X/Y-axis precision value"),
118 pms_sysctl_alps_verify, 0,
119 &alps_trackstick_xy_precision,
120 0, CTL_HW, root_num, CTL_CREATE,
121 CTL_EOL)) != 0)
122 goto err;
123 alps_trackstick_xy_precision_nodenum = node->sysctl_num;
124
125 return;
126
127 err:
128 aprint_error("%s: sysctl_createv failed (rc = %d)\n",
129 __func__, rc);
130 }
131
132 /*
133 * Publish E6 report command and get E6 signature,
134 * then check the signature
135 */
136 static int
137 pms_alps_e6sig(struct pms_softc *psc, uint8_t *e6sig)
138 {
139 uint8_t cmd[2];
140 int res;
141
142 e6sig[0] = 0;
143 cmd[0] = PMS_SET_RES; /* E8 */
144 cmd[1] = 0;
145 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
146 cmd, 2, 0, NULL, 0)) != 0)
147 goto err;
148 cmd[0] = PMS_SET_SCALE11; /* E6 */
149 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
150 cmd, 1, 0, NULL, 0)) != 0)
151 goto err;
152 cmd[0] = PMS_SET_SCALE11; /* E6 */
153 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
154 cmd, 1, 0, NULL, 0)) != 0)
155 goto err;
156 cmd[0] = PMS_SET_SCALE11; /* E6 */
157 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
158 cmd, 1, 0, NULL, 0)) != 0)
159 goto err;
160 e6sig[0] = e6sig[1] = e6sig[2] = 0;
161 /* Get E6 signature */
162 cmd[0] = PMS_GET_SCALE; /* E9 */
163 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
164 cmd, 1, 3, e6sig, 0)) != 0)
165 goto err;
166
167 /* ALPS input device returns 00-00-64 as E6 signature */
168 if (e6sig[0] != 0x00 || e6sig[1] != 0x00 ||
169 e6sig[2] != 0x64) {
170 return EINVAL;
171 }
172
173 aprint_debug_dev(psc->sc_dev,
174 "ALPS PS/2 E6 signature: 0x%X 0x%X 0x%X\n",
175 e6sig[0], e6sig[1], e6sig[2]);
176
177 return 0;
178 err:
179 aprint_error_dev(psc->sc_dev, "Failed to get E6 signature.\n");
180 return res;
181 }
182
183 /*
184 * Publish E7 report command and get E7 signature
185 */
186 static int
187 pms_alps_e7sig(struct pms_softc *psc, uint8_t *e7sig)
188 {
189 uint8_t cmd[2];
190 int res;
191
192 cmd[0] = PMS_SET_RES; /* E8 */
193 cmd[1] = 0;
194 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
195 cmd, 2, 0, NULL, 0)) != 0)
196 goto err;
197 cmd[0] = PMS_SET_SCALE21; /* E7 */
198 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
199 cmd, 1, 0, NULL, 0)) != 0)
200 goto err;
201 cmd[0] = PMS_SET_SCALE21; /* E7 */
202 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
203 cmd, 1, 0, NULL, 0)) != 0)
204 goto err;
205 cmd[0] = PMS_SET_SCALE21; /* E7 */
206 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
207 cmd, 1, 0, NULL, 0)) != 0)
208 goto err;
209 e7sig[0] = e7sig[1] = e7sig[2] = 0;
210 cmd[0] = PMS_GET_SCALE; /* E9 */
211 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
212 cmd, 1, 3, e7sig, 0)) != 0)
213 goto err;
214
215 aprint_debug_dev(psc->sc_dev,
216 "ALPS PS/2 E7 signature: 0x%X 0x%X 0x%X\n",
217 e7sig[0], e7sig[1], e7sig[2]);
218
219 return 0;
220 err:
221 aprint_error_dev(psc->sc_dev, "Failed to get E7 signature.\n");
222 return res;
223 }
224
225 /*
226 * Publish EC command and get EC signature
227 */
228 static int
229 pms_alps_ecsig(struct pms_softc *psc, uint8_t *ecsig)
230 {
231 uint8_t cmd[2];
232 int res;
233
234 cmd[0] = PMS_SET_RES; /* E8 */
235 cmd[1] = 0;
236 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
237 cmd, 2, 0, NULL, 0)) != 0)
238 goto err;
239 cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
240 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
241 cmd, 1, 0, NULL, 0)) != 0)
242 goto err;
243 cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
244 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
245 cmd, 1, 0, NULL, 0)) != 0)
246 goto err;
247 cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
248 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
249 cmd, 1, 0, NULL, 0)) != 0)
250 goto err;
251 ecsig[0] = ecsig[1] = ecsig[2] = 0;
252 cmd[0] = PMS_GET_SCALE; /* E9 */
253 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
254 cmd, 1, 3, ecsig, 0)) != 0)
255 goto err;
256
257 aprint_debug_dev(psc->sc_dev,
258 "ALPS PS/2 EC signature: 0x%X 0x%X 0x%X\n",
259 ecsig[0], ecsig[1], ecsig[2]);
260
261 return 0;
262
263 err:
264 aprint_error_dev(psc->sc_dev, "Failed to get EC signature.\n");
265 return res;
266 }
267
268 /*
269 * Enter to command mode
270 */
271 static int
272 pms_alps_start_command_mode(struct pms_softc *psc)
273 {
274 uint8_t cmd[1];
275 uint8_t resp[3];
276 int res;
277
278 cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
279 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
280 cmd, 1, 0, NULL, 0)) != 0)
281 goto err;
282 cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
283 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
284 cmd, 1, 0, NULL, 0)) != 0)
285 goto err;
286 cmd[0] = PMS_RESET_WRAP_MODE; /* EC */
287 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
288 cmd, 1, 0, NULL, 0)) != 0)
289 goto err;
290 resp[0] = resp[1] = resp[2] = 0;
291 cmd[0] = PMS_GET_SCALE; /* E9 */
292 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
293 cmd, 1, 3, resp, 0)) != 0)
294 goto err;
295
296 aprint_debug_dev(psc->sc_dev, "ALPS Firmware ID: 0x%x 0x%X 0x%X\n",
297 resp[0], resp[1], resp[2]);
298
299 if (resp[0] != 0x88 || (resp[1] & __BITS(7, 4)) != 0xb0)
300 return EINVAL;
301
302 return 0;
303 err:
304 aprint_error_dev(psc->sc_dev, "Failed to start command mode.\n");
305 return res;
306 }
307
308 /*
309 * End command mode
310 */
311 static int
312 pms_alps_end_command_mode(struct pms_softc *psc)
313 {
314 int res;
315 uint8_t cmd[1];
316
317 cmd[0] = PMS_SET_STREAM; /* EA */
318 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
319 cmd, 1, 0, NULL, 0)) != 0)
320 goto err;
321
322 return res;
323
324 err:
325 aprint_error_dev(psc->sc_dev, "Failed to end command mode.\n");
326 return res;
327 }
328
329 /*
330 * Write nibble (4-bit) data
331 */
332 static int
333 pms_alps_cm_write_nibble(pckbport_tag_t tag, pckbport_slot_t slot, uint8_t nibble)
334 {
335 uint8_t cmd[2];
336 uint8_t resp[3];
337 int sendparam;
338 int recieve;
339 int res;
340
341 sendparam = alps_v7_nibble_command_data_arr[nibble].sendparam;
342 recieve= alps_v7_nibble_command_data_arr[nibble].recieve;
343 cmd[0] = alps_v7_nibble_command_data_arr[nibble].command;
344 if (recieve) {
345 if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 3, resp, 0)) != 0) {
346 aprint_error("send nibble error: %d\n", res);
347 }
348 } else if (sendparam) {
349 cmd[1] = alps_v7_nibble_command_data_arr[nibble].data;
350 if ((res = pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0)) != 0) {
351 aprint_error("send nibble error: %d\n", res);
352 }
353 } else {
354 if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 0, NULL, 0)) != 0) {
355 aprint_error("send nibble error: %d\n", res);
356 }
357 }
358
359 return res;
360 }
361
362 /*
363 * Set an register address for read and write
364 */
365 static int
366 pms_alps_set_address(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg)
367 {
368 uint8_t cmd[1];
369 uint8_t nibble;
370 int res;
371
372 cmd[0] = PMS_RESET_WRAP_MODE;
373 if ((res = pckbport_poll_cmd(tag, slot, cmd, 1, 0, NULL, 0)) != 0)
374 goto err;
375
376 /* Set address */
377 nibble = (reg >> 12) & __BITS(3, 0);
378 if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
379 goto err;
380 }
381 nibble = (reg >> 8) & __BITS(3, 0);
382 if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
383 goto err;
384 }
385 nibble = (reg >> 4) & __BITS(3, 0);
386 if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
387 goto err;
388 }
389 nibble = (reg >> 0) & __BITS(3, 0);
390 if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
391 goto err;
392 }
393 return res;
394
395 err:
396 aprint_error("Failed to set addess.\n");
397 return res;
398 }
399
400 /*
401 * Read one byte from register
402 */
403 static int
404 pms_alps_cm_read_1(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg,
405 uint8_t *val)
406 {
407 uint8_t cmd[1];
408 uint8_t resp[3];
409 int res;
410
411 if ((res = pms_alps_set_address(tag, slot, reg)) != 0)
412 goto err;
413
414 cmd[0] = PMS_SEND_DEV_STATUS;
415 if ((res = pckbport_poll_cmd(tag, slot,
416 cmd, 1, 3, resp, 0)) != 0) {
417 goto err;
418 }
419
420 if (reg != ((resp[0] << 8) | resp[1])) {
421 return EINVAL;
422 }
423
424 *val = resp[2];
425 return res;
426
427 err:
428 aprint_error("Failed to read a value.\n");
429 *val = 0;
430 return res;
431 }
432
433 /*
434 * Write one byte to register
435 */
436 static int
437 pms_alps_cm_write_1(pckbport_tag_t tag, pckbport_slot_t slot, uint16_t reg,
438 uint8_t val)
439 {
440 uint8_t nibble;
441 int res;
442
443 if ((res = pms_alps_set_address(tag, slot, reg)) != 0)
444 goto err;
445
446 nibble = __SHIFTOUT(val, __BITS(7, 4));
447 if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
448 goto err;
449 }
450
451 nibble = __SHIFTOUT(val, __BITS(3, 0));
452 if ((res = pms_alps_cm_write_nibble(tag, slot, nibble)) != 0) {
453 goto err;
454 }
455
456 return res;
457 err:
458 aprint_error("Failed to write a value.\n");
459 return res;
460 }
461
462 /*
463 * Not used practically for initialization
464 */
465 static int
466 pms_alps_get_resolution_v7(struct pms_softc *psc)
467 {
468 #if 0
469 struct alps_softc *sc = &psc->u.alps;
470 #endif
471 pckbport_tag_t tag = psc->sc_kbctag;
472 pckbport_slot_t slot = psc->sc_kbcslot;
473
474 int res;
475 uint8_t ret;
476 #if 0
477 uint32_t x_pitch, y_pitch;
478 uint32_t x_elec, y_elec;
479 uint32_t x_phy, y_phy;
480 #endif
481 /* X/Y pitch */
482 if ((res = pms_alps_cm_read_1(tag, slot, 0xc397, &ret)) != 0) {
483 goto err;
484 }
485 #if 0
486 /* X pitch */
487 x_pitch = __SHIFTOUT(ret, __BITS(7, 4)); /* Higher 4-bit */
488 x_pitch = x_pitch * 2 + 50; /* Unit = 0.1mm */
489
490 /* Y pitch */
491 y_pitch = ret & __BITS(3, 0); /* Lower 4-bit */
492 y_pitch = y_pitch * 2 + 36; /* Unit = 0.1mm */
493
494 /* X/Y electrode */
495 if ((res = pms_alps_cm_read_1(tag, slot, 0xc397 + 1, &ret)) != 0) {
496 goto err;
497 }
498
499 /* X electrode */
500 x_elec = __SHIFTOUT(ret, __BITS(7, 4)); /* Higher 4-bit */
501 x_elec = x_elec + 17;
502
503 /* Y electrode */
504 y_elec = ret & __BITS(3, 0); /* Lower 4-bit */
505 y_elec = y_elec + 13;
506
507 /* X/Y physical in unit = 0.1mm */
508 /* X physical */
509 x_phy = (x_elec - 1) * x_pitch;
510 y_phy = (y_elec - 1) * y_pitch;
511
512 /* X/Y resolution (unit) */
513 sc->res_x = 0xfff * 10 / x_phy;
514 sc->res_y = 0x7ff * 10 / y_phy;
515 #endif
516 return res;
517
518 err:
519 aprint_error("Failed to get resolution.\n");
520 return res;
521 }
522
523 /*
524 * Enable tap mode for V2 device
525 */
526 static int
527 pms_alps_enable_tap_mode_v2(struct pms_softc *psc)
528 {
529 uint8_t cmd[2];
530 uint8_t resp[3];
531 int res;
532
533 resp[0] = resp[1] = resp[2] = 0;
534 cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
535 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
536 cmd, 1, 3, resp, 0)) != 0)
537 goto err;
538 cmd[0] = PMS_DEV_DISABLE; /* F5 */
539 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
540 cmd, 1, 0, NULL, 0)) != 0)
541 goto err;
542 cmd[0] = PMS_DEV_DISABLE; /* F5 */
543 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
544 cmd, 1, 0, NULL, 0)) != 0)
545 goto err;
546 cmd[0] = PMS_SET_SAMPLE;
547 cmd[1] = 0x0a; /* argument */
548 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
549 cmd, 2, 0, NULL, 0)) != 0)
550 goto err;
551
552 /* Get status */
553 cmd[0] = PMS_DEV_DISABLE; /* F5 */
554 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
555 cmd, 1, 0, NULL, 0)) != 0)
556 goto err;
557 cmd[0] = PMS_DEV_DISABLE; /* F5 */
558 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
559 cmd, 1, 0, NULL, 0)) != 0)
560 goto err;
561 cmd[0] = PMS_DEV_DISABLE; /* F5 */
562 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
563 cmd, 1, 0, NULL, 0)) != 0)
564 goto err;
565 resp[0] = resp[1] = resp[2] = 0;
566 cmd[0] = PMS_SEND_DEV_STATUS; /* E9 */
567 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
568 cmd, 1, 3, resp, 0)) != 0)
569 goto err;
570
571 aprint_debug_dev(psc->sc_dev, "Tap mode is enabled.\n");
572
573 return 0;
574 err:
575 aprint_error_dev(psc->sc_dev, "Failed to enable tap mode.\n");
576 return res;
577 }
578
579 static int
580 pms_alps_init_v2(struct pms_softc *psc)
581 {
582 uint8_t cmd[1];
583 int res;
584
585 if ((res = pms_alps_enable_tap_mode_v2(psc)) != 0)
586 goto err;
587
588 /* Enable absolute mode */
589 cmd[0] = PMS_DEV_DISABLE; /* F5 */
590 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
591 cmd, 1, 0, NULL, 0)) != 0)
592 goto err;
593 cmd[0] = PMS_DEV_DISABLE; /* F5 */
594 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
595 cmd, 1, 0, NULL, 0)) != 0)
596 goto err;
597 cmd[0] = PMS_DEV_DISABLE; /* F5 */
598 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
599 cmd, 1, 0, NULL, 0)) != 0)
600 goto err;
601 cmd[0] = PMS_DEV_DISABLE; /* F5 */
602 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
603 cmd, 1, 0, NULL, 0)) != 0)
604 goto err;
605 cmd[0] = PMS_DEV_ENABLE; /* F4 */
606 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
607 cmd, 1, 0, NULL, 0)) != 0)
608 goto err;
609
610 /* Enable remote mode */
611 cmd[0] = PMS_SET_REMOTE_MODE; /* F0 */
612 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
613 cmd, 1, 0, NULL, 0)) != 0)
614 goto err;
615
616 /* Start stream mode to get data */
617 cmd[0] = PMS_SET_STREAM; /* EA */
618 if ((res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
619 cmd, 1, 0, NULL, 0)) != 0)
620 goto err;
621
622
623 return 0;
624
625 err:
626 aprint_error_dev(psc->sc_dev, "Failed to initialize V2 device.\n");
627 return res;
628 }
629
630 static int
631 pms_alps_init_v7(struct pms_softc *psc)
632 {
633 uint8_t val;
634 uint8_t nibble;
635 int res;
636
637 /* Start command mode */
638 if ((res = pms_alps_start_command_mode(psc)) != 0) {
639 goto err;
640 }
641 if ((res = pms_alps_cm_read_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2d9, &val)) != 0) {
642 goto err;
643 }
644 if ((res = pms_alps_get_resolution_v7(psc)) != 0) {
645 goto err;
646 }
647 if ((res = pms_alps_cm_write_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2c9, 0x64)) != 0) {
648 goto err;
649 }
650
651 /* Start absolute mode */
652 if ((res = pms_alps_cm_read_1(psc->sc_kbctag, psc->sc_kbcslot, 0xc2c4, &val)) != 0) {
653 goto err;
654 }
655 /* Do not set address before this, so do not use pms_cm_write_1() */
656 val = val | __BIT(1);
657 nibble = __SHIFTOUT(val, __BITS(7, 4));
658 if ((res = pms_alps_cm_write_nibble(psc->sc_kbctag, psc->sc_kbcslot, nibble)) != 0) {
659 goto err;
660 }
661 nibble = __SHIFTOUT(val, __BITS(3, 0));
662 if ((res = pms_alps_cm_write_nibble(psc->sc_kbctag, psc->sc_kbcslot, nibble)) != 0) {
663 goto err;
664 }
665
666 /* End command mode */
667 if ((res = pms_alps_end_command_mode(psc)) != 0)
668 goto err;
669
670 return res;
671
672 err:
673 (void)pms_alps_end_command_mode(psc);
674 aprint_error_dev(psc->sc_dev, "Failed to initialize V7 device.\n");
675 return res;
676 }
677
678 int
679 pms_alps_probe_init(void *opaque)
680 {
681 struct pms_softc *psc = opaque;
682 struct alps_softc *sc = &psc->u.alps;
683 struct sysctllog *clog = NULL;
684 uint8_t e6sig[3];
685 uint8_t e7sig[3];
686 uint8_t ecsig[3];
687 int res;
688
689 sc->last_x1 = 0;
690 sc->last_y1 = 0;
691 sc->last_x2 = 0;
692 sc->last_y2 = 0;
693 sc->last_nfingers = 0;
694
695 pckbport_flush(psc->sc_kbctag, psc->sc_kbcslot);
696
697 if ((res = pms_alps_e6sig(psc, e6sig)) != 0)
698 goto err;
699
700 if ((res = pms_alps_e7sig(psc, e7sig)) != 0)
701 goto err;
702
703 if ((res = pms_alps_ecsig(psc, ecsig)) != 0)
704 goto err;
705
706 /* Determine protocol version */
707 if ((ecsig[0] == 0x88) && (__SHIFTOUT(ecsig[1], __BITS(7, 4)) == 0x0b)) {
708 /* V7 device in Toshiba dynabook R63/PS */
709 sc->version = ALPS_PROTO_V7;
710 } else if ((e7sig[0] == 0x73) && (e7sig[1] == 0x02) &&
711 (e7sig[2] == 0x14)) {
712 /* V2 device in NEC VJ22MF-7 (VersaPro JVF-7) */
713 sc->version = ALPS_PROTO_V2;
714 }
715
716 if (sc->version == ALPS_PROTO_V7) {
717 /* Initialize V7 device */
718 if ((res = pms_alps_init_v7(psc)) != 0)
719 goto err;
720 aprint_normal_dev(psc->sc_dev,
721 "ALPS PS/2 V7 pointing device\n");
722 } else if (sc->version == ALPS_PROTO_V2) {
723 /* Initialize V2 pointing device */
724 if ((res = pms_alps_init_v2(psc)) != 0)
725 goto err;
726 aprint_normal_dev(psc->sc_dev,
727 "ALPS PS/2 V2 pointing device\n");
728 } else {
729 aprint_error_dev(psc->sc_dev, "No supported device found.\n");
730 return EINVAL;
731 }
732
733 /* From sysctl */
734 pms_sysctl_alps(&clog);
735 /* Register hundler */
736 if (sc->version == ALPS_PROTO_V7) {
737 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
738 pms_alps_input_v7, psc, device_xname(psc->sc_dev));
739 } else if (sc->version == ALPS_PROTO_V2) {
740 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
741 pms_alps_input_v2, psc, device_xname(psc->sc_dev));
742 } else {
743 aprint_error_dev(psc->sc_dev, "No supported device found.\n");
744 return EINVAL;
745 }
746 /* Palm detection is enabled. */
747
748 return 0;
749
750 err:
751 aprint_error_dev(psc->sc_dev, "Failed to initialize an ALPS device.\n");
752 return res;
753 }
754
755 void
756 pms_alps_enable(void *opaque)
757 {
758 struct pms_softc *psc = opaque;
759 struct alps_softc *sc = &psc->u.alps;
760
761 sc->initializing = true;
762 }
763
764 void
765 pms_alps_resume(void *opaque)
766 {
767 struct pms_softc *psc = opaque;
768 struct alps_softc *sc = &psc->u.alps;
769 uint8_t cmd, resp[2];
770 int res;
771
772 cmd = PMS_RESET;
773 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, &cmd,
774 1, 2, resp, 1);
775 if (res)
776 aprint_error_dev(psc->sc_dev,
777 "ALPS reset on resume failed\n");
778 else {
779 if (sc->version == ALPS_PROTO_V7) {
780 (void)pms_alps_init_v7(psc);
781 } else if (sc->version == ALPS_PROTO_V2) {
782 (void)pms_alps_init_v2(psc);
783 } else {
784 /* Not supported */
785 }
786 pms_alps_enable(psc);
787 }
788 }
789
790 static void
791 pms_alps_decode_trackstick_packet_v7(struct pms_softc *psc)
792 {
793 int s;
794
795 int x, y, z;
796 int dx, dy, dz;
797 int left, middle, right;
798 u_int buttons;
799
800 x = (int8_t)((psc->packet[2] & 0xbf) |
801 ((psc->packet[3] & 0x10) << 2));
802 y = (int8_t)((psc->packet[3] & 0x07) | (psc->packet[4] & 0xb8) |
803 ((psc->packet[3] & 0x20) << 1));
804 z = (int8_t)((psc->packet[5] & 0x3f) |
805 ((psc->packet[3] & 0x80) >> 1));
806
807 dx = x * alps_trackstick_xy_precision;
808 dy = y * alps_trackstick_xy_precision;
809 dz = z * 1;
810
811 left = psc->packet[1] & 0x01;
812 middle = (psc->packet[1] & 0x04) >> 2;
813 right = (psc->packet[1] & 0x02) >> 1;
814 buttons = 0;
815 buttons = (u_int)((left << 0) | (middle << 1) | (right << 2));
816
817 s = spltty();
818 wsmouse_input(psc->sc_wsmousedev,
819 buttons,
820 dx, dy, dz, 0,
821 WSMOUSE_INPUT_DELTA);
822 splx(s);
823 }
824
825 static uint8_t
826 pms_alps_decode_packetid_v7(struct pms_softc *psc)
827 {
828 if (psc->packet[4] & 0x40)
829 return ALPS_V7_PACKETID_TWOFINGER;
830 else if (psc->packet[4] & 0x01)
831 return ALPS_V7_PACKETID_MULTIFINGER;
832 else if ((psc->packet[0] & 0x10) && !(psc->packet[4] & 0x43))
833 return ALPS_V7_PACKETID_NEWPACKET;
834 else if ((psc->packet[1] == 0x00) && (psc->packet[4] == 0x00))
835 return ALPS_V7_PACKETID_IDLE;
836 else
837 return ALPS_V7_PACKETID_UNKNOWN;
838 }
839
840 static void
841 pms_alps_decode_touchpad_packet_v7(struct pms_softc *psc)
842 {
843 int s;
844 struct alps_softc *sc = &psc->u.alps;
845 uint8_t packetid;
846
847 uint16_t cur_x1, cur_y1;
848 uint16_t cur_x2, cur_y2;
849 int dx1, dy1;
850 int button;
851 u_int buttons;
852
853 packetid = pms_alps_decode_packetid_v7(psc);
854 switch (packetid) {
855 case ALPS_V7_PACKETID_IDLE:
856 /* Accept meaningful packets only */
857 return;
858 case ALPS_V7_PACKETID_UNKNOWN:
859 /* Accept meaningful packets only */
860 return;
861 case ALPS_V7_PACKETID_NEWPACKET:
862 /* Sent new packet ID to reset status and not decoded */
863 sc->initializing = true;
864 return;
865 }
866
867 /* Decode a number of fingers and locations */
868 /* X0-11 ... X0-0 */
869 cur_x1 = (psc->packet[2] & 0x80) << 4; /* X0-11 */
870 cur_x1 |= (psc->packet[2] & 0x3f) << 5; /* X0-10 ... X0-5 */
871 cur_x1 |= (psc->packet[3] & 0x30) >> 1; /* X0-4, X0-3 */
872 cur_x1 |= psc->packet[3] & 0x07; /* X0-2 ... X0-0 */
873
874 /* Y0-10 ... Y0-0 */
875 cur_y1 = psc->packet[1] << 3; /* Y0-10 ... Y0-3 */
876 cur_y1 |= psc->packet[0] & 0x07; /* Y0-2 ... Y0-0 */
877
878 /* X1-11 ... X1-3 */
879 cur_x2 = (psc->packet[3] & 0x80) << 4; /* X1-11 */
880 cur_x2 |= (psc->packet[4] & 0x80) << 3; /* X1-10 */
881 cur_x2 |= (psc->packet[4] & 0x3f) << 4; /* X1-9 ... X1-4 */
882
883 /* Y1-10 ... Y1-4 */
884 cur_y2 = (psc->packet[5] & 0x80) << 3; /* Y1-10 */
885 cur_y2 |= (psc->packet[5] & 0x3f) << 4; /* Y1-9 .. Y1-4 */
886
887 switch (packetid) {
888 case ALPS_V7_PACKETID_TWOFINGER:
889 cur_x2 &= ~__BITS(3, 0); /* Clear undefined locations */
890 cur_y2 |= __BITS(3, 0); /* Fill undefined locations */
891 break;
892 case ALPS_V7_PACKETID_MULTIFINGER:
893 cur_x2 &= ~__BITS(5, 0); /* Clear undefined locations */
894 cur_y2 &= ~__BIT(5); /* Clear duplicate locations */
895 cur_y2 |= (psc->packet[4] & __BIT(1)) << 4; /* Set another */
896 cur_y2 |= __BITS(4, 0); /* Fill undefined locations */
897 break;
898 }
899
900 cur_y1 = 0x7ff - cur_y1;
901 cur_y2 = 0x7ff - cur_y2;
902
903 /* Handle finger touch reported in cur_x2/y2. only */
904 if (cur_x1 == 0 && cur_y1 == 0 && cur_x2 != 0 && cur_y2 != 0) {
905 cur_x1 = cur_x2;
906 cur_y1 = cur_y2;
907 cur_x2 = 0;
908 cur_y2 = 0;
909 }
910
911 switch (packetid) {
912 case ALPS_V7_PACKETID_TWOFINGER:
913 if ((cur_x2 == 0) && (cur_y2 == 0))
914 sc->nfingers = 1;
915 else
916 sc->nfingers = 2;
917 break;
918 case ALPS_V7_PACKETID_MULTIFINGER:
919 sc->nfingers = 3 + (psc->packet[5] & 0x03);
920 break;
921 }
922
923 button = (psc->packet[0] & 0x80) >> 7;
924 buttons = 0;
925 if (sc->nfingers == 1) {
926 if (button && (cur_y1 > 1700) && (cur_x1 < 1700))
927 buttons |= button << 0; /* Left button */
928 else if (button && (cur_y1 > 1700)
929 && (1700 <= cur_x1) && (cur_x1 <= 2700))
930 buttons |= button << 1; /* Middle button */
931 else if (button && (cur_y1 > 1700) && (2700 < cur_x1))
932 buttons |= button << 2; /* Right button */
933 } else if (sc->nfingers > 1) {
934 if (button && (cur_y2 > 1700) && (cur_x2 < 1700))
935 buttons |= button << 0; /* Left button */
936 else if (button && (cur_y2 > 1700)
937 && (1700 <= cur_x2) && (cur_x2 <= 2700))
938 buttons |= button << 1; /* Middle button */
939 else if (button && (cur_y2 > 1700) && (2700 < cur_x2))
940 buttons |= button << 2; /* Right button */
941 }
942
943 /* New touch */
944 if (sc->nfingers == 0 || sc->nfingers != sc->last_nfingers)
945 sc->initializing = true;
946
947 if (sc->initializing == true) {
948 dx1 = 0;
949 dy1 = 0;
950 } else {
951 dx1 = (int16_t)(cur_x1 - sc->last_x1);
952 dy1 = (int16_t)(sc->last_y1 - cur_y1);
953
954 dx1 = dx1 >> alps_touchpad_xy_unprecision;
955 dy1 = dy1 >> alps_touchpad_xy_unprecision;
956 }
957
958 /* Allow finger detouch during drag and drop */
959 if ((sc->nfingers < sc->last_nfingers)
960 && (cur_x2 == sc->last_x1) && (cur_y2 == sc->last_y1)) {
961 sc->last_x1 = sc->last_x2;
962 sc->last_y1 = sc->last_y2;
963 dx1 = 0;
964 dy1 = 0;
965 }
966
967 s = spltty();
968 wsmouse_input(psc->sc_wsmousedev,
969 buttons,
970 dx1, dy1, 0, 0,
971 WSMOUSE_INPUT_DELTA);
972 splx(s);
973
974 if (sc->initializing == true || (dx1 != 0))
975 sc->last_x1 = cur_x1;
976 if (sc->initializing == true || (dy1 != 0))
977 sc->last_y1 = cur_y1;
978
979 if (sc->nfingers > 0)
980 sc->initializing = false;
981 sc->last_nfingers = sc->nfingers;
982 }
983
984 static void
985 pms_alps_dispatch_packet_v7(struct pms_softc *psc)
986 {
987 if ((psc->packet[0] == 0x48) && ((psc->packet[4] & 0x47) == 0x06))
988 pms_alps_decode_trackstick_packet_v7(psc);
989 else
990 pms_alps_decode_touchpad_packet_v7(psc);
991 }
992
993 static void
994 pms_alps_decode_touchpad_packet_v2(struct pms_softc *psc)
995 {
996 int s;
997 struct alps_softc *sc = &psc->u.alps;
998 uint16_t cur_x, cur_y;
999 int16_t dx, dy;
1000 u_int left, middle, right;
1001 u_int forward, back;
1002 u_int buttons;
1003 uint8_t ges;
1004
1005 sc->nfingers = (psc->packet[2] & 0x02) >> 1;
1006 if (sc->last_nfingers == 0)
1007 sc->initializing = true;
1008
1009 left = psc->packet[3] & 0x01;
1010 right = (psc->packet[3] & 0x02) >> 1;
1011 middle = (psc->packet[3] & 0x04) >> 2;
1012
1013 cur_x = psc->packet[1];
1014 cur_x |= (psc->packet[2] & 0x78) << 4;
1015
1016 cur_y = psc->packet[4];
1017 cur_y |= (psc->packet[3] & 0x70) << 3;
1018
1019 #if 0
1020 cur_z = psc->packet[5];
1021 #endif
1022
1023 forward = (psc->packet[2] & 0x04) >> 2;
1024 back = (psc->packet[3] & 0x04) >> 2;
1025 ges = psc->packet[2] & 0x01;
1026
1027 buttons = (left | ges) << 0;
1028 buttons |= (middle | forward | back) << 1;
1029 buttons |= right << 2;
1030
1031 if (sc->initializing == true) {
1032 dx = 0;
1033 dy = 0;
1034 } else {
1035 dx = (cur_x - sc->last_x1);
1036 dy = (sc->last_y1 - cur_y);
1037
1038 dx = dx >> alps_touchpad_xy_unprecision;
1039 dy = dy >> alps_touchpad_xy_unprecision;
1040 }
1041
1042 s = spltty();
1043 wsmouse_input(psc->sc_wsmousedev,
1044 buttons,
1045 dx, dy, 0, 0,
1046 WSMOUSE_INPUT_DELTA);
1047 splx(s);
1048
1049 if (sc->initializing == true || (dx != 0))
1050 sc->last_x1 = cur_x;
1051 if (sc->initializing == true || (dy != 0))
1052 sc->last_y1 = cur_y;
1053
1054 if (sc->nfingers > 0)
1055 sc->initializing = false;
1056 sc->last_nfingers = sc->nfingers;
1057 }
1058
1059 static void
1060 pms_alps_input_v2(void *opaque, int data)
1061 {
1062 struct pms_softc *psc = opaque;
1063
1064 if (!psc->sc_enabled)
1065 return;
1066
1067 psc->packet[psc->inputstate++] = data & 0xff;
1068 if (psc->inputstate < 6)
1069 return;
1070
1071 pms_alps_decode_touchpad_packet_v2(psc);
1072
1073 psc->inputstate = 0;
1074 }
1075
1076 static void
1077 pms_alps_input_v7(void *opaque, int data)
1078 {
1079 struct pms_softc *psc = opaque;
1080
1081 if (!psc->sc_enabled)
1082 return;
1083
1084 psc->packet[psc->inputstate++] = data & 0xff;
1085 if (psc->inputstate < 6)
1086 return;
1087
1088 pms_alps_dispatch_packet_v7(psc);
1089
1090 psc->inputstate = 0;
1091 }
1092