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