Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/* $Id: eicon_pci.c,v 1.10 1999/08/22 20:26:49 calle Exp $
 *
 * ISDN low-level module for Eicon.Diehl active ISDN-Cards.
 * Hardware-specific code for PCI cards.
 *
 * Copyright 1998,99 by Armin Schindler (mac@melware.de)
 * Copyright 1999    Cytronics & Melware (info@melware.de)
 *
 * Thanks to	Eicon Technology Diehl GmbH & Co. oHG for 
 *		documents, informations and hardware. 
 *
 *		Deutsche Telekom AG for S2M support.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 * $Log: eicon_pci.c,v $
 * Revision 1.10  1999/08/22 20:26:49  calle
 * backported changes from kernel 2.3.14:
 * - several #include "config.h" gone, others come.
 * - "struct device" changed to "struct net_device" in 2.3.14, added a
 *   define in isdn_compat.h for older kernel versions.
 *
 * Revision 1.9  1999/08/11 21:01:11  keil
 * new PCI codefix
 *
 * Revision 1.8  1999/08/10 16:02:20  calle
 * struct pci_dev changed in 2.3.13. Made the necessary changes.
 *
 * Revision 1.7  1999/06/09 19:31:29  armin
 * Wrong PLX size for request_region() corrected.
 * Added first MCA code from Erik Weber.
 *
 * Revision 1.6  1999/04/01 12:48:37  armin
 * Changed some log outputs.
 *
 * Revision 1.5  1999/03/29 11:19:49  armin
 * I/O stuff now in seperate file (eicon_io.c)
 * Old ISA type cards (S,SX,SCOM,Quadro,S2M) implemented.
 *
 * Revision 1.4  1999/03/02 12:37:48  armin
 * Added some important checks.
 * Analog Modem with DSP.
 * Channels will be added to Link-Level after loading firmware.
 *
 * Revision 1.3  1999/01/24 20:14:24  armin
 * Changed and added debug stuff.
 * Better data sending. (still problems with tty's flip buffer)
 *
 * Revision 1.2  1999/01/10 18:46:06  armin
 * Bug with wrong values in HLC fixed.
 * Bytes to send are counted and limited now.
 *
 * Revision 1.1  1999/01/01 18:09:45  armin
 * First checkin of new eicon driver.
 * DIVA-Server BRI/PCI and PRI/PCI are supported.
 * Old diehl code is obsolete.
 *
 *
 */

#include <linux/config.h>
#include <linux/pci.h>

#include "eicon.h"
#include "eicon_pci.h"


char *eicon_pci_revision = "$Revision: 1.10 $";

#if CONFIG_PCI	         /* intire stuff is only for PCI */

#undef EICON_PCI_DEBUG 

int eicon_pci_find_card(char *ID)
{
  if (pci_present()) { 
    struct pci_dev *pdev = NULL;  
    int pci_nextindex=0, pci_cards=0, pci_akt=0; 
    int pci_type = PCI_MAESTRA;
    int NoMorePCICards = FALSE;
    char *ram, *reg, *cfg;	
    unsigned int pram=0, preg=0, pcfg=0;
    char did[12];
    eicon_pci_card *aparms;

   if (!(aparms = (eicon_pci_card *) kmalloc(sizeof(eicon_pci_card), GFP_KERNEL))) {
                  printk(KERN_WARNING
                      "eicon_pci: Could not allocate card-struct.\n");
                  return 0;
   }

  for (pci_cards = 0; pci_cards < 0x0f; pci_cards++)
  {
  do {
      if ((pdev = pci_find_device(PCI_VENDOR_EICON,          
                                  pci_type,                  
                                  pdev)))                    
	{
              pci_nextindex++;
              break;
	}
	else {
              pci_nextindex = 0;
              switch (pci_type) /* switch to next card type */
               {
               case PCI_MAESTRA:
                 pci_type = PCI_MAESTRAQ; break;
               case PCI_MAESTRAQ:
                 pci_type = PCI_MAESTRAQ_U; break;
               case PCI_MAESTRAQ_U:
                 pci_type = PCI_MAESTRAP; break;
               default:
               case PCI_MAESTRAP:
                 NoMorePCICards = TRUE;
               }
	}
     }
     while (!NoMorePCICards);
     if (NoMorePCICards)
        {
           if (pci_cards < 1) {
           printk(KERN_INFO "Eicon: No supported PCI cards found.\n");
	   kfree(aparms);	
           return 0;
           }
           else
           {
           printk(KERN_INFO "Eicon: %d PCI card%s registered.\n",
			pci_cards, (pci_cards > 1) ? "s":"");
	   kfree(aparms);	
           return (pci_cards);
           }
        }

   pci_akt = 0;
   switch(pci_type)
   {
    case PCI_MAESTRA:
         printk(KERN_INFO "Eicon: DIVA Server BRI/PCI detected !\n");
          aparms->type = EICON_CTYPE_MAESTRA;

          aparms->irq = pdev->irq;
          preg = pdev->resource[ 2].start & 0xfffffffc;
          pcfg = pdev->resource[ 1].start & 0xffffff80;

#ifdef EICON_PCI_DEBUG
          printk(KERN_DEBUG "eicon_pci: irq=%d\n", aparms->irq);
          printk(KERN_DEBUG "eicon_pci: reg=0x%x\n", preg);
          printk(KERN_DEBUG "eicon_pci: cfg=0x%x\n", pcfg);
#endif
	 pci_akt = 1;
         break;

    case PCI_MAESTRAQ:
    case PCI_MAESTRAQ_U:
         printk(KERN_ERR "Eicon: DIVA Server 4BRI/PCI detected but not supported !\n");
         pci_cards--;
	 pci_akt = 0;
         break;

    case PCI_MAESTRAP:
         printk(KERN_INFO "Eicon: DIVA Server PRI/PCI detected !\n");
          aparms->type = EICON_CTYPE_MAESTRAP; /*includes 9M,30M*/
          aparms->irq = pdev->irq;
          pram = pdev->resource[ 0].start & 0xfffff000;
          preg = pdev->resource[ 2].start & 0xfffff000;
          pcfg = pdev->resource[ 4].start & 0xfffff000;

#ifdef EICON_PCI_DEBUG
          printk(KERN_DEBUG "eicon_pci: irq=%d\n", aparms->irq);
          printk(KERN_DEBUG "eicon_pci: ram=0x%x\n",
               (pram));
          printk(KERN_DEBUG "eicon_pci: reg=0x%x\n",
               (preg));
          printk(KERN_DEBUG "eicon_pci: cfg=0x%x\n",
               (pcfg));
#endif
	  pci_akt = 1;
	  break;	
    default:
         printk(KERN_ERR "eicon_pci: Unknown PCI card detected !\n");
         pci_cards--;
	 pci_akt = 0;
	 break;
   }

	if (pci_akt) {
		/* remapping memory */
		switch(pci_type)
		{
    		case PCI_MAESTRA:
			aparms->PCIreg = (unsigned int) preg;
			aparms->PCIcfg = (unsigned int) pcfg;
			if (check_region((aparms->PCIreg), 0x20)) {
				printk(KERN_WARNING "eicon_pci: reg port already in use !\n");
				aparms->PCIreg = 0;
				break;	
			} else {
				request_region(aparms->PCIreg, 0x20, "eicon reg");
			}
			if (check_region((aparms->PCIcfg), 0x80)) {
				printk(KERN_WARNING "eicon_pci: cfg port already in use !\n");
				aparms->PCIcfg = 0;
				release_region(aparms->PCIreg, 0x20);
				break;	
			} else {
				request_region(aparms->PCIcfg, 0x80, "eicon cfg");
			}
			break;
    		case PCI_MAESTRAQ:
		case PCI_MAESTRAQ_U:
		case PCI_MAESTRAP:
			aparms->shmem = (eicon_pci_shmem *) ioremap(pram, 0x10000);
			ram = (u8 *) ((u32)aparms->shmem + MP_SHARED_RAM_OFFSET);
			reg =  ioremap(preg, 0x4000);
			cfg =  ioremap(pcfg, 0x1000);	
			aparms->PCIram = (unsigned int) ram;
			aparms->PCIreg = (unsigned int) reg;
			aparms->PCIcfg = (unsigned int) cfg;
			break;
		 }
		if ((!aparms->PCIreg) || (!aparms->PCIcfg)) {
			printk(KERN_ERR "eicon_pci: Card could not be added !\n");
			pci_cards--;
		} else {
			aparms->mvalid = 1;
	
			sprintf(did, "%s%d", (strlen(ID) < 1) ? "eicon":ID, pci_cards);

			printk(KERN_INFO "%s: DriverID: '%s'\n",eicon_ctype_name[aparms->type] , did);

			if (!(eicon_addcard(aparms->type, (int) aparms, aparms->irq, did))) {
				printk(KERN_ERR "eicon_pci: Card could not be added !\n");
				pci_cards--;
			}
		}
	}

  }
 } else
	printk(KERN_ERR "eicon_pci: Kernel compiled with PCI but no PCI-bios found !\n");
 return 0;
}

/*
 * Checks protocol file id for "F#xxxx" string fragment to
 * extract the features, supported by this protocol version.
 * binary representation of the feature string value is returned
 * in *value. The function returns 0 if feature string was not
 * found or has a wrong format, else 1.
 */
static int GetProtFeatureValue(char *sw_id, int *value)
{
  __u8 i, offset;

  while (*sw_id)
  {
    if ((sw_id[0] == 'F') && (sw_id[1] == '#'))
    {
      sw_id = &sw_id[2];
      for (i=0, *value=0; i<4; i++, sw_id++)
      {
        if ((*sw_id >= '0') && (*sw_id <= '9'))
        {
          offset = '0';
        }
        else if ((*sw_id >= 'A') && (*sw_id <= 'F'))
        {
          offset = 'A' + 10;
        }
        else if ((*sw_id >= 'a') && (*sw_id <= 'f'))
        {
          offset = 'a' + 10;
        }
        else
        {
          return 0;
        }
        *value |= (*sw_id - offset) << (4*(3-i));
      }
      return 1;
    }
    else
    {
      sw_id++;
    }
  }
  return 0;
}


void
eicon_pci_printpar(eicon_pci_card *card) {
        switch (card->type) {
                case EICON_CTYPE_MAESTRA:
			printk(KERN_INFO "%s at 0x%x / 0x%x, irq %d\n",
				eicon_ctype_name[card->type],
				(unsigned int)card->PCIreg,
				(unsigned int)card->PCIcfg,
				card->irq); 
			break;
                case EICON_CTYPE_MAESTRAQ:
                case EICON_CTYPE_MAESTRAQ_U:
                case EICON_CTYPE_MAESTRAP:
			printk(KERN_INFO "%s at 0x%x, irq %d\n",
				eicon_ctype_name[card->type],
				(unsigned int)card->shmem,
				card->irq); 
#ifdef EICON_PCI_DEBUG
		        printk(KERN_INFO "eicon_pci: remapped ram= 0x%x\n",(unsigned int)card->PCIram);
		        printk(KERN_INFO "eicon_pci: remapped reg= 0x%x\n",(unsigned int)card->PCIreg);
		        printk(KERN_INFO "eicon_pci: remapped cfg= 0x%x\n",(unsigned int)card->PCIcfg); 
#endif
			break;
	}
}


static void
eicon_pci_release_shmem(eicon_pci_card *card) {
	if (!card->master)
		return;
	if (card->mvalid) {
        	switch (card->type) {
                	case EICON_CTYPE_MAESTRA:
			        /* reset board */
				outb(0, card->PCIcfg + 0x4c);	/* disable interrupts from PLX */
				outb(0, card->PCIreg + M_RESET);
				SLEEP(20);
				outb(0, card->PCIreg + M_ADDRH);
				outw(0, card->PCIreg + M_ADDR);
				outw(0, card->PCIreg + M_DATA);

				release_region(card->PCIreg, 0x20);
				release_region(card->PCIcfg, 0x80);
				break;
                	case EICON_CTYPE_MAESTRAQ:
	                case EICON_CTYPE_MAESTRAQ_U:
	                case EICON_CTYPE_MAESTRAP:
			        /* reset board */
		        	writeb(_MP_RISC_RESET | _MP_LED1 | _MP_LED2, card->PCIreg + MP_RESET);
			        SLEEP(20);
			        writeb(0, card->PCIreg + MP_RESET);
			        SLEEP(20);

				iounmap((void *)card->shmem);
				iounmap((void *)card->PCIreg);
				iounmap((void *)card->PCIcfg);
				break;
		}
	}
	card->mvalid = 0;
}

static void
eicon_pci_release_irq(eicon_pci_card *card) {
	if (!card->master)
		return;
	if (card->ivalid)
		free_irq(card->irq, card);
	card->ivalid = 0;
}

void
eicon_pci_release(eicon_pci_card *card) {
        eicon_pci_release_irq(card);
        eicon_pci_release_shmem(card);
}

/*
 * Upload buffer content to adapters shared memory
 * on verify error, 1 is returned and a message is printed on screen
 * else 0 is returned
 * Can serve IO-Type and Memory type adapters
 */
int eicon_upload(t_dsp_download_space   *p_para,
            __u16                 length,   /* byte count */
            __u8                  *buffer,
            int                   verify)
{
  __u32               i, dwdata = 0, val = 0, timeout;
  __u16               data;
  eicon_pci_boot *boot = 0;

  switch (p_para->type) /* actions depend on type of union */
  {
    case DL_PARA_IO_TYPE:
      for (i=0; i<length; i+=2)
      {
	outb ((u8) ((p_para->dat.io.r3addr + i) >> 16), p_para->dat.io.ioADDRH);
	outw ((u16) (p_para->dat.io.r3addr + i), p_para->dat.io.ioADDR); 
	/* outw (((u16 *)code)[i >> 1], p_para->dat.io.ioDATA); */
	outw (*(u16 *)&buffer[i], p_para->dat.io.ioDATA); 
      }
      if (verify) /* check written block */
      {
        for (i=0; i<length; i+=2)
        {
	  outb ((u8) ((p_para->dat.io.r3addr + i) >> 16), p_para->dat.io.ioADDRH);
          outw ((u16) (p_para->dat.io.r3addr + i), p_para->dat.io.ioADDR); 
          data = inw(p_para->dat.io.ioDATA);
          if (data != *(u16 *)&buffer[i])
          {
            p_para->dat.io.r3addr  += i;
            p_para->dat.io.BadData  = data;
            p_para->dat.io.GoodData = *(u16 *)&buffer[i];
            return 1;
          }
        }
      }
      break;

    case DL_PARA_MEM_TYPE:
      boot = p_para->dat.mem.boot;
      writel(p_para->dat.mem.r3addr, &boot->addr);
      for (i=0; i<length; i+=4)
      {
        writel(((u32 *)buffer)[i >> 2], &boot->data[i]);
      }
      if (verify) /* check written block */
      {
        for (i=0; i<length; i+=4)
        {
          dwdata = readl(&boot->data[i]);
          if (((u32 *)buffer)[i >> 2] != dwdata)
          {
            p_para->dat.mem.r3addr  += i;
            p_para->dat.mem.BadData  = dwdata;
            p_para->dat.mem.GoodData = ((u32 *)buffer)[i >> 2];
            return 1;
          }
        }
      }
      writel(((length + 3) / 4), &boot->len);  /* len in dwords */
      writel(2, &boot->cmd);

	timeout = jiffies + 20;
	while (timeout > jiffies) {
		val = readl(&boot->cmd);
		if (!val) break;
		SLEEP(2);
	}
	if (val)
         {
		p_para->dat.mem.timeout = 1;
		return 1;
	 }
      break;
  }
  return 0;
}


/* show header information of code file */
static
int eicon_pci_print_hdr(unsigned char *code, int offset)
{
  unsigned char hdr[80];
  int i, fvalue = 0;

  i = 0;
  while ((i < (sizeof(hdr) -1))
          && (code[offset + i] != '\0')
          && (code[offset + i] != '\r')
          && (code[offset + i] != '\n'))
   {
     hdr[i] = code[offset + i];
     i++;
   }
   hdr[i] = '\0';
   printk(KERN_DEBUG "Eicon: loading %s\n", hdr);
   if (GetProtFeatureValue(hdr, &fvalue)) return(fvalue);
    else return(0);
}


/*
 * Configure a card, download code into BRI card,
 * check if we get interrupts and return 0 on succes.
 * Return -ERRNO on failure.
 */
int
eicon_pci_load_bri(eicon_pci_card *card, eicon_pci_codebuf *cb) {
        int               i,j;
        int               timeout;
	unsigned int	  offset, offp=0, size, length;
	int		  signature = 0;
	int		  FeatureValue = 0;
        eicon_pci_codebuf cbuf;
	t_dsp_download_space dl_para;
	t_dsp_download_desc  dsp_download_table;
        unsigned char     *code;
	unsigned int	  reg;
	unsigned int	  cfg;

        if (copy_from_user(&cbuf, cb, sizeof(eicon_pci_codebuf)))
                return -EFAULT;

	reg = card->PCIreg;
	cfg = card->PCIcfg;

	/* reset board */
	outb(0, reg + M_RESET);
	SLEEP(10);
	outb(0, reg + M_ADDRH);
	outw(0, reg + M_ADDR);
	outw(0, reg + M_DATA);

#ifdef EICON_PCI_DEBUG
	 printk(KERN_DEBUG "eicon_pci: reset card\n");
#endif

	/* clear shared memory */
	outb(0xff, reg + M_ADDRH);
	outw(0, reg + M_ADDR);
	for(i = 0; i < 0xffff; i++) outw(0, reg + M_DATA);
	SLEEP(10);

#ifdef EICON_PCI_DEBUG
	 printk(KERN_DEBUG "eicon_pci: clear shared memory\n");
#endif

	/* download protocol and dsp file */

#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: downloading firmware...\n");
#endif

       	/* Allocate code-buffer */
       	if (!(code = kmalloc(400, GFP_KERNEL))) {
                printk(KERN_WARNING "eicon_pci_boot: Couldn't allocate code buffer\n");
       	        return -ENOMEM;
        }

	/* prepare protocol upload */
	dl_para.type		= DL_PARA_IO_TYPE;
	dl_para.dat.io.ioADDR	= reg + M_ADDR;
	dl_para.dat.io.ioADDRH	= reg + M_ADDRH;
	dl_para.dat.io.ioDATA	= reg + M_DATA;

	for (j = 0; j <= cbuf.dsp_code_num; j++) 
	 {	
	   if (j == 0)  size = cbuf.protocol_len;
	           else size = cbuf.dsp_code_len[j];

        	offset = 0;

		if (j == 0) dl_para.dat.io.r3addr = 0;
		if (j == 1) dl_para.dat.io.r3addr = M_DSP_CODE_BASE +
					((sizeof(__u32) + (sizeof(dsp_download_table) * 35) + 3) &0xfffffffc);
		if (j == 2) dl_para.dat.io.r3addr = M_DSP_CODE_BASE;
		if (j == 3) dl_para.dat.io.r3addr = M_DSP_CODE_BASE + sizeof(__u32);

           do  /* download block of up to 400 bytes */
            {
              length = ((size - offset) >= 400) ? 400 : (size - offset);

        	if (copy_from_user(code, (&cb->code) + offp + offset, length)) {
                	kfree(code);
	                return -EFAULT;
        	}

		if ((offset == 0) && (j < 2)) {
		       	FeatureValue = eicon_pci_print_hdr(code, j ? 0x00 : 0x80); 
#ifdef EICON_PCI_DEBUG
	if (FeatureValue) printk(KERN_DEBUG "eicon_pci: Feature Value : 0x%04x.\n", FeatureValue);
#endif
			if ((j==0) && (!(FeatureValue & PROTCAP_TELINDUS))) {
                  		printk(KERN_ERR "eicon_pci: Protocol Code cannot handle Telindus\n");
				kfree(code);
		                return -EFAULT;
			}
                	((eicon_card *)card->card)->Feature = FeatureValue;
		}

		if (eicon_upload(&dl_para, length, code, 1))
		{
                  printk(KERN_ERR "eicon_pci: code block check failed at 0x%x !\n",dl_para.dat.io.r3addr);
		  kfree(code);
                  return -EIO;
		}
              /* move onto next block */
              offset += length;
	      dl_para.dat.io.r3addr += length;
            } while (offset < size);

#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "Eicon: %d bytes loaded.\n", offset);
#endif
	offp += size;
	}
	kfree(code);	

	/* clear signature */
	outb(0xff, reg + M_ADDRH);
	outw(0x1e, reg + M_ADDR);
	outw(0, reg + M_DATA);

#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: copy configuration data into shared memory...\n");
#endif
	/* copy configuration data into shared memory */
	outw(8, reg + M_ADDR); outb(cbuf.tei, reg + M_DATA);
	outw(9, reg + M_ADDR); outb(cbuf.nt2, reg + M_DATA);
	outw(10,reg + M_ADDR); outb(0, reg + M_DATA);
	outw(11,reg + M_ADDR); outb(cbuf.WatchDog, reg + M_DATA);
	outw(12,reg + M_ADDR); outb(cbuf.Permanent, reg + M_DATA);
	outw(13,reg + M_ADDR); outb(0, reg + M_DATA);                 /* XInterface */
	outw(14,reg + M_ADDR); outb(cbuf.StableL2, reg + M_DATA);
	outw(15,reg + M_ADDR); outb(cbuf.NoOrderCheck, reg + M_DATA);
	outw(16,reg + M_ADDR); outb(0, reg + M_DATA);                 /* HandsetType */
	outw(17,reg + M_ADDR); outb(0, reg + M_DATA);                 /* SigFlags */
	outw(18,reg + M_ADDR); outb(cbuf.LowChannel, reg + M_DATA);
	outw(19,reg + M_ADDR); outb(cbuf.ProtVersion, reg + M_DATA);
	outw(20,reg + M_ADDR); outb(cbuf.Crc4, reg + M_DATA);
	outw(21,reg + M_ADDR); outb((cbuf.Loopback) ? 2:0, reg + M_DATA);

	for (i=0;i<32;i++)
	{
		outw( 32+i, reg + M_ADDR); outb(cbuf.l[0].oad[i], reg + M_DATA);
		outw( 64+i, reg + M_ADDR); outb(cbuf.l[0].osa[i], reg + M_DATA);
		outw( 96+i, reg + M_ADDR); outb(cbuf.l[0].spid[i], reg + M_DATA);
		outw(128+i, reg + M_ADDR); outb(cbuf.l[1].oad[i], reg + M_DATA);
		outw(160+i, reg + M_ADDR); outb(cbuf.l[1].osa[i], reg + M_DATA);
		outw(192+i, reg + M_ADDR); outb(cbuf.l[1].spid[i], reg + M_DATA);
	}

#ifdef EICON_PCI_DEBUG
           printk(KERN_ERR "eicon_pci: starting CPU...\n");
#endif
	/* let the CPU run */
	outw(0x08, reg + M_RESET);

        timeout = jiffies + (5*HZ);
        while (timeout > jiffies) {
	   outw(0x1e, reg + M_ADDR);	
           signature = inw(reg + M_DATA);
           if (signature == DIVAS_SIGNATURE) break;
           SLEEP(2);
         }
        if (signature != DIVAS_SIGNATURE)
         {
#ifdef EICON_PCI_DEBUG
           printk(KERN_ERR "eicon_pci: signature 0x%x expected 0x%x\n",signature,DIVAS_SIGNATURE);
#endif
           printk(KERN_ERR "eicon_pci: Timeout, protocol code not running !\n");
           return -EIO; 
         }
#ifdef EICON_PCI_DEBUG
        printk(KERN_DEBUG "eicon_pci: Protocol code running, signature OK\n");
#endif

        /* get serial number and number of channels supported by card */
	outb(0xff, reg + M_ADDRH);
	outw(0x3f6, reg + M_ADDR);
        card->channels = inw(reg + M_DATA);
        card->serial = (u32)inw(cfg + 0x22) << 16 | (u32)inw(cfg + 0x26);
        printk(KERN_INFO "Eicon: Supported channels : %d\n", card->channels);
        printk(KERN_INFO "Eicon: Card serial no. = %lu\n", card->serial);

        /* test interrupt */
        card->irqprobe = 1;

        if (!card->ivalid) {
                if (request_irq(card->irq, &eicon_irq, 0, "Eicon PCI ISDN", card->card))
                 {
                  printk(KERN_ERR "eicon_pci: Couldn't request irq %d\n", card->irq);
                  return -EIO;
                 }
        }
        card->ivalid = 1;

#ifdef EICON_PCI_DEBUG
        printk(KERN_DEBUG "eicon_pci: testing interrupt\n");
#endif
        /* Trigger an interrupt and check if it is delivered */
        outb(0x41, cfg + 0x4c);		/* enable PLX for interrupts */
	outb(0x89, reg + M_RESET);	/* place int request */

        timeout = jiffies + 20;
        while (timeout > jiffies) {
          if (card->irqprobe != 1) break;
          SLEEP(5);
         }
        if (card->irqprobe == 1) {
           free_irq(card->irq, card); 
           card->ivalid = 0; 
           printk(KERN_ERR "eicon_pci: Getting no interrupts !\n");
           return -EIO;
         }

   /* initializing some variables */
   ((eicon_card *)card->card)->ReadyInt = 0;
   for(j=0; j<256; j++) ((eicon_card *)card->card)->IdTable[j] = NULL;
   for(j=0; j< (card->channels + 1); j++) {
                ((eicon_card *)card->card)->bch[j].e.busy = 0;
                ((eicon_card *)card->card)->bch[j].e.D3Id = 0;
                ((eicon_card *)card->card)->bch[j].e.B2Id = 0;
                ((eicon_card *)card->card)->bch[j].e.ref = 0;
                ((eicon_card *)card->card)->bch[j].e.Req = 0;
                ((eicon_card *)card->card)->bch[j].e.complete = 1;
                ((eicon_card *)card->card)->bch[j].fsm_state = EICON_STATE_NULL;
   }

   printk(KERN_INFO "Eicon: Card successfully started\n");

 return 0;
}


/*
 * Configure a card, download code into PRI card,
 * check if we get interrupts and return 0 on succes.
 * Return -ERRNO on failure.
 */
int
eicon_pci_load_pri(eicon_pci_card *card, eicon_pci_codebuf *cb) {
        eicon_pci_boot    *boot;
	eicon_pr_ram  *prram;
        int               i,j;
        int               timeout;
	int		  FeatureValue = 0;
	unsigned int	  offset, offp=0, size, length;
	unsigned long int signature = 0;
	t_dsp_download_space dl_para;
	t_dsp_download_desc  dsp_download_table;
        eicon_pci_codebuf cbuf;
        unsigned char     *code;
	unsigned char	  req_int;
    	char *ram, *reg, *cfg;	

        if (copy_from_user(&cbuf, cb, sizeof(eicon_pci_codebuf)))
                return -EFAULT;

        boot = &card->shmem->boot;
	ram = (char *)card->PCIram;
	reg = (char *)card->PCIreg;
	cfg = (char *)card->PCIcfg;
	prram = (eicon_pr_ram *)ram;

	/* reset board */
	writeb(_MP_RISC_RESET | _MP_LED1 | _MP_LED2, card->PCIreg + MP_RESET);
	SLEEP(20);
	writeb(0, card->PCIreg + MP_RESET);
	SLEEP(20);

	/* set command count to 0 */
	writel(0, &boot->reserved); 

	/* check if CPU increments the life word */
        i = readw(&boot->live);
        SLEEP(20);
        if (i == readw(&boot->live)) {
           printk(KERN_ERR "eicon_pci: card is reset, but CPU not running !\n");
           return -EIO;
         }
#ifdef EICON_PCI_DEBUG
	 printk(KERN_DEBUG "eicon_pci: reset card OK (CPU running)\n");
#endif

	/* download firmware : DSP and Protocol */
#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: downloading firmware...\n");
#endif

       	/* Allocate code-buffer */
       	if (!(code = kmalloc(400, GFP_KERNEL))) {
                printk(KERN_WARNING "eicon_pci_boot: Couldn't allocate code buffer\n");
       	        return -ENOMEM;
        }

	/* prepare protocol upload */
	dl_para.type		= DL_PARA_MEM_TYPE;
	dl_para.dat.mem.boot	= boot;

        for (j = 0; j <= cbuf.dsp_code_num; j++)
         {
	   if (j==0) size = cbuf.protocol_len;
		else size = cbuf.dsp_code_len[j];	

           if (j==1) writel(MP_DSP_ADDR, &boot->addr); /* DSP code entry point */

		if (j == 0) dl_para.dat.io.r3addr = MP_PROTOCOL_ADDR;
		if (j == 1) dl_para.dat.io.r3addr = MP_DSP_CODE_BASE +
					((sizeof(__u32) + (sizeof(dsp_download_table) * 35) + 3) &0xfffffffc);
		if (j == 2) dl_para.dat.io.r3addr = MP_DSP_CODE_BASE;
		if (j == 3) dl_para.dat.io.r3addr = MP_DSP_CODE_BASE + sizeof(__u32);

           offset = 0;
           do  /* download block of up to 400 bytes */
            {
              length = ((size - offset) >= 400) ? 400 : (size - offset);

        	if (copy_from_user(code, (&cb->code) + offp + offset, length)) {
                	kfree(code);
	                return -EFAULT;
        	}

		if ((offset == 0) && (j < 2)) {
	           	FeatureValue = eicon_pci_print_hdr(code, j ? 0x00 : 0x80); 
#ifdef EICON_PCI_DEBUG
	if (FeatureValue) printk(KERN_DEBUG "eicon_pci: Feature Value : 0x%x.\n", FeatureValue);
#endif
			if ((j==0) && (!(FeatureValue & PROTCAP_TELINDUS))) {
                  		printk(KERN_ERR "eicon_pci: Protocol Code cannot handle Telindus\n");
				kfree(code);
		                return -EFAULT;
			}
                	((eicon_card *)card->card)->Feature = FeatureValue;
		}

		if (eicon_upload(&dl_para, length, code, 1))
		{
		  if (dl_para.dat.mem.timeout == 0)
	                  printk(KERN_ERR "eicon_pci: code block check failed at 0x%x !\n",dl_para.dat.io.r3addr);
			else
			  printk(KERN_ERR "eicon_pci: timeout, no ACK to load !\n");
		  kfree(code);
                  return -EIO;
		}

              /* move onto next block */
              offset += length;
	      dl_para.dat.mem.r3addr += length;
            } while (offset < size);
#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: %d bytes loaded.\n", offset);
#endif
	 offp += size;
         }
	 kfree(code);	

	/* initialize the adapter data structure */
#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: copy configuration data into shared memory...\n");
#endif
        /* clear out config space */
        for (i = 0; i < 256; i++) writeb(0, &ram[i]);

        /* copy configuration down to the card */
        writeb(cbuf.tei, &ram[8]);
        writeb(cbuf.nt2, &ram[9]);
        writeb(0, &ram[10]);
        writeb(cbuf.WatchDog, &ram[11]);
        writeb(cbuf.Permanent, &ram[12]);
        writeb(cbuf.XInterface, &ram[13]);
        writeb(cbuf.StableL2, &ram[14]);
        writeb(cbuf.NoOrderCheck, &ram[15]);
        writeb(cbuf.HandsetType, &ram[16]);
        writeb(0, &ram[17]);
        writeb(cbuf.LowChannel, &ram[18]);
        writeb(cbuf.ProtVersion, &ram[19]);
        writeb(cbuf.Crc4, &ram[20]);
        for (i = 0; i < 32; i++)
         {
           writeb(cbuf.l[0].oad[i], &ram[32 + i]);
           writeb(cbuf.l[0].osa[i], &ram[64 + i]);
           writeb(cbuf.l[0].spid[i], &ram[96 + i]);
           writeb(cbuf.l[1].oad[i], &ram[128 + i]);
           writeb(cbuf.l[1].osa[i], &ram[160 + i]);
           writeb(cbuf.l[1].spid[i], &ram[192 + i]);
         }
#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: configured card OK\n");
#endif

	/* start adapter */
#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: tell card to start...\n");
#endif
        writel(MP_PROTOCOL_ADDR, &boot->addr); /* RISC code entry point */
        writel(3, &boot->cmd); /* DIVAS_START_CMD */

        /* wait till card ACKs */
        timeout = jiffies + (5*HZ);
        while (timeout > jiffies) {
           signature = readl(&boot->signature);
           if ((signature >> 16) == DIVAS_SIGNATURE) break;
           SLEEP(2);
         }
        if ((signature >> 16) != DIVAS_SIGNATURE)
         {
#ifdef EICON_PCI_DEBUG
           printk(KERN_ERR "eicon_pci: signature 0x%lx expected 0x%x\n",(signature >> 16),DIVAS_SIGNATURE);
#endif
           printk(KERN_ERR "eicon_pci: timeout, protocol code not running !\n");
           return -EIO;
         }
#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: Protocol code running, signature OK\n");
#endif

	/* get serial number and number of channels supported by card */
        card->channels = readb(&ram[0x3f6]);
        card->serial = readl(&ram[0x3f0]);
        printk(KERN_INFO "Eicon: Supported channels : %d\n", card->channels);
        printk(KERN_INFO "Eicon: Card serial no. = %lu\n", card->serial);

	/* test interrupt */
	readb(&ram[0x3fe]);
        writeb(0, &ram[0x3fe]); /* reset any pending interrupt */
	readb(&ram[0x3fe]);

        writew(MP_IRQ_RESET_VAL, &cfg[MP_IRQ_RESET]);
        writew(0, &cfg[MP_IRQ_RESET + 2]);

        card->irqprobe = 1;

	if (!card->ivalid) {
	        if (request_irq(card->irq, &eicon_irq, 0, "Eicon PCI ISDN", card->card)) 
        	 {
	          printk(KERN_ERR "eicon_pci: Couldn't request irq %d\n", card->irq);
        	  return -EIO;
	         }
	}
	card->ivalid = 1;

        req_int = readb(&prram->ReadyInt);
#ifdef EICON_PCI_DEBUG
	printk(KERN_DEBUG "eicon_pci: testing interrupt\n");
#endif
        req_int++;
        /* Trigger an interrupt and check if it is delivered */
        writeb(req_int, &prram->ReadyInt);

        timeout = jiffies + 20;
        while (timeout > jiffies) {
          if (card->irqprobe != 1) break;
          SLEEP(2);
         }
        if (card->irqprobe == 1) {
           free_irq(card->irq, card);
	   card->ivalid = 0;
           printk(KERN_ERR "eicon_pci: Getting no interrupts !\n");
           return -EIO;
         }

   /* initializing some variables */
   ((eicon_card *)card->card)->ReadyInt = 0;
   for(j=0; j<256; j++) ((eicon_card *)card->card)->IdTable[j] = NULL;
   for(j=0; j< (card->channels + 1); j++) {
		((eicon_card *)card->card)->bch[j].e.busy = 0;
		((eicon_card *)card->card)->bch[j].e.D3Id = 0;
		((eicon_card *)card->card)->bch[j].e.B2Id = 0;
		((eicon_card *)card->card)->bch[j].e.ref = 0;
		((eicon_card *)card->card)->bch[j].e.Req = 0;
                ((eicon_card *)card->card)->bch[j].e.complete = 1;
                ((eicon_card *)card->card)->bch[j].fsm_state = EICON_STATE_NULL;
   }

   printk(KERN_INFO "Eicon: Card successfully started\n");

 return 0;
}

#endif	/* CONFIG_PCI */