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 "dpa" devices.
27 * Includes those functions required to register the "dpa" devices
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
,
54 static unsigned int dgrp_dpa_select(struct file
*, struct poll_table_struct
*);
56 const struct file_operations dgrp_dpa_ops
= {
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
,
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 */
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 */
95 char vpd_data
[VPDSIZE
];
98 #define DIGI_GETVPD (('d'<<8) | 246) /* get VPD info */
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
;
117 rtn
= try_module_get(THIS_MODULE
);
123 if (!capable(CAP_SYS_ADMIN
)) {
129 * Make sure that the "private_data" field hasn't already been used.
131 if (file
->private_data
) {
137 * Get the node pointer, and fail if it doesn't exist.
139 nd
= PDE_DATA(inode
);
145 file
->private_data
= (void *) nd
;
148 * Allocate the DPA buffer.
151 if (nd
->nd_dpa_buf
) {
154 nd
->nd_dpa_buf
= kmalloc(DPA_MAX
, GFP_KERNEL
);
156 if (!nd
->nd_dpa_buf
) {
161 nd
->nd_dpa_lbolt
= jiffies
;
168 module_put(THIS_MODULE
);
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
;
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
);
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
);
213 module_put(THIS_MODULE
);
214 file
->private_data
= NULL
;
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
,
227 struct nd_struct
*nd
;
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
);
243 * Wait for some data to appear in the buffer.
246 spin_lock_irqsave(&nd
->nd_dpa_lock
, lock_flags
);
249 n
= (nd
->nd_dpa_in
- nd
->nd_dpa_out
) & DPA_MASK
;
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));
267 spin_lock_irqsave(&nd
->nd_dpa_lock
, lock_flags
);
271 * Read whatever is there.
279 r
= DPA_MAX
- nd
->nd_dpa_out
;
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
);
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
);
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
);
327 spin_unlock_irqrestore(&nd
->nd_dpa_lock
, lock_flags
);
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 */
345 static long dgrp_dpa_ioctl(struct file
*file
, unsigned int cmd
,
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
;
356 void __user
*uarg
= (void __user
*) arg
;
358 nd
= file
->private_data
;
362 if (copy_from_user(&getchan
, uarg
, sizeof(struct digi_chan
)))
365 port
= getchan
.ch_port
;
367 if (port
> nd
->nd_chan_count
)
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
)))
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
)))
403 if (copy_from_user(&setdebug
, uarg
, sizeof(struct digi_debug
)))
406 nd
->nd_dpa_debug
= setdebug
.onoff
;
407 nd
->nd_dpa_port
= setdebug
.port
;
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
);
419 if (copy_to_user(uarg
, &vpd
, sizeof(struct digi_vpd
)))
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
)
441 unsigned long lock_flags
;
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.
466 spin_unlock_irqrestore(&nd
->nd_dpa_lock
, lock_flags
);
471 * Copy as much data as will fit.
477 r
= DPA_MAX
- nd
->nd_dpa_in
;
480 memcpy(nd
->nd_dpa_buf
+ nd
->nd_dpa_in
, buf
, r
);
490 memcpy(nd
->nd_dpa_buf
+ nd
->nd_dpa_in
, buf
, 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
)
529 put_unaligned_be32(size
, header
+ 1);
531 dgrp_dpa(nd
, header
, sizeof(header
));
532 dgrp_dpa(nd
, buf
, size
);