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.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include "dgnc_driver.h"
24 #include "dgnc_mgmt.h"
28 #include "dgnc_sysfs.h"
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Digi International, http://www.digi.com");
32 MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
33 MODULE_SUPPORTED_DEVICE("dgnc");
35 /**************************************************************************
37 * protos for this file
40 static int dgnc_start(void);
41 static int dgnc_finalize_board_init(struct dgnc_board
*brd
);
42 static void dgnc_init_globals(void);
43 static int dgnc_found_board(struct pci_dev
*pdev
, int id
);
44 static void dgnc_cleanup_board(struct dgnc_board
*brd
);
45 static void dgnc_poll_handler(ulong dummy
);
46 static int dgnc_init_one(struct pci_dev
*pdev
,
47 const struct pci_device_id
*ent
);
48 static void dgnc_do_remap(struct dgnc_board
*brd
);
51 * File operations permitted on Control/Management major.
53 static const struct file_operations dgnc_BoardFops
= {
55 .unlocked_ioctl
= dgnc_mgmt_ioctl
,
56 .open
= dgnc_mgmt_open
,
57 .release
= dgnc_mgmt_close
65 struct dgnc_board
*dgnc_Board
[MAXBOARDS
];
66 DEFINE_SPINLOCK(dgnc_global_lock
);
68 int dgnc_poll_tick
= 20; /* Poll interval - 20 ms */
73 static struct class *dgnc_class
;
78 static DEFINE_SPINLOCK(dgnc_poll_lock
); /* Poll scheduling lock */
79 static ulong dgnc_poll_time
; /* Time of next poll */
80 static uint dgnc_poll_stop
; /* Used to tell poller to stop */
81 static struct timer_list dgnc_poll_timer
;
84 static const struct pci_device_id dgnc_pci_tbl
[] = {
85 {PCI_DEVICE(DIGI_VID
, PCI_DEVICE_CLASSIC_4_DID
), .driver_data
= 0},
86 {PCI_DEVICE(DIGI_VID
, PCI_DEVICE_CLASSIC_4_422_DID
), .driver_data
= 1},
87 {PCI_DEVICE(DIGI_VID
, PCI_DEVICE_CLASSIC_8_DID
), .driver_data
= 2},
88 {PCI_DEVICE(DIGI_VID
, PCI_DEVICE_CLASSIC_8_422_DID
), .driver_data
= 3},
91 MODULE_DEVICE_TABLE(pci
, dgnc_pci_tbl
);
96 unsigned int is_pci_express
;
99 static struct board_id dgnc_Ids
[] = {
100 { PCI_DEVICE_CLASSIC_4_PCI_NAME
, 4, 0 },
101 { PCI_DEVICE_CLASSIC_4_422_PCI_NAME
, 4, 0 },
102 { PCI_DEVICE_CLASSIC_8_PCI_NAME
, 8, 0 },
103 { PCI_DEVICE_CLASSIC_8_422_PCI_NAME
, 8, 0 },
104 { PCI_DEVICE_NEO_4_PCI_NAME
, 4, 0 },
105 { PCI_DEVICE_NEO_8_PCI_NAME
, 8, 0 },
106 { PCI_DEVICE_NEO_2DB9_PCI_NAME
, 2, 0 },
107 { PCI_DEVICE_NEO_2DB9PRI_PCI_NAME
, 2, 0 },
108 { PCI_DEVICE_NEO_2RJ45_PCI_NAME
, 2, 0 },
109 { PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME
, 2, 0 },
110 { PCI_DEVICE_NEO_1_422_PCI_NAME
, 1, 0 },
111 { PCI_DEVICE_NEO_1_422_485_PCI_NAME
, 1, 0 },
112 { PCI_DEVICE_NEO_2_422_485_PCI_NAME
, 2, 0 },
113 { PCI_DEVICE_NEO_EXPRESS_8_PCI_NAME
, 8, 1 },
114 { PCI_DEVICE_NEO_EXPRESS_4_PCI_NAME
, 4, 1 },
115 { PCI_DEVICE_NEO_EXPRESS_4RJ45_PCI_NAME
, 4, 1 },
116 { PCI_DEVICE_NEO_EXPRESS_8RJ45_PCI_NAME
, 8, 1 },
120 static struct pci_driver dgnc_driver
= {
122 .probe
= dgnc_init_one
,
123 .id_table
= dgnc_pci_tbl
,
126 /************************************************************************
128 * Driver load/unload functions
130 ************************************************************************/
133 * dgnc_cleanup_module()
135 * Module unload. This is where it all ends.
137 static void dgnc_cleanup_module(void)
142 spin_lock_irqsave(&dgnc_poll_lock
, flags
);
144 spin_unlock_irqrestore(&dgnc_poll_lock
, flags
);
146 /* Turn off poller right away. */
147 del_timer_sync(&dgnc_poll_timer
);
149 dgnc_remove_driver_sysfiles(&dgnc_driver
);
151 device_destroy(dgnc_class
, MKDEV(dgnc_Major
, 0));
152 class_destroy(dgnc_class
);
153 unregister_chrdev(dgnc_Major
, "dgnc");
155 for (i
= 0; i
< dgnc_NumBoards
; ++i
) {
156 dgnc_remove_ports_sysfiles(dgnc_Board
[i
]);
157 dgnc_tty_uninit(dgnc_Board
[i
]);
158 dgnc_cleanup_board(dgnc_Board
[i
]);
161 dgnc_tty_post_uninit();
164 pci_unregister_driver(&dgnc_driver
);
170 * Module load. This is where it all starts.
172 static int __init
dgnc_init_module(void)
177 * Initialize global stuff
185 * Find and configure all the cards
187 rc
= pci_register_driver(&dgnc_driver
);
190 * If something went wrong in the scan, bail out of driver.
193 /* Only unregister if it was actually registered. */
195 pci_unregister_driver(&dgnc_driver
);
197 pr_warn("WARNING: dgnc driver load failed. No Digi Neo or Classic boards found.\n");
199 dgnc_cleanup_module();
201 dgnc_create_driver_sysfiles(&dgnc_driver
);
207 module_init(dgnc_init_module
);
208 module_exit(dgnc_cleanup_module
);
213 static int dgnc_start(void)
219 /* make sure that the globals are init'd before we do anything else */
223 * Register our base character device into the kernel.
224 * This allows the download daemon to connect to the downld device
225 * before any of the boards are init'ed.
227 * Register management/dpa devices
229 rc
= register_chrdev(0, "dgnc", &dgnc_BoardFops
);
231 pr_err(DRVSTR
": Can't register dgnc driver device (%d)\n", rc
);
236 dgnc_class
= class_create(THIS_MODULE
, "dgnc_mgmt");
237 if (IS_ERR(dgnc_class
)) {
238 rc
= PTR_ERR(dgnc_class
);
239 pr_err(DRVSTR
": Can't create dgnc_mgmt class (%d)\n", rc
);
243 dev
= device_create(dgnc_class
, NULL
,
244 MKDEV(dgnc_Major
, 0),
248 pr_err(DRVSTR
": Can't create device (%d)\n", rc
);
253 * Init any global tty stuff.
255 rc
= dgnc_tty_preinit();
258 pr_err(DRVSTR
": tty preinit - not enough memory (%d)\n", rc
);
262 /* Start the poller */
263 spin_lock_irqsave(&dgnc_poll_lock
, flags
);
264 setup_timer(&dgnc_poll_timer
, dgnc_poll_handler
, 0);
265 dgnc_poll_time
= jiffies
+ dgnc_jiffies_from_ms(dgnc_poll_tick
);
266 dgnc_poll_timer
.expires
= dgnc_poll_time
;
267 spin_unlock_irqrestore(&dgnc_poll_lock
, flags
);
269 add_timer(&dgnc_poll_timer
);
274 device_destroy(dgnc_class
, MKDEV(dgnc_Major
, 0));
276 class_destroy(dgnc_class
);
278 unregister_chrdev(dgnc_Major
, "dgnc");
282 /* returns count (>= 0), or negative on error */
283 static int dgnc_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
287 /* wake up and enable device */
288 rc
= pci_enable_device(pdev
);
293 rc
= dgnc_found_board(pdev
, ent
->driver_data
);
301 * dgnc_cleanup_board()
303 * Free all the memory associated with a board
305 static void dgnc_cleanup_board(struct dgnc_board
*brd
)
309 if (!brd
|| brd
->magic
!= DGNC_BOARD_MAGIC
)
312 switch (brd
->device
) {
313 case PCI_DEVICE_CLASSIC_4_DID
:
314 case PCI_DEVICE_CLASSIC_8_DID
:
315 case PCI_DEVICE_CLASSIC_4_422_DID
:
316 case PCI_DEVICE_CLASSIC_8_422_DID
:
318 /* Tell card not to interrupt anymore. */
319 outb(0, brd
->iobase
+ 0x4c);
327 free_irq(brd
->irq
, brd
);
329 tasklet_kill(&brd
->helper_tasklet
);
331 if (brd
->re_map_membase
) {
332 iounmap(brd
->re_map_membase
);
333 brd
->re_map_membase
= NULL
;
336 if (brd
->msgbuf_head
) {
339 spin_lock_irqsave(&dgnc_global_lock
, flags
);
341 dev_dbg(&brd
->pdev
->dev
, "%s\n", brd
->msgbuf_head
);
342 kfree(brd
->msgbuf_head
);
343 brd
->msgbuf_head
= NULL
;
344 spin_unlock_irqrestore(&dgnc_global_lock
, flags
);
347 /* Free all allocated channels structs */
348 for (i
= 0; i
< MAXPORTS
; i
++) {
349 if (brd
->channels
[i
]) {
350 kfree(brd
->channels
[i
]->ch_rqueue
);
351 kfree(brd
->channels
[i
]->ch_equeue
);
352 kfree(brd
->channels
[i
]->ch_wqueue
);
353 kfree(brd
->channels
[i
]);
354 brd
->channels
[i
] = NULL
;
359 dgnc_Board
[brd
->boardnum
] = NULL
;
368 * A board has been found, init it.
370 static int dgnc_found_board(struct pci_dev
*pdev
, int id
)
372 struct dgnc_board
*brd
;
373 unsigned int pci_irq
;
378 /* get the board structure and prep it */
379 dgnc_Board
[dgnc_NumBoards
] = kzalloc(sizeof(*brd
), GFP_KERNEL
);
380 brd
= dgnc_Board
[dgnc_NumBoards
];
385 /* make a temporary message buffer for the boot messages */
386 brd
->msgbuf_head
= kcalloc(8192, sizeof(u8
), GFP_KERNEL
);
387 brd
->msgbuf
= brd
->msgbuf_head
;
394 /* store the info for the board we've found */
395 brd
->magic
= DGNC_BOARD_MAGIC
;
396 brd
->boardnum
= dgnc_NumBoards
;
397 brd
->vendor
= dgnc_pci_tbl
[id
].vendor
;
398 brd
->device
= dgnc_pci_tbl
[id
].device
;
400 brd
->pci_bus
= pdev
->bus
->number
;
401 brd
->pci_slot
= PCI_SLOT(pdev
->devfn
);
402 brd
->name
= dgnc_Ids
[id
].name
;
403 brd
->maxports
= dgnc_Ids
[id
].maxports
;
404 if (dgnc_Ids
[i
].is_pci_express
)
405 brd
->bd_flags
|= BD_IS_PCI_EXPRESS
;
406 brd
->dpastatus
= BD_NOFEP
;
407 init_waitqueue_head(&brd
->state_wait
);
409 spin_lock_init(&brd
->bd_lock
);
410 spin_lock_init(&brd
->bd_intr_lock
);
412 brd
->state
= BOARD_FOUND
;
414 for (i
= 0; i
< MAXPORTS
; i
++)
415 brd
->channels
[i
] = NULL
;
417 /* store which card & revision we have */
418 pci_read_config_word(pdev
, PCI_SUBSYSTEM_VENDOR_ID
, &brd
->subvendor
);
419 pci_read_config_word(pdev
, PCI_SUBSYSTEM_ID
, &brd
->subdevice
);
420 pci_read_config_byte(pdev
, PCI_REVISION_ID
, &brd
->rev
);
426 switch (brd
->device
) {
428 case PCI_DEVICE_CLASSIC_4_DID
:
429 case PCI_DEVICE_CLASSIC_8_DID
:
430 case PCI_DEVICE_CLASSIC_4_422_DID
:
431 case PCI_DEVICE_CLASSIC_8_422_DID
:
433 brd
->dpatype
= T_CLASSIC
| T_PCIBUS
;
436 * For PCI ClassicBoards
437 * PCI Local Address (i.e. "resource" number) space
438 * 0 PLX Memory Mapped Config
439 * 1 PLX I/O Mapped Config
440 * 2 I/O Mapped UARTs and Status
441 * 3 Memory Mapped VPD
442 * 4 Memory Mapped UARTs and Status
446 /* get the PCI Base Address Registers */
447 brd
->membase
= pci_resource_start(pdev
, 4);
450 dev_err(&brd
->pdev
->dev
,
451 "Card has no PCI IO resources, failing.\n");
455 brd
->membase_end
= pci_resource_end(pdev
, 4);
457 if (brd
->membase
& 1)
462 brd
->iobase
= pci_resource_start(pdev
, 1);
463 brd
->iobase_end
= pci_resource_end(pdev
, 1);
464 brd
->iobase
= ((unsigned int) (brd
->iobase
)) & 0xFFFE;
466 /* Assign the board_ops struct */
467 brd
->bd_ops
= &dgnc_cls_ops
;
469 brd
->bd_uart_offset
= 0x8;
470 brd
->bd_dividend
= 921600;
474 /* Get and store the board VPD, if it exists */
475 brd
->bd_ops
->vpd(brd
);
478 * Enable Local Interrupt 1 (0x1),
479 * Local Interrupt 1 Polarity Active high (0x2),
480 * Enable PCI interrupt (0x40)
482 outb(0x43, brd
->iobase
+ 0x4c);
487 case PCI_DEVICE_NEO_4_DID
:
488 case PCI_DEVICE_NEO_8_DID
:
489 case PCI_DEVICE_NEO_2DB9_DID
:
490 case PCI_DEVICE_NEO_2DB9PRI_DID
:
491 case PCI_DEVICE_NEO_2RJ45_DID
:
492 case PCI_DEVICE_NEO_2RJ45PRI_DID
:
493 case PCI_DEVICE_NEO_1_422_DID
:
494 case PCI_DEVICE_NEO_1_422_485_DID
:
495 case PCI_DEVICE_NEO_2_422_485_DID
:
496 case PCI_DEVICE_NEO_EXPRESS_8_DID
:
497 case PCI_DEVICE_NEO_EXPRESS_4_DID
:
498 case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID
:
499 case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID
:
502 * This chip is set up 100% when we get to it.
503 * No need to enable global interrupts or anything.
505 if (brd
->bd_flags
& BD_IS_PCI_EXPRESS
)
506 brd
->dpatype
= T_NEO_EXPRESS
| T_PCIBUS
;
508 brd
->dpatype
= T_NEO
| T_PCIBUS
;
510 /* get the PCI Base Address Registers */
511 brd
->membase
= pci_resource_start(pdev
, 0);
512 brd
->membase_end
= pci_resource_end(pdev
, 0);
514 if (brd
->membase
& 1)
519 /* Assign the board_ops struct */
520 brd
->bd_ops
= &dgnc_neo_ops
;
522 brd
->bd_uart_offset
= 0x200;
523 brd
->bd_dividend
= 921600;
527 if (brd
->re_map_membase
) {
529 /* Read and store the dvid after remapping */
530 brd
->dvid
= readb(brd
->re_map_membase
+ 0x8D);
532 /* Get and store the board VPD, if it exists */
533 brd
->bd_ops
->vpd(brd
);
538 dev_err(&brd
->pdev
->dev
,
539 "Didn't find any compatible Neo/Classic PCI boards.\n");
545 * Do tty device initialization.
548 rc
= dgnc_tty_register(brd
);
550 pr_err(DRVSTR
": Can't register tty devices (%d)\n", rc
);
554 rc
= dgnc_finalize_board_init(brd
);
556 pr_err(DRVSTR
": Can't finalize board init (%d)\n", rc
);
560 rc
= dgnc_tty_init(brd
);
562 pr_err(DRVSTR
": Can't init tty devices (%d)\n", rc
);
566 brd
->state
= BOARD_READY
;
567 brd
->dpastatus
= BD_RUNNING
;
569 dgnc_create_ports_sysfiles(brd
);
571 /* init our poll helper tasklet */
572 tasklet_init(&brd
->helper_tasklet
,
573 brd
->bd_ops
->tasklet
,
574 (unsigned long) brd
);
576 spin_lock_irqsave(&dgnc_global_lock
, flags
);
578 dev_dbg(&brd
->pdev
->dev
, "%s\n", brd
->msgbuf_head
);
579 kfree(brd
->msgbuf_head
);
580 brd
->msgbuf_head
= NULL
;
581 spin_unlock_irqrestore(&dgnc_global_lock
, flags
);
583 wake_up_interruptible(&brd
->state_wait
);
588 dgnc_tty_uninit(brd
);
589 brd
->state
= BOARD_FAILED
;
590 brd
->dpastatus
= BD_NOFEP
;
597 static int dgnc_finalize_board_init(struct dgnc_board
*brd
)
601 if (!brd
|| brd
->magic
!= DGNC_BOARD_MAGIC
)
605 rc
= request_irq(brd
->irq
, brd
->bd_ops
->intr
,
606 IRQF_SHARED
, "DGNC", brd
);
609 dev_err(&brd
->pdev
->dev
,
610 "Failed to hook IRQ %d\n", brd
->irq
);
611 brd
->state
= BOARD_FAILED
;
612 brd
->dpastatus
= BD_NOFEP
;
622 static void dgnc_do_remap(struct dgnc_board
*brd
)
625 if (!brd
|| brd
->magic
!= DGNC_BOARD_MAGIC
)
628 brd
->re_map_membase
= ioremap(brd
->membase
, 0x1000);
632 /*****************************************************************************
652 * As each timer expires, it determines (a) whether the "transmit"
653 * waiter needs to be woken up, and (b) whether the poller needs to
656 ******************************************************************************/
658 static void dgnc_poll_handler(ulong dummy
)
660 struct dgnc_board
*brd
;
663 unsigned long new_time
;
665 /* Go thru each board, kicking off a tasklet for each if needed */
666 for (i
= 0; i
< dgnc_NumBoards
; i
++) {
669 spin_lock_irqsave(&brd
->bd_lock
, flags
);
671 /* If board is in a failed state don't schedule a tasklet */
672 if (brd
->state
== BOARD_FAILED
) {
673 spin_unlock_irqrestore(&brd
->bd_lock
, flags
);
677 /* Schedule a poll helper task */
678 tasklet_schedule(&brd
->helper_tasklet
);
680 spin_unlock_irqrestore(&brd
->bd_lock
, flags
);
684 * Schedule ourself back at the nominal wakeup interval.
686 spin_lock_irqsave(&dgnc_poll_lock
, flags
);
687 dgnc_poll_time
+= dgnc_jiffies_from_ms(dgnc_poll_tick
);
689 new_time
= dgnc_poll_time
- jiffies
;
691 if ((ulong
) new_time
>= 2 * dgnc_poll_tick
)
692 dgnc_poll_time
= jiffies
+ dgnc_jiffies_from_ms(dgnc_poll_tick
);
694 setup_timer(&dgnc_poll_timer
, dgnc_poll_handler
, 0);
695 dgnc_poll_timer
.expires
= dgnc_poll_time
;
696 spin_unlock_irqrestore(&dgnc_poll_lock
, flags
);
699 add_timer(&dgnc_poll_timer
);
703 * dgnc_init_globals()
705 * This is where we initialize the globals from the static insmod
706 * configuration variables. These are declared near the head of
709 static void dgnc_init_globals(void)
715 for (i
= 0; i
< MAXBOARDS
; i
++)
716 dgnc_Board
[i
] = NULL
;
718 init_timer(&dgnc_poll_timer
);