pty: simplify unix98 allocation
[linux-2.6/sactl.git] / drivers / char / rio / riotty.c
blob2fb49e89b3241184eb1e236e2412f1663810b3d4
1 /*
2 ** -----------------------------------------------------------------------------
3 **
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.
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
28 ** ident @(#)riotty.c 1.3
30 ** -----------------------------------------------------------------------------
33 #define __EXPLICIT_DEF_H__
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>
43 #include <asm/uaccess.h>
45 #include <linux/termios.h>
47 #include <linux/serial.h>
49 #include <linux/generic_serial.h>
52 #include "linux_compat.h"
53 #include "rio_linux.h"
54 #include "pkt.h"
55 #include "daemon.h"
56 #include "rio.h"
57 #include "riospace.h"
58 #include "cmdpkt.h"
59 #include "map.h"
60 #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"
68 #include "parmmap.h"
69 #include "unixrup.h"
70 #include "board.h"
71 #include "host.h"
72 #include "phb.h"
73 #include "link.h"
74 #include "cmdblk.h"
75 #include "route.h"
76 #include "cirrus.h"
77 #include "rioioctl.h"
78 #include "param.h"
80 static void RIOClearUp(struct Port *PortP);
82 /* Below belongs in func.h */
83 int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
86 extern struct rio_info *p;
89 int riotopen(struct tty_struct *tty, struct file *filp)
91 unsigned int SysPort;
92 int repeat_this = 250;
93 struct Port *PortP; /* pointer to the port structure */
94 unsigned long flags;
95 int retval = 0;
97 func_enter();
99 /* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
100 is going to oops.
102 tty->driver_data = NULL;
104 SysPort = rio_minor(tty);
106 if (p->RIOFailed) {
107 rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
108 func_exit();
109 return -ENXIO;
112 rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
115 ** 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.
120 if (SysPort >= RIO_PORTS) { /* out of range ? */
121 rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
122 func_exit();
123 return -ENXIO;
127 ** Grab pointer to the port stucture
129 PortP = p->RIOPortp[SysPort]; /* Get control struc */
130 rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
131 if (!PortP->Mapped) { /* we aren't mapped yet! */
133 ** The system doesn't know which RTA this port
134 ** corresponds to.
136 rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
137 func_exit();
138 return -ENXIO;
141 tty->driver_data = PortP;
143 PortP->gs.port.tty = tty;
144 PortP->gs.port.count++;
146 rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
148 retval = gs_init_port(&PortP->gs);
149 if (retval) {
150 PortP->gs.port.count--;
151 return -ENXIO;
154 ** If the host hasn't been booted yet, then
155 ** fail
157 if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
158 rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
159 func_exit();
160 return -ENXIO;
164 ** 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.
168 /* I find the above code a bit hairy. I find the below code
169 easier to read and shorter. Now, if it works too that would
170 be great... -- REW
172 rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
173 while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
174 if (!PortP->WaitUntilBooted) {
175 rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
176 func_exit();
177 return -ENXIO;
180 /* 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
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;
189 if (repeat_this-- <= 0) {
190 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
191 func_exit();
192 return -EIO;
195 rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
196 rio_spin_lock_irqsave(&PortP->portSem, flags);
197 if (p->RIOHalted) {
198 goto bombout;
202 ** 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!
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");
210 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
211 retval = -EINTR;
212 goto bombout;
214 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
215 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
216 rio_spin_lock_irqsave(&PortP->portSem, flags);
217 retval = -EINTR;
218 goto bombout;
220 rio_spin_lock_irqsave(&PortP->portSem, flags);
223 if (!PortP->Mapped) {
224 rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
225 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
226 retval = -ENXIO;
227 func_exit();
228 return retval;
231 if (p->RIOHalted) {
232 goto bombout;
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.
240 /* Uh? Suppose I turn these on and then another process opens
241 the port again? The flags get cleared! Not good. -- REW */
242 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
243 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
246 if (!(PortP->firstOpen)) { /* First time ? */
247 rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");
250 PortP->firstOpen++;
251 PortP->CookMode = 0; /* XXX RIOCookMode(tp); */
252 PortP->InUse = NOT_INUSE;
254 /* Tentative fix for bug PR27. Didn't work. */
255 /* PortP->gs.xmit_cnt = 0; */
257 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
259 /* Someone explain to me why this delay/config is
260 here. If I read the docs correctly the "open"
261 command piggybacks the parameters immediately.
262 -- REW */
263 RIOParam(PortP, RIOC_OPEN, 1, OK_TO_SLEEP); /* Open the port */
264 rio_spin_lock_irqsave(&PortP->portSem, flags);
267 ** wait for the port to be not closed.
269 while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
270 rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
271 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
272 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
273 rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
274 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
275 func_exit();
276 return -EINTR;
278 rio_spin_lock_irqsave(&PortP->portSem, flags);
281 if (p->RIOHalted) {
282 retval = -EIO;
283 bombout:
284 /* RIOClearUp( PortP ); */
285 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
286 return retval;
288 rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
290 rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
292 ** ACTION
293 ** insert test for carrier here. -- ???
294 ** I already see that test here. What's the deal? -- REW
296 if ((PortP->gs.port.tty->termios->c_cflag & CLOCAL) ||
297 (PortP->ModemState & RIOC_MSVR1_CD)) {
298 rio_dprintk(RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
300 tp->tm.c_state |= CARR_ON;
301 wakeup((caddr_t) &tp->tm.c_canq);
303 PortP->State |= RIO_CARR_ON;
304 wake_up_interruptible(&PortP->gs.port.open_wait);
305 } else { /* no carrier - wait for DCD */
307 while (!(PortP->gs.port.tty->termios->c_state & CARR_ON) &&
308 !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
310 while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
311 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
313 PortP->gs.port.tty->termios->c_state |= WOPEN;
315 PortP->State |= RIO_WOPEN;
316 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
317 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
318 rio_spin_lock_irqsave(&PortP->portSem, flags);
320 ** ACTION: verify that this is a good thing
321 ** to do here. -- ???
322 ** I think it's OK. -- REW
324 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
325 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
327 tp->tm.c_state &= ~WOPEN;
329 PortP->State &= ~RIO_WOPEN;
330 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
331 func_exit();
332 return -EINTR;
334 rio_spin_lock_irqsave(&PortP->portSem, flags);
336 PortP->State &= ~RIO_WOPEN;
338 if (p->RIOHalted)
339 goto bombout;
340 rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
341 PortP->State |= RIO_MOPEN;
343 if (p->RIOHalted)
344 goto bombout;
346 rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
349 ** Count opens for port statistics reporting
351 if (PortP->statsGather)
352 PortP->opens++;
354 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
355 rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
356 func_exit();
357 return 0;
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.
366 int riotclose(void *ptr)
368 struct Port *PortP = ptr; /* pointer to the port structure */
369 int deleted = 0;
370 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.) */
373 unsigned long end_time;
374 struct tty_struct *tty;
375 unsigned long flags;
376 int rv = 0;
378 rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
380 /* PortP = p->RIOPortp[SysPort]; */
381 rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
382 /* tp = PortP->TtyP; *//* Get tty */
383 tty = PortP->gs.port.tty;
384 rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
386 if (PortP->gs.closing_wait)
387 end_time = jiffies + PortP->gs.closing_wait;
388 else
389 end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
391 rio_spin_lock_irqsave(&PortP->portSem, flags);
394 ** Setting this flag will make any process trying to open
395 ** this port block until we are complete closing it.
397 PortP->State |= RIO_CLOSING;
399 if ((PortP->State & RIO_DELETED)) {
400 rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
401 deleted = 1;
404 if (p->RIOHalted) {
405 RIOClearUp(PortP);
406 rv = -EIO;
407 goto close_end;
410 rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
412 ** clear the open bits for this device
414 PortP->State &= ~RIO_MOPEN;
415 PortP->State &= ~RIO_CARR_ON;
416 PortP->ModemState &= ~RIOC_MSVR1_CD;
418 ** 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!
424 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
426 ** The port is still open for the other task -
427 ** return, pretending that we are still active.
429 rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
430 PortP->State &= ~RIO_CLOSING;
431 if (PortP->firstOpen)
432 PortP->firstOpen--;
433 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
434 return -EIO;
437 rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
439 PortP->State &= ~RIO_DYNOROD;
442 ** This is where we wait for the port
443 ** to drain down before closing. Bye-bye....
444 ** (We never meant to do this)
446 rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
448 if (!deleted)
449 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
450 if (repeat_this-- <= 0) {
451 rv = -EINTR;
452 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
453 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
454 goto close_end;
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;
464 rio_spin_lock_irqsave(&PortP->portSem, flags);
467 PortP->TxBufferIn = PortP->TxBufferOut = 0;
468 repeat_this = 0xff;
470 PortP->InUse = 0;
471 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
473 ** The port has been re-opened for the other task -
474 ** return, pretending that we are still active.
476 rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
477 PortP->State &= ~RIO_CLOSING;
478 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
479 if (PortP->firstOpen)
480 PortP->firstOpen--;
481 return -EIO;
484 if (p->RIOHalted) {
485 RIOClearUp(PortP);
486 goto close_end;
489 /* Can't call RIOShortCommand with the port locked. */
490 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
492 if (RIOShortCommand(p, PortP, RIOC_CLOSE, 1, 0) == RIO_FAIL) {
493 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
494 rio_spin_lock_irqsave(&PortP->portSem, flags);
495 goto close_end;
498 if (!deleted)
499 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");
503 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
504 break;
506 rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
508 if (p->RIOHalted) {
509 RIOClearUp(PortP);
510 rio_spin_lock_irqsave(&PortP->portSem, flags);
511 goto close_end;
513 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
514 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
515 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
516 break;
519 rio_spin_lock_irqsave(&PortP->portSem, flags);
520 rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);
522 /* RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE); */
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.
528 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
531 ** Count opens for port statistics reporting
533 if (PortP->statsGather)
534 PortP->closes++;
536 close_end:
537 /* 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".
542 PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
543 if (PortP->firstOpen)
544 PortP->firstOpen--;
545 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
546 rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
547 return rv;
552 static void RIOClearUp(struct Port *PortP)
554 rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
555 PortP->Config = 0; /* Direct semaphore */
556 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;
562 PortP->MagicFlags = 0;
563 PortP->RxDataStart = 0;
564 PortP->TxBufferIn = 0;
565 PortP->TxBufferOut = 0;
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.
577 int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
579 struct PKT __iomem *PacketP;
580 int retries = 20; /* at 10 per second -> 2 seconds */
581 unsigned long flags;
583 rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
585 if (PortP->State & RIO_DELETED) {
586 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
587 return RIO_FAIL;
589 rio_spin_lock_irqsave(&PortP->portSem, flags);
592 ** If the port is in use for pre-emptive command, then wait for it to
593 ** be free again.
595 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
596 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
597 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
598 if (retries-- <= 0) {
599 return RIO_FAIL;
601 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
602 return RIO_FAIL;
604 rio_spin_lock_irqsave(&PortP->portSem, flags);
606 if (PortP->State & RIO_DELETED) {
607 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
608 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
609 return RIO_FAIL;
612 while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
613 rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
614 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
615 if (retries-- <= 0) {
616 rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
617 return RIO_FAIL;
619 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
620 return RIO_FAIL;
622 rio_spin_lock_irqsave(&PortP->portSem, flags);
625 if (p->RIOHalted) {
626 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
627 return RIO_FAIL;
631 ** set the command byte and the argument byte
633 writeb(command, &PacketP->data[0]);
635 if (len == 2)
636 writeb(arg, &PacketP->data[1]);
639 ** set the length of the packet and set the command bit.
641 writeb(PKT_CMD_BIT | len, &PacketP->len);
643 add_transmit(PortP);
645 ** Count characters transmitted for port statistics reporting
647 if (PortP->statsGather)
648 PortP->txchars += len;
650 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
651 return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;