aiptek.c 62 KB

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