Home | History | Annotate | Line # | Download | only in atactl
atactl.c revision 1.12
      1  1.12     ad /*	$NetBSD: atactl.c,v 1.12 2001/08/28 15:36:54 ad Exp $	*/
      2   1.1   kenh 
      3   1.1   kenh /*-
      4   1.1   kenh  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1   kenh  * All rights reserved.
      6   1.1   kenh  *
      7   1.1   kenh  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   kenh  * by Ken Hornstein.
      9   1.1   kenh  *
     10   1.1   kenh  * Redistribution and use in source and binary forms, with or without
     11   1.1   kenh  * modification, are permitted provided that the following conditions
     12   1.1   kenh  * are met:
     13   1.1   kenh  * 1. Redistributions of source code must retain the above copyright
     14   1.1   kenh  *    notice, this list of conditions and the following disclaimer.
     15   1.1   kenh  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   kenh  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   kenh  *    documentation and/or other materials provided with the distribution.
     18   1.1   kenh  * 3. All advertising materials mentioning features or use of this software
     19   1.1   kenh  *    must display the following acknowledgement:
     20   1.1   kenh  *	This product includes software developed by the NetBSD
     21   1.1   kenh  *	Foundation, Inc. and its contributors.
     22   1.1   kenh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1   kenh  *    contributors may be used to endorse or promote products derived
     24   1.1   kenh  *    from this software without specific prior written permission.
     25   1.1   kenh  *
     26   1.1   kenh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1   kenh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1   kenh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1   kenh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1   kenh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1   kenh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1   kenh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1   kenh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1   kenh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1   kenh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1   kenh  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1   kenh  */
     38   1.1   kenh 
     39   1.1   kenh /*
     40   1.4  jwise  * atactl(8) - a program to control ATA devices.
     41   1.1   kenh  */
     42   1.1   kenh 
     43   1.1   kenh #include <sys/param.h>
     44   1.1   kenh #include <sys/ioctl.h>
     45   1.1   kenh #include <err.h>
     46   1.1   kenh #include <errno.h>
     47   1.1   kenh #include <fcntl.h>
     48   1.1   kenh #include <stdio.h>
     49   1.1   kenh #include <stdlib.h>
     50   1.1   kenh #include <string.h>
     51   1.1   kenh #include <unistd.h>
     52   1.1   kenh #include <util.h>
     53   1.1   kenh 
     54   1.1   kenh #include <dev/ata/atareg.h>
     55   1.1   kenh #include <dev/ic/wdcreg.h>
     56   1.1   kenh #include <sys/ataio.h>
     57   1.1   kenh 
     58   1.1   kenh struct command {
     59   1.1   kenh 	const char *cmd_name;
     60   1.5  soren 	const char *arg_names;
     61   1.1   kenh 	void (*cmd_func) __P((int, char *[]));
     62   1.1   kenh };
     63   1.1   kenh 
     64   1.1   kenh struct bitinfo {
     65   1.1   kenh 	u_int bitmask;
     66   1.1   kenh 	const char *string;
     67   1.1   kenh };
     68   1.1   kenh 
     69   1.1   kenh int	main __P((int, char *[]));
     70   1.1   kenh void	usage __P((void));
     71   1.1   kenh void	ata_command __P((struct atareq *));
     72  1.10     is void	print_bitinfo __P((const char *, const char *, u_int, struct bitinfo *));
     73   1.1   kenh 
     74   1.1   kenh int	fd;				/* file descriptor for device */
     75   1.1   kenh const	char *dvname;			/* device name */
     76   1.1   kenh char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     77   1.1   kenh const	char *cmdname;			/* command user issued */
     78   1.5  soren const	char *argnames;			/* helpstring: expected arguments */
     79   1.1   kenh 
     80   1.1   kenh void	device_identify __P((int, char *[]));
     81   1.1   kenh void	device_setidle __P((int, char *[]));
     82   1.1   kenh void	device_idle __P((int, char *[]));
     83   1.3   kenh void	device_checkpower __P((int, char *[]));
     84   1.1   kenh 
     85   1.1   kenh struct command commands[] = {
     86   1.5  soren 	{ "identify",	"",			device_identify },
     87   1.5  soren 	{ "setidle",	"idle-timer",		device_setidle },
     88   1.5  soren 	{ "setstandby",	"standby-timer",	device_setidle },
     89   1.5  soren 	{ "idle",	"",			device_idle },
     90   1.5  soren 	{ "standby",	"",			device_idle },
     91   1.5  soren 	{ "sleep",	"",			device_idle },
     92   1.5  soren 	{ "checkpower",	"",			device_checkpower },
     93   1.5  soren 	{ NULL,		NULL,			NULL },
     94   1.1   kenh };
     95   1.1   kenh 
     96   1.1   kenh /*
     97   1.1   kenh  * Tables containing bitmasks used for error reporting and
     98   1.1   kenh  * device identification.
     99   1.1   kenh  */
    100   1.1   kenh 
    101   1.1   kenh struct bitinfo ata_caps[] = {
    102   1.1   kenh 	{ ATA_CAP_STBY, "ATA standby timer values" },
    103   1.1   kenh 	{ WDC_CAP_IORDY, "IORDY operation" },
    104   1.1   kenh 	{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
    105   1.1   kenh 	{ NULL, NULL },
    106   1.1   kenh };
    107   1.1   kenh 
    108   1.1   kenh struct bitinfo ata_vers[] = {
    109   1.1   kenh 	{ WDC_VER_ATA1,	"ATA-1" },
    110   1.1   kenh 	{ WDC_VER_ATA2,	"ATA-2" },
    111   1.1   kenh 	{ WDC_VER_ATA3,	"ATA-3" },
    112   1.1   kenh 	{ WDC_VER_ATA4,	"ATA-4" },
    113   1.1   kenh 	{ NULL, NULL },
    114   1.1   kenh };
    115   1.1   kenh 
    116   1.1   kenh struct bitinfo ata_cmd_set1[] = {
    117   1.1   kenh 	{ WDC_CMD1_NOP, "NOP command" },
    118   1.1   kenh 	{ WDC_CMD1_RB, "READ BUFFER command" },
    119   1.1   kenh 	{ WDC_CMD1_WB, "WRITE BUFFER command" },
    120   1.1   kenh 	{ WDC_CMD1_HPA, "Host Protected Area feature set" },
    121   1.1   kenh 	{ WDC_CMD1_DVRST, "DEVICE RESET command" },
    122   1.1   kenh 	{ WDC_CMD1_SRV, "SERVICE interrupt" },
    123   1.1   kenh 	{ WDC_CMD1_RLSE, "release interrupt" },
    124   1.1   kenh 	{ WDC_CMD1_AHEAD, "look-ahead" },
    125   1.1   kenh 	{ WDC_CMD1_CACHE, "write cache" },
    126   1.1   kenh 	{ WDC_CMD1_PKT, "PACKET command feature set" },
    127   1.1   kenh 	{ WDC_CMD1_PM, "Power Management feature set" },
    128   1.1   kenh 	{ WDC_CMD1_REMOV, "Removable Media feature set" },
    129   1.1   kenh 	{ WDC_CMD1_SEC, "Security Mode feature set" },
    130   1.1   kenh 	{ WDC_CMD1_SMART, "SMART feature set" },
    131   1.1   kenh 	{ NULL, NULL },
    132   1.1   kenh };
    133   1.1   kenh 
    134   1.1   kenh struct bitinfo ata_cmd_set2[] = {
    135   1.1   kenh 	{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
    136   1.1   kenh 	{ ATA_CMD2_APM, "Advanced Power Management feature set" },
    137   1.1   kenh 	{ ATA_CMD2_CFA, "CFA feature set" },
    138   1.6  soren 	{ ATA_CMD2_RWQ, "READ/WRITE DMA QUEUED commands" },
    139   1.1   kenh 	{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
    140   1.1   kenh 	{ NULL, NULL },
    141   1.1   kenh };
    142   1.1   kenh 
    143   1.1   kenh int
    144   1.1   kenh main(argc, argv)
    145   1.1   kenh 	int argc;
    146   1.1   kenh 	char *argv[];
    147   1.1   kenh {
    148   1.1   kenh 	int i;
    149   1.1   kenh 
    150   1.1   kenh 	/* Must have at least: device command */
    151   1.1   kenh 	if (argc < 3)
    152   1.1   kenh 		usage();
    153   1.1   kenh 
    154   1.1   kenh 	/* Skip program name, get and skip device name and command. */
    155   1.1   kenh 	dvname = argv[1];
    156   1.1   kenh 	cmdname = argv[2];
    157   1.1   kenh 	argv += 3;
    158   1.1   kenh 	argc -= 3;
    159   1.1   kenh 
    160   1.1   kenh 	/*
    161   1.1   kenh 	 * Open the device
    162   1.1   kenh 	 */
    163   1.1   kenh 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    164   1.1   kenh 	if (fd == -1) {
    165   1.1   kenh 		if (errno == ENOENT) {
    166   1.1   kenh 			/*
    167   1.1   kenh 			 * Device doesn't exist.  Probably trying to open
    168   1.1   kenh 			 * a device which doesn't use disk semantics for
    169   1.1   kenh 			 * device name.  Try again, specifying "cooked",
    170   1.1   kenh 			 * which leaves off the "r" in front of the device's
    171   1.1   kenh 			 * name.
    172   1.1   kenh 			 */
    173   1.1   kenh 			fd = opendisk(dvname, O_RDWR, dvname_store,
    174   1.1   kenh 			    sizeof(dvname_store), 1);
    175   1.1   kenh 			if (fd == -1)
    176   1.1   kenh 				err(1, "%s", dvname);
    177   1.4  jwise 		} else
    178   1.4  jwise 			err(1, "%s", dvname);
    179   1.1   kenh 	}
    180   1.1   kenh 
    181   1.1   kenh 	/*
    182   1.1   kenh 	 * Point the dvname at the actual device name that opendisk() opened.
    183   1.1   kenh 	 */
    184   1.1   kenh 	dvname = dvname_store;
    185   1.1   kenh 
    186   1.1   kenh 	/* Look up and call the command. */
    187   1.1   kenh 	for (i = 0; commands[i].cmd_name != NULL; i++)
    188   1.1   kenh 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    189   1.1   kenh 			break;
    190   1.1   kenh 	if (commands[i].cmd_name == NULL)
    191  1.12     ad 		errx(1, "unknown command: %s", cmdname);
    192   1.1   kenh 
    193   1.5  soren 	argnames = commands[i].arg_names;
    194   1.5  soren 
    195   1.1   kenh 	(*commands[i].cmd_func)(argc, argv);
    196   1.1   kenh 	exit(0);
    197   1.1   kenh }
    198   1.1   kenh 
    199   1.1   kenh void
    200   1.1   kenh usage()
    201   1.1   kenh {
    202   1.5  soren 	int i;
    203   1.1   kenh 
    204   1.5  soren 	fprintf(stderr, "Usage: %s device command [arg [...]]\n",
    205  1.11    cgd 	    getprogname());
    206   1.5  soren 
    207   1.5  soren 	fprintf(stderr, "   Available device commands:\n");
    208   1.5  soren 	for (i=0; commands[i].cmd_name != NULL; i++)
    209   1.5  soren 		fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
    210   1.5  soren 					    commands[i].arg_names);
    211   1.5  soren 
    212   1.1   kenh 	exit(1);
    213   1.1   kenh }
    214   1.1   kenh 
    215   1.1   kenh /*
    216   1.1   kenh  * Wrapper that calls ATAIOCCOMMAND and checks for errors
    217   1.1   kenh  */
    218   1.1   kenh 
    219   1.1   kenh void
    220   1.1   kenh ata_command(req)
    221   1.1   kenh 	struct atareq *req;
    222   1.1   kenh {
    223   1.1   kenh 	int error;
    224   1.1   kenh 
    225   1.1   kenh 	error = ioctl(fd, ATAIOCCOMMAND, req);
    226   1.1   kenh 
    227   1.1   kenh 	if (error == -1)
    228   1.1   kenh 		err(1, "ATAIOCCOMMAND failed");
    229   1.1   kenh 
    230   1.1   kenh 	switch (req->retsts) {
    231   1.1   kenh 
    232   1.1   kenh 	case ATACMD_OK:
    233   1.1   kenh 		return;
    234   1.1   kenh 	case ATACMD_TIMEOUT:
    235   1.1   kenh 		fprintf(stderr, "ATA command timed out\n");
    236   1.1   kenh 		exit(1);
    237   1.1   kenh 	case ATACMD_DF:
    238   1.1   kenh 		fprintf(stderr, "ATA device returned a Device Fault\n");
    239   1.1   kenh 		exit(1);
    240   1.1   kenh 	case ATACMD_ERROR:
    241   1.1   kenh 		if (req->error & WDCE_ABRT)
    242   1.1   kenh 			fprintf(stderr, "ATA device returned Aborted "
    243   1.1   kenh 				"Command\n");
    244   1.1   kenh 		else
    245   1.1   kenh 			fprintf(stderr, "ATA device returned error register "
    246   1.1   kenh 				"%0x\n", req->error);
    247   1.1   kenh 		exit(1);
    248   1.1   kenh 	default:
    249   1.1   kenh 		fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
    250   1.1   kenh 			"%d\n", req->retsts);
    251   1.1   kenh 		exit(1);
    252   1.1   kenh 	}
    253   1.1   kenh }
    254   1.1   kenh 
    255   1.1   kenh /*
    256   1.1   kenh  * Print out strings associated with particular bitmasks
    257   1.1   kenh  */
    258   1.1   kenh 
    259   1.1   kenh void
    260  1.10     is print_bitinfo(bf, af, bits, binfo)
    261  1.10     is 	const char *bf, *af;
    262   1.1   kenh 	u_int bits;
    263   1.1   kenh 	struct bitinfo *binfo;
    264   1.1   kenh {
    265   1.1   kenh 
    266   1.1   kenh 	for (; binfo->bitmask != NULL; binfo++)
    267   1.1   kenh 		if (bits & binfo->bitmask)
    268  1.10     is 			printf("%s%s%s", bf, binfo->string, af);
    269   1.1   kenh }
    270   1.1   kenh 
    271   1.1   kenh /*
    272   1.1   kenh  * DEVICE COMMANDS
    273   1.1   kenh  */
    274   1.1   kenh 
    275   1.1   kenh /*
    276   1.1   kenh  * device_identify:
    277   1.1   kenh  *
    278   1.1   kenh  *	Display the identity of the device
    279   1.1   kenh  */
    280   1.1   kenh void
    281   1.1   kenh device_identify(argc, argv)
    282   1.1   kenh 	int argc;
    283   1.1   kenh 	char *argv[];
    284   1.1   kenh {
    285   1.1   kenh 	struct ataparams *inqbuf;
    286   1.1   kenh 	struct atareq req;
    287   1.1   kenh 	unsigned char inbuf[DEV_BSIZE];
    288   1.2   kenh #if BYTE_ORDER == LITTLE_ENDIAN
    289   1.1   kenh 	int i;
    290   1.1   kenh 	u_int16_t *p;
    291   1.1   kenh #endif
    292   1.1   kenh 
    293   1.1   kenh 	/* No arguments. */
    294   1.1   kenh 	if (argc != 0)
    295   1.5  soren 		usage();
    296   1.1   kenh 
    297   1.1   kenh 	memset(&inbuf, 0, sizeof(inbuf));
    298   1.1   kenh 	memset(&req, 0, sizeof(req));
    299   1.1   kenh 
    300   1.1   kenh 	inqbuf = (struct ataparams *) inbuf;
    301   1.1   kenh 
    302   1.1   kenh 	req.flags = ATACMD_READ;
    303   1.1   kenh 	req.command = WDCC_IDENTIFY;
    304   1.1   kenh 	req.databuf = (caddr_t) inbuf;
    305   1.1   kenh 	req.datalen = sizeof(inbuf);
    306   1.1   kenh 	req.timeout = 1000;
    307   1.1   kenh 
    308   1.1   kenh 	ata_command(&req);
    309   1.1   kenh 
    310   1.1   kenh #if BYTE_ORDER == LITTLE_ENDIAN
    311   1.1   kenh 	/*
    312   1.1   kenh 	 * On little endian machines, we need to shuffle the string
    313   1.1   kenh 	 * byte order.  However, we don't have to do this for NEC or
    314   1.1   kenh 	 * Mitsumi ATAPI devices
    315   1.1   kenh 	 */
    316   1.1   kenh 
    317   1.1   kenh 	if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
    318   1.1   kenh 	      ((inqbuf->atap_model[0] == 'N' &&
    319   1.1   kenh 		  inqbuf->atap_model[1] == 'E') ||
    320   1.1   kenh 	       (inqbuf->atap_model[0] == 'F' &&
    321   1.1   kenh 		  inqbuf->atap_model[1] == 'X')))) {
    322   1.1   kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_model); i += 2) {
    323   1.1   kenh 			p = (u_short *) (inqbuf->atap_model + i);
    324   1.1   kenh 			*p = ntohs(*p);
    325   1.1   kenh 		}
    326   1.1   kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_serial); i += 2) {
    327   1.1   kenh 			p = (u_short *) (inqbuf->atap_serial + i);
    328   1.1   kenh 			*p = ntohs(*p);
    329   1.1   kenh 		}
    330   1.1   kenh 		for (i = 0 ; i < sizeof(inqbuf->atap_revision); i += 2) {
    331   1.1   kenh 			p = (u_short *) (inqbuf->atap_revision + i);
    332   1.1   kenh 			*p = ntohs(*p);
    333   1.1   kenh 		}
    334   1.1   kenh 	}
    335   1.1   kenh #endif
    336   1.1   kenh 
    337   1.1   kenh 	/*
    338   1.1   kenh 	 * Strip blanks off of the info strings.  Yuck, I wish this was
    339   1.1   kenh 	 * cleaner.
    340   1.1   kenh 	 */
    341   1.1   kenh 
    342   1.1   kenh 	if (inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] == ' ') {
    343   1.1   kenh 		inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] = '\0';
    344   1.1   kenh 		while (inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] == ' ')
    345   1.1   kenh 			inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] = '\0';
    346   1.1   kenh 	}
    347   1.1   kenh 
    348   1.1   kenh 	if (inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] == ' ') {
    349   1.1   kenh 		inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] = '\0';
    350   1.1   kenh 		while (inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] == ' ')
    351   1.1   kenh 			inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] = '\0';
    352   1.1   kenh 	}
    353   1.1   kenh 
    354   1.1   kenh 	if (inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] == ' ') {
    355   1.1   kenh 		inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] = '\0';
    356   1.1   kenh 		while (inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] == ' ')
    357   1.1   kenh 			inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] = '\0';
    358   1.1   kenh 	}
    359   1.1   kenh 
    360   1.1   kenh 	printf("Model: %.*s, Rev: %.*s, Serial #: %.*s\n",
    361   1.1   kenh 	       (int) sizeof(inqbuf->atap_model), inqbuf->atap_model,
    362   1.1   kenh 	       (int) sizeof(inqbuf->atap_revision), inqbuf->atap_revision,
    363   1.1   kenh 	       (int) sizeof(inqbuf->atap_serial), inqbuf->atap_serial);
    364   1.1   kenh 
    365   1.1   kenh 	printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
    366   1.1   kenh 	       "ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
    367   1.1   kenh 	       "removable");
    368   1.1   kenh 
    369   1.1   kenh 	if ((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == 0)
    370   1.1   kenh 		printf("Cylinders: %d, heads: %d, sec/track: %d, total "
    371   1.1   kenh 		       "sectors: %d\n", inqbuf->atap_cylinders,
    372   1.1   kenh 		       inqbuf->atap_heads, inqbuf->atap_sectors,
    373   1.1   kenh 		       (inqbuf->atap_capacity[1] << 16) |
    374   1.1   kenh 		       inqbuf->atap_capacity[0]);
    375   1.1   kenh 
    376   1.1   kenh 	if (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK)
    377   1.1   kenh 		printf("Device supports command queue depth of %d\n",
    378   1.1   kenh 		       inqbuf->atap_queuedepth & 0xf);
    379   1.1   kenh 
    380   1.1   kenh 	printf("Device capabilities:\n");
    381  1.10     is 	print_bitinfo("\t", "\n", inqbuf->atap_capabilities1, ata_caps);
    382   1.1   kenh 
    383   1.1   kenh 	if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
    384   1.1   kenh 		printf("Device supports following standards:\n");
    385  1.10     is 		print_bitinfo("", " ", inqbuf->atap_ata_major, ata_vers);
    386   1.1   kenh 		printf("\n");
    387   1.1   kenh 	}
    388   1.1   kenh 
    389   1.1   kenh 	if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
    390   1.1   kenh 	    inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
    391   1.1   kenh 		printf("Command set support:\n");
    392  1.10     is 		print_bitinfo("\t", "\n", inqbuf->atap_cmd_set1, ata_cmd_set1);
    393  1.10     is 		print_bitinfo("\t", "\n", inqbuf->atap_cmd_set2, ata_cmd_set2);
    394   1.1   kenh 	}
    395   1.1   kenh 
    396   1.1   kenh 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    397   1.1   kenh 		printf("Command sets/features enabled:\n");
    398  1.10     is 		print_bitinfo("\t", "\n", inqbuf->atap_cmd_set1 &
    399   1.1   kenh 			      (WDC_CMD1_SRV | WDC_CMD1_RLSE | WDC_CMD1_AHEAD |
    400   1.1   kenh 			       WDC_CMD1_CACHE | WDC_CMD1_SEC | WDC_CMD1_SMART),
    401   1.1   kenh 			       ata_cmd_set1);
    402  1.10     is 		print_bitinfo("\t", "\n", inqbuf->atap_cmd_set2 &
    403   1.1   kenh 			      (WDC_CMD2_RMSN | ATA_CMD2_APM), ata_cmd_set2);
    404   1.1   kenh 	}
    405   1.1   kenh 
    406   1.1   kenh 	return;
    407   1.1   kenh }
    408   1.1   kenh 
    409   1.1   kenh /*
    410   1.1   kenh  * device idle:
    411   1.1   kenh  *
    412   1.1   kenh  * issue the IDLE IMMEDIATE command to the drive
    413   1.1   kenh  */
    414   1.1   kenh 
    415   1.1   kenh void
    416   1.1   kenh device_idle(argc, argv)
    417   1.1   kenh 	int argc;
    418   1.1   kenh 	char *argv[];
    419   1.1   kenh {
    420   1.1   kenh 	struct atareq req;
    421   1.1   kenh 
    422   1.1   kenh 	/* No arguments. */
    423   1.1   kenh 	if (argc != 0)
    424   1.5  soren 		usage();
    425   1.1   kenh 
    426   1.1   kenh 	memset(&req, 0, sizeof(req));
    427   1.1   kenh 
    428   1.1   kenh 	if (strcmp(cmdname, "idle") == 0)
    429   1.1   kenh 		req.command = WDCC_IDLE_IMMED;
    430   1.1   kenh 	else if (strcmp(cmdname, "standby") == 0)
    431   1.1   kenh 		req.command = WDCC_STANDBY_IMMED;
    432   1.1   kenh 	else
    433   1.1   kenh 		req.command = WDCC_SLEEP;
    434   1.1   kenh 
    435   1.1   kenh 	req.timeout = 1000;
    436   1.1   kenh 
    437   1.1   kenh 	ata_command(&req);
    438   1.1   kenh 
    439   1.1   kenh 	return;
    440   1.1   kenh }
    441   1.1   kenh 
    442   1.1   kenh /*
    443   1.1   kenh  * Set the idle timer on the disk.  Set it for either idle mode or
    444   1.1   kenh  * standby mode, depending on how we were invoked.
    445   1.1   kenh  */
    446   1.1   kenh 
    447   1.1   kenh void
    448   1.1   kenh device_setidle(argc, argv)
    449   1.1   kenh 	int argc;
    450   1.1   kenh 	char *argv[];
    451   1.1   kenh {
    452   1.1   kenh 	unsigned long idle;
    453   1.1   kenh 	struct atareq req;
    454   1.1   kenh 	char *end;
    455   1.1   kenh 
    456   1.1   kenh 	/* Only one argument */
    457   1.1   kenh 	if (argc != 1)
    458   1.5  soren 		usage();
    459   1.1   kenh 
    460   1.1   kenh 	idle = strtoul(argv[0], &end, 0);
    461   1.1   kenh 
    462   1.1   kenh 	if (*end != '\0') {
    463   1.1   kenh 		fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
    464   1.1   kenh 		exit(1);
    465   1.1   kenh 	}
    466   1.1   kenh 
    467   1.1   kenh 	if (idle > 19800) {
    468   1.1   kenh 		fprintf(stderr, "Idle time has a maximum value of 5.5 "
    469   1.1   kenh 			"hours\n");
    470   1.1   kenh 		exit(1);
    471   1.1   kenh 	}
    472   1.1   kenh 
    473   1.1   kenh 	if (idle != 0 && idle < 5) {
    474   1.1   kenh 		fprintf(stderr, "Idle timer must be at least 5 seconds\n");
    475   1.1   kenh 		exit(1);
    476   1.1   kenh 	}
    477   1.1   kenh 
    478   1.1   kenh 	memset(&req, 0, sizeof(req));
    479   1.1   kenh 
    480   1.1   kenh 	if (idle <= 240*5)
    481   1.1   kenh 		req.sec_count = idle / 5;
    482   1.1   kenh 	else
    483   1.1   kenh 		req.sec_count = idle / (30*60) + 240;
    484   1.1   kenh 
    485   1.1   kenh 	req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
    486   1.1   kenh 	req.timeout = 1000;
    487   1.1   kenh 
    488   1.1   kenh 	ata_command(&req);
    489   1.1   kenh 
    490   1.1   kenh 	return;
    491   1.3   kenh }
    492   1.3   kenh 
    493   1.3   kenh /*
    494   1.3   kenh  * Query the device for the current power mode
    495   1.3   kenh  */
    496   1.3   kenh 
    497   1.3   kenh void
    498   1.3   kenh device_checkpower(argc, argv)
    499   1.3   kenh 	int argc;
    500   1.3   kenh 	char *argv[];
    501   1.3   kenh {
    502   1.3   kenh 	struct atareq req;
    503   1.3   kenh 
    504   1.3   kenh 	/* No arguments. */
    505   1.3   kenh 	if (argc != 0)
    506   1.5  soren 		usage();
    507   1.3   kenh 
    508   1.3   kenh 	memset(&req, 0, sizeof(req));
    509   1.3   kenh 
    510   1.3   kenh 	req.command = WDCC_CHECK_PWR;
    511   1.3   kenh 	req.timeout = 1000;
    512   1.3   kenh 	req.flags = ATACMD_READREG;
    513   1.3   kenh 
    514   1.3   kenh 	ata_command(&req);
    515   1.3   kenh 
    516   1.3   kenh 	printf("Current power status: ");
    517   1.3   kenh 
    518   1.3   kenh 	switch (req.sec_count) {
    519   1.3   kenh 	case 0x00:
    520   1.3   kenh 		printf("Standby mode\n");
    521   1.3   kenh 		break;
    522   1.3   kenh 	case 0x80:
    523   1.3   kenh 		printf("Idle mode\n");
    524   1.3   kenh 		break;
    525   1.3   kenh 	case 0xff:
    526   1.3   kenh 		printf("Active mode\n");
    527   1.3   kenh 		break;
    528   1.3   kenh 	default:
    529   1.3   kenh 		printf("Unknown power code (%02x)\n", req.sec_count);
    530   1.3   kenh 	}
    531   1.3   kenh 
    532   1.3   kenh 	return;
    533   1.1   kenh }
    534