fd.c revision 1.48.4.6 1 /* $NetBSD: fd.c,v 1.48.4.6 2002/11/11 21:56:15 nathanw 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.48.4.6 2002/11/11 21:56:15 nathanw 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/device.h>
45 #include <sys/ioctl.h>
46 #include <sys/fcntl.h>
47 #include <sys/disklabel.h>
48 #include <sys/disk.h>
49 #include <sys/dkbad.h>
50 #include <sys/proc.h>
51 #include <sys/conf.h>
52 #include <machine/cpu.h>
53 #include <amiga/amiga/device.h>
54 #include <amiga/amiga/custom.h>
55 #include <amiga/amiga/cia.h>
56 #include <amiga/amiga/cc.h>
57
58 #include "locators.h"
59
60 enum fdc_bits { FDB_CHANGED = 2, FDB_PROTECT, FDB_CYLZERO, FDB_READY };
61 /*
62 * partitions in fd represent different format floppies
63 * partition a is 0 etc..
64 */
65 enum fd_parttypes {
66 FDAMIGAPART = 0,
67 FDMSDOSPART,
68 FDMAXPARTS
69 };
70
71 #define FDBBSIZE (8192)
72 #define FDSBSIZE (8192)
73
74 #define FDUNIT(dev) DISKUNIT(dev)
75 #define FDPART(dev) DISKPART(dev)
76 #define FDMAKEDEV(m, u, p) MAKEDISKDEV((m), (u), (p))
77
78 /* that's nice, but we don't want to always use this as an amiga drive
79 bunghole :-) */
80 #define FDNHEADS (2) /* amiga drives always have 2 heads */
81 #define FDSECSIZE (512) /* amiga drives always have 512 byte sectors */
82 #define FDSECLWORDS (128)
83
84 #define FDSETTLEDELAY (18000) /* usec delay after seeking after switch dir */
85 #define FDSTEPDELAY (3500) /* usec delay after steping */
86 #define FDPRESIDEDELAY (1000) /* usec delay before writing can occur */
87 #define FDWRITEDELAY (1300) /* usec delay after write */
88
89 #define FDSTEPOUT (1) /* decrease track step */
90 #define FDSTEPIN (0) /* increase track step */
91
92 #define FDCUNITMASK (0x78) /* mask for all units (bits 6-3) */
93
94 #define FDRETRIES (2) /* default number of retries */
95 #define FDMAXUNITS (4) /* maximum number of supported units */
96
97 #define DISKLEN_READ (0) /* fake mask for reading */
98 #define DISKLEN_WRITE (1 << 14) /* bit for writing */
99 #define DISKLEN_DMAEN (1 << 15) /* dma go */
100 #define DMABUFSZ ((DISKLEN_WRITE - 1) * 2) /* largest dma possible */
101
102 #define FDMFMSYNC (0x4489)
103 #define FDMFMID (0x5554)
104 #define FDMFMDATA (0x5545)
105 #define FDMFMGAP1 (0x9254)
106 #define FDMFMGAP2 (0xAAAA)
107 #define FDMFMGAP3 (0x9254)
108 #define CRC16POLY (0x1021) /* (x^16) + x^12 + x^5 + x^0 */
109
110 /*
111 * Msdos-type MFM encode/decode
112 */
113 static u_char msdecode[128];
114 static u_char msencode[16] =
115 {
116 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
117 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
118 };
119 static u_short mscrctab[256];
120
121 /*
122 5554 aaaa aaaa aaa5 2aa4 4452 aa51
123 00 00 03 02 ac 0d
124 */
125
126 /*
127 * floppy device type
128 */
129 struct fdtype {
130 u_int driveid; /* drive identification (from drive) */
131 u_int ncylinders; /* number of cylinders on drive */
132 u_int amiga_nsectors; /* number of sectors per amiga track */
133 u_int msdos_nsectors; /* number of sectors per msdos track */
134 u_int nreadw; /* number of words (short) read per track */
135 u_int nwritew; /* number of words (short) written per track */
136 u_int gap; /* track gap size in long words */
137 u_int precomp[2]; /* 1st and 2nd precomp values */
138 char *desc; /* description of drive type (useq) */
139 };
140
141 /*
142 * floppy disk device data
143 */
144 struct fd_softc {
145 struct device sc_dv; /* generic device info; must come first */
146 struct disk dkdev; /* generic disk info */
147 struct bufq_state bufq; /* queue pending I/O operations */
148 struct buf curbuf; /* state of current I/O operation */
149 struct callout calibrate_ch;
150 struct callout motor_ch;
151 struct fdtype *type;
152 void *cachep; /* cached track data (write through) */
153 int cachetrk; /* cahced track -1 for none */
154 int hwunit; /* unit for amiga controlling hw */
155 int unitmask; /* mask for cia select deslect */
156 int pstepdir; /* previous step direction */
157 int curcyl; /* current curcyl head positioned on */
158 int flags; /* misc flags */
159 int wlabel;
160 int stepdelay; /* useq to delay after seek user setable */
161 int nsectors; /* number of sectors per track */
162 int openpart; /* which partition [ab] == [12] is open */
163 short retries; /* number of times to retry failed io */
164 short retried; /* number of times current io retried */
165 int bytespersec; /* number of bytes per sector */
166 };
167
168 /* fd_softc->flags */
169 #define FDF_MOTORON (0x01) /* motor is running */
170 #define FDF_MOTOROFF (0x02) /* motor is waiting to be turned off */
171 #define FDF_WMOTOROFF (0x04) /* unit wants a wakeup after off */
172 #define FDF_DIRTY (0x08) /* track cache needs write */
173 #define FDF_WRITEWAIT (0x10) /* need to head select delay on next setpos */
174 #define FDF_HAVELABEL (0x20) /* label is valid */
175 #define FDF_JUSTFLUSH (0x40) /* don't bother caching track. */
176 #define FDF_NOTRACK0 (0x80) /* was not able to recalibrate drive */
177
178 int fdc_wantwakeup;
179 int fdc_side;
180 void *fdc_dmap;
181 struct fd_softc *fdc_indma;
182 int fdc_dmalen;
183 int fdc_dmawrite;
184
185 struct fdcargs {
186 struct fdtype *type;
187 int unit;
188 };
189
190 int fdcmatch(struct device *, struct cfdata *, void *);
191 void fdcattach(struct device *, struct device *, void *);
192 int fdcprint(void *, const char *);
193 int fdmatch(struct device *, struct cfdata *, void *);
194 void fdattach(struct device *, struct device *, void *);
195
196 void fdintr(int);
197 void fdidxintr(void);
198 int fdloaddisk(struct fd_softc *);
199 void fdgetdefaultlabel(struct fd_softc *, struct disklabel *, int);
200 int fdgetdisklabel(struct fd_softc *, dev_t);
201 int fdsetdisklabel(struct fd_softc *, struct disklabel *);
202 int fdputdisklabel(struct fd_softc *, dev_t);
203 struct fdtype * fdcgetfdtype(int);
204 void fdmotoroff(void *);
205 void fdsetpos(struct fd_softc *, int, int);
206 void fdselunit(struct fd_softc *);
207 void fdstart(struct fd_softc *);
208 void fdcont(struct fd_softc *);
209 void fddmastart(struct fd_softc *, int);
210 void fdcalibrate(void *);
211 void fddmadone(struct fd_softc *, int);
212 void fddone(struct fd_softc *);
213 void fdfindwork(int);
214 void fdminphys(struct buf *);
215 void fdcachetoraw(struct fd_softc *);
216 void amcachetoraw(struct fd_softc *);
217 int amrawtocache(struct fd_softc *);
218 u_long *fdfindsync(u_long *, u_long *);
219 int fdrawtocache(struct fd_softc *);
220 void mscachetoraw(struct fd_softc *);
221 int msrawtocache(struct fd_softc *);
222 u_long *mfmblkencode(u_long *, u_long *, u_long *, int);
223 u_long *mfmblkdecode(u_long *, u_long *, u_long *, int);
224 u_short *msblkdecode(u_short *, u_char *, int);
225 u_short *msblkencode(u_short *, u_char *, int, u_short *);
226
227 /*
228 * read size is (nsectors + 1) * mfm secsize + gap bytes + 2 shorts
229 * write size is nsectors * mfm secsize + gap bytes + 3 shorts
230 * the extra shorts are to deal with a dma hw bug in the controller
231 * they are probably too much (I belive the bug is 1 short on write and
232 * 3 bits on read) but there is no need to be cheap here.
233 */
234 #define MAXTRKSZ (22 * FDSECSIZE)
235 struct fdtype fdtype[] = {
236 { 0x00000000, 80, 11, 9, 7358, 6815, 414, { 80, 161 }, "3.5dd" },
237 { 0x55555555, 40, 11, 9, 7358, 6815, 414, { 80, 161 }, "5.25dd" },
238 { 0xAAAAAAAA, 80, 22, 18, 14716, 13630, 828, { 80, 161 }, "3.5hd" }
239 };
240 int nfdtype = sizeof(fdtype) / sizeof(*fdtype);
241
242 CFATTACH_DECL(fd, sizeof(struct fd_softc),
243 fdmatch, fdattach, NULL, NULL);
244
245 extern struct cfdriver fd_cd;
246
247 dev_type_open(fdopen);
248 dev_type_close(fdclose);
249 dev_type_read(fdread);
250 dev_type_write(fdwrite);
251 dev_type_ioctl(fdioctl);
252 dev_type_strategy(fdstrategy);
253
254 const struct bdevsw fd_bdevsw = {
255 fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
256 };
257
258 const struct cdevsw fd_cdevsw = {
259 fdopen, fdclose, fdread, fdwrite, fdioctl,
260 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
261 };
262
263 struct dkdriver fddkdriver = { fdstrategy };
264
265 CFATTACH_DECL(fdc, sizeof(struct device),
266 fdcmatch, fdcattach, NULL, NULL);
267
268 /*
269 * all hw access through macros, this helps to hide the active low
270 * properties
271 */
272
273 #define FDUNITMASK(unit) (1 << (3 + (unit)))
274
275 /*
276 * select units using mask
277 */
278 #define FDSELECT(um) do { ciab.prb &= ~(um); } while (0)
279
280 /*
281 * deselect units using mask
282 */
283 #define FDDESELECT(um) do { ciab.prb |= (um); delay(1); } while (0)
284
285 /*
286 * test hw condition bits
287 */
288 #define FDTESTC(bit) ((ciaa.pra & (1 << (bit))) == 0)
289
290 /*
291 * set motor for select units, true motor on else off
292 */
293 #define FDSETMOTOR(on) do { \
294 if (on) ciab.prb &= ~CIAB_PRB_MTR; else ciab.prb |= CIAB_PRB_MTR; \
295 } while (0)
296
297 /*
298 * set head for select units
299 */
300 #define FDSETHEAD(head) do { \
301 if (head) ciab.prb &= ~CIAB_PRB_SIDE; else ciab.prb |= CIAB_PRB_SIDE; \
302 delay(1); } while (0)
303
304 /*
305 * select direction, true towards spindle else outwards
306 */
307 #define FDSETDIR(in) do { \
308 if (in) ciab.prb &= ~CIAB_PRB_DIR; else ciab.prb |= CIAB_PRB_DIR; \
309 delay(1); } while (0)
310
311 /*
312 * step the selected units
313 */
314 #define FDSTEP do { \
315 ciab.prb &= ~CIAB_PRB_STEP; ciab.prb |= CIAB_PRB_STEP; \
316 } while (0)
317
318 #define FDDMASTART(len, towrite) do { \
319 int dmasz = (len) | ((towrite) ? DISKLEN_WRITE : 0) | DISKLEN_DMAEN; \
320 custom.dsklen = dmasz; custom.dsklen = dmasz; } while (0)
321
322 #define FDDMASTOP do { custom.dsklen = 0; } while (0)
323
324
325 int
326 fdcmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
327 {
328 static int fdc_matched = 0;
329
330 /* Allow only once instance. */
331 if (matchname("fdc", auxp) == 0 || fdc_matched)
332 return(0);
333 if ((fdc_dmap = alloc_chipmem(DMABUFSZ)) == NULL) {
334 printf("fdc: unable to allocate dma buffer\n");
335 return(0);
336 }
337
338 fdc_matched = 1;
339 return(1);
340 }
341
342 void
343 fdcattach(struct device *pdp, struct device *dp, void *auxp)
344 {
345 struct fdcargs args;
346
347 printf(": dmabuf pa 0x%x", kvtop(fdc_dmap));
348 printf(": dmabuf ka %p\n", fdc_dmap);
349 args.unit = 0;
350 args.type = fdcgetfdtype(args.unit);
351
352 fdc_side = -1;
353 config_found(dp, &args, fdcprint);
354 for (args.unit++; args.unit < FDMAXUNITS; args.unit++) {
355 if ((args.type = fdcgetfdtype(args.unit)) == NULL)
356 continue;
357 config_found(dp, &args, fdcprint);
358 }
359 }
360
361 int
362 fdcprint(void *auxp, const char *pnp)
363 {
364 struct fdcargs *fcp;
365
366 fcp = auxp;
367 if (pnp)
368 printf("fd%d at %s unit %d:", fcp->unit, pnp,
369 fcp->type->driveid);
370 return(UNCONF);
371 }
372
373 /*ARGSUSED*/
374 int
375 fdmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
376 {
377 struct fdcargs *fdap;
378
379 fdap = auxp;
380 if (cfp->cf_loc[FDCCF_UNIT] == fdap->unit ||
381 cfp->cf_loc[FDCCF_UNIT] == FDCCF_UNIT_DEFAULT)
382 return(1);
383
384 return(0);
385 }
386
387 void
388 fdattach(struct device *pdp, struct device *dp, void *auxp)
389 {
390 struct fdcargs *ap;
391 struct fd_softc *sc;
392 int i;
393
394 ap = auxp;
395 sc = (struct fd_softc *)dp;
396
397 bufq_alloc(&sc->bufq, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
398 callout_init(&sc->calibrate_ch);
399 callout_init(&sc->motor_ch);
400
401 sc->curcyl = sc->cachetrk = -1;
402 sc->openpart = -1;
403 sc->type = ap->type;
404 sc->hwunit = ap->unit;
405 sc->unitmask = 1 << (3 + ap->unit);
406 sc->retries = FDRETRIES;
407 sc->stepdelay = FDSTEPDELAY;
408 sc->bytespersec = 512;
409 printf(" unit %d: %s %d cyl, %d head, %d sec [%d sec], 512 bytes/sec\n",
410 sc->hwunit, sc->type->desc, sc->type->ncylinders, FDNHEADS,
411 sc->type->amiga_nsectors, sc->type->msdos_nsectors);
412
413 /*
414 * Initialize and attach the disk structure.
415 */
416 sc->dkdev.dk_name = sc->sc_dv.dv_xname;
417 sc->dkdev.dk_driver = &fddkdriver;
418 disk_attach(&sc->dkdev);
419
420 /*
421 * calibrate the drive
422 */
423 fdsetpos(sc, 0, 0);
424 fdsetpos(sc, sc->type->ncylinders, 0);
425 fdsetpos(sc, 0, 0);
426 fdmotoroff(sc);
427
428 /*
429 * precalc msdos MFM and CRC
430 */
431 for (i = 0; i < 128; i++)
432 msdecode[i] = 0xff;
433 for (i = 0; i < 16; i++)
434 msdecode[msencode[i]] = i;
435 for (i = 0; i < 256; i++) {
436 mscrctab[i] = (0x1021 * (i & 0xf0)) ^ (0x1021 * (i & 0x0f)) ^
437 (0x1021 * (i >> 4));
438 }
439
440 /*
441 * enable disk related interrupts
442 */
443 custom.dmacon = DMAF_SETCLR | DMAF_MASTER | DMAF_DISK;
444 custom.intena = INTF_SETCLR | INTF_DSKBLK;
445 ciab.icr = CIA_ICR_FLG;
446 }
447
448 /*ARGSUSED*/
449 int
450 fdopen(dev_t dev, int flags, int devtype, struct proc *p)
451 {
452 struct fd_softc *sc;
453 int wasopen, fwork, error, s;
454
455 error = 0;
456
457 if (FDPART(dev) >= FDMAXPARTS)
458 return(ENXIO);
459
460 if ((sc = getsoftc(fd_cd, FDUNIT(dev))) == NULL)
461 return(ENXIO);
462 if (sc->flags & FDF_NOTRACK0)
463 return(ENXIO);
464 if (sc->cachep == NULL)
465 sc->cachep = malloc(MAXTRKSZ, M_DEVBUF, M_WAITOK);
466
467 s = splbio();
468 /*
469 * if we are sleeping in fdclose(); waiting for a chance to
470 * shut the motor off, do a sleep here also.
471 */
472 while (sc->flags & FDF_WMOTOROFF)
473 tsleep(fdmotoroff, PRIBIO, "fdopen", 0);
474
475 fwork = 0;
476 /*
477 * if not open let user open request type, otherwise
478 * ensure they are trying to open same type.
479 */
480 if (sc->openpart == FDPART(dev))
481 wasopen = 1;
482 else if (sc->openpart == -1) {
483 sc->openpart = FDPART(dev);
484 wasopen = 0;
485 } else {
486 wasopen = 1;
487 error = EPERM;
488 goto done;
489 }
490
491 /*
492 * wait for current io to complete if any
493 */
494 if (fdc_indma) {
495 fwork = 1;
496 fdc_wantwakeup++;
497 tsleep(fdopen, PRIBIO, "fdopen", 0);
498 }
499 if ((error = fdloaddisk(sc)) != 0)
500 goto done;
501 if ((error = fdgetdisklabel(sc, dev)) != 0)
502 goto done;
503 #ifdef FDDEBUG
504 printf(" open successful\n");
505 #endif
506 done:
507 /*
508 * if we requested that fddone()->fdfindwork() wake us, allow it to
509 * complete its job now
510 */
511 if (fwork)
512 fdfindwork(FDUNIT(dev));
513 splx(s);
514
515 /*
516 * if we were not open and we marked us so reverse that.
517 */
518 if (error && wasopen == 0)
519 sc->openpart = -1;
520 return(error);
521 }
522
523 /*ARGSUSED*/
524 int
525 fdclose(dev_t dev, int flags, int devtype, struct proc *p)
526 {
527 struct fd_softc *sc;
528 int s;
529
530 #ifdef FDDEBUG
531 printf("fdclose()\n");
532 #endif
533 sc = getsoftc(fd_cd, FDUNIT(dev));
534 s = splbio();
535 if (sc->flags & FDF_MOTORON) {
536 sc->flags |= FDF_WMOTOROFF;
537 tsleep(fdmotoroff, PRIBIO, "fdclose", 0);
538 sc->flags &= ~FDF_WMOTOROFF;
539 wakeup(fdmotoroff);
540 }
541 sc->openpart = -1;
542 splx(s);
543 return(0);
544 }
545
546 int
547 fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, 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 int
606 fdread(dev_t dev, struct uio *uio, int flags)
607 {
608 return (physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
609 }
610
611 int
612 fdwrite(dev_t dev, struct uio *uio, int flags)
613 {
614 return (physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
615 }
616
617
618 void
619 fdintr(int flag)
620 {
621 int s;
622
623 s = splbio();
624 if (fdc_indma)
625 fddmadone(fdc_indma, 0);
626 splx(s);
627 }
628
629 void
630 fdidxintr(void)
631 {
632 if (fdc_indma && fdc_dmalen) {
633 /*
634 * turn off intr and start actual dma
635 */
636 ciab.icr = CIA_ICR_FLG;
637 FDDMASTART(fdc_dmalen, fdc_dmawrite);
638 fdc_dmalen = 0;
639 }
640 }
641
642 void
643 fdstrategy(struct buf *bp)
644 {
645 struct disklabel *lp;
646 struct fd_softc *sc;
647 int unit, part, s;
648
649 unit = FDUNIT(bp->b_dev);
650 part = FDPART(bp->b_dev);
651 sc = getsoftc(fd_cd, unit);
652
653 #ifdef FDDEBUG
654 printf("fdstrategy: 0x%x\n", bp);
655 #endif
656 /*
657 * check for valid partition and bounds
658 */
659 lp = sc->dkdev.dk_label;
660 if ((sc->flags & FDF_HAVELABEL) == 0) {
661 bp->b_error = EIO;
662 goto bad;
663 }
664 if (bounds_check_with_label(bp, lp, sc->wlabel) <= 0)
665 goto done;
666
667 /*
668 * trans count of zero or bounds check indicates io is done
669 * we are done.
670 */
671 if (bp->b_bcount == 0)
672 goto done;
673
674 bp->b_rawblkno = bp->b_blkno;
675
676 /*
677 * queue the buf and kick the low level code
678 */
679 s = splbio();
680 BUFQ_PUT(&sc->bufq, bp);
681 fdstart(sc);
682 splx(s);
683 return;
684 bad:
685 bp->b_flags |= B_ERROR;
686 done:
687 bp->b_resid = bp->b_bcount;
688 biodone(bp);
689 }
690
691 /*
692 * make sure disk is loaded and label is up-to-date.
693 */
694 int
695 fdloaddisk(struct fd_softc *sc)
696 {
697 /*
698 * if diskchange is low step drive to 0 then up one then to zero.
699 */
700 fdselunit(sc); /* make sure the unit is selected */
701 if (FDTESTC(FDB_CHANGED)) {
702 fdsetpos(sc, 0, 0);
703 sc->cachetrk = -1; /* invalidate the cache */
704 sc->flags &= ~FDF_HAVELABEL;
705 fdsetpos(sc, FDNHEADS, 0);
706 fdsetpos(sc, 0, 0);
707 if (FDTESTC(FDB_CHANGED)) {
708 fdmotoroff(sc);
709 FDDESELECT(sc->unitmask);
710 return(ENXIO);
711 }
712 }
713 FDDESELECT(sc->unitmask);
714 fdmotoroff(sc);
715 sc->type = fdcgetfdtype(sc->hwunit);
716 if (sc->type == NULL)
717 return(ENXIO);
718 if (sc->openpart == FDMSDOSPART)
719 sc->nsectors = sc->type->msdos_nsectors;
720 else
721 sc->nsectors = sc->type->amiga_nsectors;
722 return(0);
723 }
724
725 void
726 fdgetdefaultlabel(struct fd_softc *sc, struct disklabel *lp, int part)
727 /* (variable part) XXX ick */
728 {
729
730 bzero(lp, sizeof(struct disklabel));
731 lp->d_secsize = FDSECSIZE;
732 lp->d_ntracks = FDNHEADS;
733 lp->d_ncylinders = sc->type->ncylinders;
734 lp->d_nsectors = sc->nsectors;
735 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
736 lp->d_type = DTYPE_FLOPPY;
737 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
738 lp->d_rpm = 300; /* good guess I suppose. */
739 lp->d_interleave = 1; /* should change when adding msdos */
740 sc->stepdelay = lp->d_trkseek = FDSTEPDELAY;
741 lp->d_bbsize = 0;
742 lp->d_sbsize = 0;
743 lp->d_partitions[part].p_size = lp->d_secperunit;
744 lp->d_partitions[part].p_fstype = FS_UNUSED;
745 lp->d_partitions[part].p_fsize = 1024;
746 lp->d_partitions[part].p_frag = 8;
747 lp->d_partitions[part].p_cpg = 2; /* adosfs: reserved blocks */
748 lp->d_npartitions = part + 1;
749 lp->d_magic = lp->d_magic2 = DISKMAGIC;
750 lp->d_checksum = dkcksum(lp);
751 }
752
753 /*
754 * read disk label, if present otherwise create one
755 * return a new label if raw part and none found, otherwise err.
756 */
757 int
758 fdgetdisklabel(struct fd_softc *sc, dev_t dev)
759 {
760 struct disklabel *lp, *dlp;
761 struct cpu_disklabel *clp;
762 struct buf *bp;
763 int error, part;
764
765 if (sc->flags & FDF_HAVELABEL &&
766 sc->dkdev.dk_label->d_npartitions == (FDPART(dev) + 1))
767 return(0);
768 #ifdef FDDEBUG
769 printf("fdgetdisklabel()\n");
770 #endif
771 part = FDPART(dev);
772 lp = sc->dkdev.dk_label;
773 clp = sc->dkdev.dk_cpulabel;
774 bzero(lp, sizeof(struct disklabel));
775 bzero(clp, sizeof(struct cpu_disklabel));
776
777 lp->d_secsize = FDSECSIZE;
778 lp->d_ntracks = FDNHEADS;
779 lp->d_ncylinders = sc->type->ncylinders;
780 lp->d_nsectors = sc->nsectors;
781 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
782 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
783 lp->d_npartitions = part + 1;
784 lp->d_partitions[part].p_size = lp->d_secperunit;
785 lp->d_partitions[part].p_fstype = FS_UNUSED;
786 lp->d_partitions[part].p_fsize = 1024;
787 lp->d_partitions[part].p_frag = 8;
788 lp->d_partitions[part].p_cpg = 2; /* for adosfs: reserved blks */
789
790 sc->flags |= FDF_HAVELABEL;
791
792 bp = (void *)geteblk((int)lp->d_secsize);
793 bp->b_dev = dev;
794 bp->b_blkno = 0;
795 bp->b_cylinder = 0;
796 bp->b_bcount = FDSECSIZE;
797 bp->b_flags |= B_READ;
798 fdstrategy(bp);
799 if ((error = biowait(bp)) != 0)
800 goto nolabel;
801 dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
802 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC ||
803 dkcksum(dlp)) {
804 error = EINVAL;
805 goto nolabel;
806 }
807 bcopy(dlp, lp, sizeof(struct disklabel));
808 if (lp->d_trkseek > FDSTEPDELAY)
809 sc->stepdelay = lp->d_trkseek;
810 brelse(bp);
811 return(0);
812 nolabel:
813 fdgetdefaultlabel(sc, lp, part);
814 brelse(bp);
815 return(0);
816 }
817
818 /*
819 * set the incore copy of this units disklabel
820 */
821 int
822 fdsetdisklabel(struct fd_softc *sc, struct disklabel *lp)
823 {
824 struct disklabel *clp;
825 struct partition *pp;
826
827 /*
828 * must have at least opened raw unit to fetch the
829 * raw_part stuff.
830 */
831 if ((sc->flags & FDF_HAVELABEL) == 0)
832 return(EINVAL);
833 clp = sc->dkdev.dk_label;
834 /*
835 * make sure things check out and we only have one valid
836 * partition
837 */
838 #ifdef FDDEBUG
839 printf("fdsetdisklabel\n");
840 #endif
841 if (lp->d_secsize != FDSECSIZE ||
842 lp->d_nsectors != clp->d_nsectors ||
843 lp->d_ntracks != FDNHEADS ||
844 lp->d_ncylinders != clp->d_ncylinders ||
845 lp->d_secpercyl != clp->d_secpercyl ||
846 lp->d_secperunit != clp->d_secperunit ||
847 lp->d_magic != DISKMAGIC ||
848 lp->d_magic2 != DISKMAGIC ||
849 lp->d_npartitions == 0 ||
850 lp->d_npartitions > FDMAXPARTS ||
851 (lp->d_partitions[0].p_offset && lp->d_partitions[1].p_offset) ||
852 dkcksum(lp))
853 return(EINVAL);
854 /*
855 * if any partitions are present make sure they
856 * represent the currently open type
857 */
858 if ((pp = &lp->d_partitions[0])->p_size) {
859 if ((pp = &lp->d_partitions[1])->p_size == 0)
860 goto done;
861 else if (sc->openpart != 1)
862 return(EINVAL);
863 } else if (sc->openpart != 0)
864 return(EINVAL);
865 /*
866 * make sure selected partition is within bounds
867 * XXX on the second check, its to handle a bug in
868 * XXX the cluster routines as they require mutliples
869 * XXX of NBPG currently
870 */
871 if ((pp->p_offset + pp->p_size >= lp->d_secperunit) ||
872 (pp->p_frag * pp->p_fsize % NBPG))
873 return(EINVAL);
874 done:
875 bcopy(lp, clp, sizeof(struct disklabel));
876 return(0);
877 }
878
879 /*
880 * write out the incore copy of this units disklabel
881 */
882 int
883 fdputdisklabel(struct fd_softc *sc, dev_t dev)
884 {
885 struct disklabel *lp, *dlp;
886 struct buf *bp;
887 int error;
888
889 if ((sc->flags & FDF_HAVELABEL) == 0)
890 return(EBADF);
891 #ifdef FDDEBUG
892 printf("fdputdisklabel\n");
893 #endif
894 /*
895 * get buf and read in sector 0
896 */
897 lp = sc->dkdev.dk_label;
898 bp = geteblk((int)lp->d_secsize);
899 bp->b_dev = FDMAKEDEV(major(dev), FDUNIT(dev), RAW_PART);
900 bp->b_blkno = 0;
901 bp->b_cylinder = 0;
902 bp->b_bcount = FDSECSIZE;
903 bp->b_flags |= B_READ;
904 fdstrategy(bp);
905 if ((error = biowait(bp)) != 0)
906 goto done;
907 /*
908 * copy disklabel to buf and write it out syncronous
909 */
910 dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
911 bcopy(lp, dlp, sizeof(struct disklabel));
912 bp->b_blkno = 0;
913 bp->b_cylinder = 0;
914 bp->b_flags &= ~(B_READ|B_DONE);
915 bp->b_flags |= B_WRITE;
916 fdstrategy(bp);
917 error = biowait(bp);
918 done:
919 brelse(bp);
920 return(error);
921 }
922
923 /*
924 * figure out drive type or NULL if none.
925 */
926 struct fdtype *
927 fdcgetfdtype(int unit)
928 {
929 struct fdtype *ftp;
930 u_long id, idb;
931 int cnt, umask;
932
933 id = 0;
934 umask = 1 << (3 + unit);
935
936 FDDESELECT(FDCUNITMASK);
937
938 FDSETMOTOR(1);
939 delay(1);
940 FDSELECT(umask);
941 delay(1);
942 FDDESELECT(umask);
943
944 FDSETMOTOR(0);
945 delay(1);
946 FDSELECT(umask);
947 delay(1);
948 FDDESELECT(umask);
949
950 for (idb = 0x80000000; idb; idb >>= 1) {
951 FDSELECT(umask);
952 delay(1);
953 if (FDTESTC(FDB_READY) == 0)
954 id |= idb;
955 FDDESELECT(umask);
956 delay(1);
957 }
958 #ifdef FDDEBUG
959 printf("fdcgettype unit %d id 0x%lx\n", unit, id);
960 #endif
961
962 for (cnt = 0, ftp = fdtype; cnt < nfdtype; ftp++, cnt++)
963 if (ftp->driveid == id)
964 return(ftp);
965 /*
966 * 3.5dd's at unit 0 do not always return id.
967 */
968 if (unit == 0)
969 return(fdtype);
970 return(NULL);
971 }
972
973 /*
974 * turn motor off if possible otherwise mark as needed and will be done
975 * later.
976 */
977 void
978 fdmotoroff(void *arg)
979 {
980 struct fd_softc *sc;
981 int s;
982
983 sc = arg;
984 s = splbio();
985
986 #ifdef FDDEBUG
987 printf("fdmotoroff: unit %d\n", sc->hwunit);
988 #endif
989 if ((sc->flags & FDF_MOTORON) == 0)
990 goto done;
991 /*
992 * if we have a timeout on a dma operation let fddmadone()
993 * deal with it.
994 */
995 if (fdc_indma == sc) {
996 fddmadone(sc, 1);
997 goto done;
998 }
999 #ifdef FDDEBUG
1000 printf(" motor was on, turning off\n");
1001 #endif
1002
1003 /*
1004 * flush cache if needed
1005 */
1006 if (sc->flags & FDF_DIRTY) {
1007 sc->flags |= FDF_JUSTFLUSH | FDF_MOTOROFF;
1008 #ifdef FDDEBUG
1009 printf(" flushing dirty buffer first\n");
1010 #endif
1011 /*
1012 * if dma'ing done for now, fddone() will call us again
1013 */
1014 if (fdc_indma)
1015 goto done;
1016 fddmastart(sc, sc->cachetrk);
1017 goto done;
1018 }
1019
1020 /*
1021 * if controller is busy just schedule us to be called back
1022 */
1023 if (fdc_indma) {
1024 /*
1025 * someone else has the controller now
1026 * just set flag and let fddone() call us again.
1027 */
1028 sc->flags |= FDF_MOTOROFF;
1029 goto done;
1030 }
1031
1032 #ifdef FDDEBUG
1033 printf(" hw turning unit off\n");
1034 #endif
1035
1036 sc->flags &= ~(FDF_MOTORON | FDF_MOTOROFF);
1037 FDDESELECT(FDCUNITMASK);
1038 FDSETMOTOR(0);
1039 delay(1);
1040 FDSELECT(sc->unitmask);
1041 delay(4);
1042 FDDESELECT(sc->unitmask);
1043 delay(1);
1044 if (sc->flags & FDF_WMOTOROFF)
1045 wakeup(fdmotoroff);
1046 done:
1047 splx(s);
1048 }
1049
1050 /*
1051 * select drive seek to track exit with motor on.
1052 * fdsetpos(x, 0, 0) does calibrates the drive.
1053 */
1054 void
1055 fdsetpos(struct fd_softc *sc, int trk, int towrite)
1056 {
1057 int nstep, sdir, ondly, ncyl, nside;
1058
1059 FDDESELECT(FDCUNITMASK);
1060 FDSETMOTOR(1);
1061 delay(1);
1062 FDSELECT(sc->unitmask);
1063 delay(1);
1064 if ((sc->flags & FDF_MOTORON) == 0) {
1065 ondly = 0;
1066 while (FDTESTC(FDB_READY) == 0) {
1067 delay(1000);
1068 if (++ondly >= 1000)
1069 break;
1070 }
1071 }
1072 sc->flags |= FDF_MOTORON;
1073
1074 ncyl = trk / FDNHEADS;
1075 nside = trk % FDNHEADS;
1076
1077 if (sc->curcyl == ncyl && fdc_side == nside)
1078 return;
1079
1080 if (towrite)
1081 sc->flags |= FDF_WRITEWAIT;
1082
1083 #ifdef FDDEBUG
1084 printf("fdsetpos: cyl %d head %d towrite %d\n", trk / FDNHEADS,
1085 trk % FDNHEADS, towrite);
1086 #endif
1087 nstep = ncyl - sc->curcyl;
1088 if (nstep) {
1089 /*
1090 * figure direction
1091 */
1092 if (nstep > 0 && ncyl != 0) {
1093 sdir = FDSTEPIN;
1094 FDSETDIR(1);
1095 } else {
1096 nstep = -nstep;
1097 sdir = FDSTEPOUT;
1098 FDSETDIR(0);
1099 }
1100 if (ncyl == 0) {
1101 /*
1102 * either just want cylinder 0 or doing
1103 * a calibrate.
1104 */
1105 nstep = 256;
1106 while (FDTESTC(FDB_CYLZERO) == 0 && nstep--) {
1107 FDSTEP;
1108 delay(sc->stepdelay);
1109 }
1110 if (nstep < 0)
1111 sc->flags |= FDF_NOTRACK0;
1112 } else {
1113 /*
1114 * step the needed amount amount.
1115 */
1116 while (nstep--) {
1117 FDSTEP;
1118 delay(sc->stepdelay);
1119 }
1120 }
1121 /*
1122 * if switched directions
1123 * allow drive to settle.
1124 */
1125 if (sc->pstepdir != sdir)
1126 delay(FDSETTLEDELAY);
1127 sc->pstepdir = sdir;
1128 sc->curcyl = ncyl;
1129 }
1130 if (nside == fdc_side)
1131 return;
1132 /*
1133 * select side
1134 */
1135 fdc_side = nside;
1136 FDSETHEAD(nside);
1137 delay(FDPRESIDEDELAY);
1138 }
1139
1140 void
1141 fdselunit(struct fd_softc *sc)
1142 {
1143 FDDESELECT(FDCUNITMASK); /* deselect all */
1144 FDSETMOTOR(sc->flags & FDF_MOTORON); /* set motor to unit's state */
1145 delay(1);
1146 FDSELECT(sc->unitmask); /* select unit */
1147 delay(1);
1148 }
1149
1150 /*
1151 * process next buf on device queue.
1152 * normall sequence of events:
1153 * fdstart() -> fddmastart();
1154 * fdidxintr();
1155 * fdintr() -> fddmadone() -> fddone();
1156 * if the track is in the cache then fdstart() will short-circuit
1157 * to fddone() else if the track cache is dirty it will flush. If
1158 * the buf is not an entire track it will cache the requested track.
1159 */
1160 void
1161 fdstart(struct fd_softc *sc)
1162 {
1163 int trk, error, write;
1164 struct buf *bp, *dp;
1165 int changed;
1166
1167 #ifdef FDDEBUG
1168 printf("fdstart: unit %d\n", sc->hwunit);
1169 #endif
1170
1171 /*
1172 * if dma'ing just return. we must have been called from fdstartegy.
1173 */
1174 if (fdc_indma)
1175 return;
1176
1177 /*
1178 * get next buf if there.
1179 */
1180 dp = &sc->curbuf;
1181 if ((bp = BUFQ_PEEK(&sc->bufq)) == NULL) {
1182 #ifdef FDDEBUG
1183 printf(" nothing to do\n");
1184 #endif
1185 return;
1186 }
1187
1188 /*
1189 * Mark us as busy now, in case fddone() gets called in one
1190 * of the cases below.
1191 */
1192 disk_busy(&sc->dkdev);
1193
1194 /*
1195 * make sure same disk is loaded
1196 */
1197 fdselunit(sc);
1198 changed = FDTESTC(FDB_CHANGED);
1199 FDDESELECT(sc->unitmask);
1200 if (changed) {
1201 /*
1202 * disk missing, invalidate all future io on
1203 * this unit until re-open()'ed also invalidate
1204 * all current io
1205 */
1206 printf("fdstart: disk changed\n");
1207 #ifdef FDDEBUG
1208 printf(" disk was removed invalidating all io\n");
1209 #endif
1210 sc->flags &= ~FDF_HAVELABEL;
1211 for (;;) {
1212 bp = BUFQ_GET(&sc->bufq);
1213 bp->b_flags |= B_ERROR;
1214 bp->b_error = EIO;
1215 if (BUFQ_PEEK(&sc->bufq) == NULL)
1216 break;
1217 biodone(bp);
1218 }
1219 /*
1220 * do fddone() on last buf to allow other units to start.
1221 */
1222 BUFQ_PUT(&sc->bufq, bp);
1223 fddone(sc);
1224 return;
1225 }
1226
1227 /*
1228 * we have a valid buf, setup our local version
1229 * we use this count to allow reading over multiple tracks.
1230 * into a single buffer
1231 */
1232 dp->b_bcount = bp->b_bcount;
1233 dp->b_blkno = bp->b_blkno;
1234 dp->b_data = bp->b_data;
1235 dp->b_flags = bp->b_flags;
1236 dp->b_resid = 0;
1237
1238 if (bp->b_flags & B_READ)
1239 write = 0;
1240 else if (FDTESTC(FDB_PROTECT) == 0)
1241 write = 1;
1242 else {
1243 error = EPERM;
1244 goto bad;
1245 }
1246
1247 /*
1248 * figure trk given blkno
1249 */
1250 trk = bp->b_blkno / sc->nsectors;
1251
1252 /*
1253 * check to see if same as currently cached track
1254 * if so we need to do no dma read.
1255 */
1256 if (trk == sc->cachetrk) {
1257 fddone(sc);
1258 return;
1259 }
1260
1261 /*
1262 * if we will be overwriting the entire cache, don't bother to
1263 * fetch it.
1264 */
1265 if (bp->b_bcount == (sc->nsectors * FDSECSIZE) && write &&
1266 bp->b_blkno % sc->nsectors == 0) {
1267 if (sc->flags & FDF_DIRTY)
1268 sc->flags |= FDF_JUSTFLUSH;
1269 else {
1270 sc->cachetrk = trk;
1271 fddone(sc);
1272 return;
1273 }
1274 }
1275
1276 /*
1277 * start dma read of `trk'
1278 */
1279 fddmastart(sc, trk);
1280 return;
1281 bad:
1282 bp->b_flags |= B_ERROR;
1283 bp->b_error = error;
1284 fddone(sc);
1285 }
1286
1287 /*
1288 * continue a started operation on next track. always begin at
1289 * sector 0 on the next track.
1290 */
1291 void
1292 fdcont(struct fd_softc *sc)
1293 {
1294 struct buf *dp, *bp;
1295 int trk, write;
1296
1297 dp = &sc->curbuf;
1298 bp = BUFQ_PEEK(&sc->bufq);
1299 dp->b_data += (dp->b_bcount - bp->b_resid);
1300 dp->b_blkno += (dp->b_bcount - bp->b_resid) / FDSECSIZE;
1301 dp->b_bcount = bp->b_resid;
1302
1303 /*
1304 * figure trk given blkno
1305 */
1306 trk = dp->b_blkno / sc->nsectors;
1307 #ifdef DEBUG
1308 if (trk != sc->cachetrk + 1 || dp->b_blkno % sc->nsectors != 0)
1309 panic("fdcont: confused");
1310 #endif
1311 if (dp->b_flags & B_READ)
1312 write = 0;
1313 else
1314 write = 1;
1315 /*
1316 * if we will be overwriting the entire cache, don't bother to
1317 * fetch it.
1318 */
1319 if (dp->b_bcount == (sc->nsectors * FDSECSIZE) && write) {
1320 if (sc->flags & FDF_DIRTY)
1321 sc->flags |= FDF_JUSTFLUSH;
1322 else {
1323 sc->cachetrk = trk;
1324 fddone(sc);
1325 return;
1326 }
1327 }
1328 /*
1329 * start dma read of `trk'
1330 */
1331 fddmastart(sc, trk);
1332 return;
1333 }
1334
1335 void
1336 fddmastart(struct fd_softc *sc, int trk)
1337 {
1338 int adkmask, ndmaw, write, dmatrk;
1339
1340 #ifdef FDDEBUG
1341 printf("fddmastart: unit %d cyl %d head %d", sc->hwunit,
1342 trk / FDNHEADS, trk % FDNHEADS);
1343 #endif
1344 /*
1345 * flush the cached track if dirty else read requested track.
1346 */
1347 if (sc->flags & FDF_DIRTY) {
1348 fdcachetoraw(sc);
1349 ndmaw = sc->type->nwritew;
1350 dmatrk = sc->cachetrk;
1351 write = 1;
1352 } else {
1353 ndmaw = sc->type->nreadw;
1354 dmatrk = trk;
1355 write = 0;
1356 }
1357
1358 #ifdef FDDEBUG
1359 printf(" %s", write ? " flushing cache\n" : " loading cache\n");
1360 #endif
1361 sc->cachetrk = trk;
1362 fdc_indma = sc;
1363 fdsetpos(sc, dmatrk, write);
1364
1365 /*
1366 * setup dma stuff
1367 */
1368 if (write == 0) {
1369 custom.adkcon = ADKF_MSBSYNC;
1370 custom.adkcon = ADKF_SETCLR | ADKF_WORDSYNC | ADKF_FAST;
1371 custom.dsksync = FDMFMSYNC;
1372 } else {
1373 custom.adkcon = ADKF_PRECOMP1 | ADKF_PRECOMP0 | ADKF_WORDSYNC |
1374 ADKF_MSBSYNC;
1375 adkmask = ADKF_SETCLR | ADKF_FAST | ADKF_MFMPREC;
1376 if (dmatrk >= sc->type->precomp[0])
1377 adkmask |= ADKF_PRECOMP0;
1378 if (dmatrk >= sc->type->precomp[1])
1379 adkmask |= ADKF_PRECOMP1;
1380 custom.adkcon = adkmask;
1381 }
1382 custom.dskpt = (u_char *)kvtop(fdc_dmap);
1383
1384 /*
1385 * If writing an MSDOS track, activate disk index pulse
1386 * interrupt, dma will be started in the intr routine fdidxintr()
1387 * Otherwise, start the DMA here.
1388 */
1389 if (write && sc->openpart == FDMSDOSPART) {
1390 fdc_dmalen = ndmaw;
1391 fdc_dmawrite = write;
1392 ciab.icr = CIA_ICR_IR_SC | CIA_ICR_FLG;
1393 } else {
1394 FDDMASTART(ndmaw, write);
1395 fdc_dmalen = 0;
1396 }
1397
1398 #ifdef FDDEBUG
1399 printf(" dma started\n");
1400 #endif
1401 }
1402
1403 /*
1404 * recalibrate the drive
1405 */
1406 void
1407 fdcalibrate(void *arg)
1408 {
1409 struct fd_softc *sc;
1410 static int loopcnt;
1411
1412 sc = arg;
1413
1414 if (loopcnt == 0) {
1415 /*
1416 * seek cyl 0
1417 */
1418 fdc_indma = sc;
1419 sc->stepdelay += 900;
1420 if (sc->cachetrk > 1)
1421 fdsetpos(sc, sc->cachetrk % FDNHEADS, 0);
1422 sc->stepdelay -= 900;
1423 }
1424 if (loopcnt++ & 1)
1425 fdsetpos(sc, sc->cachetrk, 0);
1426 else
1427 fdsetpos(sc, sc->cachetrk + FDNHEADS, 0);
1428 /*
1429 * trk++, trk, trk++, trk, trk++, trk, trk++, trk and dma
1430 */
1431 if (loopcnt < 8)
1432 callout_reset(&sc->calibrate_ch, hz / 8, fdcalibrate, sc);
1433 else {
1434 loopcnt = 0;
1435 fdc_indma = NULL;
1436 callout_reset(&sc->motor_ch, 3 * hz / 2, fdmotoroff, sc);
1437 fddmastart(sc, sc->cachetrk);
1438 }
1439 }
1440
1441 void
1442 fddmadone(struct fd_softc *sc, int timeo)
1443 {
1444 #ifdef FDDEBUG
1445 printf("fddmadone: unit %d, timeo %d\n", sc->hwunit, timeo);
1446 #endif
1447 fdc_indma = NULL;
1448 callout_stop(&sc->motor_ch);
1449 FDDMASTOP;
1450
1451 /*
1452 * guarantee the drive has been at current head and cyl
1453 * for at least FDWRITEDELAY after a write.
1454 */
1455 if (sc->flags & FDF_WRITEWAIT) {
1456 delay(FDWRITEDELAY);
1457 sc->flags &= ~FDF_WRITEWAIT;
1458 }
1459
1460 if ((sc->flags & FDF_MOTOROFF) == 0) {
1461 /*
1462 * motor runs for 1.5 seconds after last dma
1463 */
1464 callout_reset(&sc->motor_ch, 3 * hz / 2, fdmotoroff, sc);
1465 }
1466 if (sc->flags & FDF_DIRTY) {
1467 /*
1468 * if buffer dirty, the last dma cleaned it
1469 */
1470 sc->flags &= ~FDF_DIRTY;
1471 if (timeo)
1472 printf("%s: write of track cache timed out.\n",
1473 sc->sc_dv.dv_xname);
1474 if (sc->flags & FDF_JUSTFLUSH) {
1475 sc->flags &= ~FDF_JUSTFLUSH;
1476 /*
1477 * we are done dma'ing
1478 */
1479 fddone(sc);
1480 return;
1481 }
1482 /*
1483 * load the cache
1484 */
1485 fddmastart(sc, sc->cachetrk);
1486 return;
1487 }
1488 #ifdef FDDEBUG
1489 else if (sc->flags & FDF_MOTOROFF)
1490 panic("fddmadone: FDF_MOTOROFF with no FDF_DIRTY");
1491 #endif
1492
1493 /*
1494 * cache loaded decode it into cache buffer
1495 */
1496 if (timeo == 0 && fdrawtocache(sc) == 0)
1497 sc->retried = 0;
1498 else {
1499 #ifdef FDDEBUG
1500 if (timeo)
1501 printf("%s: fddmadone: cache load timed out.\n",
1502 sc->sc_dv.dv_xname);
1503 #endif
1504 if (sc->retried >= sc->retries) {
1505 sc->retried = 0;
1506 sc->cachetrk = -1;
1507 } else {
1508 sc->retried++;
1509 /*
1510 * this will be restarted at end of calibrate loop.
1511 */
1512 callout_stop(&sc->motor_ch);
1513 fdcalibrate(sc);
1514 return;
1515 }
1516 }
1517 fddone(sc);
1518 }
1519
1520 void
1521 fddone(struct fd_softc *sc)
1522 {
1523 struct buf *dp, *bp;
1524 char *data;
1525 int sz;
1526
1527 #ifdef FDDEBUG
1528 printf("fddone: unit %d\n", sc->hwunit);
1529 #endif
1530 /*
1531 * check to see if unit is just flushing the cache,
1532 * that is we have no io queued.
1533 */
1534 if (sc->flags & FDF_MOTOROFF)
1535 goto nobuf;
1536
1537 dp = &sc->curbuf;
1538 if ((bp = BUFQ_PEEK(&sc->bufq)) == NULL)
1539 panic ("fddone");
1540 /*
1541 * check for an error that may have occurred
1542 * while getting the track.
1543 */
1544 if (sc->cachetrk == -1) {
1545 sc->retried = 0;
1546 bp->b_flags |= B_ERROR;
1547 bp->b_error = EIO;
1548 } else if ((bp->b_flags & 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(sc->sc_dv.dv_unit);
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 %d", bp->b_bcount);
1670 #endif
1671 bp->b_bcount = min(bp->b_bcount, tsz - toff);
1672 #ifdef FDDEBUG
1673 printf(" after %d\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%x hchksum 0x%x trkhcksum 0x%x\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%x\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%x dchksum 0x%x trkdcksum 0x%x\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