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