aiptek.c 62 KB

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