]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/dgrp/dgrp_mon_ops.c
staging: Add dgrp driver for Digi Realport devices
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / dgrp / dgrp_mon_ops.c
CommitLineData
0b52b749
BP
1/*****************************************************************************
2 *
3 * Copyright 1999 Digi International (www.digi.com)
4 * James Puzzo <jamesp at digi dot com>
5 *
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.
10 *
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.
15 *
16 */
17
18/*
19 *
20 * Filename:
21 *
22 * dgrp_mon_ops.c
23 *
24 * Description:
25 *
26 * Handle the file operations required for the "monitor" devices.
27 * Includes those functions required to register the "mon" devices
28 * in "/proc".
29 *
30 * Author:
31 *
32 * James A. Puzzo
33 *
34 */
35
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
42#include "dgrp_common.h"
43
44/* File operation declarations */
45static int dgrp_mon_open(struct inode *, struct file *);
46static int dgrp_mon_release(struct inode *, struct file *);
47static ssize_t dgrp_mon_read(struct file *, char __user *, size_t, loff_t *);
48static long dgrp_mon_ioctl(struct file *file, unsigned int cmd,
49 unsigned long arg);
50
51static const struct file_operations mon_ops = {
52 .owner = THIS_MODULE,
53 .read = dgrp_mon_read,
54 .unlocked_ioctl = dgrp_mon_ioctl,
55 .open = dgrp_mon_open,
56 .release = dgrp_mon_release,
57};
58
59static struct inode_operations mon_inode_ops = {
60 .permission = dgrp_inode_permission
61};
62
63void dgrp_register_mon_hook(struct proc_dir_entry *de)
64{
65 struct nd_struct *node = de->data;
66
67 de->proc_iops = &mon_inode_ops;
68 de->proc_fops = &mon_ops;
69 node->nd_mon_de = de;
70 sema_init(&node->nd_mon_semaphore, 1);
71}
72
73/**
74 * dgrp_mon_open() -- open /proc/dgrp/ports device for a PortServer
75 * @inode: struct inode *
76 * @file: struct file *
77 *
78 * Open function to open the /proc/dgrp/ports device for a PortServer.
79 */
80static int dgrp_mon_open(struct inode *inode, struct file *file)
81{
82 struct nd_struct *nd;
83 struct proc_dir_entry *de;
84 struct timeval tv;
85 uint32_t time;
86 u8 *buf;
87 int rtn;
88
89 rtn = try_module_get(THIS_MODULE);
90 if (!rtn)
91 return -ENXIO;
92
93 rtn = 0;
94
95 if (!capable(CAP_SYS_ADMIN)) {
96 rtn = -EPERM;
97 goto done;
98 }
99
100 /*
101 * Make sure that the "private_data" field hasn't already been used.
102 */
103 if (file->private_data) {
104 rtn = -EINVAL;
105 goto done;
106 }
107
108 /*
109 * Get the node pointer, and fail if it doesn't exist.
110 */
111 de = PDE(inode);
112 if (!de) {
113 rtn = -ENXIO;
114 goto done;
115 }
116
117 nd = (struct nd_struct *)de->data;
118 if (!nd) {
119 rtn = -ENXIO;
120 goto done;
121 }
122
123 file->private_data = (void *) nd;
124
125 /*
126 * Allocate the monitor buffer.
127 */
128
129 /*
130 * Grab the MON lock.
131 */
132 down(&nd->nd_mon_semaphore);
133
134 if (nd->nd_mon_buf) {
135 rtn = -EBUSY;
136 goto done_up;
137 }
138
139 nd->nd_mon_buf = kmalloc(MON_MAX, GFP_KERNEL);
140
141 if (!nd->nd_mon_buf) {
142 rtn = -ENOMEM;
143 goto done_up;
144 }
145
146 /*
147 * Enter an RPDUMP file header into the buffer.
148 */
149
150 buf = nd->nd_mon_buf;
151
152 strcpy(buf, RPDUMP_MAGIC);
153 buf += strlen(buf) + 1;
154
155 do_gettimeofday(&tv);
156
157 /*
158 * tv.tv_sec might be a 64 bit quantity. Pare
159 * it down to 32 bits before attempting to encode
160 * it.
161 */
162 time = (uint32_t) (tv.tv_sec & 0xffffffff);
163
164 put_unaligned_be32(time, buf);
165 put_unaligned_be16(0, buf + 4);
166 buf += 6;
167
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;
173 buf += 8;
174 }
175
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;
181 buf += 8;
182 }
183
184 nd->nd_mon_out = 0;
185 nd->nd_mon_in = buf - nd->nd_mon_buf;
186 nd->nd_mon_lbolt = jiffies;
187
188done_up:
189 up(&nd->nd_mon_semaphore);
190
191done:
192 if (rtn)
193 module_put(THIS_MODULE);
194 return rtn;
195}
196
197
198/**
199 * dgrp_mon_release() - Close the MON device for a particular PortServer
200 * @inode: struct inode *
201 * @file: struct file *
202 */
203static int dgrp_mon_release(struct inode *inode, struct file *file)
204{
205 struct nd_struct *nd;
206
207 /*
208 * Get the node pointer, and quit if it doesn't exist.
209 */
210 nd = (struct nd_struct *)(file->private_data);
211 if (!nd)
212 goto done;
213
214 /*
215 * Free the monitor buffer.
216 */
217
218 down(&nd->nd_mon_semaphore);
219
220 kfree(nd->nd_mon_buf);
221 nd->nd_mon_buf = NULL;
222 nd->nd_mon_out = nd->nd_mon_in;
223
224 /*
225 * Wakeup any thread waiting for buffer space.
226 */
227
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);
231 }
232
233 up(&nd->nd_mon_semaphore);
234
235 /*
236 * Make sure there is no thread in the middle of writing a packet.
237 */
238 down(&nd->nd_net_semaphore);
239 up(&nd->nd_net_semaphore);
240
241done:
242 module_put(THIS_MODULE);
243 file->private_data = NULL;
244 return 0;
245}
246
247/**
248 * dgrp_mon_read() -- Copy data from the monitoring buffer to the user
249 */
250static ssize_t dgrp_mon_read(struct file *file, char __user *buf, size_t count,
251 loff_t *ppos)
252{
253 struct nd_struct *nd;
254 int r;
255 int offset = 0;
256 int res = 0;
257 ssize_t rtn;
258
259 /*
260 * Get the node pointer, and quit if it doesn't exist.
261 */
262 nd = (struct nd_struct *)(file->private_data);
263 if (!nd)
264 return -ENXIO;
265
266 /*
267 * Wait for some data to appear in the buffer.
268 */
269
270 down(&nd->nd_mon_semaphore);
271
272 for (;;) {
273 res = (nd->nd_mon_in - nd->nd_mon_out) & MON_MASK;
274
275 if (res)
276 break;
277
278 nd->nd_mon_flag |= MON_WAIT_DATA;
279
280 up(&nd->nd_mon_semaphore);
281
282 /*
283 * Go to sleep waiting until the condition becomes true.
284 */
285 rtn = wait_event_interruptible(nd->nd_mon_wqueue,
286 ((nd->nd_mon_flag & MON_WAIT_DATA) == 0));
287
288 if (rtn)
289 return rtn;
290
291 down(&nd->nd_mon_semaphore);
292 }
293
294 /*
295 * Read whatever is there.
296 */
297
298 if (res > count)
299 res = count;
300
301 r = MON_MAX - nd->nd_mon_out;
302
303 if (r <= res) {
304 rtn = copy_to_user((void __user *)buf,
305 nd->nd_mon_buf + nd->nd_mon_out, r);
306 if (rtn) {
307 up(&nd->nd_mon_semaphore);
308 return -EFAULT;
309 }
310
311 nd->nd_mon_out = 0;
312 res -= r;
313 offset = r;
314 }
315
316 rtn = copy_to_user((void __user *) buf + offset,
317 nd->nd_mon_buf + nd->nd_mon_out, res);
318 if (rtn) {
319 up(&nd->nd_mon_semaphore);
320 return -EFAULT;
321 }
322
323 nd->nd_mon_out += res;
324
325 *ppos += res;
326
327 up(&nd->nd_mon_semaphore);
328
329 /*
330 * Wakeup any thread waiting for buffer space.
331 */
332
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);
336 }
337
338 return res;
339}
340
341/* ioctl is not valid on monitor device */
342static long dgrp_mon_ioctl(struct file *file, unsigned int cmd,
343 unsigned long arg)
344{
345 return -EINVAL;
346}