driver core: Implement ns directory support for device classes.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / touchscreen / hampshire.c
blob2da6cc31bb2187f0b62770bd09aa6c0923c36687
1 /*
2 * Hampshire serial touchscreen driver
4 * Copyright (c) 2010 Adam Bennett
5 * Based on the dynapro driver (c) Tias Guns
7 */
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.
16 * 2010/04/08 Adam Bennett <abennett72@gmail.com>
17 * Copied dynapro.c and edited for Hampshire 4-byte protocol
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/input.h>
25 #include <linux/serio.h>
26 #include <linux/init.h>
28 #define DRIVER_DESC "Hampshire serial touchscreen driver"
30 MODULE_AUTHOR("Adam Bennett <abennett72@gmail.com>");
31 MODULE_DESCRIPTION(DRIVER_DESC);
32 MODULE_LICENSE("GPL");
35 * Definitions & global arrays.
38 #define HAMPSHIRE_FORMAT_TOUCH_BIT 0x40
39 #define HAMPSHIRE_FORMAT_LENGTH 4
40 #define HAMPSHIRE_RESPONSE_BEGIN_BYTE 0x80
42 #define HAMPSHIRE_MIN_XC 0
43 #define HAMPSHIRE_MAX_XC 0x1000
44 #define HAMPSHIRE_MIN_YC 0
45 #define HAMPSHIRE_MAX_YC 0x1000
47 #define HAMPSHIRE_GET_XC(data) (((data[3] & 0x0c) >> 2) | (data[1] << 2) | ((data[0] & 0x38) << 6))
48 #define HAMPSHIRE_GET_YC(data) ((data[3] & 0x03) | (data[2] << 2) | ((data[0] & 0x07) << 9))
49 #define HAMPSHIRE_GET_TOUCHED(data) (HAMPSHIRE_FORMAT_TOUCH_BIT & data[0])
52 * Per-touchscreen data.
55 struct hampshire {
56 struct input_dev *dev;
57 struct serio *serio;
58 int idx;
59 unsigned char data[HAMPSHIRE_FORMAT_LENGTH];
60 char phys[32];
63 static void hampshire_process_data(struct hampshire *phampshire)
65 struct input_dev *dev = phampshire->dev;
67 if (HAMPSHIRE_FORMAT_LENGTH == ++phampshire->idx) {
68 input_report_abs(dev, ABS_X, HAMPSHIRE_GET_XC(phampshire->data));
69 input_report_abs(dev, ABS_Y, HAMPSHIRE_GET_YC(phampshire->data));
70 input_report_key(dev, BTN_TOUCH,
71 HAMPSHIRE_GET_TOUCHED(phampshire->data));
72 input_sync(dev);
74 phampshire->idx = 0;
78 static irqreturn_t hampshire_interrupt(struct serio *serio,
79 unsigned char data, unsigned int flags)
81 struct hampshire *phampshire = serio_get_drvdata(serio);
83 phampshire->data[phampshire->idx] = data;
85 if (HAMPSHIRE_RESPONSE_BEGIN_BYTE & phampshire->data[0])
86 hampshire_process_data(phampshire);
87 else
88 dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
89 phampshire->data[0]);
91 return IRQ_HANDLED;
94 static void hampshire_disconnect(struct serio *serio)
96 struct hampshire *phampshire = serio_get_drvdata(serio);
98 input_get_device(phampshire->dev);
99 input_unregister_device(phampshire->dev);
100 serio_close(serio);
101 serio_set_drvdata(serio, NULL);
102 input_put_device(phampshire->dev);
103 kfree(phampshire);
107 * hampshire_connect() is the routine that is called when someone adds a
108 * new serio device that supports hampshire protocol and registers it as
109 * an input device. This is usually accomplished using inputattach.
112 static int hampshire_connect(struct serio *serio, struct serio_driver *drv)
114 struct hampshire *phampshire;
115 struct input_dev *input_dev;
116 int err;
118 phampshire = kzalloc(sizeof(struct hampshire), GFP_KERNEL);
119 input_dev = input_allocate_device();
120 if (!phampshire || !input_dev) {
121 err = -ENOMEM;
122 goto fail1;
125 phampshire->serio = serio;
126 phampshire->dev = input_dev;
127 snprintf(phampshire->phys, sizeof(phampshire->phys),
128 "%s/input0", serio->phys);
130 input_dev->name = "Hampshire Serial TouchScreen";
131 input_dev->phys = phampshire->phys;
132 input_dev->id.bustype = BUS_RS232;
133 input_dev->id.vendor = SERIO_HAMPSHIRE;
134 input_dev->id.product = 0;
135 input_dev->id.version = 0x0001;
136 input_dev->dev.parent = &serio->dev;
137 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
138 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
139 input_set_abs_params(phampshire->dev, ABS_X,
140 HAMPSHIRE_MIN_XC, HAMPSHIRE_MAX_XC, 0, 0);
141 input_set_abs_params(phampshire->dev, ABS_Y,
142 HAMPSHIRE_MIN_YC, HAMPSHIRE_MAX_YC, 0, 0);
144 serio_set_drvdata(serio, phampshire);
146 err = serio_open(serio, drv);
147 if (err)
148 goto fail2;
150 err = input_register_device(phampshire->dev);
151 if (err)
152 goto fail3;
154 return 0;
156 fail3: serio_close(serio);
157 fail2: serio_set_drvdata(serio, NULL);
158 fail1: input_free_device(input_dev);
159 kfree(phampshire);
160 return err;
164 * The serio driver structure.
167 static struct serio_device_id hampshire_serio_ids[] = {
169 .type = SERIO_RS232,
170 .proto = SERIO_HAMPSHIRE,
171 .id = SERIO_ANY,
172 .extra = SERIO_ANY,
174 { 0 }
177 MODULE_DEVICE_TABLE(serio, hampshire_serio_ids);
179 static struct serio_driver hampshire_drv = {
180 .driver = {
181 .name = "hampshire",
183 .description = DRIVER_DESC,
184 .id_table = hampshire_serio_ids,
185 .interrupt = hampshire_interrupt,
186 .connect = hampshire_connect,
187 .disconnect = hampshire_disconnect,
191 * The functions for inserting/removing us as a module.
194 static int __init hampshire_init(void)
196 return serio_register_driver(&hampshire_drv);
199 static void __exit hampshire_exit(void)
201 serio_unregister_driver(&hampshire_drv);
204 module_init(hampshire_init);
205 module_exit(hampshire_exit);