1 /*
2 *
3 * Copyright (c) 2003 The Regents of the University of California. All
4 * rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31
32 /*
33 * /proc/zero test program
34 *
35 * $Id: zero.c,v 1.9 2004-05-12 05:02:58 girod Exp $
36 */
37
38
39 #include <linux/config.h>
40 #include <linux/stddef.h>
41 #include <linux/version.h>
42 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
43 #include <linux/tqueue.h>
44 #endif
45 #include <linux/sched.h>
46 #include <linux/kernel.h>
47 #ifdef MODULE
48 #include <linux/module.h>
49 #endif
50 #include <linux/timer.h>
51 #include <linux/delay.h>
52 #include <linux/errno.h>
53 #include <linux/slab.h>
54 #include <linux/types.h>
55 #include <linux/fs.h>
56 #include <linux/poll.h>
57 #include <linux/devfs_fs_kernel.h>
58
59 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
60 # ifndef MODULE
61 # define MOD_INC_USE_COUNT
62 # define MOD_DEC_USE_COUNT
63 # endif
64 #endif
65
66 /************************************************************************/
67
68 static ssize_t zero_read(struct file *file,
69 char *user_buffer, /* The buffer to fill with data */
70 size_t user_length, /* The length of the buffer */
71 loff_t *offset) /* Our offset in the file */
72 {
73 int retval = 0;
74 char *buffer;
75 buffer = kmalloc(user_length, GFP_KERNEL);
76 if (buffer == NULL) {
77 return -ENOMEM;
78 }
79
80 memset(buffer, 0, user_length);
81
82 if (copy_to_user(user_buffer, buffer, user_length)) {
83 retval = -EFAULT;
84 goto out;
85 }
86
87 retval = user_length;
88
89 out:
90 kfree(buffer);
91 return retval;
92 }
93
94
95 static unsigned int zero_poll(struct file *file, poll_table *wait)
96 {
97 return POLLIN | POLLRDNORM;
98 }
99
100
101 static int zero_open(struct inode *inode, struct file *file)
102 {
103 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
104 MOD_INC_USE_COUNT;
105 #endif
106 return 0;
107 }
108
109
110 static int zero_release(struct inode *inode, struct file *file)
111 {
112 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
113 MOD_DEC_USE_COUNT;
114 #endif
115 return 0;
116 }
117
118
119 static struct file_operations zero_fops = {
120 owner: THIS_MODULE,
121 read: zero_read,
122 poll: zero_poll,
123 open: zero_open,
124 release: zero_release
125 };
126
127
128 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
129 static devfs_handle_t zero_handle = 0;
130 #else
131 static int devfs_major;
132 #endif /* VERSION < 2.6.0 */
133
134 int init_module(void)
135 {
136 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
137 zero_handle = devfs_register(NULL, "zero2",
138 DEVFS_FL_AUTO_DEVNUM,
139 0, 0,
140 S_IFCHR | S_IRUSR | S_IWUSR, //| 0666,
141 &zero_fops, NULL);
142
143 if (zero_handle == 0) {
144 printk("zero2: unable to register character device\n");
145 return -EIO;
146 }
147 #else
148 /* try to register the zero device */
149 if((devfs_major=register_chrdev(0, "zero2", &zero_fops)) < 0) {
150 printk("zero2: unable to register character device\n");
151 return -EIO;
152 }
153 /* register the zero device with devfs */
154 if (devfs_mk_cdev(MKDEV(devfs_major, 0),
155 S_IFCHR | S_IRUSR | S_IWUSR, "zero2") < 0) {
156 printk("zero2: unable to register zero2 devfs device\n");
157 unregister_chrdev(devfs_major, "zero2");
158 return -EIO;
159 }
160 #endif /* VERSION < 2.6.0 */
161 return 0;
162 }
163
164
165
166 void cleanup_module(void)
167 {
168 /* unregister the character device */
169 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
170 devfs_unregister(zero_handle);
171 #else
172 devfs_remove("zero2");
173 unregister_chrdev(devfs_major, "zero2");
174 #endif /* VERSION < 2.6.0 */
175 printk("done..\n");
176 }
177
178
179
180
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.