13fbda0895b51a94904c89b707e01a896b820bdf
[deliverable/linux.git] / drivers / input / touchscreen / usbtouchscreen.c
1 /******************************************************************************
2 * usbtouchscreen.c
3 * Driver for USB Touchscreens, supporting those devices:
4 * - eGalax Touchkit
5 * includes eTurboTouch CT-410/510/700
6 * - 3M/Microtouch EX II series
7 * - ITM
8 * - PanJit TouchSet
9 * - eTurboTouch
10 * - Gunze AHL61
11 * - DMC TSC-10/25
12 * - IRTOUCHSYSTEMS/UNITOP
13 * - IdealTEK URTC1000
14 *
15 * Copyright (C) 2004-2006 by Daniel Ritz <daniel.ritz@gmx.ch>
16 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of the
21 * License, or (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 *
32 * Driver is based on touchkitusb.c
33 * - ITM parts are from itmtouch.c
34 * - 3M parts are from mtouchusb.c
35 * - PanJit parts are from an unmerged driver by Lanslott Gish
36 * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
37 * driver from Marius Vollmer
38 *
39 *****************************************************************************/
40
41 //#define DEBUG
42
43 #include <linux/kernel.h>
44 #include <linux/slab.h>
45 #include <linux/input.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/usb.h>
49 #include <linux/usb/input.h>
50
51
52 #define DRIVER_VERSION "v0.5"
53 #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
54 #define DRIVER_DESC "USB Touchscreen Driver"
55
56 static int swap_xy;
57 module_param(swap_xy, bool, 0644);
58 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
59
60 /* device specifc data/functions */
61 struct usbtouch_usb;
62 struct usbtouch_device_info {
63 int min_xc, max_xc;
64 int min_yc, max_yc;
65 int min_press, max_press;
66 int rept_size;
67 int flags;
68
69 void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
70 int (*get_pkt_len) (unsigned char *pkt, int len);
71 int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
72 int (*init) (struct usbtouch_usb *usbtouch);
73 };
74
75 #define USBTOUCH_FLG_BUFFER 0x01
76
77
78 /* a usbtouch device */
79 struct usbtouch_usb {
80 unsigned char *data;
81 dma_addr_t data_dma;
82 unsigned char *buffer;
83 int buf_len;
84 struct urb *irq;
85 struct usb_device *udev;
86 struct input_dev *input;
87 struct usbtouch_device_info *type;
88 char name[128];
89 char phys[64];
90
91 int x, y;
92 int touch, press;
93 };
94
95
96 #if defined(CONFIG_TOUCHSCREEN_USB_EGALAX) || defined(CONFIG_TOUCHSCREEN_USB_ETURBO) || defined(CONFIG_TOUCHSCREEN_USB_IDEALTEK)
97 #define MULTI_PACKET
98 #endif
99
100 #ifdef MULTI_PACKET
101 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
102 unsigned char *pkt, int len);
103 #endif
104
105 /* device types */
106 enum {
107 DEVTPYE_DUMMY = -1,
108 DEVTYPE_EGALAX,
109 DEVTYPE_PANJIT,
110 DEVTYPE_3M,
111 DEVTYPE_ITM,
112 DEVTYPE_ETURBO,
113 DEVTYPE_GUNZE,
114 DEVTYPE_DMC_TSC10,
115 DEVTYPE_IRTOUCH,
116 DEVTYPE_IDEALTEK,
117 };
118
119 static struct usb_device_id usbtouch_devices[] = {
120 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
121 {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
122 {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
123 {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
124 {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
125 {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
126 {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
127 {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
128 #endif
129
130 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
131 {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
132 {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
133 {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
134 {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
135 #endif
136
137 #ifdef CONFIG_TOUCHSCREEN_USB_3M
138 {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
139 #endif
140
141 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
142 {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
143 #endif
144
145 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
146 {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
147 #endif
148
149 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
150 {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
151 #endif
152
153 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
154 {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
155 #endif
156
157 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
158 {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
159 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
160 #endif
161
162 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
163 {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
164 #endif
165
166 {}
167 };
168
169
170 /*****************************************************************************
171 * eGalax part
172 */
173
174 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
175
176 #define EGALAX_PKT_TYPE_MASK 0xFE
177 #define EGALAX_PKT_TYPE_REPT 0x80
178 #define EGALAX_PKT_TYPE_DIAG 0x0A
179
180 static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
181 {
182 if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
183 return 0;
184
185 dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
186 dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
187 dev->touch = pkt[0] & 0x01;
188
189 return 1;
190 }
191
192 static int egalax_get_pkt_len(unsigned char *buf, int len)
193 {
194 switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
195 case EGALAX_PKT_TYPE_REPT:
196 return 5;
197
198 case EGALAX_PKT_TYPE_DIAG:
199 if (len < 2)
200 return -1;
201
202 return buf[1] + 2;
203 }
204
205 return 0;
206 }
207 #endif
208
209
210 /*****************************************************************************
211 * PanJit Part
212 */
213 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
214 static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
215 {
216 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
217 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
218 dev->touch = pkt[0] & 0x01;
219
220 return 1;
221 }
222 #endif
223
224
225 /*****************************************************************************
226 * 3M/Microtouch Part
227 */
228 #ifdef CONFIG_TOUCHSCREEN_USB_3M
229
230 #define MTOUCHUSB_ASYNC_REPORT 1
231 #define MTOUCHUSB_RESET 7
232 #define MTOUCHUSB_REQ_CTRLLR_ID 10
233
234 static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
235 {
236 dev->x = (pkt[8] << 8) | pkt[7];
237 dev->y = (pkt[10] << 8) | pkt[9];
238 dev->touch = (pkt[2] & 0x40) ? 1 : 0;
239
240 return 1;
241 }
242
243 static int mtouch_init(struct usbtouch_usb *usbtouch)
244 {
245 int ret, i;
246
247 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
248 MTOUCHUSB_RESET,
249 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
250 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
251 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
252 __FUNCTION__, ret);
253 if (ret < 0)
254 return ret;
255 msleep(150);
256
257 for (i = 0; i < 3; i++) {
258 ret = usb_control_msg(usbtouch->udev, usb_rcvctrlpipe(usbtouch->udev, 0),
259 MTOUCHUSB_ASYNC_REPORT,
260 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
261 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
262 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
263 __FUNCTION__, ret);
264 if (ret >= 0)
265 break;
266 if (ret != -EPIPE)
267 return ret;
268 }
269
270 return 0;
271 }
272 #endif
273
274
275 /*****************************************************************************
276 * ITM Part
277 */
278 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
279 static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
280 {
281 int touch;
282 /*
283 * ITM devices report invalid x/y data if not touched.
284 * if the screen was touched before but is not touched any more
285 * report touch as 0 with the last valid x/y data once. then stop
286 * reporting data until touched again.
287 */
288 dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
289
290 touch = ~pkt[7] & 0x20;
291 if (!touch) {
292 if (dev->touch) {
293 dev->touch = 0;
294 return 1;
295 }
296
297 return 0;
298 }
299
300 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
301 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
302 dev->touch = touch;
303
304 return 1;
305 }
306 #endif
307
308
309 /*****************************************************************************
310 * eTurboTouch part
311 */
312 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
313 static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
314 {
315 unsigned int shift;
316
317 /* packets should start with sync */
318 if (!(pkt[0] & 0x80))
319 return 0;
320
321 shift = (6 - (pkt[0] & 0x03));
322 dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
323 dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
324 dev->touch = (pkt[0] & 0x10) ? 1 : 0;
325
326 return 1;
327 }
328
329 static int eturbo_get_pkt_len(unsigned char *buf, int len)
330 {
331 if (buf[0] & 0x80)
332 return 5;
333 if (buf[0] == 0x01)
334 return 3;
335 return 0;
336 }
337 #endif
338
339
340 /*****************************************************************************
341 * Gunze part
342 */
343 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
344 static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
345 {
346 if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
347 return 0;
348
349 dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
350 dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
351 dev->touch = pkt[0] & 0x20;
352
353 return 1;
354 }
355 #endif
356
357 /*****************************************************************************
358 * DMC TSC-10/25 Part
359 *
360 * Documentation about the controller and it's protocol can be found at
361 * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
362 * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
363 */
364 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
365
366 /* supported data rates. currently using 130 */
367 #define TSC10_RATE_POINT 0x50
368 #define TSC10_RATE_30 0x40
369 #define TSC10_RATE_50 0x41
370 #define TSC10_RATE_80 0x42
371 #define TSC10_RATE_100 0x43
372 #define TSC10_RATE_130 0x44
373 #define TSC10_RATE_150 0x45
374
375 /* commands */
376 #define TSC10_CMD_RESET 0x55
377 #define TSC10_CMD_RATE 0x05
378 #define TSC10_CMD_DATA1 0x01
379
380 static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
381 {
382 struct usb_device *dev = usbtouch->udev;
383 int ret;
384 unsigned char buf[2];
385
386 /* reset */
387 buf[0] = buf[1] = 0xFF;
388 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
389 TSC10_CMD_RESET,
390 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
391 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
392 if (ret < 0)
393 return ret;
394 if (buf[0] != 0x06 || buf[1] != 0x00)
395 return -ENODEV;
396
397 /* set coordinate output rate */
398 buf[0] = buf[1] = 0xFF;
399 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
400 TSC10_CMD_RATE,
401 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
402 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
403 if (ret < 0)
404 return ret;
405 if ((buf[0] != 0x06 || buf[1] != 0x00) &&
406 (buf[0] != 0x15 || buf[1] != 0x01))
407 return -ENODEV;
408
409 /* start sending data */
410 ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
411 TSC10_CMD_DATA1,
412 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
413 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
414 if (ret < 0)
415 return ret;
416
417 return 0;
418 }
419
420
421 static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
422 {
423 dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
424 dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
425 dev->touch = pkt[0] & 0x01;
426
427 return 1;
428 }
429 #endif
430
431
432 /*****************************************************************************
433 * IRTOUCH Part
434 */
435 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
436 static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
437 {
438 dev->x = (pkt[3] << 8) | pkt[2];
439 dev->y = (pkt[5] << 8) | pkt[4];
440 dev->touch = (pkt[1] & 0x03) ? 1 : 0;
441
442 return 1;
443 }
444 #endif
445
446
447 /*****************************************************************************
448 * IdealTEK URTC1000 Part
449 */
450 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
451 static int idealtek_get_pkt_len(unsigned char *buf, int len)
452 {
453 if (buf[0] & 0x80)
454 return 5;
455 if (buf[0] == 0x01)
456 return len;
457 return 0;
458 }
459
460 static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
461 {
462 switch (pkt[0] & 0x98) {
463 case 0x88:
464 /* touch data in IdealTEK mode */
465 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
466 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
467 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
468 return 1;
469
470 case 0x98:
471 /* touch data in MT emulation mode */
472 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
473 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
474 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
475 return 1;
476
477 default:
478 return 0;
479 }
480 }
481 #endif
482
483
484 /*****************************************************************************
485 * the different device descriptors
486 */
487 static struct usbtouch_device_info usbtouch_dev_info[] = {
488 #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
489 [DEVTYPE_EGALAX] = {
490 .min_xc = 0x0,
491 .max_xc = 0x07ff,
492 .min_yc = 0x0,
493 .max_yc = 0x07ff,
494 .rept_size = 16,
495 .flags = USBTOUCH_FLG_BUFFER,
496 .process_pkt = usbtouch_process_multi,
497 .get_pkt_len = egalax_get_pkt_len,
498 .read_data = egalax_read_data,
499 },
500 #endif
501
502 #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
503 [DEVTYPE_PANJIT] = {
504 .min_xc = 0x0,
505 .max_xc = 0x0fff,
506 .min_yc = 0x0,
507 .max_yc = 0x0fff,
508 .rept_size = 8,
509 .read_data = panjit_read_data,
510 },
511 #endif
512
513 #ifdef CONFIG_TOUCHSCREEN_USB_3M
514 [DEVTYPE_3M] = {
515 .min_xc = 0x0,
516 .max_xc = 0x4000,
517 .min_yc = 0x0,
518 .max_yc = 0x4000,
519 .rept_size = 11,
520 .read_data = mtouch_read_data,
521 .init = mtouch_init,
522 },
523 #endif
524
525 #ifdef CONFIG_TOUCHSCREEN_USB_ITM
526 [DEVTYPE_ITM] = {
527 .min_xc = 0x0,
528 .max_xc = 0x0fff,
529 .min_yc = 0x0,
530 .max_yc = 0x0fff,
531 .max_press = 0xff,
532 .rept_size = 8,
533 .read_data = itm_read_data,
534 },
535 #endif
536
537 #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
538 [DEVTYPE_ETURBO] = {
539 .min_xc = 0x0,
540 .max_xc = 0x07ff,
541 .min_yc = 0x0,
542 .max_yc = 0x07ff,
543 .rept_size = 8,
544 .flags = USBTOUCH_FLG_BUFFER,
545 .process_pkt = usbtouch_process_multi,
546 .get_pkt_len = eturbo_get_pkt_len,
547 .read_data = eturbo_read_data,
548 },
549 #endif
550
551 #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
552 [DEVTYPE_GUNZE] = {
553 .min_xc = 0x0,
554 .max_xc = 0x0fff,
555 .min_yc = 0x0,
556 .max_yc = 0x0fff,
557 .rept_size = 4,
558 .read_data = gunze_read_data,
559 },
560 #endif
561
562 #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
563 [DEVTYPE_DMC_TSC10] = {
564 .min_xc = 0x0,
565 .max_xc = 0x03ff,
566 .min_yc = 0x0,
567 .max_yc = 0x03ff,
568 .rept_size = 5,
569 .init = dmc_tsc10_init,
570 .read_data = dmc_tsc10_read_data,
571 },
572 #endif
573
574 #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
575 [DEVTYPE_IRTOUCH] = {
576 .min_xc = 0x0,
577 .max_xc = 0x0fff,
578 .min_yc = 0x0,
579 .max_yc = 0x0fff,
580 .rept_size = 8,
581 .read_data = irtouch_read_data,
582 },
583 #endif
584
585 #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
586 [DEVTYPE_IDEALTEK] = {
587 .min_xc = 0x0,
588 .max_xc = 0x0fff,
589 .min_yc = 0x0,
590 .max_yc = 0x0fff,
591 .rept_size = 8,
592 .flags = USBTOUCH_FLG_BUFFER,
593 .process_pkt = usbtouch_process_multi,
594 .get_pkt_len = idealtek_get_pkt_len,
595 .read_data = idealtek_read_data,
596 },
597 #endif
598 };
599
600
601 /*****************************************************************************
602 * Generic Part
603 */
604 static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
605 unsigned char *pkt, int len)
606 {
607 struct usbtouch_device_info *type = usbtouch->type;
608
609 if (!type->read_data(usbtouch, pkt))
610 return;
611
612 input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
613
614 if (swap_xy) {
615 input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
616 input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
617 } else {
618 input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
619 input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
620 }
621 if (type->max_press)
622 input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
623 input_sync(usbtouch->input);
624 }
625
626
627 #ifdef MULTI_PACKET
628 static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
629 unsigned char *pkt, int len)
630 {
631 unsigned char *buffer;
632 int pkt_len, pos, buf_len, tmp;
633
634 /* process buffer */
635 if (unlikely(usbtouch->buf_len)) {
636 /* try to get size */
637 pkt_len = usbtouch->type->get_pkt_len(
638 usbtouch->buffer, usbtouch->buf_len);
639
640 /* drop? */
641 if (unlikely(!pkt_len))
642 goto out_flush_buf;
643
644 /* need to append -pkt_len bytes before able to get size */
645 if (unlikely(pkt_len < 0)) {
646 int append = -pkt_len;
647 if (unlikely(append > len))
648 append = len;
649 if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
650 goto out_flush_buf;
651 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
652 usbtouch->buf_len += append;
653
654 pkt_len = usbtouch->type->get_pkt_len(
655 usbtouch->buffer, usbtouch->buf_len);
656 if (pkt_len < 0)
657 return;
658 }
659
660 /* append */
661 tmp = pkt_len - usbtouch->buf_len;
662 if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
663 goto out_flush_buf;
664 memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
665 usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
666
667 buffer = pkt + tmp;
668 buf_len = len - tmp;
669 } else {
670 buffer = pkt;
671 buf_len = len;
672 }
673
674 /* loop over the received packet, process */
675 pos = 0;
676 while (pos < buf_len) {
677 /* get packet len */
678 pkt_len = usbtouch->type->get_pkt_len(buffer + pos, len);
679
680 /* unknown packet: drop everything */
681 if (unlikely(!pkt_len))
682 goto out_flush_buf;
683
684 /* full packet: process */
685 if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
686 usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
687 } else {
688 /* incomplete packet: save in buffer */
689 memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
690 usbtouch->buf_len = buf_len - pos;
691 return;
692 }
693 pos += pkt_len;
694 }
695
696 out_flush_buf:
697 usbtouch->buf_len = 0;
698 return;
699 }
700 #endif
701
702
703 static void usbtouch_irq(struct urb *urb)
704 {
705 struct usbtouch_usb *usbtouch = urb->context;
706 int retval;
707
708 switch (urb->status) {
709 case 0:
710 /* success */
711 break;
712 case -ETIME:
713 /* this urb is timing out */
714 dbg("%s - urb timed out - was the device unplugged?",
715 __FUNCTION__);
716 return;
717 case -ECONNRESET:
718 case -ENOENT:
719 case -ESHUTDOWN:
720 /* this urb is terminated, clean up */
721 dbg("%s - urb shutting down with status: %d",
722 __FUNCTION__, urb->status);
723 return;
724 default:
725 dbg("%s - nonzero urb status received: %d",
726 __FUNCTION__, urb->status);
727 goto exit;
728 }
729
730 usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
731
732 exit:
733 retval = usb_submit_urb(urb, GFP_ATOMIC);
734 if (retval)
735 err("%s - usb_submit_urb failed with result: %d",
736 __FUNCTION__, retval);
737 }
738
739 static int usbtouch_open(struct input_dev *input)
740 {
741 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
742
743 usbtouch->irq->dev = usbtouch->udev;
744
745 if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
746 return -EIO;
747
748 return 0;
749 }
750
751 static void usbtouch_close(struct input_dev *input)
752 {
753 struct usbtouch_usb *usbtouch = input_get_drvdata(input);
754
755 usb_kill_urb(usbtouch->irq);
756 }
757
758
759 static void usbtouch_free_buffers(struct usb_device *udev,
760 struct usbtouch_usb *usbtouch)
761 {
762 usb_buffer_free(udev, usbtouch->type->rept_size,
763 usbtouch->data, usbtouch->data_dma);
764 kfree(usbtouch->buffer);
765 }
766
767
768 static int usbtouch_probe(struct usb_interface *intf,
769 const struct usb_device_id *id)
770 {
771 struct usbtouch_usb *usbtouch;
772 struct input_dev *input_dev;
773 struct usb_host_interface *interface;
774 struct usb_endpoint_descriptor *endpoint;
775 struct usb_device *udev = interface_to_usbdev(intf);
776 struct usbtouch_device_info *type;
777 int err = -ENOMEM;
778
779 interface = intf->cur_altsetting;
780 endpoint = &interface->endpoint[0].desc;
781
782 usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
783 input_dev = input_allocate_device();
784 if (!usbtouch || !input_dev)
785 goto out_free;
786
787 type = &usbtouch_dev_info[id->driver_info];
788 usbtouch->type = type;
789 if (!type->process_pkt)
790 type->process_pkt = usbtouch_process_pkt;
791
792 usbtouch->data = usb_buffer_alloc(udev, type->rept_size,
793 GFP_KERNEL, &usbtouch->data_dma);
794 if (!usbtouch->data)
795 goto out_free;
796
797 if (type->flags & USBTOUCH_FLG_BUFFER) {
798 usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
799 if (!usbtouch->buffer)
800 goto out_free_buffers;
801 }
802
803 usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
804 if (!usbtouch->irq) {
805 dbg("%s - usb_alloc_urb failed: usbtouch->irq", __FUNCTION__);
806 goto out_free_buffers;
807 }
808
809 usbtouch->udev = udev;
810 usbtouch->input = input_dev;
811
812 if (udev->manufacturer)
813 strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
814
815 if (udev->product) {
816 if (udev->manufacturer)
817 strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
818 strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
819 }
820
821 if (!strlen(usbtouch->name))
822 snprintf(usbtouch->name, sizeof(usbtouch->name),
823 "USB Touchscreen %04x:%04x",
824 le16_to_cpu(udev->descriptor.idVendor),
825 le16_to_cpu(udev->descriptor.idProduct));
826
827 usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
828 strlcpy(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
829
830 input_dev->name = usbtouch->name;
831 input_dev->phys = usbtouch->phys;
832 usb_to_input_id(udev, &input_dev->id);
833 input_dev->dev.parent = &intf->dev;
834
835 input_set_drvdata(input_dev, usbtouch);
836
837 input_dev->open = usbtouch_open;
838 input_dev->close = usbtouch_close;
839
840 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
841 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
842 input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
843 input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
844 if (type->max_press)
845 input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
846 type->max_press, 0, 0);
847
848 usb_fill_int_urb(usbtouch->irq, usbtouch->udev,
849 usb_rcvintpipe(usbtouch->udev, endpoint->bEndpointAddress),
850 usbtouch->data, type->rept_size,
851 usbtouch_irq, usbtouch, endpoint->bInterval);
852
853 usbtouch->irq->dev = usbtouch->udev;
854 usbtouch->irq->transfer_dma = usbtouch->data_dma;
855 usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
856
857 /* device specific init */
858 if (type->init) {
859 err = type->init(usbtouch);
860 if (err) {
861 dbg("%s - type->init() failed, err: %d", __FUNCTION__, err);
862 goto out_free_buffers;
863 }
864 }
865
866 err = input_register_device(usbtouch->input);
867 if (err) {
868 dbg("%s - input_register_device failed, err: %d", __FUNCTION__, err);
869 goto out_free_buffers;
870 }
871
872 usb_set_intfdata(intf, usbtouch);
873
874 return 0;
875
876 out_free_buffers:
877 usbtouch_free_buffers(udev, usbtouch);
878 out_free:
879 input_free_device(input_dev);
880 kfree(usbtouch);
881 return err;
882 }
883
884 static void usbtouch_disconnect(struct usb_interface *intf)
885 {
886 struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
887
888 dbg("%s - called", __FUNCTION__);
889
890 if (!usbtouch)
891 return;
892
893 dbg("%s - usbtouch is initialized, cleaning up", __FUNCTION__);
894 usb_set_intfdata(intf, NULL);
895 usb_kill_urb(usbtouch->irq);
896 input_unregister_device(usbtouch->input);
897 usb_free_urb(usbtouch->irq);
898 usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
899 kfree(usbtouch);
900 }
901
902 MODULE_DEVICE_TABLE(usb, usbtouch_devices);
903
904 static struct usb_driver usbtouch_driver = {
905 .name = "usbtouchscreen",
906 .probe = usbtouch_probe,
907 .disconnect = usbtouch_disconnect,
908 .id_table = usbtouch_devices,
909 };
910
911 static int __init usbtouch_init(void)
912 {
913 return usb_register(&usbtouch_driver);
914 }
915
916 static void __exit usbtouch_cleanup(void)
917 {
918 usb_deregister(&usbtouch_driver);
919 }
920
921 module_init(usbtouch_init);
922 module_exit(usbtouch_cleanup);
923
924 MODULE_AUTHOR(DRIVER_AUTHOR);
925 MODULE_DESCRIPTION(DRIVER_DESC);
926 MODULE_LICENSE("GPL");
927
928 MODULE_ALIAS("touchkitusb");
929 MODULE_ALIAS("itmtouch");
930 MODULE_ALIAS("mtouchusb");
This page took 0.049065 seconds and 4 git commands to generate.