Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[deliverable/linux.git] / drivers / char / riscom8.c
CommitLineData
1da177e4
LT
1/*
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
9492e135
AC
7 * 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
1da177e4
LT
10 * 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
9492e135 36#include <linux/io.h>
1da177e4
LT
37#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>
33f0f88f 49#include <linux/tty_flip.h>
405f5571 50#include <linux/smp_lock.h>
d9afa435 51#include <linux/spinlock.h>
5c9f5806 52#include <linux/device.h>
1da177e4 53
9492e135 54#include <linux/uaccess.h>
1da177e4
LT
55
56#include "riscom8.h"
57#include "riscom8_reg.h"
58
59/* Am I paranoid or not ? ;-) */
60#define RISCOM_PARANOIA_CHECK
61
9492e135
AC
62/*
63 * Crazy InteliCom/8 boards sometimes have swapped CTS & DSR signals.
1da177e4 64 * You can slightly speed up things by #undefing the following option,
9492e135 65 * if you are REALLY sure that your board is correct one.
1da177e4
LT
66 */
67
68#define RISCOM_BRAIN_DAMAGED_CTS
69
9492e135 70/*
1da177e4
LT
71 * The following defines are mostly for testing purposes. But if you need
72 * some nice reporting in your syslog, you can define them also.
73 */
74#undef RC_REPORT_FIFO
75#undef RC_REPORT_OVERRUN
76
77
78#define RISCOM_LEGAL_FLAGS \
79 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
80 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
81 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
82
1da177e4 83static struct tty_driver *riscom_driver;
1da177e4 84
d9afa435
JG
85static DEFINE_SPINLOCK(riscom_lock);
86
1da177e4
LT
87static struct riscom_board rc_board[RC_NBOARD] = {
88 {
89 .base = RC_IOBASE1,
90 },
91 {
92 .base = RC_IOBASE2,
93 },
94 {
95 .base = RC_IOBASE3,
96 },
97 {
98 .base = RC_IOBASE4,
99 },
100};
101
102static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
103
104/* RISCom/8 I/O ports addresses (without address translation) */
105static unsigned short rc_ioport[] = {
fe971071 106#if 1
1da177e4 107 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
fe971071 108#else
1da177e4
LT
109 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
110 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
111 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
fe971071 112#endif
1da177e4 113};
fe971071 114#define RC_NIOPORT ARRAY_SIZE(rc_ioport)
1da177e4
LT
115
116
9492e135 117static int rc_paranoia_check(struct riscom_port const *port,
1da177e4
LT
118 char *name, const char *routine)
119{
120#ifdef RISCOM_PARANOIA_CHECK
121 static const char badmagic[] = KERN_INFO
122 "rc: Warning: bad riscom port magic number for device %s in %s\n";
123 static const char badinfo[] = KERN_INFO
124 "rc: Warning: null riscom port for device %s in %s\n";
125
126 if (!port) {
127 printk(badinfo, name, routine);
128 return 1;
129 }
130 if (port->magic != RISCOM8_MAGIC) {
131 printk(badmagic, name, routine);
132 return 1;
133 }
134#endif
135 return 0;
136}
137
138/*
9492e135 139 *
1da177e4 140 * Service functions for RISCom/8 driver.
9492e135 141 *
1da177e4
LT
142 */
143
144/* Get board number from pointer */
9492e135 145static inline int board_No(struct riscom_board const *bp)
1da177e4
LT
146{
147 return bp - rc_board;
148}
149
150/* Get port number from pointer */
9492e135 151static inline int port_No(struct riscom_port const *port)
1da177e4 152{
9492e135 153 return RC_PORT(port - rc_port);
1da177e4
LT
154}
155
156/* Get pointer to board from pointer to port */
9492e135 157static inline struct riscom_board *port_Board(struct riscom_port const *port)
1da177e4
LT
158{
159 return &rc_board[RC_BOARD(port - rc_port)];
160}
161
162/* Input Byte from CL CD180 register */
9492e135
AC
163static inline unsigned char rc_in(struct riscom_board const *bp,
164 unsigned short reg)
1da177e4
LT
165{
166 return inb(bp->base + RC_TO_ISA(reg));
167}
168
169/* Output Byte to CL CD180 register */
9492e135 170static inline void rc_out(struct riscom_board const *bp, unsigned short reg,
1da177e4
LT
171 unsigned char val)
172{
173 outb(val, bp->base + RC_TO_ISA(reg));
174}
175
176/* Wait for Channel Command Register ready */
9492e135 177static void rc_wait_CCR(struct riscom_board const *bp)
1da177e4
LT
178{
179 unsigned long delay;
180
181 /* FIXME: need something more descriptive then 100000 :) */
9492e135 182 for (delay = 100000; delay; delay--)
1da177e4
LT
183 if (!rc_in(bp, CD180_CCR))
184 return;
9492e135 185
1da177e4
LT
186 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
187}
188
189/*
190 * RISCom/8 probe functions.
191 */
192
9492e135 193static int rc_request_io_range(struct riscom_board * const bp)
1da177e4
LT
194{
195 int i;
9492e135
AC
196
197 for (i = 0; i < RC_NIOPORT; i++)
1da177e4
LT
198 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
199 "RISCom/8")) {
200 goto out_release;
201 }
202 return 0;
203out_release:
204 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
205 board_No(bp), bp->base);
9492e135 206 while (--i >= 0)
1da177e4
LT
207 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
208 return 1;
209}
210
9492e135 211static void rc_release_io_range(struct riscom_board * const bp)
1da177e4
LT
212{
213 int i;
9492e135
AC
214
215 for (i = 0; i < RC_NIOPORT; i++)
1da177e4
LT
216 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
217}
9492e135 218
1da177e4 219/* Reset and setup CD180 chip */
9492e135 220static void __init rc_init_CD180(struct riscom_board const *bp)
1da177e4
LT
221{
222 unsigned long flags;
9492e135 223
d9afa435
JG
224 spin_lock_irqsave(&riscom_lock, flags);
225
9492e135
AC
226 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
227 rc_wait_CCR(bp); /* Wait for CCR ready */
228 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
d9afa435 229 spin_unlock_irqrestore(&riscom_lock, flags);
9492e135 230 msleep(50); /* Delay 0.05 sec */
d9afa435 231 spin_lock_irqsave(&riscom_lock, flags);
9492e135
AC
232 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
233 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
234 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
235 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for tx intr */
236 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for rx intr */
237
1da177e4
LT
238 /* Setting up prescaler. We need 4 ticks per 1 ms */
239 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
240 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
9492e135 241
d9afa435 242 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
243}
244
245/* Main probing routine, also sets irq. */
246static int __init rc_probe(struct riscom_board *bp)
247{
248 unsigned char val1, val2;
249 int irqs = 0;
250 int retries;
9492e135 251
1da177e4
LT
252 bp->irq = 0;
253
254 if (rc_request_io_range(bp))
255 return 1;
9492e135 256
1da177e4
LT
257 /* Are the I/O ports here ? */
258 rc_out(bp, CD180_PPRL, 0x5a);
259 outb(0xff, 0x80);
260 val1 = rc_in(bp, CD180_PPRL);
261 rc_out(bp, CD180_PPRL, 0xa5);
262 outb(0x00, 0x80);
263 val2 = rc_in(bp, CD180_PPRL);
9492e135 264
1da177e4
LT
265 if ((val1 != 0x5a) || (val2 != 0xa5)) {
266 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
267 board_No(bp), bp->base);
268 goto out_release;
269 }
9492e135 270
1da177e4 271 /* It's time to find IRQ for this board */
9492e135 272 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
1da177e4 273 irqs = probe_irq_on();
9492e135
AC
274 rc_init_CD180(bp); /* Reset CD180 chip */
275 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
1da177e4 276 rc_wait_CCR(bp);
9492e135
AC
277 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
278 rc_out(bp, CD180_IER, IER_TXRDY);/* Enable tx empty intr */
c4ebd927 279 msleep(50);
1da177e4 280 irqs = probe_irq_off(irqs);
9492e135
AC
281 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
282 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
283 rc_init_CD180(bp); /* Reset CD180 again */
284
1da177e4
LT
285 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
286 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
287 "found.\n", board_No(bp), bp->base);
288 goto out_release;
289 }
290 }
9492e135 291
1da177e4
LT
292 if (irqs <= 0) {
293 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
294 "at 0x%03x.\n", board_No(bp), bp->base);
295 goto out_release;
296 }
297 bp->irq = irqs;
298 bp->flags |= RC_BOARD_PRESENT;
9492e135 299
1da177e4
LT
300 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
301 "0x%03x, IRQ %d.\n",
302 board_No(bp),
303 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
304 bp->base, bp->irq);
9492e135 305
1da177e4
LT
306 return 0;
307out_release:
308 rc_release_io_range(bp);
309 return 1;
310}
311
9492e135
AC
312/*
313 *
1da177e4 314 * Interrupt processing routines.
9492e135 315 *
1da177e4
LT
316 */
317
9492e135
AC
318static struct riscom_port *rc_get_port(struct riscom_board const *bp,
319 unsigned char const *what)
1da177e4
LT
320{
321 unsigned char channel;
9492e135
AC
322 struct riscom_port *port;
323
1da177e4
LT
324 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
325 if (channel < CD180_NCH) {
326 port = &rc_port[board_No(bp) * RC_NPORT + channel];
85f8f810 327 if (port->port.flags & ASYNC_INITIALIZED)
1da177e4 328 return port;
1da177e4 329 }
9492e135 330 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
1da177e4
LT
331 board_No(bp), what, channel);
332 return NULL;
333}
334
9492e135 335static void rc_receive_exc(struct riscom_board const *bp)
1da177e4
LT
336{
337 struct riscom_port *port;
338 struct tty_struct *tty;
339 unsigned char status;
33f0f88f 340 unsigned char ch, flag;
9492e135
AC
341
342 port = rc_get_port(bp, "Receive");
343 if (port == NULL)
1da177e4
LT
344 return;
345
6ff1ab28 346 tty = tty_port_tty_get(&port->port);
9492e135
AC
347
348#ifdef RC_REPORT_OVERRUN
1da177e4 349 status = rc_in(bp, CD180_RCSR);
33f0f88f 350 if (status & RCSR_OE)
1da177e4 351 port->overrun++;
1da177e4 352 status &= port->mark_mask;
9492e135 353#else
1da177e4 354 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
9492e135 355#endif
1da177e4 356 ch = rc_in(bp, CD180_RDR);
9492e135 357 if (!status)
6ff1ab28 358 goto out;
1da177e4
LT
359 if (status & RCSR_TOUT) {
360 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
9492e135 361 "Hardware problems ?\n",
1da177e4 362 board_No(bp), port_No(port));
6ff1ab28 363 goto out;
9492e135 364
1da177e4
LT
365 } else if (status & RCSR_BREAK) {
366 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
367 board_No(bp), port_No(port));
33f0f88f 368 flag = TTY_BREAK;
6ff1ab28 369 if (tty && (port->port.flags & ASYNC_SAK))
1da177e4 370 do_SAK(tty);
9492e135
AC
371
372 } else if (status & RCSR_PE)
33f0f88f 373 flag = TTY_PARITY;
9492e135
AC
374
375 else if (status & RCSR_FE)
33f0f88f 376 flag = TTY_FRAME;
9492e135
AC
377
378 else if (status & RCSR_OE)
33f0f88f 379 flag = TTY_OVERRUN;
1da177e4 380 else
33f0f88f 381 flag = TTY_NORMAL;
9492e135 382
6ff1ab28
AC
383 if (tty) {
384 tty_insert_flip_char(tty, ch, flag);
385 tty_flip_buffer_push(tty);
386 }
387out:
388 tty_kref_put(tty);
1da177e4
LT
389}
390
9492e135 391static void rc_receive(struct riscom_board const *bp)
1da177e4
LT
392{
393 struct riscom_port *port;
394 struct tty_struct *tty;
395 unsigned char count;
9492e135
AC
396
397 port = rc_get_port(bp, "Receive");
398 if (port == NULL)
1da177e4 399 return;
9492e135 400
6ff1ab28 401 tty = tty_port_tty_get(&port->port);
9492e135 402
1da177e4 403 count = rc_in(bp, CD180_RDCR);
9492e135 404
1da177e4
LT
405#ifdef RC_REPORT_FIFO
406 port->hits[count > 8 ? 9 : count]++;
9492e135
AC
407#endif
408
1da177e4 409 while (count--) {
6ff1ab28
AC
410 u8 ch = rc_in(bp, CD180_RDR);
411 if (tty)
412 tty_insert_flip_char(tty, ch, TTY_NORMAL);
413 }
414 if (tty) {
415 tty_flip_buffer_push(tty);
416 tty_kref_put(tty);
1da177e4 417 }
1da177e4
LT
418}
419
9492e135 420static void rc_transmit(struct riscom_board const *bp)
1da177e4
LT
421{
422 struct riscom_port *port;
423 struct tty_struct *tty;
424 unsigned char count;
9492e135
AC
425
426 port = rc_get_port(bp, "Transmit");
427 if (port == NULL)
1da177e4 428 return;
9492e135 429
6ff1ab28 430 tty = tty_port_tty_get(&port->port);
9492e135
AC
431
432 if (port->IER & IER_TXEMPTY) {
1da177e4
LT
433 /* FIFO drained */
434 rc_out(bp, CD180_CAR, port_No(port));
435 port->IER &= ~IER_TXEMPTY;
436 rc_out(bp, CD180_IER, port->IER);
6ff1ab28 437 goto out;
1da177e4 438 }
9492e135 439
1da177e4 440 if ((port->xmit_cnt <= 0 && !port->break_length)
6ff1ab28 441 || (tty && (tty->stopped || tty->hw_stopped))) {
1da177e4
LT
442 rc_out(bp, CD180_CAR, port_No(port));
443 port->IER &= ~IER_TXRDY;
444 rc_out(bp, CD180_IER, port->IER);
6ff1ab28 445 goto out;
1da177e4 446 }
9492e135 447
1da177e4
LT
448 if (port->break_length) {
449 if (port->break_length > 0) {
450 if (port->COR2 & COR2_ETC) {
451 rc_out(bp, CD180_TDR, CD180_C_ESC);
452 rc_out(bp, CD180_TDR, CD180_C_SBRK);
453 port->COR2 &= ~COR2_ETC;
454 }
455 count = min_t(int, port->break_length, 0xff);
456 rc_out(bp, CD180_TDR, CD180_C_ESC);
457 rc_out(bp, CD180_TDR, CD180_C_DELAY);
458 rc_out(bp, CD180_TDR, count);
9492e135
AC
459 port->break_length -= count;
460 if (port->break_length == 0)
1da177e4
LT
461 port->break_length--;
462 } else {
463 rc_out(bp, CD180_TDR, CD180_C_ESC);
464 rc_out(bp, CD180_TDR, CD180_C_EBRK);
465 rc_out(bp, CD180_COR2, port->COR2);
466 rc_wait_CCR(bp);
467 rc_out(bp, CD180_CCR, CCR_CORCHG2);
468 port->break_length = 0;
469 }
7e63d0c4 470 goto out;
1da177e4 471 }
9492e135 472
1da177e4
LT
473 count = CD180_NFIFO;
474 do {
85f8f810 475 rc_out(bp, CD180_TDR, port->port.xmit_buf[port->xmit_tail++]);
1da177e4
LT
476 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
477 if (--port->xmit_cnt <= 0)
478 break;
479 } while (--count > 0);
9492e135 480
1da177e4
LT
481 if (port->xmit_cnt <= 0) {
482 rc_out(bp, CD180_CAR, port_No(port));
483 port->IER &= ~IER_TXRDY;
484 rc_out(bp, CD180_IER, port->IER);
485 }
6ff1ab28 486 if (tty && port->xmit_cnt <= port->wakeup_chars)
b98e70de 487 tty_wakeup(tty);
6ff1ab28
AC
488out:
489 tty_kref_put(tty);
1da177e4
LT
490}
491
9492e135 492static void rc_check_modem(struct riscom_board const *bp)
1da177e4
LT
493{
494 struct riscom_port *port;
495 struct tty_struct *tty;
496 unsigned char mcr;
9492e135
AC
497
498 port = rc_get_port(bp, "Modem");
499 if (port == NULL)
1da177e4 500 return;
9492e135 501
6ff1ab28 502 tty = tty_port_tty_get(&port->port);
9492e135 503
1da177e4 504 mcr = rc_in(bp, CD180_MCR);
9492e135
AC
505 if (mcr & MCR_CDCHG) {
506 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
85f8f810 507 wake_up_interruptible(&port->port.open_wait);
6ff1ab28 508 else if (tty)
b98e70de 509 tty_hangup(tty);
1da177e4 510 }
9492e135 511
1da177e4
LT
512#ifdef RISCOM_BRAIN_DAMAGED_CTS
513 if (mcr & MCR_CTSCHG) {
514 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
1da177e4 515 port->IER |= IER_TXRDY;
6ff1ab28
AC
516 if (tty) {
517 tty->hw_stopped = 0;
518 if (port->xmit_cnt <= port->wakeup_chars)
519 tty_wakeup(tty);
520 }
1da177e4 521 } else {
6ff1ab28
AC
522 if (tty)
523 tty->hw_stopped = 1;
1da177e4
LT
524 port->IER &= ~IER_TXRDY;
525 }
526 rc_out(bp, CD180_IER, port->IER);
527 }
528 if (mcr & MCR_DSRCHG) {
529 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
1da177e4 530 port->IER |= IER_TXRDY;
6ff1ab28
AC
531 if (tty) {
532 tty->hw_stopped = 0;
533 if (port->xmit_cnt <= port->wakeup_chars)
534 tty_wakeup(tty);
535 }
1da177e4 536 } else {
6ff1ab28
AC
537 if (tty)
538 tty->hw_stopped = 1;
1da177e4
LT
539 port->IER &= ~IER_TXRDY;
540 }
541 rc_out(bp, CD180_IER, port->IER);
542 }
543#endif /* RISCOM_BRAIN_DAMAGED_CTS */
9492e135 544
1da177e4
LT
545 /* Clear change bits */
546 rc_out(bp, CD180_MCR, 0);
6ff1ab28 547 tty_kref_put(tty);
1da177e4
LT
548}
549
550/* The main interrupt processing routine */
9492e135 551static irqreturn_t rc_interrupt(int dummy, void *dev_id)
1da177e4
LT
552{
553 unsigned char status;
554 unsigned char ack;
f07ef395 555 struct riscom_board *bp = dev_id;
1da177e4
LT
556 unsigned long loop = 0;
557 int handled = 0;
558
c7bec5ab 559 if (!(bp->flags & RC_BOARD_ACTIVE))
1da177e4 560 return IRQ_NONE;
c7bec5ab 561
1da177e4
LT
562 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
563 (RC_BSR_TOUT | RC_BSR_TINT |
564 RC_BSR_MINT | RC_BSR_RINT))) {
565 handled = 1;
9492e135 566 if (status & RC_BSR_TOUT)
1da177e4
LT
567 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
568 "error?\n", board_No(bp));
1da177e4
LT
569 else if (status & RC_BSR_RINT) {
570 ack = rc_in(bp, RC_ACK_RINT);
1da177e4
LT
571 if (ack == (RC_ID | GIVR_IT_RCV))
572 rc_receive(bp);
573 else if (ack == (RC_ID | GIVR_IT_REXC))
574 rc_receive_exc(bp);
575 else
576 printk(KERN_WARNING "rc%d: Bad receive ack "
577 "0x%02x.\n",
578 board_No(bp), ack);
1da177e4
LT
579 } else if (status & RC_BSR_TINT) {
580 ack = rc_in(bp, RC_ACK_TINT);
1da177e4
LT
581 if (ack == (RC_ID | GIVR_IT_TX))
582 rc_transmit(bp);
583 else
584 printk(KERN_WARNING "rc%d: Bad transmit ack "
585 "0x%02x.\n",
586 board_No(bp), ack);
1da177e4
LT
587 } else /* if (status & RC_BSR_MINT) */ {
588 ack = rc_in(bp, RC_ACK_MINT);
9492e135 589 if (ack == (RC_ID | GIVR_IT_MODEM))
1da177e4
LT
590 rc_check_modem(bp);
591 else
592 printk(KERN_WARNING "rc%d: Bad modem ack "
593 "0x%02x.\n",
594 board_No(bp), ack);
9492e135 595 }
1da177e4
LT
596 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
597 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
598 }
599 return IRQ_RETVAL(handled);
600}
601
602/*
603 * Routines for open & close processing.
604 */
605
606/* Called with disabled interrupts */
9492e135 607static int rc_setup_board(struct riscom_board *bp)
1da177e4
LT
608{
609 int error;
610
9492e135 611 if (bp->flags & RC_BOARD_ACTIVE)
1da177e4 612 return 0;
9492e135 613
0f2ed4c6 614 error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
f07ef395 615 "RISCom/8", bp);
9492e135 616 if (error)
1da177e4 617 return error;
9492e135 618
1da177e4
LT
619 rc_out(bp, RC_CTOUT, 0); /* Just in case */
620 bp->DTR = ~0;
621 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
9492e135 622
1da177e4 623 bp->flags |= RC_BOARD_ACTIVE;
9492e135 624
1da177e4
LT
625 return 0;
626}
627
628/* Called with disabled interrupts */
f07ef395 629static void rc_shutdown_board(struct riscom_board *bp)
1da177e4
LT
630{
631 if (!(bp->flags & RC_BOARD_ACTIVE))
632 return;
9492e135 633
1da177e4 634 bp->flags &= ~RC_BOARD_ACTIVE;
9492e135 635
1da177e4 636 free_irq(bp->irq, NULL);
9492e135 637
1da177e4
LT
638 bp->DTR = ~0;
639 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
9492e135 640
1da177e4
LT
641}
642
643/*
9492e135 644 * Setting up port characteristics.
1da177e4
LT
645 * Must be called with disabled interrupts
646 */
6ff1ab28
AC
647static void rc_change_speed(struct tty_struct *tty, struct riscom_board *bp,
648 struct riscom_port *port)
1da177e4 649{
1da177e4
LT
650 unsigned long baud;
651 long tmp;
652 unsigned char cor1 = 0, cor3 = 0;
653 unsigned char mcor1 = 0, mcor2 = 0;
9492e135 654
1da177e4
LT
655 port->IER = 0;
656 port->COR2 = 0;
657 port->MSVR = MSVR_RTS;
9492e135 658
c7bce309 659 baud = tty_get_baud_rate(tty);
9492e135 660
1da177e4
LT
661 /* Select port on the board */
662 rc_out(bp, CD180_CAR, port_No(port));
9492e135 663
c7bce309 664 if (!baud) {
1da177e4
LT
665 /* Drop DTR & exit */
666 bp->DTR |= (1u << port_No(port));
667 rc_out(bp, RC_DTR, bp->DTR);
668 return;
669 } else {
670 /* Set DTR on */
671 bp->DTR &= ~(1u << port_No(port));
672 rc_out(bp, RC_DTR, bp->DTR);
673 }
9492e135 674
1da177e4 675 /*
9492e135 676 * Now we must calculate some speed depended things
1da177e4 677 */
9492e135 678
1da177e4 679 /* Set baud rate for port */
c7bce309 680 tmp = (((RC_OSCFREQ + baud/2) / baud +
1da177e4
LT
681 CD180_TPC/2) / CD180_TPC);
682
9492e135
AC
683 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
684 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
685 rc_out(bp, CD180_RBPRL, tmp & 0xff);
1da177e4 686 rc_out(bp, CD180_TBPRL, tmp & 0xff);
9492e135 687
c7bce309 688 baud = (baud + 5) / 10; /* Estimated CPS */
9492e135 689
1da177e4 690 /* Two timer ticks seems enough to wakeup something like SLIP driver */
9492e135 691 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
1da177e4
LT
692 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
693 SERIAL_XMIT_SIZE - 1 : tmp);
9492e135 694
1da177e4
LT
695 /* Receiver timeout will be transmission time for 1.5 chars */
696 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
697 tmp = (tmp > 0xff) ? 0xff : tmp;
698 rc_out(bp, CD180_RTPR, tmp);
9492e135
AC
699
700 switch (C_CSIZE(tty)) {
701 case CS5:
1da177e4
LT
702 cor1 |= COR1_5BITS;
703 break;
9492e135 704 case CS6:
1da177e4
LT
705 cor1 |= COR1_6BITS;
706 break;
9492e135 707 case CS7:
1da177e4
LT
708 cor1 |= COR1_7BITS;
709 break;
9492e135 710 case CS8:
1da177e4
LT
711 cor1 |= COR1_8BITS;
712 break;
713 }
9492e135 714 if (C_CSTOPB(tty))
1da177e4 715 cor1 |= COR1_2SB;
9492e135 716
1da177e4 717 cor1 |= COR1_IGNORE;
9492e135 718 if (C_PARENB(tty)) {
1da177e4 719 cor1 |= COR1_NORMPAR;
9492e135 720 if (C_PARODD(tty))
1da177e4 721 cor1 |= COR1_ODDP;
9492e135 722 if (I_INPCK(tty))
1da177e4
LT
723 cor1 &= ~COR1_IGNORE;
724 }
725 /* Set marking of some errors */
726 port->mark_mask = RCSR_OE | RCSR_TOUT;
9492e135 727 if (I_INPCK(tty))
1da177e4 728 port->mark_mask |= RCSR_FE | RCSR_PE;
9492e135 729 if (I_BRKINT(tty) || I_PARMRK(tty))
1da177e4 730 port->mark_mask |= RCSR_BREAK;
9492e135 731 if (I_IGNPAR(tty))
1da177e4 732 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
9492e135 733 if (I_IGNBRK(tty)) {
1da177e4 734 port->mark_mask &= ~RCSR_BREAK;
9492e135 735 if (I_IGNPAR(tty))
1da177e4
LT
736 /* Real raw mode. Ignore all */
737 port->mark_mask &= ~RCSR_OE;
738 }
739 /* Enable Hardware Flow Control */
740 if (C_CRTSCTS(tty)) {
741#ifdef RISCOM_BRAIN_DAMAGED_CTS
742 port->IER |= IER_DSR | IER_CTS;
743 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
744 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
9492e135
AC
745 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) &
746 (MSVR_CTS|MSVR_DSR));
1da177e4
LT
747#else
748 port->COR2 |= COR2_CTSAE;
749#endif
750 }
751 /* Enable Software Flow Control. FIXME: I'm not sure about this */
752 /* Some people reported that it works, but I still doubt */
753 if (I_IXON(tty)) {
754 port->COR2 |= COR2_TXIBE;
755 cor3 |= (COR3_FCT | COR3_SCDE);
756 if (I_IXANY(tty))
757 port->COR2 |= COR2_IXM;
758 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
759 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
760 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
761 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
762 }
763 if (!C_CLOCAL(tty)) {
764 /* Enable CD check */
765 port->IER |= IER_CD;
766 mcor1 |= MCOR1_CDZD;
767 mcor2 |= MCOR2_CDOD;
768 }
9492e135
AC
769
770 if (C_CREAD(tty))
1da177e4
LT
771 /* Enable receiver */
772 port->IER |= IER_RXD;
9492e135 773
1da177e4 774 /* Set input FIFO size (1-8 bytes) */
9492e135 775 cor3 |= RISCOM_RXFIFO;
1da177e4
LT
776 /* Setting up CD180 channel registers */
777 rc_out(bp, CD180_COR1, cor1);
778 rc_out(bp, CD180_COR2, port->COR2);
779 rc_out(bp, CD180_COR3, cor3);
780 /* Make CD180 know about registers change */
781 rc_wait_CCR(bp);
782 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
783 /* Setting up modem option registers */
784 rc_out(bp, CD180_MCOR1, mcor1);
785 rc_out(bp, CD180_MCOR2, mcor2);
786 /* Enable CD180 transmitter & receiver */
787 rc_wait_CCR(bp);
788 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
789 /* Enable interrupts */
790 rc_out(bp, CD180_IER, port->IER);
791 /* And finally set RTS on */
792 rc_out(bp, CD180_MSVR, port->MSVR);
793}
794
795/* Must be called with interrupts enabled */
8f1e6723 796static int rc_activate_port(struct tty_port *port, struct tty_struct *tty)
1da177e4 797{
8f1e6723
AC
798 struct riscom_port *rp = container_of(port, struct riscom_port, port);
799 struct riscom_board *bp = port_Board(rp);
1da177e4 800 unsigned long flags;
9492e135 801
8f1e6723 802 if (tty_port_alloc_xmit_buf(port) < 0)
85f8f810
AC
803 return -ENOMEM;
804
d9afa435
JG
805 spin_lock_irqsave(&riscom_lock, flags);
806
6ff1ab28 807 clear_bit(TTY_IO_ERROR, &tty->flags);
8f1e6723
AC
808 bp->count++;
809 rp->xmit_cnt = rp->xmit_head = rp->xmit_tail = 0;
810 rc_change_speed(tty, bp, rp);
d9afa435 811 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
812 return 0;
813}
814
815/* Must be called with interrupts disabled */
d99101fd
AC
816static void rc_shutdown_port(struct tty_struct *tty,
817 struct riscom_board *bp, struct riscom_port *port)
1da177e4 818{
1da177e4
LT
819#ifdef RC_REPORT_OVERRUN
820 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
821 board_No(bp), port_No(port), port->overrun);
9492e135 822#endif
1da177e4
LT
823#ifdef RC_REPORT_FIFO
824 {
825 int i;
9492e135 826
1da177e4
LT
827 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
828 board_No(bp), port_No(port));
9492e135 829 for (i = 0; i < 10; i++)
1da177e4 830 printk("%ld ", port->hits[i]);
1da177e4
LT
831 printk("].\n");
832 }
9492e135 833#endif
85f8f810 834 tty_port_free_xmit_buf(&port->port);
9492e135
AC
835
836 /* Select port */
1da177e4
LT
837 rc_out(bp, CD180_CAR, port_No(port));
838 /* Reset port */
839 rc_wait_CCR(bp);
840 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
841 /* Disable all interrupts from this port */
842 port->IER = 0;
843 rc_out(bp, CD180_IER, port->IER);
9492e135 844
d99101fd 845 set_bit(TTY_IO_ERROR, &tty->flags);
9492e135 846
1da177e4
LT
847 if (--bp->count < 0) {
848 printk(KERN_INFO "rc%d: rc_shutdown_port: "
849 "bad board count: %d\n",
850 board_No(bp), bp->count);
851 bp->count = 0;
852 }
1da177e4
LT
853 /*
854 * If this is the last opened port on the board
855 * shutdown whole board
856 */
9492e135 857 if (!bp->count)
1da177e4
LT
858 rc_shutdown_board(bp);
859}
860
31f35939
AC
861static int carrier_raised(struct tty_port *port)
862{
863 struct riscom_port *p = container_of(port, struct riscom_port, port);
864 struct riscom_board *bp = port_Board(p);
865 unsigned long flags;
866 int CD;
867
868 spin_lock_irqsave(&riscom_lock, flags);
869 rc_out(bp, CD180_CAR, port_No(p));
870 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
871 rc_out(bp, CD180_MSVR, MSVR_RTS);
872 bp->DTR &= ~(1u << port_No(p));
873 rc_out(bp, RC_DTR, bp->DTR);
874 spin_unlock_irqrestore(&riscom_lock, flags);
875 return CD;
876}
877
8f1e6723
AC
878static void dtr_rts(struct tty_port *port, int onoff)
879{
880 struct riscom_port *p = container_of(port, struct riscom_port, port);
881 struct riscom_board *bp = port_Board(p);
882 unsigned long flags;
883
884 spin_lock_irqsave(&riscom_lock, flags);
885 bp->DTR &= ~(1u << port_No(p));
886 if (onoff == 0)
887 bp->DTR |= (1u << port_No(p));
888 rc_out(bp, RC_DTR, bp->DTR);
889 spin_unlock_irqrestore(&riscom_lock, flags);
890}
891
9492e135 892static int rc_open(struct tty_struct *tty, struct file *filp)
1da177e4
LT
893{
894 int board;
895 int error;
9492e135
AC
896 struct riscom_port *port;
897 struct riscom_board *bp;
898
1da177e4
LT
899 board = RC_BOARD(tty->index);
900 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
901 return -ENODEV;
9492e135 902
1da177e4
LT
903 bp = &rc_board[board];
904 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
905 if (rc_paranoia_check(port, tty->name, "rc_open"))
906 return -ENODEV;
9492e135
AC
907
908 error = rc_setup_board(bp);
909 if (error)
1da177e4 910 return error;
9492e135 911
a2d1e351 912 tty->driver_data = port;
8f1e6723 913 return tty_port_open(&port->port, tty, filp);
1da177e4
LT
914}
915
978e595f
AC
916static void rc_flush_buffer(struct tty_struct *tty)
917{
c9f19e96 918 struct riscom_port *port = tty->driver_data;
978e595f
AC
919 unsigned long flags;
920
921 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
922 return;
923
924 spin_lock_irqsave(&riscom_lock, flags);
978e595f 925 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
978e595f
AC
926 spin_unlock_irqrestore(&riscom_lock, flags);
927
928 tty_wakeup(tty);
929}
930
90387f5e 931static void rc_close_port(struct tty_port *port)
1da177e4 932{
1da177e4 933 unsigned long flags;
c1469425
AC
934 struct riscom_port *rp = container_of(port, struct riscom_port, port);
935 struct riscom_board *bp = port_Board(rp);
1da177e4 936 unsigned long timeout;
a6614999 937
1da177e4
LT
938 /*
939 * At this point we stop accepting input. To do this, we
940 * disable the receive line status interrupts, and tell the
941 * interrupt driver to stop checking the data ready bit in the
942 * line status register.
943 */
c2ba38cd
AC
944
945 spin_lock_irqsave(&riscom_lock, flags);
c1469425 946 rp->IER &= ~IER_RXD;
8f1e6723
AC
947
948 rp->IER &= ~IER_TXRDY;
949 rp->IER |= IER_TXEMPTY;
950 rc_out(bp, CD180_CAR, port_No(rp));
951 rc_out(bp, CD180_IER, rp->IER);
952 /*
953 * Before we drop DTR, make sure the UART transmitter
954 * has completely drained; this is especially
955 * important if there is a transmit FIFO!
956 */
957 timeout = jiffies + HZ;
958 while (rp->IER & IER_TXEMPTY) {
959 spin_unlock_irqrestore(&riscom_lock, flags);
960 msleep_interruptible(jiffies_to_msecs(rp->timeout));
961 spin_lock_irqsave(&riscom_lock, flags);
962 if (time_after(jiffies, timeout))
963 break;
1da177e4 964 }
90387f5e 965 rc_shutdown_port(port->tty, bp, rp);
c2ba38cd 966 spin_unlock_irqrestore(&riscom_lock, flags);
c1469425
AC
967}
968
969static void rc_close(struct tty_struct *tty, struct file *filp)
970{
971 struct riscom_port *port = tty->driver_data;
972
973 if (!port || rc_paranoia_check(port, tty->name, "close"))
974 return;
6ff1ab28 975 tty_port_close(&port->port, tty, filp);
1da177e4
LT
976}
977
9492e135 978static int rc_write(struct tty_struct *tty,
1da177e4
LT
979 const unsigned char *buf, int count)
980{
c9f19e96 981 struct riscom_port *port = tty->driver_data;
1da177e4
LT
982 struct riscom_board *bp;
983 int c, total = 0;
984 unsigned long flags;
9492e135 985
1da177e4
LT
986 if (rc_paranoia_check(port, tty->name, "rc_write"))
987 return 0;
9492e135 988
1da177e4
LT
989 bp = port_Board(port);
990
1da177e4 991 while (1) {
d9afa435
JG
992 spin_lock_irqsave(&riscom_lock, flags);
993
1da177e4
LT
994 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
995 SERIAL_XMIT_SIZE - port->xmit_head));
d9afa435
JG
996 if (c <= 0)
997 break; /* lock continues to be held */
1da177e4 998
85f8f810 999 memcpy(port->port.xmit_buf + port->xmit_head, buf, c);
1da177e4
LT
1000 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1001 port->xmit_cnt += c;
d9afa435
JG
1002
1003 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1004
1005 buf += c;
1006 count -= c;
1007 total += c;
1008 }
1009
1da177e4
LT
1010 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1011 !(port->IER & IER_TXRDY)) {
1012 port->IER |= IER_TXRDY;
1013 rc_out(bp, CD180_CAR, port_No(port));
1014 rc_out(bp, CD180_IER, port->IER);
1015 }
d9afa435
JG
1016
1017 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1018
1019 return total;
1020}
1021
9492e135 1022static int rc_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4 1023{
c9f19e96 1024 struct riscom_port *port = tty->driver_data;
1da177e4 1025 unsigned long flags;
bbbbb96f 1026 int ret = 0;
1da177e4
LT
1027
1028 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
bbbbb96f 1029 return 0;
1da177e4 1030
d9afa435 1031 spin_lock_irqsave(&riscom_lock, flags);
9492e135 1032
1da177e4
LT
1033 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1034 goto out;
1035
85f8f810 1036 port->port.xmit_buf[port->xmit_head++] = ch;
1da177e4
LT
1037 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1038 port->xmit_cnt++;
bbbbb96f 1039 ret = 1;
d9afa435
JG
1040
1041out:
1042 spin_unlock_irqrestore(&riscom_lock, flags);
bbbbb96f 1043 return ret;
1da177e4
LT
1044}
1045
9492e135 1046static void rc_flush_chars(struct tty_struct *tty)
1da177e4 1047{
c9f19e96 1048 struct riscom_port *port = tty->driver_data;
1da177e4 1049 unsigned long flags;
9492e135 1050
1da177e4
LT
1051 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1052 return;
9492e135 1053
d99101fd 1054 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped)
1da177e4
LT
1055 return;
1056
d9afa435
JG
1057 spin_lock_irqsave(&riscom_lock, flags);
1058
1da177e4
LT
1059 port->IER |= IER_TXRDY;
1060 rc_out(port_Board(port), CD180_CAR, port_No(port));
1061 rc_out(port_Board(port), CD180_IER, port->IER);
d9afa435
JG
1062
1063 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1064}
1065
9492e135 1066static int rc_write_room(struct tty_struct *tty)
1da177e4 1067{
c9f19e96 1068 struct riscom_port *port = tty->driver_data;
1da177e4 1069 int ret;
9492e135 1070
1da177e4
LT
1071 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1072 return 0;
1073
1074 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1075 if (ret < 0)
1076 ret = 0;
1077 return ret;
1078}
1079
1080static int rc_chars_in_buffer(struct tty_struct *tty)
1081{
c9f19e96 1082 struct riscom_port *port = tty->driver_data;
9492e135 1083
1da177e4
LT
1084 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1085 return 0;
9492e135 1086
1da177e4
LT
1087 return port->xmit_cnt;
1088}
1089
1da177e4
LT
1090static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1091{
c9f19e96 1092 struct riscom_port *port = tty->driver_data;
9492e135 1093 struct riscom_board *bp;
1da177e4
LT
1094 unsigned char status;
1095 unsigned int result;
1096 unsigned long flags;
1097
bf9d8929 1098 if (rc_paranoia_check(port, tty->name, __func__))
1da177e4
LT
1099 return -ENODEV;
1100
1101 bp = port_Board(port);
d9afa435
JG
1102
1103 spin_lock_irqsave(&riscom_lock, flags);
1104
1da177e4
LT
1105 rc_out(bp, CD180_CAR, port_No(port));
1106 status = rc_in(bp, CD180_MSVR);
1107 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
d9afa435
JG
1108
1109 spin_unlock_irqrestore(&riscom_lock, flags);
1110
1da177e4
LT
1111 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1112 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1113 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1114 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1115 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1116 return result;
1117}
1118
1119static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1120 unsigned int set, unsigned int clear)
1121{
c9f19e96 1122 struct riscom_port *port = tty->driver_data;
1da177e4
LT
1123 unsigned long flags;
1124 struct riscom_board *bp;
1125
bf9d8929 1126 if (rc_paranoia_check(port, tty->name, __func__))
1da177e4
LT
1127 return -ENODEV;
1128
1129 bp = port_Board(port);
1130
d9afa435
JG
1131 spin_lock_irqsave(&riscom_lock, flags);
1132
1da177e4
LT
1133 if (set & TIOCM_RTS)
1134 port->MSVR |= MSVR_RTS;
1135 if (set & TIOCM_DTR)
1136 bp->DTR &= ~(1u << port_No(port));
1137
1138 if (clear & TIOCM_RTS)
1139 port->MSVR &= ~MSVR_RTS;
1140 if (clear & TIOCM_DTR)
1141 bp->DTR |= (1u << port_No(port));
1142
1143 rc_out(bp, CD180_CAR, port_No(port));
1144 rc_out(bp, CD180_MSVR, port->MSVR);
1145 rc_out(bp, RC_DTR, bp->DTR);
d9afa435
JG
1146
1147 spin_unlock_irqrestore(&riscom_lock, flags);
1148
1da177e4
LT
1149 return 0;
1150}
1151
781cff5c 1152static int rc_send_break(struct tty_struct *tty, int length)
1da177e4 1153{
c9f19e96 1154 struct riscom_port *port = tty->driver_data;
1da177e4
LT
1155 struct riscom_board *bp = port_Board(port);
1156 unsigned long flags;
9492e135 1157
781cff5c
AC
1158 if (length == 0 || length == -1)
1159 return -EOPNOTSUPP;
1160
d9afa435
JG
1161 spin_lock_irqsave(&riscom_lock, flags);
1162
1da177e4
LT
1163 port->break_length = RISCOM_TPS / HZ * length;
1164 port->COR2 |= COR2_ETC;
1165 port->IER |= IER_TXRDY;
1166 rc_out(bp, CD180_CAR, port_No(port));
1167 rc_out(bp, CD180_COR2, port->COR2);
1168 rc_out(bp, CD180_IER, port->IER);
1169 rc_wait_CCR(bp);
1170 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1171 rc_wait_CCR(bp);
d9afa435
JG
1172
1173 spin_unlock_irqrestore(&riscom_lock, flags);
781cff5c 1174 return 0;
1da177e4
LT
1175}
1176
6ff1ab28 1177static int rc_set_serial_info(struct tty_struct *tty, struct riscom_port *port,
9492e135 1178 struct serial_struct __user *newinfo)
1da177e4
LT
1179{
1180 struct serial_struct tmp;
1181 struct riscom_board *bp = port_Board(port);
1182 int change_speed;
9492e135 1183
1da177e4
LT
1184 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1185 return -EFAULT;
9492e135 1186
85f8f810 1187 change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
1da177e4 1188 (tmp.flags & ASYNC_SPD_MASK));
9492e135 1189
1da177e4 1190 if (!capable(CAP_SYS_ADMIN)) {
44b7d1b3
AC
1191 if ((tmp.close_delay != port->port.close_delay) ||
1192 (tmp.closing_wait != port->port.closing_wait) ||
1da177e4 1193 ((tmp.flags & ~ASYNC_USR_MASK) !=
85f8f810 1194 (port->port.flags & ~ASYNC_USR_MASK)))
1da177e4 1195 return -EPERM;
85f8f810 1196 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
1da177e4
LT
1197 (tmp.flags & ASYNC_USR_MASK));
1198 } else {
85f8f810 1199 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
1da177e4 1200 (tmp.flags & ASYNC_FLAGS));
44b7d1b3
AC
1201 port->port.close_delay = tmp.close_delay;
1202 port->port.closing_wait = tmp.closing_wait;
1da177e4
LT
1203 }
1204 if (change_speed) {
d9afa435
JG
1205 unsigned long flags;
1206
1207 spin_lock_irqsave(&riscom_lock, flags);
6ff1ab28 1208 rc_change_speed(tty, bp, port);
d9afa435 1209 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1210 }
1211 return 0;
1212}
1213
9492e135 1214static int rc_get_serial_info(struct riscom_port *port,
1da177e4
LT
1215 struct serial_struct __user *retinfo)
1216{
1217 struct serial_struct tmp;
1218 struct riscom_board *bp = port_Board(port);
9492e135 1219
1da177e4
LT
1220 memset(&tmp, 0, sizeof(tmp));
1221 tmp.type = PORT_CIRRUS;
1222 tmp.line = port - rc_port;
1223 tmp.port = bp->base;
1224 tmp.irq = bp->irq;
85f8f810 1225 tmp.flags = port->port.flags;
1da177e4 1226 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
44b7d1b3
AC
1227 tmp.close_delay = port->port.close_delay * HZ/100;
1228 tmp.closing_wait = port->port.closing_wait * HZ/100;
1da177e4
LT
1229 tmp.xmit_fifo_size = CD180_NFIFO;
1230 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1231}
1232
9492e135 1233static int rc_ioctl(struct tty_struct *tty, struct file *filp,
1da177e4 1234 unsigned int cmd, unsigned long arg)
1da177e4 1235{
c9f19e96 1236 struct riscom_port *port = tty->driver_data;
1da177e4 1237 void __user *argp = (void __user *)arg;
781cff5c 1238 int retval;
9492e135 1239
1da177e4
LT
1240 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1241 return -ENODEV;
9492e135 1242
1da177e4 1243 switch (cmd) {
9492e135
AC
1244 case TIOCGSERIAL:
1245 lock_kernel();
eb174552
AC
1246 retval = rc_get_serial_info(port, argp);
1247 unlock_kernel();
1da177e4 1248 break;
9492e135
AC
1249 case TIOCSSERIAL:
1250 lock_kernel();
6ff1ab28 1251 retval = rc_set_serial_info(tty, port, argp);
eb174552
AC
1252 unlock_kernel();
1253 break;
9492e135 1254 default:
eb174552 1255 retval = -ENOIOCTLCMD;
1da177e4 1256 }
eb174552 1257 return retval;
1da177e4
LT
1258}
1259
9492e135 1260static void rc_throttle(struct tty_struct *tty)
1da177e4 1261{
c9f19e96 1262 struct riscom_port *port = tty->driver_data;
1da177e4
LT
1263 struct riscom_board *bp;
1264 unsigned long flags;
9492e135 1265
1da177e4
LT
1266 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1267 return;
1da177e4 1268 bp = port_Board(port);
d9afa435
JG
1269
1270 spin_lock_irqsave(&riscom_lock, flags);
1da177e4
LT
1271 port->MSVR &= ~MSVR_RTS;
1272 rc_out(bp, CD180_CAR, port_No(port));
d9afa435 1273 if (I_IXOFF(tty)) {
1da177e4
LT
1274 rc_wait_CCR(bp);
1275 rc_out(bp, CD180_CCR, CCR_SSCH2);
1276 rc_wait_CCR(bp);
1277 }
1278 rc_out(bp, CD180_MSVR, port->MSVR);
d9afa435 1279 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1280}
1281
9492e135 1282static void rc_unthrottle(struct tty_struct *tty)
1da177e4 1283{
c9f19e96 1284 struct riscom_port *port = tty->driver_data;
1da177e4
LT
1285 struct riscom_board *bp;
1286 unsigned long flags;
9492e135 1287
1da177e4
LT
1288 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1289 return;
1da177e4 1290 bp = port_Board(port);
d9afa435 1291
9492e135 1292 spin_lock_irqsave(&riscom_lock, flags);
1da177e4
LT
1293 port->MSVR |= MSVR_RTS;
1294 rc_out(bp, CD180_CAR, port_No(port));
1295 if (I_IXOFF(tty)) {
1296 rc_wait_CCR(bp);
1297 rc_out(bp, CD180_CCR, CCR_SSCH1);
1298 rc_wait_CCR(bp);
1299 }
1300 rc_out(bp, CD180_MSVR, port->MSVR);
d9afa435 1301 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1302}
1303
9492e135 1304static void rc_stop(struct tty_struct *tty)
1da177e4 1305{
c9f19e96 1306 struct riscom_port *port = tty->driver_data;
1da177e4
LT
1307 struct riscom_board *bp;
1308 unsigned long flags;
9492e135 1309
1da177e4
LT
1310 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1311 return;
9492e135 1312
1da177e4 1313 bp = port_Board(port);
d9afa435 1314
9492e135 1315 spin_lock_irqsave(&riscom_lock, flags);
1da177e4
LT
1316 port->IER &= ~IER_TXRDY;
1317 rc_out(bp, CD180_CAR, port_No(port));
1318 rc_out(bp, CD180_IER, port->IER);
d9afa435 1319 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1320}
1321
9492e135 1322static void rc_start(struct tty_struct *tty)
1da177e4 1323{
c9f19e96 1324 struct riscom_port *port = tty->driver_data;
1da177e4
LT
1325 struct riscom_board *bp;
1326 unsigned long flags;
9492e135 1327
1da177e4
LT
1328 if (rc_paranoia_check(port, tty->name, "rc_start"))
1329 return;
9492e135 1330
1da177e4 1331 bp = port_Board(port);
9492e135 1332
d9afa435
JG
1333 spin_lock_irqsave(&riscom_lock, flags);
1334
85f8f810 1335 if (port->xmit_cnt && port->port.xmit_buf && !(port->IER & IER_TXRDY)) {
1da177e4
LT
1336 port->IER |= IER_TXRDY;
1337 rc_out(bp, CD180_CAR, port_No(port));
1338 rc_out(bp, CD180_IER, port->IER);
1339 }
d9afa435 1340 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1341}
1342
9492e135 1343static void rc_hangup(struct tty_struct *tty)
1da177e4 1344{
c9f19e96 1345 struct riscom_port *port = tty->driver_data;
9492e135 1346
1da177e4
LT
1347 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1348 return;
9492e135 1349
6ff1ab28 1350 tty_port_hangup(&port->port);
1da177e4
LT
1351}
1352
9492e135
AC
1353static void rc_set_termios(struct tty_struct *tty,
1354 struct ktermios *old_termios)
1da177e4 1355{
c9f19e96 1356 struct riscom_port *port = tty->driver_data;
1da177e4 1357 unsigned long flags;
9492e135 1358
1da177e4
LT
1359 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1360 return;
1da177e4 1361
d9afa435 1362 spin_lock_irqsave(&riscom_lock, flags);
6ff1ab28 1363 rc_change_speed(tty, port_Board(port), port);
d9afa435 1364 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1365
1366 if ((old_termios->c_cflag & CRTSCTS) &&
1367 !(tty->termios->c_cflag & CRTSCTS)) {
1368 tty->hw_stopped = 0;
1369 rc_start(tty);
1370 }
1371}
1372
b68e31d0 1373static const struct tty_operations riscom_ops = {
1da177e4
LT
1374 .open = rc_open,
1375 .close = rc_close,
1376 .write = rc_write,
1377 .put_char = rc_put_char,
1378 .flush_chars = rc_flush_chars,
1379 .write_room = rc_write_room,
1380 .chars_in_buffer = rc_chars_in_buffer,
1381 .flush_buffer = rc_flush_buffer,
1382 .ioctl = rc_ioctl,
1383 .throttle = rc_throttle,
1384 .unthrottle = rc_unthrottle,
1385 .set_termios = rc_set_termios,
1386 .stop = rc_stop,
1387 .start = rc_start,
1388 .hangup = rc_hangup,
1389 .tiocmget = rc_tiocmget,
1390 .tiocmset = rc_tiocmset,
781cff5c 1391 .break_ctl = rc_send_break,
1da177e4
LT
1392};
1393
31f35939
AC
1394static const struct tty_port_operations riscom_port_ops = {
1395 .carrier_raised = carrier_raised,
8f1e6723 1396 .dtr_rts = dtr_rts,
6ff1ab28 1397 .shutdown = rc_close_port,
8f1e6723 1398 .activate = rc_activate_port,
31f35939
AC
1399};
1400
1401
1386a820 1402static int __init rc_init_drivers(void)
1da177e4
LT
1403{
1404 int error;
1405 int i;
1406
1407 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
9492e135 1408 if (!riscom_driver)
1da177e4 1409 return -ENOMEM;
9492e135 1410
1da177e4
LT
1411 riscom_driver->owner = THIS_MODULE;
1412 riscom_driver->name = "ttyL";
1da177e4
LT
1413 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1414 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1415 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1416 riscom_driver->init_termios = tty_std_termios;
1417 riscom_driver->init_termios.c_cflag =
1418 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
606d099c
AC
1419 riscom_driver->init_termios.c_ispeed = 9600;
1420 riscom_driver->init_termios.c_ospeed = 9600;
781cff5c 1421 riscom_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_HARDWARE_BREAK;
1da177e4 1422 tty_set_operations(riscom_driver, &riscom_ops);
9492e135
AC
1423 error = tty_register_driver(riscom_driver);
1424 if (error != 0) {
1da177e4
LT
1425 put_tty_driver(riscom_driver);
1426 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
9492e135 1427 "error = %d\n", error);
1da177e4
LT
1428 return 1;
1429 }
1da177e4
LT
1430 memset(rc_port, 0, sizeof(rc_port));
1431 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
85f8f810 1432 tty_port_init(&rc_port[i].port);
31f35939 1433 rc_port[i].port.ops = &riscom_port_ops;
44b7d1b3 1434 rc_port[i].magic = RISCOM8_MAGIC;
1da177e4 1435 }
1da177e4
LT
1436 return 0;
1437}
1438
1439static void rc_release_drivers(void)
1440{
1da177e4
LT
1441 tty_unregister_driver(riscom_driver);
1442 put_tty_driver(riscom_driver);
1da177e4
LT
1443}
1444
1445#ifndef MODULE
1446/*
1447 * Called at boot time.
9492e135 1448 *
1da177e4
LT
1449 * You can specify IO base for up to RC_NBOARD cards,
1450 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1451 * Note that there will be no probing at default
1452 * addresses in this case.
1453 *
9492e135 1454 */
1da177e4
LT
1455static int __init riscom8_setup(char *str)
1456{
1457 int ints[RC_NBOARD];
1458 int i;
1459
1460 str = get_options(str, ARRAY_SIZE(ints), ints);
1461
1462 for (i = 0; i < RC_NBOARD; i++) {
1463 if (i < ints[0])
1464 rc_board[i].base = ints[i+1];
9492e135 1465 else
1da177e4
LT
1466 rc_board[i].base = 0;
1467 }
1468 return 1;
1469}
1470
1471__setup("riscom8=", riscom8_setup);
1472#endif
1473
1474static char banner[] __initdata =
1475 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1476 "1994-1996.\n";
1477static char no_boards_msg[] __initdata =
1478 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1479
9492e135
AC
1480/*
1481 * This routine must be called by kernel at boot time
1da177e4
LT
1482 */
1483static int __init riscom8_init(void)
1484{
1485 int i;
1486 int found = 0;
1487
1488 printk(banner);
1489
9492e135 1490 if (rc_init_drivers())
1da177e4
LT
1491 return -EIO;
1492
9492e135
AC
1493 for (i = 0; i < RC_NBOARD; i++)
1494 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1da177e4 1495 found++;
1da177e4
LT
1496 if (!found) {
1497 rc_release_drivers();
1498 printk(no_boards_msg);
1499 return -EIO;
1500 }
1501 return 0;
1502}
1503
1504#ifdef MODULE
1505static int iobase;
1506static int iobase1;
1507static int iobase2;
1508static int iobase3;
8d3b33f6
RR
1509module_param(iobase, int, 0);
1510module_param(iobase1, int, 0);
1511module_param(iobase2, int, 0);
1512module_param(iobase3, int, 0);
1da177e4
LT
1513
1514MODULE_LICENSE("GPL");
5c9f5806 1515MODULE_ALIAS_CHARDEV_MAJOR(RISCOM8_NORMAL_MAJOR);
1da177e4
LT
1516#endif /* MODULE */
1517
1518/*
1519 * You can setup up to 4 boards (current value of RC_NBOARD)
1520 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1521 *
1522 */
9492e135 1523static int __init riscom8_init_module(void)
1da177e4
LT
1524{
1525#ifdef MODULE
1526 int i;
1527
1528 if (iobase || iobase1 || iobase2 || iobase3) {
9492e135 1529 for (i = 0; i < RC_NBOARD; i++)
9efda797 1530 rc_board[i].base = 0;
1da177e4
LT
1531 }
1532
1533 if (iobase)
1534 rc_board[0].base = iobase;
1535 if (iobase1)
1536 rc_board[1].base = iobase1;
1537 if (iobase2)
1538 rc_board[2].base = iobase2;
1539 if (iobase3)
1540 rc_board[3].base = iobase3;
1541#endif /* MODULE */
1542
1543 return riscom8_init();
1544}
9492e135
AC
1545
1546static void __exit riscom8_exit_module(void)
1da177e4
LT
1547{
1548 int i;
9492e135 1549
1da177e4 1550 rc_release_drivers();
9492e135
AC
1551 for (i = 0; i < RC_NBOARD; i++)
1552 if (rc_board[i].flags & RC_BOARD_PRESENT)
1da177e4 1553 rc_release_io_range(&rc_board[i]);
9492e135 1554
1da177e4
LT
1555}
1556
1557module_init(riscom8_init_module);
1558module_exit(riscom8_exit_module);
This page took 0.615038 seconds and 5 git commands to generate.