aiptek.c 65 KB

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