blob: 4ce67bdb5d5e33876d3587e6332c3757b8205092 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081typedef struct idefloppy_packet_command_s {
82 u8 c[12]; /* Actual packet bytes */
Borislav Petkov0571c7a2008-02-02 19:56:38 +010083 int retries; /* On each retry, we increment
84 retries */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int error; /* Error code */
86 int request_transfer; /* Bytes to transfer */
87 int actually_transferred; /* Bytes actually transferred */
88 int buffer_size; /* Size of our data buffer */
Borislav Petkov0571c7a2008-02-02 19:56:38 +010089 int b_count; /* Missing/Available data on
90 the current buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct request *rq; /* The corresponding request */
92 u8 *buffer; /* Data buffer */
Borislav Petkov0571c7a2008-02-02 19:56:38 +010093 u8 *current_position; /* Pointer into above buffer */
94 void (*callback) (ide_drive_t *); /* Called when this packet
95 command is completed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
Borislav Petkov0571c7a2008-02-02 19:56:38 +010097 unsigned long flags; /* Status/Action bit flags: long
98 for set_bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099} idefloppy_pc_t;
100
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100101/* Packet command flag bits. */
102enum {
103 /* 1 when we prefer to use DMA if possible */
104 PC_FLAG_DMA_RECOMMENDED = (1 << 0),
105 /* 1 while DMA in progress */
106 PC_FLAG_DMA_IN_PROGRESS = (1 << 1),
107 /* 1 when encountered problem during DMA */
108 PC_FLAG_DMA_ERROR = (1 << 2),
109 /* Data direction */
110 PC_FLAG_WRITING = (1 << 3),
111 /* Suppress error reporting */
112 PC_FLAG_SUPPRESS_ERROR = (1 << 4),
113};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100115/* format capacities descriptor codes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define CAPACITY_INVALID 0x00
117#define CAPACITY_UNFORMATTED 0x01
118#define CAPACITY_CURRENT 0x02
119#define CAPACITY_NO_CARTRIDGE 0x03
120
121/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100122 * Most of our global data which we need to save even as we leave the driver
123 * due to an interrupt or a timer event is stored in a variable of type
124 * idefloppy_floppy_t, defined below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
126typedef struct ide_floppy_obj {
127 ide_drive_t *drive;
128 ide_driver_t *driver;
129 struct gendisk *disk;
130 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100131 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* Current packet command */
134 idefloppy_pc_t *pc;
135 /* Last failed packet command */
136 idefloppy_pc_t *failed_pc;
137 /* Packet command stack */
138 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
139 /* Next free packet command storage space */
140 int pc_stack_index;
141 struct request rq_stack[IDEFLOPPY_PC_STACK];
142 /* We implement a circular array */
143 int rq_stack_index;
144
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100145 /* Last error information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 u8 sense_key, asc, ascq;
147 /* delay this long before sending packet command */
148 u8 ticks;
149 int progress_indication;
150
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100151 /* Device information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* Current format */
153 int blocks, block_size, bs_factor;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100154 /* Last format capacity descriptor */
155 u8 cap_desc[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* Copy of the flexible disk page */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100157 u8 flexible_disk_page[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* Write protect */
159 int wp;
160 /* Supports format progress report */
161 int srfp;
162 /* Status/Action flags */
163 unsigned long flags;
164} idefloppy_floppy_t;
165
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200166#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100168/* Floppy flag bits values. */
169enum {
170 /* DRQ interrupt device */
171 IDEFLOPPY_FLAG_DRQ_INTERRUPT = (1 << 0),
172 /* Media may have changed */
173 IDEFLOPPY_FLAG_MEDIA_CHANGED = (1 << 1),
174 /* Format in progress */
175 IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS = (1 << 2),
176 /* Avoid commands not supported in Clik drive */
177 IDEFLOPPY_FLAG_CLIK_DRIVE = (1 << 3),
178 /* Requires BH algorithm for packets */
179 IDEFLOPPY_FLAG_ZIP_DRIVE = (1 << 4),
180};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100182/* Defines for the MODE SENSE command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#define MODE_SENSE_CURRENT 0x00
184#define MODE_SENSE_CHANGEABLE 0x01
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100185#define MODE_SENSE_DEFAULT 0x02
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define MODE_SENSE_SAVED 0x03
187
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100188/* IOCTLs used in low-level formatting. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
190#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
191#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
192#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
193
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100194/* Error code returned in rq->errors to the higher part of the driver. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#define IDEFLOPPY_ERROR_GENERAL 101
196
197/*
Borislav Petkov948391d2008-02-02 19:56:34 +0100198 * Pages of the SELECT SENSE / MODE SENSE packet commands.
199 * See SFF-8070i spec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
201#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
202#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
203
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800204static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
207
208#define ide_floppy_g(disk) \
209 container_of((disk)->private_data, struct ide_floppy_obj, driver)
210
211static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
212{
213 struct ide_floppy_obj *floppy = NULL;
214
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800215 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 floppy = ide_floppy_g(disk);
217 if (floppy)
218 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800219 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return floppy;
221}
222
Borislav Petkov9a24b632008-02-02 19:56:33 +0100223static void idefloppy_cleanup_obj(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225static void ide_floppy_put(struct ide_floppy_obj *floppy)
226{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800227 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100228 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800229 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100233 * Used to finish servicing a request. For read/write requests, we will call
234 * ide_end_request to pass to the next buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
237{
238 idefloppy_floppy_t *floppy = drive->driver_data;
239 struct request *rq = HWGROUP(drive)->rq;
240 int error;
241
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100242 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 switch (uptodate) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100245 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
246 case 1: error = 0; break;
247 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 if (error)
250 floppy->failed_pc = NULL;
251 /* Why does this happen? */
252 if (!rq)
253 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200254 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* our real local end request function */
256 ide_end_request(drive, uptodate, nsecs);
257 return 0;
258 }
259 rq->errors = error;
260 /* fixme: need to move this local also */
261 ide_end_drive_cmd(drive, 0, 0);
262 return 0;
263}
264
Borislav Petkov32925e12008-02-02 19:56:36 +0100265static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc,
266 unsigned int bcount, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200269 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct bio_vec *bvec;
271 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200272 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 char *data;
274
NeilBrown5705f702007-09-25 12:35:59 +0200275 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200276 if (!bcount)
277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Jens Axboe6c92e692007-08-16 13:43:12 +0200279 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Jens Axboe6c92e692007-08-16 13:43:12 +0200281 data = bvec_kmap_irq(bvec, &flags);
Borislav Petkov32925e12008-02-02 19:56:36 +0100282 if (direction)
283 drive->hwif->atapi_output_bytes(drive, data, count);
284 else
285 drive->hwif->atapi_input_bytes(drive, data, count);
Jens Axboe6c92e692007-08-16 13:43:12 +0200286 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Jens Axboe6c92e692007-08-16 13:43:12 +0200288 bcount -= count;
289 pc->b_count += count;
290 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
293 idefloppy_do_end_request(drive, 1, done >> 9);
294
295 if (bcount) {
Borislav Petkov32925e12008-02-02 19:56:36 +0100296 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
297 drive->name, __func__, bcount);
298 if (direction)
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200299 ide_atapi_write_zeros(drive, bcount);
Borislav Petkov32925e12008-02-02 19:56:36 +0100300 else
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200301 ide_atapi_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303}
304
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100305static void idefloppy_update_buffers(ide_drive_t *drive, idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct request *rq = pc->rq;
308 struct bio *bio = rq->bio;
309
310 while ((bio = rq->bio) != NULL)
311 idefloppy_do_end_request(drive, 1, 0);
312}
313
314/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100315 * Generate a new packet command request in front of the request queue, before
316 * the current request so that it will be processed immediately, on the next
317 * pass through the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100319static void idefloppy_queue_pc_head(ide_drive_t *drive, idefloppy_pc_t *pc,
320 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 struct ide_floppy_obj *floppy = drive->driver_data;
323
324 ide_init_drive_cmd(rq);
325 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200326 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 rq->rq_disk = floppy->disk;
328 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
329}
330
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100331static idefloppy_pc_t *idefloppy_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 idefloppy_floppy_t *floppy = drive->driver_data;
334
335 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100336 floppy->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return (&floppy->pc_stack[floppy->pc_stack_index++]);
338}
339
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100340static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 idefloppy_floppy_t *floppy = drive->driver_data;
343
344 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
345 floppy->rq_stack_index = 0;
346 return (&floppy->rq_stack[floppy->rq_stack_index++]);
347}
348
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100349static void idefloppy_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100352 u8 *buf = floppy->pc->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100354 debug_log("Reached %s\n", __func__);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!floppy->pc->error) {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100357 floppy->sense_key = buf[2] & 0x0F;
358 floppy->asc = buf[12];
359 floppy->ascq = buf[13];
360 floppy->progress_indication = buf[15] & 0x80 ?
361 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
362
363 if (floppy->failed_pc)
364 debug_log("pc = %x, sense key = %x, asc = %x,"
365 " ascq = %x\n",
366 floppy->failed_pc->c[0],
367 floppy->sense_key,
368 floppy->asc,
369 floppy->ascq);
370 else
371 debug_log("sense key = %x, asc = %x, ascq = %x\n",
372 floppy->sense_key,
373 floppy->asc,
374 floppy->ascq);
375
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 idefloppy_do_end_request(drive, 1, 0);
378 } else {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100379 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
380 " request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 idefloppy_do_end_request(drive, 0, 0);
382 }
383}
384
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100385/* General packet command callback function. */
386static void idefloppy_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100389
390 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
393}
394
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100395static void idefloppy_init_pc(idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 memset(pc->c, 0, 12);
398 pc->retries = 0;
399 pc->flags = 0;
400 pc->request_transfer = 0;
401 pc->buffer = pc->pc_buffer;
402 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
403 pc->callback = &idefloppy_pc_callback;
404}
405
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100406static void idefloppy_create_request_sense_cmd(idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Borislav Petkov30d67092008-02-02 19:56:33 +0100408 idefloppy_init_pc(pc);
409 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 pc->c[4] = 255;
411 pc->request_transfer = 18;
412 pc->callback = &idefloppy_request_sense_callback;
413}
414
415/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100416 * Called when an error was detected during the last packet command. We queue a
417 * request sense packet command in the head of the request list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100419static void idefloppy_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 idefloppy_pc_t *pc;
422 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100424 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 pc = idefloppy_next_pc_storage(drive);
426 rq = idefloppy_next_rq_storage(drive);
427 idefloppy_create_request_sense_cmd(pc);
428 idefloppy_queue_pc_head(drive, pc, rq);
429}
430
Borislav Petkov0eea6452008-02-02 19:56:36 +0100431/* The usual interrupt handler called during a packet command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
433{
434 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100435 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 idefloppy_pc_t *pc = floppy->pc;
437 struct request *rq = pc->rq;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100438 xfer_func_t *xferfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 unsigned int temp;
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100440 int dma_error = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100441 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100442 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100444 debug_log("Reached %s interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100446 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100447 dma_error = hwif->ide_dma_end(drive);
448 if (dma_error) {
449 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
450 rq_data_dir(rq) ? "write" : "read");
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100451 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 } else {
453 pc->actually_transferred = pc->request_transfer;
454 idefloppy_update_buffers(drive, pc);
455 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100456 debug_log("DMA finished\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
459 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100460 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100462 /* No more interrupts */
463 if ((stat & DRQ_STAT) == 0) {
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100464 debug_log("Packet command completed, %d bytes transferred\n",
465 pc->actually_transferred);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100466 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Ingo Molnar366c7f52006-07-03 00:25:25 -0700468 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100470 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Error detected */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100472 debug_log("%s: I/O error\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 rq->errors++;
Borislav Petkov30d67092008-02-02 19:56:33 +0100474 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 printk(KERN_ERR "ide-floppy: I/O error in "
476 "request sense command\n");
477 return ide_do_reset(drive);
478 }
479 /* Retry operation */
480 idefloppy_retry_pc(drive);
481 /* queued, but not started */
482 return ide_stopped;
483 }
484 pc->error = 0;
485 if (floppy->failed_pc == pc)
486 floppy->failed_pc = NULL;
487 /* Command finished - Call the callback function */
488 pc->callback(drive);
489 return ide_stopped;
490 }
491
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100492 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
493 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
495 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100496 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return ide_do_reset(drive);
498 }
499
500 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200501 bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
502 hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* on this interrupt */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200504 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100506 if (ireason & CD) {
Borislav Petkov0eea6452008-02-02 19:56:36 +0100507 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return ide_do_reset(drive);
509 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100510 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Hopefully, we will never get here */
512 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100513 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 printk(KERN_ERR "but the floppy wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100515 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return ide_do_reset(drive);
517 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100518 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100520 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (temp > pc->request_transfer) {
522 if (temp > pc->buffer_size) {
523 printk(KERN_ERR "ide-floppy: The floppy wants "
524 "to send us more data than expected "
525 "- discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200526 ide_atapi_discard_data(drive, bcount);
Borislav Petkov0078df22008-02-02 19:56:33 +0100527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ide_set_handler(drive,
529 &idefloppy_pc_intr,
530 IDEFLOPPY_WAIT_CMD,
531 NULL);
532 return ide_started;
533 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100534 debug_log("The floppy wants to send us more data than"
535 " expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100538 if (pc->flags & PC_FLAG_WRITING)
Borislav Petkov0eea6452008-02-02 19:56:36 +0100539 xferfunc = hwif->atapi_output_bytes;
Borislav Petkov32925e12008-02-02 19:56:36 +0100540 else
Borislav Petkov0eea6452008-02-02 19:56:36 +0100541 xferfunc = hwif->atapi_input_bytes;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100542
543 if (pc->buffer)
544 xferfunc(drive, pc->current_position, bcount);
545 else
Borislav Petkov32925e12008-02-02 19:56:36 +0100546 ide_floppy_io_buffers(drive, pc, bcount,
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100547 !!(pc->flags & PC_FLAG_WRITING));
Borislav Petkov0eea6452008-02-02 19:56:36 +0100548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100550 pc->actually_transferred += bcount;
551 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Borislav Petkov32925e12008-02-02 19:56:36 +0100553 /* And set the interrupt handler again */
554 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return ide_started;
556}
557
558/*
559 * This is the original routine that did the packet transfer.
560 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
561 * for that drive below. The algorithm is chosen based on drive type
562 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100563static ide_startstop_t idefloppy_transfer_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200565 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 ide_startstop_t startstop;
567 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100568 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
571 printk(KERN_ERR "ide-floppy: Strange, packet command "
572 "initiated yet DRQ isn't asserted\n");
573 return startstop;
574 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200575 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100576 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
578 "issuing a packet command\n");
579 return ide_do_reset(drive);
580 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Set the interrupt routine */
583 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
584 /* Send the actual packet */
585 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
586 return ide_started;
587}
588
589
590/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100591 * What we have here is a classic case of a top half / bottom half interrupt
592 * service routine. In interrupt mode, the device sends an interrupt to signal
593 * that it is ready to receive a packet. However, we need to delay about 2-3
594 * ticks before issuing the packet or we gets in trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 *
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100596 * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
597 * In either case, when the device says it's ready for a packet, we schedule
598 * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100600static int idefloppy_transfer_pc2(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 idefloppy_floppy_t *floppy = drive->driver_data;
603
604 /* Send the actual packet */
605 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
606 /* Timeout for the packet command */
607 return IDEFLOPPY_WAIT_CMD;
608}
609
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100610static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200612 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 idefloppy_floppy_t *floppy = drive->driver_data;
614 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100615 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
618 printk(KERN_ERR "ide-floppy: Strange, packet command "
619 "initiated yet DRQ isn't asserted\n");
620 return startstop;
621 }
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200622 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100623 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
625 "while issuing a packet command\n");
626 return ide_do_reset(drive);
627 }
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100628 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * The following delay solves a problem with ATAPI Zip 100 drives
630 * where the Busy flag was apparently being deasserted before the
631 * unit was ready to receive data. This was happening on a
632 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
633 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
634 * used until after the packet is moved in about 50 msec.
635 */
Borislav Petkov0078df22008-02-02 19:56:33 +0100636
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100637 ide_set_handler(drive, &idefloppy_pc_intr, floppy->ticks,
638 &idefloppy_transfer_pc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return ide_started;
640}
641
Borislav Petkovd652c132008-02-02 19:56:35 +0100642static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
643 idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Borislav Petkovd652c132008-02-02 19:56:35 +0100645 /* supress error messages resulting from Medium not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (floppy->sense_key == 0x02 &&
647 floppy->asc == 0x3a &&
648 floppy->ascq == 0x00)
Borislav Petkovd652c132008-02-02 19:56:35 +0100649 return;
650
651 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
652 "asc = %2x, ascq = %2x\n",
653 floppy->drive->name, pc->c[0], floppy->sense_key,
654 floppy->asc, floppy->ascq);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100658static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
659 idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 idefloppy_floppy_t *floppy = drive->driver_data;
662 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100664 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100665 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100668 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 floppy->failed_pc = pc;
670 /* Set the current packet command */
671 floppy->pc = pc;
672
Borislav Petkov757ced82008-02-02 19:56:37 +0100673 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100674 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
Borislav Petkov757ced82008-02-02 19:56:37 +0100675 ide_floppy_report_error(floppy, pc);
676 /* Giving up */
677 pc->error = IDEFLOPPY_ERROR_GENERAL;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 floppy->failed_pc = NULL;
680 pc->callback(drive);
681 return ide_stopped;
682 }
683
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100684 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 pc->retries++;
687 /* We haven't transferred any data yet */
688 pc->actually_transferred = 0;
689 pc->current_position = pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100690 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100692 if (pc->flags & PC_FLAG_DMA_ERROR) {
693 pc->flags &= ~PC_FLAG_DMA_ERROR;
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100694 ide_dma_off(drive);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100695 }
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100696 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100698 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100699 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100701 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
702 IDE_TFLAG_OUT_DEVICE, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100704 if (dma) {
705 /* Begin DMA, if necessary */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100706 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 hwif->dma_start(drive);
708 }
709
710 /* Can we transfer the packet when we get the interrupt or wait? */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100711 if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* wait */
713 pkt_xfer_routine = &idefloppy_transfer_pc1;
714 } else {
715 /* immediate */
716 pkt_xfer_routine = &idefloppy_transfer_pc;
717 }
718
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100719 if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* Issue the packet command */
721 ide_execute_command(drive, WIN_PACKETCMD,
722 pkt_xfer_routine,
723 IDEFLOPPY_WAIT_CMD,
724 NULL);
725 return ide_started;
726 } else {
727 /* Issue the packet command */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200728 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return (*pkt_xfer_routine) (drive);
730 }
731}
732
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100733static void idefloppy_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100735 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 idefloppy_do_end_request(drive, 1, 0);
738 return;
739}
740
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100741static void idefloppy_create_prevent_cmd(idefloppy_pc_t *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100743 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100746 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 pc->c[4] = prevent;
748}
749
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100750static void idefloppy_create_read_capacity_cmd(idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100753 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 pc->c[7] = 255;
755 pc->c[8] = 255;
756 pc->request_transfer = 255;
757}
758
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100759static void idefloppy_create_format_unit_cmd(idefloppy_pc_t *pc, int b, int l,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int flags)
761{
762 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100763 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 pc->c[1] = 0x17;
765
766 memset(pc->buffer, 0, 12);
767 pc->buffer[1] = 0xA2;
768 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
769
770 if (flags & 1) /* Verify bit on... */
771 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
772 pc->buffer[3] = 8;
773
Borislav Petkov8f622432008-02-02 19:56:33 +0100774 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
775 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100776 pc->buffer_size = 12;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100777 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100780/* A mode sense command is used to "sense" floppy parameters. */
781static void idefloppy_create_mode_sense_cmd(idefloppy_pc_t *pc, u8 page_code,
782 u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Borislav Petkov24a5d702008-02-02 19:56:34 +0100784 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100787 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 pc->c[1] = 0;
789 pc->c[2] = page_code + (type << 6);
790
791 switch (page_code) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100792 case IDEFLOPPY_CAPABILITIES_PAGE:
793 length += 12;
794 break;
795 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
796 length += 32;
797 break;
798 default:
799 printk(KERN_ERR "ide-floppy: unsupported page code "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 "in create_mode_sense_cmd\n");
801 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100802 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 pc->request_transfer = length;
804}
805
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100806static void idefloppy_create_start_stop_cmd(idefloppy_pc_t *pc, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100809 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 pc->c[4] = start;
811}
812
813static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
814{
815 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100816 pc->c[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100819static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
820 idefloppy_pc_t *pc, struct request *rq,
821 unsigned long sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 int block = sector / floppy->bs_factor;
824 int blocks = rq->nr_sectors / floppy->bs_factor;
825 int cmd = rq_data_dir(rq);
826
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100827 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 block, blocks);
829
830 idefloppy_init_pc(pc);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100831 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
832 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Borislav Petkov8f622432008-02-02 19:56:33 +0100833 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 pc->callback = &idefloppy_rw_callback;
836 pc->rq = rq;
837 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200838 if (rq->cmd_flags & REQ_RW)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100839 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 pc->buffer = NULL;
841 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100842 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100845static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
846 idefloppy_pc_t *pc, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 idefloppy_init_pc(pc);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200849 pc->callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200851 pc->rq = rq;
852 pc->b_count = rq->data_len;
853 if (rq->data_len && rq_data_dir(rq) == WRITE)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100854 pc->flags |= PC_FLAG_WRITING;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200855 pc->buffer = rq->data;
856 if (rq->bio)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100857 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200858 /*
859 * possibly problematic, doesn't look like ide-floppy correctly
860 * handled scattered requests if dma fails...
861 */
862 pc->request_transfer = pc->buffer_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100865static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
866 struct request *rq, sector_t block_s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 idefloppy_floppy_t *floppy = drive->driver_data;
869 idefloppy_pc_t *pc;
870 unsigned long block = (unsigned long)block_s;
871
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100872 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -0700873 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100874 rq->cmd_type, rq->errors);
875 debug_log("sector: %ld, nr_sectors: %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 "current_nr_sectors: %d\n", (long)rq->sector,
877 rq->nr_sectors, rq->current_nr_sectors);
878
879 if (rq->errors >= ERROR_MAX) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100880 if (floppy->failed_pc)
881 ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 else
883 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
884 drive->name);
885 idefloppy_do_end_request(drive, 0, 0);
886 return ide_stopped;
887 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200888 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (((long)rq->sector % floppy->bs_factor) ||
890 (rq->nr_sectors % floppy->bs_factor)) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100891 printk(KERN_ERR "%s: unsupported r/w request size\n",
892 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 idefloppy_do_end_request(drive, 0, 0);
894 return ide_stopped;
895 }
896 pc = idefloppy_next_pc_storage(drive);
897 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200898 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 pc = (idefloppy_pc_t *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200900 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200902 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 } else {
904 blk_dump_rq_flags(rq,
905 "ide-floppy: unsupported command in queue");
906 idefloppy_do_end_request(drive, 0, 0);
907 return ide_stopped;
908 }
909
910 pc->rq = rq;
911 return idefloppy_issue_pc(drive, pc);
912}
913
914/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100915 * Add a special packet command request to the tail of the request queue,
916 * and wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100918static int idefloppy_queue_pc_tail(ide_drive_t *drive, idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 struct ide_floppy_obj *floppy = drive->driver_data;
921 struct request rq;
922
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100923 ide_init_drive_cmd(&rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200925 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 rq.rq_disk = floppy->disk;
927
928 return ide_do_drive_cmd(drive, &rq, ide_wait);
929}
930
931/*
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100932 * Look at the flexible disk page parameters. We ignore the CHS capacity
933 * parameters and use the LBA parameters instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100935static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 idefloppy_floppy_t *floppy = drive->driver_data;
938 idefloppy_pc_t pc;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100939 u8 *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int capacity, lba_capacity;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100941 u16 transfer_rate, sector_size, cyls, rpm;
942 u8 heads, sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100944 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
945 MODE_SENSE_CURRENT);
946
947 if (idefloppy_queue_pc_tail(drive, &pc)) {
948 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
949 " parameters\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return 1;
951 }
Borislav Petkov24a5d702008-02-02 19:56:34 +0100952 floppy->wp = !!(pc.buffer[3] & 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 set_disk_ro(floppy->disk, floppy->wp);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100954 page = &pc.buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100956 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
957 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
958 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
959 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
960 heads = pc.buffer[8 + 4];
961 sectors = pc.buffer[8 + 5];
962
963 capacity = cyls * heads * sectors * sector_size;
964
965 if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
967 "%d sector size, %d rpm\n",
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100968 drive->name, capacity / 1024, cyls, heads,
969 sectors, transfer_rate / 8, sector_size, rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100971 memcpy(&floppy->flexible_disk_page, page, 32);
972 drive->bios_cyl = cyls;
973 drive->bios_head = heads;
974 drive->bios_sect = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 lba_capacity = floppy->blocks * floppy->block_size;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (capacity < lba_capacity) {
978 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
979 "bytes, but the drive only handles %d\n",
980 drive->name, lba_capacity, capacity);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100981 floppy->blocks = floppy->block_size ?
982 capacity / floppy->block_size : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 return 0;
985}
986
Borislav Petkov948391d2008-02-02 19:56:34 +0100987static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 idefloppy_floppy_t *floppy = drive->driver_data;
990 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 floppy->srfp = 0;
993 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
994 MODE_SENSE_CURRENT);
995
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100996 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
Borislav Petkov948391d2008-02-02 19:56:34 +0100997 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Borislav Petkov948391d2008-02-02 19:56:34 +01001000 floppy->srfp = pc.buffer[8 + 2] & 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return (0);
1002}
1003
1004/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001005 * Determine if a media is present in the floppy drive, and if so, its LBA
1006 * capacity.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001008static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 idefloppy_floppy_t *floppy = drive->driver_data;
1011 idefloppy_pc_t pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001012 u8 *cap_desc;
1013 u8 header_len, desc_cnt;
1014 int i, rc = 1, blocks, length;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 drive->bios_cyl = 0;
1017 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +01001018 floppy->blocks = 0;
1019 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 set_capacity(floppy->disk, 0);
1021
1022 idefloppy_create_read_capacity_cmd(&pc);
1023 if (idefloppy_queue_pc_tail(drive, &pc)) {
1024 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1025 return 1;
1026 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001027 header_len = pc.buffer[3];
1028 cap_desc = &pc.buffer[4];
1029 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001031 for (i = 0; i < desc_cnt; i++) {
1032 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001034 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1035 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1036
1037 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1038 i, blocks * length / 1024, blocks, length);
1039
1040 if (i)
1041 continue;
1042 /*
1043 * the code below is valid only for the 1st descriptor, ie i=0
1044 */
1045
1046 switch (pc.buffer[desc_start + 4] & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1048 case CAPACITY_UNFORMATTED:
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001049 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001050 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * If it is not a clik drive, break out
1052 * (maintains previous driver behaviour)
1053 */
1054 break;
1055 case CAPACITY_CURRENT:
1056 /* Normal Zip/LS-120 disks */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001057 if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1059 "sector size\n", drive->name,
1060 blocks * length / 1024, blocks, length);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001061 memcpy(&floppy->cap_desc, cap_desc, 8);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!length || length % 512) {
1064 printk(KERN_NOTICE "%s: %d bytes block size "
1065 "not supported\n", drive->name, length);
1066 } else {
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001067 floppy->blocks = blocks;
1068 floppy->block_size = length;
1069 floppy->bs_factor = length / 512;
1070 if (floppy->bs_factor != 1)
1071 printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 "512 bytes block size not "
1073 "fully supported\n",
1074 drive->name);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001075 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 break;
1078 case CAPACITY_NO_CARTRIDGE:
1079 /*
1080 * This is a KERN_ERR so it appears on screen
1081 * for the user to see
1082 */
1083 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1084 break;
1085 case CAPACITY_INVALID:
1086 printk(KERN_ERR "%s: Invalid capacity for disk "
1087 "in drive\n", drive->name);
1088 break;
1089 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001090 debug_log("Descriptor 0 Code: %d\n",
1091 pc.buffer[desc_start + 4] & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
1094 /* Clik! disk does not support get_flexible_disk_page */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001095 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001096 (void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1099 return rc;
1100}
1101
1102/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001103 * Obtain the list of formattable capacities.
1104 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1105 * descriptors to userland, instead of our own structures.
1106 *
1107 * Userland gives us the following structure:
1108 *
1109 * struct idefloppy_format_capacities {
1110 * int nformats;
1111 * struct {
1112 * int nblocks;
1113 * int blocksize;
1114 * } formats[];
1115 * };
1116 *
1117 * userland initializes nformats to the number of allocated formats[] records.
1118 * On exit we set nformats to the number of records we've actually initialized.
1119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001121static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001123 idefloppy_pc_t pc;
1124 u8 header_len, desc_cnt;
1125 int i, blocks, length, u_array_size, u_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int __user *argp;
1127
1128 if (get_user(u_array_size, arg))
1129 return (-EFAULT);
1130
1131 if (u_array_size <= 0)
1132 return (-EINVAL);
1133
1134 idefloppy_create_read_capacity_cmd(&pc);
1135 if (idefloppy_queue_pc_tail(drive, &pc)) {
1136 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001137 return (-EIO);
1138 }
1139 header_len = pc.buffer[3];
1140 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 u_index = 0;
1143 argp = arg + 1;
1144
1145 /*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001146 * We always skip the first capacity descriptor. That's the current
1147 * capacity. We are interested in the remaining descriptors, the
1148 * formattable capacities.
1149 */
1150 for (i = 1; i < desc_cnt; i++) {
1151 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (u_index >= u_array_size)
1154 break; /* User-supplied buffer too small */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001156 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1157 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 if (put_user(blocks, argp))
1160 return(-EFAULT);
1161 ++argp;
1162
1163 if (put_user(length, argp))
1164 return (-EFAULT);
1165 ++argp;
1166
1167 ++u_index;
1168 }
1169
1170 if (put_user(u_index, arg))
1171 return (-EFAULT);
1172 return (0);
1173}
1174
1175/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001176 * Get ATAPI_FORMAT_UNIT progress indication.
1177 *
1178 * Userland gives a pointer to an int. The int is set to a progress
1179 * indicator 0-65536, with 65536=100%.
1180 *
1181 * If the drive does not support format progress indication, we just check
1182 * the dsc bit, and return either 0 or 65536.
1183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1186{
1187 idefloppy_floppy_t *floppy = drive->driver_data;
1188 idefloppy_pc_t pc;
1189 int progress_indication = 0x10000;
1190
1191 if (floppy->srfp) {
1192 idefloppy_create_request_sense_cmd(&pc);
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001193 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return (-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (floppy->sense_key == 2 &&
1197 floppy->asc == 4 &&
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001198 floppy->ascq == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 progress_indication = floppy->progress_indication;
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001200
1201 /* Else assume format_unit has finished, and we're at 0x10000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001204 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 local_irq_save(flags);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001207 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 local_irq_restore(flags);
1209
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001210 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 if (put_user(progress_indication, arg))
1213 return (-EFAULT);
1214
1215 return (0);
1216}
1217
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001218static sector_t idefloppy_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 idefloppy_floppy_t *floppy = drive->driver_data;
1221 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1222
1223 return capacity;
1224}
1225
1226/*
Borislav Petkovf373bd82008-02-02 19:56:36 +01001227 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1228 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
Borislav Petkovf373bd82008-02-02 19:56:36 +01001230static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Borislav Petkov3ad67762008-04-18 00:46:25 +02001232 u8 gcw[2];
1233 u8 device_type, protocol, removable, drq_type, packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 *((u16 *) &gcw) = id->config;
1236
Borislav Petkov3ad67762008-04-18 00:46:25 +02001237 device_type = gcw[1] & 0x1F;
1238 removable = (gcw[0] & 0x80) >> 7;
1239 protocol = (gcw[1] & 0xC0) >> 6;
1240 drq_type = (gcw[0] & 0x60) >> 5;
1241 packet_size = gcw[0] & 0x03;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#ifdef CONFIG_PPC
1244 /* kludge for Apple PowerBook internal zip */
Borislav Petkov3ad67762008-04-18 00:46:25 +02001245 if (device_type == 5 &&
1246 !strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP"))
1247 device_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248#endif
1249
Borislav Petkov3ad67762008-04-18 00:46:25 +02001250 if (protocol != 2)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001251 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
Borislav Petkov3ad67762008-04-18 00:46:25 +02001252 protocol);
1253 else if (device_type != 0)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001254 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
Borislav Petkov3ad67762008-04-18 00:46:25 +02001255 "to floppy\n", device_type);
1256 else if (!removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
Borislav Petkov3ad67762008-04-18 00:46:25 +02001258 else if (drq_type == 3)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001259 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
Borislav Petkov3ad67762008-04-18 00:46:25 +02001260 "supported\n", drq_type);
1261 else if (packet_size != 0)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001262 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
Borislav Petkov3ad67762008-04-18 00:46:25 +02001263 "bytes\n", packet_size);
1264 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 1;
1266 return 0;
1267}
1268
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001269#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270static void idefloppy_add_settings(ide_drive_t *drive)
1271{
1272 idefloppy_floppy_t *floppy = drive->driver_data;
1273
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001274 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
1275 &drive->bios_cyl, NULL);
1276 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1277 &drive->bios_head, NULL);
1278 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
1279 &drive->bios_sect, NULL);
1280 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1281 &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001283#else
1284static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001287static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Borislav Petkov3ad67762008-04-18 00:46:25 +02001289 u8 gcw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 *((u16 *) &gcw) = drive->id->config;
1292 floppy->pc = floppy->pc_stack;
Borislav Petkov3ad67762008-04-18 00:46:25 +02001293
1294 if (((gcw[0] & 0x60) >> 5) == 1)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001295 floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001297 * We used to check revisions here. At this point however I'm giving up.
1298 * Just assume they are all broken, its easier.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 *
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001300 * The actual reason for the workarounds was likely a driver bug after
1301 * all rather than a firmware bug, and the workaround below used to hide
1302 * it. It should be fixed as of version 1.9, but to be on the safe side
1303 * we'll leave the limitation below for the 2.2.x tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001306 floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /* This value will be visible in the /proc/ide/hdx/settings */
1308 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1309 blk_queue_max_sectors(drive->queue, 64);
1310 }
1311
1312 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001313 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1314 * nasty clicking noises without it, so please don't remove this.
1315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1317 blk_queue_max_sectors(drive->queue, 64);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001318 floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001321 (void) ide_floppy_get_capacity(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 idefloppy_add_settings(drive);
1323}
1324
Russell King4031bbe2006-01-06 11:41:00 +00001325static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 idefloppy_floppy_t *floppy = drive->driver_data;
1328 struct gendisk *g = floppy->disk;
1329
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001330 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 del_gendisk(g);
1333
1334 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Borislav Petkov9a24b632008-02-02 19:56:33 +01001337static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1340 ide_drive_t *drive = floppy->drive;
1341 struct gendisk *g = floppy->disk;
1342
1343 drive->driver_data = NULL;
1344 g->private_data = NULL;
1345 put_disk(g);
1346 kfree(floppy);
1347}
1348
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001349#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001350static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1351 int count, int *eof, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 ide_drive_t*drive = (ide_drive_t *)data;
1354 int len;
1355
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001356 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1357 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360static ide_proc_entry_t idefloppy_proc[] = {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001361 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1362 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 { NULL, 0, NULL, NULL }
1364};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001365#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Russell King4031bbe2006-01-06 11:41:00 +00001367static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001370 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001371 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001372 .name = "ide-floppy",
1373 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001374 },
Russell King4031bbe2006-01-06 11:41:00 +00001375 .probe = ide_floppy_probe,
1376 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 .version = IDEFLOPPY_VERSION,
1378 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 .do_request = idefloppy_do_request,
1381 .end_request = idefloppy_do_end_request,
1382 .error = __ide_error,
1383 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001384#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001386#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387};
1388
1389static int idefloppy_open(struct inode *inode, struct file *filp)
1390{
1391 struct gendisk *disk = inode->i_bdev->bd_disk;
1392 struct ide_floppy_obj *floppy;
1393 ide_drive_t *drive;
1394 idefloppy_pc_t pc;
1395 int ret = 0;
1396
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001397 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001399 floppy = ide_floppy_get(disk);
1400 if (!floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return -ENXIO;
1402
1403 drive = floppy->drive;
1404
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001405 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001407 if (floppy->openers == 1) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001408 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* Just in case */
1410
1411 idefloppy_create_test_unit_ready_cmd(&pc);
1412 if (idefloppy_queue_pc_tail(drive, &pc)) {
1413 idefloppy_create_start_stop_cmd(&pc, 1);
1414 (void) idefloppy_queue_pc_tail(drive, &pc);
1415 }
1416
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001417 if (ide_floppy_get_capacity(drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 && (filp->f_flags & O_NDELAY) == 0
1419 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001420 * Allow O_NDELAY to open a drive without a disk, or with an
1421 * unreadable disk, so that we can get the format capacity
1422 * of the drive or begin the format - Sam
1423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 ret = -EIO;
1426 goto out_put_floppy;
1427 }
1428
1429 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 ret = -EROFS;
1431 goto out_put_floppy;
1432 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001433 floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001435 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 idefloppy_create_prevent_cmd(&pc, 1);
1437 (void) idefloppy_queue_pc_tail(drive, &pc);
1438 }
1439 check_disk_change(inode->i_bdev);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001440 } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 ret = -EBUSY;
1442 goto out_put_floppy;
1443 }
1444 return 0;
1445
1446out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001447 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ide_floppy_put(floppy);
1449 return ret;
1450}
1451
1452static int idefloppy_release(struct inode *inode, struct file *filp)
1453{
1454 struct gendisk *disk = inode->i_bdev->bd_disk;
1455 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1456 ide_drive_t *drive = floppy->drive;
1457 idefloppy_pc_t pc;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001458
1459 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001461 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001463 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 idefloppy_create_prevent_cmd(&pc, 0);
1465 (void) idefloppy_queue_pc_tail(drive, &pc);
1466 }
1467
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001468 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001470
1471 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 ide_floppy_put(floppy);
1474
1475 return 0;
1476}
1477
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001478static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1479{
1480 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1481 ide_drive_t *drive = floppy->drive;
1482
1483 geo->heads = drive->bios_head;
1484 geo->sectors = drive->bios_sect;
1485 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1486 return 0;
1487}
1488
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001489static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
1490 unsigned long arg, unsigned int cmd)
1491{
1492 if (floppy->openers > 1)
1493 return -EBUSY;
1494
1495 /* The IOMEGA Clik! Drive doesn't support this command -
1496 * no room for an eject mechanism */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001497 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001498 int prevent = arg ? 1 : 0;
1499
1500 if (cmd == CDROMEJECT)
1501 prevent = 0;
1502
1503 idefloppy_create_prevent_cmd(pc, prevent);
1504 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1505 }
1506
1507 if (cmd == CDROMEJECT) {
1508 idefloppy_create_start_stop_cmd(pc, 2);
1509 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1510 }
1511
1512 return 0;
1513}
1514
1515static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1516 int __user *arg)
1517{
1518 int blocks, length, flags, err = 0;
1519 idefloppy_pc_t pc;
1520
1521 if (floppy->openers > 1) {
1522 /* Don't format if someone is using the disk */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001523 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001524 return -EBUSY;
1525 }
1526
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001527 floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001528
1529 /*
1530 * Send ATAPI_FORMAT_UNIT to the drive.
1531 *
1532 * Userland gives us the following structure:
1533 *
1534 * struct idefloppy_format_command {
1535 * int nblocks;
1536 * int blocksize;
1537 * int flags;
1538 * } ;
1539 *
1540 * flags is a bitmask, currently, the only defined flag is:
1541 *
1542 * 0x01 - verify media after format.
1543 */
1544 if (get_user(blocks, arg) ||
1545 get_user(length, arg+1) ||
1546 get_user(flags, arg+2)) {
1547 err = -EFAULT;
1548 goto out;
1549 }
1550
1551 (void) idefloppy_get_sfrp_bit(floppy->drive);
1552 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1553
1554 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1555 err = -EIO;
1556
1557out:
1558 if (err)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001559 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001560 return err;
1561}
1562
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564static int idefloppy_ioctl(struct inode *inode, struct file *file,
1565 unsigned int cmd, unsigned long arg)
1566{
1567 struct block_device *bdev = inode->i_bdev;
1568 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1569 ide_drive_t *drive = floppy->drive;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001570 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001572 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 switch (cmd) {
1575 case CDROMEJECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /* fall through */
1577 case CDROM_LOCKDOOR:
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001578 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1580 return 0;
1581 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001582 return ide_floppy_get_format_capacities(drive, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 case IDEFLOPPY_IOCTL_FORMAT_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (!(file->f_mode & 2))
1585 return -EPERM;
1586
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001587 return ide_floppy_format_unit(floppy, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1589 return idefloppy_get_format_progress(drive, argp);
1590 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001591
1592 /*
1593 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1594 * and CDROM_SEND_PACKET (legacy) ioctls
1595 */
1596 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1597 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1598 bdev->bd_disk, cmd, argp);
1599 else
1600 err = -ENOTTY;
1601
1602 if (err == -ENOTTY)
1603 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1604
1605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608static int idefloppy_media_changed(struct gendisk *disk)
1609{
1610 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1611 ide_drive_t *drive = floppy->drive;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001612 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /* do not scan partitions twice if this is a removable device */
1615 if (drive->attach) {
1616 drive->attach = 0;
1617 return 0;
1618 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001619 ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
1620 floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
1621 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
1624static int idefloppy_revalidate_disk(struct gendisk *disk)
1625{
1626 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1627 set_capacity(disk, idefloppy_capacity(floppy->drive));
1628 return 0;
1629}
1630
1631static struct block_device_operations idefloppy_ops = {
1632 .owner = THIS_MODULE,
1633 .open = idefloppy_open,
1634 .release = idefloppy_release,
1635 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001636 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 .media_changed = idefloppy_media_changed,
1638 .revalidate_disk= idefloppy_revalidate_disk
1639};
1640
Russell King4031bbe2006-01-06 11:41:00 +00001641static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 idefloppy_floppy_t *floppy;
1644 struct gendisk *g;
1645
1646 if (!strstr("ide-floppy", drive->driver_req))
1647 goto failed;
1648 if (!drive->present)
1649 goto failed;
1650 if (drive->media != ide_floppy)
1651 goto failed;
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001652 if (!idefloppy_identify_device(drive, drive->id)) {
1653 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1654 " of ide-floppy\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto failed;
1656 }
1657 if (drive->scsi) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001658 printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi"
1659 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto failed;
1661 }
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001662 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1663 if (!floppy) {
1664 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1665 " structure\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto failed;
1667 }
1668
1669 g = alloc_disk(1 << PARTN_BITS);
1670 if (!g)
1671 goto out_free_floppy;
1672
1673 ide_init_disk(g, drive);
1674
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001675 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 kref_init(&floppy->kref);
1678
1679 floppy->drive = drive;
1680 floppy->driver = &idefloppy_driver;
1681 floppy->disk = g;
1682
1683 g->private_data = &floppy->driver;
1684
1685 drive->driver_data = floppy;
1686
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001687 idefloppy_setup(drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 g->minors = 1 << PARTN_BITS;
1690 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1692 g->fops = &idefloppy_ops;
1693 drive->attach = 1;
1694 add_disk(g);
1695 return 0;
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697out_free_floppy:
1698 kfree(floppy);
1699failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001700 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001703static void __exit idefloppy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001705 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001708static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001711 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Kay Sievers263756e2005-12-12 18:03:44 +01001714MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715module_init(idefloppy_init);
1716module_exit(idefloppy_exit);
1717MODULE_LICENSE("GPL");
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001718MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1719