USB: gtco.c: fix a use-before-check
[linux-2.6.git] / drivers / usb / input / gtco.c
blobae756e0afc996fd5d50d1192a7c062bc3a38145f
1 /* -*- linux-c -*-
3 GTCO digitizer USB driver
5 Use the err(), dbg() and info() macros from usb.h for system logging
7 TO CHECK: Is pressure done right on report 5?
9 Copyright (C) 2006 GTCO CalComp
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.
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.
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.
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.
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.
43 GTCO CalComp, Inc.
44 7125 Riverwood Drive
45 Columbia, MD 21046
47 Jeremy Roberson jroberson@gtcocalcomp.com
48 Scott Hill shill@gtcocalcomp.com
53 /*#define DEBUG*/
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>
67 #include <linux/version.h>
68 #include <linux/usb/input.h>
70 /* Version with a Major number of 2 is for kernel inclusion only. */
71 #define GTCO_VERSION "2.00.0006"
74 /* MACROS */
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
83 /* Max size of a single report */
84 #define REPORT_MAX_SIZE 10
87 /* Bitmask whether pen is in range */
88 #define MASK_INRANGE 0x20
89 #define MASK_BUTTON 0x01F
91 #define PATHLENGTH 64
93 /* DATA STRUCTURES */
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) },
104 MODULE_DEVICE_TABLE (usb, gtco_usbid_table);
107 /* Structure to hold all of our device specific stuff */
108 struct gtco {
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 */
116 char usbpath[PATHLENGTH];
117 int openCount;
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;
135 /* Code for parsing the HID REPORT DESCRIPTOR */
137 /* From HID1.11 spec */
138 struct hid_descriptor
140 struct usb_descriptor_header header;
141 __le16 bcdHID;
142 u8 bCountryCode;
143 u8 bNumDescriptors;
144 u8 bDescriptorType;
145 __le16 wDescriptorLength;
146 } __attribute__ ((packed));
149 #define HID_DESCRIPTOR_SIZE 9
150 #define HID_DEVICE_TYPE 33
151 #define REPORT_DEVICE_TYPE 34
154 #define PREF_TAG(x) ((x)>>4)
155 #define PREF_TYPE(x) ((x>>2)&0x03)
156 #define PREF_SIZE(x) ((x)&0x03)
158 #define TYPE_MAIN 0
159 #define TYPE_GLOBAL 1
160 #define TYPE_LOCAL 2
161 #define TYPE_RESERVED 3
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
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
182 #define TAG_GLOB_MAX 12
184 #define DIGITIZER_USAGE_TIP_PRESSURE 0x30
185 #define DIGITIZER_USAGE_TILT_X 0x3D
186 #define DIGITIZER_USAGE_TILT_Y 0x3E
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:
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.
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.
204 static void parse_hid_report_descriptor(struct gtco *device, char * report,
205 int length)
207 int x,i=0;
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;
219 /* For parsing logic */
220 int inputnum = 0;
221 __u32 usage = 0;
223 /* Global Values, indexed by TAG */
224 __u32 globalval[TAG_GLOB_MAX];
225 __u32 oldval[TAG_GLOB_MAX];
227 /* Debug stuff */
228 char maintype='x';
229 char globtype[12];
230 int indent=0;
231 char indentstr[10]="";
235 dbg("======>>>>>>PARSE<<<<<<======");
237 /* Walk this report and pull out the info we need */
238 while (i<length){
239 prefix=report[i];
241 /* Skip over prefix */
242 i++;
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]))));
258 /* Skip size of data */
259 i+=size;
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){
269 case TAG_MAIN_INPUT:
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
277 maintype='I';
278 if (data==2){
279 strcpy(globtype,"Variable");
281 if (data==3){
282 strcpy(globtype,"Var|Const");
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]));
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
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];
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];
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];
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];
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];
340 break;
343 inputnum++;
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';
356 if (data==0){
357 dbg("======>>>>>> Physical");
358 strcpy(globtype,"Physical");
359 }else{
360 dbg("======>>>>>>");
363 /* Indent the debug output */
364 indent++;
365 for (x=0;x<indent;x++){
366 indentstr[x]='-';
368 indentstr[x]=0;
370 /* Save global tags */
371 for (x=0;x<TAG_GLOB_MAX;x++){
372 oldval[x] = globalval[x];
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]='-';
383 indentstr[x]=0;
385 /* Copy global tags back */
386 for (x=0;x<TAG_GLOB_MAX;x++){
387 globalval[x] = oldval[x];
390 break;
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;
407 break;
408 case TYPE_GLOBAL:
409 switch(tag){
410 case TAG_GLOB_USAGE:
412 * First time we hit the global usage tag,
413 * it should tell us the type of device
415 if (device->usage == 0){
416 device->usage = data;
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;
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;
475 }else{
476 dbg("%sGLOBALTAG: ILLEGAL TAG:%d SIZE: %d ",
477 indentstr,tag,size);
481 break;
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");
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;
515 break;
524 /* INPUT DRIVER Routines */
528 * Called when opening the input device. This will submit the URB to
529 * the usb system so we start getting reports
531 static int gtco_input_open(struct input_dev *inputdev)
533 struct gtco *device;
534 device = inputdev->private;
536 device->urbinfo->dev = device->usbdev;
537 if (usb_submit_urb(device->urbinfo, GFP_KERNEL)) {
538 return -EIO;
540 return 0;
544 Called when closing the input device. This will unlink the URB
546 static void gtco_input_close(struct input_dev *inputdev)
548 struct gtco *device = inputdev->private;
550 usb_kill_urb(device->urbinfo);
556 * Setup input device capabilities. Tell the input system what this
557 * device is capable of generating.
559 * This information is based on what is read from the HID report and
560 * placed in the struct gtco structure
563 static void gtco_setup_caps(struct input_dev *inputdev)
565 struct gtco *device = inputdev->private;
568 /* Which events */
569 inputdev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_MSC);
572 /* Misc event menu block */
573 inputdev->mscbit[0] = BIT(MSC_SCAN)|BIT(MSC_SERIAL)|BIT(MSC_RAW) ;
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);
582 /* Proximity */
583 input_set_abs_params(inputdev, ABS_DISTANCE, 0, 1, 0, 0);
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);
594 /* Transducer */
595 input_set_abs_params(inputdev, ABS_MISC, 0,0xFF, 0, 0);
601 /* USB Routines */
605 * URB callback routine. Called when we get IRQ reports from the
606 * digitizer.
608 * This bridges the USB and input device worlds. It generates events
609 * on the input device based on the USB reports.
611 static void gtco_urb_callback(struct urb *urbinfo)
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];
622 inputdev = device->inputdevice;
625 /* Was callback OK? */
626 if ((urbinfo->status == -ECONNRESET ) ||
627 (urbinfo->status == -ENOENT ) ||
628 (urbinfo->status == -ESHUTDOWN )){
630 /* Shutdown is occurring. Return and don't queue up any more */
631 return;
634 if (urbinfo->status != 0 ) {
635 /* Some unknown error. Hopefully temporary. Just go and */
636 /* requeue an URB */
637 goto resubmit;
641 * Good URB, now process
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 ))
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
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]);
664 /* Mask out the Y tilt value used for pressure */
665 device->buffer[7] = (u8)((device->buffer[7]) & 0x7F);
668 /* Fall thru */
669 case 4:
670 /* Tilt */
672 /* Sign extend these 7 bit numbers. */
673 if (device->buffer[6] & 0x40)
674 device->buffer[6] |= 0x80;
676 if (device->buffer[7] & 0x40)
677 device->buffer[7] |= 0x80;
680 valsigned = (device->buffer[6]);
681 input_report_abs(inputdev, ABS_TILT_X, (s32)valsigned);
683 valsigned = (device->buffer[7]);
684 input_report_abs(inputdev, ABS_TILT_Y, (s32)valsigned);
686 /* Fall thru */
688 case 2:
689 case 3:
690 /* Convert buttons, only 5 bits possible */
691 val = (device->buffer[5])&MASK_BUTTON;
693 /* We don't apply any meaning to the bitmask,
694 just report */
695 input_event(inputdev, EV_MSC, MSC_SERIAL, val);
697 /* Fall thru */
698 case 1:
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);
704 val = le16_to_cpu(get_unaligned((__le16 *) &(device->buffer[3])));
705 input_report_abs(inputdev, ABS_Y, val);
708 /* Ditto for proximity bit */
709 if (device->buffer[5]& MASK_INRANGE){
710 val = 1;
711 }else{
712 val=0;
714 input_report_abs(inputdev, ABS_DISTANCE, val);
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){
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);
729 * We don't apply any meaning to the button
730 * index, just report it
732 input_event(inputdev, EV_MSC, MSC_SERIAL, val);
737 break;
738 case 7:
739 /* Menu blocks */
740 input_event(inputdev, EV_MSC, MSC_SCAN,
741 device->buffer[1]);
744 break;
750 /* Other pid class */
751 if ((inputdev->id.product == PID_400 )||
752 (inputdev->id.product == PID_401 ))
755 /* Report 2 */
756 if (device->buffer[0] == 2){
757 /* Menu blocks */
758 input_event(inputdev, EV_MSC, MSC_SCAN,
759 device->buffer[1]);
762 /* Report 1 */
763 if (device->buffer[0] == 1){
764 char buttonbyte;
767 /* IF X max > 64K, we still a bit from the y report */
768 if (device->max_X > 0x10000){
770 val = (u16)(((u16)(device->buffer[2]<<8))|((u8)(device->buffer[1])));
771 val |= (u32)(((u8)device->buffer[3]&0x1)<< 16);
773 input_report_abs(inputdev, ABS_X, val);
775 le_buffer[0] = (u8)((u8)(device->buffer[3])>>1);
776 le_buffer[0] |= (u8)((device->buffer[3]&0x1)<<7);
778 le_buffer[1] = (u8)(device->buffer[4]>>1);
779 le_buffer[1] |= (u8)((device->buffer[5]&0x1)<<7);
781 val = le16_to_cpu(get_unaligned((__le16 *)(le_buffer)));
783 input_report_abs(inputdev, ABS_Y, val);
787 * Shift the button byte right by one to
788 * make it look like the standard report
790 buttonbyte = (device->buffer[5])>>1;
791 }else{
793 val = le16_to_cpu(get_unaligned((__le16 *) (&(device->buffer[1]))));
794 input_report_abs(inputdev, ABS_X, val);
796 val = le16_to_cpu(get_unaligned((__le16 *) (&(device->buffer[3]))));
797 input_report_abs(inputdev, ABS_Y, val);
799 buttonbyte = device->buffer[5];
804 /* BUTTONS and PROXIMITY */
805 if (buttonbyte& MASK_INRANGE){
806 val = 1;
807 }else{
808 val=0;
810 input_report_abs(inputdev, ABS_DISTANCE, val);
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));
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]);
828 /* Everybody gets report ID's */
829 input_event(inputdev, EV_MSC, MSC_RAW, device->buffer[0]);
831 /* Sync it up */
832 input_sync(inputdev);
834 resubmit:
835 rc = usb_submit_urb(urbinfo, GFP_ATOMIC);
836 if (rc != 0) {
837 err("usb_submit_urb failed rc=0x%x",rc);
843 * The probe routine. This is called when the kernel find the matching USB
844 * vendor/product. We do the following:
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
853 static int gtco_probe(struct usb_interface *usbinterface,
854 const struct usb_device_id *id)
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;
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;
873 device->inputdevice = input_allocate_device();
874 if (!device->inputdevice){
875 kfree(device);
876 err("No more memory");
877 return -ENOMEM;
880 /* Get pointer to the input device */
881 inputdev = device->inputdevice;
883 /* Save interface information */
884 device->usbdev = usb_get_dev(interface_to_usbdev(usbinterface));
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;
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;
910 * The endpoint is always altsetting 0, we know this since we know
911 * this device only has one interrupt endpoint
913 endpoint = &usbinterface->altsetting[0].endpoint[0].desc;
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");
923 dbg("endpoint extra len:%d ",usbinterface->altsetting[0].extralen);
928 * Find the HID descriptor so we can find out the size of the
929 * HID report descriptor
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;
941 dbg("Extra descriptor success: type:%d len:%d",
942 hid_desc->bDescriptorType, hid_desc->wDescriptorLength);
944 if (!(report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL))) {
945 usb_buffer_free(device->usbdev, REPORT_MAX_SIZE,
946 device->buffer, device->buf_dma);
948 input_free_device(device->inputdevice);
949 kfree(device);
950 err("No more memory");
951 return -ENOMEM;
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 */
966 if (result == hid_desc->wDescriptorLength)
967 break;
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;
984 /* Now we parse the report */
985 parse_hid_report_descriptor(device,report,result);
987 /* Now we delete it */
988 kfree(report);
990 /* Create a device file node */
991 usb_make_path(device->usbdev, path, PATHLENGTH);
992 sprintf(device->usbpath, "%s/input0", path);
995 /* Set Input device functions */
996 inputdev->open = gtco_input_open;
997 inputdev->close = gtco_input_close;
999 /* Set input device information */
1000 inputdev->name = "GTCO_CalComp";
1001 inputdev->phys = device->usbpath;
1002 inputdev->private = device;
1005 /* Now set up all the input device capabilities */
1006 gtco_setup_caps(inputdev);
1008 /* Set input device required ID information */
1009 usb_to_input_id(device->usbdev, &device->inputdevice->id);
1010 inputdev->cdev.dev = &usbinterface->dev;
1012 /* Setup the URB, it will be posted later on open of input device */
1013 endpoint = &usbinterface->altsetting[0].endpoint[0].desc;
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);
1025 device->urbinfo->transfer_dma = device->buf_dma;
1026 device->urbinfo->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1029 /* Save device pointer in USB interface device */
1030 usb_set_intfdata(usbinterface, device);
1032 /* All done, now register the input device */
1033 input_register_device(inputdev);
1035 info( "gtco driver created usb: %s\n", path);
1036 return 0;
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
1045 static void gtco_disconnect(struct usb_interface *interface)
1048 /* Grab private device ptr */
1049 struct gtco *device = usb_get_intfdata (interface);
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);
1061 info("gtco driver disconnected");
1065 /* STANDARD MODULE LOAD ROUTINES */
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,
1077 * Register this module with the USB subsystem
1079 static int __init gtco_init(void)
1081 int rc;
1082 rc = usb_register(&gtco_driverinfo_table);
1083 if (rc) {
1084 err("usb_register() failed rc=0x%x", rc);
1086 printk("GTCO usb driver version: %s",GTCO_VERSION);
1087 return rc;
1091 * Deregister this module with the USB subsystem
1093 static void __exit gtco_exit(void)
1095 usb_deregister(&gtco_driverinfo_table);
1098 module_init (gtco_init);
1099 module_exit (gtco_exit);
1101 MODULE_LICENSE("GPL");