static int mtclose(dev_t dev, int flag, int fmt, struct lwp *l) { struct mt_softc *sc = device_lookup_private(&mt_cd,UNIT(dev)); if (sc->sc_flags & MTF_WRT) { (void) mtcommand(dev, MTWEOF, 2); (void) mtcommand(dev, MTBSF, 0); } if ((minor(dev) & T_NOREWIND) == 0) (void) mtcommand(dev, MTREW, 0); sc->sc_flags &= ~MTF_OPEN; tprintf_close(sc->sc_ttyp); return 0; }
int mtopen(dev_t dev, int flag, int mode, struct lwp *l) { struct mt_softc *sc; int req_den; int error; sc = device_lookup_private(&mt_cd, MTUNIT(dev)); if (sc == NULL || (sc->sc_flags & MTF_EXISTS) == 0) return (ENXIO); if (sc->sc_flags & MTF_OPEN) return (EBUSY); DPRINTF(MDB_ANY, ("%s open: flags 0x%x", device_xname(sc->sc_dev), sc->sc_flags)); sc->sc_flags |= MTF_OPEN; sc->sc_ttyp = tprintf_open(l->l_proc); if ((sc->sc_flags & MTF_ALIVE) == 0) { error = mtcommand(dev, MTRESET, 0); if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0) goto errout; if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE) (void) mtcommand(dev, MTREW, 0); } for (;;) { if ((error = mtcommand(dev, MTNOP, 0)) != 0) goto errout; if (!(sc->sc_flags & MTF_REW)) break; error = kpause("mt", true, hz, NULL); if (error != 0 && error != EWOULDBLOCK) { error = EINTR; goto errout; } } if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) { error = EROFS; goto errout; } if (!(sc->sc_stat1 & SR1_ONLINE)) { uprintf("%s: not online\n", device_xname(sc->sc_dev)); error = EIO; goto errout; } /* * Select density: * - find out what density the drive is set to * (i.e. the density of the current tape) * - if we are going to write * - if we're not at the beginning of the tape * - complain if we want to change densities * - otherwise, select the mtcommand to set the density * * If the drive doesn't support it then don't change the recorded * density. * * The original MOREbsd code had these additional conditions * for the mid-tape change * * req_den != T_BADBPI && * sc->sc_density != T_6250BPI * * which suggests that it would be possible to write multiple * densities if req_den == T_BAD_BPI or the current tape * density was 6250. Testing of our 7980 suggests that the * device cannot change densities mid-tape. * * [email protected] */ sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : ( (sc->sc_stat3 & SR3_1600) ? T_1600BPI : ( (sc->sc_stat3 & SR3_800) ? T_800BPI : -1)); req_den = (dev & T_DENSEL); if (flag & FWRITE) { if (!(sc->sc_stat1 & SR1_BOT)) { if (sc->sc_density != req_den) { uprintf("%s: can't change density mid-tape\n", device_xname(sc->sc_dev)); error = EIO; goto errout; } } else { int mtset_density = (req_den == T_800BPI ? MTSET800BPI : ( req_den == T_1600BPI ? MTSET1600BPI : ( req_den == T_6250BPI ? MTSET6250BPI : ( sc->sc_type == MT7980ID ? MTSET6250DC : MTSET6250BPI)))); if (mtcommand(dev, mtset_density, 0) == 0) sc->sc_density = req_den; } } return (0); errout: sc->sc_flags &= ~MTF_OPEN; return (error); }