1 /*
2 * linux/drivers/char/amiserial.c
3 *
4 * Serial driver for the amiga builtin port.
5 *
6 * This code was created by taking serial.c version 4.30 from kernel
7 * release 2.3.22, replacing all hardware related stuff with the
8 * corresponding amiga hardware actions, and removing all irrelevant
9 * code. As a consequence, it uses many of the constants and names
10 * associated with the registers and bits of 16550 compatible UARTS -
11 * but only to keep track of status, etc in the state variables. It
12 * was done this was to make it easier to keep the code in line with
13 * (non hardware specific) changes to serial.c.
14 *
15 * The port is registered with the tty driver as minor device 64, and
16 * therefore other ports should should only use 65 upwards.
17 *
18 * Richard Lucock 28/12/99
19 *
20 * Copyright (C) 1991, 1992 Linus Torvalds
21 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
22 * 1998, 1999 Theodore Ts'o
23 *
24 */
25
26 /*
27 * Serial driver configuration section. Here are the various options:
28 *
29 * SERIAL_PARANOIA_CHECK
30 * Check the magic number for the async_structure where
31 * ever possible.
32 */
33
34 #include <linux/config.h>
35 #include <linux/version.h>
36
37 #undef SERIAL_PARANOIA_CHECK
38 #define SERIAL_DO_RESTART
39
40 /* Set of debugging defines */
41
42 #undef SERIAL_DEBUG_INTR
43 #undef SERIAL_DEBUG_OPEN
44 #undef SERIAL_DEBUG_FLOW
45 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
46
47 /* Sanity checks */
48
49 #define SERIAL_INLINE
50
51 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
52 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
53 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
54 #else
55 #define DBG_CNT(s)
56 #endif
57
58 /*
59 * End of serial driver configuration section.
60 */
61
62 #ifdef MODVERSIONS
63 #include <linux/modversions.h>
64 #endif
65 #include <linux/module.h>
66
67 #include <linux/types.h>
68 #include <linux/serial.h>
69 #include <linux/serialP.h>
70 #include <linux/serial_reg.h>
71 static char *serial_version = "4.30";
72
73 #include <linux/errno.h>
74 #include <linux/signal.h>
75 #include <linux/sched.h>
76 #include <linux/kernel.h>
77 #include <linux/timer.h>
78 #include <linux/interrupt.h>
79 #include <linux/tty.h>
80 #include <linux/tty_flip.h>
81 #include <linux/console.h>
82 #include <linux/major.h>
83 #include <linux/string.h>
84 #include <linux/fcntl.h>
85 #include <linux/ptrace.h>
86 #include <linux/ioport.h>
87 #include <linux/mm.h>
88 #include <linux/malloc.h>
89 #include <linux/init.h>
90 #include <linux/delay.h>
91
92 #include <asm/setup.h>
93
94 #include <asm/system.h>
95 #include <asm/io.h>
96 #include <asm/irq.h>
97 #include <asm/bitops.h>
98
99 #include <asm/amigahw.h>
100 #include <asm/amigaints.h>
101
102 #ifdef SERIAL_INLINE
103 #define _INLINE_ inline
104 #endif
105
106 static char *serial_name = "Amiga-builtin serial driver";
107
108 static DECLARE_TASK_QUEUE(tq_serial);
109
110 static struct tty_driver serial_driver, callout_driver;
111 static int serial_refcount;
112
113 /* serial subtype definitions */
114 #ifndef SERIAL_TYPE_NORMAL
115 #define SERIAL_TYPE_NORMAL 1
116 #define SERIAL_TYPE_CALLOUT 2
117 #endif
118
119 /* number of characters left in xmit buffer before we ask for more */
120 #define WAKEUP_CHARS 256
121
122 static struct async_struct *IRQ_ports;
123
124 static unsigned char current_ctl_bits;
125
126 static void change_speed(struct async_struct *info, struct termios *old);
127 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
128
129
130 static struct serial_state rs_table[1];
131
132 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
133
134
135 static struct tty_struct *serial_table[NR_PORTS];
136 static struct termios *serial_termios[NR_PORTS];
137 static struct termios *serial_termios_locked[NR_PORTS];
138
139 #ifndef MIN
140 #define MIN(a,b) ((a) < (b) ? (a) : (b))
141 #endif
142
143 /*
144 * tmp_buf is used as a temporary buffer by serial_write. We need to
145 * lock it in case the copy_from_user blocks while swapping in a page,
146 * and some other program tries to do a serial write at the same time.
147 * Since the lock will only come under contention when the system is
148 * swapping and available memory is low, it makes sense to share one
149 * buffer across all the serial ports, since it significantly saves
150 * memory if large numbers of serial ports are open.
151 */
152 static unsigned char *tmp_buf;
153 static DECLARE_MUTEX(tmp_buf_sem);
154
155 #include <asm/uaccess.h>
156
157 #define serial_isroot() (capable(CAP_SYS_ADMIN))
158
159
160 static inline int serial_paranoia_check(struct async_struct *info,
161 kdev_t device, const char *routine)
162 {
163 #ifdef SERIAL_PARANOIA_CHECK
164 static const char *badmagic =
165 "Warning: bad magic number for serial struct (%s) in %s\n";
166 static const char *badinfo =
167 "Warning: null async_struct for (%s) in %s\n";
168
169 if (!info) {
170 printk(badinfo, kdevname(device), routine);
171 return 1;
172 }
173 if (info->magic != SERIAL_MAGIC) {
174 printk(badmagic, kdevname(device), routine);
175 return 1;
176 }
177 #endif
178 return 0;
179 }
180
181 /* some serial hardware definitions */
182 #define SDR_OVRUN (1<<15)
183 #define SDR_RBF (1<<14)
184 #define SDR_TBE (1<<13)
185 #define SDR_TSRE (1<<12)
186
187 #define SERPER_PARENB (1<<15)
188
189 #define AC_SETCLR (1<<15)
190 #define AC_UARTBRK (1<<11)
191
192 #define SER_DTR (1<<7)
193 #define SER_RTS (1<<6)
194 #define SER_DCD (1<<5)
195 #define SER_CTS (1<<4)
196 #define SER_DSR (1<<3)
197
198 static __inline__ void rtsdtr_ctrl(int bits)
199 {
200 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
201 }
202
203 /*
204 * ------------------------------------------------------------
205 * rs_stop() and rs_start()
206 *
207 * This routines are called before setting or resetting tty->stopped.
208 * They enable or disable transmitter interrupts, as necessary.
209 * ------------------------------------------------------------
210 */
211 static void rs_stop(struct tty_struct *tty)
212 {
213 struct async_struct *info = (struct async_struct *)tty->driver_data;
214 unsigned long flags;
215
216 if (serial_paranoia_check(info, tty->device, "rs_stop"))
217 return;
218
219 save_flags(flags); cli();
220 if (info->IER & UART_IER_THRI) {
221 info->IER &= ~UART_IER_THRI;
222 /* disable Tx interrupt and remove any pending interrupts */
223 custom.intena = IF_TBE;
224 mb();
225 custom.intreq = IF_TBE;
226 mb();
227 }
228 restore_flags(flags);
229 }
230
231 static void rs_start(struct tty_struct *tty)
232 {
233 struct async_struct *info = (struct async_struct *)tty->driver_data;
234 unsigned long flags;
235
236 if (serial_paranoia_check(info, tty->device, "rs_start"))
237 return;
238
239 save_flags(flags); cli();
240 if (info->xmit.head != info->xmit.tail
241 && info->xmit.buf
242 && !(info->IER & UART_IER_THRI)) {
243 info->IER |= UART_IER_THRI;
244 custom.intena = IF_SETCLR | IF_TBE;
245 mb();
246 /* set a pending Tx Interrupt, transmitter should restart now */
247 custom.intreq = IF_SETCLR | IF_TBE;
248 mb();
249 }
250 restore_flags(flags);
251 }
252
253 /*
254 * ----------------------------------------------------------------------
255 *
256 * Here starts the interrupt handling routines. All of the following
257 * subroutines are declared as inline and are folded into
258 * rs_interrupt(). They were separated out for readability's sake.
259 *
260 * Note: rs_interrupt() is a "fast" interrupt, which means that it
261 * runs with interrupts turned off. People who may want to modify
262 * rs_interrupt() should try to keep the interrupt handler as fast as
263 * possible. After you are done making modifications, it is not a bad
264 * idea to do:
265 *
266 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
267 *
268 * and look at the resulting assemble code in serial.s.
269 *
270 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
271 * -----------------------------------------------------------------------
272 */
273
274 /*
275 * This routine is used by the interrupt handler to schedule
276 * processing in the software interrupt portion of the driver.
277 */
278 static _INLINE_ void rs_sched_event(struct async_struct *info,
279 int event)
280 {
281 info->event |= 1 << event;
282 queue_task(&info->tqueue, &tq_serial);
283 mark_bh(SERIAL_BH);
284 }
285
286 static _INLINE_ void receive_chars(struct async_struct *info)
287 {
288 int status;
289 int serdatr;
290 struct tty_struct *tty = info->tty;
291 unsigned char ch;
292 struct async_icount *icount;
293
294 icount = &info->state->icount;
295
296 status = UART_LSR_DR; /* We obviously have a character! */
297 serdatr = custom.serdatr;
298 mb();
299 custom.intreq = IF_RBF;
300 mb();
301
302 if((serdatr & 0x1ff) == 0)
303 status |= UART_LSR_BI;
304 if(serdatr & SDR_OVRUN)
305 status |= UART_LSR_OE;
306
307 ch = serdatr & 0xff;
308 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
309 goto ignore_char;
310 *tty->flip.char_buf_ptr = ch;
311 icount->rx++;
312
313 #ifdef SERIAL_DEBUG_INTR
314 printk("DR%02x:%02x...", ch, status);
315 #endif
316 *tty->flip.flag_buf_ptr = 0;
317
318 /*
319 * We don't handle parity or frame errors - but I have left
320 * the code in, since I'm not sure that the errors can't be
321 * detected.
322 */
323
324 if (status & (UART_LSR_BI | UART_LSR_PE |
325 UART_LSR_FE | UART_LSR_OE)) {
326 /*
327 * For statistics only
328 */
329 if (status & UART_LSR_BI) {
330 status &= ~(UART_LSR_FE | UART_LSR_PE);
331 icount->brk++;
332 } else if (status & UART_LSR_PE)
333 icount->parity++;
334 else if (status & UART_LSR_FE)
335 icount->frame++;
336 if (status & UART_LSR_OE)
337 icount->overrun++;
338
339 /*
340 * Now check to see if character should be
341 * ignored, and mask off conditions which
342 * should be ignored.
343 */
344 if (status & info->ignore_status_mask)
345 goto ignore_char;
346
347 status &= info->read_status_mask;
348
349 if (status & (UART_LSR_BI)) {
350 #ifdef SERIAL_DEBUG_INTR
351 printk("handling break....");
352 #endif
353 *tty->flip.flag_buf_ptr = TTY_BREAK;
354 if (info->flags & ASYNC_SAK)
355 do_SAK(tty);
356 } else if (status & UART_LSR_PE)
357 *tty->flip.flag_buf_ptr = TTY_PARITY;
358 else if (status & UART_LSR_FE)
359 *tty->flip.flag_buf_ptr = TTY_FRAME;
360 if (status & UART_LSR_OE) {
361 /*
362 * Overrun is special, since it's
363 * reported immediately, and doesn't
364 * affect the current character
365 */
366 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
367 tty->flip.count++;
368 tty->flip.flag_buf_ptr++;
369 tty->flip.char_buf_ptr++;
370 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
371 }
372 }
373 }
374 tty->flip.flag_buf_ptr++;
375 tty->flip.char_buf_ptr++;
376 tty->flip.count++;
377 ignore_char:
378
379 tty_flip_buffer_push(tty);
380 }
381
382 static _INLINE_ void transmit_chars(struct async_struct *info)
383 {
384 custom.intreq = IF_TBE;
385 mb();
386 if (info->x_char) {
387 custom.serdat = info->x_char | 0x100;
388 mb();
389 info->state->icount.tx++;
390 info->x_char = 0;
391 return;
392 }
393 if (info->xmit.head == info->xmit.tail
394 || info->tty->stopped
395 || info->tty->hw_stopped) {
396 info->IER &= ~UART_IER_THRI;
397 custom.intena = IF_TBE;
398 mb();
399 return;
400 }
401
402 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
403 mb();
404 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
405 info->state->icount.tx++;
406
407 if (CIRC_CNT(info->xmit.head,
408 info->xmit.tail,
409 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
410 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
411
412 #ifdef SERIAL_DEBUG_INTR
413 printk("THRE...");
414 #endif
415 if (info->xmit.head == info->xmit.tail) {
416 custom.intena = IF_TBE;
417 mb();
418 info->IER &= ~UART_IER_THRI;
419 }
420 }
421
422 static _INLINE_ void check_modem_status(struct async_struct *info)
423 {
424 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
425 unsigned char dstatus;
426 struct async_icount *icount;
427
428 /* Determine bits that have changed */
429 dstatus = status ^ current_ctl_bits;
430 current_ctl_bits = status;
431
432 if (dstatus) {
433 icount = &info->state->icount;
434 /* update input line counters */
435 if (dstatus & SER_DSR)
436 icount->dsr++;
437 if (dstatus & SER_DCD) {
438 icount->dcd++;
439 #ifdef CONFIG_HARD_PPS
440 if ((info->flags & ASYNC_HARDPPS_CD) &&
441 !(status & SER_DCD))
442 hardpps();
443 #endif
444 }
445 if (dstatus & SER_CTS)
446 icount->cts++;
447 wake_up_interruptible(&info->delta_msr_wait);
448 }
449
450 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
451 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
452 printk("ttyS%02d CD now %s...", info->line,
453 (!(status & SER_DCD)) ? "on" : "off");
454 #endif
455 if (!(status & SER_DCD))
456 wake_up_interruptible(&info->open_wait);
457 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
458 (info->flags & ASYNC_CALLOUT_NOHUP))) {
459 #ifdef SERIAL_DEBUG_OPEN
460 printk("doing serial hangup...");
461 #endif
462 if (info->tty)
463 tty_hangup(info->tty);
464 }
465 }
466 if (info->flags & ASYNC_CTS_FLOW) {
467 if (info->tty->hw_stopped) {
468 if (!(status & SER_CTS)) {
469 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
470 printk("CTS tx start...");
471 #endif
472 info->tty->hw_stopped = 0;
473 info->IER |= UART_IER_THRI;
474 custom.intena = IF_SETCLR | IF_TBE;
475 mb();
476 /* set a pending Tx Interrupt, transmitter should restart now */
477 custom.intreq = IF_SETCLR | IF_TBE;
478 mb();
479 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
480 return;
481 }
482 } else {
483 if ((status & SER_CTS)) {
484 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
485 printk("CTS tx stop...");
486 #endif
487 info->tty->hw_stopped = 1;
488 info->IER &= ~UART_IER_THRI;
489 /* disable Tx interrupt and remove any pending interrupts */
490 custom.intena = IF_TBE;
491 mb();
492 custom.intreq = IF_TBE;
493 mb();
494 }
495 }
496 }
497 }
498
499 static void ser_vbl_int( int irq, void *data, struct pt_regs *regs)
500 {
501 /* vbl is just a periodic interrupt we tie into to update modem status */
502 struct async_struct * info = IRQ_ports;
503 /*
504 * TBD - is it better to unregister from this interrupt or to
505 * ignore it if MSI is clear ?
506 */
507 if(info->IER & UART_IER_MSI)
508 check_modem_status(info);
509 }
510
511 static void ser_rx_int(int irq, void *dev_id, struct pt_regs * regs)
512 {
513 struct async_struct * info;
514
515 #ifdef SERIAL_DEBUG_INTR
516 printk("ser_rx_int...");
517 #endif
518
519 info = IRQ_ports;
520 if (!info || !info->tty)
521 return;
522
523 receive_chars(info);
524 info->last_active = jiffies;
525 #ifdef SERIAL_DEBUG_INTR
526 printk("end.\n");
527 #endif
528 }
529
530 static void ser_tx_int(int irq, void *dev_id, struct pt_regs * regs)
531 {
532 struct async_struct * info;
533
534 if (custom.serdatr & SDR_TBE) {
535 #ifdef SERIAL_DEBUG_INTR
536 printk("ser_tx_int...");
537 #endif
538
539 info = IRQ_ports;
540 if (!info || !info->tty)
541 return;
542
543 transmit_chars(info);
544 info->last_active = jiffies;
545 #ifdef SERIAL_DEBUG_INTR
546 printk("end.\n");
547 #endif
548 }
549 }
550
551 /*
552 * -------------------------------------------------------------------
553 * Here ends the serial interrupt routines.
554 * -------------------------------------------------------------------
555 */
556
557 /*
558 * This routine is used to handle the "bottom half" processing for the
559 * serial driver, known also the "software interrupt" processing.
560 * This processing is done at the kernel interrupt level, after the
561 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
562 * is where time-consuming activities which can not be done in the
563 * interrupt driver proper are done; the interrupt driver schedules
564 * them using rs_sched_event(), and they get done here.
565 */
566 static void do_serial_bh(void)
567 {
568 run_task_queue(&tq_serial);
569 }
570
571 static void do_softint(void *private_)
572 {
573 struct async_struct *info = (struct async_struct *) private_;
574 struct tty_struct *tty;
575
576 tty = info->tty;
577 if (!tty)
578 return;
579
580 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
581 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
582 tty->ldisc.write_wakeup)
583 (tty->ldisc.write_wakeup)(tty);
584 wake_up_interruptible(&tty->write_wait);
585 }
586 }
587
588 /*
589 * ---------------------------------------------------------------
590 * Low level utility subroutines for the serial driver: routines to
591 * figure out the appropriate timeout for an interrupt chain, routines
592 * to initialize and startup a serial port, and routines to shutdown a
593 * serial port. Useful stuff like that.
594 * ---------------------------------------------------------------
595 */
596
597 static int startup(struct async_struct * info)
598 {
599 unsigned long flags;
600 int retval=0;
601 unsigned long page;
602
603 page = get_free_page(GFP_KERNEL);
604 if (!page)
605 return -ENOMEM;
606
607 save_flags(flags); cli();
608
609 if (info->flags & ASYNC_INITIALIZED) {
610 free_page(page);
611 goto errout;
612 }
613
614 if (info->xmit.buf)
615 free_page(page);
616 else
617 info->xmit.buf = (unsigned char *) page;
618
619 #ifdef SERIAL_DEBUG_OPEN
620 printk("starting up ttys%d ...", info->line);
621 #endif
622
623 /* Clear anything in the input buffer */
624
625 custom.intreq = IF_RBF;
626 mb();
627
628 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
629 if (retval) {
630 if (serial_isroot()) {
631 if (info->tty)
632 set_bit(TTY_IO_ERROR,
633 &info->tty->flags);
634 retval = 0;
635 }
636 goto errout;
637 }
638
639 /* enable both Rx and Tx interrupts */
640 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
641 mb();
642 info->IER = UART_IER_MSI;
643
644 /* remember current state of the DCD and CTS bits */
645 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
646
647 IRQ_ports = info;
648
649 info->MCR = 0;
650 if (info->tty->termios->c_cflag & CBAUD)
651 info->MCR = SER_DTR | SER_RTS;
652 rtsdtr_ctrl(info->MCR);
653
654 if (info->tty)
655 clear_bit(TTY_IO_ERROR, &info->tty->flags);
656 info->xmit.head = info->xmit.tail = 0;
657
658 /*
659 * Set up the tty->alt_speed kludge
660 */
661 if (info->tty) {
662 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
663 info->tty->alt_speed = 57600;
664 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
665 info->tty->alt_speed = 115200;
666 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
667 info->tty->alt_speed = 230400;
668 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
669 info->tty->alt_speed = 460800;
670 }
671
672 /*
673 * and set the speed of the serial port
674 */
675 change_speed(info, 0);
676
677 info->flags |= ASYNC_INITIALIZED;
678 restore_flags(flags);
679 return 0;
680
681 errout:
682 restore_flags(flags);
683 return retval;
684 }
685
686 /*
687 * This routine will shutdown a serial port; interrupts are disabled, and
688 * DTR is dropped if the hangup on close termio flag is on.
689 */
690 static void shutdown(struct async_struct * info)
691 {
692 unsigned long flags;
693 struct serial_state *state;
694
695 if (!(info->flags & ASYNC_INITIALIZED))
696 return;
697
698 state = info->state;
699
700 #ifdef SERIAL_DEBUG_OPEN
701 printk("Shutting down serial port %d ....\n", info->line);
702 #endif
703
704 save_flags(flags); cli(); /* Disable interrupts */
705
706 /*
707 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
708 * here so the queue might never be waken up
709 */
710 wake_up_interruptible(&info->delta_msr_wait);
711
712 IRQ_ports = NULL;
713
714 /*
715 * Free the IRQ, if necessary
716 */
717 free_irq(IRQ_AMIGA_VERTB, info);
718
719 if (info->xmit.buf) {
720 free_page((unsigned long) info->xmit.buf);
721 info->xmit.buf = 0;
722 }
723
724 info->IER = 0;
725 custom.intena = IF_RBF | IF_TBE;
726 mb();
727
728 /* disable break condition */
729 custom.adkcon = AC_UARTBRK;
730 mb();
731
732 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
733 info->MCR &= ~(SER_DTR|SER_RTS);
734 rtsdtr_ctrl(info->MCR);
735
736 if (info->tty)
737 set_bit(TTY_IO_ERROR, &info->tty->flags);
738
739 info->flags &= ~ASYNC_INITIALIZED;
740 restore_flags(flags);
741 }
742
743
744 /*
745 * This routine is called to set the UART divisor registers to match
746 * the specified baud rate for a serial port.
747 */
748 static void change_speed(struct async_struct *info,
749 struct termios *old_termios)
750 {
751 int quot = 0, baud_base, baud;
752 unsigned cflag, cval = 0;
753 int bits;
754 unsigned long flags;
755
756 if (!info->tty || !info->tty->termios)
757 return;
758 cflag = info->tty->termios->c_cflag;
759
760 /* Byte size is always 8 bits plus parity bit if requested */
761
762 cval = 3; bits = 10;
763 if (cflag & CSTOPB) {
764 cval |= 0x04;
765 bits++;
766 }
767 if (cflag & PARENB) {
768 cval |= UART_LCR_PARITY;
769 bits++;
770 }
771 if (!(cflag & PARODD))
772 cval |= UART_LCR_EPAR;
773 #ifdef CMSPAR
774 if (cflag & CMSPAR)
775 cval |= UART_LCR_SPAR;
776 #endif
777
778 /* Determine divisor based on baud rate */
779 baud = tty_get_baud_rate(info->tty);
780 if (!baud)
781 baud = 9600; /* B0 transition handled in rs_set_termios */
782 baud_base = info->state->baud_base;
783 if (baud == 38400 &&
784 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
785 quot = info->state->custom_divisor;
786 else {
787 if (baud == 134)
788 /* Special case since 134 is really 134.5 */
789 quot = (2*baud_base / 269);
790 else if (baud)
791 quot = baud_base / baud;
792 }
793 /* If the quotient is zero refuse the change */
794 if (!quot && old_termios) {
795 info->tty->termios->c_cflag &= ~CBAUD;
796 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
797 baud = tty_get_baud_rate(info->tty);
798 if (!baud)
799 baud = 9600;
800 if (baud == 38400 &&
801 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
802 quot = info->state->custom_divisor;
803 else {
804 if (baud == 134)
805 /* Special case since 134 is really 134.5 */
806 quot = (2*baud_base / 269);
807 else if (baud)
808 quot = baud_base / baud;
809 }
810 }
811 /* As a last resort, if the quotient is zero, default to 9600 bps */
812 if (!quot)
813 quot = baud_base / 9600;
814 info->quot = quot;
815 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
816 info->timeout += HZ/50; /* Add .02 seconds of slop */
817
818 /* CTS flow control flag and modem status interrupts */
819 info->IER &= ~UART_IER_MSI;
820 if (info->flags & ASYNC_HARDPPS_CD)
821 info->IER |= UART_IER_MSI;
822 if (cflag & CRTSCTS) {
823 info->flags |= ASYNC_CTS_FLOW;
824 info->IER |= UART_IER_MSI;
825 } else
826 info->flags &= ~ASYNC_CTS_FLOW;
827 if (cflag & CLOCAL)
828 info->flags &= ~ASYNC_CHECK_CD;
829 else {
830 info->flags |= ASYNC_CHECK_CD;
831 info->IER |= UART_IER_MSI;
832 }
833 /* TBD:
834 * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
835 */
836
837 /*
838 * Set up parity check flag
839 */
840 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
841
842 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
843 if (I_INPCK(info->tty))
844 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
845 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
846 info->read_status_mask |= UART_LSR_BI;
847
848 /*
849 * Characters to ignore
850 */
851 info->ignore_status_mask = 0;
852 if (I_IGNPAR(info->tty))
853 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
854 if (I_IGNBRK(info->tty)) {
855 info->ignore_status_mask |= UART_LSR_BI;
856 /*
857 * If we're ignore parity and break indicators, ignore
858 * overruns too. (For real raw support).
859 */
860 if (I_IGNPAR(info->tty))
861 info->ignore_status_mask |= UART_LSR_OE;
862 }
863 /*
864 * !!! ignore all characters if CREAD is not set
865 */
866 if ((cflag & CREAD) == 0)
867 info->ignore_status_mask |= UART_LSR_DR;
868 save_flags(flags); cli();
869
870 {
871 short serper;
872
873 /* Set up the baud rate */
874 serper = quot - 1;
875
876 /* Enable or disable parity bit */
877
878 if(cval & UART_LCR_PARITY)
879 serper |= (SERPER_PARENB);
880
881 custom.serper = serper;
882 mb();
883 }
884
885 info->LCR = cval; /* Save LCR */
886 restore_flags(flags);
887 }
888
889 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
890 {
891 struct async_struct *info = (struct async_struct *)tty->driver_data;
892 unsigned long flags;
893
894 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
895 return;
896
897 if (!tty || !info->xmit.buf)
898 return;
899
900 save_flags(flags); cli();
901 if (CIRC_SPACE(info->xmit.head,
902 info->xmit.tail,
903 SERIAL_XMIT_SIZE) == 0) {
904 restore_flags(flags);
905 return;
906 }
907
908 info->xmit.buf[info->xmit.head++] = ch;
909 info->xmit.head &= SERIAL_XMIT_SIZE-1;
910 restore_flags(flags);
911 }
912
913 static void rs_flush_chars(struct tty_struct *tty)
914 {
915 struct async_struct *info = (struct async_struct *)tty->driver_data;
916 unsigned long flags;
917
918 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
919 return;
920
921 if (info->xmit.head == info->xmit.tail
922 || tty->stopped
923 || tty->hw_stopped
924 || !info->xmit.buf)
925 return;
926
927 save_flags(flags); cli();
928 info->IER |= UART_IER_THRI;
929 custom.intena = IF_SETCLR | IF_TBE;
930 mb();
931 /* set a pending Tx Interrupt, transmitter should restart now */
932 custom.intreq = IF_SETCLR | IF_TBE;
933 mb();
934 restore_flags(flags);
935 }
936
937 static int rs_write(struct tty_struct * tty, int from_user,
938 const unsigned char *buf, int count)
939 {
940 int c, ret = 0;
941 struct async_struct *info = (struct async_struct *)tty->driver_data;
942 unsigned long flags;
943
944 if (serial_paranoia_check(info, tty->device, "rs_write"))
945 return 0;
946
947 if (!tty || !info->xmit.buf || !tmp_buf)
948 return 0;
949
950 save_flags(flags);
951 if (from_user) {
952 down(&tmp_buf_sem);
953 while (1) {
954 int c1;
955 c = CIRC_SPACE_TO_END(info->xmit.head,
956 info->xmit.tail,
957 SERIAL_XMIT_SIZE);
958 if (count < c)
959 c = count;
960
961 c -= copy_from_user(tmp_buf, buf, c);
962 if (!c) {
963 if (!ret)
964 ret = -EFAULT;
965 break;
966 }
967 cli();
968 c1 = CIRC_SPACE_TO_END(info->xmit.head,
969 info->xmit.tail,
970 SERIAL_XMIT_SIZE);
971 if (c1 < c)
972 c = c1;
973 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
974 info->xmit.head = ((info->xmit.head + c) &
975 (SERIAL_XMIT_SIZE-1));
976 restore_flags(flags);
977 buf += c;
978 count -= c;
979 ret += c;
980 }
981 up(&tmp_buf_sem);
982 } else {
983 cli();
984 while (1) {
985 c = CIRC_SPACE_TO_END(info->xmit.head,
986 info->xmit.tail,
987 SERIAL_XMIT_SIZE);
988 if (count < c)
989 c = count;
990 if (c <= 0) {
991 break;
992 }
993 memcpy(info->xmit.buf + info->xmit.head, buf, c);
994 info->xmit.head = ((info->xmit.head + c) &
995 (SERIAL_XMIT_SIZE-1));
996 buf += c;
997 count -= c;
998 ret += c;
999 }
1000 restore_flags(flags);
1001 }
1002 if (info->xmit.head != info->xmit.tail
1003 && !tty->stopped
1004 && !tty->hw_stopped
1005 && !(info->IER & UART_IER_THRI)) {
1006 info->IER |= UART_IER_THRI;
1007 cli();
1008 custom.intena = IF_SETCLR | IF_TBE;
1009 mb();
1010 /* set a pending Tx Interrupt, transmitter should restart now */
1011 custom.intreq = IF_SETCLR | IF_TBE;
1012 mb();
1013 restore_flags(flags);
1014 }
1015 return ret;
1016 }
1017
1018 static int rs_write_room(struct tty_struct *tty)
1019 {
1020 struct async_struct *info = (struct async_struct *)tty->driver_data;
1021
1022 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1023 return 0;
1024 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1025 }
1026
1027 static int rs_chars_in_buffer(struct tty_struct *tty)
1028 {
1029 struct async_struct *info = (struct async_struct *)tty->driver_data;
1030
1031 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1032 return 0;
1033 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1034 }
1035
1036 static void rs_flush_buffer(struct tty_struct *tty)
1037 {
1038 struct async_struct *info = (struct async_struct *)tty->driver_data;
1039 unsigned long flags;
1040
1041 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1042 return;
1043 save_flags(flags); cli();
1044 info->xmit.head = info->xmit.tail = 0;
1045 restore_flags(flags);
1046 wake_up_interruptible(&tty->write_wait);
1047 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1048 tty->ldisc.write_wakeup)
1049 (tty->ldisc.write_wakeup)(tty);
1050 }
1051
1052 /*
1053 * This function is used to send a high-priority XON/XOFF character to
1054 * the device
1055 */
1056 static void rs_send_xchar(struct tty_struct *tty, char ch)
1057 {
1058 struct async_struct *info = (struct async_struct *)tty->driver_data;
1059 unsigned long flags;
1060
1061 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1062 return;
1063
1064 info->x_char = ch;
1065 if (ch) {
1066 /* Make sure transmit interrupts are on */
1067
1068 /* Check this ! */
1069 save_flags(flags);
1070 cli();
1071 if(!(custom.intenar & IF_TBE)) {
1072 custom.intena = IF_SETCLR | IF_TBE;
1073 mb();
1074 /* set a pending Tx Interrupt, transmitter should restart now */
1075 custom.intreq = IF_SETCLR | IF_TBE;
1076 mb();
1077 }
1078 restore_flags(flags);
1079
1080 info->IER |= UART_IER_THRI;
1081 }
1082 }
1083
1084 /*
1085 * ------------------------------------------------------------
1086 * rs_throttle()
1087 *
1088 * This routine is called by the upper-layer tty layer to signal that
1089 * incoming characters should be throttled.
1090 * ------------------------------------------------------------
1091 */
1092 static void rs_throttle(struct tty_struct * tty)
1093 {
1094 struct async_struct *info = (struct async_struct *)tty->driver_data;
1095 unsigned long flags;
1096 #ifdef SERIAL_DEBUG_THROTTLE
1097 char buf[64];
1098
1099 printk("throttle %s: %d....\n", tty_name(tty, buf),
1100 tty->ldisc.chars_in_buffer(tty));
1101 #endif
1102
1103 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1104 return;
1105
1106 if (I_IXOFF(tty))
1107 rs_send_xchar(tty, STOP_CHAR(tty));
1108
1109 if (tty->termios->c_cflag & CRTSCTS)
1110 info->MCR &= ~SER_RTS;
1111
1112 save_flags(flags); cli();
1113 rtsdtr_ctrl(info->MCR);
1114 restore_flags(flags);
1115 }
1116
1117 static void rs_unthrottle(struct tty_struct * tty)
1118 {
1119 struct async_struct *info = (struct async_struct *)tty->driver_data;
1120 unsigned long flags;
1121 #ifdef SERIAL_DEBUG_THROTTLE
1122 char buf[64];
1123
1124 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1125 tty->ldisc.chars_in_buffer(tty));
1126 #endif
1127
1128 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1129 return;
1130
1131 if (I_IXOFF(tty)) {
1132 if (info->x_char)
1133 info->x_char = 0;
1134 else
1135 rs_send_xchar(tty, START_CHAR(tty));
1136 }
1137 if (tty->termios->c_cflag & CRTSCTS)
1138 info->MCR |= SER_RTS;
1139 save_flags(flags); cli();
1140 rtsdtr_ctrl(info->MCR);
1141 restore_flags(flags);
1142 }
1143
1144 /*
1145 * ------------------------------------------------------------
1146 * rs_ioctl() and friends
1147 * ------------------------------------------------------------
1148 */
1149
1150 static int get_serial_info(struct async_struct * info,
1151 struct serial_struct * retinfo)
1152 {
1153 struct serial_struct tmp;
1154 struct serial_state *state = info->state;
1155
1156 if (!retinfo)
1157 return -EFAULT;
1158 memset(&tmp, 0, sizeof(tmp));
1159 tmp.type = state->type;
1160 tmp.line = state->line;
1161 tmp.port = state->port;
1162 tmp.irq = state->irq;
1163 tmp.flags = state->flags;
1164 tmp.xmit_fifo_size = state->xmit_fifo_size;
1165 tmp.baud_base = state->baud_base;
1166 tmp.close_delay = state->close_delay;
1167 tmp.closing_wait = state->closing_wait;
1168 tmp.custom_divisor = state->custom_divisor;
1169 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1170 return -EFAULT;
1171 return 0;
1172 }
1173
1174 static int set_serial_info(struct async_struct * info,
1175 struct serial_struct * new_info)
1176 {
1177 struct serial_struct new_serial;
1178 struct serial_state old_state, *state;
1179 unsigned int change_irq,change_port;
1180 int retval = 0;
1181
1182 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1183 return -EFAULT;
1184 state = info->state;
1185 old_state = *state;
1186
1187 change_irq = new_serial.irq != state->irq;
1188 change_port = (new_serial.port != state->port);
1189 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
1190 return -EINVAL;
1191
1192 if (!serial_isroot()) {
1193 if ((new_serial.baud_base != state->baud_base) ||
1194 (new_serial.close_delay != state->close_delay) ||
1195 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1196 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1197 (state->flags & ~ASYNC_USR_MASK)))
1198 return -EPERM;
1199 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1200 (new_serial.flags & ASYNC_USR_MASK));
1201 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1202 (new_serial.flags & ASYNC_USR_MASK));
1203 state->custom_divisor = new_serial.custom_divisor;
1204 goto check_and_exit;
1205 }
1206
1207 if (new_serial.baud_base < 9600)
1208 return -EINVAL;
1209
1210 /*
1211 * OK, past this point, all the error checking has been done.
1212 * At this point, we start making changes.....
1213 */
1214
1215 state->baud_base = new_serial.baud_base;
1216 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1217 (new_serial.flags & ASYNC_FLAGS));
1218 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1219 (info->flags & ASYNC_INTERNAL_FLAGS));
1220 state->custom_divisor = new_serial.custom_divisor;
1221 state->close_delay = new_serial.close_delay * HZ/100;
1222 state->closing_wait = new_serial.closing_wait * HZ/100;
1223 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1224
1225 check_and_exit:
1226 if (info->flags & ASYNC_INITIALIZED) {
1227 if (((old_state.flags & ASYNC_SPD_MASK) !=
1228 (state->flags & ASYNC_SPD_MASK)) ||
1229 (old_state.custom_divisor != state->custom_divisor)) {
1230 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1231 info->tty->alt_speed = 57600;
1232 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1233 info->tty->alt_speed = 115200;
1234 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1235 info->tty->alt_speed = 230400;
1236 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1237 info->tty->alt_speed = 460800;
1238 change_speed(info, 0);
1239 }
1240 } else
1241 retval = startup(info);
1242 return retval;
1243 }
1244
1245
1246 /*
1247 * get_lsr_info - get line status register info
1248 *
1249 * Purpose: Let user call ioctl() to get info when the UART physically
1250 * is emptied. On bus types like RS485, the transmitter must
1251 * release the bus after transmitting. This must be done when
1252 * the transmit shift register is empty, not be done when the
1253 * transmit holding register is empty. This functionality
1254 * allows an RS485 driver to be written in user space.
1255 */
1256 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1257 {
1258 unsigned char status;
1259 unsigned int result;
1260 unsigned long flags;
1261
1262 save_flags(flags); cli();
1263 status = custom.serdatr;
1264 mb();
1265 restore_flags(flags);
1266 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1267 if (copy_to_user(value, &result, sizeof(int)))
1268 return -EFAULT;
1269 return 0;
1270 }
1271
1272
1273 static int get_modem_info(struct async_struct * info, unsigned int *value)
1274 {
1275 unsigned char control, status;
1276 unsigned int result;
1277 unsigned long flags;
1278
1279 control = info->MCR;
1280 save_flags(flags); cli();
1281 status = ciab.pra;
1282 restore_flags(flags);
1283 result = ((control & SER_RTS) ? TIOCM_RTS : 0)
1284 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1285 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1286 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1287 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1288 if (copy_to_user(value, &result, sizeof(int)))
1289 return -EFAULT;
1290 return 0;
1291 }
1292
1293 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1294 unsigned int *value)
1295 {
1296 unsigned int arg;
1297 unsigned long flags;
1298
1299 if (copy_from_user(&arg, value, sizeof(int)))
1300 return -EFAULT;
1301
1302 switch (cmd) {
1303 case TIOCMBIS:
1304 if (arg & TIOCM_RTS)
1305 info->MCR |= SER_RTS;
1306 if (arg & TIOCM_DTR)
1307 info->MCR |= SER_DTR;
1308 break;
1309 case TIOCMBIC:
1310 if (arg & TIOCM_RTS)
1311 info->MCR &= ~SER_RTS;
1312 if (arg & TIOCM_DTR)
1313 info->MCR &= ~SER_DTR;
1314 break;
1315 case TIOCMSET:
1316 info->MCR = ((info->MCR & ~(SER_RTS | SER_DTR))
1317 | ((arg & TIOCM_RTS) ? SER_RTS : 0)
1318 | ((arg & TIOCM_DTR) ? SER_DTR : 0));
1319 break;
1320 default:
1321 return -EINVAL;
1322 }
1323 save_flags(flags); cli();
1324 rtsdtr_ctrl(info->MCR);
1325 restore_flags(flags);
1326 return 0;
1327 }
1328
1329 /*
1330 * rs_break() --- routine which turns the break handling on or off
1331 */
1332 static void rs_break(struct tty_struct *tty, int break_state)
1333 {
1334 struct async_struct * info = (struct async_struct *)tty->driver_data;
1335 unsigned long flags;
1336
1337 if (serial_paranoia_check(info, tty->device, "rs_break"))
1338 return;
1339
1340 save_flags(flags); cli();
1341 if (break_state == -1)
1342 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1343 else
1344 custom.adkcon = AC_UARTBRK;
1345 mb();
1346 restore_flags(flags);
1347 }
1348
1349
1350 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1351 unsigned int cmd, unsigned long arg)
1352 {
1353 struct async_struct * info = (struct async_struct *)tty->driver_data;
1354 struct async_icount cprev, cnow; /* kernel counter temps */
1355 struct serial_icounter_struct icount;
1356 unsigned long flags;
1357
1358 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1359 return -ENODEV;
1360
1361 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1362 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1363 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1364 if (tty->flags & (1 << TTY_IO_ERROR))
1365 return -EIO;
1366 }
1367
1368 switch (cmd) {
1369 case TIOCMGET:
1370 return get_modem_info(info, (unsigned int *) arg);
1371 case TIOCMBIS:
1372 case TIOCMBIC:
1373 case TIOCMSET:
1374 return set_modem_info(info, cmd, (unsigned int *) arg);
1375 case TIOCGSERIAL:
1376 return get_serial_info(info,
1377 (struct serial_struct *) arg);
1378 case TIOCSSERIAL:
1379 return set_serial_info(info,
1380 (struct serial_struct *) arg);
1381 case TIOCSERCONFIG:
1382 return 0;
1383
1384 case TIOCSERGETLSR: /* Get line status register */
1385 return get_lsr_info(info, (unsigned int *) arg);
1386
1387 case TIOCSERGSTRUCT:
1388 if (copy_to_user((struct async_struct *) arg,
1389 info, sizeof(struct async_struct)))
1390 return -EFAULT;
1391 return 0;
1392
1393 /*
1394 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1395 * - mask passed in arg for lines of interest
1396 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1397 * Caller should use TIOCGICOUNT to see which one it was
1398 */
1399 case TIOCMIWAIT:
1400 save_flags(flags); cli();
1401 /* note the counters on entry */
1402 cprev = info->state->icount;
1403 restore_flags(flags);
1404 while (1) {
1405 interruptible_sleep_on(&info->delta_msr_wait);
1406 /* see if a signal did it */
1407 if (signal_pending(current))
1408 return -ERESTARTSYS;
1409 save_flags(flags); cli();
1410 cnow = info->state->icount; /* atomic copy */
1411 restore_flags(flags);
1412 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1413 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1414 return -EIO; /* no change => error */
1415 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1416 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1417 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1418 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1419 return 0;
1420 }
1421 cprev = cnow;
1422 }
1423 /* NOTREACHED */
1424
1425 /*
1426 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1427 * Return: write counters to the user passed counter struct
1428 * NB: both 1->0 and 0->1 transitions are counted except for
1429 * RI where only 0->1 is counted.
1430 */
1431 case TIOCGICOUNT:
1432 save_flags(flags); cli();
1433 cnow = info->state->icount;
1434 restore_flags(flags);
1435 icount.cts = cnow.cts;
1436 icount.dsr = cnow.dsr;
1437 icount.rng = cnow.rng;
1438 icount.dcd = cnow.dcd;
1439 icount.rx = cnow.rx;
1440 icount.tx = cnow.tx;
1441 icount.frame = cnow.frame;
1442 icount.overrun = cnow.overrun;
1443 icount.parity = cnow.parity;
1444 icount.brk = cnow.brk;
1445 icount.buf_overrun = cnow.buf_overrun;
1446
1447 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1448 return -EFAULT;
1449 return 0;
1450 case TIOCSERGWILD:
1451 case TIOCSERSWILD:
1452 /* "setserial -W" is called in Debian boot */
1453 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1454 return 0;
1455
1456 default:
1457 return -ENOIOCTLCMD;
1458 }
1459 return 0;
1460 }
1461
1462 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1463 {
1464 struct async_struct *info = (struct async_struct *)tty->driver_data;
1465 unsigned long flags;
1466 unsigned int cflag = tty->termios->c_cflag;
1467
1468 if ( (cflag == old_termios->c_cflag)
1469 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1470 == RELEVANT_IFLAG(old_termios->c_iflag)))
1471 return;
1472
1473 change_speed(info, old_termios);
1474
1475 /* Handle transition to B0 status */
1476 if ((old_termios->c_cflag & CBAUD) &&
1477 !(cflag & CBAUD)) {
1478 info->MCR &= ~(SER_DTR|SER_RTS);
1479 save_flags(flags); cli();
1480 rtsdtr_ctrl(info->MCR);
1481 restore_flags(flags);
1482 }
1483
1484 /* Handle transition away from B0 status */
1485 if (!(old_termios->c_cflag & CBAUD) &&
1486 (cflag & CBAUD)) {
1487 info->MCR |= SER_DTR;
1488 if (!(tty->termios->c_cflag & CRTSCTS) ||
1489 !test_bit(TTY_THROTTLED, &tty->flags)) {
1490 info->MCR |= SER_RTS;
1491 }
1492 save_flags(flags); cli();
1493 rtsdtr_ctrl(info->MCR);
1494 restore_flags(flags);
1495 }
1496
1497 /* Handle turning off CRTSCTS */
1498 if ((old_termios->c_cflag & CRTSCTS) &&
1499 !(tty->termios->c_cflag & CRTSCTS)) {
1500 tty->hw_stopped = 0;
1501 rs_start(tty);
1502 }
1503
1504 #if 0
1505 /*
1506 * No need to wake up processes in open wait, since they
1507 * sample the CLOCAL flag once, and don't recheck it.
1508 * XXX It's not clear whether the current behavior is correct
1509 * or not. Hence, this may change.....
1510 */
1511 if (!(old_termios->c_cflag & CLOCAL) &&
1512 (tty->termios->c_cflag & CLOCAL))
1513 wake_up_interruptible(&info->open_wait);
1514 #endif
1515 }
1516
1517 /*
1518 * ------------------------------------------------------------
1519 * rs_close()
1520 *
1521 * This routine is called when the serial port gets closed. First, we
1522 * wait for the last remaining data to be sent. Then, we unlink its
1523 * async structure from the interrupt chain if necessary, and we free
1524 * that IRQ if nothing is left in the chain.
1525 * ------------------------------------------------------------
1526 */
1527 static void rs_close(struct tty_struct *tty, struct file * filp)
1528 {
1529 struct async_struct * info = (struct async_struct *)tty->driver_data;
1530 struct serial_state *state;
1531 unsigned long flags;
1532
1533 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1534 return;
1535
1536 state = info->state;
1537
1538 save_flags(flags); cli();
1539
1540 if (tty_hung_up_p(filp)) {
1541 DBG_CNT("before DEC-hung");
1542 MOD_DEC_USE_COUNT;
1543 restore_flags(flags);
1544 return;
1545 }
1546
1547 #ifdef SERIAL_DEBUG_OPEN
1548 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1549 #endif
1550 if ((tty->count == 1) && (state->count != 1)) {
1551 /*
1552 * Uh, oh. tty->count is 1, which means that the tty
1553 * structure will be freed. state->count should always
1554 * be one in these conditions. If it's greater than
1555 * one, we've got real problems, since it means the
1556 * serial port won't be shutdown.
1557 */
1558 printk("rs_close: bad serial port count; tty->count is 1, "
1559 "state->count is %d\n", state->count);
1560 state->count = 1;
1561 }
1562 if (--state->count < 0) {
1563 printk("rs_close: bad serial port count for ttys%d: %d\n",
1564 info->line, state->count);
1565 state->count = 0;
1566 }
1567 if (state->count) {
1568 DBG_CNT("before DEC-2");
1569 MOD_DEC_USE_COUNT;
1570 restore_flags(flags);
1571 return;
1572 }
1573 info->flags |= ASYNC_CLOSING;
1574 /*
1575 * Save the termios structure, since this port may have
1576 * separate termios for callout and dialin.
1577 */
1578 if (info->flags & ASYNC_NORMAL_ACTIVE)
1579 info->state->normal_termios = *tty->termios;
1580 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1581 info->state->callout_termios = *tty->termios;
1582 /*
1583 * Now we wait for the transmit buffer to clear; and we notify
1584 * the line discipline to only process XON/XOFF characters.
1585 */
1586 tty->closing = 1;
1587 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1588 tty_wait_until_sent(tty, info->closing_wait);
1589 /*
1590 * At this point we stop accepting input. To do this, we
1591 * disable the receive line status interrupts, and tell the
1592 * interrupt driver to stop checking the data ready bit in the
1593 * line status register.
1594 */
1595 info->read_status_mask &= ~UART_LSR_DR;
1596 if (info->flags & ASYNC_INITIALIZED) {
1597 /* disable receive interrupts */
1598 custom.intena = IF_RBF;
1599 mb();
1600 /* clear any pending receive interrupt */
1601 custom.intreq = IF_RBF;
1602 mb();
1603
1604 /*
1605 * Before we drop DTR, make sure the UART transmitter
1606 * has completely drained; this is especially
1607 * important if there is a transmit FIFO!
1608 */
1609 rs_wait_until_sent(tty, info->timeout);
1610 }
1611 shutdown(info);
1612 if (tty->driver.flush_buffer)
1613 tty->driver.flush_buffer(tty);
1614 if (tty->ldisc.flush_buffer)
1615 tty->ldisc.flush_buffer(tty);
1616 tty->closing = 0;
1617 info->event = 0;
1618 info->tty = 0;
1619 if (info->blocked_open) {
1620 if (info->close_delay) {
1621 current->state = TASK_INTERRUPTIBLE;
1622 schedule_timeout(info->close_delay);
1623 }
1624 wake_up_interruptible(&info->open_wait);
1625 }
1626 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1627 ASYNC_CLOSING);
1628 wake_up_interruptible(&info->close_wait);
1629 MOD_DEC_USE_COUNT;
1630 restore_flags(flags);
1631 }
1632
1633 /*
1634 * rs_wait_until_sent() --- wait until the transmitter is empty
1635 */
1636 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1637 {
1638 struct async_struct * info = (struct async_struct *)tty->driver_data;
1639 unsigned long orig_jiffies, char_time;
1640 int lsr;
1641
1642 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1643 return;
1644
1645 if (info->xmit_fifo_size == 0)
1646 return; /* Just in case.... */
1647
1648 orig_jiffies = jiffies;
1649 /*
1650 * Set the check interval to be 1/5 of the estimated time to
1651 * send a single character, and make it at least 1. The check
1652 * interval should also be less than the timeout.
1653 *
1654 * Note: we have to use pretty tight timings here to satisfy
1655 * the NIST-PCTS.
1656 */
1657 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1658 char_time = char_time / 5;
1659 if (char_time == 0)
1660 char_time = 1;
1661 if (timeout)
1662 char_time = MIN(char_time, timeout);
1663 /*
1664 * If the transmitter hasn't cleared in twice the approximate
1665 * amount of time to send the entire FIFO, it probably won't
1666 * ever clear. This assumes the UART isn't doing flow
1667 * control, which is currently the case. Hence, if it ever
1668 * takes longer than info->timeout, this is probably due to a
1669 * UART bug of some kind. So, we clamp the timeout parameter at
1670 * 2*info->timeout.
1671 */
1672 if (!timeout || timeout > 2*info->timeout)
1673 timeout = 2*info->timeout;
1674 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1675 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1676 printk("jiff=%lu...", jiffies);
1677 #endif
1678 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1679 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1680 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1681 #endif
1682 current->state = TASK_INTERRUPTIBLE;
1683 schedule_timeout(char_time);
1684 if (signal_pending(current))
1685 break;
1686 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1687 break;
1688 }
1689 current->state = TASK_RUNNING;
1690 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1691 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1692 #endif
1693 }
1694
1695 /*
1696 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1697 */
1698 static void rs_hangup(struct tty_struct *tty)
1699 {
1700 struct async_struct * info = (struct async_struct *)tty->driver_data;
1701 struct serial_state *state = info->state;
1702
1703 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1704 return;
1705
1706 state = info->state;
1707
1708 rs_flush_buffer(tty);
1709 shutdown(info);
1710 info->event = 0;
1711 state->count = 0;
1712 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1713 info->tty = 0;
1714 wake_up_interruptible(&info->open_wait);
1715 }
1716
1717 /*
1718 * ------------------------------------------------------------
1719 * rs_open() and friends
1720 * ------------------------------------------------------------
1721 */
1722 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1723 struct async_struct *info)
1724 {
1725 #ifdef DECLARE_WAITQUEUE
1726 DECLARE_WAITQUEUE(wait, current);
1727 #else
1728 struct wait_queue wait = { current, NULL };
1729 #endif
1730 struct serial_state *state = info->state;
1731 int retval;
1732 int do_clocal = 0, extra_count = 0;
1733 unsigned long flags;
1734
1735 /*
1736 * If the device is in the middle of being closed, then block
1737 * until it's done, and then try again.
1738 */
1739 if (tty_hung_up_p(filp) ||
1740 (info->flags & ASYNC_CLOSING)) {
1741 if (info->flags & ASYNC_CLOSING)
1742 interruptible_sleep_on(&info->close_wait);
1743 #ifdef SERIAL_DO_RESTART
1744 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1745 -EAGAIN : -ERESTARTSYS);
1746 #else
1747 return -EAGAIN;
1748 #endif
1749 }
1750
1751 /*
1752 * If this is a callout device, then just make sure the normal
1753 * device isn't being used.
1754 */
1755 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1756 if (info->flags & ASYNC_NORMAL_ACTIVE)
1757 return -EBUSY;
1758 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1759 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1760 (info->session != current->session))
1761 return -EBUSY;
1762 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1763 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1764 (info->pgrp != current->pgrp))
1765 return -EBUSY;
1766 info->flags |= ASYNC_CALLOUT_ACTIVE;
1767 return 0;
1768 }
1769
1770 /*
1771 * If non-blocking mode is set, or the port is not enabled,
1772 * then make the check up front and then exit.
1773 */
1774 if ((filp->f_flags & O_NONBLOCK) ||
1775 (tty->flags & (1 << TTY_IO_ERROR))) {
1776 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1777 return -EBUSY;
1778 info->flags |= ASYNC_NORMAL_ACTIVE;
1779 return 0;
1780 }
1781
1782 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1783 if (state->normal_termios.c_cflag & CLOCAL)
1784 do_clocal = 1;
1785 } else {
1786 if (tty->termios->c_cflag & CLOCAL)
1787 do_clocal = 1;
1788 }
1789
1790 /*
1791 * Block waiting for the carrier detect and the line to become
1792 * free (i.e., not in use by the callout). While we are in
1793 * this loop, state->count is dropped by one, so that
1794 * rs_close() knows when to free things. We restore it upon
1795 * exit, either normal or abnormal.
1796 */
1797 retval = 0;
1798 add_wait_queue(&info->open_wait, &wait);
1799 #ifdef SERIAL_DEBUG_OPEN
1800 printk("block_til_ready before block: ttys%d, count = %d\n",
1801 state->line, state->count);
1802 #endif
1803 save_flags(flags); cli();
1804 if (!tty_hung_up_p(filp)) {
1805 extra_count = 1;
1806 state->count--;
1807 }
1808 restore_flags(flags);
1809 info->blocked_open++;
1810 while (1) {
1811 save_flags(flags); cli();
1812 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1813 (tty->termios->c_cflag & CBAUD))
1814 rtsdtr_ctrl(SER_DTR|SER_RTS);
1815 restore_flags(flags);
1816 set_current_state(TASK_INTERRUPTIBLE);
1817 if (tty_hung_up_p(filp) ||
1818 !(info->flags & ASYNC_INITIALIZED)) {
1819 #ifdef SERIAL_DO_RESTART
1820 if (info->flags & ASYNC_HUP_NOTIFY)
1821 retval = -EAGAIN;
1822 else
1823 retval = -ERESTARTSYS;
1824 #else
1825 retval = -EAGAIN;
1826 #endif
1827 break;
1828 }
1829 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1830 !(info->flags & ASYNC_CLOSING) &&
1831 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1832 break;
1833 if (signal_pending(current)) {
1834 retval = -ERESTARTSYS;
1835 break;
1836 }
1837 #ifdef SERIAL_DEBUG_OPEN
1838 printk("block_til_ready blocking: ttys%d, count = %d\n",
1839 info->line, state->count);
1840 #endif
1841 schedule();
1842 }
1843 current->state = TASK_RUNNING;
1844 remove_wait_queue(&info->open_wait, &wait);
1845 if (extra_count)
1846 state->count++;
1847 info->blocked_open--;
1848 #ifdef SERIAL_DEBUG_OPEN
1849 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1850 info->line, state->count);
1851 #endif
1852 if (retval)
1853 return retval;
1854 info->flags |= ASYNC_NORMAL_ACTIVE;
1855 return 0;
1856 }
1857
1858 static int get_async_struct(int line, struct async_struct **ret_info)
1859 {
1860 struct async_struct *info;
1861 struct serial_state *sstate;
1862
1863 sstate = rs_table + line;
1864 sstate->count++;
1865 if (sstate->info) {
1866 *ret_info = sstate->info;
1867 return 0;
1868 }
1869 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1870 if (!info) {
1871 sstate->count--;
1872 return -ENOMEM;
1873 }
1874 memset(info, 0, sizeof(struct async_struct));
1875 #ifdef DECLARE_WAITQUEUE
1876 init_waitqueue_head(&info->open_wait);
1877 init_waitqueue_head(&info->close_wait);
1878 init_waitqueue_head(&info->delta_msr_wait);
1879 #endif
1880 info->magic = SERIAL_MAGIC;
1881 info->port = sstate->port;
1882 info->flags = sstate->flags;
1883 info->xmit_fifo_size = sstate->xmit_fifo_size;
1884 info->line = line;
1885 info->tqueue.routine = do_softint;
1886 info->tqueue.data = info;
1887 info->state = sstate;
1888 if (sstate->info) {
1889 kfree(info);
1890 *ret_info = sstate->info;
1891 return 0;
1892 }
1893 *ret_info = sstate->info = info;
1894 return 0;
1895 }
1896
1897 /*
1898 * This routine is called whenever a serial port is opened. It
1899 * enables interrupts for a serial port, linking in its async structure into
1900 * the IRQ chain. It also performs the serial-specific
1901 * initialization for the tty structure.
1902 */
1903 static int rs_open(struct tty_struct *tty, struct file * filp)
1904 {
1905 struct async_struct *info;
1906 int retval, line;
1907 unsigned long page;
1908
1909 MOD_INC_USE_COUNT;
1910 line = MINOR(tty->device) - tty->driver.minor_start;
1911 if ((line < 0) || (line >= NR_PORTS)) {
1912 MOD_DEC_USE_COUNT;
1913 return -ENODEV;
1914 }
1915 retval = get_async_struct(line, &info);
1916 if (retval) {
1917 MOD_DEC_USE_COUNT;
1918 return retval;
1919 }
1920 tty->driver_data = info;
1921 info->tty = tty;
1922 if (serial_paranoia_check(info, tty->device, "rs_open"))
1923 return -ENODEV;
1924
1925 #ifdef SERIAL_DEBUG_OPEN
1926 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1927 info->state->count);
1928 #endif
1929 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1930
1931 if (!tmp_buf) {
1932 page = get_free_page(GFP_KERNEL);
1933 if (!page) {
1934 return -ENOMEM;
1935 }
1936 if (tmp_buf)
1937 free_page(page);
1938 else
1939 tmp_buf = (unsigned char *) page;
1940 }
1941
1942 /*
1943 * If the port is the middle of closing, bail out now
1944 */
1945 if (tty_hung_up_p(filp) ||
1946 (info->flags & ASYNC_CLOSING)) {
1947 if (info->flags & ASYNC_CLOSING)
1948 interruptible_sleep_on(&info->close_wait);
1949 #ifdef SERIAL_DO_RESTART
1950 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1951 -EAGAIN : -ERESTARTSYS);
1952 #else
1953 return -EAGAIN;
1954 #endif
1955 }
1956
1957 /*
1958 * Start up serial port
1959 */
1960 retval = startup(info);
1961 if (retval) {
1962 return retval;
1963 }
1964
1965 retval = block_til_ready(tty, filp, info);
1966 if (retval) {
1967 #ifdef SERIAL_DEBUG_OPEN
1968 printk("rs_open returning after block_til_ready with %d\n",
1969 retval);
1970 #endif
1971 return retval;
1972 }
1973
1974 if ((info->state->count == 1) &&
1975 (info->flags & ASYNC_SPLIT_TERMIOS)) {
1976 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1977 *tty->termios = info->state->normal_termios;
1978 else
1979 *tty->termios = info->state->callout_termios;
1980 change_speed(info, 0);
1981 }
1982 info->session = current->session;
1983 info->pgrp = current->pgrp;
1984
1985 #ifdef SERIAL_DEBUG_OPEN
1986 printk("rs_open ttys%d successful...", info->line);
1987 #endif
1988 return 0;
1989 }
1990
1991 /*
1992 * /proc fs routines....
1993 */
1994
1995 static inline int line_info(char *buf, struct serial_state *state)
1996 {
1997 struct async_struct *info = state->info, scr_info;
1998 char stat_buf[30], control, status;
1999 int ret;
2000 unsigned long flags;
2001
2002 ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
2003
2004 /*
2005 * Figure out the current RS-232 lines
2006 */
2007 if (!info) {
2008 info = &scr_info; /* This is just for serial_{in,out} */
2009
2010 info->magic = SERIAL_MAGIC;
2011 info->flags = state->flags;
2012 info->quot = 0;
2013 info->tty = 0;
2014 }
2015 save_flags(flags); cli();
2016 status = ciab.pra;
2017 control = info ? info->MCR : status;
2018 restore_flags(flags);
2019
2020 stat_buf[0] = 0;
2021 stat_buf[1] = 0;
2022 if(!(control & SER_RTS))
2023 strcat(stat_buf, "|RTS");
2024 if(!(status & SER_CTS))
2025 strcat(stat_buf, "|CTS");
2026 if(!(control & SER_DTR))
2027 strcat(stat_buf, "|DTR");
2028 if(!(status & SER_DSR))
2029 strcat(stat_buf, "|DSR");
2030 if(!(status & SER_DCD))
2031 strcat(stat_buf, "|CD");
2032
2033 if (info->quot) {
2034 ret += sprintf(buf+ret, " baud:%d",
2035 state->baud_base / info->quot);
2036 }
2037
2038 ret += sprintf(buf+ret, " tx:%d rx:%d",
2039 state->icount.tx, state->icount.rx);
2040
2041 if (state->icount.frame)
2042 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2043
2044 if (state->icount.parity)
2045 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2046
2047 if (state->icount.brk)
2048 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2049
2050 if (state->icount.overrun)
2051 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2052
2053 /*
2054 * Last thing is the RS-232 status lines
2055 */
2056 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2057 return ret;
2058 }
2059
2060 int rs_read_proc(char *page, char **start, off_t off, int count,
2061 int *eof, void *data)
2062 {
2063 int len = 0, l;
2064 off_t begin = 0;
2065
2066 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2067 l = line_info(page + len, &rs_table[0]);
2068 len += l;
2069 if (len+begin > off+count)
2070 goto done;
2071 if (len+begin < off) {
2072 begin += len;
2073 len = 0;
2074 }
2075 *eof = 1;
2076 done:
2077 if (off >= len+begin)
2078 return 0;
2079 *start = page + (off-begin);
2080 return ((count < begin+len-off) ? count : begin+len-off);
2081 }
2082
2083 /*
2084 * ---------------------------------------------------------------------
2085 * rs_init() and friends
2086 *
2087 * rs_init() is called at boot-time to initialize the serial driver.
2088 * ---------------------------------------------------------------------
2089 */
2090
2091 /*
2092 * This routine prints out the appropriate serial driver version
2093 * number, and identifies which options were configured into this
2094 * driver.
2095 */
2096 static _INLINE_ void show_serial_version(void)
2097 {
2098 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2099 }
2100
2101
2102 int register_serial(struct serial_struct *req);
2103 void unregister_serial(int line);
2104
2105
2106 /*
2107 * The serial driver boot-time initialization code!
2108 */
2109 static int __init rs_init(void)
2110 {
2111 unsigned long flags;
2112 struct serial_state * state;
2113
2114 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
2115 return -ENODEV;
2116
2117 /*
2118 * We request SERDAT and SERPER only, because the serial registers are
2119 * too spreaded over the custom register space
2120 */
2121 if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
2122 return -EBUSY;
2123
2124 init_bh(SERIAL_BH, do_serial_bh);
2125
2126 IRQ_ports = NULL;
2127
2128 show_serial_version();
2129
2130 /* Initialize the tty_driver structure */
2131
2132 memset(&serial_driver, 0, sizeof(struct tty_driver));
2133 serial_driver.magic = TTY_DRIVER_MAGIC;
2134 serial_driver.driver_name = "amiserial";
2135 serial_driver.name = "ttyS";
2136 serial_driver.major = TTY_MAJOR;
2137 serial_driver.minor_start = 64;
2138 serial_driver.num = 1;
2139 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2140 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2141 serial_driver.init_termios = tty_std_termios;
2142 serial_driver.init_termios.c_cflag =
2143 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2144 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2145 serial_driver.refcount = &serial_refcount;
2146 serial_driver.table = serial_table;
2147 serial_driver.termios = serial_termios;
2148 serial_driver.termios_locked = serial_termios_locked;
2149
2150 serial_driver.open = rs_open;
2151 serial_driver.close = rs_close;
2152 serial_driver.write = rs_write;
2153 serial_driver.put_char = rs_put_char;
2154 serial_driver.flush_chars = rs_flush_chars;
2155 serial_driver.write_room = rs_write_room;
2156 serial_driver.chars_in_buffer = rs_chars_in_buffer;
2157 serial_driver.flush_buffer = rs_flush_buffer;
2158 serial_driver.ioctl = rs_ioctl;
2159 serial_driver.throttle = rs_throttle;
2160 serial_driver.unthrottle = rs_unthrottle;
2161 serial_driver.set_termios = rs_set_termios;
2162 serial_driver.stop = rs_stop;
2163 serial_driver.start = rs_start;
2164 serial_driver.hangup = rs_hangup;
2165 serial_driver.break_ctl = rs_break;
2166 serial_driver.send_xchar = rs_send_xchar;
2167 serial_driver.wait_until_sent = rs_wait_until_sent;
2168 serial_driver.read_proc = rs_read_proc;
2169
2170 /*
2171 * The callout device is just like normal device except for
2172 * major number and the subtype code.
2173 */
2174 callout_driver = serial_driver;
2175 callout_driver.name = "cua";
2176 callout_driver.major = TTYAUX_MAJOR;
2177 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2178 callout_driver.read_proc = 0;
2179 callout_driver.proc_entry = 0;
2180
2181 if (tty_register_driver(&serial_driver))
2182 panic("Couldn't register serial driver\n");
2183 if (tty_register_driver(&callout_driver))
2184 panic("Couldn't register callout driver\n");
2185
2186 state = rs_table;
2187 state->magic = SSTATE_MAGIC;
2188 state->port = (int)&custom.serdatr; /* Just to give it a value */
2189 state->line = 0;
2190 state->custom_divisor = 0;
2191 state->close_delay = 5*HZ/10;
2192 state->closing_wait = 30*HZ;
2193 state->callout_termios = callout_driver.init_termios;
2194 state->normal_termios = serial_driver.init_termios;
2195 state->icount.cts = state->icount.dsr =
2196 state->icount.rng = state->icount.dcd = 0;
2197 state->icount.rx = state->icount.tx = 0;
2198 state->icount.frame = state->icount.parity = 0;
2199 state->icount.overrun = state->icount.brk = 0;
2200 /*
2201 if(state->port && check_region(state->port,REGION_LENGTH(state)))
2202 continue;
2203 */
2204
2205 printk(KERN_INFO "ttyS%02d is the amiga builtin serial port\n",
2206 state->line);
2207
2208 /* Hardware set up */
2209
2210 state->baud_base = amiga_colorclock;
2211 state->xmit_fifo_size = 1;
2212
2213 save_flags (flags);
2214 cli();
2215
2216 /* set ISRs, and then disable the rx interrupts */
2217 request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2218 request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
2219
2220 /* turn off Rx and Tx interrupts */
2221 custom.intena = IF_RBF | IF_TBE;
2222 mb();
2223
2224 /* clear any pending interrupt */
2225 custom.intreq = IF_RBF | IF_TBE;
2226 mb();
2227
2228 restore_flags (flags);
2229
2230 /*
2231 * set the appropriate directions for the modem control flags,
2232 * and clear RTS and DTR
2233 */
2234 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2235 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2236
2237 return 0;
2238 }
2239
2240 static __exit void rs_exit(void)
2241 {
2242 unsigned long flags;
2243 int e1, e2;
2244 struct async_struct *info;
2245
2246 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2247 save_flags(flags);
2248 cli();
2249 remove_bh(SERIAL_BH);
2250 if ((e1 = tty_unregister_driver(&serial_driver)))
2251 printk("SERIAL: failed to unregister serial driver (%d)\n",
2252 e1);
2253 if ((e2 = tty_unregister_driver(&callout_driver)))
2254 printk("SERIAL: failed to unregister callout driver (%d)\n",
2255 e2);
2256 restore_flags(flags);
2257
2258 info = rs_table[0].info;
2259 if (info) {
2260 rs_table[0].info = NULL;
2261 kfree(info);
2262 }
2263
2264 if (tmp_buf) {
2265 free_page((unsigned long) tmp_buf);
2266 tmp_buf = NULL;
2267 }
2268
2269 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2270 }
2271
2272 module_init(rs_init)
2273 module_exit(rs_exit)
2274
2275
2276 /*
2277 * ------------------------------------------------------------
2278 * Serial console driver
2279 * ------------------------------------------------------------
2280 */
2281 #ifdef CONFIG_SERIAL_CONSOLE
2282
2283 static void amiga_serial_putc(char c)
2284 {
2285 custom.serdat = (unsigned char)c | 0x100;
2286 while (!(custom.serdatr & 0x2000))
2287 barrier();
2288 }
2289
2290 /*
2291 * Print a string to the serial port trying not to disturb
2292 * any possible real use of the port...
2293 *
2294 * The console_lock must be held when we get here.
2295 */
2296 static void serial_console_write(struct console *co, const char *s,
2297 unsigned count)
2298 {
2299 unsigned short intena = custom.intenar;
2300
2301 custom.intena = IF_TBE;
2302
2303 while (count--) {
2304 if (*s == '\n')
2305 amiga_serial_putc('\r');
2306 amiga_serial_putc(*s++);
2307 }
2308
2309 custom.intena = IF_SETCLR | (intena & IF_TBE);
2310 }
2311
2312 /*
2313 * Receive character from the serial port
2314 */
2315 static int serial_console_wait_key(struct console *co)
2316 {
2317 unsigned short intena = custom.intenar;
2318 int ch;
2319
2320 custom.intena = IF_RBF;
2321
2322 while (!(custom.intreqr & IF_RBF))
2323 barrier();
2324 ch = custom.serdatr & 0xff;
2325 custom.intreq = IF_RBF;
2326
2327 custom.intena = IF_SETCLR | (intena & IF_RBF);
2328
2329 return ch;
2330 }
2331
2332 static kdev_t serial_console_device(struct console *c)
2333 {
2334 return MKDEV(TTY_MAJOR, 64);
2335 }
2336
2337 static struct console sercons = {
2338 "ttyS",
2339 serial_console_write,
2340 NULL,
2341 serial_console_device,
2342 serial_console_wait_key,
2343 NULL,
2344 NULL,
2345 CON_PRINTBUFFER,
2346 -1,
2347 0,
2348 NULL
2349 };
2350
2351 /*
2352 * Register console.
2353 */
2354 void __init serial_console_init(void)
2355 {
2356 register_console(&sercons);
2357 }
2358 #endif
2359
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.