Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/macintosh/adbhid.c
0004  *
0005  * ADB HID driver for Power Macintosh computers.
0006  *
0007  * Adapted from drivers/macintosh/mac_keyb.c by Franz Sirl.
0008  * drivers/macintosh/mac_keyb.c was Copyright (C) 1996 Paul Mackerras
0009  * with considerable contributions from Ben Herrenschmidt and others.
0010  *
0011  * Copyright (C) 2000 Franz Sirl.
0012  *
0013  * Adapted to ADB changes and support for more devices by
0014  * Benjamin Herrenschmidt. Adapted from code in MkLinux
0015  * and reworked.
0016  * 
0017  * Supported devices:
0018  *
0019  * - Standard 1 button mouse
0020  * - All standard Apple Extended protocol (handler ID 4)
0021  * - mouseman and trackman mice & trackballs 
0022  * - PowerBook Trackpad (default setup: enable tapping)
0023  * - MicroSpeed mouse & trackball (needs testing)
0024  * - CH Products Trackball Pro (needs testing)
0025  * - Contour Design (Contour Mouse)
0026  * - Hunter digital (NoHandsMouse)
0027  * - Kensignton TurboMouse 5 (needs testing)
0028  * - Mouse Systems A3 mice and trackballs <aidan@kublai.com>
0029  * - MacAlly 2-buttons mouse (needs testing) <pochini@denise.shiny.it>
0030  *
0031  * To do:
0032  *
0033  * Improve Kensington support.
0034  * Split mouse/kbd
0035  * Move to syfs
0036  */
0037 
0038 #include <linux/module.h>
0039 #include <linux/slab.h>
0040 #include <linux/init.h>
0041 #include <linux/notifier.h>
0042 #include <linux/input.h>
0043 
0044 #include <linux/adb.h>
0045 #include <linux/cuda.h>
0046 #include <linux/pmu.h>
0047 
0048 #include <asm/machdep.h>
0049 #ifdef CONFIG_PPC_PMAC
0050 #include <asm/backlight.h>
0051 #include <asm/pmac_feature.h>
0052 #endif
0053 
0054 MODULE_AUTHOR("Franz Sirl <Franz.Sirl-kernel@lauterbach.com>");
0055 
0056 static int restore_capslock_events;
0057 module_param(restore_capslock_events, int, 0644);
0058 MODULE_PARM_DESC(restore_capslock_events,
0059     "Produce keypress events for capslock on both keyup and keydown.");
0060 
0061 #define KEYB_KEYREG 0   /* register # for key up/down data */
0062 #define KEYB_LEDREG 2   /* register # for leds on ADB keyboard */
0063 #define MOUSE_DATAREG   0   /* reg# for movement/button codes from mouse */
0064 
0065 static int adb_message_handler(struct notifier_block *, unsigned long, void *);
0066 static struct notifier_block adbhid_adb_notifier = {
0067     .notifier_call  = adb_message_handler,
0068 };
0069 
0070 /* Some special keys */
0071 #define ADB_KEY_DEL     0x33
0072 #define ADB_KEY_CMD     0x37
0073 #define ADB_KEY_CAPSLOCK    0x39
0074 #define ADB_KEY_FN      0x3f
0075 #define ADB_KEY_FWDEL       0x75
0076 #define ADB_KEY_POWER_OLD   0x7e
0077 #define ADB_KEY_POWER       0x7f
0078 
0079 static const u16 adb_to_linux_keycodes[128] = {
0080     /* 0x00 */ KEY_A,       /*  30 */
0081     /* 0x01 */ KEY_S,       /*  31 */
0082     /* 0x02 */ KEY_D,       /*  32 */
0083     /* 0x03 */ KEY_F,       /*  33 */
0084     /* 0x04 */ KEY_H,       /*  35 */
0085     /* 0x05 */ KEY_G,       /*  34 */
0086     /* 0x06 */ KEY_Z,       /*  44 */
0087     /* 0x07 */ KEY_X,       /*  45 */
0088     /* 0x08 */ KEY_C,       /*  46 */
0089     /* 0x09 */ KEY_V,       /*  47 */
0090     /* 0x0a */ KEY_102ND,       /*  86 */
0091     /* 0x0b */ KEY_B,       /*  48 */
0092     /* 0x0c */ KEY_Q,       /*  16 */
0093     /* 0x0d */ KEY_W,       /*  17 */
0094     /* 0x0e */ KEY_E,       /*  18 */
0095     /* 0x0f */ KEY_R,       /*  19 */
0096     /* 0x10 */ KEY_Y,       /*  21 */
0097     /* 0x11 */ KEY_T,       /*  20 */
0098     /* 0x12 */ KEY_1,       /*   2 */
0099     /* 0x13 */ KEY_2,       /*   3 */
0100     /* 0x14 */ KEY_3,       /*   4 */
0101     /* 0x15 */ KEY_4,       /*   5 */
0102     /* 0x16 */ KEY_6,       /*   7 */
0103     /* 0x17 */ KEY_5,       /*   6 */
0104     /* 0x18 */ KEY_EQUAL,       /*  13 */
0105     /* 0x19 */ KEY_9,       /*  10 */
0106     /* 0x1a */ KEY_7,       /*   8 */
0107     /* 0x1b */ KEY_MINUS,       /*  12 */
0108     /* 0x1c */ KEY_8,       /*   9 */
0109     /* 0x1d */ KEY_0,       /*  11 */
0110     /* 0x1e */ KEY_RIGHTBRACE,  /*  27 */
0111     /* 0x1f */ KEY_O,       /*  24 */
0112     /* 0x20 */ KEY_U,       /*  22 */
0113     /* 0x21 */ KEY_LEFTBRACE,   /*  26 */
0114     /* 0x22 */ KEY_I,       /*  23 */
0115     /* 0x23 */ KEY_P,       /*  25 */
0116     /* 0x24 */ KEY_ENTER,       /*  28 */
0117     /* 0x25 */ KEY_L,       /*  38 */
0118     /* 0x26 */ KEY_J,       /*  36 */
0119     /* 0x27 */ KEY_APOSTROPHE,  /*  40 */
0120     /* 0x28 */ KEY_K,       /*  37 */
0121     /* 0x29 */ KEY_SEMICOLON,   /*  39 */
0122     /* 0x2a */ KEY_BACKSLASH,   /*  43 */
0123     /* 0x2b */ KEY_COMMA,       /*  51 */
0124     /* 0x2c */ KEY_SLASH,       /*  53 */
0125     /* 0x2d */ KEY_N,       /*  49 */
0126     /* 0x2e */ KEY_M,       /*  50 */
0127     /* 0x2f */ KEY_DOT,     /*  52 */
0128     /* 0x30 */ KEY_TAB,     /*  15 */
0129     /* 0x31 */ KEY_SPACE,       /*  57 */
0130     /* 0x32 */ KEY_GRAVE,       /*  41 */
0131     /* 0x33 */ KEY_BACKSPACE,   /*  14 */
0132     /* 0x34 */ KEY_KPENTER,     /*  96 */
0133     /* 0x35 */ KEY_ESC,     /*   1 */
0134     /* 0x36 */ KEY_LEFTCTRL,    /*  29 */
0135     /* 0x37 */ KEY_LEFTMETA,    /* 125 */
0136     /* 0x38 */ KEY_LEFTSHIFT,   /*  42 */
0137     /* 0x39 */ KEY_CAPSLOCK,    /*  58 */
0138     /* 0x3a */ KEY_LEFTALT,     /*  56 */
0139     /* 0x3b */ KEY_LEFT,        /* 105 */
0140     /* 0x3c */ KEY_RIGHT,       /* 106 */
0141     /* 0x3d */ KEY_DOWN,        /* 108 */
0142     /* 0x3e */ KEY_UP,      /* 103 */
0143     /* 0x3f */ KEY_FN,      /* 0x1d0 */
0144     /* 0x40 */ 0,
0145     /* 0x41 */ KEY_KPDOT,       /*  83 */
0146     /* 0x42 */ 0,
0147     /* 0x43 */ KEY_KPASTERISK,  /*  55 */
0148     /* 0x44 */ 0,
0149     /* 0x45 */ KEY_KPPLUS,      /*  78 */
0150     /* 0x46 */ 0,
0151     /* 0x47 */ KEY_NUMLOCK,     /*  69 */
0152     /* 0x48 */ 0,
0153     /* 0x49 */ 0,
0154     /* 0x4a */ 0,
0155     /* 0x4b */ KEY_KPSLASH,     /*  98 */
0156     /* 0x4c */ KEY_KPENTER,     /*  96 */
0157     /* 0x4d */ 0,
0158     /* 0x4e */ KEY_KPMINUS,     /*  74 */
0159     /* 0x4f */ 0,
0160     /* 0x50 */ 0,
0161     /* 0x51 */ KEY_KPEQUAL,     /* 117 */
0162     /* 0x52 */ KEY_KP0,     /*  82 */
0163     /* 0x53 */ KEY_KP1,     /*  79 */
0164     /* 0x54 */ KEY_KP2,     /*  80 */
0165     /* 0x55 */ KEY_KP3,     /*  81 */
0166     /* 0x56 */ KEY_KP4,     /*  75 */
0167     /* 0x57 */ KEY_KP5,     /*  76 */
0168     /* 0x58 */ KEY_KP6,     /*  77 */
0169     /* 0x59 */ KEY_KP7,     /*  71 */
0170     /* 0x5a */ 0,
0171     /* 0x5b */ KEY_KP8,     /*  72 */
0172     /* 0x5c */ KEY_KP9,     /*  73 */
0173     /* 0x5d */ KEY_YEN,     /* 124 */
0174     /* 0x5e */ KEY_RO,      /*  89 */
0175     /* 0x5f */ KEY_KPCOMMA,     /* 121 */
0176     /* 0x60 */ KEY_F5,      /*  63 */
0177     /* 0x61 */ KEY_F6,      /*  64 */
0178     /* 0x62 */ KEY_F7,      /*  65 */
0179     /* 0x63 */ KEY_F3,      /*  61 */
0180     /* 0x64 */ KEY_F8,      /*  66 */
0181     /* 0x65 */ KEY_F9,      /*  67 */
0182     /* 0x66 */ KEY_HANJA,       /* 123 */
0183     /* 0x67 */ KEY_F11,     /*  87 */
0184     /* 0x68 */ KEY_HANGEUL,     /* 122 */
0185     /* 0x69 */ KEY_SYSRQ,       /*  99 */
0186     /* 0x6a */ 0,
0187     /* 0x6b */ KEY_SCROLLLOCK,  /*  70 */
0188     /* 0x6c */ 0,
0189     /* 0x6d */ KEY_F10,     /*  68 */
0190     /* 0x6e */ KEY_COMPOSE,     /* 127 */
0191     /* 0x6f */ KEY_F12,     /*  88 */
0192     /* 0x70 */ 0,
0193     /* 0x71 */ KEY_PAUSE,       /* 119 */
0194     /* 0x72 */ KEY_INSERT,      /* 110 */
0195     /* 0x73 */ KEY_HOME,        /* 102 */
0196     /* 0x74 */ KEY_PAGEUP,      /* 104 */
0197     /* 0x75 */ KEY_DELETE,      /* 111 */
0198     /* 0x76 */ KEY_F4,      /*  62 */
0199     /* 0x77 */ KEY_END,     /* 107 */
0200     /* 0x78 */ KEY_F2,      /*  60 */
0201     /* 0x79 */ KEY_PAGEDOWN,    /* 109 */
0202     /* 0x7a */ KEY_F1,      /*  59 */
0203     /* 0x7b */ KEY_RIGHTSHIFT,  /*  54 */
0204     /* 0x7c */ KEY_RIGHTALT,    /* 100 */
0205     /* 0x7d */ KEY_RIGHTCTRL,   /*  97 */
0206     /* 0x7e */ KEY_RIGHTMETA,   /* 126 */
0207     /* 0x7f */ KEY_POWER,       /* 116 */
0208 };
0209 
0210 struct adbhid {
0211     struct input_dev *input;
0212     int id;
0213     int default_id;
0214     int original_handler_id;
0215     int current_handler_id;
0216     int mouse_kind;
0217     u16 *keycode;
0218     char name[64];
0219     char phys[32];
0220     int flags;
0221 };
0222 
0223 #define FLAG_FN_KEY_PRESSED     0x00000001
0224 #define FLAG_POWER_FROM_FN      0x00000002
0225 #define FLAG_EMU_FWDEL_DOWN     0x00000004
0226 #define FLAG_CAPSLOCK_TRANSLATE     0x00000008
0227 #define FLAG_CAPSLOCK_DOWN      0x00000010
0228 #define FLAG_CAPSLOCK_IGNORE_NEXT   0x00000020
0229 #define FLAG_POWER_KEY_PRESSED      0x00000040
0230 
0231 static struct adbhid *adbhid[16];
0232 
0233 static void adbhid_probe(void);
0234 
0235 static void adbhid_input_keycode(int, int, int);
0236 
0237 static void init_trackpad(int id);
0238 static void init_trackball(int id);
0239 static void init_turbomouse(int id);
0240 static void init_microspeed(int id);
0241 static void init_ms_a3(int id);
0242 
0243 static struct adb_ids keyboard_ids;
0244 static struct adb_ids mouse_ids;
0245 static struct adb_ids buttons_ids;
0246 
0247 /* Kind of keyboard, see Apple technote 1152  */
0248 #define ADB_KEYBOARD_UNKNOWN    0
0249 #define ADB_KEYBOARD_ANSI   0x0100
0250 #define ADB_KEYBOARD_ISO    0x0200
0251 #define ADB_KEYBOARD_JIS    0x0300
0252 
0253 /* Kind of mouse  */
0254 #define ADBMOUSE_STANDARD_100   0   /* Standard 100cpi mouse (handler 1) */
0255 #define ADBMOUSE_STANDARD_200   1   /* Standard 200cpi mouse (handler 2) */
0256 #define ADBMOUSE_EXTENDED   2   /* Apple Extended mouse (handler 4) */
0257 #define ADBMOUSE_TRACKBALL  3   /* TrackBall (handler 4) */
0258 #define ADBMOUSE_TRACKPAD       4   /* Apple's PowerBook trackpad (handler 4) */
0259 #define ADBMOUSE_TURBOMOUSE5    5   /* Turbomouse 5 (previously req. mousehack) */
0260 #define ADBMOUSE_MICROSPEED 6   /* Microspeed mouse (&trackball ?), MacPoint */
0261 #define ADBMOUSE_TRACKBALLPRO   7   /* Trackball Pro (special buttons) */
0262 #define ADBMOUSE_MS_A3      8   /* Mouse systems A3 trackball (handler 3) */
0263 #define ADBMOUSE_MACALLY2   9   /* MacAlly 2-button mouse */
0264 
0265 static void
0266 adbhid_keyboard_input(unsigned char *data, int nb, int apoll)
0267 {
0268     int id = (data[0] >> 4) & 0x0f;
0269 
0270     if (!adbhid[id]) {
0271         pr_err("ADB HID on ID %d not yet registered, packet %#02x, %#02x, %#02x, %#02x\n",
0272                id, data[0], data[1], data[2], data[3]);
0273         return;
0274     }
0275 
0276     /* first check this is from register 0 */
0277     if (nb != 3 || (data[0] & 3) != KEYB_KEYREG)
0278         return;     /* ignore it */
0279     adbhid_input_keycode(id, data[1], 0);
0280     if (!(data[2] == 0xff || (data[2] == 0x7f && data[1] == 0x7f)))
0281         adbhid_input_keycode(id, data[2], 0);
0282 }
0283 
0284 static void
0285 adbhid_input_keycode(int id, int scancode, int repeat)
0286 {
0287     struct adbhid *ahid = adbhid[id];
0288     int keycode, up_flag, key;
0289 
0290     keycode = scancode & 0x7f;
0291     up_flag = scancode & 0x80;
0292 
0293     if (restore_capslock_events) {
0294         if (keycode == ADB_KEY_CAPSLOCK && !up_flag) {
0295             /* Key pressed, turning on the CapsLock LED.
0296              * The next 0xff will be interpreted as a release. */
0297             if (ahid->flags & FLAG_CAPSLOCK_IGNORE_NEXT) {
0298                 /* Throw away this key event if it happens
0299                  * just after resume. */
0300                 ahid->flags &= ~FLAG_CAPSLOCK_IGNORE_NEXT;
0301                 return;
0302             } else {
0303                 ahid->flags |= FLAG_CAPSLOCK_TRANSLATE
0304                     | FLAG_CAPSLOCK_DOWN;
0305             }
0306         } else if (scancode == 0xff &&
0307                !(ahid->flags & FLAG_POWER_KEY_PRESSED)) {
0308             /* Scancode 0xff usually signifies that the capslock
0309              * key was either pressed or released, or that the
0310              * power button was released. */
0311             if (ahid->flags & FLAG_CAPSLOCK_TRANSLATE) {
0312                 keycode = ADB_KEY_CAPSLOCK;
0313                 if (ahid->flags & FLAG_CAPSLOCK_DOWN) {
0314                     /* Key released */
0315                     up_flag = 1;
0316                     ahid->flags &= ~FLAG_CAPSLOCK_DOWN;
0317                 } else {
0318                     /* Key pressed */
0319                     up_flag = 0;
0320                     ahid->flags &= ~FLAG_CAPSLOCK_TRANSLATE;
0321                 }
0322             } else {
0323                 pr_info("Spurious caps lock event (scancode 0xff).\n");
0324             }
0325         }
0326     }
0327 
0328     switch (keycode) {
0329     case ADB_KEY_CAPSLOCK:
0330         if (!restore_capslock_events) {
0331             /* Generate down/up events for CapsLock every time. */
0332             input_report_key(ahid->input, KEY_CAPSLOCK, 1);
0333             input_sync(ahid->input);
0334             input_report_key(ahid->input, KEY_CAPSLOCK, 0);
0335             input_sync(ahid->input);
0336             return;
0337         }
0338         break;
0339 #ifdef CONFIG_PPC_PMAC
0340     case ADB_KEY_POWER_OLD: /* Power key on PBook 3400 needs remapping */
0341         switch(pmac_call_feature(PMAC_FTR_GET_MB_INFO,
0342             NULL, PMAC_MB_INFO_MODEL, 0)) {
0343         case PMAC_TYPE_COMET:
0344         case PMAC_TYPE_HOOPER:
0345         case PMAC_TYPE_KANGA:
0346             keycode = ADB_KEY_POWER;
0347         }
0348         break;
0349     case ADB_KEY_POWER:
0350         /* Keep track of the power key state */
0351         if (up_flag)
0352             ahid->flags &= ~FLAG_POWER_KEY_PRESSED;
0353         else
0354             ahid->flags |= FLAG_POWER_KEY_PRESSED;
0355 
0356         /* Fn + Command will produce a bogus "power" keycode */
0357         if (ahid->flags & FLAG_FN_KEY_PRESSED) {
0358             keycode = ADB_KEY_CMD;
0359             if (up_flag)
0360                 ahid->flags &= ~FLAG_POWER_FROM_FN;
0361             else
0362                 ahid->flags |= FLAG_POWER_FROM_FN;
0363         } else if (ahid->flags & FLAG_POWER_FROM_FN) {
0364             keycode = ADB_KEY_CMD;
0365             ahid->flags &= ~FLAG_POWER_FROM_FN;
0366         }
0367         break;
0368     case ADB_KEY_FN:
0369         /* Keep track of the Fn key state */
0370         if (up_flag) {
0371             ahid->flags &= ~FLAG_FN_KEY_PRESSED;
0372             /* Emulate Fn+delete = forward delete */
0373             if (ahid->flags & FLAG_EMU_FWDEL_DOWN) {
0374                 ahid->flags &= ~FLAG_EMU_FWDEL_DOWN;
0375                 keycode = ADB_KEY_FWDEL;
0376                 break;
0377             }
0378         } else
0379             ahid->flags |= FLAG_FN_KEY_PRESSED;
0380         break;
0381     case ADB_KEY_DEL:
0382         /* Emulate Fn+delete = forward delete */
0383         if (ahid->flags & FLAG_FN_KEY_PRESSED) {
0384             keycode = ADB_KEY_FWDEL;
0385             if (up_flag)
0386                 ahid->flags &= ~FLAG_EMU_FWDEL_DOWN;
0387             else
0388                 ahid->flags |= FLAG_EMU_FWDEL_DOWN;
0389         }
0390         break;
0391 #endif /* CONFIG_PPC_PMAC */
0392     }
0393 
0394     key = adbhid[id]->keycode[keycode];
0395     if (key) {
0396         input_report_key(adbhid[id]->input, key, !up_flag);
0397         input_sync(adbhid[id]->input);
0398     } else
0399         pr_info("Unhandled ADB key (scancode %#02x) %s.\n", keycode,
0400             up_flag ? "released" : "pressed");
0401 
0402 }
0403 
0404 static void
0405 adbhid_mouse_input(unsigned char *data, int nb, int autopoll)
0406 {
0407     int id = (data[0] >> 4) & 0x0f;
0408 
0409     if (!adbhid[id]) {
0410         pr_err("ADB HID on ID %d not yet registered\n", id);
0411         return;
0412     }
0413 
0414   /*
0415     Handler 1 -- 100cpi original Apple mouse protocol.
0416     Handler 2 -- 200cpi original Apple mouse protocol.
0417 
0418     For Apple's standard one-button mouse protocol the data array will
0419     contain the following values:
0420 
0421                 BITS    COMMENTS
0422     data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
0423     data[1] = bxxx xxxx First button and x-axis motion.
0424     data[2] = byyy yyyy Second button and y-axis motion.
0425 
0426     Handler 4 -- Apple Extended mouse protocol.
0427 
0428     For Apple's 3-button mouse protocol the data array will contain the
0429     following values:
0430 
0431         BITS    COMMENTS
0432     data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
0433     data[1] = bxxx xxxx Left button and x-axis motion.
0434     data[2] = byyy yyyy Second button and y-axis motion.
0435     data[3] = byyy bxxx Third button and fourth button.  Y is additional
0436           high bits of y-axis motion.  XY is additional
0437           high bits of x-axis motion.
0438 
0439     MacAlly 2-button mouse protocol.
0440 
0441     For MacAlly 2-button mouse protocol the data array will contain the
0442     following values:
0443 
0444         BITS    COMMENTS
0445     data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
0446     data[1] = bxxx xxxx Left button and x-axis motion.
0447     data[2] = byyy yyyy Right button and y-axis motion.
0448     data[3] = ???? ???? unknown
0449     data[4] = ???? ???? unknown
0450 
0451   */
0452 
0453     /* If it's a trackpad, we alias the second button to the first.
0454        NOTE: Apple sends an ADB flush command to the trackpad when
0455              the first (the real) button is released. We could do
0456          this here using async flush requests.
0457     */
0458     switch (adbhid[id]->mouse_kind)
0459     {
0460         case ADBMOUSE_TRACKPAD:
0461         data[1] = (data[1] & 0x7f) | ((data[1] & data[2]) & 0x80);
0462         data[2] = data[2] | 0x80;
0463         break;
0464         case ADBMOUSE_MICROSPEED:
0465         data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
0466         data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
0467         data[3] = (data[3] & 0x77) | ((data[3] & 0x04) << 5)
0468             | (data[3] & 0x08);
0469         break;
0470         case ADBMOUSE_TRACKBALLPRO:
0471         data[1] = (data[1] & 0x7f) | (((data[3] & 0x04) << 5)
0472             & ((data[3] & 0x08) << 4));
0473         data[2] = (data[2] & 0x7f) | ((data[3] & 0x01) << 7);
0474         data[3] = (data[3] & 0x77) | ((data[3] & 0x02) << 6);
0475         break;
0476         case ADBMOUSE_MS_A3:
0477         data[1] = (data[1] & 0x7f) | ((data[3] & 0x01) << 7);
0478         data[2] = (data[2] & 0x7f) | ((data[3] & 0x02) << 6);
0479         data[3] = ((data[3] & 0x04) << 5);
0480         break;
0481             case ADBMOUSE_MACALLY2:
0482         data[3] = (data[2] & 0x80) ? 0x80 : 0x00;
0483         data[2] |= 0x80;  /* Right button is mapped as button 3 */
0484         nb=4;
0485                 break;
0486     }
0487 
0488     input_report_key(adbhid[id]->input, BTN_LEFT,   !((data[1] >> 7) & 1));
0489     input_report_key(adbhid[id]->input, BTN_MIDDLE, !((data[2] >> 7) & 1));
0490 
0491     if (nb >= 4 && adbhid[id]->mouse_kind != ADBMOUSE_TRACKPAD)
0492         input_report_key(adbhid[id]->input, BTN_RIGHT,  !((data[3] >> 7) & 1));
0493 
0494     input_report_rel(adbhid[id]->input, REL_X,
0495              ((data[2]&0x7f) < 64 ? (data[2]&0x7f) : (data[2]&0x7f)-128 ));
0496     input_report_rel(adbhid[id]->input, REL_Y,
0497              ((data[1]&0x7f) < 64 ? (data[1]&0x7f) : (data[1]&0x7f)-128 ));
0498 
0499     input_sync(adbhid[id]->input);
0500 }
0501 
0502 static void
0503 adbhid_buttons_input(unsigned char *data, int nb, int autopoll)
0504 {
0505     int id = (data[0] >> 4) & 0x0f;
0506 
0507     if (!adbhid[id]) {
0508         pr_err("ADB HID on ID %d not yet registered\n", id);
0509         return;
0510     }
0511 
0512     switch (adbhid[id]->original_handler_id) {
0513     default:
0514     case 0x02: /* Adjustable keyboard button device */
0515       {
0516         int down = (data[1] == (data[1] & 0xf));
0517 
0518         switch (data[1] & 0x0f) {
0519         case 0x0:   /* microphone */
0520             input_report_key(adbhid[id]->input, KEY_SOUND, down);
0521             break;
0522 
0523         case 0x1:   /* mute */
0524             input_report_key(adbhid[id]->input, KEY_MUTE, down);
0525             break;
0526 
0527         case 0x2:   /* volume decrease */
0528             input_report_key(adbhid[id]->input, KEY_VOLUMEDOWN, down);
0529             break;
0530 
0531         case 0x3:   /* volume increase */
0532             input_report_key(adbhid[id]->input, KEY_VOLUMEUP, down);
0533             break;
0534 
0535         default:
0536             pr_info("Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
0537                 data[0], data[1], data[2], data[3]);
0538             break;
0539         }
0540       }
0541       break;
0542 
0543     case 0x1f: /* Powerbook button device */
0544       {
0545         int down = (data[1] == (data[1] & 0xf));
0546 
0547         /*
0548          * XXX: Where is the contrast control for the passive?
0549          *  -- Cort
0550          */
0551 
0552         switch (data[1] & 0x0f) {
0553         case 0x8:   /* mute */
0554             input_report_key(adbhid[id]->input, KEY_MUTE, down);
0555             break;
0556 
0557         case 0x7:   /* volume decrease */
0558             input_report_key(adbhid[id]->input, KEY_VOLUMEDOWN, down);
0559             break;
0560 
0561         case 0x6:   /* volume increase */
0562             input_report_key(adbhid[id]->input, KEY_VOLUMEUP, down);
0563             break;
0564 
0565         case 0xb:   /* eject */
0566             input_report_key(adbhid[id]->input, KEY_EJECTCD, down);
0567             break;
0568 
0569         case 0xa:   /* brightness decrease */
0570 #ifdef CONFIG_PMAC_BACKLIGHT
0571             if (down)
0572                 pmac_backlight_key_down();
0573 #endif
0574             input_report_key(adbhid[id]->input, KEY_BRIGHTNESSDOWN, down);
0575             break;
0576 
0577         case 0x9:   /* brightness increase */
0578 #ifdef CONFIG_PMAC_BACKLIGHT
0579             if (down)
0580                 pmac_backlight_key_up();
0581 #endif
0582             input_report_key(adbhid[id]->input, KEY_BRIGHTNESSUP, down);
0583             break;
0584 
0585         case 0xc:   /* videomode switch */
0586             input_report_key(adbhid[id]->input, KEY_SWITCHVIDEOMODE, down);
0587             break;
0588 
0589         case 0xd:   /* keyboard illumination toggle */
0590             input_report_key(adbhid[id]->input, KEY_KBDILLUMTOGGLE, down);
0591             break;
0592 
0593         case 0xe:   /* keyboard illumination decrease */
0594             input_report_key(adbhid[id]->input, KEY_KBDILLUMDOWN, down);
0595             break;
0596 
0597         case 0xf:
0598             switch (data[1]) {
0599             case 0x8f:
0600             case 0x0f:
0601                 /* keyboard illumination increase */
0602                 input_report_key(adbhid[id]->input, KEY_KBDILLUMUP, down);
0603                 break;
0604 
0605             case 0x7f:
0606             case 0xff:
0607                 /* keypad overlay toogle */
0608                 break;
0609 
0610             default:
0611                 pr_info("Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
0612                     data[0], data[1], data[2], data[3]);
0613                 break;
0614             }
0615             break;
0616         default:
0617             pr_info("Unhandled ADB_MISC event %02x, %02x, %02x, %02x\n",
0618                 data[0], data[1], data[2], data[3]);
0619             break;
0620         }
0621       }
0622       break;
0623     }
0624 
0625     input_sync(adbhid[id]->input);
0626 }
0627 
0628 static struct adb_request led_request;
0629 static int leds_pending[16];
0630 static int leds_req_pending;
0631 static int pending_devs[16];
0632 static int pending_led_start;
0633 static int pending_led_end;
0634 static DEFINE_SPINLOCK(leds_lock);
0635 
0636 static void leds_done(struct adb_request *req)
0637 {
0638     int leds = 0, device = 0, pending = 0;
0639     unsigned long flags;
0640 
0641     spin_lock_irqsave(&leds_lock, flags);
0642 
0643     if (pending_led_start != pending_led_end) {
0644         device = pending_devs[pending_led_start];
0645         leds = leds_pending[device] & 0xff;
0646         leds_pending[device] = 0;
0647         pending_led_start++;
0648         pending_led_start = (pending_led_start < 16) ? pending_led_start : 0;
0649         pending = leds_req_pending;
0650     } else
0651         leds_req_pending = 0;
0652     spin_unlock_irqrestore(&leds_lock, flags);
0653     if (pending)
0654         adb_request(&led_request, leds_done, 0, 3,
0655                 ADB_WRITEREG(device, KEYB_LEDREG), 0xff, ~leds);
0656 }
0657 
0658 static void real_leds(unsigned char leds, int device)
0659 {
0660     unsigned long flags;
0661 
0662     spin_lock_irqsave(&leds_lock, flags);
0663     if (!leds_req_pending) {
0664         leds_req_pending = 1;
0665         spin_unlock_irqrestore(&leds_lock, flags);         
0666         adb_request(&led_request, leds_done, 0, 3,
0667                 ADB_WRITEREG(device, KEYB_LEDREG), 0xff, ~leds);
0668         return;
0669     } else {
0670         if (!(leds_pending[device] & 0x100)) {
0671             pending_devs[pending_led_end] = device;
0672             pending_led_end++;
0673             pending_led_end = (pending_led_end < 16) ? pending_led_end : 0;
0674         }
0675         leds_pending[device] = leds | 0x100;
0676     }
0677     spin_unlock_irqrestore(&leds_lock, flags);         
0678 }
0679 
0680 /*
0681  * Event callback from the input module. Events that change the state of
0682  * the hardware are processed here.
0683  */
0684 static int adbhid_kbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
0685 {
0686     struct adbhid *adbhid = input_get_drvdata(dev);
0687     unsigned char leds;
0688 
0689     switch (type) {
0690     case EV_LED:
0691         leds =  (test_bit(LED_SCROLLL, dev->led) ? 4 : 0) |
0692             (test_bit(LED_NUML,    dev->led) ? 1 : 0) |
0693             (test_bit(LED_CAPSL,   dev->led) ? 2 : 0);
0694         real_leds(leds, adbhid->id);
0695         return 0;
0696     }
0697 
0698     return -1;
0699 }
0700 
0701 static void
0702 adbhid_kbd_capslock_remember(void)
0703 {
0704     struct adbhid *ahid;
0705     int i;
0706 
0707     for (i = 1; i < 16; i++) {
0708         ahid = adbhid[i];
0709 
0710         if (ahid && ahid->id == ADB_KEYBOARD)
0711             if (ahid->flags & FLAG_CAPSLOCK_TRANSLATE)
0712                 ahid->flags |= FLAG_CAPSLOCK_IGNORE_NEXT;
0713     }
0714 }
0715 
0716 static int
0717 adb_message_handler(struct notifier_block *this, unsigned long code, void *x)
0718 {
0719     switch (code) {
0720     case ADB_MSG_PRE_RESET:
0721     case ADB_MSG_POWERDOWN:
0722         /* Stop the repeat timer. Autopoll is already off at this point */
0723         {
0724             int i;
0725             for (i = 1; i < 16; i++) {
0726                 if (adbhid[i])
0727                     del_timer_sync(&adbhid[i]->input->timer);
0728             }
0729         }
0730 
0731         /* Stop pending led requests */
0732         while (leds_req_pending)
0733             adb_poll();
0734 
0735         /* After resume, and if the capslock LED is on, the PMU will
0736          * send a "capslock down" key event. This confuses the
0737          * restore_capslock_events logic. Remember if the capslock
0738          * LED was on before suspend so the unwanted key event can
0739          * be ignored after resume. */
0740         if (restore_capslock_events)
0741             adbhid_kbd_capslock_remember();
0742 
0743         break;
0744 
0745     case ADB_MSG_POST_RESET:
0746         adbhid_probe();
0747         break;
0748     }
0749     return NOTIFY_DONE;
0750 }
0751 
0752 static int
0753 adbhid_input_register(int id, int default_id, int original_handler_id,
0754               int current_handler_id, int mouse_kind)
0755 {
0756     struct adbhid *hid;
0757     struct input_dev *input_dev;
0758     int err;
0759     int i;
0760     char *keyboard_type;
0761 
0762     if (adbhid[id]) {
0763         pr_err("Trying to reregister ADB HID on ID %d\n", id);
0764         return -EEXIST;
0765     }
0766 
0767     adbhid[id] = hid = kzalloc(sizeof(struct adbhid), GFP_KERNEL);
0768     input_dev = input_allocate_device();
0769     if (!hid || !input_dev) {
0770         err = -ENOMEM;
0771         goto fail;
0772     }
0773 
0774     sprintf(hid->phys, "adb%d:%d.%02x/input", id, default_id, original_handler_id);
0775 
0776     hid->input = input_dev;
0777     hid->id = default_id;
0778     hid->original_handler_id = original_handler_id;
0779     hid->current_handler_id = current_handler_id;
0780     hid->mouse_kind = mouse_kind;
0781     hid->flags = 0;
0782     input_set_drvdata(input_dev, hid);
0783     input_dev->name = hid->name;
0784     input_dev->phys = hid->phys;
0785     input_dev->id.bustype = BUS_ADB;
0786     input_dev->id.vendor = 0x0001;
0787     input_dev->id.product = (id << 12) | (default_id << 8) | original_handler_id;
0788     input_dev->id.version = 0x0100;
0789 
0790     switch (default_id) {
0791     case ADB_KEYBOARD:
0792         hid->keycode = kmemdup(adb_to_linux_keycodes,
0793                        sizeof(adb_to_linux_keycodes), GFP_KERNEL);
0794         if (!hid->keycode) {
0795             err = -ENOMEM;
0796             goto fail;
0797         }
0798 
0799         sprintf(hid->name, "ADB keyboard");
0800 
0801         switch (original_handler_id) {
0802         default:
0803             keyboard_type = "<unknown>";
0804             input_dev->id.version = ADB_KEYBOARD_UNKNOWN;
0805             break;
0806 
0807         case 0x01: case 0x02: case 0x03: case 0x06: case 0x08:
0808         case 0x0C: case 0x10: case 0x18: case 0x1B: case 0x1C:
0809         case 0xC0: case 0xC3: case 0xC6:
0810             keyboard_type = "ANSI";
0811             input_dev->id.version = ADB_KEYBOARD_ANSI;
0812             break;
0813 
0814         case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
0815         case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
0816         case 0xC4: case 0xC7:
0817             keyboard_type = "ISO, swapping keys";
0818             input_dev->id.version = ADB_KEYBOARD_ISO;
0819             swap(hid->keycode[10], hid->keycode[50]);
0820             break;
0821 
0822         case 0x12: case 0x15: case 0x16: case 0x17: case 0x1A:
0823         case 0x1E: case 0xC2: case 0xC5: case 0xC8: case 0xC9:
0824             keyboard_type = "JIS";
0825             input_dev->id.version = ADB_KEYBOARD_JIS;
0826             break;
0827         }
0828         pr_info("Detected ADB keyboard, type %s.\n", keyboard_type);
0829 
0830         for (i = 0; i < 128; i++)
0831             if (hid->keycode[i])
0832                 set_bit(hid->keycode[i], input_dev->keybit);
0833 
0834         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
0835             BIT_MASK(EV_REP);
0836         input_dev->ledbit[0] = BIT_MASK(LED_SCROLLL) |
0837             BIT_MASK(LED_CAPSL) | BIT_MASK(LED_NUML);
0838         input_dev->event = adbhid_kbd_event;
0839         input_dev->keycodemax = KEY_FN;
0840         input_dev->keycodesize = sizeof(hid->keycode[0]);
0841         break;
0842 
0843     case ADB_MOUSE:
0844         sprintf(hid->name, "ADB mouse");
0845 
0846         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
0847         input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
0848             BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
0849         input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
0850         break;
0851 
0852     case ADB_MISC:
0853         switch (original_handler_id) {
0854         case 0x02: /* Adjustable keyboard button device */
0855             sprintf(hid->name, "ADB adjustable keyboard buttons");
0856             input_dev->evbit[0] = BIT_MASK(EV_KEY) |
0857                 BIT_MASK(EV_REP);
0858             set_bit(KEY_SOUND, input_dev->keybit);
0859             set_bit(KEY_MUTE, input_dev->keybit);
0860             set_bit(KEY_VOLUMEUP, input_dev->keybit);
0861             set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
0862             break;
0863         case 0x1f: /* Powerbook button device */
0864             sprintf(hid->name, "ADB Powerbook buttons");
0865             input_dev->evbit[0] = BIT_MASK(EV_KEY) |
0866                 BIT_MASK(EV_REP);
0867             set_bit(KEY_MUTE, input_dev->keybit);
0868             set_bit(KEY_VOLUMEUP, input_dev->keybit);
0869             set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
0870             set_bit(KEY_BRIGHTNESSUP, input_dev->keybit);
0871             set_bit(KEY_BRIGHTNESSDOWN, input_dev->keybit);
0872             set_bit(KEY_EJECTCD, input_dev->keybit);
0873             set_bit(KEY_SWITCHVIDEOMODE, input_dev->keybit);
0874             set_bit(KEY_KBDILLUMTOGGLE, input_dev->keybit);
0875             set_bit(KEY_KBDILLUMDOWN, input_dev->keybit);
0876             set_bit(KEY_KBDILLUMUP, input_dev->keybit);
0877             break;
0878         }
0879         if (hid->name[0])
0880             break;
0881         fallthrough;
0882 
0883     default:
0884         pr_info("Trying to register unknown ADB device to input layer.\n");
0885         err = -ENODEV;
0886         goto fail;
0887     }
0888 
0889     input_dev->keycode = hid->keycode;
0890 
0891     err = input_register_device(input_dev);
0892     if (err)
0893         goto fail;
0894 
0895     if (default_id == ADB_KEYBOARD) {
0896         /* HACK WARNING!! This should go away as soon there is an utility
0897          * to control that for event devices.
0898          */
0899         input_dev->rep[REP_DELAY] = 500;   /* input layer default: 250 */
0900         input_dev->rep[REP_PERIOD] = 66; /* input layer default: 33 */
0901     }
0902 
0903     return 0;
0904 
0905  fail:  input_free_device(input_dev);
0906     if (hid) {
0907         kfree(hid->keycode);
0908         kfree(hid);
0909     }
0910     adbhid[id] = NULL;
0911     return err;
0912 }
0913 
0914 static void adbhid_input_unregister(int id)
0915 {
0916     input_unregister_device(adbhid[id]->input);
0917     kfree(adbhid[id]->keycode);
0918     kfree(adbhid[id]);
0919     adbhid[id] = NULL;
0920 }
0921 
0922 
0923 static u16
0924 adbhid_input_reregister(int id, int default_id, int org_handler_id,
0925             int cur_handler_id, int mk)
0926 {
0927     if (adbhid[id]) {
0928         if (adbhid[id]->input->id.product !=
0929             ((id << 12)|(default_id << 8)|org_handler_id)) {
0930             adbhid_input_unregister(id);
0931             adbhid_input_register(id, default_id, org_handler_id,
0932                           cur_handler_id, mk);
0933         }
0934     } else
0935         adbhid_input_register(id, default_id, org_handler_id,
0936                       cur_handler_id, mk);
0937     return 1<<id;
0938 }
0939 
0940 static void
0941 adbhid_input_devcleanup(u16 exist)
0942 {
0943     int i;
0944     for(i=1; i<16; i++)
0945         if (adbhid[i] && !(exist&(1<<i)))
0946             adbhid_input_unregister(i);
0947 }
0948 
0949 static void
0950 adbhid_probe(void)
0951 {
0952     struct adb_request req;
0953     int i, default_id, org_handler_id, cur_handler_id;
0954     u16 reg = 0;
0955 
0956     adb_register(ADB_MOUSE, 0, &mouse_ids, adbhid_mouse_input);
0957     adb_register(ADB_KEYBOARD, 0, &keyboard_ids, adbhid_keyboard_input);
0958     adb_register(ADB_MISC, 0, &buttons_ids, adbhid_buttons_input);
0959 
0960     for (i = 0; i < keyboard_ids.nids; i++) {
0961         int id = keyboard_ids.id[i];
0962 
0963         adb_get_infos(id, &default_id, &org_handler_id);
0964 
0965         /* turn off all leds */
0966         adb_request(&req, NULL, ADBREQ_SYNC, 3,
0967                 ADB_WRITEREG(id, KEYB_LEDREG), 0xff, 0xff);
0968 
0969         /* Enable full feature set of the keyboard
0970            ->get it to send separate codes for left and right shift,
0971            control, option keys */
0972 #if 0       /* handler 5 doesn't send separate codes for R modifiers */
0973         if (!adb_try_handler_change(id, 5))
0974 #endif
0975         adb_try_handler_change(id, 3);
0976 
0977         adb_get_infos(id, &default_id, &cur_handler_id);
0978         printk(KERN_DEBUG "ADB keyboard at %d has handler 0x%X\n",
0979                id, cur_handler_id);
0980         reg |= adbhid_input_reregister(id, default_id, org_handler_id,
0981                            cur_handler_id, 0);
0982     }
0983 
0984     for (i = 0; i < buttons_ids.nids; i++) {
0985         int id = buttons_ids.id[i];
0986 
0987         adb_get_infos(id, &default_id, &org_handler_id);
0988         reg |= adbhid_input_reregister(id, default_id, org_handler_id,
0989                            org_handler_id, 0);
0990     }
0991 
0992     /* Try to switch all mice to handler 4, or 2 for three-button
0993        mode and full resolution. */
0994     for (i = 0; i < mouse_ids.nids; i++) {
0995         int id = mouse_ids.id[i];
0996         int mouse_kind;
0997         char *desc = "standard";
0998 
0999         adb_get_infos(id, &default_id, &org_handler_id);
1000 
1001         if (adb_try_handler_change(id, 4)) {
1002             mouse_kind = ADBMOUSE_EXTENDED;
1003         }
1004         else if (adb_try_handler_change(id, 0x2F)) {
1005             mouse_kind = ADBMOUSE_MICROSPEED;
1006         }
1007         else if (adb_try_handler_change(id, 0x42)) {
1008             mouse_kind = ADBMOUSE_TRACKBALLPRO;
1009         }
1010         else if (adb_try_handler_change(id, 0x66)) {
1011             mouse_kind = ADBMOUSE_MICROSPEED;
1012         }
1013         else if (adb_try_handler_change(id, 0x5F)) {
1014             mouse_kind = ADBMOUSE_MICROSPEED;
1015         }
1016         else if (adb_try_handler_change(id, 3)) {
1017             mouse_kind = ADBMOUSE_MS_A3;
1018         }
1019         else if (adb_try_handler_change(id, 2)) {
1020             mouse_kind = ADBMOUSE_STANDARD_200;
1021         }
1022         else {
1023             mouse_kind = ADBMOUSE_STANDARD_100;
1024         }
1025 
1026         if ((mouse_kind == ADBMOUSE_TRACKBALLPRO)
1027             || (mouse_kind == ADBMOUSE_MICROSPEED)) {
1028             desc = "Microspeed/MacPoint or compatible";
1029             init_microspeed(id);
1030         } else if (mouse_kind == ADBMOUSE_MS_A3) {
1031             desc = "Mouse Systems A3 Mouse or compatible";
1032             init_ms_a3(id);
1033         } else if (mouse_kind ==  ADBMOUSE_EXTENDED) {
1034             desc = "extended";
1035             /*
1036              * Register 1 is usually used for device
1037              * identification.  Here, we try to identify
1038              * a known device and call the appropriate
1039              * init function.
1040              */
1041             adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
1042                     ADB_READREG(id, 1));
1043 
1044             if ((req.reply_len) &&
1045                 (req.reply[1] == 0x9a) && ((req.reply[2] == 0x21)
1046                     || (req.reply[2] == 0x20))) {
1047                 mouse_kind = ADBMOUSE_TRACKBALL;
1048                 desc = "trackman/mouseman";
1049                 init_trackball(id);
1050             }
1051             else if ((req.reply_len >= 4) &&
1052                 (req.reply[1] == 0x74) && (req.reply[2] == 0x70) &&
1053                 (req.reply[3] == 0x61) && (req.reply[4] == 0x64)) {
1054                 mouse_kind = ADBMOUSE_TRACKPAD;
1055                 desc = "trackpad";
1056                 init_trackpad(id);
1057             }
1058             else if ((req.reply_len >= 4) &&
1059                 (req.reply[1] == 0x4b) && (req.reply[2] == 0x4d) &&
1060                 (req.reply[3] == 0x4c) && (req.reply[4] == 0x31)) {
1061                 mouse_kind = ADBMOUSE_TURBOMOUSE5;
1062                 desc = "TurboMouse 5";
1063                 init_turbomouse(id);
1064             }
1065             else if ((req.reply_len == 9) &&
1066                 (req.reply[1] == 0x4b) && (req.reply[2] == 0x4f) &&
1067                 (req.reply[3] == 0x49) && (req.reply[4] == 0x54)) {
1068                 if (adb_try_handler_change(id, 0x42)) {
1069                     mouse_kind = ADBMOUSE_MACALLY2;
1070                     desc = "MacAlly 2-button";
1071                 }
1072             }
1073         }
1074 
1075         adb_get_infos(id, &default_id, &cur_handler_id);
1076         printk(KERN_DEBUG "ADB mouse (%s) at %d has handler 0x%X\n",
1077                desc, id, cur_handler_id);
1078         reg |= adbhid_input_reregister(id, default_id, org_handler_id,
1079                            cur_handler_id, mouse_kind);
1080     }
1081     adbhid_input_devcleanup(reg);
1082 }
1083 
1084 static void 
1085 init_trackpad(int id)
1086 {
1087     struct adb_request req;
1088     unsigned char r1_buffer[8];
1089 
1090     adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
1091             ADB_READREG(id,1));
1092     if (req.reply_len < 8)
1093         pr_err("%s: bad length for reg. 1\n", __func__);
1094     else
1095     {
1096         memcpy(r1_buffer, &req.reply[1], 8);
1097 
1098         adb_request(&req, NULL, ADBREQ_SYNC, 9,
1099             ADB_WRITEREG(id,1),
1100                 r1_buffer[0],
1101                 r1_buffer[1],
1102                 r1_buffer[2],
1103                 r1_buffer[3],
1104                 r1_buffer[4],
1105                 r1_buffer[5],
1106                 0x0d,
1107                 r1_buffer[7]);
1108 
1109             adb_request(&req, NULL, ADBREQ_SYNC, 9,
1110             ADB_WRITEREG(id,2),
1111                 0x99,
1112                 0x94,
1113                 0x19,
1114                 0xff,
1115                 0xb2,
1116                 0x8a,
1117                 0x1b,
1118                 0x50);
1119 
1120         adb_request(&req, NULL, ADBREQ_SYNC, 9,
1121             ADB_WRITEREG(id,1),
1122                 r1_buffer[0],
1123                 r1_buffer[1],
1124                 r1_buffer[2],
1125                 r1_buffer[3],
1126                 r1_buffer[4],
1127                 r1_buffer[5],
1128                 0x03, /*r1_buffer[6],*/
1129                 r1_buffer[7]);
1130 
1131         /* Without this flush, the trackpad may be locked up */
1132         adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1133         }
1134 }
1135 
1136 static void 
1137 init_trackball(int id)
1138 {
1139     struct adb_request req;
1140 
1141     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1142     ADB_WRITEREG(id,1), 00,0x81);
1143 
1144     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1145     ADB_WRITEREG(id,1), 01,0x81);
1146 
1147     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1148     ADB_WRITEREG(id,1), 02,0x81);
1149 
1150     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1151     ADB_WRITEREG(id,1), 03,0x38);
1152 
1153     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1154     ADB_WRITEREG(id,1), 00,0x81);
1155 
1156     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1157     ADB_WRITEREG(id,1), 01,0x81);
1158 
1159     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1160     ADB_WRITEREG(id,1), 02,0x81);
1161 
1162     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1163     ADB_WRITEREG(id,1), 03,0x38);
1164 }
1165 
1166 static void
1167 init_turbomouse(int id)
1168 {
1169     struct adb_request req;
1170 
1171     adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1172 
1173     adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
1174 
1175     adb_request(&req, NULL, ADBREQ_SYNC, 9,
1176     ADB_WRITEREG(3,2),
1177         0xe7,
1178         0x8c,
1179         0,
1180         0,
1181         0,
1182         0xff,
1183         0xff,
1184         0x94);
1185 
1186     adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(3));
1187 
1188     adb_request(&req, NULL, ADBREQ_SYNC, 9,
1189     ADB_WRITEREG(3,2),
1190         0xa5,
1191         0x14,
1192         0,
1193         0,
1194         0x69,
1195         0xff,
1196         0xff,
1197         0x27);
1198 }
1199 
1200 static void
1201 init_microspeed(int id)
1202 {
1203     struct adb_request req;
1204 
1205     adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1206 
1207     /* This will initialize mice using the Microspeed, MacPoint and
1208        other compatible firmware. Bit 12 enables extended protocol.
1209        
1210        Register 1 Listen (4 Bytes)
1211             0 -  3     Button is mouse (set also for double clicking!!!)
1212             4 -  7     Button is locking (affects change speed also)
1213             8 - 11     Button changes speed
1214            12          1 = Extended mouse mode, 0 = normal mouse mode
1215            13 - 15     unused 0
1216            16 - 23     normal speed
1217            24 - 31     changed speed
1218 
1219        Register 1 talk holds version and product identification information.
1220        Register 1 Talk (4 Bytes):
1221             0 -  7     Product code
1222             8 - 23     undefined, reserved
1223            24 - 31     Version number
1224         
1225        Speed 0 is max. 1 to 255 set speed in increments of 1/256 of max.
1226  */
1227     adb_request(&req, NULL, ADBREQ_SYNC, 5,
1228     ADB_WRITEREG(id,1),
1229         0x20,   /* alt speed = 0x20 (rather slow) */
1230         0x00,   /* norm speed = 0x00 (fastest) */
1231         0x10,   /* extended protocol, no speed change */
1232         0x07);  /* all buttons enabled as mouse buttons, no locking */
1233 
1234 
1235     adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1236 }
1237 
1238 static void
1239 init_ms_a3(int id)
1240 {
1241     struct adb_request req;
1242 
1243     adb_request(&req, NULL, ADBREQ_SYNC, 3,
1244     ADB_WRITEREG(id, 0x2),
1245         0x00,
1246         0x07);
1247  
1248     adb_request(&req, NULL, ADBREQ_SYNC, 1, ADB_FLUSH(id));
1249 }
1250 
1251 static int __init adbhid_init(void)
1252 {
1253 #ifndef CONFIG_MAC
1254     if (!machine_is(chrp) && !machine_is(powermac))
1255         return 0;
1256 #endif
1257 
1258     led_request.complete = 1;
1259 
1260     adbhid_probe();
1261 
1262     blocking_notifier_chain_register(&adb_client_list,
1263             &adbhid_adb_notifier);
1264 
1265     return 0;
1266 }
1267 
1268 static void __exit adbhid_exit(void)
1269 {
1270 }
1271  
1272 module_init(adbhid_init);
1273 module_exit(adbhid_exit);