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