xref: /openbmc/linux/drivers/input/mouse/logips2pp.c (revision b627b4ed)
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 	u8 model;
34 	u8 kind;
35 	u16 features;
36 };
37 
38 /*
39  * Process a PS2++ or PS2T++ packet.
40  */
41 
42 static psmouse_ret_t ps2pp_process_byte(struct psmouse *psmouse)
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 	if ((packet[0] & 0x48) == 0x48 && (packet[1] & 0x02) == 0x02) {
55 
56 		/* Logitech extended packet */
57 		switch ((packet[1] >> 4) | (packet[0] & 0x30)) {
58 
59 			case 0x0d: /* Mouse extra info */
60 
61 				input_report_rel(dev, packet[2] & 0x80 ? REL_HWHEEL : REL_WHEEL,
62 					(int) (packet[2] & 8) - (int) (packet[2] & 7));
63 				input_report_key(dev, BTN_SIDE, (packet[2] >> 4) & 1);
64 				input_report_key(dev, BTN_EXTRA, (packet[2] >> 5) & 1);
65 
66 				break;
67 
68 			case 0x0e: /* buttons 4, 5, 6, 7, 8, 9, 10 info */
69 
70 				input_report_key(dev, BTN_SIDE, (packet[2]) & 1);
71 				input_report_key(dev, BTN_EXTRA, (packet[2] >> 1) & 1);
72 				input_report_key(dev, BTN_BACK, (packet[2] >> 3) & 1);
73 				input_report_key(dev, BTN_FORWARD, (packet[2] >> 4) & 1);
74 				input_report_key(dev, BTN_TASK, (packet[2] >> 2) & 1);
75 
76 				break;
77 
78 			case 0x0f: /* TouchPad extra info */
79 
80 				input_report_rel(dev, packet[2] & 0x08 ? REL_HWHEEL : REL_WHEEL,
81 					(int) ((packet[2] >> 4) & 8) - (int) ((packet[2] >> 4) & 7));
82 				packet[0] = packet[2] | 0x08;
83 				break;
84 
85 #ifdef DEBUG
86 			default:
87 				printk(KERN_WARNING "psmouse.c: Received PS2++ packet #%x, but don't know how to handle.\n",
88 					(packet[1] >> 4) | (packet[0] & 0x30));
89 #endif
90 		}
91 	} else {
92 		/* Standard PS/2 motion data */
93 		input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
94 		input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
95 	}
96 
97 	input_report_key(dev, BTN_LEFT,    packet[0]       & 1);
98 	input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
99 	input_report_key(dev, BTN_RIGHT,  (packet[0] >> 1) & 1);
100 
101 	input_sync(dev);
102 
103 	return PSMOUSE_FULL_PACKET;
104 
105 }
106 
107 /*
108  * ps2pp_cmd() sends a PS2++ command, sliced into two bit
109  * pieces through the SETRES command. This is needed to send extended
110  * commands to mice on notebooks that try to understand the PS/2 protocol
111  * Ugly.
112  */
113 
114 static int ps2pp_cmd(struct psmouse *psmouse, unsigned char *param, unsigned char command)
115 {
116 	if (psmouse_sliced_command(psmouse, command))
117 		return -1;
118 
119 	if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_POLL | 0x0300))
120 		return -1;
121 
122 	return 0;
123 }
124 
125 /*
126  * SmartScroll / CruiseControl for some newer Logitech mice Defaults to
127  * enabled if we do nothing to it. Of course I put this in because I want it
128  * disabled :P
129  * 1 - enabled (if previously disabled, also default)
130  * 0 - disabled
131  */
132 
133 static void ps2pp_set_smartscroll(struct psmouse *psmouse, unsigned int smartscroll)
134 {
135 	struct ps2dev *ps2dev = &psmouse->ps2dev;
136 	unsigned char param[4];
137 
138 	if (smartscroll > 1)
139 		smartscroll = 1;
140 
141 	ps2pp_cmd(psmouse, param, 0x32);
142 
143 	param[0] = 0;
144 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
145 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
146 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
147 
148 	param[0] = smartscroll;
149 	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
150 }
151 
152 static ssize_t ps2pp_attr_show_smartscroll(struct psmouse *psmouse, void *data, char *buf)
153 {
154 	return sprintf(buf, "%d\n", psmouse->smartscroll ? 1 : 0);
155 }
156 
157 static ssize_t ps2pp_attr_set_smartscroll(struct psmouse *psmouse, void *data, const char *buf, size_t count)
158 {
159 	unsigned long value;
160 
161 	if (strict_strtoul(buf, 10, &value) || value > 1)
162 		return -EINVAL;
163 
164 	ps2pp_set_smartscroll(psmouse, value);
165 	psmouse->smartscroll = value;
166 	return count;
167 }
168 
169 PSMOUSE_DEFINE_ATTR(smartscroll, S_IWUSR | S_IRUGO, NULL,
170 			ps2pp_attr_show_smartscroll, ps2pp_attr_set_smartscroll);
171 
172 /*
173  * Support 800 dpi resolution _only_ if the user wants it (there are good
174  * reasons to not use it even if the mouse supports it, and of course there are
175  * also good reasons to use it, let the user decide).
176  */
177 
178 static void ps2pp_set_resolution(struct psmouse *psmouse, unsigned int resolution)
179 {
180 	if (resolution > 400) {
181 		struct ps2dev *ps2dev = &psmouse->ps2dev;
182 		unsigned char param = 3;
183 
184 		ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
185 		ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
186 		ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
187 		ps2_command(ps2dev, &param, PSMOUSE_CMD_SETRES);
188 		psmouse->resolution = 800;
189 	} else
190 		psmouse_set_resolution(psmouse, resolution);
191 }
192 
193 static void ps2pp_disconnect(struct psmouse *psmouse)
194 {
195 	device_remove_file(&psmouse->ps2dev.serio->dev, &psmouse_attr_smartscroll.dattr);
196 }
197 
198 static const struct ps2pp_info *get_model_info(unsigned char model)
199 {
200 	static const struct ps2pp_info ps2pp_list[] = {
201 		{  1,	0,			0 },	/* Simple 2-button mouse */
202 		{ 12,	0,			PS2PP_SIDE_BTN},
203 		{ 13,	0,			0 },
204 		{ 15,	PS2PP_KIND_MX,					/* MX1000 */
205 				PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
206 				PS2PP_EXTRA_BTN | PS2PP_NAV_BTN | PS2PP_HWHEEL },
207 		{ 40,	0,			PS2PP_SIDE_BTN },
208 		{ 41,	0,			PS2PP_SIDE_BTN },
209 		{ 42,	0,			PS2PP_SIDE_BTN },
210 		{ 43,	0,			PS2PP_SIDE_BTN },
211 		{ 50,	0,			0 },
212 		{ 51,	0,			0 },
213 		{ 52,	PS2PP_KIND_WHEEL,	PS2PP_SIDE_BTN | PS2PP_WHEEL },
214 		{ 53,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
215 		{ 56,	PS2PP_KIND_WHEEL,	PS2PP_SIDE_BTN | PS2PP_WHEEL }, /* Cordless MouseMan Wheel */
216 		{ 61,	PS2PP_KIND_MX,					/* MX700 */
217 				PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
218 				PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
219 		{ 66,	PS2PP_KIND_MX,					/* MX3100 reciver */
220 				PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
221 				PS2PP_EXTRA_BTN | PS2PP_NAV_BTN | PS2PP_HWHEEL },
222 		{ 72,	PS2PP_KIND_TRACKMAN,	0 },			/* T-CH11: TrackMan Marble */
223 		{ 73,	0,			PS2PP_SIDE_BTN },
224 		{ 75,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
225 		{ 76,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
226 		{ 79,	PS2PP_KIND_TRACKMAN,	PS2PP_WHEEL },		/* TrackMan with wheel */
227 		{ 80,	PS2PP_KIND_WHEEL,	PS2PP_SIDE_BTN | PS2PP_WHEEL },
228 		{ 81,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
229 		{ 83,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
230 		{ 85,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
231 		{ 86,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
232 		{ 87,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
233 		{ 88,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
234 		{ 96,	0,			0 },
235 		{ 97,	PS2PP_KIND_TP3,		PS2PP_WHEEL | PS2PP_HWHEEL },
236 		{ 99,	PS2PP_KIND_WHEEL,	PS2PP_WHEEL },
237 		{ 100,	PS2PP_KIND_MX,					/* MX510 */
238 				PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
239 				PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
240 		{ 111,  PS2PP_KIND_MX,	PS2PP_WHEEL | PS2PP_SIDE_BTN },	/* MX300 reports task button as side */
241 		{ 112,	PS2PP_KIND_MX,					/* MX500 */
242 				PS2PP_WHEEL | PS2PP_SIDE_BTN | PS2PP_TASK_BTN |
243 				PS2PP_EXTRA_BTN | PS2PP_NAV_BTN },
244 		{ 114,	PS2PP_KIND_MX,					/* MX310 */
245 				PS2PP_WHEEL | PS2PP_SIDE_BTN |
246 				PS2PP_TASK_BTN | PS2PP_EXTRA_BTN }
247 	};
248 	int i;
249 
250 	for (i = 0; i < ARRAY_SIZE(ps2pp_list); i++)
251 		if (model == ps2pp_list[i].model)
252 			return &ps2pp_list[i];
253 
254 	printk(KERN_WARNING "logips2pp: Detected unknown logitech mouse model %d\n", model);
255 	return NULL;
256 }
257 
258 /*
259  * Set up input device's properties based on the detected mouse model.
260  */
261 
262 static void ps2pp_set_model_properties(struct psmouse *psmouse,
263 				       const struct ps2pp_info *model_info,
264 				       int using_ps2pp)
265 {
266 	struct input_dev *input_dev = psmouse->dev;
267 
268 	if (model_info->features & PS2PP_SIDE_BTN)
269 		set_bit(BTN_SIDE, input_dev->keybit);
270 
271 	if (model_info->features & PS2PP_EXTRA_BTN)
272 		set_bit(BTN_EXTRA, input_dev->keybit);
273 
274 	if (model_info->features & PS2PP_TASK_BTN)
275 		set_bit(BTN_TASK, input_dev->keybit);
276 
277 	if (model_info->features & PS2PP_NAV_BTN) {
278 		set_bit(BTN_FORWARD, input_dev->keybit);
279 		set_bit(BTN_BACK, input_dev->keybit);
280 	}
281 
282 	if (model_info->features & PS2PP_WHEEL)
283 		set_bit(REL_WHEEL, input_dev->relbit);
284 
285 	if (model_info->features & PS2PP_HWHEEL)
286 		set_bit(REL_HWHEEL, input_dev->relbit);
287 
288 	switch (model_info->kind) {
289 		case PS2PP_KIND_WHEEL:
290 			psmouse->name = "Wheel Mouse";
291 			break;
292 
293 		case PS2PP_KIND_MX:
294 			psmouse->name = "MX Mouse";
295 			break;
296 
297 		case PS2PP_KIND_TP3:
298 			psmouse->name = "TouchPad 3";
299 			break;
300 
301 		case PS2PP_KIND_TRACKMAN:
302 			psmouse->name = "TrackMan";
303 			break;
304 
305 		default:
306 			/*
307 			 * Set name to "Mouse" only when using PS2++,
308 			 * otherwise let other protocols define suitable
309 			 * name
310 			 */
311 			if (using_ps2pp)
312 				psmouse->name = "Mouse";
313 			break;
314 	}
315 }
316 
317 
318 /*
319  * Logitech magic init. Detect whether the mouse is a Logitech one
320  * and its exact model and try turning on extended protocol for ones
321  * that support it.
322  */
323 
324 int ps2pp_init(struct psmouse *psmouse, int set_properties)
325 {
326 	struct ps2dev *ps2dev = &psmouse->ps2dev;
327 	unsigned char param[4];
328 	unsigned char model, buttons;
329 	const struct ps2pp_info *model_info;
330 	int use_ps2pp = 0;
331 	int error;
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 	model = ((param[0] >> 4) & 0x07) | ((param[0] << 3) & 0x78);
342 	buttons = param[1];
343 
344 	if (!model || !buttons)
345 		return -1;
346 
347 	if ((model_info = get_model_info(model)) != NULL) {
348 
349 /*
350  * Do Logitech PS2++ / PS2T++ magic init.
351  */
352 		if (model_info->kind == PS2PP_KIND_TP3) { /* 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 				error = device_create_file(&psmouse->ps2dev.serio->dev,
398 							   &psmouse_attr_smartscroll.dattr);
399 				if (error) {
400 					printk(KERN_ERR
401 						"logips2pp.c: failed to create smartscroll "
402 						"sysfs attribute, error: %d\n", error);
403 					return -1;
404 				}
405 			}
406 		}
407 
408 		if (buttons < 3)
409 			clear_bit(BTN_MIDDLE, psmouse->dev->keybit);
410 
411 		if (model_info)
412 			ps2pp_set_model_properties(psmouse, model_info, use_ps2pp);
413 	}
414 
415 	return use_ps2pp ? 0 : -1;
416 }
417 
418