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