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 const struct pci_device_id dgnc_pci_tbl[] = {
101 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_DID), .driver_data = 0},
102 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_422_DID), .driver_data = 1},
103 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_DID), .driver_data = 2},
104 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_422_DID), .driver_data = 3},
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)
243 /* make sure that the globals are init'd before we do anything else */
247 * Register our base character device into the kernel.
248 * This allows the download daemon to connect to the downld device
249 * before any of the boards are init'ed.
251 * Register management/dpa devices
253 rc = register_chrdev(0, "dgnc", &dgnc_BoardFops);
255 pr_err(DRVSTR ": Can't register dgnc driver device (%d)\n", rc);
260 dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
261 if (IS_ERR(dgnc_class)) {
262 rc = PTR_ERR(dgnc_class);
263 pr_err(DRVSTR ": Can't create dgnc_mgmt class (%d)\n", rc);
267 dev = device_create(dgnc_class, NULL,
268 MKDEV(dgnc_Major, 0),
272 pr_err(DRVSTR ": Can't create device (%d)\n", rc);
277 * Init any global tty stuff.
279 rc = dgnc_tty_preinit();
282 pr_err(DRVSTR ": tty preinit - not enough memory (%d)\n", rc);
286 /* Start the poller */
287 spin_lock_irqsave(&dgnc_poll_lock, flags);
288 init_timer(&dgnc_poll_timer);
289 dgnc_poll_timer.function = dgnc_poll_handler;
290 dgnc_poll_timer.data = 0;
291 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
292 dgnc_poll_timer.expires = dgnc_poll_time;
293 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
295 add_timer(&dgnc_poll_timer);
300 device_destroy(dgnc_class, MKDEV(dgnc_Major, 0));
302 class_destroy(dgnc_class);
304 unregister_chrdev(dgnc_Major, "dgnc");
308 /* returns count (>= 0), or negative on error */
309 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
313 /* wake up and enable device */
314 rc = pci_enable_device(pdev);
319 rc = dgnc_found_board(pdev, ent->driver_data);
327 * dgnc_cleanup_board()
329 * Free all the memory associated with a board
331 static void dgnc_cleanup_board(struct dgnc_board *brd)
335 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
338 switch (brd->device) {
339 case PCI_DEVICE_CLASSIC_4_DID:
340 case PCI_DEVICE_CLASSIC_8_DID:
341 case PCI_DEVICE_CLASSIC_4_422_DID:
342 case PCI_DEVICE_CLASSIC_8_422_DID:
344 /* Tell card not to interrupt anymore. */
345 outb(0, brd->iobase + 0x4c);
353 free_irq(brd->irq, brd);
355 tasklet_kill(&brd->helper_tasklet);
357 if (brd->re_map_membase) {
358 iounmap(brd->re_map_membase);
359 brd->re_map_membase = NULL;
362 if (brd->msgbuf_head) {
365 spin_lock_irqsave(&dgnc_global_lock, flags);
367 printk("%s", brd->msgbuf_head);
368 kfree(brd->msgbuf_head);
369 brd->msgbuf_head = NULL;
370 spin_unlock_irqrestore(&dgnc_global_lock, flags);
373 /* Free all allocated channels structs */
374 for (i = 0; i < MAXPORTS ; i++) {
375 if (brd->channels[i]) {
376 kfree(brd->channels[i]->ch_rqueue);
377 kfree(brd->channels[i]->ch_equeue);
378 kfree(brd->channels[i]->ch_wqueue);
379 kfree(brd->channels[i]);
380 brd->channels[i] = NULL;
386 dgnc_Board[brd->boardnum] = NULL;
395 * A board has been found, init it.
397 static int dgnc_found_board(struct pci_dev *pdev, int id)
399 struct dgnc_board *brd;
400 unsigned int pci_irq;
405 /* get the board structure and prep it */
406 dgnc_Board[dgnc_NumBoards] = kzalloc(sizeof(*brd), GFP_KERNEL);
407 brd = dgnc_Board[dgnc_NumBoards];
412 /* make a temporary message buffer for the boot messages */
413 brd->msgbuf_head = kzalloc(sizeof(u8) * 8192, GFP_KERNEL);
414 brd->msgbuf = brd->msgbuf_head;
421 /* store the info for the board we've found */
422 brd->magic = DGNC_BOARD_MAGIC;
423 brd->boardnum = dgnc_NumBoards;
424 brd->vendor = dgnc_pci_tbl[id].vendor;
425 brd->device = dgnc_pci_tbl[id].device;
427 brd->pci_bus = pdev->bus->number;
428 brd->pci_slot = PCI_SLOT(pdev->devfn);
429 brd->name = dgnc_Ids[id].name;
430 brd->maxports = dgnc_Ids[id].maxports;
431 if (dgnc_Ids[i].is_pci_express)
432 brd->bd_flags |= BD_IS_PCI_EXPRESS;
433 brd->dpastatus = BD_NOFEP;
434 init_waitqueue_head(&brd->state_wait);
436 spin_lock_init(&brd->bd_lock);
437 spin_lock_init(&brd->bd_intr_lock);
439 brd->state = BOARD_FOUND;
441 for (i = 0; i < MAXPORTS; i++)
442 brd->channels[i] = NULL;
444 /* store which card & revision we have */
445 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
446 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
447 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
453 switch (brd->device) {
455 case PCI_DEVICE_CLASSIC_4_DID:
456 case PCI_DEVICE_CLASSIC_8_DID:
457 case PCI_DEVICE_CLASSIC_4_422_DID:
458 case PCI_DEVICE_CLASSIC_8_422_DID:
460 brd->dpatype = T_CLASSIC | T_PCIBUS;
463 * For PCI ClassicBoards
464 * PCI Local Address (i.e. "resource" number) space
465 * 0 PLX Memory Mapped Config
466 * 1 PLX I/O Mapped Config
467 * 2 I/O Mapped UARTs and Status
468 * 3 Memory Mapped VPD
469 * 4 Memory Mapped UARTs and Status
473 /* get the PCI Base Address Registers */
474 brd->membase = pci_resource_start(pdev, 4);
477 dev_err(&brd->pdev->dev,
478 "Card has no PCI IO resources, failing.\n");
482 brd->membase_end = pci_resource_end(pdev, 4);
484 if (brd->membase & 1)
489 brd->iobase = pci_resource_start(pdev, 1);
490 brd->iobase_end = pci_resource_end(pdev, 1);
491 brd->iobase = ((unsigned int) (brd->iobase)) & 0xFFFE;
493 /* Assign the board_ops struct */
494 brd->bd_ops = &dgnc_cls_ops;
496 brd->bd_uart_offset = 0x8;
497 brd->bd_dividend = 921600;
501 /* Get and store the board VPD, if it exists */
502 brd->bd_ops->vpd(brd);
505 * Enable Local Interrupt 1 (0x1),
506 * Local Interrupt 1 Polarity Active high (0x2),
507 * Enable PCI interrupt (0x40)
509 outb(0x43, brd->iobase + 0x4c);
514 case PCI_DEVICE_NEO_4_DID:
515 case PCI_DEVICE_NEO_8_DID:
516 case PCI_DEVICE_NEO_2DB9_DID:
517 case PCI_DEVICE_NEO_2DB9PRI_DID:
518 case PCI_DEVICE_NEO_2RJ45_DID:
519 case PCI_DEVICE_NEO_2RJ45PRI_DID:
520 case PCI_DEVICE_NEO_1_422_DID:
521 case PCI_DEVICE_NEO_1_422_485_DID:
522 case PCI_DEVICE_NEO_2_422_485_DID:
523 case PCI_DEVICE_NEO_EXPRESS_8_DID:
524 case PCI_DEVICE_NEO_EXPRESS_4_DID:
525 case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID:
526 case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID:
529 * This chip is set up 100% when we get to it.
530 * No need to enable global interrupts or anything.
532 if (brd->bd_flags & BD_IS_PCI_EXPRESS)
533 brd->dpatype = T_NEO_EXPRESS | T_PCIBUS;
535 brd->dpatype = T_NEO | T_PCIBUS;
537 /* get the PCI Base Address Registers */
538 brd->membase = pci_resource_start(pdev, 0);
539 brd->membase_end = pci_resource_end(pdev, 0);
541 if (brd->membase & 1)
546 /* Assign the board_ops struct */
547 brd->bd_ops = &dgnc_neo_ops;
549 brd->bd_uart_offset = 0x200;
550 brd->bd_dividend = 921600;
554 if (brd->re_map_membase) {
556 /* After remap is complete, we need to read and store the dvid */
557 brd->dvid = readb(brd->re_map_membase + 0x8D);
559 /* Get and store the board VPD, if it exists */
560 brd->bd_ops->vpd(brd);
565 dev_err(&brd->pdev->dev,
566 "Didn't find any compatible Neo/Classic PCI boards.\n");
572 * Do tty device initialization.
575 rc = dgnc_tty_register(brd);
577 dgnc_tty_uninit(brd);
578 pr_err(DRVSTR ": Can't register tty devices (%d)\n", rc);
579 brd->state = BOARD_FAILED;
580 brd->dpastatus = BD_NOFEP;
584 rc = dgnc_finalize_board_init(brd);
586 pr_err(DRVSTR ": Can't finalize board init (%d)\n", rc);
587 brd->state = BOARD_FAILED;
588 brd->dpastatus = BD_NOFEP;
593 rc = dgnc_tty_init(brd);
595 dgnc_tty_uninit(brd);
596 pr_err(DRVSTR ": Can't init tty devices (%d)\n", rc);
597 brd->state = BOARD_FAILED;
598 brd->dpastatus = BD_NOFEP;
603 brd->state = BOARD_READY;
604 brd->dpastatus = BD_RUNNING;
606 dgnc_create_ports_sysfiles(brd);
608 /* init our poll helper tasklet */
609 tasklet_init(&brd->helper_tasklet, brd->bd_ops->tasklet, (unsigned long) brd);
611 spin_lock_irqsave(&dgnc_global_lock, flags);
613 printk("%s", brd->msgbuf_head);
614 kfree(brd->msgbuf_head);
615 brd->msgbuf_head = NULL;
616 spin_unlock_irqrestore(&dgnc_global_lock, flags);
619 * allocate flip buffer for board.
621 * Okay to malloc with GFP_KERNEL, we are not at interrupt
622 * context, and there are no locks held.
624 brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
626 wake_up_interruptible(&brd->state_wait);
637 static int dgnc_finalize_board_init(struct dgnc_board *brd)
641 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
645 rc = request_irq(brd->irq, brd->bd_ops->intr,
646 IRQF_SHARED, "DGNC", brd);
649 dev_err(&brd->pdev->dev,
650 "Failed to hook IRQ %d\n", brd->irq);
651 brd->state = BOARD_FAILED;
652 brd->dpastatus = BD_NOFEP;
662 static void dgnc_do_remap(struct dgnc_board *brd)
665 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
668 brd->re_map_membase = ioremap(brd->membase, 0x1000);
672 /*****************************************************************************
692 * As each timer expires, it determines (a) whether the "transmit"
693 * waiter needs to be woken up, and (b) whether the poller needs to
696 ******************************************************************************/
698 static void dgnc_poll_handler(ulong dummy)
700 struct dgnc_board *brd;
703 unsigned long new_time;
705 /* Go thru each board, kicking off a tasklet for each if needed */
706 for (i = 0; i < dgnc_NumBoards; i++) {
709 spin_lock_irqsave(&brd->bd_lock, flags);
711 /* If board is in a failed state, don't bother scheduling a tasklet */
712 if (brd->state == BOARD_FAILED) {
713 spin_unlock_irqrestore(&brd->bd_lock, flags);
717 /* Schedule a poll helper task */
718 tasklet_schedule(&brd->helper_tasklet);
720 spin_unlock_irqrestore(&brd->bd_lock, flags);
724 * Schedule ourself back at the nominal wakeup interval.
726 spin_lock_irqsave(&dgnc_poll_lock, flags);
727 dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
729 new_time = dgnc_poll_time - jiffies;
731 if ((ulong) new_time >= 2 * dgnc_poll_tick)
732 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
734 init_timer(&dgnc_poll_timer);
735 dgnc_poll_timer.function = dgnc_poll_handler;
736 dgnc_poll_timer.data = 0;
737 dgnc_poll_timer.expires = dgnc_poll_time;
738 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
741 add_timer(&dgnc_poll_timer);
745 * dgnc_init_globals()
747 * This is where we initialize the globals from the static insmod
748 * configuration variables. These are declared near the head of
751 static void dgnc_init_globals(void)
757 for (i = 0; i < MAXBOARDS; i++)
758 dgnc_Board[i] = NULL;
760 init_timer(&dgnc_poll_timer);