2 ** -----------------------------------------------------------------------------
4 ** Perle Specialix driver for Linux
5 ** Ported from existing RIO Driver for SCO sources.
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
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.
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.
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.
25 ** Last Modified : 11/6/98 10:33:47
26 ** Retrieved : 11/6/98 10:33:50
28 ** ident @(#)riotty.c 1.3
30 ** -----------------------------------------------------------------------------
33 static char *_riotty_c_sccs_ = "@(#)riotty.c 1.3";
37 #define __EXPLICIT_DEF_H__
39 #include <linux/module.h>
40 #include <linux/slab.h>
41 #include <linux/errno.h>
42 #include <linux/tty.h>
43 #include <linux/string.h>
45 #include <asm/system.h>
46 #include <asm/string.h>
47 #include <asm/uaccess.h>
49 #include <linux/termios.h>
51 #include <linux/serial.h>
53 #include <linux/generic_serial.h>
56 #include "linux_compat.h"
57 #include "rio_linux.h"
84 static void RIOClearUp(struct Port *PortP);
86 /* Below belongs in func.h */
87 int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
90 extern struct rio_info *p;
93 int riotopen(struct tty_struct *tty, struct file *filp)
96 int repeat_this = 250;
97 struct Port *PortP; /* pointer to the port structure */
103 /* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
106 tty->driver_data = NULL;
108 SysPort = rio_minor(tty);
111 rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
116 rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
119 ** Validate that we have received a legitimate request.
120 ** Currently, just check that we are opening a port on
121 ** a host card that actually exists, and that the port
122 ** has been mapped onto a host.
124 if (SysPort >= RIO_PORTS) { /* out of range ? */
125 rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
131 ** Grab pointer to the port stucture
133 PortP = p->RIOPortp[SysPort]; /* Get control struc */
134 rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
135 if (!PortP->Mapped) { /* we aren't mapped yet! */
137 ** The system doesn't know which RTA this port
140 rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
145 tty->driver_data = PortP;
150 rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
152 retval = gs_init_port(&PortP->gs);
158 ** If the host hasn't been booted yet, then
161 if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
162 rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
168 ** If the RTA has not booted yet and the user has choosen to block
169 ** until the RTA is present then we must spin here waiting for
172 /* I find the above code a bit hairy. I find the below code
173 easier to read and shorter. Now, if it works too that would
176 rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
177 while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
178 if (!PortP->WaitUntilBooted) {
179 rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
184 /* Under Linux you'd normally use a wait instead of this
185 busy-waiting. I'll stick with the old implementation for
188 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
189 rio_dprintk(RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
193 if (repeat_this-- <= 0) {
194 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
199 rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
200 rio_spin_lock_irqsave(&PortP->portSem, flags);
206 ** If the port is in the final throws of being closed,
207 ** we should wait here (politely), waiting
208 ** for it to finish, so that it doesn't close us!
210 while ((PortP->State & RIO_CLOSING) && !p->RIOHalted) {
211 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RIO_CLOSING to go away\n");
212 if (repeat_this-- <= 0) {
213 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
214 RIOPreemptiveCmd(p, PortP, FCLOSE);
218 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
219 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
220 rio_spin_lock_irqsave(&PortP->portSem, flags);
224 rio_spin_lock_irqsave(&PortP->portSem, flags);
227 if (!PortP->Mapped) {
228 rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
229 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
240 ** 15.10.1998 ARG - ESIL 0761 part fix
241 ** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,
242 ** we need to make sure that the flags are clear when the port is opened.
244 /* Uh? Suppose I turn these on and then another process opens
245 the port again? The flags get cleared! Not good. -- REW */
246 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
247 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
250 if (!(PortP->firstOpen)) { /* First time ? */
251 rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");
255 PortP->CookMode = 0; /* XXX RIOCookMode(tp); */
256 PortP->InUse = NOT_INUSE;
258 /* Tentative fix for bug PR27. Didn't work. */
259 /* PortP->gs.xmit_cnt = 0; */
261 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
263 /* Someone explain to me why this delay/config is
264 here. If I read the docs correctly the "open"
265 command piggybacks the parameters immediately.
267 RIOParam(PortP, OPEN, 1, OK_TO_SLEEP); /* Open the port */
268 rio_spin_lock_irqsave(&PortP->portSem, flags);
271 ** wait for the port to be not closed.
273 while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
274 rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
275 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
276 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
277 rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
278 RIOPreemptiveCmd(p, PortP, FCLOSE);
282 rio_spin_lock_irqsave(&PortP->portSem, flags);
288 /* RIOClearUp( PortP ); */
289 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
292 rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
294 rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
297 ** insert test for carrier here. -- ???
298 ** I already see that test here. What's the deal? -- REW
300 if ((PortP->gs.tty->termios->c_cflag & CLOCAL) || (PortP->ModemState & MSVR1_CD)) {
301 rio_dprintk(RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
303 tp->tm.c_state |= CARR_ON;
304 wakeup((caddr_t) &tp->tm.c_canq);
306 PortP->State |= RIO_CARR_ON;
307 wake_up_interruptible(&PortP->gs.open_wait);
308 } else { /* no carrier - wait for DCD */
310 while (!(PortP->gs.tty->termios->c_state & CARR_ON) &&
311 !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
313 while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
314 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
316 PortP->gs.tty->termios->c_state |= WOPEN;
318 PortP->State |= RIO_WOPEN;
319 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
320 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
321 rio_spin_lock_irqsave(&PortP->portSem, flags);
323 ** ACTION: verify that this is a good thing
324 ** to do here. -- ???
325 ** I think it's OK. -- REW
327 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
328 RIOPreemptiveCmd(p, PortP, FCLOSE);
330 tp->tm.c_state &= ~WOPEN;
332 PortP->State &= ~RIO_WOPEN;
333 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
337 rio_spin_lock_irqsave(&PortP->portSem, flags);
339 PortP->State &= ~RIO_WOPEN;
343 rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
344 PortP->State |= RIO_MOPEN;
349 rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
352 ** Count opens for port statistics reporting
354 if (PortP->statsGather)
357 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
358 rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
364 ** RIOClose the port.
365 ** The operating system thinks that this is last close for the device.
366 ** As there are two interfaces to the port (Modem and tty), we need to
367 ** check that both are closed before we close the device.
369 int riotclose(void *ptr)
371 struct Port *PortP = ptr; /* pointer to the port structure */
373 int try = -1; /* Disable the timeouts by setting them to -1 */
374 int repeat_this = -1; /* Congrats to those having 15 years of
375 uptime! (You get to break the driver.) */
376 unsigned long end_time;
377 struct tty_struct *tty;
381 rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
383 /* PortP = p->RIOPortp[SysPort]; */
384 rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
385 /* tp = PortP->TtyP; *//* Get tty */
387 rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
389 if (PortP->gs.closing_wait)
390 end_time = jiffies + PortP->gs.closing_wait;
392 end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
394 rio_spin_lock_irqsave(&PortP->portSem, flags);
397 ** Setting this flag will make any process trying to open
398 ** this port block until we are complete closing it.
400 PortP->State |= RIO_CLOSING;
402 if ((PortP->State & RIO_DELETED)) {
403 rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
413 rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
415 ** clear the open bits for this device
417 PortP->State &= ~RIO_MOPEN;
418 PortP->State &= ~RIO_CARR_ON;
419 PortP->ModemState &= ~MSVR1_CD;
421 ** If the device was open as both a Modem and a tty line
422 ** then we need to wimp out here, as the port has not really
423 ** been finally closed (gee, whizz!) The test here uses the
424 ** bit for the OTHER mode of operation, to see if THAT is
427 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
429 ** The port is still open for the other task -
430 ** return, pretending that we are still active.
432 rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
433 PortP->State &= ~RIO_CLOSING;
434 if (PortP->firstOpen)
436 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
440 rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
442 PortP->State &= ~RIO_DYNOROD;
445 ** This is where we wait for the port
446 ** to drain down before closing. Bye-bye....
447 ** (We never meant to do this)
449 rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
452 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
453 if (repeat_this-- <= 0) {
455 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
456 RIOPreemptiveCmd(p, PortP, FCLOSE);
459 rio_dprintk(RIO_DEBUG_TTY, "Calling timeout to flush in closing\n");
460 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
461 if (RIODelay_ni(PortP, HUNDRED_MS * 10) == RIO_FAIL) {
462 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
464 rio_spin_lock_irqsave(&PortP->portSem, flags);
467 rio_spin_lock_irqsave(&PortP->portSem, flags);
470 PortP->TxBufferIn = PortP->TxBufferOut = 0;
474 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
476 ** The port has been re-opened for the other task -
477 ** return, pretending that we are still active.
479 rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
480 PortP->State &= ~RIO_CLOSING;
481 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
482 if (PortP->firstOpen)
492 /* Can't call RIOShortCommand with the port locked. */
493 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
495 if (RIOShortCommand(p, PortP, CLOSE, 1, 0) == RIO_FAIL) {
496 RIOPreemptiveCmd(p, PortP, FCLOSE);
497 rio_spin_lock_irqsave(&PortP->portSem, flags);
502 while (try && (PortP->PortState & PORT_ISOPEN)) {
504 if (time_after(jiffies, end_time)) {
505 rio_dprintk(RIO_DEBUG_TTY, "Run out of tries - force the bugger shut!\n");
506 RIOPreemptiveCmd(p, PortP, FCLOSE);
509 rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
513 rio_spin_lock_irqsave(&PortP->portSem, flags);
516 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
517 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
518 RIOPreemptiveCmd(p, PortP, FCLOSE);
522 rio_spin_lock_irqsave(&PortP->portSem, flags);
523 rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);
525 /* RIOPreemptiveCmd(p, PortP, FCLOSE); */
528 ** 15.10.1998 ARG - ESIL 0761 part fix
529 ** 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.
531 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
534 ** Count opens for port statistics reporting
536 if (PortP->statsGather)
540 /* XXX: Why would a "DELETED" flag be reset here? I'd have
541 thought that a "deleted" flag means that the port was
542 permanently gone, but here we can make it reappear by it
543 being in close during the "deletion".
545 PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
546 if (PortP->firstOpen)
548 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
549 rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
555 static void RIOClearUp(struct Port *PortP)
557 rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
558 PortP->Config = 0; /* Direct semaphore */
559 PortP->PortState = 0;
560 PortP->firstOpen = 0;
561 PortP->FlushCmdBodge = 0;
562 PortP->ModemState = PortP->CookMode = 0;
564 PortP->WflushFlag = 0;
565 PortP->MagicFlags = 0;
566 PortP->RxDataStart = 0;
567 PortP->TxBufferIn = 0;
568 PortP->TxBufferOut = 0;
572 ** Put a command onto a port.
573 ** The PortPointer, command, length and arg are passed.
574 ** The len is the length *inclusive* of the command byte,
575 ** and so for a command that takes no data, len==1.
576 ** The arg is a single byte, and is only used if len==2.
577 ** Other values of len aren't allowed, and will cause
580 int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
582 struct PKT __iomem *PacketP;
583 int retries = 20; /* at 10 per second -> 2 seconds */
586 rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
588 if (PortP->State & RIO_DELETED) {
589 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
592 rio_spin_lock_irqsave(&PortP->portSem, flags);
595 ** If the port is in use for pre-emptive command, then wait for it to
598 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
599 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
600 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
601 if (retries-- <= 0) {
604 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
607 rio_spin_lock_irqsave(&PortP->portSem, flags);
609 if (PortP->State & RIO_DELETED) {
610 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
611 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
615 while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
616 rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
617 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
618 if (retries-- <= 0) {
619 rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
622 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
625 rio_spin_lock_irqsave(&PortP->portSem, flags);
629 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
634 ** set the command byte and the argument byte
636 writeb(command, &PacketP->data[0]);
639 writeb(arg, &PacketP->data[1]);
642 ** set the length of the packet and set the command bit.
644 writeb(PKT_CMD_BIT | len, &PacketP->len);
648 ** Count characters transmitted for port statistics reporting
650 if (PortP->statsGather)
651 PortP->txchars += len;
653 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
654 return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;