aiptek.c 61 KB

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