1 /*
2 * Generic linux-input device driver for axis-bearing devices
3 *
4 * Copyright (c) 2001 Brian S. Julin
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL").
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 *
29 * References:
30 * HP-HIL Technical Reference Manual. Hewlett Packard Product No. 45918A
31 *
32 */
33
34 #include <linux/hil.h>
35 #include <linux/input.h>
36 #include <linux/serio.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41
42 #define PREFIX "HIL PTR: "
43 #define HIL_GENERIC_NAME "generic HIL pointer device"
44
45 MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
46 MODULE_DESCRIPTION(HIL_GENERIC_NAME " driver");
47 MODULE_LICENSE("Dual BSD/GPL");
48
49
50 #define TABLET_SIMULATES_MOUSE /* allow tablet to be used as mouse */
51 #undef TABLET_AUTOADJUST /* auto-adjust valid tablet ranges */
52
53
54 #define HIL_PTR_MAX_LENGTH 16
55
56 struct hil_ptr {
57 struct input_dev dev;
58 struct serio *serio;
59
60 /* Input buffer and index for packets from HIL bus. */
61 hil_packet data[HIL_PTR_MAX_LENGTH];
62 int idx4; /* four counts per packet */
63
64 /* Raw device info records from HIL bus, see hil.h for fields. */
65 char idd[HIL_PTR_MAX_LENGTH]; /* DID byte and IDD record */
66 char rsc[HIL_PTR_MAX_LENGTH]; /* RSC record */
67 char exd[HIL_PTR_MAX_LENGTH]; /* EXD record */
68 char rnm[HIL_PTR_MAX_LENGTH + 1]; /* RNM record + NULL term. */
69
70 /* Extra device details not contained in struct input_dev. */
71 unsigned int nbtn, naxes;
72 unsigned int btnmap[7];
73
74 /* Something to sleep around with. */
75 struct semaphore sem;
76 };
77
78 /* Process a complete packet after transfer from the HIL */
hil_ptr_process_record(struct hil_ptr * ptr)79 static void hil_ptr_process_record(struct hil_ptr *ptr)
80 {
81 struct input_dev *dev = &ptr->dev;
82 hil_packet *data = ptr->data;
83 hil_packet p;
84 int idx, i, cnt, laxis;
85 int ax16, absdev;
86
87 idx = ptr->idx4/4;
88 p = data[idx - 1];
89
90 if ((p & ~HIL_CMDCT_POL) ==
91 (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL)) goto report;
92 if ((p & ~HIL_CMDCT_RPL) ==
93 (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_RPL)) goto report;
94
95 /* Not a poll response. See if we are loading config records. */
96 switch (p & HIL_PKT_DATA_MASK) {
97 case HIL_CMD_IDD:
98 for (i = 0; i < idx; i++)
99 ptr->idd[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
100 for (; i < HIL_PTR_MAX_LENGTH; i++)
101 ptr->idd[i] = 0;
102 break;
103 case HIL_CMD_RSC:
104 for (i = 0; i < idx; i++)
105 ptr->rsc[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
106 for (; i < HIL_PTR_MAX_LENGTH; i++)
107 ptr->rsc[i] = 0;
108 break;
109 case HIL_CMD_EXD:
110 for (i = 0; i < idx; i++)
111 ptr->exd[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
112 for (; i < HIL_PTR_MAX_LENGTH; i++)
113 ptr->exd[i] = 0;
114 break;
115 case HIL_CMD_RNM:
116 for (i = 0; i < idx; i++)
117 ptr->rnm[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
118 for (; i < HIL_PTR_MAX_LENGTH + 1; i++)
119 ptr->rnm[i] = '\0';
120 break;
121 default:
122 /* These occur when device isn't present */
123 if (p == (HIL_ERR_INT | HIL_PKT_CMD)) break;
124 /* Anything else we'd like to know about. */
125 printk(KERN_WARNING PREFIX "Device sent unknown record %x\n", p);
126 break;
127 }
128 goto out;
129
130 report:
131 if ((p & HIL_CMDCT_POL) != idx - 1) {
132 printk(KERN_WARNING PREFIX "Malformed poll packet %x (idx = %i)\n", p, idx);
133 goto out;
134 }
135
136 i = (ptr->data[0] & HIL_POL_AXIS_ALT) ? 3 : 0;
137 laxis = ptr->data[0] & HIL_POL_NUM_AXES_MASK;
138 laxis += i;
139
140 ax16 = ptr->idd[1] & HIL_IDD_HEADER_16BIT; /* 8 or 16bit resolution */
141 absdev = ptr->idd[1] & HIL_IDD_HEADER_ABS;
142
143 for (cnt = 1; i < laxis; i++) {
144 unsigned int lo,hi,val;
145 lo = ptr->data[cnt++] & HIL_PKT_DATA_MASK;
146 hi = ax16 ? (ptr->data[cnt++] & HIL_PKT_DATA_MASK) : 0;
147 if (absdev) {
148 val = lo + (hi<<8);
149 #ifdef TABLET_AUTOADJUST
150 if (val < ptr->dev.absmin[ABS_X + i])
151 ptr->dev.absmin[ABS_X + i] = val;
152 if (val > ptr->dev.absmax[ABS_X + i])
153 ptr->dev.absmax[ABS_X + i] = val;
154 #endif
155 if (i%3) val = ptr->dev.absmax[ABS_X + i] - val;
156 input_report_abs(dev, ABS_X + i, val);
157 } else {
158 val = (int) (((int8_t)lo) | ((int8_t)hi<<8));
159 if (i%3) val *= -1;
160 input_report_rel(dev, REL_X + i, val);
161 }
162 }
163
164 while (cnt < idx - 1) {
165 unsigned int btn;
166 int up;
167 btn = ptr->data[cnt++];
168 up = btn & 1;
169 btn &= 0xfe;
170 if (btn == 0x8e) {
171 continue; /* TODO: proximity == touch? */
172 }
173 else if ((btn > 0x8c) || (btn < 0x80)) continue;
174 btn = (btn - 0x80) >> 1;
175 btn = ptr->btnmap[btn];
176 input_report_key(dev, btn, !up);
177 }
178 out:
179 ptr->idx4 = 0;
180 up(&ptr->sem);
181 }
182
hil_ptr_process_err(struct hil_ptr * ptr)183 static void hil_ptr_process_err(struct hil_ptr *ptr) {
184 printk(KERN_WARNING PREFIX "errored HIL packet\n");
185 ptr->idx4 = 0;
186 up(&ptr->sem);
187 return;
188 }
189
hil_ptr_interrupt(struct serio * serio,unsigned char data,unsigned int flags)190 static void hil_ptr_interrupt(struct serio *serio,
191 unsigned char data,
192 unsigned int flags)
193 {
194 struct hil_ptr *ptr;
195 hil_packet packet;
196 int idx;
197
198 ptr = (struct hil_ptr *)serio->private;
199 if (ptr == NULL) {
200 BUG();
201 return;
202 }
203
204 if (ptr->idx4 >= (HIL_PTR_MAX_LENGTH * sizeof(hil_packet))) {
205 hil_ptr_process_err(ptr);
206 return;
207 }
208 idx = ptr->idx4/4;
209 if (!(ptr->idx4 % 4)) ptr->data[idx] = 0;
210 packet = ptr->data[idx];
211 packet |= ((hil_packet)data) << ((3 - (ptr->idx4 % 4)) * 8);
212 ptr->data[idx] = packet;
213
214 /* Records of N 4-byte hil_packets must terminate with a command. */
215 if ((++(ptr->idx4)) % 4) return;
216 if ((packet & 0xffff0000) != HIL_ERR_INT) {
217 hil_ptr_process_err(ptr);
218 return;
219 }
220 if (packet & HIL_PKT_CMD)
221 hil_ptr_process_record(ptr);
222 }
223
hil_ptr_disconnect(struct serio * serio)224 static void hil_ptr_disconnect(struct serio *serio)
225 {
226 struct hil_ptr *ptr;
227
228 ptr = (struct hil_ptr *)serio->private;
229 if (ptr == NULL) {
230 BUG();
231 return;
232 }
233
234 input_unregister_device(&ptr->dev);
235 serio_close(serio);
236 kfree(ptr);
237 }
238
hil_ptr_connect(struct serio * serio,struct serio_dev * dev)239 static void hil_ptr_connect(struct serio *serio, struct serio_dev *dev)
240 {
241 struct hil_ptr *ptr;
242 char *txt;
243 unsigned int i, naxsets, btntype;
244 uint8_t did, *idd;
245
246 if (serio->type != (SERIO_HIL_MLC | SERIO_HIL)) return;
247
248 if (!(ptr = kmalloc(sizeof(struct hil_ptr), GFP_KERNEL))) return;
249 memset(ptr, 0, sizeof(struct hil_ptr));
250
251 if (serio_open(serio, dev)) goto bail0;
252
253 serio->private = ptr;
254 ptr->serio = serio;
255 ptr->dev.private = ptr;
256
257 init_MUTEX_LOCKED(&(ptr->sem));
258
259 /* Get device info. MLC driver supplies devid/status/etc. */
260 serio->write(serio, 0);
261 serio->write(serio, 0);
262 serio->write(serio, HIL_PKT_CMD >> 8);
263 serio->write(serio, HIL_CMD_IDD);
264 down(&(ptr->sem));
265
266 serio->write(serio, 0);
267 serio->write(serio, 0);
268 serio->write(serio, HIL_PKT_CMD >> 8);
269 serio->write(serio, HIL_CMD_RSC);
270 down(&(ptr->sem));
271
272 serio->write(serio, 0);
273 serio->write(serio, 0);
274 serio->write(serio, HIL_PKT_CMD >> 8);
275 serio->write(serio, HIL_CMD_RNM);
276 down(&(ptr->sem));
277
278 serio->write(serio, 0);
279 serio->write(serio, 0);
280 serio->write(serio, HIL_PKT_CMD >> 8);
281 serio->write(serio, HIL_CMD_EXD);
282 down(&(ptr->sem));
283
284 up(&(ptr->sem));
285
286 did = ptr->idd[0];
287 idd = ptr->idd + 1;
288 txt = "unknown";
289 if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_REL) {
290 ptr->dev.evbit[0] = BIT(EV_REL);
291 txt = "relative";
292 }
293
294 if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_ABS) {
295 ptr->dev.evbit[0] = BIT(EV_ABS);
296 txt = "absolute";
297 }
298 if (!ptr->dev.evbit[0]) {
299 goto bail1;
300 }
301
302 ptr->nbtn = HIL_IDD_NUM_BUTTONS(idd);
303 if (ptr->nbtn) ptr->dev.evbit[0] |= BIT(EV_KEY);
304
305 naxsets = HIL_IDD_NUM_AXSETS(*idd);
306 ptr->naxes = HIL_IDD_NUM_AXES_PER_SET(*idd);
307
308 printk(KERN_INFO PREFIX "HIL pointer device found (did: 0x%02x, axis: %s)\n",
309 did, txt);
310 printk(KERN_INFO PREFIX "HIL pointer has %i buttons and %i sets of %i axes\n",
311 ptr->nbtn, naxsets, ptr->naxes);
312
313 btntype = BTN_MISC;
314 if ((did & HIL_IDD_DID_ABS_TABLET_MASK) == HIL_IDD_DID_ABS_TABLET)
315 #ifdef TABLET_SIMULATES_MOUSE
316 btntype = BTN_TOUCH;
317 #else
318 btntype = BTN_DIGI;
319 #endif
320 if ((did & HIL_IDD_DID_ABS_TSCREEN_MASK) == HIL_IDD_DID_ABS_TSCREEN)
321 btntype = BTN_TOUCH;
322
323 if ((did & HIL_IDD_DID_REL_MOUSE_MASK) == HIL_IDD_DID_REL_MOUSE)
324 btntype = BTN_MOUSE;
325
326 for (i = 0; i < ptr->nbtn; i++) {
327 set_bit(btntype | i, ptr->dev.keybit);
328 ptr->btnmap[i] = btntype | i;
329 }
330
331 if (btntype == BTN_MOUSE) {
332 /* Swap buttons 2 and 3 */
333 ptr->btnmap[1] = BTN_MIDDLE;
334 ptr->btnmap[2] = BTN_RIGHT;
335 }
336
337 if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_REL) {
338 for (i = 0; i < ptr->naxes; i++) {
339 set_bit(REL_X + i, ptr->dev.relbit);
340 }
341 for (i = 3; (i < ptr->naxes + 3) && (naxsets > 1); i++) {
342 set_bit(REL_X + i, ptr->dev.relbit);
343 }
344 } else {
345 for (i = 0; i < ptr->naxes; i++) {
346 set_bit(ABS_X + i, ptr->dev.absbit);
347 ptr->dev.absmin[ABS_X + i] = 0;
348 ptr->dev.absmax[ABS_X + i] =
349 HIL_IDD_AXIS_MAX((ptr->idd + 1), i);
350 }
351 for (i = 3; (i < ptr->naxes + 3) && (naxsets > 1); i++) {
352 set_bit(ABS_X + i, ptr->dev.absbit);
353 ptr->dev.absmin[ABS_X + i] = 0;
354 ptr->dev.absmax[ABS_X + i] =
355 HIL_IDD_AXIS_MAX((ptr->idd + 1), (i - 3));
356 }
357 #ifdef TABLET_AUTOADJUST
358 for (i = 0; i < ABS_MAX; i++) {
359 int diff = ptr->dev.absmax[ABS_X + i] / 10;
360 ptr->dev.absmin[ABS_X + i] += diff;
361 ptr->dev.absmax[ABS_X + i] -= diff;
362 }
363 #endif
364 }
365
366 ptr->dev.name = strlen(ptr->rnm) ? ptr->rnm : HIL_GENERIC_NAME;
367
368 ptr->dev.idbus = BUS_HIL;
369 ptr->dev.idvendor = SERIO_HIL;
370 ptr->dev.idproduct = 0x0001; /* TODO: get from ptr->rsc */
371 ptr->dev.idversion = 0x0100; /* TODO: get from ptr->rsc */
372
373 input_register_device(&ptr->dev);
374 printk(KERN_INFO "input%d: %s on hil%d\n",
375 ptr->dev.number,
376 (btntype == BTN_MOUSE) ? "HIL mouse":"HIL tablet or touchpad",
377 0);
378
379 return;
380 bail1:
381 serio_close(serio);
382 bail0:
383 kfree(ptr);
384 return;
385 }
386
387
388 struct serio_dev hil_ptr_serio_dev = {
389 .connect = hil_ptr_connect,
390 .disconnect = hil_ptr_disconnect,
391 .interrupt = hil_ptr_interrupt
392 };
393
hil_ptr_init(void)394 static int __init hil_ptr_init(void)
395 {
396 serio_register_device(&hil_ptr_serio_dev);
397 return 0;
398 }
399
hil_ptr_exit(void)400 static void __exit hil_ptr_exit(void)
401 {
402 serio_unregister_device(&hil_ptr_serio_dev);
403 }
404
405 module_init(hil_ptr_init);
406 module_exit(hil_ptr_exit);
407