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.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include "dgnc_driver.h"
23 #include "dgnc_mgmt.h"
27 #include "dgnc_sysfs.h"
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Digi International, http://www.digi.com");
31 MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
32 MODULE_SUPPORTED_DEVICE("dgnc");
34 /**************************************************************************
36 * protos for this file
39 static int dgnc_start(void);
40 static int dgnc_finalize_board_init(struct dgnc_board *brd);
41 static int dgnc_found_board(struct pci_dev *pdev, int id);
42 static void dgnc_cleanup_board(struct dgnc_board *brd);
43 static void dgnc_poll_handler(ulong dummy);
44 static int dgnc_init_one(struct pci_dev *pdev,
45 const struct pci_device_id *ent);
46 static void dgnc_do_remap(struct dgnc_board *brd);
49 * File operations permitted on Control/Management major.
51 static const struct file_operations dgnc_BoardFops = {
53 .unlocked_ioctl = dgnc_mgmt_ioctl,
54 .open = dgnc_mgmt_open,
55 .release = dgnc_mgmt_close
62 struct dgnc_board *dgnc_Board[MAXBOARDS];
63 DEFINE_SPINLOCK(dgnc_global_lock);
64 DEFINE_SPINLOCK(dgnc_poll_lock); /* Poll scheduling lock */
66 int dgnc_poll_tick = 20; /* Poll interval - 20 ms */
71 static struct class *dgnc_class;
76 static ulong dgnc_poll_time; /* Time of next poll */
77 static uint dgnc_poll_stop; /* Used to tell poller to stop */
78 static struct timer_list dgnc_poll_timer;
80 static const struct pci_device_id dgnc_pci_tbl[] = {
81 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_DID), .driver_data = 0},
82 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_4_422_DID), .driver_data = 1},
83 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_DID), .driver_data = 2},
84 {PCI_DEVICE(DIGI_VID, PCI_DEVICE_CLASSIC_8_422_DID), .driver_data = 3},
87 MODULE_DEVICE_TABLE(pci, dgnc_pci_tbl);
92 unsigned int is_pci_express;
95 static struct board_id dgnc_Ids[] = {
96 { PCI_DEVICE_CLASSIC_4_PCI_NAME, 4, 0 },
97 { PCI_DEVICE_CLASSIC_4_422_PCI_NAME, 4, 0 },
98 { PCI_DEVICE_CLASSIC_8_PCI_NAME, 8, 0 },
99 { PCI_DEVICE_CLASSIC_8_422_PCI_NAME, 8, 0 },
100 { PCI_DEVICE_NEO_4_PCI_NAME, 4, 0 },
101 { PCI_DEVICE_NEO_8_PCI_NAME, 8, 0 },
102 { PCI_DEVICE_NEO_2DB9_PCI_NAME, 2, 0 },
103 { PCI_DEVICE_NEO_2DB9PRI_PCI_NAME, 2, 0 },
104 { PCI_DEVICE_NEO_2RJ45_PCI_NAME, 2, 0 },
105 { PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME, 2, 0 },
106 { PCI_DEVICE_NEO_1_422_PCI_NAME, 1, 0 },
107 { PCI_DEVICE_NEO_1_422_485_PCI_NAME, 1, 0 },
108 { PCI_DEVICE_NEO_2_422_485_PCI_NAME, 2, 0 },
109 { PCI_DEVICE_NEO_EXPRESS_8_PCI_NAME, 8, 1 },
110 { PCI_DEVICE_NEO_EXPRESS_4_PCI_NAME, 4, 1 },
111 { PCI_DEVICE_NEO_EXPRESS_4RJ45_PCI_NAME, 4, 1 },
112 { PCI_DEVICE_NEO_EXPRESS_8RJ45_PCI_NAME, 8, 1 },
116 static struct pci_driver dgnc_driver = {
118 .probe = dgnc_init_one,
119 .id_table = dgnc_pci_tbl,
122 /************************************************************************
124 * Driver load/unload functions
126 ************************************************************************/
129 * dgnc_cleanup_module()
131 * Module unload. This is where it all ends.
133 static void dgnc_cleanup_module(void)
138 spin_lock_irqsave(&dgnc_poll_lock, flags);
140 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
142 /* Turn off poller right away. */
143 del_timer_sync(&dgnc_poll_timer);
145 dgnc_remove_driver_sysfiles(&dgnc_driver);
147 device_destroy(dgnc_class, MKDEV(dgnc_Major, 0));
148 class_destroy(dgnc_class);
149 unregister_chrdev(dgnc_Major, "dgnc");
151 for (i = 0; i < dgnc_NumBoards; ++i) {
152 dgnc_remove_ports_sysfiles(dgnc_Board[i]);
153 dgnc_tty_uninit(dgnc_Board[i]);
154 dgnc_cleanup_board(dgnc_Board[i]);
157 dgnc_tty_post_uninit();
160 pci_unregister_driver(&dgnc_driver);
166 * Module load. This is where it all starts.
168 static int __init dgnc_init_module(void)
173 * Initialize global stuff
181 * Find and configure all the cards
183 rc = pci_register_driver(&dgnc_driver);
185 pr_warn("WARNING: dgnc driver load failed. No Digi Neo or Classic boards found.\n");
186 dgnc_cleanup_module();
189 dgnc_create_driver_sysfiles(&dgnc_driver);
194 module_init(dgnc_init_module);
195 module_exit(dgnc_cleanup_module);
200 static int dgnc_start(void)
206 /* make sure timer is initialized before we do anything else */
207 init_timer(&dgnc_poll_timer);
210 * Register our base character device into the kernel.
211 * This allows the download daemon to connect to the downld device
212 * before any of the boards are init'ed.
214 * Register management/dpa devices
216 rc = register_chrdev(0, "dgnc", &dgnc_BoardFops);
218 pr_err(DRVSTR ": Can't register dgnc driver device (%d)\n", rc);
223 dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
224 if (IS_ERR(dgnc_class)) {
225 rc = PTR_ERR(dgnc_class);
226 pr_err(DRVSTR ": Can't create dgnc_mgmt class (%d)\n", rc);
230 dev = device_create(dgnc_class, NULL,
231 MKDEV(dgnc_Major, 0),
235 pr_err(DRVSTR ": Can't create device (%d)\n", rc);
240 * Init any global tty stuff.
242 rc = dgnc_tty_preinit();
245 pr_err(DRVSTR ": tty preinit - not enough memory (%d)\n", rc);
249 /* Start the poller */
250 spin_lock_irqsave(&dgnc_poll_lock, flags);
251 setup_timer(&dgnc_poll_timer, dgnc_poll_handler, 0);
252 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
253 dgnc_poll_timer.expires = dgnc_poll_time;
254 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
256 add_timer(&dgnc_poll_timer);
261 device_destroy(dgnc_class, MKDEV(dgnc_Major, 0));
263 class_destroy(dgnc_class);
265 unregister_chrdev(dgnc_Major, "dgnc");
269 /* returns count (>= 0), or negative on error */
270 static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
274 /* wake up and enable device */
275 rc = pci_enable_device(pdev);
280 rc = dgnc_found_board(pdev, ent->driver_data);
288 * dgnc_cleanup_board()
290 * Free all the memory associated with a board
292 static void dgnc_cleanup_board(struct dgnc_board *brd)
296 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
299 switch (brd->device) {
300 case PCI_DEVICE_CLASSIC_4_DID:
301 case PCI_DEVICE_CLASSIC_8_DID:
302 case PCI_DEVICE_CLASSIC_4_422_DID:
303 case PCI_DEVICE_CLASSIC_8_422_DID:
305 /* Tell card not to interrupt anymore. */
306 outb(0, brd->iobase + 0x4c);
314 free_irq(brd->irq, brd);
316 tasklet_kill(&brd->helper_tasklet);
318 if (brd->re_map_membase) {
319 iounmap(brd->re_map_membase);
320 brd->re_map_membase = NULL;
323 if (brd->msgbuf_head) {
326 spin_lock_irqsave(&dgnc_global_lock, flags);
328 dev_dbg(&brd->pdev->dev, "%s\n", brd->msgbuf_head);
329 kfree(brd->msgbuf_head);
330 brd->msgbuf_head = NULL;
331 spin_unlock_irqrestore(&dgnc_global_lock, flags);
334 /* Free all allocated channels structs */
335 for (i = 0; i < MAXPORTS ; i++) {
336 if (brd->channels[i]) {
337 kfree(brd->channels[i]->ch_rqueue);
338 kfree(brd->channels[i]->ch_equeue);
339 kfree(brd->channels[i]->ch_wqueue);
340 kfree(brd->channels[i]);
341 brd->channels[i] = NULL;
345 dgnc_Board[brd->boardnum] = NULL;
353 * A board has been found, init it.
355 static int dgnc_found_board(struct pci_dev *pdev, int id)
357 struct dgnc_board *brd;
358 unsigned int pci_irq;
363 /* get the board structure and prep it */
364 dgnc_Board[dgnc_NumBoards] = kzalloc(sizeof(*brd), GFP_KERNEL);
365 brd = dgnc_Board[dgnc_NumBoards];
370 /* make a temporary message buffer for the boot messages */
371 brd->msgbuf_head = kcalloc(8192, sizeof(u8), GFP_KERNEL);
372 brd->msgbuf = brd->msgbuf_head;
379 /* store the info for the board we've found */
380 brd->magic = DGNC_BOARD_MAGIC;
381 brd->boardnum = dgnc_NumBoards;
382 brd->vendor = dgnc_pci_tbl[id].vendor;
383 brd->device = dgnc_pci_tbl[id].device;
385 brd->pci_bus = pdev->bus->number;
386 brd->pci_slot = PCI_SLOT(pdev->devfn);
387 brd->name = dgnc_Ids[id].name;
388 brd->maxports = dgnc_Ids[id].maxports;
389 if (dgnc_Ids[i].is_pci_express)
390 brd->bd_flags |= BD_IS_PCI_EXPRESS;
391 brd->dpastatus = BD_NOFEP;
392 init_waitqueue_head(&brd->state_wait);
394 spin_lock_init(&brd->bd_lock);
395 spin_lock_init(&brd->bd_intr_lock);
397 brd->state = BOARD_FOUND;
399 for (i = 0; i < MAXPORTS; i++)
400 brd->channels[i] = NULL;
402 /* store which card & revision we have */
403 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
404 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
405 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
410 switch (brd->device) {
411 case PCI_DEVICE_CLASSIC_4_DID:
412 case PCI_DEVICE_CLASSIC_8_DID:
413 case PCI_DEVICE_CLASSIC_4_422_DID:
414 case PCI_DEVICE_CLASSIC_8_422_DID:
416 brd->dpatype = T_CLASSIC | T_PCIBUS;
419 * For PCI ClassicBoards
420 * PCI Local Address (i.e. "resource" number) space
421 * 0 PLX Memory Mapped Config
422 * 1 PLX I/O Mapped Config
423 * 2 I/O Mapped UARTs and Status
424 * 3 Memory Mapped VPD
425 * 4 Memory Mapped UARTs and Status
428 /* get the PCI Base Address Registers */
429 brd->membase = pci_resource_start(pdev, 4);
432 dev_err(&brd->pdev->dev,
433 "Card has no PCI IO resources, failing.\n");
437 brd->membase_end = pci_resource_end(pdev, 4);
439 if (brd->membase & 1)
444 brd->iobase = pci_resource_start(pdev, 1);
445 brd->iobase_end = pci_resource_end(pdev, 1);
446 brd->iobase = ((unsigned int)(brd->iobase)) & 0xFFFE;
448 /* Assign the board_ops struct */
449 brd->bd_ops = &dgnc_cls_ops;
451 brd->bd_uart_offset = 0x8;
452 brd->bd_dividend = 921600;
456 /* Get and store the board VPD, if it exists */
457 brd->bd_ops->vpd(brd);
460 * Enable Local Interrupt 1 (0x1),
461 * Local Interrupt 1 Polarity Active high (0x2),
462 * Enable PCI interrupt (0x40)
464 outb(0x43, brd->iobase + 0x4c);
468 case PCI_DEVICE_NEO_4_DID:
469 case PCI_DEVICE_NEO_8_DID:
470 case PCI_DEVICE_NEO_2DB9_DID:
471 case PCI_DEVICE_NEO_2DB9PRI_DID:
472 case PCI_DEVICE_NEO_2RJ45_DID:
473 case PCI_DEVICE_NEO_2RJ45PRI_DID:
474 case PCI_DEVICE_NEO_1_422_DID:
475 case PCI_DEVICE_NEO_1_422_485_DID:
476 case PCI_DEVICE_NEO_2_422_485_DID:
477 case PCI_DEVICE_NEO_EXPRESS_8_DID:
478 case PCI_DEVICE_NEO_EXPRESS_4_DID:
479 case PCI_DEVICE_NEO_EXPRESS_4RJ45_DID:
480 case PCI_DEVICE_NEO_EXPRESS_8RJ45_DID:
483 * This chip is set up 100% when we get to it.
484 * No need to enable global interrupts or anything.
486 if (brd->bd_flags & BD_IS_PCI_EXPRESS)
487 brd->dpatype = T_NEO_EXPRESS | T_PCIBUS;
489 brd->dpatype = T_NEO | T_PCIBUS;
491 /* get the PCI Base Address Registers */
492 brd->membase = pci_resource_start(pdev, 0);
493 brd->membase_end = pci_resource_end(pdev, 0);
495 if (brd->membase & 1)
500 /* Assign the board_ops struct */
501 brd->bd_ops = &dgnc_neo_ops;
503 brd->bd_uart_offset = 0x200;
504 brd->bd_dividend = 921600;
508 if (brd->re_map_membase) {
509 /* Read and store the dvid after remapping */
510 brd->dvid = readb(brd->re_map_membase + 0x8D);
512 /* Get and store the board VPD, if it exists */
513 brd->bd_ops->vpd(brd);
518 dev_err(&brd->pdev->dev,
519 "Didn't find any compatible Neo/Classic PCI boards.\n");
524 * Do tty device initialization.
527 rc = dgnc_tty_register(brd);
529 pr_err(DRVSTR ": Can't register tty devices (%d)\n", rc);
533 rc = dgnc_finalize_board_init(brd);
535 pr_err(DRVSTR ": Can't finalize board init (%d)\n", rc);
539 rc = dgnc_tty_init(brd);
541 pr_err(DRVSTR ": Can't init tty devices (%d)\n", rc);
545 brd->state = BOARD_READY;
546 brd->dpastatus = BD_RUNNING;
548 dgnc_create_ports_sysfiles(brd);
550 /* init our poll helper tasklet */
551 tasklet_init(&brd->helper_tasklet,
552 brd->bd_ops->tasklet,
555 spin_lock_irqsave(&dgnc_global_lock, flags);
557 dev_dbg(&brd->pdev->dev, "%s\n", brd->msgbuf_head);
558 kfree(brd->msgbuf_head);
559 brd->msgbuf_head = NULL;
560 spin_unlock_irqrestore(&dgnc_global_lock, flags);
562 wake_up_interruptible(&brd->state_wait);
567 dgnc_tty_uninit(brd);
568 brd->state = BOARD_FAILED;
569 brd->dpastatus = BD_NOFEP;
574 static int dgnc_finalize_board_init(struct dgnc_board *brd)
578 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
582 rc = request_irq(brd->irq, brd->bd_ops->intr,
583 IRQF_SHARED, "DGNC", brd);
586 dev_err(&brd->pdev->dev,
587 "Failed to hook IRQ %d\n", brd->irq);
588 brd->state = BOARD_FAILED;
589 brd->dpastatus = BD_NOFEP;
599 static void dgnc_do_remap(struct dgnc_board *brd)
601 if (!brd || brd->magic != DGNC_BOARD_MAGIC)
604 brd->re_map_membase = ioremap(brd->membase, 0x1000);
607 /*****************************************************************************
627 * As each timer expires, it determines (a) whether the "transmit"
628 * waiter needs to be woken up, and (b) whether the poller needs to
631 ******************************************************************************/
633 static void dgnc_poll_handler(ulong dummy)
635 struct dgnc_board *brd;
638 unsigned long new_time;
640 /* Go thru each board, kicking off a tasklet for each if needed */
641 for (i = 0; i < dgnc_NumBoards; i++) {
644 spin_lock_irqsave(&brd->bd_lock, flags);
646 /* If board is in a failed state don't schedule a tasklet */
647 if (brd->state == BOARD_FAILED) {
648 spin_unlock_irqrestore(&brd->bd_lock, flags);
652 /* Schedule a poll helper task */
653 tasklet_schedule(&brd->helper_tasklet);
655 spin_unlock_irqrestore(&brd->bd_lock, flags);
659 * Schedule ourself back at the nominal wakeup interval.
661 spin_lock_irqsave(&dgnc_poll_lock, flags);
662 dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
664 new_time = dgnc_poll_time - jiffies;
666 if ((ulong)new_time >= 2 * dgnc_poll_tick)
667 dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
669 setup_timer(&dgnc_poll_timer, dgnc_poll_handler, 0);
670 dgnc_poll_timer.expires = dgnc_poll_time;
671 spin_unlock_irqrestore(&dgnc_poll_lock, flags);
674 add_timer(&dgnc_poll_timer);