1 | /* libcqcam - shared Color Quickcam routines |
---|
2 | * Copyright (C) 1996-1998 by Patrick Reynolds |
---|
3 | * Email: <no.email@noemail.com> |
---|
4 | * |
---|
5 | * This library is free software; you can redistribute it and/or |
---|
6 | * modify it under the terms of the GNU Library General Public |
---|
7 | * License as published by the Free Software Foundation; either |
---|
8 | * version 2 of the License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This library is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | * Library General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Library General Public |
---|
16 | * License along with this library; if not, write to the |
---|
17 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
---|
18 | * Boston, MA 02110-1301, USA. |
---|
19 | */ |
---|
20 | |
---|
21 | // I/O ports wrapper definitions and prototypes |
---|
22 | // This file might need tweaking if you're trying to port my code to other |
---|
23 | // x86 Unix platforms. Code is already available for Linux, FreeBSD, and |
---|
24 | // QNX; see the Makefile. |
---|
25 | // |
---|
26 | // QNX code by: Anders Arpteg <aa11ac@hik.se> |
---|
27 | // FreeBSD code by: Patrick Reynolds <reynolds@cs.duke.edu> and Charles |
---|
28 | // Henrich <henrich@msu.edu> |
---|
29 | // Inlining implemented by: Philip Blundell <philip.blundell@pobox.com> |
---|
30 | |
---|
31 | #ifndef PORT_H |
---|
32 | #define PORT_H |
---|
33 | |
---|
34 | //#include "config.h" |
---|
35 | |
---|
36 | #include <unistd.h> |
---|
37 | |
---|
38 | #ifdef __linux__ |
---|
39 | #if !defined(arm) && !defined(__hppa__) && !defined(__sparc__) && !defined(__ppc__) && !defined(__powerpc__) && !defined(__s390__) && !defined(__s390x__) && !defined(__mips__) && !defined(__mc68000__) |
---|
40 | #include <sys/io.h> |
---|
41 | #endif /* !arm */ |
---|
42 | #elif defined(QNX) |
---|
43 | #include <conio.h> |
---|
44 | #elif defined(__FreeBSD__) |
---|
45 | #include <machine/cpufunc.h> |
---|
46 | #include <stdio.h> |
---|
47 | #elif defined(BSDI) |
---|
48 | #include <machine/inline.h> |
---|
49 | #elif defined(OPENBSD) |
---|
50 | #include <machine/pio.h> |
---|
51 | #elif defined(LYNX) |
---|
52 | #include "lynx-io.h" |
---|
53 | #elif defined(SOLARIS) |
---|
54 | #include "solaris-io.h" |
---|
55 | #else |
---|
56 | #error Please define a platform in the Makefile |
---|
57 | #endif |
---|
58 | |
---|
59 | #if defined(arm) || defined(__hppa__) || defined(__sparc__) || defined(__ppc__) || defined(__powerpc__) || defined(__s390__) || defined(__s390x__) || defined(__mips__) || defined(__mc68000__) |
---|
60 | static char ports_temp; |
---|
61 | |
---|
62 | #ifdef inb |
---|
63 | #undef inb |
---|
64 | #endif /* inb */ |
---|
65 | #define inb(port) \ |
---|
66 | lseek(devport, port, SEEK_SET), \ |
---|
67 | read(devport, &ports_temp, 1), \ |
---|
68 | ports_temp |
---|
69 | |
---|
70 | #ifdef outb |
---|
71 | #undef outb |
---|
72 | #endif /* inb */ |
---|
73 | #define outb(data, port) \ |
---|
74 | lseek(devport, port, SEEK_SET); \ |
---|
75 | ports_temp = data; \ |
---|
76 | write(devport, &ports_temp, 1); |
---|
77 | |
---|
78 | #endif /* arm, hppa */ |
---|
79 | |
---|
80 | class port_t { |
---|
81 | public: |
---|
82 | port_t(int iport); |
---|
83 | ~port_t(void); |
---|
84 | |
---|
85 | inline int read_data(void) { return inb(port); } |
---|
86 | inline int read_status(void) { return inb(port1); } |
---|
87 | inline int read_control(void) { return inb(port2); } |
---|
88 | |
---|
89 | #if defined(LINUX) || defined(LYNX) |
---|
90 | inline void write_data(int data) { outb(data, port); } |
---|
91 | inline void write_control(int data) { outb(control_reg = data, port2); } |
---|
92 | inline void setbit_control(int data) { outb(control_reg |= data, port2); } |
---|
93 | inline void clearbit_control(int data) { outb(control_reg &= ~data, port2); } |
---|
94 | #else // Solaris, QNX, and *BSD use (port, data) instead |
---|
95 | inline void write_data(int data) { outb(port, data); } |
---|
96 | inline void write_control(int data) { outb(port2, control_reg = data); } |
---|
97 | inline void setbit_control(int data) { outb(port2, control_reg |= data); } |
---|
98 | inline void clearbit_control(int data) { outb(port2, control_reg &= ~data); } |
---|
99 | #endif |
---|
100 | |
---|
101 | inline int get_port() { return port; } |
---|
102 | inline operator bool () const { return port != -1; } |
---|
103 | |
---|
104 | private: |
---|
105 | int port; // number of the base port |
---|
106 | int port1; // port+1, precalculated for speed |
---|
107 | int port2; // port+2, precalculated for speed |
---|
108 | int control_reg; // current contents of the control register |
---|
109 | #ifdef LOCKING |
---|
110 | int lock_fd; |
---|
111 | int lock(int portnum); |
---|
112 | void unlock(int portnum); |
---|
113 | #endif |
---|
114 | |
---|
115 | #ifdef FREEBSD |
---|
116 | FILE *devio; |
---|
117 | #endif |
---|
118 | #if defined(__linux__) && (defined(arm) || defined(__hppa__) || defined(__sparc__) || defined(__ppc__) || defined(__powerpc__) || defined(__s390__) || defined(__s390x__) || defined(__mips__) || defined(__mc68000__)) |
---|
119 | int devport; |
---|
120 | #endif |
---|
121 | }; |
---|
122 | |
---|
123 | #endif |
---|