Linux Audio

Check our new training course

Embedded Linux Audio

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

Bootlin logo

Elixir Cross Referencer

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 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
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
/**
 * @file me6000_ao.c
 *
 * @brief ME-6000 analog output subdevice instance.
 * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
 * @author Guenter Gebhardt
 * @author Krzysztof Gantzke	(k.gantzke@meilhaus.de)
 */

/*
 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
 *
 * This file 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __KERNEL__
#  define __KERNEL__
#endif

/* Includes
 */
#include <linux/version.h>
#include <linux/module.h>

#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/delay.h>

#include <linux/workqueue.h>

#include "medefines.h"
#include "meinternal.h"
#include "meerror.h"

#include "medebug.h"
#include "meids.h"
#include "me6000_reg.h"
#include "me6000_ao_reg.h"
#include "me6000_ao.h"

/* Defines
 */

static int me6000_ao_query_range_by_min_max(me_subdevice_t *subdevice,
					    int unit,
					    int *min,
					    int *max, int *maxdata, int *range);

static int me6000_ao_query_number_ranges(me_subdevice_t *subdevice,
					 int unit, int *count);

static int me6000_ao_query_range_info(me_subdevice_t *subdevice,
				      int range,
				      int *unit,
				      int *min, int *max, int *maxdata);

static int me6000_ao_query_timer(me_subdevice_t *subdevice,
				 int timer,
				 int *base_frequency,
				 long long *min_ticks, long long *max_ticks);

static int me6000_ao_query_number_channels(me_subdevice_t *subdevice,
					   int *number);

static int me6000_ao_query_subdevice_type(me_subdevice_t *subdevice,
					  int *type, int *subtype);

static int me6000_ao_query_subdevice_caps(me_subdevice_t *subdevice,
					  int *caps);

static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
					       int cap, int *args, int count);

/** Remove subdevice. */
static void me6000_ao_destructor(struct me_subdevice *subdevice);

/** Reset subdevice. Stop all actions. Reset registry. Disable FIFO. Set output to 0V and status to 'none'. */
static int me6000_ao_io_reset_subdevice(me_subdevice_t *subdevice,
					struct file *filep, int flags);

/** Set output as single */
static int me6000_ao_io_single_config(me_subdevice_t *subdevice,
				      struct file *filep,
				      int channel,
				      int single_config,
				      int ref,
				      int trig_chan,
				      int trig_type, int trig_edge, int flags);

/** Pass to user actual value of output. */
static int me6000_ao_io_single_read(me_subdevice_t *subdevice,
				    struct file *filep,
				    int channel,
				    int *value, int time_out, int flags);

/** Write to output requed value. */
static int me6000_ao_io_single_write(me_subdevice_t *subdevice,
				     struct file *filep,
				     int channel,
				     int value, int time_out, int flags);

/** Set output as streamed device. */
static int me6000_ao_io_stream_config(me_subdevice_t *subdevice,
				      struct file *filep,
				      meIOStreamConfig_t *config_list,
				      int count,
				      meIOStreamTrigger_t *trigger,
				      int fifo_irq_threshold, int flags);

/** Wait for / Check empty space in buffer. */
static int me6000_ao_io_stream_new_values(me_subdevice_t *subdevice,
					  struct file *filep,
					  int time_out, int *count, int flags);

/** Start streaming. */
static int me6000_ao_io_stream_start(me_subdevice_t *subdevice,
				     struct file *filep,
				     int start_mode, int time_out, int flags);

/** Check actual state. / Wait for end. */
static int me6000_ao_io_stream_status(me_subdevice_t *subdevice,
				      struct file *filep,
				      int wait,
				      int *status, int *values, int flags);

/** Stop streaming. */
static int me6000_ao_io_stream_stop(me_subdevice_t *subdevice,
				    struct file *filep,
				    int stop_mode, int flags);

/** Write datas to buffor. */
static int me6000_ao_io_stream_write(me_subdevice_t *subdevice,
				     struct file *filep,
				     int write_mode,
				     int *values, int *count, int flags);

/** Interrupt handler. Copy from buffer to FIFO. */
static irqreturn_t me6000_ao_isr(int irq, void *dev_id);

/** Copy data from circular buffer to fifo (fast) in wraparound mode. */
inline int ao_write_data_wraparound(me6000_ao_subdevice_t *instance, int count,
				    int start_pos);

/** Copy data from circular buffer to fifo (fast).*/
inline int ao_write_data(me6000_ao_subdevice_t *instance, int count,
			 int start_pos);

/** Copy data from circular buffer to fifo (slow).*/
inline int ao_write_data_pooling(me6000_ao_subdevice_t *instance, int count,
				 int start_pos);

/** Copy data from user space to circular buffer. */
inline int ao_get_data_from_user(me6000_ao_subdevice_t *instance, int count,
				 int *user_values);

/** Stop presentation. Preserve FIFOs. */
inline int ao_stop_immediately(me6000_ao_subdevice_t *instance);

/** Function for checking timeout in non-blocking mode. */
static void me6000_ao_work_control_task(struct work_struct *work);

/* Functions
 */

static int me6000_ao_io_reset_subdevice(me_subdevice_t *subdevice,
					struct file *filep, int flags)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;
	uint32_t tmp;
	uint32_t ctrl;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (flags) {
		PERROR("Invalid flag specified.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	ME_SUBDEVICE_ENTER;

	instance->status = ao_status_none;
	instance->ao_control_task_flag = 0;
	cancel_delayed_work(&instance->ao_control_task);
	instance->timeout.delay = 0;
	instance->timeout.start_time = jiffies;

	//Stop state machine.
	err = ao_stop_immediately(instance);

	//Remove from synchronous start.
	spin_lock(instance->preload_reg_lock);
	tmp = inl(instance->preload_reg);
	tmp &=
	    ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
	      ao_idx);
	outl(tmp, instance->preload_reg);
	PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->preload_reg - instance->reg_base, tmp);
	*instance->preload_flags &=
	    ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
	      ao_idx);

	//Reset triggering flag
	*instance->triggering_flags &= ~(0x1 << instance->ao_idx);
	spin_unlock(instance->preload_reg_lock);

	if (instance->fifo) {
		//Set single mode, dissable FIFO, dissable external trigger, block interrupt.
		ctrl = ME6000_AO_MODE_SINGLE;

		//Block ISM.
		ctrl |=
		    (ME6000_AO_CTRL_BIT_STOP |
		     ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);

		outl(ctrl, instance->ctrl_reg);
		PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->ctrl_reg - instance->reg_base, ctrl);
		//Set speed
		outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
		//Reset interrupt latch
		inl(instance->irq_reset_reg);
	}

	instance->hardware_stop_delay = HZ / 10;	//100ms

	//Set output to 0V
	outl(0x8000, instance->single_reg);
	PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->single_reg - instance->reg_base, 0x8000);

	instance->circ_buf.head = 0;
	instance->circ_buf.tail = 0;
	instance->preloaded_count = 0;
	instance->data_count = 0;
	instance->single_value = 0x8000;
	instance->single_value_in_fifo = 0x8000;

	//Set status to signal that device is unconfigured.
	instance->status = ao_status_none;
	//Signal reset if user is on wait.
	wake_up_interruptible_all(&instance->wait_queue);

	ME_SUBDEVICE_EXIT;

	return err;
}

static int me6000_ao_io_single_config(me_subdevice_t *subdevice,
				      struct file *filep,
				      int channel,
				      int single_config,
				      int ref,
				      int trig_chan,
				      int trig_type, int trig_edge, int flags)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;
	uint32_t ctrl;
	uint32_t sync;
	unsigned long cpu_flags;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. ID=%d\n", instance->ao_idx);

	// Checking parameters
	if (flags) {
		PERROR
		    ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if (instance->fifo) {	//Stream hardware (with or without fifo)
		if ((trig_edge == ME_TRIG_TYPE_SW)
		    && (trig_edge != ME_TRIG_EDGE_NONE)) {
			PERROR
			    ("Invalid trigger edge. Software trigger has not edge.\n");
			return ME_ERRNO_INVALID_TRIG_EDGE;
		}

		if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
			switch (trig_edge) {
			case ME_TRIG_EDGE_ANY:
			case ME_TRIG_EDGE_RISING:
			case ME_TRIG_EDGE_FALLING:
				break;

			default:
				PERROR("Invalid trigger edge.\n");
				return ME_ERRNO_INVALID_TRIG_EDGE;
			}
		}

		if ((trig_type != ME_TRIG_TYPE_SW)
		    && (trig_type != ME_TRIG_TYPE_EXT_DIGITAL)) {
			PERROR
			    ("Invalid trigger type. Trigger must be software or digital.\n");
			return ME_ERRNO_INVALID_TRIG_TYPE;
		}
	} else {		//Single
		if (trig_edge != ME_TRIG_EDGE_NONE) {
			PERROR
			    ("Invalid trigger edge. Single output trigger hasn't own edge.\n");
			return ME_ERRNO_INVALID_TRIG_EDGE;
		}

		if (trig_type != ME_TRIG_TYPE_SW) {
			PERROR
			    ("Invalid trigger type. Trigger must be software.\n");
			return ME_ERRNO_INVALID_TRIG_TYPE;
		}

	}

	if ((trig_chan != ME_TRIG_CHAN_DEFAULT)
	    && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS)) {
		PERROR("Invalid trigger channel specified.\n");
		return ME_ERRNO_INVALID_TRIG_CHAN;
	}
/*
	if ((trig_type == ME_TRIG_TYPE_EXT_DIGITAL) && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS))
	{
		PERROR("Invalid trigger channel specified. Must be synchronous when digital is choose.\n");
		return ME_ERRNO_INVALID_TRIG_CHAN;
	}
*/
	if (ref != ME_REF_AO_GROUND) {
		PERROR
		    ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
		return ME_ERRNO_INVALID_REF;
	}

	if (single_config != 0) {
		PERROR
		    ("Invalid single config specified. Only one range for anlog outputs is available.\n");
		return ME_ERRNO_INVALID_SINGLE_CONFIG;
	}

	if (channel != 0) {
		PERROR
		    ("Invalid channel number specified. Analog output have only one channel.\n");
		return ME_ERRNO_INVALID_CHANNEL;
	}

	ME_SUBDEVICE_ENTER;

	//Subdevice running in stream mode!
	if ((instance->status >= ao_status_stream_run_wait)
	    && (instance->status < ao_status_stream_end)) {
		PERROR("Subdevice is busy.\n");
		ME_SUBDEVICE_EXIT;

		return ME_ERRNO_SUBDEVICE_BUSY;
	}
/// @note For single all calls (config and write) are erasing previous state!

	instance->status = ao_status_none;

	// Correct single mirrors
	instance->single_value_in_fifo = instance->single_value;

	//Stop device
	err = ao_stop_immediately(instance);
	if (err) {
		PERROR_CRITICAL("FSM IS BUSY!\n");
		ME_SUBDEVICE_EXIT;

		return ME_ERRNO_SUBDEVICE_BUSY;
	}

	if (instance->fifo) {	// Set control register.
		spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
		// Set stop bit. Stop streaming mode (If running.).
		ctrl = inl(instance->ctrl_reg);
		//Reset all bits.
		ctrl =
		    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
		if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
			PINFO("External digital trigger.\n");

			if (trig_edge == ME_TRIG_EDGE_ANY) {
//                                      ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE | ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
				instance->ctrl_trg =
				    ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
				    ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
			} else if (trig_edge == ME_TRIG_EDGE_FALLING) {
//                                      ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
				instance->ctrl_trg =
				    ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
			} else if (trig_edge == ME_TRIG_EDGE_RISING) {
				instance->ctrl_trg = 0x0;
			}
		} else if (trig_type == ME_TRIG_TYPE_SW) {
			PDEBUG("SOFTWARE TRIGGER\n");
			instance->ctrl_trg = 0x0;
		}
		outl(ctrl, instance->ctrl_reg);
		PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->ctrl_reg - instance->reg_base, ctrl);
		spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
	} else {
		PDEBUG("SOFTWARE TRIGGER\n");
	}

	// Set preload/synchronization register.
	spin_lock(instance->preload_reg_lock);

	if (trig_type == ME_TRIG_TYPE_SW) {
		*instance->preload_flags &=
		    ~(ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx);
	} else			//if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL)
	{
		*instance->preload_flags |=
		    ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
	}

	if (trig_chan == ME_TRIG_CHAN_DEFAULT) {
		*instance->preload_flags &=
		    ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);
	} else			//if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS)
	{
		*instance->preload_flags |=
		    ME6000_AO_SYNC_HOLD << instance->ao_idx;
	}

	//Reset hardware register
	sync = inl(instance->preload_reg);
	PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->preload_reg - instance->reg_base, sync);
	sync &= ~(ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx);
	sync |= ME6000_AO_SYNC_HOLD << instance->ao_idx;

	//Output configured in default mode (safe one)
	outl(sync, instance->preload_reg);
	PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->preload_reg - instance->reg_base, sync);
	spin_unlock(instance->preload_reg_lock);

	instance->status = ao_status_single_configured;

	ME_SUBDEVICE_EXIT;

	return err;
}

static int me6000_ao_io_single_read(me_subdevice_t *subdevice,
				    struct file *filep,
				    int channel,
				    int *value, int time_out, int flags)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;

	unsigned long j;
	unsigned long delay = 0;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
		PERROR("Invalid flag specified. %d\n", flags);
		return ME_ERRNO_INVALID_FLAGS;
	}

	if ((instance->status >= ao_status_stream_configured)
	    && (instance->status <= ao_status_stream_end)) {
		PERROR("Subdevice not configured to work in single mode!\n");
		return ME_ERRNO_PREVIOUS_CONFIG;
	}

	if (channel != 0) {
		PERROR("Invalid channel number specified.\n");
		return ME_ERRNO_INVALID_CHANNEL;
	}

	if (time_out < 0) {
		PERROR("Invalid timeout specified.\n");
		return ME_ERRNO_INVALID_TIMEOUT;
	}

	ME_SUBDEVICE_ENTER;
	if ((!flags) && (instance->status == ao_status_single_run_wait)) {	//Blocking mode. Wait for trigger.
		if (time_out) {
			delay = (time_out * HZ) / 1000;
			if (delay == 0)
				delay = 1;
		}

		j = jiffies;

		//Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
		wait_event_interruptible_timeout(instance->wait_queue,
						 (instance->status !=
						  ao_status_single_run_wait),
						 (delay) ? delay : LONG_MAX);

		if (instance->status == ao_status_none) {
			PDEBUG("Single canceled.\n");
			err = ME_ERRNO_CANCELLED;
		}

		if (signal_pending(current)) {
			PERROR("Wait on start of state machine interrupted.\n");
			instance->status = ao_status_none;
			ao_stop_immediately(instance);
			err = ME_ERRNO_SIGNAL;
		}

		if ((delay) && ((jiffies - j) >= delay)) {
			PDEBUG("Timeout reached.\n");
			err = ME_ERRNO_TIMEOUT;
		}

		*value =
		    (!err) ? instance->single_value_in_fifo : instance->
		    single_value;
	} else {		//Non-blocking mode
		//Read value
		*value = instance->single_value;
	}

	ME_SUBDEVICE_EXIT;

	return err;
}

static int me6000_ao_io_single_write(me_subdevice_t *subdevice,
				     struct file *filep,
				     int channel,
				     int value, int time_out, int flags)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;
	unsigned long cpu_flags;
	unsigned long j;
	unsigned long delay = 0;

	uint32_t sync_mask;
	uint32_t mode;

	uint32_t tmp;

/// Workaround for mix-mode - begin
	uint32_t ctrl = 0x0;
	uint32_t status;
/// Workaround for mix-mode - end

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (flags &
	    ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS |
	      ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
		PERROR("Invalid flag specified.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if ((instance->status == ao_status_none)
	    || (instance->status > ao_status_single_end)) {
		PERROR("Subdevice not configured to work in single mode!\n");
		return ME_ERRNO_PREVIOUS_CONFIG;
	}

	if (channel != 0) {
		PERROR("Invalid channel number specified.\n");
		return ME_ERRNO_INVALID_CHANNEL;
	}

	if (value & ~ME6000_AO_MAX_DATA) {
		PERROR("Invalid value provided.\n");
		return ME_ERRNO_VALUE_OUT_OF_RANGE;
	}

	if (time_out < 0) {
		PERROR("Invalid timeout specified.\n");
		return ME_ERRNO_INVALID_TIMEOUT;
	}

	ME_SUBDEVICE_ENTER;

/// @note For single all calls (config and write) are erasing previous state!

	//Cancel control task
	PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
	instance->ao_control_task_flag = 0;
	cancel_delayed_work(&instance->ao_control_task);

	// Correct single mirrors
	instance->single_value_in_fifo = instance->single_value;

	//Stop device
	err = ao_stop_immediately(instance);
	if (err) {
		PERROR_CRITICAL("FSM IS BUSY!\n");
		ME_SUBDEVICE_EXIT;

		return ME_ERRNO_SUBDEVICE_BUSY;
	}

	if (time_out) {
		delay = (time_out * HZ) / 1000;

		if (delay == 0)
			delay = 1;
	}

	spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);

	instance->single_value_in_fifo = value;

	if (instance->fifo) {
		ctrl = inl(instance->ctrl_reg);
	}

	if (instance->fifo & ME6000_AO_HAS_FIFO) {	/// Workaround for mix-mode - begin
		//Set speed
		outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
		PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->timer_reg - instance->reg_base,
			   (int)ME6000_AO_MIN_CHAN_TICKS);
		instance->hardware_stop_delay = HZ / 10;	//100ms

		status = inl(instance->status_reg);

		//Set the continous mode.
		ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
		ctrl |= ME6000_AO_MODE_CONTINUOUS;

		//Prepare FIFO
		if (!(ctrl & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) {	//FIFO wasn't enabeled. Do it.
			PINFO("Enableing FIFO.\n");
			ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
			ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
		} else {	//Check if FIFO is empty
			if (status & ME6000_AO_STATUS_BIT_EF) {	//FIFO not empty
				PINFO("Reseting FIFO.\n");
				ctrl &=
				    ~(ME6000_AO_CTRL_BIT_ENABLE_FIFO |
				      ME6000_AO_CTRL_BIT_ENABLE_IRQ);
				outl(ctrl, instance->ctrl_reg);
				PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
					   instance->reg_base,
					   instance->ctrl_reg -
					   instance->reg_base, ctrl);

				ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
			} else {	//FIFO empty, only interrupt needs to be disabled!
				ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
			}
		}

		outl(ctrl, instance->ctrl_reg);
		PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->ctrl_reg - instance->reg_base, ctrl);

		//Reset interrupt latch
		inl(instance->irq_reset_reg);

		//Write output - 1 value to FIFO
		if (instance->ao_idx & 0x1) {
			outl(value <<= 16, instance->fifo_reg);
			PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->fifo_reg - instance->reg_base,
				   value <<= 16);
		} else {
			outl(value, instance->fifo_reg);
			PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->fifo_reg - instance->reg_base,
				   value);
		}
		/// Workaround for mix-mode - end
	} else {		//No FIFO - always in single mode
		//Write value
		PDEBUG("Write value\n");
		outl(value, instance->single_reg);
		PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->single_reg - instance->reg_base, value);
	}

	mode = *instance->preload_flags >> instance->ao_idx;
	mode &= (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG);

	PINFO("Triggering mode: 0x%08x\n", mode);

	spin_lock(instance->preload_reg_lock);
	sync_mask = inl(instance->preload_reg);
	PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->preload_reg - instance->reg_base, sync_mask);
	switch (mode) {
	case 0:		//0x00000000: Individual software
		ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;

		if (instance->fifo & ME6000_AO_HAS_FIFO) {	// FIFO - Continous mode
			ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
			if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != 0x0) {	//Now we can set correct mode.
				sync_mask &=
				    ~((ME6000_AO_SYNC_EXT_TRIG |
				       ME6000_AO_SYNC_HOLD) << instance->
				      ao_idx);

				outl(sync_mask, instance->preload_reg);
				PDEBUG_REG
				    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				     instance->reg_base,
				     instance->preload_reg - instance->reg_base,
				     sync_mask);
			}
		} else {	// No FIFO - Single mode: In this case resetting 'ME6000_AO_SYNC_HOLD' will trigger output.
			if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != ME6000_AO_SYNC_HOLD) {	//Now we can set correct mode. This is exception. It is set to synchronous and triggered later.
				sync_mask &=
				    ~(ME6000_AO_SYNC_EXT_TRIG << instance->
				      ao_idx);
				sync_mask |=
				    ME6000_AO_SYNC_HOLD << instance->ao_idx;

				outl(sync_mask, instance->preload_reg);
				PDEBUG_REG
				    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				     instance->reg_base,
				     instance->preload_reg - instance->reg_base,
				     sync_mask);
			}
		}
		instance->single_value = value;
		break;

	case ME6000_AO_SYNC_EXT_TRIG:	//0x00010000: Individual hardware
		PDEBUG("DIGITAL TRIGGER\n");
		ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;

		if (instance->fifo & ME6000_AO_HAS_FIFO) {	// FIFO - Continous mode
			if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != 0x0) {	//Now we can set correct mode.
				sync_mask &=
				    ~((ME6000_AO_SYNC_EXT_TRIG |
				       ME6000_AO_SYNC_HOLD) << instance->
				      ao_idx);

				outl(sync_mask, instance->preload_reg);
				PDEBUG_REG
				    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				     instance->reg_base,
				     instance->preload_reg - instance->reg_base,
				     sync_mask);
			}
		} else {	// No FIFO - Single mode
			if ((sync_mask &
			     ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
			      instance->ao_idx)) != ME6000_AO_SYNC_HOLD) {
				//Now we can set correct mode
				sync_mask &=
				    ~(ME6000_AO_SYNC_EXT_TRIG << instance->
				      ao_idx);
				sync_mask |=
				    ME6000_AO_SYNC_HOLD << instance->ao_idx;

				outl(sync_mask, instance->preload_reg);
				PDEBUG_REG
				    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				     instance->reg_base,
				     instance->preload_reg - instance->reg_base,
				     sync_mask);
			}
		}
		break;

	case ME6000_AO_SYNC_HOLD:	//0x00000001: Synchronous software
		ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;

		if ((sync_mask &
		     ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
		      instance->ao_idx)) !=
		    (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG)) {
			//Now we can set correct mode
			sync_mask |=
			    ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
			sync_mask |= ME6000_AO_SYNC_HOLD << instance->ao_idx;
			outl(sync_mask, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   sync_mask);
		}
		//Set triggering flag
		*instance->triggering_flags |= 0x1 << instance->ao_idx;
		break;

	case (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG):	//0x00010001: Synchronous hardware
		PDEBUG("DIGITAL TRIGGER\n");
		ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;

		if ((sync_mask &
		     ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
		      instance->ao_idx)) !=
		    (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG)) {
			//Now we can set correct mode
			sync_mask |=
			    (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
			    instance->ao_idx;
			outl(sync_mask, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   sync_mask);
		}
		//Set triggering flag
		*instance->triggering_flags |= 0x1 << instance->ao_idx;
		break;
	}
//              spin_unlock(instance->preload_reg_lock);        // Moved down.

	if (instance->fifo) {	//Activate ISM (remove 'stop' bits)
		ctrl &=
		    ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
		      ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
		ctrl |= instance->ctrl_trg;
		ctrl &=
		    ~(ME6000_AO_CTRL_BIT_STOP |
		      ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);

		outl(ctrl, instance->ctrl_reg);
		PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->ctrl_reg - instance->reg_base, ctrl);
	}
	spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);

/// @note When flag 'ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS' is set than output is triggered. ALWAYS!

	PINFO("<%s> start mode= 0x%08x %s\n", __func__, mode,
	      (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
	      "");
	if (instance->fifo & ME6000_AO_HAS_FIFO) {	// FIFO - Continous mode
		if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) {	//Trigger outputs
			//Add channel to start list
			outl(sync_mask |
			     (ME6000_AO_SYNC_HOLD << instance->ao_idx),
			     instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   sync_mask | (ME6000_AO_SYNC_HOLD <<
						instance->ao_idx));

			//Fire
			PINFO
			    ("Fired all software synchronous outputs by software trigger.\n");
			outl(0x8000, instance->single_reg);
			PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->single_reg - instance->reg_base,
				   0x8000);

			//Restore save settings
			outl(sync_mask, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   sync_mask);

		} else if (!mode) {	//Trigger outputs
/*			//Remove channel from start list
			outl(sync_mask & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, sync_mask & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx));
*/
			//Fire
			PINFO("Software trigger.\n");
			outl(0x8000, instance->single_reg);
			PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->single_reg - instance->reg_base,
				   0x8000);

/*			//Restore save settings
			outl(sync_mask, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, sync_mask);
*/
		}
/// @note This is mix-mode case. For now I do not have possibility to trigger first 4 channels (continous mode) and other (single) ones at once.
/// @note Because triggering is not working it can not be add to synchronous list. First 4 channels don't need this information, anyway.
		*instance->triggering_flags &= 0xFFFFFFF0;
	} else {		// No FIFO - Single mode
		if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) {	//Fired all software synchronous outputs.
			tmp = ~(*instance->preload_flags | 0xFFFF0000);
			PINFO
			    ("Fired all software synchronous outputs. mask:0x%08x\n",
			     tmp);
			tmp |= sync_mask & 0xFFFF0000;
			// Add this channel to list
			tmp &= ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);

			//Fire
			PINFO("Software trigger.\n");
			outl(tmp, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   tmp);

			//Restore save settings
			outl(sync_mask, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   sync_mask);

			//Set all as triggered.
			*instance->triggering_flags = 0x0;
		} else if (!mode) {	// Add this channel to list
			outl(sync_mask &
			     ~(ME6000_AO_SYNC_HOLD << instance->ao_idx),
			     instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   sync_mask & ~(ME6000_AO_SYNC_HOLD <<
						 instance->ao_idx));

			//Fire
			PINFO("Software trigger.\n");

			//Restore save settings
			outl(sync_mask, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   sync_mask);

			//Set all as triggered.
			*instance->triggering_flags = 0x0;
		}

	}
	spin_unlock(instance->preload_reg_lock);

	instance->status = ao_status_single_run_wait;

	instance->timeout.delay = delay;
	instance->timeout.start_time = jiffies;
	instance->ao_control_task_flag = 1;
	queue_delayed_work(instance->me6000_workqueue,
			   &instance->ao_control_task, 1);

	if (!(flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
		j = jiffies;

		//Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
		wait_event_interruptible_timeout(instance->wait_queue,
						 (instance->status !=
						  ao_status_single_run_wait),
						 (delay) ? delay +
						 1 : LONG_MAX);

		if (instance->status != ao_status_single_end) {
			PDEBUG("Single canceled.\n");
			err = ME_ERRNO_CANCELLED;
		}

		if (signal_pending(current)) {
			PERROR("Wait on start of state machine interrupted.\n");
			instance->ao_control_task_flag = 0;
			cancel_delayed_work(&instance->ao_control_task);
			ao_stop_immediately(instance);
			instance->status = ao_status_none;
			err = ME_ERRNO_SIGNAL;
		}

		if ((delay) && ((jiffies - j) >= delay)) {
			if (instance->status == ao_status_single_end) {
				PDEBUG("Timeout reached.\n");
			} else if ((jiffies - j) > delay) {
				PERROR
				    ("Timeout reached. Not handled by control task!\n");
				ao_stop_immediately(instance);
			} else {
				PERROR
				    ("Timeout reached. Signal come but status is strange: %d\n",
				     instance->status);
				ao_stop_immediately(instance);
			}

			instance->ao_control_task_flag = 0;
			cancel_delayed_work(&instance->ao_control_task);
			instance->status = ao_status_single_end;
			err = ME_ERRNO_TIMEOUT;
		}
	}

	ME_SUBDEVICE_EXIT;

	return err;
}

static int me6000_ao_io_stream_config(me_subdevice_t *subdevice,
				      struct file *filep,
				      meIOStreamConfig_t *config_list,
				      int count,
				      meIOStreamTrigger_t *trigger,
				      int fifo_irq_threshold, int flags)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;
	uint32_t ctrl;
	unsigned long cpu_flags;
	uint64_t conv_ticks;
	unsigned int conv_start_ticks_low = trigger->iConvStartTicksLow;
	unsigned int conv_start_ticks_high = trigger->iConvStartTicksHigh;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
		PERROR("Not a streaming ao.\n");
		return ME_ERRNO_NOT_SUPPORTED;
	}

	conv_ticks =
	    (uint64_t) conv_start_ticks_low +
	    ((uint64_t) conv_start_ticks_high << 32);

	if (flags &
	    ~(ME_IO_STREAM_CONFIG_HARDWARE_ONLY |
	      ME_IO_STREAM_CONFIG_WRAPAROUND)) {
		PERROR("Invalid flags.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) {
		if (!(flags & ME_IO_STREAM_CONFIG_WRAPAROUND)) {
			PERROR
			    ("Hardware ME_IO_STREAM_CONFIG_HARDWARE_ONLY has to be with ME_IO_STREAM_CONFIG_WRAPAROUND.\n");
			return ME_ERRNO_INVALID_FLAGS;
		}

		if ((trigger->iAcqStopTrigType != ME_TRIG_TYPE_NONE)
		    || (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE)) {
			PERROR
			    ("Hardware wraparound mode must be in infinite mode.\n");
			return ME_ERRNO_INVALID_FLAGS;
		}
	}

	if (count != 1) {
		PERROR("Only 1 entry in config list acceptable.\n");
		return ME_ERRNO_INVALID_CONFIG_LIST_COUNT;
	}

	if (config_list[0].iChannel != 0) {
		PERROR("Invalid channel number specified.\n");
		return ME_ERRNO_INVALID_CHANNEL;
	}

	if (config_list[0].iStreamConfig != 0) {
		PERROR("Only one range available.\n");
		return ME_ERRNO_INVALID_STREAM_CONFIG;
	}

	if (config_list[0].iRef != ME_REF_AO_GROUND) {
		PERROR("Output is referenced to ground.\n");
		return ME_ERRNO_INVALID_REF;
	}

	if ((trigger->iAcqStartTicksLow != 0)
	    || (trigger->iAcqStartTicksHigh != 0)) {
		PERROR
		    ("Invalid acquisition start trigger argument specified.\n");
		return ME_ERRNO_INVALID_ACQ_START_ARG;
	}

	if (config_list[0].iFlags) {
		PERROR("Invalid config list flag.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if ((trigger->iAcqStartTrigType != ME_TRIG_TYPE_SW)
	    && (trigger->iAcqStartTrigType != ME_TRIG_TYPE_EXT_DIGITAL)) {
		PERROR("Invalid acquisition start trigger type specified.\n");
		return ME_ERRNO_INVALID_ACQ_START_TRIG_TYPE;
	}

	if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) {
		switch (trigger->iAcqStartTrigEdge) {
		case ME_TRIG_EDGE_RISING:
		case ME_TRIG_EDGE_FALLING:
		case ME_TRIG_EDGE_ANY:
			break;

		default:
			PERROR
			    ("Invalid acquisition start trigger edge specified.\n");
			return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
		}
	}

	if ((trigger->iAcqStartTrigType == ME_TRIG_TYPE_SW)
	    && (trigger->iAcqStartTrigEdge != ME_TRIG_TYPE_NONE)) {
		PERROR("Invalid acquisition start trigger edge specified.\n");
		return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
	}

	if (trigger->iScanStartTrigType != ME_TRIG_TYPE_FOLLOW) {
		PERROR("Invalid scan start trigger type specified.\n");
		return ME_ERRNO_INVALID_SCAN_START_TRIG_TYPE;
	}

	if (trigger->iConvStartTrigType != ME_TRIG_TYPE_TIMER) {
		PERROR("Invalid conv start trigger type specified.\n");
		return ME_ERRNO_INVALID_CONV_START_TRIG_TYPE;
	}

	if ((conv_ticks < ME6000_AO_MIN_CHAN_TICKS)
	    || (conv_ticks > ME6000_AO_MAX_CHAN_TICKS)) {
		PERROR("Invalid conv start trigger argument specified.\n");
		return ME_ERRNO_INVALID_CONV_START_ARG;
	}

	if (trigger->iAcqStartTicksLow || trigger->iAcqStartTicksHigh) {
		PERROR("Invalid acq start trigger argument specified.\n");
		return ME_ERRNO_INVALID_ACQ_START_ARG;
	}

	if (trigger->iScanStartTicksLow || trigger->iScanStartTicksHigh) {
		PERROR("Invalid scan start trigger argument specified.\n");
		return ME_ERRNO_INVALID_SCAN_START_ARG;
	}

	switch (trigger->iScanStopTrigType) {
	case ME_TRIG_TYPE_NONE:
		if (trigger->iScanStopCount != 0) {
			PERROR("Invalid scan stop count specified.\n");
			return ME_ERRNO_INVALID_SCAN_STOP_ARG;
		}
		break;

	case ME_TRIG_TYPE_COUNT:
		if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
			if (trigger->iScanStopCount <= 0) {
				PERROR("Invalid scan stop count specified.\n");
				return ME_ERRNO_INVALID_SCAN_STOP_ARG;
			}
		} else {
			PERROR("The continous mode has not 'scan' contects.\n");
			return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
		}
		break;

	default:
		PERROR("Invalid scan stop trigger type specified.\n");
		return ME_ERRNO_INVALID_SCAN_STOP_TRIG_TYPE;
	}

	switch (trigger->iAcqStopTrigType) {
	case ME_TRIG_TYPE_NONE:
		if (trigger->iAcqStopCount != 0) {
			PERROR("Invalid acq stop count specified.\n");
			return ME_ERRNO_INVALID_ACQ_STOP_ARG;
		}
		break;

	case ME_TRIG_TYPE_COUNT:
		if (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE) {
			PERROR("Invalid acq stop trigger type specified.\n");
			return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
		}

		if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
			if (trigger->iAcqStopCount <= 0) {
				PERROR
				    ("The continous mode has not 'scan' contects.\n");
				return ME_ERRNO_INVALID_ACQ_STOP_ARG;
			}
		}
//                      else
//                      {
//                              PERROR("Invalid acq stop trigger type specified.\n");
//                              return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
//                      }

		break;

	default:
		PERROR("Invalid acq stop trigger type specified.\n");
		return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
	}

	switch (trigger->iAcqStartTrigChan) {
	case ME_TRIG_CHAN_DEFAULT:
	case ME_TRIG_CHAN_SYNCHRONOUS:
		break;

	default:
		PERROR("Invalid acq start trigger channel specified.\n");
		return ME_ERRNO_INVALID_ACQ_START_TRIG_CHAN;
	}

	ME_SUBDEVICE_ENTER;

	//Stop device

	//Cancel control task
	PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
	instance->ao_control_task_flag = 0;
	cancel_delayed_work(&instance->ao_control_task);

	//Check if state machine is stopped.
	err = ao_stop_immediately(instance);
	if (err) {
		PERROR_CRITICAL("FSM IS BUSY!\n");
		ME_SUBDEVICE_EXIT;

		return ME_ERRNO_SUBDEVICE_BUSY;
	}

	spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
	//Reset control register. Block all actions. Disable IRQ. Disable FIFO.
	ctrl = ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
	outl(ctrl, instance->ctrl_reg);
	PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->ctrl_reg - instance->reg_base, ctrl);

	//Reset interrupt latch
	inl(instance->irq_reset_reg);

	//This is paranoic, but to be sure.
	instance->preloaded_count = 0;
	instance->data_count = 0;
	instance->circ_buf.head = 0;
	instance->circ_buf.tail = 0;

	/* Set mode. */
	if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {	//Wraparound
		if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) {	//Hardware wraparound
			PINFO("Hardware wraparound.\n");
			ctrl |= ME6000_AO_MODE_WRAPAROUND;
			instance->mode = ME6000_AO_HW_WRAP_MODE;
		} else {	//Software wraparound
			PINFO("Software wraparound.\n");
			ctrl |= ME6000_AO_MODE_CONTINUOUS;
			instance->mode = ME6000_AO_SW_WRAP_MODE;
		}
	} else {		//Continous
		PINFO("Continous.\n");
		ctrl |= ME6000_AO_MODE_CONTINUOUS;
		instance->mode = ME6000_AO_CONTINOUS;
	}

	//Set the trigger edge.
	if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) {	//Set the trigger type and edge for external trigger.
		PINFO("External digital trigger.\n");
		instance->start_mode = ME6000_AO_EXT_TRIG;

		switch (trigger->iAcqStartTrigEdge) {
		case ME_TRIG_EDGE_RISING:
			PINFO("Set the trigger edge: rising.\n");
			instance->ctrl_trg = 0x0;
			break;

		case ME_TRIG_EDGE_FALLING:
			PINFO("Set the trigger edge: falling.\n");
//                                      ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
			instance->ctrl_trg = ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
			break;

		case ME_TRIG_EDGE_ANY:
			PINFO("Set the trigger edge: both edges.\n");
//                                      ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE | ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
			instance->ctrl_trg =
			    ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
			    ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
			break;
		}
	} else {
		PINFO("Internal software trigger.\n");
		instance->start_mode = 0;
	}

	//Set the stop mode and value.
	if (trigger->iAcqStopTrigType == ME_TRIG_TYPE_COUNT) {	//Amount of data
		instance->stop_mode = ME6000_AO_ACQ_STOP_MODE;
		instance->stop_count = trigger->iAcqStopCount;
	} else if (trigger->iScanStopTrigType == ME_TRIG_TYPE_COUNT) {	//Amount of 'scans'
		instance->stop_mode = ME6000_AO_SCAN_STOP_MODE;
		instance->stop_count = trigger->iScanStopCount;
	} else {		//Infinite
		instance->stop_mode = ME6000_AO_INF_STOP_MODE;
		instance->stop_count = 0;
	}

	PINFO("Stop count: %d.\n", instance->stop_count);

	if (trigger->iAcqStartTrigChan == ME_TRIG_CHAN_SYNCHRONOUS) {	//Synchronous start
		instance->start_mode |= ME6000_AO_SYNC_HOLD;
		if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) {	//Externaly triggered
			PINFO("Synchronous start. Externaly trigger active.\n");
			instance->start_mode |= ME6000_AO_SYNC_EXT_TRIG;
		}
#ifdef MEDEBUG_INFO
		else {
			PINFO
			    ("Synchronous start. Externaly trigger dissabled.\n");
		}
#endif

	}
	//Set speed
	outl(conv_ticks - 2, instance->timer_reg);
	PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%llx\n", instance->reg_base,
		   instance->timer_reg - instance->reg_base, conv_ticks - 2);
	instance->hardware_stop_delay = (int)(conv_ticks * HZ) / ME6000_AO_BASE_FREQUENCY;	//<== MUST be with cast!

	// Write the control word
	outl(ctrl, instance->ctrl_reg);
	PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->ctrl_reg - instance->reg_base, ctrl);

	//Set status.
	instance->status = ao_status_stream_configured;
	spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);

	ME_SUBDEVICE_EXIT;

	return err;
}

static int me6000_ao_io_stream_new_values(me_subdevice_t *subdevice,
					  struct file *filep,
					  int time_out, int *count, int flags)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;
	long t = 0;
	long j;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
		PERROR("Not a streaming ao.\n");
		return ME_ERRNO_NOT_SUPPORTED;
	}

	if (flags) {
		PERROR("Invalid flag specified.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if (!instance->circ_buf.buf) {
		PERROR("Circular buffer not exists.\n");
		return ME_ERRNO_INTERNAL;
	}

	if (time_out < 0) {
		PERROR("Invalid time_out specified.\n");
		return ME_ERRNO_INVALID_TIMEOUT;
	}

	ME_SUBDEVICE_ENTER;

	if (me_circ_buf_space(&instance->circ_buf)) {	//The buffer is NOT full.
		*count = me_circ_buf_space(&instance->circ_buf);
	} else {		//The buffer is full.
		if (time_out) {
			t = (time_out * HZ) / 1000;

			if (t == 0)
				t = 1;
		} else {	//Max time.
			t = LONG_MAX;
		}

		*count = 0;

		j = jiffies;

		//Only runing process will interrupt this call. Interrupts are when FIFO HF is signaled.
		wait_event_interruptible_timeout(instance->wait_queue,
						 ((me_circ_buf_space
						   (&instance->circ_buf))
						  || !(inl(instance->status_reg)
						       &
						       ME6000_AO_STATUS_BIT_FSM)),
						 t);

		if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) {
			PERROR("AO subdevice is not running.\n");
			err = ME_ERRNO_SUBDEVICE_NOT_RUNNING;
		} else if (signal_pending(current)) {
			PERROR("Wait on values interrupted from signal.\n");
			instance->status = ao_status_none;
			ao_stop_immediately(instance);
			err = ME_ERRNO_SIGNAL;
		} else if ((jiffies - j) >= t) {
			PERROR("Wait on values timed out.\n");
			err = ME_ERRNO_TIMEOUT;
		} else {	//Uff... all is good. Inform user about empty space.
			*count = me_circ_buf_space(&instance->circ_buf);
		}
	}

	ME_SUBDEVICE_EXIT;

	return err;
}

static int me6000_ao_io_stream_start(me_subdevice_t *subdevice,
				     struct file *filep,
				     int start_mode, int time_out, int flags)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;
	unsigned long cpu_flags = 0;
	uint32_t status;
	uint32_t ctrl;
	uint32_t synch;
	int count = 0;
	int circ_buffer_count;

	unsigned long ref;
	unsigned long delay = 0;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
		PERROR("Not a streaming ao.\n");
		return ME_ERRNO_NOT_SUPPORTED;
	}

	if (flags & ~ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS) {
		PERROR("Invalid flags.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if (time_out < 0) {
		PERROR("Invalid timeout specified.\n");
		return ME_ERRNO_INVALID_TIMEOUT;
	}

	if ((start_mode != ME_START_MODE_BLOCKING)
	    && (start_mode != ME_START_MODE_NONBLOCKING)) {
		PERROR("Invalid start mode specified.\n");
		return ME_ERRNO_INVALID_START_MODE;
	}

	if (time_out) {
		delay = (time_out * HZ) / 1000;
		if (delay == 0)
			delay = 1;
	}

	switch (instance->status) {	//Checking actual mode.
	case ao_status_stream_configured:
	case ao_status_stream_end:
		//Correct modes!
		break;

		//The device is in wrong mode.
	case ao_status_none:
	case ao_status_single_configured:
	case ao_status_single_run_wait:
	case ao_status_single_run:
	case ao_status_single_end_wait:
		PERROR
		    ("Subdevice must be preinitialize correctly for streaming.\n");
		return ME_ERRNO_PREVIOUS_CONFIG;

	case ao_status_stream_fifo_error:
	case ao_status_stream_buffer_error:
	case ao_status_stream_error:
		PDEBUG("Before restart broke stream 'STOP' must be caled.\n");
		return ME_STATUS_ERROR;

	case ao_status_stream_run_wait:
	case ao_status_stream_run:
	case ao_status_stream_end_wait:
		PDEBUG("Stream is already working.\n");
		return ME_ERRNO_SUBDEVICE_BUSY;

	default:
		instance->status = ao_status_stream_error;
		PERROR_CRITICAL("Status is in wrong state!\n");
		return ME_ERRNO_INTERNAL;

	}

	ME_SUBDEVICE_ENTER;

	if (instance->mode == ME6000_AO_CONTINOUS) {	//Continous
		instance->circ_buf.tail += instance->preloaded_count;
		instance->circ_buf.tail &= instance->circ_buf.mask;
	}
	circ_buffer_count = me_circ_buf_values(&instance->circ_buf);

	if (!circ_buffer_count && !instance->preloaded_count) {	//No values in buffer
		ME_SUBDEVICE_EXIT;
		PERROR("No values in buffer!\n");
		return ME_ERRNO_LACK_OF_RESOURCES;
	}

	//Cancel control task
	PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
	instance->ao_control_task_flag = 0;
	cancel_delayed_work(&instance->ao_control_task);

	//Stop device
	err = ao_stop_immediately(instance);
	if (err) {
		PERROR_CRITICAL("FSM IS BUSY!\n");
		ME_SUBDEVICE_EXIT;

		return ME_ERRNO_SUBDEVICE_BUSY;
	}
	//Set values for single_read()
	instance->single_value = ME6000_AO_MAX_DATA + 1;
	instance->single_value_in_fifo = ME6000_AO_MAX_DATA + 1;

	//Setting stop points
	if (instance->stop_mode == ME6000_AO_SCAN_STOP_MODE) {
		instance->stop_data_count =
		    instance->stop_count * circ_buffer_count;
	} else {
		instance->stop_data_count = instance->stop_count;
	}

	if ((instance->stop_data_count != 0)
	    && (instance->stop_data_count < circ_buffer_count)) {
		PERROR("More data in buffer than previously set limit!\n");
	}

	spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
	ctrl = inl(instance->ctrl_reg);
	//Check FIFO
	if (!(ctrl & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) {	//FIFO wasn't enabeled. Do it. <= This should be done by user call with ME_WRITE_MODE_PRELOAD
		PINFO("Enableing FIFO.\n");
		ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
		ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;

		instance->preloaded_count = 0;
		instance->data_count = 0;
	} else {		//Block IRQ
		ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
	}
	outl(ctrl, instance->ctrl_reg);
	PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->ctrl_reg - instance->reg_base, ctrl);

	//Reset interrupt latch
	inl(instance->irq_reset_reg);

	//Fill FIFO <= Generaly this should be done by user pre-load call but this is second place to do it.
	status = inl(instance->status_reg);
	if (!(status & ME6000_AO_STATUS_BIT_EF)) {	//FIFO empty
		if (instance->stop_data_count != 0) {
			count = ME6000_AO_FIFO_COUNT;
		} else {
			count =
			    (ME6000_AO_FIFO_COUNT <
			     instance->
			     stop_data_count) ? ME6000_AO_FIFO_COUNT :
			    instance->stop_data_count;
		}

		//Copy data
		count =
		    ao_write_data(instance, count, instance->preloaded_count);

		if (count < 0) {	//This should never happend!
			PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);
			ME_SUBDEVICE_EXIT;
			return ME_ERRNO_INTERNAL;
		}
	}
	//Set pre-load features.
	spin_lock(instance->preload_reg_lock);
	synch = inl(instance->preload_reg);
	synch &=
	    ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
	      ao_idx);
	synch |=
	    (instance->start_mode & ~ME6000_AO_EXT_TRIG) << instance->ao_idx;
	outl(synch, instance->preload_reg);
	PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->preload_reg - instance->reg_base, synch);
	spin_unlock(instance->preload_reg_lock);

	//Default count is '0'
	if (instance->mode == ME6000_AO_CONTINOUS) {	//Continous
		instance->preloaded_count = 0;
		instance->circ_buf.tail += count;
		instance->circ_buf.tail &= instance->circ_buf.mask;
	} else {		//Wraparound
		instance->preloaded_count += count;
		instance->data_count += count;

		//Special case: Infinite wraparound with less than FIFO datas always should runs in hardware mode.
		if ((instance->stop_mode == ME6000_AO_INF_STOP_MODE)
		    && (circ_buffer_count <= ME6000_AO_FIFO_COUNT)) {	//Change to hardware wraparound
			PDEBUG
			    ("Changeing mode from software wraparound to hardware wraparound.\n");
			//Copy all data
			count =
			    ao_write_data(instance, circ_buffer_count,
					  instance->preloaded_count);
			ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
			ctrl |= ME6000_AO_MODE_WRAPAROUND;
		}

		if (instance->preloaded_count == me_circ_buf_values(&instance->circ_buf)) {	//Reset position indicator.
			instance->preloaded_count = 0;
		} else if (instance->preloaded_count > me_circ_buf_values(&instance->circ_buf)) {	//This should never happend!
			PERROR_CRITICAL
			    ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);
			ME_SUBDEVICE_EXIT;
			return ME_ERRNO_INTERNAL;
		}
	}

	//Set status to 'wait for start'
	instance->status = ao_status_stream_run_wait;

	status = inl(instance->status_reg);
	//Start state machine and interrupts
	PINFO("<%s:%d> Start state machine.\n", __func__, __LINE__);
	ctrl &= ~(ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
	if (instance->start_mode == ME6000_AO_EXT_TRIG) {
		PDEBUG("DIGITAL TRIGGER\n");
		ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
	}
	if (!(status & ME6000_AO_STATUS_BIT_HF)) {	//More than half!
		if ((ctrl & ME6000_AO_CTRL_MODE_MASK) == ME6000_AO_MODE_CONTINUOUS) {	//Enable IRQ only when hardware_continous is set and FIFO is more than half
			PINFO("<%s:%d> Start interrupts.\n", __func__,
			      __LINE__);
			ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
		}
	}
	outl(ctrl, instance->ctrl_reg);
	PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->ctrl_reg - instance->reg_base, ctrl);
	spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);

	//Trigger output
	PINFO("<%s> start mode= 0x%x %s\n", __func__, instance->start_mode,
	      (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
	      "");
	if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) {	//Trigger outputs
		spin_lock(instance->preload_reg_lock);
		synch = inl(instance->preload_reg);
		//Add channel to start list
		outl(synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx),
		     instance->preload_reg);
		PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->preload_reg - instance->reg_base,
			   synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx));

		//Fire
		PINFO
		    ("Fired all software synchronous outputs by software trigger.\n");
		outl(0x8000, instance->single_reg);
		PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->single_reg - instance->reg_base, 0x8000);

		//Restore save settings
		outl(synch, instance->preload_reg);
		PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->preload_reg - instance->reg_base, synch);
		spin_unlock(instance->preload_reg_lock);
	} else if (!instance->start_mode) {	//Trigger outputs
/*
		spin_lock(instance->preload_reg_lock);
			synch = inl(instance->preload_reg);
			//Remove channel from start list
			outl(synch & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx));
*/
		//Fire
		PINFO("Software trigger.\n");
		outl(0x8000, instance->single_reg);
		PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->single_reg - instance->reg_base, 0x8000);

/*
			//Restore save settings
			outl(synch, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch);
		spin_unlock(instance->preload_reg_lock);
*/
	}
	// Set control task's timeout
	instance->timeout.delay = delay;
	instance->timeout.start_time = jiffies;

	if (status & ME6000_AO_STATUS_BIT_HF) {	//Less than half but not empty!
		PINFO("Less than half.\n");
		if (instance->stop_data_count == 0) {
			count = ME6000_AO_FIFO_COUNT / 2;
		} else {
			count =
			    ((ME6000_AO_FIFO_COUNT / 2) <
			     instance->stop_data_count) ? ME6000_AO_FIFO_COUNT /
			    2 : instance->stop_data_count;
		}

		//Copy data
		count =
		    ao_write_data(instance, count, instance->preloaded_count);

		if (count < 0) {	//This should never happend!
			PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
			ME_SUBDEVICE_EXIT;
			return ME_ERRNO_INTERNAL;
		}

		if (instance->mode == ME6000_AO_CONTINOUS) {	//Continous
			instance->circ_buf.tail += count;
			instance->circ_buf.tail &= instance->circ_buf.mask;
		} else {	//Wraparound
			instance->data_count += count;
			instance->preloaded_count += count;

			if (instance->preloaded_count == me_circ_buf_values(&instance->circ_buf)) {	//Reset position indicator.
				instance->preloaded_count = 0;
			} else if (instance->preloaded_count > me_circ_buf_values(&instance->circ_buf)) {	//This should never happend!
				PERROR_CRITICAL
				    ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
				ME_SUBDEVICE_EXIT;
				return ME_ERRNO_INTERNAL;
			}
		}

		status = inl(instance->status_reg);
		if (!(status & ME6000_AO_STATUS_BIT_HF)) {	//More than half!
			spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
			PINFO("<%s:%d> Start interrupts.\n", __func__,
			      __LINE__);
			ctrl = inl(instance->ctrl_reg);
			ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
			outl(ctrl, instance->ctrl_reg);
			PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->ctrl_reg - instance->reg_base,
				   ctrl);
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);
		}
	}
	//Special case: Limited wraparound with less than HALF FIFO datas need work around to generate first interrupt.
	if ((instance->stop_mode != ME6000_AO_INF_STOP_MODE)
	    && (instance->mode == ME6000_AO_SW_WRAP_MODE)
	    && (circ_buffer_count <= (ME6000_AO_FIFO_COUNT / 2))) {	//Put more data to FIFO
		PINFO("Limited wraparound with less than HALF FIFO datas.\n");
		if (instance->preloaded_count) {	//This should never happend!
			PERROR_CRITICAL
			    ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
			ME_SUBDEVICE_EXIT;
			return ME_ERRNO_INTERNAL;
		}

		while (instance->stop_data_count > instance->data_count) {	//Maximum data not set jet.
			//Copy to buffer
			if (circ_buffer_count != ao_write_data(instance, circ_buffer_count, 0)) {	//This should never happend!
				PERROR_CRITICAL
				    ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
				ME_SUBDEVICE_EXIT;
				return ME_ERRNO_INTERNAL;
			}
			instance->data_count += circ_buffer_count;

			if (!((status = inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF)) {	//FIFO is more than half. Enable IRQ and end copy.
				//Reset interrupt latch
				inl(instance->irq_reset_reg);

				spin_lock_irqsave(&instance->subdevice_lock,
						  cpu_flags);
				PINFO("<%s:%d> Start interrupts.\n",
				      __func__, __LINE__);
				ctrl = inl(instance->ctrl_reg);
				ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
				outl(ctrl, instance->ctrl_reg);
				PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
					   instance->reg_base,
					   instance->ctrl_reg -
					   instance->reg_base, ctrl);
				spin_unlock_irqrestore(&instance->
						       subdevice_lock,
						       cpu_flags);
				break;
			}
		}
	}
	// Schedule control task
	instance->ao_control_task_flag = 1;
	queue_delayed_work(instance->me6000_workqueue,
			   &instance->ao_control_task, 1);

	if (start_mode == ME_START_MODE_BLOCKING) {	//Wait for start.
		ref = jiffies;
		//Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
		wait_event_interruptible_timeout(instance->wait_queue,
						 (instance->status !=
						  ao_status_stream_run_wait),
						 (delay) ? delay +
						 1 : LONG_MAX);

		if ((instance->status != ao_status_stream_run)
		    && (instance->status != ao_status_stream_end)) {
			PDEBUG("Starting stream canceled. %d\n",
			       instance->status);
			err = ME_ERRNO_CANCELLED;
		}

		if (signal_pending(current)) {
			PERROR("Wait on start of state machine interrupted.\n");
			instance->status = ao_status_none;
			ao_stop_immediately(instance);
			err = ME_ERRNO_SIGNAL;
		}

		if ((delay) && ((jiffies - ref) >= delay)) {
			if (instance->status != ao_status_stream_run) {
				if (instance->status == ao_status_stream_end) {
					PDEBUG("Timeout reached.\n");
				} else if ((jiffies - ref) > delay) {
					PERROR
					    ("Timeout reached. Not handled by control task!\n");
					ao_stop_immediately(instance);
				} else {
					PERROR
					    ("Timeout reached. Signal come but status is strange: %d\n",
					     instance->status);
					ao_stop_immediately(instance);
				}

				instance->ao_control_task_flag = 0;
				cancel_delayed_work(&instance->ao_control_task);
				instance->status = ao_status_stream_end;
				err = ME_ERRNO_TIMEOUT;
			}
		}
	}

	ME_SUBDEVICE_EXIT;
	return err;
}

static int me6000_ao_io_stream_status(me_subdevice_t *subdevice,
				      struct file *filep,
				      int wait,
				      int *status, int *values, int flags)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
		PERROR("Not a streaming ao.\n");
		return ME_ERRNO_NOT_SUPPORTED;
	}

	if (flags) {
		PERROR("Invalid flag specified.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if ((wait != ME_WAIT_NONE) && (wait != ME_WAIT_IDLE)) {
		PERROR("Invalid wait argument specified.\n");
		*status = ME_STATUS_INVALID;
		return ME_ERRNO_INVALID_WAIT;
	}

	ME_SUBDEVICE_ENTER;

	switch (instance->status) {
	case ao_status_single_configured:
	case ao_status_single_end:
	case ao_status_stream_configured:
	case ao_status_stream_end:
	case ao_status_stream_fifo_error:
	case ao_status_stream_buffer_error:
	case ao_status_stream_error:
		*status = ME_STATUS_IDLE;
		break;

	case ao_status_single_run_wait:
	case ao_status_single_run:
	case ao_status_single_end_wait:
	case ao_status_stream_run_wait:
	case ao_status_stream_run:
	case ao_status_stream_end_wait:
		*status = ME_STATUS_BUSY;
		break;

	case ao_status_none:
	default:
		*status =
		    (inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM) ?
		    ME_STATUS_BUSY : ME_STATUS_IDLE;
		break;
	}

	if ((wait == ME_WAIT_IDLE) && (*status == ME_STATUS_BUSY)) {
		//Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
		wait_event_interruptible_timeout(instance->wait_queue,
						 ((instance->status !=
						   ao_status_single_run_wait)
						  && (instance->status !=
						      ao_status_single_run)
						  && (instance->status !=
						      ao_status_single_end_wait)
						  && (instance->status !=
						      ao_status_stream_run_wait)
						  && (instance->status !=
						      ao_status_stream_run)
						  && (instance->status !=
						      ao_status_stream_end_wait)),
						 LONG_MAX);

		if (instance->status != ao_status_stream_end) {
			PDEBUG("Wait for IDLE canceled. %d\n",
			       instance->status);
			err = ME_ERRNO_CANCELLED;
		}

		if (signal_pending(current)) {
			PERROR("Wait for IDLE interrupted.\n");
			instance->status = ao_status_none;
			ao_stop_immediately(instance);
			err = ME_ERRNO_SIGNAL;
		}

		*status = ME_STATUS_IDLE;
	}

	*values = me_circ_buf_space(&instance->circ_buf);

	ME_SUBDEVICE_EXIT;

	return err;
}

static int me6000_ao_io_stream_stop(me_subdevice_t *subdevice,
				    struct file *filep,
				    int stop_mode, int flags)
{				/// @note Stop work and empty buffer and FIFO
	int err = ME_ERRNO_SUCCESS;
	me6000_ao_subdevice_t *instance;
	unsigned long cpu_flags;
	volatile uint32_t ctrl;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (flags & ~ME_IO_STREAM_STOP_PRESERVE_BUFFERS) {
		PERROR("Invalid flag specified.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if ((stop_mode != ME_STOP_MODE_IMMEDIATE)
	    && (stop_mode != ME_STOP_MODE_LAST_VALUE)) {
		PERROR("Invalid stop mode specified.\n");
		return ME_ERRNO_INVALID_STOP_MODE;
	}

	if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
		PERROR("Not a streaming ao.\n");
		return ME_ERRNO_NOT_SUPPORTED;
	}

	if (instance->status < ao_status_stream_configured) {
		//There is nothing to stop!
		PERROR("Subdevice not in streaming mode. %d\n",
		       instance->status);
		return ME_ERRNO_PREVIOUS_CONFIG;
	}

	ME_SUBDEVICE_ENTER;

	//Mark as stopping. => Software stop.
	instance->status = ao_status_stream_end_wait;

	if (stop_mode == ME_STOP_MODE_IMMEDIATE) {	//Stopped now!
		err = ao_stop_immediately(instance);
	} else if (stop_mode == ME_STOP_MODE_LAST_VALUE) {
		ctrl = inl(instance->ctrl_reg) & ME6000_AO_CTRL_MODE_MASK;
		if (ctrl == ME6000_AO_MODE_WRAPAROUND) {	//Hardware wraparound => Hardware stop.
			spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
			ctrl = inl(instance->ctrl_reg);
			ctrl |= ME6000_AO_CTRL_BIT_STOP;
			ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
			outl(ctrl, instance->ctrl_reg);
			PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->ctrl_reg - instance->reg_base,
				   ctrl);
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);

			//Reset interrupt latch
			inl(instance->irq_reset_reg);
		}
		//Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
		wait_event_interruptible_timeout(instance->wait_queue,
						 (instance->status !=
						  ao_status_stream_end_wait),
						 LONG_MAX);

		if (instance->status != ao_status_stream_end) {
			PDEBUG("Stopping stream canceled.\n");
			err = ME_ERRNO_CANCELLED;
		}

		if (signal_pending(current)) {
			PERROR("Stopping stream interrupted.\n");
			instance->status = ao_status_none;
			ao_stop_immediately(instance);
			err = ME_ERRNO_SIGNAL;
		}
	}

	spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
	ctrl = inl(instance->ctrl_reg);
	ctrl |= ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
	ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
	if (!flags) {		//Reset FIFO
		ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
	}
	outl(ctrl, instance->ctrl_reg);
	PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->ctrl_reg - instance->reg_base, ctrl);
	spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);

	//Reset interrupt latch
	inl(instance->irq_reset_reg);

	if (!flags) {		//Reset software buffer
		instance->circ_buf.head = 0;
		instance->circ_buf.tail = 0;
		instance->preloaded_count = 0;
		instance->data_count = 0;
	}

	ME_SUBDEVICE_EXIT;

	return err;
}

static int me6000_ao_io_stream_write(me_subdevice_t *subdevice,
				     struct file *filep,
				     int write_mode,
				     int *values, int *count, int flags)
{
	int err = ME_ERRNO_SUCCESS;
	me6000_ao_subdevice_t *instance;
	unsigned long cpu_flags = 0;
	uint32_t reg_copy;

	int copied_from_user = 0;
	int left_to_copy_from_user = *count;

	int copied_values;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	//Checking arguments
	if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
		PERROR("Not a streaming ao.\n");
		return ME_ERRNO_NOT_SUPPORTED;
	}

	if (flags) {
		PERROR("Invalid flag specified.\n");
		return ME_ERRNO_INVALID_FLAGS;
	}

	if (*count <= 0) {
		PERROR("Invalid count of values specified.\n");
		return ME_ERRNO_INVALID_VALUE_COUNT;
	}

	if (values == NULL) {
		PERROR("Invalid address of values specified.\n");
		return ME_ERRNO_INVALID_POINTER;
	}

	if ((instance->status == ao_status_none) || (instance->status == ao_status_single_configured)) {	//The device is in single mode.
		PERROR
		    ("Subdevice must be preinitialize correctly for streaming.\n");
		return ME_ERRNO_PREVIOUS_CONFIG;
	}

	switch (write_mode) {
	case ME_WRITE_MODE_PRELOAD:

		//Device must be stopped.
		if ((instance->status != ao_status_stream_configured)
		    && (instance->status != ao_status_stream_end)) {
			PERROR
			    ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
			return ME_ERRNO_PREVIOUS_CONFIG;
		}
		break;
	case ME_WRITE_MODE_NONBLOCKING:
	case ME_WRITE_MODE_BLOCKING:
		/// @note In blocking mode: When device is not runing and there is not enought space call will blocked up!
		/// @note Some other thread must empty buffer by strating engine.
		break;

	default:
		PERROR("Invalid write mode specified.\n");
		return ME_ERRNO_INVALID_WRITE_MODE;
	}

	if (instance->mode & ME6000_AO_WRAP_MODE) {	//Wraparound mode. Device must be stopped.
		if ((instance->status != ao_status_stream_configured)
		    && (instance->status != ao_status_stream_end)) {
			PERROR
			    ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
			return ME_ERRNO_INVALID_WRITE_MODE;
		}
	}

	if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
	    && (write_mode != ME_WRITE_MODE_PRELOAD)) {
/*
		PERROR("Only 'pre-load' write is acceptable in hardware wraparound mode.\n");
		return ME_ERRNO_PREVIOUS_CONFIG;
*/
		//This is transparent for user.
		PDEBUG("Changing write_mode to ME_WRITE_MODE_PRELOAD.\n");
		write_mode = ME_WRITE_MODE_PRELOAD;
	}

	ME_SUBDEVICE_ENTER;

	if (write_mode == ME_WRITE_MODE_PRELOAD) {	//Init enviroment - preload
		spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
		reg_copy = inl(instance->ctrl_reg);
		//Check FIFO
		if (!(reg_copy & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) {	//FIFO not active. Enable it.
			reg_copy |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
			outl(reg_copy, instance->ctrl_reg);
			PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->ctrl_reg - instance->reg_base,
				   reg_copy);
			instance->preloaded_count = 0;
		}
		spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
	}

	while (1) {
		//Copy to buffer. This step is common for all modes.
		copied_from_user =
		    ao_get_data_from_user(instance, left_to_copy_from_user,
					  values + (*count -
						    left_to_copy_from_user));
		left_to_copy_from_user -= copied_from_user;

		reg_copy = inl(instance->status_reg);
		if ((instance->status == ao_status_stream_run) && !(reg_copy & ME6000_AO_STATUS_BIT_FSM)) {	//BROKEN PIPE! The state machine is stoped but logical status show that should be working.
			PERROR("Broken pipe in write.\n");
			err = ME_ERRNO_SUBDEVICE_NOT_RUNNING;
			break;
		}

		if ((instance->status == ao_status_stream_run) && (instance->mode == ME6000_AO_CONTINOUS) && (reg_copy & ME6000_AO_STATUS_BIT_HF)) {	//Continous mode runing and data are below half!

			// Block interrupts.
			spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
			reg_copy = inl(instance->ctrl_reg);
			reg_copy &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
			outl(reg_copy, instance->ctrl_reg);
			PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->ctrl_reg - instance->reg_base,
				   reg_copy);
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);

			//Fast copy
			copied_values =
			    ao_write_data(instance, ME6000_AO_FIFO_COUNT / 2,
					  0);
			if (copied_values > 0) {
				instance->circ_buf.tail += copied_values;
				instance->circ_buf.tail &=
				    instance->circ_buf.mask;
				continue;
			}
			//Reset interrupt latch
			inl(instance->irq_reset_reg);

			// Activate interrupts.
			spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
			reg_copy = inl(instance->ctrl_reg);
			reg_copy |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
			outl(reg_copy, instance->ctrl_reg);
			PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->ctrl_reg - instance->reg_base,
				   reg_copy);
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);

			if (copied_values == 0) {	//This was checked and never should happend!
				PERROR_CRITICAL("COPY FINISH WITH 0!\n");
			}

			if (copied_values < 0) {	//This was checked and never should happend!
				PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
				instance->status = ao_status_stream_fifo_error;
				err = ME_ERRNO_FIFO_BUFFER_OVERFLOW;
				break;
			}
		}

		if (!left_to_copy_from_user) {	//All datas were copied.
			break;
		} else {	//Not all datas were copied.
			if (instance->mode & ME6000_AO_WRAP_MODE) {	//Error too much datas! Wraparound is limited in size!
				PERROR
				    ("Too much data for wraparound mode!  Exceeded size of %ld.\n",
				     ME6000_AO_CIRC_BUF_COUNT - 1);
				err = ME_ERRNO_RING_BUFFER_OVERFLOW;
				break;
			}

			if (write_mode != ME_WRITE_MODE_BLOCKING) {	//Non blocking calls
				break;
			}

			wait_event_interruptible(instance->wait_queue,
						 me_circ_buf_space(&instance->
								   circ_buf));

			if (signal_pending(current)) {
				PERROR("Writing interrupted by signal.\n");
				instance->status = ao_status_none;
				ao_stop_immediately(instance);
				err = ME_ERRNO_SIGNAL;
				break;
			}

			if (instance->status == ao_status_none) {	//Reset
				PERROR("Writing interrupted by reset.\n");
				err = ME_ERRNO_CANCELLED;
				break;
			}
		}
	}

	if (write_mode == ME_WRITE_MODE_PRELOAD) {	//Copy data to FIFO - preload
		copied_values =
		    ao_write_data_pooling(instance, ME6000_AO_FIFO_COUNT,
					  instance->preloaded_count);
		instance->preloaded_count += copied_values;
		instance->data_count += copied_values;

		if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
		    && (me_circ_buf_values(&instance->circ_buf) >
			ME6000_AO_FIFO_COUNT)) {
			PERROR
			    ("Too much data for hardware wraparound mode! Exceeded size of %d.\n",
			     ME6000_AO_FIFO_COUNT);
			err = ME_ERRNO_FIFO_BUFFER_OVERFLOW;
		}
	}

	*count = *count - left_to_copy_from_user;
	ME_SUBDEVICE_EXIT;

	return err;
}

static irqreturn_t me6000_ao_isr(int irq, void *dev_id)
{
	me6000_ao_subdevice_t *instance = dev_id;
	uint32_t irq_status;
	uint32_t ctrl;
	uint32_t status;
	int count = 0;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (irq != instance->irq) {
		PERROR("Incorrect interrupt num: %d.\n", irq);
		return IRQ_NONE;
	}

	irq_status = inl(instance->irq_status_reg);
	if (!(irq_status & (ME6000_IRQ_STATUS_BIT_AO_HF << instance->ao_idx))) {
		PINFO("%ld Shared interrupt. %s(): ID=%d: status_reg=0x%04X\n",
		      jiffies, __func__, instance->ao_idx, irq_status);
		return IRQ_NONE;
	}

	if (!instance->circ_buf.buf) {
		instance->status = ao_status_stream_error;
		PERROR_CRITICAL("CIRCULAR BUFFER NOT EXISTS!\n");
		//Block interrupts. Stop machine.
		ctrl = inl(instance->ctrl_reg);
		ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
		ctrl |=
		    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
		outl(ctrl, instance->ctrl_reg);
		PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->ctrl_reg - instance->reg_base, ctrl);

		//Inform user
		wake_up_interruptible_all(&instance->wait_queue);
		return IRQ_HANDLED;
	}

	status = inl(instance->status_reg);
	if (!(status & ME6000_AO_STATUS_BIT_FSM)) {	//Too late. Not working! END? BROKEN PIPE?
		/// @note Error checking was moved to separate task.
		PDEBUG("Interrupt come but ISM is not working!\n");
		//Block interrupts. Stop machine.
		ctrl = inl(instance->ctrl_reg);
		ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
		ctrl |=
		    ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
		outl(ctrl, instance->ctrl_reg);
		PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->ctrl_reg - instance->reg_base, ctrl);

		//Reset interrupt latch
		inl(instance->irq_reset_reg);

		/// @note User notification was also moved to separate task.
		return IRQ_HANDLED;
	}
	//General procedure. Process more datas.

#ifdef MEDEBUG_DEBUG
	if (!me_circ_buf_values(&instance->circ_buf)) {	//Buffer is empty!
		PDEBUG("Circular buffer empty!\n");
	}
#endif

	//Check FIFO
	if (status & ME6000_AO_STATUS_BIT_HF) {	//OK less than half

		//Block interrupts
		ctrl = inl(instance->ctrl_reg);
		ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
		outl(ctrl, instance->ctrl_reg);
		PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->ctrl_reg - instance->reg_base, ctrl);

		do {
			//Calculate how many should be copied.
			count =
			    (instance->stop_data_count) ? instance->
			    stop_data_count -
			    instance->data_count : ME6000_AO_FIFO_COUNT / 2;
			if (ME6000_AO_FIFO_COUNT / 2 < count) {
				count = ME6000_AO_FIFO_COUNT / 2;
			}
			//Copy data
			if (instance->mode == ME6000_AO_CONTINOUS) {	//Continous
				count = ao_write_data(instance, count, 0);
				if (count > 0) {
					instance->circ_buf.tail += count;
					instance->circ_buf.tail &=
					    instance->circ_buf.mask;
					instance->data_count += count;

					if ((instance->status == ao_status_stream_end_wait) && !me_circ_buf_values(&instance->circ_buf)) {	//Stoping. Whole buffer was copied.
						break;
					}
				}
			} else if ((instance->mode == ME6000_AO_SW_WRAP_MODE) && ((ctrl & ME6000_AO_CTRL_MODE_MASK) == ME6000_AO_MODE_CONTINUOUS)) {	//Wraparound (software)
				if (instance->status == ao_status_stream_end_wait) {	//We stoping => Copy to the end of the buffer.
					count =
					    ao_write_data(instance, count, 0);
				} else {	//Copy in wraparound mode.
					count =
					    ao_write_data_wraparound(instance,
								     count,
								     instance->
								     preloaded_count);
				}

				if (count > 0) {
					instance->data_count += count;
					instance->preloaded_count += count;
					instance->preloaded_count %=
					    me_circ_buf_values(&instance->
							       circ_buf);

					if ((instance->status == ao_status_stream_end_wait) && !instance->preloaded_count) {	//Stoping. Whole buffer was copied.
						break;
					}
				}
			}

			if ((count <= 0) || (instance->stop_data_count && (instance->stop_data_count <= instance->data_count))) {	//End of work.
				break;
			}
		}		//Repeat if still is under half fifo
		while ((status =
			inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF);

		//Unblock interrupts
		ctrl = inl(instance->ctrl_reg);
		if (count >= 0) {	//Copy was successful.
			if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) {	//Finishing work. No more interrupts.
				PDEBUG("Finishing work. Interrupt disabled.\n");
				instance->status = ao_status_stream_end_wait;
			} else if (count > 0) {	//Normal work. Enable interrupt.
				PDEBUG("Normal work. Enable interrupt.\n");
				ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
			} else {	//Normal work but there are no more data in buffer. Interrupt blocked. stream_write() will unblock it.
				PDEBUG
				    ("No data in software buffer. Interrupt blocked.\n");
			}
		} else {	//Error during copy.
			instance->status = ao_status_stream_fifo_error;
		}

		outl(ctrl, instance->ctrl_reg);
		PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
			   instance->reg_base,
			   instance->ctrl_reg - instance->reg_base, ctrl);
	} else {		//?? more than half
		PDEBUG
		    ("Interrupt come but FIFO more than half full! Reset interrupt.\n");
	}

	PINFO("ISR: Buffer count: %d.(T:%d H:%d)\n",
	      me_circ_buf_values(&instance->circ_buf), instance->circ_buf.tail,
	      instance->circ_buf.head);
	PINFO("ISR: Stop count: %d.\n", instance->stop_count);
	PINFO("ISR: Stop data count: %d.\n", instance->stop_data_count);
	PINFO("ISR: Data count: %d.\n", instance->data_count);

	//Reset interrupt latch
	inl(instance->irq_reset_reg);

	//Inform user
	wake_up_interruptible_all(&instance->wait_queue);

	return IRQ_HANDLED;
}

static void me6000_ao_destructor(struct me_subdevice *subdevice)
{
	me6000_ao_subdevice_t *instance;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	instance->ao_control_task_flag = 0;

	// Reset subdevice to asure clean exit.
	me6000_ao_io_reset_subdevice(subdevice, NULL,
				     ME_IO_RESET_SUBDEVICE_NO_FLAGS);

	// Remove any tasks from work queue. This is paranoic because it was done allready in reset().
	if (!cancel_delayed_work(&instance->ao_control_task)) {	//Wait 2 ticks to be sure that control task is removed from queue.
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(2);
	}

	if (instance->fifo & ME6000_AO_HAS_FIFO) {
		if (instance->irq) {
			free_irq(instance->irq, instance);
			instance->irq = 0;
		}

		if (instance->circ_buf.buf) {
			PDEBUG("free circ_buf = %p size=%d",
			       instance->circ_buf.buf,
			       PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
			free_pages((unsigned long)instance->circ_buf.buf,
				   ME6000_AO_CIRC_BUF_SIZE_ORDER);
		}
		instance->circ_buf.buf = NULL;
	}

	me_subdevice_deinit(&instance->base);
	kfree(instance);
}

me6000_ao_subdevice_t *me6000_ao_constructor(uint32_t reg_base,
					     spinlock_t *preload_reg_lock,
					     uint32_t *preload_flags,
					     uint32_t *triggering_flags,
					     int ao_idx,
					     int fifo,
					     int irq,
					     int high_range,
					     struct workqueue_struct *me6000_wq)
{
	me6000_ao_subdevice_t *subdevice;
	int err;

	PDEBUG("executed ID=%d.\n", ao_idx);

	/* Allocate memory for subdevice instance */
	subdevice = kmalloc(sizeof(me6000_ao_subdevice_t), GFP_KERNEL);

	if (!subdevice) {
		PERROR("Cannot get memory for subdevice instance.\n");
		return NULL;
	}

	memset(subdevice, 0, sizeof(me6000_ao_subdevice_t));

	/* Initialize subdevice base class */
	err = me_subdevice_init(&subdevice->base);

	if (err) {
		PERROR("Cannot initialize subdevice base class instance.\n");
		kfree(subdevice);
		return NULL;
	}
	// Initialize spin locks.
	spin_lock_init(&subdevice->subdevice_lock);

	subdevice->preload_reg_lock = preload_reg_lock;
	subdevice->preload_flags = preload_flags;
	subdevice->triggering_flags = triggering_flags;

	/* Store analog output index */
	subdevice->ao_idx = ao_idx;

	/* Store if analog output has fifo */
	subdevice->fifo = fifo;

	if (subdevice->fifo & ME6000_AO_HAS_FIFO) {
		/* Allocate and initialize circular buffer */
		subdevice->circ_buf.mask = ME6000_AO_CIRC_BUF_COUNT - 1;
		subdevice->circ_buf.buf =
		    (void *)__get_free_pages(GFP_KERNEL,
					     ME6000_AO_CIRC_BUF_SIZE_ORDER);
		PDEBUG("circ_buf = %p size=%ld\n", subdevice->circ_buf.buf,
		       ME6000_AO_CIRC_BUF_SIZE);

		if (!subdevice->circ_buf.buf) {
			PERROR
			    ("Cannot initialize subdevice base class instance.\n");
			kfree(subdevice);
			return NULL;
		}

		memset(subdevice->circ_buf.buf, 0, ME6000_AO_CIRC_BUF_SIZE);
	} else {
		subdevice->circ_buf.mask = 0;
		subdevice->circ_buf.buf = NULL;
	}
	subdevice->circ_buf.head = 0;
	subdevice->circ_buf.tail = 0;

	subdevice->status = ao_status_none;
	subdevice->ao_control_task_flag = 0;
	subdevice->timeout.delay = 0;
	subdevice->timeout.start_time = jiffies;

	/* Initialize wait queue */
	init_waitqueue_head(&subdevice->wait_queue);

	/* Initialize single value to 0V */
	subdevice->single_value = 0x8000;
	subdevice->single_value_in_fifo = 0x8000;

	/* Initialize range boarders */
	if (high_range) {
		subdevice->min = ME6000_AO_MIN_RANGE_HIGH;
		subdevice->max = ME6000_AO_MAX_RANGE_HIGH;
	} else {
		subdevice->min = ME6000_AO_MIN_RANGE;
		subdevice->max = ME6000_AO_MAX_RANGE;
	}

	/* Register interrupt service routine */

	if (subdevice->fifo & ME6000_AO_HAS_FIFO) {
		subdevice->irq = irq;
		if (request_irq(subdevice->irq, me6000_ao_isr,
#ifdef IRQF_DISABLED
				IRQF_DISABLED | IRQF_SHARED,
#else
				SA_INTERRUPT | SA_SHIRQ,
#endif
				ME6000_NAME, subdevice)) {
			PERROR("Cannot get interrupt line.\n");
			PDEBUG("free circ_buf = %p size=%d",
			       subdevice->circ_buf.buf,
			       PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
			free_pages((unsigned long)subdevice->circ_buf.buf,
				   ME6000_AO_CIRC_BUF_SIZE_ORDER);
			subdevice->circ_buf.buf = NULL;
			kfree(subdevice);
			return NULL;
		}
		PINFO("Registered irq=%d.\n", subdevice->irq);
	} else {
		subdevice->irq = 0;
	}

	/* Initialize registers */
	// Only streamed subdevices support interrupts. For the rest this register has no meaning.
	subdevice->irq_status_reg = reg_base + ME6000_AO_IRQ_STATUS_REG;
	subdevice->preload_reg = reg_base + ME6000_AO_PRELOAD_REG;

	if (ao_idx == 0) {
		subdevice->ctrl_reg = reg_base + ME6000_AO_00_CTRL_REG;
		subdevice->status_reg = reg_base + ME6000_AO_00_STATUS_REG;
		subdevice->fifo_reg = reg_base + ME6000_AO_00_FIFO_REG;
		subdevice->timer_reg = reg_base + ME6000_AO_00_TIMER_REG;
		subdevice->irq_reset_reg =
		    reg_base + ME6000_AO_00_IRQ_RESET_REG;
		subdevice->single_reg = reg_base + ME6000_AO_00_SINGLE_REG;
	} else if (ao_idx == 1) {
		subdevice->ctrl_reg = reg_base + ME6000_AO_01_CTRL_REG;
		subdevice->status_reg = reg_base + ME6000_AO_01_STATUS_REG;
		subdevice->fifo_reg = reg_base + ME6000_AO_01_FIFO_REG;
		subdevice->timer_reg = reg_base + ME6000_AO_01_TIMER_REG;
		subdevice->irq_reset_reg =
		    reg_base + ME6000_AO_01_IRQ_RESET_REG;
		subdevice->single_reg = reg_base + ME6000_AO_01_SINGLE_REG;
	} else if (ao_idx == 2) {
		subdevice->ctrl_reg = reg_base + ME6000_AO_02_CTRL_REG;
		subdevice->status_reg = reg_base + ME6000_AO_02_STATUS_REG;
		subdevice->fifo_reg = reg_base + ME6000_AO_02_FIFO_REG;
		subdevice->timer_reg = reg_base + ME6000_AO_02_TIMER_REG;
		subdevice->irq_reset_reg =
		    reg_base + ME6000_AO_02_IRQ_RESET_REG;
		subdevice->single_reg = reg_base + ME6000_AO_02_SINGLE_REG;
	} else if (ao_idx == 3) {
		subdevice->ctrl_reg = reg_base + ME6000_AO_03_CTRL_REG;
		subdevice->status_reg = reg_base + ME6000_AO_03_STATUS_REG;
		subdevice->fifo_reg = reg_base + ME6000_AO_03_FIFO_REG;
		subdevice->timer_reg = reg_base + ME6000_AO_03_TIMER_REG;
		subdevice->irq_reset_reg =
		    reg_base + ME6000_AO_03_IRQ_RESET_REG;
		subdevice->single_reg = reg_base + ME6000_AO_03_SINGLE_REG;
	} else {
		subdevice->ctrl_reg = reg_base + ME6000_AO_DUMY;
		subdevice->fifo_reg = reg_base + ME6000_AO_DUMY;
		subdevice->timer_reg = reg_base + ME6000_AO_DUMY;
		subdevice->irq_reset_reg = reg_base + ME6000_AO_DUMY;
		subdevice->single_reg = reg_base + ME6000_AO_DUMY;

		subdevice->status_reg = reg_base + ME6000_AO_SINGLE_STATUS_REG;
		if (ao_idx == 4) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_04_SINGLE_REG;
		} else if (ao_idx == 5) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_05_SINGLE_REG;
		} else if (ao_idx == 6) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_06_SINGLE_REG;
		} else if (ao_idx == 7) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_07_SINGLE_REG;
		} else if (ao_idx == 8) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_08_SINGLE_REG;
		} else if (ao_idx == 9) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_09_SINGLE_REG;
		} else if (ao_idx == 10) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_10_SINGLE_REG;
		} else if (ao_idx == 11) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_11_SINGLE_REG;
		} else if (ao_idx == 12) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_12_SINGLE_REG;
		} else if (ao_idx == 13) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_13_SINGLE_REG;
		} else if (ao_idx == 14) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_14_SINGLE_REG;
		} else if (ao_idx == 15) {
			subdevice->single_reg =
			    reg_base + ME6000_AO_15_SINGLE_REG;
		} else {
			PERROR_CRITICAL("WRONG SUBDEVICE ID=%d!", ao_idx);
			me_subdevice_deinit((me_subdevice_t *) subdevice);
			if (subdevice->fifo) {
				free_pages((unsigned long)subdevice->circ_buf.
					   buf, ME6000_AO_CIRC_BUF_SIZE_ORDER);
			}
			subdevice->circ_buf.buf = NULL;
			kfree(subdevice);
			return NULL;
		}
	}
#ifdef MEDEBUG_DEBUG_REG
	subdevice->reg_base = reg_base;
#endif

	/* Override base class methods. */
	subdevice->base.me_subdevice_destructor = me6000_ao_destructor;
	subdevice->base.me_subdevice_io_reset_subdevice =
	    me6000_ao_io_reset_subdevice;
	subdevice->base.me_subdevice_io_single_config =
	    me6000_ao_io_single_config;
	subdevice->base.me_subdevice_io_single_read = me6000_ao_io_single_read;
	subdevice->base.me_subdevice_io_single_write =
	    me6000_ao_io_single_write;
	subdevice->base.me_subdevice_io_stream_config =
	    me6000_ao_io_stream_config;
	subdevice->base.me_subdevice_io_stream_new_values =
	    me6000_ao_io_stream_new_values;
	subdevice->base.me_subdevice_io_stream_write =
	    me6000_ao_io_stream_write;
	subdevice->base.me_subdevice_io_stream_start =
	    me6000_ao_io_stream_start;
	subdevice->base.me_subdevice_io_stream_status =
	    me6000_ao_io_stream_status;
	subdevice->base.me_subdevice_io_stream_stop = me6000_ao_io_stream_stop;
	subdevice->base.me_subdevice_query_number_channels =
	    me6000_ao_query_number_channels;
	subdevice->base.me_subdevice_query_subdevice_type =
	    me6000_ao_query_subdevice_type;
	subdevice->base.me_subdevice_query_subdevice_caps =
	    me6000_ao_query_subdevice_caps;
	subdevice->base.me_subdevice_query_subdevice_caps_args =
	    me6000_ao_query_subdevice_caps_args;
	subdevice->base.me_subdevice_query_range_by_min_max =
	    me6000_ao_query_range_by_min_max;
	subdevice->base.me_subdevice_query_number_ranges =
	    me6000_ao_query_number_ranges;
	subdevice->base.me_subdevice_query_range_info =
	    me6000_ao_query_range_info;
	subdevice->base.me_subdevice_query_timer = me6000_ao_query_timer;

	//prepare work queue and work function
	subdevice->me6000_workqueue = me6000_wq;

/* workqueue API changed in kernel 2.6.20 */
	INIT_DELAYED_WORK(&subdevice->ao_control_task,
			  me6000_ao_work_control_task);

	if (subdevice->fifo) {	//Set speed
		outl(ME6000_AO_MIN_CHAN_TICKS - 1, subdevice->timer_reg);
		subdevice->hardware_stop_delay = HZ / 10;	//100ms
	}

	return subdevice;
}

/** @brief Stop presentation. Preserve FIFOs.
*
* @param instance The subdevice instance (pointer).
*/
inline int ao_stop_immediately(me6000_ao_subdevice_t *instance)
{
	unsigned long cpu_flags;
	uint32_t ctrl;
	int timeout;
	int i;
	uint32_t single_mask;

	if (instance->ao_idx < ME6000_AO_SINGLE_STATUS_OFFSET)
		single_mask = 0x0000;
	else
		single_mask = 0x0001 << (instance->ao_idx -
				ME6000_AO_SINGLE_STATUS_OFFSET);

	timeout =
	    (instance->hardware_stop_delay >
	     (HZ / 10)) ? instance->hardware_stop_delay : HZ / 10;
	for (i = 0; i <= timeout; i++) {
		if (instance->fifo) {
			spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
			// Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
			ctrl = inl(instance->ctrl_reg);
			ctrl |=
			    ME6000_AO_CTRL_BIT_STOP |
			    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
			ctrl &=
			    ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
			      ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
			outl(ctrl, instance->ctrl_reg);
			PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->ctrl_reg - instance->reg_base,
				   ctrl);
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);

			if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) {	// Exit.
				break;
			}
		} else {
			if (!(inl(instance->status_reg) & single_mask)) {	// Exit.
				break;
			}
		}

		PINFO("<%s> Wait for stop: %d\n", __func__, i);

		//Still working!
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(1);
	}

	if (i > timeout) {
		PERROR_CRITICAL("FSM IS BUSY!\n");
		return ME_ERRNO_INTERNAL;
	}
	return ME_ERRNO_SUCCESS;
}

/** @brief Copy data from circular buffer to fifo (fast) in wraparound.
* @note This is time critical function. Checking is done at begining and end only.
* @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
*
* @param instance The subdevice instance (pointer).
* @param count Maximum number of copied data.
* @param start_pos Position of the firs value in buffer.
*
* @return On success: Number of copied data.
* @return On error/success: 0.	No datas were copied => no data in buffer.
* @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
*/
inline int ao_write_data_wraparound(me6000_ao_subdevice_t *instance, int count,
				    int start_pos)
{				/// @note This is time critical function!
	uint32_t status;
	uint32_t value;
	int pos =
	    (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
	int local_count = count;
	int i = 1;

	if (count <= 0) {	//Wrong count!
		return 0;
	}

	while (i < local_count) {
		//Get value from buffer
		value = *(instance->circ_buf.buf + pos);
		//Prepare it
		if (instance->ao_idx & 0x1) {
			value <<= 16;
		}
		//Put value to FIFO
		outl(value, instance->fifo_reg);
		//PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);

		pos++;
		pos &= instance->circ_buf.mask;
		if (pos == instance->circ_buf.head) {
			pos = instance->circ_buf.tail;
		}
		i++;
	}

	status = inl(instance->status_reg);
	if (!(status & ME6000_AO_STATUS_BIT_FF)) {	//FIFO is full before all datas were copied!
		PERROR("idx=%d FIFO is full before all datas were copied!\n",
		       instance->ao_idx);
		return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
	} else {		//Add last value
		value = *(instance->circ_buf.buf + pos);
		if (instance->ao_idx & 0x1) {
			value <<= 16;
		}
		//Put value to FIFO
		outl(value, instance->fifo_reg);
		//PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
	}

	PINFO("idx=%d WRAPAROUND LOADED %d values\n", instance->ao_idx,
	      local_count);
	return local_count;
}

/** @brief Copy data from software buffer to fifo (fast).
* @note This is time critical function. Checking is done at begining and end only.
* @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
*
* @param instance The subdevice instance (pointer).
* @param count Maximum number of copied data.
* @param start_pos Position of the firs value in buffer.
*
* @return On success: Number of copied data.
* @return On error/success: 0.	No datas were copied => no data in buffer.
* @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
*/
inline int ao_write_data(me6000_ao_subdevice_t *instance, int count,
			 int start_pos)
{				/// @note This is time critical function!
	uint32_t status;
	uint32_t value;
	int pos =
	    (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
	int local_count = count;
	int max_count;
	int i = 1;

	if (count <= 0) {	//Wrong count!
		return 0;
	}

	max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
	if (max_count <= 0) {	//No data to copy!
		return 0;
	}

	if (max_count < count) {
		local_count = max_count;
	}

	while (i < local_count) {
		//Get value from buffer
		value = *(instance->circ_buf.buf + pos);
		//Prepare it
		if (instance->ao_idx & 0x1) {
			value <<= 16;
		}
		//Put value to FIFO
		outl(value, instance->fifo_reg);
		//PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);

		pos++;
		pos &= instance->circ_buf.mask;
		i++;
	}

	status = inl(instance->status_reg);
	if (!(status & ME6000_AO_STATUS_BIT_FF)) {	//FIFO is full before all datas were copied!
		PERROR("idx=%d FIFO is full before all datas were copied!\n",
		       instance->ao_idx);
		return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
	} else {		//Add last value
		value = *(instance->circ_buf.buf + pos);
		if (instance->ao_idx & 0x1) {
			value <<= 16;
		}
		//Put value to FIFO
		outl(value, instance->fifo_reg);
		//PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
	}

	PINFO("idx=%d FAST LOADED %d values\n", instance->ao_idx, local_count);
	return local_count;
}

/** @brief Copy data from software buffer to fifo (slow).
* @note This is slow function that copy all data from buffer to FIFO with full control.
*
* @param instance The subdevice instance (pointer).
* @param count Maximum number of copied data.
* @param start_pos Position of the firs value in buffer.
*
* @return On success: Number of copied values.
* @return On error/success: 0.	FIFO was full at begining.
* @return On error: -ME_ERRNO_RING_BUFFER_UNDEFFLOW.
*/
inline int ao_write_data_pooling(me6000_ao_subdevice_t *instance, int count,
				 int start_pos)
{				/// @note This is slow function!
	uint32_t status;
	uint32_t value;
	int pos =
	    (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
	int local_count = count;
	int i;
	int max_count;

	if (count <= 0) {	//Wrong count!
		PERROR("idx=%d SLOW LOADED: Wrong count!\n", instance->ao_idx);
		return 0;
	}

	max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
	if (max_count <= 0) {	//No data to copy!
		PERROR("idx=%d SLOW LOADED: No data to copy!\n",
		       instance->ao_idx);
		return 0;
	}

	if (max_count < count) {
		local_count = max_count;
	}

	for (i = 0; i < local_count; i++) {
		status = inl(instance->status_reg);
		if (!(status & ME6000_AO_STATUS_BIT_FF)) {	//FIFO is full!
			return i;
		}
		//Get value from buffer
		value = *(instance->circ_buf.buf + pos);
		//Prepare it
		if (instance->ao_idx & 0x1) {
			value <<= 16;
		}
		//Put value to FIFO
		outl(value, instance->fifo_reg);
		//PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);

		pos++;
		pos &= instance->circ_buf.mask;
	}

	PINFO("idx=%d SLOW LOADED %d values\n", instance->ao_idx, local_count);
	return local_count;
}

/** @brief Copy data from user space to circular buffer.
* @param instance The subdevice instance (pointer).
* @param count Number of datas in user space.
* @param user_values Buffer's pointer.
*
* @return On success: Number of copied values.
* @return On error: -ME_ERRNO_INTERNAL.
*/
inline int ao_get_data_from_user(me6000_ao_subdevice_t *instance, int count,
				 int *user_values)
{
	int i, err;
	int empty_space;
	int copied;
	int value;

	empty_space = me_circ_buf_space(&instance->circ_buf);
	//We have only this space free.
	copied = (count < empty_space) ? count : empty_space;
	for (i = 0; i < copied; i++) {	//Copy from user to buffer
		if ((err = get_user(value, (int *)(user_values + i)))) {
			PERROR
			    ("idx=%d BUFFER LOADED: get_user(0x%p) return an error: %d\n",
			     instance->ao_idx, user_values + i, err);
			return -ME_ERRNO_INTERNAL;
		}
		/// @note The analog output in me6000 series has size of 16 bits.
		*(instance->circ_buf.buf + instance->circ_buf.head) =
		    (uint16_t) value;
		instance->circ_buf.head++;
		instance->circ_buf.head &= instance->circ_buf.mask;
	}

	PINFO("idx=%d BUFFER LOADED %d values\n", instance->ao_idx, copied);
	return copied;
}

static void me6000_ao_work_control_task(struct work_struct *work)
{
	me6000_ao_subdevice_t *instance;
	unsigned long cpu_flags = 0;
	uint32_t status;
	uint32_t ctrl;
	uint32_t synch;
	int reschedule = 0;
	int signaling = 0;
	uint32_t single_mask;

	instance =
	    container_of((void *)work, me6000_ao_subdevice_t, ao_control_task);
	PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
	      instance->ao_idx);

	status = inl(instance->status_reg);
	PDEBUG_REG("status_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
		   instance->status_reg - instance->reg_base, status);

/// @note AO_STATUS_BIT_FSM doesn't work as should be for pure single channels (idx>=4)
//      single_mask = (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET < 0) ? 0x0000 : (0x0001 << (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET));
	single_mask = *instance->triggering_flags & (0x1 << instance->ao_idx);

	switch (instance->status) {	// Checking actual mode.

		// Not configured for work.
	case ao_status_none:
		break;

		//This are stable modes. No need to do anything. (?)
	case ao_status_single_configured:
	case ao_status_stream_configured:
	case ao_status_stream_fifo_error:
	case ao_status_stream_buffer_error:
	case ao_status_stream_error:
		PERROR("Shouldn't be running!.\n");
		break;

		// Single modes
	case ao_status_single_run_wait:
	case ao_status_single_run:
	case ao_status_single_end_wait:
		if (instance->fifo) {	// Extra registers.
			if (!(status & ME6000_AO_STATUS_BIT_FSM)) {	// State machine is not working.
				if (((instance->fifo & ME6000_AO_HAS_FIFO)
				     && (!(status & ME6000_AO_STATUS_BIT_EF)))
				    || (!(instance->fifo & ME6000_AO_HAS_FIFO))) {	// Single is in end state.
					PDEBUG
					    ("Single call has been complited.\n");

					// Set correct value for single_read();
					instance->single_value =
					    instance->single_value_in_fifo;

					// Set status as 'ao_status_single_end'
					instance->status = ao_status_single_end;

					spin_lock(instance->preload_reg_lock);
					if ((single_mask) && (*instance->preload_flags & (ME6000_AO_SYNC_HOLD << instance->ao_idx))) {	// This is one of synchronous start channels. Set all as triggered.
						*instance->triggering_flags =
						    0x00000000;
					} else {
						//Set this channel as triggered (none active).
						*instance->triggering_flags &=
						    ~(0x1 << instance->ao_idx);
					}
					spin_unlock(instance->preload_reg_lock);

					// Signal the end.
					signaling = 1;
					// Wait for stop ISM.
					reschedule = 1;

					break;
				}
			}
			// Check timeout.
			if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {	// Timeout
				PDEBUG("Timeout reached.\n");
				// Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
				spin_lock_irqsave(&instance->subdevice_lock,
						  cpu_flags);
				ctrl = inl(instance->ctrl_reg);
				ctrl |=
				    ME6000_AO_CTRL_BIT_STOP |
				    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
				ctrl &=
				    ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
				      ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
				ctrl &=
				    ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
				      ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
				//Disabling FIFO
				ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;

				outl(ctrl, instance->ctrl_reg);
				PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
					   instance->reg_base,
					   instance->ctrl_reg -
					   instance->reg_base, ctrl);
				spin_unlock_irqrestore(&instance->
						       subdevice_lock,
						       cpu_flags);

				//Reset interrupt latch
				inl(instance->irq_reset_reg);

				spin_lock(instance->preload_reg_lock);
				//Remove from synchronous start. Block triggering from this output.
				synch = inl(instance->preload_reg);
				synch &=
				    ~((ME6000_AO_SYNC_HOLD |
				       ME6000_AO_SYNC_EXT_TRIG) << instance->
				      ao_idx);
				if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {	// No FIFO - set to single safe mode
					synch |=
					    ME6000_AO_SYNC_HOLD << instance->
					    ao_idx;
				}
				outl(synch, instance->preload_reg);
				PDEBUG_REG
				    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				     instance->reg_base,
				     instance->preload_reg - instance->reg_base,
				     synch);
				//Set this channel as triggered (none active).
				*instance->triggering_flags &=
				    ~(0x1 << instance->ao_idx);
				spin_unlock(instance->preload_reg_lock);

				// Set correct value for single_read();
				instance->single_value_in_fifo =
				    instance->single_value;

				instance->status = ao_status_single_end;

				// Signal the end.
				signaling = 1;
			}
		} else {	// No extra registers.
/*
				if (!(status & single_mask))
				{// State machine is not working.
					PDEBUG("Single call has been complited.\n");

					// Set correct value for single_read();
					instance->single_value = instance->single_value_in_fifo;

					// Set status as 'ao_status_single_end'
					instance->status = ao_status_single_end;

					// Signal the end.
					signaling = 1;
					// Wait for stop ISM.
					reschedule = 1;

					break;
				}
*/
			if (!single_mask) {	// Was triggered.
				PDEBUG("Single call has been complited.\n");

				// Set correct value for single_read();
				instance->single_value =
				    instance->single_value_in_fifo;

				// Set status as 'ao_status_single_end'
				instance->status = ao_status_single_end;

				// Signal the end.
				signaling = 1;

				break;
			}
			// Check timeout.
			if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {	// Timeout
				PDEBUG("Timeout reached.\n");

				spin_lock(instance->preload_reg_lock);
				//Remove from synchronous start. Block triggering from this output.
				synch = inl(instance->preload_reg);
				synch &=
				    ~(ME6000_AO_SYNC_EXT_TRIG << instance->
				      ao_idx);
				synch |=
				    ME6000_AO_SYNC_HOLD << instance->ao_idx;

				outl(synch, instance->preload_reg);
				PDEBUG_REG
				    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				     instance->reg_base,
				     instance->preload_reg - instance->reg_base,
				     synch);
				//Set this channel as triggered (none active).
				*instance->triggering_flags &=
				    ~(0x1 << instance->ao_idx);
				spin_unlock(instance->preload_reg_lock);

				// Restore old settings.
				PDEBUG("Write old value back to register.\n");
				outl(instance->single_value,
				     instance->single_reg);
				PDEBUG_REG
				    ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
				     instance->reg_base,
				     instance->single_reg - instance->reg_base,
				     instance->single_value);

				// Set correct value for single_read();
				instance->single_value_in_fifo =
				    instance->single_value;

				instance->status = ao_status_single_end;

				// Signal the end.
				signaling = 1;
			}
		}

		// Wait for stop.
		reschedule = 1;
		break;

	case ao_status_stream_end:
		if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {	// No FIFO
			PERROR_CRITICAL
			    ("Streaming on single device! This feature is not implemented in this version!\n");
			instance->status = ao_status_stream_error;
			// Signal the end.
			signaling = 1;
			break;
		}
	case ao_status_single_end:
		if (instance->fifo) {	// Extra registers.
			if (status & ME6000_AO_STATUS_BIT_FSM) {	// State machine is working but the status is set to end. Force stop.

				// Wait for stop.
				reschedule = 1;
			}

			spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
			// Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
			ctrl = inl(instance->ctrl_reg);
			ctrl |=
			    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP |
			    ME6000_AO_CTRL_BIT_STOP;
			ctrl &=
			    ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
			      ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
			outl(ctrl, instance->ctrl_reg);
			PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->ctrl_reg - instance->reg_base,
				   ctrl);
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);

			//Reset interrupt latch
			inl(instance->irq_reset_reg);
		} else {	// No extra registers.
/*
				if (status & single_mask)
				{// State machine is working but the status is set to end. Force stop.

					// Wait for stop.
					reschedule = 1;
				}
*/
		}
		break;

		// Stream modes
	case ao_status_stream_run_wait:
		if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {	// No FIFO
			PERROR_CRITICAL
			    ("Streaming on single device! This feature is not implemented in this version!\n");
			instance->status = ao_status_stream_error;
			// Signal the end.
			signaling = 1;
			break;
		}

		if (status & ME6000_AO_STATUS_BIT_FSM) {	// State machine is working. Waiting for start finish.
			instance->status = ao_status_stream_run;

			// Signal end of this step
			signaling = 1;
		} else {	// State machine is not working.
			if (!(status & ME6000_AO_STATUS_BIT_EF)) {	// FIFO is empty. Procedure has started and finish already!
				instance->status = ao_status_stream_end;

				// Signal the end.
				signaling = 1;
				// Wait for stop.
				reschedule = 1;
				break;
			}
		}

		// Check timeout.
		if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {	// Timeout
			PDEBUG("Timeout reached.\n");
			// Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
			spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
			ctrl = inl(instance->ctrl_reg);
			ctrl |=
			    ME6000_AO_CTRL_BIT_STOP |
			    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
			ctrl &=
			    ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
			      ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
			outl(ctrl, instance->ctrl_reg);
			PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->ctrl_reg - instance->reg_base,
				   ctrl);
			spin_unlock_irqrestore(&instance->subdevice_lock,
					       cpu_flags);

			//Reset interrupt latch
			inl(instance->irq_reset_reg);

			spin_lock(instance->preload_reg_lock);
			//Remove from synchronous start. Block triggering from this output.
			synch = inl(instance->preload_reg);
			synch &=
			    ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
			      instance->ao_idx);
			outl(synch, instance->preload_reg);
			PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
				   instance->reg_base,
				   instance->preload_reg - instance->reg_base,
				   synch);
			spin_unlock(instance->preload_reg_lock);

			instance->status = ao_status_stream_end;

			// Signal the end.
			signaling = 1;
		}
		// Wait for stop.
		reschedule = 1;
		break;

	case ao_status_stream_run:
		if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {	// No FIFO
			PERROR_CRITICAL
			    ("Streaming on single device! This feature is not implemented in this version!\n");
			instance->status = ao_status_stream_error;
			// Signal the end.
			signaling = 1;
			break;
		}

		if (!(status & ME6000_AO_STATUS_BIT_FSM)) {	// State machine is not working. This is an error.
			// BROKEN PIPE!
			if (!(status & ME6000_AO_STATUS_BIT_EF)) {	// FIFO is empty.
				if (me_circ_buf_values(&instance->circ_buf)) {	// Software buffer is not empty.
					if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) {	//Finishing work. Requed data shown.
						PDEBUG
						    ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
						instance->status =
						    ao_status_stream_end;
					} else {
						PERROR
						    ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
						instance->status =
						    ao_status_stream_buffer_error;
					}
				} else {	// Software buffer is empty.
					PDEBUG
					    ("ISM stoped. No data in FIFO. Buffer is empty.\n");
					instance->status = ao_status_stream_end;
				}
			} else {	// There are still datas in FIFO.
				if (me_circ_buf_values(&instance->circ_buf)) {	// Software buffer is not empty.
					PERROR
					    ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
				} else {	// Software buffer is empty.
					PERROR
					    ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
				}
				instance->status = ao_status_stream_fifo_error;

			}

			// Signal the failure.
			signaling = 1;
			break;
		}
		// Wait for stop.
		reschedule = 1;
		break;

	case ao_status_stream_end_wait:
		if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {	// No FIFO
			PERROR_CRITICAL
			    ("Streaming on single device! This feature is not implemented in this version!\n");
			instance->status = ao_status_stream_error;
			// Signal the end.
			signaling = 1;
			break;
		}

		if (!(status & ME6000_AO_STATUS_BIT_FSM)) {	// State machine is not working. Waiting for stop finish.
			instance->status = ao_status_stream_end;
			signaling = 1;
		}
		// State machine is working.
		reschedule = 1;
		break;

	default:
		PERROR_CRITICAL("Status is in wrong state (%d)!\n",
				instance->status);
		instance->status = ao_status_stream_error;
		// Signal the end.
		signaling = 1;
		break;

	}

	if (signaling) {	//Signal it.
		wake_up_interruptible_all(&instance->wait_queue);
	}

	if (instance->ao_control_task_flag && reschedule) {	// Reschedule task
		queue_delayed_work(instance->me6000_workqueue,
				   &instance->ao_control_task, 1);
	} else {
		PINFO("<%s> Ending control task.\n", __func__);
	}

}

static int me6000_ao_query_range_by_min_max(me_subdevice_t *subdevice,
					    int unit,
					    int *min,
					    int *max, int *maxdata, int *range)
{
	me6000_ao_subdevice_t *instance;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if ((*max - *min) < 0) {
		PERROR("Invalid minimum and maximum values specified.\n");
		return ME_ERRNO_INVALID_MIN_MAX;
	}

	if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
		if ((*max <= (instance->max + 1000)) && (*min >= instance->min)) {
			*min = instance->min;
			*max = instance->max;
			*maxdata = ME6000_AO_MAX_DATA;
			*range = 0;
		} else {
			PERROR("No matching range available.\n");
			return ME_ERRNO_NO_RANGE;
		}
	} else {
		PERROR("Invalid physical unit specified.\n");
		return ME_ERRNO_INVALID_UNIT;
	}

	return ME_ERRNO_SUCCESS;
}

static int me6000_ao_query_number_ranges(me_subdevice_t *subdevice,
					 int unit, int *count)
{
	me6000_ao_subdevice_t *instance;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
		*count = 1;
	} else {
		*count = 0;
	}

	return ME_ERRNO_SUCCESS;
}

static int me6000_ao_query_range_info(me_subdevice_t *subdevice,
				      int range,
				      int *unit,
				      int *min, int *max, int *maxdata)
{
	me6000_ao_subdevice_t *instance;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (range == 0) {
		*unit = ME_UNIT_VOLT;
		*min = instance->min;
		*max = instance->max;
		*maxdata = ME6000_AO_MAX_DATA;
	} else {
		PERROR("Invalid range number specified.\n");
		return ME_ERRNO_INVALID_RANGE;
	}

	return ME_ERRNO_SUCCESS;
}

static int me6000_ao_query_timer(me_subdevice_t *subdevice,
				 int timer,
				 int *base_frequency,
				 long long *min_ticks, long long *max_ticks)
{
	me6000_ao_subdevice_t *instance;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (instance->fifo) {	//Streaming device.
		*base_frequency = ME6000_AO_BASE_FREQUENCY;
		if (timer == ME_TIMER_ACQ_START) {
			*min_ticks = ME6000_AO_MIN_ACQ_TICKS;
			*max_ticks = ME6000_AO_MAX_ACQ_TICKS;
		} else if (timer == ME_TIMER_CONV_START) {
			*min_ticks = ME6000_AO_MIN_CHAN_TICKS;
			*max_ticks = ME6000_AO_MAX_CHAN_TICKS;
		}
	} else {		//Not streaming device!
		*base_frequency = 0;
		*min_ticks = 0;
		*max_ticks = 0;
	}

	return ME_ERRNO_SUCCESS;
}

static int me6000_ao_query_number_channels(me_subdevice_t *subdevice,
					   int *number)
{
	me6000_ao_subdevice_t *instance;
	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	*number = 1;
	return ME_ERRNO_SUCCESS;
}

static int me6000_ao_query_subdevice_type(me_subdevice_t *subdevice,
					  int *type, int *subtype)
{
	me6000_ao_subdevice_t *instance;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	*type = ME_TYPE_AO;
	*subtype =
	    (instance->
	     fifo & ME6000_AO_HAS_FIFO) ? ME_SUBTYPE_STREAMING :
	    ME_SUBTYPE_SINGLE;

	return ME_ERRNO_SUCCESS;
}

static int me6000_ao_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
{
	me6000_ao_subdevice_t *instance;
	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	*caps =
	    ME_CAPS_AO_TRIG_SYNCHRONOUS | ((instance->fifo) ? ME_CAPS_AO_FIFO :
					   ME_CAPS_NONE);

	return ME_ERRNO_SUCCESS;
}

static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
					       int cap, int *args, int count)
{
	me6000_ao_subdevice_t *instance;
	int err = ME_ERRNO_SUCCESS;

	instance = (me6000_ao_subdevice_t *) subdevice;

	PDEBUG("executed. idx=%d\n", instance->ao_idx);

	if (count != 1) {
		PERROR("Invalid capability argument count.\n");
		return ME_ERRNO_INVALID_CAP_ARG_COUNT;
	}

	switch (cap) {
	case ME_CAP_AI_FIFO_SIZE:
		args[0] = (instance->fifo) ? ME6000_AO_FIFO_COUNT : 0;
		break;

	case ME_CAP_AI_BUFFER_SIZE:
		args[0] =
		    (instance->circ_buf.buf) ? ME6000_AO_CIRC_BUF_COUNT : 0;
		break;

	default:
		PERROR("Invalid capability.\n");
		err = ME_ERRNO_INVALID_CAP;
		args[0] = 0;
	}

	return err;
}