aiptek.c 62 KB

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