2 * Copyright (c) 2001 Joerg Wunsch
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <dev/ic/nec765.h>
31 #include <sys/fdcio.h>
42 * Decode the FDC status pointed to by `fdcsp', and print a textual
43 * translation to stderr. If `terse' is false, the numerical FDC
44 * register status is printed, too.
47 printstatus(struct fdc_status *fdcsp, int terse)
53 "\nFDC status ST0=%#x ST1=%#x ST2=%#x C=%u H=%u R=%u N=%u:\n",
54 fdcsp->status[0] & 0xff,
55 fdcsp->status[1] & 0xff,
56 fdcsp->status[2] & 0xff,
57 fdcsp->status[3] & 0xff,
58 fdcsp->status[4] & 0xff,
59 fdcsp->status[5] & 0xff,
60 fdcsp->status[6] & 0xff);
62 if ((fdcsp->status[0] & NE7_ST0_IC_RC) == 0) {
63 sprintf(msgbuf, "timeout");
64 } else if ((fdcsp->status[0] & NE7_ST0_IC_RC) != NE7_ST0_IC_AT) {
65 sprintf(msgbuf, "unexcpted interrupt code %#x",
66 fdcsp->status[0] & NE7_ST0_IC_RC);
68 strcpy(msgbuf, "unexpected error code in ST1/ST2");
70 if (fdcsp->status[1] & NE7_ST1_EN)
71 strcpy(msgbuf, "end of cylinder (wrong format)");
72 else if (fdcsp->status[1] & NE7_ST1_DE) {
73 if (fdcsp->status[2] & NE7_ST2_DD)
74 strcpy(msgbuf, "CRC error in data field");
76 strcpy(msgbuf, "CRC error in ID field");
77 } else if (fdcsp->status[1] & NE7_ST1_MA) {
78 if (fdcsp->status[2] & NE7_ST2_MD)
79 strcpy(msgbuf, "no address mark in data field");
81 strcpy(msgbuf, "no address mark in ID field");
82 } else if (fdcsp->status[2] & NE7_ST2_WC)
83 strcpy(msgbuf, "wrong cylinder (format mismatch)");
84 else if (fdcsp->status[1] & NE7_ST1_ND)
85 strcpy(msgbuf, "no data (sector not found)");
87 fputs(msgbuf, stderr);
90 static struct fd_type fd_types_auto[1] =
91 { { 0,0,0,0,0,0,0,0,0,0,0,FL_AUTO } };
94 static struct fd_type fd_types_288m[] = {
107 { 0,0,0,0,0,0,0,0,0,0,0,0 }
110 static struct fd_type fd_types_144m[] = {
118 { 0,0,0,0,0,0,0,0,0,0,0,0 }
127 { 0,0,0,0,0,0,0,0,0,0,0,0 }
131 static struct fd_type fd_types_12m[] = {
138 { 0,0,0,0,0,0,0,0,0,0,0,0 }
147 { FDF_5_360 | FL_2STEP },
149 { 0,0,0,0,0,0,0,0,0,0,0,0 }
153 static struct fd_type fd_types_720k[] =
158 { 0,0,0,0,0,0,0,0,0,0,0,0 }
161 static struct fd_type fd_types_360k[] =
166 { 0,0,0,0,0,0,0,0,0,0,0,0 }
171 * Parse a format string, and fill in the parameter pointed to by `out'.
173 * sectrac,secsize,datalen,gap,ncyls,speed,heads,f_gap,f_inter,offs2,flags[...]
175 * sectrac = sectors per track
176 * secsize = sector size in bytes
177 * datalen = length of sector if secsize == 128
178 * gap = gap length when reading
179 * ncyls = number of cylinders
180 * speed = transfer speed 250/300/500/1000 KB/s
181 * heads = number of heads
182 * f_gap = gap length when formatting
183 * f_inter = sector interleave when formatting
184 * offs2 = offset of sectors on side 2
185 * flags = +/-mfm | +/-2step | +/-perpend
186 * mfm - use MFM recording
187 * 2step - use 2 steps between cylinders
188 * perpend - user perpendicular (vertical) recording
190 * Any omitted value will be passed on from parameter `in'.
193 parse_fmt(const char *s, enum fd_drivetype type,
194 struct fd_type in, struct fd_type *out)
206 if ((cp = strchr(s, ',')) == 0) {
212 s1 = malloc(cp - s + 1);
215 memcpy(s1, s, cp - s);
220 if (strlen(s1) == 0) {
226 case 0: /* sectrac */
227 if (getnum(s1, &out->sectrac))
229 "bad numeric value for sectrac: %s", s1);
232 case 1: /* secsize */
235 "bad numeric value for secsize: %s", s1);
236 if (j == 128) out->secsize = 0;
237 else if (j == 256) out->secsize = 1;
238 else if (j == 512) out->secsize = 2;
239 else if (j == 1024) out->secsize = 3;
241 errx(EX_USAGE, "bad sector size %d", j);
244 case 2: /* datalen */
247 "bad numeric value for datalen: %s", s1);
249 errx(EX_USAGE, "bad datalen %d", j);
254 if (getnum(s1, &out->gap))
256 "bad numeric value for gap: %s", s1);
262 "bad numeric value for ncyls: %s", s1);
264 errx(EX_USAGE, "bad # of cylinders %d", j);
271 "bad numeric value for speed: %s", s1);
274 abort(); /* paranoia */
279 out->trans = FDC_250KBPS;
281 errx(EX_USAGE, "bad speed %d", j);
286 out->trans = FDC_300KBPS;
288 out->trans = FDC_250KBPS;
290 out->trans = FDC_500KBPS;
292 errx(EX_USAGE, "bad speed %d", j);
297 out->trans = FDC_1MBPS;
301 out->trans = FDC_250KBPS;
303 out->trans = FDC_500KBPS;
305 errx(EX_USAGE, "bad speed %d", j);
313 "bad numeric value for heads: %s", s1);
314 if (j == 1 || j == 2)
317 errx(EX_USAGE, "bad # of heads %d", j);
321 if (getnum(s1, &out->f_gap))
323 "bad numeric value for f_gap: %s", s1);
326 case 8: /* f_inter */
327 if (getnum(s1, &out->f_inter))
329 "bad numeric value for f_inter: %s", s1);
333 if (getnum(s1, &out->offset_side2))
335 "bad numeric value for offs2: %s", s1);
339 if (strcmp(s1, "+mfm") == 0)
340 out->flags |= FL_MFM;
341 else if (strcmp(s1, "-mfm") == 0)
342 out->flags &= ~FL_MFM;
343 else if (strcmp(s1, "+auto") == 0)
344 out->flags |= FL_AUTO;
345 else if (strcmp(s1, "-auto") == 0)
346 out->flags &= ~FL_AUTO;
347 else if (strcmp(s1, "+2step") == 0)
348 out->flags |= FL_2STEP;
349 else if (strcmp(s1, "-2step") == 0)
350 out->flags &= ~FL_2STEP;
351 else if (strcmp(s1, "+perpnd") == 0)
352 out->flags |= FL_PERPND;
353 else if (strcmp(s1, "-perpnd") == 0)
354 out->flags &= ~FL_PERPND;
356 errx(EX_USAGE, "bad flag: %s", s1);
362 out->size = out->tracks * out->heads * out->sectrac;
366 * Print a textual translation of the drive (density) type described
367 * by `in' to stdout. The string uses the same form that is parseable
371 print_fmt(struct fd_type in)
375 secsize = 128 << in.secsize;
377 case FDC_250KBPS: speed = 250; break;
378 case FDC_300KBPS: speed = 300; break;
379 case FDC_500KBPS: speed = 500; break;
380 case FDC_1MBPS: speed = 1000; break;
381 default: speed = 1; break;
384 printf("%d,%d,%#x,%#x,%d,%d,%d,%#x,%d,%d",
385 in.sectrac, secsize, in.datalen, in.gap, in.tracks,
386 speed, in.heads, in.f_gap, in.f_inter, in.offset_side2);
387 if (in.flags & FL_MFM)
389 if (in.flags & FL_2STEP)
391 if (in.flags & FL_PERPND)
393 if (in.flags & FL_AUTO)
399 * Based on `size' (in kilobytes), walk through the table of known
400 * densities for drive type `type' and see if we can find one. If
401 * found, return it (as a pointer to static storage), otherwise return
405 get_fmt(int size, enum fd_drivetype type)
408 struct fd_type *fdtp;
415 fdtp = fd_types_360k;
416 n = sizeof fd_types_360k / sizeof(struct fd_type);
420 fdtp = fd_types_720k;
421 n = sizeof fd_types_720k / sizeof(struct fd_type);
426 n = sizeof fd_types_12m / sizeof(struct fd_type);
430 fdtp = fd_types_144m;
431 n = sizeof fd_types_144m / sizeof(struct fd_type);
435 fdtp = fd_types_288m;
436 n = sizeof fd_types_288m / sizeof(struct fd_type);
441 return fd_types_auto;
443 for (i = 0; i < n; i++, fdtp++) {
444 fdtp->size = fdtp->sectrac * fdtp->heads * fdtp->tracks;
445 if (((128 << fdtp->secsize) * fdtp->size / 1024) == size)
452 * Parse a number from `s'. If the string cannot be converted into a
453 * number completely, return -1, otherwise 0. The result is returned
457 getnum(const char *s, int *res)
462 ul = strtoul(s, &cp, 0);
471 * Return a short name and a verbose description for the drive
475 getname(enum fd_drivetype t, const char **name, const char **descr)
481 *descr = "unknown drive type";
486 *descr = "5.25\" double-density";
491 *descr = "5.25\" high-density";
496 *descr = "3.5\" double-density";
501 *descr = "3.5\" high-density";
506 *descr = "3.5\" extra-density";