summaryrefslogtreecommitdiffstats
path: root/drivers/net/fec.h
blob: 1d421606984fd4925e0a058ffb6bbc57e1332b06 (plain) (blame)
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
/****************************************************************************/

/*
 *	fec.h  --  Fast Ethernet Controller for Motorola ColdFire SoC
 *		   processors.
 *
 *	(C) Copyright 2000-2005, Greg Ungerer (gerg@snapgear.com)
 *	(C) Copyright 2000-2001, Lineo (www.lineo.com)
 */

/****************************************************************************/
#ifndef FEC_H
#define	FEC_H
/****************************************************************************/

#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
    defined(CONFIG_M520x) || defined(CONFIG_M532x)
/*
 *	Just figures, Motorola would have to change the offsets for
 *	registers in the same peripheral device on different models
 *	of the ColdFire!
 */
typedef struct fec {
	unsigned long	fec_reserved0;
	unsigned long	fec_ievent;		/* Interrupt event reg */
	unsigned long	fec_imask;		/* Interrupt mask reg */
	unsigned long	fec_reserved1;
	unsigned long	fec_r_des_active;	/* Receive descriptor reg */
	unsigned long	fec_x_des_active;	/* Transmit descriptor reg */
	unsigned long	fec_reserved2[3];
	unsigned long	fec_ecntrl;		/* Ethernet control reg */
	unsigned long	fec_reserved3[6];
	unsigned long	fec_mii_data;		/* MII manage frame reg */
	unsigned long	fec_mii_speed;		/* MII speed control reg */
	unsigned long	fec_reserved4[7];
	unsigned long	fec_mib_ctrlstat;	/* MIB control/status reg */
	unsigned long	fec_reserved5[7];
	unsigned long	fec_r_cntrl;		/* Receive control reg */
	unsigned long	fec_reserved6[15];
	unsigned long	fec_x_cntrl;		/* Transmit Control reg */
	unsigned long	fec_reserved7[7];
	unsigned long	fec_addr_low;		/* Low 32bits MAC address */
	unsigned long	fec_addr_high;		/* High 16bits MAC address */
	unsigned long	fec_opd;		/* Opcode + Pause duration */
	unsigned long	fec_reserved8[10];
	unsigned long	fec_hash_table_high;	/* High 32bits hash table */
	unsigned long	fec_hash_table_low;	/* Low 32bits hash table */
	unsigned long	fec_grp_hash_table_high;/* High 32bits hash table */
	unsigned long	fec_grp_hash_table_low;	/* Low 32bits hash table */
	unsigned long	fec_reserved9[7];
	unsigned long	fec_x_wmrk;		/* FIFO transmit water mark */
	unsigned long	fec_reserved10;
	unsigned long	fec_r_bound;		/* FIFO receive bound reg */
	unsigned long	fec_r_fstart;		/* FIFO receive start reg */
	unsigned long	fec_reserved11[11];
	unsigned long	fec_r_des_start;	/* Receive descriptor ring */
	unsigned long	fec_x_des_start;	/* Transmit descriptor ring */
	unsigned long	fec_r_buff_size;	/* Maximum receive buff size */
} fec_t;

#else

/*
 *	Define device register set address map.
 */
typedef struct fec {
	unsigned long	fec_ecntrl;		/* Ethernet control reg */
	unsigned long	fec_ievent;		/* Interrupt even reg */
	unsigned long	fec_imask;		/* Interrupt mask reg */
	unsigned long	fec_ivec;		/* Interrupt vec status reg */
	unsigned long	fec_r_des_active;	/* Receive descriptor reg */
	unsigned long	fec_x_des_active;	/* Transmit descriptor reg */
	unsigned long	fec_reserved1[10];
	unsigned long	fec_mii_data;		/* MII manage frame reg */
	unsigned long	fec_mii_speed;		/* MII speed control reg */
	unsigned long	fec_reserved2[17];
	unsigned long	fec_r_bound;		/* FIFO receive bound reg */
	unsigned long	fec_r_fstart;		/* FIFO receive start reg */
	unsigned long	fec_reserved3[4];
	unsigned long	fec_x_wmrk;		/* FIFO transmit water mark */
	unsigned long	fec_reserved4;
	unsigned long	fec_x_fstart;		/* FIFO transmit start reg */
	unsigned long	fec_reserved5[21];
	unsigned long	fec_r_cntrl;		/* Receive control reg */
	unsigned long	fec_max_frm_len;	/* Maximum frame length reg */
	unsigned long	fec_reserved6[14];
	unsigned long	fec_x_cntrl;		/* Transmit Control reg */
	unsigned long	fec_reserved7[158];
	unsigned long	fec_addr_low;		/* Low 32bits MAC address */
	unsigned long	fec_addr_high;		/* High 16bits MAC address */
	unsigned long	fec_hash_table_high;	/* High 32bits hash table */
	unsigned long	fec_hash_table_low;	/* Low 32bits hash table */
	unsigned long	fec_r_des_start;	/* Receive descriptor ring */
	unsigned long	fec_x_des_start;	/* Transmit descriptor ring */
	unsigned long	fec_r_buff_size;	/* Maximum receive buff size */
	unsigned long	reserved8[9];
	unsigned long	fec_fifo_ram[112];	/* FIFO RAM buffer */
} fec_t;

#endif /* CONFIG_M5272 */


/*
 *	Define the buffer descriptor structure.
 */
typedef struct bufdesc {
	unsigned short	cbd_sc;			/* Control and status info */
	unsigned short	cbd_datlen;		/* Data length */
	unsigned long	cbd_bufaddr;		/* Buffer address */
} cbd_t;


/*
 *	The following definitions courtesy of commproc.h, which where
 *	Copyright (c) 1997 Dan Malek (dmalek@jlc.net).
 */
#define BD_SC_EMPTY     ((ushort)0x8000)        /* Recieve is empty */
#define BD_SC_READY     ((ushort)0x8000)        /* Transmit is ready */
#define BD_SC_WRAP      ((ushort)0x2000)        /* Last buffer descriptor */
#define BD_SC_INTRPT    ((ushort)0x1000)        /* Interrupt on change */
#define BD_SC_CM        ((ushort)0x0200)        /* Continous mode */
#define BD_SC_ID        ((ushort)0x0100)        /* Rec'd too many idles */
#define BD_SC_P         ((ushort)0x0100)        /* xmt preamble */
#define BD_SC_BR        ((ushort)0x0020)        /* Break received */
#define BD_SC_FR        ((ushort)0x0010)        /* Framing error */
#define BD_SC_PR        ((ushort)0x0008)        /* Parity error */
#define BD_SC_OV        ((ushort)0x0002)        /* Overrun */
#define BD_SC_CD        ((ushort)0x0001)        /* ?? */

/* Buffer descriptor control/status used by Ethernet receive.
*/
#define BD_ENET_RX_EMPTY        ((ushort)0x8000)
#define BD_ENET_RX_WRAP         ((ushort)0x2000)
#define BD_ENET_RX_INTR         ((ushort)0x1000)
#define BD_ENET_RX_LAST         ((ushort)0x0800)
#define BD_ENET_RX_FIRST        ((ushort)0x0400)
#define BD_ENET_RX_MISS         ((ushort)0x0100)
#define BD_ENET_RX_LG           ((ushort)0x0020)
#define BD_ENET_RX_NO           ((ushort)0x0010)
#define BD_ENET_RX_SH           ((ushort)0x0008)
#define BD_ENET_RX_CR           ((ushort)0x0004)
#define BD_ENET_RX_OV           ((ushort)0x0002)
#define BD_ENET_RX_CL           ((ushort)0x0001)
#define BD_ENET_RX_STATS        ((ushort)0x013f)        /* All status bits */

/* Buffer descriptor control/status used by Ethernet transmit.
*/
#define BD_ENET_TX_READY        ((ushort)0x8000)
#define BD_ENET_TX_PAD          ((ushort)0x4000)
#define BD_ENET_TX_WRAP         ((ushort)0x2000)
#define BD_ENET_TX_INTR         ((ushort)0x1000)
#define BD_ENET_TX_LAST         ((ushort)0x0800)
#define BD_ENET_TX_TC           ((ushort)0x0400)
#define BD_ENET_TX_DEF          ((ushort)0x0200)
#define BD_ENET_TX_HB           ((ushort)0x0100)
#define BD_ENET_TX_LC           ((ushort)0x0080)
#define BD_ENET_TX_RL           ((ushort)0x0040)
#define BD_ENET_TX_RCMASK       ((ushort)0x003c)
#define BD_ENET_TX_UN           ((ushort)0x0002)
#define BD_ENET_TX_CSL          ((ushort)0x0001)
#define BD_ENET_TX_STATS        ((ushort)0x03ff)        /* All status bits */


/****************************************************************************/
#endif /* FEC_H */
r Linus Torvalds 2005-04-17 00:20:36 +0200 committer Linus Torvalds 2005-04-17 00:20:36 +0200 Linux-2.6.12-rc2' href='/openslx/kernel-qcow2-linux.git/commit/drivers/mtd/mtdchar.c?id=1da177e4c3f41524e886b7f1b8a0c1fc7321cac2'>1da177e4c3f4
f1a28c02843e ^
cd874237d97f ^
1da177e4c3f4


1da177e4c3f4
aeb5d727062a ^
1da177e4c3f4

5aa82940b23d ^
1da177e4c3f4
97894cda5773 ^
6071239ef194 ^



97894cda5773 ^
402d326519c1 ^
1da177e4c3f4
6071239ef194 ^

1da177e4c3f4

cd874237d97f ^












402d326519c1 ^
1da177e4c3f4
aeb5d727062a ^
cd874237d97f ^
1da177e4c3f4
6071239ef194 ^

1da177e4c3f4
97894cda5773 ^
f1a28c02843e ^

cd874237d97f ^
f1a28c02843e ^
6071239ef194 ^

f1a28c02843e ^
cd874237d97f ^
f1a28c02843e ^


6071239ef194 ^
5aa82940b23d ^
6071239ef194 ^
1da177e4c3f4





f1a28c02843e ^

1da177e4c3f4


7eafaed55f4b ^
aeb5d727062a ^
1da177e4c3f4
97894cda5773 ^
cd874237d97f ^

1da177e4c3f4
f1a28c02843e ^

1da177e4c3f4










f1a28c02843e ^

1da177e4c3f4




97894cda5773 ^
1da177e4c3f4






97894cda5773 ^
1da177e4c3f4

b802c0741103 ^








1da177e4c3f4
b802c0741103 ^
97894cda5773 ^
1da177e4c3f4



f1a28c02843e ^

31f4233baeaa ^




f1a28c02843e ^












31f4233baeaa ^
f4a43cfcecfc ^
31f4233baeaa ^
1da177e4c3f4

97894cda5773 ^
9a1fcdfd4bee ^
25985edcedea ^
9a1fcdfd4bee ^
1da177e4c3f4


9a1fcdfd4bee ^
1da177e4c3f4

f4a43cfcecfc ^
1da177e4c3f4






31f4233baeaa ^

1da177e4c3f4




97894cda5773 ^
1da177e4c3f4

b802c0741103 ^
1da177e4c3f4




f1a28c02843e ^

1da177e4c3f4






97894cda5773 ^
1da177e4c3f4

97894cda5773 ^
1da177e4c3f4





b802c0741103 ^







1da177e4c3f4
b802c0741103 ^
97894cda5773 ^
1da177e4c3f4



1da177e4c3f4



97894cda5773 ^
f1a28c02843e ^

31f4233baeaa ^








f1a28c02843e ^














31f4233baeaa ^


1da177e4c3f4









1da177e4c3f4

b802c0741103 ^
1da177e4c3f4












34a82443b79d ^
f1a28c02843e ^




























977185404046 ^
















0040476b0efa ^
977185404046 ^











df1f1d1cb43b ^


977185404046 ^























































cc26c3cd3d1c ^

















0ceacf36e9c4 ^
cc26c3cd3d1c ^













d0f7959e2b70 ^









d0f7959e2b70 ^








a7e93dcd9aac ^



d0f7959e2b70 ^















55929332c92e ^
1da177e4c3f4
f1a28c02843e ^

1da177e4c3f4


73c619ea0e49 ^
97894cda5773 ^
1da177e4c3f4










97894cda5773 ^
1da177e4c3f4







b67c5f87c13f ^

bcc98a46eafd ^
1da177e4c3f4
b67c5f87c13f ^
1da177e4c3f4

5e59be1f351b ^


b67c5f87c13f ^




1da177e4c3f4
b67c5f87c13f ^
1da177e4c3f4



a0c5a3944ce1 ^
73c619ea0e49 ^





64f60710568d ^

73c619ea0e49 ^
1da177e4c3f4



0dc54e9f33e2 ^
1da177e4c3f4


aeb5d727062a ^
1da177e4c3f4

95b93a0cd466 ^
1da177e4c3f4







0dc54e9f33e2 ^



















1da177e4c3f4



97894cda5773 ^
1da177e4c3f4


97894cda5773 ^
1da177e4c3f4
























977185404046 ^
1da177e4c3f4
977185404046 ^

1da177e4c3f4
977185404046 ^


1da177e4c3f4
1da177e4c3f4




977185404046 ^
8593fbc68b0d ^
977185404046 ^

1da177e4c3f4
977185404046 ^


1da177e4c3f4


aea7cea9fa9e ^



























1da177e4c3f4

175428b2b3ee ^
1da177e4c3f4
175428b2b3ee ^
1da177e4c3f4




175428b2b3ee ^
1da177e4c3f4




175428b2b3ee ^
1da177e4c3f4
175428b2b3ee ^
1da177e4c3f4




175428b2b3ee ^
1da177e4c3f4


9938424f0c4d ^













5bd34c091a04 ^
1da177e4c3f4

5bd34c091a04 ^










d25ade71ef80 ^
5bd34c091a04 ^

1da177e4c3f4






97894cda5773 ^
1da177e4c3f4





















34a82443b79d ^
31f4233baeaa ^




f1a28c02843e ^




81dba488792b ^
31f4233baeaa ^









f1a28c02843e ^

31f4233baeaa ^






f1a28c02843e ^

31f4233baeaa ^















175428b2b3ee ^
31f4233baeaa ^
f1a28c02843e ^
31f4233baeaa ^
175428b2b3ee ^
31f4233baeaa ^


175428b2b3ee ^
31f4233baeaa ^



cc26c3cd3d1c ^
f1a28c02843e ^

cc26c3cd3d1c ^

f1a28c02843e ^


cc26c3cd3d1c ^








f1a28c02843e ^


































d0f7959e2b70 ^















1da177e4c3f4






55929332c92e ^



5aa82940b23d ^
55929332c92e ^
5aa82940b23d ^
55929332c92e ^



977185404046 ^















0b6585ce05f1 ^
977185404046 ^

5aa82940b23d ^
977185404046 ^






























55929332c92e ^
977185404046 ^

5aa82940b23d ^
977185404046 ^





402d326519c1 ^











































dd02b67d5e9e ^
























402d326519c1 ^
402d326519c1 ^
dd02b67d5e9e ^
402d326519c1 ^





d54b1fdb1d9f ^
1da177e4c3f4



55929332c92e ^
977185404046 ^


1da177e4c3f4

402d326519c1 ^



1da177e4c3f4

51139adac92f ^

cd874237d97f ^
c74a1cbb3cac ^
cd874237d97f ^



51139adac92f ^
cd874237d97f ^






















1da177e4c3f4

cd874237d97f ^
1f24b5a8ecbb ^
cd874237d97f ^
dad0db318b39 ^
cd874237d97f ^



9bc7b38731dd ^

cd874237d97f ^




















1da177e4c3f4



cd874237d97f ^
f03c65993b98 ^
cd874237d97f ^
dad0db318b39 ^
1da177e4c3f4




1f24b5a8ecbb ^
1da177e4c3f4



90160e13b0bf ^
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
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
  














                                                                             


   

                         
                     
                      
                       

                         

                        
                        
                              
                         
                        
                        
                          
                                 
                          
 
                        
 
                                     
                               
                                                    
 
  

                                                               
   

                             
                          

                                 
 

                                                                    

                                                       

                       
                      
                      
                      
                                      
                      
                      
                                    




                               
                                               
                                            
 
                       







                                                           
                    
                             
                                  
                              


                                              
                                              
                                                        

                               
                               
                                           
 



                                   
 
                                      
                                    

                              

         












                                                                         
 
                                                                 
                                                                            
                              
                                    

                              
         
 

                                                
                              
                                    

                              
         
                           


                                 
    
                                 
                   





                                                                        

                                                       


                                               
                                    
                                                      
                               
 

                       
                            

                                  










                                                                                       

                                                       




                              
 






                                             
 

                                                                   








                                                           
                       
 
                                             



                                               

                                          




                                                                                      












                                                              
                        
                                                                        
                 

                                                                      
                                                  
                                                                          
                                                                           
                                                       


                                                                    
                                                                     

                                                              
                                            






                                                       

                                          




                                    
 

         
                    




                                                                                              

                                                       






                                              
 

                               
 





                                          







                                                           
                       
 
                                             



                                               



                                                     
 

                                          








                                                                                       














                                                               


                                                                              









                                               

         
                    












                                                                        
                          




























                                                                   
















                                                                   
                                                                        











                                                                     


                                              























































                                                                     

















                                                                         
                                                                        













                                                             









                                                              








                                                                       



                                                                           















                                                                            
                                                              
 

                                                       


                                               
                                  
 










                                                         
 







                                                                             

                                                 
                                                          
 
                                                         

                                       


                                                   




                                                                 
                                       
 



                        
                                               





                                                 

                                                   
                                                                            



                                       
                        


                                         
                                                 

                                      
                                                                    







                                                         



















                                                                                



                                                                 
 


                                                                      
 
























                                                                                
                                                           
 

                                                                    
                                      


                                                                               
                      




                                       
                                                           
 

                                                                   
                                      


                                                                        


                      



























                                                                               

                     
                                             
 
                                                                




                                          
                                                                        




                       
                                             
 
                                                                




                                          
                                                                          


                      













                                                                             
                              

                          










                                                                              
                                                       

                                                                         






                                       
 





















                                                                
                          




                                                             




                                                     
                                









                                                                 

                                          






                                                                              

                              















                                                                            
                                      
 
                                                   
                                       
                                                                


                                             
                                                                              



                      
                                                                          

                          

                                                   


                                           








                                                                


































                                                               















                                                                 






                              



                                                                        
                               
                                        
                                 



                   















                                                                   
                                            

                    
                               






























                                                                   
                                                                

         
                                 





                          











































                                                                           
























                                                                                
 
                         
         





                                                       
                                                



                                      
                                             


                                           

                                    



                                                   

  

                                                                            
 
                                                                                   



                                                   
                                  






















                                                                           

                                    
                
 
                                                                  
                                                     



                                                                                

         




















                                                                              



                                        
                                               
                              
                                                 
                                                                      




                             
                                           



                                                                    
                                           
/*
 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <linux/device.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/mutex.h>
#include <linux/backing-dev.h>
#include <linux/compat.h>
#include <linux/mount.h>
#include <linux/blkpg.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/map.h>

#include <asm/uaccess.h>

#define MTD_INODE_FS_MAGIC 0x11307854
static DEFINE_MUTEX(mtd_mutex);
static struct vfsmount *mtd_inode_mnt __read_mostly;

/*
 * Data structure to hold the pointer to the mtd device as well
 * as mode information ofr various use cases.
 */
struct mtd_file_info {
	struct mtd_info *mtd;
	struct inode *ino;
	enum mtd_file_modes mode;
};

static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
{
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;

	switch (orig) {
	case SEEK_SET:
		break;
	case SEEK_CUR:
		offset += file->f_pos;
		break;
	case SEEK_END:
		offset += mtd->size;
		break;
	default:
		return -EINVAL;
	}

	if (offset >= 0 && offset <= mtd->size)
		return file->f_pos = offset;

	return -EINVAL;
}



static int mtd_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	int devnum = minor >> 1;
	int ret = 0;
	struct mtd_info *mtd;
	struct mtd_file_info *mfi;
	struct inode *mtd_ino;

	DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");

	/* You can't open the RO devices RW */
	if ((file->f_mode & FMODE_WRITE) && (minor & 1))
		return -EACCES;

	mutex_lock(&mtd_mutex);
	mtd = get_mtd_device(NULL, devnum);

	if (IS_ERR(mtd)) {
		ret = PTR_ERR(mtd);
		goto out;
	}

	if (mtd->type == MTD_ABSENT) {
		put_mtd_device(mtd);
		ret = -ENODEV;
		goto out;
	}

	mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
	if (!mtd_ino) {
		put_mtd_device(mtd);
		ret = -ENOMEM;
		goto out;
	}
	if (mtd_ino->i_state & I_NEW) {
		mtd_ino->i_private = mtd;
		mtd_ino->i_mode = S_IFCHR;
		mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
		unlock_new_inode(mtd_ino);
	}
	file->f_mapping = mtd_ino->i_mapping;

	/* You can't open it RW if it's not a writeable device */
	if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
		iput(mtd_ino);
		put_mtd_device(mtd);
		ret = -EACCES;
		goto out;
	}

	mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
	if (!mfi) {
		iput(mtd_ino);
		put_mtd_device(mtd);
		ret = -ENOMEM;
		goto out;
	}
	mfi->ino = mtd_ino;
	mfi->mtd = mtd;
	file->private_data = mfi;

out:
	mutex_unlock(&mtd_mutex);
	return ret;
} /* mtd_open */

/*====================================================================*/

static int mtd_close(struct inode *inode, struct file *file)
{
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;

	DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");

	/* Only sync if opened RW */
	if ((file->f_mode & FMODE_WRITE) && mtd->sync)
		mtd->sync(mtd);

	iput(mfi->ino);

	put_mtd_device(mtd);
	file->private_data = NULL;
	kfree(mfi);

	return 0;
} /* mtd_close */

/* FIXME: This _really_ needs to die. In 2.5, we should lock the
   userspace buffer down and use it directly with readv/writev.
*/
#define MAX_KMALLOC_SIZE 0x20000

static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
{
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
	size_t retlen=0;
	size_t total_retlen=0;
	int ret=0;
	int len;
	char *kbuf;

	DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");

	if (*ppos + count > mtd->size)
		count = mtd->size - *ppos;

	if (!count)
		return 0;

	/* FIXME: Use kiovec in 2.5 to lock down the user's buffers
	   and pass them directly to the MTD functions */

	if (count > MAX_KMALLOC_SIZE)
		kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
	else
		kbuf=kmalloc(count, GFP_KERNEL);

	if (!kbuf)
		return -ENOMEM;

	while (count) {

		if (count > MAX_KMALLOC_SIZE)
			len = MAX_KMALLOC_SIZE;
		else
			len = count;