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