[PATCH] devfs: Remove the devfs_fs_kernel.h file from the tree
[safe/jmp/linux-2.6] / 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/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>
18 #include <asm/io.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>
27
28 #include "platform.h"
29 #undef ID_MASK
30 #undef N_DATA
31 #include "pc.h"
32 #include "di_defs.h"
33 #include "divasync.h"
34 #include "diva.h"
35 #include "di.h"
36 #include "io.h"
37 #include "xdi_msg.h"
38 #include "xdi_adapter.h"
39 #include "xdi_vers.h"
40 #include "diva_dma.h"
41 #include "diva_pci.h"
42
43 static char *main_revision = "$Revision: 1.55.4.6 $";
44
45 static int major;
46
47 static int dbgmask;
48
49 MODULE_DESCRIPTION("Kernel driver for Eicon DIVA Server cards");
50 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
51 MODULE_LICENSE("GPL");
52
53 module_param(dbgmask, int, 0);
54 MODULE_PARM_DESC(dbgmask, "initial debug mask");
55
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";
61
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);
69
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;
74
75 /* --------------------------------------------------------------------------
76     PCI driver interface section
77    -------------------------------------------------------------------------- */
78 /*
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)
81   subdevice
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.
85   */
86
87 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2)
88 #define PCI_DEVICE_ID_EICON_MAESTRAP_2       0xE015
89 #endif
90
91 #if !defined(PCI_DEVICE_ID_EICON_4BRI_VOIP)
92 #define PCI_DEVICE_ID_EICON_4BRI_VOIP        0xE016
93 #endif
94
95 #if !defined(PCI_DEVICE_ID_EICON_4BRI_2_VOIP)
96 #define PCI_DEVICE_ID_EICON_4BRI_2_VOIP      0xE017
97 #endif
98
99 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2)
100 #define PCI_DEVICE_ID_EICON_BRI2M_2          0xE018
101 #endif
102
103 #if !defined(PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP)
104 #define PCI_DEVICE_ID_EICON_MAESTRAP_2_VOIP  0xE019
105 #endif
106
107 #if !defined(PCI_DEVICE_ID_EICON_2F)
108 #define PCI_DEVICE_ID_EICON_2F               0xE01A
109 #endif
110
111 #if !defined(PCI_DEVICE_ID_EICON_BRI2M_2_VOIP)
112 #define PCI_DEVICE_ID_EICON_BRI2M_2_VOIP     0xE01B
113 #endif
114
115 /*
116   This table should be sorted by PCI device ID
117   */
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. */
154 };
155 MODULE_DEVICE_TABLE(pci, divas_pci_tbl);
156
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);
160
161 static struct pci_driver diva_pci_driver = {
162         .name     = "divas",
163         .probe    = divas_init_one,
164         .remove   = __devexit_p(divas_remove_one),
165         .id_table = divas_pci_tbl,
166 };
167
168 /*********************************************************
169  ** little helper functions
170  *********************************************************/
171 static char *getrev(const char *revision)
172 {
173         char *rev;
174         char *p;
175         if ((p = strchr(revision, ':'))) {
176                 rev = p + 2;
177                 p = strchr(rev, '$');
178                 *--p = 0;
179         } else
180                 rev = "1.0";
181         return rev;
182 }
183
184 void diva_log_info(unsigned char *format, ...)
185 {
186         va_list args;
187         unsigned char line[160];
188
189         va_start(args, format);
190         vsprintf(line, format, args);
191         va_end(args);
192
193         printk(KERN_INFO "%s: %s\n", DRIVERLNAME, line);
194 }
195
196 void divas_get_version(char *p)
197 {
198         char tmprev[32];
199
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);
203 }
204
205 /* --------------------------------------------------------------------------
206     PCI Bus services  
207    -------------------------------------------------------------------------- */
208 byte diva_os_get_pci_bus(void *pci_dev_handle)
209 {
210         struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
211         return ((byte) pdev->bus->number);
212 }
213
214 byte diva_os_get_pci_func(void *pci_dev_handle)
215 {
216         struct pci_dev *pdev = (struct pci_dev *) pci_dev_handle;
217         return ((byte) pdev->devfn);
218 }
219
220 unsigned long divasa_get_pci_irq(unsigned char bus, unsigned char func,
221                                  void *pci_dev_handle)
222 {
223         unsigned char irq = 0;
224         struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
225
226         irq = dev->irq;
227
228         return ((unsigned long) irq);
229 }
230
231 unsigned long divasa_get_pci_bar(unsigned char bus, unsigned char func,
232                                  int bar, void *pci_dev_handle)
233 {
234         unsigned long ret = 0;
235         struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
236
237         if (bar < 6) {
238                 ret = dev->resource[bar].start;
239         }
240
241         DBG_TRC(("GOT BAR[%d]=%08x", bar, ret));
242
243         {
244                 unsigned long type = (ret & 0x00000001);
245                 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
246                         DBG_TRC(("  I/O"));
247                         ret &= PCI_BASE_ADDRESS_IO_MASK;
248                 } else {
249                         DBG_TRC(("  memory"));
250                         ret &= PCI_BASE_ADDRESS_MEM_MASK;
251                 }
252                 DBG_TRC(("  final=%08x", ret));
253         }
254
255         return (ret);
256 }
257
258 void PCIwrite(byte bus, byte func, int offset, void *data, int length,
259               void *pci_dev_handle)
260 {
261         struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
262
263         switch (length) {
264         case 1:         /* byte */
265                 pci_write_config_byte(dev, offset,
266                                       *(unsigned char *) data);
267                 break;
268         case 2:         /* word */
269                 pci_write_config_word(dev, offset,
270                                       *(unsigned short *) data);
271                 break;
272         case 4:         /* dword */
273                 pci_write_config_dword(dev, offset,
274                                        *(unsigned int *) data);
275                 break;
276
277         default:                /* buffer */
278                 if (!(length % 4) && !(length & 0x03)) {        /* Copy as dword */
279                         dword *p = (dword *) data;
280                         length /= 4;
281
282                         while (length--) {
283                                 pci_write_config_dword(dev, offset,
284                                                        *(unsigned int *)
285                                                        p++);
286                         }
287                 } else {        /* copy as byte stream */
288                         byte *p = (byte *) data;
289
290                         while (length--) {
291                                 pci_write_config_byte(dev, offset,
292                                                       *(unsigned char *)
293                                                       p++);
294                         }
295                 }
296         }
297 }
298
299 void PCIread(byte bus, byte func, int offset, void *data, int length,
300              void *pci_dev_handle)
301 {
302         struct pci_dev *dev = (struct pci_dev *) pci_dev_handle;
303
304         switch (length) {
305         case 1:         /* byte */
306                 pci_read_config_byte(dev, offset, (unsigned char *) data);
307                 break;
308         case 2:         /* word */
309                 pci_read_config_word(dev, offset, (unsigned short *) data);
310                 break;
311         case 4:         /* dword */
312                 pci_read_config_dword(dev, offset, (unsigned int *) data);
313                 break;
314
315         default:                /* buffer */
316                 if (!(length % 4) && !(length & 0x03)) {        /* Copy as dword */
317                         dword *p = (dword *) data;
318                         length /= 4;
319
320                         while (length--) {
321                                 pci_read_config_dword(dev, offset,
322                                                       (unsigned int *)
323                                                       p++);
324                         }
325                 } else {        /* copy as byte stream */
326                         byte *p = (byte *) data;
327
328                         while (length--) {
329                                 pci_read_config_byte(dev, offset,
330                                                      (unsigned char *)
331                                                      p++);
332                         }
333                 }
334         }
335 }
336
337 /*
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
340   interface
341   */
342 static void *diva_pci_alloc_consistent(struct pci_dev *hwdev,
343                                        size_t size,
344                                        dma_addr_t * dma_handle,
345                                        void **addr_handle)
346 {
347         void *addr = pci_alloc_consistent(hwdev, size, dma_handle);
348
349         *addr_handle = addr;
350
351         return (addr);
352 }
353
354 void diva_init_dma_map(void *hdev,
355                        struct _diva_dma_map_entry **ppmap, int nentries)
356 {
357         struct pci_dev *pdev = (struct pci_dev *) hdev;
358         struct _diva_dma_map_entry *pmap =
359             diva_alloc_dma_map(hdev, nentries);
360
361         if (pmap) {
362                 int i;
363                 dma_addr_t dma_handle;
364                 void *cpu_addr;
365                 void *addr_handle;
366
367                 for (i = 0; i < nentries; i++) {
368                         if (!(cpu_addr = diva_pci_alloc_consistent(pdev,
369                                                                    PAGE_SIZE,
370                                                                    &dma_handle,
371                                                                    &addr_handle)))
372                         {
373                                 break;
374                         }
375                         diva_init_dma_map_entry(pmap, i, cpu_addr,
376                                                 (dword) dma_handle,
377                                                 addr_handle);
378                         DBG_TRC(("dma map alloc [%d]=(%08lx:%08x:%08lx)",
379                                  i, (unsigned long) cpu_addr,
380                                  (dword) dma_handle,
381                                  (unsigned long) addr_handle))}
382         }
383
384         *ppmap = pmap;
385 }
386
387 /*
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
390   */
391 void diva_free_dma_map(void *hdev, struct _diva_dma_map_entry *pmap)
392 {
393         struct pci_dev *pdev = (struct pci_dev *) hdev;
394         int i;
395         dword phys_addr;
396         void *cpu_addr;
397         dma_addr_t dma_handle;
398         void *addr_handle;
399
400         for (i = 0; (pmap != 0); i++) {
401                 diva_get_dma_map_entry(pmap, i, &cpu_addr, &phys_addr);
402                 if (!cpu_addr) {
403                         break;
404                 }
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,
408                                     dma_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))
412         }
413
414         diva_free_dma_mapping(pmap);
415 }
416
417
418 /*********************************************************
419  ** I/O port utilities  
420  *********************************************************/
421
422 int
423 diva_os_register_io_port(void *adapter, int on, unsigned long port,
424                          unsigned long length, const char *name, int id)
425 {
426         if (on) {
427                 if (!request_region(port, length, name)) {
428                         DBG_ERR(("A: I/O: can't register port=%08x", port))
429                         return (-1);
430                 }
431         } else {
432                 release_region(port, length);
433         }
434         return (0);
435 }
436
437 void __iomem *divasa_remap_pci_bar(diva_os_xdi_adapter_t *a, int id, unsigned long bar, unsigned long area_length)
438 {
439         void __iomem *ret = ioremap(bar, area_length);
440         DBG_TRC(("remap(%08x)->%p", bar, ret));
441         return (ret);
442 }
443
444 void divasa_unmap_pci_bar(void __iomem *bar)
445 {
446         if (bar) {
447                 iounmap(bar);
448         }
449 }
450
451 /*********************************************************
452  ** I/O port access 
453  *********************************************************/
454 byte __inline__ inpp(void __iomem *addr)
455 {
456         return (inb((unsigned long) addr));
457 }
458
459 word __inline__ inppw(void __iomem *addr)
460 {
461         return (inw((unsigned long) addr));
462 }
463
464 void __inline__ inppw_buffer(void __iomem *addr, void *P, int length)
465 {
466         insw((unsigned long) addr, (word *) P, length >> 1);
467 }
468
469 void __inline__ outppw_buffer(void __iomem *addr, void *P, int length)
470 {
471         outsw((unsigned long) addr, (word *) P, length >> 1);
472 }
473
474 void __inline__ outppw(void __iomem *addr, word w)
475 {
476         outw(w, (unsigned long) addr);
477 }
478
479 void __inline__ outpp(void __iomem *addr, word p)
480 {
481         outb(p, (unsigned long) addr);
482 }
483
484 /* --------------------------------------------------------------------------
485     IRQ request / remove  
486    -------------------------------------------------------------------------- */
487 int diva_os_register_irq(void *context, byte irq, const char *name)
488 {
489         int result = request_irq(irq, diva_os_irq_wrapper,
490                                  SA_INTERRUPT | SA_SHIRQ, name, context);
491         return (result);
492 }
493
494 void diva_os_remove_irq(void *context, byte irq)
495 {
496         free_irq(irq, context);
497 }
498
499 /* --------------------------------------------------------------------------
500     DPC framework implementation
501    -------------------------------------------------------------------------- */
502 static void diva_os_dpc_proc(unsigned long context)
503 {
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;
506
507         (*(pisr->callback)) (pisr, pisr->callback_context);
508 }
509
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)
513 {
514         diva_os_thread_dpc_t *pdpc;
515
516         pdpc = (diva_os_thread_dpc_t *) diva_os_malloc(0, sizeof(*pdpc));
517         if (!(psoft_isr->object = pdpc)) {
518                 return (-1);
519         }
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);
525
526         return (0);
527 }
528
529 int diva_os_schedule_soft_isr(diva_os_soft_isr_t * psoft_isr)
530 {
531         if (psoft_isr && psoft_isr->object) {
532                 diva_os_thread_dpc_t *pdpc =
533                     (diva_os_thread_dpc_t *) psoft_isr->object;
534
535                 tasklet_schedule(&pdpc->divas_task);
536         }
537
538         return (1);
539 }
540
541 int diva_os_cancel_soft_isr(diva_os_soft_isr_t * psoft_isr)
542 {
543         return (0);
544 }
545
546 void diva_os_remove_soft_isr(diva_os_soft_isr_t * psoft_isr)
547 {
548         if (psoft_isr && psoft_isr->object) {
549                 diva_os_thread_dpc_t *pdpc =
550                     (diva_os_thread_dpc_t *) psoft_isr->object;
551                 void *mem;
552
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);
558         }
559 }
560
561 /*
562  * kernel/user space copy functions
563  */
564 static int
565 xdi_copy_to_user(void *os_handle, void __user *dst, const void *src, int length)
566 {
567         if (copy_to_user(dst, src, length)) {
568                 return (-EFAULT);
569         }
570         return (length);
571 }
572
573 static int
574 xdi_copy_from_user(void *os_handle, void *dst, const void __user *src, int length)
575 {
576         if (copy_from_user(dst, src, length)) {
577                 return (-EFAULT);
578         }
579         return (length);
580 }
581
582 /*
583  * device node operations
584  */
585 static int divas_open(struct inode *inode, struct file *file)
586 {
587         return (0);
588 }
589
590 static int divas_release(struct inode *inode, struct file *file)
591 {
592         if (file->private_data) {
593                 diva_xdi_close_adapter(file->private_data, file);
594         }
595         return (0);
596 }
597
598 static ssize_t divas_write(struct file *file, const char __user *buf,
599                            size_t count, loff_t * ppos)
600 {
601         int ret = -EINVAL;
602
603         if (!file->private_data) {
604                 file->private_data = diva_xdi_open_adapter(file, buf,
605                                                            count,
606                                                            xdi_copy_from_user);
607         }
608         if (!file->private_data) {
609                 return (-ENODEV);
610         }
611
612         ret = diva_xdi_write(file->private_data, file,
613                              buf, count, xdi_copy_from_user);
614         switch (ret) {
615         case -1:                /* Message should be removed from rx mailbox first */
616                 ret = -EBUSY;
617                 break;
618         case -2:                /* invalid adapter was specified in this call */
619                 ret = -ENOMEM;
620                 break;
621         case -3:
622                 ret = -ENXIO;
623                 break;
624         }
625         DBG_TRC(("write: ret %d", ret));
626         return (ret);
627 }
628
629 static ssize_t divas_read(struct file *file, char __user *buf,
630                           size_t count, loff_t * ppos)
631 {
632         int ret = -EINVAL;
633
634         if (!file->private_data) {
635                 file->private_data = diva_xdi_open_adapter(file, buf,
636                                                            count,
637                                                            xdi_copy_from_user);
638         }
639         if (!file->private_data) {
640                 return (-ENODEV);
641         }
642
643         ret = diva_xdi_read(file->private_data, file,
644                             buf, count, xdi_copy_to_user);
645         switch (ret) {
646         case -1:                /* RX mailbox is empty */
647                 ret = -EAGAIN;
648                 break;
649         case -2:                /* no memory, mailbox was cleared, last command is failed */
650                 ret = -ENOMEM;
651                 break;
652         case -3:                /* can't copy to user, retry */
653                 ret = -EFAULT;
654                 break;
655         }
656         DBG_TRC(("read: ret %d", ret));
657         return (ret);
658 }
659
660 static unsigned int divas_poll(struct file *file, poll_table * wait)
661 {
662         if (!file->private_data) {
663                 return (POLLERR);
664         }
665         return (POLLIN | POLLRDNORM);
666 }
667
668 static struct file_operations divas_fops = {
669         .owner   = THIS_MODULE,
670         .llseek  = no_llseek,
671         .read    = divas_read,
672         .write   = divas_write,
673         .poll    = divas_poll,
674         .open    = divas_open,
675         .release = divas_release
676 };
677
678 static void divas_unregister_chrdev(void)
679 {
680         unregister_chrdev(major, DEVNAME);
681 }
682
683 static int DIVA_INIT_FUNCTION divas_register_chrdev(void)
684 {
685         if ((major = register_chrdev(0, DEVNAME, &divas_fops)) < 0)
686         {
687                 printk(KERN_ERR "%s: failed to create /dev entry.\n",
688                        DRIVERLNAME);
689                 return (0);
690         }
691
692         return (1);
693 }
694
695 /* --------------------------------------------------------------------------
696     PCI driver section
697    -------------------------------------------------------------------------- */
698 static int __devinit divas_init_one(struct pci_dev *pdev,
699                                     const struct pci_device_id *ent)
700 {
701         void *pdiva = NULL;
702         u8 pci_latency;
703         u8 new_latency = 32;
704
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);
711
712         if (pci_enable_device(pdev)) {
713                 DBG_TRC(("%s: %s bus: %08x fn: %08x device init failed.\n",
714                          DRIVERLNAME,
715                          CardProperties[ent->driver_data].Name,
716                          pdev->bus->number,
717                          pdev->devfn))
718                 printk(KERN_ERR
719                         "%s: %s bus: %08x fn: %08x device init failed.\n",
720                         DRIVERLNAME,
721                         CardProperties[ent->driver_data].
722                         Name, pdev->bus->number,
723                         pdev->devfn);
724                 return (-EIO);
725         }
726
727         pci_set_master(pdev);
728
729         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
730         if (!pci_latency) {
731                 DBG_TRC(("%s: bus: %08x fn: %08x fix latency.\n",
732                          DRIVERLNAME, pdev->bus->number, pdev->devfn))
733                 printk(KERN_INFO
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);
737         }
738
739         if (!(pdiva = diva_driver_add_card(pdev, ent->driver_data))) {
740                 DBG_TRC(("%s: %s bus: %08x fn: %08x card init failed.\n",
741                          DRIVERLNAME,
742                          CardProperties[ent->driver_data].Name,
743                          pdev->bus->number,
744                          pdev->devfn))
745                 printk(KERN_ERR
746                         "%s: %s bus: %08x fn: %08x card init failed.\n",
747                         DRIVERLNAME,
748                         CardProperties[ent->driver_data].
749                         Name, pdev->bus->number,
750                         pdev->devfn);
751                 return (-EIO);
752         }
753
754         pci_set_drvdata(pdev, pdiva);
755
756         return (0);
757 }
758
759 static void __devexit divas_remove_one(struct pci_dev *pdev)
760 {
761         void *pdiva = pci_get_drvdata(pdev);
762
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);
767
768         if (pdiva) {
769                 diva_driver_remove_card(pdiva);
770         }
771
772 }
773
774 /* --------------------------------------------------------------------------
775     Driver Load / Startup  
776    -------------------------------------------------------------------------- */
777 static int DIVA_INIT_FUNCTION divas_init(void)
778 {
779         char tmprev[50];
780         int ret = 0;
781
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
789         printk("BRI/PCI ");
790 #endif
791 #ifdef CONFIG_ISDN_DIVAS_PRIPCI
792         printk("PRI/PCI ");
793 #endif
794         printk("adapters\n");
795
796         if (!divasfunc_init(dbgmask)) {
797                 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
798                        DRIVERLNAME);
799                 ret = -EIO;
800                 goto out;
801         }
802
803         if (!divas_register_chrdev()) {
804 #ifdef MODULE
805                 divasfunc_exit();
806 #endif
807                 ret = -EIO;
808                 goto out;
809         }
810
811         if (!create_divas_proc()) {
812 #ifdef MODULE
813                 remove_divas_proc();
814                 divas_unregister_chrdev();
815                 divasfunc_exit();
816 #endif
817                 printk(KERN_ERR "%s: failed to create proc entry.\n",
818                        DRIVERLNAME);
819                 ret = -EIO;
820                 goto out;
821         }
822
823         if ((ret = pci_register_driver(&diva_pci_driver))) {
824 #ifdef MODULE
825                 remove_divas_proc();
826                 divas_unregister_chrdev();
827                 divasfunc_exit();
828 #endif
829                 printk(KERN_ERR "%s: failed to init pci driver.\n",
830                        DRIVERLNAME);
831                 goto out;
832         }
833         printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
834
835       out:
836         return (ret);
837 }
838
839 /* --------------------------------------------------------------------------
840     Driver Unload
841    -------------------------------------------------------------------------- */
842 static void DIVA_EXIT_FUNCTION divas_exit(void)
843 {
844         pci_unregister_driver(&diva_pci_driver);
845         remove_divas_proc();
846         divas_unregister_chrdev();
847         divasfunc_exit();
848
849         printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
850 }
851
852 module_init(divas_init);
853 module_exit(divas_exit);