usbtouchscreen.c 37 KB

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