2 * Copyright (c) 1998 - 2005 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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
43 #include <sys/taskqueue.h>
45 #include <machine/bus.h>
46 #include <geom/geom_disk.h>
47 #include <dev/ata/ata-all.h>
48 #include <dev/ata/atapi-fd.h>
53 static disk_open_t afd_open;
54 static disk_close_t afd_close;
55 static disk_strategy_t afd_strategy;
56 static disk_ioctl_t afd_ioctl;
57 static int afd_sense(device_t);
58 static void afd_describe(device_t);
59 static void afd_done(struct ata_request *);
60 static int afd_prevent_allow(device_t, int);
61 static int afd_test_ready(device_t);
64 static MALLOC_DEFINE(M_AFD, "AFD driver", "ATAPI floppy driver buffers");
67 afd_probe(device_t dev)
69 struct ata_device *atadev = device_get_softc(dev);
71 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
72 (atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_DIRECT)
79 afd_attach(device_t dev)
81 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
82 struct ata_device *atadev = device_get_softc(dev);
83 struct afd_softc *fdp;
85 if (!(fdp = malloc(sizeof(struct afd_softc), M_AFD, M_NOWAIT | M_ZERO))) {
86 device_printf(dev, "out of memory\n");
89 device_set_ivars(dev, fdp);
90 ATA_SETMODE(device_get_parent(dev), dev);
93 device_set_ivars(dev, NULL);
97 atadev->flags |= ATA_D_MEDIA_CHANGED;
99 /* announce we are here */
102 /* create the disk device */
103 fdp->disk = disk_alloc();
104 fdp->disk->d_open = afd_open;
105 fdp->disk->d_close = afd_close;
106 fdp->disk->d_strategy = afd_strategy;
107 fdp->disk->d_ioctl = afd_ioctl;
108 fdp->disk->d_name = "afd";
109 fdp->disk->d_drv1 = dev;
111 fdp->disk->d_maxsize = ch->dma->max_iosize;
113 fdp->disk->d_maxsize = DFLTPHYS;
114 fdp->disk->d_unit = device_get_unit(dev);
115 disk_create(fdp->disk, DISK_VERSION);
120 afd_detach(device_t dev)
122 struct afd_softc *fdp = device_get_ivars(dev);
124 /* detroy disk from the system so we dont get any further requests */
125 disk_destroy(fdp->disk);
127 /* fail requests on the queue and any thats "in flight" for this device */
128 ata_fail_requests(dev);
130 /* dont leave anything behind */
131 device_set_ivars(dev, NULL);
137 afd_shutdown(device_t dev)
139 struct ata_device *atadev = device_get_softc(dev);
141 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
142 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
146 afd_reinit(device_t dev)
148 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
149 struct ata_device *atadev = device_get_softc(dev);
150 struct afd_softc *fdp = device_get_ivars(dev);
152 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
153 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
154 device_set_ivars(dev, NULL);
158 ATA_SETMODE(device_get_parent(dev), dev);
163 afd_open(struct disk *dp)
165 device_t dev = dp->d_drv1;
166 struct ata_device *atadev = device_get_softc(dev);
167 struct afd_softc *fdp = device_get_ivars(dev);
171 if (!device_is_attached(dev))
175 afd_prevent_allow(dev, 1);
178 device_printf(dev, "sense media type failed\n");
179 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
181 fdp->disk->d_sectorsize = fdp->cap.sector_size;
182 fdp->disk->d_mediasize = (off_t)fdp->cap.sector_size * fdp->cap.sectors *
183 fdp->cap.heads * fdp->cap.cylinders;
184 fdp->disk->d_fwsectors = fdp->cap.sectors;
185 fdp->disk->d_fwheads = fdp->cap.heads;
191 afd_close(struct disk *dp)
193 device_t dev = dp->d_drv1;
195 afd_prevent_allow(dev, 0);
200 afd_strategy(struct bio *bp)
202 device_t dev = bp->bio_disk->d_drv1;
203 struct ata_device *atadev = device_get_softc(dev);
204 struct afd_softc *fdp = device_get_ivars(dev);
205 struct ata_request *request;
209 /* if it's a null transfer, return immediatly. */
210 if (bp->bio_bcount == 0) {
216 /* should reject all queued entries if media have changed. */
217 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
218 biofinish(bp, NULL, EIO);
222 count = bp->bio_bcount / fdp->cap.sector_size;
223 bp->bio_resid = bp->bio_bcount;
225 bzero(ccb, sizeof(ccb));
227 if (bp->bio_cmd == BIO_READ)
228 ccb[0] = ATAPI_READ_BIG;
230 ccb[0] = ATAPI_WRITE_BIG;
232 ccb[2] = bp->bio_pblkno >> 24;
233 ccb[3] = bp->bio_pblkno >> 16;
234 ccb[4] = bp->bio_pblkno >> 8;
235 ccb[5] = bp->bio_pblkno;
239 if (!(request = ata_alloc_request())) {
240 biofinish(bp, NULL, ENOMEM);
245 bcopy(ccb, request->u.atapi.ccb,
246 (atadev->param.config & ATA_PROTO_MASK) ==
247 ATA_PROTO_ATAPI_12 ? 16 : 12);
248 request->data = bp->bio_data;
249 request->bytecount = count * fdp->cap.sector_size;
250 request->transfersize = min(request->bytecount, 65534);
251 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
252 request->retries = 2;
253 request->callback = afd_done;
254 switch (bp->bio_cmd) {
256 request->flags = (ATA_R_ATAPI | ATA_R_READ);
259 request->flags = (ATA_R_ATAPI | ATA_R_WRITE);
262 device_printf(dev, "unknown BIO operation\n");
263 ata_free_request(request);
264 biofinish(bp, NULL, EIO);
267 if (atadev->mode >= ATA_DMA)
268 request->flags |= ATA_R_DMA;
269 request->flags |= ATA_R_ORDERED;
270 ata_queue_request(request);
274 afd_done(struct ata_request *request)
276 struct bio *bp = request->bio;
278 /* finish up transfer */
279 if ((bp->bio_error = request->result))
280 bp->bio_flags |= BIO_ERROR;
281 bp->bio_resid = bp->bio_bcount - request->donecount;
283 ata_free_request(request);
287 afd_ioctl(struct disk *disk, u_long cmd, void *data, int flag,struct thread *td)
289 return ata_device_ioctl(disk->d_drv1, cmd, data);
293 afd_sense(device_t dev)
295 struct ata_device *atadev = device_get_softc(dev);
296 struct afd_softc *fdp = device_get_ivars(dev);
297 int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
298 0, 0, 0, 0, sizeof(struct afd_cappage) >> 8,
299 sizeof(struct afd_cappage) & 0xff, 0, 0, 0, 0, 0, 0, 0 };
302 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
303 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) {
304 fdp->cap.transfer_rate = 500;
306 fdp->cap.sectors = 2;
307 fdp->cap.cylinders = 39441;
308 fdp->cap.sector_size = 512;
313 /* get drive capabilities, some bugridden drives needs this repeated */
314 for (count = 0 ; count < 5 ; count++) {
315 if (!ata_atapicmd(dev, ccb, (caddr_t)&fdp->cap,
316 sizeof(struct afd_cappage), ATA_R_READ, 30) &&
317 fdp->cap.page_code == ATAPI_REWRITEABLE_CAP_PAGE) {
318 fdp->cap.cylinders = ntohs(fdp->cap.cylinders);
319 fdp->cap.sector_size = ntohs(fdp->cap.sector_size);
320 fdp->cap.transfer_rate = ntohs(fdp->cap.transfer_rate);
328 afd_prevent_allow(device_t dev, int lock)
330 struct ata_device *atadev = device_get_softc(dev);
331 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
332 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
334 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12))
336 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
340 afd_test_ready(device_t dev)
342 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
343 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
345 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
349 afd_describe(device_t dev)
351 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
352 struct ata_device *atadev = device_get_softc(dev);
353 struct afd_softc *fdp = device_get_ivars(dev);
356 device_printf(dev, "<%.40s/%.8s> removable drive at ata%d as %s\n",
357 atadev->param.model, atadev->param.revision,
358 device_get_unit(ch->dev),
359 (atadev->unit == ATA_MASTER) ? "master" : "slave");
361 "%luMB (%u sectors), %u cyls, %u heads, %u S/T, %u B/S\n",
362 (fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors) /
363 ((1024L * 1024L) / fdp->cap.sector_size),
364 fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors,
365 fdp->cap.cylinders, fdp->cap.heads, fdp->cap.sectors,
366 fdp->cap.sector_size);
367 device_printf(dev, "%dKB/s,", fdp->cap.transfer_rate / 8);
368 printf(" %s\n", ata_mode2str(atadev->mode));
369 if (fdp->cap.medium_type) {
370 device_printf(dev, "Medium: ");
371 switch (fdp->cap.medium_type) {
373 printf("720KB DD disk"); break;
376 printf("1.2MB HD disk"); break;
379 printf("1.44MB HD disk"); break;
382 printf("120MB UHD disk"); break;
385 printf("Unknown (0x%x)", fdp->cap.medium_type);
387 if (fdp->cap.wp) printf(", writeprotected");
392 device_printf(dev, "REMOVABLE <%.40s/%.8s> at ata%d-%s %s\n",
393 atadev->param.model, atadev->param.revision,
394 device_get_unit(ch->dev),
395 (atadev->unit == ATA_MASTER) ? "master" : "slave",
396 ata_mode2str(atadev->mode));
400 static device_method_t afd_methods[] = {
401 /* device interface */
402 DEVMETHOD(device_probe, afd_probe),
403 DEVMETHOD(device_attach, afd_attach),
404 DEVMETHOD(device_detach, afd_detach),
405 DEVMETHOD(device_shutdown, afd_shutdown),
408 DEVMETHOD(ata_reinit, afd_reinit),
413 static driver_t afd_driver = {
419 static devclass_t afd_devclass;
421 DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL);
422 MODULE_VERSION(afd, 1);
423 MODULE_DEPEND(afd, ata, 1, 1, 1);