cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
ioctl.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 #!/usr/bin/python2.4
21 # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
22 #
23 # $Id$
24 #
25 # Copyright (C) 1999-2006 Keith Dart <keith@kdart.com>
26 #
27 # This library is free software; you can redistribute it and/or
28 # modify it under the terms of the GNU Lesser General Public
29 # License as published by the Free Software Foundation; either
30 # version 2.1 of the License, or (at your option) any later version.
31 #
32 # This library is distributed in the hope that it will be useful,
33 # but WITHOUT ANY WARRANTY; without even the implied warranty of
34 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 # Lesser General Public License for more details.
36 
37 ##
38 #
39 # Linux ioctl macros. Taken from /usr/include/asm/ioctl.h
40 #
41 #
42 
43 # ioctl command encoding: 32 bits total, command in lower 16 bits,
44 # size of the parameter structure in the lower 14 bits of the
45 # upper 16 bits.
46 # Encoding the size of the parameter structure in the ioctl request
47 # is useful for catching programs compiled with old versions
48 # and to avoid overwriting user space outside the user buffer area.
49 # The highest 2 bits are reserved for indicating the ``access mode''.
50 # NOTE: This limits the max parameter size to 16kB -1 !
51 #
52 #
53 # The following is for compatibility across the various Linux
54 # platforms. The i386 ioctl numbering scheme doesn't really enforce
55 # a type field. De facto, however, the top 8 bits of the lower 16
56 # bits are indeed used as a type field, so we might just as well make
57 # this explicit here. Please be sure to use the decoding macros
58 # below from now on.
59 
60 import struct
61 sizeof = struct.calcsize
62 
63 _IOC_NRBITS = 8
64 _IOC_TYPEBITS = 8
65 _IOC_SIZEBITS = 14
66 _IOC_DIRBITS = 2
67 _IOC_NRMASK = ((1 << _IOC_NRBITS)-1)
68 _IOC_TYPEMASK = ((1 << _IOC_TYPEBITS)-1)
69 _IOC_SIZEMASK = ((1 << _IOC_SIZEBITS)-1)
70 _IOC_DIRMASK = ((1 << _IOC_DIRBITS)-1)
71 _IOC_NRSHIFT = 0
72 _IOC_TYPESHIFT = (_IOC_NRSHIFT+_IOC_NRBITS)
73 _IOC_SIZESHIFT = (_IOC_TYPESHIFT+_IOC_TYPEBITS)
74 _IOC_DIRSHIFT = (_IOC_SIZESHIFT+_IOC_SIZEBITS)
75 
76 IOCSIZE_MASK = (_IOC_SIZEMASK << _IOC_SIZESHIFT)
77 IOCSIZE_SHIFT = (_IOC_SIZESHIFT)
78 
79 ###
80 # direction bits
81 _IOC_NONE = 0
82 _IOC_WRITE = 1
83 _IOC_READ = 2
84 
85 def _IOC(dir,type,nr,FMT):
86  return int((((dir) << _IOC_DIRSHIFT) | \
87  ((type) << _IOC_TYPESHIFT) | \
88  ((nr) << _IOC_NRSHIFT) | \
89  ((FMT) << _IOC_SIZESHIFT)) & 0xffffffff )
90 
91 
92 # used to create numbers
93 # type is the assigned type from the kernel developers
94 # nr is the base ioctl number (defined by driver writer)
95 # FMT is a struct module format string.
96 def _IO(type,nr): return _IOC(_IOC_NONE,(type),(nr),0)
97 def _IOR(type,nr,FMT): return _IOC(_IOC_READ,(type),(nr),sizeof(FMT))
98 def _IOW(type,nr,FMT): return _IOC(_IOC_WRITE,(type),(nr),sizeof(FMT))
99 def _IOWR(type,nr,FMT): return _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(FMT))
100 
101 # used to decode ioctl numbers
102 def _IOC_DIR(nr): return (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
103 def _IOC_TYPE(nr): return (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
104 def _IOC_NR(nr): return (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
105 def _IOC_SIZE(nr): return (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
106