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