aiptek.c 62 KB

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