sched_clock: Disable seqlock lockdep usage in sched_clock()
[linux-2.6/btrfs-unstable.git] / drivers / staging / dgrp / dgrp_mon_ops.c
blobd18be4180e3bad40cad4c83557db85c78fb9e1ef
1 /*****************************************************************************
3 * Copyright 1999 Digi International (www.digi.com)
4 * James Puzzo <jamesp at digi dot com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
20 * Filename:
22 * dgrp_mon_ops.c
24 * Description:
26 * Handle the file operations required for the "monitor" devices.
27 * Includes those functions required to register the "mon" devices
28 * in "/proc".
30 * Author:
32 * James A. Puzzo
36 #include <linux/module.h>
37 #include <linux/tty.h>
38 #include <linux/sched.h>
39 #include <asm/unaligned.h>
40 #include <linux/slab.h>
41 #include <linux/proc_fs.h>
42 #include <linux/uaccess.h>
44 #include "dgrp_common.h"
46 /* File operation declarations */
47 static int dgrp_mon_open(struct inode *, struct file *);
48 static int dgrp_mon_release(struct inode *, struct file *);
49 static ssize_t dgrp_mon_read(struct file *, char __user *, size_t, loff_t *);
50 static long dgrp_mon_ioctl(struct file *file, unsigned int cmd,
51 unsigned long arg);
53 const struct file_operations dgrp_mon_ops = {
54 .owner = THIS_MODULE,
55 .read = dgrp_mon_read,
56 .unlocked_ioctl = dgrp_mon_ioctl,
57 .open = dgrp_mon_open,
58 .release = dgrp_mon_release,
61 /**
62 * dgrp_mon_open() -- open /proc/dgrp/ports device for a PortServer
63 * @inode: struct inode *
64 * @file: struct file *
66 * Open function to open the /proc/dgrp/ports device for a PortServer.
68 static int dgrp_mon_open(struct inode *inode, struct file *file)
70 struct nd_struct *nd;
71 struct timeval tv;
72 uint32_t time;
73 u8 *buf;
74 int rtn;
76 rtn = try_module_get(THIS_MODULE);
77 if (!rtn)
78 return -ENXIO;
80 rtn = 0;
82 if (!capable(CAP_SYS_ADMIN)) {
83 rtn = -EPERM;
84 goto done;
88 * Make sure that the "private_data" field hasn't already been used.
90 if (file->private_data) {
91 rtn = -EINVAL;
92 goto done;
96 * Get the node pointer, and fail if it doesn't exist.
98 nd = PDE_DATA(inode);
99 if (!nd) {
100 rtn = -ENXIO;
101 goto done;
104 file->private_data = (void *) nd;
107 * Allocate the monitor buffer.
111 * Grab the MON lock.
113 down(&nd->nd_mon_semaphore);
115 if (nd->nd_mon_buf) {
116 rtn = -EBUSY;
117 goto done_up;
120 nd->nd_mon_buf = kmalloc(MON_MAX, GFP_KERNEL);
122 if (!nd->nd_mon_buf) {
123 rtn = -ENOMEM;
124 goto done_up;
128 * Enter an RPDUMP file header into the buffer.
131 buf = nd->nd_mon_buf;
133 strcpy(buf, RPDUMP_MAGIC);
134 buf += strlen(buf) + 1;
136 do_gettimeofday(&tv);
139 * tv.tv_sec might be a 64 bit quantity. Pare
140 * it down to 32 bits before attempting to encode
141 * it.
143 time = (uint32_t) (tv.tv_sec & 0xffffffff);
145 put_unaligned_be32(time, buf);
146 put_unaligned_be16(0, buf + 4);
147 buf += 6;
149 if (nd->nd_tx_module) {
150 buf[0] = RPDUMP_CLIENT;
151 put_unaligned_be32(0, buf + 1);
152 put_unaligned_be16(1, buf + 5);
153 buf[7] = 0xf0 + nd->nd_tx_module;
154 buf += 8;
157 if (nd->nd_rx_module) {
158 buf[0] = RPDUMP_SERVER;
159 put_unaligned_be32(0, buf + 1);
160 put_unaligned_be16(1, buf + 5);
161 buf[7] = 0xf0 + nd->nd_rx_module;
162 buf += 8;
165 nd->nd_mon_out = 0;
166 nd->nd_mon_in = buf - nd->nd_mon_buf;
167 nd->nd_mon_lbolt = jiffies;
169 done_up:
170 up(&nd->nd_mon_semaphore);
172 done:
173 if (rtn)
174 module_put(THIS_MODULE);
175 return rtn;
180 * dgrp_mon_release() - Close the MON device for a particular PortServer
181 * @inode: struct inode *
182 * @file: struct file *
184 static int dgrp_mon_release(struct inode *inode, struct file *file)
186 struct nd_struct *nd;
189 * Get the node pointer, and quit if it doesn't exist.
191 nd = (struct nd_struct *)(file->private_data);
192 if (!nd)
193 goto done;
196 * Free the monitor buffer.
199 down(&nd->nd_mon_semaphore);
201 kfree(nd->nd_mon_buf);
202 nd->nd_mon_buf = NULL;
203 nd->nd_mon_out = nd->nd_mon_in;
206 * Wakeup any thread waiting for buffer space.
209 if (nd->nd_mon_flag & MON_WAIT_SPACE) {
210 nd->nd_mon_flag &= ~MON_WAIT_SPACE;
211 wake_up_interruptible(&nd->nd_mon_wqueue);
214 up(&nd->nd_mon_semaphore);
217 * Make sure there is no thread in the middle of writing a packet.
219 down(&nd->nd_net_semaphore);
220 up(&nd->nd_net_semaphore);
222 done:
223 module_put(THIS_MODULE);
224 file->private_data = NULL;
225 return 0;
229 * dgrp_mon_read() -- Copy data from the monitoring buffer to the user
231 static ssize_t dgrp_mon_read(struct file *file, char __user *buf, size_t count,
232 loff_t *ppos)
234 struct nd_struct *nd;
235 int r;
236 int offset = 0;
237 int res = 0;
238 ssize_t rtn;
241 * Get the node pointer, and quit if it doesn't exist.
243 nd = (struct nd_struct *)(file->private_data);
244 if (!nd)
245 return -ENXIO;
248 * Wait for some data to appear in the buffer.
251 down(&nd->nd_mon_semaphore);
253 for (;;) {
254 res = (nd->nd_mon_in - nd->nd_mon_out) & MON_MASK;
256 if (res)
257 break;
259 nd->nd_mon_flag |= MON_WAIT_DATA;
261 up(&nd->nd_mon_semaphore);
264 * Go to sleep waiting until the condition becomes true.
266 rtn = wait_event_interruptible(nd->nd_mon_wqueue,
267 ((nd->nd_mon_flag & MON_WAIT_DATA) == 0));
269 if (rtn)
270 return rtn;
272 down(&nd->nd_mon_semaphore);
276 * Read whatever is there.
279 if (res > count)
280 res = count;
282 r = MON_MAX - nd->nd_mon_out;
284 if (r <= res) {
285 rtn = copy_to_user((void __user *)buf,
286 nd->nd_mon_buf + nd->nd_mon_out, r);
287 if (rtn) {
288 up(&nd->nd_mon_semaphore);
289 return -EFAULT;
292 nd->nd_mon_out = 0;
293 res -= r;
294 offset = r;
297 rtn = copy_to_user((void __user *) buf + offset,
298 nd->nd_mon_buf + nd->nd_mon_out, res);
299 if (rtn) {
300 up(&nd->nd_mon_semaphore);
301 return -EFAULT;
304 nd->nd_mon_out += res;
306 *ppos += res;
308 up(&nd->nd_mon_semaphore);
311 * Wakeup any thread waiting for buffer space.
314 if (nd->nd_mon_flag & MON_WAIT_SPACE) {
315 nd->nd_mon_flag &= ~MON_WAIT_SPACE;
316 wake_up_interruptible(&nd->nd_mon_wqueue);
319 return res;
322 /* ioctl is not valid on monitor device */
323 static long dgrp_mon_ioctl(struct file *file, unsigned int cmd,
324 unsigned long arg)
326 return -EINVAL;