blob: 9ab5b5ea809d1a9c00820dd4937d6419be1e9733 [file] [log] [blame]
Stephen Evanchik541e3162005-08-08 01:26:18 -05001/*
2 * Stephen Evanchik <evanchsa@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 *
8 * Trademarks are the property of their respective owners.
9 */
10
11#include <linux/delay.h>
12#include <linux/serio.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/input.h>
16#include <linux/libps2.h>
17#include <linux/proc_fs.h>
18#include <asm/uaccess.h>
19#include "psmouse.h"
20#include "trackpoint.h"
21
Stephen Evanchik541e3162005-08-08 01:26:18 -050022/*
23 * Device IO: read, write and toggle bit
24 */
25static int trackpoint_read(struct ps2dev *ps2dev, unsigned char loc, unsigned char *results)
26{
27 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
28 ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
29 return -1;
30 }
31
32 return 0;
33}
34
35static int trackpoint_write(struct ps2dev *ps2dev, unsigned char loc, unsigned char val)
36{
37 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
38 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_WRITE_MEM)) ||
39 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
40 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, val))) {
41 return -1;
42 }
43
44 return 0;
45}
46
47static int trackpoint_toggle_bit(struct ps2dev *ps2dev, unsigned char loc, unsigned char mask)
48{
49 /* Bad things will happen if the loc param isn't in this range */
50 if (loc < 0x20 || loc >= 0x2F)
51 return -1;
52
53 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
54 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_TOGGLE)) ||
55 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
56 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, mask))) {
57 return -1;
58 }
59
60 return 0;
61}
62
Stephen Evanchik541e3162005-08-08 01:26:18 -050063
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050064/*
65 * Trackpoint-specific attributes
66 */
67struct trackpoint_attr_data {
68 size_t field_offset;
69 unsigned char command;
70 unsigned char mask;
Dmitry Torokhovb8044c72006-02-15 00:49:09 -050071 unsigned char inverted;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050072};
Stephen Evanchik541e3162005-08-08 01:26:18 -050073
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050074static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse, void *data, char *buf)
75{
76 struct trackpoint_data *tp = psmouse->private;
77 struct trackpoint_attr_data *attr = data;
Dmitry Torokhovb8044c72006-02-15 00:49:09 -050078 unsigned char value = *(unsigned char *)((char *)tp + attr->field_offset);
Stephen Evanchik541e3162005-08-08 01:26:18 -050079
Dmitry Torokhovb8044c72006-02-15 00:49:09 -050080 if (attr->inverted)
81 value = !value;
82
83 return sprintf(buf, "%u\n", value);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050084}
Stephen Evanchik541e3162005-08-08 01:26:18 -050085
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050086static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
87 const char *buf, size_t count)
88{
89 struct trackpoint_data *tp = psmouse->private;
90 struct trackpoint_attr_data *attr = data;
91 unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
92 unsigned long value;
93 char *rest;
Stephen Evanchik541e3162005-08-08 01:26:18 -050094
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050095 value = simple_strtoul(buf, &rest, 10);
96 if (*rest || value > 255)
97 return -EINVAL;
Stephen Evanchik541e3162005-08-08 01:26:18 -050098
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050099 *field = value;
100 trackpoint_write(&psmouse->ps2dev, attr->command, value);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500101
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500102 return count;
103}
Stephen Evanchik541e3162005-08-08 01:26:18 -0500104
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500105#define TRACKPOINT_INT_ATTR(_name, _command) \
106 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
107 .field_offset = offsetof(struct trackpoint_data, _name), \
108 .command = _command, \
109 }; \
110 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
111 &trackpoint_attr_##_name, \
112 trackpoint_show_int_attr, trackpoint_set_int_attr)
Stephen Evanchik541e3162005-08-08 01:26:18 -0500113
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500114static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
115 const char *buf, size_t count)
116{
117 struct trackpoint_data *tp = psmouse->private;
118 struct trackpoint_attr_data *attr = data;
119 unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
120 unsigned long value;
121 char *rest;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500122
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500123 value = simple_strtoul(buf, &rest, 10);
124 if (*rest || value > 1)
125 return -EINVAL;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500126
Dmitry Torokhovb8044c72006-02-15 00:49:09 -0500127 if (attr->inverted)
128 value = !value;
129
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500130 if (*field != value) {
131 *field = value;
132 trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
133 }
Stephen Evanchik541e3162005-08-08 01:26:18 -0500134
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500135 return count;
136}
137
138
Dmitry Torokhovb8044c72006-02-15 00:49:09 -0500139#define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv) \
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500140 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
141 .field_offset = offsetof(struct trackpoint_data, _name), \
142 .command = _command, \
143 .mask = _mask, \
Dmitry Torokhovb8044c72006-02-15 00:49:09 -0500144 .inverted = _inv, \
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500145 }; \
146 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
147 &trackpoint_attr_##_name, \
148 trackpoint_show_int_attr, trackpoint_set_bit_attr)
149
150TRACKPOINT_INT_ATTR(sensitivity, TP_SENS);
151TRACKPOINT_INT_ATTR(speed, TP_SPEED);
152TRACKPOINT_INT_ATTR(inertia, TP_INERTIA);
153TRACKPOINT_INT_ATTR(reach, TP_REACH);
154TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS);
155TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG);
156TRACKPOINT_INT_ATTR(thresh, TP_THRESH);
157TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH);
158TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME);
159TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV);
160
Dmitry Torokhovb8044c72006-02-15 00:49:09 -0500161TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, 0);
162TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, 0);
163TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, 1);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500164
165static struct attribute *trackpoint_attrs[] = {
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500166 &psmouse_attr_sensitivity.dattr.attr,
167 &psmouse_attr_speed.dattr.attr,
168 &psmouse_attr_inertia.dattr.attr,
169 &psmouse_attr_reach.dattr.attr,
170 &psmouse_attr_draghys.dattr.attr,
171 &psmouse_attr_mindrag.dattr.attr,
172 &psmouse_attr_thresh.dattr.attr,
173 &psmouse_attr_upthresh.dattr.attr,
174 &psmouse_attr_ztime.dattr.attr,
175 &psmouse_attr_jenks.dattr.attr,
176 &psmouse_attr_press_to_select.dattr.attr,
177 &psmouse_attr_skipback.dattr.attr,
178 &psmouse_attr_ext_dev.dattr.attr,
Stephen Evanchik541e3162005-08-08 01:26:18 -0500179 NULL
180};
181
182static struct attribute_group trackpoint_attr_group = {
183 .attrs = trackpoint_attrs,
184};
185
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400186static int trackpoint_start_protocol(struct psmouse *psmouse, unsigned char *firmware_id)
Stephen Evanchik541e3162005-08-08 01:26:18 -0500187{
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400188 unsigned char param[2] = { 0 };
Stephen Evanchik541e3162005-08-08 01:26:18 -0500189
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400190 if (ps2_command(&psmouse->ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
191 return -1;
192
193 if (param[0] != TP_MAGIC_IDENT)
194 return -1;
195
196 if (firmware_id)
197 *firmware_id = param[1];
198
199 return 0;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500200}
201
202static int trackpoint_sync(struct psmouse *psmouse)
203{
Stephen Evanchik541e3162005-08-08 01:26:18 -0500204 struct trackpoint_data *tp = psmouse->private;
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400205 unsigned char toggle;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500206
207 /* Disable features that may make device unusable with this driver */
208 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, &toggle);
209 if (toggle & TP_MASK_TWOHAND)
210 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, TP_MASK_TWOHAND);
211
212 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, &toggle);
213 if (toggle & TP_MASK_SOURCE_TAG)
214 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, TP_MASK_SOURCE_TAG);
215
216 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_MB, &toggle);
217 if (toggle & TP_MASK_MB)
218 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_MB, TP_MASK_MB);
219
220 /* Push the config to the device */
221 trackpoint_write(&psmouse->ps2dev, TP_SENS, tp->sensitivity);
222 trackpoint_write(&psmouse->ps2dev, TP_INERTIA, tp->inertia);
223 trackpoint_write(&psmouse->ps2dev, TP_SPEED, tp->speed);
224
225 trackpoint_write(&psmouse->ps2dev, TP_REACH, tp->reach);
226 trackpoint_write(&psmouse->ps2dev, TP_DRAGHYS, tp->draghys);
227 trackpoint_write(&psmouse->ps2dev, TP_MINDRAG, tp->mindrag);
228
229 trackpoint_write(&psmouse->ps2dev, TP_THRESH, tp->thresh);
230 trackpoint_write(&psmouse->ps2dev, TP_UP_THRESH, tp->upthresh);
231
232 trackpoint_write(&psmouse->ps2dev, TP_Z_TIME, tp->ztime);
233 trackpoint_write(&psmouse->ps2dev, TP_JENKS_CURV, tp->jenks);
234
235 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_PTSON, &toggle);
236 if (((toggle & TP_MASK_PTSON) == TP_MASK_PTSON) != tp->press_to_select)
237 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_PTSON, TP_MASK_PTSON);
238
239 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, &toggle);
240 if (((toggle & TP_MASK_SKIPBACK) == TP_MASK_SKIPBACK) != tp->skipback)
241 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK);
242
243 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, &toggle);
244 if (((toggle & TP_MASK_EXT_DEV) == TP_MASK_EXT_DEV) != tp->ext_dev)
245 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV);
246
247 return 0;
248}
249
250static void trackpoint_defaults(struct trackpoint_data *tp)
251{
252 tp->press_to_select = TP_DEF_PTSON;
253 tp->sensitivity = TP_DEF_SENS;
254 tp->speed = TP_DEF_SPEED;
255 tp->reach = TP_DEF_REACH;
256
257 tp->draghys = TP_DEF_DRAGHYS;
258 tp->mindrag = TP_DEF_MINDRAG;
259
260 tp->thresh = TP_DEF_THRESH;
261 tp->upthresh = TP_DEF_UP_THRESH;
262
263 tp->ztime = TP_DEF_Z_TIME;
264 tp->jenks = TP_DEF_JENKS_CURV;
265
266 tp->inertia = TP_DEF_INERTIA;
267 tp->skipback = TP_DEF_SKIPBACK;
268 tp->ext_dev = TP_DEF_EXT_DEV;
269}
270
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400271static void trackpoint_disconnect(struct psmouse *psmouse)
272{
273 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group);
274
275 kfree(psmouse->private);
276 psmouse->private = NULL;
277}
278
279static int trackpoint_reconnect(struct psmouse *psmouse)
280{
281 if (trackpoint_start_protocol(psmouse, NULL))
282 return -1;
283
284 if (trackpoint_sync(psmouse))
285 return -1;
286
287 return 0;
288}
289
Stephen Evanchik541e3162005-08-08 01:26:18 -0500290int trackpoint_detect(struct psmouse *psmouse, int set_properties)
291{
292 struct trackpoint_data *priv;
293 struct ps2dev *ps2dev = &psmouse->ps2dev;
294 unsigned char firmware_id;
295 unsigned char button_info;
Jeff Garzik8ff22ea2006-11-02 23:59:30 -0500296 int error;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500297
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400298 if (trackpoint_start_protocol(psmouse, &firmware_id))
Stephen Evanchik541e3162005-08-08 01:26:18 -0500299 return -1;
300
301 if (!set_properties)
302 return 0;
303
Stephen Evanchik541e3162005-08-08 01:26:18 -0500304 if (trackpoint_read(&psmouse->ps2dev, TP_EXT_BTN, &button_info)) {
305 printk(KERN_WARNING "trackpoint.c: failed to get extended button data\n");
306 button_info = 0;
307 }
308
Jeff Garzik8ff22ea2006-11-02 23:59:30 -0500309 psmouse->private = priv = kzalloc(sizeof(struct trackpoint_data), GFP_KERNEL);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500310 if (!priv)
311 return -1;
312
313 psmouse->vendor = "IBM";
314 psmouse->name = "TrackPoint";
315
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400316 psmouse->reconnect = trackpoint_reconnect;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500317 psmouse->disconnect = trackpoint_disconnect;
318
319 trackpoint_defaults(priv);
320 trackpoint_sync(psmouse);
321
Jeff Garzik8ff22ea2006-11-02 23:59:30 -0500322 error = sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group);
323 if (error) {
324 printk(KERN_ERR
325 "trackpoint.c: failed to create sysfs attributes, error: %d\n",
326 error);
327 kfree(priv);
328 return -1;
329 }
Stephen Evanchik541e3162005-08-08 01:26:18 -0500330
331 printk(KERN_INFO "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
332 firmware_id, (button_info & 0xf0) >> 4, button_info & 0x0f);
333
334 return 0;
335}
336