Home | History | Annotate | Line # | Download | only in libsa
fd.c revision 1.1
      1  1.1  minoura /*	$NetBSD: fd.c,v 1.1 2001/09/27 10:03:28 minoura Exp $	*/
      2  1.1  minoura 
      3  1.1  minoura /*
      4  1.1  minoura  * Copyright (c) 2001 MINOURA Makoto.
      5  1.1  minoura  * All rights reserved.
      6  1.1  minoura  *
      7  1.1  minoura  * Redistribution and use in source and binary forms, with or without
      8  1.1  minoura  * modification, are permitted provided that the following conditions
      9  1.1  minoura  * are met:
     10  1.1  minoura  * 1. Redistributions of source code must retain the above copyright
     11  1.1  minoura  *    notice, this list of conditions and the following disclaimer.
     12  1.1  minoura  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  minoura  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  minoura  *    documentation and/or other materials provided with the distribution.
     15  1.1  minoura  *
     16  1.1  minoura  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  minoura  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  minoura  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  minoura  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  minoura  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  minoura  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  minoura  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  minoura  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  minoura  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  minoura  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  minoura  */
     27  1.1  minoura 
     28  1.1  minoura #include <sys/param.h>
     29  1.1  minoura #include <sys/disklabel.h>
     30  1.1  minoura #include <lib/libsa/stand.h>
     31  1.1  minoura 
     32  1.1  minoura #include "fdvar.h"
     33  1.1  minoura #include "iocs.h"
     34  1.1  minoura 
     35  1.1  minoura 
     36  1.1  minoura int
     37  1.1  minoura fdopen (struct open_file *f, int id, int part)
     38  1.1  minoura {
     39  1.1  minoura 	int error;
     40  1.1  minoura 	struct fd_softc *sc;
     41  1.1  minoura 	struct fdfmt fdfmt;
     42  1.1  minoura 
     43  1.1  minoura 	if (id < 0 || id > 3)
     44  1.1  minoura 		return ENXIO;
     45  1.1  minoura 	sc = alloc(sizeof (struct fd_softc));
     46  1.1  minoura 
     47  1.1  minoura 	/* lock the medium */
     48  1.1  minoura 	error = IOCS_B_DRVCHK((0x90 + id) << 8, 2);
     49  1.1  minoura 	if ((error & 2) == 0)
     50  1.1  minoura 		return ENXIO;
     51  1.1  minoura 
     52  1.1  minoura 	/* detect the sector size */
     53  1.1  minoura 	error = IOCS_B_RECALI((0x90 + id) << 8);
     54  1.1  minoura 	error = fd_check_format (id, 0, &sc->fmt);
     55  1.1  minoura 	if (error < 0) {
     56  1.1  minoura 		IOCS_B_DRVCHK((0x90 + id) << 8, 3); /* unlock */
     57  1.1  minoura 		free(sc, sizeof (struct fd_softc));
     58  1.1  minoura 		return -error;
     59  1.1  minoura 	}
     60  1.1  minoura 
     61  1.1  minoura 	/* check the second side */
     62  1.1  minoura 	error = fd_check_format (id, 1, &fdfmt);
     63  1.1  minoura 	if (error == 0)		/* valid second side; set the #heads */
     64  1.1  minoura 		sc->fmt.maxsec.H = fdfmt.maxsec.H;
     65  1.1  minoura 
     66  1.1  minoura 	sc->unit = id;
     67  1.1  minoura 	f->f_devdata = sc;
     68  1.1  minoura 
     69  1.1  minoura 	return 0;
     70  1.1  minoura }
     71  1.1  minoura 
     72  1.1  minoura int
     73  1.1  minoura fdclose (struct open_file *f)
     74  1.1  minoura {
     75  1.1  minoura 	struct fd_softc *sc = f->f_devdata;
     76  1.1  minoura 
     77  1.1  minoura 	IOCS_B_DRVCHK((0x90 + sc->unit) << 8, 3);
     78  1.1  minoura 	free (sc, sizeof (struct fd_softc));
     79  1.1  minoura 	return 0;
     80  1.1  minoura }
     81  1.1  minoura 
     82  1.1  minoura int
     83  1.1  minoura fdstrategy (void *arg, int rw, daddr_t dblk, size_t size,
     84  1.1  minoura 	    void *buf, size_t *rsize)
     85  1.1  minoura {
     86  1.1  minoura 	struct fd_softc *sc = arg;
     87  1.1  minoura 	int cyl, head, sect;
     88  1.1  minoura 	int nhead, nsect;
     89  1.1  minoura 	int error, nbytes;
     90  1.1  minoura 
     91  1.1  minoura 	if (size == 0) {
     92  1.1  minoura 		if (rsize)
     93  1.1  minoura 			*rsize = 0;
     94  1.1  minoura 		return 0;
     95  1.1  minoura 	}
     96  1.1  minoura 	nbytes = howmany (size, 128 << sc->fmt.minsec.N)
     97  1.1  minoura 		* (128 << sc->fmt.minsec.N);
     98  1.1  minoura 
     99  1.1  minoura 	nhead = sc->fmt.maxsec.H - sc->fmt.minsec.H + 1;
    100  1.1  minoura 	nsect = sc->fmt.maxsec.R - sc->fmt.minsec.R + 1;
    101  1.1  minoura 
    102  1.1  minoura 	sect = 	dblk % nsect + sc->fmt.minsec.R;
    103  1.1  minoura 	head = (dblk / nsect) % nhead + sc->fmt.minsec.H;
    104  1.1  minoura 	cyl = (dblk / nsect) / nhead + sc->fmt.minsec.C;
    105  1.1  minoura 
    106  1.1  minoura 	error = IOCS_B_READ((sc->unit+0x90)*256 + 0x70,
    107  1.1  minoura 			    ((sc->fmt.minsec.N << 24) |
    108  1.1  minoura 			     (cyl << 16) |
    109  1.1  minoura 			     (head << 8) |
    110  1.1  minoura 			     (sect)),
    111  1.1  minoura 			    nbytes,
    112  1.1  minoura 			    buf);
    113  1.1  minoura 	if (error & 0xf8ffff00) {
    114  1.1  minoura 		nbytes = 0;
    115  1.1  minoura 		error = EIO;
    116  1.1  minoura 	} else
    117  1.1  minoura 		error = 0;
    118  1.1  minoura 
    119  1.1  minoura 	if (rsize)
    120  1.1  minoura 		*rsize = nbytes;
    121  1.1  minoura 	return error;
    122  1.1  minoura }
    123