linux/drivers/staging/dgnc/dgnc_driver.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Copyright 2003 Digi International (www.digi.com)
   4 *      Scott H Kilau <Scott_Kilau at digi dot com>
   5 */
   6
   7#include <linux/kernel.h>
   8#include <linux/module.h>
   9#include <linux/pci.h>
  10#include <linux/slab.h>
  11#include <linux/sched.h>
  12#include "dgnc_driver.h"
  13#include "dgnc_tty.h"
  14#include "dgnc_cls.h"
  15
  16MODULE_LICENSE("GPL");
  17MODULE_AUTHOR("Digi International, http://www.digi.com");
  18MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
  19MODULE_SUPPORTED_DEVICE("dgnc");
  20
  21static unsigned int dgnc_num_boards;
  22struct dgnc_board               *dgnc_board[MAXBOARDS];
  23static DEFINE_SPINLOCK(dgnc_poll_lock); /* Poll scheduling lock */
  24
  25static int              dgnc_poll_tick = 20;    /* Poll interval - 20 ms */
  26static ulong            dgnc_poll_time; /* Time of next poll */
  27static uint             dgnc_poll_stop; /* Used to tell poller to stop */
  28static struct timer_list dgnc_poll_timer;
  29
  30#define DIGI_VID                                0x114F
  31#define PCI_DEVICE_CLASSIC_4_DID                0x0028
  32#define PCI_DEVICE_CLASSIC_8_DID                0x0029
  33#define PCI_DEVICE_CLASSIC_4_422_DID            0x00D0
  34#define PCI_DEVICE_CLASSIC_8_422_DID            0x00D1
  35
  36#define PCI_DEVICE_CLASSIC_4_PCI_NAME           "ClassicBoard 4 PCI"
  37#define PCI_DEVICE_CLASSIC_8_PCI_NAME           "ClassicBoard 8 PCI"
  38#define PCI_DEVICE_CLASSIC_4_422_PCI_NAME       "ClassicBoard 4 422 PCI"
  39#define PCI_DEVICE_CLASSIC_8_422_PCI_NAME       "ClassicBoard 8 422 PCI"
  40
  41static const struct pci_device_id dgnc_pci_tbl[] = {
  42        {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_DID),     .driver_data = 0},
  43        {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_422_DID), .driver_data = 1},
  44        {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_DID),     .driver_data = 2},
  45        {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_422_DID), .driver_data = 3},
  46        {0,}
  47};
  48MODULE_DEVICE_TABLE(pci, dgnc_pci_tbl);
  49
  50struct board_id {
  51        unsigned char *name;
  52        uint maxports;
  53        unsigned int is_pci_express;
  54};
  55
  56static const struct board_id dgnc_ids[] = {
  57        {       PCI_DEVICE_CLASSIC_4_PCI_NAME,          4,      0       },
  58        {       PCI_DEVICE_CLASSIC_4_422_PCI_NAME,      4,      0       },
  59        {       PCI_DEVICE_CLASSIC_8_PCI_NAME,          8,      0       },
  60        {       PCI_DEVICE_CLASSIC_8_422_PCI_NAME,      8,      0       },
  61        {       NULL,                                   0,      0       }
  62};
  63
  64/* Remap PCI memory. */
  65static int dgnc_do_remap(struct dgnc_board *brd)
  66{
  67        brd->re_map_membase = ioremap(brd->membase, 0x1000);
  68        if (!brd->re_map_membase)
  69                return -ENOMEM;
  70
  71        return 0;
  72}
  73
  74/* A board has been found, initialize  it. */
  75static struct dgnc_board *dgnc_found_board(struct pci_dev *pdev, int id)
  76{
  77        struct dgnc_board *brd;
  78        unsigned int pci_irq;
  79        int rc = 0;
  80
  81        brd = kzalloc(sizeof(*brd), GFP_KERNEL);
  82        if (!brd)
  83                return ERR_PTR(-ENOMEM);
  84
  85        /* store the info for the board we've found */
  86        brd->boardnum = dgnc_num_boards;
  87        brd->device = dgnc_pci_tbl[id].device;
  88        brd->pdev = pdev;
  89        brd->name = dgnc_ids[id].name;
  90        brd->maxports = dgnc_ids[id].maxports;
  91        init_waitqueue_head(&brd->state_wait);
  92
  93        spin_lock_init(&brd->bd_lock);
  94        spin_lock_init(&brd->bd_intr_lock);
  95
  96        brd->state              = BOARD_FOUND;
  97
  98        pci_irq = pdev->irq;
  99        brd->irq = pci_irq;
 100
 101        switch (brd->device) {
 102        case PCI_DEVICE_CLASSIC_4_DID:
 103        case PCI_DEVICE_CLASSIC_8_DID:
 104        case PCI_DEVICE_CLASSIC_4_422_DID:
 105        case PCI_DEVICE_CLASSIC_8_422_DID:
 106                /*
 107                 * For PCI ClassicBoards
 108                 * PCI Local Address (i.e. "resource" number) space
 109                 * 0    PLX Memory Mapped Config
 110                 * 1    PLX I/O Mapped Config
 111                 * 2    I/O Mapped UARTs and Status
 112                 * 3    Memory Mapped VPD
 113                 * 4    Memory Mapped UARTs and Status
 114                 */
 115
 116                brd->membase = pci_resource_start(pdev, 4);
 117
 118                if (!brd->membase) {
 119                        dev_err(&brd->pdev->dev,
 120                                "Card has no PCI IO resources, failing.\n");
 121                        rc = -ENODEV;
 122                        goto failed;
 123                }
 124
 125                brd->membase_end = pci_resource_end(pdev, 4);
 126
 127                if (brd->membase & 1)
 128                        brd->membase &= ~3;
 129                else
 130                        brd->membase &= ~15;
 131
 132                brd->iobase     = pci_resource_start(pdev, 1);
 133                brd->iobase_end = pci_resource_end(pdev, 1);
 134                brd->iobase     = ((unsigned int)(brd->iobase)) & 0xFFFE;
 135
 136                brd->bd_ops = &dgnc_cls_ops;
 137
 138                brd->bd_uart_offset = 0x8;
 139                brd->bd_dividend = 921600;
 140
 141                rc = dgnc_do_remap(brd);
 142                if (rc < 0)
 143                        goto failed;
 144
 145                /*
 146                 * Enable Local Interrupt 1               (0x1),
 147                 * Local Interrupt 1 Polarity Active high (0x2),
 148                 * Enable PCI interrupt                   (0x40)
 149                 */
 150                outb(0x43, brd->iobase + 0x4c);
 151
 152                break;
 153
 154        default:
 155                dev_err(&brd->pdev->dev,
 156                        "Didn't find any compatible Neo/Classic PCI boards.\n");
 157                rc = -ENXIO;
 158                goto failed;
 159        }
 160
 161        tasklet_init(&brd->helper_tasklet,
 162                     brd->bd_ops->tasklet,
 163                     (unsigned long)brd);
 164
 165        wake_up_interruptible(&brd->state_wait);
 166
 167        return brd;
 168
 169failed:
 170        kfree(brd);
 171
 172        return ERR_PTR(rc);
 173}
 174
 175static int dgnc_request_irq(struct dgnc_board *brd)
 176{
 177        if (brd->irq) {
 178                int rc = request_irq(brd->irq, brd->bd_ops->intr,
 179                                 IRQF_SHARED, "DGNC", brd);
 180                if (rc) {
 181                        dev_err(&brd->pdev->dev,
 182                                "Failed to hook IRQ %d\n", brd->irq);
 183                        brd->state = BOARD_FAILED;
 184                        return -ENODEV;
 185                }
 186        }
 187        return 0;
 188}
 189
 190static void dgnc_free_irq(struct dgnc_board *brd)
 191{
 192        if (brd->irq)
 193                free_irq(brd->irq, brd);
 194}
 195
 196 /*
 197  * As each timer expires, it determines (a) whether the "transmit"
 198  * waiter needs to be woken up, and (b) whether the poller needs to
 199  * be rescheduled.
 200  */
 201static void dgnc_poll_handler(struct timer_list *unused)
 202{
 203        struct dgnc_board *brd;
 204        unsigned long flags;
 205        int i;
 206        unsigned long new_time;
 207
 208        for (i = 0; i < dgnc_num_boards; i++) {
 209                brd = dgnc_board[i];
 210
 211                spin_lock_irqsave(&brd->bd_lock, flags);
 212
 213                if (brd->state == BOARD_FAILED) {
 214                        spin_unlock_irqrestore(&brd->bd_lock, flags);
 215                        continue;
 216                }
 217
 218                tasklet_schedule(&brd->helper_tasklet);
 219
 220                spin_unlock_irqrestore(&brd->bd_lock, flags);
 221        }
 222
 223        /* Schedule ourself back at the nominal wakeup interval. */
 224
 225        spin_lock_irqsave(&dgnc_poll_lock, flags);
 226        dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
 227
 228        new_time = dgnc_poll_time - jiffies;
 229
 230        if ((ulong)new_time >= 2 * dgnc_poll_tick)
 231                dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
 232
 233        timer_setup(&dgnc_poll_timer, dgnc_poll_handler, 0);
 234        dgnc_poll_timer.expires = dgnc_poll_time;
 235        spin_unlock_irqrestore(&dgnc_poll_lock, flags);
 236
 237        if (!dgnc_poll_stop)
 238                add_timer(&dgnc_poll_timer);
 239}
 240
 241/* returns count (>= 0), or negative on error */
 242static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 243{
 244        int rc;
 245        struct dgnc_board *brd;
 246
 247        rc = pci_enable_device(pdev);
 248        if (rc)
 249                return -EIO;
 250
 251        brd = dgnc_found_board(pdev, ent->driver_data);
 252        if (IS_ERR(brd))
 253                return PTR_ERR(brd);
 254
 255        rc = dgnc_tty_register(brd);
 256        if (rc < 0) {
 257                pr_err(DRVSTR ": Can't register tty devices (%d)\n", rc);
 258                goto failed;
 259        }
 260
 261        rc = dgnc_request_irq(brd);
 262        if (rc < 0) {
 263                pr_err(DRVSTR ": Can't finalize board init (%d)\n", rc);
 264                goto unregister_tty;
 265        }
 266
 267        rc = dgnc_tty_init(brd);
 268        if (rc < 0) {
 269                pr_err(DRVSTR ": Can't init tty devices (%d)\n", rc);
 270                goto free_irq;
 271        }
 272
 273        brd->state = BOARD_READY;
 274
 275        dgnc_board[dgnc_num_boards++] = brd;
 276
 277        return 0;
 278
 279free_irq:
 280        dgnc_free_irq(brd);
 281unregister_tty:
 282        dgnc_tty_unregister(brd);
 283failed:
 284        kfree(brd);
 285
 286        return rc;
 287}
 288
 289static struct pci_driver dgnc_driver = {
 290        .name           = "dgnc",
 291        .probe          = dgnc_init_one,
 292        .id_table       = dgnc_pci_tbl,
 293};
 294
 295static int dgnc_start(void)
 296{
 297        unsigned long flags;
 298
 299        /* Start the poller */
 300        spin_lock_irqsave(&dgnc_poll_lock, flags);
 301        timer_setup(&dgnc_poll_timer, dgnc_poll_handler, 0);
 302        dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
 303        dgnc_poll_timer.expires = dgnc_poll_time;
 304        spin_unlock_irqrestore(&dgnc_poll_lock, flags);
 305
 306        add_timer(&dgnc_poll_timer);
 307
 308        return 0;
 309}
 310
 311/* Free all the memory associated with a board */
 312static void dgnc_cleanup_board(struct dgnc_board *brd)
 313{
 314        int i = 0;
 315
 316        if (!brd)
 317                return;
 318
 319        switch (brd->device) {
 320        case PCI_DEVICE_CLASSIC_4_DID:
 321        case PCI_DEVICE_CLASSIC_8_DID:
 322        case PCI_DEVICE_CLASSIC_4_422_DID:
 323        case PCI_DEVICE_CLASSIC_8_422_DID:
 324
 325                /* Tell card not to interrupt anymore. */
 326                outb(0, brd->iobase + 0x4c);
 327                break;
 328
 329        default:
 330                break;
 331        }
 332
 333        if (brd->irq)
 334                free_irq(brd->irq, brd);
 335
 336        tasklet_kill(&brd->helper_tasklet);
 337
 338        if (brd->re_map_membase) {
 339                iounmap(brd->re_map_membase);
 340                brd->re_map_membase = NULL;
 341        }
 342
 343        for (i = 0; i < MAXPORTS ; i++) {
 344                if (brd->channels[i]) {
 345                        kfree(brd->channels[i]->ch_rqueue);
 346                        kfree(brd->channels[i]->ch_equeue);
 347                        kfree(brd->channels[i]->ch_wqueue);
 348                        kfree(brd->channels[i]);
 349                        brd->channels[i] = NULL;
 350                }
 351        }
 352
 353        dgnc_board[brd->boardnum] = NULL;
 354
 355        kfree(brd);
 356}
 357
 358/* Driver load/unload functions */
 359
 360static void cleanup(void)
 361{
 362        int i;
 363        unsigned long flags;
 364
 365        spin_lock_irqsave(&dgnc_poll_lock, flags);
 366        dgnc_poll_stop = 1;
 367        spin_unlock_irqrestore(&dgnc_poll_lock, flags);
 368
 369        /* Turn off poller right away. */
 370        del_timer_sync(&dgnc_poll_timer);
 371
 372        for (i = 0; i < dgnc_num_boards; ++i) {
 373                dgnc_cleanup_tty(dgnc_board[i]);
 374                dgnc_cleanup_board(dgnc_board[i]);
 375        }
 376}
 377
 378static void __exit dgnc_cleanup_module(void)
 379{
 380        cleanup();
 381        pci_unregister_driver(&dgnc_driver);
 382}
 383
 384static int __init dgnc_init_module(void)
 385{
 386        int rc;
 387
 388        /* Initialize global stuff */
 389        rc = dgnc_start();
 390        if (rc < 0)
 391                return rc;
 392
 393        /* Find and configure all the cards */
 394        rc = pci_register_driver(&dgnc_driver);
 395        if (rc) {
 396                pr_warn("WARNING: dgnc driver load failed.  No Digi Neo or Classic boards found.\n");
 397                cleanup();
 398                return rc;
 399        }
 400        return 0;
 401}
 402
 403module_init(dgnc_init_module);
 404module_exit(dgnc_cleanup_module);
 405