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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/pci.h>
36 #include <linux/slab.h>
37 #include <linux/sched.h>
38 #include "dgnc_driver.h"
40 #include "dpacompat.h"
41 #include "dgnc_mgmt.h"
45 #include "dgnc_sysfs.h"
47 MODULE_LICENSE("GPL");
48 MODULE_AUTHOR("Digi International, http://www.digi.com");
49 MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
50 MODULE_SUPPORTED_DEVICE("dgnc");
52 /**************************************************************************
54 * protos for this file
57 static int dgnc_start(void);
58 static int dgnc_finalize_board_init(struct dgnc_board *brd);
59 static void dgnc_init_globals(void);
60 static int dgnc_found_board(struct pci_dev *pdev, int id);
61 static void dgnc_cleanup_board(struct dgnc_board *brd);
62 static void dgnc_poll_handler(ulong dummy);
63 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
64 static void dgnc_do_remap(struct dgnc_board *brd);
67 * File operations permitted on Control/Management major.
69 static const struct file_operations dgnc_BoardFops = {
71 .unlocked_ioctl = dgnc_mgmt_ioctl,
72 .open = dgnc_mgmt_open,
73 .release = dgnc_mgmt_close
81 struct dgnc_board *dgnc_Board[MAXBOARDS];
82 DEFINE_SPINLOCK(dgnc_global_lock);
84 int dgnc_poll_tick = 20; /* Poll interval - 20 ms */
89 static struct class *dgnc_class;
94 static DEFINE_SPINLOCK(dgnc_poll_lock); /* Poll scheduling lock */
95 static ulong dgnc_poll_time; /* Time of next poll */
96 static uint dgnc_poll_stop; /* Used to tell poller to stop */
97 static struct timer_list dgnc_poll_timer;
100 static struct pci_device_id dgnc_pci_tbl[] = {
101 { DIGI_VID, PCI_DEVICE_CLASSIC_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
102 { DIGI_VID, PCI_DEVICE_CLASSIC_4_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
103 { DIGI_VID, PCI_DEVICE_CLASSIC_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
104 { DIGI_VID, PCI_DEVICE_CLASSIC_8_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
105 {0,} /* 0 terminated list. */
107 MODULE_DEVICE_TABLE(pci, dgnc_pci_tbl);
112 unsigned int is_pci_express;
115 static struct board_id dgnc_Ids[] = {
116 { PCI_DEVICE_CLASSIC_4_PCI_NAME, 4, 0 },
117 { PCI_DEVICE_CLASSIC_4_422_PCI_NAME, 4, 0 },
118 { PCI_DEVICE_CLASSIC_8_PCI_NAME, 8, 0 },
119 { PCI_DEVICE_CLASSIC_8_422_PCI_NAME, 8, 0 },
120 { PCI_DEVICE_NEO_4_PCI_NAME, 4, 0 },
121 { PCI_DEVICE_NEO_8_PCI_NAME, 8, 0 },
122 { PCI_DEVICE_NEO_2DB9_PCI_NAME, 2, 0 },
123 { PCI_DEVICE_NEO_2DB9PRI_PCI_NAME, 2, 0 },
124 { PCI_DEVICE_NEO_2RJ45_PCI_NAME, 2, 0 },
125 { PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME, 2, 0 },
126 { PCI_DEVICE_NEO_1_422_PCI_NAME, 1, 0 },
127 { PCI_DEVICE_NEO_1_422_485_PCI_NAME, 1, 0 },
128 { PCI_DEVICE_NEO_2_422_485_PCI_NAME, 2, 0 },
129 { PCI_DEVICE_NEO_EXPRESS_8_PCI_NAME, 8, 1 },
130 { PCI_DEVICE_NEO_EXPRESS_4_PCI_NAME, 4, 1 },
131 { PCI_DEVICE_NEO_EXPRESS_4RJ45_PCI_NAME, 4, 1 },
132 { PCI_DEVICE_NEO_EXPRESS_8RJ45_PCI_NAME, 8, 1 },
136 static struct pci_driver dgnc_driver = {
138 .probe = dgnc_init_one,
139 .id_table = dgnc_pci_tbl,
143 char *dgnc_state_text[] = {
150 /************************************************************************
152 * Driver load/unload functions
154 ************************************************************************/
157 * dgnc_cleanup_module()
159 * Module unload. This is where it all ends.
161 static void dgnc_cleanup_module(void)
166 spin_lock_irqsave(&dgnc_poll_lock, flags);
168 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
170 /* Turn off poller right away. */
171 del_timer_sync(&dgnc_poll_timer);
173 dgnc_remove_driver_sysfiles(&dgnc_driver);
175 device_destroy(dgnc_class, MKDEV(dgnc_Major, 0));
176 class_destroy(dgnc_class);
177 unregister_chrdev(dgnc_Major, "dgnc");
179 for (i = 0; i < dgnc_NumBoards; ++i) {
180 dgnc_remove_ports_sysfiles(dgnc_Board[i]);
181 dgnc_tty_uninit(dgnc_Board[i]);
182 dgnc_cleanup_board(dgnc_Board[i]);
185 dgnc_tty_post_uninit();
188 pci_unregister_driver(&dgnc_driver);
194 * Module load. This is where it all starts.
196 static int __init dgnc_init_module(void)
201 * Initialize global stuff
209 * Find and configure all the cards
211 rc = pci_register_driver(&dgnc_driver);
214 * If something went wrong in the scan, bail out of driver.
217 /* Only unregister the pci driver if it was actually registered. */
219 pci_unregister_driver(&dgnc_driver);
221 pr_warn("WARNING: dgnc driver load failed. No Digi Neo or Classic boards found.\n");
223 dgnc_cleanup_module();
225 dgnc_create_driver_sysfiles(&dgnc_driver);
231 module_init(dgnc_init_module);
232 module_exit(dgnc_cleanup_module);
237 static int dgnc_start(void)
242 /* make sure that the globals are init'd before we do anything else */
246 * Register our base character device into the kernel.
247 * This allows the download daemon to connect to the downld device
248 * before any of the boards are init'ed.
250 * Register management/dpa devices
252 rc = register_chrdev(0, "dgnc", &dgnc_BoardFops);
254 pr_err(DRVSTR ": Can't register dgnc driver device (%d)\n", rc);
259 dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
260 device_create(dgnc_class, NULL,
261 MKDEV(dgnc_Major, 0),
265 * Init any global tty stuff.
267 rc = dgnc_tty_preinit();
270 pr_err(DRVSTR ": tty preinit - not enough memory (%d)\n", rc);
274 /* Start the poller */
275 spin_lock_irqsave(&dgnc_poll_lock, flags);
276 init_timer(&dgnc_poll_timer);
277 dgnc_poll_timer.function = dgnc_poll_handler;
278 dgnc_poll_timer.data = 0;
279 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
280 dgnc_poll_timer.expires = dgnc_poll_time;
281 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
283 add_timer(&dgnc_poll_timer);
288 /* returns count (>= 0), or negative on error */
289 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
293 /* wake up and enable device */
294 rc = pci_enable_device(pdev);
299 rc = dgnc_found_board(pdev, ent->driver_data);
307 * dgnc_cleanup_board()
309 * Free all the memory associated with a board
311 static void dgnc_cleanup_board(struct dgnc_board *brd)
315 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
318 switch (brd->device) {
319 case PCI_DEVICE_CLASSIC_4_DID:
320 case PCI_DEVICE_CLASSIC_8_DID:
321 case PCI_DEVICE_CLASSIC_4_422_DID:
322 case PCI_DEVICE_CLASSIC_8_422_DID:
324 /* Tell card not to interrupt anymore. */
325 outb(0, brd->iobase + 0x4c);
333 free_irq(brd->irq, brd);
335 tasklet_kill(&brd->helper_tasklet);
337 if (brd->re_map_membase) {
338 iounmap(brd->re_map_membase);
339 brd->re_map_membase = NULL;
342 if (brd->msgbuf_head) {
345 spin_lock_irqsave(&dgnc_global_lock, flags);
347 printk("%s", brd->msgbuf_head);
348 kfree(brd->msgbuf_head);
349 brd->msgbuf_head = NULL;
350 spin_unlock_irqrestore(&dgnc_global_lock, flags);
353 /* Free all allocated channels structs */
354 for (i = 0; i < MAXPORTS ; i++) {
355 if (brd->channels[i]) {
356 kfree(brd->channels[i]->ch_rqueue);
357 kfree(brd->channels[i]->ch_equeue);
358 kfree(brd->channels[i]->ch_wqueue);
359 kfree(brd->channels[i]);
360 brd->channels[i] = NULL;
366 dgnc_Board[brd->boardnum] = NULL;
375 * A board has been found, init it.
377 static int dgnc_found_board(struct pci_dev *pdev, int id)
379 struct dgnc_board *brd;
380 unsigned int pci_irq;
385 /* get the board structure and prep it */
386 dgnc_Board[dgnc_NumBoards] = kzalloc(sizeof(*brd), GFP_KERNEL);
387 brd = dgnc_Board[dgnc_NumBoards];
392 /* make a temporary message buffer for the boot messages */
393 brd->msgbuf_head = kzalloc(sizeof(u8) * 8192, GFP_KERNEL);
394 brd->msgbuf = brd->msgbuf_head;
401 /* store the info for the board we've found */
402 brd->magic = DGNC_BOARD_MAGIC;
403 brd->boardnum = dgnc_NumBoards;
404 brd->vendor = dgnc_pci_tbl[id].vendor;
405 brd->device = dgnc_pci_tbl[id].device;
407 brd->pci_bus = pdev->bus->number;
408 brd->pci_slot = PCI_SLOT(pdev->devfn);
409 brd->name = dgnc_Ids[id].name;
410 brd->maxports = dgnc_Ids[id].maxports;
411 if (dgnc_Ids[i].is_pci_express)
412 brd->bd_flags |= BD_IS_PCI_EXPRESS;
413 brd->dpastatus = BD_NOFEP;
414 init_waitqueue_head(&brd->state_wait);
416 spin_lock_init(&brd->bd_lock);
417 spin_lock_init(&brd->bd_intr_lock);
419 brd->state = BOARD_FOUND;
421 for (i = 0; i < MAXPORTS; i++)
422 brd->channels[i] = NULL;
424 /* store which card & revision we have */
425 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
426 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
427 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
433 switch (brd->device) {
435 case PCI_DEVICE_CLASSIC_4_DID:
436 case PCI_DEVICE_CLASSIC_8_DID:
437 case PCI_DEVICE_CLASSIC_4_422_DID:
438 case PCI_DEVICE_CLASSIC_8_422_DID:
440 brd->dpatype = T_CLASSIC | T_PCIBUS;
443 * For PCI ClassicBoards
444 * PCI Local Address (i.e. "resource" number) space
445 * 0 PLX Memory Mapped Config
446 * 1 PLX I/O Mapped Config
447 * 2 I/O Mapped UARTs and Status
448 * 3 Memory Mapped VPD
449 * 4 Memory Mapped UARTs and Status
453 /* get the PCI Base Address Registers */
454 brd->membase = pci_resource_start(pdev, 4);
457 dev_err(&brd->pdev->dev,
458 "Card has no PCI IO resources, failing.\n");
462 brd->membase_end = pci_resource_end(pdev, 4);
464 if (brd->membase & 1)
469 brd->iobase = pci_resource_start(pdev, 1);
470 brd->iobase_end = pci_resource_end(pdev, 1);
471 brd->iobase = ((unsigned int) (brd->iobase)) & 0xFFFE;
473 /* Assign the board_ops struct */
474 brd->bd_ops = &dgnc_cls_ops;
476 brd->bd_uart_offset = 0x8;
477 brd->bd_dividend = 921600;
481 /* Get and store the board VPD, if it exists */
482 brd->bd_ops->vpd(brd);
485 * Enable Local Interrupt 1 (0x1),
486 * Local Interrupt 1 Polarity Active high (0x2),
487 * Enable PCI interrupt (0x40)
489 outb(0x43, brd->iobase + 0x4c);
494 case PCI_DEVICE_NEO_4_DID:
495 case PCI_DEVICE_NEO_8_DID:
496 case PCI_DEVICE_NEO_2DB9_DID:
497 case PCI_DEVICE_NEO_2DB9PRI_DID:
498 case PCI_DEVICE_NEO_2RJ45_DID:
499 case PCI_DEVICE_NEO_2RJ45PRI_DID:
500 case PCI_DEVICE_NEO_1_422_DID:
501 case PCI_DEVICE_NEO_1_422_485_DID:
502 case PCI_DEVICE_NEO_2_422_485_DID:
503 case PCI_DEVICE_NEO_EXPRESS_8_DID:
504 case PCI_DEVICE_NEO_EXPRESS_4_DID:
505 case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID:
506 case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID:
509 * This chip is set up 100% when we get to it.
510 * No need to enable global interrupts or anything.
512 if (brd->bd_flags & BD_IS_PCI_EXPRESS)
513 brd->dpatype = T_NEO_EXPRESS | T_PCIBUS;
515 brd->dpatype = T_NEO | T_PCIBUS;
517 /* get the PCI Base Address Registers */
518 brd->membase = pci_resource_start(pdev, 0);
519 brd->membase_end = pci_resource_end(pdev, 0);
521 if (brd->membase & 1)
526 /* Assign the board_ops struct */
527 brd->bd_ops = &dgnc_neo_ops;
529 brd->bd_uart_offset = 0x200;
530 brd->bd_dividend = 921600;
534 if (brd->re_map_membase) {
536 /* After remap is complete, we need to read and store the dvid */
537 brd->dvid = readb(brd->re_map_membase + 0x8D);
539 /* Get and store the board VPD, if it exists */
540 brd->bd_ops->vpd(brd);
545 dev_err(&brd->pdev->dev,
546 "Didn't find any compatible Neo/Classic PCI boards.\n");
552 * Do tty device initialization.
555 rc = dgnc_tty_register(brd);
557 dgnc_tty_uninit(brd);
558 pr_err(DRVSTR ": Can't register tty devices (%d)\n", rc);
559 brd->state = BOARD_FAILED;
560 brd->dpastatus = BD_NOFEP;
564 rc = dgnc_finalize_board_init(brd);
566 pr_err(DRVSTR ": Can't finalize board init (%d)\n", rc);
567 brd->state = BOARD_FAILED;
568 brd->dpastatus = BD_NOFEP;
573 rc = dgnc_tty_init(brd);
575 dgnc_tty_uninit(brd);
576 pr_err(DRVSTR ": Can't init tty devices (%d)\n", rc);
577 brd->state = BOARD_FAILED;
578 brd->dpastatus = BD_NOFEP;
583 brd->state = BOARD_READY;
584 brd->dpastatus = BD_RUNNING;
586 dgnc_create_ports_sysfiles(brd);
588 /* init our poll helper tasklet */
589 tasklet_init(&brd->helper_tasklet, brd->bd_ops->tasklet, (unsigned long) brd);
591 spin_lock_irqsave(&dgnc_global_lock, flags);
593 printk("%s", brd->msgbuf_head);
594 kfree(brd->msgbuf_head);
595 brd->msgbuf_head = NULL;
596 spin_unlock_irqrestore(&dgnc_global_lock, flags);
599 * allocate flip buffer for board.
601 * Okay to malloc with GFP_KERNEL, we are not at interrupt
602 * context, and there are no locks held.
604 brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
606 wake_up_interruptible(&brd->state_wait);
617 static int dgnc_finalize_board_init(struct dgnc_board *brd)
621 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
625 rc = request_irq(brd->irq, brd->bd_ops->intr,
626 IRQF_SHARED, "DGNC", brd);
629 dev_err(&brd->pdev->dev,
630 "Failed to hook IRQ %d\n", brd->irq);
631 brd->state = BOARD_FAILED;
632 brd->dpastatus = BD_NOFEP;
642 static void dgnc_do_remap(struct dgnc_board *brd)
645 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
648 brd->re_map_membase = ioremap(brd->membase, 0x1000);
652 /*****************************************************************************
672 * As each timer expires, it determines (a) whether the "transmit"
673 * waiter needs to be woken up, and (b) whether the poller needs to
676 ******************************************************************************/
678 static void dgnc_poll_handler(ulong dummy)
680 struct dgnc_board *brd;
683 unsigned long new_time;
685 /* Go thru each board, kicking off a tasklet for each if needed */
686 for (i = 0; i < dgnc_NumBoards; i++) {
689 spin_lock_irqsave(&brd->bd_lock, flags);
691 /* If board is in a failed state, don't bother scheduling a tasklet */
692 if (brd->state == BOARD_FAILED) {
693 spin_unlock_irqrestore(&brd->bd_lock, flags);
697 /* Schedule a poll helper task */
698 tasklet_schedule(&brd->helper_tasklet);
700 spin_unlock_irqrestore(&brd->bd_lock, flags);
704 * Schedule ourself back at the nominal wakeup interval.
706 spin_lock_irqsave(&dgnc_poll_lock, flags);
707 dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
709 new_time = dgnc_poll_time - jiffies;
711 if ((ulong) new_time >= 2 * dgnc_poll_tick)
712 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
714 init_timer(&dgnc_poll_timer);
715 dgnc_poll_timer.function = dgnc_poll_handler;
716 dgnc_poll_timer.data = 0;
717 dgnc_poll_timer.expires = dgnc_poll_time;
718 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
721 add_timer(&dgnc_poll_timer);
725 * dgnc_init_globals()
727 * This is where we initialize the globals from the static insmod
728 * configuration variables. These are declared near the head of
731 static void dgnc_init_globals(void)
737 for (i = 0; i < MAXBOARDS; i++)
738 dgnc_Board[i] = NULL;
740 init_timer(&dgnc_poll_timer);