Off-by-one (and comment) fix.
[xorg/acecad] / src / acecad.c
1 /*
2  * Copyright (c) 2001 Edouard TISSERANT <tissered@esstin.u-nancy.fr>
3  * Parts inspired from Shane Watts <shane@bofh.asn.au> XFree86 3 Acecad Driver
4  * Thanks to Emily, from AceCad, For giving me documents.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
21  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  *
25  */
26 /* $XFree86: xc/programs/Xserver/hw/xfree86/input/acecad/acecad.c,v 1.4 2003/10/30 00:40:45 dawes Exp $ */
27
28 #include "config.h"
29
30 #define _ACECAD_C_
31 /*****************************************************************************
32  *      Standard Headers
33  ****************************************************************************/
34
35 #ifdef LINUX_INPUT
36 #include <asm/types.h>
37 #include <linux/input.h>
38 #ifndef EV_SYN
39 #define EV_SYN EV_RST
40 #define SYN_REPORT 0
41 #endif
42 #ifdef BUS_PCI
43 #undef BUS_PCI
44 #endif
45 #ifdef BUS_ISA
46 #undef BUS_ISA
47 #endif
48 #endif
49
50 #include <misc.h>
51 #include <xf86.h>
52 #ifndef NEED_XF86_TYPES
53 #define NEED_XF86_TYPES
54 #endif
55 #include <xf86_OSproc.h>
56 #include <xisb.h>
57 #include <xf86Xinput.h>
58 #include <exevents.h>
59 #include <xf86Module.h>
60
61 #include <string.h>
62 #include <stdio.h>
63
64 #ifdef LINUX_INPUT
65 #include <errno.h>
66 #include <fcntl.h>
67 #ifdef LINUX_SYSFS
68 #include <sysfs/libsysfs.h>
69 #include <dlfcn.h>
70 #endif
71 #endif
72
73 /*****************************************************************************
74  *      Local Headers
75  ****************************************************************************/
76 #include "acecad.h"
77
78 /*****************************************************************************
79  *      Variables without includable headers
80  ****************************************************************************/
81
82 /*****************************************************************************
83  *      Local Variables
84  ****************************************************************************/
85
86 #undef read
87 #define read(a,b,c) xf86ReadSerial((a),(b),(c))
88
89 /* max number of input events to read in one read call */
90 #define MAX_EVENTS 50
91
92 _X_EXPORT InputDriverRec ACECAD =
93 {
94         1,
95         "acecad",
96         NULL,
97         AceCadPreInit,
98         NULL,
99         NULL,
100         0
101 };
102
103 #ifdef XFree86LOADER
104 static XF86ModuleVersionInfo VersionRec =
105 {
106         "acecad",
107         MODULEVENDORSTRING,
108         MODINFOSTRING1,
109         MODINFOSTRING2,
110         XORG_VERSION_CURRENT,
111         PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
112         ABI_CLASS_XINPUT,
113         ABI_XINPUT_VERSION,
114         MOD_CLASS_XINPUT,
115         {0, 0, 0, 0}
116 };
117
118
119 _X_EXPORT XF86ModuleData acecadModuleData = {
120         &VersionRec,
121         SetupProc,
122         TearDownProc
123 };
124
125 /*****************************************************************************
126  *      Function Definitions
127  ****************************************************************************/
128
129 static pointer
130 SetupProc(      pointer module,
131                 pointer options,
132                 int *errmaj,
133                 int *errmin )
134 {
135         xf86AddInputDriver(&ACECAD, module, 0);
136         return module;
137 }
138
139 static void
140 TearDownProc( pointer p )
141 {
142 #if 0
143         LocalDevicePtr local = (LocalDevicePtr) p;
144         AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
145
146         DeviceOff (local->dev);
147
148         xf86CloseSerial (local->fd);
149         XisbFree (priv->buffer);
150         xfree (priv);
151         xfree (local->name);
152         xfree (local);
153 #endif
154 }
155 #endif
156
157 static const char *default_options[] =
158 {
159         "BaudRate", "9600",
160         "StopBits", "1",
161         "DataBits", "8",
162         "Parity", "Odd",
163         "Vmin", "1",
164         "Vtime", "10",
165         "FlowControl", "Xoff",
166         NULL
167 };
168
169 #ifdef LINUX_INPUT
170 static int
171 IsUSBLine(int fd)
172 {
173     int version;
174     int err;
175
176     SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
177
178     if (!err) {
179         xf86MsgVerb(X_PROBED, 4, "Kernel Input driver version is %d.%d.%d\n",
180                 version >> 16, (version >> 8) & 0xff, version & 0xff);
181         return 1;
182     } else {
183         xf86MsgVerb(X_PROBED, 4, "No Kernel Input driver found\n");
184         return 0;
185     }
186 }
187
188 /* Heavily inspired by synaptics/eventcomm.c */
189
190 #define DEV_INPUT_EVENT "/dev/input/event"
191 #define EV_DEV_NAME_MAXLEN 64
192 #define SET_EVENT_NUM(str, num) \
193     snprintf(str, EV_DEV_NAME_MAXLEN, "%s%d", DEV_INPUT_EVENT, num)
194
195 static Bool
196 fd_query_acecad(int fd, char *ace_name) {
197     char name[256] = "Unknown";
198     int cmp_at = strlen(ace_name);
199     if (cmp_at > 255)
200         cmp_at = 255;
201     ioctl(fd, EVIOCGNAME(sizeof(name)), name);
202     name[cmp_at] = '\0';
203     if (xf86NameCmp(name, ace_name) == 0)
204         return TRUE;
205     return FALSE;
206 }
207
208 static char ace_name_default[7] = "acecad";
209
210 #ifdef LINUX_SYSFS
211 static char usb_bus_name[4] = "usb";
212 static char acecad_driver_name[11] = "usb_acecad";
213 #endif
214
215 static Bool
216 AceCadAutoDevProbe(LocalDevicePtr local, int verb)
217 {
218     /* We are trying to find the right eventX device */
219     int i = 0;
220     Bool have_evdev = FALSE;
221     int noent_cnt = 0;
222     const int max_skip = 10;
223     char *ace_name = xf86FindOptionValue(local->options, "Name");
224     char fname[EV_DEV_NAME_MAXLEN];
225     int np;
226
227 #ifdef LINUX_SYSFS
228     struct sysfs_bus *usb_bus = NULL;
229     struct sysfs_driver *acecad_driver = NULL;
230     struct sysfs_device *candidate = NULL;
231     char *link = NULL;
232     struct dlist *devs = NULL;
233     struct dlist *links = NULL;
234     unsigned int major = 0, minor = 0;
235     void *libsysfs = NULL;
236
237     if (libsysfs = dlopen("libsysfs.so", RTLD_NOW | RTLD_GLOBAL)) {
238         xf86MsgVerb(X_INFO, verb, "%s: querying sysfs for Acecad tablets\n", local->name);
239         usb_bus = sysfs_open_bus(usb_bus_name);
240         if (usb_bus) {
241             xf86MsgVerb(X_PROBED, 4, "%s: usb bus opened\n", local->name);
242             acecad_driver = sysfs_get_bus_driver(usb_bus, acecad_driver_name);
243             if (acecad_driver) {
244                 xf86MsgVerb(X_PROBED, 4, "%s: usb_acecad driver opened\n", local->name);
245                 devs = sysfs_get_driver_devices(acecad_driver);
246                 if (devs) {
247                     xf86MsgVerb(X_PROBED, 4, "%s: usb_acecad devices retrieved\n", local->name);
248                     dlist_for_each_data(devs, candidate, struct sysfs_device) {
249                         xf86MsgVerb(X_PROBED, 4, "%s: device %s at %s\n", local->name, candidate->name, candidate->path);
250                         links = sysfs_open_link_list(candidate->path);
251                         dlist_for_each_data(links, link, char) {
252                             if (sscanf(link, "input:event%d", &i) == 1) {
253                                 xf86MsgVerb(X_PROBED, 4, "%s: device %s at %s: %s\n", local->name, candidate->name, candidate->path, link);
254                                 break;
255                             }
256                         }
257                         sysfs_close_list(links);
258                         if (i > 0) /* We found something */
259                             break;
260                     }
261                 } else
262                     xf86MsgVerb(X_WARNING, 4, "%s: no usb_acecad devices found\n", local->name);
263             } else
264                 xf86MsgVerb(X_WARNING, 4, "%s: usb_acecad driver not found\n", local->name);
265         } else
266             xf86MsgVerb(X_WARNING, 4, "%s: usb bus not found\n", local->name);
267         sysfs_close_bus(usb_bus);
268         dlclose(libsysfs);
269
270         if (i > 0) {
271             /* We found something */
272             np = SET_EVENT_NUM(fname, i);
273             if (np < 0 || np >= EV_DEV_NAME_MAXLEN) {
274                 xf86MsgVerb(X_WARNING, verb, "%s: unable to manage event device %d\n", local->name, i);
275             } else {
276                 goto ProbeFound;
277             }
278         } else
279             xf86MsgVerb(X_WARNING, verb, "%s: no Acecad devices found via sysfs\n", local->name);
280     } else
281         xf86MsgVerb(X_WARNING, 4, "%s: libsysfs not found\n", local->name);
282
283 #endif
284
285     if (!ace_name)
286         ace_name = ace_name_default;
287
288     xf86MsgVerb(X_INFO, verb, "%s: probing event devices for Acecad tablets\n", local->name);
289     for (i = 0; ; i++) {
290         int fd = -1;
291         Bool is_acecad;
292
293         np = SET_EVENT_NUM(fname, i);
294         if (np < 0 || np >= EV_DEV_NAME_MAXLEN) {
295             xf86MsgVerb(X_WARNING, verb, "%s: too many devices, giving up %d\n", local->name, i);
296             break;
297         }
298         SYSCALL(fd = open(fname, O_RDONLY));
299         if (fd < 0) {
300             if (errno == ENOENT) {
301                 if (++noent_cnt >= max_skip)
302                     break;
303                 else
304                     continue;
305             } else {
306                 continue;
307             }
308         }
309         noent_cnt = 0;
310         have_evdev = TRUE;
311         is_acecad = fd_query_acecad(fd, ace_name);
312         SYSCALL(close(fd));
313         if (is_acecad) {
314             goto ProbeFound;
315         }
316     }
317     xf86MsgVerb(X_WARNING, verb, "%s: no Acecad event device found (checked %d nodes, no device name started with '%s')\n",
318             local->name, i + 1, ace_name);
319     if (i <= max_skip)
320         xf86MsgVerb(X_WARNING, verb, "%s: The /dev/input/event* device nodes seem to be missing\n",
321                 local->name);
322     if (i > max_skip && !have_evdev)
323         xf86MsgVerb(X_WARNING, verb, "%s: The evdev kernel module seems to be missing\n", local->name);
324     return FALSE;
325
326 ProbeFound:
327     xf86Msg(X_PROBED, "%s auto-dev sets device to %s\n",
328             local->name, fname);
329     xf86ReplaceStrOption(local->options, "Device", fname);
330     return TRUE;
331 }
332
333 #endif
334
335 static InputInfoPtr
336 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
337 {
338     LocalDevicePtr local = xf86AllocateInput(drv, 0);
339     AceCadPrivatePtr priv = xcalloc (1, sizeof(AceCadPrivateRec));
340     int speed;
341     int msgtype;
342     char *s;
343
344     if ((!local) || (!priv))
345         goto SetupProc_fail;
346
347     memset(priv, 0, sizeof(AceCadPrivateRec));
348
349     local->name = dev->identifier;
350     local->type_name = "ACECAD Tablet";
351     local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
352     local->motion_history_proc = xf86GetMotionEvents;
353     local->control_proc = NULL;
354     local->close_proc = CloseProc;
355     local->switch_mode = NULL;
356     local->conversion_proc = ConvertProc;
357     local->reverse_conversion_proc = ReverseConvertProc;
358     local->dev = NULL;
359     local->private = priv;
360     local->private_flags = 0;
361     local->conf_idev = dev;
362     local->device_control = DeviceControl;
363     /*local->always_core_feedback = 0;*/
364
365     xf86CollectInputOptions(local, default_options, NULL);
366
367     xf86OptionListReport(local->options);
368
369     priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
370
371     s = xf86FindOptionValue(local->options, "Device");
372     if (!s || (s && (xf86NameCmp(s, "auto-dev") == 0))) {
373 #ifdef LINUX_INPUT
374         priv->flags |= AUTODEV_FLAG;
375         if (!AceCadAutoDevProbe(local, 0))
376         {
377             xf86Msg(X_ERROR, "%s: unable to find device\n", local->name);
378             goto SetupProc_fail;
379         }
380 #else
381         xf86Msg(X_NOT_IMPLEMENTED, "%s: device autodetection not implemented, sorry\n", local->name);
382         goto SetupProc_fail;
383 #endif
384     }
385
386     local->fd = xf86OpenSerial (local->options);
387     if (local->fd == -1)
388     {
389         xf86Msg(X_ERROR, "%s: unable to open device\n", local->name);
390         goto SetupProc_fail;
391     }
392     xf86ErrorFVerb( 6, "tty port opened successfully\n" );
393
394 #ifdef LINUX_INPUT
395     if (IsUSBLine(local->fd)) {
396         priv->flags |= USB_FLAG;
397
398         local->read_input = USBReadInput;
399
400         if (USBQueryHardware(local) != Success)
401         {
402             xf86Msg(X_ERROR, "%s: unable to query/initialize hardware (not an %s?).\n", local->name, local->type_name);
403             goto SetupProc_fail;
404         }
405     } else
406 #endif
407     {
408         local->read_input = ReadInput;
409
410         msgtype = X_DEFAULT;
411         if (xf86FindOptionValue(local->options, "ReportSpeed")) {
412             msgtype = X_CONFIG;
413             speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
414         } else {
415             speed = 85;
416         }
417
418         switch (speed)
419         {
420             case 120:
421                 priv->acecadReportSpeed = 'Q';
422                 break;
423             case 85:
424                 priv->acecadReportSpeed = 'R';
425                 break;
426             case 10:
427                 priv->acecadReportSpeed = 'S';
428                 break;
429             case 2:
430                 priv->acecadReportSpeed = 'T';
431                 break;
432             default:
433                 priv->acecadReportSpeed = 'R';
434                 speed = 85;
435                 xf86Msg(X_ERROR, "%s: ReportSpeed value %d invalid. Possible values: 120, 85, 10, 2. Defaulting to 85\n", local->name, speed);
436                 msgtype = X_DEFAULT;
437         }
438
439         xf86Msg(msgtype, "%s report %d points/s\n", local->name, speed);
440
441         priv->buffer = XisbNew (local->fd, 200);
442
443         /*
444          * Verify that hardware is attached and fuctional
445          */
446         if (QueryHardware(priv) != Success)
447         {
448             xf86Msg(X_ERROR, "%s: unable to query/initialize hardware (not an %s?).\n", local->name, local->type_name);
449             goto SetupProc_fail;
450         }
451     }
452
453     s = xf86FindOptionValue(local->options, "Mode");
454     msgtype = s ? X_CONFIG : X_DEFAULT;
455     if (!(s && (xf86NameCmp(s, "relative") == 0)))
456     {
457         priv->flags |= ABSOLUTE_FLAG;
458     }
459
460     xf86Msg(msgtype, "%s is in %s mode\n", local->name, (priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
461     DBG (9, XisbTrace (priv->buffer, 1));
462
463     local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
464
465     xf86ProcessCommonOptions(local, local->options);
466
467     local->flags |= XI86_CONFIGURED;
468
469     if (local->fd != -1)
470     {
471         RemoveEnabledDevice (local->fd);
472         if (priv->buffer)
473         {
474             XisbFree(priv->buffer);
475             priv->buffer = NULL;
476         }
477         xf86CloseSerial(local->fd);
478     }
479     RemoveEnabledDevice (local->fd);
480     local->fd = -1;
481     return local;
482
483     /*
484      * If something went wrong, cleanup and return NULL
485      */
486 SetupProc_fail:
487     if ((local) && (local->fd))
488         xf86CloseSerial (local->fd);
489     if ((priv) && (priv->buffer))
490         XisbFree (priv->buffer);
491     if (priv)
492         xfree (priv);
493     return NULL;
494 }
495
496 static Bool
497 DeviceControl (DeviceIntPtr dev, int mode)
498 {
499     Bool RetValue;
500
501     switch (mode)
502     {
503         case DEVICE_INIT:
504             DeviceInit(dev);
505             RetValue = Success;
506             break;
507         case DEVICE_ON:
508             RetValue = DeviceOn(dev);
509             break;
510         case DEVICE_OFF:
511             RetValue = DeviceOff(dev);
512             break;
513         case DEVICE_CLOSE:
514             RetValue = DeviceClose(dev);
515             break;
516         default:
517             RetValue = BadValue;
518     }
519
520     return RetValue;
521 }
522
523 static Bool
524 DeviceOn (DeviceIntPtr dev)
525 {
526     char buffer[256];
527     LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
528     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
529
530     xf86MsgVerb(X_INFO, 4, "%s Device On\n", local->name);
531
532     local->fd = xf86OpenSerial(local->options);
533     if (local->fd == -1)
534     {
535         xf86Msg(X_WARNING, "%s: cannot open input device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
536         priv->flags &= ~AVAIL_FLAG;
537 #ifdef LINUX_INPUT
538         if ((priv->flags & AUTODEV_FLAG) && AceCadAutoDevProbe(local, 4))
539             local->fd = xf86OpenSerial(local->options);
540         if (local->fd == -1)
541 #endif
542             return !Success;
543     }
544     priv->flags |= AVAIL_FLAG;
545
546
547     if (!(priv->flags & USB_FLAG)) {
548         priv->buffer = XisbNew(local->fd, 200);
549         if (!priv->buffer)
550         {
551             xf86CloseSerial(local->fd);
552             local->fd = -1;
553             return !Success;
554         }
555
556         /* Rets qu'a l'envoyer a la tablette */
557         sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed, ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
558         XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
559     }
560
561     xf86FlushInput(local->fd);
562     xf86AddEnabledDevice (local);
563     dev->public.on = TRUE;
564     return Success;
565 }
566
567 static Bool
568 DeviceOff (DeviceIntPtr dev)
569 {
570     LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
571     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
572
573     xf86MsgVerb(X_INFO, 4, "%s Device Off\n", local->name);
574
575     if (local->fd != -1)
576     {
577         RemoveEnabledDevice (local->fd);
578         if (priv->buffer)
579         {
580             XisbFree(priv->buffer);
581             priv->buffer = NULL;
582         }
583         xf86CloseSerial(local->fd);
584     }
585
586
587     xf86RemoveEnabledDevice (local);
588     dev->public.on = FALSE;
589     return Success;
590 }
591
592 static Bool
593 DeviceClose (DeviceIntPtr dev)
594 {
595     LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
596
597     xf86MsgVerb(X_INFO, 4, "%s Device Close\n", local->name);
598
599     return Success;
600 }
601
602 static void
603 ControlProc(DeviceIntPtr dev, PtrCtrl *ctrl)
604 {
605     LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
606
607     xf86MsgVerb(X_INFO, 4, "%s Control Proc\n", local->name);
608 }
609
610 static Bool
611 DeviceInit (DeviceIntPtr dev)
612 {
613     int rx, ry;
614     LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
615     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
616     unsigned char map[] =
617     {0, 1, 2, 3};
618
619     xf86MsgVerb(X_INFO, 4, "%s Init\n", local->name);
620
621     /* 3 boutons */
622     if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
623     {
624         xf86Msg(X_ERROR, "%s: unable to allocate ButtonClassDeviceStruct\n", local->name);
625         return !Success;
626     }
627
628     if (InitFocusClassDeviceStruct (dev) == FALSE)
629     {
630         xf86Msg(X_ERROR, "%s: unable to allocate FocusClassDeviceStruct\n", local->name);
631         return !Success;
632     }
633
634     if (InitPtrFeedbackClassDeviceStruct(dev, ControlProc) == FALSE) {
635         xf86Msg(X_ERROR, "%s: unable to init ptr feedback\n", local->name);
636         return !Success;
637     }
638
639
640     /* 3 axes */
641     if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
642                 local->history_size,
643                 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
644             == FALSE)
645     {
646         xf86Msg(X_ERROR, "%s: unable to allocate ValuatorClassDeviceStruct\n", local->name);
647         return !Success;
648     }
649     else
650     {
651
652         InitValuatorAxisStruct(dev,
653                 0,
654                 0,                      /* min val */
655                 priv->acecadMaxX,       /* max val */
656                 1000,                   /* resolution */
657                 0,                      /* min_res */
658                 1000);                  /* max_res */
659         InitValuatorAxisStruct(dev,
660                 1,
661                 0,                      /* min val */
662                 priv->acecadMaxY,       /* max val */
663                 1000,                   /* resolution */
664                 0,                      /* min_res */
665                 1000);                  /* max_res */
666         InitValuatorAxisStruct(dev,
667                 2,
668                 0,                      /* min val */
669                 priv->acecadMaxZ,       /* max val */
670                 1000,                   /* resolution */
671                 0,                      /* min_res */
672                 1000);          /* max_res */
673
674     }
675
676     if (InitProximityClassDeviceStruct (dev) == FALSE)
677     {
678         xf86Msg(X_ERROR, "%s: unable to allocate ProximityClassDeviceStruct\n", local->name);
679         return !Success;
680     }
681
682     xf86MotionHistoryAllocate (local);
683
684
685     /* On ne peut pas calculer l'increment avant, faute d'ecran pour
686        connaitre la taille... */
687
688     if (priv->acecadInc > 95)
689         priv->acecadInc = 95;
690     if (priv->acecadInc < 1)
691     {
692         /* guess the best increment value given video mode */
693         rx = priv->acecadMaxX / screenInfo.screens[0]->width;
694         ry = priv->acecadMaxY / screenInfo.screens[0]->height;
695         if (rx < ry)
696             priv->acecadInc = rx;
697         else
698             priv->acecadInc = ry;
699         if (priv->acecadInc < 1)
700             priv->acecadInc = 1;
701     }
702
703     xf86Msg(X_INFO, "%s Increment: %d\n", local->name, priv->acecadInc);
704
705     return Success;
706 }
707
708 static void
709 ReadInput (LocalDevicePtr local)
710 {
711     int x, y, z;
712     int prox, buttons;
713     int is_core_pointer, is_absolute;
714     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
715
716     /*xf86Msg(X_INFO, "ACECAD Tablet Read Input\n");*/
717
718     is_absolute = (priv->flags & ABSOLUTE_FLAG);
719     is_core_pointer = xf86IsCorePointer(local->dev);
720
721     /*
722      * set blocking to -1 on the first call because we know there is data to
723      * read. Xisb automatically clears it after one successful read so that
724      * succeeding reads are preceeded buy a select with a 0 timeout to prevent
725      * read from blocking indefinately.
726      */
727     XisbBlockDuration (priv->buffer, -1);
728
729     while (AceCadGetPacket (priv) == Success)
730     {
731         x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
732         y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
733
734         if (!(priv->flags & ABSOLUTE_FLAG))
735         {
736             x = priv->packet[0] & XSIGN_BIT? x:-x;
737             y = priv->packet[0] & YSIGN_BIT? y:-y;
738         }
739         else
740         {
741             y = priv->acecadMaxY - y ;
742         }
743
744
745         z = ((int)priv->packet[5] << 2) |
746             (((int)priv->packet[6] & 0x01) << 1) |
747             (((int)priv->packet[6] & 0x10) >> 4);
748
749         buttons = ((int)priv->packet[0] & 0x07) |
750             ((int)priv->packet[6] & 0x02 << 2);
751
752         prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
753
754         if (prox)
755         {
756             if (!(priv->acecadOldProximity))
757                 if (!is_core_pointer)
758                 {
759                     /*xf86Msg(X_INFO, "ACECAD Tablet ProxIN %d %d %d\n",x, y, z);*/
760                     xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
761                 }
762
763             if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
764                     || (!is_absolute && (x || y)))
765             {
766                 if (is_absolute || priv->acecadOldProximity)
767                 {
768                     /*xf86Msg(X_INFO, "ACECAD Tablet Motion %d %d %d\n", x, y, z);*/
769                     xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
770                 }
771             }
772
773             if (priv->acecadOldButtons != buttons)
774             {
775                 int delta = buttons ^ priv->acecadOldButtons;
776                 while (delta)
777                 {
778                     int id = ffs(delta);
779                     delta &= ~(1 << (id-1));
780
781                     /*xf86Msg(X_INFO, "ACECAD Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
782                     xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
783                 }
784             }
785
786             priv->acecadOldButtons = buttons;
787             priv->acecadOldX = x;
788             priv->acecadOldY = y;
789             priv->acecadOldZ = z;
790             priv->acecadOldProximity = prox;
791         }
792         else
793         {
794             if (!is_core_pointer)
795                 if (priv->acecadOldProximity)
796                 {
797                     /*xf86Msg(X_INFO, "ACECAD Tablet ProxOUT %d %d %d\n",x, y, z);*/
798                     xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
799                 }
800             priv->acecadOldProximity = 0;
801         }
802     }
803     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
804 }
805
806 #ifdef LINUX_INPUT
807 #define set_bit(byte,nb,bit)    (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
808 static void
809 USBReadInput (LocalDevicePtr local)
810 {
811     int len;
812     struct input_event * event;
813     char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
814     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
815     int x = priv->acecadOldX;
816     int y = priv->acecadOldY;
817     int z = priv->acecadOldZ;
818     int prox = priv->acecadOldProximity;
819     int buttons = priv->acecadOldButtons;
820     int is_core_pointer = xf86IsCorePointer(local->dev);
821     /* Is autodev active? */
822     int autodev = priv->flags & AUTODEV_FLAG;
823     /* Was the device available last time we checked? */
824     int avail = priv->flags & AVAIL_FLAG;
825
826     SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
827
828     if (len <= 0) {
829         if (avail) {
830             xf86Msg(X_ERROR, "%s: error reading device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
831         }
832         if (NOTAVAIL) {
833             priv->flags &= ~AVAIL_FLAG;
834             if(autodev) {
835                 if (AceCadAutoDevProbe(local, 4)) {
836                     DeviceOff(local->dev);
837                     DeviceOn(local->dev);
838                 }
839             }
840         }
841         return;
842     } else {
843         if (!avail) {
844             /* If the device wasn't available last time we checked */
845             xf86Msg(X_INFO, "%s: device %s is available again\n", local->name, xf86FindOptionValue(local->options, "Device"));
846             priv->flags |= AVAIL_FLAG;
847         }
848     }
849
850     for (event = (struct input_event *)eventbuf;
851             event < (struct input_event *)(eventbuf+len); event++) {
852
853         switch (event->type) {
854             case EV_SYN: /* 2.6.x */
855                 if (event->code != SYN_REPORT)
856                     xf86Msg(X_ERROR, "%s: unknown EV_SYN code %d\n", local->name, event->code);
857                 break;
858             case EV_ABS:
859                 switch (event->code) {
860                     case ABS_X:
861                         x = event->value;
862                         break;
863
864                     case ABS_Y:
865                         y = event->value;
866                         break;
867
868                     case ABS_PRESSURE:
869                         z = event->value;
870                         break;
871
872                     case ABS_MISC:
873                         break;
874
875                 }
876                 break; /* EV_ABS */
877
878             case EV_KEY:
879                 switch (event->code) {
880                     case BTN_TOOL_PEN:
881                         prox = event->value;
882                         break;
883
884                     case BTN_TOUCH:
885                         buttons = set_bit(buttons,0,event->value);
886                         break;
887
888                     case BTN_STYLUS:
889                         buttons = set_bit(buttons,1,event->value);
890                         break;
891
892                     case BTN_STYLUS2:
893                         buttons = set_bit(buttons,2,event->value);
894                         break;
895                 }
896                 break; /* EV_KEY */
897             default:
898                 xf86Msg(X_ERROR, "%s: unknown event type/code %d/%d\n", local->name, event->type, event->code);
899         } /* switch event->type */
900
901         /* Linux Kernel 2.6.x sends EV_SYN/SYN_REPORT as an event terminator,
902          * whereas 2.4.x sends EV_ABS/ABS_MISC. We have to support both.
903          */
904         if (!(  (event->type == EV_SYN && event->code == SYN_REPORT) ||
905                     (event->type == EV_ABS && event->code == ABS_MISC)
906              )) {
907             continue;
908         }
909
910         if (prox)
911         {
912             if (!(priv->acecadOldProximity))
913                 if (!is_core_pointer)
914                 {
915                     xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
916                 }
917
918
919             xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
920
921             if (priv->acecadOldButtons != buttons)
922             {
923                 int delta = buttons ^ priv->acecadOldButtons;
924                 while (delta)
925                 {
926                     int id = ffs(delta);
927                     delta &= ~(1 << (id-1));
928
929                     xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
930                 }
931             }
932         }
933         else
934         {
935             if (!is_core_pointer)
936                 if (priv->acecadOldProximity)
937                 {
938                     xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
939                 }
940             priv->acecadOldProximity = 0;
941         }
942
943         priv->acecadOldButtons = buttons;
944         priv->acecadOldX = x;
945         priv->acecadOldY = y;
946         priv->acecadOldZ = z;
947         priv->acecadOldProximity = prox;
948     }
949     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
950 }
951 #endif
952
953 static void
954 CloseProc (LocalDevicePtr local)
955 {
956 }
957
958 /*
959  * The ConvertProc function may need to be tailored for your device.
960  * This function converts the device's valuator outputs to x and y coordinates
961  * to simulate mouse events.
962  */
963 static Bool
964 ConvertProc (LocalDevicePtr local, int first, int num,
965         int v0, int v1, int v2, int v3, int v4, int v5,
966         int *x, int *y)
967 {
968     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
969
970     *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
971     *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
972     return TRUE;
973 }
974
975
976 static Bool
977 ReverseConvertProc (LocalDevicePtr local,
978         int x, int  y,
979         int *valuators)
980 {
981     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
982
983     valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
984     valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
985
986     return TRUE;
987 }
988
989
990 #define WriteString(str)\
991     XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
992
993
994 static Bool
995 QueryHardware (AceCadPrivatePtr priv)
996 {
997
998     /* Reset */
999     WriteString("z0");
1000
1001     /* Wait */
1002     milisleep (250);
1003
1004     /* Prompt Mode in order to not be disturbed */
1005     WriteString(ACECAD_PROMPT_MODE);
1006
1007     /* Flush */
1008     while (XisbRead(priv->buffer) >= 0);
1009
1010     /* Ask for Config packet*/
1011     WriteString(ACECAD_CONFIG);
1012
1013     /* Read the packet */
1014     XisbBlockDuration (priv->buffer, 1000000);
1015     NewPacket (priv);
1016
1017     /*xf86Msg(X_CONFIG, "ACECAD Tablet init envoyé \n");*/
1018
1019     if ((AceCadGetPacket (priv) == Success))
1020     {
1021         priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
1022         priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
1023         priv->acecadMaxZ = 512;
1024         xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d\n", priv->acecadMaxX, priv->acecadMaxY);
1025     }
1026     else
1027         return !Success;
1028
1029     /*xf86Msg(X_INFO, "ACECAD Tablet query hardware fini \n");*/
1030     return Success;
1031 }
1032
1033 #define BITS_PER_LONG (sizeof(long) * 8)
1034 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
1035 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
1036 #define OFF(x)  ((x)%BITS_PER_LONG)
1037 #define LONG(x) ((x)/BITS_PER_LONG)
1038
1039 #ifdef LINUX_INPUT
1040 static Bool
1041 USBQueryHardware (LocalDevicePtr local)
1042 {
1043     AceCadPrivatePtr    priv = (AceCadPrivatePtr) local->private;
1044     unsigned long       bit[EV_MAX][NBITS(KEY_MAX)];
1045     int                 i, j;
1046     int                 abs[5];
1047     char                name[256] = "Unknown";
1048
1049     ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
1050     xf86MsgVerb(X_PROBED, 4, "Kernel Input device name: \"%s\"\n", name);
1051
1052     memset(bit, 0, sizeof(bit));
1053     ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
1054
1055     for (i = 0; i < EV_MAX; i++)
1056         if (test_bit(i, bit[0])) {
1057             ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
1058             for (j = 0; j < KEY_MAX; j++)
1059                 if (test_bit(j, bit[i])) {
1060                     if (i == EV_ABS) {
1061                         ioctl(local->fd, EVIOCGABS(j), abs);
1062                         switch (j) {
1063                             case ABS_X:
1064                                 priv->acecadMaxX = abs[2];
1065                                 break;
1066
1067                             case ABS_Y:
1068                                 priv->acecadMaxY = abs[2];
1069                                 break;
1070
1071                             case ABS_PRESSURE:
1072                                 priv->acecadMaxZ = abs[2];
1073                                 break;
1074                         }
1075                     }
1076                 }
1077         }
1078
1079     xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d MaxZ:%d\n", priv->acecadMaxX, priv->acecadMaxY, priv->acecadMaxZ);
1080     return Success;
1081 }
1082 #endif
1083
1084 static void
1085 NewPacket (AceCadPrivatePtr priv)
1086 {
1087     priv->packeti = 0;
1088 }
1089
1090 static Bool
1091 AceCadGetPacket (AceCadPrivatePtr priv)
1092 {
1093     int count = 0;
1094     int c = 0;
1095
1096     while((c = XisbRead(priv->buffer)) >= 0 )
1097     {
1098
1099         /*
1100          * fail after 500 bytes so the server doesn't hang forever if a
1101          * device sends bad data.
1102          */
1103         if (count++ > 500)
1104         {
1105             NewPacket (priv);
1106             return !Success;
1107         }
1108
1109         if (c & PHASING_BIT)
1110         {
1111             NewPacket(priv);
1112
1113             /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
1114             XisbBlockDuration (priv->buffer, 10000);
1115             priv->packet[priv->packeti++] = c;
1116             count = ACECAD_PACKET_SIZE - 1;
1117             while (count-- && (c = XisbRead(priv->buffer)) >= 0)
1118             {
1119                 /*xf86Msg(X_INFO, "Push %2.2x\n",(char) c);*/
1120                 priv->packet[priv->packeti++] = c;
1121             }
1122             XisbBlockDuration (priv->buffer, 0);
1123             if(c > 0)
1124                 return Success;
1125         }
1126     }
1127     return !Success;
1128 }