blob: d5fac8626988b4a9a80c062201ee4cfe41b06b28 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Serial driver for the amiga builtin port.
3 *
4 * This code was created by taking serial.c version 4.30 from kernel
5 * release 2.3.22, replacing all hardware related stuff with the
6 * corresponding amiga hardware actions, and removing all irrelevant
7 * code. As a consequence, it uses many of the constants and names
8 * associated with the registers and bits of 16550 compatible UARTS -
9 * but only to keep track of status, etc in the state variables. It
10 * was done this was to make it easier to keep the code in line with
11 * (non hardware specific) changes to serial.c.
12 *
13 * The port is registered with the tty driver as minor device 64, and
14 * therefore other ports should should only use 65 upwards.
15 *
16 * Richard Lucock 28/12/99
17 *
18 * Copyright (C) 1991, 1992 Linus Torvalds
19 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
20 * 1998, 1999 Theodore Ts'o
21 *
22 */
23
24/*
25 * Serial driver configuration section. Here are the various options:
26 *
27 * SERIAL_PARANOIA_CHECK
28 * Check the magic number for the async_structure where
29 * ever possible.
30 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
33
34#undef SERIAL_PARANOIA_CHECK
35#define SERIAL_DO_RESTART
36
37/* Set of debugging defines */
38
39#undef SERIAL_DEBUG_INTR
40#undef SERIAL_DEBUG_OPEN
41#undef SERIAL_DEBUG_FLOW
42#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
43
44/* Sanity checks */
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
47#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
48 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
49#else
50#define DBG_CNT(s)
51#endif
52
53/*
54 * End of serial driver configuration section.
55 */
56
57#include <linux/module.h>
58
59#include <linux/types.h>
60#include <linux/serial.h>
61#include <linux/serialP.h>
62#include <linux/serial_reg.h>
63static char *serial_version = "4.30";
64
65#include <linux/errno.h>
66#include <linux/signal.h>
67#include <linux/sched.h>
68#include <linux/kernel.h>
69#include <linux/timer.h>
70#include <linux/interrupt.h>
71#include <linux/tty.h>
72#include <linux/tty_flip.h>
73#include <linux/console.h>
74#include <linux/major.h>
75#include <linux/string.h>
76#include <linux/fcntl.h>
77#include <linux/ptrace.h>
78#include <linux/ioport.h>
79#include <linux/mm.h>
Alexey Dobriyand5940272009-03-31 15:19:23 -070080#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/slab.h>
82#include <linux/init.h>
83#include <linux/bitops.h>
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +020084#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <asm/setup.h>
87
88#include <asm/system.h>
89
90#include <asm/irq.h>
91
92#include <asm/amigahw.h>
93#include <asm/amigaints.h>
94
Al Virob4290a22006-01-12 01:06:12 -080095#define custom amiga_custom
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static char *serial_name = "Amiga-builtin serial driver";
97
98static struct tty_driver *serial_driver;
99
100/* number of characters left in xmit buffer before we ask for more */
101#define WAKEUP_CHARS 256
102
103static struct async_struct *IRQ_ports;
104
105static unsigned char current_ctl_bits;
106
Alan Cox606d0992006-12-08 02:38:45 -0800107static void change_speed(struct async_struct *info, struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
109
110
111static struct serial_state rs_table[1];
112
Tobias Klauserfe971072006-01-09 20:54:02 -0800113#define NR_PORTS ARRAY_SIZE(rs_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <asm/uaccess.h>
116
117#define serial_isroot() (capable(CAP_SYS_ADMIN))
118
119
120static inline int serial_paranoia_check(struct async_struct *info,
121 char *name, const char *routine)
122{
123#ifdef SERIAL_PARANOIA_CHECK
124 static const char *badmagic =
125 "Warning: bad magic number for serial struct (%s) in %s\n";
126 static const char *badinfo =
127 "Warning: null async_struct for (%s) in %s\n";
128
129 if (!info) {
130 printk(badinfo, name, routine);
131 return 1;
132 }
133 if (info->magic != SERIAL_MAGIC) {
134 printk(badmagic, name, routine);
135 return 1;
136 }
137#endif
138 return 0;
139}
140
141/* some serial hardware definitions */
142#define SDR_OVRUN (1<<15)
143#define SDR_RBF (1<<14)
144#define SDR_TBE (1<<13)
145#define SDR_TSRE (1<<12)
146
147#define SERPER_PARENB (1<<15)
148
149#define AC_SETCLR (1<<15)
150#define AC_UARTBRK (1<<11)
151
152#define SER_DTR (1<<7)
153#define SER_RTS (1<<6)
154#define SER_DCD (1<<5)
155#define SER_CTS (1<<4)
156#define SER_DSR (1<<3)
157
158static __inline__ void rtsdtr_ctrl(int bits)
159{
160 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
161}
162
163/*
164 * ------------------------------------------------------------
165 * rs_stop() and rs_start()
166 *
167 * This routines are called before setting or resetting tty->stopped.
168 * They enable or disable transmitter interrupts, as necessary.
169 * ------------------------------------------------------------
170 */
171static void rs_stop(struct tty_struct *tty)
172{
Alan Coxc9f19e92009-01-02 13:47:26 +0000173 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned long flags;
175
176 if (serial_paranoia_check(info, tty->name, "rs_stop"))
177 return;
178
179 local_irq_save(flags);
180 if (info->IER & UART_IER_THRI) {
181 info->IER &= ~UART_IER_THRI;
182 /* disable Tx interrupt and remove any pending interrupts */
183 custom.intena = IF_TBE;
184 mb();
185 custom.intreq = IF_TBE;
186 mb();
187 }
188 local_irq_restore(flags);
189}
190
191static void rs_start(struct tty_struct *tty)
192{
Alan Coxc9f19e92009-01-02 13:47:26 +0000193 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned long flags;
195
196 if (serial_paranoia_check(info, tty->name, "rs_start"))
197 return;
198
199 local_irq_save(flags);
200 if (info->xmit.head != info->xmit.tail
201 && info->xmit.buf
202 && !(info->IER & UART_IER_THRI)) {
203 info->IER |= UART_IER_THRI;
204 custom.intena = IF_SETCLR | IF_TBE;
205 mb();
206 /* set a pending Tx Interrupt, transmitter should restart now */
207 custom.intreq = IF_SETCLR | IF_TBE;
208 mb();
209 }
210 local_irq_restore(flags);
211}
212
213/*
214 * ----------------------------------------------------------------------
215 *
216 * Here starts the interrupt handling routines. All of the following
217 * subroutines are declared as inline and are folded into
218 * rs_interrupt(). They were separated out for readability's sake.
219 *
220 * Note: rs_interrupt() is a "fast" interrupt, which means that it
221 * runs with interrupts turned off. People who may want to modify
222 * rs_interrupt() should try to keep the interrupt handler as fast as
223 * possible. After you are done making modifications, it is not a bad
224 * idea to do:
225 *
226 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
227 *
228 * and look at the resulting assemble code in serial.s.
229 *
230 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
231 * -----------------------------------------------------------------------
232 */
233
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800234static void receive_chars(struct async_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int status;
237 int serdatr;
238 struct tty_struct *tty = info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800239 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct async_icount *icount;
Alan Cox33f0f882006-01-09 20:54:13 -0800241 int oe = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 icount = &info->state->icount;
244
245 status = UART_LSR_DR; /* We obviously have a character! */
246 serdatr = custom.serdatr;
247 mb();
248 custom.intreq = IF_RBF;
249 mb();
250
251 if((serdatr & 0x1ff) == 0)
252 status |= UART_LSR_BI;
253 if(serdatr & SDR_OVRUN)
254 status |= UART_LSR_OE;
255
256 ch = serdatr & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 icount->rx++;
258
259#ifdef SERIAL_DEBUG_INTR
260 printk("DR%02x:%02x...", ch, status);
261#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800262 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
265 * We don't handle parity or frame errors - but I have left
266 * the code in, since I'm not sure that the errors can't be
267 * detected.
268 */
269
270 if (status & (UART_LSR_BI | UART_LSR_PE |
271 UART_LSR_FE | UART_LSR_OE)) {
272 /*
273 * For statistics only
274 */
275 if (status & UART_LSR_BI) {
276 status &= ~(UART_LSR_FE | UART_LSR_PE);
277 icount->brk++;
278 } else if (status & UART_LSR_PE)
279 icount->parity++;
280 else if (status & UART_LSR_FE)
281 icount->frame++;
282 if (status & UART_LSR_OE)
283 icount->overrun++;
284
285 /*
286 * Now check to see if character should be
287 * ignored, and mask off conditions which
288 * should be ignored.
289 */
290 if (status & info->ignore_status_mask)
Alan Cox33f0f882006-01-09 20:54:13 -0800291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 status &= info->read_status_mask;
294
295 if (status & (UART_LSR_BI)) {
296#ifdef SERIAL_DEBUG_INTR
297 printk("handling break....");
298#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800299 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (info->flags & ASYNC_SAK)
301 do_SAK(tty);
302 } else if (status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800303 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else if (status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800305 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (status & UART_LSR_OE) {
307 /*
308 * Overrun is special, since it's
309 * reported immediately, and doesn't
310 * affect the current character
311 */
Alan Cox33f0f882006-01-09 20:54:13 -0800312 oe = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 }
Alan Cox33f0f882006-01-09 20:54:13 -0800315 tty_insert_flip_char(tty, ch, flag);
316 if (oe == 1)
317 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 tty_flip_buffer_push(tty);
Alan Cox33f0f882006-01-09 20:54:13 -0800319out:
320 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800323static void transmit_chars(struct async_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 custom.intreq = IF_TBE;
326 mb();
327 if (info->x_char) {
328 custom.serdat = info->x_char | 0x100;
329 mb();
330 info->state->icount.tx++;
331 info->x_char = 0;
332 return;
333 }
334 if (info->xmit.head == info->xmit.tail
335 || info->tty->stopped
336 || info->tty->hw_stopped) {
337 info->IER &= ~UART_IER_THRI;
338 custom.intena = IF_TBE;
339 mb();
340 return;
341 }
342
343 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
344 mb();
345 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
346 info->state->icount.tx++;
347
348 if (CIRC_CNT(info->xmit.head,
349 info->xmit.tail,
350 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
Jiri Slabyc5f05082012-03-05 14:52:13 +0100351 tty_wakeup(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353#ifdef SERIAL_DEBUG_INTR
354 printk("THRE...");
355#endif
356 if (info->xmit.head == info->xmit.tail) {
357 custom.intena = IF_TBE;
358 mb();
359 info->IER &= ~UART_IER_THRI;
360 }
361}
362
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800363static void check_modem_status(struct async_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
366 unsigned char dstatus;
367 struct async_icount *icount;
368
369 /* Determine bits that have changed */
370 dstatus = status ^ current_ctl_bits;
371 current_ctl_bits = status;
372
373 if (dstatus) {
374 icount = &info->state->icount;
375 /* update input line counters */
376 if (dstatus & SER_DSR)
377 icount->dsr++;
378 if (dstatus & SER_DCD) {
379 icount->dcd++;
380#ifdef CONFIG_HARD_PPS
381 if ((info->flags & ASYNC_HARDPPS_CD) &&
382 !(status & SER_DCD))
383 hardpps();
384#endif
385 }
386 if (dstatus & SER_CTS)
387 icount->cts++;
388 wake_up_interruptible(&info->delta_msr_wait);
389 }
390
391 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
392#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
393 printk("ttyS%d CD now %s...", info->line,
394 (!(status & SER_DCD)) ? "on" : "off");
395#endif
396 if (!(status & SER_DCD))
397 wake_up_interruptible(&info->open_wait);
398 else {
399#ifdef SERIAL_DEBUG_OPEN
400 printk("doing serial hangup...");
401#endif
402 if (info->tty)
403 tty_hangup(info->tty);
404 }
405 }
406 if (info->flags & ASYNC_CTS_FLOW) {
407 if (info->tty->hw_stopped) {
408 if (!(status & SER_CTS)) {
409#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
410 printk("CTS tx start...");
411#endif
412 info->tty->hw_stopped = 0;
413 info->IER |= UART_IER_THRI;
414 custom.intena = IF_SETCLR | IF_TBE;
415 mb();
416 /* set a pending Tx Interrupt, transmitter should restart now */
417 custom.intreq = IF_SETCLR | IF_TBE;
418 mb();
Jiri Slabyc5f05082012-03-05 14:52:13 +0100419 tty_wakeup(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return;
421 }
422 } else {
423 if ((status & SER_CTS)) {
424#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
425 printk("CTS tx stop...");
426#endif
427 info->tty->hw_stopped = 1;
428 info->IER &= ~UART_IER_THRI;
429 /* disable Tx interrupt and remove any pending interrupts */
430 custom.intena = IF_TBE;
431 mb();
432 custom.intreq = IF_TBE;
433 mb();
434 }
435 }
436 }
437}
438
David Howells7d12e782006-10-05 14:55:46 +0100439static irqreturn_t ser_vbl_int( int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 /* vbl is just a periodic interrupt we tie into to update modem status */
442 struct async_struct * info = IRQ_ports;
443 /*
444 * TBD - is it better to unregister from this interrupt or to
445 * ignore it if MSI is clear ?
446 */
447 if(info->IER & UART_IER_MSI)
448 check_modem_status(info);
449 return IRQ_HANDLED;
450}
451
David Howells7d12e782006-10-05 14:55:46 +0100452static irqreturn_t ser_rx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct async_struct * info;
455
456#ifdef SERIAL_DEBUG_INTR
457 printk("ser_rx_int...");
458#endif
459
460 info = IRQ_ports;
461 if (!info || !info->tty)
462 return IRQ_NONE;
463
464 receive_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#ifdef SERIAL_DEBUG_INTR
466 printk("end.\n");
467#endif
468 return IRQ_HANDLED;
469}
470
David Howells7d12e782006-10-05 14:55:46 +0100471static irqreturn_t ser_tx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 struct async_struct * info;
474
475 if (custom.serdatr & SDR_TBE) {
476#ifdef SERIAL_DEBUG_INTR
477 printk("ser_tx_int...");
478#endif
479
480 info = IRQ_ports;
481 if (!info || !info->tty)
482 return IRQ_NONE;
483
484 transmit_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#ifdef SERIAL_DEBUG_INTR
486 printk("end.\n");
487#endif
488 }
489 return IRQ_HANDLED;
490}
491
492/*
493 * -------------------------------------------------------------------
494 * Here ends the serial interrupt routines.
495 * -------------------------------------------------------------------
496 */
497
498/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * ---------------------------------------------------------------
500 * Low level utility subroutines for the serial driver: routines to
501 * figure out the appropriate timeout for an interrupt chain, routines
502 * to initialize and startup a serial port, and routines to shutdown a
503 * serial port. Useful stuff like that.
504 * ---------------------------------------------------------------
505 */
506
507static int startup(struct async_struct * info)
508{
509 unsigned long flags;
510 int retval=0;
511 unsigned long page;
512
513 page = get_zeroed_page(GFP_KERNEL);
514 if (!page)
515 return -ENOMEM;
516
517 local_irq_save(flags);
518
519 if (info->flags & ASYNC_INITIALIZED) {
520 free_page(page);
521 goto errout;
522 }
523
524 if (info->xmit.buf)
525 free_page(page);
526 else
527 info->xmit.buf = (unsigned char *) page;
528
529#ifdef SERIAL_DEBUG_OPEN
530 printk("starting up ttys%d ...", info->line);
531#endif
532
533 /* Clear anything in the input buffer */
534
535 custom.intreq = IF_RBF;
536 mb();
537
538 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
539 if (retval) {
540 if (serial_isroot()) {
541 if (info->tty)
542 set_bit(TTY_IO_ERROR,
543 &info->tty->flags);
544 retval = 0;
545 }
546 goto errout;
547 }
548
549 /* enable both Rx and Tx interrupts */
550 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
551 mb();
552 info->IER = UART_IER_MSI;
553
554 /* remember current state of the DCD and CTS bits */
555 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
556
557 IRQ_ports = info;
558
559 info->MCR = 0;
560 if (info->tty->termios->c_cflag & CBAUD)
561 info->MCR = SER_DTR | SER_RTS;
562 rtsdtr_ctrl(info->MCR);
563
564 if (info->tty)
565 clear_bit(TTY_IO_ERROR, &info->tty->flags);
566 info->xmit.head = info->xmit.tail = 0;
567
568 /*
569 * Set up the tty->alt_speed kludge
570 */
571 if (info->tty) {
572 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
573 info->tty->alt_speed = 57600;
574 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
575 info->tty->alt_speed = 115200;
576 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
577 info->tty->alt_speed = 230400;
578 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
579 info->tty->alt_speed = 460800;
580 }
581
582 /*
583 * and set the speed of the serial port
584 */
585 change_speed(info, NULL);
586
587 info->flags |= ASYNC_INITIALIZED;
588 local_irq_restore(flags);
589 return 0;
590
591errout:
592 local_irq_restore(flags);
593 return retval;
594}
595
596/*
597 * This routine will shutdown a serial port; interrupts are disabled, and
598 * DTR is dropped if the hangup on close termio flag is on.
599 */
600static void shutdown(struct async_struct * info)
601{
602 unsigned long flags;
603 struct serial_state *state;
604
605 if (!(info->flags & ASYNC_INITIALIZED))
606 return;
607
608 state = info->state;
609
610#ifdef SERIAL_DEBUG_OPEN
611 printk("Shutting down serial port %d ....\n", info->line);
612#endif
613
614 local_irq_save(flags); /* Disable interrupts */
615
616 /*
617 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
618 * here so the queue might never be waken up
619 */
620 wake_up_interruptible(&info->delta_msr_wait);
621
622 IRQ_ports = NULL;
623
624 /*
625 * Free the IRQ, if necessary
626 */
627 free_irq(IRQ_AMIGA_VERTB, info);
628
629 if (info->xmit.buf) {
630 free_page((unsigned long) info->xmit.buf);
631 info->xmit.buf = NULL;
632 }
633
634 info->IER = 0;
635 custom.intena = IF_RBF | IF_TBE;
636 mb();
637
638 /* disable break condition */
639 custom.adkcon = AC_UARTBRK;
640 mb();
641
642 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
643 info->MCR &= ~(SER_DTR|SER_RTS);
644 rtsdtr_ctrl(info->MCR);
645
646 if (info->tty)
647 set_bit(TTY_IO_ERROR, &info->tty->flags);
648
649 info->flags &= ~ASYNC_INITIALIZED;
650 local_irq_restore(flags);
651}
652
653
654/*
655 * This routine is called to set the UART divisor registers to match
656 * the specified baud rate for a serial port.
657 */
658static void change_speed(struct async_struct *info,
Alan Cox606d0992006-12-08 02:38:45 -0800659 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 int quot = 0, baud_base, baud;
662 unsigned cflag, cval = 0;
663 int bits;
664 unsigned long flags;
665
666 if (!info->tty || !info->tty->termios)
667 return;
668 cflag = info->tty->termios->c_cflag;
669
670 /* Byte size is always 8 bits plus parity bit if requested */
671
672 cval = 3; bits = 10;
673 if (cflag & CSTOPB) {
674 cval |= 0x04;
675 bits++;
676 }
677 if (cflag & PARENB) {
678 cval |= UART_LCR_PARITY;
679 bits++;
680 }
681 if (!(cflag & PARODD))
682 cval |= UART_LCR_EPAR;
683#ifdef CMSPAR
684 if (cflag & CMSPAR)
685 cval |= UART_LCR_SPAR;
686#endif
687
688 /* Determine divisor based on baud rate */
689 baud = tty_get_baud_rate(info->tty);
690 if (!baud)
691 baud = 9600; /* B0 transition handled in rs_set_termios */
692 baud_base = info->state->baud_base;
693 if (baud == 38400 &&
694 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
695 quot = info->state->custom_divisor;
696 else {
697 if (baud == 134)
698 /* Special case since 134 is really 134.5 */
699 quot = (2*baud_base / 269);
700 else if (baud)
701 quot = baud_base / baud;
702 }
703 /* If the quotient is zero refuse the change */
704 if (!quot && old_termios) {
Alan Coxdb0ef082007-07-15 23:41:47 -0700705 /* FIXME: Will need updating for new tty in the end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 info->tty->termios->c_cflag &= ~CBAUD;
707 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
708 baud = tty_get_baud_rate(info->tty);
709 if (!baud)
710 baud = 9600;
711 if (baud == 38400 &&
712 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
713 quot = info->state->custom_divisor;
714 else {
715 if (baud == 134)
716 /* Special case since 134 is really 134.5 */
717 quot = (2*baud_base / 269);
718 else if (baud)
719 quot = baud_base / baud;
720 }
721 }
722 /* As a last resort, if the quotient is zero, default to 9600 bps */
723 if (!quot)
724 quot = baud_base / 9600;
725 info->quot = quot;
726 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
727 info->timeout += HZ/50; /* Add .02 seconds of slop */
728
729 /* CTS flow control flag and modem status interrupts */
730 info->IER &= ~UART_IER_MSI;
731 if (info->flags & ASYNC_HARDPPS_CD)
732 info->IER |= UART_IER_MSI;
733 if (cflag & CRTSCTS) {
734 info->flags |= ASYNC_CTS_FLOW;
735 info->IER |= UART_IER_MSI;
736 } else
737 info->flags &= ~ASYNC_CTS_FLOW;
738 if (cflag & CLOCAL)
739 info->flags &= ~ASYNC_CHECK_CD;
740 else {
741 info->flags |= ASYNC_CHECK_CD;
742 info->IER |= UART_IER_MSI;
743 }
744 /* TBD:
Thadeu Lima de Souza Cascardo4b512d22009-04-14 23:14:10 -0300745 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
747
748 /*
749 * Set up parity check flag
750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
753 if (I_INPCK(info->tty))
754 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
755 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
756 info->read_status_mask |= UART_LSR_BI;
757
758 /*
759 * Characters to ignore
760 */
761 info->ignore_status_mask = 0;
762 if (I_IGNPAR(info->tty))
763 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
764 if (I_IGNBRK(info->tty)) {
765 info->ignore_status_mask |= UART_LSR_BI;
766 /*
767 * If we're ignore parity and break indicators, ignore
768 * overruns too. (For real raw support).
769 */
770 if (I_IGNPAR(info->tty))
771 info->ignore_status_mask |= UART_LSR_OE;
772 }
773 /*
774 * !!! ignore all characters if CREAD is not set
775 */
776 if ((cflag & CREAD) == 0)
777 info->ignore_status_mask |= UART_LSR_DR;
778 local_irq_save(flags);
779
780 {
781 short serper;
782
783 /* Set up the baud rate */
784 serper = quot - 1;
785
786 /* Enable or disable parity bit */
787
788 if(cval & UART_LCR_PARITY)
789 serper |= (SERPER_PARENB);
790
791 custom.serper = serper;
792 mb();
793 }
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 local_irq_restore(flags);
796}
797
Alan Cox257afa32008-04-30 00:54:02 -0700798static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700800 struct async_struct *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned long flags;
802
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700803 info = tty->driver_data;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
Alan Cox257afa32008-04-30 00:54:02 -0700806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700808 if (!info->xmit.buf)
Alan Cox257afa32008-04-30 00:54:02 -0700809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 local_irq_save(flags);
812 if (CIRC_SPACE(info->xmit.head,
813 info->xmit.tail,
814 SERIAL_XMIT_SIZE) == 0) {
815 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
819 info->xmit.buf[info->xmit.head++] = ch;
820 info->xmit.head &= SERIAL_XMIT_SIZE-1;
821 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700822 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825static void rs_flush_chars(struct tty_struct *tty)
826{
Alan Coxc9f19e92009-01-02 13:47:26 +0000827 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 unsigned long flags;
829
830 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
831 return;
832
833 if (info->xmit.head == info->xmit.tail
834 || tty->stopped
835 || tty->hw_stopped
836 || !info->xmit.buf)
837 return;
838
839 local_irq_save(flags);
840 info->IER |= UART_IER_THRI;
841 custom.intena = IF_SETCLR | IF_TBE;
842 mb();
843 /* set a pending Tx Interrupt, transmitter should restart now */
844 custom.intreq = IF_SETCLR | IF_TBE;
845 mb();
846 local_irq_restore(flags);
847}
848
849static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
850{
851 int c, ret = 0;
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700852 struct async_struct *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 unsigned long flags;
854
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700855 info = tty->driver_data;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (serial_paranoia_check(info, tty->name, "rs_write"))
858 return 0;
859
Jiri Slabyb3218a72006-10-04 02:15:27 -0700860 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 0;
862
Jiri Kosina3abf3be2007-02-10 01:46:48 -0800863 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 while (1) {
865 c = CIRC_SPACE_TO_END(info->xmit.head,
866 info->xmit.tail,
867 SERIAL_XMIT_SIZE);
868 if (count < c)
869 c = count;
870 if (c <= 0) {
871 break;
872 }
873 memcpy(info->xmit.buf + info->xmit.head, buf, c);
874 info->xmit.head = ((info->xmit.head + c) &
875 (SERIAL_XMIT_SIZE-1));
876 buf += c;
877 count -= c;
878 ret += c;
879 }
880 local_irq_restore(flags);
881
882 if (info->xmit.head != info->xmit.tail
883 && !tty->stopped
884 && !tty->hw_stopped
885 && !(info->IER & UART_IER_THRI)) {
886 info->IER |= UART_IER_THRI;
887 local_irq_disable();
888 custom.intena = IF_SETCLR | IF_TBE;
889 mb();
890 /* set a pending Tx Interrupt, transmitter should restart now */
891 custom.intreq = IF_SETCLR | IF_TBE;
892 mb();
893 local_irq_restore(flags);
894 }
895 return ret;
896}
897
898static int rs_write_room(struct tty_struct *tty)
899{
Alan Coxc9f19e92009-01-02 13:47:26 +0000900 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
903 return 0;
904 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
905}
906
907static int rs_chars_in_buffer(struct tty_struct *tty)
908{
Alan Coxc9f19e92009-01-02 13:47:26 +0000909 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
912 return 0;
913 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
914}
915
916static void rs_flush_buffer(struct tty_struct *tty)
917{
Alan Coxc9f19e92009-01-02 13:47:26 +0000918 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unsigned long flags;
920
921 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
922 return;
923 local_irq_save(flags);
924 info->xmit.head = info->xmit.tail = 0;
925 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 tty_wakeup(tty);
927}
928
929/*
930 * This function is used to send a high-priority XON/XOFF character to
931 * the device
932 */
933static void rs_send_xchar(struct tty_struct *tty, char ch)
934{
Alan Coxc9f19e92009-01-02 13:47:26 +0000935 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 unsigned long flags;
937
938 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
939 return;
940
941 info->x_char = ch;
942 if (ch) {
943 /* Make sure transmit interrupts are on */
944
945 /* Check this ! */
946 local_irq_save(flags);
947 if(!(custom.intenar & IF_TBE)) {
948 custom.intena = IF_SETCLR | IF_TBE;
949 mb();
950 /* set a pending Tx Interrupt, transmitter should restart now */
951 custom.intreq = IF_SETCLR | IF_TBE;
952 mb();
953 }
954 local_irq_restore(flags);
955
956 info->IER |= UART_IER_THRI;
957 }
958}
959
960/*
961 * ------------------------------------------------------------
962 * rs_throttle()
963 *
964 * This routine is called by the upper-layer tty layer to signal that
965 * incoming characters should be throttled.
966 * ------------------------------------------------------------
967 */
968static void rs_throttle(struct tty_struct * tty)
969{
Alan Coxc9f19e92009-01-02 13:47:26 +0000970 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 unsigned long flags;
972#ifdef SERIAL_DEBUG_THROTTLE
973 char buf[64];
974
975 printk("throttle %s: %d....\n", tty_name(tty, buf),
976 tty->ldisc.chars_in_buffer(tty));
977#endif
978
979 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
980 return;
981
982 if (I_IXOFF(tty))
983 rs_send_xchar(tty, STOP_CHAR(tty));
984
985 if (tty->termios->c_cflag & CRTSCTS)
986 info->MCR &= ~SER_RTS;
987
988 local_irq_save(flags);
989 rtsdtr_ctrl(info->MCR);
990 local_irq_restore(flags);
991}
992
993static void rs_unthrottle(struct tty_struct * tty)
994{
Alan Coxc9f19e92009-01-02 13:47:26 +0000995 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned long flags;
997#ifdef SERIAL_DEBUG_THROTTLE
998 char buf[64];
999
1000 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1001 tty->ldisc.chars_in_buffer(tty));
1002#endif
1003
1004 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1005 return;
1006
1007 if (I_IXOFF(tty)) {
1008 if (info->x_char)
1009 info->x_char = 0;
1010 else
1011 rs_send_xchar(tty, START_CHAR(tty));
1012 }
1013 if (tty->termios->c_cflag & CRTSCTS)
1014 info->MCR |= SER_RTS;
1015 local_irq_save(flags);
1016 rtsdtr_ctrl(info->MCR);
1017 local_irq_restore(flags);
1018}
1019
1020/*
1021 * ------------------------------------------------------------
1022 * rs_ioctl() and friends
1023 * ------------------------------------------------------------
1024 */
1025
1026static int get_serial_info(struct async_struct * info,
Al Viroab14cae2006-01-12 01:06:30 -08001027 struct serial_struct __user * retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 struct serial_struct tmp;
1030 struct serial_state *state = info->state;
1031
1032 if (!retinfo)
1033 return -EFAULT;
1034 memset(&tmp, 0, sizeof(tmp));
Arnd Bergmannec79d602010-06-01 22:53:01 +02001035 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 tmp.type = state->type;
1037 tmp.line = state->line;
1038 tmp.port = state->port;
1039 tmp.irq = state->irq;
1040 tmp.flags = state->flags;
1041 tmp.xmit_fifo_size = state->xmit_fifo_size;
1042 tmp.baud_base = state->baud_base;
1043 tmp.close_delay = state->close_delay;
1044 tmp.closing_wait = state->closing_wait;
1045 tmp.custom_divisor = state->custom_divisor;
Arnd Bergmannec79d602010-06-01 22:53:01 +02001046 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1048 return -EFAULT;
1049 return 0;
1050}
1051
1052static int set_serial_info(struct async_struct * info,
Al Viroab14cae2006-01-12 01:06:30 -08001053 struct serial_struct __user * new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct serial_struct new_serial;
1056 struct serial_state old_state, *state;
1057 unsigned int change_irq,change_port;
1058 int retval = 0;
1059
1060 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1061 return -EFAULT;
Alan Coxe18ce492008-04-30 00:53:16 -07001062
Arnd Bergmannec79d602010-06-01 22:53:01 +02001063 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 state = info->state;
1065 old_state = *state;
1066
1067 change_irq = new_serial.irq != state->irq;
1068 change_port = (new_serial.port != state->port);
Alan Coxe18ce492008-04-30 00:53:16 -07001069 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001070 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (!serial_isroot()) {
1075 if ((new_serial.baud_base != state->baud_base) ||
1076 (new_serial.close_delay != state->close_delay) ||
1077 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1078 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1079 (state->flags & ~ASYNC_USR_MASK)))
1080 return -EPERM;
1081 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1082 (new_serial.flags & ASYNC_USR_MASK));
1083 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1084 (new_serial.flags & ASYNC_USR_MASK));
1085 state->custom_divisor = new_serial.custom_divisor;
1086 goto check_and_exit;
1087 }
1088
Alan Coxe18ce492008-04-30 00:53:16 -07001089 if (new_serial.baud_base < 9600) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001090 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /*
1095 * OK, past this point, all the error checking has been done.
1096 * At this point, we start making changes.....
1097 */
1098
1099 state->baud_base = new_serial.baud_base;
1100 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1101 (new_serial.flags & ASYNC_FLAGS));
1102 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1103 (info->flags & ASYNC_INTERNAL_FLAGS));
1104 state->custom_divisor = new_serial.custom_divisor;
1105 state->close_delay = new_serial.close_delay * HZ/100;
1106 state->closing_wait = new_serial.closing_wait * HZ/100;
1107 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1108
1109check_and_exit:
1110 if (info->flags & ASYNC_INITIALIZED) {
1111 if (((old_state.flags & ASYNC_SPD_MASK) !=
1112 (state->flags & ASYNC_SPD_MASK)) ||
1113 (old_state.custom_divisor != state->custom_divisor)) {
1114 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1115 info->tty->alt_speed = 57600;
1116 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1117 info->tty->alt_speed = 115200;
1118 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1119 info->tty->alt_speed = 230400;
1120 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1121 info->tty->alt_speed = 460800;
1122 change_speed(info, NULL);
1123 }
1124 } else
1125 retval = startup(info);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001126 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return retval;
1128}
1129
1130
1131/*
1132 * get_lsr_info - get line status register info
1133 *
1134 * Purpose: Let user call ioctl() to get info when the UART physically
1135 * is emptied. On bus types like RS485, the transmitter must
1136 * release the bus after transmitting. This must be done when
1137 * the transmit shift register is empty, not be done when the
1138 * transmit holding register is empty. This functionality
1139 * allows an RS485 driver to be written in user space.
1140 */
Al Viroab14cae2006-01-12 01:06:30 -08001141static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 unsigned char status;
1144 unsigned int result;
1145 unsigned long flags;
1146
1147 local_irq_save(flags);
1148 status = custom.serdatr;
1149 mb();
1150 local_irq_restore(flags);
1151 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1152 if (copy_to_user(value, &result, sizeof(int)))
1153 return -EFAULT;
1154 return 0;
1155}
1156
1157
Alan Cox60b33c12011-02-14 16:26:14 +00001158static int rs_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Alan Coxc9f19e92009-01-02 13:47:26 +00001160 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unsigned char control, status;
1162 unsigned long flags;
1163
1164 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1165 return -ENODEV;
1166 if (tty->flags & (1 << TTY_IO_ERROR))
1167 return -EIO;
1168
1169 control = info->MCR;
1170 local_irq_save(flags);
1171 status = ciab.pra;
1172 local_irq_restore(flags);
1173 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1174 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1175 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1176 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1177 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1178}
1179
Alan Cox20b9d172011-02-14 16:26:50 +00001180static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1181 unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Alan Coxc9f19e92009-01-02 13:47:26 +00001183 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 unsigned long flags;
1185
1186 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1187 return -ENODEV;
1188 if (tty->flags & (1 << TTY_IO_ERROR))
1189 return -EIO;
1190
1191 local_irq_save(flags);
1192 if (set & TIOCM_RTS)
1193 info->MCR |= SER_RTS;
1194 if (set & TIOCM_DTR)
1195 info->MCR |= SER_DTR;
1196 if (clear & TIOCM_RTS)
1197 info->MCR &= ~SER_RTS;
1198 if (clear & TIOCM_DTR)
1199 info->MCR &= ~SER_DTR;
1200 rtsdtr_ctrl(info->MCR);
1201 local_irq_restore(flags);
1202 return 0;
1203}
1204
1205/*
1206 * rs_break() --- routine which turns the break handling on or off
1207 */
Alan Cox9e98966c2008-07-22 11:18:03 +01001208static int rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Alan Coxc9f19e92009-01-02 13:47:26 +00001210 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 unsigned long flags;
1212
1213 if (serial_paranoia_check(info, tty->name, "rs_break"))
Geert Uytterhoeven970a8a52008-08-06 22:19:39 +02001214 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 local_irq_save(flags);
1217 if (break_state == -1)
1218 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1219 else
1220 custom.adkcon = AC_UARTBRK;
1221 mb();
1222 local_irq_restore(flags);
Alan Cox9e98966c2008-07-22 11:18:03 +01001223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Alan Cox05871022010-09-16 18:21:52 +01001226/*
1227 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1228 * Return: write counters to the user passed counter struct
1229 * NB: both 1->0 and 0->1 transitions are counted except for
1230 * RI where only 0->1 is counted.
1231 */
1232static int rs_get_icount(struct tty_struct *tty,
1233 struct serial_icounter_struct *icount)
1234{
1235 struct async_struct *info = tty->driver_data;
1236 struct async_icount cnow;
1237 unsigned long flags;
1238
1239 local_irq_save(flags);
1240 cnow = info->state->icount;
1241 local_irq_restore(flags);
1242 icount->cts = cnow.cts;
1243 icount->dsr = cnow.dsr;
1244 icount->rng = cnow.rng;
1245 icount->dcd = cnow.dcd;
1246 icount->rx = cnow.rx;
1247 icount->tx = cnow.tx;
1248 icount->frame = cnow.frame;
1249 icount->overrun = cnow.overrun;
1250 icount->parity = cnow.parity;
1251 icount->brk = cnow.brk;
1252 icount->buf_overrun = cnow.buf_overrun;
1253
1254 return 0;
1255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Alan Cox6caa76b2011-02-14 16:27:22 +00001257static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 unsigned int cmd, unsigned long arg)
1259{
Alan Coxc9f19e92009-01-02 13:47:26 +00001260 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 struct async_icount cprev, cnow; /* kernel counter temps */
Al Viroab14cae2006-01-12 01:06:30 -08001262 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 unsigned long flags;
1264
1265 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1266 return -ENODEV;
1267
1268 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1269 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1270 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1271 if (tty->flags & (1 << TTY_IO_ERROR))
1272 return -EIO;
1273 }
1274
1275 switch (cmd) {
1276 case TIOCGSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001277 return get_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 case TIOCSSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001279 return set_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 case TIOCSERCONFIG:
1281 return 0;
1282
1283 case TIOCSERGETLSR: /* Get line status register */
Al Viroab14cae2006-01-12 01:06:30 -08001284 return get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 case TIOCSERGSTRUCT:
Al Viroab14cae2006-01-12 01:06:30 -08001287 if (copy_to_user(argp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 info, sizeof(struct async_struct)))
1289 return -EFAULT;
1290 return 0;
1291
1292 /*
1293 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1294 * - mask passed in arg for lines of interest
1295 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1296 * Caller should use TIOCGICOUNT to see which one it was
1297 */
1298 case TIOCMIWAIT:
1299 local_irq_save(flags);
1300 /* note the counters on entry */
1301 cprev = info->state->icount;
1302 local_irq_restore(flags);
1303 while (1) {
1304 interruptible_sleep_on(&info->delta_msr_wait);
1305 /* see if a signal did it */
1306 if (signal_pending(current))
1307 return -ERESTARTSYS;
1308 local_irq_save(flags);
1309 cnow = info->state->icount; /* atomic copy */
1310 local_irq_restore(flags);
1311 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1312 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1313 return -EIO; /* no change => error */
1314 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1315 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1316 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1317 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1318 return 0;
1319 }
1320 cprev = cnow;
1321 }
1322 /* NOTREACHED */
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 case TIOCSERGWILD:
1325 case TIOCSERSWILD:
1326 /* "setserial -W" is called in Debian boot */
1327 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1328 return 0;
1329
1330 default:
1331 return -ENOIOCTLCMD;
1332 }
1333 return 0;
1334}
1335
Alan Cox606d0992006-12-08 02:38:45 -08001336static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Alan Coxc9f19e92009-01-02 13:47:26 +00001338 struct async_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 unsigned long flags;
1340 unsigned int cflag = tty->termios->c_cflag;
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 change_speed(info, old_termios);
1343
1344 /* Handle transition to B0 status */
1345 if ((old_termios->c_cflag & CBAUD) &&
1346 !(cflag & CBAUD)) {
1347 info->MCR &= ~(SER_DTR|SER_RTS);
1348 local_irq_save(flags);
1349 rtsdtr_ctrl(info->MCR);
1350 local_irq_restore(flags);
1351 }
1352
1353 /* Handle transition away from B0 status */
1354 if (!(old_termios->c_cflag & CBAUD) &&
1355 (cflag & CBAUD)) {
1356 info->MCR |= SER_DTR;
1357 if (!(tty->termios->c_cflag & CRTSCTS) ||
1358 !test_bit(TTY_THROTTLED, &tty->flags)) {
1359 info->MCR |= SER_RTS;
1360 }
1361 local_irq_save(flags);
1362 rtsdtr_ctrl(info->MCR);
1363 local_irq_restore(flags);
1364 }
1365
1366 /* Handle turning off CRTSCTS */
1367 if ((old_termios->c_cflag & CRTSCTS) &&
1368 !(tty->termios->c_cflag & CRTSCTS)) {
1369 tty->hw_stopped = 0;
1370 rs_start(tty);
1371 }
1372
1373#if 0
1374 /*
1375 * No need to wake up processes in open wait, since they
1376 * sample the CLOCAL flag once, and don't recheck it.
1377 * XXX It's not clear whether the current behavior is correct
1378 * or not. Hence, this may change.....
1379 */
1380 if (!(old_termios->c_cflag & CLOCAL) &&
1381 (tty->termios->c_cflag & CLOCAL))
1382 wake_up_interruptible(&info->open_wait);
1383#endif
1384}
1385
1386/*
1387 * ------------------------------------------------------------
1388 * rs_close()
1389 *
1390 * This routine is called when the serial port gets closed. First, we
1391 * wait for the last remaining data to be sent. Then, we unlink its
1392 * async structure from the interrupt chain if necessary, and we free
1393 * that IRQ if nothing is left in the chain.
1394 * ------------------------------------------------------------
1395 */
1396static void rs_close(struct tty_struct *tty, struct file * filp)
1397{
Alan Coxc9f19e92009-01-02 13:47:26 +00001398 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 struct serial_state *state;
1400 unsigned long flags;
1401
1402 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1403 return;
1404
1405 state = info->state;
1406
1407 local_irq_save(flags);
1408
1409 if (tty_hung_up_p(filp)) {
1410 DBG_CNT("before DEC-hung");
1411 local_irq_restore(flags);
1412 return;
1413 }
1414
1415#ifdef SERIAL_DEBUG_OPEN
1416 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1417#endif
1418 if ((tty->count == 1) && (state->count != 1)) {
1419 /*
1420 * Uh, oh. tty->count is 1, which means that the tty
1421 * structure will be freed. state->count should always
1422 * be one in these conditions. If it's greater than
1423 * one, we've got real problems, since it means the
1424 * serial port won't be shutdown.
1425 */
1426 printk("rs_close: bad serial port count; tty->count is 1, "
1427 "state->count is %d\n", state->count);
1428 state->count = 1;
1429 }
1430 if (--state->count < 0) {
1431 printk("rs_close: bad serial port count for ttys%d: %d\n",
1432 info->line, state->count);
1433 state->count = 0;
1434 }
1435 if (state->count) {
1436 DBG_CNT("before DEC-2");
1437 local_irq_restore(flags);
1438 return;
1439 }
1440 info->flags |= ASYNC_CLOSING;
1441 /*
1442 * Now we wait for the transmit buffer to clear; and we notify
1443 * the line discipline to only process XON/XOFF characters.
1444 */
1445 tty->closing = 1;
1446 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1447 tty_wait_until_sent(tty, info->closing_wait);
1448 /*
1449 * At this point we stop accepting input. To do this, we
1450 * disable the receive line status interrupts, and tell the
1451 * interrupt driver to stop checking the data ready bit in the
1452 * line status register.
1453 */
1454 info->read_status_mask &= ~UART_LSR_DR;
1455 if (info->flags & ASYNC_INITIALIZED) {
1456 /* disable receive interrupts */
1457 custom.intena = IF_RBF;
1458 mb();
1459 /* clear any pending receive interrupt */
1460 custom.intreq = IF_RBF;
1461 mb();
1462
1463 /*
1464 * Before we drop DTR, make sure the UART transmitter
1465 * has completely drained; this is especially
1466 * important if there is a transmit FIFO!
1467 */
1468 rs_wait_until_sent(tty, info->timeout);
1469 }
1470 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001471 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 tty_ldisc_flush(tty);
1474 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 info->tty = NULL;
1476 if (info->blocked_open) {
1477 if (info->close_delay) {
1478 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1479 }
1480 wake_up_interruptible(&info->open_wait);
1481 }
1482 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1483 wake_up_interruptible(&info->close_wait);
1484 local_irq_restore(flags);
1485}
1486
1487/*
1488 * rs_wait_until_sent() --- wait until the transmitter is empty
1489 */
1490static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1491{
Alan Coxc9f19e92009-01-02 13:47:26 +00001492 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 unsigned long orig_jiffies, char_time;
1494 int lsr;
1495
1496 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1497 return;
1498
1499 if (info->xmit_fifo_size == 0)
1500 return; /* Just in case.... */
1501
1502 orig_jiffies = jiffies;
Alan Cox978e5952008-04-30 00:53:59 -07001503
Arnd Bergmann20365212010-06-01 22:53:07 +02001504 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 * Set the check interval to be 1/5 of the estimated time to
1506 * send a single character, and make it at least 1. The check
1507 * interval should also be less than the timeout.
1508 *
1509 * Note: we have to use pretty tight timings here to satisfy
1510 * the NIST-PCTS.
1511 */
1512 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1513 char_time = char_time / 5;
1514 if (char_time == 0)
1515 char_time = 1;
1516 if (timeout)
1517 char_time = min_t(unsigned long, char_time, timeout);
1518 /*
1519 * If the transmitter hasn't cleared in twice the approximate
1520 * amount of time to send the entire FIFO, it probably won't
1521 * ever clear. This assumes the UART isn't doing flow
1522 * control, which is currently the case. Hence, if it ever
1523 * takes longer than info->timeout, this is probably due to a
1524 * UART bug of some kind. So, we clamp the timeout parameter at
1525 * 2*info->timeout.
1526 */
1527 if (!timeout || timeout > 2*info->timeout)
1528 timeout = 2*info->timeout;
1529#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1530 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1531 printk("jiff=%lu...", jiffies);
1532#endif
1533 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1534#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1535 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1536#endif
1537 msleep_interruptible(jiffies_to_msecs(char_time));
1538 if (signal_pending(current))
1539 break;
1540 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1541 break;
1542 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001543 __set_current_state(TASK_RUNNING);
Jiri Slabyeff4b0b2011-07-14 14:35:13 +02001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1546 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1547#endif
1548}
1549
1550/*
1551 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1552 */
1553static void rs_hangup(struct tty_struct *tty)
1554{
Alan Coxc9f19e92009-01-02 13:47:26 +00001555 struct async_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct serial_state *state = info->state;
1557
1558 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1559 return;
1560
1561 state = info->state;
1562
1563 rs_flush_buffer(tty);
1564 shutdown(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 state->count = 0;
1566 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1567 info->tty = NULL;
1568 wake_up_interruptible(&info->open_wait);
1569}
1570
1571/*
1572 * ------------------------------------------------------------
1573 * rs_open() and friends
1574 * ------------------------------------------------------------
1575 */
1576static int block_til_ready(struct tty_struct *tty, struct file * filp,
1577 struct async_struct *info)
1578{
1579#ifdef DECLARE_WAITQUEUE
1580 DECLARE_WAITQUEUE(wait, current);
1581#else
1582 struct wait_queue wait = { current, NULL };
1583#endif
1584 struct serial_state *state = info->state;
1585 int retval;
1586 int do_clocal = 0, extra_count = 0;
1587 unsigned long flags;
1588
1589 /*
1590 * If the device is in the middle of being closed, then block
1591 * until it's done, and then try again.
1592 */
1593 if (tty_hung_up_p(filp) ||
1594 (info->flags & ASYNC_CLOSING)) {
1595 if (info->flags & ASYNC_CLOSING)
1596 interruptible_sleep_on(&info->close_wait);
1597#ifdef SERIAL_DO_RESTART
1598 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1599 -EAGAIN : -ERESTARTSYS);
1600#else
1601 return -EAGAIN;
1602#endif
1603 }
1604
1605 /*
1606 * If non-blocking mode is set, or the port is not enabled,
1607 * then make the check up front and then exit.
1608 */
1609 if ((filp->f_flags & O_NONBLOCK) ||
1610 (tty->flags & (1 << TTY_IO_ERROR))) {
1611 info->flags |= ASYNC_NORMAL_ACTIVE;
1612 return 0;
1613 }
1614
1615 if (tty->termios->c_cflag & CLOCAL)
1616 do_clocal = 1;
1617
1618 /*
1619 * Block waiting for the carrier detect and the line to become
1620 * free (i.e., not in use by the callout). While we are in
1621 * this loop, state->count is dropped by one, so that
1622 * rs_close() knows when to free things. We restore it upon
1623 * exit, either normal or abnormal.
1624 */
1625 retval = 0;
1626 add_wait_queue(&info->open_wait, &wait);
1627#ifdef SERIAL_DEBUG_OPEN
1628 printk("block_til_ready before block: ttys%d, count = %d\n",
1629 state->line, state->count);
1630#endif
1631 local_irq_save(flags);
1632 if (!tty_hung_up_p(filp)) {
1633 extra_count = 1;
1634 state->count--;
1635 }
1636 local_irq_restore(flags);
1637 info->blocked_open++;
1638 while (1) {
1639 local_irq_save(flags);
1640 if (tty->termios->c_cflag & CBAUD)
1641 rtsdtr_ctrl(SER_DTR|SER_RTS);
1642 local_irq_restore(flags);
1643 set_current_state(TASK_INTERRUPTIBLE);
1644 if (tty_hung_up_p(filp) ||
1645 !(info->flags & ASYNC_INITIALIZED)) {
1646#ifdef SERIAL_DO_RESTART
1647 if (info->flags & ASYNC_HUP_NOTIFY)
1648 retval = -EAGAIN;
1649 else
1650 retval = -ERESTARTSYS;
1651#else
1652 retval = -EAGAIN;
1653#endif
1654 break;
1655 }
1656 if (!(info->flags & ASYNC_CLOSING) &&
1657 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1658 break;
1659 if (signal_pending(current)) {
1660 retval = -ERESTARTSYS;
1661 break;
1662 }
1663#ifdef SERIAL_DEBUG_OPEN
1664 printk("block_til_ready blocking: ttys%d, count = %d\n",
1665 info->line, state->count);
1666#endif
Arnd Bergmanne142a312010-06-01 22:53:10 +02001667 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001669 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001671 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 remove_wait_queue(&info->open_wait, &wait);
1673 if (extra_count)
1674 state->count++;
1675 info->blocked_open--;
1676#ifdef SERIAL_DEBUG_OPEN
1677 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1678 info->line, state->count);
1679#endif
1680 if (retval)
1681 return retval;
1682 info->flags |= ASYNC_NORMAL_ACTIVE;
1683 return 0;
1684}
1685
1686static int get_async_struct(int line, struct async_struct **ret_info)
1687{
1688 struct async_struct *info;
1689 struct serial_state *sstate;
1690
1691 sstate = rs_table + line;
1692 sstate->count++;
1693 if (sstate->info) {
1694 *ret_info = sstate->info;
1695 return 0;
1696 }
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001697 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (!info) {
1699 sstate->count--;
1700 return -ENOMEM;
1701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702#ifdef DECLARE_WAITQUEUE
1703 init_waitqueue_head(&info->open_wait);
1704 init_waitqueue_head(&info->close_wait);
1705 init_waitqueue_head(&info->delta_msr_wait);
1706#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 info->port = sstate->port;
1708 info->flags = sstate->flags;
1709 info->xmit_fifo_size = sstate->xmit_fifo_size;
1710 info->line = line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 info->state = sstate;
1712 if (sstate->info) {
1713 kfree(info);
1714 *ret_info = sstate->info;
1715 return 0;
1716 }
1717 *ret_info = sstate->info = info;
1718 return 0;
1719}
1720
1721/*
1722 * This routine is called whenever a serial port is opened. It
1723 * enables interrupts for a serial port, linking in its async structure into
1724 * the IRQ chain. It also performs the serial-specific
1725 * initialization for the tty structure.
1726 */
1727static int rs_open(struct tty_struct *tty, struct file * filp)
1728{
1729 struct async_struct *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001730 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Jiri Slaby410235f2012-03-05 14:52:01 +01001732 retval = get_async_struct(tty->index, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (retval) {
1734 return retval;
1735 }
1736 tty->driver_data = info;
1737 info->tty = tty;
1738 if (serial_paranoia_check(info, tty->name, "rs_open"))
1739 return -ENODEV;
1740
1741#ifdef SERIAL_DEBUG_OPEN
1742 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1743#endif
1744 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /*
1747 * If the port is the middle of closing, bail out now
1748 */
1749 if (tty_hung_up_p(filp) ||
1750 (info->flags & ASYNC_CLOSING)) {
1751 if (info->flags & ASYNC_CLOSING)
1752 interruptible_sleep_on(&info->close_wait);
1753#ifdef SERIAL_DO_RESTART
1754 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1755 -EAGAIN : -ERESTARTSYS);
1756#else
1757 return -EAGAIN;
1758#endif
1759 }
1760
1761 /*
1762 * Start up serial port
1763 */
1764 retval = startup(info);
1765 if (retval) {
1766 return retval;
1767 }
1768
1769 retval = block_til_ready(tty, filp, info);
1770 if (retval) {
1771#ifdef SERIAL_DEBUG_OPEN
1772 printk("rs_open returning after block_til_ready with %d\n",
1773 retval);
1774#endif
1775 return retval;
1776 }
1777
1778#ifdef SERIAL_DEBUG_OPEN
1779 printk("rs_open %s successful...", tty->name);
1780#endif
1781 return 0;
1782}
1783
1784/*
1785 * /proc fs routines....
1786 */
1787
Alexey Dobriyand5940272009-03-31 15:19:23 -07001788static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 struct async_struct *info = state->info, scr_info;
1791 char stat_buf[30], control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 unsigned long flags;
1793
Alexey Dobriyand5940272009-03-31 15:19:23 -07001794 seq_printf(m, "%d: uart:amiga_builtin",state->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /*
1797 * Figure out the current RS-232 lines
1798 */
1799 if (!info) {
1800 info = &scr_info; /* This is just for serial_{in,out} */
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 info->flags = state->flags;
1803 info->quot = 0;
1804 info->tty = NULL;
1805 }
1806 local_irq_save(flags);
1807 status = ciab.pra;
1808 control = info ? info->MCR : status;
1809 local_irq_restore(flags);
1810
1811 stat_buf[0] = 0;
1812 stat_buf[1] = 0;
1813 if(!(control & SER_RTS))
1814 strcat(stat_buf, "|RTS");
1815 if(!(status & SER_CTS))
1816 strcat(stat_buf, "|CTS");
1817 if(!(control & SER_DTR))
1818 strcat(stat_buf, "|DTR");
1819 if(!(status & SER_DSR))
1820 strcat(stat_buf, "|DSR");
1821 if(!(status & SER_DCD))
1822 strcat(stat_buf, "|CD");
1823
1824 if (info->quot) {
Alexey Dobriyand5940272009-03-31 15:19:23 -07001825 seq_printf(m, " baud:%d", state->baud_base / info->quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827
Alexey Dobriyand5940272009-03-31 15:19:23 -07001828 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 if (state->icount.frame)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001831 seq_printf(m, " fe:%d", state->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (state->icount.parity)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001834 seq_printf(m, " pe:%d", state->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 if (state->icount.brk)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001837 seq_printf(m, " brk:%d", state->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 if (state->icount.overrun)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001840 seq_printf(m, " oe:%d", state->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /*
1843 * Last thing is the RS-232 status lines
1844 */
Alexey Dobriyand5940272009-03-31 15:19:23 -07001845 seq_printf(m, " %s\n", stat_buf+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Alexey Dobriyand5940272009-03-31 15:19:23 -07001848static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Alexey Dobriyand5940272009-03-31 15:19:23 -07001850 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1851 line_info(m, &rs_table[0]);
1852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Alexey Dobriyand5940272009-03-31 15:19:23 -07001855static int rs_proc_open(struct inode *inode, struct file *file)
1856{
1857 return single_open(file, rs_proc_show, NULL);
1858}
1859
1860static const struct file_operations rs_proc_fops = {
1861 .owner = THIS_MODULE,
1862 .open = rs_proc_open,
1863 .read = seq_read,
1864 .llseek = seq_lseek,
1865 .release = single_release,
1866};
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868/*
1869 * ---------------------------------------------------------------------
1870 * rs_init() and friends
1871 *
1872 * rs_init() is called at boot-time to initialize the serial driver.
1873 * ---------------------------------------------------------------------
1874 */
1875
1876/*
1877 * This routine prints out the appropriate serial driver version
1878 * number, and identifies which options were configured into this
1879 * driver.
1880 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001881static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1884}
1885
1886
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001887static const struct tty_operations serial_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 .open = rs_open,
1889 .close = rs_close,
1890 .write = rs_write,
1891 .put_char = rs_put_char,
1892 .flush_chars = rs_flush_chars,
1893 .write_room = rs_write_room,
1894 .chars_in_buffer = rs_chars_in_buffer,
1895 .flush_buffer = rs_flush_buffer,
1896 .ioctl = rs_ioctl,
1897 .throttle = rs_throttle,
1898 .unthrottle = rs_unthrottle,
1899 .set_termios = rs_set_termios,
1900 .stop = rs_stop,
1901 .start = rs_start,
1902 .hangup = rs_hangup,
1903 .break_ctl = rs_break,
1904 .send_xchar = rs_send_xchar,
1905 .wait_until_sent = rs_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 .tiocmget = rs_tiocmget,
1907 .tiocmset = rs_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001908 .get_icount = rs_get_icount,
Alexey Dobriyand5940272009-03-31 15:19:23 -07001909 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910};
1911
1912/*
1913 * The serial driver boot-time initialization code!
1914 */
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001915static int __init amiga_serial_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 unsigned long flags;
1918 struct serial_state * state;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001919 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Jiri Slaby410235f2012-03-05 14:52:01 +01001921 serial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (!serial_driver)
1923 return -ENOMEM;
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 IRQ_ports = NULL;
1926
1927 show_serial_version();
1928
1929 /* Initialize the tty_driver structure */
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 serial_driver->driver_name = "amiserial";
1932 serial_driver->name = "ttyS";
1933 serial_driver->major = TTY_MAJOR;
1934 serial_driver->minor_start = 64;
1935 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1936 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1937 serial_driver->init_termios = tty_std_termios;
1938 serial_driver->init_termios.c_cflag =
1939 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1940 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1941 tty_set_operations(serial_driver, &serial_ops);
1942
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001943 error = tty_register_driver(serial_driver);
1944 if (error)
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001945 goto fail_put_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 state = rs_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 state->port = (int)&custom.serdatr; /* Just to give it a value */
1949 state->line = 0;
1950 state->custom_divisor = 0;
1951 state->close_delay = 5*HZ/10;
1952 state->closing_wait = 30*HZ;
1953 state->icount.cts = state->icount.dsr =
1954 state->icount.rng = state->icount.dcd = 0;
1955 state->icount.rx = state->icount.tx = 0;
1956 state->icount.frame = state->icount.parity = 0;
1957 state->icount.overrun = state->icount.brk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1960 state->line);
1961
1962 /* Hardware set up */
1963
1964 state->baud_base = amiga_colorclock;
1965 state->xmit_fifo_size = 1;
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* set ISRs, and then disable the rx interrupts */
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001968 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1969 if (error)
1970 goto fail_unregister;
1971
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001972 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001973 "serial RX", state);
1974 if (error)
1975 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Julia Lawallc70c0362010-04-06 14:34:46 -07001977 local_irq_save(flags);
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /* turn off Rx and Tx interrupts */
1980 custom.intena = IF_RBF | IF_TBE;
1981 mb();
1982
1983 /* clear any pending interrupt */
1984 custom.intreq = IF_RBF | IF_TBE;
1985 mb();
1986
1987 local_irq_restore(flags);
1988
1989 /*
1990 * set the appropriate directions for the modem control flags,
1991 * and clear RTS and DTR
1992 */
1993 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1994 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1995
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001996 platform_set_drvdata(pdev, state);
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 0;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001999
2000fail_free_irq:
2001 free_irq(IRQ_AMIGA_TBE, state);
2002fail_unregister:
2003 tty_unregister_driver(serial_driver);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01002004fail_put_tty_driver:
2005 put_tty_driver(serial_driver);
2006 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002009static int __exit amiga_serial_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 int error;
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002012 struct serial_state *state = platform_get_drvdata(pdev);
2013 struct async_struct *info = state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if ((error = tty_unregister_driver(serial_driver)))
2017 printk("SERIAL: failed to unregister serial driver (%d)\n",
2018 error);
2019 put_tty_driver(serial_driver);
2020
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002021 rs_table[0].info = NULL;
2022 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01002024 free_irq(IRQ_AMIGA_TBE, rs_table);
2025 free_irq(IRQ_AMIGA_RBF, rs_table);
2026
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002027 platform_set_drvdata(pdev, NULL);
2028
2029 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002032static struct platform_driver amiga_serial_driver = {
2033 .remove = __exit_p(amiga_serial_remove),
2034 .driver = {
2035 .name = "amiga-serial",
2036 .owner = THIS_MODULE,
2037 },
2038};
2039
2040static int __init amiga_serial_init(void)
2041{
2042 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
2043}
2044
2045module_init(amiga_serial_init);
2046
2047static void __exit amiga_serial_exit(void)
2048{
2049 platform_driver_unregister(&amiga_serial_driver);
2050}
2051
2052module_exit(amiga_serial_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01002055#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057/*
2058 * ------------------------------------------------------------
2059 * Serial console driver
2060 * ------------------------------------------------------------
2061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063static void amiga_serial_putc(char c)
2064{
2065 custom.serdat = (unsigned char)c | 0x100;
2066 while (!(custom.serdatr & 0x2000))
2067 barrier();
2068}
2069
2070/*
2071 * Print a string to the serial port trying not to disturb
2072 * any possible real use of the port...
2073 *
2074 * The console must be locked when we get here.
2075 */
2076static void serial_console_write(struct console *co, const char *s,
2077 unsigned count)
2078{
2079 unsigned short intena = custom.intenar;
2080
2081 custom.intena = IF_TBE;
2082
2083 while (count--) {
2084 if (*s == '\n')
2085 amiga_serial_putc('\r');
2086 amiga_serial_putc(*s++);
2087 }
2088
2089 custom.intena = IF_SETCLR | (intena & IF_TBE);
2090}
2091
2092static struct tty_driver *serial_console_device(struct console *c, int *index)
2093{
2094 *index = 0;
2095 return serial_driver;
2096}
2097
2098static struct console sercons = {
2099 .name = "ttyS",
2100 .write = serial_console_write,
2101 .device = serial_console_device,
2102 .flags = CON_PRINTBUFFER,
2103 .index = -1,
2104};
2105
2106/*
2107 * Register console.
2108 */
2109static int __init amiserial_console_init(void)
2110{
2111 register_console(&sercons);
2112 return 0;
2113}
2114console_initcall(amiserial_console_init);
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01002115
2116#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118MODULE_LICENSE("GPL");
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002119MODULE_ALIAS("platform:amiga-serial");