blob: c83f4b2ec7d3546e5229cb148b4a087265b05c16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic linux-input device driver for keyboard 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>
Dmitry Torokhov6777f012009-08-07 23:17:46 -070040#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
42#include <linux/pci_ids.h>
43
Dmitry Torokhovfa71c602009-08-07 23:17:47 -070044#define PREFIX "HIL: "
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
Dmitry Torokhovfa71c602009-08-07 23:17:47 -070047MODULE_DESCRIPTION("HIL keyboard/mouse driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048MODULE_LICENSE("Dual BSD/GPL");
Dmitry Torokhovfa71c602009-08-07 23:17:47 -070049MODULE_ALIAS("serio:ty03pr25id00ex*"); /* HIL keyboard */
50MODULE_ALIAS("serio:ty03pr25id0Fex*"); /* HIL mouse */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Dmitry Torokhov1437dc32009-08-07 23:17:47 -070052#define HIL_PACKET_MAX_LENGTH 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define HIL_KBD_SET1_UPBIT 0x01
55#define HIL_KBD_SET1_SHIFT 1
Helge Deller3acaf542007-02-28 23:51:19 -050056static unsigned int hil_kbd_set1[HIL_KEYCODES_SET1_TBLSIZE] __read_mostly =
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 { HIL_KEYCODES_SET1 };
58
59#define HIL_KBD_SET2_UPBIT 0x01
60#define HIL_KBD_SET2_SHIFT 1
61/* Set2 is user defined */
62
63#define HIL_KBD_SET3_UPBIT 0x80
64#define HIL_KBD_SET3_SHIFT 0
Helge Deller3acaf542007-02-28 23:51:19 -050065static unsigned int hil_kbd_set3[HIL_KEYCODES_SET3_TBLSIZE] __read_mostly =
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 { HIL_KEYCODES_SET3 };
67
Helge Deller3acaf542007-02-28 23:51:19 -050068static const char hil_language[][16] = { HIL_LOCALE_MAP };
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dmitry Torokhov1437dc32009-08-07 23:17:47 -070070struct hil_dev {
Helge Deller102c8c72006-03-26 07:41:55 -070071 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct serio *serio;
73
74 /* Input buffer and index for packets from HIL bus. */
Dmitry Torokhov1437dc32009-08-07 23:17:47 -070075 hil_packet data[HIL_PACKET_MAX_LENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int idx4; /* four counts per packet */
77
78 /* Raw device info records from HIL bus, see hil.h for fields. */
Dmitry Torokhov1437dc32009-08-07 23:17:47 -070079 char idd[HIL_PACKET_MAX_LENGTH]; /* DID byte and IDD record */
80 char rsc[HIL_PACKET_MAX_LENGTH]; /* RSC record */
81 char exd[HIL_PACKET_MAX_LENGTH]; /* EXD record */
82 char rnm[HIL_PACKET_MAX_LENGTH + 1]; /* RNM record + NULL term. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Dmitry Torokhov6777f012009-08-07 23:17:46 -070084 struct completion cmd_done;
Dmitry Torokhovfa71c602009-08-07 23:17:47 -070085
86 bool is_pointer;
87 /* Extra device details needed for pointing devices. */
88 unsigned int nbtn, naxes;
89 unsigned int btnmap[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Dmitry Torokhov1437dc32009-08-07 23:17:47 -070092static bool hil_dev_is_command_response(hil_packet p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Dmitry Torokhov6777f012009-08-07 23:17:46 -070094 if ((p & ~HIL_CMDCT_POL) == (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL))
95 return false;
96
97 if ((p & ~HIL_CMDCT_RPL) == (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_RPL))
98 return false;
99
100 return true;
101}
102
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700103static void hil_dev_handle_command_response(struct hil_dev *dev)
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700104{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 hil_packet p;
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700106 char *buf;
107 int i, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700109 idx = dev->idx4 / 4;
110 p = dev->data[idx - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 switch (p & HIL_PKT_DATA_MASK) {
113 case HIL_CMD_IDD:
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700114 buf = dev->idd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 case HIL_CMD_RSC:
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700118 buf = dev->rsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 case HIL_CMD_EXD:
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700122 buf = dev->exd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 case HIL_CMD_RNM:
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700126 dev->rnm[HIL_PACKET_MAX_LENGTH] = 0;
127 buf = dev->rnm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 default:
131 /* These occur when device isn't present */
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700132 if (p != (HIL_ERR_INT | HIL_PKT_CMD)) {
133 /* Anything else we'd like to know about. */
134 printk(KERN_WARNING PREFIX "Device sent unknown record %x\n", p);
135 }
136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700139 for (i = 0; i < idx; i++)
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700140 buf[i] = dev->data[i] & HIL_PKT_DATA_MASK;
141 for (; i < HIL_PACKET_MAX_LENGTH; i++)
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700142 buf[i] = 0;
143 out:
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700144 complete(&dev->cmd_done);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700145}
146
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700147static void hil_dev_handle_kbd_events(struct hil_dev *kbd)
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700148{
149 struct input_dev *dev = kbd->dev;
150 int idx = kbd->idx4 / 4;
151 int i;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 switch (kbd->data[0] & HIL_POL_CHARTYPE_MASK) {
154 case HIL_POL_CHARTYPE_NONE:
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700155 return;
Helge Dellerffd51f42007-02-28 23:51:29 -0500156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 case HIL_POL_CHARTYPE_ASCII:
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700158 for (i = 1; i < idx - 1; i++)
159 input_report_key(dev, kbd->data[i] & 0x7f, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 case HIL_POL_CHARTYPE_RSVD1:
163 case HIL_POL_CHARTYPE_RSVD2:
164 case HIL_POL_CHARTYPE_BINARY:
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700165 for (i = 1; i < idx - 1; i++)
166 input_report_key(dev, kbd->data[i], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 case HIL_POL_CHARTYPE_SET1:
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700170 for (i = 1; i < idx - 1; i++) {
171 unsigned int key = kbd->data[i];
172 int up = key & HIL_KBD_SET1_UPBIT;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 key &= (~HIL_KBD_SET1_UPBIT & 0xff);
175 key = hil_kbd_set1[key >> HIL_KBD_SET1_SHIFT];
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700176 input_report_key(dev, key, !up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 case HIL_POL_CHARTYPE_SET2:
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700181 for (i = 1; i < idx - 1; i++) {
182 unsigned int key = kbd->data[i];
183 int up = key & HIL_KBD_SET2_UPBIT;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 key &= (~HIL_KBD_SET1_UPBIT & 0xff);
186 key = key >> HIL_KBD_SET2_SHIFT;
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700187 input_report_key(dev, key, !up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 case HIL_POL_CHARTYPE_SET3:
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700192 for (i = 1; i < idx - 1; i++) {
193 unsigned int key = kbd->data[i];
194 int up = key & HIL_KBD_SET3_UPBIT;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 key &= (~HIL_KBD_SET1_UPBIT & 0xff);
197 key = hil_kbd_set3[key >> HIL_KBD_SET3_SHIFT];
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700198 input_report_key(dev, key, !up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 break;
201 }
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700202
203 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700206static void hil_dev_handle_ptr_events(struct hil_dev *ptr)
207{
208 struct input_dev *dev = ptr->dev;
209 int idx = ptr->idx4 / 4;
210 hil_packet p = ptr->data[idx - 1];
211 int i, cnt, laxis;
212 bool absdev, ax16;
213
214 if ((p & HIL_CMDCT_POL) != idx - 1) {
215 printk(KERN_WARNING PREFIX
216 "Malformed poll packet %x (idx = %i)\n", p, idx);
217 return;
218 }
219
220 i = (p & HIL_POL_AXIS_ALT) ? 3 : 0;
221 laxis = (p & HIL_POL_NUM_AXES_MASK) + i;
222
223 ax16 = ptr->idd[1] & HIL_IDD_HEADER_16BIT; /* 8 or 16bit resolution */
224 absdev = ptr->idd[1] & HIL_IDD_HEADER_ABS;
225
226 for (cnt = 1; i < laxis; i++) {
227 unsigned int lo, hi, val;
228
229 lo = ptr->data[cnt++] & HIL_PKT_DATA_MASK;
230 hi = ax16 ? (ptr->data[cnt++] & HIL_PKT_DATA_MASK) : 0;
231
232 if (absdev) {
233 val = lo + (hi << 8);
234#ifdef TABLET_AUTOADJUST
235 if (val < dev->absmin[ABS_X + i])
236 dev->absmin[ABS_X + i] = val;
237 if (val > dev->absmax[ABS_X + i])
238 dev->absmax[ABS_X + i] = val;
239#endif
240 if (i%3) val = dev->absmax[ABS_X + i] - val;
241 input_report_abs(dev, ABS_X + i, val);
242 } else {
243 val = (int) (((int8_t)lo) | ((int8_t)hi << 8));
244 if (i % 3)
245 val *= -1;
246 input_report_rel(dev, REL_X + i, val);
247 }
248 }
249
250 while (cnt < idx - 1) {
251 unsigned int btn = ptr->data[cnt++];
252 int up = btn & 1;
253
254 btn &= 0xfe;
255 if (btn == 0x8e)
256 continue; /* TODO: proximity == touch? */
257 if (btn > 0x8c || btn < 0x80)
258 continue;
259 btn = (btn - 0x80) >> 1;
260 btn = ptr->btnmap[btn];
261 input_report_key(dev, btn, !up);
262 }
263
264 input_sync(dev);
265}
266
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700267static void hil_dev_process_err(struct hil_dev *dev)
Helge Dellerffd51f42007-02-28 23:51:29 -0500268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 printk(KERN_WARNING PREFIX "errored HIL packet\n");
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700270 dev->idx4 = 0;
271 complete(&dev->cmd_done); /* just in case somebody is waiting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700274static irqreturn_t hil_dev_interrupt(struct serio *serio,
Helge Dellerffd51f42007-02-28 23:51:29 -0500275 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700277 struct hil_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 hil_packet packet;
279 int idx;
280
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700281 dev = serio_get_drvdata(serio);
282 BUG_ON(dev == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700284 if (dev->idx4 >= HIL_PACKET_MAX_LENGTH * sizeof(hil_packet)) {
285 hil_dev_process_err(dev);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700286 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700288
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700289 idx = dev->idx4 / 4;
290 if (!(dev->idx4 % 4))
291 dev->data[idx] = 0;
292 packet = dev->data[idx];
293 packet |= ((hil_packet)data) << ((3 - (dev->idx4 % 4)) * 8);
294 dev->data[idx] = packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /* Records of N 4-byte hil_packets must terminate with a command. */
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700297 if ((++dev->idx4 % 4) == 0) {
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700298 if ((packet & 0xffff0000) != HIL_ERR_INT) {
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700299 hil_dev_process_err(dev);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700300 } else if (packet & HIL_PKT_CMD) {
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700301 if (hil_dev_is_command_response(packet))
302 hil_dev_handle_command_response(dev);
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700303 else if (dev->is_pointer)
304 hil_dev_handle_ptr_events(dev);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700305 else
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700306 hil_dev_handle_kbd_events(dev);
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700307 dev->idx4 = 0;
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700310 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return IRQ_HANDLED;
312}
313
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700314static void hil_dev_disconnect(struct serio *serio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700316 struct hil_dev *dev = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700318 BUG_ON(dev == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 serio_close(serio);
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700321 input_unregister_device(dev->dev);
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700322 serio_set_drvdata(serio, NULL);
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700323 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700326static void hil_dev_keyboard_setup(struct hil_dev *kbd)
327{
328 struct input_dev *input_dev = kbd->dev;
329 uint8_t did = kbd->idd[0];
330 int i;
331
332 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
333 input_dev->ledbit[0] = BIT_MASK(LED_NUML) | BIT_MASK(LED_CAPSL) |
334 BIT_MASK(LED_SCROLLL);
335
336 for (i = 0; i < 128; i++) {
337 __set_bit(hil_kbd_set1[i], input_dev->keybit);
338 __set_bit(hil_kbd_set3[i], input_dev->keybit);
339 }
340 __clear_bit(KEY_RESERVED, input_dev->keybit);
341
342 input_dev->keycodemax = HIL_KEYCODES_SET1_TBLSIZE;
343 input_dev->keycodesize = sizeof(hil_kbd_set1[0]);
344 input_dev->keycode = hil_kbd_set1;
345
346 input_dev->name = strlen(kbd->rnm) ? kbd->rnm : "HIL keyboard";
347 input_dev->phys = "hpkbd/input0";
348
349 printk(KERN_INFO PREFIX "HIL keyboard found (did = 0x%02x, lang = %s)\n",
350 did, hil_language[did & HIL_IDD_DID_TYPE_KB_LANG_MASK]);
351}
352
353static void hil_dev_pointer_setup(struct hil_dev *ptr)
354{
355 struct input_dev *input_dev = ptr->dev;
356 uint8_t did = ptr->idd[0];
357 uint8_t *idd = ptr->idd + 1;
358 unsigned int naxsets = HIL_IDD_NUM_AXSETS(*idd);
359 unsigned int i, btntype;
360 const char *txt;
361
362 ptr->naxes = HIL_IDD_NUM_AXES_PER_SET(*idd);
363
364 switch (did & HIL_IDD_DID_TYPE_MASK) {
365 case HIL_IDD_DID_TYPE_REL:
366 input_dev->evbit[0] = BIT_MASK(EV_REL);
367
368 for (i = 0; i < ptr->naxes; i++)
369 __set_bit(REL_X + i, input_dev->relbit);
370
371 for (i = 3; naxsets > 1 && i < ptr->naxes + 3; i++)
372 __set_bit(REL_X + i, input_dev->relbit);
373
374 txt = "relative";
375 break;
376
377 case HIL_IDD_DID_TYPE_ABS:
378 input_dev->evbit[0] = BIT_MASK(EV_ABS);
379
380 for (i = 0; i < ptr->naxes; i++)
381 input_set_abs_params(input_dev, ABS_X + i,
382 0, HIL_IDD_AXIS_MAX(idd, i), 0, 0);
383
384 for (i = 3; naxsets > 1 && i < ptr->naxes + 3; i++)
385 input_set_abs_params(input_dev, ABS_X + i,
386 0, HIL_IDD_AXIS_MAX(idd, i - 3), 0, 0);
387
388#ifdef TABLET_AUTOADJUST
389 for (i = 0; i < ABS_MAX; i++) {
390 int diff = input_dev->absmax[ABS_X + i] / 10;
391 input_dev->absmin[ABS_X + i] += diff;
392 input_dev->absmax[ABS_X + i] -= diff;
393 }
394#endif
395
396 txt = "absolute";
397 break;
398
399 default:
400 BUG();
401 }
402
403 ptr->nbtn = HIL_IDD_NUM_BUTTONS(idd);
404 if (ptr->nbtn)
405 input_dev->evbit[0] |= BIT_MASK(EV_KEY);
406
407 btntype = BTN_MISC;
408 if ((did & HIL_IDD_DID_ABS_TABLET_MASK) == HIL_IDD_DID_ABS_TABLET)
409#ifdef TABLET_SIMULATES_MOUSE
410 btntype = BTN_TOUCH;
411#else
412 btntype = BTN_DIGI;
413#endif
414 if ((did & HIL_IDD_DID_ABS_TSCREEN_MASK) == HIL_IDD_DID_ABS_TSCREEN)
415 btntype = BTN_TOUCH;
416
417 if ((did & HIL_IDD_DID_REL_MOUSE_MASK) == HIL_IDD_DID_REL_MOUSE)
418 btntype = BTN_MOUSE;
419
420 for (i = 0; i < ptr->nbtn; i++) {
421 __set_bit(btntype | i, input_dev->keybit);
422 ptr->btnmap[i] = btntype | i;
423 }
424
425 if (btntype == BTN_MOUSE) {
426 /* Swap buttons 2 and 3 */
427 ptr->btnmap[1] = BTN_MIDDLE;
428 ptr->btnmap[2] = BTN_RIGHT;
429 }
430
431 input_dev->name = strlen(ptr->rnm) ? ptr->rnm : "HIL pointer device";
432
433 printk(KERN_INFO PREFIX
434 "HIL pointer device found (did: 0x%02x, axis: %s)\n",
435 did, txt);
436 printk(KERN_INFO PREFIX
437 "HIL pointer has %i buttons and %i sets of %i axes\n",
438 ptr->nbtn, naxsets, ptr->naxes);
439}
440
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700441static int hil_dev_connect(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700443 struct hil_dev *dev;
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700444 struct input_dev *input_dev;
445 uint8_t did, *idd;
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700446 int error;
Dmitry Torokhov95d465f2006-04-02 00:08:05 -0500447
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700448 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700449 input_dev = input_allocate_device();
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700450 if (!dev || !input_dev) {
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700451 error = -ENOMEM;
452 goto bail0;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700455 dev->serio = serio;
456 dev->dev = input_dev;
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700457
458 error = serio_open(serio, drv);
459 if (error)
Dmitry Torokhov95d465f2006-04-02 00:08:05 -0500460 goto bail0;
461
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700462 serio_set_drvdata(serio, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* Get device info. MLC driver supplies devid/status/etc. */
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700465 init_completion(&dev->cmd_done);
Dmitry Torokhovdd0d5442009-08-05 00:30:26 -0700466 serio_write(serio, 0);
467 serio_write(serio, 0);
468 serio_write(serio, HIL_PKT_CMD >> 8);
469 serio_write(serio, HIL_CMD_IDD);
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700470 error = wait_for_completion_killable(&dev->cmd_done);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700471 if (error)
472 goto bail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700474 init_completion(&dev->cmd_done);
Dmitry Torokhovdd0d5442009-08-05 00:30:26 -0700475 serio_write(serio, 0);
476 serio_write(serio, 0);
477 serio_write(serio, HIL_PKT_CMD >> 8);
478 serio_write(serio, HIL_CMD_RSC);
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700479 error = wait_for_completion_killable(&dev->cmd_done);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700480 if (error)
481 goto bail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700483 init_completion(&dev->cmd_done);
Dmitry Torokhovdd0d5442009-08-05 00:30:26 -0700484 serio_write(serio, 0);
485 serio_write(serio, 0);
486 serio_write(serio, HIL_PKT_CMD >> 8);
487 serio_write(serio, HIL_CMD_RNM);
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700488 error = wait_for_completion_killable(&dev->cmd_done);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700489 if (error)
490 goto bail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700492 init_completion(&dev->cmd_done);
Dmitry Torokhovdd0d5442009-08-05 00:30:26 -0700493 serio_write(serio, 0);
494 serio_write(serio, 0);
495 serio_write(serio, HIL_PKT_CMD >> 8);
496 serio_write(serio, HIL_CMD_EXD);
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700497 error = wait_for_completion_killable(&dev->cmd_done);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700498 if (error)
499 goto bail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700501 did = dev->idd[0];
502 idd = dev->idd + 1;
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 switch (did & HIL_IDD_DID_TYPE_MASK) {
505 case HIL_IDD_DID_TYPE_KB_INTEGRAL:
506 case HIL_IDD_DID_TYPE_KB_ITF:
507 case HIL_IDD_DID_TYPE_KB_RSVD:
508 case HIL_IDD_DID_TYPE_CHAR:
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700509 if (HIL_IDD_NUM_BUTTONS(idd) ||
510 HIL_IDD_NUM_AXES_PER_SET(*idd)) {
511 printk(KERN_INFO PREFIX
512 "combo devices are not supported.\n");
513 goto bail1;
514 }
515
516 dev->is_pointer = false;
517 hil_dev_keyboard_setup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700519
520 case HIL_IDD_DID_TYPE_REL:
521 case HIL_IDD_DID_TYPE_ABS:
522 dev->is_pointer = true;
523 hil_dev_pointer_setup(dev);
524 break;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 default:
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700527 goto bail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700530 input_dev->id.bustype = BUS_HIL;
531 input_dev->id.vendor = PCI_VENDOR_ID_HP;
532 input_dev->id.product = 0x0001; /* TODO: get from kbd->rsc */
533 input_dev->id.version = 0x0100; /* TODO: get from kbd->rsc */
534 input_dev->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700536 if (!dev->is_pointer) {
537 serio_write(serio, 0);
538 serio_write(serio, 0);
539 serio_write(serio, HIL_PKT_CMD >> 8);
540 /* Enable Keyswitch Autorepeat 1 */
541 serio_write(serio, HIL_CMD_EK1);
542 /* No need to wait for completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700544
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700545 error = input_register_device(input_dev);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700546 if (error)
547 goto bail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400549 return 0;
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700550
551 bail1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 serio_close(serio);
Matthew Wilcoxd668da82006-04-03 13:44:17 +0000553 serio_set_drvdata(serio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 bail0:
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700555 input_free_device(input_dev);
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700556 kfree(dev);
Dmitry Torokhov6777f012009-08-07 23:17:46 -0700557 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700560static struct serio_device_id hil_dev_ids[] = {
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400561 {
562 .type = SERIO_HIL_MLC,
563 .proto = SERIO_HIL,
564 .id = SERIO_ANY,
565 .extra = SERIO_ANY,
566 },
567 { 0 }
568};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700570static struct serio_driver hil_serio_drv = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 .driver = {
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700572 .name = "hil_dev",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 },
Dmitry Torokhovfa71c602009-08-07 23:17:47 -0700574 .description = "HP HIL keyboard/mouse/tablet driver",
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700575 .id_table = hil_dev_ids,
576 .connect = hil_dev_connect,
577 .disconnect = hil_dev_disconnect,
578 .interrupt = hil_dev_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579};
580
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700581static int __init hil_dev_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700583 return serio_register_driver(&hil_serio_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
Helge Dellerffd51f42007-02-28 23:51:29 -0500585
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700586static void __exit hil_dev_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700588 serio_unregister_driver(&hil_serio_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
Helge Dellerffd51f42007-02-28 23:51:29 -0500590
Dmitry Torokhov1437dc32009-08-07 23:17:47 -0700591module_init(hil_dev_init);
592module_exit(hil_dev_exit);