added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / include / linux / kdev_t.h
1 #ifndef _LINUX_KDEV_T_H
2 #define _LINUX_KDEV_T_H
3 #if defined(__KERNEL__) || defined(_LVM_H_INCLUDE)
4 /*
5 As a preparation for the introduction of larger device numbers,
6 we introduce a type kdev_t to hold them. No information about
7 this type is known outside of this include file.
8
9 Objects of type kdev_t designate a device. Outside of the kernel
10 the corresponding things are objects of type dev_t - usually an
11 integral type with the device major and minor in the high and low
12 bits, respectively. Conversion is done by
13
14 extern kdev_t to_kdev_t(int);
15
16 It is up to the various file systems to decide how objects of type
17 dev_t are stored on disk.
18 The only other point of contact between kernel and outside world
19 are the system calls stat and mknod, new versions of which will
20 eventually have to be used in libc.
21
22 [Unfortunately, the floppy control ioctls fail to hide the internal
23 kernel structures, and the fd_device field of a struct floppy_drive_struct
24 is user-visible. So, it remains a dev_t for the moment, with some ugly
25 conversions in floppy.c.]
26
27 Inside the kernel, we aim for a kdev_t type that is a pointer
28 to a structure with information about the device (like major,
29 minor, size, blocksize, sectorsize, name, read-only flag,
30 struct file_operations etc.).
31
32 However, for the time being we let kdev_t be almost the same as dev_t:
33
34 typedef struct { unsigned short major, minor; } kdev_t;
35
36 Admissible operations on an object of type kdev_t:
37 - passing it along
38 - comparing it for equality with another such object
39 - storing it in ROOT_DEV, inode->i_dev, inode->i_rdev, sb->s_dev,
40   bh->b_dev, req->rq_dev, de->dc_dev, tty->device
41 - using its bit pattern as argument in a hash function
42 - finding its major and minor
43 - complaining about it
44
45 An object of type kdev_t is created only by the function MKDEV(),
46 with the single exception of the constant 0 (no device).
47
48 Right now the other information mentioned above is usually found
49 in static arrays indexed by major or major,minor.
50
51 An obstacle to immediately using
52     typedef struct { ... (* lots of information *) } *kdev_t
53 is the case of mknod used to create a block device that the
54 kernel doesn't know about at present (but first learns about
55 when some module is inserted).
56
57 aeb - 950811
58 */
59
60 /* Since MINOR(dev) is used as index in static arrays,
61    the kernel is not quite ready yet for larger minors.
62    However, everything runs fine with an arbitrary kdev_t type. */
63
64 #define MINORBITS       8
65 #define MINORMASK       ((1U << MINORBITS) - 1)
66
67 typedef unsigned short kdev_t;
68
69 #define MAJOR(dev)      ((unsigned int) ((dev) >> MINORBITS))
70 #define MINOR(dev)      ((unsigned int) ((dev) & MINORMASK))
71 #define HASHDEV(dev)    ((unsigned int) (dev))
72 #define NODEV           0
73 #define MKDEV(ma,mi)    (((ma) << MINORBITS) | (mi))
74 #define B_FREE          0xffff          /* yuk */
75
76 extern const char * kdevname(kdev_t);   /* note: returns pointer to static data! */
77
78 /* 2.5.x compatibility */
79 #define mk_kdev(a,b)    MKDEV(a,b)
80 #define major(d)        MAJOR(d)
81 #define minor(d)        MINOR(d)
82 #define kdev_same(a,b)  ((a) == (b))
83 #define kdev_none(d)    (!(d))
84 #define kdev_val(d)     ((unsigned int)(d))
85 #define val_to_kdev(d)  ((kdev_t)(d))
86
87 /*
88 As long as device numbers in the outside world have 16 bits only,
89 we use these conversions.
90 */
91
92 static inline unsigned int kdev_t_to_nr(kdev_t dev) {
93         return (MAJOR(dev)<<8) | MINOR(dev);
94 }
95
96 static inline kdev_t to_kdev_t(int dev)
97 {
98         int major, minor;
99 #if 0
100         major = (dev >> 16);
101         if (!major) {
102                 major = (dev >> 8);
103                 minor = (dev & 0xff);
104         } else
105                 minor = (dev & 0xffff);
106 #else
107         major = (dev >> 8);
108         minor = (dev & 0xff);
109 #endif
110         return MKDEV(major, minor);
111 }
112
113 #else /* __KERNEL__ || _LVM_H_INCLUDE */
114
115 /*
116 Some programs want their definitions of MAJOR and MINOR and MKDEV
117 from the kernel sources. These must be the externally visible ones.
118 */
119 #define MAJOR(dev)      ((dev)>>8)
120 #define MINOR(dev)      ((dev) & 0xff)
121 #define MKDEV(ma,mi)    ((ma)<<8 | (mi))
122 #endif /* __KERNEL__ || _LVM_H_INCLUDE */
123 #endif