blob: 71c1971abf80873ba4a2a46590483b72428c4098 [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>
Matthew Wilcox0f17e4c2008-07-24 08:30:48 -040040#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
42#include <linux/pci_ids.h>
43
44#define PREFIX "HIL KEYB: "
45#define HIL_GENERIC_NAME "HIL keyboard"
46
47MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
48MODULE_DESCRIPTION(HIL_GENERIC_NAME " driver");
49MODULE_LICENSE("Dual BSD/GPL");
50
51#define HIL_KBD_MAX_LENGTH 16
52
53#define HIL_KBD_SET1_UPBIT 0x01
54#define HIL_KBD_SET1_SHIFT 1
Helge Deller3acaf542007-02-28 23:51:19 -050055static unsigned int hil_kbd_set1[HIL_KEYCODES_SET1_TBLSIZE] __read_mostly =
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 { HIL_KEYCODES_SET1 };
57
58#define HIL_KBD_SET2_UPBIT 0x01
59#define HIL_KBD_SET2_SHIFT 1
60/* Set2 is user defined */
61
62#define HIL_KBD_SET3_UPBIT 0x80
63#define HIL_KBD_SET3_SHIFT 0
Helge Deller3acaf542007-02-28 23:51:19 -050064static unsigned int hil_kbd_set3[HIL_KEYCODES_SET3_TBLSIZE] __read_mostly =
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { HIL_KEYCODES_SET3 };
66
Helge Deller3acaf542007-02-28 23:51:19 -050067static const char hil_language[][16] = { HIL_LOCALE_MAP };
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69struct hil_kbd {
Helge Deller102c8c72006-03-26 07:41:55 -070070 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct serio *serio;
72
73 /* Input buffer and index for packets from HIL bus. */
74 hil_packet data[HIL_KBD_MAX_LENGTH];
75 int idx4; /* four counts per packet */
76
77 /* Raw device info records from HIL bus, see hil.h for fields. */
78 char idd[HIL_KBD_MAX_LENGTH]; /* DID byte and IDD record */
79 char rsc[HIL_KBD_MAX_LENGTH]; /* RSC record */
80 char exd[HIL_KBD_MAX_LENGTH]; /* EXD record */
81 char rnm[HIL_KBD_MAX_LENGTH + 1]; /* RNM record + NULL term. */
82
83 /* Something to sleep around with. */
84 struct semaphore sem;
85};
86
87/* Process a complete packet after transfer from the HIL */
88static void hil_kbd_process_record(struct hil_kbd *kbd)
89{
Helge Deller102c8c72006-03-26 07:41:55 -070090 struct input_dev *dev = kbd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 hil_packet *data = kbd->data;
92 hil_packet p;
93 int idx, i, cnt;
94
95 idx = kbd->idx4/4;
96 p = data[idx - 1];
97
Helge Dellerffd51f42007-02-28 23:51:29 -050098 if ((p & ~HIL_CMDCT_POL) ==
99 (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL))
100 goto report;
101 if ((p & ~HIL_CMDCT_RPL) ==
102 (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_RPL))
103 goto report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 /* Not a poll response. See if we are loading config records. */
106 switch (p & HIL_PKT_DATA_MASK) {
107 case HIL_CMD_IDD:
108 for (i = 0; i < idx; i++)
109 kbd->idd[i] = kbd->data[i] & HIL_PKT_DATA_MASK;
110 for (; i < HIL_KBD_MAX_LENGTH; i++)
111 kbd->idd[i] = 0;
112 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 case HIL_CMD_RSC:
115 for (i = 0; i < idx; i++)
116 kbd->rsc[i] = kbd->data[i] & HIL_PKT_DATA_MASK;
117 for (; i < HIL_KBD_MAX_LENGTH; i++)
118 kbd->rsc[i] = 0;
119 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 case HIL_CMD_EXD:
122 for (i = 0; i < idx; i++)
123 kbd->exd[i] = kbd->data[i] & HIL_PKT_DATA_MASK;
124 for (; i < HIL_KBD_MAX_LENGTH; i++)
125 kbd->exd[i] = 0;
126 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 case HIL_CMD_RNM:
129 for (i = 0; i < idx; i++)
130 kbd->rnm[i] = kbd->data[i] & HIL_PKT_DATA_MASK;
131 for (; i < HIL_KBD_MAX_LENGTH + 1; i++)
132 kbd->rnm[i] = '\0';
133 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 default:
136 /* These occur when device isn't present */
Helge Dellerffd51f42007-02-28 23:51:29 -0500137 if (p == (HIL_ERR_INT | HIL_PKT_CMD))
138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* Anything else we'd like to know about. */
140 printk(KERN_WARNING PREFIX "Device sent unknown record %x\n", p);
141 break;
142 }
143 goto out;
144
145 report:
146 cnt = 1;
147 switch (kbd->data[0] & HIL_POL_CHARTYPE_MASK) {
148 case HIL_POL_CHARTYPE_NONE:
149 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 case HIL_POL_CHARTYPE_ASCII:
152 while (cnt < idx - 1)
153 input_report_key(dev, kbd->data[cnt++] & 0x7f, 1);
154 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 case HIL_POL_CHARTYPE_RSVD1:
157 case HIL_POL_CHARTYPE_RSVD2:
158 case HIL_POL_CHARTYPE_BINARY:
159 while (cnt < idx - 1)
160 input_report_key(dev, kbd->data[cnt++], 1);
161 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 case HIL_POL_CHARTYPE_SET1:
164 while (cnt < idx - 1) {
165 unsigned int key;
166 int up;
167 key = kbd->data[cnt++];
168 up = key & HIL_KBD_SET1_UPBIT;
169 key &= (~HIL_KBD_SET1_UPBIT & 0xff);
170 key = hil_kbd_set1[key >> HIL_KBD_SET1_SHIFT];
171 if (key != KEY_RESERVED)
172 input_report_key(dev, key, !up);
173 }
174 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 case HIL_POL_CHARTYPE_SET2:
177 while (cnt < idx - 1) {
178 unsigned int key;
179 int up;
180 key = kbd->data[cnt++];
181 up = key & HIL_KBD_SET2_UPBIT;
182 key &= (~HIL_KBD_SET1_UPBIT & 0xff);
183 key = key >> HIL_KBD_SET2_SHIFT;
184 if (key != KEY_RESERVED)
185 input_report_key(dev, key, !up);
186 }
187 break;
Helge Dellerffd51f42007-02-28 23:51:29 -0500188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 case HIL_POL_CHARTYPE_SET3:
190 while (cnt < idx - 1) {
191 unsigned int key;
192 int up;
193 key = kbd->data[cnt++];
194 up = key & HIL_KBD_SET3_UPBIT;
195 key &= (~HIL_KBD_SET1_UPBIT & 0xff);
196 key = hil_kbd_set3[key >> HIL_KBD_SET3_SHIFT];
197 if (key != KEY_RESERVED)
198 input_report_key(dev, key, !up);
199 }
200 break;
201 }
202 out:
203 kbd->idx4 = 0;
204 up(&kbd->sem);
205}
206
Helge Dellerffd51f42007-02-28 23:51:29 -0500207static void hil_kbd_process_err(struct hil_kbd *kbd)
208{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 printk(KERN_WARNING PREFIX "errored HIL packet\n");
210 kbd->idx4 = 0;
211 up(&kbd->sem);
212}
213
Helge Dellerffd51f42007-02-28 23:51:29 -0500214static irqreturn_t hil_kbd_interrupt(struct serio *serio,
215 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct hil_kbd *kbd;
218 hil_packet packet;
219 int idx;
220
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400221 kbd = serio_get_drvdata(serio);
Helge Dellerffd51f42007-02-28 23:51:29 -0500222 BUG_ON(kbd == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (kbd->idx4 >= (HIL_KBD_MAX_LENGTH * sizeof(hil_packet))) {
225 hil_kbd_process_err(kbd);
226 return IRQ_HANDLED;
227 }
228 idx = kbd->idx4/4;
Helge Dellerffd51f42007-02-28 23:51:29 -0500229 if (!(kbd->idx4 % 4))
230 kbd->data[idx] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 packet = kbd->data[idx];
232 packet |= ((hil_packet)data) << ((3 - (kbd->idx4 % 4)) * 8);
233 kbd->data[idx] = packet;
234
235 /* Records of N 4-byte hil_packets must terminate with a command. */
Helge Dellerffd51f42007-02-28 23:51:29 -0500236 if ((++(kbd->idx4)) % 4)
237 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if ((packet & 0xffff0000) != HIL_ERR_INT) {
239 hil_kbd_process_err(kbd);
240 return IRQ_HANDLED;
241 }
Helge Dellerffd51f42007-02-28 23:51:29 -0500242 if (packet & HIL_PKT_CMD)
243 hil_kbd_process_record(kbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return IRQ_HANDLED;
245}
246
247static void hil_kbd_disconnect(struct serio *serio)
248{
249 struct hil_kbd *kbd;
250
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400251 kbd = serio_get_drvdata(serio);
Helge Dellerffd51f42007-02-28 23:51:29 -0500252 BUG_ON(kbd == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 serio_close(serio);
Kyle McMartincd7a9202006-03-30 11:47:32 -0500255 input_unregister_device(kbd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 kfree(kbd);
257}
258
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400259static int hil_kbd_connect(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct hil_kbd *kbd;
262 uint8_t did, *idd;
263 int i;
Dmitry Torokhov95d465f2006-04-02 00:08:05 -0500264
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500265 kbd = kzalloc(sizeof(*kbd), GFP_KERNEL);
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400266 if (!kbd)
267 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Helge Deller102c8c72006-03-26 07:41:55 -0700269 kbd->dev = input_allocate_device();
Dmitry Torokhov95d465f2006-04-02 00:08:05 -0500270 if (!kbd->dev)
271 goto bail0;
272
Dmitry Torokhov95d465f2006-04-02 00:08:05 -0500273 if (serio_open(serio, drv))
274 goto bail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400276 serio_set_drvdata(serio, kbd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 kbd->serio = serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Helge Dellerffd51f42007-02-28 23:51:29 -0500279 init_MUTEX_LOCKED(&kbd->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Get device info. MLC driver supplies devid/status/etc. */
282 serio->write(serio, 0);
283 serio->write(serio, 0);
284 serio->write(serio, HIL_PKT_CMD >> 8);
285 serio->write(serio, HIL_CMD_IDD);
Helge Dellerffd51f42007-02-28 23:51:29 -0500286 down(&kbd->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 serio->write(serio, 0);
289 serio->write(serio, 0);
290 serio->write(serio, HIL_PKT_CMD >> 8);
291 serio->write(serio, HIL_CMD_RSC);
Helge Dellerffd51f42007-02-28 23:51:29 -0500292 down(&kbd->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 serio->write(serio, 0);
295 serio->write(serio, 0);
296 serio->write(serio, HIL_PKT_CMD >> 8);
297 serio->write(serio, HIL_CMD_RNM);
Helge Dellerffd51f42007-02-28 23:51:29 -0500298 down(&kbd->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 serio->write(serio, 0);
301 serio->write(serio, 0);
302 serio->write(serio, HIL_PKT_CMD >> 8);
303 serio->write(serio, HIL_CMD_EXD);
Helge Dellerffd51f42007-02-28 23:51:29 -0500304 down(&kbd->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Helge Dellerffd51f42007-02-28 23:51:29 -0500306 up(&kbd->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 did = kbd->idd[0];
309 idd = kbd->idd + 1;
310 switch (did & HIL_IDD_DID_TYPE_MASK) {
311 case HIL_IDD_DID_TYPE_KB_INTEGRAL:
312 case HIL_IDD_DID_TYPE_KB_ITF:
313 case HIL_IDD_DID_TYPE_KB_RSVD:
314 case HIL_IDD_DID_TYPE_CHAR:
315 printk(KERN_INFO PREFIX "HIL keyboard found (did = 0x%02x, lang = %s)\n",
316 did, hil_language[did & HIL_IDD_DID_TYPE_KB_LANG_MASK]);
317 break;
318 default:
Helge Deller102c8c72006-03-26 07:41:55 -0700319 goto bail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
Helge Dellerffd51f42007-02-28 23:51:29 -0500322 if (HIL_IDD_NUM_BUTTONS(idd) || HIL_IDD_NUM_AXES_PER_SET(*idd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 printk(KERN_INFO PREFIX "keyboards only, no combo devices supported.\n");
Helge Deller102c8c72006-03-26 07:41:55 -0700324 goto bail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700327 kbd->dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
328 kbd->dev->ledbit[0] = BIT_MASK(LED_NUML) | BIT_MASK(LED_CAPSL) |
329 BIT_MASK(LED_SCROLLL);
Helge Deller102c8c72006-03-26 07:41:55 -0700330 kbd->dev->keycodemax = HIL_KEYCODES_SET1_TBLSIZE;
331 kbd->dev->keycodesize = sizeof(hil_kbd_set1[0]);
332 kbd->dev->keycode = hil_kbd_set1;
333 kbd->dev->name = strlen(kbd->rnm) ? kbd->rnm : HIL_GENERIC_NAME;
334 kbd->dev->phys = "hpkbd/input0"; /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Helge Deller102c8c72006-03-26 07:41:55 -0700336 kbd->dev->id.bustype = BUS_HIL;
337 kbd->dev->id.vendor = PCI_VENDOR_ID_HP;
338 kbd->dev->id.product = 0x0001; /* TODO: get from kbd->rsc */
339 kbd->dev->id.version = 0x0100; /* TODO: get from kbd->rsc */
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400340 kbd->dev->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 for (i = 0; i < 128; i++) {
Helge Deller102c8c72006-03-26 07:41:55 -0700343 set_bit(hil_kbd_set1[i], kbd->dev->keybit);
344 set_bit(hil_kbd_set3[i], kbd->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Helge Deller102c8c72006-03-26 07:41:55 -0700346 clear_bit(0, kbd->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Helge Deller102c8c72006-03-26 07:41:55 -0700348 input_register_device(kbd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 printk(KERN_INFO "input: %s, ID: %d\n",
Helge Deller102c8c72006-03-26 07:41:55 -0700350 kbd->dev->name, did);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 serio->write(serio, 0);
353 serio->write(serio, 0);
354 serio->write(serio, HIL_PKT_CMD >> 8);
355 serio->write(serio, HIL_CMD_EK1); /* Enable Keyswitch Autorepeat 1 */
Helge Dellerffd51f42007-02-28 23:51:29 -0500356 down(&kbd->sem);
357 up(&kbd->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400359 return 0;
Helge Deller102c8c72006-03-26 07:41:55 -0700360 bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 serio_close(serio);
Matthew Wilcoxd668da82006-04-03 13:44:17 +0000362 serio_set_drvdata(serio, NULL);
Helge Deller102c8c72006-03-26 07:41:55 -0700363 bail1:
364 input_free_device(kbd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 bail0:
366 kfree(kbd);
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400367 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400370static struct serio_device_id hil_kbd_ids[] = {
371 {
372 .type = SERIO_HIL_MLC,
373 .proto = SERIO_HIL,
374 .id = SERIO_ANY,
375 .extra = SERIO_ANY,
376 },
377 { 0 }
378};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Helge Deller3acaf542007-02-28 23:51:19 -0500380static struct serio_driver hil_kbd_serio_drv = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 .driver = {
382 .name = "hil_kbd",
383 },
384 .description = "HP HIL keyboard driver",
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400385 .id_table = hil_kbd_ids,
Helge Dellerffd51f42007-02-28 23:51:29 -0500386 .connect = hil_kbd_connect,
387 .disconnect = hil_kbd_disconnect,
388 .interrupt = hil_kbd_interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389};
390
391static int __init hil_kbd_init(void)
392{
Akinobu Mita153a9df02006-11-23 23:35:10 -0500393 return serio_register_driver(&hil_kbd_serio_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
Helge Dellerffd51f42007-02-28 23:51:29 -0500395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static void __exit hil_kbd_exit(void)
397{
398 serio_unregister_driver(&hil_kbd_serio_drv);
399}
Helge Dellerffd51f42007-02-28 23:51:29 -0500400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401module_init(hil_kbd_init);
402module_exit(hil_kbd_exit);