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