/kern_oII/drivers/input/tablet/aiptek.c

http://omnia2droid.googlecode.com/ · C · 1952 lines · 1155 code · 270 blank · 527 comment · 217 complexity · 92fb3e7be193e677bb637b88cd2d981c MD5 · raw file

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