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