]> Pileus Git - ~andy/linux/blob - drivers/input/mouse/hil_ptr.c
Input: HIL - cleanup coding style
[~andy/linux] / drivers / input / mouse / hil_ptr.c
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 #include <linux/pci_ids.h>
42
43 #define PREFIX "HIL PTR: "
44 #define HIL_GENERIC_NAME "HIL pointer device"
45
46 MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
47 MODULE_DESCRIPTION(HIL_GENERIC_NAME " driver");
48 MODULE_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
57 struct hil_ptr {
58         struct input_dev *dev;
59         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 */
80 static void hil_ptr_process_record(struct hil_ptr *ptr)
81 {
82         struct input_dev *dev = ptr->dev;
83         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))
93                 goto report;
94         if ((p & ~HIL_CMDCT_RPL) ==
95             (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_RPL))
96                 goto report;
97
98         /* Not a poll response.  See if we are loading config records. */
99         switch (p & HIL_PKT_DATA_MASK) {
100         case HIL_CMD_IDD:
101                 for (i = 0; i < idx; i++)
102                         ptr->idd[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
103                 for (; i < HIL_PTR_MAX_LENGTH; i++)
104                         ptr->idd[i] = 0;
105                 break;
106
107         case HIL_CMD_RSC:
108                 for (i = 0; i < idx; i++)
109                         ptr->rsc[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
110                 for (; i < HIL_PTR_MAX_LENGTH; i++)
111                         ptr->rsc[i] = 0;
112                 break;
113
114         case HIL_CMD_EXD:
115                 for (i = 0; i < idx; i++)
116                         ptr->exd[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
117                 for (; i < HIL_PTR_MAX_LENGTH; i++)
118                         ptr->exd[i] = 0;
119                 break;
120
121         case HIL_CMD_RNM:
122                 for (i = 0; i < idx; i++)
123                         ptr->rnm[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
124                 for (; i < HIL_PTR_MAX_LENGTH + 1; i++)
125                         ptr->rnm[i] = 0;
126                 break;
127
128         default:
129                 /* These occur when device isn't present */
130                 if (p == (HIL_ERR_INT | HIL_PKT_CMD))
131                         break;
132                 /* Anything else we'd like to know about. */
133                 printk(KERN_WARNING PREFIX "Device sent unknown record %x\n", p);
134                 break;
135         }
136         goto out;
137
138  report:
139         if ((p & HIL_CMDCT_POL) != idx - 1) {
140                 printk(KERN_WARNING PREFIX
141                         "Malformed poll packet %x (idx = %i)\n", p, idx);
142                 goto out;
143         }
144
145         i = (ptr->data[0] & HIL_POL_AXIS_ALT) ? 3 : 0;
146         laxis = ptr->data[0] & HIL_POL_NUM_AXES_MASK;
147         laxis += i;
148
149         ax16 = ptr->idd[1] & HIL_IDD_HEADER_16BIT; /* 8 or 16bit resolution */
150         absdev = ptr->idd[1] & HIL_IDD_HEADER_ABS;
151
152         for (cnt = 1; i < laxis; i++) {
153                 unsigned int lo,hi,val;
154                 lo = ptr->data[cnt++] & HIL_PKT_DATA_MASK;
155                 hi = ax16 ? (ptr->data[cnt++] & HIL_PKT_DATA_MASK) : 0;
156                 if (absdev) {
157                         val = lo + (hi<<8);
158 #ifdef TABLET_AUTOADJUST
159                         if (val < dev->absmin[ABS_X + i])
160                                 dev->absmin[ABS_X + i] = val;
161                         if (val > dev->absmax[ABS_X + i])
162                                 dev->absmax[ABS_X + i] = val;
163 #endif
164                         if (i%3) val = dev->absmax[ABS_X + i] - val;
165                         input_report_abs(dev, ABS_X + i, val);
166                 } else {
167                         val = (int) (((int8_t)lo) | ((int8_t)hi<<8));
168                         if (i%3)
169                                 val *= -1;
170                         input_report_rel(dev, REL_X + i, val);
171                 }
172         }
173
174         while (cnt < idx - 1) {
175                 unsigned int btn;
176                 int up;
177                 btn = ptr->data[cnt++];
178                 up = btn & 1;
179                 btn &= 0xfe;
180                 if (btn == 0x8e)
181                         continue; /* TODO: proximity == touch? */
182                 else
183                         if ((btn > 0x8c) || (btn < 0x80))
184                                 continue;
185                 btn = (btn - 0x80) >> 1;
186                 btn = ptr->btnmap[btn];
187                 input_report_key(dev, btn, !up);
188         }
189         input_sync(dev);
190  out:
191         ptr->idx4 = 0;
192         up(&ptr->sem);
193 }
194
195 static void hil_ptr_process_err(struct hil_ptr *ptr)
196 {
197         printk(KERN_WARNING PREFIX "errored HIL packet\n");
198         ptr->idx4 = 0;
199         up(&ptr->sem);
200 }
201
202 static irqreturn_t hil_ptr_interrupt(struct serio *serio,
203         unsigned char data, unsigned int flags)
204 {
205         struct hil_ptr *ptr;
206         hil_packet packet;
207         int idx;
208
209         ptr = serio_get_drvdata(serio);
210         BUG_ON(ptr == NULL);
211
212         if (ptr->idx4 >= (HIL_PTR_MAX_LENGTH * sizeof(hil_packet))) {
213                 hil_ptr_process_err(ptr);
214                 return IRQ_HANDLED;
215         }
216         idx = ptr->idx4/4;
217         if (!(ptr->idx4 % 4))
218                 ptr->data[idx] = 0;
219         packet = ptr->data[idx];
220         packet |= ((hil_packet)data) << ((3 - (ptr->idx4 % 4)) * 8);
221         ptr->data[idx] = packet;
222
223         /* Records of N 4-byte hil_packets must terminate with a command. */
224         if ((++(ptr->idx4)) % 4)
225                 return IRQ_HANDLED;
226         if ((packet & 0xffff0000) != HIL_ERR_INT) {
227                 hil_ptr_process_err(ptr);
228                 return IRQ_HANDLED;
229         }
230         if (packet & HIL_PKT_CMD)
231                 hil_ptr_process_record(ptr);
232
233         return IRQ_HANDLED;
234 }
235
236 static void hil_ptr_disconnect(struct serio *serio)
237 {
238         struct hil_ptr *ptr;
239
240         ptr = serio_get_drvdata(serio);
241         BUG_ON(ptr == NULL);
242
243         serio_close(serio);
244         input_unregister_device(ptr->dev);
245         kfree(ptr);
246 }
247
248 static int hil_ptr_connect(struct serio *serio, struct serio_driver *driver)
249 {
250         struct hil_ptr   *ptr;
251         const char       *txt;
252         unsigned int     i, naxsets, btntype;
253         uint8_t          did, *idd;
254
255         if (!(ptr = kzalloc(sizeof(struct hil_ptr), GFP_KERNEL)))
256                 return -ENOMEM;
257
258         ptr->dev = input_allocate_device();
259         if (!ptr->dev)
260                 goto bail0;
261
262         ptr->dev->private = ptr;
263
264         if (serio_open(serio, driver))
265                 goto bail1;
266
267         serio_set_drvdata(serio, ptr);
268         ptr->serio = serio;
269
270         init_MUTEX_LOCKED(&ptr->sem);
271
272         /* Get device info.  MLC driver supplies devid/status/etc. */
273         serio->write(serio, 0);
274         serio->write(serio, 0);
275         serio->write(serio, HIL_PKT_CMD >> 8);
276         serio->write(serio, HIL_CMD_IDD);
277         down(&ptr->sem);
278
279         serio->write(serio, 0);
280         serio->write(serio, 0);
281         serio->write(serio, HIL_PKT_CMD >> 8);
282         serio->write(serio, HIL_CMD_RSC);
283         down(&ptr->sem);
284
285         serio->write(serio, 0);
286         serio->write(serio, 0);
287         serio->write(serio, HIL_PKT_CMD >> 8);
288         serio->write(serio, HIL_CMD_RNM);
289         down(&ptr->sem);
290
291         serio->write(serio, 0);
292         serio->write(serio, 0);
293         serio->write(serio, HIL_PKT_CMD >> 8);
294         serio->write(serio, HIL_CMD_EXD);
295         down(&ptr->sem);
296
297         up(&ptr->sem);
298
299         did = ptr->idd[0];
300         idd = ptr->idd + 1;
301         txt = "unknown";
302         if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_REL) {
303                 ptr->dev->evbit[0] = BIT(EV_REL);
304                 txt = "relative";
305         }
306
307         if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_ABS) {
308                 ptr->dev->evbit[0] = BIT(EV_ABS);
309                 txt = "absolute";
310         }
311         if (!ptr->dev->evbit[0])
312                 goto bail2;
313
314         ptr->nbtn = HIL_IDD_NUM_BUTTONS(idd);
315         if (ptr->nbtn)
316                 ptr->dev->evbit[0] |= BIT(EV_KEY);
317
318         naxsets = HIL_IDD_NUM_AXSETS(*idd);
319         ptr->naxes = HIL_IDD_NUM_AXES_PER_SET(*idd);
320
321         printk(KERN_INFO PREFIX "HIL pointer device found (did: 0x%02x, axis: %s)\n",
322                         did, txt);
323         printk(KERN_INFO PREFIX "HIL pointer has %i buttons and %i sets of %i axes\n",
324                         ptr->nbtn, naxsets, ptr->naxes);
325
326         btntype = BTN_MISC;
327         if ((did & HIL_IDD_DID_ABS_TABLET_MASK) == HIL_IDD_DID_ABS_TABLET)
328 #ifdef TABLET_SIMULATES_MOUSE
329                 btntype = BTN_TOUCH;
330 #else
331                 btntype = BTN_DIGI;
332 #endif
333         if ((did & HIL_IDD_DID_ABS_TSCREEN_MASK) == HIL_IDD_DID_ABS_TSCREEN)
334                 btntype = BTN_TOUCH;
335
336         if ((did & HIL_IDD_DID_REL_MOUSE_MASK) == HIL_IDD_DID_REL_MOUSE)
337                 btntype = BTN_MOUSE;
338
339         for (i = 0; i < ptr->nbtn; i++) {
340                 set_bit(btntype | i, ptr->dev->keybit);
341                 ptr->btnmap[i] = btntype | i;
342         }
343
344         if (btntype == BTN_MOUSE) {
345                 /* Swap buttons 2 and 3 */
346                 ptr->btnmap[1] = BTN_MIDDLE;
347                 ptr->btnmap[2] = BTN_RIGHT;
348         }
349
350         if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_REL) {
351                 for (i = 0; i < ptr->naxes; i++)
352                         set_bit(REL_X + i, ptr->dev->relbit);
353                 for (i = 3; (i < ptr->naxes + 3) && (naxsets > 1); i++)
354                         set_bit(REL_X + i, ptr->dev->relbit);
355         } else {
356                 for (i = 0; i < ptr->naxes; i++) {
357                         set_bit(ABS_X + i, ptr->dev->absbit);
358                         ptr->dev->absmin[ABS_X + i] = 0;
359                         ptr->dev->absmax[ABS_X + i] =
360                                 HIL_IDD_AXIS_MAX((ptr->idd + 1), i);
361                 }
362                 for (i = 3; (i < ptr->naxes + 3) && (naxsets > 1); i++) {
363                         set_bit(ABS_X + i, ptr->dev->absbit);
364                         ptr->dev->absmin[ABS_X + i] = 0;
365                         ptr->dev->absmax[ABS_X + i] =
366                                 HIL_IDD_AXIS_MAX((ptr->idd + 1), (i - 3));
367                 }
368 #ifdef TABLET_AUTOADJUST
369                 for (i = 0; i < ABS_MAX; i++) {
370                         int diff = ptr->dev->absmax[ABS_X + i] / 10;
371                         ptr->dev->absmin[ABS_X + i] += diff;
372                         ptr->dev->absmax[ABS_X + i] -= diff;
373                 }
374 #endif
375         }
376
377         ptr->dev->name = strlen(ptr->rnm) ? ptr->rnm : HIL_GENERIC_NAME;
378
379         ptr->dev->id.bustype    = BUS_HIL;
380         ptr->dev->id.vendor     = PCI_VENDOR_ID_HP;
381         ptr->dev->id.product    = 0x0001; /* TODO: get from ptr->rsc */
382         ptr->dev->id.version    = 0x0100; /* TODO: get from ptr->rsc */
383         ptr->dev->cdev.dev      = &serio->dev;
384
385         input_register_device(ptr->dev);
386         printk(KERN_INFO "input: %s (%s), ID: %d\n",
387                 ptr->dev->name,
388                 (btntype == BTN_MOUSE) ? "HIL mouse":"HIL tablet or touchpad",
389                 did);
390
391         return 0;
392  bail2:
393         serio_close(serio);
394  bail1:
395         input_free_device(ptr->dev);
396  bail0:
397         kfree(ptr);
398         serio_set_drvdata(serio, NULL);
399         return -ENODEV;
400 }
401
402 static struct serio_device_id hil_ptr_ids[] = {
403         {
404                 .type = SERIO_HIL_MLC,
405                 .proto = SERIO_HIL,
406                 .id = SERIO_ANY,
407                 .extra = SERIO_ANY,
408         },
409         { 0 }
410 };
411
412 static struct serio_driver hil_ptr_serio_driver = {
413         .driver         = {
414                 .name   = "hil_ptr",
415         },
416         .description    = "HP HIL mouse/tablet driver",
417         .id_table       = hil_ptr_ids,
418         .connect        = hil_ptr_connect,
419         .disconnect     = hil_ptr_disconnect,
420         .interrupt      = hil_ptr_interrupt
421 };
422
423 static int __init hil_ptr_init(void)
424 {
425         return serio_register_driver(&hil_ptr_serio_driver);
426 }
427
428 static void __exit hil_ptr_exit(void)
429 {
430         serio_unregister_driver(&hil_ptr_serio_driver);
431 }
432
433 module_init(hil_ptr_init);
434 module_exit(hil_ptr_exit);