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 <asm/uaccess.h>
19 #include <linux/ioport.h>
20 #include <linux/workqueue.h>
21 #include <linux/pci.h>
22 #include <linux/smp_lock.h>
23 #include <linux/interrupt.h>
24 #include <linux/list.h>
25 #include <linux/poll.h>
26 #include <linux/kmod.h>
38 #include "xdi_adapter.h"
43 static char *main_revision = "$Revision: 1.55.4.6 $";
49 MODULE_DESCRIPTION("Kernel driver for Eicon DIVA Server cards");
50 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
51 MODULE_LICENSE("GPL");
53 module_param(dbgmask, int, 0);
54 MODULE_PARM_DESC(dbgmask, "initial debug mask");
56 static char *DRIVERNAME =
57 "Eicon DIVA Server driver (http://www.melware.net)";
58 static char *DRIVERLNAME = "divas";
59 static char *DEVNAME = "Divas";
60 char *DRIVERRELEASE_DIVAS = "2.0";
62 extern irqreturn_t diva_os_irq_wrapper(int irq, void *context,
63 struct pt_regs *regs);
64 extern int create_divas_proc(void);
65 extern void remove_divas_proc(void);
66 extern void diva_get_vserial_number(PISDN_ADAPTER IoAdapter, char *buf);
67 extern int divasfunc_init(int dbgmask);
68 extern void divasfunc_exit(void);
70 typedef struct _diva_os_thread_dpc {
71 struct tasklet_struct divas_task;
72 diva_os_soft_isr_t *psoft_isr;
73 } diva_os_thread_dpc_t;
75 /* --------------------------------------------------------------------------
76 PCI driver interface section
77 -------------------------------------------------------------------------- */
79 vendor, device Vendor and device ID to match (or PCI_ANY_ID)
80 subvendor, Subsystem vendor and device ID to match (or PCI_ANY_ID)
82 class, Device class to match. The class_mask tells which bits
83 class_mask of the class are honored during the comparison.
84 driver_data Data private to the driver.
87 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2)
88 #define PCI_DEVICE_ID_EICON_MAESTRAP_2 0xE015
91 #if !defined(PCI_DEVICE_ID_EICON_4BRI_VOIP)
92 #define PCI_DEVICE_ID_EICON_4BRI_VOIP 0xE016
95 #if !defined(PCI_DEVICE_ID_EICON_4BRI_2_VOIP)
96 #define PCI_DEVICE_ID_EICON_4BRI_2_VOIP 0xE017
99 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2)
100 #define PCI_DEVICE_ID_EICON_BRI2M_2 0xE018
103 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP)
104 #define PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP 0xE019
107 #if !defined(PCI_DEVICE_ID_EICON_2F)
108 #define PCI_DEVICE_ID_EICON_2F 0xE01A
111 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2_VOIP)
112 #define PCI_DEVICE_ID_EICON_BRI2M_2_VOIP 0xE01B
116 This table should be sorted by PCI device ID
118 static struct pci_device_id divas_pci_tbl[] = {
119 /* Diva Server BRI-2M PCI 0xE010 */
120 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRA,
121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_MAESTRA_PCI},
122 /* Diva Server 4BRI-8M PCI 0xE012 */
123 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ,
124 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_PCI},
125 /* Diva Server 4BRI-8M 2.0 PCI 0xE013 */
126 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ_U,
127 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_V2_PCI},
128 /* Diva Server PRI-30M PCI 0xE014 */
129 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP,
130 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_PCI},
131 /* Diva Server PRI 2.0 adapter 0xE015 */
132 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2,
133 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_V2_PCI},
134 /* Diva Server Voice 4BRI-8M PCI 0xE016 */
135 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_VOIP,
136 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_PCI},
137 /* Diva Server Voice 4BRI-8M 2.0 PCI 0xE017 */
138 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_2_VOIP,
139 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_V2_PCI},
140 /* Diva Server BRI-2M 2.0 PCI 0xE018 */
141 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2,
142 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2M_V2_PCI},
143 /* Diva Server Voice PRI 2.0 PCI 0xE019 */
144 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP,
145 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
146 CARDTYPE_DIVASRV_VOICE_P_30M_V2_PCI},
147 /* Diva Server 2FX 0xE01A */
148 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_2F,
149 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2F_PCI},
150 /* Diva Server Voice BRI-2M 2.0 PCI 0xE01B */
151 {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2_VOIP,
152 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_B_2M_V2_PCI},
153 {0,} /* 0 terminated list. */
155 MODULE_DEVICE_TABLE(pci, divas_pci_tbl);
157 static int divas_init_one(struct pci_dev *pdev,
158 const struct pci_device_id *ent);
159 static void __devexit divas_remove_one(struct pci_dev *pdev);
161 static struct pci_driver diva_pci_driver = {
163 .probe = divas_init_one,
164 .remove = __devexit_p(divas_remove_one),
165 .id_table = divas_pci_tbl,
168 /*********************************************************
169 ** little helper functions
170 *********************************************************/
171 static char *getrev(const char *revision)
175 if ((p = strchr(revision, ':'))) {
177 p = strchr(rev, '$');
184 void diva_log_info(unsigned char *format, ...)
187 unsigned char line[160];
189 va_start(args, format);
190 vsprintf(line, format, args);
193 printk(KERN_INFO "%s: %s\n", DRIVERLNAME, line);
196 void divas_get_version(char *p)
200 strcpy(tmprev, main_revision);
201 sprintf(p, "%s: %s(%s) %s(%s) major=%d\n", DRIVERLNAME, DRIVERRELEASE_DIVAS,
202 getrev(tmprev), diva_xdi_common_code_build, DIVA_BUILD, major);
205 /* --------------------------------------------------------------------------
207 -------------------------------------------------------------------------- */
208 byte diva_os_get_pci_bus(void *pci_dev_handle)
210 struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
211 return ((byte) pdev->bus->number);
214 byte diva_os_get_pci_func(void *pci_dev_handle)
216 struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
217 return ((byte) pdev->devfn);
220 unsigned long divasa_get_pci_irq(unsigned char bus, unsigned char func,
221 void *pci_dev_handle)
223 unsigned char irq = 0;
224 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
228 return ((unsigned long) irq);
231 unsigned long divasa_get_pci_bar(unsigned char bus, unsigned char func,
232 int bar, void *pci_dev_handle)
234 unsigned long ret = 0;
235 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
238 ret = dev->resource[bar].start;
241 DBG_TRC(("GOT BAR[%d]=%08x", bar, ret));
244 unsigned long type = (ret & 0x00000001);
245 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
247 ret &= PCI_BASE_ADDRESS_IO_MASK;
249 DBG_TRC((" memory"));
250 ret &= PCI_BASE_ADDRESS_MEM_MASK;
252 DBG_TRC((" final=%08x", ret));
258 void PCIwrite(byte bus, byte func, int offset, void *data, int length,
259 void *pci_dev_handle)
261 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
265 pci_write_config_byte(dev, offset,
266 *(unsigned char *) data);
269 pci_write_config_word(dev, offset,
270 *(unsigned short *) data);
273 pci_write_config_dword(dev, offset,
274 *(unsigned int *) data);
277 default: /* buffer */
278 if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
279 dword *p = (dword *) data;
283 pci_write_config_dword(dev, offset,
287 } else { /* copy as byte stream */
288 byte *p = (byte *) data;
291 pci_write_config_byte(dev, offset,
299 void PCIread(byte bus, byte func, int offset, void *data, int length,
300 void *pci_dev_handle)
302 struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
306 pci_read_config_byte(dev, offset, (unsigned char *) data);
309 pci_read_config_word(dev, offset, (unsigned short *) data);
312 pci_read_config_dword(dev, offset, (unsigned int *) data);
315 default: /* buffer */
316 if (!(length % 4) && !(length & 0x03)) { /* Copy as dword */
317 dword *p = (dword *) data;
321 pci_read_config_dword(dev, offset,
325 } else { /* copy as byte stream */
326 byte *p = (byte *) data;
329 pci_read_config_byte(dev, offset,
338 Init map with DMA pages. It is not problem if some allocations fail -
339 the channels that will not get one DMA page will use standard PIO
342 static void *diva_pci_alloc_consistent(struct pci_dev *hwdev,
344 dma_addr_t * dma_handle,
347 void *addr = pci_alloc_consistent(hwdev, size, dma_handle);
354 void diva_init_dma_map(void *hdev,
355 struct _diva_dma_map_entry **ppmap, int nentries)
357 struct pci_dev *pdev = (struct pci_dev *) hdev;
358 struct _diva_dma_map_entry *pmap =
359 diva_alloc_dma_map(hdev, nentries);
363 dma_addr_t dma_handle;
367 for (i = 0; i < nentries; i++) {
368 if (!(cpu_addr = diva_pci_alloc_consistent(pdev,
375 diva_init_dma_map_entry(pmap, i, cpu_addr,
378 DBG_TRC(("dma map alloc [%d]=(%08lx:%08x:%08lx)",
379 i, (unsigned long) cpu_addr,
381 (unsigned long) addr_handle))}
388 Free all contained in the map entries and memory used by the map
389 Should be always called after adapter removal from DIDD array
391 void diva_free_dma_map(void *hdev, struct _diva_dma_map_entry *pmap)
393 struct pci_dev *pdev = (struct pci_dev *) hdev;
397 dma_addr_t dma_handle;
400 for (i = 0; (pmap != 0); i++) {
401 diva_get_dma_map_entry(pmap, i, &cpu_addr, &phys_addr);
405 addr_handle = diva_get_entry_handle(pmap, i);
406 dma_handle = (dma_addr_t) phys_addr;
407 pci_free_consistent(pdev, PAGE_SIZE, addr_handle,
409 DBG_TRC(("dma map free [%d]=(%08lx:%08x:%08lx)", i,
410 (unsigned long) cpu_addr, (dword) dma_handle,
411 (unsigned long) addr_handle))
414 diva_free_dma_mapping(pmap);
418 /*********************************************************
419 ** I/O port utilities
420 *********************************************************/
423 diva_os_register_io_port(void *adapter, int on, unsigned long port,
424 unsigned long length, const char *name, int id)
427 if (!request_region(port, length, name)) {
428 DBG_ERR(("A: I/O: can't register port=%08x", port))
432 release_region(port, length);
437 void __iomem *divasa_remap_pci_bar(diva_os_xdi_adapter_t *a, int id, unsigned long bar, unsigned long area_length)
439 void __iomem *ret = ioremap(bar, area_length);
440 DBG_TRC(("remap(%08x)->%p", bar, ret));
444 void divasa_unmap_pci_bar(void __iomem *bar)
451 /*********************************************************
453 *********************************************************/
454 byte __inline__ inpp(void __iomem *addr)
456 return (inb((unsigned long) addr));
459 word __inline__ inppw(void __iomem *addr)
461 return (inw((unsigned long) addr));
464 void __inline__ inppw_buffer(void __iomem *addr, void *P, int length)
466 insw((unsigned long) addr, (word *) P, length >> 1);
469 void __inline__ outppw_buffer(void __iomem *addr, void *P, int length)
471 outsw((unsigned long) addr, (word *) P, length >> 1);
474 void __inline__ outppw(void __iomem *addr, word w)
476 outw(w, (unsigned long) addr);
479 void __inline__ outpp(void __iomem *addr, word p)
481 outb(p, (unsigned long) addr);
484 /* --------------------------------------------------------------------------
486 -------------------------------------------------------------------------- */
487 int diva_os_register_irq(void *context, byte irq, const char *name)
489 int result = request_irq(irq, diva_os_irq_wrapper,
490 SA_INTERRUPT | SA_SHIRQ, name, context);
494 void diva_os_remove_irq(void *context, byte irq)
496 free_irq(irq, context);
499 /* --------------------------------------------------------------------------
500 DPC framework implementation
501 -------------------------------------------------------------------------- */
502 static void diva_os_dpc_proc(unsigned long context)
504 diva_os_thread_dpc_t *psoft_isr = (diva_os_thread_dpc_t *) context;
505 diva_os_soft_isr_t *pisr = psoft_isr->psoft_isr;
507 (*(pisr->callback)) (pisr, pisr->callback_context);
510 int diva_os_initialize_soft_isr(diva_os_soft_isr_t * psoft_isr,
511 diva_os_soft_isr_callback_t callback,
512 void *callback_context)
514 diva_os_thread_dpc_t *pdpc;
516 pdpc = (diva_os_thread_dpc_t *) diva_os_malloc(0, sizeof(*pdpc));
517 if (!(psoft_isr->object = pdpc)) {
520 memset(pdpc, 0x00, sizeof(*pdpc));
521 psoft_isr->callback = callback;
522 psoft_isr->callback_context = callback_context;
523 pdpc->psoft_isr = psoft_isr;
524 tasklet_init(&pdpc->divas_task, diva_os_dpc_proc, (unsigned long)pdpc);
529 int diva_os_schedule_soft_isr(diva_os_soft_isr_t * psoft_isr)
531 if (psoft_isr && psoft_isr->object) {
532 diva_os_thread_dpc_t *pdpc =
533 (diva_os_thread_dpc_t *) psoft_isr->object;
535 tasklet_schedule(&pdpc->divas_task);
541 int diva_os_cancel_soft_isr(diva_os_soft_isr_t * psoft_isr)
546 void diva_os_remove_soft_isr(diva_os_soft_isr_t * psoft_isr)
548 if (psoft_isr && psoft_isr->object) {
549 diva_os_thread_dpc_t *pdpc =
550 (diva_os_thread_dpc_t *) psoft_isr->object;
553 tasklet_kill(&pdpc->divas_task);
554 flush_scheduled_work();
555 mem = psoft_isr->object;
556 psoft_isr->object = NULL;
557 diva_os_free(0, mem);
562 * kernel/user space copy functions
565 xdi_copy_to_user(void *os_handle, void __user *dst, const void *src, int length)
567 if (copy_to_user(dst, src, length)) {
574 xdi_copy_from_user(void *os_handle, void *dst, const void __user *src, int length)
576 if (copy_from_user(dst, src, length)) {
583 * device node operations
585 static int divas_open(struct inode *inode, struct file *file)
590 static int divas_release(struct inode *inode, struct file *file)
592 if (file->private_data) {
593 diva_xdi_close_adapter(file->private_data, file);
598 static ssize_t divas_write(struct file *file, const char __user *buf,
599 size_t count, loff_t * ppos)
603 if (!file->private_data) {
604 file->private_data = diva_xdi_open_adapter(file, buf,
608 if (!file->private_data) {
612 ret = diva_xdi_write(file->private_data, file,
613 buf, count, xdi_copy_from_user);
615 case -1: /* Message should be removed from rx mailbox first */
618 case -2: /* invalid adapter was specified in this call */
625 DBG_TRC(("write: ret %d", ret));
629 static ssize_t divas_read(struct file *file, char __user *buf,
630 size_t count, loff_t * ppos)
634 if (!file->private_data) {
635 file->private_data = diva_xdi_open_adapter(file, buf,
639 if (!file->private_data) {
643 ret = diva_xdi_read(file->private_data, file,
644 buf, count, xdi_copy_to_user);
646 case -1: /* RX mailbox is empty */
649 case -2: /* no memory, mailbox was cleared, last command is failed */
652 case -3: /* can't copy to user, retry */
656 DBG_TRC(("read: ret %d", ret));
660 static unsigned int divas_poll(struct file *file, poll_table * wait)
662 if (!file->private_data) {
665 return (POLLIN | POLLRDNORM);
668 static struct file_operations divas_fops = {
669 .owner = THIS_MODULE,
672 .write = divas_write,
675 .release = divas_release
678 static void divas_unregister_chrdev(void)
680 unregister_chrdev(major, DEVNAME);
683 static int DIVA_INIT_FUNCTION divas_register_chrdev(void)
685 if ((major = register_chrdev(0, DEVNAME, &divas_fops)) < 0)
687 printk(KERN_ERR "%s: failed to create /dev entry.\n",
695 /* --------------------------------------------------------------------------
697 -------------------------------------------------------------------------- */
698 static int __devinit divas_init_one(struct pci_dev *pdev,
699 const struct pci_device_id *ent)
705 DBG_TRC(("%s bus: %08x fn: %08x insertion.\n",
706 CardProperties[ent->driver_data].Name,
707 pdev->bus->number, pdev->devfn))
708 printk(KERN_INFO "%s: %s bus: %08x fn: %08x insertion.\n",
709 DRIVERLNAME, CardProperties[ent->driver_data].Name,
710 pdev->bus->number, pdev->devfn);
712 if (pci_enable_device(pdev)) {
713 DBG_TRC(("%s: %s bus: %08x fn: %08x device init failed.\n",
715 CardProperties[ent->driver_data].Name,
719 "%s: %s bus: %08x fn: %08x device init failed.\n",
721 CardProperties[ent->driver_data].
722 Name, pdev->bus->number,
727 pci_set_master(pdev);
729 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
731 DBG_TRC(("%s: bus: %08x fn: %08x fix latency.\n",
732 DRIVERLNAME, pdev->bus->number, pdev->devfn))
734 "%s: bus: %08x fn: %08x fix latency.\n",
735 DRIVERLNAME, pdev->bus->number, pdev->devfn);
736 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, new_latency);
739 if (!(pdiva = diva_driver_add_card(pdev, ent->driver_data))) {
740 DBG_TRC(("%s: %s bus: %08x fn: %08x card init failed.\n",
742 CardProperties[ent->driver_data].Name,
746 "%s: %s bus: %08x fn: %08x card init failed.\n",
748 CardProperties[ent->driver_data].
749 Name, pdev->bus->number,
754 pci_set_drvdata(pdev, pdiva);
759 static void __devexit divas_remove_one(struct pci_dev *pdev)
761 void *pdiva = pci_get_drvdata(pdev);
763 DBG_TRC(("bus: %08x fn: %08x removal.\n",
764 pdev->bus->number, pdev->devfn))
765 printk(KERN_INFO "%s: bus: %08x fn: %08x removal.\n",
766 DRIVERLNAME, pdev->bus->number, pdev->devfn);
769 diva_driver_remove_card(pdiva);
774 /* --------------------------------------------------------------------------
775 Driver Load / Startup
776 -------------------------------------------------------------------------- */
777 static int DIVA_INIT_FUNCTION divas_init(void)
782 printk(KERN_INFO "%s\n", DRIVERNAME);
783 printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_DIVAS);
784 strcpy(tmprev, main_revision);
785 printk("%s Build: %s(%s)\n", getrev(tmprev),
786 diva_xdi_common_code_build, DIVA_BUILD);
787 printk(KERN_INFO "%s: support for: ", DRIVERLNAME);
788 #ifdef CONFIG_ISDN_DIVAS_BRIPCI
791 #ifdef CONFIG_ISDN_DIVAS_PRIPCI
794 printk("adapters\n");
796 if (!divasfunc_init(dbgmask)) {
797 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
803 if (!divas_register_chrdev()) {
811 if (!create_divas_proc()) {
814 divas_unregister_chrdev();
817 printk(KERN_ERR "%s: failed to create proc entry.\n",
823 if ((ret = pci_register_driver(&diva_pci_driver))) {
826 divas_unregister_chrdev();
829 printk(KERN_ERR "%s: failed to init pci driver.\n",
833 printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
839 /* --------------------------------------------------------------------------
841 -------------------------------------------------------------------------- */
842 static void DIVA_EXIT_FUNCTION divas_exit(void)
844 pci_unregister_driver(&diva_pci_driver);
846 divas_unregister_chrdev();
849 printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
852 module_init(divas_init);
853 module_exit(divas_exit);