]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/pc98/pc98/wfd.c
This commit was generated by cvs2svn to compensate for changes in r56083,
[FreeBSD/FreeBSD.git] / sys / pc98 / pc98 / wfd.c
1 /*
2  * Copyright (c) 1997,1998  Junichi Satoh <junichi@astec.co.jp>
3  *   All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
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.
14  *
15  * THIS SOFTWARE IS PROVIDED BY Junichi Satoh ``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 Junichi Satoh 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.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * ATAPI Floppy, LS-120 driver
31  */
32
33 #include "wdc.h"
34 #include "wfd.h"
35
36 #if NWFD > 0 && NWDC > 0
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/conf.h>
42 #include <sys/proc.h>
43 #include <sys/malloc.h>
44 #include <sys/buf.h>
45 #include <sys/devicestat.h>
46 #include <sys/disklabel.h>
47 #include <sys/diskslice.h>
48 #include <sys/cdio.h>
49
50 #include <i386/isa/atapi.h>
51
52 static  d_open_t        wfdopen;
53 static  d_close_t       wfdclose;
54 static  d_ioctl_t       wfdioctl;
55 static  d_strategy_t    wfdstrategy;
56
57 #define CDEV_MAJOR 87
58 #define BDEV_MAJOR 1
59
60 static struct cdevsw wfd_cdevsw = {
61         /* open */      wfdopen,
62         /* close */     wfdclose,
63         /* read */      physread,
64         /* write */     physwrite,
65         /* ioctl */     wfdioctl,
66         /* poll */      nopoll,
67         /* mmap */      nommap,
68         /* strategy */  wfdstrategy,
69         /* name */      "wfd",
70         /* maj */       CDEV_MAJOR,
71         /* dump */      nodump,
72         /* psize */     nopsize,
73         /* flags */     D_DISK,
74         /* bmaj */      BDEV_MAJOR
75 };
76
77 int  wfdattach(struct atapi*, int, struct atapi_params*, int);
78
79 #define NUNIT   (NWDC*2)                /* Max. number of devices */
80 #define UNIT(d) ((minor(d) >> 3) & 3)   /* Unit part of minor device number */
81
82 #define F_BOPEN         0x0001          /* The block device is opened */
83 #define F_MEDIA_CHANGED 0x0002          /* The media have changed since open */
84 #define F_DEBUG         0x0004          /* Print debug info */
85
86 /*
87  * LS-120 Capabilities and Mechanical Status Page
88  */
89 struct cappage {
90     /* Mode data header */
91     u_short     data_length;
92     u_char      medium_type;
93 #define MDT_UNKNOWN     0x00
94 #define MDT_NO_DISC     0x70
95 #define MDT_DOOR_OPEN   0x71
96 #define MDT_FMT_ERROR   0x72
97
98 #define MDT_2DD_UN      0x10
99 #define MDT_2DD         0x11
100 #define MDT_2HD_UN      0x20
101 #define MDT_2HD_12_98   0x22
102 #define MDT_2HD_12      0x23
103 #define MDT_2HD_144     0x24
104 #define MDT_LS120       0x31
105
106     unsigned        reserved0       :7;
107     unsigned        wp              :1;     /* Write protect */
108     u_char          reserved1[4];
109
110     /* Capabilities page */
111     unsigned        page_code       :6;     /* Page code - Should be 0x5 */
112 #define CAP_PAGE        0x05
113     unsigned        reserved1_6     :1;     /* Reserved */
114     unsigned        ps              :1;     /* The device is capable of saving the page */
115     u_char          page_length;            /* Page Length - Should be 0x1e */
116     u_short         transfer_rate;          /* In kilobits per second */
117     u_char          heads, sectors;         /* Number of heads, Number of sectors per track */
118     u_short         sector_size;            /* Byes per sector */
119     u_short         cyls;                   /* Number of cylinders */
120     u_char          reserved10[10];
121     u_char          motor_delay;            /* Motor off delay */
122     u_char          reserved21[7];
123     u_short         rpm;                    /* Rotations per minute */
124     u_char          reserved30[2];
125 };
126
127 struct wfd {
128         struct atapi *ata;              /* Controller structure */
129         int unit;                       /* IDE bus drive unit */
130         int lun;                        /* Logical device unit */
131         int flags;                      /* Device state flags */
132         int refcnt;                     /* The number of raw opens */
133         int maxblks;                    /* transfer size limit */
134         struct buf_queue_head buf_queue;  /* Queue of i/o requests */
135         struct atapi_params *param;     /* Drive parameters table */
136         struct cappage cap;             /* Capabilities page info */
137         char description[80];           /* Device description */
138         struct diskslices *dk_slices;   /* virtual drives */
139
140         struct devstat device_stats;
141 };
142
143 static struct wfd *wfdtab[NUNIT]; /* Drive info by unit number */
144 static int wfdnlun = 0;           /* Number of configured drives */
145
146 static void wfd_start (struct wfd *t);
147 static void wfd_done (struct wfd *t, struct buf *bp, int resid,
148         struct atapires result);
149 static void wfd_error (struct wfd *t, struct atapires result);
150 static int wfd_request_wait (struct wfd *t, u_char cmd, u_char a1, u_char a2,
151         u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8,
152         u_char a9, char *addr, int count);
153 static void wfd_describe (struct wfd *t);
154 static int wfd_eject (struct wfd *t, int closeit);
155
156 /*
157  * Dump the array in hexadecimal format for debugging purposes.
158  */
159 static void wfd_dump (int lun, char *label, void *data, int len)
160 {
161         u_char *p = data;
162
163         printf ("wfd%d: %s %x", lun, label, *p++);
164         while (--len > 0)
165                 printf ("-%x", *p++);
166         printf ("\n");
167 }
168
169 int 
170 wfdattach (struct atapi *ata, int unit, struct atapi_params *ap, int debug)
171 {
172         struct wfd *t;
173         struct atapires result;
174         int lun, i;
175
176         if (wfdnlun >= NUNIT) {
177                 printf ("wfd: too many units\n");
178                 return (0);
179         }
180         if (!atapi_request_immediate) {
181                 printf("wfd: configuration error, ATAPI core code not present!\n");
182                 printf("wfd: check `options ATAPI_STATIC' in your kernel config file!\n");
183                 return (0);
184         }
185         t = malloc (sizeof (struct wfd), M_TEMP, M_NOWAIT);
186         if (! t) {
187                 printf ("wfd: out of memory\n");
188                 return (0);
189         }
190         wfdtab[wfdnlun] = t;
191         bzero (t, sizeof (struct wfd));
192         bufq_init(&t->buf_queue);
193         t->ata = ata;
194         t->unit = unit;
195         lun = t->lun = wfdnlun;
196         t->param = ap;
197         t->flags = F_MEDIA_CHANGED;
198         t->refcnt = 0;
199         if (debug) {
200                 t->flags |= F_DEBUG;
201                 /* Print params. */
202                 wfd_dump (t->lun, "info", ap, sizeof *ap);
203         }
204
205         /* Get drive capabilities. */
206         /* Do it twice to avoid the stale media changed state. */
207         for (i = 0; i < 2; i++) {
208                 result = atapi_request_immediate (ata, unit, ATAPI_MODE_SENSE,
209                         0, CAP_PAGE, 0, 0, 0, 0, 
210                         sizeof (t->cap) >> 8, sizeof (t->cap),
211                         0, 0, 0, 0, 0, 0, 0, (char*) &t->cap, sizeof (t->cap));
212         }
213
214         if (result.code == RES_ERR &&
215             (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)
216                 result = atapi_request_immediate (ata, unit, ATAPI_MODE_SENSE,
217                         0, CAP_PAGE, 0, 0, 0, 0, sizeof (t->cap) >> 8,
218                         sizeof (t->cap), 0, 0, 0, 0, 0, 0, 0,
219                         (char*) &t->cap, sizeof (t->cap));
220
221         /* Some drives have shorter capabilities page. */
222         if (result.code == RES_UNDERRUN)
223                 result.code = 0;
224
225         if (result.code == 0) {
226                 wfd_describe (t);
227                 if (t->flags & F_DEBUG)
228                         wfd_dump (t->lun, "cap", &t->cap, sizeof t->cap);
229         } else
230                 return -1;
231
232         /*
233          * The IOMEGA ZIP 100, at firmware 21.* and 23.* at least
234          * is known to lock up if transfers > 64 blocks are
235          * requested.
236          */
237         if (!strcmp(ap->model, "IOMEGA  ZIP 100       ATAPI")) {
238                 printf("wfd%d: buggy Zip drive, 64-block transfer limit set\n",
239                        t->lun);
240                 t->maxblks = 64;
241         } else {
242                 t->maxblks = 0; /* no limit */
243         }
244         
245         
246
247         make_dev(&wfd_cdevsw, dkmakeminor(t->lun, WHOLE_DISK_SLICE, RAW_PART),
248             UID_ROOT, GID_OPERATOR, 0640, "rwfd%d", t->lun);
249
250         /*
251          * Export the drive to the devstat interface.
252          */
253         devstat_add_entry(&t->device_stats, "wfd", 
254                           t->lun, t->cap.sector_size,
255                           DEVSTAT_NO_ORDERED_TAGS,
256                           DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_IDE,
257                           DEVSTAT_PRIORITY_WFD);
258         wfdnlun++;
259         return (1);
260 }
261
262 void wfd_describe (struct wfd *t)
263 {
264         int no_print = 0;
265
266         t->cap.cyls = ntohs (t->cap.cyls);
267         t->cap.sector_size = ntohs (t->cap.sector_size);
268
269         printf ("wfd%d: ", t->lun);
270         switch (t->cap.medium_type) {
271         case MDT_UNKNOWN:
272                 printf ("medium type unknown (no disk)");
273                 no_print = 1;
274                 break;
275         case MDT_2DD_UN:
276                 printf ("2DD(capacity unknown) floppy disk loaded");
277                 no_print = 1;
278                 break;
279         case MDT_2DD:
280                 printf ("720KB floppy disk loaded");
281                 break;
282         case MDT_2HD_UN:
283                 printf ("2HD(capacity unknown) floppy disk loaded");
284                 no_print = 1;
285                 break;
286         case MDT_2HD_12_98:
287                 printf ("1.25MB(PC-9801 format) floppy disk loaded");
288                 break;
289         case MDT_2HD_12:
290                 printf ("1.2MB floppy disk loaded");
291                 break;
292         case MDT_2HD_144:
293                 printf ("1.44MB floppy disk loaded");
294                 break;
295         case MDT_LS120:
296                 printf ("120MB floppy disk loaded");
297                 break;
298         case MDT_NO_DISC:
299                 printf ("no disc inside");
300                 no_print = 1;
301                 break;
302         case MDT_DOOR_OPEN:
303                 printf ("door open");
304                 no_print = 1;
305                 break;
306         case MDT_FMT_ERROR:
307                 printf ("medium format error");
308                 no_print = 1;
309                 break;
310         default:
311                 printf ("medium type=0x%x", t->cap.medium_type);
312                 break;
313         }
314         if (t->cap.wp)
315                 printf(", write protected");
316         printf ("\n");
317
318         if (!no_print) {
319                 printf ("wfd%d: ", t->lun);
320                 printf ("%u cyls", t->cap.cyls);
321                 printf (", %u heads, %u S/T", t->cap.heads, t->cap.sectors);
322                 printf (", %u B/S", t->cap.sector_size);
323                 printf ("\n");
324         }
325 }
326
327 int wfdopen (dev_t dev, int flags, int fmt, struct proc *p)
328 {
329         int lun = UNIT(dev);
330         struct wfd *t;
331         struct atapires result;
332         int errcode = 0;
333         struct disklabel label;
334
335         /* Check that the device number is legal
336          * and the ATAPI driver is loaded. */
337         if (lun >= wfdnlun || ! atapi_request_immediate)
338                 return (ENXIO);
339         t = wfdtab[lun];
340
341         t->flags &= ~F_MEDIA_CHANGED;
342         /* Lock the media. */
343         wfd_request_wait (t, ATAPI_PREVENT_ALLOW,
344                           0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
345
346         /* Sense the media type */
347         result = atapi_request_wait (t->ata, t->unit, ATAPI_MODE_SENSE,
348                                      0, CAP_PAGE, 0, 0, 0, 0, 
349                                      sizeof (t->cap) >> 8, sizeof (t->cap),
350                                      0, 0, 0, 0, 0, 0, 0, 
351                                      (char*) &t->cap, sizeof (t->cap));
352         if (result.code)
353                 printf ("wfd%d: Sense the media type is failed.\n", t->lun);
354         else {
355                 t->cap.cyls = ntohs (t->cap.cyls);
356                 t->cap.sector_size = ntohs (t->cap.sector_size);
357         }
358
359         /* Build label for whole disk. */
360         bzero(&label, sizeof label);
361         label.d_secsize = t->cap.sector_size;
362         label.d_nsectors = t->cap.sectors;
363         label.d_ntracks = t->cap.heads;
364         label.d_ncylinders = t->cap.cyls;
365         label.d_secpercyl = t->cap.heads * t->cap.sectors;
366         label.d_rpm = 720;
367         label.d_secperunit = label.d_secpercyl * t->cap.cyls;
368
369         /* Initialize slice tables. */
370         errcode = dsopen(dev, fmt, 0, &t->dk_slices, &label);
371         if (errcode != 0)
372                 return errcode;
373
374         t->flags |= F_BOPEN;
375         return (0);
376 }
377
378 /*
379  * Close the device.  Only called if we are the LAST
380  * occurence of an open device.
381  */
382 int wfdclose (dev_t dev, int flags, int fmt, struct proc *p)
383 {
384         int lun = UNIT(dev);
385         struct wfd *t = wfdtab[lun];
386
387         dsclose(dev, fmt, t->dk_slices);
388         if(!dsisopen(t->dk_slices)) {
389                 /* If we were the last open of the entire device, release it. */
390                 if (! t->refcnt)
391                         wfd_request_wait (t, ATAPI_PREVENT_ALLOW,
392                                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
393                 t->flags &= ~F_BOPEN;
394         }
395         return (0);
396 }
397
398 /*
399  * Actually translate the requested transfer into one the physical driver can
400  * understand. The transfer is described by a buf and will include only one
401  * physical transfer.
402  */
403 void wfdstrategy (struct buf *bp)
404 {
405         int lun = UNIT(bp->b_dev);
406         struct wfd *t = wfdtab[lun];
407         int x;
408
409         /* If it's a null transfer, return immediatly. */
410         if (bp->b_bcount == 0) {
411                 bp->b_resid = 0;
412                 biodone (bp);
413                 return;
414         }
415
416         /*
417          * Do bounds checking, adjust transfer, and set b_pblkno.
418          */
419         if (dscheck(bp, t->dk_slices) <= 0) {
420                 biodone(bp);
421                 return;
422         }
423
424         x = splbio();
425
426         /* Place it in the queue of disk activities for this disk. */
427         bufqdisksort (&t->buf_queue, bp);
428
429         /* Tell the device to get going on the transfer if it's
430          * not doing anything, otherwise just wait for completion. */
431         wfd_start (t);
432         splx(x);
433 }
434
435 /*
436  * Look to see if there is a buf waiting for the device
437  * and that the device is not already busy. If both are true,
438  * It dequeues the buf and creates an ATAPI command to perform the
439  * transfer in the buf.
440  * The bufs are queued by the strategy routine (wfdstrategy).
441  * Must be called at the correct (splbio) level.
442  */
443 static void wfd_start (struct wfd *t)
444 {
445         struct buf *bp = bufq_first(&t->buf_queue);
446         u_long blkno, nblk;
447         u_char op_code;
448         long count;
449         int pxcount, pxnblk;
450         u_char *pxdest;
451         
452
453         /* See if there is a buf to do and we are not already doing one. */
454         if (! bp)
455                 return;
456
457         /* Unqueue the request. */
458         bufq_remove(&t->buf_queue, bp);
459
460         /* Tell devstat we are starting on the transaction */
461         devstat_start_transaction(&t->device_stats);
462
463         /* We have a buf, now we should make a command
464          * First, translate the block to absolute and put it in terms of the
465          * logical blocksize of the device. */
466         blkno = bp->b_pblkno / (t->cap.sector_size / 512);
467         nblk = (bp->b_bcount + (t->cap.sector_size - 1)) / t->cap.sector_size;
468
469         if ((t->maxblks == 0) || (nblk <= t->maxblks)) {
470
471                 if(bp->b_flags & B_READ) {
472                         op_code = ATAPI_READ_BIG;
473                         count = bp->b_bcount;
474                 } else {
475                         op_code = ATAPI_WRITE_BIG;
476                         count = -bp->b_bcount;
477                 }
478
479                 /* only one transfer */
480                 (int)bp->b_driver1 = 0;
481                 (int)bp->b_driver2 = 0;
482                 atapi_request_callback (t->ata, t->unit, op_code, 0,
483                                         blkno>>24, blkno>>16, blkno>>8, blkno,
484                                         0, nblk>>8, nblk, 0, 0,
485                                         0, 0, 0, 0, 0, 
486                                         (u_char*) bp->b_data, count, 
487                                         (void*)wfd_done, t, bp);
488         } else {
489
490                 /*
491                  * We can't handle this request in a single
492                  * read/write operation.  Instead, queue a set of
493                  * transfers, and record the number of transfers
494                  * and the running residual in the b_driver
495                  * fields of the bp.
496                  */ 
497
498                 if(bp->b_flags & B_READ) {
499                         op_code = ATAPI_READ_BIG;
500                 } else {
501                         op_code = ATAPI_WRITE_BIG;
502                 }
503
504                 /* calculate number of transfers */
505                 (int)bp->b_driver1 = (nblk - 1) / t->maxblks;
506                 (int)bp->b_driver2 = 0;
507
508                 pxdest = (u_char *)bp->b_data;
509                 pxcount = bp->b_bcount;
510
511                 /* construct partial transfer requests */
512                 while (nblk > 0) {
513                         pxnblk = min(nblk, t->maxblks);
514                         count = min(pxcount, t->maxblks * t->cap.sector_size);
515
516                         atapi_request_callback(t->ata, t->unit, op_code, 0,
517                                                blkno>>24, blkno>>16, blkno>>8,
518                                                blkno, 0, pxnblk>>8, pxnblk, 
519                                                0, 0, 0, 0, 0, 0, 0,
520                                                pxdest, 
521                                                (bp->b_flags & B_READ) ?
522                                                count : -count, 
523                                                (void*)wfd_done, t, bp);
524                         nblk -= pxnblk;
525                         pxcount -= count;
526                         pxdest += count;
527                         blkno += pxnblk;
528                 }
529         }
530 }
531
532 static void wfd_done (struct wfd *t, struct buf *bp, int resid,
533         struct atapires result)
534 {
535                 
536         if (result.code) {
537                 wfd_error (t, result);
538                 bp->b_error = EIO;
539                 bp->b_flags |= B_ERROR;
540         } else
541                 (int)bp->b_driver2 += resid;
542         /*
543          * We can't call biodone until all outstanding
544          * transfer fragments are handled.  If one hits
545          * an error, we will be returning an error, but
546          * only when all are complete.
547          */
548         if (((int)bp->b_driver1)-- <= 0) {
549                 bp->b_resid = (int)bp->b_driver2;
550                 devstat_end_transaction_buf(&t->device_stats, bp);
551                 biodone (bp);
552         }
553         
554         wfd_start (t);
555 }
556
557 static void wfd_error (struct wfd *t, struct atapires result)
558 {
559         if (result.code != RES_ERR)
560                 return;
561         switch (result.error & AER_SKEY) {
562         case AER_SK_NOT_READY:
563                 if (result.error & ~AER_SKEY) {
564                         /* Not Ready */
565                         printf ("wfd%d: not ready\n", t->lun);
566                         return;
567                 }
568                 /* Tray open. */
569                 if (! (t->flags & F_MEDIA_CHANGED))
570                         printf ("wfd%d: tray open\n", t->lun);
571                 t->flags |= F_MEDIA_CHANGED;
572                 return;
573
574         case AER_SK_UNIT_ATTENTION:
575                 /* Media changed. */
576                 if (! (t->flags & F_MEDIA_CHANGED))
577                         printf ("wfd%d: media changed\n", t->lun);
578                 t->flags |= F_MEDIA_CHANGED;
579                 return;
580
581         case AER_SK_ILLEGAL_REQUEST:
582                 /* Unknown command or invalid command arguments. */
583                 if (t->flags & F_DEBUG)
584                         printf ("wfd%d: invalid command\n", t->lun);
585                 return;
586         }
587         printf ("wfd%d: i/o error, status=%b, error=%b\n", t->lun,
588                 result.status, ARS_BITS, result.error, AER_BITS);
589 }
590
591 static int wfd_request_wait (struct wfd *t, u_char cmd, u_char a1, u_char a2,
592         u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8,
593         u_char a9, char *addr, int count)
594 {
595         struct atapires result;
596
597         result = atapi_request_wait (t->ata, t->unit, cmd,
598                 a1, a2, a3, a4, a5, a6, a7, a8, a9, 0, 0, 0, 0, 0, 0,
599                 addr, count);
600         if (result.code) {
601                 wfd_error (t, result);
602                 return (EIO);
603         }
604         return (0);
605 }
606
607 /*
608  * Perform special action on behalf of the user.
609  * Knows about the internals of this device
610  */
611 int wfdioctl (dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
612 {
613         int lun = UNIT(dev);
614         struct wfd *t = wfdtab[lun];
615         int error = 0;
616
617         error = dsioctl(dev, cmd, addr, flag, &t->dk_slices);
618         if (error != ENOIOCTL)
619                 return (error);
620
621         if (t->flags & F_MEDIA_CHANGED)
622                 switch (cmd) {
623                 case CDIOCSETDEBUG:
624                 case CDIOCCLRDEBUG:
625                 case CDIOCRESET:
626                         /* These ops are media change transparent. */
627                         break;
628                 default:
629                         /* Lock the media. */
630                         wfd_request_wait (t, ATAPI_PREVENT_ALLOW,
631                                 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
632                         break;
633                 }
634         switch (cmd) {
635         case CDIOCSETDEBUG:
636                 error = suser(p);
637                 if (error)
638                         return (error);
639                 t->flags |= F_DEBUG;
640                 atapi_debug (t->ata, 1);
641                 return 0;
642         case CDIOCCLRDEBUG:
643                 error = suser(p);
644                 if (error)
645                         return (error);
646                 t->flags &= ~F_DEBUG;
647                 atapi_debug (t->ata, 0);
648                 return 0;
649         case CDIOCRESET:
650                 error = suser(p);
651                 if (error)
652                         return (error);
653                 return wfd_request_wait (t, ATAPI_TEST_UNIT_READY,
654                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
655         case CDIOCEJECT:
656                 /* Don't allow eject if the device is opened
657                  * by somebody (not us) in block mode. */
658                 if ((t->flags & F_BOPEN) && t->refcnt)
659                         return (EBUSY);
660                 return wfd_eject (t, 0);
661         case CDIOCCLOSE:
662                 if ((t->flags & F_BOPEN) && t->refcnt)
663                         return (0);
664                 return wfd_eject (t, 1);
665         default:
666                 return (ENOTTY);
667         }
668         return (error);
669 }
670
671 static int wfd_eject (struct wfd *t, int closeit)
672 {
673         struct atapires result;
674
675         /* Try to stop the disc. */
676         result = atapi_request_wait (t->ata, t->unit, ATAPI_START_STOP,
677                 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
678
679         if (result.code == RES_ERR &&
680             ((result.error & AER_SKEY) == AER_SK_NOT_READY ||
681             (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)) {
682                 int err;
683
684                 if (!closeit)
685                         return (0);
686                 /*
687                  * The disc was unloaded.
688                  * Load it (close tray).
689                  * Read the table of contents.
690                  */
691                 err = wfd_request_wait (t, ATAPI_START_STOP,
692                         0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0);
693                 if (err)
694                         return (err);
695
696                 /* Lock the media. */
697                 wfd_request_wait (t, ATAPI_PREVENT_ALLOW,
698                         0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
699
700                 return (0);
701         }
702
703         if (result.code) {
704                 wfd_error (t, result);
705                 return (EIO);
706         }
707
708         if (closeit)
709                 return (0);
710
711         /* Give it some time to stop spinning. */
712         tsleep ((caddr_t)&lbolt, PRIBIO, "wfdej1", 0);
713         tsleep ((caddr_t)&lbolt, PRIBIO, "wfdej2", 0);
714
715         /* Unlock. */
716         wfd_request_wait (t, ATAPI_PREVENT_ALLOW,
717                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
718
719         /* Eject. */
720         t->flags |= F_MEDIA_CHANGED;
721         return wfd_request_wait (t, ATAPI_START_STOP,
722                 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0);
723 }
724
725 static void     wfd_drvinit(void *unused)
726 {
727         cdevsw_add(&wfd_cdevsw);
728 }
729
730 SYSINIT(wfddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,wfd_drvinit,NULL)
731
732 #endif /* NWFD && NWDC */