usbtouchscreen.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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 || buf[1] != 0x00) {
  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[1] != 0x00) &&
  368. (buf[0] != 0x15 || buf[1] != 0x01)) {
  369. ret = -ENODEV;
  370. goto err_out;
  371. }
  372. /* start sending data */
  373. ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
  374. TSC10_CMD_DATA1,
  375. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  376. 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  377. err_out:
  378. kfree(buf);
  379. err_nobuf:
  380. return ret;
  381. }
  382. static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  383. {
  384. dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
  385. dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
  386. dev->touch = pkt[0] & 0x01;
  387. return 1;
  388. }
  389. #endif
  390. /*****************************************************************************
  391. * IRTOUCH Part
  392. */
  393. #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
  394. static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  395. {
  396. dev->x = (pkt[3] << 8) | pkt[2];
  397. dev->y = (pkt[5] << 8) | pkt[4];
  398. dev->touch = (pkt[1] & 0x03) ? 1 : 0;
  399. return 1;
  400. }
  401. #endif
  402. /*****************************************************************************
  403. * IdealTEK URTC1000 Part
  404. */
  405. #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
  406. #ifndef MULTI_PACKET
  407. #define MULTI_PACKET
  408. #endif
  409. static int idealtek_get_pkt_len(unsigned char *buf, int len)
  410. {
  411. if (buf[0] & 0x80)
  412. return 5;
  413. if (buf[0] == 0x01)
  414. return len;
  415. return 0;
  416. }
  417. static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  418. {
  419. switch (pkt[0] & 0x98) {
  420. case 0x88:
  421. /* touch data in IdealTEK mode */
  422. dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
  423. dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
  424. dev->touch = (pkt[0] & 0x40) ? 1 : 0;
  425. return 1;
  426. case 0x98:
  427. /* touch data in MT emulation mode */
  428. dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
  429. dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
  430. dev->touch = (pkt[0] & 0x40) ? 1 : 0;
  431. return 1;
  432. default:
  433. return 0;
  434. }
  435. }
  436. #endif
  437. /*****************************************************************************
  438. * General Touch Part
  439. */
  440. #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
  441. static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  442. {
  443. dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1] ;
  444. dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3] ;
  445. dev->press = pkt[5] & 0xff;
  446. dev->touch = pkt[0] & 0x01;
  447. return 1;
  448. }
  449. #endif
  450. /*****************************************************************************
  451. * GoTop Part
  452. */
  453. #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
  454. static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  455. {
  456. dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
  457. dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
  458. dev->touch = pkt[0] & 0x01;
  459. return 1;
  460. }
  461. #endif
  462. /*****************************************************************************
  463. * the different device descriptors
  464. */
  465. #ifdef MULTI_PACKET
  466. static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
  467. unsigned char *pkt, int len);
  468. #endif
  469. static struct usbtouch_device_info usbtouch_dev_info[] = {
  470. #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
  471. [DEVTYPE_EGALAX] = {
  472. .min_xc = 0x0,
  473. .max_xc = 0x07ff,
  474. .min_yc = 0x0,
  475. .max_yc = 0x07ff,
  476. .rept_size = 16,
  477. .process_pkt = usbtouch_process_multi,
  478. .get_pkt_len = egalax_get_pkt_len,
  479. .read_data = egalax_read_data,
  480. },
  481. #endif
  482. #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
  483. [DEVTYPE_PANJIT] = {
  484. .min_xc = 0x0,
  485. .max_xc = 0x0fff,
  486. .min_yc = 0x0,
  487. .max_yc = 0x0fff,
  488. .rept_size = 8,
  489. .read_data = panjit_read_data,
  490. },
  491. #endif
  492. #ifdef CONFIG_TOUCHSCREEN_USB_3M
  493. [DEVTYPE_3M] = {
  494. .min_xc = 0x0,
  495. .max_xc = 0x4000,
  496. .min_yc = 0x0,
  497. .max_yc = 0x4000,
  498. .rept_size = 11,
  499. .read_data = mtouch_read_data,
  500. .init = mtouch_init,
  501. },
  502. #endif
  503. #ifdef CONFIG_TOUCHSCREEN_USB_ITM
  504. [DEVTYPE_ITM] = {
  505. .min_xc = 0x0,
  506. .max_xc = 0x0fff,
  507. .min_yc = 0x0,
  508. .max_yc = 0x0fff,
  509. .max_press = 0xff,
  510. .rept_size = 8,
  511. .read_data = itm_read_data,
  512. },
  513. #endif
  514. #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
  515. [DEVTYPE_ETURBO] = {
  516. .min_xc = 0x0,
  517. .max_xc = 0x07ff,
  518. .min_yc = 0x0,
  519. .max_yc = 0x07ff,
  520. .rept_size = 8,
  521. .process_pkt = usbtouch_process_multi,
  522. .get_pkt_len = eturbo_get_pkt_len,
  523. .read_data = eturbo_read_data,
  524. },
  525. #endif
  526. #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
  527. [DEVTYPE_GUNZE] = {
  528. .min_xc = 0x0,
  529. .max_xc = 0x0fff,
  530. .min_yc = 0x0,
  531. .max_yc = 0x0fff,
  532. .rept_size = 4,
  533. .read_data = gunze_read_data,
  534. },
  535. #endif
  536. #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
  537. [DEVTYPE_DMC_TSC10] = {
  538. .min_xc = 0x0,
  539. .max_xc = 0x03ff,
  540. .min_yc = 0x0,
  541. .max_yc = 0x03ff,
  542. .rept_size = 5,
  543. .init = dmc_tsc10_init,
  544. .read_data = dmc_tsc10_read_data,
  545. },
  546. #endif
  547. #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
  548. [DEVTYPE_IRTOUCH] = {
  549. .min_xc = 0x0,
  550. .max_xc = 0x0fff,
  551. .min_yc = 0x0,
  552. .max_yc = 0x0fff,
  553. .rept_size = 8,
  554. .read_data = irtouch_read_data,
  555. },
  556. #endif
  557. #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
  558. [DEVTYPE_IDEALTEK] = {
  559. .min_xc = 0x0,
  560. .max_xc = 0x0fff,
  561. .min_yc = 0x0,
  562. .max_yc = 0x0fff,
  563. .rept_size = 8,
  564. .process_pkt = usbtouch_process_multi,
  565. .get_pkt_len = idealtek_get_pkt_len,
  566. .read_data = idealtek_read_data,
  567. },
  568. #endif
  569. #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
  570. [DEVTYPE_GENERAL_TOUCH] = {
  571. .min_xc = 0x0,
  572. .max_xc = 0x0500,
  573. .min_yc = 0x0,
  574. .max_yc = 0x0500,
  575. .rept_size = 7,
  576. .read_data = general_touch_read_data,
  577. },
  578. #endif
  579. #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
  580. [DEVTYPE_GOTOP] = {
  581. .min_xc = 0x0,
  582. .max_xc = 0x03ff,
  583. .min_yc = 0x0,
  584. .max_yc = 0x03ff,
  585. .rept_size = 4,
  586. .read_data = gotop_read_data,
  587. },
  588. #endif
  589. };
  590. /*****************************************************************************
  591. * Generic Part
  592. */
  593. static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
  594. unsigned char *pkt, int len)
  595. {
  596. struct usbtouch_device_info *type = usbtouch->type;
  597. if (!type->read_data(usbtouch, pkt))
  598. return;
  599. input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
  600. if (swap_xy) {
  601. input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
  602. input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
  603. } else {
  604. input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
  605. input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
  606. }
  607. if (type->max_press)
  608. input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
  609. input_sync(usbtouch->input);
  610. }
  611. #ifdef MULTI_PACKET
  612. static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
  613. unsigned char *pkt, int len)
  614. {
  615. unsigned char *buffer;
  616. int pkt_len, pos, buf_len, tmp;
  617. /* process buffer */
  618. if (unlikely(usbtouch->buf_len)) {
  619. /* try to get size */
  620. pkt_len = usbtouch->type->get_pkt_len(
  621. usbtouch->buffer, usbtouch->buf_len);
  622. /* drop? */
  623. if (unlikely(!pkt_len))
  624. goto out_flush_buf;
  625. /* need to append -pkt_len bytes before able to get size */
  626. if (unlikely(pkt_len < 0)) {
  627. int append = -pkt_len;
  628. if (unlikely(append > len))
  629. append = len;
  630. if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
  631. goto out_flush_buf;
  632. memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
  633. usbtouch->buf_len += append;
  634. pkt_len = usbtouch->type->get_pkt_len(
  635. usbtouch->buffer, usbtouch->buf_len);
  636. if (pkt_len < 0)
  637. return;
  638. }
  639. /* append */
  640. tmp = pkt_len - usbtouch->buf_len;
  641. if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
  642. goto out_flush_buf;
  643. memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
  644. usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
  645. buffer = pkt + tmp;
  646. buf_len = len - tmp;
  647. } else {
  648. buffer = pkt;
  649. buf_len = len;
  650. }
  651. /* loop over the received packet, process */
  652. pos = 0;
  653. while (pos < buf_len) {
  654. /* get packet len */
  655. pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
  656. buf_len - pos);
  657. /* unknown packet: skip one byte */
  658. if (unlikely(!pkt_len)) {
  659. pos++;
  660. continue;
  661. }
  662. /* full packet: process */
  663. if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
  664. usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
  665. } else {
  666. /* incomplete packet: save in buffer */
  667. memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
  668. usbtouch->buf_len = buf_len - pos;
  669. return;
  670. }
  671. pos += pkt_len;
  672. }
  673. out_flush_buf:
  674. usbtouch->buf_len = 0;
  675. return;
  676. }
  677. #endif
  678. static void usbtouch_irq(struct urb *urb)
  679. {
  680. struct usbtouch_usb *usbtouch = urb->context;
  681. int retval;
  682. switch (urb->status) {
  683. case 0:
  684. /* success */
  685. break;
  686. case -ETIME:
  687. /* this urb is timing out */
  688. dbg("%s - urb timed out - was the device unplugged?",
  689. __func__);
  690. return;
  691. case -ECONNRESET:
  692. case -ENOENT:
  693. case -ESHUTDOWN:
  694. /* this urb is terminated, clean up */
  695. dbg("%s - urb shutting down with status: %d",
  696. __func__, urb->status);
  697. return;
  698. default:
  699. dbg("%s - nonzero urb status received: %d",
  700. __func__, urb->status);
  701. goto exit;
  702. }
  703. usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
  704. exit:
  705. retval = usb_submit_urb(urb, GFP_ATOMIC);
  706. if (retval)
  707. err("%s - usb_submit_urb failed with result: %d",
  708. __func__, retval);
  709. }
  710. static int usbtouch_open(struct input_dev *input)
  711. {
  712. struct usbtouch_usb *usbtouch = input_get_drvdata(input);
  713. usbtouch->irq->dev = usbtouch->udev;
  714. if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
  715. return -EIO;
  716. return 0;
  717. }
  718. static void usbtouch_close(struct input_dev *input)
  719. {
  720. struct usbtouch_usb *usbtouch = input_get_drvdata(input);
  721. usb_kill_urb(usbtouch->irq);
  722. }
  723. static void usbtouch_free_buffers(struct usb_device *udev,
  724. struct usbtouch_usb *usbtouch)
  725. {
  726. usb_buffer_free(udev, usbtouch->type->rept_size,
  727. usbtouch->data, usbtouch->data_dma);
  728. kfree(usbtouch->buffer);
  729. }
  730. static int usbtouch_probe(struct usb_interface *intf,
  731. const struct usb_device_id *id)
  732. {
  733. struct usbtouch_usb *usbtouch;
  734. struct input_dev *input_dev;
  735. struct usb_host_interface *interface;
  736. struct usb_endpoint_descriptor *endpoint;
  737. struct usb_device *udev = interface_to_usbdev(intf);
  738. struct usbtouch_device_info *type;
  739. int err = -ENOMEM;
  740. /* some devices are ignored */
  741. if (id->driver_info == DEVTYPE_IGNORE)
  742. return -ENODEV;
  743. interface = intf->cur_altsetting;
  744. endpoint = &interface->endpoint[0].desc;
  745. usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
  746. input_dev = input_allocate_device();
  747. if (!usbtouch || !input_dev)
  748. goto out_free;
  749. type = &usbtouch_dev_info[id->driver_info];
  750. usbtouch->type = type;
  751. if (!type->process_pkt)
  752. type->process_pkt = usbtouch_process_pkt;
  753. usbtouch->data = usb_buffer_alloc(udev, type->rept_size,
  754. GFP_KERNEL, &usbtouch->data_dma);
  755. if (!usbtouch->data)
  756. goto out_free;
  757. if (type->get_pkt_len) {
  758. usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
  759. if (!usbtouch->buffer)
  760. goto out_free_buffers;
  761. }
  762. usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
  763. if (!usbtouch->irq) {
  764. dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
  765. goto out_free_buffers;
  766. }
  767. usbtouch->udev = udev;
  768. usbtouch->input = input_dev;
  769. if (udev->manufacturer)
  770. strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
  771. if (udev->product) {
  772. if (udev->manufacturer)
  773. strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
  774. strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
  775. }
  776. if (!strlen(usbtouch->name))
  777. snprintf(usbtouch->name, sizeof(usbtouch->name),
  778. "USB Touchscreen %04x:%04x",
  779. le16_to_cpu(udev->descriptor.idVendor),
  780. le16_to_cpu(udev->descriptor.idProduct));
  781. usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
  782. strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
  783. input_dev->name = usbtouch->name;
  784. input_dev->phys = usbtouch->phys;
  785. usb_to_input_id(udev, &input_dev->id);
  786. input_dev->dev.parent = &intf->dev;
  787. input_set_drvdata(input_dev, usbtouch);
  788. input_dev->open = usbtouch_open;
  789. input_dev->close = usbtouch_close;
  790. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  791. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  792. input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
  793. input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
  794. if (type->max_press)
  795. input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
  796. type->max_press, 0, 0);
  797. usb_fill_int_urb(usbtouch->irq, usbtouch->udev,
  798. usb_rcvintpipe(usbtouch->udev, endpoint->bEndpointAddress),
  799. usbtouch->data, type->rept_size,
  800. usbtouch_irq, usbtouch, endpoint->bInterval);
  801. usbtouch->irq->dev = usbtouch->udev;
  802. usbtouch->irq->transfer_dma = usbtouch->data_dma;
  803. usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  804. /* device specific init */
  805. if (type->init) {
  806. err = type->init(usbtouch);
  807. if (err) {
  808. dbg("%s - type->init() failed, err: %d", __func__, err);
  809. goto out_free_buffers;
  810. }
  811. }
  812. err = input_register_device(usbtouch->input);
  813. if (err) {
  814. dbg("%s - input_register_device failed, err: %d", __func__, err);
  815. goto out_free_buffers;
  816. }
  817. usb_set_intfdata(intf, usbtouch);
  818. return 0;
  819. out_free_buffers:
  820. usbtouch_free_buffers(udev, usbtouch);
  821. out_free:
  822. input_free_device(input_dev);
  823. kfree(usbtouch);
  824. return err;
  825. }
  826. static void usbtouch_disconnect(struct usb_interface *intf)
  827. {
  828. struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
  829. dbg("%s - called", __func__);
  830. if (!usbtouch)
  831. return;
  832. dbg("%s - usbtouch is initialized, cleaning up", __func__);
  833. usb_set_intfdata(intf, NULL);
  834. usb_kill_urb(usbtouch->irq);
  835. input_unregister_device(usbtouch->input);
  836. usb_free_urb(usbtouch->irq);
  837. usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
  838. kfree(usbtouch);
  839. }
  840. MODULE_DEVICE_TABLE(usb, usbtouch_devices);
  841. static struct usb_driver usbtouch_driver = {
  842. .name = "usbtouchscreen",
  843. .probe = usbtouch_probe,
  844. .disconnect = usbtouch_disconnect,
  845. .id_table = usbtouch_devices,
  846. };
  847. static int __init usbtouch_init(void)
  848. {
  849. return usb_register(&usbtouch_driver);
  850. }
  851. static void __exit usbtouch_cleanup(void)
  852. {
  853. usb_deregister(&usbtouch_driver);
  854. }
  855. module_init(usbtouch_init);
  856. module_exit(usbtouch_cleanup);
  857. MODULE_AUTHOR(DRIVER_AUTHOR);
  858. MODULE_DESCRIPTION(DRIVER_DESC);
  859. MODULE_LICENSE("GPL");
  860. MODULE_ALIAS("touchkitusb");
  861. MODULE_ALIAS("itmtouch");
  862. MODULE_ALIAS("mtouchusb");