Input: drop redundant includes of moduleparam.h
[linux-3.10.git] / drivers / input / mouse / trackpoint.c
1 /*
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/input.h>
15 #include <linux/libps2.h>
16 #include <linux/proc_fs.h>
17 #include <asm/uaccess.h>
18 #include "psmouse.h"
19 #include "trackpoint.h"
20
21 /*
22  * Device IO: read, write and toggle bit
23  */
24 static int trackpoint_read(struct ps2dev *ps2dev, unsigned char loc, unsigned char *results)
25 {
26         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
27             ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
28                 return -1;
29         }
30
31         return 0;
32 }
33
34 static int trackpoint_write(struct ps2dev *ps2dev, unsigned char loc, unsigned char val)
35 {
36         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
37             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_WRITE_MEM)) ||
38             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
39             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, val))) {
40                 return -1;
41         }
42
43         return 0;
44 }
45
46 static int trackpoint_toggle_bit(struct ps2dev *ps2dev, unsigned char loc, unsigned char mask)
47 {
48         /* Bad things will happen if the loc param isn't in this range */
49         if (loc < 0x20 || loc >= 0x2F)
50                 return -1;
51
52         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
53             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_TOGGLE)) ||
54             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
55             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, mask))) {
56                 return -1;
57         }
58
59         return 0;
60 }
61
62
63 /*
64  * Trackpoint-specific attributes
65  */
66 struct trackpoint_attr_data {
67         size_t field_offset;
68         unsigned char command;
69         unsigned char mask;
70         unsigned char inverted;
71 };
72
73 static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse, void *data, char *buf)
74 {
75         struct trackpoint_data *tp = psmouse->private;
76         struct trackpoint_attr_data *attr = data;
77         unsigned char value = *(unsigned char *)((char *)tp + attr->field_offset);
78
79         if (attr->inverted)
80                 value = !value;
81
82         return sprintf(buf, "%u\n", value);
83 }
84
85 static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
86                                         const char *buf, size_t count)
87 {
88         struct trackpoint_data *tp = psmouse->private;
89         struct trackpoint_attr_data *attr = data;
90         unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
91         unsigned long value;
92         char *rest;
93
94         value = simple_strtoul(buf, &rest, 10);
95         if (*rest || value > 255)
96                 return -EINVAL;
97
98         *field = value;
99         trackpoint_write(&psmouse->ps2dev, attr->command, value);
100
101         return count;
102 }
103
104 #define TRACKPOINT_INT_ATTR(_name, _command)                                    \
105         static struct trackpoint_attr_data trackpoint_attr_##_name = {          \
106                 .field_offset = offsetof(struct trackpoint_data, _name),        \
107                 .command = _command,                                            \
108         };                                                                      \
109         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                           \
110                             &trackpoint_attr_##_name,                           \
111                             trackpoint_show_int_attr, trackpoint_set_int_attr)
112
113 static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
114                                         const char *buf, size_t count)
115 {
116         struct trackpoint_data *tp = psmouse->private;
117         struct trackpoint_attr_data *attr = data;
118         unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
119         unsigned long value;
120         char *rest;
121
122         value = simple_strtoul(buf, &rest, 10);
123         if (*rest || value > 1)
124                 return -EINVAL;
125
126         if (attr->inverted)
127                 value = !value;
128
129         if (*field != value) {
130                 *field = value;
131                 trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
132         }
133
134         return count;
135 }
136
137
138 #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv)                               \
139         static struct trackpoint_attr_data trackpoint_attr_##_name = {          \
140                 .field_offset   = offsetof(struct trackpoint_data, _name),      \
141                 .command        = _command,                                     \
142                 .mask           = _mask,                                        \
143                 .inverted       = _inv,                                         \
144         };                                                                      \
145         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                           \
146                             &trackpoint_attr_##_name,                           \
147                             trackpoint_show_int_attr, trackpoint_set_bit_attr)
148
149 TRACKPOINT_INT_ATTR(sensitivity, TP_SENS);
150 TRACKPOINT_INT_ATTR(speed, TP_SPEED);
151 TRACKPOINT_INT_ATTR(inertia, TP_INERTIA);
152 TRACKPOINT_INT_ATTR(reach, TP_REACH);
153 TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS);
154 TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG);
155 TRACKPOINT_INT_ATTR(thresh, TP_THRESH);
156 TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH);
157 TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME);
158 TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV);
159
160 TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, 0);
161 TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, 0);
162 TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, 1);
163
164 static struct attribute *trackpoint_attrs[] = {
165         &psmouse_attr_sensitivity.dattr.attr,
166         &psmouse_attr_speed.dattr.attr,
167         &psmouse_attr_inertia.dattr.attr,
168         &psmouse_attr_reach.dattr.attr,
169         &psmouse_attr_draghys.dattr.attr,
170         &psmouse_attr_mindrag.dattr.attr,
171         &psmouse_attr_thresh.dattr.attr,
172         &psmouse_attr_upthresh.dattr.attr,
173         &psmouse_attr_ztime.dattr.attr,
174         &psmouse_attr_jenks.dattr.attr,
175         &psmouse_attr_press_to_select.dattr.attr,
176         &psmouse_attr_skipback.dattr.attr,
177         &psmouse_attr_ext_dev.dattr.attr,
178         NULL
179 };
180
181 static struct attribute_group trackpoint_attr_group = {
182         .attrs = trackpoint_attrs,
183 };
184
185 static int trackpoint_start_protocol(struct psmouse *psmouse, unsigned char *firmware_id)
186 {
187         unsigned char param[2] = { 0 };
188
189         if (ps2_command(&psmouse->ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
190                 return -1;
191
192         if (param[0] != TP_MAGIC_IDENT)
193                 return -1;
194
195         if (firmware_id)
196                 *firmware_id = param[1];
197
198         return 0;
199 }
200
201 static int trackpoint_sync(struct psmouse *psmouse)
202 {
203         struct trackpoint_data *tp = psmouse->private;
204         unsigned char toggle;
205
206         /* Disable features that may make device unusable with this driver */
207         trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, &toggle);
208         if (toggle & TP_MASK_TWOHAND)
209                 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, TP_MASK_TWOHAND);
210
211         trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, &toggle);
212         if (toggle & TP_MASK_SOURCE_TAG)
213                 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, TP_MASK_SOURCE_TAG);
214
215         trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_MB, &toggle);
216         if (toggle & TP_MASK_MB)
217                 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_MB, TP_MASK_MB);
218
219         /* Push the config to the device */
220         trackpoint_write(&psmouse->ps2dev, TP_SENS, tp->sensitivity);
221         trackpoint_write(&psmouse->ps2dev, TP_INERTIA, tp->inertia);
222         trackpoint_write(&psmouse->ps2dev, TP_SPEED, tp->speed);
223
224         trackpoint_write(&psmouse->ps2dev, TP_REACH, tp->reach);
225         trackpoint_write(&psmouse->ps2dev, TP_DRAGHYS, tp->draghys);
226         trackpoint_write(&psmouse->ps2dev, TP_MINDRAG, tp->mindrag);
227
228         trackpoint_write(&psmouse->ps2dev, TP_THRESH, tp->thresh);
229         trackpoint_write(&psmouse->ps2dev, TP_UP_THRESH, tp->upthresh);
230
231         trackpoint_write(&psmouse->ps2dev, TP_Z_TIME, tp->ztime);
232         trackpoint_write(&psmouse->ps2dev, TP_JENKS_CURV, tp->jenks);
233
234         trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_PTSON, &toggle);
235         if (((toggle & TP_MASK_PTSON) == TP_MASK_PTSON) != tp->press_to_select)
236                  trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_PTSON, TP_MASK_PTSON);
237
238         trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, &toggle);
239         if (((toggle & TP_MASK_SKIPBACK) == TP_MASK_SKIPBACK) != tp->skipback)
240                 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK);
241
242         trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, &toggle);
243         if (((toggle & TP_MASK_EXT_DEV) == TP_MASK_EXT_DEV) != tp->ext_dev)
244                 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV);
245
246         return 0;
247 }
248
249 static void trackpoint_defaults(struct trackpoint_data *tp)
250 {
251         tp->press_to_select = TP_DEF_PTSON;
252         tp->sensitivity = TP_DEF_SENS;
253         tp->speed = TP_DEF_SPEED;
254         tp->reach = TP_DEF_REACH;
255
256         tp->draghys = TP_DEF_DRAGHYS;
257         tp->mindrag = TP_DEF_MINDRAG;
258
259         tp->thresh = TP_DEF_THRESH;
260         tp->upthresh = TP_DEF_UP_THRESH;
261
262         tp->ztime = TP_DEF_Z_TIME;
263         tp->jenks = TP_DEF_JENKS_CURV;
264
265         tp->inertia = TP_DEF_INERTIA;
266         tp->skipback = TP_DEF_SKIPBACK;
267         tp->ext_dev = TP_DEF_EXT_DEV;
268 }
269
270 static void trackpoint_disconnect(struct psmouse *psmouse)
271 {
272         sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group);
273
274         kfree(psmouse->private);
275         psmouse->private = NULL;
276 }
277
278 static int trackpoint_reconnect(struct psmouse *psmouse)
279 {
280         if (trackpoint_start_protocol(psmouse, NULL))
281                 return -1;
282
283         if (trackpoint_sync(psmouse))
284                 return -1;
285
286         return 0;
287 }
288
289 int trackpoint_detect(struct psmouse *psmouse, int set_properties)
290 {
291         struct trackpoint_data *priv;
292         struct ps2dev *ps2dev = &psmouse->ps2dev;
293         unsigned char firmware_id;
294         unsigned char button_info;
295         int error;
296
297         if (trackpoint_start_protocol(psmouse, &firmware_id))
298                 return -1;
299
300         if (!set_properties)
301                 return 0;
302
303         if (trackpoint_read(&psmouse->ps2dev, TP_EXT_BTN, &button_info)) {
304                 printk(KERN_WARNING "trackpoint.c: failed to get extended button data\n");
305                 button_info = 0;
306         }
307
308         psmouse->private = priv = kzalloc(sizeof(struct trackpoint_data), GFP_KERNEL);
309         if (!priv)
310                 return -1;
311
312         psmouse->vendor = "IBM";
313         psmouse->name = "TrackPoint";
314
315         psmouse->reconnect = trackpoint_reconnect;
316         psmouse->disconnect = trackpoint_disconnect;
317
318         trackpoint_defaults(priv);
319         trackpoint_sync(psmouse);
320
321         error = sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group);
322         if (error) {
323                 printk(KERN_ERR
324                         "trackpoint.c: failed to create sysfs attributes, error: %d\n",
325                         error);
326                 kfree(priv);
327                 return -1;
328         }
329
330         printk(KERN_INFO "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
331                 firmware_id, (button_info & 0xf0) >> 4, button_info & 0x0f);
332
333         return 0;
334 }
335