Pull bugzilla-7897 into release branch
[sfrench/cifs-2.6.git] / drivers / isdn / hardware / eicon / divasmain.c
1 /* $Id: divasmain.c,v 1.55.4.6 2005/02/09 19:28:20 armin Exp $
2  *
3  * Low level driver for Eicon DIVA Server ISDN cards.
4  *
5  * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
6  * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
7  *
8  * This software may be used and distributed according to the terms
9  * of the GNU General Public License, incorporated herein by reference.
10  */
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <asm/uaccess.h>
16 #include <asm/io.h>
17 #include <linux/ioport.h>
18 #include <linux/workqueue.h>
19 #include <linux/pci.h>
20 #include <linux/smp_lock.h>
21 #include <linux/interrupt.h>
22 #include <linux/list.h>
23 #include <linux/poll.h>
24 #include <linux/kmod.h>
25
26 #include "platform.h"
27 #undef ID_MASK
28 #undef N_DATA
29 #include "pc.h"
30 #include "di_defs.h"
31 #include "divasync.h"
32 #include "diva.h"
33 #include "di.h"
34 #include "io.h"
35 #include "xdi_msg.h"
36 #include "xdi_adapter.h"
37 #include "xdi_vers.h"
38 #include "diva_dma.h"
39 #include "diva_pci.h"
40
41 static char *main_revision = "$Revision: 1.55.4.6 $";
42
43 static int major;
44
45 static int dbgmask;
46
47 MODULE_DESCRIPTION("Kernel driver for Eicon DIVA Server cards");
48 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
49 MODULE_LICENSE("GPL");
50
51 module_param(dbgmask, int, 0);
52 MODULE_PARM_DESC(dbgmask, "initial debug mask");
53
54 static char *DRIVERNAME =
55     "Eicon DIVA Server driver (http://www.melware.net)";
56 static char *DRIVERLNAME = "divas";
57 static char *DEVNAME = "Divas";
58 char *DRIVERRELEASE_DIVAS = "2.0";
59
60 extern irqreturn_t diva_os_irq_wrapper(int irq, void *context);
61 extern int create_divas_proc(void);
62 extern void remove_divas_proc(void);
63 extern void diva_get_vserial_number(PISDN_ADAPTER IoAdapter, char *buf);
64 extern int divasfunc_init(int dbgmask);
65 extern void divasfunc_exit(void);
66
67 typedef struct _diva_os_thread_dpc {
68         struct tasklet_struct divas_task;
69         diva_os_soft_isr_t *psoft_isr;
70 } diva_os_thread_dpc_t;
71
72 /* --------------------------------------------------------------------------
73     PCI driver interface section
74    -------------------------------------------------------------------------- */
75 /*
76   vendor, device        Vendor and device ID to match (or PCI_ANY_ID)
77   subvendor,    Subsystem vendor and device ID to match (or PCI_ANY_ID)
78   subdevice
79   class,                Device class to match. The class_mask tells which bits
80   class_mask    of the class are honored during the comparison.
81   driver_data   Data private to the driver.
82   */
83
84 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2)
85 #define PCI_DEVICE_ID_EICON_MAESTRAP_2       0xE015
86 #endif
87
88 #if !defined(PCI_DEVICE_ID_EICON_4BRI_VOIP)
89 #define PCI_DEVICE_ID_EICON_4BRI_VOIP        0xE016
90 #endif
91
92 #if !defined(PCI_DEVICE_ID_EICON_4BRI_2_VOIP)
93 #define PCI_DEVICE_ID_EICON_4BRI_2_VOIP      0xE017
94 #endif
95
96 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2)
97 #define PCI_DEVICE_ID_EICON_BRI2M_2          0xE018
98 #endif
99
100 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP)
101 #define PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP  0xE019
102 #endif
103
104 #if !defined(PCI_DEVICE_ID_EICON_2F)
105 #define PCI_DEVICE_ID_EICON_2F               0xE01A
106 #endif
107
108 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2_VOIP)
109 #define PCI_DEVICE_ID_EICON_BRI2M_2_VOIP     0xE01B
110 #endif
111
112 /*
113   This table should be sorted by PCI device ID
114   */
115 static struct pci_device_id divas_pci_tbl[] = {
116 /* Diva Server BRI-2M PCI 0xE010 */
117         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRA,
118          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_MAESTRA_PCI},
119 /* Diva Server 4BRI-8M PCI 0xE012 */
120         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ,
121          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_PCI},
122 /* Diva Server 4BRI-8M 2.0 PCI 0xE013 */
123         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAQ_U,
124          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_Q_8M_V2_PCI},
125 /* Diva Server PRI-30M PCI 0xE014 */
126         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP,
127          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_PCI},
128 /* Diva Server PRI 2.0 adapter 0xE015 */
129         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2,
130          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_P_30M_V2_PCI},
131 /* Diva Server Voice 4BRI-8M PCI 0xE016 */
132         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_VOIP,
133          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_PCI},
134 /* Diva Server Voice 4BRI-8M 2.0 PCI 0xE017 */
135         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_4BRI_2_VOIP,
136          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_Q_8M_V2_PCI},
137 /* Diva Server BRI-2M 2.0 PCI 0xE018 */
138         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2,
139          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2M_V2_PCI},
140 /* Diva Server Voice PRI 2.0 PCI 0xE019 */
141         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP,
142          PCI_ANY_ID, PCI_ANY_ID, 0, 0,
143          CARDTYPE_DIVASRV_VOICE_P_30M_V2_PCI},
144 /* Diva Server 2FX 0xE01A */
145         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_2F,
146          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_B_2F_PCI},
147 /* Diva Server Voice BRI-2M 2.0 PCI 0xE01B */
148         {PCI_VENDOR_ID_EICON, PCI_DEVICE_ID_EICON_BRI2M_2_VOIP,
149          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CARDTYPE_DIVASRV_VOICE_B_2M_V2_PCI},
150         {0,}                    /* 0 terminated list. */
151 };
152 MODULE_DEVICE_TABLE(pci, divas_pci_tbl);
153
154 static int divas_init_one(struct pci_dev *pdev,
155                           const struct pci_device_id *ent);
156 static void __devexit divas_remove_one(struct pci_dev *pdev);
157
158 static struct pci_driver diva_pci_driver = {
159         .name     = "divas",
160         .probe    = divas_init_one,
161         .remove   = __devexit_p(divas_remove_one),
162         .id_table = divas_pci_tbl,
163 };
164
165 /*********************************************************
166  ** little helper functions
167  *********************************************************/
168 static char *getrev(const char *revision)
169 {
170         char *rev;
171         char *p;
172         if ((p = strchr(revision, ':'))) {
173                 rev = p + 2;
174                 p = strchr(rev, '$');
175                 *--p = 0;
176         } else
177                 rev = "1.0";
178         return rev;
179 }
180
181 void diva_log_info(unsigned char *format, ...)
182 {
183         va_list args;
184         unsigned char line[160];
185
186         va_start(args, format);
187         vsnprintf(line, sizeof(line), format, args);
188         va_end(args);
189
190         printk(KERN_INFO "%s: %s\n", DRIVERLNAME, line);
191 }
192
193 void divas_get_version(char *p)
194 {
195         char tmprev[32];
196
197         strcpy(tmprev, main_revision);
198         sprintf(p, "%s: %s(%s) %s(%s) major=%d\n", DRIVERLNAME, DRIVERRELEASE_DIVAS,
199                 getrev(tmprev), diva_xdi_common_code_build, DIVA_BUILD, major);
200 }
201
202 /* --------------------------------------------------------------------------
203     PCI Bus services  
204    -------------------------------------------------------------------------- */
205 byte diva_os_get_pci_bus(void *pci_dev_handle)
206 {
207         struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
208         return ((byte) pdev->bus->number);
209 }
210
211 byte diva_os_get_pci_func(void *pci_dev_handle)
212 {
213         struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
214         return ((byte) pdev->devfn);
215 }
216
217 unsigned long divasa_get_pci_irq(unsigned char bus, unsigned char func,
218                                  void *pci_dev_handle)
219 {
220         unsigned char irq = 0;
221         struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
222
223         irq = dev->irq;
224
225         return ((unsigned long) irq);
226 }
227
228 unsigned long divasa_get_pci_bar(unsigned char bus, unsigned char func,
229                                  int bar, void *pci_dev_handle)
230 {
231         unsigned long ret = 0;
232         struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
233
234         if (bar < 6) {
235                 ret = dev->resource[bar].start;
236         }
237
238         DBG_TRC(("GOT BAR[%d]=%08x", bar, ret));
239
240         {
241                 unsigned long type = (ret & 0x00000001);
242                 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
243                         DBG_TRC(("  I/O"));
244                         ret &= PCI_BASE_ADDRESS_IO_MASK;
245                 } else {
246                         DBG_TRC(("  memory"));
247                         ret &= PCI_BASE_ADDRESS_MEM_MASK;
248                 }
249                 DBG_TRC(("  final=%08x", ret));
250         }
251
252         return (ret);
253 }
254
255 void PCIwrite(byte bus, byte func, int offset, void *data, int length,
256               void *pci_dev_handle)
257 {
258         struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
259
260         switch (length) {
261         case 1:         /* byte */
262                 pci_write_config_byte(dev, offset,
263                                       *(unsigned char *) data);
264                 break;
265         case 2:         /* word */
266                 pci_write_config_word(dev, offset,
267                                       *(unsigned short *) data);
268                 break;
269         case 4:         /* dword */
270                 pci_write_config_dword(dev, offset,
271                                        *(unsigned int *) data);
272                 break;
273
274         default:                /* buffer */
275                 if (!(length % 4) && !(length & 0x03)) {        /* Copy as dword */
276                         dword *p = (dword *) data;
277                         length /= 4;
278
279                         while (length--) {
280                                 pci_write_config_dword(dev, offset,
281                                                        *(unsigned int *)
282                                                        p++);
283                         }
284                 } else {        /* copy as byte stream */
285                         byte *p = (byte *) data;
286
287                         while (length--) {
288                                 pci_write_config_byte(dev, offset,
289                                                       *(unsigned char *)
290                                                       p++);
291                         }
292                 }
293         }
294 }
295
296 void PCIread(byte bus, byte func, int offset, void *data, int length,
297              void *pci_dev_handle)
298 {
299         struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
300
301         switch (length) {
302         case 1:         /* byte */
303                 pci_read_config_byte(dev, offset, (unsigned char *) data);
304                 break;
305         case 2:         /* word */
306                 pci_read_config_word(dev, offset, (unsigned short *) data);
307                 break;
308         case 4:         /* dword */
309                 pci_read_config_dword(dev, offset, (unsigned int *) data);
310                 break;
311
312         default:                /* buffer */
313                 if (!(length % 4) && !(length & 0x03)) {        /* Copy as dword */
314                         dword *p = (dword *) data;
315                         length /= 4;
316
317                         while (length--) {
318                                 pci_read_config_dword(dev, offset,
319                                                       (unsigned int *)
320                                                       p++);
321                         }
322                 } else {        /* copy as byte stream */
323                         byte *p = (byte *) data;
324
325                         while (length--) {
326                                 pci_read_config_byte(dev, offset,
327                                                      (unsigned char *)
328                                                      p++);
329                         }
330                 }
331         }
332 }
333
334 /*
335   Init map with DMA pages. It is not problem if some allocations fail -
336   the channels that will not get one DMA page will use standard PIO
337   interface
338   */
339 static void *diva_pci_alloc_consistent(struct pci_dev *hwdev,
340                                        size_t size,
341                                        dma_addr_t * dma_handle,
342                                        void **addr_handle)
343 {
344         void *addr = pci_alloc_consistent(hwdev, size, dma_handle);
345
346         *addr_handle = addr;
347
348         return (addr);
349 }
350
351 void diva_init_dma_map(void *hdev,
352                        struct _diva_dma_map_entry **ppmap, int nentries)
353 {
354         struct pci_dev *pdev = (struct pci_dev *) hdev;
355         struct _diva_dma_map_entry *pmap =
356             diva_alloc_dma_map(hdev, nentries);
357
358         if (pmap) {
359                 int i;
360                 dma_addr_t dma_handle;
361                 void *cpu_addr;
362                 void *addr_handle;
363
364                 for (i = 0; i < nentries; i++) {
365                         if (!(cpu_addr = diva_pci_alloc_consistent(pdev,
366                                                                    PAGE_SIZE,
367                                                                    &dma_handle,
368                                                                    &addr_handle)))
369                         {
370                                 break;
371                         }
372                         diva_init_dma_map_entry(pmap, i, cpu_addr,
373                                                 (dword) dma_handle,
374                                                 addr_handle);
375                         DBG_TRC(("dma map alloc [%d]=(%08lx:%08x:%08lx)",
376                                  i, (unsigned long) cpu_addr,
377                                  (dword) dma_handle,
378                                  (unsigned long) addr_handle))}
379         }
380
381         *ppmap = pmap;
382 }
383
384 /*
385   Free all contained in the map entries and memory used by the map
386   Should be always called after adapter removal from DIDD array
387   */
388 void diva_free_dma_map(void *hdev, struct _diva_dma_map_entry *pmap)
389 {
390         struct pci_dev *pdev = (struct pci_dev *) hdev;
391         int i;
392         dword phys_addr;
393         void *cpu_addr;
394         dma_addr_t dma_handle;
395         void *addr_handle;
396
397         for (i = 0; (pmap != 0); i++) {
398                 diva_get_dma_map_entry(pmap, i, &cpu_addr, &phys_addr);
399                 if (!cpu_addr) {
400                         break;
401                 }
402                 addr_handle = diva_get_entry_handle(pmap, i);
403                 dma_handle = (dma_addr_t) phys_addr;
404                 pci_free_consistent(pdev, PAGE_SIZE, addr_handle,
405                                     dma_handle);
406                 DBG_TRC(("dma map free [%d]=(%08lx:%08x:%08lx)", i,
407                          (unsigned long) cpu_addr, (dword) dma_handle,
408                          (unsigned long) addr_handle))
409         }
410
411         diva_free_dma_mapping(pmap);
412 }
413
414
415 /*********************************************************
416  ** I/O port utilities  
417  *********************************************************/
418
419 int
420 diva_os_register_io_port(void *adapter, int on, unsigned long port,
421                          unsigned long length, const char *name, int id)
422 {
423         if (on) {
424                 if (!request_region(port, length, name)) {
425                         DBG_ERR(("A: I/O: can't register port=%08x", port))
426                         return (-1);
427                 }
428         } else {
429                 release_region(port, length);
430         }
431         return (0);
432 }
433
434 void __iomem *divasa_remap_pci_bar(diva_os_xdi_adapter_t *a, int id, unsigned long bar, unsigned long area_length)
435 {
436         void __iomem *ret = ioremap(bar, area_length);
437         DBG_TRC(("remap(%08x)->%p", bar, ret));
438         return (ret);
439 }
440
441 void divasa_unmap_pci_bar(void __iomem *bar)
442 {
443         if (bar) {
444                 iounmap(bar);
445         }
446 }
447
448 /*********************************************************
449  ** I/O port access 
450  *********************************************************/
451 byte __inline__ inpp(void __iomem *addr)
452 {
453         return (inb((unsigned long) addr));
454 }
455
456 word __inline__ inppw(void __iomem *addr)
457 {
458         return (inw((unsigned long) addr));
459 }
460
461 void __inline__ inppw_buffer(void __iomem *addr, void *P, int length)
462 {
463         insw((unsigned long) addr, (word *) P, length >> 1);
464 }
465
466 void __inline__ outppw_buffer(void __iomem *addr, void *P, int length)
467 {
468         outsw((unsigned long) addr, (word *) P, length >> 1);
469 }
470
471 void __inline__ outppw(void __iomem *addr, word w)
472 {
473         outw(w, (unsigned long) addr);
474 }
475
476 void __inline__ outpp(void __iomem *addr, word p)
477 {
478         outb(p, (unsigned long) addr);
479 }
480
481 /* --------------------------------------------------------------------------
482     IRQ request / remove  
483    -------------------------------------------------------------------------- */
484 int diva_os_register_irq(void *context, byte irq, const char *name)
485 {
486         int result = request_irq(irq, diva_os_irq_wrapper,
487                                  IRQF_DISABLED | IRQF_SHARED, name, context);
488         return (result);
489 }
490
491 void diva_os_remove_irq(void *context, byte irq)
492 {
493         free_irq(irq, context);
494 }
495
496 /* --------------------------------------------------------------------------
497     DPC framework implementation
498    -------------------------------------------------------------------------- */
499 static void diva_os_dpc_proc(unsigned long context)
500 {
501         diva_os_thread_dpc_t *psoft_isr = (diva_os_thread_dpc_t *) context;
502         diva_os_soft_isr_t *pisr = psoft_isr->psoft_isr;
503
504         (*(pisr->callback)) (pisr, pisr->callback_context);
505 }
506
507 int diva_os_initialize_soft_isr(diva_os_soft_isr_t * psoft_isr,
508                                 diva_os_soft_isr_callback_t callback,
509                                 void *callback_context)
510 {
511         diva_os_thread_dpc_t *pdpc;
512
513         pdpc = (diva_os_thread_dpc_t *) diva_os_malloc(0, sizeof(*pdpc));
514         if (!(psoft_isr->object = pdpc)) {
515                 return (-1);
516         }
517         memset(pdpc, 0x00, sizeof(*pdpc));
518         psoft_isr->callback = callback;
519         psoft_isr->callback_context = callback_context;
520         pdpc->psoft_isr = psoft_isr;
521         tasklet_init(&pdpc->divas_task, diva_os_dpc_proc, (unsigned long)pdpc);
522
523         return (0);
524 }
525
526 int diva_os_schedule_soft_isr(diva_os_soft_isr_t * psoft_isr)
527 {
528         if (psoft_isr && psoft_isr->object) {
529                 diva_os_thread_dpc_t *pdpc =
530                     (diva_os_thread_dpc_t *) psoft_isr->object;
531
532                 tasklet_schedule(&pdpc->divas_task);
533         }
534
535         return (1);
536 }
537
538 int diva_os_cancel_soft_isr(diva_os_soft_isr_t * psoft_isr)
539 {
540         return (0);
541 }
542
543 void diva_os_remove_soft_isr(diva_os_soft_isr_t * psoft_isr)
544 {
545         if (psoft_isr && psoft_isr->object) {
546                 diva_os_thread_dpc_t *pdpc =
547                     (diva_os_thread_dpc_t *) psoft_isr->object;
548                 void *mem;
549
550                 tasklet_kill(&pdpc->divas_task);
551                 flush_scheduled_work();
552                 mem = psoft_isr->object;
553                 psoft_isr->object = NULL;
554                 diva_os_free(0, mem);
555         }
556 }
557
558 /*
559  * kernel/user space copy functions
560  */
561 static int
562 xdi_copy_to_user(void *os_handle, void __user *dst, const void *src, int length)
563 {
564         if (copy_to_user(dst, src, length)) {
565                 return (-EFAULT);
566         }
567         return (length);
568 }
569
570 static int
571 xdi_copy_from_user(void *os_handle, void *dst, const void __user *src, int length)
572 {
573         if (copy_from_user(dst, src, length)) {
574                 return (-EFAULT);
575         }
576         return (length);
577 }
578
579 /*
580  * device node operations
581  */
582 static int divas_open(struct inode *inode, struct file *file)
583 {
584         return (0);
585 }
586
587 static int divas_release(struct inode *inode, struct file *file)
588 {
589         if (file->private_data) {
590                 diva_xdi_close_adapter(file->private_data, file);
591         }
592         return (0);
593 }
594
595 static ssize_t divas_write(struct file *file, const char __user *buf,
596                            size_t count, loff_t * ppos)
597 {
598         int ret = -EINVAL;
599
600         if (!file->private_data) {
601                 file->private_data = diva_xdi_open_adapter(file, buf,
602                                                            count,
603                                                            xdi_copy_from_user);
604         }
605         if (!file->private_data) {
606                 return (-ENODEV);
607         }
608
609         ret = diva_xdi_write(file->private_data, file,
610                              buf, count, xdi_copy_from_user);
611         switch (ret) {
612         case -1:                /* Message should be removed from rx mailbox first */
613                 ret = -EBUSY;
614                 break;
615         case -2:                /* invalid adapter was specified in this call */
616                 ret = -ENOMEM;
617                 break;
618         case -3:
619                 ret = -ENXIO;
620                 break;
621         }
622         DBG_TRC(("write: ret %d", ret));
623         return (ret);
624 }
625
626 static ssize_t divas_read(struct file *file, char __user *buf,
627                           size_t count, loff_t * ppos)
628 {
629         int ret = -EINVAL;
630
631         if (!file->private_data) {
632                 file->private_data = diva_xdi_open_adapter(file, buf,
633                                                            count,
634                                                            xdi_copy_from_user);
635         }
636         if (!file->private_data) {
637                 return (-ENODEV);
638         }
639
640         ret = diva_xdi_read(file->private_data, file,
641                             buf, count, xdi_copy_to_user);
642         switch (ret) {
643         case -1:                /* RX mailbox is empty */
644                 ret = -EAGAIN;
645                 break;
646         case -2:                /* no memory, mailbox was cleared, last command is failed */
647                 ret = -ENOMEM;
648                 break;
649         case -3:                /* can't copy to user, retry */
650                 ret = -EFAULT;
651                 break;
652         }
653         DBG_TRC(("read: ret %d", ret));
654         return (ret);
655 }
656
657 static unsigned int divas_poll(struct file *file, poll_table * wait)
658 {
659         if (!file->private_data) {
660                 return (POLLERR);
661         }
662         return (POLLIN | POLLRDNORM);
663 }
664
665 static const struct file_operations divas_fops = {
666         .owner   = THIS_MODULE,
667         .llseek  = no_llseek,
668         .read    = divas_read,
669         .write   = divas_write,
670         .poll    = divas_poll,
671         .open    = divas_open,
672         .release = divas_release
673 };
674
675 static void divas_unregister_chrdev(void)
676 {
677         unregister_chrdev(major, DEVNAME);
678 }
679
680 static int DIVA_INIT_FUNCTION divas_register_chrdev(void)
681 {
682         if ((major = register_chrdev(0, DEVNAME, &divas_fops)) < 0)
683         {
684                 printk(KERN_ERR "%s: failed to create /dev entry.\n",
685                        DRIVERLNAME);
686                 return (0);
687         }
688
689         return (1);
690 }
691
692 /* --------------------------------------------------------------------------
693     PCI driver section
694    -------------------------------------------------------------------------- */
695 static int __devinit divas_init_one(struct pci_dev *pdev,
696                                     const struct pci_device_id *ent)
697 {
698         void *pdiva = NULL;
699         u8 pci_latency;
700         u8 new_latency = 32;
701
702         DBG_TRC(("%s bus: %08x fn: %08x insertion.\n",
703                  CardProperties[ent->driver_data].Name,
704                  pdev->bus->number, pdev->devfn))
705         printk(KERN_INFO "%s: %s bus: %08x fn: %08x insertion.\n",
706                 DRIVERLNAME, CardProperties[ent->driver_data].Name,
707                 pdev->bus->number, pdev->devfn);
708
709         if (pci_enable_device(pdev)) {
710                 DBG_TRC(("%s: %s bus: %08x fn: %08x device init failed.\n",
711                          DRIVERLNAME,
712                          CardProperties[ent->driver_data].Name,
713                          pdev->bus->number,
714                          pdev->devfn))
715                 printk(KERN_ERR
716                         "%s: %s bus: %08x fn: %08x device init failed.\n",
717                         DRIVERLNAME,
718                         CardProperties[ent->driver_data].
719                         Name, pdev->bus->number,
720                         pdev->devfn);
721                 return (-EIO);
722         }
723
724         pci_set_master(pdev);
725
726         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
727         if (!pci_latency) {
728                 DBG_TRC(("%s: bus: %08x fn: %08x fix latency.\n",
729                          DRIVERLNAME, pdev->bus->number, pdev->devfn))
730                 printk(KERN_INFO
731                         "%s: bus: %08x fn: %08x fix latency.\n",
732                          DRIVERLNAME, pdev->bus->number, pdev->devfn);
733                 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, new_latency);
734         }
735
736         if (!(pdiva = diva_driver_add_card(pdev, ent->driver_data))) {
737                 DBG_TRC(("%s: %s bus: %08x fn: %08x card init failed.\n",
738                          DRIVERLNAME,
739                          CardProperties[ent->driver_data].Name,
740                          pdev->bus->number,
741                          pdev->devfn))
742                 printk(KERN_ERR
743                         "%s: %s bus: %08x fn: %08x card init failed.\n",
744                         DRIVERLNAME,
745                         CardProperties[ent->driver_data].
746                         Name, pdev->bus->number,
747                         pdev->devfn);
748                 return (-EIO);
749         }
750
751         pci_set_drvdata(pdev, pdiva);
752
753         return (0);
754 }
755
756 static void __devexit divas_remove_one(struct pci_dev *pdev)
757 {
758         void *pdiva = pci_get_drvdata(pdev);
759
760         DBG_TRC(("bus: %08x fn: %08x removal.\n",
761                  pdev->bus->number, pdev->devfn))
762         printk(KERN_INFO "%s: bus: %08x fn: %08x removal.\n",
763                 DRIVERLNAME, pdev->bus->number, pdev->devfn);
764
765         if (pdiva) {
766                 diva_driver_remove_card(pdiva);
767         }
768
769 }
770
771 /* --------------------------------------------------------------------------
772     Driver Load / Startup  
773    -------------------------------------------------------------------------- */
774 static int DIVA_INIT_FUNCTION divas_init(void)
775 {
776         char tmprev[50];
777         int ret = 0;
778
779         printk(KERN_INFO "%s\n", DRIVERNAME);
780         printk(KERN_INFO "%s: Rel:%s  Rev:", DRIVERLNAME, DRIVERRELEASE_DIVAS);
781         strcpy(tmprev, main_revision);
782         printk("%s  Build: %s(%s)\n", getrev(tmprev),
783                diva_xdi_common_code_build, DIVA_BUILD);
784         printk(KERN_INFO "%s: support for: ", DRIVERLNAME);
785 #ifdef CONFIG_ISDN_DIVAS_BRIPCI
786         printk("BRI/PCI ");
787 #endif
788 #ifdef CONFIG_ISDN_DIVAS_PRIPCI
789         printk("PRI/PCI ");
790 #endif
791         printk("adapters\n");
792
793         if (!divasfunc_init(dbgmask)) {
794                 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
795                        DRIVERLNAME);
796                 ret = -EIO;
797                 goto out;
798         }
799
800         if (!divas_register_chrdev()) {
801 #ifdef MODULE
802                 divasfunc_exit();
803 #endif
804                 ret = -EIO;
805                 goto out;
806         }
807
808         if (!create_divas_proc()) {
809 #ifdef MODULE
810                 remove_divas_proc();
811                 divas_unregister_chrdev();
812                 divasfunc_exit();
813 #endif
814                 printk(KERN_ERR "%s: failed to create proc entry.\n",
815                        DRIVERLNAME);
816                 ret = -EIO;
817                 goto out;
818         }
819
820         if ((ret = pci_register_driver(&diva_pci_driver))) {
821 #ifdef MODULE
822                 remove_divas_proc();
823                 divas_unregister_chrdev();
824                 divasfunc_exit();
825 #endif
826                 printk(KERN_ERR "%s: failed to init pci driver.\n",
827                        DRIVERLNAME);
828                 goto out;
829         }
830         printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
831
832       out:
833         return (ret);
834 }
835
836 /* --------------------------------------------------------------------------
837     Driver Unload
838    -------------------------------------------------------------------------- */
839 static void DIVA_EXIT_FUNCTION divas_exit(void)
840 {
841         pci_unregister_driver(&diva_pci_driver);
842         remove_divas_proc();
843         divas_unregister_chrdev();
844         divasfunc_exit();
845
846         printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
847 }
848
849 module_init(divas_init);
850 module_exit(divas_exit);