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