Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 1 | /* |
| 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 | * |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 20 | * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE! |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 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 | * |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 27 | * Send any bug fixes/changes to: Eng.Linux at digi dot com. |
| 28 | * Thank you. |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 29 | * |
| 30 | */ |
| 31 | |
| 32 | /************************************************************************ |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 33 | * |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 34 | * This file implements the mgmt functionality for the |
| 35 | * Neo and ClassicBoard based product lines. |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 36 | * |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 37 | ************************************************************************ |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 38 | */ |
| 39 | #include <linux/kernel.h> |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 40 | #include <linux/ctype.h> |
| 41 | #include <linux/sched.h> /* For jiffies, task states */ |
| 42 | #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */ |
| 43 | #include <linux/serial_reg.h> |
| 44 | #include <linux/termios.h> |
| 45 | #include <asm/uaccess.h> /* For copy_from_user/copy_to_user */ |
| 46 | |
| 47 | #include "dgnc_driver.h" |
| 48 | #include "dgnc_pci.h" |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 49 | #include "dgnc_kcompat.h" /* Kernel 2.4/2.6 compat includes */ |
| 50 | #include "dgnc_mgmt.h" |
| 51 | #include "dpacompat.h" |
| 52 | |
| 53 | |
| 54 | /* Our "in use" variables, to enforce 1 open only */ |
| 55 | static int dgnc_mgmt_in_use[MAXMGMTDEVICES]; |
| 56 | |
| 57 | |
| 58 | /* |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 59 | * dgnc_mgmt_open() |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 60 | * |
| 61 | * Open the mgmt/downld/dpa device |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 62 | */ |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 63 | int dgnc_mgmt_open(struct inode *inode, struct file *file) |
| 64 | { |
| 65 | unsigned long lock_flags; |
| 66 | unsigned int minor = iminor(inode); |
| 67 | |
| 68 | DPR_MGMT(("dgnc_mgmt_open start.\n")); |
| 69 | |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 70 | DGNC_LOCK(dgnc_global_lock, lock_flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 71 | |
| 72 | /* mgmt device */ |
| 73 | if (minor < MAXMGMTDEVICES) { |
| 74 | /* Only allow 1 open at a time on mgmt device */ |
| 75 | if (dgnc_mgmt_in_use[minor]) { |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 76 | DGNC_UNLOCK(dgnc_global_lock, lock_flags); |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 77 | return -EBUSY; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 78 | } |
| 79 | dgnc_mgmt_in_use[minor]++; |
Iulia Manda | 1c1dd2a | 2014-03-11 00:08:06 +0200 | [diff] [blame] | 80 | } else { |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 81 | DGNC_UNLOCK(dgnc_global_lock, lock_flags); |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 82 | return -ENXIO; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 85 | DGNC_UNLOCK(dgnc_global_lock, lock_flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 86 | |
| 87 | DPR_MGMT(("dgnc_mgmt_open finish.\n")); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /* |
| 94 | * dgnc_mgmt_close() |
| 95 | * |
| 96 | * Open the mgmt/dpa device |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 97 | */ |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 98 | int dgnc_mgmt_close(struct inode *inode, struct file *file) |
| 99 | { |
| 100 | unsigned long lock_flags; |
| 101 | unsigned int minor = iminor(inode); |
| 102 | |
| 103 | DPR_MGMT(("dgnc_mgmt_close start.\n")); |
| 104 | |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 105 | DGNC_LOCK(dgnc_global_lock, lock_flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 106 | |
| 107 | /* mgmt device */ |
| 108 | if (minor < MAXMGMTDEVICES) { |
Iulia Manda | 969fbc8 | 2014-03-11 00:09:11 +0200 | [diff] [blame^] | 109 | if (dgnc_mgmt_in_use[minor]) |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 110 | dgnc_mgmt_in_use[minor] = 0; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 111 | } |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 112 | DGNC_UNLOCK(dgnc_global_lock, lock_flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 113 | |
| 114 | DPR_MGMT(("dgnc_mgmt_close finish.\n")); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* |
| 121 | * dgnc_mgmt_ioctl() |
| 122 | * |
| 123 | * ioctl the mgmt/dpa device |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 124 | */ |
Lidza Louina | 62a86e5 | 2013-08-20 14:15:36 -0400 | [diff] [blame] | 125 | |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 126 | long dgnc_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 127 | { |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 128 | unsigned long lock_flags; |
| 129 | void __user *uarg = (void __user *) arg; |
| 130 | |
| 131 | DPR_MGMT(("dgnc_mgmt_ioctl start.\n")); |
| 132 | |
| 133 | switch (cmd) { |
| 134 | |
| 135 | case DIGI_GETDD: |
| 136 | { |
| 137 | /* |
| 138 | * This returns the total number of boards |
| 139 | * in the system, as well as driver version |
| 140 | * and has space for a reserved entry |
| 141 | */ |
| 142 | struct digi_dinfo ddi; |
| 143 | |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 144 | DGNC_LOCK(dgnc_global_lock, lock_flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 145 | |
| 146 | ddi.dinfo_nboards = dgnc_NumBoards; |
| 147 | sprintf(ddi.dinfo_version, "%s", DG_PART); |
| 148 | |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 149 | DGNC_UNLOCK(dgnc_global_lock, lock_flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 150 | |
| 151 | DPR_MGMT(("DIGI_GETDD returning numboards: %d version: %s\n", |
| 152 | ddi.dinfo_nboards, ddi.dinfo_version)); |
| 153 | |
| 154 | if (copy_to_user(uarg, &ddi, sizeof (ddi))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 155 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 156 | |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | case DIGI_GETBD: |
| 161 | { |
| 162 | int brd; |
| 163 | |
| 164 | struct digi_info di; |
| 165 | |
Iulia Manda | 969fbc8 | 2014-03-11 00:09:11 +0200 | [diff] [blame^] | 166 | if (copy_from_user(&brd, uarg, sizeof(int))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 167 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 168 | |
| 169 | DPR_MGMT(("DIGI_GETBD asking about board: %d\n", brd)); |
| 170 | |
| 171 | if ((brd < 0) || (brd > dgnc_NumBoards) || (dgnc_NumBoards == 0)) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 172 | return -ENODEV; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 173 | |
| 174 | memset(&di, 0, sizeof(di)); |
| 175 | |
| 176 | di.info_bdnum = brd; |
| 177 | |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 178 | DGNC_LOCK(dgnc_Board[brd]->bd_lock, lock_flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 179 | |
| 180 | di.info_bdtype = dgnc_Board[brd]->dpatype; |
| 181 | di.info_bdstate = dgnc_Board[brd]->dpastatus; |
| 182 | di.info_ioport = 0; |
| 183 | di.info_physaddr = (ulong) dgnc_Board[brd]->membase; |
| 184 | di.info_physsize = (ulong) dgnc_Board[brd]->membase - dgnc_Board[brd]->membase_end; |
| 185 | if (dgnc_Board[brd]->state != BOARD_FAILED) |
| 186 | di.info_nports = dgnc_Board[brd]->nasync; |
| 187 | else |
| 188 | di.info_nports = 0; |
| 189 | |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 190 | DGNC_UNLOCK(dgnc_Board[brd]->bd_lock, lock_flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 191 | |
| 192 | DPR_MGMT(("DIGI_GETBD returning type: %x state: %x ports: %x size: %x\n", |
| 193 | di.info_bdtype, di.info_bdstate, di.info_nports, di.info_physsize)); |
| 194 | |
| 195 | if (copy_to_user(uarg, &di, sizeof (di))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 196 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 197 | |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | case DIGI_GET_NI_INFO: |
| 202 | { |
| 203 | struct channel_t *ch; |
| 204 | struct ni_info ni; |
| 205 | uchar mstat = 0; |
| 206 | uint board = 0; |
| 207 | uint channel = 0; |
| 208 | |
Iulia Manda | 969fbc8 | 2014-03-11 00:09:11 +0200 | [diff] [blame^] | 209 | if (copy_from_user(&ni, uarg, sizeof(ni))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 210 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 211 | |
| 212 | DPR_MGMT(("DIGI_GETBD asking about board: %d channel: %d\n", |
| 213 | ni.board, ni.channel)); |
| 214 | |
| 215 | board = ni.board; |
| 216 | channel = ni.channel; |
| 217 | |
| 218 | /* Verify boundaries on board */ |
Ashvini Varatharaj | e15b0c4 | 2013-10-19 13:26:44 +0530 | [diff] [blame] | 219 | if ((board > dgnc_NumBoards) || (dgnc_NumBoards == 0)) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 220 | return -ENODEV; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 221 | |
| 222 | /* Verify boundaries on channel */ |
| 223 | if ((channel < 0) || (channel > dgnc_Board[board]->nasync)) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 224 | return -ENODEV; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 225 | |
| 226 | ch = dgnc_Board[board]->channels[channel]; |
| 227 | |
| 228 | if (!ch || ch->magic != DGNC_CHANNEL_MAGIC) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 229 | return -ENODEV; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 230 | |
| 231 | memset(&ni, 0, sizeof(ni)); |
| 232 | ni.board = board; |
| 233 | ni.channel = channel; |
| 234 | |
| 235 | DGNC_LOCK(ch->ch_lock, lock_flags); |
| 236 | |
| 237 | mstat = (ch->ch_mostat | ch->ch_mistat); |
| 238 | |
| 239 | if (mstat & UART_MCR_DTR) { |
| 240 | ni.mstat |= TIOCM_DTR; |
| 241 | ni.dtr = TIOCM_DTR; |
| 242 | } |
| 243 | if (mstat & UART_MCR_RTS) { |
| 244 | ni.mstat |= TIOCM_RTS; |
| 245 | ni.rts = TIOCM_RTS; |
| 246 | } |
| 247 | if (mstat & UART_MSR_CTS) { |
| 248 | ni.mstat |= TIOCM_CTS; |
| 249 | ni.cts = TIOCM_CTS; |
| 250 | } |
| 251 | if (mstat & UART_MSR_RI) { |
| 252 | ni.mstat |= TIOCM_RI; |
| 253 | ni.ri = TIOCM_RI; |
| 254 | } |
| 255 | if (mstat & UART_MSR_DCD) { |
| 256 | ni.mstat |= TIOCM_CD; |
| 257 | ni.dcd = TIOCM_CD; |
| 258 | } |
| 259 | if (mstat & UART_MSR_DSR) |
| 260 | ni.mstat |= TIOCM_DSR; |
| 261 | |
| 262 | ni.iflag = ch->ch_c_iflag; |
| 263 | ni.oflag = ch->ch_c_oflag; |
| 264 | ni.cflag = ch->ch_c_cflag; |
| 265 | ni.lflag = ch->ch_c_lflag; |
| 266 | |
| 267 | if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) |
| 268 | ni.hflow = 1; |
| 269 | else |
| 270 | ni.hflow = 0; |
| 271 | |
| 272 | if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI)) |
| 273 | ni.recv_stopped = 1; |
| 274 | else |
| 275 | ni.recv_stopped = 0; |
| 276 | |
| 277 | if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP)) |
| 278 | ni.xmit_stopped = 1; |
| 279 | else |
| 280 | ni.xmit_stopped = 0; |
| 281 | |
| 282 | ni.curtx = ch->ch_txcount; |
| 283 | ni.currx = ch->ch_rxcount; |
| 284 | |
| 285 | ni.baud = ch->ch_old_baud; |
| 286 | |
| 287 | DGNC_UNLOCK(ch->ch_lock, lock_flags); |
| 288 | |
| 289 | if (copy_to_user(uarg, &ni, sizeof(ni))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 290 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 291 | |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | } |
| 297 | |
| 298 | DPR_MGMT(("dgnc_mgmt_ioctl finish.\n")); |
| 299 | |
| 300 | return 0; |
| 301 | } |