Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * itmtouch.c -- Driver for ITM touchscreen panel |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | * |
| 18 | * Based upon original work by Chris Collins <xfire-itmtouch@xware.cx>. |
| 19 | * |
| 20 | * Kudos to ITM for providing me with the datasheet for the panel, |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 21 | * even though it was a day later than I had finished writing this |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 22 | * driver. |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 23 | * |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 24 | * It has meant that I've been able to correct my interpretation of the |
| 25 | * protocol packets however. |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 26 | * |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 27 | * CC -- 2003/9/29 |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 28 | * |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 29 | * History |
| 30 | * 1.0 & 1.1 2003 (CC) vojtech@suse.cz |
| 31 | * Original version for 2.4.x kernels |
| 32 | * |
| 33 | * 1.2 02/03/2005 (HCE) hc@mivu.no |
| 34 | * Complete rewrite to support Linux 2.6.10, thanks to mtouchusb.c for hints. |
| 35 | * Unfortunately no calibration support at this time. |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 36 | * |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 37 | * 1.2.1 09/03/2005 (HCE) hc@mivu.no |
| 38 | * Code cleanup and adjusting syntax to start matching kernel standards |
Mark Assad | 77dc2db | 2006-10-05 12:25:05 +1000 | [diff] [blame^] | 39 | * |
| 40 | * 1.2.2 10/05/2006 (MJA) massad@gmail.com |
| 41 | * Flag for detecting if the screen was being touch was incorrectly |
| 42 | * inverted, so no touch events were being detected. |
| 43 | * |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 44 | *****************************************************************************/ |
| 45 | |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 46 | #include <linux/kernel.h> |
| 47 | #include <linux/slab.h> |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 48 | #include <linux/module.h> |
| 49 | #include <linux/init.h> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 50 | #include <linux/usb/input.h> |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 51 | |
| 52 | /* only an 8 byte buffer necessary for a single packet */ |
| 53 | #define ITM_BUFSIZE 8 |
| 54 | #define PATH_SIZE 64 |
| 55 | |
| 56 | #define USB_VENDOR_ID_ITMINC 0x0403 |
| 57 | #define USB_PRODUCT_ID_TOUCHPANEL 0xf9e9 |
| 58 | |
| 59 | #define DRIVER_AUTHOR "Hans-Christian Egtvedt <hc@mivu.no>" |
Mark Assad | 77dc2db | 2006-10-05 12:25:05 +1000 | [diff] [blame^] | 60 | #define DRIVER_VERSION "v1.2.2" |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 61 | #define DRIVER_DESC "USB ITM Inc Touch Panel Driver" |
| 62 | #define DRIVER_LICENSE "GPL" |
| 63 | |
| 64 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 65 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 66 | MODULE_LICENSE( DRIVER_LICENSE ); |
| 67 | |
| 68 | struct itmtouch_dev { |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 69 | struct usb_device *usbdev; /* usb device */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 70 | struct input_dev *inputdev; /* input device */ |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 71 | struct urb *readurb; /* urb */ |
| 72 | char rbuf[ITM_BUFSIZE]; /* data */ |
| 73 | int users; |
| 74 | char name[128]; |
| 75 | char phys[64]; |
| 76 | }; |
| 77 | |
| 78 | static struct usb_device_id itmtouch_ids [] = { |
| 79 | { USB_DEVICE(USB_VENDOR_ID_ITMINC, USB_PRODUCT_ID_TOUCHPANEL) }, |
| 80 | { } |
| 81 | }; |
| 82 | |
| 83 | static void itmtouch_irq(struct urb *urb, struct pt_regs *regs) |
| 84 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 85 | struct itmtouch_dev *itmtouch = urb->context; |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 86 | unsigned char *data = urb->transfer_buffer; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 87 | struct input_dev *dev = itmtouch->inputdev; |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 88 | int retval; |
| 89 | |
| 90 | switch (urb->status) { |
| 91 | case 0: |
| 92 | /* success */ |
| 93 | break; |
Pete Zaitcev | 38e2bfc | 2006-09-18 22:49:02 -0700 | [diff] [blame] | 94 | case -ETIME: |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 95 | /* this urb is timing out */ |
| 96 | dbg("%s - urb timed out - was the device unplugged?", |
| 97 | __FUNCTION__); |
| 98 | return; |
| 99 | case -ECONNRESET: |
| 100 | case -ENOENT: |
| 101 | case -ESHUTDOWN: |
| 102 | /* this urb is terminated, clean up */ |
| 103 | dbg("%s - urb shutting down with status: %d", |
| 104 | __FUNCTION__, urb->status); |
| 105 | return; |
| 106 | default: |
| 107 | dbg("%s - nonzero urb status received: %d", |
| 108 | __FUNCTION__, urb->status); |
| 109 | goto exit; |
| 110 | } |
| 111 | |
| 112 | input_regs(dev, regs); |
| 113 | |
| 114 | /* if pressure has been released, then don't report X/Y */ |
Mark Assad | 77dc2db | 2006-10-05 12:25:05 +1000 | [diff] [blame^] | 115 | if (!(data[7] & 0x20)) { |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 116 | input_report_abs(dev, ABS_X, (data[0] & 0x1F) << 7 | (data[3] & 0x7F)); |
| 117 | input_report_abs(dev, ABS_Y, (data[1] & 0x1F) << 7 | (data[4] & 0x7F)); |
| 118 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 119 | |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 120 | input_report_abs(dev, ABS_PRESSURE, (data[2] & 1) << 7 | (data[5] & 0x7F)); |
| 121 | input_report_key(dev, BTN_TOUCH, ~data[7] & 0x20); |
| 122 | input_sync(dev); |
| 123 | |
| 124 | exit: |
| 125 | retval = usb_submit_urb (urb, GFP_ATOMIC); |
| 126 | if (retval) |
| 127 | printk(KERN_ERR "%s - usb_submit_urb failed with result: %d", |
| 128 | __FUNCTION__, retval); |
| 129 | } |
| 130 | |
| 131 | static int itmtouch_open(struct input_dev *input) |
| 132 | { |
| 133 | struct itmtouch_dev *itmtouch = input->private; |
| 134 | |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 135 | itmtouch->readurb->dev = itmtouch->usbdev; |
| 136 | |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 137 | if (usb_submit_urb(itmtouch->readurb, GFP_KERNEL)) |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 138 | return -EIO; |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static void itmtouch_close(struct input_dev *input) |
| 144 | { |
| 145 | struct itmtouch_dev *itmtouch = input->private; |
| 146 | |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 147 | usb_kill_urb(itmtouch->readurb); |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int itmtouch_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 151 | { |
| 152 | struct itmtouch_dev *itmtouch; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 153 | struct input_dev *input_dev; |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 154 | struct usb_host_interface *interface; |
| 155 | struct usb_endpoint_descriptor *endpoint; |
| 156 | struct usb_device *udev = interface_to_usbdev(intf); |
| 157 | unsigned int pipe; |
| 158 | unsigned int maxp; |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 159 | |
| 160 | interface = intf->cur_altsetting; |
| 161 | endpoint = &interface->endpoint[0].desc; |
| 162 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 163 | itmtouch = kzalloc(sizeof(struct itmtouch_dev), GFP_KERNEL); |
| 164 | input_dev = input_allocate_device(); |
| 165 | if (!itmtouch || !input_dev) { |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 166 | err("%s - Out of memory.", __FUNCTION__); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 167 | goto fail; |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | itmtouch->usbdev = udev; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 171 | itmtouch->inputdev = input_dev; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 172 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 173 | if (udev->manufacturer) |
| 174 | strlcpy(itmtouch->name, udev->manufacturer, sizeof(itmtouch->name)); |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 175 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 176 | if (udev->product) { |
| 177 | if (udev->manufacturer) |
| 178 | strlcat(itmtouch->name, " ", sizeof(itmtouch->name)); |
| 179 | strlcat(itmtouch->name, udev->product, sizeof(itmtouch->name)); |
| 180 | } |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 181 | |
| 182 | if (!strlen(itmtouch->name)) |
| 183 | sprintf(itmtouch->name, "USB ITM touchscreen"); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 184 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 185 | usb_make_path(udev, itmtouch->phys, sizeof(itmtouch->phys)); |
| 186 | strlcpy(itmtouch->phys, "/input0", sizeof(itmtouch->phys)); |
| 187 | |
| 188 | input_dev->name = itmtouch->name; |
| 189 | input_dev->phys = itmtouch->phys; |
| 190 | usb_to_input_id(udev, &input_dev->id); |
| 191 | input_dev->cdev.dev = &intf->dev; |
| 192 | input_dev->private = itmtouch; |
| 193 | |
| 194 | input_dev->open = itmtouch_open; |
| 195 | input_dev->close = itmtouch_close; |
| 196 | |
| 197 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
| 198 | input_dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE); |
| 199 | input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH); |
| 200 | |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 201 | /* device limits */ |
| 202 | /* as specified by the ITM datasheet, X and Y are 12bit, |
| 203 | * Z (pressure) is 8 bit. However, the fields are defined up |
| 204 | * to 14 bits for future possible expansion. |
| 205 | */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 206 | input_set_abs_params(input_dev, ABS_X, 0, 0x0FFF, 2, 0); |
| 207 | input_set_abs_params(input_dev, ABS_Y, 0, 0x0FFF, 2, 0); |
| 208 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xFF, 2, 0); |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 209 | |
| 210 | /* initialise the URB so we can read from the transport stream */ |
| 211 | pipe = usb_rcvintpipe(itmtouch->usbdev, endpoint->bEndpointAddress); |
| 212 | maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe)); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 213 | |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 214 | if (maxp > ITM_BUFSIZE) |
| 215 | maxp = ITM_BUFSIZE; |
| 216 | |
| 217 | itmtouch->readurb = usb_alloc_urb(0, GFP_KERNEL); |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 218 | if (!itmtouch->readurb) { |
| 219 | dbg("%s - usb_alloc_urb failed: itmtouch->readurb", __FUNCTION__); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 220 | goto fail; |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 221 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 222 | |
| 223 | usb_fill_int_urb(itmtouch->readurb, itmtouch->usbdev, pipe, itmtouch->rbuf, |
| 224 | maxp, itmtouch_irq, itmtouch, endpoint->bInterval); |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 225 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 226 | input_register_device(itmtouch->inputdev); |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 227 | |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 228 | usb_set_intfdata(intf, itmtouch); |
| 229 | |
| 230 | return 0; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 231 | |
| 232 | fail: input_free_device(input_dev); |
| 233 | kfree(itmtouch); |
| 234 | return -ENOMEM; |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void itmtouch_disconnect(struct usb_interface *intf) |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 238 | { |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 239 | struct itmtouch_dev *itmtouch = usb_get_intfdata(intf); |
| 240 | |
| 241 | usb_set_intfdata(intf, NULL); |
| 242 | |
| 243 | if (itmtouch) { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 244 | input_unregister_device(itmtouch->inputdev); |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 245 | usb_kill_urb(itmtouch->readurb); |
| 246 | usb_free_urb(itmtouch->readurb); |
| 247 | kfree(itmtouch); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | MODULE_DEVICE_TABLE(usb, itmtouch_ids); |
| 252 | |
| 253 | static struct usb_driver itmtouch_driver = { |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 254 | .name = "itmtouch", |
| 255 | .probe = itmtouch_probe, |
| 256 | .disconnect = itmtouch_disconnect, |
| 257 | .id_table = itmtouch_ids, |
Hans-Christian Egtvedt | 8608471 | 2005-05-29 02:27:45 -0500 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | static int __init itmtouch_init(void) |
| 261 | { |
| 262 | info(DRIVER_DESC " " DRIVER_VERSION); |
| 263 | info(DRIVER_AUTHOR); |
| 264 | return usb_register(&itmtouch_driver); |
| 265 | } |
| 266 | |
| 267 | static void __exit itmtouch_exit(void) |
| 268 | { |
| 269 | usb_deregister(&itmtouch_driver); |
| 270 | } |
| 271 | |
| 272 | module_init(itmtouch_init); |
| 273 | module_exit(itmtouch_exit); |