riotty.c 18.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
** -----------------------------------------------------------------------------
**
**  Perle Specialix driver for Linux
**  Ported from existing RIO Driver for SCO sources.
 *
 *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
**	Module		: riotty.c
**	SID		: 1.3
**	Last Modified	: 11/6/98 10:33:47
**	Retrieved	: 11/6/98 10:33:50
**
**  ident @(#)riotty.c	1.3
**
** -----------------------------------------------------------------------------
*/
#ifdef SCCS_LABELS
static char *_riotty_c_sccs_ = "@(#)riotty.c	1.3";
#endif


#define __EXPLICIT_DEF_H__

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/string.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/string.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>

#include <linux/termios.h>

#include <linux/serial.h>

#include <linux/generic_serial.h>


#include "linux_compat.h"
#include "rio_linux.h"
#include "pkt.h"
#include "daemon.h"
#include "rio.h"
#include "riospace.h"
#include "cmdpkt.h"
#include "map.h"
#include "rup.h"
#include "port.h"
#include "riodrvr.h"
#include "rioinfo.h"
#include "func.h"
#include "errors.h"
#include "pci.h"

#include "parmmap.h"
#include "unixrup.h"
#include "board.h"
#include "host.h"
#include "phb.h"
#include "link.h"
#include "cmdblk.h"
#include "route.h"
#include "cirrus.h"
#include "rioioctl.h"
#include "param.h"

static void RIOClearUp(struct Port *PortP);

87 88
/* Below belongs in func.h */
int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
L
Linus Torvalds 已提交
89 90 91 92 93


extern struct rio_info *p;


A
Andrew Morton 已提交
94
int riotopen(struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
95
{
96
	unsigned int SysPort;
L
Linus Torvalds 已提交
97
	int repeat_this = 250;
A
Andrew Morton 已提交
98
	struct Port *PortP;	/* pointer to the port structure */
L
Linus Torvalds 已提交
99 100 101
	unsigned long flags;
	int retval = 0;

A
Andrew Morton 已提交
102
	func_enter();
L
Linus Torvalds 已提交
103 104 105

	/* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
	   is going to oops.
A
Andrew Morton 已提交
106
	 */
L
Linus Torvalds 已提交
107
	tty->driver_data = NULL;
A
Andrew Morton 已提交
108

L
Linus Torvalds 已提交
109 110
	SysPort = rio_minor(tty);

A
Andrew Morton 已提交
111 112 113
	if (p->RIOFailed) {
		rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
		func_exit();
L
Linus Torvalds 已提交
114 115 116
		return -ENXIO;
	}

117
	rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
L
Linus Torvalds 已提交
118 119

	/*
A
Andrew Morton 已提交
120 121 122 123 124
	 ** Validate that we have received a legitimate request.
	 ** Currently, just check that we are opening a port on
	 ** a host card that actually exists, and that the port
	 ** has been mapped onto a host.
	 */
L
Linus Torvalds 已提交
125
	if (SysPort >= RIO_PORTS) {	/* out of range ? */
A
Andrew Morton 已提交
126
		rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
L
Linus Torvalds 已提交
127 128 129 130 131
		func_exit();
		return -ENXIO;
	}

	/*
A
Andrew Morton 已提交
132 133
	 ** Grab pointer to the port stucture
	 */
L
Linus Torvalds 已提交
134
	PortP = p->RIOPortp[SysPort];	/* Get control struc */
A
Andrew Morton 已提交
135 136
	rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
	if (!PortP->Mapped) {	/* we aren't mapped yet! */
L
Linus Torvalds 已提交
137
		/*
A
Andrew Morton 已提交
138 139 140 141 142
		 ** The system doesn't know which RTA this port
		 ** corresponds to.
		 */
		rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
		func_exit();
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150
		return -ENXIO;
	}

	tty->driver_data = PortP;

	PortP->gs.tty = tty;
	PortP->gs.count++;

A
Andrew Morton 已提交
151
	rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
L
Linus Torvalds 已提交
152

A
Andrew Morton 已提交
153
	retval = gs_init_port(&PortP->gs);
L
Linus Torvalds 已提交
154 155 156 157 158
	if (retval) {
		PortP->gs.count--;
		return -ENXIO;
	}
	/*
A
Andrew Morton 已提交
159 160 161 162 163 164
	 ** If the host hasn't been booted yet, then
	 ** fail
	 */
	if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
		rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
		func_exit();
L
Linus Torvalds 已提交
165 166 167 168
		return -ENXIO;
	}

	/*
A
Andrew Morton 已提交
169 170 171 172
	 ** If the RTA has not booted yet and the user has choosen to block
	 ** until the RTA is present then we must spin here waiting for
	 ** the RTA to boot.
	 */
L
Linus Torvalds 已提交
173
	/* I find the above code a bit hairy. I find the below code
A
Andrew Morton 已提交
174
	   easier to read and shorter. Now, if it works too that would
L
Linus Torvalds 已提交
175
	   be great... -- REW 
A
Andrew Morton 已提交
176 177
	 */
	rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
L
Linus Torvalds 已提交
178
	while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
A
Andrew Morton 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		if (!PortP->WaitUntilBooted) {
			rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
			func_exit();
			return -ENXIO;
		}

		/* Under Linux you'd normally use a wait instead of this
		   busy-waiting. I'll stick with the old implementation for
		   now. --REW
		 */
		if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
			rio_dprintk(RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
			func_exit();
			return -EINTR;
		}
		if (repeat_this-- <= 0) {
			rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
			func_exit();
			return -EIO;
		}
L
Linus Torvalds 已提交
199
	}
A
Andrew Morton 已提交
200
	rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
L
Linus Torvalds 已提交
201
	rio_spin_lock_irqsave(&PortP->portSem, flags);
A
Andrew Morton 已提交
202
	if (p->RIOHalted) {
L
Linus Torvalds 已提交
203 204 205 206
		goto bombout;
	}

	/*
A
Andrew Morton 已提交
207 208 209 210 211 212 213 214 215
	 ** If the port is in the final throws of being closed,
	 ** we should wait here (politely), waiting
	 ** for it to finish, so that it doesn't close us!
	 */
	while ((PortP->State & RIO_CLOSING) && !p->RIOHalted) {
		rio_dprintk(RIO_DEBUG_TTY, "Waiting for RIO_CLOSING to go away\n");
		if (repeat_this-- <= 0) {
			rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
			RIOPreemptiveCmd(p, PortP, FCLOSE);
L
Linus Torvalds 已提交
216 217 218 219 220
			retval = -EINTR;
			goto bombout;
		}
		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
		if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
A
Andrew Morton 已提交
221
			rio_spin_lock_irqsave(&PortP->portSem, flags);
L
Linus Torvalds 已提交
222 223 224
			retval = -EINTR;
			goto bombout;
		}
A
Andrew Morton 已提交
225
		rio_spin_lock_irqsave(&PortP->portSem, flags);
L
Linus Torvalds 已提交
226 227
	}

A
Andrew Morton 已提交
228 229
	if (!PortP->Mapped) {
		rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
L
Linus Torvalds 已提交
230 231
		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
		retval = -ENXIO;
A
Andrew Morton 已提交
232
		func_exit();
L
Linus Torvalds 已提交
233 234 235
		return retval;
	}

A
Andrew Morton 已提交
236
	if (p->RIOHalted) {
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246
		goto bombout;
	}

/*
** 15.10.1998 ARG - ESIL 0761 part fix
** 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.
*/
	/* Uh? Suppose I turn these on and then another process opens
	   the port again? The flags get cleared! Not good. -- REW */
A
Andrew Morton 已提交
247 248
	if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
		PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
L
Linus Torvalds 已提交
249 250 251
	}

	if (!(PortP->firstOpen)) {	/* First time ? */
A
Andrew Morton 已提交
252 253
		rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");

L
Linus Torvalds 已提交
254 255

		PortP->firstOpen++;
A
Andrew Morton 已提交
256
		PortP->CookMode = 0;	/* XXX RIOCookMode(tp); */
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264
		PortP->InUse = NOT_INUSE;

		/* Tentative fix for bug PR27. Didn't work. */
		/* PortP->gs.xmit_cnt = 0; */

		rio_spin_unlock_irqrestore(&PortP->portSem, flags);

		/* Someone explain to me why this delay/config is
A
Andrew Morton 已提交
265 266
		   here. If I read the docs correctly the "open"
		   command piggybacks the parameters immediately.
L
Linus Torvalds 已提交
267
		   -- REW */
268
		RIOParam(PortP, OPEN, 1, OK_TO_SLEEP);	/* Open the port */
L
Linus Torvalds 已提交
269 270 271
		rio_spin_lock_irqsave(&PortP->portSem, flags);

		/*
A
Andrew Morton 已提交
272 273 274 275
		 ** wait for the port to be not closed.
		 */
		while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
			rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
L
Linus Torvalds 已提交
276 277
			rio_spin_unlock_irqrestore(&PortP->portSem, flags);
			if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
A
Andrew Morton 已提交
278 279 280
				rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
				RIOPreemptiveCmd(p, PortP, FCLOSE);
				func_exit();
L
Linus Torvalds 已提交
281 282 283 284 285
				return -EINTR;
			}
			rio_spin_lock_irqsave(&PortP->portSem, flags);
		}

A
Andrew Morton 已提交
286 287 288 289
		if (p->RIOHalted) {
			retval = -EIO;
		      bombout:
			/*                    RIOClearUp( PortP ); */
L
Linus Torvalds 已提交
290 291 292
			rio_spin_unlock_irqrestore(&PortP->portSem, flags);
			return retval;
		}
A
Andrew Morton 已提交
293
		rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
L
Linus Torvalds 已提交
294
	}
295 296 297 298 299 300 301 302
	rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
	/*
	 ** ACTION
	 ** insert test for carrier here. -- ???
	 ** I already see that test here. What's the deal? -- REW
	 */
	if ((PortP->gs.tty->termios->c_cflag & CLOCAL) || (PortP->ModemState & MSVR1_CD)) {
		rio_dprintk(RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
L
Linus Torvalds 已提交
303
		/*
304 305
		   tp->tm.c_state |= CARR_ON;
		   wakeup((caddr_t) &tp->tm.c_canq);
A
Andrew Morton 已提交
306
		 */
307 308 309
		PortP->State |= RIO_CARR_ON;
		wake_up_interruptible(&PortP->gs.open_wait);
	} else {	/* no carrier - wait for DCD */
L
Linus Torvalds 已提交
310
			/*
311 312 313 314 315
		   while (!(PortP->gs.tty->termios->c_state & CARR_ON) &&
		   !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
		 */
		while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
				rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
A
Andrew Morton 已提交
316
			/*
317
			   PortP->gs.tty->termios->c_state |= WOPEN;
A
Andrew Morton 已提交
318
			 */
319 320 321
			PortP->State |= RIO_WOPEN;
			rio_spin_unlock_irqrestore(&PortP->portSem, flags);
			if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
J
Jiri Slaby 已提交
322
				rio_spin_lock_irqsave(&PortP->portSem, flags);
323 324 325 326 327 328 329
				/*
				 ** ACTION: verify that this is a good thing
				 ** to do here. -- ???
				 ** I think it's OK. -- REW
				 */
				rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
				RIOPreemptiveCmd(p, PortP, FCLOSE);
L
Linus Torvalds 已提交
330
				/*
331
				   tp->tm.c_state &= ~WOPEN;
A
Andrew Morton 已提交
332
				 */
333
				PortP->State &= ~RIO_WOPEN;
L
Linus Torvalds 已提交
334
				rio_spin_unlock_irqrestore(&PortP->portSem, flags);
335 336
				func_exit();
				return -EINTR;
L
Linus Torvalds 已提交
337
			}
J
Jiri Slaby 已提交
338
			rio_spin_lock_irqsave(&PortP->portSem, flags);
L
Linus Torvalds 已提交
339
		}
340
		PortP->State &= ~RIO_WOPEN;
L
Linus Torvalds 已提交
341
	}
342 343 344 345
	if (p->RIOHalted)
		goto bombout;
	rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
	PortP->State |= RIO_MOPEN;
L
Linus Torvalds 已提交
346

347
	if (p->RIOHalted)
L
Linus Torvalds 已提交
348 349
		goto bombout;

A
Andrew Morton 已提交
350
	rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
L
Linus Torvalds 已提交
351 352

	/*
A
Andrew Morton 已提交
353 354
	 ** Count opens for port statistics reporting
	 */
L
Linus Torvalds 已提交
355 356 357 358
	if (PortP->statsGather)
		PortP->opens++;

	rio_spin_unlock_irqrestore(&PortP->portSem, flags);
A
Andrew Morton 已提交
359 360
	rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
	func_exit();
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368
	return 0;
}

/*
** RIOClose the port.
** The operating system thinks that this is last close for the device.
** As there are two interfaces to the port (Modem and tty), we need to
** check that both are closed before we close the device.
A
Andrew Morton 已提交
369 370
*/
int riotclose(void *ptr)
L
Linus Torvalds 已提交
371
{
372
	struct Port *PortP = ptr;	/* pointer to the port structure */
L
Linus Torvalds 已提交
373
	int deleted = 0;
A
Andrew Morton 已提交
374 375 376
	int try = -1;		/* Disable the timeouts by setting them to -1 */
	int repeat_this = -1;	/* Congrats to those having 15 years of
				   uptime! (You get to break the driver.) */
377
	unsigned long end_time;
A
Andrew Morton 已提交
378
	struct tty_struct *tty;
L
Linus Torvalds 已提交
379
	unsigned long flags;
380
	int rv = 0;
A
Andrew Morton 已提交
381 382

	rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
L
Linus Torvalds 已提交
383 384

	/* PortP = p->RIOPortp[SysPort]; */
385
	rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
A
Andrew Morton 已提交
386
	/* tp = PortP->TtyP; *//* Get tty */
L
Linus Torvalds 已提交
387
	tty = PortP->gs.tty;
388
	rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
L
Linus Torvalds 已提交
389

A
Andrew Morton 已提交
390
	if (PortP->gs.closing_wait)
L
Linus Torvalds 已提交
391
		end_time = jiffies + PortP->gs.closing_wait;
A
Andrew Morton 已提交
392
	else
L
Linus Torvalds 已提交
393 394 395 396 397
		end_time = jiffies + MAX_SCHEDULE_TIMEOUT;

	rio_spin_lock_irqsave(&PortP->portSem, flags);

	/*
A
Andrew Morton 已提交
398 399 400
	 ** Setting this flag will make any process trying to open
	 ** this port block until we are complete closing it.
	 */
L
Linus Torvalds 已提交
401 402
	PortP->State |= RIO_CLOSING;

A
Andrew Morton 已提交
403 404
	if ((PortP->State & RIO_DELETED)) {
		rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
L
Linus Torvalds 已提交
405 406
		deleted = 1;
	}
A
Andrew Morton 已提交
407 408 409

	if (p->RIOHalted) {
		RIOClearUp(PortP);
L
Linus Torvalds 已提交
410 411 412 413
		rv = -EIO;
		goto close_end;
	}

A
Andrew Morton 已提交
414
	rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
L
Linus Torvalds 已提交
415
	/*
A
Andrew Morton 已提交
416 417
	 ** clear the open bits for this device
	 */
418
	PortP->State &= ~RIO_MOPEN;
L
Linus Torvalds 已提交
419 420 421
	PortP->State &= ~RIO_CARR_ON;
	PortP->ModemState &= ~MSVR1_CD;
	/*
A
Andrew Morton 已提交
422 423 424 425 426 427 428
	 ** If the device was open as both a Modem and a tty line
	 ** then we need to wimp out here, as the port has not really
	 ** been finally closed (gee, whizz!) The test here uses the
	 ** bit for the OTHER mode of operation, to see if THAT is
	 ** still active!
	 */
	if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
L
Linus Torvalds 已提交
429
		/*
A
Andrew Morton 已提交
430 431 432 433
		 ** The port is still open for the other task -
		 ** return, pretending that we are still active.
		 */
		rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
L
Linus Torvalds 已提交
434 435 436 437 438 439 440
		PortP->State &= ~RIO_CLOSING;
		if (PortP->firstOpen)
			PortP->firstOpen--;
		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
		return -EIO;
	}

A
Andrew Morton 已提交
441
	rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
L
Linus Torvalds 已提交
442 443 444 445

	PortP->State &= ~RIO_DYNOROD;

	/*
A
Andrew Morton 已提交
446 447 448 449 450
	 ** This is where we wait for the port
	 ** to drain down before closing. Bye-bye....
	 ** (We never meant to do this)
	 */
	rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
L
Linus Torvalds 已提交
451 452

	if (!deleted)
A
Andrew Morton 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
			if (repeat_this-- <= 0) {
				rv = -EINTR;
				rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
				RIOPreemptiveCmd(p, PortP, FCLOSE);
				goto close_end;
			}
			rio_dprintk(RIO_DEBUG_TTY, "Calling timeout to flush in closing\n");
			rio_spin_unlock_irqrestore(&PortP->portSem, flags);
			if (RIODelay_ni(PortP, HUNDRED_MS * 10) == RIO_FAIL) {
				rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
				rv = -EINTR;
				rio_spin_lock_irqsave(&PortP->portSem, flags);
				goto close_end;
			}
L
Linus Torvalds 已提交
468 469 470 471 472 473 474
			rio_spin_lock_irqsave(&PortP->portSem, flags);
		}

	PortP->TxBufferIn = PortP->TxBufferOut = 0;
	repeat_this = 0xff;

	PortP->InUse = 0;
A
Andrew Morton 已提交
475
	if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
L
Linus Torvalds 已提交
476
		/*
A
Andrew Morton 已提交
477 478 479 480
		 ** The port has been re-opened for the other task -
		 ** return, pretending that we are still active.
		 */
		rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
L
Linus Torvalds 已提交
481 482 483 484 485 486 487
		PortP->State &= ~RIO_CLOSING;
		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
		if (PortP->firstOpen)
			PortP->firstOpen--;
		return -EIO;
	}

A
Andrew Morton 已提交
488 489
	if (p->RIOHalted) {
		RIOClearUp(PortP);
L
Linus Torvalds 已提交
490 491 492 493 494 495 496
		goto close_end;
	}

	/* Can't call RIOShortCommand with the port locked. */
	rio_spin_unlock_irqrestore(&PortP->portSem, flags);

	if (RIOShortCommand(p, PortP, CLOSE, 1, 0) == RIO_FAIL) {
497
		RIOPreemptiveCmd(p, PortP, FCLOSE);
J
Jiri Slaby 已提交
498
		rio_spin_lock_irqsave(&PortP->portSem, flags);
499
		goto close_end;
L
Linus Torvalds 已提交
500 501 502
	}

	if (!deleted)
A
Andrew Morton 已提交
503 504 505 506 507 508 509 510
		while (try && (PortP->PortState & PORT_ISOPEN)) {
			try--;
			if (time_after(jiffies, end_time)) {
				rio_dprintk(RIO_DEBUG_TTY, "Run out of tries - force the bugger shut!\n");
				RIOPreemptiveCmd(p, PortP, FCLOSE);
				break;
			}
			rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
L
Linus Torvalds 已提交
511

A
Andrew Morton 已提交
512 513
			if (p->RIOHalted) {
				RIOClearUp(PortP);
J
Jiri Slaby 已提交
514
				rio_spin_lock_irqsave(&PortP->portSem, flags);
A
Andrew Morton 已提交
515 516 517 518 519 520 521
				goto close_end;
			}
			if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
				rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
				RIOPreemptiveCmd(p, PortP, FCLOSE);
				break;
			}
L
Linus Torvalds 已提交
522 523
		}
	rio_spin_lock_irqsave(&PortP->portSem, flags);
A
Andrew Morton 已提交
524 525
	rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);

L
Linus Torvalds 已提交
526 527 528 529 530 531
	/* RIOPreemptiveCmd(p, PortP, FCLOSE); */

/*
** 15.10.1998 ARG - ESIL 0761 part fix
** 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.
*/
A
Andrew Morton 已提交
532
	PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
L
Linus Torvalds 已提交
533 534

	/*
A
Andrew Morton 已提交
535 536
	 ** Count opens for port statistics reporting
	 */
L
Linus Torvalds 已提交
537 538 539
	if (PortP->statsGather)
		PortP->closes++;

540
close_end:
L
Linus Torvalds 已提交
541 542 543 544
	/* XXX: Why would a "DELETED" flag be reset here? I'd have
	   thought that a "deleted" flag means that the port was
	   permanently gone, but here we can make it reappear by it
	   being in close during the "deletion".
A
Andrew Morton 已提交
545 546
	 */
	PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
L
Linus Torvalds 已提交
547 548 549
	if (PortP->firstOpen)
		PortP->firstOpen--;
	rio_spin_unlock_irqrestore(&PortP->portSem, flags);
A
Andrew Morton 已提交
550
	rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
L
Linus Torvalds 已提交
551 552 553 554 555
	return rv;
}



556
static void RIOClearUp(struct Port *PortP)
L
Linus Torvalds 已提交
557
{
A
Andrew Morton 已提交
558 559
	rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
	PortP->Config = 0;	/* Direct semaphore */
L
Linus Torvalds 已提交
560 561 562 563 564 565
	PortP->PortState = 0;
	PortP->firstOpen = 0;
	PortP->FlushCmdBodge = 0;
	PortP->ModemState = PortP->CookMode = 0;
	PortP->Mapped = 0;
	PortP->WflushFlag = 0;
A
Andrew Morton 已提交
566
	PortP->MagicFlags = 0;
L
Linus Torvalds 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580
	PortP->RxDataStart = 0;
	PortP->TxBufferIn = 0;
	PortP->TxBufferOut = 0;
}

/*
** Put a command onto a port.
** The PortPointer, command, length and arg are passed.
** The len is the length *inclusive* of the command byte,
** and so for a command that takes no data, len==1.
** The arg is a single byte, and is only used if len==2.
** Other values of len aren't allowed, and will cause
** a panic.
*/
A
Andrew Morton 已提交
581
int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
L
Linus Torvalds 已提交
582
{
A
Al Viro 已提交
583
	struct PKT __iomem *PacketP;
A
Andrew Morton 已提交
584
	int retries = 20;	/* at 10 per second -> 2 seconds */
L
Linus Torvalds 已提交
585 586
	unsigned long flags;

A
Andrew Morton 已提交
587
	rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
L
Linus Torvalds 已提交
588

A
Andrew Morton 已提交
589 590
	if (PortP->State & RIO_DELETED) {
		rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
L
Linus Torvalds 已提交
591 592 593 594 595
		return RIO_FAIL;
	}
	rio_spin_lock_irqsave(&PortP->portSem, flags);

	/*
A
Andrew Morton 已提交
596 597 598 599 600
	 ** If the port is in use for pre-emptive command, then wait for it to
	 ** be free again.
	 */
	while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
		rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
L
Linus Torvalds 已提交
601 602 603 604 605 606 607 608 609
		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
		if (retries-- <= 0) {
			return RIO_FAIL;
		}
		if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
			return RIO_FAIL;
		}
		rio_spin_lock_irqsave(&PortP->portSem, flags);
	}
A
Andrew Morton 已提交
610 611
	if (PortP->State & RIO_DELETED) {
		rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
L
Linus Torvalds 已提交
612 613 614 615
		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
		return RIO_FAIL;
	}

A
Andrew Morton 已提交
616 617
	while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
		rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
L
Linus Torvalds 已提交
618 619
		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
		if (retries-- <= 0) {
A
Andrew Morton 已提交
620
			rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
L
Linus Torvalds 已提交
621 622
			return RIO_FAIL;
		}
A
Andrew Morton 已提交
623
		if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
L
Linus Torvalds 已提交
624 625 626 627 628
			return RIO_FAIL;
		}
		rio_spin_lock_irqsave(&PortP->portSem, flags);
	}

A
Andrew Morton 已提交
629
	if (p->RIOHalted) {
L
Linus Torvalds 已提交
630 631 632 633 634
		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
		return RIO_FAIL;
	}

	/*
A
Andrew Morton 已提交
635 636
	 ** set the command byte and the argument byte
	 */
637
	writeb(command, &PacketP->data[0]);
L
Linus Torvalds 已提交
638

A
Andrew Morton 已提交
639
	if (len == 2)
640
		writeb(arg, &PacketP->data[1]);
L
Linus Torvalds 已提交
641 642

	/*
A
Andrew Morton 已提交
643 644
	 ** set the length of the packet and set the command bit.
	 */
645
	writeb(PKT_CMD_BIT | len, &PacketP->len);
L
Linus Torvalds 已提交
646 647 648

	add_transmit(PortP);
	/*
A
Andrew Morton 已提交
649 650
	 ** Count characters transmitted for port statistics reporting
	 */
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658
	if (PortP->statsGather)
		PortP->txchars += len;

	rio_spin_unlock_irqrestore(&PortP->portSem, flags);
	return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;
}