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> |
Iulia Manda | 8dd64ac | 2014-03-11 00:10:12 +0200 | [diff] [blame] | 45 | #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */ |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 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 | { |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 65 | unsigned long flags; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 66 | unsigned int minor = iminor(inode); |
| 67 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 68 | spin_lock_irqsave(&dgnc_global_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 69 | |
| 70 | /* mgmt device */ |
| 71 | if (minor < MAXMGMTDEVICES) { |
| 72 | /* Only allow 1 open at a time on mgmt device */ |
| 73 | if (dgnc_mgmt_in_use[minor]) { |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 74 | spin_unlock_irqrestore(&dgnc_global_lock, flags); |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 75 | return -EBUSY; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 76 | } |
| 77 | dgnc_mgmt_in_use[minor]++; |
Iulia Manda | 1c1dd2a | 2014-03-11 00:08:06 +0200 | [diff] [blame] | 78 | } else { |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 79 | spin_unlock_irqrestore(&dgnc_global_lock, flags); |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 80 | return -ENXIO; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 83 | spin_unlock_irqrestore(&dgnc_global_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 84 | |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * dgnc_mgmt_close() |
| 91 | * |
| 92 | * Open the mgmt/dpa device |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 93 | */ |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 94 | int dgnc_mgmt_close(struct inode *inode, struct file *file) |
| 95 | { |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 96 | unsigned long flags; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 97 | unsigned int minor = iminor(inode); |
| 98 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 99 | spin_lock_irqsave(&dgnc_global_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 100 | |
| 101 | /* mgmt device */ |
| 102 | if (minor < MAXMGMTDEVICES) { |
Iulia Manda | 969fbc8 | 2014-03-11 00:09:11 +0200 | [diff] [blame] | 103 | if (dgnc_mgmt_in_use[minor]) |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 104 | dgnc_mgmt_in_use[minor] = 0; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 105 | } |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 106 | spin_unlock_irqrestore(&dgnc_global_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 107 | |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /* |
| 113 | * dgnc_mgmt_ioctl() |
| 114 | * |
| 115 | * ioctl the mgmt/dpa device |
Lidza Louina | 0986cda | 2013-08-21 11:08:05 -0400 | [diff] [blame] | 116 | */ |
Lidza Louina | 62a86e5 | 2013-08-20 14:15:36 -0400 | [diff] [blame] | 117 | |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 118 | long dgnc_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 119 | { |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 120 | unsigned long flags; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 121 | void __user *uarg = (void __user *) arg; |
| 122 | |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 123 | switch (cmd) { |
| 124 | |
| 125 | case DIGI_GETDD: |
| 126 | { |
| 127 | /* |
| 128 | * This returns the total number of boards |
| 129 | * in the system, as well as driver version |
| 130 | * and has space for a reserved entry |
| 131 | */ |
| 132 | struct digi_dinfo ddi; |
| 133 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 134 | spin_lock_irqsave(&dgnc_global_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 135 | |
| 136 | ddi.dinfo_nboards = dgnc_NumBoards; |
| 137 | sprintf(ddi.dinfo_version, "%s", DG_PART); |
| 138 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 139 | spin_unlock_irqrestore(&dgnc_global_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 140 | |
Iulia Manda | 3393fe9 | 2014-03-10 23:00:03 -0700 | [diff] [blame] | 141 | if (copy_to_user(uarg, &ddi, sizeof(ddi))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 142 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 143 | |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | case DIGI_GETBD: |
| 148 | { |
| 149 | int brd; |
| 150 | |
| 151 | struct digi_info di; |
| 152 | |
Iulia Manda | 969fbc8 | 2014-03-11 00:09:11 +0200 | [diff] [blame] | 153 | if (copy_from_user(&brd, uarg, sizeof(int))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 154 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 155 | |
Iulia Manda | 0469c74 | 2014-03-13 16:17:03 -0700 | [diff] [blame] | 156 | if ((brd < 0) || (brd > dgnc_NumBoards) || |
| 157 | (dgnc_NumBoards == 0)) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 158 | return -ENODEV; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 159 | |
| 160 | memset(&di, 0, sizeof(di)); |
| 161 | |
| 162 | di.info_bdnum = brd; |
| 163 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 164 | spin_lock_irqsave(&dgnc_Board[brd]->bd_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 165 | |
| 166 | di.info_bdtype = dgnc_Board[brd]->dpatype; |
| 167 | di.info_bdstate = dgnc_Board[brd]->dpastatus; |
| 168 | di.info_ioport = 0; |
| 169 | di.info_physaddr = (ulong) dgnc_Board[brd]->membase; |
| 170 | di.info_physsize = (ulong) dgnc_Board[brd]->membase - dgnc_Board[brd]->membase_end; |
| 171 | if (dgnc_Board[brd]->state != BOARD_FAILED) |
| 172 | di.info_nports = dgnc_Board[brd]->nasync; |
| 173 | else |
| 174 | di.info_nports = 0; |
| 175 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 176 | spin_unlock_irqrestore(&dgnc_Board[brd]->bd_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 177 | |
Iulia Manda | 3393fe9 | 2014-03-10 23:00:03 -0700 | [diff] [blame] | 178 | if (copy_to_user(uarg, &di, sizeof(di))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 179 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 180 | |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | case DIGI_GET_NI_INFO: |
| 185 | { |
| 186 | struct channel_t *ch; |
| 187 | struct ni_info ni; |
| 188 | uchar mstat = 0; |
| 189 | uint board = 0; |
| 190 | uint channel = 0; |
| 191 | |
Iulia Manda | 969fbc8 | 2014-03-11 00:09:11 +0200 | [diff] [blame] | 192 | if (copy_from_user(&ni, uarg, sizeof(ni))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 193 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 194 | |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 195 | board = ni.board; |
| 196 | channel = ni.channel; |
| 197 | |
| 198 | /* Verify boundaries on board */ |
Ashvini Varatharaj | e15b0c4 | 2013-10-19 13:26:44 +0530 | [diff] [blame] | 199 | if ((board > dgnc_NumBoards) || (dgnc_NumBoards == 0)) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 200 | return -ENODEV; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 201 | |
| 202 | /* Verify boundaries on channel */ |
| 203 | if ((channel < 0) || (channel > dgnc_Board[board]->nasync)) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 204 | return -ENODEV; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 205 | |
| 206 | ch = dgnc_Board[board]->channels[channel]; |
| 207 | |
| 208 | if (!ch || ch->magic != DGNC_CHANNEL_MAGIC) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 209 | return -ENODEV; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 210 | |
| 211 | memset(&ni, 0, sizeof(ni)); |
| 212 | ni.board = board; |
| 213 | ni.channel = channel; |
| 214 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 215 | spin_lock_irqsave(&ch->ch_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 216 | |
| 217 | mstat = (ch->ch_mostat | ch->ch_mistat); |
| 218 | |
| 219 | if (mstat & UART_MCR_DTR) { |
| 220 | ni.mstat |= TIOCM_DTR; |
| 221 | ni.dtr = TIOCM_DTR; |
| 222 | } |
| 223 | if (mstat & UART_MCR_RTS) { |
| 224 | ni.mstat |= TIOCM_RTS; |
| 225 | ni.rts = TIOCM_RTS; |
| 226 | } |
| 227 | if (mstat & UART_MSR_CTS) { |
| 228 | ni.mstat |= TIOCM_CTS; |
| 229 | ni.cts = TIOCM_CTS; |
| 230 | } |
| 231 | if (mstat & UART_MSR_RI) { |
| 232 | ni.mstat |= TIOCM_RI; |
| 233 | ni.ri = TIOCM_RI; |
| 234 | } |
| 235 | if (mstat & UART_MSR_DCD) { |
| 236 | ni.mstat |= TIOCM_CD; |
| 237 | ni.dcd = TIOCM_CD; |
| 238 | } |
| 239 | if (mstat & UART_MSR_DSR) |
| 240 | ni.mstat |= TIOCM_DSR; |
| 241 | |
| 242 | ni.iflag = ch->ch_c_iflag; |
| 243 | ni.oflag = ch->ch_c_oflag; |
| 244 | ni.cflag = ch->ch_c_cflag; |
| 245 | ni.lflag = ch->ch_c_lflag; |
| 246 | |
Iulia Manda | 0469c74 | 2014-03-13 16:17:03 -0700 | [diff] [blame] | 247 | if (ch->ch_digi.digi_flags & CTSPACE || |
| 248 | ch->ch_c_cflag & CRTSCTS) |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 249 | ni.hflow = 1; |
| 250 | else |
| 251 | ni.hflow = 0; |
| 252 | |
Iulia Manda | 0469c74 | 2014-03-13 16:17:03 -0700 | [diff] [blame] | 253 | if ((ch->ch_flags & CH_STOPI) || |
| 254 | (ch->ch_flags & CH_FORCED_STOPI)) |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 255 | ni.recv_stopped = 1; |
| 256 | else |
| 257 | ni.recv_stopped = 0; |
| 258 | |
| 259 | if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP)) |
| 260 | ni.xmit_stopped = 1; |
| 261 | else |
| 262 | ni.xmit_stopped = 0; |
| 263 | |
| 264 | ni.curtx = ch->ch_txcount; |
| 265 | ni.currx = ch->ch_rxcount; |
| 266 | |
| 267 | ni.baud = ch->ch_old_baud; |
| 268 | |
Roberta Dobrescu | 9a03d7c | 2014-09-26 23:58:36 +0300 | [diff] [blame^] | 269 | spin_unlock_irqrestore(&ch->ch_lock, flags); |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 270 | |
| 271 | if (copy_to_user(uarg, &ni, sizeof(ni))) |
Lidza Louina | 8f90ef8 | 2013-09-09 15:01:23 -0400 | [diff] [blame] | 272 | return -EFAULT; |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 273 | |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | } |
| 279 | |
Lidza Louina | 0b99d58 | 2013-08-01 17:00:20 -0400 | [diff] [blame] | 280 | return 0; |
| 281 | } |