xref: /OK3568_Linux_fs/external/xserver/hw/xfree86/common/xisb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 1997  Metro Link Incorporated
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun  * all copies or substantial portions of the Software.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*4882a593Smuzhiyun  * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18*4882a593Smuzhiyun  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19*4882a593Smuzhiyun  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20*4882a593Smuzhiyun  * SOFTWARE.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * Except as contained in this notice, the name of the Metro Link shall not be
23*4882a593Smuzhiyun  * used in advertising or otherwise to promote the sale, use or other dealings
24*4882a593Smuzhiyun  * in this Software without prior written authorization from Metro Link.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun 	X Input Serial Buffer routines for use in any XInput driver that accesses
30*4882a593Smuzhiyun 	a serial device.
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /*****************************************************************************
34*4882a593Smuzhiyun  *	Standard Headers
35*4882a593Smuzhiyun  ****************************************************************************/
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #ifdef HAVE_XORG_CONFIG_H
38*4882a593Smuzhiyun #include <xorg-config.h>
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #include <misc.h>
42*4882a593Smuzhiyun #include <xf86.h>
43*4882a593Smuzhiyun #include <xf86_OSproc.h>
44*4882a593Smuzhiyun #include <xf86_OSlib.h>
45*4882a593Smuzhiyun #include <xf86Xinput.h>
46*4882a593Smuzhiyun #include "xisb.h"
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*****************************************************************************
49*4882a593Smuzhiyun  *	Local Headers
50*4882a593Smuzhiyun  ****************************************************************************/
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /*****************************************************************************
53*4882a593Smuzhiyun  *	Variables without includable headers
54*4882a593Smuzhiyun  ****************************************************************************/
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /*****************************************************************************
57*4882a593Smuzhiyun  *	Local Variables
58*4882a593Smuzhiyun  ****************************************************************************/
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /*****************************************************************************
61*4882a593Smuzhiyun  *	Function Definitions
62*4882a593Smuzhiyun  ****************************************************************************/
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun XISBuffer *
XisbNew(int fd,ssize_t size)65*4882a593Smuzhiyun XisbNew(int fd, ssize_t size)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun     XISBuffer *b;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun     b = malloc(sizeof(XISBuffer));
70*4882a593Smuzhiyun     if (!b)
71*4882a593Smuzhiyun         return NULL;
72*4882a593Smuzhiyun     b->buf = malloc((sizeof(unsigned char) * size));
73*4882a593Smuzhiyun     if (!b->buf) {
74*4882a593Smuzhiyun         free(b);
75*4882a593Smuzhiyun         return NULL;
76*4882a593Smuzhiyun     }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun     b->fd = fd;
79*4882a593Smuzhiyun     b->trace = 0;
80*4882a593Smuzhiyun     b->block_duration = 0;
81*4882a593Smuzhiyun     b->current = 1;             /* force it to be past the end to trigger initial read */
82*4882a593Smuzhiyun     b->end = 0;
83*4882a593Smuzhiyun     b->buffer_size = size;
84*4882a593Smuzhiyun     return b;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun void
XisbFree(XISBuffer * b)88*4882a593Smuzhiyun XisbFree(XISBuffer * b)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun     free(b->buf);
91*4882a593Smuzhiyun     free(b);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun int
XisbRead(XISBuffer * b)95*4882a593Smuzhiyun XisbRead(XISBuffer * b)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun     int ret;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun     if (b->current >= b->end) {
100*4882a593Smuzhiyun         if (b->block_duration >= 0) {
101*4882a593Smuzhiyun             if (xf86WaitForInput(b->fd, b->block_duration) < 1)
102*4882a593Smuzhiyun                 return -1;
103*4882a593Smuzhiyun         }
104*4882a593Smuzhiyun         else {
105*4882a593Smuzhiyun             /*
106*4882a593Smuzhiyun              * automatically clear it so if XisbRead is called in a loop
107*4882a593Smuzhiyun              * the next call will make sure there is data with select and
108*4882a593Smuzhiyun              * thus prevent a blocking read
109*4882a593Smuzhiyun              */
110*4882a593Smuzhiyun             b->block_duration = 0;
111*4882a593Smuzhiyun         }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun         ret = xf86ReadSerial(b->fd, b->buf, b->buffer_size);
114*4882a593Smuzhiyun         switch (ret) {
115*4882a593Smuzhiyun         case 0:
116*4882a593Smuzhiyun             return -1;          /* timeout */
117*4882a593Smuzhiyun         case -1:
118*4882a593Smuzhiyun             return -2;          /* error */
119*4882a593Smuzhiyun         default:
120*4882a593Smuzhiyun             b->end = ret;
121*4882a593Smuzhiyun             b->current = 0;
122*4882a593Smuzhiyun             break;
123*4882a593Smuzhiyun         }
124*4882a593Smuzhiyun     }
125*4882a593Smuzhiyun     if (b->trace)
126*4882a593Smuzhiyun         ErrorF("read 0x%02x (%c)\n", b->buf[b->current],
127*4882a593Smuzhiyun                isprint(b->buf[b->current]) ? b->buf[b->current] : '.');
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun     return b->buf[b->current++];
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /* the only purpose of this function is to provide output tracing */
133*4882a593Smuzhiyun ssize_t
XisbWrite(XISBuffer * b,unsigned char * msg,ssize_t len)134*4882a593Smuzhiyun XisbWrite(XISBuffer * b, unsigned char *msg, ssize_t len)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun     if (b->trace) {
137*4882a593Smuzhiyun         int i = 0;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun         for (i = 0; i < len; i++)
140*4882a593Smuzhiyun             ErrorF("\t\twrote 0x%02x (%c)\n", msg[i], msg[i]);
141*4882a593Smuzhiyun     }
142*4882a593Smuzhiyun     return (xf86WriteSerial(b->fd, msg, len));
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /* turn tracing of this buffer on (1) or off (0) */
146*4882a593Smuzhiyun void
XisbTrace(XISBuffer * b,int trace)147*4882a593Smuzhiyun XisbTrace(XISBuffer * b, int trace)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun     b->trace = trace;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun  * specify a block_duration of -1 when you know the buffer's fd is ready to
154*4882a593Smuzhiyun  * read. After a read, it is automatically set to 0 so that the next read
155*4882a593Smuzhiyun  * will use check to select for data and prevent a block.
156*4882a593Smuzhiyun  * It is the caller's responsibility to set the block_duration to -1 if it
157*4882a593Smuzhiyun  * knows that there is data to read (because the main select loop triggered
158*4882a593Smuzhiyun  * the read) and want's to avoid the unnecessary overhead of the select call
159*4882a593Smuzhiyun  *
160*4882a593Smuzhiyun  * a zero or positive block duration will cause the select to block for the
161*4882a593Smuzhiyun  * give duration in usecs.
162*4882a593Smuzhiyun  */
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun void
XisbBlockDuration(XISBuffer * b,int block_duration)165*4882a593Smuzhiyun XisbBlockDuration(XISBuffer * b, int block_duration)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun     b->block_duration = block_duration;
168*4882a593Smuzhiyun }
169