1 /* $Id: divasmain.c,v 1.55.4.6 2005/02/09 19:28:20 armin Exp $
3 * Low level driver for Eicon DIVA Server ISDN cards.
5 * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
6 * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/devfs_fs_kernel.h>
18 #include <asm/uaccess.h>
20 #include <linux/ioport.h>
21 #include <linux/workqueue.h>
22 #include <linux/pci.h>
23 #include <linux/smp_lock.h>
24 #include <linux/interrupt.h>
25 #include <linux/list.h>
26 #include <linux/poll.h>
27 #include <linux/kmod.h>
39 #include "xdi_adapter.h"
44 static char *main_revision = "$Revision: 1.55.4.6 $";
50 MODULE_DESCRIPTION("Kernel driver for Eicon DIVA Server cards");
51 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
52 MODULE_LICENSE("GPL");
54 module_param(dbgmask, int, 0);
55 MODULE_PARM_DESC(dbgmask, "initial debug mask");
57 static char *DRIVERNAME =
58 "Eicon DIVA Server driver (http://www.melware.net)";
59 static char *DRIVERLNAME = "divas";
60 static char *DEVNAME = "Divas";
61 char *DRIVERRELEASE_DIVAS = "2.0";
63 extern irqreturn_t diva_os_irq_wrapper(int irq, void *context,
64 struct pt_regs *regs);
65 extern int create_divas_proc(void);
66 extern void remove_divas_proc(void);
67 extern void diva_get_vserial_number(PISDN_ADAPTER IoAdapter, char *buf);
68 extern int divasfunc_init(int dbgmask);
69 extern void divasfunc_exit(void);
71 typedef struct _diva_os_thread_dpc {
72 struct tasklet_struct divas_task;
73 diva_os_soft_isr_t *psoft_isr;
74 } diva_os_thread_dpc_t;
76 /* --------------------------------------------------------------------------
77 PCI driver interface section
78 -------------------------------------------------------------------------- */
80 vendor, device Vendor and device ID to match (or PCI_ANY_ID)
81 subvendor, Subsystem vendor and device ID to match (or PCI_ANY_ID)
83 class, Device class to match. The class_mask tells which bits
84 class_mask of the class are honored during the comparison.
85 driver_data Data private to the driver.
88 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2)
89 #define PCI_DEVICE_ID_EICON_MAESTRAP_2 0xE015
92 #if !defined(PCI_DEVICE_ID_EICON_4BRI_VOIP)
93 #define PCI_DEVICE_ID_EICON_4BRI_VOIP 0xE016
96 #if !defined(PCI_DEVICE_ID_EICON_4BRI_2_VOIP)
97 #define PCI_DEVICE_ID_EICON_4BRI_2_VOIP 0xE017
100 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2)
101 #define PCI_DEVICE_ID_EICON_BRI2M_2 0xE018
104 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP)
105 #define PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP 0xE019
108 #if !defined(PCI_DEVICE_ID_EICON_2F)
109 #define PCI_DEVICE_ID_EICON_2F 0xE01A
112 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2_VOIP)
113 #define PCI_DEVICE_ID_EICON_BRI2M_2_VOIP 0xE01B
117 This table should be sorted by PCI device ID
119 static struct pci_device_id divas_pci_tbl[] = {
120 /* Diva Server BRI-2M PCI 0xE010 */
121 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRA,
122 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_MAESTRA_PCI},
123 /* Diva Server 4BRI-8M PCI 0xE012 */
124 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ,
125 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_PCI},
126 /* Diva Server 4BRI-8M 2.0 PCI 0xE013 */
127 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ_U,
128 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_V2_PCI},
129 /* Diva Server PRI-30M PCI 0xE014 */
130 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP,
131 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_PCI},
132 /* Diva Server PRI 2.0 adapter 0xE015 */
133 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2,
134 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_V2_PCI},
135 /* Diva Server Voice 4BRI-8M PCI 0xE016 */
136 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_VOIP,
137 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_PCI},
138 /* Diva Server Voice 4BRI-8M 2.0 PCI 0xE017 */
139 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_2_VOIP,
140 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_V2_PCI},
141 /* Diva Server BRI-2M 2.0 PCI 0xE018 */
142 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2,
143 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2M_V2_PCI},
144 /* Diva Server Voice PRI 2.0 PCI 0xE019 */
145 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP,
146 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
147 CARDTYPE_DIVASRV_VOICE_P_30M_V2_PCI},
148 /* Diva Server 2FX 0xE01A */
149 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_2F,
150 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2F_PCI},
151 /* Diva Server Voice BRI-2M 2.0 PCI 0xE01B */
152 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2_VOIP,
153 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_B_2M_V2_PCI},
154 {0,} /* 0 terminated list. */
156 MODULE_DEVICE_TABLE(pci, divas_pci_tbl);
158 static int divas_init_one(struct pci_dev *pdev,
159 const struct pci_device_id *ent);
160 static void __devexit divas_remove_one(struct pci_dev *pdev);
162 static struct pci_driver diva_pci_driver = {
164 .probe = divas_init_one,
165 .remove = __devexit_p(divas_remove_one),
166 .id_table = divas_pci_tbl,
169 /*********************************************************
170 ** little helper functions
171 *********************************************************/
172 static char *getrev(const char *revision)
176 if ((p = strchr(revision, ':'))) {
178 p = strchr(rev, '$');
185 void diva_log_info(unsigned char *format, ...)
188 unsigned char line[160];
190 va_start(args, format);
191 vsprintf(line, format, args);
194 printk(KERN_INFO "%s: %s\n", DRIVERLNAME, line);
197 void divas_get_version(char *p)
201 strcpy(tmprev, main_revision);
202 sprintf(p, "%s: %s(%s) %s(%s) major=%d\n", DRIVERLNAME, DRIVERRELEASE_DIVAS,
203 getrev(tmprev), diva_xdi_common_code_build, DIVA_BUILD, major);
206 /* --------------------------------------------------------------------------
208 -------------------------------------------------------------------------- */
209 byte diva_os_get_pci_bus(void *pci_dev_handle)
211 struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
212 return ((byte) pdev->bus->number);
215 byte diva_os_get_pci_func(void *pci_dev_handle)
217 struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
218 return ((byte) pdev->devfn);
221 unsigned long divasa_get_pci_irq(unsigned char bus, unsigned char func,
222 void *pci_dev_handle)
224 unsigned char irq = 0;
225 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
229 return ((unsigned long) irq);
232 unsigned long divasa_get_pci_bar(unsigned char bus, unsigned char func,
233 int bar, void *pci_dev_handle)
235 unsigned long ret = 0;
236 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
239 ret = dev->resource[bar].start;
242 DBG_TRC(("GOT BAR[%d]=%08x", bar, ret));
245 unsigned long type = (ret & 0x00000001);
246 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
248 ret &= PCI_BASE_ADDRESS_IO_MASK;
250 DBG_TRC((" memory"));
251 ret &= PCI_BASE_ADDRESS_MEM_MASK;
253 DBG_TRC((" final=%08x", ret));
259 void PCIwrite(byte bus, byte func, int offset, void *data, int length,
260 void *pci_dev_handle)
262 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
266 pci_write_config_byte(dev, offset,
267 *(unsigned char *) data);
270 pci_write_config_word(dev, offset,
271 *(unsigned short *) data);
274 pci_write_config_dword(dev, offset,
275 *(unsigned int *) data);
278 default: /* buffer */
279 if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
280 dword *p = (dword *) data;
284 pci_write_config_dword(dev, offset,
288 } else { /* copy as byte stream */
289 byte *p = (byte *) data;
292 pci_write_config_byte(dev, offset,
300 void PCIread(byte bus, byte func, int offset, void *data, int length,
301 void *pci_dev_handle)
303 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
307 pci_read_config_byte(dev, offset, (unsigned char *) data);
310 pci_read_config_word(dev, offset, (unsigned short *) data);
313 pci_read_config_dword(dev, offset, (unsigned int *) data);
316 default: /* buffer */
317 if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
318 dword *p = (dword *) data;
322 pci_read_config_dword(dev, offset,
326 } else { /* copy as byte stream */
327 byte *p = (byte *) data;
330 pci_read_config_byte(dev, offset,
339 Init map with DMA pages. It is not problem if some allocations fail -
340 the channels that will not get one DMA page will use standard PIO
343 static void *diva_pci_alloc_consistent(struct pci_dev *hwdev,
345 dma_addr_t * dma_handle,
348 void *addr = pci_alloc_consistent(hwdev, size, dma_handle);
355 void diva_init_dma_map(void *hdev,
356 struct _diva_dma_map_entry **ppmap, int nentries)
358 struct pci_dev *pdev = (struct pci_dev *) hdev;
359 struct _diva_dma_map_entry *pmap =
360 diva_alloc_dma_map(hdev, nentries);
364 dma_addr_t dma_handle;
368 for (i = 0; i < nentries; i++) {
369 if (!(cpu_addr = diva_pci_alloc_consistent(pdev,
376 diva_init_dma_map_entry(pmap, i, cpu_addr,
379 DBG_TRC(("dma map alloc [%d]=(%08lx:%08x:%08lx)",
380 i, (unsigned long) cpu_addr,
382 (unsigned long) addr_handle))}
389 Free all contained in the map entries and memory used by the map
390 Should be always called after adapter removal from DIDD array
392 void diva_free_dma_map(void *hdev, struct _diva_dma_map_entry *pmap)
394 struct pci_dev *pdev = (struct pci_dev *) hdev;
398 dma_addr_t dma_handle;
401 for (i = 0; (pmap != 0); i++) {
402 diva_get_dma_map_entry(pmap, i, &cpu_addr, &phys_addr);
406 addr_handle = diva_get_entry_handle(pmap, i);
407 dma_handle = (dma_addr_t) phys_addr;
408 pci_free_consistent(pdev, PAGE_SIZE, addr_handle,
410 DBG_TRC(("dma map free [%d]=(%08lx:%08x:%08lx)", i,
411 (unsigned long) cpu_addr, (dword) dma_handle,
412 (unsigned long) addr_handle))
415 diva_free_dma_mapping(pmap);
419 /*********************************************************
420 ** I/O port utilities
421 *********************************************************/
424 diva_os_register_io_port(void *adapter, int on, unsigned long port,
425 unsigned long length, const char *name, int id)
428 if (!request_region(port, length, name)) {
429 DBG_ERR(("A: I/O: can't register port=%08x", port))
433 release_region(port, length);
438 void __iomem *divasa_remap_pci_bar(diva_os_xdi_adapter_t *a, int id, unsigned long bar, unsigned long area_length)
440 void __iomem *ret = ioremap(bar, area_length);
441 DBG_TRC(("remap(%08x)->%p", bar, ret));
445 void divasa_unmap_pci_bar(void __iomem *bar)
452 /*********************************************************
454 *********************************************************/
455 byte __inline__ inpp(void __iomem *addr)
457 return (inb((unsigned long) addr));
460 word __inline__ inppw(void __iomem *addr)
462 return (inw((unsigned long) addr));
465 void __inline__ inppw_buffer(void __iomem *addr, void *P, int length)
467 insw((unsigned long) addr, (word *) P, length >> 1);
470 void __inline__ outppw_buffer(void __iomem *addr, void *P, int length)
472 outsw((unsigned long) addr, (word *) P, length >> 1);
475 void __inline__ outppw(void __iomem *addr, word w)
477 outw(w, (unsigned long) addr);
480 void __inline__ outpp(void __iomem *addr, word p)
482 outb(p, (unsigned long) addr);
485 /* --------------------------------------------------------------------------
487 -------------------------------------------------------------------------- */
488 int diva_os_register_irq(void *context, byte irq, const char *name)
490 int result = request_irq(irq, diva_os_irq_wrapper,
491 SA_INTERRUPT | SA_SHIRQ, name, context);
495 void diva_os_remove_irq(void *context, byte irq)
497 free_irq(irq, context);
500 /* --------------------------------------------------------------------------
501 DPC framework implementation
502 -------------------------------------------------------------------------- */
503 static void diva_os_dpc_proc(unsigned long context)
505 diva_os_thread_dpc_t *psoft_isr = (diva_os_thread_dpc_t *) context;
506 diva_os_soft_isr_t *pisr = psoft_isr->psoft_isr;
508 (*(pisr->callback)) (pisr, pisr->callback_context);
511 int diva_os_initialize_soft_isr(diva_os_soft_isr_t * psoft_isr,
512 diva_os_soft_isr_callback_t callback,
513 void *callback_context)
515 diva_os_thread_dpc_t *pdpc;
517 pdpc = (diva_os_thread_dpc_t *) diva_os_malloc(0, sizeof(*pdpc));
518 if (!(psoft_isr->object = pdpc)) {
521 memset(pdpc, 0x00, sizeof(*pdpc));
522 psoft_isr->callback = callback;
523 psoft_isr->callback_context = callback_context;
524 pdpc->psoft_isr = psoft_isr;
525 tasklet_init(&pdpc->divas_task, diva_os_dpc_proc, (unsigned long)pdpc);
530 int diva_os_schedule_soft_isr(diva_os_soft_isr_t * psoft_isr)
532 if (psoft_isr && psoft_isr->object) {
533 diva_os_thread_dpc_t *pdpc =
534 (diva_os_thread_dpc_t *) psoft_isr->object;
536 tasklet_schedule(&pdpc->divas_task);
542 int diva_os_cancel_soft_isr(diva_os_soft_isr_t * psoft_isr)
547 void diva_os_remove_soft_isr(diva_os_soft_isr_t * psoft_isr)
549 if (psoft_isr && psoft_isr->object) {
550 diva_os_thread_dpc_t *pdpc =
551 (diva_os_thread_dpc_t *) psoft_isr->object;
554 tasklet_kill(&pdpc->divas_task);
555 flush_scheduled_work();
556 mem = psoft_isr->object;
557 psoft_isr->object = NULL;
558 diva_os_free(0, mem);
563 * kernel/user space copy functions
566 xdi_copy_to_user(void *os_handle, void __user *dst, const void *src, int length)
568 if (copy_to_user(dst, src, length)) {
575 xdi_copy_from_user(void *os_handle, void *dst, const void __user *src, int length)
577 if (copy_from_user(dst, src, length)) {
584 * device node operations
586 static int divas_open(struct inode *inode, struct file *file)
591 static int divas_release(struct inode *inode, struct file *file)
593 if (file->private_data) {
594 diva_xdi_close_adapter(file->private_data, file);
599 static ssize_t divas_write(struct file *file, const char __user *buf,
600 size_t count, loff_t * ppos)
604 if (!file->private_data) {
605 file->private_data = diva_xdi_open_adapter(file, buf,
609 if (!file->private_data) {
613 ret = diva_xdi_write(file->private_data, file,
614 buf, count, xdi_copy_from_user);
616 case -1: /* Message should be removed from rx mailbox first */
619 case -2: /* invalid adapter was specified in this call */
626 DBG_TRC(("write: ret %d", ret));
630 static ssize_t divas_read(struct file *file, char __user *buf,
631 size_t count, loff_t * ppos)
635 if (!file->private_data) {
636 file->private_data = diva_xdi_open_adapter(file, buf,
640 if (!file->private_data) {
644 ret = diva_xdi_read(file->private_data, file,
645 buf, count, xdi_copy_to_user);
647 case -1: /* RX mailbox is empty */
650 case -2: /* no memory, mailbox was cleared, last command is failed */
653 case -3: /* can't copy to user, retry */
657 DBG_TRC(("read: ret %d", ret));
661 static unsigned int divas_poll(struct file *file, poll_table * wait)
663 if (!file->private_data) {
666 return (POLLIN | POLLRDNORM);
669 static struct file_operations divas_fops = {
670 .owner = THIS_MODULE,
673 .write = divas_write,
676 .release = divas_release
679 static void divas_unregister_chrdev(void)
681 devfs_remove(DEVNAME);
682 unregister_chrdev(major, DEVNAME);
685 static int DIVA_INIT_FUNCTION divas_register_chrdev(void)
687 if ((major = register_chrdev(0, DEVNAME, &divas_fops)) < 0)
689 printk(KERN_ERR "%s: failed to create /dev entry.\n",
693 devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
698 /* --------------------------------------------------------------------------
700 -------------------------------------------------------------------------- */
701 static int __devinit divas_init_one(struct pci_dev *pdev,
702 const struct pci_device_id *ent)
708 DBG_TRC(("%s bus: %08x fn: %08x insertion.\n",
709 CardProperties[ent->driver_data].Name,
710 pdev->bus->number, pdev->devfn))
711 printk(KERN_INFO "%s: %s bus: %08x fn: %08x insertion.\n",
712 DRIVERLNAME, CardProperties[ent->driver_data].Name,
713 pdev->bus->number, pdev->devfn);
715 if (pci_enable_device(pdev)) {
716 DBG_TRC(("%s: %s bus: %08x fn: %08x device init failed.\n",
718 CardProperties[ent->driver_data].Name,
722 "%s: %s bus: %08x fn: %08x device init failed.\n",
724 CardProperties[ent->driver_data].
725 Name, pdev->bus->number,
730 pci_set_master(pdev);
732 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
734 DBG_TRC(("%s: bus: %08x fn: %08x fix latency.\n",
735 DRIVERLNAME, pdev->bus->number, pdev->devfn))
737 "%s: bus: %08x fn: %08x fix latency.\n",
738 DRIVERLNAME, pdev->bus->number, pdev->devfn);
739 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, new_latency);
742 if (!(pdiva = diva_driver_add_card(pdev, ent->driver_data))) {
743 DBG_TRC(("%s: %s bus: %08x fn: %08x card init failed.\n",
745 CardProperties[ent->driver_data].Name,
749 "%s: %s bus: %08x fn: %08x card init failed.\n",
751 CardProperties[ent->driver_data].
752 Name, pdev->bus->number,
757 pci_set_drvdata(pdev, pdiva);
762 static void __devexit divas_remove_one(struct pci_dev *pdev)
764 void *pdiva = pci_get_drvdata(pdev);
766 DBG_TRC(("bus: %08x fn: %08x removal.\n",
767 pdev->bus->number, pdev->devfn))
768 printk(KERN_INFO "%s: bus: %08x fn: %08x removal.\n",
769 DRIVERLNAME, pdev->bus->number, pdev->devfn);
772 diva_driver_remove_card(pdiva);
777 /* --------------------------------------------------------------------------
778 Driver Load / Startup
779 -------------------------------------------------------------------------- */
780 static int DIVA_INIT_FUNCTION divas_init(void)
785 printk(KERN_INFO "%s\n", DRIVERNAME);
786 printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_DIVAS);
787 strcpy(tmprev, main_revision);
788 printk("%s Build: %s(%s)\n", getrev(tmprev),
789 diva_xdi_common_code_build, DIVA_BUILD);
790 printk(KERN_INFO "%s: support for: ", DRIVERLNAME);
791 #ifdef CONFIG_ISDN_DIVAS_BRIPCI
794 #ifdef CONFIG_ISDN_DIVAS_PRIPCI
797 printk("adapters\n");
799 if (!divasfunc_init(dbgmask)) {
800 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
806 if (!divas_register_chrdev()) {
814 if (!create_divas_proc()) {
817 divas_unregister_chrdev();
820 printk(KERN_ERR "%s: failed to create proc entry.\n",
826 if ((ret = pci_register_driver(&diva_pci_driver))) {
829 divas_unregister_chrdev();
832 printk(KERN_ERR "%s: failed to init pci driver.\n",
836 printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
842 /* --------------------------------------------------------------------------
844 -------------------------------------------------------------------------- */
845 static void DIVA_EXIT_FUNCTION divas_exit(void)
847 pci_unregister_driver(&diva_pci_driver);
849 divas_unregister_chrdev();
852 printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
855 module_init(divas_init);
856 module_exit(divas_exit);