blob: fd3ebd1be570f98062ae3761bf6a51d3b1145427 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/delay.h>
35
36#undef SERIAL_PARANOIA_CHECK
37#define SERIAL_DO_RESTART
38
39/* Set of debugging defines */
40
41#undef SERIAL_DEBUG_INTR
42#undef SERIAL_DEBUG_OPEN
43#undef SERIAL_DEBUG_FLOW
44#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
45
46/* Sanity checks */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
49#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
50 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
51#else
52#define DBG_CNT(s)
53#endif
54
55/*
56 * End of serial driver configuration section.
57 */
58
59#include <linux/module.h>
60
61#include <linux/types.h>
62#include <linux/serial.h>
63#include <linux/serialP.h>
64#include <linux/serial_reg.h>
65static char *serial_version = "4.30";
66
67#include <linux/errno.h>
68#include <linux/signal.h>
69#include <linux/sched.h>
70#include <linux/kernel.h>
71#include <linux/timer.h>
72#include <linux/interrupt.h>
73#include <linux/tty.h>
74#include <linux/tty_flip.h>
75#include <linux/console.h>
76#include <linux/major.h>
77#include <linux/string.h>
78#include <linux/fcntl.h>
79#include <linux/ptrace.h>
80#include <linux/ioport.h>
81#include <linux/mm.h>
Alexey Dobriyand5940272009-03-31 15:19:23 -070082#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/slab.h>
84#include <linux/init.h>
85#include <linux/bitops.h>
86
87#include <asm/setup.h>
88
89#include <asm/system.h>
90
91#include <asm/irq.h>
92
93#include <asm/amigahw.h>
94#include <asm/amigaints.h>
95
Al Virob4290a22006-01-12 01:06:12 -080096#define custom amiga_custom
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static char *serial_name = "Amiga-builtin serial driver";
98
99static struct tty_driver *serial_driver;
100
101/* number of characters left in xmit buffer before we ask for more */
102#define WAKEUP_CHARS 256
103
104static struct async_struct *IRQ_ports;
105
106static unsigned char current_ctl_bits;
107
Alan Cox606d0992006-12-08 02:38:45 -0800108static void change_speed(struct async_struct *info, struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
110
111
112static struct serial_state rs_table[1];
113
Tobias Klauserfe971072006-01-09 20:54:02 -0800114#define NR_PORTS ARRAY_SIZE(rs_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <asm/uaccess.h>
117
118#define serial_isroot() (capable(CAP_SYS_ADMIN))
119
120
121static inline int serial_paranoia_check(struct async_struct *info,
122 char *name, const char *routine)
123{
124#ifdef SERIAL_PARANOIA_CHECK
125 static const char *badmagic =
126 "Warning: bad magic number for serial struct (%s) in %s\n";
127 static const char *badinfo =
128 "Warning: null async_struct for (%s) in %s\n";
129
130 if (!info) {
131 printk(badinfo, name, routine);
132 return 1;
133 }
134 if (info->magic != SERIAL_MAGIC) {
135 printk(badmagic, name, routine);
136 return 1;
137 }
138#endif
139 return 0;
140}
141
142/* some serial hardware definitions */
143#define SDR_OVRUN (1<<15)
144#define SDR_RBF (1<<14)
145#define SDR_TBE (1<<13)
146#define SDR_TSRE (1<<12)
147
148#define SERPER_PARENB (1<<15)
149
150#define AC_SETCLR (1<<15)
151#define AC_UARTBRK (1<<11)
152
153#define SER_DTR (1<<7)
154#define SER_RTS (1<<6)
155#define SER_DCD (1<<5)
156#define SER_CTS (1<<4)
157#define SER_DSR (1<<3)
158
159static __inline__ void rtsdtr_ctrl(int bits)
160{
161 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
162}
163
164/*
165 * ------------------------------------------------------------
166 * rs_stop() and rs_start()
167 *
168 * This routines are called before setting or resetting tty->stopped.
169 * They enable or disable transmitter interrupts, as necessary.
170 * ------------------------------------------------------------
171 */
172static void rs_stop(struct tty_struct *tty)
173{
Alan Coxc9f19e92009-01-02 13:47:26 +0000174 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned long flags;
176
177 if (serial_paranoia_check(info, tty->name, "rs_stop"))
178 return;
179
180 local_irq_save(flags);
181 if (info->IER & UART_IER_THRI) {
182 info->IER &= ~UART_IER_THRI;
183 /* disable Tx interrupt and remove any pending interrupts */
184 custom.intena = IF_TBE;
185 mb();
186 custom.intreq = IF_TBE;
187 mb();
188 }
189 local_irq_restore(flags);
190}
191
192static void rs_start(struct tty_struct *tty)
193{
Alan Coxc9f19e92009-01-02 13:47:26 +0000194 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned long flags;
196
197 if (serial_paranoia_check(info, tty->name, "rs_start"))
198 return;
199
200 local_irq_save(flags);
201 if (info->xmit.head != info->xmit.tail
202 && info->xmit.buf
203 && !(info->IER & UART_IER_THRI)) {
204 info->IER |= UART_IER_THRI;
205 custom.intena = IF_SETCLR | IF_TBE;
206 mb();
207 /* set a pending Tx Interrupt, transmitter should restart now */
208 custom.intreq = IF_SETCLR | IF_TBE;
209 mb();
210 }
211 local_irq_restore(flags);
212}
213
214/*
215 * ----------------------------------------------------------------------
216 *
217 * Here starts the interrupt handling routines. All of the following
218 * subroutines are declared as inline and are folded into
219 * rs_interrupt(). They were separated out for readability's sake.
220 *
221 * Note: rs_interrupt() is a "fast" interrupt, which means that it
222 * runs with interrupts turned off. People who may want to modify
223 * rs_interrupt() should try to keep the interrupt handler as fast as
224 * possible. After you are done making modifications, it is not a bad
225 * idea to do:
226 *
227 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
228 *
229 * and look at the resulting assemble code in serial.s.
230 *
231 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
232 * -----------------------------------------------------------------------
233 */
234
235/*
236 * This routine is used by the interrupt handler to schedule
237 * processing in the software interrupt portion of the driver.
238 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800239static void rs_sched_event(struct async_struct *info,
240 int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 info->event |= 1 << event;
243 tasklet_schedule(&info->tlet);
244}
245
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800246static void receive_chars(struct async_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int status;
249 int serdatr;
250 struct tty_struct *tty = info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800251 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct async_icount *icount;
Alan Cox33f0f882006-01-09 20:54:13 -0800253 int oe = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 icount = &info->state->icount;
256
257 status = UART_LSR_DR; /* We obviously have a character! */
258 serdatr = custom.serdatr;
259 mb();
260 custom.intreq = IF_RBF;
261 mb();
262
263 if((serdatr & 0x1ff) == 0)
264 status |= UART_LSR_BI;
265 if(serdatr & SDR_OVRUN)
266 status |= UART_LSR_OE;
267
268 ch = serdatr & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 icount->rx++;
270
271#ifdef SERIAL_DEBUG_INTR
272 printk("DR%02x:%02x...", ch, status);
273#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800274 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
277 * We don't handle parity or frame errors - but I have left
278 * the code in, since I'm not sure that the errors can't be
279 * detected.
280 */
281
282 if (status & (UART_LSR_BI | UART_LSR_PE |
283 UART_LSR_FE | UART_LSR_OE)) {
284 /*
285 * For statistics only
286 */
287 if (status & UART_LSR_BI) {
288 status &= ~(UART_LSR_FE | UART_LSR_PE);
289 icount->brk++;
290 } else if (status & UART_LSR_PE)
291 icount->parity++;
292 else if (status & UART_LSR_FE)
293 icount->frame++;
294 if (status & UART_LSR_OE)
295 icount->overrun++;
296
297 /*
298 * Now check to see if character should be
299 * ignored, and mask off conditions which
300 * should be ignored.
301 */
302 if (status & info->ignore_status_mask)
Alan Cox33f0f882006-01-09 20:54:13 -0800303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 status &= info->read_status_mask;
306
307 if (status & (UART_LSR_BI)) {
308#ifdef SERIAL_DEBUG_INTR
309 printk("handling break....");
310#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800311 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (info->flags & ASYNC_SAK)
313 do_SAK(tty);
314 } else if (status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800315 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 else if (status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800317 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (status & UART_LSR_OE) {
319 /*
320 * Overrun is special, since it's
321 * reported immediately, and doesn't
322 * affect the current character
323 */
Alan Cox33f0f882006-01-09 20:54:13 -0800324 oe = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 }
Alan Cox33f0f882006-01-09 20:54:13 -0800327 tty_insert_flip_char(tty, ch, flag);
328 if (oe == 1)
329 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 tty_flip_buffer_push(tty);
Alan Cox33f0f882006-01-09 20:54:13 -0800331out:
332 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800335static void transmit_chars(struct async_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 custom.intreq = IF_TBE;
338 mb();
339 if (info->x_char) {
340 custom.serdat = info->x_char | 0x100;
341 mb();
342 info->state->icount.tx++;
343 info->x_char = 0;
344 return;
345 }
346 if (info->xmit.head == info->xmit.tail
347 || info->tty->stopped
348 || info->tty->hw_stopped) {
349 info->IER &= ~UART_IER_THRI;
350 custom.intena = IF_TBE;
351 mb();
352 return;
353 }
354
355 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
356 mb();
357 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
358 info->state->icount.tx++;
359
360 if (CIRC_CNT(info->xmit.head,
361 info->xmit.tail,
362 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
363 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
364
365#ifdef SERIAL_DEBUG_INTR
366 printk("THRE...");
367#endif
368 if (info->xmit.head == info->xmit.tail) {
369 custom.intena = IF_TBE;
370 mb();
371 info->IER &= ~UART_IER_THRI;
372 }
373}
374
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800375static void check_modem_status(struct async_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
378 unsigned char dstatus;
379 struct async_icount *icount;
380
381 /* Determine bits that have changed */
382 dstatus = status ^ current_ctl_bits;
383 current_ctl_bits = status;
384
385 if (dstatus) {
386 icount = &info->state->icount;
387 /* update input line counters */
388 if (dstatus & SER_DSR)
389 icount->dsr++;
390 if (dstatus & SER_DCD) {
391 icount->dcd++;
392#ifdef CONFIG_HARD_PPS
393 if ((info->flags & ASYNC_HARDPPS_CD) &&
394 !(status & SER_DCD))
395 hardpps();
396#endif
397 }
398 if (dstatus & SER_CTS)
399 icount->cts++;
400 wake_up_interruptible(&info->delta_msr_wait);
401 }
402
403 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
404#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
405 printk("ttyS%d CD now %s...", info->line,
406 (!(status & SER_DCD)) ? "on" : "off");
407#endif
408 if (!(status & SER_DCD))
409 wake_up_interruptible(&info->open_wait);
410 else {
411#ifdef SERIAL_DEBUG_OPEN
412 printk("doing serial hangup...");
413#endif
414 if (info->tty)
415 tty_hangup(info->tty);
416 }
417 }
418 if (info->flags & ASYNC_CTS_FLOW) {
419 if (info->tty->hw_stopped) {
420 if (!(status & SER_CTS)) {
421#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
422 printk("CTS tx start...");
423#endif
424 info->tty->hw_stopped = 0;
425 info->IER |= UART_IER_THRI;
426 custom.intena = IF_SETCLR | IF_TBE;
427 mb();
428 /* set a pending Tx Interrupt, transmitter should restart now */
429 custom.intreq = IF_SETCLR | IF_TBE;
430 mb();
431 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
432 return;
433 }
434 } else {
435 if ((status & SER_CTS)) {
436#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
437 printk("CTS tx stop...");
438#endif
439 info->tty->hw_stopped = 1;
440 info->IER &= ~UART_IER_THRI;
441 /* disable Tx interrupt and remove any pending interrupts */
442 custom.intena = IF_TBE;
443 mb();
444 custom.intreq = IF_TBE;
445 mb();
446 }
447 }
448 }
449}
450
David Howells7d12e782006-10-05 14:55:46 +0100451static irqreturn_t ser_vbl_int( int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 /* vbl is just a periodic interrupt we tie into to update modem status */
454 struct async_struct * info = IRQ_ports;
455 /*
456 * TBD - is it better to unregister from this interrupt or to
457 * ignore it if MSI is clear ?
458 */
459 if(info->IER & UART_IER_MSI)
460 check_modem_status(info);
461 return IRQ_HANDLED;
462}
463
David Howells7d12e782006-10-05 14:55:46 +0100464static irqreturn_t ser_rx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct async_struct * info;
467
468#ifdef SERIAL_DEBUG_INTR
469 printk("ser_rx_int...");
470#endif
471
472 info = IRQ_ports;
473 if (!info || !info->tty)
474 return IRQ_NONE;
475
476 receive_chars(info);
477 info->last_active = jiffies;
478#ifdef SERIAL_DEBUG_INTR
479 printk("end.\n");
480#endif
481 return IRQ_HANDLED;
482}
483
David Howells7d12e782006-10-05 14:55:46 +0100484static irqreturn_t ser_tx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct async_struct * info;
487
488 if (custom.serdatr & SDR_TBE) {
489#ifdef SERIAL_DEBUG_INTR
490 printk("ser_tx_int...");
491#endif
492
493 info = IRQ_ports;
494 if (!info || !info->tty)
495 return IRQ_NONE;
496
497 transmit_chars(info);
498 info->last_active = jiffies;
499#ifdef SERIAL_DEBUG_INTR
500 printk("end.\n");
501#endif
502 }
503 return IRQ_HANDLED;
504}
505
506/*
507 * -------------------------------------------------------------------
508 * Here ends the serial interrupt routines.
509 * -------------------------------------------------------------------
510 */
511
512/*
513 * This routine is used to handle the "bottom half" processing for the
514 * serial driver, known also the "software interrupt" processing.
515 * This processing is done at the kernel interrupt level, after the
516 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
517 * is where time-consuming activities which can not be done in the
518 * interrupt driver proper are done; the interrupt driver schedules
519 * them using rs_sched_event(), and they get done here.
520 */
521
522static void do_softint(unsigned long private_)
523{
524 struct async_struct *info = (struct async_struct *) private_;
525 struct tty_struct *tty;
526
527 tty = info->tty;
528 if (!tty)
529 return;
530
Jiri Slabyb963a842007-02-10 01:44:55 -0800531 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
536 * ---------------------------------------------------------------
537 * Low level utility subroutines for the serial driver: routines to
538 * figure out the appropriate timeout for an interrupt chain, routines
539 * to initialize and startup a serial port, and routines to shutdown a
540 * serial port. Useful stuff like that.
541 * ---------------------------------------------------------------
542 */
543
544static int startup(struct async_struct * info)
545{
546 unsigned long flags;
547 int retval=0;
548 unsigned long page;
549
550 page = get_zeroed_page(GFP_KERNEL);
551 if (!page)
552 return -ENOMEM;
553
554 local_irq_save(flags);
555
556 if (info->flags & ASYNC_INITIALIZED) {
557 free_page(page);
558 goto errout;
559 }
560
561 if (info->xmit.buf)
562 free_page(page);
563 else
564 info->xmit.buf = (unsigned char *) page;
565
566#ifdef SERIAL_DEBUG_OPEN
567 printk("starting up ttys%d ...", info->line);
568#endif
569
570 /* Clear anything in the input buffer */
571
572 custom.intreq = IF_RBF;
573 mb();
574
575 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
576 if (retval) {
577 if (serial_isroot()) {
578 if (info->tty)
579 set_bit(TTY_IO_ERROR,
580 &info->tty->flags);
581 retval = 0;
582 }
583 goto errout;
584 }
585
586 /* enable both Rx and Tx interrupts */
587 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
588 mb();
589 info->IER = UART_IER_MSI;
590
591 /* remember current state of the DCD and CTS bits */
592 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
593
594 IRQ_ports = info;
595
596 info->MCR = 0;
597 if (info->tty->termios->c_cflag & CBAUD)
598 info->MCR = SER_DTR | SER_RTS;
599 rtsdtr_ctrl(info->MCR);
600
601 if (info->tty)
602 clear_bit(TTY_IO_ERROR, &info->tty->flags);
603 info->xmit.head = info->xmit.tail = 0;
604
605 /*
606 * Set up the tty->alt_speed kludge
607 */
608 if (info->tty) {
609 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
610 info->tty->alt_speed = 57600;
611 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
612 info->tty->alt_speed = 115200;
613 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
614 info->tty->alt_speed = 230400;
615 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
616 info->tty->alt_speed = 460800;
617 }
618
619 /*
620 * and set the speed of the serial port
621 */
622 change_speed(info, NULL);
623
624 info->flags |= ASYNC_INITIALIZED;
625 local_irq_restore(flags);
626 return 0;
627
628errout:
629 local_irq_restore(flags);
630 return retval;
631}
632
633/*
634 * This routine will shutdown a serial port; interrupts are disabled, and
635 * DTR is dropped if the hangup on close termio flag is on.
636 */
637static void shutdown(struct async_struct * info)
638{
639 unsigned long flags;
640 struct serial_state *state;
641
642 if (!(info->flags & ASYNC_INITIALIZED))
643 return;
644
645 state = info->state;
646
647#ifdef SERIAL_DEBUG_OPEN
648 printk("Shutting down serial port %d ....\n", info->line);
649#endif
650
651 local_irq_save(flags); /* Disable interrupts */
652
653 /*
654 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
655 * here so the queue might never be waken up
656 */
657 wake_up_interruptible(&info->delta_msr_wait);
658
659 IRQ_ports = NULL;
660
661 /*
662 * Free the IRQ, if necessary
663 */
664 free_irq(IRQ_AMIGA_VERTB, info);
665
666 if (info->xmit.buf) {
667 free_page((unsigned long) info->xmit.buf);
668 info->xmit.buf = NULL;
669 }
670
671 info->IER = 0;
672 custom.intena = IF_RBF | IF_TBE;
673 mb();
674
675 /* disable break condition */
676 custom.adkcon = AC_UARTBRK;
677 mb();
678
679 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
680 info->MCR &= ~(SER_DTR|SER_RTS);
681 rtsdtr_ctrl(info->MCR);
682
683 if (info->tty)
684 set_bit(TTY_IO_ERROR, &info->tty->flags);
685
686 info->flags &= ~ASYNC_INITIALIZED;
687 local_irq_restore(flags);
688}
689
690
691/*
692 * This routine is called to set the UART divisor registers to match
693 * the specified baud rate for a serial port.
694 */
695static void change_speed(struct async_struct *info,
Alan Cox606d0992006-12-08 02:38:45 -0800696 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 int quot = 0, baud_base, baud;
699 unsigned cflag, cval = 0;
700 int bits;
701 unsigned long flags;
702
703 if (!info->tty || !info->tty->termios)
704 return;
705 cflag = info->tty->termios->c_cflag;
706
707 /* Byte size is always 8 bits plus parity bit if requested */
708
709 cval = 3; bits = 10;
710 if (cflag & CSTOPB) {
711 cval |= 0x04;
712 bits++;
713 }
714 if (cflag & PARENB) {
715 cval |= UART_LCR_PARITY;
716 bits++;
717 }
718 if (!(cflag & PARODD))
719 cval |= UART_LCR_EPAR;
720#ifdef CMSPAR
721 if (cflag & CMSPAR)
722 cval |= UART_LCR_SPAR;
723#endif
724
725 /* Determine divisor based on baud rate */
726 baud = tty_get_baud_rate(info->tty);
727 if (!baud)
728 baud = 9600; /* B0 transition handled in rs_set_termios */
729 baud_base = info->state->baud_base;
730 if (baud == 38400 &&
731 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
732 quot = info->state->custom_divisor;
733 else {
734 if (baud == 134)
735 /* Special case since 134 is really 134.5 */
736 quot = (2*baud_base / 269);
737 else if (baud)
738 quot = baud_base / baud;
739 }
740 /* If the quotient is zero refuse the change */
741 if (!quot && old_termios) {
Alan Coxdb0ef082007-07-15 23:41:47 -0700742 /* FIXME: Will need updating for new tty in the end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 info->tty->termios->c_cflag &= ~CBAUD;
744 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
745 baud = tty_get_baud_rate(info->tty);
746 if (!baud)
747 baud = 9600;
748 if (baud == 38400 &&
749 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
750 quot = info->state->custom_divisor;
751 else {
752 if (baud == 134)
753 /* Special case since 134 is really 134.5 */
754 quot = (2*baud_base / 269);
755 else if (baud)
756 quot = baud_base / baud;
757 }
758 }
759 /* As a last resort, if the quotient is zero, default to 9600 bps */
760 if (!quot)
761 quot = baud_base / 9600;
762 info->quot = quot;
763 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
764 info->timeout += HZ/50; /* Add .02 seconds of slop */
765
766 /* CTS flow control flag and modem status interrupts */
767 info->IER &= ~UART_IER_MSI;
768 if (info->flags & ASYNC_HARDPPS_CD)
769 info->IER |= UART_IER_MSI;
770 if (cflag & CRTSCTS) {
771 info->flags |= ASYNC_CTS_FLOW;
772 info->IER |= UART_IER_MSI;
773 } else
774 info->flags &= ~ASYNC_CTS_FLOW;
775 if (cflag & CLOCAL)
776 info->flags &= ~ASYNC_CHECK_CD;
777 else {
778 info->flags |= ASYNC_CHECK_CD;
779 info->IER |= UART_IER_MSI;
780 }
781 /* TBD:
782 * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
783 */
784
785 /*
786 * Set up parity check flag
787 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
790 if (I_INPCK(info->tty))
791 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
792 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
793 info->read_status_mask |= UART_LSR_BI;
794
795 /*
796 * Characters to ignore
797 */
798 info->ignore_status_mask = 0;
799 if (I_IGNPAR(info->tty))
800 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
801 if (I_IGNBRK(info->tty)) {
802 info->ignore_status_mask |= UART_LSR_BI;
803 /*
804 * If we're ignore parity and break indicators, ignore
805 * overruns too. (For real raw support).
806 */
807 if (I_IGNPAR(info->tty))
808 info->ignore_status_mask |= UART_LSR_OE;
809 }
810 /*
811 * !!! ignore all characters if CREAD is not set
812 */
813 if ((cflag & CREAD) == 0)
814 info->ignore_status_mask |= UART_LSR_DR;
815 local_irq_save(flags);
816
817 {
818 short serper;
819
820 /* Set up the baud rate */
821 serper = quot - 1;
822
823 /* Enable or disable parity bit */
824
825 if(cval & UART_LCR_PARITY)
826 serper |= (SERPER_PARENB);
827
828 custom.serper = serper;
829 mb();
830 }
831
832 info->LCR = cval; /* Save LCR */
833 local_irq_restore(flags);
834}
835
Alan Cox257afa32008-04-30 00:54:02 -0700836static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700838 struct async_struct *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 unsigned long flags;
840
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700841 info = tty->driver_data;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
Alan Cox257afa32008-04-30 00:54:02 -0700844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700846 if (!info->xmit.buf)
Alan Cox257afa32008-04-30 00:54:02 -0700847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 local_irq_save(flags);
850 if (CIRC_SPACE(info->xmit.head,
851 info->xmit.tail,
852 SERIAL_XMIT_SIZE) == 0) {
853 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 info->xmit.buf[info->xmit.head++] = ch;
858 info->xmit.head &= SERIAL_XMIT_SIZE-1;
859 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700860 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863static void rs_flush_chars(struct tty_struct *tty)
864{
Alan Coxc9f19e92009-01-02 13:47:26 +0000865 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 unsigned long flags;
867
868 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
869 return;
870
871 if (info->xmit.head == info->xmit.tail
872 || tty->stopped
873 || tty->hw_stopped
874 || !info->xmit.buf)
875 return;
876
877 local_irq_save(flags);
878 info->IER |= UART_IER_THRI;
879 custom.intena = IF_SETCLR | IF_TBE;
880 mb();
881 /* set a pending Tx Interrupt, transmitter should restart now */
882 custom.intreq = IF_SETCLR | IF_TBE;
883 mb();
884 local_irq_restore(flags);
885}
886
887static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
888{
889 int c, ret = 0;
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700890 struct async_struct *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 unsigned long flags;
892
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700893 info = tty->driver_data;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (serial_paranoia_check(info, tty->name, "rs_write"))
896 return 0;
897
Jiri Slabyb3218a72006-10-04 02:15:27 -0700898 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
900
Jiri Kosina3abf3be2007-02-10 01:46:48 -0800901 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 while (1) {
903 c = CIRC_SPACE_TO_END(info->xmit.head,
904 info->xmit.tail,
905 SERIAL_XMIT_SIZE);
906 if (count < c)
907 c = count;
908 if (c <= 0) {
909 break;
910 }
911 memcpy(info->xmit.buf + info->xmit.head, buf, c);
912 info->xmit.head = ((info->xmit.head + c) &
913 (SERIAL_XMIT_SIZE-1));
914 buf += c;
915 count -= c;
916 ret += c;
917 }
918 local_irq_restore(flags);
919
920 if (info->xmit.head != info->xmit.tail
921 && !tty->stopped
922 && !tty->hw_stopped
923 && !(info->IER & UART_IER_THRI)) {
924 info->IER |= UART_IER_THRI;
925 local_irq_disable();
926 custom.intena = IF_SETCLR | IF_TBE;
927 mb();
928 /* set a pending Tx Interrupt, transmitter should restart now */
929 custom.intreq = IF_SETCLR | IF_TBE;
930 mb();
931 local_irq_restore(flags);
932 }
933 return ret;
934}
935
936static int rs_write_room(struct tty_struct *tty)
937{
Alan Coxc9f19e92009-01-02 13:47:26 +0000938 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
941 return 0;
942 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
943}
944
945static int rs_chars_in_buffer(struct tty_struct *tty)
946{
Alan Coxc9f19e92009-01-02 13:47:26 +0000947 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
950 return 0;
951 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
952}
953
954static void rs_flush_buffer(struct tty_struct *tty)
955{
Alan Coxc9f19e92009-01-02 13:47:26 +0000956 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 unsigned long flags;
958
959 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
960 return;
961 local_irq_save(flags);
962 info->xmit.head = info->xmit.tail = 0;
963 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 tty_wakeup(tty);
965}
966
967/*
968 * This function is used to send a high-priority XON/XOFF character to
969 * the device
970 */
971static void rs_send_xchar(struct tty_struct *tty, char ch)
972{
Alan Coxc9f19e92009-01-02 13:47:26 +0000973 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned long flags;
975
976 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
977 return;
978
979 info->x_char = ch;
980 if (ch) {
981 /* Make sure transmit interrupts are on */
982
983 /* Check this ! */
984 local_irq_save(flags);
985 if(!(custom.intenar & IF_TBE)) {
986 custom.intena = IF_SETCLR | IF_TBE;
987 mb();
988 /* set a pending Tx Interrupt, transmitter should restart now */
989 custom.intreq = IF_SETCLR | IF_TBE;
990 mb();
991 }
992 local_irq_restore(flags);
993
994 info->IER |= UART_IER_THRI;
995 }
996}
997
998/*
999 * ------------------------------------------------------------
1000 * rs_throttle()
1001 *
1002 * This routine is called by the upper-layer tty layer to signal that
1003 * incoming characters should be throttled.
1004 * ------------------------------------------------------------
1005 */
1006static void rs_throttle(struct tty_struct * tty)
1007{
Alan Coxc9f19e92009-01-02 13:47:26 +00001008 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unsigned long flags;
1010#ifdef SERIAL_DEBUG_THROTTLE
1011 char buf[64];
1012
1013 printk("throttle %s: %d....\n", tty_name(tty, buf),
1014 tty->ldisc.chars_in_buffer(tty));
1015#endif
1016
1017 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1018 return;
1019
1020 if (I_IXOFF(tty))
1021 rs_send_xchar(tty, STOP_CHAR(tty));
1022
1023 if (tty->termios->c_cflag & CRTSCTS)
1024 info->MCR &= ~SER_RTS;
1025
1026 local_irq_save(flags);
1027 rtsdtr_ctrl(info->MCR);
1028 local_irq_restore(flags);
1029}
1030
1031static void rs_unthrottle(struct tty_struct * tty)
1032{
Alan Coxc9f19e92009-01-02 13:47:26 +00001033 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned long flags;
1035#ifdef SERIAL_DEBUG_THROTTLE
1036 char buf[64];
1037
1038 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1039 tty->ldisc.chars_in_buffer(tty));
1040#endif
1041
1042 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1043 return;
1044
1045 if (I_IXOFF(tty)) {
1046 if (info->x_char)
1047 info->x_char = 0;
1048 else
1049 rs_send_xchar(tty, START_CHAR(tty));
1050 }
1051 if (tty->termios->c_cflag & CRTSCTS)
1052 info->MCR |= SER_RTS;
1053 local_irq_save(flags);
1054 rtsdtr_ctrl(info->MCR);
1055 local_irq_restore(flags);
1056}
1057
1058/*
1059 * ------------------------------------------------------------
1060 * rs_ioctl() and friends
1061 * ------------------------------------------------------------
1062 */
1063
1064static int get_serial_info(struct async_struct * info,
Al Viroab14cae2006-01-12 01:06:30 -08001065 struct serial_struct __user * retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct serial_struct tmp;
1068 struct serial_state *state = info->state;
1069
1070 if (!retinfo)
1071 return -EFAULT;
1072 memset(&tmp, 0, sizeof(tmp));
Alan Coxe18ce492008-04-30 00:53:16 -07001073 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 tmp.type = state->type;
1075 tmp.line = state->line;
1076 tmp.port = state->port;
1077 tmp.irq = state->irq;
1078 tmp.flags = state->flags;
1079 tmp.xmit_fifo_size = state->xmit_fifo_size;
1080 tmp.baud_base = state->baud_base;
1081 tmp.close_delay = state->close_delay;
1082 tmp.closing_wait = state->closing_wait;
1083 tmp.custom_divisor = state->custom_divisor;
Alan Coxe18ce492008-04-30 00:53:16 -07001084 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1086 return -EFAULT;
1087 return 0;
1088}
1089
1090static int set_serial_info(struct async_struct * info,
Al Viroab14cae2006-01-12 01:06:30 -08001091 struct serial_struct __user * new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 struct serial_struct new_serial;
1094 struct serial_state old_state, *state;
1095 unsigned int change_irq,change_port;
1096 int retval = 0;
1097
1098 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1099 return -EFAULT;
Alan Coxe18ce492008-04-30 00:53:16 -07001100
1101 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 state = info->state;
1103 old_state = *state;
1104
1105 change_irq = new_serial.irq != state->irq;
1106 change_port = (new_serial.port != state->port);
Alan Coxe18ce492008-04-30 00:53:16 -07001107 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
1108 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (!serial_isroot()) {
1113 if ((new_serial.baud_base != state->baud_base) ||
1114 (new_serial.close_delay != state->close_delay) ||
1115 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1116 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1117 (state->flags & ~ASYNC_USR_MASK)))
1118 return -EPERM;
1119 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1120 (new_serial.flags & ASYNC_USR_MASK));
1121 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1122 (new_serial.flags & ASYNC_USR_MASK));
1123 state->custom_divisor = new_serial.custom_divisor;
1124 goto check_and_exit;
1125 }
1126
Alan Coxe18ce492008-04-30 00:53:16 -07001127 if (new_serial.baud_base < 9600) {
1128 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /*
1133 * OK, past this point, all the error checking has been done.
1134 * At this point, we start making changes.....
1135 */
1136
1137 state->baud_base = new_serial.baud_base;
1138 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1139 (new_serial.flags & ASYNC_FLAGS));
1140 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1141 (info->flags & ASYNC_INTERNAL_FLAGS));
1142 state->custom_divisor = new_serial.custom_divisor;
1143 state->close_delay = new_serial.close_delay * HZ/100;
1144 state->closing_wait = new_serial.closing_wait * HZ/100;
1145 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1146
1147check_and_exit:
1148 if (info->flags & ASYNC_INITIALIZED) {
1149 if (((old_state.flags & ASYNC_SPD_MASK) !=
1150 (state->flags & ASYNC_SPD_MASK)) ||
1151 (old_state.custom_divisor != state->custom_divisor)) {
1152 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1153 info->tty->alt_speed = 57600;
1154 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1155 info->tty->alt_speed = 115200;
1156 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1157 info->tty->alt_speed = 230400;
1158 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1159 info->tty->alt_speed = 460800;
1160 change_speed(info, NULL);
1161 }
1162 } else
1163 retval = startup(info);
Alan Coxe18ce492008-04-30 00:53:16 -07001164 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return retval;
1166}
1167
1168
1169/*
1170 * get_lsr_info - get line status register info
1171 *
1172 * Purpose: Let user call ioctl() to get info when the UART physically
1173 * is emptied. On bus types like RS485, the transmitter must
1174 * release the bus after transmitting. This must be done when
1175 * the transmit shift register is empty, not be done when the
1176 * transmit holding register is empty. This functionality
1177 * allows an RS485 driver to be written in user space.
1178 */
Al Viroab14cae2006-01-12 01:06:30 -08001179static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 unsigned char status;
1182 unsigned int result;
1183 unsigned long flags;
1184
1185 local_irq_save(flags);
1186 status = custom.serdatr;
1187 mb();
1188 local_irq_restore(flags);
1189 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1190 if (copy_to_user(value, &result, sizeof(int)))
1191 return -EFAULT;
1192 return 0;
1193}
1194
1195
1196static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1197{
Alan Coxc9f19e92009-01-02 13:47:26 +00001198 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 unsigned char control, status;
1200 unsigned long flags;
1201
1202 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1203 return -ENODEV;
1204 if (tty->flags & (1 << TTY_IO_ERROR))
1205 return -EIO;
1206
1207 control = info->MCR;
1208 local_irq_save(flags);
1209 status = ciab.pra;
1210 local_irq_restore(flags);
1211 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1212 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1213 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1214 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1215 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1216}
1217
1218static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1219 unsigned int set, unsigned int clear)
1220{
Alan Coxc9f19e92009-01-02 13:47:26 +00001221 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 unsigned long flags;
1223
1224 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1225 return -ENODEV;
1226 if (tty->flags & (1 << TTY_IO_ERROR))
1227 return -EIO;
1228
1229 local_irq_save(flags);
1230 if (set & TIOCM_RTS)
1231 info->MCR |= SER_RTS;
1232 if (set & TIOCM_DTR)
1233 info->MCR |= SER_DTR;
1234 if (clear & TIOCM_RTS)
1235 info->MCR &= ~SER_RTS;
1236 if (clear & TIOCM_DTR)
1237 info->MCR &= ~SER_DTR;
1238 rtsdtr_ctrl(info->MCR);
1239 local_irq_restore(flags);
1240 return 0;
1241}
1242
1243/*
1244 * rs_break() --- routine which turns the break handling on or off
1245 */
Alan Cox9e989662008-07-22 11:18:03 +01001246static int rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Alan Coxc9f19e92009-01-02 13:47:26 +00001248 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 unsigned long flags;
1250
1251 if (serial_paranoia_check(info, tty->name, "rs_break"))
Geert Uytterhoeven970a8a52008-08-06 22:19:39 +02001252 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 local_irq_save(flags);
1255 if (break_state == -1)
1256 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1257 else
1258 custom.adkcon = AC_UARTBRK;
1259 mb();
1260 local_irq_restore(flags);
Alan Cox9e989662008-07-22 11:18:03 +01001261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264
1265static int rs_ioctl(struct tty_struct *tty, struct file * file,
1266 unsigned int cmd, unsigned long arg)
1267{
Alan Coxc9f19e92009-01-02 13:47:26 +00001268 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 struct async_icount cprev, cnow; /* kernel counter temps */
1270 struct serial_icounter_struct icount;
Al Viroab14cae2006-01-12 01:06:30 -08001271 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 unsigned long flags;
1273
1274 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1275 return -ENODEV;
1276
1277 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1278 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1279 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1280 if (tty->flags & (1 << TTY_IO_ERROR))
1281 return -EIO;
1282 }
1283
1284 switch (cmd) {
1285 case TIOCGSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001286 return get_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 case TIOCSSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001288 return set_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 case TIOCSERCONFIG:
1290 return 0;
1291
1292 case TIOCSERGETLSR: /* Get line status register */
Al Viroab14cae2006-01-12 01:06:30 -08001293 return get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 case TIOCSERGSTRUCT:
Al Viroab14cae2006-01-12 01:06:30 -08001296 if (copy_to_user(argp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 info, sizeof(struct async_struct)))
1298 return -EFAULT;
1299 return 0;
1300
1301 /*
1302 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1303 * - mask passed in arg for lines of interest
1304 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1305 * Caller should use TIOCGICOUNT to see which one it was
1306 */
1307 case TIOCMIWAIT:
1308 local_irq_save(flags);
1309 /* note the counters on entry */
1310 cprev = info->state->icount;
1311 local_irq_restore(flags);
1312 while (1) {
1313 interruptible_sleep_on(&info->delta_msr_wait);
1314 /* see if a signal did it */
1315 if (signal_pending(current))
1316 return -ERESTARTSYS;
1317 local_irq_save(flags);
1318 cnow = info->state->icount; /* atomic copy */
1319 local_irq_restore(flags);
1320 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1321 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1322 return -EIO; /* no change => error */
1323 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1324 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1325 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1326 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1327 return 0;
1328 }
1329 cprev = cnow;
1330 }
1331 /* NOTREACHED */
1332
1333 /*
1334 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1335 * Return: write counters to the user passed counter struct
1336 * NB: both 1->0 and 0->1 transitions are counted except for
1337 * RI where only 0->1 is counted.
1338 */
1339 case TIOCGICOUNT:
1340 local_irq_save(flags);
1341 cnow = info->state->icount;
1342 local_irq_restore(flags);
1343 icount.cts = cnow.cts;
1344 icount.dsr = cnow.dsr;
1345 icount.rng = cnow.rng;
1346 icount.dcd = cnow.dcd;
1347 icount.rx = cnow.rx;
1348 icount.tx = cnow.tx;
1349 icount.frame = cnow.frame;
1350 icount.overrun = cnow.overrun;
1351 icount.parity = cnow.parity;
1352 icount.brk = cnow.brk;
1353 icount.buf_overrun = cnow.buf_overrun;
1354
Al Viroab14cae2006-01-12 01:06:30 -08001355 if (copy_to_user(argp, &icount, sizeof(icount)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EFAULT;
1357 return 0;
1358 case TIOCSERGWILD:
1359 case TIOCSERSWILD:
1360 /* "setserial -W" is called in Debian boot */
1361 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1362 return 0;
1363
1364 default:
1365 return -ENOIOCTLCMD;
1366 }
1367 return 0;
1368}
1369
Alan Cox606d0992006-12-08 02:38:45 -08001370static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Alan Coxc9f19e92009-01-02 13:47:26 +00001372 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 unsigned long flags;
1374 unsigned int cflag = tty->termios->c_cflag;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 change_speed(info, old_termios);
1377
1378 /* Handle transition to B0 status */
1379 if ((old_termios->c_cflag & CBAUD) &&
1380 !(cflag & CBAUD)) {
1381 info->MCR &= ~(SER_DTR|SER_RTS);
1382 local_irq_save(flags);
1383 rtsdtr_ctrl(info->MCR);
1384 local_irq_restore(flags);
1385 }
1386
1387 /* Handle transition away from B0 status */
1388 if (!(old_termios->c_cflag & CBAUD) &&
1389 (cflag & CBAUD)) {
1390 info->MCR |= SER_DTR;
1391 if (!(tty->termios->c_cflag & CRTSCTS) ||
1392 !test_bit(TTY_THROTTLED, &tty->flags)) {
1393 info->MCR |= SER_RTS;
1394 }
1395 local_irq_save(flags);
1396 rtsdtr_ctrl(info->MCR);
1397 local_irq_restore(flags);
1398 }
1399
1400 /* Handle turning off CRTSCTS */
1401 if ((old_termios->c_cflag & CRTSCTS) &&
1402 !(tty->termios->c_cflag & CRTSCTS)) {
1403 tty->hw_stopped = 0;
1404 rs_start(tty);
1405 }
1406
1407#if 0
1408 /*
1409 * No need to wake up processes in open wait, since they
1410 * sample the CLOCAL flag once, and don't recheck it.
1411 * XXX It's not clear whether the current behavior is correct
1412 * or not. Hence, this may change.....
1413 */
1414 if (!(old_termios->c_cflag & CLOCAL) &&
1415 (tty->termios->c_cflag & CLOCAL))
1416 wake_up_interruptible(&info->open_wait);
1417#endif
1418}
1419
1420/*
1421 * ------------------------------------------------------------
1422 * rs_close()
1423 *
1424 * This routine is called when the serial port gets closed. First, we
1425 * wait for the last remaining data to be sent. Then, we unlink its
1426 * async structure from the interrupt chain if necessary, and we free
1427 * that IRQ if nothing is left in the chain.
1428 * ------------------------------------------------------------
1429 */
1430static void rs_close(struct tty_struct *tty, struct file * filp)
1431{
Alan Coxc9f19e92009-01-02 13:47:26 +00001432 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct serial_state *state;
1434 unsigned long flags;
1435
1436 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1437 return;
1438
1439 state = info->state;
1440
1441 local_irq_save(flags);
1442
1443 if (tty_hung_up_p(filp)) {
1444 DBG_CNT("before DEC-hung");
1445 local_irq_restore(flags);
1446 return;
1447 }
1448
1449#ifdef SERIAL_DEBUG_OPEN
1450 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1451#endif
1452 if ((tty->count == 1) && (state->count != 1)) {
1453 /*
1454 * Uh, oh. tty->count is 1, which means that the tty
1455 * structure will be freed. state->count should always
1456 * be one in these conditions. If it's greater than
1457 * one, we've got real problems, since it means the
1458 * serial port won't be shutdown.
1459 */
1460 printk("rs_close: bad serial port count; tty->count is 1, "
1461 "state->count is %d\n", state->count);
1462 state->count = 1;
1463 }
1464 if (--state->count < 0) {
1465 printk("rs_close: bad serial port count for ttys%d: %d\n",
1466 info->line, state->count);
1467 state->count = 0;
1468 }
1469 if (state->count) {
1470 DBG_CNT("before DEC-2");
1471 local_irq_restore(flags);
1472 return;
1473 }
1474 info->flags |= ASYNC_CLOSING;
1475 /*
1476 * Now we wait for the transmit buffer to clear; and we notify
1477 * the line discipline to only process XON/XOFF characters.
1478 */
1479 tty->closing = 1;
1480 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1481 tty_wait_until_sent(tty, info->closing_wait);
1482 /*
1483 * At this point we stop accepting input. To do this, we
1484 * disable the receive line status interrupts, and tell the
1485 * interrupt driver to stop checking the data ready bit in the
1486 * line status register.
1487 */
1488 info->read_status_mask &= ~UART_LSR_DR;
1489 if (info->flags & ASYNC_INITIALIZED) {
1490 /* disable receive interrupts */
1491 custom.intena = IF_RBF;
1492 mb();
1493 /* clear any pending receive interrupt */
1494 custom.intreq = IF_RBF;
1495 mb();
1496
1497 /*
1498 * Before we drop DTR, make sure the UART transmitter
1499 * has completely drained; this is especially
1500 * important if there is a transmit FIFO!
1501 */
1502 rs_wait_until_sent(tty, info->timeout);
1503 }
1504 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001505 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 tty_ldisc_flush(tty);
1508 tty->closing = 0;
1509 info->event = 0;
1510 info->tty = NULL;
1511 if (info->blocked_open) {
1512 if (info->close_delay) {
1513 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1514 }
1515 wake_up_interruptible(&info->open_wait);
1516 }
1517 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1518 wake_up_interruptible(&info->close_wait);
1519 local_irq_restore(flags);
1520}
1521
1522/*
1523 * rs_wait_until_sent() --- wait until the transmitter is empty
1524 */
1525static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1526{
Alan Coxc9f19e92009-01-02 13:47:26 +00001527 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 unsigned long orig_jiffies, char_time;
1529 int lsr;
1530
1531 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1532 return;
1533
1534 if (info->xmit_fifo_size == 0)
1535 return; /* Just in case.... */
1536
1537 orig_jiffies = jiffies;
Alan Cox978e5952008-04-30 00:53:59 -07001538
1539 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /*
1541 * Set the check interval to be 1/5 of the estimated time to
1542 * send a single character, and make it at least 1. The check
1543 * interval should also be less than the timeout.
1544 *
1545 * Note: we have to use pretty tight timings here to satisfy
1546 * the NIST-PCTS.
1547 */
1548 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1549 char_time = char_time / 5;
1550 if (char_time == 0)
1551 char_time = 1;
1552 if (timeout)
1553 char_time = min_t(unsigned long, char_time, timeout);
1554 /*
1555 * If the transmitter hasn't cleared in twice the approximate
1556 * amount of time to send the entire FIFO, it probably won't
1557 * ever clear. This assumes the UART isn't doing flow
1558 * control, which is currently the case. Hence, if it ever
1559 * takes longer than info->timeout, this is probably due to a
1560 * UART bug of some kind. So, we clamp the timeout parameter at
1561 * 2*info->timeout.
1562 */
1563 if (!timeout || timeout > 2*info->timeout)
1564 timeout = 2*info->timeout;
1565#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1566 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1567 printk("jiff=%lu...", jiffies);
1568#endif
1569 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1570#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1571 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1572#endif
1573 msleep_interruptible(jiffies_to_msecs(char_time));
1574 if (signal_pending(current))
1575 break;
1576 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1577 break;
1578 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001579 __set_current_state(TASK_RUNNING);
Alan Cox978e5952008-04-30 00:53:59 -07001580 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1582 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1583#endif
1584}
1585
1586/*
1587 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1588 */
1589static void rs_hangup(struct tty_struct *tty)
1590{
Alan Coxc9f19e92009-01-02 13:47:26 +00001591 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 struct serial_state *state = info->state;
1593
1594 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1595 return;
1596
1597 state = info->state;
1598
1599 rs_flush_buffer(tty);
1600 shutdown(info);
1601 info->event = 0;
1602 state->count = 0;
1603 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1604 info->tty = NULL;
1605 wake_up_interruptible(&info->open_wait);
1606}
1607
1608/*
1609 * ------------------------------------------------------------
1610 * rs_open() and friends
1611 * ------------------------------------------------------------
1612 */
1613static int block_til_ready(struct tty_struct *tty, struct file * filp,
1614 struct async_struct *info)
1615{
1616#ifdef DECLARE_WAITQUEUE
1617 DECLARE_WAITQUEUE(wait, current);
1618#else
1619 struct wait_queue wait = { current, NULL };
1620#endif
1621 struct serial_state *state = info->state;
1622 int retval;
1623 int do_clocal = 0, extra_count = 0;
1624 unsigned long flags;
1625
1626 /*
1627 * If the device is in the middle of being closed, then block
1628 * until it's done, and then try again.
1629 */
1630 if (tty_hung_up_p(filp) ||
1631 (info->flags & ASYNC_CLOSING)) {
1632 if (info->flags & ASYNC_CLOSING)
1633 interruptible_sleep_on(&info->close_wait);
1634#ifdef SERIAL_DO_RESTART
1635 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1636 -EAGAIN : -ERESTARTSYS);
1637#else
1638 return -EAGAIN;
1639#endif
1640 }
1641
1642 /*
1643 * If non-blocking mode is set, or the port is not enabled,
1644 * then make the check up front and then exit.
1645 */
1646 if ((filp->f_flags & O_NONBLOCK) ||
1647 (tty->flags & (1 << TTY_IO_ERROR))) {
1648 info->flags |= ASYNC_NORMAL_ACTIVE;
1649 return 0;
1650 }
1651
1652 if (tty->termios->c_cflag & CLOCAL)
1653 do_clocal = 1;
1654
1655 /*
1656 * Block waiting for the carrier detect and the line to become
1657 * free (i.e., not in use by the callout). While we are in
1658 * this loop, state->count is dropped by one, so that
1659 * rs_close() knows when to free things. We restore it upon
1660 * exit, either normal or abnormal.
1661 */
1662 retval = 0;
1663 add_wait_queue(&info->open_wait, &wait);
1664#ifdef SERIAL_DEBUG_OPEN
1665 printk("block_til_ready before block: ttys%d, count = %d\n",
1666 state->line, state->count);
1667#endif
1668 local_irq_save(flags);
1669 if (!tty_hung_up_p(filp)) {
1670 extra_count = 1;
1671 state->count--;
1672 }
1673 local_irq_restore(flags);
1674 info->blocked_open++;
1675 while (1) {
1676 local_irq_save(flags);
1677 if (tty->termios->c_cflag & CBAUD)
1678 rtsdtr_ctrl(SER_DTR|SER_RTS);
1679 local_irq_restore(flags);
1680 set_current_state(TASK_INTERRUPTIBLE);
1681 if (tty_hung_up_p(filp) ||
1682 !(info->flags & ASYNC_INITIALIZED)) {
1683#ifdef SERIAL_DO_RESTART
1684 if (info->flags & ASYNC_HUP_NOTIFY)
1685 retval = -EAGAIN;
1686 else
1687 retval = -ERESTARTSYS;
1688#else
1689 retval = -EAGAIN;
1690#endif
1691 break;
1692 }
1693 if (!(info->flags & ASYNC_CLOSING) &&
1694 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1695 break;
1696 if (signal_pending(current)) {
1697 retval = -ERESTARTSYS;
1698 break;
1699 }
1700#ifdef SERIAL_DEBUG_OPEN
1701 printk("block_til_ready blocking: ttys%d, count = %d\n",
1702 info->line, state->count);
1703#endif
1704 schedule();
1705 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001706 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 remove_wait_queue(&info->open_wait, &wait);
1708 if (extra_count)
1709 state->count++;
1710 info->blocked_open--;
1711#ifdef SERIAL_DEBUG_OPEN
1712 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1713 info->line, state->count);
1714#endif
1715 if (retval)
1716 return retval;
1717 info->flags |= ASYNC_NORMAL_ACTIVE;
1718 return 0;
1719}
1720
1721static int get_async_struct(int line, struct async_struct **ret_info)
1722{
1723 struct async_struct *info;
1724 struct serial_state *sstate;
1725
1726 sstate = rs_table + line;
1727 sstate->count++;
1728 if (sstate->info) {
1729 *ret_info = sstate->info;
1730 return 0;
1731 }
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001732 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (!info) {
1734 sstate->count--;
1735 return -ENOMEM;
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737#ifdef DECLARE_WAITQUEUE
1738 init_waitqueue_head(&info->open_wait);
1739 init_waitqueue_head(&info->close_wait);
1740 init_waitqueue_head(&info->delta_msr_wait);
1741#endif
1742 info->magic = SERIAL_MAGIC;
1743 info->port = sstate->port;
1744 info->flags = sstate->flags;
1745 info->xmit_fifo_size = sstate->xmit_fifo_size;
1746 info->line = line;
1747 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1748 info->state = sstate;
1749 if (sstate->info) {
1750 kfree(info);
1751 *ret_info = sstate->info;
1752 return 0;
1753 }
1754 *ret_info = sstate->info = info;
1755 return 0;
1756}
1757
1758/*
1759 * This routine is called whenever a serial port is opened. It
1760 * enables interrupts for a serial port, linking in its async structure into
1761 * the IRQ chain. It also performs the serial-specific
1762 * initialization for the tty structure.
1763 */
1764static int rs_open(struct tty_struct *tty, struct file * filp)
1765{
1766 struct async_struct *info;
1767 int retval, line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 line = tty->index;
1770 if ((line < 0) || (line >= NR_PORTS)) {
1771 return -ENODEV;
1772 }
1773 retval = get_async_struct(line, &info);
1774 if (retval) {
1775 return retval;
1776 }
1777 tty->driver_data = info;
1778 info->tty = tty;
1779 if (serial_paranoia_check(info, tty->name, "rs_open"))
1780 return -ENODEV;
1781
1782#ifdef SERIAL_DEBUG_OPEN
1783 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1784#endif
1785 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * If the port is the middle of closing, bail out now
1789 */
1790 if (tty_hung_up_p(filp) ||
1791 (info->flags & ASYNC_CLOSING)) {
1792 if (info->flags & ASYNC_CLOSING)
1793 interruptible_sleep_on(&info->close_wait);
1794#ifdef SERIAL_DO_RESTART
1795 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1796 -EAGAIN : -ERESTARTSYS);
1797#else
1798 return -EAGAIN;
1799#endif
1800 }
1801
1802 /*
1803 * Start up serial port
1804 */
1805 retval = startup(info);
1806 if (retval) {
1807 return retval;
1808 }
1809
1810 retval = block_til_ready(tty, filp, info);
1811 if (retval) {
1812#ifdef SERIAL_DEBUG_OPEN
1813 printk("rs_open returning after block_til_ready with %d\n",
1814 retval);
1815#endif
1816 return retval;
1817 }
1818
1819#ifdef SERIAL_DEBUG_OPEN
1820 printk("rs_open %s successful...", tty->name);
1821#endif
1822 return 0;
1823}
1824
1825/*
1826 * /proc fs routines....
1827 */
1828
Alexey Dobriyand5940272009-03-31 15:19:23 -07001829static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 struct async_struct *info = state->info, scr_info;
1832 char stat_buf[30], control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 unsigned long flags;
1834
Alexey Dobriyand5940272009-03-31 15:19:23 -07001835 seq_printf(m, "%d: uart:amiga_builtin",state->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 /*
1838 * Figure out the current RS-232 lines
1839 */
1840 if (!info) {
1841 info = &scr_info; /* This is just for serial_{in,out} */
1842
1843 info->magic = SERIAL_MAGIC;
1844 info->flags = state->flags;
1845 info->quot = 0;
1846 info->tty = NULL;
1847 }
1848 local_irq_save(flags);
1849 status = ciab.pra;
1850 control = info ? info->MCR : status;
1851 local_irq_restore(flags);
1852
1853 stat_buf[0] = 0;
1854 stat_buf[1] = 0;
1855 if(!(control & SER_RTS))
1856 strcat(stat_buf, "|RTS");
1857 if(!(status & SER_CTS))
1858 strcat(stat_buf, "|CTS");
1859 if(!(control & SER_DTR))
1860 strcat(stat_buf, "|DTR");
1861 if(!(status & SER_DSR))
1862 strcat(stat_buf, "|DSR");
1863 if(!(status & SER_DCD))
1864 strcat(stat_buf, "|CD");
1865
1866 if (info->quot) {
Alexey Dobriyand5940272009-03-31 15:19:23 -07001867 seq_printf(m, " baud:%d", state->baud_base / info->quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
Alexey Dobriyand5940272009-03-31 15:19:23 -07001870 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 if (state->icount.frame)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001873 seq_printf(m, " fe:%d", state->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 if (state->icount.parity)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001876 seq_printf(m, " pe:%d", state->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 if (state->icount.brk)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001879 seq_printf(m, " brk:%d", state->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 if (state->icount.overrun)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001882 seq_printf(m, " oe:%d", state->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /*
1885 * Last thing is the RS-232 status lines
1886 */
Alexey Dobriyand5940272009-03-31 15:19:23 -07001887 seq_printf(m, " %s\n", stat_buf+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Alexey Dobriyand5940272009-03-31 15:19:23 -07001890static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Alexey Dobriyand5940272009-03-31 15:19:23 -07001892 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1893 line_info(m, &rs_table[0]);
1894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
Alexey Dobriyand5940272009-03-31 15:19:23 -07001897static int rs_proc_open(struct inode *inode, struct file *file)
1898{
1899 return single_open(file, rs_proc_show, NULL);
1900}
1901
1902static const struct file_operations rs_proc_fops = {
1903 .owner = THIS_MODULE,
1904 .open = rs_proc_open,
1905 .read = seq_read,
1906 .llseek = seq_lseek,
1907 .release = single_release,
1908};
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910/*
1911 * ---------------------------------------------------------------------
1912 * rs_init() and friends
1913 *
1914 * rs_init() is called at boot-time to initialize the serial driver.
1915 * ---------------------------------------------------------------------
1916 */
1917
1918/*
1919 * This routine prints out the appropriate serial driver version
1920 * number, and identifies which options were configured into this
1921 * driver.
1922 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001923static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1926}
1927
1928
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001929static const struct tty_operations serial_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 .open = rs_open,
1931 .close = rs_close,
1932 .write = rs_write,
1933 .put_char = rs_put_char,
1934 .flush_chars = rs_flush_chars,
1935 .write_room = rs_write_room,
1936 .chars_in_buffer = rs_chars_in_buffer,
1937 .flush_buffer = rs_flush_buffer,
1938 .ioctl = rs_ioctl,
1939 .throttle = rs_throttle,
1940 .unthrottle = rs_unthrottle,
1941 .set_termios = rs_set_termios,
1942 .stop = rs_stop,
1943 .start = rs_start,
1944 .hangup = rs_hangup,
1945 .break_ctl = rs_break,
1946 .send_xchar = rs_send_xchar,
1947 .wait_until_sent = rs_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 .tiocmget = rs_tiocmget,
1949 .tiocmset = rs_tiocmset,
Alexey Dobriyand5940272009-03-31 15:19:23 -07001950 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951};
1952
1953/*
1954 * The serial driver boot-time initialization code!
1955 */
1956static int __init rs_init(void)
1957{
1958 unsigned long flags;
1959 struct serial_state * state;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001960 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
1963 return -ENODEV;
1964
1965 serial_driver = alloc_tty_driver(1);
1966 if (!serial_driver)
1967 return -ENOMEM;
1968
1969 /*
1970 * We request SERDAT and SERPER only, because the serial registers are
1971 * too spreaded over the custom register space
1972 */
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001973 if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4,
1974 "amiserial [Paula]")) {
1975 error = -EBUSY;
1976 goto fail_put_tty_driver;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 IRQ_ports = NULL;
1980
1981 show_serial_version();
1982
1983 /* Initialize the tty_driver structure */
1984
1985 serial_driver->owner = THIS_MODULE;
1986 serial_driver->driver_name = "amiserial";
1987 serial_driver->name = "ttyS";
1988 serial_driver->major = TTY_MAJOR;
1989 serial_driver->minor_start = 64;
1990 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1991 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1992 serial_driver->init_termios = tty_std_termios;
1993 serial_driver->init_termios.c_cflag =
1994 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1995 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1996 tty_set_operations(serial_driver, &serial_ops);
1997
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001998 error = tty_register_driver(serial_driver);
1999 if (error)
2000 goto fail_release_mem_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 state = rs_table;
2003 state->magic = SSTATE_MAGIC;
2004 state->port = (int)&custom.serdatr; /* Just to give it a value */
2005 state->line = 0;
2006 state->custom_divisor = 0;
2007 state->close_delay = 5*HZ/10;
2008 state->closing_wait = 30*HZ;
2009 state->icount.cts = state->icount.dsr =
2010 state->icount.rng = state->icount.dcd = 0;
2011 state->icount.rx = state->icount.tx = 0;
2012 state->icount.frame = state->icount.parity = 0;
2013 state->icount.overrun = state->icount.brk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
2016 state->line);
2017
2018 /* Hardware set up */
2019
2020 state->baud_base = amiga_colorclock;
2021 state->xmit_fifo_size = 1;
2022
2023 local_irq_save(flags);
2024
2025 /* set ISRs, and then disable the rx interrupts */
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01002026 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2027 if (error)
2028 goto fail_unregister;
2029
2030 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, IRQF_DISABLED,
2031 "serial RX", state);
2032 if (error)
2033 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /* turn off Rx and Tx interrupts */
2036 custom.intena = IF_RBF | IF_TBE;
2037 mb();
2038
2039 /* clear any pending interrupt */
2040 custom.intreq = IF_RBF | IF_TBE;
2041 mb();
2042
2043 local_irq_restore(flags);
2044
2045 /*
2046 * set the appropriate directions for the modem control flags,
2047 * and clear RTS and DTR
2048 */
2049 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2050 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2051
2052 return 0;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01002053
2054fail_free_irq:
2055 free_irq(IRQ_AMIGA_TBE, state);
2056fail_unregister:
2057 tty_unregister_driver(serial_driver);
2058fail_release_mem_region:
2059 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2060fail_put_tty_driver:
2061 put_tty_driver(serial_driver);
2062 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
2065static __exit void rs_exit(void)
2066{
2067 int error;
2068 struct async_struct *info = rs_table[0].info;
2069
2070 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2071 tasklet_kill(&info->tlet);
2072 if ((error = tty_unregister_driver(serial_driver)))
2073 printk("SERIAL: failed to unregister serial driver (%d)\n",
2074 error);
2075 put_tty_driver(serial_driver);
2076
2077 if (info) {
2078 rs_table[0].info = NULL;
2079 kfree(info);
2080 }
2081
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01002082 free_irq(IRQ_AMIGA_TBE, rs_table);
2083 free_irq(IRQ_AMIGA_RBF, rs_table);
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2086}
2087
2088module_init(rs_init)
2089module_exit(rs_exit)
2090
2091
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01002092#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094/*
2095 * ------------------------------------------------------------
2096 * Serial console driver
2097 * ------------------------------------------------------------
2098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100static void amiga_serial_putc(char c)
2101{
2102 custom.serdat = (unsigned char)c | 0x100;
2103 while (!(custom.serdatr & 0x2000))
2104 barrier();
2105}
2106
2107/*
2108 * Print a string to the serial port trying not to disturb
2109 * any possible real use of the port...
2110 *
2111 * The console must be locked when we get here.
2112 */
2113static void serial_console_write(struct console *co, const char *s,
2114 unsigned count)
2115{
2116 unsigned short intena = custom.intenar;
2117
2118 custom.intena = IF_TBE;
2119
2120 while (count--) {
2121 if (*s == '\n')
2122 amiga_serial_putc('\r');
2123 amiga_serial_putc(*s++);
2124 }
2125
2126 custom.intena = IF_SETCLR | (intena & IF_TBE);
2127}
2128
2129static struct tty_driver *serial_console_device(struct console *c, int *index)
2130{
2131 *index = 0;
2132 return serial_driver;
2133}
2134
2135static struct console sercons = {
2136 .name = "ttyS",
2137 .write = serial_console_write,
2138 .device = serial_console_device,
2139 .flags = CON_PRINTBUFFER,
2140 .index = -1,
2141};
2142
2143/*
2144 * Register console.
2145 */
2146static int __init amiserial_console_init(void)
2147{
2148 register_console(&sercons);
2149 return 0;
2150}
2151console_initcall(amiserial_console_init);
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01002152
2153#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155MODULE_LICENSE("GPL");