1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2003 Digi International (www.digi.com)
4 * Scott H Kilau <Scott_Kilau at digi dot com>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include "dgnc_driver.h"
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Digi International, http://www.digi.com");
18 MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
19 MODULE_SUPPORTED_DEVICE("dgnc");
21 static unsigned int dgnc_num_boards;
22 struct dgnc_board *dgnc_board[MAXBOARDS];
23 static DEFINE_SPINLOCK(dgnc_poll_lock); /* Poll scheduling lock */
25 static int dgnc_poll_tick = 20; /* Poll interval - 20 ms */
26 static ulong dgnc_poll_time; /* Time of next poll */
27 static uint dgnc_poll_stop; /* Used to tell poller to stop */
28 static struct timer_list dgnc_poll_timer;
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
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"
41 static 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},
48 MODULE_DEVICE_TABLE(pci, dgnc_pci_tbl);
53 unsigned int is_pci_express;
56 static 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 },
64 /* Remap PCI memory. */
65 static int dgnc_do_remap(struct dgnc_board *brd)
67 brd->re_map_membase = ioremap(brd->membase, 0x1000);
68 if (!brd->re_map_membase)
74 /* A board has been found, initialize it. */
75 static struct dgnc_board *dgnc_found_board(struct pci_dev *pdev, int id)
77 struct dgnc_board *brd;
81 brd = kzalloc(sizeof(*brd), GFP_KERNEL);
83 return ERR_PTR(-ENOMEM);
85 /* store the info for the board we've found */
86 brd->boardnum = dgnc_num_boards;
87 brd->device = dgnc_pci_tbl[id].device;
89 brd->name = dgnc_ids[id].name;
90 brd->maxports = dgnc_ids[id].maxports;
91 init_waitqueue_head(&brd->state_wait);
93 spin_lock_init(&brd->bd_lock);
94 spin_lock_init(&brd->bd_intr_lock);
96 brd->state = BOARD_FOUND;
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:
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
116 brd->membase = pci_resource_start(pdev, 4);
119 dev_err(&brd->pdev->dev,
120 "Card has no PCI IO resources, failing.\n");
125 brd->membase_end = pci_resource_end(pdev, 4);
127 if (brd->membase & 1)
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;
136 brd->bd_ops = &dgnc_cls_ops;
138 brd->bd_uart_offset = 0x8;
139 brd->bd_dividend = 921600;
141 rc = dgnc_do_remap(brd);
146 * Enable Local Interrupt 1 (0x1),
147 * Local Interrupt 1 Polarity Active high (0x2),
148 * Enable PCI interrupt (0x40)
150 outb(0x43, brd->iobase + 0x4c);
155 dev_err(&brd->pdev->dev,
156 "Didn't find any compatible Neo/Classic PCI boards.\n");
161 tasklet_init(&brd->helper_tasklet,
162 brd->bd_ops->tasklet,
165 wake_up_interruptible(&brd->state_wait);
175 static int dgnc_request_irq(struct dgnc_board *brd)
178 int rc = request_irq(brd->irq, brd->bd_ops->intr,
179 IRQF_SHARED, "DGNC", brd);
181 dev_err(&brd->pdev->dev,
182 "Failed to hook IRQ %d\n", brd->irq);
183 brd->state = BOARD_FAILED;
190 static void dgnc_free_irq(struct dgnc_board *brd)
193 free_irq(brd->irq, brd);
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
201 static void dgnc_poll_handler(struct timer_list *unused)
203 struct dgnc_board *brd;
206 unsigned long new_time;
208 for (i = 0; i < dgnc_num_boards; i++) {
211 spin_lock_irqsave(&brd->bd_lock, flags);
213 if (brd->state == BOARD_FAILED) {
214 spin_unlock_irqrestore(&brd->bd_lock, flags);
218 tasklet_schedule(&brd->helper_tasklet);
220 spin_unlock_irqrestore(&brd->bd_lock, flags);
223 /* Schedule ourself back at the nominal wakeup interval. */
225 spin_lock_irqsave(&dgnc_poll_lock, flags);
226 dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
228 new_time = dgnc_poll_time - jiffies;
230 if ((ulong)new_time >= 2 * dgnc_poll_tick)
231 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
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);
238 add_timer(&dgnc_poll_timer);
241 /* returns count (>= 0), or negative on error */
242 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
245 struct dgnc_board *brd;
247 rc = pci_enable_device(pdev);
251 brd = dgnc_found_board(pdev, ent->driver_data);
255 rc = dgnc_tty_register(brd);
257 pr_err(DRVSTR ": Can't register tty devices (%d)\n", rc);
261 rc = dgnc_request_irq(brd);
263 pr_err(DRVSTR ": Can't finalize board init (%d)\n", rc);
267 rc = dgnc_tty_init(brd);
269 pr_err(DRVSTR ": Can't init tty devices (%d)\n", rc);
273 brd->state = BOARD_READY;
275 dgnc_board[dgnc_num_boards++] = brd;
282 dgnc_tty_unregister(brd);
289 static struct pci_driver dgnc_driver = {
291 .probe = dgnc_init_one,
292 .id_table = dgnc_pci_tbl,
295 static int dgnc_start(void)
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);
306 add_timer(&dgnc_poll_timer);
311 /* Free all the memory associated with a board */
312 static void dgnc_cleanup_board(struct dgnc_board *brd)
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:
325 /* Tell card not to interrupt anymore. */
326 outb(0, brd->iobase + 0x4c);
334 free_irq(brd->irq, brd);
336 tasklet_kill(&brd->helper_tasklet);
338 if (brd->re_map_membase) {
339 iounmap(brd->re_map_membase);
340 brd->re_map_membase = NULL;
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;
353 dgnc_board[brd->boardnum] = NULL;
358 /* Driver load/unload functions */
360 static void cleanup(void)
365 spin_lock_irqsave(&dgnc_poll_lock, flags);
367 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
369 /* Turn off poller right away. */
370 del_timer_sync(&dgnc_poll_timer);
372 for (i = 0; i < dgnc_num_boards; ++i) {
373 dgnc_cleanup_tty(dgnc_board[i]);
374 dgnc_cleanup_board(dgnc_board[i]);
378 static void __exit dgnc_cleanup_module(void)
381 pci_unregister_driver(&dgnc_driver);
384 static int __init dgnc_init_module(void)
388 /* Initialize global stuff */
393 /* Find and configure all the cards */
394 rc = pci_register_driver(&dgnc_driver);
396 pr_warn("WARNING: dgnc driver load failed. No Digi Neo or Classic boards found.\n");
403 module_init(dgnc_init_module);
404 module_exit(dgnc_cleanup_module);