37 #include <linux/module.h>
38 #include <linux/slab.h>
41 #include <linux/input.h>
43 #include <linux/adb.h>
44 #include <linux/cuda.h>
45 #include <linux/pmu.h>
47 #include <asm/machdep.h>
48 #ifdef CONFIG_PPC_PMAC
55 static int restore_capslock_events;
58 "Produce keypress events for capslock on both keyup and keydown.");
62 #define MOUSE_DATAREG 0
64 static int adb_message_handler(
struct notifier_block *,
unsigned long,
void *);
66 .notifier_call = adb_message_handler,
70 #define ADB_KEY_DEL 0x33
71 #define ADB_KEY_CMD 0x37
72 #define ADB_KEY_CAPSLOCK 0x39
73 #define ADB_KEY_FN 0x3f
74 #define ADB_KEY_FWDEL 0x75
75 #define ADB_KEY_POWER_OLD 0x7e
76 #define ADB_KEY_POWER 0x7f
78 static const u16 adb_to_linux_keycodes[128] = {
222 #define FLAG_FN_KEY_PRESSED 0x00000001
223 #define FLAG_POWER_FROM_FN 0x00000002
224 #define FLAG_EMU_FWDEL_DOWN 0x00000004
225 #define FLAG_CAPSLOCK_TRANSLATE 0x00000008
226 #define FLAG_CAPSLOCK_DOWN 0x00000010
227 #define FLAG_CAPSLOCK_IGNORE_NEXT 0x00000020
228 #define FLAG_POWER_KEY_PRESSED 0x00000040
232 static void adbhid_probe(
void);
234 static void adbhid_input_keycode(
int,
int,
int);
236 static void init_trackpad(
int id);
237 static void init_trackball(
int id);
238 static void init_turbomouse(
int id);
239 static void init_microspeed(
int id);
240 static void init_ms_a3(
int id);
242 static struct adb_ids keyboard_ids;
243 static struct adb_ids mouse_ids;
244 static struct adb_ids buttons_ids;
247 #define ADB_KEYBOARD_UNKNOWN 0
248 #define ADB_KEYBOARD_ANSI 0x0100
249 #define ADB_KEYBOARD_ISO 0x0200
250 #define ADB_KEYBOARD_JIS 0x0300
253 #define ADBMOUSE_STANDARD_100 0
254 #define ADBMOUSE_STANDARD_200 1
255 #define ADBMOUSE_EXTENDED 2
256 #define ADBMOUSE_TRACKBALL 3
257 #define ADBMOUSE_TRACKPAD 4
258 #define ADBMOUSE_TURBOMOUSE5 5
259 #define ADBMOUSE_MICROSPEED 6
260 #define ADBMOUSE_TRACKBALLPRO 7
261 #define ADBMOUSE_MS_A3 8
262 #define ADBMOUSE_MACALLY2 9
265 adbhid_keyboard_input(
unsigned char *
data,
int nb,
int apoll)
267 int id = (data[0] >> 4) & 0x0f;
270 printk(
KERN_ERR "ADB HID on ID %d not yet registered, packet %#02x, %#02x, %#02x, %#02x\n",
271 id, data[0], data[1], data[2], data[3]);
278 adbhid_input_keycode(
id, data[1], 0);
279 if (!(data[2] == 0xff || (data[2] == 0x7f && data[1] == 0x7f)))
280 adbhid_input_keycode(
id, data[2], 0);
284 adbhid_input_keycode(
int id,
int scancode,
int repeat)
286 struct adbhid *ahid = adbhid[
id];
289 keycode = scancode & 0x7f;
290 up_flag = scancode & 0x80;
292 if (restore_capslock_events) {
305 }
else if (scancode == 0xff &&
323 "(scancode 0xff).\n");
330 if (!restore_capslock_events) {
333 input_sync(ahid->
input);
335 input_sync(ahid->
input);
339 #ifdef CONFIG_PPC_PMAC
341 switch(pmac_call_feature(PMAC_FTR_GET_MB_INFO,
342 NULL, PMAC_MB_INFO_MODEL, 0)) {
343 case PMAC_TYPE_COMET:
344 case PMAC_TYPE_HOOPER:
345 case PMAC_TYPE_KANGA:
396 input_report_key(adbhid[
id]->
input, key, !up_flag);
397 input_sync(adbhid[
id]->
input);
400 up_flag ?
"released" :
"pressed");
405 adbhid_mouse_input(
unsigned char *
data,
int nb,
int autopoll)
407 int id = (data[0] >> 4) & 0x0f;
461 data[1] = (data[1] & 0x7f) | ((data[1] & data[2]) & 0x80);
462 data[2] = data[2] | 0x80;
465 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
466 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
467 data[3] = (data[3] & 0x77) | ((data[3] & 0x04) << 5)
471 data[1] = (data[1] & 0x7f) | (((data[3] & 0x04) << 5)
472 & ((data[3] & 0x08) << 4));
473 data[2] = (data[2] & 0x7f) | ((data[3] & 0x01) << 7);
474 data[3] = (data[3] & 0x77) | ((data[3] & 0x02) << 6);
477 data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
478 data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
479 data[3] = ((data[3] & 0x04) << 5);
482 data[3] = (data[2] & 0x80) ? 0x80 : 0x00;
488 input_report_key(adbhid[
id]->
input,
BTN_LEFT, !((data[1] >> 7) & 1));
489 input_report_key(adbhid[
id]->
input,
BTN_MIDDLE, !((data[2] >> 7) & 1));
492 input_report_key(adbhid[
id]->
input,
BTN_RIGHT, !((data[3] >> 7) & 1));
495 ((data[2]&0x7f) < 64 ? (data[2]&0x7f) : (data[2]&0x7f)-128 ));
497 ((data[1]&0x7f) < 64 ? (data[1]&0x7f) : (data[1]&0x7f)-128 ));
499 input_sync(adbhid[
id]->
input);
503 adbhid_buttons_input(
unsigned char *data,
int nb,
int autopoll)
505 int id = (data[0] >> 4) & 0x0f;
516 int down = (data[1] == (data[1] & 0xf));
518 switch (data[1] & 0x0f) {
537 data[0], data[1], data[2], data[3]);
545 int down = (data[1] == (data[1] & 0xf));
552 switch (data[1] & 0x0f) {
570 #ifdef CONFIG_PMAC_BACKLIGHT
572 pmac_backlight_key_down();
578 #ifdef CONFIG_PMAC_BACKLIGHT
580 pmac_backlight_key_up();
612 data[0], data[1], data[2], data[3]);
618 data[0], data[1], data[2], data[3]);
625 input_sync(adbhid[
id]->
input);
629 static int leds_pending[16];
630 static int leds_req_pending;
631 static int pending_devs[16];
632 static int pending_led_start;
633 static int pending_led_end;
638 int leds = 0,
device = 0, pending = 0;
643 if (pending_led_start != pending_led_end) {
644 device = pending_devs[pending_led_start];
645 leds = leds_pending[
device] & 0xff;
648 pending_led_start = (pending_led_start < 16) ? pending_led_start : 0;
649 pending = leds_req_pending;
651 leds_req_pending = 0;
652 spin_unlock_irqrestore(&leds_lock, flags);
658 static void real_leds(
unsigned char leds,
int device)
663 if (!leds_req_pending) {
664 leds_req_pending = 1;
665 spin_unlock_irqrestore(&leds_lock, flags);
670 if (!(leds_pending[device] & 0x100)) {
671 pending_devs[pending_led_end] = device;
673 pending_led_end = (pending_led_end < 16) ? pending_led_end : 0;
675 leds_pending[device] = leds | 0x100;
677 spin_unlock_irqrestore(&leds_lock, flags);
684 static int adbhid_kbd_event(
struct input_dev *
dev,
unsigned int type,
unsigned int code,
int value)
686 struct adbhid *adbhid = input_get_drvdata(dev);
694 real_leds(leds, adbhid->
id);
702 adbhid_kbd_capslock_remember(
void)
707 for (i = 1; i < 16; i++) {
717 adb_message_handler(
struct notifier_block *
this,
unsigned long code,
void *
x)
725 for (i = 1; i < 16; i++) {
732 while (leds_req_pending)
740 if (restore_capslock_events)
741 adbhid_kbd_capslock_remember();
757 struct input_dev *input_dev;
766 adbhid[
id] = hid = kzalloc(
sizeof(
struct adbhid),
GFP_KERNEL);
767 input_dev = input_allocate_device();
768 if (!hid || !input_dev) {
773 sprintf(hid->
phys,
"adb%d:%d.%02x/input",
id, default_id, original_handler_id);
775 hid->
input = input_dev;
776 hid->
id = default_id;
781 input_set_drvdata(input_dev, hid);
782 input_dev->name = hid->
name;
783 input_dev->phys = hid->
phys;
784 input_dev->id.bustype =
BUS_ADB;
785 input_dev->id.vendor = 0x0001;
786 input_dev->id.product = (
id << 12) | (default_id << 8) | original_handler_id;
787 input_dev->id.version = 0x0100;
789 switch (default_id) {
799 memcpy(hid->
keycode, adb_to_linux_keycodes,
sizeof(adb_to_linux_keycodes));
802 switch (original_handler_id) {
808 case 0x01:
case 0x02:
case 0x03:
case 0x06:
case 0x08:
809 case 0x0C:
case 0x10:
case 0x18:
case 0x1B:
case 0x1C:
810 case 0xC0:
case 0xC3:
case 0xC6:
815 case 0x04:
case 0x05:
case 0x07:
case 0x09:
case 0x0D:
816 case 0x11:
case 0x14:
case 0x19:
case 0x1D:
case 0xC1:
817 case 0xC4:
case 0xC7:
818 printk(
"ISO, swapping keys.\n");
825 case 0x12:
case 0x15:
case 0x16:
case 0x17:
case 0x1A:
826 case 0x1E:
case 0xC2:
case 0xC5:
case 0xC8:
case 0xC9:
832 for (i = 0; i < 128; i++)
840 input_dev->event = adbhid_kbd_event;
841 input_dev->keycodemax =
KEY_FN;
842 input_dev->keycodesize =
sizeof(hid->
keycode[0]);
855 switch (original_handler_id) {
857 sprintf(hid->
name,
"ADB adjustable keyboard buttons");
886 printk(
KERN_INFO "Trying to register unknown ADB device to input layer.\n");
891 input_dev->keycode = hid->
keycode;
893 err = input_register_device(input_dev);
907 fail: input_free_device(input_dev);
916 static void adbhid_input_unregister(
int id)
918 input_unregister_device(adbhid[
id]->
input);
919 kfree(adbhid[
id]->keycode);
926 adbhid_input_reregister(
int id,
int default_id,
int org_handler_id,
927 int cur_handler_id,
int mk)
930 if (adbhid[
id]->
input->id.product !=
931 ((
id << 12)|(default_id << 8)|org_handler_id)) {
932 adbhid_input_unregister(
id);
933 adbhid_input_register(
id, default_id, org_handler_id,
937 adbhid_input_register(
id, default_id, org_handler_id,
943 adbhid_input_devcleanup(
u16 exist)
947 if (adbhid[i] && !(exist&(1<<i)))
948 adbhid_input_unregister(i);
955 int i, default_id, org_handler_id, cur_handler_id;
962 for (i = 0; i < keyboard_ids.nids; i++) {
963 int id = keyboard_ids.id[
i];
976 printk(
"ADB keyboard at %d, handler set to 5\n",
id);
980 printk(
"ADB keyboard at %d, handler set to 3\n",
id);
982 printk(
"ADB keyboard at %d, handler 1\n",
id);
985 reg |= adbhid_input_reregister(
id, default_id, org_handler_id,
989 for (i = 0; i < buttons_ids.nids; i++) {
990 int id = buttons_ids.id[
i];
993 reg |= adbhid_input_reregister(
id, default_id, org_handler_id,
999 for (i = 0; i < mouse_ids.nids; i++) {
1000 int id = mouse_ids.id[
i];
1006 printk(
"ADB mouse at %d, handler set to 4",
id);
1010 printk(
"ADB mouse at %d, handler set to 0x2F",
id);
1014 printk(
"ADB mouse at %d, handler set to 0x42",
id);
1018 printk(
"ADB mouse at %d, handler set to 0x66",
id);
1022 printk(
"ADB mouse at %d, handler set to 0x5F",
id);
1026 printk(
"ADB mouse at %d, handler set to 3",
id);
1030 printk(
"ADB mouse at %d, handler set to 2",
id);
1034 printk(
"ADB mouse at %d, handler 1",
id);
1040 init_microspeed(
id);
1054 (req.
reply[1] == 0x9a) && ((req.
reply[2] == 0x21)
1055 || (req.
reply[2] == 0x20))) {
1060 (req.
reply[1] == 0x74) && (req.
reply[2] == 0x70) &&
1061 (req.
reply[3] == 0x61) && (req.
reply[4] == 0x64)) {
1066 (req.
reply[1] == 0x4b) && (req.
reply[2] == 0x4d) &&
1067 (req.
reply[3] == 0x4c) && (req.
reply[4] == 0x31)) {
1069 init_turbomouse(
id);
1072 (req.
reply[1] == 0x4b) && (req.
reply[2] == 0x4f) &&
1073 (req.
reply[3] == 0x49) && (req.
reply[4] == 0x54)) {
1075 printk(
"\nADB MacAlly 2-button mouse at %d, handler set to 0x42",
id);
1083 reg |= adbhid_input_reregister(
id, default_id, org_handler_id,
1084 cur_handler_id, mouse_kind);
1086 adbhid_input_devcleanup(reg);
1090 init_trackpad(
int id)
1093 unsigned char r1_buffer[8];
1100 printk(
"bad length for reg. 1\n");
1144 init_trackball(
int id)
1148 printk(
" (trackman/mouseman)");
1176 init_turbomouse(
int id)
1180 printk(
" (TurboMouse 5)");
1212 init_microspeed(
int id)
1216 printk(
" (Microspeed/MacPoint or compatible)");
1256 printk(
" (Mouse Systems A3 Mouse, or compatible)");
1265 static int __init adbhid_init(
void)
1268 if (!machine_is(chrp) && !machine_is(powermac))
1272 led_request.complete = 1;
1277 &adbhid_adb_notifier);
1282 static void __exit adbhid_exit(
void)