blob: 7e62dfc186f68829ae5e2b54086173c184889b85 [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
Borislav Petkovfc6c5bc2008-02-02 19:56:38 +010018#define IDEFLOPPY_VERSION "1.00"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/timer.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
28#include <linux/major.h>
29#include <linux/errno.h>
30#include <linux/genhd.h>
31#include <linux/slab.h>
32#include <linux/cdrom.h>
33#include <linux/ide.h>
34#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +020037#include <scsi/scsi_ioctl.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/byteorder.h>
Borislav Petkov7e8b1632008-02-02 19:56:34 +010040#include <linux/irq.h>
41#include <linux/uaccess.h>
42#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44
Borislav Petkovf373bd82008-02-02 19:56:36 +010045/* define to see debug info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define IDEFLOPPY_DEBUG_LOG 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
Borislav Petkov0571c7a2008-02-02 19:56:38 +010049#define IDEFLOPPY_DEBUG(fmt, args...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#if IDEFLOPPY_DEBUG_LOG
Borislav Petkovbcc77d92008-02-02 19:56:34 +010052#define debug_log(fmt, args...) \
53 printk(KERN_INFO "ide-floppy: " fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#else
Borislav Petkov0571c7a2008-02-02 19:56:38 +010055#define debug_log(fmt, args...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
58
Borislav Petkov0571c7a2008-02-02 19:56:38 +010059/* Some drives require a longer irq timeout. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
61
62/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +010063 * After each failed packet command we issue a request sense command and retry
64 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66#define IDEFLOPPY_MAX_PC_RETRIES 3
67
68/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +010069 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
70 * bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72#define IDEFLOPPY_PC_BUFFER_SIZE 256
73
74/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +010075 * In various places in the driver, we need to allocate storage for packet
76 * commands and requests, which will remain valid while we leave the driver to
77 * wait for an interrupt or a timeout event.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
79#define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
80
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +010081/* Packet command flag bits. */
82enum {
83 /* 1 when we prefer to use DMA if possible */
84 PC_FLAG_DMA_RECOMMENDED = (1 << 0),
85 /* 1 while DMA in progress */
86 PC_FLAG_DMA_IN_PROGRESS = (1 << 1),
87 /* 1 when encountered problem during DMA */
88 PC_FLAG_DMA_ERROR = (1 << 2),
89 /* Data direction */
90 PC_FLAG_WRITING = (1 << 3),
91 /* Suppress error reporting */
92 PC_FLAG_SUPPRESS_ERROR = (1 << 4),
93};
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Borislav Petkov194ec0c2008-02-02 19:56:35 +010095/* format capacities descriptor codes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define CAPACITY_INVALID 0x00
97#define CAPACITY_UNFORMATTED 0x01
98#define CAPACITY_CURRENT 0x02
99#define CAPACITY_NO_CARTRIDGE 0x03
100
101/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100102 * Most of our global data which we need to save even as we leave the driver
103 * due to an interrupt or a timer event is stored in a variable of type
104 * idefloppy_floppy_t, defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
106typedef struct ide_floppy_obj {
107 ide_drive_t *drive;
108 ide_driver_t *driver;
109 struct gendisk *disk;
110 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100111 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /* Current packet command */
Borislav Petkov8e555122008-04-18 00:46:27 +0200114 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /* Last failed packet command */
Borislav Petkov8e555122008-04-18 00:46:27 +0200116 struct ide_atapi_pc *failed_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* Packet command stack */
Borislav Petkov8e555122008-04-18 00:46:27 +0200118 struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /* Next free packet command storage space */
120 int pc_stack_index;
121 struct request rq_stack[IDEFLOPPY_PC_STACK];
122 /* We implement a circular array */
123 int rq_stack_index;
124
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100125 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 u8 sense_key, asc, ascq;
127 /* delay this long before sending packet command */
128 u8 ticks;
129 int progress_indication;
130
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100131 /* Device information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 /* Current format */
133 int blocks, block_size, bs_factor;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100134 /* Last format capacity descriptor */
135 u8 cap_desc[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* Copy of the flexible disk page */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100137 u8 flexible_disk_page[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* Write protect */
139 int wp;
140 /* Supports format progress report */
141 int srfp;
142 /* Status/Action flags */
143 unsigned long flags;
144} idefloppy_floppy_t;
145
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200146#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100148/* Floppy flag bits values. */
149enum {
150 /* DRQ interrupt device */
151 IDEFLOPPY_FLAG_DRQ_INTERRUPT = (1 << 0),
152 /* Media may have changed */
153 IDEFLOPPY_FLAG_MEDIA_CHANGED = (1 << 1),
154 /* Format in progress */
155 IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS = (1 << 2),
156 /* Avoid commands not supported in Clik drive */
157 IDEFLOPPY_FLAG_CLIK_DRIVE = (1 << 3),
158 /* Requires BH algorithm for packets */
159 IDEFLOPPY_FLAG_ZIP_DRIVE = (1 << 4),
160};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100162/* Defines for the MODE SENSE command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define MODE_SENSE_CURRENT 0x00
164#define MODE_SENSE_CHANGEABLE 0x01
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100165#define MODE_SENSE_DEFAULT 0x02
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define MODE_SENSE_SAVED 0x03
167
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100168/* IOCTLs used in low-level formatting. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
170#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
171#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
172#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
173
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100174/* Error code returned in rq->errors to the higher part of the driver. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define IDEFLOPPY_ERROR_GENERAL 101
176
177/*
Borislav Petkov948391d2008-02-02 19:56:34 +0100178 * Pages of the SELECT SENSE / MODE SENSE packet commands.
179 * See SFF-8070i spec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
182#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
183
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800184static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
187
188#define ide_floppy_g(disk) \
189 container_of((disk)->private_data, struct ide_floppy_obj, driver)
190
191static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
192{
193 struct ide_floppy_obj *floppy = NULL;
194
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800195 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 floppy = ide_floppy_g(disk);
197 if (floppy)
198 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800199 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return floppy;
201}
202
Borislav Petkov9a24b632008-02-02 19:56:33 +0100203static void idefloppy_cleanup_obj(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205static void ide_floppy_put(struct ide_floppy_obj *floppy)
206{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800207 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100208 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800209 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100213 * Used to finish servicing a request. For read/write requests, we will call
214 * ide_end_request to pass to the next buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
216static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
217{
218 idefloppy_floppy_t *floppy = drive->driver_data;
219 struct request *rq = HWGROUP(drive)->rq;
220 int error;
221
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100222 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 switch (uptodate) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100225 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
226 case 1: error = 0; break;
227 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 if (error)
230 floppy->failed_pc = NULL;
231 /* Why does this happen? */
232 if (!rq)
233 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200234 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* our real local end request function */
236 ide_end_request(drive, uptodate, nsecs);
237 return 0;
238 }
239 rq->errors = error;
240 /* fixme: need to move this local also */
241 ide_end_drive_cmd(drive, 0, 0);
242 return 0;
243}
244
Borislav Petkov8e555122008-04-18 00:46:27 +0200245static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov32925e12008-02-02 19:56:36 +0100246 unsigned int bcount, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200249 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct bio_vec *bvec;
251 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200252 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 char *data;
254
NeilBrown5705f702007-09-25 12:35:59 +0200255 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200256 if (!bcount)
257 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Jens Axboe6c92e692007-08-16 13:43:12 +0200259 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jens Axboe6c92e692007-08-16 13:43:12 +0200261 data = bvec_kmap_irq(bvec, &flags);
Borislav Petkov32925e12008-02-02 19:56:36 +0100262 if (direction)
263 drive->hwif->atapi_output_bytes(drive, data, count);
264 else
265 drive->hwif->atapi_input_bytes(drive, data, count);
Jens Axboe6c92e692007-08-16 13:43:12 +0200266 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jens Axboe6c92e692007-08-16 13:43:12 +0200268 bcount -= count;
269 pc->b_count += count;
270 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 idefloppy_do_end_request(drive, 1, done >> 9);
274
275 if (bcount) {
Borislav Petkov32925e12008-02-02 19:56:36 +0100276 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
277 drive->name, __func__, bcount);
278 if (direction)
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200279 ide_atapi_write_zeros(drive, bcount);
Borislav Petkov32925e12008-02-02 19:56:36 +0100280 else
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200281 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283}
284
Borislav Petkov8e555122008-04-18 00:46:27 +0200285static void idefloppy_update_buffers(ide_drive_t *drive,
286 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct request *rq = pc->rq;
289 struct bio *bio = rq->bio;
290
291 while ((bio = rq->bio) != NULL)
292 idefloppy_do_end_request(drive, 1, 0);
293}
294
295/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100296 * Generate a new packet command request in front of the request queue, before
297 * the current request so that it will be processed immediately, on the next
298 * pass through the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
Borislav Petkov8e555122008-04-18 00:46:27 +0200300static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100301 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct ide_floppy_obj *floppy = drive->driver_data;
304
305 ide_init_drive_cmd(rq);
306 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200307 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 rq->rq_disk = floppy->disk;
309 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
310}
311
Borislav Petkov8e555122008-04-18 00:46:27 +0200312static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 idefloppy_floppy_t *floppy = drive->driver_data;
315
316 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100317 floppy->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return (&floppy->pc_stack[floppy->pc_stack_index++]);
319}
320
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100321static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 idefloppy_floppy_t *floppy = drive->driver_data;
324
325 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
326 floppy->rq_stack_index = 0;
327 return (&floppy->rq_stack[floppy->rq_stack_index++]);
328}
329
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100330static void idefloppy_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200333 u8 *buf = floppy->pc->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100335 debug_log("Reached %s\n", __func__);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!floppy->pc->error) {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100338 floppy->sense_key = buf[2] & 0x0F;
339 floppy->asc = buf[12];
340 floppy->ascq = buf[13];
341 floppy->progress_indication = buf[15] & 0x80 ?
342 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
343
344 if (floppy->failed_pc)
345 debug_log("pc = %x, sense key = %x, asc = %x,"
346 " ascq = %x\n",
347 floppy->failed_pc->c[0],
348 floppy->sense_key,
349 floppy->asc,
350 floppy->ascq);
351 else
352 debug_log("sense key = %x, asc = %x, ascq = %x\n",
353 floppy->sense_key,
354 floppy->asc,
355 floppy->ascq);
356
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 idefloppy_do_end_request(drive, 1, 0);
359 } else {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100360 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
361 " request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 idefloppy_do_end_request(drive, 0, 0);
363 }
364}
365
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100366/* General packet command callback function. */
367static void idefloppy_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100370
371 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
374}
375
Borislav Petkov8e555122008-04-18 00:46:27 +0200376static void idefloppy_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 memset(pc->c, 0, 12);
379 pc->retries = 0;
380 pc->flags = 0;
Borislav Petkov8e555122008-04-18 00:46:27 +0200381 pc->req_xfer = 0;
382 pc->buf = pc->pc_buf;
383 pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
384 pc->idefloppy_callback = &idefloppy_pc_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Borislav Petkov8e555122008-04-18 00:46:27 +0200387static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Borislav Petkov30d67092008-02-02 19:56:33 +0100389 idefloppy_init_pc(pc);
390 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 pc->c[4] = 255;
Borislav Petkov8e555122008-04-18 00:46:27 +0200392 pc->req_xfer = 18;
393 pc->idefloppy_callback = &idefloppy_request_sense_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100397 * Called when an error was detected during the last packet command. We queue a
398 * request sense packet command in the head of the request list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100400static void idefloppy_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Borislav Petkov8e555122008-04-18 00:46:27 +0200402 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100405 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 pc = idefloppy_next_pc_storage(drive);
407 rq = idefloppy_next_rq_storage(drive);
408 idefloppy_create_request_sense_cmd(pc);
409 idefloppy_queue_pc_head(drive, pc, rq);
410}
411
Borislav Petkov0eea6452008-02-02 19:56:36 +0100412/* The usual interrupt handler called during a packet command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
414{
415 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100416 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8e555122008-04-18 00:46:27 +0200417 struct ide_atapi_pc *pc = floppy->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct request *rq = pc->rq;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100419 xfer_func_t *xferfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 unsigned int temp;
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100421 int dma_error = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100422 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100423 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100425 debug_log("Reached %s interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100427 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100428 dma_error = hwif->ide_dma_end(drive);
429 if (dma_error) {
430 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
431 rq_data_dir(rq) ? "write" : "read");
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100432 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 } else {
Borislav Petkov8e555122008-04-18 00:46:27 +0200434 pc->xferred = pc->req_xfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 idefloppy_update_buffers(drive, pc);
436 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100437 debug_log("DMA finished\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100441 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100443 /* No more interrupts */
444 if ((stat & DRQ_STAT) == 0) {
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100445 debug_log("Packet command completed, %d bytes transferred\n",
Borislav Petkov8e555122008-04-18 00:46:27 +0200446 pc->xferred);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100447 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Ingo Molnar366c7f52006-07-03 00:25:25 -0700449 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100451 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* Error detected */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100453 debug_log("%s: I/O error\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 rq->errors++;
Borislav Petkov30d67092008-02-02 19:56:33 +0100455 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printk(KERN_ERR "ide-floppy: I/O error in "
457 "request sense command\n");
458 return ide_do_reset(drive);
459 }
460 /* Retry operation */
461 idefloppy_retry_pc(drive);
462 /* queued, but not started */
463 return ide_stopped;
464 }
465 pc->error = 0;
466 if (floppy->failed_pc == pc)
467 floppy->failed_pc = NULL;
468 /* Command finished - Call the callback function */
Borislav Petkov8e555122008-04-18 00:46:27 +0200469 pc->idefloppy_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return ide_stopped;
471 }
472
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100473 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
474 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
476 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100477 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return ide_do_reset(drive);
479 }
480
481 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200482 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
483 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /* on this interrupt */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200485 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100487 if (ireason & CD) {
Borislav Petkov0eea6452008-02-02 19:56:36 +0100488 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return ide_do_reset(drive);
490 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100491 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Hopefully, we will never get here */
493 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100494 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 printk(KERN_ERR "but the floppy wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100496 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return ide_do_reset(drive);
498 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100499 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Reading - Check that we have enough space */
Borislav Petkov8e555122008-04-18 00:46:27 +0200501 temp = pc->xferred + bcount;
502 if (temp > pc->req_xfer) {
503 if (temp > pc->buf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 printk(KERN_ERR "ide-floppy: The floppy wants "
505 "to send us more data than expected "
506 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200507 ide_atapi_discard_data(drive, bcount);
Borislav Petkov0078df22008-02-02 19:56:33 +0100508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 ide_set_handler(drive,
510 &idefloppy_pc_intr,
511 IDEFLOPPY_WAIT_CMD,
512 NULL);
513 return ide_started;
514 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100515 debug_log("The floppy wants to send us more data than"
516 " expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100519 if (pc->flags & PC_FLAG_WRITING)
Borislav Petkov0eea6452008-02-02 19:56:36 +0100520 xferfunc = hwif->atapi_output_bytes;
Borislav Petkov32925e12008-02-02 19:56:36 +0100521 else
Borislav Petkov0eea6452008-02-02 19:56:36 +0100522 xferfunc = hwif->atapi_input_bytes;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100523
Borislav Petkov8e555122008-04-18 00:46:27 +0200524 if (pc->buf)
525 xferfunc(drive, pc->cur_pos, bcount);
Borislav Petkov0eea6452008-02-02 19:56:36 +0100526 else
Borislav Petkov32925e12008-02-02 19:56:36 +0100527 ide_floppy_io_buffers(drive, pc, bcount,
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100528 !!(pc->flags & PC_FLAG_WRITING));
Borislav Petkov0eea6452008-02-02 19:56:36 +0100529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Update the current position */
Borislav Petkov8e555122008-04-18 00:46:27 +0200531 pc->xferred += bcount;
532 pc->cur_pos += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Borislav Petkov32925e12008-02-02 19:56:36 +0100534 /* And set the interrupt handler again */
535 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return ide_started;
537}
538
539/*
540 * This is the original routine that did the packet transfer.
541 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
542 * for that drive below. The algorithm is chosen based on drive type
543 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100544static ide_startstop_t idefloppy_transfer_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200546 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ide_startstop_t startstop;
548 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100549 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
552 printk(KERN_ERR "ide-floppy: Strange, packet command "
553 "initiated yet DRQ isn't asserted\n");
554 return startstop;
555 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200556 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100557 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
559 "issuing a packet command\n");
560 return ide_do_reset(drive);
561 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Set the interrupt routine */
564 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
565 /* Send the actual packet */
566 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
567 return ide_started;
568}
569
570
571/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100572 * What we have here is a classic case of a top half / bottom half interrupt
573 * service routine. In interrupt mode, the device sends an interrupt to signal
574 * that it is ready to receive a packet. However, we need to delay about 2-3
575 * ticks before issuing the packet or we gets in trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100577 * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
578 * In either case, when the device says it's ready for a packet, we schedule
579 * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100581static int idefloppy_transfer_pc2(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 idefloppy_floppy_t *floppy = drive->driver_data;
584
585 /* Send the actual packet */
586 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
587 /* Timeout for the packet command */
588 return IDEFLOPPY_WAIT_CMD;
589}
590
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100591static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200593 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 idefloppy_floppy_t *floppy = drive->driver_data;
595 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100596 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
599 printk(KERN_ERR "ide-floppy: Strange, packet command "
600 "initiated yet DRQ isn't asserted\n");
601 return startstop;
602 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200603 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100604 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
606 "while issuing a packet command\n");
607 return ide_do_reset(drive);
608 }
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100609 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * The following delay solves a problem with ATAPI Zip 100 drives
611 * where the Busy flag was apparently being deasserted before the
612 * unit was ready to receive data. This was happening on a
613 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
614 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
615 * used until after the packet is moved in about 50 msec.
616 */
Borislav Petkov0078df22008-02-02 19:56:33 +0100617
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100618 ide_set_handler(drive, &idefloppy_pc_intr, floppy->ticks,
619 &idefloppy_transfer_pc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return ide_started;
621}
622
Borislav Petkovd652c132008-02-02 19:56:35 +0100623static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200624 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Borislav Petkovd652c132008-02-02 19:56:35 +0100626 /* supress error messages resulting from Medium not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (floppy->sense_key == 0x02 &&
628 floppy->asc == 0x3a &&
629 floppy->ascq == 0x00)
Borislav Petkovd652c132008-02-02 19:56:35 +0100630 return;
631
632 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
633 "asc = %2x, ascq = %2x\n",
634 floppy->drive->name, pc->c[0], floppy->sense_key,
635 floppy->asc, floppy->ascq);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100639static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
Borislav Petkov8e555122008-04-18 00:46:27 +0200640 struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 idefloppy_floppy_t *floppy = drive->driver_data;
643 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100645 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100646 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100649 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 floppy->failed_pc = pc;
651 /* Set the current packet command */
652 floppy->pc = pc;
653
Borislav Petkov757ced82008-02-02 19:56:37 +0100654 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100655 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
Borislav Petkov757ced82008-02-02 19:56:37 +0100656 ide_floppy_report_error(floppy, pc);
657 /* Giving up */
658 pc->error = IDEFLOPPY_ERROR_GENERAL;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 floppy->failed_pc = NULL;
Borislav Petkov8e555122008-04-18 00:46:27 +0200661 pc->idefloppy_callback(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return ide_stopped;
663 }
664
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100665 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 pc->retries++;
668 /* We haven't transferred any data yet */
Borislav Petkov8e555122008-04-18 00:46:27 +0200669 pc->xferred = 0;
670 pc->cur_pos = pc->buf;
671 bcount = min(pc->req_xfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100673 if (pc->flags & PC_FLAG_DMA_ERROR) {
674 pc->flags &= ~PC_FLAG_DMA_ERROR;
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100675 ide_dma_off(drive);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100676 }
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100677 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100679 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100680 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100682 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
683 IDE_TFLAG_OUT_DEVICE, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100685 if (dma) {
686 /* Begin DMA, if necessary */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100687 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 hwif->dma_start(drive);
689 }
690
691 /* Can we transfer the packet when we get the interrupt or wait? */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100692 if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* wait */
694 pkt_xfer_routine = &idefloppy_transfer_pc1;
695 } else {
696 /* immediate */
697 pkt_xfer_routine = &idefloppy_transfer_pc;
698 }
Borislav Petkov8e555122008-04-18 00:46:27 +0200699
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100700 if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* Issue the packet command */
702 ide_execute_command(drive, WIN_PACKETCMD,
703 pkt_xfer_routine,
704 IDEFLOPPY_WAIT_CMD,
705 NULL);
706 return ide_started;
707 } else {
708 /* Issue the packet command */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200709 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return (*pkt_xfer_routine) (drive);
711 }
712}
713
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100714static void idefloppy_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100716 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 idefloppy_do_end_request(drive, 1, 0);
719 return;
720}
721
Borislav Petkov8e555122008-04-18 00:46:27 +0200722static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100724 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100727 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 pc->c[4] = prevent;
729}
730
Borislav Petkov8e555122008-04-18 00:46:27 +0200731static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100734 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 pc->c[7] = 255;
736 pc->c[8] = 255;
Borislav Petkov8e555122008-04-18 00:46:27 +0200737 pc->req_xfer = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Borislav Petkov8e555122008-04-18 00:46:27 +0200740static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
741 int l, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100744 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 pc->c[1] = 0x17;
746
Borislav Petkov8e555122008-04-18 00:46:27 +0200747 memset(pc->buf, 0, 12);
748 pc->buf[1] = 0xA2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
750
751 if (flags & 1) /* Verify bit on... */
Borislav Petkov8e555122008-04-18 00:46:27 +0200752 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
753 pc->buf[3] = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Borislav Petkov8e555122008-04-18 00:46:27 +0200755 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
756 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
757 pc->buf_size = 12;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100758 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100761/* A mode sense command is used to "sense" floppy parameters. */
Borislav Petkov8e555122008-04-18 00:46:27 +0200762static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
763 u8 page_code, u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Borislav Petkov24a5d702008-02-02 19:56:34 +0100765 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100768 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 pc->c[1] = 0;
770 pc->c[2] = page_code + (type << 6);
771
772 switch (page_code) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100773 case IDEFLOPPY_CAPABILITIES_PAGE:
774 length += 12;
775 break;
776 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
777 length += 32;
778 break;
779 default:
780 printk(KERN_ERR "ide-floppy: unsupported page code "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 "in create_mode_sense_cmd\n");
782 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100783 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Borislav Petkov8e555122008-04-18 00:46:27 +0200784 pc->req_xfer = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Borislav Petkov8e555122008-04-18 00:46:27 +0200787static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100790 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 pc->c[4] = start;
792}
793
Borislav Petkov8e555122008-04-18 00:46:27 +0200794static void idefloppy_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100797 pc->c[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100800static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200801 struct ide_atapi_pc *pc, struct request *rq,
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100802 unsigned long sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 int block = sector / floppy->bs_factor;
805 int blocks = rq->nr_sectors / floppy->bs_factor;
806 int cmd = rq_data_dir(rq);
807
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100808 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 block, blocks);
810
811 idefloppy_init_pc(pc);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100812 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
813 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Borislav Petkov8f622432008-02-02 19:56:33 +0100814 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100815
Borislav Petkov8e555122008-04-18 00:46:27 +0200816 pc->idefloppy_callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 pc->rq = rq;
818 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200819 if (rq->cmd_flags & REQ_RW)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100820 pc->flags |= PC_FLAG_WRITING;
Borislav Petkov8e555122008-04-18 00:46:27 +0200821 pc->buf = NULL;
822 pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100823 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100826static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
Borislav Petkov8e555122008-04-18 00:46:27 +0200827 struct ide_atapi_pc *pc, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 idefloppy_init_pc(pc);
Borislav Petkov8e555122008-04-18 00:46:27 +0200830 pc->idefloppy_callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200832 pc->rq = rq;
833 pc->b_count = rq->data_len;
834 if (rq->data_len && rq_data_dir(rq) == WRITE)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100835 pc->flags |= PC_FLAG_WRITING;
Borislav Petkov8e555122008-04-18 00:46:27 +0200836 pc->buf = rq->data;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200837 if (rq->bio)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100838 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200839 /*
840 * possibly problematic, doesn't look like ide-floppy correctly
841 * handled scattered requests if dma fails...
842 */
Borislav Petkov8e555122008-04-18 00:46:27 +0200843 pc->req_xfer = pc->buf_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100846static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
847 struct request *rq, sector_t block_s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200850 struct ide_atapi_pc *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned long block = (unsigned long)block_s;
852
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100853 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -0700854 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100855 rq->cmd_type, rq->errors);
856 debug_log("sector: %ld, nr_sectors: %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 "current_nr_sectors: %d\n", (long)rq->sector,
858 rq->nr_sectors, rq->current_nr_sectors);
859
860 if (rq->errors >= ERROR_MAX) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100861 if (floppy->failed_pc)
862 ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 else
864 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
865 drive->name);
866 idefloppy_do_end_request(drive, 0, 0);
867 return ide_stopped;
868 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200869 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (((long)rq->sector % floppy->bs_factor) ||
871 (rq->nr_sectors % floppy->bs_factor)) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100872 printk(KERN_ERR "%s: unsupported r/w request size\n",
873 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 idefloppy_do_end_request(drive, 0, 0);
875 return ide_stopped;
876 }
877 pc = idefloppy_next_pc_storage(drive);
878 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200879 } else if (blk_special_request(rq)) {
Borislav Petkov8e555122008-04-18 00:46:27 +0200880 pc = (struct ide_atapi_pc *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200881 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200883 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else {
885 blk_dump_rq_flags(rq,
886 "ide-floppy: unsupported command in queue");
887 idefloppy_do_end_request(drive, 0, 0);
888 return ide_stopped;
889 }
890
891 pc->rq = rq;
892 return idefloppy_issue_pc(drive, pc);
893}
894
895/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100896 * Add a special packet command request to the tail of the request queue,
897 * and wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Borislav Petkov8e555122008-04-18 00:46:27 +0200899static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct ide_floppy_obj *floppy = drive->driver_data;
902 struct request rq;
903
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100904 ide_init_drive_cmd(&rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200906 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 rq.rq_disk = floppy->disk;
908
909 return ide_do_drive_cmd(drive, &rq, ide_wait);
910}
911
912/*
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100913 * Look at the flexible disk page parameters. We ignore the CHS capacity
914 * parameters and use the LBA parameters instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100916static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200919 struct ide_atapi_pc pc;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100920 u8 *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int capacity, lba_capacity;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100922 u16 transfer_rate, sector_size, cyls, rpm;
923 u8 heads, sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100925 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
926 MODE_SENSE_CURRENT);
927
928 if (idefloppy_queue_pc_tail(drive, &pc)) {
929 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
930 " parameters\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 1;
932 }
Borislav Petkov8e555122008-04-18 00:46:27 +0200933 floppy->wp = !!(pc.buf[3] & 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 set_disk_ro(floppy->disk, floppy->wp);
Borislav Petkov8e555122008-04-18 00:46:27 +0200935 page = &pc.buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Borislav Petkov8e555122008-04-18 00:46:27 +0200937 transfer_rate = be16_to_cpu(*(u16 *)&pc.buf[8 + 2]);
938 sector_size = be16_to_cpu(*(u16 *)&pc.buf[8 + 6]);
939 cyls = be16_to_cpu(*(u16 *)&pc.buf[8 + 8]);
940 rpm = be16_to_cpu(*(u16 *)&pc.buf[8 + 28]);
941 heads = pc.buf[8 + 4];
942 sectors = pc.buf[8 + 5];
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100943
944 capacity = cyls * heads * sectors * sector_size;
945
946 if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
948 "%d sector size, %d rpm\n",
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100949 drive->name, capacity / 1024, cyls, heads,
950 sectors, transfer_rate / 8, sector_size, rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100952 memcpy(&floppy->flexible_disk_page, page, 32);
953 drive->bios_cyl = cyls;
954 drive->bios_head = heads;
955 drive->bios_sect = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 lba_capacity = floppy->blocks * floppy->block_size;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (capacity < lba_capacity) {
959 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
960 "bytes, but the drive only handles %d\n",
961 drive->name, lba_capacity, capacity);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100962 floppy->blocks = floppy->block_size ?
963 capacity / floppy->block_size : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 return 0;
966}
967
Borislav Petkov948391d2008-02-02 19:56:34 +0100968static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200971 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 floppy->srfp = 0;
974 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
975 MODE_SENSE_CURRENT);
976
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100977 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
Borislav Petkov948391d2008-02-02 19:56:34 +0100978 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Borislav Petkov8e555122008-04-18 00:46:27 +0200981 floppy->srfp = pc.buf[8 + 2] & 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return (0);
983}
984
985/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100986 * Determine if a media is present in the floppy drive, and if so, its LBA
987 * capacity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100989static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +0200992 struct ide_atapi_pc pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100993 u8 *cap_desc;
994 u8 header_len, desc_cnt;
995 int i, rc = 1, blocks, length;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 drive->bios_cyl = 0;
998 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +0100999 floppy->blocks = 0;
1000 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 set_capacity(floppy->disk, 0);
1002
1003 idefloppy_create_read_capacity_cmd(&pc);
1004 if (idefloppy_queue_pc_tail(drive, &pc)) {
1005 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1006 return 1;
1007 }
Borislav Petkov8e555122008-04-18 00:46:27 +02001008 header_len = pc.buf[3];
1009 cap_desc = &pc.buf[4];
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001010 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001012 for (i = 0; i < desc_cnt; i++) {
1013 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Borislav Petkov8e555122008-04-18 00:46:27 +02001015 blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]);
1016 length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001017
1018 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1019 i, blocks * length / 1024, blocks, length);
1020
1021 if (i)
1022 continue;
1023 /*
1024 * the code below is valid only for the 1st descriptor, ie i=0
1025 */
1026
Borislav Petkov8e555122008-04-18 00:46:27 +02001027 switch (pc.buf[desc_start + 4] & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1029 case CAPACITY_UNFORMATTED:
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001030 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001031 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * If it is not a clik drive, break out
1033 * (maintains previous driver behaviour)
1034 */
1035 break;
1036 case CAPACITY_CURRENT:
1037 /* Normal Zip/LS-120 disks */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001038 if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1040 "sector size\n", drive->name,
1041 blocks * length / 1024, blocks, length);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001042 memcpy(&floppy->cap_desc, cap_desc, 8);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!length || length % 512) {
1045 printk(KERN_NOTICE "%s: %d bytes block size "
1046 "not supported\n", drive->name, length);
1047 } else {
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001048 floppy->blocks = blocks;
1049 floppy->block_size = length;
1050 floppy->bs_factor = length / 512;
1051 if (floppy->bs_factor != 1)
1052 printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 "512 bytes block size not "
1054 "fully supported\n",
1055 drive->name);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001056 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 break;
1059 case CAPACITY_NO_CARTRIDGE:
1060 /*
1061 * This is a KERN_ERR so it appears on screen
1062 * for the user to see
1063 */
1064 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1065 break;
1066 case CAPACITY_INVALID:
1067 printk(KERN_ERR "%s: Invalid capacity for disk "
1068 "in drive\n", drive->name);
1069 break;
1070 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001071 debug_log("Descriptor 0 Code: %d\n",
Borislav Petkov8e555122008-04-18 00:46:27 +02001072 pc.buf[desc_start + 4] & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074
1075 /* Clik! disk does not support get_flexible_disk_page */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001076 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001077 (void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1080 return rc;
1081}
1082
1083/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001084 * Obtain the list of formattable capacities.
1085 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1086 * descriptors to userland, instead of our own structures.
1087 *
1088 * Userland gives us the following structure:
1089 *
1090 * struct idefloppy_format_capacities {
1091 * int nformats;
1092 * struct {
1093 * int nblocks;
1094 * int blocksize;
1095 * } formats[];
1096 * };
1097 *
1098 * userland initializes nformats to the number of allocated formats[] records.
1099 * On exit we set nformats to the number of records we've actually initialized.
1100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001102static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Borislav Petkov8e555122008-04-18 00:46:27 +02001104 struct ide_atapi_pc pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001105 u8 header_len, desc_cnt;
1106 int i, blocks, length, u_array_size, u_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 int __user *argp;
1108
1109 if (get_user(u_array_size, arg))
1110 return (-EFAULT);
1111
1112 if (u_array_size <= 0)
1113 return (-EINVAL);
1114
1115 idefloppy_create_read_capacity_cmd(&pc);
1116 if (idefloppy_queue_pc_tail(drive, &pc)) {
1117 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001118 return (-EIO);
1119 }
Borislav Petkov8e555122008-04-18 00:46:27 +02001120 header_len = pc.buf[3];
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001121 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 u_index = 0;
1124 argp = arg + 1;
1125
1126 /*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001127 * We always skip the first capacity descriptor. That's the current
1128 * capacity. We are interested in the remaining descriptors, the
1129 * formattable capacities.
1130 */
1131 for (i = 1; i < desc_cnt; i++) {
1132 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (u_index >= u_array_size)
1135 break; /* User-supplied buffer too small */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Borislav Petkov8e555122008-04-18 00:46:27 +02001137 blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]);
1138 length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 if (put_user(blocks, argp))
1141 return(-EFAULT);
1142 ++argp;
1143
1144 if (put_user(length, argp))
1145 return (-EFAULT);
1146 ++argp;
1147
1148 ++u_index;
1149 }
1150
1151 if (put_user(u_index, arg))
1152 return (-EFAULT);
1153 return (0);
1154}
1155
1156/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001157 * Get ATAPI_FORMAT_UNIT progress indication.
1158 *
1159 * Userland gives a pointer to an int. The int is set to a progress
1160 * indicator 0-65536, with 65536=100%.
1161 *
1162 * If the drive does not support format progress indication, we just check
1163 * the dsc bit, and return either 0 or 65536.
1164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1167{
1168 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkov8e555122008-04-18 00:46:27 +02001169 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 int progress_indication = 0x10000;
1171
1172 if (floppy->srfp) {
1173 idefloppy_create_request_sense_cmd(&pc);
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001174 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return (-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (floppy->sense_key == 2 &&
1178 floppy->asc == 4 &&
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001179 floppy->ascq == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 progress_indication = floppy->progress_indication;
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001181
1182 /* Else assume format_unit has finished, and we're at 0x10000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001185 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 local_irq_save(flags);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001188 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 local_irq_restore(flags);
1190
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001191 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193 if (put_user(progress_indication, arg))
1194 return (-EFAULT);
1195
1196 return (0);
1197}
1198
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001199static sector_t idefloppy_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 idefloppy_floppy_t *floppy = drive->driver_data;
1202 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1203
1204 return capacity;
1205}
1206
1207/*
Borislav Petkovf373bd82008-02-02 19:56:36 +01001208 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1209 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
Borislav Petkovf373bd82008-02-02 19:56:36 +01001211static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Borislav Petkov3ad67762008-04-18 00:46:25 +02001213 u8 gcw[2];
1214 u8 device_type, protocol, removable, drq_type, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 *((u16 *) &gcw) = id->config;
1217
Borislav Petkov3ad67762008-04-18 00:46:25 +02001218 device_type = gcw[1] & 0x1F;
1219 removable = (gcw[0] & 0x80) >> 7;
1220 protocol = (gcw[1] & 0xC0) >> 6;
1221 drq_type = (gcw[0] & 0x60) >> 5;
1222 packet_size = gcw[0] & 0x03;
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224#ifdef CONFIG_PPC
1225 /* kludge for Apple PowerBook internal zip */
Borislav Petkov3ad67762008-04-18 00:46:25 +02001226 if (device_type == 5 &&
1227 !strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP"))
1228 device_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229#endif
1230
Borislav Petkov3ad67762008-04-18 00:46:25 +02001231 if (protocol != 2)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001232 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov3ad67762008-04-18 00:46:25 +02001233 protocol);
1234 else if (device_type != 0)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001235 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
Borislav Petkov3ad67762008-04-18 00:46:25 +02001236 "to floppy\n", device_type);
1237 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
Borislav Petkov3ad67762008-04-18 00:46:25 +02001239 else if (drq_type == 3)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001240 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
Borislav Petkov3ad67762008-04-18 00:46:25 +02001241 "supported\n", drq_type);
1242 else if (packet_size != 0)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001243 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
Borislav Petkov3ad67762008-04-18 00:46:25 +02001244 "bytes\n", packet_size);
1245 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 1;
1247 return 0;
1248}
1249
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001250#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251static void idefloppy_add_settings(ide_drive_t *drive)
1252{
1253 idefloppy_floppy_t *floppy = drive->driver_data;
1254
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001255 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
1256 &drive->bios_cyl, NULL);
1257 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1258 &drive->bios_head, NULL);
1259 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
1260 &drive->bios_sect, NULL);
1261 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1262 &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001264#else
1265static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001268static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Borislav Petkov3ad67762008-04-18 00:46:25 +02001270 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 *((u16 *) &gcw) = drive->id->config;
1273 floppy->pc = floppy->pc_stack;
Borislav Petkov3ad67762008-04-18 00:46:25 +02001274
1275 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001276 floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001278 * We used to check revisions here. At this point however I'm giving up.
1279 * Just assume they are all broken, its easier.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 *
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001281 * The actual reason for the workarounds was likely a driver bug after
1282 * all rather than a firmware bug, and the workaround below used to hide
1283 * it. It should be fixed as of version 1.9, but to be on the safe side
1284 * we'll leave the limitation below for the 2.2.x tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001287 floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* This value will be visible in the /proc/ide/hdx/settings */
1289 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1290 blk_queue_max_sectors(drive->queue, 64);
1291 }
1292
1293 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001294 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1295 * nasty clicking noises without it, so please don't remove this.
1296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1298 blk_queue_max_sectors(drive->queue, 64);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001299 floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001302 (void) ide_floppy_get_capacity(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 idefloppy_add_settings(drive);
1304}
1305
Russell King4031bbe2006-01-06 11:41:00 +00001306static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 idefloppy_floppy_t *floppy = drive->driver_data;
1309 struct gendisk *g = floppy->disk;
1310
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001311 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 del_gendisk(g);
1314
1315 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Borislav Petkov9a24b632008-02-02 19:56:33 +01001318static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1321 ide_drive_t *drive = floppy->drive;
1322 struct gendisk *g = floppy->disk;
1323
1324 drive->driver_data = NULL;
1325 g->private_data = NULL;
1326 put_disk(g);
1327 kfree(floppy);
1328}
1329
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001330#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001331static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1332 int count, int *eof, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 ide_drive_t*drive = (ide_drive_t *)data;
1335 int len;
1336
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001337 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1338 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341static ide_proc_entry_t idefloppy_proc[] = {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001342 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1343 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 { NULL, 0, NULL, NULL }
1345};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001346#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Russell King4031bbe2006-01-06 11:41:00 +00001348static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001351 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001352 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001353 .name = "ide-floppy",
1354 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001355 },
Russell King4031bbe2006-01-06 11:41:00 +00001356 .probe = ide_floppy_probe,
1357 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 .version = IDEFLOPPY_VERSION,
1359 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 .do_request = idefloppy_do_request,
1362 .end_request = idefloppy_do_end_request,
1363 .error = __ide_error,
1364 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001365#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368};
1369
1370static int idefloppy_open(struct inode *inode, struct file *filp)
1371{
1372 struct gendisk *disk = inode->i_bdev->bd_disk;
1373 struct ide_floppy_obj *floppy;
1374 ide_drive_t *drive;
Borislav Petkov8e555122008-04-18 00:46:27 +02001375 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 int ret = 0;
1377
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001378 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001380 floppy = ide_floppy_get(disk);
1381 if (!floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return -ENXIO;
1383
1384 drive = floppy->drive;
1385
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001386 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001388 if (floppy->openers == 1) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001389 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Just in case */
1391
1392 idefloppy_create_test_unit_ready_cmd(&pc);
1393 if (idefloppy_queue_pc_tail(drive, &pc)) {
1394 idefloppy_create_start_stop_cmd(&pc, 1);
1395 (void) idefloppy_queue_pc_tail(drive, &pc);
1396 }
1397
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001398 if (ide_floppy_get_capacity(drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 && (filp->f_flags & O_NDELAY) == 0
1400 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001401 * Allow O_NDELAY to open a drive without a disk, or with an
1402 * unreadable disk, so that we can get the format capacity
1403 * of the drive or begin the format - Sam
1404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 ret = -EIO;
1407 goto out_put_floppy;
1408 }
1409
1410 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 ret = -EROFS;
1412 goto out_put_floppy;
1413 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001414 floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001416 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 idefloppy_create_prevent_cmd(&pc, 1);
1418 (void) idefloppy_queue_pc_tail(drive, &pc);
1419 }
1420 check_disk_change(inode->i_bdev);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001421 } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 ret = -EBUSY;
1423 goto out_put_floppy;
1424 }
1425 return 0;
1426
1427out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001428 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 ide_floppy_put(floppy);
1430 return ret;
1431}
1432
1433static int idefloppy_release(struct inode *inode, struct file *filp)
1434{
1435 struct gendisk *disk = inode->i_bdev->bd_disk;
1436 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1437 ide_drive_t *drive = floppy->drive;
Borislav Petkov8e555122008-04-18 00:46:27 +02001438 struct ide_atapi_pc pc;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001439
1440 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001442 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001444 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 idefloppy_create_prevent_cmd(&pc, 0);
1446 (void) idefloppy_queue_pc_tail(drive, &pc);
1447 }
1448
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001449 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001451
1452 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 ide_floppy_put(floppy);
1455
1456 return 0;
1457}
1458
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001459static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1460{
1461 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1462 ide_drive_t *drive = floppy->drive;
1463
1464 geo->heads = drive->bios_head;
1465 geo->sectors = drive->bios_sect;
1466 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1467 return 0;
1468}
1469
Borislav Petkov8e555122008-04-18 00:46:27 +02001470static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy,
1471 struct ide_atapi_pc *pc, unsigned long arg, unsigned int cmd)
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001472{
1473 if (floppy->openers > 1)
1474 return -EBUSY;
1475
1476 /* The IOMEGA Clik! Drive doesn't support this command -
1477 * no room for an eject mechanism */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001478 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001479 int prevent = arg ? 1 : 0;
1480
1481 if (cmd == CDROMEJECT)
1482 prevent = 0;
1483
1484 idefloppy_create_prevent_cmd(pc, prevent);
1485 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1486 }
1487
1488 if (cmd == CDROMEJECT) {
1489 idefloppy_create_start_stop_cmd(pc, 2);
1490 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1491 }
1492
1493 return 0;
1494}
1495
1496static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1497 int __user *arg)
1498{
1499 int blocks, length, flags, err = 0;
Borislav Petkov8e555122008-04-18 00:46:27 +02001500 struct ide_atapi_pc pc;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001501
1502 if (floppy->openers > 1) {
1503 /* Don't format if someone is using the disk */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001504 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001505 return -EBUSY;
1506 }
1507
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001508 floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001509
1510 /*
1511 * Send ATAPI_FORMAT_UNIT to the drive.
1512 *
1513 * Userland gives us the following structure:
1514 *
1515 * struct idefloppy_format_command {
1516 * int nblocks;
1517 * int blocksize;
1518 * int flags;
1519 * } ;
1520 *
1521 * flags is a bitmask, currently, the only defined flag is:
1522 *
1523 * 0x01 - verify media after format.
1524 */
1525 if (get_user(blocks, arg) ||
1526 get_user(length, arg+1) ||
1527 get_user(flags, arg+2)) {
1528 err = -EFAULT;
1529 goto out;
1530 }
1531
1532 (void) idefloppy_get_sfrp_bit(floppy->drive);
1533 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1534
1535 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1536 err = -EIO;
1537
1538out:
1539 if (err)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001540 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001541 return err;
1542}
1543
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545static int idefloppy_ioctl(struct inode *inode, struct file *file,
1546 unsigned int cmd, unsigned long arg)
1547{
1548 struct block_device *bdev = inode->i_bdev;
1549 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1550 ide_drive_t *drive = floppy->drive;
Borislav Petkov8e555122008-04-18 00:46:27 +02001551 struct ide_atapi_pc pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001553 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 switch (cmd) {
1556 case CDROMEJECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* fall through */
1558 case CDROM_LOCKDOOR:
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001559 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1561 return 0;
1562 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001563 return ide_floppy_get_format_capacities(drive, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 case IDEFLOPPY_IOCTL_FORMAT_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (!(file->f_mode & 2))
1566 return -EPERM;
1567
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001568 return ide_floppy_format_unit(floppy, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1570 return idefloppy_get_format_progress(drive, argp);
1571 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001572
1573 /*
1574 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1575 * and CDROM_SEND_PACKET (legacy) ioctls
1576 */
1577 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1578 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1579 bdev->bd_disk, cmd, argp);
1580 else
1581 err = -ENOTTY;
1582
1583 if (err == -ENOTTY)
1584 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1585
1586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589static int idefloppy_media_changed(struct gendisk *disk)
1590{
1591 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1592 ide_drive_t *drive = floppy->drive;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001593 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /* do not scan partitions twice if this is a removable device */
1596 if (drive->attach) {
1597 drive->attach = 0;
1598 return 0;
1599 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001600 ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
1601 floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
1602 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605static int idefloppy_revalidate_disk(struct gendisk *disk)
1606{
1607 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1608 set_capacity(disk, idefloppy_capacity(floppy->drive));
1609 return 0;
1610}
1611
1612static struct block_device_operations idefloppy_ops = {
1613 .owner = THIS_MODULE,
1614 .open = idefloppy_open,
1615 .release = idefloppy_release,
1616 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001617 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .media_changed = idefloppy_media_changed,
1619 .revalidate_disk= idefloppy_revalidate_disk
1620};
1621
Russell King4031bbe2006-01-06 11:41:00 +00001622static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 idefloppy_floppy_t *floppy;
1625 struct gendisk *g;
1626
1627 if (!strstr("ide-floppy", drive->driver_req))
1628 goto failed;
1629 if (!drive->present)
1630 goto failed;
1631 if (drive->media != ide_floppy)
1632 goto failed;
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001633 if (!idefloppy_identify_device(drive, drive->id)) {
1634 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1635 " of ide-floppy\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto failed;
1637 }
1638 if (drive->scsi) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001639 printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi"
1640 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 goto failed;
1642 }
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001643 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1644 if (!floppy) {
1645 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1646 " structure\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 goto failed;
1648 }
1649
1650 g = alloc_disk(1 << PARTN_BITS);
1651 if (!g)
1652 goto out_free_floppy;
1653
1654 ide_init_disk(g, drive);
1655
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001656 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 kref_init(&floppy->kref);
1659
1660 floppy->drive = drive;
1661 floppy->driver = &idefloppy_driver;
1662 floppy->disk = g;
1663
1664 g->private_data = &floppy->driver;
1665
1666 drive->driver_data = floppy;
1667
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001668 idefloppy_setup(drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 g->minors = 1 << PARTN_BITS;
1671 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1673 g->fops = &idefloppy_ops;
1674 drive->attach = 1;
1675 add_disk(g);
1676 return 0;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678out_free_floppy:
1679 kfree(floppy);
1680failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001681 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001684static void __exit idefloppy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001686 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001689static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001692 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Kay Sievers263756e2005-12-12 18:03:44 +01001695MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696module_init(idefloppy_init);
1697module_exit(idefloppy_exit);
1698MODULE_LICENSE("GPL");
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001699MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1700