summaryrefslogtreecommitdiff
path: root/driver/xf86-input-mouse/src/bsd_mouse.c
blob: fbe954db6386e2695c7610594412c26c7bedb9ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819

/*
 * Copyright (c) 1999-2003 by The XFree86 Project, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the copyright holder(s)
 * and author(s) shall not be used in advertising or otherwise to promote
 * the sale, use or other dealings in this Software without prior written
 * authorization from the copyright holder(s) and author(s).
 */

#include <xorg-server.h>

#include <X11/X.h>
#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"
#include "xf86Xinput.h"
#include "mouse.h"
#include "xisb.h"
#include "mipointer.h"
#ifdef WSCONS_SUPPORT
#include <dev/wscons/wsconsio.h>
#endif
#ifdef USBMOUSE_SUPPORT
#ifdef HAS_LIB_USB_HID
#include <usbhid.h>
#else
#include "usb.h"
#endif

#include <dev/usb/usb.h>
#ifdef USB_GET_REPORT_ID
#define USB_NEW_HID
#endif

#define HUP_GENERIC_DESKTOP     0x0001
#define HUP_BUTTON              0x0009

#define HUG_X                   0x0030
#define HUG_Y                   0x0031
#define HUG_Z                   0x0032
#define HUG_WHEEL               0x0038

#define HID_USAGE2(p,u) (((p) << 16) | u)

/* The UMS mice have middle button as number 3 */
#define UMS_BUT(i) ((i) == 0 ? 2 : (i) == 1 ? 0 : (i) == 2 ? 1 : (i))
#endif /* USBMOUSE_SUPPORT */

#ifdef USBMOUSE_SUPPORT
static void usbSigioReadInput (int fd, void *closure);
#endif
static const char *FindDevice(InputInfoPtr, const char *, int);

#ifdef X_PRIVSEP
extern int priv_open_device(const char *);
#endif

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
/* These are for FreeBSD and DragonFly */
#define DEFAULT_MOUSE_DEV               "/dev/mouse"
#define DEFAULT_SYSMOUSE_DEV            "/dev/sysmouse"
#define DEFAULT_PS2_DEV                 "/dev/psm0"

static const char *mouseDevs[] = {
        DEFAULT_MOUSE_DEV,
        DEFAULT_SYSMOUSE_DEV,
        DEFAULT_PS2_DEV,
        NULL
};
#elif (defined(__OpenBSD__) || defined(__NetBSD__)) && defined(WSCONS_SUPPORT)
/* Only wsmouse mice are autoconfigured for now on OpenBSD */
#define DEFAULT_WSMOUSE_DEV             "/dev/wsmouse"
#define DEFAULT_WSMOUSE0_DEV            "/dev/wsmouse0"

static const char *mouseDevs[] = {
        DEFAULT_WSMOUSE_DEV,
        DEFAULT_WSMOUSE0_DEV,
        NULL
};
#endif

static int
SupportedInterfaces(void)
{
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__NetBSD__)
    return MSE_SERIAL | MSE_BUS | MSE_PS2 | MSE_AUTO | MSE_MISC;
#else
    return MSE_SERIAL | MSE_BUS | MSE_PS2 | MSE_XPS2 | MSE_AUTO | MSE_MISC;
#endif
}

/* Names of protocols that are handled internally here. */
static const char *internalNames[] = {
#if defined(WSCONS_SUPPORT)
        "WSMouse",
#endif
#if defined(USBMOUSE_SUPPORT)
        "usb",
#endif
        NULL
};

/*
 * Names of MSC_MISC protocols that the OS supports.  These are decoded by
 * main "mouse" driver.
 */
static const char *miscNames[] = {
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
        "SysMouse",
#endif
        NULL
};

static const char **
BuiltinNames(void)
{
    return internalNames;
}

static Bool
CheckProtocol(const char *protocol)
{
    int i;

    for (i = 0; internalNames[i]; i++)
        if (xf86NameCmp(protocol, internalNames[i]) == 0)
            return TRUE;
    for (i = 0; miscNames[i]; i++)
        if (xf86NameCmp(protocol, miscNames[i]) == 0)
            return TRUE;
    return FALSE;
}

static const char *
DefaultProtocol(void)
{
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
    return "Auto";
#elif (defined(__OpenBSD__) || defined(__NetBSD__)) && defined(WSCONS_SUPPORT)
    return "WSMouse";
#else
    return NULL;
#endif
}

#if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)) && defined(MOUSE_PROTO_SYSMOUSE)
static struct {
        int dproto;
        const char *name;
} devproto[] = {
        { MOUSE_PROTO_MS,               "Microsoft" },
        { MOUSE_PROTO_MSC,              "MouseSystems" },
        { MOUSE_PROTO_LOGI,             "Logitech" },
        { MOUSE_PROTO_MM,               "MMSeries" },
        { MOUSE_PROTO_LOGIMOUSEMAN,     "MouseMan" },
        { MOUSE_PROTO_BUS,              "BusMouse" },
        { MOUSE_PROTO_INPORT,           "BusMouse" },
        { MOUSE_PROTO_PS2,              "PS/2" },
        { MOUSE_PROTO_HITTAB,           "MMHitTab" },
        { MOUSE_PROTO_GLIDEPOINT,       "GlidePoint" },
        { MOUSE_PROTO_INTELLI,          "Intellimouse" },
        { MOUSE_PROTO_THINK,            "ThinkingMouse" },
        { MOUSE_PROTO_SYSMOUSE,         "SysMouse" }
};

static const char *
SetupAuto(InputInfoPtr pInfo, int *protoPara)
{
    int i;
    mousehw_t hw;
    mousemode_t mode;

    if (pInfo->fd == -1)
        return NULL;

    /* set the driver operation level, if applicable */
    i = 1;
    ioctl(pInfo->fd, MOUSE_SETLEVEL, &i);

    /* interrogate the driver and get some intelligence on the device. */
    hw.iftype = MOUSE_IF_UNKNOWN;
    hw.model = MOUSE_MODEL_GENERIC;
    ioctl(pInfo->fd, MOUSE_GETHWINFO, &hw);
    xf86MsgVerb(X_INFO, 3, "%s: SetupAuto: hw.iftype is %d, hw.model is %d\n",
                pInfo->name, hw.iftype, hw.model);
    if (ioctl(pInfo->fd, MOUSE_GETMODE, &mode) == 0) {
        for (i = 0; i < sizeof(devproto)/sizeof(devproto[0]); ++i) {
            if (mode.protocol == devproto[i].dproto) {
                /* override some parameters */
                if (protoPara) {
                    protoPara[4] = mode.packetsize;
                    protoPara[0] = mode.syncmask[0];
                    protoPara[1] = mode.syncmask[1];
                }
                xf86MsgVerb(X_INFO, 3, "%s: SetupAuto: protocol is %s\n",
                            pInfo->name, devproto[i].name);
                return devproto[i].name;
            }
        }
    }
    return NULL;
}

static void
SetSysMouseRes(InputInfoPtr pInfo, const char *protocol, int rate, int res)
{
    mousemode_t mode;
    MouseDevPtr pMse;

    pMse = pInfo->private;

    mode.rate = rate > 0 ? rate : -1;
    mode.resolution = res > 0 ? res : -1;
    mode.accelfactor = -1;
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
    if (pMse->autoProbe ||
        (protocol && xf86NameCmp(protocol, "SysMouse") == 0)) {
        /*
         * As the FreeBSD sysmouse driver defaults to protocol level 0
         * every time it is opened we enforce protocol level 1 again at
         * this point.
         */
        mode.level = 1;
    } else
        mode.level = -1;
#else
    mode.level = -1;
#endif
    ioctl(pInfo->fd, MOUSE_SETMODE, &mode);
}
#endif

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)

#define MOUSED_PID_FILE "/var/run/moused.pid"

/*
 * Try to check if moused is running.  DEFAULT_SYSMOUSE_DEV is useless without
 * it.  There doesn't seem to be a better way of checking.
 */
static Bool
MousedRunning(void)
{
    FILE *f = NULL;
    unsigned int pid;

    if ((f = fopen(MOUSED_PID_FILE, "r")) != NULL) {
        if (fscanf(f, "%u", &pid) == 1 && pid > 0) {
            if (kill(pid, 0) == 0) {
                fclose(f);
                return TRUE;
            }
        }
        fclose(f);
    }
    return FALSE;
}

static const char *
FindDevice(InputInfoPtr pInfo, const char *protocol, int flags)
{
    int fd = -1;
    const char **pdev, *dev = NULL;
    Bool devMouse = FALSE;
    struct stat devMouseStat;
    struct stat sb;

    for (pdev = mouseDevs; *pdev; pdev++) {
#ifndef X_PRIVSEP
        SYSCALL (fd = open(*pdev, O_RDWR | O_NONBLOCK));
#else
        fd = priv_open_device(*pdev);
#endif
        if (fd == -1) {
#ifdef DEBUG
            ErrorF("Cannot open %s (%s)\n", *pdev, strerror(errno));
#endif
        } else {
            /*
             * /dev/mouse is held until checks for matches with other devices
             * are done.  This is so that when it points to /dev/sysmouse,
             * the test for whether /dev/sysmouse is usable can be made.
             */
            if (!strcmp(*pdev, DEFAULT_MOUSE_DEV)) {
                if (fstat(fd, &devMouseStat) == 0)
                    devMouse = TRUE;
                close(fd);
                continue;
            } else if (!strcmp(*pdev, DEFAULT_SYSMOUSE_DEV)) {
                /* Check if /dev/mouse is the same as /dev/sysmouse. */
                if (devMouse && fstat(fd, &sb) == 0 &&
                    devMouseStat.st_dev == sb.st_dev &&
                    devMouseStat.st_ino == sb.st_ino) {
                    /* If the same, use /dev/sysmouse. */
                    devMouse = FALSE;
                }
                close(fd);
                if (MousedRunning())
                    break;
                else {
#ifdef DEBUG
                    ErrorF("moused isn't running\n");
#endif
                }
            } else {
                close(fd);
                break;
            }
        }
    }

    if (*pdev)
        dev = *pdev;
    else if (devMouse)
        dev = DEFAULT_MOUSE_DEV;

    if (dev) {
        /* Set the Device option. */
        pInfo->options =
            xf86AddNewOption(pInfo->options, "Device", dev);
        xf86Msg(X_INFO, "%s: Setting Device option to \"%s\"\n",
                pInfo->name, dev);
    }

    return *pdev;
}
#endif

#if (defined(__OpenBSD__) || defined(__NetBSD__)) && defined(WSCONS_SUPPORT)

/* Only support wsmouse configuration for now */
static const char *
SetupAuto(InputInfoPtr pInfo, int *protoPara)
{

    xf86MsgVerb(X_INFO, 3, "%s: SetupAuto: protocol is %s\n",
                pInfo->name, "wsmouse");
    return "wsmouse";
}

static void
SetMouseRes(InputInfoPtr pInfo, const char *protocol, int rate, int res)
{

    xf86MsgVerb(X_INFO, 3, "%s: SetMouseRes: protocol %s rate %d res %d\n",
            pInfo->name, protocol, rate, res);
}

static const char *
FindDevice(InputInfoPtr pInfo, const char *protocol, int flags)
{
    int fd = -1;
    const char **pdev;

    for (pdev = mouseDevs; *pdev; pdev++) {
#ifndef X_PRIVSEP
        SYSCALL(fd = open(*pdev, O_RDWR | O_NONBLOCK));
#else
        fd = priv_open_device(*pdev);
#endif
        if (fd != -1) {
            /* Set the Device option. */
            pInfo->options =
                xf86AddNewOption(pInfo->options,
                                 "Device", *pdev);
            xf86Msg(X_INFO, "%s: found Device \"%s\"\n",
                    pInfo->name, *pdev);
            close(fd);
            break;
        }
    }
    return *pdev;
}
#endif /* __OpenBSD__ || __NetBSD__ && WSCONS_SUPPORT */

#ifdef WSCONS_SUPPORT
#define NUMEVENTS 64

static void
wsconsReadInput(InputInfoPtr pInfo)
{
    MouseDevPtr pMse;
    static struct wscons_event eventList[NUMEVENTS];
    int n, c;
    struct wscons_event *event = eventList;
    unsigned char *pBuf;

    pMse = pInfo->private;

    XisbBlockDuration(pMse->buffer, -1);
    pBuf = (unsigned char *)eventList;
    n = 0;
    while (n < sizeof(eventList) && (c = XisbRead(pMse->buffer)) >= 0) {
        pBuf[n++] = (unsigned char)c;
    }

    if (n == 0)
        return;

    n /= sizeof(struct wscons_event);
    while( n-- ) {
        int buttons = pMse->lastButtons;
        int dx = 0, dy = 0, dz = 0, dw = 0, x, y;
        switch (event->type) {
        case WSCONS_EVENT_MOUSE_UP:
#define BUTBIT (1 << (event->value <= 2 ? 2 - event->value : event->value))
            buttons &= ~BUTBIT;
            break;
        case WSCONS_EVENT_MOUSE_DOWN:
            buttons |= BUTBIT;
            break;
        case WSCONS_EVENT_MOUSE_DELTA_X:
            dx = event->value;
            break;
        case WSCONS_EVENT_MOUSE_DELTA_Y:
            dy = -event->value;
            break;
#ifdef WSCONS_EVENT_MOUSE_DELTA_Z
        case WSCONS_EVENT_MOUSE_DELTA_Z:
            dz = event->value;
            break;
#endif
#ifdef WSCONS_EVENT_MOUSE_DELTA_W
        case WSCONS_EVENT_MOUSE_DELTA_W:
            dw = event->value;
            break;
#endif
	case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
	    x = event->value;
	    xf86PostMotionEvent(pInfo->dev, TRUE, 0, 1, x);
	    ++event;
	    continue;
	case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
	    y = event->value;
	    xf86PostMotionEvent(pInfo->dev, TRUE, 1, 1, y);
	    ++event;
	    continue;
#ifdef WSCONS_EVENT_MOUSE_ABSOLUTE_Z
	case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
	    ++event;
	    continue;
#endif
#ifdef WSCONS_EVENT_MOUSE_ABSOLUTE_W
	case WSCONS_EVENT_MOUSE_ABSOLUTE_W:
	    ++event;
	    continue;
#endif
#ifdef WSCONS_EVENT_SYNC
	case WSCONS_EVENT_SYNC:
	    ++event;
	    continue;
#endif
        default:
            LogMessageVerbSigSafe(X_WARNING, -1,
                                  "%s: bad wsmouse event type=%d\n", pInfo->name,
                                  event->type);
            ++event;
            continue;
        }

        pMse->PostEvent(pInfo, buttons, dx, dy, dz, dw);
        ++event;
    }
    return;
}


/* This function is called when the protocol is "wsmouse". */
static Bool
wsconsPreInit(InputInfoPtr pInfo, const char *protocol, int flags)
{
    MouseDevPtr pMse = pInfo->private;

    /* Setup the local input proc. */
    pInfo->read_input = wsconsReadInput;
    pMse->xisbscale = sizeof(struct wscons_event);

#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
    pInfo->flags |= XI86_CONFIGURED;
#endif
    return TRUE;
}
#endif

#if defined(USBMOUSE_SUPPORT)

typedef struct _UsbMseRec {
    int packetSize;
    int iid;
    hid_item_t loc_x;           /* x locator item */
    hid_item_t loc_y;           /* y locator item */
    hid_item_t loc_z;           /* z (wheel) locator item */
    hid_item_t loc_w;           /* z (wheel) locator item */
    hid_item_t loc_btn[MSE_MAXBUTTONS]; /* buttons locator items */
   unsigned char *buffer;
} UsbMseRec, *UsbMsePtr;

static int
usbMouseProc(DeviceIntPtr pPointer, int what)
{
    InputInfoPtr pInfo;
    MouseDevPtr pMse;
    UsbMsePtr pUsbMse;
    unsigned char map[MSE_MAXBUTTONS + 1];
    int nbuttons;

    pInfo = pPointer->public.devicePrivate;
    pMse = pInfo->private;
    pMse->device = pPointer;
    pUsbMse = pMse->mousePriv;

    switch (what) {
    case DEVICE_INIT:
        pPointer->public.on = FALSE;

        for (nbuttons = 0; nbuttons < MSE_MAXBUTTONS; ++nbuttons)
            map[nbuttons + 1] = nbuttons + 1;

        InitPointerDeviceStruct((DevicePtr)pPointer,
                                map,
                                min(pMse->buttons, MSE_MAXBUTTONS),
                                miPointerGetMotionEvents,
                                pMse->Ctrl,
                                miPointerGetMotionBufferSize());

        /* X valuator */
        xf86InitValuatorAxisStruct(pPointer, 0, 0, -1, 1, 0, 1);
        xf86InitValuatorDefaults(pPointer, 0);
        /* Y valuator */
        xf86InitValuatorAxisStruct(pPointer, 1, 0, -1, 1, 0, 1);
        xf86InitValuatorDefaults(pPointer, 1);
        xf86MotionHistoryAllocate(pInfo);
        break;

    case DEVICE_ON:
        pInfo->fd = xf86OpenSerial(pInfo->options);
        if (pInfo->fd == -1)
            xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
        else {
            pMse->buffer = XisbNew(pInfo->fd, pUsbMse->packetSize);
            if (!pMse->buffer) {
                free(pMse);
                xf86CloseSerial(pInfo->fd);
                pInfo->fd = -1;
            } else {
                xf86FlushInput(pInfo->fd);
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 23
                if (!xf86InstallSIGIOHandler (pInfo->fd, usbSigioReadInput,
                                              pInfo))
#endif
                    AddEnabledDevice(pInfo->fd);
            }
        }
        pMse->lastButtons = 0;
        pMse->lastMappedButtons = 0;
        pMse->emulateState = 0;
        pPointer->public.on = TRUE;
        break;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        if (pInfo->fd != -1) {
            RemoveEnabledDevice(pInfo->fd);
            if (pUsbMse->packetSize > 8 && pUsbMse->buffer) {
                free(pUsbMse->buffer);
            }
            if (pMse->buffer) {
                XisbFree(pMse->buffer);
                pMse->buffer = NULL;
            }
            xf86CloseSerial(pInfo->fd);
            pInfo->fd = -1;
        }
        pPointer->public.on = FALSE;
        usleep(300000);
        break;

    default:
        return BadValue;
    }
    return Success;
}

static void
usbReadInput(InputInfoPtr pInfo)
{
    MouseDevPtr pMse;
    UsbMsePtr pUsbMse;
    int buttons = pMse->lastButtons;
    int dx = 0, dy = 0, dz = 0, dw = 0;
    int n, c;
    unsigned char *pBuf;

    pMse = pInfo->private;
    pUsbMse = pMse->mousePriv;

    XisbBlockDuration(pMse->buffer, -1);
    pBuf = pUsbMse->buffer;
    n = 0;
    while ((c = XisbRead(pMse->buffer)) >= 0 && n < pUsbMse->packetSize) {
        pBuf[n++] = (unsigned char)c;
    }
    if (n == 0)
        return;
    if (n != pUsbMse->packetSize) {
        LogMessageVerbSigSafe(X_WARNING, -1,
                              "%s: incomplete packet, size %d\n",
                              pInfo->name, n);
    }
    /* discard packets with an id that don't match the mouse */
    /* XXX this is probably not the right thing */
    if (pUsbMse->iid != 0) {
        if (*pBuf++ != pUsbMse->iid)
            return;
    }
    dx = hid_get_data(pBuf, &pUsbMse->loc_x);
    dy = hid_get_data(pBuf, &pUsbMse->loc_y);
    dz = hid_get_data(pBuf, &pUsbMse->loc_z);
    dw = hid_get_data(pBuf, &pUsbMse->loc_w);

    buttons = 0;
    for (n = 0; n < pMse->buttons; n++) {
        if (hid_get_data(pBuf, &pUsbMse->loc_btn[n]))
            buttons |= (1 << UMS_BUT(n));
    }
    pMse->PostEvent(pInfo, buttons, dx, dy, dz, dw);
    return;
}

static void
usbSigioReadInput (int fd, void *closure)
{
    usbReadInput ((InputInfoPtr) closure);
}

/* This function is called when the protocol is "usb". */
static Bool
usbPreInit(InputInfoPtr pInfo, const char *protocol, int flags)
{
    MouseDevPtr pMse = pInfo->private;
    UsbMsePtr pUsbMse;
    report_desc_t reportDesc;
    int i;

    pUsbMse = malloc(sizeof(UsbMseRec));
    if (pUsbMse == NULL) {
        xf86Msg(X_ERROR, "%s: cannot allocate UsbMouseRec\n", pInfo->name);
        free(pMse);
        return FALSE;
    }

    pMse->protocol = protocol;
    xf86Msg(X_CONFIG, "%s: Protocol: %s\n", pInfo->name, protocol);

    /* Collect the options, and process the common options. */
    COLLECT_INPUT_OPTIONS(pInfo, NULL);
    xf86ProcessCommonOptions(pInfo, pInfo->options);

    /* Check if the device can be opened. */
    pInfo->fd = xf86OpenSerial(pInfo->options);
    if (pInfo->fd == -1) {
        if (xf86GetAllowMouseOpenFail())
            xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
        else {
            xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
            free(pUsbMse);
            free(pMse);
            return FALSE;
        }
    }
    /* Get USB information */
    reportDesc = hid_get_report_desc(pInfo->fd);
    /* Get packet size & iid */
#ifdef USB_NEW_HID
    if (ioctl(pInfo->fd, USB_GET_REPORT_ID, &pUsbMse->iid) == -1) {
            xf86Msg(X_ERROR, "Error ioctl USB_GET_REPORT_ID on %s : %s\n",
                    pInfo->name, strerror(errno));
            return FALSE;
    }
    pUsbMse->packetSize = hid_report_size(reportDesc, hid_input,
                                              pUsbMse->iid);
#else
    pUsbMse->packetSize = hid_report_size(reportDesc, hid_input,
                                              &pUsbMse->iid);
#endif
    /* Allocate buffer */
    if (pUsbMse->packetSize <= 8) {
        pUsbMse->buffer = pMse->protoBuf;
    } else {
        pUsbMse->buffer = malloc(pUsbMse->packetSize);
    }
    if (pUsbMse->buffer == NULL) {
        xf86Msg(X_ERROR, "%s: cannot allocate buffer\n", pInfo->name);
        free(pUsbMse);
        free(pMse);
        xf86CloseSerial(pInfo->fd);
        return FALSE;
    }
#ifdef USB_NEW_HID
    if (hid_locate(reportDesc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
                   hid_input, &pUsbMse->loc_x, pUsbMse->iid) < 0) {
        xf86Msg(X_WARNING, "%s: no x locator\n", pInfo->name);
    }
    if (hid_locate(reportDesc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
                   hid_input, &pUsbMse->loc_y, pUsbMse->iid) < 0) {
        xf86Msg(X_WARNING, "%s: no y locator\n", pInfo->name);
    }
    if (hid_locate(reportDesc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
                   hid_input, &pUsbMse->loc_z, pUsbMse->iid) < 0) {
    }
#else
    if (hid_locate(reportDesc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
                   hid_input, &pUsbMse->loc_x) < 0) {
        xf86Msg(X_WARNING, "%s: no x locator\n", pInfo->name);
    }
    if (hid_locate(reportDesc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
                   hid_input, &pUsbMse->loc_y) < 0) {
        xf86Msg(X_WARNING, "%s: no y locator\n", pInfo->name);
    }
    if (hid_locate(reportDesc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
                   hid_input, &pUsbMse->loc_z) < 0) {
    }
#endif
    /* Probe for number of buttons */
    for (i = 1; i <= MSE_MAXBUTTONS; i++) {
        if (!hid_locate(reportDesc, HID_USAGE2(HUP_BUTTON, i),
                        hid_input, &pUsbMse->loc_btn[i-1]
#ifdef USB_NEW_HID
                        , pUsbMse->iid
#endif
                        ))
            break;
    }
    pMse->buttons = i-1;

    xf86CloseSerial(pInfo->fd);
    pInfo->fd = -1;

    /* Private structure */
    pMse->mousePriv = pUsbMse;

    /* Process common mouse options (like Emulate3Buttons, etc). */
    pMse->CommonOptions(pInfo);

    /* Setup the local procs. */
    pInfo->device_control = usbMouseProc;
    pInfo->read_input = usbReadInput;

#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
    pInfo->flags |= XI86_CONFIGURED;
#endif
    return TRUE;
}
#endif /* USBMOUSE */

static Bool
bsdMousePreInit(InputInfoPtr pInfo, const char *protocol, int flags)
{
    /* The protocol is guaranteed to be one of the internalNames[] */
#ifdef WSCONS_SUPPORT
    if (xf86NameCmp(protocol, "WSMouse") == 0) {
        return wsconsPreInit(pInfo, protocol, flags);
    }
#endif
#ifdef USBMOUSE_SUPPORT
    if (xf86NameCmp(protocol, "usb") == 0) {
        return usbPreInit(pInfo, protocol, flags);
    }
#endif
    return TRUE;
}

OSMouseInfoPtr
OSMouseInit(int flags)
{
    OSMouseInfoPtr p;

    p = calloc(sizeof(OSMouseInfoRec), 1);
    if (!p)
        return NULL;
    p->SupportedInterfaces = SupportedInterfaces;
    p->BuiltinNames = BuiltinNames;
    p->DefaultProtocol = DefaultProtocol;
    p->CheckProtocol = CheckProtocol;
#if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)) && defined(MOUSE_PROTO_SYSMOUSE)
    p->SetupAuto = SetupAuto;
    p->SetPS2Res = SetSysMouseRes;
    p->SetBMRes = SetSysMouseRes;
    p->SetMiscRes = SetSysMouseRes;
#endif
#if (defined(__OpenBSD__) || defined(__NetBSD__)) && defined(WSCONS_SUPPORT)
    p->SetupAuto = SetupAuto;
    p->SetMiscRes = SetMouseRes;
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
    p->FindDevice = FindDevice;
#endif
    p->PreInit = bsdMousePreInit;
    return p;
}