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");
53 * insmod command line overrideable parameters
55 * NOTE: we use a set of macros to create the variables, which allows
56 * us to specify the variable type, name, initial value, and description.
58 PARM_INT(debug, 0x00, 0644, "Driver debugging level");
59 PARM_INT(rawreadok, 1, 0644, "Bypass flip buffers on input");
60 PARM_INT(trcbuf_size, 0x100000, 0644, "Debugging trace buffer size.");
62 /**************************************************************************
64 * protos for this file
67 static int dgnc_start(void);
68 static int dgnc_finalize_board_init(struct dgnc_board *brd);
69 static void dgnc_init_globals(void);
70 static int dgnc_found_board(struct pci_dev *pdev, int id);
71 static void dgnc_cleanup_board(struct dgnc_board *brd);
72 static void dgnc_poll_handler(ulong dummy);
73 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
74 static void dgnc_do_remap(struct dgnc_board *brd);
77 * File operations permitted on Control/Management major.
79 static const struct file_operations dgnc_BoardFops = {
81 .unlocked_ioctl = dgnc_mgmt_ioctl,
82 .open = dgnc_mgmt_open,
83 .release = dgnc_mgmt_close
91 struct dgnc_board *dgnc_Board[MAXBOARDS];
92 DEFINE_SPINLOCK(dgnc_global_lock);
94 int dgnc_poll_tick = 20; /* Poll interval - 20 ms */
99 static struct class *dgnc_class;
104 static DEFINE_SPINLOCK(dgnc_poll_lock); /* Poll scheduling lock */
105 static ulong dgnc_poll_time; /* Time of next poll */
106 static uint dgnc_poll_stop; /* Used to tell poller to stop */
107 static struct timer_list dgnc_poll_timer;
110 static struct pci_device_id dgnc_pci_tbl[] = {
111 { DIGI_VID, PCI_DEVICE_CLASSIC_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
112 { DIGI_VID, PCI_DEVICE_CLASSIC_4_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
113 { DIGI_VID, PCI_DEVICE_CLASSIC_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
114 { DIGI_VID, PCI_DEVICE_CLASSIC_8_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
115 {0,} /* 0 terminated list. */
117 MODULE_DEVICE_TABLE(pci, dgnc_pci_tbl);
122 unsigned int is_pci_express;
125 static struct board_id dgnc_Ids[] = {
126 { PCI_DEVICE_CLASSIC_4_PCI_NAME, 4, 0 },
127 { PCI_DEVICE_CLASSIC_4_422_PCI_NAME, 4, 0 },
128 { PCI_DEVICE_CLASSIC_8_PCI_NAME, 8, 0 },
129 { PCI_DEVICE_CLASSIC_8_422_PCI_NAME, 8, 0 },
130 { PCI_DEVICE_NEO_4_PCI_NAME, 4, 0 },
131 { PCI_DEVICE_NEO_8_PCI_NAME, 8, 0 },
132 { PCI_DEVICE_NEO_2DB9_PCI_NAME, 2, 0 },
133 { PCI_DEVICE_NEO_2DB9PRI_PCI_NAME, 2, 0 },
134 { PCI_DEVICE_NEO_2RJ45_PCI_NAME, 2, 0 },
135 { PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME, 2, 0 },
136 { PCI_DEVICE_NEO_1_422_PCI_NAME, 1, 0 },
137 { PCI_DEVICE_NEO_1_422_485_PCI_NAME, 1, 0 },
138 { PCI_DEVICE_NEO_2_422_485_PCI_NAME, 2, 0 },
139 { PCI_DEVICE_NEO_EXPRESS_8_PCI_NAME, 8, 1 },
140 { PCI_DEVICE_NEO_EXPRESS_4_PCI_NAME, 4, 1 },
141 { PCI_DEVICE_NEO_EXPRESS_4RJ45_PCI_NAME, 4, 1 },
142 { PCI_DEVICE_NEO_EXPRESS_8RJ45_PCI_NAME, 8, 1 },
146 static struct pci_driver dgnc_driver = {
148 .probe = dgnc_init_one,
149 .id_table = dgnc_pci_tbl,
153 char *dgnc_state_text[] = {
160 /************************************************************************
162 * Driver load/unload functions
164 ************************************************************************/
167 * dgnc_cleanup_module()
169 * Module unload. This is where it all ends.
171 static void dgnc_cleanup_module(void)
176 spin_lock_irqsave(&dgnc_poll_lock, flags);
178 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
180 /* Turn off poller right away. */
181 del_timer_sync(&dgnc_poll_timer);
183 dgnc_remove_driver_sysfiles(&dgnc_driver);
185 device_destroy(dgnc_class, MKDEV(dgnc_Major, 0));
186 class_destroy(dgnc_class);
187 unregister_chrdev(dgnc_Major, "dgnc");
189 for (i = 0; i < dgnc_NumBoards; ++i) {
190 dgnc_remove_ports_sysfiles(dgnc_Board[i]);
191 dgnc_tty_uninit(dgnc_Board[i]);
192 dgnc_cleanup_board(dgnc_Board[i]);
195 dgnc_tty_post_uninit();
198 pci_unregister_driver(&dgnc_driver);
204 * Module load. This is where it all starts.
206 static int __init dgnc_init_module(void)
210 APR(("%s, Digi International Part Number %s\n", DG_NAME, DG_PART));
213 * Initialize global stuff
221 * Find and configure all the cards
223 rc = pci_register_driver(&dgnc_driver);
226 * If something went wrong in the scan, bail out of driver.
229 /* Only unregister the pci driver if it was actually registered. */
231 pci_unregister_driver(&dgnc_driver);
233 pr_warn("WARNING: dgnc driver load failed. No Digi Neo or Classic boards found.\n");
235 dgnc_cleanup_module();
237 dgnc_create_driver_sysfiles(&dgnc_driver);
243 module_init(dgnc_init_module);
244 module_exit(dgnc_cleanup_module);
249 static int dgnc_start(void)
254 /* make sure that the globals are init'd before we do anything else */
257 APR(("For the tools package or updated drivers please visit http://www.digi.com\n"));
260 * Register our base character device into the kernel.
261 * This allows the download daemon to connect to the downld device
262 * before any of the boards are init'ed.
264 * Register management/dpa devices
266 rc = register_chrdev(0, "dgnc", &dgnc_BoardFops);
268 APR(("Can't register dgnc driver device (%d)\n", rc));
273 dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
274 device_create(dgnc_class, NULL,
275 MKDEV(dgnc_Major, 0),
279 * Init any global tty stuff.
281 rc = dgnc_tty_preinit();
284 APR(("tty preinit - not enough memory (%d)\n", rc));
288 /* Start the poller */
289 spin_lock_irqsave(&dgnc_poll_lock, flags);
290 init_timer(&dgnc_poll_timer);
291 dgnc_poll_timer.function = dgnc_poll_handler;
292 dgnc_poll_timer.data = 0;
293 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
294 dgnc_poll_timer.expires = dgnc_poll_time;
295 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
297 add_timer(&dgnc_poll_timer);
302 /* returns count (>= 0), or negative on error */
303 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
307 /* wake up and enable device */
308 rc = pci_enable_device(pdev);
313 rc = dgnc_found_board(pdev, ent->driver_data);
321 * dgnc_cleanup_board()
323 * Free all the memory associated with a board
325 static void dgnc_cleanup_board(struct dgnc_board *brd)
329 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
332 switch (brd->device) {
333 case PCI_DEVICE_CLASSIC_4_DID:
334 case PCI_DEVICE_CLASSIC_8_DID:
335 case PCI_DEVICE_CLASSIC_4_422_DID:
336 case PCI_DEVICE_CLASSIC_8_422_DID:
338 /* Tell card not to interrupt anymore. */
339 outb(0, brd->iobase + 0x4c);
347 free_irq(brd->irq, brd);
349 tasklet_kill(&brd->helper_tasklet);
351 if (brd->re_map_membase) {
352 iounmap(brd->re_map_membase);
353 brd->re_map_membase = NULL;
356 if (brd->msgbuf_head) {
359 spin_lock_irqsave(&dgnc_global_lock, flags);
361 printk("%s", brd->msgbuf_head);
362 kfree(brd->msgbuf_head);
363 brd->msgbuf_head = NULL;
364 spin_unlock_irqrestore(&dgnc_global_lock, flags);
367 /* Free all allocated channels structs */
368 for (i = 0; i < MAXPORTS ; i++) {
369 if (brd->channels[i]) {
370 kfree(brd->channels[i]->ch_rqueue);
371 kfree(brd->channels[i]->ch_equeue);
372 kfree(brd->channels[i]->ch_wqueue);
373 kfree(brd->channels[i]);
374 brd->channels[i] = NULL;
380 dgnc_Board[brd->boardnum] = NULL;
389 * A board has been found, init it.
391 static int dgnc_found_board(struct pci_dev *pdev, int id)
393 struct dgnc_board *brd;
394 unsigned int pci_irq;
399 /* get the board structure and prep it */
400 dgnc_Board[dgnc_NumBoards] = kzalloc(sizeof(*brd), GFP_KERNEL);
401 brd = dgnc_Board[dgnc_NumBoards];
406 /* make a temporary message buffer for the boot messages */
407 brd->msgbuf_head = kzalloc(sizeof(u8) * 8192, GFP_KERNEL);
408 brd->msgbuf = brd->msgbuf_head;
415 /* store the info for the board we've found */
416 brd->magic = DGNC_BOARD_MAGIC;
417 brd->boardnum = dgnc_NumBoards;
418 brd->vendor = dgnc_pci_tbl[id].vendor;
419 brd->device = dgnc_pci_tbl[id].device;
421 brd->pci_bus = pdev->bus->number;
422 brd->pci_slot = PCI_SLOT(pdev->devfn);
423 brd->name = dgnc_Ids[id].name;
424 brd->maxports = dgnc_Ids[id].maxports;
425 if (dgnc_Ids[i].is_pci_express)
426 brd->bd_flags |= BD_IS_PCI_EXPRESS;
427 brd->dpastatus = BD_NOFEP;
428 init_waitqueue_head(&brd->state_wait);
430 spin_lock_init(&brd->bd_lock);
431 spin_lock_init(&brd->bd_intr_lock);
433 brd->state = BOARD_FOUND;
435 for (i = 0; i < MAXPORTS; i++)
436 brd->channels[i] = NULL;
438 /* store which card & revision we have */
439 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
440 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
441 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
447 switch (brd->device) {
449 case PCI_DEVICE_CLASSIC_4_DID:
450 case PCI_DEVICE_CLASSIC_8_DID:
451 case PCI_DEVICE_CLASSIC_4_422_DID:
452 case PCI_DEVICE_CLASSIC_8_422_DID:
454 brd->dpatype = T_CLASSIC | T_PCIBUS;
457 * For PCI ClassicBoards
458 * PCI Local Address (i.e. "resource" number) space
459 * 0 PLX Memory Mapped Config
460 * 1 PLX I/O Mapped Config
461 * 2 I/O Mapped UARTs and Status
462 * 3 Memory Mapped VPD
463 * 4 Memory Mapped UARTs and Status
467 /* get the PCI Base Address Registers */
468 brd->membase = pci_resource_start(pdev, 4);
471 APR(("card has no PCI IO resources, failing board.\n"));
475 brd->membase_end = pci_resource_end(pdev, 4);
477 if (brd->membase & 1)
482 brd->iobase = pci_resource_start(pdev, 1);
483 brd->iobase_end = pci_resource_end(pdev, 1);
484 brd->iobase = ((unsigned int) (brd->iobase)) & 0xFFFE;
486 /* Assign the board_ops struct */
487 brd->bd_ops = &dgnc_cls_ops;
489 brd->bd_uart_offset = 0x8;
490 brd->bd_dividend = 921600;
494 /* Get and store the board VPD, if it exists */
495 brd->bd_ops->vpd(brd);
498 * Enable Local Interrupt 1 (0x1),
499 * Local Interrupt 1 Polarity Active high (0x2),
500 * Enable PCI interrupt (0x40)
502 outb(0x43, brd->iobase + 0x4c);
507 case PCI_DEVICE_NEO_4_DID:
508 case PCI_DEVICE_NEO_8_DID:
509 case PCI_DEVICE_NEO_2DB9_DID:
510 case PCI_DEVICE_NEO_2DB9PRI_DID:
511 case PCI_DEVICE_NEO_2RJ45_DID:
512 case PCI_DEVICE_NEO_2RJ45PRI_DID:
513 case PCI_DEVICE_NEO_1_422_DID:
514 case PCI_DEVICE_NEO_1_422_485_DID:
515 case PCI_DEVICE_NEO_2_422_485_DID:
516 case PCI_DEVICE_NEO_EXPRESS_8_DID:
517 case PCI_DEVICE_NEO_EXPRESS_4_DID:
518 case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID:
519 case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID:
522 * This chip is set up 100% when we get to it.
523 * No need to enable global interrupts or anything.
525 if (brd->bd_flags & BD_IS_PCI_EXPRESS)
526 brd->dpatype = T_NEO_EXPRESS | T_PCIBUS;
528 brd->dpatype = T_NEO | T_PCIBUS;
530 /* get the PCI Base Address Registers */
531 brd->membase = pci_resource_start(pdev, 0);
532 brd->membase_end = pci_resource_end(pdev, 0);
534 if (brd->membase & 1)
539 /* Assign the board_ops struct */
540 brd->bd_ops = &dgnc_neo_ops;
542 brd->bd_uart_offset = 0x200;
543 brd->bd_dividend = 921600;
547 if (brd->re_map_membase) {
549 /* After remap is complete, we need to read and store the dvid */
550 brd->dvid = readb(brd->re_map_membase + 0x8D);
552 /* Get and store the board VPD, if it exists */
553 brd->bd_ops->vpd(brd);
558 APR(("Did not find any compatible Neo or Classic PCI boards in system.\n"));
564 * Do tty device initialization.
567 rc = dgnc_tty_register(brd);
569 dgnc_tty_uninit(brd);
570 APR(("Can't register tty devices (%d)\n", rc));
571 brd->state = BOARD_FAILED;
572 brd->dpastatus = BD_NOFEP;
576 rc = dgnc_finalize_board_init(brd);
578 APR(("Can't finalize board init (%d)\n", rc));
579 brd->state = BOARD_FAILED;
580 brd->dpastatus = BD_NOFEP;
585 rc = dgnc_tty_init(brd);
587 dgnc_tty_uninit(brd);
588 APR(("Can't init tty devices (%d)\n", rc));
589 brd->state = BOARD_FAILED;
590 brd->dpastatus = BD_NOFEP;
595 brd->state = BOARD_READY;
596 brd->dpastatus = BD_RUNNING;
598 dgnc_create_ports_sysfiles(brd);
600 /* init our poll helper tasklet */
601 tasklet_init(&brd->helper_tasklet, brd->bd_ops->tasklet, (unsigned long) brd);
603 spin_lock_irqsave(&dgnc_global_lock, flags);
605 printk("%s", brd->msgbuf_head);
606 kfree(brd->msgbuf_head);
607 brd->msgbuf_head = NULL;
608 spin_unlock_irqrestore(&dgnc_global_lock, flags);
611 * allocate flip buffer for board.
613 * Okay to malloc with GFP_KERNEL, we are not at interrupt
614 * context, and there are no locks held.
616 brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
618 wake_up_interruptible(&brd->state_wait);
629 static int dgnc_finalize_board_init(struct dgnc_board *brd)
633 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
637 rc = request_irq(brd->irq, brd->bd_ops->intr,
638 IRQF_SHARED, "DGNC", brd);
641 dev_err(&brd->pdev->dev,
642 "Failed to hook IRQ %d\n", brd->irq);
643 brd->state = BOARD_FAILED;
644 brd->dpastatus = BD_NOFEP;
654 static void dgnc_do_remap(struct dgnc_board *brd)
657 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
660 brd->re_map_membase = ioremap(brd->membase, 0x1000);
664 /*****************************************************************************
684 * As each timer expires, it determines (a) whether the "transmit"
685 * waiter needs to be woken up, and (b) whether the poller needs to
688 ******************************************************************************/
690 static void dgnc_poll_handler(ulong dummy)
692 struct dgnc_board *brd;
695 unsigned long new_time;
697 /* Go thru each board, kicking off a tasklet for each if needed */
698 for (i = 0; i < dgnc_NumBoards; i++) {
701 spin_lock_irqsave(&brd->bd_lock, flags);
703 /* If board is in a failed state, don't bother scheduling a tasklet */
704 if (brd->state == BOARD_FAILED) {
705 spin_unlock_irqrestore(&brd->bd_lock, flags);
709 /* Schedule a poll helper task */
710 tasklet_schedule(&brd->helper_tasklet);
712 spin_unlock_irqrestore(&brd->bd_lock, flags);
716 * Schedule ourself back at the nominal wakeup interval.
718 spin_lock_irqsave(&dgnc_poll_lock, flags);
719 dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
721 new_time = dgnc_poll_time - jiffies;
723 if ((ulong) new_time >= 2 * dgnc_poll_tick)
724 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
726 init_timer(&dgnc_poll_timer);
727 dgnc_poll_timer.function = dgnc_poll_handler;
728 dgnc_poll_timer.data = 0;
729 dgnc_poll_timer.expires = dgnc_poll_time;
730 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
733 add_timer(&dgnc_poll_timer);
737 * dgnc_init_globals()
739 * This is where we initialize the globals from the static insmod
740 * configuration variables. These are declared near the head of
743 static void dgnc_init_globals(void)
747 dgnc_rawreadok = rawreadok;
748 dgnc_trcbuf_size = trcbuf_size;
752 for (i = 0; i < MAXBOARDS; i++)
753 dgnc_Board[i] = NULL;
755 init_timer(&dgnc_poll_timer);