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