xisb.c revision 05b261ec
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/***************************************************************************** 35 * Standard Headers 36 ****************************************************************************/ 37 38#ifdef HAVE_XORG_CONFIG_H 39#include <xorg-config.h> 40#endif 41 42#include <misc.h> 43#include <xf86.h> 44#include <xf86Version.h> 45#include <xf86_OSproc.h> 46#include <xf86_OSlib.h> 47#include <xf86Xinput.h> 48#include "xisb.h" 49 50/***************************************************************************** 51 * Local Headers 52 ****************************************************************************/ 53 54/***************************************************************************** 55 * Variables without includable headers 56 ****************************************************************************/ 57 58/***************************************************************************** 59 * Local Variables 60 ****************************************************************************/ 61 62/***************************************************************************** 63 * Function Definitions 64 ****************************************************************************/ 65 66_X_EXPORT XISBuffer * 67XisbNew (int fd, ssize_t size) 68{ 69 XISBuffer *b; 70 71 b = xalloc (sizeof (XISBuffer)); 72 if (!b) 73 return (NULL); 74 b->buf = xalloc ((sizeof (unsigned char) * size)); 75 if (!b->buf) 76 { 77 xfree (b); 78 return (NULL); 79 } 80 81 b->fd = fd; 82 b->trace = 0; 83 b->block_duration = 0; 84 b->current = 1; /* force it to be past the end to trigger initial read */ 85 b->end = 0; 86 b->buffer_size = size; 87 return (b); 88} 89 90_X_EXPORT void 91XisbFree (XISBuffer *b) 92{ 93 xfree (b->buf); 94 xfree (b); 95} 96 97_X_EXPORT int 98XisbRead (XISBuffer *b) 99{ 100 int ret; 101 102 if (b->current >= b->end) 103 { 104 if (b->block_duration >= 0) 105 { 106 if (xf86WaitForInput (b->fd, b->block_duration) < 1) 107 return (-1); 108 } 109 else 110 { 111 /* 112 * automatically clear it so if XisbRead is called in a loop 113 * the next call will make sure there is data with select and 114 * thus prevent a blocking read 115 */ 116 b->block_duration = 0; 117 } 118 119 ret = xf86ReadSerial (b->fd, b->buf, b->buffer_size); 120 switch (ret) 121 { 122 case 0: 123 return (-1); /* timeout */ 124 case -1: 125 return (-2); /* error */ 126 default: 127 b->end = ret; 128 b->current = 0; 129 break; 130 } 131 } 132 if (b->trace) 133 ErrorF ("read 0x%02x (%c)\n", b->buf[b->current], 134 isprint(b->buf[b->current])?b->buf[b->current]:'.'); 135 136 return (b->buf[b->current++]); 137} 138 139/* the only purpose of this function is to provide output tracing */ 140_X_EXPORT ssize_t 141XisbWrite (XISBuffer *b, unsigned char *msg, ssize_t len) 142{ 143 if (b->trace) 144 { 145 int i = 0; 146 for (i = 0; i < len; i++) 147 ErrorF ("\t\twrote 0x%02x (%c)\n", msg[i], msg[i]); 148 } 149 return (xf86WriteSerial (b->fd, msg, len)); 150} 151 152/* turn tracing of this buffer on (1) or off (0) */ 153_X_EXPORT void 154XisbTrace (XISBuffer *b, int trace) 155{ 156 b->trace = trace; 157} 158 159/* 160 * specify a block_duration of -1 when you know the buffer's fd is ready to 161 * read. After a read, it is automatically set to 0 so that the next read 162 * will use check to select for data and prevent a block. 163 * It is the caller's responsibility to set the block_duration to -1 if it 164 * knows that there is data to read (because the main select loop triggered 165 * the read) and want's to avoid the unnecessary overhead of the select call 166 * 167 * a zero or positive block duration will cause the select to block for the 168 * give duration in usecs. 169 */ 170 171_X_EXPORT void 172XisbBlockDuration (XISBuffer *b, int block_duration) 173{ 174 b->block_duration = block_duration; 175} 176