blob: 14662d7aa628e3dbdc9509d226d140fbbd411ce5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/riscom.c -- RISCom/8 multiport serial driver.
3 *
4 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
5 *
6 * This code is loosely based on the Linux serial driver, written by
Alan Cox9492e132008-04-30 00:54:15 -07007 * Linus Torvalds, Theodore T'so and others. The RISCom/8 card
8 * programming info was obtained from various drivers for other OSes
9 * (FreeBSD, ISC, etc), but no source code from those drivers were
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * directly included in this driver.
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 * Revision 1.1
28 *
29 * ChangeLog:
30 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 27-Jun-2001
31 * - get rid of check_region and several cleanups
32 */
33
34#include <linux/module.h>
35
Alan Cox9492e132008-04-30 00:54:15 -070036#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/ioport.h>
40#include <linux/interrupt.h>
41#include <linux/errno.h>
42#include <linux/tty.h>
43#include <linux/mm.h>
44#include <linux/serial.h>
45#include <linux/fcntl.h>
46#include <linux/major.h>
47#include <linux/init.h>
48#include <linux/delay.h>
Alan Cox33f0f882006-01-09 20:54:13 -080049#include <linux/tty_flip.h>
Jeff Garzikd9afa432008-02-06 01:36:11 -080050#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Alan Cox9492e132008-04-30 00:54:15 -070052#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "riscom8.h"
55#include "riscom8_reg.h"
56
57/* Am I paranoid or not ? ;-) */
58#define RISCOM_PARANOIA_CHECK
59
Alan Cox9492e132008-04-30 00:54:15 -070060/*
61 * Crazy InteliCom/8 boards sometimes have swapped CTS & DSR signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * You can slightly speed up things by #undefing the following option,
Alan Cox9492e132008-04-30 00:54:15 -070063 * if you are REALLY sure that your board is correct one.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
65
66#define RISCOM_BRAIN_DAMAGED_CTS
67
Alan Cox9492e132008-04-30 00:54:15 -070068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * The following defines are mostly for testing purposes. But if you need
70 * some nice reporting in your syslog, you can define them also.
71 */
72#undef RC_REPORT_FIFO
73#undef RC_REPORT_OVERRUN
74
75
76#define RISCOM_LEGAL_FLAGS \
77 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
78 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
79 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static struct tty_driver *riscom_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Jeff Garzikd9afa432008-02-06 01:36:11 -080083static DEFINE_SPINLOCK(riscom_lock);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static struct riscom_board rc_board[RC_NBOARD] = {
86 {
87 .base = RC_IOBASE1,
88 },
89 {
90 .base = RC_IOBASE2,
91 },
92 {
93 .base = RC_IOBASE3,
94 },
95 {
96 .base = RC_IOBASE4,
97 },
98};
99
100static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
101
102/* RISCom/8 I/O ports addresses (without address translation) */
103static unsigned short rc_ioport[] = {
Tobias Klauserfe971072006-01-09 20:54:02 -0800104#if 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
Tobias Klauserfe971072006-01-09 20:54:02 -0800106#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
108 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
109 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
Tobias Klauserfe971072006-01-09 20:54:02 -0800110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
Tobias Klauserfe971072006-01-09 20:54:02 -0800112#define RC_NIOPORT ARRAY_SIZE(rc_ioport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114
Alan Cox9492e132008-04-30 00:54:15 -0700115static int rc_paranoia_check(struct riscom_port const *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 char *name, const char *routine)
117{
118#ifdef RISCOM_PARANOIA_CHECK
119 static const char badmagic[] = KERN_INFO
120 "rc: Warning: bad riscom port magic number for device %s in %s\n";
121 static const char badinfo[] = KERN_INFO
122 "rc: Warning: null riscom port for device %s in %s\n";
123
124 if (!port) {
125 printk(badinfo, name, routine);
126 return 1;
127 }
128 if (port->magic != RISCOM8_MAGIC) {
129 printk(badmagic, name, routine);
130 return 1;
131 }
132#endif
133 return 0;
134}
135
136/*
Alan Cox9492e132008-04-30 00:54:15 -0700137 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * Service functions for RISCom/8 driver.
Alan Cox9492e132008-04-30 00:54:15 -0700139 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
141
142/* Get board number from pointer */
Alan Cox9492e132008-04-30 00:54:15 -0700143static inline int board_No(struct riscom_board const *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 return bp - rc_board;
146}
147
148/* Get port number from pointer */
Alan Cox9492e132008-04-30 00:54:15 -0700149static inline int port_No(struct riscom_port const *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Alan Cox9492e132008-04-30 00:54:15 -0700151 return RC_PORT(port - rc_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154/* Get pointer to board from pointer to port */
Alan Cox9492e132008-04-30 00:54:15 -0700155static inline struct riscom_board *port_Board(struct riscom_port const *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 return &rc_board[RC_BOARD(port - rc_port)];
158}
159
160/* Input Byte from CL CD180 register */
Alan Cox9492e132008-04-30 00:54:15 -0700161static inline unsigned char rc_in(struct riscom_board const *bp,
162 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 return inb(bp->base + RC_TO_ISA(reg));
165}
166
167/* Output Byte to CL CD180 register */
Alan Cox9492e132008-04-30 00:54:15 -0700168static inline void rc_out(struct riscom_board const *bp, unsigned short reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned char val)
170{
171 outb(val, bp->base + RC_TO_ISA(reg));
172}
173
174/* Wait for Channel Command Register ready */
Alan Cox9492e132008-04-30 00:54:15 -0700175static void rc_wait_CCR(struct riscom_board const *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 unsigned long delay;
178
179 /* FIXME: need something more descriptive then 100000 :) */
Alan Cox9492e132008-04-30 00:54:15 -0700180 for (delay = 100000; delay; delay--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (!rc_in(bp, CD180_CCR))
182 return;
Alan Cox9492e132008-04-30 00:54:15 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
185}
186
187/*
188 * RISCom/8 probe functions.
189 */
190
Alan Cox9492e132008-04-30 00:54:15 -0700191static int rc_request_io_range(struct riscom_board * const bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int i;
Alan Cox9492e132008-04-30 00:54:15 -0700194
195 for (i = 0; i < RC_NIOPORT; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
197 "RISCom/8")) {
198 goto out_release;
199 }
200 return 0;
201out_release:
202 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
203 board_No(bp), bp->base);
Alan Cox9492e132008-04-30 00:54:15 -0700204 while (--i >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
206 return 1;
207}
208
Alan Cox9492e132008-04-30 00:54:15 -0700209static void rc_release_io_range(struct riscom_board * const bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int i;
Alan Cox9492e132008-04-30 00:54:15 -0700212
213 for (i = 0; i < RC_NIOPORT; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
215}
Alan Cox9492e132008-04-30 00:54:15 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/* Reset and setup CD180 chip */
Alan Cox9492e132008-04-30 00:54:15 -0700218static void __init rc_init_CD180(struct riscom_board const *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -0700221
Jeff Garzikd9afa432008-02-06 01:36:11 -0800222 spin_lock_irqsave(&riscom_lock, flags);
223
Alan Cox9492e132008-04-30 00:54:15 -0700224 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
225 rc_wait_CCR(bp); /* Wait for CCR ready */
226 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
Jeff Garzikd9afa432008-02-06 01:36:11 -0800227 spin_unlock_irqrestore(&riscom_lock, flags);
Alan Cox9492e132008-04-30 00:54:15 -0700228 msleep(50); /* Delay 0.05 sec */
Jeff Garzikd9afa432008-02-06 01:36:11 -0800229 spin_lock_irqsave(&riscom_lock, flags);
Alan Cox9492e132008-04-30 00:54:15 -0700230 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
231 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
232 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
233 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for tx intr */
234 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for rx intr */
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* Setting up prescaler. We need 4 ticks per 1 ms */
237 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
238 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
Alan Cox9492e132008-04-30 00:54:15 -0700239
Jeff Garzikd9afa432008-02-06 01:36:11 -0800240 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/* Main probing routine, also sets irq. */
244static int __init rc_probe(struct riscom_board *bp)
245{
246 unsigned char val1, val2;
247 int irqs = 0;
248 int retries;
Alan Cox9492e132008-04-30 00:54:15 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 bp->irq = 0;
251
252 if (rc_request_io_range(bp))
253 return 1;
Alan Cox9492e132008-04-30 00:54:15 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* Are the I/O ports here ? */
256 rc_out(bp, CD180_PPRL, 0x5a);
257 outb(0xff, 0x80);
258 val1 = rc_in(bp, CD180_PPRL);
259 rc_out(bp, CD180_PPRL, 0xa5);
260 outb(0x00, 0x80);
261 val2 = rc_in(bp, CD180_PPRL);
Alan Cox9492e132008-04-30 00:54:15 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if ((val1 != 0x5a) || (val2 != 0xa5)) {
264 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
265 board_No(bp), bp->base);
266 goto out_release;
267 }
Alan Cox9492e132008-04-30 00:54:15 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* It's time to find IRQ for this board */
Alan Cox9492e132008-04-30 00:54:15 -0700270 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 irqs = probe_irq_on();
Alan Cox9492e132008-04-30 00:54:15 -0700272 rc_init_CD180(bp); /* Reset CD180 chip */
273 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 rc_wait_CCR(bp);
Alan Cox9492e132008-04-30 00:54:15 -0700275 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
276 rc_out(bp, CD180_IER, IER_TXRDY);/* Enable tx empty intr */
Jiri Slabyc4ebd922007-07-17 04:05:20 -0700277 msleep(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 irqs = probe_irq_off(irqs);
Alan Cox9492e132008-04-30 00:54:15 -0700279 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
280 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
281 rc_init_CD180(bp); /* Reset CD180 again */
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
284 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
285 "found.\n", board_No(bp), bp->base);
286 goto out_release;
287 }
288 }
Alan Cox9492e132008-04-30 00:54:15 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (irqs <= 0) {
291 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
292 "at 0x%03x.\n", board_No(bp), bp->base);
293 goto out_release;
294 }
295 bp->irq = irqs;
296 bp->flags |= RC_BOARD_PRESENT;
Alan Cox9492e132008-04-30 00:54:15 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
299 "0x%03x, IRQ %d.\n",
300 board_No(bp),
301 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
302 bp->base, bp->irq);
Alan Cox9492e132008-04-30 00:54:15 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305out_release:
306 rc_release_io_range(bp);
307 return 1;
308}
309
Alan Cox9492e132008-04-30 00:54:15 -0700310/*
311 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * Interrupt processing routines.
Alan Cox9492e132008-04-30 00:54:15 -0700313 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315
Alan Cox9492e132008-04-30 00:54:15 -0700316static struct riscom_port *rc_get_port(struct riscom_board const *bp,
317 unsigned char const *what)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 unsigned char channel;
Alan Cox9492e132008-04-30 00:54:15 -0700320 struct riscom_port *port;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
323 if (channel < CD180_NCH) {
324 port = &rc_port[board_No(bp) * RC_NPORT + channel];
Alan Cox85f8f812008-07-16 21:55:29 +0100325 if (port->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Alan Cox9492e132008-04-30 00:54:15 -0700328 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 board_No(bp), what, channel);
330 return NULL;
331}
332
Alan Cox9492e132008-04-30 00:54:15 -0700333static void rc_receive_exc(struct riscom_board const *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct riscom_port *port;
336 struct tty_struct *tty;
337 unsigned char status;
Alan Cox33f0f882006-01-09 20:54:13 -0800338 unsigned char ch, flag;
Alan Cox9492e132008-04-30 00:54:15 -0700339
340 port = rc_get_port(bp, "Receive");
341 if (port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return;
343
Alan Cox85f8f812008-07-16 21:55:29 +0100344 tty = port->port.tty;
Alan Cox9492e132008-04-30 00:54:15 -0700345
346#ifdef RC_REPORT_OVERRUN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 status = rc_in(bp, CD180_RCSR);
Alan Cox33f0f882006-01-09 20:54:13 -0800348 if (status & RCSR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 port->overrun++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 status &= port->mark_mask;
Alan Cox9492e132008-04-30 00:54:15 -0700351#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
Alan Cox9492e132008-04-30 00:54:15 -0700353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 ch = rc_in(bp, CD180_RDR);
Alan Cox9492e132008-04-30 00:54:15 -0700355 if (!status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (status & RCSR_TOUT) {
358 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
Alan Cox9492e132008-04-30 00:54:15 -0700359 "Hardware problems ?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 board_No(bp), port_No(port));
361 return;
Alan Cox9492e132008-04-30 00:54:15 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 } else if (status & RCSR_BREAK) {
364 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
365 board_No(bp), port_No(port));
Alan Cox33f0f882006-01-09 20:54:13 -0800366 flag = TTY_BREAK;
Alan Cox85f8f812008-07-16 21:55:29 +0100367 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 do_SAK(tty);
Alan Cox9492e132008-04-30 00:54:15 -0700369
370 } else if (status & RCSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800371 flag = TTY_PARITY;
Alan Cox9492e132008-04-30 00:54:15 -0700372
373 else if (status & RCSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800374 flag = TTY_FRAME;
Alan Cox9492e132008-04-30 00:54:15 -0700375
376 else if (status & RCSR_OE)
Alan Cox33f0f882006-01-09 20:54:13 -0800377 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 else
Alan Cox33f0f882006-01-09 20:54:13 -0800379 flag = TTY_NORMAL;
Alan Cox9492e132008-04-30 00:54:15 -0700380
Alan Cox33f0f882006-01-09 20:54:13 -0800381 tty_insert_flip_char(tty, ch, flag);
382 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Alan Cox9492e132008-04-30 00:54:15 -0700385static void rc_receive(struct riscom_board const *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct riscom_port *port;
388 struct tty_struct *tty;
389 unsigned char count;
Alan Cox9492e132008-04-30 00:54:15 -0700390
391 port = rc_get_port(bp, "Receive");
392 if (port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return;
Alan Cox9492e132008-04-30 00:54:15 -0700394
Alan Cox85f8f812008-07-16 21:55:29 +0100395 tty = port->port.tty;
Alan Cox9492e132008-04-30 00:54:15 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 count = rc_in(bp, CD180_RDCR);
Alan Cox9492e132008-04-30 00:54:15 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#ifdef RC_REPORT_FIFO
400 port->hits[count > 8 ? 9 : count]++;
Alan Cox9492e132008-04-30 00:54:15 -0700401#endif
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 while (count--) {
Alan Cox33f0f882006-01-09 20:54:13 -0800404 if (tty_buffer_request_room(tty, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 printk(KERN_WARNING "rc%d: port %d: Working around "
406 "flip buffer overflow.\n",
407 board_No(bp), port_No(port));
408 break;
409 }
Alan Cox33f0f882006-01-09 20:54:13 -0800410 tty_insert_flip_char(tty, rc_in(bp, CD180_RDR), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Alan Cox33f0f882006-01-09 20:54:13 -0800412 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Alan Cox9492e132008-04-30 00:54:15 -0700415static void rc_transmit(struct riscom_board const *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct riscom_port *port;
418 struct tty_struct *tty;
419 unsigned char count;
Alan Cox9492e132008-04-30 00:54:15 -0700420
421 port = rc_get_port(bp, "Transmit");
422 if (port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return;
Alan Cox9492e132008-04-30 00:54:15 -0700424
Alan Cox85f8f812008-07-16 21:55:29 +0100425 tty = port->port.tty;
Alan Cox9492e132008-04-30 00:54:15 -0700426
427 if (port->IER & IER_TXEMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* FIFO drained */
429 rc_out(bp, CD180_CAR, port_No(port));
430 port->IER &= ~IER_TXEMPTY;
431 rc_out(bp, CD180_IER, port->IER);
432 return;
433 }
Alan Cox9492e132008-04-30 00:54:15 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if ((port->xmit_cnt <= 0 && !port->break_length)
436 || tty->stopped || tty->hw_stopped) {
437 rc_out(bp, CD180_CAR, port_No(port));
438 port->IER &= ~IER_TXRDY;
439 rc_out(bp, CD180_IER, port->IER);
440 return;
441 }
Alan Cox9492e132008-04-30 00:54:15 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (port->break_length) {
444 if (port->break_length > 0) {
445 if (port->COR2 & COR2_ETC) {
446 rc_out(bp, CD180_TDR, CD180_C_ESC);
447 rc_out(bp, CD180_TDR, CD180_C_SBRK);
448 port->COR2 &= ~COR2_ETC;
449 }
450 count = min_t(int, port->break_length, 0xff);
451 rc_out(bp, CD180_TDR, CD180_C_ESC);
452 rc_out(bp, CD180_TDR, CD180_C_DELAY);
453 rc_out(bp, CD180_TDR, count);
Alan Cox9492e132008-04-30 00:54:15 -0700454 port->break_length -= count;
455 if (port->break_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 port->break_length--;
457 } else {
458 rc_out(bp, CD180_TDR, CD180_C_ESC);
459 rc_out(bp, CD180_TDR, CD180_C_EBRK);
460 rc_out(bp, CD180_COR2, port->COR2);
461 rc_wait_CCR(bp);
462 rc_out(bp, CD180_CCR, CCR_CORCHG2);
463 port->break_length = 0;
464 }
465 return;
466 }
Alan Cox9492e132008-04-30 00:54:15 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 count = CD180_NFIFO;
469 do {
Alan Cox85f8f812008-07-16 21:55:29 +0100470 rc_out(bp, CD180_TDR, port->port.xmit_buf[port->xmit_tail++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
472 if (--port->xmit_cnt <= 0)
473 break;
474 } while (--count > 0);
Alan Cox9492e132008-04-30 00:54:15 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (port->xmit_cnt <= 0) {
477 rc_out(bp, CD180_CAR, port_No(port));
478 port->IER &= ~IER_TXRDY;
479 rc_out(bp, CD180_IER, port->IER);
480 }
481 if (port->xmit_cnt <= port->wakeup_chars)
Jiri Slabyb98e70d2008-02-07 00:16:41 -0800482 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Alan Cox9492e132008-04-30 00:54:15 -0700485static void rc_check_modem(struct riscom_board const *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct riscom_port *port;
488 struct tty_struct *tty;
489 unsigned char mcr;
Alan Cox9492e132008-04-30 00:54:15 -0700490
491 port = rc_get_port(bp, "Modem");
492 if (port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return;
Alan Cox9492e132008-04-30 00:54:15 -0700494
Alan Cox85f8f812008-07-16 21:55:29 +0100495 tty = port->port.tty;
Alan Cox9492e132008-04-30 00:54:15 -0700496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 mcr = rc_in(bp, CD180_MCR);
Alan Cox9492e132008-04-30 00:54:15 -0700498 if (mcr & MCR_CDCHG) {
499 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
Alan Cox85f8f812008-07-16 21:55:29 +0100500 wake_up_interruptible(&port->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 else
Jiri Slabyb98e70d2008-02-07 00:16:41 -0800502 tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Alan Cox9492e132008-04-30 00:54:15 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505#ifdef RISCOM_BRAIN_DAMAGED_CTS
506 if (mcr & MCR_CTSCHG) {
507 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
508 tty->hw_stopped = 0;
509 port->IER |= IER_TXRDY;
510 if (port->xmit_cnt <= port->wakeup_chars)
Jiri Slabyb98e70d2008-02-07 00:16:41 -0800511 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 } else {
513 tty->hw_stopped = 1;
514 port->IER &= ~IER_TXRDY;
515 }
516 rc_out(bp, CD180_IER, port->IER);
517 }
518 if (mcr & MCR_DSRCHG) {
519 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
520 tty->hw_stopped = 0;
521 port->IER |= IER_TXRDY;
522 if (port->xmit_cnt <= port->wakeup_chars)
Jiri Slabyb98e70d2008-02-07 00:16:41 -0800523 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 } else {
525 tty->hw_stopped = 1;
526 port->IER &= ~IER_TXRDY;
527 }
528 rc_out(bp, CD180_IER, port->IER);
529 }
530#endif /* RISCOM_BRAIN_DAMAGED_CTS */
Alan Cox9492e132008-04-30 00:54:15 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Clear change bits */
533 rc_out(bp, CD180_MCR, 0);
534}
535
536/* The main interrupt processing routine */
Alan Cox9492e132008-04-30 00:54:15 -0700537static irqreturn_t rc_interrupt(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 unsigned char status;
540 unsigned char ack;
Jeff Garzikf07ef392007-10-23 19:12:11 -0400541 struct riscom_board *bp = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 unsigned long loop = 0;
543 int handled = 0;
544
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400545 if (!(bp->flags & RC_BOARD_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return IRQ_NONE;
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
549 (RC_BSR_TOUT | RC_BSR_TINT |
550 RC_BSR_MINT | RC_BSR_RINT))) {
551 handled = 1;
Alan Cox9492e132008-04-30 00:54:15 -0700552 if (status & RC_BSR_TOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
554 "error?\n", board_No(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 else if (status & RC_BSR_RINT) {
556 ack = rc_in(bp, RC_ACK_RINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (ack == (RC_ID | GIVR_IT_RCV))
558 rc_receive(bp);
559 else if (ack == (RC_ID | GIVR_IT_REXC))
560 rc_receive_exc(bp);
561 else
562 printk(KERN_WARNING "rc%d: Bad receive ack "
563 "0x%02x.\n",
564 board_No(bp), ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 } else if (status & RC_BSR_TINT) {
566 ack = rc_in(bp, RC_ACK_TINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (ack == (RC_ID | GIVR_IT_TX))
568 rc_transmit(bp);
569 else
570 printk(KERN_WARNING "rc%d: Bad transmit ack "
571 "0x%02x.\n",
572 board_No(bp), ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 } else /* if (status & RC_BSR_MINT) */ {
574 ack = rc_in(bp, RC_ACK_MINT);
Alan Cox9492e132008-04-30 00:54:15 -0700575 if (ack == (RC_ID | GIVR_IT_MODEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 rc_check_modem(bp);
577 else
578 printk(KERN_WARNING "rc%d: Bad modem ack "
579 "0x%02x.\n",
580 board_No(bp), ack);
Alan Cox9492e132008-04-30 00:54:15 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
583 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
584 }
585 return IRQ_RETVAL(handled);
586}
587
588/*
589 * Routines for open & close processing.
590 */
591
592/* Called with disabled interrupts */
Alan Cox9492e132008-04-30 00:54:15 -0700593static int rc_setup_board(struct riscom_board *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 int error;
596
Alan Cox9492e132008-04-30 00:54:15 -0700597 if (bp->flags & RC_BOARD_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
Alan Cox9492e132008-04-30 00:54:15 -0700599
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700600 error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
Jeff Garzikf07ef392007-10-23 19:12:11 -0400601 "RISCom/8", bp);
Alan Cox9492e132008-04-30 00:54:15 -0700602 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return error;
Alan Cox9492e132008-04-30 00:54:15 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 rc_out(bp, RC_CTOUT, 0); /* Just in case */
606 bp->DTR = ~0;
607 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
Alan Cox9492e132008-04-30 00:54:15 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 bp->flags |= RC_BOARD_ACTIVE;
Alan Cox9492e132008-04-30 00:54:15 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 0;
612}
613
614/* Called with disabled interrupts */
Jeff Garzikf07ef392007-10-23 19:12:11 -0400615static void rc_shutdown_board(struct riscom_board *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 if (!(bp->flags & RC_BOARD_ACTIVE))
618 return;
Alan Cox9492e132008-04-30 00:54:15 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 bp->flags &= ~RC_BOARD_ACTIVE;
Alan Cox9492e132008-04-30 00:54:15 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 free_irq(bp->irq, NULL);
Alan Cox9492e132008-04-30 00:54:15 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 bp->DTR = ~0;
625 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
Alan Cox9492e132008-04-30 00:54:15 -0700626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/*
Alan Cox9492e132008-04-30 00:54:15 -0700630 * Setting up port characteristics.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Must be called with disabled interrupts
632 */
633static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
634{
Alan Cox85f8f812008-07-16 21:55:29 +0100635 struct tty_struct *tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 unsigned long baud;
637 long tmp;
638 unsigned char cor1 = 0, cor3 = 0;
639 unsigned char mcor1 = 0, mcor2 = 0;
Alan Cox9492e132008-04-30 00:54:15 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 port->IER = 0;
642 port->COR2 = 0;
643 port->MSVR = MSVR_RTS;
Alan Cox9492e132008-04-30 00:54:15 -0700644
Alan Coxc7bce302006-09-30 23:27:24 -0700645 baud = tty_get_baud_rate(tty);
Alan Cox9492e132008-04-30 00:54:15 -0700646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* Select port on the board */
648 rc_out(bp, CD180_CAR, port_No(port));
Alan Cox9492e132008-04-30 00:54:15 -0700649
Alan Coxc7bce302006-09-30 23:27:24 -0700650 if (!baud) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* Drop DTR & exit */
652 bp->DTR |= (1u << port_No(port));
653 rc_out(bp, RC_DTR, bp->DTR);
654 return;
655 } else {
656 /* Set DTR on */
657 bp->DTR &= ~(1u << port_No(port));
658 rc_out(bp, RC_DTR, bp->DTR);
659 }
Alan Cox9492e132008-04-30 00:54:15 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
Alan Cox9492e132008-04-30 00:54:15 -0700662 * Now we must calculate some speed depended things
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Alan Cox9492e132008-04-30 00:54:15 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Set baud rate for port */
Alan Coxc7bce302006-09-30 23:27:24 -0700666 tmp = (((RC_OSCFREQ + baud/2) / baud +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 CD180_TPC/2) / CD180_TPC);
668
Alan Cox9492e132008-04-30 00:54:15 -0700669 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
670 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
671 rc_out(bp, CD180_RBPRL, tmp & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 rc_out(bp, CD180_TBPRL, tmp & 0xff);
Alan Cox9492e132008-04-30 00:54:15 -0700673
Alan Coxc7bce302006-09-30 23:27:24 -0700674 baud = (baud + 5) / 10; /* Estimated CPS */
Alan Cox9492e132008-04-30 00:54:15 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* Two timer ticks seems enough to wakeup something like SLIP driver */
Alan Cox9492e132008-04-30 00:54:15 -0700677 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
679 SERIAL_XMIT_SIZE - 1 : tmp);
Alan Cox9492e132008-04-30 00:54:15 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* Receiver timeout will be transmission time for 1.5 chars */
682 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
683 tmp = (tmp > 0xff) ? 0xff : tmp;
684 rc_out(bp, CD180_RTPR, tmp);
Alan Cox9492e132008-04-30 00:54:15 -0700685
686 switch (C_CSIZE(tty)) {
687 case CS5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 cor1 |= COR1_5BITS;
689 break;
Alan Cox9492e132008-04-30 00:54:15 -0700690 case CS6:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 cor1 |= COR1_6BITS;
692 break;
Alan Cox9492e132008-04-30 00:54:15 -0700693 case CS7:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 cor1 |= COR1_7BITS;
695 break;
Alan Cox9492e132008-04-30 00:54:15 -0700696 case CS8:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 cor1 |= COR1_8BITS;
698 break;
699 }
Alan Cox9492e132008-04-30 00:54:15 -0700700 if (C_CSTOPB(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 cor1 |= COR1_2SB;
Alan Cox9492e132008-04-30 00:54:15 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 cor1 |= COR1_IGNORE;
Alan Cox9492e132008-04-30 00:54:15 -0700704 if (C_PARENB(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 cor1 |= COR1_NORMPAR;
Alan Cox9492e132008-04-30 00:54:15 -0700706 if (C_PARODD(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 cor1 |= COR1_ODDP;
Alan Cox9492e132008-04-30 00:54:15 -0700708 if (I_INPCK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 cor1 &= ~COR1_IGNORE;
710 }
711 /* Set marking of some errors */
712 port->mark_mask = RCSR_OE | RCSR_TOUT;
Alan Cox9492e132008-04-30 00:54:15 -0700713 if (I_INPCK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 port->mark_mask |= RCSR_FE | RCSR_PE;
Alan Cox9492e132008-04-30 00:54:15 -0700715 if (I_BRKINT(tty) || I_PARMRK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 port->mark_mask |= RCSR_BREAK;
Alan Cox9492e132008-04-30 00:54:15 -0700717 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
Alan Cox9492e132008-04-30 00:54:15 -0700719 if (I_IGNBRK(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 port->mark_mask &= ~RCSR_BREAK;
Alan Cox9492e132008-04-30 00:54:15 -0700721 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* Real raw mode. Ignore all */
723 port->mark_mask &= ~RCSR_OE;
724 }
725 /* Enable Hardware Flow Control */
726 if (C_CRTSCTS(tty)) {
727#ifdef RISCOM_BRAIN_DAMAGED_CTS
728 port->IER |= IER_DSR | IER_CTS;
729 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
730 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
Alan Cox9492e132008-04-30 00:54:15 -0700731 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) &
732 (MSVR_CTS|MSVR_DSR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733#else
734 port->COR2 |= COR2_CTSAE;
735#endif
736 }
737 /* Enable Software Flow Control. FIXME: I'm not sure about this */
738 /* Some people reported that it works, but I still doubt */
739 if (I_IXON(tty)) {
740 port->COR2 |= COR2_TXIBE;
741 cor3 |= (COR3_FCT | COR3_SCDE);
742 if (I_IXANY(tty))
743 port->COR2 |= COR2_IXM;
744 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
745 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
746 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
747 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
748 }
749 if (!C_CLOCAL(tty)) {
750 /* Enable CD check */
751 port->IER |= IER_CD;
752 mcor1 |= MCOR1_CDZD;
753 mcor2 |= MCOR2_CDOD;
754 }
Alan Cox9492e132008-04-30 00:54:15 -0700755
756 if (C_CREAD(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* Enable receiver */
758 port->IER |= IER_RXD;
Alan Cox9492e132008-04-30 00:54:15 -0700759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* Set input FIFO size (1-8 bytes) */
Alan Cox9492e132008-04-30 00:54:15 -0700761 cor3 |= RISCOM_RXFIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* Setting up CD180 channel registers */
763 rc_out(bp, CD180_COR1, cor1);
764 rc_out(bp, CD180_COR2, port->COR2);
765 rc_out(bp, CD180_COR3, cor3);
766 /* Make CD180 know about registers change */
767 rc_wait_CCR(bp);
768 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
769 /* Setting up modem option registers */
770 rc_out(bp, CD180_MCOR1, mcor1);
771 rc_out(bp, CD180_MCOR2, mcor2);
772 /* Enable CD180 transmitter & receiver */
773 rc_wait_CCR(bp);
774 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
775 /* Enable interrupts */
776 rc_out(bp, CD180_IER, port->IER);
777 /* And finally set RTS on */
778 rc_out(bp, CD180_MSVR, port->MSVR);
779}
780
781/* Must be called with interrupts enabled */
782static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
783{
784 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -0700785
Alan Cox85f8f812008-07-16 21:55:29 +0100786 if (port->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return 0;
Alan Cox9492e132008-04-30 00:54:15 -0700788
Alan Cox85f8f812008-07-16 21:55:29 +0100789 if (tty_port_alloc_xmit_buf(&port->port) < 0)
790 return -ENOMEM;
791
Jeff Garzikd9afa432008-02-06 01:36:11 -0800792 spin_lock_irqsave(&riscom_lock, flags);
793
Alan Coxd99101f2008-07-16 21:55:37 +0100794 clear_bit(TTY_IO_ERROR, &port->port.tty->flags);
Alan Cox85f8f812008-07-16 21:55:29 +0100795 if (port->port.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 bp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
798 rc_change_speed(bp, port);
Alan Cox85f8f812008-07-16 21:55:29 +0100799 port->port.flags |= ASYNC_INITIALIZED;
Alan Cox9492e132008-04-30 00:54:15 -0700800
Jeff Garzikd9afa432008-02-06 01:36:11 -0800801 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803}
804
805/* Must be called with interrupts disabled */
Alan Coxd99101f2008-07-16 21:55:37 +0100806static void rc_shutdown_port(struct tty_struct *tty,
807 struct riscom_board *bp, struct riscom_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Alan Cox85f8f812008-07-16 21:55:29 +0100809 if (!(port->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return;
Alan Cox9492e132008-04-30 00:54:15 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#ifdef RC_REPORT_OVERRUN
813 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
814 board_No(bp), port_No(port), port->overrun);
Alan Cox9492e132008-04-30 00:54:15 -0700815#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#ifdef RC_REPORT_FIFO
817 {
818 int i;
Alan Cox9492e132008-04-30 00:54:15 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
821 board_No(bp), port_No(port));
Alan Cox9492e132008-04-30 00:54:15 -0700822 for (i = 0; i < 10; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 printk("%ld ", port->hits[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 printk("].\n");
825 }
Alan Cox9492e132008-04-30 00:54:15 -0700826#endif
Alan Cox85f8f812008-07-16 21:55:29 +0100827 tty_port_free_xmit_buf(&port->port);
Alan Coxd99101f2008-07-16 21:55:37 +0100828 if (C_HUPCL(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Drop DTR */
830 bp->DTR |= (1u << port_No(port));
831 rc_out(bp, RC_DTR, bp->DTR);
832 }
Alan Cox9492e132008-04-30 00:54:15 -0700833
834 /* Select port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 rc_out(bp, CD180_CAR, port_No(port));
836 /* Reset port */
837 rc_wait_CCR(bp);
838 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
839 /* Disable all interrupts from this port */
840 port->IER = 0;
841 rc_out(bp, CD180_IER, port->IER);
Alan Cox9492e132008-04-30 00:54:15 -0700842
Alan Coxd99101f2008-07-16 21:55:37 +0100843 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox85f8f812008-07-16 21:55:29 +0100844 port->port.flags &= ~ASYNC_INITIALIZED;
Alan Cox9492e132008-04-30 00:54:15 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (--bp->count < 0) {
847 printk(KERN_INFO "rc%d: rc_shutdown_port: "
848 "bad board count: %d\n",
849 board_No(bp), bp->count);
850 bp->count = 0;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /*
853 * If this is the last opened port on the board
854 * shutdown whole board
855 */
Alan Cox9492e132008-04-30 00:54:15 -0700856 if (!bp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 rc_shutdown_board(bp);
858}
859
Alan Cox31f35932009-01-02 13:45:05 +0000860static int carrier_raised(struct tty_port *port)
861{
862 struct riscom_port *p = container_of(port, struct riscom_port, port);
863 struct riscom_board *bp = port_Board(p);
864 unsigned long flags;
865 int CD;
866
867 spin_lock_irqsave(&riscom_lock, flags);
868 rc_out(bp, CD180_CAR, port_No(p));
869 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
870 rc_out(bp, CD180_MSVR, MSVR_RTS);
871 bp->DTR &= ~(1u << port_No(p));
872 rc_out(bp, RC_DTR, bp->DTR);
873 spin_unlock_irqrestore(&riscom_lock, flags);
874 return CD;
875}
876
Alan Cox9492e132008-04-30 00:54:15 -0700877static int block_til_ready(struct tty_struct *tty, struct file *filp,
Alan Cox31f35932009-01-02 13:45:05 +0000878 struct riscom_port *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int retval;
882 int do_clocal = 0;
883 int CD;
Jeff Garzikd9afa432008-02-06 01:36:11 -0800884 unsigned long flags;
Alan Cox31f35932009-01-02 13:45:05 +0000885 struct tty_port *port = &rp->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /*
888 * If the device is in the middle of being closed, then block
889 * until it's done, and then try again.
890 */
Alan Cox31f35932009-01-02 13:45:05 +0000891 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
892 interruptible_sleep_on(&port->close_wait);
893 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -EAGAIN;
895 else
896 return -ERESTARTSYS;
897 }
898
899 /*
900 * If non-blocking mode is set, or the port is not enabled,
901 * then make the check up front and then exit.
902 */
903 if ((filp->f_flags & O_NONBLOCK) ||
904 (tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox31f35932009-01-02 13:45:05 +0000905 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
907 }
908
Alan Cox9492e132008-04-30 00:54:15 -0700909 if (C_CLOCAL(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 do_clocal = 1;
911
912 /*
913 * Block waiting for the carrier detect and the line to become
914 * free (i.e., not in use by the callout). While we are in
915 * this loop, info->count is dropped by one, so that
916 * rs_close() knows when to free things. We restore it upon
917 * exit, either normal or abnormal.
918 */
919 retval = 0;
Alan Cox31f35932009-01-02 13:45:05 +0000920 add_wait_queue(&port->open_wait, &wait);
Jeff Garzikd9afa432008-02-06 01:36:11 -0800921
Alan Coxc2ba38c2009-01-02 13:45:50 +0000922 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!tty_hung_up_p(filp))
Alan Cox31f35932009-01-02 13:45:05 +0000924 port->count--;
Alan Cox31f35932009-01-02 13:45:05 +0000925 port->blocked_open++;
Alan Coxc2ba38c2009-01-02 13:45:50 +0000926 spin_unlock_irqrestore(&port->lock, flags);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 while (1) {
Jeff Garzikd9afa432008-02-06 01:36:11 -0800929
Alan Cox31f35932009-01-02 13:45:05 +0000930 CD = tty_port_carrier_raised(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 set_current_state(TASK_INTERRUPTIBLE);
932 if (tty_hung_up_p(filp) ||
Alan Cox31f35932009-01-02 13:45:05 +0000933 !(port->flags & ASYNC_INITIALIZED)) {
934 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 retval = -EAGAIN;
936 else
Alan Cox9492e132008-04-30 00:54:15 -0700937 retval = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 break;
939 }
Alan Cox31f35932009-01-02 13:45:05 +0000940 if (!(port->flags & ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 (do_clocal || CD))
942 break;
943 if (signal_pending(current)) {
944 retval = -ERESTARTSYS;
945 break;
946 }
947 schedule();
948 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -0700949 __set_current_state(TASK_RUNNING);
Alan Cox31f35932009-01-02 13:45:05 +0000950 remove_wait_queue(&port->open_wait, &wait);
Alan Coxc2ba38c2009-01-02 13:45:50 +0000951 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!tty_hung_up_p(filp))
Alan Cox31f35932009-01-02 13:45:05 +0000953 port->count++;
954 port->blocked_open--;
Alan Coxc2ba38c2009-01-02 13:45:50 +0000955 if (retval == 0)
956 port->flags |= ASYNC_NORMAL_ACTIVE;
957 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
Alan Cox9492e132008-04-30 00:54:15 -0700959}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Alan Cox9492e132008-04-30 00:54:15 -0700961static int rc_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 int board;
964 int error;
Alan Cox9492e132008-04-30 00:54:15 -0700965 struct riscom_port *port;
966 struct riscom_board *bp;
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 board = RC_BOARD(tty->index);
969 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
970 return -ENODEV;
Alan Cox9492e132008-04-30 00:54:15 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 bp = &rc_board[board];
973 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
974 if (rc_paranoia_check(port, tty->name, "rc_open"))
975 return -ENODEV;
Alan Cox9492e132008-04-30 00:54:15 -0700976
977 error = rc_setup_board(bp);
978 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return error;
Alan Cox9492e132008-04-30 00:54:15 -0700980
Alan Cox85f8f812008-07-16 21:55:29 +0100981 port->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 tty->driver_data = port;
Alan Cox85f8f812008-07-16 21:55:29 +0100983 port->port.tty = tty;
Alan Cox9492e132008-04-30 00:54:15 -0700984
985 error = rc_setup_port(bp, port);
986 if (error == 0)
987 error = block_til_ready(tty, filp, port);
988 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Alan Cox978e5952008-04-30 00:53:59 -0700991static void rc_flush_buffer(struct tty_struct *tty)
992{
993 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
994 unsigned long flags;
995
996 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
997 return;
998
999 spin_lock_irqsave(&riscom_lock, flags);
Alan Cox978e5952008-04-30 00:53:59 -07001000 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
Alan Cox978e5952008-04-30 00:53:59 -07001001 spin_unlock_irqrestore(&riscom_lock, flags);
1002
1003 tty_wakeup(tty);
1004}
1005
Alan Cox9492e132008-04-30 00:54:15 -07001006static void rc_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1009 struct riscom_board *bp;
1010 unsigned long flags;
1011 unsigned long timeout;
Alan Cox9492e132008-04-30 00:54:15 -07001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (!port || rc_paranoia_check(port, tty->name, "close"))
1014 return;
Jeff Garzikd9afa432008-02-06 01:36:11 -08001015
Alan Coxc2ba38c2009-01-02 13:45:50 +00001016 spin_lock_irqsave(&port->port.lock, flags);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (tty_hung_up_p(filp))
1019 goto out;
Alan Cox9492e132008-04-30 00:54:15 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 bp = port_Board(port);
Alan Cox85f8f812008-07-16 21:55:29 +01001022 if ((tty->count == 1) && (port->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 printk(KERN_INFO "rc%d: rc_close: bad port count;"
1024 " tty->count is 1, port count is %d\n",
Alan Cox85f8f812008-07-16 21:55:29 +01001025 board_No(bp), port->port.count);
1026 port->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Alan Cox85f8f812008-07-16 21:55:29 +01001028 if (--port->port.count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 printk(KERN_INFO "rc%d: rc_close: bad port count "
1030 "for tty%d: %d\n",
Alan Cox85f8f812008-07-16 21:55:29 +01001031 board_No(bp), port_No(port), port->port.count);
1032 port->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Alan Cox85f8f812008-07-16 21:55:29 +01001034 if (port->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 goto out;
Alan Cox85f8f812008-07-16 21:55:29 +01001036 port->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /*
Alan Cox9492e132008-04-30 00:54:15 -07001038 * Now we wait for the transmit buffer to clear; and we notify
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * the line discipline to only process XON/XOFF characters.
1040 */
1041 tty->closing = 1;
Alan Coxc2ba38c2009-01-02 13:45:50 +00001042 spin_unlock_irqrestore(&port->port.lock, flags);
Alan Cox44b7d1b2008-07-16 21:57:18 +01001043 if (port->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1044 tty_wait_until_sent(tty, port->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * At this point we stop accepting input. To do this, we
1047 * disable the receive line status interrupts, and tell the
1048 * interrupt driver to stop checking the data ready bit in the
1049 * line status register.
1050 */
Alan Coxc2ba38c2009-01-02 13:45:50 +00001051
1052 spin_lock_irqsave(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 port->IER &= ~IER_RXD;
Alan Cox85f8f812008-07-16 21:55:29 +01001054 if (port->port.flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 port->IER &= ~IER_TXRDY;
1056 port->IER |= IER_TXEMPTY;
1057 rc_out(bp, CD180_CAR, port_No(port));
1058 rc_out(bp, CD180_IER, port->IER);
1059 /*
1060 * Before we drop DTR, make sure the UART transmitter
1061 * has completely drained; this is especially
1062 * important if there is a transmit FIFO!
1063 */
Alan Cox9492e132008-04-30 00:54:15 -07001064 timeout = jiffies + HZ;
1065 while (port->IER & IER_TXEMPTY) {
Alan Coxc2ba38c2009-01-02 13:45:50 +00001066 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 msleep_interruptible(jiffies_to_msecs(port->timeout));
Alan Coxc2ba38c2009-01-02 13:45:50 +00001068 spin_lock_irqsave(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (time_after(jiffies, timeout))
1070 break;
1071 }
1072 }
Alan Coxd99101f2008-07-16 21:55:37 +01001073 rc_shutdown_port(tty, bp, port);
Alan Cox978e5952008-04-30 00:53:59 -07001074 rc_flush_buffer(tty);
Alan Coxc2ba38c2009-01-02 13:45:50 +00001075 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 tty_ldisc_flush(tty);
1077
Alan Coxc2ba38c2009-01-02 13:45:50 +00001078 spin_lock_irqsave(&port->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 tty->closing = 0;
Alan Cox85f8f812008-07-16 21:55:29 +01001080 port->port.tty = NULL;
1081 if (port->port.blocked_open) {
Alan Coxc2ba38c2009-01-02 13:45:50 +00001082 spin_unlock_irqrestore(&port->port.lock, flags);
Alan Cox44b7d1b2008-07-16 21:57:18 +01001083 if (port->port.close_delay)
1084 msleep_interruptible(jiffies_to_msecs(port->port.close_delay));
Alan Cox85f8f812008-07-16 21:55:29 +01001085 wake_up_interruptible(&port->port.open_wait);
Alan Coxc2ba38c2009-01-02 13:45:50 +00001086 spin_lock_irqsave(&port->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Alan Cox85f8f812008-07-16 21:55:29 +01001088 port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1089 wake_up_interruptible(&port->port.close_wait);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001090
1091out:
1092 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Alan Cox9492e132008-04-30 00:54:15 -07001095static int rc_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 const unsigned char *buf, int count)
1097{
1098 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1099 struct riscom_board *bp;
1100 int c, total = 0;
1101 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (rc_paranoia_check(port, tty->name, "rc_write"))
1104 return 0;
Alan Cox9492e132008-04-30 00:54:15 -07001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 bp = port_Board(port);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 while (1) {
Jeff Garzikd9afa432008-02-06 01:36:11 -08001109 spin_lock_irqsave(&riscom_lock, flags);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1112 SERIAL_XMIT_SIZE - port->xmit_head));
Jeff Garzikd9afa432008-02-06 01:36:11 -08001113 if (c <= 0)
1114 break; /* lock continues to be held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Alan Cox85f8f812008-07-16 21:55:29 +01001116 memcpy(port->port.xmit_buf + port->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1118 port->xmit_cnt += c;
Jeff Garzikd9afa432008-02-06 01:36:11 -08001119
1120 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 buf += c;
1123 count -= c;
1124 total += c;
1125 }
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1128 !(port->IER & IER_TXRDY)) {
1129 port->IER |= IER_TXRDY;
1130 rc_out(bp, CD180_CAR, port_No(port));
1131 rc_out(bp, CD180_IER, port->IER);
1132 }
Jeff Garzikd9afa432008-02-06 01:36:11 -08001133
1134 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 return total;
1137}
1138
Alan Cox9492e132008-04-30 00:54:15 -07001139static int rc_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1142 unsigned long flags;
Alan Coxbbbbb962008-04-30 00:54:05 -07001143 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
Alan Coxbbbbb962008-04-30 00:54:05 -07001146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jeff Garzikd9afa432008-02-06 01:36:11 -08001148 spin_lock_irqsave(&riscom_lock, flags);
Alan Cox9492e132008-04-30 00:54:15 -07001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1151 goto out;
1152
Alan Cox85f8f812008-07-16 21:55:29 +01001153 port->port.xmit_buf[port->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1155 port->xmit_cnt++;
Alan Coxbbbbb962008-04-30 00:54:05 -07001156 ret = 1;
Jeff Garzikd9afa432008-02-06 01:36:11 -08001157
1158out:
1159 spin_unlock_irqrestore(&riscom_lock, flags);
Alan Coxbbbbb962008-04-30 00:54:05 -07001160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Alan Cox9492e132008-04-30 00:54:15 -07001163static void rc_flush_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1166 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1169 return;
Alan Cox9492e132008-04-30 00:54:15 -07001170
Alan Coxd99101f2008-07-16 21:55:37 +01001171 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return;
1173
Jeff Garzikd9afa432008-02-06 01:36:11 -08001174 spin_lock_irqsave(&riscom_lock, flags);
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 port->IER |= IER_TXRDY;
1177 rc_out(port_Board(port), CD180_CAR, port_No(port));
1178 rc_out(port_Board(port), CD180_IER, port->IER);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001179
1180 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Alan Cox9492e132008-04-30 00:54:15 -07001183static int rc_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1186 int ret;
Alan Cox9492e132008-04-30 00:54:15 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1189 return 0;
1190
1191 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1192 if (ret < 0)
1193 ret = 0;
1194 return ret;
1195}
1196
1197static int rc_chars_in_buffer(struct tty_struct *tty)
1198{
1199 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
Alan Cox9492e132008-04-30 00:54:15 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1202 return 0;
Alan Cox9492e132008-04-30 00:54:15 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return port->xmit_cnt;
1205}
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1208{
1209 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
Alan Cox9492e132008-04-30 00:54:15 -07001210 struct riscom_board *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 unsigned char status;
1212 unsigned int result;
1213 unsigned long flags;
1214
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001215 if (rc_paranoia_check(port, tty->name, __func__))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return -ENODEV;
1217
1218 bp = port_Board(port);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001219
1220 spin_lock_irqsave(&riscom_lock, flags);
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 rc_out(bp, CD180_CAR, port_No(port));
1223 status = rc_in(bp, CD180_MSVR);
1224 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
Jeff Garzikd9afa432008-02-06 01:36:11 -08001225
1226 spin_unlock_irqrestore(&riscom_lock, flags);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1229 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1230 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1231 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1232 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1233 return result;
1234}
1235
1236static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1237 unsigned int set, unsigned int clear)
1238{
1239 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1240 unsigned long flags;
1241 struct riscom_board *bp;
1242
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001243 if (rc_paranoia_check(port, tty->name, __func__))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -ENODEV;
1245
1246 bp = port_Board(port);
1247
Jeff Garzikd9afa432008-02-06 01:36:11 -08001248 spin_lock_irqsave(&riscom_lock, flags);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (set & TIOCM_RTS)
1251 port->MSVR |= MSVR_RTS;
1252 if (set & TIOCM_DTR)
1253 bp->DTR &= ~(1u << port_No(port));
1254
1255 if (clear & TIOCM_RTS)
1256 port->MSVR &= ~MSVR_RTS;
1257 if (clear & TIOCM_DTR)
1258 bp->DTR |= (1u << port_No(port));
1259
1260 rc_out(bp, CD180_CAR, port_No(port));
1261 rc_out(bp, CD180_MSVR, port->MSVR);
1262 rc_out(bp, RC_DTR, bp->DTR);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001263
1264 spin_unlock_irqrestore(&riscom_lock, flags);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return 0;
1267}
1268
Alan Cox781cff52008-07-22 11:18:30 +01001269static int rc_send_break(struct tty_struct *tty, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Alan Cox781cff52008-07-22 11:18:30 +01001271 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct riscom_board *bp = port_Board(port);
1273 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001274
Alan Cox781cff52008-07-22 11:18:30 +01001275 if (length == 0 || length == -1)
1276 return -EOPNOTSUPP;
1277
Jeff Garzikd9afa432008-02-06 01:36:11 -08001278 spin_lock_irqsave(&riscom_lock, flags);
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 port->break_length = RISCOM_TPS / HZ * length;
1281 port->COR2 |= COR2_ETC;
1282 port->IER |= IER_TXRDY;
1283 rc_out(bp, CD180_CAR, port_No(port));
1284 rc_out(bp, CD180_COR2, port->COR2);
1285 rc_out(bp, CD180_IER, port->IER);
1286 rc_wait_CCR(bp);
1287 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1288 rc_wait_CCR(bp);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001289
1290 spin_unlock_irqrestore(&riscom_lock, flags);
Alan Cox781cff52008-07-22 11:18:30 +01001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Alan Cox9492e132008-04-30 00:54:15 -07001294static int rc_set_serial_info(struct riscom_port *port,
1295 struct serial_struct __user *newinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct serial_struct tmp;
1298 struct riscom_board *bp = port_Board(port);
1299 int change_speed;
Alan Cox9492e132008-04-30 00:54:15 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1302 return -EFAULT;
Alan Cox9492e132008-04-30 00:54:15 -07001303
1304#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if ((tmp.irq != bp->irq) ||
1306 (tmp.port != bp->base) ||
1307 (tmp.type != PORT_CIRRUS) ||
1308 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1309 (tmp.custom_divisor != 0) ||
1310 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1311 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1312 return -EINVAL;
Alan Cox9492e132008-04-30 00:54:15 -07001313#endif
1314
Alan Cox85f8f812008-07-16 21:55:29 +01001315 change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 (tmp.flags & ASYNC_SPD_MASK));
Alan Cox9492e132008-04-30 00:54:15 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!capable(CAP_SYS_ADMIN)) {
Alan Cox44b7d1b2008-07-16 21:57:18 +01001319 if ((tmp.close_delay != port->port.close_delay) ||
1320 (tmp.closing_wait != port->port.closing_wait) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 ((tmp.flags & ~ASYNC_USR_MASK) !=
Alan Cox85f8f812008-07-16 21:55:29 +01001322 (port->port.flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -EPERM;
Alan Cox85f8f812008-07-16 21:55:29 +01001324 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 (tmp.flags & ASYNC_USR_MASK));
1326 } else {
Alan Cox85f8f812008-07-16 21:55:29 +01001327 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 (tmp.flags & ASYNC_FLAGS));
Alan Cox44b7d1b2008-07-16 21:57:18 +01001329 port->port.close_delay = tmp.close_delay;
1330 port->port.closing_wait = tmp.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 if (change_speed) {
Jeff Garzikd9afa432008-02-06 01:36:11 -08001333 unsigned long flags;
1334
1335 spin_lock_irqsave(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 rc_change_speed(bp, port);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001337 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 return 0;
1340}
1341
Alan Cox9492e132008-04-30 00:54:15 -07001342static int rc_get_serial_info(struct riscom_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 struct serial_struct __user *retinfo)
1344{
1345 struct serial_struct tmp;
1346 struct riscom_board *bp = port_Board(port);
Alan Cox9492e132008-04-30 00:54:15 -07001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 memset(&tmp, 0, sizeof(tmp));
1349 tmp.type = PORT_CIRRUS;
1350 tmp.line = port - rc_port;
1351 tmp.port = bp->base;
1352 tmp.irq = bp->irq;
Alan Cox85f8f812008-07-16 21:55:29 +01001353 tmp.flags = port->port.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
Alan Cox44b7d1b2008-07-16 21:57:18 +01001355 tmp.close_delay = port->port.close_delay * HZ/100;
1356 tmp.closing_wait = port->port.closing_wait * HZ/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 tmp.xmit_fifo_size = CD180_NFIFO;
1358 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1359}
1360
Alan Cox9492e132008-04-30 00:54:15 -07001361static int rc_ioctl(struct tty_struct *tty, struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1365 void __user *argp = (void __user *)arg;
Alan Cox781cff52008-07-22 11:18:30 +01001366 int retval;
Alan Cox9492e132008-04-30 00:54:15 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1369 return -ENODEV;
Alan Cox9492e132008-04-30 00:54:15 -07001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 switch (cmd) {
Alan Cox9492e132008-04-30 00:54:15 -07001372 case TIOCGSERIAL:
1373 lock_kernel();
Alan Coxeb174552008-04-30 00:53:21 -07001374 retval = rc_get_serial_info(port, argp);
1375 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
Alan Cox9492e132008-04-30 00:54:15 -07001377 case TIOCSSERIAL:
1378 lock_kernel();
Alan Coxeb174552008-04-30 00:53:21 -07001379 retval = rc_set_serial_info(port, argp);
1380 unlock_kernel();
1381 break;
Alan Cox9492e132008-04-30 00:54:15 -07001382 default:
Alan Coxeb174552008-04-30 00:53:21 -07001383 retval = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Alan Coxeb174552008-04-30 00:53:21 -07001385 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Alan Cox9492e132008-04-30 00:54:15 -07001388static void rc_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1391 struct riscom_board *bp;
1392 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1395 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 bp = port_Board(port);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001397
1398 spin_lock_irqsave(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 port->MSVR &= ~MSVR_RTS;
1400 rc_out(bp, CD180_CAR, port_No(port));
Jeff Garzikd9afa432008-02-06 01:36:11 -08001401 if (I_IXOFF(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 rc_wait_CCR(bp);
1403 rc_out(bp, CD180_CCR, CCR_SSCH2);
1404 rc_wait_CCR(bp);
1405 }
1406 rc_out(bp, CD180_MSVR, port->MSVR);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001407 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Alan Cox9492e132008-04-30 00:54:15 -07001410static void rc_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1413 struct riscom_board *bp;
1414 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 bp = port_Board(port);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001419
Alan Cox9492e132008-04-30 00:54:15 -07001420 spin_lock_irqsave(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 port->MSVR |= MSVR_RTS;
1422 rc_out(bp, CD180_CAR, port_No(port));
1423 if (I_IXOFF(tty)) {
1424 rc_wait_CCR(bp);
1425 rc_out(bp, CD180_CCR, CCR_SSCH1);
1426 rc_wait_CCR(bp);
1427 }
1428 rc_out(bp, CD180_MSVR, port->MSVR);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001429 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Alan Cox9492e132008-04-30 00:54:15 -07001432static void rc_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1435 struct riscom_board *bp;
1436 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1439 return;
Jeff Garzikd9afa432008-02-06 01:36:11 -08001440
Alan Cox9492e132008-04-30 00:54:15 -07001441 bp = port_Board(port);
1442
1443 spin_lock_irqsave(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 port->IER &= ~IER_TXRDY;
1445 rc_out(bp, CD180_CAR, port_No(port));
1446 rc_out(bp, CD180_IER, port->IER);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001447 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Alan Cox9492e132008-04-30 00:54:15 -07001450static void rc_start(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1453 struct riscom_board *bp;
1454 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (rc_paranoia_check(port, tty->name, "rc_start"))
1457 return;
Alan Cox9492e132008-04-30 00:54:15 -07001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 bp = port_Board(port);
Alan Cox9492e132008-04-30 00:54:15 -07001460
Jeff Garzikd9afa432008-02-06 01:36:11 -08001461 spin_lock_irqsave(&riscom_lock, flags);
1462
Alan Cox85f8f812008-07-16 21:55:29 +01001463 if (port->xmit_cnt && port->port.xmit_buf && !(port->IER & IER_TXRDY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 port->IER |= IER_TXRDY;
1465 rc_out(bp, CD180_CAR, port_No(port));
1466 rc_out(bp, CD180_IER, port->IER);
1467 }
Jeff Garzikd9afa432008-02-06 01:36:11 -08001468 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Alan Cox9492e132008-04-30 00:54:15 -07001471static void rc_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1474 struct riscom_board *bp;
Alan Coxc2ba38c2009-01-02 13:45:50 +00001475 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1478 return;
Alan Cox9492e132008-04-30 00:54:15 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 bp = port_Board(port);
Alan Cox9492e132008-04-30 00:54:15 -07001481
Alan Coxd99101f2008-07-16 21:55:37 +01001482 rc_shutdown_port(tty, bp, port);
Alan Coxc2ba38c2009-01-02 13:45:50 +00001483 spin_lock_irqsave(&port->port.lock, flags);
Alan Cox85f8f812008-07-16 21:55:29 +01001484 port->port.count = 0;
1485 port->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1486 port->port.tty = NULL;
1487 wake_up_interruptible(&port->port.open_wait);
Alan Coxc2ba38c2009-01-02 13:45:50 +00001488 spin_unlock_irqrestore(&port->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Alan Cox9492e132008-04-30 00:54:15 -07001491static void rc_set_termios(struct tty_struct *tty,
1492 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1495 unsigned long flags;
Alan Cox9492e132008-04-30 00:54:15 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1498 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Jeff Garzikd9afa432008-02-06 01:36:11 -08001500 spin_lock_irqsave(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 rc_change_speed(port_Board(port), port);
Jeff Garzikd9afa432008-02-06 01:36:11 -08001502 spin_unlock_irqrestore(&riscom_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if ((old_termios->c_cflag & CRTSCTS) &&
1505 !(tty->termios->c_cflag & CRTSCTS)) {
1506 tty->hw_stopped = 0;
1507 rc_start(tty);
1508 }
1509}
1510
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001511static const struct tty_operations riscom_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 .open = rc_open,
1513 .close = rc_close,
1514 .write = rc_write,
1515 .put_char = rc_put_char,
1516 .flush_chars = rc_flush_chars,
1517 .write_room = rc_write_room,
1518 .chars_in_buffer = rc_chars_in_buffer,
1519 .flush_buffer = rc_flush_buffer,
1520 .ioctl = rc_ioctl,
1521 .throttle = rc_throttle,
1522 .unthrottle = rc_unthrottle,
1523 .set_termios = rc_set_termios,
1524 .stop = rc_stop,
1525 .start = rc_start,
1526 .hangup = rc_hangup,
1527 .tiocmget = rc_tiocmget,
1528 .tiocmset = rc_tiocmset,
Alan Cox781cff52008-07-22 11:18:30 +01001529 .break_ctl = rc_send_break,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530};
1531
Alan Cox31f35932009-01-02 13:45:05 +00001532static const struct tty_port_operations riscom_port_ops = {
1533 .carrier_raised = carrier_raised,
1534};
1535
1536
Jiri Slaby1386a822008-02-07 00:16:36 -08001537static int __init rc_init_drivers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
1539 int error;
1540 int i;
1541
1542 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
Alan Cox9492e132008-04-30 00:54:15 -07001543 if (!riscom_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return -ENOMEM;
Alan Cox9492e132008-04-30 00:54:15 -07001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 riscom_driver->owner = THIS_MODULE;
1547 riscom_driver->name = "ttyL";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1549 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1550 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1551 riscom_driver->init_termios = tty_std_termios;
1552 riscom_driver->init_termios.c_cflag =
1553 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08001554 riscom_driver->init_termios.c_ispeed = 9600;
1555 riscom_driver->init_termios.c_ospeed = 9600;
Alan Cox781cff52008-07-22 11:18:30 +01001556 riscom_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_HARDWARE_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 tty_set_operations(riscom_driver, &riscom_ops);
Alan Cox9492e132008-04-30 00:54:15 -07001558 error = tty_register_driver(riscom_driver);
1559 if (error != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 put_tty_driver(riscom_driver);
1561 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
Alan Cox9492e132008-04-30 00:54:15 -07001562 "error = %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 1;
1564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 memset(rc_port, 0, sizeof(rc_port));
1566 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
Alan Cox85f8f812008-07-16 21:55:29 +01001567 tty_port_init(&rc_port[i].port);
Alan Cox31f35932009-01-02 13:45:05 +00001568 rc_port[i].port.ops = &riscom_port_ops;
Alan Cox44b7d1b2008-07-16 21:57:18 +01001569 rc_port[i].magic = RISCOM8_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572}
1573
1574static void rc_release_drivers(void)
1575{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 tty_unregister_driver(riscom_driver);
1577 put_tty_driver(riscom_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580#ifndef MODULE
1581/*
1582 * Called at boot time.
Alan Cox9492e132008-04-30 00:54:15 -07001583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * You can specify IO base for up to RC_NBOARD cards,
1585 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1586 * Note that there will be no probing at default
1587 * addresses in this case.
1588 *
Alan Cox9492e132008-04-30 00:54:15 -07001589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590static int __init riscom8_setup(char *str)
1591{
1592 int ints[RC_NBOARD];
1593 int i;
1594
1595 str = get_options(str, ARRAY_SIZE(ints), ints);
1596
1597 for (i = 0; i < RC_NBOARD; i++) {
1598 if (i < ints[0])
1599 rc_board[i].base = ints[i+1];
Alan Cox9492e132008-04-30 00:54:15 -07001600 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 rc_board[i].base = 0;
1602 }
1603 return 1;
1604}
1605
1606__setup("riscom8=", riscom8_setup);
1607#endif
1608
1609static char banner[] __initdata =
1610 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1611 "1994-1996.\n";
1612static char no_boards_msg[] __initdata =
1613 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1614
Alan Cox9492e132008-04-30 00:54:15 -07001615/*
1616 * This routine must be called by kernel at boot time
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
1618static int __init riscom8_init(void)
1619{
1620 int i;
1621 int found = 0;
1622
1623 printk(banner);
1624
Alan Cox9492e132008-04-30 00:54:15 -07001625 if (rc_init_drivers())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return -EIO;
1627
Alan Cox9492e132008-04-30 00:54:15 -07001628 for (i = 0; i < RC_NBOARD; i++)
1629 if (rc_board[i].base && !rc_probe(&rc_board[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 found++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (!found) {
1632 rc_release_drivers();
1633 printk(no_boards_msg);
1634 return -EIO;
1635 }
1636 return 0;
1637}
1638
1639#ifdef MODULE
1640static int iobase;
1641static int iobase1;
1642static int iobase2;
1643static int iobase3;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08001644module_param(iobase, int, 0);
1645module_param(iobase1, int, 0);
1646module_param(iobase2, int, 0);
1647module_param(iobase3, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649MODULE_LICENSE("GPL");
1650#endif /* MODULE */
1651
1652/*
1653 * You can setup up to 4 boards (current value of RC_NBOARD)
1654 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1655 *
1656 */
Alan Cox9492e132008-04-30 00:54:15 -07001657static int __init riscom8_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659#ifdef MODULE
1660 int i;
1661
1662 if (iobase || iobase1 || iobase2 || iobase3) {
Alan Cox9492e132008-04-30 00:54:15 -07001663 for (i = 0; i < RC_NBOARD; i++)
Jiri Slaby9efda792008-03-13 12:32:39 -07001664 rc_board[i].base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666
1667 if (iobase)
1668 rc_board[0].base = iobase;
1669 if (iobase1)
1670 rc_board[1].base = iobase1;
1671 if (iobase2)
1672 rc_board[2].base = iobase2;
1673 if (iobase3)
1674 rc_board[3].base = iobase3;
1675#endif /* MODULE */
1676
1677 return riscom8_init();
1678}
Alan Cox9492e132008-04-30 00:54:15 -07001679
1680static void __exit riscom8_exit_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 int i;
Alan Cox9492e132008-04-30 00:54:15 -07001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 rc_release_drivers();
Alan Cox9492e132008-04-30 00:54:15 -07001685 for (i = 0; i < RC_NBOARD; i++)
1686 if (rc_board[i].flags & RC_BOARD_PRESENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 rc_release_io_range(&rc_board[i]);
Alan Cox9492e132008-04-30 00:54:15 -07001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
1691module_init(riscom8_init_module);
1692module_exit(riscom8_exit_module);