aiptek.c 61 KB

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