PageRenderTime 70ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/input/tablet/aiptek.c

http://github.com/mirrors/linux
C | 1931 lines | 1150 code | 269 blank | 512 comment | 216 complexity | a5dd4ab23b21507a4e0621d64e9ee001 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  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 <chris@crud.net>
  7. * Copyright (c) 2002-2004 Bryan W. Headley <bwheadley@earthlink.net>
  8. *
  9. * based on wacom.c by
  10. * Vojtech Pavlik <vojtech@suse.cz>
  11. * Andreas Bach Aaen <abach@stofanet.dk>
  12. * Clifford Wolf <clifford@clifford.at>
  13. * Sam Mosel <sam.mosel@computer.org>
  14. * James E. Blair <corvus@gnu.org>
  15. * Daniel Egger <egger@suse.de>
  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 ((ret =
  838. aiptek_get_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
  839. dev_dbg(&aiptek->intf->dev,
  840. "aiptek_query failed: returned 0x%02x 0x%02x 0x%02x\n",
  841. buf[0], buf[1], buf[2]);
  842. ret = -EIO;
  843. } else {
  844. ret = get_unaligned_le16(buf + 1);
  845. }
  846. kfree(buf);
  847. return ret;
  848. }
  849. /***********************************************************************
  850. * Program the tablet into either absolute or relative mode.
  851. * We also get information about the tablet's size.
  852. */
  853. static int aiptek_program_tablet(struct aiptek *aiptek)
  854. {
  855. int ret;
  856. /* Execute Resolution500LPI */
  857. if ((ret = aiptek_command(aiptek, 0x18, 0x04)) < 0)
  858. return ret;
  859. /* Query getModelCode */
  860. if ((ret = aiptek_query(aiptek, 0x02, 0x00)) < 0)
  861. return ret;
  862. aiptek->features.modelCode = ret & 0xff;
  863. /* Query getODMCode */
  864. if ((ret = aiptek_query(aiptek, 0x03, 0x00)) < 0)
  865. return ret;
  866. aiptek->features.odmCode = ret;
  867. /* Query getFirmwareCode */
  868. if ((ret = aiptek_query(aiptek, 0x04, 0x00)) < 0)
  869. return ret;
  870. aiptek->features.firmwareCode = ret;
  871. /* Query getXextension */
  872. if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
  873. return ret;
  874. input_set_abs_params(aiptek->inputdev, ABS_X, 0, ret - 1, 0, 0);
  875. /* Query getYextension */
  876. if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
  877. return ret;
  878. input_set_abs_params(aiptek->inputdev, ABS_Y, 0, ret - 1, 0, 0);
  879. /* Query getPressureLevels */
  880. if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
  881. return ret;
  882. input_set_abs_params(aiptek->inputdev, ABS_PRESSURE, 0, ret - 1, 0, 0);
  883. /* Depending on whether we are in absolute or relative mode, we will
  884. * do a switchToTablet(absolute) or switchToMouse(relative) command.
  885. */
  886. if (aiptek->curSetting.coordinateMode ==
  887. AIPTEK_COORDINATE_ABSOLUTE_MODE) {
  888. /* Execute switchToTablet */
  889. if ((ret = aiptek_command(aiptek, 0x10, 0x01)) < 0) {
  890. return ret;
  891. }
  892. } else {
  893. /* Execute switchToMouse */
  894. if ((ret = aiptek_command(aiptek, 0x10, 0x00)) < 0) {
  895. return ret;
  896. }
  897. }
  898. /* Enable the macro keys */
  899. if ((ret = aiptek_command(aiptek, 0x11, 0x02)) < 0)
  900. return ret;
  901. #if 0
  902. /* Execute FilterOn */
  903. if ((ret = aiptek_command(aiptek, 0x17, 0x00)) < 0)
  904. return ret;
  905. #endif
  906. /* Execute AutoGainOn */
  907. if ((ret = aiptek_command(aiptek, 0x12, 0xff)) < 0)
  908. return ret;
  909. /* Reset the eventCount, so we track events from last (re)programming
  910. */
  911. aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
  912. aiptek->eventCount = 0;
  913. return 0;
  914. }
  915. /***********************************************************************
  916. * Sysfs functions. Sysfs prefers that individually-tunable parameters
  917. * exist in their separate pseudo-files. Summary data that is immutable
  918. * may exist in a singular file so long as you don't define a writeable
  919. * interface.
  920. */
  921. /***********************************************************************
  922. * support the 'size' file -- display support
  923. */
  924. static ssize_t show_tabletSize(struct device *dev, struct device_attribute *attr, char *buf)
  925. {
  926. struct aiptek *aiptek = dev_get_drvdata(dev);
  927. return snprintf(buf, PAGE_SIZE, "%dx%d\n",
  928. input_abs_get_max(aiptek->inputdev, ABS_X) + 1,
  929. input_abs_get_max(aiptek->inputdev, ABS_Y) + 1);
  930. }
  931. /* These structs define the sysfs files, param #1 is the name of the
  932. * file, param 2 is the file permissions, param 3 & 4 are to the
  933. * output generator and input parser routines. Absence of a routine is
  934. * permitted -- it only means can't either 'cat' the file, or send data
  935. * to it.
  936. */
  937. static DEVICE_ATTR(size, S_IRUGO, show_tabletSize, NULL);
  938. /***********************************************************************
  939. * support routines for the 'pointer_mode' file. Note that this file
  940. * both displays current setting and allows reprogramming.
  941. */
  942. static struct aiptek_map pointer_mode_map[] = {
  943. { "stylus", AIPTEK_POINTER_ONLY_STYLUS_MODE },
  944. { "mouse", AIPTEK_POINTER_ONLY_MOUSE_MODE },
  945. { "either", AIPTEK_POINTER_EITHER_MODE },
  946. { NULL, AIPTEK_INVALID_VALUE }
  947. };
  948. static ssize_t show_tabletPointerMode(struct device *dev, struct device_attribute *attr, char *buf)
  949. {
  950. struct aiptek *aiptek = dev_get_drvdata(dev);
  951. return snprintf(buf, PAGE_SIZE, "%s\n",
  952. map_val_to_str(pointer_mode_map,
  953. aiptek->curSetting.pointerMode));
  954. }
  955. static ssize_t
  956. store_tabletPointerMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  957. {
  958. struct aiptek *aiptek = dev_get_drvdata(dev);
  959. int new_mode = map_str_to_val(pointer_mode_map, buf, count);
  960. if (new_mode == AIPTEK_INVALID_VALUE)
  961. return -EINVAL;
  962. aiptek->newSetting.pointerMode = new_mode;
  963. return count;
  964. }
  965. static DEVICE_ATTR(pointer_mode,
  966. S_IRUGO | S_IWUSR,
  967. show_tabletPointerMode, store_tabletPointerMode);
  968. /***********************************************************************
  969. * support routines for the 'coordinate_mode' file. Note that this file
  970. * both displays current setting and allows reprogramming.
  971. */
  972. static struct aiptek_map coordinate_mode_map[] = {
  973. { "absolute", AIPTEK_COORDINATE_ABSOLUTE_MODE },
  974. { "relative", AIPTEK_COORDINATE_RELATIVE_MODE },
  975. { NULL, AIPTEK_INVALID_VALUE }
  976. };
  977. static ssize_t show_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, char *buf)
  978. {
  979. struct aiptek *aiptek = dev_get_drvdata(dev);
  980. return snprintf(buf, PAGE_SIZE, "%s\n",
  981. map_val_to_str(coordinate_mode_map,
  982. aiptek->curSetting.coordinateMode));
  983. }
  984. static ssize_t
  985. store_tabletCoordinateMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  986. {
  987. struct aiptek *aiptek = dev_get_drvdata(dev);
  988. int new_mode = map_str_to_val(coordinate_mode_map, buf, count);
  989. if (new_mode == AIPTEK_INVALID_VALUE)
  990. return -EINVAL;
  991. aiptek->newSetting.coordinateMode = new_mode;
  992. return count;
  993. }
  994. static DEVICE_ATTR(coordinate_mode,
  995. S_IRUGO | S_IWUSR,
  996. show_tabletCoordinateMode, store_tabletCoordinateMode);
  997. /***********************************************************************
  998. * support routines for the 'tool_mode' file. Note that this file
  999. * both displays current setting and allows reprogramming.
  1000. */
  1001. static struct aiptek_map tool_mode_map[] = {
  1002. { "mouse", AIPTEK_TOOL_BUTTON_MOUSE_MODE },
  1003. { "eraser", AIPTEK_TOOL_BUTTON_ERASER_MODE },
  1004. { "pencil", AIPTEK_TOOL_BUTTON_PENCIL_MODE },
  1005. { "pen", AIPTEK_TOOL_BUTTON_PEN_MODE },
  1006. { "brush", AIPTEK_TOOL_BUTTON_BRUSH_MODE },
  1007. { "airbrush", AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE },
  1008. { "lens", AIPTEK_TOOL_BUTTON_LENS_MODE },
  1009. { NULL, AIPTEK_INVALID_VALUE }
  1010. };
  1011. static ssize_t show_tabletToolMode(struct device *dev, struct device_attribute *attr, char *buf)
  1012. {
  1013. struct aiptek *aiptek = dev_get_drvdata(dev);
  1014. return snprintf(buf, PAGE_SIZE, "%s\n",
  1015. map_val_to_str(tool_mode_map,
  1016. aiptek->curSetting.toolMode));
  1017. }
  1018. static ssize_t
  1019. store_tabletToolMode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1020. {
  1021. struct aiptek *aiptek = dev_get_drvdata(dev);
  1022. int new_mode = map_str_to_val(tool_mode_map, buf, count);
  1023. if (new_mode == AIPTEK_INVALID_VALUE)
  1024. return -EINVAL;
  1025. aiptek->newSetting.toolMode = new_mode;
  1026. return count;
  1027. }
  1028. static DEVICE_ATTR(tool_mode,
  1029. S_IRUGO | S_IWUSR,
  1030. show_tabletToolMode, store_tabletToolMode);
  1031. /***********************************************************************
  1032. * support routines for the 'xtilt' file. Note that this file
  1033. * both displays current setting and allows reprogramming.
  1034. */
  1035. static ssize_t show_tabletXtilt(struct device *dev, struct device_attribute *attr, char *buf)
  1036. {
  1037. struct aiptek *aiptek = dev_get_drvdata(dev);
  1038. if (aiptek->curSetting.xTilt == AIPTEK_TILT_DISABLE) {
  1039. return snprintf(buf, PAGE_SIZE, "disable\n");
  1040. } else {
  1041. return snprintf(buf, PAGE_SIZE, "%d\n",
  1042. aiptek->curSetting.xTilt);
  1043. }
  1044. }
  1045. static ssize_t
  1046. store_tabletXtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1047. {
  1048. struct aiptek *aiptek = dev_get_drvdata(dev);
  1049. int x;
  1050. if (kstrtoint(buf, 10, &x)) {
  1051. size_t len = buf[count - 1] == '\n' ? count - 1 : count;
  1052. if (strncmp(buf, "disable", len))
  1053. return -EINVAL;
  1054. aiptek->newSetting.xTilt = AIPTEK_TILT_DISABLE;
  1055. } else {
  1056. if (x < AIPTEK_TILT_MIN || x > AIPTEK_TILT_MAX)
  1057. return -EINVAL;
  1058. aiptek->newSetting.xTilt = x;
  1059. }
  1060. return count;
  1061. }
  1062. static DEVICE_ATTR(xtilt,
  1063. S_IRUGO | S_IWUSR, show_tabletXtilt, store_tabletXtilt);
  1064. /***********************************************************************
  1065. * support routines for the 'ytilt' file. Note that this file
  1066. * both displays current setting and allows reprogramming.
  1067. */
  1068. static ssize_t show_tabletYtilt(struct device *dev, struct device_attribute *attr, char *buf)
  1069. {
  1070. struct aiptek *aiptek = dev_get_drvdata(dev);
  1071. if (aiptek->curSetting.yTilt == AIPTEK_TILT_DISABLE) {
  1072. return snprintf(buf, PAGE_SIZE, "disable\n");
  1073. } else {
  1074. return snprintf(buf, PAGE_SIZE, "%d\n",
  1075. aiptek->curSetting.yTilt);
  1076. }
  1077. }
  1078. static ssize_t
  1079. store_tabletYtilt(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1080. {
  1081. struct aiptek *aiptek = dev_get_drvdata(dev);
  1082. int y;
  1083. if (kstrtoint(buf, 10, &y)) {
  1084. size_t len = buf[count - 1] == '\n' ? count - 1 : count;
  1085. if (strncmp(buf, "disable", len))
  1086. return -EINVAL;
  1087. aiptek->newSetting.yTilt = AIPTEK_TILT_DISABLE;
  1088. } else {
  1089. if (y < AIPTEK_TILT_MIN || y > AIPTEK_TILT_MAX)
  1090. return -EINVAL;
  1091. aiptek->newSetting.yTilt = y;
  1092. }
  1093. return count;
  1094. }
  1095. static DEVICE_ATTR(ytilt,
  1096. S_IRUGO | S_IWUSR, show_tabletYtilt, store_tabletYtilt);
  1097. /***********************************************************************
  1098. * support routines for the 'jitter' file. Note that this file
  1099. * both displays current setting and allows reprogramming.
  1100. */
  1101. static ssize_t show_tabletJitterDelay(struct device *dev, struct device_attribute *attr, char *buf)
  1102. {
  1103. struct aiptek *aiptek = dev_get_drvdata(dev);
  1104. return snprintf(buf, PAGE_SIZE, "%d\n", aiptek->curSetting.jitterDelay);
  1105. }
  1106. static ssize_t
  1107. store_tabletJitterDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1108. {
  1109. struct aiptek *aiptek = dev_get_drvdata(dev);
  1110. int err, j;
  1111. err = kstrtoint(buf, 10, &j);
  1112. if (err)
  1113. return err;
  1114. aiptek->newSetting.jitterDelay = j;
  1115. return count;
  1116. }
  1117. static DEVICE_ATTR(jitter,
  1118. S_IRUGO | S_IWUSR,
  1119. show_tabletJitterDelay, store_tabletJitterDelay);
  1120. /***********************************************************************
  1121. * support routines for the 'delay' file. Note that this file
  1122. * both displays current setting and allows reprogramming.
  1123. */
  1124. static ssize_t show_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, char *buf)
  1125. {
  1126. struct aiptek *aiptek = dev_get_drvdata(dev);
  1127. return snprintf(buf, PAGE_SIZE, "%d\n",
  1128. aiptek->curSetting.programmableDelay);
  1129. }
  1130. static ssize_t
  1131. store_tabletProgrammableDelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1132. {
  1133. struct aiptek *aiptek = dev_get_drvdata(dev);
  1134. int err, d;
  1135. err = kstrtoint(buf, 10, &d);
  1136. if (err)
  1137. return err;
  1138. aiptek->newSetting.programmableDelay = d;
  1139. return count;
  1140. }
  1141. static DEVICE_ATTR(delay,
  1142. S_IRUGO | S_IWUSR,
  1143. show_tabletProgrammableDelay, store_tabletProgrammableDelay);
  1144. /***********************************************************************
  1145. * support routines for the 'event_count' file. Note that this file
  1146. * only displays current setting.
  1147. */
  1148. static ssize_t show_tabletEventsReceived(struct device *dev, struct device_attribute *attr, char *buf)
  1149. {
  1150. struct aiptek *aiptek = dev_get_drvdata(dev);
  1151. return snprintf(buf, PAGE_SIZE, "%ld\n", aiptek->eventCount);
  1152. }
  1153. static DEVICE_ATTR(event_count, S_IRUGO, show_tabletEventsReceived, NULL);
  1154. /***********************************************************************
  1155. * support routines for the 'diagnostic' file. Note that this file
  1156. * only displays current setting.
  1157. */
  1158. static ssize_t show_tabletDiagnosticMessage(struct device *dev, struct device_attribute *attr, char *buf)
  1159. {
  1160. struct aiptek *aiptek = dev_get_drvdata(dev);
  1161. char *retMsg;
  1162. switch (aiptek->diagnostic) {
  1163. case AIPTEK_DIAGNOSTIC_NA:
  1164. retMsg = "no errors\n";
  1165. break;
  1166. case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
  1167. retMsg = "Error: receiving relative reports\n";
  1168. break;
  1169. case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
  1170. retMsg = "Error: receiving absolute reports\n";
  1171. break;
  1172. case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
  1173. if (aiptek->curSetting.pointerMode ==
  1174. AIPTEK_POINTER_ONLY_MOUSE_MODE) {
  1175. retMsg = "Error: receiving stylus reports\n";
  1176. } else {
  1177. retMsg = "Error: receiving mouse reports\n";
  1178. }
  1179. break;
  1180. default:
  1181. return 0;
  1182. }
  1183. return snprintf(buf, PAGE_SIZE, retMsg);
  1184. }
  1185. static DEVICE_ATTR(diagnostic, S_IRUGO, show_tabletDiagnosticMessage, NULL);
  1186. /***********************************************************************
  1187. * support routines for the 'stylus_upper' file. Note that this file
  1188. * both displays current setting and allows for setting changing.
  1189. */
  1190. static struct aiptek_map stylus_button_map[] = {
  1191. { "upper", AIPTEK_STYLUS_UPPER_BUTTON },
  1192. { "lower", AIPTEK_STYLUS_LOWER_BUTTON },
  1193. { NULL, AIPTEK_INVALID_VALUE }
  1194. };
  1195. static ssize_t show_tabletStylusUpper(struct device *dev, struct device_attribute *attr, char *buf)
  1196. {
  1197. struct aiptek *aiptek = dev_get_drvdata(dev);
  1198. return snprintf(buf, PAGE_SIZE, "%s\n",
  1199. map_val_to_str(stylus_button_map,
  1200. aiptek->curSetting.stylusButtonUpper));
  1201. }
  1202. static ssize_t
  1203. store_tabletStylusUpper(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1204. {
  1205. struct aiptek *aiptek = dev_get_drvdata(dev);
  1206. int new_button = map_str_to_val(stylus_button_map, buf, count);
  1207. if (new_button == AIPTEK_INVALID_VALUE)
  1208. return -EINVAL;
  1209. aiptek->newSetting.stylusButtonUpper = new_button;
  1210. return count;
  1211. }
  1212. static DEVICE_ATTR(stylus_upper,
  1213. S_IRUGO | S_IWUSR,
  1214. show_tabletStylusUpper, store_tabletStylusUpper);
  1215. /***********************************************************************
  1216. * support routines for the 'stylus_lower' file. Note that this file
  1217. * both displays current setting and allows for setting changing.
  1218. */
  1219. static ssize_t show_tabletStylusLower(struct device *dev, struct device_attribute *attr, char *buf)
  1220. {
  1221. struct aiptek *aiptek = dev_get_drvdata(dev);
  1222. return snprintf(buf, PAGE_SIZE, "%s\n",
  1223. map_val_to_str(stylus_button_map,
  1224. aiptek->curSetting.stylusButtonLower));
  1225. }
  1226. static ssize_t
  1227. store_tabletStylusLower(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1228. {
  1229. struct aiptek *aiptek = dev_get_drvdata(dev);
  1230. int new_button = map_str_to_val(stylus_button_map, buf, count);
  1231. if (new_button == AIPTEK_INVALID_VALUE)
  1232. return -EINVAL;
  1233. aiptek->newSetting.stylusButtonLower = new_button;
  1234. return count;
  1235. }
  1236. static DEVICE_ATTR(stylus_lower,
  1237. S_IRUGO | S_IWUSR,
  1238. show_tabletStylusLower, store_tabletStylusLower);
  1239. /***********************************************************************
  1240. * support routines for the 'mouse_left' file. Note that this file
  1241. * both displays current setting and allows for setting changing.
  1242. */
  1243. static struct aiptek_map mouse_button_map[] = {
  1244. { "left", AIPTEK_MOUSE_LEFT_BUTTON },
  1245. { "middle", AIPTEK_MOUSE_MIDDLE_BUTTON },
  1246. { "right", AIPTEK_MOUSE_RIGHT_BUTTON },
  1247. { NULL, AIPTEK_INVALID_VALUE }
  1248. };
  1249. static ssize_t show_tabletMouseLeft(struct device *dev, struct device_attribute *attr, char *buf)
  1250. {
  1251. struct aiptek *aiptek = dev_get_drvdata(dev);
  1252. return snprintf(buf, PAGE_SIZE, "%s\n",
  1253. map_val_to_str(mouse_button_map,
  1254. aiptek->curSetting.mouseButtonLeft));
  1255. }
  1256. static ssize_t
  1257. store_tabletMouseLeft(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1258. {
  1259. struct aiptek *aiptek = dev_get_drvdata(dev);
  1260. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1261. if (new_button == AIPTEK_INVALID_VALUE)
  1262. return -EINVAL;
  1263. aiptek->newSetting.mouseButtonLeft = new_button;
  1264. return count;
  1265. }
  1266. static DEVICE_ATTR(mouse_left,
  1267. S_IRUGO | S_IWUSR,
  1268. show_tabletMouseLeft, store_tabletMouseLeft);
  1269. /***********************************************************************
  1270. * support routines for the 'mouse_middle' file. Note that this file
  1271. * both displays current setting and allows for setting changing.
  1272. */
  1273. static ssize_t show_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, char *buf)
  1274. {
  1275. struct aiptek *aiptek = dev_get_drvdata(dev);
  1276. return snprintf(buf, PAGE_SIZE, "%s\n",
  1277. map_val_to_str(mouse_button_map,
  1278. aiptek->curSetting.mouseButtonMiddle));
  1279. }
  1280. static ssize_t
  1281. store_tabletMouseMiddle(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1282. {
  1283. struct aiptek *aiptek = dev_get_drvdata(dev);
  1284. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1285. if (new_button == AIPTEK_INVALID_VALUE)
  1286. return -EINVAL;
  1287. aiptek->newSetting.mouseButtonMiddle = new_button;
  1288. return count;
  1289. }
  1290. static DEVICE_ATTR(mouse_middle,
  1291. S_IRUGO | S_IWUSR,
  1292. show_tabletMouseMiddle, store_tabletMouseMiddle);
  1293. /***********************************************************************
  1294. * support routines for the 'mouse_right' file. Note that this file
  1295. * both displays current setting and allows for setting changing.
  1296. */
  1297. static ssize_t show_tabletMouseRight(struct device *dev, struct device_attribute *attr, char *buf)
  1298. {
  1299. struct aiptek *aiptek = dev_get_drvdata(dev);
  1300. return snprintf(buf, PAGE_SIZE, "%s\n",
  1301. map_val_to_str(mouse_button_map,
  1302. aiptek->curSetting.mouseButtonRight));
  1303. }
  1304. static ssize_t
  1305. store_tabletMouseRight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1306. {
  1307. struct aiptek *aiptek = dev_get_drvdata(dev);
  1308. int new_button = map_str_to_val(mouse_button_map, buf, count);
  1309. if (new_button == AIPTEK_INVALID_VALUE)
  1310. return -EINVAL;
  1311. aiptek->newSetting.mouseButtonRight = new_button;
  1312. return count;
  1313. }
  1314. static DEVICE_ATTR(mouse_right,
  1315. S_IRUGO | S_IWUSR,
  1316. show_tabletMouseRight, store_tabletMouseRight);
  1317. /***********************************************************************
  1318. * support routines for the 'wheel' file. Note that this file
  1319. * both displays current setting and allows for setting changing.
  1320. */
  1321. static ssize_t show_tabletWheel(struct device *dev, struct device_attribute *attr, char *buf)
  1322. {
  1323. struct aiptek *aiptek = dev_get_drvdata(dev);
  1324. if (aiptek->curSetting.wheel == AIPTEK_WHEEL_DISABLE) {
  1325. return snprintf(buf, PAGE_SIZE, "disable\n");
  1326. } else {
  1327. return snprintf(buf, PAGE_SIZE, "%d\n",
  1328. aiptek->curSetting.wheel);
  1329. }
  1330. }
  1331. static ssize_t
  1332. store_tabletWheel(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1333. {
  1334. struct aiptek *aiptek = dev_get_drvdata(dev);
  1335. int err, w;
  1336. err = kstrtoint(buf, 10, &w);
  1337. if (err)
  1338. return err;
  1339. aiptek->newSetting.wheel = w;
  1340. return count;
  1341. }
  1342. static DEVICE_ATTR(wheel,
  1343. S_IRUGO | S_IWUSR, 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_IWUSR, 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 const 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. dev_warn(&intf->dev,
  1460. "cannot allocate memory or input device\n");
  1461. goto fail1;
  1462. }
  1463. aiptek->data = usb_alloc_coherent(usbdev, AIPTEK_PACKET_LENGTH,
  1464. GFP_KERNEL, &aiptek->data_dma);
  1465. if (!aiptek->data) {
  1466. dev_warn(&intf->dev, "cannot allocate usb buffer\n");
  1467. goto fail1;
  1468. }
  1469. aiptek->urb = usb_alloc_urb(0, GFP_KERNEL);
  1470. if (!aiptek->urb) {
  1471. dev_warn(&intf->dev, "cannot allocate urb\n");
  1472. goto fail2;
  1473. }
  1474. aiptek->inputdev = inputdev;
  1475. aiptek->intf = intf;
  1476. aiptek->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  1477. aiptek->inDelay = 0;
  1478. aiptek->endDelay = 0;
  1479. aiptek->previousJitterable = 0;
  1480. aiptek->lastMacro = -1;
  1481. /* Set up the curSettings struct. Said struct contains the current
  1482. * programmable parameters. The newSetting struct contains changes
  1483. * the user makes to the settings via the sysfs interface. Those
  1484. * changes are not "committed" to curSettings until the user
  1485. * writes to the sysfs/.../execute file.
  1486. */
  1487. aiptek->curSetting.pointerMode = AIPTEK_POINTER_EITHER_MODE;
  1488. aiptek->curSetting.coordinateMode = AIPTEK_COORDINATE_ABSOLUTE_MODE;
  1489. aiptek->curSetting.toolMode = AIPTEK_TOOL_BUTTON_PEN_MODE;
  1490. aiptek->curSetting.xTilt = AIPTEK_TILT_DISABLE;
  1491. aiptek->curSetting.yTilt = AIPTEK_TILT_DISABLE;
  1492. aiptek->curSetting.mouseButtonLeft = AIPTEK_MOUSE_LEFT_BUTTON;
  1493. aiptek->curSetting.mouseButtonMiddle = AIPTEK_MOUSE_MIDDLE_BUTTON;
  1494. aiptek->curSetting.mouseButtonRight = AIPTEK_MOUSE_RIGHT_BUTTON;
  1495. aiptek->curSetting.stylusButtonUpper = AIPTEK_STYLUS_UPPER_BUTTON;
  1496. aiptek->curSetting.stylusButtonLower = AIPTEK_STYLUS_LOWER_BUTTON;
  1497. aiptek->curSetting.jitterDelay = jitterDelay;
  1498. aiptek->curSetting.programmableDelay = programmableDelay;
  1499. /* Both structs should have equivalent settings
  1500. */
  1501. aiptek->newSetting = aiptek->curSetting;
  1502. /* Determine the usb devices' physical path.
  1503. * Asketh not why we always pretend we're using "../input0",
  1504. * but I suspect this will have to be refactored one
  1505. * day if a single USB device can be a keyboard & a mouse
  1506. * & a tablet, and the inputX number actually will tell
  1507. * us something...
  1508. */
  1509. usb_make_path(usbdev, aiptek->features.usbPath,
  1510. sizeof(aiptek->features.usbPath));
  1511. strlcat(aiptek->features.usbPath, "/input0",
  1512. sizeof(aiptek->features.usbPath));
  1513. /* Set up client data, pointers to open and close routines
  1514. * for the input device.
  1515. */
  1516. inputdev->name = "Aiptek";
  1517. inputdev->phys = aiptek->features.usbPath;
  1518. usb_to_input_id(usbdev, &inputdev->id);
  1519. inputdev->dev.parent = &intf->dev;
  1520. input_set_drvdata(inputdev, aiptek);
  1521. inputdev->open = aiptek_open;
  1522. inputdev->close = aiptek_close;
  1523. /* Now program the capacities of the tablet, in terms of being
  1524. * an input device.
  1525. */
  1526. for (i = 0; i < ARRAY_SIZE(eventTypes); ++i)
  1527. __set_bit(eventTypes[i], inputdev->evbit);
  1528. for (i = 0; i < ARRAY_SIZE(absEvents); ++i)
  1529. __set_bit(absEvents[i], inputdev->absbit);
  1530. for (i = 0; i < ARRAY_SIZE(relEvents); ++i)
  1531. __set_bit(relEvents[i], inputdev->relbit);
  1532. __set_bit(MSC_SERIAL, inputdev->mscbit);
  1533. /* Set up key and button codes */
  1534. for (i = 0; i < ARRAY_SIZE(buttonEvents); ++i)
  1535. __set_bit(buttonEvents[i], inputdev->keybit);
  1536. for (i = 0; i < ARRAY_SIZE(macroKeyEvents); ++i)
  1537. __set_bit(macroKeyEvents[i], inputdev->keybit);
  1538. /*
  1539. * Program the input device coordinate capacities. We do not yet
  1540. * know what maximum X, Y, and Z values are, so we're putting fake
  1541. * values in. Later, we'll ask the tablet to put in the correct
  1542. * values.
  1543. */
  1544. input_set_abs_params(inputdev, ABS_X, 0, 2999, 0, 0);
  1545. input_set_abs_params(inputdev, ABS_Y, 0, 2249, 0, 0);
  1546. input_set_abs_params(inputdev, ABS_PRESSURE, 0, 511, 0, 0);
  1547. input_set_abs_params(inputdev, ABS_TILT_X, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
  1548. input_set_abs_params(inputdev, ABS_TILT_Y, AIPTEK_TILT_MIN, AIPTEK_TILT_MAX, 0, 0);
  1549. input_set_abs_params(inputdev, ABS_WHEEL, AIPTEK_WHEEL_MIN, AIPTEK_WHEEL_MAX - 1, 0, 0);
  1550. /* Verify that a device really has an endpoint */
  1551. if (intf->cur_altsetting->desc.bNumEndpoints < 1) {
  1552. dev_err(&intf->dev,
  1553. "interface has %d endpoints, but must have minimum 1\n",
  1554. intf->cur_altsetting->desc.bNumEndpoints);
  1555. err = -EINVAL;
  1556. goto fail3;
  1557. }
  1558. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  1559. /* Go set up our URB, which is called when the tablet receives
  1560. * input.
  1561. */
  1562. usb_fill_int_urb(aiptek->urb,
  1563. usbdev,
  1564. usb_rcvintpipe(usbdev,
  1565. endpoint->bEndpointAddress),
  1566. aiptek->data, 8, aiptek_irq, aiptek,
  1567. endpoint->bInterval);
  1568. aiptek->urb->transfer_dma = aiptek->data_dma;
  1569. aiptek->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1570. /* Program the tablet. This sets the tablet up in the mode
  1571. * specified in newSetting, and also queries the tablet's
  1572. * physical capacities.
  1573. *
  1574. * Sanity check: if a tablet doesn't like the slow programmatic
  1575. * delay, we often get sizes of 0x0. Let's use that as an indicator
  1576. * to try faster delays, up to 25 ms. If that logic fails, well, you'll
  1577. * have to explain to us how your tablet thinks it's 0x0, and yet that's
  1578. * not an error :-)
  1579. */
  1580. for (i = 0; i < ARRAY_SIZE(speeds); ++i) {
  1581. aiptek->curSetting.programmableDelay = speeds[i];
  1582. (void)aiptek_program_tablet(aiptek);
  1583. if (input_abs_get_max(aiptek->inputdev, ABS_X) > 0) {
  1584. dev_info(&intf->dev,
  1585. "Aiptek using %d ms programming speed\n",
  1586. aiptek->curSetting.programmableDelay);
  1587. break;
  1588. }
  1589. }
  1590. /* Murphy says that some day someone will have a tablet that fails the
  1591. above test. That's you, Frederic Rodrigo */
  1592. if (i == ARRAY_SIZE(speeds)) {
  1593. dev_info(&intf->dev,
  1594. "Aiptek tried all speeds, no sane response\n");
  1595. err = -EINVAL;
  1596. goto fail3;
  1597. }
  1598. /* Associate this driver's struct with the usb interface.
  1599. */
  1600. usb_set_intfdata(intf, aiptek);
  1601. /* Set up the sysfs files
  1602. */
  1603. err = sysfs_create_group(&intf->dev.kobj, &aiptek_attribute_group);
  1604. if (err) {
  1605. dev_warn(&intf->dev, "cannot create sysfs group err: %d\n",
  1606. err);
  1607. goto fail3;
  1608. }
  1609. /* Register the tablet as an Input Device
  1610. */
  1611. err = input_register_device(aiptek->inputdev);
  1612. if (err) {
  1613. dev_warn(&intf->dev,
  1614. "input_register_device returned err: %d\n", err);
  1615. goto fail4;
  1616. }
  1617. return 0;
  1618. fail4: sysfs_remove_group(&intf->dev.kobj, &aiptek_attribute_group);
  1619. fail3: usb_free_urb(aiptek->urb);
  1620. fail2: usb_free_coherent(usbdev, AIPTEK_PACKET_LENGTH, aiptek->data,
  1621. aiptek->data_dma);
  1622. fail1: usb_set_intfdata(intf, NULL);
  1623. input_free_device(inputdev);
  1624. kfree(aiptek);
  1625. return err;
  1626. }
  1627. /***********************************************************************
  1628. * Deal with tablet disconnecting from the system.
  1629. */
  1630. static void aiptek_disconnect(struct usb_interface *intf)
  1631. {
  1632. struct aiptek *aiptek = usb_get_intfdata(intf);
  1633. /* Disassociate driver's struct with usb interface
  1634. */
  1635. usb_set_intfdata(intf, NULL);
  1636. if (aiptek != NULL) {
  1637. /* Free & unhook everything from the system.
  1638. */
  1639. usb_kill_urb(aiptek->urb);
  1640. input_unregister_device(aiptek->inputdev);
  1641. sysfs_remove_group(&intf->dev.kobj, &aiptek_attribute_group);
  1642. usb_free_urb(aiptek->urb);
  1643. usb_free_coherent(interface_to_usbdev(intf),
  1644. AIPTEK_PACKET_LENGTH,
  1645. aiptek->data, aiptek->data_dma);
  1646. kfree(aiptek);
  1647. }
  1648. }
  1649. static struct usb_driver aiptek_driver = {
  1650. .name = "aiptek",
  1651. .probe = aiptek_probe,
  1652. .disconnect = aiptek_disconnect,
  1653. .id_table = aiptek_ids,
  1654. };
  1655. module_usb_driver(aiptek_driver);
  1656. MODULE_AUTHOR("Bryan W. Headley/Chris Atenasio/Cedric Brun/Rene van Paassen");
  1657. MODULE_DESCRIPTION("Aiptek HyperPen USB Tablet Driver");
  1658. MODULE_LICENSE("GPL");
  1659. module_param(programmableDelay, int, 0);
  1660. MODULE_PARM_DESC(programmableDelay, "delay used during tablet programming");
  1661. module_param(jitterDelay, int, 0);
  1662. MODULE_PARM_DESC(jitterDelay, "stylus/mouse settlement delay");