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