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