blob: 2faa84b850190ed352ae03cadaf95d7dc4fa6413 [file] [log] [blame]
cliechti619e4562002-07-31 00:53:06 +00001#!/usr/bin/env python
2
3import struct
4import fcntl
5import os
6
7#----
8# Generated by h2py 0.1.1 from <linux/ppdev.h>,
9# then cleaned up a bit by Michael P. Ashton and then a gain by chris ;-)
10
11def sizeof(type): return struct.calcsize(type)
12def _IOC(dir, type, nr, size): return (dir << _IOC_DIRSHIFT ) | (type << _IOC_TYPESHIFT ) |\
13 (nr << _IOC_NRSHIFT ) | (size << _IOC_SIZESHIFT)
14def _IO(type, nr): return _IOC ( _IOC_NONE , ( type ) , ( nr ) , 0 )
15def _IOR(type,nr,size): return _IOC(_IOC_READ, type, nr, sizeof(size) )
16def _IOW(type,nr,size): return _IOC(_IOC_WRITE, type, nr, sizeof(size) )
17
18_IOC_SIZEBITS = 14
19_IOC_SIZEMASK = (1 << _IOC_SIZEBITS ) - 1
20_IOC_NRSHIFT = 0
21_IOC_NRBITS = 8
22_IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS
23_IOC_TYPEBITS = 8
24_IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS
25IOCSIZE_MASK = _IOC_SIZEMASK << _IOC_SIZESHIFT
26IOCSIZE_SHIFT = _IOC_SIZESHIFT
27_IOC_WRITE = 1
28_IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS
29IOC_IN = _IOC_WRITE << _IOC_DIRSHIFT
30_IOC_READ = 2
31IOC_INOUT = (_IOC_WRITE | _IOC_READ) << _IOC_DIRSHIFT
32IOC_OUT = _IOC_READ << _IOC_DIRSHIFT
33
34_IOC_NONE = 0
35PP_IOCTL = ord('p')
36PPCLAIM = _IO(PP_IOCTL , 0x8b)
37PPCLRIRQ = _IOR(PP_IOCTL, 0x93, 'i')
38
39PPDATADIR = _IOW(PP_IOCTL, 0x90, 'i')
40PPEXCL = _IO(PP_IOCTL, 0x8f)
41PPFCONTROL = _IOW(PP_IOCTL, 0x8e, 'BB')
42PPGETFLAGS = _IOR(PP_IOCTL, 0x9a, 'i')
43PPGETMODE = _IOR(PP_IOCTL, 0x98, 'i')
44PPGETMODES = _IOR(PP_IOCTL, 0x97, 'I')
45PPGETPHASE = _IOR(PP_IOCTL, 0x99, 'i')
46PPGETTIME = _IOR(PP_IOCTL, 0x95, 'll')
47PPNEGOT = _IOW(PP_IOCTL, 0x91, 'i')
48PPRCONTROL = _IOR(PP_IOCTL, 0x83, 'B')
49PPRDATA = _IOR(PP_IOCTL, 0x85, 'B')
50#'OBSOLETE__IOR' undefined in 'PPRECONTROL'
51PPRELEASE = _IO(PP_IOCTL, 0x8c)
52#'OBSOLETE__IOR' undefined in 'PPRFIFO'
53PPRSTATUS = _IOR(PP_IOCTL, 0x81, 'B')
54PPSETFLAGS = _IOW(PP_IOCTL, 0x9b, 'i')
55PPSETMODE = _IOW(PP_IOCTL, 0x80, 'i')
56PPSETPHASE = _IOW(PP_IOCTL, 0x94, 'i')
57PPSETTIME = _IOW(PP_IOCTL, 0x96, 'll')
58PPWCONTROL = _IOW(PP_IOCTL, 0x84, 'B')
59PPWCTLONIRQ = _IOW(PP_IOCTL, 0x92, 'B')
60PPWDATA = _IOW(PP_IOCTL, 0x86, 'B')
61#'OBSOLETE__IOW' undefined in 'PPWECONTROL'
62#'OBSOLETE__IOW' undefined in 'PPWFIFO'
63#'OBSOLETE__IOW' undefined in 'PPWSTATUS'
64PPYIELD = _IO(PP_IOCTL, 0x8d)
65PP_FASTREAD = 1 << 3
66PP_FASTWRITE = 1 << 2
67PP_W91284PIC = 1 << 4
68PP_FLAGMASK = PP_FASTWRITE | PP_FASTREAD | PP_W91284PIC
69PP_MAJOR = 99
70_ASMI386_IOCTL_H= None
71_IOC_DIRBITS = 2
72_IOC_DIRMASK = (1 << _IOC_DIRBITS) - 1
73_IOC_NRMASK = (1 << _IOC_NRBITS) - 1
74_IOC_TYPEMASK = (1 << _IOC_TYPEBITS ) - 1
75
76def _IOC_DIR(nr): return (nr >> _IOC_DIRSHIFT) & _IOC_DIRMASK
77def _IOC_NR(nr): return (nr >> _IOC_NRSHIFT) & _IOC_NRMASK
78def _IOC_SIZE(nr): return (nr >> _IOC_SIZESHIFT) & _IOC_SIZEMASK
79def _IOC_TYPE(nr): return (nr >> _IOC_TYPESHIFT) & _IOC_TYPEMASK
80def _IOWR(type, nr, size): return _IOC(_IOC_READ | _IOC_WRITE, type, nr , sizeof(size))
81
82__ELF__ = 1
83__i386 = 1
84__i386__ = 1
85__linux = 1
86__linux__ = 1
87__unix = 1
88__unix__ = 1
89i386 = 1
90linux = 1
91unix = 1
92
93#-------- Constants from <linux/parport.h>
94
95PARPORT_CONTROL_STROBE = 0x1
96PARPORT_CONTROL_AUTOFD = 0x2
97PARPORT_CONTROL_INIT = 0x4
98PARPORT_CONTROL_SELECT = 0x8
99PARPORT_STATUS_ERROR = 8
100PARPORT_STATUS_SELECT = 0x10
101PARPORT_STATUS_PAPEROUT = 0x20
102PARPORT_STATUS_ACK = 0x40
103PARPORT_STATUS_BUSY = 0x80
104
105IEEE1284_MODE_NIBBLE = 0
106IEEE1284_MODE_BYTE = 1
107IEEE1284_MODE_COMPAT = 1<<8
108IEEE1284_MODE_BECP = 1<<9
109IEEE1284_MODE_ECP = 1<<4
110IEEE1284_MODE_ECPRLE = IEEE1284_MODE_ECP | (1<<5)
111IEEE1284_MODE_ECPSWE = 1<<10
112IEEE1284_MODE_EPP = 1<<6
113IEEE1284_MODE_EPPSL = 1<<11
114IEEE1284_MODE_EPPSWE = 1<<12
115IEEE1284_DEVICEID = 1<<2
116IEEE1284_EXT_LINK = 1<<14
117
118IEEE1284_ADDR = 1<<13
119IEEE1284_DATA = 0
120
121PARPORT_EPP_FAST = 1
122PARPORT_W91284PIC = 2
123#----
124
125class Parallel:
126 """Class for controlling the pins on a parallel port
127
128 This class provides bit-level access to the pins on a PC parallel
129 port. It is primarily designed for programs which must control
130 special circuitry - most often non-IEEE-1284-compliant devices
131 other than printers - using 'bit-banging' techniques.
132
133 The current implementation makes ioctl() calls to the Linux ppdev
134 driver, using the Python fcntl library. It might be rewritten in
135 C for extra speed. This particular implementation is written for
136 Linux; all of the upper-level calls can be ported to Windows as
137 well.
138
139 On Linux, the ppdev device driver, from the Linux 2.4 parallel
140 port subsystem, is used to control the parallel port hardware.
141 This driver must be made available from a kernel compile. The
142 option is called "Support user-space parallel-port drivers". When
143 using the module, be sure to unload the lp module first: usually
144 the lp module claims exclusive access to the parallel port, and if
145 it is loaded, this class will fail to open the parallel port file,
146 and throw an exception.
147
148 The primary source of information about the Linux 2.4 parallel
149 port subsystem is Tim Waugh's documentation, the source for which
150 is available in the kernel tree. This document (called,
151 appropriately enough, "The Linux 2.4 Parallel Port Subsystem"),
152 thoroughly describes the parallel port drivers and how to use
153 them.
154
155 This class provides a method for each of the ioctls supported by
156 the ppdev module. The ioctl methods are named, in uppercase, the
157 same as the ioctls they invoke. The documentation for these
158 methods was taken directly from the documentation for their
159 corresponding ioctl, and modified only where necessary.
160
161 Unless you have special reason to use the Linux ioctls, you should
162 use instead the upper-level functions, which are named in
163 lowerCase fashion and should be portable between Linux and
164 Windows. This way, any code you write for this class will (or
165 should) also work with the Windows version of this class.
166
167 """
168 def __init__(self, port = 0):
169 if type(port) == type(""):
170 self.device = port
171 else:
172 self.device = "/dev/parports/%d" % port
173 self._fd = os.open(self.device, os.O_RDWR)
174 self.PPEXCL()
175 self.PPCLAIM()
176 self.setDataDir(1)
177 self.setData(0)
178
179 def __del__(self):
180 self.PPRELEASE()
181 os.close(self._fd)
182
183 def timevalToFloat(self, timeval):
184 t=struct.unpack('ll', timeval)
185 return t[0] + (t[1]/1000000.0)
186
187 def floatToTimeval(self, time):
188 sec = int(time)
189 usec = int(time*1000000.0)
190 return struct.pack('ll', sec, usec)
191
192 def PPCLAIM(self):
193 """
194 Claims access to the port. As a user-land device driver
195 writer, you will need to do this before you are able to
196 actually change the state of the parallel port in any
197 way. Note that some operations only affect the ppdev driver
198 and not the port, such as PPSETMODE; they can be performed
199 while access to the port is not claimed.
200 """
201 fcntl.ioctl(self._fd,PPCLAIM)
202
203 def PPEXCL(self):
204 """
205 Instructs the kernel driver to forbid any sharing of the port
206 with other drivers, i.e. it requests exclusivity. The PPEXCL
207 command is only valid when the port is not already claimed for
208 use, and it may mean that the next PPCLAIM ioctl will fail:
209 some other driver may already have registered itself on that
210 port.
211
212 Most device drivers don't need exclusive access to the
213 port. It's only provided in case it is really needed, for
214 example for devices where access to the port is required for
215 extensive periods of time (many seconds).
216
217 Note that the PPEXCL ioctl doesn't actually claim the port
218 there and then---action is deferred until the PPCLAIM ioctl is
219 performed.
220 """
221 fcntl.ioctl(self._fd,PPEXCL)
222
223 def PPRELEASE(self):
224 """
225 Releases the port. Releasing the port undoes the effect of
226 claiming the port. It allows other device drivers to talk to
227 their devices (assuming that there are any).
228 """
229 fcntl.ioctl(self._fd, PPRELEASE)
230
231 def PPYIELD(self):
232 """
233 Yields the port to another driver. This ioctl is a kind of
234 short-hand for releasing the port and immediately reclaiming
235 it. It gives other drivers a chance to talk to their devices,
236 but afterwards claims the port back. An example of using this
237 would be in a user-land printer driver: once a few characters
238 have been written we could give the port to another device
239 driver for a while, but if we still have characters to send to
240 the printer we would want the port back as soon as possible.
241
242 It is important not to claim the parallel port for too long,
243 as other device drivers will have no time to service their
244 devices. If your device does not allow for parallel port
245 sharing at all, it is better to claim the parallel port
246 exclusively (see PPEXCL).
247 """
248 fcntl.ioctl(self._fd, PPYIELD)
249
250 def PPNEGOT(self, mode):
251 """
252 Performs IEEE 1284 negotiation into a particular
253 mode. Briefly, negotiation is the method by which the host and
254 the peripheral decide on a protocol to use when transferring
255 data.
256
257 An IEEE 1284 compliant device will start out in compatibility
258 mode, and then the host can negotiate to another mode (such as
259 ECP).
260
261 The 'mode' parameter should be one of the following constants
262 from PPDEV:
263
264 - IEEE1284_MODE_COMPAT
265 - IEEE1284_MODE_NIBBLE
266 - IEEE1284_MODE_BYTE
267 - IEEE1284_MODE_EPP
268 - IEEE1284_MODE_ECP
269
270 The PPNEGOT ioctl actually does two things: it performs the
271 on-the-wire negotiation, and it sets the behaviour of
272 subsequent read/write calls so that they use that mode (but
273 see PPSETMODE).
274 """
275 fcntl.ioctl(self._fd, PPNEGOT, struct.pack('i', mode))
276
277 def PPSETMODE(self, mode):
278 """
279 Sets which IEEE 1284 protocol to use for the read and write
280 calls.
281
282 The 'mode' parameter should be one of the following constants
283 from PPDEV:
284
285 - IEEE1284_MODE_COMPAT
286 - IEEE1284_MODE_NIBBLE
287 - IEEE1284_MODE_BYTE
288 - IEEE1284_MODE_EPP
289 - IEEE1284_MODE_ECP
290 """
291 fcntl.ioctl(self._fd, PPSETMODE, struct.pack('i', mode))
292
293 def PPGETMODE(self):
294 """
295 Retrieves the IEEE 1284 mode being used for read and
296 write. The return value is one of the following constants
297 from PPDEV:
298
299 - IEEE1284_MODE_COMPAT
300 - IEEE1284_MODE_NIBBLE
301 - IEEE1284_MODE_BYTE
302 - IEEE1284_MODE_EPP
303 - IEEE1284_MODE_ECP
304 """
305 ret = struct.pack('i', 0)
306 ret = fcntl.ioctl(self._fd, PPGETMODE, ret)
307 return struct.unpack('i', ret)[0]
308
309 def PPGETTIME(self):
310 """
311 Retrieves the time-out value. The read and write calls will
312 time out if the peripheral doesn't respond quickly enough. The
313 PPGETTIME ioctl retrieves the length of time that the
314 peripheral is allowed to have before giving up.
315
316 Returns the timeout value in seconds as a floating-point value.
317 """
318 ret = struct.pack('ll', 0, 0)
319 ret = fcntl.ioctl(self._fd, PPGETTIME, ret)
320 return timevalToFloat(ret)
321
322 def PPSETTIME(self, time):
323 """
324 Sets the time-out (see PPGETTIME for more information).
325 'time' is the new time-out in seconds; floating-point values
326 are acceptable.
327 """
328 fcntl.ioctl(self._fd, PPSETTIME, floatToTimeval(time))
329
330 def PPGETMODES(self):
331 """
332 Retrieves the capabilities of the hardware (i.e. the modes
333 field of the parport structure).
334 """
335 raise NotImplementedError
336
337 def PPSETFLAGS(self):
338 """
339 Sets flags on the ppdev device which can affect future I/O
340 operations. Available flags are:
341
342 - PP_FASTWRITE
343 - PP_FASTREAD
344 - PP_W91284PIC
345 """
346 raise NotImplementedError
347
348 def PPWCONTROL(self, lines):
349 """
350 Sets the control lines. The 'lines' parameter is a bitwise OR
351 of the following constants from PPDEV:
352
353 - PARPORT_CONTROL_STROBE
354 - PARPORT_CONTROL_AUTOFD
355 - PARPORT_CONTROL_INIT
356 - PARPORT_CONTROL_SELECT
357 """
358 fcntl.ioctl(self._fd, PPWCONTROL, struct.pack('B', lines))
359
360 def PPRCONTROL(self):
361 """
362 Returns the last value written to the control register, in the
363 form of an integer, for which each bit corresponds to a control
364 line (although some are unused).
365
366 This doesn't actually touch the hardware; the last value
367 written is remembered in software. This is because some
368 parallel port hardware does not offer read access to the
369 control register.
370
371 The control lines bits are defined by the following constants
372 from PPDEV:
373
374 - PARPORT_CONTROL_STROBE
375 - PARPORT_CONTROL_AUTOFD
376 - PARPORT_CONTROL_SELECT
377 - PARPORT_CONTROL_INIT
378 """
379 ret = struct.pack('B',0)
380 ret = fcntl.ioctl(self._fd, PPRCONTROL, ret)
381 return struct.unpack('B', ret)[0]
382
383 def PPFCONTROL(self, mask, val):
384 """
385 Frobs the control lines. Since a common operation is to change
386 one of the control signals while leaving the others alone, it
387 would be quite inefficient for the user-land driver to have to
388 use PPRCONTROL, make the change, and then use PPWCONTROL. Of
389 course, each driver could remember what state the control
390 lines are supposed to be in (they are never changed by
391 anything else), but in order to provide PPRCONTROL, ppdev must
392 remember the state of the control lines anyway.
393
394 The PPFCONTROL ioctl is for "frobbing" control lines, and is
395 like PPWCONTROL but acts on a restricted set of control
396 lines. The ioctl parameter is a pointer to a struct
397 ppdev_frob_struct:
398
399 struct ppdev_frob_struct {
400 unsigned char mask;
401 unsigned char val;
402 };
403
404 The mask and val fields are bitwise ORs of control line names
405 (such as in PPWCONTROL). The operation performed by PPFCONTROL
406 is:
407
408 new_ctr = (old_ctr & ~mask) | val
409
410 In other words, the signals named in mask are set to the
411 values in val.
412 """
413 fcntl.ioctl(self._fd, PPFCONTROL, struct.pack('BB', mask, val))
414
415 def PPRSTATUS(self):
416 """
417 Returns an unsigned char containing bits set for each status
418 line that is set (for instance, PARPORT_STATUS_BUSY). The
419 ioctl parameter should be a pointer to an unsigned char.
420 """
421 ret = struct.pack('B',0)
422 ret = fcntl.ioctl(self._fd, PPRSTATUS, ret)
423 return struct.unpack('B', ret)[0]
424
425 def PPDATADIR(self, out):
426 """
427 Controls the data line drivers. Normally the computer's
428 parallel port will drive the data lines, but for byte-wide
429 transfers from the peripheral to the host it is useful to turn
430 off those drivers and let the peripheral drive the
431 signals. (If the drivers on the computer's parallel port are
432 left on when this happens, the port might be damaged.)
433 This is only needed in conjunction with PPWDATA or PPRDATA.
434
435 The 'out' parameter indicates the desired port direction. If
436 'out' is true or non-zero, the drivers are turned on (forward
437 direction); otherwise, the drivers are turned off (reverse
438 direction).
439 """
440 if out:
441 msg=struct.pack('i',0)
442 else:
443 msg=struct.pack('i',1)
444 fcntl.ioctl(self._fd, PPDATADIR, msg)
445
446 def PPWDATA(self, byte):
447 """
448 Sets the data lines (if in forward mode). The ioctl parameter
449 is a pointer to an unsigned char.
450 """
451 fcntl.ioctl(self._fd, PPWDATA,struct.pack('B',byte))
452
453 def PPRDATA(self):
454 """
455 Reads the data lines (if in reverse mode). The ioctl parameter
456 is a pointer to an unsigned char.
457 """
458 ret=struct.pack('B',0)
459 ret=fcntl.ioctl(self._fd, PPRDATA,ret)
460 return struct.unpack('B',ret)[0]
461
462 def PPCLRIRQ(self):
463 """
464 Returns the current interrupt count, and clears it. The ppdev
465 driver keeps a count of interrupts as they are triggered.
466 """
467 ret=struct.pack('i',0)
468 ret=fcntl.ioctl(self._fd, PPCLRIRQ,ret)
469 return struct.unpack('i',ret)[0]
470
471 def PPWCTLONIRQ(self, lines):
472 """
473 Set a trigger response. Afterwards when an interrupt is
474 triggered, the interrupt handler will set the control lines as
475 requested. The ioctl parameter is a pointer to an unsigned
476 char, which is interpreted in the same way as for PPWCONTROL.
477
478 The reason for this ioctl is simply speed. Without this ioctl,
479 responding to an interrupt would start in the interrupt
480 handler, switch context to the user-land driver via poll or
481 select, and then switch context back to the kernel in order to
482 handle PPWCONTROL. Doing the whole lot in the interrupt
483 handler is a lot faster.
484 """
485 fcntl.ioctl(self._fd, PPWCTLONIRQ,struct.pack('B',lines))
486
487 #data lines
488## def data(self):
489## """Returns the states of the data bus line drivers (pins 2-9)"""
490## return self._data
491
492 def setDataDir(self,out):
493 """Activates or deactivates the data bus line drivers (pins 2-9)"""
494 self._dataDir = out
495 self.PPDATADIR(out)
496
497 def dataDir(self):
498 """Returns true if the data bus line drivers are on (pins 2-9)"""
499 return self._dataDir
500
501 #control lines
502## def strobe(self):
503## """Returns the state of the nStrobe output (pin 1)"""
504## return (self.PPRCONTROL()&PARPORT_CONTROL_STROBE)==0
505
506 def setDataStrobe(self, level):
507 """Sets the state of the nStrobe output (pin 1)"""
508 if level:
509 self.PPFCONTROL(PARPORT_CONTROL_STROBE, 0)
510 else:
511 self.PPFCONTROL(PARPORT_CONTROL_STROBE, PARPORT_CONTROL_STROBE)
512
513## def autoFd(self):
514## """Returns the state of the nAutoFd output (pin 14)"""
515## return (self.PPRCONTROL()&PARPORT_CONTROL_AUTOFD)==0
516
517 def setAutoFeed(self, level):
518 """Sets the state of the nAutoFd output (pin 14)"""
519 if level:
520 self.PPFCONTROL(PARPORT_CONTROL_AUTOFD, 0)
521 else:
522 self.PPFCONTROL(PARPORT_CONTROL_AUTOFD, PARPORT_CONTROL_AUTOFD)
523
524## def init(self):
525## """Returns the state of the nInit output (pin 16)"""
526## return (self.PPRCONTROL()&PARPORT_CONTROL_INIT)!=0
527
528 def setInitOut(self, level):
529 """Sets the state of the nInit output (pin 16)"""
530 if level:
531 self.PPFCONTROL(PARPORT_CONTROL_INIT, PARPORT_CONTROL_INIT)
532 else:
533 self.PPFCONTROL(PARPORT_CONTROL_INIT, 0)
534
535## def selectIn(self):
536## """Returns the state of the nSelectIn output (pin 17)"""
537## return (self.PPRCONTROL()&PARPORT_CONTROL_SELECT)==0
538
539 def setSelect(self,level):
540 """Sets the state of the nSelectIn output (pin 17)"""
541 if level:
542 self.PPFCONTROL(PARPORT_CONTROL_SELECT, 0)
543 else:
544 self.PPFCONTROL(PARPORT_CONTROL_SELECT, PARPORT_CONTROL_SELECT)
545
546 def setData(self,d):
547 """Sets the states of the data bus line drivers (pins 2-9)"""
548 self._data=d
549 return self.PPWDATA(d)
550
551 #status lines
552 def getInError(self):
553 """Returns the level on the nFault pin (15)"""
554 return (self.PPRSTATUS() & PARPORT_STATUS_ERROR) != 0
555
556 def gettInSelected(self):
557 """Returns the level on the Select pin (13)"""
558 return (self.PPRSTATUS() & PARPORT_STATUS_SELECT) != 0
559
560 def getInPaperOut(self):
561 """Returns the level on the paperOut pin (12)"""
562 return (self.PPRSTATUS() & PARPORT_STATUS_PAPEROUT) != 0
563
564 def getInAcknowledge(self):
565 """Returns the level on the nAck pin (10)"""
566 return (self.PPRSTATUS() & PARPORT_STATUS_ACK) != 0
567
568 def getInBusy(self):
569 """Returns the level on the Busy pin (11)"""
570 return (self.PPRSTATUS() & PARPORT_STATUS_BUSY) == 0
571