mt.c revision 69248
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1980, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3527752Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1980, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4127752Scharnier#if 0
4223693Speterstatic char sccsid[] = "@(#)mt.c	8.2 (Berkeley) 5/4/95";
4327752Scharnier#endif
4427752Scharnierstatic const char rcsid[] =
4550477Speter  "$FreeBSD: head/usr.bin/mt/mt.c 69248 2000-11-27 06:40:35Z kris $";
461590Srgrimes#endif /* not lint */
471590Srgrimes
481590Srgrimes/*
491590Srgrimes * mt --
501590Srgrimes *   magnetic tape manipulation program
511590Srgrimes */
521590Srgrimes#include <sys/types.h>
531590Srgrimes#include <sys/ioctl.h>
541590Srgrimes#include <sys/mtio.h>
5523693Speter
5623693Speter#include <ctype.h>
5727752Scharnier#include <err.h>
581590Srgrimes#include <fcntl.h>
5923693Speter#include <stdio.h>
601590Srgrimes#include <stdlib.h>
611590Srgrimes#include <string.h>
6223693Speter#include <unistd.h>
631590Srgrimes
647913Sjoerg/* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
657913Sjoerg#if defined(__FreeBSD__)
667913Sjoerg/* c_flags */
677913Sjoerg#define NEED_2ARGS	0x01
687929Sjoerg#define ZERO_ALLOWED	0x02
697929Sjoerg#define IS_DENSITY	0x04
709541Sjoerg#define DISABLE_THIS	0x08
7139260Sgibbs#define IS_COMP		0x10
727913Sjoerg#endif /* defined(__FreeBSD__) */
737913Sjoerg
7439260Sgibbs#ifndef TRUE
7539260Sgibbs#define TRUE 1
7639260Sgibbs#endif
7739260Sgibbs#ifndef FALSE
7839260Sgibbs#define FALSE 0
7939260Sgibbs#endif
8039260Sgibbs
811590Srgrimesstruct commands {
821590Srgrimes	char *c_name;
831590Srgrimes	int c_code;
841590Srgrimes	int c_ronly;
857913Sjoerg#if defined(__FreeBSD__)
867913Sjoerg	int c_flags;
877913Sjoerg#endif /* defined(__FreeBSD__) */
881590Srgrimes} com[] = {
891590Srgrimes	{ "bsf",	MTBSF,	1 },
901590Srgrimes	{ "bsr",	MTBSR,	1 },
919541Sjoerg#if defined(__FreeBSD__)
929541Sjoerg	/* XXX FreeBSD considered "eof" dangerous, since it's being
939541Sjoerg	   confused with "eom" (and is an alias for "weof" anyway) */
949541Sjoerg	{ "eof",	MTWEOF, 0, DISABLE_THIS },
959541Sjoerg#else
969541Sjoerg	{ "eof",	MTWEOF, 0 },
979541Sjoerg#endif
981590Srgrimes	{ "fsf",	MTFSF,	1 },
991590Srgrimes	{ "fsr",	MTFSR,	1 },
1001590Srgrimes	{ "offline",	MTOFFL,	1 },
1011590Srgrimes	{ "rewind",	MTREW,	1 },
1021590Srgrimes	{ "rewoffl",	MTOFFL,	1 },
1031590Srgrimes	{ "status",	MTNOP,	1 },
10441913Smjacob#if defined(__FreeBSD__)
10541913Smjacob	{ "weof",	MTWEOF,	0, ZERO_ALLOWED },
10641913Smjacob#else
1071590Srgrimes	{ "weof",	MTWEOF,	0 },
10841913Smjacob#endif
1097913Sjoerg#if defined(__FreeBSD__)
11039260Sgibbs	{ "erase",	MTERASE, 0, ZERO_ALLOWED},
1117913Sjoerg	{ "blocksize",	MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
1127929Sjoerg	{ "density",	MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
1137913Sjoerg	{ "eom",	MTEOD, 1 },
11428492Sjoerg	{ "eod",	MTEOD, 1 },
11542010Smjacob	{ "smk",	MTWSS, 0 },
11642010Smjacob	{ "wss",	MTWSS, 0 },
11742010Smjacob	{ "fss",	MTFSS, 1 },
11842010Smjacob	{ "bss",	MTBSS, 1 },
11939260Sgibbs	{ "comp",	MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
12013401Sjoerg	{ "retension",	MTRETENS, 1 },
12141913Smjacob	{ "rdhpos",     MTIOCRDHPOS,  0 },
12241913Smjacob	{ "rdspos",     MTIOCRDSPOS,  0 },
12341913Smjacob	{ "sethpos",    MTIOCHLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
12441913Smjacob	{ "setspos",    MTIOCSLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
12541945Smjacob	{ "errstat",	MTIOCERRSTAT, 0 },
12646928Smjacob	{ "setmodel",	MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
12746928Smjacob	{ "seteotmodel",	MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
12846928Smjacob	{ "getmodel",	MTIOCGETEOTMODEL },
12946928Smjacob	{ "geteotmodel",	MTIOCGETEOTMODEL },
1307913Sjoerg#endif /* defined(__FreeBSD__) */
1311590Srgrimes	{ NULL }
1321590Srgrimes};
1331590Srgrimes
1341590Srgrimesvoid printreg __P((char *, u_int, char *));
1351590Srgrimesvoid status __P((struct mtget *));
1361590Srgrimesvoid usage __P((void));
1377913Sjoerg#if defined (__FreeBSD__)
1387913Sjoergvoid st_status (struct mtget *);
1397929Sjoergint stringtodens (const char *s);
1407929Sjoergconst char *denstostring (int d);
14139260Sgibbsint denstobp(int d, int bpi);
14239260Sgibbsu_int32_t stringtocomp(const char *s);
14339260Sgibbsconst char * comptostring(u_int32_t comp);
1449541Sjoergvoid warn_eof __P((void));
1457913Sjoerg#endif /* defined (__FreeBSD__) */
1461590Srgrimes
1471590Srgrimesint
1481590Srgrimesmain(argc, argv)
1491590Srgrimes	int argc;
1501590Srgrimes	char *argv[];
1511590Srgrimes{
1521590Srgrimes	register struct commands *comp;
1531590Srgrimes	struct mtget mt_status;
1541590Srgrimes	struct mtop mt_com;
1551590Srgrimes	int ch, len, mtfd;
1561590Srgrimes	char *p, *tape;
1571590Srgrimes
1581590Srgrimes	if ((tape = getenv("TAPE")) == NULL)
1591590Srgrimes		tape = DEFTAPE;
1601590Srgrimes
16124360Simp	while ((ch = getopt(argc, argv, "f:t:")) != -1)
1621590Srgrimes		switch(ch) {
1631590Srgrimes		case 'f':
1641590Srgrimes		case 't':
1651590Srgrimes			tape = optarg;
1661590Srgrimes			break;
1671590Srgrimes		case '?':
1681590Srgrimes		default:
1691590Srgrimes			usage();
1701590Srgrimes		}
1711590Srgrimes	argc -= optind;
1721590Srgrimes	argv += optind;
1731590Srgrimes
1741590Srgrimes	if (argc < 1 || argc > 2)
1751590Srgrimes		usage();
1761590Srgrimes
1771590Srgrimes	len = strlen(p = *argv++);
1781590Srgrimes	for (comp = com;; comp++) {
1791590Srgrimes		if (comp->c_name == NULL)
18027752Scharnier			errx(1, "%s: unknown command", p);
1811590Srgrimes		if (strncmp(p, comp->c_name, len) == 0)
1821590Srgrimes			break;
1831590Srgrimes	}
1847913Sjoerg#if defined(__FreeBSD__)
1857913Sjoerg	if((comp->c_flags & NEED_2ARGS) && argc != 2)
1867913Sjoerg		usage();
1879541Sjoerg	if(comp->c_flags & DISABLE_THIS) {
1889541Sjoerg		warn_eof();
1899541Sjoerg	}
1907913Sjoerg#endif /* defined(__FreeBSD__) */
1911590Srgrimes	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
19227752Scharnier		err(1, "%s", tape);
1931590Srgrimes	if (comp->c_code != MTNOP) {
1941590Srgrimes		mt_com.mt_op = comp->c_code;
1951590Srgrimes		if (*argv) {
1967913Sjoerg#if defined (__FreeBSD__)
1977929Sjoerg			if (!isdigit(**argv) &&
19841925Smjacob			    (comp->c_flags & IS_DENSITY)) {
1997929Sjoerg				const char *dcanon;
2007929Sjoerg				mt_com.mt_count = stringtodens(*argv);
2017929Sjoerg				if (mt_com.mt_count == 0)
20227752Scharnier					errx(1, "%s: unknown density", *argv);
2037929Sjoerg				dcanon = denstostring(mt_com.mt_count);
2047929Sjoerg				if (strcmp(dcanon, *argv) != 0)
2057929Sjoerg					printf(
2067929Sjoerg					"Using \"%s\" as an alias for %s\n",
2077929Sjoerg					       *argv, dcanon);
2087929Sjoerg				p = "";
20939260Sgibbs			} else if (!isdigit(**argv) &&
21041925Smjacob				   (comp->c_flags & IS_COMP)) {
21139260Sgibbs
21239260Sgibbs				mt_com.mt_count = stringtocomp(*argv);
21339260Sgibbs				if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
21439260Sgibbs					errx(1, "%s: unknown compression",
21539260Sgibbs					     *argv);
21639260Sgibbs				p = "";
2177929Sjoerg			} else
2187929Sjoerg				/* allow for hex numbers; useful for density */
2197929Sjoerg				mt_com.mt_count = strtol(*argv, &p, 0);
2207913Sjoerg#else
2211590Srgrimes			mt_com.mt_count = strtol(*argv, &p, 10);
2227913Sjoerg#endif /* defined(__FreeBSD__) */
22341925Smjacob			if ((mt_com.mt_count <=
2247913Sjoerg#if defined (__FreeBSD__)
2257913Sjoerg			    ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
22639260Sgibbs			    && ((comp->c_flags & IS_COMP) == 0)
2277913Sjoerg#else
2287913Sjoerg			    0
2297913Sjoerg#endif /* defined (__FreeBSD__) */
23041925Smjacob			    ) || *p)
23127752Scharnier				errx(1, "%s: illegal count", *argv);
2321590Srgrimes		}
2331590Srgrimes		else
2341590Srgrimes			mt_com.mt_count = 1;
23541913Smjacob#if	defined(__FreeBSD__)
23641913Smjacob		switch (comp->c_code) {
23741945Smjacob		case MTIOCERRSTAT:
23841945Smjacob		{
23941945Smjacob			int i;
24041945Smjacob			union mterrstat umn;
24141945Smjacob			struct scsi_tape_errors *s = &umn.scsi_errstat;
24241945Smjacob
24341945Smjacob			if (ioctl(mtfd, comp->c_code, (caddr_t)&umn) < 0)
24441945Smjacob				err(2, "%s", tape);
24541945Smjacob			(void)printf("Last I/O Residual: %u\n", s->io_resid);
24642010Smjacob			(void)printf(" Last I/O Command:");
24742010Smjacob			for (i = 0; i < sizeof (s->io_cdb); i++)
24842010Smjacob				(void)printf(" %02X", s->io_cdb[i]);
24942010Smjacob			(void)printf("\n");
25041945Smjacob			(void)printf("   Last I/O Sense:\n\n\t");
25141945Smjacob			for (i = 0; i < sizeof (s->io_sense); i++) {
25241945Smjacob				(void)printf(" %02X", s->io_sense[i]);
25341945Smjacob				if (((i + 1) & 0xf) == 0) {
25441945Smjacob					(void)printf("\n\t");
25541945Smjacob				}
25641945Smjacob			}
25741945Smjacob			(void)printf("\n");
25841945Smjacob			(void)printf("Last Control Residual: %u\n",
25941945Smjacob			    s->ctl_resid);
26042010Smjacob			(void)printf(" Last Control Command:");
26142010Smjacob			for (i = 0; i < sizeof (s->ctl_cdb); i++)
26242010Smjacob				(void)printf(" %02X", s->ctl_cdb[i]);
26342010Smjacob			(void)printf("\n");
26441945Smjacob			(void)printf("   Last Control Sense:\n\n\t");
26541945Smjacob			for (i = 0; i < sizeof (s->ctl_sense); i++) {
26641945Smjacob				(void)printf(" %02X", s->ctl_sense[i]);
26741945Smjacob				if (((i + 1) & 0xf) == 0) {
26841945Smjacob					(void)printf("\n\t");
26941945Smjacob				}
27041945Smjacob			}
27141945Smjacob			(void)printf("\n\n");
27241945Smjacob			exit(0);
27341945Smjacob			/* NOTREACHED */
27441945Smjacob		}
27541913Smjacob		case MTIOCRDHPOS:
27641913Smjacob		case MTIOCRDSPOS:
27741925Smjacob		{
27841925Smjacob			u_int32_t block;
27941925Smjacob			if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
28041913Smjacob				err(2, "%s", tape);
28141945Smjacob			(void)printf("%s: %s block location %u\n", tape,
28241913Smjacob			    (comp->c_code == MTIOCRDHPOS)? "hardware" :
28341925Smjacob			    "logical", block);
28441925Smjacob			exit(0);
28541913Smjacob			/* NOTREACHED */
28641925Smjacob		}
28741913Smjacob		case MTIOCSLOCATE:
28841913Smjacob		case MTIOCHLOCATE:
28941925Smjacob		{
29041925Smjacob			u_int32_t block = (u_int32_t)mt_com.mt_count;
29141925Smjacob			if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
29241913Smjacob				err(2, "%s", tape);
29341925Smjacob			exit(0);
29441913Smjacob			/* NOTREACHED */
29541925Smjacob		}
29646928Smjacob		case MTIOCGETEOTMODEL:
29746928Smjacob		{
29846928Smjacob			u_int32_t om;
29946928Smjacob			if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
30046928Smjacob				err(2, "%s", tape);
30146928Smjacob			(void)printf("%s: the model is %u filemar%s at EOT\n",
30246928Smjacob			    tape, om, (om > 1)? "ks" : "k");
30346928Smjacob			exit(0);
30446928Smjacob			/* NOTREACHED */
30546928Smjacob		}
30646928Smjacob		case MTIOCSETEOTMODEL:
30746928Smjacob		{
30846928Smjacob			u_int32_t om, nm = (u_int32_t)mt_com.mt_count;
30946928Smjacob			if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
31046928Smjacob				err(2, "%s", tape);
31146928Smjacob			if (ioctl(mtfd, comp->c_code, (caddr_t)&nm) < 0)
31246928Smjacob				err(2, "%s", tape);
31346928Smjacob			(void)printf("%s: old model was %u filemar%s at EOT\n",
31446928Smjacob			    tape, om, (om > 1)? "ks" : "k");
31546928Smjacob			(void)printf("%s: new model  is %u filemar%s at EOT\n",
31646928Smjacob			    tape, nm, (nm > 1)? "ks" : "k");
31746928Smjacob			exit(0);
31846928Smjacob			/* NOTREACHED */
31946928Smjacob		}
32041913Smjacob		default:
32141913Smjacob			break;
32241913Smjacob		}
32341913Smjacob#endif
3241590Srgrimes		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
32527752Scharnier			err(1, "%s: %s", tape, comp->c_name);
3261590Srgrimes	} else {
3271590Srgrimes		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
32827752Scharnier			err(1, NULL);
3291590Srgrimes		status(&mt_status);
3301590Srgrimes	}
33141925Smjacob	exit(0);
3321590Srgrimes	/* NOTREACHED */
3331590Srgrimes}
3341590Srgrimes
3351590Srgrimes#ifdef vax
3361590Srgrimes#include <vax/mba/mtreg.h>
3371590Srgrimes#include <vax/mba/htreg.h>
3381590Srgrimes
3391590Srgrimes#include <vax/uba/utreg.h>
3401590Srgrimes#include <vax/uba/tmreg.h>
3411590Srgrimes#undef b_repcnt		/* argh */
3421590Srgrimes#include <vax/uba/tsreg.h>
3431590Srgrimes#endif
3441590Srgrimes
3451590Srgrimes#ifdef sun
3461590Srgrimes#include <sundev/tmreg.h>
3471590Srgrimes#include <sundev/arreg.h>
3481590Srgrimes#endif
3491590Srgrimes
3501590Srgrimes#ifdef tahoe
3511590Srgrimes#include <tahoe/vba/cyreg.h>
3521590Srgrimes#endif
3531590Srgrimes
35439913Sdfr#if defined(__FreeBSD__) && defined(__i386__)
35514176Sjoerg#include <machine/wtio.h>
35614176Sjoerg#endif
35714176Sjoerg
3581590Srgrimesstruct tape_desc {
3591590Srgrimes	short	t_type;		/* type of magtape device */
3601590Srgrimes	char	*t_name;	/* printing name */
3611590Srgrimes	char	*t_dsbits;	/* "drive status" register */
3621590Srgrimes	char	*t_erbits;	/* "error" register */
3631590Srgrimes} tapes[] = {
3641590Srgrimes#ifdef vax
3651590Srgrimes	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
3661590Srgrimes	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
3671590Srgrimes	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
3681590Srgrimes	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
3691590Srgrimes	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
3701590Srgrimes#endif
3711590Srgrimes#ifdef sun
3721590Srgrimes	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
3731590Srgrimes	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
3741590Srgrimes#endif
3751590Srgrimes#ifdef tahoe
3761590Srgrimes	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
3771590Srgrimes#endif
3787913Sjoerg#if defined (__FreeBSD__)
3797913Sjoerg	/*
38014176Sjoerg	 * XXX This is weird.  The st driver reports the tape drive
3817913Sjoerg	 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
3827913Sjoerg	 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
3837913Sjoerg	 * (MT_ISMFOUR) for other tapes.
3847913Sjoerg	 * XXX for the wt driver, rely on it behaving like a "standard"
3857913Sjoerg	 * magtape driver.
3867913Sjoerg	 */
3877913Sjoerg	{ MT_ISAR,	"SCSI tape drive", 0,		0 },
38839913Sdfr#if defined (__i386__)
38914176Sjoerg	{ MT_ISVIPER1,	"Archive Viper", WTDS_BITS, WTER_BITS },
39014176Sjoerg	{ MT_ISMFOUR,	"Wangtek",	 WTDS_BITS, WTER_BITS },
39139913Sdfr#endif
3927913Sjoerg#endif /* defined (__FreeBSD__) */
3931590Srgrimes	{ 0 }
3941590Srgrimes};
3951590Srgrimes
3961590Srgrimes/*
3971590Srgrimes * Interpret the status buffer returned
3981590Srgrimes */
3991590Srgrimesvoid
4001590Srgrimesstatus(bp)
4011590Srgrimes	register struct mtget *bp;
4021590Srgrimes{
4031590Srgrimes	register struct tape_desc *mt;
4041590Srgrimes
4051590Srgrimes	for (mt = tapes;; mt++) {
4061590Srgrimes		if (mt->t_type == 0) {
4071590Srgrimes			(void)printf("%d: unknown tape drive type\n",
4081590Srgrimes			    bp->mt_type);
4091590Srgrimes			return;
4101590Srgrimes		}
4111590Srgrimes		if (mt->t_type == bp->mt_type)
4121590Srgrimes			break;
4131590Srgrimes	}
4147913Sjoerg#if defined (__FreeBSD__)
4157913Sjoerg	if(mt->t_type == MT_ISAR)
4167913Sjoerg		st_status(bp);
4177913Sjoerg	else {
4187913Sjoerg#endif /* defined (__FreeBSD__) */
4191590Srgrimes	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
42014176Sjoerg	printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
42114176Sjoerg	printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
4221590Srgrimes	(void)putchar('\n');
4237913Sjoerg#if defined (__FreeBSD__)
4247913Sjoerg	}
4257913Sjoerg#endif /* defined (__FreeBSD__) */
4261590Srgrimes}
4271590Srgrimes
4281590Srgrimes/*
4291590Srgrimes * Print a register a la the %b format of the kernel's printf.
4301590Srgrimes */
4311590Srgrimesvoid
4321590Srgrimesprintreg(s, v, bits)
4331590Srgrimes	char *s;
4341590Srgrimes	register u_int v;
4351590Srgrimes	register char *bits;
4361590Srgrimes{
4371590Srgrimes	register int i, any = 0;
4381590Srgrimes	register char c;
4391590Srgrimes
4401590Srgrimes	if (bits && *bits == 8)
4411590Srgrimes		printf("%s=%o", s, v);
4421590Srgrimes	else
4431590Srgrimes		printf("%s=%x", s, v);
44411608Sbde	if (!bits)
44511608Sbde		return;
4461590Srgrimes	bits++;
4471590Srgrimes	if (v && bits) {
4481590Srgrimes		putchar('<');
44927752Scharnier		while ((i = *bits++)) {
4501590Srgrimes			if (v & (1 << (i-1))) {
4511590Srgrimes				if (any)
4521590Srgrimes					putchar(',');
4531590Srgrimes				any = 1;
4541590Srgrimes				for (; (c = *bits) > 32; bits++)
4551590Srgrimes					putchar(c);
4561590Srgrimes			} else
4571590Srgrimes				for (; *bits > 32; bits++)
4581590Srgrimes					;
4591590Srgrimes		}
4601590Srgrimes		putchar('>');
4611590Srgrimes	}
4621590Srgrimes}
4631590Srgrimes
4641590Srgrimesvoid
4651590Srgrimesusage()
4661590Srgrimes{
4671590Srgrimes	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
4681590Srgrimes	exit(1);
4691590Srgrimes}
4701590Srgrimes
4717913Sjoerg#if defined (__FreeBSD__)
4727913Sjoerg
4737913Sjoergstruct densities {
4747913Sjoerg	int dens;
47539260Sgibbs	int bpmm;
47639260Sgibbs	int bpi;
4777913Sjoerg	const char *name;
47839260Sgibbs} dens[] = {
47939260Sgibbs	/*
48039260Sgibbs	 * Taken from T10 Project 997D
48139260Sgibbs	 * SCSI-3 Stream Device Commands (SSC)
48239260Sgibbs	 * Revision 11, 4-Nov-97
48339260Sgibbs	 */
48439260Sgibbs	/*Num.  bpmm    bpi     Reference     */
48539260Sgibbs	{ 0x1,	32,	800,	"X3.22-1983" },
48639260Sgibbs	{ 0x2,	63,	1600,	"X3.39-1986" },
48739260Sgibbs	{ 0x3,	246,	6250,	"X3.54-1986" },
48839260Sgibbs	{ 0x5,	315,	8000,	"X3.136-1986" },
48939260Sgibbs	{ 0x6,	126,	3200,	"X3.157-1987" },
49039260Sgibbs	{ 0x7,	252,	6400,	"X3.116-1986" },
49139260Sgibbs	{ 0x8,	315,	8000,	"X3.158-1987" },
49239260Sgibbs	{ 0x9,	491,	37871,	"X3.180" },
49339260Sgibbs	{ 0xA,	262,	6667,	"X3B5/86-199" },
49439260Sgibbs	{ 0xB,	63,	1600,	"X3.56-1986" },
49539260Sgibbs	{ 0xC,	500,	12690,	"HI-TC1" },
49639260Sgibbs	{ 0xD,	999,	25380,	"HI-TC2" },
49739260Sgibbs	{ 0xF,	394,	10000,	"QIC-120" },
49839260Sgibbs	{ 0x10,	394,	10000,	"QIC-150" },
49939260Sgibbs	{ 0x11,	630,	16000,	"QIC-320" },
50039260Sgibbs	{ 0x12,	2034,	51667,	"QIC-1350" },
50139260Sgibbs	{ 0x13,	2400,	61000,	"X3B5/88-185A" },
50239260Sgibbs	{ 0x14,	1703,	43245,	"X3.202-1991" },
50339260Sgibbs	{ 0x15,	1789,	45434,	"ECMA TC17" },
50439260Sgibbs	{ 0x16,	394,	10000,	"X3.193-1990" },
50539260Sgibbs	{ 0x17,	1673,	42500,	"X3B5/91-174" },
50639260Sgibbs	{ 0x18,	1673,	42500,	"X3B5/92-50" },
50739260Sgibbs	{ 0x1C, 1654,	42000,	"QIC-385M" },
50839260Sgibbs	{ 0x1D,	1512,	38400,	"QIC-410M" },
50939260Sgibbs	{ 0x1E, 1385,	36000,	"QIC-1000C" },
51039260Sgibbs	{ 0x1F,	2666,	67733,	"QIC-2100C" },
51139260Sgibbs	{ 0x20, 2666,	67733,	"QIC-6GB(M)" },
51239260Sgibbs	{ 0x21,	2666,	67733,	"QIC-20GB(C)" },
51339260Sgibbs	{ 0x22,	1600,	40640,	"QIC-2GB(C)" },
51439260Sgibbs	{ 0x23, 2666,	67733,	"QIC-875M" },
51539260Sgibbs	{ 0x24,	2400,	61000,	"DDS-2" },
51639260Sgibbs	{ 0x25,	3816,	97000,	"DDS-3" },
51739260Sgibbs	{ 0x26,	3816,	97000,	"DDS-4" },
51839260Sgibbs	{ 0x27,	3056,	77611,	"Mammoth" },
51939260Sgibbs	{ 0x28,	1491,	37871,	"X3.224" },
52039260Sgibbs	{ 0, 0, 0, NULL }
5217913Sjoerg};
5227913Sjoerg
52339260Sgibbsstruct compression_types {
52439260Sgibbs	u_int32_t	comp_number;
52539260Sgibbs	const char 	*name;
52639260Sgibbs} comp_types[] = {
52739260Sgibbs	{ 0x00, "none" },
52839260Sgibbs	{ 0x00, "off" },
52939260Sgibbs	{ 0x10, "IDRC" },
53039260Sgibbs	{ 0x20, "DCLZ" },
53139260Sgibbs	{ 0xffffffff, "enable" },
53239260Sgibbs	{ 0xffffffff, "on" },
53339260Sgibbs	{ 0xf0f0f0f0, NULL}
53439260Sgibbs};
53539260Sgibbs
5367913Sjoergconst char *
5377929Sjoergdenstostring(int d)
5387913Sjoerg{
5397913Sjoerg	static char buf[20];
5407913Sjoerg	struct densities *sd;
5417913Sjoerg
54244397Smjacob	/* densities 0 and 0x7f are handled as special cases */
54344397Smjacob	if (d == 0)
54444397Smjacob		return "default";
54544397Smjacob	if (d == 0x7f)
54644397Smjacob		return "same";
5477913Sjoerg	for (sd = dens; sd->dens; sd++)
5487913Sjoerg		if (sd->dens == d)
5497913Sjoerg			break;
55044397Smjacob	if (sd->dens == 0)
5517929Sjoerg		sprintf(buf, "0x%02x", d);
55244397Smjacob	else
55344397Smjacob		sprintf(buf, "0x%02x:%s", d, sd->name);
55444397Smjacob	return buf;
5557913Sjoerg}
5567913Sjoerg
55739260Sgibbs/*
55839260Sgibbs * Given a specific density number, return either the bits per inch or bits
55939260Sgibbs * per millimeter for the given density.
56039260Sgibbs */
5617929Sjoergint
56239260Sgibbsdenstobp(int d, int bpi)
56339260Sgibbs{
56439260Sgibbs	struct densities *sd;
56539260Sgibbs
56639260Sgibbs	for (sd = dens; sd->dens; sd++)
56739260Sgibbs		if (sd->dens == d)
56839260Sgibbs			break;
56939260Sgibbs	if (sd->dens == 0)
57039260Sgibbs		return(0);
57139260Sgibbs	else {
57239260Sgibbs		if (bpi)
57339260Sgibbs			return(sd->bpi);
57439260Sgibbs		else
57539260Sgibbs			return(sd->bpmm);
57639260Sgibbs	}
57739260Sgibbs}
57839260Sgibbs
57939260Sgibbsint
5807929Sjoergstringtodens(const char *s)
5817929Sjoerg{
5827929Sjoerg	struct densities *sd;
5837929Sjoerg	size_t l = strlen(s);
5847929Sjoerg
5857929Sjoerg	for (sd = dens; sd->dens; sd++)
5867929Sjoerg		if (strncasecmp(sd->name, s, l) == 0)
5877929Sjoerg			break;
5887929Sjoerg	return sd->dens;
5897929Sjoerg}
5907929Sjoerg
5917929Sjoerg
5927913Sjoergconst char *
5937913Sjoerggetblksiz(int bs)
5947913Sjoerg{
5957913Sjoerg	static char buf[25];
5967913Sjoerg	if (bs == 0)
5977913Sjoerg		return "variable";
5987913Sjoerg	else {
59939260Sgibbs		sprintf(buf, "%d bytes", bs);
6007913Sjoerg		return buf;
6017913Sjoerg	}
6027913Sjoerg}
6037913Sjoerg
60439260Sgibbsconst char *
60539260Sgibbscomptostring(u_int32_t comp)
60639260Sgibbs{
60739260Sgibbs	static char buf[20];
60839260Sgibbs	struct compression_types *ct;
6097913Sjoerg
61039260Sgibbs	if (comp == MT_COMP_DISABLED)
61139260Sgibbs		return "disabled";
61239260Sgibbs	else if (comp == MT_COMP_UNSUPP)
61339260Sgibbs		return "unsupported";
61439260Sgibbs
61539260Sgibbs	for (ct = comp_types; ct->name; ct++)
61639260Sgibbs		if (ct->comp_number == comp)
61739260Sgibbs			break;
61839260Sgibbs
61939260Sgibbs	if (ct->comp_number == 0xf0f0f0f0) {
62044618Smjacob		sprintf(buf, "0x%x", comp);
62139260Sgibbs		return(buf);
62239260Sgibbs	} else
62339260Sgibbs		return(ct->name);
62439260Sgibbs}
62539260Sgibbs
62639260Sgibbsu_int32_t
62739260Sgibbsstringtocomp(const char *s)
62839260Sgibbs{
62939260Sgibbs	struct compression_types *ct;
63039260Sgibbs	size_t l = strlen(s);
63139260Sgibbs
63239260Sgibbs	for (ct = comp_types; ct->name; ct++)
63339260Sgibbs		if (strncasecmp(ct->name, s, l) == 0)
63439260Sgibbs			break;
63539260Sgibbs
63639260Sgibbs	return(ct->comp_number);
63739260Sgibbs}
63839260Sgibbs
6397913Sjoergvoid
6407913Sjoergst_status(struct mtget *bp)
6417913Sjoerg{
64244644Smjacob	printf("Mode      Density              Blocksize      bpi      "
64339260Sgibbs	       "Compression\n"
64444644Smjacob	       "Current:  %-17s    %-12s   %-7d  %s\n"
64539260Sgibbs	       "---------available modes---------\n"
64644644Smjacob	       "0:        %-17s    %-12s   %-7d  %s\n"
64744644Smjacob	       "1:        %-17s    %-12s   %-7d  %s\n"
64844644Smjacob	       "2:        %-17s    %-12s   %-7d  %s\n"
64944644Smjacob	       "3:        %-17s    %-12s   %-7d  %s\n",
65039260Sgibbs	       denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
65139260Sgibbs	       denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
65239260Sgibbs	       denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
65339260Sgibbs	       denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
65439260Sgibbs	       denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
65539260Sgibbs	       denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
65639260Sgibbs	       denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
65739260Sgibbs	       denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
65839260Sgibbs	       denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
65939260Sgibbs	       denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
66043629Smjacob
66143629Smjacob	if (bp->mt_dsreg != MTIO_DSREG_NIL) {
66243629Smjacob		auto char foo[32];
66369248Skris		const char sfmt[] = "Current Driver State: %s.\n";
66443629Smjacob		printf("---------------------------------\n");
66543629Smjacob		switch (bp->mt_dsreg) {
66643629Smjacob		case MTIO_DSREG_REST:
66743629Smjacob			printf(sfmt, "at rest");
66843629Smjacob			break;
66943629Smjacob		case MTIO_DSREG_RBSY:
67043629Smjacob			printf(sfmt, "Communicating with drive");
67143629Smjacob			break;
67243629Smjacob		case MTIO_DSREG_WR:
67343629Smjacob			printf(sfmt, "Writing");
67443629Smjacob			break;
67543629Smjacob		case MTIO_DSREG_FMK:
67643629Smjacob			printf(sfmt, "Writing Filemarks");
67743629Smjacob			break;
67843629Smjacob		case MTIO_DSREG_ZER:
67943629Smjacob			printf(sfmt, "Erasing");
68043629Smjacob			break;
68143629Smjacob		case MTIO_DSREG_RD:
68243629Smjacob			printf(sfmt, "Reading");
68343629Smjacob			break;
68443629Smjacob		case MTIO_DSREG_FWD:
68543629Smjacob			printf(sfmt, "Spacing Forward");
68643629Smjacob			break;
68743629Smjacob		case MTIO_DSREG_REV:
68843629Smjacob			printf(sfmt, "Spacing Reverse");
68943629Smjacob			break;
69043629Smjacob		case MTIO_DSREG_POS:
69143629Smjacob			printf(sfmt,
69243629Smjacob			    "Hardware Positioning (direction unknown)");
69343629Smjacob			break;
69443629Smjacob		case MTIO_DSREG_REW:
69543629Smjacob			printf(sfmt, "Rewinding");
69643629Smjacob			break;
69743629Smjacob		case MTIO_DSREG_TEN:
69843629Smjacob			printf(sfmt, "Retensioning");
69943629Smjacob			break;
70043629Smjacob		case MTIO_DSREG_UNL:
70143629Smjacob			printf(sfmt, "Unloading");
70243629Smjacob			break;
70343629Smjacob		case MTIO_DSREG_LD:
70443629Smjacob			printf(sfmt, "Loading");
70543629Smjacob			break;
70643629Smjacob		default:
70743629Smjacob			(void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg);
70843629Smjacob			printf(sfmt, foo);
70943629Smjacob			break;
71043629Smjacob		}
71143629Smjacob	}
71243629Smjacob	if (bp->mt_fileno == (daddr_t) -1 || bp->mt_blkno == (daddr_t) -1)
71343629Smjacob		return;
71443629Smjacob	printf("---------------------------------\n");
71543629Smjacob	printf("File Number: %ld\tRecord Number: %ld\n", bp->mt_fileno,
71643629Smjacob	    bp->mt_blkno);
7177913Sjoerg}
7187913Sjoerg
7199541Sjoergvoid
7209541Sjoergwarn_eof(void)
7219541Sjoerg{
7229541Sjoerg	fprintf(stderr,
7239541Sjoerg		"The \"eof\" command has been disabled.\n"
7249541Sjoerg		"Use \"weof\" if you really want to write end-of-file marks,\n"
7259541Sjoerg		"or \"eom\" if you rather want to skip to the end of "
7269541Sjoerg		"recorded medium.\n");
7279541Sjoerg	exit(1);
7289541Sjoerg}
7299541Sjoerg
7307913Sjoerg#endif /* defined (__FreeBSD__) */
731