usbtouchscreen.c 25 KB

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