1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Universal/legacy driver for 8250/16550-type serial ports
4 *
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 *
7 * Copyright (C) 2001 Russell King.
8 *
9 * Supports:
10 * early_serial_setup() ports
11 * userspace-configurable "phantom" ports
12 * serial8250_register_8250_port() ports
13 */
14
15#include <linux/acpi.h>
16#include <linux/hashtable.h>
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/ioport.h>
20#include <linux/init.h>
21#include <linux/console.h>
22#include <linux/sysrq.h>
23#include <linux/delay.h>
24#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/tty.h>
27#include <linux/ratelimit.h>
28#include <linux/tty_flip.h>
29#include <linux/serial.h>
30#include <linux/serial_8250.h>
31#include <linux/nmi.h>
32#include <linux/mutex.h>
33#include <linux/slab.h>
34#include <linux/string_helpers.h>
35#include <linux/uaccess.h>
36#include <linux/io.h>
37
38#include <asm/irq.h>
39
40#include "8250.h"
41
42#define PASS_LIMIT 512
43
44struct irq_info {
45 struct hlist_node node;
46 int irq;
47 spinlock_t lock; /* Protects list not the hash */
48 struct list_head *head;
49};
50
51#define IRQ_HASH_BITS 5 /* Can be adjusted later */
52static DEFINE_HASHTABLE(irq_lists, IRQ_HASH_BITS);
53static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
54
55static bool skip_txen_test;
56module_param(skip_txen_test, bool, 0644);
57MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
58
59/*
60 * This is the serial driver's interrupt routine.
61 *
62 * Arjan thinks the old way was overly complex, so it got simplified.
63 * Alan disagrees, saying that need the complexity to handle the weird
64 * nature of ISA shared interrupts. (This is a special exception.)
65 *
66 * In order to handle ISA shared interrupts properly, we need to check
67 * that all ports have been serviced, and therefore the ISA interrupt
68 * line has been de-asserted.
69 *
70 * This means we need to loop through all ports. checking that they
71 * don't have an interrupt pending.
72 */
73static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
74{
75 struct irq_info *i = dev_id;
76 struct list_head *l, *end = NULL;
77 int pass_counter = 0, handled = 0;
78
79 guard(spinlock)(l: &i->lock);
80
81 l = i->head;
82 do {
83 struct uart_8250_port *up = list_entry(l, struct uart_8250_port, list);
84 struct uart_port *port = &up->port;
85
86 if (port->handle_irq(port)) {
87 handled = 1;
88 end = NULL;
89 } else if (end == NULL)
90 end = l;
91
92 l = l->next;
93
94 if (l == i->head && pass_counter++ > PASS_LIMIT)
95 break;
96 } while (l != end);
97
98 return IRQ_RETVAL(handled);
99}
100
101/*
102 * To support ISA shared interrupts, we need to have one interrupt
103 * handler that ensures that the IRQ line has been deasserted
104 * before returning. Failing to do this will result in the IRQ
105 * line being stuck active, and, since ISA irqs are edge triggered,
106 * no more IRQs will be seen.
107 */
108static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
109{
110 spin_lock_irq(lock: &i->lock);
111
112 if (!list_empty(head: i->head)) {
113 if (i->head == &up->list)
114 i->head = i->head->next;
115 list_del(entry: &up->list);
116 } else {
117 BUG_ON(i->head != &up->list);
118 i->head = NULL;
119 }
120 spin_unlock_irq(lock: &i->lock);
121 /* List empty so throw away the hash node */
122 if (i->head == NULL) {
123 hlist_del(n: &i->node);
124 kfree(objp: i);
125 }
126}
127
128/*
129 * Either:
130 * - find the corresponding info in the hashtable and return it, or
131 * - allocate a new one, add it to the hashtable and return it.
132 */
133static struct irq_info *serial_get_or_create_irq_info(const struct uart_8250_port *up)
134{
135 struct irq_info *i;
136
137 guard(mutex)(T: &hash_mutex);
138
139 hash_for_each_possible(irq_lists, i, node, up->port.irq)
140 if (i->irq == up->port.irq)
141 return i;
142
143 i = kzalloc(sizeof(*i), GFP_KERNEL);
144 if (i == NULL)
145 return ERR_PTR(error: -ENOMEM);
146
147 spin_lock_init(&i->lock);
148 i->irq = up->port.irq;
149 hash_add(irq_lists, &i->node, i->irq);
150
151 return i;
152}
153
154static int serial_link_irq_chain(struct uart_8250_port *up)
155{
156 struct irq_info *i;
157 int ret;
158
159 i = serial_get_or_create_irq_info(up);
160 if (IS_ERR(ptr: i))
161 return PTR_ERR(ptr: i);
162
163 scoped_guard(spinlock_irq, &i->lock) {
164 if (i->head) {
165 list_add(new: &up->list, head: i->head);
166
167 return 0;
168 }
169
170 INIT_LIST_HEAD(list: &up->list);
171 i->head = &up->list;
172 }
173
174 ret = request_irq(irq: up->port.irq, handler: serial8250_interrupt, flags: up->port.irqflags, name: up->port.name, dev: i);
175 if (ret < 0)
176 serial_do_unlink(i, up);
177
178 return ret;
179}
180
181static void serial_unlink_irq_chain(struct uart_8250_port *up)
182{
183 struct irq_info *i;
184
185 guard(mutex)(T: &hash_mutex);
186
187 hash_for_each_possible(irq_lists, i, node, up->port.irq)
188 if (i->irq == up->port.irq) {
189 if (WARN_ON(i->head == NULL))
190 return;
191
192 if (list_empty(head: i->head))
193 free_irq(up->port.irq, i);
194
195 serial_do_unlink(i, up);
196
197 return;
198 }
199
200 WARN_ON(1);
201}
202
203/*
204 * This function is used to handle ports that do not have an
205 * interrupt. This doesn't work very well for 16450's, but gives
206 * barely passable results for a 16550A. (Although at the expense
207 * of much CPU overhead).
208 */
209static void serial8250_timeout(struct timer_list *t)
210{
211 struct uart_8250_port *up = timer_container_of(up, t, timer);
212
213 up->port.handle_irq(&up->port);
214 mod_timer(timer: &up->timer, expires: jiffies + uart_poll_timeout(port: &up->port));
215}
216
217static void serial8250_backup_timeout(struct timer_list *t)
218{
219 struct uart_8250_port *up = timer_container_of(up, t, timer);
220 unsigned int iir, ier = 0, lsr;
221 unsigned long flags;
222
223 uart_port_lock_irqsave(up: &up->port, flags: &flags);
224
225 /*
226 * Must disable interrupts or else we risk racing with the interrupt
227 * based handler.
228 */
229 if (up->port.irq) {
230 ier = serial_in(up, UART_IER);
231 serial_out(up, UART_IER, value: 0);
232 }
233
234 iir = serial_in(up, UART_IIR);
235
236 /*
237 * This should be a safe test for anyone who doesn't trust the
238 * IIR bits on their UART, but it's specifically designed for
239 * the "Diva" UART used on the management processor on many HP
240 * ia64 and parisc boxes.
241 */
242 lsr = serial_lsr_in(up);
243 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
244 (!kfifo_is_empty(&up->port.state->port.xmit_fifo) ||
245 up->port.x_char) &&
246 (lsr & UART_LSR_THRE)) {
247 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
248 iir |= UART_IIR_THRI;
249 }
250
251 if (!(iir & UART_IIR_NO_INT))
252 serial8250_tx_chars(up);
253
254 if (up->port.irq)
255 serial_out(up, UART_IER, value: ier);
256
257 uart_port_unlock_irqrestore(up: &up->port, flags);
258
259 /* Standard timer interval plus 0.2s to keep the port running */
260 mod_timer(timer: &up->timer,
261 expires: jiffies + uart_poll_timeout(port: &up->port) + HZ / 5);
262}
263
264static void univ8250_setup_timer(struct uart_8250_port *up)
265{
266 struct uart_port *port = &up->port;
267
268 /*
269 * The above check will only give an accurate result the first time
270 * the port is opened so this value needs to be preserved.
271 */
272 if (up->bugs & UART_BUG_THRE) {
273 pr_debug("%s - using backup timer\n", port->name);
274
275 up->timer.function = serial8250_backup_timeout;
276 mod_timer(timer: &up->timer, expires: jiffies +
277 uart_poll_timeout(port) + HZ / 5);
278 }
279
280 /*
281 * If the "interrupt" for this port doesn't correspond with any
282 * hardware interrupt, we use a timer-based system. The original
283 * driver used to do this with IRQ0.
284 */
285 if (!port->irq)
286 mod_timer(timer: &up->timer, expires: jiffies + uart_poll_timeout(port));
287}
288
289static int univ8250_setup_irq(struct uart_8250_port *up)
290{
291 struct uart_port *port = &up->port;
292
293 if (port->irq)
294 return serial_link_irq_chain(up);
295
296 return 0;
297}
298
299static void univ8250_release_irq(struct uart_8250_port *up)
300{
301 struct uart_port *port = &up->port;
302
303 timer_delete_sync(timer: &up->timer);
304 up->timer.function = serial8250_timeout;
305 if (port->irq)
306 serial_unlink_irq_chain(up);
307}
308
309const struct uart_ops *univ8250_port_base_ops;
310struct uart_ops univ8250_port_ops;
311
312static const struct uart_8250_ops univ8250_driver_ops = {
313 .setup_irq = univ8250_setup_irq,
314 .release_irq = univ8250_release_irq,
315 .setup_timer = univ8250_setup_timer,
316};
317
318static struct uart_8250_port serial8250_ports[UART_NR];
319
320/**
321 * serial8250_get_port - retrieve struct uart_8250_port
322 * @line: serial line number
323 *
324 * This function retrieves struct uart_8250_port for the specific line.
325 * This struct *must* *not* be used to perform a 8250 or serial core operation
326 * which is not accessible otherwise. Its only purpose is to make the struct
327 * accessible to the runtime-pm callbacks for context suspend/restore.
328 * The lock assumption made here is none because runtime-pm suspend/resume
329 * callbacks should not be invoked if there is any operation performed on the
330 * port.
331 */
332struct uart_8250_port *serial8250_get_port(int line)
333{
334 return &serial8250_ports[line];
335}
336EXPORT_SYMBOL_GPL(serial8250_get_port);
337
338static inline void serial8250_apply_quirks(struct uart_8250_port *up)
339{
340 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
341}
342
343struct uart_8250_port *serial8250_setup_port(int index)
344{
345 struct uart_8250_port *up;
346
347 if (index >= UART_NR)
348 return NULL;
349
350 up = &serial8250_ports[index];
351 up->port.line = index;
352 up->port.port_id = index;
353
354 serial8250_init_port(up);
355 if (!univ8250_port_base_ops)
356 univ8250_port_base_ops = up->port.ops;
357 up->port.ops = &univ8250_port_ops;
358
359 timer_setup(&up->timer, serial8250_timeout, 0);
360
361 up->ops = &univ8250_driver_ops;
362
363 serial8250_set_defaults(up);
364
365 return up;
366}
367
368void __init serial8250_register_ports(struct uart_driver *drv, struct device *dev)
369{
370 int i;
371
372 for (i = 0; i < nr_uarts; i++) {
373 struct uart_8250_port *up = &serial8250_ports[i];
374
375 if (up->port.type == PORT_8250_CIR)
376 continue;
377
378 if (up->port.dev)
379 continue;
380
381 up->port.dev = dev;
382
383 if (uart_console_registered(port: &up->port))
384 pm_runtime_get_sync(dev: up->port.dev);
385
386 serial8250_apply_quirks(up);
387 uart_add_one_port(reg: drv, port: &up->port);
388 }
389}
390
391#ifdef CONFIG_SERIAL_8250_CONSOLE
392
393static void univ8250_console_write(struct console *co, const char *s,
394 unsigned int count)
395{
396 struct uart_8250_port *up = &serial8250_ports[co->index];
397
398 serial8250_console_write(up, s, count);
399}
400
401static int univ8250_console_setup(struct console *co, char *options)
402{
403 struct uart_8250_port *up;
404 struct uart_port *port;
405 int retval, i;
406
407 /*
408 * Check whether an invalid uart number has been specified, and
409 * if so, search for the first available port that does have
410 * console support.
411 */
412 if (co->index < 0 || co->index >= UART_NR)
413 co->index = 0;
414
415 /*
416 * If the console is past the initial isa ports, init more ports up to
417 * co->index as needed and increment nr_uarts accordingly.
418 */
419 for (i = nr_uarts; i <= co->index; i++) {
420 up = serial8250_setup_port(index: i);
421 if (!up)
422 return -ENODEV;
423 nr_uarts++;
424 }
425
426 port = &serial8250_ports[co->index].port;
427 /* link port to console */
428 uart_port_set_cons(up: port, con: co);
429
430 retval = serial8250_console_setup(port, options, probe: false);
431 if (retval != 0)
432 uart_port_set_cons(up: port, NULL);
433 return retval;
434}
435
436static int univ8250_console_exit(struct console *co)
437{
438 struct uart_port *port;
439
440 port = &serial8250_ports[co->index].port;
441 return serial8250_console_exit(port);
442}
443
444/**
445 * univ8250_console_match - non-standard console matching
446 * @co: registering console
447 * @name: name from console command line
448 * @idx: index from console command line
449 * @options: ptr to option string from console command line
450 *
451 * Only attempts to match console command lines of the form:
452 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
453 * console=uart[8250],0x<addr>[,<options>]
454 * This form is used to register an initial earlycon boot console and
455 * replace it with the serial8250_console at 8250 driver init.
456 *
457 * Performs console setup for a match (as required by interface)
458 * If no <options> are specified, then assume the h/w is already setup.
459 *
460 * Returns 0 if console matches; otherwise non-zero to use default matching
461 */
462static int univ8250_console_match(struct console *co, char *name, int idx,
463 char *options)
464{
465 char match[] = "uart"; /* 8250-specific earlycon name */
466 enum uart_iotype iotype;
467 resource_size_t addr;
468 int i;
469
470 if (strncmp(name, match, 4) != 0)
471 return -ENODEV;
472
473 if (uart_parse_earlycon(p: options, iotype: &iotype, addr: &addr, options: &options))
474 return -ENODEV;
475
476 /* try to match the port specified on the command line */
477 for (i = 0; i < nr_uarts; i++) {
478 struct uart_port *port = &serial8250_ports[i].port;
479
480 if (port->iotype != iotype)
481 continue;
482 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
483 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
484 && (port->mapbase != addr))
485 continue;
486 if (iotype == UPIO_PORT && port->iobase != addr)
487 continue;
488
489 co->index = i;
490 uart_port_set_cons(up: port, con: co);
491 return serial8250_console_setup(port, options, probe: true);
492 }
493
494 return -ENODEV;
495}
496
497static struct console univ8250_console = {
498 .name = "ttyS",
499 .write = univ8250_console_write,
500 .device = uart_console_device,
501 .setup = univ8250_console_setup,
502 .exit = univ8250_console_exit,
503 .match = univ8250_console_match,
504 .flags = CON_PRINTBUFFER | CON_ANYTIME,
505 .index = -1,
506 .data = &serial8250_reg,
507};
508
509static int __init univ8250_console_init(void)
510{
511 if (nr_uarts == 0)
512 return -ENODEV;
513
514 serial8250_isa_init_ports();
515 register_console(&univ8250_console);
516 return 0;
517}
518console_initcall(univ8250_console_init);
519
520#define SERIAL8250_CONSOLE (&univ8250_console)
521#else
522#define SERIAL8250_CONSOLE NULL
523#endif
524
525struct uart_driver serial8250_reg = {
526 .owner = THIS_MODULE,
527 .driver_name = "serial",
528 .dev_name = "ttyS",
529 .major = TTY_MAJOR,
530 .minor = 64,
531 .cons = SERIAL8250_CONSOLE,
532};
533
534/*
535 * early_serial_setup - early registration for 8250 ports
536 *
537 * Setup an 8250 port structure prior to console initialisation. Use
538 * after console initialisation will cause undefined behaviour.
539 */
540int __init early_serial_setup(struct uart_port *port)
541{
542 struct uart_port *p;
543
544 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
545 return -ENODEV;
546
547 serial8250_isa_init_ports();
548 p = &serial8250_ports[port->line].port;
549 p->iobase = port->iobase;
550 p->membase = port->membase;
551 p->irq = port->irq;
552 p->irqflags = port->irqflags;
553 p->uartclk = port->uartclk;
554 p->fifosize = port->fifosize;
555 p->regshift = port->regshift;
556 p->iotype = port->iotype;
557 p->flags = port->flags;
558 p->mapbase = port->mapbase;
559 p->mapsize = port->mapsize;
560 p->private_data = port->private_data;
561 p->type = port->type;
562 p->line = port->line;
563
564 serial8250_set_defaults(up: up_to_u8250p(up: p));
565
566 if (port->serial_in)
567 p->serial_in = port->serial_in;
568 if (port->serial_out)
569 p->serial_out = port->serial_out;
570 if (port->handle_irq)
571 p->handle_irq = port->handle_irq;
572
573 return 0;
574}
575
576/**
577 * serial8250_suspend_port - suspend one serial port
578 * @line: serial line number
579 *
580 * Suspend one serial port.
581 */
582void serial8250_suspend_port(int line)
583{
584 struct uart_8250_port *up = &serial8250_ports[line];
585 struct uart_port *port = &up->port;
586
587 if (!console_suspend_enabled && uart_console(port) &&
588 port->type != PORT_8250) {
589 unsigned char canary = 0xa5;
590
591 serial_out(up, UART_SCR, value: canary);
592 if (serial_in(up, UART_SCR) == canary)
593 up->canary = canary;
594 }
595
596 uart_suspend_port(reg: &serial8250_reg, port);
597}
598EXPORT_SYMBOL(serial8250_suspend_port);
599
600/**
601 * serial8250_resume_port - resume one serial port
602 * @line: serial line number
603 *
604 * Resume one serial port.
605 */
606void serial8250_resume_port(int line)
607{
608 struct uart_8250_port *up = &serial8250_ports[line];
609 struct uart_port *port = &up->port;
610
611 up->canary = 0;
612
613 if (up->capabilities & UART_NATSEMI) {
614 /* Ensure it's still in high speed mode */
615 serial_port_out(up: port, UART_LCR, value: 0xE0);
616
617 ns16550a_goto_highspeed(up);
618
619 serial_port_out(up: port, UART_LCR, value: 0);
620 port->uartclk = 921600*16;
621 }
622 uart_resume_port(reg: &serial8250_reg, port);
623}
624EXPORT_SYMBOL(serial8250_resume_port);
625
626/*
627 * serial8250_register_8250_port and serial8250_unregister_port allows for
628 * 16x50 serial ports to be configured at run-time, to support PCMCIA
629 * modems and PCI multiport cards.
630 */
631static DEFINE_MUTEX(serial_mutex);
632
633static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port)
634{
635 int i;
636
637 /*
638 * First, find a port entry which matches.
639 */
640 for (i = 0; i < nr_uarts; i++)
641 if (uart_match_port(port1: &serial8250_ports[i].port, port2: port))
642 return &serial8250_ports[i];
643
644 /* try line number first if still available */
645 i = port->line;
646 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
647 serial8250_ports[i].port.iobase == 0)
648 return &serial8250_ports[i];
649 /*
650 * We didn't find a matching entry, so look for the first
651 * free entry. We look for one which hasn't been previously
652 * used (indicated by zero iobase).
653 */
654 for (i = 0; i < nr_uarts; i++)
655 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
656 serial8250_ports[i].port.iobase == 0)
657 return &serial8250_ports[i];
658
659 /*
660 * That also failed. Last resort is to find any entry which
661 * doesn't have a real port associated with it.
662 */
663 for (i = 0; i < nr_uarts; i++)
664 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
665 return &serial8250_ports[i];
666
667 return NULL;
668}
669
670static void serial_8250_overrun_backoff_work(struct work_struct *work)
671{
672 struct uart_8250_port *up = container_of(to_delayed_work(work), struct uart_8250_port,
673 overrun_backoff);
674
675 guard(uart_port_lock_irqsave)(l: &up->port);
676 up->ier |= UART_IER_RLSI | UART_IER_RDI;
677 serial_out(up, UART_IER, value: up->ier);
678}
679
680/**
681 * serial8250_register_8250_port - register a serial port
682 * @up: serial port template
683 *
684 * Configure the serial port specified by the request. If the
685 * port exists and is in use, it is hung up and unregistered
686 * first.
687 *
688 * The port is then probed and if necessary the IRQ is autodetected
689 * If this fails an error is returned.
690 *
691 * On success the port is ready to use and the line number is returned.
692 */
693int serial8250_register_8250_port(const struct uart_8250_port *up)
694{
695 struct uart_8250_port *uart;
696 int ret;
697
698 if (up->port.uartclk == 0)
699 return -EINVAL;
700
701 guard(mutex)(T: &serial_mutex);
702
703 uart = serial8250_find_match_or_unused(port: &up->port);
704 if (!uart) {
705 /*
706 * If the port is past the initial isa ports, initialize a new
707 * port and increment nr_uarts accordingly.
708 */
709 uart = serial8250_setup_port(index: nr_uarts);
710 if (!uart)
711 return -ENOSPC;
712 nr_uarts++;
713 }
714
715 /* Check if it is CIR already. We check this below again, see there why. */
716 if (uart->port.type == PORT_8250_CIR)
717 return -ENODEV;
718
719 if (uart->port.dev)
720 uart_remove_one_port(reg: &serial8250_reg, port: &uart->port);
721
722 uart->port.ctrl_id = up->port.ctrl_id;
723 uart->port.port_id = up->port.port_id;
724 uart->port.iobase = up->port.iobase;
725 uart->port.membase = up->port.membase;
726 uart->port.irq = up->port.irq;
727 uart->port.irqflags = up->port.irqflags;
728 uart->port.uartclk = up->port.uartclk;
729 uart->port.fifosize = up->port.fifosize;
730 uart->port.regshift = up->port.regshift;
731 uart->port.iotype = up->port.iotype;
732 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
733 uart->bugs = up->bugs;
734 uart->port.mapbase = up->port.mapbase;
735 uart->port.mapsize = up->port.mapsize;
736 uart->port.private_data = up->port.private_data;
737 uart->tx_loadsz = up->tx_loadsz;
738 uart->capabilities = up->capabilities;
739 uart->port.throttle = up->port.throttle;
740 uart->port.unthrottle = up->port.unthrottle;
741 uart->port.rs485_config = up->port.rs485_config;
742 uart->port.rs485_supported = up->port.rs485_supported;
743 uart->port.rs485 = up->port.rs485;
744 uart->rs485_start_tx = up->rs485_start_tx;
745 uart->rs485_stop_tx = up->rs485_stop_tx;
746 uart->lsr_save_mask = up->lsr_save_mask;
747 uart->dma = up->dma;
748
749 /* Take tx_loadsz from fifosize if it wasn't set separately */
750 if (uart->port.fifosize && !uart->tx_loadsz)
751 uart->tx_loadsz = uart->port.fifosize;
752
753 if (up->port.dev) {
754 uart->port.dev = up->port.dev;
755 ret = uart_get_rs485_mode(port: &uart->port);
756 if (ret)
757 goto err;
758 }
759
760 if (up->port.flags & UPF_FIXED_TYPE)
761 uart->port.type = up->port.type;
762
763 /*
764 * Only call mctrl_gpio_init(), if the device has no ACPI
765 * companion device
766 */
767 if (!has_acpi_companion(dev: uart->port.dev)) {
768 struct mctrl_gpios *gpios = mctrl_gpio_init(port: &uart->port, idx: 0);
769 if (IS_ERR(ptr: gpios)) {
770 ret = PTR_ERR(ptr: gpios);
771 goto err;
772 } else {
773 uart->gpios = gpios;
774 }
775 }
776
777 serial8250_set_defaults(up: uart);
778
779 /* Possibly override default I/O functions. */
780 if (up->port.serial_in)
781 uart->port.serial_in = up->port.serial_in;
782 if (up->port.serial_out)
783 uart->port.serial_out = up->port.serial_out;
784 if (up->port.handle_irq)
785 uart->port.handle_irq = up->port.handle_irq;
786 /* Possibly override set_termios call */
787 if (up->port.set_termios)
788 uart->port.set_termios = up->port.set_termios;
789 if (up->port.set_ldisc)
790 uart->port.set_ldisc = up->port.set_ldisc;
791 if (up->port.get_mctrl)
792 uart->port.get_mctrl = up->port.get_mctrl;
793 if (up->port.set_mctrl)
794 uart->port.set_mctrl = up->port.set_mctrl;
795 if (up->port.get_divisor)
796 uart->port.get_divisor = up->port.get_divisor;
797 if (up->port.set_divisor)
798 uart->port.set_divisor = up->port.set_divisor;
799 if (up->port.startup)
800 uart->port.startup = up->port.startup;
801 if (up->port.shutdown)
802 uart->port.shutdown = up->port.shutdown;
803 if (up->port.pm)
804 uart->port.pm = up->port.pm;
805 if (up->port.handle_break)
806 uart->port.handle_break = up->port.handle_break;
807 if (up->dl_read)
808 uart->dl_read = up->dl_read;
809 if (up->dl_write)
810 uart->dl_write = up->dl_write;
811
812 /* Check the type (again)! It might have changed by the port.type assignment above. */
813 if (uart->port.type != PORT_8250_CIR) {
814 if (uart_console_registered(port: &uart->port))
815 pm_runtime_get_sync(dev: uart->port.dev);
816
817 if (serial8250_isa_config != NULL)
818 serial8250_isa_config(0, &uart->port,
819 &uart->capabilities);
820
821 serial8250_apply_quirks(up: uart);
822 ret = uart_add_one_port(reg: &serial8250_reg,
823 port: &uart->port);
824 if (ret)
825 goto err;
826
827 ret = uart->port.line;
828 } else {
829 dev_info(uart->port.dev,
830 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
831 uart->port.iobase,
832 (unsigned long long)uart->port.mapbase,
833 uart->port.irq);
834
835 ret = 0;
836 }
837
838 if (!uart->lsr_save_mask)
839 uart->lsr_save_mask = LSR_SAVE_FLAGS; /* Use default LSR mask */
840
841 /* Initialise interrupt backoff work if required */
842 if (up->overrun_backoff_time_ms > 0) {
843 uart->overrun_backoff_time_ms =
844 up->overrun_backoff_time_ms;
845 INIT_DELAYED_WORK(&uart->overrun_backoff,
846 serial_8250_overrun_backoff_work);
847 } else {
848 uart->overrun_backoff_time_ms = 0;
849 }
850
851 return ret;
852
853err:
854 uart->port.dev = NULL;
855 return ret;
856}
857EXPORT_SYMBOL(serial8250_register_8250_port);
858
859/**
860 * serial8250_unregister_port - remove a 16x50 serial port at runtime
861 * @line: serial line number
862 *
863 * Remove one serial port. This may not be called from interrupt
864 * context. We hand the port back to the our control.
865 */
866void serial8250_unregister_port(int line)
867{
868 struct uart_8250_port *uart = &serial8250_ports[line];
869
870 guard(mutex)(T: &serial_mutex);
871
872 if (uart->em485) {
873 guard(uart_port_lock_irqsave)(l: &uart->port);
874 serial8250_em485_destroy(p: uart);
875 }
876
877 uart_remove_one_port(reg: &serial8250_reg, port: &uart->port);
878 if (serial8250_isa_devs) {
879 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
880 uart->port.type = PORT_UNKNOWN;
881 uart->port.dev = &serial8250_isa_devs->dev;
882 uart->port.port_id = line;
883 uart->capabilities = 0;
884 serial8250_init_port(up: uart);
885 serial8250_apply_quirks(up: uart);
886 uart_add_one_port(reg: &serial8250_reg, port: &uart->port);
887 } else {
888 uart->port.dev = NULL;
889 }
890}
891EXPORT_SYMBOL(serial8250_unregister_port);
892
893MODULE_LICENSE("GPL");
894MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
895

source code of linux/drivers/tty/serial/8250/8250_core.c