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