blob: 69f02178c528c87178a3ae188181ea3c2ae581d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#include <linux/pci_ids.h>
42
43#define PREFIX "HIL PTR: "
44#define HIL_GENERIC_NAME "HIL pointer device"
45
46MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
47MODULE_DESCRIPTION(HIL_GENERIC_NAME " driver");
48MODULE_LICENSE("Dual BSD/GPL");
49
50
51#define TABLET_SIMULATES_MOUSE /* allow tablet to be used as mouse */
52#undef TABLET_AUTOADJUST /* auto-adjust valid tablet ranges */
53
54
55#define HIL_PTR_MAX_LENGTH 16
56
57struct hil_ptr {
Helge Deller102c8c72006-03-26 07:41:55 -070058 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct serio *serio;
60
61 /* Input buffer and index for packets from HIL bus. */
62 hil_packet data[HIL_PTR_MAX_LENGTH];
63 int idx4; /* four counts per packet */
64
65 /* Raw device info records from HIL bus, see hil.h for fields. */
66 char idd[HIL_PTR_MAX_LENGTH]; /* DID byte and IDD record */
67 char rsc[HIL_PTR_MAX_LENGTH]; /* RSC record */
68 char exd[HIL_PTR_MAX_LENGTH]; /* EXD record */
69 char rnm[HIL_PTR_MAX_LENGTH + 1]; /* RNM record + NULL term. */
70
71 /* Extra device details not contained in struct input_dev. */
72 unsigned int nbtn, naxes;
73 unsigned int btnmap[7];
74
75 /* Something to sleep around with. */
76 struct semaphore sem;
77};
78
79/* Process a complete packet after transfer from the HIL */
80static void hil_ptr_process_record(struct hil_ptr *ptr)
81{
Helge Deller102c8c72006-03-26 07:41:55 -070082 struct input_dev *dev = ptr->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 hil_packet *data = ptr->data;
84 hil_packet p;
85 int idx, i, cnt, laxis;
86 int ax16, absdev;
87
88 idx = ptr->idx4/4;
89 p = data[idx - 1];
90
91 if ((p & ~HIL_CMDCT_POL) ==
92 (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL)) goto report;
93 if ((p & ~HIL_CMDCT_RPL) ==
94 (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_RPL)) goto report;
95
96 /* Not a poll response. See if we are loading config records. */
97 switch (p & HIL_PKT_DATA_MASK) {
98 case HIL_CMD_IDD:
99 for (i = 0; i < idx; i++)
100 ptr->idd[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
101 for (; i < HIL_PTR_MAX_LENGTH; i++)
102 ptr->idd[i] = 0;
103 break;
104 case HIL_CMD_RSC:
105 for (i = 0; i < idx; i++)
106 ptr->rsc[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
107 for (; i < HIL_PTR_MAX_LENGTH; i++)
108 ptr->rsc[i] = 0;
109 break;
110 case HIL_CMD_EXD:
111 for (i = 0; i < idx; i++)
112 ptr->exd[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
113 for (; i < HIL_PTR_MAX_LENGTH; i++)
114 ptr->exd[i] = 0;
115 break;
116 case HIL_CMD_RNM:
117 for (i = 0; i < idx; i++)
118 ptr->rnm[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
119 for (; i < HIL_PTR_MAX_LENGTH + 1; i++)
120 ptr->rnm[i] = '\0';
121 break;
122 default:
123 /* These occur when device isn't present */
124 if (p == (HIL_ERR_INT | HIL_PKT_CMD)) break;
125 /* Anything else we'd like to know about. */
126 printk(KERN_WARNING PREFIX "Device sent unknown record %x\n", p);
127 break;
128 }
129 goto out;
130
131 report:
132 if ((p & HIL_CMDCT_POL) != idx - 1) {
133 printk(KERN_WARNING PREFIX "Malformed poll packet %x (idx = %i)\n", p, idx);
134 goto out;
135 }
136
137 i = (ptr->data[0] & HIL_POL_AXIS_ALT) ? 3 : 0;
138 laxis = ptr->data[0] & HIL_POL_NUM_AXES_MASK;
139 laxis += i;
140
141 ax16 = ptr->idd[1] & HIL_IDD_HEADER_16BIT; /* 8 or 16bit resolution */
142 absdev = ptr->idd[1] & HIL_IDD_HEADER_ABS;
143
144 for (cnt = 1; i < laxis; i++) {
145 unsigned int lo,hi,val;
146 lo = ptr->data[cnt++] & HIL_PKT_DATA_MASK;
147 hi = ax16 ? (ptr->data[cnt++] & HIL_PKT_DATA_MASK) : 0;
148 if (absdev) {
149 val = lo + (hi<<8);
150#ifdef TABLET_AUTOADJUST
Helge Deller102c8c72006-03-26 07:41:55 -0700151 if (val < dev->absmin[ABS_X + i])
152 dev->absmin[ABS_X + i] = val;
153 if (val > dev->absmax[ABS_X + i])
154 dev->absmax[ABS_X + i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#endif
Helge Deller102c8c72006-03-26 07:41:55 -0700156 if (i%3) val = dev->absmax[ABS_X + i] - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 input_report_abs(dev, ABS_X + i, val);
158 } else {
159 val = (int) (((int8_t)lo) | ((int8_t)hi<<8));
160 if (i%3) val *= -1;
161 input_report_rel(dev, REL_X + i, val);
162 }
163 }
164
165 while (cnt < idx - 1) {
166 unsigned int btn;
167 int up;
168 btn = ptr->data[cnt++];
169 up = btn & 1;
170 btn &= 0xfe;
171 if (btn == 0x8e) {
172 continue; /* TODO: proximity == touch? */
173 }
174 else if ((btn > 0x8c) || (btn < 0x80)) continue;
175 btn = (btn - 0x80) >> 1;
176 btn = ptr->btnmap[btn];
177 input_report_key(dev, btn, !up);
178 }
179 input_sync(dev);
180 out:
181 ptr->idx4 = 0;
182 up(&ptr->sem);
183}
184
185static void hil_ptr_process_err(struct hil_ptr *ptr) {
186 printk(KERN_WARNING PREFIX "errored HIL packet\n");
187 ptr->idx4 = 0;
188 up(&ptr->sem);
189 return;
190}
191
192static irqreturn_t hil_ptr_interrupt(struct serio *serio,
193 unsigned char data, unsigned int flags, struct pt_regs *regs)
194{
195 struct hil_ptr *ptr;
196 hil_packet packet;
197 int idx;
198
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400199 ptr = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (ptr == NULL) {
201 BUG();
202 return IRQ_HANDLED;
203 }
204
205 if (ptr->idx4 >= (HIL_PTR_MAX_LENGTH * sizeof(hil_packet))) {
206 hil_ptr_process_err(ptr);
207 return IRQ_HANDLED;
208 }
209 idx = ptr->idx4/4;
210 if (!(ptr->idx4 % 4)) ptr->data[idx] = 0;
211 packet = ptr->data[idx];
212 packet |= ((hil_packet)data) << ((3 - (ptr->idx4 % 4)) * 8);
213 ptr->data[idx] = packet;
214
215 /* Records of N 4-byte hil_packets must terminate with a command. */
216 if ((++(ptr->idx4)) % 4) return IRQ_HANDLED;
217 if ((packet & 0xffff0000) != HIL_ERR_INT) {
218 hil_ptr_process_err(ptr);
219 return IRQ_HANDLED;
220 }
221 if (packet & HIL_PKT_CMD)
222 hil_ptr_process_record(ptr);
223 return IRQ_HANDLED;
224}
225
226static void hil_ptr_disconnect(struct serio *serio)
227{
228 struct hil_ptr *ptr;
229
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400230 ptr = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (ptr == NULL) {
232 BUG();
233 return;
234 }
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 serio_close(serio);
Kyle McMartincd7a9202006-03-30 11:47:32 -0500237 input_unregister_device(ptr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 kfree(ptr);
239}
240
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400241static int hil_ptr_connect(struct serio *serio, struct serio_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Helge Deller102c8c72006-03-26 07:41:55 -0700243 struct hil_ptr *ptr;
244 char *txt;
245 unsigned int i, naxsets, btntype;
246 uint8_t did, *idd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500248 if (!(ptr = kzalloc(sizeof(struct hil_ptr), GFP_KERNEL)))
249 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Helge Deller102c8c72006-03-26 07:41:55 -0700251 ptr->dev = input_allocate_device();
Dmitry Torokhov95d465f2006-04-02 00:08:05 -0500252 if (!ptr->dev)
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500253 goto bail0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Helge Deller102c8c72006-03-26 07:41:55 -0700255 ptr->dev->private = ptr;
256
Dmitry Torokhov95d465f2006-04-02 00:08:05 -0500257 if (serio_open(serio, driver))
258 goto bail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400260 serio_set_drvdata(serio, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ptr->serio = serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 init_MUTEX_LOCKED(&(ptr->sem));
264
265 /* Get device info. MLC driver supplies devid/status/etc. */
266 serio->write(serio, 0);
267 serio->write(serio, 0);
268 serio->write(serio, HIL_PKT_CMD >> 8);
269 serio->write(serio, HIL_CMD_IDD);
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_RSC);
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_RNM);
282 down(&(ptr->sem));
283
284 serio->write(serio, 0);
285 serio->write(serio, 0);
286 serio->write(serio, HIL_PKT_CMD >> 8);
287 serio->write(serio, HIL_CMD_EXD);
288 down(&(ptr->sem));
289
290 up(&(ptr->sem));
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 did = ptr->idd[0];
293 idd = ptr->idd + 1;
294 txt = "unknown";
295 if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_REL) {
Helge Deller102c8c72006-03-26 07:41:55 -0700296 ptr->dev->evbit[0] = BIT(EV_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 txt = "relative";
298 }
299
300 if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_ABS) {
Helge Deller102c8c72006-03-26 07:41:55 -0700301 ptr->dev->evbit[0] = BIT(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 txt = "absolute";
303 }
Helge Deller102c8c72006-03-26 07:41:55 -0700304 if (!ptr->dev->evbit[0]) {
305 goto bail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308 ptr->nbtn = HIL_IDD_NUM_BUTTONS(idd);
Helge Deller102c8c72006-03-26 07:41:55 -0700309 if (ptr->nbtn) ptr->dev->evbit[0] |= BIT(EV_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 naxsets = HIL_IDD_NUM_AXSETS(*idd);
312 ptr->naxes = HIL_IDD_NUM_AXES_PER_SET(*idd);
313
314 printk(KERN_INFO PREFIX "HIL pointer device found (did: 0x%02x, axis: %s)\n",
315 did, txt);
316 printk(KERN_INFO PREFIX "HIL pointer has %i buttons and %i sets of %i axes\n",
317 ptr->nbtn, naxsets, ptr->naxes);
318
319 btntype = BTN_MISC;
320 if ((did & HIL_IDD_DID_ABS_TABLET_MASK) == HIL_IDD_DID_ABS_TABLET)
321#ifdef TABLET_SIMULATES_MOUSE
322 btntype = BTN_TOUCH;
323#else
324 btntype = BTN_DIGI;
325#endif
326 if ((did & HIL_IDD_DID_ABS_TSCREEN_MASK) == HIL_IDD_DID_ABS_TSCREEN)
327 btntype = BTN_TOUCH;
328
329 if ((did & HIL_IDD_DID_REL_MOUSE_MASK) == HIL_IDD_DID_REL_MOUSE)
330 btntype = BTN_MOUSE;
331
332 for (i = 0; i < ptr->nbtn; i++) {
Helge Deller102c8c72006-03-26 07:41:55 -0700333 set_bit(btntype | i, ptr->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ptr->btnmap[i] = btntype | i;
335 }
336
337 if (btntype == BTN_MOUSE) {
338 /* Swap buttons 2 and 3 */
339 ptr->btnmap[1] = BTN_MIDDLE;
340 ptr->btnmap[2] = BTN_RIGHT;
341 }
342
343 if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_REL) {
344 for (i = 0; i < ptr->naxes; i++) {
Helge Deller102c8c72006-03-26 07:41:55 -0700345 set_bit(REL_X + i, ptr->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 for (i = 3; (i < ptr->naxes + 3) && (naxsets > 1); i++) {
Helge Deller102c8c72006-03-26 07:41:55 -0700348 set_bit(REL_X + i, ptr->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 } else {
351 for (i = 0; i < ptr->naxes; i++) {
Helge Deller102c8c72006-03-26 07:41:55 -0700352 set_bit(ABS_X + i, ptr->dev->absbit);
353 ptr->dev->absmin[ABS_X + i] = 0;
354 ptr->dev->absmax[ABS_X + i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 HIL_IDD_AXIS_MAX((ptr->idd + 1), i);
356 }
357 for (i = 3; (i < ptr->naxes + 3) && (naxsets > 1); i++) {
Helge Deller102c8c72006-03-26 07:41:55 -0700358 set_bit(ABS_X + i, ptr->dev->absbit);
359 ptr->dev->absmin[ABS_X + i] = 0;
360 ptr->dev->absmax[ABS_X + i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 HIL_IDD_AXIS_MAX((ptr->idd + 1), (i - 3));
362 }
363#ifdef TABLET_AUTOADJUST
364 for (i = 0; i < ABS_MAX; i++) {
Helge Deller102c8c72006-03-26 07:41:55 -0700365 int diff = ptr->dev->absmax[ABS_X + i] / 10;
366 ptr->dev->absmin[ABS_X + i] += diff;
367 ptr->dev->absmax[ABS_X + i] -= diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369#endif
370 }
371
Helge Deller102c8c72006-03-26 07:41:55 -0700372 ptr->dev->name = strlen(ptr->rnm) ? ptr->rnm : HIL_GENERIC_NAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Helge Deller102c8c72006-03-26 07:41:55 -0700374 ptr->dev->id.bustype = BUS_HIL;
375 ptr->dev->id.vendor = PCI_VENDOR_ID_HP;
376 ptr->dev->id.product = 0x0001; /* TODO: get from ptr->rsc */
377 ptr->dev->id.version = 0x0100; /* TODO: get from ptr->rsc */
378 ptr->dev->dev = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Helge Deller102c8c72006-03-26 07:41:55 -0700380 input_register_device(ptr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 printk(KERN_INFO "input: %s (%s), ID: %d\n",
Helge Deller102c8c72006-03-26 07:41:55 -0700382 ptr->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 (btntype == BTN_MOUSE) ? "HIL mouse":"HIL tablet or touchpad",
384 did);
385
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400386 return 0;
Helge Deller102c8c72006-03-26 07:41:55 -0700387 bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 serio_close(serio);
Helge Deller102c8c72006-03-26 07:41:55 -0700389 bail1:
390 input_free_device(ptr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 bail0:
392 kfree(ptr);
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400393 serio_set_drvdata(serio, NULL);
394 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400397static struct serio_device_id hil_ptr_ids[] = {
398 {
399 .type = SERIO_HIL_MLC,
400 .proto = SERIO_HIL,
401 .id = SERIO_ANY,
402 .extra = SERIO_ANY,
403 },
404 { 0 }
405};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407static struct serio_driver hil_ptr_serio_driver = {
408 .driver = {
409 .name = "hil_ptr",
410 },
411 .description = "HP HIL mouse/tablet driver",
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400412 .id_table = hil_ptr_ids,
413 .connect = hil_ptr_connect,
414 .disconnect = hil_ptr_disconnect,
415 .interrupt = hil_ptr_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416};
417
418static int __init hil_ptr_init(void)
419{
420 serio_register_driver(&hil_ptr_serio_driver);
421 return 0;
422}
423
424static void __exit hil_ptr_exit(void)
425{
426 serio_unregister_driver(&hil_ptr_serio_driver);
427}
428
429module_init(hil_ptr_init);
430module_exit(hil_ptr_exit);