Home | History | Annotate | Line # | Download | only in libsa
      1  1.9    isaki /*	$NetBSD: fd.c,v 1.9 2024/01/07 07:58:34 isaki 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.5    isaki #include "libx68k.h"
     33  1.1  minoura #include "fdvar.h"
     34  1.1  minoura #include "iocs.h"
     35  1.2  minoura 
     36  1.5    isaki /* fdopen(struct open_file *f, int id, int part) */
     37  1.1  minoura int
     38  1.5    isaki fdopen(struct open_file *f, ...)
     39  1.1  minoura {
     40  1.1  minoura 	int error;
     41  1.1  minoura 	struct fd_softc *sc;
     42  1.1  minoura 	struct fdfmt fdfmt;
     43  1.8    isaki 	int id;
     44  1.8    isaki 	int part __unused;
     45  1.5    isaki 	va_list ap;
     46  1.5    isaki 
     47  1.5    isaki 	va_start(ap, f);
     48  1.5    isaki 	id   = va_arg(ap, int);
     49  1.5    isaki 	part = va_arg(ap, int);
     50  1.5    isaki 	va_end(ap);
     51  1.1  minoura 
     52  1.1  minoura 	if (id < 0 || id > 3)
     53  1.1  minoura 		return ENXIO;
     54  1.1  minoura 	sc = alloc(sizeof (struct fd_softc));
     55  1.1  minoura 
     56  1.1  minoura 	/* lock the medium */
     57  1.1  minoura 	error = IOCS_B_DRVCHK((0x90 + id) << 8, 2);
     58  1.1  minoura 	if ((error & 2) == 0)
     59  1.1  minoura 		return ENXIO;
     60  1.1  minoura 
     61  1.1  minoura 	/* detect the sector size */
     62  1.1  minoura 	error = IOCS_B_RECALI((0x90 + id) << 8);
     63  1.4    isaki 	error = fd_check_format(id, 0, &sc->fmt);
     64  1.1  minoura 	if (error < 0) {
     65  1.1  minoura 		IOCS_B_DRVCHK((0x90 + id) << 8, 3); /* unlock */
     66  1.4    isaki 		dealloc(sc, sizeof(struct fd_softc));
     67  1.1  minoura 		return -error;
     68  1.1  minoura 	}
     69  1.1  minoura 
     70  1.1  minoura 	/* check the second side */
     71  1.4    isaki 	error = fd_check_format(id, 1, &fdfmt);
     72  1.1  minoura 	if (error == 0)		/* valid second side; set the #heads */
     73  1.1  minoura 		sc->fmt.maxsec.H = fdfmt.maxsec.H;
     74  1.1  minoura 
     75  1.1  minoura 	sc->unit = id;
     76  1.1  minoura 	f->f_devdata = sc;
     77  1.1  minoura 
     78  1.1  minoura 	return 0;
     79  1.1  minoura }
     80  1.1  minoura 
     81  1.1  minoura int
     82  1.4    isaki fdclose(struct open_file *f)
     83  1.1  minoura {
     84  1.1  minoura 	struct fd_softc *sc = f->f_devdata;
     85  1.1  minoura 
     86  1.1  minoura 	IOCS_B_DRVCHK((0x90 + sc->unit) << 8, 3);
     87  1.4    isaki 	dealloc(sc, sizeof(struct fd_softc));
     88  1.1  minoura 	return 0;
     89  1.1  minoura }
     90  1.1  minoura 
     91  1.1  minoura int
     92  1.4    isaki fdstrategy(void *arg, int rw, daddr_t dblk, size_t size,
     93  1.4    isaki            void *buf, size_t *rsize)
     94  1.1  minoura {
     95  1.1  minoura 	struct fd_softc *sc = arg;
     96  1.1  minoura 	int cyl, head, sect;
     97  1.1  minoura 	int nhead, nsect;
     98  1.1  minoura 	int error, nbytes;
     99  1.1  minoura 
    100  1.1  minoura 	if (size == 0) {
    101  1.1  minoura 		if (rsize)
    102  1.1  minoura 			*rsize = 0;
    103  1.1  minoura 		return 0;
    104  1.1  minoura 	}
    105  1.4    isaki 	nbytes = howmany(size, 128 << sc->fmt.minsec.N)
    106  1.1  minoura 		* (128 << sc->fmt.minsec.N);
    107  1.1  minoura 
    108  1.1  minoura 	nhead = sc->fmt.maxsec.H - sc->fmt.minsec.H + 1;
    109  1.1  minoura 	nsect = sc->fmt.maxsec.R - sc->fmt.minsec.R + 1;
    110  1.1  minoura 
    111  1.9    isaki 	sect =  dblk % nsect + sc->fmt.minsec.R;
    112  1.1  minoura 	head = (dblk / nsect) % nhead + sc->fmt.minsec.H;
    113  1.1  minoura 	cyl = (dblk / nsect) / nhead + sc->fmt.minsec.C;
    114  1.1  minoura 
    115  1.6  tsutsui 	error = IOCS_B_READ((sc->unit + 0x90) * 256 + 0x70,
    116  1.1  minoura 			    ((sc->fmt.minsec.N << 24) |
    117  1.1  minoura 			     (cyl << 16) |
    118  1.1  minoura 			     (head << 8) |
    119  1.1  minoura 			     (sect)),
    120  1.1  minoura 			    nbytes,
    121  1.1  minoura 			    buf);
    122  1.1  minoura 	if (error & 0xf8ffff00) {
    123  1.1  minoura 		nbytes = 0;
    124  1.1  minoura 		error = EIO;
    125  1.4    isaki 	} else {
    126  1.1  minoura 		error = 0;
    127  1.4    isaki 	}
    128  1.1  minoura 
    129  1.1  minoura 	if (rsize)
    130  1.1  minoura 		*rsize = nbytes;
    131  1.1  minoura 	return error;
    132  1.1  minoura }
    133