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.4 2003/06/23 17:55:29 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>
45 #include <dev/ata/ata-all.h>
46 #include <dev/ata/atapi-all.h>
47 #include <dev/ata/atapi-fd.h>
49 /* device structures */
50 static d_open_t afdopen;
51 static d_close_t afdclose;
52 static d_ioctl_t afdioctl;
53 static d_strategy_t afdstrategy;
54 static struct cdevsw afd_cdevsw = {
58 /* write */ physwrite,
62 /* strategy */ afdstrategy,
67 /* flags */ D_DISK | D_TRACKCLOSE,
69 static struct cdevsw afddisk_cdevsw;
72 static int afd_sense(struct afd_softc *);
73 static void afd_describe(struct afd_softc *);
74 static int afd_done(struct atapi_request *);
75 static int afd_eject(struct afd_softc *, int);
76 static int afd_start_stop(struct afd_softc *, int);
77 static int afd_prevent_allow(struct afd_softc *, int);
80 static u_int32_t afd_lun_map = 0;
81 static MALLOC_DEFINE(M_AFD, "AFD driver", "ATAPI floppy driver buffers");
84 afdattach(struct ata_device *atadev)
86 struct afd_softc *fdp;
89 fdp = malloc(sizeof(struct afd_softc), M_AFD, M_NOWAIT | M_ZERO);
91 ata_prtdev(atadev, "out of memory\n");
96 fdp->lun = ata_get_lun(&afd_lun_map);
97 ata_set_name(atadev, "afd", fdp->lun);
98 bufq_init(&fdp->queue);
100 if (afd_sense(fdp)) {
105 devstat_add_entry(&fdp->stats, "afd", fdp->lun, DEV_BSIZE,
106 DEVSTAT_NO_ORDERED_TAGS,
107 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
108 DEVSTAT_PRIORITY_WFD);
109 dev = disk_create(fdp->lun, &fdp->disk, 0, &afd_cdevsw, &afddisk_cdevsw);
113 if (!strncmp(atadev->param->model, "IOMEGA ZIP", 10) ||
114 !strncmp(atadev->param->model, "IOMEGA Clik!", 12))
115 fdp->dev->si_iosize_max = 64 * DEV_BSIZE;
117 fdp->dev->si_iosize_max = 252 * DEV_BSIZE;
120 atadev->flags |= ATA_D_MEDIA_CHANGED;
121 atadev->driver = fdp;
126 afddetach(struct ata_device *atadev)
128 struct afd_softc *fdp = atadev->driver;
131 while ((bp = bufq_first(&fdp->queue))) {
132 bufq_remove(&fdp->queue, bp);
133 bp->b_flags |= B_ERROR;
137 disk_invalidate(&fdp->disk);
138 disk_destroy(fdp->dev);
139 devstat_remove_entry(&fdp->stats);
140 ata_free_name(atadev);
141 ata_free_lun(&afd_lun_map, fdp->lun);
143 atadev->driver = NULL;
147 afd_sense(struct afd_softc *fdp)
149 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
150 0, 0, 0, 0, sizeof(struct afd_cappage) >> 8,
151 sizeof(struct afd_cappage) & 0xff, 0, 0, 0, 0, 0, 0, 0 };
152 int count, error = 0;
154 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
155 if (!strncmp(fdp->device->param->model, "IOMEGA Clik!", 12)) {
156 fdp->cap.transfer_rate = 500;
158 fdp->cap.sectors = 2;
159 fdp->cap.cylinders = 39441;
160 fdp->cap.sector_size = 512;
161 atapi_test_ready(fdp->device);
165 /* get drive capabilities, some drives needs this repeated */
166 for (count = 0 ; count < 5 ; count++) {
167 if (!(error = atapi_queue_cmd(fdp->device, ccb, (caddr_t)&fdp->cap,
168 sizeof(struct afd_cappage),
169 ATPR_F_READ, 30, NULL, NULL)))
172 if (error || fdp->cap.page_code != ATAPI_REWRITEABLE_CAP_PAGE)
174 fdp->cap.cylinders = ntohs(fdp->cap.cylinders);
175 fdp->cap.sector_size = ntohs(fdp->cap.sector_size);
176 fdp->cap.transfer_rate = ntohs(fdp->cap.transfer_rate);
181 afd_describe(struct afd_softc *fdp)
184 ata_prtdev(fdp->device,
185 "<%.40s/%.8s> rewriteable drive at ata%d as %s\n",
186 fdp->device->param->model, fdp->device->param->revision,
187 device_get_unit(fdp->device->channel->dev),
188 (fdp->device->unit == ATA_MASTER) ? "master" : "slave");
189 ata_prtdev(fdp->device,
190 "%luMB (%u sectors), %u cyls, %u heads, %u S/T, %u B/S\n",
191 (fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors) /
192 ((1024L * 1024L) / fdp->cap.sector_size),
193 fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors,
194 fdp->cap.cylinders, fdp->cap.heads, fdp->cap.sectors,
195 fdp->cap.sector_size);
196 ata_prtdev(fdp->device, "%dKB/s,", fdp->lun, fdp->cap.transfer_rate/8);
197 printf(" %s\n", ata_mode2str(fdp->device->mode));
198 if (fdp->cap.medium_type) {
199 ata_prtdev(fdp->device, "Medium: ");
200 switch (fdp->cap.medium_type) {
202 printf("720KB DD disk"); break;
205 printf("1.2MB HD disk"); break;
208 printf("1.44MB HD disk"); break;
211 printf("120MB UHD disk"); break;
214 printf("Unknown (0x%x)", fdp->cap.medium_type);
216 if (fdp->cap.wp) printf(", writeprotected");
221 ata_prtdev(fdp->device, "%luMB <%.40s> [%d/%d/%d] at ata%d-%s %s\n",
222 (fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors) /
223 ((1024L * 1024L) / fdp->cap.sector_size),
224 fdp->device->param->model,
225 fdp->cap.cylinders, fdp->cap.heads, fdp->cap.sectors,
226 device_get_unit(fdp->device->channel->dev),
227 (fdp->device->unit == ATA_MASTER) ? "master" : "slave",
228 ata_mode2str(fdp->device->mode));
233 afdopen(dev_t dev, int flags, int fmt, struct thread *td)
235 struct afd_softc *fdp = dev->si_drv1;
236 struct disklabel *label = &fdp->disk.d_label;
238 atapi_test_ready(fdp->device);
240 if (count_dev(dev) == 1)
241 afd_prevent_allow(fdp, 1);
244 ata_prtdev(fdp->device, "sense media type failed\n");
246 fdp->device->flags &= ~ATA_D_MEDIA_CHANGED;
248 bzero(label, sizeof *label);
249 label->d_secsize = fdp->cap.sector_size;
250 label->d_nsectors = fdp->cap.sectors;
251 label->d_ntracks = fdp->cap.heads;
252 label->d_ncylinders = fdp->cap.cylinders;
253 label->d_secpercyl = fdp->cap.sectors * fdp->cap.heads;
254 label->d_secperunit = label->d_secpercyl * fdp->cap.cylinders;
259 afdclose(dev_t dev, int flags, int fmt, struct thread *td)
261 struct afd_softc *fdp = dev->si_drv1;
263 if (count_dev(dev) == 1)
264 afd_prevent_allow(fdp, 0);
269 afdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
271 struct afd_softc *fdp = dev->si_drv1;
275 if (count_dev(dev) > 1)
277 return afd_eject(fdp, 0);
280 if (count_dev(dev) > 1)
282 return afd_eject(fdp, 1);
290 afdstrategy(struct buf *bp)
292 struct afd_softc *fdp = bp->b_dev->si_drv1;
295 if (fdp->device->flags & ATA_D_DETACHING) {
296 bp->b_flags |= B_ERROR;
302 /* if it's a null transfer, return immediatly. */
303 if (bp->b_bcount == 0) {
310 bufqdisksort(&fdp->queue, bp);
312 ata_start(fdp->device->channel);
316 afd_start(struct ata_device *atadev)
318 struct afd_softc *fdp = atadev->driver;
319 struct buf *bp = bufq_first(&fdp->queue);
328 bufq_remove(&fdp->queue, bp);
330 /* should reject all queued entries if media have changed. */
331 if (fdp->device->flags & ATA_D_MEDIA_CHANGED) {
332 bp->b_flags |= B_ERROR;
339 count = bp->b_bcount / fdp->cap.sector_size;
340 data_ptr = bp->b_data;
341 bp->b_resid = bp->b_bcount;
343 bzero(ccb, sizeof(ccb));
345 if (bp->b_flags & B_READ)
346 ccb[0] = ATAPI_READ_BIG;
348 ccb[0] = ATAPI_WRITE_BIG;
357 devstat_start_transaction(&fdp->stats);
359 atapi_queue_cmd(fdp->device, ccb, data_ptr, count * fdp->cap.sector_size,
360 (bp->b_flags & B_READ) ? ATPR_F_READ : 0, 30,
365 afd_done(struct atapi_request *request)
367 struct buf *bp = request->driver;
368 struct afd_softc *fdp = request->device->driver;
370 if (request->error || (bp->b_flags & B_ERROR)) {
371 bp->b_error = request->error;
372 bp->b_flags |= B_ERROR;
375 bp->b_resid = bp->b_bcount - request->donecount;
376 devstat_end_transaction_buf(&fdp->stats, bp);
382 afd_eject(struct afd_softc *fdp, int close)
386 if ((error = afd_start_stop(fdp, 0)) == EBUSY) {
389 if ((error = afd_start_stop(fdp, 3)))
391 return afd_prevent_allow(fdp, 1);
397 if ((error = afd_prevent_allow(fdp, 0)))
399 fdp->device->flags |= ATA_D_MEDIA_CHANGED;
400 return afd_start_stop(fdp, 2);
404 afd_start_stop(struct afd_softc *fdp, int start)
406 int8_t ccb[16] = { ATAPI_START_STOP, 0, 0, 0, start,
407 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
409 return atapi_queue_cmd(fdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);
413 afd_prevent_allow(struct afd_softc *fdp, int lock)
415 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
416 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
418 if (!strncmp(fdp->device->param->model, "IOMEGA Clik!", 12))
420 return atapi_queue_cmd(fdp->device, ccb, NULL, 0, 0, 30, NULL, NULL);