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/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <asm/uaccess.h>
17 #include <linux/ioport.h>
18 #include <linux/workqueue.h>
19 #include <linux/pci.h>
20 #include <linux/interrupt.h>
21 #include <linux/list.h>
22 #include <linux/poll.h>
23 #include <linux/kmod.h>
35 #include "xdi_adapter.h"
40 static char *main_revision = "$Revision: 1.55.4.6 $";
46 MODULE_DESCRIPTION("Kernel driver for Eicon DIVA Server cards");
47 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
48 MODULE_LICENSE("GPL");
50 module_param(dbgmask, int, 0);
51 MODULE_PARM_DESC(dbgmask, "initial debug mask");
53 static char *DRIVERNAME =
54 "Eicon DIVA Server driver (http://www.melware.net)";
55 static char *DRIVERLNAME = "divas";
56 static char *DEVNAME = "Divas";
57 char *DRIVERRELEASE_DIVAS = "2.0";
59 extern irqreturn_t diva_os_irq_wrapper(int irq, void *context);
60 extern int create_divas_proc(void);
61 extern void remove_divas_proc(void);
62 extern void diva_get_vserial_number(PISDN_ADAPTER IoAdapter, char *buf);
63 extern int divasfunc_init(int dbgmask);
64 extern void divasfunc_exit(void);
66 typedef struct _diva_os_thread_dpc {
67 struct tasklet_struct divas_task;
68 diva_os_soft_isr_t *psoft_isr;
69 } diva_os_thread_dpc_t;
71 /* --------------------------------------------------------------------------
72 PCI driver interface section
73 -------------------------------------------------------------------------- */
75 vendor, device Vendor and device ID to match (or PCI_ANY_ID)
76 subvendor, Subsystem vendor and device ID to match (or PCI_ANY_ID)
78 class, Device class to match. The class_mask tells which bits
79 class_mask of the class are honored during the comparison.
80 driver_data Data private to the driver.
83 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2)
84 #define PCI_DEVICE_ID_EICON_MAESTRAP_2 0xE015
87 #if !defined(PCI_DEVICE_ID_EICON_4BRI_VOIP)
88 #define PCI_DEVICE_ID_EICON_4BRI_VOIP 0xE016
91 #if !defined(PCI_DEVICE_ID_EICON_4BRI_2_VOIP)
92 #define PCI_DEVICE_ID_EICON_4BRI_2_VOIP 0xE017
95 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2)
96 #define PCI_DEVICE_ID_EICON_BRI2M_2 0xE018
99 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP)
100 #define PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP 0xE019
103 #if !defined(PCI_DEVICE_ID_EICON_2F)
104 #define PCI_DEVICE_ID_EICON_2F 0xE01A
107 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2_VOIP)
108 #define PCI_DEVICE_ID_EICON_BRI2M_2_VOIP 0xE01B
112 This table should be sorted by PCI device ID
114 static struct pci_device_id divas_pci_tbl[] = {
115 /* Diva Server BRI-2M PCI 0xE010 */
116 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRA,
117 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_MAESTRA_PCI},
118 /* Diva Server 4BRI-8M PCI 0xE012 */
119 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ,
120 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_PCI},
121 /* Diva Server 4BRI-8M 2.0 PCI 0xE013 */
122 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ_U,
123 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_V2_PCI},
124 /* Diva Server PRI-30M PCI 0xE014 */
125 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP,
126 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_PCI},
127 /* Diva Server PRI 2.0 adapter 0xE015 */
128 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2,
129 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_V2_PCI},
130 /* Diva Server Voice 4BRI-8M PCI 0xE016 */
131 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_VOIP,
132 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_PCI},
133 /* Diva Server Voice 4BRI-8M 2.0 PCI 0xE017 */
134 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_2_VOIP,
135 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_V2_PCI},
136 /* Diva Server BRI-2M 2.0 PCI 0xE018 */
137 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2,
138 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2M_V2_PCI},
139 /* Diva Server Voice PRI 2.0 PCI 0xE019 */
140 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP,
141 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
142 CARDTYPE_DIVASRV_VOICE_P_30M_V2_PCI},
143 /* Diva Server 2FX 0xE01A */
144 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_2F,
145 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2F_PCI},
146 /* Diva Server Voice BRI-2M 2.0 PCI 0xE01B */
147 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2_VOIP,
148 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_B_2M_V2_PCI},
149 {0,} /* 0 terminated list. */
151 MODULE_DEVICE_TABLE(pci, divas_pci_tbl);
153 static int divas_init_one(struct pci_dev *pdev,
154 const struct pci_device_id *ent);
155 static void __devexit divas_remove_one(struct pci_dev *pdev);
157 static struct pci_driver diva_pci_driver = {
159 .probe = divas_init_one,
160 .remove = __devexit_p(divas_remove_one),
161 .id_table = divas_pci_tbl,
164 /*********************************************************
165 ** little helper functions
166 *********************************************************/
167 static char *getrev(const char *revision)
171 if ((p = strchr(revision, ':'))) {
173 p = strchr(rev, '$');
180 void diva_log_info(unsigned char *format, ...)
183 unsigned char line[160];
185 va_start(args, format);
186 vsnprintf(line, sizeof(line), format, args);
189 printk(KERN_INFO "%s: %s\n", DRIVERLNAME, line);
192 void divas_get_version(char *p)
196 strcpy(tmprev, main_revision);
197 sprintf(p, "%s: %s(%s) %s(%s) major=%d\n", DRIVERLNAME, DRIVERRELEASE_DIVAS,
198 getrev(tmprev), diva_xdi_common_code_build, DIVA_BUILD, major);
201 /* --------------------------------------------------------------------------
203 -------------------------------------------------------------------------- */
204 byte diva_os_get_pci_bus(void *pci_dev_handle)
206 struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
207 return ((byte) pdev->bus->number);
210 byte diva_os_get_pci_func(void *pci_dev_handle)
212 struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
213 return ((byte) pdev->devfn);
216 unsigned long divasa_get_pci_irq(unsigned char bus, unsigned char func,
217 void *pci_dev_handle)
219 unsigned char irq = 0;
220 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
224 return ((unsigned long) irq);
227 unsigned long divasa_get_pci_bar(unsigned char bus, unsigned char func,
228 int bar, void *pci_dev_handle)
230 unsigned long ret = 0;
231 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
234 ret = dev->resource[bar].start;
237 DBG_TRC(("GOT BAR[%d]=%08x", bar, ret));
240 unsigned long type = (ret & 0x00000001);
241 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
243 ret &= PCI_BASE_ADDRESS_IO_MASK;
245 DBG_TRC((" memory"));
246 ret &= PCI_BASE_ADDRESS_MEM_MASK;
248 DBG_TRC((" final=%08x", ret));
254 void PCIwrite(byte bus, byte func, int offset, void *data, int length,
255 void *pci_dev_handle)
257 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
261 pci_write_config_byte(dev, offset,
262 *(unsigned char *) data);
265 pci_write_config_word(dev, offset,
266 *(unsigned short *) data);
269 pci_write_config_dword(dev, offset,
270 *(unsigned int *) data);
273 default: /* buffer */
274 if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
275 dword *p = (dword *) data;
279 pci_write_config_dword(dev, offset,
283 } else { /* copy as byte stream */
284 byte *p = (byte *) data;
287 pci_write_config_byte(dev, offset,
295 void PCIread(byte bus, byte func, int offset, void *data, int length,
296 void *pci_dev_handle)
298 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
302 pci_read_config_byte(dev, offset, (unsigned char *) data);
305 pci_read_config_word(dev, offset, (unsigned short *) data);
308 pci_read_config_dword(dev, offset, (unsigned int *) data);
311 default: /* buffer */
312 if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
313 dword *p = (dword *) data;
317 pci_read_config_dword(dev, offset,
321 } else { /* copy as byte stream */
322 byte *p = (byte *) data;
325 pci_read_config_byte(dev, offset,
334 Init map with DMA pages. It is not problem if some allocations fail -
335 the channels that will not get one DMA page will use standard PIO
338 static void *diva_pci_alloc_consistent(struct pci_dev *hwdev,
340 dma_addr_t * dma_handle,
343 void *addr = pci_alloc_consistent(hwdev, size, dma_handle);
350 void diva_init_dma_map(void *hdev,
351 struct _diva_dma_map_entry **ppmap, int nentries)
353 struct pci_dev *pdev = (struct pci_dev *) hdev;
354 struct _diva_dma_map_entry *pmap =
355 diva_alloc_dma_map(hdev, nentries);
359 dma_addr_t dma_handle;
363 for (i = 0; i < nentries; i++) {
364 if (!(cpu_addr = diva_pci_alloc_consistent(pdev,
371 diva_init_dma_map_entry(pmap, i, cpu_addr,
374 DBG_TRC(("dma map alloc [%d]=(%08lx:%08x:%08lx)",
375 i, (unsigned long) cpu_addr,
377 (unsigned long) addr_handle))}
384 Free all contained in the map entries and memory used by the map
385 Should be always called after adapter removal from DIDD array
387 void diva_free_dma_map(void *hdev, struct _diva_dma_map_entry *pmap)
389 struct pci_dev *pdev = (struct pci_dev *) hdev;
393 dma_addr_t dma_handle;
396 for (i = 0; (pmap != 0); i++) {
397 diva_get_dma_map_entry(pmap, i, &cpu_addr, &phys_addr);
401 addr_handle = diva_get_entry_handle(pmap, i);
402 dma_handle = (dma_addr_t) phys_addr;
403 pci_free_consistent(pdev, PAGE_SIZE, addr_handle,
405 DBG_TRC(("dma map free [%d]=(%08lx:%08x:%08lx)", i,
406 (unsigned long) cpu_addr, (dword) dma_handle,
407 (unsigned long) addr_handle))
410 diva_free_dma_mapping(pmap);
414 /*********************************************************
415 ** I/O port utilities
416 *********************************************************/
419 diva_os_register_io_port(void *adapter, int on, unsigned long port,
420 unsigned long length, const char *name, int id)
423 if (!request_region(port, length, name)) {
424 DBG_ERR(("A: I/O: can't register port=%08x", port))
428 release_region(port, length);
433 void __iomem *divasa_remap_pci_bar(diva_os_xdi_adapter_t *a, int id, unsigned long bar, unsigned long area_length)
435 void __iomem *ret = ioremap(bar, area_length);
436 DBG_TRC(("remap(%08x)->%p", bar, ret));
440 void divasa_unmap_pci_bar(void __iomem *bar)
447 /*********************************************************
449 *********************************************************/
450 byte __inline__ inpp(void __iomem *addr)
452 return (inb((unsigned long) addr));
455 word __inline__ inppw(void __iomem *addr)
457 return (inw((unsigned long) addr));
460 void __inline__ inppw_buffer(void __iomem *addr, void *P, int length)
462 insw((unsigned long) addr, (word *) P, length >> 1);
465 void __inline__ outppw_buffer(void __iomem *addr, void *P, int length)
467 outsw((unsigned long) addr, (word *) P, length >> 1);
470 void __inline__ outppw(void __iomem *addr, word w)
472 outw(w, (unsigned long) addr);
475 void __inline__ outpp(void __iomem *addr, word p)
477 outb(p, (unsigned long) addr);
480 /* --------------------------------------------------------------------------
482 -------------------------------------------------------------------------- */
483 int diva_os_register_irq(void *context, byte irq, const char *name)
485 int result = request_irq(irq, diva_os_irq_wrapper,
486 IRQF_DISABLED | IRQF_SHARED, name, context);
490 void diva_os_remove_irq(void *context, byte irq)
492 free_irq(irq, context);
495 /* --------------------------------------------------------------------------
496 DPC framework implementation
497 -------------------------------------------------------------------------- */
498 static void diva_os_dpc_proc(unsigned long context)
500 diva_os_thread_dpc_t *psoft_isr = (diva_os_thread_dpc_t *) context;
501 diva_os_soft_isr_t *pisr = psoft_isr->psoft_isr;
503 (*(pisr->callback)) (pisr, pisr->callback_context);
506 int diva_os_initialize_soft_isr(diva_os_soft_isr_t * psoft_isr,
507 diva_os_soft_isr_callback_t callback,
508 void *callback_context)
510 diva_os_thread_dpc_t *pdpc;
512 pdpc = (diva_os_thread_dpc_t *) diva_os_malloc(0, sizeof(*pdpc));
513 if (!(psoft_isr->object = pdpc)) {
516 memset(pdpc, 0x00, sizeof(*pdpc));
517 psoft_isr->callback = callback;
518 psoft_isr->callback_context = callback_context;
519 pdpc->psoft_isr = psoft_isr;
520 tasklet_init(&pdpc->divas_task, diva_os_dpc_proc, (unsigned long)pdpc);
525 int diva_os_schedule_soft_isr(diva_os_soft_isr_t * psoft_isr)
527 if (psoft_isr && psoft_isr->object) {
528 diva_os_thread_dpc_t *pdpc =
529 (diva_os_thread_dpc_t *) psoft_isr->object;
531 tasklet_schedule(&pdpc->divas_task);
537 int diva_os_cancel_soft_isr(diva_os_soft_isr_t * psoft_isr)
542 void diva_os_remove_soft_isr(diva_os_soft_isr_t * psoft_isr)
544 if (psoft_isr && psoft_isr->object) {
545 diva_os_thread_dpc_t *pdpc =
546 (diva_os_thread_dpc_t *) psoft_isr->object;
549 tasklet_kill(&pdpc->divas_task);
550 flush_scheduled_work();
551 mem = psoft_isr->object;
552 psoft_isr->object = NULL;
553 diva_os_free(0, mem);
558 * kernel/user space copy functions
561 xdi_copy_to_user(void *os_handle, void __user *dst, const void *src, int length)
563 if (copy_to_user(dst, src, length)) {
570 xdi_copy_from_user(void *os_handle, void *dst, const void __user *src, int length)
572 if (copy_from_user(dst, src, length)) {
579 * device node operations
581 static int divas_open(struct inode *inode, struct file *file)
586 static int divas_release(struct inode *inode, struct file *file)
588 if (file->private_data) {
589 diva_xdi_close_adapter(file->private_data, file);
594 static ssize_t divas_write(struct file *file, const char __user *buf,
595 size_t count, loff_t * ppos)
599 if (!file->private_data) {
600 file->private_data = diva_xdi_open_adapter(file, buf,
604 if (!file->private_data) {
608 ret = diva_xdi_write(file->private_data, file,
609 buf, count, xdi_copy_from_user);
611 case -1: /* Message should be removed from rx mailbox first */
614 case -2: /* invalid adapter was specified in this call */
621 DBG_TRC(("write: ret %d", ret));
625 static ssize_t divas_read(struct file *file, char __user *buf,
626 size_t count, loff_t * ppos)
630 if (!file->private_data) {
631 file->private_data = diva_xdi_open_adapter(file, buf,
635 if (!file->private_data) {
639 ret = diva_xdi_read(file->private_data, file,
640 buf, count, xdi_copy_to_user);
642 case -1: /* RX mailbox is empty */
645 case -2: /* no memory, mailbox was cleared, last command is failed */
648 case -3: /* can't copy to user, retry */
652 DBG_TRC(("read: ret %d", ret));
656 static unsigned int divas_poll(struct file *file, poll_table * wait)
658 if (!file->private_data) {
661 return (POLLIN | POLLRDNORM);
664 static const struct file_operations divas_fops = {
665 .owner = THIS_MODULE,
668 .write = divas_write,
671 .release = divas_release
674 static void divas_unregister_chrdev(void)
676 unregister_chrdev(major, DEVNAME);
679 static int DIVA_INIT_FUNCTION divas_register_chrdev(void)
681 if ((major = register_chrdev(0, DEVNAME, &divas_fops)) < 0)
683 printk(KERN_ERR "%s: failed to create /dev entry.\n",
691 /* --------------------------------------------------------------------------
693 -------------------------------------------------------------------------- */
694 static int __devinit divas_init_one(struct pci_dev *pdev,
695 const struct pci_device_id *ent)
701 DBG_TRC(("%s bus: %08x fn: %08x insertion.\n",
702 CardProperties[ent->driver_data].Name,
703 pdev->bus->number, pdev->devfn))
704 printk(KERN_INFO "%s: %s bus: %08x fn: %08x insertion.\n",
705 DRIVERLNAME, CardProperties[ent->driver_data].Name,
706 pdev->bus->number, pdev->devfn);
708 if (pci_enable_device(pdev)) {
709 DBG_TRC(("%s: %s bus: %08x fn: %08x device init failed.\n",
711 CardProperties[ent->driver_data].Name,
715 "%s: %s bus: %08x fn: %08x device init failed.\n",
717 CardProperties[ent->driver_data].
718 Name, pdev->bus->number,
723 pci_set_master(pdev);
725 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
727 DBG_TRC(("%s: bus: %08x fn: %08x fix latency.\n",
728 DRIVERLNAME, pdev->bus->number, pdev->devfn))
730 "%s: bus: %08x fn: %08x fix latency.\n",
731 DRIVERLNAME, pdev->bus->number, pdev->devfn);
732 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, new_latency);
735 if (!(pdiva = diva_driver_add_card(pdev, ent->driver_data))) {
736 DBG_TRC(("%s: %s bus: %08x fn: %08x card init failed.\n",
738 CardProperties[ent->driver_data].Name,
742 "%s: %s bus: %08x fn: %08x card init failed.\n",
744 CardProperties[ent->driver_data].
745 Name, pdev->bus->number,
750 pci_set_drvdata(pdev, pdiva);
755 static void __devexit divas_remove_one(struct pci_dev *pdev)
757 void *pdiva = pci_get_drvdata(pdev);
759 DBG_TRC(("bus: %08x fn: %08x removal.\n",
760 pdev->bus->number, pdev->devfn))
761 printk(KERN_INFO "%s: bus: %08x fn: %08x removal.\n",
762 DRIVERLNAME, pdev->bus->number, pdev->devfn);
765 diva_driver_remove_card(pdiva);
770 /* --------------------------------------------------------------------------
771 Driver Load / Startup
772 -------------------------------------------------------------------------- */
773 static int DIVA_INIT_FUNCTION divas_init(void)
778 printk(KERN_INFO "%s\n", DRIVERNAME);
779 printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_DIVAS);
780 strcpy(tmprev, main_revision);
781 printk("%s Build: %s(%s)\n", getrev(tmprev),
782 diva_xdi_common_code_build, DIVA_BUILD);
783 printk(KERN_INFO "%s: support for: ", DRIVERLNAME);
784 #ifdef CONFIG_ISDN_DIVAS_BRIPCI
787 #ifdef CONFIG_ISDN_DIVAS_PRIPCI
790 printk("adapters\n");
792 if (!divasfunc_init(dbgmask)) {
793 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
799 if (!divas_register_chrdev()) {
807 if (!create_divas_proc()) {
810 divas_unregister_chrdev();
813 printk(KERN_ERR "%s: failed to create proc entry.\n",
819 if ((ret = pci_register_driver(&diva_pci_driver))) {
822 divas_unregister_chrdev();
825 printk(KERN_ERR "%s: failed to init pci driver.\n",
829 printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
835 /* --------------------------------------------------------------------------
837 -------------------------------------------------------------------------- */
838 static void DIVA_EXIT_FUNCTION divas_exit(void)
840 pci_unregister_driver(&diva_pci_driver);
842 divas_unregister_chrdev();
845 printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
848 module_init(divas_init);
849 module_exit(divas_exit);