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;
360 dgnc_Board[brd->boardnum] = NULL;
369 * A board has been found, init it.
371 static int dgnc_found_board(struct pci_dev *pdev, int id)
373 struct dgnc_board *brd;
374 unsigned int pci_irq;
379 /* get the board structure and prep it */
380 dgnc_Board[dgnc_NumBoards] = kzalloc(sizeof(*brd), GFP_KERNEL);
381 brd = dgnc_Board[dgnc_NumBoards];
386 /* make a temporary message buffer for the boot messages */
387 brd->msgbuf_head = kcalloc(8192, sizeof(u8), GFP_KERNEL);
388 brd->msgbuf = brd->msgbuf_head;
395 /* store the info for the board we've found */
396 brd->magic = DGNC_BOARD_MAGIC;
397 brd->boardnum = dgnc_NumBoards;
398 brd->vendor = dgnc_pci_tbl[id].vendor;
399 brd->device = dgnc_pci_tbl[id].device;
401 brd->pci_bus = pdev->bus->number;
402 brd->pci_slot = PCI_SLOT(pdev->devfn);
403 brd->name = dgnc_Ids[id].name;
404 brd->maxports = dgnc_Ids[id].maxports;
405 if (dgnc_Ids[i].is_pci_express)
406 brd->bd_flags |= BD_IS_PCI_EXPRESS;
407 brd->dpastatus = BD_NOFEP;
408 init_waitqueue_head(&brd->state_wait);
410 spin_lock_init(&brd->bd_lock);
411 spin_lock_init(&brd->bd_intr_lock);
413 brd->state = BOARD_FOUND;
415 for (i = 0; i < MAXPORTS; i++)
416 brd->channels[i] = NULL;
418 /* store which card & revision we have */
419 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
420 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
421 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
427 switch (brd->device) {
429 case PCI_DEVICE_CLASSIC_4_DID:
430 case PCI_DEVICE_CLASSIC_8_DID:
431 case PCI_DEVICE_CLASSIC_4_422_DID:
432 case PCI_DEVICE_CLASSIC_8_422_DID:
434 brd->dpatype = T_CLASSIC | T_PCIBUS;
437 * For PCI ClassicBoards
438 * PCI Local Address (i.e. "resource" number) space
439 * 0 PLX Memory Mapped Config
440 * 1 PLX I/O Mapped Config
441 * 2 I/O Mapped UARTs and Status
442 * 3 Memory Mapped VPD
443 * 4 Memory Mapped UARTs and Status
447 /* get the PCI Base Address Registers */
448 brd->membase = pci_resource_start(pdev, 4);
451 dev_err(&brd->pdev->dev,
452 "Card has no PCI IO resources, failing.\n");
456 brd->membase_end = pci_resource_end(pdev, 4);
458 if (brd->membase & 1)
463 brd->iobase = pci_resource_start(pdev, 1);
464 brd->iobase_end = pci_resource_end(pdev, 1);
465 brd->iobase = ((unsigned int) (brd->iobase)) & 0xFFFE;
467 /* Assign the board_ops struct */
468 brd->bd_ops = &dgnc_cls_ops;
470 brd->bd_uart_offset = 0x8;
471 brd->bd_dividend = 921600;
475 /* Get and store the board VPD, if it exists */
476 brd->bd_ops->vpd(brd);
479 * Enable Local Interrupt 1 (0x1),
480 * Local Interrupt 1 Polarity Active high (0x2),
481 * Enable PCI interrupt (0x40)
483 outb(0x43, brd->iobase + 0x4c);
488 case PCI_DEVICE_NEO_4_DID:
489 case PCI_DEVICE_NEO_8_DID:
490 case PCI_DEVICE_NEO_2DB9_DID:
491 case PCI_DEVICE_NEO_2DB9PRI_DID:
492 case PCI_DEVICE_NEO_2RJ45_DID:
493 case PCI_DEVICE_NEO_2RJ45PRI_DID:
494 case PCI_DEVICE_NEO_1_422_DID:
495 case PCI_DEVICE_NEO_1_422_485_DID:
496 case PCI_DEVICE_NEO_2_422_485_DID:
497 case PCI_DEVICE_NEO_EXPRESS_8_DID:
498 case PCI_DEVICE_NEO_EXPRESS_4_DID:
499 case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID:
500 case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID:
503 * This chip is set up 100% when we get to it.
504 * No need to enable global interrupts or anything.
506 if (brd->bd_flags & BD_IS_PCI_EXPRESS)
507 brd->dpatype = T_NEO_EXPRESS | T_PCIBUS;
509 brd->dpatype = T_NEO | T_PCIBUS;
511 /* get the PCI Base Address Registers */
512 brd->membase = pci_resource_start(pdev, 0);
513 brd->membase_end = pci_resource_end(pdev, 0);
515 if (brd->membase & 1)
520 /* Assign the board_ops struct */
521 brd->bd_ops = &dgnc_neo_ops;
523 brd->bd_uart_offset = 0x200;
524 brd->bd_dividend = 921600;
528 if (brd->re_map_membase) {
530 /* Read and store the dvid after remapping */
531 brd->dvid = readb(brd->re_map_membase + 0x8D);
533 /* Get and store the board VPD, if it exists */
534 brd->bd_ops->vpd(brd);
539 dev_err(&brd->pdev->dev,
540 "Didn't find any compatible Neo/Classic PCI boards.\n");
546 * Do tty device initialization.
549 rc = dgnc_tty_register(brd);
551 pr_err(DRVSTR ": Can't register tty devices (%d)\n", rc);
555 rc = dgnc_finalize_board_init(brd);
557 pr_err(DRVSTR ": Can't finalize board init (%d)\n", rc);
561 rc = dgnc_tty_init(brd);
563 pr_err(DRVSTR ": Can't init tty devices (%d)\n", rc);
567 brd->state = BOARD_READY;
568 brd->dpastatus = BD_RUNNING;
570 dgnc_create_ports_sysfiles(brd);
572 /* init our poll helper tasklet */
573 tasklet_init(&brd->helper_tasklet,
574 brd->bd_ops->tasklet,
575 (unsigned long) brd);
577 spin_lock_irqsave(&dgnc_global_lock, flags);
579 dev_dbg(&brd->pdev->dev, "%s\n", brd->msgbuf_head);
580 kfree(brd->msgbuf_head);
581 brd->msgbuf_head = NULL;
582 spin_unlock_irqrestore(&dgnc_global_lock, flags);
585 * allocate flip buffer for board.
587 * Okay to malloc with GFP_KERNEL, we are not at interrupt
588 * context, and there are no locks held.
590 brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
592 wake_up_interruptible(&brd->state_wait);
597 dgnc_tty_uninit(brd);
598 brd->state = BOARD_FAILED;
599 brd->dpastatus = BD_NOFEP;
606 static int dgnc_finalize_board_init(struct dgnc_board *brd)
610 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
614 rc = request_irq(brd->irq, brd->bd_ops->intr,
615 IRQF_SHARED, "DGNC", brd);
618 dev_err(&brd->pdev->dev,
619 "Failed to hook IRQ %d\n", brd->irq);
620 brd->state = BOARD_FAILED;
621 brd->dpastatus = BD_NOFEP;
631 static void dgnc_do_remap(struct dgnc_board *brd)
634 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
637 brd->re_map_membase = ioremap(brd->membase, 0x1000);
641 /*****************************************************************************
661 * As each timer expires, it determines (a) whether the "transmit"
662 * waiter needs to be woken up, and (b) whether the poller needs to
665 ******************************************************************************/
667 static void dgnc_poll_handler(ulong dummy)
669 struct dgnc_board *brd;
672 unsigned long new_time;
674 /* Go thru each board, kicking off a tasklet for each if needed */
675 for (i = 0; i < dgnc_NumBoards; i++) {
678 spin_lock_irqsave(&brd->bd_lock, flags);
680 /* If board is in a failed state don't schedule a tasklet */
681 if (brd->state == BOARD_FAILED) {
682 spin_unlock_irqrestore(&brd->bd_lock, flags);
686 /* Schedule a poll helper task */
687 tasklet_schedule(&brd->helper_tasklet);
689 spin_unlock_irqrestore(&brd->bd_lock, flags);
693 * Schedule ourself back at the nominal wakeup interval.
695 spin_lock_irqsave(&dgnc_poll_lock, flags);
696 dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
698 new_time = dgnc_poll_time - jiffies;
700 if ((ulong) new_time >= 2 * dgnc_poll_tick)
701 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
703 setup_timer(&dgnc_poll_timer, dgnc_poll_handler, 0);
704 dgnc_poll_timer.expires = dgnc_poll_time;
705 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
708 add_timer(&dgnc_poll_timer);
712 * dgnc_init_globals()
714 * This is where we initialize the globals from the static insmod
715 * configuration variables. These are declared near the head of
718 static void dgnc_init_globals(void)
724 for (i = 0; i < MAXBOARDS; i++)
725 dgnc_Board[i] = NULL;
727 init_timer(&dgnc_poll_timer);