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>
41 #include <linux/uaccess.h>
43 #include "dgrp_common.h"
45 /* File operation declarations */
46 static int dgrp_mon_open(struct inode
*, struct file
*);
47 static int dgrp_mon_release(struct inode
*, struct file
*);
48 static ssize_t
dgrp_mon_read(struct file
*, char __user
*, size_t, loff_t
*);
49 static long dgrp_mon_ioctl(struct file
*file
, unsigned int cmd
,
52 const struct file_operations dgrp_mon_ops
= {
54 .read
= dgrp_mon_read
,
55 .unlocked_ioctl
= dgrp_mon_ioctl
,
56 .open
= dgrp_mon_open
,
57 .release
= dgrp_mon_release
,
61 * dgrp_mon_open() -- open /proc/dgrp/ports device for a PortServer
62 * @inode: struct inode *
63 * @file: struct file *
65 * Open function to open the /proc/dgrp/ports device for a PortServer.
67 static int dgrp_mon_open(struct inode
*inode
, struct file
*file
)
75 rtn
= try_module_get(THIS_MODULE
);
81 if (!capable(CAP_SYS_ADMIN
)) {
87 * Make sure that the "private_data" field hasn't already been used.
89 if (file
->private_data
) {
95 * Get the node pointer, and fail if it doesn't exist.
103 file
->private_data
= (void *) nd
;
106 * Allocate the monitor buffer.
112 down(&nd
->nd_mon_semaphore
);
114 if (nd
->nd_mon_buf
) {
119 nd
->nd_mon_buf
= kmalloc(MON_MAX
, GFP_KERNEL
);
121 if (!nd
->nd_mon_buf
) {
127 * Enter an RPDUMP file header into the buffer.
130 buf
= nd
->nd_mon_buf
;
132 strcpy(buf
, RPDUMP_MAGIC
);
133 buf
+= strlen(buf
) + 1;
135 do_gettimeofday(&tv
);
138 * tv.tv_sec might be a 64 bit quantity. Pare
139 * it down to 32 bits before attempting to encode
142 time
= (uint32_t) (tv
.tv_sec
& 0xffffffff);
144 put_unaligned_be32(time
, buf
);
145 put_unaligned_be16(0, buf
+ 4);
148 if (nd
->nd_tx_module
) {
149 buf
[0] = RPDUMP_CLIENT
;
150 put_unaligned_be32(0, buf
+ 1);
151 put_unaligned_be16(1, buf
+ 5);
152 buf
[7] = 0xf0 + nd
->nd_tx_module
;
156 if (nd
->nd_rx_module
) {
157 buf
[0] = RPDUMP_SERVER
;
158 put_unaligned_be32(0, buf
+ 1);
159 put_unaligned_be16(1, buf
+ 5);
160 buf
[7] = 0xf0 + nd
->nd_rx_module
;
165 nd
->nd_mon_in
= buf
- nd
->nd_mon_buf
;
166 nd
->nd_mon_lbolt
= jiffies
;
169 up(&nd
->nd_mon_semaphore
);
173 module_put(THIS_MODULE
);
179 * dgrp_mon_release() - Close the MON device for a particular PortServer
180 * @inode: struct inode *
181 * @file: struct file *
183 static int dgrp_mon_release(struct inode
*inode
, struct file
*file
)
185 struct nd_struct
*nd
;
188 * Get the node pointer, and quit if it doesn't exist.
190 nd
= (struct nd_struct
*)(file
->private_data
);
195 * Free the monitor buffer.
198 down(&nd
->nd_mon_semaphore
);
200 kfree(nd
->nd_mon_buf
);
201 nd
->nd_mon_buf
= NULL
;
202 nd
->nd_mon_out
= nd
->nd_mon_in
;
205 * Wakeup any thread waiting for buffer space.
208 if (nd
->nd_mon_flag
& MON_WAIT_SPACE
) {
209 nd
->nd_mon_flag
&= ~MON_WAIT_SPACE
;
210 wake_up_interruptible(&nd
->nd_mon_wqueue
);
213 up(&nd
->nd_mon_semaphore
);
216 * Make sure there is no thread in the middle of writing a packet.
218 down(&nd
->nd_net_semaphore
);
219 up(&nd
->nd_net_semaphore
);
222 module_put(THIS_MODULE
);
223 file
->private_data
= NULL
;
228 * dgrp_mon_read() -- Copy data from the monitoring buffer to the user
230 static ssize_t
dgrp_mon_read(struct file
*file
, char __user
*buf
, size_t count
,
233 struct nd_struct
*nd
;
240 * Get the node pointer, and quit if it doesn't exist.
242 nd
= (struct nd_struct
*)(file
->private_data
);
247 * Wait for some data to appear in the buffer.
250 down(&nd
->nd_mon_semaphore
);
253 res
= (nd
->nd_mon_in
- nd
->nd_mon_out
) & MON_MASK
;
258 nd
->nd_mon_flag
|= MON_WAIT_DATA
;
260 up(&nd
->nd_mon_semaphore
);
263 * Go to sleep waiting until the condition becomes true.
265 rtn
= wait_event_interruptible(nd
->nd_mon_wqueue
,
266 ((nd
->nd_mon_flag
& MON_WAIT_DATA
) == 0));
271 down(&nd
->nd_mon_semaphore
);
275 * Read whatever is there.
281 r
= MON_MAX
- nd
->nd_mon_out
;
284 rtn
= copy_to_user((void __user
*)buf
,
285 nd
->nd_mon_buf
+ nd
->nd_mon_out
, r
);
287 up(&nd
->nd_mon_semaphore
);
296 rtn
= copy_to_user((void __user
*) buf
+ offset
,
297 nd
->nd_mon_buf
+ nd
->nd_mon_out
, res
);
299 up(&nd
->nd_mon_semaphore
);
303 nd
->nd_mon_out
+= res
;
307 up(&nd
->nd_mon_semaphore
);
310 * Wakeup any thread waiting for buffer space.
313 if (nd
->nd_mon_flag
& MON_WAIT_SPACE
) {
314 nd
->nd_mon_flag
&= ~MON_WAIT_SPACE
;
315 wake_up_interruptible(&nd
->nd_mon_wqueue
);
321 /* ioctl is not valid on monitor device */
322 static long dgrp_mon_ioctl(struct file
*file
, unsigned int cmd
,