blob: cfa54361473ffe05cb989be41987e0ffbefb04c8 [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*/
32#ifdef SCCS_LABELS
33static char *_riotty_c_sccs_ = "@(#)riotty.c 1.3";
34#endif
35
36
37#define __EXPLICIT_DEF_H__
38
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>
44#include <asm/io.h>
45#include <asm/system.h>
46#include <asm/string.h>
47#include <asm/semaphore.h>
48#include <asm/uaccess.h>
49
50#include <linux/termios.h>
51
52#include <linux/serial.h>
53
54#include <linux/generic_serial.h>
55
56
57#include "linux_compat.h"
58#include "rio_linux.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "pkt.h"
60#include "daemon.h"
61#include "rio.h"
62#include "riospace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "cmdpkt.h"
64#include "map.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include "rup.h"
66#include "port.h"
67#include "riodrvr.h"
68#include "rioinfo.h"
69#include "func.h"
70#include "errors.h"
71#include "pci.h"
72
73#include "parmmap.h"
74#include "unixrup.h"
75#include "board.h"
76#include "host.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include "phb.h"
78#include "link.h"
79#include "cmdblk.h"
80#include "route.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include "cirrus.h"
82#include "rioioctl.h"
83#include "param.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static void RIOClearUp(struct Port *PortP);
Alan Cox554b7c82006-03-24 03:18:32 -080086
87/* Below belongs in func.h */
Andrew Morton8d8706e2006-01-11 12:17:49 -080088int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091extern struct rio_info *p;
92
93
Andrew Morton8d8706e2006-01-11 12:17:49 -080094int riotopen(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Alan Coxe2b3afd2006-03-24 03:18:27 -080096 unsigned int SysPort;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int repeat_this = 250;
Andrew Morton8d8706e2006-01-11 12:17:49 -080098 struct Port *PortP; /* pointer to the port structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned long flags;
100 int retval = 0;
101
Andrew Morton8d8706e2006-01-11 12:17:49 -0800102 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
105 is going to oops.
Andrew Morton8d8706e2006-01-11 12:17:49 -0800106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 tty->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Andrew Morton8d8706e2006-01-11 12:17:49 -0800109 SysPort = rio_minor(tty);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800110
111 if (p->RIOFailed) {
112 rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
Andrew Morton8d8706e2006-01-11 12:17:49 -0800113 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -ENXIO;
115 }
116
Alan Cox554b7c82006-03-24 03:18:32 -0800117 rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800120 ** Validate that we have received a legitimate request.
121 ** Currently, just check that we are opening a port on
122 ** a host card that actually exists, and that the port
123 ** has been mapped onto a host.
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (SysPort >= RIO_PORTS) { /* out of range ? */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800126 rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 func_exit();
128 return -ENXIO;
129 }
130
131 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800132 ** Grab pointer to the port stucture
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 PortP = p->RIOPortp[SysPort]; /* Get control struc */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800135 rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
136 if (!PortP->Mapped) { /* we aren't mapped yet! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800138 ** The system doesn't know which RTA this port
139 ** corresponds to.
140 */
141 rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
142 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return -ENXIO;
144 }
145
146 tty->driver_data = PortP;
147
148 PortP->gs.tty = tty;
149 PortP->gs.count++;
150
Andrew Morton8d8706e2006-01-11 12:17:49 -0800151 rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Andrew Morton8d8706e2006-01-11 12:17:49 -0800153 retval = gs_init_port(&PortP->gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (retval) {
155 PortP->gs.count--;
156 return -ENXIO;
157 }
158 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800159 ** If the host hasn't been booted yet, then
160 ** fail
161 */
162 if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
163 rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
Andrew Morton8d8706e2006-01-11 12:17:49 -0800164 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -ENXIO;
166 }
167
168 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800169 ** If the RTA has not booted yet and the user has choosen to block
170 ** until the RTA is present then we must spin here waiting for
171 ** the RTA to boot.
172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* I find the above code a bit hairy. I find the below code
Andrew Morton8d8706e2006-01-11 12:17:49 -0800174 easier to read and shorter. Now, if it works too that would
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 be great... -- REW
Andrew Morton8d8706e2006-01-11 12:17:49 -0800176 */
177 rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800179 if (!PortP->WaitUntilBooted) {
180 rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
181 func_exit();
182 return -ENXIO;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Andrew Morton8d8706e2006-01-11 12:17:49 -0800185 /* Under Linux you'd normally use a wait instead of this
186 busy-waiting. I'll stick with the old implementation for
187 now. --REW
188 */
189 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
190 rio_dprintk(RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
191 func_exit();
192 return -EINTR;
193 }
194 if (repeat_this-- <= 0) {
195 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
196 func_exit();
197 return -EIO;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800200 rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800202 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto bombout;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800207 ** If the port is in the final throws of being closed,
208 ** we should wait here (politely), waiting
209 ** for it to finish, so that it doesn't close us!
210 */
211 while ((PortP->State & RIO_CLOSING) && !p->RIOHalted) {
212 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RIO_CLOSING to go away\n");
213 if (repeat_this-- <= 0) {
214 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
215 RIOPreemptiveCmd(p, PortP, FCLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 retval = -EINTR;
217 goto bombout;
218 }
219 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
220 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800221 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 retval = -EINTR;
223 goto bombout;
224 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800225 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
Andrew Morton8d8706e2006-01-11 12:17:49 -0800228 if (!PortP->Mapped) {
229 rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
231 retval = -ENXIO;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800232 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return retval;
234 }
235
Andrew Morton8d8706e2006-01-11 12:17:49 -0800236 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto bombout;
238 }
239
240/*
241** 15.10.1998 ARG - ESIL 0761 part fix
242** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,
243** we need to make sure that the flags are clear when the port is opened.
244*/
245 /* Uh? Suppose I turn these on and then another process opens
246 the port again? The flags get cleared! Not good. -- REW */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800247 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
248 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
251 if (!(PortP->firstOpen)) { /* First time ? */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800252 rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 PortP->firstOpen++;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800256 PortP->CookMode = 0; /* XXX RIOCookMode(tp); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 PortP->InUse = NOT_INUSE;
258
259 /* Tentative fix for bug PR27. Didn't work. */
260 /* PortP->gs.xmit_cnt = 0; */
261
262 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Someone explain to me why this delay/config is
Andrew Morton8d8706e2006-01-11 12:17:49 -0800265 here. If I read the docs correctly the "open"
266 command piggybacks the parameters immediately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 -- REW */
Alan Cox554b7c82006-03-24 03:18:32 -0800268 RIOParam(PortP, OPEN, 1, OK_TO_SLEEP); /* Open the port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 rio_spin_lock_irqsave(&PortP->portSem, flags);
270
271 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800272 ** wait for the port to be not closed.
273 */
274 while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
275 rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
277 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800278 rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
279 RIOPreemptiveCmd(p, PortP, FCLOSE);
280 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -EINTR;
282 }
283 rio_spin_lock_irqsave(&PortP->portSem, flags);
284 }
285
Andrew Morton8d8706e2006-01-11 12:17:49 -0800286 if (p->RIOHalted) {
287 retval = -EIO;
288 bombout:
289 /* RIOClearUp( PortP ); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
291 return retval;
292 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800293 rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Alan Cox554b7c82006-03-24 03:18:32 -0800295 rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
296 /*
297 ** ACTION
298 ** insert test for carrier here. -- ???
299 ** I already see that test here. What's the deal? -- REW
300 */
301 if ((PortP->gs.tty->termios->c_cflag & CLOCAL) || (PortP->ModemState & MSVR1_CD)) {
302 rio_dprintk(RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /*
Alan Cox554b7c82006-03-24 03:18:32 -0800304 tp->tm.c_state |= CARR_ON;
305 wakeup((caddr_t) &tp->tm.c_canq);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800306 */
Alan Cox554b7c82006-03-24 03:18:32 -0800307 PortP->State |= RIO_CARR_ON;
308 wake_up_interruptible(&PortP->gs.open_wait);
309 } else { /* no carrier - wait for DCD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
Alan Cox554b7c82006-03-24 03:18:32 -0800311 while (!(PortP->gs.tty->termios->c_state & CARR_ON) &&
312 !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
313 */
314 while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800315 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
Alan Cox554b7c82006-03-24 03:18:32 -0800316 /*
317 PortP->gs.tty->termios->c_state |= WOPEN;
318 */
319 PortP->State |= RIO_WOPEN;
320 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
321 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700322 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
Alan Cox554b7c82006-03-24 03:18:32 -0800324 ** ACTION: verify that this is a good thing
325 ** to do here. -- ???
326 ** I think it's OK. -- REW
Andrew Morton8d8706e2006-01-11 12:17:49 -0800327 */
Alan Cox554b7c82006-03-24 03:18:32 -0800328 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
329 RIOPreemptiveCmd(p, PortP, FCLOSE);
330 /*
331 tp->tm.c_state &= ~WOPEN;
332 */
333 PortP->State &= ~RIO_WOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Alan Cox554b7c82006-03-24 03:18:32 -0800335 func_exit();
336 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700338 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Alan Cox554b7c82006-03-24 03:18:32 -0800340 PortP->State &= ~RIO_WOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Alan Cox554b7c82006-03-24 03:18:32 -0800342 if (p->RIOHalted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto bombout;
Alan Cox554b7c82006-03-24 03:18:32 -0800344 rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
345 PortP->State |= RIO_MOPEN;
346
347 if (p->RIOHalted)
348 goto bombout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Andrew Morton8d8706e2006-01-11 12:17:49 -0800350 rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800353 ** Count opens for port statistics reporting
354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (PortP->statsGather)
356 PortP->opens++;
357
358 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800359 rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
360 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362}
363
364/*
365** RIOClose the port.
366** The operating system thinks that this is last close for the device.
367** As there are two interfaces to the port (Modem and tty), we need to
368** check that both are closed before we close the device.
Andrew Morton8d8706e2006-01-11 12:17:49 -0800369*/
370int riotclose(void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700372 struct Port *PortP = ptr; /* pointer to the port structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int deleted = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800374 int try = -1; /* Disable the timeouts by setting them to -1 */
375 int repeat_this = -1; /* Congrats to those having 15 years of
376 uptime! (You get to break the driver.) */
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700377 unsigned long end_time;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800378 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned long flags;
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700380 int rv = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800381
382 rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* PortP = p->RIOPortp[SysPort]; */
Alan Cox554b7c82006-03-24 03:18:32 -0800385 rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800386 /* tp = PortP->TtyP; *//* Get tty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 tty = PortP->gs.tty;
Alan Cox554b7c82006-03-24 03:18:32 -0800388 rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Andrew Morton8d8706e2006-01-11 12:17:49 -0800390 if (PortP->gs.closing_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 end_time = jiffies + PortP->gs.closing_wait;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800392 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 rio_spin_lock_irqsave(&PortP->portSem, flags);
396
397 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800398 ** Setting this flag will make any process trying to open
399 ** this port block until we are complete closing it.
400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 PortP->State |= RIO_CLOSING;
402
Andrew Morton8d8706e2006-01-11 12:17:49 -0800403 if ((PortP->State & RIO_DELETED)) {
404 rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 deleted = 1;
406 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800407
408 if (p->RIOHalted) {
409 RIOClearUp(PortP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 rv = -EIO;
411 goto close_end;
412 }
413
Andrew Morton8d8706e2006-01-11 12:17:49 -0800414 rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800416 ** clear the open bits for this device
417 */
Alan Cox554b7c82006-03-24 03:18:32 -0800418 PortP->State &= ~RIO_MOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 PortP->State &= ~RIO_CARR_ON;
420 PortP->ModemState &= ~MSVR1_CD;
421 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800422 ** If the device was open as both a Modem and a tty line
423 ** then we need to wimp out here, as the port has not really
424 ** been finally closed (gee, whizz!) The test here uses the
425 ** bit for the OTHER mode of operation, to see if THAT is
426 ** still active!
427 */
428 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800430 ** The port is still open for the other task -
431 ** return, pretending that we are still active.
432 */
433 rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 PortP->State &= ~RIO_CLOSING;
435 if (PortP->firstOpen)
436 PortP->firstOpen--;
437 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
438 return -EIO;
439 }
440
Andrew Morton8d8706e2006-01-11 12:17:49 -0800441 rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 PortP->State &= ~RIO_DYNOROD;
444
445 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800446 ** This is where we wait for the port
447 ** to drain down before closing. Bye-bye....
448 ** (We never meant to do this)
449 */
450 rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (!deleted)
Andrew Morton8d8706e2006-01-11 12:17:49 -0800453 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800454 if (repeat_this-- <= 0) {
455 rv = -EINTR;
456 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
457 RIOPreemptiveCmd(p, PortP, FCLOSE);
458 goto close_end;
459 }
460 rio_dprintk(RIO_DEBUG_TTY, "Calling timeout to flush in closing\n");
461 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
462 if (RIODelay_ni(PortP, HUNDRED_MS * 10) == RIO_FAIL) {
463 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
464 rv = -EINTR;
465 rio_spin_lock_irqsave(&PortP->portSem, flags);
466 goto close_end;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 PortP->TxBufferIn = PortP->TxBufferOut = 0;
472 repeat_this = 0xff;
473
474 PortP->InUse = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800475 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800477 ** The port has been re-opened for the other task -
478 ** return, pretending that we are still active.
479 */
480 rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 PortP->State &= ~RIO_CLOSING;
482 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
483 if (PortP->firstOpen)
484 PortP->firstOpen--;
485 return -EIO;
486 }
487
Andrew Morton8d8706e2006-01-11 12:17:49 -0800488 if (p->RIOHalted) {
489 RIOClearUp(PortP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto close_end;
491 }
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Can't call RIOShortCommand with the port locked. */
494 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
495
496 if (RIOShortCommand(p, PortP, CLOSE, 1, 0) == RIO_FAIL) {
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700497 RIOPreemptiveCmd(p, PortP, FCLOSE);
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700498 rio_spin_lock_irqsave(&PortP->portSem, flags);
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700499 goto close_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 if (!deleted)
Andrew Morton8d8706e2006-01-11 12:17:49 -0800503 while (try && (PortP->PortState & PORT_ISOPEN)) {
504 try--;
505 if (time_after(jiffies, end_time)) {
506 rio_dprintk(RIO_DEBUG_TTY, "Run out of tries - force the bugger shut!\n");
507 RIOPreemptiveCmd(p, PortP, FCLOSE);
508 break;
509 }
510 rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Andrew Morton8d8706e2006-01-11 12:17:49 -0800512 if (p->RIOHalted) {
513 RIOClearUp(PortP);
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700514 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800515 goto close_end;
516 }
517 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
518 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
519 RIOPreemptiveCmd(p, PortP, FCLOSE);
520 break;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800524 rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* RIOPreemptiveCmd(p, PortP, FCLOSE); */
527
528/*
529** 15.10.1998 ARG - ESIL 0761 part fix
530** 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*/
Andrew Morton8d8706e2006-01-11 12:17:49 -0800532 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800535 ** Count opens for port statistics reporting
536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (PortP->statsGather)
538 PortP->closes++;
539
Alan Cox554b7c82006-03-24 03:18:32 -0800540close_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* XXX: Why would a "DELETED" flag be reset here? I'd have
542 thought that a "deleted" flag means that the port was
543 permanently gone, but here we can make it reappear by it
544 being in close during the "deletion".
Andrew Morton8d8706e2006-01-11 12:17:49 -0800545 */
546 PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (PortP->firstOpen)
548 PortP->firstOpen--;
549 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800550 rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return rv;
552}
553
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Alan Cox554b7c82006-03-24 03:18:32 -0800556static void RIOClearUp(struct Port *PortP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Andrew Morton8d8706e2006-01-11 12:17:49 -0800558 rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
559 PortP->Config = 0; /* Direct semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 PortP->PortState = 0;
561 PortP->firstOpen = 0;
562 PortP->FlushCmdBodge = 0;
563 PortP->ModemState = PortP->CookMode = 0;
564 PortP->Mapped = 0;
565 PortP->WflushFlag = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800566 PortP->MagicFlags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 PortP->RxDataStart = 0;
568 PortP->TxBufferIn = 0;
569 PortP->TxBufferOut = 0;
570}
571
572/*
573** Put a command onto a port.
574** The PortPointer, command, length and arg are passed.
575** The len is the length *inclusive* of the command byte,
576** and so for a command that takes no data, len==1.
577** The arg is a single byte, and is only used if len==2.
578** Other values of len aren't allowed, and will cause
579** a panic.
580*/
Andrew Morton8d8706e2006-01-11 12:17:49 -0800581int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Al Virod886cb52006-05-27 00:08:25 -0400583 struct PKT __iomem *PacketP;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800584 int retries = 20; /* at 10 per second -> 2 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 unsigned long flags;
586
Andrew Morton8d8706e2006-01-11 12:17:49 -0800587 rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Andrew Morton8d8706e2006-01-11 12:17:49 -0800589 if (PortP->State & RIO_DELETED) {
590 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return RIO_FAIL;
592 }
593 rio_spin_lock_irqsave(&PortP->portSem, flags);
594
595 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800596 ** If the port is in use for pre-emptive command, then wait for it to
597 ** be free again.
598 */
599 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
600 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
602 if (retries-- <= 0) {
603 return RIO_FAIL;
604 }
605 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
606 return RIO_FAIL;
607 }
608 rio_spin_lock_irqsave(&PortP->portSem, flags);
609 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800610 if (PortP->State & RIO_DELETED) {
611 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
613 return RIO_FAIL;
614 }
615
Andrew Morton8d8706e2006-01-11 12:17:49 -0800616 while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
617 rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
619 if (retries-- <= 0) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800620 rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return RIO_FAIL;
622 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800623 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return RIO_FAIL;
625 }
626 rio_spin_lock_irqsave(&PortP->portSem, flags);
627 }
628
Andrew Morton8d8706e2006-01-11 12:17:49 -0800629 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
631 return RIO_FAIL;
632 }
633
634 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800635 ** set the command byte and the argument byte
636 */
Alan Coxe2b3afd2006-03-24 03:18:27 -0800637 writeb(command, &PacketP->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Andrew Morton8d8706e2006-01-11 12:17:49 -0800639 if (len == 2)
Alan Coxe2b3afd2006-03-24 03:18:27 -0800640 writeb(arg, &PacketP->data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800643 ** set the length of the packet and set the command bit.
644 */
Alan Coxe2b3afd2006-03-24 03:18:27 -0800645 writeb(PKT_CMD_BIT | len, &PacketP->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 add_transmit(PortP);
648 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800649 ** Count characters transmitted for port statistics reporting
650 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (PortP->statsGather)
652 PortP->txchars += len;
653
654 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
655 return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;
656}
657
658