summaryrefslogtreecommitdiff
path: root/miscutils/hdparm.c
blob: 04ce073dbc6a195d715b50190fd13375f394330c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
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
/* vi: set sw=4 ts=4: */
/*
 * hdparm implementation for busybox
 *
 *
 * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
 *
 * Hacked by Tito <farmatito@tiscali.it> for size optimization.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 *
 * This program is based on the source code of hdparm: see below...
 * hdparm.c - Command line interface to get/set hard disk parameters
 *          - by Mark Lord (C) 1994-2002 -- freely distributable
 */

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <endian.h>
#include <sys/ioctl.h>
#include <sys/shm.h>
#include <sys/sysmacros.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/mount.h>
#include "busybox.h"
#include <linux/types.h>
#include <linux/hdreg.h>
#include <linux/major.h>
#include <asm/byteorder.h>


#if (__BYTE_ORDER == __BIG_ENDIAN) && !defined(__USE_XOPEN)
#define __USE_XOPEN
#endif

/* device types */
/* ------------ */
#define NO_DEV                  0xffff
#define ATA_DEV                 0x0000
#define ATAPI_DEV               0x0001

/* word definitions */
/* ---------------- */
#define GEN_CONFIG		0   /* general configuration */
#define LCYLS			1   /* number of logical cylinders */
#define CONFIG			2   /* specific configuration */
#define LHEADS			3   /* number of logical heads */
#define TRACK_BYTES		4   /* number of bytes/track (ATA-1) */
#define SECT_BYTES		5   /* number of bytes/sector (ATA-1) */
#define LSECTS			6   /* number of logical sectors/track */
#define START_SERIAL            10  /* ASCII serial number */
#define LENGTH_SERIAL           10  /* 10 words (20 bytes or characters) */
#define BUF_TYPE		20  /* buffer type (ATA-1) */
#define BUFFER__SIZE		21  /* buffer size (ATA-1) */
#define RW_LONG			22  /* extra bytes in R/W LONG cmd ( < ATA-4)*/
#define START_FW_REV            23  /* ASCII firmware revision */
#define LENGTH_FW_REV		 4  /*  4 words (8 bytes or characters) */
#define START_MODEL    		27  /* ASCII model number */
#define LENGTH_MODEL    	20  /* 20 words (40 bytes or characters) */
#define SECTOR_XFER_MAX	        47  /* r/w multiple: max sectors xfered */
#define DWORD_IO		48  /* can do double-word IO (ATA-1 only) */
#define CAPAB_0			49  /* capabilities */
#define CAPAB_1			50
#define PIO_MODE		51  /* max PIO mode supported (obsolete)*/
#define DMA_MODE		52  /* max Singleword DMA mode supported (obs)*/
#define WHATS_VALID		53  /* what fields are valid */
#define LCYLS_CUR		54  /* current logical cylinders */
#define LHEADS_CUR		55  /* current logical heads */
#define LSECTS_CUR	        56  /* current logical sectors/track */
#define CAPACITY_LSB		57  /* current capacity in sectors */
#define CAPACITY_MSB		58
#define SECTOR_XFER_CUR		59  /* r/w multiple: current sectors xfered */
#define LBA_SECTS_LSB		60  /* LBA: total number of user */
#define LBA_SECTS_MSB		61  /*      addressable sectors */
#define SINGLE_DMA		62  /* singleword DMA modes */
#define MULTI_DMA		63  /* multiword DMA modes */
#define ADV_PIO_MODES		64  /* advanced PIO modes supported */
				    /* multiword DMA xfer cycle time: */
#define DMA_TIME_MIN		65  /*   - minimum */
#define DMA_TIME_NORM		66  /*   - manufacturer's recommended	*/
				    /* minimum PIO xfer cycle time: */
#define PIO_NO_FLOW		67  /*   - without flow control */
#define PIO_FLOW		68  /*   - with IORDY flow control */
#define PKT_REL			71  /* typical #ns from PKT cmd to bus rel */
#define SVC_NBSY		72  /* typical #ns from SERVICE cmd to !BSY */
#define CDR_MAJOR		73  /* CD ROM: major version number */
#define CDR_MINOR		74  /* CD ROM: minor version number */
#define QUEUE_DEPTH		75  /* queue depth */
#define MAJOR			80  /* major version number */
#define MINOR			81  /* minor version number */
#define CMDS_SUPP_0		82  /* command/feature set(s) supported */
#define CMDS_SUPP_1		83
#define CMDS_SUPP_2		84
#define CMDS_EN_0		85  /* command/feature set(s) enabled */
#define CMDS_EN_1		86
#define CMDS_EN_2		87
#define ULTRA_DMA		88  /* ultra DMA modes */
				    /* time to complete security erase */
#define ERASE_TIME		89  /*   - ordinary */
#define ENH_ERASE_TIME		90  /*   - enhanced */
#define ADV_PWR			91  /* current advanced power management level
				       in low byte, 0x40 in high byte. */
#define PSWD_CODE		92  /* master password revision code	*/
#define HWRST_RSLT		93  /* hardware reset result */
#define ACOUSTIC  		94  /* acoustic mgmt values ( >= ATA-6) */
#define LBA_LSB			100 /* LBA: maximum.  Currently only 48 */
#define LBA_MID			101 /*      bits are used, but addr 103 */
#define LBA_48_MSB		102 /*      has been reserved for LBA in */
#define LBA_64_MSB		103 /*      the future. */
#define RM_STAT 		127 /* removable media status notification feature set support */
#define SECU_STATUS		128 /* security status */
#define CFA_PWR_MODE		160 /* CFA power mode 1 */
#define START_MEDIA             176 /* media serial number */
#define LENGTH_MEDIA            20  /* 20 words (40 bytes or characters)*/
#define START_MANUF             196 /* media manufacturer I.D. */
#define LENGTH_MANUF            10  /* 10 words (20 bytes or characters) */
#define INTEGRITY		255 /* integrity word */

/* bit definitions within the words */
/* -------------------------------- */

/* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
#define VALID			0xc000
#define VALID_VAL		0x4000
/* many words are considered invalid if they are either all-0 or all-1 */
#define NOVAL_0			0x0000
#define NOVAL_1			0xffff

/* word 0: gen_config */
#define NOT_ATA			0x8000
#define NOT_ATAPI		0x4000	/* (check only if bit 15 == 1) */
#define MEDIA_REMOVABLE		0x0080
#define DRIVE_NOT_REMOVABLE	0x0040  /* bit obsoleted in ATA 6 */
#define INCOMPLETE		0x0004
#define CFA_SUPPORT_VAL		0x848a	/* 848a=CFA feature set support */
#define DRQ_RESPONSE_TIME	0x0060
#define DRQ_3MS_VAL		0x0000
#define DRQ_INTR_VAL		0x0020
#define DRQ_50US_VAL		0x0040
#define PKT_SIZE_SUPPORTED	0x0003
#define PKT_SIZE_12_VAL		0x0000
#define PKT_SIZE_16_VAL		0x0001
#define EQPT_TYPE		0x1f00
#define SHIFT_EQPT		8

#define CDROM 0x0005

#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static const char * const pkt_str[] = {
	"Direct-access device",			/* word 0, bits 12-8 = 00 */
	"Sequential-access device",		/* word 0, bits 12-8 = 01 */
	"Printer",				/* word 0, bits 12-8 = 02 */
	"Processor",				/* word 0, bits 12-8 = 03 */
	"Write-once device",			/* word 0, bits 12-8 = 04 */
	"CD-ROM",				/* word 0, bits 12-8 = 05 */
	"Scanner",				/* word 0, bits 12-8 = 06 */
	"Optical memory",			/* word 0, bits 12-8 = 07 */
	"Medium changer",			/* word 0, bits 12-8 = 08 */
	"Communications device",		/* word 0, bits 12-8 = 09 */
	"ACS-IT8 device",			/* word 0, bits 12-8 = 0a */
	"ACS-IT8 device",			/* word 0, bits 12-8 = 0b */
	"Array controller",			/* word 0, bits 12-8 = 0c */
	"Enclosure services",			/* word 0, bits 12-8 = 0d */
	"Reduced block command device",		/* word 0, bits 12-8 = 0e */
	"Optical card reader/writer",		/* word 0, bits 12-8 = 0f */
	"",					/* word 0, bits 12-8 = 10 */
	"",					/* word 0, bits 12-8 = 11 */
	"",					/* word 0, bits 12-8 = 12 */
	"",					/* word 0, bits 12-8 = 13 */
	"",					/* word 0, bits 12-8 = 14 */
	"",					/* word 0, bits 12-8 = 15 */
	"",					/* word 0, bits 12-8 = 16 */
	"",					/* word 0, bits 12-8 = 17 */
	"",					/* word 0, bits 12-8 = 18 */
	"",					/* word 0, bits 12-8 = 19 */
	"",					/* word 0, bits 12-8 = 1a */
	"",					/* word 0, bits 12-8 = 1b */
	"",					/* word 0, bits 12-8 = 1c */
	"",					/* word 0, bits 12-8 = 1d */
	"",					/* word 0, bits 12-8 = 1e */
	"Unknown",				/* word 0, bits 12-8 = 1f */
};

static const char * const ata1_cfg_str[] = {			/* word 0 in ATA-1 mode */
	"reserved",				/* bit 0 */
	"hard sectored",			/* bit 1 */
	"soft sectored",			/* bit 2 */
	"not MFM encoded ",			/* bit 3 */
	"head switch time > 15us",		/* bit 4 */
	"spindle motor control option",		/* bit 5 */
	"fixed drive",				/* bit 6 */
	"removable drive",			/* bit 7 */
	"disk xfer rate <= 5Mbs",		/* bit 8 */
	"disk xfer rate > 5Mbs, <= 10Mbs",	/* bit 9 */
	"disk xfer rate > 5Mbs",		/* bit 10 */
	"rotational speed tol.",		/* bit 11 */
	"data strobe offset option",		/* bit 12 */
	"track offset option",			/* bit 13 */
	"format speed tolerance gap reqd",	/* bit 14 */
	"ATAPI"					/* bit 14 */
};
#endif

/* word 1: number of logical cylinders */
#define LCYLS_MAX		0x3fff /* maximum allowable value */

/* word 2: specific configuration
 * (a) require SET FEATURES to spin-up
 * (b) require spin-up to fully reply to IDENTIFY DEVICE
 */
#define STBY_NID_VAL		0x37c8  /*     (a) and     (b) */
#define STBY_ID_VAL		0x738c	/*     (a) and not (b) */
#define PWRD_NID_VAL 		0x8c73	/* not (a) and     (b) */
#define PWRD_ID_VAL		0xc837	/* not (a) and not (b) */

/* words 47 & 59: sector_xfer_max & sector_xfer_cur */
#define SECTOR_XFER		0x00ff  /* sectors xfered on r/w multiple cmds*/
#define MULTIPLE_SETTING_VALID  0x0100  /* 1=multiple sector setting is valid */

/* word 49: capabilities 0 */
#define STD_STBY  	  	0x2000  /* 1=standard values supported (ATA);
					   0=vendor specific values */
#define IORDY_SUP		0x0800  /* 1=support; 0=may be supported */
#define IORDY_OFF		0x0400  /* 1=may be disabled */
#define LBA_SUP			0x0200  /* 1=Logical Block Address support */
#define DMA_SUP			0x0100  /* 1=Direct Memory Access support */
#define DMA_IL_SUP		0x8000  /* 1=interleaved DMA support (ATAPI) */
#define CMD_Q_SUP		0x4000  /* 1=command queuing support (ATAPI) */
#define OVLP_SUP		0x2000  /* 1=overlap operation support (ATAPI) */
#define SWRST_REQ		0x1000  /* 1=ATA SW reset required (ATAPI, obsolete */

/* word 50: capabilities 1 */
#define MIN_STANDBY_TIMER	0x0001  /* 1=device specific standby timer value minimum */

/* words 51 & 52: PIO & DMA cycle times */
#define MODE			0xff00  /* the mode is in the MSBs */

/* word 53: whats_valid */
#define OK_W88     	   	0x0004	/* the ultra_dma info is valid */
#define OK_W64_70		0x0002  /* see above for word descriptions */
#define OK_W54_58		0x0001  /* current cyl, head, sector, cap. info valid */

/*word 63,88: dma_mode, ultra_dma_mode*/
#define MODE_MAX		7	/* bit definitions force udma <=7 (when
					 * udma >=8 comes out it'll have to be
					 * defined in a new dma_mode word!) */

/* word 64: PIO transfer modes */
#define PIO_SUP			0x00ff  /* only bits 0 & 1 are used so far,  */
#define PIO_MODE_MAX		8       /* but all 8 bits are defined        */

/* word 75: queue_depth */
#define DEPTH_BITS		0x001f  /* bits used for queue depth */

/* words 80-81: version numbers */
/* NOVAL_0 or  NOVAL_1 means device does not report version */

/* word 81: minor version number */
#define MINOR_MAX		0x1C
#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static const char * const minor_str[] = {				/* word 81 value: */
	"device does not report version",		/* 0x0000	*/
	"ATA-1 X3T9.2 781D prior to revision 4",	/* 0x0001	*/
	"ATA-1 published, ANSI X3.221-1994",		/* 0x0002	*/
	"ATA-1 X3T9.2 781D revision 4",			/* 0x0003	*/
	"ATA-2 published, ANSI X3.279-1996",		/* 0x0004	*/
	"ATA-2 X3T10 948D prior to revision 2k",	/* 0x0005	*/
	"ATA-3 X3T10 2008D revision 1",			/* 0x0006	*/
	"ATA-2 X3T10 948D revision 2k",			/* 0x0007	*/
	"ATA-3 X3T10 2008D revision 0",			/* 0x0008	*/
	"ATA-2 X3T10 948D revision 3",			/* 0x0009	*/
	"ATA-3 published, ANSI X3.298-199x",		/* 0x000a	*/
	"ATA-3 X3T10 2008D revision 6",			/* 0x000b	*/
	"ATA-3 X3T13 2008D revision 7 and 7a",		/* 0x000c	*/
	"ATA/ATAPI-4 X3T13 1153D revision 6",		/* 0x000d	*/
	"ATA/ATAPI-4 T13 1153D revision 13",		/* 0x000e	*/
	"ATA/ATAPI-4 X3T13 1153D revision 7",		/* 0x000f	*/
	"ATA/ATAPI-4 T13 1153D revision 18",		/* 0x0010	*/
	"ATA/ATAPI-4 T13 1153D revision 15",		/* 0x0011	*/
	"ATA/ATAPI-4 published, ANSI NCITS 317-1998",	/* 0x0012	*/
	"ATA/ATAPI-5 T13 1321D revision 3",
	"ATA/ATAPI-4 T13 1153D revision 14",		/* 0x0014	*/
	"ATA/ATAPI-5 T13 1321D revision 1",		/* 0x0015	*/
	"ATA/ATAPI-5 published, ANSI NCITS 340-2000",	/* 0x0016	*/
	"ATA/ATAPI-4 T13 1153D revision 17",		/* 0x0017	*/
	"ATA/ATAPI-6 T13 1410D revision 0",		/* 0x0018	*/
	"ATA/ATAPI-6 T13 1410D revision 3a",		/* 0x0019	*/
	"Reserved",					/* 0x001a	*/
	"ATA/ATAPI-6 T13 1410D revision 2",		/* 0x001b	*/
	"ATA/ATAPI-6 T13 1410D revision 1",		/* 0x001c	*/
	"reserved"					/* 0x001d	*/
	"reserved"					/* 0x001e	*/
	"reserved"					/* 0x001f-0xfffe*/
};
#endif
static const char actual_ver[] = {
			/* word 81 value: */
	0,		/* 0x0000	WARNING: 	*/
	1,		/* 0x0001	WARNING: 	*/
	1,		/* 0x0002	WARNING: 	*/
	1,		/* 0x0003	WARNING: 	*/
	2,		/* 0x0004	WARNING:   This array 		*/
	2,		/* 0x0005	WARNING:   corresponds 		*/
	3,		/* 0x0006	WARNING:   *exactly*		*/
	2,		/* 0x0007	WARNING:   to the ATA/		*/
	3,		/* 0x0008	WARNING:   ATAPI version	*/
	2,		/* 0x0009	WARNING:   listed in	 	*/
	3,		/* 0x000a	WARNING:   the 		 	*/
	3,		/* 0x000b	WARNING:   minor_str 		*/
	3,		/* 0x000c	WARNING:   array		*/
	4,		/* 0x000d	WARNING:   above.		*/
	4,		/* 0x000e	WARNING:  			*/
	4,		/* 0x000f	WARNING:   if you change 	*/
	4,		/* 0x0010	WARNING:   that one,      	*/
	4,		/* 0x0011	WARNING:   change this one	*/
	4,		/* 0x0012	WARNING:   too!!!        	*/
	5,		/* 0x0013	WARNING:	*/
	4,		/* 0x0014	WARNING:	*/
	5,		/* 0x0015	WARNING:	*/
	5,		/* 0x0016	WARNING:	*/
	4,		/* 0x0017	WARNING:	*/
	6,		/* 0x0018	WARNING:	*/
	6,		/* 0x0019	WARNING:	*/
	0,		/* 0x001a	WARNING:	*/
	6,		/* 0x001b	WARNING:	*/
	6,		/* 0x001c	WARNING:	*/
	0		/* 0x001d-0xfffe    		*/
};

/* words 82-84: cmds/feats supported */
#define CMDS_W82		0x77ff  /* word 82: defined command locations*/
#define CMDS_W83		0x3fff  /* word 83: defined command locations*/
#define CMDS_W84		0x002f  /* word 83: defined command locations*/
#define SUPPORT_48_BIT		0x0400
#define NUM_CMD_FEAT_STR	48

#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static const char * const cmd_feat_str[] = {
	"",					/* word 82 bit 15: obsolete  */
	"NOP cmd",				/* word 82 bit 14 */
	"READ BUFFER cmd",			/* word 82 bit 13 */
	"WRITE BUFFER cmd",			/* word 82 bit 12 */
	"",					/* word 82 bit 11: obsolete  */
	"Host Protected Area feature set",	/* word 82 bit 10 */
	"DEVICE RESET cmd",			/* word 82 bit  9 */
	"SERVICE interrupt",			/* word 82 bit  8 */
	"Release interrupt",			/* word 82 bit  7 */
	"Look-ahead",				/* word 82 bit  6 */
	"Write cache",				/* word 82 bit  5 */
	"PACKET command feature set",		/* word 82 bit  4 */
	"Power Management feature set",		/* word 82 bit  3 */
	"Removable Media feature set",		/* word 82 bit  2 */
	"Security Mode feature set",		/* word 82 bit  1 */
	"SMART feature set",			/* word 82 bit  0 */
						/* --------------*/
	"",					/* word 83 bit 15: !valid bit */
	"",					/* word 83 bit 14:  valid bit */
	"FLUSH CACHE EXT command",		/* word 83 bit 13 */
	"Mandatory FLUSH CACHE command ",	/* word 83 bit 12 */
	"Device Configuration Overlay feature set ",
	"48-bit Address feature set ",		/* word 83 bit 10 */
	"",
	"SET MAX security extension",		/* word 83 bit  8 */
	"Address Offset Reserved Area Boot",	/* word 83 bit  7 */
	"SET FEATURES subcommand required to spinup after power up",
	"Power-Up In Standby feature set",	/* word 83 bit  5 */
	"Removable Media Status Notification feature set",
	"Adv. Power Management feature set",/* word 83 bit  3 */
	"CFA feature set",			/* word 83 bit  2 */
	"READ/WRITE DMA QUEUED",		/* word 83 bit  1 */
	"DOWNLOAD MICROCODE cmd", 		/* word 83 bit  0 */
						/* --------------*/
	"",					/* word 84 bit 15: !valid bit */
	"",					/* word 84 bit 14:  valid bit */
	"",					/* word 84 bit 13:  reserved */
	"",					/* word 84 bit 12:  reserved */
	"",					/* word 84 bit 11:  reserved */
	"",					/* word 84 bit 10:  reserved */
	"",					/* word 84 bit  9:  reserved */
	"",					/* word 84 bit  8:  reserved */
	"",					/* word 84 bit  7:  reserved */
	"",					/* word 84 bit  6:  reserved */
	"General Purpose Logging feature set",	/* word 84 bit  5 */
	"",					/* word 84 bit  4:  reserved */
	"Media Card Pass Through Command feature set ",
	"Media serial number ",			/* word 84 bit  2 */
	"SMART self-test ",			/* word 84 bit  1 */
	"SMART error logging "			/* word 84 bit  0 */
};
#endif


/* words 85-87: cmds/feats enabled */
/* use cmd_feat_str[] to display what commands and features have
 * been enabled with words 85-87
 */

/* words 89, 90, SECU ERASE TIME */
#define ERASE_BITS		0x00ff

/* word 92: master password revision */
/* NOVAL_0 or  NOVAL_1 means no support for master password revision */

/* word 93: hw reset result */
#define CBLID			0x2000  /* CBLID status */
#define RST0			0x0001  /* 1=reset to device #0 */
#define DEV_DET			0x0006  /* how device num determined */
#define JUMPER_VAL		0x0002  /* device num determined by jumper */
#define CSEL_VAL		0x0004  /* device num determined by CSEL_VAL */

/* word 127: removable media status notification feature set support */
#define RM_STAT_BITS 		0x0003
#define RM_STAT_SUP		0x0001

/* word 128: security */
#define SECU_ENABLED	0x0002
#define SECU_LEVEL		0x0010
#define NUM_SECU_STR	6
#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static const char * const secu_str[] = {
	"supported",			/* word 128, bit 0 */
	"enabled",			/* word 128, bit 1 */
	"locked",			/* word 128, bit 2 */
	"frozen",			/* word 128, bit 3 */
	"expired: security count",	/* word 128, bit 4 */
	"supported: enhanced erase"	/* word 128, bit 5 */
};
#endif

/* word 160: CFA power mode */
#define VALID_W160		0x8000  /* 1=word valid */
#define PWR_MODE_REQ		0x2000  /* 1=CFA power mode req'd by some cmds*/
#define PWR_MODE_OFF		0x1000  /* 1=CFA power moded disabled */
#define MAX_AMPS		0x0fff  /* value = max current in ma */

/* word 255: integrity */
#define SIG			0x00ff  /* signature location */
#define SIG_VAL			0x00A5  /* signature value */

#define VERSION "v5.4"

#define TIMING_MB		64
#define TIMING_BUF_MB		1
#define TIMING_BUF_BYTES	(TIMING_BUF_MB * 1024 * 1024)
#define TIMING_BUF_COUNT	(timing_MB / TIMING_BUF_MB)
#define BUFCACHE_FACTOR		2

#undef DO_FLUSHCACHE		/* under construction: force cache flush on -W0 */

/* Busybox messages and functions */

static const char bb_msg_shared_mem[] = "could not %s sharedmem buf";
static const char bb_msg_op_not_supp[] = " operation not supported on %s disks";

static void bb_ioctl(int fd, int request, void *argp, const char *string)
{
	if (ioctl (fd, request, argp) != 0)
		bb_perror_msg(" %s", string);
}

static void if_printf(unsigned long i, char *fmt, ... )
{
		va_list ap;
		va_start(ap, fmt);
		if (i)
			vprintf(fmt, ap);
		va_end(ap);
}

static void on_off (unsigned int value);

static void if_printf_on_off(unsigned long get_arg,const char *fmt, unsigned long arg)
{
	if (get_arg)
	{
		printf(fmt, arg);
		on_off(arg);
	}
}

static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
							 const char * fmt)
{
	if (ioctl (fd, request, &argp) != 0)
		bb_perror_msg(" %s", string);
	else
	{
		printf(fmt, (unsigned long) argp);
		on_off((unsigned long) argp);
	}
}

#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
{
		va_list ap;
		va_start(ap, fmt2);
		if (i)
			vprintf(fmt1, ap);
		else
			vprintf(fmt2, ap);
		va_end(ap);
}

static void print_ascii(uint16_t *p, uint8_t length);

static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
{
	if(val[i])
	{
		printf("\t%-20s",string);
		print_ascii(&val[i], n);
	}
}

static void if_strcat(unsigned long test, char *modes, char *string)
{
	if (test)
		strcat(modes,string);
}
#endif

static void sync_and_sleep(int i)
{
	sync();
	sleep(i);
}

static uint16_t check_if_min_and_set_val(uint16_t a, uint16_t b)
{
	if( a < b)
		a = b;
	return a;
}

static uint16_t check_if_maj_and_set_val(uint16_t a, uint16_t b)
{
	if( a > b)
		a = b;
	return a;
}

static unsigned long int set_flag(char *p, char max)
{
	if (*p >= '0' && *p <=  max )
		return 1;
	return 0;
}

/* end of  busybox specific stuff */

#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
{
	uint16_t ii;
	uint8_t err_dma = 0;

	for(ii = 0; ii <= MODE_MAX; ii++)
	{
		if(mode_sel & 0x0001)
		{
			printf("*%cdma%u ",cc,ii);
			if(*have_mode)
				err_dma = 1;
			*have_mode = 1;
		}
		else if(mode_sup & 0x0001)
			printf("%cdma%u ",cc,ii);

		mode_sup >>=1;
		mode_sel >>=1;
	}
	return err_dma;
}

static void print_ascii(uint16_t *p, uint8_t length) {
	uint8_t ii;
	char cl;

	/* find first non-space & print it */
	for(ii = 0; ii< length; ii++)
	{
		if(((char) 0x00ff&((*p)>>8)) != ' ')
			break;
		if((cl = (char) 0x00ff&(*p)) != ' ')
		{
			if_printf((cl != '\0'),"%c",cl);
			p++;
			ii++;
			break;
		}
		p++;
	}
	/* print the rest */
	for(; ii< length; ii++)
	{
		if(!(*p))
			break; /* some older devices have NULLs */
		printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
		p++;
	}
	printf("\n");
}

/* identify() is the only extern function used across two source files.  The
   others, though, were declared in hdparm.c with global scope; since other
   functions in that file have static (file) scope, I assume the difference is
   intentional. */
static void identify (uint16_t *id_supplied, const char *devname)
{

	char *id_file = NULL;
	FILE *fl;
	uint16_t val[256], ii, jj, kk;
	uint16_t like_std = 1, std = 0, min_std = 0xffff;
	uint16_t dev = NO_DEV, eqpt = NO_DEV;
	uint8_t  have_mode = 0, err_dma = 0;
	uint8_t  chksum = 0;
	uint32_t ll, mm, nn, oo;
	__u64 bbbig; /* (:) */

	if (id_supplied)
	{
#if __BYTE_ORDER == __BIG_ENDIAN
		swab(id_supplied, val, sizeof(val));
#else
		memcpy(val, id_supplied, sizeof(val));
#endif
	}
	else
	{
		id_file = xcalloc(1, strlen(devname)+22);
		sprintf(id_file, "/proc/ide/%s/identify", devname);
		/* open the file, read in all the info and close it */
		if (id_file == NULL)
			fl = stdin;
		else
			fl = bb_xfopen(id_file, "r");

		/* calculate checksum over all bytes */
		for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
		{
			unsigned int scratch;
			if(1 != fscanf(fl,"%04x",&scratch))
				break;
			val[ii] = (uint16_t)scratch;
			chksum += val[ii] + (val[ii] >> 8);
		}
		fclose(fl);
		/*bb_fclose_nonstdin(fl);*/
		if(ii < (INTEGRITY+1))
			bb_error_msg_and_die("Input file wrong format or length");
	}
	chksum &= 0xff;

	/* check if we recognise the device type */
	printf("\n");
	if(!(val[GEN_CONFIG] & NOT_ATA))
	{
		dev = ATA_DEV;
		printf("ATA device, with ");
	}
	else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
	{
		dev = ATA_DEV;
		like_std = 4;
		printf("CompactFlash ATA device, with ");
	}
	else if(!(val[GEN_CONFIG] & NOT_ATAPI))
	{
		dev = ATAPI_DEV;
		eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
		printf("ATAPI %s, with ", pkt_str[eqpt]);
		like_std = 3;
	}
	else
		/*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
		bb_error_msg_and_die("Unknown device type");

	if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
	printf("removable media\n");

	/* Info from the specific configuration word says whether or not the
	 * ID command completed correctly.  It is only defined, however in
	 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
	 * standards.  Since the values allowed for this word are extremely
	 * specific, it should be safe to check it now, even though we don't
	 * know yet what standard this device is using.
	 */
	if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
	   (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
	{
		like_std = 5;
		if_printf(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)),
					"powers-up in standby; SET FEATURES subcmd spins-up.\n");
		if_printf((((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)),
					"\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
	}

	/* output the model and serial numbers and the fw revision */
	xprint_ascii(val, START_MODEL,  "Model Number:",        LENGTH_MODEL);
	xprint_ascii(val, START_SERIAL, "Serial Number:",       LENGTH_SERIAL);
	xprint_ascii(val, START_FW_REV, "Firmware Revision:",   LENGTH_FW_REV);
	xprint_ascii(val, START_MEDIA,  "Media Serial Num:",    LENGTH_MEDIA);
	xprint_ascii(val, START_MANUF,  "Media Manufacturer:",  LENGTH_MANUF);

	/* major & minor standards version number (Note: these words were not
	 * defined until ATA-3 & the CDROM std uses different words.) */
	printf("Standards:");
	if(eqpt != CDROM)
	{
		if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
		{
			like_std=check_if_min_and_set_val(like_std, 3);
			std = actual_ver[val[MINOR]];
			if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);

		}
		/* looks like when they up-issue the std, they obsolete one;
		 * thus, only the newest 4 issues need be supported. (That's
		 * what "kk" and "min_std" are all about.) */
		if(val[MAJOR] && (val[MAJOR] !=NOVAL_1))
		{
			printf("\n\tSupported: ");
			jj = val[MAJOR] << 1;
			kk = like_std >4 ? like_std-4: 0;
			for(ii = 14; (ii >0)&&(ii>kk); ii--)
			{
				if(jj & 0x8000)
				{
					printf("%u ", ii);
					if(like_std < ii)
					{
						like_std = ii;
						kk = like_std >4 ? like_std-4: 0;
					}
					min_std=check_if_maj_and_set_val(min_std, ii);
				}
				jj <<= 1;
			}
			like_std=check_if_min_and_set_val(like_std, 3);
		}
		/* Figure out what standard the device is using if it hasn't told
		 * us.  If we know the std, check if the device is using any of
		 * the words from the next level up.  It happens.
		 */
		like_std=check_if_min_and_set_val(like_std, std);

		if(((std == 5) || (!std && (like_std < 6))) &&
			((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
			((	val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
			(((	val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
			(	val[CMDS_SUPP_2] & CMDS_W84) ) ) )
		{
			like_std = 6;
		}
		else if(((std == 4) || (!std && (like_std < 5))) &&
			((((val[INTEGRITY] 	& SIG) == SIG_VAL) && !chksum) ||
			((	val[HWRST_RSLT] & VALID) == VALID_VAL) ||
			(((	val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
			((	val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
		{
			like_std = 5;
		}
		else if(((std == 3) || (!std && (like_std < 4))) &&
				((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
				(((	val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
				((	val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
				((	val[CAPAB_1] & VALID) == VALID_VAL) ||
				((	val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
				((	val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
		{
			like_std = 4;
		}
		else if(((std == 2) || (!std && (like_std < 3))) &&
			   ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
		{
			like_std = 3;
		}
		else if(((std == 1) || (!std && (like_std < 2))) &&
				((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
				(val[WHATS_VALID] & OK_W64_70)) )
		{
			like_std = 2;
		}
		if(!std)
			printf("\n\tLikely used: %u\n",like_std);
		else if(like_std > std)
		 	printf("& some of %u\n",like_std);
		else
			printf("\n");
	}
	else
	{
		/* TBD: do CDROM stuff more thoroughly.  For now... */
	  	kk = 0;
		if(val[CDR_MINOR] == 9)
		{
			kk = 1;
			printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
		}
		if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
		{
			kk = 1;
			printf("\n\tSupported: CD-ROM ATAPI");
			jj = val[CDR_MAJOR] >> 1;
			for(ii = 1; ii <15; ii++)
			{
				if_printf((jj & 0x0001),"-%u ", ii);
				jj >>= 1;
			}
		}
		if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
		/* the cdrom stuff is more like ATA-2 than anything else, so: */
		like_std = 2;
	}

	if(min_std == 0xffff)
		min_std = like_std > 4 ? like_std - 3 : 1;

	printf("Configuration:\n");
	/* more info from the general configuration word */
	if((eqpt != CDROM) && (like_std == 1))
	{
		jj = val[GEN_CONFIG] >> 1;
		for(ii = 1; ii < 15; ii++)
		{
			if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
			jj >>=1;
		}
	}
	if(dev == ATAPI_DEV)
	{
		printf("\tDRQ response: "); /* Data Request (DRQ) */
		switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
		{
			case DRQ_3MS_VAL :
				printf("3ms.\n");
				break;
			case DRQ_INTR_VAL :
				printf("<=10ms with INTRQ\n");
				break;
			case DRQ_50US_VAL :
				printf("50us.\n");
				break;
			default :
				printf("unknown.\n");
				break;
		}
		printf("\tPacket size: ");
		switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
		{
			case PKT_SIZE_12_VAL :
				printf("12 bytes\n");
				break;
			case PKT_SIZE_16_VAL :
				printf("16 bytes\n");
				break;
			default :
				printf("Unknown\n");
				break;
		}
	}
	else
	{
		/* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
		ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
		mm = 0; bbbig = 0;
		if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
		  	printf("\tCHS addressing not supported\n");
		else
		{
			jj = val[WHATS_VALID] & OK_W54_58;
			printf("\tLogical\t\tmax\tcurrent\n\tcylinders\t%u\t%u\n\theads\t\t%u\t%u\n\tsectors/track\t%u\t%u\n\t--\n",
					val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);

			if_printf(((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])),
						"\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);

			if(jj)
			{
				mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
				if(like_std < 3)
				{
					 /* check Endian of capacity bytes */
					nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
					oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
					if(abs(mm - nn) > abs(oo - nn))
						mm = oo;
				}
				printf("\tCHS current addressable sectors:%11u\n",mm);
			}
		}
		/* LBA addressing */
		printf("\tLBA    user addressable sectors:%11u\n",ll);
		if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
		     (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
		{
			bbbig = (__u64)val[LBA_64_MSB] 	<< 48 |
			        (__u64)val[LBA_48_MSB] 	<< 32 |
			        (__u64)val[LBA_MID] 	<< 16 |
					val[LBA_LSB] ;
			printf("\tLBA48  user addressable sectors:%11llu\n",bbbig);
		}

		if (!bbbig)
			bbbig = (__u64)(ll>mm ? ll : mm); /* # 512 byte blocks */
		printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
		bbbig = (bbbig<<9)/1000000;
		printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);

		if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);

	}

	/* hw support of commands (capabilities) */
	printf("Capabilities:\n\t");

	if(dev == ATAPI_DEV)
	{
		if(eqpt != CDROM)
			if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");

		if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
	}
	if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");

	if(like_std != 1)
	{
		printf("IORDY");
		if_printf((!(val[CAPAB_0] & IORDY_SUP)),"(may be)");
		if_else_printf((val[CAPAB_0] & IORDY_OFF),"(can","(cannot");
		printf(" be disabled)\n");
	}
	else
		printf("no IORDY\n");

	if((like_std == 1) && val[BUF_TYPE])
	{
		kk = val[BUF_TYPE];
		printf("\tBuffer type: %04x: ",kk);
		if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
		if_printf((kk > 2)," with read caching ability");
		printf("\n");
	}
	jj = 0;
	if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
	{
		printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
		jj = 1;
	}
	if((min_std < 4) && (val[RW_LONG]))
	{
		printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
		jj = 1;
	}
	if((eqpt != CDROM) && (like_std > 3))
	{
		printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
		jj = 1;
	}
	if_printf(jj,"\n");

	if(dev == ATA_DEV)
	{
		if(like_std == 1)
			printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
		else
		{
			printf("\tStandby timer values: spec'd by ");
			if_else_printf((val[CAPAB_0] & STD_STBY),"Standard","Vendor");
			if((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
				printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
			else
			  printf("\n");
		}
		printf("\tR/W multiple sector transfer: ");
		if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
			printf("not supported\n");
		else
		{
			printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
			if_else_printf((val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID),
							"%u\n","?\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
		}
		if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
		{
			/* We print out elsewhere whether the APM feature is enabled or
			   not.  If it's not enabled, let's not repeat the info; just print
			   nothing here. */
			printf("\tAdvancedPM level: ");
			if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
			{
				uint8_t apm_level = val[ADV_PWR] & 0x00FF;
				printf("%u (0x%x)\n", apm_level, apm_level);
			}
			else
				printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
		}
		if(like_std > 5)
		{
			if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
									(val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
		}
	}
	else
	{
		 /* ATAPI */
		if(eqpt != CDROM)
			if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");

		if(val[PKT_REL] || val[SVC_NBSY])
		{
			printf("\tOverlap support:");
			if_printf(val[PKT_REL]," %uus to release bus.",val[PKT_REL]);
			if_printf(val[SVC_NBSY]," %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
			printf("\n");
		}
	}

	/* DMA stuff. Check that only one DMA mode is selected. */
	printf("\tDMA: ");
	if(!(val[CAPAB_0] & DMA_SUP))
		printf("not supported\n");
	else
	{
		if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
					" sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
		if(val[SINGLE_DMA])
		{
			jj = val[SINGLE_DMA];
			kk = val[SINGLE_DMA] >> 8;
			err_dma += mode_loop(jj,kk,'s',&have_mode);
		}
		if(val[MULTI_DMA])
		{
			jj = val[MULTI_DMA];
			kk = val[MULTI_DMA] >> 8;
			err_dma += mode_loop(jj,kk,'m',&have_mode);
		}
		if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
		{
			jj = val[ULTRA_DMA];
			kk = val[ULTRA_DMA] >> 8;
			err_dma += mode_loop(jj,kk,'u',&have_mode);
		}
		if_printf((err_dma || !have_mode),"(?)");
		printf("\n");

		if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
					"\t     Interleaved DMA support\n");

		if((val[WHATS_VALID] & OK_W64_70) &&
		   (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
		{
			printf("\t     Cycle time:");
			if_printf(val[DMA_TIME_MIN]," min=%uns",val[DMA_TIME_MIN]);
			if_printf(val[DMA_TIME_NORM]," recommended=%uns",val[DMA_TIME_NORM]);
			printf("\n");
		}
	}

	/* Programmed IO stuff */
	printf("\tPIO: ");
        /* If a drive supports mode n (e.g. 3), it also supports all modes less
	 * than n (e.g. 3, 2, 1 and 0).  Print all the modes. */
	if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
	{
		jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
		for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
		{
			if_printf((jj & 0x0001),"pio%d ",ii);
			jj >>=1;
		}
		printf("\n");
	}
	else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
	{
		for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
			printf("pio%d ",ii);
		printf("\n");
	}
	else
		printf("unknown\n");

	if(val[WHATS_VALID] & OK_W64_70)
	{
		if(val[PIO_NO_FLOW] || val[PIO_FLOW])
		{
			printf("\t     Cycle time:");
			if_printf(val[PIO_NO_FLOW]," no flow control=%uns", val[PIO_NO_FLOW]);
			if_printf(val[PIO_FLOW],"  IORDY flow control=%uns", val[PIO_FLOW]);
			printf("\n");
		}
	}

	if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
	{
		printf("Commands/features:\n\tEnabled\tSupported:\n");
		jj = val[CMDS_SUPP_0];
		kk = val[CMDS_EN_0];
		for(ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
		{
			if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
			{
				if_else_printf((kk & 0x8000),"\t   *","\t");
				printf("\t%s\n",cmd_feat_str[ii]);
			}
			jj <<=1; kk<<=1;
			if(ii%16 == 15)
			{
				jj = val[CMDS_SUPP_0+1+(ii/16)];
				kk = val[CMDS_EN_0+1+(ii/16)];
			}
			if(ii == 31)
			{
				if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
					ii +=16;
			}
		}
	}
	if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
				"\tRemovable Media Status Notification feature set supported\n");


	/* security */
	if((eqpt != CDROM) && (like_std > 3) &&
	   (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
	{
		printf("Security: \n");
		if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
					"\tMaster password revision code = %u\n",val[PSWD_CODE]);
		jj = val[SECU_STATUS];
		if(jj)
		{
			for(ii = 0; ii < NUM_SECU_STR; ii++)
			{
				if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
				jj >>=1;
			}
			if(val[SECU_STATUS] & SECU_ENABLED)
			{
				printf("\tSecurity level ");
				if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
			}
		}
		jj =  val[ERASE_TIME]     & ERASE_BITS;
		kk =  val[ENH_ERASE_TIME] & ERASE_BITS;
		if(jj || kk)
		{
			printf("\t");
			if_printf(jj,"%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
			if_printf(kk,"%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
			printf("\n");
		}
	}

	/* reset result */
	if((val[HWRST_RSLT] & VALID) == VALID_VAL)
	{
		printf("HW reset results:\n");
		if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");

		if(val[HWRST_RSLT] & RST0)
		{
			printf("\tDevice num = 0");
			jj = val[HWRST_RSLT];
		}
		else
		{
			printf("\tDevice num = 1");
			jj = val[HWRST_RSLT] >> 8;
		}

		if((jj & DEV_DET) == JUMPER_VAL)
			printf(" determined by the jumper");
		else if((jj & DEV_DET) == CSEL_VAL)
			printf(" determined by CSEL");
		printf("\n");
	}

	/* more stuff from std 5 */
	if((like_std > 4) && (eqpt != CDROM))
	{
		if(val[CFA_PWR_MODE] & VALID_W160)
		{
			printf("CFA power mode 1:\n\t");
			if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");

			if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
			printf("\n");

			if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
		}
		if((val[INTEGRITY] & SIG) == SIG_VAL)
		{
			printf("Checksum: ");
			if_printf(chksum,"in");
			printf("correct\n");
		}
	}

	exit(0);
}
#endif

static int verbose, get_identity, get_geom, noisy = 1, quiet;
static int flagcount, do_flush, is_scsi_hd, is_xt_hd;
static int do_ctimings, do_timings;

static unsigned long set_readahead, get_readahead, Xreadahead;
static unsigned long set_readonly, get_readonly, readonly;
static unsigned long set_unmask, get_unmask, unmask;
static unsigned long set_mult, get_mult, mult;
#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
static unsigned long set_dma, get_dma, dma;
#endif
static unsigned long set_dma_q, get_dma_q, dma_q;
static unsigned long set_nowerr, get_nowerr, nowerr;
static unsigned long set_keep, get_keep, keep;
static unsigned long set_io32bit, get_io32bit, io32bit;
static unsigned long set_piomode, noisy_piomode;
static int piomode;
#ifdef HDIO_DRIVE_CMD
static unsigned long set_dkeep, get_dkeep, dkeep;
static unsigned long set_standby, get_standby, standby_requested;
static unsigned long set_xfermode, get_xfermode;
static int xfermode_requested;
static unsigned long set_lookahead, get_lookahead, lookahead;
static unsigned long set_prefetch, get_prefetch, prefetch;
static unsigned long set_defects, get_defects, defects;
static unsigned long set_wcache, get_wcache, wcache;
static unsigned long set_doorlock, get_doorlock, doorlock;
static unsigned long set_seagate, get_seagate;
static unsigned long set_standbynow, get_standbynow;
static unsigned long set_sleepnow, get_sleepnow;
static unsigned long get_powermode;
static unsigned long set_apmmode, get_apmmode, apmmode;
#endif
#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static int get_IDentity;
#endif
#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
static int	unregister_hwif;
static int	hwif;
#endif
#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
static int	scan_hwif;
static int	hwif_data;
static int	hwif_ctrl;
static int	hwif_irq;
#endif
#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
static int	set_busstate, get_busstate, busstate;
#endif
static int	reread_partn;

#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
static int	perform_reset;
#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
static int	perform_tristate,	tristate;
#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */

// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
// then the HDIO_GET_IDENTITY only returned 142 bytes.
// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
// and HDIO_GET_IDENTITY returns 512 bytes.  But the latest
// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
// (which they should, but they should just return -EINVAL).
//
// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
// On a really old system, it will not, and we will be confused.
// Too bad, really.

#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static const char * const cfg_str[] =
{	"",	     " HardSect",   " SoftSect",  " NotMFM",
	" HdSw>15uSec", " SpinMotCtl", " Fixed",     " Removeable",
	" DTR<=5Mbs",   " DTR>5Mbs",   " DTR>10Mbs", " RotSpdTol>.5%",
	" dStbOff",     " TrkOff",     " FmtGapReq", " nonMagnetic"
};

static const char * const BuffType[]	= {"unknown", "1Sect", "DualPort", "DualPortCache"};

static void dump_identity (const struct hd_driveid *id)
{
	int i;
	char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
	const unsigned short int *id_regs= (const void*) id;
	unsigned long capacity;

	printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
				id->model, id->fw_rev, id->serial_no);
	for (i=0; i<=15; i++)
		if_printf((id->config & (1<<i)),"%s", cfg_str[i]);

	printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
				id->cyls, id->heads, id->sectors, id->track_bytes,
				id->sector_bytes, id->ecc_bytes);

	if (id->buf_type > 3)
		printf("%s%u", " BuffType=", id->buf_type);
	else
		printf("%s%s", " BuffType=", BuffType[id->buf_type]);

	printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
	if (id->max_multsect)
	{
		printf(", MultSect=");
		if (!(id->multsect_valid&1))
			printf("?%u?", id->multsect);
		else if (id->multsect)
			printf("%u", id->multsect);
		else
			printf("off");
	}
	printf("\n");
	if (id->tPIO <= 5)
	{
		strcat(pmodes, "pio0 ");
		if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
		if_strcat((id->tPIO >= 2), pmodes, "pio2 ");

	}
	if_printf((!(id->field_valid&1))," (maybe):");
#if __BYTE_ORDER == __BIG_ENDIAN
	capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
#else
	capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
#endif
	printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
								 					id->cur_sectors, capacity ,
								 			((id->capability&2)==0)?"no":"yes");

	if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);

	if (id->capability&1)
	{
		if (id->dma_1word | id->dma_mword)
		{
			if_strcat((id->dma_1word & 0x100),	dmodes, "*");
			if_strcat((id->dma_1word & 1),		dmodes, "sdma0 ");
			if_strcat((id->dma_1word & 0x200),	dmodes, "*");
			if_strcat((id->dma_1word & 2),		dmodes, "sdma1 ");
			if_strcat((id->dma_1word & 0x400),	dmodes, "*");
			if_strcat((id->dma_1word & 4), 		dmodes, "sdma2 ");
			if_strcat((id->dma_1word & 0xf800),	dmodes, "*");
			if_strcat((id->dma_1word & 0xf8),	dmodes, "sdma? ");
			if_strcat((id->dma_mword & 0x100),	dmodes, "*");
			if_strcat((id->dma_mword & 1),		dmodes, "mdma0 ");
			if_strcat((id->dma_mword & 0x200),	dmodes, "*");
			if_strcat((id->dma_mword & 2),		dmodes, "mdma1 ");
			if_strcat((id->dma_mword & 0x400),	dmodes, "*");
			if_strcat((id->dma_mword & 4),		dmodes, "mdma2 ");
			if_strcat((id->dma_mword & 0xf800),	dmodes, "*");
			if_strcat((id->dma_mword & 0xf8),	dmodes, "mdma? ");
		}
	}
	printf("\n IORDY=");
	if (id->capability&8)
		printf((id->capability&4) ? "on/off" : "yes");
	else
		printf("no");

	if ((id->capability&8) || (id->field_valid&2))
	{
		if (id->field_valid&2)
		{
			printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
			if_strcat((id->eide_pio_modes & 1), pmodes, "pio3 ");
			if_strcat((id->eide_pio_modes & 2), pmodes, "pio4 ");
			if_strcat((id->eide_pio_modes &~3), pmodes, "pio? ");
		}
		if (id->field_valid&4)
		{
			if_strcat((id->dma_ultra & 0x100),umodes,"*");
			if_strcat((id->dma_ultra & 0x001),umodes,"udma0 ");
			if_strcat((id->dma_ultra & 0x200),umodes,"*");
			if_strcat((id->dma_ultra & 0x002),umodes,"udma1 ");
			if_strcat((id->dma_ultra & 0x400),umodes,"*");
			if_strcat((id->dma_ultra & 0x004),umodes,"udma2 ");
#ifdef __NEW_HD_DRIVE_ID
			if (id->hw_config & 0x2000)
			{
#else /* !__NEW_HD_DRIVE_ID */
			if (id->word93 & 0x2000)
			{
#endif /* __NEW_HD_DRIVE_ID */
				if_strcat((id->dma_ultra & 0x0800),umodes,"*");
				if_strcat((id->dma_ultra & 0x0008),umodes,"udma3 ");
				if_strcat((id->dma_ultra & 0x1000),umodes,"*");
				if_strcat((id->dma_ultra & 0x0010),umodes,"udma4 ");
				if_strcat((id->dma_ultra & 0x2000),umodes,"*");
				if_strcat((id->dma_ultra & 0x0020),umodes,"udma5 ");
				if_strcat((id->dma_ultra & 0x4000),umodes,"*");
				if_strcat((id->dma_ultra & 0x0040),umodes,"udma6 ");
				if_strcat((id->dma_ultra & 0x8000),umodes,"*");
				if_strcat((id->dma_ultra & 0x0080),umodes,"udma7 ");
			}
		}
	}
	if_printf(((id->capability&1) && (id->field_valid&2)),
				", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
	printf("\n PIO modes:  %s", pmodes);
	if_printf((*dmodes),"\n DMA modes:  %s", dmodes);
	if_printf((*umodes),"\n UDMA modes: %s", umodes);

	printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
	if (id_regs[83] & 8)
	{
		if (!(id_regs[86]&8))
			printf(": disabled (255)");
		else if ((id_regs[91]&0xFF00)!=0x4000)
			printf(": unknown setting");
		else
			printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
	}
	if_printf( (id_regs[82]&0x20)," WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
#ifdef __NEW_HD_DRIVE_ID
	if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
	{
		printf("\n Drive conforms to: ");
		if_else_printf((id->minor_rev_num <= 31),"%s: ","unknown: ", minor_str[id->minor_rev_num]);
		if (id->major_rev_num < 31)
		{
			for (i=0; i <= 15; i++)
				if_printf((id->major_rev_num & (1<<i))," %u", i);
		}
	}
#endif /* __NEW_HD_DRIVE_ID */
	printf("\n\n * signifies the current active mode\n\n");
}
#endif

static void flush_buffer_cache (int fd)
{
	fsync (fd);				/* flush buffers */
	bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
#ifdef HDIO_DRIVE_CMD
	sleep(1);
	if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL)	/* await completion */
			bb_perror_msg("HDIO_DRIVE_CMD");
#endif
}

static int seek_to_zero (int fd)
{
	if (lseek(fd, (off_t) 0, SEEK_SET))
		return 1;
	return 0;
}

static int read_big_block (int fd, char *buf)
{

	const char *string;
	int i, rc;
	if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
	{
		switch(rc)
		{
			case -1:
				string = "read()";
				break;
			case  0:
				string = "read() hit EOF - device too small";
				break;
			default:
				string = "read(%u) returned %u bytes";
		}
		bb_error_msg(string, TIMING_BUF_BYTES, rc);
		return 1;
	}

	/* access all sectors of buf to ensure the read fully completed */
	for (i = 0; i < TIMING_BUF_BYTES; i += 512)
		buf[i] &= 1;
	return 0;
}

static double correction;

static void do_time (int flag, int fd)
/*
	flag = 0 time_cache
	flag = 1 time_device
*/
{
	int i;
	char *buf;
	double elapsed;
	struct itimerval e1, e2;
	int shmid;
	int timing_MB = TIMING_MB;

	if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
	{
		bb_error_msg (bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
		return;
	}
	if (shmctl(shmid, SHM_LOCK, NULL) == -1)
	{
		bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
		(void) shmctl(shmid, IPC_RMID, NULL);
		return;
	}
	if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
	{
		bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
		(void) shmctl(shmid, IPC_RMID, NULL);
		return;
	}
	if (shmctl(shmid, IPC_RMID, NULL) == -1)
		bb_error_msg ("shmctl(,IPC_RMID,)");

	/* Clear out the device request queues & give them time to complete */
	sync_and_sleep(3);

	if(flag  == 0) /* Time cache */
	{
		/* Calculate a correction factor for the basic
		* overhead of doing a read() from the buffer cache.
		* To do this, we read the data once to "cache it" and
		* to force full preallocation of our timing buffer,
		* and then we re-read it 10 times while timing it.
		*
		* getitimer() is used rather than gettimeofday() because
		* it is much more consistent (on my machine, at least).
		*/
		setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
		if (seek_to_zero (fd))
			return;
		if (read_big_block (fd, buf))
			return;
		printf(" Timing buffer-cache reads:   ");
		fflush(stdout);

		/* Clear out the device request queues & give them time to complete */
		sync_and_sleep(1);

		/* Time re-reading from the buffer-cache */
		getitimer(ITIMER_REAL, &e1);
		for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
		{
			if (seek_to_zero (fd))
				goto quit;
			if (read_big_block (fd, buf))
				goto quit;
		}
		getitimer(ITIMER_REAL, &e2);
		correction = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);

		/* Now remove the lseek() from the correction factor */
		getitimer(ITIMER_REAL, &e1);
		for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
		{
			if (seek_to_zero (fd))
				goto quit;
		}
		getitimer(ITIMER_REAL, &e2);
		correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
		+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);

		if ((BUFCACHE_FACTOR * timing_MB) >= correction)  /* more than 1MB/s */
			printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
				(BUFCACHE_FACTOR * timing_MB),  correction,
				(BUFCACHE_FACTOR * timing_MB) / correction);
		else
			printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
				(BUFCACHE_FACTOR * timing_MB),  correction,
				(BUFCACHE_FACTOR * timing_MB) / correction * 1024);
		correction /= BUFCACHE_FACTOR;

		flush_buffer_cache(fd);
		sleep(1);
	}
	else /* Time device */
	{
		printf(" Timing buffered disk reads:  ");
		fflush(stdout);

		/*
		* getitimer() is used rather than gettimeofday() because
		* it is much more consistent (on my machine, at least).
		*/
		setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);

		/* Now do the timings for real */
		getitimer(ITIMER_REAL, &e1);
		for (i = TIMING_BUF_COUNT; i > 0; --i)
		{
			if (read_big_block (fd, buf))
				goto quit;
		}
		getitimer(ITIMER_REAL, &e2);

		elapsed =	(e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);

		if (timing_MB >= elapsed)  /* more than 1MB/s */
			printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",timing_MB, elapsed, timing_MB / elapsed);
		else
			printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);

		/*"Hmm.. suspicious results: probably not enough free memory for a proper test.");*/
		if (elapsed <= (correction * 2))
					bb_error_msg(bb_msg_memory_exhausted);

#if 0  /* the "estimate" is just plain wrong for many systems.. */
		else if (correction != 0.0) {
			printf(" Estimating raw driver speed: ");
			elapsed -= correction;
			if (timing_MB >= elapsed)  /* more than 1MB/s */
				printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
					timing_MB, elapsed, timing_MB / elapsed);
			else
				printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
					timing_MB, elapsed, timing_MB / elapsed * 1024);
		}
#endif
	}
quit:
	if (-1 == shmdt(buf))
		bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
}


static void no_scsi (void)
{
	/*" operation not supported on SCSI disks"*/
	if (is_scsi_hd)
		bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
}

static void no_xt (void)
{
	if (is_xt_hd)
		bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
}

static void on_off (unsigned int value)
{
	printf(value ? " (on)\n" : " (off)\n");
}

#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
static void bus_state_value (unsigned int value)
{
	const char *string;

	switch (value)
	{
		case BUSSTATE_ON:
			string = " (on)\n";
			break;
		case BUSSTATE_OFF:
			string = " (off)\n";
			break;
		case BUSSTATE_TRISTATE:
			string = " (tristate)\n";
			break;
		default:
			string = " (unknown: %d)\n";
			break;
	}
	printf(string, value);
}
#endif

#ifdef HDIO_DRIVE_CMD
static void interpret_standby (unsigned int standby)
{
	printf(" (");
	switch(standby)
	{
		case 0:
			printf("off");
			break;
		case 252:
			printf("21 minutes");
			break;
		case 253:
			printf("vendor-specific");
			break;
		case 254:
			printf("?reserved");
			break;
		case 255:
			printf("21 minutes + 15 seconds");
			break;
		default:
			if (standby <= 240)
			{
				unsigned int secs = standby * 5;
				unsigned int mins = secs / 60;
				secs %= 60;
				if_printf(mins,"%u minutes", mins);
				if_printf((mins && secs)," + ");
				if_printf(secs,"%u seconds", secs);
			}
			else if (standby <= 251)
			{
				unsigned int mins = (standby - 240) * 30;
				unsigned int hrs  = mins / 60;
				mins %= 60;
				if_printf(hrs,"%u hours", hrs);
				if_printf((hrs && mins)," + ");
				if_printf(mins,"%u minutes", mins);
			}
			else
				printf("illegal value");
			break;
	}
	printf(")\n");
}

struct xfermode_entry {
    int val;
    const char *name;
};

static const struct xfermode_entry xfermode_table[] = {
    { 8,    "pio0" },
    { 9,    "pio1" },
    { 10,   "pio2" },
    { 11,   "pio3" },
    { 12,   "pio4" },
    { 13,   "pio5" },
    { 14,   "pio6" },
    { 15,   "pio7" },
    { 16,   "sdma0" },
    { 17,   "sdma1" },
    { 18,   "sdma2" },
    { 19,   "sdma3" },
    { 20,   "sdma4" },
    { 21,   "sdma5" },
    { 22,   "sdma6" },
    { 23,   "sdma7" },
    { 32,   "mdma0" },
    { 33,   "mdma1" },
    { 34,   "mdma2" },
    { 35,   "mdma3" },
    { 36,   "mdma4" },
    { 37,   "mdma5" },
    { 38,   "mdma6" },
    { 39,   "mdma7" },
    { 64,   "udma0" },
    { 65,   "udma1" },
    { 66,   "udma2" },
    { 67,   "udma3" },
    { 68,   "udma4" },
    { 69,   "udma5" },
    { 70,   "udma6" },
    { 71,   "udma7" },
    { 0, NULL }
};

static int translate_xfermode(char * name)
{
	const struct xfermode_entry *tmp;
	char *endptr;
	int val = -1;


	for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
	{
		if (!strcmp(name, tmp->name))
			return tmp->val;

	}

	val = strtol(name, &endptr, 10);
	if (*endptr == '\0')
		return val;

	return -1;
}

static void interpret_xfermode (unsigned int xfermode)
{
	printf(" (");
	switch(xfermode) {
		case 0:
			printf("default PIO mode");
			break;
		case 1:
			printf("default PIO mode, disable IORDY");
			break;
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
			printf("PIO flow control mode%u", xfermode-8);
			break;
		case 16:
		case 17:
		case 18:
		case 19:
		case 20:
		case 21:
		case 22:
		case 23:
			printf("singleword DMA mode%u", xfermode-16);
			break;
		case 32:
		case 33:
		case 34:
		case 35:
		case 36:
		case 37:
		case 38:
		case 39:
			printf("multiword DMA mode%u", xfermode-32);
			break;
		case 64:
		case 65:
		case 66:
		case 67:
		case 68:
		case 69:
		case 70:
		case 71:
			printf("UltraDMA mode%u", xfermode-64);
			break;
		default:
			printf("unknown, probably not valid");
			break;
	}
	printf(")\n");
}
#endif /* HDIO_DRIVE_CMD */

#ifndef VXVM_MAJOR
#define VXVM_MAJOR 199
#endif

#ifndef CCISS_MAJOR
#define CCISS_MAJOR 104
#endif

static void process_dev (char *devname)
{
	int fd;
	static long parm, multcount;
	struct stat stat_buf;
#ifndef HDIO_DRIVE_CMD
	int force_operation = 0;
#endif
	if (stat(devname,&stat_buf))
		bb_perror_msg_and_die(devname);

	switch(major(stat_buf.st_rdev))
	{
#ifdef SCSI_DISK0_MAJOR
		case (SCSI_DISK0_MAJOR):
		case (SCSI_DISK1_MAJOR):
		case (SCSI_DISK2_MAJOR):
		case (SCSI_DISK3_MAJOR):
		case (SCSI_DISK4_MAJOR):
		case (SCSI_DISK5_MAJOR):
		case (SCSI_DISK6_MAJOR):
		case (SCSI_DISK7_MAJOR):
#else
		case (SCSI_DISK_MAJOR):
#endif
#ifdef MD_MAJOR
		case (MD_MAJOR):
#endif
		case (VXVM_MAJOR):
#ifdef LVM_BLK_MAJOR
		case (LVM_BLK_MAJOR):
#endif
		case (CCISS_MAJOR):
			is_scsi_hd = 1;
			break;
#ifdef XT_DISK_MAJOR
		case (XT_DISK_MAJOR):
			is_xt_hd = 1;
			break;
#endif
		case IDE0_MAJOR:
		case IDE1_MAJOR:
#ifdef IDE2_MAJOR
		case IDE2_MAJOR:
#endif
#ifdef IDE3_MAJOR
		case IDE3_MAJOR:
#endif
#ifdef IDE4_MAJOR
		case IDE4_MAJOR:
#endif
#ifdef IDE5_MAJOR
		case IDE5_MAJOR:
#endif
#ifdef IDE6_MAJOR
		case IDE6_MAJOR:
#endif
#ifdef IDE7_MAJOR
		case IDE7_MAJOR:
#endif
#ifdef IDE8_MAJOR
		case IDE8_MAJOR:
#endif
#ifdef IDE9_MAJOR
		case IDE9_MAJOR:
#endif
			break;	/* do nothing */
		default:
			bb_error_msg_and_die("%s not supported",devname);
	}

	fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
	if_printf( (!quiet),"\n%s:\n", devname);

	if (set_readahead)
	{
		if_printf(get_readahead," setting fs readahead to %ld\n", Xreadahead);
		bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
	}
#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
	if (unregister_hwif)
	{
		no_scsi();
		printf(" attempting to unregister hwif#%u\n", hwif);
		bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)hwif,"HDIO_UNREGISTER_HWIF");
	}
#endif
#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
	if (scan_hwif)
	{
		int	args[3];
		no_scsi();
		printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
		args[0] = hwif_data;
		args[1] = hwif_ctrl;
		args[2] = hwif_irq;
		bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
	}
#endif
	if (set_piomode)
	{
		no_scsi();
		no_xt();

		if (noisy_piomode)
		{
			printf(" attempting to ");
			if (piomode == 255)
				printf("auto-tune PIO mode\n");
			else if (piomode < 100)
				printf("set PIO mode to %d\n", piomode);
			else if (piomode < 200)
				printf("set MDMA mode to %d\n", (piomode-100));
			else
				printf("set UDMA mode to %d\n", (piomode-200));
		}
		bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)piomode, "HDIO_SET_PIO_MODE");
	}
	if (set_io32bit)
	{
		no_scsi();
		no_xt();
		if_printf(get_io32bit," setting 32-bit IO_support flag to %ld\n", io32bit);
		bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
	}
	if (set_mult)
	{
		no_scsi();
		no_xt();
		if_printf(get_mult, " setting multcount to %ld\n", mult);
		if(ioctl(fd, HDIO_SET_MULTCOUNT, mult))
			bb_perror_msg("HDIO_SET_MULTCOUNT");
#ifndef HDIO_DRIVE_CMD
		else
			force_operation = 1;
#endif
	}
	if (set_readonly)
	{
		if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
		bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
	}
	if (set_unmask)
	{
		no_scsi();
		no_xt();
		if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
		bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
	}
#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
	if (set_dma)
	{
		no_scsi();
		if_printf_on_off(get_dma," setting using_dma to %ld", dma);
		bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
	}
#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
	if (set_dma_q)
	{
		no_scsi();
		if_printf_on_off(get_dma_q," setting DMA queue_depth to %ld", dma_q);
		bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
	}
	if (set_nowerr)
	{
		no_scsi();
		no_xt();
		if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
		bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
	}
	if (set_keep)
	{
		no_scsi();
		no_xt();
		if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
		bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
	}
#ifdef HDIO_DRIVE_CMD
	if (set_doorlock)
	{
		unsigned char args[4] = {0,0,0,0};
		no_scsi();
		no_xt();

		args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
		if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
	}
	if (set_dkeep)
	{
		/* lock/unlock the drive's "feature" settings */
		unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
		no_scsi();
		no_xt();

		if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
		args[2] = dkeep ? 0x66 : 0xcc;
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
	}
	if (set_defects)
	{
		unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
		no_scsi();
		args[2] = defects ? 0x04 : 0x84;
		if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
	}
	if (set_prefetch)
	{
		unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
		no_scsi();
		no_xt();

		args[1] = prefetch;
		if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
	}
	if (set_xfermode)
	{
		unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
		no_scsi();
		no_xt();

		args[1] = xfermode_requested;
		if (get_xfermode)
		{
			printf(" setting xfermode to %d", xfermode_requested);
			interpret_xfermode(xfermode_requested);
		}
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
	}
	if (set_lookahead)
	{
		unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
		no_scsi();
		no_xt();

		args[2] = lookahead ? 0xaa : 0x55;
		if_printf_on_off(get_lookahead," setting drive read-lookahead to %ld", lookahead);
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
	}
	if (set_apmmode)
	{
		unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
		no_scsi();
		apmmode=check_if_min_and_set_val(apmmode,1);
		apmmode=check_if_maj_and_set_val(apmmode,255);
		if_printf(get_apmmode," setting APM level to");
		if (apmmode==255)
		{
			/* disable Advanced Power Management */
			args[2] = 0x85; /* feature register */
			if_printf(get_apmmode," disabled\n");
		}
		else
		{
			/* set Advanced Power Management mode */
			args[2] = 0x05; /* feature register */
			args[1] = apmmode; /* sector count register */
			if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
		}
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
	}
	if (set_wcache)
	{
#ifdef DO_FLUSHCACHE
#ifndef WIN_FLUSHCACHE
#define WIN_FLUSHCACHE 0xe7
#endif
		unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
#endif /* DO_FLUSHCACHE */
		unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
		no_scsi();
		no_xt();
		args[2] = wcache ? 0x02 : 0x82;
		if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
#ifdef DO_FLUSHCACHE
		if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
			bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
#endif /* DO_FLUSHCACHE */
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
#ifdef DO_FLUSHCACHE
		if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
			bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
#endif /* DO_FLUSHCACHE */
	}
	if (set_standbynow)
	{
#ifndef WIN_STANDBYNOW1
#define WIN_STANDBYNOW1 0xE0
#endif
#ifndef WIN_STANDBYNOW2
#define WIN_STANDBYNOW2 0x94
#endif
		unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
		unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
		no_scsi();
		if_printf(get_standbynow," issuing standby command\n");
		if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
		 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
			bb_perror_msg("HDIO_DRIVE_CMD(standby)");
	}
	if (set_sleepnow)
	{
#ifndef WIN_SLEEPNOW1
#define WIN_SLEEPNOW1 0xE6
#endif
#ifndef WIN_SLEEPNOW2
#define WIN_SLEEPNOW2 0x99
#endif
		unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
		unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
		no_scsi();
		if_printf(get_sleepnow," issuing sleep command\n");
		if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
		 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
			bb_perror_msg("HDIO_DRIVE_CMD(sleep)");
	}
	if (set_seagate)
	{
		unsigned char args[4] = {0xfb,0,0,0};
		no_scsi();
		no_xt();
		if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
	}
	if (set_standby)
	{
		unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
		no_scsi();
		no_xt();
		if (get_standby)
		{
			printf(" setting standby to %lu", standby_requested);
			interpret_standby(standby_requested);
		}
		bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
	}
#else	/* HDIO_DRIVE_CMD */
	if (force_operation)
	{
		char buf[512];
		flush_buffer_cache(fd);
		if (-1 == read(fd, buf, sizeof(buf)))
			bb_error_msg("access failed");
	}
#endif	/* HDIO_DRIVE_CMD */

	if (!flagcount)
		verbose = 1;

	if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
	{
		no_scsi();
		multcount = -1;
		if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
		{
			if ((verbose && !is_xt_hd) || get_mult)
				bb_perror_msg("HDIO_GET_MULTCOUNT");
		}
		else if (verbose | get_mult)
		{
			printf(" multcount    = %2ld", multcount);
			on_off(multcount);
		}
	}
	if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
	{
		no_scsi();
		no_xt();
		if(ioctl(fd, HDIO_GET_32BIT, &parm))
			bb_perror_msg("HDIO_GET_32BIT");
		else
		{
			printf(" IO_support   =%3ld (", parm);
			switch (parm)
			{
				case 0:
					printf("default ");
				case 2:
					printf("16-bit)\n");
					break;
				case 1:
					printf("32-bit)\n");
					break;
				case 3:
					printf("32-bit w/sync)\n");
					break;
				case 8:
					printf("Request-Queue-Bypass)\n");
					break;
				default:
					printf("\?\?\?)\n");
				/*esac*/
			}
		}
	}
	if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
	{
		no_scsi();
		no_xt();
		bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
						"HDIO_GET_UNMASKINTR"," unmaskirq    = %2ld");
	}


#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
	if ((verbose && !is_scsi_hd) || get_dma) {
		no_scsi();
		if(ioctl(fd, HDIO_GET_DMA, &parm))
			bb_perror_msg("HDIO_GET_DMA");
		else
		{
			printf(" using_dma    = %2ld", parm);
			if (parm == 8)
				printf(" (DMA-Assisted-PIO)\n");
			else
				on_off(parm);
		}
	}
#endif
	if (get_dma_q)
	{
		no_scsi();
		bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
						  "HDIO_GET_QDMA"," queue_depth  = %2ld");
	}
	if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
	{
		no_scsi();
		no_xt();
		bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
							"HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
	}

	if (get_nowerr)
	{
		no_scsi();
		no_xt();
		bb_ioctl_on_off  (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
							" HDIO_GET_NOWERR"," nowerr       = %2ld");
	}
	if (verbose || get_readonly)
	{
		bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
						  " BLKROGET"," readonly     = %2ld");
	}
	if ((verbose && !is_scsi_hd) || get_readahead)
	{
		bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
							" BLKRAGET"," readahead    = %2ld");
	}
	if (verbose || get_geom)
	{
		static const char msg[] = " geometry     = %u/%u/%u, sectors = %ld, start = %ld\n";
		static struct hd_geometry g;
#ifdef HDIO_GETGEO_BIG
		static struct hd_big_geometry bg;
#endif

		if (ioctl(fd, BLKGETSIZE, &parm))
			bb_perror_msg("BLKGETSIZE");
#ifdef HDIO_GETGEO_BIG
		else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
			printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
#endif
		else if (ioctl(fd, HDIO_GETGEO, &g))
			bb_perror_msg("HDIO_GETGEO");
		else
			printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
	}
#ifdef HDIO_DRIVE_CMD
	if (get_powermode)
	{
#ifndef WIN_CHECKPOWERMODE1
#define WIN_CHECKPOWERMODE1 0xE5
#endif
#ifndef WIN_CHECKPOWERMODE2
#define WIN_CHECKPOWERMODE2 0x98
#endif
		unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
		const char *state;
		no_scsi();
		if (ioctl(fd, HDIO_DRIVE_CMD, &args)
			 && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
		 	&& ioctl(fd, HDIO_DRIVE_CMD, &args))
		{
			if (errno != EIO || args[0] != 0 || args[1] != 0)
				state = "unknown";
			else
				state = "sleeping";
		}
		else
			state = (args[2] == 255) ? "active/idle" : "standby";

		printf(" drive state is:  %s\n", state);
	}
#endif
#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
	if (perform_reset)
	{
		no_scsi();
		no_xt();
		bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
	}
#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
	if (perform_tristate)
	{
		unsigned char args[4] = {0,tristate,0,0};
		no_scsi();
		no_xt();
		bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
	}
#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
	if (get_identity)
	{
		static struct hd_driveid id;

		no_scsi();
		no_xt();

		if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
		{
			if (multcount != -1)
			{
				id.multsect = multcount;
				id.multsect_valid |= 1;
			}
			else
				id.multsect_valid &= ~1;
			dump_identity(&id);
		}
		else if (errno == -ENOMSG)
			printf(" no identification info available\n");
		else
			bb_perror_msg("HDIO_GET_IDENTITY");
	}

	if (get_IDentity)
	{
		unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
		unsigned i;

		no_scsi();
		no_xt();

		if (ioctl(fd, HDIO_DRIVE_CMD, &args))
		{
			args[0] = WIN_PIDENTIFY;
			if (ioctl(fd, HDIO_DRIVE_CMD, &args))
			{
				bb_perror_msg("HDIO_DRIVE_CMD(identify)");
				goto identify_abort;
			}
		}
		for(i=0; i<(sizeof args)/2; i+=2)
		    __le16_to_cpus((uint16_t *)(&args[i]));

		identify((void *)&args[4], NULL);
identify_abort:
	/* VOID */;
	}
#endif
#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
	if (set_busstate)
	{
		no_scsi();
		if (get_busstate)
		{
			printf(" setting bus state to %d", busstate);
			bus_state_value(busstate);
		}
		bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)busstate, "HDIO_SET_BUSSTATE");
	}
#endif
#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
	if (get_busstate)
	{
		no_scsi();
		if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
			bb_perror_msg("HDIO_GET_BUSSTATE");
		else
		{
			printf(" busstate     = %2ld", parm);
			bus_state_value(parm);
		}
	}
#endif
	if (reread_partn)
		bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");


	if (do_ctimings)
		do_time(0,fd); 		/*time cache  */
	if (do_timings)
		do_time(1,fd);		/*time device */
	if (do_flush)
		flush_buffer_cache (fd);
	close (fd);
}

static char * GET_NUMBER(char *p, unsigned long *flag, unsigned long *num)
{
	*num = 0;
	while (isdigit(*p)) {
		*flag = 1;
		*num = (*num * 10) + (*p++ - '0');
	}
	return p;
}

static char * GET_STRING(char *p, unsigned long *flag, int *num)
{
	char *tmpstr;
	char name[32];
	tmpstr = name;
	tmpstr[0] = '\0';
	while (isalnum(*p) && (tmpstr - name) < 31) {
		tmpstr[0] = *p++;
		tmpstr[1] = '\0';
		++tmpstr;
	}
	*num = translate_xfermode(name);
	if (*num == -1)
		*flag = 0;
	else
		*flag = 1;
	return p;
}

#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
static int fromhex (unsigned char c)
{
	if (c >= 'a' && c <= 'f')
		return 10 + (c - 'a');
	if (c >= '0' && c <= '9')
		return (c - '0');
	bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
}

static int identify_from_stdin (void)
{
	unsigned short sbuf[800];
	unsigned char  buf[1600], *b = (unsigned char *)buf;
	int i, count = read(0, buf, 1280);

	if (count != 1280)
		bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
	for (i = 0; count >= 4; ++i)
	{
		sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
		__le16_to_cpus((uint16_t *)(&sbuf[i]));
		b += 5;
		count -= 5;
	}
	identify(sbuf, NULL);
	return 0;
}
#endif

static void missing_arg(int arg, char c, char* add)
{
	if (!arg)
		bb_error_msg("-%c: missing value %s", c, (add!=NULL)? add :"");
}

/* our main() routine: */
int hdparm_main(int argc, char **argv)
{
	char c, *p;

	++argv;
	if (!--argc)
		bb_show_usage();

	while (argc--)
	{
#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
		if (!strcmp("-Istdin", *argv))
		{
			return identify_from_stdin();
		}
#endif
		p = *argv++;
		if (*p == '-')
		{
			if (!*++p)
				bb_show_usage();
			while ((c = *p++))
			{
				++flagcount;
				switch (c)
				{
					case 'V':
						/*bb_error_msg_and_die("%s", VERSION);*/
						/* We have to return 0 here and not 1 */
						printf("%s %s\n",bb_applet_name, VERSION);
						return EXIT_SUCCESS;
					case 'v':
						verbose = 1;
						break;
#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
					case 'I':
						get_IDentity = 1;
						break;
					case 'i':
						get_identity = 1;
						break;
#endif
					case 'g':
						get_geom = 1;
						break;
					case 'f':
						do_flush = 1;
						break;
					case 'q':
						quiet = 1;
						noisy = 0;
						break;
					case 'u':
						get_unmask = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_unmask = set_flag(p,'1'))==1)
							unmask  = *p++ - '0';
						break;
#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
					case 'd':
						get_dma = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_dma = set_flag(p,'9'))==1)
							dma = *p++ - '0';
						break;
#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
					case 'n':
						get_nowerr = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_nowerr = set_flag(p,'1'))==1)
							nowerr  = *p++ - '0';
						break;
					case 'p':
						noisy_piomode = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_STRING(p,&set_piomode,&piomode);
						break;
					case 'r':
						get_readonly = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_readonly = set_flag(p,'1'))==1)
							readonly  = *p++ - '0';
						break;
					case 'm':
						get_mult = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_NUMBER(p,&set_mult,&mult);
						break;
					case 'c':
						get_io32bit = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_NUMBER(p,&set_io32bit,&io32bit);
						break;
#ifdef HDIO_DRIVE_CMD
					case 'S':
						get_standby = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_NUMBER(p,&set_standby,&standby_requested);
						missing_arg(set_standby, c, NULL);
						break;

					case 'D':
						get_defects = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_NUMBER(p,&set_defects,&defects);
						missing_arg(set_defects, c, NULL);
						break;
					case 'P':
						get_prefetch = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_NUMBER(p,&set_prefetch,&prefetch);
						missing_arg(set_prefetch, c, NULL);
						break;

					case 'X':
						get_xfermode = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_STRING(p,&set_xfermode,&xfermode_requested);
						missing_arg(set_xfermode, c, NULL);
						break;

					case 'K':
						get_dkeep = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_dkeep = set_flag(p,'1'))==1)
							dkeep  = *p++ - '0';
						else
							goto missing_arg_error;
						break;

					case 'A':
						get_lookahead = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_lookahead = set_flag(p,'1'))==1)
							lookahead  = *p++ - '0';
						else
							goto missing_arg_error;
						break;

					case 'L':
						get_doorlock = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_doorlock = set_flag(p,'1'))==1)
							doorlock  = *p++ - '0';
						else
							goto missing_arg_error;
						break;

					case 'W':
						get_wcache = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_wcache = set_flag(p,'1'))==1)
							wcache  = *p++ - '0';
						else
missing_arg_error:
							missing_arg(1, c, "(0/1)");
						break;

					case 'C':
						get_powermode = noisy;
						noisy = 1;
						break;

					case 'y':
						get_standbynow = noisy;
						noisy = 1;
						set_standbynow = 1;
						break;

					case 'Y':
						get_sleepnow = noisy;
						noisy = 1;
						set_sleepnow = 1;
						break;

					case 'z':
						reread_partn = 1;
						break;

					case 'Z':
						get_seagate = noisy;
						noisy = 1;
						set_seagate = 1;
						break;
#endif /* HDIO_DRIVE_CMD */
					case 'k':
						get_keep = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_keep = set_flag(p,'1'))==1)
							keep  = *p++ - '0';
						break;
#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
					case 'U':
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if(! p)
							goto expected_hwif_error; /* "expected hwif_nr" */

						sscanf(p++, "%i", &hwif);

						unregister_hwif = 1;
						break;
#endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
					case 'R':
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if(! p)
							goto expected_hwif_error; /* "expected hwif_data" */

						sscanf(p++, "%i", &hwif_data);

						if (argc && isdigit(**argv))
							p = *argv++, --argc;
						else
							goto expected_hwif_error; /* "expected hwif_ctrl" */

						sscanf(p, "%i", &hwif_ctrl);

						if (argc && isdigit(**argv))
							p = *argv++, --argc;
						else
expected_hwif_error:
							bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */

						sscanf(p, "%i", &hwif_irq);

						*p = '\0';

						scan_hwif = 1;
						break;
#endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
					case 'Q':
#ifdef HDIO_GET_QDMA
						get_dma_q = noisy;
						noisy = 1;
#ifdef HDIO_SET_QDMA
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_NUMBER(p,&set_dma_q,&dma_q);
#ifdef HDIO_GET_QDMA
						dma_q = -dma_q;
#endif
#endif
#endif
						break;

#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
					case 'w':
						perform_reset = 1;
						break;
#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
					case 'x':
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((perform_tristate = set_flag(p,'1'))==1)
							tristate  = *p++ - '0';
						else
							missing_arg(1, c, "(0/1)");
						break;

#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
					case 'a':
						get_readahead = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_NUMBER(p,&set_readahead,&Xreadahead);
						break;
					case 'B':
						get_apmmode = noisy;
						noisy = 1;
						if (!*p && argc && isalnum(**argv))
							p = *argv++, --argc;
						p=GET_NUMBER(p,&set_apmmode,&apmmode);
						missing_arg(set_apmmode, c, "(1-255)");
						break;
					case 't':
						do_timings = 1;
						do_flush = 1;
						break;
					case 'T':
						do_ctimings = 1;
						do_flush = 1;
						break;
#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
					case 'b':
						get_busstate = noisy;
						noisy = 1;
						if (!*p && argc && isdigit(**argv))
							p = *argv++, --argc;
						if((set_busstate = set_flag(p,'2'))==1)
							busstate  = *p++ - '0';
						break;
#endif
					case 'h':
					default:
						bb_show_usage();
				}
			}
			if (!argc)
				bb_show_usage();
		} else {
			process_dev (p);
		}
	}
	return 0 ;
}