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