blob: 2fb49e89b3241184eb1e236e2412f1663810b3d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : riotty.c
24** SID : 1.3
25** Last Modified : 11/6/98 10:33:47
26** Retrieved : 11/6/98 10:33:50
27**
28** ident @(#)riotty.c 1.3
29**
30** -----------------------------------------------------------------------------
31*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define __EXPLICIT_DEF_H__
34
35#include <linux/module.h>
36#include <linux/slab.h>
37#include <linux/errno.h>
38#include <linux/tty.h>
39#include <linux/string.h>
40#include <asm/io.h>
41#include <asm/system.h>
42#include <asm/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
44
45#include <linux/termios.h>
46
47#include <linux/serial.h>
48
49#include <linux/generic_serial.h>
50
51
52#include "linux_compat.h"
53#include "rio_linux.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "pkt.h"
55#include "daemon.h"
56#include "rio.h"
57#include "riospace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include "cmdpkt.h"
59#include "map.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "rup.h"
61#include "port.h"
62#include "riodrvr.h"
63#include "rioinfo.h"
64#include "func.h"
65#include "errors.h"
66#include "pci.h"
67
68#include "parmmap.h"
69#include "unixrup.h"
70#include "board.h"
71#include "host.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include "phb.h"
73#include "link.h"
74#include "cmdblk.h"
75#include "route.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include "cirrus.h"
77#include "rioioctl.h"
78#include "param.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static void RIOClearUp(struct Port *PortP);
Alan Cox554b7c82006-03-24 03:18:32 -080081
82/* Below belongs in func.h */
Andrew Morton8d8706e2006-01-11 12:17:49 -080083int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086extern struct rio_info *p;
87
88
Andrew Morton8d8706e2006-01-11 12:17:49 -080089int riotopen(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Alan Coxe2b3afd2006-03-24 03:18:27 -080091 unsigned int SysPort;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int repeat_this = 250;
Andrew Morton8d8706e2006-01-11 12:17:49 -080093 struct Port *PortP; /* pointer to the port structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unsigned long flags;
95 int retval = 0;
96
Andrew Morton8d8706e2006-01-11 12:17:49 -080097 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
100 is going to oops.
Andrew Morton8d8706e2006-01-11 12:17:49 -0800101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 tty->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Andrew Morton8d8706e2006-01-11 12:17:49 -0800104 SysPort = rio_minor(tty);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800105
106 if (p->RIOFailed) {
107 rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
Andrew Morton8d8706e2006-01-11 12:17:49 -0800108 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -ENXIO;
110 }
111
Alan Cox554b7c82006-03-24 03:18:32 -0800112 rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800115 ** Validate that we have received a legitimate request.
116 ** Currently, just check that we are opening a port on
117 ** a host card that actually exists, and that the port
118 ** has been mapped onto a host.
119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (SysPort >= RIO_PORTS) { /* out of range ? */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800121 rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 func_exit();
123 return -ENXIO;
124 }
125
126 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800127 ** Grab pointer to the port stucture
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 PortP = p->RIOPortp[SysPort]; /* Get control struc */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800130 rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
131 if (!PortP->Mapped) { /* we aren't mapped yet! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800133 ** The system doesn't know which RTA this port
134 ** corresponds to.
135 */
136 rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
137 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -ENXIO;
139 }
140
141 tty->driver_data = PortP;
142
Alan Coxb5391e22008-07-16 21:55:20 +0100143 PortP->gs.port.tty = tty;
144 PortP->gs.port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Andrew Morton8d8706e2006-01-11 12:17:49 -0800146 rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Andrew Morton8d8706e2006-01-11 12:17:49 -0800148 retval = gs_init_port(&PortP->gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (retval) {
Alan Coxb5391e22008-07-16 21:55:20 +0100150 PortP->gs.port.count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -ENXIO;
152 }
153 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800154 ** If the host hasn't been booted yet, then
155 ** fail
156 */
157 if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
158 rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
Andrew Morton8d8706e2006-01-11 12:17:49 -0800159 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -ENXIO;
161 }
162
163 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800164 ** If the RTA has not booted yet and the user has choosen to block
165 ** until the RTA is present then we must spin here waiting for
166 ** the RTA to boot.
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* I find the above code a bit hairy. I find the below code
Andrew Morton8d8706e2006-01-11 12:17:49 -0800169 easier to read and shorter. Now, if it works too that would
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 be great... -- REW
Andrew Morton8d8706e2006-01-11 12:17:49 -0800171 */
172 rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800174 if (!PortP->WaitUntilBooted) {
175 rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
176 func_exit();
177 return -ENXIO;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Andrew Morton8d8706e2006-01-11 12:17:49 -0800180 /* Under Linux you'd normally use a wait instead of this
181 busy-waiting. I'll stick with the old implementation for
182 now. --REW
183 */
184 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
185 rio_dprintk(RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
186 func_exit();
187 return -EINTR;
188 }
189 if (repeat_this-- <= 0) {
190 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
191 func_exit();
192 return -EIO;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800195 rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800197 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto bombout;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800202 ** If the port is in the final throws of being closed,
203 ** we should wait here (politely), waiting
204 ** for it to finish, so that it doesn't close us!
205 */
206 while ((PortP->State & RIO_CLOSING) && !p->RIOHalted) {
207 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RIO_CLOSING to go away\n");
208 if (repeat_this-- <= 0) {
209 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
Jiri Slabyd6f63412008-04-30 00:53:57 -0700210 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 retval = -EINTR;
212 goto bombout;
213 }
214 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
215 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800216 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 retval = -EINTR;
218 goto bombout;
219 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800220 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
Andrew Morton8d8706e2006-01-11 12:17:49 -0800223 if (!PortP->Mapped) {
224 rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
226 retval = -ENXIO;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800227 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return retval;
229 }
230
Andrew Morton8d8706e2006-01-11 12:17:49 -0800231 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto bombout;
233 }
234
235/*
236** 15.10.1998 ARG - ESIL 0761 part fix
237** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,
238** we need to make sure that the flags are clear when the port is opened.
239*/
240 /* Uh? Suppose I turn these on and then another process opens
241 the port again? The flags get cleared! Not good. -- REW */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800242 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
243 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 if (!(PortP->firstOpen)) { /* First time ? */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800247 rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 PortP->firstOpen++;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800251 PortP->CookMode = 0; /* XXX RIOCookMode(tp); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 PortP->InUse = NOT_INUSE;
253
254 /* Tentative fix for bug PR27. Didn't work. */
255 /* PortP->gs.xmit_cnt = 0; */
256
257 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* Someone explain to me why this delay/config is
Andrew Morton8d8706e2006-01-11 12:17:49 -0800260 here. If I read the docs correctly the "open"
261 command piggybacks the parameters immediately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 -- REW */
Jiri Slabyd6f63412008-04-30 00:53:57 -0700263 RIOParam(PortP, RIOC_OPEN, 1, OK_TO_SLEEP); /* Open the port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 rio_spin_lock_irqsave(&PortP->portSem, flags);
265
266 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800267 ** wait for the port to be not closed.
268 */
269 while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
270 rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
272 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800273 rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
Jiri Slabyd6f63412008-04-30 00:53:57 -0700274 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800275 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINTR;
277 }
278 rio_spin_lock_irqsave(&PortP->portSem, flags);
279 }
280
Andrew Morton8d8706e2006-01-11 12:17:49 -0800281 if (p->RIOHalted) {
282 retval = -EIO;
283 bombout:
284 /* RIOClearUp( PortP ); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
286 return retval;
287 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800288 rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Alan Cox554b7c82006-03-24 03:18:32 -0800290 rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
291 /*
292 ** ACTION
293 ** insert test for carrier here. -- ???
294 ** I already see that test here. What's the deal? -- REW
295 */
Alan Coxb5391e22008-07-16 21:55:20 +0100296 if ((PortP->gs.port.tty->termios->c_cflag & CLOCAL) ||
Jiri Slabyd6f63412008-04-30 00:53:57 -0700297 (PortP->ModemState & RIOC_MSVR1_CD)) {
Alan Cox554b7c82006-03-24 03:18:32 -0800298 rio_dprintk(RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /*
Alan Cox554b7c82006-03-24 03:18:32 -0800300 tp->tm.c_state |= CARR_ON;
301 wakeup((caddr_t) &tp->tm.c_canq);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800302 */
Alan Cox554b7c82006-03-24 03:18:32 -0800303 PortP->State |= RIO_CARR_ON;
Alan Coxb5391e22008-07-16 21:55:20 +0100304 wake_up_interruptible(&PortP->gs.port.open_wait);
Alan Cox554b7c82006-03-24 03:18:32 -0800305 } else { /* no carrier - wait for DCD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /*
Alan Coxb5391e22008-07-16 21:55:20 +0100307 while (!(PortP->gs.port.tty->termios->c_state & CARR_ON) &&
Alan Cox554b7c82006-03-24 03:18:32 -0800308 !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
309 */
310 while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800311 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
Alan Cox554b7c82006-03-24 03:18:32 -0800312 /*
Alan Coxb5391e22008-07-16 21:55:20 +0100313 PortP->gs.port.tty->termios->c_state |= WOPEN;
Alan Cox554b7c82006-03-24 03:18:32 -0800314 */
315 PortP->State |= RIO_WOPEN;
316 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
317 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700318 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
Alan Cox554b7c82006-03-24 03:18:32 -0800320 ** ACTION: verify that this is a good thing
321 ** to do here. -- ???
322 ** I think it's OK. -- REW
Andrew Morton8d8706e2006-01-11 12:17:49 -0800323 */
Alan Cox554b7c82006-03-24 03:18:32 -0800324 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
Jiri Slabyd6f63412008-04-30 00:53:57 -0700325 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
Alan Cox554b7c82006-03-24 03:18:32 -0800326 /*
327 tp->tm.c_state &= ~WOPEN;
328 */
329 PortP->State &= ~RIO_WOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Alan Cox554b7c82006-03-24 03:18:32 -0800331 func_exit();
332 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700334 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Alan Cox554b7c82006-03-24 03:18:32 -0800336 PortP->State &= ~RIO_WOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Alan Cox554b7c82006-03-24 03:18:32 -0800338 if (p->RIOHalted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 goto bombout;
Alan Cox554b7c82006-03-24 03:18:32 -0800340 rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
341 PortP->State |= RIO_MOPEN;
342
343 if (p->RIOHalted)
344 goto bombout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Andrew Morton8d8706e2006-01-11 12:17:49 -0800346 rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800349 ** Count opens for port statistics reporting
350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (PortP->statsGather)
352 PortP->opens++;
353
354 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800355 rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
356 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
360/*
361** RIOClose the port.
362** The operating system thinks that this is last close for the device.
363** As there are two interfaces to the port (Modem and tty), we need to
364** check that both are closed before we close the device.
Andrew Morton8d8706e2006-01-11 12:17:49 -0800365*/
366int riotclose(void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700368 struct Port *PortP = ptr; /* pointer to the port structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int deleted = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800370 int try = -1; /* Disable the timeouts by setting them to -1 */
371 int repeat_this = -1; /* Congrats to those having 15 years of
372 uptime! (You get to break the driver.) */
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700373 unsigned long end_time;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800374 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned long flags;
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700376 int rv = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800377
378 rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* PortP = p->RIOPortp[SysPort]; */
Alan Cox554b7c82006-03-24 03:18:32 -0800381 rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800382 /* tp = PortP->TtyP; *//* Get tty */
Alan Coxb5391e22008-07-16 21:55:20 +0100383 tty = PortP->gs.port.tty;
Alan Cox554b7c82006-03-24 03:18:32 -0800384 rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Andrew Morton8d8706e2006-01-11 12:17:49 -0800386 if (PortP->gs.closing_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 end_time = jiffies + PortP->gs.closing_wait;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800388 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 rio_spin_lock_irqsave(&PortP->portSem, flags);
392
393 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800394 ** Setting this flag will make any process trying to open
395 ** this port block until we are complete closing it.
396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 PortP->State |= RIO_CLOSING;
398
Andrew Morton8d8706e2006-01-11 12:17:49 -0800399 if ((PortP->State & RIO_DELETED)) {
400 rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 deleted = 1;
402 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800403
404 if (p->RIOHalted) {
405 RIOClearUp(PortP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 rv = -EIO;
407 goto close_end;
408 }
409
Andrew Morton8d8706e2006-01-11 12:17:49 -0800410 rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800412 ** clear the open bits for this device
413 */
Alan Cox554b7c82006-03-24 03:18:32 -0800414 PortP->State &= ~RIO_MOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 PortP->State &= ~RIO_CARR_ON;
Jiri Slabyd6f63412008-04-30 00:53:57 -0700416 PortP->ModemState &= ~RIOC_MSVR1_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800418 ** If the device was open as both a Modem and a tty line
419 ** then we need to wimp out here, as the port has not really
420 ** been finally closed (gee, whizz!) The test here uses the
421 ** bit for the OTHER mode of operation, to see if THAT is
422 ** still active!
423 */
424 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800426 ** The port is still open for the other task -
427 ** return, pretending that we are still active.
428 */
429 rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 PortP->State &= ~RIO_CLOSING;
431 if (PortP->firstOpen)
432 PortP->firstOpen--;
433 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
434 return -EIO;
435 }
436
Andrew Morton8d8706e2006-01-11 12:17:49 -0800437 rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 PortP->State &= ~RIO_DYNOROD;
440
441 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800442 ** This is where we wait for the port
443 ** to drain down before closing. Bye-bye....
444 ** (We never meant to do this)
445 */
446 rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (!deleted)
Andrew Morton8d8706e2006-01-11 12:17:49 -0800449 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800450 if (repeat_this-- <= 0) {
451 rv = -EINTR;
452 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
Jiri Slabyd6f63412008-04-30 00:53:57 -0700453 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800454 goto close_end;
455 }
456 rio_dprintk(RIO_DEBUG_TTY, "Calling timeout to flush in closing\n");
457 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
458 if (RIODelay_ni(PortP, HUNDRED_MS * 10) == RIO_FAIL) {
459 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
460 rv = -EINTR;
461 rio_spin_lock_irqsave(&PortP->portSem, flags);
462 goto close_end;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 PortP->TxBufferIn = PortP->TxBufferOut = 0;
468 repeat_this = 0xff;
469
470 PortP->InUse = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800471 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800473 ** The port has been re-opened for the other task -
474 ** return, pretending that we are still active.
475 */
476 rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 PortP->State &= ~RIO_CLOSING;
478 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
479 if (PortP->firstOpen)
480 PortP->firstOpen--;
481 return -EIO;
482 }
483
Andrew Morton8d8706e2006-01-11 12:17:49 -0800484 if (p->RIOHalted) {
485 RIOClearUp(PortP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto close_end;
487 }
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Can't call RIOShortCommand with the port locked. */
490 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
491
Jiri Slabyd6f63412008-04-30 00:53:57 -0700492 if (RIOShortCommand(p, PortP, RIOC_CLOSE, 1, 0) == RIO_FAIL) {
493 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700494 rio_spin_lock_irqsave(&PortP->portSem, flags);
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700495 goto close_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
498 if (!deleted)
Andrew Morton8d8706e2006-01-11 12:17:49 -0800499 while (try && (PortP->PortState & PORT_ISOPEN)) {
500 try--;
501 if (time_after(jiffies, end_time)) {
502 rio_dprintk(RIO_DEBUG_TTY, "Run out of tries - force the bugger shut!\n");
Jiri Slabyd6f63412008-04-30 00:53:57 -0700503 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800504 break;
505 }
506 rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Andrew Morton8d8706e2006-01-11 12:17:49 -0800508 if (p->RIOHalted) {
509 RIOClearUp(PortP);
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700510 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800511 goto close_end;
512 }
513 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
514 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
Jiri Slabyd6f63412008-04-30 00:53:57 -0700515 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800516 break;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800520 rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);
521
Jiri Slabyd6f63412008-04-30 00:53:57 -0700522 /* RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524/*
525** 15.10.1998 ARG - ESIL 0761 part fix
526** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,** we need to make sure that the flags are clear when the port is opened.
527*/
Andrew Morton8d8706e2006-01-11 12:17:49 -0800528 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800531 ** Count opens for port statistics reporting
532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (PortP->statsGather)
534 PortP->closes++;
535
Alan Cox554b7c82006-03-24 03:18:32 -0800536close_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* XXX: Why would a "DELETED" flag be reset here? I'd have
538 thought that a "deleted" flag means that the port was
539 permanently gone, but here we can make it reappear by it
540 being in close during the "deletion".
Andrew Morton8d8706e2006-01-11 12:17:49 -0800541 */
542 PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (PortP->firstOpen)
544 PortP->firstOpen--;
545 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800546 rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return rv;
548}
549
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Alan Cox554b7c82006-03-24 03:18:32 -0800552static void RIOClearUp(struct Port *PortP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Andrew Morton8d8706e2006-01-11 12:17:49 -0800554 rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
555 PortP->Config = 0; /* Direct semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 PortP->PortState = 0;
557 PortP->firstOpen = 0;
558 PortP->FlushCmdBodge = 0;
559 PortP->ModemState = PortP->CookMode = 0;
560 PortP->Mapped = 0;
561 PortP->WflushFlag = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800562 PortP->MagicFlags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 PortP->RxDataStart = 0;
564 PortP->TxBufferIn = 0;
565 PortP->TxBufferOut = 0;
566}
567
568/*
569** Put a command onto a port.
570** The PortPointer, command, length and arg are passed.
571** The len is the length *inclusive* of the command byte,
572** and so for a command that takes no data, len==1.
573** The arg is a single byte, and is only used if len==2.
574** Other values of len aren't allowed, and will cause
575** a panic.
576*/
Andrew Morton8d8706e2006-01-11 12:17:49 -0800577int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Al Virod886cb52006-05-27 00:08:25 -0400579 struct PKT __iomem *PacketP;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800580 int retries = 20; /* at 10 per second -> 2 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 unsigned long flags;
582
Andrew Morton8d8706e2006-01-11 12:17:49 -0800583 rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Andrew Morton8d8706e2006-01-11 12:17:49 -0800585 if (PortP->State & RIO_DELETED) {
586 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return RIO_FAIL;
588 }
589 rio_spin_lock_irqsave(&PortP->portSem, flags);
590
591 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800592 ** If the port is in use for pre-emptive command, then wait for it to
593 ** be free again.
594 */
595 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
596 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
598 if (retries-- <= 0) {
599 return RIO_FAIL;
600 }
601 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
602 return RIO_FAIL;
603 }
604 rio_spin_lock_irqsave(&PortP->portSem, flags);
605 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800606 if (PortP->State & RIO_DELETED) {
607 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
609 return RIO_FAIL;
610 }
611
Andrew Morton8d8706e2006-01-11 12:17:49 -0800612 while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
613 rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
615 if (retries-- <= 0) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800616 rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return RIO_FAIL;
618 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800619 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return RIO_FAIL;
621 }
622 rio_spin_lock_irqsave(&PortP->portSem, flags);
623 }
624
Andrew Morton8d8706e2006-01-11 12:17:49 -0800625 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
627 return RIO_FAIL;
628 }
629
630 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800631 ** set the command byte and the argument byte
632 */
Alan Coxe2b3afd2006-03-24 03:18:27 -0800633 writeb(command, &PacketP->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Andrew Morton8d8706e2006-01-11 12:17:49 -0800635 if (len == 2)
Alan Coxe2b3afd2006-03-24 03:18:27 -0800636 writeb(arg, &PacketP->data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800639 ** set the length of the packet and set the command bit.
640 */
Alan Coxe2b3afd2006-03-24 03:18:27 -0800641 writeb(PKT_CMD_BIT | len, &PacketP->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 add_transmit(PortP);
644 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800645 ** Count characters transmitted for port statistics reporting
646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (PortP->statsGather)
648 PortP->txchars += len;
649
650 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
651 return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;
652}
653
654