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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/******************************************************
*
* zexy - implementation file
*
* copyleft (c) IOhannes m zmölnig
*
* 1999:forum::für::umläute:2004
*
* institute of electronic music and acoustics (iem)
*
******************************************************
*
* license: GNU General Public License v.2
*
******************************************************/
/*
(c) 2000:forum::für::umläute:2005
write to the parallel port
extended to write to any port (if we do have permissions)
2005-09-28: write to devices instead of hardware-addresses
http://people.redhat.com/twaugh/parport/html/ppdev.html
TODO: don't lock when multiple objects refer to the same device
TODO: allow readonly/writeonly access
TODO: test for timeouts,...
thanks to
Olaf Matthes: porting to WindozeNT/2000/XP
Thomas Musil: adding "control-output" and "input"
*/
#define BASE0 0x3bc
#define BASE1 0x378
#define BASE2 0x278
#define MODE_IOPERM 1
#define MODE_IOPL 0
#define MODE_NONE -1
#include "zexy.h"
/* ----------------------- lpt --------------------- */
#ifdef Z_WANT_LPT
# include <stdlib.h>
# include <errno.h>
# ifdef HAVE_LINUX_PPDEV_H
# include <unistd.h>
# include <sys/ioctl.h>
# include <linux/ppdev.h>
# include <linux/parport.h>
# include <fcntl.h>
# include <stdio.h>
# endif /* HAVE_LINUX_PPDEV_H */
# ifdef __WIN32__
/* on windoze everything is so complicated... */
extern int read_parport(int port);
extern void write_parport(int port, int value);
extern int open_port(int port);
static int ioperm(int port, int a, int b)
{
if(open_port(port) == -1)return(1);
return(0);
}
static int iopl(int i)
{
return(-1);
}
static void sys_outb(unsigned char byte, int port)
{
write_parport(port, byte);
}
static int sys_inb(int port)
{
return read_parport(port);
}
# else
/* thankfully there is linux */
# include <sys/io.h>
static void sys_outb(unsigned char byte, int port)
{
outb(byte, port);
}
static int sys_inb(int port)
{
return inb(port);
}
# endif /* __WIN32__ */
#endif /* Z_WANT_LP */
static int count_iopl = 0;
static t_class *lpt_class;
typedef struct _lpt
{
t_object x_obj;
unsigned long port;
int device; /* file descriptor of device, in case we are using one ...*/
int mode; // MODE_IOPERM, MODE_IOPL
} t_lpt;
static void lpt_float(t_lpt *x, t_floatarg f)
{
#ifdef Z_WANT_LPT
unsigned char b = f;
# ifdef HAVE_LINUX_PPDEV_H
if (x->device>0){
ioctl (x->device, PPWDATA, &b);
} else
# endif
if (x->port) {
sys_outb(b, x->port+0);
}
#endif /* Z_WANT_LPT */
}
static void lpt_control(t_lpt *x, t_floatarg f)
{
#ifdef Z_WANT_LPT
unsigned char b = f;
# ifdef HAVE_LINUX_PPDEV_H
if (x->device>0){
ioctl (x->device, PPWCONTROL, &b);
} else
# endif
if (x->port) {
sys_outb(b, x->port+2);
}
#endif /* Z_WANT_LPT */
}
static void lpt_bang(t_lpt *x)
{
#ifdef Z_WANT_LPT
# ifdef HAVE_LINUX_PPDEV_H
if (x->device>0){
unsigned char b=0;
ioctl (x->device, PPRCONTROL, &b);
outlet_float(x->x_obj.ob_outlet, (float)b);
} else
# endif
if (x->port) {
outlet_float(x->x_obj.ob_outlet, (float)sys_inb(x->port+1));
}
#endif /* Z_WANT_LPT */
}
static void *lpt_new(t_symbol *s, int argc, t_atom *argv)
{
t_lpt *x = (t_lpt *)pd_new(lpt_class);
char*devname=atom_getsymbol(argv)->s_name;
if(s==gensym("lp"))
error("lpt: the use of 'lp' has been deprecated; use 'lpt' instead");
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("control"));
outlet_new(&x->x_obj, gensym("float"));
x->mode = MODE_NONE;
x->port = 0;
x->device = -1;
#ifdef Z_WANT_LPT
if ((argc==0)||(argv->a_type==A_FLOAT)) {
/* FLOAT specifies a parallel port */
switch ((int)((argc)?atom_getfloat(argv):0)) {
case 0:
x->port = BASE0;
break;
case 1:
x->port = BASE1;
break;
case 2:
x->port = BASE2;
break;
default:
error("lpt : only lpt0, lpt1 and lpt2 are accessible");
x->port = 0;
return (x);
}
} else {
/* SYMBOL might be a file or a hex port-number;
we ignore the file (device) case by now;
LATER think about this
*/
x->device=-1;
x->port=strtol(devname, 0, 16);
if(0==x->port){
#ifdef HAVE_LINUX_PPDEV_H
x->device = open(devname, O_RDWR);
if(x->device<=0){
error("lpt: bad device %s", devname);
return(x);
} else {
if (ioctl (x->device, PPCLAIM)) {
perror ("PPCLAIM");
close (x->device);
x->device=-1;
}
}
#endif /* HAVE_LINUX_PPDEV_H */
}
}
if ((x->device<0) && (!x->port || x->port>65535)){
post("lpt : bad port %x", x->port);
x->port = 0;
return (x);
}
if (x->device<0){
/* this is ugly: when using a named device,
* we are currently assuming that we have read/write-access
* of course, this is not necessary true
*/
/* furthermore, we might also use the object
* withOUT write permissions
* (just reading the parport)
*/
if (x->port && x->port < 0x400){
if (ioperm(x->port, 8, 1)) {
x->mode=MODE_NONE;
} else x->mode = MODE_IOPERM;
}
if(x->mode==MODE_NONE){
if (iopl(3)){
x->mode=MODE_NONE;
} else x->mode=MODE_IOPL;
count_iopl++;
// post("iopl.............................%d", count_iopl);
}
if(x->mode==MODE_NONE){
error("lpt : couldn't get write permissions");
x->port = 0;
return (x);
}
}
if(x->device>0)
post("lpt: connected to device %s", devname);
else
post("lpt: connected to port %x in mode '%s'", x->port, (x->mode==MODE_IOPL)?"iopl":"ioperm");
if (x->mode==MODE_IOPL)post("lpt-warning: this might seriously damage your pc...");
#else
error("zexy has been compiled without [lpt]!");
count_iopl=0;
#endif /* Z_WANT_LPT */
devname=0;
return (x);
}
static void lpt_free(t_lpt *x)
{
#ifdef Z_WANT_LPT
# ifdef HAVE_LINUX_PPDEV_H
if (x->device>0){
ioctl (x->device, PPRELEASE);
close(x->device);
x->device=0;
} else
# endif
if (x->port) {
if (x->mode==MODE_IOPERM && ioperm(x->port, 8, 0)) error("lpt: couldn't clean up device");
else if (x->mode==MODE_IOPL && (!--count_iopl) && iopl(0))
error("lpt: couldn't clean up device");
}
#endif /* Z_WANT_LPT */
}
static void lpt_helper(t_lpt *x)
{
ZEXY_USEVAR(x);
post("\n%c lpt :: direct access to the parallel port", HEARTSYMBOL);
post("<byte>\t: write byte to the parallel-port");
post("\ncreation:\t\"lpt [<port>]\": connect to parallel port <port> (0..2)");
post("\t\t\"lpt <portaddr>\": connect to port @ <portaddr> (hex)");
}
void lpt_setup(void)
{
lpt_class = class_new(gensym("lpt"),
(t_newmethod)lpt_new, (t_method)lpt_free,
sizeof(t_lpt), 0, A_GIMME, 0);
class_addcreator((t_newmethod)lpt_new, gensym("lp"), A_GIMME, 0);
class_addfloat(lpt_class, (t_method)lpt_float);
class_addmethod(lpt_class, (t_method)lpt_control, gensym("control"), A_FLOAT, 0);
class_addbang(lpt_class, (t_method)lpt_bang);
class_addmethod(lpt_class, (t_method)lpt_helper, gensym("help"), 0);
class_sethelpsymbol(lpt_class, gensym("zexy/lpt"));
zexy_register("lpt");
}
|