2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include "dgnc_driver.h"
23 #include "dgnc_mgmt.h"
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Digi International, http://www.digi.com");
30 MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
31 MODULE_SUPPORTED_DEVICE("dgnc");
33 static const struct file_operations dgnc_board_fops = {
35 .unlocked_ioctl = dgnc_mgmt_ioctl,
36 .open = dgnc_mgmt_open,
37 .release = dgnc_mgmt_close
41 struct dgnc_board *dgnc_board[MAXBOARDS];
42 DEFINE_SPINLOCK(dgnc_global_lock);
43 DEFINE_SPINLOCK(dgnc_poll_lock); /* Poll scheduling lock */
45 int dgnc_poll_tick = 20; /* Poll interval - 20 ms */
47 static struct class *dgnc_class;
49 static ulong dgnc_poll_time; /* Time of next poll */
50 static uint dgnc_poll_stop; /* Used to tell poller to stop */
51 static struct timer_list dgnc_poll_timer;
53 static const struct pci_device_id dgnc_pci_tbl[] = {
54 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_DID), .driver_data = 0},
55 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_422_DID), .driver_data = 1},
56 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_DID), .driver_data = 2},
57 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_422_DID), .driver_data = 3},
60 MODULE_DEVICE_TABLE(pci, dgnc_pci_tbl);
65 unsigned int is_pci_express;
68 static const struct board_id dgnc_ids[] = {
69 { PCI_DEVICE_CLASSIC_4_PCI_NAME, 4, 0 },
70 { PCI_DEVICE_CLASSIC_4_422_PCI_NAME, 4, 0 },
71 { PCI_DEVICE_CLASSIC_8_PCI_NAME, 8, 0 },
72 { PCI_DEVICE_CLASSIC_8_422_PCI_NAME, 8, 0 },
73 { PCI_DEVICE_NEO_4_PCI_NAME, 4, 0 },
74 { PCI_DEVICE_NEO_8_PCI_NAME, 8, 0 },
75 { PCI_DEVICE_NEO_2DB9_PCI_NAME, 2, 0 },
76 { PCI_DEVICE_NEO_2DB9PRI_PCI_NAME, 2, 0 },
77 { PCI_DEVICE_NEO_2RJ45_PCI_NAME, 2, 0 },
78 { PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME, 2, 0 },
79 { PCI_DEVICE_NEO_1_422_PCI_NAME, 1, 0 },
80 { PCI_DEVICE_NEO_1_422_485_PCI_NAME, 1, 0 },
81 { PCI_DEVICE_NEO_2_422_485_PCI_NAME, 2, 0 },
82 { PCI_DEVICE_NEO_EXPRESS_8_PCI_NAME, 8, 1 },
83 { PCI_DEVICE_NEO_EXPRESS_4_PCI_NAME, 4, 1 },
84 { PCI_DEVICE_NEO_EXPRESS_4RJ45_PCI_NAME, 4, 1 },
85 { PCI_DEVICE_NEO_EXPRESS_8RJ45_PCI_NAME, 8, 1 },
89 /* Remap PCI memory. */
90 static int dgnc_do_remap(struct dgnc_board *brd)
92 brd->re_map_membase = ioremap(brd->membase, 0x1000);
93 if (!brd->re_map_membase)
99 /* A board has been found, initialize it. */
100 static struct dgnc_board *dgnc_found_board(struct pci_dev *pdev, int id)
102 struct dgnc_board *brd;
103 unsigned int pci_irq;
107 brd = kzalloc(sizeof(*brd), GFP_KERNEL);
109 return ERR_PTR(-ENOMEM);
111 /* store the info for the board we've found */
112 brd->boardnum = dgnc_num_boards;
113 brd->vendor = dgnc_pci_tbl[id].vendor;
114 brd->device = dgnc_pci_tbl[id].device;
116 brd->pci_bus = pdev->bus->number;
117 brd->pci_slot = PCI_SLOT(pdev->devfn);
118 brd->name = dgnc_ids[id].name;
119 brd->maxports = dgnc_ids[id].maxports;
120 if (dgnc_ids[i].is_pci_express)
121 brd->bd_flags |= BD_IS_PCI_EXPRESS;
122 brd->dpastatus = BD_NOFEP;
123 init_waitqueue_head(&brd->state_wait);
125 spin_lock_init(&brd->bd_lock);
126 spin_lock_init(&brd->bd_intr_lock);
128 brd->state = BOARD_FOUND;
130 /* store which card & revision we have */
131 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
132 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
133 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
138 switch (brd->device) {
139 case PCI_DEVICE_CLASSIC_4_DID:
140 case PCI_DEVICE_CLASSIC_8_DID:
141 case PCI_DEVICE_CLASSIC_4_422_DID:
142 case PCI_DEVICE_CLASSIC_8_422_DID:
144 brd->dpatype = T_CLASSIC | T_PCIBUS;
147 * For PCI ClassicBoards
148 * PCI Local Address (i.e. "resource" number) space
149 * 0 PLX Memory Mapped Config
150 * 1 PLX I/O Mapped Config
151 * 2 I/O Mapped UARTs and Status
152 * 3 Memory Mapped VPD
153 * 4 Memory Mapped UARTs and Status
156 brd->membase = pci_resource_start(pdev, 4);
159 dev_err(&brd->pdev->dev,
160 "Card has no PCI IO resources, failing.\n");
165 brd->membase_end = pci_resource_end(pdev, 4);
167 if (brd->membase & 1)
172 brd->iobase = pci_resource_start(pdev, 1);
173 brd->iobase_end = pci_resource_end(pdev, 1);
174 brd->iobase = ((unsigned int)(brd->iobase)) & 0xFFFE;
176 brd->bd_ops = &dgnc_cls_ops;
178 brd->bd_uart_offset = 0x8;
179 brd->bd_dividend = 921600;
181 rc = dgnc_do_remap(brd);
185 /* Get and store the board VPD, if it exists */
186 brd->bd_ops->vpd(brd);
189 * Enable Local Interrupt 1 (0x1),
190 * Local Interrupt 1 Polarity Active high (0x2),
191 * Enable PCI interrupt (0x40)
193 outb(0x43, brd->iobase + 0x4c);
197 case PCI_DEVICE_NEO_4_DID:
198 case PCI_DEVICE_NEO_8_DID:
199 case PCI_DEVICE_NEO_2DB9_DID:
200 case PCI_DEVICE_NEO_2DB9PRI_DID:
201 case PCI_DEVICE_NEO_2RJ45_DID:
202 case PCI_DEVICE_NEO_2RJ45PRI_DID:
203 case PCI_DEVICE_NEO_1_422_DID:
204 case PCI_DEVICE_NEO_1_422_485_DID:
205 case PCI_DEVICE_NEO_2_422_485_DID:
206 case PCI_DEVICE_NEO_EXPRESS_8_DID:
207 case PCI_DEVICE_NEO_EXPRESS_4_DID:
208 case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID:
209 case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID:
212 * This chip is set up 100% when we get to it.
213 * No need to enable global interrupts or anything.
215 if (brd->bd_flags & BD_IS_PCI_EXPRESS)
216 brd->dpatype = T_NEO_EXPRESS | T_PCIBUS;
218 brd->dpatype = T_NEO | T_PCIBUS;
220 brd->membase = pci_resource_start(pdev, 0);
221 brd->membase_end = pci_resource_end(pdev, 0);
223 if (brd->membase & 1)
228 brd->bd_ops = &dgnc_neo_ops;
230 brd->bd_uart_offset = 0x200;
231 brd->bd_dividend = 921600;
233 rc = dgnc_do_remap(brd);
238 /* Read and store the dvid after remapping */
239 brd->dvid = readb(brd->re_map_membase + 0x8D);
241 /* Get and store the board VPD, if it exists */
242 brd->bd_ops->vpd(brd);
247 dev_err(&brd->pdev->dev,
248 "Didn't find any compatible Neo/Classic PCI boards.\n");
253 tasklet_init(&brd->helper_tasklet,
254 brd->bd_ops->tasklet,
257 wake_up_interruptible(&brd->state_wait);
267 static int dgnc_request_irq(struct dgnc_board *brd)
270 int rc = request_irq(brd->irq, brd->bd_ops->intr,
271 IRQF_SHARED, "DGNC", brd);
273 dev_err(&brd->pdev->dev,
274 "Failed to hook IRQ %d\n", brd->irq);
275 brd->state = BOARD_FAILED;
276 brd->dpastatus = BD_NOFEP;
283 static void dgnc_free_irq(struct dgnc_board *brd)
286 free_irq(brd->irq, brd);
290 * As each timer expires, it determines (a) whether the "transmit"
291 * waiter needs to be woken up, and (b) whether the poller needs to
294 static void dgnc_poll_handler(ulong dummy)
296 struct dgnc_board *brd;
299 unsigned long new_time;
301 for (i = 0; i < dgnc_num_boards; i++) {
304 spin_lock_irqsave(&brd->bd_lock, flags);
306 if (brd->state == BOARD_FAILED) {
307 spin_unlock_irqrestore(&brd->bd_lock, flags);
311 tasklet_schedule(&brd->helper_tasklet);
313 spin_unlock_irqrestore(&brd->bd_lock, flags);
316 /* Schedule ourself back at the nominal wakeup interval. */
318 spin_lock_irqsave(&dgnc_poll_lock, flags);
319 dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
321 new_time = dgnc_poll_time - jiffies;
323 if ((ulong)new_time >= 2 * dgnc_poll_tick)
324 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
326 setup_timer(&dgnc_poll_timer, dgnc_poll_handler, 0);
327 dgnc_poll_timer.expires = dgnc_poll_time;
328 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
331 add_timer(&dgnc_poll_timer);
334 /* returns count (>= 0), or negative on error */
335 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
338 struct dgnc_board *brd;
340 rc = pci_enable_device(pdev);
344 brd = dgnc_found_board(pdev, ent->driver_data);
348 rc = dgnc_tty_register(brd);
350 pr_err(DRVSTR ": Can't register tty devices (%d)\n", rc);
354 rc = dgnc_request_irq(brd);
356 pr_err(DRVSTR ": Can't finalize board init (%d)\n", rc);
360 rc = dgnc_tty_init(brd);
362 pr_err(DRVSTR ": Can't init tty devices (%d)\n", rc);
366 brd->state = BOARD_READY;
367 brd->dpastatus = BD_RUNNING;
369 dgnc_board[dgnc_num_boards++] = brd;
376 dgnc_tty_unregister(brd);
383 static struct pci_driver dgnc_driver = {
385 .probe = dgnc_init_one,
386 .id_table = dgnc_pci_tbl,
389 static int dgnc_start(void)
395 init_timer(&dgnc_poll_timer);
397 rc = register_chrdev(0, "dgnc", &dgnc_board_fops);
399 pr_err(DRVSTR ": Can't register dgnc driver device (%d)\n", rc);
404 dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
405 if (IS_ERR(dgnc_class)) {
406 rc = PTR_ERR(dgnc_class);
407 pr_err(DRVSTR ": Can't create dgnc_mgmt class (%d)\n", rc);
411 dev = device_create(dgnc_class, NULL,
412 MKDEV(dgnc_major, 0),
416 pr_err(DRVSTR ": Can't create device (%d)\n", rc);
420 /* Start the poller */
421 spin_lock_irqsave(&dgnc_poll_lock, flags);
422 setup_timer(&dgnc_poll_timer, dgnc_poll_handler, 0);
423 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
424 dgnc_poll_timer.expires = dgnc_poll_time;
425 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
427 add_timer(&dgnc_poll_timer);
432 class_destroy(dgnc_class);
434 unregister_chrdev(dgnc_major, "dgnc");
439 /* Free all the memory associated with a board */
440 static void dgnc_cleanup_board(struct dgnc_board *brd)
447 switch (brd->device) {
448 case PCI_DEVICE_CLASSIC_4_DID:
449 case PCI_DEVICE_CLASSIC_8_DID:
450 case PCI_DEVICE_CLASSIC_4_422_DID:
451 case PCI_DEVICE_CLASSIC_8_422_DID:
453 /* Tell card not to interrupt anymore. */
454 outb(0, brd->iobase + 0x4c);
462 free_irq(brd->irq, brd);
464 tasklet_kill(&brd->helper_tasklet);
466 if (brd->re_map_membase) {
467 iounmap(brd->re_map_membase);
468 brd->re_map_membase = NULL;
471 for (i = 0; i < MAXPORTS ; i++) {
472 if (brd->channels[i]) {
473 kfree(brd->channels[i]->ch_rqueue);
474 kfree(brd->channels[i]->ch_equeue);
475 kfree(brd->channels[i]->ch_wqueue);
476 kfree(brd->channels[i]);
477 brd->channels[i] = NULL;
481 dgnc_board[brd->boardnum] = NULL;
486 /* Driver load/unload functions */
488 static void cleanup(void)
493 spin_lock_irqsave(&dgnc_poll_lock, flags);
495 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
497 /* Turn off poller right away. */
498 del_timer_sync(&dgnc_poll_timer);
500 device_destroy(dgnc_class, MKDEV(dgnc_major, 0));
501 class_destroy(dgnc_class);
502 unregister_chrdev(dgnc_major, "dgnc");
504 for (i = 0; i < dgnc_num_boards; ++i) {
505 dgnc_cleanup_tty(dgnc_board[i]);
506 dgnc_cleanup_board(dgnc_board[i]);
510 static void __exit dgnc_cleanup_module(void)
513 pci_unregister_driver(&dgnc_driver);
516 static int __init dgnc_init_module(void)
520 /* Initialize global stuff */
525 /* Find and configure all the cards */
526 rc = pci_register_driver(&dgnc_driver);
528 pr_warn("WARNING: dgnc driver load failed. No Digi Neo or Classic boards found.\n");
535 module_init(dgnc_init_module);
536 module_exit(dgnc_cleanup_module);