usbtouchscreen.c 40 KB

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