LCOV - code coverage report
Current view: top level - tty/serial/8250 - 8250_core.c (source / functions) Coverage Total Hit
Test: TTY Combined Coverage Lines: 0.0 % 462 0
Test Date: 2025-08-26 15:45:50 Functions: 0.0 % 26 0

            Line data    Source code
       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              :  */
      69            0 : static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
      70              : {
      71            0 :         struct irq_info *i = dev_id;
      72            0 :         struct list_head *l, *end = NULL;
      73            0 :         int pass_counter = 0, handled = 0;
      74              : 
      75            0 :         spin_lock(&i->lock);
      76              : 
      77            0 :         l = i->head;
      78            0 :         do {
      79            0 :                 struct uart_8250_port *up = list_entry(l, struct uart_8250_port, list);
      80            0 :                 struct uart_port *port = &up->port;
      81              : 
      82            0 :                 if (port->handle_irq(port)) {
      83            0 :                         handled = 1;
      84            0 :                         end = NULL;
      85            0 :                 } else if (end == NULL)
      86            0 :                         end = l;
      87              : 
      88            0 :                 l = l->next;
      89              : 
      90            0 :                 if (l == i->head && pass_counter++ > PASS_LIMIT)
      91            0 :                         break;
      92            0 :         } while (l != end);
      93              : 
      94            0 :         spin_unlock(&i->lock);
      95              : 
      96            0 :         return IRQ_RETVAL(handled);
      97            0 : }
      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              :  */
     106            0 : static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
     107              : {
     108            0 :         spin_lock_irq(&i->lock);
     109              : 
     110            0 :         if (!list_empty(i->head)) {
     111            0 :                 if (i->head == &up->list)
     112            0 :                         i->head = i->head->next;
     113            0 :                 list_del(&up->list);
     114            0 :         } else {
     115            0 :                 BUG_ON(i->head != &up->list);
     116            0 :                 i->head = NULL;
     117              :         }
     118            0 :         spin_unlock_irq(&i->lock);
     119              :         /* List empty so throw away the hash node */
     120            0 :         if (i->head == NULL) {
     121            0 :                 hlist_del(&i->node);
     122            0 :                 kfree(i);
     123            0 :         }
     124            0 : }
     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              :  */
     131            0 : static struct irq_info *serial_get_or_create_irq_info(const struct uart_8250_port *up)
     132              : {
     133            0 :         struct irq_info *i;
     134              : 
     135            0 :         mutex_lock(&hash_mutex);
     136              : 
     137            0 :         hash_for_each_possible(irq_lists, i, node, up->port.irq)
     138            0 :                 if (i->irq == up->port.irq)
     139            0 :                         goto unlock;
     140              : 
     141            0 :         i = kzalloc(sizeof(*i), GFP_KERNEL);
     142            0 :         if (i == NULL) {
     143            0 :                 i = ERR_PTR(-ENOMEM);
     144            0 :                 goto unlock;
     145              :         }
     146            0 :         spin_lock_init(&i->lock);
     147            0 :         i->irq = up->port.irq;
     148            0 :         hash_add(irq_lists, &i->node, i->irq);
     149              : unlock:
     150            0 :         mutex_unlock(&hash_mutex);
     151              : 
     152            0 :         return i;
     153            0 : }
     154              : 
     155            0 : static int serial_link_irq_chain(struct uart_8250_port *up)
     156              : {
     157            0 :         struct irq_info *i;
     158            0 :         int ret;
     159              : 
     160            0 :         i = serial_get_or_create_irq_info(up);
     161            0 :         if (IS_ERR(i))
     162            0 :                 return PTR_ERR(i);
     163              : 
     164            0 :         spin_lock_irq(&i->lock);
     165              : 
     166            0 :         if (i->head) {
     167            0 :                 list_add(&up->list, i->head);
     168            0 :                 spin_unlock_irq(&i->lock);
     169              : 
     170            0 :                 ret = 0;
     171            0 :         } else {
     172            0 :                 INIT_LIST_HEAD(&up->list);
     173            0 :                 i->head = &up->list;
     174            0 :                 spin_unlock_irq(&i->lock);
     175            0 :                 ret = request_irq(up->port.irq, serial8250_interrupt,
     176            0 :                                   up->port.irqflags, up->port.name, i);
     177            0 :                 if (ret < 0)
     178            0 :                         serial_do_unlink(i, up);
     179              :         }
     180              : 
     181            0 :         return ret;
     182            0 : }
     183              : 
     184            0 : static void serial_unlink_irq_chain(struct uart_8250_port *up)
     185              : {
     186            0 :         struct irq_info *i;
     187              : 
     188            0 :         mutex_lock(&hash_mutex);
     189              : 
     190            0 :         hash_for_each_possible(irq_lists, i, node, up->port.irq)
     191            0 :                 if (i->irq == up->port.irq)
     192            0 :                         break;
     193              : 
     194            0 :         BUG_ON(i == NULL);
     195            0 :         BUG_ON(i->head == NULL);
     196              : 
     197            0 :         if (list_empty(i->head))
     198            0 :                 free_irq(up->port.irq, i);
     199              : 
     200            0 :         serial_do_unlink(i, up);
     201            0 :         mutex_unlock(&hash_mutex);
     202            0 : }
     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              :  */
     210            0 : static void serial8250_timeout(struct timer_list *t)
     211              : {
     212            0 :         struct uart_8250_port *up = timer_container_of(up, t, timer);
     213              : 
     214            0 :         up->port.handle_irq(&up->port);
     215            0 :         mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
     216            0 : }
     217              : 
     218            0 : static void serial8250_backup_timeout(struct timer_list *t)
     219              : {
     220            0 :         struct uart_8250_port *up = timer_container_of(up, t, timer);
     221            0 :         unsigned int iir, ier = 0, lsr;
     222            0 :         unsigned long flags;
     223              : 
     224            0 :         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            0 :         if (up->port.irq) {
     231            0 :                 ier = serial_in(up, UART_IER);
     232            0 :                 serial_out(up, UART_IER, 0);
     233            0 :         }
     234              : 
     235            0 :         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            0 :         lsr = serial_lsr_in(up);
     244            0 :         if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
     245            0 :             (!kfifo_is_empty(&up->port.state->port.xmit_fifo) ||
     246            0 :              up->port.x_char) &&
     247            0 :             (lsr & UART_LSR_THRE)) {
     248            0 :                 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
     249            0 :                 iir |= UART_IIR_THRI;
     250            0 :         }
     251              : 
     252            0 :         if (!(iir & UART_IIR_NO_INT))
     253            0 :                 serial8250_tx_chars(up);
     254              : 
     255            0 :         if (up->port.irq)
     256            0 :                 serial_out(up, UART_IER, ier);
     257              : 
     258            0 :         uart_port_unlock_irqrestore(&up->port, flags);
     259              : 
     260              :         /* Standard timer interval plus 0.2s to keep the port running */
     261            0 :         mod_timer(&up->timer,
     262            0 :                 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
     263            0 : }
     264              : 
     265            0 : static void univ8250_setup_timer(struct uart_8250_port *up)
     266              : {
     267            0 :         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            0 :         if (up->bugs & UART_BUG_THRE) {
     274            0 :                 pr_debug("%s - using backup timer\n", port->name);
     275              : 
     276            0 :                 up->timer.function = serial8250_backup_timeout;
     277            0 :                 mod_timer(&up->timer, jiffies +
     278            0 :                           uart_poll_timeout(port) + HZ / 5);
     279            0 :         }
     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            0 :         if (!port->irq)
     287            0 :                 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
     288            0 : }
     289              : 
     290            0 : static int univ8250_setup_irq(struct uart_8250_port *up)
     291              : {
     292            0 :         struct uart_port *port = &up->port;
     293              : 
     294            0 :         if (port->irq)
     295            0 :                 return serial_link_irq_chain(up);
     296              : 
     297            0 :         return 0;
     298            0 : }
     299              : 
     300            0 : static void univ8250_release_irq(struct uart_8250_port *up)
     301              : {
     302            0 :         struct uart_port *port = &up->port;
     303              : 
     304            0 :         timer_delete_sync(&up->timer);
     305            0 :         up->timer.function = serial8250_timeout;
     306            0 :         if (port->irq)
     307            0 :                 serial_unlink_irq_chain(up);
     308            0 : }
     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              :  */
     333            0 : struct uart_8250_port *serial8250_get_port(int line)
     334              : {
     335            0 :         return &serial8250_ports[line];
     336              : }
     337              : EXPORT_SYMBOL_GPL(serial8250_get_port);
     338              : 
     339            0 : static inline void serial8250_apply_quirks(struct uart_8250_port *up)
     340              : {
     341            0 :         up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
     342            0 : }
     343              : 
     344            0 : struct uart_8250_port *serial8250_setup_port(int index)
     345              : {
     346            0 :         struct uart_8250_port *up;
     347              : 
     348            0 :         if (index >= UART_NR)
     349            0 :                 return NULL;
     350              : 
     351            0 :         up = &serial8250_ports[index];
     352            0 :         up->port.line = index;
     353            0 :         up->port.port_id = index;
     354              : 
     355            0 :         serial8250_init_port(up);
     356            0 :         if (!univ8250_port_base_ops)
     357            0 :                 univ8250_port_base_ops = up->port.ops;
     358            0 :         up->port.ops = &univ8250_port_ops;
     359              : 
     360            0 :         timer_setup(&up->timer, serial8250_timeout, 0);
     361              : 
     362            0 :         up->ops = &univ8250_driver_ops;
     363              : 
     364            0 :         serial8250_set_defaults(up);
     365              : 
     366            0 :         return up;
     367            0 : }
     368              : 
     369            0 : void __init serial8250_register_ports(struct uart_driver *drv, struct device *dev)
     370              : {
     371            0 :         int i;
     372              : 
     373            0 :         for (i = 0; i < nr_uarts; i++) {
     374            0 :                 struct uart_8250_port *up = &serial8250_ports[i];
     375              : 
     376            0 :                 if (up->port.type == PORT_8250_CIR)
     377            0 :                         continue;
     378              : 
     379            0 :                 if (up->port.dev)
     380            0 :                         continue;
     381              : 
     382            0 :                 up->port.dev = dev;
     383              : 
     384            0 :                 if (uart_console_registered(&up->port))
     385            0 :                         pm_runtime_get_sync(up->port.dev);
     386              : 
     387            0 :                 serial8250_apply_quirks(up);
     388            0 :                 uart_add_one_port(drv, &up->port);
     389            0 :         }
     390            0 : }
     391              : 
     392              : #ifdef CONFIG_SERIAL_8250_CONSOLE
     393              : 
     394            0 : static void univ8250_console_write(struct console *co, const char *s,
     395              :                                    unsigned int count)
     396              : {
     397            0 :         struct uart_8250_port *up = &serial8250_ports[co->index];
     398              : 
     399            0 :         serial8250_console_write(up, s, count);
     400            0 : }
     401              : 
     402            0 : static int univ8250_console_setup(struct console *co, char *options)
     403              : {
     404            0 :         struct uart_8250_port *up;
     405            0 :         struct uart_port *port;
     406            0 :         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            0 :         if (co->index < 0 || co->index >= UART_NR)
     414            0 :                 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            0 :         for (i = nr_uarts; i <= co->index; i++) {
     421            0 :                 up = serial8250_setup_port(i);
     422            0 :                 if (!up)
     423            0 :                         return -ENODEV;
     424            0 :                 nr_uarts++;
     425            0 :         }
     426              : 
     427            0 :         port = &serial8250_ports[co->index].port;
     428              :         /* link port to console */
     429            0 :         uart_port_set_cons(port, co);
     430              : 
     431            0 :         retval = serial8250_console_setup(port, options, false);
     432            0 :         if (retval != 0)
     433            0 :                 uart_port_set_cons(port, NULL);
     434            0 :         return retval;
     435            0 : }
     436              : 
     437            0 : static int univ8250_console_exit(struct console *co)
     438              : {
     439            0 :         struct uart_port *port;
     440              : 
     441            0 :         port = &serial8250_ports[co->index].port;
     442            0 :         return serial8250_console_exit(port);
     443            0 : }
     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              :  */
     463            0 : static int univ8250_console_match(struct console *co, char *name, int idx,
     464              :                                   char *options)
     465              : {
     466            0 :         char match[] = "uart";        /* 8250-specific earlycon name */
     467            0 :         enum uart_iotype iotype;
     468            0 :         resource_size_t addr;
     469            0 :         int i;
     470              : 
     471            0 :         if (strncmp(name, match, 4) != 0)
     472            0 :                 return -ENODEV;
     473              : 
     474            0 :         if (uart_parse_earlycon(options, &iotype, &addr, &options))
     475            0 :                 return -ENODEV;
     476              : 
     477              :         /* try to match the port specified on the command line */
     478            0 :         for (i = 0; i < nr_uarts; i++) {
     479            0 :                 struct uart_port *port = &serial8250_ports[i].port;
     480              : 
     481            0 :                 if (port->iotype != iotype)
     482            0 :                         continue;
     483            0 :                 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
     484            0 :                      iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
     485            0 :                     && (port->mapbase != addr))
     486            0 :                         continue;
     487            0 :                 if (iotype == UPIO_PORT && port->iobase != addr)
     488            0 :                         continue;
     489              : 
     490            0 :                 co->index = i;
     491            0 :                 uart_port_set_cons(port, co);
     492            0 :                 return serial8250_console_setup(port, options, true);
     493            0 :         }
     494              : 
     495            0 :         return -ENODEV;
     496            0 : }
     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              : 
     510            0 : static int __init univ8250_console_init(void)
     511              : {
     512            0 :         if (nr_uarts == 0)
     513            0 :                 return -ENODEV;
     514              : 
     515            0 :         serial8250_isa_init_ports();
     516            0 :         register_console(&univ8250_console);
     517            0 :         return 0;
     518            0 : }
     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              :  */
     541            0 : int __init early_serial_setup(struct uart_port *port)
     542              : {
     543            0 :         struct uart_port *p;
     544              : 
     545            0 :         if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
     546            0 :                 return -ENODEV;
     547              : 
     548            0 :         serial8250_isa_init_ports();
     549            0 :         p = &serial8250_ports[port->line].port;
     550            0 :         p->iobase       = port->iobase;
     551            0 :         p->membase      = port->membase;
     552            0 :         p->irq          = port->irq;
     553            0 :         p->irqflags     = port->irqflags;
     554            0 :         p->uartclk      = port->uartclk;
     555            0 :         p->fifosize     = port->fifosize;
     556            0 :         p->regshift     = port->regshift;
     557            0 :         p->iotype       = port->iotype;
     558            0 :         p->flags        = port->flags;
     559            0 :         p->mapbase      = port->mapbase;
     560            0 :         p->mapsize      = port->mapsize;
     561            0 :         p->private_data = port->private_data;
     562            0 :         p->type              = port->type;
     563            0 :         p->line              = port->line;
     564              : 
     565            0 :         serial8250_set_defaults(up_to_u8250p(p));
     566              : 
     567            0 :         if (port->serial_in)
     568            0 :                 p->serial_in = port->serial_in;
     569            0 :         if (port->serial_out)
     570            0 :                 p->serial_out = port->serial_out;
     571            0 :         if (port->handle_irq)
     572            0 :                 p->handle_irq = port->handle_irq;
     573              : 
     574            0 :         return 0;
     575            0 : }
     576              : 
     577              : /**
     578              :  *      serial8250_suspend_port - suspend one serial port
     579              :  *      @line:  serial line number
     580              :  *
     581              :  *      Suspend one serial port.
     582              :  */
     583            0 : void serial8250_suspend_port(int line)
     584              : {
     585            0 :         struct uart_8250_port *up = &serial8250_ports[line];
     586            0 :         struct uart_port *port = &up->port;
     587              : 
     588            0 :         if (!console_suspend_enabled && uart_console(port) &&
     589            0 :             port->type != PORT_8250) {
     590            0 :                 unsigned char canary = 0xa5;
     591              : 
     592            0 :                 serial_out(up, UART_SCR, canary);
     593            0 :                 if (serial_in(up, UART_SCR) == canary)
     594            0 :                         up->canary = canary;
     595            0 :         }
     596              : 
     597            0 :         uart_suspend_port(&serial8250_reg, port);
     598            0 : }
     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              :  */
     607            0 : void serial8250_resume_port(int line)
     608              : {
     609            0 :         struct uart_8250_port *up = &serial8250_ports[line];
     610            0 :         struct uart_port *port = &up->port;
     611              : 
     612            0 :         up->canary = 0;
     613              : 
     614            0 :         if (up->capabilities & UART_NATSEMI) {
     615              :                 /* Ensure it's still in high speed mode */
     616            0 :                 serial_port_out(port, UART_LCR, 0xE0);
     617              : 
     618            0 :                 ns16550a_goto_highspeed(up);
     619              : 
     620            0 :                 serial_port_out(port, UART_LCR, 0);
     621            0 :                 port->uartclk = 921600*16;
     622            0 :         }
     623            0 :         uart_resume_port(&serial8250_reg, port);
     624            0 : }
     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              : 
     634            0 : static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port)
     635              : {
     636            0 :         int i;
     637              : 
     638              :         /*
     639              :          * First, find a port entry which matches.
     640              :          */
     641            0 :         for (i = 0; i < nr_uarts; i++)
     642            0 :                 if (uart_match_port(&serial8250_ports[i].port, port))
     643            0 :                         return &serial8250_ports[i];
     644              : 
     645              :         /* try line number first if still available */
     646            0 :         i = port->line;
     647            0 :         if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
     648            0 :                         serial8250_ports[i].port.iobase == 0)
     649            0 :                 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            0 :         for (i = 0; i < nr_uarts; i++)
     656            0 :                 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
     657            0 :                     serial8250_ports[i].port.iobase == 0)
     658            0 :                         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            0 :         for (i = 0; i < nr_uarts; i++)
     665            0 :                 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
     666            0 :                         return &serial8250_ports[i];
     667              : 
     668            0 :         return NULL;
     669            0 : }
     670              : 
     671            0 : static void serial_8250_overrun_backoff_work(struct work_struct *work)
     672              : {
     673            0 :         struct uart_8250_port *up =
     674            0 :             container_of(to_delayed_work(work), struct uart_8250_port,
     675              :                          overrun_backoff);
     676            0 :         struct uart_port *port = &up->port;
     677            0 :         unsigned long flags;
     678              : 
     679            0 :         uart_port_lock_irqsave(port, &flags);
     680            0 :         up->ier |= UART_IER_RLSI | UART_IER_RDI;
     681            0 :         serial_out(up, UART_IER, up->ier);
     682            0 :         uart_port_unlock_irqrestore(port, flags);
     683            0 : }
     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              :  */
     698            0 : int serial8250_register_8250_port(const struct uart_8250_port *up)
     699              : {
     700            0 :         struct uart_8250_port *uart;
     701            0 :         int ret = -ENOSPC;
     702              : 
     703            0 :         if (up->port.uartclk == 0)
     704            0 :                 return -EINVAL;
     705              : 
     706            0 :         mutex_lock(&serial_mutex);
     707              : 
     708            0 :         uart = serial8250_find_match_or_unused(&up->port);
     709            0 :         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            0 :                 uart = serial8250_setup_port(nr_uarts);
     715            0 :                 if (!uart)
     716            0 :                         goto unlock;
     717            0 :                 nr_uarts++;
     718            0 :         }
     719              : 
     720              :         /* Check if it is CIR already. We check this below again, see there why. */
     721            0 :         if (uart->port.type == PORT_8250_CIR) {
     722            0 :                 ret = -ENODEV;
     723            0 :                 goto unlock;
     724              :         }
     725              : 
     726            0 :         if (uart->port.dev)
     727            0 :                 uart_remove_one_port(&serial8250_reg, &uart->port);
     728              : 
     729            0 :         uart->port.ctrl_id   = up->port.ctrl_id;
     730            0 :         uart->port.port_id   = up->port.port_id;
     731            0 :         uart->port.iobase       = up->port.iobase;
     732            0 :         uart->port.membase      = up->port.membase;
     733            0 :         uart->port.irq          = up->port.irq;
     734            0 :         uart->port.irqflags     = up->port.irqflags;
     735            0 :         uart->port.uartclk      = up->port.uartclk;
     736            0 :         uart->port.fifosize     = up->port.fifosize;
     737            0 :         uart->port.regshift     = up->port.regshift;
     738            0 :         uart->port.iotype       = up->port.iotype;
     739            0 :         uart->port.flags        = up->port.flags | UPF_BOOT_AUTOCONF;
     740            0 :         uart->bugs           = up->bugs;
     741            0 :         uart->port.mapbase      = up->port.mapbase;
     742            0 :         uart->port.mapsize      = up->port.mapsize;
     743            0 :         uart->port.private_data = up->port.private_data;
     744            0 :         uart->tx_loadsz              = up->tx_loadsz;
     745            0 :         uart->capabilities   = up->capabilities;
     746            0 :         uart->port.throttle  = up->port.throttle;
     747            0 :         uart->port.unthrottle        = up->port.unthrottle;
     748            0 :         uart->port.rs485_config      = up->port.rs485_config;
     749            0 :         uart->port.rs485_supported = up->port.rs485_supported;
     750            0 :         uart->port.rs485     = up->port.rs485;
     751            0 :         uart->rs485_start_tx = up->rs485_start_tx;
     752            0 :         uart->rs485_stop_tx  = up->rs485_stop_tx;
     753            0 :         uart->lsr_save_mask  = up->lsr_save_mask;
     754            0 :         uart->dma            = up->dma;
     755              : 
     756              :         /* Take tx_loadsz from fifosize if it wasn't set separately */
     757            0 :         if (uart->port.fifosize && !uart->tx_loadsz)
     758            0 :                 uart->tx_loadsz = uart->port.fifosize;
     759              : 
     760            0 :         if (up->port.dev) {
     761            0 :                 uart->port.dev = up->port.dev;
     762            0 :                 ret = uart_get_rs485_mode(&uart->port);
     763            0 :                 if (ret)
     764            0 :                         goto err;
     765            0 :         }
     766              : 
     767            0 :         if (up->port.flags & UPF_FIXED_TYPE)
     768            0 :                 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            0 :         if (!has_acpi_companion(uart->port.dev)) {
     775            0 :                 struct mctrl_gpios *gpios = mctrl_gpio_init(&uart->port, 0);
     776            0 :                 if (IS_ERR(gpios)) {
     777            0 :                         ret = PTR_ERR(gpios);
     778            0 :                         goto err;
     779              :                 } else {
     780            0 :                         uart->gpios = gpios;
     781              :                 }
     782            0 :         }
     783              : 
     784            0 :         serial8250_set_defaults(uart);
     785              : 
     786              :         /* Possibly override default I/O functions.  */
     787            0 :         if (up->port.serial_in)
     788            0 :                 uart->port.serial_in = up->port.serial_in;
     789            0 :         if (up->port.serial_out)
     790            0 :                 uart->port.serial_out = up->port.serial_out;
     791            0 :         if (up->port.handle_irq)
     792            0 :                 uart->port.handle_irq = up->port.handle_irq;
     793              :         /*  Possibly override set_termios call */
     794            0 :         if (up->port.set_termios)
     795            0 :                 uart->port.set_termios = up->port.set_termios;
     796            0 :         if (up->port.set_ldisc)
     797            0 :                 uart->port.set_ldisc = up->port.set_ldisc;
     798            0 :         if (up->port.get_mctrl)
     799            0 :                 uart->port.get_mctrl = up->port.get_mctrl;
     800            0 :         if (up->port.set_mctrl)
     801            0 :                 uart->port.set_mctrl = up->port.set_mctrl;
     802            0 :         if (up->port.get_divisor)
     803            0 :                 uart->port.get_divisor = up->port.get_divisor;
     804            0 :         if (up->port.set_divisor)
     805            0 :                 uart->port.set_divisor = up->port.set_divisor;
     806            0 :         if (up->port.startup)
     807            0 :                 uart->port.startup = up->port.startup;
     808            0 :         if (up->port.shutdown)
     809            0 :                 uart->port.shutdown = up->port.shutdown;
     810            0 :         if (up->port.pm)
     811            0 :                 uart->port.pm = up->port.pm;
     812            0 :         if (up->port.handle_break)
     813            0 :                 uart->port.handle_break = up->port.handle_break;
     814            0 :         if (up->dl_read)
     815            0 :                 uart->dl_read = up->dl_read;
     816            0 :         if (up->dl_write)
     817            0 :                 uart->dl_write = up->dl_write;
     818              : 
     819              :         /* Check the type (again)! It might have changed by the port.type assignment above. */
     820            0 :         if (uart->port.type != PORT_8250_CIR) {
     821            0 :                 if (uart_console_registered(&uart->port))
     822            0 :                         pm_runtime_get_sync(uart->port.dev);
     823              : 
     824            0 :                 if (serial8250_isa_config != NULL)
     825            0 :                         serial8250_isa_config(0, &uart->port,
     826            0 :                                         &uart->capabilities);
     827              : 
     828            0 :                 serial8250_apply_quirks(uart);
     829            0 :                 ret = uart_add_one_port(&serial8250_reg,
     830            0 :                                         &uart->port);
     831            0 :                 if (ret)
     832            0 :                         goto err;
     833              : 
     834            0 :                 ret = uart->port.line;
     835            0 :         } else {
     836            0 :                 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            0 :                 ret = 0;
     843              :         }
     844              : 
     845            0 :         if (!uart->lsr_save_mask)
     846            0 :                 uart->lsr_save_mask = LSR_SAVE_FLAGS;        /* Use default LSR mask */
     847              : 
     848              :         /* Initialise interrupt backoff work if required */
     849            0 :         if (up->overrun_backoff_time_ms > 0) {
     850            0 :                 uart->overrun_backoff_time_ms =
     851            0 :                         up->overrun_backoff_time_ms;
     852            0 :                 INIT_DELAYED_WORK(&uart->overrun_backoff,
     853              :                                 serial_8250_overrun_backoff_work);
     854            0 :         } else {
     855            0 :                 uart->overrun_backoff_time_ms = 0;
     856              :         }
     857              : 
     858              : unlock:
     859            0 :         mutex_unlock(&serial_mutex);
     860              : 
     861            0 :         return ret;
     862              : 
     863              : err:
     864            0 :         uart->port.dev = NULL;
     865            0 :         mutex_unlock(&serial_mutex);
     866            0 :         return ret;
     867            0 : }
     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              :  */
     877            0 : void serial8250_unregister_port(int line)
     878              : {
     879            0 :         struct uart_8250_port *uart = &serial8250_ports[line];
     880              : 
     881            0 :         mutex_lock(&serial_mutex);
     882              : 
     883            0 :         if (uart->em485) {
     884            0 :                 unsigned long flags;
     885              : 
     886            0 :                 uart_port_lock_irqsave(&uart->port, &flags);
     887            0 :                 serial8250_em485_destroy(uart);
     888            0 :                 uart_port_unlock_irqrestore(&uart->port, flags);
     889            0 :         }
     890              : 
     891            0 :         uart_remove_one_port(&serial8250_reg, &uart->port);
     892            0 :         if (serial8250_isa_devs) {
     893            0 :                 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
     894            0 :                 uart->port.type = PORT_UNKNOWN;
     895            0 :                 uart->port.dev = &serial8250_isa_devs->dev;
     896            0 :                 uart->port.port_id = line;
     897            0 :                 uart->capabilities = 0;
     898            0 :                 serial8250_init_port(uart);
     899            0 :                 serial8250_apply_quirks(uart);
     900            0 :                 uart_add_one_port(&serial8250_reg, &uart->port);
     901            0 :         } else {
     902            0 :                 uart->port.dev = NULL;
     903              :         }
     904            0 :         mutex_unlock(&serial_mutex);
     905            0 : }
     906              : EXPORT_SYMBOL(serial8250_unregister_port);
     907              : 
     908              : MODULE_LICENSE("GPL");
     909              : MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
        

Generated by: LCOV version 2.0-1