blob: 1cb8580a161dc72b1403b13fff81e862748ab239 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
48
49#include <linux/termios.h>
50
51#include <linux/serial.h>
52
53#include <linux/generic_serial.h>
54
55
56#include "linux_compat.h"
57#include "rio_linux.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include "pkt.h"
59#include "daemon.h"
60#include "rio.h"
61#include "riospace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "cmdpkt.h"
63#include "map.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "rup.h"
65#include "port.h"
66#include "riodrvr.h"
67#include "rioinfo.h"
68#include "func.h"
69#include "errors.h"
70#include "pci.h"
71
72#include "parmmap.h"
73#include "unixrup.h"
74#include "board.h"
75#include "host.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include "phb.h"
77#include "link.h"
78#include "cmdblk.h"
79#include "route.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include "cirrus.h"
81#include "rioioctl.h"
82#include "param.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static void RIOClearUp(struct Port *PortP);
Alan Cox554b7c82006-03-24 03:18:32 -080085
86/* Below belongs in func.h */
Andrew Morton8d8706e2006-01-11 12:17:49 -080087int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090extern struct rio_info *p;
91
92
Andrew Morton8d8706e2006-01-11 12:17:49 -080093int riotopen(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Alan Coxe2b3afd2006-03-24 03:18:27 -080095 unsigned int SysPort;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int repeat_this = 250;
Andrew Morton8d8706e2006-01-11 12:17:49 -080097 struct Port *PortP; /* pointer to the port structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned long flags;
99 int retval = 0;
100
Andrew Morton8d8706e2006-01-11 12:17:49 -0800101 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
104 is going to oops.
Andrew Morton8d8706e2006-01-11 12:17:49 -0800105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 tty->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Andrew Morton8d8706e2006-01-11 12:17:49 -0800108 SysPort = rio_minor(tty);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800109
110 if (p->RIOFailed) {
111 rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
Andrew Morton8d8706e2006-01-11 12:17:49 -0800112 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -ENXIO;
114 }
115
Alan Cox554b7c82006-03-24 03:18:32 -0800116 rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800119 ** 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.
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (SysPort >= RIO_PORTS) { /* out of range ? */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800125 rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 func_exit();
127 return -ENXIO;
128 }
129
130 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800131 ** Grab pointer to the port stucture
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 PortP = p->RIOPortp[SysPort]; /* Get control struc */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800134 rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
135 if (!PortP->Mapped) { /* we aren't mapped yet! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800137 ** The system doesn't know which RTA this port
138 ** corresponds to.
139 */
140 rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
141 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -ENXIO;
143 }
144
145 tty->driver_data = PortP;
146
147 PortP->gs.tty = tty;
148 PortP->gs.count++;
149
Andrew Morton8d8706e2006-01-11 12:17:49 -0800150 rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Andrew Morton8d8706e2006-01-11 12:17:49 -0800152 retval = gs_init_port(&PortP->gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (retval) {
154 PortP->gs.count--;
155 return -ENXIO;
156 }
157 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800158 ** If the host hasn't been booted yet, then
159 ** fail
160 */
161 if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
162 rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
Andrew Morton8d8706e2006-01-11 12:17:49 -0800163 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ENXIO;
165 }
166
167 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800168 ** 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
170 ** the RTA to boot.
171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* I find the above code a bit hairy. I find the below code
Andrew Morton8d8706e2006-01-11 12:17:49 -0800173 easier to read and shorter. Now, if it works too that would
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 be great... -- REW
Andrew Morton8d8706e2006-01-11 12:17:49 -0800175 */
176 rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800178 if (!PortP->WaitUntilBooted) {
179 rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
180 func_exit();
181 return -ENXIO;
182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Andrew Morton8d8706e2006-01-11 12:17:49 -0800184 /* Under Linux you'd normally use a wait instead of this
185 busy-waiting. I'll stick with the old implementation for
186 now. --REW
187 */
188 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
189 rio_dprintk(RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
190 func_exit();
191 return -EINTR;
192 }
193 if (repeat_this-- <= 0) {
194 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
195 func_exit();
196 return -EIO;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800199 rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800201 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto bombout;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800206 ** 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!
209 */
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 retval = -EINTR;
216 goto bombout;
217 }
218 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
219 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800220 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 retval = -EINTR;
222 goto bombout;
223 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800224 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
Andrew Morton8d8706e2006-01-11 12:17:49 -0800227 if (!PortP->Mapped) {
228 rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
230 retval = -ENXIO;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800231 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return retval;
233 }
234
Andrew Morton8d8706e2006-01-11 12:17:49 -0800235 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto bombout;
237 }
238
239/*
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.
243*/
244 /* Uh? Suppose I turn these on and then another process opens
245 the port again? The flags get cleared! Not good. -- REW */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800246 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
247 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
250 if (!(PortP->firstOpen)) { /* First time ? */
Andrew Morton8d8706e2006-01-11 12:17:49 -0800251 rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 PortP->firstOpen++;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800255 PortP->CookMode = 0; /* XXX RIOCookMode(tp); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 PortP->InUse = NOT_INUSE;
257
258 /* Tentative fix for bug PR27. Didn't work. */
259 /* PortP->gs.xmit_cnt = 0; */
260
261 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* Someone explain to me why this delay/config is
Andrew Morton8d8706e2006-01-11 12:17:49 -0800264 here. If I read the docs correctly the "open"
265 command piggybacks the parameters immediately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 -- REW */
Alan Cox554b7c82006-03-24 03:18:32 -0800267 RIOParam(PortP, OPEN, 1, OK_TO_SLEEP); /* Open the port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 rio_spin_lock_irqsave(&PortP->portSem, flags);
269
270 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800271 ** wait for the port to be not closed.
272 */
273 while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
274 rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
276 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800277 rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
278 RIOPreemptiveCmd(p, PortP, FCLOSE);
279 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -EINTR;
281 }
282 rio_spin_lock_irqsave(&PortP->portSem, flags);
283 }
284
Andrew Morton8d8706e2006-01-11 12:17:49 -0800285 if (p->RIOHalted) {
286 retval = -EIO;
287 bombout:
288 /* RIOClearUp( PortP ); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
290 return retval;
291 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800292 rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Alan Cox554b7c82006-03-24 03:18:32 -0800294 rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
295 /*
296 ** ACTION
297 ** insert test for carrier here. -- ???
298 ** I already see that test here. What's the deal? -- REW
299 */
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /*
Alan Cox554b7c82006-03-24 03:18:32 -0800303 tp->tm.c_state |= CARR_ON;
304 wakeup((caddr_t) &tp->tm.c_canq);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800305 */
Alan Cox554b7c82006-03-24 03:18:32 -0800306 PortP->State |= RIO_CARR_ON;
307 wake_up_interruptible(&PortP->gs.open_wait);
308 } else { /* no carrier - wait for DCD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /*
Alan Cox554b7c82006-03-24 03:18:32 -0800310 while (!(PortP->gs.tty->termios->c_state & CARR_ON) &&
311 !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
312 */
313 while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800314 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
Alan Cox554b7c82006-03-24 03:18:32 -0800315 /*
316 PortP->gs.tty->termios->c_state |= WOPEN;
317 */
318 PortP->State |= RIO_WOPEN;
319 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
320 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700321 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /*
Alan Cox554b7c82006-03-24 03:18:32 -0800323 ** ACTION: verify that this is a good thing
324 ** to do here. -- ???
325 ** I think it's OK. -- REW
Andrew Morton8d8706e2006-01-11 12:17:49 -0800326 */
Alan Cox554b7c82006-03-24 03:18:32 -0800327 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
328 RIOPreemptiveCmd(p, PortP, FCLOSE);
329 /*
330 tp->tm.c_state &= ~WOPEN;
331 */
332 PortP->State &= ~RIO_WOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Alan Cox554b7c82006-03-24 03:18:32 -0800334 func_exit();
335 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700337 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Alan Cox554b7c82006-03-24 03:18:32 -0800339 PortP->State &= ~RIO_WOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Alan Cox554b7c82006-03-24 03:18:32 -0800341 if (p->RIOHalted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto bombout;
Alan Cox554b7c82006-03-24 03:18:32 -0800343 rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
344 PortP->State |= RIO_MOPEN;
345
346 if (p->RIOHalted)
347 goto bombout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Andrew Morton8d8706e2006-01-11 12:17:49 -0800349 rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800352 ** Count opens for port statistics reporting
353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (PortP->statsGather)
355 PortP->opens++;
356
357 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800358 rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
359 func_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
362
363/*
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.
Andrew Morton8d8706e2006-01-11 12:17:49 -0800368*/
369int riotclose(void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700371 struct Port *PortP = ptr; /* pointer to the port structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int deleted = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800373 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.) */
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700376 unsigned long end_time;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800377 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 unsigned long flags;
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700379 int rv = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800380
381 rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* PortP = p->RIOPortp[SysPort]; */
Alan Cox554b7c82006-03-24 03:18:32 -0800384 rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800385 /* tp = PortP->TtyP; *//* Get tty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 tty = PortP->gs.tty;
Alan Cox554b7c82006-03-24 03:18:32 -0800387 rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Andrew Morton8d8706e2006-01-11 12:17:49 -0800389 if (PortP->gs.closing_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 end_time = jiffies + PortP->gs.closing_wait;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800391 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 rio_spin_lock_irqsave(&PortP->portSem, flags);
395
396 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800397 ** Setting this flag will make any process trying to open
398 ** this port block until we are complete closing it.
399 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 PortP->State |= RIO_CLOSING;
401
Andrew Morton8d8706e2006-01-11 12:17:49 -0800402 if ((PortP->State & RIO_DELETED)) {
403 rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 deleted = 1;
405 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800406
407 if (p->RIOHalted) {
408 RIOClearUp(PortP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rv = -EIO;
410 goto close_end;
411 }
412
Andrew Morton8d8706e2006-01-11 12:17:49 -0800413 rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800415 ** clear the open bits for this device
416 */
Alan Cox554b7c82006-03-24 03:18:32 -0800417 PortP->State &= ~RIO_MOPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 PortP->State &= ~RIO_CARR_ON;
419 PortP->ModemState &= ~MSVR1_CD;
420 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800421 ** 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
425 ** still active!
426 */
427 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800429 ** The port is still open for the other task -
430 ** return, pretending that we are still active.
431 */
432 rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 PortP->State &= ~RIO_CLOSING;
434 if (PortP->firstOpen)
435 PortP->firstOpen--;
436 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
437 return -EIO;
438 }
439
Andrew Morton8d8706e2006-01-11 12:17:49 -0800440 rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 PortP->State &= ~RIO_DYNOROD;
443
444 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800445 ** This is where we wait for the port
446 ** to drain down before closing. Bye-bye....
447 ** (We never meant to do this)
448 */
449 rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (!deleted)
Andrew Morton8d8706e2006-01-11 12:17:49 -0800452 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800453 if (repeat_this-- <= 0) {
454 rv = -EINTR;
455 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
456 RIOPreemptiveCmd(p, PortP, FCLOSE);
457 goto close_end;
458 }
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");
463 rv = -EINTR;
464 rio_spin_lock_irqsave(&PortP->portSem, flags);
465 goto close_end;
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 rio_spin_lock_irqsave(&PortP->portSem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 PortP->TxBufferIn = PortP->TxBufferOut = 0;
471 repeat_this = 0xff;
472
473 PortP->InUse = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800474 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800476 ** The port has been re-opened for the other task -
477 ** return, pretending that we are still active.
478 */
479 rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 PortP->State &= ~RIO_CLOSING;
481 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
482 if (PortP->firstOpen)
483 PortP->firstOpen--;
484 return -EIO;
485 }
486
Andrew Morton8d8706e2006-01-11 12:17:49 -0800487 if (p->RIOHalted) {
488 RIOClearUp(PortP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto close_end;
490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Can't call RIOShortCommand with the port locked. */
493 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
494
495 if (RIOShortCommand(p, PortP, CLOSE, 1, 0) == RIO_FAIL) {
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700496 RIOPreemptiveCmd(p, PortP, FCLOSE);
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700497 rio_spin_lock_irqsave(&PortP->portSem, flags);
M.Baris Demirayd2a457c2005-06-25 14:58:40 -0700498 goto close_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 if (!deleted)
Andrew Morton8d8706e2006-01-11 12:17:49 -0800502 while (try && (PortP->PortState & PORT_ISOPEN)) {
503 try--;
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);
507 break;
508 }
509 rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Andrew Morton8d8706e2006-01-11 12:17:49 -0800511 if (p->RIOHalted) {
512 RIOClearUp(PortP);
Jiri Slaby212e7bb2008-04-02 13:04:48 -0700513 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800514 goto close_end;
515 }
516 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
517 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
518 RIOPreemptiveCmd(p, PortP, FCLOSE);
519 break;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rio_spin_lock_irqsave(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800523 rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* RIOPreemptiveCmd(p, PortP, FCLOSE); */
526
527/*
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.
530*/
Andrew Morton8d8706e2006-01-11 12:17:49 -0800531 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800534 ** Count opens for port statistics reporting
535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (PortP->statsGather)
537 PortP->closes++;
538
Alan Cox554b7c82006-03-24 03:18:32 -0800539close_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* 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".
Andrew Morton8d8706e2006-01-11 12:17:49 -0800544 */
545 PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (PortP->firstOpen)
547 PortP->firstOpen--;
548 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
Andrew Morton8d8706e2006-01-11 12:17:49 -0800549 rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return rv;
551}
552
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Alan Cox554b7c82006-03-24 03:18:32 -0800555static void RIOClearUp(struct Port *PortP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Andrew Morton8d8706e2006-01-11 12:17:49 -0800557 rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
558 PortP->Config = 0; /* Direct semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 PortP->PortState = 0;
560 PortP->firstOpen = 0;
561 PortP->FlushCmdBodge = 0;
562 PortP->ModemState = PortP->CookMode = 0;
563 PortP->Mapped = 0;
564 PortP->WflushFlag = 0;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800565 PortP->MagicFlags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 PortP->RxDataStart = 0;
567 PortP->TxBufferIn = 0;
568 PortP->TxBufferOut = 0;
569}
570
571/*
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
578** a panic.
579*/
Andrew Morton8d8706e2006-01-11 12:17:49 -0800580int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Al Virod886cb52006-05-27 00:08:25 -0400582 struct PKT __iomem *PacketP;
Andrew Morton8d8706e2006-01-11 12:17:49 -0800583 int retries = 20; /* at 10 per second -> 2 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned long flags;
585
Andrew Morton8d8706e2006-01-11 12:17:49 -0800586 rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Andrew Morton8d8706e2006-01-11 12:17:49 -0800588 if (PortP->State & RIO_DELETED) {
589 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return RIO_FAIL;
591 }
592 rio_spin_lock_irqsave(&PortP->portSem, flags);
593
594 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800595 ** If the port is in use for pre-emptive command, then wait for it to
596 ** be free again.
597 */
598 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
599 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
601 if (retries-- <= 0) {
602 return RIO_FAIL;
603 }
604 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
605 return RIO_FAIL;
606 }
607 rio_spin_lock_irqsave(&PortP->portSem, flags);
608 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800609 if (PortP->State & RIO_DELETED) {
610 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
612 return RIO_FAIL;
613 }
614
Andrew Morton8d8706e2006-01-11 12:17:49 -0800615 while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
616 rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
618 if (retries-- <= 0) {
Andrew Morton8d8706e2006-01-11 12:17:49 -0800619 rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return RIO_FAIL;
621 }
Andrew Morton8d8706e2006-01-11 12:17:49 -0800622 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return RIO_FAIL;
624 }
625 rio_spin_lock_irqsave(&PortP->portSem, flags);
626 }
627
Andrew Morton8d8706e2006-01-11 12:17:49 -0800628 if (p->RIOHalted) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
630 return RIO_FAIL;
631 }
632
633 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800634 ** set the command byte and the argument byte
635 */
Alan Coxe2b3afd2006-03-24 03:18:27 -0800636 writeb(command, &PacketP->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Andrew Morton8d8706e2006-01-11 12:17:49 -0800638 if (len == 2)
Alan Coxe2b3afd2006-03-24 03:18:27 -0800639 writeb(arg, &PacketP->data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800642 ** set the length of the packet and set the command bit.
643 */
Alan Coxe2b3afd2006-03-24 03:18:27 -0800644 writeb(PKT_CMD_BIT | len, &PacketP->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 add_transmit(PortP);
647 /*
Andrew Morton8d8706e2006-01-11 12:17:49 -0800648 ** Count characters transmitted for port statistics reporting
649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (PortP->statsGather)
651 PortP->txchars += len;
652
653 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
654 return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;
655}
656
657