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.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 /* $XFree86: xc/programs/Xserver/hw/xfree86/input/acecad/acecad.c,v 1.4 2003/10/30 00:40:45 dawes Exp $ */
31 /*****************************************************************************
33 ****************************************************************************/
36 #include <asm/types.h>
37 #include <linux/input.h>
52 #ifndef NEED_XF86_TYPES
53 #define NEED_XF86_TYPES
55 #include <xf86_OSproc.h>
57 #include <xf86Xinput.h>
59 #include <xf86Module.h>
68 /*****************************************************************************
70 ****************************************************************************/
73 /*****************************************************************************
74 * Variables without includable headers
75 ****************************************************************************/
77 /*****************************************************************************
79 ****************************************************************************/
82 #define read(a,b,c) xf86ReadSerial((a),(b),(c))
84 /* max number of input events to read in one read call */
87 _X_EXPORT InputDriverRec ACECAD =
99 static XF86ModuleVersionInfo VersionRec =
105 XORG_VERSION_CURRENT,
106 PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
114 _X_EXPORT XF86ModuleData acecadModuleData = {
120 /*****************************************************************************
121 * Function Definitions
122 ****************************************************************************/
125 SetupProc( pointer module,
130 xf86AddInputDriver(&ACECAD, module, 0);
135 TearDownProc( pointer p )
138 LocalDevicePtr local = (LocalDevicePtr) p;
139 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
141 DeviceOff (local->dev);
143 xf86CloseSerial (local->fd);
144 XisbFree (priv->buffer);
152 static const char *default_options[] =
160 "FlowControl", "Xoff",
171 SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
174 xf86Msg(X_PROBED, "Kernel Input driver version is %d.%d.%d\n",
175 version >> 16, (version >> 8) & 0xff, version & 0xff);
178 xf86Msg(X_PROBED, "No Kernel Input driver found\n");
185 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
187 LocalDevicePtr local = xf86AllocateInput(drv, 0);
188 AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
193 if ((!local) || (!priv))
196 memset(priv,0,sizeof (AceCadPrivateRec));
198 local->name = dev->identifier;
199 local->type_name = "AceCad Tablet";
200 local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
201 local->motion_history_proc = xf86GetMotionEvents;
202 local->control_proc = NULL;
203 local->close_proc = CloseProc;
204 local->switch_mode = NULL;
205 local->conversion_proc = ConvertProc;
206 local->reverse_conversion_proc = ReverseConvertProc;
208 local->private = priv;
209 local->private_flags = 0;
210 local->conf_idev = dev;
211 local->device_control = DeviceControl;
212 /*local->always_core_feedback = 0;*/
214 xf86CollectInputOptions(local, default_options, NULL);
216 xf86OptionListReport(local->options);
218 priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
220 local->fd = xf86OpenSerial (local->options);
223 xf86Msg(X_ERROR, "AceCad driver unable to open device\n");
226 xf86ErrorFVerb( 6, "tty port opened successfully\n" );
229 if(IsUSBLine(local->fd)){
232 local->read_input = USBReadInput;
234 if (USBQueryHardware(local) != Success)
236 ErrorF ("Unable to query/initialize AceCad hardware.\n");
244 local->read_input = ReadInput;
247 if (xf86FindOptionValue(local->options, "ReportSpeed")) {
249 speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
257 priv->acecadReportSpeed = 'Q';
260 priv->acecadReportSpeed = 'R';
263 priv->acecadReportSpeed = 'S';
266 priv->acecadReportSpeed = 'T';
269 priv->acecadReportSpeed = 'R';
271 xf86Msg(X_ERROR, "Acecad Tablet: ReportSpeed value %d invalid. Possible values: 120, 85, 10, 2. Defaulting to 85\n", speed);
275 xf86Msg(msgtype, "Acecad Tablet report %d points/s\n", speed);
277 priv->buffer = XisbNew (local->fd, 200);
280 * Verify that hardware is attached and fuctional
282 if (QueryHardware(priv) != Success)
284 xf86Msg(X_ERROR, "Unable to query/initialize AceCad hardware.\n");
289 s = xf86FindOptionValue(local->options, "Mode");
290 msgtype = s ? X_CONFIG : X_DEFAULT;
291 if (s && (xf86NameCmp(s, "Relative") == 0))
293 priv->flags = priv->flags & ~ABSOLUTE_FLAG;
297 priv->flags = priv->flags | ABSOLUTE_FLAG;
300 xf86Msg(msgtype, "Acecad Tablet is in %s mode\n", (priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
301 DBG (9, XisbTrace (priv->buffer, 1));
303 local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
305 xf86ProcessCommonOptions(local, local->options);
307 local->flags |= XI86_CONFIGURED;
311 RemoveEnabledDevice (local->fd);
314 XisbFree(priv->buffer);
317 xf86CloseSerial(local->fd);
319 RemoveEnabledDevice (local->fd);
324 * If something went wrong, cleanup and return NULL
327 if ((local) && (local->fd))
328 xf86CloseSerial (local->fd);
329 if ((priv) && (priv->buffer))
330 XisbFree (priv->buffer);
337 DeviceControl (DeviceIntPtr dev, int mode)
348 RetValue = DeviceOn( dev );
351 RetValue = DeviceOff( dev );
354 RetValue = DeviceClose( dev );
364 DeviceOn (DeviceIntPtr dev)
367 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
368 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
370 xf86Msg(X_INFO, "Acecad Tablet Device On\n");
372 local->fd = xf86OpenSerial(local->options);
375 xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
380 if (priv->acecadUSB==0){
381 priv->buffer = XisbNew(local->fd, 200);
384 xf86CloseSerial(local->fd);
389 /*Rets qu'a l'envoyer a la tablette */
390 sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed, ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
391 XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
394 xf86FlushInput(local->fd);
395 xf86AddEnabledDevice (local);
396 dev->public.on = TRUE;
401 DeviceOff (DeviceIntPtr dev)
403 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
404 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
409 RemoveEnabledDevice (local->fd);
412 XisbFree(priv->buffer);
415 xf86CloseSerial(local->fd);
419 xf86RemoveEnabledDevice (local);
420 dev->public.on = FALSE;
425 DeviceClose (DeviceIntPtr dev)
427 xf86Msg(X_INFO, "Acecad Tablet Device Close\n");
432 ControlProc(DeviceIntPtr device,
435 xf86Msg(X_INFO, "Acecad Tablet Control Proc\n");
439 DeviceInit (DeviceIntPtr dev)
442 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
443 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
444 unsigned char map[] =
447 xf86Msg(X_INFO, "Acecad Tablet Device Init\n");
450 if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
452 ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
456 if (InitFocusClassDeviceStruct (dev) == FALSE)
458 ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
462 if (InitPtrFeedbackClassDeviceStruct(dev,
463 ControlProc) == FALSE) {
464 ErrorF("unable to init ptr feedback\n");
470 if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
472 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
475 ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
481 InitValuatorAxisStruct(dev,
484 priv->acecadMaxX, /* max val */
485 1000, /* resolution */
488 InitValuatorAxisStruct(dev,
491 priv->acecadMaxY, /* max val */
492 1000, /* resolution */
495 InitValuatorAxisStruct(dev,
498 priv->acecadMaxZ, /* max val */
499 1000, /* resolution */
505 if (InitProximityClassDeviceStruct (dev) == FALSE)
507 ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
511 xf86MotionHistoryAllocate (local);
514 /* On ne peut pas calculer l'increment avant, faute d'ecran pour
515 connaitre la taille... */
517 if (priv->acecadInc > 95)
518 priv->acecadInc = 95;
519 if (priv->acecadInc < 1)
521 /* guess the best increment value given video mode */
522 rx=priv->acecadMaxX / screenInfo.screens[0]->width;
523 ry=priv->acecadMaxY / screenInfo.screens[0]->height;
525 priv->acecadInc = rx;
527 priv->acecadInc = ry;
528 if (priv->acecadInc < 1)
532 xf86Msg(X_INFO, "Acecad Tablet Increment: %d\n", priv->acecadInc);
538 ReadInput (LocalDevicePtr local)
542 int is_core_pointer, is_absolute;
543 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
545 /*xf86Msg(X_INFO, "Acecad Tablet Read Input\n");*/
547 is_absolute = (priv->flags & ABSOLUTE_FLAG);
548 is_core_pointer = xf86IsCorePointer(local->dev);
551 * set blocking to -1 on the first call because we know there is data to
552 * read. Xisb automatically clears it after one successful read so that
553 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
554 * read from blocking indefinately.
556 XisbBlockDuration (priv->buffer, -1);
558 while (AceCadGetPacket (priv) == Success)
560 x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
561 y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
563 if (!(priv->flags & ABSOLUTE_FLAG))
565 x = priv->packet[0] & XSIGN_BIT? x:-x;
566 y = priv->packet[0] & YSIGN_BIT? y:-y;
570 y = priv->acecadMaxY - y ;
574 z = ((int)priv->packet[5] << 2) |
575 (((int)priv->packet[6] & 0x01) << 1) |
576 (((int)priv->packet[6] & 0x10) >> 4);
578 buttons = ((int)priv->packet[0] & 0x07) |
579 ((int)priv->packet[6] & 0x02 << 2);
581 prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
585 if (!(priv->acecadOldProximity))
586 if (!is_core_pointer)
588 /*xf86Msg(X_INFO, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
589 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
592 if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
593 || (!is_absolute && (x || y)))
595 if (is_absolute || priv->acecadOldProximity)
597 /*xf86Msg(X_INFO, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
598 xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
602 if (priv->acecadOldButtons != buttons)
606 delta = buttons ^ priv->acecadOldButtons;
612 delta &= ~(1 << (id-1));
614 /*xf86Msg(X_INFO, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
615 xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
619 priv->acecadOldButtons = buttons;
620 priv->acecadOldX = x;
621 priv->acecadOldY = y;
622 priv->acecadOldZ = z;
623 priv->acecadOldProximity = prox;
627 if (!is_core_pointer)
628 if (priv->acecadOldProximity)
630 /*xf86Msg(X_INFO, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
631 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
633 priv->acecadOldProximity = 0;
636 /*xf86Msg(X_INFO, "Acecad Tablet Sortie Read Input\n");*/
640 #define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
642 USBReadInput (LocalDevicePtr local)
645 struct input_event * event;
646 char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
647 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
648 int x = priv->acecadOldX;
649 int y = priv->acecadOldY;
650 int z = priv->acecadOldZ;
651 int prox = priv->acecadOldProximity;
652 int buttons = priv->acecadOldButtons;
655 is_core_pointer = xf86IsCorePointer(local->dev);
657 SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
660 ErrorF("Error reading wacom device : %s\n", strerror(errno));
664 for (event=(struct input_event *)eventbuf;
665 event<(struct input_event *)(eventbuf+len); event++) {
667 switch (event->type) {
668 case EV_SYN: /* 2.6.x */
669 if (event->code != SYN_REPORT)
670 xf86Msg(X_ERROR, "UNKNOWN EV_SYN code %d\n", event->code);
673 switch (event->code) {
693 switch (event->code) {
699 buttons=set_bit(buttons,0,event->value);
703 buttons=set_bit(buttons,1,event->value);
707 buttons=set_bit(buttons,2,event->value);
712 xf86Msg(X_ERROR, "UNKNOWN event type/code %d/%d\n", event->type, event->code);
713 } /* switch event->type */
715 /* Linux Kernel 2.6.x sends EV_SYN/SYN_REPORT as an event terminator,
716 * whereas 2.4.x sends EV_ABS/ABS_MISC. We have to support both.
718 if (!( (event->type == EV_SYN && event->code == SYN_REPORT) ||
719 (event->type == EV_ABS && event->code == ABS_MISC)
726 if (!(priv->acecadOldProximity))
727 if (!is_core_pointer)
729 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
733 xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
735 if (priv->acecadOldButtons != buttons)
739 delta = buttons ^ priv->acecadOldButtons;
745 delta &= ~(1 << (id-1));
747 xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
753 if (!is_core_pointer)
754 if (priv->acecadOldProximity)
756 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
758 priv->acecadOldProximity = 0;
761 priv->acecadOldButtons = buttons;
762 priv->acecadOldX = x;
763 priv->acecadOldY = y;
764 priv->acecadOldZ = z;
765 priv->acecadOldProximity = prox;
767 /*xf86Msg(X_INFO, "Acecad Tablet Sortie Read Input\n");*/
772 CloseProc (LocalDevicePtr local)
777 * The ConvertProc function may need to be tailored for your device.
778 * This function converts the device's valuator outputs to x and y coordinates
779 * to simulate mouse events.
782 ConvertProc (LocalDevicePtr local,
794 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
796 *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
797 *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
803 ReverseConvertProc( LocalDevicePtr local,
808 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
810 valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
811 valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
817 #define WriteString(str)\
818 XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
822 QueryHardware (AceCadPrivatePtr priv)
831 /* Prompt Mode in order to not be disturbed */
832 WriteString(ACECAD_PROMPT_MODE);
835 while(XisbRead(priv->buffer)>=0);
837 /* Ask for Config packet*/
838 WriteString(ACECAD_CONFIG);
840 /* Read the packet */
841 XisbBlockDuration (priv->buffer, 1000000);
844 /*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/
846 if ((AceCadGetPacket (priv) == Success))
848 priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
849 priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
850 priv->acecadMaxZ = 512;
851 xf86Msg(X_INFO, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
856 /*xf86Msg(X_INFO, "Acecad Tablet query hardware fini \n");*/
860 #define BITS_PER_LONG (sizeof(long) * 8)
861 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
862 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
863 #define OFF(x) ((x)%BITS_PER_LONG)
864 #define LONG(x) ((x)/BITS_PER_LONG)
868 USBQueryHardware (LocalDevicePtr local)
870 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
871 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
874 char name[256] = "Unknown";
876 ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
877 xf86Msg(X_PROBED, "Kernel Input device name: \"%s\"\n", name);
879 memset(bit, 0, sizeof(bit));
880 ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
882 for (i = 0; i < EV_MAX; i++)
883 if (test_bit(i, bit[0])) {
884 ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
885 for (j = 0; j < KEY_MAX; j++)
886 if (test_bit(j, bit[i])) {
888 ioctl(local->fd, EVIOCGABS(j), abs);
891 priv->acecadMaxX = abs[2];
895 priv->acecadMaxY = abs[2];
899 priv->acecadMaxZ = abs[2];
906 xf86Msg(X_PROBED, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n", priv->acecadMaxX, priv->acecadMaxY,priv->acecadMaxZ);
912 NewPacket (AceCadPrivatePtr priv)
918 AceCadGetPacket (AceCadPrivatePtr priv)
923 while((c = XisbRead(priv->buffer))>=0 )
927 * fail after 500 bytes so the server doesn't hang forever if a
928 * device sends bad data.
940 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
941 XisbBlockDuration (priv->buffer, 10000);
942 priv->packet[priv->packeti++] = c;
943 count=ACECAD_PACKET_SIZE-1;
944 while(count-- && (c = XisbRead(priv->buffer))>=0)
946 /*xf86Msg(X_INFO, "Push %2.2x\n",(char) c);*/
947 priv->packet[priv->packeti++] = c;
949 XisbBlockDuration (priv->buffer, 0);