2 * Logitech PS/2++ mouse driver
4 * Copyright (c) 1999-2003 Vojtech Pavlik <vojtech@suse.cz>
5 * Copyright (c) 2003 Eric Wong <eric@yhbt.net>
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.
12 #include <linux/bitops.h>
13 #include <linux/input.h>
14 #include <linux/serio.h>
15 #include <linux/libps2.h>
16 #include <linux/types.h>
18 #include "logips2pp.h"
20 /* Logitech mouse types */
21 #define PS2PP_KIND_WHEEL 1
22 #define PS2PP_KIND_MX 2
23 #define PS2PP_KIND_TP3 3
24 #define PS2PP_KIND_TRACKMAN 4
26 /* Logitech mouse features */
27 #define PS2PP_WHEEL BIT(0)
28 #define PS2PP_HWHEEL BIT(1)
29 #define PS2PP_SIDE_BTN BIT(2)
30 #define PS2PP_EXTRA_BTN BIT(3)
31 #define PS2PP_TASK_BTN BIT(4)
32 #define PS2PP_NAV_BTN BIT(5)
41 * Process a PS2++ or PS2T++ packet.
44 static psmouse_ret_t ps2pp_process_byte(struct psmouse *psmouse)
46 struct input_dev *dev = psmouse->dev;
47 u8 *packet = psmouse->packet;
49 if (psmouse->pktcnt < 3)
50 return PSMOUSE_GOOD_DATA;
53 * Full packet accumulated, process it
56 if ((packet[0] & 0x48) == 0x48 && (packet[1] & 0x02) == 0x02) {
58 /* Logitech extended packet */
59 switch ((packet[1] >> 4) | (packet[0] & 0x30)) {
61 case 0x0d: /* Mouse extra info */
64 packet[2] & 0x80 ? REL_HWHEEL : REL_WHEEL,
65 -sign_extend32(packet[2], 3));
66 input_report_key(dev, BTN_SIDE, packet[2] & BIT(4));
67 input_report_key(dev, BTN_EXTRA, packet[2] & BIT(5));
71 case 0x0e: /* buttons 4, 5, 6, 7, 8, 9, 10 info */
73 input_report_key(dev, BTN_SIDE, packet[2] & BIT(0));
74 input_report_key(dev, BTN_EXTRA, packet[2] & BIT(1));
75 input_report_key(dev, BTN_TASK, packet[2] & BIT(2));
76 input_report_key(dev, BTN_BACK, packet[2] & BIT(3));
77 input_report_key(dev, BTN_FORWARD, packet[2] & BIT(4));
81 case 0x0f: /* TouchPad extra info */
84 packet[2] & 0x08 ? REL_HWHEEL : REL_WHEEL,
85 -sign_extend32(packet[2] >> 4, 3));
86 packet[0] = packet[2] | BIT(3);
91 "Received PS2++ packet #%x, but don't know how to handle.\n",
92 (packet[1] >> 4) | (packet[0] & 0x30));
96 psmouse_report_standard_buttons(dev, packet[0]);
99 /* Standard PS/2 motion data */
100 psmouse_report_standard_packet(dev, packet);
105 return PSMOUSE_FULL_PACKET;
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
116 static int ps2pp_cmd(struct psmouse *psmouse, u8 *param, u8 command)
120 error = ps2_sliced_command(&psmouse->ps2dev, command);
124 error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_POLL | 0x0300);
132 * SmartScroll / CruiseControl for some newer Logitech mice Defaults to
133 * enabled if we do nothing to it. Of course I put this in because I want it
135 * 1 - enabled (if previously disabled, also default)
139 static void ps2pp_set_smartscroll(struct psmouse *psmouse, bool smartscroll)
141 struct ps2dev *ps2dev = &psmouse->ps2dev;
144 ps2pp_cmd(psmouse, param, 0x32);
147 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
148 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
149 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
151 param[0] = smartscroll;
152 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
155 static ssize_t ps2pp_attr_show_smartscroll(struct psmouse *psmouse,
156 void *data, char *buf)
158 return sprintf(buf, "%d\n", psmouse->smartscroll);
161 static ssize_t ps2pp_attr_set_smartscroll(struct psmouse *psmouse, void *data,
162 const char *buf, size_t count)
167 err = kstrtouint(buf, 10, &value);
174 ps2pp_set_smartscroll(psmouse, value);
175 psmouse->smartscroll = value;
179 PSMOUSE_DEFINE_ATTR(smartscroll, S_IWUSR | S_IRUGO, NULL,
180 ps2pp_attr_show_smartscroll, ps2pp_attr_set_smartscroll);
183 * Support 800 dpi resolution _only_ if the user wants it (there are good
184 * reasons to not use it even if the mouse supports it, and of course there are
185 * also good reasons to use it, let the user decide).
188 static void ps2pp_set_resolution(struct psmouse *psmouse,
189 unsigned int resolution)
191 if (resolution > 400) {
192 struct ps2dev *ps2dev = &psmouse->ps2dev;
195 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
196 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
197 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
198 ps2_command(ps2dev, ¶m, PSMOUSE_CMD_SETRES);
199 psmouse->resolution = 800;
201 psmouse_set_resolution(psmouse, resolution);
204 static void ps2pp_disconnect(struct psmouse *psmouse)
206 device_remove_file(&psmouse->ps2dev.serio->dev,
207 &psmouse_attr_smartscroll.dattr);
210 static const struct ps2pp_info *get_model_info(unsigned char model)
212 static const struct ps2pp_info ps2pp_list[] = {
213 { 1, 0, 0 }, /* Simple 2-button mouse */
214 { 12, 0, PS2PP_SIDE_BTN},
216 { 15, PS2PP_KIND_MX, /* MX1000 */
217 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
218 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN | PS2PP_HWHEEL },
219 { 40, 0, PS2PP_SIDE_BTN },
220 { 41, 0, PS2PP_SIDE_BTN },
221 { 42, 0, PS2PP_SIDE_BTN },
222 { 43, 0, PS2PP_SIDE_BTN },
225 { 52, PS2PP_KIND_WHEEL, PS2PP_SIDE_BTN | PS2PP_WHEEL },
226 { 53, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
227 { 56, PS2PP_KIND_WHEEL, PS2PP_SIDE_BTN | PS2PP_WHEEL }, /* Cordless MouseMan Wheel */
228 { 61, PS2PP_KIND_MX, /* MX700 */
229 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
230 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
231 { 66, PS2PP_KIND_MX, /* MX3100 receiver */
232 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
233 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN | PS2PP_HWHEEL },
234 { 72, PS2PP_KIND_TRACKMAN, 0 }, /* T-CH11: TrackMan Marble */
235 { 73, PS2PP_KIND_TRACKMAN, PS2PP_SIDE_BTN }, /* TrackMan FX */
236 { 75, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
237 { 76, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
238 { 79, PS2PP_KIND_TRACKMAN, PS2PP_WHEEL }, /* TrackMan with wheel */
239 { 80, PS2PP_KIND_WHEEL, PS2PP_SIDE_BTN | PS2PP_WHEEL },
240 { 81, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
241 { 83, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
242 { 85, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
243 { 86, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
244 { 87, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
245 { 88, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
247 { 97, PS2PP_KIND_TP3, PS2PP_WHEEL | PS2PP_HWHEEL },
248 { 99, PS2PP_KIND_WHEEL, PS2PP_WHEEL },
249 { 100, PS2PP_KIND_MX, /* MX510 */
250 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
251 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
252 { 111, PS2PP_KIND_MX, PS2PP_WHEEL | PS2PP_SIDE_BTN }, /* MX300 reports task button as side */
253 { 112, PS2PP_KIND_MX, /* MX500 */
254 PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
255 PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
256 { 114, PS2PP_KIND_MX, /* MX310 */
257 PS2PP_WHEEL | PS2PP_SIDE_BTN |
258 PS2PP_TASK_BTN | PS2PP_EXTRA_BTN }
262 for (i = 0; i < ARRAY_SIZE(ps2pp_list); i++)
263 if (model == ps2pp_list[i].model)
264 return &ps2pp_list[i];
270 * Set up input device's properties based on the detected mouse model.
273 static void ps2pp_set_model_properties(struct psmouse *psmouse,
274 const struct ps2pp_info *model_info,
277 struct input_dev *input_dev = psmouse->dev;
279 if (model_info->features & PS2PP_SIDE_BTN)
280 input_set_capability(input_dev, EV_KEY, BTN_SIDE);
282 if (model_info->features & PS2PP_EXTRA_BTN)
283 input_set_capability(input_dev, EV_KEY, BTN_EXTRA);
285 if (model_info->features & PS2PP_TASK_BTN)
286 input_set_capability(input_dev, EV_KEY, BTN_TASK);
288 if (model_info->features & PS2PP_NAV_BTN) {
289 input_set_capability(input_dev, EV_KEY, BTN_FORWARD);
290 input_set_capability(input_dev, EV_KEY, BTN_BACK);
293 if (model_info->features & PS2PP_WHEEL)
294 input_set_capability(input_dev, EV_REL, REL_WHEEL);
296 if (model_info->features & PS2PP_HWHEEL)
297 input_set_capability(input_dev, EV_REL, REL_HWHEEL);
299 switch (model_info->kind) {
301 case PS2PP_KIND_WHEEL:
302 psmouse->name = "Wheel Mouse";
306 psmouse->name = "MX Mouse";
310 psmouse->name = "TouchPad 3";
313 case PS2PP_KIND_TRACKMAN:
314 psmouse->name = "TrackMan";
319 * Set name to "Mouse" only when using PS2++,
320 * otherwise let other protocols define suitable
324 psmouse->name = "Mouse";
329 static int ps2pp_setup_protocol(struct psmouse *psmouse,
330 const struct ps2pp_info *model_info)
334 psmouse->protocol_handler = ps2pp_process_byte;
335 psmouse->pktsize = 3;
337 if (model_info->kind != PS2PP_KIND_TP3) {
338 psmouse->set_resolution = ps2pp_set_resolution;
339 psmouse->disconnect = ps2pp_disconnect;
341 error = device_create_file(&psmouse->ps2dev.serio->dev,
342 &psmouse_attr_smartscroll.dattr);
345 "failed to create smartscroll sysfs attribute, error: %d\n",
355 * Logitech magic init. Detect whether the mouse is a Logitech one
356 * and its exact model and try turning on extended protocol for ones
360 int ps2pp_detect(struct psmouse *psmouse, bool set_properties)
362 struct ps2dev *ps2dev = &psmouse->ps2dev;
363 const struct ps2pp_info *model_info;
366 bool use_ps2pp = false;
370 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
371 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
372 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
373 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
375 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
377 model = ((param[0] >> 4) & 0x07) | ((param[0] << 3) & 0x78);
380 if (!model || !buttons)
383 model_info = get_model_info(model);
387 * Do Logitech PS2++ / PS2T++ magic init.
389 if (model_info->kind == PS2PP_KIND_TP3) { /* Touch Pad 3 */
392 param[0] = 0x11; param[1] = 0x04; param[2] = 0x68;
393 ps2_command(ps2dev, param, 0x30d1);
394 /* Enable features */
395 param[0] = 0x11; param[1] = 0x05; param[2] = 0x0b;
396 ps2_command(ps2dev, param, 0x30d1);
398 param[0] = 0x11; param[1] = 0x09; param[2] = 0xc3;
399 ps2_command(ps2dev, param, 0x30d1);
402 if (!ps2_command(ps2dev, param, 0x13d1) &&
403 param[0] == 0x06 && param[1] == 0x00 &&
410 param[0] = param[1] = param[2] = 0;
411 ps2pp_cmd(psmouse, param, 0x39); /* Magic knock */
412 ps2pp_cmd(psmouse, param, 0xDB);
414 if ((param[0] & 0x78) == 0x48 &&
415 (param[1] & 0xf3) == 0xc2 &&
416 (param[2] & 0x03) == ((param[1] >> 2) & 3)) {
417 ps2pp_set_smartscroll(psmouse, false);
423 psmouse_warn(psmouse,
424 "Detected unknown Logitech mouse model %d\n",
428 if (set_properties) {
429 psmouse->vendor = "Logitech";
430 psmouse->model = model;
433 error = ps2pp_setup_protocol(psmouse, model_info);
439 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
442 ps2pp_set_model_properties(psmouse, model_info, use_ps2pp);
445 return use_ps2pp ? 0 : -ENXIO;