OSDN Git Service

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