aiptek.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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_device *usbdev; /* usb device struct */
  290. struct usb_interface *intf; /* usb interface struct */
  291. struct urb *urb; /* urb for incoming reports */
  292. dma_addr_t data_dma; /* our dma stuffage */
  293. struct aiptek_features features; /* tablet's array of features */
  294. struct aiptek_settings curSetting; /* tablet's current programmable */
  295. struct aiptek_settings newSetting; /* ... and new param settings */
  296. unsigned int ifnum; /* interface number for IO */
  297. int diagnostic; /* tablet diagnostic codes */
  298. unsigned long eventCount; /* event count */
  299. int inDelay; /* jitter: in jitter delay? */
  300. unsigned long endDelay; /* jitter: time when delay ends */
  301. int previousJitterable; /* jitterable prev value */
  302. int lastMacro; /* macro key to reset */
  303. int previousToolMode; /* pen, pencil, brush, etc. tool */
  304. unsigned char *data; /* incoming packet data */
  305. };
  306. static const int eventTypes[] = {
  307. EV_KEY, EV_ABS, EV_REL, EV_MSC,
  308. };
  309. static const int absEvents[] = {
  310. ABS_X, ABS_Y, ABS_PRESSURE, ABS_TILT_X, ABS_TILT_Y,
  311. ABS_WHEEL, ABS_MISC,
  312. };
  313. static const int relEvents[] = {
  314. REL_X, REL_Y, REL_WHEEL,
  315. };
  316. static const int buttonEvents[] = {
  317. BTN_LEFT, BTN_RIGHT, BTN_MIDDLE,
  318. BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_PENCIL, BTN_TOOL_AIRBRUSH,
  319. BTN_TOOL_BRUSH, BTN_TOOL_MOUSE, BTN_TOOL_LENS, BTN_TOUCH,
  320. BTN_STYLUS, BTN_STYLUS2,
  321. };
  322. /*
  323. * Permit easy lookup of keyboard events to send, versus
  324. * the bitmap which comes from the tablet. This hides the
  325. * issue that the F_keys are not sequentially numbered.
  326. */
  327. static const int macroKeyEvents[] = {
  328. KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5,
  329. KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11,
  330. KEY_F12, KEY_F13, KEY_F14, KEY_F15, KEY_F16, KEY_F17,
  331. KEY_F18, KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23,
  332. KEY_F24, KEY_STOP, KEY_AGAIN, KEY_PROPS, KEY_UNDO,
  333. KEY_FRONT, KEY_COPY, KEY_OPEN, KEY_PASTE, 0
  334. };
  335. /***********************************************************************
  336. * Map values to strings and back. Every map should have the following
  337. * as its last element: { NULL, AIPTEK_INVALID_VALUE }.
  338. */
  339. #define AIPTEK_INVALID_VALUE -1
  340. struct aiptek_map {
  341. const char *string;
  342. int value;
  343. };
  344. static int map_str_to_val(const struct aiptek_map *map, const char *str, size_t count)
  345. {
  346. const struct aiptek_map *p;
  347. if (str[count - 1] == '\n')
  348. count--;
  349. for (p = map; p->string; p++)
  350. if (!strncmp(str, p->string, count))
  351. return p->value;
  352. return AIPTEK_INVALID_VALUE;
  353. }
  354. static const char *map_val_to_str(const struct aiptek_map *map, int val)
  355. {
  356. const struct aiptek_map *p;
  357. for (p = map; p->value != AIPTEK_INVALID_VALUE; p++)
  358. if (val == p->value)
  359. return p->string;
  360. return "unknown";
  361. }
  362. /***********************************************************************
  363. * aiptek_irq can receive one of six potential reports.
  364. * The documentation for each is in the body of the function.
  365. *
  366. * The tablet reports on several attributes per invocation of
  367. * aiptek_irq. Because the Linux Input Event system allows the
  368. * transmission of ONE attribute per input_report_xxx() call,
  369. * collation has to be done on the other end to reconstitute
  370. * a complete tablet report. Further, the number of Input Event reports
  371. * submitted varies, depending on what USB report type, and circumstance.
  372. * To deal with this, EV_MSC is used to indicate an 'end-of-report'
  373. * message. This has been an undocumented convention understood by the kernel
  374. * tablet driver and clients such as gpm and XFree86's tablet drivers.
  375. *
  376. * Of the information received from the tablet, the one piece I
  377. * cannot transmit is the proximity bit (without resorting to an EV_MSC
  378. * convention above.) I therefore have taken over REL_MISC and ABS_MISC
  379. * (for relative and absolute reports, respectively) for communicating
  380. * Proximity. Why two events? I thought it interesting to know if the
  381. * Proximity event occurred while the tablet was in absolute or relative
  382. * mode.
  383. * Update: REL_MISC proved not to be such a good idea. With REL_MISC you
  384. * get an event transmitted each time. ABS_MISC works better, since it
  385. * can be set and re-set. Thus, only using ABS_MISC from now on.
  386. *
  387. * Other tablets use the notion of a certain minimum stylus pressure
  388. * to infer proximity. While that could have been done, that is yet
  389. * another 'by convention' behavior, the documentation for which
  390. * would be spread between two (or more) pieces of software.
  391. *
  392. * EV_MSC usage was terminated for this purpose in Linux 2.5.x, and
  393. * replaced with the input_sync() method (which emits EV_SYN.)
  394. */
  395. static void aiptek_irq(struct urb *urb)
  396. {
  397. struct aiptek *aiptek = urb->context;
  398. unsigned char *data = aiptek->data;
  399. struct input_dev *inputdev = aiptek->inputdev;
  400. struct usb_interface *intf = aiptek->intf;
  401. int jitterable = 0;
  402. int retval, macro, x, y, z, left, right, middle, p, dv, tip, bs, pck;
  403. switch (urb->status) {
  404. case 0:
  405. /* Success */
  406. break;
  407. case -ECONNRESET:
  408. case -ENOENT:
  409. case -ESHUTDOWN:
  410. /* This urb is terminated, clean up */
  411. dev_dbg(&intf->dev, "%s - urb shutting down with status: %d\n",
  412. __func__, urb->status);
  413. return;
  414. default:
  415. dev_dbg(&intf->dev, "%s - nonzero urb status received: %d\n",
  416. __func__, urb->status);
  417. goto exit;
  418. }
  419. /* See if we are in a delay loop -- throw out report if true.
  420. */
  421. if (aiptek->inDelay == 1 && time_after(aiptek->endDelay, jiffies)) {
  422. goto exit;
  423. }
  424. aiptek->inDelay = 0;
  425. aiptek->eventCount++;
  426. /* Report 1 delivers relative coordinates with either a stylus
  427. * or the mouse. You do not know, however, which input
  428. * tool generated the event.
  429. */
  430. if (data[0] == 1) {
  431. if (aiptek->curSetting.coordinateMode ==
  432. AIPTEK_COORDINATE_ABSOLUTE_MODE) {
  433. aiptek->diagnostic =
  434. AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE;
  435. } else {
  436. x = (signed char) data[2];
  437. y = (signed char) data[3];
  438. /* jitterable keeps track of whether any button has been pressed.
  439. * We're also using it to remap the physical mouse button mask
  440. * to pseudo-settings. (We don't specifically care about it's
  441. * value after moving/transposing mouse button bitmasks, except
  442. * that a non-zero value indicates that one or more
  443. * mouse button was pressed.)
  444. */
  445. jitterable = data[1] & 0x07;
  446. left = (data[1] & aiptek->curSetting.mouseButtonLeft >> 2) != 0 ? 1 : 0;
  447. right = (data[1] & aiptek->curSetting.mouseButtonRight >> 2) != 0 ? 1 : 0;
  448. middle = (data[1] & aiptek->curSetting.mouseButtonMiddle >> 2) != 0 ? 1 : 0;
  449. input_report_key(inputdev, BTN_LEFT, left);
  450. input_report_key(inputdev, BTN_MIDDLE, middle);
  451. input_report_key(inputdev, BTN_RIGHT, right);
  452. input_report_abs(inputdev, ABS_MISC,
  453. 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
  454. input_report_rel(inputdev, REL_X, x);
  455. input_report_rel(inputdev, REL_Y, y);
  456. /* Wheel support is in the form of a single-event
  457. * firing.
  458. */
  459. if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
  460. input_report_rel(inputdev, REL_WHEEL,
  461. aiptek->curSetting.wheel);
  462. aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
  463. }
  464. if (aiptek->lastMacro != -1) {
  465. input_report_key(inputdev,
  466. macroKeyEvents[aiptek->lastMacro], 0);
  467. aiptek->lastMacro = -1;
  468. }
  469. input_sync(inputdev);
  470. }
  471. }
  472. /* Report 2 is delivered only by the stylus, and delivers
  473. * absolute coordinates.
  474. */
  475. else if (data[0] == 2) {
  476. if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
  477. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
  478. } else if (!AIPTEK_POINTER_ALLOW_STYLUS_MODE
  479. (aiptek->curSetting.pointerMode)) {
  480. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
  481. } else {
  482. x = get_unaligned_le16(data + 1);
  483. y = get_unaligned_le16(data + 3);
  484. z = get_unaligned_le16(data + 6);
  485. dv = (data[5] & 0x01) != 0 ? 1 : 0;
  486. p = (data[5] & 0x02) != 0 ? 1 : 0;
  487. tip = (data[5] & 0x04) != 0 ? 1 : 0;
  488. /* Use jitterable to re-arrange button masks
  489. */
  490. jitterable = data[5] & 0x18;
  491. bs = (data[5] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
  492. pck = (data[5] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;
  493. /* dv indicates 'data valid' (e.g., the tablet is in sync
  494. * and has delivered a "correct" report) We will ignore
  495. * all 'bad' reports...
  496. */
  497. if (dv != 0) {
  498. /* If the selected tool changed, reset the old
  499. * tool key, and set the new one.
  500. */
  501. if (aiptek->previousToolMode !=
  502. aiptek->curSetting.toolMode) {
  503. input_report_key(inputdev,
  504. aiptek->previousToolMode, 0);
  505. input_report_key(inputdev,
  506. aiptek->curSetting.toolMode,
  507. 1);
  508. aiptek->previousToolMode =
  509. aiptek->curSetting.toolMode;
  510. }
  511. if (p != 0) {
  512. input_report_abs(inputdev, ABS_X, x);
  513. input_report_abs(inputdev, ABS_Y, y);
  514. input_report_abs(inputdev, ABS_PRESSURE, z);
  515. input_report_key(inputdev, BTN_TOUCH, tip);
  516. input_report_key(inputdev, BTN_STYLUS, bs);
  517. input_report_key(inputdev, BTN_STYLUS2, pck);
  518. if (aiptek->curSetting.xTilt !=
  519. AIPTEK_TILT_DISABLE) {
  520. input_report_abs(inputdev,
  521. ABS_TILT_X,
  522. aiptek->curSetting.xTilt);
  523. }
  524. if (aiptek->curSetting.yTilt != AIPTEK_TILT_DISABLE) {
  525. input_report_abs(inputdev,
  526. ABS_TILT_Y,
  527. aiptek->curSetting.yTilt);
  528. }
  529. /* Wheel support is in the form of a single-event
  530. * firing.
  531. */
  532. if (aiptek->curSetting.wheel !=
  533. AIPTEK_WHEEL_DISABLE) {
  534. input_report_abs(inputdev,
  535. ABS_WHEEL,
  536. aiptek->curSetting.wheel);
  537. aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
  538. }
  539. }
  540. input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_STYLUS);
  541. if (aiptek->lastMacro != -1) {
  542. input_report_key(inputdev,
  543. macroKeyEvents[aiptek->lastMacro], 0);
  544. aiptek->lastMacro = -1;
  545. }
  546. input_sync(inputdev);
  547. }
  548. }
  549. }
  550. /* Report 3's come from the mouse in absolute mode.
  551. */
  552. else if (data[0] == 3) {
  553. if (aiptek->curSetting.coordinateMode == AIPTEK_COORDINATE_RELATIVE_MODE) {
  554. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
  555. } else if (!AIPTEK_POINTER_ALLOW_MOUSE_MODE
  556. (aiptek->curSetting.pointerMode)) {
  557. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
  558. } else {
  559. x = get_unaligned_le16(data + 1);
  560. y = get_unaligned_le16(data + 3);
  561. jitterable = data[5] & 0x1c;
  562. dv = (data[5] & 0x01) != 0 ? 1 : 0;
  563. p = (data[5] & 0x02) != 0 ? 1 : 0;
  564. left = (data[5] & aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
  565. right = (data[5] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
  566. middle = (data[5] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
  567. if (dv != 0) {
  568. /* If the selected tool changed, reset the old
  569. * tool key, and set the new one.
  570. */
  571. if (aiptek->previousToolMode !=
  572. aiptek->curSetting.toolMode) {
  573. input_report_key(inputdev,
  574. aiptek->previousToolMode, 0);
  575. input_report_key(inputdev,
  576. aiptek->curSetting.toolMode,
  577. 1);
  578. aiptek->previousToolMode =
  579. aiptek->curSetting.toolMode;
  580. }
  581. if (p != 0) {
  582. input_report_abs(inputdev, ABS_X, x);
  583. input_report_abs(inputdev, ABS_Y, y);
  584. input_report_key(inputdev, BTN_LEFT, left);
  585. input_report_key(inputdev, BTN_MIDDLE, middle);
  586. input_report_key(inputdev, BTN_RIGHT, right);
  587. /* Wheel support is in the form of a single-event
  588. * firing.
  589. */
  590. if (aiptek->curSetting.wheel != AIPTEK_WHEEL_DISABLE) {
  591. input_report_abs(inputdev,
  592. ABS_WHEEL,
  593. aiptek->curSetting.wheel);
  594. aiptek->curSetting.wheel = AIPTEK_WHEEL_DISABLE;
  595. }
  596. }
  597. input_report_abs(inputdev, ABS_MISC, p | AIPTEK_REPORT_TOOL_MOUSE);
  598. if (aiptek->lastMacro != -1) {
  599. input_report_key(inputdev,
  600. macroKeyEvents[aiptek->lastMacro], 0);
  601. aiptek->lastMacro = -1;
  602. }
  603. input_sync(inputdev);
  604. }
  605. }
  606. }
  607. /* Report 4s come from the macro keys when pressed by stylus
  608. */
  609. else if (data[0] == 4) {
  610. jitterable = data[1] & 0x18;
  611. dv = (data[1] & 0x01) != 0 ? 1 : 0;
  612. p = (data[1] & 0x02) != 0 ? 1 : 0;
  613. tip = (data[1] & 0x04) != 0 ? 1 : 0;
  614. bs = (data[1] & aiptek->curSetting.stylusButtonLower) != 0 ? 1 : 0;
  615. pck = (data[1] & aiptek->curSetting.stylusButtonUpper) != 0 ? 1 : 0;
  616. macro = dv && p && tip && !(data[3] & 1) ? (data[3] >> 1) : -1;
  617. z = get_unaligned_le16(data + 4);
  618. if (dv) {
  619. /* If the selected tool changed, reset the old
  620. * tool key, and set the new one.
  621. */
  622. if (aiptek->previousToolMode !=
  623. aiptek->curSetting.toolMode) {
  624. input_report_key(inputdev,
  625. aiptek->previousToolMode, 0);
  626. input_report_key(inputdev,
  627. aiptek->curSetting.toolMode,
  628. 1);
  629. aiptek->previousToolMode =
  630. aiptek->curSetting.toolMode;
  631. }
  632. }
  633. if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
  634. input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
  635. aiptek->lastMacro = -1;
  636. }
  637. if (macro != -1 && macro != aiptek->lastMacro) {
  638. input_report_key(inputdev, macroKeyEvents[macro], 1);
  639. aiptek->lastMacro = macro;
  640. }
  641. input_report_abs(inputdev, ABS_MISC,
  642. p | AIPTEK_REPORT_TOOL_STYLUS);
  643. input_sync(inputdev);
  644. }
  645. /* Report 5s come from the macro keys when pressed by mouse
  646. */
  647. else if (data[0] == 5) {
  648. jitterable = data[1] & 0x1c;
  649. dv = (data[1] & 0x01) != 0 ? 1 : 0;
  650. p = (data[1] & 0x02) != 0 ? 1 : 0;
  651. left = (data[1]& aiptek->curSetting.mouseButtonLeft) != 0 ? 1 : 0;
  652. right = (data[1] & aiptek->curSetting.mouseButtonRight) != 0 ? 1 : 0;
  653. middle = (data[1] & aiptek->curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
  654. macro = dv && p && left && !(data[3] & 1) ? (data[3] >> 1) : 0;
  655. if (dv) {
  656. /* If the selected tool changed, reset the old
  657. * tool key, and set the new one.
  658. */
  659. if (aiptek->previousToolMode !=
  660. aiptek->curSetting.toolMode) {
  661. input_report_key(inputdev,
  662. aiptek->previousToolMode, 0);
  663. input_report_key(inputdev,
  664. aiptek->curSetting.toolMode, 1);
  665. aiptek->previousToolMode = aiptek->curSetting.toolMode;
  666. }
  667. }
  668. if (aiptek->lastMacro != -1 && aiptek->lastMacro != macro) {
  669. input_report_key(inputdev, macroKeyEvents[aiptek->lastMacro], 0);
  670. aiptek->lastMacro = -1;
  671. }
  672. if (macro != -1 && macro != aiptek->lastMacro) {
  673. input_report_key(inputdev, macroKeyEvents[macro], 1);
  674. aiptek->lastMacro = macro;
  675. }
  676. input_report_abs(inputdev, ABS_MISC,
  677. p | AIPTEK_REPORT_TOOL_MOUSE);
  678. input_sync(inputdev);
  679. }
  680. /* We have no idea which tool can generate a report 6. Theoretically,
  681. * neither need to, having been given reports 4 & 5 for such use.
  682. * However, report 6 is the 'official-looking' report for macroKeys;
  683. * reports 4 & 5 supposively are used to support unnamed, unknown
  684. * hat switches (which just so happen to be the macroKeys.)
  685. */
  686. else if (data[0] == 6) {
  687. macro = get_unaligned_le16(data + 1);
  688. if (macro > 0) {
  689. input_report_key(inputdev, macroKeyEvents[macro - 1],
  690. 0);
  691. }
  692. if (macro < 25) {
  693. input_report_key(inputdev, macroKeyEvents[macro + 1],
  694. 0);
  695. }
  696. /* If the selected tool changed, reset the old
  697. tool key, and set the new one.
  698. */
  699. if (aiptek->previousToolMode !=
  700. aiptek->curSetting.toolMode) {
  701. input_report_key(inputdev,
  702. aiptek->previousToolMode, 0);
  703. input_report_key(inputdev,
  704. aiptek->curSetting.toolMode,
  705. 1);
  706. aiptek->previousToolMode =
  707. aiptek->curSetting.toolMode;
  708. }
  709. input_report_key(inputdev, macroKeyEvents[macro], 1);
  710. input_report_abs(inputdev, ABS_MISC,
  711. 1 | AIPTEK_REPORT_TOOL_UNKNOWN);
  712. input_sync(inputdev);
  713. } else {
  714. dev_dbg(&intf->dev, "Unknown report %d\n", data[0]);
  715. }
  716. /* Jitter may occur when the user presses a button on the stlyus
  717. * or the mouse. What we do to prevent that is wait 'x' milliseconds
  718. * following a 'jitterable' event, which should give the hand some time
  719. * stabilize itself.
  720. *
  721. * We just introduced aiptek->previousJitterable to carry forth the
  722. * notion that jitter occurs when the button state changes from on to off:
  723. * a person drawing, holding a button down is not subject to jittering.
  724. * With that in mind, changing from upper button depressed to lower button
  725. * WILL transition through a jitter delay.
  726. */
  727. if (aiptek->previousJitterable != jitterable &&
  728. aiptek->curSetting.jitterDelay != 0 && aiptek->inDelay != 1) {
  729. aiptek->endDelay = jiffies +
  730. ((aiptek->curSetting.jitterDelay * HZ) / 1000);
  731. aiptek->inDelay = 1;
  732. }
  733. aiptek->previousJitterable = jitterable;
  734. exit:
  735. retval = usb_submit_urb(urb, GFP_ATOMIC);
  736. if (retval != 0) {
  737. dev_err(&intf->dev,
  738. "%s - usb_submit_urb failed with result %d\n",
  739. __func__, retval);
  740. }
  741. }
  742. /***********************************************************************
  743. * These are the USB id's known so far. We do not identify them to
  744. * specific Aiptek model numbers, because there has been overlaps,
  745. * use, and reuse of id's in existing models. Certain models have
  746. * been known to use more than one ID, indicative perhaps of
  747. * manufacturing revisions. In any event, we consider these
  748. * IDs to not be model-specific nor unique.
  749. */
  750. static const struct usb_device_id aiptek_ids[] = {
  751. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x01)},
  752. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x10)},
  753. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20)},
  754. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x21)},
  755. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x22)},
  756. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x23)},
  757. {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x24)},
  758. {USB_DEVICE(USB_VENDOR_ID_KYE, 0x5003)},
  759. {}
  760. };
  761. MODULE_DEVICE_TABLE(usb, aiptek_ids);
  762. /***********************************************************************
  763. * Open an instance of the tablet driver.
  764. */
  765. static int aiptek_open(struct input_dev *inputdev)
  766. {
  767. struct aiptek *aiptek = input_get_drvdata(inputdev);
  768. aiptek->urb->dev = aiptek->usbdev;
  769. if (usb_submit_urb(aiptek->urb, GFP_KERNEL) != 0)
  770. return -EIO;
  771. return 0;
  772. }
  773. /***********************************************************************
  774. * Close an instance of the tablet driver.
  775. */
  776. static void aiptek_close(struct input_dev *inputdev)
  777. {
  778. struct aiptek *aiptek = input_get_drvdata(inputdev);
  779. usb_kill_urb(aiptek->urb);
  780. }
  781. /***********************************************************************
  782. * aiptek_set_report and aiptek_get_report() are borrowed from Linux 2.4.x,
  783. * where they were known as usb_set_report and usb_get_report.
  784. */
  785. static int
  786. aiptek_set_report(struct aiptek *aiptek,
  787. unsigned char report_type,
  788. unsigned char report_id, void *buffer, int size)
  789. {
  790. return usb_control_msg(aiptek->usbdev,
  791. usb_sndctrlpipe(aiptek->usbdev, 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. return usb_control_msg(aiptek->usbdev,
  803. usb_rcvctrlpipe(aiptek->usbdev, 0),
  804. USB_REQ_GET_REPORT,
  805. USB_TYPE_CLASS | USB_RECIP_INTERFACE |
  806. USB_DIR_IN, (report_type << 8) + report_id,
  807. aiptek->ifnum, buffer, size, 5000);
  808. }
  809. /***********************************************************************
  810. * Send a command to the tablet.
  811. */
  812. static int
  813. aiptek_command(struct aiptek *aiptek, unsigned char command, unsigned char data)
  814. {
  815. const int sizeof_buf = 3 * sizeof(u8);
  816. int ret;
  817. u8 *buf;
  818. buf = kmalloc(sizeof_buf, GFP_KERNEL);
  819. if (!buf)
  820. return -ENOMEM;
  821. buf[0] = 2;
  822. buf[1] = command;
  823. buf[2] = data;
  824. if ((ret =
  825. aiptek_set_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
  826. dev_dbg(&aiptek->intf->dev,
  827. "aiptek_program: failed, tried to send: 0x%02x 0x%02x\n",
  828. command, data);
  829. }
  830. kfree(buf);
  831. return ret < 0 ? ret : 0;
  832. }
  833. /***********************************************************************
  834. * Retrieve information from the tablet. Querying info is defined as first
  835. * sending the {command,data} sequence as a command, followed by a wait
  836. * (aka, "programmaticDelay") and then a "read" request.
  837. */
  838. static int
  839. aiptek_query(struct aiptek *aiptek, unsigned char command, unsigned char data)
  840. {
  841. const int sizeof_buf = 3 * sizeof(u8);
  842. int ret;
  843. u8 *buf;
  844. buf = kmalloc(sizeof_buf, GFP_KERNEL);
  845. if (!buf)
  846. return -ENOMEM;
  847. buf[0] = 2;
  848. buf[1] = command;
  849. buf[2] = data;
  850. if (aiptek_command(aiptek, command, data) != 0) {
  851. kfree(buf);
  852. return -EIO;
  853. }
  854. msleep(aiptek->curSetting.programmableDelay);
  855. if ((ret =
  856. aiptek_get_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
  857. dev_dbg(&aiptek->intf->dev,
  858. "aiptek_query failed: returned 0x%02x 0x%02x 0x%02x\n",
  859. buf[0], buf[1], buf[2]);
  860. ret = -EIO;
  861. } else {
  862. ret = get_unaligned_le16(buf + 1);
  863. }
  864. kfree(buf);
  865. return ret;
  866. }
  867. /***********************************************************************
  868. * Program the tablet into either absolute or relative mode.
  869. * We also get information about the tablet's size.
  870. */
  871. static int aiptek_program_tablet(struct aiptek *aiptek)
  872. {
  873. int ret;
  874. /* Execute Resolution500LPI */
  875. if ((ret = aiptek_command(aiptek, 0x18, 0x04)) < 0)
  876. return ret;
  877. /* Query getModelCode */
  878. if ((ret = aiptek_query(aiptek, 0x02, 0x00)) < 0)
  879. return ret;
  880. aiptek->features.modelCode = ret & 0xff;
  881. /* Query getODMCode */
  882. if ((ret = aiptek_query(aiptek, 0x03, 0x00)) < 0)
  883. return ret;
  884. aiptek->features.odmCode = ret;
  885. /* Query getFirmwareCode */
  886. if ((ret = aiptek_query(aiptek, 0x04, 0x00)) < 0)
  887. return ret;
  888. aiptek->features.firmwareCode = ret;
  889. /* Query getXextension */
  890. if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
  891. return ret;
  892. input_set_abs_params(aiptek->inputdev, ABS_X, 0, ret - 1, 0, 0);
  893. /* Query getYextension */
  894. if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
  895. return ret;
  896. input_set_abs_params(aiptek->inputdev, ABS_Y, 0, ret - 1, 0, 0);
  897. /* Query getPressureLevels */
  898. if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
  899. return ret;
  900. input_set_abs_params(aiptek->inputdev, ABS_PRESSURE, 0, ret - 1, 0, 0);
  901. /* Depending on whether we are in absolute or relative mode, we will
  902. * do a switchToTablet(absolute) or switchToMouse(relative) command.
  903. */
  904. if (aiptek->curSetting.coordinateMode ==
  905. AIPTEK_COORDINATE_ABSOLUTE_MODE) {
  906. /* Execute switchToTablet */
  907. if ((ret = aiptek_command(aiptek, 0x10, 0x01)) < 0) {
  908. return ret;
  909. }
  910. } else {
  911. /* Execute switchToMouse */
  912. if ((ret = aiptek_command(aiptek, 0x10, 0x00)) < 0) {
  913. return ret;
  914. }
  915. }
  916. /* Enable the macro keys */
  917. if ((ret = aiptek_command(aiptek, 0x11, 0x02)) < 0)
  918. return ret;
  919. #if 0
  920. /* Execute FilterOn */
  921. if ((ret = aiptek_command(aiptek, 0x17, 0x00)) < 0)
  922. return ret;
  923. #endif
  924. /* Execute AutoGainOn */
  925. if ((ret = aiptek_command(aiptek, 0x12, 0xff)) < 0)
  926. return ret;
  927. /* Reset the eventCount, so we track events from last (re)programming
  928. */
  929. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
  930. aiptek->eventCount = 0;
  931. return 0;
  932. }
  933. /***********************************************************************
  934. * Sysfs functions. Sysfs prefers that individually-tunable parameters
  935. * exist in their separate pseudo-files. Summary data that is immutable
  936. * may exist in a singular file so long as you don't define a writeable
  937. * interface.
  938. */
  939. /***********************************************************************
  940. * support the 'size' file -- display support
  941. */
  942. static ssize_t show_tabletSize(struct device *dev, struct device_attribute *attr, char *buf)
  943. {
  944. struct aiptek *aiptek = dev_get_drvdata(dev);
  945. return snprintf(buf, PAGE_SIZE, "%dx%d\n",
  946. input_abs_get_max(aiptek->inputdev, ABS_X) + 1,
  947. input_abs_get_max(aiptek->inputdev, ABS_Y) + 1);
  948. }
  949. /* These structs define the sysfs files, param #1 is the name of the
  950. * file, param 2 is the file permissions, param 3 & 4 are to the
  951. * output generator and input parser routines. Absence of a routine is
  952. * permitted -- it only means can't either 'cat' the file, or send data
  953. * to it.
  954. */
  955. static DEVICE_ATTR(size, S_IRUGO, show_tabletSize, NULL);
  956. /***********************************************************************
  957. * support routines for the 'pointer_mode' file. Note that this file
  958. * both displays current setting and allows reprogramming.
  959. */
  960. static struct aiptek_map pointer_mode_map[] = {
  961. { "stylus", AIPTEK_POINTER_ONLY_STYLUS_MODE },
  962. { "mouse", AIPTEK_POINTER_ONLY_MOUSE_MODE },
  963. { "either", AIPTEK_POINTER_EITHER_MODE },
  964. { NULL, AIPTEK_INVALID_VALUE }
  965. };
  966. static ssize_t show_tabletPointerMode(struct device *dev, struct device_attribute *attr, char *buf)
  967. {
  968. struct aiptek *aiptek = dev_get_drvdata(dev);
  969. return snprintf(buf, PAGE_SIZE, "%s\n",
  970. map_val_to_str(pointer_mode_map,
  971. aiptek->curSetting.pointerMode));
  972. }
  973. static ssize_t
  974. store_tabletPointerMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  975. {
  976. struct aiptek *aiptek = dev_get_drvdata(dev);
  977. int new_mode = map_str_to_val(pointer_mode_map, buf, count);
  978. if (new_mode == AIPTEK_INVALID_VALUE)
  979. return -EINVAL;
  980. aiptek->newSetting.pointerMode = new_mode;
  981. return count;
  982. }
  983. static DEVICE_ATTR(pointer_mode,
  984. S_IRUGO | S_IWUSR,
  985. show_tabletPointerMode, store_tabletPointerMode);
  986. /***********************************************************************
  987. * support routines for the 'coordinate_mode' file. Note that this file
  988. * both displays current setting and allows reprogramming.
  989. */
  990. static struct aiptek_map coordinate_mode_map[] = {
  991. { "absolute", AIPTEK_COORDINATE_ABSOLUTE_MODE },
  992. { "relative", AIPTEK_COORDINATE_RELATIVE_MODE },
  993. { NULL, AIPTEK_INVALID_VALUE }
  994. };
  995. static ssize_t show_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, char *buf)
  996. {
  997. struct aiptek *aiptek = dev_get_drvdata(dev);
  998. return snprintf(buf, PAGE_SIZE, "%s\n",
  999. map_val_to_str(coordinate_mode_map,
  1000. aiptek->curSetting.coordinateMode));
  1001. }
  1002. static ssize_t
  1003. store_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1004. {
  1005. struct aiptek *aiptek = dev_get_drvdata(dev);
  1006. int new_mode = map_str_to_val(coordinate_mode_map, buf, count);
  1007. if (new_mode == AIPTEK_INVALID_VALUE)
  1008. return -EINVAL;
  1009. aiptek->newSetting.coordinateMode = new_mode;
  1010. return count;
  1011. }
  1012. static DEVICE_ATTR(coordinate_mode,
  1013. S_IRUGO | S_IWUSR,
  1014. show_tabletCoordinateMode, store_tabletCoordinateMode);
  1015. /***********************************************************************
  1016. * support routines for the 'tool_mode' file. Note that this file
  1017. * both displays current setting and allows reprogramming.
  1018. */
  1019. static struct aiptek_map tool_mode_map[] = {
  1020. { "mouse", AIPTEK_TOOL_BUTTON_MOUSE_MODE },
  1021. { "eraser", AIPTEK_TOOL_BUTTON_ERASER_MODE },
  1022. { "pencil", AIPTEK_TOOL_BUTTON_PENCIL_MODE },
  1023. { "pen", AIPTEK_TOOL_BUTTON_PEN_MODE },
  1024. { "brush", AIPTEK_TOOL_BUTTON_BRUSH_MODE },
  1025. { "airbrush", AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE },
  1026. { "lens", AIPTEK_TOOL_BUTTON_LENS_MODE },
  1027. { NULL, AIPTEK_INVALID_VALUE }
  1028. };
  1029. static ssize_t show_tabletToolMode(struct device *dev, struct device_attribute *attr, char *buf)
  1030. {
  1031. struct aiptek *aiptek = dev_get_drvdata(dev);
  1032. return snprintf(buf, PAGE_SIZE, "%s\n",
  1033. map_val_to_str(tool_mode_map,
  1034. aiptek->curSetting.toolMode));
  1035. }
  1036. static ssize_t
  1037. store_tabletToolMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1038. {
  1039. struct aiptek *aiptek = dev_get_drvdata(dev);
  1040. int new_mode = map_str_to_val(tool_mode_map, buf, count);
  1041. if (new_mode == AIPTEK_INVALID_VALUE)
  1042. return -EINVAL;
  1043. aiptek->newSetting.toolMode = new_mode;
  1044. return count;
  1045. }
  1046. static DEVICE_ATTR(tool_mode,
  1047. S_IRUGO | S_IWUSR,
  1048. show_tabletToolMode, store_tabletToolMode);
  1049. /***********************************************************************
  1050. * support routines for the 'xtilt' file. Note that this file
  1051. * both displays current setting and allows reprogramming.
  1052. */
  1053. static ssize_t show_tabletXtilt(struct device *dev, struct device_attribute *attr, char *buf)
  1054. {
  1055. struct aiptek *aiptek = dev_get_drvdata(dev);
  1056. if (aiptek->curSetting.xTilt == AIPTEK_TILT_DISABLE) {
  1057. return snprintf(buf, PAGE_SIZE, "disable\n");
  1058. } else {
  1059. return snprintf(buf, PAGE_SIZE, "%d\n",
  1060. aiptek->curSetting.xTilt);
  1061. }
  1062. }
  1063. static ssize_t
  1064. store_tabletXtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1065. {
  1066. struct aiptek *aiptek = dev_get_drvdata(dev);
  1067. int x;
  1068. if (kstrtoint(buf, 10, &x)) {
  1069. size_t len = buf[count - 1] == '\n' ? count - 1 : count;
  1070. if (strncmp(buf, "disable", len))
  1071. return -EINVAL;
  1072. aiptek->newSetting.xTilt = AIPTEK_TILT_DISABLE;
  1073. } else {
  1074. if (x < AIPTEK_TILT_MIN || x > AIPTEK_TILT_MAX)
  1075. return -EINVAL;
  1076. aiptek->newSetting.xTilt = x;
  1077. }
  1078. return count;
  1079. }
  1080. static DEVICE_ATTR(xtilt,
  1081. S_IRUGO | S_IWUSR, show_tabletXtilt, store_tabletXtilt);
  1082. /***********************************************************************
  1083. * support routines for the 'ytilt' file. Note that this file
  1084. * both displays current setting and allows reprogramming.
  1085. */
  1086. static ssize_t show_tabletYtilt(struct device *dev, struct device_attribute *attr, char *buf)
  1087. {
  1088. struct aiptek *aiptek = dev_get_drvdata(dev);
  1089. if (aiptek->curSetting.yTilt == AIPTEK_TILT_DISABLE) {
  1090. return snprintf(buf, PAGE_SIZE, "disable\n");
  1091. } else {
  1092. return snprintf(buf, PAGE_SIZE, "%d\n",
  1093. aiptek->curSetting.yTilt);
  1094. }
  1095. }
  1096. static ssize_t
  1097. store_tabletYtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1098. {
  1099. struct aiptek *aiptek = dev_get_drvdata(dev);
  1100. int y;
  1101. if (kstrtoint(buf, 10, &y)) {
  1102. size_t len = buf[count - 1] == '\n' ? count - 1 : count;
  1103. if (strncmp(buf, "disable", len))
  1104. return -EINVAL;
  1105. aiptek->newSetting.yTilt = AIPTEK_TILT_DISABLE;
  1106. } else {
  1107. if (y < AIPTEK_TILT_MIN || y > AIPTEK_TILT_MAX)
  1108. return -EINVAL;
  1109. aiptek->newSetting.yTilt = y;
  1110. }
  1111. return count;
  1112. }
  1113. static DEVICE_ATTR(ytilt,
  1114. S_IRUGO | S_IWUSR, show_tabletYtilt, store_tabletYtilt);
  1115. /***********************************************************************
  1116. * support routines for the 'jitter' file. Note that this file
  1117. * both displays current setting and allows reprogramming.
  1118. */
  1119. static ssize_t show_tabletJitterDelay(struct device *dev, struct device_attribute *attr, char *buf)
  1120. {
  1121. struct aiptek *aiptek = dev_get_drvdata(dev);
  1122. return snprintf(buf, PAGE_SIZE, "%d\n", aiptek->curSetting.jitterDelay);
  1123. }
  1124. static ssize_t
  1125. store_tabletJitterDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1126. {
  1127. struct aiptek *aiptek = dev_get_drvdata(dev);
  1128. int err, j;
  1129. err = kstrtoint(buf, 10, &j);
  1130. if (err)
  1131. return err;
  1132. aiptek->newSetting.jitterDelay = j;
  1133. return count;
  1134. }
  1135. static DEVICE_ATTR(jitter,
  1136. S_IRUGO | S_IWUSR,
  1137. show_tabletJitterDelay, store_tabletJitterDelay);
  1138. /***********************************************************************
  1139. * support routines for the 'delay' file. Note that this file
  1140. * both displays current setting and allows reprogramming.
  1141. */
  1142. static ssize_t show_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, char *buf)
  1143. {
  1144. struct aiptek *aiptek = dev_get_drvdata(dev);
  1145. return snprintf(buf, PAGE_SIZE, "%d\n",
  1146. aiptek->curSetting.programmableDelay);
  1147. }
  1148. static ssize_t
  1149. store_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1150. {
  1151. struct aiptek *aiptek = dev_get_drvdata(dev);
  1152. int err, d;
  1153. err = kstrtoint(buf, 10, &d);
  1154. if (err)
  1155. return err;
  1156. aiptek->newSetting.programmableDelay = d;
  1157. return count;
  1158. }
  1159. static DEVICE_ATTR(delay,
  1160. S_IRUGO | S_IWUSR,
  1161. show_tabletProgrammableDelay, store_tabletProgrammableDelay);
  1162. /***********************************************************************
  1163. * support routines for the 'event_count' file. Note that this file
  1164. * only displays current setting.
  1165. */
  1166. static ssize_t show_tabletEventsReceived(struct device *dev, struct device_attribute *attr, char *buf)
  1167. {
  1168. struct aiptek *aiptek = dev_get_drvdata(dev);
  1169. return snprintf(buf, PAGE_SIZE, "%ld\n", aiptek->eventCount);
  1170. }
  1171. static DEVICE_ATTR(event_count, S_IRUGO, show_tabletEventsReceived, NULL);
  1172. /***********************************************************************
  1173. * support routines for the 'diagnostic' file. Note that this file
  1174. * only displays current setting.
  1175. */
  1176. static ssize_t show_tabletDiagnosticMessage(struct device *dev, struct device_attribute *attr, char *buf)
  1177. {
  1178. struct aiptek *aiptek = dev_get_drvdata(dev);
  1179. char *retMsg;
  1180. switch (aiptek->diagnostic) {
  1181. case AIPTEK_DIAGNOSTIC_NA:
  1182. retMsg = "no errors\n";
  1183. break;
  1184. case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
  1185. retMsg = "Error: receiving relative reports\n";
  1186. break;
  1187. case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
  1188. retMsg = "Error: receiving absolute reports\n";
  1189. break;
  1190. case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
  1191. if (aiptek->curSetting.pointerMode ==
  1192. AIPTEK_POINTER_ONLY_MOUSE_MODE) {
  1193. retMsg = "Error: receiving stylus reports\n";
  1194. } else {
  1195. retMsg = "Error: receiving mouse reports\n";
  1196. }
  1197. break;
  1198. default:
  1199. return 0;
  1200. }
  1201. return snprintf(buf, PAGE_SIZE, retMsg);
  1202. }
  1203. static DEVICE_ATTR(diagnostic, S_IRUGO, show_tabletDiagnosticMessage, NULL);
  1204. /***********************************************************************
  1205. * support routines for the 'stylus_upper' file. Note that this file
  1206. * both displays current setting and allows for setting changing.
  1207. */
  1208. static struct aiptek_map stylus_button_map[] = {
  1209. { "upper", AIPTEK_STYLUS_UPPER_BUTTON },
  1210. { "lower", AIPTEK_STYLUS_LOWER_BUTTON },
  1211. { NULL, AIPTEK_INVALID_VALUE }
  1212. };
  1213. static ssize_t show_tabletStylusUpper(struct device *dev, struct device_attribute *attr, char *buf)
  1214. {
  1215. struct aiptek *aiptek = dev_get_drvdata(dev);
  1216. return snprintf(buf, PAGE_SIZE, "%s\n",
  1217. map_val_to_str(stylus_button_map,
  1218. aiptek->curSetting.stylusButtonUpper));
  1219. }
  1220. static ssize_t
  1221. store_tabletStylusUpper(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1222. {
  1223. struct aiptek *aiptek = dev_get_drvdata(dev);
  1224. int new_button = map_str_to_val(stylus_button_map, buf, count);
  1225. if (new_button == AIPTEK_INVALID_VALUE)
  1226. return -EINVAL;
  1227. aiptek->newSetting.stylusButtonUpper = new_button;
  1228. return count;
  1229. }
  1230. static DEVICE_ATTR(stylus_upper,
  1231. S_IRUGO | S_IWUSR,
  1232. show_tabletStylusUpper, store_tabletStylusUpper);
  1233. /***********************************************************************
  1234. * support routines for the 'stylus_lower' file. Note that this file
  1235. * both displays current setting and allows for setting changing.
  1236. */
  1237. static ssize_t show_tabletStylusLower(struct device *dev, struct device_attribute *attr, char *buf)
  1238. {
  1239. struct aiptek *aiptek = dev_get_drvdata(dev);
  1240. return snprintf(buf, PAGE_SIZE, "%s\n",
  1241. map_val_to_str(stylus_button_map,
  1242. aiptek->curSetting.stylusButtonLower));
  1243. }
  1244. static ssize_t
  1245. store_tabletStylusLower(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1246. {
  1247. struct aiptek *aiptek = dev_get_drvdata(dev);
  1248. int new_button = map_str_to_val(stylus_button_map, buf, count);
  1249. if (new_button == AIPTEK_INVALID_VALUE)
  1250. return -EINVAL;
  1251. aiptek->newSetting.stylusButtonLower = new_button;
  1252. return count;
  1253. }
  1254. static DEVICE_ATTR(stylus_lower,
  1255. S_IRUGO | S_IWUSR,
  1256. show_tabletStylusLower, store_tabletStylusLower);
  1257. /***********************************************************************
  1258. * support routines for the 'mouse_left' file. Note that this file
  1259. * both displays current setting and allows for setting changing.
  1260. */
  1261. static struct aiptek_map mouse_button_map[] = {
  1262. { "left", AIPTEK_MOUSE_LEFT_BUTTON },
  1263. { "middle", AIPTEK_MOUSE_MIDDLE_BUTTON },
  1264. { "right", AIPTEK_MOUSE_RIGHT_BUTTON },
  1265. { NULL, AIPTEK_INVALID_VALUE }
  1266. };
  1267. static ssize_t show_tabletMouseLeft(struct device *dev, struct device_attribute *attr, char *buf)
  1268. {
  1269. struct aiptek *aiptek = dev_get_drvdata(dev);
  1270. return snprintf(buf, PAGE_SIZE, "%s\n",
  1271. map_val_to_str(mouse_button_map,
  1272. aiptek->curSetting.mouseButtonLeft));
  1273. }
  1274. static ssize_t
  1275. store_tabletMouseLeft(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1276. {
  1277. struct aiptek *aiptek = dev_get_drvdata(dev);
  1278. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1279. if (new_button == AIPTEK_INVALID_VALUE)
  1280. return -EINVAL;
  1281. aiptek->newSetting.mouseButtonLeft = new_button;
  1282. return count;
  1283. }
  1284. static DEVICE_ATTR(mouse_left,
  1285. S_IRUGO | S_IWUSR,
  1286. show_tabletMouseLeft, store_tabletMouseLeft);
  1287. /***********************************************************************
  1288. * support routines for the 'mouse_middle' file. Note that this file
  1289. * both displays current setting and allows for setting changing.
  1290. */
  1291. static ssize_t show_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, char *buf)
  1292. {
  1293. struct aiptek *aiptek = dev_get_drvdata(dev);
  1294. return snprintf(buf, PAGE_SIZE, "%s\n",
  1295. map_val_to_str(mouse_button_map,
  1296. aiptek->curSetting.mouseButtonMiddle));
  1297. }
  1298. static ssize_t
  1299. store_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1300. {
  1301. struct aiptek *aiptek = dev_get_drvdata(dev);
  1302. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1303. if (new_button == AIPTEK_INVALID_VALUE)
  1304. return -EINVAL;
  1305. aiptek->newSetting.mouseButtonMiddle = new_button;
  1306. return count;
  1307. }
  1308. static DEVICE_ATTR(mouse_middle,
  1309. S_IRUGO | S_IWUSR,
  1310. show_tabletMouseMiddle, store_tabletMouseMiddle);
  1311. /***********************************************************************
  1312. * support routines for the 'mouse_right' file. Note that this file
  1313. * both displays current setting and allows for setting changing.
  1314. */
  1315. static ssize_t show_tabletMouseRight(struct device *dev, struct device_attribute *attr, char *buf)
  1316. {
  1317. struct aiptek *aiptek = dev_get_drvdata(dev);
  1318. return snprintf(buf, PAGE_SIZE, "%s\n",
  1319. map_val_to_str(mouse_button_map,
  1320. aiptek->curSetting.mouseButtonRight));
  1321. }
  1322. static ssize_t
  1323. store_tabletMouseRight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1324. {
  1325. struct aiptek *aiptek = dev_get_drvdata(dev);
  1326. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1327. if (new_button == AIPTEK_INVALID_VALUE)
  1328. return -EINVAL;
  1329. aiptek->newSetting.mouseButtonRight = new_button;
  1330. return count;
  1331. }
  1332. static DEVICE_ATTR(mouse_right,
  1333. S_IRUGO | S_IWUSR,
  1334. show_tabletMouseRight, store_tabletMouseRight);
  1335. /***********************************************************************
  1336. * support routines for the 'wheel' file. Note that this file
  1337. * both displays current setting and allows for setting changing.
  1338. */
  1339. static ssize_t show_tabletWheel(struct device *dev, struct device_attribute *attr, char *buf)
  1340. {
  1341. struct aiptek *aiptek = dev_get_drvdata(dev);
  1342. if (aiptek->curSetting.wheel == AIPTEK_WHEEL_DISABLE) {
  1343. return snprintf(buf, PAGE_SIZE, "disable\n");
  1344. } else {
  1345. return snprintf(buf, PAGE_SIZE, "%d\n",
  1346. aiptek->curSetting.wheel);
  1347. }
  1348. }
  1349. static ssize_t
  1350. store_tabletWheel(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1351. {
  1352. struct aiptek *aiptek = dev_get_drvdata(dev);
  1353. int err, w;
  1354. err = kstrtoint(buf, 10, &w);
  1355. if (err)
  1356. return err;
  1357. aiptek->newSetting.wheel = w;
  1358. return count;
  1359. }
  1360. static DEVICE_ATTR(wheel,
  1361. S_IRUGO | S_IWUSR, show_tabletWheel, store_tabletWheel);
  1362. /***********************************************************************
  1363. * support routines for the 'execute' file. Note that this file
  1364. * both displays current setting and allows for setting changing.
  1365. */
  1366. static ssize_t show_tabletExecute(struct device *dev, struct device_attribute *attr, char *buf)
  1367. {
  1368. /* There is nothing useful to display, so a one-line manual
  1369. * is in order...
  1370. */
  1371. return snprintf(buf, PAGE_SIZE,
  1372. "Write anything to this file to program your tablet.\n");
  1373. }
  1374. static ssize_t
  1375. store_tabletExecute(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1376. {
  1377. struct aiptek *aiptek = dev_get_drvdata(dev);
  1378. /* We do not care what you write to this file. Merely the action
  1379. * of writing to this file triggers a tablet reprogramming.
  1380. */
  1381. memcpy(&aiptek->curSetting, &aiptek->newSetting,
  1382. sizeof(struct aiptek_settings));
  1383. if (aiptek_program_tablet(aiptek) < 0)
  1384. return -EIO;
  1385. return count;
  1386. }
  1387. static DEVICE_ATTR(execute,
  1388. S_IRUGO | S_IWUSR, show_tabletExecute, store_tabletExecute);
  1389. /***********************************************************************
  1390. * support routines for the 'odm_code' file. Note that this file
  1391. * only displays current setting.
  1392. */
  1393. static ssize_t show_tabletODMCode(struct device *dev, struct device_attribute *attr, char *buf)
  1394. {
  1395. struct aiptek *aiptek = dev_get_drvdata(dev);
  1396. return snprintf(buf, PAGE_SIZE, "0x%04x\n", aiptek->features.odmCode);
  1397. }
  1398. static DEVICE_ATTR(odm_code, S_IRUGO, show_tabletODMCode, NULL);
  1399. /***********************************************************************
  1400. * support routines for the 'model_code' file. Note that this file
  1401. * only displays current setting.
  1402. */
  1403. static ssize_t show_tabletModelCode(struct device *dev, struct device_attribute *attr, char *buf)
  1404. {
  1405. struct aiptek *aiptek = dev_get_drvdata(dev);
  1406. return snprintf(buf, PAGE_SIZE, "0x%04x\n", aiptek->features.modelCode);
  1407. }
  1408. static DEVICE_ATTR(model_code, S_IRUGO, show_tabletModelCode, NULL);
  1409. /***********************************************************************
  1410. * support routines for the 'firmware_code' file. Note that this file
  1411. * only displays current setting.
  1412. */
  1413. static ssize_t show_firmwareCode(struct device *dev, struct device_attribute *attr, char *buf)
  1414. {
  1415. struct aiptek *aiptek = dev_get_drvdata(dev);
  1416. return snprintf(buf, PAGE_SIZE, "%04x\n",
  1417. aiptek->features.firmwareCode);
  1418. }
  1419. static DEVICE_ATTR(firmware_code, S_IRUGO, show_firmwareCode, NULL);
  1420. static struct attribute *aiptek_attributes[] = {
  1421. &dev_attr_size.attr,
  1422. &dev_attr_pointer_mode.attr,
  1423. &dev_attr_coordinate_mode.attr,
  1424. &dev_attr_tool_mode.attr,
  1425. &dev_attr_xtilt.attr,
  1426. &dev_attr_ytilt.attr,
  1427. &dev_attr_jitter.attr,
  1428. &dev_attr_delay.attr,
  1429. &dev_attr_event_count.attr,
  1430. &dev_attr_diagnostic.attr,
  1431. &dev_attr_odm_code.attr,
  1432. &dev_attr_model_code.attr,
  1433. &dev_attr_firmware_code.attr,
  1434. &dev_attr_stylus_lower.attr,
  1435. &dev_attr_stylus_upper.attr,
  1436. &dev_attr_mouse_left.attr,
  1437. &dev_attr_mouse_middle.attr,
  1438. &dev_attr_mouse_right.attr,
  1439. &dev_attr_wheel.attr,
  1440. &dev_attr_execute.attr,
  1441. NULL
  1442. };
  1443. static struct attribute_group aiptek_attribute_group = {
  1444. .attrs = aiptek_attributes,
  1445. };
  1446. /***********************************************************************
  1447. * This routine is called when a tablet has been identified. It basically
  1448. * sets up the tablet and the driver's internal structures.
  1449. */
  1450. static int
  1451. aiptek_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1452. {
  1453. struct usb_device *usbdev = interface_to_usbdev(intf);
  1454. struct usb_endpoint_descriptor *endpoint;
  1455. struct aiptek *aiptek;
  1456. struct input_dev *inputdev;
  1457. int i;
  1458. int speeds[] = { 0,
  1459. AIPTEK_PROGRAMMABLE_DELAY_50,
  1460. AIPTEK_PROGRAMMABLE_DELAY_400,
  1461. AIPTEK_PROGRAMMABLE_DELAY_25,
  1462. AIPTEK_PROGRAMMABLE_DELAY_100,
  1463. AIPTEK_PROGRAMMABLE_DELAY_200,
  1464. AIPTEK_PROGRAMMABLE_DELAY_300
  1465. };
  1466. int err = -ENOMEM;
  1467. /* programmableDelay is where the command-line specified
  1468. * delay is kept. We make it the first element of speeds[],
  1469. * so therefore, your override speed is tried first, then the
  1470. * remainder. Note that the default value of 400ms will be tried
  1471. * if you do not specify any command line parameter.
  1472. */
  1473. speeds[0] = programmableDelay;
  1474. aiptek = kzalloc(sizeof(struct aiptek), GFP_KERNEL);
  1475. inputdev = input_allocate_device();
  1476. if (!aiptek || !inputdev) {
  1477. dev_warn(&intf->dev,
  1478. "cannot allocate memory or input device\n");
  1479. goto fail1;
  1480. }
  1481. aiptek->data = usb_alloc_coherent(usbdev, AIPTEK_PACKET_LENGTH,
  1482. GFP_ATOMIC, &aiptek->data_dma);
  1483. if (!aiptek->data) {
  1484. dev_warn(&intf->dev, "cannot allocate usb buffer\n");
  1485. goto fail1;
  1486. }
  1487. aiptek->urb = usb_alloc_urb(0, GFP_KERNEL);
  1488. if (!aiptek->urb) {
  1489. dev_warn(&intf->dev, "cannot allocate urb\n");
  1490. goto fail2;
  1491. }
  1492. aiptek->inputdev = inputdev;
  1493. aiptek->usbdev = usbdev;
  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. aiptek->usbdev,
  1583. usb_rcvintpipe(aiptek->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");