2 * Copyright (c) 1998 - 2006 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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
39 #include <sys/endian.h>
42 #include <sys/taskqueue.h>
44 #include <machine/bus.h>
45 #include <geom/geom_disk.h>
46 #include <dev/ata/ata-all.h>
47 #include <dev/ata/atapi-fd.h>
52 static disk_open_t afd_open;
53 static disk_close_t afd_close;
54 static disk_strategy_t afd_strategy;
55 static disk_ioctl_t afd_ioctl;
56 static int afd_sense(device_t);
57 static void afd_describe(device_t);
58 static void afd_done(struct ata_request *);
59 static int afd_prevent_allow(device_t, int);
60 static int afd_test_ready(device_t);
63 static MALLOC_DEFINE(M_AFD, "afd_driver", "ATAPI floppy driver buffers");
66 afd_probe(device_t dev)
68 struct ata_device *atadev = device_get_softc(dev);
69 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
70 (atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_DIRECT)
77 afd_attach(device_t dev)
79 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
80 struct ata_device *atadev = device_get_softc(dev);
81 struct afd_softc *fdp;
83 if (!(fdp = malloc(sizeof(struct afd_softc), M_AFD, M_NOWAIT | M_ZERO))) {
84 device_printf(dev, "out of memory\n");
87 device_set_ivars(dev, fdp);
88 ATA_SETMODE(device_get_parent(dev), dev);
91 device_set_ivars(dev, NULL);
95 atadev->flags |= ATA_D_MEDIA_CHANGED;
97 /* announce we are here */
100 /* create the disk device */
101 fdp->disk = disk_alloc();
102 fdp->disk->d_open = afd_open;
103 fdp->disk->d_close = afd_close;
104 fdp->disk->d_strategy = afd_strategy;
105 fdp->disk->d_ioctl = afd_ioctl;
106 fdp->disk->d_name = "afd";
107 fdp->disk->d_drv1 = dev;
109 fdp->disk->d_maxsize = ch->dma->max_iosize;
111 fdp->disk->d_maxsize = DFLTPHYS;
112 fdp->disk->d_unit = device_get_unit(dev);
113 disk_create(fdp->disk, DISK_VERSION);
118 afd_detach(device_t dev)
120 struct afd_softc *fdp = device_get_ivars(dev);
122 /* check that we have a valid device to detach */
123 if (!device_get_ivars(dev))
126 /* detroy disk from the system so we dont get any further requests */
127 disk_destroy(fdp->disk);
129 /* fail requests on the queue and any thats "in flight" for this device */
130 ata_fail_requests(dev);
132 /* dont leave anything behind */
133 device_set_ivars(dev, NULL);
139 afd_shutdown(device_t dev)
141 struct ata_device *atadev = device_get_softc(dev);
143 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
144 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
148 afd_reinit(device_t dev)
150 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
151 struct ata_device *atadev = device_get_softc(dev);
152 struct afd_softc *fdp = device_get_ivars(dev);
154 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
155 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
156 device_set_ivars(dev, NULL);
160 ATA_SETMODE(device_get_parent(dev), dev);
165 afd_open(struct disk *dp)
167 device_t dev = dp->d_drv1;
168 struct ata_device *atadev = device_get_softc(dev);
169 struct afd_softc *fdp = device_get_ivars(dev);
173 if (!device_is_attached(dev))
177 afd_prevent_allow(dev, 1);
180 device_printf(dev, "sense media type failed\n");
181 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
186 fdp->disk->d_sectorsize = fdp->sectorsize;
187 fdp->disk->d_mediasize = fdp->mediasize;
188 fdp->disk->d_fwsectors = fdp->sectors;
189 fdp->disk->d_fwheads = fdp->heads;
194 afd_close(struct disk *dp)
196 device_t dev = dp->d_drv1;
198 afd_prevent_allow(dev, 0);
203 afd_strategy(struct bio *bp)
205 device_t dev = bp->bio_disk->d_drv1;
206 struct ata_device *atadev = device_get_softc(dev);
207 struct afd_softc *fdp = device_get_ivars(dev);
208 struct ata_request *request;
212 /* if it's a null transfer, return immediatly. */
213 if (bp->bio_bcount == 0) {
219 /* should reject all queued entries if media have changed. */
220 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
221 biofinish(bp, NULL, EIO);
225 count = bp->bio_bcount / fdp->sectorsize;
226 bp->bio_resid = bp->bio_bcount;
228 bzero(ccb, sizeof(ccb));
230 if (bp->bio_cmd == BIO_READ)
231 ccb[0] = ATAPI_READ_BIG;
233 ccb[0] = ATAPI_WRITE_BIG;
235 ccb[2] = bp->bio_pblkno >> 24;
236 ccb[3] = bp->bio_pblkno >> 16;
237 ccb[4] = bp->bio_pblkno >> 8;
238 ccb[5] = bp->bio_pblkno;
242 if (!(request = ata_alloc_request())) {
243 biofinish(bp, NULL, ENOMEM);
248 bcopy(ccb, request->u.atapi.ccb,
249 (atadev->param.config & ATA_PROTO_MASK) ==
250 ATA_PROTO_ATAPI_12 ? 16 : 12);
251 request->data = bp->bio_data;
252 request->bytecount = count * fdp->sectorsize;
253 request->transfersize = min(request->bytecount, 65534);
254 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
255 request->retries = 2;
256 request->callback = afd_done;
257 switch (bp->bio_cmd) {
259 request->flags = (ATA_R_ATAPI | ATA_R_READ);
262 request->flags = (ATA_R_ATAPI | ATA_R_WRITE);
265 device_printf(dev, "unknown BIO operation\n");
266 ata_free_request(request);
267 biofinish(bp, NULL, EIO);
270 if (atadev->mode >= ATA_DMA)
271 request->flags |= ATA_R_DMA;
272 request->flags |= ATA_R_ORDERED;
273 ata_queue_request(request);
277 afd_done(struct ata_request *request)
279 struct bio *bp = request->bio;
281 /* finish up transfer */
282 if ((bp->bio_error = request->result))
283 bp->bio_flags |= BIO_ERROR;
284 bp->bio_resid = bp->bio_bcount - request->donecount;
286 ata_free_request(request);
290 afd_ioctl(struct disk *disk, u_long cmd, void *data, int flag,struct thread *td)
292 return ata_device_ioctl(disk->d_drv1, cmd, data);
296 afd_sense(device_t dev)
298 struct ata_device *atadev = device_get_softc(dev);
299 struct afd_softc *fdp = device_get_ivars(dev);
300 struct afd_capacity capacity;
301 struct afd_capacity_big capacity_big;
302 struct afd_capabilities capabilities;
303 int8_t ccb1[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
304 0, 0, 0, 0, 0, 0, 0, 0 };
305 int8_t ccb2[16] = { ATAPI_SERVICE_ACTION_IN, 0x10, 0, 0, 0, 0, 0, 0, 0, 0,
306 0, 0, 0, sizeof(struct afd_capacity_big) & 0xff, 0, 0 };
307 int8_t ccb3[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
308 0, 0, 0, 0, sizeof(struct afd_capabilities) >> 8,
309 sizeof(struct afd_capabilities) & 0xff,
310 0, 0, 0, 0, 0, 0, 0 };
316 /* wait for device to get ready */
317 while ((error = afd_test_ready(dev)) && timeout--) {
323 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
324 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) {
327 fdp->mediasize = 39441 * 1024;
328 fdp->sectorsize = 512;
333 /* get drive capacity */
334 if (!ata_atapicmd(dev, ccb1, (caddr_t)&capacity,
335 sizeof(struct afd_capacity), ATA_R_READ, 30)) {
338 fdp->sectorsize = be32toh(capacity.blocksize);
339 fdp->mediasize = (u_int64_t)be32toh(capacity.capacity)*fdp->sectorsize;
344 /* get drive capacity big */
345 if (!ata_atapicmd(dev, ccb2, (caddr_t)&capacity_big,
346 sizeof(struct afd_capacity_big),
347 ATA_R_READ | ATA_R_QUIET, 30)) {
350 fdp->sectorsize = be32toh(capacity_big.blocksize);
351 fdp->mediasize = be64toh(capacity_big.capacity)*fdp->sectorsize;
356 /* get drive capabilities, some bugridden drives needs this repeated */
357 for (count = 0 ; count < 5 ; count++) {
358 if (!ata_atapicmd(dev, ccb3, (caddr_t)&capabilities,
359 sizeof(struct afd_capabilities), ATA_R_READ, 30) &&
360 capabilities.page_code == ATAPI_REWRITEABLE_CAP_PAGE) {
361 fdp->heads = capabilities.heads;
362 fdp->sectors = capabilities.sectors;
363 fdp->sectorsize = be16toh(capabilities.sector_size);
364 fdp->mediasize = be16toh(capabilities.cylinders) *
365 fdp->heads * fdp->sectors * fdp->sectorsize;
366 if (!capabilities.medium_type)
375 afd_prevent_allow(device_t dev, int lock)
377 struct ata_device *atadev = device_get_softc(dev);
378 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
381 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12))
383 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
387 afd_test_ready(device_t dev)
389 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
390 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
392 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
396 afd_describe(device_t dev)
398 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
399 struct ata_device *atadev = device_get_softc(dev);
400 struct afd_softc *fdp = device_get_ivars(dev);
403 if (fdp->mediasize > 1048576 * 5)
404 sprintf(sizestring, "%juMB", fdp->mediasize / 1048576);
405 else if (fdp->mediasize)
406 sprintf(sizestring, "%juKB", fdp->mediasize / 1024);
408 strcpy(sizestring, "(no media)");
410 device_printf(dev, "%s <%.40s %.8s> at ata%d-%s %s\n",
411 sizestring, atadev->param.model, atadev->param.revision,
412 device_get_unit(ch->dev),
413 (atadev->unit == ATA_MASTER) ? "master" : "slave",
414 ata_mode2str(atadev->mode));
416 device_printf(dev, "%ju sectors [%juC/%dH/%dS]\n",
417 fdp->mediasize / fdp->sectorsize,
418 fdp->mediasize /(fdp->sectorsize*fdp->sectors*fdp->heads),
419 fdp->heads, fdp->sectors);
423 static device_method_t afd_methods[] = {
424 /* device interface */
425 DEVMETHOD(device_probe, afd_probe),
426 DEVMETHOD(device_attach, afd_attach),
427 DEVMETHOD(device_detach, afd_detach),
428 DEVMETHOD(device_shutdown, afd_shutdown),
431 DEVMETHOD(ata_reinit, afd_reinit),
436 static driver_t afd_driver = {
442 static devclass_t afd_devclass;
444 DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL);
445 MODULE_VERSION(afd, 1);
446 MODULE_DEPEND(afd, ata, 1, 1, 1);