aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/tty')
-rw-r--r--drivers/tty/hvc/hvc_console.c11
-rw-r--r--drivers/tty/hvc/hvc_console.h3
-rw-r--r--drivers/tty/hvc/hvc_iucv.c64
-rw-r--r--drivers/tty/n_gsm.c29
-rw-r--r--drivers/tty/n_tty.c1401
-rw-r--r--drivers/tty/pty.c18
-rw-r--r--drivers/tty/serial/8250/8250_core.c2
-rw-r--r--drivers/tty/serial/8250/8250_em.c27
-rw-r--r--drivers/tty/serial/8250/8250_pci.c15
-rw-r--r--drivers/tty/serial/8250/Kconfig2
-rw-r--r--drivers/tty/serial/Kconfig26
-rw-r--r--drivers/tty/serial/Makefile1
-rw-r--r--drivers/tty/serial/altera_jtaguart.c3
-rw-r--r--drivers/tty/serial/altera_uart.c2
-rw-r--r--drivers/tty/serial/amba-pl010.c2
-rw-r--r--drivers/tty/serial/amba-pl011.c14
-rw-r--r--drivers/tty/serial/ar933x_uart.c3
-rw-r--r--drivers/tty/serial/arc_uart.c33
-rw-r--r--drivers/tty/serial/atmel_serial.c857
-rw-r--r--drivers/tty/serial/bcm63xx_uart.c1
-rw-r--r--drivers/tty/serial/bfin_sport_uart.c7
-rw-r--r--drivers/tty/serial/bfin_uart.c15
-rw-r--r--drivers/tty/serial/clps711x.c11
-rw-r--r--drivers/tty/serial/efm32-uart.c25
-rw-r--r--drivers/tty/serial/fsl_lpuart.c7
-rw-r--r--drivers/tty/serial/ifx6x60.c2
-rw-r--r--drivers/tty/serial/imx.c517
-rw-r--r--drivers/tty/serial/max3100.c2
-rw-r--r--drivers/tty/serial/max310x.c991
-rw-r--r--drivers/tty/serial/mcf.c3
-rw-r--r--drivers/tty/serial/mpsc.c4
-rw-r--r--drivers/tty/serial/msm_serial.c184
-rw-r--r--drivers/tty/serial/msm_serial.h19
-rw-r--r--drivers/tty/serial/msm_serial_hs.c2
-rw-r--r--drivers/tty/serial/mxs-auart.c8
-rw-r--r--drivers/tty/serial/netx-serial.c2
-rw-r--r--drivers/tty/serial/omap-serial.c47
-rw-r--r--drivers/tty/serial/pch_uart.c82
-rw-r--r--drivers/tty/serial/pmac_zilog.c1
-rw-r--r--drivers/tty/serial/pnx8xxx_uart.c2
-rw-r--r--drivers/tty/serial/pxa.c2
-rw-r--r--drivers/tty/serial/sa1100.c2
-rw-r--r--drivers/tty/serial/samsung.c4
-rw-r--r--drivers/tty/serial/sc26xx.c2
-rw-r--r--drivers/tty/serial/sccnxp.c339
-rw-r--r--drivers/tty/serial/serial_core.c4
-rw-r--r--drivers/tty/serial/serial_txx9.c2
-rw-r--r--drivers/tty/serial/sh-sci.c4
-rw-r--r--drivers/tty/serial/sirfsoc_uart.c3
-rw-r--r--drivers/tty/serial/st-asc.c939
-rw-r--r--drivers/tty/serial/vr41xx_siu.c2
-rw-r--r--drivers/tty/serial/vt8500_serial.c1
-rw-r--r--drivers/tty/synclink.c130
-rw-r--r--drivers/tty/tty_buffer.c417
-rw-r--r--drivers/tty/tty_io.c33
-rw-r--r--drivers/tty/tty_ioctl.c90
-rw-r--r--drivers/tty/tty_ldisc.c461
-rw-r--r--drivers/tty/vt/keyboard.c21
-rw-r--r--drivers/tty/vt/selection.c8
-rw-r--r--drivers/tty/vt/vt.c4
60 files changed, 4521 insertions, 2392 deletions
diff --git a/drivers/tty/hvc/hvc_console.c b/drivers/tty/hvc/hvc_console.c
index eb255e807c06..9eba119bcdd3 100644
--- a/drivers/tty/hvc/hvc_console.c
+++ b/drivers/tty/hvc/hvc_console.c
@@ -361,7 +361,12 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
361 tty->driver_data = NULL; 361 tty->driver_data = NULL;
362 tty_port_put(&hp->port); 362 tty_port_put(&hp->port);
363 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc); 363 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
364 } 364 } else
365 /* We are ready... raise DTR/RTS */
366 if (C_BAUD(tty))
367 if (hp->ops->dtr_rts)
368 hp->ops->dtr_rts(hp, 1);
369
365 /* Force wakeup of the polling thread */ 370 /* Force wakeup of the polling thread */
366 hvc_kick(); 371 hvc_kick();
367 372
@@ -393,6 +398,10 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
393 /* We are done with the tty pointer now. */ 398 /* We are done with the tty pointer now. */
394 tty_port_tty_set(&hp->port, NULL); 399 tty_port_tty_set(&hp->port, NULL);
395 400
401 if (C_HUPCL(tty))
402 if (hp->ops->dtr_rts)
403 hp->ops->dtr_rts(hp, 0);
404
396 if (hp->ops->notifier_del) 405 if (hp->ops->notifier_del)
397 hp->ops->notifier_del(hp, hp->data); 406 hp->ops->notifier_del(hp, hp->data);
398 407
diff --git a/drivers/tty/hvc/hvc_console.h b/drivers/tty/hvc/hvc_console.h
index 674d23cb919a..913101980827 100644
--- a/drivers/tty/hvc/hvc_console.h
+++ b/drivers/tty/hvc/hvc_console.h
@@ -75,6 +75,9 @@ struct hv_ops {
75 /* tiocmget/set implementation */ 75 /* tiocmget/set implementation */
76 int (*tiocmget)(struct hvc_struct *hp); 76 int (*tiocmget)(struct hvc_struct *hp);
77 int (*tiocmset)(struct hvc_struct *hp, unsigned int set, unsigned int clear); 77 int (*tiocmset)(struct hvc_struct *hp, unsigned int set, unsigned int clear);
78
79 /* Callbacks to handle tty ports */
80 void (*dtr_rts)(struct hvc_struct *hp, int raise);
78}; 81};
79 82
80/* Register a vterm and a slot index for use as a console (console_init) */ 83/* Register a vterm and a slot index for use as a console (console_init) */
diff --git a/drivers/tty/hvc/hvc_iucv.c b/drivers/tty/hvc/hvc_iucv.c
index 9d47f50c2755..fd17a9b804b8 100644
--- a/drivers/tty/hvc/hvc_iucv.c
+++ b/drivers/tty/hvc/hvc_iucv.c
@@ -656,21 +656,64 @@ static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
656} 656}
657 657
658/** 658/**
659 * hvc_iucv_dtr_rts() - HVC notifier for handling DTR/RTS
660 * @hp: Pointer the HVC device (struct hvc_struct)
661 * @raise: Non-zero to raise or zero to lower DTR/RTS lines
662 *
663 * This routine notifies the HVC back-end to raise or lower DTR/RTS
664 * lines. Raising DTR/RTS is ignored. Lowering DTR/RTS indicates to
665 * drop the IUCV connection (similar to hang up the modem).
666 */
667static void hvc_iucv_dtr_rts(struct hvc_struct *hp, int raise)
668{
669 struct hvc_iucv_private *priv;
670 struct iucv_path *path;
671
672 /* Raising the DTR/RTS is ignored as IUCV connections can be
673 * established at any times.
674 */
675 if (raise)
676 return;
677
678 priv = hvc_iucv_get_private(hp->vtermno);
679 if (!priv)
680 return;
681
682 /* Lowering the DTR/RTS lines disconnects an established IUCV
683 * connection.
684 */
685 flush_sndbuf_sync(priv);
686
687 spin_lock_bh(&priv->lock);
688 path = priv->path; /* save reference to IUCV path */
689 priv->path = NULL;
690 priv->iucv_state = IUCV_DISCONN;
691 spin_unlock_bh(&priv->lock);
692
693 /* Sever IUCV path outside of priv->lock due to lock ordering of:
694 * priv->lock <--> iucv_table_lock */
695 if (path) {
696 iucv_path_sever(path, NULL);
697 iucv_path_free(path);
698 }
699}
700
701/**
659 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time. 702 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
660 * @hp: Pointer to the HVC device (struct hvc_struct) 703 * @hp: Pointer to the HVC device (struct hvc_struct)
661 * @id: Additional data (originally passed to hvc_alloc): 704 * @id: Additional data (originally passed to hvc_alloc):
662 * the index of an struct hvc_iucv_private instance. 705 * the index of an struct hvc_iucv_private instance.
663 * 706 *
664 * This routine notifies the HVC back-end that the last tty device fd has been 707 * This routine notifies the HVC back-end that the last tty device fd has been
665 * closed. The function calls hvc_iucv_cleanup() to clean up the struct 708 * closed. The function cleans up tty resources. The clean-up of the IUCV
666 * hvc_iucv_private instance. 709 * connection is done in hvc_iucv_dtr_rts() and depends on the HUPCL termios
710 * control setting.
667 * 711 *
668 * Locking: struct hvc_iucv_private->lock 712 * Locking: struct hvc_iucv_private->lock
669 */ 713 */
670static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id) 714static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
671{ 715{
672 struct hvc_iucv_private *priv; 716 struct hvc_iucv_private *priv;
673 struct iucv_path *path;
674 717
675 priv = hvc_iucv_get_private(id); 718 priv = hvc_iucv_get_private(id);
676 if (!priv) 719 if (!priv)
@@ -679,17 +722,11 @@ static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
679 flush_sndbuf_sync(priv); 722 flush_sndbuf_sync(priv);
680 723
681 spin_lock_bh(&priv->lock); 724 spin_lock_bh(&priv->lock);
682 path = priv->path; /* save reference to IUCV path */ 725 destroy_tty_buffer_list(&priv->tty_outqueue);
683 priv->path = NULL; 726 destroy_tty_buffer_list(&priv->tty_inqueue);
684 hvc_iucv_cleanup(priv); 727 priv->tty_state = TTY_CLOSED;
728 priv->sndbuf_len = 0;
685 spin_unlock_bh(&priv->lock); 729 spin_unlock_bh(&priv->lock);
686
687 /* sever IUCV path outside of priv->lock due to lock ordering of:
688 * priv->lock <--> iucv_table_lock */
689 if (path) {
690 iucv_path_sever(path, NULL);
691 iucv_path_free(path);
692 }
693} 730}
694 731
695/** 732/**
@@ -931,6 +968,7 @@ static const struct hv_ops hvc_iucv_ops = {
931 .notifier_add = hvc_iucv_notifier_add, 968 .notifier_add = hvc_iucv_notifier_add,
932 .notifier_del = hvc_iucv_notifier_del, 969 .notifier_del = hvc_iucv_notifier_del,
933 .notifier_hangup = hvc_iucv_notifier_hangup, 970 .notifier_hangup = hvc_iucv_notifier_hangup,
971 .dtr_rts = hvc_iucv_dtr_rts,
934}; 972};
935 973
936/* Suspend / resume device operations */ 974/* Suspend / resume device operations */
diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 642239015b46..c0f76da55304 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -807,7 +807,7 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
807 int h = dlci->adaption - 1; 807 int h = dlci->adaption - 1;
808 808
809 total_size = 0; 809 total_size = 0;
810 while(1) { 810 while (1) {
811 len = kfifo_len(dlci->fifo); 811 len = kfifo_len(dlci->fifo);
812 if (len == 0) 812 if (len == 0)
813 return total_size; 813 return total_size;
@@ -827,8 +827,8 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
827 switch (dlci->adaption) { 827 switch (dlci->adaption) {
828 case 1: /* Unstructured */ 828 case 1: /* Unstructured */
829 break; 829 break;
830 case 2: /* Unstructed with modem bits. Always one byte as we never 830 case 2: /* Unstructed with modem bits.
831 send inline break data */ 831 Always one byte as we never send inline break data */
832 *dp++ = gsm_encode_modem(dlci); 832 *dp++ = gsm_encode_modem(dlci);
833 break; 833 break;
834 } 834 }
@@ -968,7 +968,7 @@ static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
968 unsigned long flags; 968 unsigned long flags;
969 int sweep; 969 int sweep;
970 970
971 if (dlci->constipated) 971 if (dlci->constipated)
972 return; 972 return;
973 973
974 spin_lock_irqsave(&dlci->gsm->tx_lock, flags); 974 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
@@ -981,7 +981,7 @@ static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
981 gsm_dlci_data_output(dlci->gsm, dlci); 981 gsm_dlci_data_output(dlci->gsm, dlci);
982 } 982 }
983 if (sweep) 983 if (sweep)
984 gsm_dlci_data_sweep(dlci->gsm); 984 gsm_dlci_data_sweep(dlci->gsm);
985 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags); 985 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
986} 986}
987 987
@@ -1138,7 +1138,7 @@ static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen)
1138static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen) 1138static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen)
1139{ 1139{
1140 struct tty_port *port; 1140 struct tty_port *port;
1141 unsigned int addr = 0 ; 1141 unsigned int addr = 0;
1142 u8 bits; 1142 u8 bits;
1143 int len = clen; 1143 int len = clen;
1144 u8 *dp = data; 1144 u8 *dp = data;
@@ -1740,10 +1740,11 @@ static void gsm_queue(struct gsm_mux *gsm)
1740 1740
1741 if ((gsm->control & ~PF) == UI) 1741 if ((gsm->control & ~PF) == UI)
1742 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len); 1742 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len);
1743 if (gsm->encoding == 0){ 1743 if (gsm->encoding == 0) {
1744 /* WARNING: gsm->received_fcs is used for gsm->encoding = 0 only. 1744 /* WARNING: gsm->received_fcs is used for
1745 In this case it contain the last piece of data 1745 gsm->encoding = 0 only.
1746 required to generate final CRC */ 1746 In this case it contain the last piece of data
1747 required to generate final CRC */
1747 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs); 1748 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs);
1748 } 1749 }
1749 if (gsm->fcs != GOOD_FCS) { 1750 if (gsm->fcs != GOOD_FCS) {
@@ -2904,9 +2905,11 @@ static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
2904 gsm = gsm_mux[mux]; 2905 gsm = gsm_mux[mux];
2905 if (gsm->dead) 2906 if (gsm->dead)
2906 return -EL2HLT; 2907 return -EL2HLT;
2907 /* If DLCI 0 is not yet fully open return an error. This is ok from a locking 2908 /* If DLCI 0 is not yet fully open return an error.
2908 perspective as we don't have to worry about this if DLCI0 is lost */ 2909 This is ok from a locking
2909 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN) 2910 perspective as we don't have to worry about this
2911 if DLCI0 is lost */
2912 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN)
2910 return -EL2NSYNC; 2913 return -EL2NSYNC;
2911 dlci = gsm->dlci[line]; 2914 dlci = gsm->dlci[line];
2912 if (dlci == NULL) { 2915 if (dlci == NULL) {
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 4bf0fc0843d7..dd8ae0cad1d5 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -50,6 +50,7 @@
50#include <linux/uaccess.h> 50#include <linux/uaccess.h>
51#include <linux/module.h> 51#include <linux/module.h>
52#include <linux/ratelimit.h> 52#include <linux/ratelimit.h>
53#include <linux/vmalloc.h>
53 54
54 55
55/* number of characters left in xmit buffer before select has we have room */ 56/* number of characters left in xmit buffer before select has we have room */
@@ -74,37 +75,81 @@
74#define ECHO_OP_SET_CANON_COL 0x81 75#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82 76#define ECHO_OP_ERASE_TAB 0x82
76 77
78#define ECHO_COMMIT_WATERMARK 256
79#define ECHO_BLOCK 256
80#define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
81
82
83#undef N_TTY_TRACE
84#ifdef N_TTY_TRACE
85# define n_tty_trace(f, args...) trace_printk(f, ##args)
86#else
87# define n_tty_trace(f, args...)
88#endif
89
77struct n_tty_data { 90struct n_tty_data {
78 unsigned int column; 91 /* producer-published */
92 size_t read_head;
93 size_t canon_head;
94 size_t echo_head;
95 size_t echo_commit;
96 DECLARE_BITMAP(char_map, 256);
97
98 /* private to n_tty_receive_overrun (single-threaded) */
79 unsigned long overrun_time; 99 unsigned long overrun_time;
80 int num_overrun; 100 int num_overrun;
81 101
102 /* non-atomic */
103 bool no_room;
104
105 /* must hold exclusive termios_rwsem to reset these */
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1; 106 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
84 107
85 DECLARE_BITMAP(process_char_map, 256); 108 /* shared by producer and consumer */
109 char read_buf[N_TTY_BUF_SIZE];
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE); 110 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
111 unsigned char echo_buf[N_TTY_BUF_SIZE];
87 112
88 char *read_buf;
89 int read_head;
90 int read_tail;
91 int read_cnt;
92 int minimum_to_wake; 113 int minimum_to_wake;
93 114
94 unsigned char *echo_buf; 115 /* consumer-published */
95 unsigned int echo_pos; 116 size_t read_tail;
96 unsigned int echo_cnt; 117 size_t line_start;
97 118
98 int canon_data; 119 /* protected by output lock */
99 unsigned long canon_head; 120 unsigned int column;
100 unsigned int canon_column; 121 unsigned int canon_column;
122 size_t echo_tail;
101 123
102 struct mutex atomic_read_lock; 124 struct mutex atomic_read_lock;
103 struct mutex output_lock; 125 struct mutex output_lock;
104 struct mutex echo_lock;
105 raw_spinlock_t read_lock;
106}; 126};
107 127
128static inline size_t read_cnt(struct n_tty_data *ldata)
129{
130 return ldata->read_head - ldata->read_tail;
131}
132
133static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
134{
135 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
136}
137
138static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
139{
140 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
141}
142
143static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
144{
145 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
146}
147
148static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
149{
150 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
151}
152
108static inline int tty_put_user(struct tty_struct *tty, unsigned char x, 153static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
109 unsigned char __user *ptr) 154 unsigned char __user *ptr)
110{ 155{
@@ -114,33 +159,18 @@ static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
114 return put_user(x, ptr); 159 return put_user(x, ptr);
115} 160}
116 161
117/** 162static int receive_room(struct tty_struct *tty)
118 * n_tty_set_room - receive space
119 * @tty: terminal
120 *
121 * Updates tty->receive_room to reflect the currently available space
122 * in the input buffer, and re-schedules the flip buffer work if space
123 * just became available.
124 *
125 * Locks: Concurrent update is protected with read_lock
126 */
127
128static int set_room(struct tty_struct *tty)
129{ 163{
130 struct n_tty_data *ldata = tty->disc_data; 164 struct n_tty_data *ldata = tty->disc_data;
131 int left; 165 int left;
132 int old_left;
133 unsigned long flags;
134
135 raw_spin_lock_irqsave(&ldata->read_lock, flags);
136 166
137 if (I_PARMRK(tty)) { 167 if (I_PARMRK(tty)) {
138 /* Multiply read_cnt by 3, since each byte might take up to 168 /* Multiply read_cnt by 3, since each byte might take up to
139 * three times as many spaces when PARMRK is set (depending on 169 * three times as many spaces when PARMRK is set (depending on
140 * its flags, e.g. parity error). */ 170 * its flags, e.g. parity error). */
141 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1; 171 left = N_TTY_BUF_SIZE - read_cnt(ldata) * 3 - 1;
142 } else 172 } else
143 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1; 173 left = N_TTY_BUF_SIZE - read_cnt(ldata) - 1;
144 174
145 /* 175 /*
146 * If we are doing input canonicalization, and there are no 176 * If we are doing input canonicalization, and there are no
@@ -149,19 +179,31 @@ static int set_room(struct tty_struct *tty)
149 * characters will be beeped. 179 * characters will be beeped.
150 */ 180 */
151 if (left <= 0) 181 if (left <= 0)
152 left = ldata->icanon && !ldata->canon_data; 182 left = ldata->icanon && ldata->canon_head == ldata->read_tail;
153 old_left = tty->receive_room;
154 tty->receive_room = left;
155
156 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
157 183
158 return left && !old_left; 184 return left;
159} 185}
160 186
187/**
188 * n_tty_set_room - receive space
189 * @tty: terminal
190 *
191 * Re-schedules the flip buffer work if space just became available.
192 *
193 * Caller holds exclusive termios_rwsem
194 * or
195 * n_tty_read()/consumer path:
196 * holds non-exclusive termios_rwsem
197 */
198
161static void n_tty_set_room(struct tty_struct *tty) 199static void n_tty_set_room(struct tty_struct *tty)
162{ 200{
201 struct n_tty_data *ldata = tty->disc_data;
202
163 /* Did this open up the receive buffer? We may need to flip */ 203 /* Did this open up the receive buffer? We may need to flip */
164 if (set_room(tty)) { 204 if (unlikely(ldata->no_room) && receive_room(tty)) {
205 ldata->no_room = 0;
206
165 WARN_RATELIMIT(tty->port->itty == NULL, 207 WARN_RATELIMIT(tty->port->itty == NULL,
166 "scheduling with invalid itty\n"); 208 "scheduling with invalid itty\n");
167 /* see if ldisc has been killed - if so, this means that 209 /* see if ldisc has been killed - if so, this means that
@@ -170,17 +212,93 @@ static void n_tty_set_room(struct tty_struct *tty)
170 */ 212 */
171 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags), 213 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
172 "scheduling buffer work for halted ldisc\n"); 214 "scheduling buffer work for halted ldisc\n");
173 schedule_work(&tty->port->buf.work); 215 queue_work(system_unbound_wq, &tty->port->buf.work);
174 } 216 }
175} 217}
176 218
177static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata) 219static ssize_t chars_in_buffer(struct tty_struct *tty)
178{ 220{
179 if (ldata->read_cnt < N_TTY_BUF_SIZE) { 221 struct n_tty_data *ldata = tty->disc_data;
180 ldata->read_buf[ldata->read_head] = c; 222 ssize_t n = 0;
181 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1); 223
182 ldata->read_cnt++; 224 if (!ldata->icanon)
225 n = read_cnt(ldata);
226 else
227 n = ldata->canon_head - ldata->read_tail;
228 return n;
229}
230
231/**
232 * n_tty_write_wakeup - asynchronous I/O notifier
233 * @tty: tty device
234 *
235 * Required for the ptys, serial driver etc. since processes
236 * that attach themselves to the master and rely on ASYNC
237 * IO must be woken up
238 */
239
240static void n_tty_write_wakeup(struct tty_struct *tty)
241{
242 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
243 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
244}
245
246static void n_tty_check_throttle(struct tty_struct *tty)
247{
248 if (tty->driver->type == TTY_DRIVER_TYPE_PTY)
249 return;
250 /*
251 * Check the remaining room for the input canonicalization
252 * mode. We don't want to throttle the driver if we're in
253 * canonical mode and don't have a newline yet!
254 */
255 while (1) {
256 int throttled;
257 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
258 if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
259 break;
260 throttled = tty_throttle_safe(tty);
261 if (!throttled)
262 break;
263 }
264 __tty_set_flow_change(tty, 0);
265}
266
267static void n_tty_check_unthrottle(struct tty_struct *tty)
268{
269 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
270 tty->link->ldisc->ops->write_wakeup == n_tty_write_wakeup) {
271 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
272 return;
273 if (!tty->count)
274 return;
275 n_tty_set_room(tty);
276 n_tty_write_wakeup(tty->link);
277 wake_up_interruptible_poll(&tty->link->write_wait, POLLOUT);
278 return;
279 }
280
281 /* If there is enough space in the read buffer now, let the
282 * low-level driver know. We use chars_in_buffer() to
283 * check the buffer, as it now knows about canonical mode.
284 * Otherwise, if the driver is throttled and the line is
285 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
286 * we won't get any more characters.
287 */
288
289 while (1) {
290 int unthrottled;
291 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
292 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
293 break;
294 if (!tty->count)
295 break;
296 n_tty_set_room(tty);
297 unthrottled = tty_unthrottle_safe(tty);
298 if (!unthrottled)
299 break;
183 } 300 }
301 __tty_set_flow_change(tty, 0);
184} 302}
185 303
186/** 304/**
@@ -188,21 +306,19 @@ static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
188 * @c: character 306 * @c: character
189 * @ldata: n_tty data 307 * @ldata: n_tty data
190 * 308 *
191 * Add a character to the tty read_buf queue. This is done under the 309 * Add a character to the tty read_buf queue.
192 * read_lock to serialize character addition and also to protect us 310 *
193 * against parallel reads or flushes 311 * n_tty_receive_buf()/producer path:
312 * caller holds non-exclusive termios_rwsem
313 * modifies read_head
314 *
315 * read_head is only considered 'published' if canonical mode is
316 * not active.
194 */ 317 */
195 318
196static void put_tty_queue(unsigned char c, struct n_tty_data *ldata) 319static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
197{ 320{
198 unsigned long flags; 321 *read_buf_addr(ldata, ldata->read_head++) = c;
199 /*
200 * The problem of stomping on the buffers ends here.
201 * Why didn't anyone see this one coming? --AJK
202 */
203 raw_spin_lock_irqsave(&ldata->read_lock, flags);
204 put_tty_queue_nolock(c, ldata);
205 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
206} 322}
207 323
208/** 324/**
@@ -212,22 +328,17 @@ static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
212 * Reset the read buffer counters and clear the flags. 328 * Reset the read buffer counters and clear the flags.
213 * Called from n_tty_open() and n_tty_flush_buffer(). 329 * Called from n_tty_open() and n_tty_flush_buffer().
214 * 330 *
215 * Locking: tty_read_lock for read fields. 331 * Locking: caller holds exclusive termios_rwsem
332 * (or locking is not required)
216 */ 333 */
217 334
218static void reset_buffer_flags(struct n_tty_data *ldata) 335static void reset_buffer_flags(struct n_tty_data *ldata)
219{ 336{
220 unsigned long flags; 337 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
338 ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
339 ldata->line_start = 0;
221 340
222 raw_spin_lock_irqsave(&ldata->read_lock, flags); 341 ldata->erasing = 0;
223 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
224 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
225
226 mutex_lock(&ldata->echo_lock);
227 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
228 mutex_unlock(&ldata->echo_lock);
229
230 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
231 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 342 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
232} 343}
233 344
@@ -251,16 +362,21 @@ static void n_tty_packet_mode_flush(struct tty_struct *tty)
251 * buffer flushed (eg at hangup) or when the N_TTY line discipline 362 * buffer flushed (eg at hangup) or when the N_TTY line discipline
252 * internally has to clean the pending queue (for example some signals). 363 * internally has to clean the pending queue (for example some signals).
253 * 364 *
254 * Locking: ctrl_lock, read_lock. 365 * Holds termios_rwsem to exclude producer/consumer while
366 * buffer indices are reset.
367 *
368 * Locking: ctrl_lock, exclusive termios_rwsem
255 */ 369 */
256 370
257static void n_tty_flush_buffer(struct tty_struct *tty) 371static void n_tty_flush_buffer(struct tty_struct *tty)
258{ 372{
373 down_write(&tty->termios_rwsem);
259 reset_buffer_flags(tty->disc_data); 374 reset_buffer_flags(tty->disc_data);
260 n_tty_set_room(tty); 375 n_tty_set_room(tty);
261 376
262 if (tty->link) 377 if (tty->link)
263 n_tty_packet_mode_flush(tty); 378 n_tty_packet_mode_flush(tty);
379 up_write(&tty->termios_rwsem);
264} 380}
265 381
266/** 382/**
@@ -270,24 +386,18 @@ static void n_tty_flush_buffer(struct tty_struct *tty)
270 * Report the number of characters buffered to be delivered to user 386 * Report the number of characters buffered to be delivered to user
271 * at this instant in time. 387 * at this instant in time.
272 * 388 *
273 * Locking: read_lock 389 * Locking: exclusive termios_rwsem
274 */ 390 */
275 391
276static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) 392static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
277{ 393{
278 struct n_tty_data *ldata = tty->disc_data; 394 ssize_t n;
279 unsigned long flags;
280 ssize_t n = 0;
281 395
282 raw_spin_lock_irqsave(&ldata->read_lock, flags); 396 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
283 if (!ldata->icanon) { 397
284 n = ldata->read_cnt; 398 down_write(&tty->termios_rwsem);
285 } else if (ldata->canon_data) { 399 n = chars_in_buffer(tty);
286 n = (ldata->canon_head > ldata->read_tail) ? 400 up_write(&tty->termios_rwsem);
287 ldata->canon_head - ldata->read_tail :
288 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
289 }
290 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
291 return n; 401 return n;
292} 402}
293 403
@@ -532,33 +642,23 @@ break_out:
532 * are prioritized. Also, when control characters are echoed with a 642 * are prioritized. Also, when control characters are echoed with a
533 * prefixed "^", the pair is treated atomically and thus not separated. 643 * prefixed "^", the pair is treated atomically and thus not separated.
534 * 644 *
535 * Locking: output_lock to protect column state and space left, 645 * Locking: callers must hold output_lock
536 * echo_lock to protect the echo buffer
537 */ 646 */
538 647
539static void process_echoes(struct tty_struct *tty) 648static size_t __process_echoes(struct tty_struct *tty)
540{ 649{
541 struct n_tty_data *ldata = tty->disc_data; 650 struct n_tty_data *ldata = tty->disc_data;
542 int space, nr; 651 int space, old_space;
652 size_t tail;
543 unsigned char c; 653 unsigned char c;
544 unsigned char *cp, *buf_end;
545 654
546 if (!ldata->echo_cnt) 655 old_space = space = tty_write_room(tty);
547 return;
548
549 mutex_lock(&ldata->output_lock);
550 mutex_lock(&ldata->echo_lock);
551 656
552 space = tty_write_room(tty); 657 tail = ldata->echo_tail;
553 658 while (ldata->echo_commit != tail) {
554 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE; 659 c = echo_buf(ldata, tail);
555 cp = ldata->echo_buf + ldata->echo_pos;
556 nr = ldata->echo_cnt;
557 while (nr > 0) {
558 c = *cp;
559 if (c == ECHO_OP_START) { 660 if (c == ECHO_OP_START) {
560 unsigned char op; 661 unsigned char op;
561 unsigned char *opp;
562 int no_space_left = 0; 662 int no_space_left = 0;
563 663
564 /* 664 /*
@@ -566,18 +666,13 @@ static void process_echoes(struct tty_struct *tty)
566 * operation, get the next byte, which is either the 666 * operation, get the next byte, which is either the
567 * op code or a control character value. 667 * op code or a control character value.
568 */ 668 */
569 opp = cp + 1; 669 op = echo_buf(ldata, tail + 1);
570 if (opp == buf_end)
571 opp -= N_TTY_BUF_SIZE;
572 op = *opp;
573 670
574 switch (op) { 671 switch (op) {
575 unsigned int num_chars, num_bs; 672 unsigned int num_chars, num_bs;
576 673
577 case ECHO_OP_ERASE_TAB: 674 case ECHO_OP_ERASE_TAB:
578 if (++opp == buf_end) 675 num_chars = echo_buf(ldata, tail + 2);
579 opp -= N_TTY_BUF_SIZE;
580 num_chars = *opp;
581 676
582 /* 677 /*
583 * Determine how many columns to go back 678 * Determine how many columns to go back
@@ -603,21 +698,18 @@ static void process_echoes(struct tty_struct *tty)
603 if (ldata->column > 0) 698 if (ldata->column > 0)
604 ldata->column--; 699 ldata->column--;
605 } 700 }
606 cp += 3; 701 tail += 3;
607 nr -= 3;
608 break; 702 break;
609 703
610 case ECHO_OP_SET_CANON_COL: 704 case ECHO_OP_SET_CANON_COL:
611 ldata->canon_column = ldata->column; 705 ldata->canon_column = ldata->column;
612 cp += 2; 706 tail += 2;
613 nr -= 2;
614 break; 707 break;
615 708
616 case ECHO_OP_MOVE_BACK_COL: 709 case ECHO_OP_MOVE_BACK_COL:
617 if (ldata->column > 0) 710 if (ldata->column > 0)
618 ldata->column--; 711 ldata->column--;
619 cp += 2; 712 tail += 2;
620 nr -= 2;
621 break; 713 break;
622 714
623 case ECHO_OP_START: 715 case ECHO_OP_START:
@@ -629,8 +721,7 @@ static void process_echoes(struct tty_struct *tty)
629 tty_put_char(tty, ECHO_OP_START); 721 tty_put_char(tty, ECHO_OP_START);
630 ldata->column++; 722 ldata->column++;
631 space--; 723 space--;
632 cp += 2; 724 tail += 2;
633 nr -= 2;
634 break; 725 break;
635 726
636 default: 727 default:
@@ -651,8 +742,7 @@ static void process_echoes(struct tty_struct *tty)
651 tty_put_char(tty, op ^ 0100); 742 tty_put_char(tty, op ^ 0100);
652 ldata->column += 2; 743 ldata->column += 2;
653 space -= 2; 744 space -= 2;
654 cp += 2; 745 tail += 2;
655 nr -= 2;
656 } 746 }
657 747
658 if (no_space_left) 748 if (no_space_left)
@@ -669,80 +759,92 @@ static void process_echoes(struct tty_struct *tty)
669 tty_put_char(tty, c); 759 tty_put_char(tty, c);
670 space -= 1; 760 space -= 1;
671 } 761 }
672 cp += 1; 762 tail += 1;
673 nr -= 1;
674 } 763 }
675
676 /* When end of circular buffer reached, wrap around */
677 if (cp >= buf_end)
678 cp -= N_TTY_BUF_SIZE;
679 } 764 }
680 765
681 if (nr == 0) { 766 /* If the echo buffer is nearly full (so that the possibility exists
682 ldata->echo_pos = 0; 767 * of echo overrun before the next commit), then discard enough
683 ldata->echo_cnt = 0; 768 * data at the tail to prevent a subsequent overrun */
684 ldata->echo_overrun = 0; 769 while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
685 } else { 770 if (echo_buf(ldata, tail == ECHO_OP_START)) {
686 int num_processed = ldata->echo_cnt - nr; 771 if (echo_buf(ldata, tail) == ECHO_OP_ERASE_TAB)
687 ldata->echo_pos += num_processed; 772 tail += 3;
688 ldata->echo_pos &= N_TTY_BUF_SIZE - 1; 773 else
689 ldata->echo_cnt = nr; 774 tail += 2;
690 if (num_processed > 0) 775 } else
691 ldata->echo_overrun = 0; 776 tail++;
692 } 777 }
693 778
694 mutex_unlock(&ldata->echo_lock); 779 ldata->echo_tail = tail;
780 return old_space - space;
781}
782
783static void commit_echoes(struct tty_struct *tty)
784{
785 struct n_tty_data *ldata = tty->disc_data;
786 size_t nr, old, echoed;
787 size_t head;
788
789 head = ldata->echo_head;
790 old = ldata->echo_commit - ldata->echo_tail;
791
792 /* Process committed echoes if the accumulated # of bytes
793 * is over the threshold (and try again each time another
794 * block is accumulated) */
795 nr = head - ldata->echo_tail;
796 if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
797 return;
798
799 mutex_lock(&ldata->output_lock);
800 ldata->echo_commit = head;
801 echoed = __process_echoes(tty);
802 mutex_unlock(&ldata->output_lock);
803
804 if (echoed && tty->ops->flush_chars)
805 tty->ops->flush_chars(tty);
806}
807
808static void process_echoes(struct tty_struct *tty)
809{
810 struct n_tty_data *ldata = tty->disc_data;
811 size_t echoed;
812
813 if (!L_ECHO(tty) || ldata->echo_commit == ldata->echo_tail)
814 return;
815
816 mutex_lock(&ldata->output_lock);
817 echoed = __process_echoes(tty);
695 mutex_unlock(&ldata->output_lock); 818 mutex_unlock(&ldata->output_lock);
696 819
697 if (tty->ops->flush_chars) 820 if (echoed && tty->ops->flush_chars)
698 tty->ops->flush_chars(tty); 821 tty->ops->flush_chars(tty);
699} 822}
700 823
824static void flush_echoes(struct tty_struct *tty)
825{
826 struct n_tty_data *ldata = tty->disc_data;
827
828 if (!L_ECHO(tty) || ldata->echo_commit == ldata->echo_head)
829 return;
830
831 mutex_lock(&ldata->output_lock);
832 ldata->echo_commit = ldata->echo_head;
833 __process_echoes(tty);
834 mutex_unlock(&ldata->output_lock);
835}
836
701/** 837/**
702 * add_echo_byte - add a byte to the echo buffer 838 * add_echo_byte - add a byte to the echo buffer
703 * @c: unicode byte to echo 839 * @c: unicode byte to echo
704 * @ldata: n_tty data 840 * @ldata: n_tty data
705 * 841 *
706 * Add a character or operation byte to the echo buffer. 842 * Add a character or operation byte to the echo buffer.
707 *
708 * Should be called under the echo lock to protect the echo buffer.
709 */ 843 */
710 844
711static void add_echo_byte(unsigned char c, struct n_tty_data *ldata) 845static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
712{ 846{
713 int new_byte_pos; 847 *echo_buf_addr(ldata, ldata->echo_head++) = c;
714
715 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
716 /* Circular buffer is already at capacity */
717 new_byte_pos = ldata->echo_pos;
718
719 /*
720 * Since the buffer start position needs to be advanced,
721 * be sure to step by a whole operation byte group.
722 */
723 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
724 if (ldata->echo_buf[(ldata->echo_pos + 1) &
725 (N_TTY_BUF_SIZE - 1)] ==
726 ECHO_OP_ERASE_TAB) {
727 ldata->echo_pos += 3;
728 ldata->echo_cnt -= 2;
729 } else {
730 ldata->echo_pos += 2;
731 ldata->echo_cnt -= 1;
732 }
733 } else {
734 ldata->echo_pos++;
735 }
736 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
737
738 ldata->echo_overrun = 1;
739 } else {
740 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
741 new_byte_pos &= N_TTY_BUF_SIZE - 1;
742 ldata->echo_cnt++;
743 }
744
745 ldata->echo_buf[new_byte_pos] = c;
746} 848}
747 849
748/** 850/**
@@ -750,16 +852,12 @@ static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
750 * @ldata: n_tty data 852 * @ldata: n_tty data
751 * 853 *
752 * Add an operation to the echo buffer to move back one column. 854 * Add an operation to the echo buffer to move back one column.
753 *
754 * Locking: echo_lock to protect the echo buffer
755 */ 855 */
756 856
757static void echo_move_back_col(struct n_tty_data *ldata) 857static void echo_move_back_col(struct n_tty_data *ldata)
758{ 858{
759 mutex_lock(&ldata->echo_lock);
760 add_echo_byte(ECHO_OP_START, ldata); 859 add_echo_byte(ECHO_OP_START, ldata);
761 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata); 860 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
762 mutex_unlock(&ldata->echo_lock);
763} 861}
764 862
765/** 863/**
@@ -768,16 +866,12 @@ static void echo_move_back_col(struct n_tty_data *ldata)
768 * 866 *
769 * Add an operation to the echo buffer to set the canon column 867 * Add an operation to the echo buffer to set the canon column
770 * to the current column. 868 * to the current column.
771 *
772 * Locking: echo_lock to protect the echo buffer
773 */ 869 */
774 870
775static void echo_set_canon_col(struct n_tty_data *ldata) 871static void echo_set_canon_col(struct n_tty_data *ldata)
776{ 872{
777 mutex_lock(&ldata->echo_lock);
778 add_echo_byte(ECHO_OP_START, ldata); 873 add_echo_byte(ECHO_OP_START, ldata);
779 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata); 874 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
780 mutex_unlock(&ldata->echo_lock);
781} 875}
782 876
783/** 877/**
@@ -793,15 +887,11 @@ static void echo_set_canon_col(struct n_tty_data *ldata)
793 * of input. This information will be used later, along with 887 * of input. This information will be used later, along with
794 * canon column (if applicable), to go back the correct number 888 * canon column (if applicable), to go back the correct number
795 * of columns. 889 * of columns.
796 *
797 * Locking: echo_lock to protect the echo buffer
798 */ 890 */
799 891
800static void echo_erase_tab(unsigned int num_chars, int after_tab, 892static void echo_erase_tab(unsigned int num_chars, int after_tab,
801 struct n_tty_data *ldata) 893 struct n_tty_data *ldata)
802{ 894{
803 mutex_lock(&ldata->echo_lock);
804
805 add_echo_byte(ECHO_OP_START, ldata); 895 add_echo_byte(ECHO_OP_START, ldata);
806 add_echo_byte(ECHO_OP_ERASE_TAB, ldata); 896 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
807 897
@@ -813,8 +903,6 @@ static void echo_erase_tab(unsigned int num_chars, int after_tab,
813 num_chars |= 0x80; 903 num_chars |= 0x80;
814 904
815 add_echo_byte(num_chars, ldata); 905 add_echo_byte(num_chars, ldata);
816
817 mutex_unlock(&ldata->echo_lock);
818} 906}
819 907
820/** 908/**
@@ -826,20 +914,16 @@ static void echo_erase_tab(unsigned int num_chars, int after_tab,
826 * L_ECHO(tty) is true. Called from the driver receive_buf path. 914 * L_ECHO(tty) is true. Called from the driver receive_buf path.
827 * 915 *
828 * This variant does not treat control characters specially. 916 * This variant does not treat control characters specially.
829 *
830 * Locking: echo_lock to protect the echo buffer
831 */ 917 */
832 918
833static void echo_char_raw(unsigned char c, struct n_tty_data *ldata) 919static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
834{ 920{
835 mutex_lock(&ldata->echo_lock);
836 if (c == ECHO_OP_START) { 921 if (c == ECHO_OP_START) {
837 add_echo_byte(ECHO_OP_START, ldata); 922 add_echo_byte(ECHO_OP_START, ldata);
838 add_echo_byte(ECHO_OP_START, ldata); 923 add_echo_byte(ECHO_OP_START, ldata);
839 } else { 924 } else {
840 add_echo_byte(c, ldata); 925 add_echo_byte(c, ldata);
841 } 926 }
842 mutex_unlock(&ldata->echo_lock);
843} 927}
844 928
845/** 929/**
@@ -852,16 +936,12 @@ static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
852 * 936 *
853 * This variant tags control characters to be echoed as "^X" 937 * This variant tags control characters to be echoed as "^X"
854 * (where X is the letter representing the control char). 938 * (where X is the letter representing the control char).
855 *
856 * Locking: echo_lock to protect the echo buffer
857 */ 939 */
858 940
859static void echo_char(unsigned char c, struct tty_struct *tty) 941static void echo_char(unsigned char c, struct tty_struct *tty)
860{ 942{
861 struct n_tty_data *ldata = tty->disc_data; 943 struct n_tty_data *ldata = tty->disc_data;
862 944
863 mutex_lock(&ldata->echo_lock);
864
865 if (c == ECHO_OP_START) { 945 if (c == ECHO_OP_START) {
866 add_echo_byte(ECHO_OP_START, ldata); 946 add_echo_byte(ECHO_OP_START, ldata);
867 add_echo_byte(ECHO_OP_START, ldata); 947 add_echo_byte(ECHO_OP_START, ldata);
@@ -870,8 +950,6 @@ static void echo_char(unsigned char c, struct tty_struct *tty)
870 add_echo_byte(ECHO_OP_START, ldata); 950 add_echo_byte(ECHO_OP_START, ldata);
871 add_echo_byte(c, ldata); 951 add_echo_byte(c, ldata);
872 } 952 }
873
874 mutex_unlock(&ldata->echo_lock);
875} 953}
876 954
877/** 955/**
@@ -896,17 +974,22 @@ static inline void finish_erasing(struct n_tty_data *ldata)
896 * present in the stream from the driver layer. Handles the complexities 974 * present in the stream from the driver layer. Handles the complexities
897 * of UTF-8 multibyte symbols. 975 * of UTF-8 multibyte symbols.
898 * 976 *
899 * Locking: read_lock for tty buffers 977 * n_tty_receive_buf()/producer path:
978 * caller holds non-exclusive termios_rwsem
979 * modifies read_head
980 *
981 * Modifying the read_head is not considered a publish in this context
982 * because canonical mode is active -- only canon_head publishes
900 */ 983 */
901 984
902static void eraser(unsigned char c, struct tty_struct *tty) 985static void eraser(unsigned char c, struct tty_struct *tty)
903{ 986{
904 struct n_tty_data *ldata = tty->disc_data; 987 struct n_tty_data *ldata = tty->disc_data;
905 enum { ERASE, WERASE, KILL } kill_type; 988 enum { ERASE, WERASE, KILL } kill_type;
906 int head, seen_alnums, cnt; 989 size_t head;
907 unsigned long flags; 990 size_t cnt;
991 int seen_alnums;
908 992
909 /* FIXME: locking needed ? */
910 if (ldata->read_head == ldata->canon_head) { 993 if (ldata->read_head == ldata->canon_head) {
911 /* process_output('\a', tty); */ /* what do you think? */ 994 /* process_output('\a', tty); */ /* what do you think? */
912 return; 995 return;
@@ -917,19 +1000,11 @@ static void eraser(unsigned char c, struct tty_struct *tty)
917 kill_type = WERASE; 1000 kill_type = WERASE;
918 else { 1001 else {
919 if (!L_ECHO(tty)) { 1002 if (!L_ECHO(tty)) {
920 raw_spin_lock_irqsave(&ldata->read_lock, flags);
921 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
922 (N_TTY_BUF_SIZE - 1));
923 ldata->read_head = ldata->canon_head; 1003 ldata->read_head = ldata->canon_head;
924 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
925 return; 1004 return;
926 } 1005 }
927 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { 1006 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
928 raw_spin_lock_irqsave(&ldata->read_lock, flags);
929 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
930 (N_TTY_BUF_SIZE - 1));
931 ldata->read_head = ldata->canon_head; 1007 ldata->read_head = ldata->canon_head;
932 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
933 finish_erasing(ldata); 1008 finish_erasing(ldata);
934 echo_char(KILL_CHAR(tty), tty); 1009 echo_char(KILL_CHAR(tty), tty);
935 /* Add a newline if ECHOK is on and ECHOKE is off. */ 1010 /* Add a newline if ECHOK is on and ECHOKE is off. */
@@ -941,14 +1016,13 @@ static void eraser(unsigned char c, struct tty_struct *tty)
941 } 1016 }
942 1017
943 seen_alnums = 0; 1018 seen_alnums = 0;
944 /* FIXME: Locking ?? */
945 while (ldata->read_head != ldata->canon_head) { 1019 while (ldata->read_head != ldata->canon_head) {
946 head = ldata->read_head; 1020 head = ldata->read_head;
947 1021
948 /* erase a single possibly multibyte character */ 1022 /* erase a single possibly multibyte character */
949 do { 1023 do {
950 head = (head - 1) & (N_TTY_BUF_SIZE-1); 1024 head--;
951 c = ldata->read_buf[head]; 1025 c = read_buf(ldata, head);
952 } while (is_continuation(c, tty) && head != ldata->canon_head); 1026 } while (is_continuation(c, tty) && head != ldata->canon_head);
953 1027
954 /* do not partially erase */ 1028 /* do not partially erase */
@@ -962,11 +1036,8 @@ static void eraser(unsigned char c, struct tty_struct *tty)
962 else if (seen_alnums) 1036 else if (seen_alnums)
963 break; 1037 break;
964 } 1038 }
965 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1); 1039 cnt = ldata->read_head - head;
966 raw_spin_lock_irqsave(&ldata->read_lock, flags);
967 ldata->read_head = head; 1040 ldata->read_head = head;
968 ldata->read_cnt -= cnt;
969 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
970 if (L_ECHO(tty)) { 1041 if (L_ECHO(tty)) {
971 if (L_ECHOPRT(tty)) { 1042 if (L_ECHOPRT(tty)) {
972 if (!ldata->erasing) { 1043 if (!ldata->erasing) {
@@ -976,9 +1047,8 @@ static void eraser(unsigned char c, struct tty_struct *tty)
976 /* if cnt > 1, output a multi-byte character */ 1047 /* if cnt > 1, output a multi-byte character */
977 echo_char(c, tty); 1048 echo_char(c, tty);
978 while (--cnt > 0) { 1049 while (--cnt > 0) {
979 head = (head+1) & (N_TTY_BUF_SIZE-1); 1050 head++;
980 echo_char_raw(ldata->read_buf[head], 1051 echo_char_raw(read_buf(ldata, head), ldata);
981 ldata);
982 echo_move_back_col(ldata); 1052 echo_move_back_col(ldata);
983 } 1053 }
984 } else if (kill_type == ERASE && !L_ECHOE(tty)) { 1054 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
@@ -986,7 +1056,7 @@ static void eraser(unsigned char c, struct tty_struct *tty)
986 } else if (c == '\t') { 1056 } else if (c == '\t') {
987 unsigned int num_chars = 0; 1057 unsigned int num_chars = 0;
988 int after_tab = 0; 1058 int after_tab = 0;
989 unsigned long tail = ldata->read_head; 1059 size_t tail = ldata->read_head;
990 1060
991 /* 1061 /*
992 * Count the columns used for characters 1062 * Count the columns used for characters
@@ -996,8 +1066,8 @@ static void eraser(unsigned char c, struct tty_struct *tty)
996 * number of columns. 1066 * number of columns.
997 */ 1067 */
998 while (tail != ldata->canon_head) { 1068 while (tail != ldata->canon_head) {
999 tail = (tail-1) & (N_TTY_BUF_SIZE-1); 1069 tail--;
1000 c = ldata->read_buf[tail]; 1070 c = read_buf(ldata, tail);
1001 if (c == '\t') { 1071 if (c == '\t') {
1002 after_tab = 1; 1072 after_tab = 1;
1003 break; 1073 break;
@@ -1040,7 +1110,7 @@ static void eraser(unsigned char c, struct tty_struct *tty)
1040 * Locking: ctrl_lock 1110 * Locking: ctrl_lock
1041 */ 1111 */
1042 1112
1043static inline void isig(int sig, struct tty_struct *tty) 1113static void isig(int sig, struct tty_struct *tty)
1044{ 1114{
1045 struct pid *tty_pgrp = tty_get_pgrp(tty); 1115 struct pid *tty_pgrp = tty_get_pgrp(tty);
1046 if (tty_pgrp) { 1116 if (tty_pgrp) {
@@ -1056,10 +1126,14 @@ static inline void isig(int sig, struct tty_struct *tty)
1056 * An RS232 break event has been hit in the incoming bitstream. This 1126 * An RS232 break event has been hit in the incoming bitstream. This
1057 * can cause a variety of events depending upon the termios settings. 1127 * can cause a variety of events depending upon the termios settings.
1058 * 1128 *
1059 * Called from the receive_buf path so single threaded. 1129 * n_tty_receive_buf()/producer path:
1130 * caller holds non-exclusive termios_rwsem
1131 * publishes read_head via put_tty_queue()
1132 *
1133 * Note: may get exclusive termios_rwsem if flushing input buffer
1060 */ 1134 */
1061 1135
1062static inline void n_tty_receive_break(struct tty_struct *tty) 1136static void n_tty_receive_break(struct tty_struct *tty)
1063{ 1137{
1064 struct n_tty_data *ldata = tty->disc_data; 1138 struct n_tty_data *ldata = tty->disc_data;
1065 1139
@@ -1068,8 +1142,11 @@ static inline void n_tty_receive_break(struct tty_struct *tty)
1068 if (I_BRKINT(tty)) { 1142 if (I_BRKINT(tty)) {
1069 isig(SIGINT, tty); 1143 isig(SIGINT, tty);
1070 if (!L_NOFLSH(tty)) { 1144 if (!L_NOFLSH(tty)) {
1145 /* flushing needs exclusive termios_rwsem */
1146 up_read(&tty->termios_rwsem);
1071 n_tty_flush_buffer(tty); 1147 n_tty_flush_buffer(tty);
1072 tty_driver_flush_buffer(tty); 1148 tty_driver_flush_buffer(tty);
1149 down_read(&tty->termios_rwsem);
1073 } 1150 }
1074 return; 1151 return;
1075 } 1152 }
@@ -1094,7 +1171,7 @@ static inline void n_tty_receive_break(struct tty_struct *tty)
1094 * private. 1171 * private.
1095 */ 1172 */
1096 1173
1097static inline void n_tty_receive_overrun(struct tty_struct *tty) 1174static void n_tty_receive_overrun(struct tty_struct *tty)
1098{ 1175{
1099 struct n_tty_data *ldata = tty->disc_data; 1176 struct n_tty_data *ldata = tty->disc_data;
1100 char buf[64]; 1177 char buf[64];
@@ -1116,10 +1193,13 @@ static inline void n_tty_receive_overrun(struct tty_struct *tty)
1116 * @c: character 1193 * @c: character
1117 * 1194 *
1118 * Process a parity error and queue the right data to indicate 1195 * Process a parity error and queue the right data to indicate
1119 * the error case if necessary. Locking as per n_tty_receive_buf. 1196 * the error case if necessary.
1197 *
1198 * n_tty_receive_buf()/producer path:
1199 * caller holds non-exclusive termios_rwsem
1200 * publishes read_head via put_tty_queue()
1120 */ 1201 */
1121static inline void n_tty_receive_parity_error(struct tty_struct *tty, 1202static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
1122 unsigned char c)
1123{ 1203{
1124 struct n_tty_data *ldata = tty->disc_data; 1204 struct n_tty_data *ldata = tty->disc_data;
1125 1205
@@ -1136,6 +1216,26 @@ static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1136 wake_up_interruptible(&tty->read_wait); 1216 wake_up_interruptible(&tty->read_wait);
1137} 1217}
1138 1218
1219static void
1220n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1221{
1222 if (!L_NOFLSH(tty)) {
1223 /* flushing needs exclusive termios_rwsem */
1224 up_read(&tty->termios_rwsem);
1225 n_tty_flush_buffer(tty);
1226 tty_driver_flush_buffer(tty);
1227 down_read(&tty->termios_rwsem);
1228 }
1229 if (I_IXON(tty))
1230 start_tty(tty);
1231 if (L_ECHO(tty)) {
1232 echo_char(c, tty);
1233 commit_echoes(tty);
1234 }
1235 isig(signal, tty);
1236 return;
1237}
1238
1139/** 1239/**
1140 * n_tty_receive_char - perform processing 1240 * n_tty_receive_char - perform processing
1141 * @tty: terminal device 1241 * @tty: terminal device
@@ -1144,117 +1244,54 @@ static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1144 * Process an individual character of input received from the driver. 1244 * Process an individual character of input received from the driver.
1145 * This is serialized with respect to itself by the rules for the 1245 * This is serialized with respect to itself by the rules for the
1146 * driver above. 1246 * driver above.
1247 *
1248 * n_tty_receive_buf()/producer path:
1249 * caller holds non-exclusive termios_rwsem
1250 * publishes canon_head if canonical mode is active
1251 * otherwise, publishes read_head via put_tty_queue()
1252 *
1253 * Returns 1 if LNEXT was received, else returns 0
1147 */ 1254 */
1148 1255
1149static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) 1256static int
1257n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
1150{ 1258{
1151 struct n_tty_data *ldata = tty->disc_data; 1259 struct n_tty_data *ldata = tty->disc_data;
1152 unsigned long flags;
1153 int parmrk; 1260 int parmrk;
1154 1261
1155 if (ldata->raw) {
1156 put_tty_queue(c, ldata);
1157 return;
1158 }
1159
1160 if (I_ISTRIP(tty))
1161 c &= 0x7f;
1162 if (I_IUCLC(tty) && L_IEXTEN(tty))
1163 c = tolower(c);
1164
1165 if (L_EXTPROC(tty)) {
1166 put_tty_queue(c, ldata);
1167 return;
1168 }
1169
1170 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1171 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1172 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1173 start_tty(tty);
1174 process_echoes(tty);
1175 }
1176
1177 if (tty->closing) {
1178 if (I_IXON(tty)) {
1179 if (c == START_CHAR(tty)) {
1180 start_tty(tty);
1181 process_echoes(tty);
1182 } else if (c == STOP_CHAR(tty))
1183 stop_tty(tty);
1184 }
1185 return;
1186 }
1187
1188 /*
1189 * If the previous character was LNEXT, or we know that this
1190 * character is not one of the characters that we'll have to
1191 * handle specially, do shortcut processing to speed things
1192 * up.
1193 */
1194 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
1195 ldata->lnext = 0;
1196 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1197 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1198 /* beep if no space */
1199 if (L_ECHO(tty))
1200 process_output('\a', tty);
1201 return;
1202 }
1203 if (L_ECHO(tty)) {
1204 finish_erasing(ldata);
1205 /* Record the column of first canon char. */
1206 if (ldata->canon_head == ldata->read_head)
1207 echo_set_canon_col(ldata);
1208 echo_char(c, tty);
1209 process_echoes(tty);
1210 }
1211 if (parmrk)
1212 put_tty_queue(c, ldata);
1213 put_tty_queue(c, ldata);
1214 return;
1215 }
1216
1217 if (I_IXON(tty)) { 1262 if (I_IXON(tty)) {
1218 if (c == START_CHAR(tty)) { 1263 if (c == START_CHAR(tty)) {
1219 start_tty(tty); 1264 start_tty(tty);
1220 process_echoes(tty); 1265 commit_echoes(tty);
1221 return; 1266 return 0;
1222 } 1267 }
1223 if (c == STOP_CHAR(tty)) { 1268 if (c == STOP_CHAR(tty)) {
1224 stop_tty(tty); 1269 stop_tty(tty);
1225 return; 1270 return 0;
1226 } 1271 }
1227 } 1272 }
1228 1273
1229 if (L_ISIG(tty)) { 1274 if (L_ISIG(tty)) {
1230 int signal; 1275 if (c == INTR_CHAR(tty)) {
1231 signal = SIGINT; 1276 n_tty_receive_signal_char(tty, SIGINT, c);
1232 if (c == INTR_CHAR(tty)) 1277 return 0;
1233 goto send_signal; 1278 } else if (c == QUIT_CHAR(tty)) {
1234 signal = SIGQUIT; 1279 n_tty_receive_signal_char(tty, SIGQUIT, c);
1235 if (c == QUIT_CHAR(tty)) 1280 return 0;
1236 goto send_signal; 1281 } else if (c == SUSP_CHAR(tty)) {
1237 signal = SIGTSTP; 1282 n_tty_receive_signal_char(tty, SIGTSTP, c);
1238 if (c == SUSP_CHAR(tty)) { 1283 return 0;
1239send_signal:
1240 if (!L_NOFLSH(tty)) {
1241 n_tty_flush_buffer(tty);
1242 tty_driver_flush_buffer(tty);
1243 }
1244 if (I_IXON(tty))
1245 start_tty(tty);
1246 if (L_ECHO(tty)) {
1247 echo_char(c, tty);
1248 process_echoes(tty);
1249 }
1250 isig(signal, tty);
1251 return;
1252 } 1284 }
1253 } 1285 }
1254 1286
1287 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1288 start_tty(tty);
1289 process_echoes(tty);
1290 }
1291
1255 if (c == '\r') { 1292 if (c == '\r') {
1256 if (I_IGNCR(tty)) 1293 if (I_IGNCR(tty))
1257 return; 1294 return 0;
1258 if (I_ICRNL(tty)) 1295 if (I_ICRNL(tty))
1259 c = '\n'; 1296 c = '\n';
1260 } else if (c == '\n' && I_INLCR(tty)) 1297 } else if (c == '\n' && I_INLCR(tty))
@@ -1264,8 +1301,8 @@ send_signal:
1264 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || 1301 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1265 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { 1302 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1266 eraser(c, tty); 1303 eraser(c, tty);
1267 process_echoes(tty); 1304 commit_echoes(tty);
1268 return; 1305 return 0;
1269 } 1306 }
1270 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { 1307 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1271 ldata->lnext = 1; 1308 ldata->lnext = 1;
@@ -1274,42 +1311,32 @@ send_signal:
1274 if (L_ECHOCTL(tty)) { 1311 if (L_ECHOCTL(tty)) {
1275 echo_char_raw('^', ldata); 1312 echo_char_raw('^', ldata);
1276 echo_char_raw('\b', ldata); 1313 echo_char_raw('\b', ldata);
1277 process_echoes(tty); 1314 commit_echoes(tty);
1278 } 1315 }
1279 } 1316 }
1280 return; 1317 return 1;
1281 } 1318 }
1282 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && 1319 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
1283 L_IEXTEN(tty)) { 1320 size_t tail = ldata->canon_head;
1284 unsigned long tail = ldata->canon_head;
1285 1321
1286 finish_erasing(ldata); 1322 finish_erasing(ldata);
1287 echo_char(c, tty); 1323 echo_char(c, tty);
1288 echo_char_raw('\n', ldata); 1324 echo_char_raw('\n', ldata);
1289 while (tail != ldata->read_head) { 1325 while (tail != ldata->read_head) {
1290 echo_char(ldata->read_buf[tail], tty); 1326 echo_char(read_buf(ldata, tail), tty);
1291 tail = (tail+1) & (N_TTY_BUF_SIZE-1); 1327 tail++;
1292 } 1328 }
1293 process_echoes(tty); 1329 commit_echoes(tty);
1294 return; 1330 return 0;
1295 } 1331 }
1296 if (c == '\n') { 1332 if (c == '\n') {
1297 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
1298 if (L_ECHO(tty))
1299 process_output('\a', tty);
1300 return;
1301 }
1302 if (L_ECHO(tty) || L_ECHONL(tty)) { 1333 if (L_ECHO(tty) || L_ECHONL(tty)) {
1303 echo_char_raw('\n', ldata); 1334 echo_char_raw('\n', ldata);
1304 process_echoes(tty); 1335 commit_echoes(tty);
1305 } 1336 }
1306 goto handle_newline; 1337 goto handle_newline;
1307 } 1338 }
1308 if (c == EOF_CHAR(tty)) { 1339 if (c == EOF_CHAR(tty)) {
1309 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
1310 return;
1311 if (ldata->canon_head != ldata->read_head)
1312 set_bit(TTY_PUSH, &tty->flags);
1313 c = __DISABLED_CHAR; 1340 c = __DISABLED_CHAR;
1314 goto handle_newline; 1341 goto handle_newline;
1315 } 1342 }
@@ -1317,11 +1344,6 @@ send_signal:
1317 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { 1344 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1318 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) 1345 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1319 ? 1 : 0; 1346 ? 1 : 0;
1320 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1321 if (L_ECHO(tty))
1322 process_output('\a', tty);
1323 return;
1324 }
1325 /* 1347 /*
1326 * XXX are EOL_CHAR and EOL2_CHAR echoed?!? 1348 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1327 */ 1349 */
@@ -1330,7 +1352,7 @@ send_signal:
1330 if (ldata->canon_head == ldata->read_head) 1352 if (ldata->canon_head == ldata->read_head)
1331 echo_set_canon_col(ldata); 1353 echo_set_canon_col(ldata);
1332 echo_char(c, tty); 1354 echo_char(c, tty);
1333 process_echoes(tty); 1355 commit_echoes(tty);
1334 } 1356 }
1335 /* 1357 /*
1336 * XXX does PARMRK doubling happen for 1358 * XXX does PARMRK doubling happen for
@@ -1340,26 +1362,17 @@ send_signal:
1340 put_tty_queue(c, ldata); 1362 put_tty_queue(c, ldata);
1341 1363
1342handle_newline: 1364handle_newline:
1343 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1365 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
1344 set_bit(ldata->read_head, ldata->read_flags); 1366 put_tty_queue(c, ldata);
1345 put_tty_queue_nolock(c, ldata);
1346 ldata->canon_head = ldata->read_head; 1367 ldata->canon_head = ldata->read_head;
1347 ldata->canon_data++;
1348 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1349 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1368 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1350 if (waitqueue_active(&tty->read_wait)) 1369 if (waitqueue_active(&tty->read_wait))
1351 wake_up_interruptible(&tty->read_wait); 1370 wake_up_interruptible(&tty->read_wait);
1352 return; 1371 return 0;
1353 } 1372 }
1354 } 1373 }
1355 1374
1356 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; 1375 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1357 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1358 /* beep if no space */
1359 if (L_ECHO(tty))
1360 process_output('\a', tty);
1361 return;
1362 }
1363 if (L_ECHO(tty)) { 1376 if (L_ECHO(tty)) {
1364 finish_erasing(ldata); 1377 finish_erasing(ldata);
1365 if (c == '\n') 1378 if (c == '\n')
@@ -1370,29 +1383,123 @@ handle_newline:
1370 echo_set_canon_col(ldata); 1383 echo_set_canon_col(ldata);
1371 echo_char(c, tty); 1384 echo_char(c, tty);
1372 } 1385 }
1373 process_echoes(tty); 1386 commit_echoes(tty);
1374 } 1387 }
1375 1388
1376 if (parmrk) 1389 if (parmrk)
1377 put_tty_queue(c, ldata); 1390 put_tty_queue(c, ldata);
1378 1391
1379 put_tty_queue(c, ldata); 1392 put_tty_queue(c, ldata);
1393 return 0;
1380} 1394}
1381 1395
1396static inline void
1397n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
1398{
1399 struct n_tty_data *ldata = tty->disc_data;
1400 int parmrk;
1382 1401
1383/** 1402 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1384 * n_tty_write_wakeup - asynchronous I/O notifier 1403 start_tty(tty);
1385 * @tty: tty device 1404 process_echoes(tty);
1386 * 1405 }
1387 * Required for the ptys, serial driver etc. since processes 1406 if (L_ECHO(tty)) {
1388 * that attach themselves to the master and rely on ASYNC 1407 finish_erasing(ldata);
1389 * IO must be woken up 1408 /* Record the column of first canon char. */
1390 */ 1409 if (ldata->canon_head == ldata->read_head)
1410 echo_set_canon_col(ldata);
1411 echo_char(c, tty);
1412 commit_echoes(tty);
1413 }
1414 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1415 if (parmrk)
1416 put_tty_queue(c, ldata);
1417 put_tty_queue(c, ldata);
1418}
1391 1419
1392static void n_tty_write_wakeup(struct tty_struct *tty) 1420static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1393{ 1421{
1394 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) 1422 n_tty_receive_char_inline(tty, c);
1395 kill_fasync(&tty->fasync, SIGIO, POLL_OUT); 1423}
1424
1425static inline void
1426n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1427{
1428 struct n_tty_data *ldata = tty->disc_data;
1429
1430 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1431 start_tty(tty);
1432 process_echoes(tty);
1433 }
1434 if (L_ECHO(tty)) {
1435 finish_erasing(ldata);
1436 /* Record the column of first canon char. */
1437 if (ldata->canon_head == ldata->read_head)
1438 echo_set_canon_col(ldata);
1439 echo_char(c, tty);
1440 commit_echoes(tty);
1441 }
1442 put_tty_queue(c, ldata);
1443}
1444
1445static inline void
1446n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
1447{
1448 if (I_ISTRIP(tty))
1449 c &= 0x7f;
1450 if (I_IUCLC(tty) && L_IEXTEN(tty))
1451 c = tolower(c);
1452
1453 if (I_IXON(tty)) {
1454 if (c == STOP_CHAR(tty))
1455 stop_tty(tty);
1456 else if (c == START_CHAR(tty) ||
1457 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1458 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1459 c != SUSP_CHAR(tty))) {
1460 start_tty(tty);
1461 process_echoes(tty);
1462 }
1463 }
1464}
1465
1466static void
1467n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1468{
1469 char buf[64];
1470
1471 switch (flag) {
1472 case TTY_BREAK:
1473 n_tty_receive_break(tty);
1474 break;
1475 case TTY_PARITY:
1476 case TTY_FRAME:
1477 n_tty_receive_parity_error(tty, c);
1478 break;
1479 case TTY_OVERRUN:
1480 n_tty_receive_overrun(tty);
1481 break;
1482 default:
1483 printk(KERN_ERR "%s: unknown flag %d\n",
1484 tty_name(tty, buf), flag);
1485 break;
1486 }
1487}
1488
1489static void
1490n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1491{
1492 struct n_tty_data *ldata = tty->disc_data;
1493
1494 ldata->lnext = 0;
1495 if (likely(flag == TTY_NORMAL)) {
1496 if (I_ISTRIP(tty))
1497 c &= 0x7f;
1498 if (I_IUCLC(tty) && L_IEXTEN(tty))
1499 c = tolower(c);
1500 n_tty_receive_char(tty, c);
1501 } else
1502 n_tty_receive_char_flagged(tty, c, flag);
1396} 1503}
1397 1504
1398/** 1505/**
@@ -1406,86 +1513,220 @@ static void n_tty_write_wakeup(struct tty_struct *tty)
1406 * been received. This function must be called from soft contexts 1513 * been received. This function must be called from soft contexts
1407 * not from interrupt context. The driver is responsible for making 1514 * not from interrupt context. The driver is responsible for making
1408 * calls one at a time and in order (or using flush_to_ldisc) 1515 * calls one at a time and in order (or using flush_to_ldisc)
1516 *
1517 * n_tty_receive_buf()/producer path:
1518 * claims non-exclusive termios_rwsem
1519 * publishes read_head and canon_head
1409 */ 1520 */
1410 1521
1411static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, 1522static void
1412 char *fp, int count) 1523n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1524 char *fp, int count)
1413{ 1525{
1414 struct n_tty_data *ldata = tty->disc_data; 1526 struct n_tty_data *ldata = tty->disc_data;
1415 const unsigned char *p; 1527 size_t n, head;
1416 char *f, flags = TTY_NORMAL; 1528
1417 int i; 1529 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1418 char buf[64]; 1530 n = N_TTY_BUF_SIZE - max(read_cnt(ldata), head);
1419 unsigned long cpuflags; 1531 n = min_t(size_t, count, n);
1420 1532 memcpy(read_buf_addr(ldata, head), cp, n);
1421 if (ldata->real_raw) { 1533 ldata->read_head += n;
1422 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags); 1534 cp += n;
1423 i = min(N_TTY_BUF_SIZE - ldata->read_cnt, 1535 count -= n;
1424 N_TTY_BUF_SIZE - ldata->read_head); 1536
1425 i = min(count, i); 1537 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1426 memcpy(ldata->read_buf + ldata->read_head, cp, i); 1538 n = N_TTY_BUF_SIZE - max(read_cnt(ldata), head);
1427 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1); 1539 n = min_t(size_t, count, n);
1428 ldata->read_cnt += i; 1540 memcpy(read_buf_addr(ldata, head), cp, n);
1429 cp += i; 1541 ldata->read_head += n;
1430 count -= i; 1542}
1431 1543
1432 i = min(N_TTY_BUF_SIZE - ldata->read_cnt, 1544static void
1433 N_TTY_BUF_SIZE - ldata->read_head); 1545n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1434 i = min(count, i); 1546 char *fp, int count)
1435 memcpy(ldata->read_buf + ldata->read_head, cp, i); 1547{
1436 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1); 1548 struct n_tty_data *ldata = tty->disc_data;
1437 ldata->read_cnt += i; 1549 char flag = TTY_NORMAL;
1438 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags); 1550
1439 } else { 1551 while (count--) {
1440 for (i = count, p = cp, f = fp; i; i--, p++) { 1552 if (fp)
1441 if (f) 1553 flag = *fp++;
1442 flags = *f++; 1554 if (likely(flag == TTY_NORMAL))
1443 switch (flags) { 1555 put_tty_queue(*cp++, ldata);
1444 case TTY_NORMAL: 1556 else
1445 n_tty_receive_char(tty, *p); 1557 n_tty_receive_char_flagged(tty, *cp++, flag);
1446 break; 1558 }
1447 case TTY_BREAK: 1559}
1448 n_tty_receive_break(tty); 1560
1449 break; 1561static void
1450 case TTY_PARITY: 1562n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1451 case TTY_FRAME: 1563 char *fp, int count)
1452 n_tty_receive_parity_error(tty, *p); 1564{
1453 break; 1565 char flag = TTY_NORMAL;
1454 case TTY_OVERRUN: 1566
1455 n_tty_receive_overrun(tty); 1567 while (count--) {
1456 break; 1568 if (fp)
1457 default: 1569 flag = *fp++;
1458 printk(KERN_ERR "%s: unknown flag %d\n", 1570 if (likely(flag == TTY_NORMAL))
1459 tty_name(tty, buf), flags); 1571 n_tty_receive_char_closing(tty, *cp++);
1460 break; 1572 else
1573 n_tty_receive_char_flagged(tty, *cp++, flag);
1574 }
1575}
1576
1577static void
1578n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
1579 char *fp, int count)
1580{
1581 struct n_tty_data *ldata = tty->disc_data;
1582 char flag = TTY_NORMAL;
1583
1584 while (count--) {
1585 if (fp)
1586 flag = *fp++;
1587 if (likely(flag == TTY_NORMAL)) {
1588 unsigned char c = *cp++;
1589
1590 if (I_ISTRIP(tty))
1591 c &= 0x7f;
1592 if (I_IUCLC(tty) && L_IEXTEN(tty))
1593 c = tolower(c);
1594 if (L_EXTPROC(tty)) {
1595 put_tty_queue(c, ldata);
1596 continue;
1597 }
1598 if (!test_bit(c, ldata->char_map))
1599 n_tty_receive_char_inline(tty, c);
1600 else if (n_tty_receive_char_special(tty, c) && count) {
1601 if (fp)
1602 flag = *fp++;
1603 n_tty_receive_char_lnext(tty, *cp++, flag);
1604 count--;
1605 }
1606 } else
1607 n_tty_receive_char_flagged(tty, *cp++, flag);
1608 }
1609}
1610
1611static void
1612n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1613 char *fp, int count)
1614{
1615 struct n_tty_data *ldata = tty->disc_data;
1616 char flag = TTY_NORMAL;
1617
1618 while (count--) {
1619 if (fp)
1620 flag = *fp++;
1621 if (likely(flag == TTY_NORMAL)) {
1622 unsigned char c = *cp++;
1623
1624 if (!test_bit(c, ldata->char_map))
1625 n_tty_receive_char_fast(tty, c);
1626 else if (n_tty_receive_char_special(tty, c) && count) {
1627 if (fp)
1628 flag = *fp++;
1629 n_tty_receive_char_lnext(tty, *cp++, flag);
1630 count--;
1461 } 1631 }
1632 } else
1633 n_tty_receive_char_flagged(tty, *cp++, flag);
1634 }
1635}
1636
1637static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1638 char *fp, int count)
1639{
1640 struct n_tty_data *ldata = tty->disc_data;
1641 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
1642
1643 if (ldata->real_raw)
1644 n_tty_receive_buf_real_raw(tty, cp, fp, count);
1645 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
1646 n_tty_receive_buf_raw(tty, cp, fp, count);
1647 else if (tty->closing && !L_EXTPROC(tty))
1648 n_tty_receive_buf_closing(tty, cp, fp, count);
1649 else {
1650 if (ldata->lnext) {
1651 char flag = TTY_NORMAL;
1652
1653 if (fp)
1654 flag = *fp++;
1655 n_tty_receive_char_lnext(tty, *cp++, flag);
1656 count--;
1462 } 1657 }
1658
1659 if (!preops && !I_PARMRK(tty))
1660 n_tty_receive_buf_fast(tty, cp, fp, count);
1661 else
1662 n_tty_receive_buf_standard(tty, cp, fp, count);
1663
1664 flush_echoes(tty);
1463 if (tty->ops->flush_chars) 1665 if (tty->ops->flush_chars)
1464 tty->ops->flush_chars(tty); 1666 tty->ops->flush_chars(tty);
1465 } 1667 }
1466 1668
1467 set_room(tty); 1669 if ((!ldata->icanon && (read_cnt(ldata) >= ldata->minimum_to_wake)) ||
1468
1469 if ((!ldata->icanon && (ldata->read_cnt >= ldata->minimum_to_wake)) ||
1470 L_EXTPROC(tty)) { 1670 L_EXTPROC(tty)) {
1471 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1671 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1472 if (waitqueue_active(&tty->read_wait)) 1672 if (waitqueue_active(&tty->read_wait))
1473 wake_up_interruptible(&tty->read_wait); 1673 wake_up_interruptible(&tty->read_wait);
1474 } 1674 }
1675}
1676
1677static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1678 char *fp, int count)
1679{
1680 int room, n;
1681
1682 down_read(&tty->termios_rwsem);
1475 1683
1476 /*
1477 * Check the remaining room for the input canonicalization
1478 * mode. We don't want to throttle the driver if we're in
1479 * canonical mode and don't have a newline yet!
1480 */
1481 while (1) { 1684 while (1) {
1482 tty_set_flow_change(tty, TTY_THROTTLE_SAFE); 1685 room = receive_room(tty);
1483 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE) 1686 n = min(count, room);
1687 if (!n)
1484 break; 1688 break;
1485 if (!tty_throttle_safe(tty)) 1689 __receive_buf(tty, cp, fp, n);
1690 cp += n;
1691 if (fp)
1692 fp += n;
1693 count -= n;
1694 }
1695
1696 tty->receive_room = room;
1697 n_tty_check_throttle(tty);
1698 up_read(&tty->termios_rwsem);
1699}
1700
1701static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1702 char *fp, int count)
1703{
1704 struct n_tty_data *ldata = tty->disc_data;
1705 int room, n, rcvd = 0;
1706
1707 down_read(&tty->termios_rwsem);
1708
1709 while (1) {
1710 room = receive_room(tty);
1711 n = min(count, room);
1712 if (!n) {
1713 if (!room)
1714 ldata->no_room = 1;
1486 break; 1715 break;
1716 }
1717 __receive_buf(tty, cp, fp, n);
1718 cp += n;
1719 if (fp)
1720 fp += n;
1721 count -= n;
1722 rcvd += n;
1487 } 1723 }
1488 __tty_set_flow_change(tty, 0); 1724
1725 tty->receive_room = room;
1726 n_tty_check_throttle(tty);
1727 up_read(&tty->termios_rwsem);
1728
1729 return rcvd;
1489} 1730}
1490 1731
1491int is_ignored(int sig) 1732int is_ignored(int sig)
@@ -1505,7 +1746,7 @@ int is_ignored(int sig)
1505 * guaranteed that this function will not be re-entered or in progress 1746 * guaranteed that this function will not be re-entered or in progress
1506 * when the ldisc is closed. 1747 * when the ldisc is closed.
1507 * 1748 *
1508 * Locking: Caller holds tty->termios_mutex 1749 * Locking: Caller holds tty->termios_rwsem
1509 */ 1750 */
1510 1751
1511static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) 1752static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
@@ -1517,12 +1758,13 @@ static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1517 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON; 1758 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
1518 if (canon_change) { 1759 if (canon_change) {
1519 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 1760 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1761 ldata->line_start = 0;
1520 ldata->canon_head = ldata->read_tail; 1762 ldata->canon_head = ldata->read_tail;
1521 ldata->canon_data = 0;
1522 ldata->erasing = 0; 1763 ldata->erasing = 0;
1764 ldata->lnext = 0;
1523 } 1765 }
1524 1766
1525 if (canon_change && !L_ICANON(tty) && ldata->read_cnt) 1767 if (canon_change && !L_ICANON(tty) && read_cnt(ldata))
1526 wake_up_interruptible(&tty->read_wait); 1768 wake_up_interruptible(&tty->read_wait);
1527 1769
1528 ldata->icanon = (L_ICANON(tty) != 0); 1770 ldata->icanon = (L_ICANON(tty) != 0);
@@ -1531,41 +1773,38 @@ static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1531 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || 1773 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1532 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || 1774 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1533 I_PARMRK(tty)) { 1775 I_PARMRK(tty)) {
1534 bitmap_zero(ldata->process_char_map, 256); 1776 bitmap_zero(ldata->char_map, 256);
1535 1777
1536 if (I_IGNCR(tty) || I_ICRNL(tty)) 1778 if (I_IGNCR(tty) || I_ICRNL(tty))
1537 set_bit('\r', ldata->process_char_map); 1779 set_bit('\r', ldata->char_map);
1538 if (I_INLCR(tty)) 1780 if (I_INLCR(tty))
1539 set_bit('\n', ldata->process_char_map); 1781 set_bit('\n', ldata->char_map);
1540 1782
1541 if (L_ICANON(tty)) { 1783 if (L_ICANON(tty)) {
1542 set_bit(ERASE_CHAR(tty), ldata->process_char_map); 1784 set_bit(ERASE_CHAR(tty), ldata->char_map);
1543 set_bit(KILL_CHAR(tty), ldata->process_char_map); 1785 set_bit(KILL_CHAR(tty), ldata->char_map);
1544 set_bit(EOF_CHAR(tty), ldata->process_char_map); 1786 set_bit(EOF_CHAR(tty), ldata->char_map);
1545 set_bit('\n', ldata->process_char_map); 1787 set_bit('\n', ldata->char_map);
1546 set_bit(EOL_CHAR(tty), ldata->process_char_map); 1788 set_bit(EOL_CHAR(tty), ldata->char_map);
1547 if (L_IEXTEN(tty)) { 1789 if (L_IEXTEN(tty)) {
1548 set_bit(WERASE_CHAR(tty), 1790 set_bit(WERASE_CHAR(tty), ldata->char_map);
1549 ldata->process_char_map); 1791 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1550 set_bit(LNEXT_CHAR(tty), 1792 set_bit(EOL2_CHAR(tty), ldata->char_map);
1551 ldata->process_char_map);
1552 set_bit(EOL2_CHAR(tty),
1553 ldata->process_char_map);
1554 if (L_ECHO(tty)) 1793 if (L_ECHO(tty))
1555 set_bit(REPRINT_CHAR(tty), 1794 set_bit(REPRINT_CHAR(tty),
1556 ldata->process_char_map); 1795 ldata->char_map);
1557 } 1796 }
1558 } 1797 }
1559 if (I_IXON(tty)) { 1798 if (I_IXON(tty)) {
1560 set_bit(START_CHAR(tty), ldata->process_char_map); 1799 set_bit(START_CHAR(tty), ldata->char_map);
1561 set_bit(STOP_CHAR(tty), ldata->process_char_map); 1800 set_bit(STOP_CHAR(tty), ldata->char_map);
1562 } 1801 }
1563 if (L_ISIG(tty)) { 1802 if (L_ISIG(tty)) {
1564 set_bit(INTR_CHAR(tty), ldata->process_char_map); 1803 set_bit(INTR_CHAR(tty), ldata->char_map);
1565 set_bit(QUIT_CHAR(tty), ldata->process_char_map); 1804 set_bit(QUIT_CHAR(tty), ldata->char_map);
1566 set_bit(SUSP_CHAR(tty), ldata->process_char_map); 1805 set_bit(SUSP_CHAR(tty), ldata->char_map);
1567 } 1806 }
1568 clear_bit(__DISABLED_CHAR, ldata->process_char_map); 1807 clear_bit(__DISABLED_CHAR, ldata->char_map);
1569 ldata->raw = 0; 1808 ldata->raw = 0;
1570 ldata->real_raw = 0; 1809 ldata->real_raw = 0;
1571 } else { 1810 } else {
@@ -1608,9 +1847,7 @@ static void n_tty_close(struct tty_struct *tty)
1608 if (tty->link) 1847 if (tty->link)
1609 n_tty_packet_mode_flush(tty); 1848 n_tty_packet_mode_flush(tty);
1610 1849
1611 kfree(ldata->read_buf); 1850 vfree(ldata);
1612 kfree(ldata->echo_buf);
1613 kfree(ldata);
1614 tty->disc_data = NULL; 1851 tty->disc_data = NULL;
1615} 1852}
1616 1853
@@ -1628,26 +1865,23 @@ static int n_tty_open(struct tty_struct *tty)
1628{ 1865{
1629 struct n_tty_data *ldata; 1866 struct n_tty_data *ldata;
1630 1867
1631 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL); 1868 /* Currently a malloc failure here can panic */
1869 ldata = vmalloc(sizeof(*ldata));
1632 if (!ldata) 1870 if (!ldata)
1633 goto err; 1871 goto err;
1634 1872
1635 ldata->overrun_time = jiffies; 1873 ldata->overrun_time = jiffies;
1636 mutex_init(&ldata->atomic_read_lock); 1874 mutex_init(&ldata->atomic_read_lock);
1637 mutex_init(&ldata->output_lock); 1875 mutex_init(&ldata->output_lock);
1638 mutex_init(&ldata->echo_lock);
1639 raw_spin_lock_init(&ldata->read_lock);
1640
1641 /* These are ugly. Currently a malloc failure here can panic */
1642 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1643 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1644 if (!ldata->read_buf || !ldata->echo_buf)
1645 goto err_free_bufs;
1646 1876
1647 tty->disc_data = ldata; 1877 tty->disc_data = ldata;
1648 reset_buffer_flags(tty->disc_data); 1878 reset_buffer_flags(tty->disc_data);
1649 ldata->column = 0; 1879 ldata->column = 0;
1880 ldata->canon_column = 0;
1650 ldata->minimum_to_wake = 1; 1881 ldata->minimum_to_wake = 1;
1882 ldata->num_overrun = 0;
1883 ldata->no_room = 0;
1884 ldata->lnext = 0;
1651 tty->closing = 0; 1885 tty->closing = 0;
1652 /* indicate buffer work may resume */ 1886 /* indicate buffer work may resume */
1653 clear_bit(TTY_LDISC_HALTED, &tty->flags); 1887 clear_bit(TTY_LDISC_HALTED, &tty->flags);
@@ -1655,10 +1889,6 @@ static int n_tty_open(struct tty_struct *tty)
1655 tty_unthrottle(tty); 1889 tty_unthrottle(tty);
1656 1890
1657 return 0; 1891 return 0;
1658err_free_bufs:
1659 kfree(ldata->read_buf);
1660 kfree(ldata->echo_buf);
1661 kfree(ldata);
1662err: 1892err:
1663 return -ENOMEM; 1893 return -ENOMEM;
1664} 1894}
@@ -1667,11 +1897,10 @@ static inline int input_available_p(struct tty_struct *tty, int amt)
1667{ 1897{
1668 struct n_tty_data *ldata = tty->disc_data; 1898 struct n_tty_data *ldata = tty->disc_data;
1669 1899
1670 tty_flush_to_ldisc(tty);
1671 if (ldata->icanon && !L_EXTPROC(tty)) { 1900 if (ldata->icanon && !L_EXTPROC(tty)) {
1672 if (ldata->canon_data) 1901 if (ldata->canon_head != ldata->read_tail)
1673 return 1; 1902 return 1;
1674 } else if (ldata->read_cnt >= (amt ? amt : 1)) 1903 } else if (read_cnt(ldata) >= (amt ? amt : 1))
1675 return 1; 1904 return 1;
1676 1905
1677 return 0; 1906 return 0;
@@ -1692,6 +1921,9 @@ static inline int input_available_p(struct tty_struct *tty, int amt)
1692 * 1921 *
1693 * Called under the ldata->atomic_read_lock sem 1922 * Called under the ldata->atomic_read_lock sem
1694 * 1923 *
1924 * n_tty_read()/consumer path:
1925 * caller holds non-exclusive termios_rwsem
1926 * read_tail published
1695 */ 1927 */
1696 1928
1697static int copy_from_read_buf(struct tty_struct *tty, 1929static int copy_from_read_buf(struct tty_struct *tty,
@@ -1702,34 +1934,114 @@ static int copy_from_read_buf(struct tty_struct *tty,
1702 struct n_tty_data *ldata = tty->disc_data; 1934 struct n_tty_data *ldata = tty->disc_data;
1703 int retval; 1935 int retval;
1704 size_t n; 1936 size_t n;
1705 unsigned long flags;
1706 bool is_eof; 1937 bool is_eof;
1938 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1707 1939
1708 retval = 0; 1940 retval = 0;
1709 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1941 n = min(read_cnt(ldata), N_TTY_BUF_SIZE - tail);
1710 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
1711 n = min(*nr, n); 1942 n = min(*nr, n);
1712 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1713 if (n) { 1943 if (n) {
1714 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n); 1944 retval = copy_to_user(*b, read_buf_addr(ldata, tail), n);
1715 n -= retval; 1945 n -= retval;
1716 is_eof = n == 1 && 1946 is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty);
1717 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty); 1947 tty_audit_add_data(tty, read_buf_addr(ldata, tail), n,
1718 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
1719 ldata->icanon); 1948 ldata->icanon);
1720 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1949 ldata->read_tail += n;
1721 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1722 ldata->read_cnt -= n;
1723 /* Turn single EOF into zero-length read */ 1950 /* Turn single EOF into zero-length read */
1724 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt) 1951 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !read_cnt(ldata))
1725 n = 0; 1952 n = 0;
1726 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1727 *b += n; 1953 *b += n;
1728 *nr -= n; 1954 *nr -= n;
1729 } 1955 }
1730 return retval; 1956 return retval;
1731} 1957}
1732 1958
1959/**
1960 * canon_copy_from_read_buf - copy read data in canonical mode
1961 * @tty: terminal device
1962 * @b: user data
1963 * @nr: size of data
1964 *
1965 * Helper function for n_tty_read. It is only called when ICANON is on;
1966 * it copies one line of input up to and including the line-delimiting
1967 * character into the user-space buffer.
1968 *
1969 * Called under the atomic_read_lock mutex
1970 *
1971 * n_tty_read()/consumer path:
1972 * caller holds non-exclusive termios_rwsem
1973 * read_tail published
1974 */
1975
1976static int canon_copy_from_read_buf(struct tty_struct *tty,
1977 unsigned char __user **b,
1978 size_t *nr)
1979{
1980 struct n_tty_data *ldata = tty->disc_data;
1981 size_t n, size, more, c;
1982 size_t eol;
1983 size_t tail;
1984 int ret, found = 0;
1985 bool eof_push = 0;
1986
1987 /* N.B. avoid overrun if nr == 0 */
1988 n = min(*nr, read_cnt(ldata));
1989 if (!n)
1990 return 0;
1991
1992 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1993 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
1994
1995 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
1996 __func__, *nr, tail, n, size);
1997
1998 eol = find_next_bit(ldata->read_flags, size, tail);
1999 more = n - (size - tail);
2000 if (eol == N_TTY_BUF_SIZE && more) {
2001 /* scan wrapped without finding set bit */
2002 eol = find_next_bit(ldata->read_flags, more, 0);
2003 if (eol != more)
2004 found = 1;
2005 } else if (eol != size)
2006 found = 1;
2007
2008 size = N_TTY_BUF_SIZE - tail;
2009 n = (found + eol + size) & (N_TTY_BUF_SIZE - 1);
2010 c = n;
2011
2012 if (found && read_buf(ldata, eol) == __DISABLED_CHAR) {
2013 n--;
2014 eof_push = !n && ldata->read_tail != ldata->line_start;
2015 }
2016
2017 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
2018 __func__, eol, found, n, c, size, more);
2019
2020 if (n > size) {
2021 ret = copy_to_user(*b, read_buf_addr(ldata, tail), size);
2022 if (ret)
2023 return -EFAULT;
2024 ret = copy_to_user(*b + size, ldata->read_buf, n - size);
2025 } else
2026 ret = copy_to_user(*b, read_buf_addr(ldata, tail), n);
2027
2028 if (ret)
2029 return -EFAULT;
2030 *b += n;
2031 *nr -= n;
2032
2033 if (found)
2034 clear_bit(eol, ldata->read_flags);
2035 smp_mb__after_clear_bit();
2036 ldata->read_tail += c;
2037
2038 if (found) {
2039 ldata->line_start = ldata->read_tail;
2040 tty_audit_push(tty);
2041 }
2042 return eof_push ? -EAGAIN : 0;
2043}
2044
1733extern ssize_t redirected_tty_write(struct file *, const char __user *, 2045extern ssize_t redirected_tty_write(struct file *, const char __user *,
1734 size_t, loff_t *); 2046 size_t, loff_t *);
1735 2047
@@ -1787,6 +2099,10 @@ static int job_control(struct tty_struct *tty, struct file *file)
1787 * a hangup. Always called in user context, may sleep. 2099 * a hangup. Always called in user context, may sleep.
1788 * 2100 *
1789 * This code must be sure never to sleep through a hangup. 2101 * This code must be sure never to sleep through a hangup.
2102 *
2103 * n_tty_read()/consumer path:
2104 * claims non-exclusive termios_rwsem
2105 * publishes read_tail
1790 */ 2106 */
1791 2107
1792static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, 2108static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
@@ -1798,16 +2114,16 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1798 int c; 2114 int c;
1799 int minimum, time; 2115 int minimum, time;
1800 ssize_t retval = 0; 2116 ssize_t retval = 0;
1801 ssize_t size;
1802 long timeout; 2117 long timeout;
1803 unsigned long flags; 2118 unsigned long flags;
1804 int packet; 2119 int packet;
1805 2120
1806do_it_again:
1807 c = job_control(tty, file); 2121 c = job_control(tty, file);
1808 if (c < 0) 2122 if (c < 0)
1809 return c; 2123 return c;
1810 2124
2125 down_read(&tty->termios_rwsem);
2126
1811 minimum = time = 0; 2127 minimum = time = 0;
1812 timeout = MAX_SCHEDULE_TIMEOUT; 2128 timeout = MAX_SCHEDULE_TIMEOUT;
1813 if (!ldata->icanon) { 2129 if (!ldata->icanon) {
@@ -1829,11 +2145,15 @@ do_it_again:
1829 * Internal serialization of reads. 2145 * Internal serialization of reads.
1830 */ 2146 */
1831 if (file->f_flags & O_NONBLOCK) { 2147 if (file->f_flags & O_NONBLOCK) {
1832 if (!mutex_trylock(&ldata->atomic_read_lock)) 2148 if (!mutex_trylock(&ldata->atomic_read_lock)) {
2149 up_read(&tty->termios_rwsem);
1833 return -EAGAIN; 2150 return -EAGAIN;
2151 }
1834 } else { 2152 } else {
1835 if (mutex_lock_interruptible(&ldata->atomic_read_lock)) 2153 if (mutex_lock_interruptible(&ldata->atomic_read_lock)) {
2154 up_read(&tty->termios_rwsem);
1836 return -ERESTARTSYS; 2155 return -ERESTARTSYS;
2156 }
1837 } 2157 }
1838 packet = tty->packet; 2158 packet = tty->packet;
1839 2159
@@ -1883,7 +2203,11 @@ do_it_again:
1883 break; 2203 break;
1884 } 2204 }
1885 n_tty_set_room(tty); 2205 n_tty_set_room(tty);
2206 up_read(&tty->termios_rwsem);
2207
1886 timeout = schedule_timeout(timeout); 2208 timeout = schedule_timeout(timeout);
2209
2210 down_read(&tty->termios_rwsem);
1887 continue; 2211 continue;
1888 } 2212 }
1889 __set_current_state(TASK_RUNNING); 2213 __set_current_state(TASK_RUNNING);
@@ -1899,45 +2223,11 @@ do_it_again:
1899 } 2223 }
1900 2224
1901 if (ldata->icanon && !L_EXTPROC(tty)) { 2225 if (ldata->icanon && !L_EXTPROC(tty)) {
1902 /* N.B. avoid overrun if nr == 0 */ 2226 retval = canon_copy_from_read_buf(tty, &b, &nr);
1903 raw_spin_lock_irqsave(&ldata->read_lock, flags); 2227 if (retval == -EAGAIN) {
1904 while (nr && ldata->read_cnt) { 2228 retval = 0;
1905 int eol; 2229 continue;
1906 2230 } else if (retval)
1907 eol = test_and_clear_bit(ldata->read_tail,
1908 ldata->read_flags);
1909 c = ldata->read_buf[ldata->read_tail];
1910 ldata->read_tail = ((ldata->read_tail+1) &
1911 (N_TTY_BUF_SIZE-1));
1912 ldata->read_cnt--;
1913 if (eol) {
1914 /* this test should be redundant:
1915 * we shouldn't be reading data if
1916 * canon_data is 0
1917 */
1918 if (--ldata->canon_data < 0)
1919 ldata->canon_data = 0;
1920 }
1921 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1922
1923 if (!eol || (c != __DISABLED_CHAR)) {
1924 if (tty_put_user(tty, c, b++)) {
1925 retval = -EFAULT;
1926 b--;
1927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1928 break;
1929 }
1930 nr--;
1931 }
1932 if (eol) {
1933 tty_audit_push(tty);
1934 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1935 break;
1936 }
1937 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1938 }
1939 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1940 if (retval)
1941 break; 2231 break;
1942 } else { 2232 } else {
1943 int uncopied; 2233 int uncopied;
@@ -1951,24 +2241,7 @@ do_it_again:
1951 } 2241 }
1952 } 2242 }
1953 2243
1954 /* If there is enough space in the read buffer now, let the 2244 n_tty_check_unthrottle(tty);
1955 * low-level driver know. We use n_tty_chars_in_buffer() to
1956 * check the buffer, as it now knows about canonical mode.
1957 * Otherwise, if the driver is throttled and the line is
1958 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1959 * we won't get any more characters.
1960 */
1961 while (1) {
1962 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1963 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1964 break;
1965 if (!tty->count)
1966 break;
1967 n_tty_set_room(tty);
1968 if (!tty_unthrottle_safe(tty))
1969 break;
1970 }
1971 __tty_set_flow_change(tty, 0);
1972 2245
1973 if (b - buf >= minimum) 2246 if (b - buf >= minimum)
1974 break; 2247 break;
@@ -1982,15 +2255,11 @@ do_it_again:
1982 ldata->minimum_to_wake = minimum; 2255 ldata->minimum_to_wake = minimum;
1983 2256
1984 __set_current_state(TASK_RUNNING); 2257 __set_current_state(TASK_RUNNING);
1985 size = b - buf; 2258 if (b - buf)
1986 if (size) { 2259 retval = b - buf;
1987 retval = size;
1988 if (nr)
1989 clear_bit(TTY_PUSH, &tty->flags);
1990 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1991 goto do_it_again;
1992 2260
1993 n_tty_set_room(tty); 2261 n_tty_set_room(tty);
2262 up_read(&tty->termios_rwsem);
1994 return retval; 2263 return retval;
1995} 2264}
1996 2265
@@ -2031,6 +2300,8 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2031 return retval; 2300 return retval;
2032 } 2301 }
2033 2302
2303 down_read(&tty->termios_rwsem);
2304
2034 /* Write out any echoed characters that are still pending */ 2305 /* Write out any echoed characters that are still pending */
2035 process_echoes(tty); 2306 process_echoes(tty);
2036 2307
@@ -2084,13 +2355,18 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2084 retval = -EAGAIN; 2355 retval = -EAGAIN;
2085 break; 2356 break;
2086 } 2357 }
2358 up_read(&tty->termios_rwsem);
2359
2087 schedule(); 2360 schedule();
2361
2362 down_read(&tty->termios_rwsem);
2088 } 2363 }
2089break_out: 2364break_out:
2090 __set_current_state(TASK_RUNNING); 2365 __set_current_state(TASK_RUNNING);
2091 remove_wait_queue(&tty->write_wait, &wait); 2366 remove_wait_queue(&tty->write_wait, &wait);
2092 if (b - buf != nr && tty->fasync) 2367 if (b - buf != nr && tty->fasync)
2093 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 2368 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2369 up_read(&tty->termios_rwsem);
2094 return (b - buf) ? b - buf : retval; 2370 return (b - buf) ? b - buf : retval;
2095} 2371}
2096 2372
@@ -2139,19 +2415,19 @@ static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2139 2415
2140static unsigned long inq_canon(struct n_tty_data *ldata) 2416static unsigned long inq_canon(struct n_tty_data *ldata)
2141{ 2417{
2142 int nr, head, tail; 2418 size_t nr, head, tail;
2143 2419
2144 if (!ldata->canon_data) 2420 if (ldata->canon_head == ldata->read_tail)
2145 return 0; 2421 return 0;
2146 head = ldata->canon_head; 2422 head = ldata->canon_head;
2147 tail = ldata->read_tail; 2423 tail = ldata->read_tail;
2148 nr = (head - tail) & (N_TTY_BUF_SIZE-1); 2424 nr = head - tail;
2149 /* Skip EOF-chars.. */ 2425 /* Skip EOF-chars.. */
2150 while (head != tail) { 2426 while (head != tail) {
2151 if (test_bit(tail, ldata->read_flags) && 2427 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2152 ldata->read_buf[tail] == __DISABLED_CHAR) 2428 read_buf(ldata, tail) == __DISABLED_CHAR)
2153 nr--; 2429 nr--;
2154 tail = (tail+1) & (N_TTY_BUF_SIZE-1); 2430 tail++;
2155 } 2431 }
2156 return nr; 2432 return nr;
2157} 2433}
@@ -2166,10 +2442,12 @@ static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2166 case TIOCOUTQ: 2442 case TIOCOUTQ:
2167 return put_user(tty_chars_in_buffer(tty), (int __user *) arg); 2443 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2168 case TIOCINQ: 2444 case TIOCINQ:
2169 /* FIXME: Locking */ 2445 down_write(&tty->termios_rwsem);
2170 retval = ldata->read_cnt;
2171 if (L_ICANON(tty)) 2446 if (L_ICANON(tty))
2172 retval = inq_canon(ldata); 2447 retval = inq_canon(ldata);
2448 else
2449 retval = read_cnt(ldata);
2450 up_write(&tty->termios_rwsem);
2173 return put_user(retval, (unsigned int __user *) arg); 2451 return put_user(retval, (unsigned int __user *) arg);
2174 default: 2452 default:
2175 return n_tty_ioctl_helper(tty, file, cmd, arg); 2453 return n_tty_ioctl_helper(tty, file, cmd, arg);
@@ -2203,6 +2481,7 @@ struct tty_ldisc_ops tty_ldisc_N_TTY = {
2203 .receive_buf = n_tty_receive_buf, 2481 .receive_buf = n_tty_receive_buf,
2204 .write_wakeup = n_tty_write_wakeup, 2482 .write_wakeup = n_tty_write_wakeup,
2205 .fasync = n_tty_fasync, 2483 .fasync = n_tty_fasync,
2484 .receive_buf2 = n_tty_receive_buf2,
2206}; 2485};
2207 2486
2208/** 2487/**
diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
index abfd99089781..25c9bc783722 100644
--- a/drivers/tty/pty.c
+++ b/drivers/tty/pty.c
@@ -89,17 +89,13 @@ static void pty_unthrottle(struct tty_struct *tty)
89 * pty_space - report space left for writing 89 * pty_space - report space left for writing
90 * @to: tty we are writing into 90 * @to: tty we are writing into
91 * 91 *
92 * The tty buffers allow 64K but we sneak a peak and clip at 8K this 92 * Limit the buffer space used by ptys to 8k.
93 * allows a lot of overspill room for echo and other fun messes to
94 * be handled properly
95 */ 93 */
96 94
97static int pty_space(struct tty_struct *to) 95static int pty_space(struct tty_struct *to)
98{ 96{
99 int n = 8192 - to->port->buf.memory_used; 97 int n = tty_buffer_space_avail(to->port);
100 if (n < 0) 98 return min(n, 8192);
101 return 0;
102 return n;
103} 99}
104 100
105/** 101/**
@@ -125,10 +121,8 @@ static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
125 /* Stuff the data into the input queue of the other end */ 121 /* Stuff the data into the input queue of the other end */
126 c = tty_insert_flip_string(to->port, buf, c); 122 c = tty_insert_flip_string(to->port, buf, c);
127 /* And shovel */ 123 /* And shovel */
128 if (c) { 124 if (c)
129 tty_flip_buffer_push(to->port); 125 tty_flip_buffer_push(to->port);
130 tty_wakeup(tty);
131 }
132 } 126 }
133 return c; 127 return c;
134} 128}
@@ -287,7 +281,7 @@ static int pty_resize(struct tty_struct *tty, struct winsize *ws)
287 struct tty_struct *pty = tty->link; 281 struct tty_struct *pty = tty->link;
288 282
289 /* For a PTY we need to lock the tty side */ 283 /* For a PTY we need to lock the tty side */
290 mutex_lock(&tty->termios_mutex); 284 mutex_lock(&tty->winsize_mutex);
291 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 285 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
292 goto done; 286 goto done;
293 287
@@ -314,7 +308,7 @@ static int pty_resize(struct tty_struct *tty, struct winsize *ws)
314 tty->winsize = *ws; 308 tty->winsize = *ws;
315 pty->winsize = *ws; /* Never used so will go away soon */ 309 pty->winsize = *ws; /* Never used so will go away soon */
316done: 310done:
317 mutex_unlock(&tty->termios_mutex); 311 mutex_unlock(&tty->winsize_mutex);
318 return 0; 312 return 0;
319} 313}
320 314
diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
index 86c00b1c5583..570df9d2a5d2 100644
--- a/drivers/tty/serial/8250/8250_core.c
+++ b/drivers/tty/serial/8250/8250_core.c
@@ -3062,7 +3062,7 @@ void serial8250_resume_port(int line)
3062 */ 3062 */
3063static int serial8250_probe(struct platform_device *dev) 3063static int serial8250_probe(struct platform_device *dev)
3064{ 3064{
3065 struct plat_serial8250_port *p = dev->dev.platform_data; 3065 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
3066 struct uart_8250_port uart; 3066 struct uart_8250_port uart;
3067 int ret, i, irqflag = 0; 3067 int ret, i, irqflag = 0;
3068 3068
diff --git a/drivers/tty/serial/8250/8250_em.c b/drivers/tty/serial/8250/8250_em.c
index 916cc19fbbda..5f3bba12c159 100644
--- a/drivers/tty/serial/8250/8250_em.c
+++ b/drivers/tty/serial/8250/8250_em.c
@@ -95,25 +95,23 @@ static int serial8250_em_probe(struct platform_device *pdev)
95 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 95 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
96 struct serial8250_em_priv *priv; 96 struct serial8250_em_priv *priv;
97 struct uart_8250_port up; 97 struct uart_8250_port up;
98 int ret = -EINVAL; 98 int ret;
99 99
100 if (!regs || !irq) { 100 if (!regs || !irq) {
101 dev_err(&pdev->dev, "missing registers or irq\n"); 101 dev_err(&pdev->dev, "missing registers or irq\n");
102 goto err0; 102 return -EINVAL;
103 } 103 }
104 104
105 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 105 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
106 if (!priv) { 106 if (!priv) {
107 dev_err(&pdev->dev, "unable to allocate private data\n"); 107 dev_err(&pdev->dev, "unable to allocate private data\n");
108 ret = -ENOMEM; 108 return -ENOMEM;
109 goto err0;
110 } 109 }
111 110
112 priv->sclk = clk_get(&pdev->dev, "sclk"); 111 priv->sclk = devm_clk_get(&pdev->dev, "sclk");
113 if (IS_ERR(priv->sclk)) { 112 if (IS_ERR(priv->sclk)) {
114 dev_err(&pdev->dev, "unable to get clock\n"); 113 dev_err(&pdev->dev, "unable to get clock\n");
115 ret = PTR_ERR(priv->sclk); 114 return PTR_ERR(priv->sclk);
116 goto err1;
117 } 115 }
118 116
119 memset(&up, 0, sizeof(up)); 117 memset(&up, 0, sizeof(up));
@@ -136,20 +134,13 @@ static int serial8250_em_probe(struct platform_device *pdev)
136 ret = serial8250_register_8250_port(&up); 134 ret = serial8250_register_8250_port(&up);
137 if (ret < 0) { 135 if (ret < 0) {
138 dev_err(&pdev->dev, "unable to register 8250 port\n"); 136 dev_err(&pdev->dev, "unable to register 8250 port\n");
139 goto err2; 137 clk_disable(priv->sclk);
138 return ret;
140 } 139 }
141 140
142 priv->line = ret; 141 priv->line = ret;
143 platform_set_drvdata(pdev, priv); 142 platform_set_drvdata(pdev, priv);
144 return 0; 143 return 0;
145
146 err2:
147 clk_disable(priv->sclk);
148 clk_put(priv->sclk);
149 err1:
150 kfree(priv);
151 err0:
152 return ret;
153} 144}
154 145
155static int serial8250_em_remove(struct platform_device *pdev) 146static int serial8250_em_remove(struct platform_device *pdev)
@@ -158,8 +149,6 @@ static int serial8250_em_remove(struct platform_device *pdev)
158 149
159 serial8250_unregister_port(priv->line); 150 serial8250_unregister_port(priv->line);
160 clk_disable(priv->sclk); 151 clk_disable(priv->sclk);
161 clk_put(priv->sclk);
162 kfree(priv);
163 return 0; 152 return 0;
164} 153}
165 154
diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index c52948b368d8..c810da7c7a88 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -1565,6 +1565,7 @@ pci_wch_ch353_setup(struct serial_private *priv,
1565#define PCI_DEVICE_ID_COMMTECH_4228PCIE 0x0021 1565#define PCI_DEVICE_ID_COMMTECH_4228PCIE 0x0021
1566#define PCI_DEVICE_ID_COMMTECH_4222PCIE 0x0022 1566#define PCI_DEVICE_ID_COMMTECH_4222PCIE 0x0022
1567#define PCI_DEVICE_ID_BROADCOM_TRUMANAGE 0x160a 1567#define PCI_DEVICE_ID_BROADCOM_TRUMANAGE 0x160a
1568#define PCI_DEVICE_ID_AMCC_ADDIDATA_APCI7800 0x818e
1568 1569
1569#define PCI_VENDOR_ID_SUNIX 0x1fd4 1570#define PCI_VENDOR_ID_SUNIX 0x1fd4
1570#define PCI_DEVICE_ID_SUNIX_1999 0x1999 1571#define PCI_DEVICE_ID_SUNIX_1999 0x1999
@@ -1587,8 +1588,8 @@ static struct pci_serial_quirk pci_serial_quirks[] __refdata = {
1587 * ADDI-DATA GmbH communication cards <info@addi-data.com> 1588 * ADDI-DATA GmbH communication cards <info@addi-data.com>
1588 */ 1589 */
1589 { 1590 {
1590 .vendor = PCI_VENDOR_ID_ADDIDATA_OLD, 1591 .vendor = PCI_VENDOR_ID_AMCC,
1591 .device = PCI_DEVICE_ID_ADDIDATA_APCI7800, 1592 .device = PCI_DEVICE_ID_AMCC_ADDIDATA_APCI7800,
1592 .subvendor = PCI_ANY_ID, 1593 .subvendor = PCI_ANY_ID,
1593 .subdevice = PCI_ANY_ID, 1594 .subdevice = PCI_ANY_ID,
1594 .setup = addidata_apci7800_setup, 1595 .setup = addidata_apci7800_setup,
@@ -4697,8 +4698,8 @@ static struct pci_device_id serial_pci_tbl[] = {
4697 0, 4698 0,
4698 pbn_b0_1_115200 }, 4699 pbn_b0_1_115200 },
4699 4700
4700 { PCI_VENDOR_ID_ADDIDATA_OLD, 4701 { PCI_VENDOR_ID_AMCC,
4701 PCI_DEVICE_ID_ADDIDATA_APCI7800, 4702 PCI_DEVICE_ID_AMCC_ADDIDATA_APCI7800,
4702 PCI_ANY_ID, 4703 PCI_ANY_ID,
4703 PCI_ANY_ID, 4704 PCI_ANY_ID,
4704 0, 4705 0,
@@ -4797,6 +4798,12 @@ static struct pci_device_id serial_pci_tbl[] = {
4797 PCI_VENDOR_ID_IBM, 0x0299, 4798 PCI_VENDOR_ID_IBM, 0x0299,
4798 0, 0, pbn_b0_bt_2_115200 }, 4799 0, 0, pbn_b0_bt_2_115200 },
4799 4800
4801 /*
4802 * other NetMos 9835 devices are most likely handled by the
4803 * parport_serial driver, check drivers/parport/parport_serial.c
4804 * before adding them here.
4805 */
4806
4800 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9901, 4807 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9901,
4801 0xA000, 0x1000, 4808 0xA000, 0x1000,
4802 0, 0, pbn_b0_1_115200 }, 4809 0, 0, pbn_b0_1_115200 },
diff --git a/drivers/tty/serial/8250/Kconfig b/drivers/tty/serial/8250/Kconfig
index a1ba94d64885..f3b306efaa59 100644
--- a/drivers/tty/serial/8250/Kconfig
+++ b/drivers/tty/serial/8250/Kconfig
@@ -116,6 +116,8 @@ config SERIAL_8250_PCI
116 This builds standard PCI serial support. You may be able to 116 This builds standard PCI serial support. You may be able to
117 disable this feature if you only need legacy serial support. 117 disable this feature if you only need legacy serial support.
118 Saves about 9K. 118 Saves about 9K.
119 Note that serial ports on NetMos 9835 Multi-I/O cards are handled
120 by the parport_serial driver, enabled with CONFIG_PARPORT_SERIAL.
119 121
120config SERIAL_8250_HP300 122config SERIAL_8250_HP300
121 tristate 123 tristate
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index 1456673bcca0..f13624807d12 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -291,13 +291,13 @@ config SERIAL_MAX3100
291 291
292config SERIAL_MAX310X 292config SERIAL_MAX310X
293 bool "MAX310X support" 293 bool "MAX310X support"
294 depends on SPI 294 depends on SPI_MASTER
295 select SERIAL_CORE 295 select SERIAL_CORE
296 select REGMAP_SPI if SPI 296 select REGMAP_SPI if SPI_MASTER
297 default n 297 default n
298 help 298 help
299 This selects support for an advanced UART from Maxim (Dallas). 299 This selects support for an advanced UART from Maxim (Dallas).
300 Supported ICs are MAX3107, MAX3108. 300 Supported ICs are MAX3107, MAX3108, MAX3109, MAX14830.
301 Each IC contains 128 words each of receive and transmit FIFO 301 Each IC contains 128 words each of receive and transmit FIFO
302 that can be controlled through I2C or high-speed SPI. 302 that can be controlled through I2C or high-speed SPI.
303 303
@@ -1424,8 +1424,8 @@ config SERIAL_AR933X_NR_UARTS
1424 to support. 1424 to support.
1425 1425
1426config SERIAL_EFM32_UART 1426config SERIAL_EFM32_UART
1427 tristate "EFM32 UART/USART port." 1427 tristate "EFM32 UART/USART port"
1428 depends on ARCH_EFM32 1428 depends on ARM && (ARCH_EFM32 || COMPILE_TEST)
1429 select SERIAL_CORE 1429 select SERIAL_CORE
1430 help 1430 help
1431 This driver support the USART and UART ports on 1431 This driver support the USART and UART ports on
@@ -1497,6 +1497,22 @@ config SERIAL_FSL_LPUART_CONSOLE
1497 If you have enabled the lpuart serial port on the Freescale SoCs, 1497 If you have enabled the lpuart serial port on the Freescale SoCs,
1498 you can make it the console by answering Y to this option. 1498 you can make it the console by answering Y to this option.
1499 1499
1500config SERIAL_ST_ASC
1501 tristate "ST ASC serial port support"
1502 select SERIAL_CORE
1503 help
1504 This driver is for the on-chip Asychronous Serial Controller on
1505 STMicroelectronics STi SoCs.
1506 ASC is embedded in ST COMMS IP block. It supports Rx & Tx functionality.
1507 It support all industry standard baud rates.
1508
1509 If unsure, say N.
1510
1511config SERIAL_ST_ASC_CONSOLE
1512 bool "Support for console on ST ASC"
1513 depends on SERIAL_ST_ASC=y
1514 select SERIAL_CORE_CONSOLE
1515
1500endmenu 1516endmenu
1501 1517
1502endif # TTY 1518endif # TTY
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
index cf650f0cd6e4..47b679c547e9 100644
--- a/drivers/tty/serial/Makefile
+++ b/drivers/tty/serial/Makefile
@@ -65,6 +65,7 @@ obj-$(CONFIG_SERIAL_KGDB_NMI) += kgdb_nmi.o
65obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o 65obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
66obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o 66obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o
67obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o 67obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o
68obj-$(CONFIG_SERIAL_ST_ASC) += st-asc.o
68obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o 69obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o
69obj-$(CONFIG_SERIAL_QE) += ucc_uart.o 70obj-$(CONFIG_SERIAL_QE) += ucc_uart.o
70obj-$(CONFIG_SERIAL_TIMBERDALE) += timbuart.o 71obj-$(CONFIG_SERIAL_TIMBERDALE) += timbuart.o
diff --git a/drivers/tty/serial/altera_jtaguart.c b/drivers/tty/serial/altera_jtaguart.c
index c6bdb943726b..2299a34225d4 100644
--- a/drivers/tty/serial/altera_jtaguart.c
+++ b/drivers/tty/serial/altera_jtaguart.c
@@ -408,7 +408,8 @@ static struct uart_driver altera_jtaguart_driver = {
408 408
409static int altera_jtaguart_probe(struct platform_device *pdev) 409static int altera_jtaguart_probe(struct platform_device *pdev)
410{ 410{
411 struct altera_jtaguart_platform_uart *platp = pdev->dev.platform_data; 411 struct altera_jtaguart_platform_uart *platp =
412 dev_get_platdata(&pdev->dev);
412 struct uart_port *port; 413 struct uart_port *port;
413 struct resource *res_irq, *res_mem; 414 struct resource *res_irq, *res_mem;
414 int i = pdev->id; 415 int i = pdev->id;
diff --git a/drivers/tty/serial/altera_uart.c b/drivers/tty/serial/altera_uart.c
index 1d46966e2a65..fa638db9a98c 100644
--- a/drivers/tty/serial/altera_uart.c
+++ b/drivers/tty/serial/altera_uart.c
@@ -534,7 +534,7 @@ static int altera_uart_get_of_uartclk(struct platform_device *pdev,
534 534
535static int altera_uart_probe(struct platform_device *pdev) 535static int altera_uart_probe(struct platform_device *pdev)
536{ 536{
537 struct altera_uart_platform_uart *platp = pdev->dev.platform_data; 537 struct altera_uart_platform_uart *platp = dev_get_platdata(&pdev->dev);
538 struct uart_port *port; 538 struct uart_port *port;
539 struct resource *res_mem; 539 struct resource *res_mem;
540 struct resource *res_irq; 540 struct resource *res_irq;
diff --git a/drivers/tty/serial/amba-pl010.c b/drivers/tty/serial/amba-pl010.c
index c36840519527..8b90f0b6dfdf 100644
--- a/drivers/tty/serial/amba-pl010.c
+++ b/drivers/tty/serial/amba-pl010.c
@@ -721,7 +721,7 @@ static int pl010_probe(struct amba_device *dev, const struct amba_id *id)
721 uap->port.flags = UPF_BOOT_AUTOCONF; 721 uap->port.flags = UPF_BOOT_AUTOCONF;
722 uap->port.line = i; 722 uap->port.line = i;
723 uap->dev = dev; 723 uap->dev = dev;
724 uap->data = dev->dev.platform_data; 724 uap->data = dev_get_platdata(&dev->dev);
725 725
726 amba_ports[i] = uap; 726 amba_ports[i] = uap;
727 727
diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
index 28b35ad9c6cd..eb38fb851b4e 100644
--- a/drivers/tty/serial/amba-pl011.c
+++ b/drivers/tty/serial/amba-pl011.c
@@ -265,7 +265,7 @@ static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
265static void pl011_dma_probe_initcall(struct device *dev, struct uart_amba_port *uap) 265static void pl011_dma_probe_initcall(struct device *dev, struct uart_amba_port *uap)
266{ 266{
267 /* DMA is the sole user of the platform data right now */ 267 /* DMA is the sole user of the platform data right now */
268 struct amba_pl011_data *plat = uap->port.dev->platform_data; 268 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
269 struct dma_slave_config tx_conf = { 269 struct dma_slave_config tx_conf = {
270 .dst_addr = uap->port.mapbase + UART01x_DR, 270 .dst_addr = uap->port.mapbase + UART01x_DR,
271 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE, 271 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
@@ -1497,10 +1497,10 @@ static int pl011_hwinit(struct uart_port *port)
1497 uap->im = readw(uap->port.membase + UART011_IMSC); 1497 uap->im = readw(uap->port.membase + UART011_IMSC);
1498 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC); 1498 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1499 1499
1500 if (uap->port.dev->platform_data) { 1500 if (dev_get_platdata(uap->port.dev)) {
1501 struct amba_pl011_data *plat; 1501 struct amba_pl011_data *plat;
1502 1502
1503 plat = uap->port.dev->platform_data; 1503 plat = dev_get_platdata(uap->port.dev);
1504 if (plat->init) 1504 if (plat->init)
1505 plat->init(); 1505 plat->init();
1506 } 1506 }
@@ -1645,10 +1645,10 @@ static void pl011_shutdown(struct uart_port *port)
1645 /* Optionally let pins go into sleep states */ 1645 /* Optionally let pins go into sleep states */
1646 pinctrl_pm_select_sleep_state(port->dev); 1646 pinctrl_pm_select_sleep_state(port->dev);
1647 1647
1648 if (uap->port.dev->platform_data) { 1648 if (dev_get_platdata(uap->port.dev)) {
1649 struct amba_pl011_data *plat; 1649 struct amba_pl011_data *plat;
1650 1650
1651 plat = uap->port.dev->platform_data; 1651 plat = dev_get_platdata(uap->port.dev);
1652 if (plat->exit) 1652 if (plat->exit)
1653 plat->exit(); 1653 plat->exit();
1654 } 1654 }
@@ -2002,10 +2002,10 @@ static int __init pl011_console_setup(struct console *co, char *options)
2002 if (ret) 2002 if (ret)
2003 return ret; 2003 return ret;
2004 2004
2005 if (uap->port.dev->platform_data) { 2005 if (dev_get_platdata(uap->port.dev)) {
2006 struct amba_pl011_data *plat; 2006 struct amba_pl011_data *plat;
2007 2007
2008 plat = uap->port.dev->platform_data; 2008 plat = dev_get_platdata(uap->port.dev);
2009 if (plat->init) 2009 if (plat->init)
2010 plat->init(); 2010 plat->init();
2011 } 2011 }
diff --git a/drivers/tty/serial/ar933x_uart.c b/drivers/tty/serial/ar933x_uart.c
index 27f20c57abed..7aa9062153e8 100644
--- a/drivers/tty/serial/ar933x_uart.c
+++ b/drivers/tty/serial/ar933x_uart.c
@@ -629,7 +629,7 @@ static int ar933x_uart_probe(struct platform_device *pdev)
629 int id; 629 int id;
630 int ret; 630 int ret;
631 631
632 pdata = pdev->dev.platform_data; 632 pdata = dev_get_platdata(&pdev->dev);
633 if (!pdata) 633 if (!pdata)
634 return -EINVAL; 634 return -EINVAL;
635 635
@@ -703,7 +703,6 @@ static int ar933x_uart_remove(struct platform_device *pdev)
703 struct ar933x_uart_port *up; 703 struct ar933x_uart_port *up;
704 704
705 up = platform_get_drvdata(pdev); 705 up = platform_get_drvdata(pdev);
706 platform_set_drvdata(pdev, NULL);
707 706
708 if (up) { 707 if (up) {
709 uart_remove_one_port(&ar933x_uart_driver, &up->port); 708 uart_remove_one_port(&ar933x_uart_driver, &up->port);
diff --git a/drivers/tty/serial/arc_uart.c b/drivers/tty/serial/arc_uart.c
index 22f280aa4f2c..37abf6ca2f34 100644
--- a/drivers/tty/serial/arc_uart.c
+++ b/drivers/tty/serial/arc_uart.c
@@ -209,9 +209,9 @@ static void arc_serial_start_tx(struct uart_port *port)
209 arc_serial_tx_chars(uart); 209 arc_serial_tx_chars(uart);
210} 210}
211 211
212static void arc_serial_rx_chars(struct arc_uart_port *uart) 212static void arc_serial_rx_chars(struct arc_uart_port *uart, unsigned int status)
213{ 213{
214 unsigned int status, ch, flg = 0; 214 unsigned int ch, flg = 0;
215 215
216 /* 216 /*
217 * UART has 4 deep RX-FIFO. Driver's recongnition of this fact 217 * UART has 4 deep RX-FIFO. Driver's recongnition of this fact
@@ -222,11 +222,11 @@ static void arc_serial_rx_chars(struct arc_uart_port *uart)
222 * before RX-EMPTY=0, implies some sort of buffering going on in the 222 * before RX-EMPTY=0, implies some sort of buffering going on in the
223 * controller, which is indeed the Rx-FIFO. 223 * controller, which is indeed the Rx-FIFO.
224 */ 224 */
225 while (!((status = UART_GET_STATUS(uart)) & RXEMPTY)) { 225 do {
226 226 /*
227 ch = UART_GET_DATA(uart); 227 * This could be an Rx Intr for err (no data),
228 uart->port.icount.rx++; 228 * so check err and clear that Intr first
229 229 */
230 if (unlikely(status & (RXOERR | RXFERR))) { 230 if (unlikely(status & (RXOERR | RXFERR))) {
231 if (status & RXOERR) { 231 if (status & RXOERR) {
232 uart->port.icount.overrun++; 232 uart->port.icount.overrun++;
@@ -242,14 +242,17 @@ static void arc_serial_rx_chars(struct arc_uart_port *uart)
242 } else 242 } else
243 flg = TTY_NORMAL; 243 flg = TTY_NORMAL;
244 244
245 if (unlikely(uart_handle_sysrq_char(&uart->port, ch))) 245 if (status & RXEMPTY)
246 goto done; 246 continue;
247 247
248 uart_insert_char(&uart->port, status, RXOERR, ch, flg); 248 ch = UART_GET_DATA(uart);
249 uart->port.icount.rx++;
250
251 if (!(uart_handle_sysrq_char(&uart->port, ch)))
252 uart_insert_char(&uart->port, status, RXOERR, ch, flg);
249 253
250done:
251 tty_flip_buffer_push(&uart->port.state->port); 254 tty_flip_buffer_push(&uart->port.state->port);
252 } 255 } while (!((status = UART_GET_STATUS(uart)) & RXEMPTY));
253} 256}
254 257
255/* 258/*
@@ -292,11 +295,11 @@ static irqreturn_t arc_serial_isr(int irq, void *dev_id)
292 * notifications from the UART Controller. 295 * notifications from the UART Controller.
293 * To demultiplex between the two, we check the relevant bits 296 * To demultiplex between the two, we check the relevant bits
294 */ 297 */
295 if ((status & RXIENB) && !(status & RXEMPTY)) { 298 if (status & RXIENB) {
296 299
297 /* already in ISR, no need of xx_irqsave */ 300 /* already in ISR, no need of xx_irqsave */
298 spin_lock(&uart->port.lock); 301 spin_lock(&uart->port.lock);
299 arc_serial_rx_chars(uart); 302 arc_serial_rx_chars(uart, status);
300 spin_unlock(&uart->port.lock); 303 spin_unlock(&uart->port.lock);
301 } 304 }
302 305
@@ -528,7 +531,7 @@ arc_uart_init_one(struct platform_device *pdev, int dev_id)
528 unsigned long *plat_data; 531 unsigned long *plat_data;
529 struct arc_uart_port *uart = &arc_uart_ports[dev_id]; 532 struct arc_uart_port *uart = &arc_uart_ports[dev_id];
530 533
531 plat_data = ((unsigned long *)(pdev->dev.platform_data)); 534 plat_data = (unsigned long *)dev_get_platdata(&pdev->dev);
532 if (!plat_data) 535 if (!plat_data)
533 return -ENODEV; 536 return -ENODEV;
534 537
diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c
index 691265faebbe..d067285a2d20 100644
--- a/drivers/tty/serial/atmel_serial.c
+++ b/drivers/tty/serial/atmel_serial.c
@@ -39,8 +39,8 @@
39#include <linux/atmel_pdc.h> 39#include <linux/atmel_pdc.h>
40#include <linux/atmel_serial.h> 40#include <linux/atmel_serial.h>
41#include <linux/uaccess.h> 41#include <linux/uaccess.h>
42#include <linux/pinctrl/consumer.h>
43#include <linux/platform_data/atmel.h> 42#include <linux/platform_data/atmel.h>
43#include <linux/timer.h>
44 44
45#include <asm/io.h> 45#include <asm/io.h>
46#include <asm/ioctls.h> 46#include <asm/ioctls.h>
@@ -98,6 +98,7 @@ static void atmel_stop_rx(struct uart_port *port);
98#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) 98#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
99#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) 99#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
100#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR) 100#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
101#define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
101 102
102 /* PDC registers */ 103 /* PDC registers */
103#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) 104#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
@@ -140,13 +141,25 @@ struct atmel_uart_port {
140 u32 backup_imr; /* IMR saved during suspend */ 141 u32 backup_imr; /* IMR saved during suspend */
141 int break_active; /* break being received */ 142 int break_active; /* break being received */
142 143
143 short use_dma_rx; /* enable PDC receiver */ 144 bool use_dma_rx; /* enable DMA receiver */
145 bool use_pdc_rx; /* enable PDC receiver */
144 short pdc_rx_idx; /* current PDC RX buffer */ 146 short pdc_rx_idx; /* current PDC RX buffer */
145 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 147 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
146 148
147 short use_dma_tx; /* enable PDC transmitter */ 149 bool use_dma_tx; /* enable DMA transmitter */
150 bool use_pdc_tx; /* enable PDC transmitter */
148 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 151 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
149 152
153 spinlock_t lock_tx; /* port lock */
154 spinlock_t lock_rx; /* port lock */
155 struct dma_chan *chan_tx;
156 struct dma_chan *chan_rx;
157 struct dma_async_tx_descriptor *desc_tx;
158 struct dma_async_tx_descriptor *desc_rx;
159 dma_cookie_t cookie_tx;
160 dma_cookie_t cookie_rx;
161 struct scatterlist sg_tx;
162 struct scatterlist sg_rx;
150 struct tasklet_struct tasklet; 163 struct tasklet_struct tasklet;
151 unsigned int irq_status; 164 unsigned int irq_status;
152 unsigned int irq_status_prev; 165 unsigned int irq_status_prev;
@@ -155,6 +168,14 @@ struct atmel_uart_port {
155 168
156 struct serial_rs485 rs485; /* rs485 settings */ 169 struct serial_rs485 rs485; /* rs485 settings */
157 unsigned int tx_done_mask; 170 unsigned int tx_done_mask;
171 bool is_usart; /* usart or uart */
172 struct timer_list uart_timer; /* uart timer */
173 int (*prepare_rx)(struct uart_port *port);
174 int (*prepare_tx)(struct uart_port *port);
175 void (*schedule_rx)(struct uart_port *port);
176 void (*schedule_tx)(struct uart_port *port);
177 void (*release_rx)(struct uart_port *port);
178 void (*release_tx)(struct uart_port *port);
158}; 179};
159 180
160static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 181static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
@@ -181,31 +202,45 @@ to_atmel_uart_port(struct uart_port *uart)
181} 202}
182 203
183#ifdef CONFIG_SERIAL_ATMEL_PDC 204#ifdef CONFIG_SERIAL_ATMEL_PDC
184static bool atmel_use_dma_rx(struct uart_port *port) 205static bool atmel_use_pdc_rx(struct uart_port *port)
185{ 206{
186 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 207 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
187 208
188 return atmel_port->use_dma_rx; 209 return atmel_port->use_pdc_rx;
189} 210}
190 211
191static bool atmel_use_dma_tx(struct uart_port *port) 212static bool atmel_use_pdc_tx(struct uart_port *port)
192{ 213{
193 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 214 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
194 215
195 return atmel_port->use_dma_tx; 216 return atmel_port->use_pdc_tx;
196} 217}
197#else 218#else
198static bool atmel_use_dma_rx(struct uart_port *port) 219static bool atmel_use_pdc_rx(struct uart_port *port)
199{ 220{
200 return false; 221 return false;
201} 222}
202 223
203static bool atmel_use_dma_tx(struct uart_port *port) 224static bool atmel_use_pdc_tx(struct uart_port *port)
204{ 225{
205 return false; 226 return false;
206} 227}
207#endif 228#endif
208 229
230static bool atmel_use_dma_tx(struct uart_port *port)
231{
232 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
233
234 return atmel_port->use_dma_tx;
235}
236
237static bool atmel_use_dma_rx(struct uart_port *port)
238{
239 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
240
241 return atmel_port->use_dma_rx;
242}
243
209/* Enable or disable the rs485 support */ 244/* Enable or disable the rs485 support */
210void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) 245void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
211{ 246{
@@ -233,7 +268,7 @@ void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
233 mode |= ATMEL_US_USMODE_RS485; 268 mode |= ATMEL_US_USMODE_RS485;
234 } else { 269 } else {
235 dev_dbg(port->dev, "Setting UART to RS232\n"); 270 dev_dbg(port->dev, "Setting UART to RS232\n");
236 if (atmel_use_dma_tx(port)) 271 if (atmel_use_pdc_tx(port))
237 atmel_port->tx_done_mask = ATMEL_US_ENDTX | 272 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
238 ATMEL_US_TXBUFE; 273 ATMEL_US_TXBUFE;
239 else 274 else
@@ -345,7 +380,7 @@ static void atmel_stop_tx(struct uart_port *port)
345{ 380{
346 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 381 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
347 382
348 if (atmel_use_dma_tx(port)) { 383 if (atmel_use_pdc_tx(port)) {
349 /* disable PDC transmit */ 384 /* disable PDC transmit */
350 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 385 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
351 } 386 }
@@ -364,7 +399,7 @@ static void atmel_start_tx(struct uart_port *port)
364{ 399{
365 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 400 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
366 401
367 if (atmel_use_dma_tx(port)) { 402 if (atmel_use_pdc_tx(port)) {
368 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) 403 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
369 /* The transmitter is already running. Yes, we 404 /* The transmitter is already running. Yes, we
370 really need this.*/ 405 really need this.*/
@@ -390,7 +425,7 @@ static void atmel_start_rx(struct uart_port *port)
390 425
391 UART_PUT_CR(port, ATMEL_US_RXEN); 426 UART_PUT_CR(port, ATMEL_US_RXEN);
392 427
393 if (atmel_use_dma_rx(port)) { 428 if (atmel_use_pdc_rx(port)) {
394 /* enable PDC controller */ 429 /* enable PDC controller */
395 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 430 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
396 port->read_status_mask); 431 port->read_status_mask);
@@ -407,7 +442,7 @@ static void atmel_stop_rx(struct uart_port *port)
407{ 442{
408 UART_PUT_CR(port, ATMEL_US_RXDIS); 443 UART_PUT_CR(port, ATMEL_US_RXDIS);
409 444
410 if (atmel_use_dma_rx(port)) { 445 if (atmel_use_pdc_rx(port)) {
411 /* disable PDC receive */ 446 /* disable PDC receive */
412 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); 447 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
413 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 448 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
@@ -564,6 +599,372 @@ static void atmel_tx_chars(struct uart_port *port)
564 UART_PUT_IER(port, atmel_port->tx_done_mask); 599 UART_PUT_IER(port, atmel_port->tx_done_mask);
565} 600}
566 601
602static void atmel_complete_tx_dma(void *arg)
603{
604 struct atmel_uart_port *atmel_port = arg;
605 struct uart_port *port = &atmel_port->uart;
606 struct circ_buf *xmit = &port->state->xmit;
607 struct dma_chan *chan = atmel_port->chan_tx;
608 unsigned long flags;
609
610 spin_lock_irqsave(&port->lock, flags);
611
612 if (chan)
613 dmaengine_terminate_all(chan);
614 xmit->tail += sg_dma_len(&atmel_port->sg_tx);
615 xmit->tail &= UART_XMIT_SIZE - 1;
616
617 port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
618
619 spin_lock_irq(&atmel_port->lock_tx);
620 async_tx_ack(atmel_port->desc_tx);
621 atmel_port->cookie_tx = -EINVAL;
622 atmel_port->desc_tx = NULL;
623 spin_unlock_irq(&atmel_port->lock_tx);
624
625 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
626 uart_write_wakeup(port);
627
628 /* Do we really need this? */
629 if (!uart_circ_empty(xmit))
630 tasklet_schedule(&atmel_port->tasklet);
631
632 spin_unlock_irqrestore(&port->lock, flags);
633}
634
635static void atmel_release_tx_dma(struct uart_port *port)
636{
637 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
638 struct dma_chan *chan = atmel_port->chan_tx;
639
640 if (chan) {
641 dmaengine_terminate_all(chan);
642 dma_release_channel(chan);
643 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
644 DMA_MEM_TO_DEV);
645 }
646
647 atmel_port->desc_tx = NULL;
648 atmel_port->chan_tx = NULL;
649 atmel_port->cookie_tx = -EINVAL;
650}
651
652/*
653 * Called from tasklet with TXRDY interrupt is disabled.
654 */
655static void atmel_tx_dma(struct uart_port *port)
656{
657 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
658 struct circ_buf *xmit = &port->state->xmit;
659 struct dma_chan *chan = atmel_port->chan_tx;
660 struct dma_async_tx_descriptor *desc;
661 struct scatterlist *sg = &atmel_port->sg_tx;
662
663 /* Make sure we have an idle channel */
664 if (atmel_port->desc_tx != NULL)
665 return;
666
667 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
668 /*
669 * DMA is idle now.
670 * Port xmit buffer is already mapped,
671 * and it is one page... Just adjust
672 * offsets and lengths. Since it is a circular buffer,
673 * we have to transmit till the end, and then the rest.
674 * Take the port lock to get a
675 * consistent xmit buffer state.
676 */
677 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
678 sg_dma_address(sg) = (sg_dma_address(sg) &
679 ~(UART_XMIT_SIZE - 1))
680 + sg->offset;
681 sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
682 xmit->tail,
683 UART_XMIT_SIZE);
684 BUG_ON(!sg_dma_len(sg));
685
686 desc = dmaengine_prep_slave_sg(chan,
687 sg,
688 1,
689 DMA_MEM_TO_DEV,
690 DMA_PREP_INTERRUPT |
691 DMA_CTRL_ACK);
692 if (!desc) {
693 dev_err(port->dev, "Failed to send via dma!\n");
694 return;
695 }
696
697 dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
698
699 atmel_port->desc_tx = desc;
700 desc->callback = atmel_complete_tx_dma;
701 desc->callback_param = atmel_port;
702 atmel_port->cookie_tx = dmaengine_submit(desc);
703
704 } else {
705 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
706 /* DMA done, stop TX, start RX for RS485 */
707 atmel_start_rx(port);
708 }
709 }
710
711 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
712 uart_write_wakeup(port);
713}
714
715static int atmel_prepare_tx_dma(struct uart_port *port)
716{
717 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
718 dma_cap_mask_t mask;
719 struct dma_slave_config config;
720 int ret, nent;
721
722 dma_cap_zero(mask);
723 dma_cap_set(DMA_SLAVE, mask);
724
725 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
726 if (atmel_port->chan_tx == NULL)
727 goto chan_err;
728 dev_info(port->dev, "using %s for tx DMA transfers\n",
729 dma_chan_name(atmel_port->chan_tx));
730
731 spin_lock_init(&atmel_port->lock_tx);
732 sg_init_table(&atmel_port->sg_tx, 1);
733 /* UART circular tx buffer is an aligned page. */
734 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
735 sg_set_page(&atmel_port->sg_tx,
736 virt_to_page(port->state->xmit.buf),
737 UART_XMIT_SIZE,
738 (int)port->state->xmit.buf & ~PAGE_MASK);
739 nent = dma_map_sg(port->dev,
740 &atmel_port->sg_tx,
741 1,
742 DMA_MEM_TO_DEV);
743
744 if (!nent) {
745 dev_dbg(port->dev, "need to release resource of dma\n");
746 goto chan_err;
747 } else {
748 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
749 sg_dma_len(&atmel_port->sg_tx),
750 port->state->xmit.buf,
751 sg_dma_address(&atmel_port->sg_tx));
752 }
753
754 /* Configure the slave DMA */
755 memset(&config, 0, sizeof(config));
756 config.direction = DMA_MEM_TO_DEV;
757 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
758 config.dst_addr = port->mapbase + ATMEL_US_THR;
759
760 ret = dmaengine_device_control(atmel_port->chan_tx,
761 DMA_SLAVE_CONFIG,
762 (unsigned long)&config);
763 if (ret) {
764 dev_err(port->dev, "DMA tx slave configuration failed\n");
765 goto chan_err;
766 }
767
768 return 0;
769
770chan_err:
771 dev_err(port->dev, "TX channel not available, switch to pio\n");
772 atmel_port->use_dma_tx = 0;
773 if (atmel_port->chan_tx)
774 atmel_release_tx_dma(port);
775 return -EINVAL;
776}
777
778static void atmel_flip_buffer_rx_dma(struct uart_port *port,
779 char *buf, size_t count)
780{
781 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
782 struct tty_port *tport = &port->state->port;
783
784 dma_sync_sg_for_cpu(port->dev,
785 &atmel_port->sg_rx,
786 1,
787 DMA_DEV_TO_MEM);
788
789 tty_insert_flip_string(tport, buf, count);
790
791 dma_sync_sg_for_device(port->dev,
792 &atmel_port->sg_rx,
793 1,
794 DMA_DEV_TO_MEM);
795 /*
796 * Drop the lock here since it might end up calling
797 * uart_start(), which takes the lock.
798 */
799 spin_unlock(&port->lock);
800 tty_flip_buffer_push(tport);
801 spin_lock(&port->lock);
802}
803
804static void atmel_complete_rx_dma(void *arg)
805{
806 struct uart_port *port = arg;
807 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
808
809 tasklet_schedule(&atmel_port->tasklet);
810}
811
812static void atmel_release_rx_dma(struct uart_port *port)
813{
814 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
815 struct dma_chan *chan = atmel_port->chan_rx;
816
817 if (chan) {
818 dmaengine_terminate_all(chan);
819 dma_release_channel(chan);
820 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
821 DMA_DEV_TO_MEM);
822 }
823
824 atmel_port->desc_rx = NULL;
825 atmel_port->chan_rx = NULL;
826 atmel_port->cookie_rx = -EINVAL;
827
828 if (!atmel_port->is_usart)
829 del_timer_sync(&atmel_port->uart_timer);
830}
831
832static void atmel_rx_from_dma(struct uart_port *port)
833{
834 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
835 struct circ_buf *ring = &atmel_port->rx_ring;
836 struct dma_chan *chan = atmel_port->chan_rx;
837 struct dma_tx_state state;
838 enum dma_status dmastat;
839 size_t pending, count;
840
841
842 /* Reset the UART timeout early so that we don't miss one */
843 UART_PUT_CR(port, ATMEL_US_STTTO);
844 dmastat = dmaengine_tx_status(chan,
845 atmel_port->cookie_rx,
846 &state);
847 /* Restart a new tasklet if DMA status is error */
848 if (dmastat == DMA_ERROR) {
849 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
850 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
851 tasklet_schedule(&atmel_port->tasklet);
852 return;
853 }
854 /* current transfer size should no larger than dma buffer */
855 pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
856 BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
857
858 /*
859 * This will take the chars we have so far,
860 * ring->head will record the transfer size, only new bytes come
861 * will insert into the framework.
862 */
863 if (pending > ring->head) {
864 count = pending - ring->head;
865
866 atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
867
868 ring->head += count;
869 if (ring->head == sg_dma_len(&atmel_port->sg_rx))
870 ring->head = 0;
871
872 port->icount.rx += count;
873 }
874
875 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
876}
877
878static int atmel_prepare_rx_dma(struct uart_port *port)
879{
880 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
881 struct dma_async_tx_descriptor *desc;
882 dma_cap_mask_t mask;
883 struct dma_slave_config config;
884 struct circ_buf *ring;
885 int ret, nent;
886
887 ring = &atmel_port->rx_ring;
888
889 dma_cap_zero(mask);
890 dma_cap_set(DMA_CYCLIC, mask);
891
892 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
893 if (atmel_port->chan_rx == NULL)
894 goto chan_err;
895 dev_info(port->dev, "using %s for rx DMA transfers\n",
896 dma_chan_name(atmel_port->chan_rx));
897
898 spin_lock_init(&atmel_port->lock_rx);
899 sg_init_table(&atmel_port->sg_rx, 1);
900 /* UART circular rx buffer is an aligned page. */
901 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
902 sg_set_page(&atmel_port->sg_rx,
903 virt_to_page(ring->buf),
904 ATMEL_SERIAL_RINGSIZE,
905 (int)ring->buf & ~PAGE_MASK);
906 nent = dma_map_sg(port->dev,
907 &atmel_port->sg_rx,
908 1,
909 DMA_DEV_TO_MEM);
910
911 if (!nent) {
912 dev_dbg(port->dev, "need to release resource of dma\n");
913 goto chan_err;
914 } else {
915 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
916 sg_dma_len(&atmel_port->sg_rx),
917 ring->buf,
918 sg_dma_address(&atmel_port->sg_rx));
919 }
920
921 /* Configure the slave DMA */
922 memset(&config, 0, sizeof(config));
923 config.direction = DMA_DEV_TO_MEM;
924 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
925 config.src_addr = port->mapbase + ATMEL_US_RHR;
926
927 ret = dmaengine_device_control(atmel_port->chan_rx,
928 DMA_SLAVE_CONFIG,
929 (unsigned long)&config);
930 if (ret) {
931 dev_err(port->dev, "DMA rx slave configuration failed\n");
932 goto chan_err;
933 }
934 /*
935 * Prepare a cyclic dma transfer, assign 2 descriptors,
936 * each one is half ring buffer size
937 */
938 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
939 sg_dma_address(&atmel_port->sg_rx),
940 sg_dma_len(&atmel_port->sg_rx),
941 sg_dma_len(&atmel_port->sg_rx)/2,
942 DMA_DEV_TO_MEM,
943 DMA_PREP_INTERRUPT);
944 desc->callback = atmel_complete_rx_dma;
945 desc->callback_param = port;
946 atmel_port->desc_rx = desc;
947 atmel_port->cookie_rx = dmaengine_submit(desc);
948
949 return 0;
950
951chan_err:
952 dev_err(port->dev, "RX channel not available, switch to pio\n");
953 atmel_port->use_dma_rx = 0;
954 if (atmel_port->chan_rx)
955 atmel_release_rx_dma(port);
956 return -EINVAL;
957}
958
959static void atmel_uart_timer_callback(unsigned long data)
960{
961 struct uart_port *port = (void *)data;
962 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
963
964 tasklet_schedule(&atmel_port->tasklet);
965 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
966}
967
567/* 968/*
568 * receive interrupt handler. 969 * receive interrupt handler.
569 */ 970 */
@@ -572,7 +973,7 @@ atmel_handle_receive(struct uart_port *port, unsigned int pending)
572{ 973{
573 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 974 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
574 975
575 if (atmel_use_dma_rx(port)) { 976 if (atmel_use_pdc_rx(port)) {
576 /* 977 /*
577 * PDC receive. Just schedule the tasklet and let it 978 * PDC receive. Just schedule the tasklet and let it
578 * figure out the details. 979 * figure out the details.
@@ -591,6 +992,13 @@ atmel_handle_receive(struct uart_port *port, unsigned int pending)
591 atmel_pdc_rxerr(port, pending); 992 atmel_pdc_rxerr(port, pending);
592 } 993 }
593 994
995 if (atmel_use_dma_rx(port)) {
996 if (pending & ATMEL_US_TIMEOUT) {
997 UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
998 tasklet_schedule(&atmel_port->tasklet);
999 }
1000 }
1001
594 /* Interrupt receive */ 1002 /* Interrupt receive */
595 if (pending & ATMEL_US_RXRDY) 1003 if (pending & ATMEL_US_RXRDY)
596 atmel_rx_chars(port); 1004 atmel_rx_chars(port);
@@ -658,10 +1066,21 @@ static irqreturn_t atmel_interrupt(int irq, void *dev_id)
658 return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1066 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
659} 1067}
660 1068
1069static void atmel_release_tx_pdc(struct uart_port *port)
1070{
1071 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1072 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1073
1074 dma_unmap_single(port->dev,
1075 pdc->dma_addr,
1076 pdc->dma_size,
1077 DMA_TO_DEVICE);
1078}
1079
661/* 1080/*
662 * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1081 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
663 */ 1082 */
664static void atmel_tx_dma(struct uart_port *port) 1083static void atmel_tx_pdc(struct uart_port *port)
665{ 1084{
666 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1085 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
667 struct circ_buf *xmit = &port->state->xmit; 1086 struct circ_buf *xmit = &port->state->xmit;
@@ -710,6 +1129,23 @@ static void atmel_tx_dma(struct uart_port *port)
710 uart_write_wakeup(port); 1129 uart_write_wakeup(port);
711} 1130}
712 1131
1132static int atmel_prepare_tx_pdc(struct uart_port *port)
1133{
1134 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1135 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1136 struct circ_buf *xmit = &port->state->xmit;
1137
1138 pdc->buf = xmit->buf;
1139 pdc->dma_addr = dma_map_single(port->dev,
1140 pdc->buf,
1141 UART_XMIT_SIZE,
1142 DMA_TO_DEVICE);
1143 pdc->dma_size = UART_XMIT_SIZE;
1144 pdc->ofs = 0;
1145
1146 return 0;
1147}
1148
713static void atmel_rx_from_ring(struct uart_port *port) 1149static void atmel_rx_from_ring(struct uart_port *port)
714{ 1150{
715 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1151 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
@@ -778,7 +1214,26 @@ static void atmel_rx_from_ring(struct uart_port *port)
778 spin_lock(&port->lock); 1214 spin_lock(&port->lock);
779} 1215}
780 1216
781static void atmel_rx_from_dma(struct uart_port *port) 1217static void atmel_release_rx_pdc(struct uart_port *port)
1218{
1219 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1220 int i;
1221
1222 for (i = 0; i < 2; i++) {
1223 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1224
1225 dma_unmap_single(port->dev,
1226 pdc->dma_addr,
1227 pdc->dma_size,
1228 DMA_FROM_DEVICE);
1229 kfree(pdc->buf);
1230 }
1231
1232 if (!atmel_port->is_usart)
1233 del_timer_sync(&atmel_port->uart_timer);
1234}
1235
1236static void atmel_rx_from_pdc(struct uart_port *port)
782{ 1237{
783 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1238 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
784 struct tty_port *tport = &port->state->port; 1239 struct tty_port *tport = &port->state->port;
@@ -855,6 +1310,45 @@ static void atmel_rx_from_dma(struct uart_port *port)
855 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1310 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
856} 1311}
857 1312
1313static int atmel_prepare_rx_pdc(struct uart_port *port)
1314{
1315 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1316 int i;
1317
1318 for (i = 0; i < 2; i++) {
1319 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1320
1321 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1322 if (pdc->buf == NULL) {
1323 if (i != 0) {
1324 dma_unmap_single(port->dev,
1325 atmel_port->pdc_rx[0].dma_addr,
1326 PDC_BUFFER_SIZE,
1327 DMA_FROM_DEVICE);
1328 kfree(atmel_port->pdc_rx[0].buf);
1329 }
1330 atmel_port->use_pdc_rx = 0;
1331 return -ENOMEM;
1332 }
1333 pdc->dma_addr = dma_map_single(port->dev,
1334 pdc->buf,
1335 PDC_BUFFER_SIZE,
1336 DMA_FROM_DEVICE);
1337 pdc->dma_size = PDC_BUFFER_SIZE;
1338 pdc->ofs = 0;
1339 }
1340
1341 atmel_port->pdc_rx_idx = 0;
1342
1343 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1344 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1345
1346 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1347 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1348
1349 return 0;
1350}
1351
858/* 1352/*
859 * tasklet handling tty stuff outside the interrupt handler. 1353 * tasklet handling tty stuff outside the interrupt handler.
860 */ 1354 */
@@ -868,10 +1362,7 @@ static void atmel_tasklet_func(unsigned long data)
868 /* The interrupt handler does not take the lock */ 1362 /* The interrupt handler does not take the lock */
869 spin_lock(&port->lock); 1363 spin_lock(&port->lock);
870 1364
871 if (atmel_use_dma_tx(port)) 1365 atmel_port->schedule_tx(port);
872 atmel_tx_dma(port);
873 else
874 atmel_tx_chars(port);
875 1366
876 status = atmel_port->irq_status; 1367 status = atmel_port->irq_status;
877 status_change = status ^ atmel_port->irq_status_prev; 1368 status_change = status ^ atmel_port->irq_status_prev;
@@ -893,19 +1384,152 @@ static void atmel_tasklet_func(unsigned long data)
893 atmel_port->irq_status_prev = status; 1384 atmel_port->irq_status_prev = status;
894 } 1385 }
895 1386
896 if (atmel_use_dma_rx(port)) 1387 atmel_port->schedule_rx(port);
897 atmel_rx_from_dma(port);
898 else
899 atmel_rx_from_ring(port);
900 1388
901 spin_unlock(&port->lock); 1389 spin_unlock(&port->lock);
902} 1390}
903 1391
1392static int atmel_init_property(struct atmel_uart_port *atmel_port,
1393 struct platform_device *pdev)
1394{
1395 struct device_node *np = pdev->dev.of_node;
1396 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1397
1398 if (np) {
1399 /* DMA/PDC usage specification */
1400 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1401 if (of_get_property(np, "dmas", NULL)) {
1402 atmel_port->use_dma_rx = true;
1403 atmel_port->use_pdc_rx = false;
1404 } else {
1405 atmel_port->use_dma_rx = false;
1406 atmel_port->use_pdc_rx = true;
1407 }
1408 } else {
1409 atmel_port->use_dma_rx = false;
1410 atmel_port->use_pdc_rx = false;
1411 }
1412
1413 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1414 if (of_get_property(np, "dmas", NULL)) {
1415 atmel_port->use_dma_tx = true;
1416 atmel_port->use_pdc_tx = false;
1417 } else {
1418 atmel_port->use_dma_tx = false;
1419 atmel_port->use_pdc_tx = true;
1420 }
1421 } else {
1422 atmel_port->use_dma_tx = false;
1423 atmel_port->use_pdc_tx = false;
1424 }
1425
1426 } else {
1427 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1428 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1429 atmel_port->use_dma_rx = false;
1430 atmel_port->use_dma_tx = false;
1431 }
1432
1433 return 0;
1434}
1435
1436static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
1437 struct platform_device *pdev)
1438{
1439 struct device_node *np = pdev->dev.of_node;
1440 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1441
1442 if (np) {
1443 u32 rs485_delay[2];
1444 /* rs485 properties */
1445 if (of_property_read_u32_array(np, "rs485-rts-delay",
1446 rs485_delay, 2) == 0) {
1447 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1448
1449 rs485conf->delay_rts_before_send = rs485_delay[0];
1450 rs485conf->delay_rts_after_send = rs485_delay[1];
1451 rs485conf->flags = 0;
1452
1453 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1454 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1455
1456 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1457 NULL))
1458 rs485conf->flags |= SER_RS485_ENABLED;
1459 }
1460 } else {
1461 atmel_port->rs485 = pdata->rs485;
1462 }
1463
1464}
1465
1466static void atmel_set_ops(struct uart_port *port)
1467{
1468 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1469
1470 if (atmel_use_dma_rx(port)) {
1471 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1472 atmel_port->schedule_rx = &atmel_rx_from_dma;
1473 atmel_port->release_rx = &atmel_release_rx_dma;
1474 } else if (atmel_use_pdc_rx(port)) {
1475 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1476 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1477 atmel_port->release_rx = &atmel_release_rx_pdc;
1478 } else {
1479 atmel_port->prepare_rx = NULL;
1480 atmel_port->schedule_rx = &atmel_rx_from_ring;
1481 atmel_port->release_rx = NULL;
1482 }
1483
1484 if (atmel_use_dma_tx(port)) {
1485 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1486 atmel_port->schedule_tx = &atmel_tx_dma;
1487 atmel_port->release_tx = &atmel_release_tx_dma;
1488 } else if (atmel_use_pdc_tx(port)) {
1489 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1490 atmel_port->schedule_tx = &atmel_tx_pdc;
1491 atmel_port->release_tx = &atmel_release_tx_pdc;
1492 } else {
1493 atmel_port->prepare_tx = NULL;
1494 atmel_port->schedule_tx = &atmel_tx_chars;
1495 atmel_port->release_tx = NULL;
1496 }
1497}
1498
1499/*
1500 * Get ip name usart or uart
1501 */
1502static int atmel_get_ip_name(struct uart_port *port)
1503{
1504 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1505 int name = UART_GET_IP_NAME(port);
1506 int usart, uart;
1507 /* usart and uart ascii */
1508 usart = 0x55534152;
1509 uart = 0x44424755;
1510
1511 atmel_port->is_usart = false;
1512
1513 if (name == usart) {
1514 dev_dbg(port->dev, "This is usart\n");
1515 atmel_port->is_usart = true;
1516 } else if (name == uart) {
1517 dev_dbg(port->dev, "This is uart\n");
1518 atmel_port->is_usart = false;
1519 } else {
1520 dev_err(port->dev, "Not supported ip name, set to uart\n");
1521 return -EINVAL;
1522 }
1523
1524 return 0;
1525}
1526
904/* 1527/*
905 * Perform initialization and enable port for reception 1528 * Perform initialization and enable port for reception
906 */ 1529 */
907static int atmel_startup(struct uart_port *port) 1530static int atmel_startup(struct uart_port *port)
908{ 1531{
1532 struct platform_device *pdev = to_platform_device(port->dev);
909 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1533 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
910 struct tty_struct *tty = port->state->port.tty; 1534 struct tty_struct *tty = port->state->port.tty;
911 int retval; 1535 int retval;
@@ -930,53 +1554,19 @@ static int atmel_startup(struct uart_port *port)
930 /* 1554 /*
931 * Initialize DMA (if necessary) 1555 * Initialize DMA (if necessary)
932 */ 1556 */
933 if (atmel_use_dma_rx(port)) { 1557 atmel_init_property(atmel_port, pdev);
934 int i;
935
936 for (i = 0; i < 2; i++) {
937 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
938
939 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
940 if (pdc->buf == NULL) {
941 if (i != 0) {
942 dma_unmap_single(port->dev,
943 atmel_port->pdc_rx[0].dma_addr,
944 PDC_BUFFER_SIZE,
945 DMA_FROM_DEVICE);
946 kfree(atmel_port->pdc_rx[0].buf);
947 }
948 free_irq(port->irq, port);
949 return -ENOMEM;
950 }
951 pdc->dma_addr = dma_map_single(port->dev,
952 pdc->buf,
953 PDC_BUFFER_SIZE,
954 DMA_FROM_DEVICE);
955 pdc->dma_size = PDC_BUFFER_SIZE;
956 pdc->ofs = 0;
957 }
958 1558
959 atmel_port->pdc_rx_idx = 0; 1559 if (atmel_port->prepare_rx) {
960 1560 retval = atmel_port->prepare_rx(port);
961 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); 1561 if (retval < 0)
962 UART_PUT_RCR(port, PDC_BUFFER_SIZE); 1562 atmel_set_ops(port);
963
964 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
965 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
966 } 1563 }
967 if (atmel_use_dma_tx(port)) {
968 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
969 struct circ_buf *xmit = &port->state->xmit;
970 1564
971 pdc->buf = xmit->buf; 1565 if (atmel_port->prepare_tx) {
972 pdc->dma_addr = dma_map_single(port->dev, 1566 retval = atmel_port->prepare_tx(port);
973 pdc->buf, 1567 if (retval < 0)
974 UART_XMIT_SIZE, 1568 atmel_set_ops(port);
975 DMA_TO_DEVICE);
976 pdc->dma_size = UART_XMIT_SIZE;
977 pdc->ofs = 0;
978 } 1569 }
979
980 /* 1570 /*
981 * If there is a specific "open" function (to register 1571 * If there is a specific "open" function (to register
982 * control line interrupts) 1572 * control line interrupts)
@@ -1000,14 +1590,38 @@ static int atmel_startup(struct uart_port *port)
1000 /* enable xmit & rcvr */ 1590 /* enable xmit & rcvr */
1001 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 1591 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1002 1592
1003 if (atmel_use_dma_rx(port)) { 1593 if (atmel_use_pdc_rx(port)) {
1004 /* set UART timeout */ 1594 /* set UART timeout */
1005 UART_PUT_RTOR(port, PDC_RX_TIMEOUT); 1595 if (!atmel_port->is_usart) {
1006 UART_PUT_CR(port, ATMEL_US_STTTO); 1596 setup_timer(&atmel_port->uart_timer,
1007 1597 atmel_uart_timer_callback,
1008 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1598 (unsigned long)port);
1599 mod_timer(&atmel_port->uart_timer,
1600 jiffies + uart_poll_timeout(port));
1601 /* set USART timeout */
1602 } else {
1603 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1604 UART_PUT_CR(port, ATMEL_US_STTTO);
1605
1606 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1607 }
1009 /* enable PDC controller */ 1608 /* enable PDC controller */
1010 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); 1609 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
1610 } else if (atmel_use_dma_rx(port)) {
1611 /* set UART timeout */
1612 if (!atmel_port->is_usart) {
1613 setup_timer(&atmel_port->uart_timer,
1614 atmel_uart_timer_callback,
1615 (unsigned long)port);
1616 mod_timer(&atmel_port->uart_timer,
1617 jiffies + uart_poll_timeout(port));
1618 /* set USART timeout */
1619 } else {
1620 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1621 UART_PUT_CR(port, ATMEL_US_STTTO);
1622
1623 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
1624 }
1011 } else { 1625 } else {
1012 /* enable receive only */ 1626 /* enable receive only */
1013 UART_PUT_IER(port, ATMEL_US_RXRDY); 1627 UART_PUT_IER(port, ATMEL_US_RXRDY);
@@ -1031,27 +1645,10 @@ static void atmel_shutdown(struct uart_port *port)
1031 /* 1645 /*
1032 * Shut-down the DMA. 1646 * Shut-down the DMA.
1033 */ 1647 */
1034 if (atmel_use_dma_rx(port)) { 1648 if (atmel_port->release_rx)
1035 int i; 1649 atmel_port->release_rx(port);
1036 1650 if (atmel_port->release_tx)
1037 for (i = 0; i < 2; i++) { 1651 atmel_port->release_tx(port);
1038 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1039
1040 dma_unmap_single(port->dev,
1041 pdc->dma_addr,
1042 pdc->dma_size,
1043 DMA_FROM_DEVICE);
1044 kfree(pdc->buf);
1045 }
1046 }
1047 if (atmel_use_dma_tx(port)) {
1048 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1049
1050 dma_unmap_single(port->dev,
1051 pdc->dma_addr,
1052 pdc->dma_size,
1053 DMA_TO_DEVICE);
1054 }
1055 1652
1056 /* 1653 /*
1057 * Disable all interrupts, port and break condition. 1654 * Disable all interrupts, port and break condition.
@@ -1080,7 +1677,7 @@ static void atmel_flush_buffer(struct uart_port *port)
1080{ 1677{
1081 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1678 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1082 1679
1083 if (atmel_use_dma_tx(port)) { 1680 if (atmel_use_pdc_tx(port)) {
1084 UART_PUT_TCR(port, 0); 1681 UART_PUT_TCR(port, 0);
1085 atmel_port->pdc_tx.ofs = 0; 1682 atmel_port->pdc_tx.ofs = 0;
1086 } 1683 }
@@ -1193,7 +1790,7 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1193 if (termios->c_iflag & (BRKINT | PARMRK)) 1790 if (termios->c_iflag & (BRKINT | PARMRK))
1194 port->read_status_mask |= ATMEL_US_RXBRK; 1791 port->read_status_mask |= ATMEL_US_RXBRK;
1195 1792
1196 if (atmel_use_dma_rx(port)) 1793 if (atmel_use_pdc_rx(port))
1197 /* need to enable error interrupts */ 1794 /* need to enable error interrupts */
1198 UART_PUT_IER(port, port->read_status_mask); 1795 UART_PUT_IER(port, port->read_status_mask);
1199 1796
@@ -1423,38 +2020,6 @@ static struct uart_ops atmel_pops = {
1423#endif 2020#endif
1424}; 2021};
1425 2022
1426static void atmel_of_init_port(struct atmel_uart_port *atmel_port,
1427 struct device_node *np)
1428{
1429 u32 rs485_delay[2];
1430
1431 /* DMA/PDC usage specification */
1432 if (of_get_property(np, "atmel,use-dma-rx", NULL))
1433 atmel_port->use_dma_rx = 1;
1434 else
1435 atmel_port->use_dma_rx = 0;
1436 if (of_get_property(np, "atmel,use-dma-tx", NULL))
1437 atmel_port->use_dma_tx = 1;
1438 else
1439 atmel_port->use_dma_tx = 0;
1440
1441 /* rs485 properties */
1442 if (of_property_read_u32_array(np, "rs485-rts-delay",
1443 rs485_delay, 2) == 0) {
1444 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1445
1446 rs485conf->delay_rts_before_send = rs485_delay[0];
1447 rs485conf->delay_rts_after_send = rs485_delay[1];
1448 rs485conf->flags = 0;
1449
1450 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1451 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1452
1453 if (of_get_property(np, "linux,rs485-enabled-at-boot-time", NULL))
1454 rs485conf->flags |= SER_RS485_ENABLED;
1455 }
1456}
1457
1458/* 2023/*
1459 * Configure the port from the platform device resource info. 2024 * Configure the port from the platform device resource info.
1460 */ 2025 */
@@ -1463,15 +2028,12 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port,
1463{ 2028{
1464 int ret; 2029 int ret;
1465 struct uart_port *port = &atmel_port->uart; 2030 struct uart_port *port = &atmel_port->uart;
1466 struct atmel_uart_data *pdata = pdev->dev.platform_data; 2031 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1467 2032
1468 if (pdev->dev.of_node) { 2033 if (!atmel_init_property(atmel_port, pdev))
1469 atmel_of_init_port(atmel_port, pdev->dev.of_node); 2034 atmel_set_ops(port);
1470 } else { 2035
1471 atmel_port->use_dma_rx = pdata->use_dma_rx; 2036 atmel_init_rs485(atmel_port, pdev);
1472 atmel_port->use_dma_tx = pdata->use_dma_tx;
1473 atmel_port->rs485 = pdata->rs485;
1474 }
1475 2037
1476 port->iotype = UPIO_MEM; 2038 port->iotype = UPIO_MEM;
1477 port->flags = UPF_BOOT_AUTOCONF; 2039 port->flags = UPF_BOOT_AUTOCONF;
@@ -1516,7 +2078,7 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port,
1516 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ 2078 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1517 if (atmel_port->rs485.flags & SER_RS485_ENABLED) 2079 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
1518 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 2080 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
1519 else if (atmel_use_dma_tx(port)) { 2081 else if (atmel_use_pdc_tx(port)) {
1520 port->fifosize = PDC_BUFFER_SIZE; 2082 port->fifosize = PDC_BUFFER_SIZE;
1521 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2083 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
1522 } else { 2084 } else {
@@ -1664,7 +2226,7 @@ static int __init atmel_console_init(void)
1664 int ret; 2226 int ret;
1665 if (atmel_default_console_device) { 2227 if (atmel_default_console_device) {
1666 struct atmel_uart_data *pdata = 2228 struct atmel_uart_data *pdata =
1667 atmel_default_console_device->dev.platform_data; 2229 dev_get_platdata(&atmel_default_console_device->dev);
1668 int id = pdata->num; 2230 int id = pdata->num;
1669 struct atmel_uart_port *port = &atmel_ports[id]; 2231 struct atmel_uart_port *port = &atmel_ports[id];
1670 2232
@@ -1772,10 +2334,9 @@ static int atmel_serial_probe(struct platform_device *pdev)
1772{ 2334{
1773 struct atmel_uart_port *port; 2335 struct atmel_uart_port *port;
1774 struct device_node *np = pdev->dev.of_node; 2336 struct device_node *np = pdev->dev.of_node;
1775 struct atmel_uart_data *pdata = pdev->dev.platform_data; 2337 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1776 void *data; 2338 void *data;
1777 int ret = -ENODEV; 2339 int ret = -ENODEV;
1778 struct pinctrl *pinctrl;
1779 2340
1780 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2341 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
1781 2342
@@ -1809,13 +2370,7 @@ static int atmel_serial_probe(struct platform_device *pdev)
1809 if (ret) 2370 if (ret)
1810 goto err; 2371 goto err;
1811 2372
1812 pinctrl = devm_pinctrl_get_select_default(&pdev->dev); 2373 if (!atmel_use_pdc_rx(&port->uart)) {
1813 if (IS_ERR(pinctrl)) {
1814 ret = PTR_ERR(pinctrl);
1815 goto err;
1816 }
1817
1818 if (!atmel_use_dma_rx(&port->uart)) {
1819 ret = -ENOMEM; 2374 ret = -ENOMEM;
1820 data = kmalloc(sizeof(struct atmel_uart_char) 2375 data = kmalloc(sizeof(struct atmel_uart_char)
1821 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); 2376 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
@@ -1847,6 +2402,13 @@ static int atmel_serial_probe(struct platform_device *pdev)
1847 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN); 2402 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
1848 } 2403 }
1849 2404
2405 /*
2406 * Get port name of usart or uart
2407 */
2408 ret = atmel_get_ip_name(&port->uart);
2409 if (ret < 0)
2410 goto err_add_port;
2411
1850 return 0; 2412 return 0;
1851 2413
1852err_add_port: 2414err_add_port:
@@ -1868,7 +2430,6 @@ static int atmel_serial_remove(struct platform_device *pdev)
1868 int ret = 0; 2430 int ret = 0;
1869 2431
1870 device_init_wakeup(&pdev->dev, 0); 2432 device_init_wakeup(&pdev->dev, 0);
1871 platform_set_drvdata(pdev, NULL);
1872 2433
1873 ret = uart_remove_one_port(&atmel_uart, port); 2434 ret = uart_remove_one_port(&atmel_uart, port);
1874 2435
diff --git a/drivers/tty/serial/bcm63xx_uart.c b/drivers/tty/serial/bcm63xx_uart.c
index 6fa2ae77fffd..d14ba5aa2ec7 100644
--- a/drivers/tty/serial/bcm63xx_uart.c
+++ b/drivers/tty/serial/bcm63xx_uart.c
@@ -852,7 +852,6 @@ static int bcm_uart_remove(struct platform_device *pdev)
852 852
853 port = platform_get_drvdata(pdev); 853 port = platform_get_drvdata(pdev);
854 uart_remove_one_port(&bcm_uart_driver, port); 854 uart_remove_one_port(&bcm_uart_driver, port);
855 platform_set_drvdata(pdev, NULL);
856 /* mark port as free */ 855 /* mark port as free */
857 ports[pdev->id].membase = 0; 856 ports[pdev->id].membase = 0;
858 return 0; 857 return 0;
diff --git a/drivers/tty/serial/bfin_sport_uart.c b/drivers/tty/serial/bfin_sport_uart.c
index 487c173b0f72..424ed36c26f1 100644
--- a/drivers/tty/serial/bfin_sport_uart.c
+++ b/drivers/tty/serial/bfin_sport_uart.c
@@ -766,7 +766,8 @@ static int sport_uart_probe(struct platform_device *pdev)
766 } 766 }
767 767
768 ret = peripheral_request_list( 768 ret = peripheral_request_list(
769 (unsigned short *)pdev->dev.platform_data, DRV_NAME); 769 (unsigned short *)dev_get_platdata(&pdev->dev),
770 DRV_NAME);
770 if (ret) { 771 if (ret) {
771 dev_err(&pdev->dev, 772 dev_err(&pdev->dev,
772 "Fail to request SPORT peripherals\n"); 773 "Fail to request SPORT peripherals\n");
@@ -843,7 +844,7 @@ out_error_unmap:
843 iounmap(sport->port.membase); 844 iounmap(sport->port.membase);
844out_error_free_peripherals: 845out_error_free_peripherals:
845 peripheral_free_list( 846 peripheral_free_list(
846 (unsigned short *)pdev->dev.platform_data); 847 (unsigned short *)dev_get_platdata(&pdev->dev));
847out_error_free_mem: 848out_error_free_mem:
848 kfree(sport); 849 kfree(sport);
849 bfin_sport_uart_ports[pdev->id] = NULL; 850 bfin_sport_uart_ports[pdev->id] = NULL;
@@ -863,7 +864,7 @@ static int sport_uart_remove(struct platform_device *pdev)
863 uart_remove_one_port(&sport_uart_reg, &sport->port); 864 uart_remove_one_port(&sport_uart_reg, &sport->port);
864 iounmap(sport->port.membase); 865 iounmap(sport->port.membase);
865 peripheral_free_list( 866 peripheral_free_list(
866 (unsigned short *)pdev->dev.platform_data); 867 (unsigned short *)dev_get_platdata(&pdev->dev));
867 kfree(sport); 868 kfree(sport);
868 bfin_sport_uart_ports[pdev->id] = NULL; 869 bfin_sport_uart_ports[pdev->id] = NULL;
869 } 870 }
diff --git a/drivers/tty/serial/bfin_uart.c b/drivers/tty/serial/bfin_uart.c
index 26a3be7ced7d..378f3c383992 100644
--- a/drivers/tty/serial/bfin_uart.c
+++ b/drivers/tty/serial/bfin_uart.c
@@ -41,10 +41,6 @@
41# undef CONFIG_EARLY_PRINTK 41# undef CONFIG_EARLY_PRINTK
42#endif 42#endif
43 43
44#ifdef CONFIG_SERIAL_BFIN_MODULE
45# undef CONFIG_EARLY_PRINTK
46#endif
47
48/* UART name and device definitions */ 44/* UART name and device definitions */
49#define BFIN_SERIAL_DEV_NAME "ttyBF" 45#define BFIN_SERIAL_DEV_NAME "ttyBF"
50#define BFIN_SERIAL_MAJOR 204 46#define BFIN_SERIAL_MAJOR 204
@@ -1244,7 +1240,8 @@ static int bfin_serial_probe(struct platform_device *pdev)
1244 */ 1240 */
1245#endif 1241#endif
1246 ret = peripheral_request_list( 1242 ret = peripheral_request_list(
1247 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME); 1243 (unsigned short *)dev_get_platdata(&pdev->dev),
1244 DRIVER_NAME);
1248 if (ret) { 1245 if (ret) {
1249 dev_err(&pdev->dev, 1246 dev_err(&pdev->dev,
1250 "fail to request bfin serial peripherals\n"); 1247 "fail to request bfin serial peripherals\n");
@@ -1362,7 +1359,7 @@ out_error_unmap:
1362 iounmap(uart->port.membase); 1359 iounmap(uart->port.membase);
1363out_error_free_peripherals: 1360out_error_free_peripherals:
1364 peripheral_free_list( 1361 peripheral_free_list(
1365 (unsigned short *)pdev->dev.platform_data); 1362 (unsigned short *)dev_get_platdata(&pdev->dev));
1366out_error_free_mem: 1363out_error_free_mem:
1367 kfree(uart); 1364 kfree(uart);
1368 bfin_serial_ports[pdev->id] = NULL; 1365 bfin_serial_ports[pdev->id] = NULL;
@@ -1381,7 +1378,7 @@ static int bfin_serial_remove(struct platform_device *pdev)
1381 uart_remove_one_port(&bfin_serial_reg, &uart->port); 1378 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1382 iounmap(uart->port.membase); 1379 iounmap(uart->port.membase);
1383 peripheral_free_list( 1380 peripheral_free_list(
1384 (unsigned short *)pdev->dev.platform_data); 1381 (unsigned short *)dev_get_platdata(&pdev->dev));
1385 kfree(uart); 1382 kfree(uart);
1386 bfin_serial_ports[pdev->id] = NULL; 1383 bfin_serial_ports[pdev->id] = NULL;
1387 } 1384 }
@@ -1436,7 +1433,7 @@ static int bfin_earlyprintk_probe(struct platform_device *pdev)
1436 } 1433 }
1437 1434
1438 ret = peripheral_request_list( 1435 ret = peripheral_request_list(
1439 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME); 1436 (unsigned short *)dev_get_platdata(&pdev->dev), DRIVER_NAME);
1440 if (ret) { 1437 if (ret) {
1441 dev_err(&pdev->dev, 1438 dev_err(&pdev->dev,
1442 "fail to request bfin serial peripherals\n"); 1439 "fail to request bfin serial peripherals\n");
@@ -1467,7 +1464,7 @@ static int bfin_earlyprintk_probe(struct platform_device *pdev)
1467 1464
1468out_error_free_peripherals: 1465out_error_free_peripherals:
1469 peripheral_free_list( 1466 peripheral_free_list(
1470 (unsigned short *)pdev->dev.platform_data); 1467 (unsigned short *)dev_get_platdata(&pdev->dev));
1471 1468
1472 return ret; 1469 return ret;
1473} 1470}
diff --git a/drivers/tty/serial/clps711x.c b/drivers/tty/serial/clps711x.c
index bfb17968c8db..7e4e4088471c 100644
--- a/drivers/tty/serial/clps711x.c
+++ b/drivers/tty/serial/clps711x.c
@@ -438,8 +438,7 @@ static int uart_clps711x_probe(struct platform_device *pdev)
438 s->uart_clk = devm_clk_get(&pdev->dev, "uart"); 438 s->uart_clk = devm_clk_get(&pdev->dev, "uart");
439 if (IS_ERR(s->uart_clk)) { 439 if (IS_ERR(s->uart_clk)) {
440 dev_err(&pdev->dev, "Can't get UART clocks\n"); 440 dev_err(&pdev->dev, "Can't get UART clocks\n");
441 ret = PTR_ERR(s->uart_clk); 441 return PTR_ERR(s->uart_clk);
442 goto err_out;
443 } 442 }
444 443
445 s->uart.owner = THIS_MODULE; 444 s->uart.owner = THIS_MODULE;
@@ -461,7 +460,7 @@ static int uart_clps711x_probe(struct platform_device *pdev)
461 if (ret) { 460 if (ret) {
462 dev_err(&pdev->dev, "Registering UART driver failed\n"); 461 dev_err(&pdev->dev, "Registering UART driver failed\n");
463 devm_clk_put(&pdev->dev, s->uart_clk); 462 devm_clk_put(&pdev->dev, s->uart_clk);
464 goto err_out; 463 return ret;
465 } 464 }
466 465
467 for (i = 0; i < UART_CLPS711X_NR; i++) { 466 for (i = 0; i < UART_CLPS711X_NR; i++) {
@@ -478,11 +477,6 @@ static int uart_clps711x_probe(struct platform_device *pdev)
478 } 477 }
479 478
480 return 0; 479 return 0;
481
482err_out:
483 platform_set_drvdata(pdev, NULL);
484
485 return ret;
486} 480}
487 481
488static int uart_clps711x_remove(struct platform_device *pdev) 482static int uart_clps711x_remove(struct platform_device *pdev)
@@ -495,7 +489,6 @@ static int uart_clps711x_remove(struct platform_device *pdev)
495 489
496 devm_clk_put(&pdev->dev, s->uart_clk); 490 devm_clk_put(&pdev->dev, s->uart_clk);
497 uart_unregister_driver(&s->uart); 491 uart_unregister_driver(&s->uart);
498 platform_set_drvdata(pdev, NULL);
499 492
500 return 0; 493 return 0;
501} 494}
diff --git a/drivers/tty/serial/efm32-uart.c b/drivers/tty/serial/efm32-uart.c
index 7d199c8e1a75..98adaa1c054f 100644
--- a/drivers/tty/serial/efm32-uart.c
+++ b/drivers/tty/serial/efm32-uart.c
@@ -698,6 +698,7 @@ static int efm32_uart_probe(struct platform_device *pdev)
698{ 698{
699 struct efm32_uart_port *efm_port; 699 struct efm32_uart_port *efm_port;
700 struct resource *res; 700 struct resource *res;
701 unsigned int line;
701 int ret; 702 int ret;
702 703
703 efm_port = kzalloc(sizeof(*efm_port), GFP_KERNEL); 704 efm_port = kzalloc(sizeof(*efm_port), GFP_KERNEL);
@@ -750,18 +751,21 @@ static int efm32_uart_probe(struct platform_device *pdev)
750 751
751 if (pdata) 752 if (pdata)
752 efm_port->pdata = *pdata; 753 efm_port->pdata = *pdata;
753 } 754 } else if (ret < 0)
755 goto err_probe_dt;
756
757 line = efm_port->port.line;
754 758
755 if (efm_port->port.line >= 0 && 759 if (line >= 0 && line < ARRAY_SIZE(efm32_uart_ports))
756 efm_port->port.line < ARRAY_SIZE(efm32_uart_ports)) 760 efm32_uart_ports[line] = efm_port;
757 efm32_uart_ports[efm_port->port.line] = efm_port;
758 761
759 ret = uart_add_one_port(&efm32_uart_reg, &efm_port->port); 762 ret = uart_add_one_port(&efm32_uart_reg, &efm_port->port);
760 if (ret) { 763 if (ret) {
761 dev_dbg(&pdev->dev, "failed to add port: %d\n", ret); 764 dev_dbg(&pdev->dev, "failed to add port: %d\n", ret);
762 765
763 if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(efm32_uart_ports)) 766 if (line >= 0 && line < ARRAY_SIZE(efm32_uart_ports))
764 efm32_uart_ports[pdev->id] = NULL; 767 efm32_uart_ports[line] = NULL;
768err_probe_dt:
765err_get_rxirq: 769err_get_rxirq:
766err_too_small: 770err_too_small:
767err_get_base: 771err_get_base:
@@ -777,20 +781,19 @@ err_get_base:
777static int efm32_uart_remove(struct platform_device *pdev) 781static int efm32_uart_remove(struct platform_device *pdev)
778{ 782{
779 struct efm32_uart_port *efm_port = platform_get_drvdata(pdev); 783 struct efm32_uart_port *efm_port = platform_get_drvdata(pdev);
780 784 unsigned int line = efm_port->port.line;
781 platform_set_drvdata(pdev, NULL);
782 785
783 uart_remove_one_port(&efm32_uart_reg, &efm_port->port); 786 uart_remove_one_port(&efm32_uart_reg, &efm_port->port);
784 787
785 if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(efm32_uart_ports)) 788 if (line >= 0 && line < ARRAY_SIZE(efm32_uart_ports))
786 efm32_uart_ports[pdev->id] = NULL; 789 efm32_uart_ports[line] = NULL;
787 790
788 kfree(efm_port); 791 kfree(efm_port);
789 792
790 return 0; 793 return 0;
791} 794}
792 795
793static struct of_device_id efm32_uart_dt_ids[] = { 796static const struct of_device_id efm32_uart_dt_ids[] = {
794 { 797 {
795 .compatible = "efm32,uart", 798 .compatible = "efm32,uart",
796 }, { 799 }, {
diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
index 263cfaabe9e2..8978dc9a58b7 100644
--- a/drivers/tty/serial/fsl_lpuart.c
+++ b/drivers/tty/serial/fsl_lpuart.c
@@ -342,8 +342,10 @@ static void lpuart_break_ctl(struct uart_port *port, int break_state)
342static void lpuart_setup_watermark(struct lpuart_port *sport) 342static void lpuart_setup_watermark(struct lpuart_port *sport)
343{ 343{
344 unsigned char val, cr2; 344 unsigned char val, cr2;
345 unsigned char cr2_saved;
345 346
346 cr2 = readb(sport->port.membase + UARTCR2); 347 cr2 = readb(sport->port.membase + UARTCR2);
348 cr2_saved = cr2;
347 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE | 349 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
348 UARTCR2_RIE | UARTCR2_RE); 350 UARTCR2_RIE | UARTCR2_RE);
349 writeb(cr2, sport->port.membase + UARTCR2); 351 writeb(cr2, sport->port.membase + UARTCR2);
@@ -366,6 +368,9 @@ static void lpuart_setup_watermark(struct lpuart_port *sport)
366 368
367 writeb(2, sport->port.membase + UARTTWFIFO); 369 writeb(2, sport->port.membase + UARTTWFIFO);
368 writeb(1, sport->port.membase + UARTRWFIFO); 370 writeb(1, sport->port.membase + UARTRWFIFO);
371
372 /* Restore cr2 */
373 writeb(cr2_saved, sport->port.membase + UARTCR2);
369} 374}
370 375
371static int lpuart_startup(struct uart_port *port) 376static int lpuart_startup(struct uart_port *port)
@@ -858,7 +863,7 @@ static int __init lpuart_serial_init(void)
858 if (ret) 863 if (ret)
859 uart_unregister_driver(&lpuart_reg); 864 uart_unregister_driver(&lpuart_reg);
860 865
861 return 0; 866 return ret;
862} 867}
863 868
864static void __exit lpuart_serial_exit(void) 869static void __exit lpuart_serial_exit(void)
diff --git a/drivers/tty/serial/ifx6x60.c b/drivers/tty/serial/ifx6x60.c
index 8b1534c424af..af286e6713eb 100644
--- a/drivers/tty/serial/ifx6x60.c
+++ b/drivers/tty/serial/ifx6x60.c
@@ -1008,7 +1008,7 @@ static int ifx_spi_spi_probe(struct spi_device *spi)
1008 return -ENODEV; 1008 return -ENODEV;
1009 } 1009 }
1010 1010
1011 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data; 1011 pl_data = (struct ifx_modem_platform_data *)dev_get_platdata(&spi->dev);
1012 if (!pl_data) { 1012 if (!pl_data) {
1013 dev_err(&spi->dev, "missing platform data!"); 1013 dev_err(&spi->dev, "missing platform data!");
1014 return -ENODEV; 1014 return -ENODEV;
diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 415cec62073f..a5f32c7c6c66 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -47,11 +47,12 @@
47#include <linux/slab.h> 47#include <linux/slab.h>
48#include <linux/of.h> 48#include <linux/of.h>
49#include <linux/of_device.h> 49#include <linux/of_device.h>
50#include <linux/pinctrl/consumer.h>
51#include <linux/io.h> 50#include <linux/io.h>
51#include <linux/dma-mapping.h>
52 52
53#include <asm/irq.h> 53#include <asm/irq.h>
54#include <linux/platform_data/serial-imx.h> 54#include <linux/platform_data/serial-imx.h>
55#include <linux/platform_data/dma-imx.h>
55 56
56/* Register definitions */ 57/* Register definitions */
57#define URXD0 0x0 /* Receiver Register */ 58#define URXD0 0x0 /* Receiver Register */
@@ -83,6 +84,7 @@
83#define UCR1_ADBR (1<<14) /* Auto detect baud rate */ 84#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
84#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */ 85#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
85#define UCR1_IDEN (1<<12) /* Idle condition interrupt */ 86#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
87#define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
86#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */ 88#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
87#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */ 89#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
88#define UCR1_IREN (1<<7) /* Infrared interface enable */ 90#define UCR1_IREN (1<<7) /* Infrared interface enable */
@@ -91,6 +93,7 @@
91#define UCR1_SNDBRK (1<<4) /* Send break */ 93#define UCR1_SNDBRK (1<<4) /* Send break */
92#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */ 94#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
93#define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */ 95#define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
96#define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
94#define UCR1_DOZE (1<<1) /* Doze */ 97#define UCR1_DOZE (1<<1) /* Doze */
95#define UCR1_UARTEN (1<<0) /* UART enabled */ 98#define UCR1_UARTEN (1<<0) /* UART enabled */
96#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */ 99#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
@@ -126,6 +129,7 @@
126#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */ 129#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
127#define UCR4_WKEN (1<<7) /* Wake interrupt enable */ 130#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
128#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */ 131#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
132#define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
129#define UCR4_IRSC (1<<5) /* IR special case */ 133#define UCR4_IRSC (1<<5) /* IR special case */
130#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */ 134#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
131#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */ 135#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
@@ -187,6 +191,7 @@
187enum imx_uart_type { 191enum imx_uart_type {
188 IMX1_UART, 192 IMX1_UART,
189 IMX21_UART, 193 IMX21_UART,
194 IMX6Q_UART,
190}; 195};
191 196
192/* device type dependent stuff */ 197/* device type dependent stuff */
@@ -209,6 +214,19 @@ struct imx_port {
209 struct clk *clk_ipg; 214 struct clk *clk_ipg;
210 struct clk *clk_per; 215 struct clk *clk_per;
211 const struct imx_uart_data *devdata; 216 const struct imx_uart_data *devdata;
217
218 /* DMA fields */
219 unsigned int dma_is_inited:1;
220 unsigned int dma_is_enabled:1;
221 unsigned int dma_is_rxing:1;
222 unsigned int dma_is_txing:1;
223 struct dma_chan *dma_chan_rx, *dma_chan_tx;
224 struct scatterlist rx_sgl, tx_sgl[2];
225 void *rx_buf;
226 unsigned int rx_bytes, tx_bytes;
227 struct work_struct tsk_dma_rx, tsk_dma_tx;
228 unsigned int dma_tx_nents;
229 wait_queue_head_t dma_wait;
212}; 230};
213 231
214struct imx_port_ucrs { 232struct imx_port_ucrs {
@@ -232,6 +250,10 @@ static struct imx_uart_data imx_uart_devdata[] = {
232 .uts_reg = IMX21_UTS, 250 .uts_reg = IMX21_UTS,
233 .devtype = IMX21_UART, 251 .devtype = IMX21_UART,
234 }, 252 },
253 [IMX6Q_UART] = {
254 .uts_reg = IMX21_UTS,
255 .devtype = IMX6Q_UART,
256 },
235}; 257};
236 258
237static struct platform_device_id imx_uart_devtype[] = { 259static struct platform_device_id imx_uart_devtype[] = {
@@ -242,12 +264,16 @@ static struct platform_device_id imx_uart_devtype[] = {
242 .name = "imx21-uart", 264 .name = "imx21-uart",
243 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART], 265 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART],
244 }, { 266 }, {
267 .name = "imx6q-uart",
268 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX6Q_UART],
269 }, {
245 /* sentinel */ 270 /* sentinel */
246 } 271 }
247}; 272};
248MODULE_DEVICE_TABLE(platform, imx_uart_devtype); 273MODULE_DEVICE_TABLE(platform, imx_uart_devtype);
249 274
250static struct of_device_id imx_uart_dt_ids[] = { 275static struct of_device_id imx_uart_dt_ids[] = {
276 { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
251 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], }, 277 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
252 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], }, 278 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
253 { /* sentinel */ } 279 { /* sentinel */ }
@@ -269,6 +295,10 @@ static inline int is_imx21_uart(struct imx_port *sport)
269 return sport->devdata->devtype == IMX21_UART; 295 return sport->devdata->devtype == IMX21_UART;
270} 296}
271 297
298static inline int is_imx6q_uart(struct imx_port *sport)
299{
300 return sport->devdata->devtype == IMX6Q_UART;
301}
272/* 302/*
273 * Save and restore functions for UCR1, UCR2 and UCR3 registers 303 * Save and restore functions for UCR1, UCR2 and UCR3 registers
274 */ 304 */
@@ -387,6 +417,13 @@ static void imx_stop_tx(struct uart_port *port)
387 return; 417 return;
388 } 418 }
389 419
420 /*
421 * We are maybe in the SMP context, so if the DMA TX thread is running
422 * on other cpu, we have to wait for it to finish.
423 */
424 if (sport->dma_is_enabled && sport->dma_is_txing)
425 return;
426
390 temp = readl(sport->port.membase + UCR1); 427 temp = readl(sport->port.membase + UCR1);
391 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1); 428 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
392} 429}
@@ -399,6 +436,13 @@ static void imx_stop_rx(struct uart_port *port)
399 struct imx_port *sport = (struct imx_port *)port; 436 struct imx_port *sport = (struct imx_port *)port;
400 unsigned long temp; 437 unsigned long temp;
401 438
439 /*
440 * We are maybe in the SMP context, so if the DMA TX thread is running
441 * on other cpu, we have to wait for it to finish.
442 */
443 if (sport->dma_is_enabled && sport->dma_is_rxing)
444 return;
445
402 temp = readl(sport->port.membase + UCR2); 446 temp = readl(sport->port.membase + UCR2);
403 writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2); 447 writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2);
404} 448}
@@ -434,6 +478,95 @@ static inline void imx_transmit_buffer(struct imx_port *sport)
434 imx_stop_tx(&sport->port); 478 imx_stop_tx(&sport->port);
435} 479}
436 480
481static void dma_tx_callback(void *data)
482{
483 struct imx_port *sport = data;
484 struct scatterlist *sgl = &sport->tx_sgl[0];
485 struct circ_buf *xmit = &sport->port.state->xmit;
486 unsigned long flags;
487
488 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
489
490 sport->dma_is_txing = 0;
491
492 /* update the stat */
493 spin_lock_irqsave(&sport->port.lock, flags);
494 xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
495 sport->port.icount.tx += sport->tx_bytes;
496 spin_unlock_irqrestore(&sport->port.lock, flags);
497
498 dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
499
500 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
501 uart_write_wakeup(&sport->port);
502
503 if (waitqueue_active(&sport->dma_wait)) {
504 wake_up(&sport->dma_wait);
505 dev_dbg(sport->port.dev, "exit in %s.\n", __func__);
506 return;
507 }
508
509 schedule_work(&sport->tsk_dma_tx);
510}
511
512static void dma_tx_work(struct work_struct *w)
513{
514 struct imx_port *sport = container_of(w, struct imx_port, tsk_dma_tx);
515 struct circ_buf *xmit = &sport->port.state->xmit;
516 struct scatterlist *sgl = sport->tx_sgl;
517 struct dma_async_tx_descriptor *desc;
518 struct dma_chan *chan = sport->dma_chan_tx;
519 struct device *dev = sport->port.dev;
520 enum dma_status status;
521 unsigned long flags;
522 int ret;
523
524 status = chan->device->device_tx_status(chan, (dma_cookie_t)0, NULL);
525 if (DMA_IN_PROGRESS == status)
526 return;
527
528 spin_lock_irqsave(&sport->port.lock, flags);
529 sport->tx_bytes = uart_circ_chars_pending(xmit);
530 if (sport->tx_bytes == 0) {
531 spin_unlock_irqrestore(&sport->port.lock, flags);
532 return;
533 }
534
535 if (xmit->tail > xmit->head) {
536 sport->dma_tx_nents = 2;
537 sg_init_table(sgl, 2);
538 sg_set_buf(sgl, xmit->buf + xmit->tail,
539 UART_XMIT_SIZE - xmit->tail);
540 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
541 } else {
542 sport->dma_tx_nents = 1;
543 sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
544 }
545 spin_unlock_irqrestore(&sport->port.lock, flags);
546
547 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
548 if (ret == 0) {
549 dev_err(dev, "DMA mapping error for TX.\n");
550 return;
551 }
552 desc = dmaengine_prep_slave_sg(chan, sgl, sport->dma_tx_nents,
553 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
554 if (!desc) {
555 dev_err(dev, "We cannot prepare for the TX slave dma!\n");
556 return;
557 }
558 desc->callback = dma_tx_callback;
559 desc->callback_param = sport;
560
561 dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
562 uart_circ_chars_pending(xmit));
563 /* fire it */
564 sport->dma_is_txing = 1;
565 dmaengine_submit(desc);
566 dma_async_issue_pending(chan);
567 return;
568}
569
437/* 570/*
438 * interrupts disabled on entry 571 * interrupts disabled on entry
439 */ 572 */
@@ -460,8 +593,10 @@ static void imx_start_tx(struct uart_port *port)
460 temp |= UCR4_OREN; 593 temp |= UCR4_OREN;
461 writel(temp, sport->port.membase + UCR4); 594 writel(temp, sport->port.membase + UCR4);
462 595
463 temp = readl(sport->port.membase + UCR1); 596 if (!sport->dma_is_enabled) {
464 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1); 597 temp = readl(sport->port.membase + UCR1);
598 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
599 }
465 600
466 if (USE_IRDA(sport)) { 601 if (USE_IRDA(sport)) {
467 temp = readl(sport->port.membase + UCR1); 602 temp = readl(sport->port.membase + UCR1);
@@ -473,6 +608,15 @@ static void imx_start_tx(struct uart_port *port)
473 writel(temp, sport->port.membase + UCR4); 608 writel(temp, sport->port.membase + UCR4);
474 } 609 }
475 610
611 if (sport->dma_is_enabled) {
612 /*
613 * We may in the interrupt context, so arise a work_struct to
614 * do the real job.
615 */
616 schedule_work(&sport->tsk_dma_tx);
617 return;
618 }
619
476 if (readl(sport->port.membase + uts_reg(sport)) & UTS_TXEMPTY) 620 if (readl(sport->port.membase + uts_reg(sport)) & UTS_TXEMPTY)
477 imx_transmit_buffer(sport); 621 imx_transmit_buffer(sport);
478} 622}
@@ -588,6 +732,28 @@ out:
588 return IRQ_HANDLED; 732 return IRQ_HANDLED;
589} 733}
590 734
735/*
736 * If the RXFIFO is filled with some data, and then we
737 * arise a DMA operation to receive them.
738 */
739static void imx_dma_rxint(struct imx_port *sport)
740{
741 unsigned long temp;
742
743 temp = readl(sport->port.membase + USR2);
744 if ((temp & USR2_RDR) && !sport->dma_is_rxing) {
745 sport->dma_is_rxing = 1;
746
747 /* disable the `Recerver Ready Interrrupt` */
748 temp = readl(sport->port.membase + UCR1);
749 temp &= ~(UCR1_RRDYEN);
750 writel(temp, sport->port.membase + UCR1);
751
752 /* tell the DMA to receive the data. */
753 schedule_work(&sport->tsk_dma_rx);
754 }
755}
756
591static irqreturn_t imx_int(int irq, void *dev_id) 757static irqreturn_t imx_int(int irq, void *dev_id)
592{ 758{
593 struct imx_port *sport = dev_id; 759 struct imx_port *sport = dev_id;
@@ -596,8 +762,12 @@ static irqreturn_t imx_int(int irq, void *dev_id)
596 762
597 sts = readl(sport->port.membase + USR1); 763 sts = readl(sport->port.membase + USR1);
598 764
599 if (sts & USR1_RRDY) 765 if (sts & USR1_RRDY) {
600 imx_rxint(irq, dev_id); 766 if (sport->dma_is_enabled)
767 imx_dma_rxint(sport);
768 else
769 imx_rxint(irq, dev_id);
770 }
601 771
602 if (sts & USR1_TRDY && 772 if (sts & USR1_TRDY &&
603 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN) 773 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
@@ -654,7 +824,8 @@ static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
654 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS; 824 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
655 825
656 if (mctrl & TIOCM_RTS) 826 if (mctrl & TIOCM_RTS)
657 temp |= UCR2_CTS; 827 if (!sport->dma_is_enabled)
828 temp |= UCR2_CTS;
658 829
659 writel(temp, sport->port.membase + UCR2); 830 writel(temp, sport->port.membase + UCR2);
660} 831}
@@ -693,6 +864,226 @@ static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
693 return 0; 864 return 0;
694} 865}
695 866
867#define RX_BUF_SIZE (PAGE_SIZE)
868static int start_rx_dma(struct imx_port *sport);
869static void dma_rx_work(struct work_struct *w)
870{
871 struct imx_port *sport = container_of(w, struct imx_port, tsk_dma_rx);
872 struct tty_port *port = &sport->port.state->port;
873
874 if (sport->rx_bytes) {
875 tty_insert_flip_string(port, sport->rx_buf, sport->rx_bytes);
876 tty_flip_buffer_push(port);
877 sport->rx_bytes = 0;
878 }
879
880 if (sport->dma_is_rxing)
881 start_rx_dma(sport);
882}
883
884static void imx_rx_dma_done(struct imx_port *sport)
885{
886 unsigned long temp;
887
888 /* Enable this interrupt when the RXFIFO is empty. */
889 temp = readl(sport->port.membase + UCR1);
890 temp |= UCR1_RRDYEN;
891 writel(temp, sport->port.membase + UCR1);
892
893 sport->dma_is_rxing = 0;
894
895 /* Is the shutdown waiting for us? */
896 if (waitqueue_active(&sport->dma_wait))
897 wake_up(&sport->dma_wait);
898}
899
900/*
901 * There are three kinds of RX DMA interrupts(such as in the MX6Q):
902 * [1] the RX DMA buffer is full.
903 * [2] the Aging timer expires(wait for 8 bytes long)
904 * [3] the Idle Condition Detect(enabled the UCR4_IDDMAEN).
905 *
906 * The [2] is trigger when a character was been sitting in the FIFO
907 * meanwhile [3] can wait for 32 bytes long when the RX line is
908 * on IDLE state and RxFIFO is empty.
909 */
910static void dma_rx_callback(void *data)
911{
912 struct imx_port *sport = data;
913 struct dma_chan *chan = sport->dma_chan_rx;
914 struct scatterlist *sgl = &sport->rx_sgl;
915 struct dma_tx_state state;
916 enum dma_status status;
917 unsigned int count;
918
919 /* unmap it first */
920 dma_unmap_sg(sport->port.dev, sgl, 1, DMA_FROM_DEVICE);
921
922 status = chan->device->device_tx_status(chan, (dma_cookie_t)0, &state);
923 count = RX_BUF_SIZE - state.residue;
924 dev_dbg(sport->port.dev, "We get %d bytes.\n", count);
925
926 if (count) {
927 sport->rx_bytes = count;
928 schedule_work(&sport->tsk_dma_rx);
929 } else
930 imx_rx_dma_done(sport);
931}
932
933static int start_rx_dma(struct imx_port *sport)
934{
935 struct scatterlist *sgl = &sport->rx_sgl;
936 struct dma_chan *chan = sport->dma_chan_rx;
937 struct device *dev = sport->port.dev;
938 struct dma_async_tx_descriptor *desc;
939 int ret;
940
941 sg_init_one(sgl, sport->rx_buf, RX_BUF_SIZE);
942 ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
943 if (ret == 0) {
944 dev_err(dev, "DMA mapping error for RX.\n");
945 return -EINVAL;
946 }
947 desc = dmaengine_prep_slave_sg(chan, sgl, 1, DMA_DEV_TO_MEM,
948 DMA_PREP_INTERRUPT);
949 if (!desc) {
950 dev_err(dev, "We cannot prepare for the RX slave dma!\n");
951 return -EINVAL;
952 }
953 desc->callback = dma_rx_callback;
954 desc->callback_param = sport;
955
956 dev_dbg(dev, "RX: prepare for the DMA.\n");
957 dmaengine_submit(desc);
958 dma_async_issue_pending(chan);
959 return 0;
960}
961
962static void imx_uart_dma_exit(struct imx_port *sport)
963{
964 if (sport->dma_chan_rx) {
965 dma_release_channel(sport->dma_chan_rx);
966 sport->dma_chan_rx = NULL;
967
968 kfree(sport->rx_buf);
969 sport->rx_buf = NULL;
970 }
971
972 if (sport->dma_chan_tx) {
973 dma_release_channel(sport->dma_chan_tx);
974 sport->dma_chan_tx = NULL;
975 }
976
977 sport->dma_is_inited = 0;
978}
979
980static int imx_uart_dma_init(struct imx_port *sport)
981{
982 struct dma_slave_config slave_config;
983 struct device *dev = sport->port.dev;
984 int ret;
985
986 /* Prepare for RX : */
987 sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
988 if (!sport->dma_chan_rx) {
989 dev_dbg(dev, "cannot get the DMA channel.\n");
990 ret = -EINVAL;
991 goto err;
992 }
993
994 slave_config.direction = DMA_DEV_TO_MEM;
995 slave_config.src_addr = sport->port.mapbase + URXD0;
996 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
997 slave_config.src_maxburst = RXTL;
998 ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
999 if (ret) {
1000 dev_err(dev, "error in RX dma configuration.\n");
1001 goto err;
1002 }
1003
1004 sport->rx_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
1005 if (!sport->rx_buf) {
1006 dev_err(dev, "cannot alloc DMA buffer.\n");
1007 ret = -ENOMEM;
1008 goto err;
1009 }
1010 sport->rx_bytes = 0;
1011
1012 /* Prepare for TX : */
1013 sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1014 if (!sport->dma_chan_tx) {
1015 dev_err(dev, "cannot get the TX DMA channel!\n");
1016 ret = -EINVAL;
1017 goto err;
1018 }
1019
1020 slave_config.direction = DMA_MEM_TO_DEV;
1021 slave_config.dst_addr = sport->port.mapbase + URTX0;
1022 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1023 slave_config.dst_maxburst = TXTL;
1024 ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
1025 if (ret) {
1026 dev_err(dev, "error in TX dma configuration.");
1027 goto err;
1028 }
1029
1030 sport->dma_is_inited = 1;
1031
1032 return 0;
1033err:
1034 imx_uart_dma_exit(sport);
1035 return ret;
1036}
1037
1038static void imx_enable_dma(struct imx_port *sport)
1039{
1040 unsigned long temp;
1041 struct tty_port *port = &sport->port.state->port;
1042
1043 port->low_latency = 1;
1044 INIT_WORK(&sport->tsk_dma_tx, dma_tx_work);
1045 INIT_WORK(&sport->tsk_dma_rx, dma_rx_work);
1046 init_waitqueue_head(&sport->dma_wait);
1047
1048 /* set UCR1 */
1049 temp = readl(sport->port.membase + UCR1);
1050 temp |= UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN |
1051 /* wait for 32 idle frames for IDDMA interrupt */
1052 UCR1_ICD_REG(3);
1053 writel(temp, sport->port.membase + UCR1);
1054
1055 /* set UCR4 */
1056 temp = readl(sport->port.membase + UCR4);
1057 temp |= UCR4_IDDMAEN;
1058 writel(temp, sport->port.membase + UCR4);
1059
1060 sport->dma_is_enabled = 1;
1061}
1062
1063static void imx_disable_dma(struct imx_port *sport)
1064{
1065 unsigned long temp;
1066 struct tty_port *port = &sport->port.state->port;
1067
1068 /* clear UCR1 */
1069 temp = readl(sport->port.membase + UCR1);
1070 temp &= ~(UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN);
1071 writel(temp, sport->port.membase + UCR1);
1072
1073 /* clear UCR2 */
1074 temp = readl(sport->port.membase + UCR2);
1075 temp &= ~(UCR2_CTSC | UCR2_CTS);
1076 writel(temp, sport->port.membase + UCR2);
1077
1078 /* clear UCR4 */
1079 temp = readl(sport->port.membase + UCR4);
1080 temp &= ~UCR4_IDDMAEN;
1081 writel(temp, sport->port.membase + UCR4);
1082
1083 sport->dma_is_enabled = 0;
1084 port->low_latency = 0;
1085}
1086
696/* half the RX buffer size */ 1087/* half the RX buffer size */
697#define CTSTL 16 1088#define CTSTL 16
698 1089
@@ -702,15 +1093,13 @@ static int imx_startup(struct uart_port *port)
702 int retval; 1093 int retval;
703 unsigned long flags, temp; 1094 unsigned long flags, temp;
704 1095
705 if (!uart_console(port)) { 1096 retval = clk_prepare_enable(sport->clk_per);
706 retval = clk_prepare_enable(sport->clk_per); 1097 if (retval)
707 if (retval) 1098 goto error_out1;
708 goto error_out1; 1099 retval = clk_prepare_enable(sport->clk_ipg);
709 retval = clk_prepare_enable(sport->clk_ipg); 1100 if (retval) {
710 if (retval) { 1101 clk_disable_unprepare(sport->clk_per);
711 clk_disable_unprepare(sport->clk_per); 1102 goto error_out1;
712 goto error_out1;
713 }
714 } 1103 }
715 1104
716 imx_setup_ufcr(sport, 0); 1105 imx_setup_ufcr(sport, 0);
@@ -803,7 +1192,7 @@ static int imx_startup(struct uart_port *port)
803 } 1192 }
804 } 1193 }
805 1194
806 if (is_imx21_uart(sport)) { 1195 if (!is_imx1_uart(sport)) {
807 temp = readl(sport->port.membase + UCR3); 1196 temp = readl(sport->port.membase + UCR3);
808 temp |= IMX21_UCR3_RXDMUXSEL; 1197 temp |= IMX21_UCR3_RXDMUXSEL;
809 writel(temp, sport->port.membase + UCR3); 1198 writel(temp, sport->port.membase + UCR3);
@@ -833,7 +1222,7 @@ static int imx_startup(struct uart_port *port)
833 1222
834 if (USE_IRDA(sport)) { 1223 if (USE_IRDA(sport)) {
835 struct imxuart_platform_data *pdata; 1224 struct imxuart_platform_data *pdata;
836 pdata = sport->port.dev->platform_data; 1225 pdata = dev_get_platdata(sport->port.dev);
837 sport->irda_inv_rx = pdata->irda_inv_rx; 1226 sport->irda_inv_rx = pdata->irda_inv_rx;
838 sport->irda_inv_tx = pdata->irda_inv_tx; 1227 sport->irda_inv_tx = pdata->irda_inv_tx;
839 sport->trcv_delay = pdata->transceiver_delay; 1228 sport->trcv_delay = pdata->transceiver_delay;
@@ -859,6 +1248,15 @@ static void imx_shutdown(struct uart_port *port)
859 unsigned long temp; 1248 unsigned long temp;
860 unsigned long flags; 1249 unsigned long flags;
861 1250
1251 if (sport->dma_is_enabled) {
1252 /* We have to wait for the DMA to finish. */
1253 wait_event(sport->dma_wait,
1254 !sport->dma_is_rxing && !sport->dma_is_txing);
1255 imx_stop_rx(port);
1256 imx_disable_dma(sport);
1257 imx_uart_dma_exit(sport);
1258 }
1259
862 spin_lock_irqsave(&sport->port.lock, flags); 1260 spin_lock_irqsave(&sport->port.lock, flags);
863 temp = readl(sport->port.membase + UCR2); 1261 temp = readl(sport->port.membase + UCR2);
864 temp &= ~(UCR2_TXEN); 1262 temp &= ~(UCR2_TXEN);
@@ -867,7 +1265,7 @@ static void imx_shutdown(struct uart_port *port)
867 1265
868 if (USE_IRDA(sport)) { 1266 if (USE_IRDA(sport)) {
869 struct imxuart_platform_data *pdata; 1267 struct imxuart_platform_data *pdata;
870 pdata = sport->port.dev->platform_data; 1268 pdata = dev_get_platdata(sport->port.dev);
871 if (pdata->irda_enable) 1269 if (pdata->irda_enable)
872 pdata->irda_enable(0); 1270 pdata->irda_enable(0);
873 } 1271 }
@@ -901,10 +1299,8 @@ static void imx_shutdown(struct uart_port *port)
901 writel(temp, sport->port.membase + UCR1); 1299 writel(temp, sport->port.membase + UCR1);
902 spin_unlock_irqrestore(&sport->port.lock, flags); 1300 spin_unlock_irqrestore(&sport->port.lock, flags);
903 1301
904 if (!uart_console(&sport->port)) { 1302 clk_disable_unprepare(sport->clk_per);
905 clk_disable_unprepare(sport->clk_per); 1303 clk_disable_unprepare(sport->clk_ipg);
906 clk_disable_unprepare(sport->clk_ipg);
907 }
908} 1304}
909 1305
910static void 1306static void
@@ -947,6 +1343,11 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
947 if (sport->have_rtscts) { 1343 if (sport->have_rtscts) {
948 ucr2 &= ~UCR2_IRTS; 1344 ucr2 &= ~UCR2_IRTS;
949 ucr2 |= UCR2_CTSC; 1345 ucr2 |= UCR2_CTSC;
1346
1347 /* Can we enable the DMA support? */
1348 if (is_imx6q_uart(sport) && !uart_console(port)
1349 && !sport->dma_is_inited)
1350 imx_uart_dma_init(sport);
950 } else { 1351 } else {
951 termios->c_cflag &= ~CRTSCTS; 1352 termios->c_cflag &= ~CRTSCTS;
952 } 1353 }
@@ -1020,6 +1421,11 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
1020 */ 1421 */
1021 div = 1; 1422 div = 1;
1022 } else { 1423 } else {
1424 /* custom-baudrate handling */
1425 div = sport->port.uartclk / (baud * 16);
1426 if (baud == 38400 && quot != div)
1427 baud = sport->port.uartclk / (quot * 16);
1428
1023 div = sport->port.uartclk / (baud * 16); 1429 div = sport->port.uartclk / (baud * 16);
1024 if (div > 7) 1430 if (div > 7)
1025 div = 7; 1431 div = 7;
@@ -1048,7 +1454,7 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
1048 writel(num, sport->port.membase + UBIR); 1454 writel(num, sport->port.membase + UBIR);
1049 writel(denom, sport->port.membase + UBMR); 1455 writel(denom, sport->port.membase + UBMR);
1050 1456
1051 if (is_imx21_uart(sport)) 1457 if (!is_imx1_uart(sport))
1052 writel(sport->port.uartclk / div / 1000, 1458 writel(sport->port.uartclk / div / 1000,
1053 sport->port.membase + IMX21_ONEMS); 1459 sport->port.membase + IMX21_ONEMS);
1054 1460
@@ -1060,6 +1466,8 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
1060 if (UART_ENABLE_MS(&sport->port, termios->c_cflag)) 1466 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
1061 imx_enable_ms(&sport->port); 1467 imx_enable_ms(&sport->port);
1062 1468
1469 if (sport->dma_is_inited && !sport->dma_is_enabled)
1470 imx_enable_dma(sport);
1063 spin_unlock_irqrestore(&sport->port.lock, flags); 1471 spin_unlock_irqrestore(&sport->port.lock, flags);
1064} 1472}
1065 1473
@@ -1251,6 +1659,16 @@ imx_console_write(struct console *co, const char *s, unsigned int count)
1251 unsigned int ucr1; 1659 unsigned int ucr1;
1252 unsigned long flags = 0; 1660 unsigned long flags = 0;
1253 int locked = 1; 1661 int locked = 1;
1662 int retval;
1663
1664 retval = clk_enable(sport->clk_per);
1665 if (retval)
1666 return;
1667 retval = clk_enable(sport->clk_ipg);
1668 if (retval) {
1669 clk_disable(sport->clk_per);
1670 return;
1671 }
1254 1672
1255 if (sport->port.sysrq) 1673 if (sport->port.sysrq)
1256 locked = 0; 1674 locked = 0;
@@ -1286,6 +1704,9 @@ imx_console_write(struct console *co, const char *s, unsigned int count)
1286 1704
1287 if (locked) 1705 if (locked)
1288 spin_unlock_irqrestore(&sport->port.lock, flags); 1706 spin_unlock_irqrestore(&sport->port.lock, flags);
1707
1708 clk_disable(sport->clk_ipg);
1709 clk_disable(sport->clk_per);
1289} 1710}
1290 1711
1291/* 1712/*
@@ -1359,6 +1780,7 @@ imx_console_setup(struct console *co, char *options)
1359 int bits = 8; 1780 int bits = 8;
1360 int parity = 'n'; 1781 int parity = 'n';
1361 int flow = 'n'; 1782 int flow = 'n';
1783 int retval;
1362 1784
1363 /* 1785 /*
1364 * Check whether an invalid uart number has been specified, and 1786 * Check whether an invalid uart number has been specified, and
@@ -1371,6 +1793,11 @@ imx_console_setup(struct console *co, char *options)
1371 if (sport == NULL) 1793 if (sport == NULL)
1372 return -ENODEV; 1794 return -ENODEV;
1373 1795
1796 /* For setting the registers, we only need to enable the ipg clock. */
1797 retval = clk_prepare_enable(sport->clk_ipg);
1798 if (retval)
1799 goto error_console;
1800
1374 if (options) 1801 if (options)
1375 uart_parse_options(options, &baud, &parity, &bits, &flow); 1802 uart_parse_options(options, &baud, &parity, &bits, &flow);
1376 else 1803 else
@@ -1378,7 +1805,20 @@ imx_console_setup(struct console *co, char *options)
1378 1805
1379 imx_setup_ufcr(sport, 0); 1806 imx_setup_ufcr(sport, 0);
1380 1807
1381 return uart_set_options(&sport->port, co, baud, parity, bits, flow); 1808 retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
1809
1810 clk_disable(sport->clk_ipg);
1811 if (retval) {
1812 clk_unprepare(sport->clk_ipg);
1813 goto error_console;
1814 }
1815
1816 retval = clk_prepare(sport->clk_per);
1817 if (retval)
1818 clk_disable_unprepare(sport->clk_ipg);
1819
1820error_console:
1821 return retval;
1382} 1822}
1383 1823
1384static struct uart_driver imx_reg; 1824static struct uart_driver imx_reg;
@@ -1485,7 +1925,7 @@ static inline int serial_imx_probe_dt(struct imx_port *sport,
1485static void serial_imx_probe_pdata(struct imx_port *sport, 1925static void serial_imx_probe_pdata(struct imx_port *sport,
1486 struct platform_device *pdev) 1926 struct platform_device *pdev)
1487{ 1927{
1488 struct imxuart_platform_data *pdata = pdev->dev.platform_data; 1928 struct imxuart_platform_data *pdata = dev_get_platdata(&pdev->dev);
1489 1929
1490 sport->port.line = pdev->id; 1930 sport->port.line = pdev->id;
1491 sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data; 1931 sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
@@ -1507,7 +1947,6 @@ static int serial_imx_probe(struct platform_device *pdev)
1507 void __iomem *base; 1947 void __iomem *base;
1508 int ret = 0; 1948 int ret = 0;
1509 struct resource *res; 1949 struct resource *res;
1510 struct pinctrl *pinctrl;
1511 1950
1512 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL); 1951 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
1513 if (!sport) 1952 if (!sport)
@@ -1543,13 +1982,6 @@ static int serial_imx_probe(struct platform_device *pdev)
1543 sport->timer.function = imx_timeout; 1982 sport->timer.function = imx_timeout;
1544 sport->timer.data = (unsigned long)sport; 1983 sport->timer.data = (unsigned long)sport;
1545 1984
1546 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1547 if (IS_ERR(pinctrl)) {
1548 ret = PTR_ERR(pinctrl);
1549 dev_err(&pdev->dev, "failed to get default pinctrl: %d\n", ret);
1550 return ret;
1551 }
1552
1553 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 1985 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1554 if (IS_ERR(sport->clk_ipg)) { 1986 if (IS_ERR(sport->clk_ipg)) {
1555 ret = PTR_ERR(sport->clk_ipg); 1987 ret = PTR_ERR(sport->clk_ipg);
@@ -1564,18 +1996,15 @@ static int serial_imx_probe(struct platform_device *pdev)
1564 return ret; 1996 return ret;
1565 } 1997 }
1566 1998
1567 clk_prepare_enable(sport->clk_per);
1568 clk_prepare_enable(sport->clk_ipg);
1569
1570 sport->port.uartclk = clk_get_rate(sport->clk_per); 1999 sport->port.uartclk = clk_get_rate(sport->clk_per);
1571 2000
1572 imx_ports[sport->port.line] = sport; 2001 imx_ports[sport->port.line] = sport;
1573 2002
1574 pdata = pdev->dev.platform_data; 2003 pdata = dev_get_platdata(&pdev->dev);
1575 if (pdata && pdata->init) { 2004 if (pdata && pdata->init) {
1576 ret = pdata->init(pdev); 2005 ret = pdata->init(pdev);
1577 if (ret) 2006 if (ret)
1578 goto clkput; 2007 return ret;
1579 } 2008 }
1580 2009
1581 ret = uart_add_one_port(&imx_reg, &sport->port); 2010 ret = uart_add_one_port(&imx_reg, &sport->port);
@@ -1583,18 +2012,10 @@ static int serial_imx_probe(struct platform_device *pdev)
1583 goto deinit; 2012 goto deinit;
1584 platform_set_drvdata(pdev, sport); 2013 platform_set_drvdata(pdev, sport);
1585 2014
1586 if (!uart_console(&sport->port)) {
1587 clk_disable_unprepare(sport->clk_per);
1588 clk_disable_unprepare(sport->clk_ipg);
1589 }
1590
1591 return 0; 2015 return 0;
1592deinit: 2016deinit:
1593 if (pdata && pdata->exit) 2017 if (pdata && pdata->exit)
1594 pdata->exit(pdev); 2018 pdata->exit(pdev);
1595clkput:
1596 clk_disable_unprepare(sport->clk_per);
1597 clk_disable_unprepare(sport->clk_ipg);
1598 return ret; 2019 return ret;
1599} 2020}
1600 2021
@@ -1603,9 +2024,7 @@ static int serial_imx_remove(struct platform_device *pdev)
1603 struct imxuart_platform_data *pdata; 2024 struct imxuart_platform_data *pdata;
1604 struct imx_port *sport = platform_get_drvdata(pdev); 2025 struct imx_port *sport = platform_get_drvdata(pdev);
1605 2026
1606 pdata = pdev->dev.platform_data; 2027 pdata = dev_get_platdata(&pdev->dev);
1607
1608 platform_set_drvdata(pdev, NULL);
1609 2028
1610 uart_remove_one_port(&imx_reg, &sport->port); 2029 uart_remove_one_port(&imx_reg, &sport->port);
1611 2030
diff --git a/drivers/tty/serial/max3100.c b/drivers/tty/serial/max3100.c
index 35866d5872ad..79f9a9eff545 100644
--- a/drivers/tty/serial/max3100.c
+++ b/drivers/tty/serial/max3100.c
@@ -779,7 +779,7 @@ static int max3100_probe(struct spi_device *spi)
779 max3100s[i]->irq = spi->irq; 779 max3100s[i]->irq = spi->irq;
780 spin_lock_init(&max3100s[i]->conf_lock); 780 spin_lock_init(&max3100s[i]->conf_lock);
781 spi_set_drvdata(spi, max3100s[i]); 781 spi_set_drvdata(spi, max3100s[i]);
782 pdata = spi->dev.platform_data; 782 pdata = dev_get_platdata(&spi->dev);
783 max3100s[i]->crystal = pdata->crystal; 783 max3100s[i]->crystal = pdata->crystal;
784 max3100s[i]->loopback = pdata->loopback; 784 max3100s[i]->loopback = pdata->loopback;
785 max3100s[i]->poll_time = pdata->poll_time * HZ / 1000; 785 max3100s[i]->poll_time = pdata->poll_time * HZ / 1000;
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index 8941e6418942..b2e707aa603a 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * Maxim (Dallas) MAX3107/8 serial driver 2 * Maxim (Dallas) MAX3107/8/9, MAX14830 serial driver
3 * 3 *
4 * Copyright (C) 2012 Alexander Shiyan <shc_work@mail.ru> 4 * Copyright (C) 2012-2013 Alexander Shiyan <shc_work@mail.ru>
5 * 5 *
6 * Based on max3100.c, by Christian Pellegrin <chripell@evolware.org> 6 * Based on max3100.c, by Christian Pellegrin <chripell@evolware.org>
7 * Based on max3110.c, by Feng Tang <feng.tang@intel.com> 7 * Based on max3110.c, by Feng Tang <feng.tang@intel.com>
@@ -13,11 +13,10 @@
13 * (at your option) any later version. 13 * (at your option) any later version.
14 */ 14 */
15 15
16/* TODO: MAX3109 support (Dual) */
17/* TODO: MAX14830 support (Quad) */
18
19#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/delay.h>
20#include <linux/device.h> 18#include <linux/device.h>
19#include <linux/bitops.h>
21#include <linux/serial_core.h> 20#include <linux/serial_core.h>
22#include <linux/serial.h> 21#include <linux/serial.h>
23#include <linux/tty.h> 22#include <linux/tty.h>
@@ -25,8 +24,10 @@
25#include <linux/regmap.h> 24#include <linux/regmap.h>
26#include <linux/gpio.h> 25#include <linux/gpio.h>
27#include <linux/spi/spi.h> 26#include <linux/spi/spi.h>
27
28#include <linux/platform_data/max310x.h> 28#include <linux/platform_data/max310x.h>
29 29
30#define MAX310X_NAME "max310x"
30#define MAX310X_MAJOR 204 31#define MAX310X_MAJOR 204
31#define MAX310X_MINOR 209 32#define MAX310X_MINOR 209
32 33
@@ -37,7 +38,8 @@
37#define MAX310X_IRQSTS_REG (0x02) /* IRQ status */ 38#define MAX310X_IRQSTS_REG (0x02) /* IRQ status */
38#define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */ 39#define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */
39#define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */ 40#define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */
40#define MAX310X_SPCHR_IRQEN_REG (0x05) /* Special char IRQ enable */ 41#define MAX310X_REG_05 (0x05)
42#define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */
41#define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */ 43#define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */
42#define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */ 44#define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */
43#define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */ 45#define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */
@@ -63,8 +65,15 @@
63#define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */ 65#define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */
64#define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */ 66#define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */
65#define MAX310X_CLKSRC_REG (0x1e) /* Clock source */ 67#define MAX310X_CLKSRC_REG (0x1e) /* Clock source */
66/* Only present in MAX3107 */ 68#define MAX310X_REG_1F (0x1f)
67#define MAX3107_REVID_REG (0x1f) /* Revision identification */ 69
70#define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */
71
72#define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */
73#define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */
74
75/* Extended registers */
76#define MAX310X_REVID_EXTREG MAX310X_REG_05 /* Revision ID */
68 77
69/* IRQ register bits */ 78/* IRQ register bits */
70#define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */ 79#define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
@@ -246,58 +255,210 @@
246#define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */ 255#define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
247#define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */ 256#define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
248 257
258/* Global commands */
259#define MAX310X_EXTREG_ENBL (0xce)
260#define MAX310X_EXTREG_DSBL (0xcd)
261
249/* Misc definitions */ 262/* Misc definitions */
250#define MAX310X_FIFO_SIZE (128) 263#define MAX310X_FIFO_SIZE (128)
264#define MAX310x_REV_MASK (0xfc)
251 265
252/* MAX3107 specific */ 266/* MAX3107 specific */
253#define MAX3107_REV_ID (0xa0) 267#define MAX3107_REV_ID (0xa0)
254#define MAX3107_REV_MASK (0xfe) 268
255 269/* MAX3109 specific */
256/* IRQ status bits definitions */ 270#define MAX3109_REV_ID (0xc0)
257#define MAX310X_IRQ_TX (MAX310X_IRQ_TXFIFO_BIT | \ 271
258 MAX310X_IRQ_TXEMPTY_BIT) 272/* MAX14830 specific */
259#define MAX310X_IRQ_RX (MAX310X_IRQ_RXFIFO_BIT | \ 273#define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */
260 MAX310X_IRQ_RXEMPTY_BIT) 274#define MAX14830_REV_ID (0xb0)
261 275
262/* Supported chip types */ 276struct max310x_devtype {
263enum { 277 char name[9];
264 MAX310X_TYPE_MAX3107 = 3107, 278 int nr;
265 MAX310X_TYPE_MAX3108 = 3108, 279 int (*detect)(struct device *);
280 void (*power)(struct uart_port *, int);
266}; 281};
267 282
268struct max310x_port { 283struct max310x_one {
269 struct uart_driver uart;
270 struct uart_port port; 284 struct uart_port port;
285 struct work_struct tx_work;
286};
271 287
272 const char *name; 288struct max310x_port {
273 int uartclk; 289 struct uart_driver uart;
274 290 struct max310x_devtype *devtype;
275 unsigned int nr_gpio; 291 struct regmap *regmap;
292 struct regmap_config regcfg;
293 struct mutex mutex;
294 struct max310x_pdata *pdata;
295 int gpio_used;
276#ifdef CONFIG_GPIOLIB 296#ifdef CONFIG_GPIOLIB
277 struct gpio_chip gpio; 297 struct gpio_chip gpio;
278#endif 298#endif
299 struct max310x_one p[0];
300};
279 301
280 struct regmap *regmap; 302static u8 max310x_port_read(struct uart_port *port, u8 reg)
281 struct regmap_config regcfg; 303{
304 struct max310x_port *s = dev_get_drvdata(port->dev);
305 unsigned int val = 0;
282 306
283 struct workqueue_struct *wq; 307 regmap_read(s->regmap, port->iobase + reg, &val);
284 struct work_struct tx_work;
285 308
286 struct mutex max310x_mutex; 309 return val;
310}
287 311
288 struct max310x_pdata *pdata; 312static void max310x_port_write(struct uart_port *port, u8 reg, u8 val)
313{
314 struct max310x_port *s = dev_get_drvdata(port->dev);
315
316 regmap_write(s->regmap, port->iobase + reg, val);
317}
318
319static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val)
320{
321 struct max310x_port *s = dev_get_drvdata(port->dev);
322
323 regmap_update_bits(s->regmap, port->iobase + reg, mask, val);
324}
325
326static int max3107_detect(struct device *dev)
327{
328 struct max310x_port *s = dev_get_drvdata(dev);
329 unsigned int val = 0;
330 int ret;
331
332 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
333 if (ret)
334 return ret;
335
336 if (((val & MAX310x_REV_MASK) != MAX3107_REV_ID)) {
337 dev_err(dev,
338 "%s ID 0x%02x does not match\n", s->devtype->name, val);
339 return -ENODEV;
340 }
341
342 return 0;
343}
344
345static int max3108_detect(struct device *dev)
346{
347 struct max310x_port *s = dev_get_drvdata(dev);
348 unsigned int val = 0;
349 int ret;
350
351 /* MAX3108 have not REV ID register, we just check default value
352 * from clocksource register to make sure everything works.
353 */
354 ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
355 if (ret)
356 return ret;
357
358 if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) {
359 dev_err(dev, "%s not present\n", s->devtype->name);
360 return -ENODEV;
361 }
362
363 return 0;
364}
365
366static int max3109_detect(struct device *dev)
367{
368 struct max310x_port *s = dev_get_drvdata(dev);
369 unsigned int val = 0;
370 int ret;
371
372 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
373 if (ret)
374 return ret;
375
376 if (((val & MAX310x_REV_MASK) != MAX3109_REV_ID)) {
377 dev_err(dev,
378 "%s ID 0x%02x does not match\n", s->devtype->name, val);
379 return -ENODEV;
380 }
381
382 return 0;
383}
384
385static void max310x_power(struct uart_port *port, int on)
386{
387 max310x_port_update(port, MAX310X_MODE1_REG,
388 MAX310X_MODE1_FORCESLEEP_BIT,
389 on ? 0 : MAX310X_MODE1_FORCESLEEP_BIT);
390 if (on)
391 msleep(50);
392}
393
394static int max14830_detect(struct device *dev)
395{
396 struct max310x_port *s = dev_get_drvdata(dev);
397 unsigned int val = 0;
398 int ret;
399
400 ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
401 MAX310X_EXTREG_ENBL);
402 if (ret)
403 return ret;
404
405 regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
406 regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
407 if (((val & MAX310x_REV_MASK) != MAX14830_REV_ID)) {
408 dev_err(dev,
409 "%s ID 0x%02x does not match\n", s->devtype->name, val);
410 return -ENODEV;
411 }
412
413 return 0;
414}
415
416static void max14830_power(struct uart_port *port, int on)
417{
418 max310x_port_update(port, MAX310X_BRGCFG_REG,
419 MAX14830_BRGCFG_CLKDIS_BIT,
420 on ? 0 : MAX14830_BRGCFG_CLKDIS_BIT);
421 if (on)
422 msleep(50);
423}
424
425static const struct max310x_devtype max3107_devtype = {
426 .name = "MAX3107",
427 .nr = 1,
428 .detect = max3107_detect,
429 .power = max310x_power,
430};
431
432static const struct max310x_devtype max3108_devtype = {
433 .name = "MAX3108",
434 .nr = 1,
435 .detect = max3108_detect,
436 .power = max310x_power,
437};
438
439static const struct max310x_devtype max3109_devtype = {
440 .name = "MAX3109",
441 .nr = 2,
442 .detect = max3109_detect,
443 .power = max310x_power,
444};
445
446static const struct max310x_devtype max14830_devtype = {
447 .name = "MAX14830",
448 .nr = 4,
449 .detect = max14830_detect,
450 .power = max14830_power,
289}; 451};
290 452
291static bool max3107_8_reg_writeable(struct device *dev, unsigned int reg) 453static bool max310x_reg_writeable(struct device *dev, unsigned int reg)
292{ 454{
293 switch (reg) { 455 switch (reg & 0x1f) {
294 case MAX310X_IRQSTS_REG: 456 case MAX310X_IRQSTS_REG:
295 case MAX310X_LSR_IRQSTS_REG: 457 case MAX310X_LSR_IRQSTS_REG:
296 case MAX310X_SPCHR_IRQSTS_REG: 458 case MAX310X_SPCHR_IRQSTS_REG:
297 case MAX310X_STS_IRQSTS_REG: 459 case MAX310X_STS_IRQSTS_REG:
298 case MAX310X_TXFIFOLVL_REG: 460 case MAX310X_TXFIFOLVL_REG:
299 case MAX310X_RXFIFOLVL_REG: 461 case MAX310X_RXFIFOLVL_REG:
300 case MAX3107_REVID_REG: /* Only available on MAX3107 */
301 return false; 462 return false;
302 default: 463 default:
303 break; 464 break;
@@ -308,7 +469,7 @@ static bool max3107_8_reg_writeable(struct device *dev, unsigned int reg)
308 469
309static bool max310x_reg_volatile(struct device *dev, unsigned int reg) 470static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
310{ 471{
311 switch (reg) { 472 switch (reg & 0x1f) {
312 case MAX310X_RHR_REG: 473 case MAX310X_RHR_REG:
313 case MAX310X_IRQSTS_REG: 474 case MAX310X_IRQSTS_REG:
314 case MAX310X_LSR_IRQSTS_REG: 475 case MAX310X_LSR_IRQSTS_REG:
@@ -317,6 +478,9 @@ static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
317 case MAX310X_TXFIFOLVL_REG: 478 case MAX310X_TXFIFOLVL_REG:
318 case MAX310X_RXFIFOLVL_REG: 479 case MAX310X_RXFIFOLVL_REG:
319 case MAX310X_GPIODATA_REG: 480 case MAX310X_GPIODATA_REG:
481 case MAX310X_BRGDIVLSB_REG:
482 case MAX310X_REG_05:
483 case MAX310X_REG_1F:
320 return true; 484 return true;
321 default: 485 default:
322 break; 486 break;
@@ -327,7 +491,7 @@ static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
327 491
328static bool max310x_reg_precious(struct device *dev, unsigned int reg) 492static bool max310x_reg_precious(struct device *dev, unsigned int reg)
329{ 493{
330 switch (reg) { 494 switch (reg & 0x1f) {
331 case MAX310X_RHR_REG: 495 case MAX310X_RHR_REG:
332 case MAX310X_IRQSTS_REG: 496 case MAX310X_IRQSTS_REG:
333 case MAX310X_SPCHR_IRQSTS_REG: 497 case MAX310X_SPCHR_IRQSTS_REG:
@@ -340,42 +504,25 @@ static bool max310x_reg_precious(struct device *dev, unsigned int reg)
340 return false; 504 return false;
341} 505}
342 506
343static void max310x_set_baud(struct max310x_port *s, int baud) 507static void max310x_set_baud(struct uart_port *port, int baud)
344{ 508{
345 unsigned int mode = 0, div = s->uartclk / baud; 509 unsigned int mode = 0, div = port->uartclk / baud;
346 510
347 if (!(div / 16)) { 511 if (!(div / 16)) {
348 /* Mode x2 */ 512 /* Mode x2 */
349 mode = MAX310X_BRGCFG_2XMODE_BIT; 513 mode = MAX310X_BRGCFG_2XMODE_BIT;
350 div = (s->uartclk * 2) / baud; 514 div = (port->uartclk * 2) / baud;
351 } 515 }
352 516
353 if (!(div / 16)) { 517 if (!(div / 16)) {
354 /* Mode x4 */ 518 /* Mode x4 */
355 mode = MAX310X_BRGCFG_4XMODE_BIT; 519 mode = MAX310X_BRGCFG_4XMODE_BIT;
356 div = (s->uartclk * 4) / baud; 520 div = (port->uartclk * 4) / baud;
357 } 521 }
358 522
359 regmap_write(s->regmap, MAX310X_BRGDIVMSB_REG, 523 max310x_port_write(port, MAX310X_BRGDIVMSB_REG, (div / 16) >> 8);
360 ((div / 16) >> 8) & 0xff); 524 max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div / 16);
361 regmap_write(s->regmap, MAX310X_BRGDIVLSB_REG, (div / 16) & 0xff); 525 max310x_port_write(port, MAX310X_BRGCFG_REG, (div % 16) | mode);
362 regmap_write(s->regmap, MAX310X_BRGCFG_REG, (div % 16) | mode);
363}
364
365static void max310x_wait_pll(struct max310x_port *s)
366{
367 int tryes = 1000;
368
369 /* Wait for PLL only if crystal is used */
370 if (!(s->pdata->driver_flags & MAX310X_EXT_CLK)) {
371 unsigned int sts = 0;
372
373 while (tryes--) {
374 regmap_read(s->regmap, MAX310X_STS_IRQSTS_REG, &sts);
375 if (sts & MAX310X_STS_CLKREADY_BIT)
376 break;
377 }
378 }
379} 526}
380 527
381static int max310x_update_best_err(unsigned long f, long *besterr) 528static int max310x_update_best_err(unsigned long f, long *besterr)
@@ -449,49 +596,49 @@ static int max310x_set_ref_clk(struct max310x_port *s)
449 596
450 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc); 597 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc);
451 598
452 if (pllcfg) 599 /* Wait for crystal */
453 max310x_wait_pll(s); 600 if (pllcfg && !(s->pdata->driver_flags & MAX310X_EXT_CLK))
454 601 msleep(10);
455 dev_dbg(s->port.dev, "Reference clock set to %lu Hz\n", bestfreq);
456 602
457 return (int)bestfreq; 603 return (int)bestfreq;
458} 604}
459 605
460static void max310x_handle_rx(struct max310x_port *s, unsigned int rxlen) 606static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen)
461{ 607{
462 unsigned int sts = 0, ch = 0, flag; 608 unsigned int sts, ch, flag;
463 609
464 if (unlikely(rxlen >= MAX310X_FIFO_SIZE)) { 610 if (unlikely(rxlen >= port->fifosize)) {
465 dev_warn(s->port.dev, "Possible RX FIFO overrun %d\n", rxlen); 611 dev_warn_ratelimited(port->dev,
612 "Port %i: Possible RX FIFO overrun\n",
613 port->line);
614 port->icount.buf_overrun++;
466 /* Ensure sanity of RX level */ 615 /* Ensure sanity of RX level */
467 rxlen = MAX310X_FIFO_SIZE; 616 rxlen = port->fifosize;
468 } 617 }
469 618
470 dev_dbg(s->port.dev, "RX Len = %u\n", rxlen);
471
472 while (rxlen--) { 619 while (rxlen--) {
473 regmap_read(s->regmap, MAX310X_RHR_REG, &ch); 620 ch = max310x_port_read(port, MAX310X_RHR_REG);
474 regmap_read(s->regmap, MAX310X_LSR_IRQSTS_REG, &sts); 621 sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
475 622
476 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT | 623 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT |
477 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT; 624 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT;
478 625
479 s->port.icount.rx++; 626 port->icount.rx++;
480 flag = TTY_NORMAL; 627 flag = TTY_NORMAL;
481 628
482 if (unlikely(sts)) { 629 if (unlikely(sts)) {
483 if (sts & MAX310X_LSR_RXBRK_BIT) { 630 if (sts & MAX310X_LSR_RXBRK_BIT) {
484 s->port.icount.brk++; 631 port->icount.brk++;
485 if (uart_handle_break(&s->port)) 632 if (uart_handle_break(port))
486 continue; 633 continue;
487 } else if (sts & MAX310X_LSR_RXPAR_BIT) 634 } else if (sts & MAX310X_LSR_RXPAR_BIT)
488 s->port.icount.parity++; 635 port->icount.parity++;
489 else if (sts & MAX310X_LSR_FRERR_BIT) 636 else if (sts & MAX310X_LSR_FRERR_BIT)
490 s->port.icount.frame++; 637 port->icount.frame++;
491 else if (sts & MAX310X_LSR_RXOVR_BIT) 638 else if (sts & MAX310X_LSR_RXOVR_BIT)
492 s->port.icount.overrun++; 639 port->icount.overrun++;
493 640
494 sts &= s->port.read_status_mask; 641 sts &= port->read_status_mask;
495 if (sts & MAX310X_LSR_RXBRK_BIT) 642 if (sts & MAX310X_LSR_RXBRK_BIT)
496 flag = TTY_BREAK; 643 flag = TTY_BREAK;
497 else if (sts & MAX310X_LSR_RXPAR_BIT) 644 else if (sts & MAX310X_LSR_RXPAR_BIT)
@@ -502,129 +649,129 @@ static void max310x_handle_rx(struct max310x_port *s, unsigned int rxlen)
502 flag = TTY_OVERRUN; 649 flag = TTY_OVERRUN;
503 } 650 }
504 651
505 if (uart_handle_sysrq_char(s->port, ch)) 652 if (uart_handle_sysrq_char(port, ch))
506 continue; 653 continue;
507 654
508 if (sts & s->port.ignore_status_mask) 655 if (sts & port->ignore_status_mask)
509 continue; 656 continue;
510 657
511 uart_insert_char(&s->port, sts, MAX310X_LSR_RXOVR_BIT, 658 uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag);
512 ch, flag);
513 } 659 }
514 660
515 tty_flip_buffer_push(&s->port.state->port); 661 tty_flip_buffer_push(&port->state->port);
516} 662}
517 663
518static void max310x_handle_tx(struct max310x_port *s) 664static void max310x_handle_tx(struct uart_port *port)
519{ 665{
520 struct circ_buf *xmit = &s->port.state->xmit; 666 struct circ_buf *xmit = &port->state->xmit;
521 unsigned int txlen = 0, to_send; 667 unsigned int txlen, to_send;
522 668
523 if (unlikely(s->port.x_char)) { 669 if (unlikely(port->x_char)) {
524 regmap_write(s->regmap, MAX310X_THR_REG, s->port.x_char); 670 max310x_port_write(port, MAX310X_THR_REG, port->x_char);
525 s->port.icount.tx++; 671 port->icount.tx++;
526 s->port.x_char = 0; 672 port->x_char = 0;
527 return; 673 return;
528 } 674 }
529 675
530 if (uart_circ_empty(xmit) || uart_tx_stopped(&s->port)) 676 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
531 return; 677 return;
532 678
533 /* Get length of data pending in circular buffer */ 679 /* Get length of data pending in circular buffer */
534 to_send = uart_circ_chars_pending(xmit); 680 to_send = uart_circ_chars_pending(xmit);
535 if (likely(to_send)) { 681 if (likely(to_send)) {
536 /* Limit to size of TX FIFO */ 682 /* Limit to size of TX FIFO */
537 regmap_read(s->regmap, MAX310X_TXFIFOLVL_REG, &txlen); 683 txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
538 txlen = MAX310X_FIFO_SIZE - txlen; 684 txlen = port->fifosize - txlen;
539 to_send = (to_send > txlen) ? txlen : to_send; 685 to_send = (to_send > txlen) ? txlen : to_send;
540 686
541 dev_dbg(s->port.dev, "TX Len = %u\n", to_send);
542
543 /* Add data to send */ 687 /* Add data to send */
544 s->port.icount.tx += to_send; 688 port->icount.tx += to_send;
545 while (to_send--) { 689 while (to_send--) {
546 regmap_write(s->regmap, MAX310X_THR_REG, 690 max310x_port_write(port, MAX310X_THR_REG,
547 xmit->buf[xmit->tail]); 691 xmit->buf[xmit->tail]);
548 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 692 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
549 }; 693 };
550 } 694 }
551 695
552 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 696 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
553 uart_write_wakeup(&s->port); 697 uart_write_wakeup(port);
554} 698}
555 699
556static irqreturn_t max310x_ist(int irq, void *dev_id) 700static void max310x_port_irq(struct max310x_port *s, int portno)
557{ 701{
558 struct max310x_port *s = (struct max310x_port *)dev_id; 702 struct uart_port *port = &s->p[portno].port;
559 unsigned int ists = 0, lsr = 0, rxlen = 0;
560 703
561 mutex_lock(&s->max310x_mutex); 704 do {
705 unsigned int ists, lsr, rxlen;
562 706
563 for (;;) {
564 /* Read IRQ status & RX FIFO level */ 707 /* Read IRQ status & RX FIFO level */
565 regmap_read(s->regmap, MAX310X_IRQSTS_REG, &ists); 708 ists = max310x_port_read(port, MAX310X_IRQSTS_REG);
566 regmap_read(s->regmap, MAX310X_LSR_IRQSTS_REG, &lsr); 709 rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG);
567 regmap_read(s->regmap, MAX310X_RXFIFOLVL_REG, &rxlen); 710 if (!ists && !rxlen)
568 if (!ists && !(lsr & MAX310X_LSR_RXTO_BIT) && !rxlen)
569 break; 711 break;
570 712
571 dev_dbg(s->port.dev, "IRQ status: 0x%02x\n", ists); 713 if (ists & MAX310X_IRQ_CTS_BIT) {
572 714 lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
573 if (rxlen) 715 uart_handle_cts_change(port,
574 max310x_handle_rx(s, rxlen);
575 if (ists & MAX310X_IRQ_TX)
576 max310x_handle_tx(s);
577 if (ists & MAX310X_IRQ_CTS_BIT)
578 uart_handle_cts_change(&s->port,
579 !!(lsr & MAX310X_LSR_CTS_BIT)); 716 !!(lsr & MAX310X_LSR_CTS_BIT));
580 } 717 }
718 if (rxlen)
719 max310x_handle_rx(port, rxlen);
720 if (ists & MAX310X_IRQ_TXEMPTY_BIT) {
721 mutex_lock(&s->mutex);
722 max310x_handle_tx(port);
723 mutex_unlock(&s->mutex);
724 }
725 } while (1);
726}
727
728static irqreturn_t max310x_ist(int irq, void *dev_id)
729{
730 struct max310x_port *s = (struct max310x_port *)dev_id;
581 731
582 mutex_unlock(&s->max310x_mutex); 732 if (s->uart.nr > 1) {
733 do {
734 unsigned int val = ~0;
735
736 WARN_ON_ONCE(regmap_read(s->regmap,
737 MAX310X_GLOBALIRQ_REG, &val));
738 val = ((1 << s->uart.nr) - 1) & ~val;
739 if (!val)
740 break;
741 max310x_port_irq(s, fls(val) - 1);
742 } while (1);
743 } else
744 max310x_port_irq(s, 0);
583 745
584 return IRQ_HANDLED; 746 return IRQ_HANDLED;
585} 747}
586 748
587static void max310x_wq_proc(struct work_struct *ws) 749static void max310x_wq_proc(struct work_struct *ws)
588{ 750{
589 struct max310x_port *s = container_of(ws, struct max310x_port, tx_work); 751 struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
752 struct max310x_port *s = dev_get_drvdata(one->port.dev);
590 753
591 mutex_lock(&s->max310x_mutex); 754 mutex_lock(&s->mutex);
592 max310x_handle_tx(s); 755 max310x_handle_tx(&one->port);
593 mutex_unlock(&s->max310x_mutex); 756 mutex_unlock(&s->mutex);
594} 757}
595 758
596static void max310x_start_tx(struct uart_port *port) 759static void max310x_start_tx(struct uart_port *port)
597{ 760{
598 struct max310x_port *s = container_of(port, struct max310x_port, port); 761 struct max310x_one *one = container_of(port, struct max310x_one, port);
599
600 queue_work(s->wq, &s->tx_work);
601}
602
603static void max310x_stop_tx(struct uart_port *port)
604{
605 /* Do nothing */
606}
607 762
608static void max310x_stop_rx(struct uart_port *port) 763 if (!work_pending(&one->tx_work))
609{ 764 schedule_work(&one->tx_work);
610 /* Do nothing */
611} 765}
612 766
613static unsigned int max310x_tx_empty(struct uart_port *port) 767static unsigned int max310x_tx_empty(struct uart_port *port)
614{ 768{
615 unsigned int val = 0; 769 unsigned int lvl, sts;
616 struct max310x_port *s = container_of(port, struct max310x_port, port);
617 770
618 mutex_lock(&s->max310x_mutex); 771 lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
619 regmap_read(s->regmap, MAX310X_TXFIFOLVL_REG, &val); 772 sts = max310x_port_read(port, MAX310X_IRQSTS_REG);
620 mutex_unlock(&s->max310x_mutex);
621 773
622 return val ? 0 : TIOCSER_TEMT; 774 return ((sts & MAX310X_IRQ_TXEMPTY_BIT) && !lvl) ? TIOCSER_TEMT : 0;
623}
624
625static void max310x_enable_ms(struct uart_port *port)
626{
627 /* Modem status not supported */
628} 775}
629 776
630static unsigned int max310x_get_mctrl(struct uart_port *port) 777static unsigned int max310x_get_mctrl(struct uart_port *port)
@@ -644,28 +791,20 @@ static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl)
644 791
645static void max310x_break_ctl(struct uart_port *port, int break_state) 792static void max310x_break_ctl(struct uart_port *port, int break_state)
646{ 793{
647 struct max310x_port *s = container_of(port, struct max310x_port, port); 794 max310x_port_update(port, MAX310X_LCR_REG,
648 795 MAX310X_LCR_TXBREAK_BIT,
649 mutex_lock(&s->max310x_mutex); 796 break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
650 regmap_update_bits(s->regmap, MAX310X_LCR_REG,
651 MAX310X_LCR_TXBREAK_BIT,
652 break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
653 mutex_unlock(&s->max310x_mutex);
654} 797}
655 798
656static void max310x_set_termios(struct uart_port *port, 799static void max310x_set_termios(struct uart_port *port,
657 struct ktermios *termios, 800 struct ktermios *termios,
658 struct ktermios *old) 801 struct ktermios *old)
659{ 802{
660 struct max310x_port *s = container_of(port, struct max310x_port, port);
661 unsigned int lcr, flow = 0; 803 unsigned int lcr, flow = 0;
662 int baud; 804 int baud;
663 805
664 mutex_lock(&s->max310x_mutex);
665
666 /* Mask termios capabilities we don't support */ 806 /* Mask termios capabilities we don't support */
667 termios->c_cflag &= ~CMSPAR; 807 termios->c_cflag &= ~CMSPAR;
668 termios->c_iflag &= ~IXANY;
669 808
670 /* Word size */ 809 /* Word size */
671 switch (termios->c_cflag & CSIZE) { 810 switch (termios->c_cflag & CSIZE) {
@@ -696,7 +835,7 @@ static void max310x_set_termios(struct uart_port *port,
696 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */ 835 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */
697 836
698 /* Update LCR register */ 837 /* Update LCR register */
699 regmap_write(s->regmap, MAX310X_LCR_REG, lcr); 838 max310x_port_write(port, MAX310X_LCR_REG, lcr);
700 839
701 /* Set read status mask */ 840 /* Set read status mask */
702 port->read_status_mask = MAX310X_LSR_RXOVR_BIT; 841 port->read_status_mask = MAX310X_LSR_RXOVR_BIT;
@@ -717,8 +856,8 @@ static void max310x_set_termios(struct uart_port *port,
717 MAX310X_LSR_RXBRK_BIT; 856 MAX310X_LSR_RXBRK_BIT;
718 857
719 /* Configure flow control */ 858 /* Configure flow control */
720 regmap_write(s->regmap, MAX310X_XON1_REG, termios->c_cc[VSTART]); 859 max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]);
721 regmap_write(s->regmap, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]); 860 max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]);
722 if (termios->c_cflag & CRTSCTS) 861 if (termios->c_cflag & CRTSCTS)
723 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT | 862 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT |
724 MAX310X_FLOWCTRL_AUTORTS_BIT; 863 MAX310X_FLOWCTRL_AUTORTS_BIT;
@@ -728,7 +867,7 @@ static void max310x_set_termios(struct uart_port *port,
728 if (termios->c_iflag & IXOFF) 867 if (termios->c_iflag & IXOFF)
729 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT | 868 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT |
730 MAX310X_FLOWCTRL_SWFLOWEN_BIT; 869 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
731 regmap_write(s->regmap, MAX310X_FLOWCTRL_REG, flow); 870 max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow);
732 871
733 /* Get baud rate generator configuration */ 872 /* Get baud rate generator configuration */
734 baud = uart_get_baud_rate(port, termios, old, 873 baud = uart_get_baud_rate(port, termios, old,
@@ -736,36 +875,30 @@ static void max310x_set_termios(struct uart_port *port,
736 port->uartclk / 4); 875 port->uartclk / 4);
737 876
738 /* Setup baudrate generator */ 877 /* Setup baudrate generator */
739 max310x_set_baud(s, baud); 878 max310x_set_baud(port, baud);
740 879
741 /* Update timeout according to new baud rate */ 880 /* Update timeout according to new baud rate */
742 uart_update_timeout(port, termios->c_cflag, baud); 881 uart_update_timeout(port, termios->c_cflag, baud);
743
744 mutex_unlock(&s->max310x_mutex);
745} 882}
746 883
747static int max310x_startup(struct uart_port *port) 884static int max310x_startup(struct uart_port *port)
748{ 885{
749 unsigned int val, line = port->line; 886 unsigned int val, line = port->line;
750 struct max310x_port *s = container_of(port, struct max310x_port, port); 887 struct max310x_port *s = dev_get_drvdata(port->dev);
751
752 if (s->pdata->suspend)
753 s->pdata->suspend(0);
754 888
755 mutex_lock(&s->max310x_mutex); 889 s->devtype->power(port, 1);
756 890
757 /* Configure baud rate, 9600 as default */ 891 /* Configure baud rate, 9600 as default */
758 max310x_set_baud(s, 9600); 892 max310x_set_baud(port, 9600);
759 893
760 /* Configure LCR register, 8N1 mode by default */ 894 /* Configure LCR register, 8N1 mode by default */
761 val = MAX310X_LCR_WORD_LEN_8; 895 max310x_port_write(port, MAX310X_LCR_REG, MAX310X_LCR_WORD_LEN_8);
762 regmap_write(s->regmap, MAX310X_LCR_REG, val);
763 896
764 /* Configure MODE1 register */ 897 /* Configure MODE1 register */
765 regmap_update_bits(s->regmap, MAX310X_MODE1_REG, 898 max310x_port_update(port, MAX310X_MODE1_REG,
766 MAX310X_MODE1_TRNSCVCTRL_BIT, 899 MAX310X_MODE1_TRNSCVCTRL_BIT,
767 (s->pdata->uart_flags[line] & MAX310X_AUTO_DIR_CTRL) 900 (s->pdata->uart_flags[line] & MAX310X_AUTO_DIR_CTRL)
768 ? MAX310X_MODE1_TRNSCVCTRL_BIT : 0); 901 ? MAX310X_MODE1_TRNSCVCTRL_BIT : 0);
769 902
770 /* Configure MODE2 register */ 903 /* Configure MODE2 register */
771 val = MAX310X_MODE2_RXEMPTINV_BIT; 904 val = MAX310X_MODE2_RXEMPTINV_BIT;
@@ -776,63 +909,40 @@ static int max310x_startup(struct uart_port *port)
776 909
777 /* Reset FIFOs */ 910 /* Reset FIFOs */
778 val |= MAX310X_MODE2_FIFORST_BIT; 911 val |= MAX310X_MODE2_FIFORST_BIT;
779 regmap_write(s->regmap, MAX310X_MODE2_REG, val); 912 max310x_port_write(port, MAX310X_MODE2_REG, val);
780 913 max310x_port_update(port, MAX310X_MODE2_REG,
781 /* Configure FIFO trigger level register */ 914 MAX310X_MODE2_FIFORST_BIT, 0);
782 /* RX FIFO trigger for 16 words, TX FIFO trigger for 64 words */
783 val = MAX310X_FIFOTRIGLVL_RX(16) | MAX310X_FIFOTRIGLVL_TX(64);
784 regmap_write(s->regmap, MAX310X_FIFOTRIGLVL_REG, val);
785 915
786 /* Configure flow control levels */ 916 /* Configure flow control levels */
787 /* Flow control halt level 96, resume level 48 */ 917 /* Flow control halt level 96, resume level 48 */
788 val = MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96); 918 max310x_port_write(port, MAX310X_FLOWLVL_REG,
789 regmap_write(s->regmap, MAX310X_FLOWLVL_REG, val); 919 MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96));
790
791 /* Clear timeout register */
792 regmap_write(s->regmap, MAX310X_RXTO_REG, 0);
793 920
794 /* Configure LSR interrupt enable register */ 921 /* Clear IRQ status register */
795 /* Enable RX timeout interrupt */ 922 max310x_port_read(port, MAX310X_IRQSTS_REG);
796 val = MAX310X_LSR_RXTO_BIT;
797 regmap_write(s->regmap, MAX310X_LSR_IRQEN_REG, val);
798 923
799 /* Clear FIFO reset */ 924 /* Enable RX, TX, CTS change interrupts */
800 regmap_update_bits(s->regmap, MAX310X_MODE2_REG, 925 val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
801 MAX310X_MODE2_FIFORST_BIT, 0); 926 max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
802
803 /* Clear IRQ status register by reading it */
804 regmap_read(s->regmap, MAX310X_IRQSTS_REG, &val);
805
806 /* Configure interrupt enable register */
807 /* Enable CTS change interrupt */
808 val = MAX310X_IRQ_CTS_BIT;
809 /* Enable RX, TX interrupts */
810 val |= MAX310X_IRQ_RX | MAX310X_IRQ_TX;
811 regmap_write(s->regmap, MAX310X_IRQEN_REG, val);
812
813 mutex_unlock(&s->max310x_mutex);
814 927
815 return 0; 928 return 0;
816} 929}
817 930
818static void max310x_shutdown(struct uart_port *port) 931static void max310x_shutdown(struct uart_port *port)
819{ 932{
820 struct max310x_port *s = container_of(port, struct max310x_port, port); 933 struct max310x_port *s = dev_get_drvdata(port->dev);
821 934
822 /* Disable all interrupts */ 935 /* Disable all interrupts */
823 mutex_lock(&s->max310x_mutex); 936 max310x_port_write(port, MAX310X_IRQEN_REG, 0);
824 regmap_write(s->regmap, MAX310X_IRQEN_REG, 0);
825 mutex_unlock(&s->max310x_mutex);
826 937
827 if (s->pdata->suspend) 938 s->devtype->power(port, 0);
828 s->pdata->suspend(1);
829} 939}
830 940
831static const char *max310x_type(struct uart_port *port) 941static const char *max310x_type(struct uart_port *port)
832{ 942{
833 struct max310x_port *s = container_of(port, struct max310x_port, port); 943 struct max310x_port *s = dev_get_drvdata(port->dev);
834 944
835 return (port->type == PORT_MAX310X) ? s->name : NULL; 945 return (port->type == PORT_MAX310X) ? s->devtype->name : NULL;
836} 946}
837 947
838static int max310x_request_port(struct uart_port *port) 948static int max310x_request_port(struct uart_port *port)
@@ -841,134 +951,99 @@ static int max310x_request_port(struct uart_port *port)
841 return 0; 951 return 0;
842} 952}
843 953
844static void max310x_release_port(struct uart_port *port)
845{
846 /* Do nothing */
847}
848
849static void max310x_config_port(struct uart_port *port, int flags) 954static void max310x_config_port(struct uart_port *port, int flags)
850{ 955{
851 if (flags & UART_CONFIG_TYPE) 956 if (flags & UART_CONFIG_TYPE)
852 port->type = PORT_MAX310X; 957 port->type = PORT_MAX310X;
853} 958}
854 959
855static int max310x_verify_port(struct uart_port *port, struct serial_struct *ser) 960static int max310x_verify_port(struct uart_port *port, struct serial_struct *s)
856{ 961{
857 if ((ser->type == PORT_UNKNOWN) || (ser->type == PORT_MAX310X)) 962 if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X))
858 return 0; 963 return -EINVAL;
859 if (ser->irq == port->irq) 964 if (s->irq != port->irq)
860 return 0; 965 return -EINVAL;
861 966
862 return -EINVAL; 967 return 0;
863} 968}
864 969
865static struct uart_ops max310x_ops = { 970static void max310x_null_void(struct uart_port *port)
971{
972 /* Do nothing */
973}
974
975static const struct uart_ops max310x_ops = {
866 .tx_empty = max310x_tx_empty, 976 .tx_empty = max310x_tx_empty,
867 .set_mctrl = max310x_set_mctrl, 977 .set_mctrl = max310x_set_mctrl,
868 .get_mctrl = max310x_get_mctrl, 978 .get_mctrl = max310x_get_mctrl,
869 .stop_tx = max310x_stop_tx, 979 .stop_tx = max310x_null_void,
870 .start_tx = max310x_start_tx, 980 .start_tx = max310x_start_tx,
871 .stop_rx = max310x_stop_rx, 981 .stop_rx = max310x_null_void,
872 .enable_ms = max310x_enable_ms, 982 .enable_ms = max310x_null_void,
873 .break_ctl = max310x_break_ctl, 983 .break_ctl = max310x_break_ctl,
874 .startup = max310x_startup, 984 .startup = max310x_startup,
875 .shutdown = max310x_shutdown, 985 .shutdown = max310x_shutdown,
876 .set_termios = max310x_set_termios, 986 .set_termios = max310x_set_termios,
877 .type = max310x_type, 987 .type = max310x_type,
878 .request_port = max310x_request_port, 988 .request_port = max310x_request_port,
879 .release_port = max310x_release_port, 989 .release_port = max310x_null_void,
880 .config_port = max310x_config_port, 990 .config_port = max310x_config_port,
881 .verify_port = max310x_verify_port, 991 .verify_port = max310x_verify_port,
882}; 992};
883 993
884#ifdef CONFIG_PM_SLEEP 994static int __maybe_unused max310x_suspend(struct device *dev)
885
886static int max310x_suspend(struct device *dev)
887{ 995{
888 int ret;
889 struct max310x_port *s = dev_get_drvdata(dev); 996 struct max310x_port *s = dev_get_drvdata(dev);
997 int i;
890 998
891 dev_dbg(dev, "Suspend\n"); 999 for (i = 0; i < s->uart.nr; i++) {
892 1000 uart_suspend_port(&s->uart, &s->p[i].port);
893 ret = uart_suspend_port(&s->uart, &s->port); 1001 s->devtype->power(&s->p[i].port, 0);
894 1002 }
895 mutex_lock(&s->max310x_mutex);
896
897 /* Enable sleep mode */
898 regmap_update_bits(s->regmap, MAX310X_MODE1_REG,
899 MAX310X_MODE1_FORCESLEEP_BIT,
900 MAX310X_MODE1_FORCESLEEP_BIT);
901
902 mutex_unlock(&s->max310x_mutex);
903
904 if (s->pdata->suspend)
905 s->pdata->suspend(1);
906 1003
907 return ret; 1004 return 0;
908} 1005}
909 1006
910static int max310x_resume(struct device *dev) 1007static int __maybe_unused max310x_resume(struct device *dev)
911{ 1008{
912 struct max310x_port *s = dev_get_drvdata(dev); 1009 struct max310x_port *s = dev_get_drvdata(dev);
1010 int i;
913 1011
914 dev_dbg(dev, "Resume\n"); 1012 for (i = 0; i < s->uart.nr; i++) {
915 1013 s->devtype->power(&s->p[i].port, 1);
916 if (s->pdata->suspend) 1014 uart_resume_port(&s->uart, &s->p[i].port);
917 s->pdata->suspend(0); 1015 }
918
919 mutex_lock(&s->max310x_mutex);
920
921 /* Disable sleep mode */
922 regmap_update_bits(s->regmap, MAX310X_MODE1_REG,
923 MAX310X_MODE1_FORCESLEEP_BIT,
924 0);
925
926 max310x_wait_pll(s);
927
928 mutex_unlock(&s->max310x_mutex);
929 1016
930 return uart_resume_port(&s->uart, &s->port); 1017 return 0;
931} 1018}
932 1019
933static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
934#define MAX310X_PM_OPS (&max310x_pm_ops)
935
936#else
937#define MAX310X_PM_OPS NULL
938#endif
939
940#ifdef CONFIG_GPIOLIB 1020#ifdef CONFIG_GPIOLIB
941static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset) 1021static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset)
942{ 1022{
943 unsigned int val = 0; 1023 unsigned int val;
944 struct max310x_port *s = container_of(chip, struct max310x_port, gpio); 1024 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
1025 struct uart_port *port = &s->p[offset / 4].port;
945 1026
946 mutex_lock(&s->max310x_mutex); 1027 val = max310x_port_read(port, MAX310X_GPIODATA_REG);
947 regmap_read(s->regmap, MAX310X_GPIODATA_REG, &val);
948 mutex_unlock(&s->max310x_mutex);
949 1028
950 return !!((val >> 4) & (1 << offset)); 1029 return !!((val >> 4) & (1 << (offset % 4)));
951} 1030}
952 1031
953static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 1032static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
954{ 1033{
955 struct max310x_port *s = container_of(chip, struct max310x_port, gpio); 1034 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
1035 struct uart_port *port = &s->p[offset / 4].port;
956 1036
957 mutex_lock(&s->max310x_mutex); 1037 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
958 regmap_update_bits(s->regmap, MAX310X_GPIODATA_REG, 1 << offset, value ? 1038 value ? 1 << (offset % 4) : 0);
959 1 << offset : 0);
960 mutex_unlock(&s->max310x_mutex);
961} 1039}
962 1040
963static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 1041static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
964{ 1042{
965 struct max310x_port *s = container_of(chip, struct max310x_port, gpio); 1043 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
1044 struct uart_port *port = &s->p[offset / 4].port;
966 1045
967 mutex_lock(&s->max310x_mutex); 1046 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0);
968
969 regmap_update_bits(s->regmap, MAX310X_GPIOCFG_REG, 1 << offset, 0);
970
971 mutex_unlock(&s->max310x_mutex);
972 1047
973 return 0; 1048 return 0;
974} 1049}
@@ -977,74 +1052,42 @@ static int max310x_gpio_direction_output(struct gpio_chip *chip,
977 unsigned offset, int value) 1052 unsigned offset, int value)
978{ 1053{
979 struct max310x_port *s = container_of(chip, struct max310x_port, gpio); 1054 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
1055 struct uart_port *port = &s->p[offset / 4].port;
980 1056
981 mutex_lock(&s->max310x_mutex); 1057 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
982 1058 value ? 1 << (offset % 4) : 0);
983 regmap_update_bits(s->regmap, MAX310X_GPIOCFG_REG, 1 << offset, 1059 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4),
984 1 << offset); 1060 1 << (offset % 4));
985 regmap_update_bits(s->regmap, MAX310X_GPIODATA_REG, 1 << offset, value ?
986 1 << offset : 0);
987
988 mutex_unlock(&s->max310x_mutex);
989 1061
990 return 0; 1062 return 0;
991} 1063}
992#endif 1064#endif
993 1065
994/* Generic platform data */ 1066static int max310x_probe(struct device *dev, int is_spi,
995static struct max310x_pdata generic_plat_data = { 1067 struct max310x_devtype *devtype, int irq)
996 .driver_flags = MAX310X_EXT_CLK,
997 .uart_flags[0] = MAX310X_ECHO_SUPRESS,
998 .frequency = 26000000,
999};
1000
1001static int max310x_probe(struct spi_device *spi)
1002{ 1068{
1003 struct max310x_port *s; 1069 struct max310x_port *s;
1004 struct device *dev = &spi->dev; 1070 struct max310x_pdata *pdata = dev_get_platdata(dev);
1005 int chiptype = spi_get_device_id(spi)->driver_data; 1071 int i, ret, uartclk;
1006 struct max310x_pdata *pdata = dev->platform_data;
1007 unsigned int val = 0;
1008 int ret;
1009 1072
1010 /* Check for IRQ */ 1073 /* Check for IRQ */
1011 if (spi->irq <= 0) { 1074 if (irq <= 0) {
1012 dev_err(dev, "No IRQ specified\n"); 1075 dev_err(dev, "No IRQ specified\n");
1013 return -ENOTSUPP; 1076 return -ENOTSUPP;
1014 } 1077 }
1015 1078
1079 if (!pdata) {
1080 dev_err(dev, "No platform data supplied\n");
1081 return -EINVAL;
1082 }
1083
1016 /* Alloc port structure */ 1084 /* Alloc port structure */
1017 s = devm_kzalloc(dev, sizeof(struct max310x_port), GFP_KERNEL); 1085 s = devm_kzalloc(dev, sizeof(*s) +
1086 sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL);
1018 if (!s) { 1087 if (!s) {
1019 dev_err(dev, "Error allocating port structure\n"); 1088 dev_err(dev, "Error allocating port structure\n");
1020 return -ENOMEM; 1089 return -ENOMEM;
1021 } 1090 }
1022 dev_set_drvdata(dev, s);
1023
1024 if (!pdata) {
1025 dev_warn(dev, "No platform data supplied, using defaults\n");
1026 pdata = &generic_plat_data;
1027 }
1028 s->pdata = pdata;
1029
1030 /* Individual chip settings */
1031 switch (chiptype) {
1032 case MAX310X_TYPE_MAX3107:
1033 s->name = "MAX3107";
1034 s->nr_gpio = 4;
1035 s->uart.nr = 1;
1036 s->regcfg.max_register = 0x1f;
1037 break;
1038 case MAX310X_TYPE_MAX3108:
1039 s->name = "MAX3108";
1040 s->nr_gpio = 4;
1041 s->uart.nr = 1;
1042 s->regcfg.max_register = 0x1e;
1043 break;
1044 default:
1045 dev_err(dev, "Unsupported chip type %i\n", chiptype);
1046 return -ENOTSUPP;
1047 }
1048 1091
1049 /* Check input frequency */ 1092 /* Check input frequency */
1050 if ((pdata->driver_flags & MAX310X_EXT_CLK) && 1093 if ((pdata->driver_flags & MAX310X_EXT_CLK) &&
@@ -1055,13 +1098,11 @@ static int max310x_probe(struct spi_device *spi)
1055 ((pdata->frequency < 1000000) || (pdata->frequency > 4000000))) 1098 ((pdata->frequency < 1000000) || (pdata->frequency > 4000000)))
1056 goto err_freq; 1099 goto err_freq;
1057 1100
1058 mutex_init(&s->max310x_mutex); 1101 s->pdata = pdata;
1102 s->devtype = devtype;
1103 dev_set_drvdata(dev, s);
1059 1104
1060 /* Setup SPI bus */ 1105 mutex_init(&s->mutex);
1061 spi->mode = SPI_MODE_0;
1062 spi->bits_per_word = 8;
1063 spi->max_speed_hz = 26000000;
1064 spi_setup(spi);
1065 1106
1066 /* Setup regmap */ 1107 /* Setup regmap */
1067 s->regcfg.reg_bits = 8; 1108 s->regcfg.reg_bits = 8;
@@ -1069,109 +1110,100 @@ static int max310x_probe(struct spi_device *spi)
1069 s->regcfg.read_flag_mask = 0x00; 1110 s->regcfg.read_flag_mask = 0x00;
1070 s->regcfg.write_flag_mask = 0x80; 1111 s->regcfg.write_flag_mask = 0x80;
1071 s->regcfg.cache_type = REGCACHE_RBTREE; 1112 s->regcfg.cache_type = REGCACHE_RBTREE;
1072 s->regcfg.writeable_reg = max3107_8_reg_writeable; 1113 s->regcfg.writeable_reg = max310x_reg_writeable;
1073 s->regcfg.volatile_reg = max310x_reg_volatile; 1114 s->regcfg.volatile_reg = max310x_reg_volatile;
1074 s->regcfg.precious_reg = max310x_reg_precious; 1115 s->regcfg.precious_reg = max310x_reg_precious;
1075 s->regmap = devm_regmap_init_spi(spi, &s->regcfg); 1116 s->regcfg.max_register = devtype->nr * 0x20 - 1;
1117
1118 if (IS_ENABLED(CONFIG_SPI_MASTER) && is_spi) {
1119 struct spi_device *spi = to_spi_device(dev);
1120
1121 s->regmap = devm_regmap_init_spi(spi, &s->regcfg);
1122 } else
1123 return -ENOTSUPP;
1124
1076 if (IS_ERR(s->regmap)) { 1125 if (IS_ERR(s->regmap)) {
1077 ret = PTR_ERR(s->regmap);
1078 dev_err(dev, "Failed to initialize register map\n"); 1126 dev_err(dev, "Failed to initialize register map\n");
1079 goto err_out; 1127 return PTR_ERR(s->regmap);
1080 }
1081
1082 /* Reset chip & check SPI function */
1083 ret = regmap_write(s->regmap, MAX310X_MODE2_REG, MAX310X_MODE2_RST_BIT);
1084 if (ret) {
1085 dev_err(dev, "SPI transfer failed\n");
1086 goto err_out;
1087 }
1088 /* Clear chip reset */
1089 regmap_write(s->regmap, MAX310X_MODE2_REG, 0);
1090
1091 switch (chiptype) {
1092 case MAX310X_TYPE_MAX3107:
1093 /* Check REV ID to ensure we are talking to what we expect */
1094 regmap_read(s->regmap, MAX3107_REVID_REG, &val);
1095 if (((val & MAX3107_REV_MASK) != MAX3107_REV_ID)) {
1096 dev_err(dev, "%s ID 0x%02x does not match\n",
1097 s->name, val);
1098 ret = -ENODEV;
1099 goto err_out;
1100 }
1101 break;
1102 case MAX310X_TYPE_MAX3108:
1103 /* MAX3108 have not REV ID register, we just check default value
1104 * from clocksource register to make sure everything works.
1105 */
1106 regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
1107 if (val != (MAX310X_CLKSRC_EXTCLK_BIT |
1108 MAX310X_CLKSRC_PLLBYP_BIT)) {
1109 dev_err(dev, "%s not present\n", s->name);
1110 ret = -ENODEV;
1111 goto err_out;
1112 }
1113 break;
1114 } 1128 }
1115 1129
1116 /* Board specific configure */ 1130 /* Board specific configure */
1117 if (pdata->init) 1131 if (s->pdata->init)
1118 pdata->init(); 1132 s->pdata->init();
1119 if (pdata->suspend) 1133
1120 pdata->suspend(0); 1134 /* Check device to ensure we are talking to what we expect */
1121 1135 ret = devtype->detect(dev);
1122 /* Calculate referecne clock */ 1136 if (ret)
1123 s->uartclk = max310x_set_ref_clk(s); 1137 return ret;
1124 1138
1125 /* Disable all interrupts */ 1139 for (i = 0; i < devtype->nr; i++) {
1126 regmap_write(s->regmap, MAX310X_IRQEN_REG, 0); 1140 unsigned int offs = i << 5;
1127 1141
1128 /* Setup MODE1 register */ 1142 /* Reset port */
1129 val = MAX310X_MODE1_IRQSEL_BIT; /* Enable IRQ pin */ 1143 regmap_write(s->regmap, MAX310X_MODE2_REG + offs,
1130 if (pdata->driver_flags & MAX310X_AUTOSLEEP) 1144 MAX310X_MODE2_RST_BIT);
1131 val = MAX310X_MODE1_AUTOSLEEP_BIT; 1145 /* Clear port reset */
1132 regmap_write(s->regmap, MAX310X_MODE1_REG, val); 1146 regmap_write(s->regmap, MAX310X_MODE2_REG + offs, 0);
1133 1147
1134 /* Setup interrupt */ 1148 /* Wait for port startup */
1135 ret = devm_request_threaded_irq(dev, spi->irq, NULL, max310x_ist, 1149 do {
1136 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1150 regmap_read(s->regmap,
1137 dev_name(dev), s); 1151 MAX310X_BRGDIVLSB_REG + offs, &ret);
1138 if (ret) { 1152 } while (ret != 0x01);
1139 dev_err(dev, "Unable to reguest IRQ %i\n", spi->irq); 1153
1140 goto err_out; 1154 regmap_update_bits(s->regmap, MAX310X_MODE1_REG + offs,
1155 MAX310X_MODE1_AUTOSLEEP_BIT,
1156 MAX310X_MODE1_AUTOSLEEP_BIT);
1141 } 1157 }
1142 1158
1159 uartclk = max310x_set_ref_clk(s);
1160 dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
1161
1143 /* Register UART driver */ 1162 /* Register UART driver */
1144 s->uart.owner = THIS_MODULE; 1163 s->uart.owner = THIS_MODULE;
1145 s->uart.driver_name = dev_name(dev);
1146 s->uart.dev_name = "ttyMAX"; 1164 s->uart.dev_name = "ttyMAX";
1147 s->uart.major = MAX310X_MAJOR; 1165 s->uart.major = MAX310X_MAJOR;
1148 s->uart.minor = MAX310X_MINOR; 1166 s->uart.minor = MAX310X_MINOR;
1167 s->uart.nr = devtype->nr;
1149 ret = uart_register_driver(&s->uart); 1168 ret = uart_register_driver(&s->uart);
1150 if (ret) { 1169 if (ret) {
1151 dev_err(dev, "Registering UART driver failed\n"); 1170 dev_err(dev, "Registering UART driver failed\n");
1152 goto err_out; 1171 return ret;
1153 } 1172 }
1154 1173
1155 /* Initialize workqueue for start TX */ 1174 for (i = 0; i < devtype->nr; i++) {
1156 s->wq = create_freezable_workqueue(dev_name(dev)); 1175 /* Initialize port data */
1157 INIT_WORK(&s->tx_work, max310x_wq_proc); 1176 s->p[i].port.line = i;
1158 1177 s->p[i].port.dev = dev;
1159 /* Initialize UART port data */ 1178 s->p[i].port.irq = irq;
1160 s->port.line = 0; 1179 s->p[i].port.type = PORT_MAX310X;
1161 s->port.dev = dev; 1180 s->p[i].port.fifosize = MAX310X_FIFO_SIZE;
1162 s->port.irq = spi->irq; 1181 s->p[i].port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE |
1163 s->port.type = PORT_MAX310X; 1182 UPF_LOW_LATENCY;
1164 s->port.fifosize = MAX310X_FIFO_SIZE; 1183 s->p[i].port.iotype = UPIO_PORT;
1165 s->port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE; 1184 s->p[i].port.iobase = i * 0x20;
1166 s->port.iotype = UPIO_PORT; 1185 s->p[i].port.membase = (void __iomem *)~0;
1167 s->port.membase = (void __iomem *)0xffffffff; /* Bogus value */ 1186 s->p[i].port.uartclk = uartclk;
1168 s->port.uartclk = s->uartclk; 1187 s->p[i].port.ops = &max310x_ops;
1169 s->port.ops = &max310x_ops; 1188 /* Disable all interrupts */
1170 uart_add_one_port(&s->uart, &s->port); 1189 max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0);
1190 /* Clear IRQ status register */
1191 max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG);
1192 /* Enable IRQ pin */
1193 max310x_port_update(&s->p[i].port, MAX310X_MODE1_REG,
1194 MAX310X_MODE1_IRQSEL_BIT,
1195 MAX310X_MODE1_IRQSEL_BIT);
1196 /* Initialize queue for start TX */
1197 INIT_WORK(&s->p[i].tx_work, max310x_wq_proc);
1198 /* Register port */
1199 uart_add_one_port(&s->uart, &s->p[i].port);
1200 /* Go to suspend mode */
1201 devtype->power(&s->p[i].port, 0);
1202 }
1171 1203
1172#ifdef CONFIG_GPIOLIB 1204#ifdef CONFIG_GPIOLIB
1173 /* Setup GPIO cotroller */ 1205 /* Setup GPIO cotroller */
1174 if (pdata->gpio_base) { 1206 if (s->pdata->gpio_base) {
1175 s->gpio.owner = THIS_MODULE; 1207 s->gpio.owner = THIS_MODULE;
1176 s->gpio.dev = dev; 1208 s->gpio.dev = dev;
1177 s->gpio.label = dev_name(dev); 1209 s->gpio.label = dev_name(dev);
@@ -1179,86 +1211,107 @@ static int max310x_probe(struct spi_device *spi)
1179 s->gpio.get = max310x_gpio_get; 1211 s->gpio.get = max310x_gpio_get;
1180 s->gpio.direction_output= max310x_gpio_direction_output; 1212 s->gpio.direction_output= max310x_gpio_direction_output;
1181 s->gpio.set = max310x_gpio_set; 1213 s->gpio.set = max310x_gpio_set;
1182 s->gpio.base = pdata->gpio_base; 1214 s->gpio.base = s->pdata->gpio_base;
1183 s->gpio.ngpio = s->nr_gpio; 1215 s->gpio.ngpio = devtype->nr * 4;
1184 s->gpio.can_sleep = 1; 1216 s->gpio.can_sleep = 1;
1185 if (gpiochip_add(&s->gpio)) { 1217 if (!gpiochip_add(&s->gpio))
1186 /* Indicate that we should not call gpiochip_remove */ 1218 s->gpio_used = 1;
1187 s->gpio.base = 0;
1188 }
1189 } else 1219 } else
1190 dev_info(dev, "GPIO support not enabled\n"); 1220 dev_info(dev, "GPIO support not enabled\n");
1191#endif 1221#endif
1192 1222
1193 /* Go to suspend mode */ 1223 /* Setup interrupt */
1194 if (pdata->suspend) 1224 ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
1195 pdata->suspend(1); 1225 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1226 dev_name(dev), s);
1227 if (ret) {
1228 dev_err(dev, "Unable to reguest IRQ %i\n", irq);
1229#ifdef CONFIG_GPIOLIB
1230 if (s->gpio_used)
1231 WARN_ON(gpiochip_remove(&s->gpio));
1232#endif
1233 }
1196 1234
1197 return 0; 1235 return ret;
1198 1236
1199err_freq: 1237err_freq:
1200 dev_err(dev, "Frequency parameter incorrect\n"); 1238 dev_err(dev, "Frequency parameter incorrect\n");
1201 ret = -EINVAL; 1239 return -EINVAL;
1202
1203err_out:
1204 dev_set_drvdata(dev, NULL);
1205
1206 return ret;
1207} 1240}
1208 1241
1209static int max310x_remove(struct spi_device *spi) 1242static int max310x_remove(struct device *dev)
1210{ 1243{
1211 struct device *dev = &spi->dev;
1212 struct max310x_port *s = dev_get_drvdata(dev); 1244 struct max310x_port *s = dev_get_drvdata(dev);
1213 int ret = 0; 1245 int i, ret = 0;
1214
1215 dev_dbg(dev, "Removing port\n");
1216
1217 devm_free_irq(dev, s->port.irq, s);
1218
1219 destroy_workqueue(s->wq);
1220 1246
1221 uart_remove_one_port(&s->uart, &s->port); 1247 for (i = 0; i < s->uart.nr; i++) {
1248 cancel_work_sync(&s->p[i].tx_work);
1249 uart_remove_one_port(&s->uart, &s->p[i].port);
1250 s->devtype->power(&s->p[i].port, 0);
1251 }
1222 1252
1223 uart_unregister_driver(&s->uart); 1253 uart_unregister_driver(&s->uart);
1224 1254
1225#ifdef CONFIG_GPIOLIB 1255#ifdef CONFIG_GPIOLIB
1226 if (s->pdata->gpio_base) { 1256 if (s->gpio_used)
1227 ret = gpiochip_remove(&s->gpio); 1257 ret = gpiochip_remove(&s->gpio);
1228 if (ret)
1229 dev_err(dev, "Failed to remove gpio chip: %d\n", ret);
1230 }
1231#endif 1258#endif
1232 1259
1233 dev_set_drvdata(dev, NULL);
1234
1235 if (s->pdata->suspend)
1236 s->pdata->suspend(1);
1237 if (s->pdata->exit) 1260 if (s->pdata->exit)
1238 s->pdata->exit(); 1261 s->pdata->exit();
1239 1262
1240 return ret; 1263 return ret;
1241} 1264}
1242 1265
1266#ifdef CONFIG_SPI_MASTER
1267static int max310x_spi_probe(struct spi_device *spi)
1268{
1269 struct max310x_devtype *devtype =
1270 (struct max310x_devtype *)spi_get_device_id(spi)->driver_data;
1271 int ret;
1272
1273 /* Setup SPI bus */
1274 spi->bits_per_word = 8;
1275 spi->mode = spi->mode ? : SPI_MODE_0;
1276 spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
1277 ret = spi_setup(spi);
1278 if (ret) {
1279 dev_err(&spi->dev, "SPI setup failed\n");
1280 return ret;
1281 }
1282
1283 return max310x_probe(&spi->dev, 1, devtype, spi->irq);
1284}
1285
1286static int max310x_spi_remove(struct spi_device *spi)
1287{
1288 return max310x_remove(&spi->dev);
1289}
1290
1291static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
1292
1243static const struct spi_device_id max310x_id_table[] = { 1293static const struct spi_device_id max310x_id_table[] = {
1244 { "max3107", MAX310X_TYPE_MAX3107 }, 1294 { "max3107", (kernel_ulong_t)&max3107_devtype, },
1245 { "max3108", MAX310X_TYPE_MAX3108 }, 1295 { "max3108", (kernel_ulong_t)&max3108_devtype, },
1296 { "max3109", (kernel_ulong_t)&max3109_devtype, },
1297 { "max14830", (kernel_ulong_t)&max14830_devtype, },
1246 { } 1298 { }
1247}; 1299};
1248MODULE_DEVICE_TABLE(spi, max310x_id_table); 1300MODULE_DEVICE_TABLE(spi, max310x_id_table);
1249 1301
1250static struct spi_driver max310x_driver = { 1302static struct spi_driver max310x_uart_driver = {
1251 .driver = { 1303 .driver = {
1252 .name = "max310x", 1304 .name = MAX310X_NAME,
1253 .owner = THIS_MODULE, 1305 .owner = THIS_MODULE,
1254 .pm = MAX310X_PM_OPS, 1306 .pm = &max310x_pm_ops,
1255 }, 1307 },
1256 .probe = max310x_probe, 1308 .probe = max310x_spi_probe,
1257 .remove = max310x_remove, 1309 .remove = max310x_spi_remove,
1258 .id_table = max310x_id_table, 1310 .id_table = max310x_id_table,
1259}; 1311};
1260module_spi_driver(max310x_driver); 1312module_spi_driver(max310x_uart_driver);
1313#endif
1261 1314
1262MODULE_LICENSE("GPL v2"); 1315MODULE_LICENSE("GPL");
1263MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); 1316MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1264MODULE_DESCRIPTION("MAX310X serial driver"); 1317MODULE_DESCRIPTION("MAX310X serial driver");
diff --git a/drivers/tty/serial/mcf.c b/drivers/tty/serial/mcf.c
index 65be0c00c4bf..a2e97c96e159 100644
--- a/drivers/tty/serial/mcf.c
+++ b/drivers/tty/serial/mcf.c
@@ -24,6 +24,7 @@
24#include <linux/serial_core.h> 24#include <linux/serial_core.h>
25#include <linux/io.h> 25#include <linux/io.h>
26#include <linux/uaccess.h> 26#include <linux/uaccess.h>
27#include <linux/platform_device.h>
27#include <asm/coldfire.h> 28#include <asm/coldfire.h>
28#include <asm/mcfsim.h> 29#include <asm/mcfsim.h>
29#include <asm/mcfuart.h> 30#include <asm/mcfuart.h>
@@ -644,7 +645,7 @@ static struct uart_driver mcf_driver = {
644 645
645static int mcf_probe(struct platform_device *pdev) 646static int mcf_probe(struct platform_device *pdev)
646{ 647{
647 struct mcf_platform_uart *platp = pdev->dev.platform_data; 648 struct mcf_platform_uart *platp = dev_get_platdata(&pdev->dev);
648 struct uart_port *port; 649 struct uart_port *port;
649 int i; 650 int i;
650 651
diff --git a/drivers/tty/serial/mpsc.c b/drivers/tty/serial/mpsc.c
index bc24f4931670..fba52578d02f 100644
--- a/drivers/tty/serial/mpsc.c
+++ b/drivers/tty/serial/mpsc.c
@@ -1884,7 +1884,7 @@ static int mpsc_shared_drv_probe(struct platform_device *dev)
1884 if (dev->id == 0) { 1884 if (dev->id == 0) {
1885 if (!(rc = mpsc_shared_map_regs(dev))) { 1885 if (!(rc = mpsc_shared_map_regs(dev))) {
1886 pdata = (struct mpsc_shared_pdata *) 1886 pdata = (struct mpsc_shared_pdata *)
1887 dev->dev.platform_data; 1887 dev_get_platdata(&dev->dev);
1888 1888
1889 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val; 1889 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val;
1890 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val; 1890 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val;
@@ -2025,7 +2025,7 @@ static void mpsc_drv_get_platform_data(struct mpsc_port_info *pi,
2025{ 2025{
2026 struct mpsc_pdata *pdata; 2026 struct mpsc_pdata *pdata;
2027 2027
2028 pdata = (struct mpsc_pdata *)pd->dev.platform_data; 2028 pdata = (struct mpsc_pdata *)dev_get_platdata(&pd->dev);
2029 2029
2030 pi->port.uartclk = pdata->brg_clk_freq; 2030 pi->port.uartclk = pdata->brg_clk_freq;
2031 pi->port.iotype = UPIO_MEM; 2031 pi->port.iotype = UPIO_MEM;
diff --git a/drivers/tty/serial/msm_serial.c b/drivers/tty/serial/msm_serial.c
index 2c6cfb3cf032..252d514b47fb 100644
--- a/drivers/tty/serial/msm_serial.c
+++ b/drivers/tty/serial/msm_serial.c
@@ -45,16 +45,19 @@ struct msm_port {
45 struct clk *clk; 45 struct clk *clk;
46 struct clk *pclk; 46 struct clk *pclk;
47 unsigned int imr; 47 unsigned int imr;
48 unsigned int *gsbi_base; 48 void __iomem *gsbi_base;
49 int is_uartdm; 49 int is_uartdm;
50 unsigned int old_snap_state; 50 unsigned int old_snap_state;
51}; 51};
52 52
53static inline void wait_for_xmitr(struct uart_port *port, int bits) 53static inline void wait_for_xmitr(struct uart_port *port)
54{ 54{
55 if (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) 55 while (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) {
56 while ((msm_read(port, UART_ISR) & bits) != bits) 56 if (msm_read(port, UART_ISR) & UART_ISR_TX_READY)
57 cpu_relax(); 57 break;
58 udelay(1);
59 }
60 msm_write(port, UART_CR_CMD_RESET_TX_READY, UART_CR);
58} 61}
59 62
60static void msm_stop_tx(struct uart_port *port) 63static void msm_stop_tx(struct uart_port *port)
@@ -192,49 +195,63 @@ static void handle_rx(struct uart_port *port)
192 tty_flip_buffer_push(tport); 195 tty_flip_buffer_push(tport);
193} 196}
194 197
195static void reset_dm_count(struct uart_port *port) 198static void reset_dm_count(struct uart_port *port, int count)
196{ 199{
197 wait_for_xmitr(port, UART_ISR_TX_READY); 200 wait_for_xmitr(port);
198 msm_write(port, 1, UARTDM_NCF_TX); 201 msm_write(port, count, UARTDM_NCF_TX);
202 msm_read(port, UARTDM_NCF_TX);
199} 203}
200 204
201static void handle_tx(struct uart_port *port) 205static void handle_tx(struct uart_port *port)
202{ 206{
203 struct circ_buf *xmit = &port->state->xmit; 207 struct circ_buf *xmit = &port->state->xmit;
204 struct msm_port *msm_port = UART_TO_MSM(port); 208 struct msm_port *msm_port = UART_TO_MSM(port);
205 int sent_tx; 209 unsigned int tx_count, num_chars;
210 unsigned int tf_pointer = 0;
211
212 tx_count = uart_circ_chars_pending(xmit);
213 tx_count = min3(tx_count, (unsigned int)UART_XMIT_SIZE - xmit->tail,
214 port->fifosize);
206 215
207 if (port->x_char) { 216 if (port->x_char) {
208 if (msm_port->is_uartdm) 217 if (msm_port->is_uartdm)
209 reset_dm_count(port); 218 reset_dm_count(port, tx_count + 1);
210 219
211 msm_write(port, port->x_char, 220 msm_write(port, port->x_char,
212 msm_port->is_uartdm ? UARTDM_TF : UART_TF); 221 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
213 port->icount.tx++; 222 port->icount.tx++;
214 port->x_char = 0; 223 port->x_char = 0;
224 } else if (tx_count && msm_port->is_uartdm) {
225 reset_dm_count(port, tx_count);
215 } 226 }
216 227
217 if (msm_port->is_uartdm) 228 while (tf_pointer < tx_count) {
218 reset_dm_count(port); 229 int i;
230 char buf[4] = { 0 };
231 unsigned int *bf = (unsigned int *)&buf;
219 232
220 while (msm_read(port, UART_SR) & UART_SR_TX_READY) { 233 if (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
221 if (uart_circ_empty(xmit)) {
222 /* disable tx interrupts */
223 msm_port->imr &= ~UART_IMR_TXLEV;
224 msm_write(port, msm_port->imr, UART_IMR);
225 break; 234 break;
226 }
227 msm_write(port, xmit->buf[xmit->tail],
228 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
229 235
230 if (msm_port->is_uartdm) 236 if (msm_port->is_uartdm)
231 reset_dm_count(port); 237 num_chars = min(tx_count - tf_pointer, sizeof(buf));
238 else
239 num_chars = 1;
232 240
233 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 241 for (i = 0; i < num_chars; i++) {
234 port->icount.tx++; 242 buf[i] = xmit->buf[xmit->tail + i];
235 sent_tx = 1; 243 port->icount.tx++;
244 }
245
246 msm_write(port, *bf, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
247 xmit->tail = (xmit->tail + num_chars) & (UART_XMIT_SIZE - 1);
248 tf_pointer += num_chars;
236 } 249 }
237 250
251 /* disable tx interrupts if nothing more to send */
252 if (uart_circ_empty(xmit))
253 msm_stop_tx(port);
254
238 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 255 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
239 uart_write_wakeup(port); 256 uart_write_wakeup(port);
240} 257}
@@ -295,7 +312,7 @@ static void msm_reset(struct uart_port *port)
295 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR); 312 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
296} 313}
297 314
298void msm_set_mctrl(struct uart_port *port, unsigned int mctrl) 315static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
299{ 316{
300 unsigned int mr; 317 unsigned int mr;
301 mr = msm_read(port, UART_MR1); 318 mr = msm_read(port, UART_MR1);
@@ -318,70 +335,60 @@ static void msm_break_ctl(struct uart_port *port, int break_ctl)
318 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR); 335 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
319} 336}
320 337
338struct msm_baud_map {
339 u16 divisor;
340 u8 code;
341 u8 rxstale;
342};
343
344static const struct msm_baud_map *
345msm_find_best_baud(struct uart_port *port, unsigned int baud)
346{
347 unsigned int i, divisor;
348 const struct msm_baud_map *entry;
349 static const struct msm_baud_map table[] = {
350 { 1536, 0x00, 1 },
351 { 768, 0x11, 1 },
352 { 384, 0x22, 1 },
353 { 192, 0x33, 1 },
354 { 96, 0x44, 1 },
355 { 48, 0x55, 1 },
356 { 32, 0x66, 1 },
357 { 24, 0x77, 1 },
358 { 16, 0x88, 1 },
359 { 12, 0x99, 6 },
360 { 8, 0xaa, 6 },
361 { 6, 0xbb, 6 },
362 { 4, 0xcc, 6 },
363 { 3, 0xdd, 8 },
364 { 2, 0xee, 16 },
365 { 1, 0xff, 31 },
366 };
367
368 divisor = uart_get_divisor(port, baud);
369
370 for (i = 0, entry = table; i < ARRAY_SIZE(table); i++, entry++)
371 if (entry->divisor <= divisor)
372 break;
373
374 return entry; /* Default to smallest divider */
375}
376
321static int msm_set_baud_rate(struct uart_port *port, unsigned int baud) 377static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
322{ 378{
323 unsigned int baud_code, rxstale, watermark; 379 unsigned int rxstale, watermark;
324 struct msm_port *msm_port = UART_TO_MSM(port); 380 struct msm_port *msm_port = UART_TO_MSM(port);
381 const struct msm_baud_map *entry;
325 382
326 switch (baud) { 383 entry = msm_find_best_baud(port, baud);
327 case 300:
328 baud_code = UART_CSR_300;
329 rxstale = 1;
330 break;
331 case 600:
332 baud_code = UART_CSR_600;
333 rxstale = 1;
334 break;
335 case 1200:
336 baud_code = UART_CSR_1200;
337 rxstale = 1;
338 break;
339 case 2400:
340 baud_code = UART_CSR_2400;
341 rxstale = 1;
342 break;
343 case 4800:
344 baud_code = UART_CSR_4800;
345 rxstale = 1;
346 break;
347 case 9600:
348 baud_code = UART_CSR_9600;
349 rxstale = 2;
350 break;
351 case 14400:
352 baud_code = UART_CSR_14400;
353 rxstale = 3;
354 break;
355 case 19200:
356 baud_code = UART_CSR_19200;
357 rxstale = 4;
358 break;
359 case 28800:
360 baud_code = UART_CSR_28800;
361 rxstale = 6;
362 break;
363 case 38400:
364 baud_code = UART_CSR_38400;
365 rxstale = 8;
366 break;
367 case 57600:
368 baud_code = UART_CSR_57600;
369 rxstale = 16;
370 break;
371 case 115200:
372 default:
373 baud_code = UART_CSR_115200;
374 baud = 115200;
375 rxstale = 31;
376 break;
377 }
378 384
379 if (msm_port->is_uartdm) 385 if (msm_port->is_uartdm)
380 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR); 386 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
381 387
382 msm_write(port, baud_code, UART_CSR); 388 msm_write(port, entry->code, UART_CSR);
383 389
384 /* RX stale watermark */ 390 /* RX stale watermark */
391 rxstale = entry->rxstale;
385 watermark = UART_IPR_STALE_LSB & rxstale; 392 watermark = UART_IPR_STALE_LSB & rxstale;
386 watermark |= UART_IPR_RXSTALE_LAST; 393 watermark |= UART_IPR_RXSTALE_LAST;
387 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2); 394 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
@@ -589,12 +596,10 @@ static void msm_release_port(struct uart_port *port)
589 port->membase = NULL; 596 port->membase = NULL;
590 597
591 if (msm_port->gsbi_base) { 598 if (msm_port->gsbi_base) {
592 iowrite32(GSBI_PROTOCOL_IDLE, msm_port->gsbi_base + 599 writel_relaxed(GSBI_PROTOCOL_IDLE,
593 GSBI_CONTROL); 600 msm_port->gsbi_base + GSBI_CONTROL);
594
595 gsbi_resource = platform_get_resource(pdev,
596 IORESOURCE_MEM, 1);
597 601
602 gsbi_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
598 if (unlikely(!gsbi_resource)) 603 if (unlikely(!gsbi_resource))
599 return; 604 return;
600 605
@@ -637,7 +642,7 @@ static int msm_request_port(struct uart_port *port)
637 if (!request_mem_region(gsbi_resource->start, size, 642 if (!request_mem_region(gsbi_resource->start, size,
638 "msm_serial")) { 643 "msm_serial")) {
639 ret = -EBUSY; 644 ret = -EBUSY;
640 goto fail_release_port; 645 goto fail_release_port_membase;
641 } 646 }
642 647
643 msm_port->gsbi_base = ioremap(gsbi_resource->start, size); 648 msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
@@ -651,6 +656,8 @@ static int msm_request_port(struct uart_port *port)
651 656
652fail_release_gsbi: 657fail_release_gsbi:
653 release_mem_region(gsbi_resource->start, size); 658 release_mem_region(gsbi_resource->start, size);
659fail_release_port_membase:
660 iounmap(port->membase);
654fail_release_port: 661fail_release_port:
655 release_mem_region(port->mapbase, size); 662 release_mem_region(port->mapbase, size);
656 return ret; 663 return ret;
@@ -666,10 +673,9 @@ static void msm_config_port(struct uart_port *port, int flags)
666 if (ret) 673 if (ret)
667 return; 674 return;
668 } 675 }
669
670 if (msm_port->is_uartdm) 676 if (msm_port->is_uartdm)
671 iowrite32(GSBI_PROTOCOL_UART, msm_port->gsbi_base + 677 writel_relaxed(GSBI_PROTOCOL_UART,
672 GSBI_CONTROL); 678 msm_port->gsbi_base + GSBI_CONTROL);
673} 679}
674 680
675static int msm_verify_port(struct uart_port *port, struct serial_struct *ser) 681static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
@@ -766,7 +772,7 @@ static void msm_console_putchar(struct uart_port *port, int c)
766 struct msm_port *msm_port = UART_TO_MSM(port); 772 struct msm_port *msm_port = UART_TO_MSM(port);
767 773
768 if (msm_port->is_uartdm) 774 if (msm_port->is_uartdm)
769 reset_dm_count(port); 775 reset_dm_count(port, 1);
770 776
771 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY)) 777 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
772 ; 778 ;
diff --git a/drivers/tty/serial/msm_serial.h b/drivers/tty/serial/msm_serial.h
index e4acef5de77e..469fda50ac63 100644
--- a/drivers/tty/serial/msm_serial.h
+++ b/drivers/tty/serial/msm_serial.h
@@ -38,19 +38,7 @@
38#define UART_MR2_PARITY_MODE_SPACE 0x3 38#define UART_MR2_PARITY_MODE_SPACE 0x3
39#define UART_MR2_PARITY_MODE 0x3 39#define UART_MR2_PARITY_MODE 0x3
40 40
41#define UART_CSR 0x0008 41#define UART_CSR 0x0008
42#define UART_CSR_115200 0xFF
43#define UART_CSR_57600 0xEE
44#define UART_CSR_38400 0xDD
45#define UART_CSR_28800 0xCC
46#define UART_CSR_19200 0xBB
47#define UART_CSR_14400 0xAA
48#define UART_CSR_9600 0x99
49#define UART_CSR_4800 0x77
50#define UART_CSR_2400 0x55
51#define UART_CSR_1200 0x44
52#define UART_CSR_600 0x33
53#define UART_CSR_300 0x22
54 42
55#define UART_TF 0x000C 43#define UART_TF 0x000C
56#define UARTDM_TF 0x0070 44#define UARTDM_TF 0x0070
@@ -71,6 +59,7 @@
71#define UART_CR_CMD_RESET_RFR (14 << 4) 59#define UART_CR_CMD_RESET_RFR (14 << 4)
72#define UART_CR_CMD_PROTECTION_EN (16 << 4) 60#define UART_CR_CMD_PROTECTION_EN (16 << 4)
73#define UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4) 61#define UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4)
62#define UART_CR_CMD_RESET_TX_READY (3 << 8)
74#define UART_CR_TX_DISABLE (1 << 3) 63#define UART_CR_TX_DISABLE (1 << 3)
75#define UART_CR_TX_ENABLE (1 << 2) 64#define UART_CR_TX_ENABLE (1 << 2)
76#define UART_CR_RX_DISABLE (1 << 1) 65#define UART_CR_RX_DISABLE (1 << 1)
@@ -151,6 +140,7 @@ static inline void msm_serial_set_mnd_regs_tcxo(struct uart_port *port)
151 msm_write(port, 0xF1, UART_NREG); 140 msm_write(port, 0xF1, UART_NREG);
152 msm_write(port, 0x0F, UART_DREG); 141 msm_write(port, 0x0F, UART_DREG);
153 msm_write(port, 0x1A, UART_MNDREG); 142 msm_write(port, 0x1A, UART_MNDREG);
143 port->uartclk = 1843200;
154} 144}
155 145
156/* 146/*
@@ -162,6 +152,7 @@ static inline void msm_serial_set_mnd_regs_tcxoby4(struct uart_port *port)
162 msm_write(port, 0xF6, UART_NREG); 152 msm_write(port, 0xF6, UART_NREG);
163 msm_write(port, 0x0F, UART_DREG); 153 msm_write(port, 0x0F, UART_DREG);
164 msm_write(port, 0x0A, UART_MNDREG); 154 msm_write(port, 0x0A, UART_MNDREG);
155 port->uartclk = 1843200;
165} 156}
166 157
167static inline 158static inline
@@ -169,7 +160,7 @@ void msm_serial_set_mnd_regs_from_uartclk(struct uart_port *port)
169{ 160{
170 if (port->uartclk == 19200000) 161 if (port->uartclk == 19200000)
171 msm_serial_set_mnd_regs_tcxo(port); 162 msm_serial_set_mnd_regs_tcxo(port);
172 else 163 else if (port->uartclk == 4800000)
173 msm_serial_set_mnd_regs_tcxoby4(port); 164 msm_serial_set_mnd_regs_tcxoby4(port);
174} 165}
175 166
diff --git a/drivers/tty/serial/msm_serial_hs.c b/drivers/tty/serial/msm_serial_hs.c
index 4ca2f64861e6..48e94961a9e5 100644
--- a/drivers/tty/serial/msm_serial_hs.c
+++ b/drivers/tty/serial/msm_serial_hs.c
@@ -1618,7 +1618,7 @@ static int msm_hs_probe(struct platform_device *pdev)
1618 struct msm_hs_port *msm_uport; 1618 struct msm_hs_port *msm_uport;
1619 struct resource *resource; 1619 struct resource *resource;
1620 const struct msm_serial_hs_platform_data *pdata = 1620 const struct msm_serial_hs_platform_data *pdata =
1621 pdev->dev.platform_data; 1621 dev_get_platdata(&pdev->dev);
1622 1622
1623 if (pdev->id < 0 || pdev->id >= UARTDM_NR) { 1623 if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
1624 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id); 1624 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
index f85b8e6d0346..736e95c1ac4e 100644
--- a/drivers/tty/serial/mxs-auart.c
+++ b/drivers/tty/serial/mxs-auart.c
@@ -32,7 +32,6 @@
32#include <linux/clk.h> 32#include <linux/clk.h>
33#include <linux/delay.h> 33#include <linux/delay.h>
34#include <linux/io.h> 34#include <linux/io.h>
35#include <linux/pinctrl/consumer.h>
36#include <linux/of_device.h> 35#include <linux/of_device.h>
37#include <linux/dma-mapping.h> 36#include <linux/dma-mapping.h>
38#include <linux/dmaengine.h> 37#include <linux/dmaengine.h>
@@ -1021,7 +1020,6 @@ static int mxs_auart_probe(struct platform_device *pdev)
1021 u32 version; 1020 u32 version;
1022 int ret = 0; 1021 int ret = 0;
1023 struct resource *r; 1022 struct resource *r;
1024 struct pinctrl *pinctrl;
1025 1023
1026 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL); 1024 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
1027 if (!s) { 1025 if (!s) {
@@ -1035,12 +1033,6 @@ static int mxs_auart_probe(struct platform_device *pdev)
1035 else if (ret < 0) 1033 else if (ret < 0)
1036 goto out_free; 1034 goto out_free;
1037 1035
1038 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1039 if (IS_ERR(pinctrl)) {
1040 ret = PTR_ERR(pinctrl);
1041 goto out_free;
1042 }
1043
1044 if (of_id) { 1036 if (of_id) {
1045 pdev->id_entry = of_id->data; 1037 pdev->id_entry = of_id->data;
1046 s->devtype = pdev->id_entry->driver_data; 1038 s->devtype = pdev->id_entry->driver_data;
diff --git a/drivers/tty/serial/netx-serial.c b/drivers/tty/serial/netx-serial.c
index b9a40ed70be2..ce04f3f8c547 100644
--- a/drivers/tty/serial/netx-serial.c
+++ b/drivers/tty/serial/netx-serial.c
@@ -693,8 +693,6 @@ static int serial_netx_remove(struct platform_device *pdev)
693{ 693{
694 struct netx_port *sport = platform_get_drvdata(pdev); 694 struct netx_port *sport = platform_get_drvdata(pdev);
695 695
696 platform_set_drvdata(pdev, NULL);
697
698 if (sport) 696 if (sport)
699 uart_remove_one_port(&netx_reg, &sport->port); 697 uart_remove_one_port(&netx_reg, &sport->port);
700 698
diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
index b6d172873076..c7517063eb9b 100644
--- a/drivers/tty/serial/omap-serial.c
+++ b/drivers/tty/serial/omap-serial.c
@@ -40,7 +40,6 @@
40#include <linux/pm_runtime.h> 40#include <linux/pm_runtime.h>
41#include <linux/of.h> 41#include <linux/of.h>
42#include <linux/gpio.h> 42#include <linux/gpio.h>
43#include <linux/pinctrl/consumer.h>
44#include <linux/platform_data/serial-omap.h> 43#include <linux/platform_data/serial-omap.h>
45 44
46#define OMAP_MAX_HSUART_PORTS 6 45#define OMAP_MAX_HSUART_PORTS 6
@@ -52,6 +51,11 @@
52#define OMAP_UART_REV_52 0x0502 51#define OMAP_UART_REV_52 0x0502
53#define OMAP_UART_REV_63 0x0603 52#define OMAP_UART_REV_63 0x0603
54 53
54#define OMAP_UART_TX_WAKEUP_EN BIT(7)
55
56/* Feature flags */
57#define OMAP_UART_WER_HAS_TX_WAKEUP BIT(0)
58
55#define UART_ERRATA_i202_MDR1_ACCESS BIT(0) 59#define UART_ERRATA_i202_MDR1_ACCESS BIT(0)
56#define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1) 60#define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1)
57 61
@@ -137,6 +141,7 @@ struct uart_omap_port {
137 unsigned char dlh; 141 unsigned char dlh;
138 unsigned char mdr1; 142 unsigned char mdr1;
139 unsigned char scr; 143 unsigned char scr;
144 unsigned char wer;
140 145
141 int use_dma; 146 int use_dma;
142 /* 147 /*
@@ -151,6 +156,7 @@ struct uart_omap_port {
151 int context_loss_cnt; 156 int context_loss_cnt;
152 u32 errata; 157 u32 errata;
153 u8 wakeups_enabled; 158 u8 wakeups_enabled;
159 u32 features;
154 160
155 int DTR_gpio; 161 int DTR_gpio;
156 int DTR_inverted; 162 int DTR_inverted;
@@ -160,7 +166,6 @@ struct uart_omap_port {
160 u32 latency; 166 u32 latency;
161 u32 calc_latency; 167 u32 calc_latency;
162 struct work_struct qos_work; 168 struct work_struct qos_work;
163 struct pinctrl *pins;
164 bool is_suspending; 169 bool is_suspending;
165}; 170};
166 171
@@ -195,7 +200,7 @@ static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
195 200
196static int serial_omap_get_context_loss_count(struct uart_omap_port *up) 201static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
197{ 202{
198 struct omap_uart_port_info *pdata = up->dev->platform_data; 203 struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
199 204
200 if (!pdata || !pdata->get_context_loss_count) 205 if (!pdata || !pdata->get_context_loss_count)
201 return -EINVAL; 206 return -EINVAL;
@@ -205,7 +210,7 @@ static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
205 210
206static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable) 211static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
207{ 212{
208 struct omap_uart_port_info *pdata = up->dev->platform_data; 213 struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
209 214
210 if (!pdata || !pdata->enable_wakeup) 215 if (!pdata || !pdata->enable_wakeup)
211 return; 216 return;
@@ -310,7 +315,8 @@ static void transmit_chars(struct uart_omap_port *up, unsigned int lsr)
310 serial_omap_stop_tx(&up->port); 315 serial_omap_stop_tx(&up->port);
311 return; 316 return;
312 } 317 }
313 count = up->port.fifosize / 4; 318 count = up->port.fifosize -
319 (serial_in(up, UART_OMAP_TXFIFO_LVL) & 0xFF);
314 do { 320 do {
315 serial_out(up, UART_TX, xmit->buf[xmit->tail]); 321 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
316 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 322 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
@@ -479,7 +485,6 @@ static irqreturn_t serial_omap_irq(int irq, void *dev_id)
479 struct uart_omap_port *up = dev_id; 485 struct uart_omap_port *up = dev_id;
480 unsigned int iir, lsr; 486 unsigned int iir, lsr;
481 unsigned int type; 487 unsigned int type;
482 irqreturn_t ret = IRQ_NONE;
483 int max_count = 256; 488 int max_count = 256;
484 489
485 spin_lock(&up->port.lock); 490 spin_lock(&up->port.lock);
@@ -490,7 +495,6 @@ static irqreturn_t serial_omap_irq(int irq, void *dev_id)
490 if (iir & UART_IIR_NO_INT) 495 if (iir & UART_IIR_NO_INT)
491 break; 496 break;
492 497
493 ret = IRQ_HANDLED;
494 lsr = serial_in(up, UART_LSR); 498 lsr = serial_in(up, UART_LSR);
495 499
496 /* extract IRQ type from IIR register */ 500 /* extract IRQ type from IIR register */
@@ -529,7 +533,7 @@ static irqreturn_t serial_omap_irq(int irq, void *dev_id)
529 pm_runtime_put_autosuspend(up->dev); 533 pm_runtime_put_autosuspend(up->dev);
530 up->port_activity = jiffies; 534 up->port_activity = jiffies;
531 535
532 return ret; 536 return IRQ_HANDLED;
533} 537}
534 538
535static unsigned int serial_omap_tx_empty(struct uart_port *port) 539static unsigned int serial_omap_tx_empty(struct uart_port *port)
@@ -683,7 +687,11 @@ static int serial_omap_startup(struct uart_port *port)
683 serial_out(up, UART_IER, up->ier); 687 serial_out(up, UART_IER, up->ier);
684 688
685 /* Enable module level wake up */ 689 /* Enable module level wake up */
686 serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP); 690 up->wer = OMAP_UART_WER_MOD_WKUP;
691 if (up->features & OMAP_UART_WER_HAS_TX_WAKEUP)
692 up->wer |= OMAP_UART_TX_WAKEUP_EN;
693
694 serial_out(up, UART_OMAP_WER, up->wer);
687 695
688 pm_runtime_mark_last_busy(up->dev); 696 pm_runtime_mark_last_busy(up->dev);
689 pm_runtime_put_autosuspend(up->dev); 697 pm_runtime_put_autosuspend(up->dev);
@@ -1334,7 +1342,7 @@ static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
1334 u32 mvr, scheme; 1342 u32 mvr, scheme;
1335 u16 revision, major, minor; 1343 u16 revision, major, minor;
1336 1344
1337 mvr = serial_in(up, UART_OMAP_MVER); 1345 mvr = readl(up->port.membase + (UART_OMAP_MVER << up->port.regshift));
1338 1346
1339 /* Check revision register scheme */ 1347 /* Check revision register scheme */
1340 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT; 1348 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
@@ -1373,9 +1381,11 @@ static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
1373 case OMAP_UART_REV_52: 1381 case OMAP_UART_REV_52:
1374 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS | 1382 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1375 UART_ERRATA_i291_DMA_FORCEIDLE); 1383 UART_ERRATA_i291_DMA_FORCEIDLE);
1384 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
1376 break; 1385 break;
1377 case OMAP_UART_REV_63: 1386 case OMAP_UART_REV_63:
1378 up->errata |= UART_ERRATA_i202_MDR1_ACCESS; 1387 up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
1388 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
1379 break; 1389 break;
1380 default: 1390 default:
1381 break; 1391 break;
@@ -1399,11 +1409,13 @@ static int serial_omap_probe(struct platform_device *pdev)
1399{ 1409{
1400 struct uart_omap_port *up; 1410 struct uart_omap_port *up;
1401 struct resource *mem, *irq; 1411 struct resource *mem, *irq;
1402 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data; 1412 struct omap_uart_port_info *omap_up_info = dev_get_platdata(&pdev->dev);
1403 int ret; 1413 int ret;
1404 1414
1405 if (pdev->dev.of_node) 1415 if (pdev->dev.of_node) {
1406 omap_up_info = of_get_uart_port_info(&pdev->dev); 1416 omap_up_info = of_get_uart_port_info(&pdev->dev);
1417 pdev->dev.platform_data = omap_up_info;
1418 }
1407 1419
1408 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1420 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1409 if (!mem) { 1421 if (!mem) {
@@ -1468,13 +1480,6 @@ static int serial_omap_probe(struct platform_device *pdev)
1468 goto err_port_line; 1480 goto err_port_line;
1469 } 1481 }
1470 1482
1471 up->pins = devm_pinctrl_get_select_default(&pdev->dev);
1472 if (IS_ERR(up->pins)) {
1473 dev_warn(&pdev->dev, "did not get pins for uart%i error: %li\n",
1474 up->port.line, PTR_ERR(up->pins));
1475 up->pins = NULL;
1476 }
1477
1478 sprintf(up->name, "OMAP UART%d", up->port.line); 1483 sprintf(up->name, "OMAP UART%d", up->port.line);
1479 up->port.mapbase = mem->start; 1484 up->port.mapbase = mem->start;
1480 up->port.membase = devm_ioremap(&pdev->dev, mem->start, 1485 up->port.membase = devm_ioremap(&pdev->dev, mem->start,
@@ -1501,7 +1506,6 @@ static int serial_omap_probe(struct platform_device *pdev)
1501 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work); 1506 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
1502 1507
1503 platform_set_drvdata(pdev, up); 1508 platform_set_drvdata(pdev, up);
1504 pm_runtime_enable(&pdev->dev);
1505 if (omap_up_info->autosuspend_timeout == 0) 1509 if (omap_up_info->autosuspend_timeout == 0)
1506 omap_up_info->autosuspend_timeout = -1; 1510 omap_up_info->autosuspend_timeout = -1;
1507 device_init_wakeup(up->dev, true); 1511 device_init_wakeup(up->dev, true);
@@ -1510,6 +1514,8 @@ static int serial_omap_probe(struct platform_device *pdev)
1510 omap_up_info->autosuspend_timeout); 1514 omap_up_info->autosuspend_timeout);
1511 1515
1512 pm_runtime_irq_safe(&pdev->dev); 1516 pm_runtime_irq_safe(&pdev->dev);
1517 pm_runtime_enable(&pdev->dev);
1518
1513 pm_runtime_get_sync(&pdev->dev); 1519 pm_runtime_get_sync(&pdev->dev);
1514 1520
1515 omap_serial_fill_features_erratas(up); 1521 omap_serial_fill_features_erratas(up);
@@ -1609,6 +1615,7 @@ static void serial_omap_restore_context(struct uart_omap_port *up)
1609 serial_omap_mdr1_errataset(up, up->mdr1); 1615 serial_omap_mdr1_errataset(up, up->mdr1);
1610 else 1616 else
1611 serial_out(up, UART_OMAP_MDR1, up->mdr1); 1617 serial_out(up, UART_OMAP_MDR1, up->mdr1);
1618 serial_out(up, UART_OMAP_WER, up->wer);
1612} 1619}
1613 1620
1614static int serial_omap_runtime_suspend(struct device *dev) 1621static int serial_omap_runtime_suspend(struct device *dev)
diff --git a/drivers/tty/serial/pch_uart.c b/drivers/tty/serial/pch_uart.c
index 572d48189de9..5040c517f3b8 100644
--- a/drivers/tty/serial/pch_uart.c
+++ b/drivers/tty/serial/pch_uart.c
@@ -232,7 +232,7 @@ struct eg20t_port {
232 unsigned int iobase; 232 unsigned int iobase;
233 struct pci_dev *pdev; 233 struct pci_dev *pdev;
234 int fifo_size; 234 int fifo_size;
235 int uartclk; 235 unsigned int uartclk;
236 int start_tx; 236 int start_tx;
237 int start_rx; 237 int start_rx;
238 int tx_empty; 238 int tx_empty;
@@ -373,35 +373,62 @@ static const struct file_operations port_regs_ops = {
373}; 373};
374#endif /* CONFIG_DEBUG_FS */ 374#endif /* CONFIG_DEBUG_FS */
375 375
376static struct dmi_system_id pch_uart_dmi_table[] = {
377 {
378 .ident = "CM-iTC",
379 {
380 DMI_MATCH(DMI_BOARD_NAME, "CM-iTC"),
381 },
382 (void *)CMITC_UARTCLK,
383 },
384 {
385 .ident = "FRI2",
386 {
387 DMI_MATCH(DMI_BIOS_VERSION, "FRI2"),
388 },
389 (void *)FRI2_64_UARTCLK,
390 },
391 {
392 .ident = "Fish River Island II",
393 {
394 DMI_MATCH(DMI_PRODUCT_NAME, "Fish River Island II"),
395 },
396 (void *)FRI2_48_UARTCLK,
397 },
398 {
399 .ident = "COMe-mTT",
400 {
401 DMI_MATCH(DMI_BOARD_NAME, "COMe-mTT"),
402 },
403 (void *)NTC1_UARTCLK,
404 },
405 {
406 .ident = "nanoETXexpress-TT",
407 {
408 DMI_MATCH(DMI_BOARD_NAME, "nanoETXexpress-TT"),
409 },
410 (void *)NTC1_UARTCLK,
411 },
412 {
413 .ident = "MinnowBoard",
414 {
415 DMI_MATCH(DMI_BOARD_NAME, "MinnowBoard"),
416 },
417 (void *)MINNOW_UARTCLK,
418 },
419};
420
376/* Return UART clock, checking for board specific clocks. */ 421/* Return UART clock, checking for board specific clocks. */
377static int pch_uart_get_uartclk(void) 422static unsigned int pch_uart_get_uartclk(void)
378{ 423{
379 const char *cmp; 424 const struct dmi_system_id *d;
380 425
381 if (user_uartclk) 426 if (user_uartclk)
382 return user_uartclk; 427 return user_uartclk;
383 428
384 cmp = dmi_get_system_info(DMI_BOARD_NAME); 429 d = dmi_first_match(pch_uart_dmi_table);
385 if (cmp && strstr(cmp, "CM-iTC")) 430 if (d)
386 return CMITC_UARTCLK; 431 return (unsigned long)d->driver_data;
387
388 cmp = dmi_get_system_info(DMI_BIOS_VERSION);
389 if (cmp && strnstr(cmp, "FRI2", 4))
390 return FRI2_64_UARTCLK;
391
392 cmp = dmi_get_system_info(DMI_PRODUCT_NAME);
393 if (cmp && strstr(cmp, "Fish River Island II"))
394 return FRI2_48_UARTCLK;
395
396 /* Kontron COMe-mTT10 (nanoETXexpress-TT) */
397 cmp = dmi_get_system_info(DMI_BOARD_NAME);
398 if (cmp && (strstr(cmp, "COMe-mTT") ||
399 strstr(cmp, "nanoETXexpress-TT")))
400 return NTC1_UARTCLK;
401
402 cmp = dmi_get_system_info(DMI_BOARD_NAME);
403 if (cmp && strstr(cmp, "MinnowBoard"))
404 return MINNOW_UARTCLK;
405 432
406 return DEFAULT_UARTCLK; 433 return DEFAULT_UARTCLK;
407} 434}
@@ -422,7 +449,7 @@ static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
422 iowrite8(ier, priv->membase + UART_IER); 449 iowrite8(ier, priv->membase + UART_IER);
423} 450}
424 451
425static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud, 452static int pch_uart_hal_set_line(struct eg20t_port *priv, unsigned int baud,
426 unsigned int parity, unsigned int bits, 453 unsigned int parity, unsigned int bits,
427 unsigned int stb) 454 unsigned int stb)
428{ 455{
@@ -457,7 +484,7 @@ static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
457 lcr |= bits; 484 lcr |= bits;
458 lcr |= stb; 485 lcr |= stb;
459 486
460 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n", 487 dev_dbg(priv->port.dev, "%s:baud = %u, div = %04x, lcr = %02x (%lu)\n",
461 __func__, baud, div, lcr, jiffies); 488 __func__, baud, div, lcr, jiffies);
462 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR); 489 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
463 iowrite8(dll, priv->membase + PCH_UART_DLL); 490 iowrite8(dll, priv->membase + PCH_UART_DLL);
@@ -1363,9 +1390,8 @@ static void pch_uart_shutdown(struct uart_port *port)
1363static void pch_uart_set_termios(struct uart_port *port, 1390static void pch_uart_set_termios(struct uart_port *port,
1364 struct ktermios *termios, struct ktermios *old) 1391 struct ktermios *termios, struct ktermios *old)
1365{ 1392{
1366 int baud;
1367 int rtn; 1393 int rtn;
1368 unsigned int parity, bits, stb; 1394 unsigned int baud, parity, bits, stb;
1369 struct eg20t_port *priv; 1395 struct eg20t_port *priv;
1370 unsigned long flags; 1396 unsigned long flags;
1371 1397
diff --git a/drivers/tty/serial/pmac_zilog.c b/drivers/tty/serial/pmac_zilog.c
index b1785f58b6e3..f87f1a0c8c6e 100644
--- a/drivers/tty/serial/pmac_zilog.c
+++ b/drivers/tty/serial/pmac_zilog.c
@@ -1798,7 +1798,6 @@ static int __exit pmz_detach(struct platform_device *pdev)
1798 1798
1799 uart_remove_one_port(&pmz_uart_reg, &uap->port); 1799 uart_remove_one_port(&pmz_uart_reg, &uap->port);
1800 1800
1801 platform_set_drvdata(pdev, NULL);
1802 uap->port.dev = NULL; 1801 uap->port.dev = NULL;
1803 1802
1804 return 0; 1803 return 0;
diff --git a/drivers/tty/serial/pnx8xxx_uart.c b/drivers/tty/serial/pnx8xxx_uart.c
index 7e277a5384a7..b6b7aca5707a 100644
--- a/drivers/tty/serial/pnx8xxx_uart.c
+++ b/drivers/tty/serial/pnx8xxx_uart.c
@@ -801,8 +801,6 @@ static int pnx8xxx_serial_remove(struct platform_device *pdev)
801{ 801{
802 struct pnx8xxx_port *sport = platform_get_drvdata(pdev); 802 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
803 803
804 platform_set_drvdata(pdev, NULL);
805
806 if (sport) 804 if (sport)
807 uart_remove_one_port(&pnx8xxx_reg, &sport->port); 805 uart_remove_one_port(&pnx8xxx_reg, &sport->port);
808 806
diff --git a/drivers/tty/serial/pxa.c b/drivers/tty/serial/pxa.c
index 05f504e0c271..ac8b2f5b2396 100644
--- a/drivers/tty/serial/pxa.c
+++ b/drivers/tty/serial/pxa.c
@@ -945,8 +945,6 @@ static int serial_pxa_remove(struct platform_device *dev)
945{ 945{
946 struct uart_pxa_port *sport = platform_get_drvdata(dev); 946 struct uart_pxa_port *sport = platform_get_drvdata(dev);
947 947
948 platform_set_drvdata(dev, NULL);
949
950 uart_remove_one_port(&serial_pxa_reg, &sport->port); 948 uart_remove_one_port(&serial_pxa_reg, &sport->port);
951 949
952 clk_unprepare(sport->clk); 950 clk_unprepare(sport->clk);
diff --git a/drivers/tty/serial/sa1100.c b/drivers/tty/serial/sa1100.c
index af6b3e3ad24d..fc23ea19073a 100644
--- a/drivers/tty/serial/sa1100.c
+++ b/drivers/tty/serial/sa1100.c
@@ -864,8 +864,6 @@ static int sa1100_serial_remove(struct platform_device *pdev)
864{ 864{
865 struct sa1100_port *sport = platform_get_drvdata(pdev); 865 struct sa1100_port *sport = platform_get_drvdata(pdev);
866 866
867 platform_set_drvdata(pdev, NULL);
868
869 if (sport) 867 if (sport)
870 uart_remove_one_port(&sa1100_reg, &sport->port); 868 uart_remove_one_port(&sa1100_reg, &sport->port);
871 869
diff --git a/drivers/tty/serial/samsung.c b/drivers/tty/serial/samsung.c
index 376079b9bd75..019b9154d7d4 100644
--- a/drivers/tty/serial/samsung.c
+++ b/drivers/tty/serial/samsung.c
@@ -1250,8 +1250,8 @@ static int s3c24xx_serial_probe(struct platform_device *pdev)
1250 1250
1251 ourport->baudclk = ERR_PTR(-EINVAL); 1251 ourport->baudclk = ERR_PTR(-EINVAL);
1252 ourport->info = ourport->drv_data->info; 1252 ourport->info = ourport->drv_data->info;
1253 ourport->cfg = (pdev->dev.platform_data) ? 1253 ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
1254 (struct s3c2410_uartcfg *)pdev->dev.platform_data : 1254 (struct s3c2410_uartcfg *)dev_get_platdata(&pdev->dev) :
1255 ourport->drv_data->def_cfg; 1255 ourport->drv_data->def_cfg;
1256 1256
1257 ourport->port.fifosize = (ourport->info->fifosize) ? 1257 ourport->port.fifosize = (ourport->info->fifosize) ?
diff --git a/drivers/tty/serial/sc26xx.c b/drivers/tty/serial/sc26xx.c
index 4b1434d53e9d..887b4f770749 100644
--- a/drivers/tty/serial/sc26xx.c
+++ b/drivers/tty/serial/sc26xx.c
@@ -637,7 +637,7 @@ static int sc26xx_probe(struct platform_device *dev)
637{ 637{
638 struct resource *res; 638 struct resource *res;
639 struct uart_sc26xx_port *up; 639 struct uart_sc26xx_port *up;
640 unsigned int *sc26xx_data = dev->dev.platform_data; 640 unsigned int *sc26xx_data = dev_get_platdata(&dev->dev);
641 int err; 641 int err;
642 642
643 res = platform_get_resource(dev, IORESOURCE_MEM, 0); 643 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
diff --git a/drivers/tty/serial/sccnxp.c b/drivers/tty/serial/sccnxp.c
index c77304155410..49e9bbfe6cab 100644
--- a/drivers/tty/serial/sccnxp.c
+++ b/drivers/tty/serial/sccnxp.c
@@ -15,6 +15,7 @@
15#define SUPPORT_SYSRQ 15#define SUPPORT_SYSRQ
16#endif 16#endif
17 17
18#include <linux/clk.h>
18#include <linux/err.h> 19#include <linux/err.h>
19#include <linux/module.h> 20#include <linux/module.h>
20#include <linux/device.h> 21#include <linux/device.h>
@@ -94,16 +95,17 @@
94#define MCTRL_IBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_IP0) 95#define MCTRL_IBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_IP0)
95#define MCTRL_OBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_OP0) 96#define MCTRL_OBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_OP0)
96 97
97/* Supported chip types */ 98#define SCCNXP_HAVE_IO 0x00000001
98enum { 99#define SCCNXP_HAVE_MR0 0x00000002
99 SCCNXP_TYPE_SC2681 = 2681, 100
100 SCCNXP_TYPE_SC2691 = 2691, 101struct sccnxp_chip {
101 SCCNXP_TYPE_SC2692 = 2692, 102 const char *name;
102 SCCNXP_TYPE_SC2891 = 2891, 103 unsigned int nr;
103 SCCNXP_TYPE_SC2892 = 2892, 104 unsigned long freq_min;
104 SCCNXP_TYPE_SC28202 = 28202, 105 unsigned long freq_std;
105 SCCNXP_TYPE_SC68681 = 68681, 106 unsigned long freq_max;
106 SCCNXP_TYPE_SC68692 = 68692, 107 unsigned int flags;
108 unsigned int fifosize;
107}; 109};
108 110
109struct sccnxp_port { 111struct sccnxp_port {
@@ -111,16 +113,10 @@ struct sccnxp_port {
111 struct uart_port port[SCCNXP_MAX_UARTS]; 113 struct uart_port port[SCCNXP_MAX_UARTS];
112 bool opened[SCCNXP_MAX_UARTS]; 114 bool opened[SCCNXP_MAX_UARTS];
113 115
114 const char *name;
115 int irq; 116 int irq;
116
117 u8 imr; 117 u8 imr;
118 u8 addr_mask;
119 int freq_std;
120 118
121 int flags; 119 struct sccnxp_chip *chip;
122#define SCCNXP_HAVE_IO 0x00000001
123#define SCCNXP_HAVE_MR0 0x00000002
124 120
125#ifdef CONFIG_SERIAL_SCCNXP_CONSOLE 121#ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
126 struct console console; 122 struct console console;
@@ -136,29 +132,94 @@ struct sccnxp_port {
136 struct regulator *regulator; 132 struct regulator *regulator;
137}; 133};
138 134
139static inline u8 sccnxp_raw_read(void __iomem *base, u8 reg, u8 shift) 135static const struct sccnxp_chip sc2681 = {
140{ 136 .name = "SC2681",
141 return readb(base + (reg << shift)); 137 .nr = 2,
142} 138 .freq_min = 1000000,
139 .freq_std = 3686400,
140 .freq_max = 4000000,
141 .flags = SCCNXP_HAVE_IO,
142 .fifosize = 3,
143};
143 144
144static inline void sccnxp_raw_write(void __iomem *base, u8 reg, u8 shift, u8 v) 145static const struct sccnxp_chip sc2691 = {
145{ 146 .name = "SC2691",
146 writeb(v, base + (reg << shift)); 147 .nr = 1,
147} 148 .freq_min = 1000000,
149 .freq_std = 3686400,
150 .freq_max = 4000000,
151 .flags = 0,
152 .fifosize = 3,
153};
154
155static const struct sccnxp_chip sc2692 = {
156 .name = "SC2692",
157 .nr = 2,
158 .freq_min = 1000000,
159 .freq_std = 3686400,
160 .freq_max = 4000000,
161 .flags = SCCNXP_HAVE_IO,
162 .fifosize = 3,
163};
164
165static const struct sccnxp_chip sc2891 = {
166 .name = "SC2891",
167 .nr = 1,
168 .freq_min = 100000,
169 .freq_std = 3686400,
170 .freq_max = 8000000,
171 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
172 .fifosize = 16,
173};
174
175static const struct sccnxp_chip sc2892 = {
176 .name = "SC2892",
177 .nr = 2,
178 .freq_min = 100000,
179 .freq_std = 3686400,
180 .freq_max = 8000000,
181 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
182 .fifosize = 16,
183};
184
185static const struct sccnxp_chip sc28202 = {
186 .name = "SC28202",
187 .nr = 2,
188 .freq_min = 1000000,
189 .freq_std = 14745600,
190 .freq_max = 50000000,
191 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
192 .fifosize = 256,
193};
194
195static const struct sccnxp_chip sc68681 = {
196 .name = "SC68681",
197 .nr = 2,
198 .freq_min = 1000000,
199 .freq_std = 3686400,
200 .freq_max = 4000000,
201 .flags = SCCNXP_HAVE_IO,
202 .fifosize = 3,
203};
204
205static const struct sccnxp_chip sc68692 = {
206 .name = "SC68692",
207 .nr = 2,
208 .freq_min = 1000000,
209 .freq_std = 3686400,
210 .freq_max = 4000000,
211 .flags = SCCNXP_HAVE_IO,
212 .fifosize = 3,
213};
148 214
149static inline u8 sccnxp_read(struct uart_port *port, u8 reg) 215static inline u8 sccnxp_read(struct uart_port *port, u8 reg)
150{ 216{
151 struct sccnxp_port *s = dev_get_drvdata(port->dev); 217 return readb(port->membase + (reg << port->regshift));
152
153 return sccnxp_raw_read(port->membase, reg & s->addr_mask,
154 port->regshift);
155} 218}
156 219
157static inline void sccnxp_write(struct uart_port *port, u8 reg, u8 v) 220static inline void sccnxp_write(struct uart_port *port, u8 reg, u8 v)
158{ 221{
159 struct sccnxp_port *s = dev_get_drvdata(port->dev); 222 writeb(v, port->membase + (reg << port->regshift));
160
161 sccnxp_raw_write(port->membase, reg & s->addr_mask, port->regshift, v);
162} 223}
163 224
164static inline u8 sccnxp_port_read(struct uart_port *port, u8 reg) 225static inline u8 sccnxp_port_read(struct uart_port *port, u8 reg)
@@ -224,13 +285,14 @@ static int sccnxp_set_baud(struct uart_port *port, int baud)
224{ 285{
225 struct sccnxp_port *s = dev_get_drvdata(port->dev); 286 struct sccnxp_port *s = dev_get_drvdata(port->dev);
226 int div_std, tmp_baud, bestbaud = baud, besterr = -1; 287 int div_std, tmp_baud, bestbaud = baud, besterr = -1;
288 struct sccnxp_chip *chip = s->chip;
227 u8 i, acr = 0, csr = 0, mr0 = 0; 289 u8 i, acr = 0, csr = 0, mr0 = 0;
228 290
229 /* Find best baud from table */ 291 /* Find best baud from table */
230 for (i = 0; baud_std[i].baud && besterr; i++) { 292 for (i = 0; baud_std[i].baud && besterr; i++) {
231 if (baud_std[i].mr0 && !(s->flags & SCCNXP_HAVE_MR0)) 293 if (baud_std[i].mr0 && !(chip->flags & SCCNXP_HAVE_MR0))
232 continue; 294 continue;
233 div_std = DIV_ROUND_CLOSEST(s->freq_std, baud_std[i].baud); 295 div_std = DIV_ROUND_CLOSEST(chip->freq_std, baud_std[i].baud);
234 tmp_baud = DIV_ROUND_CLOSEST(port->uartclk, div_std); 296 tmp_baud = DIV_ROUND_CLOSEST(port->uartclk, div_std);
235 if (!sccnxp_update_best_err(baud, tmp_baud, &besterr)) { 297 if (!sccnxp_update_best_err(baud, tmp_baud, &besterr)) {
236 acr = baud_std[i].acr; 298 acr = baud_std[i].acr;
@@ -240,7 +302,7 @@ static int sccnxp_set_baud(struct uart_port *port, int baud)
240 } 302 }
241 } 303 }
242 304
243 if (s->flags & SCCNXP_HAVE_MR0) { 305 if (chip->flags & SCCNXP_HAVE_MR0) {
244 /* Enable FIFO, set half level for TX */ 306 /* Enable FIFO, set half level for TX */
245 mr0 |= MR0_FIFO | MR0_TXLVL; 307 mr0 |= MR0_FIFO | MR0_TXLVL;
246 /* Update MR0 */ 308 /* Update MR0 */
@@ -363,7 +425,7 @@ static void sccnxp_handle_tx(struct uart_port *port)
363 sccnxp_disable_irq(port, IMR_TXRDY); 425 sccnxp_disable_irq(port, IMR_TXRDY);
364 426
365 /* Set direction to input */ 427 /* Set direction to input */
366 if (s->flags & SCCNXP_HAVE_IO) 428 if (s->chip->flags & SCCNXP_HAVE_IO)
367 sccnxp_set_bit(port, DIR_OP, 0); 429 sccnxp_set_bit(port, DIR_OP, 0);
368 } 430 }
369 return; 431 return;
@@ -437,7 +499,7 @@ static void sccnxp_start_tx(struct uart_port *port)
437 spin_lock_irqsave(&s->lock, flags); 499 spin_lock_irqsave(&s->lock, flags);
438 500
439 /* Set direction to output */ 501 /* Set direction to output */
440 if (s->flags & SCCNXP_HAVE_IO) 502 if (s->chip->flags & SCCNXP_HAVE_IO)
441 sccnxp_set_bit(port, DIR_OP, 1); 503 sccnxp_set_bit(port, DIR_OP, 1);
442 504
443 sccnxp_enable_irq(port, IMR_TXRDY); 505 sccnxp_enable_irq(port, IMR_TXRDY);
@@ -483,7 +545,7 @@ static void sccnxp_set_mctrl(struct uart_port *port, unsigned int mctrl)
483 struct sccnxp_port *s = dev_get_drvdata(port->dev); 545 struct sccnxp_port *s = dev_get_drvdata(port->dev);
484 unsigned long flags; 546 unsigned long flags;
485 547
486 if (!(s->flags & SCCNXP_HAVE_IO)) 548 if (!(s->chip->flags & SCCNXP_HAVE_IO))
487 return; 549 return;
488 550
489 spin_lock_irqsave(&s->lock, flags); 551 spin_lock_irqsave(&s->lock, flags);
@@ -501,7 +563,7 @@ static unsigned int sccnxp_get_mctrl(struct uart_port *port)
501 struct sccnxp_port *s = dev_get_drvdata(port->dev); 563 struct sccnxp_port *s = dev_get_drvdata(port->dev);
502 unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR; 564 unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
503 565
504 if (!(s->flags & SCCNXP_HAVE_IO)) 566 if (!(s->chip->flags & SCCNXP_HAVE_IO))
505 return mctrl; 567 return mctrl;
506 568
507 spin_lock_irqsave(&s->lock, flags); 569 spin_lock_irqsave(&s->lock, flags);
@@ -617,7 +679,7 @@ static void sccnxp_set_termios(struct uart_port *port,
617 679
618 /* Setup baudrate */ 680 /* Setup baudrate */
619 baud = uart_get_baud_rate(port, termios, old, 50, 681 baud = uart_get_baud_rate(port, termios, old, 50,
620 (s->flags & SCCNXP_HAVE_MR0) ? 682 (s->chip->flags & SCCNXP_HAVE_MR0) ?
621 230400 : 38400); 683 230400 : 38400);
622 baud = sccnxp_set_baud(port, baud); 684 baud = sccnxp_set_baud(port, baud);
623 685
@@ -641,7 +703,7 @@ static int sccnxp_startup(struct uart_port *port)
641 703
642 spin_lock_irqsave(&s->lock, flags); 704 spin_lock_irqsave(&s->lock, flags);
643 705
644 if (s->flags & SCCNXP_HAVE_IO) { 706 if (s->chip->flags & SCCNXP_HAVE_IO) {
645 /* Outputs are controlled manually */ 707 /* Outputs are controlled manually */
646 sccnxp_write(port, SCCNXP_OPCR_REG, 0); 708 sccnxp_write(port, SCCNXP_OPCR_REG, 0);
647 } 709 }
@@ -681,7 +743,7 @@ static void sccnxp_shutdown(struct uart_port *port)
681 sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE | CR_TX_DISABLE); 743 sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE | CR_TX_DISABLE);
682 744
683 /* Leave direction to input */ 745 /* Leave direction to input */
684 if (s->flags & SCCNXP_HAVE_IO) 746 if (s->chip->flags & SCCNXP_HAVE_IO)
685 sccnxp_set_bit(port, DIR_OP, 0); 747 sccnxp_set_bit(port, DIR_OP, 0);
686 748
687 spin_unlock_irqrestore(&s->lock, flags); 749 spin_unlock_irqrestore(&s->lock, flags);
@@ -691,7 +753,7 @@ static const char *sccnxp_type(struct uart_port *port)
691{ 753{
692 struct sccnxp_port *s = dev_get_drvdata(port->dev); 754 struct sccnxp_port *s = dev_get_drvdata(port->dev);
693 755
694 return (port->type == PORT_SC26XX) ? s->name : NULL; 756 return (port->type == PORT_SC26XX) ? s->chip->name : NULL;
695} 757}
696 758
697static void sccnxp_release_port(struct uart_port *port) 759static void sccnxp_release_port(struct uart_port *port)
@@ -778,19 +840,31 @@ static int sccnxp_console_setup(struct console *co, char *options)
778} 840}
779#endif 841#endif
780 842
843static const struct platform_device_id sccnxp_id_table[] = {
844 { .name = "sc2681", .driver_data = (kernel_ulong_t)&sc2681, },
845 { .name = "sc2691", .driver_data = (kernel_ulong_t)&sc2691, },
846 { .name = "sc2692", .driver_data = (kernel_ulong_t)&sc2692, },
847 { .name = "sc2891", .driver_data = (kernel_ulong_t)&sc2891, },
848 { .name = "sc2892", .driver_data = (kernel_ulong_t)&sc2892, },
849 { .name = "sc28202", .driver_data = (kernel_ulong_t)&sc28202, },
850 { .name = "sc68681", .driver_data = (kernel_ulong_t)&sc68681, },
851 { .name = "sc68692", .driver_data = (kernel_ulong_t)&sc68692, },
852 { }
853};
854MODULE_DEVICE_TABLE(platform, sccnxp_id_table);
855
781static int sccnxp_probe(struct platform_device *pdev) 856static int sccnxp_probe(struct platform_device *pdev)
782{ 857{
783 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 858 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
784 int chiptype = pdev->id_entry->driver_data;
785 struct sccnxp_pdata *pdata = dev_get_platdata(&pdev->dev); 859 struct sccnxp_pdata *pdata = dev_get_platdata(&pdev->dev);
786 int i, ret, fifosize, freq_min, freq_max; 860 int i, ret, uartclk;
787 struct sccnxp_port *s; 861 struct sccnxp_port *s;
788 void __iomem *membase; 862 void __iomem *membase;
863 struct clk *clk;
789 864
790 if (!res) { 865 membase = devm_ioremap_resource(&pdev->dev, res);
791 dev_err(&pdev->dev, "Missing memory resource data\n"); 866 if (IS_ERR(membase))
792 return -EADDRNOTAVAIL; 867 return PTR_ERR(membase);
793 }
794 868
795 s = devm_kzalloc(&pdev->dev, sizeof(struct sccnxp_port), GFP_KERNEL); 869 s = devm_kzalloc(&pdev->dev, sizeof(struct sccnxp_port), GFP_KERNEL);
796 if (!s) { 870 if (!s) {
@@ -801,99 +875,38 @@ static int sccnxp_probe(struct platform_device *pdev)
801 875
802 spin_lock_init(&s->lock); 876 spin_lock_init(&s->lock);
803 877
804 /* Individual chip settings */ 878 s->chip = (struct sccnxp_chip *)pdev->id_entry->driver_data;
805 switch (chiptype) { 879
806 case SCCNXP_TYPE_SC2681: 880 s->regulator = devm_regulator_get(&pdev->dev, "vcc");
807 s->name = "SC2681"; 881 if (!IS_ERR(s->regulator)) {
808 s->uart.nr = 2; 882 ret = regulator_enable(s->regulator);
809 s->freq_std = 3686400; 883 if (ret) {
810 s->addr_mask = 0x0f; 884 dev_err(&pdev->dev,
811 s->flags = SCCNXP_HAVE_IO; 885 "Failed to enable regulator: %i\n", ret);
812 fifosize = 3; 886 return ret;
813 freq_min = 1000000; 887 }
814 freq_max = 4000000; 888 } else if (PTR_ERR(s->regulator) == -EPROBE_DEFER)
815 break; 889 return -EPROBE_DEFER;
816 case SCCNXP_TYPE_SC2691: 890
817 s->name = "SC2691"; 891 clk = devm_clk_get(&pdev->dev, NULL);
818 s->uart.nr = 1; 892 if (IS_ERR(clk)) {
819 s->freq_std = 3686400; 893 if (PTR_ERR(clk) == -EPROBE_DEFER) {
820 s->addr_mask = 0x07; 894 ret = -EPROBE_DEFER;
821 s->flags = 0; 895 goto err_out;
822 fifosize = 3; 896 }
823 freq_min = 1000000; 897 dev_notice(&pdev->dev, "Using default clock frequency\n");
824 freq_max = 4000000; 898 uartclk = s->chip->freq_std;
825 break; 899 } else
826 case SCCNXP_TYPE_SC2692: 900 uartclk = clk_get_rate(clk);
827 s->name = "SC2692"; 901
828 s->uart.nr = 2; 902 /* Check input frequency */
829 s->freq_std = 3686400; 903 if ((uartclk < s->chip->freq_min) || (uartclk > s->chip->freq_max)) {
830 s->addr_mask = 0x0f; 904 dev_err(&pdev->dev, "Frequency out of bounds\n");
831 s->flags = SCCNXP_HAVE_IO; 905 ret = -EINVAL;
832 fifosize = 3;
833 freq_min = 1000000;
834 freq_max = 4000000;
835 break;
836 case SCCNXP_TYPE_SC2891:
837 s->name = "SC2891";
838 s->uart.nr = 1;
839 s->freq_std = 3686400;
840 s->addr_mask = 0x0f;
841 s->flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0;
842 fifosize = 16;
843 freq_min = 100000;
844 freq_max = 8000000;
845 break;
846 case SCCNXP_TYPE_SC2892:
847 s->name = "SC2892";
848 s->uart.nr = 2;
849 s->freq_std = 3686400;
850 s->addr_mask = 0x0f;
851 s->flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0;
852 fifosize = 16;
853 freq_min = 100000;
854 freq_max = 8000000;
855 break;
856 case SCCNXP_TYPE_SC28202:
857 s->name = "SC28202";
858 s->uart.nr = 2;
859 s->freq_std = 14745600;
860 s->addr_mask = 0x7f;
861 s->flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0;
862 fifosize = 256;
863 freq_min = 1000000;
864 freq_max = 50000000;
865 break;
866 case SCCNXP_TYPE_SC68681:
867 s->name = "SC68681";
868 s->uart.nr = 2;
869 s->freq_std = 3686400;
870 s->addr_mask = 0x0f;
871 s->flags = SCCNXP_HAVE_IO;
872 fifosize = 3;
873 freq_min = 1000000;
874 freq_max = 4000000;
875 break;
876 case SCCNXP_TYPE_SC68692:
877 s->name = "SC68692";
878 s->uart.nr = 2;
879 s->freq_std = 3686400;
880 s->addr_mask = 0x0f;
881 s->flags = SCCNXP_HAVE_IO;
882 fifosize = 3;
883 freq_min = 1000000;
884 freq_max = 4000000;
885 break;
886 default:
887 dev_err(&pdev->dev, "Unsupported chip type %i\n", chiptype);
888 ret = -ENOTSUPP;
889 goto err_out; 906 goto err_out;
890 } 907 }
891 908
892 if (!pdata) { 909 if (pdata)
893 dev_warn(&pdev->dev,
894 "No platform data supplied, using defaults\n");
895 s->pdata.frequency = s->freq_std;
896 } else
897 memcpy(&s->pdata, pdata, sizeof(struct sccnxp_pdata)); 910 memcpy(&s->pdata, pdata, sizeof(struct sccnxp_pdata));
898 911
899 if (s->pdata.poll_time_us) { 912 if (s->pdata.poll_time_us) {
@@ -911,34 +924,11 @@ static int sccnxp_probe(struct platform_device *pdev)
911 } 924 }
912 } 925 }
913 926
914 /* Check input frequency */
915 if ((s->pdata.frequency < freq_min) ||
916 (s->pdata.frequency > freq_max)) {
917 dev_err(&pdev->dev, "Frequency out of bounds\n");
918 ret = -EINVAL;
919 goto err_out;
920 }
921
922 s->regulator = devm_regulator_get(&pdev->dev, "VCC");
923 if (!IS_ERR(s->regulator)) {
924 ret = regulator_enable(s->regulator);
925 if (ret) {
926 dev_err(&pdev->dev,
927 "Failed to enable regulator: %i\n", ret);
928 return ret;
929 }
930 }
931
932 membase = devm_ioremap_resource(&pdev->dev, res);
933 if (IS_ERR(membase)) {
934 ret = PTR_ERR(membase);
935 goto err_out;
936 }
937
938 s->uart.owner = THIS_MODULE; 927 s->uart.owner = THIS_MODULE;
939 s->uart.dev_name = "ttySC"; 928 s->uart.dev_name = "ttySC";
940 s->uart.major = SCCNXP_MAJOR; 929 s->uart.major = SCCNXP_MAJOR;
941 s->uart.minor = SCCNXP_MINOR; 930 s->uart.minor = SCCNXP_MINOR;
931 s->uart.nr = s->chip->nr;
942#ifdef CONFIG_SERIAL_SCCNXP_CONSOLE 932#ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
943 s->uart.cons = &s->console; 933 s->uart.cons = &s->console;
944 s->uart.cons->device = uart_console_device; 934 s->uart.cons->device = uart_console_device;
@@ -960,17 +950,17 @@ static int sccnxp_probe(struct platform_device *pdev)
960 s->port[i].dev = &pdev->dev; 950 s->port[i].dev = &pdev->dev;
961 s->port[i].irq = s->irq; 951 s->port[i].irq = s->irq;
962 s->port[i].type = PORT_SC26XX; 952 s->port[i].type = PORT_SC26XX;
963 s->port[i].fifosize = fifosize; 953 s->port[i].fifosize = s->chip->fifosize;
964 s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE; 954 s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
965 s->port[i].iotype = UPIO_MEM; 955 s->port[i].iotype = UPIO_MEM;
966 s->port[i].mapbase = res->start; 956 s->port[i].mapbase = res->start;
967 s->port[i].membase = membase; 957 s->port[i].membase = membase;
968 s->port[i].regshift = s->pdata.reg_shift; 958 s->port[i].regshift = s->pdata.reg_shift;
969 s->port[i].uartclk = s->pdata.frequency; 959 s->port[i].uartclk = uartclk;
970 s->port[i].ops = &sccnxp_ops; 960 s->port[i].ops = &sccnxp_ops;
971 uart_add_one_port(&s->uart, &s->port[i]); 961 uart_add_one_port(&s->uart, &s->port[i]);
972 /* Set direction to input */ 962 /* Set direction to input */
973 if (s->flags & SCCNXP_HAVE_IO) 963 if (s->chip->flags & SCCNXP_HAVE_IO)
974 sccnxp_set_bit(&s->port[i], DIR_OP, 0); 964 sccnxp_set_bit(&s->port[i], DIR_OP, 0);
975 } 965 }
976 966
@@ -997,7 +987,8 @@ static int sccnxp_probe(struct platform_device *pdev)
997 } 987 }
998 988
999err_out: 989err_out:
1000 platform_set_drvdata(pdev, NULL); 990 if (!IS_ERR(s->regulator))
991 return regulator_disable(s->regulator);
1001 992
1002 return ret; 993 return ret;
1003} 994}
@@ -1016,7 +1007,6 @@ static int sccnxp_remove(struct platform_device *pdev)
1016 uart_remove_one_port(&s->uart, &s->port[i]); 1007 uart_remove_one_port(&s->uart, &s->port[i]);
1017 1008
1018 uart_unregister_driver(&s->uart); 1009 uart_unregister_driver(&s->uart);
1019 platform_set_drvdata(pdev, NULL);
1020 1010
1021 if (!IS_ERR(s->regulator)) 1011 if (!IS_ERR(s->regulator))
1022 return regulator_disable(s->regulator); 1012 return regulator_disable(s->regulator);
@@ -1024,19 +1014,6 @@ static int sccnxp_remove(struct platform_device *pdev)
1024 return 0; 1014 return 0;
1025} 1015}
1026 1016
1027static const struct platform_device_id sccnxp_id_table[] = {
1028 { "sc2681", SCCNXP_TYPE_SC2681 },
1029 { "sc2691", SCCNXP_TYPE_SC2691 },
1030 { "sc2692", SCCNXP_TYPE_SC2692 },
1031 { "sc2891", SCCNXP_TYPE_SC2891 },
1032 { "sc2892", SCCNXP_TYPE_SC2892 },
1033 { "sc28202", SCCNXP_TYPE_SC28202 },
1034 { "sc68681", SCCNXP_TYPE_SC68681 },
1035 { "sc68692", SCCNXP_TYPE_SC68692 },
1036 { },
1037};
1038MODULE_DEVICE_TABLE(platform, sccnxp_id_table);
1039
1040static struct platform_driver sccnxp_uart_driver = { 1017static struct platform_driver sccnxp_uart_driver = {
1041 .driver = { 1018 .driver = {
1042 .name = SCCNXP_NAME, 1019 .name = SCCNXP_NAME,
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 28cdd2829139..0f02351c9239 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -2095,12 +2095,12 @@ uart_report_port(struct uart_driver *drv, struct uart_port *port)
2095 break; 2095 break;
2096 } 2096 }
2097 2097
2098 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n", 2098 printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2099 port->dev ? dev_name(port->dev) : "", 2099 port->dev ? dev_name(port->dev) : "",
2100 port->dev ? ": " : "", 2100 port->dev ? ": " : "",
2101 drv->dev_name, 2101 drv->dev_name,
2102 drv->tty_driver->name_base + port->line, 2102 drv->tty_driver->name_base + port->line,
2103 address, port->irq, uart_type(port)); 2103 address, port->irq, port->uartclk / 16, uart_type(port));
2104} 2104}
2105 2105
2106static void 2106static void
diff --git a/drivers/tty/serial/serial_txx9.c b/drivers/tty/serial/serial_txx9.c
index fe48a0c2b4ca..440a962412da 100644
--- a/drivers/tty/serial/serial_txx9.c
+++ b/drivers/tty/serial/serial_txx9.c
@@ -1097,7 +1097,7 @@ static void serial_txx9_unregister_port(int line)
1097 */ 1097 */
1098static int serial_txx9_probe(struct platform_device *dev) 1098static int serial_txx9_probe(struct platform_device *dev)
1099{ 1099{
1100 struct uart_port *p = dev->dev.platform_data; 1100 struct uart_port *p = dev_get_platdata(&dev->dev);
1101 struct uart_port port; 1101 struct uart_port port;
1102 int ret, i; 1102 int ret, i;
1103 1103
diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
index 7477e0ea5cdb..537750261aaa 100644
--- a/drivers/tty/serial/sh-sci.c
+++ b/drivers/tty/serial/sh-sci.c
@@ -2380,7 +2380,7 @@ static char early_serial_buf[32];
2380 2380
2381static int sci_probe_earlyprintk(struct platform_device *pdev) 2381static int sci_probe_earlyprintk(struct platform_device *pdev)
2382{ 2382{
2383 struct plat_sci_port *cfg = pdev->dev.platform_data; 2383 struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
2384 2384
2385 if (early_serial_console.data) 2385 if (early_serial_console.data)
2386 return -EEXIST; 2386 return -EEXIST;
@@ -2469,7 +2469,7 @@ static int sci_probe_single(struct platform_device *dev,
2469 2469
2470static int sci_probe(struct platform_device *dev) 2470static int sci_probe(struct platform_device *dev)
2471{ 2471{
2472 struct plat_sci_port *p = dev->dev.platform_data; 2472 struct plat_sci_port *p = dev_get_platdata(&dev->dev);
2473 struct sci_port *sp = &sci_ports[dev->id]; 2473 struct sci_port *sp = &sci_ports[dev->id];
2474 int ret; 2474 int ret;
2475 2475
diff --git a/drivers/tty/serial/sirfsoc_uart.c b/drivers/tty/serial/sirfsoc_uart.c
index 1fd564b8194b..67a0d1b8341c 100644
--- a/drivers/tty/serial/sirfsoc_uart.c
+++ b/drivers/tty/serial/sirfsoc_uart.c
@@ -717,7 +717,6 @@ port_err:
717 clk_disable_unprepare(sirfport->clk); 717 clk_disable_unprepare(sirfport->clk);
718 clk_put(sirfport->clk); 718 clk_put(sirfport->clk);
719clk_err: 719clk_err:
720 platform_set_drvdata(pdev, NULL);
721 if (sirfport->hw_flow_ctrl) 720 if (sirfport->hw_flow_ctrl)
722 pinctrl_put(sirfport->p); 721 pinctrl_put(sirfport->p);
723err: 722err:
@@ -728,7 +727,7 @@ static int sirfsoc_uart_remove(struct platform_device *pdev)
728{ 727{
729 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev); 728 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
730 struct uart_port *port = &sirfport->port; 729 struct uart_port *port = &sirfport->port;
731 platform_set_drvdata(pdev, NULL); 730
732 if (sirfport->hw_flow_ctrl) 731 if (sirfport->hw_flow_ctrl)
733 pinctrl_put(sirfport->p); 732 pinctrl_put(sirfport->p);
734 clk_disable_unprepare(sirfport->clk); 733 clk_disable_unprepare(sirfport->clk);
diff --git a/drivers/tty/serial/st-asc.c b/drivers/tty/serial/st-asc.c
new file mode 100644
index 000000000000..2ff4b1bb922d
--- /dev/null
+++ b/drivers/tty/serial/st-asc.c
@@ -0,0 +1,939 @@
1/*
2 * st-asc.c: ST Asynchronous serial controller (ASC) driver
3 *
4 * Copyright (C) 2003-2013 STMicroelectronics (R&D) Limited
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 */
12
13#if defined(CONFIG_SERIAL_ST_ASC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
14#define SUPPORT_SYSRQ
15#endif
16
17#include <linux/module.h>
18#include <linux/serial.h>
19#include <linux/console.h>
20#include <linux/sysrq.h>
21#include <linux/platform_device.h>
22#include <linux/io.h>
23#include <linux/irq.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/delay.h>
27#include <linux/spinlock.h>
28#include <linux/pm_runtime.h>
29#include <linux/of.h>
30#include <linux/of_platform.h>
31#include <linux/serial_core.h>
32#include <linux/clk.h>
33
34#define DRIVER_NAME "st-asc"
35#define ASC_SERIAL_NAME "ttyAS"
36#define ASC_FIFO_SIZE 16
37#define ASC_MAX_PORTS 8
38
39struct asc_port {
40 struct uart_port port;
41 struct clk *clk;
42 unsigned int hw_flow_control:1;
43 unsigned int force_m1:1;
44};
45
46static struct asc_port asc_ports[ASC_MAX_PORTS];
47static struct uart_driver asc_uart_driver;
48
49/*---- UART Register definitions ------------------------------*/
50
51/* Register offsets */
52
53#define ASC_BAUDRATE 0x00
54#define ASC_TXBUF 0x04
55#define ASC_RXBUF 0x08
56#define ASC_CTL 0x0C
57#define ASC_INTEN 0x10
58#define ASC_STA 0x14
59#define ASC_GUARDTIME 0x18
60#define ASC_TIMEOUT 0x1C
61#define ASC_TXRESET 0x20
62#define ASC_RXRESET 0x24
63#define ASC_RETRIES 0x28
64
65/* ASC_RXBUF */
66#define ASC_RXBUF_PE 0x100
67#define ASC_RXBUF_FE 0x200
68/**
69 * Some of status comes from higher bits of the character and some come from
70 * the status register. Combining both of them in to single status using dummy
71 * bits.
72 */
73#define ASC_RXBUF_DUMMY_RX 0x10000
74#define ASC_RXBUF_DUMMY_BE 0x20000
75#define ASC_RXBUF_DUMMY_OE 0x40000
76
77/* ASC_CTL */
78
79#define ASC_CTL_MODE_MSK 0x0007
80#define ASC_CTL_MODE_8BIT 0x0001
81#define ASC_CTL_MODE_7BIT_PAR 0x0003
82#define ASC_CTL_MODE_9BIT 0x0004
83#define ASC_CTL_MODE_8BIT_WKUP 0x0005
84#define ASC_CTL_MODE_8BIT_PAR 0x0007
85#define ASC_CTL_STOP_MSK 0x0018
86#define ASC_CTL_STOP_HALFBIT 0x0000
87#define ASC_CTL_STOP_1BIT 0x0008
88#define ASC_CTL_STOP_1_HALFBIT 0x0010
89#define ASC_CTL_STOP_2BIT 0x0018
90#define ASC_CTL_PARITYODD 0x0020
91#define ASC_CTL_LOOPBACK 0x0040
92#define ASC_CTL_RUN 0x0080
93#define ASC_CTL_RXENABLE 0x0100
94#define ASC_CTL_SCENABLE 0x0200
95#define ASC_CTL_FIFOENABLE 0x0400
96#define ASC_CTL_CTSENABLE 0x0800
97#define ASC_CTL_BAUDMODE 0x1000
98
99/* ASC_GUARDTIME */
100
101#define ASC_GUARDTIME_MSK 0x00FF
102
103/* ASC_INTEN */
104
105#define ASC_INTEN_RBE 0x0001
106#define ASC_INTEN_TE 0x0002
107#define ASC_INTEN_THE 0x0004
108#define ASC_INTEN_PE 0x0008
109#define ASC_INTEN_FE 0x0010
110#define ASC_INTEN_OE 0x0020
111#define ASC_INTEN_TNE 0x0040
112#define ASC_INTEN_TOI 0x0080
113#define ASC_INTEN_RHF 0x0100
114
115/* ASC_RETRIES */
116
117#define ASC_RETRIES_MSK 0x00FF
118
119/* ASC_RXBUF */
120
121#define ASC_RXBUF_MSK 0x03FF
122
123/* ASC_STA */
124
125#define ASC_STA_RBF 0x0001
126#define ASC_STA_TE 0x0002
127#define ASC_STA_THE 0x0004
128#define ASC_STA_PE 0x0008
129#define ASC_STA_FE 0x0010
130#define ASC_STA_OE 0x0020
131#define ASC_STA_TNE 0x0040
132#define ASC_STA_TOI 0x0080
133#define ASC_STA_RHF 0x0100
134#define ASC_STA_TF 0x0200
135#define ASC_STA_NKD 0x0400
136
137/* ASC_TIMEOUT */
138
139#define ASC_TIMEOUT_MSK 0x00FF
140
141/* ASC_TXBUF */
142
143#define ASC_TXBUF_MSK 0x01FF
144
145/*---- Inline function definitions ---------------------------*/
146
147static inline struct asc_port *to_asc_port(struct uart_port *port)
148{
149 return container_of(port, struct asc_port, port);
150}
151
152static inline u32 asc_in(struct uart_port *port, u32 offset)
153{
154 return readl(port->membase + offset);
155}
156
157static inline void asc_out(struct uart_port *port, u32 offset, u32 value)
158{
159 writel(value, port->membase + offset);
160}
161
162/*
163 * Some simple utility functions to enable and disable interrupts.
164 * Note that these need to be called with interrupts disabled.
165 */
166static inline void asc_disable_tx_interrupts(struct uart_port *port)
167{
168 u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_THE;
169 asc_out(port, ASC_INTEN, intenable);
170 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
171}
172
173static inline void asc_enable_tx_interrupts(struct uart_port *port)
174{
175 u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_THE;
176 asc_out(port, ASC_INTEN, intenable);
177}
178
179static inline void asc_disable_rx_interrupts(struct uart_port *port)
180{
181 u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_RBE;
182 asc_out(port, ASC_INTEN, intenable);
183 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
184}
185
186static inline void asc_enable_rx_interrupts(struct uart_port *port)
187{
188 u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_RBE;
189 asc_out(port, ASC_INTEN, intenable);
190}
191
192static inline u32 asc_txfifo_is_empty(struct uart_port *port)
193{
194 return asc_in(port, ASC_STA) & ASC_STA_TE;
195}
196
197static inline int asc_txfifo_is_full(struct uart_port *port)
198{
199 return asc_in(port, ASC_STA) & ASC_STA_TF;
200}
201
202static inline const char *asc_port_name(struct uart_port *port)
203{
204 return to_platform_device(port->dev)->name;
205}
206
207/*----------------------------------------------------------------------*/
208
209/*
210 * This section contains code to support the use of the ASC as a
211 * generic serial port.
212 */
213
214static inline unsigned asc_hw_txroom(struct uart_port *port)
215{
216 u32 status = asc_in(port, ASC_STA);
217
218 if (status & ASC_STA_THE)
219 return port->fifosize / 2;
220 else if (!(status & ASC_STA_TF))
221 return 1;
222
223 return 0;
224}
225
226/*
227 * Start transmitting chars.
228 * This is called from both interrupt and task level.
229 * Either way interrupts are disabled.
230 */
231static void asc_transmit_chars(struct uart_port *port)
232{
233 struct circ_buf *xmit = &port->state->xmit;
234 int txroom;
235 unsigned char c;
236
237 txroom = asc_hw_txroom(port);
238
239 if ((txroom != 0) && port->x_char) {
240 c = port->x_char;
241 port->x_char = 0;
242 asc_out(port, ASC_TXBUF, c);
243 port->icount.tx++;
244 txroom = asc_hw_txroom(port);
245 }
246
247 if (uart_tx_stopped(port)) {
248 /*
249 * We should try and stop the hardware here, but I
250 * don't think the ASC has any way to do that.
251 */
252 asc_disable_tx_interrupts(port);
253 return;
254 }
255
256 if (uart_circ_empty(xmit)) {
257 asc_disable_tx_interrupts(port);
258 return;
259 }
260
261 if (txroom == 0)
262 return;
263
264 do {
265 c = xmit->buf[xmit->tail];
266 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
267 asc_out(port, ASC_TXBUF, c);
268 port->icount.tx++;
269 txroom--;
270 } while ((txroom > 0) && (!uart_circ_empty(xmit)));
271
272 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
273 uart_write_wakeup(port);
274
275 if (uart_circ_empty(xmit))
276 asc_disable_tx_interrupts(port);
277}
278
279static void asc_receive_chars(struct uart_port *port)
280{
281 struct tty_port *tport = &port->state->port;
282 unsigned long status;
283 unsigned long c = 0;
284 char flag;
285
286 if (port->irq_wake)
287 pm_wakeup_event(tport->tty->dev, 0);
288
289 while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) {
290 c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX;
291 flag = TTY_NORMAL;
292 port->icount.rx++;
293
294 if ((c & (ASC_RXBUF_FE | ASC_RXBUF_PE)) ||
295 status & ASC_STA_OE) {
296
297 if (c & ASC_RXBUF_FE) {
298 if (c == ASC_RXBUF_FE) {
299 port->icount.brk++;
300 if (uart_handle_break(port))
301 continue;
302 c |= ASC_RXBUF_DUMMY_BE;
303 } else {
304 port->icount.frame++;
305 }
306 } else if (c & ASC_RXBUF_PE) {
307 port->icount.parity++;
308 }
309 /*
310 * Reading any data from the RX FIFO clears the
311 * overflow error condition.
312 */
313 if (status & ASC_STA_OE) {
314 port->icount.overrun++;
315 c |= ASC_RXBUF_DUMMY_OE;
316 }
317
318 c &= port->read_status_mask;
319
320 if (c & ASC_RXBUF_DUMMY_BE)
321 flag = TTY_BREAK;
322 else if (c & ASC_RXBUF_PE)
323 flag = TTY_PARITY;
324 else if (c & ASC_RXBUF_FE)
325 flag = TTY_FRAME;
326 }
327
328 if (uart_handle_sysrq_char(port, c))
329 continue;
330
331 uart_insert_char(port, c, ASC_RXBUF_DUMMY_OE, c & 0xff, flag);
332 }
333
334 /* Tell the rest of the system the news. New characters! */
335 tty_flip_buffer_push(tport);
336}
337
338static irqreturn_t asc_interrupt(int irq, void *ptr)
339{
340 struct uart_port *port = ptr;
341 u32 status;
342
343 spin_lock(&port->lock);
344
345 status = asc_in(port, ASC_STA);
346
347 if (status & ASC_STA_RBF) {
348 /* Receive FIFO not empty */
349 asc_receive_chars(port);
350 }
351
352 if ((status & ASC_STA_THE) &&
353 (asc_in(port, ASC_INTEN) & ASC_INTEN_THE)) {
354 /* Transmitter FIFO at least half empty */
355 asc_transmit_chars(port);
356 }
357
358 spin_unlock(&port->lock);
359
360 return IRQ_HANDLED;
361}
362
363/*----------------------------------------------------------------------*/
364
365/*
366 * UART Functions
367 */
368
369static unsigned int asc_tx_empty(struct uart_port *port)
370{
371 return asc_txfifo_is_empty(port) ? TIOCSER_TEMT : 0;
372}
373
374static void asc_set_mctrl(struct uart_port *port, unsigned int mctrl)
375{
376 /*
377 * This routine is used for seting signals of: DTR, DCD, CTS/RTS
378 * We use ASC's hardware for CTS/RTS, so don't need any for that.
379 * Some boards have DTR and DCD implemented using PIO pins,
380 * code to do this should be hooked in here.
381 */
382}
383
384static unsigned int asc_get_mctrl(struct uart_port *port)
385{
386 /*
387 * This routine is used for geting signals of: DTR, DCD, DSR, RI,
388 * and CTS/RTS
389 */
390 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
391}
392
393/* There are probably characters waiting to be transmitted. */
394static void asc_start_tx(struct uart_port *port)
395{
396 struct circ_buf *xmit = &port->state->xmit;
397
398 if (!uart_circ_empty(xmit))
399 asc_enable_tx_interrupts(port);
400}
401
402/* Transmit stop */
403static void asc_stop_tx(struct uart_port *port)
404{
405 asc_disable_tx_interrupts(port);
406}
407
408/* Receive stop */
409static void asc_stop_rx(struct uart_port *port)
410{
411 asc_disable_rx_interrupts(port);
412}
413
414/* Force modem status interrupts on */
415static void asc_enable_ms(struct uart_port *port)
416{
417 /* Nothing here yet .. */
418}
419
420/* Handle breaks - ignored by us */
421static void asc_break_ctl(struct uart_port *port, int break_state)
422{
423 /* Nothing here yet .. */
424}
425
426/*
427 * Enable port for reception.
428 */
429static int asc_startup(struct uart_port *port)
430{
431 if (request_irq(port->irq, asc_interrupt, IRQF_NO_SUSPEND,
432 asc_port_name(port), port)) {
433 dev_err(port->dev, "cannot allocate irq.\n");
434 return -ENODEV;
435 }
436
437 asc_transmit_chars(port);
438 asc_enable_rx_interrupts(port);
439
440 return 0;
441}
442
443static void asc_shutdown(struct uart_port *port)
444{
445 asc_disable_tx_interrupts(port);
446 asc_disable_rx_interrupts(port);
447 free_irq(port->irq, port);
448}
449
450static void asc_pm(struct uart_port *port, unsigned int state,
451 unsigned int oldstate)
452{
453 struct asc_port *ascport = to_asc_port(port);
454 unsigned long flags = 0;
455 u32 ctl;
456
457 switch (state) {
458 case UART_PM_STATE_ON:
459 clk_prepare_enable(ascport->clk);
460 break;
461 case UART_PM_STATE_OFF:
462 /*
463 * Disable the ASC baud rate generator, which is as close as
464 * we can come to turning it off. Note this is not called with
465 * the port spinlock held.
466 */
467 spin_lock_irqsave(&port->lock, flags);
468 ctl = asc_in(port, ASC_CTL) & ~ASC_CTL_RUN;
469 asc_out(port, ASC_CTL, ctl);
470 spin_unlock_irqrestore(&port->lock, flags);
471 clk_disable_unprepare(ascport->clk);
472 break;
473 }
474}
475
476static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
477 struct ktermios *old)
478{
479 struct asc_port *ascport = to_asc_port(port);
480 unsigned int baud;
481 u32 ctrl_val;
482 tcflag_t cflag;
483 unsigned long flags;
484
485 /* Update termios to reflect hardware capabilities */
486 termios->c_cflag &= ~(CMSPAR |
487 (ascport->hw_flow_control ? 0 : CRTSCTS));
488
489 port->uartclk = clk_get_rate(ascport->clk);
490
491 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
492 cflag = termios->c_cflag;
493
494 spin_lock_irqsave(&port->lock, flags);
495
496 /* read control register */
497 ctrl_val = asc_in(port, ASC_CTL);
498
499 /* stop serial port and reset value */
500 asc_out(port, ASC_CTL, (ctrl_val & ~ASC_CTL_RUN));
501 ctrl_val = ASC_CTL_RXENABLE | ASC_CTL_FIFOENABLE;
502
503 /* reset fifo rx & tx */
504 asc_out(port, ASC_TXRESET, 1);
505 asc_out(port, ASC_RXRESET, 1);
506
507 /* set character length */
508 if ((cflag & CSIZE) == CS7) {
509 ctrl_val |= ASC_CTL_MODE_7BIT_PAR;
510 } else {
511 ctrl_val |= (cflag & PARENB) ? ASC_CTL_MODE_8BIT_PAR :
512 ASC_CTL_MODE_8BIT;
513 }
514
515 /* set stop bit */
516 ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT;
517
518 /* odd parity */
519 if (cflag & PARODD)
520 ctrl_val |= ASC_CTL_PARITYODD;
521
522 /* hardware flow control */
523 if ((cflag & CRTSCTS))
524 ctrl_val |= ASC_CTL_CTSENABLE;
525
526 if ((baud < 19200) && !ascport->force_m1) {
527 asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud)));
528 } else {
529 /*
530 * MODE 1: recommended for high bit rates (above 19.2K)
531 *
532 * baudrate * 16 * 2^16
533 * ASCBaudRate = ------------------------
534 * inputclock
535 *
536 * However to keep the maths inside 32bits we divide top and
537 * bottom by 64. The +1 is to avoid a divide by zero if the
538 * input clock rate is something unexpected.
539 */
540 u32 counter = (baud * 16384) / ((port->uartclk / 64) + 1);
541 asc_out(port, ASC_BAUDRATE, counter);
542 ctrl_val |= ASC_CTL_BAUDMODE;
543 }
544
545 uart_update_timeout(port, cflag, baud);
546
547 ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE;
548 if (termios->c_iflag & INPCK)
549 ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
550 if (termios->c_iflag & (BRKINT | PARMRK))
551 ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE;
552
553 /*
554 * Characters to ignore
555 */
556 ascport->port.ignore_status_mask = 0;
557 if (termios->c_iflag & IGNPAR)
558 ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
559 if (termios->c_iflag & IGNBRK) {
560 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE;
561 /*
562 * If we're ignoring parity and break indicators,
563 * ignore overruns too (for real raw support).
564 */
565 if (termios->c_iflag & IGNPAR)
566 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE;
567 }
568
569 /*
570 * Ignore all characters if CREAD is not set.
571 */
572 if (!(termios->c_cflag & CREAD))
573 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX;
574
575 /* Set the timeout */
576 asc_out(port, ASC_TIMEOUT, 20);
577
578 /* write final value and enable port */
579 asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
580
581 spin_unlock_irqrestore(&port->lock, flags);
582}
583
584static const char *asc_type(struct uart_port *port)
585{
586 return (port->type == PORT_ASC) ? DRIVER_NAME : NULL;
587}
588
589static void asc_release_port(struct uart_port *port)
590{
591}
592
593static int asc_request_port(struct uart_port *port)
594{
595 return 0;
596}
597
598/*
599 * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
600 * Set type field if successful
601 */
602static void asc_config_port(struct uart_port *port, int flags)
603{
604 if ((flags & UART_CONFIG_TYPE))
605 port->type = PORT_ASC;
606}
607
608static int
609asc_verify_port(struct uart_port *port, struct serial_struct *ser)
610{
611 /* No user changeable parameters */
612 return -EINVAL;
613}
614
615#ifdef CONFIG_CONSOLE_POLL
616/*
617 * Console polling routines for writing and reading from the uart while
618 * in an interrupt or debug context (i.e. kgdb).
619 */
620
621static int asc_get_poll_char(struct uart_port *port)
622{
623 if (!(asc_in(port, ASC_STA) & ASC_STA_RBF))
624 return NO_POLL_CHAR;
625
626 return asc_in(port, ASC_RXBUF);
627}
628
629static void asc_put_poll_char(struct uart_port *port, unsigned char c)
630{
631 while (asc_txfifo_is_full(port))
632 cpu_relax();
633 asc_out(port, ASC_TXBUF, c);
634}
635
636#endif /* CONFIG_CONSOLE_POLL */
637
638/*---------------------------------------------------------------------*/
639
640static struct uart_ops asc_uart_ops = {
641 .tx_empty = asc_tx_empty,
642 .set_mctrl = asc_set_mctrl,
643 .get_mctrl = asc_get_mctrl,
644 .start_tx = asc_start_tx,
645 .stop_tx = asc_stop_tx,
646 .stop_rx = asc_stop_rx,
647 .enable_ms = asc_enable_ms,
648 .break_ctl = asc_break_ctl,
649 .startup = asc_startup,
650 .shutdown = asc_shutdown,
651 .set_termios = asc_set_termios,
652 .type = asc_type,
653 .release_port = asc_release_port,
654 .request_port = asc_request_port,
655 .config_port = asc_config_port,
656 .verify_port = asc_verify_port,
657 .pm = asc_pm,
658#ifdef CONFIG_CONSOLE_POLL
659 .poll_get_char = asc_get_poll_char,
660 .poll_put_char = asc_put_poll_char,
661#endif /* CONFIG_CONSOLE_POLL */
662};
663
664static int asc_init_port(struct asc_port *ascport,
665 struct platform_device *pdev)
666{
667 struct uart_port *port = &ascport->port;
668 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
669
670 if (!res) {
671 dev_err(&pdev->dev, "Unable to get io resource\n");
672 return -ENODEV;
673 }
674
675 port->iotype = UPIO_MEM;
676 port->flags = UPF_BOOT_AUTOCONF;
677 port->ops = &asc_uart_ops;
678 port->fifosize = ASC_FIFO_SIZE;
679 port->dev = &pdev->dev;
680 port->mapbase = res->start;
681 port->irq = platform_get_irq(pdev, 0);
682
683 port->membase = devm_request_and_ioremap(&pdev->dev, res);
684 if (!port->membase) {
685 dev_err(&pdev->dev, "Unable to request io memory\n");
686 return -ENODEV;
687 }
688
689 spin_lock_init(&port->lock);
690
691 ascport->clk = devm_clk_get(&pdev->dev, NULL);
692
693 if (WARN_ON(IS_ERR(ascport->clk)))
694 return -EINVAL;
695 /* ensure that clk rate is correct by enabling the clk */
696 clk_prepare_enable(ascport->clk);
697 ascport->port.uartclk = clk_get_rate(ascport->clk);
698 WARN_ON(ascport->port.uartclk == 0);
699 clk_disable_unprepare(ascport->clk);
700
701 return 0;
702}
703
704static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev)
705{
706 struct device_node *np = pdev->dev.of_node;
707 int id;
708
709 if (!np)
710 return NULL;
711
712 id = of_alias_get_id(np, ASC_SERIAL_NAME);
713
714 if (id < 0)
715 id = 0;
716
717 if (WARN_ON(id >= ASC_MAX_PORTS))
718 return NULL;
719
720 asc_ports[id].hw_flow_control = of_property_read_bool(np,
721 "st,hw-flow-control");
722 asc_ports[id].force_m1 = of_property_read_bool(np, "st,force_m1");
723 asc_ports[id].port.line = id;
724 return &asc_ports[id];
725}
726
727#ifdef CONFIG_OF
728static struct of_device_id asc_match[] = {
729 { .compatible = "st,asc", },
730 {},
731};
732
733MODULE_DEVICE_TABLE(of, asc_match);
734#endif
735
736static int asc_serial_probe(struct platform_device *pdev)
737{
738 int ret;
739 struct asc_port *ascport;
740
741 ascport = asc_of_get_asc_port(pdev);
742 if (!ascport)
743 return -ENODEV;
744
745 ret = asc_init_port(ascport, pdev);
746 if (ret)
747 return ret;
748
749 ret = uart_add_one_port(&asc_uart_driver, &ascport->port);
750 if (ret)
751 return ret;
752
753 platform_set_drvdata(pdev, &ascport->port);
754
755 return 0;
756}
757
758static int asc_serial_remove(struct platform_device *pdev)
759{
760 struct uart_port *port = platform_get_drvdata(pdev);
761
762 platform_set_drvdata(pdev, NULL);
763 return uart_remove_one_port(&asc_uart_driver, port);
764}
765
766#ifdef CONFIG_PM_SLEEP
767static int asc_serial_suspend(struct device *dev)
768{
769 struct platform_device *pdev = to_platform_device(dev);
770 struct uart_port *port = platform_get_drvdata(pdev);
771
772 return uart_suspend_port(&asc_uart_driver, port);
773}
774
775static int asc_serial_resume(struct device *dev)
776{
777 struct platform_device *pdev = to_platform_device(dev);
778 struct uart_port *port = platform_get_drvdata(pdev);
779
780 return uart_resume_port(&asc_uart_driver, port);
781}
782
783#endif /* CONFIG_PM_SLEEP */
784
785/*----------------------------------------------------------------------*/
786
787#ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
788static void asc_console_putchar(struct uart_port *port, int ch)
789{
790 unsigned int timeout = 1000000;
791
792 /* Wait for upto 1 second in case flow control is stopping us. */
793 while (--timeout && asc_txfifo_is_full(port))
794 udelay(1);
795
796 asc_out(port, ASC_TXBUF, ch);
797}
798
799/*
800 * Print a string to the serial port trying not to disturb
801 * any possible real use of the port...
802 */
803
804static void asc_console_write(struct console *co, const char *s, unsigned count)
805{
806 struct uart_port *port = &asc_ports[co->index].port;
807 unsigned long flags;
808 unsigned long timeout = 1000000;
809 int locked = 1;
810 u32 intenable;
811
812 local_irq_save(flags);
813 if (port->sysrq)
814 locked = 0; /* asc_interrupt has already claimed the lock */
815 else if (oops_in_progress)
816 locked = spin_trylock(&port->lock);
817 else
818 spin_lock(&port->lock);
819
820 /*
821 * Disable interrupts so we don't get the IRQ line bouncing
822 * up and down while interrupts are disabled.
823 */
824 intenable = asc_in(port, ASC_INTEN);
825 asc_out(port, ASC_INTEN, 0);
826 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
827
828 uart_console_write(port, s, count, asc_console_putchar);
829
830 while (--timeout && !asc_txfifo_is_empty(port))
831 udelay(1);
832
833 asc_out(port, ASC_INTEN, intenable);
834
835 if (locked)
836 spin_unlock(&port->lock);
837 local_irq_restore(flags);
838}
839
840static int asc_console_setup(struct console *co, char *options)
841{
842 struct asc_port *ascport;
843 int baud = 9600;
844 int bits = 8;
845 int parity = 'n';
846 int flow = 'n';
847
848 if (co->index >= ASC_MAX_PORTS)
849 return -ENODEV;
850
851 ascport = &asc_ports[co->index];
852
853 /*
854 * This driver does not support early console initialization
855 * (use ARM early printk support instead), so we only expect
856 * this to be called during the uart port registration when the
857 * driver gets probed and the port should be mapped at that point.
858 */
859 BUG_ON(ascport->port.mapbase == 0 || ascport->port.membase == NULL);
860
861 if (options)
862 uart_parse_options(options, &baud, &parity, &bits, &flow);
863
864 return uart_set_options(&ascport->port, co, baud, parity, bits, flow);
865}
866
867static struct console asc_console = {
868 .name = ASC_SERIAL_NAME,
869 .device = uart_console_device,
870 .write = asc_console_write,
871 .setup = asc_console_setup,
872 .flags = CON_PRINTBUFFER,
873 .index = -1,
874 .data = &asc_uart_driver,
875};
876
877#define ASC_SERIAL_CONSOLE (&asc_console)
878
879#else
880#define ASC_SERIAL_CONSOLE NULL
881#endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
882
883static struct uart_driver asc_uart_driver = {
884 .owner = THIS_MODULE,
885 .driver_name = DRIVER_NAME,
886 .dev_name = ASC_SERIAL_NAME,
887 .major = 0,
888 .minor = 0,
889 .nr = ASC_MAX_PORTS,
890 .cons = ASC_SERIAL_CONSOLE,
891};
892
893static const struct dev_pm_ops asc_serial_pm_ops = {
894 SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend, asc_serial_resume)
895};
896
897static struct platform_driver asc_serial_driver = {
898 .probe = asc_serial_probe,
899 .remove = asc_serial_remove,
900 .driver = {
901 .name = DRIVER_NAME,
902 .pm = &asc_serial_pm_ops,
903 .owner = THIS_MODULE,
904 .of_match_table = of_match_ptr(asc_match),
905 },
906};
907
908static int __init asc_init(void)
909{
910 int ret;
911 static char banner[] __initdata =
912 KERN_INFO "STMicroelectronics ASC driver initialized\n";
913
914 printk(banner);
915
916 ret = uart_register_driver(&asc_uart_driver);
917 if (ret)
918 return ret;
919
920 ret = platform_driver_register(&asc_serial_driver);
921 if (ret)
922 uart_unregister_driver(&asc_uart_driver);
923
924 return ret;
925}
926
927static void __exit asc_exit(void)
928{
929 platform_driver_unregister(&asc_serial_driver);
930 uart_unregister_driver(&asc_uart_driver);
931}
932
933module_init(asc_init);
934module_exit(asc_exit);
935
936MODULE_ALIAS("platform:" DRIVER_NAME);
937MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
938MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
939MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/vr41xx_siu.c b/drivers/tty/serial/vr41xx_siu.c
index f655997f44af..a63c14bc9a24 100644
--- a/drivers/tty/serial/vr41xx_siu.c
+++ b/drivers/tty/serial/vr41xx_siu.c
@@ -705,7 +705,7 @@ static int siu_init_ports(struct platform_device *pdev)
705{ 705{
706 struct uart_port *port; 706 struct uart_port *port;
707 struct resource *res; 707 struct resource *res;
708 int *type = pdev->dev.platform_data; 708 int *type = dev_get_platdata(&pdev->dev);
709 int i; 709 int i;
710 710
711 if (!type) 711 if (!type)
diff --git a/drivers/tty/serial/vt8500_serial.c b/drivers/tty/serial/vt8500_serial.c
index 48af43de3467..a90bf0440b4f 100644
--- a/drivers/tty/serial/vt8500_serial.c
+++ b/drivers/tty/serial/vt8500_serial.c
@@ -630,7 +630,6 @@ static int vt8500_serial_remove(struct platform_device *pdev)
630{ 630{
631 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev); 631 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
632 632
633 platform_set_drvdata(pdev, NULL);
634 clk_disable_unprepare(vt8500_port->clk); 633 clk_disable_unprepare(vt8500_port->clk);
635 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart); 634 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
636 635
diff --git a/drivers/tty/synclink.c b/drivers/tty/synclink.c
index 8eaf1ab8addb..e1ce141bad5e 100644
--- a/drivers/tty/synclink.c
+++ b/drivers/tty/synclink.c
@@ -577,22 +577,22 @@ struct mgsl_struct {
577 577
578#define SICR_RXC_ACTIVE BIT15 578#define SICR_RXC_ACTIVE BIT15
579#define SICR_RXC_INACTIVE BIT14 579#define SICR_RXC_INACTIVE BIT14
580#define SICR_RXC (BIT15+BIT14) 580#define SICR_RXC (BIT15|BIT14)
581#define SICR_TXC_ACTIVE BIT13 581#define SICR_TXC_ACTIVE BIT13
582#define SICR_TXC_INACTIVE BIT12 582#define SICR_TXC_INACTIVE BIT12
583#define SICR_TXC (BIT13+BIT12) 583#define SICR_TXC (BIT13|BIT12)
584#define SICR_RI_ACTIVE BIT11 584#define SICR_RI_ACTIVE BIT11
585#define SICR_RI_INACTIVE BIT10 585#define SICR_RI_INACTIVE BIT10
586#define SICR_RI (BIT11+BIT10) 586#define SICR_RI (BIT11|BIT10)
587#define SICR_DSR_ACTIVE BIT9 587#define SICR_DSR_ACTIVE BIT9
588#define SICR_DSR_INACTIVE BIT8 588#define SICR_DSR_INACTIVE BIT8
589#define SICR_DSR (BIT9+BIT8) 589#define SICR_DSR (BIT9|BIT8)
590#define SICR_DCD_ACTIVE BIT7 590#define SICR_DCD_ACTIVE BIT7
591#define SICR_DCD_INACTIVE BIT6 591#define SICR_DCD_INACTIVE BIT6
592#define SICR_DCD (BIT7+BIT6) 592#define SICR_DCD (BIT7|BIT6)
593#define SICR_CTS_ACTIVE BIT5 593#define SICR_CTS_ACTIVE BIT5
594#define SICR_CTS_INACTIVE BIT4 594#define SICR_CTS_INACTIVE BIT4
595#define SICR_CTS (BIT5+BIT4) 595#define SICR_CTS (BIT5|BIT4)
596#define SICR_RCC_UNDERFLOW BIT3 596#define SICR_RCC_UNDERFLOW BIT3
597#define SICR_DPLL_NO_SYNC BIT2 597#define SICR_DPLL_NO_SYNC BIT2
598#define SICR_BRG1_ZERO BIT1 598#define SICR_BRG1_ZERO BIT1
@@ -1161,7 +1161,7 @@ static void mgsl_isr_receive_status( struct mgsl_struct *info )
1161{ 1161{
1162 u16 status = usc_InReg( info, RCSR ); 1162 u16 status = usc_InReg( info, RCSR );
1163 1163
1164 if ( debug_level >= DEBUG_LEVEL_ISR ) 1164 if ( debug_level >= DEBUG_LEVEL_ISR )
1165 printk("%s(%d):mgsl_isr_receive_status status=%04X\n", 1165 printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
1166 __FILE__,__LINE__,status); 1166 __FILE__,__LINE__,status);
1167 1167
@@ -1181,7 +1181,7 @@ static void mgsl_isr_receive_status( struct mgsl_struct *info )
1181 (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED)); 1181 (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
1182 } 1182 }
1183 1183
1184 if (status & (RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)) { 1184 if (status & (RXSTATUS_EXITED_HUNT | RXSTATUS_IDLE_RECEIVED)) {
1185 if (status & RXSTATUS_EXITED_HUNT) 1185 if (status & RXSTATUS_EXITED_HUNT)
1186 info->icount.exithunt++; 1186 info->icount.exithunt++;
1187 if (status & RXSTATUS_IDLE_RECEIVED) 1187 if (status & RXSTATUS_IDLE_RECEIVED)
@@ -1463,21 +1463,21 @@ static void mgsl_isr_receive_data( struct mgsl_struct *info )
1463 1463
1464 /* get the status of the received byte */ 1464 /* get the status of the received byte */
1465 status = usc_InReg(info, RCSR); 1465 status = usc_InReg(info, RCSR);
1466 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR + 1466 if ( status & (RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR |
1467 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) 1467 RXSTATUS_OVERRUN | RXSTATUS_BREAK_RECEIVED) )
1468 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL); 1468 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
1469 1469
1470 icount->rx++; 1470 icount->rx++;
1471 1471
1472 flag = 0; 1472 flag = 0;
1473 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR + 1473 if ( status & (RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR |
1474 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) { 1474 RXSTATUS_OVERRUN | RXSTATUS_BREAK_RECEIVED) ) {
1475 printk("rxerr=%04X\n",status); 1475 printk("rxerr=%04X\n",status);
1476 /* update error statistics */ 1476 /* update error statistics */
1477 if ( status & RXSTATUS_BREAK_RECEIVED ) { 1477 if ( status & RXSTATUS_BREAK_RECEIVED ) {
1478 status &= ~(RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR); 1478 status &= ~(RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR);
1479 icount->brk++; 1479 icount->brk++;
1480 } else if (status & RXSTATUS_PARITY_ERROR) 1480 } else if (status & RXSTATUS_PARITY_ERROR)
1481 icount->parity++; 1481 icount->parity++;
1482 else if (status & RXSTATUS_FRAMING_ERROR) 1482 else if (status & RXSTATUS_FRAMING_ERROR)
1483 icount->frame++; 1483 icount->frame++;
@@ -1488,7 +1488,7 @@ static void mgsl_isr_receive_data( struct mgsl_struct *info )
1488 icount->overrun++; 1488 icount->overrun++;
1489 } 1489 }
1490 1490
1491 /* discard char if tty control flags say so */ 1491 /* discard char if tty control flags say so */
1492 if (status & info->ignore_status_mask) 1492 if (status & info->ignore_status_mask)
1493 continue; 1493 continue;
1494 1494
@@ -1545,8 +1545,8 @@ static void mgsl_isr_misc( struct mgsl_struct *info )
1545 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL); 1545 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
1546 usc_DmaCmd(info, DmaCmd_ResetRxChannel); 1546 usc_DmaCmd(info, DmaCmd_ResetRxChannel);
1547 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL); 1547 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
1548 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS); 1548 usc_ClearIrqPendingBits(info, RECEIVE_DATA | RECEIVE_STATUS);
1549 usc_DisableInterrupts(info, RECEIVE_DATA + RECEIVE_STATUS); 1549 usc_DisableInterrupts(info, RECEIVE_DATA | RECEIVE_STATUS);
1550 1550
1551 /* schedule BH handler to restart receiver */ 1551 /* schedule BH handler to restart receiver */
1552 info->pending_bh |= BH_RECEIVE; 1552 info->pending_bh |= BH_RECEIVE;
@@ -1595,7 +1595,7 @@ static void mgsl_isr_receive_dma( struct mgsl_struct *info )
1595 u16 status; 1595 u16 status;
1596 1596
1597 /* clear interrupt pending and IUS bit for Rx DMA IRQ */ 1597 /* clear interrupt pending and IUS bit for Rx DMA IRQ */
1598 usc_OutDmaReg( info, CDIR, BIT9+BIT1 ); 1598 usc_OutDmaReg( info, CDIR, BIT9 | BIT1 );
1599 1599
1600 /* Read the receive DMA status to identify interrupt type. */ 1600 /* Read the receive DMA status to identify interrupt type. */
1601 /* This also clears the status bits. */ 1601 /* This also clears the status bits. */
@@ -1639,7 +1639,7 @@ static void mgsl_isr_transmit_dma( struct mgsl_struct *info )
1639 u16 status; 1639 u16 status;
1640 1640
1641 /* clear interrupt pending and IUS bit for Tx DMA IRQ */ 1641 /* clear interrupt pending and IUS bit for Tx DMA IRQ */
1642 usc_OutDmaReg(info, CDIR, BIT8+BIT0 ); 1642 usc_OutDmaReg(info, CDIR, BIT8 | BIT0 );
1643 1643
1644 /* Read the transmit DMA status to identify interrupt type. */ 1644 /* Read the transmit DMA status to identify interrupt type. */
1645 /* This also clears the status bits. */ 1645 /* This also clears the status bits. */
@@ -1832,8 +1832,8 @@ static void shutdown(struct mgsl_struct * info)
1832 usc_DisableMasterIrqBit(info); 1832 usc_DisableMasterIrqBit(info);
1833 usc_stop_receiver(info); 1833 usc_stop_receiver(info);
1834 usc_stop_transmitter(info); 1834 usc_stop_transmitter(info);
1835 usc_DisableInterrupts(info,RECEIVE_DATA + RECEIVE_STATUS + 1835 usc_DisableInterrupts(info,RECEIVE_DATA | RECEIVE_STATUS |
1836 TRANSMIT_DATA + TRANSMIT_STATUS + IO_PIN + MISC ); 1836 TRANSMIT_DATA | TRANSMIT_STATUS | IO_PIN | MISC );
1837 usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE); 1837 usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
1838 1838
1839 /* Disable DMAEN (Port 7, Bit 14) */ 1839 /* Disable DMAEN (Port 7, Bit 14) */
@@ -1886,7 +1886,7 @@ static void mgsl_program_hw(struct mgsl_struct *info)
1886 info->ri_chkcount = 0; 1886 info->ri_chkcount = 0;
1887 info->dsr_chkcount = 0; 1887 info->dsr_chkcount = 0;
1888 1888
1889 usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI); 1889 usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);
1890 usc_EnableInterrupts(info, IO_PIN); 1890 usc_EnableInterrupts(info, IO_PIN);
1891 usc_get_serial_signals(info); 1891 usc_get_serial_signals(info);
1892 1892
@@ -2773,7 +2773,7 @@ static int mgsl_wait_event(struct mgsl_struct * info, int __user * mask_ptr)
2773 if (!waitqueue_active(&info->event_wait_q)) { 2773 if (!waitqueue_active(&info->event_wait_q)) {
2774 /* disable enable exit hunt mode/idle rcvd IRQs */ 2774 /* disable enable exit hunt mode/idle rcvd IRQs */
2775 usc_OutReg(info, RICR, usc_InReg(info,RICR) & 2775 usc_OutReg(info, RICR, usc_InReg(info,RICR) &
2776 ~(RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)); 2776 ~(RXSTATUS_EXITED_HUNT | RXSTATUS_IDLE_RECEIVED));
2777 } 2777 }
2778 spin_unlock_irqrestore(&info->irq_spinlock,flags); 2778 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2779 } 2779 }
@@ -3092,7 +3092,7 @@ static void mgsl_close(struct tty_struct *tty, struct file * filp)
3092 printk("%s(%d):mgsl_close(%s) entry, count=%d\n", 3092 printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
3093 __FILE__,__LINE__, info->device_name, info->port.count); 3093 __FILE__,__LINE__, info->device_name, info->port.count);
3094 3094
3095 if (tty_port_close_start(&info->port, tty, filp) == 0) 3095 if (tty_port_close_start(&info->port, tty, filp) == 0)
3096 goto cleanup; 3096 goto cleanup;
3097 3097
3098 mutex_lock(&info->port.mutex); 3098 mutex_lock(&info->port.mutex);
@@ -4297,7 +4297,7 @@ static struct mgsl_struct* mgsl_allocate_device(void)
4297 spin_lock_init(&info->irq_spinlock); 4297 spin_lock_init(&info->irq_spinlock);
4298 spin_lock_init(&info->netlock); 4298 spin_lock_init(&info->netlock);
4299 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS)); 4299 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
4300 info->idle_mode = HDLC_TXIDLE_FLAGS; 4300 info->idle_mode = HDLC_TXIDLE_FLAGS;
4301 info->num_tx_dma_buffers = 1; 4301 info->num_tx_dma_buffers = 1;
4302 info->num_tx_holding_buffers = 0; 4302 info->num_tx_holding_buffers = 0;
4303 } 4303 }
@@ -4722,7 +4722,7 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
4722 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG ) 4722 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG )
4723 RegValue |= BIT15; 4723 RegValue |= BIT15;
4724 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC ) 4724 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC )
4725 RegValue |= BIT15 + BIT14; 4725 RegValue |= BIT15 | BIT14;
4726 } 4726 }
4727 4727
4728 if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE ) 4728 if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE )
@@ -4763,11 +4763,11 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
4763 switch ( info->params.encoding ) { 4763 switch ( info->params.encoding ) {
4764 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break; 4764 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4765 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break; 4765 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4766 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break; 4766 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 | BIT13; break;
4767 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break; 4767 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4768 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break; 4768 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 | BIT13; break;
4769 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break; 4769 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14; break;
4770 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break; 4770 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14 | BIT13; break;
4771 } 4771 }
4772 4772
4773 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT ) 4773 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
@@ -4838,15 +4838,15 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
4838 switch ( info->params.encoding ) { 4838 switch ( info->params.encoding ) {
4839 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break; 4839 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4840 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break; 4840 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4841 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break; 4841 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 | BIT13; break;
4842 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break; 4842 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4843 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break; 4843 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 | BIT13; break;
4844 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break; 4844 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14; break;
4845 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break; 4845 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14 | BIT13; break;
4846 } 4846 }
4847 4847
4848 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT ) 4848 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
4849 RegValue |= BIT9 + BIT8; 4849 RegValue |= BIT9 | BIT8;
4850 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT ) 4850 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
4851 RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8); 4851 RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8);
4852 4852
@@ -4957,7 +4957,7 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
4957 4957
4958 RegValue = 0x0000; 4958 RegValue = 0x0000;
4959 4959
4960 if ( info->params.flags & (HDLC_FLAG_RXC_DPLL + HDLC_FLAG_TXC_DPLL) ) { 4960 if ( info->params.flags & (HDLC_FLAG_RXC_DPLL | HDLC_FLAG_TXC_DPLL) ) {
4961 u32 XtalSpeed; 4961 u32 XtalSpeed;
4962 u32 DpllDivisor; 4962 u32 DpllDivisor;
4963 u16 Tc; 4963 u16 Tc;
@@ -5019,7 +5019,7 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
5019 case HDLC_ENCODING_BIPHASE_MARK: 5019 case HDLC_ENCODING_BIPHASE_MARK:
5020 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break; 5020 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break;
5021 case HDLC_ENCODING_BIPHASE_LEVEL: 5021 case HDLC_ENCODING_BIPHASE_LEVEL:
5022 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 + BIT8; break; 5022 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 | BIT8; break;
5023 } 5023 }
5024 } 5024 }
5025 5025
@@ -5056,8 +5056,8 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
5056 /* enable Master Interrupt Enable bit (MIE) */ 5056 /* enable Master Interrupt Enable bit (MIE) */
5057 usc_EnableMasterIrqBit( info ); 5057 usc_EnableMasterIrqBit( info );
5058 5058
5059 usc_ClearIrqPendingBits( info, RECEIVE_STATUS + RECEIVE_DATA + 5059 usc_ClearIrqPendingBits( info, RECEIVE_STATUS | RECEIVE_DATA |
5060 TRANSMIT_STATUS + TRANSMIT_DATA + MISC); 5060 TRANSMIT_STATUS | TRANSMIT_DATA | MISC);
5061 5061
5062 /* arm RCC underflow interrupt */ 5062 /* arm RCC underflow interrupt */
5063 usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3)); 5063 usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3));
@@ -5175,14 +5175,14 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
5175 switch ( info->params.preamble_length ) { 5175 switch ( info->params.preamble_length ) {
5176 case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break; 5176 case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break;
5177 case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break; 5177 case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break;
5178 case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 + BIT10; break; 5178 case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 | BIT10; break;
5179 } 5179 }
5180 5180
5181 switch ( info->params.preamble ) { 5181 switch ( info->params.preamble ) {
5182 case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 + BIT12; break; 5182 case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 | BIT12; break;
5183 case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break; 5183 case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break;
5184 case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break; 5184 case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break;
5185 case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 + BIT8; break; 5185 case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 | BIT8; break;
5186 } 5186 }
5187 5187
5188 usc_OutReg( info, CCR, RegValue ); 5188 usc_OutReg( info, CCR, RegValue );
@@ -5221,7 +5221,7 @@ static void usc_enable_loopback(struct mgsl_struct *info, int enable)
5221{ 5221{
5222 if (enable) { 5222 if (enable) {
5223 /* blank external TXD output */ 5223 /* blank external TXD output */
5224 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7+BIT6)); 5224 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7 | BIT6));
5225 5225
5226 /* Clock mode Control Register (CMCR) 5226 /* Clock mode Control Register (CMCR)
5227 * 5227 *
@@ -5260,7 +5260,7 @@ static void usc_enable_loopback(struct mgsl_struct *info, int enable)
5260 outw( 0x0300, info->io_base + CCAR ); 5260 outw( 0x0300, info->io_base + CCAR );
5261 } else { 5261 } else {
5262 /* enable external TXD output */ 5262 /* enable external TXD output */
5263 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7+BIT6)); 5263 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7 | BIT6));
5264 5264
5265 /* clear Internal Data loopback mode */ 5265 /* clear Internal Data loopback mode */
5266 info->loopback_bits = 0; 5266 info->loopback_bits = 0;
@@ -5447,13 +5447,13 @@ static void usc_process_rxoverrun_sync( struct mgsl_struct *info )
5447 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) ); 5447 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5448 5448
5449 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL ); 5449 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5450 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS ); 5450 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
5451 usc_EnableInterrupts( info, RECEIVE_STATUS ); 5451 usc_EnableInterrupts( info, RECEIVE_STATUS );
5452 5452
5453 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */ 5453 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5454 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */ 5454 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5455 5455
5456 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 ); 5456 usc_OutDmaReg( info, RDIAR, BIT3 | BIT2 );
5457 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) ); 5457 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5458 usc_DmaCmd( info, DmaCmd_InitRxChannel ); 5458 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5459 if ( info->params.flags & HDLC_FLAG_AUTO_DCD ) 5459 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
@@ -5488,8 +5488,8 @@ static void usc_stop_receiver( struct mgsl_struct *info )
5488 usc_DmaCmd( info, DmaCmd_ResetRxChannel ); 5488 usc_DmaCmd( info, DmaCmd_ResetRxChannel );
5489 5489
5490 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL ); 5490 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5491 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS ); 5491 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
5492 usc_DisableInterrupts( info, RECEIVE_DATA + RECEIVE_STATUS ); 5492 usc_DisableInterrupts( info, RECEIVE_DATA | RECEIVE_STATUS );
5493 5493
5494 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL); 5494 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5495 5495
@@ -5536,13 +5536,13 @@ static void usc_start_receiver( struct mgsl_struct *info )
5536 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) ); 5536 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5537 5537
5538 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL ); 5538 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5539 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS ); 5539 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
5540 usc_EnableInterrupts( info, RECEIVE_STATUS ); 5540 usc_EnableInterrupts( info, RECEIVE_STATUS );
5541 5541
5542 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */ 5542 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5543 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */ 5543 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5544 5544
5545 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 ); 5545 usc_OutDmaReg( info, RDIAR, BIT3 | BIT2 );
5546 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) ); 5546 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5547 usc_DmaCmd( info, DmaCmd_InitRxChannel ); 5547 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5548 if ( info->params.flags & HDLC_FLAG_AUTO_DCD ) 5548 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
@@ -5551,7 +5551,7 @@ static void usc_start_receiver( struct mgsl_struct *info )
5551 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL); 5551 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5552 } else { 5552 } else {
5553 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL); 5553 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
5554 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS); 5554 usc_ClearIrqPendingBits(info, RECEIVE_DATA | RECEIVE_STATUS);
5555 usc_EnableInterrupts(info, RECEIVE_DATA); 5555 usc_EnableInterrupts(info, RECEIVE_DATA);
5556 5556
5557 usc_RTCmd( info, RTCmd_PurgeRxFifo ); 5557 usc_RTCmd( info, RTCmd_PurgeRxFifo );
@@ -5925,7 +5925,7 @@ static void usc_set_async_mode( struct mgsl_struct *info )
5925 RegValue = 0; 5925 RegValue = 0;
5926 5926
5927 if ( info->params.data_bits != 8 ) 5927 if ( info->params.data_bits != 8 )
5928 RegValue |= BIT4+BIT3+BIT2; 5928 RegValue |= BIT4 | BIT3 | BIT2;
5929 5929
5930 if ( info->params.parity != ASYNC_PARITY_NONE ) { 5930 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5931 RegValue |= BIT5; 5931 RegValue |= BIT5;
@@ -5982,7 +5982,7 @@ static void usc_set_async_mode( struct mgsl_struct *info )
5982 RegValue = 0; 5982 RegValue = 0;
5983 5983
5984 if ( info->params.data_bits != 8 ) 5984 if ( info->params.data_bits != 8 )
5985 RegValue |= BIT4+BIT3+BIT2; 5985 RegValue |= BIT4 | BIT3 | BIT2;
5986 5986
5987 if ( info->params.parity != ASYNC_PARITY_NONE ) { 5987 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5988 RegValue |= BIT5; 5988 RegValue |= BIT5;
@@ -6129,7 +6129,7 @@ static void usc_loopback_frame( struct mgsl_struct *info )
6129 6129
6130 /* WAIT FOR RECEIVE COMPLETE */ 6130 /* WAIT FOR RECEIVE COMPLETE */
6131 for (i=0 ; i<1000 ; i++) 6131 for (i=0 ; i<1000 ; i++)
6132 if (usc_InReg( info, RCSR ) & (BIT8 + BIT4 + BIT3 + BIT1)) 6132 if (usc_InReg( info, RCSR ) & (BIT8 | BIT4 | BIT3 | BIT1))
6133 break; 6133 break;
6134 6134
6135 /* clear Internal Data loopback mode */ 6135 /* clear Internal Data loopback mode */
@@ -6579,8 +6579,8 @@ static bool mgsl_get_rx_frame(struct mgsl_struct *info)
6579 6579
6580 status = info->rx_buffer_list[EndIndex].status; 6580 status = info->rx_buffer_list[EndIndex].status;
6581 6581
6582 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN + 6582 if ( status & (RXSTATUS_SHORT_FRAME | RXSTATUS_OVERRUN |
6583 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) { 6583 RXSTATUS_CRC_ERROR | RXSTATUS_ABORT) ) {
6584 if ( status & RXSTATUS_SHORT_FRAME ) 6584 if ( status & RXSTATUS_SHORT_FRAME )
6585 info->icount.rxshort++; 6585 info->icount.rxshort++;
6586 else if ( status & RXSTATUS_ABORT ) 6586 else if ( status & RXSTATUS_ABORT )
@@ -6762,8 +6762,8 @@ static bool mgsl_get_raw_rx_frame(struct mgsl_struct *info)
6762 6762
6763 status = info->rx_buffer_list[CurrentIndex].status; 6763 status = info->rx_buffer_list[CurrentIndex].status;
6764 6764
6765 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN + 6765 if ( status & (RXSTATUS_SHORT_FRAME | RXSTATUS_OVERRUN |
6766 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) { 6766 RXSTATUS_CRC_ERROR | RXSTATUS_ABORT) ) {
6767 if ( status & RXSTATUS_SHORT_FRAME ) 6767 if ( status & RXSTATUS_SHORT_FRAME )
6768 info->icount.rxshort++; 6768 info->icount.rxshort++;
6769 else if ( status & RXSTATUS_ABORT ) 6769 else if ( status & RXSTATUS_ABORT )
@@ -6899,7 +6899,7 @@ static void mgsl_load_tx_dma_buffer(struct mgsl_struct *info,
6899 /* set CMR:13 to start transmit when 6899 /* set CMR:13 to start transmit when
6900 * next GoAhead (abort) is received 6900 * next GoAhead (abort) is received
6901 */ 6901 */
6902 info->cmr_value |= BIT13; 6902 info->cmr_value |= BIT13;
6903 } 6903 }
6904 6904
6905 /* begin loading the frame in the next available tx dma 6905 /* begin loading the frame in the next available tx dma
@@ -7278,7 +7278,7 @@ static bool mgsl_dma_test( struct mgsl_struct *info )
7278 7278
7279 spin_unlock_irqrestore(&info->irq_spinlock,flags); 7279 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7280 7280
7281 7281
7282 /******************************/ 7282 /******************************/
7283 /* WAIT FOR TRANSMIT COMPLETE */ 7283 /* WAIT FOR TRANSMIT COMPLETE */
7284 /******************************/ 7284 /******************************/
@@ -7292,7 +7292,7 @@ static bool mgsl_dma_test( struct mgsl_struct *info )
7292 status = usc_InReg( info, TCSR ); 7292 status = usc_InReg( info, TCSR );
7293 spin_unlock_irqrestore(&info->irq_spinlock,flags); 7293 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7294 7294
7295 while ( !(status & (BIT6+BIT5+BIT4+BIT2+BIT1)) ) { 7295 while ( !(status & (BIT6 | BIT5 | BIT4 | BIT2 | BIT1)) ) {
7296 if (time_after(jiffies, EndTime)) { 7296 if (time_after(jiffies, EndTime)) {
7297 rc = false; 7297 rc = false;
7298 break; 7298 break;
@@ -7307,7 +7307,7 @@ static bool mgsl_dma_test( struct mgsl_struct *info )
7307 7307
7308 if ( rc ){ 7308 if ( rc ){
7309 /* CHECK FOR TRANSMIT ERRORS */ 7309 /* CHECK FOR TRANSMIT ERRORS */
7310 if ( status & (BIT5 + BIT1) ) 7310 if ( status & (BIT5 | BIT1) )
7311 rc = false; 7311 rc = false;
7312 } 7312 }
7313 7313
@@ -7333,7 +7333,7 @@ static bool mgsl_dma_test( struct mgsl_struct *info )
7333 /* CHECK FOR RECEIVE ERRORS */ 7333 /* CHECK FOR RECEIVE ERRORS */
7334 status = info->rx_buffer_list[0].status; 7334 status = info->rx_buffer_list[0].status;
7335 7335
7336 if ( status & (BIT8 + BIT3 + BIT1) ) { 7336 if ( status & (BIT8 | BIT3 | BIT1) ) {
7337 /* receive error has occurred */ 7337 /* receive error has occurred */
7338 rc = false; 7338 rc = false;
7339 } else { 7339 } else {
@@ -7605,7 +7605,7 @@ static void usc_loopmode_send_done( struct mgsl_struct * info )
7605{ 7605{
7606 info->loopmode_send_done_requested = false; 7606 info->loopmode_send_done_requested = false;
7607 /* clear CMR:13 to 0 to start echoing RxData to TxData */ 7607 /* clear CMR:13 to 0 to start echoing RxData to TxData */
7608 info->cmr_value &= ~BIT13; 7608 info->cmr_value &= ~BIT13;
7609 usc_OutReg(info, CMR, info->cmr_value); 7609 usc_OutReg(info, CMR, info->cmr_value);
7610} 7610}
7611 7611
diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c
index 9121c1f7aeef..c043136fbe51 100644
--- a/drivers/tty/tty_buffer.c
+++ b/drivers/tty/tty_buffer.c
@@ -18,31 +18,118 @@
18#include <linux/module.h> 18#include <linux/module.h>
19#include <linux/ratelimit.h> 19#include <linux/ratelimit.h>
20 20
21
22#define MIN_TTYB_SIZE 256
23#define TTYB_ALIGN_MASK 255
24
25/*
26 * Byte threshold to limit memory consumption for flip buffers.
27 * The actual memory limit is > 2x this amount.
28 */
29#define TTYB_MEM_LIMIT 65536
30
31/*
32 * We default to dicing tty buffer allocations to this many characters
33 * in order to avoid multiple page allocations. We know the size of
34 * tty_buffer itself but it must also be taken into account that the
35 * the buffer is 256 byte aligned. See tty_buffer_find for the allocation
36 * logic this must match
37 */
38
39#define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
40
41
42/**
43 * tty_buffer_lock_exclusive - gain exclusive access to buffer
44 * tty_buffer_unlock_exclusive - release exclusive access
45 *
46 * @port - tty_port owning the flip buffer
47 *
48 * Guarantees safe use of the line discipline's receive_buf() method by
49 * excluding the buffer work and any pending flush from using the flip
50 * buffer. Data can continue to be added concurrently to the flip buffer
51 * from the driver side.
52 *
53 * On release, the buffer work is restarted if there is data in the
54 * flip buffer
55 */
56
57void tty_buffer_lock_exclusive(struct tty_port *port)
58{
59 struct tty_bufhead *buf = &port->buf;
60
61 atomic_inc(&buf->priority);
62 mutex_lock(&buf->lock);
63}
64
65void tty_buffer_unlock_exclusive(struct tty_port *port)
66{
67 struct tty_bufhead *buf = &port->buf;
68 int restart;
69
70 restart = buf->head->commit != buf->head->read;
71
72 atomic_dec(&buf->priority);
73 mutex_unlock(&buf->lock);
74 if (restart)
75 queue_work(system_unbound_wq, &buf->work);
76}
77
78/**
79 * tty_buffer_space_avail - return unused buffer space
80 * @port - tty_port owning the flip buffer
81 *
82 * Returns the # of bytes which can be written by the driver without
83 * reaching the buffer limit.
84 *
85 * Note: this does not guarantee that memory is available to write
86 * the returned # of bytes (use tty_prepare_flip_string_xxx() to
87 * pre-allocate if memory guarantee is required).
88 */
89
90int tty_buffer_space_avail(struct tty_port *port)
91{
92 int space = TTYB_MEM_LIMIT - atomic_read(&port->buf.memory_used);
93 return max(space, 0);
94}
95
96static void tty_buffer_reset(struct tty_buffer *p, size_t size)
97{
98 p->used = 0;
99 p->size = size;
100 p->next = NULL;
101 p->commit = 0;
102 p->read = 0;
103}
104
21/** 105/**
22 * tty_buffer_free_all - free buffers used by a tty 106 * tty_buffer_free_all - free buffers used by a tty
23 * @tty: tty to free from 107 * @tty: tty to free from
24 * 108 *
25 * Remove all the buffers pending on a tty whether queued with data 109 * Remove all the buffers pending on a tty whether queued with data
26 * or in the free ring. Must be called when the tty is no longer in use 110 * or in the free ring. Must be called when the tty is no longer in use
27 *
28 * Locking: none
29 */ 111 */
30 112
31void tty_buffer_free_all(struct tty_port *port) 113void tty_buffer_free_all(struct tty_port *port)
32{ 114{
33 struct tty_bufhead *buf = &port->buf; 115 struct tty_bufhead *buf = &port->buf;
34 struct tty_buffer *thead; 116 struct tty_buffer *p, *next;
117 struct llist_node *llist;
35 118
36 while ((thead = buf->head) != NULL) { 119 while ((p = buf->head) != NULL) {
37 buf->head = thead->next; 120 buf->head = p->next;
38 kfree(thead); 121 if (p->size > 0)
39 } 122 kfree(p);
40 while ((thead = buf->free) != NULL) {
41 buf->free = thead->next;
42 kfree(thead);
43 } 123 }
44 buf->tail = NULL; 124 llist = llist_del_all(&buf->free);
45 buf->memory_used = 0; 125 llist_for_each_entry_safe(p, next, llist, free)
126 kfree(p);
127
128 tty_buffer_reset(&buf->sentinel, 0);
129 buf->head = &buf->sentinel;
130 buf->tail = &buf->sentinel;
131
132 atomic_set(&buf->memory_used, 0);
46} 133}
47 134
48/** 135/**
@@ -51,29 +138,39 @@ void tty_buffer_free_all(struct tty_port *port)
51 * @size: desired size (characters) 138 * @size: desired size (characters)
52 * 139 *
53 * Allocate a new tty buffer to hold the desired number of characters. 140 * Allocate a new tty buffer to hold the desired number of characters.
141 * We round our buffers off in 256 character chunks to get better
142 * allocation behaviour.
54 * Return NULL if out of memory or the allocation would exceed the 143 * Return NULL if out of memory or the allocation would exceed the
55 * per device queue 144 * per device queue
56 *
57 * Locking: Caller must hold tty->buf.lock
58 */ 145 */
59 146
60static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size) 147static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
61{ 148{
149 struct llist_node *free;
62 struct tty_buffer *p; 150 struct tty_buffer *p;
63 151
64 if (port->buf.memory_used + size > 65536) 152 /* Round the buffer size out */
153 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
154
155 if (size <= MIN_TTYB_SIZE) {
156 free = llist_del_first(&port->buf.free);
157 if (free) {
158 p = llist_entry(free, struct tty_buffer, free);
159 goto found;
160 }
161 }
162
163 /* Should possibly check if this fails for the largest buffer we
164 have queued and recycle that ? */
165 if (atomic_read(&port->buf.memory_used) > TTYB_MEM_LIMIT)
65 return NULL; 166 return NULL;
66 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC); 167 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
67 if (p == NULL) 168 if (p == NULL)
68 return NULL; 169 return NULL;
69 p->used = 0; 170
70 p->size = size; 171found:
71 p->next = NULL; 172 tty_buffer_reset(p, size);
72 p->commit = 0; 173 atomic_add(size, &port->buf.memory_used);
73 p->read = 0;
74 p->char_buf_ptr = (char *)(p->data);
75 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
76 port->buf.memory_used += size;
77 return p; 174 return p;
78} 175}
79 176
@@ -84,8 +181,6 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
84 * 181 *
85 * Free a tty buffer, or add it to the free list according to our 182 * Free a tty buffer, or add it to the free list according to our
86 * internal strategy 183 * internal strategy
87 *
88 * Locking: Caller must hold tty->buf.lock
89 */ 184 */
90 185
91static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b) 186static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
@@ -93,41 +188,12 @@ static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
93 struct tty_bufhead *buf = &port->buf; 188 struct tty_bufhead *buf = &port->buf;
94 189
95 /* Dumb strategy for now - should keep some stats */ 190 /* Dumb strategy for now - should keep some stats */
96 buf->memory_used -= b->size; 191 WARN_ON(atomic_sub_return(b->size, &buf->memory_used) < 0);
97 WARN_ON(buf->memory_used < 0);
98 192
99 if (b->size >= 512) 193 if (b->size > MIN_TTYB_SIZE)
100 kfree(b); 194 kfree(b);
101 else { 195 else if (b->size > 0)
102 b->next = buf->free; 196 llist_add(&b->free, &buf->free);
103 buf->free = b;
104 }
105}
106
107/**
108 * __tty_buffer_flush - flush full tty buffers
109 * @tty: tty to flush
110 *
111 * flush all the buffers containing receive data. Caller must
112 * hold the buffer lock and must have ensured no parallel flush to
113 * ldisc is running.
114 *
115 * Locking: Caller must hold tty->buf.lock
116 */
117
118static void __tty_buffer_flush(struct tty_port *port)
119{
120 struct tty_bufhead *buf = &port->buf;
121 struct tty_buffer *thead;
122
123 if (unlikely(buf->head == NULL))
124 return;
125 while ((thead = buf->head->next) != NULL) {
126 tty_buffer_free(port, buf->head);
127 buf->head = thead;
128 }
129 WARN_ON(buf->head != buf->tail);
130 buf->head->read = buf->head->commit;
131} 197}
132 198
133/** 199/**
@@ -138,65 +204,28 @@ static void __tty_buffer_flush(struct tty_port *port)
138 * being processed by flush_to_ldisc then we defer the processing 204 * being processed by flush_to_ldisc then we defer the processing
139 * to that function 205 * to that function
140 * 206 *
141 * Locking: none 207 * Locking: takes buffer lock to ensure single-threaded flip buffer
208 * 'consumer'
142 */ 209 */
143 210
144void tty_buffer_flush(struct tty_struct *tty) 211void tty_buffer_flush(struct tty_struct *tty)
145{ 212{
146 struct tty_port *port = tty->port; 213 struct tty_port *port = tty->port;
147 struct tty_bufhead *buf = &port->buf; 214 struct tty_bufhead *buf = &port->buf;
148 unsigned long flags; 215 struct tty_buffer *next;
149
150 spin_lock_irqsave(&buf->lock, flags);
151
152 /* If the data is being pushed to the tty layer then we can't
153 process it here. Instead set a flag and the flush_to_ldisc
154 path will process the flush request before it exits */
155 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
156 set_bit(TTYP_FLUSHPENDING, &port->iflags);
157 spin_unlock_irqrestore(&buf->lock, flags);
158 wait_event(tty->read_wait,
159 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
160 return;
161 } else
162 __tty_buffer_flush(port);
163 spin_unlock_irqrestore(&buf->lock, flags);
164}
165 216
166/** 217 atomic_inc(&buf->priority);
167 * tty_buffer_find - find a free tty buffer
168 * @tty: tty owning the buffer
169 * @size: characters wanted
170 *
171 * Locate an existing suitable tty buffer or if we are lacking one then
172 * allocate a new one. We round our buffers off in 256 character chunks
173 * to get better allocation behaviour.
174 *
175 * Locking: Caller must hold tty->buf.lock
176 */
177 218
178static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size) 219 mutex_lock(&buf->lock);
179{ 220 while ((next = buf->head->next) != NULL) {
180 struct tty_buffer **tbh = &port->buf.free; 221 tty_buffer_free(port, buf->head);
181 while ((*tbh) != NULL) { 222 buf->head = next;
182 struct tty_buffer *t = *tbh;
183 if (t->size >= size) {
184 *tbh = t->next;
185 t->next = NULL;
186 t->used = 0;
187 t->commit = 0;
188 t->read = 0;
189 port->buf.memory_used += t->size;
190 return t;
191 }
192 tbh = &((*tbh)->next);
193 } 223 }
194 /* Round the buffer size out */ 224 buf->head->read = buf->head->commit;
195 size = (size + 0xFF) & ~0xFF; 225 atomic_dec(&buf->priority);
196 return tty_buffer_alloc(port, size); 226 mutex_unlock(&buf->lock);
197 /* Should possibly check if this fails for the largest buffer we
198 have queued and recycle that ? */
199} 227}
228
200/** 229/**
201 * tty_buffer_request_room - grow tty buffer if needed 230 * tty_buffer_request_room - grow tty buffer if needed
202 * @tty: tty structure 231 * @tty: tty structure
@@ -204,38 +233,26 @@ static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size)
204 * 233 *
205 * Make at least size bytes of linear space available for the tty 234 * Make at least size bytes of linear space available for the tty
206 * buffer. If we fail return the size we managed to find. 235 * buffer. If we fail return the size we managed to find.
207 *
208 * Locking: Takes port->buf.lock
209 */ 236 */
210int tty_buffer_request_room(struct tty_port *port, size_t size) 237int tty_buffer_request_room(struct tty_port *port, size_t size)
211{ 238{
212 struct tty_bufhead *buf = &port->buf; 239 struct tty_bufhead *buf = &port->buf;
213 struct tty_buffer *b, *n; 240 struct tty_buffer *b, *n;
214 int left; 241 int left;
215 unsigned long flags; 242
216 spin_lock_irqsave(&buf->lock, flags);
217 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
218 remove this conditional if its worth it. This would be invisible
219 to the callers */
220 b = buf->tail; 243 b = buf->tail;
221 if (b != NULL) 244 left = b->size - b->used;
222 left = b->size - b->used;
223 else
224 left = 0;
225 245
226 if (left < size) { 246 if (left < size) {
227 /* This is the slow path - looking for new buffers to use */ 247 /* This is the slow path - looking for new buffers to use */
228 if ((n = tty_buffer_find(port, size)) != NULL) { 248 if ((n = tty_buffer_alloc(port, size)) != NULL) {
229 if (b != NULL) {
230 b->next = n;
231 b->commit = b->used;
232 } else
233 buf->head = n;
234 buf->tail = n; 249 buf->tail = n;
250 b->commit = b->used;
251 smp_mb();
252 b->next = n;
235 } else 253 } else
236 size = left; 254 size = left;
237 } 255 }
238 spin_unlock_irqrestore(&buf->lock, flags);
239 return size; 256 return size;
240} 257}
241EXPORT_SYMBOL_GPL(tty_buffer_request_room); 258EXPORT_SYMBOL_GPL(tty_buffer_request_room);
@@ -249,8 +266,6 @@ EXPORT_SYMBOL_GPL(tty_buffer_request_room);
249 * 266 *
250 * Queue a series of bytes to the tty buffering. All the characters 267 * Queue a series of bytes to the tty buffering. All the characters
251 * passed are marked with the supplied flag. Returns the number added. 268 * passed are marked with the supplied flag. Returns the number added.
252 *
253 * Locking: Called functions may take port->buf.lock
254 */ 269 */
255 270
256int tty_insert_flip_string_fixed_flag(struct tty_port *port, 271int tty_insert_flip_string_fixed_flag(struct tty_port *port,
@@ -261,12 +276,10 @@ int tty_insert_flip_string_fixed_flag(struct tty_port *port,
261 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); 276 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
262 int space = tty_buffer_request_room(port, goal); 277 int space = tty_buffer_request_room(port, goal);
263 struct tty_buffer *tb = port->buf.tail; 278 struct tty_buffer *tb = port->buf.tail;
264 /* If there is no space then tb may be NULL */ 279 if (unlikely(space == 0))
265 if (unlikely(space == 0)) {
266 break; 280 break;
267 } 281 memcpy(char_buf_ptr(tb, tb->used), chars, space);
268 memcpy(tb->char_buf_ptr + tb->used, chars, space); 282 memset(flag_buf_ptr(tb, tb->used), flag, space);
269 memset(tb->flag_buf_ptr + tb->used, flag, space);
270 tb->used += space; 283 tb->used += space;
271 copied += space; 284 copied += space;
272 chars += space; 285 chars += space;
@@ -287,8 +300,6 @@ EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
287 * Queue a series of bytes to the tty buffering. For each character 300 * Queue a series of bytes to the tty buffering. For each character
288 * the flags array indicates the status of the character. Returns the 301 * the flags array indicates the status of the character. Returns the
289 * number added. 302 * number added.
290 *
291 * Locking: Called functions may take port->buf.lock
292 */ 303 */
293 304
294int tty_insert_flip_string_flags(struct tty_port *port, 305int tty_insert_flip_string_flags(struct tty_port *port,
@@ -299,12 +310,10 @@ int tty_insert_flip_string_flags(struct tty_port *port,
299 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); 310 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
300 int space = tty_buffer_request_room(port, goal); 311 int space = tty_buffer_request_room(port, goal);
301 struct tty_buffer *tb = port->buf.tail; 312 struct tty_buffer *tb = port->buf.tail;
302 /* If there is no space then tb may be NULL */ 313 if (unlikely(space == 0))
303 if (unlikely(space == 0)) {
304 break; 314 break;
305 } 315 memcpy(char_buf_ptr(tb, tb->used), chars, space);
306 memcpy(tb->char_buf_ptr + tb->used, chars, space); 316 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
307 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
308 tb->used += space; 317 tb->used += space;
309 copied += space; 318 copied += space;
310 chars += space; 319 chars += space;
@@ -325,20 +334,14 @@ EXPORT_SYMBOL(tty_insert_flip_string_flags);
325 * processing by the line discipline. 334 * processing by the line discipline.
326 * Note that this function can only be used when the low_latency flag 335 * Note that this function can only be used when the low_latency flag
327 * is unset. Otherwise the workqueue won't be flushed. 336 * is unset. Otherwise the workqueue won't be flushed.
328 *
329 * Locking: Takes port->buf.lock
330 */ 337 */
331 338
332void tty_schedule_flip(struct tty_port *port) 339void tty_schedule_flip(struct tty_port *port)
333{ 340{
334 struct tty_bufhead *buf = &port->buf; 341 struct tty_bufhead *buf = &port->buf;
335 unsigned long flags;
336 WARN_ON(port->low_latency); 342 WARN_ON(port->low_latency);
337 343
338 spin_lock_irqsave(&buf->lock, flags); 344 buf->tail->commit = buf->tail->used;
339 if (buf->tail != NULL)
340 buf->tail->commit = buf->tail->used;
341 spin_unlock_irqrestore(&buf->lock, flags);
342 schedule_work(&buf->work); 345 schedule_work(&buf->work);
343} 346}
344EXPORT_SYMBOL(tty_schedule_flip); 347EXPORT_SYMBOL(tty_schedule_flip);
@@ -354,8 +357,6 @@ EXPORT_SYMBOL(tty_schedule_flip);
354 * accounted for as ready for normal characters. This is used for drivers 357 * accounted for as ready for normal characters. This is used for drivers
355 * that need their own block copy routines into the buffer. There is no 358 * that need their own block copy routines into the buffer. There is no
356 * guarantee the buffer is a DMA target! 359 * guarantee the buffer is a DMA target!
357 *
358 * Locking: May call functions taking port->buf.lock
359 */ 360 */
360 361
361int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars, 362int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
@@ -364,8 +365,8 @@ int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
364 int space = tty_buffer_request_room(port, size); 365 int space = tty_buffer_request_room(port, size);
365 if (likely(space)) { 366 if (likely(space)) {
366 struct tty_buffer *tb = port->buf.tail; 367 struct tty_buffer *tb = port->buf.tail;
367 *chars = tb->char_buf_ptr + tb->used; 368 *chars = char_buf_ptr(tb, tb->used);
368 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); 369 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
369 tb->used += space; 370 tb->used += space;
370 } 371 }
371 return space; 372 return space;
@@ -384,8 +385,6 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
384 * accounted for as ready for characters. This is used for drivers 385 * accounted for as ready for characters. This is used for drivers
385 * that need their own block copy routines into the buffer. There is no 386 * that need their own block copy routines into the buffer. There is no
386 * guarantee the buffer is a DMA target! 387 * guarantee the buffer is a DMA target!
387 *
388 * Locking: May call functions taking port->buf.lock
389 */ 388 */
390 389
391int tty_prepare_flip_string_flags(struct tty_port *port, 390int tty_prepare_flip_string_flags(struct tty_port *port,
@@ -394,8 +393,8 @@ int tty_prepare_flip_string_flags(struct tty_port *port,
394 int space = tty_buffer_request_room(port, size); 393 int space = tty_buffer_request_room(port, size);
395 if (likely(space)) { 394 if (likely(space)) {
396 struct tty_buffer *tb = port->buf.tail; 395 struct tty_buffer *tb = port->buf.tail;
397 *chars = tb->char_buf_ptr + tb->used; 396 *chars = char_buf_ptr(tb, tb->used);
398 *flags = tb->flag_buf_ptr + tb->used; 397 *flags = flag_buf_ptr(tb, tb->used);
399 tb->used += space; 398 tb->used += space;
400 } 399 }
401 return space; 400 return space;
@@ -403,6 +402,23 @@ int tty_prepare_flip_string_flags(struct tty_port *port,
403EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); 402EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
404 403
405 404
405static int
406receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
407{
408 struct tty_ldisc *disc = tty->ldisc;
409 unsigned char *p = char_buf_ptr(head, head->read);
410 char *f = flag_buf_ptr(head, head->read);
411
412 if (disc->ops->receive_buf2)
413 count = disc->ops->receive_buf2(tty, p, f, count);
414 else {
415 count = min_t(int, count, tty->receive_room);
416 if (count)
417 disc->ops->receive_buf(tty, p, f, count);
418 }
419 head->read += count;
420 return count;
421}
406 422
407/** 423/**
408 * flush_to_ldisc 424 * flush_to_ldisc
@@ -411,9 +427,10 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
411 * This routine is called out of the software interrupt to flush data 427 * This routine is called out of the software interrupt to flush data
412 * from the buffer chain to the line discipline. 428 * from the buffer chain to the line discipline.
413 * 429 *
414 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock 430 * The receive_buf method is single threaded for each tty instance.
415 * while invoking the line discipline receive_buf method. The 431 *
416 * receive_buf method is single threaded for each tty instance. 432 * Locking: takes buffer lock to ensure single-threaded flip buffer
433 * 'consumer'
417 */ 434 */
418 435
419static void flush_to_ldisc(struct work_struct *work) 436static void flush_to_ldisc(struct work_struct *work)
@@ -421,7 +438,6 @@ static void flush_to_ldisc(struct work_struct *work)
421 struct tty_port *port = container_of(work, struct tty_port, buf.work); 438 struct tty_port *port = container_of(work, struct tty_port, buf.work);
422 struct tty_bufhead *buf = &port->buf; 439 struct tty_bufhead *buf = &port->buf;
423 struct tty_struct *tty; 440 struct tty_struct *tty;
424 unsigned long flags;
425 struct tty_ldisc *disc; 441 struct tty_ldisc *disc;
426 442
427 tty = port->itty; 443 tty = port->itty;
@@ -429,52 +445,34 @@ static void flush_to_ldisc(struct work_struct *work)
429 return; 445 return;
430 446
431 disc = tty_ldisc_ref(tty); 447 disc = tty_ldisc_ref(tty);
432 if (disc == NULL) /* !TTY_LDISC */ 448 if (disc == NULL)
433 return; 449 return;
434 450
435 spin_lock_irqsave(&buf->lock, flags); 451 mutex_lock(&buf->lock);
436 452
437 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) { 453 while (1) {
438 struct tty_buffer *head; 454 struct tty_buffer *head = buf->head;
439 while ((head = buf->head) != NULL) { 455 int count;
440 int count; 456
441 char *char_buf; 457 /* Ldisc or user is trying to gain exclusive access */
442 unsigned char *flag_buf; 458 if (atomic_read(&buf->priority))
443 459 break;
444 count = head->commit - head->read; 460
445 if (!count) { 461 count = head->commit - head->read;
446 if (head->next == NULL) 462 if (!count) {
447 break; 463 if (head->next == NULL)
448 buf->head = head->next;
449 tty_buffer_free(port, head);
450 continue;
451 }
452 if (!tty->receive_room)
453 break;
454 if (count > tty->receive_room)
455 count = tty->receive_room;
456 char_buf = head->char_buf_ptr + head->read;
457 flag_buf = head->flag_buf_ptr + head->read;
458 head->read += count;
459 spin_unlock_irqrestore(&buf->lock, flags);
460 disc->ops->receive_buf(tty, char_buf,
461 flag_buf, count);
462 spin_lock_irqsave(&buf->lock, flags);
463 /* Ldisc or user is trying to flush the buffers.
464 We may have a deferred request to flush the
465 input buffer, if so pull the chain under the lock
466 and empty the queue */
467 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
468 __tty_buffer_flush(port);
469 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
470 wake_up(&tty->read_wait);
471 break; 464 break;
472 } 465 buf->head = head->next;
466 tty_buffer_free(port, head);
467 continue;
473 } 468 }
474 clear_bit(TTYP_FLUSHING, &port->iflags); 469
470 count = receive_buf(tty, head, count);
471 if (!count)
472 break;
475 } 473 }
476 474
477 spin_unlock_irqrestore(&buf->lock, flags); 475 mutex_unlock(&buf->lock);
478 476
479 tty_ldisc_deref(disc); 477 tty_ldisc_deref(disc);
480} 478}
@@ -503,19 +501,13 @@ void tty_flush_to_ldisc(struct tty_struct *tty)
503 * 501 *
504 * In the event of the queue being busy for flipping the work will be 502 * In the event of the queue being busy for flipping the work will be
505 * held off and retried later. 503 * held off and retried later.
506 *
507 * Locking: tty buffer lock. Driver locks in low latency mode.
508 */ 504 */
509 505
510void tty_flip_buffer_push(struct tty_port *port) 506void tty_flip_buffer_push(struct tty_port *port)
511{ 507{
512 struct tty_bufhead *buf = &port->buf; 508 struct tty_bufhead *buf = &port->buf;
513 unsigned long flags;
514 509
515 spin_lock_irqsave(&buf->lock, flags); 510 buf->tail->commit = buf->tail->used;
516 if (buf->tail != NULL)
517 buf->tail->commit = buf->tail->used;
518 spin_unlock_irqrestore(&buf->lock, flags);
519 511
520 if (port->low_latency) 512 if (port->low_latency)
521 flush_to_ldisc(&buf->work); 513 flush_to_ldisc(&buf->work);
@@ -530,19 +522,18 @@ EXPORT_SYMBOL(tty_flip_buffer_push);
530 * 522 *
531 * Set up the initial state of the buffer management for a tty device. 523 * Set up the initial state of the buffer management for a tty device.
532 * Must be called before the other tty buffer functions are used. 524 * Must be called before the other tty buffer functions are used.
533 *
534 * Locking: none
535 */ 525 */
536 526
537void tty_buffer_init(struct tty_port *port) 527void tty_buffer_init(struct tty_port *port)
538{ 528{
539 struct tty_bufhead *buf = &port->buf; 529 struct tty_bufhead *buf = &port->buf;
540 530
541 spin_lock_init(&buf->lock); 531 mutex_init(&buf->lock);
542 buf->head = NULL; 532 tty_buffer_reset(&buf->sentinel, 0);
543 buf->tail = NULL; 533 buf->head = &buf->sentinel;
544 buf->free = NULL; 534 buf->tail = &buf->sentinel;
545 buf->memory_used = 0; 535 init_llist_head(&buf->free);
536 atomic_set(&buf->memory_used, 0);
537 atomic_set(&buf->priority, 0);
546 INIT_WORK(&buf->work, flush_to_ldisc); 538 INIT_WORK(&buf->work, flush_to_ldisc);
547} 539}
548
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 366af832794b..a9355ce1c6d5 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -603,8 +603,8 @@ static int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
603 * BTM 603 * BTM
604 * redirect lock for undoing redirection 604 * redirect lock for undoing redirection
605 * file list lock for manipulating list of ttys 605 * file list lock for manipulating list of ttys
606 * tty_ldisc_lock from called functions 606 * tty_ldiscs_lock from called functions
607 * termios_mutex resetting termios data 607 * termios_rwsem resetting termios data
608 * tasklist_lock to walk task list for hangup event 608 * tasklist_lock to walk task list for hangup event
609 * ->siglock to protect ->signal/->sighand 609 * ->siglock to protect ->signal/->sighand
610 */ 610 */
@@ -629,6 +629,11 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
629 629
630 tty_lock(tty); 630 tty_lock(tty);
631 631
632 if (test_bit(TTY_HUPPED, &tty->flags)) {
633 tty_unlock(tty);
634 return;
635 }
636
632 /* some functions below drop BTM, so we need this bit */ 637 /* some functions below drop BTM, so we need this bit */
633 set_bit(TTY_HUPPING, &tty->flags); 638 set_bit(TTY_HUPPING, &tty->flags);
634 639
@@ -664,7 +669,6 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
664 669
665 spin_lock_irq(&tty->ctrl_lock); 670 spin_lock_irq(&tty->ctrl_lock);
666 clear_bit(TTY_THROTTLED, &tty->flags); 671 clear_bit(TTY_THROTTLED, &tty->flags);
667 clear_bit(TTY_PUSH, &tty->flags);
668 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 672 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
669 put_pid(tty->session); 673 put_pid(tty->session);
670 put_pid(tty->pgrp); 674 put_pid(tty->pgrp);
@@ -1388,8 +1392,7 @@ static int tty_reopen(struct tty_struct *tty)
1388 struct tty_driver *driver = tty->driver; 1392 struct tty_driver *driver = tty->driver;
1389 1393
1390 if (test_bit(TTY_CLOSING, &tty->flags) || 1394 if (test_bit(TTY_CLOSING, &tty->flags) ||
1391 test_bit(TTY_HUPPING, &tty->flags) || 1395 test_bit(TTY_HUPPING, &tty->flags))
1392 test_bit(TTY_LDISC_CHANGING, &tty->flags))
1393 return -EIO; 1396 return -EIO;
1394 1397
1395 if (driver->type == TTY_DRIVER_TYPE_PTY && 1398 if (driver->type == TTY_DRIVER_TYPE_PTY &&
@@ -1405,7 +1408,7 @@ static int tty_reopen(struct tty_struct *tty)
1405 } 1408 }
1406 tty->count++; 1409 tty->count++;
1407 1410
1408 WARN_ON(!test_bit(TTY_LDISC, &tty->flags)); 1411 WARN_ON(!tty->ldisc);
1409 1412
1410 return 0; 1413 return 0;
1411} 1414}
@@ -2202,7 +2205,7 @@ static int tty_fasync(int fd, struct file *filp, int on)
2202 * FIXME: does not honour flow control ?? 2205 * FIXME: does not honour flow control ??
2203 * 2206 *
2204 * Locking: 2207 * Locking:
2205 * Called functions take tty_ldisc_lock 2208 * Called functions take tty_ldiscs_lock
2206 * current->signal->tty check is safe without locks 2209 * current->signal->tty check is safe without locks
2207 * 2210 *
2208 * FIXME: may race normal receive processing 2211 * FIXME: may race normal receive processing
@@ -2231,7 +2234,7 @@ static int tiocsti(struct tty_struct *tty, char __user *p)
2231 * 2234 *
2232 * Copies the kernel idea of the window size into the user buffer. 2235 * Copies the kernel idea of the window size into the user buffer.
2233 * 2236 *
2234 * Locking: tty->termios_mutex is taken to ensure the winsize data 2237 * Locking: tty->winsize_mutex is taken to ensure the winsize data
2235 * is consistent. 2238 * is consistent.
2236 */ 2239 */
2237 2240
@@ -2239,9 +2242,9 @@ static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2239{ 2242{
2240 int err; 2243 int err;
2241 2244
2242 mutex_lock(&tty->termios_mutex); 2245 mutex_lock(&tty->winsize_mutex);
2243 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); 2246 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2244 mutex_unlock(&tty->termios_mutex); 2247 mutex_unlock(&tty->winsize_mutex);
2245 2248
2246 return err ? -EFAULT: 0; 2249 return err ? -EFAULT: 0;
2247} 2250}
@@ -2262,7 +2265,7 @@ int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2262 unsigned long flags; 2265 unsigned long flags;
2263 2266
2264 /* Lock the tty */ 2267 /* Lock the tty */
2265 mutex_lock(&tty->termios_mutex); 2268 mutex_lock(&tty->winsize_mutex);
2266 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 2269 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2267 goto done; 2270 goto done;
2268 /* Get the PID values and reference them so we can 2271 /* Get the PID values and reference them so we can
@@ -2277,7 +2280,7 @@ int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2277 2280
2278 tty->winsize = *ws; 2281 tty->winsize = *ws;
2279done: 2282done:
2280 mutex_unlock(&tty->termios_mutex); 2283 mutex_unlock(&tty->winsize_mutex);
2281 return 0; 2284 return 0;
2282} 2285}
2283EXPORT_SYMBOL(tty_do_resize); 2286EXPORT_SYMBOL(tty_do_resize);
@@ -3016,8 +3019,10 @@ void initialize_tty_struct(struct tty_struct *tty,
3016 tty->session = NULL; 3019 tty->session = NULL;
3017 tty->pgrp = NULL; 3020 tty->pgrp = NULL;
3018 mutex_init(&tty->legacy_mutex); 3021 mutex_init(&tty->legacy_mutex);
3019 mutex_init(&tty->termios_mutex); 3022 mutex_init(&tty->throttle_mutex);
3020 mutex_init(&tty->ldisc_mutex); 3023 init_rwsem(&tty->termios_rwsem);
3024 mutex_init(&tty->winsize_mutex);
3025 init_ldsem(&tty->ldisc_sem);
3021 init_waitqueue_head(&tty->write_wait); 3026 init_waitqueue_head(&tty->write_wait);
3022 init_waitqueue_head(&tty->read_wait); 3027 init_waitqueue_head(&tty->read_wait);
3023 INIT_WORK(&tty->hangup_work, do_tty_hangup); 3028 INIT_WORK(&tty->hangup_work, do_tty_hangup);
diff --git a/drivers/tty/tty_ioctl.c b/drivers/tty/tty_ioctl.c
index 3500d4114147..03ba081c5772 100644
--- a/drivers/tty/tty_ioctl.c
+++ b/drivers/tty/tty_ioctl.c
@@ -94,20 +94,20 @@ EXPORT_SYMBOL(tty_driver_flush_buffer);
94 * @tty: terminal 94 * @tty: terminal
95 * 95 *
96 * Indicate that a tty should stop transmitting data down the stack. 96 * Indicate that a tty should stop transmitting data down the stack.
97 * Takes the termios mutex to protect against parallel throttle/unthrottle 97 * Takes the termios rwsem to protect against parallel throttle/unthrottle
98 * and also to ensure the driver can consistently reference its own 98 * and also to ensure the driver can consistently reference its own
99 * termios data at this point when implementing software flow control. 99 * termios data at this point when implementing software flow control.
100 */ 100 */
101 101
102void tty_throttle(struct tty_struct *tty) 102void tty_throttle(struct tty_struct *tty)
103{ 103{
104 mutex_lock(&tty->termios_mutex); 104 down_write(&tty->termios_rwsem);
105 /* check TTY_THROTTLED first so it indicates our state */ 105 /* check TTY_THROTTLED first so it indicates our state */
106 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) && 106 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
107 tty->ops->throttle) 107 tty->ops->throttle)
108 tty->ops->throttle(tty); 108 tty->ops->throttle(tty);
109 tty->flow_change = 0; 109 tty->flow_change = 0;
110 mutex_unlock(&tty->termios_mutex); 110 up_write(&tty->termios_rwsem);
111} 111}
112EXPORT_SYMBOL(tty_throttle); 112EXPORT_SYMBOL(tty_throttle);
113 113
@@ -116,7 +116,7 @@ EXPORT_SYMBOL(tty_throttle);
116 * @tty: terminal 116 * @tty: terminal
117 * 117 *
118 * Indicate that a tty may continue transmitting data down the stack. 118 * Indicate that a tty may continue transmitting data down the stack.
119 * Takes the termios mutex to protect against parallel throttle/unthrottle 119 * Takes the termios rwsem to protect against parallel throttle/unthrottle
120 * and also to ensure the driver can consistently reference its own 120 * and also to ensure the driver can consistently reference its own
121 * termios data at this point when implementing software flow control. 121 * termios data at this point when implementing software flow control.
122 * 122 *
@@ -126,12 +126,12 @@ EXPORT_SYMBOL(tty_throttle);
126 126
127void tty_unthrottle(struct tty_struct *tty) 127void tty_unthrottle(struct tty_struct *tty)
128{ 128{
129 mutex_lock(&tty->termios_mutex); 129 down_write(&tty->termios_rwsem);
130 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) && 130 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
131 tty->ops->unthrottle) 131 tty->ops->unthrottle)
132 tty->ops->unthrottle(tty); 132 tty->ops->unthrottle(tty);
133 tty->flow_change = 0; 133 tty->flow_change = 0;
134 mutex_unlock(&tty->termios_mutex); 134 up_write(&tty->termios_rwsem);
135} 135}
136EXPORT_SYMBOL(tty_unthrottle); 136EXPORT_SYMBOL(tty_unthrottle);
137 137
@@ -151,7 +151,7 @@ int tty_throttle_safe(struct tty_struct *tty)
151{ 151{
152 int ret = 0; 152 int ret = 0;
153 153
154 mutex_lock(&tty->termios_mutex); 154 mutex_lock(&tty->throttle_mutex);
155 if (!test_bit(TTY_THROTTLED, &tty->flags)) { 155 if (!test_bit(TTY_THROTTLED, &tty->flags)) {
156 if (tty->flow_change != TTY_THROTTLE_SAFE) 156 if (tty->flow_change != TTY_THROTTLE_SAFE)
157 ret = 1; 157 ret = 1;
@@ -161,7 +161,7 @@ int tty_throttle_safe(struct tty_struct *tty)
161 tty->ops->throttle(tty); 161 tty->ops->throttle(tty);
162 } 162 }
163 } 163 }
164 mutex_unlock(&tty->termios_mutex); 164 mutex_unlock(&tty->throttle_mutex);
165 165
166 return ret; 166 return ret;
167} 167}
@@ -182,7 +182,7 @@ int tty_unthrottle_safe(struct tty_struct *tty)
182{ 182{
183 int ret = 0; 183 int ret = 0;
184 184
185 mutex_lock(&tty->termios_mutex); 185 mutex_lock(&tty->throttle_mutex);
186 if (test_bit(TTY_THROTTLED, &tty->flags)) { 186 if (test_bit(TTY_THROTTLED, &tty->flags)) {
187 if (tty->flow_change != TTY_UNTHROTTLE_SAFE) 187 if (tty->flow_change != TTY_UNTHROTTLE_SAFE)
188 ret = 1; 188 ret = 1;
@@ -192,7 +192,7 @@ int tty_unthrottle_safe(struct tty_struct *tty)
192 tty->ops->unthrottle(tty); 192 tty->ops->unthrottle(tty);
193 } 193 }
194 } 194 }
195 mutex_unlock(&tty->termios_mutex); 195 mutex_unlock(&tty->throttle_mutex);
196 196
197 return ret; 197 return ret;
198} 198}
@@ -468,7 +468,7 @@ EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
468 * @obad: output baud rate 468 * @obad: output baud rate
469 * 469 *
470 * Update the current termios data for the tty with the new speed 470 * Update the current termios data for the tty with the new speed
471 * settings. The caller must hold the termios_mutex for the tty in 471 * settings. The caller must hold the termios_rwsem for the tty in
472 * question. 472 * question.
473 */ 473 */
474 474
@@ -528,7 +528,7 @@ EXPORT_SYMBOL(tty_termios_hw_change);
528 * is a bit of layering violation here with n_tty in terms of the 528 * is a bit of layering violation here with n_tty in terms of the
529 * internal knowledge of this function. 529 * internal knowledge of this function.
530 * 530 *
531 * Locking: termios_mutex 531 * Locking: termios_rwsem
532 */ 532 */
533 533
534int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios) 534int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
@@ -544,7 +544,7 @@ int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
544 544
545 /* FIXME: we need to decide on some locking/ordering semantics 545 /* FIXME: we need to decide on some locking/ordering semantics
546 for the set_termios notification eventually */ 546 for the set_termios notification eventually */
547 mutex_lock(&tty->termios_mutex); 547 down_write(&tty->termios_rwsem);
548 old_termios = tty->termios; 548 old_termios = tty->termios;
549 tty->termios = *new_termios; 549 tty->termios = *new_termios;
550 unset_locked_termios(&tty->termios, &old_termios, &tty->termios_locked); 550 unset_locked_termios(&tty->termios, &old_termios, &tty->termios_locked);
@@ -586,7 +586,7 @@ int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
586 (ld->ops->set_termios)(tty, &old_termios); 586 (ld->ops->set_termios)(tty, &old_termios);
587 tty_ldisc_deref(ld); 587 tty_ldisc_deref(ld);
588 } 588 }
589 mutex_unlock(&tty->termios_mutex); 589 up_write(&tty->termios_rwsem);
590 return 0; 590 return 0;
591} 591}
592EXPORT_SYMBOL_GPL(tty_set_termios); 592EXPORT_SYMBOL_GPL(tty_set_termios);
@@ -601,7 +601,7 @@ EXPORT_SYMBOL_GPL(tty_set_termios);
601 * functions before using tty_set_termios to do the actual changes. 601 * functions before using tty_set_termios to do the actual changes.
602 * 602 *
603 * Locking: 603 * Locking:
604 * Called functions take ldisc and termios_mutex locks 604 * Called functions take ldisc and termios_rwsem locks
605 */ 605 */
606 606
607static int set_termios(struct tty_struct *tty, void __user *arg, int opt) 607static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
@@ -613,9 +613,9 @@ static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
613 if (retval) 613 if (retval)
614 return retval; 614 return retval;
615 615
616 mutex_lock(&tty->termios_mutex); 616 down_read(&tty->termios_rwsem);
617 tmp_termios = tty->termios; 617 tmp_termios = tty->termios;
618 mutex_unlock(&tty->termios_mutex); 618 up_read(&tty->termios_rwsem);
619 619
620 if (opt & TERMIOS_TERMIO) { 620 if (opt & TERMIOS_TERMIO) {
621 if (user_termio_to_kernel_termios(&tmp_termios, 621 if (user_termio_to_kernel_termios(&tmp_termios,
@@ -667,16 +667,16 @@ static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
667 667
668static void copy_termios(struct tty_struct *tty, struct ktermios *kterm) 668static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
669{ 669{
670 mutex_lock(&tty->termios_mutex); 670 down_read(&tty->termios_rwsem);
671 *kterm = tty->termios; 671 *kterm = tty->termios;
672 mutex_unlock(&tty->termios_mutex); 672 up_read(&tty->termios_rwsem);
673} 673}
674 674
675static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm) 675static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
676{ 676{
677 mutex_lock(&tty->termios_mutex); 677 down_read(&tty->termios_rwsem);
678 *kterm = tty->termios_locked; 678 *kterm = tty->termios_locked;
679 mutex_unlock(&tty->termios_mutex); 679 up_read(&tty->termios_rwsem);
680} 680}
681 681
682static int get_termio(struct tty_struct *tty, struct termio __user *termio) 682static int get_termio(struct tty_struct *tty, struct termio __user *termio)
@@ -723,10 +723,10 @@ static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
723 return -ERESTARTSYS; 723 return -ERESTARTSYS;
724 } 724 }
725 725
726 mutex_lock(&tty->termios_mutex); 726 down_write(&tty->termios_rwsem);
727 if (tty->ops->set_termiox) 727 if (tty->ops->set_termiox)
728 tty->ops->set_termiox(tty, &tnew); 728 tty->ops->set_termiox(tty, &tnew);
729 mutex_unlock(&tty->termios_mutex); 729 up_write(&tty->termios_rwsem);
730 return 0; 730 return 0;
731} 731}
732 732
@@ -761,13 +761,13 @@ static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
761{ 761{
762 struct sgttyb tmp; 762 struct sgttyb tmp;
763 763
764 mutex_lock(&tty->termios_mutex); 764 down_read(&tty->termios_rwsem);
765 tmp.sg_ispeed = tty->termios.c_ispeed; 765 tmp.sg_ispeed = tty->termios.c_ispeed;
766 tmp.sg_ospeed = tty->termios.c_ospeed; 766 tmp.sg_ospeed = tty->termios.c_ospeed;
767 tmp.sg_erase = tty->termios.c_cc[VERASE]; 767 tmp.sg_erase = tty->termios.c_cc[VERASE];
768 tmp.sg_kill = tty->termios.c_cc[VKILL]; 768 tmp.sg_kill = tty->termios.c_cc[VKILL];
769 tmp.sg_flags = get_sgflags(tty); 769 tmp.sg_flags = get_sgflags(tty);
770 mutex_unlock(&tty->termios_mutex); 770 up_read(&tty->termios_rwsem);
771 771
772 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0; 772 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
773} 773}
@@ -806,7 +806,7 @@ static void set_sgflags(struct ktermios *termios, int flags)
806 * Updates a terminal from the legacy BSD style terminal information 806 * Updates a terminal from the legacy BSD style terminal information
807 * structure. 807 * structure.
808 * 808 *
809 * Locking: termios_mutex 809 * Locking: termios_rwsem
810 */ 810 */
811 811
812static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) 812static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
@@ -822,7 +822,7 @@ static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
822 if (copy_from_user(&tmp, sgttyb, sizeof(tmp))) 822 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
823 return -EFAULT; 823 return -EFAULT;
824 824
825 mutex_lock(&tty->termios_mutex); 825 down_write(&tty->termios_rwsem);
826 termios = tty->termios; 826 termios = tty->termios;
827 termios.c_cc[VERASE] = tmp.sg_erase; 827 termios.c_cc[VERASE] = tmp.sg_erase;
828 termios.c_cc[VKILL] = tmp.sg_kill; 828 termios.c_cc[VKILL] = tmp.sg_kill;
@@ -832,7 +832,7 @@ static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
832 tty_termios_encode_baud_rate(&termios, termios.c_ispeed, 832 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
833 termios.c_ospeed); 833 termios.c_ospeed);
834#endif 834#endif
835 mutex_unlock(&tty->termios_mutex); 835 up_write(&tty->termios_rwsem);
836 tty_set_termios(tty, &termios); 836 tty_set_termios(tty, &termios);
837 return 0; 837 return 0;
838} 838}
@@ -843,14 +843,14 @@ static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
843{ 843{
844 struct tchars tmp; 844 struct tchars tmp;
845 845
846 mutex_lock(&tty->termios_mutex); 846 down_read(&tty->termios_rwsem);
847 tmp.t_intrc = tty->termios.c_cc[VINTR]; 847 tmp.t_intrc = tty->termios.c_cc[VINTR];
848 tmp.t_quitc = tty->termios.c_cc[VQUIT]; 848 tmp.t_quitc = tty->termios.c_cc[VQUIT];
849 tmp.t_startc = tty->termios.c_cc[VSTART]; 849 tmp.t_startc = tty->termios.c_cc[VSTART];
850 tmp.t_stopc = tty->termios.c_cc[VSTOP]; 850 tmp.t_stopc = tty->termios.c_cc[VSTOP];
851 tmp.t_eofc = tty->termios.c_cc[VEOF]; 851 tmp.t_eofc = tty->termios.c_cc[VEOF];
852 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */ 852 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */
853 mutex_unlock(&tty->termios_mutex); 853 up_read(&tty->termios_rwsem);
854 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; 854 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
855} 855}
856 856
@@ -860,14 +860,14 @@ static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
860 860
861 if (copy_from_user(&tmp, tchars, sizeof(tmp))) 861 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
862 return -EFAULT; 862 return -EFAULT;
863 mutex_lock(&tty->termios_mutex); 863 down_write(&tty->termios_rwsem);
864 tty->termios.c_cc[VINTR] = tmp.t_intrc; 864 tty->termios.c_cc[VINTR] = tmp.t_intrc;
865 tty->termios.c_cc[VQUIT] = tmp.t_quitc; 865 tty->termios.c_cc[VQUIT] = tmp.t_quitc;
866 tty->termios.c_cc[VSTART] = tmp.t_startc; 866 tty->termios.c_cc[VSTART] = tmp.t_startc;
867 tty->termios.c_cc[VSTOP] = tmp.t_stopc; 867 tty->termios.c_cc[VSTOP] = tmp.t_stopc;
868 tty->termios.c_cc[VEOF] = tmp.t_eofc; 868 tty->termios.c_cc[VEOF] = tmp.t_eofc;
869 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */ 869 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
870 mutex_unlock(&tty->termios_mutex); 870 up_write(&tty->termios_rwsem);
871 return 0; 871 return 0;
872} 872}
873#endif 873#endif
@@ -877,7 +877,7 @@ static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
877{ 877{
878 struct ltchars tmp; 878 struct ltchars tmp;
879 879
880 mutex_lock(&tty->termios_mutex); 880 down_read(&tty->termios_rwsem);
881 tmp.t_suspc = tty->termios.c_cc[VSUSP]; 881 tmp.t_suspc = tty->termios.c_cc[VSUSP];
882 /* what is dsuspc anyway? */ 882 /* what is dsuspc anyway? */
883 tmp.t_dsuspc = tty->termios.c_cc[VSUSP]; 883 tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
@@ -886,7 +886,7 @@ static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
886 tmp.t_flushc = tty->termios.c_cc[VEOL2]; 886 tmp.t_flushc = tty->termios.c_cc[VEOL2];
887 tmp.t_werasc = tty->termios.c_cc[VWERASE]; 887 tmp.t_werasc = tty->termios.c_cc[VWERASE];
888 tmp.t_lnextc = tty->termios.c_cc[VLNEXT]; 888 tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
889 mutex_unlock(&tty->termios_mutex); 889 up_read(&tty->termios_rwsem);
890 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; 890 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
891} 891}
892 892
@@ -897,7 +897,7 @@ static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
897 if (copy_from_user(&tmp, ltchars, sizeof(tmp))) 897 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
898 return -EFAULT; 898 return -EFAULT;
899 899
900 mutex_lock(&tty->termios_mutex); 900 down_write(&tty->termios_rwsem);
901 tty->termios.c_cc[VSUSP] = tmp.t_suspc; 901 tty->termios.c_cc[VSUSP] = tmp.t_suspc;
902 /* what is dsuspc anyway? */ 902 /* what is dsuspc anyway? */
903 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc; 903 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
@@ -906,7 +906,7 @@ static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
906 tty->termios.c_cc[VEOL2] = tmp.t_flushc; 906 tty->termios.c_cc[VEOL2] = tmp.t_flushc;
907 tty->termios.c_cc[VWERASE] = tmp.t_werasc; 907 tty->termios.c_cc[VWERASE] = tmp.t_werasc;
908 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc; 908 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
909 mutex_unlock(&tty->termios_mutex); 909 up_write(&tty->termios_rwsem);
910 return 0; 910 return 0;
911} 911}
912#endif 912#endif
@@ -946,7 +946,7 @@ static int send_prio_char(struct tty_struct *tty, char ch)
946 * @arg: enable/disable CLOCAL 946 * @arg: enable/disable CLOCAL
947 * 947 *
948 * Perform a change to the CLOCAL state and call into the driver 948 * Perform a change to the CLOCAL state and call into the driver
949 * layer to make it visible. All done with the termios mutex 949 * layer to make it visible. All done with the termios rwsem
950 */ 950 */
951 951
952static int tty_change_softcar(struct tty_struct *tty, int arg) 952static int tty_change_softcar(struct tty_struct *tty, int arg)
@@ -955,7 +955,7 @@ static int tty_change_softcar(struct tty_struct *tty, int arg)
955 int bit = arg ? CLOCAL : 0; 955 int bit = arg ? CLOCAL : 0;
956 struct ktermios old; 956 struct ktermios old;
957 957
958 mutex_lock(&tty->termios_mutex); 958 down_write(&tty->termios_rwsem);
959 old = tty->termios; 959 old = tty->termios;
960 tty->termios.c_cflag &= ~CLOCAL; 960 tty->termios.c_cflag &= ~CLOCAL;
961 tty->termios.c_cflag |= bit; 961 tty->termios.c_cflag |= bit;
@@ -963,7 +963,7 @@ static int tty_change_softcar(struct tty_struct *tty, int arg)
963 tty->ops->set_termios(tty, &old); 963 tty->ops->set_termios(tty, &old);
964 if ((tty->termios.c_cflag & CLOCAL) != bit) 964 if ((tty->termios.c_cflag & CLOCAL) != bit)
965 ret = -EINVAL; 965 ret = -EINVAL;
966 mutex_unlock(&tty->termios_mutex); 966 up_write(&tty->termios_rwsem);
967 return ret; 967 return ret;
968} 968}
969 969
@@ -1066,9 +1066,9 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
1066 if (user_termios_to_kernel_termios(&kterm, 1066 if (user_termios_to_kernel_termios(&kterm,
1067 (struct termios __user *) arg)) 1067 (struct termios __user *) arg))
1068 return -EFAULT; 1068 return -EFAULT;
1069 mutex_lock(&real_tty->termios_mutex); 1069 down_write(&real_tty->termios_rwsem);
1070 real_tty->termios_locked = kterm; 1070 real_tty->termios_locked = kterm;
1071 mutex_unlock(&real_tty->termios_mutex); 1071 up_write(&real_tty->termios_rwsem);
1072 return 0; 1072 return 0;
1073#else 1073#else
1074 case TIOCGLCKTRMIOS: 1074 case TIOCGLCKTRMIOS:
@@ -1083,9 +1083,9 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
1083 if (user_termios_to_kernel_termios_1(&kterm, 1083 if (user_termios_to_kernel_termios_1(&kterm,
1084 (struct termios __user *) arg)) 1084 (struct termios __user *) arg))
1085 return -EFAULT; 1085 return -EFAULT;
1086 mutex_lock(&real_tty->termios_mutex); 1086 down_write(&real_tty->termios_rwsem);
1087 real_tty->termios_locked = kterm; 1087 real_tty->termios_locked = kterm;
1088 mutex_unlock(&real_tty->termios_mutex); 1088 up_write(&real_tty->termios_rwsem);
1089 return ret; 1089 return ret;
1090#endif 1090#endif
1091#ifdef TCGETX 1091#ifdef TCGETX
@@ -1093,9 +1093,9 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
1093 struct termiox ktermx; 1093 struct termiox ktermx;
1094 if (real_tty->termiox == NULL) 1094 if (real_tty->termiox == NULL)
1095 return -EINVAL; 1095 return -EINVAL;
1096 mutex_lock(&real_tty->termios_mutex); 1096 down_read(&real_tty->termios_rwsem);
1097 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox)); 1097 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
1098 mutex_unlock(&real_tty->termios_mutex); 1098 up_read(&real_tty->termios_rwsem);
1099 if (copy_to_user(p, &ktermx, sizeof(struct termiox))) 1099 if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
1100 ret = -EFAULT; 1100 ret = -EFAULT;
1101 return ret; 1101 return ret;
diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c
index 1afe192bef6a..6458e11e8e9d 100644
--- a/drivers/tty/tty_ldisc.c
+++ b/drivers/tty/tty_ldisc.c
@@ -31,14 +31,20 @@
31#define tty_ldisc_debug(tty, f, args...) 31#define tty_ldisc_debug(tty, f, args...)
32#endif 32#endif
33 33
34/* lockdep nested classes for tty->ldisc_sem */
35enum {
36 LDISC_SEM_NORMAL,
37 LDISC_SEM_OTHER,
38};
39
40
34/* 41/*
35 * This guards the refcounted line discipline lists. The lock 42 * This guards the refcounted line discipline lists. The lock
36 * must be taken with irqs off because there are hangup path 43 * must be taken with irqs off because there are hangup path
37 * callers who will do ldisc lookups and cannot sleep. 44 * callers who will do ldisc lookups and cannot sleep.
38 */ 45 */
39 46
40static DEFINE_RAW_SPINLOCK(tty_ldisc_lock); 47static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
41static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
42/* Line disc dispatch table */ 48/* Line disc dispatch table */
43static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; 49static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
44 50
@@ -52,7 +58,7 @@ static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
52 * from this point onwards. 58 * from this point onwards.
53 * 59 *
54 * Locking: 60 * Locking:
55 * takes tty_ldisc_lock to guard against ldisc races 61 * takes tty_ldiscs_lock to guard against ldisc races
56 */ 62 */
57 63
58int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) 64int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
@@ -63,11 +69,11 @@ int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
63 if (disc < N_TTY || disc >= NR_LDISCS) 69 if (disc < N_TTY || disc >= NR_LDISCS)
64 return -EINVAL; 70 return -EINVAL;
65 71
66 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 72 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
67 tty_ldiscs[disc] = new_ldisc; 73 tty_ldiscs[disc] = new_ldisc;
68 new_ldisc->num = disc; 74 new_ldisc->num = disc;
69 new_ldisc->refcount = 0; 75 new_ldisc->refcount = 0;
70 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 76 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
71 77
72 return ret; 78 return ret;
73} 79}
@@ -82,7 +88,7 @@ EXPORT_SYMBOL(tty_register_ldisc);
82 * currently in use. 88 * currently in use.
83 * 89 *
84 * Locking: 90 * Locking:
85 * takes tty_ldisc_lock to guard against ldisc races 91 * takes tty_ldiscs_lock to guard against ldisc races
86 */ 92 */
87 93
88int tty_unregister_ldisc(int disc) 94int tty_unregister_ldisc(int disc)
@@ -93,12 +99,12 @@ int tty_unregister_ldisc(int disc)
93 if (disc < N_TTY || disc >= NR_LDISCS) 99 if (disc < N_TTY || disc >= NR_LDISCS)
94 return -EINVAL; 100 return -EINVAL;
95 101
96 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 102 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
97 if (tty_ldiscs[disc]->refcount) 103 if (tty_ldiscs[disc]->refcount)
98 ret = -EBUSY; 104 ret = -EBUSY;
99 else 105 else
100 tty_ldiscs[disc] = NULL; 106 tty_ldiscs[disc] = NULL;
101 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 107 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
102 108
103 return ret; 109 return ret;
104} 110}
@@ -109,7 +115,7 @@ static struct tty_ldisc_ops *get_ldops(int disc)
109 unsigned long flags; 115 unsigned long flags;
110 struct tty_ldisc_ops *ldops, *ret; 116 struct tty_ldisc_ops *ldops, *ret;
111 117
112 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 118 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
113 ret = ERR_PTR(-EINVAL); 119 ret = ERR_PTR(-EINVAL);
114 ldops = tty_ldiscs[disc]; 120 ldops = tty_ldiscs[disc];
115 if (ldops) { 121 if (ldops) {
@@ -119,7 +125,7 @@ static struct tty_ldisc_ops *get_ldops(int disc)
119 ret = ldops; 125 ret = ldops;
120 } 126 }
121 } 127 }
122 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 128 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
123 return ret; 129 return ret;
124} 130}
125 131
@@ -127,10 +133,10 @@ static void put_ldops(struct tty_ldisc_ops *ldops)
127{ 133{
128 unsigned long flags; 134 unsigned long flags;
129 135
130 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 136 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
131 ldops->refcount--; 137 ldops->refcount--;
132 module_put(ldops->owner); 138 module_put(ldops->owner);
133 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 139 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
134} 140}
135 141
136/** 142/**
@@ -143,10 +149,10 @@ static void put_ldops(struct tty_ldisc_ops *ldops)
143 * available 149 * available
144 * 150 *
145 * Locking: 151 * Locking:
146 * takes tty_ldisc_lock to guard against ldisc races 152 * takes tty_ldiscs_lock to guard against ldisc races
147 */ 153 */
148 154
149static struct tty_ldisc *tty_ldisc_get(int disc) 155static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
150{ 156{
151 struct tty_ldisc *ld; 157 struct tty_ldisc *ld;
152 struct tty_ldisc_ops *ldops; 158 struct tty_ldisc_ops *ldops;
@@ -173,8 +179,7 @@ static struct tty_ldisc *tty_ldisc_get(int disc)
173 } 179 }
174 180
175 ld->ops = ldops; 181 ld->ops = ldops;
176 atomic_set(&ld->users, 1); 182 ld->tty = tty;
177 init_waitqueue_head(&ld->wq_idle);
178 183
179 return ld; 184 return ld;
180} 185}
@@ -186,20 +191,11 @@ static struct tty_ldisc *tty_ldisc_get(int disc)
186 */ 191 */
187static inline void tty_ldisc_put(struct tty_ldisc *ld) 192static inline void tty_ldisc_put(struct tty_ldisc *ld)
188{ 193{
189 unsigned long flags;
190
191 if (WARN_ON_ONCE(!ld)) 194 if (WARN_ON_ONCE(!ld))
192 return; 195 return;
193 196
194 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 197 put_ldops(ld->ops);
195
196 /* unreleased reader reference(s) will cause this WARN */
197 WARN_ON(!atomic_dec_and_test(&ld->users));
198
199 ld->ops->refcount--;
200 module_put(ld->ops->owner);
201 kfree(ld); 198 kfree(ld);
202 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
203} 199}
204 200
205static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) 201static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
@@ -251,34 +247,6 @@ const struct file_operations tty_ldiscs_proc_fops = {
251}; 247};
252 248
253/** 249/**
254 * tty_ldisc_try - internal helper
255 * @tty: the tty
256 *
257 * Make a single attempt to grab and bump the refcount on
258 * the tty ldisc. Return 0 on failure or 1 on success. This is
259 * used to implement both the waiting and non waiting versions
260 * of tty_ldisc_ref
261 *
262 * Locking: takes tty_ldisc_lock
263 */
264
265static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
266{
267 unsigned long flags;
268 struct tty_ldisc *ld;
269
270 /* FIXME: this allows reference acquire after TTY_LDISC is cleared */
271 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
272 ld = NULL;
273 if (test_bit(TTY_LDISC, &tty->flags) && tty->ldisc) {
274 ld = tty->ldisc;
275 atomic_inc(&ld->users);
276 }
277 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
278 return ld;
279}
280
281/**
282 * tty_ldisc_ref_wait - wait for the tty ldisc 250 * tty_ldisc_ref_wait - wait for the tty ldisc
283 * @tty: tty device 251 * @tty: tty device
284 * 252 *
@@ -291,16 +259,15 @@ static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
291 * against a discipline change, such as an existing ldisc reference 259 * against a discipline change, such as an existing ldisc reference
292 * (which we check for) 260 * (which we check for)
293 * 261 *
294 * Locking: call functions take tty_ldisc_lock 262 * Note: only callable from a file_operations routine (which
263 * guarantees tty->ldisc != NULL when the lock is acquired).
295 */ 264 */
296 265
297struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) 266struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
298{ 267{
299 struct tty_ldisc *ld; 268 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
300 269 WARN_ON(!tty->ldisc);
301 /* wait_event is a macro */ 270 return tty->ldisc;
302 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
303 return ld;
304} 271}
305EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); 272EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
306 273
@@ -311,13 +278,18 @@ EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
311 * Dereference the line discipline for the terminal and take a 278 * Dereference the line discipline for the terminal and take a
312 * reference to it. If the line discipline is in flux then 279 * reference to it. If the line discipline is in flux then
313 * return NULL. Can be called from IRQ and timer functions. 280 * return NULL. Can be called from IRQ and timer functions.
314 *
315 * Locking: called functions take tty_ldisc_lock
316 */ 281 */
317 282
318struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) 283struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
319{ 284{
320 return tty_ldisc_try(tty); 285 struct tty_ldisc *ld = NULL;
286
287 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
288 ld = tty->ldisc;
289 if (!ld)
290 ldsem_up_read(&tty->ldisc_sem);
291 }
292 return ld;
321} 293}
322EXPORT_SYMBOL_GPL(tty_ldisc_ref); 294EXPORT_SYMBOL_GPL(tty_ldisc_ref);
323 295
@@ -327,48 +299,91 @@ EXPORT_SYMBOL_GPL(tty_ldisc_ref);
327 * 299 *
328 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May 300 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
329 * be called in IRQ context. 301 * be called in IRQ context.
330 *
331 * Locking: takes tty_ldisc_lock
332 */ 302 */
333 303
334void tty_ldisc_deref(struct tty_ldisc *ld) 304void tty_ldisc_deref(struct tty_ldisc *ld)
335{ 305{
336 unsigned long flags; 306 ldsem_up_read(&ld->tty->ldisc_sem);
307}
308EXPORT_SYMBOL_GPL(tty_ldisc_deref);
337 309
338 if (WARN_ON_ONCE(!ld))
339 return;
340 310
341 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 311static inline int __lockfunc
342 /* 312tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
343 * WARNs if one-too-many reader references were released 313{
344 * - the last reference must be released with tty_ldisc_put 314 return ldsem_down_write(&tty->ldisc_sem, timeout);
345 */ 315}
346 WARN_ON(atomic_dec_and_test(&ld->users));
347 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
348 316
349 if (waitqueue_active(&ld->wq_idle)) 317static inline int __lockfunc
350 wake_up(&ld->wq_idle); 318tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
319{
320 return ldsem_down_write_nested(&tty->ldisc_sem,
321 LDISC_SEM_OTHER, timeout);
351} 322}
352EXPORT_SYMBOL_GPL(tty_ldisc_deref);
353 323
354/** 324static inline void tty_ldisc_unlock(struct tty_struct *tty)
355 * tty_ldisc_enable - allow ldisc use 325{
356 * @tty: terminal to activate ldisc on 326 return ldsem_up_write(&tty->ldisc_sem);
357 * 327}
358 * Set the TTY_LDISC flag when the line discipline can be called
359 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
360 * changing flag to indicate any ldisc change is now over.
361 *
362 * Note: nobody should set the TTY_LDISC bit except via this function.
363 * Clearing directly is allowed.
364 */
365 328
366static void tty_ldisc_enable(struct tty_struct *tty) 329static int __lockfunc
330tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
331 unsigned long timeout)
332{
333 int ret;
334
335 if (tty < tty2) {
336 ret = tty_ldisc_lock(tty, timeout);
337 if (ret) {
338 ret = tty_ldisc_lock_nested(tty2, timeout);
339 if (!ret)
340 tty_ldisc_unlock(tty);
341 }
342 } else {
343 /* if this is possible, it has lots of implications */
344 WARN_ON_ONCE(tty == tty2);
345 if (tty2 && tty != tty2) {
346 ret = tty_ldisc_lock(tty2, timeout);
347 if (ret) {
348 ret = tty_ldisc_lock_nested(tty, timeout);
349 if (!ret)
350 tty_ldisc_unlock(tty2);
351 }
352 } else
353 ret = tty_ldisc_lock(tty, timeout);
354 }
355
356 if (!ret)
357 return -EBUSY;
358
359 set_bit(TTY_LDISC_HALTED, &tty->flags);
360 if (tty2)
361 set_bit(TTY_LDISC_HALTED, &tty2->flags);
362 return 0;
363}
364
365static void __lockfunc
366tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
367{
368 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
369}
370
371static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
372 struct tty_struct *tty2)
373{
374 tty_ldisc_unlock(tty);
375 if (tty2)
376 tty_ldisc_unlock(tty2);
377}
378
379static void __lockfunc tty_ldisc_enable_pair(struct tty_struct *tty,
380 struct tty_struct *tty2)
367{ 381{
368 clear_bit(TTY_LDISC_HALTED, &tty->flags); 382 clear_bit(TTY_LDISC_HALTED, &tty->flags);
369 set_bit(TTY_LDISC, &tty->flags); 383 if (tty2)
370 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 384 clear_bit(TTY_LDISC_HALTED, &tty2->flags);
371 wake_up(&tty_ldisc_wait); 385
386 tty_ldisc_unlock_pair(tty, tty2);
372} 387}
373 388
374/** 389/**
@@ -400,14 +415,14 @@ EXPORT_SYMBOL_GPL(tty_ldisc_flush);
400 * they are not on hot paths so a little discipline won't do 415 * they are not on hot paths so a little discipline won't do
401 * any harm. 416 * any harm.
402 * 417 *
403 * Locking: takes termios_mutex 418 * Locking: takes termios_rwsem
404 */ 419 */
405 420
406static void tty_set_termios_ldisc(struct tty_struct *tty, int num) 421static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
407{ 422{
408 mutex_lock(&tty->termios_mutex); 423 down_write(&tty->termios_rwsem);
409 tty->termios.c_line = num; 424 tty->termios.c_line = num;
410 mutex_unlock(&tty->termios_mutex); 425 up_write(&tty->termios_rwsem);
411} 426}
412 427
413/** 428/**
@@ -468,14 +483,14 @@ static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
468 int r; 483 int r;
469 484
470 /* There is an outstanding reference here so this is safe */ 485 /* There is an outstanding reference here so this is safe */
471 old = tty_ldisc_get(old->ops->num); 486 old = tty_ldisc_get(tty, old->ops->num);
472 WARN_ON(IS_ERR(old)); 487 WARN_ON(IS_ERR(old));
473 tty->ldisc = old; 488 tty->ldisc = old;
474 tty_set_termios_ldisc(tty, old->ops->num); 489 tty_set_termios_ldisc(tty, old->ops->num);
475 if (tty_ldisc_open(tty, old) < 0) { 490 if (tty_ldisc_open(tty, old) < 0) {
476 tty_ldisc_put(old); 491 tty_ldisc_put(old);
477 /* This driver is always present */ 492 /* This driver is always present */
478 new_ldisc = tty_ldisc_get(N_TTY); 493 new_ldisc = tty_ldisc_get(tty, N_TTY);
479 if (IS_ERR(new_ldisc)) 494 if (IS_ERR(new_ldisc))
480 panic("n_tty: get"); 495 panic("n_tty: get");
481 tty->ldisc = new_ldisc; 496 tty->ldisc = new_ldisc;
@@ -489,101 +504,6 @@ static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
489} 504}
490 505
491/** 506/**
492 * tty_ldisc_wait_idle - wait for the ldisc to become idle
493 * @tty: tty to wait for
494 * @timeout: for how long to wait at most
495 *
496 * Wait for the line discipline to become idle. The discipline must
497 * have been halted for this to guarantee it remains idle.
498 */
499static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
500{
501 long ret;
502 ret = wait_event_timeout(tty->ldisc->wq_idle,
503 atomic_read(&tty->ldisc->users) == 1, timeout);
504 return ret > 0 ? 0 : -EBUSY;
505}
506
507/**
508 * tty_ldisc_halt - shut down the line discipline
509 * @tty: tty device
510 * @o_tty: paired pty device (can be NULL)
511 * @timeout: # of jiffies to wait for ldisc refs to be released
512 *
513 * Shut down the line discipline and work queue for this tty device and
514 * its paired pty (if exists). Clearing the TTY_LDISC flag ensures
515 * no further references can be obtained, while waiting for existing
516 * references to be released ensures no more data is fed to the ldisc.
517 *
518 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
519 * in order to make sure any currently executing ldisc work is also
520 * flushed.
521 */
522
523static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
524 long timeout)
525{
526 int retval;
527
528 clear_bit(TTY_LDISC, &tty->flags);
529 if (o_tty)
530 clear_bit(TTY_LDISC, &o_tty->flags);
531
532 retval = tty_ldisc_wait_idle(tty, timeout);
533 if (!retval && o_tty)
534 retval = tty_ldisc_wait_idle(o_tty, timeout);
535 if (retval)
536 return retval;
537
538 set_bit(TTY_LDISC_HALTED, &tty->flags);
539 if (o_tty)
540 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
541
542 return 0;
543}
544
545/**
546 * tty_ldisc_hangup_halt - halt the line discipline for hangup
547 * @tty: tty being hung up
548 *
549 * Shut down the line discipline and work queue for the tty device
550 * being hungup. Clear the TTY_LDISC flag to ensure no further
551 * references can be obtained and wait for remaining references to be
552 * released to ensure no more data is fed to this ldisc.
553 * Caller must hold legacy and ->ldisc_mutex.
554 *
555 * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
556 * with this function by checking the TTY_HUPPING flag.
557 */
558static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
559{
560 char cur_n[TASK_COMM_LEN], tty_n[64];
561 long timeout = 3 * HZ;
562
563 clear_bit(TTY_LDISC, &tty->flags);
564
565 if (tty->ldisc) { /* Not yet closed */
566 tty_unlock(tty);
567
568 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
569 timeout = MAX_SCHEDULE_TIMEOUT;
570 printk_ratelimited(KERN_WARNING
571 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
572 __func__, get_task_comm(cur_n, current),
573 tty_name(tty, tty_n));
574 }
575
576 set_bit(TTY_LDISC_HALTED, &tty->flags);
577
578 /* must reacquire both locks and preserve lock order */
579 mutex_unlock(&tty->ldisc_mutex);
580 tty_lock(tty);
581 mutex_lock(&tty->ldisc_mutex);
582 }
583 return !!tty->ldisc;
584}
585
586/**
587 * tty_set_ldisc - set line discipline 507 * tty_set_ldisc - set line discipline
588 * @tty: the terminal to set 508 * @tty: the terminal to set
589 * @ldisc: the line discipline 509 * @ldisc: the line discipline
@@ -592,110 +512,49 @@ static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
592 * context. The ldisc change logic has to protect itself against any 512 * context. The ldisc change logic has to protect itself against any
593 * overlapping ldisc change (including on the other end of pty pairs), 513 * overlapping ldisc change (including on the other end of pty pairs),
594 * the close of one side of a tty/pty pair, and eventually hangup. 514 * the close of one side of a tty/pty pair, and eventually hangup.
595 *
596 * Locking: takes tty_ldisc_lock, termios_mutex
597 */ 515 */
598 516
599int tty_set_ldisc(struct tty_struct *tty, int ldisc) 517int tty_set_ldisc(struct tty_struct *tty, int ldisc)
600{ 518{
601 int retval; 519 int retval;
602 struct tty_ldisc *o_ldisc, *new_ldisc; 520 struct tty_ldisc *old_ldisc, *new_ldisc;
603 struct tty_struct *o_tty; 521 struct tty_struct *o_tty = tty->link;
604 522
605 new_ldisc = tty_ldisc_get(ldisc); 523 new_ldisc = tty_ldisc_get(tty, ldisc);
606 if (IS_ERR(new_ldisc)) 524 if (IS_ERR(new_ldisc))
607 return PTR_ERR(new_ldisc); 525 return PTR_ERR(new_ldisc);
608 526
609 tty_lock(tty); 527 retval = tty_ldisc_lock_pair_timeout(tty, o_tty, 5 * HZ);
610 /* 528 if (retval) {
611 * We need to look at the tty locking here for pty/tty pairs 529 tty_ldisc_put(new_ldisc);
612 * when both sides try to change in parallel. 530 return retval;
613 */ 531 }
614
615 o_tty = tty->link; /* o_tty is the pty side or NULL */
616
617 532
618 /* 533 /*
619 * Check the no-op case 534 * Check the no-op case
620 */ 535 */
621 536
622 if (tty->ldisc->ops->num == ldisc) { 537 if (tty->ldisc->ops->num == ldisc) {
623 tty_unlock(tty); 538 tty_ldisc_enable_pair(tty, o_tty);
624 tty_ldisc_put(new_ldisc); 539 tty_ldisc_put(new_ldisc);
625 return 0; 540 return 0;
626 } 541 }
627 542
628 mutex_lock(&tty->ldisc_mutex); 543 old_ldisc = tty->ldisc;
629
630 /*
631 * We could be midstream of another ldisc change which has
632 * dropped the lock during processing. If so we need to wait.
633 */
634
635 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
636 mutex_unlock(&tty->ldisc_mutex);
637 tty_unlock(tty);
638 wait_event(tty_ldisc_wait,
639 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
640 tty_lock(tty);
641 mutex_lock(&tty->ldisc_mutex);
642 }
643
644 set_bit(TTY_LDISC_CHANGING, &tty->flags);
645
646 /*
647 * No more input please, we are switching. The new ldisc
648 * will update this value in the ldisc open function
649 */
650
651 tty->receive_room = 0;
652
653 o_ldisc = tty->ldisc;
654
655 tty_unlock(tty);
656 /*
657 * Make sure we don't change while someone holds a
658 * reference to the line discipline. The TTY_LDISC bit
659 * prevents anyone taking a reference once it is clear.
660 * We need the lock to avoid racing reference takers.
661 *
662 * We must clear the TTY_LDISC bit here to avoid a livelock
663 * with a userspace app continually trying to use the tty in
664 * parallel to the change and re-referencing the tty.
665 */
666
667 retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
668
669 /*
670 * Wait for hangup to complete, if pending.
671 * We must drop the mutex here in case a hangup is also in process.
672 */
673
674 mutex_unlock(&tty->ldisc_mutex);
675
676 flush_work(&tty->hangup_work);
677
678 tty_lock(tty); 544 tty_lock(tty);
679 mutex_lock(&tty->ldisc_mutex);
680
681 /* handle wait idle failure locked */
682 if (retval) {
683 tty_ldisc_put(new_ldisc);
684 goto enable;
685 }
686 545
687 if (test_bit(TTY_HUPPING, &tty->flags)) { 546 if (test_bit(TTY_HUPPING, &tty->flags) ||
547 test_bit(TTY_HUPPED, &tty->flags)) {
688 /* We were raced by the hangup method. It will have stomped 548 /* We were raced by the hangup method. It will have stomped
689 the ldisc data and closed the ldisc down */ 549 the ldisc data and closed the ldisc down */
690 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 550 tty_ldisc_enable_pair(tty, o_tty);
691 mutex_unlock(&tty->ldisc_mutex);
692 tty_ldisc_put(new_ldisc); 551 tty_ldisc_put(new_ldisc);
693 tty_unlock(tty); 552 tty_unlock(tty);
694 return -EIO; 553 return -EIO;
695 } 554 }
696 555
697 /* Shutdown the current discipline. */ 556 /* Shutdown the old discipline. */
698 tty_ldisc_close(tty, o_ldisc); 557 tty_ldisc_close(tty, old_ldisc);
699 558
700 /* Now set up the new line discipline. */ 559 /* Now set up the new line discipline. */
701 tty->ldisc = new_ldisc; 560 tty->ldisc = new_ldisc;
@@ -705,26 +564,24 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
705 if (retval < 0) { 564 if (retval < 0) {
706 /* Back to the old one or N_TTY if we can't */ 565 /* Back to the old one or N_TTY if we can't */
707 tty_ldisc_put(new_ldisc); 566 tty_ldisc_put(new_ldisc);
708 tty_ldisc_restore(tty, o_ldisc); 567 tty_ldisc_restore(tty, old_ldisc);
709 } 568 }
710 569
711 /* At this point we hold a reference to the new ldisc and a 570 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc)
712 a reference to the old ldisc. If we ended up flipping back
713 to the existing ldisc we have two references to it */
714
715 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
716 tty->ops->set_ldisc(tty); 571 tty->ops->set_ldisc(tty);
717 572
718 tty_ldisc_put(o_ldisc); 573 /* At this point we hold a reference to the new ldisc and a
574 reference to the old ldisc, or we hold two references to
575 the old ldisc (if it was restored as part of error cleanup
576 above). In either case, releasing a single reference from
577 the old ldisc is correct. */
578
579 tty_ldisc_put(old_ldisc);
719 580
720enable:
721 /* 581 /*
722 * Allow ldisc referencing to occur again 582 * Allow ldisc referencing to occur again
723 */ 583 */
724 584 tty_ldisc_enable_pair(tty, o_tty);
725 tty_ldisc_enable(tty);
726 if (o_tty)
727 tty_ldisc_enable(o_tty);
728 585
729 /* Restart the work queue in case no characters kick it off. Safe if 586 /* Restart the work queue in case no characters kick it off. Safe if
730 already running */ 587 already running */
@@ -732,7 +589,6 @@ enable:
732 if (o_tty) 589 if (o_tty)
733 schedule_work(&o_tty->port->buf.work); 590 schedule_work(&o_tty->port->buf.work);
734 591
735 mutex_unlock(&tty->ldisc_mutex);
736 tty_unlock(tty); 592 tty_unlock(tty);
737 return retval; 593 return retval;
738} 594}
@@ -746,11 +602,11 @@ enable:
746 602
747static void tty_reset_termios(struct tty_struct *tty) 603static void tty_reset_termios(struct tty_struct *tty)
748{ 604{
749 mutex_lock(&tty->termios_mutex); 605 down_write(&tty->termios_rwsem);
750 tty->termios = tty->driver->init_termios; 606 tty->termios = tty->driver->init_termios;
751 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 607 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
752 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 608 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
753 mutex_unlock(&tty->termios_mutex); 609 up_write(&tty->termios_rwsem);
754} 610}
755 611
756 612
@@ -765,7 +621,7 @@ static void tty_reset_termios(struct tty_struct *tty)
765 621
766static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc) 622static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
767{ 623{
768 struct tty_ldisc *ld = tty_ldisc_get(ldisc); 624 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
769 625
770 if (IS_ERR(ld)) 626 if (IS_ERR(ld))
771 return -1; 627 return -1;
@@ -804,14 +660,8 @@ void tty_ldisc_hangup(struct tty_struct *tty)
804 660
805 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc); 661 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
806 662
807 /*
808 * FIXME! What are the locking issues here? This may me overdoing
809 * things... This question is especially important now that we've
810 * removed the irqlock.
811 */
812 ld = tty_ldisc_ref(tty); 663 ld = tty_ldisc_ref(tty);
813 if (ld != NULL) { 664 if (ld != NULL) {
814 /* We may have no line discipline at this point */
815 if (ld->ops->flush_buffer) 665 if (ld->ops->flush_buffer)
816 ld->ops->flush_buffer(tty); 666 ld->ops->flush_buffer(tty);
817 tty_driver_flush_buffer(tty); 667 tty_driver_flush_buffer(tty);
@@ -822,21 +672,22 @@ void tty_ldisc_hangup(struct tty_struct *tty)
822 ld->ops->hangup(tty); 672 ld->ops->hangup(tty);
823 tty_ldisc_deref(ld); 673 tty_ldisc_deref(ld);
824 } 674 }
825 /* 675
826 * FIXME: Once we trust the LDISC code better we can wait here for
827 * ldisc completion and fix the driver call race
828 */
829 wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 676 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
830 wake_up_interruptible_poll(&tty->read_wait, POLLIN); 677 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
678
679 tty_unlock(tty);
680
831 /* 681 /*
832 * Shutdown the current line discipline, and reset it to 682 * Shutdown the current line discipline, and reset it to
833 * N_TTY if need be. 683 * N_TTY if need be.
834 * 684 *
835 * Avoid racing set_ldisc or tty_ldisc_release 685 * Avoid racing set_ldisc or tty_ldisc_release
836 */ 686 */
837 mutex_lock(&tty->ldisc_mutex); 687 tty_ldisc_lock_pair(tty, tty->link);
688 tty_lock(tty);
838 689
839 if (tty_ldisc_hangup_halt(tty)) { 690 if (tty->ldisc) {
840 691
841 /* At this point we have a halted ldisc; we want to close it and 692 /* At this point we have a halted ldisc; we want to close it and
842 reopen a new ldisc. We could defer the reopen to the next 693 reopen a new ldisc. We could defer the reopen to the next
@@ -855,9 +706,8 @@ void tty_ldisc_hangup(struct tty_struct *tty)
855 BUG_ON(tty_ldisc_reinit(tty, N_TTY)); 706 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
856 WARN_ON(tty_ldisc_open(tty, tty->ldisc)); 707 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
857 } 708 }
858 tty_ldisc_enable(tty);
859 } 709 }
860 mutex_unlock(&tty->ldisc_mutex); 710 tty_ldisc_enable_pair(tty, tty->link);
861 if (reset) 711 if (reset)
862 tty_reset_termios(tty); 712 tty_reset_termios(tty);
863 713
@@ -889,15 +739,12 @@ int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
889 tty_ldisc_close(tty, ld); 739 tty_ldisc_close(tty, ld);
890 return retval; 740 return retval;
891 } 741 }
892 tty_ldisc_enable(o_tty);
893 } 742 }
894 tty_ldisc_enable(tty);
895 return 0; 743 return 0;
896} 744}
897 745
898static void tty_ldisc_kill(struct tty_struct *tty) 746static void tty_ldisc_kill(struct tty_struct *tty)
899{ 747{
900 mutex_lock(&tty->ldisc_mutex);
901 /* 748 /*
902 * Now kill off the ldisc 749 * Now kill off the ldisc
903 */ 750 */
@@ -908,7 +755,6 @@ static void tty_ldisc_kill(struct tty_struct *tty)
908 755
909 /* Ensure the next open requests the N_TTY ldisc */ 756 /* Ensure the next open requests the N_TTY ldisc */
910 tty_set_termios_ldisc(tty, N_TTY); 757 tty_set_termios_ldisc(tty, N_TTY);
911 mutex_unlock(&tty->ldisc_mutex);
912} 758}
913 759
914/** 760/**
@@ -930,15 +776,16 @@ void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
930 776
931 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc); 777 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
932 778
933 tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT); 779 tty_ldisc_lock_pair(tty, o_tty);
934
935 tty_lock_pair(tty, o_tty); 780 tty_lock_pair(tty, o_tty);
936 /* This will need doing differently if we need to lock */ 781
937 tty_ldisc_kill(tty); 782 tty_ldisc_kill(tty);
938 if (o_tty) 783 if (o_tty)
939 tty_ldisc_kill(o_tty); 784 tty_ldisc_kill(o_tty);
940 785
941 tty_unlock_pair(tty, o_tty); 786 tty_unlock_pair(tty, o_tty);
787 tty_ldisc_unlock_pair(tty, o_tty);
788
942 /* And the memory resources remaining (buffers, termios) will be 789 /* And the memory resources remaining (buffers, termios) will be
943 disposed of when the kref hits zero */ 790 disposed of when the kref hits zero */
944 791
@@ -955,7 +802,7 @@ void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
955 802
956void tty_ldisc_init(struct tty_struct *tty) 803void tty_ldisc_init(struct tty_struct *tty)
957{ 804{
958 struct tty_ldisc *ld = tty_ldisc_get(N_TTY); 805 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
959 if (IS_ERR(ld)) 806 if (IS_ERR(ld))
960 panic("n_tty: init_tty"); 807 panic("n_tty: init_tty");
961 tty->ldisc = ld; 808 tty->ldisc = ld;
diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
index a9af1b9ae160..d0e3a4497707 100644
--- a/drivers/tty/vt/keyboard.c
+++ b/drivers/tty/vt/keyboard.c
@@ -132,12 +132,6 @@ static int shift_state = 0;
132static unsigned char ledstate = 0xff; /* undefined */ 132static unsigned char ledstate = 0xff; /* undefined */
133static unsigned char ledioctl; 133static unsigned char ledioctl;
134 134
135static struct ledptr {
136 unsigned int *addr;
137 unsigned int mask;
138 unsigned char valid:1;
139} ledptrs[3];
140
141/* 135/*
142 * Notifier list for console keyboard events 136 * Notifier list for console keyboard events
143 */ 137 */
@@ -994,24 +988,11 @@ void setledstate(struct kbd_struct *kbd, unsigned int led)
994static inline unsigned char getleds(void) 988static inline unsigned char getleds(void)
995{ 989{
996 struct kbd_struct *kbd = kbd_table + fg_console; 990 struct kbd_struct *kbd = kbd_table + fg_console;
997 unsigned char leds;
998 int i;
999 991
1000 if (kbd->ledmode == LED_SHOW_IOCTL) 992 if (kbd->ledmode == LED_SHOW_IOCTL)
1001 return ledioctl; 993 return ledioctl;
1002 994
1003 leds = kbd->ledflagstate; 995 return kbd->ledflagstate;
1004
1005 if (kbd->ledmode == LED_SHOW_MEM) {
1006 for (i = 0; i < 3; i++)
1007 if (ledptrs[i].valid) {
1008 if (*ledptrs[i].addr & ledptrs[i].mask)
1009 leds |= (1 << i);
1010 else
1011 leds &= ~(1 << i);
1012 }
1013 }
1014 return leds;
1015} 996}
1016 997
1017static int kbd_update_leds_helper(struct input_handle *handle, void *data) 998static int kbd_update_leds_helper(struct input_handle *handle, void *data)
diff --git a/drivers/tty/vt/selection.c b/drivers/tty/vt/selection.c
index 60b7b6926059..ea27804d87af 100644
--- a/drivers/tty/vt/selection.c
+++ b/drivers/tty/vt/selection.c
@@ -24,6 +24,7 @@
24#include <linux/selection.h> 24#include <linux/selection.h>
25#include <linux/tiocl.h> 25#include <linux/tiocl.h>
26#include <linux/console.h> 26#include <linux/console.h>
27#include <linux/tty_flip.h>
27 28
28/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */ 29/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
29#define isspace(c) ((c) == ' ') 30#define isspace(c) ((c) == ' ')
@@ -346,8 +347,8 @@ int paste_selection(struct tty_struct *tty)
346 console_unlock(); 347 console_unlock();
347 348
348 ld = tty_ldisc_ref_wait(tty); 349 ld = tty_ldisc_ref_wait(tty);
350 tty_buffer_lock_exclusive(&vc->port);
349 351
350 /* FIXME: this is completely unsafe */
351 add_wait_queue(&vc->paste_wait, &wait); 352 add_wait_queue(&vc->paste_wait, &wait);
352 while (sel_buffer && sel_buffer_lth > pasted) { 353 while (sel_buffer && sel_buffer_lth > pasted) {
353 set_current_state(TASK_INTERRUPTIBLE); 354 set_current_state(TASK_INTERRUPTIBLE);
@@ -356,13 +357,14 @@ int paste_selection(struct tty_struct *tty)
356 continue; 357 continue;
357 } 358 }
358 count = sel_buffer_lth - pasted; 359 count = sel_buffer_lth - pasted;
359 count = min(count, tty->receive_room); 360 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
360 ld->ops->receive_buf(tty, sel_buffer + pasted, NULL, count); 361 count);
361 pasted += count; 362 pasted += count;
362 } 363 }
363 remove_wait_queue(&vc->paste_wait, &wait); 364 remove_wait_queue(&vc->paste_wait, &wait);
364 __set_current_state(TASK_RUNNING); 365 __set_current_state(TASK_RUNNING);
365 366
367 tty_buffer_unlock_exclusive(&vc->port);
366 tty_ldisc_deref(ld); 368 tty_ldisc_deref(ld);
367 return 0; 369 return 0;
368} 370}
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index c677829baa8b..02af6ccefe6a 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -828,7 +828,7 @@ static inline int resize_screen(struct vc_data *vc, int width, int height,
828 * If the caller passes a tty structure then update the termios winsize 828 * If the caller passes a tty structure then update the termios winsize
829 * information and perform any necessary signal handling. 829 * information and perform any necessary signal handling.
830 * 830 *
831 * Caller must hold the console semaphore. Takes the termios mutex and 831 * Caller must hold the console semaphore. Takes the termios rwsem and
832 * ctrl_lock of the tty IFF a tty is passed. 832 * ctrl_lock of the tty IFF a tty is passed.
833 */ 833 */
834 834
@@ -972,7 +972,7 @@ int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
972 * the actual work. 972 * the actual work.
973 * 973 *
974 * Takes the console sem and the called methods then take the tty 974 * Takes the console sem and the called methods then take the tty
975 * termios_mutex and the tty ctrl_lock in that order. 975 * termios_rwsem and the tty ctrl_lock in that order.
976 */ 976 */
977static int vt_resize(struct tty_struct *tty, struct winsize *ws) 977static int vt_resize(struct tty_struct *tty, struct winsize *ws)
978{ 978{