blob: 38dca45ffd111f6341017a299b4aedab663f610e [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
72/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +010073 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
74 * bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
76#define IDEFLOPPY_PC_BUFFER_SIZE 256
77
Borislav Petkov194ec0c2008-02-02 19:56:35 +010078/* format capacities descriptor codes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define CAPACITY_INVALID 0x00
80#define CAPACITY_UNFORMATTED 0x01
81#define CAPACITY_CURRENT 0x02
82#define CAPACITY_NO_CARTRIDGE 0x03
83
84/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +010085 * Most of our global data which we need to save even as we leave the driver
86 * due to an interrupt or a timer event is stored in a variable of type
87 * idefloppy_floppy_t, defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89typedef struct ide_floppy_obj {
90 ide_drive_t *drive;
91 ide_driver_t *driver;
92 struct gendisk *disk;
93 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +010094 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /* Current packet command */
Borislav Petkov8e555122008-04-18 00:46:27 +020097 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* Last failed packet command */
Borislav Petkov8e555122008-04-18 00:46:27 +020099 struct ide_atapi_pc *failed_pc;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200100 /* used for blk_{fs,pc}_request() requests */
101 struct ide_atapi_pc queued_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200102
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200103 struct ide_atapi_pc request_sense_pc;
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200104 struct request request_sense_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100106 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 u8 sense_key, asc, ascq;
108 /* delay this long before sending packet command */
109 u8 ticks;
110 int progress_indication;
111
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100112 /* Device information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* Current format */
114 int blocks, block_size, bs_factor;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100115 /* Last format capacity descriptor */
116 u8 cap_desc[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* Copy of the flexible disk page */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100118 u8 flexible_disk_page[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /* Write protect */
120 int wp;
121 /* Supports format progress report */
122 int srfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123} idefloppy_floppy_t;
124
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200125#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100127/* IOCTLs used in low-level formatting. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
129#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
130#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
131#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
132
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100133/* Error code returned in rq->errors to the higher part of the driver. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define IDEFLOPPY_ERROR_GENERAL 101
135
136/*
Borislav Petkov948391d2008-02-02 19:56:34 +0100137 * Pages of the SELECT SENSE / MODE SENSE packet commands.
138 * See SFF-8070i spec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
140#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
141#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
142
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800143static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
146
147#define ide_floppy_g(disk) \
148 container_of((disk)->private_data, struct ide_floppy_obj, driver)
149
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200150static void idefloppy_cleanup_obj(struct kref *);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
153{
154 struct ide_floppy_obj *floppy = NULL;
155
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800156 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 floppy = ide_floppy_g(disk);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200158 if (floppy) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200159 if (ide_device_get(floppy->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200160 floppy = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200161 else
162 kref_get(&floppy->kref);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200163 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800164 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return floppy;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static void ide_floppy_put(struct ide_floppy_obj *floppy)
169{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200170 ide_drive_t *drive = floppy->drive;
171
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800172 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100173 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +0200174 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800175 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100179 * Used to finish servicing a request. For read/write requests, we will call
180 * ide_end_request to pass to the next buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200182static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 idefloppy_floppy_t *floppy = drive->driver_data;
185 struct request *rq = HWGROUP(drive)->rq;
186 int error;
187
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100188 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 switch (uptodate) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100191 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
192 case 1: error = 0; break;
193 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 if (error)
196 floppy->failed_pc = NULL;
197 /* Why does this happen? */
198 if (!rq)
199 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200200 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* our real local end request function */
202 ide_end_request(drive, uptodate, nsecs);
203 return 0;
204 }
205 rq->errors = error;
206 /* fixme: need to move this local also */
207 ide_end_drive_cmd(drive, 0, 0);
208 return 0;
209}
210
Borislav Petkov8e555122008-04-18 00:46:27 +0200211static void idefloppy_update_buffers(ide_drive_t *drive,
212 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct request *rq = pc->rq;
215 struct bio *bio = rq->bio;
216
217 while ((bio = rq->bio) != NULL)
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200218 idefloppy_end_request(drive, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100222 * Generate a new packet command request in front of the request queue, before
223 * the current request so that it will be processed immediately, on the next
224 * pass through the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
Borislav Petkov8e555122008-04-18 00:46:27 +0200226static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100227 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct ide_floppy_obj *floppy = drive->driver_data;
230
FUJITA Tomonori124cafc2008-07-15 21:21:44 +0200231 blk_rq_init(NULL, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200233 rq->cmd_type = REQ_TYPE_SPECIAL;
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200234 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 rq->rq_disk = floppy->disk;
Borislav Petkov20cd93be672008-07-23 19:56:00 +0200236 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200237 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Borislav Petkov81f49382008-07-15 21:21:56 +0200240static void ide_floppy_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov81f49382008-07-15 21:21:56 +0200243 struct ide_atapi_pc *pc = floppy->pc;
244 int uptodate = pc->error ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100246 debug_log("Reached %s\n", __func__);
247
Bartlomiej Zolnierkiewiczdd2e9a02008-07-15 21:22:01 +0200248 if (floppy->failed_pc == pc)
249 floppy->failed_pc = NULL;
250
Borislav Petkov81f49382008-07-15 21:21:56 +0200251 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
252 (pc->rq && blk_pc_request(pc->rq)))
253 uptodate = 1; /* FIXME */
254 else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
255 u8 *buf = floppy->pc->buf;
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100256
Borislav Petkov81f49382008-07-15 21:21:56 +0200257 if (!pc->error) {
258 floppy->sense_key = buf[2] & 0x0F;
259 floppy->asc = buf[12];
260 floppy->ascq = buf[13];
261 floppy->progress_indication = buf[15] & 0x80 ?
262 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
263
264 if (floppy->failed_pc)
265 debug_log("pc = %x, ", floppy->failed_pc->c[0]);
266
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100267 debug_log("sense key = %x, asc = %x, ascq = %x\n",
Borislav Petkov81f49382008-07-15 21:21:56 +0200268 floppy->sense_key, floppy->asc, floppy->ascq);
269 } else
270 printk(KERN_ERR "Error in REQUEST SENSE itself - "
271 "Aborting request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Borislav Petkov81f49382008-07-15 21:21:56 +0200274 idefloppy_end_request(drive, uptodate, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Borislav Petkov8e555122008-04-18 00:46:27 +0200277static void idefloppy_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Borislav Petkov68dc3572008-07-16 20:33:47 +0200279 memset(pc, 0, sizeof(*pc));
Borislav Petkov8e555122008-04-18 00:46:27 +0200280 pc->buf = pc->pc_buf;
281 pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Borislav Petkov8e555122008-04-18 00:46:27 +0200284static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Borislav Petkov30d67092008-02-02 19:56:33 +0100286 idefloppy_init_pc(pc);
287 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 pc->c[4] = 255;
Borislav Petkov8e555122008-04-18 00:46:27 +0200289 pc->req_xfer = 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100293 * Called when an error was detected during the last packet command. We queue a
294 * request sense packet command in the head of the request list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100296static void idefloppy_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Bartlomiej Zolnierkiewicz394a4c22008-10-10 22:39:35 +0200298 struct ide_floppy_obj *floppy = drive->driver_data;
299 struct request *rq = &floppy->request_sense_rq;
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200300 struct ide_atapi_pc *pc = &floppy->request_sense_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100302 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 idefloppy_create_request_sense_cmd(pc);
304 idefloppy_queue_pc_head(drive, pc, rq);
305}
306
Borislav Petkov0eea6452008-02-02 19:56:36 +0100307/* The usual interrupt handler called during a packet command. */
Paolo Ciarrocchi52d3ccf2008-04-26 17:36:41 +0200308static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 idefloppy_floppy_t *floppy = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200312 return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
313 IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200314 idefloppy_retry_pc, NULL, ide_io_buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100318 * What we have here is a classic case of a top half / bottom half interrupt
319 * service routine. In interrupt mode, the device sends an interrupt to signal
320 * that it is ready to receive a packet. However, we need to delay about 2-3
321 * ticks before issuing the packet or we gets in trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Borislav Petkovcbbc4e82008-07-15 21:22:03 +0200323static int idefloppy_transfer_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 idefloppy_floppy_t *floppy = drive->driver_data;
326
327 /* Send the actual packet */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200328 drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* Timeout for the packet command */
331 return IDEFLOPPY_WAIT_CMD;
332}
333
Borislav Petkovcbbc4e82008-07-15 21:22:03 +0200334
335/*
336 * Called as an interrupt (or directly). When the device says it's ready for a
337 * packet, we schedule the packet transfer to occur about 2-3 ticks later in
338 * transfer_pc.
339 */
340static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz6ffb66412008-07-15 21:21:56 +0200343 struct ide_atapi_pc *pc = floppy->pc;
Bartlomiej Zolnierkiewicz0b2eea42008-07-15 21:21:54 +0200344 ide_expiry_t *expiry;
345 unsigned int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100347 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * The following delay solves a problem with ATAPI Zip 100 drives
349 * where the Busy flag was apparently being deasserted before the
350 * unit was ready to receive data. This was happening on a
351 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
352 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
353 * used until after the packet is moved in about 50 msec.
354 */
Borislav Petkovea68d272008-07-23 19:56:01 +0200355 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
Bartlomiej Zolnierkiewicz0b2eea42008-07-15 21:21:54 +0200356 timeout = floppy->ticks;
Borislav Petkovcbbc4e82008-07-15 21:22:03 +0200357 expiry = &idefloppy_transfer_pc;
Bartlomiej Zolnierkiewicz0b2eea42008-07-15 21:21:54 +0200358 } else {
359 timeout = IDEFLOPPY_WAIT_CMD;
360 expiry = NULL;
361 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100362
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200363 return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Borislav Petkovd652c132008-02-02 19:56:35 +0100366static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200367 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Borislav Petkovd652c132008-02-02 19:56:35 +0100369 /* supress error messages resulting from Medium not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (floppy->sense_key == 0x02 &&
371 floppy->asc == 0x3a &&
372 floppy->ascq == 0x00)
Borislav Petkovd652c132008-02-02 19:56:35 +0100373 return;
374
375 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
376 "asc = %2x, ascq = %2x\n",
377 floppy->drive->name, pc->c[0], floppy->sense_key,
378 floppy->asc, floppy->ascq);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100382static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
Borislav Petkov8e555122008-04-18 00:46:27 +0200383 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 idefloppy_floppy_t *floppy = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100388 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 floppy->failed_pc = pc;
390 /* Set the current packet command */
391 floppy->pc = pc;
392
Borislav Petkov757ced82008-02-02 19:56:37 +0100393 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100394 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
Borislav Petkov757ced82008-02-02 19:56:37 +0100395 ide_floppy_report_error(floppy, pc);
396 /* Giving up */
397 pc->error = IDEFLOPPY_ERROR_GENERAL;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 floppy->failed_pc = NULL;
Borislav Petkov2207fa52008-07-23 19:55:59 +0200400 drive->pc_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return ide_stopped;
402 }
403
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100404 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 pc->retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Borislav Petkovcbbc4e82008-07-15 21:22:03 +0200408 return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200409 IDEFLOPPY_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Borislav Petkov8e555122008-04-18 00:46:27 +0200412static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100414 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100417 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 pc->c[4] = prevent;
419}
420
Borislav Petkov8e555122008-04-18 00:46:27 +0200421static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100424 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 pc->c[7] = 255;
426 pc->c[8] = 255;
Borislav Petkov8e555122008-04-18 00:46:27 +0200427 pc->req_xfer = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Borislav Petkov8e555122008-04-18 00:46:27 +0200430static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
431 int l, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100434 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 pc->c[1] = 0x17;
436
Borislav Petkov8e555122008-04-18 00:46:27 +0200437 memset(pc->buf, 0, 12);
438 pc->buf[1] = 0xA2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
440
441 if (flags & 1) /* Verify bit on... */
Borislav Petkov8e555122008-04-18 00:46:27 +0200442 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
443 pc->buf[3] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Borislav Petkov8e555122008-04-18 00:46:27 +0200445 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
446 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
447 pc->buf_size = 12;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100448 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100451/* A mode sense command is used to "sense" floppy parameters. */
Borislav Petkov8e555122008-04-18 00:46:27 +0200452static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200453 u8 page_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Borislav Petkov24a5d702008-02-02 19:56:34 +0100455 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100458 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 pc->c[1] = 0;
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200460 pc->c[2] = page_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 switch (page_code) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100463 case IDEFLOPPY_CAPABILITIES_PAGE:
464 length += 12;
465 break;
466 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
467 length += 32;
468 break;
469 default:
470 printk(KERN_ERR "ide-floppy: unsupported page code "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 "in create_mode_sense_cmd\n");
472 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100473 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Borislav Petkov8e555122008-04-18 00:46:27 +0200474 pc->req_xfer = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Borislav Petkov8e555122008-04-18 00:46:27 +0200477static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100480 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 pc->c[4] = start;
482}
483
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100484static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200485 struct ide_atapi_pc *pc, struct request *rq,
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100486 unsigned long sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 int block = sector / floppy->bs_factor;
489 int blocks = rq->nr_sectors / floppy->bs_factor;
490 int cmd = rq_data_dir(rq);
491
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100492 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 block, blocks);
494
495 idefloppy_init_pc(pc);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100496 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
497 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Borislav Petkov8f622432008-02-02 19:56:33 +0100498 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100499
Borislav Petkov20cd93be672008-07-23 19:56:00 +0200500 memcpy(rq->cmd, pc->c, 12);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 pc->rq = rq;
Borislav Petkovb98b3402008-10-10 22:39:35 +0200503 pc->b_count = 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200504 if (rq->cmd_flags & REQ_RW)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100505 pc->flags |= PC_FLAG_WRITING;
Borislav Petkov8e555122008-04-18 00:46:27 +0200506 pc->buf = NULL;
507 pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200508 pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100511static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200512 struct ide_atapi_pc *pc, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 idefloppy_init_pc(pc);
515 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200516 pc->rq = rq;
Borislav Petkovb98b3402008-10-10 22:39:35 +0200517 pc->b_count = 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200518 if (rq->data_len && rq_data_dir(rq) == WRITE)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100519 pc->flags |= PC_FLAG_WRITING;
Borislav Petkov8e555122008-04-18 00:46:27 +0200520 pc->buf = rq->data;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200521 if (rq->bio)
Bartlomiej Zolnierkiewicz5e331092008-07-15 21:21:56 +0200522 pc->flags |= PC_FLAG_DMA_OK;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200523 /*
524 * possibly problematic, doesn't look like ide-floppy correctly
525 * handled scattered requests if dma fails...
526 */
Borislav Petkov8e555122008-04-18 00:46:27 +0200527 pc->req_xfer = pc->buf_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100530static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
531 struct request *rq, sector_t block_s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovb98b3402008-10-10 22:39:35 +0200534 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8e555122008-04-18 00:46:27 +0200535 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 unsigned long block = (unsigned long)block_s;
537
Borislav Petkovb98b3402008-10-10 22:39:35 +0200538 debug_log("%s: dev: %s, cmd: 0x%x, cmd_type: %x, errors: %d\n",
539 __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
540 rq->cmd[0], rq->cmd_type, rq->errors);
541
542 debug_log("%s: sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
543 __func__, (long)rq->sector, rq->nr_sectors,
544 rq->current_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if (rq->errors >= ERROR_MAX) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100547 if (floppy->failed_pc)
548 ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 else
550 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
551 drive->name);
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200552 idefloppy_end_request(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return ide_stopped;
554 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200555 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (((long)rq->sector % floppy->bs_factor) ||
557 (rq->nr_sectors % floppy->bs_factor)) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100558 printk(KERN_ERR "%s: unsupported r/w request size\n",
559 drive->name);
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200560 idefloppy_end_request(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return ide_stopped;
562 }
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200563 pc = &floppy->queued_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200565 } else if (blk_special_request(rq)) {
Borislav Petkov8e555122008-04-18 00:46:27 +0200566 pc = (struct ide_atapi_pc *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200567 } else if (blk_pc_request(rq)) {
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200568 pc = &floppy->queued_pc;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200569 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 } else {
571 blk_dump_rq_flags(rq,
572 "ide-floppy: unsupported command in queue");
Borislav Petkovc2b2b292008-04-18 00:46:27 +0200573 idefloppy_end_request(drive, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return ide_stopped;
575 }
576
Borislav Petkovb98b3402008-10-10 22:39:35 +0200577 ide_init_sg_cmd(drive, rq);
578 ide_map_sg(drive, rq);
579
580 pc->sg = hwif->sg_table;
581 pc->sg_cnt = hwif->sg_nents;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 pc->rq = rq;
Bartlomiej Zolnierkiewicz5d418932008-07-15 21:21:57 +0200584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return idefloppy_issue_pc(drive, pc);
586}
587
588/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100589 * Add a special packet command request to the tail of the request queue,
590 * and wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 */
Borislav Petkov8e555122008-04-18 00:46:27 +0200592static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct ide_floppy_obj *floppy = drive->driver_data;
FUJITA Tomonori6fe16232008-07-15 21:21:43 +0200595 struct request *rq;
596 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
FUJITA Tomonori6fe16232008-07-15 21:21:43 +0200598 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
599 rq->buffer = (char *) pc;
600 rq->cmd_type = REQ_TYPE_SPECIAL;
Borislav Petkov20cd93be672008-07-23 19:56:00 +0200601 memcpy(rq->cmd, pc->c, 12);
FUJITA Tomonori6fe16232008-07-15 21:21:43 +0200602 error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
603 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
FUJITA Tomonori6fe16232008-07-15 21:21:43 +0200605 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/*
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100609 * Look at the flexible disk page parameters. We ignore the CHS capacity
610 * parameters and use the LBA parameters instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100612static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200615 struct ide_atapi_pc pc;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100616 u8 *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int capacity, lba_capacity;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100618 u16 transfer_rate, sector_size, cyls, rpm;
619 u8 heads, sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200621 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100622
623 if (idefloppy_queue_pc_tail(drive, &pc)) {
624 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
625 " parameters\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 1;
627 }
Borislav Petkov8e555122008-04-18 00:46:27 +0200628 floppy->wp = !!(pc.buf[3] & 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 set_disk_ro(floppy->disk, floppy->wp);
Borislav Petkov8e555122008-04-18 00:46:27 +0200630 page = &pc.buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Harvey Harrison85ae98a32008-07-16 20:33:47 +0200632 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
633 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
634 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
635 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
Borislav Petkov8e555122008-04-18 00:46:27 +0200636 heads = pc.buf[8 + 4];
637 sectors = pc.buf[8 + 5];
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100638
639 capacity = cyls * heads * sectors * sector_size;
640
641 if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
643 "%d sector size, %d rpm\n",
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100644 drive->name, capacity / 1024, cyls, heads,
645 sectors, transfer_rate / 8, sector_size, rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100647 memcpy(&floppy->flexible_disk_page, page, 32);
648 drive->bios_cyl = cyls;
649 drive->bios_head = heads;
650 drive->bios_sect = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 lba_capacity = floppy->blocks * floppy->block_size;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (capacity < lba_capacity) {
654 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
655 "bytes, but the drive only handles %d\n",
656 drive->name, lba_capacity, capacity);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100657 floppy->blocks = floppy->block_size ?
658 capacity / floppy->block_size : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 return 0;
661}
662
Borislav Petkov948391d2008-02-02 19:56:34 +0100663static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200666 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 floppy->srfp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200670 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100671 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
Bartlomiej Zolnierkiewicz4de4b9e2008-10-10 22:39:36 +0200672
Borislav Petkov948391d2008-02-02 19:56:34 +0100673 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Borislav Petkov8e555122008-04-18 00:46:27 +0200676 floppy->srfp = pc.buf[8 + 2] & 0x40;
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100681 * Determine if a media is present in the floppy drive, and if so, its LBA
682 * capacity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100684static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200687 struct ide_atapi_pc pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100688 u8 *cap_desc;
689 u8 header_len, desc_cnt;
690 int i, rc = 1, blocks, length;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 drive->bios_cyl = 0;
693 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +0100694 floppy->blocks = 0;
695 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 set_capacity(floppy->disk, 0);
697
698 idefloppy_create_read_capacity_cmd(&pc);
699 if (idefloppy_queue_pc_tail(drive, &pc)) {
700 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
701 return 1;
702 }
Borislav Petkov8e555122008-04-18 00:46:27 +0200703 header_len = pc.buf[3];
704 cap_desc = &pc.buf[4];
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100705 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100707 for (i = 0; i < desc_cnt; i++) {
708 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Harvey Harrison85ae98a32008-07-16 20:33:47 +0200710 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
711 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100712
713 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
714 i, blocks * length / 1024, blocks, length);
715
716 if (i)
717 continue;
718 /*
719 * the code below is valid only for the 1st descriptor, ie i=0
720 */
721
Borislav Petkov8e555122008-04-18 00:46:27 +0200722 switch (pc.buf[desc_start + 4] & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Clik! drive returns this instead of CAPACITY_CURRENT */
724 case CAPACITY_UNFORMATTED:
Borislav Petkovea68d272008-07-23 19:56:01 +0200725 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100726 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * If it is not a clik drive, break out
728 * (maintains previous driver behaviour)
729 */
730 break;
731 case CAPACITY_CURRENT:
732 /* Normal Zip/LS-120 disks */
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100733 if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
735 "sector size\n", drive->name,
736 blocks * length / 1024, blocks, length);
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100737 memcpy(&floppy->cap_desc, cap_desc, 8);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!length || length % 512) {
740 printk(KERN_NOTICE "%s: %d bytes block size "
741 "not supported\n", drive->name, length);
742 } else {
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100743 floppy->blocks = blocks;
744 floppy->block_size = length;
745 floppy->bs_factor = length / 512;
746 if (floppy->bs_factor != 1)
747 printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 "512 bytes block size not "
749 "fully supported\n",
750 drive->name);
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100751 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 break;
754 case CAPACITY_NO_CARTRIDGE:
755 /*
756 * This is a KERN_ERR so it appears on screen
757 * for the user to see
758 */
759 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
760 break;
761 case CAPACITY_INVALID:
762 printk(KERN_ERR "%s: Invalid capacity for disk "
763 "in drive\n", drive->name);
764 break;
765 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100766 debug_log("Descriptor 0 Code: %d\n",
Borislav Petkov8e555122008-04-18 00:46:27 +0200767 pc.buf[desc_start + 4] & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
770 /* Clik! disk does not support get_flexible_disk_page */
Borislav Petkovea68d272008-07-23 19:56:01 +0200771 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100772 (void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
775 return rc;
776}
777
778/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100779 * Obtain the list of formattable capacities.
780 * Very similar to ide_floppy_get_capacity, except that we push the capacity
781 * descriptors to userland, instead of our own structures.
782 *
783 * Userland gives us the following structure:
784 *
785 * struct idefloppy_format_capacities {
786 * int nformats;
787 * struct {
788 * int nblocks;
789 * int blocksize;
790 * } formats[];
791 * };
792 *
793 * userland initializes nformats to the number of allocated formats[] records.
794 * On exit we set nformats to the number of records we've actually initialized.
795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100797static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Borislav Petkov8e555122008-04-18 00:46:27 +0200799 struct ide_atapi_pc pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100800 u8 header_len, desc_cnt;
801 int i, blocks, length, u_array_size, u_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int __user *argp;
803
804 if (get_user(u_array_size, arg))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200805 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (u_array_size <= 0)
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200808 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 idefloppy_create_read_capacity_cmd(&pc);
811 if (idefloppy_queue_pc_tail(drive, &pc)) {
812 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200813 return -EIO;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100814 }
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200815
Borislav Petkov8e555122008-04-18 00:46:27 +0200816 header_len = pc.buf[3];
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100817 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 u_index = 0;
820 argp = arg + 1;
821
822 /*
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100823 * We always skip the first capacity descriptor. That's the current
824 * capacity. We are interested in the remaining descriptors, the
825 * formattable capacities.
826 */
827 for (i = 1; i < desc_cnt; i++) {
828 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (u_index >= u_array_size)
831 break; /* User-supplied buffer too small */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Harvey Harrison85ae98a32008-07-16 20:33:47 +0200833 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
834 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (put_user(blocks, argp))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200837 return -EFAULT;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ++argp;
840
841 if (put_user(length, argp))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200842 return -EFAULT;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ++argp;
845
846 ++u_index;
847 }
848
849 if (put_user(u_index, arg))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200850 return -EFAULT;
851
852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100856 * Get ATAPI_FORMAT_UNIT progress indication.
857 *
858 * Userland gives a pointer to an int. The int is set to a progress
859 * indicator 0-65536, with 65536=100%.
860 *
861 * If the drive does not support format progress indication, we just check
862 * the dsc bit, and return either 0 or 65536.
863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +0200865static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200868 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int progress_indication = 0x10000;
870
871 if (floppy->srfp) {
872 idefloppy_create_request_sense_cmd(&pc);
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100873 if (idefloppy_queue_pc_tail(drive, &pc))
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200874 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 if (floppy->sense_key == 2 &&
877 floppy->asc == 4 &&
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100878 floppy->ascq == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 progress_indication = floppy->progress_indication;
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100880
881 /* Else assume format_unit has finished, and we're at 0x10000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 } else {
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200883 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100885 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 local_irq_save(flags);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200888 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 local_irq_restore(flags);
890
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200891 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Bartlomiej Zolnierkiewicze3faa242008-10-10 22:39:34 +0200894 if (put_user(progress_indication, arg))
895 return -EFAULT;
896
897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100900static sector_t idefloppy_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 idefloppy_floppy_t *floppy = drive->driver_data;
903 unsigned long capacity = floppy->blocks * floppy->bs_factor;
904
905 return capacity;
906}
907
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200908#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200909ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
910ide_devset_rw(bios_head, 0, 255, bios_head);
911ide_devset_rw(bios_sect, 0, 63, bios_sect);
912
913static int get_ticks(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200916 return floppy->ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200918
919static int set_ticks(ide_drive_t *drive, int arg)
920{
921 idefloppy_floppy_t *floppy = drive->driver_data;
922 floppy->ticks = arg;
923 return 0;
924}
925
926IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
927
928static const struct ide_devset *idefloppy_settings[] = {
929 &ide_devset_bios_cyl,
930 &ide_devset_bios_head,
931 &ide_devset_bios_sect,
932 &ide_devset_ticks,
933 NULL
934};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100937static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200939 u16 *id = drive->id;
Borislav Petkov3ad67762008-04-18 00:46:25 +0200940 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200942 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
Bartlomiej Zolnierkiewicz2e8a6f82008-10-10 22:39:36 +0200943
Borislav Petkov2207fa52008-07-23 19:55:59 +0200944 drive->pc_callback = ide_floppy_callback;
Borislav Petkov3ad67762008-04-18 00:46:25 +0200945
946 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkovea68d272008-07-23 19:56:01 +0200947 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100949 * We used to check revisions here. At this point however I'm giving up.
950 * Just assume they are all broken, its easier.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100952 * The actual reason for the workarounds was likely a driver bug after
953 * all rather than a firmware bug, and the workaround below used to hide
954 * it. It should be fixed as of version 1.9, but to be on the safe side
955 * we'll leave the limitation below for the 2.2.x tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200957 if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
Borislav Petkovea68d272008-07-23 19:56:01 +0200958 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /* This value will be visible in the /proc/ide/hdx/settings */
960 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
961 blk_queue_max_sectors(drive->queue, 64);
962 }
963
964 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100965 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
966 * nasty clicking noises without it, so please don't remove this.
967 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200968 if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 blk_queue_max_sectors(drive->queue, 64);
Borislav Petkovea68d272008-07-23 19:56:01 +0200970 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100973 (void) ide_floppy_get_capacity(drive);
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +0200974
975 ide_proc_register_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Russell King4031bbe2006-01-06 11:41:00 +0000978static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 idefloppy_floppy_t *floppy = drive->driver_data;
981 struct gendisk *g = floppy->disk;
982
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200983 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 del_gendisk(g);
986
987 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Borislav Petkov9a24b632008-02-02 19:56:33 +0100990static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
993 ide_drive_t *drive = floppy->drive;
994 struct gendisk *g = floppy->disk;
995
996 drive->driver_data = NULL;
997 g->private_data = NULL;
998 put_disk(g);
999 kfree(floppy);
1000}
1001
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001002#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001003static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1004 int count, int *eof, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 ide_drive_t*drive = (ide_drive_t *)data;
1007 int len;
1008
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001009 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1010 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013static ide_proc_entry_t idefloppy_proc[] = {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001014 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1015 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 { NULL, 0, NULL, NULL }
1017};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001018#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Russell King4031bbe2006-01-06 11:41:00 +00001020static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001023 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001024 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001025 .name = "ide-floppy",
1026 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001027 },
Russell King4031bbe2006-01-06 11:41:00 +00001028 .probe = ide_floppy_probe,
1029 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 .version = IDEFLOPPY_VERSION,
1031 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 .do_request = idefloppy_do_request,
Borislav Petkovc2b2b292008-04-18 00:46:27 +02001033 .end_request = idefloppy_end_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001035#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001037 .settings = idefloppy_settings,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001038#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039};
1040
1041static int idefloppy_open(struct inode *inode, struct file *filp)
1042{
1043 struct gendisk *disk = inode->i_bdev->bd_disk;
1044 struct ide_floppy_obj *floppy;
1045 ide_drive_t *drive;
Borislav Petkov8e555122008-04-18 00:46:27 +02001046 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int ret = 0;
1048
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001049 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001051 floppy = ide_floppy_get(disk);
1052 if (!floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return -ENXIO;
1054
1055 drive = floppy->drive;
1056
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001057 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001059 if (floppy->openers == 1) {
Borislav Petkovea68d272008-07-23 19:56:01 +02001060 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* Just in case */
1062
Borislav Petkovc96a7df2008-07-16 20:33:46 +02001063 idefloppy_init_pc(&pc);
1064 pc.c[0] = GPCMD_TEST_UNIT_READY;
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (idefloppy_queue_pc_tail(drive, &pc)) {
1067 idefloppy_create_start_stop_cmd(&pc, 1);
1068 (void) idefloppy_queue_pc_tail(drive, &pc);
1069 }
1070
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001071 if (ide_floppy_get_capacity(drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 && (filp->f_flags & O_NDELAY) == 0
1073 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001074 * Allow O_NDELAY to open a drive without a disk, or with an
1075 * unreadable disk, so that we can get the format capacity
1076 * of the drive or begin the format - Sam
1077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ret = -EIO;
1080 goto out_put_floppy;
1081 }
1082
1083 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ret = -EROFS;
1085 goto out_put_floppy;
1086 }
Borislav Petkovea68d272008-07-23 19:56:01 +02001087 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkovea68d272008-07-23 19:56:01 +02001089 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 idefloppy_create_prevent_cmd(&pc, 1);
1091 (void) idefloppy_queue_pc_tail(drive, &pc);
1092 }
1093 check_disk_change(inode->i_bdev);
Borislav Petkovea68d272008-07-23 19:56:01 +02001094 } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 ret = -EBUSY;
1096 goto out_put_floppy;
1097 }
1098 return 0;
1099
1100out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001101 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ide_floppy_put(floppy);
1103 return ret;
1104}
1105
1106static int idefloppy_release(struct inode *inode, struct file *filp)
1107{
1108 struct gendisk *disk = inode->i_bdev->bd_disk;
1109 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1110 ide_drive_t *drive = floppy->drive;
Borislav Petkov8e555122008-04-18 00:46:27 +02001111 struct ide_atapi_pc pc;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001112
1113 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001115 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkovea68d272008-07-23 19:56:01 +02001117 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 idefloppy_create_prevent_cmd(&pc, 0);
1119 (void) idefloppy_queue_pc_tail(drive, &pc);
1120 }
1121
Borislav Petkovea68d272008-07-23 19:56:01 +02001122 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001124
1125 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 ide_floppy_put(floppy);
1128
1129 return 0;
1130}
1131
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001132static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1133{
1134 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1135 ide_drive_t *drive = floppy->drive;
1136
1137 geo->heads = drive->bios_head;
1138 geo->sectors = drive->bios_sect;
1139 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1140 return 0;
1141}
1142
Borislav Petkovea68d272008-07-23 19:56:01 +02001143static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1144 unsigned long arg, unsigned int cmd)
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001145{
Borislav Petkovea68d272008-07-23 19:56:01 +02001146 idefloppy_floppy_t *floppy = drive->driver_data;
1147
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001148 if (floppy->openers > 1)
1149 return -EBUSY;
1150
1151 /* The IOMEGA Clik! Drive doesn't support this command -
1152 * no room for an eject mechanism */
Borislav Petkovea68d272008-07-23 19:56:01 +02001153 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001154 int prevent = arg ? 1 : 0;
1155
1156 if (cmd == CDROMEJECT)
1157 prevent = 0;
1158
1159 idefloppy_create_prevent_cmd(pc, prevent);
1160 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1161 }
1162
1163 if (cmd == CDROMEJECT) {
1164 idefloppy_create_start_stop_cmd(pc, 2);
1165 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1166 }
1167
1168 return 0;
1169}
1170
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001171static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001172{
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001173 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +02001174 struct ide_atapi_pc pc;
Borislav Petkovea68d272008-07-23 19:56:01 +02001175 int blocks, length, flags, err = 0;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001176
1177 if (floppy->openers > 1) {
1178 /* Don't format if someone is using the disk */
Borislav Petkovea68d272008-07-23 19:56:01 +02001179 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001180 return -EBUSY;
1181 }
1182
Borislav Petkovea68d272008-07-23 19:56:01 +02001183 drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001184
1185 /*
1186 * Send ATAPI_FORMAT_UNIT to the drive.
1187 *
1188 * Userland gives us the following structure:
1189 *
1190 * struct idefloppy_format_command {
1191 * int nblocks;
1192 * int blocksize;
1193 * int flags;
1194 * } ;
1195 *
1196 * flags is a bitmask, currently, the only defined flag is:
1197 *
1198 * 0x01 - verify media after format.
1199 */
1200 if (get_user(blocks, arg) ||
1201 get_user(length, arg+1) ||
1202 get_user(flags, arg+2)) {
1203 err = -EFAULT;
1204 goto out;
1205 }
1206
Borislav Petkovea68d272008-07-23 19:56:01 +02001207 (void) idefloppy_get_sfrp_bit(drive);
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001208 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1209
Borislav Petkovea68d272008-07-23 19:56:01 +02001210 if (idefloppy_queue_pc_tail(drive, &pc))
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001211 err = -EIO;
1212
1213out:
1214 if (err)
Borislav Petkovea68d272008-07-23 19:56:01 +02001215 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001216 return err;
1217}
1218
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001219static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
1220 unsigned int cmd, void __user *argp)
1221{
1222 switch (cmd) {
1223 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1224 return 0;
1225 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1226 return ide_floppy_get_format_capacities(drive, argp);
1227 case IDEFLOPPY_IOCTL_FORMAT_START:
1228 if (!(file->f_mode & 2))
1229 return -EPERM;
1230 return ide_floppy_format_unit(drive, (int __user *)argp);
1231 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1232 return ide_floppy_get_format_progress(drive, argp);
1233 default:
1234 return -ENOTTY;
1235 }
1236}
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static int idefloppy_ioctl(struct inode *inode, struct file *file,
1239 unsigned int cmd, unsigned long arg)
1240{
1241 struct block_device *bdev = inode->i_bdev;
1242 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1243 ide_drive_t *drive = floppy->drive;
Borislav Petkov8e555122008-04-18 00:46:27 +02001244 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001246 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001248 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
Borislav Petkovea68d272008-07-23 19:56:01 +02001249 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Bartlomiej Zolnierkiewiczd56c99e2008-10-10 22:39:34 +02001251 err = ide_floppy_format_ioctl(drive, file, cmd, argp);
1252 if (err != -ENOTTY)
1253 return err;
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001254
1255 /*
1256 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1257 * and CDROM_SEND_PACKET (legacy) ioctls
1258 */
1259 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1260 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1261 bdev->bd_disk, cmd, argp);
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001262
1263 if (err == -ENOTTY)
1264 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1265
1266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269static int idefloppy_media_changed(struct gendisk *disk)
1270{
1271 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1272 ide_drive_t *drive = floppy->drive;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001273 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 /* do not scan partitions twice if this is a removable device */
1276 if (drive->attach) {
1277 drive->attach = 0;
1278 return 0;
1279 }
Borislav Petkovea68d272008-07-23 19:56:01 +02001280 ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1281 drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001282 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
1285static int idefloppy_revalidate_disk(struct gendisk *disk)
1286{
1287 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1288 set_capacity(disk, idefloppy_capacity(floppy->drive));
1289 return 0;
1290}
1291
1292static struct block_device_operations idefloppy_ops = {
Paolo Ciarrocchi52d3ccf2008-04-26 17:36:41 +02001293 .owner = THIS_MODULE,
1294 .open = idefloppy_open,
1295 .release = idefloppy_release,
1296 .ioctl = idefloppy_ioctl,
1297 .getgeo = idefloppy_getgeo,
1298 .media_changed = idefloppy_media_changed,
1299 .revalidate_disk = idefloppy_revalidate_disk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300};
1301
Russell King4031bbe2006-01-06 11:41:00 +00001302static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 idefloppy_floppy_t *floppy;
1305 struct gendisk *g;
1306
1307 if (!strstr("ide-floppy", drive->driver_req))
1308 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (drive->media != ide_floppy)
1311 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001312
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +02001313 if (!ide_check_atapi_device(drive, DRV_NAME)) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001314 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1315 " of ide-floppy\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto failed;
1317 }
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001318 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1319 if (!floppy) {
1320 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1321 " structure\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto failed;
1323 }
1324
1325 g = alloc_disk(1 << PARTN_BITS);
1326 if (!g)
1327 goto out_free_floppy;
1328
1329 ide_init_disk(g, drive);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 kref_init(&floppy->kref);
1332
1333 floppy->drive = drive;
1334 floppy->driver = &idefloppy_driver;
1335 floppy->disk = g;
1336
1337 g->private_data = &floppy->driver;
1338
1339 drive->driver_data = floppy;
1340
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001341 idefloppy_setup(drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 g->minors = 1 << PARTN_BITS;
1344 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1346 g->fops = &idefloppy_ops;
1347 drive->attach = 1;
1348 add_disk(g);
1349 return 0;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351out_free_floppy:
1352 kfree(floppy);
1353failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001354 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001357static void __exit idefloppy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001359 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001362static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001365 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Kay Sievers263756e2005-12-12 18:03:44 +01001368MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369module_init(idefloppy_init);
1370module_exit(idefloppy_exit);
1371MODULE_LICENSE("GPL");
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001372MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1373