Merge branch 'linux-2.6'
[linux-2.6] / drivers / input / mouse / lifebook.c
1 /*
2  * Fujitsu B-series Lifebook PS/2 TouchScreen driver
3  *
4  * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
5  * Copyright (c) 2005 Kenan Esau <kenan.esau@conan.de>
6  *
7  * TouchScreen detection, absolute mode setting and packet layout is taken from
8  * Harald Hoyer's description of the device.
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License version 2 as published by
12  * the Free Software Foundation.
13  */
14
15 #include <linux/input.h>
16 #include <linux/serio.h>
17 #include <linux/libps2.h>
18 #include <linux/dmi.h>
19
20 #include "psmouse.h"
21 #include "lifebook.h"
22
23 struct lifebook_data {
24         struct input_dev *dev2;         /* Relative device */
25         char phys[32];
26 };
27
28 static const char *desired_serio_phys;
29
30 static int lifebook_set_serio_phys(const struct dmi_system_id *d)
31 {
32         desired_serio_phys = d->driver_data;
33         return 0;
34 }
35
36 static unsigned char lifebook_use_6byte_proto;
37
38 static int lifebook_set_6byte_proto(const struct dmi_system_id *d)
39 {
40         lifebook_use_6byte_proto = 1;
41         return 0;
42 }
43
44 static const struct dmi_system_id lifebook_dmi_table[] = {
45         {
46                 .ident = "FLORA-ie 55mi",
47                 .matches = {
48                         DMI_MATCH(DMI_PRODUCT_NAME, "FLORA-ie 55mi"),
49                 },
50         },
51         {
52                 .ident = "LifeBook B",
53                 .matches = {
54                         DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook B Series"),
55                 },
56         },
57         {
58                 .ident = "Lifebook B",
59                 .matches = {
60                         DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK B Series"),
61                 },
62         },
63         {
64                 .ident = "Lifebook B213x/B2150",
65                 .matches = {
66                         DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook B2131/B2133/B2150"),
67                 },
68         },
69         {
70                 .ident = "Zephyr",
71                 .matches = {
72                         DMI_MATCH(DMI_PRODUCT_NAME, "ZEPHYR"),
73                 },
74         },
75         {
76                 .ident = "CF-18",
77                 .matches = {
78                         DMI_MATCH(DMI_PRODUCT_NAME, "CF-18"),
79                 },
80                 .callback = lifebook_set_serio_phys,
81                 .driver_data = "isa0060/serio3",
82         },
83         {
84                 .ident = "Panasonic CF-28",
85                 .matches = {
86                         DMI_MATCH(DMI_SYS_VENDOR, "Matsushita"),
87                         DMI_MATCH(DMI_PRODUCT_NAME, "CF-28"),
88                 },
89                 .callback = lifebook_set_6byte_proto,
90         },
91         {
92                 .ident = "Panasonic CF-29",
93                 .matches = {
94                         DMI_MATCH(DMI_SYS_VENDOR, "Matsushita"),
95                         DMI_MATCH(DMI_PRODUCT_NAME, "CF-29"),
96                 },
97                 .callback = lifebook_set_6byte_proto,
98         },
99         {
100                 .ident = "CF-72",
101                 .matches = {
102                         DMI_MATCH(DMI_PRODUCT_NAME, "CF-72"),
103                 },
104                 .callback = lifebook_set_serio_phys,
105                 .driver_data = "isa0060/serio3",
106         },
107         {
108                 .ident = "Lifebook B142",
109                 .matches = {
110                         DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook B142"),
111                 },
112         },
113         { }
114 };
115
116 static psmouse_ret_t lifebook_process_byte(struct psmouse *psmouse)
117 {
118         struct lifebook_data *priv = psmouse->private;
119         struct input_dev *dev1 = psmouse->dev;
120         struct input_dev *dev2 = priv ? priv->dev2 : NULL;
121         unsigned char *packet = psmouse->packet;
122         int relative_packet = packet[0] & 0x08;
123
124         if (relative_packet || !lifebook_use_6byte_proto) {
125                 if (psmouse->pktcnt != 3)
126                         return PSMOUSE_GOOD_DATA;
127         } else {
128                 switch (psmouse->pktcnt) {
129                 case 1:
130                         return (packet[0] & 0xf8) == 0x00 ?
131                                 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
132                 case 2:
133                         return PSMOUSE_GOOD_DATA;
134                 case 3:
135                         return ((packet[2] & 0x30) << 2) == (packet[2] & 0xc0) ?
136                                 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
137                 case 4:
138                         return (packet[3] & 0xf8) == 0xc0 ?
139                                 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
140                 case 5:
141                         return (packet[4] & 0xc0) == (packet[2] & 0xc0) ?
142                                 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
143                 case 6:
144                         if (((packet[5] & 0x30) << 2) != (packet[5] & 0xc0))
145                                 return PSMOUSE_BAD_DATA;
146                         if ((packet[5] & 0xc0) != (packet[1] & 0xc0))
147                                 return PSMOUSE_BAD_DATA;
148                         break; /* report data */
149                 }
150         }
151
152         if (relative_packet) {
153                 if (!dev2)
154                         printk(KERN_WARNING "lifebook.c: got relative packet "
155                                 "but no relative device set up\n");
156         } else if (lifebook_use_6byte_proto) {
157                 input_report_abs(dev1, ABS_X,
158                                  ((packet[1] & 0x3f) << 6) | (packet[2] & 0x3f));
159                 input_report_abs(dev1, ABS_Y,
160                                  4096 - (((packet[4] & 0x3f) << 6) | (packet[5] & 0x3f)));
161         } else {
162                 input_report_abs(dev1, ABS_X,
163                                  (packet[1] | ((packet[0] & 0x30) << 4)));
164                 input_report_abs(dev1, ABS_Y,
165                                  1024 - (packet[2] | ((packet[0] & 0xC0) << 2)));
166         }
167
168         input_report_key(dev1, BTN_TOUCH, packet[0] & 0x04);
169         input_sync(dev1);
170
171         if (dev2) {
172                 if (relative_packet) {
173                         input_report_rel(dev2, REL_X,
174                                 ((packet[0] & 0x10) ? packet[1] - 256 : packet[1]));
175                         input_report_rel(dev2, REL_Y,
176                                  -(int)((packet[0] & 0x20) ? packet[2] - 256 : packet[2]));
177                 }
178                 input_report_key(dev2, BTN_LEFT, packet[0] & 0x01);
179                 input_report_key(dev2, BTN_RIGHT, packet[0] & 0x02);
180                 input_sync(dev2);
181         }
182
183         return PSMOUSE_FULL_PACKET;
184 }
185
186 static int lifebook_absolute_mode(struct psmouse *psmouse)
187 {
188         struct ps2dev *ps2dev = &psmouse->ps2dev;
189         unsigned char param;
190
191         if (psmouse_reset(psmouse))
192                 return -1;
193
194         /*
195            Enable absolute output -- ps2_command fails always but if
196            you leave this call out the touchsreen will never send
197            absolute coordinates
198         */
199         param = lifebook_use_6byte_proto ? 0x08 : 0x07;
200         ps2_command(ps2dev, &param, PSMOUSE_CMD_SETRES);
201
202         return 0;
203 }
204
205 static void lifebook_relative_mode(struct psmouse *psmouse)
206 {
207         struct ps2dev *ps2dev = &psmouse->ps2dev;
208         unsigned char param = 0x06;
209
210         ps2_command(ps2dev, &param, PSMOUSE_CMD_SETRES);
211 }
212
213 static void lifebook_set_resolution(struct psmouse *psmouse, unsigned int resolution)
214 {
215         static const unsigned char params[] = { 0, 1, 2, 2, 3 };
216         unsigned char p;
217
218         if (resolution == 0 || resolution > 400)
219                 resolution = 400;
220
221         p = params[resolution / 100];
222         ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
223         psmouse->resolution = 50 << p;
224 }
225
226 static void lifebook_disconnect(struct psmouse *psmouse)
227 {
228         struct lifebook_data *priv = psmouse->private;
229
230         psmouse_reset(psmouse);
231         if (priv) {
232                 input_unregister_device(priv->dev2);
233                 kfree(priv);
234         }
235         psmouse->private = NULL;
236 }
237
238 int lifebook_detect(struct psmouse *psmouse, int set_properties)
239 {
240         if (!dmi_check_system(lifebook_dmi_table))
241                 return -1;
242
243         if (desired_serio_phys &&
244             strcmp(psmouse->ps2dev.serio->phys, desired_serio_phys))
245                 return -1;
246
247         if (set_properties) {
248                 psmouse->vendor = "Fujitsu";
249                 psmouse->name = "Lifebook TouchScreen";
250         }
251
252         return 0;
253 }
254
255 static int lifebook_create_relative_device(struct psmouse *psmouse)
256 {
257         struct input_dev *dev2;
258         struct lifebook_data *priv;
259         int error = -ENOMEM;
260
261         priv = kzalloc(sizeof(struct lifebook_data), GFP_KERNEL);
262         dev2 = input_allocate_device();
263         if (!priv || !dev2)
264                 goto err_out;
265
266         priv->dev2 = dev2;
267         snprintf(priv->phys, sizeof(priv->phys),
268                  "%s/input1", psmouse->ps2dev.serio->phys);
269
270         dev2->phys = priv->phys;
271         dev2->name = "PS/2 Touchpad";
272         dev2->id.bustype = BUS_I8042;
273         dev2->id.vendor  = 0x0002;
274         dev2->id.product = PSMOUSE_LIFEBOOK;
275         dev2->id.version = 0x0000;
276         dev2->dev.parent = &psmouse->ps2dev.serio->dev;
277
278         dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
279         dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
280         dev2->keybit[BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) |
281                 BIT_MASK(BTN_RIGHT);
282
283         error = input_register_device(priv->dev2);
284         if (error)
285                 goto err_out;
286
287         psmouse->private = priv;
288         return 0;
289
290  err_out:
291         input_free_device(dev2);
292         kfree(priv);
293         return error;
294 }
295
296 int lifebook_init(struct psmouse *psmouse)
297 {
298         struct input_dev *dev1 = psmouse->dev;
299         int max_coord = lifebook_use_6byte_proto ? 4096 : 1024;
300
301         if (lifebook_absolute_mode(psmouse))
302                 return -1;
303
304         dev1->evbit[0] = BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY);
305         dev1->relbit[0] = 0;
306         dev1->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
307         input_set_abs_params(dev1, ABS_X, 0, max_coord, 0, 0);
308         input_set_abs_params(dev1, ABS_Y, 0, max_coord, 0, 0);
309
310         if (!desired_serio_phys) {
311                 if (lifebook_create_relative_device(psmouse)) {
312                         lifebook_relative_mode(psmouse);
313                         return -1;
314                 }
315         }
316
317         psmouse->protocol_handler = lifebook_process_byte;
318         psmouse->set_resolution = lifebook_set_resolution;
319         psmouse->disconnect = lifebook_disconnect;
320         psmouse->reconnect  = lifebook_absolute_mode;
321
322         psmouse->model = lifebook_use_6byte_proto ? 6 : 3;
323
324         /*
325          * Use packet size = 3 even when using 6-byte protocol because
326          * that's what POLL will return on Lifebooks (according to spec).
327          */
328         psmouse->pktsize = 3;
329
330         return 0;
331 }
332