LCOV - code coverage report
Current view: top level - drivers/tty/serial/8250 - 8250_core.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 150 515 29.1 %
Date: 2021-04-22 12:43:58 Functions: 11 30 36.7 %

          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: ISA-compatible 8250/16550 ports
      10             :  *            PNP 8250/16550 ports
      11             :  *            early_serial_setup() ports
      12             :  *            userspace-configurable "phantom" ports
      13             :  *            "serial8250" platform devices
      14             :  *            serial8250_register_8250_port() ports
      15             :  */
      16             : 
      17             : #include <linux/acpi.h>
      18             : #include <linux/module.h>
      19             : #include <linux/moduleparam.h>
      20             : #include <linux/ioport.h>
      21             : #include <linux/init.h>
      22             : #include <linux/console.h>
      23             : #include <linux/sysrq.h>
      24             : #include <linux/delay.h>
      25             : #include <linux/platform_device.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/uaccess.h>
      35             : #include <linux/pm_runtime.h>
      36             : #include <linux/io.h>
      37             : #ifdef CONFIG_SPARC
      38             : #include <linux/sunserialcore.h>
      39             : #endif
      40             : 
      41             : #include <asm/irq.h>
      42             : 
      43             : #include "8250.h"
      44             : 
      45             : /*
      46             :  * Configuration:
      47             :  *   share_irqs - whether we pass IRQF_SHARED to request_irq().  This option
      48             :  *                is unsafe when used on edge-triggered interrupts.
      49             :  */
      50             : static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
      51             : 
      52             : static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
      53             : 
      54             : static struct uart_driver serial8250_reg;
      55             : 
      56             : static unsigned int skip_txen_test; /* force skip of txen test at init time */
      57             : 
      58             : #define PASS_LIMIT      512
      59             : 
      60             : #include <asm/serial.h>
      61             : /*
      62             :  * SERIAL_PORT_DFNS tells us about built-in ports that have no
      63             :  * standard enumeration mechanism.   Platforms that can find all
      64             :  * serial ports via mechanisms like ACPI or PCI need not supply it.
      65             :  */
      66             : #ifndef SERIAL_PORT_DFNS
      67             : #define SERIAL_PORT_DFNS
      68             : #endif
      69             : 
      70             : static const struct old_serial_port old_serial_port[] = {
      71             :         SERIAL_PORT_DFNS /* defined in asm/serial.h */
      72             : };
      73             : 
      74             : #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
      75             : 
      76             : #ifdef CONFIG_SERIAL_8250_RSA
      77             : 
      78             : #define PORT_RSA_MAX 4
      79             : static unsigned long probe_rsa[PORT_RSA_MAX];
      80             : static unsigned int probe_rsa_count;
      81             : #endif /* CONFIG_SERIAL_8250_RSA  */
      82             : 
      83             : struct irq_info {
      84             :         struct                  hlist_node node;
      85             :         int                     irq;
      86             :         spinlock_t              lock;   /* Protects list not the hash */
      87             :         struct list_head        *head;
      88             : };
      89             : 
      90             : #define NR_IRQ_HASH             32      /* Can be adjusted later */
      91             : static struct hlist_head irq_lists[NR_IRQ_HASH];
      92             : static DEFINE_MUTEX(hash_mutex);        /* Used to walk the hash */
      93             : 
      94             : /*
      95             :  * This is the serial driver's interrupt routine.
      96             :  *
      97             :  * Arjan thinks the old way was overly complex, so it got simplified.
      98             :  * Alan disagrees, saying that need the complexity to handle the weird
      99             :  * nature of ISA shared interrupts.  (This is a special exception.)
     100             :  *
     101             :  * In order to handle ISA shared interrupts properly, we need to check
     102             :  * that all ports have been serviced, and therefore the ISA interrupt
     103             :  * line has been de-asserted.
     104             :  *
     105             :  * This means we need to loop through all ports. checking that they
     106             :  * don't have an interrupt pending.
     107             :  */
     108         379 : static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
     109             : {
     110         379 :         struct irq_info *i = dev_id;
     111         379 :         struct list_head *l, *end = NULL;
     112         379 :         int pass_counter = 0, handled = 0;
     113             : 
     114         379 :         pr_debug("%s(%d): start\n", __func__, irq);
     115             : 
     116         379 :         spin_lock(&i->lock);
     117             : 
     118         379 :         l = i->head;
     119        1129 :         do {
     120        1129 :                 struct uart_8250_port *up;
     121        1129 :                 struct uart_port *port;
     122             : 
     123        1129 :                 up = list_entry(l, struct uart_8250_port, list);
     124        1129 :                 port = &up->port;
     125             : 
     126        1129 :                 if (port->handle_irq(port)) {
     127             :                         handled = 1;
     128             :                         end = NULL;
     129         379 :                 } else if (end == NULL)
     130         379 :                         end = l;
     131             : 
     132        1129 :                 l = l->next;
     133             : 
     134        1129 :                 if (l == i->head && pass_counter++ > PASS_LIMIT)
     135             :                         break;
     136        1129 :         } while (l != end);
     137             : 
     138         379 :         spin_unlock(&i->lock);
     139             : 
     140         379 :         pr_debug("%s(%d): end\n", __func__, irq);
     141             : 
     142         379 :         return IRQ_RETVAL(handled);
     143             : }
     144             : 
     145             : /*
     146             :  * To support ISA shared interrupts, we need to have one interrupt
     147             :  * handler that ensures that the IRQ line has been deasserted
     148             :  * before returning.  Failing to do this will result in the IRQ
     149             :  * line being stuck active, and, since ISA irqs are edge triggered,
     150             :  * no more IRQs will be seen.
     151             :  */
     152           0 : static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
     153             : {
     154           0 :         spin_lock_irq(&i->lock);
     155             : 
     156           0 :         if (!list_empty(i->head)) {
     157           0 :                 if (i->head == &up->list)
     158           0 :                         i->head = i->head->next;
     159           0 :                 list_del(&up->list);
     160             :         } else {
     161           0 :                 BUG_ON(i->head != &up->list);
     162           0 :                 i->head = NULL;
     163             :         }
     164           0 :         spin_unlock_irq(&i->lock);
     165             :         /* List empty so throw away the hash node */
     166           0 :         if (i->head == NULL) {
     167           0 :                 hlist_del(&i->node);
     168           0 :                 kfree(i);
     169             :         }
     170           0 : }
     171             : 
     172           1 : static int serial_link_irq_chain(struct uart_8250_port *up)
     173             : {
     174           1 :         struct hlist_head *h;
     175           1 :         struct hlist_node *n;
     176           1 :         struct irq_info *i;
     177           1 :         int ret;
     178             : 
     179           1 :         mutex_lock(&hash_mutex);
     180             : 
     181           1 :         h = &irq_lists[up->port.irq % NR_IRQ_HASH];
     182             : 
     183           1 :         hlist_for_each(n, h) {
     184           0 :                 i = hlist_entry(n, struct irq_info, node);
     185           0 :                 if (i->irq == up->port.irq)
     186             :                         break;
     187             :         }
     188             : 
     189           1 :         if (n == NULL) {
     190           1 :                 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
     191           1 :                 if (i == NULL) {
     192           0 :                         mutex_unlock(&hash_mutex);
     193           0 :                         return -ENOMEM;
     194             :                 }
     195           1 :                 spin_lock_init(&i->lock);
     196           1 :                 i->irq = up->port.irq;
     197           1 :                 hlist_add_head(&i->node, h);
     198             :         }
     199           1 :         mutex_unlock(&hash_mutex);
     200             : 
     201           1 :         spin_lock_irq(&i->lock);
     202             : 
     203           1 :         if (i->head) {
     204           0 :                 list_add(&up->list, i->head);
     205           0 :                 spin_unlock_irq(&i->lock);
     206             : 
     207           0 :                 ret = 0;
     208             :         } else {
     209           1 :                 INIT_LIST_HEAD(&up->list);
     210           1 :                 i->head = &up->list;
     211           1 :                 spin_unlock_irq(&i->lock);
     212           1 :                 ret = request_irq(up->port.irq, serial8250_interrupt,
     213             :                                   up->port.irqflags, up->port.name, i);
     214           1 :                 if (ret < 0)
     215           0 :                         serial_do_unlink(i, up);
     216             :         }
     217             : 
     218             :         return ret;
     219             : }
     220             : 
     221           0 : static void serial_unlink_irq_chain(struct uart_8250_port *up)
     222             : {
     223             :         /*
     224             :          * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
     225             :          * but no, we are not going to take a patch that assigns NULL below.
     226             :          */
     227           0 :         struct irq_info *i;
     228           0 :         struct hlist_node *n;
     229           0 :         struct hlist_head *h;
     230             : 
     231           0 :         mutex_lock(&hash_mutex);
     232             : 
     233           0 :         h = &irq_lists[up->port.irq % NR_IRQ_HASH];
     234             : 
     235           0 :         hlist_for_each(n, h) {
     236           0 :                 i = hlist_entry(n, struct irq_info, node);
     237           0 :                 if (i->irq == up->port.irq)
     238             :                         break;
     239             :         }
     240             : 
     241           0 :         BUG_ON(n == NULL);
     242           0 :         BUG_ON(i->head == NULL);
     243             : 
     244           0 :         if (list_empty(i->head))
     245           0 :                 free_irq(up->port.irq, i);
     246             : 
     247           0 :         serial_do_unlink(i, up);
     248           0 :         mutex_unlock(&hash_mutex);
     249           0 : }
     250             : 
     251             : /*
     252             :  * This function is used to handle ports that do not have an
     253             :  * interrupt.  This doesn't work very well for 16450's, but gives
     254             :  * barely passable results for a 16550A.  (Although at the expense
     255             :  * of much CPU overhead).
     256             :  */
     257           0 : static void serial8250_timeout(struct timer_list *t)
     258             : {
     259           0 :         struct uart_8250_port *up = from_timer(up, t, timer);
     260             : 
     261           0 :         up->port.handle_irq(&up->port);
     262           0 :         mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
     263           0 : }
     264             : 
     265           0 : static void serial8250_backup_timeout(struct timer_list *t)
     266             : {
     267           0 :         struct uart_8250_port *up = from_timer(up, t, timer);
     268           0 :         unsigned int iir, ier = 0, lsr;
     269           0 :         unsigned long flags;
     270             : 
     271           0 :         spin_lock_irqsave(&up->port.lock, flags);
     272             : 
     273             :         /*
     274             :          * Must disable interrupts or else we risk racing with the interrupt
     275             :          * based handler.
     276             :          */
     277           0 :         if (up->port.irq) {
     278           0 :                 ier = serial_in(up, UART_IER);
     279           0 :                 serial_out(up, UART_IER, 0);
     280             :         }
     281             : 
     282           0 :         iir = serial_in(up, UART_IIR);
     283             : 
     284             :         /*
     285             :          * This should be a safe test for anyone who doesn't trust the
     286             :          * IIR bits on their UART, but it's specifically designed for
     287             :          * the "Diva" UART used on the management processor on many HP
     288             :          * ia64 and parisc boxes.
     289             :          */
     290           0 :         lsr = serial_in(up, UART_LSR);
     291           0 :         up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
     292           0 :         if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
     293           0 :             (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
     294           0 :             (lsr & UART_LSR_THRE)) {
     295           0 :                 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
     296           0 :                 iir |= UART_IIR_THRI;
     297             :         }
     298             : 
     299           0 :         if (!(iir & UART_IIR_NO_INT))
     300           0 :                 serial8250_tx_chars(up);
     301             : 
     302           0 :         if (up->port.irq)
     303           0 :                 serial_out(up, UART_IER, ier);
     304             : 
     305           0 :         spin_unlock_irqrestore(&up->port.lock, flags);
     306             : 
     307             :         /* Standard timer interval plus 0.2s to keep the port running */
     308           0 :         mod_timer(&up->timer,
     309           0 :                 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
     310           0 : }
     311             : 
     312           1 : static int univ8250_setup_irq(struct uart_8250_port *up)
     313             : {
     314           1 :         struct uart_port *port = &up->port;
     315           1 :         int retval = 0;
     316             : 
     317             :         /*
     318             :          * The above check will only give an accurate result the first time
     319             :          * the port is opened so this value needs to be preserved.
     320             :          */
     321           1 :         if (up->bugs & UART_BUG_THRE) {
     322           0 :                 pr_debug("%s - using backup timer\n", port->name);
     323             : 
     324           0 :                 up->timer.function = serial8250_backup_timeout;
     325           0 :                 mod_timer(&up->timer, jiffies +
     326           0 :                           uart_poll_timeout(port) + HZ / 5);
     327             :         }
     328             : 
     329             :         /*
     330             :          * If the "interrupt" for this port doesn't correspond with any
     331             :          * hardware interrupt, we use a timer-based system.  The original
     332             :          * driver used to do this with IRQ0.
     333             :          */
     334           1 :         if (!port->irq) {
     335           0 :                 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
     336             :         } else
     337           1 :                 retval = serial_link_irq_chain(up);
     338             : 
     339           1 :         return retval;
     340             : }
     341             : 
     342           0 : static void univ8250_release_irq(struct uart_8250_port *up)
     343             : {
     344           0 :         struct uart_port *port = &up->port;
     345             : 
     346           0 :         del_timer_sync(&up->timer);
     347           0 :         up->timer.function = serial8250_timeout;
     348           0 :         if (port->irq)
     349           0 :                 serial_unlink_irq_chain(up);
     350           0 : }
     351             : 
     352             : #ifdef CONFIG_SERIAL_8250_RSA
     353             : static int serial8250_request_rsa_resource(struct uart_8250_port *up)
     354             : {
     355             :         unsigned long start = UART_RSA_BASE << up->port.regshift;
     356             :         unsigned int size = 8 << up->port.regshift;
     357             :         struct uart_port *port = &up->port;
     358             :         int ret = -EINVAL;
     359             : 
     360             :         switch (port->iotype) {
     361             :         case UPIO_HUB6:
     362             :         case UPIO_PORT:
     363             :                 start += port->iobase;
     364             :                 if (request_region(start, size, "serial-rsa"))
     365             :                         ret = 0;
     366             :                 else
     367             :                         ret = -EBUSY;
     368             :                 break;
     369             :         }
     370             : 
     371             :         return ret;
     372             : }
     373             : 
     374             : static void serial8250_release_rsa_resource(struct uart_8250_port *up)
     375             : {
     376             :         unsigned long offset = UART_RSA_BASE << up->port.regshift;
     377             :         unsigned int size = 8 << up->port.regshift;
     378             :         struct uart_port *port = &up->port;
     379             : 
     380             :         switch (port->iotype) {
     381             :         case UPIO_HUB6:
     382             :         case UPIO_PORT:
     383             :                 release_region(port->iobase + offset, size);
     384             :                 break;
     385             :         }
     386             : }
     387             : #endif
     388             : 
     389             : static const struct uart_ops *base_ops;
     390             : static struct uart_ops univ8250_port_ops;
     391             : 
     392             : static const struct uart_8250_ops univ8250_driver_ops = {
     393             :         .setup_irq      = univ8250_setup_irq,
     394             :         .release_irq    = univ8250_release_irq,
     395             : };
     396             : 
     397             : static struct uart_8250_port serial8250_ports[UART_NR];
     398             : 
     399             : /**
     400             :  * serial8250_get_port - retrieve struct uart_8250_port
     401             :  * @line: serial line number
     402             :  *
     403             :  * This function retrieves struct uart_8250_port for the specific line.
     404             :  * This struct *must* *not* be used to perform a 8250 or serial core operation
     405             :  * which is not accessible otherwise. Its only purpose is to make the struct
     406             :  * accessible to the runtime-pm callbacks for context suspend/restore.
     407             :  * The lock assumption made here is none because runtime-pm suspend/resume
     408             :  * callbacks should not be invoked if there is any operation performed on the
     409             :  * port.
     410             :  */
     411           0 : struct uart_8250_port *serial8250_get_port(int line)
     412             : {
     413           0 :         return &serial8250_ports[line];
     414             : }
     415             : EXPORT_SYMBOL_GPL(serial8250_get_port);
     416             : 
     417             : static void (*serial8250_isa_config)(int port, struct uart_port *up,
     418             :         u32 *capabilities);
     419             : 
     420           0 : void serial8250_set_isa_configurator(
     421             :         void (*v)(int port, struct uart_port *up, u32 *capabilities))
     422             : {
     423           0 :         serial8250_isa_config = v;
     424           0 : }
     425             : EXPORT_SYMBOL(serial8250_set_isa_configurator);
     426             : 
     427             : #ifdef CONFIG_SERIAL_8250_RSA
     428             : 
     429             : static void univ8250_config_port(struct uart_port *port, int flags)
     430             : {
     431             :         struct uart_8250_port *up = up_to_u8250p(port);
     432             : 
     433             :         up->probe &= ~UART_PROBE_RSA;
     434             :         if (port->type == PORT_RSA) {
     435             :                 if (serial8250_request_rsa_resource(up) == 0)
     436             :                         up->probe |= UART_PROBE_RSA;
     437             :         } else if (flags & UART_CONFIG_TYPE) {
     438             :                 int i;
     439             : 
     440             :                 for (i = 0; i < probe_rsa_count; i++) {
     441             :                         if (probe_rsa[i] == up->port.iobase) {
     442             :                                 if (serial8250_request_rsa_resource(up) == 0)
     443             :                                         up->probe |= UART_PROBE_RSA;
     444             :                                 break;
     445             :                         }
     446             :                 }
     447             :         }
     448             : 
     449             :         base_ops->config_port(port, flags);
     450             : 
     451             :         if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
     452             :                 serial8250_release_rsa_resource(up);
     453             : }
     454             : 
     455             : static int univ8250_request_port(struct uart_port *port)
     456             : {
     457             :         struct uart_8250_port *up = up_to_u8250p(port);
     458             :         int ret;
     459             : 
     460             :         ret = base_ops->request_port(port);
     461             :         if (ret == 0 && port->type == PORT_RSA) {
     462             :                 ret = serial8250_request_rsa_resource(up);
     463             :                 if (ret < 0)
     464             :                         base_ops->release_port(port);
     465             :         }
     466             : 
     467             :         return ret;
     468             : }
     469             : 
     470             : static void univ8250_release_port(struct uart_port *port)
     471             : {
     472             :         struct uart_8250_port *up = up_to_u8250p(port);
     473             : 
     474             :         if (port->type == PORT_RSA)
     475             :                 serial8250_release_rsa_resource(up);
     476             :         base_ops->release_port(port);
     477             : }
     478             : 
     479             : static void univ8250_rsa_support(struct uart_ops *ops)
     480             : {
     481             :         ops->config_port  = univ8250_config_port;
     482             :         ops->request_port = univ8250_request_port;
     483             :         ops->release_port = univ8250_release_port;
     484             : }
     485             : 
     486             : #else
     487             : #define univ8250_rsa_support(x)         do { } while (0)
     488             : #endif /* CONFIG_SERIAL_8250_RSA */
     489             : 
     490           4 : static inline void serial8250_apply_quirks(struct uart_8250_port *up)
     491             : {
     492           8 :         up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
     493             : }
     494             : 
     495           2 : static void __init serial8250_isa_init_ports(void)
     496             : {
     497           2 :         struct uart_8250_port *up;
     498           2 :         static int first = 1;
     499           2 :         int i, irqflag = 0;
     500             : 
     501           2 :         if (!first)
     502             :                 return;
     503           1 :         first = 0;
     504             : 
     505           1 :         if (nr_uarts > UART_NR)
     506           0 :                 nr_uarts = UART_NR;
     507             : 
     508           5 :         for (i = 0; i < nr_uarts; i++) {
     509           4 :                 struct uart_8250_port *up = &serial8250_ports[i];
     510           4 :                 struct uart_port *port = &up->port;
     511             : 
     512           4 :                 port->line = i;
     513           4 :                 serial8250_init_port(up);
     514           4 :                 if (!base_ops)
     515           1 :                         base_ops = port->ops;
     516           4 :                 port->ops = &univ8250_port_ops;
     517             : 
     518           4 :                 timer_setup(&up->timer, serial8250_timeout, 0);
     519             : 
     520           4 :                 up->ops = &univ8250_driver_ops;
     521             : 
     522             :                 /*
     523             :                  * ALPHA_KLUDGE_MCR needs to be killed.
     524             :                  */
     525           4 :                 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
     526           4 :                 up->mcr_force = ALPHA_KLUDGE_MCR;
     527           4 :                 serial8250_set_defaults(up);
     528             :         }
     529             : 
     530             :         /* chain base port ops to support Remote Supervisor Adapter */
     531           1 :         univ8250_port_ops = *base_ops;
     532           1 :         univ8250_rsa_support(&univ8250_port_ops);
     533             : 
     534           1 :         if (share_irqs)
     535           0 :                 irqflag = IRQF_SHARED;
     536             : 
     537           1 :         for (i = 0, up = serial8250_ports;
     538           5 :              i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
     539           4 :              i++, up++) {
     540           4 :                 struct uart_port *port = &up->port;
     541             : 
     542           4 :                 port->iobase   = old_serial_port[i].port;
     543           4 :                 port->irq      = irq_canonicalize(old_serial_port[i].irq);
     544           4 :                 port->irqflags = 0;
     545           4 :                 port->uartclk  = old_serial_port[i].baud_base * 16;
     546           4 :                 port->flags    = old_serial_port[i].flags;
     547           4 :                 port->hub6     = 0;
     548           4 :                 port->membase  = old_serial_port[i].iomem_base;
     549           4 :                 port->iotype   = old_serial_port[i].io_type;
     550           4 :                 port->regshift = old_serial_port[i].iomem_reg_shift;
     551             : 
     552           4 :                 port->irqflags |= irqflag;
     553           4 :                 if (serial8250_isa_config != NULL)
     554           0 :                         serial8250_isa_config(i, &up->port, &up->capabilities);
     555             :         }
     556             : }
     557             : 
     558             : static void __init
     559           1 : serial8250_register_ports(struct uart_driver *drv, struct device *dev)
     560             : {
     561           1 :         int i;
     562             : 
     563           5 :         for (i = 0; i < nr_uarts; i++) {
     564           4 :                 struct uart_8250_port *up = &serial8250_ports[i];
     565             : 
     566           4 :                 if (up->port.type == PORT_8250_CIR)
     567           0 :                         continue;
     568             : 
     569           4 :                 if (up->port.dev)
     570           0 :                         continue;
     571             : 
     572           4 :                 up->port.dev = dev;
     573             : 
     574           4 :                 serial8250_apply_quirks(up);
     575           4 :                 uart_add_one_port(drv, &up->port);
     576             :         }
     577           1 : }
     578             : 
     579             : #ifdef CONFIG_SERIAL_8250_CONSOLE
     580             : 
     581         256 : static void univ8250_console_write(struct console *co, const char *s,
     582             :                                    unsigned int count)
     583             : {
     584         256 :         struct uart_8250_port *up = &serial8250_ports[co->index];
     585             : 
     586         256 :         serial8250_console_write(up, s, count);
     587         256 : }
     588             : 
     589           1 : static int univ8250_console_setup(struct console *co, char *options)
     590             : {
     591           1 :         struct uart_port *port;
     592           1 :         int retval;
     593             : 
     594             :         /*
     595             :          * Check whether an invalid uart number has been specified, and
     596             :          * if so, search for the first available port that does have
     597             :          * console support.
     598             :          */
     599           1 :         if (co->index >= nr_uarts)
     600           0 :                 co->index = 0;
     601           1 :         port = &serial8250_ports[co->index].port;
     602             :         /* link port to console */
     603           1 :         port->cons = co;
     604             : 
     605           1 :         retval = serial8250_console_setup(port, options, false);
     606           1 :         if (retval != 0)
     607           0 :                 port->cons = NULL;
     608           1 :         return retval;
     609             : }
     610             : 
     611           0 : static int univ8250_console_exit(struct console *co)
     612             : {
     613           0 :         struct uart_port *port;
     614             : 
     615           0 :         port = &serial8250_ports[co->index].port;
     616           0 :         return serial8250_console_exit(port);
     617             : }
     618             : 
     619             : /**
     620             :  *      univ8250_console_match - non-standard console matching
     621             :  *      @co:      registering console
     622             :  *      @name:    name from console command line
     623             :  *      @idx:     index from console command line
     624             :  *      @options: ptr to option string from console command line
     625             :  *
     626             :  *      Only attempts to match console command lines of the form:
     627             :  *          console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
     628             :  *          console=uart[8250],0x<addr>[,<options>]
     629             :  *      This form is used to register an initial earlycon boot console and
     630             :  *      replace it with the serial8250_console at 8250 driver init.
     631             :  *
     632             :  *      Performs console setup for a match (as required by interface)
     633             :  *      If no <options> are specified, then assume the h/w is already setup.
     634             :  *
     635             :  *      Returns 0 if console matches; otherwise non-zero to use default matching
     636             :  */
     637           1 : static int univ8250_console_match(struct console *co, char *name, int idx,
     638             :                                   char *options)
     639             : {
     640           1 :         char match[] = "uart";        /* 8250-specific earlycon name */
     641           1 :         unsigned char iotype;
     642           1 :         resource_size_t addr;
     643           1 :         int i;
     644             : 
     645           1 :         if (strncmp(name, match, 4) != 0)
     646             :                 return -ENODEV;
     647             : 
     648           0 :         if (uart_parse_earlycon(options, &iotype, &addr, &options))
     649             :                 return -ENODEV;
     650             : 
     651             :         /* try to match the port specified on the command line */
     652           0 :         for (i = 0; i < nr_uarts; i++) {
     653           0 :                 struct uart_port *port = &serial8250_ports[i].port;
     654             : 
     655           0 :                 if (port->iotype != iotype)
     656           0 :                         continue;
     657           0 :                 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
     658           0 :                      iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
     659           0 :                     && (port->mapbase != addr))
     660           0 :                         continue;
     661           0 :                 if (iotype == UPIO_PORT && port->iobase != addr)
     662           0 :                         continue;
     663             : 
     664           0 :                 co->index = i;
     665           0 :                 port->cons = co;
     666           0 :                 return serial8250_console_setup(port, options, true);
     667             :         }
     668             : 
     669             :         return -ENODEV;
     670             : }
     671             : 
     672             : static struct console univ8250_console = {
     673             :         .name           = "ttyS",
     674             :         .write          = univ8250_console_write,
     675             :         .device         = uart_console_device,
     676             :         .setup          = univ8250_console_setup,
     677             :         .exit           = univ8250_console_exit,
     678             :         .match          = univ8250_console_match,
     679             :         .flags          = CON_PRINTBUFFER | CON_ANYTIME,
     680             :         .index          = -1,
     681             :         .data           = &serial8250_reg,
     682             : };
     683             : 
     684           1 : static int __init univ8250_console_init(void)
     685             : {
     686           1 :         if (nr_uarts == 0)
     687             :                 return -ENODEV;
     688             : 
     689           1 :         serial8250_isa_init_ports();
     690           1 :         register_console(&univ8250_console);
     691           1 :         return 0;
     692             : }
     693             : console_initcall(univ8250_console_init);
     694             : 
     695             : #define SERIAL8250_CONSOLE      (&univ8250_console)
     696             : #else
     697             : #define SERIAL8250_CONSOLE      NULL
     698             : #endif
     699             : 
     700             : static struct uart_driver serial8250_reg = {
     701             :         .owner                  = THIS_MODULE,
     702             :         .driver_name            = "serial",
     703             :         .dev_name               = "ttyS",
     704             :         .major                  = TTY_MAJOR,
     705             :         .minor                  = 64,
     706             :         .cons                   = SERIAL8250_CONSOLE,
     707             : };
     708             : 
     709             : /*
     710             :  * early_serial_setup - early registration for 8250 ports
     711             :  *
     712             :  * Setup an 8250 port structure prior to console initialisation.  Use
     713             :  * after console initialisation will cause undefined behaviour.
     714             :  */
     715           0 : int __init early_serial_setup(struct uart_port *port)
     716             : {
     717           0 :         struct uart_port *p;
     718             : 
     719           0 :         if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
     720             :                 return -ENODEV;
     721             : 
     722           0 :         serial8250_isa_init_ports();
     723           0 :         p = &serial8250_ports[port->line].port;
     724           0 :         p->iobase       = port->iobase;
     725           0 :         p->membase      = port->membase;
     726           0 :         p->irq          = port->irq;
     727           0 :         p->irqflags     = port->irqflags;
     728           0 :         p->uartclk      = port->uartclk;
     729           0 :         p->fifosize     = port->fifosize;
     730           0 :         p->regshift     = port->regshift;
     731           0 :         p->iotype       = port->iotype;
     732           0 :         p->flags        = port->flags;
     733           0 :         p->mapbase      = port->mapbase;
     734           0 :         p->mapsize      = port->mapsize;
     735           0 :         p->private_data = port->private_data;
     736           0 :         p->type              = port->type;
     737           0 :         p->line              = port->line;
     738             : 
     739           0 :         serial8250_set_defaults(up_to_u8250p(p));
     740             : 
     741           0 :         if (port->serial_in)
     742           0 :                 p->serial_in = port->serial_in;
     743           0 :         if (port->serial_out)
     744           0 :                 p->serial_out = port->serial_out;
     745           0 :         if (port->handle_irq)
     746           0 :                 p->handle_irq = port->handle_irq;
     747             : 
     748             :         return 0;
     749             : }
     750             : 
     751             : /**
     752             :  *      serial8250_suspend_port - suspend one serial port
     753             :  *      @line:  serial line number
     754             :  *
     755             :  *      Suspend one serial port.
     756             :  */
     757           0 : void serial8250_suspend_port(int line)
     758             : {
     759           0 :         struct uart_8250_port *up = &serial8250_ports[line];
     760           0 :         struct uart_port *port = &up->port;
     761             : 
     762           0 :         if (!console_suspend_enabled && uart_console(port) &&
     763           0 :             port->type != PORT_8250) {
     764           0 :                 unsigned char canary = 0xa5;
     765           0 :                 serial_out(up, UART_SCR, canary);
     766           0 :                 if (serial_in(up, UART_SCR) == canary)
     767           0 :                         up->canary = canary;
     768             :         }
     769             : 
     770           0 :         uart_suspend_port(&serial8250_reg, port);
     771           0 : }
     772             : EXPORT_SYMBOL(serial8250_suspend_port);
     773             : 
     774             : /**
     775             :  *      serial8250_resume_port - resume one serial port
     776             :  *      @line:  serial line number
     777             :  *
     778             :  *      Resume one serial port.
     779             :  */
     780           0 : void serial8250_resume_port(int line)
     781             : {
     782           0 :         struct uart_8250_port *up = &serial8250_ports[line];
     783           0 :         struct uart_port *port = &up->port;
     784             : 
     785           0 :         up->canary = 0;
     786             : 
     787           0 :         if (up->capabilities & UART_NATSEMI) {
     788             :                 /* Ensure it's still in high speed mode */
     789           0 :                 serial_port_out(port, UART_LCR, 0xE0);
     790             : 
     791           0 :                 ns16550a_goto_highspeed(up);
     792             : 
     793           0 :                 serial_port_out(port, UART_LCR, 0);
     794           0 :                 port->uartclk = 921600*16;
     795             :         }
     796           0 :         uart_resume_port(&serial8250_reg, port);
     797           0 : }
     798             : EXPORT_SYMBOL(serial8250_resume_port);
     799             : 
     800             : /*
     801             :  * Register a set of serial devices attached to a platform device.  The
     802             :  * list is terminated with a zero flags entry, which means we expect
     803             :  * all entries to have at least UPF_BOOT_AUTOCONF set.
     804             :  */
     805           1 : static int serial8250_probe(struct platform_device *dev)
     806             : {
     807           1 :         struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
     808           1 :         struct uart_8250_port uart;
     809           1 :         int ret, i, irqflag = 0;
     810             : 
     811           1 :         memset(&uart, 0, sizeof(uart));
     812             : 
     813           1 :         if (share_irqs)
     814           0 :                 irqflag = IRQF_SHARED;
     815             : 
     816           1 :         for (i = 0; p && p->flags != 0; p++, i++) {
     817           0 :                 uart.port.iobase        = p->iobase;
     818           0 :                 uart.port.membase       = p->membase;
     819           0 :                 uart.port.irq           = p->irq;
     820           0 :                 uart.port.irqflags      = p->irqflags;
     821           0 :                 uart.port.uartclk       = p->uartclk;
     822           0 :                 uart.port.regshift      = p->regshift;
     823           0 :                 uart.port.iotype        = p->iotype;
     824           0 :                 uart.port.flags         = p->flags;
     825           0 :                 uart.port.mapbase       = p->mapbase;
     826           0 :                 uart.port.hub6          = p->hub6;
     827           0 :                 uart.port.has_sysrq     = p->has_sysrq;
     828           0 :                 uart.port.private_data  = p->private_data;
     829           0 :                 uart.port.type          = p->type;
     830           0 :                 uart.port.serial_in     = p->serial_in;
     831           0 :                 uart.port.serial_out    = p->serial_out;
     832           0 :                 uart.port.handle_irq    = p->handle_irq;
     833           0 :                 uart.port.handle_break  = p->handle_break;
     834           0 :                 uart.port.set_termios   = p->set_termios;
     835           0 :                 uart.port.set_ldisc     = p->set_ldisc;
     836           0 :                 uart.port.get_mctrl     = p->get_mctrl;
     837           0 :                 uart.port.pm            = p->pm;
     838           0 :                 uart.port.dev           = &dev->dev;
     839           0 :                 uart.port.irqflags      |= irqflag;
     840           0 :                 ret = serial8250_register_8250_port(&uart);
     841           0 :                 if (ret < 0) {
     842           0 :                         dev_err(&dev->dev, "unable to register port at index %d "
     843             :                                 "(IO%lx MEM%llx IRQ%d): %d\n", i,
     844             :                                 p->iobase, (unsigned long long)p->mapbase,
     845             :                                 p->irq, ret);
     846             :                 }
     847             :         }
     848           1 :         return 0;
     849             : }
     850             : 
     851             : /*
     852             :  * Remove serial ports registered against a platform device.
     853             :  */
     854           0 : static int serial8250_remove(struct platform_device *dev)
     855             : {
     856           0 :         int i;
     857             : 
     858           0 :         for (i = 0; i < nr_uarts; i++) {
     859           0 :                 struct uart_8250_port *up = &serial8250_ports[i];
     860             : 
     861           0 :                 if (up->port.dev == &dev->dev)
     862           0 :                         serial8250_unregister_port(i);
     863             :         }
     864           0 :         return 0;
     865             : }
     866             : 
     867           0 : static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
     868             : {
     869           0 :         int i;
     870             : 
     871           0 :         for (i = 0; i < UART_NR; i++) {
     872           0 :                 struct uart_8250_port *up = &serial8250_ports[i];
     873             : 
     874           0 :                 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
     875           0 :                         uart_suspend_port(&serial8250_reg, &up->port);
     876             :         }
     877             : 
     878           0 :         return 0;
     879             : }
     880             : 
     881           0 : static int serial8250_resume(struct platform_device *dev)
     882             : {
     883           0 :         int i;
     884             : 
     885           0 :         for (i = 0; i < UART_NR; i++) {
     886           0 :                 struct uart_8250_port *up = &serial8250_ports[i];
     887             : 
     888           0 :                 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
     889           0 :                         serial8250_resume_port(i);
     890             :         }
     891             : 
     892           0 :         return 0;
     893             : }
     894             : 
     895             : static struct platform_driver serial8250_isa_driver = {
     896             :         .probe          = serial8250_probe,
     897             :         .remove         = serial8250_remove,
     898             :         .suspend        = serial8250_suspend,
     899             :         .resume         = serial8250_resume,
     900             :         .driver         = {
     901             :                 .name   = "serial8250",
     902             :         },
     903             : };
     904             : 
     905             : /*
     906             :  * This "device" covers _all_ ISA 8250-compatible serial devices listed
     907             :  * in the table in include/asm/serial.h
     908             :  */
     909             : static struct platform_device *serial8250_isa_devs;
     910             : 
     911             : /*
     912             :  * serial8250_register_8250_port and serial8250_unregister_port allows for
     913             :  * 16x50 serial ports to be configured at run-time, to support PCMCIA
     914             :  * modems and PCI multiport cards.
     915             :  */
     916             : static DEFINE_MUTEX(serial_mutex);
     917             : 
     918           0 : static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
     919             : {
     920           0 :         int i;
     921             : 
     922             :         /*
     923             :          * First, find a port entry which matches.
     924             :          */
     925           0 :         for (i = 0; i < nr_uarts; i++)
     926           0 :                 if (uart_match_port(&serial8250_ports[i].port, port))
     927           0 :                         return &serial8250_ports[i];
     928             : 
     929             :         /* try line number first if still available */
     930           0 :         i = port->line;
     931           0 :         if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
     932           0 :                         serial8250_ports[i].port.iobase == 0)
     933           0 :                 return &serial8250_ports[i];
     934             :         /*
     935             :          * We didn't find a matching entry, so look for the first
     936             :          * free entry.  We look for one which hasn't been previously
     937             :          * used (indicated by zero iobase).
     938             :          */
     939           0 :         for (i = 0; i < nr_uarts; i++)
     940           0 :                 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
     941           0 :                     serial8250_ports[i].port.iobase == 0)
     942           0 :                         return &serial8250_ports[i];
     943             : 
     944             :         /*
     945             :          * That also failed.  Last resort is to find any entry which
     946             :          * doesn't have a real port associated with it.
     947             :          */
     948           0 :         for (i = 0; i < nr_uarts; i++)
     949           0 :                 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
     950           0 :                         return &serial8250_ports[i];
     951             : 
     952             :         return NULL;
     953             : }
     954             : 
     955           0 : static void serial_8250_overrun_backoff_work(struct work_struct *work)
     956             : {
     957           0 :         struct uart_8250_port *up =
     958           0 :             container_of(to_delayed_work(work), struct uart_8250_port,
     959             :                          overrun_backoff);
     960           0 :         struct uart_port *port = &up->port;
     961           0 :         unsigned long flags;
     962             : 
     963           0 :         spin_lock_irqsave(&port->lock, flags);
     964           0 :         up->ier |= UART_IER_RLSI | UART_IER_RDI;
     965           0 :         up->port.read_status_mask |= UART_LSR_DR;
     966           0 :         serial_out(up, UART_IER, up->ier);
     967           0 :         spin_unlock_irqrestore(&port->lock, flags);
     968           0 : }
     969             : 
     970             : /**
     971             :  *      serial8250_register_8250_port - register a serial port
     972             :  *      @up: serial port template
     973             :  *
     974             :  *      Configure the serial port specified by the request. If the
     975             :  *      port exists and is in use, it is hung up and unregistered
     976             :  *      first.
     977             :  *
     978             :  *      The port is then probed and if necessary the IRQ is autodetected
     979             :  *      If this fails an error is returned.
     980             :  *
     981             :  *      On success the port is ready to use and the line number is returned.
     982             :  */
     983           0 : int serial8250_register_8250_port(struct uart_8250_port *up)
     984             : {
     985           0 :         struct uart_8250_port *uart;
     986           0 :         int ret = -ENOSPC;
     987             : 
     988           0 :         if (up->port.uartclk == 0)
     989             :                 return -EINVAL;
     990             : 
     991           0 :         mutex_lock(&serial_mutex);
     992             : 
     993           0 :         uart = serial8250_find_match_or_unused(&up->port);
     994           0 :         if (uart && uart->port.type != PORT_8250_CIR) {
     995           0 :                 struct mctrl_gpios *gpios;
     996             : 
     997           0 :                 if (uart->port.dev)
     998           0 :                         uart_remove_one_port(&serial8250_reg, &uart->port);
     999             : 
    1000           0 :                 uart->port.iobase       = up->port.iobase;
    1001           0 :                 uart->port.membase      = up->port.membase;
    1002           0 :                 uart->port.irq          = up->port.irq;
    1003           0 :                 uart->port.irqflags     = up->port.irqflags;
    1004           0 :                 uart->port.uartclk      = up->port.uartclk;
    1005           0 :                 uart->port.fifosize     = up->port.fifosize;
    1006           0 :                 uart->port.regshift     = up->port.regshift;
    1007           0 :                 uart->port.iotype       = up->port.iotype;
    1008           0 :                 uart->port.flags        = up->port.flags | UPF_BOOT_AUTOCONF;
    1009           0 :                 uart->bugs           = up->bugs;
    1010           0 :                 uart->port.mapbase      = up->port.mapbase;
    1011           0 :                 uart->port.mapsize      = up->port.mapsize;
    1012           0 :                 uart->port.private_data = up->port.private_data;
    1013           0 :                 uart->tx_loadsz              = up->tx_loadsz;
    1014           0 :                 uart->capabilities   = up->capabilities;
    1015           0 :                 uart->port.throttle  = up->port.throttle;
    1016           0 :                 uart->port.unthrottle        = up->port.unthrottle;
    1017           0 :                 uart->port.rs485_config      = up->port.rs485_config;
    1018           0 :                 uart->port.rs485     = up->port.rs485;
    1019           0 :                 uart->rs485_start_tx = up->rs485_start_tx;
    1020           0 :                 uart->rs485_stop_tx  = up->rs485_stop_tx;
    1021           0 :                 uart->dma            = up->dma;
    1022             : 
    1023             :                 /* Take tx_loadsz from fifosize if it wasn't set separately */
    1024           0 :                 if (uart->port.fifosize && !uart->tx_loadsz)
    1025           0 :                         uart->tx_loadsz = uart->port.fifosize;
    1026             : 
    1027           0 :                 if (up->port.dev) {
    1028           0 :                         uart->port.dev = up->port.dev;
    1029           0 :                         ret = uart_get_rs485_mode(&uart->port);
    1030           0 :                         if (ret)
    1031           0 :                                 goto err;
    1032             :                 }
    1033             : 
    1034           0 :                 if (up->port.flags & UPF_FIXED_TYPE)
    1035           0 :                         uart->port.type = up->port.type;
    1036             : 
    1037             :                 /*
    1038             :                  * Only call mctrl_gpio_init(), if the device has no ACPI
    1039             :                  * companion device
    1040             :                  */
    1041           0 :                 if (!has_acpi_companion(uart->port.dev)) {
    1042           0 :                         gpios = mctrl_gpio_init(&uart->port, 0);
    1043           0 :                         if (IS_ERR(gpios)) {
    1044             :                                 ret = PTR_ERR(gpios);
    1045             :                                 goto err;
    1046             :                         } else {
    1047           0 :                                 uart->gpios = gpios;
    1048             :                         }
    1049             :                 }
    1050             : 
    1051           0 :                 serial8250_set_defaults(uart);
    1052             : 
    1053             :                 /* Possibly override default I/O functions.  */
    1054           0 :                 if (up->port.serial_in)
    1055           0 :                         uart->port.serial_in = up->port.serial_in;
    1056           0 :                 if (up->port.serial_out)
    1057           0 :                         uart->port.serial_out = up->port.serial_out;
    1058           0 :                 if (up->port.handle_irq)
    1059           0 :                         uart->port.handle_irq = up->port.handle_irq;
    1060             :                 /*  Possibly override set_termios call */
    1061           0 :                 if (up->port.set_termios)
    1062           0 :                         uart->port.set_termios = up->port.set_termios;
    1063           0 :                 if (up->port.set_ldisc)
    1064           0 :                         uart->port.set_ldisc = up->port.set_ldisc;
    1065           0 :                 if (up->port.get_mctrl)
    1066           0 :                         uart->port.get_mctrl = up->port.get_mctrl;
    1067           0 :                 if (up->port.set_mctrl)
    1068           0 :                         uart->port.set_mctrl = up->port.set_mctrl;
    1069           0 :                 if (up->port.get_divisor)
    1070           0 :                         uart->port.get_divisor = up->port.get_divisor;
    1071           0 :                 if (up->port.set_divisor)
    1072           0 :                         uart->port.set_divisor = up->port.set_divisor;
    1073           0 :                 if (up->port.startup)
    1074           0 :                         uart->port.startup = up->port.startup;
    1075           0 :                 if (up->port.shutdown)
    1076           0 :                         uart->port.shutdown = up->port.shutdown;
    1077           0 :                 if (up->port.pm)
    1078           0 :                         uart->port.pm = up->port.pm;
    1079           0 :                 if (up->port.handle_break)
    1080           0 :                         uart->port.handle_break = up->port.handle_break;
    1081           0 :                 if (up->dl_read)
    1082           0 :                         uart->dl_read = up->dl_read;
    1083           0 :                 if (up->dl_write)
    1084           0 :                         uart->dl_write = up->dl_write;
    1085             : 
    1086           0 :                 if (uart->port.type != PORT_8250_CIR) {
    1087           0 :                         if (serial8250_isa_config != NULL)
    1088           0 :                                 serial8250_isa_config(0, &uart->port,
    1089             :                                                 &uart->capabilities);
    1090             : 
    1091           0 :                         serial8250_apply_quirks(uart);
    1092           0 :                         ret = uart_add_one_port(&serial8250_reg,
    1093             :                                                 &uart->port);
    1094           0 :                         if (ret)
    1095           0 :                                 goto err;
    1096             : 
    1097           0 :                         ret = uart->port.line;
    1098             :                 } else {
    1099           0 :                         dev_info(uart->port.dev,
    1100             :                                 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
    1101             :                                 uart->port.iobase,
    1102             :                                 (unsigned long long)uart->port.mapbase,
    1103             :                                 uart->port.irq);
    1104             : 
    1105           0 :                         ret = 0;
    1106             :                 }
    1107             : 
    1108             :                 /* Initialise interrupt backoff work if required */
    1109           0 :                 if (up->overrun_backoff_time_ms > 0) {
    1110           0 :                         uart->overrun_backoff_time_ms =
    1111             :                                 up->overrun_backoff_time_ms;
    1112           0 :                         INIT_DELAYED_WORK(&uart->overrun_backoff,
    1113             :                                         serial_8250_overrun_backoff_work);
    1114             :                 } else {
    1115           0 :                         uart->overrun_backoff_time_ms = 0;
    1116             :                 }
    1117             :         }
    1118             : 
    1119           0 :         mutex_unlock(&serial_mutex);
    1120             : 
    1121           0 :         return ret;
    1122             : 
    1123           0 : err:
    1124           0 :         uart->port.dev = NULL;
    1125           0 :         mutex_unlock(&serial_mutex);
    1126           0 :         return ret;
    1127             : }
    1128             : EXPORT_SYMBOL(serial8250_register_8250_port);
    1129             : 
    1130             : /**
    1131             :  *      serial8250_unregister_port - remove a 16x50 serial port at runtime
    1132             :  *      @line: serial line number
    1133             :  *
    1134             :  *      Remove one serial port.  This may not be called from interrupt
    1135             :  *      context.  We hand the port back to the our control.
    1136             :  */
    1137           0 : void serial8250_unregister_port(int line)
    1138             : {
    1139           0 :         struct uart_8250_port *uart = &serial8250_ports[line];
    1140             : 
    1141           0 :         mutex_lock(&serial_mutex);
    1142             : 
    1143           0 :         if (uart->em485) {
    1144           0 :                 unsigned long flags;
    1145             : 
    1146           0 :                 spin_lock_irqsave(&uart->port.lock, flags);
    1147           0 :                 serial8250_em485_destroy(uart);
    1148           0 :                 spin_unlock_irqrestore(&uart->port.lock, flags);
    1149             :         }
    1150             : 
    1151           0 :         uart_remove_one_port(&serial8250_reg, &uart->port);
    1152           0 :         if (serial8250_isa_devs) {
    1153           0 :                 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
    1154           0 :                 uart->port.type = PORT_UNKNOWN;
    1155           0 :                 uart->port.dev = &serial8250_isa_devs->dev;
    1156           0 :                 uart->capabilities = 0;
    1157           0 :                 serial8250_apply_quirks(uart);
    1158           0 :                 uart_add_one_port(&serial8250_reg, &uart->port);
    1159             :         } else {
    1160           0 :                 uart->port.dev = NULL;
    1161             :         }
    1162           0 :         mutex_unlock(&serial_mutex);
    1163           0 : }
    1164             : EXPORT_SYMBOL(serial8250_unregister_port);
    1165             : 
    1166           1 : static int __init serial8250_init(void)
    1167             : {
    1168           1 :         int ret;
    1169             : 
    1170           1 :         if (nr_uarts == 0)
    1171             :                 return -ENODEV;
    1172             : 
    1173           1 :         serial8250_isa_init_ports();
    1174             : 
    1175           2 :         pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
    1176             :                 nr_uarts, share_irqs ? "en" : "dis");
    1177             : 
    1178             : #ifdef CONFIG_SPARC
    1179             :         ret = sunserial_register_minors(&serial8250_reg, UART_NR);
    1180             : #else
    1181           1 :         serial8250_reg.nr = UART_NR;
    1182           1 :         ret = uart_register_driver(&serial8250_reg);
    1183             : #endif
    1184           1 :         if (ret)
    1185           0 :                 goto out;
    1186             : 
    1187           1 :         ret = serial8250_pnp_init();
    1188           1 :         if (ret)
    1189             :                 goto unreg_uart_drv;
    1190             : 
    1191           1 :         serial8250_isa_devs = platform_device_alloc("serial8250",
    1192             :                                                     PLAT8250_DEV_LEGACY);
    1193           1 :         if (!serial8250_isa_devs) {
    1194           0 :                 ret = -ENOMEM;
    1195           0 :                 goto unreg_pnp;
    1196             :         }
    1197             : 
    1198           1 :         ret = platform_device_add(serial8250_isa_devs);
    1199           1 :         if (ret)
    1200           0 :                 goto put_dev;
    1201             : 
    1202           1 :         serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
    1203             : 
    1204           1 :         ret = platform_driver_register(&serial8250_isa_driver);
    1205           1 :         if (ret == 0)
    1206           1 :                 goto out;
    1207             : 
    1208           0 :         platform_device_del(serial8250_isa_devs);
    1209           0 : put_dev:
    1210           0 :         platform_device_put(serial8250_isa_devs);
    1211           0 : unreg_pnp:
    1212           0 :         serial8250_pnp_exit();
    1213           0 : unreg_uart_drv:
    1214             : #ifdef CONFIG_SPARC
    1215             :         sunserial_unregister_minors(&serial8250_reg, UART_NR);
    1216             : #else
    1217           0 :         uart_unregister_driver(&serial8250_reg);
    1218             : #endif
    1219             : out:
    1220             :         return ret;
    1221             : }
    1222             : 
    1223           0 : static void __exit serial8250_exit(void)
    1224             : {
    1225           0 :         struct platform_device *isa_dev = serial8250_isa_devs;
    1226             : 
    1227             :         /*
    1228             :          * This tells serial8250_unregister_port() not to re-register
    1229             :          * the ports (thereby making serial8250_isa_driver permanently
    1230             :          * in use.)
    1231             :          */
    1232           0 :         serial8250_isa_devs = NULL;
    1233             : 
    1234           0 :         platform_driver_unregister(&serial8250_isa_driver);
    1235           0 :         platform_device_unregister(isa_dev);
    1236             : 
    1237           0 :         serial8250_pnp_exit();
    1238             : 
    1239             : #ifdef CONFIG_SPARC
    1240             :         sunserial_unregister_minors(&serial8250_reg, UART_NR);
    1241             : #else
    1242           0 :         uart_unregister_driver(&serial8250_reg);
    1243             : #endif
    1244           0 : }
    1245             : 
    1246             : module_init(serial8250_init);
    1247             : module_exit(serial8250_exit);
    1248             : 
    1249             : MODULE_LICENSE("GPL");
    1250             : MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
    1251             : 
    1252             : module_param_hw(share_irqs, uint, other, 0644);
    1253             : MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
    1254             : 
    1255             : module_param(nr_uarts, uint, 0644);
    1256             : MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
    1257             : 
    1258             : module_param(skip_txen_test, uint, 0644);
    1259             : MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
    1260             : 
    1261             : #ifdef CONFIG_SERIAL_8250_RSA
    1262             : module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
    1263             : MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
    1264             : #endif
    1265             : MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
    1266             : 
    1267             : #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
    1268             : #ifndef MODULE
    1269             : /* This module was renamed to 8250_core in 3.7.  Keep the old "8250" name
    1270             :  * working as well for the module options so we don't break people.  We
    1271             :  * need to keep the names identical and the convenient macros will happily
    1272             :  * refuse to let us do that by failing the build with redefinition errors
    1273             :  * of global variables.  So we stick them inside a dummy function to avoid
    1274             :  * those conflicts.  The options still get parsed, and the redefined
    1275             :  * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
    1276             :  *
    1277             :  * This is hacky.  I'm sorry.
    1278             :  */
    1279             : static void __used s8250_options(void)
    1280             : {
    1281             : #undef MODULE_PARAM_PREFIX
    1282             : #define MODULE_PARAM_PREFIX "8250_core."
    1283             : 
    1284             :         module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
    1285             :         module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
    1286             :         module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
    1287             : #ifdef CONFIG_SERIAL_8250_RSA
    1288             :         __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
    1289             :                 &param_array_ops, .arr = &__param_arr_probe_rsa,
    1290             :                 0444, -1, 0);
    1291             : #endif
    1292             : }
    1293             : #else
    1294             : MODULE_ALIAS("8250_core");
    1295             : #endif
    1296             : #endif

Generated by: LCOV version 1.14