x86_64: Geode HW Random Number Generator depends on X86_32
[safe/jmp/linux-2.6] / drivers / char / stallion.c
index 4ccf4a5..4a80b2f 100644 (file)
@@ -63,7 +63,7 @@
 #define        BRD_EASYIOPCI   28
 
 struct stlconf {
-       int             brdtype;
+       unsigned int    brdtype;
        int             ioaddr1;
        int             ioaddr2;
        unsigned long   memaddr;
@@ -121,21 +121,13 @@ static struct ktermios            stl_deftermios = {
 };
 
 /*
- *     Define global stats structures. Not used often, and can be
- *     re-used for each stats call.
- */
-static comstats_t      stl_comstats;
-static combrd_t                stl_brdstats;
-static struct stlbrd           stl_dummybrd;
-static struct stlport  stl_dummyport;
-
-/*
  *     Define global place to put buffer overflow characters.
  */
 static char            stl_unwanted[SC26198_RXFIFOSIZE];
 
 /*****************************************************************************/
 
+static DEFINE_MUTEX(stl_brdslock);
 static struct stlbrd           *stl_brds[STL_MAXBRDS];
 
 /*
@@ -143,6 +135,8 @@ static struct stlbrd                *stl_brds[STL_MAXBRDS];
  *     Not really much here!
  */
 #define        BRD_FOUND       0x1
+#define STL_PROBED     0x2
+
 
 /*
  *     Define the port structure istate flags. These set of flags are
@@ -197,7 +191,7 @@ static char *stl_brdnames[] = {
  *     load line. These allow for easy board definitions, and easy
  *     modification of the io, memory and irq resoucres.
  */
-static int     stl_nargs = 0;
+static unsigned int stl_nargs;
 static char    *board0[4];
 static char    *board1[4];
 static char    *board2[4];
@@ -629,20 +623,20 @@ static struct class *stallion_class;
 static int __init stl_parsebrd(struct stlconf *confp, char **argp)
 {
        char    *sp;
-       int     i;
+       unsigned int i;
 
        pr_debug("stl_parsebrd(confp=%p,argp=%p)\n", confp, argp);
 
        if ((argp[0] == NULL) || (*argp[0] == 0))
                return 0;
 
-       for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
+       for (sp = argp[0], i = 0; (*sp != 0) && (i < 25); sp++, i++)
                *sp = tolower(*sp);
 
-       for (i = 0; i < ARRAY_SIZE(stl_brdstr); i++) {
+       for (i = 0; i < ARRAY_SIZE(stl_brdstr); i++)
                if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
                        break;
-       }
+
        if (i == ARRAY_SIZE(stl_brdstr)) {
                printk("STALLION: unknown board name, %s?\n", argp[0]);
                return 0;
@@ -691,8 +685,8 @@ static int stl_open(struct tty_struct *tty, struct file *filp)
 {
        struct stlport  *portp;
        struct stlbrd   *brdp;
-       unsigned int    minordev;
-       int             brdnr, panelnr, portnr, rc;
+       unsigned int    minordev, brdnr, panelnr;
+       int             portnr, rc;
 
        pr_debug("stl_open(tty=%p,filp=%p): device=%s\n", tty, filp, tty->name);
 
@@ -704,7 +698,7 @@ static int stl_open(struct tty_struct *tty, struct file *filp)
        if (brdp == NULL)
                return -ENODEV;
        minordev = MINOR2PORT(minordev);
-       for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
+       for (portnr = -1, panelnr = 0; panelnr < STL_MAXPANELS; panelnr++) {
                if (brdp->panels[panelnr] == NULL)
                        break;
                if (minordev < brdp->panels[panelnr]->nrports) {
@@ -763,10 +757,10 @@ static int stl_open(struct tty_struct *tty, struct file *filp)
  *     previous opens still in effect. If we are a normal serial device
  *     then also we might have to wait for carrier.
  */
-       if (!(filp->f_flags & O_NONBLOCK)) {
+       if (!(filp->f_flags & O_NONBLOCK))
                if ((rc = stl_waitcarrier(portp, filp)) != 0)
                        return rc;
-       }
+
        portp->flags |= ASYNC_NORMAL_ACTIVE;
 
        return 0;
@@ -810,9 +804,8 @@ static int stl_waitcarrier(struct stlport *portp, struct file *filp)
                        break;
                }
                if (((portp->flags & ASYNC_CLOSING) == 0) &&
-                   (doclocal || (portp->sigs & TIOCM_CD))) {
+                   (doclocal || (portp->sigs & TIOCM_CD)))
                        break;
-               }
                if (signal_pending(current)) {
                        rc = -ERESTARTSYS;
                        break;
@@ -1088,7 +1081,7 @@ static int stl_writeroom(struct tty_struct *tty)
 
        head = portp->tx.head;
        tail = portp->tx.tail;
-       return ((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
+       return (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1);
 }
 
 /*****************************************************************************/
@@ -1258,10 +1251,9 @@ static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd
                return -ENODEV;
 
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
-           (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
+           (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS))
                if (tty->flags & (1 << TTY_IO_ERROR))
                        return -EIO;
-       }
 
        rc = 0;
 
@@ -1535,7 +1527,7 @@ static int stl_portinfo(struct stlport *portp, int portnr, char *pos)
        *sp = ' ';
        sp += cnt;
 
-       for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
+       for (cnt = sp - pos; cnt < (MAXLINE - 1); cnt++)
                *sp++ = ' ';
        if (cnt >= MAXLINE)
                pos[(MAXLINE - 2)] = '+';
@@ -1555,8 +1547,8 @@ static int stl_readproc(char *page, char **start, off_t off, int count, int *eof
        struct stlbrd   *brdp;
        struct stlpanel *panelp;
        struct stlport  *portp;
-       int             brdnr, panelnr, portnr, totalport;
-       int             curoff, maxoff;
+       unsigned int    brdnr, panelnr, portnr;
+       int             totalport, curoff, maxoff;
        char            *pos;
 
        pr_debug("stl_readproc(page=%p,start=%p,off=%lx,count=%d,eof=%p,"
@@ -1579,7 +1571,7 @@ static int stl_readproc(char *page, char **start, off_t off, int count, int *eof
  *     We scan through for each board, panel and port. The offset is
  *     calculated on the fly, and irrelevant ports are skipped.
  */
-       for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
+       for (brdnr = 0; brdnr < stl_nrbrds; brdnr++) {
                brdp = stl_brds[brdnr];
                if (brdp == NULL)
                        continue;
@@ -1593,7 +1585,7 @@ static int stl_readproc(char *page, char **start, off_t off, int count, int *eof
                }
 
                totalport = brdnr * STL_MAXPORTS;
-               for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
+               for (panelnr = 0; panelnr < brdp->nrpanels; panelnr++) {
                        panelp = brdp->panels[panelnr];
                        if (panelp == NULL)
                                continue;
@@ -1605,7 +1597,7 @@ static int stl_readproc(char *page, char **start, off_t off, int count, int *eof
                                continue;
                        }
 
-                       for (portnr = 0; (portnr < panelp->nrports); portnr++,
+                       for (portnr = 0; portnr < panelp->nrports; portnr++,
                            totalport++) {
                                portp = panelp->ports[portnr];
                                if (portp == NULL)
@@ -1623,7 +1615,7 @@ static int stl_readproc(char *page, char **start, off_t off, int count, int *eof
 
 stl_readdone:
        *start = page;
-       return (pos - page);
+       return pos - page;
 }
 
 /*****************************************************************************/
@@ -1674,15 +1666,14 @@ static int stl_eiointr(struct stlbrd *brdp)
 static int stl_echatintr(struct stlbrd *brdp)
 {
        struct stlpanel *panelp;
-       unsigned int    ioaddr;
-       int             bnknr;
+       unsigned int    ioaddr, bnknr;
        int             handled = 0;
 
        outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
 
        while (inb(brdp->iostatus) & ECH_INTRPEND) {
                handled = 1;
-               for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
+               for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
                        ioaddr = brdp->bnkstataddr[bnknr];
                        if (inb(ioaddr) & ECH_PNLINTRPEND) {
                                panelp = brdp->bnk2panel[bnknr];
@@ -1705,13 +1696,12 @@ static int stl_echatintr(struct stlbrd *brdp)
 static int stl_echmcaintr(struct stlbrd *brdp)
 {
        struct stlpanel *panelp;
-       unsigned int    ioaddr;
-       int             bnknr;
+       unsigned int    ioaddr, bnknr;
        int             handled = 0;
 
        while (inb(brdp->iostatus) & ECH_INTRPEND) {
                handled = 1;
-               for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
+               for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
                        ioaddr = brdp->bnkstataddr[bnknr];
                        if (inb(ioaddr) & ECH_PNLINTRPEND) {
                                panelp = brdp->bnk2panel[bnknr];
@@ -1731,13 +1721,12 @@ static int stl_echmcaintr(struct stlbrd *brdp)
 static int stl_echpciintr(struct stlbrd *brdp)
 {
        struct stlpanel *panelp;
-       unsigned int    ioaddr;
-       int             bnknr, recheck;
+       unsigned int    ioaddr, bnknr, recheck;
        int             handled = 0;
 
        while (1) {
                recheck = 0;
-               for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
+               for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
                        outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
                        ioaddr = brdp->bnkstataddr[bnknr];
                        if (inb(ioaddr) & ECH_PNLINTRPEND) {
@@ -1762,13 +1751,12 @@ static int stl_echpciintr(struct stlbrd *brdp)
 static int stl_echpci64intr(struct stlbrd *brdp)
 {
        struct stlpanel *panelp;
-       unsigned int    ioaddr;
-       int             bnknr;
+       unsigned int    ioaddr, bnknr;
        int             handled = 0;
 
        while (inb(brdp->ioctrl) & 0x1) {
                handled = 1;
-               for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
+               for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
                        ioaddr = brdp->bnkstataddr[bnknr];
                        if (inb(ioaddr) & ECH_PNLINTRPEND) {
                                panelp = brdp->bnk2panel[bnknr];
@@ -1800,22 +1788,19 @@ static void stl_offintr(struct work_struct *work)
        if (tty == NULL)
                return;
 
-       lock_kernel();
-       if (test_bit(ASYI_TXLOW, &portp->istate)) {
+       if (test_bit(ASYI_TXLOW, &portp->istate))
                tty_wakeup(tty);
-       }
+
        if (test_bit(ASYI_DCDCHANGE, &portp->istate)) {
                clear_bit(ASYI_DCDCHANGE, &portp->istate);
                oldsigs = portp->sigs;
                portp->sigs = stl_getsignals(portp);
                if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
                        wake_up_interruptible(&portp->open_wait);
-               if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0)) {
+               if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0))
                        if (portp->flags & ASYNC_CHECK_CD)
                                tty_hangup(tty);        /* FIXME: module removal race here - AKPM */
-               }
        }
-       unlock_kernel();
 }
 
 /*****************************************************************************/
@@ -1826,8 +1811,9 @@ static void stl_offintr(struct work_struct *work)
 
 static int __devinit stl_initports(struct stlbrd *brdp, struct stlpanel *panelp)
 {
-       struct stlport  *portp;
-       int             chipmask, i;
+       struct stlport *portp;
+       unsigned int i;
+       int chipmask;
 
        pr_debug("stl_initports(brdp=%p,panelp=%p)\n", brdp, panelp);
 
@@ -1837,7 +1823,7 @@ static int __devinit stl_initports(struct stlbrd *brdp, struct stlpanel *panelp)
  *     All UART's are initialized (if found!). Now go through and setup
  *     each ports data structures.
  */
-       for (i = 0; (i < panelp->nrports); i++) {
+       for (i = 0; i < panelp->nrports; i++) {
                portp = kzalloc(sizeof(struct stlport), GFP_KERNEL);
                if (!portp) {
                        printk("STALLION: failed to allocate memory "
@@ -1864,7 +1850,7 @@ static int __devinit stl_initports(struct stlbrd *brdp, struct stlpanel *panelp)
                stl_portinit(brdp, panelp, portp);
        }
 
-       return(0);
+       return 0;
 }
 
 static void stl_cleanup_panels(struct stlbrd *brdp)
@@ -2052,8 +2038,8 @@ err:
 static int __devinit stl_initech(struct stlbrd *brdp)
 {
        struct stlpanel *panelp;
-       unsigned int    status, nxtid, ioaddr, conflict;
-       int             panelnr, banknr, i, retval;
+       unsigned int    status, nxtid, ioaddr, conflict, panelnr, banknr, i;
+       int             retval;
        char            *name;
 
        pr_debug("stl_initech(brdp=%p)\n", brdp);
@@ -2089,7 +2075,7 @@ static int __devinit stl_initech(struct stlbrd *brdp)
                outb((status | ECH_BRDRESET), brdp->ioaddr1);
                brdp->ioctrlval = ECH_INTENABLE |
                        ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
-               for (i = 0; (i < 10); i++)
+               for (i = 0; i < 10; i++)
                        outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
                brdp->iosize1 = 2;
                brdp->iosize2 = 32;
@@ -2177,18 +2163,19 @@ static int __devinit stl_initech(struct stlbrd *brdp)
        panelnr = 0;
        nxtid = 0;
 
-       for (i = 0; (i < STL_MAXPANELS); i++) {
+       for (i = 0; i < STL_MAXPANELS; i++) {
                if (brdp->brdtype == BRD_ECHPCI) {
                        outb(nxtid, brdp->ioctrl);
                        ioaddr = brdp->ioaddr2;
                }
                status = inb(ioaddr + ECH_PNLSTATUS);
                if ((status & ECH_PNLIDMASK) != nxtid)
-                       goto err_fr;
+                       break;
                panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
                if (!panelp) {
                        printk("STALLION: failed to allocate memory "
                                "(size=%Zd)\n", sizeof(struct stlpanel));
+                       retval = -ENOMEM;
                        goto err_fr;
                }
                panelp->magic = STL_PANELMAGIC;
@@ -2210,9 +2197,8 @@ static int __devinit stl_initech(struct stlbrd *brdp)
                                brdp->bnkpageaddr[banknr] = nxtid;
                                brdp->bnkstataddr[banknr++] = ioaddr + 4 +
                                        ECH_PNLSTATUS;
-                       } else {
+                       } else
                                panelp->nrports = 8;
-                       }
                } else {
                        panelp->uartp = &stl_cd1400uart;
                        panelp->isr = stl_cd1400echintr;
@@ -2236,8 +2222,10 @@ static int __devinit stl_initech(struct stlbrd *brdp)
                brdp->nrports += panelp->nrports;
                brdp->panels[panelnr++] = panelp;
                if ((brdp->brdtype != BRD_ECHPCI) &&
-                   (ioaddr >= (brdp->ioaddr2 + brdp->iosize2)))
+                   (ioaddr >= (brdp->ioaddr2 + brdp->iosize2))) {
+                       retval = -EINVAL;
                        goto err_fr;
+               }
        }
 
        brdp->nrpanels = panelnr;
@@ -2301,7 +2289,6 @@ static int __devinit stl_brdinit(struct stlbrd *brdp)
                goto err;
        }
 
-       stl_brds[brdp->brdnr] = brdp;
        if ((brdp->state & BRD_FOUND) == 0) {
                printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
                        stl_brdnames[brdp->brdtype], brdp->brdnr,
@@ -2309,7 +2296,7 @@ static int __devinit stl_brdinit(struct stlbrd *brdp)
                goto err_free;
        }
 
-       for (i = 0; (i < STL_MAXPANELS); i++)
+       for (i = 0; i < STL_MAXPANELS; i++)
                if (brdp->panels[i] != NULL)
                        stl_initports(brdp, brdp->panels[i]);
 
@@ -2327,8 +2314,6 @@ err_free:
        release_region(brdp->ioaddr1, brdp->iosize1);
        if (brdp->iosize2 > 0)
                release_region(brdp->ioaddr2, brdp->iosize2);
-
-       stl_brds[brdp->brdnr] = NULL;
 err:
        return retval;
 }
@@ -2341,16 +2326,16 @@ err:
 
 static int __devinit stl_getbrdnr(void)
 {
-       int     i;
+       unsigned int i;
 
-       for (i = 0; (i < STL_MAXBRDS); i++) {
+       for (i = 0; i < STL_MAXBRDS; i++)
                if (stl_brds[i] == NULL) {
                        if (i >= stl_nrbrds)
                                stl_nrbrds = i + 1;
-                       return(i);
+                       return i;
                }
-       }
-       return(-1);
+
+       return -1;
 }
 
 /*****************************************************************************/
@@ -2364,15 +2349,12 @@ static int __devinit stl_pciprobe(struct pci_dev *pdev,
                const struct pci_device_id *ent)
 {
        struct stlbrd *brdp;
-       unsigned int brdtype = ent->driver_data;
-       int retval = -ENODEV;
+       unsigned int i, brdtype = ent->driver_data;
+       int brdnr, retval = -ENODEV;
 
        if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE)
                goto err;
 
-       dev_info(&pdev->dev, "please, report this to LKML: %x/%x/%x\n",
-                       pdev->vendor, pdev->device, pdev->class);
-
        retval = pci_enable_device(pdev);
        if (retval)
                goto err;
@@ -2381,13 +2363,21 @@ static int __devinit stl_pciprobe(struct pci_dev *pdev,
                retval = -ENOMEM;
                goto err;
        }
-       brdp->brdnr = stl_getbrdnr();
-       if (brdp->brdnr < 0) {
+       mutex_lock(&stl_brdslock);
+       brdnr = stl_getbrdnr();
+       if (brdnr < 0) {
                dev_err(&pdev->dev, "too many boards found, "
                        "maximum supported %d\n", STL_MAXBRDS);
+               mutex_unlock(&stl_brdslock);
+               retval = -ENODEV;
                goto err_fr;
        }
+       brdp->brdnr = (unsigned int)brdnr;
+       stl_brds[brdp->brdnr] = brdp;
+       mutex_unlock(&stl_brdslock);
+
        brdp->brdtype = brdtype;
+       brdp->state |= STL_PROBED;
 
 /*
  *     We have all resources from the board, so let's setup the actual
@@ -2414,11 +2404,17 @@ static int __devinit stl_pciprobe(struct pci_dev *pdev,
        brdp->irq = pdev->irq;
        retval = stl_brdinit(brdp);
        if (retval)
-               goto err_fr;
+               goto err_null;
 
        pci_set_drvdata(pdev, brdp);
 
+       for (i = 0; i < brdp->nrports; i++)
+               tty_register_device(stl_serial,
+                               brdp->brdnr * STL_MAXPORTS + i, &pdev->dev);
+
        return 0;
+err_null:
+       stl_brds[brdp->brdnr] = NULL;
 err_fr:
        kfree(brdp);
 err:
@@ -2428,6 +2424,7 @@ err:
 static void __devexit stl_pciremove(struct pci_dev *pdev)
 {
        struct stlbrd *brdp = pci_get_drvdata(pdev);
+       unsigned int i;
 
        free_irq(brdp->irq, brdp);
 
@@ -2437,6 +2434,10 @@ static void __devexit stl_pciremove(struct pci_dev *pdev)
        if (brdp->iosize2 > 0)
                release_region(brdp->ioaddr2, brdp->iosize2);
 
+       for (i = 0; i < brdp->nrports; i++)
+               tty_unregister_device(stl_serial,
+                               brdp->brdnr * STL_MAXPORTS + i);
+
        stl_brds[brdp->brdnr] = NULL;
        kfree(brdp);
 }
@@ -2456,17 +2457,18 @@ static struct pci_driver stl_pcidriver = {
 
 static int stl_getbrdstats(combrd_t __user *bp)
 {
+       combrd_t        stl_brdstats;
        struct stlbrd   *brdp;
        struct stlpanel *panelp;
-       int             i;
+       unsigned int i;
 
        if (copy_from_user(&stl_brdstats, bp, sizeof(combrd_t)))
                return -EFAULT;
        if (stl_brdstats.brd >= STL_MAXBRDS)
-               return(-ENODEV);
+               return -ENODEV;
        brdp = stl_brds[stl_brdstats.brd];
        if (brdp == NULL)
-               return(-ENODEV);
+               return -ENODEV;
 
        memset(&stl_brdstats, 0, sizeof(combrd_t));
        stl_brdstats.brd = brdp->brdnr;
@@ -2478,7 +2480,7 @@ static int stl_getbrdstats(combrd_t __user *bp)
        stl_brdstats.irq = brdp->irq;
        stl_brdstats.nrpanels = brdp->nrpanels;
        stl_brdstats.nrports = brdp->nrports;
-       for (i = 0; (i < brdp->nrpanels); i++) {
+       for (i = 0; i < brdp->nrpanels; i++) {
                panelp = brdp->panels[i];
                stl_brdstats.panels[i].panel = i;
                stl_brdstats.panels[i].hwid = panelp->hwid;
@@ -2499,19 +2501,19 @@ static struct stlport *stl_getport(int brdnr, int panelnr, int portnr)
        struct stlbrd   *brdp;
        struct stlpanel *panelp;
 
-       if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
-               return(NULL);
+       if (brdnr < 0 || brdnr >= STL_MAXBRDS)
+               return NULL;
        brdp = stl_brds[brdnr];
        if (brdp == NULL)
-               return(NULL);
-       if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
-               return(NULL);
+               return NULL;
+       if (panelnr < 0 || (unsigned int)panelnr >= brdp->nrpanels)
+               return NULL;
        panelp = brdp->panels[panelnr];
        if (panelp == NULL)
-               return(NULL);
-       if ((portnr < 0) || (portnr >= panelp->nrports))
-               return(NULL);
-       return(panelp->ports[portnr]);
+               return NULL;
+       if (portnr < 0 || (unsigned int)portnr >= panelp->nrports)
+               return NULL;
+       return panelp->ports[portnr];
 }
 
 /*****************************************************************************/
@@ -2524,6 +2526,7 @@ static struct stlport *stl_getport(int brdnr, int panelnr, int portnr)
 
 static int stl_getportstats(struct stlport *portp, comstats_t __user *cp)
 {
+       comstats_t      stl_comstats;
        unsigned char   *head, *tail;
        unsigned long   flags;
 
@@ -2533,7 +2536,7 @@ static int stl_getportstats(struct stlport *portp, comstats_t __user *cp)
                portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
                        stl_comstats.port);
                if (portp == NULL)
-                       return(-ENODEV);
+                       return -ENODEV;
        }
 
        portp->stats.state = portp->istate;
@@ -2548,7 +2551,7 @@ static int stl_getportstats(struct stlport *portp, comstats_t __user *cp)
        portp->stats.rxbuffered = 0;
 
        spin_lock_irqsave(&stallion_lock, flags);
-       if (portp->tty != NULL) {
+       if (portp->tty != NULL)
                if (portp->tty->driver_data == portp) {
                        portp->stats.ttystate = portp->tty->flags;
                        /* No longer available as a statistic */
@@ -2560,13 +2563,12 @@ static int stl_getportstats(struct stlport *portp, comstats_t __user *cp)
                                portp->stats.lflags = portp->tty->termios->c_lflag;
                        }
                }
-       }
        spin_unlock_irqrestore(&stallion_lock, flags);
 
        head = portp->tx.head;
        tail = portp->tx.tail;
-       portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
-               (STL_TXBUFSIZE - (tail - head)));
+       portp->stats.txbuffered = (head >= tail) ? (head - tail) :
+               (STL_TXBUFSIZE - (tail - head));
 
        portp->stats.signals = (unsigned long) stl_getsignals(portp);
 
@@ -2582,13 +2584,15 @@ static int stl_getportstats(struct stlport *portp, comstats_t __user *cp)
 
 static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp)
 {
+       comstats_t      stl_comstats;
+
        if (!portp) {
                if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
                        return -EFAULT;
                portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
                        stl_comstats.port);
                if (portp == NULL)
-                       return(-ENODEV);
+                       return -ENODEV;
        }
 
        memset(&portp->stats, 0, sizeof(comstats_t));
@@ -2607,6 +2611,7 @@ static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp)
 
 static int stl_getportstruct(struct stlport __user *arg)
 {
+       struct stlport  stl_dummyport;
        struct stlport  *portp;
 
        if (copy_from_user(&stl_dummyport, arg, sizeof(struct stlport)))
@@ -2626,15 +2631,16 @@ static int stl_getportstruct(struct stlport __user *arg)
 
 static int stl_getbrdstruct(struct stlbrd __user *arg)
 {
+       struct stlbrd   stl_dummybrd;
        struct stlbrd   *brdp;
 
        if (copy_from_user(&stl_dummybrd, arg, sizeof(struct stlbrd)))
                return -EFAULT;
-       if ((stl_dummybrd.brdnr < 0) || (stl_dummybrd.brdnr >= STL_MAXBRDS))
+       if (stl_dummybrd.brdnr >= STL_MAXBRDS)
                return -ENODEV;
        brdp = stl_brds[stl_dummybrd.brdnr];
        if (!brdp)
-               return(-ENODEV);
+               return -ENODEV;
        return copy_to_user(arg, brdp, sizeof(struct stlbrd)) ? -EFAULT : 0;
 }
 
@@ -2655,7 +2661,7 @@ static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, uns
 
        brdnr = iminor(ip);
        if (brdnr >= STL_MAXBRDS)
-               return(-ENODEV);
+               return -ENODEV;
        rc = 0;
 
        switch (cmd) {
@@ -2679,7 +2685,7 @@ static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, uns
                break;
        }
 
-       return(rc);
+       return rc;
 }
 
 static const struct tty_operations stl_ops = {
@@ -2724,13 +2730,13 @@ static int stl_cd1400getreg(struct stlport *portp, int regnr)
 
 static void stl_cd1400setreg(struct stlport *portp, int regnr, int value)
 {
-       outb((regnr + portp->uartaddr), portp->ioaddr);
+       outb(regnr + portp->uartaddr, portp->ioaddr);
        outb(value, portp->ioaddr + EREG_DATA);
 }
 
 static int stl_cd1400updatereg(struct stlport *portp, int regnr, int value)
 {
-       outb((regnr + portp->uartaddr), portp->ioaddr);
+       outb(regnr + portp->uartaddr, portp->ioaddr);
        if (inb(portp->ioaddr + EREG_DATA) != value) {
                outb(value, portp->ioaddr + EREG_DATA);
                return 1;
@@ -2763,13 +2769,12 @@ static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
  */
        chipmask = 0;
        nrchips = panelp->nrports / CD1400_PORTS;
-       for (i = 0; (i < nrchips); i++) {
+       for (i = 0; i < nrchips; i++) {
                if (brdp->brdtype == BRD_ECHPCI) {
                        outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
                        ioaddr = panelp->iobase;
-               } else {
+               } else
                        ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
-               }
                uartaddr = (i & 0x01) ? 0x080 : 0;
                outb((GFRCR + uartaddr), ioaddr);
                outb(0, (ioaddr + EREG_DATA));
@@ -2777,10 +2782,10 @@ static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
                outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
                outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
                outb((GFRCR + uartaddr), ioaddr);
-               for (j = 0; (j < CCR_MAXWAIT); j++) {
+               for (j = 0; j < CCR_MAXWAIT; j++)
                        if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
                                break;
-               }
+
                if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
                        printk("STALLION: cd1400 not responding, "
                                "brd=%d panel=%d chip=%d\n",
@@ -2838,11 +2843,9 @@ static void stl_cd1400ccrwait(struct stlport *portp)
 {
        int     i;
 
-       for (i = 0; (i < CCR_MAXWAIT); i++) {
-               if (stl_cd1400getreg(portp, CCR) == 0) {
+       for (i = 0; i < CCR_MAXWAIT; i++)
+               if (stl_cd1400getreg(portp, CCR) == 0)
                        return;
-               }
-       }
 
        printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
                portp->portnr, portp->panelnr, portp->brdnr);
@@ -2978,8 +2981,8 @@ static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp)
                baudrate = STL_CD1400MAXBAUD;
 
        if (baudrate > 0) {
-               for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
-                       clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) / baudrate);
+               for (clk = 0; clk < CD1400_NUMCLKS; clk++) {
+                       clkdiv = (portp->clk / stl_cd1400clkdivs[clk]) / baudrate;
                        if (clkdiv < 0x100)
                                break;
                }
@@ -2994,9 +2997,8 @@ static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp)
                mcor2 |= MCOR2_DCD;
                sreron |= SRER_MODEM;
                portp->flags |= ASYNC_CHECK_CD;
-       } else {
+       } else
                portp->flags &= ~ASYNC_CHECK_CD;
-       }
 
 /*
  *     Setup cd1400 enhanced modes if we can. In particular we want to
@@ -3640,18 +3642,16 @@ static void stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr)
                                                do_SAK(tty);
                                                BRDENABLE(portp->brdnr, portp->pagenr);
                                        }
-                               } else if (status & ST_PARITY) {
+                               } else if (status & ST_PARITY)
                                        status = TTY_PARITY;
-                               } else if (status & ST_FRAMING) {
+                               else if (status & ST_FRAMING)
                                        status = TTY_FRAME;
-                               } else if(status & ST_OVERRUN) {
+                               else if(status & ST_OVERRUN)
                                        status = TTY_OVERRUN;
-                               } else {
+                               else
                                        status = 0;
-                               }
-                       } else {
+                       } else
                                status = 0;
-                       }
                        tty_insert_flip_char(tty, ch, status);
                        tty_schedule_flip(tty);
                }
@@ -3778,7 +3778,7 @@ static int stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
        if (brdp->brdtype == BRD_ECHPCI)
                outb(panelp->pagenr, brdp->ioctrl);
 
-       for (i = 0; (i < nrchips); i++) {
+       for (i = 0; i < nrchips; i++) {
                ioaddr = panelp->iobase + (i * 4); 
                outb(SCCR, (ioaddr + XP_ADDR));
                outb(CR_RESETALL, (ioaddr + XP_DATA));
@@ -3898,9 +3898,8 @@ static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp)
                        mr1 |= (MR1_PARENB | MR1_PARODD);
                else
                        mr1 |= (MR1_PARENB | MR1_PAREVEN);
-       } else {
+       } else
                mr1 |= MR1_PARNONE;
-       }
 
        mr1 |= MR1_ERRBLOCK;
 
@@ -3940,12 +3939,10 @@ static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp)
        if (baudrate > STL_SC26198MAXBAUD)
                baudrate = STL_SC26198MAXBAUD;
 
-       if (baudrate > 0) {
-               for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
+       if (baudrate > 0)
+               for (clk = 0; clk < SC26198_NRBAUDS; clk++)
                        if (baudrate <= sc26198_baudtable[clk])
                                break;
-               }
-       }
 
 /*
  *     Check what form of modem signaling is required and set it up.
@@ -3967,9 +3964,9 @@ static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp)
        if (tiosp->c_iflag & IXON) {
                mr0 |= MR0_SWFTX | MR0_SWFT;
                imron |= IR_XONXOFF;
-       } else {
+       } else
                imroff |= IR_XONXOFF;
-       }
+
        if (tiosp->c_iflag & IXOFF)
                mr0 |= MR0_SWFRX;
 
@@ -4180,9 +4177,9 @@ static void stl_sc26198sendbreak(struct stlport *portp, int len)
        if (len == 1) {
                stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
                portp->stats.txbreaks++;
-       } else {
+       } else
                stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
-       }
+
        BRDDISABLE(portp->brdnr);
        spin_unlock_irqrestore(&brd_lock, flags);
 }
@@ -4366,7 +4363,7 @@ static void stl_sc26198wait(struct stlport *portp)
        if (portp == NULL)
                return;
 
-       for (i = 0; (i < 20); i++)
+       for (i = 0; i < 20; i++)
                stl_sc26198getglobreg(portp, TSTR);
 }
 
@@ -4575,18 +4572,16 @@ static void stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char
                                        do_SAK(tty);
                                        BRDENABLE(portp->brdnr, portp->pagenr);
                                }
-                       } else if (status & SR_RXPARITY) {
+                       } else if (status & SR_RXPARITY)
                                status = TTY_PARITY;
-                       } else if (status & SR_RXFRAMING) {
+                       else if (status & SR_RXFRAMING)
                                status = TTY_FRAME;
-                       } else if(status & SR_RXOVERRUN) {
+                       else if(status & SR_RXOVERRUN)
                                status = TTY_OVERRUN;
-                       } else {
+                       else
                                status = 0;
-                       }
-               } else {
+               } else
                        status = 0;
-               }
 
                tty_insert_flip_char(tty, ch, status);
                tty_schedule_flip(tty);
@@ -4677,6 +4672,28 @@ static void stl_sc26198otherisr(struct stlport *portp, unsigned int iack)
        }
 }
 
+static void stl_free_isabrds(void)
+{
+       struct stlbrd *brdp;
+       unsigned int i;
+
+       for (i = 0; i < stl_nrbrds; i++) {
+               if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
+                       continue;
+
+               free_irq(brdp->irq, brdp);
+
+               stl_cleanup_panels(brdp);
+
+               release_region(brdp->ioaddr1, brdp->iosize1);
+               if (brdp->iosize2 > 0)
+                       release_region(brdp->ioaddr2, brdp->iosize2);
+
+               kfree(brdp);
+               stl_brds[i] = NULL;
+       }
+}
+
 /*
  *     Loadable module initialization stuff.
  */
@@ -4684,13 +4701,37 @@ static int __init stallion_module_init(void)
 {
        struct stlbrd   *brdp;
        struct stlconf  conf;
-       unsigned int i, retval;
+       unsigned int i, j;
+       int retval;
 
        printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
 
        spin_lock_init(&stallion_lock);
        spin_lock_init(&brd_lock);
 
+       stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
+       if (!stl_serial) {
+               retval = -ENOMEM;
+               goto err;
+       }
+
+       stl_serial->owner = THIS_MODULE;
+       stl_serial->driver_name = stl_drvname;
+       stl_serial->name = "ttyE";
+       stl_serial->major = STL_SERIALMAJOR;
+       stl_serial->minor_start = 0;
+       stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
+       stl_serial->subtype = SERIAL_TYPE_NORMAL;
+       stl_serial->init_termios = stl_deftermios;
+       stl_serial->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
+       tty_set_operations(stl_serial, &stl_ops);
+
+       retval = tty_register_driver(stl_serial);
+       if (retval) {
+               printk("STALLION: failed to register serial driver\n");
+               goto err_frtty;
+       }
+
 /*
  *     Find any dynamically supported boards. That is via module load
  *     line options.
@@ -4707,19 +4748,24 @@ static int __init stallion_module_init(void)
                brdp->ioaddr2 = conf.ioaddr2;
                brdp->irq = conf.irq;
                brdp->irqtype = conf.irqtype;
-               if (stl_brdinit(brdp))
+               stl_brds[brdp->brdnr] = brdp;
+               if (stl_brdinit(brdp)) {
+                       stl_brds[brdp->brdnr] = NULL;
                        kfree(brdp);
-               else
+               } else {
+                       for (j = 0; j < brdp->nrports; j++)
+                               tty_register_device(stl_serial,
+                                       brdp->brdnr * STL_MAXPORTS + j, NULL);
                        stl_nrbrds = i + 1;
+               }
        }
 
+       /* this has to be _after_ isa finding because of locking */
        retval = pci_register_driver(&stl_pcidriver);
-       if (retval)
-               goto err;
-
-       stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
-       if (!stl_serial)
-               return -1;
+       if (retval && stl_nrbrds == 0) {
+               printk(KERN_ERR "STALLION: can't register pci driver\n");
+               goto err_unrtty;
+       }
 
 /*
  *     Set up a character driver for per board stuff. This is mainly used
@@ -4729,37 +4775,26 @@ static int __init stallion_module_init(void)
                printk("STALLION: failed to register serial board device\n");
 
        stallion_class = class_create(THIS_MODULE, "staliomem");
+       if (IS_ERR(stallion_class))
+               printk("STALLION: failed to create class\n");
        for (i = 0; i < 4; i++)
                class_device_create(stallion_class, NULL,
                                    MKDEV(STL_SIOMEMMAJOR, i), NULL,
                                    "staliomem%d", i);
 
-       stl_serial->owner = THIS_MODULE;
-       stl_serial->driver_name = stl_drvname;
-       stl_serial->name = "ttyE";
-       stl_serial->major = STL_SERIALMAJOR;
-       stl_serial->minor_start = 0;
-       stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
-       stl_serial->subtype = SERIAL_TYPE_NORMAL;
-       stl_serial->init_termios = stl_deftermios;
-       stl_serial->flags = TTY_DRIVER_REAL_RAW;
-       tty_set_operations(stl_serial, &stl_ops);
-
-       if (tty_register_driver(stl_serial)) {
-               put_tty_driver(stl_serial);
-               printk("STALLION: failed to register serial driver\n");
-               return -1;
-       }
-
        return 0;
+err_unrtty:
+       tty_unregister_driver(stl_serial);
+err_frtty:
+       put_tty_driver(stl_serial);
 err:
        return retval;
 }
 
 static void __exit stallion_module_exit(void)
 {
-       struct stlbrd   *brdp;
-       int             i;
+       struct stlbrd *brdp;
+       unsigned int i, j;
 
        pr_debug("cleanup_module()\n");
 
@@ -4772,37 +4807,25 @@ static void __exit stallion_module_exit(void)
  *     a hangup on every open port - to try to flush out any processes
  *     hanging onto ports.
  */
-       i = tty_unregister_driver(stl_serial);
-       put_tty_driver(stl_serial);
-       if (i) {
-               printk("STALLION: failed to un-register tty driver, "
-                       "errno=%d\n", -i);
-               return;
+       for (i = 0; i < stl_nrbrds; i++) {
+               if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
+                       continue;
+               for (j = 0; j < brdp->nrports; j++)
+                       tty_unregister_device(stl_serial,
+                               brdp->brdnr * STL_MAXPORTS + j);
        }
+
        for (i = 0; i < 4; i++)
                class_device_destroy(stallion_class, MKDEV(STL_SIOMEMMAJOR, i));
-       if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
-               printk("STALLION: failed to un-register serial memory device, "
-                       "errno=%d\n", -i);
+       unregister_chrdev(STL_SIOMEMMAJOR, "staliomem");
        class_destroy(stallion_class);
 
        pci_unregister_driver(&stl_pcidriver);
 
-       for (i = 0; (i < stl_nrbrds); i++) {
-               if ((brdp = stl_brds[i]) == NULL)
-                       continue;
-
-               free_irq(brdp->irq, brdp);
-
-               stl_cleanup_panels(brdp);
-
-               release_region(brdp->ioaddr1, brdp->iosize1);
-               if (brdp->iosize2 > 0)
-                       release_region(brdp->ioaddr2, brdp->iosize2);
+       stl_free_isabrds();
 
-               kfree(brdp);
-               stl_brds[i] = NULL;
-       }
+       tty_unregister_driver(stl_serial);
+       put_tty_driver(stl_serial);
 }
 
 module_init(stallion_module_init);