33f05be0c864161d005f493b8e67f87d6bd37444
[oweals/busybox.git] / miscutils / hdparm.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * hdparm implementation for busybox
4  *
5  * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
6  * Hacked by Tito <farmatito@tiscali.it> for size optimization.
7  *
8  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9  *
10  * This program is based on the source code of hdparm: see below...
11  * hdparm.c - Command line interface to get/set hard disk parameters
12  *          - by Mark Lord (C) 1994-2002 -- freely distributable
13  */
14
15 #include <unistd.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include <endian.h>
22 #include <sys/ioctl.h>
23 #include <sys/shm.h>
24 #include <sys/sysmacros.h>
25 #include <sys/time.h>
26 #include <sys/times.h>
27 #include <sys/mount.h>
28 #include "busybox.h"
29 #include <linux/types.h>
30 #include <linux/hdreg.h>
31 #include <linux/major.h>
32 #include <asm/byteorder.h>
33
34
35 #if BB_BIG_ENDIAN && !defined(__USE_XOPEN)
36 # define __USE_XOPEN
37 #endif
38
39 /* device types */
40 /* ------------ */
41 #define NO_DEV                  0xffff
42 #define ATA_DEV                 0x0000
43 #define ATAPI_DEV               0x0001
44
45 /* word definitions */
46 /* ---------------- */
47 #define GEN_CONFIG              0   /* general configuration */
48 #define LCYLS                   1   /* number of logical cylinders */
49 #define CONFIG                  2   /* specific configuration */
50 #define LHEADS                  3   /* number of logical heads */
51 #define TRACK_BYTES             4   /* number of bytes/track (ATA-1) */
52 #define SECT_BYTES              5   /* number of bytes/sector (ATA-1) */
53 #define LSECTS                  6   /* number of logical sectors/track */
54 #define START_SERIAL            10  /* ASCII serial number */
55 #define LENGTH_SERIAL           10  /* 10 words (20 bytes or characters) */
56 #define BUF_TYPE                20  /* buffer type (ATA-1) */
57 #define BUFFER__SIZE            21  /* buffer size (ATA-1) */
58 #define RW_LONG                 22  /* extra bytes in R/W LONG cmd ( < ATA-4)*/
59 #define START_FW_REV            23  /* ASCII firmware revision */
60 #define LENGTH_FW_REV            4  /*  4 words (8 bytes or characters) */
61 #define START_MODEL             27  /* ASCII model number */
62 #define LENGTH_MODEL            20  /* 20 words (40 bytes or characters) */
63 #define SECTOR_XFER_MAX         47  /* r/w multiple: max sectors xfered */
64 #define DWORD_IO                48  /* can do double-word IO (ATA-1 only) */
65 #define CAPAB_0                 49  /* capabilities */
66 #define CAPAB_1                 50
67 #define PIO_MODE                51  /* max PIO mode supported (obsolete)*/
68 #define DMA_MODE                52  /* max Singleword DMA mode supported (obs)*/
69 #define WHATS_VALID             53  /* what fields are valid */
70 #define LCYLS_CUR               54  /* current logical cylinders */
71 #define LHEADS_CUR              55  /* current logical heads */
72 #define LSECTS_CUR              56  /* current logical sectors/track */
73 #define CAPACITY_LSB            57  /* current capacity in sectors */
74 #define CAPACITY_MSB            58
75 #define SECTOR_XFER_CUR         59  /* r/w multiple: current sectors xfered */
76 #define LBA_SECTS_LSB           60  /* LBA: total number of user */
77 #define LBA_SECTS_MSB           61  /*      addressable sectors */
78 #define SINGLE_DMA              62  /* singleword DMA modes */
79 #define MULTI_DMA               63  /* multiword DMA modes */
80 #define ADV_PIO_MODES           64  /* advanced PIO modes supported */
81                                     /* multiword DMA xfer cycle time: */
82 #define DMA_TIME_MIN            65  /*   - minimum */
83 #define DMA_TIME_NORM           66  /*   - manufacturer's recommended   */
84                                     /* minimum PIO xfer cycle time: */
85 #define PIO_NO_FLOW             67  /*   - without flow control */
86 #define PIO_FLOW                68  /*   - with IORDY flow control */
87 #define PKT_REL                 71  /* typical #ns from PKT cmd to bus rel */
88 #define SVC_NBSY                72  /* typical #ns from SERVICE cmd to !BSY */
89 #define CDR_MAJOR               73  /* CD ROM: major version number */
90 #define CDR_MINOR               74  /* CD ROM: minor version number */
91 #define QUEUE_DEPTH             75  /* queue depth */
92 #define MAJOR                   80  /* major version number */
93 #define MINOR                   81  /* minor version number */
94 #define CMDS_SUPP_0             82  /* command/feature set(s) supported */
95 #define CMDS_SUPP_1             83
96 #define CMDS_SUPP_2             84
97 #define CMDS_EN_0               85  /* command/feature set(s) enabled */
98 #define CMDS_EN_1               86
99 #define CMDS_EN_2               87
100 #define ULTRA_DMA               88  /* ultra DMA modes */
101                                     /* time to complete security erase */
102 #define ERASE_TIME              89  /*   - ordinary */
103 #define ENH_ERASE_TIME          90  /*   - enhanced */
104 #define ADV_PWR                 91  /* current advanced power management level
105                                        in low byte, 0x40 in high byte. */
106 #define PSWD_CODE               92  /* master password revision code    */
107 #define HWRST_RSLT              93  /* hardware reset result */
108 #define ACOUSTIC                94  /* acoustic mgmt values ( >= ATA-6) */
109 #define LBA_LSB                 100 /* LBA: maximum.  Currently only 48 */
110 #define LBA_MID                 101 /*      bits are used, but addr 103 */
111 #define LBA_48_MSB              102 /*      has been reserved for LBA in */
112 #define LBA_64_MSB              103 /*      the future. */
113 #define RM_STAT                 127 /* removable media status notification feature set support */
114 #define SECU_STATUS             128 /* security status */
115 #define CFA_PWR_MODE            160 /* CFA power mode 1 */
116 #define START_MEDIA             176 /* media serial number */
117 #define LENGTH_MEDIA            20  /* 20 words (40 bytes or characters)*/
118 #define START_MANUF             196 /* media manufacturer I.D. */
119 #define LENGTH_MANUF            10  /* 10 words (20 bytes or characters) */
120 #define INTEGRITY               255 /* integrity word */
121
122 /* bit definitions within the words */
123 /* -------------------------------- */
124
125 /* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
126 #define VALID                   0xc000
127 #define VALID_VAL               0x4000
128 /* many words are considered invalid if they are either all-0 or all-1 */
129 #define NOVAL_0                 0x0000
130 #define NOVAL_1                 0xffff
131
132 /* word 0: gen_config */
133 #define NOT_ATA                 0x8000
134 #define NOT_ATAPI               0x4000  /* (check only if bit 15 == 1) */
135 #define MEDIA_REMOVABLE         0x0080
136 #define DRIVE_NOT_REMOVABLE     0x0040  /* bit obsoleted in ATA 6 */
137 #define INCOMPLETE              0x0004
138 #define CFA_SUPPORT_VAL         0x848a  /* 848a=CFA feature set support */
139 #define DRQ_RESPONSE_TIME       0x0060
140 #define DRQ_3MS_VAL             0x0000
141 #define DRQ_INTR_VAL            0x0020
142 #define DRQ_50US_VAL            0x0040
143 #define PKT_SIZE_SUPPORTED      0x0003
144 #define PKT_SIZE_12_VAL         0x0000
145 #define PKT_SIZE_16_VAL         0x0001
146 #define EQPT_TYPE               0x1f00
147 #define SHIFT_EQPT              8
148
149 #define CDROM 0x0005
150
151 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
152 static const char * const pkt_str[] = {
153         "Direct-access device",                 /* word 0, bits 12-8 = 00 */
154         "Sequential-access device",             /* word 0, bits 12-8 = 01 */
155         "Printer",                              /* word 0, bits 12-8 = 02 */
156         "Processor",                            /* word 0, bits 12-8 = 03 */
157         "Write-once device",                    /* word 0, bits 12-8 = 04 */
158         "CD-ROM",                               /* word 0, bits 12-8 = 05 */
159         "Scanner",                              /* word 0, bits 12-8 = 06 */
160         "Optical memory",                       /* word 0, bits 12-8 = 07 */
161         "Medium changer",                       /* word 0, bits 12-8 = 08 */
162         "Communications device",                /* word 0, bits 12-8 = 09 */
163         "ACS-IT8 device",                       /* word 0, bits 12-8 = 0a */
164         "ACS-IT8 device",                       /* word 0, bits 12-8 = 0b */
165         "Array controller",                     /* word 0, bits 12-8 = 0c */
166         "Enclosure services",                   /* word 0, bits 12-8 = 0d */
167         "Reduced block command device",         /* word 0, bits 12-8 = 0e */
168         "Optical card reader/writer",           /* word 0, bits 12-8 = 0f */
169         "",                                     /* word 0, bits 12-8 = 10 */
170         "",                                     /* word 0, bits 12-8 = 11 */
171         "",                                     /* word 0, bits 12-8 = 12 */
172         "",                                     /* word 0, bits 12-8 = 13 */
173         "",                                     /* word 0, bits 12-8 = 14 */
174         "",                                     /* word 0, bits 12-8 = 15 */
175         "",                                     /* word 0, bits 12-8 = 16 */
176         "",                                     /* word 0, bits 12-8 = 17 */
177         "",                                     /* word 0, bits 12-8 = 18 */
178         "",                                     /* word 0, bits 12-8 = 19 */
179         "",                                     /* word 0, bits 12-8 = 1a */
180         "",                                     /* word 0, bits 12-8 = 1b */
181         "",                                     /* word 0, bits 12-8 = 1c */
182         "",                                     /* word 0, bits 12-8 = 1d */
183         "",                                     /* word 0, bits 12-8 = 1e */
184         "Unknown",                              /* word 0, bits 12-8 = 1f */
185 };
186
187 static const char * const ata1_cfg_str[] = {                    /* word 0 in ATA-1 mode */
188         "reserved",                             /* bit 0 */
189         "hard sectored",                        /* bit 1 */
190         "soft sectored",                        /* bit 2 */
191         "not MFM encoded ",                     /* bit 3 */
192         "head switch time > 15us",              /* bit 4 */
193         "spindle motor control option",         /* bit 5 */
194         "fixed drive",                          /* bit 6 */
195         "removable drive",                      /* bit 7 */
196         "disk xfer rate <= 5Mbs",               /* bit 8 */
197         "disk xfer rate > 5Mbs, <= 10Mbs",      /* bit 9 */
198         "disk xfer rate > 5Mbs",                /* bit 10 */
199         "rotational speed tol.",                /* bit 11 */
200         "data strobe offset option",            /* bit 12 */
201         "track offset option",                  /* bit 13 */
202         "format speed tolerance gap reqd",      /* bit 14 */
203         "ATAPI"                                 /* bit 14 */
204 };
205 #endif
206
207 /* word 1: number of logical cylinders */
208 #define LCYLS_MAX               0x3fff /* maximum allowable value */
209
210 /* word 2: specific configuration
211  * (a) require SET FEATURES to spin-up
212  * (b) require spin-up to fully reply to IDENTIFY DEVICE
213  */
214 #define STBY_NID_VAL            0x37c8  /*     (a) and     (b) */
215 #define STBY_ID_VAL             0x738c  /*     (a) and not (b) */
216 #define PWRD_NID_VAL            0x8c73  /* not (a) and     (b) */
217 #define PWRD_ID_VAL             0xc837  /* not (a) and not (b) */
218
219 /* words 47 & 59: sector_xfer_max & sector_xfer_cur */
220 #define SECTOR_XFER             0x00ff  /* sectors xfered on r/w multiple cmds*/
221 #define MULTIPLE_SETTING_VALID  0x0100  /* 1=multiple sector setting is valid */
222
223 /* word 49: capabilities 0 */
224 #define STD_STBY                0x2000  /* 1=standard values supported (ATA);
225                                            0=vendor specific values */
226 #define IORDY_SUP               0x0800  /* 1=support; 0=may be supported */
227 #define IORDY_OFF               0x0400  /* 1=may be disabled */
228 #define LBA_SUP                 0x0200  /* 1=Logical Block Address support */
229 #define DMA_SUP                 0x0100  /* 1=Direct Memory Access support */
230 #define DMA_IL_SUP              0x8000  /* 1=interleaved DMA support (ATAPI) */
231 #define CMD_Q_SUP               0x4000  /* 1=command queuing support (ATAPI) */
232 #define OVLP_SUP                0x2000  /* 1=overlap operation support (ATAPI) */
233 #define SWRST_REQ               0x1000  /* 1=ATA SW reset required (ATAPI, obsolete */
234
235 /* word 50: capabilities 1 */
236 #define MIN_STANDBY_TIMER       0x0001  /* 1=device specific standby timer value minimum */
237
238 /* words 51 & 52: PIO & DMA cycle times */
239 #define MODE                    0xff00  /* the mode is in the MSBs */
240
241 /* word 53: whats_valid */
242 #define OK_W88                  0x0004  /* the ultra_dma info is valid */
243 #define OK_W64_70               0x0002  /* see above for word descriptions */
244 #define OK_W54_58               0x0001  /* current cyl, head, sector, cap. info valid */
245
246 /*word 63,88: dma_mode, ultra_dma_mode*/
247 #define MODE_MAX                7       /* bit definitions force udma <=7 (when
248                                          * udma >=8 comes out it'll have to be
249                                          * defined in a new dma_mode word!) */
250
251 /* word 64: PIO transfer modes */
252 #define PIO_SUP                 0x00ff  /* only bits 0 & 1 are used so far,  */
253 #define PIO_MODE_MAX            8       /* but all 8 bits are defined        */
254
255 /* word 75: queue_depth */
256 #define DEPTH_BITS              0x001f  /* bits used for queue depth */
257
258 /* words 80-81: version numbers */
259 /* NOVAL_0 or  NOVAL_1 means device does not report version */
260
261 /* word 81: minor version number */
262 #define MINOR_MAX               0x22
263 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
264 static const char *minor_str[MINOR_MAX+2] = {                   /* word 81 value: */
265         "Unspecified",                                  /* 0x0000       */
266         "ATA-1 X3T9.2 781D prior to revision 4",        /* 0x0001       */
267         "ATA-1 published, ANSI X3.221-1994",            /* 0x0002       */
268         "ATA-1 X3T9.2 781D revision 4",                 /* 0x0003       */
269         "ATA-2 published, ANSI X3.279-1996",            /* 0x0004       */
270         "ATA-2 X3T10 948D prior to revision 2k",        /* 0x0005       */
271         "ATA-3 X3T10 2008D revision 1",                 /* 0x0006       */
272         "ATA-2 X3T10 948D revision 2k",                 /* 0x0007       */
273         "ATA-3 X3T10 2008D revision 0",                 /* 0x0008       */
274         "ATA-2 X3T10 948D revision 3",                  /* 0x0009       */
275         "ATA-3 published, ANSI X3.298-199x",            /* 0x000a       */
276         "ATA-3 X3T10 2008D revision 6",                 /* 0x000b       */
277         "ATA-3 X3T13 2008D revision 7 and 7a",          /* 0x000c       */
278         "ATA/ATAPI-4 X3T13 1153D revision 6",           /* 0x000d       */
279         "ATA/ATAPI-4 T13 1153D revision 13",            /* 0x000e       */
280         "ATA/ATAPI-4 X3T13 1153D revision 7",           /* 0x000f       */
281         "ATA/ATAPI-4 T13 1153D revision 18",            /* 0x0010       */
282         "ATA/ATAPI-4 T13 1153D revision 15",            /* 0x0011       */
283         "ATA/ATAPI-4 published, ANSI INCITS 317-1998",  /* 0x0012       */
284         "ATA/ATAPI-5 T13 1321D revision 3",
285         "ATA/ATAPI-4 T13 1153D revision 14",            /* 0x0014       */
286         "ATA/ATAPI-5 T13 1321D revision 1",             /* 0x0015       */
287         "ATA/ATAPI-5 published, ANSI INCITS 340-2000",  /* 0x0016       */
288         "ATA/ATAPI-4 T13 1153D revision 17",            /* 0x0017       */
289         "ATA/ATAPI-6 T13 1410D revision 0",             /* 0x0018       */
290         "ATA/ATAPI-6 T13 1410D revision 3a",            /* 0x0019       */
291         "ATA/ATAPI-7 T13 1532D revision 1",             /* 0x001a       */
292         "ATA/ATAPI-6 T13 1410D revision 2",             /* 0x001b       */
293         "ATA/ATAPI-6 T13 1410D revision 1",             /* 0x001c       */
294         "ATA/ATAPI-7 published, ANSI INCITS 397-2005",  /* 0x001d       */
295         "ATA/ATAPI-7 T13 1532D revision 0",             /* 0x001e       */
296         "Reserved"                                      /* 0x001f       */
297         "Reserved"                                      /* 0x0020       */
298         "ATA/ATAPI-7 T13 1532D revision 4a",            /* 0x0021       */
299         "ATA/ATAPI-6 published, ANSI INCITS 361-2002",  /* 0x0022       */
300         "Reserved"                                      /* 0x0023-0xfffe*/
301 };
302 #endif
303 static const char actual_ver[MINOR_MAX+2] = {
304                         /* word 81 value: */
305         0,              /* 0x0000       WARNING:        */
306         1,              /* 0x0001       WARNING:        */
307         1,              /* 0x0002       WARNING:        */
308         1,              /* 0x0003       WARNING:        */
309         2,              /* 0x0004       WARNING:   This array           */
310         2,              /* 0x0005       WARNING:   corresponds          */
311         3,              /* 0x0006       WARNING:   *exactly*            */
312         2,              /* 0x0007       WARNING:   to the ATA/          */
313         3,              /* 0x0008       WARNING:   ATAPI version        */
314         2,              /* 0x0009       WARNING:   listed in            */
315         3,              /* 0x000a       WARNING:   the                  */
316         3,              /* 0x000b       WARNING:   minor_str            */
317         3,              /* 0x000c       WARNING:   array                */
318         4,              /* 0x000d       WARNING:   above.               */
319         4,              /* 0x000e       WARNING:                        */
320         4,              /* 0x000f       WARNING:   if you change        */
321         4,              /* 0x0010       WARNING:   that one,            */
322         4,              /* 0x0011       WARNING:   change this one      */
323         4,              /* 0x0012       WARNING:   too!!!               */
324         5,              /* 0x0013       WARNING:        */
325         4,              /* 0x0014       WARNING:        */
326         5,              /* 0x0015       WARNING:        */
327         5,              /* 0x0016       WARNING:        */
328         4,              /* 0x0017       WARNING:        */
329         6,              /* 0x0018       WARNING:        */
330         6,              /* 0x0019       WARNING:        */
331         7,              /* 0x001a       WARNING:        */
332         6,              /* 0x001b       WARNING:        */
333         6,              /* 0x001c       WARNING:        */
334         7,              /* 0x001d       WARNING:        */
335         7,              /* 0x001e       WARNING:        */
336         0,              /* 0x001f       WARNING:        */
337         0,              /* 0x0020       WARNING:        */
338         7,              /* 0x0021       WARNING:        */
339         6,              /* 0x0022       WARNING:        */
340         0               /* 0x0023-0xfffe        */
341 };
342
343 /* words 82-84: cmds/feats supported */
344 #define CMDS_W82                0x77ff  /* word 82: defined command locations*/
345 #define CMDS_W83                0x3fff  /* word 83: defined command locations*/
346 #define CMDS_W84                0x002f  /* word 83: defined command locations*/
347 #define SUPPORT_48_BIT          0x0400
348 #define NUM_CMD_FEAT_STR        48
349
350 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
351 static const char * const cmd_feat_str[] = {
352         "",                                     /* word 82 bit 15: obsolete  */
353         "NOP cmd",                              /* word 82 bit 14 */
354         "READ BUFFER cmd",                      /* word 82 bit 13 */
355         "WRITE BUFFER cmd",                     /* word 82 bit 12 */
356         "",                                     /* word 82 bit 11: obsolete  */
357         "Host Protected Area feature set",      /* word 82 bit 10 */
358         "DEVICE RESET cmd",                     /* word 82 bit  9 */
359         "SERVICE interrupt",                    /* word 82 bit  8 */
360         "Release interrupt",                    /* word 82 bit  7 */
361         "Look-ahead",                           /* word 82 bit  6 */
362         "Write cache",                          /* word 82 bit  5 */
363         "PACKET command feature set",           /* word 82 bit  4 */
364         "Power Management feature set",         /* word 82 bit  3 */
365         "Removable Media feature set",          /* word 82 bit  2 */
366         "Security Mode feature set",            /* word 82 bit  1 */
367         "SMART feature set",                    /* word 82 bit  0 */
368                                                 /* --------------*/
369         "",                                     /* word 83 bit 15: !valid bit */
370         "",                                     /* word 83 bit 14:  valid bit */
371         "FLUSH CACHE EXT command",              /* word 83 bit 13 */
372         "Mandatory FLUSH CACHE command ",       /* word 83 bit 12 */
373         "Device Configuration Overlay feature set ",
374         "48-bit Address feature set ",          /* word 83 bit 10 */
375         "",
376         "SET MAX security extension",           /* word 83 bit  8 */
377         "Address Offset Reserved Area Boot",    /* word 83 bit  7 */
378         "SET FEATURES subcommand required to spinup after power up",
379         "Power-Up In Standby feature set",      /* word 83 bit  5 */
380         "Removable Media Status Notification feature set",
381         "Adv. Power Management feature set",/* word 83 bit  3 */
382         "CFA feature set",                      /* word 83 bit  2 */
383         "READ/WRITE DMA QUEUED",                /* word 83 bit  1 */
384         "DOWNLOAD MICROCODE cmd",               /* word 83 bit  0 */
385                                                 /* --------------*/
386         "",                                     /* word 84 bit 15: !valid bit */
387         "",                                     /* word 84 bit 14:  valid bit */
388         "",                                     /* word 84 bit 13:  reserved */
389         "",                                     /* word 84 bit 12:  reserved */
390         "",                                     /* word 84 bit 11:  reserved */
391         "",                                     /* word 84 bit 10:  reserved */
392         "",                                     /* word 84 bit  9:  reserved */
393         "",                                     /* word 84 bit  8:  reserved */
394         "",                                     /* word 84 bit  7:  reserved */
395         "",                                     /* word 84 bit  6:  reserved */
396         "General Purpose Logging feature set",  /* word 84 bit  5 */
397         "",                                     /* word 84 bit  4:  reserved */
398         "Media Card Pass Through Command feature set ",
399         "Media serial number ",                 /* word 84 bit  2 */
400         "SMART self-test ",                     /* word 84 bit  1 */
401         "SMART error logging "                  /* word 84 bit  0 */
402 };
403 #endif
404
405
406 /* words 85-87: cmds/feats enabled */
407 /* use cmd_feat_str[] to display what commands and features have
408  * been enabled with words 85-87
409  */
410
411 /* words 89, 90, SECU ERASE TIME */
412 #define ERASE_BITS              0x00ff
413
414 /* word 92: master password revision */
415 /* NOVAL_0 or  NOVAL_1 means no support for master password revision */
416
417 /* word 93: hw reset result */
418 #define CBLID                   0x2000  /* CBLID status */
419 #define RST0                    0x0001  /* 1=reset to device #0 */
420 #define DEV_DET                 0x0006  /* how device num determined */
421 #define JUMPER_VAL              0x0002  /* device num determined by jumper */
422 #define CSEL_VAL                0x0004  /* device num determined by CSEL_VAL */
423
424 /* word 127: removable media status notification feature set support */
425 #define RM_STAT_BITS            0x0003
426 #define RM_STAT_SUP             0x0001
427
428 /* word 128: security */
429 #define SECU_ENABLED    0x0002
430 #define SECU_LEVEL              0x0010
431 #define NUM_SECU_STR    6
432 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
433 static const char * const secu_str[] = {
434         "supported",                    /* word 128, bit 0 */
435         "enabled",                      /* word 128, bit 1 */
436         "locked",                       /* word 128, bit 2 */
437         "frozen",                       /* word 128, bit 3 */
438         "expired: security count",      /* word 128, bit 4 */
439         "supported: enhanced erase"     /* word 128, bit 5 */
440 };
441 #endif
442
443 /* word 160: CFA power mode */
444 #define VALID_W160              0x8000  /* 1=word valid */
445 #define PWR_MODE_REQ            0x2000  /* 1=CFA power mode req'd by some cmds*/
446 #define PWR_MODE_OFF            0x1000  /* 1=CFA power moded disabled */
447 #define MAX_AMPS                0x0fff  /* value = max current in ma */
448
449 /* word 255: integrity */
450 #define SIG                     0x00ff  /* signature location */
451 #define SIG_VAL                 0x00A5  /* signature value */
452
453 #define VERSION "v5.4"
454
455 #define TIMING_MB               64
456 #define TIMING_BUF_MB           1
457 #define TIMING_BUF_BYTES        (TIMING_BUF_MB * 1024 * 1024)
458 #define TIMING_BUF_COUNT        (timing_MB / TIMING_BUF_MB)
459 #define BUFCACHE_FACTOR         2
460
461 #undef DO_FLUSHCACHE            /* under construction: force cache flush on -W0 */
462
463 /* Busybox messages and functions */
464
465 static const char bb_msg_shared_mem[] = "could not %s sharedmem buf";
466 static const char bb_msg_op_not_supp[] = " operation not supported on %s disks";
467
468 static void bb_ioctl(int fd, int request, void *argp, const char *string)
469 {
470         if (ioctl (fd, request, argp) != 0)
471                 bb_perror_msg(" %s", string);
472 }
473
474 static void if_printf(unsigned long i, char *fmt, ... )
475 {
476                 va_list ap;
477                 va_start(ap, fmt);
478                 if (i)
479                         vprintf(fmt, ap);
480                 va_end(ap);
481 }
482
483 static void on_off (unsigned int value);
484
485 static void if_printf_on_off(unsigned long get_arg,const char *fmt, unsigned long arg)
486 {
487         if (get_arg)
488         {
489                 printf(fmt, arg);
490                 on_off(arg);
491         }
492 }
493
494 static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
495                                                          const char * fmt)
496 {
497         if (ioctl (fd, request, &argp) != 0)
498                 bb_perror_msg(" %s", string);
499         else
500         {
501                 printf(fmt, (unsigned long) argp);
502                 on_off((unsigned long) argp);
503         }
504 }
505
506 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
507 static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
508 {
509                 va_list ap;
510                 va_start(ap, fmt2);
511                 if (i)
512                         vprintf(fmt1, ap);
513                 else
514                         vprintf(fmt2, ap);
515                 va_end(ap);
516 }
517
518 static void print_ascii(uint16_t *p, uint8_t length);
519
520 static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
521 {
522         if(val[i])
523         {
524                 printf("\t%-20s",string);
525                 print_ascii(&val[i], n);
526         }
527 }
528
529 static void if_strcat(unsigned long test, char *modes, char *string)
530 {
531         if (test)
532                 strcat(modes,string);
533 }
534 #endif
535
536 static void sync_and_sleep(int i)
537 {
538         sync();
539         sleep(i);
540 }
541
542 static uint16_t check_if_min_and_set_val(uint16_t a, uint16_t b)
543 {
544         if( a < b)
545                 a = b;
546         return a;
547 }
548
549 static uint16_t check_if_maj_and_set_val(uint16_t a, uint16_t b)
550 {
551         if( a > b)
552                 a = b;
553         return a;
554 }
555
556 static unsigned long int set_flag(char *p, char max)
557 {
558         if (*p >= '0' && *p <=  max )
559                 return 1;
560         return 0;
561 }
562
563 /* end of  busybox specific stuff */
564
565 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
566 static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
567 {
568         uint16_t ii;
569         uint8_t err_dma = 0;
570
571         for(ii = 0; ii <= MODE_MAX; ii++)
572         {
573                 if(mode_sel & 0x0001)
574                 {
575                         printf("*%cdma%u ",cc,ii);
576                         if(*have_mode)
577                                 err_dma = 1;
578                         *have_mode = 1;
579                 }
580                 else if(mode_sup & 0x0001)
581                         printf("%cdma%u ",cc,ii);
582
583                 mode_sup >>=1;
584                 mode_sel >>=1;
585         }
586         return err_dma;
587 }
588
589 static void print_ascii(uint16_t *p, uint8_t length) {
590         uint8_t ii;
591         char cl;
592
593         /* find first non-space & print it */
594         for(ii = 0; ii< length; ii++)
595         {
596                 if(((char) 0x00ff&((*p)>>8)) != ' ')
597                         break;
598                 if((cl = (char) 0x00ff&(*p)) != ' ')
599                 {
600                         if_printf((cl != '\0'),"%c",cl);
601                         p++;
602                         ii++;
603                         break;
604                 }
605                 p++;
606         }
607         /* print the rest */
608         for(; ii< length; ii++)
609         {
610                 if(!(*p))
611                         break; /* some older devices have NULLs */
612                 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
613                 p++;
614         }
615         printf("\n");
616 }
617
618 /* identify() is the only extern function used across two source files.  The
619    others, though, were declared in hdparm.c with global scope; since other
620    functions in that file have static (file) scope, I assume the difference is
621    intentional. */
622 static void identify (uint16_t *id_supplied, const char *devname)
623 {
624
625         char *id_file = NULL;
626         FILE *fl;
627         uint16_t val[256], ii, jj, kk;
628         uint16_t like_std = 1, std = 0, min_std = 0xffff;
629         uint16_t dev = NO_DEV, eqpt = NO_DEV;
630         uint8_t  have_mode = 0, err_dma = 0;
631         uint8_t  chksum = 0;
632         uint32_t ll, mm, nn, oo;
633         uint64_t bbbig; /* (:) */
634
635         if (id_supplied)
636         {
637 #if __BYTE_ORDER == __BIG_ENDIAN
638                 swab(id_supplied, val, sizeof(val));
639 #else
640                 memcpy(val, id_supplied, sizeof(val));
641 #endif
642         }
643         else
644         {
645                 /* open the file, read in all the info and close it */
646                 if (devname == NULL)
647                         fl = stdin;
648                 else {
649                         id_file = bb_xasprintf("/proc/ide/%s/identify", devname);
650                         fl = bb_xfopen(id_file, "r");
651                 }
652                 /* calculate checksum over all bytes */
653                 for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
654                 {
655                         unsigned int scratch;
656                         if(1 != fscanf(fl,"%04x",&scratch))
657                                 break;
658                         val[ii] = (uint16_t)scratch;
659                         chksum += val[ii] + (val[ii] >> 8);
660                 }
661                 bb_fclose_nonstdin(fl);
662                 if (ENABLE_FEATURE_CLEAN_UP) free(id_file);
663                 if(ii < (INTEGRITY+1))
664                         bb_error_msg_and_die("Input file wrong format or length");
665         }
666         chksum &= 0xff;
667
668         /* check if we recognise the device type */
669         printf("\n");
670         if(!(val[GEN_CONFIG] & NOT_ATA))
671         {
672                 dev = ATA_DEV;
673                 printf("ATA device, with ");
674         }
675         else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
676         {
677                 dev = ATA_DEV;
678                 like_std = 4;
679                 printf("CompactFlash ATA device, with ");
680         }
681         else if(!(val[GEN_CONFIG] & NOT_ATAPI))
682         {
683                 dev = ATAPI_DEV;
684                 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
685                 printf("ATAPI %s, with ", pkt_str[eqpt]);
686                 like_std = 3;
687         }
688         else
689                 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
690                 bb_error_msg_and_die("Unknown device type");
691
692         if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
693         printf("removable media\n");
694
695         /* Info from the specific configuration word says whether or not the
696          * ID command completed correctly.  It is only defined, however in
697          * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
698          * standards.  Since the values allowed for this word are extremely
699          * specific, it should be safe to check it now, even though we don't
700          * know yet what standard this device is using.
701          */
702         if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
703            (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
704         {
705                 like_std = 5;
706                 if_printf(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)),
707                                         "powers-up in standby; SET FEATURES subcmd spins-up.\n");
708                 if_printf((((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)),
709                                         "\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
710         }
711
712         /* output the model and serial numbers and the fw revision */
713         xprint_ascii(val, START_MODEL,  "Model Number:",        LENGTH_MODEL);
714         xprint_ascii(val, START_SERIAL, "Serial Number:",       LENGTH_SERIAL);
715         xprint_ascii(val, START_FW_REV, "Firmware Revision:",   LENGTH_FW_REV);
716         xprint_ascii(val, START_MEDIA,  "Media Serial Num:",    LENGTH_MEDIA);
717         xprint_ascii(val, START_MANUF,  "Media Manufacturer:",  LENGTH_MANUF);
718
719         /* major & minor standards version number (Note: these words were not
720          * defined until ATA-3 & the CDROM std uses different words.) */
721         printf("Standards:");
722         if(eqpt != CDROM)
723         {
724                 if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
725                 {
726                         like_std=check_if_min_and_set_val(like_std, 3);
727                         std = actual_ver[val[MINOR]];
728                         if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);
729
730                 }
731                 /* looks like when they up-issue the std, they obsolete one;
732                  * thus, only the newest 4 issues need be supported. (That's
733                  * what "kk" and "min_std" are all about.) */
734                 if(val[MAJOR] && (val[MAJOR] !=NOVAL_1))
735                 {
736                         printf("\n\tSupported: ");
737                         jj = val[MAJOR] << 1;
738                         kk = like_std >4 ? like_std-4: 0;
739                         for(ii = 14; (ii >0)&&(ii>kk); ii--)
740                         {
741                                 if(jj & 0x8000)
742                                 {
743                                         printf("%u ", ii);
744                                         if(like_std < ii)
745                                         {
746                                                 like_std = ii;
747                                                 kk = like_std >4 ? like_std-4: 0;
748                                         }
749                                         min_std=check_if_maj_and_set_val(min_std, ii);
750                                 }
751                                 jj <<= 1;
752                         }
753                         like_std=check_if_min_and_set_val(like_std, 3);
754                 }
755                 /* Figure out what standard the device is using if it hasn't told
756                  * us.  If we know the std, check if the device is using any of
757                  * the words from the next level up.  It happens.
758                  */
759                 like_std=check_if_min_and_set_val(like_std, std);
760
761                 if(((std == 5) || (!std && (like_std < 6))) &&
762                         ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
763                         ((      val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
764                         (((     val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
765                         (       val[CMDS_SUPP_2] & CMDS_W84) ) ) )
766                 {
767                         like_std = 6;
768                 }
769                 else if(((std == 4) || (!std && (like_std < 5))) &&
770                         ((((val[INTEGRITY]      & SIG) == SIG_VAL) && !chksum) ||
771                         ((      val[HWRST_RSLT] & VALID) == VALID_VAL) ||
772                         (((     val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
773                         ((      val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
774                 {
775                         like_std = 5;
776                 }
777                 else if(((std == 3) || (!std && (like_std < 4))) &&
778                                 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
779                                 (((     val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
780                                 ((      val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
781                                 ((      val[CAPAB_1] & VALID) == VALID_VAL) ||
782                                 ((      val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
783                                 ((      val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
784                 {
785                         like_std = 4;
786                 }
787                 else if(((std == 2) || (!std && (like_std < 3))) &&
788                            ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
789                 {
790                         like_std = 3;
791                 }
792                 else if(((std == 1) || (!std && (like_std < 2))) &&
793                                 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
794                                 (val[WHATS_VALID] & OK_W64_70)) )
795                 {
796                         like_std = 2;
797                 }
798                 if(!std)
799                         printf("\n\tLikely used: %u\n",like_std);
800                 else if(like_std > std)
801                         printf("& some of %u\n",like_std);
802                 else
803                         printf("\n");
804         }
805         else
806         {
807                 /* TBD: do CDROM stuff more thoroughly.  For now... */
808                 kk = 0;
809                 if(val[CDR_MINOR] == 9)
810                 {
811                         kk = 1;
812                         printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
813                 }
814                 if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
815                 {
816                         kk = 1;
817                         printf("\n\tSupported: CD-ROM ATAPI");
818                         jj = val[CDR_MAJOR] >> 1;
819                         for(ii = 1; ii <15; ii++)
820                         {
821                                 if_printf((jj & 0x0001),"-%u ", ii);
822                                 jj >>= 1;
823                         }
824                 }
825                 if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
826                 /* the cdrom stuff is more like ATA-2 than anything else, so: */
827                 like_std = 2;
828         }
829
830         if(min_std == 0xffff)
831                 min_std = like_std > 4 ? like_std - 3 : 1;
832
833         printf("Configuration:\n");
834         /* more info from the general configuration word */
835         if((eqpt != CDROM) && (like_std == 1))
836         {
837                 jj = val[GEN_CONFIG] >> 1;
838                 for(ii = 1; ii < 15; ii++)
839                 {
840                         if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
841                         jj >>=1;
842                 }
843         }
844         if(dev == ATAPI_DEV)
845         {
846                 printf("\tDRQ response: "); /* Data Request (DRQ) */
847                 switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
848                 {
849                         case DRQ_3MS_VAL :
850                                 printf("3ms.\n");
851                                 break;
852                         case DRQ_INTR_VAL :
853                                 printf("<=10ms with INTRQ\n");
854                                 break;
855                         case DRQ_50US_VAL :
856                                 printf("50us.\n");
857                                 break;
858                         default :
859                                 printf("unknown.\n");
860                                 break;
861                 }
862                 printf("\tPacket size: ");
863                 switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
864                 {
865                         case PKT_SIZE_12_VAL :
866                                 printf("12 bytes\n");
867                                 break;
868                         case PKT_SIZE_16_VAL :
869                                 printf("16 bytes\n");
870                                 break;
871                         default :
872                                 printf("Unknown\n");
873                                 break;
874                 }
875         }
876         else
877         {
878                 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
879                 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
880                 mm = 0; bbbig = 0;
881                 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
882                         printf("\tCHS addressing not supported\n");
883                 else
884                 {
885                         jj = val[WHATS_VALID] & OK_W54_58;
886                         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",
887                                         val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
888
889                         if_printf(((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])),
890                                                 "\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
891
892                         if(jj)
893                         {
894                                 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
895                                 if(like_std < 3)
896                                 {
897                                          /* check Endian of capacity bytes */
898                                         nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
899                                         oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
900                                         if(abs(mm - nn) > abs(oo - nn))
901                                                 mm = oo;
902                                 }
903                                 printf("\tCHS current addressable sectors:%11u\n",mm);
904                         }
905                 }
906                 /* LBA addressing */
907                 printf("\tLBA    user addressable sectors:%11u\n",ll);
908                 if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
909                      (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
910                 {
911                         bbbig = (uint64_t)val[LBA_64_MSB]       << 48 |
912                                 (uint64_t)val[LBA_48_MSB]       << 32 |
913                                 (uint64_t)val[LBA_MID]  << 16 |
914                                         val[LBA_LSB] ;
915                         printf("\tLBA48  user addressable sectors:%11llu\n",bbbig);
916                 }
917
918                 if (!bbbig)
919                         bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
920                 printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
921                 bbbig = (bbbig<<9)/1000000;
922                 printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);
923
924                 if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
925
926         }
927
928         /* hw support of commands (capabilities) */
929         printf("Capabilities:\n\t");
930
931         if(dev == ATAPI_DEV)
932         {
933                 if(eqpt != CDROM)
934                         if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");
935
936                 if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
937         }
938         if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
939
940         if(like_std != 1)
941         {
942                 printf("IORDY");
943                 if_printf((!(val[CAPAB_0] & IORDY_SUP)),"(may be)");
944                 if_else_printf((val[CAPAB_0] & IORDY_OFF),"(can","(cannot");
945                 printf(" be disabled)\n");
946         }
947         else
948                 printf("no IORDY\n");
949
950         if((like_std == 1) && val[BUF_TYPE])
951         {
952                 kk = val[BUF_TYPE];
953                 printf("\tBuffer type: %04x: ",kk);
954                 if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
955                 if_printf((kk > 2)," with read caching ability");
956                 printf("\n");
957         }
958         jj = 0;
959         if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
960         {
961                 printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
962                 jj = 1;
963         }
964         if((min_std < 4) && (val[RW_LONG]))
965         {
966                 printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
967                 jj = 1;
968         }
969         if((eqpt != CDROM) && (like_std > 3))
970         {
971                 printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
972                 jj = 1;
973         }
974         if_printf(jj,"\n");
975
976         if(dev == ATA_DEV)
977         {
978                 if(like_std == 1)
979                         printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
980                 else
981                 {
982                         printf("\tStandby timer values: spec'd by ");
983                         if_else_printf((val[CAPAB_0] & STD_STBY),"Standard","Vendor");
984                         if((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
985                                 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
986                         else
987                           printf("\n");
988                 }
989                 printf("\tR/W multiple sector transfer: ");
990                 if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
991                         printf("not supported\n");
992                 else
993                 {
994                         printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
995                         if_else_printf((val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID),
996                                                         "%u\n","?\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
997                 }
998                 if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
999                 {
1000                         /* We print out elsewhere whether the APM feature is enabled or
1001                            not.  If it's not enabled, let's not repeat the info; just print
1002                            nothing here. */
1003                         printf("\tAdvancedPM level: ");
1004                         if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
1005                         {
1006                                 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
1007                                 printf("%u (0x%x)\n", apm_level, apm_level);
1008                         }
1009                         else
1010                                 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
1011                 }
1012                 if(like_std > 5)
1013                 {
1014                         if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
1015                                                                         (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
1016                 }
1017         }
1018         else
1019         {
1020                  /* ATAPI */
1021                 if(eqpt != CDROM)
1022                         if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");
1023
1024                 if(val[PKT_REL] || val[SVC_NBSY])
1025                 {
1026                         printf("\tOverlap support:");
1027                         if_printf(val[PKT_REL]," %uus to release bus.",val[PKT_REL]);
1028                         if_printf(val[SVC_NBSY]," %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
1029                         printf("\n");
1030                 }
1031         }
1032
1033         /* DMA stuff. Check that only one DMA mode is selected. */
1034         printf("\tDMA: ");
1035         if(!(val[CAPAB_0] & DMA_SUP))
1036                 printf("not supported\n");
1037         else
1038         {
1039                 if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
1040                                         " sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
1041                 if(val[SINGLE_DMA])
1042                 {
1043                         jj = val[SINGLE_DMA];
1044                         kk = val[SINGLE_DMA] >> 8;
1045                         err_dma += mode_loop(jj,kk,'s',&have_mode);
1046                 }
1047                 if(val[MULTI_DMA])
1048                 {
1049                         jj = val[MULTI_DMA];
1050                         kk = val[MULTI_DMA] >> 8;
1051                         err_dma += mode_loop(jj,kk,'m',&have_mode);
1052                 }
1053                 if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
1054                 {
1055                         jj = val[ULTRA_DMA];
1056                         kk = val[ULTRA_DMA] >> 8;
1057                         err_dma += mode_loop(jj,kk,'u',&have_mode);
1058                 }
1059                 if_printf((err_dma || !have_mode),"(?)");
1060                 printf("\n");
1061
1062                 if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
1063                                         "\t     Interleaved DMA support\n");
1064
1065                 if((val[WHATS_VALID] & OK_W64_70) &&
1066                    (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
1067                 {
1068                         printf("\t     Cycle time:");
1069                         if_printf(val[DMA_TIME_MIN]," min=%uns",val[DMA_TIME_MIN]);
1070                         if_printf(val[DMA_TIME_NORM]," recommended=%uns",val[DMA_TIME_NORM]);
1071                         printf("\n");
1072                 }
1073         }
1074
1075         /* Programmed IO stuff */
1076         printf("\tPIO: ");
1077         /* If a drive supports mode n (e.g. 3), it also supports all modes less
1078          * than n (e.g. 3, 2, 1 and 0).  Print all the modes. */
1079         if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
1080         {
1081                 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
1082                 for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
1083                 {
1084                         if_printf((jj & 0x0001),"pio%d ",ii);
1085                         jj >>=1;
1086                 }
1087                 printf("\n");
1088         }
1089         else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
1090         {
1091                 for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
1092                         printf("pio%d ",ii);
1093                 printf("\n");
1094         }
1095         else
1096                 printf("unknown\n");
1097
1098         if(val[WHATS_VALID] & OK_W64_70)
1099         {
1100                 if(val[PIO_NO_FLOW] || val[PIO_FLOW])
1101                 {
1102                         printf("\t     Cycle time:");
1103                         if_printf(val[PIO_NO_FLOW]," no flow control=%uns", val[PIO_NO_FLOW]);
1104                         if_printf(val[PIO_FLOW],"  IORDY flow control=%uns", val[PIO_FLOW]);
1105                         printf("\n");
1106                 }
1107         }
1108
1109         if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1110         {
1111                 printf("Commands/features:\n\tEnabled\tSupported:\n");
1112                 jj = val[CMDS_SUPP_0];
1113                 kk = val[CMDS_EN_0];
1114                 for(ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
1115                 {
1116                         if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
1117                         {
1118                                 if_else_printf((kk & 0x8000),"\t   *","\t");
1119                                 printf("\t%s\n",cmd_feat_str[ii]);
1120                         }
1121                         jj <<=1; kk<<=1;
1122                         if(ii%16 == 15)
1123                         {
1124                                 jj = val[CMDS_SUPP_0+1+(ii/16)];
1125                                 kk = val[CMDS_EN_0+1+(ii/16)];
1126                         }
1127                         if(ii == 31)
1128                         {
1129                                 if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1130                                         ii +=16;
1131                         }
1132                 }
1133         }
1134         if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
1135                                 "\tRemovable Media Status Notification feature set supported\n");
1136
1137
1138         /* security */
1139         if((eqpt != CDROM) && (like_std > 3) &&
1140            (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1141         {
1142                 printf("Security: \n");
1143                 if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
1144                                         "\tMaster password revision code = %u\n",val[PSWD_CODE]);
1145                 jj = val[SECU_STATUS];
1146                 if(jj)
1147                 {
1148                         for(ii = 0; ii < NUM_SECU_STR; ii++)
1149                         {
1150                                 if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
1151                                 jj >>=1;
1152                         }
1153                         if(val[SECU_STATUS] & SECU_ENABLED)
1154                         {
1155                                 printf("\tSecurity level ");
1156                                 if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
1157                         }
1158                 }
1159                 jj =  val[ERASE_TIME]     & ERASE_BITS;
1160                 kk =  val[ENH_ERASE_TIME] & ERASE_BITS;
1161                 if(jj || kk)
1162                 {
1163                         printf("\t");
1164                         if_printf(jj,"%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
1165                         if_printf(kk,"%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
1166                         printf("\n");
1167                 }
1168         }
1169
1170         /* reset result */
1171         if((val[HWRST_RSLT] & VALID) == VALID_VAL)
1172         {
1173                 printf("HW reset results:\n");
1174                 if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
1175
1176                 if(val[HWRST_RSLT] & RST0)
1177                 {
1178                         printf("\tDevice num = 0");
1179                         jj = val[HWRST_RSLT];
1180                 }
1181                 else
1182                 {
1183                         printf("\tDevice num = 1");
1184                         jj = val[HWRST_RSLT] >> 8;
1185                 }
1186
1187                 if((jj & DEV_DET) == JUMPER_VAL)
1188                         printf(" determined by the jumper");
1189                 else if((jj & DEV_DET) == CSEL_VAL)
1190                         printf(" determined by CSEL");
1191                 printf("\n");
1192         }
1193
1194         /* more stuff from std 5 */
1195         if((like_std > 4) && (eqpt != CDROM))
1196         {
1197                 if(val[CFA_PWR_MODE] & VALID_W160)
1198                 {
1199                         printf("CFA power mode 1:\n\t");
1200                         if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");
1201
1202                         if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
1203                         printf("\n");
1204
1205                         if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
1206                 }
1207                 if((val[INTEGRITY] & SIG) == SIG_VAL)
1208                 {
1209                         printf("Checksum: ");
1210                         if_printf(chksum,"in");
1211                         printf("correct\n");
1212                 }
1213         }
1214
1215         exit(0);
1216 }
1217 #endif
1218
1219 static int verbose, get_identity, get_geom, noisy = 1, quiet;
1220 static int flagcount, do_flush, is_scsi_hd, is_xt_hd;
1221 static int do_ctimings, do_timings;
1222
1223 static unsigned long set_readahead, get_readahead, Xreadahead;
1224 static unsigned long set_readonly, get_readonly, readonly;
1225 static unsigned long set_unmask, get_unmask, unmask;
1226 static unsigned long set_mult, get_mult, mult;
1227 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1228 static unsigned long set_dma, get_dma, dma;
1229 #endif
1230 static unsigned long set_dma_q, get_dma_q, dma_q;
1231 static unsigned long set_nowerr, get_nowerr, nowerr;
1232 static unsigned long set_keep, get_keep, keep;
1233 static unsigned long set_io32bit, get_io32bit, io32bit;
1234 static unsigned long set_piomode, noisy_piomode;
1235 static int piomode;
1236 #ifdef HDIO_DRIVE_CMD
1237 static unsigned long set_dkeep, get_dkeep, dkeep;
1238 static unsigned long set_standby, get_standby, standby_requested;
1239 static unsigned long set_xfermode, get_xfermode;
1240 static int xfermode_requested;
1241 static unsigned long set_lookahead, get_lookahead, lookahead;
1242 static unsigned long set_prefetch, get_prefetch, prefetch;
1243 static unsigned long set_defects, get_defects, defects;
1244 static unsigned long set_wcache, get_wcache, wcache;
1245 static unsigned long set_doorlock, get_doorlock, doorlock;
1246 static unsigned long set_seagate, get_seagate;
1247 static unsigned long set_standbynow, get_standbynow;
1248 static unsigned long set_sleepnow, get_sleepnow;
1249 static unsigned long get_powermode;
1250 static unsigned long set_apmmode, get_apmmode, apmmode;
1251 #endif
1252 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1253 static int get_IDentity;
1254 #endif
1255 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1256 static int      unregister_hwif;
1257 static int      hwif;
1258 #endif
1259 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1260 static int      scan_hwif;
1261 static int      hwif_data;
1262 static int      hwif_ctrl;
1263 static int      hwif_irq;
1264 #endif
1265 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1266 static int      set_busstate, get_busstate, busstate;
1267 #endif
1268 static int      reread_partn;
1269
1270 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1271 static int      perform_reset;
1272 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1273 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1274 static int      perform_tristate,       tristate;
1275 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1276
1277 // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1278 // then the HDIO_GET_IDENTITY only returned 142 bytes.
1279 // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1280 // and HDIO_GET_IDENTITY returns 512 bytes.  But the latest
1281 // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1282 // (which they should, but they should just return -EINVAL).
1283 //
1284 // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1285 // On a really old system, it will not, and we will be confused.
1286 // Too bad, really.
1287
1288 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1289 static const char * const cfg_str[] =
1290 {       "",          " HardSect",   " SoftSect",  " NotMFM",
1291         " HdSw>15uSec", " SpinMotCtl", " Fixed",     " Removeable",
1292         " DTR<=5Mbs",   " DTR>5Mbs",   " DTR>10Mbs", " RotSpdTol>.5%",
1293         " dStbOff",     " TrkOff",     " FmtGapReq", " nonMagnetic"
1294 };
1295
1296 static const char * const BuffType[]    = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1297
1298 static void dump_identity (const struct hd_driveid *id)
1299 {
1300         int i;
1301         char pmodes[64], dmodes[128], umodes[128];
1302         const unsigned short int *id_regs= (const void*) id;
1303         unsigned long capacity;
1304
1305         pmodes[0] = dmodes[0] = umodes[0] = '\0';
1306
1307         printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1308                                 id->model, id->fw_rev, id->serial_no);
1309         for (i=0; i<=15; i++)
1310                 if_printf((id->config & (1<<i)),"%s", cfg_str[i]);
1311
1312         printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
1313                                 id->cyls, id->heads, id->sectors, id->track_bytes,
1314                                 id->sector_bytes, id->ecc_bytes);
1315
1316         if (id->buf_type > 3)
1317                 printf("%s%u", " BuffType=", id->buf_type);
1318         else
1319                 printf("%s%s", " BuffType=", BuffType[id->buf_type]);
1320
1321         printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
1322         if (id->max_multsect)
1323         {
1324                 printf(", MultSect=");
1325                 if (!(id->multsect_valid&1))
1326                         printf("?%u?", id->multsect);
1327                 else if (id->multsect)
1328                         printf("%u", id->multsect);
1329                 else
1330                         printf("off");
1331         }
1332         printf("\n");
1333         if (id->tPIO <= 5)
1334         {
1335                 strcat(pmodes, "pio0 ");
1336                 if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
1337                 if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
1338
1339         }
1340         if_printf((!(id->field_valid&1))," (maybe):");
1341 #if __BYTE_ORDER == __BIG_ENDIAN
1342         capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1343 #else
1344         capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1345 #endif
1346         printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1347                                                                                                         id->cur_sectors, capacity ,
1348                                                                                         ((id->capability&2)==0)?"no":"yes");
1349
1350         if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);
1351
1352         if (id->capability&1)
1353         {
1354                 if (id->dma_1word | id->dma_mword)
1355                 {
1356                         if_strcat((id->dma_1word & 0x100),      dmodes, "*");
1357                         if_strcat((id->dma_1word & 1),          dmodes, "sdma0 ");
1358                         if_strcat((id->dma_1word & 0x200),      dmodes, "*");
1359                         if_strcat((id->dma_1word & 2),          dmodes, "sdma1 ");
1360                         if_strcat((id->dma_1word & 0x400),      dmodes, "*");
1361                         if_strcat((id->dma_1word & 4),          dmodes, "sdma2 ");
1362                         if_strcat((id->dma_1word & 0xf800),     dmodes, "*");
1363                         if_strcat((id->dma_1word & 0xf8),       dmodes, "sdma? ");
1364                         if_strcat((id->dma_mword & 0x100),      dmodes, "*");
1365                         if_strcat((id->dma_mword & 1),          dmodes, "mdma0 ");
1366                         if_strcat((id->dma_mword & 0x200),      dmodes, "*");
1367                         if_strcat((id->dma_mword & 2),          dmodes, "mdma1 ");
1368                         if_strcat((id->dma_mword & 0x400),      dmodes, "*");
1369                         if_strcat((id->dma_mword & 4),          dmodes, "mdma2 ");
1370                         if_strcat((id->dma_mword & 0xf800),     dmodes, "*");
1371                         if_strcat((id->dma_mword & 0xf8),       dmodes, "mdma? ");
1372                 }
1373         }
1374         printf("\n IORDY=");
1375         if (id->capability&8)
1376                 printf((id->capability&4) ? "on/off" : "yes");
1377         else
1378                 printf("no");
1379
1380         if ((id->capability&8) || (id->field_valid&2))
1381         {
1382                 if (id->field_valid&2)
1383                 {
1384                         printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1385                         if_strcat((id->eide_pio_modes & 1), pmodes, "pio3 ");
1386                         if_strcat((id->eide_pio_modes & 2), pmodes, "pio4 ");
1387                         if_strcat((id->eide_pio_modes &~3), pmodes, "pio? ");
1388                 }
1389                 if (id->field_valid&4)
1390                 {
1391                         if_strcat((id->dma_ultra & 0x100),umodes,"*");
1392                         if_strcat((id->dma_ultra & 0x001),umodes,"udma0 ");
1393                         if_strcat((id->dma_ultra & 0x200),umodes,"*");
1394                         if_strcat((id->dma_ultra & 0x002),umodes,"udma1 ");
1395                         if_strcat((id->dma_ultra & 0x400),umodes,"*");
1396                         if_strcat((id->dma_ultra & 0x004),umodes,"udma2 ");
1397 #ifdef __NEW_HD_DRIVE_ID
1398                         if (id->hw_config & 0x2000)
1399                         {
1400 #else /* !__NEW_HD_DRIVE_ID */
1401                         if (id->word93 & 0x2000)
1402                         {
1403 #endif /* __NEW_HD_DRIVE_ID */
1404                                 if_strcat((id->dma_ultra & 0x0800),umodes,"*");
1405                                 if_strcat((id->dma_ultra & 0x0008),umodes,"udma3 ");
1406                                 if_strcat((id->dma_ultra & 0x1000),umodes,"*");
1407                                 if_strcat((id->dma_ultra & 0x0010),umodes,"udma4 ");
1408                                 if_strcat((id->dma_ultra & 0x2000),umodes,"*");
1409                                 if_strcat((id->dma_ultra & 0x0020),umodes,"udma5 ");
1410                                 if_strcat((id->dma_ultra & 0x4000),umodes,"*");
1411                                 if_strcat((id->dma_ultra & 0x0040),umodes,"udma6 ");
1412                                 if_strcat((id->dma_ultra & 0x8000),umodes,"*");
1413                                 if_strcat((id->dma_ultra & 0x0080),umodes,"udma7 ");
1414                         }
1415                 }
1416         }
1417         if_printf(((id->capability&1) && (id->field_valid&2)),
1418                                 ", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1419         printf("\n PIO modes:  %s", pmodes);
1420         if_printf((*dmodes),"\n DMA modes:  %s", dmodes);
1421         if_printf((*umodes),"\n UDMA modes: %s", umodes);
1422
1423         printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1424         if (id_regs[83] & 8)
1425         {
1426                 if (!(id_regs[86]&8))
1427                         printf(": disabled (255)");
1428                 else if ((id_regs[91]&0xFF00)!=0x4000)
1429                         printf(": unknown setting");
1430                 else
1431                         printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1432         }
1433         if_printf( (id_regs[82]&0x20)," WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1434 #ifdef __NEW_HD_DRIVE_ID
1435         if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1436         {
1437                 printf("\n Drive conforms to: ");
1438                 if_else_printf((id->minor_rev_num <= 31),"%s: ","unknown: ", minor_str[id->minor_rev_num]);
1439                 if (id->major_rev_num != 0x0000 &&  /* NOVAL_0 */
1440                     id->major_rev_num != 0xFFFF) {  /* NOVAL_1 */
1441                         for (i=0; i <= 15; i++)
1442                                 if_printf((id->major_rev_num & (1<<i))," ATA/ATAPI-%u", i);
1443                 }
1444         }
1445 #endif /* __NEW_HD_DRIVE_ID */
1446         printf("\n\n * signifies the current active mode\n\n");
1447 }
1448 #endif
1449
1450 static void flush_buffer_cache (int fd)
1451 {
1452         fsync (fd);                             /* flush buffers */
1453         bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
1454 #ifdef HDIO_DRIVE_CMD
1455         sleep(1);
1456         if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1457                         bb_perror_msg("HDIO_DRIVE_CMD");
1458 #endif
1459 }
1460
1461 static int seek_to_zero (int fd)
1462 {
1463         if (lseek(fd, (off_t) 0, SEEK_SET))
1464                 return 1;
1465         return 0;
1466 }
1467
1468 static int read_big_block (int fd, char *buf)
1469 {
1470
1471         const char *string;
1472         int i, rc;
1473         if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
1474         {
1475                 switch(rc)
1476                 {
1477                         case -1:
1478                                 string = "read()";
1479                                 break;
1480                         case  0:
1481                                 string = "read() hit EOF - device too small";
1482                                 break;
1483                         default:
1484                                 string = "read(%u) returned %u bytes";
1485                 }
1486                 bb_error_msg(string, TIMING_BUF_BYTES, rc);
1487                 return 1;
1488         }
1489
1490         /* access all sectors of buf to ensure the read fully completed */
1491         for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1492                 buf[i] &= 1;
1493         return 0;
1494 }
1495
1496 static double correction;
1497
1498 static void do_time (int flag, int fd)
1499 /*
1500         flag = 0 time_cache
1501         flag = 1 time_device
1502 */
1503 {
1504         int i;
1505         char *buf;
1506         double elapsed;
1507         struct itimerval e1, e2;
1508         int shmid;
1509         int timing_MB = TIMING_MB;
1510
1511         if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
1512         {
1513                 bb_error_msg (bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
1514                 return;
1515         }
1516         if (shmctl(shmid, SHM_LOCK, NULL) == -1)
1517         {
1518                 bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
1519                 (void) shmctl(shmid, IPC_RMID, NULL);
1520                 return;
1521         }
1522         if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
1523         {
1524                 bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
1525                 (void) shmctl(shmid, IPC_RMID, NULL);
1526                 return;
1527         }
1528         if (shmctl(shmid, IPC_RMID, NULL) == -1)
1529                 bb_error_msg ("shmctl(,IPC_RMID,)");
1530
1531         /* Clear out the device request queues & give them time to complete */
1532         sync_and_sleep(3);
1533
1534         if(flag  == 0) /* Time cache */
1535         {
1536                 /* Calculate a correction factor for the basic
1537                 * overhead of doing a read() from the buffer cache.
1538                 * To do this, we read the data once to "cache it" and
1539                 * to force full preallocation of our timing buffer,
1540                 * and then we re-read it 10 times while timing it.
1541                 *
1542                 * getitimer() is used rather than gettimeofday() because
1543                 * it is much more consistent (on my machine, at least).
1544                 */
1545                 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1546                 if (seek_to_zero (fd))
1547                         return;
1548                 if (read_big_block (fd, buf))
1549                         return;
1550                 printf(" Timing buffer-cache reads:   ");
1551                 fflush(stdout);
1552
1553                 /* Clear out the device request queues & give them time to complete */
1554                 sync_and_sleep(1);
1555
1556                 /* Time re-reading from the buffer-cache */
1557                 getitimer(ITIMER_REAL, &e1);
1558                 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1559                 {
1560                         if (seek_to_zero (fd))
1561                                 goto quit;
1562                         if (read_big_block (fd, buf))
1563                                 goto quit;
1564                 }
1565                 getitimer(ITIMER_REAL, &e2);
1566                 correction = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1567
1568                 /* Now remove the lseek() from the correction factor */
1569                 getitimer(ITIMER_REAL, &e1);
1570                 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1571                 {
1572                         if (seek_to_zero (fd))
1573                                 goto quit;
1574                 }
1575                 getitimer(ITIMER_REAL, &e2);
1576                 correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
1577                 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1578
1579                 if ((BUFCACHE_FACTOR * timing_MB) >= correction)  /* more than 1MB/s */
1580                         printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1581                                 (BUFCACHE_FACTOR * timing_MB),  correction,
1582                                 (BUFCACHE_FACTOR * timing_MB) / correction);
1583                 else
1584                         printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1585                                 (BUFCACHE_FACTOR * timing_MB),  correction,
1586                                 (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
1587                 correction /= BUFCACHE_FACTOR;
1588
1589                 flush_buffer_cache(fd);
1590                 sleep(1);
1591         }
1592         else /* Time device */
1593         {
1594                 printf(" Timing buffered disk reads:  ");
1595                 fflush(stdout);
1596
1597                 /*
1598                 * getitimer() is used rather than gettimeofday() because
1599                 * it is much more consistent (on my machine, at least).
1600                 */
1601                 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1602
1603                 /* Now do the timings for real */
1604                 getitimer(ITIMER_REAL, &e1);
1605                 for (i = TIMING_BUF_COUNT; i > 0; --i)
1606                 {
1607                         if (read_big_block (fd, buf))
1608                                 goto quit;
1609                 }
1610                 getitimer(ITIMER_REAL, &e2);
1611
1612                 elapsed =       (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1613
1614                 if (timing_MB >= elapsed)  /* more than 1MB/s */
1615                         printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",timing_MB, elapsed, timing_MB / elapsed);
1616                 else
1617                         printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);
1618
1619                 /*"Hmm.. suspicious results: probably not enough free memory for a proper test.");*/
1620                 if (elapsed <= (correction * 2))
1621                                         bb_error_msg(bb_msg_memory_exhausted);
1622
1623 #if 0  /* the "estimate" is just plain wrong for many systems.. */
1624                 else if (correction != 0.0) {
1625                         printf(" Estimating raw driver speed: ");
1626                         elapsed -= correction;
1627                         if (timing_MB >= elapsed)  /* more than 1MB/s */
1628                                 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1629                                         timing_MB, elapsed, timing_MB / elapsed);
1630                         else
1631                                 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1632                                         timing_MB, elapsed, timing_MB / elapsed * 1024);
1633                 }
1634 #endif
1635         }
1636 quit:
1637         if (-1 == shmdt(buf))
1638                 bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
1639 }
1640
1641
1642 static void no_scsi (void)
1643 {
1644         /*" operation not supported on SCSI disks"*/
1645         if (is_scsi_hd)
1646                 bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
1647 }
1648
1649 static void no_xt (void)
1650 {
1651         if (is_xt_hd)
1652                 bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
1653 }
1654
1655 static void no_scsi_no_xt (void)
1656 {
1657         no_scsi();
1658         no_xt();
1659 }
1660
1661 static void on_off (unsigned int value)
1662 {
1663         printf(value ? " (on)\n" : " (off)\n");
1664 }
1665
1666 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1667 static void bus_state_value (unsigned int value)
1668 {
1669         const char *string;
1670
1671         switch (value)
1672         {
1673                 case BUSSTATE_ON:
1674                         string = " (on)\n";
1675                         break;
1676                 case BUSSTATE_OFF:
1677                         string = " (off)\n";
1678                         break;
1679                 case BUSSTATE_TRISTATE:
1680                         string = " (tristate)\n";
1681                         break;
1682                 default:
1683                         string = " (unknown: %d)\n";
1684                         break;
1685         }
1686         printf(string, value);
1687 }
1688 #endif
1689
1690 #ifdef HDIO_DRIVE_CMD
1691 static void interpret_standby (unsigned int standby)
1692 {
1693         printf(" (");
1694         switch(standby)
1695         {
1696                 case 0:
1697                         printf("off");
1698                         break;
1699                 case 252:
1700                         printf("21 minutes");
1701                         break;
1702                 case 253:
1703                         printf("vendor-specific");
1704                         break;
1705                 case 254:
1706                         printf("?reserved");
1707                         break;
1708                 case 255:
1709                         printf("21 minutes + 15 seconds");
1710                         break;
1711                 default:
1712                         if (standby <= 240)
1713                         {
1714                                 unsigned int secs = standby * 5;
1715                                 unsigned int mins = secs / 60;
1716                                 secs %= 60;
1717                                 if_printf(mins,"%u minutes", mins);
1718                                 if_printf((mins && secs)," + ");
1719                                 if_printf(secs,"%u seconds", secs);
1720                         }
1721                         else if (standby <= 251)
1722                         {
1723                                 unsigned int mins = (standby - 240) * 30;
1724                                 unsigned int hrs  = mins / 60;
1725                                 mins %= 60;
1726                                 if_printf(hrs,"%u hours", hrs);
1727                                 if_printf((hrs && mins)," + ");
1728                                 if_printf(mins,"%u minutes", mins);
1729                         }
1730                         else
1731                                 printf("illegal value");
1732                         break;
1733         }
1734         printf(")\n");
1735 }
1736
1737 struct xfermode_entry {
1738     int val;
1739     const char *name;
1740 };
1741
1742 static const struct xfermode_entry xfermode_table[] = {
1743     { 8,    "pio0" },
1744     { 9,    "pio1" },
1745     { 10,   "pio2" },
1746     { 11,   "pio3" },
1747     { 12,   "pio4" },
1748     { 13,   "pio5" },
1749     { 14,   "pio6" },
1750     { 15,   "pio7" },
1751     { 16,   "sdma0" },
1752     { 17,   "sdma1" },
1753     { 18,   "sdma2" },
1754     { 19,   "sdma3" },
1755     { 20,   "sdma4" },
1756     { 21,   "sdma5" },
1757     { 22,   "sdma6" },
1758     { 23,   "sdma7" },
1759     { 32,   "mdma0" },
1760     { 33,   "mdma1" },
1761     { 34,   "mdma2" },
1762     { 35,   "mdma3" },
1763     { 36,   "mdma4" },
1764     { 37,   "mdma5" },
1765     { 38,   "mdma6" },
1766     { 39,   "mdma7" },
1767     { 64,   "udma0" },
1768     { 65,   "udma1" },
1769     { 66,   "udma2" },
1770     { 67,   "udma3" },
1771     { 68,   "udma4" },
1772     { 69,   "udma5" },
1773     { 70,   "udma6" },
1774     { 71,   "udma7" },
1775     { 0, NULL }
1776 };
1777
1778 static int translate_xfermode(char * name)
1779 {
1780         const struct xfermode_entry *tmp;
1781         char *endptr;
1782         int val = -1;
1783
1784
1785         for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1786         {
1787                 if (!strcmp(name, tmp->name))
1788                         return tmp->val;
1789
1790         }
1791
1792         val = strtol(name, &endptr, 10);
1793         if (*endptr == '\0')
1794                 return val;
1795
1796         return -1;
1797 }
1798
1799 static void interpret_xfermode (unsigned int xfermode)
1800 {
1801         printf(" (");
1802         switch(xfermode) {
1803                 case 0:
1804                         printf("default PIO mode");
1805                         break;
1806                 case 1:
1807                         printf("default PIO mode, disable IORDY");
1808                         break;
1809                 case 8:
1810                 case 9:
1811                 case 10:
1812                 case 11:
1813                 case 12:
1814                 case 13:
1815                 case 14:
1816                 case 15:
1817                         printf("PIO flow control mode%u", xfermode-8);
1818                         break;
1819                 case 16:
1820                 case 17:
1821                 case 18:
1822                 case 19:
1823                 case 20:
1824                 case 21:
1825                 case 22:
1826                 case 23:
1827                         printf("singleword DMA mode%u", xfermode-16);
1828                         break;
1829                 case 32:
1830                 case 33:
1831                 case 34:
1832                 case 35:
1833                 case 36:
1834                 case 37:
1835                 case 38:
1836                 case 39:
1837                         printf("multiword DMA mode%u", xfermode-32);
1838                         break;
1839                 case 64:
1840                 case 65:
1841                 case 66:
1842                 case 67:
1843                 case 68:
1844                 case 69:
1845                 case 70:
1846                 case 71:
1847                         printf("UltraDMA mode%u", xfermode-64);
1848                         break;
1849                 default:
1850                         printf("unknown, probably not valid");
1851                         break;
1852         }
1853         printf(")\n");
1854 }
1855 #endif /* HDIO_DRIVE_CMD */
1856
1857 #ifndef VXVM_MAJOR
1858 #define VXVM_MAJOR 199
1859 #endif
1860
1861 #ifndef CCISS_MAJOR
1862 #define CCISS_MAJOR 104
1863 #endif
1864
1865 static void process_dev (char *devname)
1866 {
1867         int fd;
1868         static long parm, multcount;
1869         struct stat stat_buf;
1870 #ifndef HDIO_DRIVE_CMD
1871         int force_operation = 0;
1872 #endif
1873         unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1874
1875         xstat(devname,&stat_buf);
1876
1877         switch(major(stat_buf.st_rdev))
1878         {
1879 #ifdef SCSI_DISK0_MAJOR
1880                 case (SCSI_DISK0_MAJOR):
1881                 case (SCSI_DISK1_MAJOR):
1882                 case (SCSI_DISK2_MAJOR):
1883                 case (SCSI_DISK3_MAJOR):
1884                 case (SCSI_DISK4_MAJOR):
1885                 case (SCSI_DISK5_MAJOR):
1886                 case (SCSI_DISK6_MAJOR):
1887                 case (SCSI_DISK7_MAJOR):
1888 #else
1889                 case (SCSI_DISK_MAJOR):
1890 #endif
1891 #ifdef MD_MAJOR
1892                 case (MD_MAJOR):
1893 #endif
1894                 case (VXVM_MAJOR):
1895 #ifdef LVM_BLK_MAJOR
1896                 case (LVM_BLK_MAJOR):
1897 #endif
1898                 case (CCISS_MAJOR):
1899                         is_scsi_hd = 1;
1900                         break;
1901 #ifdef XT_DISK_MAJOR
1902                 case (XT_DISK_MAJOR):
1903                         is_xt_hd = 1;
1904                         break;
1905 #endif
1906                 case IDE0_MAJOR:
1907                 case IDE1_MAJOR:
1908 #ifdef IDE2_MAJOR
1909                 case IDE2_MAJOR:
1910 #endif
1911 #ifdef IDE3_MAJOR
1912                 case IDE3_MAJOR:
1913 #endif
1914 #ifdef IDE4_MAJOR
1915                 case IDE4_MAJOR:
1916 #endif
1917 #ifdef IDE5_MAJOR
1918                 case IDE5_MAJOR:
1919 #endif
1920 #ifdef IDE6_MAJOR
1921                 case IDE6_MAJOR:
1922 #endif
1923 #ifdef IDE7_MAJOR
1924                 case IDE7_MAJOR:
1925 #endif
1926 #ifdef IDE8_MAJOR
1927                 case IDE8_MAJOR:
1928 #endif
1929 #ifdef IDE9_MAJOR
1930                 case IDE9_MAJOR:
1931 #endif
1932                         break;  /* do nothing */
1933                 default:
1934                         bb_error_msg_and_die("%s not supported",devname);
1935         }
1936
1937         fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
1938         if_printf( (!quiet),"\n%s:\n", devname);
1939
1940         if (set_readahead)
1941         {
1942                 if_printf(get_readahead," setting fs readahead to %ld\n", Xreadahead);
1943                 bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
1944         }
1945 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1946         if (unregister_hwif)
1947         {
1948                 no_scsi();
1949                 printf(" attempting to unregister hwif#%u\n", hwif);
1950                 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)(unsigned long)hwif,"HDIO_UNREGISTER_HWIF");
1951         }
1952 #endif
1953 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1954         if (scan_hwif)
1955         {
1956                 no_scsi();
1957                 printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
1958                 args[0] = hwif_data;
1959                 args[1] = hwif_ctrl;
1960                 args[2] = hwif_irq;
1961                 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
1962         }
1963 #endif
1964         if (set_piomode)
1965         {
1966                 no_scsi_no_xt();
1967
1968                 if (noisy_piomode)
1969                 {
1970                         printf(" attempting to ");
1971                         if (piomode == 255)
1972                                 printf("auto-tune PIO mode\n");
1973                         else if (piomode < 100)
1974                                 printf("set PIO mode to %d\n", piomode);
1975                         else if (piomode < 200)
1976                                 printf("set MDMA mode to %d\n", (piomode-100));
1977                         else
1978                                 printf("set UDMA mode to %d\n", (piomode-200));
1979                 }
1980                 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode, "HDIO_SET_PIO_MODE");
1981         }
1982         if (set_io32bit)
1983         {
1984                 no_scsi_no_xt();
1985                 if_printf(get_io32bit," setting 32-bit IO_support flag to %ld\n", io32bit);
1986                 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
1987         }
1988         if (set_mult)
1989         {
1990                 no_scsi_no_xt();
1991                 if_printf(get_mult, " setting multcount to %ld\n", mult);
1992                 if(ioctl(fd, HDIO_SET_MULTCOUNT, mult))
1993                         bb_perror_msg("HDIO_SET_MULTCOUNT");
1994 #ifndef HDIO_DRIVE_CMD
1995                 else
1996                         force_operation = 1;
1997 #endif
1998         }
1999         if (set_readonly)
2000         {
2001                 if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
2002                 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
2003         }
2004         if (set_unmask)
2005         {
2006                 no_scsi_no_xt();
2007                 if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
2008                 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
2009         }
2010 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2011         if (set_dma)
2012         {
2013                 no_scsi();
2014                 if_printf_on_off(get_dma," setting using_dma to %ld", dma);
2015                 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
2016         }
2017 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2018         if (set_dma_q)
2019         {
2020                 no_scsi();
2021                 if_printf_on_off(get_dma_q," setting DMA queue_depth to %ld", dma_q);
2022                 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
2023         }
2024         if (set_nowerr)
2025         {
2026                 no_scsi_no_xt();
2027                 if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
2028                 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
2029         }
2030         if (set_keep)
2031         {
2032                 no_scsi_no_xt();
2033                 if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
2034                 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
2035         }
2036 #ifdef HDIO_DRIVE_CMD
2037         if (set_doorlock)
2038         {
2039                 no_scsi_no_xt();
2040                 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
2041                 if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
2042                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
2043         }
2044         if (set_dkeep)
2045         {
2046                 /* lock/unlock the drive's "feature" settings */
2047                 no_scsi_no_xt();
2048                 if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
2049                 args[2] = dkeep ? 0x66 : 0xcc;
2050                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
2051         }
2052         if (set_defects)
2053         {
2054                 no_scsi();
2055                 args[2] = defects ? 0x04 : 0x84;
2056                 if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
2057                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
2058         }
2059         if (set_prefetch)
2060         {
2061                 no_scsi_no_xt();
2062                 args[1] = prefetch;
2063                 args[2] = 0xab;
2064                 if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
2065                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
2066         }
2067         if (set_xfermode)
2068         {
2069                 no_scsi_no_xt();
2070                 args[1] = xfermode_requested;
2071                 args[2] = 3;
2072                 if (get_xfermode)
2073                 {
2074                         printf(" setting xfermode to %d", xfermode_requested);
2075                         interpret_xfermode(xfermode_requested);
2076                 }
2077                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
2078         }
2079         if (set_lookahead)
2080         {
2081                 no_scsi_no_xt();
2082                 args[2] = lookahead ? 0xaa : 0x55;
2083                 if_printf_on_off(get_lookahead," setting drive read-lookahead to %ld", lookahead);
2084                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
2085         }
2086         if (set_apmmode)
2087         {
2088                 no_scsi();
2089                 apmmode=check_if_min_and_set_val(apmmode,1);
2090                 apmmode=check_if_maj_and_set_val(apmmode,255);
2091                 if_printf(get_apmmode," setting APM level to");
2092                 if (apmmode==255)
2093                 {
2094                         /* disable Advanced Power Management */
2095                         args[2] = 0x85; /* feature register */
2096                         if_printf(get_apmmode," disabled\n");
2097                 }
2098                 else
2099                 {
2100                         /* set Advanced Power Management mode */
2101                         args[2] = 0x05; /* feature register */
2102                         args[1] = apmmode; /* sector count register */
2103                         if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
2104                 }
2105                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
2106         }
2107         if (set_wcache)
2108         {
2109 #ifdef DO_FLUSHCACHE
2110 #ifndef WIN_FLUSHCACHE
2111 #define WIN_FLUSHCACHE 0xe7
2112 #endif
2113                 static unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
2114 #endif /* DO_FLUSHCACHE */
2115                 no_scsi_no_xt();
2116                 args[2] = wcache ? 0x02 : 0x82;
2117                 if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
2118 #ifdef DO_FLUSHCACHE
2119                 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
2120                         bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
2121 #endif /* DO_FLUSHCACHE */
2122                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
2123 #ifdef DO_FLUSHCACHE
2124                 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
2125                         bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
2126 #endif /* DO_FLUSHCACHE */
2127         }
2128         if (set_standbynow)
2129         {
2130 #ifndef WIN_STANDBYNOW1
2131 #define WIN_STANDBYNOW1 0xE0
2132 #endif
2133 #ifndef WIN_STANDBYNOW2
2134 #define WIN_STANDBYNOW2 0x94
2135 #endif
2136                 static unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
2137                 static unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
2138                 no_scsi();
2139                 if_printf(get_standbynow," issuing standby command\n");
2140                 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2141                  && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2142                         bb_perror_msg("HDIO_DRIVE_CMD(standby)");
2143         }
2144         if (set_sleepnow)
2145         {
2146 #ifndef WIN_SLEEPNOW1
2147 #define WIN_SLEEPNOW1 0xE6
2148 #endif
2149 #ifndef WIN_SLEEPNOW2
2150 #define WIN_SLEEPNOW2 0x99
2151 #endif
2152                 static unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2153                 static unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2154                 no_scsi();
2155                 if_printf(get_sleepnow," issuing sleep command\n");
2156                 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2157                  && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2158                         bb_perror_msg("HDIO_DRIVE_CMD(sleep)");
2159         }
2160         if (set_seagate)
2161         {
2162                 args[0] = 0xfb;
2163                 no_scsi_no_xt();
2164                 if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
2165                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
2166         }
2167         if (set_standby)
2168         {
2169                 args[0] = WIN_SETIDLE1;
2170                 args[1] = standby_requested;
2171                 no_scsi_no_xt();
2172                 if (get_standby)
2173                 {
2174                         printf(" setting standby to %lu", standby_requested);
2175                         interpret_standby(standby_requested);
2176                 }
2177                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
2178         }
2179 #else   /* HDIO_DRIVE_CMD */
2180         if (force_operation)
2181         {
2182                 char buf[512];
2183                 flush_buffer_cache(fd);
2184                 if (-1 == read(fd, buf, sizeof(buf)))
2185                         bb_error_msg("access failed");
2186         }
2187 #endif  /* HDIO_DRIVE_CMD */
2188
2189         if (!flagcount)
2190                 verbose = 1;
2191
2192         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
2193         {
2194                 no_scsi();
2195                 multcount = -1;
2196                 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
2197                 {
2198                         if ((verbose && !is_xt_hd) || get_mult)
2199                                 bb_perror_msg("HDIO_GET_MULTCOUNT");
2200                 }
2201                 else if (verbose | get_mult)
2202                 {
2203                         printf(" multcount    = %2ld", multcount);
2204                         on_off(multcount);
2205                 }
2206         }
2207         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
2208         {
2209                 no_scsi_no_xt();
2210                 if(ioctl(fd, HDIO_GET_32BIT, &parm))
2211                         bb_perror_msg("HDIO_GET_32BIT");
2212                 else
2213                 {
2214                         printf(" IO_support   =%3ld (", parm);
2215                         switch (parm)
2216                         {
2217                                 case 0:
2218                                         printf("default ");
2219                                 case 2:
2220                                         printf("16-bit)\n");
2221                                         break;
2222                                 case 1:
2223                                         printf("32-bit)\n");
2224                                         break;
2225                                 case 3:
2226                                         printf("32-bit w/sync)\n");
2227                                         break;
2228                                 case 8:
2229                                         printf("Request-Queue-Bypass)\n");
2230                                         break;
2231                                 default:
2232                                         printf("\?\?\?)\n");
2233                                 /*esac*/
2234                         }
2235                 }
2236         }
2237         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
2238         {
2239                 no_scsi_no_xt();
2240                 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
2241                                                 "HDIO_GET_UNMASKINTR"," unmaskirq    = %2ld");
2242         }
2243
2244
2245 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2246         if ((verbose && !is_scsi_hd) || get_dma) {
2247                 no_scsi();
2248                 if(ioctl(fd, HDIO_GET_DMA, &parm))
2249                         bb_perror_msg("HDIO_GET_DMA");
2250                 else
2251                 {
2252                         printf(" using_dma    = %2ld", parm);
2253                         if (parm == 8)
2254                                 printf(" (DMA-Assisted-PIO)\n");
2255                         else
2256                                 on_off(parm);
2257                 }
2258         }
2259 #endif
2260         if (get_dma_q)
2261         {
2262                 no_scsi();
2263                 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
2264                                                   "HDIO_GET_QDMA"," queue_depth  = %2ld");
2265         }
2266         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
2267         {
2268                 no_scsi_no_xt();
2269                 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
2270                                                         "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
2271         }
2272
2273         if (get_nowerr)
2274         {
2275                 no_scsi_no_xt();
2276                 bb_ioctl_on_off  (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
2277                                                         " HDIO_GET_NOWERR"," nowerr       = %2ld");
2278         }
2279         if (verbose || get_readonly)
2280         {
2281                 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
2282                                                   " BLKROGET"," readonly     = %2ld");
2283         }
2284         if ((verbose && !is_scsi_hd) || get_readahead)
2285         {
2286                 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
2287                                                         " BLKRAGET"," readahead    = %2ld");
2288         }
2289         if (verbose || get_geom)
2290         {
2291                 static const char msg[] = " geometry     = %u/%u/%u, sectors = %ld, start = %ld\n";
2292                 static struct hd_geometry g;
2293 #ifdef HDIO_GETGEO_BIG
2294                 static struct hd_big_geometry bg;
2295 #endif
2296
2297                 if (ioctl(fd, BLKGETSIZE, &parm))
2298                         bb_perror_msg("BLKGETSIZE");
2299 #ifdef HDIO_GETGEO_BIG
2300                 else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
2301                         printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
2302 #endif
2303                 else if (ioctl(fd, HDIO_GETGEO, &g))
2304                         bb_perror_msg("HDIO_GETGEO");
2305                 else
2306                         printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
2307         }
2308 #ifdef HDIO_DRIVE_CMD
2309         if (get_powermode)
2310         {
2311 #ifndef WIN_CHECKPOWERMODE1
2312 #define WIN_CHECKPOWERMODE1 0xE5
2313 #endif
2314 #ifndef WIN_CHECKPOWERMODE2
2315 #define WIN_CHECKPOWERMODE2 0x98
2316 #endif
2317                 const char *state;
2318                 no_scsi();
2319                 args[0] = WIN_CHECKPOWERMODE1;
2320                 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
2321                          && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2322                         && ioctl(fd, HDIO_DRIVE_CMD, &args))
2323                 {
2324                         if (errno != EIO || args[0] != 0 || args[1] != 0)
2325                                 state = "unknown";
2326                         else
2327                                 state = "sleeping";
2328                 }
2329                 else
2330                         state = (args[2] == 255) ? "active/idle" : "standby";
2331
2332                 printf(" drive state is:  %s\n", state);
2333         }
2334 #endif
2335 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2336         if (perform_reset)
2337         {
2338                 no_scsi_no_xt();
2339                 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
2340         }
2341 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2342 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2343         if (perform_tristate)
2344         {
2345                 args[0] = 0;
2346                 args[1] = tristate;
2347                 no_scsi_no_xt();
2348                 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
2349         }
2350 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2351 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2352         if (get_identity)
2353         {
2354                 static struct hd_driveid id;
2355
2356                 no_scsi_no_xt();
2357
2358                 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
2359                 {
2360                         if (multcount != -1)
2361                         {
2362                                 id.multsect = multcount;
2363                                 id.multsect_valid |= 1;
2364                         }
2365                         else
2366                                 id.multsect_valid &= ~1;
2367                         dump_identity(&id);
2368                 }
2369                 else if (errno == -ENOMSG)
2370                         printf(" no identification info available\n");
2371                 else
2372                         bb_perror_msg("HDIO_GET_IDENTITY");
2373         }
2374
2375         if (get_IDentity)
2376         {
2377                 unsigned char args1[4+512] = {WIN_IDENTIFY,0,0,1,};
2378                 unsigned i;
2379
2380                 no_scsi_no_xt();
2381
2382                 if (ioctl(fd, HDIO_DRIVE_CMD, &args1))
2383                 {
2384                         args[0] = WIN_PIDENTIFY;
2385                         if (ioctl(fd, HDIO_DRIVE_CMD, &args1))
2386                         {
2387                                 bb_perror_msg("HDIO_DRIVE_CMD(identify)");
2388                                 goto identify_abort;
2389                         }
2390                 }
2391                 for(i=0; i<(sizeof args1)/2; i+=2)
2392                         __le16_to_cpus((uint16_t *)(&args1[i]));
2393
2394                 identify((void *)&args1[4], NULL);
2395 identify_abort:
2396         /* VOID */;
2397         }
2398 #endif
2399 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2400         if (set_busstate)
2401         {
2402                 no_scsi();
2403                 if (get_busstate)
2404                 {
2405                         printf(" setting bus state to %d", busstate);
2406                         bus_state_value(busstate);
2407                 }
2408                 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate, "HDIO_SET_BUSSTATE");
2409         }
2410 #endif
2411 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2412         if (get_busstate)
2413         {
2414                 no_scsi();
2415                 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
2416                         bb_perror_msg("HDIO_GET_BUSSTATE");
2417                 else
2418                 {
2419                         printf(" busstate     = %2ld", parm);
2420                         bus_state_value(parm);
2421                 }
2422         }
2423 #endif
2424         if (reread_partn)
2425                 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2426
2427
2428         if (do_ctimings)
2429                 do_time(0,fd);          /*time cache  */
2430         if (do_timings)
2431                 do_time(1,fd);          /*time device */
2432         if (do_flush)
2433                 flush_buffer_cache (fd);
2434         close (fd);
2435 }
2436
2437 static char * GET_NUMBER(char *p, unsigned long *flag, unsigned long *num)
2438 {
2439         *num = 0;
2440         while (isdigit(*p)) {
2441                 *flag = 1;
2442                 *num = (*num * 10) + (*p++ - '0');
2443         }
2444         return p;
2445 }
2446
2447 static char * GET_STRING(char *p, unsigned long *flag, int *num)
2448 {
2449         char *tmpstr;
2450         char name[32];
2451         tmpstr = name;
2452         tmpstr[0] = '\0';
2453         while (isalnum(*p) && (tmpstr - name) < 31) {
2454                 tmpstr[0] = *p++;
2455                 tmpstr[1] = '\0';
2456                 ++tmpstr;
2457         }
2458         *num = translate_xfermode(name);
2459         if (*num == -1)
2460                 *flag = 0;
2461         else
2462                 *flag = 1;
2463         return p;
2464 }
2465
2466 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2467 static int fromhex (unsigned char c)
2468 {
2469         if (c >= 'a' && c <= 'f')
2470                 return 10 + (c - 'a');
2471         if (c >= '0' && c <= '9')
2472                 return (c - '0');
2473         bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2474 }
2475
2476 static int identify_from_stdin (void)
2477 {
2478         unsigned short sbuf[800];
2479         unsigned char  buf[1600], *b = (unsigned char *)buf;
2480         int i, count = read(0, buf, 1280);
2481
2482         if (count != 1280)
2483                 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
2484         for (i = 0; count >= 4; ++i)
2485         {
2486                 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2487                 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2488                 b += 5;
2489                 count -= 5;
2490         }
2491         identify(sbuf, NULL);
2492         return 0;
2493 }
2494 #endif
2495
2496 static void missing_arg(int arg, char c, char* add)
2497 {
2498         if (!arg)
2499                 bb_error_msg("-%c: missing value %s", c, (add!=NULL)? add :"");
2500 }
2501
2502 /* our main() routine: */
2503 int hdparm_main(int argc, char **argv)
2504 {
2505         char c, *p;
2506
2507         ++argv;
2508         if (!--argc)
2509                 bb_show_usage();
2510
2511         while (argc--)
2512         {
2513 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2514                 if (!strcmp("-Istdin", *argv))
2515                 {
2516                         return identify_from_stdin();
2517                 }
2518 #endif
2519                 p = *argv++;
2520                 if (*p == '-')
2521                 {
2522                         if (!*++p)
2523                                 bb_show_usage();
2524                         while ((c = *p++))
2525                         {
2526                                 ++flagcount;
2527                                 switch (c)
2528                                 {
2529                                         case 'V':
2530                                                 /*bb_error_msg_and_die("%s", VERSION);*/
2531                                                 /* We have to return 0 here and not 1 */
2532                                                 printf("%s %s\n",bb_applet_name, VERSION);
2533                                                 return EXIT_SUCCESS;
2534                                         case 'v':
2535                                                 verbose = 1;
2536                                                 break;
2537 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2538                                         case 'I':
2539                                                 get_IDentity = 1;
2540                                                 break;
2541                                         case 'i':
2542                                                 get_identity = 1;
2543                                                 break;
2544 #endif
2545                                         case 'g':
2546                                                 get_geom = 1;
2547                                                 break;
2548                                         case 'f':
2549                                                 do_flush = 1;
2550                                                 break;
2551                                         case 'q':
2552                                                 quiet = 1;
2553                                                 noisy = 0;
2554                                                 break;
2555                                         case 'u':
2556                                                 get_unmask = noisy;
2557                                                 noisy = 1;
2558                                                 if (!*p && argc && isdigit(**argv))
2559                                                         p = *argv++, --argc;
2560                                                 if((set_unmask = set_flag(p,'1'))==1)
2561                                                         unmask  = *p++ - '0';
2562                                                 break;
2563 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2564                                         case 'd':
2565                                                 get_dma = noisy;
2566                                                 noisy = 1;
2567                                                 if (!*p && argc && isdigit(**argv))
2568                                                         p = *argv++, --argc;
2569                                                 if((set_dma = set_flag(p,'9'))==1)
2570                                                         dma = *p++ - '0';
2571                                                 break;
2572 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2573                                         case 'n':
2574                                                 get_nowerr = noisy;
2575                                                 noisy = 1;
2576                                                 if (!*p && argc && isdigit(**argv))
2577                                                         p = *argv++, --argc;
2578                                                 if((set_nowerr = set_flag(p,'1'))==1)
2579                                                         nowerr  = *p++ - '0';
2580                                                 break;
2581                                         case 'p':
2582                                                 noisy_piomode = noisy;
2583                                                 noisy = 1;
2584                                                 if (!*p && argc && isalnum(**argv))
2585                                                         p = *argv++, --argc;
2586                                                 p=GET_STRING(p,&set_piomode,&piomode);
2587                                                 break;
2588                                         case 'r':
2589                                                 get_readonly = noisy;
2590                                                 noisy = 1;
2591                                                 if (!*p && argc && isdigit(**argv))
2592                                                         p = *argv++, --argc;
2593                                                 if((set_readonly = set_flag(p,'1'))==1)
2594                                                         readonly  = *p++ - '0';
2595                                                 break;
2596                                         case 'm':
2597                                                 get_mult = noisy;
2598                                                 noisy = 1;
2599                                                 if (!*p && argc && isalnum(**argv))
2600                                                         p = *argv++, --argc;
2601                                                 p=GET_NUMBER(p,&set_mult,&mult);
2602                                                 break;
2603                                         case 'c':
2604                                                 get_io32bit = noisy;
2605                                                 noisy = 1;
2606                                                 if (!*p && argc && isalnum(**argv))
2607                                                         p = *argv++, --argc;
2608                                                 p=GET_NUMBER(p,&set_io32bit,&io32bit);
2609                                                 break;
2610 #ifdef HDIO_DRIVE_CMD
2611                                         case 'S':
2612                                                 get_standby = noisy;
2613                                                 noisy = 1;
2614                                                 if (!*p && argc && isalnum(**argv))
2615                                                         p = *argv++, --argc;
2616                                                 p=GET_NUMBER(p,&set_standby,&standby_requested);
2617                                                 missing_arg(set_standby, c, NULL);
2618                                                 break;
2619
2620                                         case 'D':
2621                                                 get_defects = noisy;
2622                                                 noisy = 1;
2623                                                 if (!*p && argc && isalnum(**argv))
2624                                                         p = *argv++, --argc;
2625                                                 p=GET_NUMBER(p,&set_defects,&defects);
2626                                                 missing_arg(set_defects, c, NULL);
2627                                                 break;
2628                                         case 'P':
2629                                                 get_prefetch = noisy;
2630                                                 noisy = 1;
2631                                                 if (!*p && argc && isalnum(**argv))
2632                                                         p = *argv++, --argc;
2633                                                 p=GET_NUMBER(p,&set_prefetch,&prefetch);
2634                                                 missing_arg(set_prefetch, c, NULL);
2635                                                 break;
2636
2637                                         case 'X':
2638                                                 get_xfermode = noisy;
2639                                                 noisy = 1;
2640                                                 if (!*p && argc && isalnum(**argv))
2641                                                         p = *argv++, --argc;
2642                                                 p=GET_STRING(p,&set_xfermode,&xfermode_requested);
2643                                                 missing_arg(set_xfermode, c, NULL);
2644                                                 break;
2645
2646                                         case 'K':
2647                                                 get_dkeep = noisy;
2648                                                 noisy = 1;
2649                                                 if (!*p && argc && isdigit(**argv))
2650                                                         p = *argv++, --argc;
2651                                                 if((set_dkeep = set_flag(p,'1'))==1)
2652                                                         dkeep  = *p++ - '0';
2653                                                 else
2654                                                         goto missing_arg_error;
2655                                                 break;
2656
2657                                         case 'A':
2658                                                 get_lookahead = noisy;
2659                                                 noisy = 1;
2660                                                 if (!*p && argc && isdigit(**argv))
2661                                                         p = *argv++, --argc;
2662                                                 if((set_lookahead = set_flag(p,'1'))==1)
2663                                                         lookahead  = *p++ - '0';
2664                                                 else
2665                                                         goto missing_arg_error;
2666                                                 break;
2667
2668                                         case 'L':
2669                                                 get_doorlock = noisy;
2670                                                 noisy = 1;
2671                                                 if (!*p && argc && isdigit(**argv))
2672                                                         p = *argv++, --argc;
2673                                                 if((set_doorlock = set_flag(p,'1'))==1)
2674                                                         doorlock  = *p++ - '0';
2675                                                 else
2676                                                         goto missing_arg_error;
2677                                                 break;
2678
2679                                         case 'W':
2680                                                 get_wcache = noisy;
2681                                                 noisy = 1;
2682                                                 if (!*p && argc && isdigit(**argv))
2683                                                         p = *argv++, --argc;
2684                                                 if((set_wcache = set_flag(p,'1'))==1)
2685                                                         wcache  = *p++ - '0';
2686                                                 else
2687 missing_arg_error:
2688                                                         missing_arg(1, c, "(0/1)");
2689                                                 break;
2690
2691                                         case 'C':
2692                                                 get_powermode = noisy;
2693                                                 noisy = 1;
2694                                                 break;
2695
2696                                         case 'y':
2697                                                 get_standbynow = noisy;
2698                                                 noisy = 1;
2699                                                 set_standbynow = 1;
2700                                                 break;
2701
2702                                         case 'Y':
2703                                                 get_sleepnow = noisy;
2704                                                 noisy = 1;
2705                                                 set_sleepnow = 1;
2706                                                 break;
2707
2708                                         case 'z':
2709                                                 reread_partn = 1;
2710                                                 break;
2711
2712                                         case 'Z':
2713                                                 get_seagate = noisy;
2714                                                 noisy = 1;
2715                                                 set_seagate = 1;
2716                                                 break;
2717 #endif /* HDIO_DRIVE_CMD */
2718                                         case 'k':
2719                                                 get_keep = noisy;
2720                                                 noisy = 1;
2721                                                 if (!*p && argc && isdigit(**argv))
2722                                                         p = *argv++, --argc;
2723                                                 if((set_keep = set_flag(p,'1'))==1)
2724                                                         keep  = *p++ - '0';
2725                                                 break;
2726 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2727                                         case 'U':
2728                                                 if (!*p && argc && isdigit(**argv))
2729                                                         p = *argv++, --argc;
2730                                                 if(! p)
2731                                                         goto expected_hwif_error; /* "expected hwif_nr" */
2732
2733                                                 sscanf(p++, "%i", &hwif);
2734
2735                                                 unregister_hwif = 1;
2736                                                 break;
2737 #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2738 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2739                                         case 'R':
2740                                                 if (!*p && argc && isdigit(**argv))
2741                                                         p = *argv++, --argc;
2742                                                 if(! p)
2743                                                         goto expected_hwif_error; /* "expected hwif_data" */
2744
2745                                                 sscanf(p++, "%i", &hwif_data);
2746
2747                                                 if (argc && isdigit(**argv))
2748                                                         p = *argv++, --argc;
2749                                                 else
2750                                                         goto expected_hwif_error; /* "expected hwif_ctrl" */
2751
2752                                                 sscanf(p, "%i", &hwif_ctrl);
2753
2754                                                 if (argc && isdigit(**argv))
2755                                                         p = *argv++, --argc;
2756                                                 else
2757 #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2758 #if defined CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF || defined CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2759 expected_hwif_error:
2760                                                         bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */
2761 #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF || CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2762 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2763
2764                                                 sscanf(p, "%i", &hwif_irq);
2765
2766                                                 *p = '\0';
2767
2768                                                 scan_hwif = 1;
2769                                                 break;
2770 #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2771                                         case 'Q':
2772 #ifdef HDIO_GET_QDMA
2773                                                 get_dma_q = noisy;
2774                                                 noisy = 1;
2775 #ifdef HDIO_SET_QDMA
2776                                                 if (!*p && argc && isalnum(**argv))
2777                                                         p = *argv++, --argc;
2778                                                 p=GET_NUMBER(p,&set_dma_q,&dma_q);
2779 #ifdef HDIO_GET_QDMA
2780                                                 dma_q = -dma_q;
2781 #endif
2782 #endif
2783 #endif
2784                                                 break;
2785
2786 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2787                                         case 'w':
2788                                                 perform_reset = 1;
2789                                                 break;
2790 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2791 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2792                                         case 'x':
2793                                                 if (!*p && argc && isdigit(**argv))
2794                                                         p = *argv++, --argc;
2795                                                 if((perform_tristate = set_flag(p,'1'))==1)
2796                                                         tristate  = *p++ - '0';
2797                                                 else
2798                                                         missing_arg(1, c, "(0/1)");
2799                                                 break;
2800
2801 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2802                                         case 'a':
2803                                                 get_readahead = noisy;
2804                                                 noisy = 1;
2805                                                 if (!*p && argc && isalnum(**argv))
2806                                                         p = *argv++, --argc;
2807                                                 p=GET_NUMBER(p,&set_readahead,&Xreadahead);
2808                                                 break;
2809                                         case 'B':
2810                                                 get_apmmode = noisy;
2811                                                 noisy = 1;
2812                                                 if (!*p && argc && isalnum(**argv))
2813                                                         p = *argv++, --argc;
2814                                                 p=GET_NUMBER(p,&set_apmmode,&apmmode);
2815                                                 missing_arg(set_apmmode, c, "(1-255)");
2816                                                 break;
2817                                         case 't':
2818                                                 do_timings = 1;
2819                                                 do_flush = 1;
2820                                                 break;
2821                                         case 'T':
2822                                                 do_ctimings = 1;
2823                                                 do_flush = 1;
2824                                                 break;
2825 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2826                                         case 'b':
2827                                                 get_busstate = noisy;
2828                                                 noisy = 1;
2829                                                 if (!*p && argc && isdigit(**argv))
2830                                                         p = *argv++, --argc;
2831                                                 if((set_busstate = set_flag(p,'2'))==1)
2832                                                         busstate  = *p++ - '0';
2833                                                 break;
2834 #endif
2835                                         case 'h':
2836                                         default:
2837                                                 bb_show_usage();
2838                                 }
2839                         }
2840                         if (!argc)
2841                                 bb_show_usage();
2842                 } else {
2843                         process_dev (p);
2844                 }
2845         }
2846         return 0 ;
2847 }