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.
28 * $FreeBSD: src/sys/dev/ata/atapi-fd.c,v 1.44.2.9 2002/07/31 11:19:26 sos Exp $
29 * $DragonFly: src/sys/dev/disk/ata/atapi-fd.c,v 1.11 2004/05/13 23:49:14 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
41 #include <sys/devicestat.h>
43 #include <machine/bus.h>
47 #include "atapi-all.h"
50 /* device structures */
51 static d_open_t afdopen;
52 static d_close_t afdclose;
53 static d_ioctl_t afdioctl;
54 static d_strategy_t afdstrategy;
56 static struct cdevsw afd_cdevsw = {
59 /* flags */ D_DISK | D_TRACKCLOSE,
66 /* write */ physwrite,
70 /* strategy */ afdstrategy,
76 static int afd_sense(struct afd_softc *);
77 static void afd_describe(struct afd_softc *);
78 static int afd_done(struct atapi_request *);
79 static int afd_eject(struct afd_softc *, int);
80 static int afd_start_stop(struct afd_softc *, int);
81 static int afd_prevent_allow(struct afd_softc *, int);
84 static u_int32_t afd_lun_map = 0;
85 static MALLOC_DEFINE(M_AFD, "AFD driver", "ATAPI floppy driver buffers");
88 afdattach(struct ata_device *atadev)
90 struct afd_softc *fdp;
93 fdp = malloc(sizeof(struct afd_softc), M_AFD, M_WAITOK | M_ZERO);
95 ata_prtdev(atadev, "out of memory\n");
100 fdp->lun = ata_get_lun(&afd_lun_map);
101 ata_set_name(atadev, "afd", fdp->lun);
102 bufq_init(&fdp->queue);
104 if (afd_sense(fdp)) {
109 devstat_add_entry(&fdp->stats, "afd", fdp->lun, DEV_BSIZE,
110 DEVSTAT_NO_ORDERED_TAGS,
111 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
112 DEVSTAT_PRIORITY_WFD);
113 dev = disk_create(fdp->lun, &fdp->disk, 0, &afd_cdevsw);
117 if (!strncmp(atadev->param->model, "IOMEGA ZIP", 10) ||
118 !strncmp(atadev->param->model, "IOMEGA Clik!", 12))
119 fdp->dev->si_iosize_max = 64 * DEV_BSIZE;
121 fdp->dev->si_iosize_max = 252 * DEV_BSIZE;
124 atadev->flags |= ATA_D_MEDIA_CHANGED;
125 atadev->driver = fdp;
130 afddetach(struct ata_device *atadev)
132 struct afd_softc *fdp = atadev->driver;
135 while ((bp = bufq_first(&fdp->queue))) {
136 bufq_remove(&fdp->queue, bp);
137 bp->b_flags |= B_ERROR;
141 disk_invalidate(&fdp->disk);
142 disk_destroy(&fdp->disk);
143 devstat_remove_entry(&fdp->stats);
144 ata_free_name(atadev);
145 ata_free_lun(&afd_lun_map, fdp->lun);
147 atadev->driver = NULL;
151 afd_sense(struct afd_softc *fdp)
153 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
154 0, 0, 0, 0, sizeof(struct afd_cappage) >> 8,
155 sizeof(struct afd_cappage) & 0xff, 0, 0, 0, 0, 0, 0, 0 };
156 int count, error = 0;
158 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
159 if (!strncmp(fdp->device->param->model, "IOMEGA Clik!", 12)) {
160 fdp->cap.transfer_rate = 500;
162 fdp->cap.sectors = 2;
163 fdp->cap.cylinders = 39441;
164 fdp->cap.sector_size = 512;
165 atapi_test_ready(fdp->device);
169 /* get drive capabilities, some drives needs this repeated */
170 for (count = 0 ; count < 5 ; count++) {
171 if (!(error = atapi_queue_cmd(fdp->device, ccb, (caddr_t)&fdp->cap,
172 sizeof(struct afd_cappage),
173 ATPR_F_READ, 30, NULL, NULL)))
176 if (error || fdp->cap.page_code != ATAPI_REWRITEABLE_CAP_PAGE)
178 fdp->cap.cylinders = ntohs(fdp->cap.cylinders);
179 fdp->cap.sector_size = ntohs(fdp->cap.sector_size);
180 fdp->cap.transfer_rate = ntohs(fdp->cap.transfer_rate);
185 afd_describe(struct afd_softc *fdp)
188 ata_prtdev(fdp->device,
189 "<%.40s/%.8s> rewriteable drive at ata%d as %s\n",
190 fdp->device->param->model, fdp->device->param->revision,
191 device_get_unit(fdp->device->channel->dev),
192 (fdp->device->unit == ATA_MASTER) ? "master" : "slave");
193 ata_prtdev(fdp->device,
194 "%luMB (%u sectors), %u cyls, %u heads, %u S/T, %u B/S\n",
195 (fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors) /
196 ((1024L * 1024L) / fdp->cap.sector_size),
197 fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors,
198 fdp->cap.cylinders, fdp->cap.heads, fdp->cap.sectors,
199 fdp->cap.sector_size);
200 ata_prtdev(fdp->device, "%dKB/s,", fdp->cap.transfer_rate / 8);
201 printf(" %s\n", ata_mode2str(fdp->device->mode));
202 if (fdp->cap.medium_type) {
203 ata_prtdev(fdp->device, "Medium: ");
204 switch (fdp->cap.medium_type) {
206 printf("720KB DD disk"); break;
209 printf("1.2MB HD disk"); break;
212 printf("1.44MB HD disk"); break;
215 printf("120MB UHD disk"); break;
218 printf("Unknown (0x%x)", fdp->cap.medium_type);
220 if (fdp->cap.wp) printf(", writeprotected");
225 ata_prtdev(fdp->device, "%luMB <%.40s> [%d/%d/%d] at ata%d-%s %s\n",
226 (fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors) /
227 ((1024L * 1024L) / fdp->cap.sector_size),
228 fdp->device->param->model,
229 fdp->cap.cylinders, fdp->cap.heads, fdp->cap.sectors,
230 device_get_unit(fdp->device->channel->dev),
231 (fdp->device->unit == ATA_MASTER) ? "master" : "slave",
232 ata_mode2str(fdp->device->mode));
237 afdopen(dev_t dev, int flags, int fmt, struct thread *td)
239 struct afd_softc *fdp = dev->si_drv1;
240 struct disklabel *label = &fdp->disk.d_label;
242 atapi_test_ready(fdp->device);
244 if (count_dev(dev) == 1)
245 afd_prevent_allow(fdp, 1);
248 ata_prtdev(fdp->device, "sense media type failed\n");
250 fdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
252 bzero(label, sizeof *label);
253 label->d_secsize = fdp->cap.sector_size;
254 label->d_nsectors = fdp->cap.sectors;
255 label->d_ntracks = fdp->cap.heads;
256 label->d_ncylinders = fdp->cap.cylinders;
257 label->d_secpercyl = fdp->cap.sectors * fdp->cap.heads;
258 label->d_secperunit = label->d_secpercyl * fdp->cap.cylinders;
263 afdclose(dev_t dev, int flags, int fmt, struct thread *td)
265 struct afd_softc *fdp = dev->si_drv1;
267 if (count_dev(dev) == 1)
268 afd_prevent_allow(fdp, 0);
273 afdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
275 struct afd_softc *fdp = dev->si_drv1;
279 if (count_dev(dev) > 1)
281 return afd_eject(fdp, 0);
284 if (count_dev(dev) > 1)
286 return afd_eject(fdp, 1);
294 afdstrategy(struct buf *bp)
296 struct afd_softc *fdp = bp->b_dev->si_drv1;
299 if (fdp->device->flags & ATA_D_DETACHING) {
300 bp->b_flags |= B_ERROR;
306 /* if it's a null transfer, return immediatly. */
307 if (bp->b_bcount == 0) {
314 bufqdisksort(&fdp->queue, bp);
316 ata_start(fdp->device->channel);
320 afd_start(struct ata_device *atadev)
322 struct afd_softc *fdp = atadev->driver;
323 struct buf *bp = bufq_first(&fdp->queue);
332 bufq_remove(&fdp->queue, bp);
334 /* should reject all queued entries if media have changed. */
335 if (fdp->device->flags & ATA_D_MEDIA_CHANGED) {
336 bp->b_flags |= B_ERROR;
343 count = bp->b_bcount / fdp->cap.sector_size;
344 data_ptr = bp->b_data;
345 bp->b_resid = bp->b_bcount;
347 bzero(ccb, sizeof(ccb));
349 if (bp->b_flags & B_READ)
350 ccb[0] = ATAPI_READ_BIG;
352 ccb[0] = ATAPI_WRITE_BIG;
361 devstat_start_transaction(&fdp->stats);
363 atapi_queue_cmd(fdp->device, ccb, data_ptr, count * fdp->cap.sector_size,
364 (bp->b_flags & B_READ) ? ATPR_F_READ : 0, 30,
369 afd_done(struct atapi_request *request)
371 struct buf *bp = request->driver;
372 struct afd_softc *fdp = request->device->driver;
374 if (request->error || (bp->b_flags & B_ERROR)) {
375 bp->b_error = request->error;
376 bp->b_flags |= B_ERROR;
379 bp->b_resid = bp->b_bcount - request->donecount;
380 devstat_end_transaction_buf(&fdp->stats, bp);
386 afd_eject(struct afd_softc *fdp, int close)
390 if ((error = afd_start_stop(fdp, 0)) == EBUSY) {
393 if ((error = afd_start_stop(fdp, 3)))
395 return afd_prevent_allow(fdp, 1);
401 if ((error = afd_prevent_allow(fdp, 0)))
403 fdp->device->flags |= ATA_D_MEDIA_CHANGED;
404 return afd_start_stop(fdp, 2);
408 afd_start_stop(struct afd_softc *fdp, int start)
410 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
411 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
413 return atapi_queue_cmd(fdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
417 afd_prevent_allow(struct afd_softc *fdp, int lock)
419 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
420 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
422 if (!strncmp(fdp->device->param->model, "IOMEGA Clik!", 12))
424 return atapi_queue_cmd(fdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);