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