blob: b8e47920111dfc6e776338d9657b580f9fa999c8 [file] [log] [blame]
Lidza Louina0b99d582013-08-01 17:00:20 -04001/*
2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 *
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
21 *
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
26 *
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
28 * Thank you.
29 *
30 */
31
32/************************************************************************
33 *
34 * This file implements the mgmt functionality for the
35 * Neo and ClassicBoard based product lines.
36 *
37 ************************************************************************
38 * $Id: dgnc_mgmt.c,v 1.2 2010/12/14 20:08:29 markh Exp $
39 */
40#include <linux/kernel.h>
41#include <linux/version.h>
42#include <linux/ctype.h>
43#include <linux/sched.h> /* For jiffies, task states */
44#include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
45#include <linux/serial_reg.h>
46#include <linux/termios.h>
47#include <asm/uaccess.h> /* For copy_from_user/copy_to_user */
48
49#include "dgnc_driver.h"
50#include "dgnc_pci.h"
51#include "dgnc_proc.h"
52#include "dgnc_kcompat.h" /* Kernel 2.4/2.6 compat includes */
53#include "dgnc_mgmt.h"
54#include "dpacompat.h"
55
56
57/* Our "in use" variables, to enforce 1 open only */
58static int dgnc_mgmt_in_use[MAXMGMTDEVICES];
59
60
61/*
62 * dgnc_mgmt_open()
63 *
64 * Open the mgmt/downld/dpa device
65 */
66int dgnc_mgmt_open(struct inode *inode, struct file *file)
67{
68 unsigned long lock_flags;
69 unsigned int minor = iminor(inode);
70
71 DPR_MGMT(("dgnc_mgmt_open start.\n"));
72
73 DGNC_LOCK(dgnc_global_lock, lock_flags);
74
75 /* mgmt device */
76 if (minor < MAXMGMTDEVICES) {
77 /* Only allow 1 open at a time on mgmt device */
78 if (dgnc_mgmt_in_use[minor]) {
79 DGNC_UNLOCK(dgnc_global_lock, lock_flags);
80 return (-EBUSY);
81 }
82 dgnc_mgmt_in_use[minor]++;
83 }
84 else {
85 DGNC_UNLOCK(dgnc_global_lock, lock_flags);
86 return (-ENXIO);
87 }
88
89 DGNC_UNLOCK(dgnc_global_lock, lock_flags);
90
91 DPR_MGMT(("dgnc_mgmt_open finish.\n"));
92
93 return 0;
94}
95
96
97/*
98 * dgnc_mgmt_close()
99 *
100 * Open the mgmt/dpa device
101 */
102int dgnc_mgmt_close(struct inode *inode, struct file *file)
103{
104 unsigned long lock_flags;
105 unsigned int minor = iminor(inode);
106
107 DPR_MGMT(("dgnc_mgmt_close start.\n"));
108
109 DGNC_LOCK(dgnc_global_lock, lock_flags);
110
111 /* mgmt device */
112 if (minor < MAXMGMTDEVICES) {
113 if (dgnc_mgmt_in_use[minor]) {
114 dgnc_mgmt_in_use[minor] = 0;
115 }
116 }
117 DGNC_UNLOCK(dgnc_global_lock, lock_flags);
118
119 DPR_MGMT(("dgnc_mgmt_close finish.\n"));
120
121 return 0;
122}
123
124
125/*
126 * dgnc_mgmt_ioctl()
127 *
128 * ioctl the mgmt/dpa device
129 */
130#ifdef HAVE_UNLOCKED_IOCTL
131long dgnc_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
132{
133 struct inode *inode = file->f_dentry->d_inode;
134#else
135int dgnc_mgmt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
136{
137#endif
138 unsigned long lock_flags;
139 void __user *uarg = (void __user *) arg;
140
141 DPR_MGMT(("dgnc_mgmt_ioctl start.\n"));
142
143 switch (cmd) {
144
145 case DIGI_GETDD:
146 {
147 /*
148 * This returns the total number of boards
149 * in the system, as well as driver version
150 * and has space for a reserved entry
151 */
152 struct digi_dinfo ddi;
153
154 DGNC_LOCK(dgnc_global_lock, lock_flags);
155
156 ddi.dinfo_nboards = dgnc_NumBoards;
157 sprintf(ddi.dinfo_version, "%s", DG_PART);
158
159 DGNC_UNLOCK(dgnc_global_lock, lock_flags);
160
161 DPR_MGMT(("DIGI_GETDD returning numboards: %d version: %s\n",
162 ddi.dinfo_nboards, ddi.dinfo_version));
163
164 if (copy_to_user(uarg, &ddi, sizeof (ddi)))
165 return(-EFAULT);
166
167 break;
168 }
169
170 case DIGI_GETBD:
171 {
172 int brd;
173
174 struct digi_info di;
175
176 if (copy_from_user(&brd, uarg, sizeof(int))) {
177 return(-EFAULT);
178 }
179
180 DPR_MGMT(("DIGI_GETBD asking about board: %d\n", brd));
181
182 if ((brd < 0) || (brd > dgnc_NumBoards) || (dgnc_NumBoards == 0))
183 return (-ENODEV);
184
185 memset(&di, 0, sizeof(di));
186
187 di.info_bdnum = brd;
188
189 DGNC_LOCK(dgnc_Board[brd]->bd_lock, lock_flags);
190
191 di.info_bdtype = dgnc_Board[brd]->dpatype;
192 di.info_bdstate = dgnc_Board[brd]->dpastatus;
193 di.info_ioport = 0;
194 di.info_physaddr = (ulong) dgnc_Board[brd]->membase;
195 di.info_physsize = (ulong) dgnc_Board[brd]->membase - dgnc_Board[brd]->membase_end;
196 if (dgnc_Board[brd]->state != BOARD_FAILED)
197 di.info_nports = dgnc_Board[brd]->nasync;
198 else
199 di.info_nports = 0;
200
201 DGNC_UNLOCK(dgnc_Board[brd]->bd_lock, lock_flags);
202
203 DPR_MGMT(("DIGI_GETBD returning type: %x state: %x ports: %x size: %x\n",
204 di.info_bdtype, di.info_bdstate, di.info_nports, di.info_physsize));
205
206 if (copy_to_user(uarg, &di, sizeof (di)))
207 return (-EFAULT);
208
209 break;
210 }
211
212 case DIGI_GET_NI_INFO:
213 {
214 struct channel_t *ch;
215 struct ni_info ni;
216 uchar mstat = 0;
217 uint board = 0;
218 uint channel = 0;
219
220 if (copy_from_user(&ni, uarg, sizeof(struct ni_info))) {
221 return(-EFAULT);
222 }
223
224 DPR_MGMT(("DIGI_GETBD asking about board: %d channel: %d\n",
225 ni.board, ni.channel));
226
227 board = ni.board;
228 channel = ni.channel;
229
230 /* Verify boundaries on board */
231 if ((board < 0) || (board > dgnc_NumBoards) || (dgnc_NumBoards == 0))
232 return (-ENODEV);
233
234 /* Verify boundaries on channel */
235 if ((channel < 0) || (channel > dgnc_Board[board]->nasync))
236 return (-ENODEV);
237
238 ch = dgnc_Board[board]->channels[channel];
239
240 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
241 return (-ENODEV);
242
243 memset(&ni, 0, sizeof(ni));
244 ni.board = board;
245 ni.channel = channel;
246
247 DGNC_LOCK(ch->ch_lock, lock_flags);
248
249 mstat = (ch->ch_mostat | ch->ch_mistat);
250
251 if (mstat & UART_MCR_DTR) {
252 ni.mstat |= TIOCM_DTR;
253 ni.dtr = TIOCM_DTR;
254 }
255 if (mstat & UART_MCR_RTS) {
256 ni.mstat |= TIOCM_RTS;
257 ni.rts = TIOCM_RTS;
258 }
259 if (mstat & UART_MSR_CTS) {
260 ni.mstat |= TIOCM_CTS;
261 ni.cts = TIOCM_CTS;
262 }
263 if (mstat & UART_MSR_RI) {
264 ni.mstat |= TIOCM_RI;
265 ni.ri = TIOCM_RI;
266 }
267 if (mstat & UART_MSR_DCD) {
268 ni.mstat |= TIOCM_CD;
269 ni.dcd = TIOCM_CD;
270 }
271 if (mstat & UART_MSR_DSR)
272 ni.mstat |= TIOCM_DSR;
273
274 ni.iflag = ch->ch_c_iflag;
275 ni.oflag = ch->ch_c_oflag;
276 ni.cflag = ch->ch_c_cflag;
277 ni.lflag = ch->ch_c_lflag;
278
279 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS)
280 ni.hflow = 1;
281 else
282 ni.hflow = 0;
283
284 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI))
285 ni.recv_stopped = 1;
286 else
287 ni.recv_stopped = 0;
288
289 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
290 ni.xmit_stopped = 1;
291 else
292 ni.xmit_stopped = 0;
293
294 ni.curtx = ch->ch_txcount;
295 ni.currx = ch->ch_rxcount;
296
297 ni.baud = ch->ch_old_baud;
298
299 DGNC_UNLOCK(ch->ch_lock, lock_flags);
300
301 if (copy_to_user(uarg, &ni, sizeof(ni)))
302 return (-EFAULT);
303
304 break;
305 }
306
307
308 }
309
310 DPR_MGMT(("dgnc_mgmt_ioctl finish.\n"));
311
312 return 0;
313}