fd.c revision 1.44 1 /* $NetBSD: fd.c,v 1.44 2000/03/16 16:37:20 kleink Exp $ */
2
3 /*
4 * Copyright (c) 1994 Christian E. Hopps
5 * Copyright (c) 1996 Ezra Story
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christian E. Hopps.
19 * This product includes software developed by Ezra Story.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/buf.h>
39 #include <sys/device.h>
40 #include <sys/ioctl.h>
41 #include <sys/fcntl.h>
42 #include <sys/disklabel.h>
43 #include <sys/disk.h>
44 #include <sys/dkbad.h>
45 #include <sys/proc.h>
46 #include <machine/cpu.h>
47 #include <amiga/amiga/device.h>
48 #include <amiga/amiga/custom.h>
49 #include <amiga/amiga/cia.h>
50 #include <amiga/amiga/cc.h>
51
52 #include <sys/conf.h>
53 #include <machine/conf.h>
54
55 #include "locators.h"
56
57 enum fdc_bits { FDB_CHANGED = 2, FDB_PROTECT, FDB_CYLZERO, FDB_READY };
58 /*
59 * partitions in fd represent different format floppies
60 * partition a is 0 etc..
61 */
62 enum fd_parttypes {
63 FDAMIGAPART = 0,
64 FDMSDOSPART,
65 FDMAXPARTS
66 };
67
68 #define FDBBSIZE (8192)
69 #define FDSBSIZE (8192)
70
71 #define FDUNIT(dev) DISKUNIT(dev)
72 #define FDPART(dev) DISKPART(dev)
73 #define FDMAKEDEV(m, u, p) MAKEDISKDEV((m), (u), (p))
74
75 /* that's nice, but we don't want to always use this as an amiga drive
76 bunghole :-) */
77 #define FDNHEADS (2) /* amiga drives always have 2 heads */
78 #define FDSECSIZE (512) /* amiga drives always have 512 byte sectors */
79 #define FDSECLWORDS (128)
80
81 #define FDSETTLEDELAY (18000) /* usec delay after seeking after switch dir */
82 #define FDSTEPDELAY (3500) /* usec delay after steping */
83 #define FDPRESIDEDELAY (1000) /* usec delay before writing can occur */
84 #define FDWRITEDELAY (1300) /* usec delay after write */
85
86 #define FDSTEPOUT (1) /* decrease track step */
87 #define FDSTEPIN (0) /* increase track step */
88
89 #define FDCUNITMASK (0x78) /* mask for all units (bits 6-3) */
90
91 #define FDRETRIES (2) /* default number of retries */
92 #define FDMAXUNITS (4) /* maximum number of supported units */
93
94 #define DISKLEN_READ (0) /* fake mask for reading */
95 #define DISKLEN_WRITE (1 << 14) /* bit for writing */
96 #define DISKLEN_DMAEN (1 << 15) /* dma go */
97 #define DMABUFSZ ((DISKLEN_WRITE - 1) * 2) /* largest dma possible */
98
99 #define FDMFMSYNC (0x4489)
100 #define FDMFMID (0x5554)
101 #define FDMFMDATA (0x5545)
102 #define FDMFMGAP1 (0x9254)
103 #define FDMFMGAP2 (0xAAAA)
104 #define FDMFMGAP3 (0x9254)
105 #define CRC16POLY (0x1021) /* (x^16) + x^12 + x^5 + x^0 */
106
107 /*
108 * Msdos-type MFM encode/decode
109 */
110 static u_char msdecode[128];
111 static u_char msencode[16] =
112 {
113 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
114 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
115 };
116 static u_short mscrctab[256];
117
118 /*
119 5554 aaaa aaaa aaa5 2aa4 4452 aa51
120 00 00 03 02 ac 0d
121 */
122
123 /*
124 * floppy device type
125 */
126 struct fdtype {
127 u_int driveid; /* drive identification (from drive) */
128 u_int ncylinders; /* number of cylinders on drive */
129 u_int amiga_nsectors; /* number of sectors per amiga track */
130 u_int msdos_nsectors; /* number of sectors per msdos track */
131 u_int nreadw; /* number of words (short) read per track */
132 u_int nwritew; /* number of words (short) written per track */
133 u_int gap; /* track gap size in long words */
134 u_int precomp[2]; /* 1st and 2nd precomp values */
135 char *desc; /* description of drive type (useq) */
136 };
137
138 /*
139 * floppy disk device data
140 */
141 struct fd_softc {
142 struct device sc_dv; /* generic device info; must come first */
143 struct disk dkdev; /* generic disk info */
144 struct buf_queue bufq; /* queue pending I/O operations */
145 struct buf curbuf; /* state of current I/O operation */
146 struct fdtype *type;
147 void *cachep; /* cached track data (write through) */
148 int cachetrk; /* cahced track -1 for none */
149 int hwunit; /* unit for amiga controlling hw */
150 int unitmask; /* mask for cia select deslect */
151 int pstepdir; /* previous step direction */
152 int curcyl; /* current curcyl head positioned on */
153 int flags; /* misc flags */
154 int wlabel;
155 int stepdelay; /* useq to delay after seek user setable */
156 int nsectors; /* number of sectors per track */
157 int openpart; /* which partition [ab] == [12] is open */
158 short retries; /* number of times to retry failed io */
159 short retried; /* number of times current io retried */
160 int bytespersec; /* number of bytes per sector */
161 };
162
163 /* fd_softc->flags */
164 #define FDF_MOTORON (0x01) /* motor is running */
165 #define FDF_MOTOROFF (0x02) /* motor is waiting to be turned off */
166 #define FDF_WMOTOROFF (0x04) /* unit wants a wakeup after off */
167 #define FDF_DIRTY (0x08) /* track cache needs write */
168 #define FDF_WRITEWAIT (0x10) /* need to head select delay on next setpos */
169 #define FDF_HAVELABEL (0x20) /* label is valid */
170 #define FDF_JUSTFLUSH (0x40) /* don't bother caching track. */
171 #define FDF_NOTRACK0 (0x80) /* was not able to recalibrate drive */
172
173 int fdc_wantwakeup;
174 int fdc_side;
175 void *fdc_dmap;
176 struct fd_softc *fdc_indma;
177 int fdc_dmalen;
178 int fdc_dmawrite;
179
180 struct fdcargs {
181 struct fdtype *type;
182 int unit;
183 };
184
185 int fdcmatch __P((struct device *, struct cfdata *, void *));
186 void fdcattach __P((struct device *, struct device *, void *));
187 int fdcprint __P((void *, const char *));
188 int fdmatch __P((struct device *, struct cfdata *, void *));
189 void fdattach __P((struct device *, struct device *, void *));
190
191 void fdintr __P((int));
192 void fdidxintr __P((void));
193 void fdstrategy __P((struct buf *));
194 int fdloaddisk __P((struct fd_softc *));
195 void fdgetdefaultlabel __P((struct fd_softc *, struct disklabel *, int));
196 int fdgetdisklabel __P((struct fd_softc *, dev_t));
197 int fdsetdisklabel __P((struct fd_softc *, struct disklabel *));
198 int fdputdisklabel __P((struct fd_softc *, dev_t));
199 struct fdtype * fdcgetfdtype __P((int));
200 void fdmotoroff __P((void *));
201 void fdsetpos __P((struct fd_softc *, int, int));
202 void fdselunit __P((struct fd_softc *));
203 void fdstart __P((struct fd_softc *));
204 void fdcont __P((struct fd_softc *));
205 void fddmastart __P((struct fd_softc *, int));
206 void fdcalibrate __P((void *));
207 void fddmadone __P((struct fd_softc *, int));
208 void fddone __P((struct fd_softc *));
209 void fdfindwork __P((int));
210 void fdminphys __P((struct buf *));
211 void fdcachetoraw __P((struct fd_softc *));
212 void amcachetoraw __P((struct fd_softc *));
213 int amrawtocache __P((struct fd_softc *));
214 u_long *fdfindsync __P((u_long *, u_long *));
215 int fdrawtocache __P((struct fd_softc *));
216 void mscachetoraw __P((struct fd_softc *));
217 int msrawtocache __P((struct fd_softc *));
218 u_long *mfmblkencode __P((u_long *, u_long *, u_long *, int));
219 u_long *mfmblkdecode __P((u_long *, u_long *, u_long *, int));
220 u_short *msblkdecode __P((u_short *, u_char *, int));
221 u_short *msblkencode __P((u_short *, u_char *, int, u_short *));
222
223 struct dkdriver fddkdriver = { fdstrategy };
224
225 /*
226 * read size is (nsectors + 1) * mfm secsize + gap bytes + 2 shorts
227 * write size is nsectors * mfm secsize + gap bytes + 3 shorts
228 * the extra shorts are to deal with a dma hw bug in the controller
229 * they are probably too much (I belive the bug is 1 short on write and
230 * 3 bits on read) but there is no need to be cheap here.
231 */
232 #define MAXTRKSZ (22 * FDSECSIZE)
233 struct fdtype fdtype[] = {
234 { 0x00000000, 80, 11, 9, 7358, 6815, 414, { 80, 161 }, "3.5dd" },
235 { 0x55555555, 40, 11, 9, 7358, 6815, 414, { 80, 161 }, "5.25dd" },
236 { 0xAAAAAAAA, 80, 22, 18, 14716, 13630, 828, { 80, 161 }, "3.5hd" }
237 };
238 int nfdtype = sizeof(fdtype) / sizeof(*fdtype);
239
240 struct cfattach fd_ca = {
241 sizeof(struct fd_softc), fdmatch, fdattach
242 };
243
244 extern struct cfdriver fd_cd;
245
246 struct cfattach fdc_ca = {
247 sizeof(struct device), fdcmatch, fdcattach
248 };
249
250 /*
251 * all hw access through macros, this helps to hide the active low
252 * properties
253 */
254
255 #define FDUNITMASK(unit) (1 << (3 + (unit)))
256
257 /*
258 * select units using mask
259 */
260 #define FDSELECT(um) do { ciab.prb &= ~(um); } while (0)
261
262 /*
263 * deselect units using mask
264 */
265 #define FDDESELECT(um) do { ciab.prb |= (um); delay(1); } while (0)
266
267 /*
268 * test hw condition bits
269 */
270 #define FDTESTC(bit) ((ciaa.pra & (1 << (bit))) == 0)
271
272 /*
273 * set motor for select units, true motor on else off
274 */
275 #define FDSETMOTOR(on) do { \
276 if (on) ciab.prb &= ~CIAB_PRB_MTR; else ciab.prb |= CIAB_PRB_MTR; \
277 } while (0)
278
279 /*
280 * set head for select units
281 */
282 #define FDSETHEAD(head) do { \
283 if (head) ciab.prb &= ~CIAB_PRB_SIDE; else ciab.prb |= CIAB_PRB_SIDE; \
284 delay(1); } while (0)
285
286 /*
287 * select direction, true towards spindle else outwards
288 */
289 #define FDSETDIR(in) do { \
290 if (in) ciab.prb &= ~CIAB_PRB_DIR; else ciab.prb |= CIAB_PRB_DIR; \
291 delay(1); } while (0)
292
293 /*
294 * step the selected units
295 */
296 #define FDSTEP do { \
297 ciab.prb &= ~CIAB_PRB_STEP; ciab.prb |= CIAB_PRB_STEP; \
298 } while (0)
299
300 #define FDDMASTART(len, towrite) do { \
301 int dmasz = (len) | ((towrite) ? DISKLEN_WRITE : 0) | DISKLEN_DMAEN; \
302 custom.dsklen = dmasz; custom.dsklen = dmasz; } while (0)
303
304 #define FDDMASTOP do { custom.dsklen = 0; } while (0)
305
306
307 int
308 fdcmatch(pdp, cfp, auxp)
309 struct device *pdp;
310 struct cfdata *cfp;
311 void *auxp;
312 {
313 static int fdc_matched = 0;
314
315 /* Allow only once instance. */
316 if (matchname("fdc", auxp) == 0 || fdc_matched)
317 return(0);
318 if ((fdc_dmap = alloc_chipmem(DMABUFSZ)) == NULL) {
319 printf("fdc: unable to allocate dma buffer\n");
320 return(0);
321 }
322
323 fdc_matched = 1;
324 return(1);
325 }
326
327 void
328 fdcattach(pdp, dp, auxp)
329 struct device *pdp, *dp;
330 void *auxp;
331 {
332 struct fdcargs args;
333
334 printf(": dmabuf pa 0x%x", kvtop(fdc_dmap));
335 printf(": dmabuf ka %p\n", fdc_dmap);
336 args.unit = 0;
337 args.type = fdcgetfdtype(args.unit);
338
339 fdc_side = -1;
340 config_found(dp, &args, fdcprint);
341 for (args.unit++; args.unit < FDMAXUNITS; args.unit++) {
342 if ((args.type = fdcgetfdtype(args.unit)) == NULL)
343 continue;
344 config_found(dp, &args, fdcprint);
345 }
346 }
347
348 int
349 fdcprint(auxp, pnp)
350 void *auxp;
351 const char *pnp;
352 {
353 struct fdcargs *fcp;
354
355 fcp = auxp;
356 if (pnp)
357 printf("fd%d at %s unit %d:", fcp->unit, pnp,
358 fcp->type->driveid);
359 return(UNCONF);
360 }
361
362 /*ARGSUSED*/
363 int
364 fdmatch(pdp, cfp, auxp)
365 struct device *pdp;
366 struct cfdata *cfp;
367 void *auxp;
368 {
369 struct fdcargs *fdap;
370
371 fdap = auxp;
372 if (cfp->cf_loc[FDCCF_UNIT] == fdap->unit ||
373 cfp->cf_loc[FDCCF_UNIT] == FDCCF_UNIT_DEFAULT)
374 return(1);
375
376 return(0);
377 }
378
379 void
380 fdattach(pdp, dp, auxp)
381 struct device *pdp, *dp;
382 void *auxp;
383 {
384 struct fdcargs *ap;
385 struct fd_softc *sc;
386 int i;
387
388 ap = auxp;
389 sc = (struct fd_softc *)dp;
390
391 BUFQ_INIT(&sc->bufq);
392
393 sc->curcyl = sc->cachetrk = -1;
394 sc->openpart = -1;
395 sc->type = ap->type;
396 sc->hwunit = ap->unit;
397 sc->unitmask = 1 << (3 + ap->unit);
398 sc->retries = FDRETRIES;
399 sc->stepdelay = FDSTEPDELAY;
400 sc->bytespersec = 512;
401 printf(" unit %d: %s %d cyl, %d head, %d sec [%d sec], 512 bytes/sec\n",
402 sc->hwunit, sc->type->desc, sc->type->ncylinders, FDNHEADS,
403 sc->type->amiga_nsectors, sc->type->msdos_nsectors);
404
405 /*
406 * Initialize and attach the disk structure.
407 */
408 sc->dkdev.dk_name = sc->sc_dv.dv_xname;
409 sc->dkdev.dk_driver = &fddkdriver;
410 disk_attach(&sc->dkdev);
411
412 /*
413 * calibrate the drive
414 */
415 fdsetpos(sc, 0, 0);
416 fdsetpos(sc, sc->type->ncylinders, 0);
417 fdsetpos(sc, 0, 0);
418 fdmotoroff(sc);
419
420 /*
421 * precalc msdos MFM and CRC
422 */
423 for (i = 0; i < 128; i++)
424 msdecode[i] = 0xff;
425 for (i = 0; i < 16; i++)
426 msdecode[msencode[i]] = i;
427 for (i = 0; i < 256; i++) {
428 mscrctab[i] = (0x1021 * (i & 0xf0)) ^ (0x1021 * (i & 0x0f)) ^
429 (0x1021 * (i >> 4));
430 }
431
432 /*
433 * enable disk related interrupts
434 */
435 custom.dmacon = DMAF_SETCLR | DMAF_MASTER | DMAF_DISK;
436 custom.intena = INTF_SETCLR | INTF_DSKBLK;
437 ciab.icr = CIA_ICR_FLG;
438 }
439
440 /*ARGSUSED*/
441 int
442 fdopen(dev, flags, devtype, p)
443 dev_t dev;
444 int flags, devtype;
445 struct proc *p;
446 {
447 struct fd_softc *sc;
448 int wasopen, fwork, error, s;
449
450 error = 0;
451
452 if (FDPART(dev) >= FDMAXPARTS)
453 return(ENXIO);
454
455 if ((sc = getsoftc(fd_cd, FDUNIT(dev))) == NULL)
456 return(ENXIO);
457 if (sc->flags & FDF_NOTRACK0)
458 return(ENXIO);
459 if (sc->cachep == NULL)
460 sc->cachep = malloc(MAXTRKSZ, M_DEVBUF, M_WAITOK);
461
462 s = splbio();
463 /*
464 * if we are sleeping in fdclose(); waiting for a chance to
465 * shut the motor off, do a sleep here also.
466 */
467 while (sc->flags & FDF_WMOTOROFF)
468 tsleep(fdmotoroff, PRIBIO, "fdopen", 0);
469
470 fwork = 0;
471 /*
472 * if not open let user open request type, otherwise
473 * ensure they are trying to open same type.
474 */
475 if (sc->openpart == FDPART(dev))
476 wasopen = 1;
477 else if (sc->openpart == -1) {
478 sc->openpart = FDPART(dev);
479 wasopen = 0;
480 } else {
481 wasopen = 1;
482 error = EPERM;
483 goto done;
484 }
485
486 /*
487 * wait for current io to complete if any
488 */
489 if (fdc_indma) {
490 fwork = 1;
491 fdc_wantwakeup++;
492 tsleep(fdopen, PRIBIO, "fdopen", 0);
493 }
494 if ((error = fdloaddisk(sc)) != 0)
495 goto done;
496 if ((error = fdgetdisklabel(sc, dev)) != 0)
497 goto done;
498 #ifdef FDDEBUG
499 printf(" open successful\n");
500 #endif
501 done:
502 /*
503 * if we requested that fddone()->fdfindwork() wake us, allow it to
504 * complete its job now
505 */
506 if (fwork)
507 fdfindwork(FDUNIT(dev));
508 splx(s);
509
510 /*
511 * if we were not open and we marked us so reverse that.
512 */
513 if (error && wasopen == 0)
514 sc->openpart = -1;
515 return(error);
516 }
517
518 /*ARGSUSED*/
519 int
520 fdclose(dev, flags, devtype, p)
521 dev_t dev;
522 int flags, devtype;
523 struct proc *p;
524 {
525 struct fd_softc *sc;
526 int s;
527
528 #ifdef FDDEBUG
529 printf("fdclose()\n");
530 #endif
531 sc = getsoftc(fd_cd, FDUNIT(dev));
532 s = splbio();
533 if (sc->flags & FDF_MOTORON) {
534 sc->flags |= FDF_WMOTOROFF;
535 tsleep(fdmotoroff, PRIBIO, "fdclose", 0);
536 sc->flags &= ~FDF_WMOTOROFF;
537 wakeup(fdmotoroff);
538 }
539 sc->openpart = -1;
540 splx(s);
541 return(0);
542 }
543
544 int
545 fdioctl(dev, cmd, addr, flag, p)
546 dev_t dev;
547 u_long cmd;
548 caddr_t addr;
549 int flag;
550 struct proc *p;
551 {
552 struct fd_softc *sc;
553 int error, wlab;
554
555 sc = getsoftc(fd_cd, FDUNIT(dev));
556
557 if ((sc->flags & FDF_HAVELABEL) == 0)
558 return(EBADF);
559
560 switch (cmd) {
561 case DIOCSBAD:
562 return(EINVAL);
563 case DIOCSRETRIES:
564 if (*(int *)addr < 0)
565 return(EINVAL);
566 sc->retries = *(int *)addr;
567 return(0);
568 case DIOCSSTEP:
569 if (*(int *)addr < FDSTEPDELAY)
570 return(EINVAL);
571 sc->dkdev.dk_label->d_trkseek = sc->stepdelay = *(int *)addr;
572 return(0);
573 case DIOCGDINFO:
574 *(struct disklabel *)addr = *(sc->dkdev.dk_label);
575 return(0);
576 case DIOCGPART:
577 ((struct partinfo *)addr)->disklab = sc->dkdev.dk_label;
578 ((struct partinfo *)addr)->part =
579 &sc->dkdev.dk_label->d_partitions[FDPART(dev)];
580 return(0);
581 case DIOCSDINFO:
582 if ((flag & FWRITE) == 0)
583 return(EBADF);
584 return(fdsetdisklabel(sc, (struct disklabel *)addr));
585 case DIOCWDINFO:
586 if ((flag & FWRITE) == 0)
587 return(EBADF);
588 if ((error = fdsetdisklabel(sc, (struct disklabel *)addr)) != 0)
589 return(error);
590 wlab = sc->wlabel;
591 sc->wlabel = 1;
592 error = fdputdisklabel(sc, dev);
593 sc->wlabel = wlab;
594 return(error);
595 case DIOCWLABEL:
596 if ((flag & FWRITE) == 0)
597 return(EBADF);
598 sc->wlabel = *(int *)addr;
599 return(0);
600 case DIOCGDEFLABEL:
601 fdgetdefaultlabel(sc, (struct disklabel *)addr, FDPART(dev));
602 return(0);
603 default:
604 return(ENOTTY);
605 }
606 }
607
608 /*
609 * no dumps to floppy disks thank you.
610 */
611 int
612 fdsize(dev)
613 dev_t dev;
614 {
615 return(-1);
616 }
617
618 int
619 fdread(dev, uio, flags)
620 dev_t dev;
621 struct uio *uio;
622 int flags;
623 {
624 return (physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
625 }
626
627 int
628 fdwrite(dev, uio, flags)
629 dev_t dev;
630 struct uio *uio;
631 int flags;
632 {
633 return (physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
634 }
635
636
637 void
638 fdintr(flag)
639 int flag;
640 {
641 int s;
642
643 s = splbio();
644 if (fdc_indma)
645 fddmadone(fdc_indma, 0);
646 splx(s);
647 }
648
649 void
650 fdidxintr()
651 {
652 if (fdc_indma && fdc_dmalen) {
653 /*
654 * turn off intr and start actual dma
655 */
656 ciab.icr = CIA_ICR_FLG;
657 FDDMASTART(fdc_dmalen, fdc_dmawrite);
658 fdc_dmalen = 0;
659 }
660 }
661
662 void
663 fdstrategy(bp)
664 struct buf *bp;
665 {
666 struct disklabel *lp;
667 struct fd_softc *sc;
668 int unit, part, s;
669
670 unit = FDUNIT(bp->b_dev);
671 part = FDPART(bp->b_dev);
672 sc = getsoftc(fd_cd, unit);
673
674 #ifdef FDDEBUG
675 printf("fdstrategy: 0x%x\n", bp);
676 #endif
677 /*
678 * check for valid partition and bounds
679 */
680 lp = sc->dkdev.dk_label;
681 if ((sc->flags & FDF_HAVELABEL) == 0) {
682 bp->b_error = EIO;
683 goto bad;
684 }
685 if (bounds_check_with_label(bp, lp, sc->wlabel) <= 0)
686 goto done;
687
688 /*
689 * trans count of zero or bounds check indicates io is done
690 * we are done.
691 */
692 if (bp->b_bcount == 0)
693 goto done;
694
695 bp->b_rawblkno = bp->b_blkno;
696
697 /*
698 * queue the buf and kick the low level code
699 */
700 s = splbio();
701 disksort_cylinder(&sc->bufq, bp);
702 fdstart(sc);
703 splx(s);
704 return;
705 bad:
706 bp->b_flags |= B_ERROR;
707 done:
708 bp->b_resid = bp->b_bcount;
709 biodone(bp);
710 }
711
712 /*
713 * make sure disk is loaded and label is up-to-date.
714 */
715 int
716 fdloaddisk(sc)
717 struct fd_softc *sc;
718 {
719 /*
720 * if diskchange is low step drive to 0 then up one then to zero.
721 */
722 fdselunit(sc); /* make sure the unit is selected */
723 if (FDTESTC(FDB_CHANGED)) {
724 fdsetpos(sc, 0, 0);
725 sc->cachetrk = -1; /* invalidate the cache */
726 sc->flags &= ~FDF_HAVELABEL;
727 fdsetpos(sc, FDNHEADS, 0);
728 fdsetpos(sc, 0, 0);
729 if (FDTESTC(FDB_CHANGED)) {
730 fdmotoroff(sc);
731 FDDESELECT(sc->unitmask);
732 return(ENXIO);
733 }
734 }
735 FDDESELECT(sc->unitmask);
736 fdmotoroff(sc);
737 sc->type = fdcgetfdtype(sc->hwunit);
738 if (sc->type == NULL)
739 return(ENXIO);
740 if (sc->openpart == FDMSDOSPART)
741 sc->nsectors = sc->type->msdos_nsectors;
742 else
743 sc->nsectors = sc->type->amiga_nsectors;
744 return(0);
745 }
746
747 void
748 fdgetdefaultlabel(sc, lp, part)
749 struct fd_softc *sc;
750 struct disklabel *lp;
751 int part; /* XXX ick */
752 {
753
754 bzero(lp, sizeof(struct disklabel));
755 lp->d_secsize = FDSECSIZE;
756 lp->d_ntracks = FDNHEADS;
757 lp->d_ncylinders = sc->type->ncylinders;
758 lp->d_nsectors = sc->nsectors;
759 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
760 lp->d_type = DTYPE_FLOPPY;
761 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
762 lp->d_rpm = 300; /* good guess I suppose. */
763 lp->d_interleave = 1; /* should change when adding msdos */
764 sc->stepdelay = lp->d_trkseek = FDSTEPDELAY;
765 lp->d_bbsize = 0;
766 lp->d_sbsize = 0;
767 lp->d_partitions[part].p_size = lp->d_secperunit;
768 lp->d_partitions[part].p_fstype = FS_UNUSED;
769 lp->d_partitions[part].p_fsize = 1024;
770 lp->d_partitions[part].p_frag = 8;
771 lp->d_partitions[part].p_cpg = 2; /* adosfs: reserved blocks */
772 lp->d_npartitions = part + 1;
773 lp->d_magic = lp->d_magic2 = DISKMAGIC;
774 lp->d_checksum = dkcksum(lp);
775 }
776
777 /*
778 * read disk label, if present otherwise create one
779 * return a new label if raw part and none found, otherwise err.
780 */
781 int
782 fdgetdisklabel(sc, dev)
783 struct fd_softc *sc;
784 dev_t dev;
785 {
786 struct disklabel *lp, *dlp;
787 struct cpu_disklabel *clp;
788 struct buf *bp;
789 int error, part;
790
791 if (sc->flags & FDF_HAVELABEL &&
792 sc->dkdev.dk_label->d_npartitions == (FDPART(dev) + 1))
793 return(0);
794 #ifdef FDDEBUG
795 printf("fdgetdisklabel()\n");
796 #endif
797 part = FDPART(dev);
798 lp = sc->dkdev.dk_label;
799 clp = sc->dkdev.dk_cpulabel;
800 bzero(lp, sizeof(struct disklabel));
801 bzero(clp, sizeof(struct cpu_disklabel));
802
803 lp->d_secsize = FDSECSIZE;
804 lp->d_ntracks = FDNHEADS;
805 lp->d_ncylinders = sc->type->ncylinders;
806 lp->d_nsectors = sc->nsectors;
807 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
808 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
809 lp->d_npartitions = part + 1;
810 lp->d_partitions[part].p_size = lp->d_secperunit;
811 lp->d_partitions[part].p_fstype = FS_UNUSED;
812 lp->d_partitions[part].p_fsize = 1024;
813 lp->d_partitions[part].p_frag = 8;
814 lp->d_partitions[part].p_cpg = 2; /* for adosfs: reserved blks */
815
816 sc->flags |= FDF_HAVELABEL;
817
818 bp = (void *)geteblk((int)lp->d_secsize);
819 bp->b_dev = dev;
820 bp->b_blkno = 0;
821 bp->b_cylinder = 0;
822 bp->b_bcount = FDSECSIZE;
823 bp->b_flags = B_BUSY | B_READ;
824 fdstrategy(bp);
825 if ((error = biowait(bp)) != 0)
826 goto nolabel;
827 dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
828 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC ||
829 dkcksum(dlp)) {
830 error = EINVAL;
831 goto nolabel;
832 }
833 bcopy(dlp, lp, sizeof(struct disklabel));
834 if (lp->d_trkseek > FDSTEPDELAY)
835 sc->stepdelay = lp->d_trkseek;
836 brelse(bp);
837 return(0);
838 nolabel:
839 fdgetdefaultlabel(sc, lp, part);
840 brelse(bp);
841 return(0);
842 }
843
844 /*
845 * set the incore copy of this units disklabel
846 */
847 int
848 fdsetdisklabel(sc, lp)
849 struct fd_softc *sc;
850 struct disklabel *lp;
851 {
852 struct disklabel *clp;
853 struct partition *pp;
854
855 /*
856 * must have at least opened raw unit to fetch the
857 * raw_part stuff.
858 */
859 if ((sc->flags & FDF_HAVELABEL) == 0)
860 return(EINVAL);
861 clp = sc->dkdev.dk_label;
862 /*
863 * make sure things check out and we only have one valid
864 * partition
865 */
866 #ifdef FDDEBUG
867 printf("fdsetdisklabel\n");
868 #endif
869 if (lp->d_secsize != FDSECSIZE ||
870 lp->d_nsectors != clp->d_nsectors ||
871 lp->d_ntracks != FDNHEADS ||
872 lp->d_ncylinders != clp->d_ncylinders ||
873 lp->d_secpercyl != clp->d_secpercyl ||
874 lp->d_secperunit != clp->d_secperunit ||
875 lp->d_magic != DISKMAGIC ||
876 lp->d_magic2 != DISKMAGIC ||
877 lp->d_npartitions == 0 ||
878 lp->d_npartitions > FDMAXPARTS ||
879 (lp->d_partitions[0].p_offset && lp->d_partitions[1].p_offset) ||
880 dkcksum(lp))
881 return(EINVAL);
882 /*
883 * if any partitions are present make sure they
884 * represent the currently open type
885 */
886 if ((pp = &lp->d_partitions[0])->p_size) {
887 if ((pp = &lp->d_partitions[1])->p_size == 0)
888 goto done;
889 else if (sc->openpart != 1)
890 return(EINVAL);
891 } else if (sc->openpart != 0)
892 return(EINVAL);
893 /*
894 * make sure selected partition is within bounds
895 * XXX on the second check, its to handle a bug in
896 * XXX the cluster routines as they require mutliples
897 * XXX of NBPG currently
898 */
899 if ((pp->p_offset + pp->p_size >= lp->d_secperunit) ||
900 (pp->p_frag * pp->p_fsize % NBPG))
901 return(EINVAL);
902 done:
903 bcopy(lp, clp, sizeof(struct disklabel));
904 return(0);
905 }
906
907 /*
908 * write out the incore copy of this units disklabel
909 */
910 int
911 fdputdisklabel(sc, dev)
912 struct fd_softc *sc;
913 dev_t dev;
914 {
915 struct disklabel *lp, *dlp;
916 struct buf *bp;
917 int error;
918
919 if ((sc->flags & FDF_HAVELABEL) == 0)
920 return(EBADF);
921 #ifdef FDDEBUG
922 printf("fdputdisklabel\n");
923 #endif
924 /*
925 * get buf and read in sector 0
926 */
927 lp = sc->dkdev.dk_label;
928 bp = (void *)geteblk((int)lp->d_secsize);
929 bp->b_dev = FDMAKEDEV(major(dev), FDUNIT(dev), RAW_PART);
930 bp->b_blkno = 0;
931 bp->b_cylinder = 0;
932 bp->b_bcount = FDSECSIZE;
933 bp->b_flags = B_BUSY | B_READ;
934 fdstrategy(bp);
935 if ((error = biowait(bp)) != 0)
936 goto done;
937 /*
938 * copy disklabel to buf and write it out syncronous
939 */
940 dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
941 bcopy(lp, dlp, sizeof(struct disklabel));
942 bp->b_blkno = 0;
943 bp->b_cylinder = 0;
944 bp->b_flags = B_WRITE;
945 fdstrategy(bp);
946 error = biowait(bp);
947 done:
948 brelse(bp);
949 return(error);
950 }
951
952 /*
953 * figure out drive type or NULL if none.
954 */
955 struct fdtype *
956 fdcgetfdtype(unit)
957 int unit;
958 {
959 struct fdtype *ftp;
960 u_long id, idb;
961 int cnt, umask;
962
963 id = 0;
964 umask = 1 << (3 + unit);
965
966 FDDESELECT(FDCUNITMASK);
967
968 FDSETMOTOR(1);
969 delay(1);
970 FDSELECT(umask);
971 delay(1);
972 FDDESELECT(umask);
973
974 FDSETMOTOR(0);
975 delay(1);
976 FDSELECT(umask);
977 delay(1);
978 FDDESELECT(umask);
979
980 for (idb = 0x80000000; idb; idb >>= 1) {
981 FDSELECT(umask);
982 delay(1);
983 if (FDTESTC(FDB_READY) == 0)
984 id |= idb;
985 FDDESELECT(umask);
986 delay(1);
987 }
988 #ifdef FDDEBUG
989 printf("fdcgettype unit %d id 0x%lx\n", unit, id);
990 #endif
991
992 for (cnt = 0, ftp = fdtype; cnt < nfdtype; ftp++, cnt++)
993 if (ftp->driveid == id)
994 return(ftp);
995 /*
996 * 3.5dd's at unit 0 do not always return id.
997 */
998 if (unit == 0)
999 return(fdtype);
1000 return(NULL);
1001 }
1002
1003 /*
1004 * turn motor off if possible otherwise mark as needed and will be done
1005 * later.
1006 */
1007 void
1008 fdmotoroff(arg)
1009 void *arg;
1010 {
1011 struct fd_softc *sc;
1012 int s;
1013
1014 sc = arg;
1015 s = splbio();
1016
1017 #ifdef FDDEBUG
1018 printf("fdmotoroff: unit %d\n", sc->hwunit);
1019 #endif
1020 if ((sc->flags & FDF_MOTORON) == 0)
1021 goto done;
1022 /*
1023 * if we have a timeout on a dma operation let fddmadone()
1024 * deal with it.
1025 */
1026 if (fdc_indma == sc) {
1027 fddmadone(sc, 1);
1028 goto done;
1029 }
1030 #ifdef FDDEBUG
1031 printf(" motor was on, turning off\n");
1032 #endif
1033
1034 /*
1035 * flush cache if needed
1036 */
1037 if (sc->flags & FDF_DIRTY) {
1038 sc->flags |= FDF_JUSTFLUSH | FDF_MOTOROFF;
1039 #ifdef FDDEBUG
1040 printf(" flushing dirty buffer first\n");
1041 #endif
1042 /*
1043 * if dma'ing done for now, fddone() will call us again
1044 */
1045 if (fdc_indma)
1046 goto done;
1047 fddmastart(sc, sc->cachetrk);
1048 goto done;
1049 }
1050
1051 /*
1052 * if controller is busy just schedule us to be called back
1053 */
1054 if (fdc_indma) {
1055 /*
1056 * someone else has the controller now
1057 * just set flag and let fddone() call us again.
1058 */
1059 sc->flags |= FDF_MOTOROFF;
1060 goto done;
1061 }
1062
1063 #ifdef FDDEBUG
1064 printf(" hw turning unit off\n");
1065 #endif
1066
1067 sc->flags &= ~(FDF_MOTORON | FDF_MOTOROFF);
1068 FDDESELECT(FDCUNITMASK);
1069 FDSETMOTOR(0);
1070 delay(1);
1071 FDSELECT(sc->unitmask);
1072 delay(4);
1073 FDDESELECT(sc->unitmask);
1074 delay(1);
1075 if (sc->flags & FDF_WMOTOROFF)
1076 wakeup(fdmotoroff);
1077 done:
1078 splx(s);
1079 }
1080
1081 /*
1082 * select drive seek to track exit with motor on.
1083 * fdsetpos(x, 0, 0) does calibrates the drive.
1084 */
1085 void
1086 fdsetpos(sc, trk, towrite)
1087 struct fd_softc *sc;
1088 int trk, towrite;
1089 {
1090 int nstep, sdir, ondly, ncyl, nside;
1091
1092 FDDESELECT(FDCUNITMASK);
1093 FDSETMOTOR(1);
1094 delay(1);
1095 FDSELECT(sc->unitmask);
1096 delay(1);
1097 if ((sc->flags & FDF_MOTORON) == 0) {
1098 ondly = 0;
1099 while (FDTESTC(FDB_READY) == 0) {
1100 delay(1000);
1101 if (++ondly >= 1000)
1102 break;
1103 }
1104 }
1105 sc->flags |= FDF_MOTORON;
1106
1107 ncyl = trk / FDNHEADS;
1108 nside = trk % FDNHEADS;
1109
1110 if (sc->curcyl == ncyl && fdc_side == nside)
1111 return;
1112
1113 if (towrite)
1114 sc->flags |= FDF_WRITEWAIT;
1115
1116 #ifdef FDDEBUG
1117 printf("fdsetpos: cyl %d head %d towrite %d\n", trk / FDNHEADS,
1118 trk % FDNHEADS, towrite);
1119 #endif
1120 nstep = ncyl - sc->curcyl;
1121 if (nstep) {
1122 /*
1123 * figure direction
1124 */
1125 if (nstep > 0 && ncyl != 0) {
1126 sdir = FDSTEPIN;
1127 FDSETDIR(1);
1128 } else {
1129 nstep = -nstep;
1130 sdir = FDSTEPOUT;
1131 FDSETDIR(0);
1132 }
1133 if (ncyl == 0) {
1134 /*
1135 * either just want cylinder 0 or doing
1136 * a calibrate.
1137 */
1138 nstep = 256;
1139 while (FDTESTC(FDB_CYLZERO) == 0 && nstep--) {
1140 FDSTEP;
1141 delay(sc->stepdelay);
1142 }
1143 if (nstep < 0)
1144 sc->flags |= FDF_NOTRACK0;
1145 } else {
1146 /*
1147 * step the needed amount amount.
1148 */
1149 while (nstep--) {
1150 FDSTEP;
1151 delay(sc->stepdelay);
1152 }
1153 }
1154 /*
1155 * if switched directions
1156 * allow drive to settle.
1157 */
1158 if (sc->pstepdir != sdir)
1159 delay(FDSETTLEDELAY);
1160 sc->pstepdir = sdir;
1161 sc->curcyl = ncyl;
1162 }
1163 if (nside == fdc_side)
1164 return;
1165 /*
1166 * select side
1167 */
1168 fdc_side = nside;
1169 FDSETHEAD(nside);
1170 delay(FDPRESIDEDELAY);
1171 }
1172
1173 void
1174 fdselunit(sc)
1175 struct fd_softc *sc;
1176 {
1177 FDDESELECT(FDCUNITMASK); /* deselect all */
1178 FDSETMOTOR(sc->flags & FDF_MOTORON); /* set motor to unit's state */
1179 delay(1);
1180 FDSELECT(sc->unitmask); /* select unit */
1181 delay(1);
1182 }
1183
1184 /*
1185 * process next buf on device queue.
1186 * normall sequence of events:
1187 * fdstart() -> fddmastart();
1188 * fdidxintr();
1189 * fdintr() -> fddmadone() -> fddone();
1190 * if the track is in the cache then fdstart() will short-circuit
1191 * to fddone() else if the track cache is dirty it will flush. If
1192 * the buf is not an entire track it will cache the requested track.
1193 */
1194 void
1195 fdstart(sc)
1196 struct fd_softc *sc;
1197 {
1198 int trk, error, write;
1199 struct buf *bp, *dp;
1200 int changed;
1201
1202 #ifdef FDDEBUG
1203 printf("fdstart: unit %d\n", sc->hwunit);
1204 #endif
1205
1206 /*
1207 * if dma'ing just return. we must have been called from fdstartegy.
1208 */
1209 if (fdc_indma)
1210 return;
1211
1212 /*
1213 * get next buf if there.
1214 */
1215 dp = &sc->curbuf;
1216 if ((bp = BUFQ_FIRST(&sc->bufq)) == NULL) {
1217 #ifdef FDDEBUG
1218 printf(" nothing to do\n");
1219 #endif
1220 return;
1221 }
1222
1223 /*
1224 * Mark us as busy now, in case fddone() gets called in one
1225 * of the cases below.
1226 */
1227 disk_busy(&sc->dkdev);
1228
1229 /*
1230 * make sure same disk is loaded
1231 */
1232 fdselunit(sc);
1233 changed = FDTESTC(FDB_CHANGED);
1234 FDDESELECT(sc->unitmask);
1235 if (changed) {
1236 /*
1237 * disk missing, invalidate all future io on
1238 * this unit until re-open()'ed also invalidate
1239 * all current io
1240 */
1241 printf("fdstart: disk changed\n");
1242 #ifdef FDDEBUG
1243 printf(" disk was removed invalidating all io\n");
1244 #endif
1245 sc->flags &= ~FDF_HAVELABEL;
1246 for (;;) {
1247 bp->b_flags |= B_ERROR;
1248 bp->b_error = EIO;
1249 if (BUFQ_NEXT(bp) == NULL)
1250 break;
1251 biodone(bp);
1252 bp = BUFQ_NEXT(bp);
1253 }
1254 /*
1255 * do fddone() on last buf to allow other units to start.
1256 */
1257 BUFQ_INSERT_HEAD(&sc->bufq, bp);
1258 fddone(sc);
1259 return;
1260 }
1261
1262 /*
1263 * we have a valid buf, setup our local version
1264 * we use this count to allow reading over multiple tracks.
1265 * into a single buffer
1266 */
1267 dp->b_bcount = bp->b_bcount;
1268 dp->b_blkno = bp->b_blkno;
1269 dp->b_data = bp->b_data;
1270 dp->b_flags = bp->b_flags;
1271 dp->b_resid = 0;
1272
1273 if (bp->b_flags & B_READ)
1274 write = 0;
1275 else if (FDTESTC(FDB_PROTECT) == 0)
1276 write = 1;
1277 else {
1278 error = EPERM;
1279 goto bad;
1280 }
1281
1282 /*
1283 * figure trk given blkno
1284 */
1285 trk = bp->b_blkno / sc->nsectors;
1286
1287 /*
1288 * check to see if same as currently cached track
1289 * if so we need to do no dma read.
1290 */
1291 if (trk == sc->cachetrk) {
1292 fddone(sc);
1293 return;
1294 }
1295
1296 /*
1297 * if we will be overwriting the entire cache, don't bother to
1298 * fetch it.
1299 */
1300 if (bp->b_bcount == (sc->nsectors * FDSECSIZE) && write &&
1301 bp->b_blkno % sc->nsectors == 0) {
1302 if (sc->flags & FDF_DIRTY)
1303 sc->flags |= FDF_JUSTFLUSH;
1304 else {
1305 sc->cachetrk = trk;
1306 fddone(sc);
1307 return;
1308 }
1309 }
1310
1311 /*
1312 * start dma read of `trk'
1313 */
1314 fddmastart(sc, trk);
1315 return;
1316 bad:
1317 bp->b_flags |= B_ERROR;
1318 bp->b_error = error;
1319 fddone(sc);
1320 }
1321
1322 /*
1323 * continue a started operation on next track. always begin at
1324 * sector 0 on the next track.
1325 */
1326 void
1327 fdcont(sc)
1328 struct fd_softc *sc;
1329 {
1330 struct buf *dp, *bp;
1331 int trk, write;
1332
1333 dp = &sc->curbuf;
1334 bp = BUFQ_FIRST(&sc->bufq);
1335 dp->b_data += (dp->b_bcount - bp->b_resid);
1336 dp->b_blkno += (dp->b_bcount - bp->b_resid) / FDSECSIZE;
1337 dp->b_bcount = bp->b_resid;
1338
1339 /*
1340 * figure trk given blkno
1341 */
1342 trk = dp->b_blkno / sc->nsectors;
1343 #ifdef DEBUG
1344 if (trk != sc->cachetrk + 1 || dp->b_blkno % sc->nsectors != 0)
1345 panic("fdcont: confused");
1346 #endif
1347 if (dp->b_flags & B_READ)
1348 write = 0;
1349 else
1350 write = 1;
1351 /*
1352 * if we will be overwriting the entire cache, don't bother to
1353 * fetch it.
1354 */
1355 if (dp->b_bcount == (sc->nsectors * FDSECSIZE) && write) {
1356 if (sc->flags & FDF_DIRTY)
1357 sc->flags |= FDF_JUSTFLUSH;
1358 else {
1359 sc->cachetrk = trk;
1360 fddone(sc);
1361 return;
1362 }
1363 }
1364 /*
1365 * start dma read of `trk'
1366 */
1367 fddmastart(sc, trk);
1368 return;
1369 }
1370
1371 void
1372 fddmastart(sc, trk)
1373 struct fd_softc *sc;
1374 int trk;
1375 {
1376 int adkmask, ndmaw, write, dmatrk;
1377
1378 #ifdef FDDEBUG
1379 printf("fddmastart: unit %d cyl %d head %d", sc->hwunit,
1380 trk / FDNHEADS, trk % FDNHEADS);
1381 #endif
1382 /*
1383 * flush the cached track if dirty else read requested track.
1384 */
1385 if (sc->flags & FDF_DIRTY) {
1386 fdcachetoraw(sc);
1387 ndmaw = sc->type->nwritew;
1388 dmatrk = sc->cachetrk;
1389 write = 1;
1390 } else {
1391 ndmaw = sc->type->nreadw;
1392 dmatrk = trk;
1393 write = 0;
1394 }
1395
1396 #ifdef FDDEBUG
1397 printf(" %s", write ? " flushing cache\n" : " loading cache\n");
1398 #endif
1399 sc->cachetrk = trk;
1400 fdc_indma = sc;
1401 fdsetpos(sc, dmatrk, write);
1402
1403 /*
1404 * setup dma stuff
1405 */
1406 if (write == 0) {
1407 custom.adkcon = ADKF_MSBSYNC;
1408 custom.adkcon = ADKF_SETCLR | ADKF_WORDSYNC | ADKF_FAST;
1409 custom.dsksync = FDMFMSYNC;
1410 } else {
1411 custom.adkcon = ADKF_PRECOMP1 | ADKF_PRECOMP0 | ADKF_WORDSYNC |
1412 ADKF_MSBSYNC;
1413 adkmask = ADKF_SETCLR | ADKF_FAST | ADKF_MFMPREC;
1414 if (dmatrk >= sc->type->precomp[0])
1415 adkmask |= ADKF_PRECOMP0;
1416 if (dmatrk >= sc->type->precomp[1])
1417 adkmask |= ADKF_PRECOMP1;
1418 custom.adkcon = adkmask;
1419 }
1420 custom.dskpt = (u_char *)kvtop(fdc_dmap);
1421
1422 /*
1423 * If writing an MSDOS track, activate disk index pulse
1424 * interrupt, dma will be started in the intr routine fdidxintr()
1425 * Otherwise, start the DMA here.
1426 */
1427 if (write && sc->openpart == FDMSDOSPART) {
1428 fdc_dmalen = ndmaw;
1429 fdc_dmawrite = write;
1430 ciab.icr = CIA_ICR_IR_SC | CIA_ICR_FLG;
1431 } else {
1432 FDDMASTART(ndmaw, write);
1433 fdc_dmalen = 0;
1434 }
1435
1436 #ifdef FDDEBUG
1437 printf(" dma started\n");
1438 #endif
1439 }
1440
1441 /*
1442 * recalibrate the drive
1443 */
1444 void
1445 fdcalibrate(arg)
1446 void *arg;
1447 {
1448 struct fd_softc *sc;
1449 static int loopcnt;
1450
1451 sc = arg;
1452
1453 if (loopcnt == 0) {
1454 /*
1455 * seek cyl 0
1456 */
1457 fdc_indma = sc;
1458 sc->stepdelay += 900;
1459 if (sc->cachetrk > 1)
1460 fdsetpos(sc, sc->cachetrk % FDNHEADS, 0);
1461 sc->stepdelay -= 900;
1462 }
1463 if (loopcnt++ & 1)
1464 fdsetpos(sc, sc->cachetrk, 0);
1465 else
1466 fdsetpos(sc, sc->cachetrk + FDNHEADS, 0);
1467 /*
1468 * trk++, trk, trk++, trk, trk++, trk, trk++, trk and dma
1469 */
1470 if (loopcnt < 8)
1471 timeout(fdcalibrate, sc, hz / 8);
1472 else {
1473 loopcnt = 0;
1474 fdc_indma = NULL;
1475 timeout(fdmotoroff, sc, 3 * hz / 2);
1476 fddmastart(sc, sc->cachetrk);
1477 }
1478 }
1479
1480 void
1481 fddmadone(sc, timeo)
1482 struct fd_softc *sc;
1483 int timeo;
1484 {
1485 #ifdef FDDEBUG
1486 printf("fddmadone: unit %d, timeo %d\n", sc->hwunit, timeo);
1487 #endif
1488 fdc_indma = NULL;
1489 untimeout(fdmotoroff, sc);
1490 FDDMASTOP;
1491
1492 /*
1493 * guarantee the drive has been at current head and cyl
1494 * for at least FDWRITEDELAY after a write.
1495 */
1496 if (sc->flags & FDF_WRITEWAIT) {
1497 delay(FDWRITEDELAY);
1498 sc->flags &= ~FDF_WRITEWAIT;
1499 }
1500
1501 if ((sc->flags & FDF_MOTOROFF) == 0) {
1502 /*
1503 * motor runs for 1.5 seconds after last dma
1504 */
1505 timeout(fdmotoroff, sc, 3 * hz / 2);
1506 }
1507 if (sc->flags & FDF_DIRTY) {
1508 /*
1509 * if buffer dirty, the last dma cleaned it
1510 */
1511 sc->flags &= ~FDF_DIRTY;
1512 if (timeo)
1513 printf("%s: write of track cache timed out.\n",
1514 sc->sc_dv.dv_xname);
1515 if (sc->flags & FDF_JUSTFLUSH) {
1516 sc->flags &= ~FDF_JUSTFLUSH;
1517 /*
1518 * we are done dma'ing
1519 */
1520 fddone(sc);
1521 return;
1522 }
1523 /*
1524 * load the cache
1525 */
1526 fddmastart(sc, sc->cachetrk);
1527 return;
1528 }
1529 #ifdef FDDEBUG
1530 else if (sc->flags & FDF_MOTOROFF)
1531 panic("fddmadone: FDF_MOTOROFF with no FDF_DIRTY");
1532 #endif
1533
1534 /*
1535 * cache loaded decode it into cache buffer
1536 */
1537 if (timeo == 0 && fdrawtocache(sc) == 0)
1538 sc->retried = 0;
1539 else {
1540 #ifdef FDDEBUG
1541 if (timeo)
1542 printf("%s: fddmadone: cache load timed out.\n",
1543 sc->sc_dv.dv_xname);
1544 #endif
1545 if (sc->retried >= sc->retries) {
1546 sc->retried = 0;
1547 sc->cachetrk = -1;
1548 } else {
1549 sc->retried++;
1550 /*
1551 * this will be restarted at end of calibrate loop.
1552 */
1553 untimeout(fdmotoroff, sc);
1554 fdcalibrate(sc);
1555 return;
1556 }
1557 }
1558 fddone(sc);
1559 }
1560
1561 void
1562 fddone(sc)
1563 struct fd_softc *sc;
1564 {
1565 struct buf *dp, *bp;
1566 char *data;
1567 int sz;
1568
1569 #ifdef FDDEBUG
1570 printf("fddone: unit %d\n", sc->hwunit);
1571 #endif
1572 /*
1573 * check to see if unit is just flushing the cache,
1574 * that is we have no io queued.
1575 */
1576 if (sc->flags & FDF_MOTOROFF)
1577 goto nobuf;
1578
1579 dp = &sc->curbuf;
1580 if ((bp = BUFQ_FIRST(&sc->bufq)) == NULL)
1581 panic ("fddone");
1582 /*
1583 * check for an error that may have occured
1584 * while getting the track.
1585 */
1586 if (sc->cachetrk == -1) {
1587 sc->retried = 0;
1588 bp->b_flags |= B_ERROR;
1589 bp->b_error = EIO;
1590 } else if ((bp->b_flags & B_ERROR) == 0) {
1591 data = sc->cachep;
1592 /*
1593 * get offset of data in track cache and limit
1594 * the copy size to not exceed the cache's end.
1595 */
1596 data += (dp->b_blkno % sc->nsectors) * FDSECSIZE;
1597 sz = sc->nsectors - dp->b_blkno % sc->nsectors;
1598 sz *= FDSECSIZE;
1599 sz = min(dp->b_bcount, sz);
1600 if (bp->b_flags & B_READ)
1601 bcopy(data, dp->b_data, sz);
1602 else {
1603 bcopy(dp->b_data, data, sz);
1604 sc->flags |= FDF_DIRTY;
1605 }
1606 bp->b_resid = dp->b_bcount - sz;
1607 if (bp->b_resid == 0) {
1608 bp->b_error = 0;
1609 } else {
1610 /*
1611 * not done yet need to read next track
1612 */
1613 fdcont(sc);
1614 return;
1615 }
1616 }
1617 /*
1618 * remove from queue.
1619 */
1620 BUFQ_REMOVE(&sc->bufq, bp);
1621
1622 disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid));
1623
1624 biodone(bp);
1625 nobuf:
1626 fdfindwork(sc->sc_dv.dv_unit);
1627 }
1628
1629 void
1630 fdfindwork(unit)
1631 int unit;
1632 {
1633 struct fd_softc *ssc, *sc;
1634 int i, last;
1635
1636 /*
1637 * first see if we have any fdopen()'s waiting
1638 */
1639 if (fdc_wantwakeup) {
1640 wakeup(fdopen);
1641 fdc_wantwakeup--;
1642 return;
1643 }
1644
1645 /*
1646 * start next available unit, linear search from the next unit
1647 * wrapping and finally this unit.
1648 */
1649 last = 0;
1650 ssc = NULL;
1651 for (i = unit + 1; last == 0; i++) {
1652 if (i == unit)
1653 last = 1;
1654 if (i >= fd_cd.cd_ndevs) {
1655 i = -1;
1656 continue;
1657 }
1658 if ((sc = fd_cd.cd_devs[i]) == NULL)
1659 continue;
1660
1661 /*
1662 * if unit has requested to be turned off
1663 * and it has no buf's queued do it now
1664 */
1665 if (sc->flags & FDF_MOTOROFF) {
1666 if (BUFQ_FIRST(&sc->bufq) == NULL)
1667 fdmotoroff(sc);
1668 else {
1669 /*
1670 * we gained a buf request while
1671 * we waited, forget the motoroff
1672 */
1673 sc->flags &= ~FDF_MOTOROFF;
1674 }
1675 /*
1676 * if we now have dma unit must have needed
1677 * flushing, quit
1678 */
1679 if (fdc_indma)
1680 return;
1681 }
1682 /*
1683 * if we have no start unit and the current unit has
1684 * io waiting choose this unit to start.
1685 */
1686 if (ssc == NULL && BUFQ_FIRST(&sc->bufq) != NULL)
1687 ssc = sc;
1688 }
1689 if (ssc)
1690 fdstart(ssc);
1691 }
1692
1693 /*
1694 * min byte count to whats left of the track in question
1695 */
1696 void
1697 fdminphys(bp)
1698 struct buf *bp;
1699 {
1700 struct fd_softc *sc;
1701 int trk, sec, toff, tsz;
1702
1703 if ((sc = getsoftc(fd_cd, FDUNIT(bp->b_dev))) == NULL)
1704 panic("fdminphys: couldn't get softc");
1705
1706 trk = bp->b_blkno / sc->nsectors;
1707 sec = bp->b_blkno % sc->nsectors;
1708
1709 toff = sec * FDSECSIZE;
1710 tsz = sc->nsectors * FDSECSIZE;
1711 #ifdef FDDEBUG
1712 printf("fdminphys: before %d", bp->b_bcount);
1713 #endif
1714 bp->b_bcount = min(bp->b_bcount, tsz - toff);
1715 #ifdef FDDEBUG
1716 printf(" after %d\n", bp->b_bcount);
1717 #endif
1718 minphys(bp);
1719 }
1720
1721 /*
1722 * encode the track cache into raw MFM ready for dma
1723 * when we go to multiple disk formats, this will call type dependent
1724 * functions
1725 */
1726 void fdcachetoraw(sc)
1727 struct fd_softc *sc;
1728 {
1729 if (sc->openpart == FDMSDOSPART)
1730 mscachetoraw(sc);
1731 else
1732 amcachetoraw(sc);
1733 }
1734
1735 /*
1736 * decode raw MFM from dma into units track cache.
1737 * when we go to multiple disk formats, this will call type dependent
1738 * functions
1739 */
1740 int
1741 fdrawtocache(sc)
1742 struct fd_softc *sc;
1743 {
1744
1745 if (sc->openpart == FDMSDOSPART)
1746 return(msrawtocache(sc));
1747 else
1748 return(amrawtocache(sc));
1749 }
1750
1751 void
1752 amcachetoraw(sc)
1753 struct fd_softc *sc;
1754 {
1755 static u_long mfmnull[4];
1756 u_long *rp, *crp, *dp, hcksum, dcksum, info, zero;
1757 int sec, i;
1758
1759 rp = fdc_dmap;
1760
1761 /*
1762 * not yet one sector (- 1 long) gap.
1763 * for now use previous drivers values
1764 */
1765 for (i = 0; i < sc->type->gap; i++)
1766 *rp++ = 0xaaaaaaaa;
1767 /*
1768 * process sectors
1769 */
1770 dp = sc->cachep;
1771 zero = 0;
1772 info = 0xff000000 | (sc->cachetrk << 16) | sc->nsectors;
1773 for (sec = 0; sec < sc->nsectors; sec++, info += (1 << 8) - 1) {
1774 hcksum = dcksum = 0;
1775 /*
1776 * sector format
1777 * offset description
1778 *-----------------------------------
1779 * 0 null
1780 * 1 sync
1781 * oddbits evenbits
1782 *----------------------
1783 * 2 3 [0xff]b [trk]b [sec]b [togap]b
1784 * 4-7 8-11 null
1785 * 12 13 header cksum [2-11]
1786 * 14 15 data cksum [16-271]
1787 * 16-143 144-271 data
1788 */
1789 *rp = 0xaaaaaaaa;
1790 if (*(rp - 1) & 0x1)
1791 *rp &= 0x7fffffff; /* clock bit correction */
1792 rp++;
1793 *rp++ = (FDMFMSYNC << 16) | FDMFMSYNC;
1794 rp = mfmblkencode(&info, rp, &hcksum, 1);
1795 rp = mfmblkencode(mfmnull, rp, &hcksum, 4);
1796 rp = mfmblkencode(&hcksum, rp, NULL, 1);
1797
1798 crp = rp;
1799 rp = mfmblkencode(dp, rp + 2, &dcksum, FDSECLWORDS);
1800 dp += FDSECLWORDS;
1801 crp = mfmblkencode(&dcksum, crp, NULL, 1);
1802 if (*(crp - 1) & 0x1)
1803 *crp &= 0x7fffffff; /* clock bit correction */
1804 else if ((*crp & 0x40000000) == 0)
1805 *crp |= 0x80000000;
1806 }
1807 *rp = 0xaaa80000;
1808 if (*(rp - 1) & 0x1)
1809 *rp &= 0x7fffffff;
1810 }
1811
1812 u_long *
1813 fdfindsync(rp, ep)
1814 u_long *rp, *ep;
1815 {
1816 u_short *sp;
1817
1818 sp = (u_short *)rp;
1819 while ((u_long *)sp < ep && *sp != FDMFMSYNC)
1820 sp++;
1821 while ((u_long *)sp < ep && *sp == FDMFMSYNC)
1822 sp++;
1823 if ((u_long *)sp < ep)
1824 return((u_long *)sp);
1825 return(NULL);
1826 }
1827
1828 int
1829 amrawtocache(sc)
1830 struct fd_softc *sc;
1831 {
1832 u_long mfmnull[4];
1833 u_long *dp, *rp, *erp, *crp, *srp, hcksum, dcksum, info, cktmp;
1834 int cnt, doagain;
1835
1836 doagain = 1;
1837 srp = rp = fdc_dmap;
1838 erp = (u_long *)((u_short *)rp + sc->type->nreadw);
1839 cnt = 0;
1840 again:
1841 if (doagain == 0 || (rp = srp = fdfindsync(srp, erp)) == NULL) {
1842 #ifdef DIAGNOSTIC
1843 printf("%s: corrupted track (%d) data.\n",
1844 sc->sc_dv.dv_xname, sc->cachetrk);
1845 #endif
1846 return(-1);
1847 }
1848
1849 /*
1850 * process sectors
1851 */
1852 for (; cnt < sc->nsectors; cnt++) {
1853 hcksum = dcksum = 0;
1854 rp = mfmblkdecode(rp, &info, &hcksum, 1);
1855 rp = mfmblkdecode(rp, mfmnull, &hcksum, 4);
1856 rp = mfmblkdecode(rp, &cktmp, NULL, 1);
1857 if (cktmp != hcksum) {
1858 #ifdef FDDEBUG
1859 printf(" info 0x%x hchksum 0x%x trkhcksum 0x%x\n",
1860 info, hcksum, cktmp);
1861 #endif
1862 goto again;
1863 }
1864 if (((info >> 16) & 0xff) != sc->cachetrk) {
1865 #ifdef DEBUG
1866 printf("%s: incorrect track found: 0x%lx %d\n",
1867 sc->sc_dv.dv_xname, info, sc->cachetrk);
1868 #endif
1869 goto again;
1870 }
1871 #ifdef FDDEBUG
1872 printf(" info 0x%x\n", info);
1873 #endif
1874
1875 rp = mfmblkdecode(rp, &cktmp, NULL, 1);
1876 dp = sc->cachep;
1877 dp += FDSECLWORDS * ((info >> 8) & 0xff);
1878 crp = mfmblkdecode(rp, dp, &dcksum, FDSECLWORDS);
1879 if (cktmp != dcksum) {
1880 #ifdef FDDEBUG
1881 printf(" info 0x%x dchksum 0x%x trkdcksum 0x%x\n",
1882 info, dcksum, cktmp);
1883 #endif
1884 goto again;
1885 }
1886
1887 /*
1888 * if we are at gap then we can no longer be sure
1889 * of correct sync marks
1890 */
1891 if ((info && 0xff) == 1)
1892 doagain = 1;
1893 else
1894 doagain = 0;
1895 srp = rp = fdfindsync(crp, erp);
1896 }
1897 return(0);
1898 }
1899
1900 void
1901 mscachetoraw(sc)
1902 struct fd_softc *sc;
1903 {
1904 u_short *rp, *erp, crc;
1905 u_char *cp, tb[5];
1906 int sec, i;
1907
1908 rp = (u_short *)fdc_dmap;
1909 erp = rp + sc->type->nwritew;
1910 cp = sc->cachep;
1911
1912 /*
1913 * initial track filler (828 * GAP1)
1914 */
1915 for (i = 0; i < sc->type->gap; i++) {
1916 *rp++ = FDMFMGAP1;
1917 *rp++ = FDMFMGAP1;
1918 }
1919
1920 for (sec = 0; sec < sc->nsectors; sec++) {
1921
1922 /*
1923 * leading sector gap
1924 * (12 * GAP2) + (3 * SYNC)
1925 */
1926 for (i = 0; i < 12; i++)
1927 *rp++ = FDMFMGAP2;
1928 *rp++ = FDMFMSYNC;
1929 *rp++ = FDMFMSYNC;
1930 *rp++ = FDMFMSYNC;
1931
1932 /*
1933 * sector information
1934 * (ID) + track + side + sector + sector size + CRC16
1935 */
1936 *rp++ = FDMFMID;
1937 tb[0] = sc->cachetrk / FDNHEADS;
1938 tb[1] = sc->cachetrk % FDNHEADS;
1939 tb[2] = sec + 1;
1940 i = sc->bytespersec;
1941 tb[3] = i < 256 ? 0 : (i < 512 ? 1 : (i < 1024 ? 2 : 3));
1942 rp = msblkencode(rp, tb, 4, &crc);
1943 tb[0] = crc >> 8;
1944 tb[1] = crc & 0xff;
1945 tb[2] = 0x4e; /* GAP1 decoded */
1946 rp = msblkencode(rp, tb, 3, 0);
1947
1948 /*
1949 * sector info/data gap
1950 * (22 * GAP1) + (12 * GAP2) + (3 * SYNC)
1951 */
1952 for (i = 0; i < 21; i++)
1953 *rp++ = FDMFMGAP1;
1954 for (i = 0; i < 12; i++)
1955 *rp++ = FDMFMGAP2;
1956 *rp++ = FDMFMSYNC;
1957 *rp++ = FDMFMSYNC;
1958 *rp++ = FDMFMSYNC;
1959
1960 /*
1961 * sector data
1962 * (DATA) + ...data... + CRC16
1963 */
1964 *rp++ = FDMFMDATA;
1965 rp = msblkencode(rp, cp, sc->bytespersec, &crc);
1966 cp += sc->bytespersec;
1967 tb[0] = crc >> 8;
1968 tb[1] = crc & 0xff;
1969 tb[2] = 0x4e; /* GAP3 decoded */
1970 rp = msblkencode(rp, tb, 3, 0);
1971
1972 /*
1973 * trailing sector gap
1974 * (80 * GAP3)
1975 */
1976 for (i = 0; i < 79; i++)
1977 *rp++ = FDMFMGAP3;
1978 }
1979
1980 /*
1981 * fill rest of track with GAP3
1982 */
1983 while (rp != erp)
1984 *rp++ = FDMFMGAP3;
1985
1986 }
1987
1988 int
1989 msrawtocache(sc)
1990 struct fd_softc *sc;
1991 {
1992 u_short *rp, *srp, *erp;
1993 u_char tb[5], *cp;
1994 int ct, sec, retry;
1995
1996 srp = rp = (u_short *)fdc_dmap;
1997 erp = rp + sc->type->nreadw;
1998 cp = sc->cachep;
1999
2000 for (ct = 0; ct < sc->nsectors; ct++) {
2001 retry = 1;
2002 do {
2003 /*
2004 * skip leading gap to sync
2005 */
2006 if ((rp = (u_short *)fdfindsync((u_long *)rp, (u_long *)erp)) == NULL) {
2007 #ifdef DIAGNOSTIC
2008 printf("%s: corrupted track (%d) data.\n",
2009 sc->sc_dv.dv_xname, sc->cachetrk);
2010 #endif
2011 return(-1);
2012 }
2013
2014 /*
2015 * Grab sector info
2016 */
2017 if (*rp++ != FDMFMID)
2018 continue;
2019 rp = msblkdecode(rp, tb, 4);
2020 #ifdef FDDEBUG
2021 printf("sector id: sector %d, track %d, side %d,"
2022 "bps %d\n", tb[2], tb[0], tb[1], 128 << tb[3]);
2023 #endif
2024 if ((tb[0] * FDNHEADS + tb[1]) != sc->cachetrk ||
2025 tb[2] > sc->nsectors)
2026 continue;
2027
2028 sec = tb[2];
2029 sc->bytespersec = 128 << tb[3];
2030 rp += 2; /* skip CRC-16 */
2031
2032 /*
2033 * skip gap and read in data
2034 */
2035 if ((rp = (u_short *)fdfindsync((u_long *)rp, (u_long *)erp)) == NULL)
2036 return(-1);
2037 if (*rp++ != FDMFMDATA)
2038 continue;
2039 rp = msblkdecode(rp, cp + ((sec-1) * sc->bytespersec),
2040 sc->bytespersec);
2041 rp += 2; /* skip CRC-16 */
2042
2043 retry = 0;
2044 } while (retry);
2045 }
2046 return(0);
2047 }
2048
2049 /*
2050 * encode len longwords of `dp' data in amiga mfm block format (`rp')
2051 * this format specified that the odd bits are at current pos and even
2052 * bits at len + current pos
2053 */
2054 u_long *
2055 mfmblkencode(dp, rp, cp, len)
2056 u_long *dp, *rp, *cp;
2057 int len;
2058 {
2059 u_long *sdp, *edp, d, dtmp, correct;
2060
2061 sdp = dp;
2062 edp = dp + len;
2063
2064 if (*(rp - 1) & 0x1)
2065 correct = 1;
2066 else
2067 correct = 0;
2068 /*
2069 * do odd bits
2070 */
2071 while (dp < edp) {
2072 d = (*dp >> 1) & 0x55555555; /* remove clock bits */
2073 dtmp = d ^ 0x55555555;
2074 d |= ((dtmp >> 1) | 0x80000000) & (dtmp << 1);
2075 /*
2076 * correct upper clock bit if needed
2077 */
2078 if (correct)
2079 d &= 0x7fffffff;
2080 if (d & 0x1)
2081 correct = 1;
2082 else
2083 correct = 0;
2084 /*
2085 * do checksums and store in raw buffer
2086 */
2087 if (cp)
2088 *cp ^= d;
2089 *rp++ = d;
2090 dp++;
2091 }
2092 /*
2093 * do even bits
2094 */
2095 dp = sdp;
2096 while (dp < edp) {
2097 d = *dp & 0x55555555; /* remove clock bits */
2098 dtmp = d ^ 0x55555555;
2099 d |= ((dtmp >> 1) | 0x80000000) & (dtmp << 1);
2100 /*
2101 * correct upper clock bit if needed
2102 */
2103 if (correct)
2104 d &= 0x7fffffff;
2105 if (d & 0x1)
2106 correct = 1;
2107 else
2108 correct = 0;
2109 /*
2110 * do checksums and store in raw buffer
2111 */
2112 if (cp)
2113 *cp ^= d;
2114 *rp++ = d;
2115 dp++;
2116 }
2117 if (cp)
2118 *cp &= 0x55555555;
2119 return(rp);
2120 }
2121
2122 /*
2123 * decode len longwords of `dp' data in amiga mfm block format (`rp')
2124 * this format specified that the odd bits are at current pos and even
2125 * bits at len + current pos
2126 */
2127 u_long *
2128 mfmblkdecode(rp, dp, cp, len)
2129 u_long *rp, *dp, *cp;
2130 int len;
2131 {
2132 u_long o, e;
2133 int cnt;
2134
2135 cnt = len;
2136 while (cnt--) {
2137 o = *rp;
2138 e = *(rp + len);
2139 if (cp) {
2140 *cp ^= o;
2141 *cp ^= e;
2142 }
2143 o &= 0x55555555;
2144 e &= 0x55555555;
2145 *dp++ = (o << 1) | e;
2146 rp++;
2147 }
2148 if (cp)
2149 *cp &= 0x55555555;
2150 return(rp + len);
2151 }
2152
2153 /*
2154 * decode len words in standard MFM format to len bytes
2155 * of data.
2156 */
2157 u_short *
2158 msblkdecode(rp, cp, len)
2159 u_short *rp;
2160 u_char *cp;
2161 int len;
2162 {
2163 while (len--) {
2164 *cp++ = msdecode[*rp & 0x7f] |
2165 (msdecode[(*rp >> 8) & 0x7f] << 4);
2166 rp++;
2167 }
2168
2169 return(rp);
2170 }
2171
2172 /*
2173 * encode len bytes of data into len words in standard MFM format.
2174 * If a pointer is supplied for crc, calculate the CRC-16 of the data
2175 * as well.
2176 */
2177 u_short *
2178 msblkencode(rp, cp, len, crc)
2179 u_short *rp;
2180 u_char *cp;
2181 int len;
2182 u_short *crc;
2183 {
2184 u_short td;
2185 u_short mycrc;
2186
2187 /* preload crc for header (4 bytes)
2188 * or data (anything else)
2189 */
2190 mycrc = (len == 4) ? 0xb230 : 0xe295;
2191
2192 while (len--) {
2193 td = (msencode[*cp >> 4] << 8) | msencode[*cp & 0x0f];
2194
2195 /* Check for zeros in top bit of encode and bottom
2196 * bit of previous encode. if so, slap a one in betweem
2197 * them.
2198 */
2199 if ((td & 0x140) == 0)
2200 td |= 0x80;
2201 if ((td & 0x4000) == 0 && (rp[-1] & 1) == 0)
2202 td |= 0x8000;
2203
2204 *rp++ = td;
2205
2206 /*
2207 * calc crc if requested
2208 */
2209 if (crc)
2210 mycrc = (mycrc << 8) ^ mscrctab[*cp ^ (mycrc >> 8)];
2211
2212 cp++;
2213 }
2214
2215 if (crc)
2216 *crc = mycrc;
2217
2218 return(rp);
2219 }
2220
2221 int
2222 fddump(dev, blkno, va, size)
2223 dev_t dev;
2224 daddr_t blkno;
2225 caddr_t va;
2226 size_t size;
2227 {
2228 return (EINVAL);
2229 }
2230