Merge gregkh@master.kernel.org:/home/rmk/linux-2.6-arm
[pandora-kernel.git] / drivers / input / mouse / logips2pp.c
1 /*
2  * Logitech PS/2++ mouse driver
3  *
4  * Copyright (c) 1999-2003 Vojtech Pavlik <vojtech@suse.cz>
5  * Copyright (c) 2003 Eric Wong <eric@yhbt.net>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  */
11
12 #include <linux/input.h>
13 #include <linux/serio.h>
14 #include <linux/libps2.h>
15 #include "psmouse.h"
16 #include "logips2pp.h"
17
18 /* Logitech mouse types */
19 #define PS2PP_KIND_WHEEL        1
20 #define PS2PP_KIND_MX           2
21 #define PS2PP_KIND_TP3          3
22 #define PS2PP_KIND_TRACKMAN     4
23
24 /* Logitech mouse features */
25 #define PS2PP_WHEEL             0x01
26 #define PS2PP_HWHEEL            0x02
27 #define PS2PP_SIDE_BTN          0x04
28 #define PS2PP_EXTRA_BTN         0x08
29 #define PS2PP_TASK_BTN          0x10
30 #define PS2PP_NAV_BTN           0x20
31
32 struct ps2pp_info {
33         const int model;
34         unsigned const int kind;
35         unsigned const int features;
36 };
37
38 /*
39  * Process a PS2++ or PS2T++ packet.
40  */
41
42 static psmouse_ret_t ps2pp_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
43 {
44         struct input_dev *dev = psmouse->dev;
45         unsigned char *packet = psmouse->packet;
46
47         if (psmouse->pktcnt < 3)
48                 return PSMOUSE_GOOD_DATA;
49
50 /*
51  * Full packet accumulated, process it
52  */
53
54         input_regs(dev, regs);
55
56         if ((packet[0] & 0x48) == 0x48 && (packet[1] & 0x02) == 0x02) {
57
58                 /* Logitech extended packet */
59                 switch ((packet[1] >> 4) | (packet[0] & 0x30)) {
60
61                         case 0x0d: /* Mouse extra info */
62
63                                 input_report_rel(dev, packet[2] & 0x80 ? REL_HWHEEL : REL_WHEEL,
64                                         (int) (packet[2] & 8) - (int) (packet[2] & 7));
65                                 input_report_key(dev, BTN_SIDE, (packet[2] >> 4) & 1);
66                                 input_report_key(dev, BTN_EXTRA, (packet[2] >> 5) & 1);
67
68                                 break;
69
70                         case 0x0e: /* buttons 4, 5, 6, 7, 8, 9, 10 info */
71
72                                 input_report_key(dev, BTN_SIDE, (packet[2]) & 1);
73                                 input_report_key(dev, BTN_EXTRA, (packet[2] >> 1) & 1);
74                                 input_report_key(dev, BTN_BACK, (packet[2] >> 3) & 1);
75                                 input_report_key(dev, BTN_FORWARD, (packet[2] >> 4) & 1);
76                                 input_report_key(dev, BTN_TASK, (packet[2] >> 2) & 1);
77
78                                 break;
79
80                         case 0x0f: /* TouchPad extra info */
81
82                                 input_report_rel(dev, packet[2] & 0x08 ? REL_HWHEEL : REL_WHEEL,
83                                         (int) ((packet[2] >> 4) & 8) - (int) ((packet[2] >> 4) & 7));
84                                 packet[0] = packet[2] | 0x08;
85                                 break;
86
87 #ifdef DEBUG
88                         default:
89                                 printk(KERN_WARNING "psmouse.c: Received PS2++ packet #%x, but don't know how to handle.\n",
90                                         (packet[1] >> 4) | (packet[0] & 0x30));
91 #endif
92                 }
93         } else {
94                 /* Standard PS/2 motion data */
95                 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
96                 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
97         }
98
99         input_report_key(dev, BTN_LEFT,    packet[0]       & 1);
100         input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
101         input_report_key(dev, BTN_RIGHT,  (packet[0] >> 1) & 1);
102
103         input_sync(dev);
104
105         return PSMOUSE_FULL_PACKET;
106
107 }
108
109 /*
110  * ps2pp_cmd() sends a PS2++ command, sliced into two bit
111  * pieces through the SETRES command. This is needed to send extended
112  * commands to mice on notebooks that try to understand the PS/2 protocol
113  * Ugly.
114  */
115
116 static int ps2pp_cmd(struct psmouse *psmouse, unsigned char *param, unsigned char command)
117 {
118         if (psmouse_sliced_command(psmouse, command))
119                 return -1;
120
121         if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_POLL | 0x0300))
122                 return -1;
123
124         return 0;
125 }
126
127 /*
128  * SmartScroll / CruiseControl for some newer Logitech mice Defaults to
129  * enabled if we do nothing to it. Of course I put this in because I want it
130  * disabled :P
131  * 1 - enabled (if previously disabled, also default)
132  * 0 - disabled
133  */
134
135 static void ps2pp_set_smartscroll(struct psmouse *psmouse, unsigned int smartscroll)
136 {
137         struct ps2dev *ps2dev = &psmouse->ps2dev;
138         unsigned char param[4];
139
140         if (smartscroll > 1)
141                 smartscroll = 1;
142
143         ps2pp_cmd(psmouse, param, 0x32);
144
145         param[0] = 0;
146         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
147         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
148         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
149
150         param[0] = smartscroll;
151         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
152 }
153
154 static ssize_t ps2pp_attr_show_smartscroll(struct psmouse *psmouse, void *data, char *buf)
155 {
156         return sprintf(buf, "%d\n", psmouse->smartscroll ? 1 : 0);
157 }
158
159 static ssize_t ps2pp_attr_set_smartscroll(struct psmouse *psmouse, void *data, const char *buf, size_t count)
160 {
161         unsigned long value;
162         char *rest;
163
164         value = simple_strtoul(buf, &rest, 10);
165         if (*rest || value > 1)
166                 return -EINVAL;
167
168         ps2pp_set_smartscroll(psmouse, value);
169         psmouse->smartscroll = value;
170         return count;
171 }
172
173 PSMOUSE_DEFINE_ATTR(smartscroll, S_IWUSR | S_IRUGO, NULL,
174                         ps2pp_attr_show_smartscroll, ps2pp_attr_set_smartscroll);
175
176 /*
177  * Support 800 dpi resolution _only_ if the user wants it (there are good
178  * reasons to not use it even if the mouse supports it, and of course there are
179  * also good reasons to use it, let the user decide).
180  */
181
182 static void ps2pp_set_resolution(struct psmouse *psmouse, unsigned int resolution)
183 {
184         if (resolution > 400) {
185                 struct ps2dev *ps2dev = &psmouse->ps2dev;
186                 unsigned char param = 3;
187
188                 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
189                 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
190                 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
191                 ps2_command(ps2dev, &param, PSMOUSE_CMD_SETRES);
192                 psmouse->resolution = 800;
193         } else
194                 psmouse_set_resolution(psmouse, resolution);
195 }
196
197 static void ps2pp_disconnect(struct psmouse *psmouse)
198 {
199         device_remove_file(&psmouse->ps2dev.serio->dev, &psmouse_attr_smartscroll.dattr);
200 }
201
202 static struct ps2pp_info *get_model_info(unsigned char model)
203 {
204         static struct ps2pp_info ps2pp_list[] = {
205                 { 12,   0,                      PS2PP_SIDE_BTN},
206                 { 13,   0,                      0 },
207                 { 15,   PS2PP_KIND_MX,                                  /* MX1000 */
208                                 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
209                                 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN | PS2PP_HWHEEL },
210                 { 40,   0,                      PS2PP_SIDE_BTN },
211                 { 41,   0,                      PS2PP_SIDE_BTN },
212                 { 42,   0,                      PS2PP_SIDE_BTN },
213                 { 43,   0,                      PS2PP_SIDE_BTN },
214                 { 50,   0,                      0 },
215                 { 51,   0,                      0 },
216                 { 52,   PS2PP_KIND_WHEEL,       PS2PP_SIDE_BTN | PS2PP_WHEEL },
217                 { 53,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
218                 { 61,   PS2PP_KIND_MX,                                  /* MX700 */
219                                 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
220                                 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
221                 { 66,   PS2PP_KIND_MX,                                  /* MX3100 reciver */
222                                 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
223                                 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN | PS2PP_HWHEEL },
224                 { 73,   0,                      PS2PP_SIDE_BTN },
225                 { 75,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
226                 { 76,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
227                 { 79,   PS2PP_KIND_TRACKMAN,    PS2PP_WHEEL },          /* TrackMan with wheel */
228                 { 80,   PS2PP_KIND_WHEEL,       PS2PP_SIDE_BTN | PS2PP_WHEEL },
229                 { 81,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
230                 { 83,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
231                 { 85,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
232                 { 86,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
233                 { 87,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
234                 { 88,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
235                 { 96,   0,                      0 },
236                 { 97,   PS2PP_KIND_TP3,         PS2PP_WHEEL | PS2PP_HWHEEL },
237                 { 99,   PS2PP_KIND_WHEEL,       PS2PP_WHEEL },
238                 { 100,  PS2PP_KIND_MX,                                  /* MX510 */
239                                 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
240                                 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
241                 { 111,  PS2PP_KIND_MX,  PS2PP_WHEEL | PS2PP_SIDE_BTN }, /* MX300 reports task button as side */
242                 { 112,  PS2PP_KIND_MX,                                  /* MX500 */
243                                 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
244                                 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
245                 { 114,  PS2PP_KIND_MX,                                  /* MX310 */
246                                 PS2PP_WHEEL | PS2PP_SIDE_BTN |
247                                 PS2PP_TASK_BTN | PS2PP_EXTRA_BTN },
248                 { }
249         };
250         int i;
251
252         for (i = 0; ps2pp_list[i].model; i++)
253                 if (model == ps2pp_list[i].model)
254                         return &ps2pp_list[i];
255
256         printk(KERN_WARNING "logips2pp: Detected unknown logitech mouse model %d\n", model);
257         return NULL;
258 }
259
260 /*
261  * Set up input device's properties based on the detected mouse model.
262  */
263
264 static void ps2pp_set_model_properties(struct psmouse *psmouse, struct ps2pp_info *model_info,
265                                        int using_ps2pp)
266 {
267         struct input_dev *input_dev = psmouse->dev;
268
269         if (model_info->features & PS2PP_SIDE_BTN)
270                 set_bit(BTN_SIDE, input_dev->keybit);
271
272         if (model_info->features & PS2PP_EXTRA_BTN)
273                 set_bit(BTN_EXTRA, input_dev->keybit);
274
275         if (model_info->features & PS2PP_TASK_BTN)
276                 set_bit(BTN_TASK, input_dev->keybit);
277
278         if (model_info->features & PS2PP_NAV_BTN) {
279                 set_bit(BTN_FORWARD, input_dev->keybit);
280                 set_bit(BTN_BACK, input_dev->keybit);
281         }
282
283         if (model_info->features & PS2PP_WHEEL)
284                 set_bit(REL_WHEEL, input_dev->relbit);
285
286         if (model_info->features & PS2PP_HWHEEL)
287                 set_bit(REL_HWHEEL, input_dev->relbit);
288
289         switch (model_info->kind) {
290                 case PS2PP_KIND_WHEEL:
291                         psmouse->name = "Wheel Mouse";
292                         break;
293
294                 case PS2PP_KIND_MX:
295                         psmouse->name = "MX Mouse";
296                         break;
297
298                 case PS2PP_KIND_TP3:
299                         psmouse->name = "TouchPad 3";
300                         break;
301
302                 case PS2PP_KIND_TRACKMAN:
303                         psmouse->name = "TrackMan";
304                         break;
305
306                 default:
307                         /*
308                          * Set name to "Mouse" only when using PS2++,
309                          * otherwise let other protocols define suitable
310                          * name
311                          */
312                         if (using_ps2pp)
313                                 psmouse->name = "Mouse";
314                         break;
315         }
316 }
317
318
319 /*
320  * Logitech magic init. Detect whether the mouse is a Logitech one
321  * and its exact model and try turning on extended protocol for ones
322  * that support it.
323  */
324
325 int ps2pp_init(struct psmouse *psmouse, int set_properties)
326 {
327         struct ps2dev *ps2dev = &psmouse->ps2dev;
328         unsigned char param[4];
329         unsigned char model, buttons;
330         struct ps2pp_info *model_info;
331         int use_ps2pp = 0;
332
333         param[0] = 0;
334         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
335         ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
336         ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
337         ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
338         param[1] = 0;
339         ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
340
341         if (!param[1])
342                 return -1;
343
344         model = ((param[0] >> 4) & 0x07) | ((param[0] << 3) & 0x78);
345         buttons = param[1];
346
347         if ((model_info = get_model_info(model)) != NULL) {
348
349 /*
350  * Do Logitech PS2++ / PS2T++ magic init.
351  */
352                 if (model == 97) { /* Touch Pad 3 */
353
354                         /* Unprotect RAM */
355                         param[0] = 0x11; param[1] = 0x04; param[2] = 0x68;
356                         ps2_command(ps2dev, param, 0x30d1);
357                         /* Enable features */
358                         param[0] = 0x11; param[1] = 0x05; param[2] = 0x0b;
359                         ps2_command(ps2dev, param, 0x30d1);
360                         /* Enable PS2++ */
361                         param[0] = 0x11; param[1] = 0x09; param[2] = 0xc3;
362                         ps2_command(ps2dev, param, 0x30d1);
363
364                         param[0] = 0;
365                         if (!ps2_command(ps2dev, param, 0x13d1) &&
366                             param[0] == 0x06 && param[1] == 0x00 && param[2] == 0x14) {
367                                 use_ps2pp = 1;
368                         }
369
370                 } else {
371
372                         param[0] = param[1] = param[2] = 0;
373                         ps2pp_cmd(psmouse, param, 0x39); /* Magic knock */
374                         ps2pp_cmd(psmouse, param, 0xDB);
375
376                         if ((param[0] & 0x78) == 0x48 &&
377                             (param[1] & 0xf3) == 0xc2 &&
378                             (param[2] & 0x03) == ((param[1] >> 2) & 3)) {
379                                 ps2pp_set_smartscroll(psmouse, psmouse->smartscroll);
380                                 use_ps2pp = 1;
381                         }
382                 }
383         }
384
385         if (set_properties) {
386                 psmouse->vendor = "Logitech";
387                 psmouse->model = model;
388
389                 if (use_ps2pp) {
390                         psmouse->protocol_handler = ps2pp_process_byte;
391                         psmouse->pktsize = 3;
392
393                         if (model_info->kind != PS2PP_KIND_TP3) {
394                                 psmouse->set_resolution = ps2pp_set_resolution;
395                                 psmouse->disconnect = ps2pp_disconnect;
396
397                                 device_create_file(&psmouse->ps2dev.serio->dev,
398                                                    &psmouse_attr_smartscroll.dattr);
399                         }
400                 }
401
402                 if (buttons < 3)
403                         clear_bit(BTN_MIDDLE, psmouse->dev->keybit);
404
405                 if (model_info)
406                         ps2pp_set_model_properties(psmouse, model_info, use_ps2pp);
407         }
408
409         return use_ps2pp ? 0 : -1;
410 }
411