fd.c revision 1.84.4.2 1 /* $NetBSD: fd.c,v 1.84.4.2 2011/06/12 00:23:52 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.2 2011/06/12 00:23:52 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 device_t sc_dev; /* 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(device_t, cfdata_t, void *);
193 void fdcattach(device_t, device_t, void *);
194 int fdcprint(void *, const char *);
195 int fdmatch(device_t, cfdata_t, void *);
196 void fdattach(device_t, device_t, 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_NEW(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_NEW(fdc, 0,
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(device_t pdp, cfdata_t 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(device_t pdp, device_t 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(device_t pdp, cfdata_t 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(device_t pdp, device_t 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 sc->sc_dev = dp;
399
400 bufq_alloc(&sc->bufq, "disksort", BUFQ_SORT_CYLINDER);
401 callout_init(&sc->calibrate_ch, 0);
402 callout_init(&sc->motor_ch, 0);
403
404 sc->curcyl = sc->cachetrk = -1;
405 sc->openpart = -1;
406 sc->type = ap->type;
407 sc->hwunit = ap->unit;
408 sc->unitmask = 1 << (3 + ap->unit);
409 sc->retries = FDRETRIES;
410 sc->stepdelay = FDSTEPDELAY;
411 sc->bytespersec = 512;
412 printf(" unit %d: %s %d cyl, %d head, %d sec [%d sec], 512 bytes/sec\n",
413 sc->hwunit, sc->type->desc, sc->type->ncylinders, FDNHEADS,
414 sc->type->amiga_nsectors, sc->type->msdos_nsectors);
415
416 /*
417 * Initialize and attach the disk structure.
418 */
419 disk_init(&sc->dkdev, device_xname(sc->sc_dev), &fddkdriver);
420 disk_attach(&sc->dkdev);
421
422 /*
423 * calibrate the drive
424 */
425 fdsetpos(sc, 0, 0);
426 fdsetpos(sc, sc->type->ncylinders, 0);
427 fdsetpos(sc, 0, 0);
428 fdmotoroff(sc);
429
430 /*
431 * precalc msdos MFM and CRC
432 */
433 for (i = 0; i < 128; i++)
434 msdecode[i] = 0xff;
435 for (i = 0; i < 16; i++)
436 msdecode[msencode[i]] = i;
437 for (i = 0; i < 256; i++) {
438 mscrctab[i] = (0x1021 * (i & 0xf0)) ^ (0x1021 * (i & 0x0f)) ^
439 (0x1021 * (i >> 4));
440 }
441
442 /*
443 * enable disk related interrupts
444 */
445 custom.dmacon = DMAF_SETCLR | DMAF_MASTER | DMAF_DISK;
446 custom.intena = INTF_SETCLR | INTF_DSKBLK;
447 ciab.icr = CIA_ICR_FLG;
448 }
449
450 /*ARGSUSED*/
451 int
452 fdopen(dev_t dev, int flags, int devtype, struct lwp *l)
453 {
454 struct fd_softc *sc;
455 int wasopen, fwork, error, s;
456
457 error = 0;
458
459 if (FDPART(dev) >= FDMAXPARTS)
460 return(ENXIO);
461
462 if ((sc = getsoftc(fd_cd, FDUNIT(dev))) == NULL)
463 return(ENXIO);
464 if (sc->flags & FDF_NOTRACK0)
465 return(ENXIO);
466 if (sc->cachep == NULL)
467 sc->cachep = malloc(MAXTRKSZ, M_DEVBUF, M_WAITOK);
468
469 s = splbio();
470 /*
471 * if we are sleeping in fdclose(); waiting for a chance to
472 * shut the motor off, do a sleep here also.
473 */
474 while (sc->flags & FDF_WMOTOROFF)
475 tsleep(fdmotoroff, PRIBIO, "fdopen", 0);
476
477 fwork = 0;
478 /*
479 * if not open let user open request type, otherwise
480 * ensure they are trying to open same type.
481 */
482 if (sc->openpart == FDPART(dev))
483 wasopen = 1;
484 else if (sc->openpart == -1) {
485 sc->openpart = FDPART(dev);
486 wasopen = 0;
487 } else {
488 wasopen = 1;
489 error = EPERM;
490 goto done;
491 }
492
493 /*
494 * wait for current io to complete if any
495 */
496 if (fdc_indma) {
497 fwork = 1;
498 fdc_wantwakeup++;
499 tsleep(fdopen, PRIBIO, "fdopen", 0);
500 }
501 if ((error = fdloaddisk(sc)) != 0)
502 goto done;
503 if ((error = fdgetdisklabel(sc, dev)) != 0)
504 goto done;
505 #ifdef FDDEBUG
506 printf(" open successful\n");
507 #endif
508 done:
509 /*
510 * if we requested that fddone()->fdfindwork() wake us, allow it to
511 * complete its job now
512 */
513 if (fwork)
514 fdfindwork(FDUNIT(dev));
515 splx(s);
516
517 /*
518 * if we were not open and we marked us so reverse that.
519 */
520 if (error && wasopen == 0)
521 sc->openpart = -1;
522 return(error);
523 }
524
525 /*ARGSUSED*/
526 int
527 fdclose(dev_t dev, int flags, int devtype, struct lwp *l)
528 {
529 struct fd_softc *sc;
530 int s;
531
532 #ifdef FDDEBUG
533 printf("fdclose()\n");
534 #endif
535 sc = getsoftc(fd_cd, FDUNIT(dev));
536 s = splbio();
537 if (sc->flags & FDF_MOTORON) {
538 sc->flags |= FDF_WMOTOROFF;
539 tsleep(fdmotoroff, PRIBIO, "fdclose", 0);
540 sc->flags &= ~FDF_WMOTOROFF;
541 wakeup(fdmotoroff);
542 }
543 sc->openpart = -1;
544 splx(s);
545 return(0);
546 }
547
548 int
549 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
550 {
551 struct fd_softc *sc;
552 int error, wlab;
553
554 sc = getsoftc(fd_cd, FDUNIT(dev));
555
556 if ((sc->flags & FDF_HAVELABEL) == 0)
557 return(EBADF);
558
559 switch (cmd) {
560 case DIOCSBAD:
561 return(EINVAL);
562 case DIOCSRETRIES:
563 if (*(int *)addr < 0)
564 return(EINVAL);
565 sc->retries = *(int *)addr;
566 return(0);
567 case DIOCSSTEP:
568 if (*(int *)addr < FDSTEPDELAY)
569 return(EINVAL);
570 sc->dkdev.dk_label->d_trkseek = sc->stepdelay = *(int *)addr;
571 return(0);
572 case DIOCGDINFO:
573 *(struct disklabel *)addr = *(sc->dkdev.dk_label);
574 return(0);
575 case DIOCGPART:
576 ((struct partinfo *)addr)->disklab = sc->dkdev.dk_label;
577 ((struct partinfo *)addr)->part =
578 &sc->dkdev.dk_label->d_partitions[FDPART(dev)];
579 return(0);
580 case DIOCSDINFO:
581 if ((flag & FWRITE) == 0)
582 return(EBADF);
583 return(fdsetdisklabel(sc, (struct disklabel *)addr));
584 case DIOCWDINFO:
585 if ((flag & FWRITE) == 0)
586 return(EBADF);
587 if ((error = fdsetdisklabel(sc, (struct disklabel *)addr)) != 0)
588 return(error);
589 wlab = sc->wlabel;
590 sc->wlabel = 1;
591 error = fdputdisklabel(sc, dev);
592 sc->wlabel = wlab;
593 return(error);
594 case DIOCWLABEL:
595 if ((flag & FWRITE) == 0)
596 return(EBADF);
597 sc->wlabel = *(int *)addr;
598 return(0);
599 case DIOCGDEFLABEL:
600 fdgetdefaultlabel(sc, (struct disklabel *)addr, FDPART(dev));
601 return(0);
602 default:
603 return(ENOTTY);
604 }
605 }
606
607 int
608 fdread(dev_t dev, struct uio *uio, int flags)
609 {
610 return (physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
611 }
612
613 int
614 fdwrite(dev_t dev, struct uio *uio, int flags)
615 {
616 return (physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
617 }
618
619
620 void
621 fdintr(int flag)
622 {
623 int s;
624
625 s = splbio();
626 if (fdc_indma)
627 fddmadone(fdc_indma, 0);
628 splx(s);
629 }
630
631 void
632 fdidxintr(void)
633 {
634 if (fdc_indma && fdc_dmalen) {
635 /*
636 * turn off intr and start actual dma
637 */
638 ciab.icr = CIA_ICR_FLG;
639 FDDMASTART(fdc_dmalen, fdc_dmawrite);
640 fdc_dmalen = 0;
641 }
642 }
643
644 void
645 fdstrategy(struct buf *bp)
646 {
647 struct disklabel *lp;
648 struct fd_softc *sc;
649 int unit, part, s;
650
651 unit = FDUNIT(bp->b_dev);
652 part = FDPART(bp->b_dev);
653 sc = getsoftc(fd_cd, unit);
654
655 #ifdef FDDEBUG
656 printf("fdstrategy: %p\n", bp);
657 #endif
658 /*
659 * check for valid partition and bounds
660 */
661 lp = sc->dkdev.dk_label;
662 if ((sc->flags & FDF_HAVELABEL) == 0) {
663 bp->b_error = EIO;
664 goto done;
665 }
666 if (bounds_check_with_label(&sc->dkdev, bp, sc->wlabel) <= 0)
667 goto done;
668
669 /*
670 * trans count of zero or bounds check indicates io is done
671 * we are done.
672 */
673 if (bp->b_bcount == 0)
674 goto done;
675
676 bp->b_rawblkno = bp->b_blkno;
677
678 /*
679 * queue the buf and kick the low level code
680 */
681 s = splbio();
682 bufq_put(sc->bufq, bp);
683 fdstart(sc);
684 splx(s);
685 return;
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 memset(lp, 0, 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 memset(lp, 0, sizeof(struct disklabel));
775 memset(clp, 0, 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 *)((char*)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 memcpy(lp, dlp, sizeof(struct disklabel));
808 if (lp->d_trkseek > FDSTEPDELAY)
809 sc->stepdelay = lp->d_trkseek;
810 brelse(bp, 0);
811 return(0);
812 nolabel:
813 fdgetdefaultlabel(sc, lp, part);
814 brelse(bp, 0);
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 PAGE_SIZE currently
870 */
871 if ((pp->p_offset + pp->p_size >= lp->d_secperunit) ||
872 (pp->p_frag * pp->p_fsize % PAGE_SIZE))
873 return(EINVAL);
874 done:
875 memcpy(clp, lp, 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 synchronous
909 */
910 dlp = (struct disklabel *)((char*)bp->b_data + LABELOFFSET);
911 memcpy(dlp, lp, sizeof(struct disklabel));
912 bp->b_blkno = 0;
913 bp->b_cylinder = 0;
914 bp->b_flags &= ~(B_READ);
915 bp->b_oflags &= ~(BO_DONE);
916 bp->b_flags |= B_WRITE;
917 fdstrategy(bp);
918 error = biowait(bp);
919 done:
920 brelse(bp, 0);
921 return(error);
922 }
923
924 /*
925 * figure out drive type or NULL if none.
926 */
927 struct fdtype *
928 fdcgetfdtype(int unit)
929 {
930 struct fdtype *ftp;
931 u_long id, idb;
932 int cnt, umask;
933
934 id = 0;
935 umask = 1 << (3 + unit);
936
937 FDDESELECT(FDCUNITMASK);
938
939 FDSETMOTOR(1);
940 delay(1);
941 FDSELECT(umask);
942 delay(1);
943 FDDESELECT(umask);
944
945 FDSETMOTOR(0);
946 delay(1);
947 FDSELECT(umask);
948 delay(1);
949 FDDESELECT(umask);
950
951 for (idb = 0x80000000; idb; idb >>= 1) {
952 FDSELECT(umask);
953 delay(1);
954 if (FDTESTC(FDB_READY) == 0)
955 id |= idb;
956 FDDESELECT(umask);
957 delay(1);
958 }
959 #ifdef FDDEBUG
960 printf("fdcgettype unit %d id 0x%lx\n", unit, id);
961 #endif
962
963 for (cnt = 0, ftp = fdtype; cnt < nfdtype; ftp++, cnt++)
964 if (ftp->driveid == id)
965 return(ftp);
966 /*
967 * 3.5dd's at unit 0 do not always return id.
968 */
969 if (unit == 0)
970 return(fdtype);
971 return(NULL);
972 }
973
974 /*
975 * turn motor off if possible otherwise mark as needed and will be done
976 * later.
977 */
978 void
979 fdmotoroff(void *arg)
980 {
981 struct fd_softc *sc;
982 int s;
983
984 sc = arg;
985 s = splbio();
986
987 #ifdef FDDEBUG
988 printf("fdmotoroff: unit %d\n", sc->hwunit);
989 #endif
990 if ((sc->flags & FDF_MOTORON) == 0)
991 goto done;
992 /*
993 * if we have a timeout on a DMA operation let fddmadone()
994 * deal with it.
995 */
996 if (fdc_indma == sc) {
997 fddmadone(sc, 1);
998 goto done;
999 }
1000 #ifdef FDDEBUG
1001 printf(" motor was on, turning off\n");
1002 #endif
1003
1004 /*
1005 * flush cache if needed
1006 */
1007 if (sc->flags & FDF_DIRTY) {
1008 sc->flags |= FDF_JUSTFLUSH | FDF_MOTOROFF;
1009 #ifdef FDDEBUG
1010 printf(" flushing dirty buffer first\n");
1011 #endif
1012 /*
1013 * if DMA'ing done for now, fddone() will call us again
1014 */
1015 if (fdc_indma)
1016 goto done;
1017 fddmastart(sc, sc->cachetrk);
1018 goto done;
1019 }
1020
1021 /*
1022 * if controller is busy just schedule us to be called back
1023 */
1024 if (fdc_indma) {
1025 /*
1026 * someone else has the controller now
1027 * just set flag and let fddone() call us again.
1028 */
1029 sc->flags |= FDF_MOTOROFF;
1030 goto done;
1031 }
1032
1033 #ifdef FDDEBUG
1034 printf(" hw turning unit off\n");
1035 #endif
1036
1037 sc->flags &= ~(FDF_MOTORON | FDF_MOTOROFF);
1038 FDDESELECT(FDCUNITMASK);
1039 FDSETMOTOR(0);
1040 delay(1);
1041 FDSELECT(sc->unitmask);
1042 delay(4);
1043 FDDESELECT(sc->unitmask);
1044 delay(1);
1045 if (sc->flags & FDF_WMOTOROFF)
1046 wakeup(fdmotoroff);
1047 done:
1048 splx(s);
1049 }
1050
1051 /*
1052 * select drive seek to track exit with motor on.
1053 * fdsetpos(x, 0, 0) does calibrates the drive.
1054 */
1055 void
1056 fdsetpos(struct fd_softc *sc, int trk, int towrite)
1057 {
1058 int nstep, sdir, ondly, ncyl, nside;
1059
1060 FDDESELECT(FDCUNITMASK);
1061 FDSETMOTOR(1);
1062 delay(1);
1063 FDSELECT(sc->unitmask);
1064 delay(1);
1065 if ((sc->flags & FDF_MOTORON) == 0) {
1066 ondly = 0;
1067 while (FDTESTC(FDB_READY) == 0) {
1068 delay(1000);
1069 if (++ondly >= 1000)
1070 break;
1071 }
1072 }
1073 sc->flags |= FDF_MOTORON;
1074
1075 ncyl = trk / FDNHEADS;
1076 nside = trk % FDNHEADS;
1077
1078 if (sc->curcyl == ncyl && fdc_side == nside)
1079 return;
1080
1081 if (towrite)
1082 sc->flags |= FDF_WRITEWAIT;
1083
1084 #ifdef FDDEBUG
1085 printf("fdsetpos: cyl %d head %d towrite %d\n", trk / FDNHEADS,
1086 trk % FDNHEADS, towrite);
1087 #endif
1088 nstep = ncyl - sc->curcyl;
1089 if (nstep) {
1090 /*
1091 * figure direction
1092 */
1093 if (nstep > 0 && ncyl != 0) {
1094 sdir = FDSTEPIN;
1095 FDSETDIR(1);
1096 } else {
1097 nstep = -nstep;
1098 sdir = FDSTEPOUT;
1099 FDSETDIR(0);
1100 }
1101 if (ncyl == 0) {
1102 /*
1103 * either just want cylinder 0 or doing
1104 * a calibrate.
1105 */
1106 nstep = 256;
1107 while (FDTESTC(FDB_CYLZERO) == 0 && nstep--) {
1108 FDSTEP;
1109 delay(sc->stepdelay);
1110 }
1111 if (nstep < 0)
1112 sc->flags |= FDF_NOTRACK0;
1113 } else {
1114 /*
1115 * step the needed amount amount.
1116 */
1117 while (nstep--) {
1118 FDSTEP;
1119 delay(sc->stepdelay);
1120 }
1121 }
1122 /*
1123 * if switched directions
1124 * allow drive to settle.
1125 */
1126 if (sc->pstepdir != sdir)
1127 delay(FDSETTLEDELAY);
1128 sc->pstepdir = sdir;
1129 sc->curcyl = ncyl;
1130 }
1131 if (nside == fdc_side)
1132 return;
1133 /*
1134 * select side
1135 */
1136 fdc_side = nside;
1137 FDSETHEAD(nside);
1138 delay(FDPRESIDEDELAY);
1139 }
1140
1141 void
1142 fdselunit(struct fd_softc *sc)
1143 {
1144 FDDESELECT(FDCUNITMASK); /* deselect all */
1145 FDSETMOTOR(sc->flags & FDF_MOTORON); /* set motor to unit's state */
1146 delay(1);
1147 FDSELECT(sc->unitmask); /* select unit */
1148 delay(1);
1149 }
1150
1151 /*
1152 * process next buf on device queue.
1153 * normall sequence of events:
1154 * fdstart() -> fddmastart();
1155 * fdidxintr();
1156 * fdintr() -> fddmadone() -> fddone();
1157 * if the track is in the cache then fdstart() will short-circuit
1158 * to fddone() else if the track cache is dirty it will flush. If
1159 * the buf is not an entire track it will cache the requested track.
1160 */
1161 void
1162 fdstart(struct fd_softc *sc)
1163 {
1164 int trk, error, write;
1165 struct buf *bp, *dp;
1166 int changed;
1167
1168 #ifdef FDDEBUG
1169 printf("fdstart: unit %d\n", sc->hwunit);
1170 #endif
1171
1172 /*
1173 * if DMA'ing just return. we must have been called from fdstartegy.
1174 */
1175 if (fdc_indma)
1176 return;
1177
1178 /*
1179 * get next buf if there.
1180 */
1181 dp = &sc->curbuf;
1182 if ((bp = bufq_peek(sc->bufq)) == NULL) {
1183 #ifdef FDDEBUG
1184 printf(" nothing to do\n");
1185 #endif
1186 return;
1187 }
1188
1189 /*
1190 * Mark us as busy now, in case fddone() gets called in one
1191 * of the cases below.
1192 */
1193 disk_busy(&sc->dkdev);
1194
1195 /*
1196 * make sure same disk is loaded
1197 */
1198 fdselunit(sc);
1199 changed = FDTESTC(FDB_CHANGED);
1200 FDDESELECT(sc->unitmask);
1201 if (changed) {
1202 /*
1203 * disk missing, invalidate all future io on
1204 * this unit until re-open()'ed also invalidate
1205 * all current io
1206 */
1207 printf("fdstart: disk changed\n");
1208 #ifdef FDDEBUG
1209 printf(" disk was removed invalidating all io\n");
1210 #endif
1211 sc->flags &= ~FDF_HAVELABEL;
1212 for (;;) {
1213 bp = bufq_get(sc->bufq);
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 done;
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 done:
1282 bp->b_error = error;
1283 fddone(sc);
1284 }
1285
1286 /*
1287 * continue a started operation on next track. always begin at
1288 * sector 0 on the next track.
1289 */
1290 void
1291 fdcont(struct fd_softc *sc)
1292 {
1293 struct buf *dp, *bp;
1294 int trk, write;
1295
1296 dp = &sc->curbuf;
1297 bp = bufq_peek(sc->bufq);
1298 dp->b_data = (char*)dp->b_data + (dp->b_bcount - bp->b_resid);
1299 dp->b_blkno += (dp->b_bcount - bp->b_resid) / FDSECSIZE;
1300 dp->b_bcount = bp->b_resid;
1301
1302 /*
1303 * figure trk given blkno
1304 */
1305 trk = dp->b_blkno / sc->nsectors;
1306 #ifdef DEBUG
1307 if (trk != sc->cachetrk + 1 || dp->b_blkno % sc->nsectors != 0)
1308 panic("fdcont: confused");
1309 #endif
1310 if (dp->b_flags & B_READ)
1311 write = 0;
1312 else
1313 write = 1;
1314 /*
1315 * if we will be overwriting the entire cache, don't bother to
1316 * fetch it.
1317 */
1318 if (dp->b_bcount == (sc->nsectors * FDSECSIZE) && write) {
1319 if (sc->flags & FDF_DIRTY)
1320 sc->flags |= FDF_JUSTFLUSH;
1321 else {
1322 sc->cachetrk = trk;
1323 fddone(sc);
1324 return;
1325 }
1326 }
1327 /*
1328 * start DMA read of `trk'
1329 */
1330 fddmastart(sc, trk);
1331 return;
1332 }
1333
1334 void
1335 fddmastart(struct fd_softc *sc, int trk)
1336 {
1337 int adkmask, ndmaw, write, dmatrk;
1338
1339 #ifdef FDDEBUG
1340 printf("fddmastart: unit %d cyl %d head %d", sc->hwunit,
1341 trk / FDNHEADS, trk % FDNHEADS);
1342 #endif
1343 /*
1344 * flush the cached track if dirty else read requested track.
1345 */
1346 if (sc->flags & FDF_DIRTY) {
1347 fdcachetoraw(sc);
1348 ndmaw = sc->type->nwritew;
1349 dmatrk = sc->cachetrk;
1350 write = 1;
1351 } else {
1352 ndmaw = sc->type->nreadw;
1353 dmatrk = trk;
1354 write = 0;
1355 }
1356
1357 #ifdef FDDEBUG
1358 printf(" %s", write ? " flushing cache\n" : " loading cache\n");
1359 #endif
1360 sc->cachetrk = trk;
1361 fdc_indma = sc;
1362 fdsetpos(sc, dmatrk, write);
1363
1364 /*
1365 * setup dma stuff
1366 */
1367 if (write == 0) {
1368 custom.adkcon = ADKF_MSBSYNC;
1369 custom.adkcon = ADKF_SETCLR | ADKF_WORDSYNC | ADKF_FAST;
1370 custom.dsksync = FDMFMSYNC;
1371 } else {
1372 custom.adkcon = ADKF_PRECOMP1 | ADKF_PRECOMP0 | ADKF_WORDSYNC |
1373 ADKF_MSBSYNC;
1374 adkmask = ADKF_SETCLR | ADKF_FAST | ADKF_MFMPREC;
1375 if (dmatrk >= sc->type->precomp[0])
1376 adkmask |= ADKF_PRECOMP0;
1377 if (dmatrk >= sc->type->precomp[1])
1378 adkmask |= ADKF_PRECOMP1;
1379 custom.adkcon = adkmask;
1380 }
1381 custom.dskpt = (u_char *)kvtop(fdc_dmap);
1382
1383 /*
1384 * If writing an MSDOS track, activate disk index pulse
1385 * interrupt, DMA will be started in the intr routine fdidxintr()
1386 * Otherwise, start the DMA here.
1387 */
1388 if (write && sc->openpart == FDMSDOSPART) {
1389 fdc_dmalen = ndmaw;
1390 fdc_dmawrite = write;
1391 ciab.icr = CIA_ICR_IR_SC | CIA_ICR_FLG;
1392 } else {
1393 FDDMASTART(ndmaw, write);
1394 fdc_dmalen = 0;
1395 }
1396
1397 #ifdef FDDEBUG
1398 printf(" DMA started\n");
1399 #endif
1400 }
1401
1402 /*
1403 * recalibrate the drive
1404 */
1405 void
1406 fdcalibrate(void *arg)
1407 {
1408 struct fd_softc *sc;
1409 static int loopcnt;
1410
1411 sc = arg;
1412
1413 if (loopcnt == 0) {
1414 /*
1415 * seek cyl 0
1416 */
1417 fdc_indma = sc;
1418 sc->stepdelay += 900;
1419 if (sc->cachetrk > 1)
1420 fdsetpos(sc, sc->cachetrk % FDNHEADS, 0);
1421 sc->stepdelay -= 900;
1422 }
1423 if (loopcnt++ & 1)
1424 fdsetpos(sc, sc->cachetrk, 0);
1425 else
1426 fdsetpos(sc, sc->cachetrk + FDNHEADS, 0);
1427 /*
1428 * trk++, trk, trk++, trk, trk++, trk, trk++, trk and DMA
1429 */
1430 if (loopcnt < 8)
1431 callout_reset(&sc->calibrate_ch, hz / 8, fdcalibrate, sc);
1432 else {
1433 loopcnt = 0;
1434 fdc_indma = NULL;
1435 callout_reset(&sc->motor_ch, 3 * hz / 2, fdmotoroff, sc);
1436 fddmastart(sc, sc->cachetrk);
1437 }
1438 }
1439
1440 void
1441 fddmadone(struct fd_softc *sc, int timeo)
1442 {
1443 #ifdef FDDEBUG
1444 printf("fddmadone: unit %d, timeo %d\n", sc->hwunit, timeo);
1445 #endif
1446 fdc_indma = NULL;
1447 callout_stop(&sc->motor_ch);
1448 FDDMASTOP;
1449
1450 /*
1451 * guarantee the drive has been at current head and cyl
1452 * for at least FDWRITEDELAY after a write.
1453 */
1454 if (sc->flags & FDF_WRITEWAIT) {
1455 delay(FDWRITEDELAY);
1456 sc->flags &= ~FDF_WRITEWAIT;
1457 }
1458
1459 if ((sc->flags & FDF_MOTOROFF) == 0) {
1460 /*
1461 * motor runs for 1.5 seconds after last DMA
1462 */
1463 callout_reset(&sc->motor_ch, 3 * hz / 2, fdmotoroff, sc);
1464 }
1465 if (sc->flags & FDF_DIRTY) {
1466 /*
1467 * if buffer dirty, the last DMA cleaned it
1468 */
1469 sc->flags &= ~FDF_DIRTY;
1470 if (timeo)
1471 aprint_error_dev(sc->sc_dev,
1472 "write of track cache timed out.\n");
1473 if (sc->flags & FDF_JUSTFLUSH) {
1474 sc->flags &= ~FDF_JUSTFLUSH;
1475 /*
1476 * we are done DMA'ing
1477 */
1478 fddone(sc);
1479 return;
1480 }
1481 /*
1482 * load the cache
1483 */
1484 fddmastart(sc, sc->cachetrk);
1485 return;
1486 }
1487 #ifdef FDDEBUG
1488 else if (sc->flags & FDF_MOTOROFF)
1489 panic("fddmadone: FDF_MOTOROFF with no FDF_DIRTY");
1490 #endif
1491
1492 /*
1493 * cache loaded decode it into cache buffer
1494 */
1495 if (timeo == 0 && fdrawtocache(sc) == 0)
1496 sc->retried = 0;
1497 else {
1498 #ifdef FDDEBUG
1499 if (timeo)
1500 aprint_debug_dev(sc->sc_dev,
1501 "fddmadone: cache load timed out.\n");
1502 #endif
1503 if (sc->retried >= sc->retries) {
1504 sc->retried = 0;
1505 sc->cachetrk = -1;
1506 } else {
1507 sc->retried++;
1508 /*
1509 * this will be restarted at end of calibrate loop.
1510 */
1511 callout_stop(&sc->motor_ch);
1512 fdcalibrate(sc);
1513 return;
1514 }
1515 }
1516 fddone(sc);
1517 }
1518
1519 void
1520 fddone(struct fd_softc *sc)
1521 {
1522 struct buf *dp, *bp;
1523 char *data;
1524 int sz;
1525
1526 #ifdef FDDEBUG
1527 printf("fddone: unit %d\n", sc->hwunit);
1528 #endif
1529 /*
1530 * check to see if unit is just flushing the cache,
1531 * that is we have no io queued.
1532 */
1533 if (sc->flags & FDF_MOTOROFF)
1534 goto nobuf;
1535
1536 dp = &sc->curbuf;
1537 if ((bp = bufq_peek(sc->bufq)) == NULL)
1538 panic ("fddone");
1539 /*
1540 * check for an error that may have occurred
1541 * while getting the track.
1542 */
1543 if (sc->cachetrk == -1) {
1544 sc->retried = 0;
1545 bp->b_error = EIO;
1546 } else if (bp->b_error == 0) {
1547 data = sc->cachep;
1548 /*
1549 * get offset of data in track cache and limit
1550 * the copy size to not exceed the cache's end.
1551 */
1552 data += (dp->b_blkno % sc->nsectors) * FDSECSIZE;
1553 sz = sc->nsectors - dp->b_blkno % sc->nsectors;
1554 sz *= FDSECSIZE;
1555 sz = min(dp->b_bcount, sz);
1556 if (bp->b_flags & B_READ)
1557 memcpy(dp->b_data, data, sz);
1558 else {
1559 memcpy(data, dp->b_data, sz);
1560 sc->flags |= FDF_DIRTY;
1561 }
1562 bp->b_resid = dp->b_bcount - sz;
1563 if (bp->b_resid == 0) {
1564 bp->b_error = 0;
1565 } else {
1566 /*
1567 * not done yet need to read next track
1568 */
1569 fdcont(sc);
1570 return;
1571 }
1572 }
1573 /*
1574 * remove from queue.
1575 */
1576 (void)bufq_get(sc->bufq);
1577
1578 disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid),
1579 (bp->b_flags & B_READ));
1580
1581 biodone(bp);
1582 nobuf:
1583 fdfindwork(device_unit(sc->sc_dev));
1584 }
1585
1586 void
1587 fdfindwork(int unit)
1588 {
1589 struct fd_softc *ssc, *sc;
1590 int i, last;
1591
1592 /*
1593 * first see if we have any fdopen()'s waiting
1594 */
1595 if (fdc_wantwakeup) {
1596 wakeup(fdopen);
1597 fdc_wantwakeup--;
1598 return;
1599 }
1600
1601 /*
1602 * start next available unit, linear search from the next unit
1603 * wrapping and finally this unit.
1604 */
1605 last = 0;
1606 ssc = NULL;
1607 for (i = unit + 1; last == 0; i++) {
1608 if (i == unit)
1609 last = 1;
1610 if (i >= fd_cd.cd_ndevs) {
1611 i = -1;
1612 continue;
1613 }
1614 if ((sc = device_lookup_private(&fd_cd, i)) == NULL)
1615 continue;
1616
1617 /*
1618 * if unit has requested to be turned off
1619 * and it has no buf's queued do it now
1620 */
1621 if (sc->flags & FDF_MOTOROFF) {
1622 if (bufq_peek(sc->bufq) == NULL)
1623 fdmotoroff(sc);
1624 else {
1625 /*
1626 * we gained a buf request while
1627 * we waited, forget the motoroff
1628 */
1629 sc->flags &= ~FDF_MOTOROFF;
1630 }
1631 /*
1632 * if we now have DMA unit must have needed
1633 * flushing, quit
1634 */
1635 if (fdc_indma)
1636 return;
1637 }
1638 /*
1639 * if we have no start unit and the current unit has
1640 * io waiting choose this unit to start.
1641 */
1642 if (ssc == NULL && bufq_peek(sc->bufq) != NULL)
1643 ssc = sc;
1644 }
1645 if (ssc)
1646 fdstart(ssc);
1647 }
1648
1649 /*
1650 * min byte count to whats left of the track in question
1651 */
1652 void
1653 fdminphys(struct buf *bp)
1654 {
1655 struct fd_softc *sc;
1656 int trk, sec, toff, tsz;
1657
1658 if ((sc = getsoftc(fd_cd, FDUNIT(bp->b_dev))) == NULL)
1659 panic("fdminphys: couldn't get softc");
1660
1661 trk = bp->b_blkno / sc->nsectors;
1662 sec = bp->b_blkno % sc->nsectors;
1663
1664 toff = sec * FDSECSIZE;
1665 tsz = sc->nsectors * FDSECSIZE;
1666 #ifdef FDDEBUG
1667 printf("fdminphys: before %ld", bp->b_bcount);
1668 #endif
1669 bp->b_bcount = min(bp->b_bcount, tsz - toff);
1670 #ifdef FDDEBUG
1671 printf(" after %ld\n", bp->b_bcount);
1672 #endif
1673 minphys(bp);
1674 }
1675
1676 /*
1677 * encode the track cache into raw MFM ready for DMA
1678 * when we go to multiple disk formats, this will call type dependent
1679 * functions
1680 */
1681 void fdcachetoraw(struct fd_softc *sc)
1682 {
1683 if (sc->openpart == FDMSDOSPART)
1684 mscachetoraw(sc);
1685 else
1686 amcachetoraw(sc);
1687 }
1688
1689 /*
1690 * decode raw MFM from DMA into units track cache.
1691 * when we go to multiple disk formats, this will call type dependent
1692 * functions
1693 */
1694 int
1695 fdrawtocache(struct fd_softc *sc)
1696 {
1697
1698 if (sc->openpart == FDMSDOSPART)
1699 return(msrawtocache(sc));
1700 else
1701 return(amrawtocache(sc));
1702 }
1703
1704 void
1705 amcachetoraw(struct fd_softc *sc)
1706 {
1707 static u_long mfmnull[4];
1708 u_long *rp, *crp, *dp, hcksum, dcksum, info, zero;
1709 int sec, i;
1710
1711 rp = fdc_dmap;
1712
1713 /*
1714 * not yet one sector (- 1 long) gap.
1715 * for now use previous drivers values
1716 */
1717 for (i = 0; i < sc->type->gap; i++)
1718 *rp++ = 0xaaaaaaaa;
1719 /*
1720 * process sectors
1721 */
1722 dp = sc->cachep;
1723 zero = 0;
1724 info = 0xff000000 | (sc->cachetrk << 16) | sc->nsectors;
1725 for (sec = 0; sec < sc->nsectors; sec++, info += (1 << 8) - 1) {
1726 hcksum = dcksum = 0;
1727 /*
1728 * sector format
1729 * offset description
1730 *-----------------------------------
1731 * 0 null
1732 * 1 sync
1733 * oddbits evenbits
1734 *----------------------
1735 * 2 3 [0xff]b [trk]b [sec]b [togap]b
1736 * 4-7 8-11 null
1737 * 12 13 header cksum [2-11]
1738 * 14 15 data cksum [16-271]
1739 * 16-143 144-271 data
1740 */
1741 *rp = 0xaaaaaaaa;
1742 if (*(rp - 1) & 0x1)
1743 *rp &= 0x7fffffff; /* clock bit correction */
1744 rp++;
1745 *rp++ = (FDMFMSYNC << 16) | FDMFMSYNC;
1746 rp = mfmblkencode(&info, rp, &hcksum, 1);
1747 rp = mfmblkencode(mfmnull, rp, &hcksum, 4);
1748 rp = mfmblkencode(&hcksum, rp, NULL, 1);
1749
1750 crp = rp;
1751 rp = mfmblkencode(dp, rp + 2, &dcksum, FDSECLWORDS);
1752 dp += FDSECLWORDS;
1753 crp = mfmblkencode(&dcksum, crp, NULL, 1);
1754 if (*(crp - 1) & 0x1)
1755 *crp &= 0x7fffffff; /* clock bit correction */
1756 else if ((*crp & 0x40000000) == 0)
1757 *crp |= 0x80000000;
1758 }
1759 *rp = 0xaaa80000;
1760 if (*(rp - 1) & 0x1)
1761 *rp &= 0x7fffffff;
1762 }
1763
1764 u_long *
1765 fdfindsync(u_long *rp, u_long *ep)
1766 {
1767 u_short *sp;
1768
1769 sp = (u_short *)rp;
1770 while ((u_long *)sp < ep && *sp != FDMFMSYNC)
1771 sp++;
1772 while ((u_long *)sp < ep && *sp == FDMFMSYNC)
1773 sp++;
1774 if ((u_long *)sp < ep)
1775 return((u_long *)sp);
1776 return(NULL);
1777 }
1778
1779 int
1780 amrawtocache(struct fd_softc *sc)
1781 {
1782 u_long mfmnull[4];
1783 u_long *dp, *rp, *erp, *crp, *srp, hcksum, dcksum, info, cktmp;
1784 int cnt, doagain;
1785
1786 doagain = 1;
1787 srp = rp = fdc_dmap;
1788 erp = (u_long *)((u_short *)rp + sc->type->nreadw);
1789 cnt = 0;
1790 again:
1791 if (doagain == 0 || (rp = srp = fdfindsync(srp, erp)) == NULL) {
1792 #ifdef DIAGNOSTIC
1793 aprint_error_dev(sc->sc_dev, "corrupted track (%d) data.\n",
1794 sc->cachetrk);
1795 #endif
1796 return(-1);
1797 }
1798
1799 /*
1800 * process sectors
1801 */
1802 for (; cnt < sc->nsectors; cnt++) {
1803 hcksum = dcksum = 0;
1804 rp = mfmblkdecode(rp, &info, &hcksum, 1);
1805 rp = mfmblkdecode(rp, mfmnull, &hcksum, 4);
1806 rp = mfmblkdecode(rp, &cktmp, NULL, 1);
1807 if (cktmp != hcksum) {
1808 #ifdef FDDEBUG
1809 printf(" info 0x%lx hchksum 0x%lx trkhcksum 0x%lx\n",
1810 info, hcksum, cktmp);
1811 #endif
1812 goto again;
1813 }
1814 if (((info >> 16) & 0xff) != sc->cachetrk) {
1815 #ifdef DEBUG
1816 aprint_debug_dev(sc->sc_dev,
1817 "incorrect track found: 0x%lx %d\n",
1818 info, sc->cachetrk);
1819 #endif
1820 goto again;
1821 }
1822 #ifdef FDDEBUG
1823 printf(" info 0x%lx\n", info);
1824 #endif
1825
1826 rp = mfmblkdecode(rp, &cktmp, NULL, 1);
1827 dp = sc->cachep;
1828 dp += FDSECLWORDS * ((info >> 8) & 0xff);
1829 crp = mfmblkdecode(rp, dp, &dcksum, FDSECLWORDS);
1830 if (cktmp != dcksum) {
1831 #ifdef FDDEBUG
1832 printf(" info 0x%lx dchksum 0x%lx trkdcksum 0x%lx\n",
1833 info, dcksum, cktmp);
1834 #endif
1835 goto again;
1836 }
1837
1838 /*
1839 * if we are at gap then we can no longer be sure
1840 * of correct sync marks
1841 */
1842 if ((info && 0xff) == 1)
1843 doagain = 1;
1844 else
1845 doagain = 0;
1846 srp = rp = fdfindsync(crp, erp);
1847 }
1848 return(0);
1849 }
1850
1851 void
1852 mscachetoraw(struct fd_softc *sc)
1853 {
1854 u_short *rp, *erp, crc;
1855 u_char *cp, tb[5];
1856 int sec, i;
1857
1858 rp = (u_short *)fdc_dmap;
1859 erp = rp + sc->type->nwritew;
1860 cp = sc->cachep;
1861
1862 /*
1863 * initial track filler (828 * GAP1)
1864 */
1865 for (i = 0; i < sc->type->gap; i++) {
1866 *rp++ = FDMFMGAP1;
1867 *rp++ = FDMFMGAP1;
1868 }
1869
1870 for (sec = 0; sec < sc->nsectors; sec++) {
1871
1872 /*
1873 * leading sector gap
1874 * (12 * GAP2) + (3 * SYNC)
1875 */
1876 for (i = 0; i < 12; i++)
1877 *rp++ = FDMFMGAP2;
1878 *rp++ = FDMFMSYNC;
1879 *rp++ = FDMFMSYNC;
1880 *rp++ = FDMFMSYNC;
1881
1882 /*
1883 * sector information
1884 * (ID) + track + side + sector + sector size + CRC16
1885 */
1886 *rp++ = FDMFMID;
1887 tb[0] = sc->cachetrk / FDNHEADS;
1888 tb[1] = sc->cachetrk % FDNHEADS;
1889 tb[2] = sec + 1;
1890 i = sc->bytespersec;
1891 tb[3] = i < 256 ? 0 : (i < 512 ? 1 : (i < 1024 ? 2 : 3));
1892 rp = msblkencode(rp, tb, 4, &crc);
1893 tb[0] = crc >> 8;
1894 tb[1] = crc & 0xff;
1895 tb[2] = 0x4e; /* GAP1 decoded */
1896 rp = msblkencode(rp, tb, 3, 0);
1897
1898 /*
1899 * sector info/data gap
1900 * (22 * GAP1) + (12 * GAP2) + (3 * SYNC)
1901 */
1902 for (i = 0; i < 21; i++)
1903 *rp++ = FDMFMGAP1;
1904 for (i = 0; i < 12; i++)
1905 *rp++ = FDMFMGAP2;
1906 *rp++ = FDMFMSYNC;
1907 *rp++ = FDMFMSYNC;
1908 *rp++ = FDMFMSYNC;
1909
1910 /*
1911 * sector data
1912 * (DATA) + ...data... + CRC16
1913 */
1914 *rp++ = FDMFMDATA;
1915 rp = msblkencode(rp, cp, sc->bytespersec, &crc);
1916 cp += sc->bytespersec;
1917 tb[0] = crc >> 8;
1918 tb[1] = crc & 0xff;
1919 tb[2] = 0x4e; /* GAP3 decoded */
1920 rp = msblkencode(rp, tb, 3, 0);
1921
1922 /*
1923 * trailing sector gap
1924 * (80 * GAP3)
1925 */
1926 for (i = 0; i < 79; i++)
1927 *rp++ = FDMFMGAP3;
1928 }
1929
1930 /*
1931 * fill rest of track with GAP3
1932 */
1933 while (rp != erp)
1934 *rp++ = FDMFMGAP3;
1935
1936 }
1937
1938 int
1939 msrawtocache(struct fd_softc *sc)
1940 {
1941 u_short *rp, *srp, *erp;
1942 u_char tb[5], *cp;
1943 int ct, sec, retry;
1944
1945 srp = rp = (u_short *)fdc_dmap;
1946 erp = rp + sc->type->nreadw;
1947 cp = sc->cachep;
1948
1949 for (ct = 0; ct < sc->nsectors; ct++) {
1950 retry = 1;
1951 do {
1952 /*
1953 * skip leading gap to sync
1954 */
1955 if ((rp = (u_short *)fdfindsync((u_long *)rp, (u_long *)erp)) == NULL) {
1956 #ifdef DIAGNOSTIC
1957 aprint_normal_dev(sc->sc_dev,
1958 "corrupted track (%d) data.\n",
1959 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