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-2003 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  *
30  * NOTE:
31  *      This kernel driver is augmented by the "Aiptek" XFree86 input
32  *      driver for your X server, as well as a GUI Front-end "Tablet Manager".
33  *      These three products are highly interactive with one another,
34  *      so therefore it's easier to document them all as one subsystem.
35  *      Please visit the project's "home page", located at,
36  *      http://aiptektablet.sourceforge.net.
37  *
38  */
39 
40 /*
41  * This program is free software; you can redistribute it and/or modify
42  * it under the terms of the GNU General Public License as published by
43  * the Free Software Foundation; either version 2 of the License, or
44  * (at your option) any later version.
45  *
46  * This program is distributed in the hope that it will be useful,
47  * but WITHOUT ANY WARRANTY; without even the implied warranty of
48  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49  * GNU General Public License for more details.
50  *
51  * You should have received a copy of the GNU General Public License
52  * along with this program; if not, write to the Free Software
53  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
54  */
55 
56 #include <linux/kernel.h>
57 #include <linux/slab.h>
58 #include <linux/input.h>
59 #include <linux/module.h>
60 #include <linux/init.h>
61 #include <linux/usb.h>
62 #include <linux/proc_fs.h>
63 #include <asm/uaccess.h>
64 
65 /*
66  * Version Information
67  */
68 #define DRIVER_VERSION "v1.0 Mar-8-2003"
69 #define DRIVER_AUTHOR  "Bryan W. Headley/Chris Atenasio"
70 #define DRIVER_DESC    "Aiptek HyperPen USB Tablet Driver (Linux 2.4.x)"
71 
72 MODULE_AUTHOR(DRIVER_AUTHOR);
73 MODULE_DESCRIPTION(DRIVER_DESC);
74 MODULE_LICENSE("GPL");
75 
76 /*
77  * Aiptek status packet:
78  * (returned as Report 1)
79  *
80  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
81  * byte0   0     0     0     0     0     0     1     0
82  * byte1  X7    X6    X5    X4    X3    X2    X1    X0
83  * byte2  X15   X14   X13   X12   X11   X10   X9    X8
84  * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
85  * byte4  Y15   Y14   Y13   Y12   Y11   Y10   Y9    Y8
86  * byte5   *     *     *    BS2   BS1   Tip   DV    IR
87  * byte6  P7    P6    P5    P4    P3    P2    P1    P0
88  * byte7  P15   P14   P13   P12   P11   P10   P9    P8
89  *
90  * IR: In Range = Proximity on
91  * DV = Data Valid
92  * BS = Barrel Switch (as in, macro keys)
93  * BS2 also referred to as Tablet Pick
94  *
95  * Command Summary:
96  *
97  * Use report_type CONTROL (3)
98  * Use report_id   2
99  *
100  * Command/Data    Description     Return Bytes    Return Value
101  * 0x10/0x00       SwitchToMouse       0
102  * 0x10/0x01       SwitchToTablet      0
103  * 0x18/0x04       Resolution500LPI    0
104  * 0x17/0x00       FilterOn            0
105  * 0x12/0xFF       AutoGainOn          0
106  * 0x01/0x00       GetXExtension       2           MaxX
107  * 0x01/0x01       GetYExtension       2           MaxY
108  * 0x02/0x00       GetModelCode        2           ModelCode = LOBYTE
109  * 0x03/0x00       GetODMCode          2           ODMCode
110  * 0x08/0x00       GetPressureLevels   2           =512
111  * 0x04/0x00       GetFirmwareVersion  2           Firmware Version
112  * 0x11/0x02       EnableMacroKeys     0
113  *
114  *
115  * To initialize the tablet:
116  *
117  * (1) Send Resolution500LPI (Command)
118  * (2) Query for Model code (Option Report)
119  * (3) Query for ODM code (Option Report)
120  * (4) Query for firmware (Option Report)
121  * (5) Query for GetXExtension (Option Report)
122  * (6) Query for GetYExtension (Option Report)
123  * (7) Query for GetPressureLevels (Option Report)
124  * (8) SwitchToTablet for Absolute coordinates, or
125  *     SwitchToMouse for Relative coordinates (Command)
126  * (9) EnableMacroKeys (Command)
127  * (10) FilterOn (Command)
128  * (11) AutoGainOn (Command)
129  *
130  * (Step 9 can be omitted, but you'll then have no function keys.)
131  *
132  *  The procfs interface
133  *  --------------------
134  *
135  *  This driver supports delivering configuration/status reports
136  *  through {procfs}/driver/usb/aiptek. ("procfs" is normally mounted
137  *  to /proc.) Said file can be found while the driver is active in
138  *  memory; it will be removed when the driver is removed, either
139  *  through user intervention (rmmod aiptek) or through software
140  *  such as "hotplug".
141  *
142  *  Reading from the Procfs interface
143  *  ---------------------------------
144  *
145  *  The user may determine the status of the tablet by reading the
146  *  report in the procfs interface, /proc/driver/usb/aiptek.
147  *  The report as of driver version 1.0, looks like,
148  *
149  * Aiptek Tablet (3000x2250, 8.00x6.00", 202x152mm)
150  * (USB VendorID 0x08ca, ProductID 0x0020, ODMCode 0x0004
151  *  ModelCode: 0x64, FirmwareCode: 0x0400)
152  * on /dev/input/event0
153  * pointer=either
154  * coordinate=absolute
155  * tool=pen
156  * xtilt=disable
157  * ytilt=disable
158  * jitter=50
159  * diagnostic=none
160  * eventsReceived=0
161  *
162  *  (spurious ", for the benefit of vim's syntax highlighting.)
163  *
164  *  This report indicates the tablet recognized. (Because Aiptek reuses
165  *  the USB 'productID' over several tablets, it's pointless for us to
166  *  guess which model you have: we'll instead tell you the size of
167  *  the tablet's drawing area, which we indicate in coordinates, inches,
168  *  and millimeters.) We also indicate datum read from the USB interface,
169  *  such as vendorId, productId, ODMcode, etc. It's there "just in case."
170  *
171  *      on /dev/input/event0
172  *
173  *  Linux supports HID-compliant USB devices (such as this tablet) by
174  *  transposing their reports to the Linux Input Event System format. Which
175  *  means, if you want to data from the tablet, that's where it will be
176  *  made available from. For information on the Input Event System, see
177  *  the docs in ./Documentation/input, in the kernel source tree.
178  *
179  *  And yes, depending on the order in which other supported Input Event
180  *  devices are recognized and configured, the tablet may be allocated
181  *  to a different device driver name: it's all dynamic. Use of the devfs
182  *  file system is a help.
183  *
184  *  The keyword=value part of the report mostly shows what the programmable
185  *  parameters have been set to. We describe those below, and how to
186  *  program/reprogram them. Note: tablet parameters are to be programmed
187  *  while the tablet is attached and active. They are not set as arguments
188  *  to the kernel during bootup.
189  *
190  *  Here are the "read-only" parameters, and what they mean:
191  *
192  *      diagnostic=stringValue
193  *      eventsReceived=numericValue
194  *
195  * diagnostic: The tablet driver attempts to explain why things are not
196  *      working correctly. (To the best of it's insular abilities)
197  *
198  *      By default, the tablet boots up in Relative Coordinate
199  *      mode. This driver initially attempts to program it in Absolute
200  *      Coordinate mode (and of course, the user can subsequently choose
201  *      which mode they want.) So, therefore, the situation can arise
202  *      where the tablet is in one mode, and the driver believes it
203  *      is in the other mode. The driver, however, cannot divine
204  *      this mismatch until input events are received.
205  *      Two reports indicate such mode-mismatches between the tablet
206  *      and the driver, and are,
207  *
208  *          "tablet sending relative reports"
209  *          "tablet sending absolute reports"
210  *
211  *      The next diagnostic operates in conjunction with the "pointer="
212  *      programmable parameter. With it, you can indicate that you want
213  *      the tablet to only accept reports from the stylus, or only from the
214  *      mouse. (You can also specify to allow reports from either.) What
215  *      happens when you specify that you only want mouse reports, yet
216  *      the tablet keeps receiving reports from the stylus? Well, first,
217  *      it's a "pilot error", but secondly, it tries to diagnose the issue
218  *      with the following reports,
219  *
220  *          "tablet seeing reports from stylus"
221  *          "tablet seeing reports from mouse"
222  *
223  *      What if there is nothing to report? The inference in the diagnostic
224  *      reports is that something is happening which shouldn't: when things
225  *      appear to be working right, the report is,
226  *
227  *          "none"
228  *
229  *      The error diagnostic report is dynamic: it only reports issues
230  *      that are happening, or have happened as of the last event received.
231  *      It will reset following any attempt to reprogram the tablet's mode.
232  *
233  * eventsReceived: Occasionally, your movements on the tablet are not being
234  *      reported. Usually, this indicates that your tablet is out of sync
235  *      with the USB interface driver, or itself is not sending reports
236  *      out. To help diagnose this, we keep an active count of events
237  *      received from the tablet. So, if you move the stylus, and yet
238  *      your client application doesn't notice, make
239  *      note of the eventsReceived, and then move the stylus again. If the
240  *      event counter's number doesn't change, then the tablet indeed has
241  *      "froze".
242  *
243  *      We have found that sending the tablet a command sequence often
244  *      will clear up "frozen" tablets. Which segues into the section
245  *      about how to program your tablet through the procfs interface,
246  *
247  *  Writing to the procfs interface
248  *  -------------------------------
249  *
250  *  The user may configure the tablet by writing ASCII
251  *  commands to the /proc/driver/usb/aiptek file. Commands which are
252  *  accepted are,
253  *
254  *      pointer=stringvalue      {stylus|mouse|either}
255  *      coordinate=stringvalue   {absolute|relative}
256  *      tool=stringvalue         {mouse|rubber|pen|pencil|brush|airbrush}
257  *      xtilt=string_or_numeric  {disable|[-128..127]}
258  *      ytilt=string_or_numeric  {disable|[-128..127]}
259  *      jitter=numericvalue      {0..xxx}
260  *
261  *  pointer: you can specify that reports are to be excepted ONLY from the
262  *      stylus, or ONLY from the mouse. 'either' allows reports from either
263  *      device to be accepted, and is the default.
264  *  coordinate: you can specify that either absolute or relative coordinate
265  *      reports are issued by the tablet. By default, absolute reports are
266  *      sent.
267  *  tool: The stylus by default prepends TOOL_BTN_PEN events with it's
268  *      reports. But you may decide that you want your stylus to behave
269  *      like an eraser (named 'rubber', following tablet conventions,)
270  *      or a pencil, etc. The behavior is dependent upon the client software
271  *      consuming the tablet's events, e.g., the XFree86 tablet driver.
272  *  xtilt: By default this is disabled. However, other tablets have a notion
273  *      of measuring the angle at which the stylus pen is held against the
274  *      drawing surface, along the X axis. Aiptek tablets cannot sense this,
275  *      but if you want to send "held-at-angle" reports, specify the value,
276  *      an integer between -128 and 127 (inclusive) that you want to send.
277  *      This data will be sent along with regular tablet input. Obviously,
278  *      the inference here is that your hand does not change angles
279  *      while drawing (until you go back to this procfs interface, and
280  *      change the value)!
281  *
282  *      When you consider actual drawing tools (real pens, brushes),
283  *      knowing the tools' tip shape and the angle that you hold the tool
284  *      becomes important, insofar as calculating the surface of the tip
285  *      that actually touches the surface of the paper. Knowledge of what
286  *      to do with xtilt reports is solely in the realm of your client
287  *      software.
288  *
289  *      Yes, there is a difference between xtilt=0 and xtilt=disable
290  *      settings. The former sends a report that the angle is a 0;
291  *      the other indicates that NO xtilt reports are to be sent at all.
292  *  ytilt: By default this is disabled. This provides similar functionality
293  *      to xtilt, except that we're measuring the angle the stylus pen is
294  *      held against the drawing surface, along the Y axis. Same cavaets
295  *      apply as for xtilt.
296  *  jitter: By default, this is set to 50. When pressing a button on
297  *      either the mouse or the stylus pen, you will probably notice that
298  *      the tool moves slightly from it's original position, until your
299  *      hand steadies it. During that period of time, the pen is "jittering",
300  *      sending spurious movement events that perhaps you'd like it not to
301  *      send. What we do is set a moratorium, measured in milliseconds,
302  *      during which we do not send movement events. So, the default is 50ms;
303  *      you obviously can set it to zero or incredibly unreasonable values
304  *      (no reports for 4 seconds following the pressing of a stylus button!)
305  *
306  * Interesting Side-Note
307  * ---------------------
308  *
309  *  The tablet has "frozen" and you'd like to send it a command to wake it
310  *  up. But you don't want to change how the driver's currently configured.
311  *
312  *  1. Send a command to /proc/driver/usb/aiptek with the same setting
313  *     already reported by the driver.
314  *  2. Send an illegal string to procfs file ("wakeup=now" is always good)
315  *  3. Because, the driver always attempts to reprogram the tablet to it's
316  *     current settings following a write to the procfs interface.
317  *
318  *  Hmm, still does not work.
319  *  -------------------------
320  *
321  *  This is slightly harder to diagnose. You may be receiving frame errors
322  *  from the USB interface driver (see /var/log/messages for any diagnostics).
323  *
324  *  Alternatively, you may be running something like 'hotplug' that attempts
325  *  to match discovered USB devices to it's list of device drivers.
326  *  Unfortunately, because this is a tablet that can send relative X,Y events,
327  *  it "looks like" a mouse! A usb mouse driver may have possession of
328  *  input from the tablet. On the other hand, the tablet also supports
329  *  absolute reports from barrel switches, which sounds a lot like a "joystick",
330  *  and the software again can be fooled into loading the wrong driver for
331  *  the tablet. The distinction is, USB HID devices tell you what they
332  *  are capable of, rather than what they are.
333  *
334  *  Come visit this driver's home page at http://aiptektablet.sourceforge.net
335  *  for further assistance.
336  */
337 
338 #define USB_VENDOR_ID_AIPTEK   0x08ca
339 
340 #define AIPTEK_POINTER_ONLY_MOUSE_MODE      0
341 #define AIPTEK_POINTER_ONLY_STYLUS_MODE     1
342 #define AIPTEK_POINTER_EITHER_MODE          2
343 
344 #define AIPTEK_POINTER_ALLOW_MOUSE_MODE(a) \
345         (a == AIPTEK_POINTER_ONLY_MOUSE_MODE || \
346          a == AIPTEK_POINTER_EITHER_MODE)
347 #define AIPTEK_POINTER_ALLOW_STYLUS_MODE(a) \
348         (a == AIPTEK_POINTER_ONLY_STYLUS_MODE || \
349          a == AIPTEK_POINTER_EITHER_MODE)
350 
351 #define AIPTEK_COORDINATE_RELATIVE_MODE 0
352 #define AIPTEK_COORDINATE_ABSOLUTE_MODE 1
353 
354 #define AIPTEK_TILT_MIN                      (-128)
355 #define AIPTEK_TILT_MAX                      127
356 #define AIPTEK_TILT_DISABLE                  (-10101)
357 
358 #define AIPTEK_TOOL_BUTTON_PEN_MODE          0
359 #define AIPTEK_TOOL_BUTTON_PENCIL_MODE       1
360 #define AIPTEK_TOOL_BUTTON_BRUSH_MODE        2
361 #define AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE     3
362 #define AIPTEK_TOOL_BUTTON_RUBBER_MODE       4
363 #define AIPTEK_TOOL_BUTTON_MOUSE_MODE        5
364 
365 #define AIPTEK_DIAGNOSTIC_NA                 0
366 #define AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE  1
367 #define AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE  2
368 #define AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED               3
369 
370     // Time to wait (in ms) to help mask hand jittering
371     // when pressing the stylus buttons.
372 #define AIPTEK_JITTER_DELAY_DEFAULT         50
373 
374 struct aiptek_features {
375 	char *name;
376 	int pktlen;
377 	int x_max;
378 	int y_max;
379 	int pressure_max;
380 	int odmCode;
381 	int modelCode;
382 	int firmwareCode;
383 	void (*irq) (struct urb * urb);
384 };
385 
386 struct aiptek {
387 	signed char data[10];
388 	struct input_dev dev;
389 	struct usb_device *usbdev;
390 	struct urb *irq;
391 	struct aiptek_features *features;
392 	unsigned int ifnum;
393 	int open_count;
394 	int pointer_mode;
395 	int coordinate_mode;
396 	int tool_mode;
397 	int xTilt;
398 	int yTilt;
399 	int diagnostic;
400 	unsigned long eventCount;
401 	int jitterDelay;
402 #ifdef CONFIG_PROC_FS
403 	struct proc_dir_entry *usbProcfsEntry;
404 	struct proc_dir_entry *aiptekProcfsEntry;
405 #endif
406 };
407 
408 /*
409  * Permit easy lookup of keyboard events to send, versus
410  * the bitmap which comes from the tablet. This hides the
411  * issue that the F_keys are not sequentially numbered.
412  */
413 static int macroKeyEvents[] = { KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6,
414 	KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12,
415 	KEY_F13, KEY_F14, KEY_F15, KEY_F16, KEY_F17, KEY_F18,
416 	KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23, KEY_F24,
417 	KEY_STOP, KEY_AGAIN, KEY_PROPS, KEY_UNDO, KEY_FRONT, KEY_COPY,
418 	KEY_OPEN, KEY_PASTE, 0
419 };
420 
421 #ifdef CONFIG_PROC_FS
422 extern struct proc_dir_entry *proc_root_driver;
423 #endif
424 
425 static int
aiptek_convert_from_2s_complement(unsigned char c)426 aiptek_convert_from_2s_complement(unsigned char c)
427 {
428 	unsigned char b = c;
429 	int negate = 0;
430 	int ret;
431 
432 	if (b & 0x80) {
433 		b = ~b;
434 		b--;
435 		negate = 1;
436 	}
437 	ret = b;
438 	ret = (negate == 1) ? -ret : ret;
439 	return ret;
440 }
441 
442 /*
443  * aiptek_irq can receive one of six potential reports.
444  * The documentation for each is in the body of the function.
445  *
446  * The tablet reports on several attributes per invocation of
447  * aiptek_irq. Because the Linux Input Event system allows the
448  * transmission of ONE attribute per input_report_xxx() call,
449  * collation has to be done on the other end to reconstitute
450  * a complete tablet report. Further, the number of Input Event reports
451  * submitted varies, depending on what USB report type, and circumstance.
452  * To deal with this, EV_MSC is used to indicate an 'end-of-report'
453  * message. This has been an undocumented convention understood by the kernel
454  * tablet driver and clients such as gpm and XFree86's tablet drivers.
455  *
456  * Of the information received from the tablet, the one piece I
457  * cannot transmit is the proximity bit (without resorting to an EV_MSC
458  * convention above.) I therefore have taken over REL_MISC and ABS_MISC
459  * (for relative and absolute reports, respectively) for communicating
460  * Proximity. Why two events? I thought it interesting to know if the
461  * Proximity event occured while the tablet was in absolute or relative
462  * mode.
463  *
464  * Other tablets use the notion of a certain minimum stylus pressure
465  * to infer proximity. While that could have been done, that is yet
466  * another 'by convention' behavior, the documentation for which
467  * would be spread between two (or more) pieces of software.
468  *
469  * EV_MSC usage is terminated in Linux 2.5.x.
470  */
471 
472 static void
aiptek_irq(struct urb * urb)473 aiptek_irq(struct urb *urb)
474 {
475 	struct aiptek *aiptek = urb->context;
476 	unsigned char *data = aiptek->data;
477 	struct input_dev *dev = &aiptek->dev;
478 	int jitterable = 0;
479 
480 	if (urb->status)
481 		return;
482 
483 	aiptek->eventCount++;
484 
485 	// Report 1 delivers relative coordinates with either a stylus
486 	// or the mouse. You do not know which tool generated the event.
487 	if (data[0] == 1) {
488 		if (aiptek->coordinate_mode == AIPTEK_COORDINATE_ABSOLUTE_MODE) {
489 			aiptek->diagnostic =
490 			    AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE;
491 		} else {
492 			int x, y, left, right, middle;
493 
494 			if (aiptek->tool_mode != AIPTEK_TOOL_BUTTON_MOUSE_MODE) {
495 				aiptek->tool_mode =
496 				    AIPTEK_TOOL_BUTTON_MOUSE_MODE;
497 				input_report_key(dev, BTN_TOOL_MOUSE, 1);
498 			}
499 			x = aiptek_convert_from_2s_complement(data[2]);
500 			y = aiptek_convert_from_2s_complement(data[3]);
501 
502 			left = data[5] & 0x01;
503 			right = data[5] & 0x02;
504 			middle = data[5] & 0x04;
505 
506 			jitterable = left | right | middle;
507 
508 			input_report_key(dev, BTN_LEFT, left);
509 			input_report_key(dev, BTN_MIDDLE, middle);
510 			input_report_key(dev, BTN_RIGHT, right);
511 			input_report_rel(dev, REL_X, x);
512 			input_report_rel(dev, REL_Y, y);
513 			input_report_rel(dev, REL_MISC, 1);
514 
515 			input_event(dev, EV_MSC, MSC_SERIAL, 0);
516 		}
517 	}
518 	// Report 2 is delivered only by the stylus, and delivers
519 	// absolute coordinates.
520 	else if (data[0] == 2) {
521 		if (aiptek->coordinate_mode == AIPTEK_COORDINATE_RELATIVE_MODE) {
522 			aiptek->diagnostic =
523 			    AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
524 		} else
525 		    if (!AIPTEK_POINTER_ALLOW_STYLUS_MODE(aiptek->pointer_mode))
526 		{
527 			aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
528 		} else {
529 			int x = ((__u32) data[1]) | ((__u32) data[2] << 8);
530 			int y = ((__u32) data[3]) | ((__u32) data[4] << 8);
531 			int z = ((__u32) data[6]) | ((__u32) data[7] << 8);
532 
533 			int p = data[5] & 0x01;
534 			int dv = data[5] & 0x02;
535 			int tip = data[5] & 0x04;
536 			int bs = data[5] & 0x08;
537 			int pck = data[5] & 0x10;
538 
539 			// dv indicates 'data valid' (e.g., the tablet is in sync
540 			// and has delivered a "correct" report) We will ignore
541 			// all 'bad' reports...
542 			if (dv != 0) {
543 				switch (aiptek->tool_mode) {
544 				case AIPTEK_TOOL_BUTTON_PEN_MODE:
545 					{
546 						input_report_key(dev,
547 								 BTN_TOOL_PEN,
548 								 1);
549 					}
550 					break;
551 
552 				case AIPTEK_TOOL_BUTTON_PENCIL_MODE:
553 					{
554 						input_report_key(dev,
555 								 BTN_TOOL_PENCIL,
556 								 1);
557 					}
558 					break;
559 
560 				case AIPTEK_TOOL_BUTTON_BRUSH_MODE:
561 					{
562 						input_report_key(dev,
563 								 BTN_TOOL_BRUSH,
564 								 1);
565 					}
566 					break;
567 
568 				case AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE:
569 					{
570 						input_report_key(dev,
571 								 BTN_TOOL_AIRBRUSH,
572 								 1);
573 					}
574 					break;
575 
576 				case AIPTEK_TOOL_BUTTON_RUBBER_MODE:
577 					{
578 						input_report_key(dev,
579 								 BTN_TOOL_RUBBER,
580 								 1);
581 					}
582 					break;
583 
584 				case AIPTEK_TOOL_BUTTON_MOUSE_MODE:
585 					{
586 						input_report_key(dev,
587 								 BTN_TOOL_MOUSE,
588 								 1);
589 					}
590 					break;
591 				}
592 
593 				input_report_abs(dev, ABS_X, x);
594 				input_report_abs(dev, ABS_Y, y);
595 
596 				/*
597 				 * The user is allowed to switch from one of the
598 				 * stylus tools to the Mouse using the front-end GUI.
599 				 * An issue that will arise, however, is what happens
600 				 * when the user HAS issued a TOOL_BTN_MOUSE, but has not
601 				 * yet swapped tools. Well, we can "pretend" to be a mouse
602 				 * by sending overriding tip, barrelswitch and pick.
603 				 * This stupidity should not be used as an excuse not
604 				 * to physically move your Aiptek mouse into the tablet's
605 				 * active area -- it merely provides momentary convenience
606 				 * during that transition.
607 				 */
608 				if (aiptek->tool_mode ==
609 				    AIPTEK_TOOL_BUTTON_MOUSE_MODE) {
610 					input_report_key(dev, BTN_LEFT, tip);
611 					input_report_key(dev, BTN_RIGHT, bs);
612 					input_report_key(dev, BTN_MIDDLE, pck);
613 
614 					jitterable = tip | bs | pck;
615 				} else {
616 					input_report_abs(dev, ABS_PRESSURE, z);
617 
618 					input_report_key(dev, BTN_TOUCH, tip);
619 					input_report_key(dev, BTN_STYLUS, bs);
620 					input_report_key(dev, BTN_STYLUS2, pck);
621 
622 					jitterable = tip | bs | pck;
623 
624 					if (aiptek->xTilt !=
625 					    AIPTEK_TILT_DISABLE)
626 						input_report_abs(dev,
627 								 ABS_TILT_X,
628 								 aiptek->xTilt);
629 					if (aiptek->yTilt !=
630 					    AIPTEK_TILT_DISABLE)
631 						input_report_abs(dev,
632 								 ABS_TILT_Y,
633 								 aiptek->yTilt);
634 				}
635 				input_report_abs(dev, ABS_MISC, p);
636 				input_event(dev, EV_MSC, MSC_SERIAL, 0);
637 			}
638 		}
639 	}
640 	// Report 3's come from the mouse in absolute mode.
641 	else if (data[0] == 3) {
642 		if (aiptek->coordinate_mode == AIPTEK_COORDINATE_RELATIVE_MODE) {
643 			aiptek->diagnostic =
644 			    AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE;
645 		} else
646 		    if (!AIPTEK_POINTER_ALLOW_MOUSE_MODE(aiptek->pointer_mode))
647 		{
648 			aiptek->diagnostic = AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED;
649 		} else {
650 			int x = ((__u32) data[1]) | ((__u32) data[2] << 8);
651 			int y = ((__u32) data[3]) | ((__u32) data[4] << 8);
652 			int p = data[5] & 0x01;
653 			int dv = data[5] & 0x02;
654 			int left = data[5] & 0x04;
655 			int right = data[5] & 0x08;
656 			int middle = data[5] & 0x10;
657 
658 			if (dv != 0) {
659 				input_report_key(dev, BTN_TOOL_MOUSE, 1);
660 				input_report_abs(dev, ABS_X, x);
661 				input_report_abs(dev, ABS_Y, y);
662 
663 				input_report_key(dev, BTN_LEFT, left);
664 				input_report_key(dev, BTN_MIDDLE, middle);
665 				input_report_key(dev, BTN_RIGHT, right);
666 
667 				jitterable = left | middle | right;
668 
669 				input_report_rel(dev, REL_MISC, p);
670 				input_event(dev, EV_MSC, MSC_SERIAL, 0);
671 			}
672 		}
673 	}
674 	// Report 4s come from the macro keys when pressed by stylus
675 	else if (data[0] == 4) {
676 		int p = data[1] & 0x01;
677 		int dv = data[1] & 0x02;
678 		int tip = data[1] & 0x04;
679 		int bs = data[1] & 0x08;
680 		int pck = data[1] & 0x10;
681 
682 		int m = data[3];
683 		int z = ((__u32) data[4]) | ((__u32) data[5] << 8);
684 
685 		if (dv != 0) {
686 			input_report_key(dev, BTN_TOUCH, tip);
687 			input_report_key(dev, BTN_STYLUS, bs);
688 			input_report_key(dev, BTN_STYLUS2, pck);
689 
690 			jitterable = tip | bs | pck;
691 
692 			input_report_key(dev, macroKeyEvents[m - 1], 1);
693 			input_report_abs(dev, ABS_PRESSURE, z);
694 			input_report_abs(dev, ABS_MISC, p);
695 			input_event(dev, EV_MSC, MSC_SERIAL, 0);
696 		}
697 	}
698 	// Report 5s come from the macro keys when pressed by mouse
699 	else if (data[0] == 5) {
700 		int p = data[1] & 0x01;
701 		int dv = data[1] & 0x02;
702 		int left = data[1] & 0x04;
703 		int right = data[1] & 0x08;
704 		int middle = data[1] & 0x10;
705 		int macro = data[3];
706 
707 		if (dv != 0) {
708 			input_report_key(dev, BTN_LEFT, left);
709 			input_report_key(dev, BTN_MIDDLE, middle);
710 			input_report_key(dev, BTN_RIGHT, right);
711 
712 			jitterable = left | middle | right;
713 
714 			input_report_key(dev, macroKeyEvents[macro - 1], 1);
715 			input_report_rel(dev, ABS_MISC, p);
716 			input_event(dev, EV_MSC, MSC_SERIAL, 0);
717 		}
718 	}
719 	// We have no idea which tool can generate a report 6. Theoretically,
720 	// neither need to, having been given reports 4 & 5 for such use.
721 	// However, report 6 is the 'official-looking' report for macroKeys;
722 	// reports 4 & 5 supposively are used to support unnamed, unknown
723 	// hat switches (which just so happen to be the macroKeys.)
724 	else if (data[0] == 6) {
725 		int macro = ((__u32) data[1]) | ((__u32) data[2] << 8);
726 
727 		input_report_key(dev, macroKeyEvents[macro - 1], 1);
728 		input_report_abs(dev, ABS_MISC, 1);
729 		input_event(dev, EV_MSC, MSC_SERIAL, 0);
730 	} else {
731 		dbg("Unknown report %d", data[0]);
732 	}
733 
734 	// Jitter may occur when the user presses a button on the stlyus
735 	// or the mouse. What we do to prevent that is wait 'x' milliseconds
736 	// following a 'jitterable' event, which should give the hand some time
737 	// stabilize itself.
738 	if (jitterable != 0 && aiptek->jitterDelay != 0) {
739 		wait_ms(aiptek->jitterDelay);
740 	}
741 }
742 
743 /*
744  * We are not able to reliably determine the tablet featureset by
745  * asking for the USB productID. Therefore, we will query the
746  * tablet dynamically and populate the struct in aiptek_probe().
747  */
748 
749 struct aiptek_features aiptek_features[] = {
750 	{"Aiptek", 8, 0, 0, 0, 0, 0, 0, aiptek_irq},
751 	{NULL, 0}
752 };
753 
754 /*
755  * These are the USB id's known so far. We do not identify them to
756  * specific Aiptek model numbers, because there has been overlaps,
757  * use, and reuse of id's in existing models. Certain models have
758  * been known to use more than one ID, indicative perhaps of
759  * manufacturing revisions. In any event, we consider these
760  * IDs to not be model-specific nor unique.
761  */
762 
763 struct usb_device_id aiptek_ids[] = {
764       {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x01), driver_info:0},
765       {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x10), driver_info:0},
766       {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20), driver_info:0},
767       {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x21), driver_info:0},
768       {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x22), driver_info:0},
769       {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x23), driver_info:0},
770       {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x24), driver_info:0},
771 	{}
772 };
773 
774 MODULE_DEVICE_TABLE(usb, aiptek_ids);
775 
776 static int
aiptek_open(struct input_dev * dev)777 aiptek_open(struct input_dev *dev)
778 {
779 	struct aiptek *aiptek = dev->private;
780 	if (aiptek->open_count++)
781 		return 0;
782 
783 	aiptek->irq->dev = aiptek->usbdev;
784 	if (usb_submit_urb(aiptek->irq))
785 		return -EIO;
786 
787 	return 0;
788 }
789 
790 static void
aiptek_close(struct input_dev * dev)791 aiptek_close(struct input_dev *dev)
792 {
793 	struct aiptek *aiptek = dev->private;
794 
795 	if (!--aiptek->open_count)
796 		usb_unlink_urb(aiptek->irq);
797 }
798 
799 /*
800  * Send a command to the tablet. No reply is expected.
801  */
802 static void
aiptek_command(struct usb_device * dev,unsigned int ifnum,unsigned char command,unsigned char data)803 aiptek_command(struct usb_device *dev, unsigned int ifnum,
804 	       unsigned char command, unsigned char data)
805 {
806 	__u8 buf[3];
807 
808 	buf[0] = 2;
809 	buf[1] = command;
810 	buf[2] = data;
811 
812 	if (usb_set_report(dev, ifnum, 3, 2, buf, sizeof (buf)) != sizeof (buf)) {
813 		dbg("aiptek_command failed, sending: 0x%02x 0x%02x", command,
814 		    data);
815 	}
816 }
817 
818 /*
819  * Send a query to the tablet. This is done by sending the query stream
820  * first as a command, waiting a few milliseconds, then submitting the
821  * same stream as a query.
822  */
823 static unsigned int
aiptek_query(struct usb_device * dev,unsigned int ifnum,unsigned char command,unsigned char data)824 aiptek_query(struct usb_device *dev, unsigned int ifnum,
825 	     unsigned char command, unsigned char data)
826 {
827 	unsigned int ret;
828 	__u8 buf[8];
829 	buf[0] = 2;
830 	buf[1] = command;
831 	buf[2] = data;
832 
833 	aiptek_command(dev, ifnum, command, data);
834 	wait_ms(400);
835 
836 	if (usb_get_report(dev, ifnum, 3, 2, buf, 3) < 3) {
837 		dbg("aiptek_query failed: returns 0x%02x 0x%02x 0x%02x",
838 		    buf[0], buf[1], buf[2]);
839 		return 0;
840 	}
841 	ret = ((__u32) buf[1]) | ((__u32) buf[2] << 8);
842 	return ret;
843 }
844 
845 /*
846  * Program the tablet into either absolute or relative mode.
847  *
848  * We also get information about the tablet's size.
849  */
850 static void
aiptek_program_tablet(struct aiptek * aiptek)851 aiptek_program_tablet(struct aiptek *aiptek)
852 {
853 	int modelCode, odmCode, firmwareCode;
854 	int xResolution, yResolution, zResolution;
855 
856 	aiptek->diagnostic = AIPTEK_DIAGNOSTIC_NA;
857 
858 	// execute Resolution500LPI
859 	aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x18, 0x04);
860 	// query getModelCode
861 	modelCode = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x02, 0x00);
862 	// query getODMCode
863 	odmCode = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x03, 0x00);
864 	// query getFirmwareCode
865 	firmwareCode = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x04, 0x00);
866 	// query getXextension
867 	xResolution = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x01, 0x00);
868 	// query getYextension
869 	yResolution = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x01, 0x01);
870 	// query getPressureLevels
871 	zResolution = aiptek_query(aiptek->usbdev, aiptek->ifnum, 0x08, 0x00);
872 
873 	// Depending on whether we are in absolute or relative mode, we will
874 	// do a switchToTablet(absolute) or switchToMouse(relative) command.
875 	if (aiptek->coordinate_mode == AIPTEK_COORDINATE_ABSOLUTE_MODE) {
876 		// execute switchToTablet
877 		aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x10, 0x01);
878 	} else {
879 		// execute switchToMouse
880 		aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x10, 0x00);
881 	}
882 	// This command enables the macro keys
883 	aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x11, 0x02);
884 	// execute FilterOn
885 	aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x17, 0x00);
886 	// execute AutoGainOn
887 	aiptek_command(aiptek->usbdev, aiptek->ifnum, 0x12, 0xff);
888 
889 	aiptek->features->odmCode = odmCode;
890 	aiptek->features->modelCode = modelCode & 0xff;
891 	aiptek->features->firmwareCode = firmwareCode;
892 	aiptek->features->pressure_max = zResolution;
893 	aiptek->features->x_max = xResolution;
894 	aiptek->features->y_max = yResolution;
895 
896 	aiptek->eventCount = 0;
897 }
898 
899 #if defined(CONFIG_PROC_FS)
900 /*
901  * This routine determines keywords and their associated values, and
902  * maps them to supported modes in this driver. It's input comes from
903  * aiptek_procfs_write().
904  */
905 static void
aiptek_procfs_parse(struct aiptek * aiptek,char * keyword,char * value)906 aiptek_procfs_parse(struct aiptek *aiptek, char *keyword, char *value)
907 {
908 	if (strcmp(keyword, "pointer") == 0) {
909 		if (strcmp(value, "stylus") == 0) {
910 			aiptek->pointer_mode = AIPTEK_POINTER_ONLY_STYLUS_MODE;
911 		} else if (strcmp(value, "mouse") == 0) {
912 			aiptek->pointer_mode = AIPTEK_POINTER_ONLY_MOUSE_MODE;
913 		} else if (strcmp(value, "either") == 0) {
914 			aiptek->pointer_mode = AIPTEK_POINTER_EITHER_MODE;
915 		}
916 	} else if (strcmp(keyword, "coordinate") == 0) {
917 		if (strcmp(value, "relative") == 0) {
918 			aiptek->coordinate_mode =
919 			    AIPTEK_COORDINATE_RELATIVE_MODE;
920 		} else if (strcmp(value, "absolute") == 0) {
921 			aiptek->coordinate_mode =
922 			    AIPTEK_COORDINATE_ABSOLUTE_MODE;
923 		}
924 	} else if (strcmp(keyword, "xtilt") == 0) {
925 		if (strcmp(value, "disable") == 0) {
926 			aiptek->xTilt = AIPTEK_TILT_DISABLE;
927 		} else {
928 			int x = (int) simple_strtol(value, 0, 10);
929 			if (x >= AIPTEK_TILT_MIN && x <= AIPTEK_TILT_MAX)
930 				aiptek->xTilt = x;
931 		}
932 	} else if (strcmp(keyword, "ytilt") == 0) {
933 		if (strcmp(value, "disable") == 0) {
934 			aiptek->yTilt = AIPTEK_TILT_DISABLE;
935 		} else {
936 			int y = (int) simple_strtol(value, 0, 10);
937 			if (y >= AIPTEK_TILT_MIN && y <= AIPTEK_TILT_MAX)
938 				aiptek->yTilt = y;
939 		}
940 	} else if (strcmp(keyword, "jitter") == 0) {
941 		aiptek->jitterDelay = (int) simple_strtol(value, 0, 10);
942 	} else if (strcmp(keyword, "tool") == 0) {
943 		if (strcmp(value, "mouse") == 0) {
944 			aiptek->tool_mode = AIPTEK_TOOL_BUTTON_MOUSE_MODE;
945 		} else if (strcmp(value, "rubber") == 0) {
946 			aiptek->tool_mode = AIPTEK_TOOL_BUTTON_RUBBER_MODE;
947 		} else if (strcmp(value, "pencil") == 0) {
948 			aiptek->tool_mode = AIPTEK_TOOL_BUTTON_PENCIL_MODE;
949 		} else if (strcmp(value, "pen") == 0) {
950 			aiptek->tool_mode = AIPTEK_TOOL_BUTTON_PEN_MODE;
951 		} else if (strcmp(value, "brush") == 0) {
952 			aiptek->tool_mode = AIPTEK_TOOL_BUTTON_BRUSH_MODE;
953 		} else if (strcmp(value, "airbrush") == 0) {
954 			aiptek->tool_mode = AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE;
955 		}
956 	}
957 }
958 
959 /*
960  * This routine reads the status of the aiptek driver, and makes it
961  * available as a procfs file. The description of the procfs file
962  * is at the top of this driver source code.
963  */
964 static int
aiptek_procfs_read(char * page,char ** start,off_t offset,int count,int * eof,void * data)965 aiptek_procfs_read(char *page, char **start, off_t offset, int count,
966 		   int *eof, void *data)
967 {
968 	int len;
969 	char *out = page;
970 	struct aiptek *aiptek = data;
971 
972 	out +=
973 	    sprintf(out, "Aiptek Tablet (%dx%d)\n",
974 		    aiptek->features->x_max, aiptek->features->y_max);
975 
976 	out +=
977 	    sprintf(out,
978 		    "(USB VendorID 0x%04x, ProductID 0x%04x, ODMCode 0x%04x\n",
979 		    aiptek->dev.idvendor, aiptek->dev.idproduct,
980 		    aiptek->features->odmCode);
981 	out +=
982 	    sprintf(out, " ModelCode: 0x%02x, FirmwareCode: 0x%04x)\n",
983 		    aiptek->features->modelCode,
984 		    aiptek->features->firmwareCode);
985 
986 	out += sprintf(out, "on /dev/input/event%d\n", aiptek->dev.number);
987 	out += sprintf(out, "pointer=%s\n",
988 		       (aiptek->pointer_mode == AIPTEK_POINTER_ONLY_MOUSE_MODE
989 			? "mouse"
990 			: (aiptek->pointer_mode ==
991 			   AIPTEK_POINTER_ONLY_STYLUS_MODE ? "stylus" :
992 			   "either")));
993 	out +=
994 	    sprintf(out, "coordinate=%s\n",
995 		    (aiptek->coordinate_mode ==
996 		     AIPTEK_COORDINATE_RELATIVE_MODE ? "relative" :
997 		     "absolute"));
998 
999 	out += sprintf(out, "tool=");
1000 	switch (aiptek->tool_mode) {
1001 	case AIPTEK_TOOL_BUTTON_MOUSE_MODE:
1002 		out += sprintf(out, "mouse\n");
1003 		break;
1004 
1005 	case AIPTEK_TOOL_BUTTON_RUBBER_MODE:
1006 		out += sprintf(out, "rubber\n");
1007 		break;
1008 
1009 	case AIPTEK_TOOL_BUTTON_PEN_MODE:
1010 		out += sprintf(out, "pen\n");
1011 		break;
1012 
1013 	case AIPTEK_TOOL_BUTTON_PENCIL_MODE:
1014 		out += sprintf(out, "pencil\n");
1015 		break;
1016 
1017 	case AIPTEK_TOOL_BUTTON_BRUSH_MODE:
1018 		out += sprintf(out, "brush\n");
1019 		break;
1020 
1021 	case AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE:
1022 		out += sprintf(out, "airbrush\n");
1023 		break;
1024 	}
1025 
1026 	out += sprintf(out, "xtilt=");
1027 	if (aiptek->xTilt == AIPTEK_TILT_DISABLE) {
1028 		out += sprintf(out, "disable\n");
1029 	} else {
1030 		out += sprintf(out, "%d\n", aiptek->xTilt);
1031 	}
1032 
1033 	out += sprintf(out, "ytilt=");
1034 	if (aiptek->yTilt == AIPTEK_TILT_DISABLE) {
1035 		out += sprintf(out, "disable\n");
1036 	} else {
1037 		out += sprintf(out, "%d\n", aiptek->yTilt);
1038 	}
1039 
1040 	out += sprintf(out, "jitter=%d\n", aiptek->jitterDelay);
1041 
1042 	out += sprintf(out, "diagnostic=");
1043 	switch (aiptek->diagnostic) {
1044 	case AIPTEK_DIAGNOSTIC_NA:
1045 		out += sprintf(out, "none\n");
1046 		break;
1047 	case AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE:
1048 		out += sprintf(out, "tablet sending relative reports\n");
1049 		break;
1050 	case AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE:
1051 		out += sprintf(out, "tablet sending absolute reports\n");
1052 		break;
1053 	case AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED:
1054 		out += sprintf(out, "tablet seeing reports from ");
1055 		if (aiptek->pointer_mode == AIPTEK_POINTER_ONLY_MOUSE_MODE)
1056 			out += sprintf(out, "stylus\n");
1057 		else
1058 			out += sprintf(out, "mouse\n");
1059 		break;
1060 	}
1061 
1062 	out += sprintf(out, "eventsReceived=%lu\n", aiptek->eventCount);
1063 
1064 	len = out - page;
1065 	len -= offset;
1066 	if (len < count) {
1067 		*eof = 1;
1068 		if (len <= 0) {
1069 			return 0;
1070 		}
1071 	} else {
1072 		len = count;
1073 	}
1074 
1075 	*start = page + offset;
1076 
1077 	return len;
1078 }
1079 
1080 /*
1081  * This routine permits the setting of driver parameters through a
1082  * procfs file. Writing to the procfs file (/proc/driver/usb/aiptek),
1083  * you can program the tablet's behavior. Parameters that can be programmed
1084  * (and their legal values) are described at the top of this driver.
1085  *
1086  *
1087  * This parser is order-insensitive, and supports one or many parameters
1088  * to be sent in one write request. As many parameters as you may fit
1089  * in 64 bytes; we only require that you separate them with \n's.
1090  *
1091  * Any command that is not understood by the parser is silently ignored.
1092  */
1093 static int
aiptek_procfs_write(struct file * file,const char * buffer,unsigned long count,void * data)1094 aiptek_procfs_write(struct file *file, const char *buffer, unsigned long count,
1095 		    void *data)
1096 {
1097 	char buf[64];
1098 	char *scan;
1099 	char *keyword = NULL;
1100 	char *value = NULL;
1101 	struct aiptek *aiptek = data;
1102 	int num;
1103 
1104 	num = (count < 64) ? count : 64;
1105 	if (copy_from_user(buf, buffer, num))
1106 		return -EFAULT;
1107 	buf[num] = '\0';
1108 
1109 	scan = buf;
1110 	while (*scan) {
1111 		if (*scan == '\n' || *scan == '\0') {
1112 			if (*scan == '\n') {
1113 				*scan = '\0';
1114 				scan++;
1115 			}
1116 			if (keyword && value) {
1117 				aiptek_procfs_parse(aiptek, keyword, value);
1118 			}
1119 			keyword = NULL;
1120 			value = NULL;
1121 			continue;
1122 		}
1123 
1124 		if (*scan != '=' && keyword == NULL) {
1125 			keyword = scan;
1126 		} else if (*scan == '=') {
1127 			*scan++ = '\0';
1128 			value = scan;
1129 		}
1130 		scan++;
1131 	}
1132 	// We're insensitive as to whether the buffer ended in a \n or not.
1133 	if (keyword && value) {
1134 		aiptek_procfs_parse(aiptek, keyword, value);
1135 	}
1136 
1137 	aiptek_program_tablet(aiptek);
1138 
1139 	return num;
1140 }
1141 
1142 /*
1143  * This routine destroys our procfs device interface. This will occur
1144  * when you remove the driver, either through rmmod or the hotplug system.
1145  */
1146 static void
destroy_procfs_file(struct aiptek * aiptek)1147 destroy_procfs_file(struct aiptek *aiptek)
1148 {
1149 	if (aiptek->aiptekProcfsEntry)
1150 		remove_proc_entry("aiptek", aiptek->usbProcfsEntry);
1151 	if (aiptek->usbProcfsEntry)
1152 		remove_proc_entry("usb", proc_root_driver);
1153 
1154 	aiptek->usbProcfsEntry = NULL;
1155 	aiptek->aiptekProcfsEntry = NULL;
1156 }
1157 
1158 /*
1159  * This routine builds the procfs file. The file is located at,
1160  *      procfs/driver/usb/aiptek.
1161  */
1162 static void
create_procfs_file(struct aiptek * aiptek)1163 create_procfs_file(struct aiptek *aiptek)
1164 {
1165 	// Make procfs/driver/usb directory
1166 	aiptek->usbProcfsEntry = create_proc_entry("usb", S_IFDIR,
1167 						   proc_root_driver);
1168 	if (!aiptek->usbProcfsEntry) {
1169 		dbg("create_procfs_file failed; no procfs/driver/usb control file.");
1170 		destroy_procfs_file(aiptek);
1171 		return;
1172 	}
1173 	aiptek->usbProcfsEntry->owner = THIS_MODULE;
1174 
1175 	// Make procfs/driver/usb/aiptek file
1176 	aiptek->aiptekProcfsEntry = create_proc_entry("aiptek",
1177 						      S_IFREG | S_IRUGO |
1178 						      S_IWUGO,
1179 						      aiptek->usbProcfsEntry);
1180 	if (!aiptek->aiptekProcfsEntry) {
1181 		dbg("create_procfs_file failed; no procfs/driver/usb control file.");
1182 		destroy_procfs_file(aiptek);
1183 		return;
1184 	}
1185 	aiptek->aiptekProcfsEntry->owner = THIS_MODULE;
1186 	aiptek->aiptekProcfsEntry->data = aiptek;
1187 	aiptek->aiptekProcfsEntry->read_proc = aiptek_procfs_read;
1188 	aiptek->aiptekProcfsEntry->write_proc = aiptek_procfs_write;
1189 }
1190 #endif
1191 
1192 static void *
aiptek_probe(struct usb_device * dev,unsigned int ifnum,const struct usb_device_id * id)1193 aiptek_probe(struct usb_device *dev, unsigned int ifnum,
1194 	     const struct usb_device_id *id)
1195 {
1196 	struct usb_endpoint_descriptor *endpoint;
1197 	struct aiptek *aiptek;
1198 	int i;
1199 
1200 	if (!(aiptek = kmalloc(sizeof (struct aiptek), GFP_KERNEL)))
1201 		return NULL;
1202 
1203 	memset(aiptek, 0, sizeof (struct aiptek));
1204 
1205 	aiptek->irq = usb_alloc_urb(0);
1206 	if (!aiptek->irq) {
1207 		kfree(aiptek);
1208 		return NULL;
1209 	}
1210 	// This used to be meaningful, when we had a matrix of
1211 	// different models with statically-assigned different
1212 	// features. Now we ask the tablet about everything.
1213 
1214 	aiptek->features = aiptek_features;
1215 
1216 	// Reset the tablet. The tablet boots up in 'SwitchtoMouse'
1217 	// mode, which indicates relative coordinates. 'SwitchToTablet'
1218 	// infers absolute coordinates. (Ergo, mice are inferred to be
1219 	// relative-only devices, which is not true. A misnomer.)
1220 	// The routine we use, aiptek_program_tablet, has been generalized
1221 	// enough such that it's callable through the procfs interface.
1222 	// This is why we use struct aiptek throughout.
1223 	aiptek->usbdev = dev;
1224 	aiptek->ifnum = ifnum;
1225 	aiptek->pointer_mode = AIPTEK_POINTER_EITHER_MODE;
1226 	aiptek->coordinate_mode = AIPTEK_COORDINATE_ABSOLUTE_MODE;
1227 	aiptek->tool_mode = AIPTEK_TOOL_BUTTON_PEN_MODE;
1228 	aiptek->xTilt = AIPTEK_TILT_DISABLE;
1229 	aiptek->yTilt = AIPTEK_TILT_DISABLE;
1230 	aiptek->jitterDelay = AIPTEK_JITTER_DELAY_DEFAULT;
1231 
1232 #ifdef CONFIG_PROC_FS
1233 	create_procfs_file(aiptek);
1234 #endif
1235 
1236 	aiptek_program_tablet(aiptek);
1237 
1238 	aiptek->dev.evbit[0] |= BIT(EV_KEY)
1239 	    | BIT(EV_ABS)
1240 	    | BIT(EV_MSC);
1241 
1242 	aiptek->dev.absbit[0] |= BIT(ABS_X)
1243 	    | BIT(ABS_Y)
1244 	    | BIT(ABS_PRESSURE)
1245 	    | BIT(ABS_TILT_X)
1246 	    | BIT(ABS_TILT_Y)
1247 	    | BIT(ABS_MISC);
1248 
1249 	aiptek->dev.relbit[0] |= BIT(REL_X)
1250 	    | BIT(REL_Y)
1251 	    | BIT(REL_MISC);
1252 
1253 	// Set the macro keys up. They are discontiguous, so it's better
1254 	// to set the bitmask this way.
1255 
1256 	for (i = 0; i < sizeof (macroKeyEvents) / sizeof (macroKeyEvents[0]);
1257 	     ++i) {
1258 		set_bit(macroKeyEvents[i], aiptek->dev.keybit);
1259 	}
1260 
1261 	aiptek->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT)
1262 	    | BIT(BTN_RIGHT)
1263 	    | BIT(BTN_MIDDLE);
1264 
1265 	aiptek->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_PEN)
1266 	    | BIT(BTN_TOOL_RUBBER)
1267 	    | BIT(BTN_TOOL_PENCIL)
1268 	    | BIT(BTN_TOOL_AIRBRUSH)
1269 	    | BIT(BTN_TOOL_BRUSH)
1270 	    | BIT(BTN_TOOL_MOUSE)
1271 	    | BIT(BTN_TOUCH)
1272 	    | BIT(BTN_STYLUS)
1273 	    | BIT(BTN_STYLUS2);
1274 
1275 	aiptek->dev.mscbit[0] = BIT(MSC_SERIAL);
1276 
1277 	aiptek->dev.absmax[ABS_X] = aiptek->features->x_max;
1278 	aiptek->dev.absmax[ABS_Y] = aiptek->features->y_max;
1279 	aiptek->dev.absmax[ABS_PRESSURE] = aiptek->features->pressure_max;
1280 	aiptek->dev.absmax[ABS_TILT_X] = AIPTEK_TILT_MAX;
1281 	aiptek->dev.absmax[ABS_TILT_Y] = AIPTEK_TILT_MAX;
1282 	aiptek->dev.absfuzz[ABS_X] = 0;
1283 	aiptek->dev.absfuzz[ABS_Y] = 0;
1284 
1285 	aiptek->dev.private = aiptek;
1286 	aiptek->dev.open = aiptek_open;
1287 	aiptek->dev.close = aiptek_close;
1288 
1289 	aiptek->dev.name = aiptek->features->name;
1290 	aiptek->dev.idbus = BUS_USB;
1291 	aiptek->dev.idvendor = dev->descriptor.idVendor;
1292 	aiptek->dev.idproduct = dev->descriptor.idProduct;
1293 	aiptek->dev.idversion = dev->descriptor.bcdDevice;
1294 	aiptek->usbdev = dev;
1295 
1296 	endpoint = dev->config[0].interface[ifnum].altsetting[0].endpoint + 0;
1297 
1298 	usb_fill_int_urb(aiptek->irq,
1299 			 dev,
1300 			 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1301 			 aiptek->data,
1302 			 aiptek->features->pktlen,
1303 			 aiptek->features->irq, aiptek, endpoint->bInterval);
1304 
1305 	input_register_device(&aiptek->dev);
1306 
1307 	printk(KERN_INFO "input%d: %s on usb%d:%d.%d\n",
1308 	       aiptek->dev.number,
1309 	       aiptek->features->name, dev->bus->busnum, dev->devnum, ifnum);
1310 
1311 	return aiptek;
1312 }
1313 
1314 static struct usb_driver aiptek_driver;
1315 
1316 static void
aiptek_disconnect(struct usb_device * dev,void * ptr)1317 aiptek_disconnect(struct usb_device *dev, void *ptr)
1318 {
1319 	struct aiptek *aiptek = ptr;
1320 #ifdef CONFIG_PROC_FS
1321 	destroy_procfs_file(aiptek);
1322 #endif
1323 	usb_unlink_urb(aiptek->irq);
1324 	input_unregister_device(&aiptek->dev);
1325 	usb_free_urb(aiptek->irq);
1326 	kfree(aiptek);
1327 }
1328 
1329 static struct usb_driver aiptek_driver = {
1330 	name:"aiptek",
1331 	probe:aiptek_probe,
1332 	disconnect:aiptek_disconnect,
1333 	id_table:aiptek_ids,
1334 };
1335 
1336 static int __init
aiptek_init(void)1337 aiptek_init(void)
1338 {
1339 	usb_register(&aiptek_driver);
1340 	info(DRIVER_VERSION ": " DRIVER_AUTHOR);
1341 	info(DRIVER_DESC);
1342 	return 0;
1343 }
1344 
1345 static void __exit
aiptek_exit(void)1346 aiptek_exit(void)
1347 {
1348 	usb_deregister(&aiptek_driver);
1349 }
1350 
1351 module_init(aiptek_init);
1352 module_exit(aiptek_exit);
1353