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