Merge branch 'for-2.6.31' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6] / drivers / input / tablet / gtco.c
1 /*    -*- linux-c -*-
2
3 GTCO digitizer USB driver
4
5 Use the err() and dbg() macros from usb.h for system logging
6
7 TO CHECK:  Is pressure done right on report 5?
8
9 Copyright (C) 2006  GTCO CalComp
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; version 2
14 of the License.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24
25 Permission to use, copy, modify, distribute, and sell this software and its
26 documentation for any purpose is hereby granted without fee, provided that
27 the above copyright notice appear in all copies and that both that
28 copyright notice and this permission notice appear in supporting
29 documentation, and that the name of GTCO-CalComp not be used in advertising
30 or publicity pertaining to distribution of the software without specific,
31 written prior permission. GTCO-CalComp makes no representations about the
32 suitability of this software for any purpose.  It is provided "as is"
33 without express or implied warranty.
34
35 GTCO-CALCOMP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
36 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
37 EVENT SHALL GTCO-CALCOMP BE LIABLE FOR ANY SPECIAL, INDIRECT OR
38 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
39 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
40 TORTIOUS ACTIONS, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
41 PERFORMANCE OF THIS SOFTWARE.
42
43 GTCO CalComp, Inc.
44 7125 Riverwood Drive
45 Columbia, MD 21046
46
47 Jeremy Roberson jroberson@gtcocalcomp.com
48 Scott Hill shill@gtcocalcomp.com
49 */
50
51
52
53 /*#define DEBUG*/
54
55 #include <linux/kernel.h>
56 #include <linux/module.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
60 #include <linux/input.h>
61 #include <linux/usb.h>
62 #include <asm/uaccess.h>
63 #include <asm/unaligned.h>
64 #include <asm/byteorder.h>
65
66
67 #include <linux/usb/input.h>
68
69 /* Version with a Major number of 2 is for kernel inclusion only. */
70 #define  GTCO_VERSION   "2.00.0006"
71
72
73 /*   MACROS  */
74
75 #define VENDOR_ID_GTCO        0x078C
76 #define PID_400               0x400
77 #define PID_401               0x401
78 #define PID_1000              0x1000
79 #define PID_1001              0x1001
80 #define PID_1002              0x1002
81
82 /* Max size of a single report */
83 #define REPORT_MAX_SIZE       10
84
85
86 /* Bitmask whether pen is in range */
87 #define MASK_INRANGE 0x20
88 #define MASK_BUTTON  0x01F
89
90 #define  PATHLENGTH     64
91
92 /* DATA STRUCTURES */
93
94 /* Device table */
95 static struct usb_device_id gtco_usbid_table [] = {
96         { USB_DEVICE(VENDOR_ID_GTCO, PID_400) },
97         { USB_DEVICE(VENDOR_ID_GTCO, PID_401) },
98         { USB_DEVICE(VENDOR_ID_GTCO, PID_1000) },
99         { USB_DEVICE(VENDOR_ID_GTCO, PID_1001) },
100         { USB_DEVICE(VENDOR_ID_GTCO, PID_1002) },
101         { }
102 };
103 MODULE_DEVICE_TABLE (usb, gtco_usbid_table);
104
105
106 /* Structure to hold all of our device specific stuff */
107 struct gtco {
108
109         struct input_dev  *inputdevice; /* input device struct pointer  */
110         struct usb_device *usbdev; /* the usb device for this device */
111         struct urb        *urbinfo;      /* urb for incoming reports      */
112         dma_addr_t        buf_dma;  /* dma addr of the data buffer*/
113         unsigned char *   buffer;   /* databuffer for reports */
114
115         char  usbpath[PATHLENGTH];
116         int   openCount;
117
118         /* Information pulled from Report Descriptor */
119         u32  usage;
120         u32  min_X;
121         u32  max_X;
122         u32  min_Y;
123         u32  max_Y;
124         s8   mintilt_X;
125         s8   maxtilt_X;
126         s8   mintilt_Y;
127         s8   maxtilt_Y;
128         u32  maxpressure;
129         u32  minpressure;
130 };
131
132
133
134 /*   Code for parsing the HID REPORT DESCRIPTOR          */
135
136 /* From HID1.11 spec */
137 struct hid_descriptor
138 {
139         struct usb_descriptor_header header;
140         __le16   bcdHID;
141         u8       bCountryCode;
142         u8       bNumDescriptors;
143         u8       bDescriptorType;
144         __le16   wDescriptorLength;
145 } __attribute__ ((packed));
146
147
148 #define HID_DESCRIPTOR_SIZE   9
149 #define HID_DEVICE_TYPE       33
150 #define REPORT_DEVICE_TYPE    34
151
152
153 #define PREF_TAG(x)     ((x)>>4)
154 #define PREF_TYPE(x)    ((x>>2)&0x03)
155 #define PREF_SIZE(x)    ((x)&0x03)
156
157 #define TYPE_MAIN       0
158 #define TYPE_GLOBAL     1
159 #define TYPE_LOCAL      2
160 #define TYPE_RESERVED   3
161
162 #define TAG_MAIN_INPUT        0x8
163 #define TAG_MAIN_OUTPUT       0x9
164 #define TAG_MAIN_FEATURE      0xB
165 #define TAG_MAIN_COL_START    0xA
166 #define TAG_MAIN_COL_END      0xC
167
168 #define TAG_GLOB_USAGE        0
169 #define TAG_GLOB_LOG_MIN      1
170 #define TAG_GLOB_LOG_MAX      2
171 #define TAG_GLOB_PHYS_MIN     3
172 #define TAG_GLOB_PHYS_MAX     4
173 #define TAG_GLOB_UNIT_EXP     5
174 #define TAG_GLOB_UNIT         6
175 #define TAG_GLOB_REPORT_SZ    7
176 #define TAG_GLOB_REPORT_ID    8
177 #define TAG_GLOB_REPORT_CNT   9
178 #define TAG_GLOB_PUSH         10
179 #define TAG_GLOB_POP          11
180
181 #define TAG_GLOB_MAX          12
182
183 #define DIGITIZER_USAGE_TIP_PRESSURE   0x30
184 #define DIGITIZER_USAGE_TILT_X         0x3D
185 #define DIGITIZER_USAGE_TILT_Y         0x3E
186
187
188 /*
189  *   This is an abbreviated parser for the HID Report Descriptor.  We
190  *   know what devices we are talking to, so this is by no means meant
191  *   to be generic.  We can make some safe assumptions:
192  *
193  *   - We know there are no LONG tags, all short
194  *   - We know that we have no MAIN Feature and MAIN Output items
195  *   - We know what the IRQ reports are supposed to look like.
196  *
197  *   The main purpose of this is to use the HID report desc to figure
198  *   out the mins and maxs of the fields in the IRQ reports.  The IRQ
199  *   reports for 400/401 change slightly if the max X is bigger than 64K.
200  *
201  */
202 static void parse_hid_report_descriptor(struct gtco *device, char * report,
203                                         int length)
204 {
205         int   x, i = 0;
206
207         /* Tag primitive vars */
208         __u8   prefix;
209         __u8   size;
210         __u8   tag;
211         __u8   type;
212         __u8   data   = 0;
213         __u16  data16 = 0;
214         __u32  data32 = 0;
215
216         /* For parsing logic */
217         int   inputnum = 0;
218         __u32 usage = 0;
219
220         /* Global Values, indexed by TAG */
221         __u32 globalval[TAG_GLOB_MAX];
222         __u32 oldval[TAG_GLOB_MAX];
223
224         /* Debug stuff */
225         char  maintype = 'x';
226         char  globtype[12];
227         int   indent = 0;
228         char  indentstr[10] = "";
229
230
231         dbg("======>>>>>>PARSE<<<<<<======");
232
233         /* Walk  this report and pull out the info we need */
234         while (i < length) {
235                 prefix = report[i];
236
237                 /* Skip over prefix */
238                 i++;
239
240                 /* Determine data size and save the data in the proper variable */
241                 size = PREF_SIZE(prefix);
242                 switch (size) {
243                 case 1:
244                         data = report[i];
245                         break;
246                 case 2:
247                         data16 = get_unaligned_le16(&report[i]);
248                         break;
249                 case 3:
250                         size = 4;
251                         data32 = get_unaligned_le32(&report[i]);
252                         break;
253                 }
254
255                 /* Skip size of data */
256                 i += size;
257
258                 /* What we do depends on the tag type */
259                 tag  = PREF_TAG(prefix);
260                 type = PREF_TYPE(prefix);
261                 switch (type) {
262                 case TYPE_MAIN:
263                         strcpy(globtype, "");
264                         switch (tag) {
265
266                         case TAG_MAIN_INPUT:
267                                 /*
268                                  * The INPUT MAIN tag signifies this is
269                                  * information from a report.  We need to
270                                  * figure out what it is and store the
271                                  * min/max values
272                                  */
273
274                                 maintype = 'I';
275                                 if (data == 2)
276                                         strcpy(globtype, "Variable");
277                                 else if (data == 3)
278                                         strcpy(globtype, "Var|Const");
279
280                                 dbg("::::: Saving Report: %d input #%d Max: 0x%X(%d) Min:0x%X(%d) of %d bits",
281                                     globalval[TAG_GLOB_REPORT_ID], inputnum,
282                                     globalval[TAG_GLOB_LOG_MAX], globalval[TAG_GLOB_LOG_MAX],
283                                     globalval[TAG_GLOB_LOG_MIN], globalval[TAG_GLOB_LOG_MIN],
284                                     globalval[TAG_GLOB_REPORT_SZ] * globalval[TAG_GLOB_REPORT_CNT]);
285
286
287                                 /*
288                                   We can assume that the first two input items
289                                   are always the X and Y coordinates.  After
290                                   that, we look for everything else by
291                                   local usage value
292                                  */
293                                 switch (inputnum) {
294                                 case 0:  /* X coord */
295                                         dbg("GER: X Usage: 0x%x", usage);
296                                         if (device->max_X == 0) {
297                                                 device->max_X = globalval[TAG_GLOB_LOG_MAX];
298                                                 device->min_X = globalval[TAG_GLOB_LOG_MIN];
299                                         }
300                                         break;
301
302                                 case 1:  /* Y coord */
303                                         dbg("GER: Y Usage: 0x%x", usage);
304                                         if (device->max_Y == 0) {
305                                                 device->max_Y = globalval[TAG_GLOB_LOG_MAX];
306                                                 device->min_Y = globalval[TAG_GLOB_LOG_MIN];
307                                         }
308                                         break;
309
310                                 default:
311                                         /* Tilt X */
312                                         if (usage == DIGITIZER_USAGE_TILT_X) {
313                                                 if (device->maxtilt_X == 0) {
314                                                         device->maxtilt_X = globalval[TAG_GLOB_LOG_MAX];
315                                                         device->mintilt_X = globalval[TAG_GLOB_LOG_MIN];
316                                                 }
317                                         }
318
319                                         /* Tilt Y */
320                                         if (usage == DIGITIZER_USAGE_TILT_Y) {
321                                                 if (device->maxtilt_Y == 0) {
322                                                         device->maxtilt_Y = globalval[TAG_GLOB_LOG_MAX];
323                                                         device->mintilt_Y = globalval[TAG_GLOB_LOG_MIN];
324                                                 }
325                                         }
326
327                                         /* Pressure */
328                                         if (usage == DIGITIZER_USAGE_TIP_PRESSURE) {
329                                                 if (device->maxpressure == 0) {
330                                                         device->maxpressure = globalval[TAG_GLOB_LOG_MAX];
331                                                         device->minpressure = globalval[TAG_GLOB_LOG_MIN];
332                                                 }
333                                         }
334
335                                         break;
336                                 }
337
338                                 inputnum++;
339                                 break;
340
341                         case TAG_MAIN_OUTPUT:
342                                 maintype = 'O';
343                                 break;
344
345                         case TAG_MAIN_FEATURE:
346                                 maintype = 'F';
347                                 break;
348
349                         case TAG_MAIN_COL_START:
350                                 maintype = 'S';
351
352                                 if (data == 0) {
353                                         dbg("======>>>>>> Physical");
354                                         strcpy(globtype, "Physical");
355                                 } else
356                                         dbg("======>>>>>>");
357
358                                 /* Indent the debug output */
359                                 indent++;
360                                 for (x = 0; x < indent; x++)
361                                         indentstr[x] = '-';
362                                 indentstr[x] = 0;
363
364                                 /* Save global tags */
365                                 for (x = 0; x < TAG_GLOB_MAX; x++)
366                                         oldval[x] = globalval[x];
367
368                                 break;
369
370                         case TAG_MAIN_COL_END:
371                                 dbg("<<<<<<======");
372                                 maintype = 'E';
373                                 indent--;
374                                 for (x = 0; x < indent; x++)
375                                         indentstr[x] = '-';
376                                 indentstr[x] = 0;
377
378                                 /* Copy global tags back */
379                                 for (x = 0; x < TAG_GLOB_MAX; x++)
380                                         globalval[x] = oldval[x];
381
382                                 break;
383                         }
384
385                         switch (size) {
386                         case 1:
387                                 dbg("%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x",
388                                     indentstr, tag, maintype, size, globtype, data);
389                                 break;
390
391                         case 2:
392                                 dbg("%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x",
393                                     indentstr, tag, maintype, size, globtype, data16);
394                                 break;
395
396                         case 4:
397                                 dbg("%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x",
398                                     indentstr, tag, maintype, size, globtype, data32);
399                                 break;
400                         }
401                         break;
402
403                 case TYPE_GLOBAL:
404                         switch (tag) {
405                         case TAG_GLOB_USAGE:
406                                 /*
407                                  * First time we hit the global usage tag,
408                                  * it should tell us the type of device
409                                  */
410                                 if (device->usage == 0)
411                                         device->usage = data;
412
413                                 strcpy(globtype, "USAGE");
414                                 break;
415
416                         case TAG_GLOB_LOG_MIN:
417                                 strcpy(globtype, "LOG_MIN");
418                                 break;
419
420                         case TAG_GLOB_LOG_MAX:
421                                 strcpy(globtype, "LOG_MAX");
422                                 break;
423
424                         case TAG_GLOB_PHYS_MIN:
425                                 strcpy(globtype, "PHYS_MIN");
426                                 break;
427
428                         case TAG_GLOB_PHYS_MAX:
429                                 strcpy(globtype, "PHYS_MAX");
430                                 break;
431
432                         case TAG_GLOB_UNIT_EXP:
433                                 strcpy(globtype, "EXP");
434                                 break;
435
436                         case TAG_GLOB_UNIT:
437                                 strcpy(globtype, "UNIT");
438                                 break;
439
440                         case TAG_GLOB_REPORT_SZ:
441                                 strcpy(globtype, "REPORT_SZ");
442                                 break;
443
444                         case TAG_GLOB_REPORT_ID:
445                                 strcpy(globtype, "REPORT_ID");
446                                 /* New report, restart numbering */
447                                 inputnum = 0;
448                                 break;
449
450                         case TAG_GLOB_REPORT_CNT:
451                                 strcpy(globtype, "REPORT_CNT");
452                                 break;
453
454                         case TAG_GLOB_PUSH:
455                                 strcpy(globtype, "PUSH");
456                                 break;
457
458                         case TAG_GLOB_POP:
459                                 strcpy(globtype, "POP");
460                                 break;
461                         }
462
463                         /* Check to make sure we have a good tag number
464                            so we don't overflow array */
465                         if (tag < TAG_GLOB_MAX) {
466                                 switch (size) {
467                                 case 1:
468                                         dbg("%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x",
469                                             indentstr, globtype, tag, size, data);
470                                         globalval[tag] = data;
471                                         break;
472
473                                 case 2:
474                                         dbg("%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x",
475                                             indentstr, globtype, tag, size, data16);
476                                         globalval[tag] = data16;
477                                         break;
478
479                                 case 4:
480                                         dbg("%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x",
481                                             indentstr, globtype, tag, size, data32);
482                                         globalval[tag] = data32;
483                                         break;
484                                 }
485                         } else {
486                                 dbg("%sGLOBALTAG: ILLEGAL TAG:%d SIZE: %d ",
487                                     indentstr, tag, size);
488                         }
489                         break;
490
491                 case TYPE_LOCAL:
492                         switch (tag) {
493                         case TAG_GLOB_USAGE:
494                                 strcpy(globtype, "USAGE");
495                                 /* Always 1 byte */
496                                 usage = data;
497                                 break;
498
499                         case TAG_GLOB_LOG_MIN:
500                                 strcpy(globtype, "MIN");
501                                 break;
502
503                         case TAG_GLOB_LOG_MAX:
504                                 strcpy(globtype, "MAX");
505                                 break;
506
507                         default:
508                                 strcpy(globtype, "UNKNOWN");
509                                 break;
510                         }
511
512                         switch (size) {
513                         case 1:
514                                 dbg("%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x",
515                                     indentstr, tag, globtype, size, data);
516                                 break;
517
518                         case 2:
519                                 dbg("%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x",
520                                     indentstr, tag, globtype, size, data16);
521                                 break;
522
523                         case 4:
524                                 dbg("%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x",
525                                     indentstr, tag, globtype, size, data32);
526                                 break;
527                         }
528
529                         break;
530                 }
531         }
532 }
533
534 /*   INPUT DRIVER Routines                               */
535
536 /*
537  * Called when opening the input device.  This will submit the URB to
538  * the usb system so we start getting reports
539  */
540 static int gtco_input_open(struct input_dev *inputdev)
541 {
542         struct gtco *device = input_get_drvdata(inputdev);
543
544         device->urbinfo->dev = device->usbdev;
545         if (usb_submit_urb(device->urbinfo, GFP_KERNEL))
546                 return -EIO;
547
548         return 0;
549 }
550
551 /*
552  * Called when closing the input device.  This will unlink the URB
553  */
554 static void gtco_input_close(struct input_dev *inputdev)
555 {
556         struct gtco *device = input_get_drvdata(inputdev);
557
558         usb_kill_urb(device->urbinfo);
559 }
560
561
562 /*
563  *  Setup input device capabilities.  Tell the input system what this
564  *  device is capable of generating.
565  *
566  *  This information is based on what is read from the HID report and
567  *  placed in the struct gtco structure
568  *
569  */
570 static void gtco_setup_caps(struct input_dev *inputdev)
571 {
572         struct gtco *device = input_get_drvdata(inputdev);
573
574         /* Which events */
575         inputdev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) |
576                 BIT_MASK(EV_MSC);
577
578         /* Misc event menu block */
579         inputdev->mscbit[0] = BIT_MASK(MSC_SCAN) | BIT_MASK(MSC_SERIAL) |
580                 BIT_MASK(MSC_RAW);
581
582         /* Absolute values based on HID report info */
583         input_set_abs_params(inputdev, ABS_X, device->min_X, device->max_X,
584                              0, 0);
585         input_set_abs_params(inputdev, ABS_Y, device->min_Y, device->max_Y,
586                              0, 0);
587
588         /* Proximity */
589         input_set_abs_params(inputdev, ABS_DISTANCE, 0, 1, 0, 0);
590
591         /* Tilt & pressure */
592         input_set_abs_params(inputdev, ABS_TILT_X, device->mintilt_X,
593                              device->maxtilt_X, 0, 0);
594         input_set_abs_params(inputdev, ABS_TILT_Y, device->mintilt_Y,
595                              device->maxtilt_Y, 0, 0);
596         input_set_abs_params(inputdev, ABS_PRESSURE, device->minpressure,
597                              device->maxpressure, 0, 0);
598
599         /* Transducer */
600         input_set_abs_params(inputdev, ABS_MISC, 0, 0xFF, 0, 0);
601 }
602
603 /*   USB Routines  */
604
605 /*
606  * URB callback routine.  Called when we get IRQ reports from the
607  *  digitizer.
608  *
609  *  This bridges the USB and input device worlds.  It generates events
610  *  on the input device based on the USB reports.
611  */
612 static void gtco_urb_callback(struct urb *urbinfo)
613 {
614         struct gtco *device = urbinfo->context;
615         struct input_dev  *inputdev;
616         int               rc;
617         u32               val = 0;
618         s8                valsigned = 0;
619         char              le_buffer[2];
620
621         inputdev = device->inputdevice;
622
623         /* Was callback OK? */
624         if (urbinfo->status == -ECONNRESET ||
625             urbinfo->status == -ENOENT ||
626             urbinfo->status == -ESHUTDOWN) {
627
628                 /* Shutdown is occurring. Return and don't queue up any more */
629                 return;
630         }
631
632         if (urbinfo->status != 0) {
633                 /*
634                  * Some unknown error.  Hopefully temporary. Just go and
635                  * requeue an URB
636                  */
637                 goto resubmit;
638         }
639
640         /*
641          * Good URB, now process
642          */
643
644         /* PID dependent when we interpret the report */
645         if (inputdev->id.product == PID_1000 ||
646             inputdev->id.product == PID_1001 ||
647             inputdev->id.product == PID_1002) {
648
649                 /*
650                  * Switch on the report ID
651                  * Conveniently, the reports have more information, the higher
652                  * the report number.  We can just fall through the case
653                  * statements if we start with the highest number report
654                  */
655                 switch (device->buffer[0]) {
656                 case 5:
657                         /* Pressure is 9 bits */
658                         val = ((u16)(device->buffer[8]) << 1);
659                         val |= (u16)(device->buffer[7] >> 7);
660                         input_report_abs(inputdev, ABS_PRESSURE,
661                                          device->buffer[8]);
662
663                         /* Mask out the Y tilt value used for pressure */
664                         device->buffer[7] = (u8)((device->buffer[7]) & 0x7F);
665
666                         /* Fall thru */
667                 case 4:
668                         /* Tilt */
669
670                         /* Sign extend these 7 bit numbers.  */
671                         if (device->buffer[6] & 0x40)
672                                 device->buffer[6] |= 0x80;
673
674                         if (device->buffer[7] & 0x40)
675                                 device->buffer[7] |= 0x80;
676
677
678                         valsigned = (device->buffer[6]);
679                         input_report_abs(inputdev, ABS_TILT_X, (s32)valsigned);
680
681                         valsigned = (device->buffer[7]);
682                         input_report_abs(inputdev, ABS_TILT_Y, (s32)valsigned);
683
684                         /* Fall thru */
685                 case 2:
686                 case 3:
687                         /* Convert buttons, only 5 bits possible */
688                         val = (device->buffer[5]) & MASK_BUTTON;
689
690                         /* We don't apply any meaning to the bitmask,
691                            just report */
692                         input_event(inputdev, EV_MSC, MSC_SERIAL, val);
693
694                         /*  Fall thru */
695                 case 1:
696                         /* All reports have X and Y coords in the same place */
697                         val = get_unaligned_le16(&device->buffer[1]);
698                         input_report_abs(inputdev, ABS_X, val);
699
700                         val = get_unaligned_le16(&device->buffer[3]);
701                         input_report_abs(inputdev, ABS_Y, val);
702
703                         /* Ditto for proximity bit */
704                         val = device->buffer[5] & MASK_INRANGE ? 1 : 0;
705                         input_report_abs(inputdev, ABS_DISTANCE, val);
706
707                         /* Report 1 is an exception to how we handle buttons */
708                         /* Buttons are an index, not a bitmask */
709                         if (device->buffer[0] == 1) {
710
711                                 /*
712                                  * Convert buttons, 5 bit index
713                                  * Report value of index set as one,
714                                  * the rest as 0
715                                  */
716                                 val = device->buffer[5] & MASK_BUTTON;
717                                 dbg("======>>>>>>REPORT 1: val 0x%X(%d)",
718                                     val, val);
719
720                                 /*
721                                  * We don't apply any meaning to the button
722                                  * index, just report it
723                                  */
724                                 input_event(inputdev, EV_MSC, MSC_SERIAL, val);
725                         }
726                         break;
727
728                 case 7:
729                         /* Menu blocks */
730                         input_event(inputdev, EV_MSC, MSC_SCAN,
731                                     device->buffer[1]);
732                         break;
733                 }
734         }
735
736         /* Other pid class */
737         if (inputdev->id.product == PID_400 ||
738             inputdev->id.product == PID_401) {
739
740                 /* Report 2 */
741                 if (device->buffer[0] == 2) {
742                         /* Menu blocks */
743                         input_event(inputdev, EV_MSC, MSC_SCAN, device->buffer[1]);
744                 }
745
746                 /*  Report 1 */
747                 if (device->buffer[0] == 1) {
748                         char buttonbyte;
749
750                         /*  IF X max > 64K, we still a bit from the y report */
751                         if (device->max_X > 0x10000) {
752
753                                 val = (u16)(((u16)(device->buffer[2] << 8)) | (u8)device->buffer[1]);
754                                 val |= (u32)(((u8)device->buffer[3] & 0x1) << 16);
755
756                                 input_report_abs(inputdev, ABS_X, val);
757
758                                 le_buffer[0]  = (u8)((u8)(device->buffer[3]) >> 1);
759                                 le_buffer[0] |= (u8)((device->buffer[3] & 0x1) << 7);
760
761                                 le_buffer[1]  = (u8)(device->buffer[4] >> 1);
762                                 le_buffer[1] |= (u8)((device->buffer[5] & 0x1) << 7);
763
764                                 val = get_unaligned_le16(le_buffer);
765                                 input_report_abs(inputdev, ABS_Y, val);
766
767                                 /*
768                                  * Shift the button byte right by one to
769                                  * make it look like the standard report
770                                  */
771                                 buttonbyte = device->buffer[5] >> 1;
772                         } else {
773
774                                 val = get_unaligned_le16(&device->buffer[1]);
775                                 input_report_abs(inputdev, ABS_X, val);
776
777                                 val = get_unaligned_le16(&device->buffer[3]);
778                                 input_report_abs(inputdev, ABS_Y, val);
779
780                                 buttonbyte = device->buffer[5];
781                         }
782
783                         /* BUTTONS and PROXIMITY */
784                         val = buttonbyte & MASK_INRANGE ? 1 : 0;
785                         input_report_abs(inputdev, ABS_DISTANCE, val);
786
787                         /* Convert buttons, only 4 bits possible */
788                         val = buttonbyte & 0x0F;
789 #ifdef USE_BUTTONS
790                         for (i = 0; i < 5; i++)
791                                 input_report_key(inputdev, BTN_DIGI + i, val & (1 << i));
792 #else
793                         /* We don't apply any meaning to the bitmask, just report */
794                         input_event(inputdev, EV_MSC, MSC_SERIAL, val);
795 #endif
796
797                         /* TRANSDUCER */
798                         input_report_abs(inputdev, ABS_MISC, device->buffer[6]);
799                 }
800         }
801
802         /* Everybody gets report ID's */
803         input_event(inputdev, EV_MSC, MSC_RAW,  device->buffer[0]);
804
805         /* Sync it up */
806         input_sync(inputdev);
807
808  resubmit:
809         rc = usb_submit_urb(urbinfo, GFP_ATOMIC);
810         if (rc != 0)
811                 err("usb_submit_urb failed rc=0x%x", rc);
812 }
813
814 /*
815  *  The probe routine.  This is called when the kernel find the matching USB
816  *   vendor/product.  We do the following:
817  *
818  *    - Allocate mem for a local structure to manage the device
819  *    - Request a HID Report Descriptor from the device and parse it to
820  *      find out the device parameters
821  *    - Create an input device and assign it attributes
822  *   - Allocate an URB so the device can talk to us when the input
823  *      queue is open
824  */
825 static int gtco_probe(struct usb_interface *usbinterface,
826                       const struct usb_device_id *id)
827 {
828
829         struct gtco             *gtco;
830         struct input_dev        *input_dev;
831         struct hid_descriptor   *hid_desc;
832         char                    *report;
833         int                     result = 0, retry;
834         int                     error;
835         struct usb_endpoint_descriptor *endpoint;
836
837         /* Allocate memory for device structure */
838         gtco = kzalloc(sizeof(struct gtco), GFP_KERNEL);
839         input_dev = input_allocate_device();
840         if (!gtco || !input_dev) {
841                 err("No more memory");
842                 error = -ENOMEM;
843                 goto err_free_devs;
844         }
845
846         /* Set pointer to the input device */
847         gtco->inputdevice = input_dev;
848
849         /* Save interface information */
850         gtco->usbdev = usb_get_dev(interface_to_usbdev(usbinterface));
851
852         /* Allocate some data for incoming reports */
853         gtco->buffer = usb_buffer_alloc(gtco->usbdev, REPORT_MAX_SIZE,
854                                         GFP_KERNEL, &gtco->buf_dma);
855         if (!gtco->buffer) {
856                 err("No more memory for us buffers");
857                 error = -ENOMEM;
858                 goto err_free_devs;
859         }
860
861         /* Allocate URB for reports */
862         gtco->urbinfo = usb_alloc_urb(0, GFP_KERNEL);
863         if (!gtco->urbinfo) {
864                 err("Failed to allocate URB");
865                 error = -ENOMEM;
866                 goto err_free_buf;
867         }
868
869         /*
870          * The endpoint is always altsetting 0, we know this since we know
871          * this device only has one interrupt endpoint
872          */
873         endpoint = &usbinterface->altsetting[0].endpoint[0].desc;
874
875         /* Some debug */
876         dbg("gtco # interfaces: %d", usbinterface->num_altsetting);
877         dbg("num endpoints:     %d", usbinterface->cur_altsetting->desc.bNumEndpoints);
878         dbg("interface class:   %d", usbinterface->cur_altsetting->desc.bInterfaceClass);
879         dbg("endpoint: attribute:0x%x type:0x%x", endpoint->bmAttributes, endpoint->bDescriptorType);
880         if (usb_endpoint_xfer_int(endpoint))
881                 dbg("endpoint: we have interrupt endpoint\n");
882
883         dbg("endpoint extra len:%d ", usbinterface->altsetting[0].extralen);
884
885         /*
886          * Find the HID descriptor so we can find out the size of the
887          * HID report descriptor
888          */
889         if (usb_get_extra_descriptor(usbinterface->cur_altsetting,
890                                      HID_DEVICE_TYPE, &hid_desc) != 0){
891                 err("Can't retrieve exta USB descriptor to get hid report descriptor length");
892                 error = -EIO;
893                 goto err_free_urb;
894         }
895
896         dbg("Extra descriptor success: type:%d  len:%d",
897             hid_desc->bDescriptorType,  hid_desc->wDescriptorLength);
898
899         report = kzalloc(le16_to_cpu(hid_desc->wDescriptorLength), GFP_KERNEL);
900         if (!report) {
901                 err("No more memory for report");
902                 error = -ENOMEM;
903                 goto err_free_urb;
904         }
905
906         /* Couple of tries to get reply */
907         for (retry = 0; retry < 3; retry++) {
908                 result = usb_control_msg(gtco->usbdev,
909                                          usb_rcvctrlpipe(gtco->usbdev, 0),
910                                          USB_REQ_GET_DESCRIPTOR,
911                                          USB_RECIP_INTERFACE | USB_DIR_IN,
912                                          REPORT_DEVICE_TYPE << 8,
913                                          0, /* interface */
914                                          report,
915                                          le16_to_cpu(hid_desc->wDescriptorLength),
916                                          5000); /* 5 secs */
917
918                 dbg("usb_control_msg result: %d", result);
919                 if (result == le16_to_cpu(hid_desc->wDescriptorLength)) {
920                         parse_hid_report_descriptor(gtco, report, result);
921                         break;
922                 }
923         }
924
925         kfree(report);
926
927         /* If we didn't get the report, fail */
928         if (result != le16_to_cpu(hid_desc->wDescriptorLength)) {
929                 err("Failed to get HID Report Descriptor of size: %d",
930                     hid_desc->wDescriptorLength);
931                 error = -EIO;
932                 goto err_free_urb;
933         }
934
935         /* Create a device file node */
936         usb_make_path(gtco->usbdev, gtco->usbpath, sizeof(gtco->usbpath));
937         strlcat(gtco->usbpath, "/input0", sizeof(gtco->usbpath));
938
939         /* Set Input device functions */
940         input_dev->open = gtco_input_open;
941         input_dev->close = gtco_input_close;
942
943         /* Set input device information */
944         input_dev->name = "GTCO_CalComp";
945         input_dev->phys = gtco->usbpath;
946
947         input_set_drvdata(input_dev, gtco);
948
949         /* Now set up all the input device capabilities */
950         gtco_setup_caps(input_dev);
951
952         /* Set input device required ID information */
953         usb_to_input_id(gtco->usbdev, &input_dev->id);
954         input_dev->dev.parent = &usbinterface->dev;
955
956         /* Setup the URB, it will be posted later on open of input device */
957         endpoint = &usbinterface->altsetting[0].endpoint[0].desc;
958
959         usb_fill_int_urb(gtco->urbinfo,
960                          gtco->usbdev,
961                          usb_rcvintpipe(gtco->usbdev,
962                                         endpoint->bEndpointAddress),
963                          gtco->buffer,
964                          REPORT_MAX_SIZE,
965                          gtco_urb_callback,
966                          gtco,
967                          endpoint->bInterval);
968
969         gtco->urbinfo->transfer_dma = gtco->buf_dma;
970         gtco->urbinfo->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
971
972         /* Save gtco pointer in USB interface gtco */
973         usb_set_intfdata(usbinterface, gtco);
974
975         /* All done, now register the input device */
976         error = input_register_device(input_dev);
977         if (error)
978                 goto err_free_urb;
979
980         return 0;
981
982  err_free_urb:
983         usb_free_urb(gtco->urbinfo);
984  err_free_buf:
985         usb_buffer_free(gtco->usbdev, REPORT_MAX_SIZE,
986                         gtco->buffer, gtco->buf_dma);
987  err_free_devs:
988         input_free_device(input_dev);
989         kfree(gtco);
990         return error;
991 }
992
993 /*
994  *  This function is a standard USB function called when the USB device
995  *  is disconnected.  We will get rid of the URV, de-register the input
996  *  device, and free up allocated memory
997  */
998 static void gtco_disconnect(struct usb_interface *interface)
999 {
1000         /* Grab private device ptr */
1001         struct gtco *gtco = usb_get_intfdata(interface);
1002
1003         /* Now reverse all the registration stuff */
1004         if (gtco) {
1005                 input_unregister_device(gtco->inputdevice);
1006                 usb_kill_urb(gtco->urbinfo);
1007                 usb_free_urb(gtco->urbinfo);
1008                 usb_buffer_free(gtco->usbdev, REPORT_MAX_SIZE,
1009                                 gtco->buffer, gtco->buf_dma);
1010                 kfree(gtco);
1011         }
1012
1013         dev_info(&interface->dev, "gtco driver disconnected\n");
1014 }
1015
1016 /*   STANDARD MODULE LOAD ROUTINES  */
1017
1018 static struct usb_driver gtco_driverinfo_table = {
1019         .name           = "gtco",
1020         .id_table       = gtco_usbid_table,
1021         .probe          = gtco_probe,
1022         .disconnect     = gtco_disconnect,
1023 };
1024
1025 /*
1026  *  Register this module with the USB subsystem
1027  */
1028 static int __init gtco_init(void)
1029 {
1030         int error;
1031
1032         error = usb_register(&gtco_driverinfo_table);
1033         if (error) {
1034                 err("usb_register() failed rc=0x%x", error);
1035                 return error;
1036         }
1037
1038         printk("GTCO usb driver version: %s", GTCO_VERSION);
1039         return 0;
1040 }
1041
1042 /*
1043  *   Deregister this module with the USB subsystem
1044  */
1045 static void __exit gtco_exit(void)
1046 {
1047         usb_deregister(&gtco_driverinfo_table);
1048 }
1049
1050 module_init(gtco_init);
1051 module_exit(gtco_exit);
1052
1053 MODULE_LICENSE("GPL");