xref: /openbmc/linux/drivers/input/touchscreen/usbtouchscreen.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d05e84e6SDmitry Torokhov /******************************************************************************
3d05e84e6SDmitry Torokhov  * usbtouchscreen.c
4d05e84e6SDmitry Torokhov  * Driver for USB Touchscreens, supporting those devices:
5d05e84e6SDmitry Torokhov  *  - eGalax Touchkit
6d05e84e6SDmitry Torokhov  *    includes eTurboTouch CT-410/510/700
7d05e84e6SDmitry Torokhov  *  - 3M/Microtouch  EX II series
8d05e84e6SDmitry Torokhov  *  - ITM
9d05e84e6SDmitry Torokhov  *  - PanJit TouchSet
10d05e84e6SDmitry Torokhov  *  - eTurboTouch
11d05e84e6SDmitry Torokhov  *  - Gunze AHL61
12d05e84e6SDmitry Torokhov  *  - DMC TSC-10/25
13df561fcdSOndrej Zary  *  - IRTOUCHSYSTEMS/UNITOP
14a14a8401SOndrej Zary  *  - IdealTEK URTC1000
1562aa366dSDaniel Ritz  *  - General Touch
1614e40206SJerrold Jones  *  - GoTop Super_Q2/GogoPen/PenPower tablets
17f7370699SJim Persson  *  - JASTEC USB touch controller/DigiTech DTR-02U
182330ed18SDaniel Silverstone  *  - Zytronic capacitive touchscreen
195197424cSOndrej Zary  *  - NEXIO/iNexio
20d2cc817aSMichael Gebetsroither  *  - Elo TouchSystems 2700 IntelliTouch
21aa87512fSArmando Visconti  *  - EasyTouch USB Dual/Multi touch controller from Data Modul
22d05e84e6SDmitry Torokhov  *
2314e40206SJerrold Jones  * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
24d05e84e6SDmitry Torokhov  * Copyright (C) by Todd E. Johnson (mtouchusb.c)
25d05e84e6SDmitry Torokhov  *
26d05e84e6SDmitry Torokhov  * Driver is based on touchkitusb.c
27d05e84e6SDmitry Torokhov  * - ITM parts are from itmtouch.c
28d05e84e6SDmitry Torokhov  * - 3M parts are from mtouchusb.c
29d05e84e6SDmitry Torokhov  * - PanJit parts are from an unmerged driver by Lanslott Gish
30d05e84e6SDmitry Torokhov  * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
31d05e84e6SDmitry Torokhov  *   driver from Marius Vollmer
32d05e84e6SDmitry Torokhov  *
33d05e84e6SDmitry Torokhov  *****************************************************************************/
34d05e84e6SDmitry Torokhov 
35d05e84e6SDmitry Torokhov //#define DEBUG
36d05e84e6SDmitry Torokhov 
37d05e84e6SDmitry Torokhov #include <linux/kernel.h>
38d05e84e6SDmitry Torokhov #include <linux/slab.h>
39d05e84e6SDmitry Torokhov #include <linux/input.h>
40d05e84e6SDmitry Torokhov #include <linux/module.h>
41d05e84e6SDmitry Torokhov #include <linux/usb.h>
42d05e84e6SDmitry Torokhov #include <linux/usb/input.h>
43ec42d448SDaniel Ritz #include <linux/hid.h>
4412e510dbSMarcus Folkesson #include <linux/mutex.h>
45d05e84e6SDmitry Torokhov 
4690ab5ee9SRusty Russell static bool swap_xy;
47d05e84e6SDmitry Torokhov module_param(swap_xy, bool, 0644);
48d05e84e6SDmitry Torokhov MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
49d05e84e6SDmitry Torokhov 
5090ab5ee9SRusty Russell static bool hwcalib_xy;
51c9cbf3d3SDan Streetman module_param(hwcalib_xy, bool, 0644);
52c9cbf3d3SDan Streetman MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
53c9cbf3d3SDan Streetman 
54d05e84e6SDmitry Torokhov /* device specifc data/functions */
55d05e84e6SDmitry Torokhov struct usbtouch_usb;
56d05e84e6SDmitry Torokhov struct usbtouch_device_info {
57d05e84e6SDmitry Torokhov 	int min_xc, max_xc;
58d05e84e6SDmitry Torokhov 	int min_yc, max_yc;
59d05e84e6SDmitry Torokhov 	int min_press, max_press;
60d05e84e6SDmitry Torokhov 	int rept_size;
61d05e84e6SDmitry Torokhov 
622330ed18SDaniel Silverstone 	/*
632330ed18SDaniel Silverstone 	 * Always service the USB devices irq not just when the input device is
642330ed18SDaniel Silverstone 	 * open. This is useful when devices have a watchdog which prevents us
652330ed18SDaniel Silverstone 	 * from periodically polling the device. Leave this unset unless your
662330ed18SDaniel Silverstone 	 * touchscreen device requires it, as it does consume more of the USB
672330ed18SDaniel Silverstone 	 * bandwidth.
682330ed18SDaniel Silverstone 	 */
692330ed18SDaniel Silverstone 	bool irq_always;
702330ed18SDaniel Silverstone 
71d05e84e6SDmitry Torokhov 	void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
7262aa366dSDaniel Ritz 
7362aa366dSDaniel Ritz 	/*
7462aa366dSDaniel Ritz 	 * used to get the packet len. possible return values:
7562aa366dSDaniel Ritz 	 * > 0: packet len
7662aa366dSDaniel Ritz 	 * = 0: skip one byte
7762aa366dSDaniel Ritz 	 * < 0: -return value more bytes needed
7862aa366dSDaniel Ritz 	 */
79d05e84e6SDmitry Torokhov 	int  (*get_pkt_len) (unsigned char *pkt, int len);
8062aa366dSDaniel Ritz 
81d05e84e6SDmitry Torokhov 	int  (*read_data)   (struct usbtouch_usb *usbtouch, unsigned char *pkt);
82a8aef622SOliver Neukum 	int  (*alloc)       (struct usbtouch_usb *usbtouch);
83d05e84e6SDmitry Torokhov 	int  (*init)        (struct usbtouch_usb *usbtouch);
845197424cSOndrej Zary 	void (*exit)	    (struct usbtouch_usb *usbtouch);
85d05e84e6SDmitry Torokhov };
86d05e84e6SDmitry Torokhov 
87d05e84e6SDmitry Torokhov /* a usbtouch device */
88d05e84e6SDmitry Torokhov struct usbtouch_usb {
89d05e84e6SDmitry Torokhov 	unsigned char *data;
90d05e84e6SDmitry Torokhov 	dma_addr_t data_dma;
914ef38351SChristian Engelmayer 	int data_size;
92d05e84e6SDmitry Torokhov 	unsigned char *buffer;
93d05e84e6SDmitry Torokhov 	int buf_len;
94d05e84e6SDmitry Torokhov 	struct urb *irq;
95fea4d14bSOndrej Zary 	struct usb_interface *interface;
96d05e84e6SDmitry Torokhov 	struct input_dev *input;
97d05e84e6SDmitry Torokhov 	struct usbtouch_device_info *type;
9812e510dbSMarcus Folkesson 	struct mutex pm_mutex;  /* serialize access to open/suspend */
9985f46fbfSMarcus Folkesson 	bool is_open;
100d05e84e6SDmitry Torokhov 	char name[128];
101d05e84e6SDmitry Torokhov 	char phys[64];
1025197424cSOndrej Zary 	void *priv;
103d05e84e6SDmitry Torokhov 
104d05e84e6SDmitry Torokhov 	int x, y;
105d05e84e6SDmitry Torokhov 	int touch, press;
106d05e84e6SDmitry Torokhov };
107d05e84e6SDmitry Torokhov 
108d05e84e6SDmitry Torokhov 
109d05e84e6SDmitry Torokhov /* device types */
110d05e84e6SDmitry Torokhov enum {
111ec42d448SDaniel Ritz 	DEVTYPE_IGNORE = -1,
112d05e84e6SDmitry Torokhov 	DEVTYPE_EGALAX,
113d05e84e6SDmitry Torokhov 	DEVTYPE_PANJIT,
114d05e84e6SDmitry Torokhov 	DEVTYPE_3M,
115d05e84e6SDmitry Torokhov 	DEVTYPE_ITM,
116d05e84e6SDmitry Torokhov 	DEVTYPE_ETURBO,
117d05e84e6SDmitry Torokhov 	DEVTYPE_GUNZE,
118d05e84e6SDmitry Torokhov 	DEVTYPE_DMC_TSC10,
119df561fcdSOndrej Zary 	DEVTYPE_IRTOUCH,
120dbea4032SLars Poeschel 	DEVTYPE_IRTOUCH_HIRES,
121a14a8401SOndrej Zary 	DEVTYPE_IDEALTEK,
1229d5657dbSIlya Frolov 	DEVTYPE_GENERAL_TOUCH,
12314e40206SJerrold Jones 	DEVTYPE_GOTOP,
124f7370699SJim Persson 	DEVTYPE_JASTEC,
1259e3b2583SFlorian Echtler 	DEVTYPE_E2I,
1262330ed18SDaniel Silverstone 	DEVTYPE_ZYTRONIC,
12738771bb4SPetr Štetiar 	DEVTYPE_TC45USB,
1285197424cSOndrej Zary 	DEVTYPE_NEXIO,
129d2cc817aSMichael Gebetsroither 	DEVTYPE_ELO,
130aa87512fSArmando Visconti 	DEVTYPE_ETOUCH,
131d05e84e6SDmitry Torokhov };
132d05e84e6SDmitry Torokhov 
133ec42d448SDaniel Ritz #define USB_DEVICE_HID_CLASS(vend, prod) \
134ec42d448SDaniel Ritz 	.match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
135ec42d448SDaniel Ritz 		| USB_DEVICE_ID_MATCH_DEVICE, \
136ec42d448SDaniel Ritz 	.idVendor = (vend), \
137ec42d448SDaniel Ritz 	.idProduct = (prod), \
138ae2aa3a5SForest Bond 	.bInterfaceClass = USB_INTERFACE_CLASS_HID
139ec42d448SDaniel Ritz 
1409cb3ce52SMárton Németh static const struct usb_device_id usbtouch_devices[] = {
141c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
142ec42d448SDaniel Ritz 	/* ignore the HID capable devices, handled by usbhid */
143ec42d448SDaniel Ritz 	{USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
144ec42d448SDaniel Ritz 	{USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
145ec42d448SDaniel Ritz 
146ec42d448SDaniel Ritz 	/* normal device IDs */
147d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
148d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
149d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
150d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
151d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
152d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
153d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
154d05e84e6SDmitry Torokhov #endif
155d05e84e6SDmitry Torokhov 
156c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
157d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
158d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
159d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
160d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
161d05e84e6SDmitry Torokhov #endif
162d05e84e6SDmitry Torokhov 
163c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_3M
164d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
165d05e84e6SDmitry Torokhov #endif
166d05e84e6SDmitry Torokhov 
167c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_ITM
168d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
16909910509SGlenn Sommer 	{USB_DEVICE(0x16e3, 0xf9e9), .driver_info = DEVTYPE_ITM},
170d05e84e6SDmitry Torokhov #endif
171d05e84e6SDmitry Torokhov 
172c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
173d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
174d05e84e6SDmitry Torokhov #endif
175d05e84e6SDmitry Torokhov 
176c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
177d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
178d05e84e6SDmitry Torokhov #endif
179d05e84e6SDmitry Torokhov 
180c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
181d05e84e6SDmitry Torokhov 	{USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
182d05e84e6SDmitry Torokhov #endif
183d05e84e6SDmitry Torokhov 
184df561fcdSOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
185e3b4f94eSJames Hilliard 	{USB_DEVICE(0x255e, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
186df561fcdSOndrej Zary 	{USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
187df561fcdSOndrej Zary 	{USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
188dbea4032SLars Poeschel 	{USB_DEVICE(0x6615, 0x0012), .driver_info = DEVTYPE_IRTOUCH_HIRES},
189df561fcdSOndrej Zary #endif
190df561fcdSOndrej Zary 
191a14a8401SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
192a14a8401SOndrej Zary 	{USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
193a14a8401SOndrej Zary #endif
194a14a8401SOndrej Zary 
1959d5657dbSIlya Frolov #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
1969d5657dbSIlya Frolov 	{USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
1979d5657dbSIlya Frolov #endif
1989d5657dbSIlya Frolov 
19914e40206SJerrold Jones #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
20014e40206SJerrold Jones 	{USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
20114e40206SJerrold Jones 	{USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
20214e40206SJerrold Jones 	{USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
20314e40206SJerrold Jones #endif
20414e40206SJerrold Jones 
205f7370699SJim Persson #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
206f7370699SJim Persson 	{USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
207f7370699SJim Persson #endif
208f7370699SJim Persson 
2099e3b2583SFlorian Echtler #ifdef CONFIG_TOUCHSCREEN_USB_E2I
2109e3b2583SFlorian Echtler 	{USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
2119e3b2583SFlorian Echtler #endif
2122330ed18SDaniel Silverstone 
2132330ed18SDaniel Silverstone #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
2142330ed18SDaniel Silverstone 	{USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
2152330ed18SDaniel Silverstone #endif
2162330ed18SDaniel Silverstone 
21738771bb4SPetr Štetiar #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
21838771bb4SPetr Štetiar 	/* TC5UH */
21938771bb4SPetr Štetiar 	{USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC45USB},
22038771bb4SPetr Štetiar 	/* TC4UM */
22138771bb4SPetr Štetiar 	{USB_DEVICE(0x0664, 0x0306), .driver_info = DEVTYPE_TC45USB},
222dbe1420bSPetr Štetiar #endif
223dbe1420bSPetr Štetiar 
2245197424cSOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
2255197424cSOndrej Zary 	/* data interface only */
2265197424cSOndrej Zary 	{USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
2275197424cSOndrej Zary 		.driver_info = DEVTYPE_NEXIO},
2285197424cSOndrej Zary 	{USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
2295197424cSOndrej Zary 		.driver_info = DEVTYPE_NEXIO},
2305197424cSOndrej Zary #endif
2315197424cSOndrej Zary 
232d2cc817aSMichael Gebetsroither #ifdef CONFIG_TOUCHSCREEN_USB_ELO
233d2cc817aSMichael Gebetsroither 	{USB_DEVICE(0x04e7, 0x0020), .driver_info = DEVTYPE_ELO},
234d2cc817aSMichael Gebetsroither #endif
235d2cc817aSMichael Gebetsroither 
236aa87512fSArmando Visconti #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
237aa87512fSArmando Visconti 	{USB_DEVICE(0x7374, 0x0001), .driver_info = DEVTYPE_ETOUCH},
238aa87512fSArmando Visconti #endif
239aa87512fSArmando Visconti 
240d05e84e6SDmitry Torokhov 	{}
241d05e84e6SDmitry Torokhov };
242d05e84e6SDmitry Torokhov 
243d05e84e6SDmitry Torokhov 
244d05e84e6SDmitry Torokhov /*****************************************************************************
2459e3b2583SFlorian Echtler  * e2i Part
2469e3b2583SFlorian Echtler  */
2479e3b2583SFlorian Echtler 
2489e3b2583SFlorian Echtler #ifdef CONFIG_TOUCHSCREEN_USB_E2I
e2i_init(struct usbtouch_usb * usbtouch)2499e3b2583SFlorian Echtler static int e2i_init(struct usbtouch_usb *usbtouch)
2509e3b2583SFlorian Echtler {
2519e3b2583SFlorian Echtler 	int ret;
252fea4d14bSOndrej Zary 	struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
2539e3b2583SFlorian Echtler 
25441e81022SJohan Hovold 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2559e3b2583SFlorian Echtler 	                      0x01, 0x02, 0x0000, 0x0081,
2569e3b2583SFlorian Echtler 	                      NULL, 0, USB_CTRL_SET_TIMEOUT);
2579e3b2583SFlorian Echtler 
2580a5ebc88SGreg Kroah-Hartman 	dev_dbg(&usbtouch->interface->dev,
259b741ab9dSGreg Kroah-Hartman 		"%s - usb_control_msg - E2I_RESET - bytes|err: %d\n",
2609e3b2583SFlorian Echtler 		__func__, ret);
2619e3b2583SFlorian Echtler 	return ret;
2629e3b2583SFlorian Echtler }
2639e3b2583SFlorian Echtler 
e2i_read_data(struct usbtouch_usb * dev,unsigned char * pkt)2649e3b2583SFlorian Echtler static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
2659e3b2583SFlorian Echtler {
2669e3b2583SFlorian Echtler 	int tmp = (pkt[0] << 8) | pkt[1];
2679e3b2583SFlorian Echtler 	dev->x  = (pkt[2] << 8) | pkt[3];
2689e3b2583SFlorian Echtler 	dev->y  = (pkt[4] << 8) | pkt[5];
2699e3b2583SFlorian Echtler 
2709e3b2583SFlorian Echtler 	tmp = tmp - 0xA000;
2719e3b2583SFlorian Echtler 	dev->touch = (tmp > 0);
2729e3b2583SFlorian Echtler 	dev->press = (tmp > 0 ? tmp : 0);
2739e3b2583SFlorian Echtler 
2749e3b2583SFlorian Echtler 	return 1;
2759e3b2583SFlorian Echtler }
2769e3b2583SFlorian Echtler #endif
2779e3b2583SFlorian Echtler 
2789e3b2583SFlorian Echtler 
2799e3b2583SFlorian Echtler /*****************************************************************************
280d05e84e6SDmitry Torokhov  * eGalax part
281d05e84e6SDmitry Torokhov  */
282d05e84e6SDmitry Torokhov 
283c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
284d05e84e6SDmitry Torokhov 
28562aa366dSDaniel Ritz #ifndef MULTI_PACKET
28662aa366dSDaniel Ritz #define MULTI_PACKET
28762aa366dSDaniel Ritz #endif
28862aa366dSDaniel Ritz 
289d05e84e6SDmitry Torokhov #define EGALAX_PKT_TYPE_MASK		0xFE
290d05e84e6SDmitry Torokhov #define EGALAX_PKT_TYPE_REPT		0x80
291d05e84e6SDmitry Torokhov #define EGALAX_PKT_TYPE_DIAG		0x0A
292d05e84e6SDmitry Torokhov 
egalax_init(struct usbtouch_usb * usbtouch)293037a833eSForest Bond static int egalax_init(struct usbtouch_usb *usbtouch)
294037a833eSForest Bond {
295037a833eSForest Bond 	int ret, i;
296037a833eSForest Bond 	unsigned char *buf;
297037a833eSForest Bond 	struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
298037a833eSForest Bond 
299037a833eSForest Bond 	/*
300037a833eSForest Bond 	 * An eGalax diagnostic packet kicks the device into using the right
301037a833eSForest Bond 	 * protocol.  We send a "check active" packet.  The response will be
302037a833eSForest Bond 	 * read later and ignored.
303037a833eSForest Bond 	 */
304037a833eSForest Bond 
305037a833eSForest Bond 	buf = kmalloc(3, GFP_KERNEL);
306037a833eSForest Bond 	if (!buf)
307037a833eSForest Bond 		return -ENOMEM;
308037a833eSForest Bond 
309037a833eSForest Bond 	buf[0] = EGALAX_PKT_TYPE_DIAG;
310037a833eSForest Bond 	buf[1] = 1;	/* length */
311037a833eSForest Bond 	buf[2] = 'A';	/* command - check active */
312037a833eSForest Bond 
313037a833eSForest Bond 	for (i = 0; i < 3; i++) {
314037a833eSForest Bond 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
315037a833eSForest Bond 				      0,
316037a833eSForest Bond 				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
317037a833eSForest Bond 				      0, 0, buf, 3,
318037a833eSForest Bond 				      USB_CTRL_SET_TIMEOUT);
319037a833eSForest Bond 		if (ret >= 0) {
320037a833eSForest Bond 			ret = 0;
321037a833eSForest Bond 			break;
322037a833eSForest Bond 		}
323037a833eSForest Bond 		if (ret != -EPIPE)
324037a833eSForest Bond 			break;
325037a833eSForest Bond 	}
326037a833eSForest Bond 
327037a833eSForest Bond 	kfree(buf);
328037a833eSForest Bond 
329037a833eSForest Bond 	return ret;
330037a833eSForest Bond }
331037a833eSForest Bond 
egalax_read_data(struct usbtouch_usb * dev,unsigned char * pkt)332d05e84e6SDmitry Torokhov static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
333d05e84e6SDmitry Torokhov {
334d05e84e6SDmitry Torokhov 	if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
335d05e84e6SDmitry Torokhov 		return 0;
336d05e84e6SDmitry Torokhov 
337d05e84e6SDmitry Torokhov 	dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
338d05e84e6SDmitry Torokhov 	dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
339d05e84e6SDmitry Torokhov 	dev->touch = pkt[0] & 0x01;
340d05e84e6SDmitry Torokhov 
341d05e84e6SDmitry Torokhov 	return 1;
342d05e84e6SDmitry Torokhov }
343d05e84e6SDmitry Torokhov 
egalax_get_pkt_len(unsigned char * buf,int len)344d05e84e6SDmitry Torokhov static int egalax_get_pkt_len(unsigned char *buf, int len)
345d05e84e6SDmitry Torokhov {
346d05e84e6SDmitry Torokhov 	switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
347d05e84e6SDmitry Torokhov 	case EGALAX_PKT_TYPE_REPT:
348d05e84e6SDmitry Torokhov 		return 5;
349d05e84e6SDmitry Torokhov 
350d05e84e6SDmitry Torokhov 	case EGALAX_PKT_TYPE_DIAG:
351d05e84e6SDmitry Torokhov 		if (len < 2)
352d05e84e6SDmitry Torokhov 			return -1;
353d05e84e6SDmitry Torokhov 
354d05e84e6SDmitry Torokhov 		return buf[1] + 2;
355d05e84e6SDmitry Torokhov 	}
356d05e84e6SDmitry Torokhov 
357d05e84e6SDmitry Torokhov 	return 0;
358d05e84e6SDmitry Torokhov }
359d05e84e6SDmitry Torokhov #endif
360d05e84e6SDmitry Torokhov 
361aa87512fSArmando Visconti /*****************************************************************************
362aa87512fSArmando Visconti  * EasyTouch part
363aa87512fSArmando Visconti  */
364aa87512fSArmando Visconti 
365aa87512fSArmando Visconti #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
366aa87512fSArmando Visconti 
367aa87512fSArmando Visconti #ifndef MULTI_PACKET
368aa87512fSArmando Visconti #define MULTI_PACKET
369aa87512fSArmando Visconti #endif
370aa87512fSArmando Visconti 
371aa87512fSArmando Visconti #define ETOUCH_PKT_TYPE_MASK		0xFE
372aa87512fSArmando Visconti #define ETOUCH_PKT_TYPE_REPT		0x80
373aa87512fSArmando Visconti #define ETOUCH_PKT_TYPE_REPT2		0xB0
374aa87512fSArmando Visconti #define ETOUCH_PKT_TYPE_DIAG		0x0A
375aa87512fSArmando Visconti 
etouch_read_data(struct usbtouch_usb * dev,unsigned char * pkt)376aa87512fSArmando Visconti static int etouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
377aa87512fSArmando Visconti {
378aa87512fSArmando Visconti 	if ((pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT &&
379aa87512fSArmando Visconti 		(pkt[0] & ETOUCH_PKT_TYPE_MASK) != ETOUCH_PKT_TYPE_REPT2)
380aa87512fSArmando Visconti 		return 0;
381aa87512fSArmando Visconti 
382aa87512fSArmando Visconti 	dev->x = ((pkt[1] & 0x1F) << 7) | (pkt[2] & 0x7F);
383aa87512fSArmando Visconti 	dev->y = ((pkt[3] & 0x1F) << 7) | (pkt[4] & 0x7F);
384aa87512fSArmando Visconti 	dev->touch = pkt[0] & 0x01;
385aa87512fSArmando Visconti 
386aa87512fSArmando Visconti 	return 1;
387aa87512fSArmando Visconti }
388aa87512fSArmando Visconti 
etouch_get_pkt_len(unsigned char * buf,int len)389aa87512fSArmando Visconti static int etouch_get_pkt_len(unsigned char *buf, int len)
390aa87512fSArmando Visconti {
391aa87512fSArmando Visconti 	switch (buf[0] & ETOUCH_PKT_TYPE_MASK) {
392aa87512fSArmando Visconti 	case ETOUCH_PKT_TYPE_REPT:
393aa87512fSArmando Visconti 	case ETOUCH_PKT_TYPE_REPT2:
394aa87512fSArmando Visconti 		return 5;
395aa87512fSArmando Visconti 
396aa87512fSArmando Visconti 	case ETOUCH_PKT_TYPE_DIAG:
397aa87512fSArmando Visconti 		if (len < 2)
398aa87512fSArmando Visconti 			return -1;
399aa87512fSArmando Visconti 
400aa87512fSArmando Visconti 		return buf[1] + 2;
401aa87512fSArmando Visconti 	}
402aa87512fSArmando Visconti 
403aa87512fSArmando Visconti 	return 0;
404aa87512fSArmando Visconti }
405aa87512fSArmando Visconti #endif
406d05e84e6SDmitry Torokhov 
407d05e84e6SDmitry Torokhov /*****************************************************************************
408d05e84e6SDmitry Torokhov  * PanJit Part
409d05e84e6SDmitry Torokhov  */
410c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
panjit_read_data(struct usbtouch_usb * dev,unsigned char * pkt)411d05e84e6SDmitry Torokhov static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
412d05e84e6SDmitry Torokhov {
413d05e84e6SDmitry Torokhov 	dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
414d05e84e6SDmitry Torokhov 	dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
415d05e84e6SDmitry Torokhov 	dev->touch = pkt[0] & 0x01;
416d05e84e6SDmitry Torokhov 
417d05e84e6SDmitry Torokhov 	return 1;
418d05e84e6SDmitry Torokhov }
419d05e84e6SDmitry Torokhov #endif
420d05e84e6SDmitry Torokhov 
421d05e84e6SDmitry Torokhov 
422d05e84e6SDmitry Torokhov /*****************************************************************************
423d05e84e6SDmitry Torokhov  * 3M/Microtouch Part
424d05e84e6SDmitry Torokhov  */
425c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_3M
426d05e84e6SDmitry Torokhov 
427d05e84e6SDmitry Torokhov #define MTOUCHUSB_ASYNC_REPORT          1
428d05e84e6SDmitry Torokhov #define MTOUCHUSB_RESET                 7
429d05e84e6SDmitry Torokhov #define MTOUCHUSB_REQ_CTRLLR_ID         10
430d05e84e6SDmitry Torokhov 
43189f84b84SNick Dyer #define MTOUCHUSB_REQ_CTRLLR_ID_LEN	16
43289f84b84SNick Dyer 
mtouch_read_data(struct usbtouch_usb * dev,unsigned char * pkt)433d05e84e6SDmitry Torokhov static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
434d05e84e6SDmitry Torokhov {
435c9cbf3d3SDan Streetman 	if (hwcalib_xy) {
436c9cbf3d3SDan Streetman 		dev->x = (pkt[4] << 8) | pkt[3];
437c9cbf3d3SDan Streetman 		dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
438c9cbf3d3SDan Streetman 	} else {
439d05e84e6SDmitry Torokhov 		dev->x = (pkt[8] << 8) | pkt[7];
440d05e84e6SDmitry Torokhov 		dev->y = (pkt[10] << 8) | pkt[9];
441c9cbf3d3SDan Streetman 	}
442d05e84e6SDmitry Torokhov 	dev->touch = (pkt[2] & 0x40) ? 1 : 0;
443d05e84e6SDmitry Torokhov 
444d05e84e6SDmitry Torokhov 	return 1;
445d05e84e6SDmitry Torokhov }
446d05e84e6SDmitry Torokhov 
44789f84b84SNick Dyer struct mtouch_priv {
44889f84b84SNick Dyer 	u8 fw_rev_major;
44989f84b84SNick Dyer 	u8 fw_rev_minor;
45089f84b84SNick Dyer };
45189f84b84SNick Dyer 
mtouch_firmware_rev_show(struct device * dev,struct device_attribute * attr,char * output)45289f84b84SNick Dyer static ssize_t mtouch_firmware_rev_show(struct device *dev,
45389f84b84SNick Dyer 				struct device_attribute *attr, char *output)
45489f84b84SNick Dyer {
45589f84b84SNick Dyer 	struct usb_interface *intf = to_usb_interface(dev);
45689f84b84SNick Dyer 	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
45789f84b84SNick Dyer 	struct mtouch_priv *priv = usbtouch->priv;
45889f84b84SNick Dyer 
45989f84b84SNick Dyer 	return scnprintf(output, PAGE_SIZE, "%1x.%1x\n",
46089f84b84SNick Dyer 			 priv->fw_rev_major, priv->fw_rev_minor);
46189f84b84SNick Dyer }
46289f84b84SNick Dyer static DEVICE_ATTR(firmware_rev, 0444, mtouch_firmware_rev_show, NULL);
46389f84b84SNick Dyer 
46489f84b84SNick Dyer static struct attribute *mtouch_attrs[] = {
46589f84b84SNick Dyer 	&dev_attr_firmware_rev.attr,
46689f84b84SNick Dyer 	NULL
46789f84b84SNick Dyer };
46889f84b84SNick Dyer 
46989f84b84SNick Dyer static const struct attribute_group mtouch_attr_group = {
47089f84b84SNick Dyer 	.attrs = mtouch_attrs,
47189f84b84SNick Dyer };
47289f84b84SNick Dyer 
mtouch_get_fw_revision(struct usbtouch_usb * usbtouch)47389f84b84SNick Dyer static int mtouch_get_fw_revision(struct usbtouch_usb *usbtouch)
47489f84b84SNick Dyer {
47589f84b84SNick Dyer 	struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
47689f84b84SNick Dyer 	struct mtouch_priv *priv = usbtouch->priv;
47789f84b84SNick Dyer 	u8 *buf;
47889f84b84SNick Dyer 	int ret;
47989f84b84SNick Dyer 
48089f84b84SNick Dyer 	buf = kzalloc(MTOUCHUSB_REQ_CTRLLR_ID_LEN, GFP_NOIO);
48189f84b84SNick Dyer 	if (!buf)
48289f84b84SNick Dyer 		return -ENOMEM;
48389f84b84SNick Dyer 
48489f84b84SNick Dyer 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
48589f84b84SNick Dyer 			      MTOUCHUSB_REQ_CTRLLR_ID,
48689f84b84SNick Dyer 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
48789f84b84SNick Dyer 			      0, 0, buf, MTOUCHUSB_REQ_CTRLLR_ID_LEN,
48889f84b84SNick Dyer 			      USB_CTRL_SET_TIMEOUT);
48989f84b84SNick Dyer 	if (ret != MTOUCHUSB_REQ_CTRLLR_ID_LEN) {
49089f84b84SNick Dyer 		dev_warn(&usbtouch->interface->dev,
49189f84b84SNick Dyer 			 "Failed to read FW rev: %d\n", ret);
49289f84b84SNick Dyer 		ret = ret < 0 ? ret : -EIO;
49389f84b84SNick Dyer 		goto free;
49489f84b84SNick Dyer 	}
49589f84b84SNick Dyer 
49689f84b84SNick Dyer 	priv->fw_rev_major = buf[3];
49789f84b84SNick Dyer 	priv->fw_rev_minor = buf[4];
49889f84b84SNick Dyer 
49989f84b84SNick Dyer 	ret = 0;
50089f84b84SNick Dyer 
50189f84b84SNick Dyer free:
50289f84b84SNick Dyer 	kfree(buf);
50389f84b84SNick Dyer 	return ret;
50489f84b84SNick Dyer }
50589f84b84SNick Dyer 
mtouch_alloc(struct usbtouch_usb * usbtouch)50689f84b84SNick Dyer static int mtouch_alloc(struct usbtouch_usb *usbtouch)
50789f84b84SNick Dyer {
50889f84b84SNick Dyer 	int ret;
50989f84b84SNick Dyer 
51089f84b84SNick Dyer 	usbtouch->priv = kmalloc(sizeof(struct mtouch_priv), GFP_KERNEL);
51189f84b84SNick Dyer 	if (!usbtouch->priv)
51289f84b84SNick Dyer 		return -ENOMEM;
51389f84b84SNick Dyer 
51489f84b84SNick Dyer 	ret = sysfs_create_group(&usbtouch->interface->dev.kobj,
51589f84b84SNick Dyer 				 &mtouch_attr_group);
51689f84b84SNick Dyer 	if (ret) {
51789f84b84SNick Dyer 		kfree(usbtouch->priv);
51889f84b84SNick Dyer 		usbtouch->priv = NULL;
51989f84b84SNick Dyer 		return ret;
52089f84b84SNick Dyer 	}
52189f84b84SNick Dyer 
52289f84b84SNick Dyer 	return 0;
52389f84b84SNick Dyer }
52489f84b84SNick Dyer 
mtouch_init(struct usbtouch_usb * usbtouch)525d05e84e6SDmitry Torokhov static int mtouch_init(struct usbtouch_usb *usbtouch)
526d05e84e6SDmitry Torokhov {
527d05e84e6SDmitry Torokhov 	int ret, i;
528fea4d14bSOndrej Zary 	struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
529d05e84e6SDmitry Torokhov 
53089f84b84SNick Dyer 	ret = mtouch_get_fw_revision(usbtouch);
53189f84b84SNick Dyer 	if (ret)
53289f84b84SNick Dyer 		return ret;
53389f84b84SNick Dyer 
53441e81022SJohan Hovold 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
535d05e84e6SDmitry Torokhov 	                      MTOUCHUSB_RESET,
536d05e84e6SDmitry Torokhov 	                      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
537d05e84e6SDmitry Torokhov 	                      1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
5380a5ebc88SGreg Kroah-Hartman 	dev_dbg(&usbtouch->interface->dev,
539b741ab9dSGreg Kroah-Hartman 		"%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d\n",
540ea3e6c59SHarvey Harrison 		__func__, ret);
541d05e84e6SDmitry Torokhov 	if (ret < 0)
542d05e84e6SDmitry Torokhov 		return ret;
543d05e84e6SDmitry Torokhov 	msleep(150);
544d05e84e6SDmitry Torokhov 
545d05e84e6SDmitry Torokhov 	for (i = 0; i < 3; i++) {
54641e81022SJohan Hovold 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
547d05e84e6SDmitry Torokhov 				      MTOUCHUSB_ASYNC_REPORT,
548d05e84e6SDmitry Torokhov 				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
549d05e84e6SDmitry Torokhov 				      1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
5500a5ebc88SGreg Kroah-Hartman 		dev_dbg(&usbtouch->interface->dev,
551b741ab9dSGreg Kroah-Hartman 			"%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d\n",
552ea3e6c59SHarvey Harrison 			__func__, ret);
553d05e84e6SDmitry Torokhov 		if (ret >= 0)
554d05e84e6SDmitry Torokhov 			break;
555d05e84e6SDmitry Torokhov 		if (ret != -EPIPE)
556d05e84e6SDmitry Torokhov 			return ret;
557d05e84e6SDmitry Torokhov 	}
558d05e84e6SDmitry Torokhov 
559c9cbf3d3SDan Streetman 	/* Default min/max xy are the raw values, override if using hw-calib */
560c9cbf3d3SDan Streetman 	if (hwcalib_xy) {
561c9cbf3d3SDan Streetman 		input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
562c9cbf3d3SDan Streetman 		input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
563c9cbf3d3SDan Streetman 	}
564c9cbf3d3SDan Streetman 
565d05e84e6SDmitry Torokhov 	return 0;
566d05e84e6SDmitry Torokhov }
56789f84b84SNick Dyer 
mtouch_exit(struct usbtouch_usb * usbtouch)56889f84b84SNick Dyer static void mtouch_exit(struct usbtouch_usb *usbtouch)
56989f84b84SNick Dyer {
57089f84b84SNick Dyer 	struct mtouch_priv *priv = usbtouch->priv;
57189f84b84SNick Dyer 
57289f84b84SNick Dyer 	sysfs_remove_group(&usbtouch->interface->dev.kobj, &mtouch_attr_group);
57389f84b84SNick Dyer 	kfree(priv);
57489f84b84SNick Dyer }
575d05e84e6SDmitry Torokhov #endif
576d05e84e6SDmitry Torokhov 
577d05e84e6SDmitry Torokhov 
578d05e84e6SDmitry Torokhov /*****************************************************************************
579d05e84e6SDmitry Torokhov  * ITM Part
580d05e84e6SDmitry Torokhov  */
581c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_ITM
itm_read_data(struct usbtouch_usb * dev,unsigned char * pkt)582d05e84e6SDmitry Torokhov static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
583d05e84e6SDmitry Torokhov {
584d05e84e6SDmitry Torokhov 	int touch;
585d05e84e6SDmitry Torokhov 	/*
586d05e84e6SDmitry Torokhov 	 * ITM devices report invalid x/y data if not touched.
587d05e84e6SDmitry Torokhov 	 * if the screen was touched before but is not touched any more
588d05e84e6SDmitry Torokhov 	 * report touch as 0 with the last valid x/y data once. then stop
589d05e84e6SDmitry Torokhov 	 * reporting data until touched again.
590d05e84e6SDmitry Torokhov 	 */
591d05e84e6SDmitry Torokhov 	dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
592d05e84e6SDmitry Torokhov 
593d05e84e6SDmitry Torokhov 	touch = ~pkt[7] & 0x20;
594d05e84e6SDmitry Torokhov 	if (!touch) {
595d05e84e6SDmitry Torokhov 		if (dev->touch) {
596d05e84e6SDmitry Torokhov 			dev->touch = 0;
597d05e84e6SDmitry Torokhov 			return 1;
598d05e84e6SDmitry Torokhov 		}
599d05e84e6SDmitry Torokhov 
600d05e84e6SDmitry Torokhov 		return 0;
601d05e84e6SDmitry Torokhov 	}
602d05e84e6SDmitry Torokhov 
603d05e84e6SDmitry Torokhov 	dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
604d05e84e6SDmitry Torokhov 	dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
605d05e84e6SDmitry Torokhov 	dev->touch = touch;
606d05e84e6SDmitry Torokhov 
607d05e84e6SDmitry Torokhov 	return 1;
608d05e84e6SDmitry Torokhov }
609d05e84e6SDmitry Torokhov #endif
610d05e84e6SDmitry Torokhov 
611d05e84e6SDmitry Torokhov 
612d05e84e6SDmitry Torokhov /*****************************************************************************
613d05e84e6SDmitry Torokhov  * eTurboTouch part
614d05e84e6SDmitry Torokhov  */
615c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
61662aa366dSDaniel Ritz #ifndef MULTI_PACKET
61762aa366dSDaniel Ritz #define MULTI_PACKET
61862aa366dSDaniel Ritz #endif
eturbo_read_data(struct usbtouch_usb * dev,unsigned char * pkt)619d05e84e6SDmitry Torokhov static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
620d05e84e6SDmitry Torokhov {
621d05e84e6SDmitry Torokhov 	unsigned int shift;
622d05e84e6SDmitry Torokhov 
623d05e84e6SDmitry Torokhov 	/* packets should start with sync */
624d05e84e6SDmitry Torokhov 	if (!(pkt[0] & 0x80))
625d05e84e6SDmitry Torokhov 		return 0;
626d05e84e6SDmitry Torokhov 
627d05e84e6SDmitry Torokhov 	shift = (6 - (pkt[0] & 0x03));
628d05e84e6SDmitry Torokhov 	dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
629d05e84e6SDmitry Torokhov 	dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
630d05e84e6SDmitry Torokhov 	dev->touch = (pkt[0] & 0x10) ? 1 : 0;
631d05e84e6SDmitry Torokhov 
632d05e84e6SDmitry Torokhov 	return 1;
633d05e84e6SDmitry Torokhov }
634d05e84e6SDmitry Torokhov 
eturbo_get_pkt_len(unsigned char * buf,int len)635d05e84e6SDmitry Torokhov static int eturbo_get_pkt_len(unsigned char *buf, int len)
636d05e84e6SDmitry Torokhov {
637d05e84e6SDmitry Torokhov 	if (buf[0] & 0x80)
638d05e84e6SDmitry Torokhov 		return 5;
639d05e84e6SDmitry Torokhov 	if (buf[0] == 0x01)
640d05e84e6SDmitry Torokhov 		return 3;
641d05e84e6SDmitry Torokhov 	return 0;
642d05e84e6SDmitry Torokhov }
643d05e84e6SDmitry Torokhov #endif
644d05e84e6SDmitry Torokhov 
645d05e84e6SDmitry Torokhov 
646d05e84e6SDmitry Torokhov /*****************************************************************************
647d05e84e6SDmitry Torokhov  * Gunze part
648d05e84e6SDmitry Torokhov  */
649c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
gunze_read_data(struct usbtouch_usb * dev,unsigned char * pkt)650d05e84e6SDmitry Torokhov static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
651d05e84e6SDmitry Torokhov {
652d05e84e6SDmitry Torokhov 	if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
653d05e84e6SDmitry Torokhov 		return 0;
654d05e84e6SDmitry Torokhov 
655d05e84e6SDmitry Torokhov 	dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
656d05e84e6SDmitry Torokhov 	dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
657d05e84e6SDmitry Torokhov 	dev->touch = pkt[0] & 0x20;
658d05e84e6SDmitry Torokhov 
659d05e84e6SDmitry Torokhov 	return 1;
660d05e84e6SDmitry Torokhov }
661d05e84e6SDmitry Torokhov #endif
662d05e84e6SDmitry Torokhov 
663d05e84e6SDmitry Torokhov /*****************************************************************************
664d05e84e6SDmitry Torokhov  * DMC TSC-10/25 Part
665d05e84e6SDmitry Torokhov  *
666d05e84e6SDmitry Torokhov  * Documentation about the controller and it's protocol can be found at
667d05e84e6SDmitry Torokhov  *   http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
668d05e84e6SDmitry Torokhov  *   http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
669d05e84e6SDmitry Torokhov  */
670c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
671d05e84e6SDmitry Torokhov 
672d05e84e6SDmitry Torokhov /* supported data rates. currently using 130 */
673d05e84e6SDmitry Torokhov #define TSC10_RATE_POINT	0x50
674d05e84e6SDmitry Torokhov #define TSC10_RATE_30		0x40
675d05e84e6SDmitry Torokhov #define TSC10_RATE_50		0x41
676d05e84e6SDmitry Torokhov #define TSC10_RATE_80		0x42
677d05e84e6SDmitry Torokhov #define TSC10_RATE_100		0x43
678d05e84e6SDmitry Torokhov #define TSC10_RATE_130		0x44
679d05e84e6SDmitry Torokhov #define TSC10_RATE_150		0x45
680d05e84e6SDmitry Torokhov 
681d05e84e6SDmitry Torokhov /* commands */
682d05e84e6SDmitry Torokhov #define TSC10_CMD_RESET		0x55
683d05e84e6SDmitry Torokhov #define TSC10_CMD_RATE		0x05
684d05e84e6SDmitry Torokhov #define TSC10_CMD_DATA1		0x01
685d05e84e6SDmitry Torokhov 
dmc_tsc10_init(struct usbtouch_usb * usbtouch)686d05e84e6SDmitry Torokhov static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
687d05e84e6SDmitry Torokhov {
688fea4d14bSOndrej Zary 	struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
68976d057ceSOliver Neukum 	int ret = -ENOMEM;
69076d057ceSOliver Neukum 	unsigned char *buf;
691d05e84e6SDmitry Torokhov 
692a8aef622SOliver Neukum 	buf = kmalloc(2, GFP_NOIO);
69376d057ceSOliver Neukum 	if (!buf)
69476d057ceSOliver Neukum 		goto err_nobuf;
695d05e84e6SDmitry Torokhov 	/* reset */
696d05e84e6SDmitry Torokhov 	buf[0] = buf[1] = 0xFF;
697d05e84e6SDmitry Torokhov 	ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
698d05e84e6SDmitry Torokhov 	                      TSC10_CMD_RESET,
699d05e84e6SDmitry Torokhov 	                      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
700d05e84e6SDmitry Torokhov 	                      0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
701d05e84e6SDmitry Torokhov 	if (ret < 0)
70276d057ceSOliver Neukum 		goto err_out;
7032ec6f246SNuno Lucas 	if (buf[0] != 0x06) {
70476d057ceSOliver Neukum 		ret = -ENODEV;
70576d057ceSOliver Neukum 		goto err_out;
70676d057ceSOliver Neukum 	}
707d05e84e6SDmitry Torokhov 
70896849170SBernhard Bender 	/* TSC-25 data sheet specifies a delay after the RESET command */
70996849170SBernhard Bender 	msleep(150);
71096849170SBernhard Bender 
711d05e84e6SDmitry Torokhov 	/* set coordinate output rate */
712d05e84e6SDmitry Torokhov 	buf[0] = buf[1] = 0xFF;
713d05e84e6SDmitry Torokhov 	ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
714d05e84e6SDmitry Torokhov 	                      TSC10_CMD_RATE,
715d05e84e6SDmitry Torokhov 	                      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
716d05e84e6SDmitry Torokhov 	                      TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
717d05e84e6SDmitry Torokhov 	if (ret < 0)
71876d057ceSOliver Neukum 		goto err_out;
7192ec6f246SNuno Lucas 	if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
72076d057ceSOliver Neukum 		ret = -ENODEV;
72176d057ceSOliver Neukum 		goto err_out;
72276d057ceSOliver Neukum 	}
723d05e84e6SDmitry Torokhov 
724d05e84e6SDmitry Torokhov 	/* start sending data */
72541e81022SJohan Hovold 	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
726d05e84e6SDmitry Torokhov 	                      TSC10_CMD_DATA1,
727d05e84e6SDmitry Torokhov 	                      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
728d05e84e6SDmitry Torokhov 	                      0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
72976d057ceSOliver Neukum err_out:
73076d057ceSOliver Neukum 	kfree(buf);
73176d057ceSOliver Neukum err_nobuf:
732d05e84e6SDmitry Torokhov 	return ret;
733d05e84e6SDmitry Torokhov }
734d05e84e6SDmitry Torokhov 
735d05e84e6SDmitry Torokhov 
dmc_tsc10_read_data(struct usbtouch_usb * dev,unsigned char * pkt)736d05e84e6SDmitry Torokhov static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
737d05e84e6SDmitry Torokhov {
738d05e84e6SDmitry Torokhov 	dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
739d05e84e6SDmitry Torokhov 	dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
740d05e84e6SDmitry Torokhov 	dev->touch = pkt[0] & 0x01;
741d05e84e6SDmitry Torokhov 
742d05e84e6SDmitry Torokhov 	return 1;
743d05e84e6SDmitry Torokhov }
744d05e84e6SDmitry Torokhov #endif
745d05e84e6SDmitry Torokhov 
746d05e84e6SDmitry Torokhov 
747d05e84e6SDmitry Torokhov /*****************************************************************************
748df561fcdSOndrej Zary  * IRTOUCH Part
749df561fcdSOndrej Zary  */
750df561fcdSOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
irtouch_read_data(struct usbtouch_usb * dev,unsigned char * pkt)751df561fcdSOndrej Zary static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
752df561fcdSOndrej Zary {
753df561fcdSOndrej Zary 	dev->x = (pkt[3] << 8) | pkt[2];
754df561fcdSOndrej Zary 	dev->y = (pkt[5] << 8) | pkt[4];
755df561fcdSOndrej Zary 	dev->touch = (pkt[1] & 0x03) ? 1 : 0;
756df561fcdSOndrej Zary 
757df561fcdSOndrej Zary 	return 1;
758df561fcdSOndrej Zary }
759df561fcdSOndrej Zary #endif
760df561fcdSOndrej Zary 
761dbe1420bSPetr Štetiar /*****************************************************************************
76238771bb4SPetr Štetiar  * ET&T TC5UH/TC4UM part
763dbe1420bSPetr Štetiar  */
76438771bb4SPetr Štetiar #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
tc45usb_read_data(struct usbtouch_usb * dev,unsigned char * pkt)76538771bb4SPetr Štetiar static int tc45usb_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
766dbe1420bSPetr Štetiar {
767dbe1420bSPetr Štetiar 	dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
768dbe1420bSPetr Štetiar 	dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
769dbe1420bSPetr Štetiar 	dev->touch = pkt[0] & 0x01;
770dbe1420bSPetr Štetiar 
771dbe1420bSPetr Štetiar 	return 1;
772dbe1420bSPetr Štetiar }
773dbe1420bSPetr Štetiar #endif
774df561fcdSOndrej Zary 
775df561fcdSOndrej Zary /*****************************************************************************
776a14a8401SOndrej Zary  * IdealTEK URTC1000 Part
777a14a8401SOndrej Zary  */
778a14a8401SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
77962aa366dSDaniel Ritz #ifndef MULTI_PACKET
78062aa366dSDaniel Ritz #define MULTI_PACKET
78162aa366dSDaniel Ritz #endif
idealtek_get_pkt_len(unsigned char * buf,int len)782a14a8401SOndrej Zary static int idealtek_get_pkt_len(unsigned char *buf, int len)
783a14a8401SOndrej Zary {
784a14a8401SOndrej Zary 	if (buf[0] & 0x80)
785a14a8401SOndrej Zary 		return 5;
786a14a8401SOndrej Zary 	if (buf[0] == 0x01)
787a14a8401SOndrej Zary 		return len;
788a14a8401SOndrej Zary 	return 0;
789a14a8401SOndrej Zary }
790a14a8401SOndrej Zary 
idealtek_read_data(struct usbtouch_usb * dev,unsigned char * pkt)791a14a8401SOndrej Zary static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
792a14a8401SOndrej Zary {
793a14a8401SOndrej Zary 	switch (pkt[0] & 0x98) {
794a14a8401SOndrej Zary 	case 0x88:
795a14a8401SOndrej Zary 		/* touch data in IdealTEK mode */
796a14a8401SOndrej Zary 		dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
797a14a8401SOndrej Zary 		dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
798a14a8401SOndrej Zary 		dev->touch = (pkt[0] & 0x40) ? 1 : 0;
799a14a8401SOndrej Zary 		return 1;
800a14a8401SOndrej Zary 
801a14a8401SOndrej Zary 	case 0x98:
802a14a8401SOndrej Zary 		/* touch data in MT emulation mode */
803a14a8401SOndrej Zary 		dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
804a14a8401SOndrej Zary 		dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
805a14a8401SOndrej Zary 		dev->touch = (pkt[0] & 0x40) ? 1 : 0;
806a14a8401SOndrej Zary 		return 1;
807a14a8401SOndrej Zary 
808a14a8401SOndrej Zary 	default:
809a14a8401SOndrej Zary 		return 0;
810a14a8401SOndrej Zary 	}
811a14a8401SOndrej Zary }
812a14a8401SOndrej Zary #endif
813a14a8401SOndrej Zary 
8149d5657dbSIlya Frolov /*****************************************************************************
8159d5657dbSIlya Frolov  * General Touch Part
8169d5657dbSIlya Frolov  */
8179d5657dbSIlya Frolov #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
general_touch_read_data(struct usbtouch_usb * dev,unsigned char * pkt)8189d5657dbSIlya Frolov static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
8199d5657dbSIlya Frolov {
820eb083ba2SRoy Yin 	dev->x = (pkt[2] << 8) | pkt[1];
821eb083ba2SRoy Yin 	dev->y = (pkt[4] << 8) | pkt[3];
8229d5657dbSIlya Frolov 	dev->press = pkt[5] & 0xff;
8239d5657dbSIlya Frolov 	dev->touch = pkt[0] & 0x01;
8249d5657dbSIlya Frolov 
8259d5657dbSIlya Frolov 	return 1;
8269d5657dbSIlya Frolov }
8279d5657dbSIlya Frolov #endif
828a14a8401SOndrej Zary 
829a14a8401SOndrej Zary /*****************************************************************************
83014e40206SJerrold Jones  * GoTop Part
83114e40206SJerrold Jones  */
83214e40206SJerrold Jones #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
gotop_read_data(struct usbtouch_usb * dev,unsigned char * pkt)83314e40206SJerrold Jones static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
83414e40206SJerrold Jones {
83514e40206SJerrold Jones 	dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
83614e40206SJerrold Jones 	dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
83714e40206SJerrold Jones 	dev->touch = pkt[0] & 0x01;
838f7370699SJim Persson 
839f7370699SJim Persson 	return 1;
840f7370699SJim Persson }
841f7370699SJim Persson #endif
842f7370699SJim Persson 
843f7370699SJim Persson /*****************************************************************************
844f7370699SJim Persson  * JASTEC Part
845f7370699SJim Persson  */
846f7370699SJim Persson #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
jastec_read_data(struct usbtouch_usb * dev,unsigned char * pkt)847f7370699SJim Persson static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
848f7370699SJim Persson {
849f7370699SJim Persson 	dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
850f7370699SJim Persson 	dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
851f7370699SJim Persson 	dev->touch = (pkt[0] & 0x40) >> 6;
852f7370699SJim Persson 
85314e40206SJerrold Jones 	return 1;
85414e40206SJerrold Jones }
85514e40206SJerrold Jones #endif
85614e40206SJerrold Jones 
8572330ed18SDaniel Silverstone /*****************************************************************************
8582330ed18SDaniel Silverstone  * Zytronic Part
8592330ed18SDaniel Silverstone  */
8602330ed18SDaniel Silverstone #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
zytronic_read_data(struct usbtouch_usb * dev,unsigned char * pkt)8612330ed18SDaniel Silverstone static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
8622330ed18SDaniel Silverstone {
8630a5ebc88SGreg Kroah-Hartman 	struct usb_interface *intf = dev->interface;
864b741ab9dSGreg Kroah-Hartman 
8652330ed18SDaniel Silverstone 	switch (pkt[0]) {
8662330ed18SDaniel Silverstone 	case 0x3A: /* command response */
8670a5ebc88SGreg Kroah-Hartman 		dev_dbg(&intf->dev, "%s: Command response %d\n", __func__, pkt[1]);
8682330ed18SDaniel Silverstone 		break;
8692330ed18SDaniel Silverstone 
8702330ed18SDaniel Silverstone 	case 0xC0: /* down */
8712330ed18SDaniel Silverstone 		dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
8722330ed18SDaniel Silverstone 		dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
8732330ed18SDaniel Silverstone 		dev->touch = 1;
8740a5ebc88SGreg Kroah-Hartman 		dev_dbg(&intf->dev, "%s: down %d,%d\n", __func__, dev->x, dev->y);
8752330ed18SDaniel Silverstone 		return 1;
8762330ed18SDaniel Silverstone 
8772330ed18SDaniel Silverstone 	case 0x80: /* up */
8782330ed18SDaniel Silverstone 		dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
8792330ed18SDaniel Silverstone 		dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
8802330ed18SDaniel Silverstone 		dev->touch = 0;
8810a5ebc88SGreg Kroah-Hartman 		dev_dbg(&intf->dev, "%s: up %d,%d\n", __func__, dev->x, dev->y);
8822330ed18SDaniel Silverstone 		return 1;
8832330ed18SDaniel Silverstone 
8842330ed18SDaniel Silverstone 	default:
8850a5ebc88SGreg Kroah-Hartman 		dev_dbg(&intf->dev, "%s: Unknown return %d\n", __func__, pkt[0]);
8862330ed18SDaniel Silverstone 		break;
8872330ed18SDaniel Silverstone 	}
8882330ed18SDaniel Silverstone 
8892330ed18SDaniel Silverstone 	return 0;
8902330ed18SDaniel Silverstone }
8912330ed18SDaniel Silverstone #endif
89214e40206SJerrold Jones 
89314e40206SJerrold Jones /*****************************************************************************
8945197424cSOndrej Zary  * NEXIO Part
8955197424cSOndrej Zary  */
8965197424cSOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
8975197424cSOndrej Zary 
8985197424cSOndrej Zary #define NEXIO_TIMEOUT	5000
8995197424cSOndrej Zary #define NEXIO_BUFSIZE	1024
9005197424cSOndrej Zary #define NEXIO_THRESHOLD	50
9015197424cSOndrej Zary 
9025197424cSOndrej Zary struct nexio_priv {
9035197424cSOndrej Zary 	struct urb *ack;
9045197424cSOndrej Zary 	unsigned char *ack_buf;
9055197424cSOndrej Zary };
9065197424cSOndrej Zary 
9075197424cSOndrej Zary struct nexio_touch_packet {
9085197424cSOndrej Zary 	u8	flags;		/* 0xe1 = touch, 0xe1 = release */
9095197424cSOndrej Zary 	__be16	data_len;	/* total bytes of touch data */
9105197424cSOndrej Zary 	__be16	x_len;		/* bytes for X axis */
9115197424cSOndrej Zary 	__be16	y_len;		/* bytes for Y axis */
9125197424cSOndrej Zary 	u8	data[];
9135197424cSOndrej Zary } __attribute__ ((packed));
9145197424cSOndrej Zary 
9155197424cSOndrej Zary static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
9165197424cSOndrej Zary static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
9175197424cSOndrej Zary 
nexio_ack_complete(struct urb * urb)9185197424cSOndrej Zary static void nexio_ack_complete(struct urb *urb)
9195197424cSOndrej Zary {
9205197424cSOndrej Zary }
9215197424cSOndrej Zary 
nexio_alloc(struct usbtouch_usb * usbtouch)922a8aef622SOliver Neukum static int nexio_alloc(struct usbtouch_usb *usbtouch)
923a8aef622SOliver Neukum {
924a8aef622SOliver Neukum 	struct nexio_priv *priv;
925a8aef622SOliver Neukum 	int ret = -ENOMEM;
926a8aef622SOliver Neukum 
927a8aef622SOliver Neukum 	usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
928a8aef622SOliver Neukum 	if (!usbtouch->priv)
929a8aef622SOliver Neukum 		goto out_buf;
930a8aef622SOliver Neukum 
931a8aef622SOliver Neukum 	priv = usbtouch->priv;
932a8aef622SOliver Neukum 
933a8aef622SOliver Neukum 	priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
934a8aef622SOliver Neukum 				GFP_KERNEL);
935a8aef622SOliver Neukum 	if (!priv->ack_buf)
936a8aef622SOliver Neukum 		goto err_priv;
937a8aef622SOliver Neukum 
938a8aef622SOliver Neukum 	priv->ack = usb_alloc_urb(0, GFP_KERNEL);
939a8aef622SOliver Neukum 	if (!priv->ack) {
9400a5ebc88SGreg Kroah-Hartman 		dev_dbg(&usbtouch->interface->dev,
941b741ab9dSGreg Kroah-Hartman 			"%s - usb_alloc_urb failed: usbtouch->ack\n", __func__);
942a8aef622SOliver Neukum 		goto err_ack_buf;
943a8aef622SOliver Neukum 	}
944a8aef622SOliver Neukum 
945a8aef622SOliver Neukum 	return 0;
946a8aef622SOliver Neukum 
947a8aef622SOliver Neukum err_ack_buf:
948a8aef622SOliver Neukum 	kfree(priv->ack_buf);
949a8aef622SOliver Neukum err_priv:
950a8aef622SOliver Neukum 	kfree(priv);
951a8aef622SOliver Neukum out_buf:
952a8aef622SOliver Neukum 	return ret;
953a8aef622SOliver Neukum }
954a8aef622SOliver Neukum 
nexio_init(struct usbtouch_usb * usbtouch)9555197424cSOndrej Zary static int nexio_init(struct usbtouch_usb *usbtouch)
9565197424cSOndrej Zary {
9575197424cSOndrej Zary 	struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
9585197424cSOndrej Zary 	struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
959a8aef622SOliver Neukum 	struct nexio_priv *priv = usbtouch->priv;
9605197424cSOndrej Zary 	int ret = -ENOMEM;
9615197424cSOndrej Zary 	int actual_len, i;
9625197424cSOndrej Zary 	unsigned char *buf;
9635197424cSOndrej Zary 	char *firmware_ver = NULL, *device_name = NULL;
9645197424cSOndrej Zary 	int input_ep = 0, output_ep = 0;
9655197424cSOndrej Zary 
9665197424cSOndrej Zary 	/* find first input and output endpoint */
9675197424cSOndrej Zary 	for (i = 0; i < interface->desc.bNumEndpoints; i++) {
9685197424cSOndrej Zary 		if (!input_ep &&
9695197424cSOndrej Zary 		    usb_endpoint_dir_in(&interface->endpoint[i].desc))
9705197424cSOndrej Zary 			input_ep = interface->endpoint[i].desc.bEndpointAddress;
9715197424cSOndrej Zary 		if (!output_ep &&
9725197424cSOndrej Zary 		    usb_endpoint_dir_out(&interface->endpoint[i].desc))
9735197424cSOndrej Zary 			output_ep = interface->endpoint[i].desc.bEndpointAddress;
9745197424cSOndrej Zary 	}
9755197424cSOndrej Zary 	if (!input_ep || !output_ep)
9765197424cSOndrej Zary 		return -ENXIO;
9775197424cSOndrej Zary 
978a8aef622SOliver Neukum 	buf = kmalloc(NEXIO_BUFSIZE, GFP_NOIO);
9795197424cSOndrej Zary 	if (!buf)
9805197424cSOndrej Zary 		goto out_buf;
9815197424cSOndrej Zary 
9825197424cSOndrej Zary 	/* two empty reads */
9835197424cSOndrej Zary 	for (i = 0; i < 2; i++) {
9845197424cSOndrej Zary 		ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
9855197424cSOndrej Zary 				   buf, NEXIO_BUFSIZE, &actual_len,
9865197424cSOndrej Zary 				   NEXIO_TIMEOUT);
9875197424cSOndrej Zary 		if (ret < 0)
9885197424cSOndrej Zary 			goto out_buf;
9895197424cSOndrej Zary 	}
9905197424cSOndrej Zary 
9915197424cSOndrej Zary 	/* send init command */
9925197424cSOndrej Zary 	memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
9935197424cSOndrej Zary 	ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
9945197424cSOndrej Zary 			   buf, sizeof(nexio_init_pkt), &actual_len,
9955197424cSOndrej Zary 			   NEXIO_TIMEOUT);
9965197424cSOndrej Zary 	if (ret < 0)
9975197424cSOndrej Zary 		goto out_buf;
9985197424cSOndrej Zary 
9995197424cSOndrej Zary 	/* read replies */
10005197424cSOndrej Zary 	for (i = 0; i < 3; i++) {
10015197424cSOndrej Zary 		memset(buf, 0, NEXIO_BUFSIZE);
10025197424cSOndrej Zary 		ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
10035197424cSOndrej Zary 				   buf, NEXIO_BUFSIZE, &actual_len,
10045197424cSOndrej Zary 				   NEXIO_TIMEOUT);
10055197424cSOndrej Zary 		if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
10065197424cSOndrej Zary 			continue;
10075197424cSOndrej Zary 		switch (buf[0]) {
10085197424cSOndrej Zary 		case 0x83:	/* firmware version */
10095197424cSOndrej Zary 			if (!firmware_ver)
1010a8aef622SOliver Neukum 				firmware_ver = kstrdup(&buf[2], GFP_NOIO);
10115197424cSOndrej Zary 			break;
10125197424cSOndrej Zary 		case 0x84:	/* device name */
10135197424cSOndrej Zary 			if (!device_name)
1014a8aef622SOliver Neukum 				device_name = kstrdup(&buf[2], GFP_NOIO);
10155197424cSOndrej Zary 			break;
10165197424cSOndrej Zary 		}
10175197424cSOndrej Zary 	}
10185197424cSOndrej Zary 
10195197424cSOndrej Zary 	printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
10205197424cSOndrej Zary 	       device_name, firmware_ver);
10215197424cSOndrej Zary 
10225197424cSOndrej Zary 	kfree(firmware_ver);
10235197424cSOndrej Zary 	kfree(device_name);
10245197424cSOndrej Zary 
10255197424cSOndrej Zary 	usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
10265197424cSOndrej Zary 			  priv->ack_buf, sizeof(nexio_ack_pkt),
10275197424cSOndrej Zary 			  nexio_ack_complete, usbtouch);
10285197424cSOndrej Zary 	ret = 0;
10295197424cSOndrej Zary 
10305197424cSOndrej Zary out_buf:
10315197424cSOndrej Zary 	kfree(buf);
10325197424cSOndrej Zary 	return ret;
10335197424cSOndrej Zary }
10345197424cSOndrej Zary 
nexio_exit(struct usbtouch_usb * usbtouch)10355197424cSOndrej Zary static void nexio_exit(struct usbtouch_usb *usbtouch)
10365197424cSOndrej Zary {
10375197424cSOndrej Zary 	struct nexio_priv *priv = usbtouch->priv;
10385197424cSOndrej Zary 
10395197424cSOndrej Zary 	usb_kill_urb(priv->ack);
10405197424cSOndrej Zary 	usb_free_urb(priv->ack);
10415197424cSOndrej Zary 	kfree(priv->ack_buf);
10425197424cSOndrej Zary 	kfree(priv);
10435197424cSOndrej Zary }
10445197424cSOndrej Zary 
nexio_read_data(struct usbtouch_usb * usbtouch,unsigned char * pkt)10455197424cSOndrej Zary static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
10465197424cSOndrej Zary {
104733f93726SLee Jones 	struct device *dev = &usbtouch->interface->dev;
10485197424cSOndrej Zary 	struct nexio_touch_packet *packet = (void *) pkt;
10495197424cSOndrej Zary 	struct nexio_priv *priv = usbtouch->priv;
10504aa5bbecSDmitry Torokhov 	unsigned int data_len = be16_to_cpu(packet->data_len);
10514aa5bbecSDmitry Torokhov 	unsigned int x_len = be16_to_cpu(packet->x_len);
10524aa5bbecSDmitry Torokhov 	unsigned int y_len = be16_to_cpu(packet->y_len);
10534aa5bbecSDmitry Torokhov 	int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
10545197424cSOndrej Zary 
10555197424cSOndrej Zary 	/* got touch data? */
10565197424cSOndrej Zary 	if ((pkt[0] & 0xe0) != 0xe0)
10575197424cSOndrej Zary 		return 0;
10585197424cSOndrej Zary 
10594aa5bbecSDmitry Torokhov 	if (data_len > 0xff)
10604aa5bbecSDmitry Torokhov 		data_len -= 0x100;
10614aa5bbecSDmitry Torokhov 	if (x_len > 0xff)
10624aa5bbecSDmitry Torokhov 		x_len -= 0x80;
1063388bbcadSOndrej Zary 
10645197424cSOndrej Zary 	/* send ACK */
10655197424cSOndrej Zary 	ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
106633f93726SLee Jones 	if (ret)
106733f93726SLee Jones 		dev_warn(dev, "Failed to submit ACK URB: %d\n", ret);
10685197424cSOndrej Zary 
10695197424cSOndrej Zary 	if (!usbtouch->type->max_xc) {
10704aa5bbecSDmitry Torokhov 		usbtouch->type->max_xc = 2 * x_len;
10714aa5bbecSDmitry Torokhov 		input_set_abs_params(usbtouch->input, ABS_X,
10724aa5bbecSDmitry Torokhov 				     0, usbtouch->type->max_xc, 0, 0);
10734aa5bbecSDmitry Torokhov 		usbtouch->type->max_yc = 2 * y_len;
10744aa5bbecSDmitry Torokhov 		input_set_abs_params(usbtouch->input, ABS_Y,
10754aa5bbecSDmitry Torokhov 				     0, usbtouch->type->max_yc, 0, 0);
10765197424cSOndrej Zary 	}
10775197424cSOndrej Zary 	/*
10785197424cSOndrej Zary 	 * The device reports state of IR sensors on X and Y axes.
10795197424cSOndrej Zary 	 * Each byte represents "darkness" percentage (0-100) of one element.
10805197424cSOndrej Zary 	 * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
10815197424cSOndrej Zary 	 * This also means that there's a limited multi-touch capability but
10825197424cSOndrej Zary 	 * it's disabled (and untested) here as there's no X driver for that.
10835197424cSOndrej Zary 	 */
10845197424cSOndrej Zary 	begin_x = end_x = begin_y = end_y = -1;
10854aa5bbecSDmitry Torokhov 	for (x = 0; x < x_len; x++) {
10865197424cSOndrej Zary 		if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
10875197424cSOndrej Zary 			begin_x = x;
10885197424cSOndrej Zary 			continue;
10895197424cSOndrej Zary 		}
10905197424cSOndrej Zary 		if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
10915197424cSOndrej Zary 			end_x = x - 1;
10924aa5bbecSDmitry Torokhov 			for (y = x_len; y < data_len; y++) {
10935197424cSOndrej Zary 				if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
10944aa5bbecSDmitry Torokhov 					begin_y = y - x_len;
10955197424cSOndrej Zary 					continue;
10965197424cSOndrej Zary 				}
10975197424cSOndrej Zary 				if (end_y == -1 &&
10985197424cSOndrej Zary 				    begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
10994aa5bbecSDmitry Torokhov 					end_y = y - 1 - x_len;
11005197424cSOndrej Zary 					w = end_x - begin_x;
11015197424cSOndrej Zary 					h = end_y - begin_y;
11025197424cSOndrej Zary #if 0
11035197424cSOndrej Zary 					/* multi-touch */
11045197424cSOndrej Zary 					input_report_abs(usbtouch->input,
11055197424cSOndrej Zary 						    ABS_MT_TOUCH_MAJOR, max(w,h));
11065197424cSOndrej Zary 					input_report_abs(usbtouch->input,
11075197424cSOndrej Zary 						    ABS_MT_TOUCH_MINOR, min(x,h));
11085197424cSOndrej Zary 					input_report_abs(usbtouch->input,
11095197424cSOndrej Zary 						    ABS_MT_POSITION_X, 2*begin_x+w);
11105197424cSOndrej Zary 					input_report_abs(usbtouch->input,
11115197424cSOndrej Zary 						    ABS_MT_POSITION_Y, 2*begin_y+h);
11125197424cSOndrej Zary 					input_report_abs(usbtouch->input,
11135197424cSOndrej Zary 						    ABS_MT_ORIENTATION, w > h);
11145197424cSOndrej Zary 					input_mt_sync(usbtouch->input);
11155197424cSOndrej Zary #endif
11165197424cSOndrej Zary 					/* single touch */
11175197424cSOndrej Zary 					usbtouch->x = 2 * begin_x + w;
11185197424cSOndrej Zary 					usbtouch->y = 2 * begin_y + h;
11195197424cSOndrej Zary 					usbtouch->touch = packet->flags & 0x01;
11205197424cSOndrej Zary 					begin_y = end_y = -1;
11215197424cSOndrej Zary 					return 1;
11225197424cSOndrej Zary 				}
11235197424cSOndrej Zary 			}
11245197424cSOndrej Zary 			begin_x = end_x = -1;
11255197424cSOndrej Zary 		}
11265197424cSOndrej Zary 
11275197424cSOndrej Zary 	}
11285197424cSOndrej Zary 	return 0;
11295197424cSOndrej Zary }
11305197424cSOndrej Zary #endif
11315197424cSOndrej Zary 
11325197424cSOndrej Zary 
11335197424cSOndrej Zary /*****************************************************************************
1134d2cc817aSMichael Gebetsroither  * ELO part
1135d2cc817aSMichael Gebetsroither  */
1136d2cc817aSMichael Gebetsroither 
1137d2cc817aSMichael Gebetsroither #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1138d2cc817aSMichael Gebetsroither 
elo_read_data(struct usbtouch_usb * dev,unsigned char * pkt)1139d2cc817aSMichael Gebetsroither static int elo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
1140d2cc817aSMichael Gebetsroither {
1141d2cc817aSMichael Gebetsroither 	dev->x = (pkt[3] << 8) | pkt[2];
1142d2cc817aSMichael Gebetsroither 	dev->y = (pkt[5] << 8) | pkt[4];
1143d2cc817aSMichael Gebetsroither 	dev->touch = pkt[6] > 0;
1144d2cc817aSMichael Gebetsroither 	dev->press = pkt[6];
1145d2cc817aSMichael Gebetsroither 
1146d2cc817aSMichael Gebetsroither 	return 1;
1147d2cc817aSMichael Gebetsroither }
1148d2cc817aSMichael Gebetsroither #endif
1149d2cc817aSMichael Gebetsroither 
1150d2cc817aSMichael Gebetsroither 
1151d2cc817aSMichael Gebetsroither /*****************************************************************************
1152d05e84e6SDmitry Torokhov  * the different device descriptors
1153d05e84e6SDmitry Torokhov  */
115462aa366dSDaniel Ritz #ifdef MULTI_PACKET
115562aa366dSDaniel Ritz static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
115662aa366dSDaniel Ritz 				   unsigned char *pkt, int len);
115762aa366dSDaniel Ritz #endif
115862aa366dSDaniel Ritz 
1159d05e84e6SDmitry Torokhov static struct usbtouch_device_info usbtouch_dev_info[] = {
1160d2cc817aSMichael Gebetsroither #ifdef CONFIG_TOUCHSCREEN_USB_ELO
1161d2cc817aSMichael Gebetsroither 	[DEVTYPE_ELO] = {
1162d2cc817aSMichael Gebetsroither 		.min_xc		= 0x0,
1163d2cc817aSMichael Gebetsroither 		.max_xc		= 0x0fff,
1164d2cc817aSMichael Gebetsroither 		.min_yc		= 0x0,
1165d2cc817aSMichael Gebetsroither 		.max_yc		= 0x0fff,
1166d2cc817aSMichael Gebetsroither 		.max_press	= 0xff,
1167d2cc817aSMichael Gebetsroither 		.rept_size	= 8,
1168d2cc817aSMichael Gebetsroither 		.read_data	= elo_read_data,
1169d2cc817aSMichael Gebetsroither 	},
1170d2cc817aSMichael Gebetsroither #endif
1171d2cc817aSMichael Gebetsroither 
1172c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
1173d05e84e6SDmitry Torokhov 	[DEVTYPE_EGALAX] = {
1174d05e84e6SDmitry Torokhov 		.min_xc		= 0x0,
1175d05e84e6SDmitry Torokhov 		.max_xc		= 0x07ff,
1176d05e84e6SDmitry Torokhov 		.min_yc		= 0x0,
1177d05e84e6SDmitry Torokhov 		.max_yc		= 0x07ff,
1178d05e84e6SDmitry Torokhov 		.rept_size	= 16,
1179d05e84e6SDmitry Torokhov 		.process_pkt	= usbtouch_process_multi,
1180d05e84e6SDmitry Torokhov 		.get_pkt_len	= egalax_get_pkt_len,
1181d05e84e6SDmitry Torokhov 		.read_data	= egalax_read_data,
1182037a833eSForest Bond 		.init		= egalax_init,
1183d05e84e6SDmitry Torokhov 	},
1184d05e84e6SDmitry Torokhov #endif
1185d05e84e6SDmitry Torokhov 
1186c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
1187d05e84e6SDmitry Torokhov 	[DEVTYPE_PANJIT] = {
1188d05e84e6SDmitry Torokhov 		.min_xc		= 0x0,
1189d05e84e6SDmitry Torokhov 		.max_xc		= 0x0fff,
1190d05e84e6SDmitry Torokhov 		.min_yc		= 0x0,
1191d05e84e6SDmitry Torokhov 		.max_yc		= 0x0fff,
1192d05e84e6SDmitry Torokhov 		.rept_size	= 8,
1193d05e84e6SDmitry Torokhov 		.read_data	= panjit_read_data,
1194d05e84e6SDmitry Torokhov 	},
1195d05e84e6SDmitry Torokhov #endif
1196d05e84e6SDmitry Torokhov 
1197c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_3M
1198d05e84e6SDmitry Torokhov 	[DEVTYPE_3M] = {
1199d05e84e6SDmitry Torokhov 		.min_xc		= 0x0,
1200d05e84e6SDmitry Torokhov 		.max_xc		= 0x4000,
1201d05e84e6SDmitry Torokhov 		.min_yc		= 0x0,
1202d05e84e6SDmitry Torokhov 		.max_yc		= 0x4000,
1203d05e84e6SDmitry Torokhov 		.rept_size	= 11,
1204d05e84e6SDmitry Torokhov 		.read_data	= mtouch_read_data,
120589f84b84SNick Dyer 		.alloc		= mtouch_alloc,
1206d05e84e6SDmitry Torokhov 		.init		= mtouch_init,
120789f84b84SNick Dyer 		.exit		= mtouch_exit,
1208d05e84e6SDmitry Torokhov 	},
1209d05e84e6SDmitry Torokhov #endif
1210d05e84e6SDmitry Torokhov 
1211c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_ITM
1212d05e84e6SDmitry Torokhov 	[DEVTYPE_ITM] = {
1213d05e84e6SDmitry Torokhov 		.min_xc		= 0x0,
1214d05e84e6SDmitry Torokhov 		.max_xc		= 0x0fff,
1215d05e84e6SDmitry Torokhov 		.min_yc		= 0x0,
1216d05e84e6SDmitry Torokhov 		.max_yc		= 0x0fff,
1217d05e84e6SDmitry Torokhov 		.max_press	= 0xff,
1218d05e84e6SDmitry Torokhov 		.rept_size	= 8,
1219d05e84e6SDmitry Torokhov 		.read_data	= itm_read_data,
1220d05e84e6SDmitry Torokhov 	},
1221d05e84e6SDmitry Torokhov #endif
1222d05e84e6SDmitry Torokhov 
1223c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
1224d05e84e6SDmitry Torokhov 	[DEVTYPE_ETURBO] = {
1225d05e84e6SDmitry Torokhov 		.min_xc		= 0x0,
1226d05e84e6SDmitry Torokhov 		.max_xc		= 0x07ff,
1227d05e84e6SDmitry Torokhov 		.min_yc		= 0x0,
1228d05e84e6SDmitry Torokhov 		.max_yc		= 0x07ff,
1229d05e84e6SDmitry Torokhov 		.rept_size	= 8,
1230d05e84e6SDmitry Torokhov 		.process_pkt	= usbtouch_process_multi,
1231d05e84e6SDmitry Torokhov 		.get_pkt_len	= eturbo_get_pkt_len,
1232d05e84e6SDmitry Torokhov 		.read_data	= eturbo_read_data,
1233d05e84e6SDmitry Torokhov 	},
1234d05e84e6SDmitry Torokhov #endif
1235d05e84e6SDmitry Torokhov 
1236c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
1237d05e84e6SDmitry Torokhov 	[DEVTYPE_GUNZE] = {
1238d05e84e6SDmitry Torokhov 		.min_xc		= 0x0,
1239d05e84e6SDmitry Torokhov 		.max_xc		= 0x0fff,
1240d05e84e6SDmitry Torokhov 		.min_yc		= 0x0,
1241d05e84e6SDmitry Torokhov 		.max_yc		= 0x0fff,
1242d05e84e6SDmitry Torokhov 		.rept_size	= 4,
1243d05e84e6SDmitry Torokhov 		.read_data	= gunze_read_data,
1244d05e84e6SDmitry Torokhov 	},
1245d05e84e6SDmitry Torokhov #endif
1246d05e84e6SDmitry Torokhov 
1247c6f8d706SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
1248d05e84e6SDmitry Torokhov 	[DEVTYPE_DMC_TSC10] = {
1249d05e84e6SDmitry Torokhov 		.min_xc		= 0x0,
1250d05e84e6SDmitry Torokhov 		.max_xc		= 0x03ff,
1251d05e84e6SDmitry Torokhov 		.min_yc		= 0x0,
1252d05e84e6SDmitry Torokhov 		.max_yc		= 0x03ff,
1253d05e84e6SDmitry Torokhov 		.rept_size	= 5,
1254d05e84e6SDmitry Torokhov 		.init		= dmc_tsc10_init,
1255d05e84e6SDmitry Torokhov 		.read_data	= dmc_tsc10_read_data,
1256d05e84e6SDmitry Torokhov 	},
1257d05e84e6SDmitry Torokhov #endif
1258df561fcdSOndrej Zary 
1259df561fcdSOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
1260df561fcdSOndrej Zary 	[DEVTYPE_IRTOUCH] = {
1261df561fcdSOndrej Zary 		.min_xc		= 0x0,
1262df561fcdSOndrej Zary 		.max_xc		= 0x0fff,
1263df561fcdSOndrej Zary 		.min_yc		= 0x0,
1264df561fcdSOndrej Zary 		.max_yc		= 0x0fff,
1265df561fcdSOndrej Zary 		.rept_size	= 8,
1266df561fcdSOndrej Zary 		.read_data	= irtouch_read_data,
1267df561fcdSOndrej Zary 	},
1268dbea4032SLars Poeschel 
1269dbea4032SLars Poeschel 	[DEVTYPE_IRTOUCH_HIRES] = {
1270dbea4032SLars Poeschel 		.min_xc		= 0x0,
1271dbea4032SLars Poeschel 		.max_xc		= 0x7fff,
1272dbea4032SLars Poeschel 		.min_yc		= 0x0,
1273dbea4032SLars Poeschel 		.max_yc		= 0x7fff,
1274dbea4032SLars Poeschel 		.rept_size	= 8,
1275dbea4032SLars Poeschel 		.read_data	= irtouch_read_data,
1276dbea4032SLars Poeschel 	},
1277df561fcdSOndrej Zary #endif
1278a14a8401SOndrej Zary 
1279a14a8401SOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
1280a14a8401SOndrej Zary 	[DEVTYPE_IDEALTEK] = {
1281a14a8401SOndrej Zary 		.min_xc		= 0x0,
1282a14a8401SOndrej Zary 		.max_xc		= 0x0fff,
1283a14a8401SOndrej Zary 		.min_yc		= 0x0,
1284a14a8401SOndrej Zary 		.max_yc		= 0x0fff,
1285a14a8401SOndrej Zary 		.rept_size	= 8,
1286a14a8401SOndrej Zary 		.process_pkt	= usbtouch_process_multi,
1287a14a8401SOndrej Zary 		.get_pkt_len	= idealtek_get_pkt_len,
1288a14a8401SOndrej Zary 		.read_data	= idealtek_read_data,
1289a14a8401SOndrej Zary 	},
1290a14a8401SOndrej Zary #endif
12919d5657dbSIlya Frolov 
12929d5657dbSIlya Frolov #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
12939d5657dbSIlya Frolov 	[DEVTYPE_GENERAL_TOUCH] = {
12949d5657dbSIlya Frolov 		.min_xc		= 0x0,
1295eb083ba2SRoy Yin 		.max_xc		= 0x7fff,
12969d5657dbSIlya Frolov 		.min_yc		= 0x0,
1297eb083ba2SRoy Yin 		.max_yc		= 0x7fff,
12989d5657dbSIlya Frolov 		.rept_size	= 7,
12999d5657dbSIlya Frolov 		.read_data	= general_touch_read_data,
130014e40206SJerrold Jones 	},
13019d5657dbSIlya Frolov #endif
13029d5657dbSIlya Frolov 
130314e40206SJerrold Jones #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
130414e40206SJerrold Jones 	[DEVTYPE_GOTOP] = {
130514e40206SJerrold Jones 		.min_xc		= 0x0,
130614e40206SJerrold Jones 		.max_xc		= 0x03ff,
130714e40206SJerrold Jones 		.min_yc		= 0x0,
130814e40206SJerrold Jones 		.max_yc		= 0x03ff,
130914e40206SJerrold Jones 		.rept_size	= 4,
131014e40206SJerrold Jones 		.read_data	= gotop_read_data,
131114e40206SJerrold Jones 	},
131214e40206SJerrold Jones #endif
1313f7370699SJim Persson 
1314f7370699SJim Persson #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
1315f7370699SJim Persson 	[DEVTYPE_JASTEC] = {
1316f7370699SJim Persson 		.min_xc		= 0x0,
1317f7370699SJim Persson 		.max_xc		= 0x0fff,
1318f7370699SJim Persson 		.min_yc		= 0x0,
1319f7370699SJim Persson 		.max_yc		= 0x0fff,
1320f7370699SJim Persson 		.rept_size	= 4,
1321f7370699SJim Persson 		.read_data	= jastec_read_data,
1322f7370699SJim Persson 	},
1323f7370699SJim Persson #endif
13249e3b2583SFlorian Echtler 
13259e3b2583SFlorian Echtler #ifdef CONFIG_TOUCHSCREEN_USB_E2I
13269e3b2583SFlorian Echtler 	[DEVTYPE_E2I] = {
13279e3b2583SFlorian Echtler 		.min_xc		= 0x0,
13289e3b2583SFlorian Echtler 		.max_xc		= 0x7fff,
13299e3b2583SFlorian Echtler 		.min_yc		= 0x0,
13309e3b2583SFlorian Echtler 		.max_yc		= 0x7fff,
13319e3b2583SFlorian Echtler 		.rept_size	= 6,
13329e3b2583SFlorian Echtler 		.init		= e2i_init,
13339e3b2583SFlorian Echtler 		.read_data	= e2i_read_data,
13349e3b2583SFlorian Echtler 	},
13359e3b2583SFlorian Echtler #endif
13362330ed18SDaniel Silverstone 
13372330ed18SDaniel Silverstone #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
13382330ed18SDaniel Silverstone 	[DEVTYPE_ZYTRONIC] = {
13392330ed18SDaniel Silverstone 		.min_xc		= 0x0,
13402330ed18SDaniel Silverstone 		.max_xc		= 0x03ff,
13412330ed18SDaniel Silverstone 		.min_yc		= 0x0,
13422330ed18SDaniel Silverstone 		.max_yc		= 0x03ff,
13432330ed18SDaniel Silverstone 		.rept_size	= 5,
13442330ed18SDaniel Silverstone 		.read_data	= zytronic_read_data,
13452330ed18SDaniel Silverstone 		.irq_always     = true,
13462330ed18SDaniel Silverstone 	},
13472330ed18SDaniel Silverstone #endif
1348dbe1420bSPetr Štetiar 
134938771bb4SPetr Štetiar #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC45USB
135038771bb4SPetr Štetiar 	[DEVTYPE_TC45USB] = {
1351dbe1420bSPetr Štetiar 		.min_xc		= 0x0,
1352dbe1420bSPetr Štetiar 		.max_xc		= 0x0fff,
1353dbe1420bSPetr Štetiar 		.min_yc		= 0x0,
1354dbe1420bSPetr Štetiar 		.max_yc		= 0x0fff,
1355dbe1420bSPetr Štetiar 		.rept_size	= 5,
135638771bb4SPetr Štetiar 		.read_data	= tc45usb_read_data,
1357dbe1420bSPetr Štetiar 	},
1358dbe1420bSPetr Štetiar #endif
13595197424cSOndrej Zary 
13605197424cSOndrej Zary #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
13615197424cSOndrej Zary 	[DEVTYPE_NEXIO] = {
1362388bbcadSOndrej Zary 		.rept_size	= 1024,
13635197424cSOndrej Zary 		.irq_always	= true,
13645197424cSOndrej Zary 		.read_data	= nexio_read_data,
1365a8aef622SOliver Neukum 		.alloc		= nexio_alloc,
13665197424cSOndrej Zary 		.init		= nexio_init,
13675197424cSOndrej Zary 		.exit		= nexio_exit,
13685197424cSOndrej Zary 	},
13695197424cSOndrej Zary #endif
1370aa87512fSArmando Visconti #ifdef CONFIG_TOUCHSCREEN_USB_EASYTOUCH
1371aa87512fSArmando Visconti 	[DEVTYPE_ETOUCH] = {
1372aa87512fSArmando Visconti 		.min_xc		= 0x0,
1373aa87512fSArmando Visconti 		.max_xc		= 0x07ff,
1374aa87512fSArmando Visconti 		.min_yc		= 0x0,
1375aa87512fSArmando Visconti 		.max_yc		= 0x07ff,
1376aa87512fSArmando Visconti 		.rept_size	= 16,
1377aa87512fSArmando Visconti 		.process_pkt	= usbtouch_process_multi,
1378aa87512fSArmando Visconti 		.get_pkt_len	= etouch_get_pkt_len,
1379aa87512fSArmando Visconti 		.read_data	= etouch_read_data,
1380aa87512fSArmando Visconti 	},
1381aa87512fSArmando Visconti #endif
1382d05e84e6SDmitry Torokhov };
1383d05e84e6SDmitry Torokhov 
1384d05e84e6SDmitry Torokhov 
1385d05e84e6SDmitry Torokhov /*****************************************************************************
1386d05e84e6SDmitry Torokhov  * Generic Part
1387d05e84e6SDmitry Torokhov  */
usbtouch_process_pkt(struct usbtouch_usb * usbtouch,unsigned char * pkt,int len)1388d05e84e6SDmitry Torokhov static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
1389d05e84e6SDmitry Torokhov                                  unsigned char *pkt, int len)
1390d05e84e6SDmitry Torokhov {
1391d05e84e6SDmitry Torokhov 	struct usbtouch_device_info *type = usbtouch->type;
1392d05e84e6SDmitry Torokhov 
1393d05e84e6SDmitry Torokhov 	if (!type->read_data(usbtouch, pkt))
1394d05e84e6SDmitry Torokhov 			return;
1395d05e84e6SDmitry Torokhov 
1396d05e84e6SDmitry Torokhov 	input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
1397d05e84e6SDmitry Torokhov 
1398d05e84e6SDmitry Torokhov 	if (swap_xy) {
1399d05e84e6SDmitry Torokhov 		input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
1400d05e84e6SDmitry Torokhov 		input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
1401d05e84e6SDmitry Torokhov 	} else {
1402d05e84e6SDmitry Torokhov 		input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
1403d05e84e6SDmitry Torokhov 		input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
1404d05e84e6SDmitry Torokhov 	}
1405d05e84e6SDmitry Torokhov 	if (type->max_press)
1406d05e84e6SDmitry Torokhov 		input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
1407d05e84e6SDmitry Torokhov 	input_sync(usbtouch->input);
1408d05e84e6SDmitry Torokhov }
1409d05e84e6SDmitry Torokhov 
1410d05e84e6SDmitry Torokhov 
1411d05e84e6SDmitry Torokhov #ifdef MULTI_PACKET
usbtouch_process_multi(struct usbtouch_usb * usbtouch,unsigned char * pkt,int len)1412d05e84e6SDmitry Torokhov static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
1413d05e84e6SDmitry Torokhov                                    unsigned char *pkt, int len)
1414d05e84e6SDmitry Torokhov {
1415d05e84e6SDmitry Torokhov 	unsigned char *buffer;
1416d05e84e6SDmitry Torokhov 	int pkt_len, pos, buf_len, tmp;
1417d05e84e6SDmitry Torokhov 
1418d05e84e6SDmitry Torokhov 	/* process buffer */
1419d05e84e6SDmitry Torokhov 	if (unlikely(usbtouch->buf_len)) {
1420d05e84e6SDmitry Torokhov 		/* try to get size */
1421d05e84e6SDmitry Torokhov 		pkt_len = usbtouch->type->get_pkt_len(
1422d05e84e6SDmitry Torokhov 				usbtouch->buffer, usbtouch->buf_len);
1423d05e84e6SDmitry Torokhov 
1424d05e84e6SDmitry Torokhov 		/* drop? */
1425d05e84e6SDmitry Torokhov 		if (unlikely(!pkt_len))
1426d05e84e6SDmitry Torokhov 			goto out_flush_buf;
1427d05e84e6SDmitry Torokhov 
1428d05e84e6SDmitry Torokhov 		/* need to append -pkt_len bytes before able to get size */
1429d05e84e6SDmitry Torokhov 		if (unlikely(pkt_len < 0)) {
1430d05e84e6SDmitry Torokhov 			int append = -pkt_len;
1431d05e84e6SDmitry Torokhov 			if (unlikely(append > len))
1432d05e84e6SDmitry Torokhov 			       append = len;
1433d05e84e6SDmitry Torokhov 			if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
1434d05e84e6SDmitry Torokhov 				goto out_flush_buf;
1435d05e84e6SDmitry Torokhov 			memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
1436d05e84e6SDmitry Torokhov 			usbtouch->buf_len += append;
1437d05e84e6SDmitry Torokhov 
1438d05e84e6SDmitry Torokhov 			pkt_len = usbtouch->type->get_pkt_len(
1439d05e84e6SDmitry Torokhov 					usbtouch->buffer, usbtouch->buf_len);
1440d05e84e6SDmitry Torokhov 			if (pkt_len < 0)
1441d05e84e6SDmitry Torokhov 				return;
1442d05e84e6SDmitry Torokhov 		}
1443d05e84e6SDmitry Torokhov 
1444d05e84e6SDmitry Torokhov 		/* append */
1445d05e84e6SDmitry Torokhov 		tmp = pkt_len - usbtouch->buf_len;
1446d05e84e6SDmitry Torokhov 		if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
1447d05e84e6SDmitry Torokhov 			goto out_flush_buf;
1448d05e84e6SDmitry Torokhov 		memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
1449d05e84e6SDmitry Torokhov 		usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
1450d05e84e6SDmitry Torokhov 
1451d05e84e6SDmitry Torokhov 		buffer = pkt + tmp;
1452d05e84e6SDmitry Torokhov 		buf_len = len - tmp;
1453d05e84e6SDmitry Torokhov 	} else {
1454d05e84e6SDmitry Torokhov 		buffer = pkt;
1455d05e84e6SDmitry Torokhov 		buf_len = len;
1456d05e84e6SDmitry Torokhov 	}
1457d05e84e6SDmitry Torokhov 
1458d05e84e6SDmitry Torokhov 	/* loop over the received packet, process */
1459d05e84e6SDmitry Torokhov 	pos = 0;
1460d05e84e6SDmitry Torokhov 	while (pos < buf_len) {
1461d05e84e6SDmitry Torokhov 		/* get packet len */
146262aa366dSDaniel Ritz 		pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
146362aa366dSDaniel Ritz 							buf_len - pos);
1464d05e84e6SDmitry Torokhov 
146562aa366dSDaniel Ritz 		/* unknown packet: skip one byte */
146662aa366dSDaniel Ritz 		if (unlikely(!pkt_len)) {
146762aa366dSDaniel Ritz 			pos++;
146862aa366dSDaniel Ritz 			continue;
146962aa366dSDaniel Ritz 		}
1470d05e84e6SDmitry Torokhov 
1471d05e84e6SDmitry Torokhov 		/* full packet: process */
1472d05e84e6SDmitry Torokhov 		if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
1473d05e84e6SDmitry Torokhov 			usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
1474d05e84e6SDmitry Torokhov 		} else {
1475d05e84e6SDmitry Torokhov 			/* incomplete packet: save in buffer */
1476d05e84e6SDmitry Torokhov 			memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
1477d05e84e6SDmitry Torokhov 			usbtouch->buf_len = buf_len - pos;
1478d05e84e6SDmitry Torokhov 			return;
1479d05e84e6SDmitry Torokhov 		}
1480d05e84e6SDmitry Torokhov 		pos += pkt_len;
1481d05e84e6SDmitry Torokhov 	}
1482d05e84e6SDmitry Torokhov 
1483d05e84e6SDmitry Torokhov out_flush_buf:
1484d05e84e6SDmitry Torokhov 	usbtouch->buf_len = 0;
1485d05e84e6SDmitry Torokhov 	return;
1486d05e84e6SDmitry Torokhov }
1487d05e84e6SDmitry Torokhov #endif
1488d05e84e6SDmitry Torokhov 
1489d05e84e6SDmitry Torokhov 
usbtouch_irq(struct urb * urb)1490d05e84e6SDmitry Torokhov static void usbtouch_irq(struct urb *urb)
1491d05e84e6SDmitry Torokhov {
1492d05e84e6SDmitry Torokhov 	struct usbtouch_usb *usbtouch = urb->context;
14930a5ebc88SGreg Kroah-Hartman 	struct device *dev = &usbtouch->interface->dev;
1494d05e84e6SDmitry Torokhov 	int retval;
1495d05e84e6SDmitry Torokhov 
1496d05e84e6SDmitry Torokhov 	switch (urb->status) {
1497d05e84e6SDmitry Torokhov 	case 0:
1498d05e84e6SDmitry Torokhov 		/* success */
1499d05e84e6SDmitry Torokhov 		break;
1500d05e84e6SDmitry Torokhov 	case -ETIME:
1501d05e84e6SDmitry Torokhov 		/* this urb is timing out */
1502b741ab9dSGreg Kroah-Hartman 		dev_dbg(dev,
1503b741ab9dSGreg Kroah-Hartman 			"%s - urb timed out - was the device unplugged?\n",
1504ea3e6c59SHarvey Harrison 			__func__);
1505d05e84e6SDmitry Torokhov 		return;
1506d05e84e6SDmitry Torokhov 	case -ECONNRESET:
1507d05e84e6SDmitry Torokhov 	case -ENOENT:
1508d05e84e6SDmitry Torokhov 	case -ESHUTDOWN:
15095197424cSOndrej Zary 	case -EPIPE:
1510d05e84e6SDmitry Torokhov 		/* this urb is terminated, clean up */
1511b741ab9dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - urb shutting down with status: %d\n",
1512ea3e6c59SHarvey Harrison 			__func__, urb->status);
1513d05e84e6SDmitry Torokhov 		return;
1514d05e84e6SDmitry Torokhov 	default:
1515b741ab9dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - nonzero urb status received: %d\n",
1516ea3e6c59SHarvey Harrison 			__func__, urb->status);
1517d05e84e6SDmitry Torokhov 		goto exit;
1518d05e84e6SDmitry Torokhov 	}
1519d05e84e6SDmitry Torokhov 
1520d05e84e6SDmitry Torokhov 	usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
1521d05e84e6SDmitry Torokhov 
1522d05e84e6SDmitry Torokhov exit:
15235d9efc59SOliver Neukum 	usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
1524d05e84e6SDmitry Torokhov 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1525d05e84e6SDmitry Torokhov 	if (retval)
15260a5ebc88SGreg Kroah-Hartman 		dev_err(dev, "%s - usb_submit_urb failed with result: %d\n",
1527ea3e6c59SHarvey Harrison 			__func__, retval);
1528d05e84e6SDmitry Torokhov }
1529d05e84e6SDmitry Torokhov 
usbtouch_open(struct input_dev * input)1530d05e84e6SDmitry Torokhov static int usbtouch_open(struct input_dev *input)
1531d05e84e6SDmitry Torokhov {
1532d05e84e6SDmitry Torokhov 	struct usbtouch_usb *usbtouch = input_get_drvdata(input);
15335d9efc59SOliver Neukum 	int r;
1534d05e84e6SDmitry Torokhov 
1535fea4d14bSOndrej Zary 	usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
1536d05e84e6SDmitry Torokhov 
15375d9efc59SOliver Neukum 	r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
15385d9efc59SOliver Neukum 	if (r < 0)
15395d9efc59SOliver Neukum 		goto out;
15405d9efc59SOliver Neukum 
154112e510dbSMarcus Folkesson 	mutex_lock(&usbtouch->pm_mutex);
15422330ed18SDaniel Silverstone 	if (!usbtouch->type->irq_always) {
15435d9efc59SOliver Neukum 		if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
15445d9efc59SOliver Neukum 			r = -EIO;
15455d9efc59SOliver Neukum 			goto out_put;
15465d9efc59SOliver Neukum 		}
15472330ed18SDaniel Silverstone 	}
1548d05e84e6SDmitry Torokhov 
15495d9efc59SOliver Neukum 	usbtouch->interface->needs_remote_wakeup = 1;
155085f46fbfSMarcus Folkesson 	usbtouch->is_open = true;
15515d9efc59SOliver Neukum out_put:
155212e510dbSMarcus Folkesson 	mutex_unlock(&usbtouch->pm_mutex);
15535d9efc59SOliver Neukum 	usb_autopm_put_interface(usbtouch->interface);
15545d9efc59SOliver Neukum out:
15555d9efc59SOliver Neukum 	return r;
1556d05e84e6SDmitry Torokhov }
1557d05e84e6SDmitry Torokhov 
usbtouch_close(struct input_dev * input)1558d05e84e6SDmitry Torokhov static void usbtouch_close(struct input_dev *input)
1559d05e84e6SDmitry Torokhov {
1560d05e84e6SDmitry Torokhov 	struct usbtouch_usb *usbtouch = input_get_drvdata(input);
15615d9efc59SOliver Neukum 	int r;
1562d05e84e6SDmitry Torokhov 
156312e510dbSMarcus Folkesson 	mutex_lock(&usbtouch->pm_mutex);
15642330ed18SDaniel Silverstone 	if (!usbtouch->type->irq_always)
1565d05e84e6SDmitry Torokhov 		usb_kill_urb(usbtouch->irq);
156685f46fbfSMarcus Folkesson 	usbtouch->is_open = false;
156712e510dbSMarcus Folkesson 	mutex_unlock(&usbtouch->pm_mutex);
156812e510dbSMarcus Folkesson 
15695d9efc59SOliver Neukum 	r = usb_autopm_get_interface(usbtouch->interface);
15705d9efc59SOliver Neukum 	usbtouch->interface->needs_remote_wakeup = 0;
15715d9efc59SOliver Neukum 	if (!r)
15725d9efc59SOliver Neukum 		usb_autopm_put_interface(usbtouch->interface);
1573d05e84e6SDmitry Torokhov }
1574d05e84e6SDmitry Torokhov 
usbtouch_suspend(struct usb_interface * intf,pm_message_t message)1575ed4299e1SOliver Neukum static int usbtouch_suspend
1576ed4299e1SOliver Neukum (struct usb_interface *intf, pm_message_t message)
1577ed4299e1SOliver Neukum {
1578ed4299e1SOliver Neukum 	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1579ed4299e1SOliver Neukum 
1580ed4299e1SOliver Neukum 	usb_kill_urb(usbtouch->irq);
1581ed4299e1SOliver Neukum 
1582ed4299e1SOliver Neukum 	return 0;
1583ed4299e1SOliver Neukum }
1584ed4299e1SOliver Neukum 
usbtouch_resume(struct usb_interface * intf)1585ed4299e1SOliver Neukum static int usbtouch_resume(struct usb_interface *intf)
1586ed4299e1SOliver Neukum {
1587ed4299e1SOliver Neukum 	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1588ed4299e1SOliver Neukum 	int result = 0;
1589ed4299e1SOliver Neukum 
159012e510dbSMarcus Folkesson 	mutex_lock(&usbtouch->pm_mutex);
159185f46fbfSMarcus Folkesson 	if (usbtouch->is_open || usbtouch->type->irq_always)
1592ed4299e1SOliver Neukum 		result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
159312e510dbSMarcus Folkesson 	mutex_unlock(&usbtouch->pm_mutex);
1594ed4299e1SOliver Neukum 
1595ed4299e1SOliver Neukum 	return result;
1596ed4299e1SOliver Neukum }
1597d05e84e6SDmitry Torokhov 
usbtouch_reset_resume(struct usb_interface * intf)1598a8aef622SOliver Neukum static int usbtouch_reset_resume(struct usb_interface *intf)
1599a8aef622SOliver Neukum {
1600a8aef622SOliver Neukum 	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1601a8aef622SOliver Neukum 	int err = 0;
1602a8aef622SOliver Neukum 
1603a8aef622SOliver Neukum 	/* reinit the device */
1604a8aef622SOliver Neukum 	if (usbtouch->type->init) {
1605a8aef622SOliver Neukum 		err = usbtouch->type->init(usbtouch);
1606a8aef622SOliver Neukum 		if (err) {
16070a5ebc88SGreg Kroah-Hartman 			dev_dbg(&intf->dev,
1608b741ab9dSGreg Kroah-Hartman 				"%s - type->init() failed, err: %d\n",
1609a8aef622SOliver Neukum 				__func__, err);
1610a8aef622SOliver Neukum 			return err;
1611a8aef622SOliver Neukum 		}
1612a8aef622SOliver Neukum 	}
1613a8aef622SOliver Neukum 
1614a8aef622SOliver Neukum 	/* restart IO if needed */
161512e510dbSMarcus Folkesson 	mutex_lock(&usbtouch->pm_mutex);
161685f46fbfSMarcus Folkesson 	if (usbtouch->is_open)
1617a8aef622SOliver Neukum 		err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
161812e510dbSMarcus Folkesson 	mutex_unlock(&usbtouch->pm_mutex);
1619a8aef622SOliver Neukum 
1620a8aef622SOliver Neukum 	return err;
1621a8aef622SOliver Neukum }
1622a8aef622SOliver Neukum 
usbtouch_free_buffers(struct usb_device * udev,struct usbtouch_usb * usbtouch)1623d05e84e6SDmitry Torokhov static void usbtouch_free_buffers(struct usb_device *udev,
1624d05e84e6SDmitry Torokhov 				  struct usbtouch_usb *usbtouch)
1625d05e84e6SDmitry Torokhov {
16264ef38351SChristian Engelmayer 	usb_free_coherent(udev, usbtouch->data_size,
1627d05e84e6SDmitry Torokhov 			  usbtouch->data, usbtouch->data_dma);
1628d05e84e6SDmitry Torokhov 	kfree(usbtouch->buffer);
1629d05e84e6SDmitry Torokhov }
1630d05e84e6SDmitry Torokhov 
1631f4a5e359SOndrej Zary static struct usb_endpoint_descriptor *
usbtouch_get_input_endpoint(struct usb_host_interface * interface)1632f4a5e359SOndrej Zary usbtouch_get_input_endpoint(struct usb_host_interface *interface)
1633f4a5e359SOndrej Zary {
1634f4a5e359SOndrej Zary 	int i;
1635f4a5e359SOndrej Zary 
1636f4a5e359SOndrej Zary 	for (i = 0; i < interface->desc.bNumEndpoints; i++)
1637f4a5e359SOndrej Zary 		if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
1638f4a5e359SOndrej Zary 			return &interface->endpoint[i].desc;
1639f4a5e359SOndrej Zary 
1640f4a5e359SOndrej Zary 	return NULL;
1641f4a5e359SOndrej Zary }
1642d05e84e6SDmitry Torokhov 
usbtouch_probe(struct usb_interface * intf,const struct usb_device_id * id)1643d05e84e6SDmitry Torokhov static int usbtouch_probe(struct usb_interface *intf,
1644d05e84e6SDmitry Torokhov 			  const struct usb_device_id *id)
1645d05e84e6SDmitry Torokhov {
1646d05e84e6SDmitry Torokhov 	struct usbtouch_usb *usbtouch;
1647d05e84e6SDmitry Torokhov 	struct input_dev *input_dev;
1648d05e84e6SDmitry Torokhov 	struct usb_endpoint_descriptor *endpoint;
1649d05e84e6SDmitry Torokhov 	struct usb_device *udev = interface_to_usbdev(intf);
1650d05e84e6SDmitry Torokhov 	struct usbtouch_device_info *type;
1651d05e84e6SDmitry Torokhov 	int err = -ENOMEM;
1652d05e84e6SDmitry Torokhov 
1653ec42d448SDaniel Ritz 	/* some devices are ignored */
1654ec42d448SDaniel Ritz 	if (id->driver_info == DEVTYPE_IGNORE)
1655ec42d448SDaniel Ritz 		return -ENODEV;
1656ec42d448SDaniel Ritz 
1657039d4ed3SJohan Hovold 	if (id->driver_info >= ARRAY_SIZE(usbtouch_dev_info))
1658039d4ed3SJohan Hovold 		return -ENODEV;
1659039d4ed3SJohan Hovold 
1660f4a5e359SOndrej Zary 	endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
1661f4a5e359SOndrej Zary 	if (!endpoint)
1662f4a5e359SOndrej Zary 		return -ENXIO;
1663d05e84e6SDmitry Torokhov 
1664d05e84e6SDmitry Torokhov 	usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
1665d05e84e6SDmitry Torokhov 	input_dev = input_allocate_device();
1666d05e84e6SDmitry Torokhov 	if (!usbtouch || !input_dev)
1667d05e84e6SDmitry Torokhov 		goto out_free;
1668d05e84e6SDmitry Torokhov 
1669b55d996fSOliver Neukum 	mutex_init(&usbtouch->pm_mutex);
1670b55d996fSOliver Neukum 
1671d05e84e6SDmitry Torokhov 	type = &usbtouch_dev_info[id->driver_info];
1672d05e84e6SDmitry Torokhov 	usbtouch->type = type;
1673d05e84e6SDmitry Torokhov 	if (!type->process_pkt)
1674d05e84e6SDmitry Torokhov 		type->process_pkt = usbtouch_process_pkt;
1675d05e84e6SDmitry Torokhov 
16764ef38351SChristian Engelmayer 	usbtouch->data_size = type->rept_size;
16774ef38351SChristian Engelmayer 	if (type->get_pkt_len) {
16784ef38351SChristian Engelmayer 		/*
16794ef38351SChristian Engelmayer 		 * When dealing with variable-length packets we should
16804ef38351SChristian Engelmayer 		 * not request more than wMaxPacketSize bytes at once
16814ef38351SChristian Engelmayer 		 * as we do not know if there is more data coming or
16824ef38351SChristian Engelmayer 		 * we filled exactly wMaxPacketSize bytes and there is
16834ef38351SChristian Engelmayer 		 * nothing else.
16844ef38351SChristian Engelmayer 		 */
16854ef38351SChristian Engelmayer 		usbtouch->data_size = min(usbtouch->data_size,
16864ef38351SChristian Engelmayer 					  usb_endpoint_maxp(endpoint));
16874ef38351SChristian Engelmayer 	}
16884ef38351SChristian Engelmayer 
16894ef38351SChristian Engelmayer 	usbtouch->data = usb_alloc_coherent(udev, usbtouch->data_size,
1690d05e84e6SDmitry Torokhov 					    GFP_KERNEL, &usbtouch->data_dma);
1691d05e84e6SDmitry Torokhov 	if (!usbtouch->data)
1692d05e84e6SDmitry Torokhov 		goto out_free;
1693d05e84e6SDmitry Torokhov 
169462aa366dSDaniel Ritz 	if (type->get_pkt_len) {
1695d05e84e6SDmitry Torokhov 		usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
1696d05e84e6SDmitry Torokhov 		if (!usbtouch->buffer)
1697d05e84e6SDmitry Torokhov 			goto out_free_buffers;
1698d05e84e6SDmitry Torokhov 	}
1699d05e84e6SDmitry Torokhov 
1700d05e84e6SDmitry Torokhov 	usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
1701d05e84e6SDmitry Torokhov 	if (!usbtouch->irq) {
1702b741ab9dSGreg Kroah-Hartman 		dev_dbg(&intf->dev,
1703b741ab9dSGreg Kroah-Hartman 			"%s - usb_alloc_urb failed: usbtouch->irq\n", __func__);
1704d05e84e6SDmitry Torokhov 		goto out_free_buffers;
1705d05e84e6SDmitry Torokhov 	}
1706d05e84e6SDmitry Torokhov 
1707fea4d14bSOndrej Zary 	usbtouch->interface = intf;
1708d05e84e6SDmitry Torokhov 	usbtouch->input = input_dev;
1709d05e84e6SDmitry Torokhov 
1710d05e84e6SDmitry Torokhov 	if (udev->manufacturer)
1711*a9f08ad7SWolfram Sang 		strscpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
1712d05e84e6SDmitry Torokhov 
1713d05e84e6SDmitry Torokhov 	if (udev->product) {
1714d05e84e6SDmitry Torokhov 		if (udev->manufacturer)
1715d05e84e6SDmitry Torokhov 			strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
1716d05e84e6SDmitry Torokhov 		strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
1717d05e84e6SDmitry Torokhov 	}
1718d05e84e6SDmitry Torokhov 
1719d05e84e6SDmitry Torokhov 	if (!strlen(usbtouch->name))
1720d05e84e6SDmitry Torokhov 		snprintf(usbtouch->name, sizeof(usbtouch->name),
1721d05e84e6SDmitry Torokhov 			"USB Touchscreen %04x:%04x",
1722d05e84e6SDmitry Torokhov 			 le16_to_cpu(udev->descriptor.idVendor),
1723d05e84e6SDmitry Torokhov 			 le16_to_cpu(udev->descriptor.idProduct));
1724d05e84e6SDmitry Torokhov 
1725d05e84e6SDmitry Torokhov 	usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
17267b6dff98SVladimir Shebordaev 	strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
1727d05e84e6SDmitry Torokhov 
1728d05e84e6SDmitry Torokhov 	input_dev->name = usbtouch->name;
1729d05e84e6SDmitry Torokhov 	input_dev->phys = usbtouch->phys;
1730d05e84e6SDmitry Torokhov 	usb_to_input_id(udev, &input_dev->id);
1731d05e84e6SDmitry Torokhov 	input_dev->dev.parent = &intf->dev;
1732d05e84e6SDmitry Torokhov 
1733d05e84e6SDmitry Torokhov 	input_set_drvdata(input_dev, usbtouch);
1734d05e84e6SDmitry Torokhov 
1735d05e84e6SDmitry Torokhov 	input_dev->open = usbtouch_open;
1736d05e84e6SDmitry Torokhov 	input_dev->close = usbtouch_close;
1737d05e84e6SDmitry Torokhov 
17387b19ada2SJiri Slaby 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
17397b19ada2SJiri Slaby 	input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1740d05e84e6SDmitry Torokhov 	input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
1741d05e84e6SDmitry Torokhov 	input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
1742d05e84e6SDmitry Torokhov 	if (type->max_press)
1743d05e84e6SDmitry Torokhov 		input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
1744d05e84e6SDmitry Torokhov 		                     type->max_press, 0, 0);
1745d05e84e6SDmitry Torokhov 
17465197424cSOndrej Zary 	if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
1747fea4d14bSOndrej Zary 		usb_fill_int_urb(usbtouch->irq, udev,
1748fea4d14bSOndrej Zary 			 usb_rcvintpipe(udev, endpoint->bEndpointAddress),
17494ef38351SChristian Engelmayer 			 usbtouch->data, usbtouch->data_size,
1750d05e84e6SDmitry Torokhov 			 usbtouch_irq, usbtouch, endpoint->bInterval);
17515197424cSOndrej Zary 	else
17525197424cSOndrej Zary 		usb_fill_bulk_urb(usbtouch->irq, udev,
17535197424cSOndrej Zary 			 usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
17544ef38351SChristian Engelmayer 			 usbtouch->data, usbtouch->data_size,
17555197424cSOndrej Zary 			 usbtouch_irq, usbtouch);
1756d05e84e6SDmitry Torokhov 
1757fea4d14bSOndrej Zary 	usbtouch->irq->dev = udev;
1758d05e84e6SDmitry Torokhov 	usbtouch->irq->transfer_dma = usbtouch->data_dma;
1759d05e84e6SDmitry Torokhov 	usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1760d05e84e6SDmitry Torokhov 
1761a8aef622SOliver Neukum 	/* device specific allocations */
1762a8aef622SOliver Neukum 	if (type->alloc) {
1763a8aef622SOliver Neukum 		err = type->alloc(usbtouch);
1764a8aef622SOliver Neukum 		if (err) {
1765b741ab9dSGreg Kroah-Hartman 			dev_dbg(&intf->dev,
1766b741ab9dSGreg Kroah-Hartman 				"%s - type->alloc() failed, err: %d\n",
1767b741ab9dSGreg Kroah-Hartman 				__func__, err);
1768a8aef622SOliver Neukum 			goto out_free_urb;
1769a8aef622SOliver Neukum 		}
1770a8aef622SOliver Neukum 	}
1771a8aef622SOliver Neukum 
1772a8aef622SOliver Neukum 	/* device specific initialisation*/
1773d05e84e6SDmitry Torokhov 	if (type->init) {
1774d05e84e6SDmitry Torokhov 		err = type->init(usbtouch);
1775d05e84e6SDmitry Torokhov 		if (err) {
1776b741ab9dSGreg Kroah-Hartman 			dev_dbg(&intf->dev,
1777b741ab9dSGreg Kroah-Hartman 				"%s - type->init() failed, err: %d\n",
1778b741ab9dSGreg Kroah-Hartman 				__func__, err);
1779a8aef622SOliver Neukum 			goto out_do_exit;
1780d05e84e6SDmitry Torokhov 		}
1781d05e84e6SDmitry Torokhov 	}
1782d05e84e6SDmitry Torokhov 
1783d05e84e6SDmitry Torokhov 	err = input_register_device(usbtouch->input);
1784d05e84e6SDmitry Torokhov 	if (err) {
1785b741ab9dSGreg Kroah-Hartman 		dev_dbg(&intf->dev,
1786b741ab9dSGreg Kroah-Hartman 			"%s - input_register_device failed, err: %d\n",
1787b741ab9dSGreg Kroah-Hartman 			__func__, err);
17885197424cSOndrej Zary 		goto out_do_exit;
1789d05e84e6SDmitry Torokhov 	}
1790d05e84e6SDmitry Torokhov 
1791d05e84e6SDmitry Torokhov 	usb_set_intfdata(intf, usbtouch);
1792d05e84e6SDmitry Torokhov 
17931e87a430SOndrej Zary 	if (usbtouch->type->irq_always) {
17945d9efc59SOliver Neukum 		/* this can't fail */
17955d9efc59SOliver Neukum 		usb_autopm_get_interface(intf);
17961e87a430SOndrej Zary 		err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
17971e87a430SOndrej Zary 		if (err) {
17985d9efc59SOliver Neukum 			usb_autopm_put_interface(intf);
17990a5ebc88SGreg Kroah-Hartman 			dev_err(&intf->dev,
1800e27ad0feSGreg Kroah-Hartman 				"%s - usb_submit_urb failed with result: %d\n",
18011e87a430SOndrej Zary 				__func__, err);
18021e87a430SOndrej Zary 			goto out_unregister_input;
18031e87a430SOndrej Zary 		}
18041e87a430SOndrej Zary 	}
18052330ed18SDaniel Silverstone 
1806d05e84e6SDmitry Torokhov 	return 0;
1807d05e84e6SDmitry Torokhov 
18081e87a430SOndrej Zary out_unregister_input:
18091e87a430SOndrej Zary 	input_unregister_device(input_dev);
18101e87a430SOndrej Zary 	input_dev = NULL;
18115197424cSOndrej Zary out_do_exit:
18125197424cSOndrej Zary 	if (type->exit)
18135197424cSOndrej Zary 		type->exit(usbtouch);
18141e87a430SOndrej Zary out_free_urb:
18151e87a430SOndrej Zary 	usb_free_urb(usbtouch->irq);
1816d05e84e6SDmitry Torokhov out_free_buffers:
1817d05e84e6SDmitry Torokhov 	usbtouch_free_buffers(udev, usbtouch);
1818d05e84e6SDmitry Torokhov out_free:
1819d05e84e6SDmitry Torokhov 	input_free_device(input_dev);
1820d05e84e6SDmitry Torokhov 	kfree(usbtouch);
1821d05e84e6SDmitry Torokhov 	return err;
1822d05e84e6SDmitry Torokhov }
1823d05e84e6SDmitry Torokhov 
usbtouch_disconnect(struct usb_interface * intf)1824d05e84e6SDmitry Torokhov static void usbtouch_disconnect(struct usb_interface *intf)
1825d05e84e6SDmitry Torokhov {
1826d05e84e6SDmitry Torokhov 	struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
1827d05e84e6SDmitry Torokhov 
1828d05e84e6SDmitry Torokhov 	if (!usbtouch)
1829d05e84e6SDmitry Torokhov 		return;
1830d05e84e6SDmitry Torokhov 
18310a5ebc88SGreg Kroah-Hartman 	dev_dbg(&intf->dev,
1832b741ab9dSGreg Kroah-Hartman 		"%s - usbtouch is initialized, cleaning up\n", __func__);
1833b741ab9dSGreg Kroah-Hartman 
1834d05e84e6SDmitry Torokhov 	usb_set_intfdata(intf, NULL);
1835722232bcSOliver Neukum 	/* this will stop IO via close */
1836d05e84e6SDmitry Torokhov 	input_unregister_device(usbtouch->input);
1837d05e84e6SDmitry Torokhov 	usb_free_urb(usbtouch->irq);
18385197424cSOndrej Zary 	if (usbtouch->type->exit)
18395197424cSOndrej Zary 		usbtouch->type->exit(usbtouch);
1840d05e84e6SDmitry Torokhov 	usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
1841d05e84e6SDmitry Torokhov 	kfree(usbtouch);
1842d05e84e6SDmitry Torokhov }
1843d05e84e6SDmitry Torokhov 
1844d05e84e6SDmitry Torokhov MODULE_DEVICE_TABLE(usb, usbtouch_devices);
1845d05e84e6SDmitry Torokhov 
1846d05e84e6SDmitry Torokhov static struct usb_driver usbtouch_driver = {
1847d05e84e6SDmitry Torokhov 	.name		= "usbtouchscreen",
1848d05e84e6SDmitry Torokhov 	.probe		= usbtouch_probe,
1849d05e84e6SDmitry Torokhov 	.disconnect	= usbtouch_disconnect,
1850ed4299e1SOliver Neukum 	.suspend	= usbtouch_suspend,
1851ed4299e1SOliver Neukum 	.resume		= usbtouch_resume,
1852a8aef622SOliver Neukum 	.reset_resume	= usbtouch_reset_resume,
1853d05e84e6SDmitry Torokhov 	.id_table	= usbtouch_devices,
18545d9efc59SOliver Neukum 	.supports_autosuspend = 1,
1855d05e84e6SDmitry Torokhov };
1856d05e84e6SDmitry Torokhov 
185708642e7cSGreg Kroah-Hartman module_usb_driver(usbtouch_driver);
1858d05e84e6SDmitry Torokhov 
1859698c03b4SJulia Lawall MODULE_AUTHOR("Daniel Ritz <daniel.ritz@gmx.ch>");
1860698c03b4SJulia Lawall MODULE_DESCRIPTION("USB Touchscreen Driver");
1861d05e84e6SDmitry Torokhov MODULE_LICENSE("GPL");
1862d05e84e6SDmitry Torokhov 
1863d05e84e6SDmitry Torokhov MODULE_ALIAS("touchkitusb");
1864d05e84e6SDmitry Torokhov MODULE_ALIAS("itmtouch");
1865d05e84e6SDmitry Torokhov MODULE_ALIAS("mtouchusb");
1866