usbtouchscreen.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. * - JASTEC USB touch controller/DigiTech DTR-02U
  17. * - Zytronic capacitive touchscreen
  18. * - NEXIO/iNexio
  19. *
  20. * Copyright (C) 2004-2007 by Daniel Ritz <daniel.ritz@gmx.ch>
  21. * Copyright (C) by Todd E. Johnson (mtouchusb.c)
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License as
  25. * published by the Free Software Foundation; either version 2 of the
  26. * License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful, but
  29. * WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. *
  37. * Driver is based on touchkitusb.c
  38. * - ITM parts are from itmtouch.c
  39. * - 3M parts are from mtouchusb.c
  40. * - PanJit parts are from an unmerged driver by Lanslott Gish
  41. * - DMC TSC 10/25 are from Holger Schurig, with ideas from an unmerged
  42. * driver from Marius Vollmer
  43. *
  44. *****************************************************************************/
  45. //#define DEBUG
  46. #include <linux/kernel.h>
  47. #include <linux/slab.h>
  48. #include <linux/input.h>
  49. #include <linux/module.h>
  50. #include <linux/init.h>
  51. #include <linux/usb.h>
  52. #include <linux/usb/input.h>
  53. #include <linux/hid.h>
  54. #define DRIVER_VERSION "v0.6"
  55. #define DRIVER_AUTHOR "Daniel Ritz <daniel.ritz@gmx.ch>"
  56. #define DRIVER_DESC "USB Touchscreen Driver"
  57. static int swap_xy;
  58. module_param(swap_xy, bool, 0644);
  59. MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
  60. static int hwcalib_xy;
  61. module_param(hwcalib_xy, bool, 0644);
  62. MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
  63. /* device specifc data/functions */
  64. struct usbtouch_usb;
  65. struct usbtouch_device_info {
  66. int min_xc, max_xc;
  67. int min_yc, max_yc;
  68. int min_press, max_press;
  69. int rept_size;
  70. /*
  71. * Always service the USB devices irq not just when the input device is
  72. * open. This is useful when devices have a watchdog which prevents us
  73. * from periodically polling the device. Leave this unset unless your
  74. * touchscreen device requires it, as it does consume more of the USB
  75. * bandwidth.
  76. */
  77. bool irq_always;
  78. void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
  79. /*
  80. * used to get the packet len. possible return values:
  81. * > 0: packet len
  82. * = 0: skip one byte
  83. * < 0: -return value more bytes needed
  84. */
  85. int (*get_pkt_len) (unsigned char *pkt, int len);
  86. int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
  87. int (*init) (struct usbtouch_usb *usbtouch);
  88. void (*exit) (struct usbtouch_usb *usbtouch);
  89. };
  90. /* a usbtouch device */
  91. struct usbtouch_usb {
  92. unsigned char *data;
  93. dma_addr_t data_dma;
  94. unsigned char *buffer;
  95. int buf_len;
  96. struct urb *irq;
  97. struct usb_interface *interface;
  98. struct input_dev *input;
  99. struct usbtouch_device_info *type;
  100. char name[128];
  101. char phys[64];
  102. void *priv;
  103. int x, y;
  104. int touch, press;
  105. };
  106. /* device types */
  107. enum {
  108. DEVTYPE_IGNORE = -1,
  109. DEVTYPE_EGALAX,
  110. DEVTYPE_PANJIT,
  111. DEVTYPE_3M,
  112. DEVTYPE_ITM,
  113. DEVTYPE_ETURBO,
  114. DEVTYPE_GUNZE,
  115. DEVTYPE_DMC_TSC10,
  116. DEVTYPE_IRTOUCH,
  117. DEVTYPE_IDEALTEK,
  118. DEVTYPE_GENERAL_TOUCH,
  119. DEVTYPE_GOTOP,
  120. DEVTYPE_JASTEC,
  121. DEVTYPE_E2I,
  122. DEVTYPE_ZYTRONIC,
  123. DEVTYPE_TC5UH,
  124. DEVTYPE_NEXIO,
  125. };
  126. #define USB_DEVICE_HID_CLASS(vend, prod) \
  127. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS \
  128. | USB_DEVICE_ID_MATCH_INT_PROTOCOL \
  129. | USB_DEVICE_ID_MATCH_DEVICE, \
  130. .idVendor = (vend), \
  131. .idProduct = (prod), \
  132. .bInterfaceClass = USB_INTERFACE_CLASS_HID, \
  133. .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_MOUSE
  134. static const struct usb_device_id usbtouch_devices[] = {
  135. #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
  136. /* ignore the HID capable devices, handled by usbhid */
  137. {USB_DEVICE_HID_CLASS(0x0eef, 0x0001), .driver_info = DEVTYPE_IGNORE},
  138. {USB_DEVICE_HID_CLASS(0x0eef, 0x0002), .driver_info = DEVTYPE_IGNORE},
  139. /* normal device IDs */
  140. {USB_DEVICE(0x3823, 0x0001), .driver_info = DEVTYPE_EGALAX},
  141. {USB_DEVICE(0x3823, 0x0002), .driver_info = DEVTYPE_EGALAX},
  142. {USB_DEVICE(0x0123, 0x0001), .driver_info = DEVTYPE_EGALAX},
  143. {USB_DEVICE(0x0eef, 0x0001), .driver_info = DEVTYPE_EGALAX},
  144. {USB_DEVICE(0x0eef, 0x0002), .driver_info = DEVTYPE_EGALAX},
  145. {USB_DEVICE(0x1234, 0x0001), .driver_info = DEVTYPE_EGALAX},
  146. {USB_DEVICE(0x1234, 0x0002), .driver_info = DEVTYPE_EGALAX},
  147. #endif
  148. #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
  149. {USB_DEVICE(0x134c, 0x0001), .driver_info = DEVTYPE_PANJIT},
  150. {USB_DEVICE(0x134c, 0x0002), .driver_info = DEVTYPE_PANJIT},
  151. {USB_DEVICE(0x134c, 0x0003), .driver_info = DEVTYPE_PANJIT},
  152. {USB_DEVICE(0x134c, 0x0004), .driver_info = DEVTYPE_PANJIT},
  153. #endif
  154. #ifdef CONFIG_TOUCHSCREEN_USB_3M
  155. {USB_DEVICE(0x0596, 0x0001), .driver_info = DEVTYPE_3M},
  156. #endif
  157. #ifdef CONFIG_TOUCHSCREEN_USB_ITM
  158. {USB_DEVICE(0x0403, 0xf9e9), .driver_info = DEVTYPE_ITM},
  159. #endif
  160. #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
  161. {USB_DEVICE(0x1234, 0x5678), .driver_info = DEVTYPE_ETURBO},
  162. #endif
  163. #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
  164. {USB_DEVICE(0x0637, 0x0001), .driver_info = DEVTYPE_GUNZE},
  165. #endif
  166. #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
  167. {USB_DEVICE(0x0afa, 0x03e8), .driver_info = DEVTYPE_DMC_TSC10},
  168. #endif
  169. #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
  170. {USB_DEVICE(0x595a, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
  171. {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
  172. #endif
  173. #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
  174. {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
  175. #endif
  176. #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
  177. {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
  178. #endif
  179. #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
  180. {USB_DEVICE(0x08f2, 0x007f), .driver_info = DEVTYPE_GOTOP},
  181. {USB_DEVICE(0x08f2, 0x00ce), .driver_info = DEVTYPE_GOTOP},
  182. {USB_DEVICE(0x08f2, 0x00f4), .driver_info = DEVTYPE_GOTOP},
  183. #endif
  184. #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
  185. {USB_DEVICE(0x0f92, 0x0001), .driver_info = DEVTYPE_JASTEC},
  186. #endif
  187. #ifdef CONFIG_TOUCHSCREEN_USB_E2I
  188. {USB_DEVICE(0x1ac7, 0x0001), .driver_info = DEVTYPE_E2I},
  189. #endif
  190. #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
  191. {USB_DEVICE(0x14c8, 0x0003), .driver_info = DEVTYPE_ZYTRONIC},
  192. #endif
  193. #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC5UH
  194. {USB_DEVICE(0x0664, 0x0309), .driver_info = DEVTYPE_TC5UH},
  195. #endif
  196. #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
  197. /* data interface only */
  198. {USB_DEVICE_AND_INTERFACE_INFO(0x10f0, 0x2002, 0x0a, 0x00, 0x00),
  199. .driver_info = DEVTYPE_NEXIO},
  200. {USB_DEVICE_AND_INTERFACE_INFO(0x1870, 0x0001, 0x0a, 0x00, 0x00),
  201. .driver_info = DEVTYPE_NEXIO},
  202. #endif
  203. {}
  204. };
  205. /*****************************************************************************
  206. * e2i Part
  207. */
  208. #ifdef CONFIG_TOUCHSCREEN_USB_E2I
  209. static int e2i_init(struct usbtouch_usb *usbtouch)
  210. {
  211. int ret;
  212. struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
  213. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  214. 0x01, 0x02, 0x0000, 0x0081,
  215. NULL, 0, USB_CTRL_SET_TIMEOUT);
  216. dbg("%s - usb_control_msg - E2I_RESET - bytes|err: %d",
  217. __func__, ret);
  218. return ret;
  219. }
  220. static int e2i_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  221. {
  222. int tmp = (pkt[0] << 8) | pkt[1];
  223. dev->x = (pkt[2] << 8) | pkt[3];
  224. dev->y = (pkt[4] << 8) | pkt[5];
  225. tmp = tmp - 0xA000;
  226. dev->touch = (tmp > 0);
  227. dev->press = (tmp > 0 ? tmp : 0);
  228. return 1;
  229. }
  230. #endif
  231. /*****************************************************************************
  232. * eGalax part
  233. */
  234. #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
  235. #ifndef MULTI_PACKET
  236. #define MULTI_PACKET
  237. #endif
  238. #define EGALAX_PKT_TYPE_MASK 0xFE
  239. #define EGALAX_PKT_TYPE_REPT 0x80
  240. #define EGALAX_PKT_TYPE_DIAG 0x0A
  241. static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  242. {
  243. if ((pkt[0] & EGALAX_PKT_TYPE_MASK) != EGALAX_PKT_TYPE_REPT)
  244. return 0;
  245. dev->x = ((pkt[3] & 0x0F) << 7) | (pkt[4] & 0x7F);
  246. dev->y = ((pkt[1] & 0x0F) << 7) | (pkt[2] & 0x7F);
  247. dev->touch = pkt[0] & 0x01;
  248. return 1;
  249. }
  250. static int egalax_get_pkt_len(unsigned char *buf, int len)
  251. {
  252. switch (buf[0] & EGALAX_PKT_TYPE_MASK) {
  253. case EGALAX_PKT_TYPE_REPT:
  254. return 5;
  255. case EGALAX_PKT_TYPE_DIAG:
  256. if (len < 2)
  257. return -1;
  258. return buf[1] + 2;
  259. }
  260. return 0;
  261. }
  262. #endif
  263. /*****************************************************************************
  264. * PanJit Part
  265. */
  266. #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
  267. static int panjit_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  268. {
  269. dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
  270. dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
  271. dev->touch = pkt[0] & 0x01;
  272. return 1;
  273. }
  274. #endif
  275. /*****************************************************************************
  276. * 3M/Microtouch Part
  277. */
  278. #ifdef CONFIG_TOUCHSCREEN_USB_3M
  279. #define MTOUCHUSB_ASYNC_REPORT 1
  280. #define MTOUCHUSB_RESET 7
  281. #define MTOUCHUSB_REQ_CTRLLR_ID 10
  282. static int mtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  283. {
  284. if (hwcalib_xy) {
  285. dev->x = (pkt[4] << 8) | pkt[3];
  286. dev->y = 0xffff - ((pkt[6] << 8) | pkt[5]);
  287. } else {
  288. dev->x = (pkt[8] << 8) | pkt[7];
  289. dev->y = (pkt[10] << 8) | pkt[9];
  290. }
  291. dev->touch = (pkt[2] & 0x40) ? 1 : 0;
  292. return 1;
  293. }
  294. static int mtouch_init(struct usbtouch_usb *usbtouch)
  295. {
  296. int ret, i;
  297. struct usb_device *udev = interface_to_usbdev(usbtouch->interface);
  298. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  299. MTOUCHUSB_RESET,
  300. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  301. 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  302. dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
  303. __func__, ret);
  304. if (ret < 0)
  305. return ret;
  306. msleep(150);
  307. for (i = 0; i < 3; i++) {
  308. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  309. MTOUCHUSB_ASYNC_REPORT,
  310. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  311. 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
  312. dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
  313. __func__, ret);
  314. if (ret >= 0)
  315. break;
  316. if (ret != -EPIPE)
  317. return ret;
  318. }
  319. /* Default min/max xy are the raw values, override if using hw-calib */
  320. if (hwcalib_xy) {
  321. input_set_abs_params(usbtouch->input, ABS_X, 0, 0xffff, 0, 0);
  322. input_set_abs_params(usbtouch->input, ABS_Y, 0, 0xffff, 0, 0);
  323. }
  324. return 0;
  325. }
  326. #endif
  327. /*****************************************************************************
  328. * ITM Part
  329. */
  330. #ifdef CONFIG_TOUCHSCREEN_USB_ITM
  331. static int itm_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  332. {
  333. int touch;
  334. /*
  335. * ITM devices report invalid x/y data if not touched.
  336. * if the screen was touched before but is not touched any more
  337. * report touch as 0 with the last valid x/y data once. then stop
  338. * reporting data until touched again.
  339. */
  340. dev->press = ((pkt[2] & 0x01) << 7) | (pkt[5] & 0x7F);
  341. touch = ~pkt[7] & 0x20;
  342. if (!touch) {
  343. if (dev->touch) {
  344. dev->touch = 0;
  345. return 1;
  346. }
  347. return 0;
  348. }
  349. dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[3] & 0x7F);
  350. dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[4] & 0x7F);
  351. dev->touch = touch;
  352. return 1;
  353. }
  354. #endif
  355. /*****************************************************************************
  356. * eTurboTouch part
  357. */
  358. #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
  359. #ifndef MULTI_PACKET
  360. #define MULTI_PACKET
  361. #endif
  362. static int eturbo_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  363. {
  364. unsigned int shift;
  365. /* packets should start with sync */
  366. if (!(pkt[0] & 0x80))
  367. return 0;
  368. shift = (6 - (pkt[0] & 0x03));
  369. dev->x = ((pkt[3] << 7) | pkt[4]) >> shift;
  370. dev->y = ((pkt[1] << 7) | pkt[2]) >> shift;
  371. dev->touch = (pkt[0] & 0x10) ? 1 : 0;
  372. return 1;
  373. }
  374. static int eturbo_get_pkt_len(unsigned char *buf, int len)
  375. {
  376. if (buf[0] & 0x80)
  377. return 5;
  378. if (buf[0] == 0x01)
  379. return 3;
  380. return 0;
  381. }
  382. #endif
  383. /*****************************************************************************
  384. * Gunze part
  385. */
  386. #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
  387. static int gunze_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  388. {
  389. if (!(pkt[0] & 0x80) || ((pkt[1] | pkt[2] | pkt[3]) & 0x80))
  390. return 0;
  391. dev->x = ((pkt[0] & 0x1F) << 7) | (pkt[2] & 0x7F);
  392. dev->y = ((pkt[1] & 0x1F) << 7) | (pkt[3] & 0x7F);
  393. dev->touch = pkt[0] & 0x20;
  394. return 1;
  395. }
  396. #endif
  397. /*****************************************************************************
  398. * DMC TSC-10/25 Part
  399. *
  400. * Documentation about the controller and it's protocol can be found at
  401. * http://www.dmccoltd.com/files/controler/tsc10usb_pi_e.pdf
  402. * http://www.dmccoltd.com/files/controler/tsc25_usb_e.pdf
  403. */
  404. #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
  405. /* supported data rates. currently using 130 */
  406. #define TSC10_RATE_POINT 0x50
  407. #define TSC10_RATE_30 0x40
  408. #define TSC10_RATE_50 0x41
  409. #define TSC10_RATE_80 0x42
  410. #define TSC10_RATE_100 0x43
  411. #define TSC10_RATE_130 0x44
  412. #define TSC10_RATE_150 0x45
  413. /* commands */
  414. #define TSC10_CMD_RESET 0x55
  415. #define TSC10_CMD_RATE 0x05
  416. #define TSC10_CMD_DATA1 0x01
  417. static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
  418. {
  419. struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
  420. int ret = -ENOMEM;
  421. unsigned char *buf;
  422. buf = kmalloc(2, GFP_KERNEL);
  423. if (!buf)
  424. goto err_nobuf;
  425. /* reset */
  426. buf[0] = buf[1] = 0xFF;
  427. ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
  428. TSC10_CMD_RESET,
  429. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  430. 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
  431. if (ret < 0)
  432. goto err_out;
  433. if (buf[0] != 0x06) {
  434. ret = -ENODEV;
  435. goto err_out;
  436. }
  437. /* set coordinate output rate */
  438. buf[0] = buf[1] = 0xFF;
  439. ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
  440. TSC10_CMD_RATE,
  441. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  442. TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
  443. if (ret < 0)
  444. goto err_out;
  445. if ((buf[0] != 0x06) && (buf[0] != 0x15 || buf[1] != 0x01)) {
  446. ret = -ENODEV;
  447. goto err_out;
  448. }
  449. /* start sending data */
  450. ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0),
  451. TSC10_CMD_DATA1,
  452. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  453. 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  454. err_out:
  455. kfree(buf);
  456. err_nobuf:
  457. return ret;
  458. }
  459. static int dmc_tsc10_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  460. {
  461. dev->x = ((pkt[2] & 0x03) << 8) | pkt[1];
  462. dev->y = ((pkt[4] & 0x03) << 8) | pkt[3];
  463. dev->touch = pkt[0] & 0x01;
  464. return 1;
  465. }
  466. #endif
  467. /*****************************************************************************
  468. * IRTOUCH Part
  469. */
  470. #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
  471. static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  472. {
  473. dev->x = (pkt[3] << 8) | pkt[2];
  474. dev->y = (pkt[5] << 8) | pkt[4];
  475. dev->touch = (pkt[1] & 0x03) ? 1 : 0;
  476. return 1;
  477. }
  478. #endif
  479. /*****************************************************************************
  480. * ET&T TC5UH part
  481. */
  482. #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC5UH
  483. static int tc5uh_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  484. {
  485. dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1];
  486. dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3];
  487. dev->touch = pkt[0] & 0x01;
  488. return 1;
  489. }
  490. #endif
  491. /*****************************************************************************
  492. * IdealTEK URTC1000 Part
  493. */
  494. #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
  495. #ifndef MULTI_PACKET
  496. #define MULTI_PACKET
  497. #endif
  498. static int idealtek_get_pkt_len(unsigned char *buf, int len)
  499. {
  500. if (buf[0] & 0x80)
  501. return 5;
  502. if (buf[0] == 0x01)
  503. return len;
  504. return 0;
  505. }
  506. static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  507. {
  508. switch (pkt[0] & 0x98) {
  509. case 0x88:
  510. /* touch data in IdealTEK mode */
  511. dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
  512. dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
  513. dev->touch = (pkt[0] & 0x40) ? 1 : 0;
  514. return 1;
  515. case 0x98:
  516. /* touch data in MT emulation mode */
  517. dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
  518. dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
  519. dev->touch = (pkt[0] & 0x40) ? 1 : 0;
  520. return 1;
  521. default:
  522. return 0;
  523. }
  524. }
  525. #endif
  526. /*****************************************************************************
  527. * General Touch Part
  528. */
  529. #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
  530. static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  531. {
  532. dev->x = (pkt[2] << 8) | pkt[1];
  533. dev->y = (pkt[4] << 8) | pkt[3];
  534. dev->press = pkt[5] & 0xff;
  535. dev->touch = pkt[0] & 0x01;
  536. return 1;
  537. }
  538. #endif
  539. /*****************************************************************************
  540. * GoTop Part
  541. */
  542. #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
  543. static int gotop_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  544. {
  545. dev->x = ((pkt[1] & 0x38) << 4) | pkt[2];
  546. dev->y = ((pkt[1] & 0x07) << 7) | pkt[3];
  547. dev->touch = pkt[0] & 0x01;
  548. return 1;
  549. }
  550. #endif
  551. /*****************************************************************************
  552. * JASTEC Part
  553. */
  554. #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
  555. static int jastec_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  556. {
  557. dev->x = ((pkt[0] & 0x3f) << 6) | (pkt[2] & 0x3f);
  558. dev->y = ((pkt[1] & 0x3f) << 6) | (pkt[3] & 0x3f);
  559. dev->touch = (pkt[0] & 0x40) >> 6;
  560. return 1;
  561. }
  562. #endif
  563. /*****************************************************************************
  564. * Zytronic Part
  565. */
  566. #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
  567. static int zytronic_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
  568. {
  569. switch (pkt[0]) {
  570. case 0x3A: /* command response */
  571. dbg("%s: Command response %d", __func__, pkt[1]);
  572. break;
  573. case 0xC0: /* down */
  574. dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
  575. dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
  576. dev->touch = 1;
  577. dbg("%s: down %d,%d", __func__, dev->x, dev->y);
  578. return 1;
  579. case 0x80: /* up */
  580. dev->x = (pkt[1] & 0x7f) | ((pkt[2] & 0x07) << 7);
  581. dev->y = (pkt[3] & 0x7f) | ((pkt[4] & 0x07) << 7);
  582. dev->touch = 0;
  583. dbg("%s: up %d,%d", __func__, dev->x, dev->y);
  584. return 1;
  585. default:
  586. dbg("%s: Unknown return %d", __func__, pkt[0]);
  587. break;
  588. }
  589. return 0;
  590. }
  591. #endif
  592. /*****************************************************************************
  593. * NEXIO Part
  594. */
  595. #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
  596. #define NEXIO_TIMEOUT 5000
  597. #define NEXIO_BUFSIZE 1024
  598. #define NEXIO_THRESHOLD 50
  599. struct nexio_priv {
  600. struct urb *ack;
  601. unsigned char *ack_buf;
  602. };
  603. struct nexio_touch_packet {
  604. u8 flags; /* 0xe1 = touch, 0xe1 = release */
  605. __be16 data_len; /* total bytes of touch data */
  606. __be16 x_len; /* bytes for X axis */
  607. __be16 y_len; /* bytes for Y axis */
  608. u8 data[];
  609. } __attribute__ ((packed));
  610. static unsigned char nexio_ack_pkt[2] = { 0xaa, 0x02 };
  611. static unsigned char nexio_init_pkt[4] = { 0x82, 0x04, 0x0a, 0x0f };
  612. static void nexio_ack_complete(struct urb *urb)
  613. {
  614. }
  615. static int nexio_init(struct usbtouch_usb *usbtouch)
  616. {
  617. struct usb_device *dev = interface_to_usbdev(usbtouch->interface);
  618. struct usb_host_interface *interface = usbtouch->interface->cur_altsetting;
  619. struct nexio_priv *priv;
  620. int ret = -ENOMEM;
  621. int actual_len, i;
  622. unsigned char *buf;
  623. char *firmware_ver = NULL, *device_name = NULL;
  624. int input_ep = 0, output_ep = 0;
  625. /* find first input and output endpoint */
  626. for (i = 0; i < interface->desc.bNumEndpoints; i++) {
  627. if (!input_ep &&
  628. usb_endpoint_dir_in(&interface->endpoint[i].desc))
  629. input_ep = interface->endpoint[i].desc.bEndpointAddress;
  630. if (!output_ep &&
  631. usb_endpoint_dir_out(&interface->endpoint[i].desc))
  632. output_ep = interface->endpoint[i].desc.bEndpointAddress;
  633. }
  634. if (!input_ep || !output_ep)
  635. return -ENXIO;
  636. buf = kmalloc(NEXIO_BUFSIZE, GFP_KERNEL);
  637. if (!buf)
  638. goto out_buf;
  639. /* two empty reads */
  640. for (i = 0; i < 2; i++) {
  641. ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
  642. buf, NEXIO_BUFSIZE, &actual_len,
  643. NEXIO_TIMEOUT);
  644. if (ret < 0)
  645. goto out_buf;
  646. }
  647. /* send init command */
  648. memcpy(buf, nexio_init_pkt, sizeof(nexio_init_pkt));
  649. ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, output_ep),
  650. buf, sizeof(nexio_init_pkt), &actual_len,
  651. NEXIO_TIMEOUT);
  652. if (ret < 0)
  653. goto out_buf;
  654. /* read replies */
  655. for (i = 0; i < 3; i++) {
  656. memset(buf, 0, NEXIO_BUFSIZE);
  657. ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, input_ep),
  658. buf, NEXIO_BUFSIZE, &actual_len,
  659. NEXIO_TIMEOUT);
  660. if (ret < 0 || actual_len < 1 || buf[1] != actual_len)
  661. continue;
  662. switch (buf[0]) {
  663. case 0x83: /* firmware version */
  664. if (!firmware_ver)
  665. firmware_ver = kstrdup(&buf[2], GFP_KERNEL);
  666. break;
  667. case 0x84: /* device name */
  668. if (!device_name)
  669. device_name = kstrdup(&buf[2], GFP_KERNEL);
  670. break;
  671. }
  672. }
  673. printk(KERN_INFO "Nexio device: %s, firmware version: %s\n",
  674. device_name, firmware_ver);
  675. kfree(firmware_ver);
  676. kfree(device_name);
  677. /* prepare ACK URB */
  678. ret = -ENOMEM;
  679. usbtouch->priv = kmalloc(sizeof(struct nexio_priv), GFP_KERNEL);
  680. if (!usbtouch->priv)
  681. goto out_buf;
  682. priv = usbtouch->priv;
  683. priv->ack_buf = kmemdup(nexio_ack_pkt, sizeof(nexio_ack_pkt),
  684. GFP_KERNEL);
  685. if (!priv->ack_buf)
  686. goto err_priv;
  687. priv->ack = usb_alloc_urb(0, GFP_KERNEL);
  688. if (!priv->ack) {
  689. dbg("%s - usb_alloc_urb failed: usbtouch->ack", __func__);
  690. goto err_ack_buf;
  691. }
  692. usb_fill_bulk_urb(priv->ack, dev, usb_sndbulkpipe(dev, output_ep),
  693. priv->ack_buf, sizeof(nexio_ack_pkt),
  694. nexio_ack_complete, usbtouch);
  695. ret = 0;
  696. goto out_buf;
  697. err_ack_buf:
  698. kfree(priv->ack_buf);
  699. err_priv:
  700. kfree(priv);
  701. out_buf:
  702. kfree(buf);
  703. return ret;
  704. }
  705. static void nexio_exit(struct usbtouch_usb *usbtouch)
  706. {
  707. struct nexio_priv *priv = usbtouch->priv;
  708. usb_kill_urb(priv->ack);
  709. usb_free_urb(priv->ack);
  710. kfree(priv->ack_buf);
  711. kfree(priv);
  712. }
  713. static int nexio_read_data(struct usbtouch_usb *usbtouch, unsigned char *pkt)
  714. {
  715. int x, y, begin_x, begin_y, end_x, end_y, w, h, ret;
  716. struct nexio_touch_packet *packet = (void *) pkt;
  717. struct nexio_priv *priv = usbtouch->priv;
  718. /* got touch data? */
  719. if ((pkt[0] & 0xe0) != 0xe0)
  720. return 0;
  721. /* send ACK */
  722. ret = usb_submit_urb(priv->ack, GFP_ATOMIC);
  723. if (!usbtouch->type->max_xc) {
  724. usbtouch->type->max_xc = 2 * be16_to_cpu(packet->x_len);
  725. input_set_abs_params(usbtouch->input, ABS_X, 0,
  726. 2 * be16_to_cpu(packet->x_len), 0, 0);
  727. usbtouch->type->max_yc = 2 * be16_to_cpu(packet->y_len);
  728. input_set_abs_params(usbtouch->input, ABS_Y, 0,
  729. 2 * be16_to_cpu(packet->y_len), 0, 0);
  730. }
  731. /*
  732. * The device reports state of IR sensors on X and Y axes.
  733. * Each byte represents "darkness" percentage (0-100) of one element.
  734. * 17" touchscreen reports only 64 x 52 bytes so the resolution is low.
  735. * This also means that there's a limited multi-touch capability but
  736. * it's disabled (and untested) here as there's no X driver for that.
  737. */
  738. begin_x = end_x = begin_y = end_y = -1;
  739. for (x = 0; x < be16_to_cpu(packet->x_len); x++) {
  740. if (begin_x == -1 && packet->data[x] > NEXIO_THRESHOLD) {
  741. begin_x = x;
  742. continue;
  743. }
  744. if (end_x == -1 && begin_x != -1 && packet->data[x] < NEXIO_THRESHOLD) {
  745. end_x = x - 1;
  746. for (y = be16_to_cpu(packet->x_len);
  747. y < be16_to_cpu(packet->data_len); y++) {
  748. if (begin_y == -1 && packet->data[y] > NEXIO_THRESHOLD) {
  749. begin_y = y - be16_to_cpu(packet->x_len);
  750. continue;
  751. }
  752. if (end_y == -1 &&
  753. begin_y != -1 && packet->data[y] < NEXIO_THRESHOLD) {
  754. end_y = y - 1 - be16_to_cpu(packet->x_len);
  755. w = end_x - begin_x;
  756. h = end_y - begin_y;
  757. #if 0
  758. /* multi-touch */
  759. input_report_abs(usbtouch->input,
  760. ABS_MT_TOUCH_MAJOR, max(w,h));
  761. input_report_abs(usbtouch->input,
  762. ABS_MT_TOUCH_MINOR, min(x,h));
  763. input_report_abs(usbtouch->input,
  764. ABS_MT_POSITION_X, 2*begin_x+w);
  765. input_report_abs(usbtouch->input,
  766. ABS_MT_POSITION_Y, 2*begin_y+h);
  767. input_report_abs(usbtouch->input,
  768. ABS_MT_ORIENTATION, w > h);
  769. input_mt_sync(usbtouch->input);
  770. #endif
  771. /* single touch */
  772. usbtouch->x = 2 * begin_x + w;
  773. usbtouch->y = 2 * begin_y + h;
  774. usbtouch->touch = packet->flags & 0x01;
  775. begin_y = end_y = -1;
  776. return 1;
  777. }
  778. }
  779. begin_x = end_x = -1;
  780. }
  781. }
  782. return 0;
  783. }
  784. #endif
  785. /*****************************************************************************
  786. * the different device descriptors
  787. */
  788. #ifdef MULTI_PACKET
  789. static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
  790. unsigned char *pkt, int len);
  791. #endif
  792. static struct usbtouch_device_info usbtouch_dev_info[] = {
  793. #ifdef CONFIG_TOUCHSCREEN_USB_EGALAX
  794. [DEVTYPE_EGALAX] = {
  795. .min_xc = 0x0,
  796. .max_xc = 0x07ff,
  797. .min_yc = 0x0,
  798. .max_yc = 0x07ff,
  799. .rept_size = 16,
  800. .process_pkt = usbtouch_process_multi,
  801. .get_pkt_len = egalax_get_pkt_len,
  802. .read_data = egalax_read_data,
  803. },
  804. #endif
  805. #ifdef CONFIG_TOUCHSCREEN_USB_PANJIT
  806. [DEVTYPE_PANJIT] = {
  807. .min_xc = 0x0,
  808. .max_xc = 0x0fff,
  809. .min_yc = 0x0,
  810. .max_yc = 0x0fff,
  811. .rept_size = 8,
  812. .read_data = panjit_read_data,
  813. },
  814. #endif
  815. #ifdef CONFIG_TOUCHSCREEN_USB_3M
  816. [DEVTYPE_3M] = {
  817. .min_xc = 0x0,
  818. .max_xc = 0x4000,
  819. .min_yc = 0x0,
  820. .max_yc = 0x4000,
  821. .rept_size = 11,
  822. .read_data = mtouch_read_data,
  823. .init = mtouch_init,
  824. },
  825. #endif
  826. #ifdef CONFIG_TOUCHSCREEN_USB_ITM
  827. [DEVTYPE_ITM] = {
  828. .min_xc = 0x0,
  829. .max_xc = 0x0fff,
  830. .min_yc = 0x0,
  831. .max_yc = 0x0fff,
  832. .max_press = 0xff,
  833. .rept_size = 8,
  834. .read_data = itm_read_data,
  835. },
  836. #endif
  837. #ifdef CONFIG_TOUCHSCREEN_USB_ETURBO
  838. [DEVTYPE_ETURBO] = {
  839. .min_xc = 0x0,
  840. .max_xc = 0x07ff,
  841. .min_yc = 0x0,
  842. .max_yc = 0x07ff,
  843. .rept_size = 8,
  844. .process_pkt = usbtouch_process_multi,
  845. .get_pkt_len = eturbo_get_pkt_len,
  846. .read_data = eturbo_read_data,
  847. },
  848. #endif
  849. #ifdef CONFIG_TOUCHSCREEN_USB_GUNZE
  850. [DEVTYPE_GUNZE] = {
  851. .min_xc = 0x0,
  852. .max_xc = 0x0fff,
  853. .min_yc = 0x0,
  854. .max_yc = 0x0fff,
  855. .rept_size = 4,
  856. .read_data = gunze_read_data,
  857. },
  858. #endif
  859. #ifdef CONFIG_TOUCHSCREEN_USB_DMC_TSC10
  860. [DEVTYPE_DMC_TSC10] = {
  861. .min_xc = 0x0,
  862. .max_xc = 0x03ff,
  863. .min_yc = 0x0,
  864. .max_yc = 0x03ff,
  865. .rept_size = 5,
  866. .init = dmc_tsc10_init,
  867. .read_data = dmc_tsc10_read_data,
  868. },
  869. #endif
  870. #ifdef CONFIG_TOUCHSCREEN_USB_IRTOUCH
  871. [DEVTYPE_IRTOUCH] = {
  872. .min_xc = 0x0,
  873. .max_xc = 0x0fff,
  874. .min_yc = 0x0,
  875. .max_yc = 0x0fff,
  876. .rept_size = 8,
  877. .read_data = irtouch_read_data,
  878. },
  879. #endif
  880. #ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
  881. [DEVTYPE_IDEALTEK] = {
  882. .min_xc = 0x0,
  883. .max_xc = 0x0fff,
  884. .min_yc = 0x0,
  885. .max_yc = 0x0fff,
  886. .rept_size = 8,
  887. .process_pkt = usbtouch_process_multi,
  888. .get_pkt_len = idealtek_get_pkt_len,
  889. .read_data = idealtek_read_data,
  890. },
  891. #endif
  892. #ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
  893. [DEVTYPE_GENERAL_TOUCH] = {
  894. .min_xc = 0x0,
  895. .max_xc = 0x7fff,
  896. .min_yc = 0x0,
  897. .max_yc = 0x7fff,
  898. .rept_size = 7,
  899. .read_data = general_touch_read_data,
  900. },
  901. #endif
  902. #ifdef CONFIG_TOUCHSCREEN_USB_GOTOP
  903. [DEVTYPE_GOTOP] = {
  904. .min_xc = 0x0,
  905. .max_xc = 0x03ff,
  906. .min_yc = 0x0,
  907. .max_yc = 0x03ff,
  908. .rept_size = 4,
  909. .read_data = gotop_read_data,
  910. },
  911. #endif
  912. #ifdef CONFIG_TOUCHSCREEN_USB_JASTEC
  913. [DEVTYPE_JASTEC] = {
  914. .min_xc = 0x0,
  915. .max_xc = 0x0fff,
  916. .min_yc = 0x0,
  917. .max_yc = 0x0fff,
  918. .rept_size = 4,
  919. .read_data = jastec_read_data,
  920. },
  921. #endif
  922. #ifdef CONFIG_TOUCHSCREEN_USB_E2I
  923. [DEVTYPE_E2I] = {
  924. .min_xc = 0x0,
  925. .max_xc = 0x7fff,
  926. .min_yc = 0x0,
  927. .max_yc = 0x7fff,
  928. .rept_size = 6,
  929. .init = e2i_init,
  930. .read_data = e2i_read_data,
  931. },
  932. #endif
  933. #ifdef CONFIG_TOUCHSCREEN_USB_ZYTRONIC
  934. [DEVTYPE_ZYTRONIC] = {
  935. .min_xc = 0x0,
  936. .max_xc = 0x03ff,
  937. .min_yc = 0x0,
  938. .max_yc = 0x03ff,
  939. .rept_size = 5,
  940. .read_data = zytronic_read_data,
  941. .irq_always = true,
  942. },
  943. #endif
  944. #ifdef CONFIG_TOUCHSCREEN_USB_ETT_TC5UH
  945. [DEVTYPE_TC5UH] = {
  946. .min_xc = 0x0,
  947. .max_xc = 0x0fff,
  948. .min_yc = 0x0,
  949. .max_yc = 0x0fff,
  950. .rept_size = 5,
  951. .read_data = tc5uh_read_data,
  952. },
  953. #endif
  954. #ifdef CONFIG_TOUCHSCREEN_USB_NEXIO
  955. [DEVTYPE_NEXIO] = {
  956. .rept_size = 128,
  957. .irq_always = true,
  958. .read_data = nexio_read_data,
  959. .init = nexio_init,
  960. .exit = nexio_exit,
  961. },
  962. #endif
  963. };
  964. /*****************************************************************************
  965. * Generic Part
  966. */
  967. static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
  968. unsigned char *pkt, int len)
  969. {
  970. struct usbtouch_device_info *type = usbtouch->type;
  971. if (!type->read_data(usbtouch, pkt))
  972. return;
  973. input_report_key(usbtouch->input, BTN_TOUCH, usbtouch->touch);
  974. if (swap_xy) {
  975. input_report_abs(usbtouch->input, ABS_X, usbtouch->y);
  976. input_report_abs(usbtouch->input, ABS_Y, usbtouch->x);
  977. } else {
  978. input_report_abs(usbtouch->input, ABS_X, usbtouch->x);
  979. input_report_abs(usbtouch->input, ABS_Y, usbtouch->y);
  980. }
  981. if (type->max_press)
  982. input_report_abs(usbtouch->input, ABS_PRESSURE, usbtouch->press);
  983. input_sync(usbtouch->input);
  984. }
  985. #ifdef MULTI_PACKET
  986. static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
  987. unsigned char *pkt, int len)
  988. {
  989. unsigned char *buffer;
  990. int pkt_len, pos, buf_len, tmp;
  991. /* process buffer */
  992. if (unlikely(usbtouch->buf_len)) {
  993. /* try to get size */
  994. pkt_len = usbtouch->type->get_pkt_len(
  995. usbtouch->buffer, usbtouch->buf_len);
  996. /* drop? */
  997. if (unlikely(!pkt_len))
  998. goto out_flush_buf;
  999. /* need to append -pkt_len bytes before able to get size */
  1000. if (unlikely(pkt_len < 0)) {
  1001. int append = -pkt_len;
  1002. if (unlikely(append > len))
  1003. append = len;
  1004. if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
  1005. goto out_flush_buf;
  1006. memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
  1007. usbtouch->buf_len += append;
  1008. pkt_len = usbtouch->type->get_pkt_len(
  1009. usbtouch->buffer, usbtouch->buf_len);
  1010. if (pkt_len < 0)
  1011. return;
  1012. }
  1013. /* append */
  1014. tmp = pkt_len - usbtouch->buf_len;
  1015. if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
  1016. goto out_flush_buf;
  1017. memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
  1018. usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
  1019. buffer = pkt + tmp;
  1020. buf_len = len - tmp;
  1021. } else {
  1022. buffer = pkt;
  1023. buf_len = len;
  1024. }
  1025. /* loop over the received packet, process */
  1026. pos = 0;
  1027. while (pos < buf_len) {
  1028. /* get packet len */
  1029. pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
  1030. buf_len - pos);
  1031. /* unknown packet: skip one byte */
  1032. if (unlikely(!pkt_len)) {
  1033. pos++;
  1034. continue;
  1035. }
  1036. /* full packet: process */
  1037. if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
  1038. usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
  1039. } else {
  1040. /* incomplete packet: save in buffer */
  1041. memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
  1042. usbtouch->buf_len = buf_len - pos;
  1043. return;
  1044. }
  1045. pos += pkt_len;
  1046. }
  1047. out_flush_buf:
  1048. usbtouch->buf_len = 0;
  1049. return;
  1050. }
  1051. #endif
  1052. static void usbtouch_irq(struct urb *urb)
  1053. {
  1054. struct usbtouch_usb *usbtouch = urb->context;
  1055. int retval;
  1056. switch (urb->status) {
  1057. case 0:
  1058. /* success */
  1059. break;
  1060. case -ETIME:
  1061. /* this urb is timing out */
  1062. dbg("%s - urb timed out - was the device unplugged?",
  1063. __func__);
  1064. return;
  1065. case -ECONNRESET:
  1066. case -ENOENT:
  1067. case -ESHUTDOWN:
  1068. case -EPIPE:
  1069. /* this urb is terminated, clean up */
  1070. dbg("%s - urb shutting down with status: %d",
  1071. __func__, urb->status);
  1072. return;
  1073. default:
  1074. dbg("%s - nonzero urb status received: %d",
  1075. __func__, urb->status);
  1076. goto exit;
  1077. }
  1078. usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
  1079. exit:
  1080. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1081. if (retval)
  1082. err("%s - usb_submit_urb failed with result: %d",
  1083. __func__, retval);
  1084. }
  1085. static int usbtouch_open(struct input_dev *input)
  1086. {
  1087. struct usbtouch_usb *usbtouch = input_get_drvdata(input);
  1088. usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
  1089. if (!usbtouch->type->irq_always) {
  1090. if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
  1091. return -EIO;
  1092. }
  1093. return 0;
  1094. }
  1095. static void usbtouch_close(struct input_dev *input)
  1096. {
  1097. struct usbtouch_usb *usbtouch = input_get_drvdata(input);
  1098. if (!usbtouch->type->irq_always)
  1099. usb_kill_urb(usbtouch->irq);
  1100. }
  1101. static void usbtouch_free_buffers(struct usb_device *udev,
  1102. struct usbtouch_usb *usbtouch)
  1103. {
  1104. usb_free_coherent(udev, usbtouch->type->rept_size,
  1105. usbtouch->data, usbtouch->data_dma);
  1106. kfree(usbtouch->buffer);
  1107. }
  1108. static struct usb_endpoint_descriptor *
  1109. usbtouch_get_input_endpoint(struct usb_host_interface *interface)
  1110. {
  1111. int i;
  1112. for (i = 0; i < interface->desc.bNumEndpoints; i++)
  1113. if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
  1114. return &interface->endpoint[i].desc;
  1115. return NULL;
  1116. }
  1117. static int usbtouch_probe(struct usb_interface *intf,
  1118. const struct usb_device_id *id)
  1119. {
  1120. struct usbtouch_usb *usbtouch;
  1121. struct input_dev *input_dev;
  1122. struct usb_endpoint_descriptor *endpoint;
  1123. struct usb_device *udev = interface_to_usbdev(intf);
  1124. struct usbtouch_device_info *type;
  1125. int err = -ENOMEM;
  1126. /* some devices are ignored */
  1127. if (id->driver_info == DEVTYPE_IGNORE)
  1128. return -ENODEV;
  1129. endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
  1130. if (!endpoint)
  1131. return -ENXIO;
  1132. usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
  1133. input_dev = input_allocate_device();
  1134. if (!usbtouch || !input_dev)
  1135. goto out_free;
  1136. type = &usbtouch_dev_info[id->driver_info];
  1137. usbtouch->type = type;
  1138. if (!type->process_pkt)
  1139. type->process_pkt = usbtouch_process_pkt;
  1140. usbtouch->data = usb_alloc_coherent(udev, type->rept_size,
  1141. GFP_KERNEL, &usbtouch->data_dma);
  1142. if (!usbtouch->data)
  1143. goto out_free;
  1144. if (type->get_pkt_len) {
  1145. usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
  1146. if (!usbtouch->buffer)
  1147. goto out_free_buffers;
  1148. }
  1149. usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
  1150. if (!usbtouch->irq) {
  1151. dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
  1152. goto out_free_buffers;
  1153. }
  1154. usbtouch->interface = intf;
  1155. usbtouch->input = input_dev;
  1156. if (udev->manufacturer)
  1157. strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
  1158. if (udev->product) {
  1159. if (udev->manufacturer)
  1160. strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
  1161. strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
  1162. }
  1163. if (!strlen(usbtouch->name))
  1164. snprintf(usbtouch->name, sizeof(usbtouch->name),
  1165. "USB Touchscreen %04x:%04x",
  1166. le16_to_cpu(udev->descriptor.idVendor),
  1167. le16_to_cpu(udev->descriptor.idProduct));
  1168. usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
  1169. strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
  1170. input_dev->name = usbtouch->name;
  1171. input_dev->phys = usbtouch->phys;
  1172. usb_to_input_id(udev, &input_dev->id);
  1173. input_dev->dev.parent = &intf->dev;
  1174. input_set_drvdata(input_dev, usbtouch);
  1175. input_dev->open = usbtouch_open;
  1176. input_dev->close = usbtouch_close;
  1177. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  1178. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  1179. input_set_abs_params(input_dev, ABS_X, type->min_xc, type->max_xc, 0, 0);
  1180. input_set_abs_params(input_dev, ABS_Y, type->min_yc, type->max_yc, 0, 0);
  1181. if (type->max_press)
  1182. input_set_abs_params(input_dev, ABS_PRESSURE, type->min_press,
  1183. type->max_press, 0, 0);
  1184. if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
  1185. usb_fill_int_urb(usbtouch->irq, udev,
  1186. usb_rcvintpipe(udev, endpoint->bEndpointAddress),
  1187. usbtouch->data, type->rept_size,
  1188. usbtouch_irq, usbtouch, endpoint->bInterval);
  1189. else
  1190. usb_fill_bulk_urb(usbtouch->irq, udev,
  1191. usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
  1192. usbtouch->data, type->rept_size,
  1193. usbtouch_irq, usbtouch);
  1194. usbtouch->irq->dev = udev;
  1195. usbtouch->irq->transfer_dma = usbtouch->data_dma;
  1196. usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1197. /* device specific init */
  1198. if (type->init) {
  1199. err = type->init(usbtouch);
  1200. if (err) {
  1201. dbg("%s - type->init() failed, err: %d", __func__, err);
  1202. goto out_free_urb;
  1203. }
  1204. }
  1205. err = input_register_device(usbtouch->input);
  1206. if (err) {
  1207. dbg("%s - input_register_device failed, err: %d", __func__, err);
  1208. goto out_do_exit;
  1209. }
  1210. usb_set_intfdata(intf, usbtouch);
  1211. if (usbtouch->type->irq_always) {
  1212. err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
  1213. if (err) {
  1214. err("%s - usb_submit_urb failed with result: %d",
  1215. __func__, err);
  1216. goto out_unregister_input;
  1217. }
  1218. }
  1219. return 0;
  1220. out_unregister_input:
  1221. input_unregister_device(input_dev);
  1222. input_dev = NULL;
  1223. out_do_exit:
  1224. if (type->exit)
  1225. type->exit(usbtouch);
  1226. out_free_urb:
  1227. usb_free_urb(usbtouch->irq);
  1228. out_free_buffers:
  1229. usbtouch_free_buffers(udev, usbtouch);
  1230. out_free:
  1231. input_free_device(input_dev);
  1232. kfree(usbtouch);
  1233. return err;
  1234. }
  1235. static void usbtouch_disconnect(struct usb_interface *intf)
  1236. {
  1237. struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
  1238. dbg("%s - called", __func__);
  1239. if (!usbtouch)
  1240. return;
  1241. dbg("%s - usbtouch is initialized, cleaning up", __func__);
  1242. usb_set_intfdata(intf, NULL);
  1243. /* this will stop IO via close */
  1244. input_unregister_device(usbtouch->input);
  1245. usb_free_urb(usbtouch->irq);
  1246. if (usbtouch->type->exit)
  1247. usbtouch->type->exit(usbtouch);
  1248. usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
  1249. kfree(usbtouch);
  1250. }
  1251. MODULE_DEVICE_TABLE(usb, usbtouch_devices);
  1252. static struct usb_driver usbtouch_driver = {
  1253. .name = "usbtouchscreen",
  1254. .probe = usbtouch_probe,
  1255. .disconnect = usbtouch_disconnect,
  1256. .id_table = usbtouch_devices,
  1257. };
  1258. static int __init usbtouch_init(void)
  1259. {
  1260. return usb_register(&usbtouch_driver);
  1261. }
  1262. static void __exit usbtouch_cleanup(void)
  1263. {
  1264. usb_deregister(&usbtouch_driver);
  1265. }
  1266. module_init(usbtouch_init);
  1267. module_exit(usbtouch_cleanup);
  1268. MODULE_AUTHOR(DRIVER_AUTHOR);
  1269. MODULE_DESCRIPTION(DRIVER_DESC);
  1270. MODULE_LICENSE("GPL");
  1271. MODULE_ALIAS("touchkitusb");
  1272. MODULE_ALIAS("itmtouch");
  1273. MODULE_ALIAS("mtouchusb");