Sync hdparm -t and -T options with hdparm-5.3, which seems
[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 static unsigned long set_dma      = 0, get_dma      = 0, dma      = 0;
1043 static unsigned long set_dma_q    = 0, get_dma_q    = 0, dma_q    = 0;
1044 static unsigned long set_nowerr   = 0, get_nowerr   = 0, nowerr   = 0;
1045 static unsigned long set_keep     = 0, get_keep     = 0, keep     = 0;
1046 static unsigned long set_io32bit  = 0, get_io32bit  = 0, io32bit  = 0;
1047 static unsigned long set_piomode  = 0, noisy_piomode= 0;
1048 static int piomode = 0;
1049 #ifdef HDIO_DRIVE_CMD
1050 static unsigned long set_dkeep    = 0, get_dkeep    = 0, dkeep    = 0;
1051 static unsigned long set_standby  = 0, get_standby  = 0, standby_requested= 0;
1052 static unsigned long set_xfermode = 0, get_xfermode = 0;
1053 static int xfermode_requested= 0;
1054 static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
1055 static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
1056 static unsigned long set_defects  = 0, get_defects  = 0, defects  = 0;
1057 static unsigned long set_wcache   = 0, get_wcache   = 0, wcache   = 0;
1058 static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
1059 static unsigned long set_seagate  = 0, get_seagate  = 0;
1060 static unsigned long set_standbynow = 0, get_standbynow = 0;
1061 static unsigned long set_sleepnow   = 0, get_sleepnow   = 0;
1062 static unsigned long get_powermode  = 0;
1063 static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
1064 #endif
1065 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1066 static int get_IDentity = 0;
1067 #endif
1068 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1069 static int      unregister_hwif = 0;
1070 static int      hwif = 0;
1071 #endif
1072 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1073 static int      scan_hwif = 0;
1074 static int      hwif_data = 0;
1075 static int      hwif_ctrl = 0;
1076 static int      hwif_irq = 0;
1077 #endif
1078 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1079 static int      set_busstate = 0, get_busstate = 0, busstate = 0;
1080 #endif
1081 static int      reread_partn = 0;
1082
1083 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1084 static int      perform_reset = 0;
1085 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1086 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1087 static int      perform_tristate = 0,   tristate = 0;
1088 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1089
1090 // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1091 // then the HDIO_GET_IDENTITY only returned 142 bytes.
1092 // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1093 // and HDIO_GET_IDENTITY returns 512 bytes.  But the latest
1094 // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1095 // (which they should, but they should just return -EINVAL).
1096 //
1097 // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1098 // On a really old system, it will not, and we will be confused.
1099 // Too bad, really.
1100
1101 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1102 static const char *cfg_str[] =
1103 {       "",          " HardSect",   " SoftSect",  " NotMFM",
1104         " HdSw>15uSec", " SpinMotCtl", " Fixed",     " Removeable",
1105         " DTR<=5Mbs",   " DTR>5Mbs",   " DTR>10Mbs", " RotSpdTol>.5%",
1106         " dStbOff",     " TrkOff",     " FmtGapReq", " nonMagnetic"
1107 };
1108
1109 static const char *BuffType[]   = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1110
1111 #define YN(b)   (((b)==0)?"no":"yes")
1112
1113 static void dmpstr (const char *prefix, unsigned int i, const char *s[], unsigned int maxi)
1114 {
1115         if (i > maxi)
1116                 printf("%s%u", prefix, i);
1117         else
1118                 printf("%s%s", prefix, s[i]);
1119 }
1120
1121 static void dump_identity (const struct hd_driveid *id)
1122 {
1123         int i;
1124         char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
1125         const unsigned short int *id_regs= (const void*) id;
1126         unsigned long capacity;
1127
1128         printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s",
1129                 id->model, id->fw_rev, id->serial_no);
1130         printf("\n Config={");
1131         for (i=0; i<=15; i++) {
1132                 if (id->config & (1<<i))
1133                         printf("%s", cfg_str[i]);
1134         }
1135         printf(" }\n");
1136         printf(" RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
1137                 id->cyls, id->heads, id->sectors,
1138                 id->track_bytes, id->sector_bytes, id->ecc_bytes);
1139         dmpstr(" BuffType=",id->buf_type,BuffType,3);
1140         printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
1141         if (id->max_multsect) {
1142                 printf(", MultSect=");
1143                 if (!(id->multsect_valid&1))
1144                         printf("?%u?", id->multsect);
1145                 else if (id->multsect)
1146                         printf("%u", id->multsect);
1147                 else
1148                         printf("off");
1149         }
1150         putchar('\n');
1151         if (id->tPIO <= 5) {
1152                 strcat(pmodes, "pio0 ");
1153                 if (id->tPIO >= 1) strcat(pmodes, "pio1 ");
1154                 if (id->tPIO >= 2) strcat(pmodes, "pio2 ");
1155         }
1156         if (!(id->field_valid&1))
1157                 printf(" (maybe):");
1158 #if __BYTE_ORDER == __BIG_ENDIAN
1159         capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1160 #else
1161         capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1162 #endif
1163         printf(" CurCHS=%u/%u/%u, CurSects=%lu", id->cur_cyls, id->cur_heads, id->cur_sectors, capacity);
1164         printf(", LBA=%s", YN(id->capability&2));
1165         if (id->capability&2)
1166                 printf(", LBAsects=%u", id->lba_capacity);
1167
1168         if (id->capability&1) {
1169                 if (id->dma_1word | id->dma_mword) {
1170                         if (id->dma_1word & 0x100)      strcat(dmodes,"*");
1171                         if (id->dma_1word & 1)          strcat(dmodes,"sdma0 ");
1172                         if (id->dma_1word & 0x200)      strcat(dmodes,"*");
1173                         if (id->dma_1word & 2)          strcat(dmodes,"sdma1 ");
1174                         if (id->dma_1word & 0x400)      strcat(dmodes,"*");
1175                         if (id->dma_1word & 4)          strcat(dmodes,"sdma2 ");
1176                         if (id->dma_1word & 0xf800)     strcat(dmodes,"*");
1177                         if (id->dma_1word & 0xf8)       strcat(dmodes,"sdma? ");
1178                         if (id->dma_mword & 0x100)      strcat(dmodes,"*");
1179                         if (id->dma_mword & 1)          strcat(dmodes,"mdma0 ");
1180                         if (id->dma_mword & 0x200)      strcat(dmodes,"*");
1181                         if (id->dma_mword & 2)          strcat(dmodes,"mdma1 ");
1182                         if (id->dma_mword & 0x400)      strcat(dmodes,"*");
1183                         if (id->dma_mword & 4)          strcat(dmodes,"mdma2 ");
1184                         if (id->dma_mword & 0xf800)     strcat(dmodes,"*");
1185                         if (id->dma_mword & 0xf8)       strcat(dmodes,"mdma? ");
1186                 }
1187         }
1188         printf("\n IORDY=");
1189         if (id->capability&8)
1190                 printf((id->capability&4) ? "on/off" : "yes");
1191         else
1192                 printf("no");
1193         if ((id->capability&8) || (id->field_valid&2)) {
1194                 if (id->field_valid&2) {
1195                         printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1196                         if (id->eide_pio_modes & 1)     strcat(pmodes, "pio3 ");
1197                         if (id->eide_pio_modes & 2)     strcat(pmodes, "pio4 ");
1198                         if (id->eide_pio_modes &~3)     strcat(pmodes, "pio? ");
1199                 }
1200                 if (id->field_valid&4) {
1201                         if (id->dma_ultra & 0x100)      strcat(umodes,"*");
1202                         if (id->dma_ultra & 0x001)      strcat(umodes,"udma0 ");
1203                         if (id->dma_ultra & 0x200)      strcat(umodes,"*");
1204                         if (id->dma_ultra & 0x002)      strcat(umodes,"udma1 ");
1205                         if (id->dma_ultra & 0x400)      strcat(umodes,"*");
1206                         if (id->dma_ultra & 0x004)      strcat(umodes,"udma2 ");
1207 #ifdef __NEW_HD_DRIVE_ID
1208                         if (id->hw_config & 0x2000) {
1209 #else /* !__NEW_HD_DRIVE_ID */
1210                         if (id->word93 & 0x2000) {
1211 #endif /* __NEW_HD_DRIVE_ID */
1212                                 if (id->dma_ultra & 0x0800)     strcat(umodes,"*");
1213                                 if (id->dma_ultra & 0x0008)     strcat(umodes,"udma3 ");
1214                                 if (id->dma_ultra & 0x1000)     strcat(umodes,"*");
1215                                 if (id->dma_ultra & 0x0010)     strcat(umodes,"udma4 ");
1216                                 if (id->dma_ultra & 0x2000)     strcat(umodes,"*");
1217                                 if (id->dma_ultra & 0x0020)     strcat(umodes,"udma5 ");
1218                                 if (id->dma_ultra & 0x4000)     strcat(umodes,"*");
1219                                 if (id->dma_ultra & 0x0040)     strcat(umodes,"udma6 ");
1220                                 if (id->dma_ultra & 0x8000)     strcat(umodes,"*");
1221                                 if (id->dma_ultra & 0x0080)     strcat(umodes,"udma7 ");
1222                         }
1223                 }
1224         }
1225         if ((id->capability&1) && (id->field_valid&2))
1226                 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1227         printf("\n PIO modes:  %s", pmodes);
1228         if (*dmodes)
1229                 printf("\n DMA modes:  %s", dmodes);
1230         if (*umodes)
1231                 printf("\n UDMA modes: %s", umodes);
1232
1233         printf("\n AdvancedPM=%s",YN(id_regs[83]&8));
1234         if (id_regs[83] & 8) { 
1235                 if (!(id_regs[86]&8)) 
1236                         printf(": disabled (255)"); 
1237                 else if ((id_regs[91]&0xFF00)!=0x4000) 
1238                         printf(": unknown setting"); 
1239                 else 
1240                         printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF); 
1241         } 
1242         if (id_regs[82]&0x20)
1243                 printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1244 #ifdef __NEW_HD_DRIVE_ID
1245         if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31)) {
1246                 printf("\n Drive conforms to: ");
1247                 if (id->minor_rev_num <= 31)
1248                         printf("%s: ", minor_str[id->minor_rev_num]);
1249                 else
1250                         printf("unknown: ");
1251                 if (id->major_rev_num < 31) {
1252                         for (i=0; i <= 15; i++) {
1253                                 if (id->major_rev_num & (1<<i))
1254                                         printf(" %u", i);
1255                         }
1256                 }
1257         }
1258 #endif /* __NEW_HD_DRIVE_ID */
1259         printf("\n");   
1260         printf("\n * signifies the current active mode\n");
1261         printf("\n");
1262 }
1263 #endif
1264
1265 static void flush_buffer_cache (int fd)
1266 {
1267         fsync (fd);                             /* flush buffers */
1268         if (ioctl(fd, BLKFLSBUF, NULL))         /* do it again, big time */
1269                 bb_perror_msg("BLKFLSBUF failed");
1270 #ifdef HDIO_DRIVE_CMD
1271         if (is_scsi_hd || is_xt_hd) {
1272                 sleep(1);
1273         } else {
1274                 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1275                         bb_perror_msg("HDIO_DRIVE_CMD(null) (wait for flush complete) failed");
1276         }
1277 #endif
1278 }
1279
1280 static int seek_to_zero (int fd)
1281 {
1282         if (lseek(fd, (off_t) 0, SEEK_SET)) {
1283                 bb_perror_msg("lseek() failed");
1284                 return 1;
1285         }
1286         return 0;
1287 }
1288
1289 static int read_big_block (int fd, char *buf)
1290 {
1291         int i, rc;
1292         if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
1293                 if (rc) {
1294                         if (rc == -1)
1295                                 bb_perror_msg("read() failed");
1296                         else
1297                                 bb_error_msg("read(%u) returned %u bytes", TIMING_BUF_BYTES, rc);
1298                 } else {
1299                         fputs ("read() hit EOF - device too small\n", stderr);
1300                 }
1301                 return 1;
1302         }
1303         /* access all sectors of buf to ensure the read fully completed */
1304         for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1305                 buf[i] &= 1;
1306         return 0;
1307 }
1308
1309 static double correction = 0.0;
1310
1311 void time_cache (int fd)
1312 {
1313         int i;
1314         char *buf;
1315         struct itimerval e1, e2;
1316         int shmid;
1317         int timing_MB = TIMING_MB;
1318
1319         if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1) {
1320                 bb_perror_msg ("could not allocate sharedmem buf");
1321                 return;
1322         }
1323         if (shmctl(shmid, SHM_LOCK, NULL) == -1) {
1324                 bb_perror_msg ("could not lock sharedmem buf");
1325                 (void) shmctl(shmid, IPC_RMID, NULL);
1326                 return;
1327         }
1328         if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1) {
1329                 bb_perror_msg ("could not attach sharedmem buf");
1330                 (void) shmctl(shmid, IPC_RMID, NULL);
1331                 return;
1332         }
1333         if (shmctl(shmid, IPC_RMID, NULL) == -1)
1334                 bb_perror_msg ("shmctl(,IPC_RMID,) failed");
1335
1336         /* Clear out the device request queues & give them time to complete */
1337         sync();
1338         sleep(3);
1339
1340         /* Calculate a correction factor for the basic
1341          * overhead of doing a read() from the buffer cache.
1342          * To do this, we read the data once to "cache it" and
1343          * to force full preallocation of our timing buffer,
1344          * and then we re-read it 10 times while timing it.
1345          *
1346          * getitimer() is used rather than gettimeofday() because
1347          * it is much more consistent (on my machine, at least).
1348          */
1349         setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1350         if (seek_to_zero (fd)) return;
1351         if (read_big_block (fd, buf)) return;
1352         printf(" Timing buffer-cache reads:   ");
1353         fflush(stdout);
1354
1355         /* Clear out the device request queues & give them time to complete */
1356         sync();
1357         sleep(1);
1358
1359         /* Time re-reading from the buffer-cache */
1360         getitimer(ITIMER_REAL, &e1);
1361         for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i) {
1362                 if (seek_to_zero (fd)) goto quit;
1363                 if (read_big_block (fd, buf)) goto quit;
1364         }
1365         getitimer(ITIMER_REAL, &e2);
1366         correction = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1367          + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1368
1369         /* Now remove the lseek() from the correction factor */
1370         getitimer(ITIMER_REAL, &e1);
1371         for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i) {
1372                 if (seek_to_zero (fd)) goto quit;
1373         }
1374         getitimer(ITIMER_REAL, &e2);
1375         correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
1376          + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1377
1378         if ((BUFCACHE_FACTOR * timing_MB) >= correction)  /* more than 1MB/s */
1379                 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1380                         (BUFCACHE_FACTOR * timing_MB), correction,
1381                         (BUFCACHE_FACTOR * timing_MB) / correction);
1382         else
1383                 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1384                         (BUFCACHE_FACTOR * timing_MB), correction,
1385                         (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
1386         correction /= BUFCACHE_FACTOR;
1387
1388         flush_buffer_cache(fd);
1389         sleep(1);
1390 quit:
1391         if (-1 == shmdt(buf))
1392                 bb_perror_msg ("could not detach sharedmem buf");
1393 }
1394
1395 void time_device (int fd)
1396 {
1397         int i;
1398         char *buf;
1399         double elapsed;
1400         struct itimerval e1, e2;
1401         int shmid;
1402         int timing_MB = TIMING_MB;
1403
1404         if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1) {
1405                 bb_perror_msg ("could not allocate sharedmem buf");
1406                 return;
1407         }
1408         if (shmctl(shmid, SHM_LOCK, NULL) == -1) {
1409                 bb_perror_msg ("could not lock sharedmem buf");
1410                 (void) shmctl(shmid, IPC_RMID, NULL);
1411                 return;
1412         }
1413         if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1) {
1414                 bb_perror_msg ("could not attach sharedmem buf");
1415                 (void) shmctl(shmid, IPC_RMID, NULL);
1416                 return;
1417         }
1418         if (shmctl(shmid, IPC_RMID, NULL) == -1)
1419                 bb_perror_msg ("shmctl(,IPC_RMID,) failed");
1420
1421         /* Clear out the device request queues & give them time to complete */
1422         sync();
1423         sleep(3);
1424
1425         printf(" Timing buffered disk reads:  ");
1426         fflush(stdout);
1427
1428         /*
1429          * getitimer() is used rather than gettimeofday() because
1430          * it is much more consistent (on my machine, at least).
1431          */
1432         setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1433
1434         /* Now do the timings for real */
1435         getitimer(ITIMER_REAL, &e1);
1436         for (i = TIMING_BUF_COUNT; i > 0; --i) {
1437                 if (read_big_block (fd, buf)) goto quit;
1438         }
1439         getitimer(ITIMER_REAL, &e2);
1440
1441         elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1442          + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1443         if (timing_MB >= elapsed)  /* more than 1MB/s */
1444                 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1445                         timing_MB, elapsed, timing_MB / elapsed);
1446         else
1447                 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1448                         timing_MB, elapsed, timing_MB / elapsed * 1024);
1449
1450         if (elapsed <= (correction * 2))
1451                 printf("Hmm.. suspicious results: probably not enough free memory for a proper test.\n");
1452 #if 0  /* the "estimate" is just plain wrong for many systems.. */
1453         else if (correction != 0.0) {
1454                 printf(" Estimating raw driver speed: ");
1455                 elapsed -= correction;
1456                 if (timing_MB >= elapsed)  /* more than 1MB/s */
1457                         printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1458                                 timing_MB, elapsed, timing_MB / elapsed);
1459                 else
1460                         printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1461                                 timing_MB, elapsed, timing_MB / elapsed * 1024);
1462         }
1463 #endif
1464 quit:
1465         if (-1 == shmdt(buf))
1466                 bb_perror_msg ("could not detach sharedmem buf");
1467 }
1468
1469 static void no_scsi (void)
1470 {
1471         if (is_scsi_hd) {
1472                 bb_error_msg_and_die(" operation not supported on SCSI disks\n");
1473         }
1474 }
1475
1476 static void no_xt (void)
1477 {
1478         if (is_xt_hd) {
1479                 bb_error_msg_and_die (" operation not supported on XT disks\n");
1480         }
1481 }
1482
1483 static void on_off (unsigned int value)
1484 {
1485         printf(value ? " (on)\n" : " (off)\n");
1486 }
1487
1488 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1489 static void bus_state_value (unsigned int value)
1490 {
1491         const char *string;
1492
1493         switch (value) {
1494         case BUSSTATE_ON:
1495                 string = " (on)\n";
1496                 break;
1497         case BUSSTATE_OFF:
1498                 string = " (off)\n";
1499                 break;
1500         case BUSSTATE_TRISTATE:
1501                 string = " (tristate)\n";
1502                 break;
1503         default:
1504                 string = " (unknown: %d)\n";
1505                 break;
1506         }
1507         printf(string, value);
1508 }
1509 #endif
1510
1511 #ifdef HDIO_DRIVE_CMD
1512 static void interpret_standby (unsigned int standby)
1513 {
1514         printf(" (");
1515         switch(standby) {
1516                 case 0:         printf("off");
1517                                 break;
1518                 case 252:       printf("21 minutes");
1519                                 break;
1520                 case 253:       printf("vendor-specific");
1521                                 break;
1522                 case 254:       printf("?reserved");
1523                                 break;
1524                 case 255:       printf("21 minutes + 15 seconds");
1525                                 break;
1526                 default:
1527                         if (standby <= 240) {
1528                                 unsigned int secs = standby * 5;
1529                                 unsigned int mins = secs / 60;
1530                                 secs %= 60;
1531                                 if (mins)         printf("%u minutes", mins);
1532                                 if (mins && secs) printf(" + ");
1533                                 if (secs)         printf("%u seconds", secs);
1534                         } else if (standby <= 251) {
1535                                 unsigned int mins = (standby - 240) * 30;
1536                                 unsigned int hrs  = mins / 60;
1537                                 mins %= 60;
1538                                 if (hrs)          printf("%u hours", hrs);
1539                                 if (hrs && mins)  printf(" + ");
1540                                 if (mins)         printf("%u minutes", mins);
1541                         } else
1542                                 printf("illegal value");
1543                         break;
1544         }
1545         printf(")\n");
1546 }
1547
1548 struct xfermode_entry {
1549     int val;
1550     const char *name;
1551 };
1552
1553 static const struct xfermode_entry xfermode_table[] = {
1554     { 8,    "pio0" },
1555     { 9,    "pio1" },
1556     { 10,   "pio2" },
1557     { 11,   "pio3" },
1558     { 12,   "pio4" },
1559     { 13,   "pio5" },
1560     { 14,   "pio6" },
1561     { 15,   "pio7" },
1562     { 16,   "sdma0" },
1563     { 17,   "sdma1" },
1564     { 18,   "sdma2" },
1565     { 19,   "sdma3" },
1566     { 20,   "sdma4" },
1567     { 21,   "sdma5" },
1568     { 22,   "sdma6" },
1569     { 23,   "sdma7" },
1570     { 32,   "mdma0" },
1571     { 33,   "mdma1" },
1572     { 34,   "mdma2" },
1573     { 35,   "mdma3" },
1574     { 36,   "mdma4" },
1575     { 37,   "mdma5" },
1576     { 38,   "mdma6" },
1577     { 39,   "mdma7" },
1578     { 64,   "udma0" },
1579     { 65,   "udma1" },
1580     { 66,   "udma2" },
1581     { 67,   "udma3" },
1582     { 68,   "udma4" },
1583     { 69,   "udma5" },
1584     { 70,   "udma6" },
1585     { 71,   "udma7" },
1586     { 0, NULL }
1587 };
1588
1589 static int translate_xfermode(char * name)
1590 {
1591     const struct xfermode_entry *tmp;
1592     char *endptr;
1593     int val = -1;
1594
1595
1596     for (tmp = xfermode_table; tmp->name != NULL; ++tmp) {
1597         if (!strcmp(name, tmp->name))
1598             return tmp->val;
1599     }
1600
1601     val = strtol(name, &endptr, 10);
1602     if (*endptr == '\0')
1603         return val;
1604
1605     return -1;
1606 }
1607
1608 static void interpret_xfermode (unsigned int xfermode)
1609 {
1610         printf(" (");
1611         switch(xfermode) {
1612                 case 0:         printf("default PIO mode");
1613                                 break;
1614                 case 1:         printf("default PIO mode, disable IORDY");
1615                                 break;
1616                 case 8:
1617                 case 9:
1618                 case 10:
1619                 case 11:
1620                 case 12:
1621                 case 13:
1622                 case 14:
1623                 case 15:        printf("PIO flow control mode%u", xfermode-8);
1624                                 break;
1625                 case 16:
1626                 case 17:
1627                 case 18:
1628                 case 19:
1629                 case 20:
1630                 case 21:
1631                 case 22:
1632                 case 23:        printf("singleword DMA mode%u", xfermode-16);
1633                                 break;
1634                 case 32:
1635                 case 33:
1636                 case 34:
1637                 case 35:
1638                 case 36:
1639                 case 37:
1640                 case 38:
1641                 case 39:        printf("multiword DMA mode%u", xfermode-32);
1642                                 break;
1643                 case 64:
1644                 case 65:
1645                 case 66:
1646                 case 67:
1647                 case 68:
1648                 case 69:
1649                 case 70:
1650                 case 71:        printf("UltraDMA mode%u", xfermode-64);
1651                                 break;
1652                 default:
1653                                 printf("unknown, probably not valid");
1654                                 break;
1655         }
1656         printf(")\n");
1657 }
1658 #endif /* HDIO_DRIVE_CMD */
1659
1660 #ifndef VXVM_MAJOR
1661 #define VXVM_MAJOR 199
1662 #endif
1663
1664 #ifndef CCISS_MAJOR
1665 #define CCISS_MAJOR 104
1666 #endif
1667
1668 static void process_dev (char *devname)
1669 {
1670         int fd;
1671         static long parm, multcount;
1672         struct stat stat_buf;
1673 #ifndef HDIO_DRIVE_CMD
1674         int force_operation = 0;
1675 #endif
1676         if (stat(devname,&stat_buf))
1677                 bb_perror_msg_and_die(devname);
1678
1679         switch(major(stat_buf.st_rdev)) {
1680 #ifdef SCSI_DISK0_MAJOR
1681         case (SCSI_DISK0_MAJOR):
1682         case (SCSI_DISK1_MAJOR):
1683         case (SCSI_DISK2_MAJOR):
1684         case (SCSI_DISK3_MAJOR):
1685         case (SCSI_DISK4_MAJOR):
1686         case (SCSI_DISK5_MAJOR):
1687         case (SCSI_DISK6_MAJOR):
1688         case (SCSI_DISK7_MAJOR):
1689 #else
1690         case (SCSI_DISK_MAJOR):
1691 #endif
1692 #ifdef MD_MAJOR
1693         case (MD_MAJOR):
1694 #endif
1695         case (VXVM_MAJOR):
1696 #ifdef LVM_BLK_MAJOR
1697         case (LVM_BLK_MAJOR):
1698 #endif
1699         case (CCISS_MAJOR):
1700                 is_scsi_hd = 1;
1701                 break;
1702 #ifdef XT_DISK_MAJOR
1703         case (XT_DISK_MAJOR):
1704                 is_xt_hd = 1;
1705                 break;
1706 #endif
1707         case IDE0_MAJOR:
1708         case IDE1_MAJOR:
1709 #ifdef IDE2_MAJOR
1710         case IDE2_MAJOR:
1711 #endif
1712 #ifdef IDE3_MAJOR
1713         case IDE3_MAJOR:
1714 #endif
1715 #ifdef IDE4_MAJOR
1716         case IDE4_MAJOR:
1717 #endif
1718 #ifdef IDE5_MAJOR
1719         case IDE5_MAJOR:
1720 #endif
1721 #ifdef IDE6_MAJOR
1722         case IDE6_MAJOR:
1723 #endif
1724 #ifdef IDE7_MAJOR
1725         case IDE7_MAJOR:
1726 #endif
1727 #ifdef IDE8_MAJOR
1728         case IDE8_MAJOR:
1729 #endif
1730 #ifdef IDE9_MAJOR
1731         case IDE9_MAJOR:
1732 #endif
1733                 break;  /* do nothing */
1734         default:
1735                 bb_error_msg_and_die("%s not supported by hdparm",devname);
1736         }
1737
1738         fd = open (devname, O_RDONLY|O_NONBLOCK);
1739         if (fd < 0)
1740                 bb_perror_msg_and_die(devname);
1741         if (!quiet)
1742                 printf("\n%s:\n", devname);
1743
1744         if (set_readahead) {
1745                 if (get_readahead)
1746                         printf(" setting fs readahead to %ld\n", readahead);
1747                 if (ioctl(fd, BLKRASET, readahead)) 
1748                         bb_perror_msg(" BLKRASET failed");
1749         }
1750 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1751         if (unregister_hwif) {
1752                 no_scsi();
1753                 printf(" attempting to unregister hwif#%u\n", hwif);
1754                 if (ioctl(fd, HDIO_UNREGISTER_HWIF, hwif)) 
1755                         bb_perror_msg(" HDIO_UNREGISTER_HWIF failed");
1756         }
1757 #endif
1758 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1759         if (scan_hwif) {
1760                 int     args[3];
1761                 no_scsi();
1762                 printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
1763                 args[0] = hwif_data;
1764                 args[1] = hwif_ctrl;
1765                 args[2] = hwif_irq;
1766                 if (ioctl(fd, HDIO_SCAN_HWIF, args)) 
1767                         bb_perror_msg(" HDIO_SCAN_HWIF failed");
1768         }
1769 #endif
1770         if (set_piomode) {
1771                 no_scsi();
1772                 no_xt();
1773                 if (noisy_piomode) {
1774                         if (piomode == 255)
1775                                 printf(" attempting to auto-tune PIO mode\n");
1776                         else if (piomode < 100)
1777                                 printf(" attempting to set PIO mode to %d\n", piomode);
1778                         else if (piomode < 200)
1779                                 printf(" attempting to set MDMA mode to %d\n", (piomode-100));
1780                         else
1781                                 printf(" attempting to set UDMA mode to %d\n", (piomode-200));
1782                 }
1783                 if (ioctl(fd, HDIO_SET_PIO_MODE, piomode)) 
1784                         bb_perror_msg(" HDIO_SET_PIO_MODE failed");
1785         }
1786         if (set_io32bit) {
1787                 no_scsi();
1788                 no_xt();
1789                 if (get_io32bit)
1790                         printf(" setting 32-bit IO_support flag to %ld\n", io32bit);
1791                 if (ioctl(fd, HDIO_SET_32BIT, io32bit)) 
1792                         bb_perror_msg(" HDIO_SET_32BIT failed");
1793         }
1794         if (set_mult) {
1795                 no_scsi();
1796                 no_xt();
1797                 if (get_mult)
1798                         printf(" setting multcount to %ld\n", mult);
1799                 if (ioctl(fd, HDIO_SET_MULTCOUNT, mult)) 
1800                         bb_perror_msg(" HDIO_SET_MULTCOUNT failed");
1801 #ifndef HDIO_DRIVE_CMD
1802                 else force_operation = 1;
1803 #endif
1804         }
1805         if (set_readonly) {
1806                 if (get_readonly) {
1807                         printf(" setting readonly to %ld", readonly);
1808                         on_off(readonly);
1809                 }
1810                 if (ioctl(fd, BLKROSET, &readonly))
1811                         bb_perror_msg(" BLKROSET failed");
1812         }
1813         if (set_unmask) {
1814                 no_scsi();
1815                 no_xt();
1816                 if (get_unmask) {
1817                         printf(" setting unmaskirq to %ld", unmask);
1818                         on_off(unmask);
1819                 }
1820                 if (ioctl(fd, HDIO_SET_UNMASKINTR, unmask))
1821                         bb_perror_msg(" HDIO_SET_UNMASKINTR failed");
1822         }
1823         if (set_dma) {
1824                 no_scsi();
1825                 if (get_dma) {
1826                         printf(" setting using_dma to %ld", dma);
1827                         on_off(dma);
1828                 }
1829                 if (ioctl(fd, HDIO_SET_DMA, dma))
1830                         bb_perror_msg(" HDIO_SET_DMA failed");
1831         }
1832         if (set_dma_q) {
1833                 no_scsi();
1834                 if (get_dma_q) {
1835                         printf(" setting DMA queue_depth to %ld", dma_q);
1836                         on_off(dma_q);
1837                 }
1838                 if (ioctl(fd, HDIO_SET_QDMA, dma_q))
1839                         bb_perror_msg(" HDIO_SET_QDMA failed");
1840         }
1841         if (set_nowerr) {
1842                 no_scsi();
1843                 no_xt();
1844                 if (get_nowerr) {
1845                         printf(" setting nowerr to %ld", nowerr);
1846                         on_off(nowerr);
1847                 }
1848                 if (ioctl(fd, HDIO_SET_NOWERR, nowerr))
1849                         bb_perror_msg(" HDIO_SET_NOWERR failed");
1850         }
1851         if (set_keep) {
1852                 no_scsi();
1853                 no_xt();
1854                 if (get_keep) {
1855                         printf(" setting keep_settings to %ld", keep);
1856                         on_off(keep);
1857                 }
1858                 if (ioctl(fd, HDIO_SET_KEEPSETTINGS, keep))
1859                         bb_perror_msg(" HDIO_SET_KEEPSETTINGS failed");
1860         }
1861 #ifdef HDIO_DRIVE_CMD
1862         if (set_doorlock) {
1863                 unsigned char args[4] = {0,0,0,0};
1864                 no_scsi();
1865                 no_xt();
1866                 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
1867                 if (get_doorlock) {
1868                         printf(" setting drive doorlock to %ld", doorlock);
1869                         on_off(doorlock);
1870                 }
1871                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1872                         bb_perror_msg(" HDIO_DRIVE_CMD(doorlock) failed");
1873         }
1874         if (set_dkeep) {
1875                 /* lock/unlock the drive's "feature" settings */
1876                 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1877                 no_scsi();
1878                 no_xt();
1879                 if (get_dkeep) {
1880                         printf(" setting drive keep features to %ld", dkeep);
1881                         on_off(dkeep);
1882                 }
1883                 args[2] = dkeep ? 0x66 : 0xcc;
1884                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1885                         bb_perror_msg(" HDIO_DRIVE_CMD(keepsettings) failed");
1886         }
1887         if (set_defects) {
1888                 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
1889                 no_scsi();
1890                 args[2] = defects ? 0x04 : 0x84;
1891                 if (get_defects)
1892                         printf(" setting drive defect-mgmt to %ld\n", defects);
1893                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1894                         bb_perror_msg(" HDIO_DRIVE_CMD(defectmgmt) failed");
1895         }
1896         if (set_prefetch) {
1897                 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
1898                 no_scsi();
1899                 no_xt();
1900                 args[1] = prefetch;
1901                 if (get_prefetch)
1902                         printf(" setting drive prefetch to %ld\n", prefetch);
1903                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1904                         bb_perror_msg(" HDIO_DRIVE_CMD(setprefetch) failed");
1905         }
1906         if (set_xfermode) {
1907                 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
1908                 no_scsi();
1909                 no_xt();
1910                 args[1] = xfermode_requested;
1911                 if (get_xfermode) {
1912                         printf(" setting xfermode to %d", xfermode_requested);
1913                         interpret_xfermode(xfermode_requested);
1914                 }
1915                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1916                         bb_perror_msg(" HDIO_DRIVE_CMD(setxfermode) failed");
1917         }
1918         if (set_lookahead) {
1919                 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1920                 no_scsi();
1921                 no_xt();
1922                 args[2] = lookahead ? 0xaa : 0x55;
1923                 if (get_lookahead) {
1924                         printf(" setting drive read-lookahead to %ld", lookahead);
1925                         on_off(lookahead);
1926                 }
1927                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1928                         bb_perror_msg(" HDIO_DRIVE_CMD(setreadahead) failed");
1929         }
1930         if (set_apmmode) {
1931                 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1932                 no_scsi();
1933                 if (apmmode<1) apmmode=1;
1934                 if (apmmode>255) apmmode=255;
1935                 if (get_apmmode) 
1936                         printf(" setting Advanced Power Management level to");
1937                 if (apmmode==255) { 
1938                         /* disable Advanced Power Management */
1939                         args[2] = 0x85; /* feature register */ 
1940                         if (get_apmmode) printf(" disabled\n");
1941                 } else {
1942                         /* set Advanced Power Management mode */
1943                         args[2] = 0x05; /* feature register */  
1944                         args[1] = apmmode; /* sector count register */
1945                         if (get_apmmode) printf(" 0x%02lX (%ld)\n",apmmode,apmmode);
1946                 } 
1947                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1948                         bb_perror_msg(" HDIO_DRIVE_CMD failed");
1949         }
1950         if (set_wcache) {
1951 #ifdef DO_FLUSHCACHE
1952 #ifndef WIN_FLUSHCACHE
1953 #define WIN_FLUSHCACHE 0xe7
1954 #endif
1955                 unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
1956 #endif /* DO_FLUSHCACHE */
1957                 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1958                 no_scsi();
1959                 no_xt();
1960                 args[2] = wcache ? 0x02 : 0x82;
1961                 if (get_wcache) {
1962                         printf(" setting drive write-caching to %ld", wcache);
1963                         on_off(wcache);
1964                 }
1965 #ifdef DO_FLUSHCACHE
1966                 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
1967                         bb_perror_msg (" HDIO_DRIVE_CMD(flushcache) failed");
1968 #endif /* DO_FLUSHCACHE */
1969                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1970                         bb_perror_msg(" HDIO_DRIVE_CMD(setcache) failed");
1971 #ifdef DO_FLUSHCACHE
1972                 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
1973                         bb_perror_msg (" HDIO_DRIVE_CMD(flushcache) failed");
1974 #endif /* DO_FLUSHCACHE */
1975         }
1976         if (set_standbynow) {
1977 #ifndef WIN_STANDBYNOW1
1978 #define WIN_STANDBYNOW1 0xE0
1979 #endif
1980 #ifndef WIN_STANDBYNOW2
1981 #define WIN_STANDBYNOW2 0x94
1982 #endif
1983                 unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
1984                 unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
1985                 no_scsi();
1986                 if (get_standbynow)
1987                         printf(" issuing standby command\n");
1988                 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
1989                  && ioctl(fd, HDIO_DRIVE_CMD, &args2))
1990                         bb_perror_msg(" HDIO_DRIVE_CMD(standby) failed");
1991         }
1992         if (set_sleepnow) {
1993 #ifndef WIN_SLEEPNOW1
1994 #define WIN_SLEEPNOW1 0xE6
1995 #endif
1996 #ifndef WIN_SLEEPNOW2
1997 #define WIN_SLEEPNOW2 0x99
1998 #endif
1999                 unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2000                 unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2001                 no_scsi();
2002                 if (get_sleepnow)
2003                         printf(" issuing sleep command\n");
2004                 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2005                  && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2006                         bb_perror_msg(" HDIO_DRIVE_CMD(sleep) failed");
2007         }
2008         if (set_seagate) {
2009                 unsigned char args[4] = {0xfb,0,0,0};
2010                 no_scsi();
2011                 no_xt();
2012                 if (get_seagate)
2013                         printf(" disabling Seagate auto powersaving mode\n");
2014                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2015                         bb_perror_msg(" HDIO_DRIVE_CMD(seagatepwrsave) failed");
2016         }
2017         if (set_standby) {
2018                 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2019                 no_scsi();
2020                 no_xt();
2021                 if (get_standby) {
2022                         printf(" setting standby to %lu", standby_requested);
2023                         interpret_standby(standby_requested);
2024                 }
2025                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2026                         bb_perror_msg(" HDIO_DRIVE_CMD(setidle1) failed");
2027         }
2028 #else   /* HDIO_DRIVE_CMD */
2029         if (force_operation) {
2030                 char buf[512];
2031                 flush_buffer_cache(fd);
2032                 if (-1 == read(fd, buf, sizeof(buf)))
2033                         bb_perror_msg(" access failed");
2034         }
2035 #endif  /* HDIO_DRIVE_CMD */
2036
2037         if (!flagcount)
2038                 verbose = 1;
2039
2040         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity) {
2041                 no_scsi();
2042                 multcount = -1;
2043                 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
2044                         if ((verbose && !is_xt_hd) || get_mult)
2045                                 bb_perror_msg(" HDIO_GET_MULTCOUNT failed");
2046                 } else if (verbose | get_mult) {
2047                         printf(" multcount    = %2ld", multcount);
2048                         on_off(multcount);
2049                 }
2050         }
2051         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit) {
2052                 no_scsi();
2053                 no_xt();
2054                 if (ioctl(fd, HDIO_GET_32BIT, &parm))
2055                         bb_perror_msg(" HDIO_GET_32BIT failed");
2056                 else {
2057                         printf(" IO_support   =%3ld (", parm);
2058                         switch (parm) {
2059                                 case 0: printf("default ");
2060                                 case 2: printf("16-bit)\n");
2061                                         break;
2062                                 case 1: printf("32-bit)\n");
2063                                         break;
2064                                 case 3: printf("32-bit w/sync)\n");
2065                                         break;
2066                                 case 8: printf("Request-Queue-Bypass)\n");
2067                                         break;
2068                                 default:printf("\?\?\?)\n");
2069                         }
2070                 }
2071         }
2072         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask) {
2073                 no_scsi();
2074                 no_xt();
2075                 if (ioctl(fd, HDIO_GET_UNMASKINTR, &parm))
2076                         bb_perror_msg(" HDIO_GET_UNMASKINTR failed");
2077                 else {
2078                         printf(" unmaskirq    = %2ld", parm);
2079                         on_off(parm);
2080                 }
2081         }
2082
2083 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GET_DMA
2084         if ((verbose && !is_scsi_hd) || get_dma) {
2085                 no_scsi();
2086                 if (ioctl(fd, HDIO_GET_DMA, &parm))
2087                         bb_perror_msg(" HDIO_GET_DMA failed");
2088                 else {
2089                         printf(" using_dma    = %2ld", parm);
2090                         if (parm == 8)
2091                                 printf(" (DMA-Assisted-PIO)\n");
2092                         else
2093                                 on_off(parm);
2094                 }
2095         }
2096 #endif
2097         if (get_dma_q) {
2098                 no_scsi();
2099                 if(ioctl(fd, HDIO_GET_QDMA, &parm)) {
2100                         bb_perror_msg(" HDIO_GET_QDMA failed");
2101                 } else {
2102                         printf(" queue_depth  = %2ld", parm);
2103                         on_off(parm);
2104                 }
2105         }
2106         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep) {
2107                 no_scsi();
2108                 no_xt();
2109                 if (ioctl(fd, HDIO_GET_KEEPSETTINGS, &parm))
2110                         bb_perror_msg(" HDIO_GET_KEEPSETTINGS failed");
2111                 else {
2112                         printf(" keepsettings = %2ld", parm);
2113                         on_off(parm);
2114                 }
2115         }
2116
2117         if (get_nowerr) {
2118                 no_scsi();
2119                 no_xt();
2120                 if (ioctl(fd, HDIO_GET_NOWERR, &parm))
2121                         bb_perror_msg(" HDIO_GET_NOWERR failed");
2122                 else {
2123                         printf(" nowerr       = %2ld", parm);
2124                         on_off(parm);
2125                 }
2126         }
2127         if (verbose || get_readonly) {
2128                 if (ioctl(fd, BLKROGET, &parm))
2129                         bb_perror_msg(" BLKROGET failed");
2130                 else {
2131                         printf(" readonly     = %2ld", parm);
2132                         on_off(parm);
2133                 }
2134         }
2135         if ((verbose && !is_scsi_hd) || get_readahead) {
2136                 if (ioctl(fd, BLKRAGET, &parm))
2137                         bb_perror_msg(" BLKRAGET failed");
2138                 else {
2139                         printf(" readahead    = %2ld", parm);
2140                         on_off(parm);
2141                 }
2142         }
2143         if (verbose || get_geom) {
2144                 static const char msg[] = " geometry     = %u/%u/%u, sectors = %ld, start = %ld\n";
2145                 static struct hd_geometry g;
2146 #ifdef HDIO_GETGEO_BIG
2147                 static struct hd_big_geometry bg;
2148 #endif
2149
2150                 if (ioctl(fd, BLKGETSIZE, &parm))
2151                         bb_perror_msg(" BLKGETSIZE failed");
2152 #ifdef HDIO_GETGEO_BIG
2153                 else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
2154                         printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
2155 #endif
2156                 else if (ioctl(fd, HDIO_GETGEO, &g))
2157                         bb_perror_msg(" HDIO_GETGEO failed");
2158                 else    printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
2159         }
2160 #ifdef HDIO_DRIVE_CMD
2161         if (get_powermode) {
2162 #ifndef WIN_CHECKPOWERMODE1
2163 #define WIN_CHECKPOWERMODE1 0xE5
2164 #endif
2165 #ifndef WIN_CHECKPOWERMODE2
2166 #define WIN_CHECKPOWERMODE2 0x98
2167 #endif
2168                 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
2169                 const char *state;
2170                 no_scsi();
2171                 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
2172                  && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2173                  && ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2174                         if (errno != EIO || args[0] != 0 || args[1] != 0)
2175                                 state = "unknown";
2176                         else
2177                                 state = "sleeping";
2178                 } else {
2179                         state = (args[2] == 255) ? "active/idle" : "standby";
2180                 }
2181                 printf(" drive state is:  %s\n", state);
2182         }
2183 #endif
2184 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2185         if (perform_reset) {
2186                 no_scsi();
2187                 no_xt();
2188                 if (ioctl(fd, HDIO_DRIVE_RESET, NULL))
2189                         bb_perror_msg(" HDIO_DRIVE_RESET failed");
2190         }
2191 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2192 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2193         if (perform_tristate) {
2194                 unsigned char args[4] = {0,tristate,0,0};
2195                 no_scsi();
2196                 no_xt();
2197                 if (ioctl(fd, HDIO_TRISTATE_HWIF, &args))
2198                         bb_perror_msg(" HDIO_TRISTATE_HWIF failed");
2199         }
2200 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2201 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2202         if (get_identity) {
2203                 static struct hd_driveid id;
2204
2205                 no_scsi();
2206                 no_xt();
2207
2208                 if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
2209                         if (multcount != -1) {
2210                                 id.multsect = multcount;
2211                                 id.multsect_valid |= 1;
2212                         } else
2213                                 id.multsect_valid &= ~1;
2214                         dump_identity(&id);
2215                 } else if (errno == -ENOMSG)
2216                         printf(" no identification info available\n");
2217                 else
2218                         bb_perror_msg(" HDIO_GET_IDENTITY failed");
2219         }
2220         if (get_IDentity) {
2221                 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2222                 unsigned i;
2223                 no_scsi();
2224                 no_xt();
2225                 if (ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2226                         args[0] = WIN_PIDENTIFY;
2227                         if (ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2228                                 bb_perror_msg(" HDIO_DRIVE_CMD(identify) failed");
2229                                 goto identify_abort;
2230                         }
2231                 }
2232                 for(i=0; i<(sizeof args)/2; i+=2) {
2233                     __le16_to_cpus((uint16_t *)(&args[i]));
2234                 }    
2235                 identify((void *)&args[4], NULL);
2236 identify_abort: ;
2237         }
2238 #endif
2239 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2240         if (set_busstate) {
2241                 no_scsi();
2242                 if (get_busstate) {
2243                         printf(" setting bus state to %d", busstate);
2244                         bus_state_value(busstate);
2245                 }
2246                 if (ioctl(fd, HDIO_SET_BUSSTATE, busstate))
2247                         bb_perror_msg(" HDIO_SET_BUSSTATE failed");
2248         }
2249 #endif
2250 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2251         if (get_busstate) {
2252                 no_scsi();
2253                 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
2254                         bb_perror_msg(" HDIO_GET_BUSSTATE failed");
2255                 else {
2256                         printf(" busstate     = %2ld", parm);
2257                         bus_state_value(parm);
2258                 }
2259         }
2260 #endif
2261         if (reread_partn) {
2262                 if (ioctl(fd, BLKRRPART, NULL)) {
2263                         bb_perror_msg(" BLKRRPART failed");
2264                 }
2265         }
2266
2267         if (do_ctimings)
2268                 time_cache (fd);
2269         if (do_timings)
2270                 time_device (fd);
2271         if (do_flush)
2272                 flush_buffer_cache (fd);
2273         close (fd);
2274 }
2275
2276 #define GET_NUMBER(flag,num)    num = 0; \
2277                                 if (!*p && argc && isdigit(**argv)) \
2278                                         p = *argv++, --argc; \
2279                                 while (isdigit(*p)) { \
2280                                         flag = 1; \
2281                                         num = (num * 10) + (*p++ - '0'); \
2282                                 }
2283
2284 #define GET_STRING(flag, num) tmpstr = name; \
2285                                 tmpstr[0] = '\0'; \
2286                                 if (!*p && argc && isalnum(**argv)) \
2287                                         p = *argv++, --argc; \
2288                                 while (isalnum(*p) && (tmpstr - name) < 31) { \
2289                                         tmpstr[0] = *p++; \
2290                                         tmpstr[1] = '\0'; \
2291                                         ++tmpstr; \
2292                                 } \
2293                                 num = translate_xfermode(name); \
2294                                 if (num == -1) \
2295                                         flag = 0; \
2296                                 else \
2297                                         flag = 1;
2298
2299 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2300 static int fromhex (unsigned char c)
2301 {
2302         if (c >= 'a' && c <= 'f')
2303                 return 10 + (c - 'a');
2304         if (c >= '0' && c <= '9')
2305                 return (c - '0');
2306         bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2307 }
2308
2309 static int identify_from_stdin (void)
2310 {
2311         unsigned short sbuf[800];
2312         unsigned char  buf[1600], *b = (unsigned char *)buf;
2313         int i, count = read(0, buf, 1280);
2314
2315         if (count != 1280)
2316                 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
2317         for (i = 0; count >= 4; ++i) {
2318                 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2319                 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2320                 b += 5;
2321                 count -= 5;
2322         }
2323         identify(sbuf, NULL);
2324         return 0;
2325 }
2326 #endif
2327
2328 /* our main() routine: */
2329 int hdparm_main(int argc, char **argv)
2330 {
2331         char c, *p;
2332         char *tmpstr;
2333         char name[32];
2334         int neg;
2335
2336         ++argv;
2337         if (!--argc)
2338                 bb_show_usage();
2339         while (argc--) {
2340 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2341                 if (!strcmp("-Istdin", *argv)) {
2342                         exit(identify_from_stdin());
2343                 }
2344 #endif
2345                 p = *argv++;
2346                 if (*p == '-') {
2347                         if (!*++p)
2348                                 bb_show_usage();
2349                         while ((c = *p++)) {
2350                                 ++flagcount;
2351                                 switch (c) {
2352                                         case 'V':
2353                                                 bb_error_msg_and_die("%s", VERSION);
2354                                         case 'v':
2355                                                 verbose = 1;
2356                                                 break;
2357 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2358                                         case 'I':
2359                                                 get_IDentity = 1;
2360                                                 break;
2361                                         case 'i':
2362                                                 get_identity = 1;
2363                                                 break;
2364 #endif
2365                                         case 'g':
2366                                                 get_geom = 1;
2367                                                 break;
2368                                         case 'f':
2369                                                 do_flush = 1;
2370                                                 break;
2371                                         case 'q':
2372                                                 quiet = 1;
2373                                                 noisy = 0;
2374                                                 break;
2375                                         case 'u':
2376                                                 get_unmask = noisy;
2377                                                 noisy = 1;
2378                                                 if (!*p && argc && isdigit(**argv))
2379                                                         p = *argv++, --argc;
2380                                                 if (*p == '0' || *p == '1') {
2381                                                         set_unmask = 1;
2382                                                         unmask = *p++ - '0';
2383                                                 }
2384                                                 break;
2385                                         case 'd':
2386                                                 get_dma = noisy;
2387                                                 noisy = 1;
2388                                                 if (!*p && argc && isdigit(**argv))
2389                                                         p = *argv++, --argc;
2390                                                 if (*p >= '0' && *p <= '9') {
2391                                                         set_dma = 1;
2392                                                         dma = *p++ - '0';
2393                                                 }
2394                                                 break;
2395                                         case 'n':
2396                                                 get_nowerr = noisy;
2397                                                 noisy = 1;
2398                                                 if (!*p && argc && isdigit(**argv))
2399                                                         p = *argv++, --argc;
2400                                                 if (*p == '0' || *p == '1') {
2401                                                         set_nowerr = 1;
2402                                                         nowerr = *p++ - '0';
2403                                                 }
2404                                                 break;
2405                                         case 'p':
2406                                                 noisy_piomode = noisy;
2407                                                 noisy = 1;
2408                                                 GET_STRING(set_piomode,piomode);
2409                                                 break;
2410                                         case 'r':
2411                                                 get_readonly = noisy;
2412                                                 noisy = 1;
2413                                                 if (!*p && argc && isdigit(**argv))
2414                                                         p = *argv++, --argc;
2415                                                 if (*p == '0' || *p == '1') {
2416                                                         set_readonly = 1;
2417                                                         readonly = *p++ - '0';
2418                                                 }
2419                                                 break;
2420                                         case 'm':
2421                                                 get_mult = noisy;
2422                                                 noisy = 1;
2423                                                 GET_NUMBER(set_mult,mult);
2424                                                 break;
2425                                         case 'c':
2426                                                 get_io32bit = noisy;
2427                                                 noisy = 1;
2428                                                 GET_NUMBER(set_io32bit,io32bit);
2429                                                 break;
2430 #ifdef HDIO_DRIVE_CMD
2431                                         case 'S':
2432                                                 get_standby = noisy;
2433                                                 noisy = 1;
2434                                                 GET_NUMBER(set_standby,standby_requested);
2435                                                 if (!set_standby)
2436                                                         bb_error_msg("-S: missing value");
2437                                                 break;
2438
2439                                         case 'D':
2440                                                 get_defects = noisy;
2441                                                 noisy = 1;
2442                                                 GET_NUMBER(set_defects,defects);
2443                                                 if (!set_defects)
2444                                                         bb_error_msg("-D: missing value");
2445                                                 break;
2446                                         case 'P':
2447                                                 get_prefetch = noisy;
2448                                                 noisy = 1;
2449                                                 GET_NUMBER(set_prefetch,prefetch);
2450                                                 if (!set_prefetch)
2451                                                         bb_error_msg("-P: missing value");
2452                                                 break;
2453
2454                                         case 'X':
2455                                                 get_xfermode = noisy;
2456                                                 noisy = 1;
2457                                                 GET_STRING(set_xfermode,xfermode_requested);
2458                                                 if (!set_xfermode)
2459                                                         bb_error_msg("-X: missing value");
2460                                                 break;
2461
2462                                         case 'K':
2463                                                 get_dkeep = noisy;
2464                                                 noisy = 1;
2465                                                 if (!*p && argc && isdigit(**argv))
2466                                                         p = *argv++, --argc;
2467                                                 if (*p == '0' || *p == '1') {
2468                                                         set_dkeep = 1;
2469                                                         dkeep = *p++ - '0';
2470                                                 } else
2471                                                         bb_error_msg("-K: missing value (0/1)");
2472                                                 break;
2473
2474                                         case 'A':
2475                                                 get_lookahead = noisy;
2476                                                 noisy = 1;
2477                                                 if (!*p && argc && isdigit(**argv))
2478                                                         p = *argv++, --argc;
2479                                                 if (*p == '0' || *p == '1') {
2480                                                         set_lookahead = 1;
2481                                                         lookahead = *p++ - '0';
2482                                                 } else
2483                                                         bb_error_msg("-A: missing value (0/1)");
2484                                                 break;
2485
2486                                         case 'L':
2487                                                 get_doorlock = noisy;
2488                                                 noisy = 1;
2489                                                 if (!*p && argc && isdigit(**argv))
2490                                                         p = *argv++, --argc;
2491                                                 if (*p == '0' || *p == '1') {
2492                                                         set_doorlock = 1;
2493                                                         doorlock = *p++ - '0';
2494                                                 } else
2495                                                         bb_error_msg("-L: missing value (0/1)");
2496                                                 break;
2497
2498                                         case 'W':
2499                                                 get_wcache = noisy;
2500                                                 noisy = 1;
2501                                                 if (!*p && argc && isdigit(**argv))
2502                                                         p = *argv++, --argc;
2503                                                 if (*p == '0' || *p == '1') {
2504                                                         set_wcache = 1;
2505                                                         wcache = *p++ - '0';
2506                                                 } else
2507                                                         bb_error_msg("-W: missing value (0/1)");
2508                                                 break;
2509
2510                                         case 'C':
2511                                                 get_powermode = noisy;
2512                                                 noisy = 1;
2513                                                 break;
2514
2515                                         case 'y':
2516                                                 get_standbynow = noisy;
2517                                                 noisy = 1;
2518                                                 set_standbynow = 1;
2519                                                 break;
2520
2521                                         case 'Y':
2522                                                 get_sleepnow = noisy;
2523                                                 noisy = 1;
2524                                                 set_sleepnow = 1;
2525                                                 break;
2526
2527                                         case 'z':
2528                                                 reread_partn = 1;
2529                                                 break;
2530
2531                                         case 'Z':
2532                                                 get_seagate = noisy;
2533                                                 noisy = 1;
2534                                                 set_seagate = 1;
2535                                                 break;
2536 #endif /* HDIO_DRIVE_CMD */
2537                                         case 'k':
2538                                                 get_keep = noisy;
2539                                                 noisy = 1;
2540                                                 if (!*p && argc && isdigit(**argv))
2541                                                         p = *argv++, --argc;
2542                                                 if (*p == '0' || *p == '1') {
2543                                                         set_keep = 1;
2544                                                         keep = *p++ - '0';
2545                                                 }
2546                                                 break;
2547 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2548                                         case 'U':
2549                                                 if (!*p && argc && isdigit(**argv))
2550                                                         p = *argv++, --argc;
2551
2552                                                 if(! p) {
2553                                                         bb_error_msg_and_die("expected hwif_nr");
2554                                                 }
2555
2556                                                 sscanf(p++, "%i", &hwif);
2557
2558                                                 unregister_hwif = 1;
2559                                                 break;
2560 #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2561 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2562                                         case 'R':
2563                                                 if (!*p && argc && isdigit(**argv))
2564                                                         p = *argv++, --argc;
2565
2566                                                 if(! p) {
2567                                                         bb_error_msg_and_die("expected hwif_data");
2568                                                 }
2569
2570                                                 sscanf(p++, "%i", &hwif_data);
2571
2572                                                 if (argc && isdigit(**argv))
2573                                                         p = *argv++, --argc;
2574                                                 else {
2575                                                         bb_error_msg_and_die("expected hwif_ctrl");
2576                                                 }
2577
2578                                                 sscanf(p, "%i", &hwif_ctrl);
2579
2580                                                 if (argc && isdigit(**argv))
2581                                                         p = *argv++, --argc;
2582                                                 else {
2583                                                         bb_error_msg_and_die("expected hwif_irq");
2584                                                 }
2585
2586                                                 sscanf(p, "%i", &hwif_irq);
2587
2588                                                 *p = '\0';
2589
2590                                                 scan_hwif = 1;
2591                                                 break;
2592 #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2593                                         case 'Q':
2594                                                 get_dma_q = noisy;
2595                                                 noisy = 1;
2596                                                 neg = 0;
2597                                                 GET_NUMBER(set_dma_q, dma_q);
2598                                                 if (neg)
2599                                                         dma_q = -dma_q;
2600                                                 break;
2601 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2602                                         case 'w':
2603                                                 perform_reset = 1;
2604                                                 break;
2605 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2606 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2607                                         case 'x':
2608                                                 if (!*p && argc && isdigit(**argv))
2609                                                         p = *argv++, --argc;
2610                                                 if (*p == '0' || *p == '1') {
2611                                                         perform_tristate = 1;
2612                                                         tristate = *p++ - '0';
2613                                                 } else
2614                                                         bb_error_msg("-x: missing value (0/1)");
2615                                                 break;
2616
2617 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2618                                         case 'a':
2619                                                 get_readahead = noisy;
2620                                                 noisy = 1;
2621                                                 GET_NUMBER(set_readahead,readahead);
2622                                                 break;
2623                                         case 'B':
2624                                                 get_apmmode = noisy;
2625                                                 noisy = 1;
2626                                                 GET_NUMBER(set_apmmode,apmmode);
2627                                                 if (!set_apmmode)
2628                                                 printf("-B: missing value (1-255)");
2629                                                 break;
2630                                         case 't':
2631                                                 do_timings = 1;
2632                                                 do_flush = 1;
2633                                                 break;
2634                                         case 'T':
2635                                                 do_ctimings = 1;
2636                                                 do_flush = 1;
2637                                                 break;
2638 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2639                                         case 'b':
2640                                                 get_busstate = noisy;
2641                                                 noisy = 1;
2642                                                 if (!*p && argc && isdigit(**argv))
2643                                                         p = *argv++, --argc;
2644                                                 switch (*p) {
2645                                                         case '0':
2646                                                         case '1':
2647                                                         case '2':
2648                                                                 set_busstate = 1;
2649                                                                 busstate = *p++ - '0';
2650                                                                 break;
2651                                                         default:
2652                                                                 break;
2653                                                 }
2654                                                 break;
2655 #endif
2656                                         case 'h':
2657                                         default:
2658                                                 bb_show_usage();
2659                                 }
2660                         }
2661                         if (!argc)
2662                                 bb_show_usage();
2663                 } else {
2664                         process_dev (p);
2665                 }
2666         }
2667         return 0 ;
2668 }