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