usbtouchscreen.c 37 KB

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