xref: /netbsd/sys/arch/atari/dev/md_root.c (revision c4a72b64)
1 /*	NetBSD: md_root.c,v 1.17 2002/05/23 14:59:28 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Leo Weppelman.
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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Leo Weppelman.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/buf.h>
38 #include <sys/proc.h>
39 #include <sys/device.h>
40 #include <sys/ioctl.h>
41 #include <sys/fcntl.h>
42 #include <sys/conf.h>
43 #include <sys/disklabel.h>
44 #include <sys/disk.h>
45 #include <sys/dkbad.h>
46 
47 #include <dev/cons.h>
48 #include <dev/md.h>
49 
50 #include <atari/atari/device.h>
51 
52 /*
53  * Misc. defines:
54  */
55 #define	RAMD_CHUNK	(9 * 512)	/* Chunk-size for auto-load	*/
56 #define	RAMD_NDEV	3		/* Number of devices configured	*/
57 
58 struct   ramd_info {
59 	u_long	ramd_size;  /* Size of disk in bytes			*/
60 	u_long	ramd_flag;  /* see defs below				*/
61 	dev_t	ramd_dev;   /* device to load from			*/
62 };
63 
64 /*
65  * ramd_flag:
66  */
67 #define	RAMD_LOAD	0x01	/* Auto load when first opened	*/
68 #define	RAMD_LCOMP	0x02	/* Input is compressed		*/
69 
70 struct ramd_info rd_info[RAMD_NDEV] = {
71     {
72 	1105920,		/*	1Mb in 2160 sectors		*/
73 	RAMD_LOAD,		/* auto-load this device		*/
74 	MAKEDISKDEV(2, 0, 2),	/* XXX: This is crap! (720Kb flop)	*/
75     },
76     {
77 	1474560,		/* 1.44Mb in 2880 sectors		*/
78 	RAMD_LOAD,		/* auto-load this device		*/
79 	MAKEDISKDEV(2, 0, 2),	/* XXX: This is crap! (720Kb flop)	*/
80     },
81     {
82 	1474560,		/* 1.44Mb in 2880 sectors		*/
83 	RAMD_LOAD,		/* auto-load this device		*/
84 	MAKEDISKDEV(2, 0, 3),	/* XXX: This is crap! (1.44Mb flop)	*/
85     }
86 };
87 
88 struct read_info {
89     struct buf	*bp;		/* buffer for strategy function		*/
90     long	nbytes;		/* total number of bytes to read	*/
91     long	offset;		/* offset in input medium		*/
92     caddr_t	bufp;		/* current output buffer		*/
93     caddr_t	ebufp;		/* absolute maximum for bufp		*/
94     int		chunk;		/* chunk size on input medium		*/
95     int		media_sz;	/* size of input medium			*/
96     void	(*strat)(struct buf *);	/* strategy function for read	*/
97 };
98 
99 
100 static int  loaddisk __P((struct  md_conf *, dev_t ld_dev, struct proc *));
101 static int  ramd_norm_read __P((struct read_info *));
102 
103 #ifdef support_compression
104 static int  cpy_uncompressed __P((caddr_t, int, struct read_info *));
105 static int  md_compressed __P((caddr_t, int, struct read_info *));
106 #endif
107 
108 /*
109  * This is called during autoconfig.
110  */
111 void
112 md_attach_hook(unit, md)
113 int		unit;
114 struct md_conf	*md;
115 {
116 	if (atari_realconfig && (unit < RAMD_NDEV) && rd_info[unit].ramd_flag) {
117 		printf ("md%d: %sauto-load on open. Size %ld bytes.\n", unit,
118 		    rd_info[unit].ramd_flag & RAMD_LCOMP ? "decompress/" : "",
119 		    rd_info[unit].ramd_size);
120 		md->md_type = MD_UNCONFIGURED; /* Paranoia... */
121 	}
122 }
123 
124 void
125 md_open_hook(unit, md)
126 int		unit;
127 struct md_conf	*md;
128 {
129 	struct ramd_info *ri;
130 
131 	if(unit >= RAMD_NDEV)
132 		return;
133 
134 	ri = &rd_info[unit];
135 	if (md->md_type != MD_UNCONFIGURED)
136 		return;	/* Only configure once */
137 	md->md_addr = malloc(ri->ramd_size, M_DEVBUF, M_WAITOK);
138 	md->md_size = ri->ramd_size;
139 	if(md->md_addr == NULL)
140 		return;
141 	if(ri->ramd_flag & RAMD_LOAD) {
142 		if (loaddisk(md, ri->ramd_dev, curproc)) {
143 			free(md->md_addr, M_DEVBUF);
144 			md->md_addr = NULL;
145 			return;
146 		}
147 	}
148 	md->md_type = MD_KMEM_ALLOCATED;
149 }
150 
151 static int
152 loaddisk(md, ld_dev, proc)
153 struct md_conf		*md;
154 dev_t			ld_dev;
155 struct proc		*proc;
156 {
157 	struct buf		buf;
158 	int			error;
159 	const struct bdevsw	*bdp;
160 	struct disklabel	dl;
161 	struct read_info	rs;
162 
163 	bdp = bdevsw_lookup(ld_dev);
164 	if (bdp == NULL)
165 		return (ENXIO);
166 
167 	/*
168 	 * Initialize our buffer header:
169 	 */
170 	memset(&buf, 0, sizeof(buf));
171 	buf.b_vnbufs.le_next = NOLIST;
172 	buf.b_flags = B_BUSY;
173 	buf.b_dev   = ld_dev;
174 	buf.b_error = 0;
175 	buf.b_proc  = proc;
176 
177 	/*
178 	 * Setup read_info:
179 	 */
180 	rs.bp       = &buf;
181 	rs.nbytes   = md->md_size;
182 	rs.offset   = 0;
183 	rs.bufp     = md->md_addr;
184 	rs.ebufp    = md->md_addr + md->md_size;
185 	rs.chunk    = RAMD_CHUNK;
186 	rs.media_sz = md->md_size;
187 	rs.strat    = bdp->d_strategy;
188 
189 	/*
190 	 * Open device and try to get some statistics.
191 	 */
192 	if((error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, proc)) != 0)
193 		return(error);
194 	if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (caddr_t)&dl, FREAD, proc) == 0) {
195 		/* Read on a cylinder basis */
196 		rs.chunk    = dl.d_secsize * dl.d_secpercyl;
197 		rs.media_sz = dl.d_secperunit * dl.d_secsize;
198 	}
199 
200 #ifdef support_compression
201 	if(ri->ramd_flag & RAMD_LCOMP)
202 		error = decompress(cpy_uncompressed, md_compressed, &rs);
203 	else
204 #endif /* support_compression */
205 		error = ramd_norm_read(&rs);
206 
207 	bdp->d_close(ld_dev,FREAD | FNONBLOCK, 0, proc);
208 	return(error);
209 }
210 
211 static int
212 ramd_norm_read(rsp)
213 struct read_info	*rsp;
214 {
215 	long		bytes_left;
216 	int		done, error;
217 	struct buf	*bp;
218 	int		s;
219 	int		dotc = 0;
220 
221 	bytes_left = rsp->nbytes;
222 	bp         = rsp->bp;
223 	error      = 0;
224 
225 	while(bytes_left > 0) {
226 		s = splbio();
227 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
228 		splx(s);
229 		bp->b_blkno  = btodb(rsp->offset);
230 		bp->b_bcount = rsp->chunk;
231 		bp->b_data   = rsp->bufp;
232 
233 		/* Initiate read */
234 		(*rsp->strat)(bp);
235 
236 		/* Wait for results	*/
237 		s = splbio();
238 		while ((bp->b_flags & B_DONE) == 0)
239 			tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
240 		if (bp->b_flags & B_ERROR)
241 			error = (bp->b_error ? bp->b_error : EIO);
242 		splx(s);
243 
244 		/* Dot counter */
245 		printf(".");
246 		if(!(++dotc % 40))
247 			printf("\n");
248 
249 		done = bp->b_bcount - bp->b_resid;
250 		bytes_left   -= done;
251 		rsp->offset  += done;
252 		rsp->bufp    += done;
253 
254 		if(error || !done)
255 			break;
256 
257 		if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) {
258 			printf("\nInsert next media and hit any key...");
259 			cngetc();
260 			printf("\n");
261 			rsp->offset = 0;
262 		}
263 	}
264 	printf("\n");
265 	return(error);
266 }
267 
268 #ifdef support_compression
269 /*
270  * Functions supporting uncompression:
271  */
272 /*
273  * Copy from the uncompression buffer to the ramdisk
274  */
275 static int
276 cpy_uncompressed(buf, nbyte, rsp)
277 caddr_t			buf;
278 struct read_info	*rsp;
279 int			nbyte;
280 {
281 	if((rsp->bufp + nbyte) >= rsp->ebufp)
282 		return(0);
283 	bcopy(buf, rsp->bufp, nbyte);
284 	rsp->bufp += nbyte;
285 	return(0);
286 }
287 
288 /*
289  * Read a maximum of 'nbyte' bytes into 'buf'.
290  */
291 static int
292 md_compressed(buf, nbyte, rsp)
293 caddr_t			buf;
294 struct read_info	*rsp;
295 int			nbyte;
296 {
297 	static int	dotc = 0;
298 	struct buf	*bp;
299 	       int	nread = 0;
300 	       int	s;
301 	       int	done, error;
302 
303 
304 	error  = 0;
305 	bp     = rsp->bp;
306 	nbyte &= ~(DEV_BSIZE - 1);
307 
308 	while(nbyte > 0) {
309 		s = splbio();
310 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
311 		splx(s);
312 		bp->b_blkno  = btodb(rsp->offset);
313 		bp->b_bcount = min(rsp->chunk, nbyte);
314 		bp->b_data   = buf;
315 
316 		/* Initiate read */
317 		(*rsp->strat)(bp);
318 
319 		/* Wait for results	*/
320 		s = splbio();
321 		while ((bp->b_flags & B_DONE) == 0)
322 			tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
323 		if (bp->b_flags & B_ERROR)
324 			error = (bp->b_error ? bp->b_error : EIO);
325 		splx(s);
326 
327 		/* Dot counter */
328 		printf(".");
329 		if(!(++dotc % 40))
330 			printf("\n");
331 
332 		done = bp->b_bcount - bp->b_resid;
333 		nbyte        -= done;
334 		nread        += done;
335 		rsp->offset  += done;
336 
337 		if(error || !done)
338 			break;
339 
340 		if((rsp->offset == rsp->media_sz) && (nbyte != 0)) {
341 		if(rsp->offset == rsp->media_sz) {
342 			printf("\nInsert next media and hit any key...");
343 			if(cngetc() != '\n')
344 				printf("\n");
345 			rsp->offset = 0;
346 		}
347 	}
348 	s = splbio();
349 	splx(s);
350 	return(nread);
351 }
352 #endif /* support_compression */
353