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