usbtouchscreen.c 39 KB

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