mt.c revision 41945
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[] =
4541945Smjacob	"$Id: mt.c,v 1.18 1998/12/18 18:16:35 mjacob Exp $";
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 },
11539260Sgibbs	{ "comp",	MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
11613401Sjoerg	{ "retension",	MTRETENS, 1 },
11741913Smjacob	{ "rdhpos",     MTIOCRDHPOS,  0 },
11841913Smjacob	{ "rdspos",     MTIOCRDSPOS,  0 },
11941913Smjacob	{ "sethpos",    MTIOCHLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
12041913Smjacob	{ "setspos",    MTIOCSLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
12141945Smjacob	{ "errstat",	MTIOCERRSTAT, 0 },
1227913Sjoerg#endif /* defined(__FreeBSD__) */
1231590Srgrimes	{ NULL }
1241590Srgrimes};
1251590Srgrimes
1261590Srgrimesvoid printreg __P((char *, u_int, char *));
1271590Srgrimesvoid status __P((struct mtget *));
1281590Srgrimesvoid usage __P((void));
1297913Sjoerg#if defined (__FreeBSD__)
1307913Sjoergvoid st_status (struct mtget *);
1317929Sjoergint stringtodens (const char *s);
1327929Sjoergconst char *denstostring (int d);
13339260Sgibbsint denstobp(int d, int bpi);
13439260Sgibbsu_int32_t stringtocomp(const char *s);
13539260Sgibbsconst char * comptostring(u_int32_t comp);
1369541Sjoergvoid warn_eof __P((void));
1377913Sjoerg#endif /* defined (__FreeBSD__) */
1381590Srgrimes
1391590Srgrimesint
1401590Srgrimesmain(argc, argv)
1411590Srgrimes	int argc;
1421590Srgrimes	char *argv[];
1431590Srgrimes{
1441590Srgrimes	register struct commands *comp;
1451590Srgrimes	struct mtget mt_status;
1461590Srgrimes	struct mtop mt_com;
1471590Srgrimes	int ch, len, mtfd;
1481590Srgrimes	char *p, *tape;
1491590Srgrimes
1501590Srgrimes	if ((tape = getenv("TAPE")) == NULL)
1511590Srgrimes		tape = DEFTAPE;
1521590Srgrimes
15324360Simp	while ((ch = getopt(argc, argv, "f:t:")) != -1)
1541590Srgrimes		switch(ch) {
1551590Srgrimes		case 'f':
1561590Srgrimes		case 't':
1571590Srgrimes			tape = optarg;
1581590Srgrimes			break;
1591590Srgrimes		case '?':
1601590Srgrimes		default:
1611590Srgrimes			usage();
1621590Srgrimes		}
1631590Srgrimes	argc -= optind;
1641590Srgrimes	argv += optind;
1651590Srgrimes
1661590Srgrimes	if (argc < 1 || argc > 2)
1671590Srgrimes		usage();
1681590Srgrimes
1691590Srgrimes	len = strlen(p = *argv++);
1701590Srgrimes	for (comp = com;; comp++) {
1711590Srgrimes		if (comp->c_name == NULL)
17227752Scharnier			errx(1, "%s: unknown command", p);
1731590Srgrimes		if (strncmp(p, comp->c_name, len) == 0)
1741590Srgrimes			break;
1751590Srgrimes	}
1767913Sjoerg#if defined(__FreeBSD__)
1777913Sjoerg	if((comp->c_flags & NEED_2ARGS) && argc != 2)
1787913Sjoerg		usage();
1799541Sjoerg	if(comp->c_flags & DISABLE_THIS) {
1809541Sjoerg		warn_eof();
1819541Sjoerg	}
1827913Sjoerg#endif /* defined(__FreeBSD__) */
1831590Srgrimes	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
18427752Scharnier		err(1, "%s", tape);
1851590Srgrimes	if (comp->c_code != MTNOP) {
1861590Srgrimes		mt_com.mt_op = comp->c_code;
1871590Srgrimes		if (*argv) {
1887913Sjoerg#if defined (__FreeBSD__)
1897929Sjoerg			if (!isdigit(**argv) &&
19041925Smjacob			    (comp->c_flags & IS_DENSITY)) {
1917929Sjoerg				const char *dcanon;
1927929Sjoerg				mt_com.mt_count = stringtodens(*argv);
1937929Sjoerg				if (mt_com.mt_count == 0)
19427752Scharnier					errx(1, "%s: unknown density", *argv);
1957929Sjoerg				dcanon = denstostring(mt_com.mt_count);
1967929Sjoerg				if (strcmp(dcanon, *argv) != 0)
1977929Sjoerg					printf(
1987929Sjoerg					"Using \"%s\" as an alias for %s\n",
1997929Sjoerg					       *argv, dcanon);
2007929Sjoerg				p = "";
20139260Sgibbs			} else if (!isdigit(**argv) &&
20241925Smjacob				   (comp->c_flags & IS_COMP)) {
20339260Sgibbs
20439260Sgibbs				mt_com.mt_count = stringtocomp(*argv);
20539260Sgibbs				if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
20639260Sgibbs					errx(1, "%s: unknown compression",
20739260Sgibbs					     *argv);
20839260Sgibbs				p = "";
2097929Sjoerg			} else
2107929Sjoerg				/* allow for hex numbers; useful for density */
2117929Sjoerg				mt_com.mt_count = strtol(*argv, &p, 0);
2127913Sjoerg#else
2131590Srgrimes			mt_com.mt_count = strtol(*argv, &p, 10);
2147913Sjoerg#endif /* defined(__FreeBSD__) */
21541925Smjacob			if ((mt_com.mt_count <=
2167913Sjoerg#if defined (__FreeBSD__)
2177913Sjoerg			    ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
21839260Sgibbs			    && ((comp->c_flags & IS_COMP) == 0)
2197913Sjoerg#else
2207913Sjoerg			    0
2217913Sjoerg#endif /* defined (__FreeBSD__) */
22241925Smjacob			    ) || *p)
22327752Scharnier				errx(1, "%s: illegal count", *argv);
2241590Srgrimes		}
2251590Srgrimes		else
2261590Srgrimes			mt_com.mt_count = 1;
22741913Smjacob#if	defined(__FreeBSD__)
22841913Smjacob		switch (comp->c_code) {
22941945Smjacob		case MTIOCERRSTAT:
23041945Smjacob		{
23141945Smjacob			int i;
23241945Smjacob			union mterrstat umn;
23341945Smjacob			struct scsi_tape_errors *s = &umn.scsi_errstat;
23441945Smjacob
23541945Smjacob			if (ioctl(mtfd, comp->c_code, (caddr_t)&umn) < 0)
23641945Smjacob				err(2, "%s", tape);
23741945Smjacob			(void)printf("Last I/O Residual: %u\n", s->io_resid);
23841945Smjacob			(void)printf("   Last I/O Sense:\n\n\t");
23941945Smjacob			for (i = 0; i < sizeof (s->io_sense); i++) {
24041945Smjacob				(void)printf(" %02X", s->io_sense[i]);
24141945Smjacob				if (((i + 1) & 0xf) == 0) {
24241945Smjacob					(void)printf("\n\t");
24341945Smjacob				}
24441945Smjacob			}
24541945Smjacob			(void)printf("\n");
24641945Smjacob			(void)printf("Last Control Residual: %u\n",
24741945Smjacob			    s->ctl_resid);
24841945Smjacob			(void)printf("   Last Control Sense:\n\n\t");
24941945Smjacob			for (i = 0; i < sizeof (s->ctl_sense); i++) {
25041945Smjacob				(void)printf(" %02X", s->ctl_sense[i]);
25141945Smjacob				if (((i + 1) & 0xf) == 0) {
25241945Smjacob					(void)printf("\n\t");
25341945Smjacob				}
25441945Smjacob			}
25541945Smjacob			(void)printf("\n\n");
25641945Smjacob			exit(0);
25741945Smjacob			/* NOTREACHED */
25841945Smjacob		}
25941913Smjacob		case MTIOCRDHPOS:
26041913Smjacob		case MTIOCRDSPOS:
26141925Smjacob		{
26241925Smjacob			u_int32_t block;
26341925Smjacob			if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
26441913Smjacob				err(2, "%s", tape);
26541945Smjacob			(void)printf("%s: %s block location %u\n", tape,
26641913Smjacob			    (comp->c_code == MTIOCRDHPOS)? "hardware" :
26741925Smjacob			    "logical", block);
26841925Smjacob			exit(0);
26941913Smjacob			/* NOTREACHED */
27041925Smjacob		}
27141913Smjacob		case MTIOCSLOCATE:
27241913Smjacob		case MTIOCHLOCATE:
27341925Smjacob		{
27441925Smjacob			u_int32_t block = (u_int32_t)mt_com.mt_count;
27541925Smjacob			if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
27641913Smjacob				err(2, "%s", tape);
27741925Smjacob			exit(0);
27841913Smjacob			/* NOTREACHED */
27941925Smjacob		}
28041913Smjacob		default:
28141913Smjacob			break;
28241913Smjacob		}
28341913Smjacob#endif
2841590Srgrimes		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
28527752Scharnier			err(1, "%s: %s", tape, comp->c_name);
2861590Srgrimes	} else {
2871590Srgrimes		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
28827752Scharnier			err(1, NULL);
2891590Srgrimes		status(&mt_status);
2901590Srgrimes	}
29141925Smjacob	exit(0);
2921590Srgrimes	/* NOTREACHED */
2931590Srgrimes}
2941590Srgrimes
2951590Srgrimes#ifdef vax
2961590Srgrimes#include <vax/mba/mtreg.h>
2971590Srgrimes#include <vax/mba/htreg.h>
2981590Srgrimes
2991590Srgrimes#include <vax/uba/utreg.h>
3001590Srgrimes#include <vax/uba/tmreg.h>
3011590Srgrimes#undef b_repcnt		/* argh */
3021590Srgrimes#include <vax/uba/tsreg.h>
3031590Srgrimes#endif
3041590Srgrimes
3051590Srgrimes#ifdef sun
3061590Srgrimes#include <sundev/tmreg.h>
3071590Srgrimes#include <sundev/arreg.h>
3081590Srgrimes#endif
3091590Srgrimes
3101590Srgrimes#ifdef tahoe
3111590Srgrimes#include <tahoe/vba/cyreg.h>
3121590Srgrimes#endif
3131590Srgrimes
31439913Sdfr#if defined(__FreeBSD__) && defined(__i386__)
31514176Sjoerg#include <machine/wtio.h>
31614176Sjoerg#endif
31714176Sjoerg
3181590Srgrimesstruct tape_desc {
3191590Srgrimes	short	t_type;		/* type of magtape device */
3201590Srgrimes	char	*t_name;	/* printing name */
3211590Srgrimes	char	*t_dsbits;	/* "drive status" register */
3221590Srgrimes	char	*t_erbits;	/* "error" register */
3231590Srgrimes} tapes[] = {
3241590Srgrimes#ifdef vax
3251590Srgrimes	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
3261590Srgrimes	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
3271590Srgrimes	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
3281590Srgrimes	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
3291590Srgrimes	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
3301590Srgrimes#endif
3311590Srgrimes#ifdef sun
3321590Srgrimes	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
3331590Srgrimes	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
3341590Srgrimes#endif
3351590Srgrimes#ifdef tahoe
3361590Srgrimes	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
3371590Srgrimes#endif
3387913Sjoerg#if defined (__FreeBSD__)
3397913Sjoerg	/*
34014176Sjoerg	 * XXX This is weird.  The st driver reports the tape drive
3417913Sjoerg	 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
3427913Sjoerg	 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
3437913Sjoerg	 * (MT_ISMFOUR) for other tapes.
3447913Sjoerg	 * XXX for the wt driver, rely on it behaving like a "standard"
3457913Sjoerg	 * magtape driver.
3467913Sjoerg	 */
3477913Sjoerg	{ MT_ISAR,	"SCSI tape drive", 0,		0 },
34839913Sdfr#if defined (__i386__)
34914176Sjoerg	{ MT_ISVIPER1,	"Archive Viper", WTDS_BITS, WTER_BITS },
35014176Sjoerg	{ MT_ISMFOUR,	"Wangtek",	 WTDS_BITS, WTER_BITS },
35139913Sdfr#endif
3527913Sjoerg#endif /* defined (__FreeBSD__) */
3531590Srgrimes	{ 0 }
3541590Srgrimes};
3551590Srgrimes
3561590Srgrimes/*
3571590Srgrimes * Interpret the status buffer returned
3581590Srgrimes */
3591590Srgrimesvoid
3601590Srgrimesstatus(bp)
3611590Srgrimes	register struct mtget *bp;
3621590Srgrimes{
3631590Srgrimes	register struct tape_desc *mt;
3641590Srgrimes
3651590Srgrimes	for (mt = tapes;; mt++) {
3661590Srgrimes		if (mt->t_type == 0) {
3671590Srgrimes			(void)printf("%d: unknown tape drive type\n",
3681590Srgrimes			    bp->mt_type);
3691590Srgrimes			return;
3701590Srgrimes		}
3711590Srgrimes		if (mt->t_type == bp->mt_type)
3721590Srgrimes			break;
3731590Srgrimes	}
3747913Sjoerg#if defined (__FreeBSD__)
3757913Sjoerg	if(mt->t_type == MT_ISAR)
3767913Sjoerg		st_status(bp);
3777913Sjoerg	else {
3787913Sjoerg#endif /* defined (__FreeBSD__) */
3791590Srgrimes	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
38014176Sjoerg	printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
38114176Sjoerg	printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
3821590Srgrimes	(void)putchar('\n');
3837913Sjoerg#if defined (__FreeBSD__)
3847913Sjoerg	}
3857913Sjoerg#endif /* defined (__FreeBSD__) */
3861590Srgrimes}
3871590Srgrimes
3881590Srgrimes/*
3891590Srgrimes * Print a register a la the %b format of the kernel's printf.
3901590Srgrimes */
3911590Srgrimesvoid
3921590Srgrimesprintreg(s, v, bits)
3931590Srgrimes	char *s;
3941590Srgrimes	register u_int v;
3951590Srgrimes	register char *bits;
3961590Srgrimes{
3971590Srgrimes	register int i, any = 0;
3981590Srgrimes	register char c;
3991590Srgrimes
4001590Srgrimes	if (bits && *bits == 8)
4011590Srgrimes		printf("%s=%o", s, v);
4021590Srgrimes	else
4031590Srgrimes		printf("%s=%x", s, v);
40411608Sbde	if (!bits)
40511608Sbde		return;
4061590Srgrimes	bits++;
4071590Srgrimes	if (v && bits) {
4081590Srgrimes		putchar('<');
40927752Scharnier		while ((i = *bits++)) {
4101590Srgrimes			if (v & (1 << (i-1))) {
4111590Srgrimes				if (any)
4121590Srgrimes					putchar(',');
4131590Srgrimes				any = 1;
4141590Srgrimes				for (; (c = *bits) > 32; bits++)
4151590Srgrimes					putchar(c);
4161590Srgrimes			} else
4171590Srgrimes				for (; *bits > 32; bits++)
4181590Srgrimes					;
4191590Srgrimes		}
4201590Srgrimes		putchar('>');
4211590Srgrimes	}
4221590Srgrimes}
4231590Srgrimes
4241590Srgrimesvoid
4251590Srgrimesusage()
4261590Srgrimes{
4271590Srgrimes	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
4281590Srgrimes	exit(1);
4291590Srgrimes}
4301590Srgrimes
4317913Sjoerg#if defined (__FreeBSD__)
4327913Sjoerg
4337913Sjoergstruct densities {
4347913Sjoerg	int dens;
43539260Sgibbs	int bpmm;
43639260Sgibbs	int bpi;
4377913Sjoerg	const char *name;
43839260Sgibbs} dens[] = {
43939260Sgibbs	/*
44039260Sgibbs	 * Taken from T10 Project 997D
44139260Sgibbs	 * SCSI-3 Stream Device Commands (SSC)
44239260Sgibbs	 * Revision 11, 4-Nov-97
44339260Sgibbs	 */
44439260Sgibbs	/*Num.  bpmm    bpi     Reference     */
44539260Sgibbs	{ 0x1,	32,	800,	"X3.22-1983" },
44639260Sgibbs	{ 0x2,	63,	1600,	"X3.39-1986" },
44739260Sgibbs	{ 0x3,	246,	6250,	"X3.54-1986" },
44839260Sgibbs	{ 0x5,	315,	8000,	"X3.136-1986" },
44939260Sgibbs	{ 0x6,	126,	3200,	"X3.157-1987" },
45039260Sgibbs	{ 0x7,	252,	6400,	"X3.116-1986" },
45139260Sgibbs	{ 0x8,	315,	8000,	"X3.158-1987" },
45239260Sgibbs	{ 0x9,	491,	37871,	"X3.180" },
45339260Sgibbs	{ 0xA,	262,	6667,	"X3B5/86-199" },
45439260Sgibbs	{ 0xB,	63,	1600,	"X3.56-1986" },
45539260Sgibbs	{ 0xC,	500,	12690,	"HI-TC1" },
45639260Sgibbs	{ 0xD,	999,	25380,	"HI-TC2" },
45739260Sgibbs	{ 0xF,	394,	10000,	"QIC-120" },
45839260Sgibbs	{ 0x10,	394,	10000,	"QIC-150" },
45939260Sgibbs	{ 0x11,	630,	16000,	"QIC-320" },
46039260Sgibbs	{ 0x12,	2034,	51667,	"QIC-1350" },
46139260Sgibbs	{ 0x13,	2400,	61000,	"X3B5/88-185A" },
46239260Sgibbs	{ 0x14,	1703,	43245,	"X3.202-1991" },
46339260Sgibbs	{ 0x15,	1789,	45434,	"ECMA TC17" },
46439260Sgibbs	{ 0x16,	394,	10000,	"X3.193-1990" },
46539260Sgibbs	{ 0x17,	1673,	42500,	"X3B5/91-174" },
46639260Sgibbs	{ 0x18,	1673,	42500,	"X3B5/92-50" },
46739260Sgibbs	{ 0x1C, 1654,	42000,	"QIC-385M" },
46839260Sgibbs	{ 0x1D,	1512,	38400,	"QIC-410M" },
46939260Sgibbs	{ 0x1E, 1385,	36000,	"QIC-1000C" },
47039260Sgibbs	{ 0x1F,	2666,	67733,	"QIC-2100C" },
47139260Sgibbs	{ 0x20, 2666,	67733,	"QIC-6GB(M)" },
47239260Sgibbs	{ 0x21,	2666,	67733,	"QIC-20GB(C)" },
47339260Sgibbs	{ 0x22,	1600,	40640,	"QIC-2GB(C)" },
47439260Sgibbs	{ 0x23, 2666,	67733,	"QIC-875M" },
47539260Sgibbs	{ 0x24,	2400,	61000,	"DDS-2" },
47639260Sgibbs	{ 0x25,	3816,	97000,	"DDS-3" },
47739260Sgibbs	{ 0x26,	3816,	97000,	"DDS-4" },
47839260Sgibbs	{ 0x27,	3056,	77611,	"Mammoth" },
47939260Sgibbs	{ 0x28,	1491,	37871,	"X3.224" },
48039260Sgibbs	{ 0, 0, 0, NULL }
4817913Sjoerg};
4827913Sjoerg
48339260Sgibbsstruct compression_types {
48439260Sgibbs	u_int32_t	comp_number;
48539260Sgibbs	const char 	*name;
48639260Sgibbs} comp_types[] = {
48739260Sgibbs	{ 0x00, "none" },
48839260Sgibbs	{ 0x00, "off" },
48939260Sgibbs	{ 0x10, "IDRC" },
49039260Sgibbs	{ 0x20, "DCLZ" },
49139260Sgibbs	{ 0xffffffff, "enable" },
49239260Sgibbs	{ 0xffffffff, "on" },
49339260Sgibbs	{ 0xf0f0f0f0, NULL}
49439260Sgibbs};
49539260Sgibbs
4967913Sjoergconst char *
4977929Sjoergdenstostring(int d)
4987913Sjoerg{
4997913Sjoerg	static char buf[20];
5007913Sjoerg	struct densities *sd;
5017913Sjoerg
5027913Sjoerg	for (sd = dens; sd->dens; sd++)
5037913Sjoerg		if (sd->dens == d)
5047913Sjoerg			break;
5057913Sjoerg	if (sd->dens == 0) {
5067929Sjoerg		sprintf(buf, "0x%02x", d);
5077913Sjoerg		return buf;
5087929Sjoerg	} else
5097913Sjoerg		return sd->name;
5107913Sjoerg}
5117913Sjoerg
51239260Sgibbs/*
51339260Sgibbs * Given a specific density number, return either the bits per inch or bits
51439260Sgibbs * per millimeter for the given density.
51539260Sgibbs */
5167929Sjoergint
51739260Sgibbsdenstobp(int d, int bpi)
51839260Sgibbs{
51939260Sgibbs	struct densities *sd;
52039260Sgibbs
52139260Sgibbs	for (sd = dens; sd->dens; sd++)
52239260Sgibbs		if (sd->dens == d)
52339260Sgibbs			break;
52439260Sgibbs	if (sd->dens == 0)
52539260Sgibbs		return(0);
52639260Sgibbs	else {
52739260Sgibbs		if (bpi)
52839260Sgibbs			return(sd->bpi);
52939260Sgibbs		else
53039260Sgibbs			return(sd->bpmm);
53139260Sgibbs	}
53239260Sgibbs}
53339260Sgibbs
53439260Sgibbsint
5357929Sjoergstringtodens(const char *s)
5367929Sjoerg{
5377929Sjoerg	struct densities *sd;
5387929Sjoerg	size_t l = strlen(s);
5397929Sjoerg
5407929Sjoerg	for (sd = dens; sd->dens; sd++)
5417929Sjoerg		if (strncasecmp(sd->name, s, l) == 0)
5427929Sjoerg			break;
5437929Sjoerg	return sd->dens;
5447929Sjoerg}
5457929Sjoerg
5467929Sjoerg
5477913Sjoergconst char *
5487913Sjoerggetblksiz(int bs)
5497913Sjoerg{
5507913Sjoerg	static char buf[25];
5517913Sjoerg	if (bs == 0)
5527913Sjoerg		return "variable";
5537913Sjoerg	else {
55439260Sgibbs		sprintf(buf, "%d bytes", bs);
5557913Sjoerg		return buf;
5567913Sjoerg	}
5577913Sjoerg}
5587913Sjoerg
55939260Sgibbsconst char *
56039260Sgibbscomptostring(u_int32_t comp)
56139260Sgibbs{
56239260Sgibbs	static char buf[20];
56339260Sgibbs	struct compression_types *ct;
5647913Sjoerg
56539260Sgibbs	if (comp == MT_COMP_DISABLED)
56639260Sgibbs		return "disabled";
56739260Sgibbs	else if (comp == MT_COMP_UNSUPP)
56839260Sgibbs		return "unsupported";
56939260Sgibbs
57039260Sgibbs	for (ct = comp_types; ct->name; ct++)
57139260Sgibbs		if (ct->comp_number == comp)
57239260Sgibbs			break;
57339260Sgibbs
57439260Sgibbs	if (ct->comp_number == 0xf0f0f0f0) {
57539260Sgibbs		sprintf(buf, "0x%2x", comp);
57639260Sgibbs		return(buf);
57739260Sgibbs	} else
57839260Sgibbs		return(ct->name);
57939260Sgibbs}
58039260Sgibbs
58139260Sgibbsu_int32_t
58239260Sgibbsstringtocomp(const char *s)
58339260Sgibbs{
58439260Sgibbs	struct compression_types *ct;
58539260Sgibbs	size_t l = strlen(s);
58639260Sgibbs
58739260Sgibbs	for (ct = comp_types; ct->name; ct++)
58839260Sgibbs		if (strncasecmp(ct->name, s, l) == 0)
58939260Sgibbs			break;
59039260Sgibbs
59139260Sgibbs	return(ct->comp_number);
59239260Sgibbs}
59339260Sgibbs
5947913Sjoergvoid
5957913Sjoergst_status(struct mtget *bp)
5967913Sjoerg{
59739260Sgibbs	printf("Mode      Density         Blocksize      bpi      "
59839260Sgibbs	       "Compression\n"
59939260Sgibbs	       "Current:  %-12s    %-12s   %-7d  %s\n"
60039260Sgibbs	       "---------available modes---------\n"
60139260Sgibbs	       "0:        %-12s    %-12s   %-7d  %s\n"
60239260Sgibbs	       "1:        %-12s    %-12s   %-7d  %s\n"
60339260Sgibbs	       "2:        %-12s    %-12s   %-7d  %s\n"
60439260Sgibbs	       "3:        %-12s    %-12s   %-7d  %s\n",
60539260Sgibbs	       denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
60639260Sgibbs	       denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
60739260Sgibbs	       denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
60839260Sgibbs	       denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
60939260Sgibbs	       denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
61039260Sgibbs	       denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
61139260Sgibbs	       denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
61239260Sgibbs	       denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
61339260Sgibbs	       denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
61439260Sgibbs	       denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
6157913Sjoerg}
6167913Sjoerg
6179541Sjoergvoid
6189541Sjoergwarn_eof(void)
6199541Sjoerg{
6209541Sjoerg	fprintf(stderr,
6219541Sjoerg		"The \"eof\" command has been disabled.\n"
6229541Sjoerg		"Use \"weof\" if you really want to write end-of-file marks,\n"
6239541Sjoerg		"or \"eom\" if you rather want to skip to the end of "
6249541Sjoerg		"recorded medium.\n");
6259541Sjoerg	exit(1);
6269541Sjoerg}
6279541Sjoerg
6287913Sjoerg#endif /* defined (__FreeBSD__) */
629