Include missing linux/slab.h inclusions
[linux-2.6.git] / drivers / staging / dgrp / dgrp_dpa_ops.c
blob1fc13de4bdd699a6a38dec3146d8ee749395da62
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_dpa_ops.c
24 * Description:
26 * Handle the file operations required for the "dpa" devices.
27 * Includes those functions required to register the "dpa" devices
28 * in "/proc".
30 * Author:
32 * James A. Puzzo
36 #include <linux/module.h>
37 #include <linux/proc_fs.h>
38 #include <linux/tty.h>
39 #include <linux/poll.h>
40 #include <linux/cred.h>
41 #include <linux/sched.h>
42 #include <linux/ratelimit.h>
43 #include <linux/slab.h>
44 #include <asm/unaligned.h>
46 #include "dgrp_common.h"
48 /* File operation declarations */
49 static int dgrp_dpa_open(struct inode *, struct file *);
50 static int dgrp_dpa_release(struct inode *, struct file *);
51 static ssize_t dgrp_dpa_read(struct file *, char __user *, size_t, loff_t *);
52 static long dgrp_dpa_ioctl(struct file *file, unsigned int cmd,
53 unsigned long arg);
54 static unsigned int dgrp_dpa_select(struct file *, struct poll_table_struct *);
56 const struct file_operations dgrp_dpa_ops = {
57 .owner = THIS_MODULE,
58 .read = dgrp_dpa_read,
59 .poll = dgrp_dpa_select,
60 .unlocked_ioctl = dgrp_dpa_ioctl,
61 .open = dgrp_dpa_open,
62 .release = dgrp_dpa_release,
65 struct digi_node {
66 uint nd_state; /* Node state: 1 = up, 0 = down. */
67 uint nd_chan_count; /* Number of channels found */
68 uint nd_tx_byte; /* Tx data count */
69 uint nd_rx_byte; /* RX data count */
70 u8 nd_ps_desc[MAX_DESC_LEN]; /* Description from PS */
73 #define DIGI_GETNODE (('d'<<8) | 249) /* get board info */
76 struct digi_chan {
77 uint ch_port; /* Port number to get info on */
78 uint ch_open; /* 1 if open, 0 if not */
79 uint ch_txcount; /* TX data count */
80 uint ch_rxcount; /* RX data count */
81 uint ch_s_brate; /* Realport BRATE */
82 uint ch_s_estat; /* Realport ELAST */
83 uint ch_s_cflag; /* Realport CFLAG */
84 uint ch_s_iflag; /* Realport IFLAG */
85 uint ch_s_oflag; /* Realport OFLAG */
86 uint ch_s_xflag; /* Realport XFLAG */
87 uint ch_s_mstat; /* Realport MLAST */
90 #define DIGI_GETCHAN (('d'<<8) | 248) /* get channel info */
93 struct digi_vpd {
94 int vpd_len;
95 char vpd_data[VPDSIZE];
98 #define DIGI_GETVPD (('d'<<8) | 246) /* get VPD info */
101 struct digi_debug {
102 int onoff;
103 int port;
106 #define DIGI_SETDEBUG (('d'<<8) | 247) /* set debug info */
110 * dgrp_dpa_open -- open the DPA device for a particular PortServer
112 static int dgrp_dpa_open(struct inode *inode, struct file *file)
114 struct nd_struct *nd;
115 int rtn = 0;
117 rtn = try_module_get(THIS_MODULE);
118 if (!rtn)
119 return -ENXIO;
121 rtn = 0;
123 if (!capable(CAP_SYS_ADMIN)) {
124 rtn = -EPERM;
125 goto done;
129 * Make sure that the "private_data" field hasn't already been used.
131 if (file->private_data) {
132 rtn = -EINVAL;
133 goto done;
137 * Get the node pointer, and fail if it doesn't exist.
139 nd = PDE_DATA(inode);
140 if (!nd) {
141 rtn = -ENXIO;
142 goto done;
145 file->private_data = (void *) nd;
148 * Allocate the DPA buffer.
151 if (nd->nd_dpa_buf) {
152 rtn = -EBUSY;
153 } else {
154 nd->nd_dpa_buf = kmalloc(DPA_MAX, GFP_KERNEL);
156 if (!nd->nd_dpa_buf) {
157 rtn = -ENOMEM;
158 } else {
159 nd->nd_dpa_out = 0;
160 nd->nd_dpa_in = 0;
161 nd->nd_dpa_lbolt = jiffies;
165 done:
167 if (rtn)
168 module_put(THIS_MODULE);
169 return rtn;
173 * dgrp_dpa_release -- close the DPA device for a particular PortServer
175 static int dgrp_dpa_release(struct inode *inode, struct file *file)
177 struct nd_struct *nd;
178 u8 *buf;
179 unsigned long lock_flags;
182 * Get the node pointer, and quit if it doesn't exist.
184 nd = (struct nd_struct *)(file->private_data);
185 if (!nd)
186 goto done;
189 * Free the dpa buffer.
192 spin_lock_irqsave(&nd->nd_dpa_lock, lock_flags);
194 buf = nd->nd_dpa_buf;
196 nd->nd_dpa_buf = NULL;
197 nd->nd_dpa_out = nd->nd_dpa_in;
200 * Wakeup any thread waiting for buffer space.
203 if (nd->nd_dpa_flag & DPA_WAIT_SPACE) {
204 nd->nd_dpa_flag &= ~DPA_WAIT_SPACE;
205 wake_up_interruptible(&nd->nd_dpa_wqueue);
208 spin_unlock_irqrestore(&nd->nd_dpa_lock, lock_flags);
210 kfree(buf);
212 done:
213 module_put(THIS_MODULE);
214 file->private_data = NULL;
215 return 0;
219 * dgrp_dpa_read
221 * Copy data from the monitoring buffer to the user, freeing space
222 * in the monitoring buffer for more messages
224 static ssize_t dgrp_dpa_read(struct file *file, char __user *buf, size_t count,
225 loff_t *ppos)
227 struct nd_struct *nd;
228 int n;
229 int r;
230 int offset = 0;
231 int res = 0;
232 ssize_t rtn;
233 unsigned long lock_flags;
236 * Get the node pointer, and quit if it doesn't exist.
238 nd = (struct nd_struct *)(file->private_data);
239 if (!nd)
240 return -ENXIO;
243 * Wait for some data to appear in the buffer.
246 spin_lock_irqsave(&nd->nd_dpa_lock, lock_flags);
248 for (;;) {
249 n = (nd->nd_dpa_in - nd->nd_dpa_out) & DPA_MASK;
251 if (n != 0)
252 break;
254 nd->nd_dpa_flag |= DPA_WAIT_DATA;
256 spin_unlock_irqrestore(&nd->nd_dpa_lock, lock_flags);
259 * Go to sleep waiting until the condition becomes true.
261 rtn = wait_event_interruptible(nd->nd_dpa_wqueue,
262 ((nd->nd_dpa_flag & DPA_WAIT_DATA) == 0));
264 if (rtn)
265 return rtn;
267 spin_lock_irqsave(&nd->nd_dpa_lock, lock_flags);
271 * Read whatever is there.
274 if (n > count)
275 n = count;
277 res = n;
279 r = DPA_MAX - nd->nd_dpa_out;
281 if (r <= n) {
283 spin_unlock_irqrestore(&nd->nd_dpa_lock, lock_flags);
284 rtn = copy_to_user((void __user *)buf,
285 nd->nd_dpa_buf + nd->nd_dpa_out, r);
286 spin_lock_irqsave(&nd->nd_dpa_lock, lock_flags);
288 if (rtn) {
289 rtn = -EFAULT;
290 goto done;
293 nd->nd_dpa_out = 0;
294 n -= r;
295 offset = r;
298 spin_unlock_irqrestore(&nd->nd_dpa_lock, lock_flags);
299 rtn = copy_to_user((void __user *)buf + offset,
300 nd->nd_dpa_buf + nd->nd_dpa_out, n);
301 spin_lock_irqsave(&nd->nd_dpa_lock, lock_flags);
303 if (rtn) {
304 rtn = -EFAULT;
305 goto done;
308 nd->nd_dpa_out += n;
310 *ppos += res;
312 rtn = res;
315 * Wakeup any thread waiting for buffer space.
318 n = (nd->nd_dpa_in - nd->nd_dpa_out) & DPA_MASK;
320 if (nd->nd_dpa_flag & DPA_WAIT_SPACE &&
321 (DPA_MAX - n) > DPA_HIGH_WATER) {
322 nd->nd_dpa_flag &= ~DPA_WAIT_SPACE;
323 wake_up_interruptible(&nd->nd_dpa_wqueue);
326 done:
327 spin_unlock_irqrestore(&nd->nd_dpa_lock, lock_flags);
328 return rtn;
331 static unsigned int dgrp_dpa_select(struct file *file,
332 struct poll_table_struct *table)
334 unsigned int retval = 0;
335 struct nd_struct *nd = file->private_data;
337 if (nd->nd_dpa_out != nd->nd_dpa_in)
338 retval |= POLLIN | POLLRDNORM; /* Conditionally readable */
340 retval |= POLLOUT | POLLWRNORM; /* Always writeable */
342 return retval;
345 static long dgrp_dpa_ioctl(struct file *file, unsigned int cmd,
346 unsigned long arg)
349 struct nd_struct *nd;
350 struct digi_chan getchan;
351 struct digi_node getnode;
352 struct ch_struct *ch;
353 struct digi_debug setdebug;
354 struct digi_vpd vpd;
355 unsigned int port;
356 void __user *uarg = (void __user *) arg;
358 nd = file->private_data;
360 switch (cmd) {
361 case DIGI_GETCHAN:
362 if (copy_from_user(&getchan, uarg, sizeof(struct digi_chan)))
363 return -EFAULT;
365 port = getchan.ch_port;
367 if (port > nd->nd_chan_count)
368 return -EINVAL;
370 ch = nd->nd_chan + port;
372 getchan.ch_open = (ch->ch_open_count > 0) ? 1 : 0;
373 getchan.ch_txcount = ch->ch_txcount;
374 getchan.ch_rxcount = ch->ch_rxcount;
375 getchan.ch_s_brate = ch->ch_s_brate;
376 getchan.ch_s_estat = ch->ch_s_elast;
377 getchan.ch_s_cflag = ch->ch_s_cflag;
378 getchan.ch_s_iflag = ch->ch_s_iflag;
379 getchan.ch_s_oflag = ch->ch_s_oflag;
380 getchan.ch_s_xflag = ch->ch_s_xflag;
381 getchan.ch_s_mstat = ch->ch_s_mlast;
383 if (copy_to_user(uarg, &getchan, sizeof(struct digi_chan)))
384 return -EFAULT;
385 break;
388 case DIGI_GETNODE:
389 getnode.nd_state = (nd->nd_state & NS_READY) ? 1 : 0;
390 getnode.nd_chan_count = nd->nd_chan_count;
391 getnode.nd_tx_byte = nd->nd_tx_byte;
392 getnode.nd_rx_byte = nd->nd_rx_byte;
394 memset(&getnode.nd_ps_desc, 0, MAX_DESC_LEN);
395 strncpy(getnode.nd_ps_desc, nd->nd_ps_desc, MAX_DESC_LEN);
397 if (copy_to_user(uarg, &getnode, sizeof(struct digi_node)))
398 return -EFAULT;
399 break;
402 case DIGI_SETDEBUG:
403 if (copy_from_user(&setdebug, uarg, sizeof(struct digi_debug)))
404 return -EFAULT;
406 nd->nd_dpa_debug = setdebug.onoff;
407 nd->nd_dpa_port = setdebug.port;
408 break;
411 case DIGI_GETVPD:
412 if (nd->nd_vpd_len > 0) {
413 vpd.vpd_len = nd->nd_vpd_len;
414 memcpy(&vpd.vpd_data, &nd->nd_vpd, nd->nd_vpd_len);
415 } else {
416 vpd.vpd_len = 0;
419 if (copy_to_user(uarg, &vpd, sizeof(struct digi_vpd)))
420 return -EFAULT;
421 break;
424 return 0;
428 * dgrp_dpa() -- send data to the device monitor queue
429 * @nd: pointer to a node structure
430 * @buf: buffer of data to copy to the monitoring buffer
431 * @len: number of bytes to transfer to the buffer
433 * Called by the net device routines to send data to the device
434 * monitor queue. If the device monitor buffer is too full to
435 * accept the data, it waits until the buffer is ready.
437 static void dgrp_dpa(struct nd_struct *nd, u8 *buf, int nbuf)
439 int n;
440 int r;
441 unsigned long lock_flags;
444 * Grab DPA lock.
446 spin_lock_irqsave(&nd->nd_dpa_lock, lock_flags);
449 * Loop while data remains.
451 while (nbuf > 0 && nd->nd_dpa_buf != NULL) {
453 n = (nd->nd_dpa_out - nd->nd_dpa_in - 1) & DPA_MASK;
456 * Enforce flow control on the DPA device.
458 if (n < (DPA_MAX - DPA_HIGH_WATER))
459 nd->nd_dpa_flag |= DPA_WAIT_SPACE;
462 * This should never happen, as the flow control above
463 * should have stopped things before they got to this point.
465 if (n == 0) {
466 spin_unlock_irqrestore(&nd->nd_dpa_lock, lock_flags);
467 return;
471 * Copy as much data as will fit.
474 if (n > nbuf)
475 n = nbuf;
477 r = DPA_MAX - nd->nd_dpa_in;
479 if (r <= n) {
480 memcpy(nd->nd_dpa_buf + nd->nd_dpa_in, buf, r);
482 n -= r;
484 nd->nd_dpa_in = 0;
486 buf += r;
487 nbuf -= r;
490 memcpy(nd->nd_dpa_buf + nd->nd_dpa_in, buf, n);
492 nd->nd_dpa_in += n;
494 buf += n;
495 nbuf -= n;
497 if (nd->nd_dpa_in >= DPA_MAX)
498 pr_info_ratelimited("%s - nd->nd_dpa_in (%i) >= DPA_MAX\n",
499 __func__, nd->nd_dpa_in);
502 * Wakeup any thread waiting for data
504 if (nd->nd_dpa_flag & DPA_WAIT_DATA) {
505 nd->nd_dpa_flag &= ~DPA_WAIT_DATA;
506 wake_up_interruptible(&nd->nd_dpa_wqueue);
511 * Release the DPA lock.
513 spin_unlock_irqrestore(&nd->nd_dpa_lock, lock_flags);
517 * dgrp_monitor_data() -- builds a DPA data packet
518 * @nd: pointer to a node structure
519 * @type: type of message to be logged in the DPA buffer
520 * @buf: buffer of data to be logged in the DPA buffer
521 * @size -- number of bytes in the "buf" buffer
523 void dgrp_dpa_data(struct nd_struct *nd, int type, u8 *buf, int size)
525 u8 header[5];
527 header[0] = type;
529 put_unaligned_be32(size, header + 1);
531 dgrp_dpa(nd, header, sizeof(header));
532 dgrp_dpa(nd, buf, size);