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