*BSD News Article 19477


Return to BSD News archive

Xref: sserve comp.os.386bsd.misc:676 alt.sources:6265
Newsgroups: comp.os.386bsd.misc,alt.sources
Path: sserve!newshost.anu.edu.au!munnari.oz.au!constellation!convex!convex!convex!cs.utexas.edu!uunet!math.fu-berlin.de!kiae!relcom!rdrel!vak
From: vak@rd.relcom.msk.su (Serge Vakulenko)
Subject: [386BSD] New driver for QIC-02 Archive and Wangtek tapes
Message-ID: <1993Aug9.210652.1984@rd.relcom.msk.su>
Originator: vak@newcom.kiae.su
Sender: news@math.fu-berlin.de (Math Department)
Nntp-Posting-Host: 144.206.136.5
Organization: Relcom R&D
Date: Mon, 9 Aug 1993 21:06:52 GMT
Lines: 1612

This is the streamer tape driver for 386bsd,
which supports Wangtek and Archive controllers.
It was developed as a replacement of the old Wangtek
tape driver from CMU.

In comparison with the CMU driver, this version has the following enhancements:
1) Support for Archive SC402 and SC499 tape controllers added.
2) Support for up to three tape controllers on the same machine.
3) Support for BSD-style ioctls MTIOCGET, MTIOCTOP.
   Mt command now works adequately with this driver.
2) Asynchronous REWIND and FSF operations, close() will not wait
   until they finish.  The next open() will wait for it instead.
4) Use of WTQICMD ioctl is limited to ERASE and RETENS operations.
   This prevents the user from locking the tape driver by strange
   tape operations.
5) Tape density switching added.
6) The status of the process, blocked on the tape operation,
   is displayed at the WCHAN column of the `ps' command as:

	wtread    reading data from the tape
	wtwrite   writing data to the tape
	wtrfm     reading the tape marker
	wtwfm     writing the tape marker
	wtrew     rewinding the tape
	wterase   doing WTQICMD ERASE operation
	wtretens  doing WTQICMD RETENS operation
	wtorew    doing MTIOCTOP REW/OFFL operation
	wtorfm    doing MTIOCTOP FSF operation
	wtowfm    doing MTIOCTOP WEOF operation

Block interface (writing blocks less than 2048 bytes) is not functioning
pwoperly.  Use raw interface instead.


Copyright (C) 1993 by:
     Sergey Ryzhkov       <sir@kiae.su>
     Serge Vakulenko      <vak@zebub.msk.su>

Version 1.0, Mon Aug  9 00:09:34 MSD 1993

Placed in the public domain with NO WARRANTIES, not even the implied
warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Authors grant any other persons or organisations permission to use
or modify this software as long as this message is kept with the software,
all derivative works or modified versions.

This driver is derived from the old 386bsd Wangtek streamer tape driver,
made by Robert Baron at CMU, based on Intel sources.
Authors thank Robert Baron, CMU and Intel and retain here
the original CMU copyright notice.

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	INSTALL
#	NOTES
#	README
#	mt
#	wt.c
#	wtreg.h
#	mt/Makefile
#	mt/README
#	mt/mt.1
#	mt/mt.c
#
echo x - INSTALL
sed 's/^X//' >INSTALL << 'END-of-INSTALL'
XTo install this driver into the kernel of 386bsd,
Xdo the following steps:
X
X1) Save of the  old version of driver and install the new one:
X
X	mv /sys/i386/isa/wt.c /sys/i386/isa/wt.c~
X	mv /sys/i386/isa/wtreg.h /sys/i386/isa/wtreg.h~
X	cp wt.c /sys/i386/isa/wt.c
X	cp wtreg.h /sys/i386/isa/wtreg.h
X
X2) Check that your config file has valid description of your
X   tape controller parameters, for example:
X
X	device  wt0     at isa? port 0x300 bio irq 5 drq 1 vector wtintr
X
X   Valid DRQs are 1..3.  It makes possible to use up to three
X   tapes on the same machine simultaneously.
X
X3) Make the tape part of your /dev/MAKEDEV looking like this:
X
X	wt*)
X		umask 2
X		mknod  rwt0a c 10 0     # 150 Mbytes
X		mknod  rwt0b c 10 8     # 120 Mbytes
X		mknod  rwt0c c 10 16    # 60 Mbytes
X		mknod nrwt0a c 10 4     # 150 Mbytes, no rewind
X		mknod nrwt0b c 10 12    # 120 Mbytes, no rewind
X		mknod nrwt0c c 10 20    # 60 Mbytes, no rewind
X		ln -s  rwt0a  rwt0
X		ln -s nrwt0a nrwt0
X		umask 77
X		;;
X
X4) Delete old tape nodes in the /dev directory, and install new entries:
X
X	cd /dev
X	rm wt* rwt*
X	sh -x MAKEDEV wt
X
X5) Reconfig, recompile and install the new kernel:
X
X	cd /sys/i386/conf
X	config YOUR_CONFIG
X	cd /sys/compile/YOUR_CONFIG
X	make depend
X	make
X	mv 386bsd /
X	sync
X
X6) Reboot and try to use the tape.
X
XGood luck!
X
X
XSerge Vakulenko, <vak@zebub.msk.su>
END-of-INSTALL
echo x - NOTES
sed 's/^X//' >NOTES << 'END-of-NOTES'
XThe version 1.0 was tested only on Archive SC499
Xcontroller, with port=300h, drq=1 and irq=3.
X
XWe did not tested it neither on Wangtek controllers,
Xnor on other Archive compatible cards.
X
XWe have no such hardware, and to gather information
Xabout this driver working with other tapes, we need
Xa feedback from the user community.
X
XPlease, if you succesfully installed this driver
Xon your hardware, or if you failed to install it,
Xand it does not function properly, please, send us
Xan e-mail letter, with the description of your hardware and
Xversion of your operating system.
X
XSergey Ryzhkov, Serge Vakulenko
X
XE-mail: <sir@kiae.su>, <vak@zebub.msk.su>
END-of-NOTES
echo x - README
sed 's/^X//' >README << 'END-of-README'
XThis is the streamer tape driver for 386bsd,
Xwhich supports Wangtek and Archive controllers.
XIt was developed as a replacement of the old Wangtek
Xtape driver from CMU.
X
XIn comparison with the CMU driver, this version has the following enhancements:
X1) Support for Archive SC402 and SC499 tape controllers added.
X2) Support for up to three tape controllers on the same machine.
X3) Support for BSD-style ioctls MTIOCGET, MTIOCTOP.
X   Mt command now works adequately with this driver.
X2) Asynchronous REWIND and FSF operations, close() will not wait
X   until they finish.  The next open() will wait for it instead.
X4) Use of WTQICMD ioctl is limited to ERASE and RETENS operations.
X   This prevents the user from locking the tape driver by strange
X   tape operations.
X5) Tape density switching added.
X6) The status of the process, blocked on the tape operation,
X   is displayed at the WCHAN column of the `ps' command as:
X
X	wtread    reading data from the tape
X	wtwrite   writing data to the tape
X	wtrfm     reading the tape marker
X	wtwfm     writing the tape marker
X	wtrew     rewinding the tape
X	wterase   doing WTQICMD ERASE operation
X	wtretens  doing WTQICMD RETENS operation
X	wtorew    doing MTIOCTOP REW/OFFL operation
X	wtorfm    doing MTIOCTOP FSF operation
X	wtowfm    doing MTIOCTOP WEOF operation
X
XBlock interface (writing blocks less than 2048 bytes) is not functioning
Xpwoperly.  Use raw interface instead.
X
X
XCopyright (C) 1993 by:
X     Sergey Ryzhkov       <sir@kiae.su>
X     Serge Vakulenko      <vak@zebub.msk.su>
X
XVersion 1.0, Mon Aug  9 00:09:34 MSD 1993
X
XPlaced in the public domain with NO WARRANTIES, not even the implied
Xwarranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
X
XAuthors grant any other persons or organisations permission to use
Xor modify this software as long as this message is kept with the software,
Xall derivative works or modified versions.
X
XThis driver is derived from the old 386bsd Wangtek streamer tape driver,
Xmade by Robert Baron at CMU, based on Intel sources.
XAuthors thank Robert Baron, CMU and Intel and retain here
Xthe original CMU copyright notice.
X
X
XCopyright (c) 1989 Carnegie-Mellon University.
XAll rights reserved.
X
XAuthors: Robert Baron
X
XPermission to use, copy, modify and distribute this software and
Xits documentation is hereby granted, provided that both the copyright
Xnotice and this permission notice appear in all copies of the
Xsoftware, derivative works or modified versions, and any portions
Xthereof, and that both notices appear in supporting documentation.
X
XCARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
XCONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
XFOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
X
XCarnegie Mellon requests users of this software to return to
X
X Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
X School of Computer Science
X Carnegie Mellon University
X Pittsburgh PA 15213-3890
X
Xany improvements or extensions that they make and grant Carnegie the
Xrights to redistribute these changes.
END-of-README
echo c - mt
mkdir mt > /dev/null 2>&1
echo x - wt.c
sed 's/^X//' >wt.c << 'END-of-wt.c'
X/*
X * Streamer tape driver for 386bsd.
X * Supports Archive QIC-02 and Wangtek QIC-02/QIC-36 boards.
X *
X * Copyright (C) 1993 by:
X *      Sergey Ryzhkov       <sir@kiae.su>
X *      Serge Vakulenko      <vak@zebub.msk.su>
X *
X * Version 1.0, Tue Aug 10 00:30:24 MSD 1993
X *
X * Placed in the public domain with NO WARRANTIES, not even the implied
X * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
X *
X * Authors grant any other persons or organisations permission to use
X * or modify this software as long as this message is kept with the software,
X * all derivative works or modified versions.
X *
X * This driver is derived from the old 386bsd Wangtek streamer tape driver,
X * made by Robert Baron at CMU, based on Intel sources.
X * Authors thank Robert Baron, CMU and Intel and retain here
X * the original CMU copyright notice.
X */
X
X/*
X * Copyright (c) 1989 Carnegie-Mellon University.
X * All rights reserved.
X *
X * Authors: Robert Baron
X * 
X * Permission to use, copy, modify and distribute this software and
X * its documentation is hereby granted, provided that both the copyright
X * notice and this permission notice appear in all copies of the
X * software, derivative works or modified versions, and any portions
X * thereof, and that both notices appear in supporting documentation.
X * 
X * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 
X * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 
X * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
X * 
X * Carnegie Mellon requests users of this software to return to
X *
X *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
X *  School of Computer Science
X *  Carnegie Mellon University
X *  Pittsburgh PA 15213-3890
X *
X * any improvements or extensions that they make and grant Carnegie the
X * rights to redistribute these changes.
X */
X
X#include "wt.h"
X#if NWT > 0
X
X#include "sys/param.h"
X#include "sys/buf.h"
X#include "sys/fcntl.h"
X#include "sys/malloc.h"
X#include "sys/ioctl.h"
X#include "sys/mtio.h"
X#include "vm/vm_param.h"
X#include "i386/include/pio.h"
X#include "i386/isa/isa_device.h"
X#include "i386/isa/wtreg.h"
X
X#define WTPRI                   (PZERO+10)      /* sleep priority */
X#define BLKSIZE                 512             /* streamer tape block size */
X
X/*
X * Wangtek controller ports
X */
X#define WT_CTLPORT(base)        ((base)+0)      /* control, write only */
X#define WT_STATPORT(base)       ((base)+0)      /* status, read only */
X#define WT_CMDPORT(base)        ((base)+1)      /* command, write only */
X#define WT_DATAPORT(base)       ((base)+1)      /* data, read only */
X#define WT_NPORT                2               /* 2 i/o ports */
X
X/* status port bits */
X#define WT_BUSY                 0x01            /* not ready bit define */
X#define WT_NOEXCEP              0x02            /* no exception bit define */
X#define WT_RESETMASK            0x07            /* to check after reset */
X#define WT_RESETVAL             0x05            /* state after reset */
X
X/* control port bits */
X#define WT_ONLINE               0x01            /* device selected */
X#define WT_RESET                0x02            /* reset command */
X#define WT_REQUEST              0x04            /* request command */
X#define WT_IEN(chan)            ((chan)>2 ? 0x10 : 0x8) /* enable intr */
X
X/*
X * Archive controller ports
X */
X#define AV_DATAPORT(base)       ((base)+0)      /* data, read only */
X#define AV_CMDPORT(base)        ((base)+0)      /* command, write only */
X#define AV_STATPORT(base)       ((base)+1)      /* status, read only */
X#define AV_CTLPORT(base)        ((base)+1)      /* control, write only */
X#define AV_SDMAPORT(base)       ((base)+2)      /* start dma */
X#define AV_RDMAPORT(base)       ((base)+3)      /* reset dma */
X#define AV_NPORT                4               /* 4 i/o ports */
X
X/* status port bits */
X#define AV_BUSY                 0x40            /* not ready bit define */
X#define AV_NOEXCEP              0x20            /* no exception bit define */
X#define AV_RESETMASK            0xf8            /* to check after reset */
X#define AV_RESETVAL             0x50            /* state after reset */
X
X/* control port bits */
X#define AV_RESET                0x80            /* reset command */
X#define AV_REQUEST              0x40            /* request command */
X#define AV_IEN                  0x20            /* enable interrupts */
X
X#define DMA_STATUSREG           0x8
X#define DMA_DONE(chan)          (1 << (chan))
X
Xtypedef struct {
X	unsigned short err;             /* code for error encountered */
X	unsigned short ercnt;           /* number of error blocks */
X	unsigned short urcnt;           /* number of underruns */
X} wtstatus_t;
X
Xtypedef struct {
X	unsigned unit;                  /* unit number */
X	unsigned port;                  /* base i/o port */
X	unsigned chan;                  /* dma channel number, 1..3 */
X	unsigned flags;                 /* state of tape drive */
X	unsigned dens;                  /* tape density */
X	void *buf;                      /* internal i/o buffer */
X
X	void *dmavaddr;                 /* virtual address of dma i/o buffer */
X	unsigned dmatotal;              /* size of i/o buffer */
X	unsigned dmaflags;              /* i/o direction, B_READ or B_WRITE */
X	unsigned dmacount;              /* resulting length of dma i/o */
X
X	wtstatus_t error;               /* status of controller */
X
X	unsigned short DATAPORT, CMDPORT, STATPORT, CTLPORT, SDMAPORT, RDMAPORT;
X	unsigned char BUSY, NOEXCEP, RESETMASK, RESETVAL;
X	unsigned char ONLINE, RESET, REQUEST, IEN;
X} wtinfo_t;
X
Xwtinfo_t wttab[NWT];                    /* tape info by unit number */
X
Xextern int hz;                          /* number of ticks per second */
X
Xstatic int wtwait (wtinfo_t *t, int catch, char *msg);
Xstatic int wtcmd (wtinfo_t *t, int cmd);
Xstatic int wtstart (wtinfo_t *t, unsigned mode, void *vaddr, unsigned len);
Xstatic void wtdma (wtinfo_t *t);
Xstatic void wtimer (wtinfo_t *t);
Xstatic void wtclock (wtinfo_t *t);
Xstatic int wtreset (wtinfo_t *t);
Xstatic int wtsense (wtinfo_t *t, int ignor);
Xstatic int wtstatus (wtinfo_t *t);
Xstatic void wtrewind (wtinfo_t *t);
Xstatic int wtreadfm (wtinfo_t *t);
Xstatic int wtwritefm (wtinfo_t *t);
Xstatic int wtpoll (wtinfo_t *t);
X
Xextern void DELAY (int usec);
Xextern void bcopy (void *from, void *to, unsigned len);
Xextern void isa_dmastart (int flags, void *addr, unsigned len, unsigned chan);
Xextern void isa_dmadone (int flags, void *addr, unsigned len, int chan);
Xextern void printf (char *str, ...);
Xextern int splbio (void);
Xextern int splx (int level);
Xextern void timeout (void (*func) (), void *arg, int timo);
Xextern int tsleep (void *chan, int priority, char *msg, int timo);
Xextern void wakeup (void *chan);
X
X/*
X * Probe for the presence of the device.
X */
Xint wtprobe (struct isa_device *id)
X{
X	wtinfo_t *t = wttab + id->id_unit;
X
X	t->unit = id->id_unit;
X	t->chan = id->id_drq;
X	t->port = 0;                    /* Mark it as not configured. */
X	if (t->chan<1 || t->chan>3) {
X		printf ("wt%d: Bad drq=%d, should be 1..3\n", t->unit, t->chan);
X		return (0);
X	}
X	t->port = id->id_iobase;
X
X	/* Try Wangtek. */
X	t->CTLPORT = WT_CTLPORT (t->port);  t->STATPORT = WT_STATPORT (t->port);
X	t->CMDPORT = WT_CMDPORT (t->port);  t->DATAPORT = WT_DATAPORT (t->port);
X	t->SDMAPORT = 0;                    t->RDMAPORT = 0;
X	t->BUSY = WT_BUSY;                  t->NOEXCEP = WT_NOEXCEP;
X	t->RESETMASK = WT_RESETMASK;        t->RESETVAL = WT_RESETVAL;
X	t->ONLINE = WT_ONLINE;              t->RESET = WT_RESET;
X	t->REQUEST = WT_REQUEST;            t->IEN = WT_IEN (t->chan);
X	if (wtreset (t))
X		return (WT_NPORT);
X
X	/* Try Archive. */
X	t->CTLPORT = AV_CTLPORT (t->port);  t->STATPORT = AV_STATPORT (t->port);
X	t->CMDPORT = AV_CMDPORT (t->port);  t->DATAPORT = AV_DATAPORT (t->port);
X	t->SDMAPORT = AV_SDMAPORT (t->port); t->RDMAPORT = AV_RDMAPORT (t->port);
X	t->BUSY = AV_BUSY;                  t->NOEXCEP = AV_NOEXCEP;
X	t->RESETMASK = AV_RESETMASK;        t->RESETVAL = AV_RESETVAL;
X	t->ONLINE = 0;                      t->RESET = AV_RESET;
X	t->REQUEST = AV_REQUEST;            t->IEN = AV_IEN;
X	if (wtreset (t))
X		return (AV_NPORT);
X
X	/* Tape controller not found. */
X	t->port = 0;
X	return (0);
X}
X
X/*
X * Device is found, configure it.
X */
Xint wtattach (struct isa_device *id)
X{
X	wtinfo_t *t = wttab + id->id_unit;
X
X	if (t->RDMAPORT) {
X		printf (" Archive");
X		outb (t->RDMAPORT, 0);          /* reset dma */
X	} else
X		printf (" Wangtek");
X	t->flags = TPSTART;                     /* tape is rewound */
X	t->dens = -1;                           /* unknown density */
X	t->buf = malloc (BLKSIZE, M_TEMP, M_NOWAIT);
X	return (1);
X}
X
Xstruct isa_driver wtdriver = { wtprobe, wtattach, "wt", };
X
Xint wtdump (int dev)
X{
X	/* Not implemented */
X	return (EINVAL);
X}
X
Xint wtsize (int dev)
X{
X	/* Not implemented */
X	return (-1);
X}
X
X/*
X * Open routine, called on every device open.
X */
Xint wtopen (int dev, int flag)
X{
X	int u = minor (dev) & T_UNIT;
X	wtinfo_t *t = wttab + u;
X	int error;
X
X	if (u >= NWT || !t->port)
X		return (ENXIO);
X
X	/* Check that device is not in use */
X	if (t->flags & TPINUSE)
X		return (EBUSY);
X
X	/* If the tape is in rewound state, check the status and set density. */
X	if (t->flags & TPSTART) {
X		/* If rewind is going on, wait */
X		error = wtwait (t, PCATCH, "wtrew");
X		if (error)
X			return (error);
X
X		if (! wtsense (t, (flag & FWRITE) ? 0 : TP_WRP)) {
X			/* Bad status. Reset the controller. */
X			if (! wtreset (t))
X				return (ENXIO);
X			if (! wtsense (t, (flag & FWRITE) ? 0 : TP_WRP))
X				return (ENXIO);
X		}
X
X		/* Set up tape density. */
X		if (t->dens != (minor (dev) & T_DENSEL)) {
X			int d;
X
X			switch (minor (dev) & T_DENSEL) {
X			default:
X			case T_800BPI:  d = QIC_FMT150; break;  /* minor 000 */
X			case T_1600BPI: d = QIC_FMT120; break;  /* minor 010 */
X			case T_6250BPI: d = QIC_FMT24;  break;  /* minor 020 */
X			case T_BADBPI:  d = QIC_FMT11;  break;  /* minor 030 */
X			}
X			if (! wtcmd (t, d))
X				return (ENXIO);
X
X			/* Check the status of the controller. */
X			if (! wtsense (t, (flag & FWRITE) ? 0 : TP_WRP))
X				return (ENXIO);
X
X			t->dens = minor (dev) & T_DENSEL;
X		}
X		t->flags &= ~TPSTART;
X	} else if (t->dens != (minor (dev) & T_DENSEL))
X		return (ENXIO);
X
X	t->flags = TPINUSE;
X	if (flag & FREAD)
X		t->flags |= TPREAD;
X	if (flag & FWRITE)
X		t->flags |= TPWRITE;
X	return (0);
X}
X
X/*
X * Close routine, called on last device close.
X */
Xint wtclose (int dev)
X{
X	int u = minor (dev) & T_UNIT;
X	wtinfo_t *t = wttab + u;
X
X	if (u >= NWT || !t->port)
X		return (ENXIO);
X
X	/* If rewind is pending, do nothing */
X	if (t->flags & TPREW)
X		goto done;
X
X	/* If seek forward is pending and no rewind on close, do nothing */
X	if ((t->flags & TPRMARK) && (minor (dev) & T_NOREWIND))
X		goto done;
X
X	/* If file mark read is going on, wait */
X	wtwait (t, 0, "wtrfm");
X
X	if (t->flags & TPWANY)
X		/* Tape was written.  Write file mark. */
X		wtwritefm (t);
X
X	if (! (minor (dev) & T_NOREWIND)) {
X		/* Rewind tape to beginning of tape. */
X		/* Don't wait until rewind, though. */
X		wtrewind (t);
X		goto done;
X	}
X	if ((t->flags & TPRANY) && ! (t->flags & (TPVOL | TPWANY)))
X		/* Space forward to after next file mark if no writing done. */
X		/* Don't wait for completion. */
X		wtreadfm (t);
Xdone:
X	t->flags &= TPREW | TPRMARK | TPSTART | TPTIMER;
X	return (0);
X}
X
X/*
X * Ioctl routine.  Compatible with BSD ioctls.
X * Direct QIC-02 commands ERASE and RETENSION added.
X * There are three possible ioctls:
X * ioctl (int fd, MTIOCGET, struct mtget *buf)  -- get status
X * ioctl (int fd, MTIOCTOP, struct mtop *buf)   -- do BSD-like op
X * ioctl (int fd, WTQICMD, int qicop)           -- do QIC op
X */
Xint wtioctl (int dev, int cmd, void *arg, int mode)
X{
X	int u = minor (dev) & T_UNIT;
X	wtinfo_t *t = wttab + u;
X	int error, count, op;
X
X	if (u >= NWT || !t->port)
X		return (ENXIO);
X
X	switch (cmd) {
X	default:
X		return (EINVAL);
X	case WTQICMD:                   /* direct QIC command */
X		op = (int) *(void**)arg;
X		switch (op) {
X		default:
X			return (EINVAL);
X		case QIC_ERASE:         /* erase the whole tape */
X			if (! (t->flags & TPWRITE) || (t->flags & TPWP))
X				return (EACCES);
X			if (error = wtwait (t, PCATCH, "wterase"))
X				return (error);
X			break;
X		case QIC_RETENS:        /* retension the tape */
X			if (error = wtwait (t, PCATCH, "wtretens"))
X				return (error);
X			break;
X		}
X		/* Both ERASE and RETENS operations work like REWIND. */
X		/* Simulate the rewind operation here. */
X		t->flags &= ~(TPRO | TPWO | TPVOL);
X		if (! wtcmd (t, op))
X			return (EIO);
X		t->flags |= TPSTART | TPREW;
X		if (op == QIC_ERASE)
X			t->flags |= TPWANY;
X		wtclock (t);
X		return (0);
X	case MTIOCIEOT:         /* ignore EOT errors */
X	case MTIOCEEOT:         /* enable EOT errors */
X		return (0);
X	case MTIOCGET:
X		((struct mtget*)arg)->mt_type = t->RDMAPORT ? MT_ISVIPER1 : 0x11;
X		((struct mtget*)arg)->mt_dsreg = t->flags;      /* status */
X		((struct mtget*)arg)->mt_erreg = t->error.err;  /* errors */
X		((struct mtget*)arg)->mt_resid = 0;
X		((struct mtget*)arg)->mt_fileno = 0;            /* file */
X		((struct mtget*)arg)->mt_blkno = 0;             /* block */
X		return (0);
X	case MTIOCTOP:
X		break;
X	}
X	switch ((short) ((struct mtop*)arg)->mt_op) {
X	default:
X	case MTFSR:             /* forward space record */
X	case MTBSR:             /* backward space record */
X	case MTBSF:             /* backward space file */
X		break;
X	case MTNOP:             /* no operation, sets status only */
X	case MTCACHE:           /* enable controller cache */
X	case MTNOCACHE:         /* disable controller cache */
X		return (0);
X	case MTREW:             /* rewind */
X	case MTOFFL:            /* rewind and put the drive offline */
X		if (t->flags & TPREW)   /* rewind is running */
X			return (0);
X		if (error = wtwait (t, PCATCH, "wtorew"))
X			return (error);
X		wtrewind (t);
X		return (0);
X	case MTFSF:             /* forward space file */
X		for (count=((struct mtop*)arg)->mt_count; count>0; --count) {
X			if (error = wtwait (t, PCATCH, "wtorfm"))
X				return (error);
X			if (error = wtreadfm (t))
X				return (error);
X		}
X		return (0);
X	case MTWEOF:            /* write an end-of-file record */
X		if (! (t->flags & TPWRITE) || (t->flags & TPWP))
X			return (EACCES);
X		if (error = wtwait (t, PCATCH, "wtowfm"))
X			return (error);
X		if (error = wtwritefm (t))
X			return (error);
X		return (0);
X	}
X	return (EINVAL);
X}
X
X/*
X * Strategy routine.
X */
Xvoid wtstrategy (struct buf *bp)
X{
X	int u = minor (bp->b_dev) & T_UNIT;
X	wtinfo_t *t = wttab + u;
X	int s;
X
X	bp->b_resid = bp->b_bcount;
X	if (u >= NWT || !t->port)
X		goto errxit;
X
X	/* at file marks and end of tape, we just return '0 bytes available' */
X	if (t->flags & TPVOL)
X		goto xit;
X
X	if (bp->b_flags & B_READ) {
X		/* Check read access and no previous write to this tape. */
X		if (! (t->flags & TPREAD) || (t->flags & TPWANY))
X			goto errxit;
X
X		/* For now, we assume that all data will be copied out */
X		/* If read command outstanding, just skip down */
X		if (! (t->flags & TPRO)) {
X			if (! wtsense (t, TP_WRP))      /* clear status */
X				goto errxit;
X			if (! wtcmd (t, QIC_RDDATA)) {  /* sed read mode */
X				wtsense (t, TP_WRP);
X				goto errxit;
X			}
X			t->flags |= TPRO | TPRANY;
X		}
X	} else {
X		/* Check write access and write protection. */
X		/* No previous read from this tape allowed. */
X		if (! (t->flags & TPWRITE) || (t->flags & (TPWP | TPRANY)))
X			goto errxit;
X
X		/* If write command outstanding, just skip down */
X		if (! (t->flags & TPWO)) {
X			if (! wtsense (t, 0))           /* clear status */
X				goto errxit;
X			if (! wtcmd (t, QIC_WRTDATA)) { /* set write mode */
X				wtsense (t, 0);
X				goto errxit;
X			}
X			t->flags |= TPWO | TPWANY;
X		}
X	}
X
X	if (! bp->b_bcount)
X		goto xit;
X
X	t->flags &= ~TPEXCEP;
X	s = splbio ();
X	if (wtstart (t, bp->b_flags, bp->b_un.b_addr, bp->b_bcount)) {
X		wtwait (t, 0, (bp->b_flags & B_READ) ? "wtread" : "wtwrite");
X		bp->b_resid -= t->dmacount;
X	}
X	splx (s);
X
X	if (t->flags & TPEXCEP) {
Xerrxit:         bp->b_flags |= B_ERROR;
X		bp->b_error = EIO;
X	}
Xxit:    biodone (bp);
X	return;
X}
X
X/*
X * Interrupt routine.
X */
Xvoid wtintr (int u)
X{
X	wtinfo_t *t = wttab + u;
X	unsigned char s;
X
X	if (u >= NWT || !t->port)
X		return;
X
X	s = inb (t->STATPORT);                  /* get status */
X	if ((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP))
X		return;                         /* device is busy */
X	outb (t->CTLPORT, t->ONLINE);           /* stop controller */
X
X	/*
X	 * Check if rewind finished.
X	 */
X	if (t->flags & TPREW) {
X		t->flags &= ~TPREW;             /* Rewind finished. */
X		wtsense (t, TP_WRP);
X		wakeup (t);
X		return;
X	}
X
X	/*
X	 * Check if writing/reading of file mark finished.
X	 */
X	if (t->flags & (TPRMARK | TPWMARK)) {
X		if (! (s & t->NOEXCEP))         /* Operation failed. */
X			wtsense (t, (t->flags & TPRMARK) ? TP_WRP : 0);
X		t->flags &= ~(TPRMARK | TPWMARK); /* Operation finished. */
X		wakeup (t);
X		return;
X	}
X
X	/*
X	 * Do we started any i/o?  If no, just return.
X	 */
X	if (! (t->flags & TPACTIVE))
X		return;
X	t->flags &= ~TPACTIVE;
X
X	if (inb (DMA_STATUSREG) & DMA_DONE (t->chan))   /* if dma finished */
X		t->dmacount += BLKSIZE;                 /* increment counter */
X
X	/*
X	 * Clean up dma.
X	 */
X	if ((t->dmaflags & B_READ) && (t->dmatotal - t->dmacount) < BLKSIZE) {
X		/* If the address crosses 64-k boundary, or reading short block,
X		 * copy the internal buffer to the user memory. */
X		isa_dmadone (t->dmaflags, t->buf, BLKSIZE, t->chan);
X		bcopy (t->buf, t->dmavaddr, t->dmatotal - t->dmacount);
X	} else
X		isa_dmadone (t->dmaflags, t->dmavaddr, BLKSIZE, t->chan);
X
X	/*
X	 * On exception, check for end of file and end of volume.
X	 */
X	if (! (s & t->NOEXCEP)) {
X		wtsense (t, (t->dmaflags & B_READ) ? TP_WRP : 0);
X		if (t->error.err & (TP_EOM | TP_FIL))
X			t->flags |= TPVOL;      /* end of file */
X		else
X			t->flags |= TPEXCEP;    /* i/o error */
X		wakeup (t);
X		return;
X	}
X
X	if (t->dmacount < t->dmatotal) {        /* continue i/o */
X		t->dmavaddr += BLKSIZE;
X		wtdma (t);
X		return;
X	}
X	if (t->dmacount > t->dmatotal)          /* short last block */
X		t->dmacount = t->dmatotal;
X	wakeup (t);                             /* wake up user level */
X}
X
X/* start the rewind operation */
Xstatic void wtrewind (wtinfo_t *t)
X{
X	t->flags &= ~(TPRO | TPWO | TPVOL);
X	if (! wtcmd (t, QIC_REWIND))
X		return;
X	t->flags |= TPSTART | TPREW;
X	wtclock (t);
X}
X
X/* start the `read marker' operation */
Xstatic int wtreadfm (wtinfo_t *t)
X{
X	t->flags &= ~(TPRO | TPWO | TPVOL);
X	if (! wtcmd (t, QIC_READFM)) {
X		wtsense (t, TP_WRP);
X		return (EIO);
X	}
X	t->flags |= TPRMARK | TPRANY;
X	wtclock (t);
X	/* Don't wait for completion here. */
X	return (0);
X}
X
X/* write marker to the tape */
Xstatic int wtwritefm (wtinfo_t *t)
X{
X	tsleep (wtwritefm, WTPRI, "wtwfm", hz);         /* timeout: 1 second */
X	t->flags &= ~(TPRO | TPWO);
X	if (! wtcmd (t, QIC_WRITEFM)) {
X		wtsense (t, 0);
X		return (EIO);
X	}
X	t->flags |= TPWMARK | TPWANY;
X	wtclock (t);
X	return (wtwait (t, 0, "wtwfm"));
X}
X
X/* wait for controller ready or exception */
Xstatic int wtpoll (wtinfo_t *t)
X{
X	int s, NOTREADY = t->BUSY | t->NOEXCEP;
X
X	/* Poll status port, waiting for ready or exception. */
X	do s = inb (t->STATPORT);
X	while ((s & NOTREADY) == NOTREADY);
X	return (s);
X}
X
X/* execute QIC command */
Xstatic int wtcmd (wtinfo_t *t, int cmd)
X{
X	if (! (wtpoll (t) & t->NOEXCEP))                /* wait for ready */
X		return (0);                             /* error */
X	
X	outb (t->CMDPORT, cmd);                         /* output the command */
X
X	outb (t->CTLPORT, t->REQUEST | t->ONLINE);      /* set request */
X	while (inb (t->STATPORT) & t->BUSY)             /* wait for ready */
X		continue;
X	outb (t->CTLPORT, t->IEN | t->ONLINE);          /* reset request */
X	while (! (inb (t->STATPORT) & t->BUSY))         /* wait for not ready */
X		continue;
X
X	return (1);
X}
X
X/* wait for the end of i/o, seeking marker or rewind operation */
Xstatic int wtwait (wtinfo_t *t, int catch, char *msg)
X{
X	int error;
X
X	while (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
X		if (error = tsleep (t, WTPRI | catch, msg, 0))
X			return (error);
X	return (0);
X}
X
X/* initialize dma for the i/o operation */
Xstatic void wtdma (wtinfo_t *t)
X{
X	t->flags |= TPACTIVE;
X	wtclock (t);
X
X	if (t->SDMAPORT)
X		outb (t->SDMAPORT, 0);          /* set dma */
X
X	if ((t->dmaflags & B_READ) && (t->dmatotal - t->dmacount) < BLKSIZE)
X		/* Reading short block.  Do it through the internal buffer. */
X		isa_dmastart (t->dmaflags, t->buf, BLKSIZE, t->chan);
X	else
X		isa_dmastart (t->dmaflags, t->dmavaddr, BLKSIZE, t->chan);
X
X	outb (t->CTLPORT, t->IEN | t->ONLINE);
X}
X
X/* start i/o operation */
Xstatic int wtstart (wtinfo_t *t, unsigned flags, void *vaddr, unsigned len)
X{
X	if (! (wtpoll (t) & t->NOEXCEP)) {      /* wait for ready or error */
X		t->flags |= TPEXCEP;            /* error */
X		return (0);
X	}
X	t->flags &= ~TPEXCEP;                   /* clear exception flag */
X	t->dmavaddr = vaddr;
X	t->dmatotal = len;
X	t->dmacount = 0;
X	t->dmaflags = flags;
X	wtdma (t);
X	return (1);
X}
X
X/* start timer */
Xstatic void wtclock (wtinfo_t *t)
X{
X	if (! (t->flags & TPTIMER)) {
X		t->flags |= TPTIMER;
X		timeout (wtimer, t, hz);
X	}
X}
X
X/*
X * Simulate an interrupt periodically while i/o is going.
X * This is necessary in case interrupts get eaten due to
X * multiple devices on a single IRQ line.
X */
Xstatic void wtimer (wtinfo_t *t)
X{
X	int s;
X
X	t->flags &= ~TPTIMER;
X	if (! (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)))
X		return;
X
X	/* If i/o going, simulate interrupt. */
X	s = splbio ();
X	wtintr (t->unit);
X	splx (s);
X
X	/* Restart timer if i/o pending. */
X	if (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
X		wtclock (t);
X}
X
X/* reset the controller */
Xstatic int wtreset (wtinfo_t *t)
X{
X	outb (t->CTLPORT, t->RESET);            /* send reset */
X	DELAY (25);
X	outb (t->CTLPORT, 0);                   /* turn off reset */
X	if ((inb (t->STATPORT) & t->RESETMASK) != t->RESETVAL)
X		return (0);
X	return (1);
X}
X
X/* get controller status information */
X/* return 0 if user i/o request should receive an i/o error code */
Xstatic int wtsense (wtinfo_t *t, int ignor)
X{
X	char *msg = 0;
X	int err;
X
X	t->flags &= ~(TPRO | TPWO);
X	if (! wtstatus (t))
X		return (0);
X	if (! (t->error.err & TP_ST0))
X		t->error.err &= ~TP_ST0MASK;
X	if (! (t->error.err & TP_ST1))
X		t->error.err &= ~TP_ST1MASK;
X	t->error.err &= ~ignor;         /* ignore certain errors */
X	err = t->error.err & (TP_FIL | TP_BNL | TP_UDA | TP_EOM | TP_WRP |
X		TP_USL | TP_CNI | TP_MBD | TP_NDT | TP_ILL);
X	if (! err)
X		return (1);
X
X	/* lifted from tdriver.c from Wangtek */
X	if      (err & TP_USL)  msg = "Drive not online";
X	else if (err & TP_CNI)  msg = "No cartridge";
X	else if ((err & TP_WRP) && !(t->flags & TPWP)) {
X		msg = "Tape is write protected";
X		t->flags |= TPWP;
X	}
X	else if (err & TP_FIL)  msg = 0 /*"Filemark detected"*/;
X	else if (err & TP_EOM)  msg = 0 /*"End of tape"*/;
X	else if (err & TP_BNL)  msg = "Block not located";
X	else if (err & TP_UDA)  msg = "Unrecoverable data error";
X	else if (err & TP_NDT)  msg = "No data detected";
X	else if (err & TP_ILL)  msg = "Illegal command";
X	if (msg)
X		printf ("wt%d: %s\n", t->unit, msg);
X	return (0);
X}
X
X/* get controller status information */
Xstatic int wtstatus (wtinfo_t *t)
X{
X	char *p;
X
X	wtpoll (t);                     /* wait for ready or exception */
X	outb (t->CMDPORT, QIC_RDSTAT);  /* send `read status' command */
X
X	outb (t->CTLPORT, t->REQUEST | t->ONLINE);      /* set request */
X	while (inb (t->STATPORT) & t->BUSY)             /* wait for ready */
X		continue;
X	outb (t->CTLPORT, t->ONLINE);                   /* reset request */
X	while (! (inb (t->STATPORT) & t->BUSY))         /* wait for not ready */
X		continue;
X
X	p = (char*) &t->error;
X	while (p < (char*)&t->error + 6) {
X		if (! (wtpoll (t) & t->NOEXCEP))        /* wait for ready */
X			return (0);                     /* error */
X
X		*p++ = inb (t->DATAPORT);               /* read status byte */
X
X		outb (t->CTLPORT, t->REQUEST);          /* set request */
X		while (! (inb (t->STATPORT) & t->BUSY)) /* wait for not ready */
X			continue;
X		/* DELAY (50); */                       /* wait 50 usec */
X		outb (t->CTLPORT, 0);                   /* unset request */
X	}
X	return (1);
X}
X#endif /* NWT */
END-of-wt.c
echo x - wtreg.h
sed 's/^X//' >wtreg.h << 'END-of-wtreg.h'
X/*
X * Streamer tape driver for 386bsd.
X * Supports Archive QIC-02 and Wangtek QIC-02/QIC-36 boards.
X *
X * Copyright (C) 1993 by:
X *      Sergey Ryzhkov       <sir@kiae.su>
X *      Serge Vakulenko      <vak@zebub.msk.su>
X *
X * Version 1.0, Tue Aug 10 00:30:24 MSD 1993
X *
X * Placed in the public domain with NO WARRANTIES, not even the implied
X * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
X *
X * Authors grant any other persons or organisations permission to use
X * or modify this software as long as this message is kept with the software,
X * all derivative works or modified versions.
X *
X * This driver is derived from the old 386bsd Wangtek streamer tape driver,
X * made by Robert Baron at CMU, based on Intel sources.
X * Authors thank Robert Baron, CMU and Intel and retain here
X * the original CMU copyright notice.
X */
X
X/*
X * Copyright (c) 1989 Carnegie-Mellon University.
X * All rights reserved.
X *
X * Authors: Robert Baron
X * 
X * Permission to use, copy, modify and distribute this software and
X * its documentation is hereby granted, provided that both the copyright
X * notice and this permission notice appear in all copies of the
X * software, derivative works or modified versions, and any portions
X * thereof, and that both notices appear in supporting documentation.
X * 
X * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 
X * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 
X * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
X * 
X * Carnegie Mellon requests users of this software to return to
X *
X *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
X *  School of Computer Science
X *  Carnegie Mellon University
X *  Pittsburgh PA 15213-3890
X *
X * any improvements or extensions that they make and grant Carnegie the
X * rights to redistribute these changes.
X */
X
X/* ioctl for direct QIC commands */
X#define WTQICMD         _IO('W', 0)
X
X/* QIC-02 commands allowed for WTQICMD */
X#define QIC_ERASE       0x22
X#define QIC_RETENS      0x24
X
X/* internal QIC-02 commands */
X#define QIC_RDDATA      0x80    /* read data */
X#define QIC_READFM      0xa0    /* read file mark */
X#define QIC_WRTDATA     0x40    /* write data */
X#define QIC_WRITEFM     0x60    /* write file mark */
X#define QIC_RDSTAT      0xc0    /* read status command */
X#define QIC_REWIND      0x21    /* rewind command (position+bot) */
X#define QIC_FMT11       0x26    /* set format QIC-11 */
X#define QIC_FMT24       0x27    /* set format QIC-24 */
X#define QIC_FMT120      0x28    /* set format QIC-120 */
X#define QIC_FMT150      0x29    /* set format QIC-150 */
X
X/* tape driver flags */
X#define TPINUSE         0x0001  /* tape is already open */
X#define TPREAD          0x0002  /* tape is only open for reading */
X#define TPWRITE         0x0004  /* tape is only open for writing */
X#define TPSTART         0x0008  /* tape must be rewound and reset */
X#define TPRMARK         0x0010  /* read file mark command outstanding */
X#define TPWMARK         0x0020  /* write file mark command outstanding */
X#define TPREW           0x0040  /* rewind command outstanding */
X#define TPEXCEP         0x0080  /* i/o exception flag */
X#define TPVOL           0x0100  /* read file mark or hit end of tape */
X#define TPWO            0x0200  /* write command outstanding */
X#define TPRO            0x0400  /* read command outstanding */
X#define TPWANY          0x0800  /* write command requested */
X#define TPRANY          0x1000  /* read command requested */
X#define TPWP            0x2000  /* write protect error seen */
X#define TPTIMER         0x4000  /* timer() is active */
X#define TPACTIVE        0x8000  /* dma i/o active */
X
X/* controller error register bits */
X#define TP_FIL          0x0001  /* File mark detected */
X#define TP_BNL          0x0002  /* Block not located */
X#define TP_UDA          0x0004  /* Unrecoverable data error */
X#define TP_EOM          0x0008  /* End of media */
X#define TP_WRP          0x0010  /* Write protected cartridge */
X#define TP_USL          0x0020  /* Unselected drive */
X#define TP_CNI          0x0040  /* Cartridge not in place */
X#define TP_ST0          0x0080  /* Status byte 0 bits */
X#define TP_ST0MASK      0x00ff  /* Status byte 0 mask */
X#define TP_POR          0x0100  /* Power on/reset occurred */
X#define TP_RES1         0x0200  /* Reserved for end of media */
X#define TP_RES2         0x0400  /* Reserved for bus parity */
X#define TP_BOM          0x0800  /* Beginning of media */
X#define	TP_MBD		0x1000	/* Marginal block detected */
X#define	TP_NDT		0x2000	/* No data detected */
X#define TP_ILL          0x4000  /* Illegal command - should not happen! */
X#define	TP_ST1		0x8000	/* Status byte 1 bits */
X#define TP_ST1MASK      0xff00  /* Status byte 1 mask */
X
X/* formats for printing flags and error values */
X#define WTDS_BITS "\16\1inuse\2read\3write\4start\5rmark\6wmark\7rew\10excep\11vol\12wo\13ro\14wany\15rany\16wp\17timer\20active"
X#define WTER_BITS "\16\1eof\2bnl\3uda\4eom\5wrp\6usl\7cni\11por\12res1\13res2\14bom\15mbd\16ndt\17ill"
END-of-wtreg.h
echo x - mt/Makefile
sed 's/^X//' >mt/Makefile << 'END-of-mt/Makefile'
X#	@(#)Makefile	5.4 (Berkeley) 1/14/91
X
XPROG=	mt
XCFLAGS+=-I/sys
X
X.include <bsd.prog.mk>
END-of-mt/Makefile
echo x - mt/README
sed 's/^X//' >mt/README << 'END-of-mt/README'
XThis is the enhanced version of mt(1) command, with
Xinterface to new streamer driver added.
X
XThere are two new (QIC-02) commands:
X
X	erase           - erase the tape
X	retension       - retension the tape
X
XCommand 'mt status' prints brief description of driver flags
Xand tape controller error register.
X
XSerge Vakulenko, <vak@zebub.msk.su>
END-of-mt/README
echo x - mt/mt.1
sed 's/^X//' >mt/mt.1 << 'END-of-mt/mt.1'
X.\" Copyright (c) 1981, 1990 The Regents of the University of California.
X.\" All rights reserved.
X.\"
X.\"
X.\" Redistribution and use in source and binary forms, with or without
X.\" modification, are permitted provided that the following conditions
X.\" are met:
X.\" 1. Redistributions of source code must retain the above copyright
X.\"    notice, this list of conditions and the following disclaimer.
X.\" 2. Redistributions in binary form must reproduce the above copyright
X.\"    notice, this list of conditions and the following disclaimer in the
X.\"    documentation and/or other materials provided with the distribution.
X.\" 3. All advertising materials mentioning features or use of this software
X.\"    must display the following acknowledgement:
X.\"	This product includes software developed by the University of
X.\"	California, Berkeley and its contributors.
X.\" 4. Neither the name of the University nor the names of its contributors
X.\"    may be used to endorse or promote products derived from this software
X.\"    without specific prior written permission.
X.\"
X.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
X.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
X.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
X.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
X.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
X.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
X.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
X.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X.\" SUCH DAMAGE.
X.\"
X.\"	@(#)mt.1	6.7 (Berkeley) 7/28/91
X.\"
X.Dd July 28, 1991
X.Dt MT 1
X.Os BSD 4
X.Sh NAME
X.Nm mt
X.Nd magnetic tape manipulating program
X.Sh SYNOPSIS
X.Nm mt
X.Op Fl f Ar tapename
X.Ar command
X.Op Ar count
X.Sh DESCRIPTION
X.Nm Mt
Xis used to give commands to a magnetic tape drive.
XBy default
X.Nm mt
Xperforms the requested operation once.  Operations
Xmay be performed multiple times by specifying
X.Ar count  .
XNote
Xthat
X.Ar tapename
Xmust reference a raw (not block) tape device.
X.Pp
XThe available commands are listed below.  Only as many
Xcharacters as are required to uniquely identify a command
Xneed be specified.
X.Bl -tag -width "eof, weof"
X.It Cm eof , weof
XWrite
X.Ar count
Xend-of-file marks at the current position on the tape.
X.It Cm fsf
XForward space
X.Ar count
Xfiles.
X.It Cm fsr
XForward space
X.Ar count
Xrecords.
X.It Cm bsf
XBack space
X.Ar count
Xfiles.
X.It Cm bsr
XBack space
X.Ar count
Xrecords.
X.It Cm rewind
XRewind the tape
X(Count is ignored).
X.It Cm offline , rewoffl
XRewind the tape and place the tape unit off-line
X(Count is ignored).
X.It Cm status
XPrint status information about the tape unit.
X.It Cm erase
XErase the tape.
X.It Cm retension
XRetension the tape.
X.El
X.Pp
XIf a tape name is not specified, and the environment variable
X.Ev TAPE
Xdoes not exist;
X.Nm mt
Xuses the device
X.Pa /dev/rmt12 .
X.Pp
X.Nm Mt
Xreturns a 0 exit status when the operation(s) were successful,
X1 if the command was unrecognized, and 2 if an operation failed.
X.Sh ENVIRONMENT
XIf the following environment variable exists, it is utilized by
X.Nm mt .
X.Bl -tag -width Fl
X.It Ev TAPE
X.Nm Mt
Xchecks the
X.Ev TAPE
Xenvironment variable if the
Xargument
X.Ar tapename
Xis not given.
X.Sh FILES
X.Bl -tag -width /dev/rmt* -compact
X.It Pa /dev/rmt*
XRaw magnetic tape interface
X.El
X.Sh SEE ALSO
X.\".Xr mtio 4 ,
X.Xr dd 1 ,
X.Xr ioctl 2 ,
X.Xr environ 7
X.Sh HISTORY
XThe
X.Nm mt
Xcommand appeared in
X.Bx 4.3 .
X.\" mt.1: mtio(4) missing
END-of-mt/mt.1
echo x - mt/mt.c
sed 's/^X//' >mt/mt.c << 'END-of-mt/mt.c'
X/*
X * Copyright (c) 1980 The Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X *    must display the following acknowledgement:
X *	This product includes software developed by the University of
X *	California, Berkeley and its contributors.
X * 4. Neither the name of the University nor the names of its contributors
X *    may be used to endorse or promote products derived from this software
X *    without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X * SUCH DAMAGE.
X */
X
X#ifndef lint
Xchar copyright[] =
X"@(#) Copyright (c) 1980 The Regents of the University of California.\n\
X All rights reserved.\n";
X#endif /* not lint */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)mt.c	5.6 (Berkeley) 6/6/91";
X#endif /* not lint */
X
X/*
X * mt --
X *   magnetic tape manipulation program
X */
X#include <sys/types.h>
X#include <sys/ioctl.h>
X#include <sys/mtio.h>
X#include <fcntl.h>
X#include <stdio.h>
X#include <ctype.h>
X
X#define MTERA           -1
X#define MTRET           -2
X
X#define	equal(s1,s2)	(strcmp(s1, s2) == 0)
X
Xstruct commands {
X	char *c_name;
X	int c_code;
X	int c_ronly;
X} com[] = {
X	{ "weof",	MTWEOF,	0 },
X	{ "eof",	MTWEOF,	0 },
X	{ "fsf",	MTFSF,	1 },
X	{ "bsf",	MTBSF,	1 },
X	{ "fsr",	MTFSR,	1 },
X	{ "bsr",	MTBSR,	1 },
X	{ "rewind",	MTREW,	1 },
X	{ "offline",	MTOFFL,	1 },
X	{ "rewoffl",	MTOFFL,	1 },
X	{ "status",	MTNOP,	1 },
X	{ "erase",      MTERA,  0 },
X	{ "retension",  MTRET,  1 },
X	{ 0 }
X};
X
Xint mtfd;
Xstruct mtop mt_com;
Xstruct mtget mt_status;
Xchar *tape;
X
Xmain(argc, argv)
X	char **argv;
X{
X	char line[80], *getenv();
X	register char *cp;
X	register struct commands *comp;
X
X	if (argc > 2 && (equal(argv[1], "-t") || equal(argv[1], "-f"))) {
X		argc -= 2;
X		tape = argv[2];
X		argv += 2;
X	} else
X		if ((tape = getenv("TAPE")) == NULL)
X			tape = DEFTAPE;
X	if (argc < 2) {
Xusage:          fprintf(stderr, "Usage:\n");
X		fprintf(stderr, "\tmt [ -f device ] command [ count ]\n");
X		fprintf(stderr, "Commands:\n");
X		fprintf(stderr, "\teof, weof - write tape mmrk\n");
X		fprintf(stderr, "\tfsf       - seek forward for tape mark\n");
X		fprintf(stderr, "\tbsf       - seek backward for tape mark\n");
X		fprintf(stderr, "\tfsr       - seek record forward\n");
X		fprintf(stderr, "\tbsr       - seek record backward\n");
X		fprintf(stderr, "\trewind    - rewind the tape\n");
X		fprintf(stderr, "\toffline   - rewind and unload the tape\n");
X		fprintf(stderr, "\tstatus    - get tape status\n");
X		fprintf(stderr, "\terase     - erase the tape\n");
X		fprintf(stderr, "\tretension - retension the tape\n");
X		exit(1);
X	}
X	cp = argv[1];
X	for (comp = com; comp->c_name != NULL; comp++)
X		if (strncmp(cp, comp->c_name, strlen(cp)) == 0)
X			break;
X	if (! comp->c_name)
X		goto usage;
X	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0) {
X		perror(tape);
X		exit(1);
X	}
X	switch (comp->c_code) {
X	case MTERA:
X		command(comp->c_code, "erase");
X		break;
X	case MTRET:
X		command(comp->c_code, "retension");
X		break;
X	case MTNOP:
X		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
X			perror("mt");
X			exit(2);
X		}
X		status(&mt_status);
X		break;
X	default:
X		mt_com.mt_op = comp->c_code;
X		mt_com.mt_count = (argc > 2 ? atoi(argv[2]) : 1);
X		if (mt_com.mt_count < 0) {
X			fprintf(stderr, "mt: negative repeat count\n");
X			exit(1);
X		}
X		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
X			fprintf(stderr, "%s %s %d ", tape, comp->c_name,
X				mt_com.mt_count);
X			perror("failed");
X			exit(2);
X		}
X		break;
X	}
X}
X
X#ifdef vax
X#include <vaxmba/mtreg.h>
X#include <vaxmba/htreg.h>
X
X#include <vaxuba/utreg.h>
X#include <vaxuba/tmreg.h>
X#undef b_repcnt		/* argh */
X#include <vaxuba/tsreg.h>
X#endif
X
X#ifdef sun
X#include <sundev/tmreg.h>
X#include <sundev/arreg.h>
X#endif
X
X#ifdef tahoe
X#include <tahoe/vba/cyreg.h>
X#endif
X
X#ifdef __386BSD__
X#include <sys/../i386/isa/wtreg.h>
X#endif
X
Xstruct tape_desc {
X	short	t_type;		/* type of magtape device */
X	char	*t_name;	/* printing name */
X	char	*t_dsbits;	/* "drive status" register */
X	char	*t_erbits;	/* "error" register */
X} tapes[] = {
X#ifdef vax
X	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
X	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
X	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
X	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
X	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
X#endif
X#ifdef sun
X	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
X	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
X#endif
X#ifdef tahoe
X	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
X#endif
X#ifdef __386BSD__
X#ifdef WTDS_BITS
X	{ MT_ISVIPER1,  "Archive",      WTDS_BITS,      WTER_BITS },
X	{ 0x11,         "Wangtek",      WTDS_BITS,      WTER_BITS },
X#endif
X#endif
X	{ 0 }
X};
X
X/*
X * Start erase or retension operation
X */
X
Xcommand(op, opname)
X	char *opname;
X{
X#ifdef WTQICMD
X	if (ioctl(mtfd, WTQICMD, op==MTERA ? QIC_ERASE : QIC_RETENS) >= 0)
X		return;
X	perror(opname);
X#else
X	fprintf(stderr, "%s %s: operation not supported\n", tape, opname);
X#endif
X}
X
X/*
X * Interpret the status buffer returned
X */
Xstatus(bp)
X	register struct mtget *bp;
X{
X	register struct tape_desc *mt;
X
X	for (mt = tapes; mt->t_type; mt++)
X		if (mt->t_type == bp->mt_type)
X			break;
X	if (mt->t_type == 0) {
X		printf("unknown tape drive type (%d)\n", bp->mt_type);
X		return;
X	}
X	printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
X	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
X	printreg("\ner", bp->mt_erreg, mt->t_erbits);
X	putchar('\n');
X}
X
X/*
X * Print a register a la the %b format of the kernel's printf
X */
Xprintreg(s, v, bits)
X	char *s;
X	register char *bits;
X	register unsigned short v;
X{
X	register int i, any = 0;
X	register char c;
X
X	if (bits && *bits == 8)
X		printf("%s=%o", s, v);
X	else
X		printf("%s=%x", s, v);
X	bits++;
X	if (v && bits) {
X		putchar('<');
X		while (i = *bits++) {
X			if (v & (1 << (i-1))) {
X				if (any)
X					putchar(',');
X				any = 1;
X				for (; (c = *bits) > 32; bits++)
X					putchar(c);
X			} else
X				for (; *bits > 32; bits++)
X					;
X		}
X		putchar('>');
X	}
X}
END-of-mt/mt.c
exit
-- 
Serge Vakulenko <vak@kiae.su>, Kurchatov Inst. of Atomic Energy
Phone office (095) 196 9006, 196 7212