File: dev_net_rtl8019.c

package info (click to toggle)
skyeye 1.2.5-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,236 kB
  • ctags: 19,345
  • sloc: ansic: 90,379; sh: 5,188; python: 707; cpp: 417; makefile: 322; exp: 38
file content (1001 lines) | stat: -rw-r--r-- 22,949 bytes parent folder | download | duplicates (3)
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
/*
	dev_net_rtl8019.c - skyeye realtek 8019 ethernet controllor simulation
	Copyright (C) 2003 - 2005 Skyeye Develop Group
        for help please send mail to <skyeye-developer@lists.gro.clinux.org>
	
	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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
*/
/*
 * 04/01/2007	added 16-bits mode.
 * 			Anthony Lee <don.anthony.lee@gmail.com>
 * 03/19/2007	fixed writing CR directly when page over 2,
 * 		fixed for running on Windows,
 * 		replaced the codes based on sigaction with portable function.
 * 			Anthony Lee <don.anthony.lee@gmail.com>
 * 05/25/2005   modified for rtl8019
 *                      walimis <wlm@student.dlut.edu.cn>
 * 04/27/2003	add net option support
 * 			chenyu <chenyu@hpclab.cs.tsinghua.edu.cn>
 * 02/25/2003 	initial version
 *			yangye <yangye@163.net> 		
 */

#include "armdefs.h"
#include "skyeye_device.h"
#include "dev_net_rtl8019.h"
#include "portable/gettimeofday.h"

//if you don't want to the debug info,just commit below two line
//#define DEBUG 1
#undef DEBUG
#if DEBUG
#define DBG_PRINT(a...) fprintf(stderr, ##a)
#else
#define DBG_PRINT(a...)
#endif

static struct device_default_value rtl8019_net_def[] = {
	/* name		base		size	interrupt array */
	{"at91",	0xfffa0000,	0xff,	{16, 0, 0, 0}},
	{"s3c44b0x",	0x06000000,	0xff,	{22, 0, 0, 0}},
	{NULL},
};

#define MAX_DEVICE_NUM 10
static struct device_desc *rtl8019_devs[MAX_DEVICE_NUM];
static struct timeval eth_timeout[MAX_DEVICE_NUM];
static int eth_timeout_flags[MAX_DEVICE_NUM] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

#define TIMEVAL_ADD_USEC(tv, usec)					\
	do {								\
		tv.tv_sec += ((tv.tv_usec + usec) / (1000000UL));	\
		tv.tv_usec = ((tv.tv_usec + usec) % (1000000UL));	\
	} while (0)

#define TIMEVAL_CMP(tv1, tv2)						\
	(memcmp(&tv1, &tv2, sizeof(struct timeval)) == 0 ?		\
	 0 : (								\
		tv1.tv_sec < tv2.tv_sec ? -1 : (			\
		(tv1.tv_sec == tv2.tv_sec && tv1.tv_usec < tv2.tv_usec) ? -1 : 1        \
	)))

static void set_time(int index, int packets)
{
	eth_timeout_flags[index] = 0;

	if (packets <= 0) return;

	if (gettimeofday(&eth_timeout[index], NULL) != 0) return;

#if 0
	TIMEVAL_ADD_USEC(eth_timeout[index], (unsigned int)packets * 100UL);
#else
	/* 
	 * NOTE by Stano:
	 * 	In 10 ms - no reason to wait 1 second for a big packet
	 *
	 * NOTE by Lee:
	 * 	Though the timeout just for generating interrupt,
	 *	but please increase the timeout microseconds to decrease
	 *	the loading of CPU when connecting to an external network.
	 */
	TIMEVAL_ADD_USEC(eth_timeout[index], 10000UL);
#endif

	eth_timeout_flags[index] = 1;
}

static inline void
net_rtl8019_set_update_intr (struct device_desc *dev)
{
	struct device_interrupt *intr = &dev->intr;
	struct machine_config *mc = (struct machine_config *) dev->mach;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
	mc->mach_set_intr (intr->interrupts[INT_RTL8019]);
	mc->mach_update_intr (mc);
}

static void send_interrupt(int index)
{
	struct device_desc *dev;

	if ((dev = rtl8019_devs[index]) != NULL) {
		struct net_device *net_dev = (struct net_device *) dev->dev;
		struct machine_config *mc = (struct machine_config *) dev->mach;
		struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
		if ((io->need_update) && (io->IMR & io->ISR)) {
			net_rtl8019_set_update_intr(dev);
			io->need_update = 0;
			set_time(index, 0);
		}
	}
}

static void check_timeout(int index)
{
	struct timeval tv;

	if (!eth_timeout_flags[index]) return;
	if (gettimeofday(&tv, NULL) != 0) return;
	if (TIMEVAL_CMP(tv, eth_timeout[index]) < 1) return;

	send_interrupt(index);

	eth_timeout_flags[index] = 0;
}

static void
write_cr (struct device_desc *dev, u8 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
	u8 startpage;
	u16 packet_len;
	u16 rtl8019_len;

	DBG_PRINT ("write_cr begin: data 0x%x\n", data);

	// Validate remote-DMA (RD2,RD1,RD0 not allowed to be 000) 
	if (((data & 0x38) == 0x00)) {	//wrong command
		DBG_PRINT ("write_cr: wrong command\n");
		return;
	}

	// XMIT command
	if (data & CMD_XMIT) {
		DBG_PRINT ("write_cr: xmit command\n");
		startpage = (io->TPSR - START_PAGE);	//tpsr - 0x40
		packet_len = (((u16) io->TBCR1 << 8) | (io->TBCR0));
		packet_len &= 0xffff;


		io->TSR = 0;
		if (!rtl8019_output
		    (dev, (io->sram + startpage * PAGE_SIZE), packet_len)) {
			io->TSR |= TSR_PTX;
		}
		else {
			io->TSR |= TSR_COL;
		}

		/**** send a interrupt to CPU here! ****/
		io->ISR |= ISR_PTX;
		set_time (io->index, packet_len);
		io->need_update = 1;
		//net_rtl8019_set_update_intr(dev);

	}

	//remote dma write 
	if ((data & CMD_READ) && (data & CMD_RUN)) {
		rtl8019_len = (((u16) io->RBCR1 << 8) | io->RBCR0);
		io->remote_read_offset =
			(u16) (io->RSAR1 << 8 | io->RSAR0) -
			(u16) (START_PAGE * PAGE_SIZE);
		io->remote_read_offset &= 0xffff;
		io->remote_read_count = rtl8019_len;
		//printf("io->remote_read_count:%d,io->remote_read_offset:%x\n", io->remote_read_count, io->remote_read_offset);
	}

	//remote dma write 
	if ((data & CMD_WRITE) && (data & CMD_RUN)
	    && ((data & CMD_NODMA) == 0)) {

		io->remote_write_offset =
			(io->RSAR1 << 8 | io->RSAR0) - START_PAGE * PAGE_SIZE;
		io->remote_write_count = (((u16) io->RBCR1 << 8) | io->RBCR0);
		io->remote_write_count &= 0xffff;
		//printf("rtl8019 rw:count:%d,offset:%x\n", io->remote_write_count, io->remote_write_offset);

	}

	io->CR = data;

	DBG_PRINT ("write_cr: end\n");
}

static void
remote_write_word (struct device_desc *dev, u16 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;

	DBG_PRINT ("remote_write begin: data 0x%x\n", data);
	if (io->CR & CMD_WRITE) {	//in remote write mode

		if (io->remote_write_offset + 1 >= PAGE_NUM * PAGE_SIZE) {
			io->ISR |= ISR_OVW;
			if ((ISR_OVW & io->IMR)) {
				net_rtl8019_set_update_intr (dev);
			}
			DBG_PRINT ("%s: write data overflow!\n", __FUNCTION__);
			return;
		}

		io->sram[io->remote_write_offset] = data & 0xff;
		io->sram[io->remote_write_offset + 1] = data >> 8;
		io->remote_write_offset += 2;
		io->remote_write_count -= 2;


		if (io->remote_write_count <= 0) {
			io->CR &= (~CMD_WRITE);	//clear dma command in CR
			io->CR |= CMD_NODMA;

			io->ISR |= ISR_RDC;	// remote write finished int
			if ((ISR_RDC & io->IMR)) {
				net_rtl8019_set_update_intr (dev);
			}
		}
		DBG_PRINT ("remote write end\n");
		return;
	}

}

static void
remote_write_byte (struct device_desc *dev, u8 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;

	DBG_PRINT ("remote_write begin: data 0x%x\n", data);
	if (io->CR & CMD_WRITE) {	//in remote write mode

		if (io->remote_write_offset >= PAGE_NUM * PAGE_SIZE) {
			io->ISR |= ISR_OVW;
			if ((ISR_OVW & io->IMR)) {
				net_rtl8019_set_update_intr (dev);
			}
			DBG_PRINT ("%s: write data overflow!\n", __FUNCTION__);
			return;
		}

		io->sram[io->remote_write_offset] = data;
		io->remote_write_offset++;
		io->remote_write_count--;


		if (io->remote_write_count == 0) {
			io->CR &= (~CMD_WRITE);	//clear dma command in CR
			io->CR |= CMD_NODMA;

			io->ISR |= ISR_RDC;	// remote write finished int
			if ((ISR_RDC & io->IMR)) {
				net_rtl8019_set_update_intr (dev);
			}
		}
		DBG_PRINT ("remote write end\n");
		return;
	}

}

static u16
remote_read_halfword (struct device_desc *dev)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
	u16 data;

	DBG_PRINT ("remote read begin\n");

	if (io->CR & CMD_READ) {

		data = io->sram[io->remote_read_offset];
		data |= io->sram[io->remote_read_offset + 1] << 8;
		io->remote_read_offset += 2;

		io->remote_read_count -= 2;

		if (io->remote_read_count <= 0) {
			io->CR &= (~CMD_READ);
			io->CR |= CMD_NODMA;
			io->ISR |= ISR_RDC;
			if ((ISR_RDC & io->IMR)) {
				net_rtl8019_set_update_intr (dev);
			}
		}

		DBG_PRINT ("remote read end:data %d\n", data);
		return data;
	}

	return 0;
}

static u8
remote_read_byte (struct device_desc *dev)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
	u8 data;

	DBG_PRINT ("remote read begin\n");

	if (io->CR & CMD_READ) {

		data = io->sram[io->remote_read_offset];
		io->remote_read_offset++;

		if (--io->remote_read_count == 0) {
			io->CR &= (~CMD_READ);
			io->CR |= CMD_NODMA;
			io->ISR |= ISR_RDC;
			if ((ISR_RDC & io->IMR)) {
				net_rtl8019_set_update_intr (dev);
			}
		}

		DBG_PRINT ("remote read end:data %d\n", data);
		return data;
	}

	return 0;
}

static void
net_rtl8019_fini (struct device_desc *dev)
{
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
	free (dev->dev);
	free (io);
}

static void
net_rtl8019_reset (struct device_desc *dev)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;

	/*init PROM */
	io->PROM[0] = (u8) net_dev->macaddr[0];	//MACADDR0;
	io->PROM[1] = (u8) net_dev->macaddr[0];	//MACADDR0;
	io->PROM[2] = (u8) net_dev->macaddr[1];	//MACADDR1;
	io->PROM[3] = (u8) net_dev->macaddr[1];	//MACADDR1;
	io->PROM[4] = (u8) net_dev->macaddr[2];	//MACADDR2;
	io->PROM[5] = (u8) net_dev->macaddr[2];	//MACADDR2;
	io->PROM[6] = (u8) net_dev->macaddr[3];	//MACADDR3;
	io->PROM[7] = (u8) net_dev->macaddr[3];	//MACADDR3;
	io->PROM[8] = (u8) net_dev->macaddr[4];	//MACADDR4;
	io->PROM[9] = (u8) net_dev->macaddr[4];	//MACADDR4;
	io->PROM[10] = (u8) net_dev->macaddr[5];	//MACADDR5;
	io->PROM[11] = (u8) net_dev->macaddr[5];	//MACADDR5;

	io->PAR0 = io->PROM[0];
	io->PAR1 = io->PROM[2];
	io->PAR2 = io->PROM[4];
	io->PAR3 = io->PROM[6];
	io->PAR4 = io->PROM[8];
	io->PAR5 = io->PROM[10];

	//init Registers
	io->CR = 0x21;		//nic Stopped
	io->PSTART = 0;
	io->PSTOP = 0;
	io->BNRY = 0;
	io->TPSR = 0;
	io->TBCR0 = 0;
	io->TBCR1 = 0;
	io->ISR = 0;
	io->RSAR0 = 0;
	io->RSAR1 = 0;
	io->RBCR0 = 0;
	io->RBCR1 = 0;
	io->RCR = 0;
	io->TCR = 0;
	io->DCR = 0x84;		//set long addr bit
	io->IMR = 0;
	io->CURR = 0;

	/* raise reset interrupt */
	io->ISR = io->ISR | ISR_RST;

	/* init nic RAM */
	if (io->sram != NULL) {
		memset (io->sram, 0, PAGE_NUM * PAGE_SIZE);
	}
	else {
		io->sram = (u8 *) malloc (PAGE_NUM * PAGE_SIZE);
	}
	io->remote_read_offset = 0;
	io->remote_write_offset = 0;
	io->remote_read_count = 0;
	io->remote_write_count = 0;
	io->need_update = 0;
}

static void
net_rtl8019_update (struct device_desc *dev)
{
	struct device_interrupt *intr = &dev->intr;
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
	struct machine_config *mc = (struct machine_config *) dev->mach;
	struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = 0;

#if 0
	/* disabled: ISR_OVW instead of this */
	if ((!mc->mach_pending_intr (intr->interrupts[INT_RTL8019])))
#else
	if (!(io->ISR & ISR_OVW))
#endif
		if(net_dev->net_wait_packet (net_dev, &tv) == 0) rtl8019_input (dev);

	check_timeout(io->index);
}


static int
net_rtl8019_read_halfword (struct device_desc *dev, u32 addr, u16 * data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;

	int offset = (u8) (addr - dev->base);
	int ret = ADDR_HIT;

	offset >>= io->op_16;

	//DBG_PRINT("nic read begin: offset %x, io->ISR %x\n",offset,io->ISR);

	if (offset == 0x10) {	//remote read
		if (io->DCR & 0x1) {
			*data = remote_read_halfword (dev);
		}
	}

	return ret;
}

static int
net_rtl8019_read_byte (struct device_desc *dev, u32 addr, u8 * data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;

	int offset = (u8) (addr - dev->base);
	int ret = ADDR_HIT;

	offset >>= io->op_16;

	DBG_PRINT ("nic read begin: offset %x, io->ISR %x\n", offset,
		   io->ISR);

	*data = 0;
	if (offset == 0x10) {	//remote read
		/* FIXME: don't use DCR here. */
		*data = remote_read_byte (dev);
		return ret;
	}
	if (offset == 0x1f) {	//reset
		net_rtl8019_reset (dev);
		return ret;
	}
	if ((io->CR >> 6) == 0) {	//read page0
		switch (offset) {
		case 0x00:	//CR
			*data = io->CR;
			break;

		case 0x03:	//BNRY
			*data = io->BNRY;
			break;
		case 0x04:	// ISR
			*data = io->TSR;
			break;
		case 0x07:	// ISR
			*data = io->ISR;
			break;
		case 0x0c:	// ISR
			*data = io->RSR;
			break;
		case 0x0d:	// ISR
			io->CNTR0 = 0;
			*data = io->CNTR0;
			break;
		}
	}			//end  if page0

	if ((io->CR >> 6) == 1) {	//read page1
		switch (offset) {
		case 0x00:	//CR
			*data = io->CR;;
			break;

		case 0x01:	//PAR0 - PAR5 ,MAC addr
			*data = io->PAR0;
			break;

		case 0x02:
			*data = io->PAR1;
			break;

		case 0x03:
			*data = io->PAR2;
			break;

		case 0x04:
			*data = io->PAR3;
			break;

		case 0x05:
			*data = io->PAR4;
			break;
		case 0x06:
			*data = io->PAR5;
			break;
		case 0x07:	//CURR
			*data = io->CURR;
			break;
		}
	}			//end if PAGE1

	if ((io->CR >> 6) == 2) {	//read page2
		switch (offset) {
		case 0x00:	//CR
			*data = io->CR;
			break;

		case 0x01:	//PSTART
			*data = io->PSTART;
			break;

		case 0x02:	//PSTOP
			*data = io->PSTOP;
			break;

		case 0x04:	//TPSR
			*data = io->TPSR;
			break;

		case 0x0C:	//RCR
			*data = io->RCR;
			break;

		case 0x0D:	//TCR
			*data = io->TCR;
			break;

		case 0x0E:	//DCR
			*data = io->DCR;
			break;

		case 0x0F:	//IMR
			*data = io->IMR;
			break;
		}
	}			//end if page2

	return ret;

}

static int
net_rtl8019_write_halfword (struct device_desc *dev, u32 addr, u16 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;

	int offset = (u8) (addr - dev->base);
	int ret = ADDR_HIT;

	offset >>= io->op_16;

	if (offset == 0x10) {	//remote write
		if (io->DCR & 0x1) {
			remote_write_word (dev, (u16) data);
		}
	}
	return ret;
	//DBG_PRINT("nic write begin: offset %x, data %x\n",offset,data);
}

//offset should be 00-0f, 10 or 1f
static int
net_rtl8019_write_byte (struct device_desc *dev, u32 addr, u8 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;

	int offset = (u8) (addr - dev->base);
	int ret = ADDR_HIT;

	offset >>= io->op_16;

	if (offset == 0x10) {	//remote write
		/* FIXME: don't use DCR here. */
		//if (io->DCR & 0x2) {
		remote_write_byte (dev, (u8) data);
		//}
		return ret;
	}
	if (offset == 0x1f) {	//reset
		net_rtl8019_reset (dev);
		return ret;
	}
	if (offset == 0x00) { // 2007-03-14 by Anthony Lee : here we should set the CR directly.
		write_cr (dev, data);
		return ret;
	}
	if ((io->CR >> 6) == 0) {	//write page0
		switch (offset) {
#if 0
		case 0x00:	//CR
			write_cr (dev, data);
			break;
#endif

		case 0x01:	//PSTART
			io->PSTART = data;
			break;

		case 0x02:	//PSTOP
			io->PSTOP = data;
			break;

		case 0x03:	//BNRY
			io->BNRY = data;
			break;

		case 0x04:	//TPSR
			io->TPSR = data;
			break;

		case 0x05:	//TBCR0
			io->TBCR0 = data;
			break;

		case 0x06:	//TBCR1
			io->TBCR1 = data;
			break;

		case 0x07:	//ISR  (write means clear)
			io->ISR = (io->ISR & (~data));
			/*
			   if (io->IMR & io->ISR) {
			   }
			 */
			//UNSET_NET_INT ();
			break;

		case 0x08:	//RSAR0
			io->RSAR0 = data;
			break;

		case 0x09:	//RSAR1
			io->RSAR1 = data;
			break;

		case 0x0a:	//RBCR0
			io->RBCR0 = data;
			break;

		case 0x0b:	//RBCR1
			io->RBCR1 = data;
			break;

		case 0x0c:	//RCR
			io->RCR = data;
			break;

		case 0x0d:	//TCR
			io->TCR = data;
			break;

		case 0x0e:	//DCR
			io->DCR = data;
			break;

		case 0x0f:	//IMR
			//printf("IMR:%x -> %x. ISR:%x\n", io->IMR, data, io->ISR);
			io->IMR = data;
			/*
			   if (io->IMR & io->ISR) {
			   net_rtl8019_set_update_intr(dev);
			   }
			 */
			break;
		}
		return ret;
	}			//end if page0


	if ((io->CR >> 6) == 1) {	//write page1
		switch (offset) {
#if 0
		case 0x00:	//CR
			write_cr (dev, data);
			break;
#endif

		case 0x01:	//PAR0 - PAR5 ,MAC addr
			io->PAR0 = data;
			break;

		case 0x02:
			io->PAR1 = data;
			break;

		case 0x03:
			io->PAR2 = data;
			break;

		case 0x04:
			io->PAR3 = data;
			break;

		case 0x05:
			io->PAR4 = data;
			break;

		case 0x06:
			io->PAR5 = data;
			break;

		case 0x07:	//CURR
			io->CURR = data;
			break;
		}
		return ret;
	}			//end if page1

#if 0
	//yangye 2003-1-21
	//add write page2 ,only CR
	if ((io->CR >> 6) == 2) {	//write page2
		if (offset == 0x00) {
			write_cr (dev, data);
			return ret;
		}
	}			//end if page2
#endif

	DBG_PRINT ("error write page or error write register\n");
	return ret;
}

static void
rtl8019_input (struct device_desc *dev)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
	int packet_len, rtl8019_len;
	u8 buf[1600];
	u8 frame_header[4];
	u8 *bufptr;
	u8 *sramptr;
	u16 i, j, len;
	u32 free_pages, occupy_pages, next_page;

	if ((io->CR & CMD_STOP) || (io->TCR & TCR_LOOP_EXT)) {
		return;
	}

	if (io->CURR < io->BNRY) {
		free_pages = io->BNRY - io->CURR;
	}
	else {
		free_pages = (io->PSTOP - io->PSTART) - (io->CURR - io->BNRY);
	}


	packet_len = net_dev->net_read (net_dev, buf, sizeof (buf));
	if (packet_len < 0)
		return;
	/* if packet_len < 60, pad zero to 60 bytes length. */
	if (packet_len < 60) {
		memset (buf + packet_len, 0, 60 - packet_len);
		packet_len = 60;
	}

	rtl8019_len = packet_len + 4;

	occupy_pages = (rtl8019_len + 255) / PAGE_SIZE;

	/* check if we have available space to receive packet */
	if (occupy_pages > free_pages) {
		io->ISR |= ISR_OVW;
		if ((ISR_OVW & io->IMR)) {
			net_rtl8019_set_update_intr (dev);
		}
		DBG_PRINT ("%s: read data overflow!\n", __FUNCTION__);
		return;
	}

	next_page = io->CURR + occupy_pages;
	if (next_page >= io->PSTOP) {
		next_page -= io->PSTOP - io->PSTART;
	}

	//add 8019 frame header
	frame_header[0] = RSR_RXOK;
	frame_header[1] = next_page;
	frame_header[2] = (rtl8019_len & 0xFF);	//low 8 bit
	frame_header[3] = (rtl8019_len >> 8);	//high 8 bit

	/* check if we are in in promiscuous mode */

       /**
       * The tuntap does sometimes match the multicast address and
        * we _want_ to get broadcasts. Ignore this all and play
        * promisc.
        */
#if 0


	if (!(io->RCR & RCR_PRO)) {
		/* not in promiscuous mode */
		if (!is_broadcast (buf)) {
			DBG_PRINT
				(" destination address is a broadcast address!!!\n");
			if (!(io->RCR & RCR_AB)) {
				/* reject broadcast destination address */
				return;
			}
		}
		else if (is_multicast (buf)) {
			DBG_PRINT
				("destination address is a multicast address!!!\n");
			if (!(io->RCR & RCR_AM)) {
				/* reject multicast destination address */
				return;
			}
		}
		else if ((io->PAR0 != buf[0]) || (io->PAR1 != buf[1])
			 || (io->PAR2 != buf[2]) || (io->PAR3 != buf[3])
			 || (io->PAR4 != buf[4]) || (io->PAR5 != buf[5])) {
			return;
		}
	}
#endif

	sramptr = &io->sram[(io->CURR - START_PAGE) * PAGE_SIZE];

	if (next_page > io->CURR || ((io->CURR + occupy_pages) == io->PSTOP)) {
		memcpy (sramptr, frame_header, 4);
		memcpy (sramptr + 4, buf, packet_len);
	}
	else {
		int copy_bytes = (io->PSTOP - io->CURR) * PAGE_SIZE;
		memcpy (sramptr, frame_header, 4);
		memcpy (sramptr + 4, buf, copy_bytes - 4);

		sramptr = &io->sram[(io->PSTART - START_PAGE) * PAGE_SIZE];
		memcpy (sramptr, (void *) (buf + copy_bytes - 4),
			(packet_len - copy_bytes + 4));
	}

	io->CURR = next_page;

	io->RSR |= RSR_RXOK;
#if 0
	fprintf(stderr, "\n----(%s)(packet_len:%d)----", __FUNCTION__, packet_len);
	print_packet (sramptr + 4, packet_len);
#endif

	/*** send CPU a rx interrupt here! *****/
	io->ISR |= ISR_PRX;	//got packet int
	if ((ISR_PRX & io->IMR)) {
		//printf ("+++%s: raise RX interrupt, ISR:%x, IMR:%x\n", __FUNCTION__, io->ISR, io->IMR);
		set_time (io->index, rtl8019_len);
		io->need_update = 1;
		net_rtl8019_set_update_intr (dev);
	}
}

static u8
rtl8019_output (struct device_desc *dev, u8 * buf, u16 packet_len)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_rtl8019_io *io = (struct net_rtl8019_io *) dev->data;
	int len;

	if (io->CR & CMD_STOP) {	//nic in stop mode 
		return 0;
	}

#if 0
	fprintf(stderr, "\n----(%s)(packet_len:%d)----", __FUNCTION__, packet_len);
	print_packet (buf, packet_len);
#endif
	if ((len = net_dev->net_write (net_dev, buf, packet_len)) == -1) {
		fprintf (stderr, "write to tapif error in skyeye-ne2k.c\n");
		return -1;
	}
	//printf ("+++%s: trans\n", __FUNCTION__);
	return 0;
}

static int
net_rtl8019_setup(struct device_desc *dev)
{
	int i;
	int enough = 0;
	struct net_rtl8019_io *io;
	struct device_interrupt *intr = &dev->intr;

	dev->fini = net_rtl8019_fini;
	dev->reset = net_rtl8019_reset;
	dev->update = net_rtl8019_update;
	dev->read_byte = net_rtl8019_read_byte;
	dev->write_byte = net_rtl8019_write_byte;
	dev->read_halfword = net_rtl8019_read_halfword;
	dev->write_halfword = net_rtl8019_write_halfword;

	io = (struct net_rtl8019_io*)malloc(sizeof(struct net_rtl8019_io));
	if (io == NULL) return 1;

	memset(io, 0, sizeof (struct net_rtl8019_io));
	dev->data = (void*)io;

	net_rtl8019_reset(dev);

	/* see if we need to set default values. */
	set_device_default(dev, rtl8019_net_def);

	for (i = 0; i < MAX_DEVICE_NUM; i++) {
		if (rtl8019_devs[i] == NULL) {
			rtl8019_devs[i] = dev;
			io->index = i;
			enough = 1;
			break;
		}
	}

	if (enough == 0) return 1;

	return 0;
}

static int
net_rtl8019_8bits_setup(struct device_desc *dev)
{
	int ret = net_rtl8019_setup(dev);

	if (ret != 0) return ret;

	((struct net_rtl8019_io*)dev->data)->op_16 = 8;

	return 0;
}

static int
net_rtl8019_16bits_setup(struct device_desc *dev)
{
	int ret = net_rtl8019_setup(dev);

	if (ret != 0) return ret;

	((struct net_rtl8019_io*)dev->data)->op_16 = 1;

	return 0;
}

void
net_rtl8019_init (struct device_module_set *mod_set)
{
	int i;

	register_device_module("rtl8019", mod_set, &net_rtl8019_setup);
	register_device_module("rtl8019_8", mod_set, &net_rtl8019_8bits_setup);
	register_device_module("rtl8019_16", mod_set, &net_rtl8019_16bits_setup);

	for (i = 0; i < MAX_DEVICE_NUM; i++) rtl8019_devs[i] = NULL;
}