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)
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.
26 * Handle the file operations required for the "monitor" devices.
27 * Includes those functions required to register the "mon" devices
36 #include <linux/module.h>
37 #include <linux/tty.h>
38 #include <linux/sched.h>
39 #include <asm/unaligned.h>
40 #include <linux/proc_fs.h>
42 #include "dgrp_common.h"
44 /* File operation declarations */
45 static int dgrp_mon_open(struct inode
*, struct file
*);
46 static int dgrp_mon_release(struct inode
*, struct file
*);
47 static ssize_t
dgrp_mon_read(struct file
*, char __user
*, size_t, loff_t
*);
48 static long dgrp_mon_ioctl(struct file
*file
, unsigned int cmd
,
51 static const struct file_operations mon_ops
= {
53 .read
= dgrp_mon_read
,
54 .unlocked_ioctl
= dgrp_mon_ioctl
,
55 .open
= dgrp_mon_open
,
56 .release
= dgrp_mon_release
,
59 static struct inode_operations mon_inode_ops
= {
60 .permission
= dgrp_inode_permission
63 void dgrp_register_mon_hook(struct proc_dir_entry
*de
)
65 struct nd_struct
*node
= de
->data
;
67 de
->proc_iops
= &mon_inode_ops
;
68 de
->proc_fops
= &mon_ops
;
70 sema_init(&node
->nd_mon_semaphore
, 1);
74 * dgrp_mon_open() -- open /proc/dgrp/ports device for a PortServer
75 * @inode: struct inode *
76 * @file: struct file *
78 * Open function to open the /proc/dgrp/ports device for a PortServer.
80 static int dgrp_mon_open(struct inode
*inode
, struct file
*file
)
83 struct proc_dir_entry
*de
;
89 rtn
= try_module_get(THIS_MODULE
);
95 if (!capable(CAP_SYS_ADMIN
)) {
101 * Make sure that the "private_data" field hasn't already been used.
103 if (file
->private_data
) {
109 * Get the node pointer, and fail if it doesn't exist.
117 nd
= (struct nd_struct
*)de
->data
;
123 file
->private_data
= (void *) nd
;
126 * Allocate the monitor buffer.
132 down(&nd
->nd_mon_semaphore
);
134 if (nd
->nd_mon_buf
) {
139 nd
->nd_mon_buf
= kmalloc(MON_MAX
, GFP_KERNEL
);
141 if (!nd
->nd_mon_buf
) {
147 * Enter an RPDUMP file header into the buffer.
150 buf
= nd
->nd_mon_buf
;
152 strcpy(buf
, RPDUMP_MAGIC
);
153 buf
+= strlen(buf
) + 1;
155 do_gettimeofday(&tv
);
158 * tv.tv_sec might be a 64 bit quantity. Pare
159 * it down to 32 bits before attempting to encode
162 time
= (uint32_t) (tv
.tv_sec
& 0xffffffff);
164 put_unaligned_be32(time
, buf
);
165 put_unaligned_be16(0, buf
+ 4);
168 if (nd
->nd_tx_module
) {
169 buf
[0] = RPDUMP_CLIENT
;
170 put_unaligned_be32(0, buf
+ 1);
171 put_unaligned_be16(1, buf
+ 5);
172 buf
[7] = 0xf0 + nd
->nd_tx_module
;
176 if (nd
->nd_rx_module
) {
177 buf
[0] = RPDUMP_SERVER
;
178 put_unaligned_be32(0, buf
+ 1);
179 put_unaligned_be16(1, buf
+ 5);
180 buf
[7] = 0xf0 + nd
->nd_rx_module
;
185 nd
->nd_mon_in
= buf
- nd
->nd_mon_buf
;
186 nd
->nd_mon_lbolt
= jiffies
;
189 up(&nd
->nd_mon_semaphore
);
193 module_put(THIS_MODULE
);
199 * dgrp_mon_release() - Close the MON device for a particular PortServer
200 * @inode: struct inode *
201 * @file: struct file *
203 static int dgrp_mon_release(struct inode
*inode
, struct file
*file
)
205 struct nd_struct
*nd
;
208 * Get the node pointer, and quit if it doesn't exist.
210 nd
= (struct nd_struct
*)(file
->private_data
);
215 * Free the monitor buffer.
218 down(&nd
->nd_mon_semaphore
);
220 kfree(nd
->nd_mon_buf
);
221 nd
->nd_mon_buf
= NULL
;
222 nd
->nd_mon_out
= nd
->nd_mon_in
;
225 * Wakeup any thread waiting for buffer space.
228 if (nd
->nd_mon_flag
& MON_WAIT_SPACE
) {
229 nd
->nd_mon_flag
&= ~MON_WAIT_SPACE
;
230 wake_up_interruptible(&nd
->nd_mon_wqueue
);
233 up(&nd
->nd_mon_semaphore
);
236 * Make sure there is no thread in the middle of writing a packet.
238 down(&nd
->nd_net_semaphore
);
239 up(&nd
->nd_net_semaphore
);
242 module_put(THIS_MODULE
);
243 file
->private_data
= NULL
;
248 * dgrp_mon_read() -- Copy data from the monitoring buffer to the user
250 static ssize_t
dgrp_mon_read(struct file
*file
, char __user
*buf
, size_t count
,
253 struct nd_struct
*nd
;
260 * Get the node pointer, and quit if it doesn't exist.
262 nd
= (struct nd_struct
*)(file
->private_data
);
267 * Wait for some data to appear in the buffer.
270 down(&nd
->nd_mon_semaphore
);
273 res
= (nd
->nd_mon_in
- nd
->nd_mon_out
) & MON_MASK
;
278 nd
->nd_mon_flag
|= MON_WAIT_DATA
;
280 up(&nd
->nd_mon_semaphore
);
283 * Go to sleep waiting until the condition becomes true.
285 rtn
= wait_event_interruptible(nd
->nd_mon_wqueue
,
286 ((nd
->nd_mon_flag
& MON_WAIT_DATA
) == 0));
291 down(&nd
->nd_mon_semaphore
);
295 * Read whatever is there.
301 r
= MON_MAX
- nd
->nd_mon_out
;
304 rtn
= copy_to_user((void __user
*)buf
,
305 nd
->nd_mon_buf
+ nd
->nd_mon_out
, r
);
307 up(&nd
->nd_mon_semaphore
);
316 rtn
= copy_to_user((void __user
*) buf
+ offset
,
317 nd
->nd_mon_buf
+ nd
->nd_mon_out
, res
);
319 up(&nd
->nd_mon_semaphore
);
323 nd
->nd_mon_out
+= res
;
327 up(&nd
->nd_mon_semaphore
);
330 * Wakeup any thread waiting for buffer space.
333 if (nd
->nd_mon_flag
& MON_WAIT_SPACE
) {
334 nd
->nd_mon_flag
&= ~MON_WAIT_SPACE
;
335 wake_up_interruptible(&nd
->nd_mon_wqueue
);
341 /* ioctl is not valid on monitor device */
342 static long dgrp_mon_ioctl(struct file
*file
, unsigned int cmd
,