1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/console.h>
29#include <linux/of.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
32#include <linux/device.h>
33#include <linux/serial.h>
34#include <linux/serial_core.h>
35#include <linux/delay.h>
36#include <linux/mutex.h>
37
38#include <asm/irq.h>
39#include <asm/uaccess.h>
40
41
42
43
44static DEFINE_MUTEX(port_mutex);
45
46
47
48
49
50static struct lock_class_key port_lock_key;
51
52#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53
54static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
55 struct ktermios *old_termios);
56static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
57static void uart_change_pm(struct uart_state *state,
58 enum uart_pm_state pm_state);
59
60static void uart_port_shutdown(struct tty_port *port);
61
62static int uart_dcd_enabled(struct uart_port *uport)
63{
64 return !!(uport->status & UPSTAT_DCD_ENABLE);
65}
66
67static inline struct uart_port *uart_port_ref(struct uart_state *state)
68{
69 if (atomic_add_unless(&state->refcount, 1, 0))
70 return state->uart_port;
71 return NULL;
72}
73
74static inline void uart_port_deref(struct uart_port *uport)
75{
76 if (uport && atomic_dec_and_test(&uport->state->refcount))
77 wake_up(&uport->state->remove_wait);
78}
79
80#define uart_port_lock(state, flags) \
81 ({ \
82 struct uart_port *__uport = uart_port_ref(state); \
83 if (__uport) \
84 spin_lock_irqsave(&__uport->lock, flags); \
85 __uport; \
86 })
87
88#define uart_port_unlock(uport, flags) \
89 ({ \
90 struct uart_port *__uport = uport; \
91 if (__uport) \
92 spin_unlock_irqrestore(&__uport->lock, flags); \
93 uart_port_deref(__uport); \
94 })
95
96static inline struct uart_port *uart_port_check(struct uart_state *state)
97{
98 lockdep_assert_held(&state->port.mutex);
99 return state->uart_port;
100}
101
102
103
104
105
106void uart_write_wakeup(struct uart_port *port)
107{
108 struct uart_state *state = port->state;
109
110
111
112
113 BUG_ON(!state);
114 tty_port_tty_wakeup(&state->port);
115}
116
117static void uart_stop(struct tty_struct *tty)
118{
119 struct uart_state *state = tty->driver_data;
120 struct uart_port *port;
121 unsigned long flags;
122
123 port = uart_port_lock(state, flags);
124 if (port)
125 port->ops->stop_tx(port);
126 uart_port_unlock(port, flags);
127}
128
129static void __uart_start(struct tty_struct *tty)
130{
131 struct uart_state *state = tty->driver_data;
132 struct uart_port *port = state->uart_port;
133
134 if (port && !uart_tx_stopped(port))
135 port->ops->start_tx(port);
136}
137
138static void uart_start(struct tty_struct *tty)
139{
140 struct uart_state *state = tty->driver_data;
141 struct uart_port *port;
142 unsigned long flags;
143
144 port = uart_port_lock(state, flags);
145 __uart_start(tty);
146 uart_port_unlock(port, flags);
147}
148
149static void
150uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
151{
152 unsigned long flags;
153 unsigned int old;
154
155 spin_lock_irqsave(&port->lock, flags);
156 old = port->mctrl;
157 port->mctrl = (old & ~clear) | set;
158 if (old != port->mctrl)
159 port->ops->set_mctrl(port, port->mctrl);
160 spin_unlock_irqrestore(&port->lock, flags);
161}
162
163#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
164#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
165
166
167
168
169
170static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
171 int init_hw)
172{
173 struct uart_port *uport = uart_port_check(state);
174 unsigned long page;
175 int retval = 0;
176
177 if (uport->type == PORT_UNKNOWN)
178 return 1;
179
180
181
182
183 uart_change_pm(state, UART_PM_STATE_ON);
184
185
186
187
188
189 if (!state->xmit.buf) {
190
191 page = get_zeroed_page(GFP_KERNEL);
192 if (!page)
193 return -ENOMEM;
194
195 state->xmit.buf = (unsigned char *) page;
196 uart_circ_clear(&state->xmit);
197 }
198
199 retval = uport->ops->startup(uport);
200 if (retval == 0) {
201 if (uart_console(uport) && uport->cons->cflag) {
202 tty->termios.c_cflag = uport->cons->cflag;
203 uport->cons->cflag = 0;
204 }
205
206
207
208 uart_change_speed(tty, state, NULL);
209
210
211
212
213
214 if (init_hw && C_BAUD(tty))
215 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
216 }
217
218
219
220
221
222
223 if (retval && capable(CAP_SYS_ADMIN))
224 return 1;
225
226 return retval;
227}
228
229static int uart_startup(struct tty_struct *tty, struct uart_state *state,
230 int init_hw)
231{
232 struct tty_port *port = &state->port;
233 int retval;
234
235 if (tty_port_initialized(port))
236 return 0;
237
238 retval = uart_port_startup(tty, state, init_hw);
239 if (retval)
240 set_bit(TTY_IO_ERROR, &tty->flags);
241
242 return retval;
243}
244
245
246
247
248
249
250
251
252static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
253{
254 struct uart_port *uport = uart_port_check(state);
255 struct tty_port *port = &state->port;
256
257
258
259
260 if (tty)
261 set_bit(TTY_IO_ERROR, &tty->flags);
262
263 if (tty_port_initialized(port)) {
264 tty_port_set_initialized(port, 0);
265
266
267
268
269 if (uport && uart_console(uport) && tty)
270 uport->cons->cflag = tty->termios.c_cflag;
271
272 if (!tty || C_HUPCL(tty))
273 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
274
275 uart_port_shutdown(port);
276 }
277
278
279
280
281
282
283 tty_port_set_suspended(port, 0);
284
285
286
287
288 if (state->xmit.buf) {
289 free_page((unsigned long)state->xmit.buf);
290 state->xmit.buf = NULL;
291 }
292}
293
294
295
296
297
298
299
300
301
302
303void
304uart_update_timeout(struct uart_port *port, unsigned int cflag,
305 unsigned int baud)
306{
307 unsigned int bits;
308
309
310 switch (cflag & CSIZE) {
311 case CS5:
312 bits = 7;
313 break;
314 case CS6:
315 bits = 8;
316 break;
317 case CS7:
318 bits = 9;
319 break;
320 default:
321 bits = 10;
322 break;
323 }
324
325 if (cflag & CSTOPB)
326 bits++;
327 if (cflag & PARENB)
328 bits++;
329
330
331
332
333 bits = bits * port->fifosize;
334
335
336
337
338
339 port->timeout = (HZ * bits) / baud + HZ/50;
340}
341
342EXPORT_SYMBOL(uart_update_timeout);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363unsigned int
364uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
365 struct ktermios *old, unsigned int min, unsigned int max)
366{
367 unsigned int try;
368 unsigned int baud;
369 unsigned int altbaud;
370 int hung_up = 0;
371 upf_t flags = port->flags & UPF_SPD_MASK;
372
373 switch (flags) {
374 case UPF_SPD_HI:
375 altbaud = 57600;
376 break;
377 case UPF_SPD_VHI:
378 altbaud = 115200;
379 break;
380 case UPF_SPD_SHI:
381 altbaud = 230400;
382 break;
383 case UPF_SPD_WARP:
384 altbaud = 460800;
385 break;
386 default:
387 altbaud = 38400;
388 break;
389 }
390
391 for (try = 0; try < 2; try++) {
392 baud = tty_termios_baud_rate(termios);
393
394
395
396
397
398 if (try == 0 && baud == 38400)
399 baud = altbaud;
400
401
402
403
404 if (baud == 0) {
405 hung_up = 1;
406 baud = 9600;
407 }
408
409 if (baud >= min && baud <= max)
410 return baud;
411
412
413
414
415
416 termios->c_cflag &= ~CBAUD;
417 if (old) {
418 baud = tty_termios_baud_rate(old);
419 if (!hung_up)
420 tty_termios_encode_baud_rate(termios,
421 baud, baud);
422 old = NULL;
423 continue;
424 }
425
426
427
428
429
430 if (!hung_up) {
431 if (baud <= min)
432 tty_termios_encode_baud_rate(termios,
433 min + 1, min + 1);
434 else
435 tty_termios_encode_baud_rate(termios,
436 max - 1, max - 1);
437 }
438 }
439
440 WARN_ON(1);
441 return 0;
442}
443
444EXPORT_SYMBOL(uart_get_baud_rate);
445
446
447
448
449
450
451
452
453unsigned int
454uart_get_divisor(struct uart_port *port, unsigned int baud)
455{
456 unsigned int quot;
457
458
459
460
461 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
462 quot = port->custom_divisor;
463 else
464 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
465
466 return quot;
467}
468
469EXPORT_SYMBOL(uart_get_divisor);
470
471
472static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
473 struct ktermios *old_termios)
474{
475 struct uart_port *uport = uart_port_check(state);
476 struct ktermios *termios;
477 int hw_stopped;
478
479
480
481
482
483 if (!tty || uport->type == PORT_UNKNOWN)
484 return;
485
486 termios = &tty->termios;
487 uport->ops->set_termios(uport, termios, old_termios);
488
489
490
491
492 spin_lock_irq(&uport->lock);
493 if (termios->c_cflag & CRTSCTS)
494 uport->status |= UPSTAT_CTS_ENABLE;
495 else
496 uport->status &= ~UPSTAT_CTS_ENABLE;
497
498 if (termios->c_cflag & CLOCAL)
499 uport->status &= ~UPSTAT_DCD_ENABLE;
500 else
501 uport->status |= UPSTAT_DCD_ENABLE;
502
503
504 hw_stopped = uport->hw_stopped;
505 uport->hw_stopped = uart_softcts_mode(uport) &&
506 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
507 if (uport->hw_stopped) {
508 if (!hw_stopped)
509 uport->ops->stop_tx(uport);
510 } else {
511 if (hw_stopped)
512 __uart_start(tty);
513 }
514 spin_unlock_irq(&uport->lock);
515}
516
517static int uart_put_char(struct tty_struct *tty, unsigned char c)
518{
519 struct uart_state *state = tty->driver_data;
520 struct uart_port *port;
521 struct circ_buf *circ;
522 unsigned long flags;
523 int ret = 0;
524
525 circ = &state->xmit;
526 if (!circ->buf)
527 return 0;
528
529 port = uart_port_lock(state, flags);
530 if (port && uart_circ_chars_free(circ) != 0) {
531 circ->buf[circ->head] = c;
532 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
533 ret = 1;
534 }
535 uart_port_unlock(port, flags);
536 return ret;
537}
538
539static void uart_flush_chars(struct tty_struct *tty)
540{
541 uart_start(tty);
542}
543
544static int uart_write(struct tty_struct *tty,
545 const unsigned char *buf, int count)
546{
547 struct uart_state *state = tty->driver_data;
548 struct uart_port *port;
549 struct circ_buf *circ;
550 unsigned long flags;
551 int c, ret = 0;
552
553
554
555
556
557 if (!state) {
558 WARN_ON(1);
559 return -EL3HLT;
560 }
561
562 circ = &state->xmit;
563 if (!circ->buf)
564 return 0;
565
566 port = uart_port_lock(state, flags);
567 while (port) {
568 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
569 if (count < c)
570 c = count;
571 if (c <= 0)
572 break;
573 memcpy(circ->buf + circ->head, buf, c);
574 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
575 buf += c;
576 count -= c;
577 ret += c;
578 }
579
580 __uart_start(tty);
581 uart_port_unlock(port, flags);
582 return ret;
583}
584
585static int uart_write_room(struct tty_struct *tty)
586{
587 struct uart_state *state = tty->driver_data;
588 struct uart_port *port;
589 unsigned long flags;
590 int ret;
591
592 port = uart_port_lock(state, flags);
593 ret = uart_circ_chars_free(&state->xmit);
594 uart_port_unlock(port, flags);
595 return ret;
596}
597
598static int uart_chars_in_buffer(struct tty_struct *tty)
599{
600 struct uart_state *state = tty->driver_data;
601 struct uart_port *port;
602 unsigned long flags;
603 int ret;
604
605 port = uart_port_lock(state, flags);
606 ret = uart_circ_chars_pending(&state->xmit);
607 uart_port_unlock(port, flags);
608 return ret;
609}
610
611static void uart_flush_buffer(struct tty_struct *tty)
612{
613 struct uart_state *state = tty->driver_data;
614 struct uart_port *port;
615 unsigned long flags;
616
617
618
619
620
621 if (!state) {
622 WARN_ON(1);
623 return;
624 }
625
626 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
627
628 port = uart_port_lock(state, flags);
629 if (!port)
630 return;
631 uart_circ_clear(&state->xmit);
632 if (port->ops->flush_buffer)
633 port->ops->flush_buffer(port);
634 uart_port_unlock(port, flags);
635 tty_port_tty_wakeup(&state->port);
636}
637
638
639
640
641
642static void uart_send_xchar(struct tty_struct *tty, char ch)
643{
644 struct uart_state *state = tty->driver_data;
645 struct uart_port *port;
646 unsigned long flags;
647
648 port = uart_port_ref(state);
649 if (!port)
650 return;
651
652 if (port->ops->send_xchar)
653 port->ops->send_xchar(port, ch);
654 else {
655 spin_lock_irqsave(&port->lock, flags);
656 port->x_char = ch;
657 if (ch)
658 port->ops->start_tx(port);
659 spin_unlock_irqrestore(&port->lock, flags);
660 }
661 uart_port_deref(port);
662}
663
664static void uart_throttle(struct tty_struct *tty)
665{
666 struct uart_state *state = tty->driver_data;
667 struct uart_port *port;
668 upstat_t mask = 0;
669
670 port = uart_port_ref(state);
671 if (!port)
672 return;
673
674 if (I_IXOFF(tty))
675 mask |= UPSTAT_AUTOXOFF;
676 if (C_CRTSCTS(tty))
677 mask |= UPSTAT_AUTORTS;
678
679 if (port->status & mask) {
680 port->ops->throttle(port);
681 mask &= ~port->status;
682 }
683
684 if (mask & UPSTAT_AUTORTS)
685 uart_clear_mctrl(port, TIOCM_RTS);
686
687 if (mask & UPSTAT_AUTOXOFF)
688 uart_send_xchar(tty, STOP_CHAR(tty));
689
690 uart_port_deref(port);
691}
692
693static void uart_unthrottle(struct tty_struct *tty)
694{
695 struct uart_state *state = tty->driver_data;
696 struct uart_port *port;
697 upstat_t mask = 0;
698
699 port = uart_port_ref(state);
700 if (!port)
701 return;
702
703 if (I_IXOFF(tty))
704 mask |= UPSTAT_AUTOXOFF;
705 if (C_CRTSCTS(tty))
706 mask |= UPSTAT_AUTORTS;
707
708 if (port->status & mask) {
709 port->ops->unthrottle(port);
710 mask &= ~port->status;
711 }
712
713 if (mask & UPSTAT_AUTORTS)
714 uart_set_mctrl(port, TIOCM_RTS);
715
716 if (mask & UPSTAT_AUTOXOFF)
717 uart_send_xchar(tty, START_CHAR(tty));
718
719 uart_port_deref(port);
720}
721
722static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
723{
724 struct uart_state *state = container_of(port, struct uart_state, port);
725 struct uart_port *uport;
726 int ret = -ENODEV;
727
728 memset(retinfo, 0, sizeof(*retinfo));
729
730
731
732
733
734 mutex_lock(&port->mutex);
735 uport = uart_port_check(state);
736 if (!uport)
737 goto out;
738
739 retinfo->type = uport->type;
740 retinfo->line = uport->line;
741 retinfo->port = uport->iobase;
742 if (HIGH_BITS_OFFSET)
743 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
744 retinfo->irq = uport->irq;
745 retinfo->flags = uport->flags;
746 retinfo->xmit_fifo_size = uport->fifosize;
747 retinfo->baud_base = uport->uartclk / 16;
748 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
749 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
750 ASYNC_CLOSING_WAIT_NONE :
751 jiffies_to_msecs(port->closing_wait) / 10;
752 retinfo->custom_divisor = uport->custom_divisor;
753 retinfo->hub6 = uport->hub6;
754 retinfo->io_type = uport->iotype;
755 retinfo->iomem_reg_shift = uport->regshift;
756 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
757
758 ret = 0;
759out:
760 mutex_unlock(&port->mutex);
761 return ret;
762}
763
764static int uart_get_info_user(struct tty_port *port,
765 struct serial_struct __user *retinfo)
766{
767 struct serial_struct tmp;
768
769 if (uart_get_info(port, &tmp) < 0)
770 return -EIO;
771
772 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
773 return -EFAULT;
774 return 0;
775}
776
777static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
778 struct uart_state *state,
779 struct serial_struct *new_info)
780{
781 struct uart_port *uport = uart_port_check(state);
782 unsigned long new_port;
783 unsigned int change_irq, change_port, closing_wait;
784 unsigned int old_custom_divisor, close_delay;
785 upf_t old_flags, new_flags;
786 int retval = 0;
787
788 if (!uport)
789 return -EIO;
790
791 new_port = new_info->port;
792 if (HIGH_BITS_OFFSET)
793 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
794
795 new_info->irq = irq_canonicalize(new_info->irq);
796 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
797 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
798 ASYNC_CLOSING_WAIT_NONE :
799 msecs_to_jiffies(new_info->closing_wait * 10);
800
801
802 change_irq = !(uport->flags & UPF_FIXED_PORT)
803 && new_info->irq != uport->irq;
804
805
806
807
808
809
810 change_port = !(uport->flags & UPF_FIXED_PORT)
811 && (new_port != uport->iobase ||
812 (unsigned long)new_info->iomem_base != uport->mapbase ||
813 new_info->hub6 != uport->hub6 ||
814 new_info->io_type != uport->iotype ||
815 new_info->iomem_reg_shift != uport->regshift ||
816 new_info->type != uport->type);
817
818 old_flags = uport->flags;
819 new_flags = new_info->flags;
820 old_custom_divisor = uport->custom_divisor;
821
822 if (!capable(CAP_SYS_ADMIN)) {
823 retval = -EPERM;
824 if (change_irq || change_port ||
825 (new_info->baud_base != uport->uartclk / 16) ||
826 (close_delay != port->close_delay) ||
827 (closing_wait != port->closing_wait) ||
828 (new_info->xmit_fifo_size &&
829 new_info->xmit_fifo_size != uport->fifosize) ||
830 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
831 goto exit;
832 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
833 (new_flags & UPF_USR_MASK));
834 uport->custom_divisor = new_info->custom_divisor;
835 goto check_and_exit;
836 }
837
838
839
840
841 if (uport->ops->verify_port)
842 retval = uport->ops->verify_port(uport, new_info);
843
844 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
845 (new_info->baud_base < 9600))
846 retval = -EINVAL;
847
848 if (retval)
849 goto exit;
850
851 if (change_port || change_irq) {
852 retval = -EBUSY;
853
854
855
856
857 if (tty_port_users(port) > 1)
858 goto exit;
859
860
861
862
863
864 uart_shutdown(tty, state);
865 }
866
867 if (change_port) {
868 unsigned long old_iobase, old_mapbase;
869 unsigned int old_type, old_iotype, old_hub6, old_shift;
870
871 old_iobase = uport->iobase;
872 old_mapbase = uport->mapbase;
873 old_type = uport->type;
874 old_hub6 = uport->hub6;
875 old_iotype = uport->iotype;
876 old_shift = uport->regshift;
877
878
879
880
881 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
882 uport->ops->release_port(uport);
883
884 uport->iobase = new_port;
885 uport->type = new_info->type;
886 uport->hub6 = new_info->hub6;
887 uport->iotype = new_info->io_type;
888 uport->regshift = new_info->iomem_reg_shift;
889 uport->mapbase = (unsigned long)new_info->iomem_base;
890
891
892
893
894 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
895 retval = uport->ops->request_port(uport);
896 } else {
897
898 retval = 0;
899 }
900
901
902
903
904
905 if (retval) {
906 uport->iobase = old_iobase;
907 uport->type = old_type;
908 uport->hub6 = old_hub6;
909 uport->iotype = old_iotype;
910 uport->regshift = old_shift;
911 uport->mapbase = old_mapbase;
912
913 if (old_type != PORT_UNKNOWN) {
914 retval = uport->ops->request_port(uport);
915
916
917
918
919 if (retval)
920 uport->type = PORT_UNKNOWN;
921
922
923
924
925 retval = -EBUSY;
926 }
927
928
929 goto exit;
930 }
931 }
932
933 if (change_irq)
934 uport->irq = new_info->irq;
935 if (!(uport->flags & UPF_FIXED_PORT))
936 uport->uartclk = new_info->baud_base * 16;
937 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
938 (new_flags & UPF_CHANGE_MASK);
939 uport->custom_divisor = new_info->custom_divisor;
940 port->close_delay = close_delay;
941 port->closing_wait = closing_wait;
942 if (new_info->xmit_fifo_size)
943 uport->fifosize = new_info->xmit_fifo_size;
944 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
945
946 check_and_exit:
947 retval = 0;
948 if (uport->type == PORT_UNKNOWN)
949 goto exit;
950 if (tty_port_initialized(port)) {
951 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
952 old_custom_divisor != uport->custom_divisor) {
953
954
955
956
957
958 if (uport->flags & UPF_SPD_MASK) {
959 dev_notice(uport->dev,
960 "%s sets custom speed on %s. This is deprecated.\n",
961 current->comm,
962 tty_name(port->tty));
963 }
964 uart_change_speed(tty, state, NULL);
965 }
966 } else {
967 retval = uart_startup(tty, state, 1);
968 if (retval > 0)
969 retval = 0;
970 }
971 exit:
972 return retval;
973}
974
975static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
976 struct serial_struct __user *newinfo)
977{
978 struct serial_struct new_serial;
979 struct tty_port *port = &state->port;
980 int retval;
981
982 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
983 return -EFAULT;
984
985
986
987
988
989
990
991
992 mutex_lock(&port->mutex);
993 retval = uart_set_info(tty, port, state, &new_serial);
994 mutex_unlock(&port->mutex);
995 return retval;
996}
997
998
999
1000
1001
1002
1003
1004static int uart_get_lsr_info(struct tty_struct *tty,
1005 struct uart_state *state, unsigned int __user *value)
1006{
1007 struct uart_port *uport = uart_port_check(state);
1008 unsigned int result;
1009
1010 result = uport->ops->tx_empty(uport);
1011
1012
1013
1014
1015
1016
1017
1018 if (uport->x_char ||
1019 ((uart_circ_chars_pending(&state->xmit) > 0) &&
1020 !uart_tx_stopped(uport)))
1021 result &= ~TIOCSER_TEMT;
1022
1023 return put_user(result, value);
1024}
1025
1026static int uart_tiocmget(struct tty_struct *tty)
1027{
1028 struct uart_state *state = tty->driver_data;
1029 struct tty_port *port = &state->port;
1030 struct uart_port *uport;
1031 int result = -EIO;
1032
1033 mutex_lock(&port->mutex);
1034 uport = uart_port_check(state);
1035 if (!uport)
1036 goto out;
1037
1038 if (!tty_io_error(tty)) {
1039 result = uport->mctrl;
1040 spin_lock_irq(&uport->lock);
1041 result |= uport->ops->get_mctrl(uport);
1042 spin_unlock_irq(&uport->lock);
1043 }
1044out:
1045 mutex_unlock(&port->mutex);
1046 return result;
1047}
1048
1049static int
1050uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1051{
1052 struct uart_state *state = tty->driver_data;
1053 struct tty_port *port = &state->port;
1054 struct uart_port *uport;
1055 int ret = -EIO;
1056
1057 mutex_lock(&port->mutex);
1058 uport = uart_port_check(state);
1059 if (!uport)
1060 goto out;
1061
1062 if (!tty_io_error(tty)) {
1063 uart_update_mctrl(uport, set, clear);
1064 ret = 0;
1065 }
1066out:
1067 mutex_unlock(&port->mutex);
1068 return ret;
1069}
1070
1071static int uart_break_ctl(struct tty_struct *tty, int break_state)
1072{
1073 struct uart_state *state = tty->driver_data;
1074 struct tty_port *port = &state->port;
1075 struct uart_port *uport;
1076 int ret = -EIO;
1077
1078 mutex_lock(&port->mutex);
1079 uport = uart_port_check(state);
1080 if (!uport)
1081 goto out;
1082
1083 if (uport->type != PORT_UNKNOWN)
1084 uport->ops->break_ctl(uport, break_state);
1085 ret = 0;
1086out:
1087 mutex_unlock(&port->mutex);
1088 return ret;
1089}
1090
1091static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1092{
1093 struct tty_port *port = &state->port;
1094 struct uart_port *uport;
1095 int flags, ret;
1096
1097 if (!capable(CAP_SYS_ADMIN))
1098 return -EPERM;
1099
1100
1101
1102
1103
1104
1105 if (mutex_lock_interruptible(&port->mutex))
1106 return -ERESTARTSYS;
1107
1108 uport = uart_port_check(state);
1109 if (!uport) {
1110 ret = -EIO;
1111 goto out;
1112 }
1113
1114 ret = -EBUSY;
1115 if (tty_port_users(port) == 1) {
1116 uart_shutdown(tty, state);
1117
1118
1119
1120
1121
1122 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1123 uport->ops->release_port(uport);
1124
1125 flags = UART_CONFIG_TYPE;
1126 if (uport->flags & UPF_AUTO_IRQ)
1127 flags |= UART_CONFIG_IRQ;
1128
1129
1130
1131
1132
1133 uport->ops->config_port(uport, flags);
1134
1135 ret = uart_startup(tty, state, 1);
1136 if (ret > 0)
1137 ret = 0;
1138 }
1139out:
1140 mutex_unlock(&port->mutex);
1141 return ret;
1142}
1143
1144static void uart_enable_ms(struct uart_port *uport)
1145{
1146
1147
1148
1149 if (uport->ops->enable_ms)
1150 uport->ops->enable_ms(uport);
1151}
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1163{
1164 struct uart_port *uport;
1165 struct tty_port *port = &state->port;
1166 DECLARE_WAITQUEUE(wait, current);
1167 struct uart_icount cprev, cnow;
1168 int ret;
1169
1170
1171
1172
1173 uport = uart_port_ref(state);
1174 if (!uport)
1175 return -EIO;
1176 spin_lock_irq(&uport->lock);
1177 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1178 uart_enable_ms(uport);
1179 spin_unlock_irq(&uport->lock);
1180
1181 add_wait_queue(&port->delta_msr_wait, &wait);
1182 for (;;) {
1183 spin_lock_irq(&uport->lock);
1184 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1185 spin_unlock_irq(&uport->lock);
1186
1187 set_current_state(TASK_INTERRUPTIBLE);
1188
1189 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1190 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1191 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1192 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1193 ret = 0;
1194 break;
1195 }
1196
1197 schedule();
1198
1199
1200 if (signal_pending(current)) {
1201 ret = -ERESTARTSYS;
1202 break;
1203 }
1204
1205 cprev = cnow;
1206 }
1207 __set_current_state(TASK_RUNNING);
1208 remove_wait_queue(&port->delta_msr_wait, &wait);
1209 uart_port_deref(uport);
1210
1211 return ret;
1212}
1213
1214
1215
1216
1217
1218
1219
1220static int uart_get_icount(struct tty_struct *tty,
1221 struct serial_icounter_struct *icount)
1222{
1223 struct uart_state *state = tty->driver_data;
1224 struct uart_icount cnow;
1225 struct uart_port *uport;
1226
1227 uport = uart_port_ref(state);
1228 if (!uport)
1229 return -EIO;
1230 spin_lock_irq(&uport->lock);
1231 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1232 spin_unlock_irq(&uport->lock);
1233 uart_port_deref(uport);
1234
1235 icount->cts = cnow.cts;
1236 icount->dsr = cnow.dsr;
1237 icount->rng = cnow.rng;
1238 icount->dcd = cnow.dcd;
1239 icount->rx = cnow.rx;
1240 icount->tx = cnow.tx;
1241 icount->frame = cnow.frame;
1242 icount->overrun = cnow.overrun;
1243 icount->parity = cnow.parity;
1244 icount->brk = cnow.brk;
1245 icount->buf_overrun = cnow.buf_overrun;
1246
1247 return 0;
1248}
1249
1250static int uart_get_rs485_config(struct uart_port *port,
1251 struct serial_rs485 __user *rs485)
1252{
1253 unsigned long flags;
1254 struct serial_rs485 aux;
1255
1256 spin_lock_irqsave(&port->lock, flags);
1257 aux = port->rs485;
1258 spin_unlock_irqrestore(&port->lock, flags);
1259
1260 if (copy_to_user(rs485, &aux, sizeof(aux)))
1261 return -EFAULT;
1262
1263 return 0;
1264}
1265
1266static int uart_set_rs485_config(struct uart_port *port,
1267 struct serial_rs485 __user *rs485_user)
1268{
1269 struct serial_rs485 rs485;
1270 int ret;
1271 unsigned long flags;
1272
1273 if (!port->rs485_config)
1274 return -ENOIOCTLCMD;
1275
1276 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1277 return -EFAULT;
1278
1279 spin_lock_irqsave(&port->lock, flags);
1280 ret = port->rs485_config(port, &rs485);
1281 spin_unlock_irqrestore(&port->lock, flags);
1282 if (ret)
1283 return ret;
1284
1285 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1286 return -EFAULT;
1287
1288 return 0;
1289}
1290
1291
1292
1293
1294static int
1295uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1296{
1297 struct uart_state *state = tty->driver_data;
1298 struct tty_port *port = &state->port;
1299 struct uart_port *uport;
1300 void __user *uarg = (void __user *)arg;
1301 int ret = -ENOIOCTLCMD;
1302
1303
1304
1305
1306
1307 switch (cmd) {
1308 case TIOCGSERIAL:
1309 ret = uart_get_info_user(port, uarg);
1310 break;
1311
1312 case TIOCSSERIAL:
1313 down_write(&tty->termios_rwsem);
1314 ret = uart_set_info_user(tty, state, uarg);
1315 up_write(&tty->termios_rwsem);
1316 break;
1317
1318 case TIOCSERCONFIG:
1319 down_write(&tty->termios_rwsem);
1320 ret = uart_do_autoconfig(tty, state);
1321 up_write(&tty->termios_rwsem);
1322 break;
1323
1324 case TIOCSERGWILD:
1325 case TIOCSERSWILD:
1326 ret = 0;
1327 break;
1328 }
1329
1330 if (ret != -ENOIOCTLCMD)
1331 goto out;
1332
1333 if (tty_io_error(tty)) {
1334 ret = -EIO;
1335 goto out;
1336 }
1337
1338
1339
1340
1341 switch (cmd) {
1342 case TIOCMIWAIT:
1343 ret = uart_wait_modem_status(state, arg);
1344 break;
1345 }
1346
1347 if (ret != -ENOIOCTLCMD)
1348 goto out;
1349
1350 mutex_lock(&port->mutex);
1351 uport = uart_port_check(state);
1352
1353 if (!uport || tty_io_error(tty)) {
1354 ret = -EIO;
1355 goto out_up;
1356 }
1357
1358
1359
1360
1361
1362
1363 switch (cmd) {
1364 case TIOCSERGETLSR:
1365 ret = uart_get_lsr_info(tty, state, uarg);
1366 break;
1367
1368 case TIOCGRS485:
1369 ret = uart_get_rs485_config(uport, uarg);
1370 break;
1371
1372 case TIOCSRS485:
1373 ret = uart_set_rs485_config(uport, uarg);
1374 break;
1375 default:
1376 if (uport->ops->ioctl)
1377 ret = uport->ops->ioctl(uport, cmd, arg);
1378 break;
1379 }
1380out_up:
1381 mutex_unlock(&port->mutex);
1382out:
1383 return ret;
1384}
1385
1386static void uart_set_ldisc(struct tty_struct *tty)
1387{
1388 struct uart_state *state = tty->driver_data;
1389 struct uart_port *uport;
1390
1391 mutex_lock(&state->port.mutex);
1392 uport = uart_port_check(state);
1393 if (uport && uport->ops->set_ldisc)
1394 uport->ops->set_ldisc(uport, &tty->termios);
1395 mutex_unlock(&state->port.mutex);
1396}
1397
1398static void uart_set_termios(struct tty_struct *tty,
1399 struct ktermios *old_termios)
1400{
1401 struct uart_state *state = tty->driver_data;
1402 struct uart_port *uport;
1403 unsigned int cflag = tty->termios.c_cflag;
1404 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1405 bool sw_changed = false;
1406
1407 mutex_lock(&state->port.mutex);
1408 uport = uart_port_check(state);
1409 if (!uport)
1410 goto out;
1411
1412
1413
1414
1415
1416 if (uport->flags & UPF_SOFT_FLOW) {
1417 iflag_mask |= IXANY|IXON|IXOFF;
1418 sw_changed =
1419 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1420 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1421 }
1422
1423
1424
1425
1426
1427
1428
1429 if ((cflag ^ old_termios->c_cflag) == 0 &&
1430 tty->termios.c_ospeed == old_termios->c_ospeed &&
1431 tty->termios.c_ispeed == old_termios->c_ispeed &&
1432 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1433 !sw_changed) {
1434 goto out;
1435 }
1436
1437 uart_change_speed(tty, state, old_termios);
1438
1439 cflag = tty->termios.c_cflag;
1440
1441
1442 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1443 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1444
1445 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1446 unsigned int mask = TIOCM_DTR;
1447 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1448 mask |= TIOCM_RTS;
1449 uart_set_mctrl(uport, mask);
1450 }
1451out:
1452 mutex_unlock(&state->port.mutex);
1453}
1454
1455
1456
1457
1458
1459
1460static void uart_close(struct tty_struct *tty, struct file *filp)
1461{
1462 struct uart_state *state = tty->driver_data;
1463 struct tty_port *port;
1464
1465 if (!state) {
1466 struct uart_driver *drv = tty->driver->driver_state;
1467
1468 state = drv->state + tty->index;
1469 port = &state->port;
1470 spin_lock_irq(&port->lock);
1471 --port->count;
1472 spin_unlock_irq(&port->lock);
1473 return;
1474 }
1475
1476 port = &state->port;
1477 pr_debug("uart_close(%d) called\n", tty->index);
1478
1479 tty_port_close(tty->port, tty, filp);
1480}
1481
1482static void uart_tty_port_shutdown(struct tty_port *port)
1483{
1484 struct uart_state *state = container_of(port, struct uart_state, port);
1485 struct uart_port *uport = uart_port_check(state);
1486
1487
1488
1489
1490
1491 if (WARN(!uport, "detached port still initialized!\n"))
1492 return;
1493
1494 spin_lock_irq(&uport->lock);
1495 uport->ops->stop_rx(uport);
1496 spin_unlock_irq(&uport->lock);
1497
1498 uart_port_shutdown(port);
1499
1500
1501
1502
1503
1504
1505 tty_port_set_suspended(port, 0);
1506
1507 uart_change_pm(state, UART_PM_STATE_OFF);
1508
1509}
1510
1511static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1512{
1513 struct uart_state *state = tty->driver_data;
1514 struct uart_port *port;
1515 unsigned long char_time, expire;
1516
1517 port = uart_port_ref(state);
1518 if (!port || port->type == PORT_UNKNOWN || port->fifosize == 0) {
1519 uart_port_deref(port);
1520 return;
1521 }
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531 char_time = (port->timeout - HZ/50) / port->fifosize;
1532 char_time = char_time / 5;
1533 if (char_time == 0)
1534 char_time = 1;
1535 if (timeout && timeout < char_time)
1536 char_time = timeout;
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547 if (timeout == 0 || timeout > 2 * port->timeout)
1548 timeout = 2 * port->timeout;
1549
1550 expire = jiffies + timeout;
1551
1552 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1553 port->line, jiffies, expire);
1554
1555
1556
1557
1558
1559
1560 while (!port->ops->tx_empty(port)) {
1561 msleep_interruptible(jiffies_to_msecs(char_time));
1562 if (signal_pending(current))
1563 break;
1564 if (time_after(jiffies, expire))
1565 break;
1566 }
1567 uart_port_deref(port);
1568}
1569
1570
1571
1572
1573
1574
1575static void uart_hangup(struct tty_struct *tty)
1576{
1577 struct uart_state *state = tty->driver_data;
1578 struct tty_port *port = &state->port;
1579 struct uart_port *uport;
1580 unsigned long flags;
1581
1582 pr_debug("uart_hangup(%d)\n", tty->index);
1583
1584 mutex_lock(&port->mutex);
1585 uport = uart_port_check(state);
1586 WARN(!uport, "hangup of detached port!\n");
1587
1588 if (tty_port_active(port)) {
1589 uart_flush_buffer(tty);
1590 uart_shutdown(tty, state);
1591 spin_lock_irqsave(&port->lock, flags);
1592 port->count = 0;
1593 spin_unlock_irqrestore(&port->lock, flags);
1594 tty_port_set_active(port, 0);
1595 tty_port_tty_set(port, NULL);
1596 if (uport && !uart_console(uport))
1597 uart_change_pm(state, UART_PM_STATE_OFF);
1598 wake_up_interruptible(&port->open_wait);
1599 wake_up_interruptible(&port->delta_msr_wait);
1600 }
1601 mutex_unlock(&port->mutex);
1602}
1603
1604
1605static void uart_port_shutdown(struct tty_port *port)
1606{
1607 struct uart_state *state = container_of(port, struct uart_state, port);
1608 struct uart_port *uport = uart_port_check(state);
1609
1610
1611
1612
1613
1614
1615
1616
1617 wake_up_interruptible(&port->delta_msr_wait);
1618
1619
1620
1621
1622 if (uport)
1623 uport->ops->shutdown(uport);
1624
1625
1626
1627
1628 if (uport)
1629 synchronize_irq(uport->irq);
1630}
1631
1632static int uart_carrier_raised(struct tty_port *port)
1633{
1634 struct uart_state *state = container_of(port, struct uart_state, port);
1635 struct uart_port *uport;
1636 int mctrl;
1637
1638 uport = uart_port_ref(state);
1639
1640
1641
1642
1643
1644
1645 if (WARN_ON(!uport))
1646 return 1;
1647 spin_lock_irq(&uport->lock);
1648 uart_enable_ms(uport);
1649 mctrl = uport->ops->get_mctrl(uport);
1650 spin_unlock_irq(&uport->lock);
1651 uart_port_deref(uport);
1652 if (mctrl & TIOCM_CAR)
1653 return 1;
1654 return 0;
1655}
1656
1657static void uart_dtr_rts(struct tty_port *port, int onoff)
1658{
1659 struct uart_state *state = container_of(port, struct uart_state, port);
1660 struct uart_port *uport;
1661
1662 uport = uart_port_ref(state);
1663 if (!uport)
1664 return;
1665
1666 if (onoff)
1667 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1668 else
1669 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1670
1671 uart_port_deref(uport);
1672}
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684static int uart_open(struct tty_struct *tty, struct file *filp)
1685{
1686 struct uart_driver *drv = tty->driver->driver_state;
1687 int retval, line = tty->index;
1688 struct uart_state *state = drv->state + line;
1689
1690 tty->driver_data = state;
1691
1692 retval = tty_port_open(&state->port, tty, filp);
1693 if (retval > 0)
1694 retval = 0;
1695
1696 return retval;
1697}
1698
1699static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1700{
1701 struct uart_state *state = container_of(port, struct uart_state, port);
1702 struct uart_port *uport;
1703
1704 uport = uart_port_check(state);
1705 if (!uport || uport->flags & UPF_DEAD)
1706 return -ENXIO;
1707
1708 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
1709
1710
1711
1712
1713 return uart_startup(tty, state, 0);
1714}
1715
1716static const char *uart_type(struct uart_port *port)
1717{
1718 const char *str = NULL;
1719
1720 if (port->ops->type)
1721 str = port->ops->type(port);
1722
1723 if (!str)
1724 str = "unknown";
1725
1726 return str;
1727}
1728
1729#ifdef CONFIG_PROC_FS
1730
1731static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1732{
1733 struct uart_state *state = drv->state + i;
1734 struct tty_port *port = &state->port;
1735 enum uart_pm_state pm_state;
1736 struct uart_port *uport;
1737 char stat_buf[32];
1738 unsigned int status;
1739 int mmio;
1740
1741 mutex_lock(&port->mutex);
1742 uport = uart_port_check(state);
1743 if (!uport)
1744 goto out;
1745
1746 mmio = uport->iotype >= UPIO_MEM;
1747 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1748 uport->line, uart_type(uport),
1749 mmio ? "mmio:0x" : "port:",
1750 mmio ? (unsigned long long)uport->mapbase
1751 : (unsigned long long)uport->iobase,
1752 uport->irq);
1753
1754 if (uport->type == PORT_UNKNOWN) {
1755 seq_putc(m, '\n');
1756 goto out;
1757 }
1758
1759 if (capable(CAP_SYS_ADMIN)) {
1760 pm_state = state->pm_state;
1761 if (pm_state != UART_PM_STATE_ON)
1762 uart_change_pm(state, UART_PM_STATE_ON);
1763 spin_lock_irq(&uport->lock);
1764 status = uport->ops->get_mctrl(uport);
1765 spin_unlock_irq(&uport->lock);
1766 if (pm_state != UART_PM_STATE_ON)
1767 uart_change_pm(state, pm_state);
1768
1769 seq_printf(m, " tx:%d rx:%d",
1770 uport->icount.tx, uport->icount.rx);
1771 if (uport->icount.frame)
1772 seq_printf(m, " fe:%d", uport->icount.frame);
1773 if (uport->icount.parity)
1774 seq_printf(m, " pe:%d", uport->icount.parity);
1775 if (uport->icount.brk)
1776 seq_printf(m, " brk:%d", uport->icount.brk);
1777 if (uport->icount.overrun)
1778 seq_printf(m, " oe:%d", uport->icount.overrun);
1779
1780#define INFOBIT(bit, str) \
1781 if (uport->mctrl & (bit)) \
1782 strncat(stat_buf, (str), sizeof(stat_buf) - \
1783 strlen(stat_buf) - 2)
1784#define STATBIT(bit, str) \
1785 if (status & (bit)) \
1786 strncat(stat_buf, (str), sizeof(stat_buf) - \
1787 strlen(stat_buf) - 2)
1788
1789 stat_buf[0] = '\0';
1790 stat_buf[1] = '\0';
1791 INFOBIT(TIOCM_RTS, "|RTS");
1792 STATBIT(TIOCM_CTS, "|CTS");
1793 INFOBIT(TIOCM_DTR, "|DTR");
1794 STATBIT(TIOCM_DSR, "|DSR");
1795 STATBIT(TIOCM_CAR, "|CD");
1796 STATBIT(TIOCM_RNG, "|RI");
1797 if (stat_buf[0])
1798 stat_buf[0] = ' ';
1799
1800 seq_puts(m, stat_buf);
1801 }
1802 seq_putc(m, '\n');
1803#undef STATBIT
1804#undef INFOBIT
1805out:
1806 mutex_unlock(&port->mutex);
1807}
1808
1809static int uart_proc_show(struct seq_file *m, void *v)
1810{
1811 struct tty_driver *ttydrv = m->private;
1812 struct uart_driver *drv = ttydrv->driver_state;
1813 int i;
1814
1815 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
1816 for (i = 0; i < drv->nr; i++)
1817 uart_line_info(m, drv, i);
1818 return 0;
1819}
1820
1821static int uart_proc_open(struct inode *inode, struct file *file)
1822{
1823 return single_open(file, uart_proc_show, PDE_DATA(inode));
1824}
1825
1826static const struct file_operations uart_proc_fops = {
1827 .owner = THIS_MODULE,
1828 .open = uart_proc_open,
1829 .read = seq_read,
1830 .llseek = seq_lseek,
1831 .release = single_release,
1832};
1833#endif
1834
1835#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1836
1837
1838
1839
1840
1841
1842
1843void uart_console_write(struct uart_port *port, const char *s,
1844 unsigned int count,
1845 void (*putchar)(struct uart_port *, int))
1846{
1847 unsigned int i;
1848
1849 for (i = 0; i < count; i++, s++) {
1850 if (*s == '\n')
1851 putchar(port, '\r');
1852 putchar(port, *s);
1853 }
1854}
1855EXPORT_SYMBOL_GPL(uart_console_write);
1856
1857
1858
1859
1860
1861
1862struct uart_port * __init
1863uart_get_console(struct uart_port *ports, int nr, struct console *co)
1864{
1865 int idx = co->index;
1866
1867 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1868 ports[idx].membase == NULL))
1869 for (idx = 0; idx < nr; idx++)
1870 if (ports[idx].iobase != 0 ||
1871 ports[idx].membase != NULL)
1872 break;
1873
1874 co->index = idx;
1875
1876 return ports + idx;
1877}
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
1898 char **options)
1899{
1900 if (strncmp(p, "mmio,", 5) == 0) {
1901 *iotype = UPIO_MEM;
1902 p += 5;
1903 } else if (strncmp(p, "mmio16,", 7) == 0) {
1904 *iotype = UPIO_MEM16;
1905 p += 7;
1906 } else if (strncmp(p, "mmio32,", 7) == 0) {
1907 *iotype = UPIO_MEM32;
1908 p += 7;
1909 } else if (strncmp(p, "mmio32be,", 9) == 0) {
1910 *iotype = UPIO_MEM32BE;
1911 p += 9;
1912 } else if (strncmp(p, "mmio32native,", 13) == 0) {
1913 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
1914 UPIO_MEM32BE : UPIO_MEM32;
1915 p += 13;
1916 } else if (strncmp(p, "io,", 3) == 0) {
1917 *iotype = UPIO_PORT;
1918 p += 3;
1919 } else if (strncmp(p, "0x", 2) == 0) {
1920 *iotype = UPIO_MEM;
1921 } else {
1922 return -EINVAL;
1923 }
1924
1925
1926
1927
1928
1929 *addr = simple_strtoull(p, NULL, 0);
1930 p = strchr(p, ',');
1931 if (p)
1932 p++;
1933
1934 *options = p;
1935 return 0;
1936}
1937EXPORT_SYMBOL_GPL(uart_parse_earlycon);
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951void
1952uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1953{
1954 char *s = options;
1955
1956 *baud = simple_strtoul(s, NULL, 10);
1957 while (*s >= '0' && *s <= '9')
1958 s++;
1959 if (*s)
1960 *parity = *s++;
1961 if (*s)
1962 *bits = *s++ - '0';
1963 if (*s)
1964 *flow = *s;
1965}
1966EXPORT_SYMBOL_GPL(uart_parse_options);
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977int
1978uart_set_options(struct uart_port *port, struct console *co,
1979 int baud, int parity, int bits, int flow)
1980{
1981 struct ktermios termios;
1982 static struct ktermios dummy;
1983
1984
1985
1986
1987
1988
1989
1990 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
1991 spin_lock_init(&port->lock);
1992 lockdep_set_class(&port->lock, &port_lock_key);
1993 }
1994
1995 memset(&termios, 0, sizeof(struct ktermios));
1996
1997 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
1998 tty_termios_encode_baud_rate(&termios, baud, baud);
1999
2000 if (bits == 7)
2001 termios.c_cflag |= CS7;
2002 else
2003 termios.c_cflag |= CS8;
2004
2005 switch (parity) {
2006 case 'o': case 'O':
2007 termios.c_cflag |= PARODD;
2008
2009 case 'e': case 'E':
2010 termios.c_cflag |= PARENB;
2011 break;
2012 }
2013
2014 if (flow == 'r')
2015 termios.c_cflag |= CRTSCTS;
2016
2017
2018
2019
2020
2021 port->mctrl |= TIOCM_DTR;
2022
2023 port->ops->set_termios(port, &termios, &dummy);
2024
2025
2026
2027
2028 if (co)
2029 co->cflag = termios.c_cflag;
2030
2031 return 0;
2032}
2033EXPORT_SYMBOL_GPL(uart_set_options);
2034#endif
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044static void uart_change_pm(struct uart_state *state,
2045 enum uart_pm_state pm_state)
2046{
2047 struct uart_port *port = uart_port_check(state);
2048
2049 if (state->pm_state != pm_state) {
2050 if (port && port->ops->pm)
2051 port->ops->pm(port, pm_state, state->pm_state);
2052 state->pm_state = pm_state;
2053 }
2054}
2055
2056struct uart_match {
2057 struct uart_port *port;
2058 struct uart_driver *driver;
2059};
2060
2061static int serial_match_port(struct device *dev, void *data)
2062{
2063 struct uart_match *match = data;
2064 struct tty_driver *tty_drv = match->driver->tty_driver;
2065 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2066 match->port->line;
2067
2068 return dev->devt == devt;
2069}
2070
2071int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2072{
2073 struct uart_state *state = drv->state + uport->line;
2074 struct tty_port *port = &state->port;
2075 struct device *tty_dev;
2076 struct uart_match match = {uport, drv};
2077
2078 mutex_lock(&port->mutex);
2079
2080 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2081 if (device_may_wakeup(tty_dev)) {
2082 if (!enable_irq_wake(uport->irq))
2083 uport->irq_wake = 1;
2084 put_device(tty_dev);
2085 mutex_unlock(&port->mutex);
2086 return 0;
2087 }
2088 put_device(tty_dev);
2089
2090
2091 if (!console_suspend_enabled && uart_console(uport))
2092 goto unlock;
2093
2094 uport->suspended = 1;
2095
2096 if (tty_port_initialized(port)) {
2097 const struct uart_ops *ops = uport->ops;
2098 int tries;
2099
2100 tty_port_set_suspended(port, 1);
2101 tty_port_set_initialized(port, 0);
2102
2103 spin_lock_irq(&uport->lock);
2104 ops->stop_tx(uport);
2105 ops->set_mctrl(uport, 0);
2106 ops->stop_rx(uport);
2107 spin_unlock_irq(&uport->lock);
2108
2109
2110
2111
2112 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2113 msleep(10);
2114 if (!tries)
2115 dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
2116 drv->dev_name,
2117 drv->tty_driver->name_base + uport->line);
2118
2119 ops->shutdown(uport);
2120 }
2121
2122
2123
2124
2125 if (uart_console(uport))
2126 console_stop(uport->cons);
2127
2128 uart_change_pm(state, UART_PM_STATE_OFF);
2129unlock:
2130 mutex_unlock(&port->mutex);
2131
2132 return 0;
2133}
2134
2135int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2136{
2137 struct uart_state *state = drv->state + uport->line;
2138 struct tty_port *port = &state->port;
2139 struct device *tty_dev;
2140 struct uart_match match = {uport, drv};
2141 struct ktermios termios;
2142
2143 mutex_lock(&port->mutex);
2144
2145 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2146 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2147 if (uport->irq_wake) {
2148 disable_irq_wake(uport->irq);
2149 uport->irq_wake = 0;
2150 }
2151 put_device(tty_dev);
2152 mutex_unlock(&port->mutex);
2153 return 0;
2154 }
2155 put_device(tty_dev);
2156 uport->suspended = 0;
2157
2158
2159
2160
2161 if (uart_console(uport)) {
2162
2163
2164
2165 memset(&termios, 0, sizeof(struct ktermios));
2166 termios.c_cflag = uport->cons->cflag;
2167
2168
2169
2170
2171 if (port->tty && termios.c_cflag == 0)
2172 termios = port->tty->termios;
2173
2174 if (console_suspend_enabled)
2175 uart_change_pm(state, UART_PM_STATE_ON);
2176 uport->ops->set_termios(uport, &termios, NULL);
2177 if (console_suspend_enabled)
2178 console_start(uport->cons);
2179 }
2180
2181 if (tty_port_suspended(port)) {
2182 const struct uart_ops *ops = uport->ops;
2183 int ret;
2184
2185 uart_change_pm(state, UART_PM_STATE_ON);
2186 spin_lock_irq(&uport->lock);
2187 ops->set_mctrl(uport, 0);
2188 spin_unlock_irq(&uport->lock);
2189 if (console_suspend_enabled || !uart_console(uport)) {
2190
2191 struct tty_struct *tty = port->tty;
2192 ret = ops->startup(uport);
2193 if (ret == 0) {
2194 if (tty)
2195 uart_change_speed(tty, state, NULL);
2196 spin_lock_irq(&uport->lock);
2197 ops->set_mctrl(uport, uport->mctrl);
2198 ops->start_tx(uport);
2199 spin_unlock_irq(&uport->lock);
2200 tty_port_set_initialized(port, 1);
2201 } else {
2202
2203
2204
2205
2206
2207 uart_shutdown(tty, state);
2208 }
2209 }
2210
2211 tty_port_set_suspended(port, 0);
2212 }
2213
2214 mutex_unlock(&port->mutex);
2215
2216 return 0;
2217}
2218
2219static inline void
2220uart_report_port(struct uart_driver *drv, struct uart_port *port)
2221{
2222 char address[64];
2223
2224 switch (port->iotype) {
2225 case UPIO_PORT:
2226 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2227 break;
2228 case UPIO_HUB6:
2229 snprintf(address, sizeof(address),
2230 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2231 break;
2232 case UPIO_MEM:
2233 case UPIO_MEM16:
2234 case UPIO_MEM32:
2235 case UPIO_MEM32BE:
2236 case UPIO_AU:
2237 case UPIO_TSI:
2238 snprintf(address, sizeof(address),
2239 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2240 break;
2241 default:
2242 strlcpy(address, "*unknown*", sizeof(address));
2243 break;
2244 }
2245
2246 printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2247 port->dev ? dev_name(port->dev) : "",
2248 port->dev ? ": " : "",
2249 drv->dev_name,
2250 drv->tty_driver->name_base + port->line,
2251 address, port->irq, port->uartclk / 16, uart_type(port));
2252}
2253
2254static void
2255uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2256 struct uart_port *port)
2257{
2258 unsigned int flags;
2259
2260
2261
2262
2263 if (!port->iobase && !port->mapbase && !port->membase)
2264 return;
2265
2266
2267
2268
2269
2270 flags = 0;
2271 if (port->flags & UPF_AUTO_IRQ)
2272 flags |= UART_CONFIG_IRQ;
2273 if (port->flags & UPF_BOOT_AUTOCONF) {
2274 if (!(port->flags & UPF_FIXED_TYPE)) {
2275 port->type = PORT_UNKNOWN;
2276 flags |= UART_CONFIG_TYPE;
2277 }
2278 port->ops->config_port(port, flags);
2279 }
2280
2281 if (port->type != PORT_UNKNOWN) {
2282 unsigned long flags;
2283
2284 uart_report_port(drv, port);
2285
2286
2287 uart_change_pm(state, UART_PM_STATE_ON);
2288
2289
2290
2291
2292
2293
2294 spin_lock_irqsave(&port->lock, flags);
2295 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2296 spin_unlock_irqrestore(&port->lock, flags);
2297
2298
2299
2300
2301
2302
2303 if (port->cons && !(port->cons->flags & CON_ENABLED))
2304 register_console(port->cons);
2305
2306
2307
2308
2309
2310 if (!uart_console(port))
2311 uart_change_pm(state, UART_PM_STATE_OFF);
2312 }
2313}
2314
2315#ifdef CONFIG_CONSOLE_POLL
2316
2317static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2318{
2319 struct uart_driver *drv = driver->driver_state;
2320 struct uart_state *state = drv->state + line;
2321 struct tty_port *tport;
2322 struct uart_port *port;
2323 int baud = 9600;
2324 int bits = 8;
2325 int parity = 'n';
2326 int flow = 'n';
2327 int ret = 0;
2328
2329 if (!state)
2330 return -1;
2331
2332 tport = &state->port;
2333 mutex_lock(&tport->mutex);
2334
2335 port = uart_port_check(state);
2336 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
2337 ret = -1;
2338 goto out;
2339 }
2340
2341 if (port->ops->poll_init) {
2342
2343
2344
2345
2346 if (!tty_port_initialized(tport))
2347 ret = port->ops->poll_init(port);
2348 }
2349
2350 if (!ret && options) {
2351 uart_parse_options(options, &baud, &parity, &bits, &flow);
2352 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2353 }
2354out:
2355 mutex_unlock(&tport->mutex);
2356 return ret;
2357}
2358
2359static int uart_poll_get_char(struct tty_driver *driver, int line)
2360{
2361 struct uart_driver *drv = driver->driver_state;
2362 struct uart_state *state = drv->state + line;
2363 struct uart_port *port;
2364 int ret = -1;
2365
2366 if (state) {
2367 port = uart_port_ref(state);
2368 if (port)
2369 ret = port->ops->poll_get_char(port);
2370 uart_port_deref(port);
2371 }
2372 return ret;
2373}
2374
2375static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2376{
2377 struct uart_driver *drv = driver->driver_state;
2378 struct uart_state *state = drv->state + line;
2379 struct uart_port *port;
2380
2381 if (!state)
2382 return;
2383
2384 port = uart_port_ref(state);
2385 if (!port)
2386 return;
2387
2388 if (ch == '\n')
2389 port->ops->poll_put_char(port, '\r');
2390 port->ops->poll_put_char(port, ch);
2391 uart_port_deref(port);
2392}
2393#endif
2394
2395static const struct tty_operations uart_ops = {
2396 .open = uart_open,
2397 .close = uart_close,
2398 .write = uart_write,
2399 .put_char = uart_put_char,
2400 .flush_chars = uart_flush_chars,
2401 .write_room = uart_write_room,
2402 .chars_in_buffer= uart_chars_in_buffer,
2403 .flush_buffer = uart_flush_buffer,
2404 .ioctl = uart_ioctl,
2405 .throttle = uart_throttle,
2406 .unthrottle = uart_unthrottle,
2407 .send_xchar = uart_send_xchar,
2408 .set_termios = uart_set_termios,
2409 .set_ldisc = uart_set_ldisc,
2410 .stop = uart_stop,
2411 .start = uart_start,
2412 .hangup = uart_hangup,
2413 .break_ctl = uart_break_ctl,
2414 .wait_until_sent= uart_wait_until_sent,
2415#ifdef CONFIG_PROC_FS
2416 .proc_fops = &uart_proc_fops,
2417#endif
2418 .tiocmget = uart_tiocmget,
2419 .tiocmset = uart_tiocmset,
2420 .get_icount = uart_get_icount,
2421#ifdef CONFIG_CONSOLE_POLL
2422 .poll_init = uart_poll_init,
2423 .poll_get_char = uart_poll_get_char,
2424 .poll_put_char = uart_poll_put_char,
2425#endif
2426};
2427
2428static const struct tty_port_operations uart_port_ops = {
2429 .carrier_raised = uart_carrier_raised,
2430 .dtr_rts = uart_dtr_rts,
2431 .activate = uart_port_activate,
2432 .shutdown = uart_tty_port_shutdown,
2433};
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448int uart_register_driver(struct uart_driver *drv)
2449{
2450 struct tty_driver *normal;
2451 int i, retval;
2452
2453 BUG_ON(drv->state);
2454
2455
2456
2457
2458
2459 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2460 if (!drv->state)
2461 goto out;
2462
2463 normal = alloc_tty_driver(drv->nr);
2464 if (!normal)
2465 goto out_kfree;
2466
2467 drv->tty_driver = normal;
2468
2469 normal->driver_name = drv->driver_name;
2470 normal->name = drv->dev_name;
2471 normal->major = drv->major;
2472 normal->minor_start = drv->minor;
2473 normal->type = TTY_DRIVER_TYPE_SERIAL;
2474 normal->subtype = SERIAL_TYPE_NORMAL;
2475 normal->init_termios = tty_std_termios;
2476 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2477 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2478 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2479 normal->driver_state = drv;
2480 tty_set_operations(normal, &uart_ops);
2481
2482
2483
2484
2485 for (i = 0; i < drv->nr; i++) {
2486 struct uart_state *state = drv->state + i;
2487 struct tty_port *port = &state->port;
2488
2489 tty_port_init(port);
2490 port->ops = &uart_port_ops;
2491 }
2492
2493 retval = tty_register_driver(normal);
2494 if (retval >= 0)
2495 return retval;
2496
2497 for (i = 0; i < drv->nr; i++)
2498 tty_port_destroy(&drv->state[i].port);
2499 put_tty_driver(normal);
2500out_kfree:
2501 kfree(drv->state);
2502out:
2503 return -ENOMEM;
2504}
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515void uart_unregister_driver(struct uart_driver *drv)
2516{
2517 struct tty_driver *p = drv->tty_driver;
2518 unsigned int i;
2519
2520 tty_unregister_driver(p);
2521 put_tty_driver(p);
2522 for (i = 0; i < drv->nr; i++)
2523 tty_port_destroy(&drv->state[i].port);
2524 kfree(drv->state);
2525 drv->state = NULL;
2526 drv->tty_driver = NULL;
2527}
2528
2529struct tty_driver *uart_console_device(struct console *co, int *index)
2530{
2531 struct uart_driver *p = co->data;
2532 *index = co->index;
2533 return p->tty_driver;
2534}
2535
2536static ssize_t uart_get_attr_uartclk(struct device *dev,
2537 struct device_attribute *attr, char *buf)
2538{
2539 struct serial_struct tmp;
2540 struct tty_port *port = dev_get_drvdata(dev);
2541
2542 uart_get_info(port, &tmp);
2543 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
2544}
2545
2546static ssize_t uart_get_attr_type(struct device *dev,
2547 struct device_attribute *attr, char *buf)
2548{
2549 struct serial_struct tmp;
2550 struct tty_port *port = dev_get_drvdata(dev);
2551
2552 uart_get_info(port, &tmp);
2553 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2554}
2555static ssize_t uart_get_attr_line(struct device *dev,
2556 struct device_attribute *attr, char *buf)
2557{
2558 struct serial_struct tmp;
2559 struct tty_port *port = dev_get_drvdata(dev);
2560
2561 uart_get_info(port, &tmp);
2562 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2563}
2564
2565static ssize_t uart_get_attr_port(struct device *dev,
2566 struct device_attribute *attr, char *buf)
2567{
2568 struct serial_struct tmp;
2569 struct tty_port *port = dev_get_drvdata(dev);
2570 unsigned long ioaddr;
2571
2572 uart_get_info(port, &tmp);
2573 ioaddr = tmp.port;
2574 if (HIGH_BITS_OFFSET)
2575 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2576 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2577}
2578
2579static ssize_t uart_get_attr_irq(struct device *dev,
2580 struct device_attribute *attr, char *buf)
2581{
2582 struct serial_struct tmp;
2583 struct tty_port *port = dev_get_drvdata(dev);
2584
2585 uart_get_info(port, &tmp);
2586 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2587}
2588
2589static ssize_t uart_get_attr_flags(struct device *dev,
2590 struct device_attribute *attr, char *buf)
2591{
2592 struct serial_struct tmp;
2593 struct tty_port *port = dev_get_drvdata(dev);
2594
2595 uart_get_info(port, &tmp);
2596 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2597}
2598
2599static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2600 struct device_attribute *attr, char *buf)
2601{
2602 struct serial_struct tmp;
2603 struct tty_port *port = dev_get_drvdata(dev);
2604
2605 uart_get_info(port, &tmp);
2606 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2607}
2608
2609
2610static ssize_t uart_get_attr_close_delay(struct device *dev,
2611 struct device_attribute *attr, char *buf)
2612{
2613 struct serial_struct tmp;
2614 struct tty_port *port = dev_get_drvdata(dev);
2615
2616 uart_get_info(port, &tmp);
2617 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2618}
2619
2620
2621static ssize_t uart_get_attr_closing_wait(struct device *dev,
2622 struct device_attribute *attr, char *buf)
2623{
2624 struct serial_struct tmp;
2625 struct tty_port *port = dev_get_drvdata(dev);
2626
2627 uart_get_info(port, &tmp);
2628 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2629}
2630
2631static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2632 struct device_attribute *attr, char *buf)
2633{
2634 struct serial_struct tmp;
2635 struct tty_port *port = dev_get_drvdata(dev);
2636
2637 uart_get_info(port, &tmp);
2638 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2639}
2640
2641static ssize_t uart_get_attr_io_type(struct device *dev,
2642 struct device_attribute *attr, char *buf)
2643{
2644 struct serial_struct tmp;
2645 struct tty_port *port = dev_get_drvdata(dev);
2646
2647 uart_get_info(port, &tmp);
2648 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2649}
2650
2651static ssize_t uart_get_attr_iomem_base(struct device *dev,
2652 struct device_attribute *attr, char *buf)
2653{
2654 struct serial_struct tmp;
2655 struct tty_port *port = dev_get_drvdata(dev);
2656
2657 uart_get_info(port, &tmp);
2658 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2659}
2660
2661static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2662 struct device_attribute *attr, char *buf)
2663{
2664 struct serial_struct tmp;
2665 struct tty_port *port = dev_get_drvdata(dev);
2666
2667 uart_get_info(port, &tmp);
2668 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2669}
2670
2671static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2672static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2673static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2674static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2675static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2676static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
2677static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
2678static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2679static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2680static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2681static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2682static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2683static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
2684
2685static struct attribute *tty_dev_attrs[] = {
2686 &dev_attr_type.attr,
2687 &dev_attr_line.attr,
2688 &dev_attr_port.attr,
2689 &dev_attr_irq.attr,
2690 &dev_attr_flags.attr,
2691 &dev_attr_xmit_fifo_size.attr,
2692 &dev_attr_uartclk.attr,
2693 &dev_attr_close_delay.attr,
2694 &dev_attr_closing_wait.attr,
2695 &dev_attr_custom_divisor.attr,
2696 &dev_attr_io_type.attr,
2697 &dev_attr_iomem_base.attr,
2698 &dev_attr_iomem_reg_shift.attr,
2699 NULL,
2700 };
2701
2702static const struct attribute_group tty_dev_attr_group = {
2703 .attrs = tty_dev_attrs,
2704 };
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2717{
2718 struct uart_state *state;
2719 struct tty_port *port;
2720 int ret = 0;
2721 struct device *tty_dev;
2722 int num_groups;
2723
2724 BUG_ON(in_interrupt());
2725
2726 if (uport->line >= drv->nr)
2727 return -EINVAL;
2728
2729 state = drv->state + uport->line;
2730 port = &state->port;
2731
2732 mutex_lock(&port_mutex);
2733 mutex_lock(&port->mutex);
2734 if (state->uart_port) {
2735 ret = -EINVAL;
2736 goto out;
2737 }
2738
2739
2740 atomic_set(&state->refcount, 1);
2741 init_waitqueue_head(&state->remove_wait);
2742 state->uart_port = uport;
2743 uport->state = state;
2744
2745 state->pm_state = UART_PM_STATE_UNDEFINED;
2746 uport->cons = drv->cons;
2747 uport->minor = drv->tty_driver->minor_start + uport->line;
2748
2749
2750
2751
2752
2753 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2754 spin_lock_init(&uport->lock);
2755 lockdep_set_class(&uport->lock, &port_lock_key);
2756 }
2757 if (uport->cons && uport->dev)
2758 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
2759
2760 uart_configure_port(drv, state, uport);
2761
2762 port->console = uart_console(uport);
2763
2764 num_groups = 2;
2765 if (uport->attr_group)
2766 num_groups++;
2767
2768 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
2769 GFP_KERNEL);
2770 if (!uport->tty_groups) {
2771 ret = -ENOMEM;
2772 goto out;
2773 }
2774 uport->tty_groups[0] = &tty_dev_attr_group;
2775 if (uport->attr_group)
2776 uport->tty_groups[1] = uport->attr_group;
2777
2778
2779
2780
2781
2782 tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
2783 uport->line, uport->dev, port, uport->tty_groups);
2784 if (likely(!IS_ERR(tty_dev))) {
2785 device_set_wakeup_capable(tty_dev, 1);
2786 } else {
2787 dev_err(uport->dev, "Cannot register tty device on line %d\n",
2788 uport->line);
2789 }
2790
2791
2792
2793
2794 uport->flags &= ~UPF_DEAD;
2795
2796 out:
2797 mutex_unlock(&port->mutex);
2798 mutex_unlock(&port_mutex);
2799
2800 return ret;
2801}
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2813{
2814 struct uart_state *state = drv->state + uport->line;
2815 struct tty_port *port = &state->port;
2816 struct uart_port *uart_port;
2817 struct tty_struct *tty;
2818 int ret = 0;
2819
2820 BUG_ON(in_interrupt());
2821
2822 mutex_lock(&port_mutex);
2823
2824
2825
2826
2827
2828 mutex_lock(&port->mutex);
2829 uart_port = uart_port_check(state);
2830 if (uart_port != uport)
2831 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
2832 uart_port, uport);
2833
2834 if (!uart_port) {
2835 mutex_unlock(&port->mutex);
2836 ret = -EINVAL;
2837 goto out;
2838 }
2839 uport->flags |= UPF_DEAD;
2840 mutex_unlock(&port->mutex);
2841
2842
2843
2844
2845 tty_unregister_device(drv->tty_driver, uport->line);
2846
2847 tty = tty_port_tty_get(port);
2848 if (tty) {
2849 tty_vhangup(port->tty);
2850 tty_kref_put(tty);
2851 }
2852
2853
2854
2855
2856 if (uart_console(uport))
2857 unregister_console(uport->cons);
2858
2859
2860
2861
2862 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
2863 uport->ops->release_port(uport);
2864 kfree(uport->tty_groups);
2865
2866
2867
2868
2869 uport->type = PORT_UNKNOWN;
2870
2871 mutex_lock(&port->mutex);
2872 WARN_ON(atomic_dec_return(&state->refcount) < 0);
2873 wait_event(state->remove_wait, !atomic_read(&state->refcount));
2874 state->uart_port = NULL;
2875 mutex_unlock(&port->mutex);
2876out:
2877 mutex_unlock(&port_mutex);
2878
2879 return ret;
2880}
2881
2882
2883
2884
2885int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2886{
2887 if (port1->iotype != port2->iotype)
2888 return 0;
2889
2890 switch (port1->iotype) {
2891 case UPIO_PORT:
2892 return (port1->iobase == port2->iobase);
2893 case UPIO_HUB6:
2894 return (port1->iobase == port2->iobase) &&
2895 (port1->hub6 == port2->hub6);
2896 case UPIO_MEM:
2897 case UPIO_MEM16:
2898 case UPIO_MEM32:
2899 case UPIO_MEM32BE:
2900 case UPIO_AU:
2901 case UPIO_TSI:
2902 return (port1->mapbase == port2->mapbase);
2903 }
2904 return 0;
2905}
2906EXPORT_SYMBOL(uart_match_port);
2907
2908
2909
2910
2911
2912
2913
2914
2915void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2916{
2917 struct tty_port *port = &uport->state->port;
2918 struct tty_struct *tty = port->tty;
2919 struct tty_ldisc *ld;
2920
2921 lockdep_assert_held_once(&uport->lock);
2922
2923 if (tty) {
2924 ld = tty_ldisc_ref(tty);
2925 if (ld) {
2926 if (ld->ops->dcd_change)
2927 ld->ops->dcd_change(tty, status);
2928 tty_ldisc_deref(ld);
2929 }
2930 }
2931
2932 uport->icount.dcd++;
2933
2934 if (uart_dcd_enabled(uport)) {
2935 if (status)
2936 wake_up_interruptible(&port->open_wait);
2937 else if (tty)
2938 tty_hangup(tty);
2939 }
2940}
2941EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2942
2943
2944
2945
2946
2947
2948
2949
2950void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2951{
2952 lockdep_assert_held_once(&uport->lock);
2953
2954 uport->icount.cts++;
2955
2956 if (uart_softcts_mode(uport)) {
2957 if (uport->hw_stopped) {
2958 if (status) {
2959 uport->hw_stopped = 0;
2960 uport->ops->start_tx(uport);
2961 uart_write_wakeup(uport);
2962 }
2963 } else {
2964 if (!status) {
2965 uport->hw_stopped = 1;
2966 uport->ops->stop_tx(uport);
2967 }
2968 }
2969
2970 }
2971}
2972EXPORT_SYMBOL_GPL(uart_handle_cts_change);
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986void uart_insert_char(struct uart_port *port, unsigned int status,
2987 unsigned int overrun, unsigned int ch, unsigned int flag)
2988{
2989 struct tty_port *tport = &port->state->port;
2990
2991 if ((status & port->ignore_status_mask & ~overrun) == 0)
2992 if (tty_insert_flip_char(tport, ch, flag) == 0)
2993 ++port->icount.buf_overrun;
2994
2995
2996
2997
2998
2999 if (status & ~port->ignore_status_mask & overrun)
3000 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3001 ++port->icount.buf_overrun;
3002}
3003EXPORT_SYMBOL_GPL(uart_insert_char);
3004
3005EXPORT_SYMBOL(uart_write_wakeup);
3006EXPORT_SYMBOL(uart_register_driver);
3007EXPORT_SYMBOL(uart_unregister_driver);
3008EXPORT_SYMBOL(uart_suspend_port);
3009EXPORT_SYMBOL(uart_resume_port);
3010EXPORT_SYMBOL(uart_add_one_port);
3011EXPORT_SYMBOL(uart_remove_one_port);
3012
3013MODULE_DESCRIPTION("Serial driver core");
3014MODULE_LICENSE("GPL");
3015