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