2 * Copyright (c) 1998,1999,2000,2001,2002 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
40 #include <sys/devicestat.h>
42 #include <machine/bus.h>
43 #include <dev/ata/ata-all.h>
44 #include <dev/ata/atapi-all.h>
45 #include <dev/ata/atapi-fd.h>
47 /* device structures */
48 static d_open_t afdopen;
49 static d_close_t afdclose;
50 static d_ioctl_t afdioctl;
51 static d_strategy_t afdstrategy;
52 static struct cdevsw afd_cdevsw = {
56 /* write */ physwrite,
60 /* strategy */ afdstrategy,
65 /* flags */ D_DISK | D_TRACKCLOSE,
67 static struct cdevsw afddisk_cdevsw;
70 static int afd_sense(struct afd_softc *);
71 static void afd_describe(struct afd_softc *);
72 static int afd_done(struct atapi_request *);
73 static int afd_eject(struct afd_softc *, int);
74 static int afd_start_stop(struct afd_softc *, int);
75 static int afd_prevent_allow(struct afd_softc *, int);
78 static u_int32_t afd_lun_map = 0;
79 static MALLOC_DEFINE(M_AFD, "AFD driver", "ATAPI floppy driver buffers");
82 afdattach(struct ata_device *atadev)
84 struct afd_softc *fdp;
87 fdp = malloc(sizeof(struct afd_softc), M_AFD, M_NOWAIT | M_ZERO);
89 ata_prtdev(atadev, "out of memory\n");
94 fdp->lun = ata_get_lun(&afd_lun_map);
95 ata_set_name(atadev, "afd", fdp->lun);
96 bioq_init(&fdp->queue);
103 devstat_add_entry(&fdp->stats, "afd", fdp->lun, DEV_BSIZE,
104 DEVSTAT_NO_ORDERED_TAGS,
105 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
106 DEVSTAT_PRIORITY_WFD);
107 dev = disk_create(fdp->lun, &fdp->disk, 0, &afd_cdevsw, &afddisk_cdevsw);
110 fdp->dev->si_iosize_max = 256 * DEV_BSIZE;
113 atadev->flags |= ATA_D_MEDIA_CHANGED;
114 atadev->driver = fdp;
119 afddetach(struct ata_device *atadev)
121 struct afd_softc *fdp = atadev->driver;
124 while ((bp = bioq_first(&fdp->queue))) {
125 bioq_remove(&fdp->queue, bp);
126 biofinish(bp, NULL, ENXIO);
128 disk_invalidate(&fdp->disk);
129 disk_destroy(fdp->dev);
130 devstat_remove_entry(&fdp->stats);
131 ata_free_name(atadev);
132 ata_free_lun(&afd_lun_map, fdp->lun);
134 atadev->driver = NULL;
138 afd_sense(struct afd_softc *fdp)
140 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
141 0, 0, 0, 0, sizeof(struct afd_cappage) >> 8,
142 sizeof(struct afd_cappage) & 0xff, 0, 0, 0, 0, 0, 0, 0 };
143 int count, error = 0;
145 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
146 if (!strncmp(fdp->device->param->model, "IOMEGA Clik!", 12)) {
147 fdp->cap.transfer_rate = 500;
149 fdp->cap.sectors = 2;
150 fdp->cap.cylinders = 39441;
151 fdp->cap.sector_size = 512;
152 atapi_test_ready(fdp->device);
156 /* get drive capabilities, some drives needs this repeated */
157 for (count = 0 ; count < 5 ; count++) {
158 if (!(error = atapi_queue_cmd(fdp->device, ccb, (caddr_t)&fdp->cap,
159 sizeof(struct afd_cappage),
160 ATPR_F_READ, 30, NULL, NULL)))
163 if (error || fdp->cap.page_code != ATAPI_REWRITEABLE_CAP_PAGE)
165 fdp->cap.cylinders = ntohs(fdp->cap.cylinders);
166 fdp->cap.sector_size = ntohs(fdp->cap.sector_size);
167 fdp->cap.transfer_rate = ntohs(fdp->cap.transfer_rate);
172 afd_describe(struct afd_softc *fdp)
175 ata_prtdev(fdp->device,
176 "<%.40s/%.8s> rewriteable drive at ata%d as %s\n",
177 fdp->device->param->model, fdp->device->param->revision,
178 device_get_unit(fdp->device->channel->dev),
179 (fdp->device->unit == ATA_MASTER) ? "master" : "slave");
180 ata_prtdev(fdp->device,
181 "%luMB (%u sectors), %u cyls, %u heads, %u S/T, %u B/S\n",
182 (fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors) /
183 ((1024L * 1024L) / fdp->cap.sector_size),
184 fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors,
185 fdp->cap.cylinders, fdp->cap.heads, fdp->cap.sectors,
186 fdp->cap.sector_size);
187 ata_prtdev(fdp->device, "%dKB/s,", fdp->cap.transfer_rate / 8);
188 printf(" %s\n", ata_mode2str(fdp->device->mode));
189 if (fdp->cap.medium_type) {
190 ata_prtdev(fdp->device, "Medium: ");
191 switch (fdp->cap.medium_type) {
193 printf("720KB DD disk"); break;
196 printf("1.2MB HD disk"); break;
199 printf("1.44MB HD disk"); break;
202 printf("120MB UHD disk"); break;
205 printf("Unknown (0x%x)", fdp->cap.medium_type);
207 if (fdp->cap.wp) printf(", writeprotected");
212 ata_prtdev(fdp->device, "%luMB <%.40s> [%d/%d/%d] at ata%d-%s %s\n",
213 (fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors) /
214 ((1024L * 1024L) / fdp->cap.sector_size),
215 fdp->device->param->model,
216 fdp->cap.cylinders, fdp->cap.heads, fdp->cap.sectors,
217 device_get_unit(fdp->device->channel->dev),
218 (fdp->device->unit == ATA_MASTER) ? "master" : "slave",
219 ata_mode2str(fdp->device->mode));
224 afdopen(dev_t dev, int flags, int fmt, struct thread *td)
226 struct afd_softc *fdp = dev->si_drv1;
227 struct disklabel *label = &fdp->disk.d_label;
229 atapi_test_ready(fdp->device);
231 if (count_dev(dev) == 1)
232 afd_prevent_allow(fdp, 1);
235 ata_prtdev(fdp->device, "sense media type failed\n");
237 fdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
239 bzero(label, sizeof *label);
240 label->d_secsize = fdp->cap.sector_size;
241 label->d_nsectors = fdp->cap.sectors;
242 label->d_ntracks = fdp->cap.heads;
243 label->d_ncylinders = fdp->cap.cylinders;
244 label->d_secpercyl = fdp->cap.sectors * fdp->cap.heads;
245 label->d_secperunit = label->d_secpercyl * fdp->cap.cylinders;
250 afdclose(dev_t dev, int flags, int fmt, struct thread *td)
252 struct afd_softc *fdp = dev->si_drv1;
254 if (count_dev(dev) == 1)
255 afd_prevent_allow(fdp, 0);
260 afdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
262 struct afd_softc *fdp = dev->si_drv1;
266 if (count_dev(dev) > 1)
268 return afd_eject(fdp, 0);
271 if (count_dev(dev) > 1)
273 return afd_eject(fdp, 1);
281 afdstrategy(struct bio *bp)
283 struct afd_softc *fdp = bp->bio_dev->si_drv1;
286 if (fdp->device->flags & ATA_D_DETACHING) {
287 biofinish(bp, NULL, ENXIO);
291 /* if it's a null transfer, return immediatly. */
292 if (bp->bio_bcount == 0) {
299 bioqdisksort(&fdp->queue, bp);
301 ata_start(fdp->device->channel);
305 afd_start(struct ata_device *atadev)
307 struct afd_softc *fdp = atadev->driver;
308 struct bio *bp = bioq_first(&fdp->queue);
317 bioq_remove(&fdp->queue, bp);
319 /* should reject all queued entries if media have changed. */
320 if (fdp->device->flags & ATA_D_MEDIA_CHANGED) {
321 biofinish(bp, NULL, EIO);
325 lba = bp->bio_pblkno;
326 count = bp->bio_bcount / fdp->cap.sector_size;
327 data_ptr = bp->bio_data;
328 bp->bio_resid = bp->bio_bcount;
330 bzero(ccb, sizeof(ccb));
332 if (bp->bio_cmd == BIO_READ)
333 ccb[0] = ATAPI_READ_BIG;
335 ccb[0] = ATAPI_WRITE_BIG;
344 devstat_start_transaction(&fdp->stats);
346 atapi_queue_cmd(fdp->device, ccb, data_ptr, count * fdp->cap.sector_size,
347 (bp->bio_cmd == BIO_READ) ? ATPR_F_READ : 0, 30,
352 afd_done(struct atapi_request *request)
354 struct bio *bp = request->driver;
355 struct afd_softc *fdp = request->device->driver;
357 if (request->error || (bp->bio_flags & BIO_ERROR)) {
358 bp->bio_error = request->error;
359 bp->bio_flags |= BIO_ERROR;
362 bp->bio_resid = bp->bio_bcount - request->donecount;
363 biofinish(bp, &fdp->stats, 0);
368 afd_eject(struct afd_softc *fdp, int close)
372 if ((error = afd_start_stop(fdp, 0)) == EBUSY) {
375 if ((error = afd_start_stop(fdp, 3)))
377 return afd_prevent_allow(fdp, 1);
383 if ((error = afd_prevent_allow(fdp, 0)))
385 fdp->device->flags |= ATA_D_MEDIA_CHANGED;
386 return afd_start_stop(fdp, 2);
390 afd_start_stop(struct afd_softc *fdp, int start)
392 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
393 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
395 return atapi_queue_cmd(fdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
399 afd_prevent_allow(struct afd_softc *fdp, int lock)
401 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
402 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
404 if (!strncmp(fdp->device->param->model, "IOMEGA Clik!", 12))
406 return atapi_queue_cmd(fdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);