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