blob: ddce28e77a4e5485b1ba5a726920068dba17ff44 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkovd3f20842008-02-01 23:09:33 +01002 * IDE ATAPI floppy driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2000-2002 Paul Bristow <paul@paulbristow.net>
6 * Copyright (C) 2005 Bartlomiej Zolnierkiewicz
Borislav Petkov0571c7a2008-02-02 19:56:38 +01007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This driver supports the following IDE floppy drives:
9 *
10 * LS-120/240 SuperDisk
11 * Iomega Zip 100/250
12 * Iomega PC Card Clik!/PocketZip
13 *
Borislav Petkovd3f20842008-02-01 23:09:33 +010014 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020018#define DRV_NAME "ide-floppy"
19
Borislav Petkovfc6c5bc2008-02-02 19:56:38 +010020#define IDEFLOPPY_VERSION "1.00"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <linux/major.h>
31#include <linux/errno.h>
32#include <linux/genhd.h>
33#include <linux/slab.h>
34#include <linux/cdrom.h>
35#include <linux/ide.h>
Bartlomiej Zolnierkiewicz3ceca722008-10-10 22:39:27 +020036#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080038#include <linux/mutex.h>
Borislav Petkovb98b3402008-10-10 22:39:35 +020039#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +020041#include <scsi/scsi_ioctl.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/byteorder.h>
Borislav Petkov7e8b1632008-02-02 19:56:34 +010044#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
48
Borislav Petkovf373bd82008-02-02 19:56:36 +010049/* define to see debug info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define IDEFLOPPY_DEBUG_LOG 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
Borislav Petkov0571c7a2008-02-02 19:56:38 +010053#define IDEFLOPPY_DEBUG(fmt, args...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#if IDEFLOPPY_DEBUG_LOG
Borislav Petkovbcc77d92008-02-02 19:56:34 +010056#define debug_log(fmt, args...) \
57 printk(KERN_INFO "ide-floppy: " fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
Borislav Petkov0571c7a2008-02-02 19:56:38 +010059#define debug_log(fmt, args...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
62
Borislav Petkov0571c7a2008-02-02 19:56:38 +010063/* Some drives require a longer irq timeout. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
65
66/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +010067 * After each failed packet command we issue a request sense command and retry
68 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
70#define IDEFLOPPY_MAX_PC_RETRIES 3
71
Borislav Petkov194ec0c2008-02-02 19:56:35 +010072/* format capacities descriptor codes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define CAPACITY_INVALID 0x00
74#define CAPACITY_UNFORMATTED 0x01
75#define CAPACITY_CURRENT 0x02
76#define CAPACITY_NO_CARTRIDGE 0x03
77
78/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +010079 * Most of our global data which we need to save even as we leave the driver
80 * due to an interrupt or a timer event is stored in a variable of type
81 * idefloppy_floppy_t, defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83typedef struct ide_floppy_obj {
84 ide_drive_t *drive;
85 ide_driver_t *driver;
86 struct gendisk *disk;
87 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +010088 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 /* Current packet command */
Borislav Petkov8e555122008-04-18 00:46:27 +020091 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* Last failed packet command */
Borislav Petkov8e555122008-04-18 00:46:27 +020093 struct ide_atapi_pc *failed_pc;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +020094 /* used for blk_{fs,pc}_request() requests */
95 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +020096
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +020097 struct ide_atapi_pc request_sense_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +020098 struct request request_sense_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100100 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 u8 sense_key, asc, ascq;
102 /* delay this long before sending packet command */
103 u8 ticks;
104 int progress_indication;
105
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100106 /* Device information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* Current format */
108 int blocks, block_size, bs_factor;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100109 /* Last format capacity descriptor */
110 u8 cap_desc[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* Copy of the flexible disk page */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100112 u8 flexible_disk_page[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* Write protect */
114 int wp;
115 /* Supports format progress report */
116 int srfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117} idefloppy_floppy_t;
118
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200119#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100121/* IOCTLs used in low-level formatting. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
123#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
124#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
125#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
126
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100127/* Error code returned in rq->errors to the higher part of the driver. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define IDEFLOPPY_ERROR_GENERAL 101
129
130/*
Borislav Petkov948391d2008-02-02 19:56:34 +0100131 * Pages of the SELECT SENSE / MODE SENSE packet commands.
132 * See SFF-8070i spec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
135#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
136
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800137static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
140
141#define ide_floppy_g(disk) \
142 container_of((disk)->private_data, struct ide_floppy_obj, driver)
143
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200144static void idefloppy_cleanup_obj(struct kref *);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
147{
148 struct ide_floppy_obj *floppy = NULL;
149
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800150 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 floppy = ide_floppy_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200152 if (floppy) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200153 if (ide_device_get(floppy->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200154 floppy = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200155 else
156 kref_get(&floppy->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200157 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800158 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return floppy;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static void ide_floppy_put(struct ide_floppy_obj *floppy)
163{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200164 ide_drive_t *drive = floppy->drive;
165
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800166 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100167 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200168 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800169 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100173 * Used to finish servicing a request. For read/write requests, we will call
174 * ide_end_request to pass to the next buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200176static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 idefloppy_floppy_t *floppy = drive->driver_data;
179 struct request *rq = HWGROUP(drive)->rq;
180 int error;
181
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100182 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 switch (uptodate) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100185 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
186 case 1: error = 0; break;
187 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 if (error)
190 floppy->failed_pc = NULL;
191 /* Why does this happen? */
192 if (!rq)
193 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200194 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* our real local end request function */
196 ide_end_request(drive, uptodate, nsecs);
197 return 0;
198 }
199 rq->errors = error;
200 /* fixme: need to move this local also */
201 ide_end_drive_cmd(drive, 0, 0);
202 return 0;
203}
204
Borislav Petkov8e555122008-04-18 00:46:27 +0200205static void idefloppy_update_buffers(ide_drive_t *drive,
206 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct request *rq = pc->rq;
209 struct bio *bio = rq->bio;
210
211 while ((bio = rq->bio) != NULL)
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200212 idefloppy_end_request(drive, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Borislav Petkov81f49382008-07-15 21:21:56 +0200215static void ide_floppy_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov81f49382008-07-15 21:21:56 +0200218 struct ide_atapi_pc *pc = floppy->pc;
219 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100221 debug_log("Reached %s\n", __func__);
222
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200223 if (floppy->failed_pc == pc)
224 floppy->failed_pc = NULL;
225
Borislav Petkov81f49382008-07-15 21:21:56 +0200226 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
227 (pc->rq && blk_pc_request(pc->rq)))
228 uptodate = 1; /* FIXME */
229 else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
230 u8 *buf = floppy->pc->buf;
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100231
Borislav Petkov81f49382008-07-15 21:21:56 +0200232 if (!pc->error) {
233 floppy->sense_key = buf[2] & 0x0F;
234 floppy->asc = buf[12];
235 floppy->ascq = buf[13];
236 floppy->progress_indication = buf[15] & 0x80 ?
237 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
238
239 if (floppy->failed_pc)
240 debug_log("pc = %x, ", floppy->failed_pc->c[0]);
241
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100242 debug_log("sense key = %x, asc = %x, ascq = %x\n",
Borislav Petkov81f49382008-07-15 21:21:56 +0200243 floppy->sense_key, floppy->asc, floppy->ascq);
244 } else
245 printk(KERN_ERR "Error in REQUEST SENSE itself - "
246 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Borislav Petkov81f49382008-07-15 21:21:56 +0200249 idefloppy_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Borislav Petkov8e555122008-04-18 00:46:27 +0200252static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200254 ide_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100255 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 pc->c[4] = 255;
Borislav Petkov8e555122008-04-18 00:46:27 +0200257 pc->req_xfer = 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100261 * Called when an error was detected during the last packet command. We queue a
262 * request sense packet command in the head of the request list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100264static void idefloppy_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200266 struct ide_floppy_obj *floppy = drive->driver_data;
267 struct request *rq = &floppy->request_sense_rq;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200268 struct ide_atapi_pc *pc = &floppy->request_sense_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100270 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 idefloppy_create_request_sense_cmd(pc);
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200272 ide_queue_pc_head(drive, floppy->disk, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Borislav Petkov0eea6452008-02-02 19:56:36 +0100275/* The usual interrupt handler called during a packet command. */
Paolo Ciarrocchi52d3ccf2008-04-26 17:36:41 +0200276static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 idefloppy_floppy_t *floppy = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200280 return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
281 IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200282 idefloppy_retry_pc, NULL, ide_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100286 * What we have here is a classic case of a top half / bottom half interrupt
287 * service routine. In interrupt mode, the device sends an interrupt to signal
288 * that it is ready to receive a packet. However, we need to delay about 2-3
289 * ticks before issuing the packet or we gets in trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Borislav Petkovcbbc4e82008-07-15 21:22:03 +0200291static int idefloppy_transfer_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 idefloppy_floppy_t *floppy = drive->driver_data;
294
295 /* Send the actual packet */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200296 drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* Timeout for the packet command */
299 return IDEFLOPPY_WAIT_CMD;
300}
301
Borislav Petkovcbbc4e82008-07-15 21:22:03 +0200302
303/*
304 * Called as an interrupt (or directly). When the device says it's ready for a
305 * packet, we schedule the packet transfer to occur about 2-3 ticks later in
306 * transfer_pc.
307 */
308static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz6ffb66412008-07-15 21:21:56 +0200311 struct ide_atapi_pc *pc = floppy->pc;
Bartlomiej Zolnierkiewicz0b2eea42008-07-15 21:21:54 +0200312 ide_expiry_t *expiry;
313 unsigned int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100315 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * The following delay solves a problem with ATAPI Zip 100 drives
317 * where the Busy flag was apparently being deasserted before the
318 * unit was ready to receive data. This was happening on a
319 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
320 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
321 * used until after the packet is moved in about 50 msec.
322 */
Borislav Petkovea68d272008-07-23 19:56:01 +0200323 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
Bartlomiej Zolnierkiewicz0b2eea42008-07-15 21:21:54 +0200324 timeout = floppy->ticks;
Borislav Petkovcbbc4e82008-07-15 21:22:03 +0200325 expiry = &idefloppy_transfer_pc;
Bartlomiej Zolnierkiewicz0b2eea42008-07-15 21:21:54 +0200326 } else {
327 timeout = IDEFLOPPY_WAIT_CMD;
328 expiry = NULL;
329 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100330
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200331 return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Borislav Petkovd652c132008-02-02 19:56:35 +0100334static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200335 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Borislav Petkovd652c132008-02-02 19:56:35 +0100337 /* supress error messages resulting from Medium not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (floppy->sense_key == 0x02 &&
339 floppy->asc == 0x3a &&
340 floppy->ascq == 0x00)
Borislav Petkovd652c132008-02-02 19:56:35 +0100341 return;
342
343 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
344 "asc = %2x, ascq = %2x\n",
345 floppy->drive->name, pc->c[0], floppy->sense_key,
346 floppy->asc, floppy->ascq);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100350static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
Borislav Petkov8e555122008-04-18 00:46:27 +0200351 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 idefloppy_floppy_t *floppy = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100356 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 floppy->failed_pc = pc;
358 /* Set the current packet command */
359 floppy->pc = pc;
360
Borislav Petkov757ced82008-02-02 19:56:37 +0100361 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100362 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
Borislav Petkov757ced82008-02-02 19:56:37 +0100363 ide_floppy_report_error(floppy, pc);
364 /* Giving up */
365 pc->error = IDEFLOPPY_ERROR_GENERAL;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 floppy->failed_pc = NULL;
Borislav Petkov2207fa52008-07-23 19:55:59 +0200368 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return ide_stopped;
370 }
371
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100372 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Borislav Petkovcbbc4e82008-07-15 21:22:03 +0200376 return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200377 IDEFLOPPY_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Borislav Petkov8e555122008-04-18 00:46:27 +0200380static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100382 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200384 ide_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100385 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 pc->c[4] = prevent;
387}
388
Borislav Petkov8e555122008-04-18 00:46:27 +0200389static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200391 ide_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100392 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 pc->c[7] = 255;
394 pc->c[8] = 255;
Borislav Petkov8e555122008-04-18 00:46:27 +0200395 pc->req_xfer = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Borislav Petkov8e555122008-04-18 00:46:27 +0200398static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
399 int l, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200401 ide_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100402 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 pc->c[1] = 0x17;
404
Borislav Petkov8e555122008-04-18 00:46:27 +0200405 memset(pc->buf, 0, 12);
406 pc->buf[1] = 0xA2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
408
409 if (flags & 1) /* Verify bit on... */
Borislav Petkov8e555122008-04-18 00:46:27 +0200410 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
411 pc->buf[3] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Borislav Petkov8e555122008-04-18 00:46:27 +0200413 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
414 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
415 pc->buf_size = 12;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100416 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100419/* A mode sense command is used to "sense" floppy parameters. */
Borislav Petkov8e555122008-04-18 00:46:27 +0200420static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200421 u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Borislav Petkov24a5d702008-02-02 19:56:34 +0100423 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100424
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200425 ide_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100426 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 pc->c[1] = 0;
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200428 pc->c[2] = page_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 switch (page_code) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100431 case IDEFLOPPY_CAPABILITIES_PAGE:
432 length += 12;
433 break;
434 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
435 length += 32;
436 break;
437 default:
438 printk(KERN_ERR "ide-floppy: unsupported page code "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 "in create_mode_sense_cmd\n");
440 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100441 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Borislav Petkov8e555122008-04-18 00:46:27 +0200442 pc->req_xfer = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Borislav Petkov8e555122008-04-18 00:46:27 +0200445static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200447 ide_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100448 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 pc->c[4] = start;
450}
451
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100452static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200453 struct ide_atapi_pc *pc, struct request *rq,
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100454 unsigned long sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 int block = sector / floppy->bs_factor;
457 int blocks = rq->nr_sectors / floppy->bs_factor;
458 int cmd = rq_data_dir(rq);
459
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100460 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 block, blocks);
462
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200463 ide_init_pc(pc);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100464 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
465 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Borislav Petkov8f622432008-02-02 19:56:33 +0100466 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100467
Borislav Petkov20cd93be672008-07-23 19:56:00 +0200468 memcpy(rq->cmd, pc->c, 12);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 pc->rq = rq;
Borislav Petkovb98b3402008-10-10 22:39:35 +0200471 pc->b_count = 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200472 if (rq->cmd_flags & REQ_RW)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100473 pc->flags |= PC_FLAG_WRITING;
Borislav Petkov8e555122008-04-18 00:46:27 +0200474 pc->buf = NULL;
475 pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200476 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100479static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200480 struct ide_atapi_pc *pc, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200482 ide_init_pc(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200484 pc->rq = rq;
Borislav Petkovb98b3402008-10-10 22:39:35 +0200485 pc->b_count = 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200486 if (rq->data_len && rq_data_dir(rq) == WRITE)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100487 pc->flags |= PC_FLAG_WRITING;
Borislav Petkov8e555122008-04-18 00:46:27 +0200488 pc->buf = rq->data;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200489 if (rq->bio)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200490 pc->flags |= PC_FLAG_DMA_OK;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200491 /*
492 * possibly problematic, doesn't look like ide-floppy correctly
493 * handled scattered requests if dma fails...
494 */
Borislav Petkov8e555122008-04-18 00:46:27 +0200495 pc->req_xfer = pc->buf_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100498static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
499 struct request *rq, sector_t block_s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovb98b3402008-10-10 22:39:35 +0200502 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8e555122008-04-18 00:46:27 +0200503 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned long block = (unsigned long)block_s;
505
Borislav Petkovb98b3402008-10-10 22:39:35 +0200506 debug_log("%s: dev: %s, cmd: 0x%x, cmd_type: %x, errors: %d\n",
507 __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
508 rq->cmd[0], rq->cmd_type, rq->errors);
509
510 debug_log("%s: sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
511 __func__, (long)rq->sector, rq->nr_sectors,
512 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (rq->errors >= ERROR_MAX) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100515 if (floppy->failed_pc)
516 ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 else
518 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
519 drive->name);
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200520 idefloppy_end_request(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return ide_stopped;
522 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200523 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (((long)rq->sector % floppy->bs_factor) ||
525 (rq->nr_sectors % floppy->bs_factor)) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100526 printk(KERN_ERR "%s: unsupported r/w request size\n",
527 drive->name);
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200528 idefloppy_end_request(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return ide_stopped;
530 }
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200531 pc = &floppy->queued_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200533 } else if (blk_special_request(rq)) {
Borislav Petkov8e555122008-04-18 00:46:27 +0200534 pc = (struct ide_atapi_pc *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200535 } else if (blk_pc_request(rq)) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200536 pc = &floppy->queued_pc;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200537 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 } else {
539 blk_dump_rq_flags(rq,
540 "ide-floppy: unsupported command in queue");
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200541 idefloppy_end_request(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return ide_stopped;
543 }
544
Borislav Petkovb98b3402008-10-10 22:39:35 +0200545 ide_init_sg_cmd(drive, rq);
546 ide_map_sg(drive, rq);
547
548 pc->sg = hwif->sg_table;
549 pc->sg_cnt = hwif->sg_nents;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 pc->rq = rq;
Bartlomiej Zolnierkiewicz5d418932008-07-15 21:21:57 +0200552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return idefloppy_issue_pc(drive, pc);
554}
555
556/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100557 * Add a special packet command request to the tail of the request queue,
558 * and wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Borislav Petkov8e555122008-04-18 00:46:27 +0200560static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct ide_floppy_obj *floppy = drive->driver_data;
FUJITA Tomonori6fe16232008-07-15 21:21:43 +0200563 struct request *rq;
564 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
FUJITA Tomonori6fe16232008-07-15 21:21:43 +0200566 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
567 rq->buffer = (char *) pc;
568 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov20cd93be672008-07-23 19:56:00 +0200569 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori6fe16232008-07-15 21:21:43 +0200570 error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
571 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
FUJITA Tomonori6fe16232008-07-15 21:21:43 +0200573 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100577 * Look at the flexible disk page parameters. We ignore the CHS capacity
578 * parameters and use the LBA parameters instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100580static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200583 struct ide_atapi_pc pc;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100584 u8 *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int capacity, lba_capacity;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100586 u16 transfer_rate, sector_size, cyls, rpm;
587 u8 heads, sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200589 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100590
591 if (idefloppy_queue_pc_tail(drive, &pc)) {
592 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
593 " parameters\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 1;
595 }
Borislav Petkov8e555122008-04-18 00:46:27 +0200596 floppy->wp = !!(pc.buf[3] & 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 set_disk_ro(floppy->disk, floppy->wp);
Borislav Petkov8e555122008-04-18 00:46:27 +0200598 page = &pc.buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Harvey Harrison85ae98a32008-07-16 20:33:47 +0200600 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
601 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
602 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
603 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
Borislav Petkov8e555122008-04-18 00:46:27 +0200604 heads = pc.buf[8 + 4];
605 sectors = pc.buf[8 + 5];
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100606
607 capacity = cyls * heads * sectors * sector_size;
608
609 if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
611 "%d sector size, %d rpm\n",
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100612 drive->name, capacity / 1024, cyls, heads,
613 sectors, transfer_rate / 8, sector_size, rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100615 memcpy(&floppy->flexible_disk_page, page, 32);
616 drive->bios_cyl = cyls;
617 drive->bios_head = heads;
618 drive->bios_sect = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 lba_capacity = floppy->blocks * floppy->block_size;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (capacity < lba_capacity) {
622 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
623 "bytes, but the drive only handles %d\n",
624 drive->name, lba_capacity, capacity);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100625 floppy->blocks = floppy->block_size ?
626 capacity / floppy->block_size : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 return 0;
629}
630
Borislav Petkov948391d2008-02-02 19:56:34 +0100631static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200634 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 floppy->srfp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200638 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100639 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200640
Borislav Petkov948391d2008-02-02 19:56:34 +0100641 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Borislav Petkov8e555122008-04-18 00:46:27 +0200644 floppy->srfp = pc.buf[8 + 2] & 0x40;
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100649 * Determine if a media is present in the floppy drive, and if so, its LBA
650 * capacity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 */
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100652static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200655 struct ide_atapi_pc pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100656 u8 *cap_desc;
657 u8 header_len, desc_cnt;
658 int i, rc = 1, blocks, length;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 drive->bios_cyl = 0;
661 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +0100662 floppy->blocks = 0;
663 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 set_capacity(floppy->disk, 0);
665
666 idefloppy_create_read_capacity_cmd(&pc);
667 if (idefloppy_queue_pc_tail(drive, &pc)) {
668 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
669 return 1;
670 }
Borislav Petkov8e555122008-04-18 00:46:27 +0200671 header_len = pc.buf[3];
672 cap_desc = &pc.buf[4];
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100673 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100675 for (i = 0; i < desc_cnt; i++) {
676 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Harvey Harrison85ae98a32008-07-16 20:33:47 +0200678 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
679 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100680
681 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
682 i, blocks * length / 1024, blocks, length);
683
684 if (i)
685 continue;
686 /*
687 * the code below is valid only for the 1st descriptor, ie i=0
688 */
689
Borislav Petkov8e555122008-04-18 00:46:27 +0200690 switch (pc.buf[desc_start + 4] & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* Clik! drive returns this instead of CAPACITY_CURRENT */
692 case CAPACITY_UNFORMATTED:
Borislav Petkovea68d272008-07-23 19:56:01 +0200693 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100694 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * If it is not a clik drive, break out
696 * (maintains previous driver behaviour)
697 */
698 break;
699 case CAPACITY_CURRENT:
700 /* Normal Zip/LS-120 disks */
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100701 if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
703 "sector size\n", drive->name,
704 blocks * length / 1024, blocks, length);
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100705 memcpy(&floppy->cap_desc, cap_desc, 8);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!length || length % 512) {
708 printk(KERN_NOTICE "%s: %d bytes block size "
709 "not supported\n", drive->name, length);
710 } else {
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100711 floppy->blocks = blocks;
712 floppy->block_size = length;
713 floppy->bs_factor = length / 512;
714 if (floppy->bs_factor != 1)
715 printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 "512 bytes block size not "
717 "fully supported\n",
718 drive->name);
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100719 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 break;
722 case CAPACITY_NO_CARTRIDGE:
723 /*
724 * This is a KERN_ERR so it appears on screen
725 * for the user to see
726 */
727 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
728 break;
729 case CAPACITY_INVALID:
730 printk(KERN_ERR "%s: Invalid capacity for disk "
731 "in drive\n", drive->name);
732 break;
733 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100734 debug_log("Descriptor 0 Code: %d\n",
Borislav Petkov8e555122008-04-18 00:46:27 +0200735 pc.buf[desc_start + 4] & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 /* Clik! disk does not support get_flexible_disk_page */
Borislav Petkovea68d272008-07-23 19:56:01 +0200739 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100740 (void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
743 return rc;
744}
745
746/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100747 * Obtain the list of formattable capacities.
748 * Very similar to ide_floppy_get_capacity, except that we push the capacity
749 * descriptors to userland, instead of our own structures.
750 *
751 * Userland gives us the following structure:
752 *
753 * struct idefloppy_format_capacities {
754 * int nformats;
755 * struct {
756 * int nblocks;
757 * int blocksize;
758 * } formats[];
759 * };
760 *
761 * userland initializes nformats to the number of allocated formats[] records.
762 * On exit we set nformats to the number of records we've actually initialized.
763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100765static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Borislav Petkov8e555122008-04-18 00:46:27 +0200767 struct ide_atapi_pc pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100768 u8 header_len, desc_cnt;
769 int i, blocks, length, u_array_size, u_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int __user *argp;
771
772 if (get_user(u_array_size, arg))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200773 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if (u_array_size <= 0)
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200776 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 idefloppy_create_read_capacity_cmd(&pc);
779 if (idefloppy_queue_pc_tail(drive, &pc)) {
780 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200781 return -EIO;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100782 }
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200783
Borislav Petkov8e555122008-04-18 00:46:27 +0200784 header_len = pc.buf[3];
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100785 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 u_index = 0;
788 argp = arg + 1;
789
790 /*
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100791 * We always skip the first capacity descriptor. That's the current
792 * capacity. We are interested in the remaining descriptors, the
793 * formattable capacities.
794 */
795 for (i = 1; i < desc_cnt; i++) {
796 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (u_index >= u_array_size)
799 break; /* User-supplied buffer too small */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Harvey Harrison85ae98a32008-07-16 20:33:47 +0200801 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
802 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (put_user(blocks, argp))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200805 return -EFAULT;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ++argp;
808
809 if (put_user(length, argp))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200810 return -EFAULT;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 ++argp;
813
814 ++u_index;
815 }
816
817 if (put_user(u_index, arg))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200818 return -EFAULT;
819
820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100824 * Get ATAPI_FORMAT_UNIT progress indication.
825 *
826 * Userland gives a pointer to an int. The int is set to a progress
827 * indicator 0-65536, with 65536=100%.
828 *
829 * If the drive does not support format progress indication, we just check
830 * the dsc bit, and return either 0 or 65536.
831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +0200833static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200836 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int progress_indication = 0x10000;
838
839 if (floppy->srfp) {
840 idefloppy_create_request_sense_cmd(&pc);
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100841 if (idefloppy_queue_pc_tail(drive, &pc))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200842 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 if (floppy->sense_key == 2 &&
845 floppy->asc == 4 &&
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100846 floppy->ascq == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 progress_indication = floppy->progress_indication;
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100848
849 /* Else assume format_unit has finished, and we're at 0x10000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 } else {
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200851 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100853 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 local_irq_save(flags);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200856 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 local_irq_restore(flags);
858
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200859 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200862 if (put_user(progress_indication, arg))
863 return -EFAULT;
864
865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100868static sector_t idefloppy_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 idefloppy_floppy_t *floppy = drive->driver_data;
871 unsigned long capacity = floppy->blocks * floppy->bs_factor;
872
873 return capacity;
874}
875
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200876#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200877ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
878ide_devset_rw(bios_head, 0, 255, bios_head);
879ide_devset_rw(bios_sect, 0, 63, bios_sect);
880
881static int get_ticks(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200884 return floppy->ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200886
887static int set_ticks(ide_drive_t *drive, int arg)
888{
889 idefloppy_floppy_t *floppy = drive->driver_data;
890 floppy->ticks = arg;
891 return 0;
892}
893
894IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
895
896static const struct ide_devset *idefloppy_settings[] = {
897 &ide_devset_bios_cyl,
898 &ide_devset_bios_head,
899 &ide_devset_bios_sect,
900 &ide_devset_ticks,
901 NULL
902};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200903#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100905static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200907 u16 *id = drive->id;
Borislav Petkov3ad67762008-04-18 00:46:25 +0200908 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200910 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200911
Borislav Petkov2207fa52008-07-23 19:55:59 +0200912 drive->pc_callback = ide_floppy_callback;
Borislav Petkov3ad67762008-04-18 00:46:25 +0200913
914 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovea68d272008-07-23 19:56:01 +0200915 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100917 * We used to check revisions here. At this point however I'm giving up.
918 * Just assume they are all broken, its easier.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 *
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100920 * The actual reason for the workarounds was likely a driver bug after
921 * all rather than a firmware bug, and the workaround below used to hide
922 * it. It should be fixed as of version 1.9, but to be on the safe side
923 * we'll leave the limitation below for the 2.2.x tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200925 if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
Borislav Petkovea68d272008-07-23 19:56:01 +0200926 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* This value will be visible in the /proc/ide/hdx/settings */
928 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
929 blk_queue_max_sectors(drive->queue, 64);
930 }
931
932 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100933 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
934 * nasty clicking noises without it, so please don't remove this.
935 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200936 if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 blk_queue_max_sectors(drive->queue, 64);
Borislav Petkovea68d272008-07-23 19:56:01 +0200938 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100941 (void) ide_floppy_get_capacity(drive);
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +0200942
943 ide_proc_register_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Russell King4031bbe2006-01-06 11:41:00 +0000946static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 idefloppy_floppy_t *floppy = drive->driver_data;
949 struct gendisk *g = floppy->disk;
950
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200951 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 del_gendisk(g);
954
955 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Borislav Petkov9a24b632008-02-02 19:56:33 +0100958static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
961 ide_drive_t *drive = floppy->drive;
962 struct gendisk *g = floppy->disk;
963
964 drive->driver_data = NULL;
965 g->private_data = NULL;
966 put_disk(g);
967 kfree(floppy);
968}
969
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +0200970#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100971static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
972 int count, int *eof, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 ide_drive_t*drive = (ide_drive_t *)data;
975 int len;
976
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100977 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
978 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981static ide_proc_entry_t idefloppy_proc[] = {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100982 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
983 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 { NULL, 0, NULL, NULL }
985};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +0200986#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Russell King4031bbe2006-01-06 11:41:00 +0000988static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200991 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +0100992 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200993 .name = "ide-floppy",
994 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200995 },
Russell King4031bbe2006-01-06 11:41:00 +0000996 .probe = ide_floppy_probe,
997 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 .version = IDEFLOPPY_VERSION,
999 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 .do_request = idefloppy_do_request,
Borislav Petkovc2b2b292008-04-18 00:46:27 +02001001 .end_request = idefloppy_end_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001003#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001005 .settings = idefloppy_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001006#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007};
1008
Bartlomiej Zolnierkiewicze996fc82008-10-10 22:39:37 +02001009static void ide_floppy_set_media_lock(ide_drive_t *drive, int on)
1010{
1011 struct ide_atapi_pc pc;
1012
1013 /* IOMEGA Clik! drives do not support lock/unlock commands */
1014 if ((drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE) == 0) {
1015 idefloppy_create_prevent_cmd(&pc, on);
1016 (void)idefloppy_queue_pc_tail(drive, &pc);
1017 }
1018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020static int idefloppy_open(struct inode *inode, struct file *filp)
1021{
1022 struct gendisk *disk = inode->i_bdev->bd_disk;
1023 struct ide_floppy_obj *floppy;
1024 ide_drive_t *drive;
Borislav Petkov8e555122008-04-18 00:46:27 +02001025 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int ret = 0;
1027
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001028 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001030 floppy = ide_floppy_get(disk);
1031 if (!floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return -ENXIO;
1033
1034 drive = floppy->drive;
1035
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001036 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001038 if (floppy->openers == 1) {
Borislav Petkovea68d272008-07-23 19:56:01 +02001039 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* Just in case */
1041
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +02001042 ide_init_pc(&pc);
Borislav Petkovc96a7df2008-07-16 20:33:46 +02001043 pc.c[0] = GPCMD_TEST_UNIT_READY;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (idefloppy_queue_pc_tail(drive, &pc)) {
1046 idefloppy_create_start_stop_cmd(&pc, 1);
1047 (void) idefloppy_queue_pc_tail(drive, &pc);
1048 }
1049
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001050 if (ide_floppy_get_capacity(drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 && (filp->f_flags & O_NDELAY) == 0
1052 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001053 * Allow O_NDELAY to open a drive without a disk, or with an
1054 * unreadable disk, so that we can get the format capacity
1055 * of the drive or begin the format - Sam
1056 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ret = -EIO;
1059 goto out_put_floppy;
1060 }
1061
1062 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ret = -EROFS;
1064 goto out_put_floppy;
1065 }
Bartlomiej Zolnierkiewicze996fc82008-10-10 22:39:37 +02001066
Borislav Petkovea68d272008-07-23 19:56:01 +02001067 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
Bartlomiej Zolnierkiewicze996fc82008-10-10 22:39:37 +02001068 ide_floppy_set_media_lock(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 check_disk_change(inode->i_bdev);
Borislav Petkovea68d272008-07-23 19:56:01 +02001070 } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 ret = -EBUSY;
1072 goto out_put_floppy;
1073 }
1074 return 0;
1075
1076out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001077 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ide_floppy_put(floppy);
1079 return ret;
1080}
1081
1082static int idefloppy_release(struct inode *inode, struct file *filp)
1083{
1084 struct gendisk *disk = inode->i_bdev->bd_disk;
1085 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1086 ide_drive_t *drive = floppy->drive;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001087
1088 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001090 if (floppy->openers == 1) {
Bartlomiej Zolnierkiewicze996fc82008-10-10 22:39:37 +02001091 ide_floppy_set_media_lock(drive, 0);
Borislav Petkovea68d272008-07-23 19:56:01 +02001092 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001094
1095 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 ide_floppy_put(floppy);
1098
1099 return 0;
1100}
1101
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001102static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1103{
1104 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1105 ide_drive_t *drive = floppy->drive;
1106
1107 geo->heads = drive->bios_head;
1108 geo->sectors = drive->bios_sect;
1109 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1110 return 0;
1111}
1112
Borislav Petkovea68d272008-07-23 19:56:01 +02001113static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1114 unsigned long arg, unsigned int cmd)
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001115{
Borislav Petkovea68d272008-07-23 19:56:01 +02001116 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicze996fc82008-10-10 22:39:37 +02001117 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
Borislav Petkovea68d272008-07-23 19:56:01 +02001118
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001119 if (floppy->openers > 1)
1120 return -EBUSY;
1121
Bartlomiej Zolnierkiewicze996fc82008-10-10 22:39:37 +02001122 ide_floppy_set_media_lock(drive, prevent);
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001123
1124 if (cmd == CDROMEJECT) {
1125 idefloppy_create_start_stop_cmd(pc, 2);
1126 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1127 }
1128
1129 return 0;
1130}
1131
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001132static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001133{
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001134 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +02001135 struct ide_atapi_pc pc;
Borislav Petkovea68d272008-07-23 19:56:01 +02001136 int blocks, length, flags, err = 0;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001137
1138 if (floppy->openers > 1) {
1139 /* Don't format if someone is using the disk */
Borislav Petkovea68d272008-07-23 19:56:01 +02001140 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001141 return -EBUSY;
1142 }
1143
Borislav Petkovea68d272008-07-23 19:56:01 +02001144 drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001145
1146 /*
1147 * Send ATAPI_FORMAT_UNIT to the drive.
1148 *
1149 * Userland gives us the following structure:
1150 *
1151 * struct idefloppy_format_command {
1152 * int nblocks;
1153 * int blocksize;
1154 * int flags;
1155 * } ;
1156 *
1157 * flags is a bitmask, currently, the only defined flag is:
1158 *
1159 * 0x01 - verify media after format.
1160 */
1161 if (get_user(blocks, arg) ||
1162 get_user(length, arg+1) ||
1163 get_user(flags, arg+2)) {
1164 err = -EFAULT;
1165 goto out;
1166 }
1167
Borislav Petkovea68d272008-07-23 19:56:01 +02001168 (void) idefloppy_get_sfrp_bit(drive);
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001169 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1170
Borislav Petkovea68d272008-07-23 19:56:01 +02001171 if (idefloppy_queue_pc_tail(drive, &pc))
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001172 err = -EIO;
1173
1174out:
1175 if (err)
Borislav Petkovea68d272008-07-23 19:56:01 +02001176 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001177 return err;
1178}
1179
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001180static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
1181 unsigned int cmd, void __user *argp)
1182{
1183 switch (cmd) {
1184 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1185 return 0;
1186 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1187 return ide_floppy_get_format_capacities(drive, argp);
1188 case IDEFLOPPY_IOCTL_FORMAT_START:
1189 if (!(file->f_mode & 2))
1190 return -EPERM;
1191 return ide_floppy_format_unit(drive, (int __user *)argp);
1192 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1193 return ide_floppy_get_format_progress(drive, argp);
1194 default:
1195 return -ENOTTY;
1196 }
1197}
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199static int idefloppy_ioctl(struct inode *inode, struct file *file,
1200 unsigned int cmd, unsigned long arg)
1201{
1202 struct block_device *bdev = inode->i_bdev;
1203 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1204 ide_drive_t *drive = floppy->drive;
Borislav Petkov8e555122008-04-18 00:46:27 +02001205 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001207 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001209 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
Borislav Petkovea68d272008-07-23 19:56:01 +02001210 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001212 err = ide_floppy_format_ioctl(drive, file, cmd, argp);
1213 if (err != -ENOTTY)
1214 return err;
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001215
1216 /*
1217 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1218 * and CDROM_SEND_PACKET (legacy) ioctls
1219 */
1220 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1221 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1222 bdev->bd_disk, cmd, argp);
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001223
1224 if (err == -ENOTTY)
1225 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1226
1227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230static int idefloppy_media_changed(struct gendisk *disk)
1231{
1232 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1233 ide_drive_t *drive = floppy->drive;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001234 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /* do not scan partitions twice if this is a removable device */
1237 if (drive->attach) {
1238 drive->attach = 0;
1239 return 0;
1240 }
Borislav Petkovea68d272008-07-23 19:56:01 +02001241 ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1242 drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246static int idefloppy_revalidate_disk(struct gendisk *disk)
1247{
1248 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1249 set_capacity(disk, idefloppy_capacity(floppy->drive));
1250 return 0;
1251}
1252
1253static struct block_device_operations idefloppy_ops = {
Paolo Ciarrocchi52d3ccf2008-04-26 17:36:41 +02001254 .owner = THIS_MODULE,
1255 .open = idefloppy_open,
1256 .release = idefloppy_release,
1257 .ioctl = idefloppy_ioctl,
1258 .getgeo = idefloppy_getgeo,
1259 .media_changed = idefloppy_media_changed,
1260 .revalidate_disk = idefloppy_revalidate_disk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261};
1262
Russell King4031bbe2006-01-06 11:41:00 +00001263static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 idefloppy_floppy_t *floppy;
1266 struct gendisk *g;
1267
1268 if (!strstr("ide-floppy", drive->driver_req))
1269 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (drive->media != ide_floppy)
1272 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001273
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02001274 if (!ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001275 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1276 " of ide-floppy\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 goto failed;
1278 }
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001279 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1280 if (!floppy) {
1281 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1282 " structure\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto failed;
1284 }
1285
1286 g = alloc_disk(1 << PARTN_BITS);
1287 if (!g)
1288 goto out_free_floppy;
1289
1290 ide_init_disk(g, drive);
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 kref_init(&floppy->kref);
1293
1294 floppy->drive = drive;
1295 floppy->driver = &idefloppy_driver;
1296 floppy->disk = g;
1297
1298 g->private_data = &floppy->driver;
1299
1300 drive->driver_data = floppy;
1301
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001302 idefloppy_setup(drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 g->minors = 1 << PARTN_BITS;
1305 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1307 g->fops = &idefloppy_ops;
1308 drive->attach = 1;
1309 add_disk(g);
1310 return 0;
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312out_free_floppy:
1313 kfree(floppy);
1314failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001315 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001318static void __exit idefloppy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001320 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001323static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001326 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Kay Sievers263756e2005-12-12 18:03:44 +01001329MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330module_init(idefloppy_init);
1331module_exit(idefloppy_exit);
1332MODULE_LICENSE("GPL");
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001333MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1334