summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/plcdrv/arm/src/linux_drv.c
blob: 970afd8f04a5a9edc544b81e18542e5ff295aa03 (plain)
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
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
/* Cleopatre project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    linux_drv.c
 * \brief   Linux Driver layer
 * \ingroup Cleopatre - PlcDrv
 *
 * This file content the Linux Driver layer, this layer correspond to the
 * interface between the driver and Linux (it's a network Linux driver).
 */

#define DRV_NAME	"SPC300"
#define DRV_LAYER	"PLC "
#define DRV_RELDATE	__DATE__ " " __TIME__


#ifndef __UTESTS__
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
//#include <linux/version.h>
#include <linux/afe.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <asm/semaphore.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#include <linux/list.h>
#include <linux/netlink.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include "net/seq_check.h"

#include <asm/arch/nvram.h>
#include <asm/arch/hardware/spi.h>
#include <asm/arch/hardware/regbank.h>
#include <asm-arm/arch-spc300/ioctl.h>

#include "boot_params.h"
#include "common.h"
#include "linux_drv.h"
#include "processing.h"
#include "mailbox.h"
#include "hal.h"
#include "registers.h"


#else
#include "common.h"
#include "linux_drv.h"
#endif

MODULE_AUTHOR("SPiDCOM Technologies");
MODULE_DESCRIPTION("SPC300 PLC driver");
MODULE_LICENSE("SPiDCOM Technologies 2009");

/** Define Debug/Trace Level */
#define TRACE(...)      if(test_bit(TRACE_LINUX, (const volatile unsigned long*)&trace)) printk(KERN_INFO DRV_NAME": "DRV_LAYER": " __VA_ARGS__)
#define PRINTPKT(a,b,c) if(test_bit(TRACE_PACKET, (const volatile unsigned long*)&trace)) print_packet(a,b,c)

/** Define DSU trace modes */
#define DSU_TRACE_NONE      0
#define DSU_TRACE_PROC      1
#define DSU_TRACE_AHB       2
#define DSU_TRACE_ALL       3

#define MSEC_PER_JIFFY  (1000/HZ)  //aligned on HZ, which corresponds to 10ms

/** Define default numbers of buffers */
#define DEFAULT_NB_DATA_BUFFERS      97
#define DEFAULT_NB_MME_BUFFERS       1
#define DEFAULT_NB_INTERFACE_BUFFERS 2

/** Fake address used to detect that a leon_start_addr was provided as a
 *  module param. */
#define INVALID_LEON_START_ADDR    1

/** Max allowed TX message in the same time */
#define MBX_TX_POOL         (L2A_RING_SIZE / MAX_MSG_SIZE / 2)

/** These identify the driver base version */
static char version[] __devinitdata = DRV_NAME " PLC driver v" DRV_VERSION " (" DRV_RELDATE ")\n";

/** Structure used with Linux list to manage the sk_buff in used */
struct skb_addr_list {
    struct list_head list;
    uint32_t *pkt_addr;
    struct sk_buff *skb;
};

/** Our global netlink mutex */
static DEFINE_MUTEX(plcdrv_nl_mutex);

/** Our global net device */
static struct net_device *plcdrv_device;

/** Our global Major number */
static dev_t number;
/** Our global character device */
static struct cdev plcdrv_char_dev;
/** Our trace character device. */
static struct cdev trace_cdev;

/** Our plc directory in procfs. */
static struct proc_dir_entry *proc_plc_dir;

/** Our mutex between firmware download and network driver start-up */
static uint8_t write_called = 0;

/** Parameters for the module */
static int nb_rx_data_buffers = DEFAULT_NB_DATA_BUFFERS;
static int nb_rx_mme_buffers = DEFAULT_NB_MME_BUFFERS;
static int nb_rx_interface_buffers = DEFAULT_NB_INTERFACE_BUFFERS;
static uint32_t leon_start_addr = INVALID_LEON_START_ADDR;
static uint32_t dsu_ctrl = (LEON_DSU_BZ | LEON_DSU_BD | LEON_DSU_BW | LEON_DSU_BE | LEON_DSU_TE);
static uint32_t dsu_trace = DSU_TRACE_PROC;
static int debug = 0;
uint32_t trace = 0;

/** For debug dump buffer. */
int debug_dump_buffer_length_received = -1;
bool debug_dump_waiting_for_buffer = false;
const uint debug_dump_buffer_length = 2048;
DECLARE_WAIT_QUEUE_HEAD(debug_dump_wait_queue);
bool debug_dump_opened = false;

module_param(nb_rx_data_buffers, int, 0644);
MODULE_PARM_DESC(nb_rx_data_buffers, "Number of Data Ethernet buffers for PLC -> ARM exchanges");
module_param(nb_rx_mme_buffers, int, 0644);
MODULE_PARM_DESC(nb_rx_mme_buffers, "Number of MME Ethernet buffers for PLC -> ARM exchanges");
module_param(nb_rx_interface_buffers, int, 0644);
MODULE_PARM_DESC(nb_rx_interface_buffers, "Number of Interface Ethernet buffers for PLC -> ARM exchanges");
module_param(leon_start_addr, uint, 0644);
MODULE_PARM_DESC(leon_start_addr, "PLC code start address");
module_param_string(boot_params, custom_boot_params,
                    sizeof(custom_boot_params), 0644);
MODULE_PARM_DESC(custom_boot_params, "PLC Boot Parameters");
module_param(dsu_ctrl, uint, 0644);
MODULE_PARM_DESC(dsu_ctrl, "Configure the PLC Processor debugger");
module_param(dsu_trace, uint, 0644);
MODULE_PARM_DESC(dsu_trace, "Configure the PLC Processor trace (0=no ; 1=proc ; 2=AHB ; 3=all)");
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Let's the user start PLC Processor by debugger");
module_param(trace, uint, 0644);

void plcdrv_uninit(struct net_device *dev);


static const unsigned char __hexdigits[] = "0123456789ABCDEF";
static void sprintf_hex(unsigned char * str, const unsigned char * ptr, int len, unsigned char delim)
{
    int i, j=0;
    for(i=0; i<len; i++){
        if(i)str[j++]=delim;
        str[j++]=__hexdigits[ptr[i]>>4];
        str[j++]=__hexdigits[ptr[i]&0x0F];
    }
    str[j] = 0;
}
static void print_packet(const char * prefix, struct sk_buff * skb, int len)
{
    struct ethhdr * h;
    unsigned char src[20], dst[20], body[50];
    int l;

    h = (struct ethhdr *)skb->data;
    l =  len - 14 > 16 ? 16 : len - 14;
    sprintf_hex(src, &h->h_source[0], 6, ':');
    sprintf_hex(dst, &h->h_dest[0],   6, ':');
    sprintf_hex(body, ((unsigned char *)skb->data)+14,   l, ' ');

    printk(KERN_INFO "%s: len=%-4d proto=0x%04X src=%s dst=%s\n", prefix, len, be16_to_cpu(h->h_proto), src, dst);
    printk(KERN_INFO "             body=%s\n", body);
}

/**
 * Read Version number by /proc.
 *
 * \param  file  file structure.
 * \param  buffer  string pointer given by user.
 * \param  start  string pointer begin.
 * \param  offset  offset value.
 * \param  count  count parameter.
 * \param  eof  end of file.
 * \param  data  network device structure.
 * \return  new pointer position.
 */
static int plcdrv_readproc_version(char *buf, char **start, off_t offset, int count, int *eof, void *data)
{
    struct net_device *dev = (struct net_device*)data;
    struct net_priv *priv;
    char *p;

    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;

    p = buf;
    p += sprintf(p, "%s: %s\n", "PLC Driver", DRV_VERSION);
    p += sprintf(p, "%s: %s\n", "PLC Firmware", priv->version);
    *eof = 1;
    return p-buf+1;
}

/**
 * Configure AFE with the default configuration.
 */
static int init_afe(void)
{
    int ret;

#ifdef CONFIG_MACH_ARIZONA
    ret = afe_write_reg(0x03, 0x04)
        || afe_write_reg(0x04, 0x35)
        || afe_write_reg(0x06, 0x44)
        || afe_write_reg(0x0A, 0x7F)
        || afe_write_reg(0x0C, 0x43)
        || afe_write_reg(0x0D, 0x01)
        || afe_write_reg(0x0E, 0x80);
#else
   ret = afe_write_reg(0x04, 0x16)
       || afe_write_reg(0x05, 0x80)
       || afe_write_reg(0x07, 0x20)
       || afe_write_reg(0x0A, 0x7F) // TODO: check diff values 9865 / 9867
       || afe_write_reg(0x0B, 0x20)
       || afe_write_reg(0x0C, 0x51) // TODO: check diff values 9865 / 9867
       || afe_write_reg(0x0D, 0x01)
       || afe_write_reg(0x0E, 0x08)
       || afe_write_reg(0x03, 0xF8);
#endif

   if (ret)
        return -EFAULT;

    return 0;
}

/**
 * Read plc stats
 *
 * \param  file  file structure.
 * \param  buffer  string pointer given by user.
 * \param  start  string pointer begin.
 * \param  offset  offset value.
 * \param  count  count parameter.
 * \param  eof  end of file.
 * \param  data  network device structure.
 * \return  new pointer position.
 */
static int plcdrv_readproc_plc_stats(char *buf, char **start, off_t offset, int count, int *eof, void *data)
{
    struct net_device *dev = (struct net_device*)data;
    struct net_priv *priv;
    char *p;

    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;

    p = buf;
    p += sprintf (p, "Tx pool: %u\n", priv->plc_stats.tx_pool);
    p += sprintf (p, "Rx pool: %u\n", priv->plc_stats.rx_pool);
    p += sprintf (p, "L2A max length: %u\n", priv->halctx->L2A_max_length);
    *eof = 1;
    return p-buf+1;
}

/**
 * Set/Unset Reset the Leon processor.
 *
 * \param  activate  1 to activate reset.
 * \return  error code.
 */
int plcdrv_reset_leon(int activate)
{
    if(activate)
    {
        RB_RST_GROUP_VA |= RST_LEONSS;
        if(!(RB_RST_GROUP_VA & RST_LEONSS))
            return -1;

        RB_RST_MODULE_VA |= RST_LCPU;
        if(!(RB_RST_MODULE_VA & RST_LCPU))
            return -1;
    }
    else
    {
        RB_RST_GROUP_VA &= ~RST_LEONSS;
        if(RB_RST_GROUP_VA & RST_LEONSS)
            return -1;

        RB_RST_MODULE_VA &= ~RST_LCPU;
        if(RB_RST_MODULE_VA & RST_LCPU)
            return -1;
    }

    return 0;
}

/**
 * Set/Unset Reset the DSP.
 *
 * \param  activate  1 to activate reset.
 * \return  error code.
 */
int plcdrv_reset_dsp(int activate)
{
    if(activate)
    {
        RB_RST_GROUP_VA |= RST_DSP;
        if(!(RB_RST_GROUP_VA & RST_DSP))
            return -1;
    }
    else
    {
        RB_RST_GROUP_VA &= ~RST_DSP;
        if(RB_RST_GROUP_VA & RST_DSP)
            return -1;
    }

    return 0;
}

/**
 * Launch the Leon processor.
 *
 * \return  error code.
 */
int plcdrv_launch_leon(void)
{
    uint32_t *leon_dsu_ptr;
    uint32_t *leon_wd_ptr;
    uint32_t *leon_trace_ptr;
    int result=0;

    //Prepare Leon registers accesses
    leon_dsu_ptr = (uint32_t*)ioremap(LEON_DSU_CTRL_BASE_ADDR, 1);
    leon_trace_ptr = (uint32_t*)ioremap(LEON_TRACE_CTRL_BASE_ADDR, 1);
    leon_wd_ptr = (uint32_t*)ioremap(LEON_WD_BASE_ADDR, 1);

    //Set trace mode for leon
    switch(dsu_trace)
    {
    case DSU_TRACE_NONE:
        dsu_ctrl &= ~LEON_DSU_TE;
        *leon_trace_ptr &= ~(LEON_TRACE_PROC_EN | LEON_TRACE_AHB_EN);
        break;
    case DSU_TRACE_ALL:
        dsu_ctrl |= LEON_DSU_TE;
        *leon_trace_ptr |= (LEON_TRACE_PROC_EN | LEON_TRACE_AHB_EN);
        break;
    case DSU_TRACE_AHB:
        dsu_ctrl |= LEON_DSU_TE;
        *leon_trace_ptr &= ~(LEON_TRACE_PROC_EN);
        *leon_trace_ptr |= (LEON_TRACE_AHB_EN);
        break;
    case DSU_TRACE_PROC:
    default:
        dsu_ctrl |= LEON_DSU_TE;
        *leon_trace_ptr &= ~(LEON_TRACE_AHB_EN);
        *leon_trace_ptr |= (LEON_TRACE_PROC_EN);
    }

    //Refresh watchdog before running (for 10 seconds)
    *leon_wd_ptr = LEON_WD_REFRESH(10);

    //Configure DSU + Resume Leon processor execution
    *leon_dsu_ptr = (dsu_ctrl & ~(LEON_DSU_BN | LEON_DSU_FT));

    //Check if processor is running
    if((*leon_dsu_ptr) & LEON_DSU_BN)
    {
        result = -1;
    }

    //Release reset Leon register
    iounmap((void*)leon_dsu_ptr);
    iounmap((void*)leon_wd_ptr);
    iounmap((void*)leon_trace_ptr);

    return result;
}

/**
 * Find the start and size of the ROM info area.
 *
 * \param  file_end  end of the file.
 * \param[out]  rom_info_start  start of the ROM info area.
 * \param[out]  rom_info_size  size of the ROM info area.
 */
void plcdrv_find_rom_info(uint8_t *file_end, uint8_t **rom_info_start,
                          size_t *rom_info_size)
{
    uint8_t *infos;
    uint32_t infos_limit;
    int infos_size;

    BUG_ON(file_end == NULL);
    BUG_ON(rom_info_start == NULL);
    BUG_ON(rom_info_size == NULL);

    infos = file_end;
    infos_limit = (uint32_t)infos - ROM_INFO_MAX_SIZE;
    infos_size = 0;

    //No informations area
    if(*infos != ROM_INFO_DELIMITER)
    {
       goto not_found;
    }

    //Find start of informations area
    //We are at the end. We go back until we find a delimiter of the beginning
    //of the area.
    while(((uint32_t)infos > infos_limit-1)
          && ((*infos != ROM_INFO_DELIMITER)
              || (*(infos-1) != ROM_INFO_DELIMITER)))
    {
        infos--;
        infos_size++;
    }

    //Informations not found
    if((uint32_t)infos <= infos_limit)
    {
        goto not_found;
    }

    //There may be more than one delimiter at the beginning of the area.
    //As we need to kwow the exact start of the area,
    //we go up until we see all the delimiters.
    BUG_ON (*infos != ROM_INFO_DELIMITER);
    while(((uint32_t)infos > infos_limit-1) && (*infos == ROM_INFO_DELIMITER))
    {
        infos--;
        infos_size++;
    }

    *rom_info_start = infos + 1;
    *rom_info_size = infos_size;

    return;

not_found:
    *rom_info_start = NULL;
    *rom_info_size = 0;

    return;
}

/**
 * Get plc.rom version number.
 *
 * \param  rom_info_start  start of the ROM info area.
 * \param  rom_info_size  size of the ROM info area.
 * \param[out]  version  version result buffer.
 */
void plcdrv_get_rom_version(uint8_t *rom_info_start, size_t rom_info_size,
                            uint8_t *version)
{
    uint8_t *infos, *p;

    BUG_ON(version == NULL);

    if ((rom_info_start == NULL) || (rom_info_size == 0))
        goto unknown;

    infos = rom_info_start;

    //Skip delimiters at the beginning of the info area
    while ((infos < (rom_info_start + rom_info_size)
            && (*infos == ROM_INFO_DELIMITER)))
    {
        infos++;
    }

    if (infos >= (rom_info_start + rom_info_size))
    {
        goto unknown;
    }

    //Split informations, format is "key: value\n"
    for (p = infos ; p < (rom_info_start + rom_info_size) ; p++)
    {
        if((*p == ROM_INFO_DELIMITER) || (*p == ROM_INFO_KEY_DELIMITER))
            *p = '\0';
    }

    //Find version key
    while (strcasecmp(infos, ROM_VERSION_KEY))
    {
        infos += strlen(infos)+1; //to skip the key
        infos += strlen(infos)+1; //to skip the associated value
    }

    //Skip version key
    infos += strlen(infos)+1;

    //Copy version value without first space
    strncpy(version, infos+1, ROM_VERSION_SIZE);

    return;

unknown:
    strcpy(version, "Unknown");

    return;
}

/**
 * Open the character device.
 *
 * \param  inp  inode structure.
 * \param  filp  file structure.
 * \return  error code.
 */
int plcdrv_char_open(struct inode *inp, struct file *filp)
{
    filp->private_data = plcdrv_device;
    write_called = 0;
    return 0;
}

/**
 * Close the character device.
 *
 * \param  inp  inode structure.
 * \param  filp  file structure.
 * \return  error code.
 */
int plcdrv_char_close(struct inode *inp, struct file *filp)
{
    struct net_device *dev = filp->private_data;
    struct net_priv *priv;

    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;

    if(write_called)
    {
        uint8_t *file_end = (uint8_t *)VIRT_PLCCODE_BASE + filp->f_pos - 1;
        uint8_t *rom_info_start;
        size_t rom_info_size;

        plcdrv_find_rom_info(file_end, &rom_info_start, &rom_info_size);

        //Get firmware version number
        plcdrv_get_rom_version(rom_info_start, rom_info_size, priv->version);

        if (rom_info_start != NULL)
        {
            /* End of the .bin file that became the .rom file. */
            uint8_t *bin_eof = rom_info_start;

            //Pass boot parameters.
            //As a consequence, the rom info that was written to Cesar memory
            //will be overwritten. But that is not a problem because the rom
            //info is not used on Cesar side.
            plcdrv_pass_boot_params(bin_eof);
        }

        //LEON code downloaded let's start network device
        priv->firmware_written = 1;
    }

    return 0;
}

/**
 * Write on the character device,
 * to download LEON binary.
 *
 * \param  filp  file structure.
 * \param  buf  user data pointer.
 * \param  pos  position.
 * \return  error code.
 */
int plcdrv_char_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
    uint8_t *ptr;

    //Offset calculation
    ptr = (uint8_t*)VIRT_PLCCODE_BASE + *f_pos;

    //Download binary into Leon base address
    if(copy_from_user(ptr, buf, count))
    {
        return -EFAULT;
    }
    *f_pos += count;

    //Write at least one packet, prepare on start network device
    write_called = 1;
    return count;
}

/**
 * Read on the character device,
 * normally not used.
 *
 * \param  filp  file structure.
 * \param  buf  user data pointer.
 * \param  pos  position.
 * \return  error code.
 */
int plcdrv_char_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
    uint32_t *ptr;

    //Offset calculation
    ptr = (uint32_t*)VIRT_PLCCODE_BASE + *f_pos;

    //Load binary from Leon base address
    if(copy_to_user(buf, ptr, count))
    {
        return -EFAULT;
    }
    *f_pos += count;
    return count;
}

/**
 * Poll the character device.
 *
 * \param  filp  file structure
 * \param  wait  poll table structure
 * \return  error code
 */
unsigned int plcdrv_char_poll(struct file *filp, poll_table * wait)
{
    struct net_device *dev = NULL;
    struct net_priv *priv = NULL;

    if (NULL == filp)
        return POLLERR;

    dev = filp->private_data;
    if (NULL == dev)
        return POLLERR;

    priv = (struct net_priv *) dev->priv;
    if (NULL == priv)
        return POLLERR;

    poll_wait (filp, &priv->plc_select.wq, wait);

    if (1 == atomic_read (&priv->plc_select.plc_error))
        return POLLPRI;

    return 0;
}

int trace_cfops_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
    static void *debug_dump_buffer = NULL;
    static uint debug_dump_buffer_remaining_length = 0;
    uint copied_data;
    int error;

    //Check used parameter.
    BUG_ON(f_pos == NULL);
    //Sanity check.
    BUG_ON(debug_dump_opened == false);

    //Allocate debug dump buffer if needed.
    if(!debug_dump_buffer)
    {
        //Allocate.
        error = alloc_debug_dump_buffer(&debug_dump_buffer,
                                        debug_dump_buffer_length);
        if(error != 0)
            return error;
    }

    //If we have nothing to copy to user.
    if(debug_dump_buffer_remaining_length == 0)
    {
        //We need to handle the case where the buffer has been received while
        //this code is not executed anymore (ctrl+c for example). There can be
        //three cases:
        //  - we need to send a new buffer to Cesar because we do not have one
        //  to deal with,
        //  - the buffer has already been sent (debug_dump_waiting_for_buffer
        //  is set to true) but still not received
        //  (debug_dump_buffer_length_received set to -1): we need to wait.
        //  - the buffer has been received (debug_dump_buffer_length_received
        //  is not -1) but not processed by this code
        //  (debug_dump_waiting_for_buffer is set to false).

        //Give debug dump buffer to CESAR if this not already done.
        if(debug_dump_waiting_for_buffer == false)
        {
            //Debug dump buffer has been given to Cesar.
            debug_dump_buffer_length_received = -1;
            debug_dump_waiting_for_buffer = true;
            error = processing_debug_dump_buffer_send(debug_dump_buffer);
            if (error != 0)
            {
                free_debug_dump_buffer(debug_dump_buffer,
                                       debug_dump_buffer_length);
                debug_dump_buffer = NULL;
                debug_dump_waiting_for_buffer = false;
                return error;
            }
        }

        //Go to sleep until debug dump buffer has returned.
        error = wait_event_interruptible
            (debug_dump_wait_queue,
             debug_dump_buffer_length_received != -1);
        if (error != 0)
            return error;

        //Debug dump buffer received from Cesar.
        debug_dump_waiting_for_buffer = false;
        debug_dump_buffer_remaining_length = debug_dump_buffer_length_received;
    }

    //If this is not the last debug dump buffer.
    if(debug_dump_buffer_remaining_length)
    {
        BUG_ON(debug_dump_buffer_remaining_length
               > debug_dump_buffer_length_received);
        //How much can we copy?
        copied_data = min(count, debug_dump_buffer_remaining_length);
        //Copy debug dump buffer to buffer for reading.
        if(copy_to_user
           (buf,
            debug_dump_buffer
            + (debug_dump_buffer_length_received
               - debug_dump_buffer_remaining_length),
            copied_data))
        {
            return -EFAULT;
        }
        debug_dump_buffer_remaining_length -= copied_data;

    }
    else
    {
        //Finish, let's clean.
        free_debug_dump_buffer(debug_dump_buffer,
                               debug_dump_buffer_length);
        debug_dump_buffer = NULL;
        debug_dump_buffer_remaining_length = 0;

        copied_data = 0;
    }

    //Update what have been done.
    *f_pos += copied_data;
    return copied_data;
}

int trace_cfops_open(struct inode *inp, struct file *filp)
{
    //Open only one time.
    if(!debug_dump_opened)
    {
        debug_dump_opened = true;
        return 0;
    }
    else
        return -EBUSY;
}

int trace_cfops_release(struct inode *inp, struct file *filp)
{
    BUG_ON(debug_dump_opened == false);

    //Device now closed.
    debug_dump_opened = false;
    return 0;
}

/**
 * Find with data packet address the sk_buff structure address,
 * this address was stored in a Linux list.
 *
 * \param  data_addr  the packet data address.
 * \return  sk_buff address.
 */
static struct sk_buff* get_skb_addr(uint32_t data_addr)
{
    struct skb_addr_list *entry;
    struct sk_buff *skb = NULL;
    struct net_priv *priv = plcdrv_device->priv;
    unsigned long flags;

    spin_lock_irqsave(&priv->lock, flags);
    //Check in the list to find the sk_buff corresponding to our data_addr
    //No need to use the _safe version as we stop as soon we have deleted the
    //element.
    list_for_each_entry(entry, &priv->list_head_skbs, list)
    {
        if(((uint32_t)entry->pkt_addr) == data_addr)
        {
            //Get the content.
            skb = entry->skb;
            //We should never have a match without any sk_buff.
            BUG_ON(skb == NULL);
            //Remove the element first.
            list_del(&entry->list);
            //Delete the entry.
            kfree(entry);
            //Go out of the loop.
            break;
        }
    }
    spin_unlock_irqrestore(&priv->lock, flags);
    return skb;
}// get_skb_addr

/**
 * Store the sk_buff->data address in a Linux list
 * this address is associated with sk_buff address.
 *
 * \param  skb  the sk_buff address.
 * \return  error code.
 */
static uint32_t put_skb_addr(struct sk_buff *skb)
{
    struct skb_addr_list *entry;
    struct net_priv *priv = plcdrv_device->priv;
    unsigned long flags;

    spin_lock_irqsave(&priv->lock, flags);
    //Create a new entry for the list of sk_buffs in used
    entry = (struct skb_addr_list*)kmalloc(sizeof(struct skb_addr_list), GFP_ATOMIC);
    if(entry == NULL)
    {
        spin_unlock_irqrestore(&priv->lock, flags);
        return -ENOMEM;
    }

    entry->skb = skb;
    entry->pkt_addr = (uint32_t*)skb->data;

    //Add the new entry in the list
    list_add_tail(&entry->list, &priv->list_head_skbs);

    spin_unlock_irqrestore(&priv->lock, flags);
    return 0;
}// put_skb_addr

uint32_t prepare_buffer_to_hw(uint32_t addr, unsigned int len,
                              enum data_direction data_dir)
{
    return dma_map_single(NULL, (void *) addr, len, data_dir);
}// prepare_buffer_to_hw

uint32_t prepare_buffer_from_hw(uint32_t addr, unsigned int len,
                                enum data_direction data_dir)
{
    dma_unmap_single(NULL, addr, len, data_dir);

    //Return the corresponding virtual addr
    return (uint32_t)(dma_to_virt(NULL, addr));
}// prepare_buffer_from_hw

/**
 * Allocate a buffer to the pool
 * and send to the communication layer.
 *
 * \param  type  type of buffer to allocate.
 * \return  error code.
 */
int alloc_buffer(enum buffer_type type)
{
    struct sk_buff *skb;
    int result;

    //Allocate an sk_buff
    skb = alloc_skb(PKT_BUF_SZ, GFP_ATOMIC | GFP_DMA);
    if(!skb)
    {
        printk(KERN_ERR DRV_NAME": Error allocating RX buffer for %s\n",plcdrv_device->name);
        return -ENOMEM;
    }

    TRACE("AddBuffer: virt@skb=%x ; virt@skb->data=%x\n",(uint32_t)skb, (uint32_t)skb->data);

    //Store the sk_buff in sk_buff list in used
    if((result = put_skb_addr(skb)))
    {
        kfree_skb(skb);
        return result;
    }

    //Send this allocated pointer to lower layer
    if((result = processing_buffer_add((void*)skb->data, type)))
    {
        kfree_skb(skb);
    }

    return result;
}// alloc_buffer

int alloc_debug_dump_buffer(void **debug_dump_buffer,
                            int debug_dump_buffer_length)
{
    //Check parameters.
    BUG_ON(debug_dump_buffer == NULL || *debug_dump_buffer != NULL);

    //Allocate debug dump buffer.
    *debug_dump_buffer = kmalloc(debug_dump_buffer_length, GFP_ATOMIC | GFP_DMA);

    if(!*debug_dump_buffer)
        return -ENOMEM;

    return 0;
}

/**
 * Release a buffer from the pool.
 *
 * \param  packet  packet pointer.
 * \param  reason  freeing reason.
 * \return  error code.
 */
int free_buffer(void *packet, enum free_reason reason)
{
    struct net_priv *priv = NULL;
    struct sk_buff *skb;

    //Check pointers
    priv = (struct net_priv *)plcdrv_device->priv;
    if(priv == NULL)
        return -1;

    if(packet)
    {
        //Find the sk_buff associated to this packet
        skb = get_skb_addr((uint32_t)packet);
        TRACE("FreeBuffer: virt@skb=%x  ;  virt@skb->data=%x  ;  @packet=%x\n",(uint32_t)skb, (uint32_t)skb->data,(uint32_t)packet);
        if(!skb)
        {
            printk(KERN_ERR DRV_NAME": %s: error getting sb_buff from received pointer\n", plcdrv_device->name);
            return -1;
        }

        //Check the free reason for stats
        switch(reason)
        {
        case RX_DROP:     priv->stats.rx_dropped++;
                          break;
        case TX_DROP:     priv->stats.tx_dropped++;
                          priv->plc_stats.tx_pool--;
                          if(netif_queue_stopped(plcdrv_device))
                              netif_wake_queue(plcdrv_device);
                          break;
        case TX_COMPLETE: priv->stats.tx_packets++;
                          priv->stats.tx_bytes += skb->len;
                          priv->plc_stats.tx_pool--;
                          if(netif_queue_stopped(plcdrv_device))
                              netif_wake_queue(plcdrv_device);
                          break;
        default:
                          break;
        }

        //Free sk_buff
        kfree_skb(skb);

        return 0;
    }
    else
    {
        printk(KERN_ERR DRV_NAME": %s: error freeing a NULL buffer\n", plcdrv_device->name);
        return -1;
    }
}// free_buffer

void free_debug_dump_buffer(void *debug_dump_buffer,
                            int debug_dump_buffer_length)
{
    //Check parameters.
    BUG_ON(debug_dump_buffer == NULL || debug_dump_buffer_length == 0);

    //Free.
    kfree(debug_dump_buffer);
}

/**
 * Receive a packet that need to transit through NETLINK.
 *
 * \param  skb  frame structure.
 * \param  dev  device structure config
 * \param  sock netlink socket where to transmit
 * \param  pid  netlink message pid
 */
void plcdrv_netlink_rx(struct sk_buff *skb, struct net_device *dev, struct sock *sock, uint32_t pid)
{
    struct net_priv *priv;
    struct sk_buff *nlskb;
    struct nlmsghdr *nlh;

    //Check pointers
    if(skb == NULL)
        return;
    if(dev == NULL)
        return;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return;

    //Allocate a new sk_buff to add netlink header
    nlskb = alloc_skb(NLMSG_LENGTH(skb->len), GFP_ATOMIC);
    if(!nlskb)
    {
        printk(KERN_ERR DRV_NAME": %s: Error allocating a netlink sk_buff\n", dev->name);
        return;
    }

    //Fill netlink header
    nlh = NLMSG_PUT(nlskb, 0, 0, NLMSG_DONE, NLMSG_LENGTH(skb->len) - sizeof(*nlh));
    NETLINK_CB(nlskb).pid = 0; //from kernel
    NETLINK_CB(nlskb).dst_group = 0; //unicast

    //Fill this new sk_buff with the old one after netlink header
    memcpy(NLMSG_DATA(nlh), skb->data, skb->len);

    //Suppress old sk_buff
    kfree_skb(skb);

    //Send netlink to plcd
    netlink_unicast(sock, nlskb, pid, MSG_DONTWAIT);

    return;

nlmsg_failure:
    kfree_skb(skb);
    kfree_skb(nlskb);
}// plcdrv_netlink_rx

/**
 * Receive a packet.
 *
 * \param  packet  packet pointer.
 * \param  length  packet length.
 * \return  error code.
 */
int plcdrv_rx(void *packet, int length, enum pkt_dest dst)
{
    struct net_priv *priv = NULL;
    struct sk_buff *skb;
    int result;

    priv = (struct net_priv *)plcdrv_device->priv;
    if(priv == NULL)
        return -1;

    //Check packet length size
    if((length <= 0) || (length > PKT_BUF_SZ))
       return -1;

    if(packet)
    {

        //Find the sk_buff address
        skb = get_skb_addr((uint32_t)packet);

        //Pass data to the Linux internal receive level
        skb->dev = plcdrv_device;
        skb_put(skb, length);
        TRACE("\nRX: virt@skb=%x  ;  virt@skb->data=%x  ;  @packet=%x\n",(uint32_t)skb, (uint32_t)skb->data,(uint32_t)packet);
        PRINTPKT("RX",skb, length);
        skb->ip_summed = CHECKSUM_UNNECESSARY;
        priv->stats.rx_packets++;
        priv->stats.rx_bytes += length;
        if(dst == NETLINK_DRV)
        {
            plcdrv_netlink_rx(skb, plcdrv_device, priv->nl_drv_sock, priv->nl_drv_pid);
        }
        else if(dst == NETLINK_MME)
        {
            plcdrv_netlink_rx(skb, plcdrv_device, priv->nl_mme_sock, priv->nl_mme_pid);
        }
        else
        {
            skb->protocol = eth_type_trans(skb, plcdrv_device);

            seq_check_rx(&priv->seq_check_ctx, skb);

            netif_rx(skb);
        }
        result = 0;
    }
    else
    {
        printk(KERN_ERR DRV_NAME": %s: Error Receiving a NULL buffer\n", plcdrv_device->name);
        result = -1;
    }
    return result;
}// plcdrv_rx

static void plcdrv_post_tx (struct sk_buff *skb, struct net_device *dev, int status)
{
    struct net_priv *priv = NULL;
    priv = (struct net_priv *)dev->priv;

    //Tx queue is nearly full we must stop it
    if(status == NEARLY_FULL ||
       priv->plc_stats.tx_pool == MBX_TX_POOL - 1)
    {
        netif_stop_queue(dev);
        priv->plc_stats.tx_pool++; // update plc stats
        TRACE("TX queue nearly Full\n");
    }
    //Tx queue is full drop the frame
    else if(status == FULL)
    {
        netif_stop_queue(dev);
        priv->stats.tx_errors++;
        priv->stats.tx_fifo_errors++;
        kfree_skb(skb);
        printk(KERN_WARNING DRV_NAME ": %s: TX queue is Full\n", dev->name);
    }
    // Transmit success
    else
    {
        priv->plc_stats.tx_pool++; // update plc stats
    }

    //Handle transmit
    dev->trans_start = jiffies;

    return;
}

/**
 * Transmit frame procedure.
 *
 * \param  skb  frame structure.
 * \param  dev  device structure.
 * \return  error code.
 */
int plcdrv_data_tx(struct sk_buff *skb, struct net_device *dev)
{
    struct net_priv *priv = NULL;
    int status;

    //Check pointers
    if(skb == NULL)
        return -1;
    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;

    TRACE("\nTX: virt@skb=%x  ;  virt@skb->data=%x  ;  skb->len=%d\n",(uint32_t)skb, (uint32_t)skb->data, skb->len);
    PRINTPKT("TX",skb, skb->len);

    seq_check_tx(&priv->seq_check_ctx, skb);

    //Store the sk_buff in sk_buff list in used
    if((status = put_skb_addr(skb)))
    {
        return status;
    }

    //Send buffer to lower layers
    status = processing_send((void *)skb->data, skb->len, DATA);

    /* check result and update stats */
    plcdrv_post_tx (skb, dev, status);

    return 0;
}// plcdrv_data_tx

/**
 * Transmit a frame received from NETLINK.
 *
 * \param  skb  frame structure.
 * \param  dev  net device structure
 * \param  sock netlink socket structure
 */
int plcdrv_netlink_tx(struct sk_buff *nlskb, struct net_device *dev, struct sock *sock)
{
    struct sk_buff *skb;
    struct net_priv *priv;
    buffer_type_t type;
    int status;

    //Check pointers
    if(nlskb == NULL)
        return -1;
    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;

    mutex_lock(&plcdrv_nl_mutex);

    //Prepare a sk_buff without netlink header
    skb_pull(nlskb, NLMSG_HDRLEN);

    //Allocate a new sk_buff because nlskb will be suppress at this end of this function
    skb = alloc_skb(nlskb->len, GFP_ATOMIC | GFP_DMA);
    if(!skb)
    {
        printk(KERN_ERR DRV_NAME": %s: Error allocating a netlink sk_buff\n", dev->name);
        return -1;
    }

    //Fill this new sk_buff with the old one
    memcpy(skb->data, nlskb->data, nlskb->len);

    //Prepare this new sk_buff
    skb_put(skb, nlskb->len);

    TRACE("\nTX: virt@skb=%x  ;  virt@skb->data=%x  ;  skb->len=%d\n",(uint32_t)skb, (uint32_t)skb->data, skb->len);
    PRINTPKT("TX",skb, skb->len);

    //Store the sk_buff in sk_buff list in used
    if((status = put_skb_addr(skb)))
    {
        return status;
    }

    if((get_eth_mme_type(skb->data) == HPAV_MME_P_FCALL) ||
       (get_eth_mme_type(skb->data) == HPAV_MME_P_SNIFFER))
    {
        type = INTERFACE;
    }
    else
    {
        type = MME;
    }
    //send buffer to lower layer
    status = processing_send((void *)skb->data, skb->len, type);

    /* check result and update stats */
    plcdrv_post_tx (skb, dev, status);

    mutex_unlock(&plcdrv_nl_mutex);

    return 0;
}// plcdrv_netlink_tx

/**
 * Transmit a frame received from DRV NETLINK.
 *
 * \param  skb  frame structure.
 */
void plcdrv_netlink_drv_tx (struct sk_buff *nlskb)
{
    struct net_priv *priv = (struct net_priv *)plcdrv_device->priv;
    struct nlmsghdr *nlh;
    if(NULL == nlskb)
        return;
    nlh = nlmsg_hdr(nlskb);
    plcdrv_netlink_tx(nlskb, plcdrv_device, priv->nl_drv_sock);
}

/**
 * Transmit a frame received from MME NETLINK.
 *
 * \param  skb  frame structure.
 */
void plcdrv_netlink_mme_tx (struct sk_buff *nlskb)
{
    struct net_priv *priv = (struct net_priv *)plcdrv_device->priv;
    struct nlmsghdr *nlh;
    if(NULL == nlskb)
        return;
    nlh = nlmsg_hdr(nlskb);
    plcdrv_netlink_tx(nlskb, plcdrv_device, priv->nl_mme_sock);
}

/**
 * Interrupt Handler Watchdog procedure.
 *
 * \param  irq  interrupt number
 * \param  dev_id  device structure
 * \return  error code
 */
irqreturn_t
plcdrv_it_wd (int irq, void * dev_id)
{
    struct net_device *dev = NULL;
    struct net_priv *priv = NULL;

    if (NULL == dev_id)
        return IRQ_NONE;

    dev = (struct net_device *) dev_id;
    priv = netdev_priv(dev);
    if (NULL == priv)
        return IRQ_NONE;

    /* Is some process waiting on a select() ? */
    if (!waitqueue_active (&priv->plc_select.wq))
    {
        /* Reset */
        /* Can't use one of the kernel reboot functions (like kernel_restart())
         * because of the EXPORT_SYMBOL_GPL. */
        RB_RST_GLOBAL_VA = 1;
    }
    else
    {
        /* Wake up the process waiting on the select(), and let it handle the
         * situation. */
        atomic_set (&priv->plc_select.plc_error, 1);
        wake_up (&priv->plc_select.wq);
        L2Awd_it_disable (priv->halctx);
    }

    return IRQ_HANDLED;
} // plcdrv_it_wd

/**
 * Interrupt Handler Receive procedure.
 *
 * \param  irq  interrupt number.
 * \param  dev_id  device structure.
 * \return  error code.
 */
irqreturn_t plcdrv_it_rx(int irq, void * dev_id)
{
    struct net_device *dev = NULL;
    struct net_priv *priv = NULL;

    //Check pointer
    dev = (struct net_device*)dev_id;
    if(dev == NULL)
       return IRQ_NONE;

    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return IRQ_NONE;

    //Call mailbox receive for interrupt part
    mailbox_receive_isr();

    //Call the tasklet for real mailbox reception part
    tasklet_schedule(&priv->tasklet_it_rx);

    return IRQ_HANDLED;
}// plcdrv_it_rx

/**
 * Bottom Half Receive procedure.
 *
 * \param  dev_id  device structure.
 */
void plcdrv_bh_rx(unsigned long dev_id)
{
    struct net_device *dev = NULL;
    struct net_priv *priv = NULL;
    unsigned int budget;

    //Check pointer
    dev = (struct net_device*)dev_id;
    if(dev == NULL)
       return;

    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return;

    budget = PLCDRV_RX_BUDGET;

    //Call mailbox receive
    mailbox_receive(&budget);

    //Re-call us if budget is exhausted
    if(!budget)
        tasklet_schedule(&priv->tasklet_it_rx);

}// plcdrv_bh_rx

/**
 * Finish the transmit frame procedure.
 *
 * \param  irq  interrupt number.
 * \param  dev  device structure.
 */
irqreturn_t plcdrv_it_txdone(int irq, void * dev_id)
{
    struct net_device *dev;

    //Check pointer
    if(dev_id == NULL)
       return IRQ_NONE;

    dev = (struct net_device*)dev_id;
    TRACE("Transmit Done IT\n");

    //A packet was just freeing by the hardware,
    //we can restart the tx queue
    if(netif_queue_stopped(dev))
        netif_wake_queue(dev);

    //call lowest layer that a tx_done is arrived
    mailbox_txdone();
    TRACE("Transmit Done IT end\n");

    return IRQ_HANDLED;
}// plcdrv_it_txdone

/**
 * Read packet status from the device.
 *
 * \param  dev  device structure.
 * \return  the device stats.
 */
struct net_device_stats *plcdrv_stats(struct net_device *dev)
{
    struct net_priv *priv = NULL;

    //Check pointers
    if(dev == NULL)
        return NULL;

    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return NULL;

    //Give statistics
    return &priv->stats;
}// plcdrv_stats

/**
 * Change the MTU.
 *
 * \param  dev  device structure.
 * \param  new_mtu  the new mtu value.
 * \return  error code.
 */
int plcdrv_change_mtu(struct net_device *dev, int new_mtu)
{
    //Check pointers
    if(dev == NULL)
        return -1;

    TRACE("change_mtu\n");

    //Check arguments
    if(new_mtu < 64 || new_mtu > 1508)
        return -EINVAL;
    else
    {
        //Change the MTU
        dev->mtu = new_mtu;
        TRACE("change_mtu end\n");
        return 0;
    }
}// plcdrv_change_mtu

/**
 * Change the MAC address.
 *
 * \param  dev  device structure.
 * \param  p  mac addr source.
 * \return  error code.
 */
int plcdrv_set_mac_address(struct net_device *dev, void *p)
{
    struct sockaddr *addr = p;

    //Check pointers
    if(dev == NULL)
        return -1;
    if(p == NULL)
        return -1;

    TRACE("set_mac_address\n");

    //Store the new address for Linux
    memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);

    TRACE("set_mac_address end\n");

    return 0;
}// plcdrv_set_mac_address

/**
 * User control device interface.
 *
 * \param  dev  device structure.
 * \param  ifr  user exchange structure.
 * \param  cmd  command to execute.
 * \return  error code.
 */
int plcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
    struct net_priv *priv = NULL;
    struct plcdrv_setpid user_data = {0};
    
    //Check pointers
    if(ifr == NULL)
        return -1;
    if(dev == NULL)
        return -1;

    TRACE("ioctl\n");

    //Find the command
    switch(cmd)
    {
        case PLCDRV_IOCTL_SETPID:

            /* Check validity of driver private data */
            if (NULL == (priv = (struct net_priv *) dev->priv))
                return -EFAULT;

            /* Get user data */
            if (copy_from_user (&user_data, ifr->ifr_data, sizeof (user_data)))
                return -EFAULT;

            /* During initialization, plcd & managerd register their pid
             * for reception on drv & mme netlink */
            if (NETLINK_PLC_DRV == user_data.nl)
            {
                priv->nl_drv_pid = user_data.pid;
                printk (KERN_INFO "%s: plcd registered with pid %d\n", __FUNCTION__,
                       priv->nl_drv_pid);
            }
            if (NETLINK_PLC_MME == user_data.nl)
            {
                priv->nl_mme_pid = user_data.pid;
                printk (KERN_INFO "%s: managerd registered with pid %d\n", __FUNCTION__,
                       priv->nl_mme_pid);
            }

            /* In case of another netlink, do nothing */
            break;

        default:
            return -EOPNOTSUPP;
    }

    TRACE("ioctl end\n");

    return 0;
}// plcdrv_ioctl

/**
 * Initialize the device.
 *
 * \param  dev  device structure.
 * \return  error code.
 */
int plcdrv_open(struct net_device *dev)
{
    struct net_priv *priv = NULL;
    struct init_info info;
    int i;
    int result;

    //Check pointers
    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;

    //Wait Downloading Leon binary into RAM to really open the driver
    if(!priv->firmware_written)
    {
        printk(KERN_ERR DRV_NAME": %s You need to download SPiDCOM PLC binary before\n",dev->name);
        return -EPERM;
    }

    //Prepare mutex
    spin_lock_init(&priv->lock);

    //Prepare sk_buff in used list
    INIT_LIST_HEAD(&priv->list_head_skbs);

    //Configure AFE with the default configuration
    if (init_afe())
        return -EFAULT;

    //Allocate rings for each mailbox
    if((priv->virt_ring_base_addr =
        (uint32_t)dma_alloc_coherent(NULL,
                                     A2L_RING_SIZE+L2A_RING_SIZE,
                                     &priv->phys_ring_base_addr,
                                     GFP_ATOMIC|GFP_DMA)
        ) == 0)
    {
        printk(KERN_ERR DRV_NAME": Error allocating mailboxes rings for %s\n", dev->name);
        return -ENOMEM;
    }
    TRACE("OPEN: virt@rings=%x  ;  phys@rings=%x\n",priv->virt_ring_base_addr, priv->phys_ring_base_addr);

    //Flush rings for each mailbox
    memset((void*)priv->virt_ring_base_addr, 0, A2L_RING_SIZE+L2A_RING_SIZE);

    //Request Receive IRQ
    if(request_irq(priv->num_mbx_it, plcdrv_it_rx, 0, dev->name, dev) != 0)
    {
        printk(KERN_ERR DRV_NAME ": %s - interrupt %d request fail\n", dev->name, dev->irq);
        result = -ENODEV;
        goto err_open;
    }

    //Request Transmit Acknowledge IRQ
    if(request_irq(priv->num_mbx_it_ack, plcdrv_it_txdone, 0, dev->name, dev) != 0)
    {
        printk(KERN_ERR DRV_NAME ": %s - interrupt %d request fail\n", dev->name, dev->irq);
        result = -ENODEV;
        goto err_rq_ack;
    }

    //Request Leon Watchdog IRQ
    if(request_irq(priv->num_mbx_it_wd, plcdrv_it_wd