drivers/char: replace remaining __FUNCTION__ occurrences
[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>
d9afa435 50#include <linux/spinlock.h>
1da177e4 51
9492e135 52#include <linux/uaccess.h>
1da177e4
LT
53
54#include "riscom8.h"
55#include "riscom8_reg.h"
56
57/* Am I paranoid or not ? ;-) */
58#define RISCOM_PARANOIA_CHECK
59
9492e135
AC
60/*
61 * Crazy InteliCom/8 boards sometimes have swapped CTS & DSR signals.
1da177e4 62 * You can slightly speed up things by #undefing the following option,
9492e135 63 * if you are REALLY sure that your board is correct one.
1da177e4
LT
64 */
65
66#define RISCOM_BRAIN_DAMAGED_CTS
67
9492e135 68/*
1da177e4
LT
69 * 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
1da177e4 81static struct tty_driver *riscom_driver;
1da177e4 82
d9afa435
JG
83static DEFINE_SPINLOCK(riscom_lock);
84
1da177e4
LT
85static 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[] = {
fe971071 104#if 1
1da177e4 105 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
fe971071 106#else
1da177e4
LT
107 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
fe971071 110#endif
1da177e4 111};
fe971071 112#define RC_NIOPORT ARRAY_SIZE(rc_ioport)
1da177e4
LT
113
114
9492e135 115static int rc_paranoia_check(struct riscom_port const *port,
1da177e4
LT
116 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/*
9492e135 137 *
1da177e4 138 * Service functions for RISCom/8 driver.
9492e135 139 *
1da177e4
LT
140 */
141
142/* Get board number from pointer */
9492e135 143static inline int board_No(struct riscom_board const *bp)
1da177e4
LT
144{
145 return bp - rc_board;
146}
147
148/* Get port number from pointer */
9492e135 149static inline int port_No(struct riscom_port const *port)
1da177e4 150{
9492e135 151 return RC_PORT(port - rc_port);
1da177e4
LT
152}
153
154/* Get pointer to board from pointer to port */
9492e135 155static inline struct riscom_board *port_Board(struct riscom_port const *port)
1da177e4
LT
156{
157 return &rc_board[RC_BOARD(port - rc_port)];
158}
159
160/* Input Byte from CL CD180 register */
9492e135
AC
161static inline unsigned char rc_in(struct riscom_board const *bp,
162 unsigned short reg)
1da177e4
LT
163{
164 return inb(bp->base + RC_TO_ISA(reg));
165}
166
167/* Output Byte to CL CD180 register */
9492e135 168static inline void rc_out(struct riscom_board const *bp, unsigned short reg,
1da177e4
LT
169 unsigned char val)
170{
171 outb(val, bp->base + RC_TO_ISA(reg));
172}
173
174/* Wait for Channel Command Register ready */
9492e135 175static void rc_wait_CCR(struct riscom_board const *bp)
1da177e4
LT
176{
177 unsigned long delay;
178
179 /* FIXME: need something more descriptive then 100000 :) */
9492e135 180 for (delay = 100000; delay; delay--)
1da177e4
LT
181 if (!rc_in(bp, CD180_CCR))
182 return;
9492e135 183
1da177e4
LT
184 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
185}
186
187/*
188 * RISCom/8 probe functions.
189 */
190
9492e135 191static int rc_request_io_range(struct riscom_board * const bp)
1da177e4
LT
192{
193 int i;
9492e135
AC
194
195 for (i = 0; i < RC_NIOPORT; i++)
1da177e4
LT
196 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);
9492e135 204 while (--i >= 0)
1da177e4
LT
205 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
206 return 1;
207}
208
9492e135 209static void rc_release_io_range(struct riscom_board * const bp)
1da177e4
LT
210{
211 int i;
9492e135
AC
212
213 for (i = 0; i < RC_NIOPORT; i++)
1da177e4
LT
214 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
215}
9492e135 216
1da177e4 217/* Reset and setup CD180 chip */
9492e135 218static void __init rc_init_CD180(struct riscom_board const *bp)
1da177e4
LT
219{
220 unsigned long flags;
9492e135 221
d9afa435
JG
222 spin_lock_irqsave(&riscom_lock, flags);
223
9492e135
AC
224 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 */
d9afa435 227 spin_unlock_irqrestore(&riscom_lock, flags);
9492e135 228 msleep(50); /* Delay 0.05 sec */
d9afa435 229 spin_lock_irqsave(&riscom_lock, flags);
9492e135
AC
230 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
1da177e4
LT
236 /* 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);
9492e135 239
d9afa435 240 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
241}
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;
9492e135 249
1da177e4
LT
250 bp->irq = 0;
251
252 if (rc_request_io_range(bp))
253 return 1;
9492e135 254
1da177e4
LT
255 /* 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);
9492e135 262
1da177e4
LT
263 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 }
9492e135 268
1da177e4 269 /* It's time to find IRQ for this board */
9492e135 270 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
1da177e4 271 irqs = probe_irq_on();
9492e135
AC
272 rc_init_CD180(bp); /* Reset CD180 chip */
273 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
1da177e4 274 rc_wait_CCR(bp);
9492e135
AC
275 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
276 rc_out(bp, CD180_IER, IER_TXRDY);/* Enable tx empty intr */
c4ebd927 277 msleep(50);
1da177e4 278 irqs = probe_irq_off(irqs);
9492e135
AC
279 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
1da177e4
LT
283 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 }
9492e135 289
1da177e4
LT
290 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;
9492e135 297
1da177e4
LT
298 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);
9492e135 303
1da177e4
LT
304 return 0;
305out_release:
306 rc_release_io_range(bp);
307 return 1;
308}
309
9492e135
AC
310/*
311 *
1da177e4 312 * Interrupt processing routines.
9492e135 313 *
1da177e4
LT
314 */
315
9492e135
AC
316static struct riscom_port *rc_get_port(struct riscom_board const *bp,
317 unsigned char const *what)
1da177e4
LT
318{
319 unsigned char channel;
9492e135
AC
320 struct riscom_port *port;
321
1da177e4
LT
322 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
323 if (channel < CD180_NCH) {
324 port = &rc_port[board_No(bp) * RC_NPORT + channel];
9492e135 325 if (port->flags & ASYNC_INITIALIZED)
1da177e4 326 return port;
1da177e4 327 }
9492e135 328 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
1da177e4
LT
329 board_No(bp), what, channel);
330 return NULL;
331}
332
9492e135 333static void rc_receive_exc(struct riscom_board const *bp)
1da177e4
LT
334{
335 struct riscom_port *port;
336 struct tty_struct *tty;
337 unsigned char status;
33f0f88f 338 unsigned char ch, flag;
9492e135
AC
339
340 port = rc_get_port(bp, "Receive");
341 if (port == NULL)
1da177e4
LT
342 return;
343
344 tty = port->tty;
9492e135
AC
345
346#ifdef RC_REPORT_OVERRUN
1da177e4 347 status = rc_in(bp, CD180_RCSR);
33f0f88f 348 if (status & RCSR_OE)
1da177e4 349 port->overrun++;
1da177e4 350 status &= port->mark_mask;
9492e135 351#else
1da177e4 352 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
9492e135 353#endif
1da177e4 354 ch = rc_in(bp, CD180_RDR);
9492e135 355 if (!status)
1da177e4 356 return;
1da177e4
LT
357 if (status & RCSR_TOUT) {
358 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
9492e135 359 "Hardware problems ?\n",
1da177e4
LT
360 board_No(bp), port_No(port));
361 return;
9492e135 362
1da177e4
LT
363 } else if (status & RCSR_BREAK) {
364 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
365 board_No(bp), port_No(port));
33f0f88f 366 flag = TTY_BREAK;
1da177e4
LT
367 if (port->flags & ASYNC_SAK)
368 do_SAK(tty);
9492e135
AC
369
370 } else if (status & RCSR_PE)
33f0f88f 371 flag = TTY_PARITY;
9492e135
AC
372
373 else if (status & RCSR_FE)
33f0f88f 374 flag = TTY_FRAME;
9492e135
AC
375
376 else if (status & RCSR_OE)
33f0f88f 377 flag = TTY_OVERRUN;
1da177e4 378 else
33f0f88f 379 flag = TTY_NORMAL;
9492e135 380
33f0f88f
AC
381 tty_insert_flip_char(tty, ch, flag);
382 tty_flip_buffer_push(tty);
1da177e4
LT
383}
384
9492e135 385static void rc_receive(struct riscom_board const *bp)
1da177e4
LT
386{
387 struct riscom_port *port;
388 struct tty_struct *tty;
389 unsigned char count;
9492e135
AC
390
391 port = rc_get_port(bp, "Receive");
392 if (port == NULL)
1da177e4 393 return;
9492e135 394
1da177e4 395 tty = port->tty;
9492e135 396
1da177e4 397 count = rc_in(bp, CD180_RDCR);
9492e135 398
1da177e4
LT
399#ifdef RC_REPORT_FIFO
400 port->hits[count > 8 ? 9 : count]++;
9492e135
AC
401#endif
402
1da177e4 403 while (count--) {
33f0f88f 404 if (tty_buffer_request_room(tty, 1) == 0) {
1da177e4
LT
405 printk(KERN_WARNING "rc%d: port %d: Working around "
406 "flip buffer overflow.\n",
407 board_No(bp), port_No(port));
408 break;
409 }
33f0f88f 410 tty_insert_flip_char(tty, rc_in(bp, CD180_RDR), TTY_NORMAL);
1da177e4 411 }
33f0f88f 412 tty_flip_buffer_push(tty);
1da177e4
LT
413}
414
9492e135 415static void rc_transmit(struct riscom_board const *bp)
1da177e4
LT
416{
417 struct riscom_port *port;
418 struct tty_struct *tty;
419 unsigned char count;
9492e135
AC
420
421 port = rc_get_port(bp, "Transmit");
422 if (port == NULL)
1da177e4 423 return;
9492e135 424
1da177e4 425 tty = port->tty;
9492e135
AC
426
427 if (port->IER & IER_TXEMPTY) {
1da177e4
LT
428 /* 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 }
9492e135 434
1da177e4
LT
435 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 }
9492e135 442
1da177e4
LT
443 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);
9492e135
AC
454 port->break_length -= count;
455 if (port->break_length == 0)
1da177e4
LT
456 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 }
9492e135 467
1da177e4
LT
468 count = CD180_NFIFO;
469 do {
470 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
471 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
472 if (--port->xmit_cnt <= 0)
473 break;
474 } while (--count > 0);
9492e135 475
1da177e4
LT
476 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)
b98e70de 482 tty_wakeup(tty);
1da177e4
LT
483}
484
9492e135 485static void rc_check_modem(struct riscom_board const *bp)
1da177e4
LT
486{
487 struct riscom_port *port;
488 struct tty_struct *tty;
489 unsigned char mcr;
9492e135
AC
490
491 port = rc_get_port(bp, "Modem");
492 if (port == NULL)
1da177e4 493 return;
9492e135 494
1da177e4 495 tty = port->tty;
9492e135 496
1da177e4 497 mcr = rc_in(bp, CD180_MCR);
9492e135
AC
498 if (mcr & MCR_CDCHG) {
499 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
1da177e4
LT
500 wake_up_interruptible(&port->open_wait);
501 else
b98e70de 502 tty_hangup(tty);
1da177e4 503 }
9492e135 504
1da177e4
LT
505#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)
b98e70de 511 tty_wakeup(tty);
1da177e4
LT
512 } 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)
b98e70de 523 tty_wakeup(tty);
1da177e4
LT
524 } 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 */
9492e135 531
1da177e4
LT
532 /* Clear change bits */
533 rc_out(bp, CD180_MCR, 0);
534}
535
536/* The main interrupt processing routine */
9492e135 537static irqreturn_t rc_interrupt(int dummy, void *dev_id)
1da177e4
LT
538{
539 unsigned char status;
540 unsigned char ack;
f07ef395 541 struct riscom_board *bp = dev_id;
1da177e4
LT
542 unsigned long loop = 0;
543 int handled = 0;
544
c7bec5ab 545 if (!(bp->flags & RC_BOARD_ACTIVE))
1da177e4 546 return IRQ_NONE;
c7bec5ab 547
1da177e4
LT
548 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;
9492e135 552 if (status & RC_BSR_TOUT)
1da177e4
LT
553 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
554 "error?\n", board_No(bp));
1da177e4
LT
555 else if (status & RC_BSR_RINT) {
556 ack = rc_in(bp, RC_ACK_RINT);
1da177e4
LT
557 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);
1da177e4
LT
565 } else if (status & RC_BSR_TINT) {
566 ack = rc_in(bp, RC_ACK_TINT);
1da177e4
LT
567 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);
1da177e4
LT
573 } else /* if (status & RC_BSR_MINT) */ {
574 ack = rc_in(bp, RC_ACK_MINT);
9492e135 575 if (ack == (RC_ID | GIVR_IT_MODEM))
1da177e4
LT
576 rc_check_modem(bp);
577 else
578 printk(KERN_WARNING "rc%d: Bad modem ack "
579 "0x%02x.\n",
580 board_No(bp), ack);
9492e135 581 }
1da177e4
LT
582 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 */
9492e135 593static int rc_setup_board(struct riscom_board *bp)
1da177e4
LT
594{
595 int error;
596
9492e135 597 if (bp->flags & RC_BOARD_ACTIVE)
1da177e4 598 return 0;
9492e135 599
0f2ed4c6 600 error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
f07ef395 601 "RISCom/8", bp);
9492e135 602 if (error)
1da177e4 603 return error;
9492e135 604
1da177e4
LT
605 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 */
9492e135 608
1da177e4 609 bp->flags |= RC_BOARD_ACTIVE;
9492e135 610
1da177e4
LT
611 return 0;
612}
613
614/* Called with disabled interrupts */
f07ef395 615static void rc_shutdown_board(struct riscom_board *bp)
1da177e4
LT
616{
617 if (!(bp->flags & RC_BOARD_ACTIVE))
618 return;
9492e135 619
1da177e4 620 bp->flags &= ~RC_BOARD_ACTIVE;
9492e135 621
1da177e4 622 free_irq(bp->irq, NULL);
9492e135 623
1da177e4
LT
624 bp->DTR = ~0;
625 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
9492e135 626
1da177e4
LT
627}
628
629/*
9492e135 630 * Setting up port characteristics.
1da177e4
LT
631 * Must be called with disabled interrupts
632 */
633static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
634{
9492e135 635 struct tty_struct *tty = port->tty;
1da177e4
LT
636 unsigned long baud;
637 long tmp;
638 unsigned char cor1 = 0, cor3 = 0;
639 unsigned char mcor1 = 0, mcor2 = 0;
9492e135
AC
640
641 if (tty == NULL || tty->termios == NULL)
1da177e4
LT
642 return;
643
644 port->IER = 0;
645 port->COR2 = 0;
646 port->MSVR = MSVR_RTS;
9492e135 647
c7bce309 648 baud = tty_get_baud_rate(tty);
9492e135 649
1da177e4
LT
650 /* Select port on the board */
651 rc_out(bp, CD180_CAR, port_No(port));
9492e135 652
c7bce309 653 if (!baud) {
1da177e4
LT
654 /* Drop DTR & exit */
655 bp->DTR |= (1u << port_No(port));
656 rc_out(bp, RC_DTR, bp->DTR);
657 return;
658 } else {
659 /* Set DTR on */
660 bp->DTR &= ~(1u << port_No(port));
661 rc_out(bp, RC_DTR, bp->DTR);
662 }
9492e135 663
1da177e4 664 /*
9492e135 665 * Now we must calculate some speed depended things
1da177e4 666 */
9492e135 667
1da177e4 668 /* Set baud rate for port */
c7bce309 669 tmp = (((RC_OSCFREQ + baud/2) / baud +
1da177e4
LT
670 CD180_TPC/2) / CD180_TPC);
671
9492e135
AC
672 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
673 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
674 rc_out(bp, CD180_RBPRL, tmp & 0xff);
1da177e4 675 rc_out(bp, CD180_TBPRL, tmp & 0xff);
9492e135 676
c7bce309 677 baud = (baud + 5) / 10; /* Estimated CPS */
9492e135 678
1da177e4 679 /* Two timer ticks seems enough to wakeup something like SLIP driver */
9492e135 680 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
1da177e4
LT
681 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
682 SERIAL_XMIT_SIZE - 1 : tmp);
9492e135 683
1da177e4
LT
684 /* Receiver timeout will be transmission time for 1.5 chars */
685 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
686 tmp = (tmp > 0xff) ? 0xff : tmp;
687 rc_out(bp, CD180_RTPR, tmp);
9492e135
AC
688
689 switch (C_CSIZE(tty)) {
690 case CS5:
1da177e4
LT
691 cor1 |= COR1_5BITS;
692 break;
9492e135 693 case CS6:
1da177e4
LT
694 cor1 |= COR1_6BITS;
695 break;
9492e135 696 case CS7:
1da177e4
LT
697 cor1 |= COR1_7BITS;
698 break;
9492e135 699 case CS8:
1da177e4
LT
700 cor1 |= COR1_8BITS;
701 break;
702 }
9492e135 703 if (C_CSTOPB(tty))
1da177e4 704 cor1 |= COR1_2SB;
9492e135 705
1da177e4 706 cor1 |= COR1_IGNORE;
9492e135 707 if (C_PARENB(tty)) {
1da177e4 708 cor1 |= COR1_NORMPAR;
9492e135 709 if (C_PARODD(tty))
1da177e4 710 cor1 |= COR1_ODDP;
9492e135 711 if (I_INPCK(tty))
1da177e4
LT
712 cor1 &= ~COR1_IGNORE;
713 }
714 /* Set marking of some errors */
715 port->mark_mask = RCSR_OE | RCSR_TOUT;
9492e135 716 if (I_INPCK(tty))
1da177e4 717 port->mark_mask |= RCSR_FE | RCSR_PE;
9492e135 718 if (I_BRKINT(tty) || I_PARMRK(tty))
1da177e4 719 port->mark_mask |= RCSR_BREAK;
9492e135 720 if (I_IGNPAR(tty))
1da177e4 721 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
9492e135 722 if (I_IGNBRK(tty)) {
1da177e4 723 port->mark_mask &= ~RCSR_BREAK;
9492e135 724 if (I_IGNPAR(tty))
1da177e4
LT
725 /* Real raw mode. Ignore all */
726 port->mark_mask &= ~RCSR_OE;
727 }
728 /* Enable Hardware Flow Control */
729 if (C_CRTSCTS(tty)) {
730#ifdef RISCOM_BRAIN_DAMAGED_CTS
731 port->IER |= IER_DSR | IER_CTS;
732 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
733 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
9492e135
AC
734 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) &
735 (MSVR_CTS|MSVR_DSR));
1da177e4
LT
736#else
737 port->COR2 |= COR2_CTSAE;
738#endif
739 }
740 /* Enable Software Flow Control. FIXME: I'm not sure about this */
741 /* Some people reported that it works, but I still doubt */
742 if (I_IXON(tty)) {
743 port->COR2 |= COR2_TXIBE;
744 cor3 |= (COR3_FCT | COR3_SCDE);
745 if (I_IXANY(tty))
746 port->COR2 |= COR2_IXM;
747 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
748 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
749 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
750 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
751 }
752 if (!C_CLOCAL(tty)) {
753 /* Enable CD check */
754 port->IER |= IER_CD;
755 mcor1 |= MCOR1_CDZD;
756 mcor2 |= MCOR2_CDOD;
757 }
9492e135
AC
758
759 if (C_CREAD(tty))
1da177e4
LT
760 /* Enable receiver */
761 port->IER |= IER_RXD;
9492e135 762
1da177e4 763 /* Set input FIFO size (1-8 bytes) */
9492e135 764 cor3 |= RISCOM_RXFIFO;
1da177e4
LT
765 /* Setting up CD180 channel registers */
766 rc_out(bp, CD180_COR1, cor1);
767 rc_out(bp, CD180_COR2, port->COR2);
768 rc_out(bp, CD180_COR3, cor3);
769 /* Make CD180 know about registers change */
770 rc_wait_CCR(bp);
771 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
772 /* Setting up modem option registers */
773 rc_out(bp, CD180_MCOR1, mcor1);
774 rc_out(bp, CD180_MCOR2, mcor2);
775 /* Enable CD180 transmitter & receiver */
776 rc_wait_CCR(bp);
777 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
778 /* Enable interrupts */
779 rc_out(bp, CD180_IER, port->IER);
780 /* And finally set RTS on */
781 rc_out(bp, CD180_MSVR, port->MSVR);
782}
783
784/* Must be called with interrupts enabled */
785static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
786{
787 unsigned long flags;
9492e135 788
1da177e4
LT
789 if (port->flags & ASYNC_INITIALIZED)
790 return 0;
9492e135 791
1da177e4
LT
792 if (!port->xmit_buf) {
793 /* We may sleep in get_zeroed_page() */
9492e135
AC
794 unsigned long tmp = get_zeroed_page(GFP_KERNEL);
795 if (tmp == 0)
1da177e4 796 return -ENOMEM;
9492e135 797 if (port->xmit_buf)
1da177e4 798 free_page(tmp);
9492e135
AC
799 else
800 port->xmit_buf = (unsigned char *) tmp;
1da177e4 801 }
d9afa435
JG
802 spin_lock_irqsave(&riscom_lock, flags);
803
9492e135 804 if (port->tty)
1da177e4 805 clear_bit(TTY_IO_ERROR, &port->tty->flags);
9492e135 806 if (port->count == 1)
1da177e4 807 bp->count++;
1da177e4
LT
808 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
809 rc_change_speed(bp, port);
810 port->flags |= ASYNC_INITIALIZED;
9492e135 811
d9afa435 812 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
813 return 0;
814}
815
816/* Must be called with interrupts disabled */
817static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
818{
819 struct tty_struct *tty;
9492e135
AC
820
821 if (!(port->flags & ASYNC_INITIALIZED))
1da177e4 822 return;
9492e135 823
1da177e4
LT
824#ifdef RC_REPORT_OVERRUN
825 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
826 board_No(bp), port_No(port), port->overrun);
9492e135 827#endif
1da177e4
LT
828#ifdef RC_REPORT_FIFO
829 {
830 int i;
9492e135 831
1da177e4
LT
832 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
833 board_No(bp), port_No(port));
9492e135 834 for (i = 0; i < 10; i++)
1da177e4 835 printk("%ld ", port->hits[i]);
1da177e4
LT
836 printk("].\n");
837 }
9492e135 838#endif
1da177e4
LT
839 if (port->xmit_buf) {
840 free_page((unsigned long) port->xmit_buf);
841 port->xmit_buf = NULL;
842 }
843
9492e135
AC
844 tty = port->tty;
845
846 if (tty == NULL || C_HUPCL(tty)) {
1da177e4
LT
847 /* Drop DTR */
848 bp->DTR |= (1u << port_No(port));
849 rc_out(bp, RC_DTR, bp->DTR);
850 }
9492e135
AC
851
852 /* Select port */
1da177e4
LT
853 rc_out(bp, CD180_CAR, port_No(port));
854 /* Reset port */
855 rc_wait_CCR(bp);
856 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
857 /* Disable all interrupts from this port */
858 port->IER = 0;
859 rc_out(bp, CD180_IER, port->IER);
9492e135
AC
860
861 if (tty)
1da177e4
LT
862 set_bit(TTY_IO_ERROR, &tty->flags);
863 port->flags &= ~ASYNC_INITIALIZED;
9492e135 864
1da177e4
LT
865 if (--bp->count < 0) {
866 printk(KERN_INFO "rc%d: rc_shutdown_port: "
867 "bad board count: %d\n",
868 board_No(bp), bp->count);
869 bp->count = 0;
870 }
1da177e4
LT
871 /*
872 * If this is the last opened port on the board
873 * shutdown whole board
874 */
9492e135 875 if (!bp->count)
1da177e4
LT
876 rc_shutdown_board(bp);
877}
878
9492e135 879static int block_til_ready(struct tty_struct *tty, struct file *filp,
1da177e4
LT
880 struct riscom_port *port)
881{
882 DECLARE_WAITQUEUE(wait, current);
883 struct riscom_board *bp = port_Board(port);
884 int retval;
885 int do_clocal = 0;
886 int CD;
d9afa435 887 unsigned long flags;
1da177e4
LT
888
889 /*
890 * If the device is in the middle of being closed, then block
891 * until it's done, and then try again.
892 */
893 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
894 interruptible_sleep_on(&port->close_wait);
895 if (port->flags & ASYNC_HUP_NOTIFY)
896 return -EAGAIN;
897 else
898 return -ERESTARTSYS;
899 }
900
901 /*
902 * If non-blocking mode is set, or the port is not enabled,
903 * then make the check up front and then exit.
904 */
905 if ((filp->f_flags & O_NONBLOCK) ||
906 (tty->flags & (1 << TTY_IO_ERROR))) {
907 port->flags |= ASYNC_NORMAL_ACTIVE;
908 return 0;
909 }
910
9492e135 911 if (C_CLOCAL(tty))
1da177e4
LT
912 do_clocal = 1;
913
914 /*
915 * Block waiting for the carrier detect and the line to become
916 * free (i.e., not in use by the callout). While we are in
917 * this loop, info->count is dropped by one, so that
918 * rs_close() knows when to free things. We restore it upon
919 * exit, either normal or abnormal.
920 */
921 retval = 0;
922 add_wait_queue(&port->open_wait, &wait);
d9afa435
JG
923
924 spin_lock_irqsave(&riscom_lock, flags);
925
1da177e4
LT
926 if (!tty_hung_up_p(filp))
927 port->count--;
d9afa435
JG
928
929 spin_unlock_irqrestore(&riscom_lock, flags);
930
1da177e4
LT
931 port->blocked_open++;
932 while (1) {
d9afa435
JG
933 spin_lock_irqsave(&riscom_lock, flags);
934
1da177e4
LT
935 rc_out(bp, CD180_CAR, port_No(port));
936 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
937 rc_out(bp, CD180_MSVR, MSVR_RTS);
938 bp->DTR &= ~(1u << port_No(port));
939 rc_out(bp, RC_DTR, bp->DTR);
d9afa435
JG
940
941 spin_unlock_irqrestore(&riscom_lock, flags);
942
1da177e4
LT
943 set_current_state(TASK_INTERRUPTIBLE);
944 if (tty_hung_up_p(filp) ||
945 !(port->flags & ASYNC_INITIALIZED)) {
946 if (port->flags & ASYNC_HUP_NOTIFY)
947 retval = -EAGAIN;
948 else
9492e135 949 retval = -ERESTARTSYS;
1da177e4
LT
950 break;
951 }
952 if (!(port->flags & ASYNC_CLOSING) &&
953 (do_clocal || CD))
954 break;
955 if (signal_pending(current)) {
956 retval = -ERESTARTSYS;
957 break;
958 }
959 schedule();
960 }
cc0a8fbb 961 __set_current_state(TASK_RUNNING);
1da177e4
LT
962 remove_wait_queue(&port->open_wait, &wait);
963 if (!tty_hung_up_p(filp))
964 port->count++;
965 port->blocked_open--;
966 if (retval)
967 return retval;
9492e135 968
1da177e4
LT
969 port->flags |= ASYNC_NORMAL_ACTIVE;
970 return 0;
9492e135 971}
1da177e4 972
9492e135 973static int rc_open(struct tty_struct *tty, struct file *filp)
1da177e4
LT
974{
975 int board;
976 int error;
9492e135
AC
977 struct riscom_port *port;
978 struct riscom_board *bp;
979
1da177e4
LT
980 board = RC_BOARD(tty->index);
981 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
982 return -ENODEV;
9492e135 983
1da177e4
LT
984 bp = &rc_board[board];
985 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
986 if (rc_paranoia_check(port, tty->name, "rc_open"))
987 return -ENODEV;
9492e135
AC
988
989 error = rc_setup_board(bp);
990 if (error)
1da177e4 991 return error;
9492e135 992
1da177e4
LT
993 port->count++;
994 tty->driver_data = port;
995 port->tty = tty;
9492e135
AC
996
997 error = rc_setup_port(bp, port);
998 if (error == 0)
999 error = block_til_ready(tty, filp, port);
1000 return error;
1da177e4
LT
1001}
1002
978e595f
AC
1003static void rc_flush_buffer(struct tty_struct *tty)
1004{
1005 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1006 unsigned long flags;
1007
1008 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
1009 return;
1010
1011 spin_lock_irqsave(&riscom_lock, flags);
978e595f 1012 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
978e595f
AC
1013 spin_unlock_irqrestore(&riscom_lock, flags);
1014
1015 tty_wakeup(tty);
1016}
1017
9492e135 1018static void rc_close(struct tty_struct *tty, struct file *filp)
1da177e4
LT
1019{
1020 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1021 struct riscom_board *bp;
1022 unsigned long flags;
1023 unsigned long timeout;
9492e135 1024
1da177e4
LT
1025 if (!port || rc_paranoia_check(port, tty->name, "close"))
1026 return;
d9afa435
JG
1027
1028 spin_lock_irqsave(&riscom_lock, flags);
1029
1da177e4
LT
1030 if (tty_hung_up_p(filp))
1031 goto out;
9492e135 1032
1da177e4
LT
1033 bp = port_Board(port);
1034 if ((tty->count == 1) && (port->count != 1)) {
1035 printk(KERN_INFO "rc%d: rc_close: bad port count;"
1036 " tty->count is 1, port count is %d\n",
1037 board_No(bp), port->count);
1038 port->count = 1;
1039 }
1040 if (--port->count < 0) {
1041 printk(KERN_INFO "rc%d: rc_close: bad port count "
1042 "for tty%d: %d\n",
1043 board_No(bp), port_No(port), port->count);
1044 port->count = 0;
1045 }
1046 if (port->count)
1047 goto out;
1048 port->flags |= ASYNC_CLOSING;
1049 /*
9492e135 1050 * Now we wait for the transmit buffer to clear; and we notify
1da177e4
LT
1051 * the line discipline to only process XON/XOFF characters.
1052 */
1053 tty->closing = 1;
1054 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1055 tty_wait_until_sent(tty, port->closing_wait);
1056 /*
1057 * At this point we stop accepting input. To do this, we
1058 * disable the receive line status interrupts, and tell the
1059 * interrupt driver to stop checking the data ready bit in the
1060 * line status register.
1061 */
1062 port->IER &= ~IER_RXD;
1063 if (port->flags & ASYNC_INITIALIZED) {
1064 port->IER &= ~IER_TXRDY;
1065 port->IER |= IER_TXEMPTY;
1066 rc_out(bp, CD180_CAR, port_No(port));
1067 rc_out(bp, CD180_IER, port->IER);
1068 /*
1069 * Before we drop DTR, make sure the UART transmitter
1070 * has completely drained; this is especially
1071 * important if there is a transmit FIFO!
1072 */
9492e135
AC
1073 timeout = jiffies + HZ;
1074 while (port->IER & IER_TXEMPTY) {
1da177e4
LT
1075 msleep_interruptible(jiffies_to_msecs(port->timeout));
1076 if (time_after(jiffies, timeout))
1077 break;
1078 }
1079 }
1080 rc_shutdown_port(bp, port);
978e595f 1081 rc_flush_buffer(tty);
1da177e4
LT
1082 tty_ldisc_flush(tty);
1083
1084 tty->closing = 0;
1da177e4
LT
1085 port->tty = NULL;
1086 if (port->blocked_open) {
9492e135 1087 if (port->close_delay)
1da177e4 1088 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1da177e4
LT
1089 wake_up_interruptible(&port->open_wait);
1090 }
1091 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1092 wake_up_interruptible(&port->close_wait);
d9afa435
JG
1093
1094out:
1095 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1096}
1097
9492e135 1098static int rc_write(struct tty_struct *tty,
1da177e4
LT
1099 const unsigned char *buf, int count)
1100{
1101 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1102 struct riscom_board *bp;
1103 int c, total = 0;
1104 unsigned long flags;
9492e135 1105
1da177e4
LT
1106 if (rc_paranoia_check(port, tty->name, "rc_write"))
1107 return 0;
9492e135 1108
1da177e4
LT
1109 bp = port_Board(port);
1110
b3218a79 1111 if (!tty || !port->xmit_buf)
1da177e4
LT
1112 return 0;
1113
1da177e4 1114 while (1) {
d9afa435
JG
1115 spin_lock_irqsave(&riscom_lock, flags);
1116
1da177e4
LT
1117 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1118 SERIAL_XMIT_SIZE - port->xmit_head));
d9afa435
JG
1119 if (c <= 0)
1120 break; /* lock continues to be held */
1da177e4
LT
1121
1122 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1123 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1124 port->xmit_cnt += c;
d9afa435
JG
1125
1126 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1127
1128 buf += c;
1129 count -= c;
1130 total += c;
1131 }
1132
1da177e4
LT
1133 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1134 !(port->IER & IER_TXRDY)) {
1135 port->IER |= IER_TXRDY;
1136 rc_out(bp, CD180_CAR, port_No(port));
1137 rc_out(bp, CD180_IER, port->IER);
1138 }
d9afa435
JG
1139
1140 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1141
1142 return total;
1143}
1144
9492e135 1145static int rc_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4
LT
1146{
1147 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1148 unsigned long flags;
bbbbb96f 1149 int ret = 0;
1da177e4
LT
1150
1151 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
bbbbb96f 1152 return 0;
1da177e4
LT
1153
1154 if (!tty || !port->xmit_buf)
bbbbb96f 1155 return 0;
1da177e4 1156
d9afa435 1157 spin_lock_irqsave(&riscom_lock, flags);
9492e135 1158
1da177e4
LT
1159 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1160 goto out;
1161
1162 port->xmit_buf[port->xmit_head++] = ch;
1163 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1164 port->xmit_cnt++;
bbbbb96f 1165 ret = 1;
d9afa435
JG
1166
1167out:
1168 spin_unlock_irqrestore(&riscom_lock, flags);
bbbbb96f 1169 return ret;
1da177e4
LT
1170}
1171
9492e135 1172static void rc_flush_chars(struct tty_struct *tty)
1da177e4
LT
1173{
1174 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1175 unsigned long flags;
9492e135 1176
1da177e4
LT
1177 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1178 return;
9492e135 1179
1da177e4
LT
1180 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1181 !port->xmit_buf)
1182 return;
1183
d9afa435
JG
1184 spin_lock_irqsave(&riscom_lock, flags);
1185
1da177e4
LT
1186 port->IER |= IER_TXRDY;
1187 rc_out(port_Board(port), CD180_CAR, port_No(port));
1188 rc_out(port_Board(port), CD180_IER, port->IER);
d9afa435
JG
1189
1190 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1191}
1192
9492e135 1193static int rc_write_room(struct tty_struct *tty)
1da177e4
LT
1194{
1195 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1196 int ret;
9492e135 1197
1da177e4
LT
1198 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1199 return 0;
1200
1201 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1202 if (ret < 0)
1203 ret = 0;
1204 return ret;
1205}
1206
1207static int rc_chars_in_buffer(struct tty_struct *tty)
1208{
1209 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
9492e135 1210
1da177e4
LT
1211 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1212 return 0;
9492e135 1213
1da177e4
LT
1214 return port->xmit_cnt;
1215}
1216
1da177e4
LT
1217static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1218{
1219 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
9492e135 1220 struct riscom_board *bp;
1da177e4
LT
1221 unsigned char status;
1222 unsigned int result;
1223 unsigned long flags;
1224
bf9d8929 1225 if (rc_paranoia_check(port, tty->name, __func__))
1da177e4
LT
1226 return -ENODEV;
1227
1228 bp = port_Board(port);
d9afa435
JG
1229
1230 spin_lock_irqsave(&riscom_lock, flags);
1231
1da177e4
LT
1232 rc_out(bp, CD180_CAR, port_No(port));
1233 status = rc_in(bp, CD180_MSVR);
1234 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
d9afa435
JG
1235
1236 spin_unlock_irqrestore(&riscom_lock, flags);
1237
1da177e4
LT
1238 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1239 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1240 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1241 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1242 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1243 return result;
1244}
1245
1246static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1247 unsigned int set, unsigned int clear)
1248{
1249 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1250 unsigned long flags;
1251 struct riscom_board *bp;
1252
bf9d8929 1253 if (rc_paranoia_check(port, tty->name, __func__))
1da177e4
LT
1254 return -ENODEV;
1255
1256 bp = port_Board(port);
1257
d9afa435
JG
1258 spin_lock_irqsave(&riscom_lock, flags);
1259
1da177e4
LT
1260 if (set & TIOCM_RTS)
1261 port->MSVR |= MSVR_RTS;
1262 if (set & TIOCM_DTR)
1263 bp->DTR &= ~(1u << port_No(port));
1264
1265 if (clear & TIOCM_RTS)
1266 port->MSVR &= ~MSVR_RTS;
1267 if (clear & TIOCM_DTR)
1268 bp->DTR |= (1u << port_No(port));
1269
1270 rc_out(bp, CD180_CAR, port_No(port));
1271 rc_out(bp, CD180_MSVR, port->MSVR);
1272 rc_out(bp, RC_DTR, bp->DTR);
d9afa435
JG
1273
1274 spin_unlock_irqrestore(&riscom_lock, flags);
1275
1da177e4
LT
1276 return 0;
1277}
1278
9492e135 1279static void rc_send_break(struct riscom_port *port, unsigned long length)
1da177e4
LT
1280{
1281 struct riscom_board *bp = port_Board(port);
1282 unsigned long flags;
9492e135 1283
d9afa435
JG
1284 spin_lock_irqsave(&riscom_lock, flags);
1285
1da177e4
LT
1286 port->break_length = RISCOM_TPS / HZ * length;
1287 port->COR2 |= COR2_ETC;
1288 port->IER |= IER_TXRDY;
1289 rc_out(bp, CD180_CAR, port_No(port));
1290 rc_out(bp, CD180_COR2, port->COR2);
1291 rc_out(bp, CD180_IER, port->IER);
1292 rc_wait_CCR(bp);
1293 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1294 rc_wait_CCR(bp);
d9afa435
JG
1295
1296 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1297}
1298
9492e135
AC
1299static int rc_set_serial_info(struct riscom_port *port,
1300 struct serial_struct __user *newinfo)
1da177e4
LT
1301{
1302 struct serial_struct tmp;
1303 struct riscom_board *bp = port_Board(port);
1304 int change_speed;
9492e135 1305
1da177e4
LT
1306 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1307 return -EFAULT;
9492e135
AC
1308
1309#if 0
1da177e4
LT
1310 if ((tmp.irq != bp->irq) ||
1311 (tmp.port != bp->base) ||
1312 (tmp.type != PORT_CIRRUS) ||
1313 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1314 (tmp.custom_divisor != 0) ||
1315 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1316 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1317 return -EINVAL;
9492e135
AC
1318#endif
1319
1da177e4
LT
1320 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1321 (tmp.flags & ASYNC_SPD_MASK));
9492e135 1322
1da177e4
LT
1323 if (!capable(CAP_SYS_ADMIN)) {
1324 if ((tmp.close_delay != port->close_delay) ||
1325 (tmp.closing_wait != port->closing_wait) ||
1326 ((tmp.flags & ~ASYNC_USR_MASK) !=
9492e135 1327 (port->flags & ~ASYNC_USR_MASK)))
1da177e4
LT
1328 return -EPERM;
1329 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1330 (tmp.flags & ASYNC_USR_MASK));
1331 } else {
1332 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1333 (tmp.flags & ASYNC_FLAGS));
1334 port->close_delay = tmp.close_delay;
1335 port->closing_wait = tmp.closing_wait;
1336 }
1337 if (change_speed) {
d9afa435
JG
1338 unsigned long flags;
1339
1340 spin_lock_irqsave(&riscom_lock, flags);
1da177e4 1341 rc_change_speed(bp, port);
d9afa435 1342 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1343 }
1344 return 0;
1345}
1346
9492e135 1347static int rc_get_serial_info(struct riscom_port *port,
1da177e4
LT
1348 struct serial_struct __user *retinfo)
1349{
1350 struct serial_struct tmp;
1351 struct riscom_board *bp = port_Board(port);
9492e135 1352
1da177e4
LT
1353 memset(&tmp, 0, sizeof(tmp));
1354 tmp.type = PORT_CIRRUS;
1355 tmp.line = port - rc_port;
1356 tmp.port = bp->base;
1357 tmp.irq = bp->irq;
1358 tmp.flags = port->flags;
1359 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1360 tmp.close_delay = port->close_delay * HZ/100;
1361 tmp.closing_wait = port->closing_wait * HZ/100;
1362 tmp.xmit_fifo_size = CD180_NFIFO;
1363 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1364}
1365
9492e135 1366static int rc_ioctl(struct tty_struct *tty, struct file *filp,
1da177e4 1367 unsigned int cmd, unsigned long arg)
1da177e4
LT
1368{
1369 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1370 void __user *argp = (void __user *)arg;
eb174552 1371 int retval = 0;
9492e135 1372
1da177e4
LT
1373 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1374 return -ENODEV;
9492e135 1375
1da177e4 1376 switch (cmd) {
9492e135 1377 case TCSBRK: /* SVID version: non-zero arg --> no break */
1da177e4
LT
1378 retval = tty_check_change(tty);
1379 if (retval)
1380 return retval;
1381 tty_wait_until_sent(tty, 0);
1382 if (!arg)
1383 rc_send_break(port, HZ/4); /* 1/4 second */
1384 break;
9492e135 1385 case TCSBRKP: /* support for POSIX tcsendbreak() */
1da177e4
LT
1386 retval = tty_check_change(tty);
1387 if (retval)
1388 return retval;
1389 tty_wait_until_sent(tty, 0);
1390 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1391 break;
9492e135
AC
1392 case TIOCGSERIAL:
1393 lock_kernel();
eb174552
AC
1394 retval = rc_get_serial_info(port, argp);
1395 unlock_kernel();
1da177e4 1396 break;
9492e135
AC
1397 case TIOCSSERIAL:
1398 lock_kernel();
eb174552
AC
1399 retval = rc_set_serial_info(port, argp);
1400 unlock_kernel();
1401 break;
9492e135 1402 default:
eb174552 1403 retval = -ENOIOCTLCMD;
1da177e4 1404 }
eb174552 1405 return retval;
1da177e4
LT
1406}
1407
9492e135 1408static void rc_throttle(struct tty_struct *tty)
1da177e4
LT
1409{
1410 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1411 struct riscom_board *bp;
1412 unsigned long flags;
9492e135 1413
1da177e4
LT
1414 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1415 return;
1da177e4 1416 bp = port_Board(port);
d9afa435
JG
1417
1418 spin_lock_irqsave(&riscom_lock, flags);
1da177e4
LT
1419 port->MSVR &= ~MSVR_RTS;
1420 rc_out(bp, CD180_CAR, port_No(port));
d9afa435 1421 if (I_IXOFF(tty)) {
1da177e4
LT
1422 rc_wait_CCR(bp);
1423 rc_out(bp, CD180_CCR, CCR_SSCH2);
1424 rc_wait_CCR(bp);
1425 }
1426 rc_out(bp, CD180_MSVR, port->MSVR);
d9afa435 1427 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1428}
1429
9492e135 1430static void rc_unthrottle(struct tty_struct *tty)
1da177e4
LT
1431{
1432 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1433 struct riscom_board *bp;
1434 unsigned long flags;
9492e135 1435
1da177e4
LT
1436 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1437 return;
1da177e4 1438 bp = port_Board(port);
d9afa435 1439
9492e135 1440 spin_lock_irqsave(&riscom_lock, flags);
1da177e4
LT
1441 port->MSVR |= MSVR_RTS;
1442 rc_out(bp, CD180_CAR, port_No(port));
1443 if (I_IXOFF(tty)) {
1444 rc_wait_CCR(bp);
1445 rc_out(bp, CD180_CCR, CCR_SSCH1);
1446 rc_wait_CCR(bp);
1447 }
1448 rc_out(bp, CD180_MSVR, port->MSVR);
d9afa435 1449 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1450}
1451
9492e135 1452static void rc_stop(struct tty_struct *tty)
1da177e4
LT
1453{
1454 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1455 struct riscom_board *bp;
1456 unsigned long flags;
9492e135 1457
1da177e4
LT
1458 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1459 return;
9492e135 1460
1da177e4 1461 bp = port_Board(port);
d9afa435 1462
9492e135 1463 spin_lock_irqsave(&riscom_lock, flags);
1da177e4
LT
1464 port->IER &= ~IER_TXRDY;
1465 rc_out(bp, CD180_CAR, port_No(port));
1466 rc_out(bp, CD180_IER, port->IER);
d9afa435 1467 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1468}
1469
9492e135 1470static void rc_start(struct tty_struct *tty)
1da177e4
LT
1471{
1472 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1473 struct riscom_board *bp;
1474 unsigned long flags;
9492e135 1475
1da177e4
LT
1476 if (rc_paranoia_check(port, tty->name, "rc_start"))
1477 return;
9492e135 1478
1da177e4 1479 bp = port_Board(port);
9492e135 1480
d9afa435
JG
1481 spin_lock_irqsave(&riscom_lock, flags);
1482
9492e135 1483 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
1da177e4
LT
1484 port->IER |= IER_TXRDY;
1485 rc_out(bp, CD180_CAR, port_No(port));
1486 rc_out(bp, CD180_IER, port->IER);
1487 }
d9afa435 1488 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1489}
1490
9492e135 1491static void rc_hangup(struct tty_struct *tty)
1da177e4
LT
1492{
1493 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1494 struct riscom_board *bp;
9492e135 1495
1da177e4
LT
1496 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1497 return;
9492e135 1498
1da177e4 1499 bp = port_Board(port);
9492e135 1500
1da177e4 1501 rc_shutdown_port(bp, port);
1da177e4
LT
1502 port->count = 0;
1503 port->flags &= ~ASYNC_NORMAL_ACTIVE;
1504 port->tty = NULL;
1505 wake_up_interruptible(&port->open_wait);
1506}
1507
9492e135
AC
1508static void rc_set_termios(struct tty_struct *tty,
1509 struct ktermios *old_termios)
1da177e4
LT
1510{
1511 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1512 unsigned long flags;
9492e135 1513
1da177e4
LT
1514 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1515 return;
1da177e4 1516
d9afa435 1517 spin_lock_irqsave(&riscom_lock, flags);
1da177e4 1518 rc_change_speed(port_Board(port), port);
d9afa435 1519 spin_unlock_irqrestore(&riscom_lock, flags);
1da177e4
LT
1520
1521 if ((old_termios->c_cflag & CRTSCTS) &&
1522 !(tty->termios->c_cflag & CRTSCTS)) {
1523 tty->hw_stopped = 0;
1524 rc_start(tty);
1525 }
1526}
1527
b68e31d0 1528static const struct tty_operations riscom_ops = {
1da177e4
LT
1529 .open = rc_open,
1530 .close = rc_close,
1531 .write = rc_write,
1532 .put_char = rc_put_char,
1533 .flush_chars = rc_flush_chars,
1534 .write_room = rc_write_room,
1535 .chars_in_buffer = rc_chars_in_buffer,
1536 .flush_buffer = rc_flush_buffer,
1537 .ioctl = rc_ioctl,
1538 .throttle = rc_throttle,
1539 .unthrottle = rc_unthrottle,
1540 .set_termios = rc_set_termios,
1541 .stop = rc_stop,
1542 .start = rc_start,
1543 .hangup = rc_hangup,
1544 .tiocmget = rc_tiocmget,
1545 .tiocmset = rc_tiocmset,
1546};
1547
1386a820 1548static int __init rc_init_drivers(void)
1da177e4
LT
1549{
1550 int error;
1551 int i;
1552
1553 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
9492e135 1554 if (!riscom_driver)
1da177e4 1555 return -ENOMEM;
9492e135 1556
1da177e4
LT
1557 riscom_driver->owner = THIS_MODULE;
1558 riscom_driver->name = "ttyL";
1da177e4
LT
1559 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1560 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1561 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1562 riscom_driver->init_termios = tty_std_termios;
1563 riscom_driver->init_termios.c_cflag =
1564 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
606d099c
AC
1565 riscom_driver->init_termios.c_ispeed = 9600;
1566 riscom_driver->init_termios.c_ospeed = 9600;
1da177e4
LT
1567 riscom_driver->flags = TTY_DRIVER_REAL_RAW;
1568 tty_set_operations(riscom_driver, &riscom_ops);
9492e135
AC
1569 error = tty_register_driver(riscom_driver);
1570 if (error != 0) {
1da177e4
LT
1571 put_tty_driver(riscom_driver);
1572 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
9492e135 1573 "error = %d\n", error);
1da177e4
LT
1574 return 1;
1575 }
1da177e4
LT
1576 memset(rc_port, 0, sizeof(rc_port));
1577 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1578 rc_port[i].magic = RISCOM8_MAGIC;
9492e135
AC
1579 rc_port[i].close_delay = 50 * HZ / 100;
1580 rc_port[i].closing_wait = 3000 * HZ / 100;
1da177e4
LT
1581 init_waitqueue_head(&rc_port[i].open_wait);
1582 init_waitqueue_head(&rc_port[i].close_wait);
1583 }
1da177e4
LT
1584 return 0;
1585}
1586
1587static void rc_release_drivers(void)
1588{
1da177e4
LT
1589 tty_unregister_driver(riscom_driver);
1590 put_tty_driver(riscom_driver);
1da177e4
LT
1591}
1592
1593#ifndef MODULE
1594/*
1595 * Called at boot time.
9492e135 1596 *
1da177e4
LT
1597 * You can specify IO base for up to RC_NBOARD cards,
1598 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1599 * Note that there will be no probing at default
1600 * addresses in this case.
1601 *
9492e135 1602 */
1da177e4
LT
1603static int __init riscom8_setup(char *str)
1604{
1605 int ints[RC_NBOARD];
1606 int i;
1607
1608 str = get_options(str, ARRAY_SIZE(ints), ints);
1609
1610 for (i = 0; i < RC_NBOARD; i++) {
1611 if (i < ints[0])
1612 rc_board[i].base = ints[i+1];
9492e135 1613 else
1da177e4
LT
1614 rc_board[i].base = 0;
1615 }
1616 return 1;
1617}
1618
1619__setup("riscom8=", riscom8_setup);
1620#endif
1621
1622static char banner[] __initdata =
1623 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1624 "1994-1996.\n";
1625static char no_boards_msg[] __initdata =
1626 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1627
9492e135
AC
1628/*
1629 * This routine must be called by kernel at boot time
1da177e4
LT
1630 */
1631static int __init riscom8_init(void)
1632{
1633 int i;
1634 int found = 0;
1635
1636 printk(banner);
1637
9492e135 1638 if (rc_init_drivers())
1da177e4
LT
1639 return -EIO;
1640
9492e135
AC
1641 for (i = 0; i < RC_NBOARD; i++)
1642 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1da177e4 1643 found++;
1da177e4
LT
1644 if (!found) {
1645 rc_release_drivers();
1646 printk(no_boards_msg);
1647 return -EIO;
1648 }
1649 return 0;
1650}
1651
1652#ifdef MODULE
1653static int iobase;
1654static int iobase1;
1655static int iobase2;
1656static int iobase3;
8d3b33f6
RR
1657module_param(iobase, int, 0);
1658module_param(iobase1, int, 0);
1659module_param(iobase2, int, 0);
1660module_param(iobase3, int, 0);
1da177e4
LT
1661
1662MODULE_LICENSE("GPL");
1663#endif /* MODULE */
1664
1665/*
1666 * You can setup up to 4 boards (current value of RC_NBOARD)
1667 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1668 *
1669 */
9492e135 1670static int __init riscom8_init_module(void)
1da177e4
LT
1671{
1672#ifdef MODULE
1673 int i;
1674
1675 if (iobase || iobase1 || iobase2 || iobase3) {
9492e135 1676 for (i = 0; i < RC_NBOARD; i++)
9efda797 1677 rc_board[i].base = 0;
1da177e4
LT
1678 }
1679
1680 if (iobase)
1681 rc_board[0].base = iobase;
1682 if (iobase1)
1683 rc_board[1].base = iobase1;
1684 if (iobase2)
1685 rc_board[2].base = iobase2;
1686 if (iobase3)
1687 rc_board[3].base = iobase3;
1688#endif /* MODULE */
1689
1690 return riscom8_init();
1691}
9492e135
AC
1692
1693static void __exit riscom8_exit_module(void)
1da177e4
LT
1694{
1695 int i;
9492e135 1696
1da177e4 1697 rc_release_drivers();
9492e135
AC
1698 for (i = 0; i < RC_NBOARD; i++)
1699 if (rc_board[i].flags & RC_BOARD_PRESENT)
1da177e4 1700 rc_release_io_range(&rc_board[i]);
9492e135 1701
1da177e4
LT
1702}
1703
1704module_init(riscom8_init_module);
1705module_exit(riscom8_exit_module);
This page took 0.508791 seconds and 5 git commands to generate.