aiptek.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  1. /*
  2. * Native support for the Aiptek HyperPen USB Tablets
  3. * (4000U/5000U/6000U/8000U/12000U)
  4. *
  5. * Copyright (c) 2001 Chris Atenasio <chris@crud.net>
  6. * Copyright (c) 2002-2004 Bryan W. Headley <bwheadley@earthlink.net>
  7. *
  8. * based on wacom.c by
  9. * Vojtech Pavlik <vojtech@suse.cz>
  10. * Andreas Bach Aaen <abach@stofanet.dk>
  11. * Clifford Wolf <clifford@clifford.at>
  12. * Sam Mosel <sam.mosel@computer.org>
  13. * James E. Blair <corvus@gnu.org>
  14. * Daniel Egger <egger@suse.de>
  15. *
  16. * Many thanks to Oliver Kuechemann for his support.
  17. *
  18. * ChangeLog:
  19. * v0.1 - Initial release
  20. * v0.2 - Hack to get around fake event 28's. (Bryan W. Headley)
  21. * v0.3 - Make URB dynamic (Bryan W. Headley, Jun-8-2002)
  22. * Released to Linux 2.4.19 and 2.5.x
  23. * v0.4 - Rewrote substantial portions of the code to deal with
  24. * corrected control sequences, timing, dynamic configuration,
  25. * support of 6000U - 12000U, procfs, and macro key support
  26. * (Jan-1-2003 - Feb-5-2003, Bryan W. Headley)
  27. * v1.0 - Added support for diagnostic messages, count of messages
  28. * received from URB - Mar-8-2003, Bryan W. Headley
  29. * v1.1 - added support for tablet resolution, changed DV and proximity
  30. * some corrections - Jun-22-2003, martin schneebacher
  31. * - Added support for the sysfs interface, deprecating the
  32. * procfs interface for 2.5.x kernel. Also added support for
  33. * Wheel command. Bryan W. Headley July-15-2003.
  34. * v1.2 - Reworked jitter timer as a kernel thread.
  35. * Bryan W. Headley November-28-2003/Jan-10-2004.
  36. * v1.3 - Repaired issue of kernel thread going nuts on single-processor
  37. * machines, introduced programmableDelay as a command line
  38. * parameter. Feb 7 2004, Bryan W. Headley.
  39. * v1.4 - Re-wire jitter so it does not require a thread. Courtesy of
  40. * Rene van Paassen. Added reporting of physical pointer device
  41. * (e.g., stylus, mouse in reports 2, 3, 4, 5. We don't know
  42. * for reports 1, 6.)
  43. * what physical device reports for reports 1, 6.) Also enabled
  44. * MOUSE and LENS tool button modes. Renamed "rubber" to "eraser".
  45. * Feb 20, 2004, Bryan W. Headley.
  46. * v1.5 - Added previousJitterable, so we don't do jitter delay when the
  47. * user is holding a button down for periods of time.
  48. *
  49. * NOTE:
  50. * This kernel driver is augmented by the "Aiptek" XFree86 input
  51. * driver for your X server, as well as the Gaiptek GUI Front-end
  52. * "Tablet Manager".
  53. * These three products are highly interactive with one another,
  54. * so therefore it's easier to document them all as one subsystem.
  55. * Please visit the project's "home page", located at,
  56. * http://aiptektablet.sourceforge.net.
  57. *
  58. * This program is free software; you can redistribute it and/or modify
  59. * it under the terms of the GNU General Public License as published by
  60. * the Free Software Foundation; either version 2 of the License, or
  61. * (at your option) any later version.
  62. *
  63. * This program is distributed in the hope that it will be useful,
  64. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  66. * GNU General Public License for more details.
  67. *
  68. * You should have received a copy of the GNU General Public License
  69. * along with this program; if not, write to the Free Software
  70. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  71. */
  72. #include <linux/jiffies.h>
  73. #include <linux/kernel.h>
  74. #include <linux/slab.h>
  75. #include <linux/module.h>
  76. #include <linux/usb/input.h>
  77. #include <asm/uaccess.h>
  78. #include <asm/unaligned.h>
  79. /*
  80. * Version Information
  81. */
  82. #define DRIVER_VERSION "v2.3 (May 2, 2007)"
  83. #define DRIVER_AUTHOR "Bryan W. Headley/Chris Atenasio/Cedric Brun/Rene van Paassen"
  84. #define DRIVER_DESC "Aiptek HyperPen USB Tablet Driver (Linux 2.6.x)"
  85. /*
  86. * Aiptek status packet:
  87. *
  88. * (returned as Report 1 - relative coordinates from mouse and stylus)
  89. *
  90. * bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
  91. * byte0 0 0 0 0 0 0 0 1
  92. * byte1 0 0 0 0 0 BS2 BS Tip
  93. * byte2 X7 X6 X5 X4 X3 X2 X1 X0
  94. * byte3 Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
  95. *
  96. * (returned as Report 2 - absolute coordinates from the stylus)
  97. *
  98. * bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
  99. * byte0 0 0 0 0 0 0 1 0
  100. * byte1 X7 X6 X5 X4 X3 X2 X1 X0
  101. * byte2 X15 X14 X13 X12 X11 X10 X9 X8
  102. * byte3 Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
  103. * byte4 Y15 Y14 Y13 Y12 Y11 Y10 Y9 Y8
  104. * byte5 * * * BS2 BS1 Tip IR DV
  105. * byte6 P7 P6 P5 P4 P3 P2 P1 P0
  106. * byte7 P15 P14 P13 P12 P11 P10 P9 P8
  107. *
  108. * (returned as Report 3 - absolute coordinates from the mouse)
  109. *
  110. * bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
  111. * byte0 0 0 0 0 0 0 1 1
  112. * byte1 X7 X6 X5 X4 X3 X2 X1 X0
  113. * byte2 X15 X14 X13 X12 X11 X10 X9 X8
  114. * byte3 Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
  115. * byte4 Y15 Y14 Y13 Y12 Y11 Y10 Y9 Y8
  116. * byte5 * * * BS2 BS1 Tip IR DV
  117. * byte6 P7 P6 P5 P4 P3 P2 P1 P0
  118. * byte7 P15 P14 P13 P12 P11 P10 P9 P8
  119. *
  120. * (returned as Report 4 - macrokeys from the stylus)
  121. *
  122. * bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
  123. * byte0 0 0 0 0 0 1 0 0
  124. * byte1 0 0 0 BS2 BS Tip IR DV
  125. * byte2 0 0 0 0 0 0 1 0
  126. * byte3 0 0 0 K4 K3 K2 K1 K0
  127. * byte4 P7 P6 P5 P4 P3 P2 P1 P0
  128. * byte5 P15 P14 P13 P12 P11 P10 P9 P8
  129. *
  130. * (returned as Report 5 - macrokeys from the mouse)
  131. *
  132. * bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
  133. * byte0 0 0 0 0 0 1 0 1
  134. * byte1 0 0 0 BS2 BS Tip IR DV
  135. * byte2 0 0 0 0 0 0 1 0
  136. * byte3 0 0 0 K4 K3 K2 K1 K0
  137. * byte4 P7 P6 P5 P4 P3 P2 P1 P0
  138. * byte5 P15 P14 P13 P12 P11 P10 P9 P8
  139. *
  140. * IR: In Range = Proximity on
  141. * DV = Data Valid
  142. * BS = Barrel Switch (as in, macro keys)
  143. * BS2 also referred to as Tablet Pick
  144. *
  145. * Command Summary:
  146. *
  147. * Use report_type CONTROL (3)
  148. * Use report_id 2
  149. *
  150. * Command/Data Description Return Bytes Return Value
  151. * 0x10/0x00 SwitchToMouse 0
  152. * 0x10/0x01 SwitchToTablet 0
  153. * 0x18/0x04 SetResolution 0
  154. * 0x12/0xFF AutoGainOn 0
  155. * 0x17/0x00 FilterOn 0
  156. * 0x01/0x00 GetXExtension 2 MaxX
  157. * 0x01/0x01 GetYExtension 2 MaxY
  158. * 0x02/0x00 GetModelCode 2 ModelCode = LOBYTE
  159. * 0x03/0x00 GetODMCode 2 ODMCode
  160. * 0x08/0x00 GetPressureLevels 2 =512
  161. * 0x04/0x00 GetFirmwareVersion 2 Firmware Version
  162. * 0x11/0x02 EnableMacroKeys 0
  163. *
  164. * To initialize the tablet:
  165. *
  166. * (1) Send Resolution500LPI (Command)
  167. * (2) Query for Model code (Option Report)
  168. * (3) Query for ODM code (Option Report)
  169. * (4) Query for firmware (Option Report)
  170. * (5) Query for GetXExtension (Option Report)
  171. * (6) Query for GetYExtension (Option Report)
  172. * (7) Query for GetPressureLevels (Option Report)
  173. * (8) SwitchToTablet for Absolute coordinates, or
  174. * SwitchToMouse for Relative coordinates (Command)
  175. * (9) EnableMacroKeys (Command)
  176. * (10) FilterOn (Command)
  177. * (11) AutoGainOn (Command)
  178. *
  179. * (Step 9 can be omitted, but you'll then have no function keys.)
  180. */
  181. #define USB_VENDOR_ID_AIPTEK 0x08ca
  182. #define USB_VENDOR_ID_KYE 0x0458
  183. #define USB_REQ_GET_REPORT 0x01
  184. #define USB_REQ_SET_REPORT 0x09
  185. /* PointerMode codes
  186. */
  187. #define AIPTEK_POINTER_ONLY_MOUSE_MODE 0
  188. #define AIPTEK_POINTER_ONLY_STYLUS_MODE 1
  189. #define AIPTEK_POINTER_EITHER_MODE 2
  190. #define AIPTEK_POINTER_ALLOW_MOUSE_MODE(a) \
  191. (a == AIPTEK_POINTER_ONLY_MOUSE_MODE || \
  192. a == AIPTEK_POINTER_EITHER_MODE)
  193. #define AIPTEK_POINTER_ALLOW_STYLUS_MODE(a) \
  194. (a == AIPTEK_POINTER_ONLY_STYLUS_MODE || \
  195. a == AIPTEK_POINTER_EITHER_MODE)
  196. /* CoordinateMode code
  197. */
  198. #define AIPTEK_COORDINATE_RELATIVE_MODE 0
  199. #define AIPTEK_COORDINATE_ABSOLUTE_MODE 1
  200. /* XTilt and YTilt values
  201. */
  202. #define AIPTEK_TILT_MIN (-128)
  203. #define AIPTEK_TILT_MAX 127
  204. #define AIPTEK_TILT_DISABLE (-10101)
  205. /* Wheel values
  206. */
  207. #define AIPTEK_WHEEL_MIN 0
  208. #define AIPTEK_WHEEL_MAX 1024
  209. #define AIPTEK_WHEEL_DISABLE (-10101)
  210. /* ToolCode values, which BTW are 0x140 .. 0x14f
  211. * We have things set up such that if the tool button has changed,
  212. * the tools get reset.
  213. */
  214. /* toolMode codes
  215. */
  216. #define AIPTEK_TOOL_BUTTON_PEN_MODE BTN_TOOL_PEN
  217. #define AIPTEK_TOOL_BUTTON_PENCIL_MODE BTN_TOOL_PENCIL
  218. #define AIPTEK_TOOL_BUTTON_BRUSH_MODE BTN_TOOL_BRUSH
  219. #define AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE BTN_TOOL_AIRBRUSH
  220. #define AIPTEK_TOOL_BUTTON_ERASER_MODE BTN_TOOL_RUBBER
  221. #define AIPTEK_TOOL_BUTTON_MOUSE_MODE BTN_TOOL_MOUSE
  222. #define AIPTEK_TOOL_BUTTON_LENS_MODE BTN_TOOL_LENS
  223. /* Diagnostic message codes
  224. */
  225. #define AIPTEK_DIAGNOSTIC_NA 0
  226. #define AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE 1
  227. #define AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE 2
  228. #define AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED 3
  229. /* Time to wait (in ms) to help mask hand jittering
  230. * when pressing the stylus buttons.
  231. */
  232. #define AIPTEK_JITTER_DELAY_DEFAULT 50
  233. /* Time to wait (in ms) in-between sending the tablet
  234. * a command and beginning the process of reading the return
  235. * sequence from the tablet.
  236. */
  237. #define AIPTEK_PROGRAMMABLE_DELAY_25 25
  238. #define AIPTEK_PROGRAMMABLE_DELAY_50 50
  239. #define AIPTEK_PROGRAMMABLE_DELAY_100 100
  240. #define AIPTEK_PROGRAMMABLE_DELAY_200 200
  241. #define AIPTEK_PROGRAMMABLE_DELAY_300 300
  242. #define AIPTEK_PROGRAMMABLE_DELAY_400 400
  243. #define AIPTEK_PROGRAMMABLE_DELAY_DEFAULT AIPTEK_PROGRAMMABLE_DELAY_400
  244. /* Mouse button programming
  245. */
  246. #define AIPTEK_MOUSE_LEFT_BUTTON 0x04
  247. #define AIPTEK_MOUSE_RIGHT_BUTTON 0x08
  248. #define AIPTEK_MOUSE_MIDDLE_BUTTON 0x10
  249. /* Stylus button programming
  250. */
  251. #define AIPTEK_STYLUS_LOWER_BUTTON 0x08
  252. #define AIPTEK_STYLUS_UPPER_BUTTON 0x10
  253. /* Length of incoming packet from the tablet
  254. */
  255. #define AIPTEK_PACKET_LENGTH 8
  256. /* We report in EV_MISC both the proximity and
  257. * whether the report came from the stylus, tablet mouse
  258. * or "unknown" -- Unknown when the tablet is in relative
  259. * mode, because we only get report 1's.
  260. */
  261. #define AIPTEK_REPORT_TOOL_UNKNOWN 0x10
  262. #define AIPTEK_REPORT_TOOL_STYLUS 0x20
  263. #define AIPTEK_REPORT_TOOL_MOUSE 0x40
  264. static int programmableDelay = AIPTEK_PROGRAMMABLE_DELAY_DEFAULT;
  265. static int jitterDelay = AIPTEK_JITTER_DELAY_DEFAULT;
  266. struct aiptek_features {
  267. int odmCode; /* Tablet manufacturer code */
  268. int modelCode; /* Tablet model code (not unique) */
  269. int firmwareCode; /* prom/eeprom version */
  270. char usbPath[64 + 1]; /* device's physical usb path */
  271. };
  272. struct aiptek_settings {
  273. int pointerMode; /* stylus-, mouse-only or either */
  274. int coordinateMode; /* absolute/relative coords */
  275. int toolMode; /* pen, pencil, brush, etc. tool */
  276. int xTilt; /* synthetic xTilt amount */
  277. int yTilt; /* synthetic yTilt amount */
  278. int wheel; /* synthetic wheel amount */
  279. int stylusButtonUpper; /* stylus upper btn delivers... */
  280. int stylusButtonLower; /* stylus lower btn delivers... */
  281. int mouseButtonLeft; /* mouse left btn delivers... */
  282. int mouseButtonMiddle; /* mouse middle btn delivers... */
  283. int mouseButtonRight; /* mouse right btn delivers... */
  284. int programmableDelay; /* delay for tablet programming */
  285. int jitterDelay; /* delay for hand jittering */
  286. };
  287. struct aiptek {
  288. struct input_dev *inputdev; /* input device struct */
  289. struct usb_interface *intf; /* usb interface struct */
  290. struct urb *urb; /* urb for incoming reports */
  291. dma_addr_t data_dma; /* our dma stuffage */
  292. struct aiptek_features features; /* tablet's array of features */
  293. struct aiptek_settings curSetting; /* tablet's current programmable */
  294. struct aiptek_settings newSetting; /* ... and new param settings */
  295. unsigned int ifnum; /* interface number for IO */
  296. int diagnostic; /* tablet diagnostic codes */
  297. unsigned long eventCount; /* event count */
  298. int inDelay; /* jitter: in jitter delay? */
  299. unsigned long endDelay; /* jitter: time when delay ends */
  300. int previousJitterable; /* jitterable prev value */
  301. int lastMacro; /* macro key to reset */
  302. int previousToolMode; /* pen, pencil, brush, etc. tool */
  303. unsigned char *data; /* incoming packet data */
  304. };
  305. static const int eventTypes[] = {
  306. EV_KEY, EV_ABS, EV_REL, EV_MSC,
  307. };
  308. static const int absEvents[] = {
  309. ABS_X, ABS_Y, ABS_PRESSURE, ABS_TILT_X, ABS_TILT_Y,
  310. ABS_WHEEL, ABS_MISC,
  311. };
  312. static const int relEvents[] = {
  313. REL_X, REL_Y, REL_WHEEL,
  314. };
  315. static const int buttonEvents[] = {
  316. BTN_LEFT, BTN_RIGHT, BTN_MIDDLE,
  317. BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_PENCIL, BTN_TOOL_AIRBRUSH,
  318. BTN_TOOL_BRUSH, BTN_TOOL_MOUSE, BTN_TOOL_LENS, BTN_TOUCH,
  319. BTN_STYLUS, BTN_STYLUS2,
  320. };
  321. /*
  322. * Permit easy lookup of keyboard events to send, versus
  323. * the bitmap which comes from the tablet. This hides the
  324. * issue that the F_keys are not sequentially numbered.
  325. */
  326. static const int macroKeyEvents[] = {
  327. KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5,
  328. KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11,
  329. KEY_F12, KEY_F13, KEY_F14, KEY_F15, KEY_F16, KEY_F17,
  330. KEY_F18, KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23,
  331. KEY_F24, KEY_STOP, KEY_AGAIN, KEY_PROPS, KEY_UNDO,
  332. KEY_FRONT, KEY_COPY, KEY_OPEN, KEY_PASTE, 0
  333. };
  334. /***********************************************************************
  335. * Map values to strings and back. Every map should have the following
  336. * as its last element: { NULL, AIPTEK_INVALID_VALUE }.
  337. */
  338. #define AIPTEK_INVALID_VALUE -1
  339. struct aiptek_map {
  340. const char *string;
  341. int value;
  342. };
  343. static int map_str_to_val(const struct aiptek_map *map, const char *str, size_t count)
  344. {
  345. const struct aiptek_map *p;
  346. if (str[count - 1] == '\n')
  347. count--;
  348. for (p = map; p->string; p++)
  349. if (!strncmp(str, p->string, count))
  350. return p->value;
  351. return AIPTEK_INVALID_VALUE;
  352. }
  353. static const char *map_val_to_str(const struct aiptek_map *map, int val)
  354. {
  355. const struct aiptek_map *p;
  356. for (p = map; p->value != AIPTEK_INVALID_VALUE; p++)
  357. if (val == p->value)
  358. return p->string;
  359. return "unknown";
  360. }
  361. /***********************************************************************
  362. * aiptek_irq can receive one of six potential reports.
  363. * The documentation for each is in the body of the function.
  364. *
  365. * The tablet reports on several attributes per invocation of
  366. * aiptek_irq. Because the Linux Input Event system allows the
  367. * transmission of ONE attribute per input_report_xxx() call,
  368. * collation has to be done on the other end to reconstitute
  369. * a complete tablet report. Further, the number of Input Event reports
  370. * submitted varies, depending on what USB report type, and circumstance.
  371. * To deal with this, EV_MSC is used to indicate an 'end-of-report'
  372. * message. This has been an undocumented convention understood by the kernel
  373. * tablet driver and clients such as gpm and XFree86's tablet drivers.
  374. *
  375. * Of the information received from the tablet, the one piece I
  376. * cannot transmit is the proximity bit (without resorting to an EV_MSC
  377. * convention above.) I therefore have taken over REL_MISC and ABS_MISC
  378. * (for relative and absolute reports, respectively) for communicating
  379. * Proximity. Why two events? I thought it interesting to know if the
  380. * Proximity event occurred while the tablet was in absolute or relative
  381. * mode.
  382. * Update: REL_MISC proved not to be such a good idea. With REL_MISC you
  383. * get an event transmitted each time. ABS_MISC works better, since it
  384. * can be set and re-set. Thus, only using ABS_MISC from now on.
  385. *
  386. * Other tablets use the notion of a certain minimum stylus pressure
  387. * to infer proximity. While that could have been done, that is yet
  388. * another 'by convention' behavior, the documentation for which
  389. * would be spread between two (or more) pieces of software.
  390. *
  391. * EV_MSC usage was terminated for this purpose in Linux 2.5.x, and
  392. * replaced with the input_sync() method (which emits EV_SYN.)
  393. */
  394. static void aiptek_irq(struct urb *urb)
  395. {
  396. struct aiptek *aiptek = urb->context;
  397. unsigned char *data = aiptek->data;
  398. struct input_dev *inputdev = aiptek->inputdev;
  399. struct usb_interface *intf = aiptek->intf;
  400. int jitterable = 0;
  401. int retval, macro, x, y, z, left, right, middle, p, dv, tip, bs, pck;
  402. switch (urb->status) {
  403. case 0:
  404. /* Success */
  405. break;
  406. case -ECONNRESET:
  407. case -ENOENT:
  408. case -ESHUTDOWN:
  409. /* This urb is terminated, clean up */
  410. dev_dbg(&intf->dev, "%s - urb shutting down with status: %d\n",
  411. __func__, urb->status);
  412. return;
  413. default:
  414. dev_dbg(&intf->dev, "%s - nonzero urb status received: %d\n",
  415. __func__, urb->status);
  416. goto exit;
  417. }
  418. /* See if we are in a delay loop -- throw out report if true.
  419. */
  420. if (aiptek->inDelay == 1 && time_after(aiptek->endDelay, jiffies)) {
  421. goto exit;
  422. }
  423. aiptek->inDelay = 0;
  424. aiptek->eventCount++;
  425. /* Report 1 delivers relative coordinates with either a stylus
  426. * or the mouse. You do not know, however, which input
  427. * tool generated the event.
  428. */
  429. if (data[0] == 1) {
  430. if (aiptek->curSetting.coordinateMode ==
  431. AIPTEK_COORDINATE_ABSOLUTE_MODE) {
  432. aiptek->diagnostic =
  433. AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE;
  434. } else {
  435. x = (signed char) data[2];
  436. y = (signed char) data[3];
  437. /* jitterable keeps track of whether any button has been pressed.
  438. * We're also using it to remap the physical mouse button mask
  439. * to pseudo-settings. (We don't specifically care about it's
  440. * value after moving/transposing mouse button bitmasks, except
  441. * that a non-zero value indicates that one or more
  442. * mouse button was pressed.)
  443. */
  444. jitterable = data[1] & 0x07;
  445. left = (data[1] & aiptek->curSetting.mouseButtonLeft >> 2) != 0 ? 1 : 0;
  446. right = (data[1] & aiptek->curSetting.mouseButtonRight >> 2) != 0 ? 1 : 0;
  447. middle = (data[1] & aiptek->curSetting.mouseButtonMiddle >> 2) != 0 ? 1 : 0;
  448. input_report_key(inputdev, BTN_LEFT, left);
  449. input_report_key(inputdev, BTN_MIDDLE, middle);
  450. input_report_key(inputdev, BTN_RIGHT, right);
  451. input_report_abs(inputdev, ABS_MISC,
  452. 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
  453. input_report_rel(inputdev, REL_X, x);
  454. input_report_rel(inputdev, REL_Y, y);
  455. /* Wheel support is in the form of a single-event
  456. * firing.
  457. */
  458. if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
  459. input_report_rel(inputdev, REL_WHEEL,
  460. aiptek->curSetting.wheel);
  461. aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
  462. }
  463. if (aiptek->lastMacro != -1) {
  464. input_report_key(inputdev,
  465. macroKeyEvents[aiptek->lastMacro], 0);
  466. aiptek->lastMacro = -1;
  467. }
  468. input_sync(inputdev);
  469. }
  470. }
  471. /* Report 2 is delivered only by the stylus, and delivers
  472. * absolute coordinates.
  473. */
  474. else if (data[0] == 2) {
  475. if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
  476. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
  477. } else if (!AIPTEK_POINTER_ALLOW_STYLUS_MODE
  478. (aiptek->curSetting.pointerMode)) {
  479. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
  480. } else {
  481. x = get_unaligned_le16(data + 1);
  482. y = get_unaligned_le16(data + 3);
  483. z = get_unaligned_le16(data + 6);
  484. dv = (data[5] & 0x01) != 0 ? 1 : 0;
  485. p = (data[5] & 0x02) != 0 ? 1 : 0;
  486. tip = (data[5] & 0x04) != 0 ? 1 : 0;
  487. /* Use jitterable to re-arrange button masks
  488. */
  489. jitterable = data[5] & 0x18;
  490. bs = (data[5] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
  491. pck = (data[5] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;
  492. /* dv indicates 'data valid' (e.g., the tablet is in sync
  493. * and has delivered a "correct" report) We will ignore
  494. * all 'bad' reports...
  495. */
  496. if (dv != 0) {
  497. /* If the selected tool changed, reset the old
  498. * tool key, and set the new one.
  499. */
  500. if (aiptek->previousToolMode !=
  501. aiptek->curSetting.toolMode) {
  502. input_report_key(inputdev,
  503. aiptek->previousToolMode, 0);
  504. input_report_key(inputdev,
  505. aiptek->curSetting.toolMode,
  506. 1);
  507. aiptek->previousToolMode =
  508. aiptek->curSetting.toolMode;
  509. }
  510. if (p != 0) {
  511. input_report_abs(inputdev, ABS_X, x);
  512. input_report_abs(inputdev, ABS_Y, y);
  513. input_report_abs(inputdev, ABS_PRESSURE, z);
  514. input_report_key(inputdev, BTN_TOUCH, tip);
  515. input_report_key(inputdev, BTN_STYLUS, bs);
  516. input_report_key(inputdev, BTN_STYLUS2, pck);
  517. if (aiptek->curSetting.xTilt !=
  518. AIPTEK_TILT_DISABLE) {
  519. input_report_abs(inputdev,
  520. ABS_TILT_X,
  521. aiptek->curSetting.xTilt);
  522. }
  523. if (aiptek->curSetting.yTilt != AIPTEK_TILT_DISABLE) {
  524. input_report_abs(inputdev,
  525. ABS_TILT_Y,
  526. aiptek->curSetting.yTilt);
  527. }
  528. /* Wheel support is in the form of a single-event
  529. * firing.
  530. */
  531. if (aiptek->curSetting.wheel !=
  532. AIPTEK_WHEEL_DISABLE) {
  533. input_report_abs(inputdev,
  534. ABS_WHEEL,
  535. aiptek->curSetting.wheel);
  536. aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
  537. }
  538. }
  539. input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_STYLUS);
  540. if (aiptek->lastMacro != -1) {
  541. input_report_key(inputdev,
  542. macroKeyEvents[aiptek->lastMacro], 0);
  543. aiptek->lastMacro = -1;
  544. }
  545. input_sync(inputdev);
  546. }
  547. }
  548. }
  549. /* Report 3's come from the mouse in absolute mode.
  550. */
  551. else if (data[0] == 3) {
  552. if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
  553. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
  554. } else if (!AIPTEK_POINTER_ALLOW_MOUSE_MODE
  555. (aiptek->curSetting.pointerMode)) {
  556. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
  557. } else {
  558. x = get_unaligned_le16(data + 1);
  559. y = get_unaligned_le16(data + 3);
  560. jitterable = data[5] & 0x1c;
  561. dv = (data[5] & 0x01) != 0 ? 1 : 0;
  562. p = (data[5] & 0x02) != 0 ? 1 : 0;
  563. left = (data[5] & aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
  564. right = (data[5] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
  565. middle = (data[5] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
  566. if (dv != 0) {
  567. /* If the selected tool changed, reset the old
  568. * tool key, and set the new one.
  569. */
  570. if (aiptek->previousToolMode !=
  571. aiptek->curSetting.toolMode) {
  572. input_report_key(inputdev,
  573. aiptek->previousToolMode, 0);
  574. input_report_key(inputdev,
  575. aiptek->curSetting.toolMode,
  576. 1);
  577. aiptek->previousToolMode =
  578. aiptek->curSetting.toolMode;
  579. }
  580. if (p != 0) {
  581. input_report_abs(inputdev, ABS_X, x);
  582. input_report_abs(inputdev, ABS_Y, y);
  583. input_report_key(inputdev, BTN_LEFT, left);
  584. input_report_key(inputdev, BTN_MIDDLE, middle);
  585. input_report_key(inputdev, BTN_RIGHT, right);
  586. /* Wheel support is in the form of a single-event
  587. * firing.
  588. */
  589. if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
  590. input_report_abs(inputdev,
  591. ABS_WHEEL,
  592. aiptek->curSetting.wheel);
  593. aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
  594. }
  595. }
  596. input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_MOUSE);
  597. if (aiptek->lastMacro != -1) {
  598. input_report_key(inputdev,
  599. macroKeyEvents[aiptek->lastMacro], 0);
  600. aiptek->lastMacro = -1;
  601. }
  602. input_sync(inputdev);
  603. }
  604. }
  605. }
  606. /* Report 4s come from the macro keys when pressed by stylus
  607. */
  608. else if (data[0] == 4) {
  609. jitterable = data[1] & 0x18;
  610. dv = (data[1] & 0x01) != 0 ? 1 : 0;
  611. p = (data[1] & 0x02) != 0 ? 1 : 0;
  612. tip = (data[1] & 0x04) != 0 ? 1 : 0;
  613. bs = (data[1] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
  614. pck = (data[1] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;
  615. macro = dv && p && tip && !(data[3] & 1) ? (data[3] >> 1) : -1;
  616. z = get_unaligned_le16(data + 4);
  617. if (dv) {
  618. /* If the selected tool changed, reset the old
  619. * tool key, and set the new one.
  620. */
  621. if (aiptek->previousToolMode !=
  622. aiptek->curSetting.toolMode) {
  623. input_report_key(inputdev,
  624. aiptek->previousToolMode, 0);
  625. input_report_key(inputdev,
  626. aiptek->curSetting.toolMode,
  627. 1);
  628. aiptek->previousToolMode =
  629. aiptek->curSetting.toolMode;
  630. }
  631. }
  632. if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
  633. input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
  634. aiptek->lastMacro = -1;
  635. }
  636. if (macro != -1 && macro != aiptek->lastMacro) {
  637. input_report_key(inputdev, macroKeyEvents[macro], 1);
  638. aiptek->lastMacro = macro;
  639. }
  640. input_report_abs(inputdev, ABS_MISC,
  641. p | AIPTEK_REPORT_TOOL_STYLUS);
  642. input_sync(inputdev);
  643. }
  644. /* Report 5s come from the macro keys when pressed by mouse
  645. */
  646. else if (data[0] == 5) {
  647. jitterable = data[1] & 0x1c;
  648. dv = (data[1] & 0x01) != 0 ? 1 : 0;
  649. p = (data[1] & 0x02) != 0 ? 1 : 0;
  650. left = (data[1]& aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
  651. right = (data[1] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
  652. middle = (data[1] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
  653. macro = dv && p && left && !(data[3] & 1) ? (data[3] >> 1) : 0;
  654. if (dv) {
  655. /* If the selected tool changed, reset the old
  656. * tool key, and set the new one.
  657. */
  658. if (aiptek->previousToolMode !=
  659. aiptek->curSetting.toolMode) {
  660. input_report_key(inputdev,
  661. aiptek->previousToolMode, 0);
  662. input_report_key(inputdev,
  663. aiptek->curSetting.toolMode, 1);
  664. aiptek->previousToolMode = aiptek->curSetting.toolMode;
  665. }
  666. }
  667. if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
  668. input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
  669. aiptek->lastMacro = -1;
  670. }
  671. if (macro != -1 && macro != aiptek->lastMacro) {
  672. input_report_key(inputdev, macroKeyEvents[macro], 1);
  673. aiptek->lastMacro = macro;
  674. }
  675. input_report_abs(inputdev, ABS_MISC,
  676. p | AIPTEK_REPORT_TOOL_MOUSE);
  677. input_sync(inputdev);
  678. }
  679. /* We have no idea which tool can generate a report 6. Theoretically,
  680. * neither need to, having been given reports 4 & 5 for such use.
  681. * However, report 6 is the 'official-looking' report for macroKeys;
  682. * reports 4 & 5 supposively are used to support unnamed, unknown
  683. * hat switches (which just so happen to be the macroKeys.)
  684. */
  685. else if (data[0] == 6) {
  686. macro = get_unaligned_le16(data + 1);
  687. if (macro > 0) {
  688. input_report_key(inputdev, macroKeyEvents[macro - 1],
  689. 0);
  690. }
  691. if (macro < 25) {
  692. input_report_key(inputdev, macroKeyEvents[macro + 1],
  693. 0);
  694. }
  695. /* If the selected tool changed, reset the old
  696. tool key, and set the new one.
  697. */
  698. if (aiptek->previousToolMode !=
  699. aiptek->curSetting.toolMode) {
  700. input_report_key(inputdev,
  701. aiptek->previousToolMode, 0);
  702. input_report_key(inputdev,
  703. aiptek->curSetting.toolMode,
  704. 1);
  705. aiptek->previousToolMode =
  706. aiptek->curSetting.toolMode;
  707. }
  708. input_report_key(inputdev, macroKeyEvents[macro], 1);
  709. input_report_abs(inputdev, ABS_MISC,
  710. 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
  711. input_sync(inputdev);
  712. } else {
  713. dev_dbg(&intf->dev, "Unknown report %d\n", data[0]);
  714. }
  715. /* Jitter may occur when the user presses a button on the stlyus
  716. * or the mouse. What we do to prevent that is wait 'x' milliseconds
  717. * following a 'jitterable' event, which should give the hand some time
  718. * stabilize itself.
  719. *
  720. * We just introduced aiptek->previousJitterable to carry forth the
  721. * notion that jitter occurs when the button state changes from on to off:
  722. * a person drawing, holding a button down is not subject to jittering.
  723. * With that in mind, changing from upper button depressed to lower button
  724. * WILL transition through a jitter delay.
  725. */
  726. if (aiptek->previousJitterable != jitterable &&
  727. aiptek->curSetting.jitterDelay != 0 && aiptek->inDelay != 1) {
  728. aiptek->endDelay = jiffies +
  729. ((aiptek->curSetting.jitterDelay * HZ) / 1000);
  730. aiptek->inDelay = 1;
  731. }
  732. aiptek->previousJitterable = jitterable;
  733. exit:
  734. retval = usb_submit_urb(urb, GFP_ATOMIC);
  735. if (retval != 0) {
  736. dev_err(&intf->dev,
  737. "%s - usb_submit_urb failed with result %d\n",
  738. __func__, retval);
  739. }
  740. }
  741. /***********************************************************************
  742. * These are the USB id's known so far. We do not identify them to
  743. * specific Aiptek model numbers, because there has been overlaps,
  744. * use, and reuse of id's in existing models. Certain models have
  745. * been known to use more than one ID, indicative perhaps of
  746. * manufacturing revisions. In any event, we consider these
  747. * IDs to not be model-specific nor unique.
  748. */
  749. static const struct usb_device_id aiptek_ids[] = {
  750. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x01)},
  751. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x10)},
  752. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20)},
  753. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x21)},
  754. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x22)},
  755. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x23)},
  756. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x24)},
  757. {USB_DEVICE(USB_VENDOR_ID_KYE, 0x5003)},
  758. {}
  759. };
  760. MODULE_DEVICE_TABLE(usb, aiptek_ids);
  761. /***********************************************************************
  762. * Open an instance of the tablet driver.
  763. */
  764. static int aiptek_open(struct input_dev *inputdev)
  765. {
  766. struct aiptek *aiptek = input_get_drvdata(inputdev);
  767. aiptek->urb->dev = interface_to_usbdev(aiptek->intf);
  768. if (usb_submit_urb(aiptek->urb, GFP_KERNEL) != 0)
  769. return -EIO;
  770. return 0;
  771. }
  772. /***********************************************************************
  773. * Close an instance of the tablet driver.
  774. */
  775. static void aiptek_close(struct input_dev *inputdev)
  776. {
  777. struct aiptek *aiptek = input_get_drvdata(inputdev);
  778. usb_kill_urb(aiptek->urb);
  779. }
  780. /***********************************************************************
  781. * aiptek_set_report and aiptek_get_report() are borrowed from Linux 2.4.x,
  782. * where they were known as usb_set_report and usb_get_report.
  783. */
  784. static int
  785. aiptek_set_report(struct aiptek *aiptek,
  786. unsigned char report_type,
  787. unsigned char report_id, void *buffer, int size)
  788. {
  789. struct usb_device *udev = interface_to_usbdev(aiptek->intf);
  790. return usb_control_msg(udev,
  791. usb_sndctrlpipe(udev, 0),
  792. USB_REQ_SET_REPORT,
  793. USB_TYPE_CLASS | USB_RECIP_INTERFACE |
  794. USB_DIR_OUT, (report_type << 8) + report_id,
  795. aiptek->ifnum, buffer, size, 5000);
  796. }
  797. static int
  798. aiptek_get_report(struct aiptek *aiptek,
  799. unsigned char report_type,
  800. unsigned char report_id, void *buffer, int size)
  801. {
  802. struct usb_device *udev = interface_to_usbdev(aiptek->intf);
  803. return usb_control_msg(udev,
  804. usb_rcvctrlpipe(udev, 0),
  805. USB_REQ_GET_REPORT,
  806. USB_TYPE_CLASS | USB_RECIP_INTERFACE |
  807. USB_DIR_IN, (report_type << 8) + report_id,
  808. aiptek->ifnum, buffer, size, 5000);
  809. }
  810. /***********************************************************************
  811. * Send a command to the tablet.
  812. */
  813. static int
  814. aiptek_command(struct aiptek *aiptek, unsigned char command, unsigned char data)
  815. {
  816. const int sizeof_buf = 3 * sizeof(u8);
  817. int ret;
  818. u8 *buf;
  819. buf = kmalloc(sizeof_buf, GFP_KERNEL);
  820. if (!buf)
  821. return -ENOMEM;
  822. buf[0] = 2;
  823. buf[1] = command;
  824. buf[2] = data;
  825. if ((ret =
  826. aiptek_set_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
  827. dev_dbg(&aiptek->intf->dev,
  828. "aiptek_program: failed, tried to send: 0x%02x 0x%02x\n",
  829. command, data);
  830. }
  831. kfree(buf);
  832. return ret < 0 ? ret : 0;
  833. }
  834. /***********************************************************************
  835. * Retrieve information from the tablet. Querying info is defined as first
  836. * sending the {command,data} sequence as a command, followed by a wait
  837. * (aka, "programmaticDelay") and then a "read" request.
  838. */
  839. static int
  840. aiptek_query(struct aiptek *aiptek, unsigned char command, unsigned char data)
  841. {
  842. const int sizeof_buf = 3 * sizeof(u8);
  843. int ret;
  844. u8 *buf;
  845. buf = kmalloc(sizeof_buf, GFP_KERNEL);
  846. if (!buf)
  847. return -ENOMEM;
  848. buf[0] = 2;
  849. buf[1] = command;
  850. buf[2] = data;
  851. if (aiptek_command(aiptek, command, data) != 0) {
  852. kfree(buf);
  853. return -EIO;
  854. }
  855. msleep(aiptek->curSetting.programmableDelay);
  856. if ((ret =
  857. aiptek_get_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
  858. dev_dbg(&aiptek->intf->dev,
  859. "aiptek_query failed: returned 0x%02x 0x%02x 0x%02x\n",
  860. buf[0], buf[1], buf[2]);
  861. ret = -EIO;
  862. } else {
  863. ret = get_unaligned_le16(buf + 1);
  864. }
  865. kfree(buf);
  866. return ret;
  867. }
  868. /***********************************************************************
  869. * Program the tablet into either absolute or relative mode.
  870. * We also get information about the tablet's size.
  871. */
  872. static int aiptek_program_tablet(struct aiptek *aiptek)
  873. {
  874. int ret;
  875. /* Execute Resolution500LPI */
  876. if ((ret = aiptek_command(aiptek, 0x18, 0x04)) < 0)
  877. return ret;
  878. /* Query getModelCode */
  879. if ((ret = aiptek_query(aiptek, 0x02, 0x00)) < 0)
  880. return ret;
  881. aiptek->features.modelCode = ret & 0xff;
  882. /* Query getODMCode */
  883. if ((ret = aiptek_query(aiptek, 0x03, 0x00)) < 0)
  884. return ret;
  885. aiptek->features.odmCode = ret;
  886. /* Query getFirmwareCode */
  887. if ((ret = aiptek_query(aiptek, 0x04, 0x00)) < 0)
  888. return ret;
  889. aiptek->features.firmwareCode = ret;
  890. /* Query getXextension */
  891. if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
  892. return ret;
  893. input_set_abs_params(aiptek->inputdev, ABS_X, 0, ret - 1, 0, 0);
  894. /* Query getYextension */
  895. if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
  896. return ret;
  897. input_set_abs_params(aiptek->inputdev, ABS_Y, 0, ret - 1, 0, 0);
  898. /* Query getPressureLevels */
  899. if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
  900. return ret;
  901. input_set_abs_params(aiptek->inputdev, ABS_PRESSURE, 0, ret - 1, 0, 0);
  902. /* Depending on whether we are in absolute or relative mode, we will
  903. * do a switchToTablet(absolute) or switchToMouse(relative) command.
  904. */
  905. if (aiptek->curSetting.coordinateMode ==
  906. AIPTEK_COORDINATE_ABSOLUTE_MODE) {
  907. /* Execute switchToTablet */
  908. if ((ret = aiptek_command(aiptek, 0x10, 0x01)) < 0) {
  909. return ret;
  910. }
  911. } else {
  912. /* Execute switchToMouse */
  913. if ((ret = aiptek_command(aiptek, 0x10, 0x00)) < 0) {
  914. return ret;
  915. }
  916. }
  917. /* Enable the macro keys */
  918. if ((ret = aiptek_command(aiptek, 0x11, 0x02)) < 0)
  919. return ret;
  920. #if 0
  921. /* Execute FilterOn */
  922. if ((ret = aiptek_command(aiptek, 0x17, 0x00)) < 0)
  923. return ret;
  924. #endif
  925. /* Execute AutoGainOn */
  926. if ((ret = aiptek_command(aiptek, 0x12, 0xff)) < 0)
  927. return ret;
  928. /* Reset the eventCount, so we track events from last (re)programming
  929. */
  930. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
  931. aiptek->eventCount = 0;
  932. return 0;
  933. }
  934. /***********************************************************************
  935. * Sysfs functions. Sysfs prefers that individually-tunable parameters
  936. * exist in their separate pseudo-files. Summary data that is immutable
  937. * may exist in a singular file so long as you don't define a writeable
  938. * interface.
  939. */
  940. /***********************************************************************
  941. * support the 'size' file -- display support
  942. */
  943. static ssize_t show_tabletSize(struct device *dev, struct device_attribute *attr, char *buf)
  944. {
  945. struct aiptek *aiptek = dev_get_drvdata(dev);
  946. return snprintf(buf, PAGE_SIZE, "%dx%d\n",
  947. input_abs_get_max(aiptek->inputdev, ABS_X) + 1,
  948. input_abs_get_max(aiptek->inputdev, ABS_Y) + 1);
  949. }
  950. /* These structs define the sysfs files, param #1 is the name of the
  951. * file, param 2 is the file permissions, param 3 & 4 are to the
  952. * output generator and input parser routines. Absence of a routine is
  953. * permitted -- it only means can't either 'cat' the file, or send data
  954. * to it.
  955. */
  956. static DEVICE_ATTR(size, S_IRUGO, show_tabletSize, NULL);
  957. /***********************************************************************
  958. * support routines for the 'pointer_mode' file. Note that this file
  959. * both displays current setting and allows reprogramming.
  960. */
  961. static struct aiptek_map pointer_mode_map[] = {
  962. { "stylus", AIPTEK_POINTER_ONLY_STYLUS_MODE },
  963. { "mouse", AIPTEK_POINTER_ONLY_MOUSE_MODE },
  964. { "either", AIPTEK_POINTER_EITHER_MODE },
  965. { NULL, AIPTEK_INVALID_VALUE }
  966. };
  967. static ssize_t show_tabletPointerMode(struct device *dev, struct device_attribute *attr, char *buf)
  968. {
  969. struct aiptek *aiptek = dev_get_drvdata(dev);
  970. return snprintf(buf, PAGE_SIZE, "%s\n",
  971. map_val_to_str(pointer_mode_map,
  972. aiptek->curSetting.pointerMode));
  973. }
  974. static ssize_t
  975. store_tabletPointerMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  976. {
  977. struct aiptek *aiptek = dev_get_drvdata(dev);
  978. int new_mode = map_str_to_val(pointer_mode_map, buf, count);
  979. if (new_mode == AIPTEK_INVALID_VALUE)
  980. return -EINVAL;
  981. aiptek->newSetting.pointerMode = new_mode;
  982. return count;
  983. }
  984. static DEVICE_ATTR(pointer_mode,
  985. S_IRUGO | S_IWUSR,
  986. show_tabletPointerMode, store_tabletPointerMode);
  987. /***********************************************************************
  988. * support routines for the 'coordinate_mode' file. Note that this file
  989. * both displays current setting and allows reprogramming.
  990. */
  991. static struct aiptek_map coordinate_mode_map[] = {
  992. { "absolute", AIPTEK_COORDINATE_ABSOLUTE_MODE },
  993. { "relative", AIPTEK_COORDINATE_RELATIVE_MODE },
  994. { NULL, AIPTEK_INVALID_VALUE }
  995. };
  996. static ssize_t show_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, char *buf)
  997. {
  998. struct aiptek *aiptek = dev_get_drvdata(dev);
  999. return snprintf(buf, PAGE_SIZE, "%s\n",
  1000. map_val_to_str(coordinate_mode_map,
  1001. aiptek->curSetting.coordinateMode));
  1002. }
  1003. static ssize_t
  1004. store_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1005. {
  1006. struct aiptek *aiptek = dev_get_drvdata(dev);
  1007. int new_mode = map_str_to_val(coordinate_mode_map, buf, count);
  1008. if (new_mode == AIPTEK_INVALID_VALUE)
  1009. return -EINVAL;
  1010. aiptek->newSetting.coordinateMode = new_mode;
  1011. return count;
  1012. }
  1013. static DEVICE_ATTR(coordinate_mode,
  1014. S_IRUGO | S_IWUSR,
  1015. show_tabletCoordinateMode, store_tabletCoordinateMode);
  1016. /***********************************************************************
  1017. * support routines for the 'tool_mode' file. Note that this file
  1018. * both displays current setting and allows reprogramming.
  1019. */
  1020. static struct aiptek_map tool_mode_map[] = {
  1021. { "mouse", AIPTEK_TOOL_BUTTON_MOUSE_MODE },
  1022. { "eraser", AIPTEK_TOOL_BUTTON_ERASER_MODE },
  1023. { "pencil", AIPTEK_TOOL_BUTTON_PENCIL_MODE },
  1024. { "pen", AIPTEK_TOOL_BUTTON_PEN_MODE },
  1025. { "brush", AIPTEK_TOOL_BUTTON_BRUSH_MODE },
  1026. { "airbrush", AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE },
  1027. { "lens", AIPTEK_TOOL_BUTTON_LENS_MODE },
  1028. { NULL, AIPTEK_INVALID_VALUE }
  1029. };
  1030. static ssize_t show_tabletToolMode(struct device *dev, struct device_attribute *attr, char *buf)
  1031. {
  1032. struct aiptek *aiptek = dev_get_drvdata(dev);
  1033. return snprintf(buf, PAGE_SIZE, "%s\n",
  1034. map_val_to_str(tool_mode_map,
  1035. aiptek->curSetting.toolMode));
  1036. }
  1037. static ssize_t
  1038. store_tabletToolMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1039. {
  1040. struct aiptek *aiptek = dev_get_drvdata(dev);
  1041. int new_mode = map_str_to_val(tool_mode_map, buf, count);
  1042. if (new_mode == AIPTEK_INVALID_VALUE)
  1043. return -EINVAL;
  1044. aiptek->newSetting.toolMode = new_mode;
  1045. return count;
  1046. }
  1047. static DEVICE_ATTR(tool_mode,
  1048. S_IRUGO | S_IWUSR,
  1049. show_tabletToolMode, store_tabletToolMode);
  1050. /***********************************************************************
  1051. * support routines for the 'xtilt' file. Note that this file
  1052. * both displays current setting and allows reprogramming.
  1053. */
  1054. static ssize_t show_tabletXtilt(struct device *dev, struct device_attribute *attr, char *buf)
  1055. {
  1056. struct aiptek *aiptek = dev_get_drvdata(dev);
  1057. if (aiptek->curSetting.xTilt == AIPTEK_TILT_DISABLE) {
  1058. return snprintf(buf, PAGE_SIZE, "disable\n");
  1059. } else {
  1060. return snprintf(buf, PAGE_SIZE, "%d\n",
  1061. aiptek->curSetting.xTilt);
  1062. }
  1063. }
  1064. static ssize_t
  1065. store_tabletXtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1066. {
  1067. struct aiptek *aiptek = dev_get_drvdata(dev);
  1068. int x;
  1069. if (kstrtoint(buf, 10, &x)) {
  1070. size_t len = buf[count - 1] == '\n' ? count - 1 : count;
  1071. if (strncmp(buf, "disable", len))
  1072. return -EINVAL;
  1073. aiptek->newSetting.xTilt = AIPTEK_TILT_DISABLE;
  1074. } else {
  1075. if (x < AIPTEK_TILT_MIN || x > AIPTEK_TILT_MAX)
  1076. return -EINVAL;
  1077. aiptek->newSetting.xTilt = x;
  1078. }
  1079. return count;
  1080. }
  1081. static DEVICE_ATTR(xtilt,
  1082. S_IRUGO | S_IWUSR, show_tabletXtilt, store_tabletXtilt);
  1083. /***********************************************************************
  1084. * support routines for the 'ytilt' file. Note that this file
  1085. * both displays current setting and allows reprogramming.
  1086. */
  1087. static ssize_t show_tabletYtilt(struct device *dev, struct device_attribute *attr, char *buf)
  1088. {
  1089. struct aiptek *aiptek = dev_get_drvdata(dev);
  1090. if (aiptek->curSetting.yTilt == AIPTEK_TILT_DISABLE) {
  1091. return snprintf(buf, PAGE_SIZE, "disable\n");
  1092. } else {
  1093. return snprintf(buf, PAGE_SIZE, "%d\n",
  1094. aiptek->curSetting.yTilt);
  1095. }
  1096. }
  1097. static ssize_t
  1098. store_tabletYtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1099. {
  1100. struct aiptek *aiptek = dev_get_drvdata(dev);
  1101. int y;
  1102. if (kstrtoint(buf, 10, &y)) {
  1103. size_t len = buf[count - 1] == '\n' ? count - 1 : count;
  1104. if (strncmp(buf, "disable", len))
  1105. return -EINVAL;
  1106. aiptek->newSetting.yTilt = AIPTEK_TILT_DISABLE;
  1107. } else {
  1108. if (y < AIPTEK_TILT_MIN || y > AIPTEK_TILT_MAX)
  1109. return -EINVAL;
  1110. aiptek->newSetting.yTilt = y;
  1111. }
  1112. return count;
  1113. }
  1114. static DEVICE_ATTR(ytilt,
  1115. S_IRUGO | S_IWUSR, show_tabletYtilt, store_tabletYtilt);
  1116. /***********************************************************************
  1117. * support routines for the 'jitter' file. Note that this file
  1118. * both displays current setting and allows reprogramming.
  1119. */
  1120. static ssize_t show_tabletJitterDelay(struct device *dev, struct device_attribute *attr, char *buf)
  1121. {
  1122. struct aiptek *aiptek = dev_get_drvdata(dev);
  1123. return snprintf(buf, PAGE_SIZE, "%d\n", aiptek->curSetting.jitterDelay);
  1124. }
  1125. static ssize_t
  1126. store_tabletJitterDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1127. {
  1128. struct aiptek *aiptek = dev_get_drvdata(dev);
  1129. int err, j;
  1130. err = kstrtoint(buf, 10, &j);
  1131. if (err)
  1132. return err;
  1133. aiptek->newSetting.jitterDelay = j;
  1134. return count;
  1135. }
  1136. static DEVICE_ATTR(jitter,
  1137. S_IRUGO | S_IWUSR,
  1138. show_tabletJitterDelay, store_tabletJitterDelay);
  1139. /***********************************************************************
  1140. * support routines for the 'delay' file. Note that this file
  1141. * both displays current setting and allows reprogramming.
  1142. */
  1143. static ssize_t show_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, char *buf)
  1144. {
  1145. struct aiptek *aiptek = dev_get_drvdata(dev);
  1146. return snprintf(buf, PAGE_SIZE, "%d\n",
  1147. aiptek->curSetting.programmableDelay);
  1148. }
  1149. static ssize_t
  1150. store_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1151. {
  1152. struct aiptek *aiptek = dev_get_drvdata(dev);
  1153. int err, d;
  1154. err = kstrtoint(buf, 10, &d);
  1155. if (err)
  1156. return err;
  1157. aiptek->newSetting.programmableDelay = d;
  1158. return count;
  1159. }
  1160. static DEVICE_ATTR(delay,
  1161. S_IRUGO | S_IWUSR,
  1162. show_tabletProgrammableDelay, store_tabletProgrammableDelay);
  1163. /***********************************************************************
  1164. * support routines for the 'event_count' file. Note that this file
  1165. * only displays current setting.
  1166. */
  1167. static ssize_t show_tabletEventsReceived(struct device *dev, struct device_attribute *attr, char *buf)
  1168. {
  1169. struct aiptek *aiptek = dev_get_drvdata(dev);
  1170. return snprintf(buf, PAGE_SIZE, "%ld\n", aiptek->eventCount);
  1171. }
  1172. static DEVICE_ATTR(event_count, S_IRUGO, show_tabletEventsReceived, NULL);
  1173. /***********************************************************************
  1174. * support routines for the 'diagnostic' file. Note that this file
  1175. * only displays current setting.
  1176. */
  1177. static ssize_t show_tabletDiagnosticMessage(struct device *dev, struct device_attribute *attr, char *buf)
  1178. {
  1179. struct aiptek *aiptek = dev_get_drvdata(dev);
  1180. char *retMsg;
  1181. switch (aiptek->diagnostic) {
  1182. case AIPTEK_DIAGNOSTIC_NA:
  1183. retMsg = "no errors\n";
  1184. break;
  1185. case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
  1186. retMsg = "Error: receiving relative reports\n";
  1187. break;
  1188. case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
  1189. retMsg = "Error: receiving absolute reports\n";
  1190. break;
  1191. case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
  1192. if (aiptek->curSetting.pointerMode ==
  1193. AIPTEK_POINTER_ONLY_MOUSE_MODE) {
  1194. retMsg = "Error: receiving stylus reports\n";
  1195. } else {
  1196. retMsg = "Error: receiving mouse reports\n";
  1197. }
  1198. break;
  1199. default:
  1200. return 0;
  1201. }
  1202. return snprintf(buf, PAGE_SIZE, retMsg);
  1203. }
  1204. static DEVICE_ATTR(diagnostic, S_IRUGO, show_tabletDiagnosticMessage, NULL);
  1205. /***********************************************************************
  1206. * support routines for the 'stylus_upper' file. Note that this file
  1207. * both displays current setting and allows for setting changing.
  1208. */
  1209. static struct aiptek_map stylus_button_map[] = {
  1210. { "upper", AIPTEK_STYLUS_UPPER_BUTTON },
  1211. { "lower", AIPTEK_STYLUS_LOWER_BUTTON },
  1212. { NULL, AIPTEK_INVALID_VALUE }
  1213. };
  1214. static ssize_t show_tabletStylusUpper(struct device *dev, struct device_attribute *attr, char *buf)
  1215. {
  1216. struct aiptek *aiptek = dev_get_drvdata(dev);
  1217. return snprintf(buf, PAGE_SIZE, "%s\n",
  1218. map_val_to_str(stylus_button_map,
  1219. aiptek->curSetting.stylusButtonUpper));
  1220. }
  1221. static ssize_t
  1222. store_tabletStylusUpper(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1223. {
  1224. struct aiptek *aiptek = dev_get_drvdata(dev);
  1225. int new_button = map_str_to_val(stylus_button_map, buf, count);
  1226. if (new_button == AIPTEK_INVALID_VALUE)
  1227. return -EINVAL;
  1228. aiptek->newSetting.stylusButtonUpper = new_button;
  1229. return count;
  1230. }
  1231. static DEVICE_ATTR(stylus_upper,
  1232. S_IRUGO | S_IWUSR,
  1233. show_tabletStylusUpper, store_tabletStylusUpper);
  1234. /***********************************************************************
  1235. * support routines for the 'stylus_lower' file. Note that this file
  1236. * both displays current setting and allows for setting changing.
  1237. */
  1238. static ssize_t show_tabletStylusLower(struct device *dev, struct device_attribute *attr, char *buf)
  1239. {
  1240. struct aiptek *aiptek = dev_get_drvdata(dev);
  1241. return snprintf(buf, PAGE_SIZE, "%s\n",
  1242. map_val_to_str(stylus_button_map,
  1243. aiptek->curSetting.stylusButtonLower));
  1244. }
  1245. static ssize_t
  1246. store_tabletStylusLower(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1247. {
  1248. struct aiptek *aiptek = dev_get_drvdata(dev);
  1249. int new_button = map_str_to_val(stylus_button_map, buf, count);
  1250. if (new_button == AIPTEK_INVALID_VALUE)
  1251. return -EINVAL;
  1252. aiptek->newSetting.stylusButtonLower = new_button;
  1253. return count;
  1254. }
  1255. static DEVICE_ATTR(stylus_lower,
  1256. S_IRUGO | S_IWUSR,
  1257. show_tabletStylusLower, store_tabletStylusLower);
  1258. /***********************************************************************
  1259. * support routines for the 'mouse_left' file. Note that this file
  1260. * both displays current setting and allows for setting changing.
  1261. */
  1262. static struct aiptek_map mouse_button_map[] = {
  1263. { "left", AIPTEK_MOUSE_LEFT_BUTTON },
  1264. { "middle", AIPTEK_MOUSE_MIDDLE_BUTTON },
  1265. { "right", AIPTEK_MOUSE_RIGHT_BUTTON },
  1266. { NULL, AIPTEK_INVALID_VALUE }
  1267. };
  1268. static ssize_t show_tabletMouseLeft(struct device *dev, struct device_attribute *attr, char *buf)
  1269. {
  1270. struct aiptek *aiptek = dev_get_drvdata(dev);
  1271. return snprintf(buf, PAGE_SIZE, "%s\n",
  1272. map_val_to_str(mouse_button_map,
  1273. aiptek->curSetting.mouseButtonLeft));
  1274. }
  1275. static ssize_t
  1276. store_tabletMouseLeft(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1277. {
  1278. struct aiptek *aiptek = dev_get_drvdata(dev);
  1279. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1280. if (new_button == AIPTEK_INVALID_VALUE)
  1281. return -EINVAL;
  1282. aiptek->newSetting.mouseButtonLeft = new_button;
  1283. return count;
  1284. }
  1285. static DEVICE_ATTR(mouse_left,
  1286. S_IRUGO | S_IWUSR,
  1287. show_tabletMouseLeft, store_tabletMouseLeft);
  1288. /***********************************************************************
  1289. * support routines for the 'mouse_middle' file. Note that this file
  1290. * both displays current setting and allows for setting changing.
  1291. */
  1292. static ssize_t show_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, char *buf)
  1293. {
  1294. struct aiptek *aiptek = dev_get_drvdata(dev);
  1295. return snprintf(buf, PAGE_SIZE, "%s\n",
  1296. map_val_to_str(mouse_button_map,
  1297. aiptek->curSetting.mouseButtonMiddle));
  1298. }
  1299. static ssize_t
  1300. store_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1301. {
  1302. struct aiptek *aiptek = dev_get_drvdata(dev);
  1303. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1304. if (new_button == AIPTEK_INVALID_VALUE)
  1305. return -EINVAL;
  1306. aiptek->newSetting.mouseButtonMiddle = new_button;
  1307. return count;
  1308. }
  1309. static DEVICE_ATTR(mouse_middle,
  1310. S_IRUGO | S_IWUSR,
  1311. show_tabletMouseMiddle, store_tabletMouseMiddle);
  1312. /***********************************************************************
  1313. * support routines for the 'mouse_right' file. Note that this file
  1314. * both displays current setting and allows for setting changing.
  1315. */
  1316. static ssize_t show_tabletMouseRight(struct device *dev, struct device_attribute *attr, char *buf)
  1317. {
  1318. struct aiptek *aiptek = dev_get_drvdata(dev);
  1319. return snprintf(buf, PAGE_SIZE, "%s\n",
  1320. map_val_to_str(mouse_button_map,
  1321. aiptek->curSetting.mouseButtonRight));
  1322. }
  1323. static ssize_t
  1324. store_tabletMouseRight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1325. {
  1326. struct aiptek *aiptek = dev_get_drvdata(dev);
  1327. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1328. if (new_button == AIPTEK_INVALID_VALUE)
  1329. return -EINVAL;
  1330. aiptek->newSetting.mouseButtonRight = new_button;
  1331. return count;
  1332. }
  1333. static DEVICE_ATTR(mouse_right,
  1334. S_IRUGO | S_IWUSR,
  1335. show_tabletMouseRight, store_tabletMouseRight);
  1336. /***********************************************************************
  1337. * support routines for the 'wheel' file. Note that this file
  1338. * both displays current setting and allows for setting changing.
  1339. */
  1340. static ssize_t show_tabletWheel(struct device *dev, struct device_attribute *attr, char *buf)
  1341. {
  1342. struct aiptek *aiptek = dev_get_drvdata(dev);
  1343. if (aiptek->curSetting.wheel == AIPTEK_WHEEL_DISABLE) {
  1344. return snprintf(buf, PAGE_SIZE, "disable\n");
  1345. } else {
  1346. return snprintf(buf, PAGE_SIZE, "%d\n",
  1347. aiptek->curSetting.wheel);
  1348. }
  1349. }
  1350. static ssize_t
  1351. store_tabletWheel(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1352. {
  1353. struct aiptek *aiptek = dev_get_drvdata(dev);
  1354. int err, w;
  1355. err = kstrtoint(buf, 10, &w);
  1356. if (err)
  1357. return err;
  1358. aiptek->newSetting.wheel = w;
  1359. return count;
  1360. }
  1361. static DEVICE_ATTR(wheel,
  1362. S_IRUGO | S_IWUSR, show_tabletWheel, store_tabletWheel);
  1363. /***********************************************************************
  1364. * support routines for the 'execute' file. Note that this file
  1365. * both displays current setting and allows for setting changing.
  1366. */
  1367. static ssize_t show_tabletExecute(struct device *dev, struct device_attribute *attr, char *buf)
  1368. {
  1369. /* There is nothing useful to display, so a one-line manual
  1370. * is in order...
  1371. */
  1372. return snprintf(buf, PAGE_SIZE,
  1373. "Write anything to this file to program your tablet.\n");
  1374. }
  1375. static ssize_t
  1376. store_tabletExecute(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1377. {
  1378. struct aiptek *aiptek = dev_get_drvdata(dev);
  1379. /* We do not care what you write to this file. Merely the action
  1380. * of writing to this file triggers a tablet reprogramming.
  1381. */
  1382. memcpy(&aiptek->curSetting, &aiptek->newSetting,
  1383. sizeof(struct aiptek_settings));
  1384. if (aiptek_program_tablet(aiptek) < 0)
  1385. return -EIO;
  1386. return count;
  1387. }
  1388. static DEVICE_ATTR(execute,
  1389. S_IRUGO | S_IWUSR, show_tabletExecute, store_tabletExecute);
  1390. /***********************************************************************
  1391. * support routines for the 'odm_code' file. Note that this file
  1392. * only displays current setting.
  1393. */
  1394. static ssize_t show_tabletODMCode(struct device *dev, struct device_attribute *attr, char *buf)
  1395. {
  1396. struct aiptek *aiptek = dev_get_drvdata(dev);
  1397. return snprintf(buf, PAGE_SIZE, "0x%04x\n", aiptek->features.odmCode);
  1398. }
  1399. static DEVICE_ATTR(odm_code, S_IRUGO, show_tabletODMCode, NULL);
  1400. /***********************************************************************
  1401. * support routines for the 'model_code' file. Note that this file
  1402. * only displays current setting.
  1403. */
  1404. static ssize_t show_tabletModelCode(struct device *dev, struct device_attribute *attr, char *buf)
  1405. {
  1406. struct aiptek *aiptek = dev_get_drvdata(dev);
  1407. return snprintf(buf, PAGE_SIZE, "0x%04x\n", aiptek->features.modelCode);
  1408. }
  1409. static DEVICE_ATTR(model_code, S_IRUGO, show_tabletModelCode, NULL);
  1410. /***********************************************************************
  1411. * support routines for the 'firmware_code' file. Note that this file
  1412. * only displays current setting.
  1413. */
  1414. static ssize_t show_firmwareCode(struct device *dev, struct device_attribute *attr, char *buf)
  1415. {
  1416. struct aiptek *aiptek = dev_get_drvdata(dev);
  1417. return snprintf(buf, PAGE_SIZE, "%04x\n",
  1418. aiptek->features.firmwareCode);
  1419. }
  1420. static DEVICE_ATTR(firmware_code, S_IRUGO, show_firmwareCode, NULL);
  1421. static struct attribute *aiptek_attributes[] = {
  1422. &dev_attr_size.attr,
  1423. &dev_attr_pointer_mode.attr,
  1424. &dev_attr_coordinate_mode.attr,
  1425. &dev_attr_tool_mode.attr,
  1426. &dev_attr_xtilt.attr,
  1427. &dev_attr_ytilt.attr,
  1428. &dev_attr_jitter.attr,
  1429. &dev_attr_delay.attr,
  1430. &dev_attr_event_count.attr,
  1431. &dev_attr_diagnostic.attr,
  1432. &dev_attr_odm_code.attr,
  1433. &dev_attr_model_code.attr,
  1434. &dev_attr_firmware_code.attr,
  1435. &dev_attr_stylus_lower.attr,
  1436. &dev_attr_stylus_upper.attr,
  1437. &dev_attr_mouse_left.attr,
  1438. &dev_attr_mouse_middle.attr,
  1439. &dev_attr_mouse_right.attr,
  1440. &dev_attr_wheel.attr,
  1441. &dev_attr_execute.attr,
  1442. NULL
  1443. };
  1444. static struct attribute_group aiptek_attribute_group = {
  1445. .attrs = aiptek_attributes,
  1446. };
  1447. /***********************************************************************
  1448. * This routine is called when a tablet has been identified. It basically
  1449. * sets up the tablet and the driver's internal structures.
  1450. */
  1451. static int
  1452. aiptek_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1453. {
  1454. struct usb_device *usbdev = interface_to_usbdev(intf);
  1455. struct usb_endpoint_descriptor *endpoint;
  1456. struct aiptek *aiptek;
  1457. struct input_dev *inputdev;
  1458. int i;
  1459. int speeds[] = { 0,
  1460. AIPTEK_PROGRAMMABLE_DELAY_50,
  1461. AIPTEK_PROGRAMMABLE_DELAY_400,
  1462. AIPTEK_PROGRAMMABLE_DELAY_25,
  1463. AIPTEK_PROGRAMMABLE_DELAY_100,
  1464. AIPTEK_PROGRAMMABLE_DELAY_200,
  1465. AIPTEK_PROGRAMMABLE_DELAY_300
  1466. };
  1467. int err = -ENOMEM;
  1468. /* programmableDelay is where the command-line specified
  1469. * delay is kept. We make it the first element of speeds[],
  1470. * so therefore, your override speed is tried first, then the
  1471. * remainder. Note that the default value of 400ms will be tried
  1472. * if you do not specify any command line parameter.
  1473. */
  1474. speeds[0] = programmableDelay;
  1475. aiptek = kzalloc(sizeof(struct aiptek), GFP_KERNEL);
  1476. inputdev = input_allocate_device();
  1477. if (!aiptek || !inputdev) {
  1478. dev_warn(&intf->dev,
  1479. "cannot allocate memory or input device\n");
  1480. goto fail1;
  1481. }
  1482. aiptek->data = usb_alloc_coherent(usbdev, AIPTEK_PACKET_LENGTH,
  1483. GFP_ATOMIC, &aiptek->data_dma);
  1484. if (!aiptek->data) {
  1485. dev_warn(&intf->dev, "cannot allocate usb buffer\n");
  1486. goto fail1;
  1487. }
  1488. aiptek->urb = usb_alloc_urb(0, GFP_KERNEL);
  1489. if (!aiptek->urb) {
  1490. dev_warn(&intf->dev, "cannot allocate urb\n");
  1491. goto fail2;
  1492. }
  1493. aiptek->inputdev = inputdev;
  1494. aiptek->intf = intf;
  1495. aiptek->ifnum = intf->altsetting[0].desc.bInterfaceNumber;
  1496. aiptek->inDelay = 0;
  1497. aiptek->endDelay = 0;
  1498. aiptek->previousJitterable = 0;
  1499. aiptek->lastMacro = -1;
  1500. /* Set up the curSettings struct. Said struct contains the current
  1501. * programmable parameters. The newSetting struct contains changes
  1502. * the user makes to the settings via the sysfs interface. Those
  1503. * changes are not "committed" to curSettings until the user
  1504. * writes to the sysfs/.../execute file.
  1505. */
  1506. aiptek->curSetting.pointerMode = AIPTEK_POINTER_EITHER_MODE;
  1507. aiptek->curSetting.coordinateMode = AIPTEK_COORDINATE_ABSOLUTE_MODE;
  1508. aiptek->curSetting.toolMode = AIPTEK_TOOL_BUTTON_PEN_MODE;
  1509. aiptek->curSetting.xTilt = AIPTEK_TILT_DISABLE;
  1510. aiptek->curSetting.yTilt = AIPTEK_TILT_DISABLE;
  1511. aiptek->curSetting.mouseButtonLeft = AIPTEK_MOUSE_LEFT_BUTTON;
  1512. aiptek->curSetting.mouseButtonMiddle = AIPTEK_MOUSE_MIDDLE_BUTTON;
  1513. aiptek->curSetting.mouseButtonRight = AIPTEK_MOUSE_RIGHT_BUTTON;
  1514. aiptek->curSetting.stylusButtonUpper = AIPTEK_STYLUS_UPPER_BUTTON;
  1515. aiptek->curSetting.stylusButtonLower = AIPTEK_STYLUS_LOWER_BUTTON;
  1516. aiptek->curSetting.jitterDelay = jitterDelay;
  1517. aiptek->curSetting.programmableDelay = programmableDelay;
  1518. /* Both structs should have equivalent settings
  1519. */
  1520. aiptek->newSetting = aiptek->curSetting;
  1521. /* Determine the usb devices' physical path.
  1522. * Asketh not why we always pretend we're using "../input0",
  1523. * but I suspect this will have to be refactored one
  1524. * day if a single USB device can be a keyboard & a mouse
  1525. * & a tablet, and the inputX number actually will tell
  1526. * us something...
  1527. */
  1528. usb_make_path(usbdev, aiptek->features.usbPath,
  1529. sizeof(aiptek->features.usbPath));
  1530. strlcat(aiptek->features.usbPath, "/input0",
  1531. sizeof(aiptek->features.usbPath));
  1532. /* Set up client data, pointers to open and close routines
  1533. * for the input device.
  1534. */
  1535. inputdev->name = "Aiptek";
  1536. inputdev->phys = aiptek->features.usbPath;
  1537. usb_to_input_id(usbdev, &inputdev->id);
  1538. inputdev->dev.parent = &intf->dev;
  1539. input_set_drvdata(inputdev, aiptek);
  1540. inputdev->open = aiptek_open;
  1541. inputdev->close = aiptek_close;
  1542. /* Now program the capacities of the tablet, in terms of being
  1543. * an input device.
  1544. */
  1545. for (i = 0; i < ARRAY_SIZE(eventTypes); ++i)
  1546. __set_bit(eventTypes[i], inputdev->evbit);
  1547. for (i = 0; i < ARRAY_SIZE(absEvents); ++i)
  1548. __set_bit(absEvents[i], inputdev->absbit);
  1549. for (i = 0; i < ARRAY_SIZE(relEvents); ++i)
  1550. __set_bit(relEvents[i], inputdev->relbit);
  1551. __set_bit(MSC_SERIAL, inputdev->mscbit);
  1552. /* Set up key and button codes */
  1553. for (i = 0; i < ARRAY_SIZE(buttonEvents); ++i)
  1554. __set_bit(buttonEvents[i], inputdev->keybit);
  1555. for (i = 0; i < ARRAY_SIZE(macroKeyEvents); ++i)
  1556. __set_bit(macroKeyEvents[i], inputdev->keybit);
  1557. /*
  1558. * Program the input device coordinate capacities. We do not yet
  1559. * know what maximum X, Y, and Z values are, so we're putting fake
  1560. * values in. Later, we'll ask the tablet to put in the correct
  1561. * values.
  1562. */
  1563. input_set_abs_params(inputdev, ABS_X, 0, 2999, 0, 0);
  1564. input_set_abs_params(inputdev, ABS_Y, 0, 2249, 0, 0);
  1565. input_set_abs_params(inputdev, ABS_PRESSURE, 0, 511, 0, 0);
  1566. input_set_abs_params(inputdev, ABS_TILT_X, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
  1567. input_set_abs_params(inputdev, ABS_TILT_Y, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
  1568. input_set_abs_params(inputdev, ABS_WHEEL, AIPTEK_WHEEL_MIN, AIPTEK_WHEEL_MAX - 1, 0, 0);
  1569. /* Verify that a device really has an endpoint */
  1570. if (intf->altsetting[0].desc.bNumEndpoints < 1) {
  1571. dev_err(&intf->dev,
  1572. "interface has %d endpoints, but must have minimum 1\n",
  1573. intf->altsetting[0].desc.bNumEndpoints);
  1574. err = -EINVAL;
  1575. goto fail3;
  1576. }
  1577. endpoint = &intf->altsetting[0].endpoint[0].desc;
  1578. /* Go set up our URB, which is called when the tablet receives
  1579. * input.
  1580. */
  1581. usb_fill_int_urb(aiptek->urb,
  1582. usbdev,
  1583. usb_rcvintpipe(usbdev,
  1584. endpoint->bEndpointAddress),
  1585. aiptek->data, 8, aiptek_irq, aiptek,
  1586. endpoint->bInterval);
  1587. aiptek->urb->transfer_dma = aiptek->data_dma;
  1588. aiptek->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1589. /* Program the tablet. This sets the tablet up in the mode
  1590. * specified in newSetting, and also queries the tablet's
  1591. * physical capacities.
  1592. *
  1593. * Sanity check: if a tablet doesn't like the slow programmatic
  1594. * delay, we often get sizes of 0x0. Let's use that as an indicator
  1595. * to try faster delays, up to 25 ms. If that logic fails, well, you'll
  1596. * have to explain to us how your tablet thinks it's 0x0, and yet that's
  1597. * not an error :-)
  1598. */
  1599. for (i = 0; i < ARRAY_SIZE(speeds); ++i) {
  1600. aiptek->curSetting.programmableDelay = speeds[i];
  1601. (void)aiptek_program_tablet(aiptek);
  1602. if (input_abs_get_max(aiptek->inputdev, ABS_X) > 0) {
  1603. dev_info(&intf->dev,
  1604. "Aiptek using %d ms programming speed\n",
  1605. aiptek->curSetting.programmableDelay);
  1606. break;
  1607. }
  1608. }
  1609. /* Murphy says that some day someone will have a tablet that fails the
  1610. above test. That's you, Frederic Rodrigo */
  1611. if (i == ARRAY_SIZE(speeds)) {
  1612. dev_info(&intf->dev,
  1613. "Aiptek tried all speeds, no sane response\n");
  1614. err = -EINVAL;
  1615. goto fail3;
  1616. }
  1617. /* Associate this driver's struct with the usb interface.
  1618. */
  1619. usb_set_intfdata(intf, aiptek);
  1620. /* Set up the sysfs files
  1621. */
  1622. err = sysfs_create_group(&intf->dev.kobj, &aiptek_attribute_group);
  1623. if (err) {
  1624. dev_warn(&intf->dev, "cannot create sysfs group err: %d\n",
  1625. err);
  1626. goto fail3;
  1627. }
  1628. /* Register the tablet as an Input Device
  1629. */
  1630. err = input_register_device(aiptek->inputdev);
  1631. if (err) {
  1632. dev_warn(&intf->dev,
  1633. "input_register_device returned err: %d\n", err);
  1634. goto fail4;
  1635. }
  1636. return 0;
  1637. fail4: sysfs_remove_group(&intf->dev.kobj, &aiptek_attribute_group);
  1638. fail3: usb_free_urb(aiptek->urb);
  1639. fail2: usb_free_coherent(usbdev, AIPTEK_PACKET_LENGTH, aiptek->data,
  1640. aiptek->data_dma);
  1641. fail1: usb_set_intfdata(intf, NULL);
  1642. input_free_device(inputdev);
  1643. kfree(aiptek);
  1644. return err;
  1645. }
  1646. /***********************************************************************
  1647. * Deal with tablet disconnecting from the system.
  1648. */
  1649. static void aiptek_disconnect(struct usb_interface *intf)
  1650. {
  1651. struct aiptek *aiptek = usb_get_intfdata(intf);
  1652. /* Disassociate driver's struct with usb interface
  1653. */
  1654. usb_set_intfdata(intf, NULL);
  1655. if (aiptek != NULL) {
  1656. /* Free & unhook everything from the system.
  1657. */
  1658. usb_kill_urb(aiptek->urb);
  1659. input_unregister_device(aiptek->inputdev);
  1660. sysfs_remove_group(&intf->dev.kobj, &aiptek_attribute_group);
  1661. usb_free_urb(aiptek->urb);
  1662. usb_free_coherent(interface_to_usbdev(intf),
  1663. AIPTEK_PACKET_LENGTH,
  1664. aiptek->data, aiptek->data_dma);
  1665. kfree(aiptek);
  1666. }
  1667. }
  1668. static struct usb_driver aiptek_driver = {
  1669. .name = "aiptek",
  1670. .probe = aiptek_probe,
  1671. .disconnect = aiptek_disconnect,
  1672. .id_table = aiptek_ids,
  1673. };
  1674. module_usb_driver(aiptek_driver);
  1675. MODULE_AUTHOR(DRIVER_AUTHOR);
  1676. MODULE_DESCRIPTION(DRIVER_DESC);
  1677. MODULE_LICENSE("GPL");
  1678. module_param(programmableDelay, int, 0);
  1679. MODULE_PARM_DESC(programmableDelay, "delay used during tablet programming");
  1680. module_param(jitterDelay, int, 0);
  1681. MODULE_PARM_DESC(jitterDelay, "stylus/mouse settlement delay");