usbtouchscreen.c 25 KB

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