File: utils.c

package info (click to toggle)
tpconfig 3.1.3-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 564 kB
  • ctags: 185
  • sloc: ansic: 1,720; sh: 455; makefile: 82
file content (224 lines) | stat: -rw-r--r-- 6,651 bytes parent folder | download | duplicates (5)
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
/***********************************************************************
* File name:  util.c                                                   *
* Written by: Bruce Kall                                               *
* Created: 2/2000 (portions extracted from tpconfig.c)                 *
* Language: C                                                          *
* Version: 1.0                                                         *
* Purpose:  Utility functions for manipulating touchpads.              *
*                                                                      *
***********************************************************************/

/* Copyright (C) 1997  C. Scott Ananian <cananian@alumni.princeton.edu>
 * Copyright (c) 1998-2001 Bruce Kalk <kall@compass.com>
 *
 * Currently maintained by: Bruce Kall, <kall@compass.com>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   $Id: utils.c,v 1.9 2002/02/22 20:35:42 bruce Exp $
 */
 

/*$Log: utils.c,v $
 *Revision 1.9  2002/02/22 20:35:42  bruce
 *Changed version number in message
 *
 *Revision 1.8  2001/04/23 20:37:25  bruce
 *Changed version string.
 *
 *Revision 1.7  2000/11/15 15:41:34  bruce
 *Added to time out message about kernel patch.
 *
 *Revision 1.6  2000/11/15 15:29:53  bruce
 *Changed last modified message
 *
 *Revision 1.5  2000/10/31 18:57:29  cph
 *Change timeout logic to use gettimeofday rather than deprecated ftime.
 *Simplify debugging messages to make high-volume dump easier to scan.
 *
 *Revision 1.4  2000/10/31 18:04:24  cph
 *Simplify byte-level I/O.  Fix alignment bug in copyright output.
 *
 *Revision 1.3  2000/10/31 15:20:04  cph
 *Add convenient byte-level I/O.
 *
 *Revision 1.2  2000/10/23 16:27:53  bruce
 *Removed extraneuos code.
 *
 *Revision 1.1  2000/09/28 13:44:18  bruce
 *Initial revision
 **/

static char rcsid[]="$Header: /home/bruce/linux-stuff/tpconfig/tpconfig-3.1.3/RCS/utils.c,v 1.9 2002/02/22 20:35:42 bruce Exp $";

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <getopt.h>
#include <termios.h>
#include "tpconfig.h"

#define TRUE 1
#define FALSE 0
#define TIMEOUT 3.0 /* wait for up to 3 seconds for a reply*/

/* General Rourines for PS2 port */
int ps2_write(int fd,unsigned char *buffer,int num_bytes)
{ /* ps2_write */
int bytes_written;
struct timeval start;
struct timeval now;
int at;
int left;
int i;
 
if (DEBUG_LEVEL == DEBUG_HIGH)
  fprintf (stderr, "PS2_write: %d bytes\n", num_bytes);
/* Use the temporary buffer to store the data as it arrives */
gettimeofday(&start, 0);
at = 0;
left = num_bytes;
if (DEBUG_LEVEL == DEBUG_HIGH)
  {
    fprintf (stderr, "PS2_write:");
    for (i = 0; i < num_bytes; i++)
      fprintf (stderr, " %#02x", *(buffer + i));
    fprintf (stderr, "\n");
  }

do
  {
  bytes_written = write(fd,(buffer + at),left);
  if(bytes_written > 0)
    {
    left = left - bytes_written;
    at = at + bytes_written;
    }
  gettimeofday(&now, 0);
  if ((((((double) now.tv_sec) * 1000000.0 + ((double) now.tv_usec))
	- (((double) start.tv_sec) * 1000000.0 + ((double) start.tv_usec)))
       / 1000000.0)
      > TIMEOUT)
    {
      fprintf (stderr, "\nTimed out waiting to write to device.\n");
      exit (-1);
    }
  }
while(left > 0);

if (DEBUG_LEVEL == DEBUG_HIGH)
  fprintf (stderr, "PS2_write: done\n");
 
return(at);
} /* ps2_write */
 
int ps2_read(int fd,unsigned char *buffer,int num_bytes)
{ /* ps2_read */
int bytes_read;
struct timeval start;
struct timeval now;
int at;
int left;
int i;
 
if (DEBUG_LEVEL == DEBUG_HIGH)
  fprintf (stderr, "PS2_read: %d bytes\n", num_bytes);
/* Use the temporary buffer to store the data as it arrives */
gettimeofday(&start, 0);
at = 0;
left = num_bytes;
do
  {
  bytes_read = read(fd,(buffer + at),left);
  if(bytes_read > 0)
    {
    left = left - bytes_read;
    at = at + bytes_read;
    }
  gettimeofday(&now, 0);
  if ((((((double) now.tv_sec) * 1000000.0 + ((double) now.tv_usec))
	- (((double) start.tv_sec) * 1000000.0 + ((double) start.tv_usec)))
       / 1000000.0)
      > TIMEOUT)
    {
      fprintf (stderr, "\nTimed out waiting to read from device (is pc_keyb.c kernel patch installed?)\n");
      exit (-1);
    }
  }
while(left > 0);
if (DEBUG_LEVEL == DEBUG_HIGH)
  {
    fprintf (stderr, "PS2_read:");
    for (i = 0; i < at; i++)
      fprintf (stderr, " %#2x", *(buffer + i));
    fprintf (stderr, "\n");
    fprintf (stderr, "PS2_read: read %d\n", at);
  }
return(at);
} /* ps2_read */

void copyright(void) 
{
  printf("\n");
  printf("                tpconfig version: %s\n", VERSION);
  printf("\n");
  printf(" Synaptics Touchpad and ALPS GlidePad/Stickpointer configuration tool\n");
  printf("\n");
  printf(" Copyright (C) 1997 C. Scott Ananian <cananian@alumni.princeton.edu>\n");
  printf(" Copyright (C) 1998-2001 Bruce Kall <kall@compass.com>\n");
  printf(" Last Modified (Version 3.1.3) by Bruce Kall, 2/22/2002\n");
  printf("\n");
  printf(" tpconfig comes with ABSOLUTELY NO WARRANTY.  This is free software,\n");
  printf(" and you are welcome to redistribute it under the terms of the GPL.\n");
  printf("\n");
}

/* write a byte to the ps/2 port, handling ACK */
void
putbyte (int fd, byte b) 
{
  if (DEBUG_LEVEL > DEBUG_LOW)
    fprintf (stderr, "putbyte: write %#02x\n", b);
  ps2_write (fd, &b, 1);
  getbyte_expected (fd, AUX_ACK, "putbyte");
}

/* read a byte from the ps/2 port */
byte
getbyte (int fd)
{
  byte b;
  ps2_read (fd, &b, 1);
  if (DEBUG_LEVEL > DEBUG_LOW)
    fprintf (stderr, "ps2_read_byte: read %#02x\n", b);
  return b;
}

void
getbyte_expected (int fd, byte expected, const char * what)
{
  byte b = getbyte (fd);
  if (DEBUG_LEVEL && b != expected)
    fprintf (stderr, "Read %#02x, expected %#02x in %s.\n", b, expected, what);
#if 0
  /* Upstream recommends commenting this out for 2.6 kernels.  */
  if (!ignore_selected_assertions)
    assert (b == expected);
#endif
}