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