blob: faf22d716f805995e6e59351c77e740b514f4fce [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 Petkov0571c7a2008-02-02 19:56:38 +0100198 * The following is used to format the general configuration word of the
199 * ATAPI IDENTIFY DEVICE command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100201struct idefloppy_id_gcw {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#if defined(__LITTLE_ENDIAN_BITFIELD)
203 unsigned packet_size :2; /* Packet Size */
204 unsigned reserved234 :3; /* Reserved */
205 unsigned drq_type :2; /* Command packet DRQ type */
206 unsigned removable :1; /* Removable media */
207 unsigned device_type :5; /* Device type */
208 unsigned reserved13 :1; /* Reserved */
209 unsigned protocol :2; /* Protocol type */
210#elif defined(__BIG_ENDIAN_BITFIELD)
211 unsigned protocol :2; /* Protocol type */
212 unsigned reserved13 :1; /* Reserved */
213 unsigned device_type :5; /* Device type */
214 unsigned removable :1; /* Removable media */
215 unsigned drq_type :2; /* Command packet DRQ type */
216 unsigned reserved234 :3; /* Reserved */
217 unsigned packet_size :2; /* Packet Size */
218#else
219#error "Bitfield endianness not defined! Check your byteorder.h"
220#endif
221};
222
223/*
Borislav Petkov948391d2008-02-02 19:56:34 +0100224 * Pages of the SELECT SENSE / MODE SENSE packet commands.
225 * See SFF-8070i spec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
227#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
228#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
229
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800230static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
233
234#define ide_floppy_g(disk) \
235 container_of((disk)->private_data, struct ide_floppy_obj, driver)
236
237static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
238{
239 struct ide_floppy_obj *floppy = NULL;
240
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800241 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 floppy = ide_floppy_g(disk);
243 if (floppy)
244 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800245 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return floppy;
247}
248
Borislav Petkov9a24b632008-02-02 19:56:33 +0100249static void idefloppy_cleanup_obj(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251static void ide_floppy_put(struct ide_floppy_obj *floppy)
252{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800253 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100254 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800255 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100259 * Too bad. The drive wants to send us data which we are not ready to accept.
260 * Just throw it away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100262static void idefloppy_discard_data(ide_drive_t *drive, unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 while (bcount--)
265 (void) HWIF(drive)->INB(IDE_DATA_REG);
266}
267
Borislav Petkov0bf399e2008-02-02 19:56:36 +0100268static void idefloppy_write_zeros(ide_drive_t *drive, unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 while (bcount--)
271 HWIF(drive)->OUTB(0, IDE_DATA_REG);
272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274
275/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100276 * Used to finish servicing a request. For read/write requests, we will call
277 * ide_end_request to pass to the next buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
280{
281 idefloppy_floppy_t *floppy = drive->driver_data;
282 struct request *rq = HWGROUP(drive)->rq;
283 int error;
284
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100285 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 switch (uptodate) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100288 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
289 case 1: error = 0; break;
290 default: error = uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 if (error)
293 floppy->failed_pc = NULL;
294 /* Why does this happen? */
295 if (!rq)
296 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200297 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* our real local end request function */
299 ide_end_request(drive, uptodate, nsecs);
300 return 0;
301 }
302 rq->errors = error;
303 /* fixme: need to move this local also */
304 ide_end_drive_cmd(drive, 0, 0);
305 return 0;
306}
307
Borislav Petkov32925e12008-02-02 19:56:36 +0100308static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc,
309 unsigned int bcount, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200312 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct bio_vec *bvec;
314 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200315 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 char *data;
317
NeilBrown5705f702007-09-25 12:35:59 +0200318 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200319 if (!bcount)
320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jens Axboe6c92e692007-08-16 13:43:12 +0200322 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Jens Axboe6c92e692007-08-16 13:43:12 +0200324 data = bvec_kmap_irq(bvec, &flags);
Borislav Petkov32925e12008-02-02 19:56:36 +0100325 if (direction)
326 drive->hwif->atapi_output_bytes(drive, data, count);
327 else
328 drive->hwif->atapi_input_bytes(drive, data, count);
Jens Axboe6c92e692007-08-16 13:43:12 +0200329 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Jens Axboe6c92e692007-08-16 13:43:12 +0200331 bcount -= count;
332 pc->b_count += count;
333 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 idefloppy_do_end_request(drive, 1, done >> 9);
337
338 if (bcount) {
Borislav Petkov32925e12008-02-02 19:56:36 +0100339 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
340 drive->name, __func__, bcount);
341 if (direction)
342 idefloppy_write_zeros(drive, bcount);
343 else
344 idefloppy_discard_data(drive, bcount);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347}
348
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100349static void idefloppy_update_buffers(ide_drive_t *drive, idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct request *rq = pc->rq;
352 struct bio *bio = rq->bio;
353
354 while ((bio = rq->bio) != NULL)
355 idefloppy_do_end_request(drive, 1, 0);
356}
357
358/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100359 * Generate a new packet command request in front of the request queue, before
360 * the current request so that it will be processed immediately, on the next
361 * pass through the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100363static void idefloppy_queue_pc_head(ide_drive_t *drive, idefloppy_pc_t *pc,
364 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct ide_floppy_obj *floppy = drive->driver_data;
367
368 ide_init_drive_cmd(rq);
369 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200370 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 rq->rq_disk = floppy->disk;
372 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
373}
374
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100375static idefloppy_pc_t *idefloppy_next_pc_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 idefloppy_floppy_t *floppy = drive->driver_data;
378
379 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100380 floppy->pc_stack_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return (&floppy->pc_stack[floppy->pc_stack_index++]);
382}
383
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100384static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 idefloppy_floppy_t *floppy = drive->driver_data;
387
388 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
389 floppy->rq_stack_index = 0;
390 return (&floppy->rq_stack[floppy->rq_stack_index++]);
391}
392
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100393static void idefloppy_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100396 u8 *buf = floppy->pc->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100398 debug_log("Reached %s\n", __func__);
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!floppy->pc->error) {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100401 floppy->sense_key = buf[2] & 0x0F;
402 floppy->asc = buf[12];
403 floppy->ascq = buf[13];
404 floppy->progress_indication = buf[15] & 0x80 ?
405 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
406
407 if (floppy->failed_pc)
408 debug_log("pc = %x, sense key = %x, asc = %x,"
409 " ascq = %x\n",
410 floppy->failed_pc->c[0],
411 floppy->sense_key,
412 floppy->asc,
413 floppy->ascq);
414 else
415 debug_log("sense key = %x, asc = %x, ascq = %x\n",
416 floppy->sense_key,
417 floppy->asc,
418 floppy->ascq);
419
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 idefloppy_do_end_request(drive, 1, 0);
422 } else {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100423 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
424 " request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 idefloppy_do_end_request(drive, 0, 0);
426 }
427}
428
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100429/* General packet command callback function. */
430static void idefloppy_pc_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100433
434 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
437}
438
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100439static void idefloppy_init_pc(idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 memset(pc->c, 0, 12);
442 pc->retries = 0;
443 pc->flags = 0;
444 pc->request_transfer = 0;
445 pc->buffer = pc->pc_buffer;
446 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
447 pc->callback = &idefloppy_pc_callback;
448}
449
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100450static void idefloppy_create_request_sense_cmd(idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Borislav Petkov30d67092008-02-02 19:56:33 +0100452 idefloppy_init_pc(pc);
453 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 pc->c[4] = 255;
455 pc->request_transfer = 18;
456 pc->callback = &idefloppy_request_sense_callback;
457}
458
459/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100460 * Called when an error was detected during the last packet command. We queue a
461 * request sense packet command in the head of the request list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100463static void idefloppy_retry_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 idefloppy_pc_t *pc;
466 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100468 (void)ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 pc = idefloppy_next_pc_storage(drive);
470 rq = idefloppy_next_rq_storage(drive);
471 idefloppy_create_request_sense_cmd(pc);
472 idefloppy_queue_pc_head(drive, pc, rq);
473}
474
Borislav Petkov0eea6452008-02-02 19:56:36 +0100475/* The usual interrupt handler called during a packet command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
477{
478 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100479 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 idefloppy_pc_t *pc = floppy->pc;
481 struct request *rq = pc->rq;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100482 xfer_func_t *xferfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned int temp;
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100484 int dma_error = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100485 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100486 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100488 debug_log("Reached %s interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100490 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100491 dma_error = hwif->ide_dma_end(drive);
492 if (dma_error) {
493 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
494 rq_data_dir(rq) ? "write" : "read");
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100495 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else {
497 pc->actually_transferred = pc->request_transfer;
498 idefloppy_update_buffers(drive, pc);
499 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100500 debug_log("DMA finished\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100504 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100506 /* No more interrupts */
507 if ((stat & DRQ_STAT) == 0) {
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100508 debug_log("Packet command completed, %d bytes transferred\n",
509 pc->actually_transferred);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100510 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Ingo Molnar366c7f52006-07-03 00:25:25 -0700512 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100514 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* Error detected */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100516 debug_log("%s: I/O error\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 rq->errors++;
Borislav Petkov30d67092008-02-02 19:56:33 +0100518 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 printk(KERN_ERR "ide-floppy: I/O error in "
520 "request sense command\n");
521 return ide_do_reset(drive);
522 }
523 /* Retry operation */
524 idefloppy_retry_pc(drive);
525 /* queued, but not started */
526 return ide_stopped;
527 }
528 pc->error = 0;
529 if (floppy->failed_pc == pc)
530 floppy->failed_pc = NULL;
531 /* Command finished - Call the callback function */
532 pc->callback(drive);
533 return ide_stopped;
534 }
535
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100536 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
537 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
539 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100540 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return ide_do_reset(drive);
542 }
543
544 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100545 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
546 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* on this interrupt */
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100548 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100550 if (ireason & CD) {
Borislav Petkov0eea6452008-02-02 19:56:36 +0100551 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return ide_do_reset(drive);
553 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100554 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Hopefully, we will never get here */
556 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100557 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 printk(KERN_ERR "but the floppy wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100559 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return ide_do_reset(drive);
561 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100562 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100564 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (temp > pc->request_transfer) {
566 if (temp > pc->buffer_size) {
567 printk(KERN_ERR "ide-floppy: The floppy wants "
568 "to send us more data than expected "
569 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100570 idefloppy_discard_data(drive, bcount);
Borislav Petkov0078df22008-02-02 19:56:33 +0100571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 ide_set_handler(drive,
573 &idefloppy_pc_intr,
574 IDEFLOPPY_WAIT_CMD,
575 NULL);
576 return ide_started;
577 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100578 debug_log("The floppy wants to send us more data than"
579 " expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100582 if (pc->flags & PC_FLAG_WRITING)
Borislav Petkov0eea6452008-02-02 19:56:36 +0100583 xferfunc = hwif->atapi_output_bytes;
Borislav Petkov32925e12008-02-02 19:56:36 +0100584 else
Borislav Petkov0eea6452008-02-02 19:56:36 +0100585 xferfunc = hwif->atapi_input_bytes;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100586
587 if (pc->buffer)
588 xferfunc(drive, pc->current_position, bcount);
589 else
Borislav Petkov32925e12008-02-02 19:56:36 +0100590 ide_floppy_io_buffers(drive, pc, bcount,
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100591 !!(pc->flags & PC_FLAG_WRITING));
Borislav Petkov0eea6452008-02-02 19:56:36 +0100592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100594 pc->actually_transferred += bcount;
595 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Borislav Petkov32925e12008-02-02 19:56:36 +0100597 /* And set the interrupt handler again */
598 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return ide_started;
600}
601
602/*
603 * This is the original routine that did the packet transfer.
604 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
605 * for that drive below. The algorithm is chosen based on drive type
606 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100607static ide_startstop_t idefloppy_transfer_pc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 ide_startstop_t startstop;
610 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100611 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
614 printk(KERN_ERR "ide-floppy: Strange, packet command "
615 "initiated yet DRQ isn't asserted\n");
616 return startstop;
617 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100618 ireason = drive->hwif->INB(IDE_IREASON_REG);
619 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
621 "issuing a packet command\n");
622 return ide_do_reset(drive);
623 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Set the interrupt routine */
626 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
627 /* Send the actual packet */
628 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
629 return ide_started;
630}
631
632
633/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100634 * What we have here is a classic case of a top half / bottom half interrupt
635 * service routine. In interrupt mode, the device sends an interrupt to signal
636 * that it is ready to receive a packet. However, we need to delay about 2-3
637 * ticks before issuing the packet or we gets in trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 *
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100639 * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
640 * In either case, when the device says it's ready for a packet, we schedule
641 * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100643static int idefloppy_transfer_pc2(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 idefloppy_floppy_t *floppy = drive->driver_data;
646
647 /* Send the actual packet */
648 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
649 /* Timeout for the packet command */
650 return IDEFLOPPY_WAIT_CMD;
651}
652
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100653static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 idefloppy_floppy_t *floppy = drive->driver_data;
656 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100657 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
660 printk(KERN_ERR "ide-floppy: Strange, packet command "
661 "initiated yet DRQ isn't asserted\n");
662 return startstop;
663 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100664 ireason = drive->hwif->INB(IDE_IREASON_REG);
665 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
667 "while issuing a packet command\n");
668 return ide_do_reset(drive);
669 }
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100670 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * The following delay solves a problem with ATAPI Zip 100 drives
672 * where the Busy flag was apparently being deasserted before the
673 * unit was ready to receive data. This was happening on a
674 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
675 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
676 * used until after the packet is moved in about 50 msec.
677 */
Borislav Petkov0078df22008-02-02 19:56:33 +0100678
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100679 ide_set_handler(drive, &idefloppy_pc_intr, floppy->ticks,
680 &idefloppy_transfer_pc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return ide_started;
682}
683
Borislav Petkovd652c132008-02-02 19:56:35 +0100684static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
685 idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Borislav Petkovd652c132008-02-02 19:56:35 +0100687 /* supress error messages resulting from Medium not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (floppy->sense_key == 0x02 &&
689 floppy->asc == 0x3a &&
690 floppy->ascq == 0x00)
Borislav Petkovd652c132008-02-02 19:56:35 +0100691 return;
692
693 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
694 "asc = %2x, ascq = %2x\n",
695 floppy->drive->name, pc->c[0], floppy->sense_key,
696 floppy->asc, floppy->ascq);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100700static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
701 idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 idefloppy_floppy_t *floppy = drive->driver_data;
704 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100706 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100707 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100710 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 floppy->failed_pc = pc;
712 /* Set the current packet command */
713 floppy->pc = pc;
714
Borislav Petkov757ced82008-02-02 19:56:37 +0100715 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100716 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
Borislav Petkov757ced82008-02-02 19:56:37 +0100717 ide_floppy_report_error(floppy, pc);
718 /* Giving up */
719 pc->error = IDEFLOPPY_ERROR_GENERAL;
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 floppy->failed_pc = NULL;
722 pc->callback(drive);
723 return ide_stopped;
724 }
725
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100726 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 pc->retries++;
729 /* We haven't transferred any data yet */
730 pc->actually_transferred = 0;
731 pc->current_position = pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100732 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100734 if (pc->flags & PC_FLAG_DMA_ERROR) {
735 pc->flags &= ~PC_FLAG_DMA_ERROR;
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100736 ide_dma_off(drive);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100737 }
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100738 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100740 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100741 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100743 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
744 IDE_TFLAG_OUT_DEVICE, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100746 if (dma) {
747 /* Begin DMA, if necessary */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100748 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 hwif->dma_start(drive);
750 }
751
752 /* Can we transfer the packet when we get the interrupt or wait? */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100753 if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* wait */
755 pkt_xfer_routine = &idefloppy_transfer_pc1;
756 } else {
757 /* immediate */
758 pkt_xfer_routine = &idefloppy_transfer_pc;
759 }
760
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100761 if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* Issue the packet command */
763 ide_execute_command(drive, WIN_PACKETCMD,
764 pkt_xfer_routine,
765 IDEFLOPPY_WAIT_CMD,
766 NULL);
767 return ide_started;
768 } else {
769 /* Issue the packet command */
770 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
771 return (*pkt_xfer_routine) (drive);
772 }
773}
774
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100775static void idefloppy_rw_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100777 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 idefloppy_do_end_request(drive, 1, 0);
780 return;
781}
782
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100783static void idefloppy_create_prevent_cmd(idefloppy_pc_t *pc, int prevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100785 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100788 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 pc->c[4] = prevent;
790}
791
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100792static void idefloppy_create_read_capacity_cmd(idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100795 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 pc->c[7] = 255;
797 pc->c[8] = 255;
798 pc->request_transfer = 255;
799}
800
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100801static void idefloppy_create_format_unit_cmd(idefloppy_pc_t *pc, int b, int l,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int flags)
803{
804 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100805 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 pc->c[1] = 0x17;
807
808 memset(pc->buffer, 0, 12);
809 pc->buffer[1] = 0xA2;
810 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
811
812 if (flags & 1) /* Verify bit on... */
813 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
814 pc->buffer[3] = 8;
815
Borislav Petkov8f622432008-02-02 19:56:33 +0100816 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
817 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100818 pc->buffer_size = 12;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100819 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100822/* A mode sense command is used to "sense" floppy parameters. */
823static void idefloppy_create_mode_sense_cmd(idefloppy_pc_t *pc, u8 page_code,
824 u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Borislav Petkov24a5d702008-02-02 19:56:34 +0100826 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100829 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 pc->c[1] = 0;
831 pc->c[2] = page_code + (type << 6);
832
833 switch (page_code) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100834 case IDEFLOPPY_CAPABILITIES_PAGE:
835 length += 12;
836 break;
837 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
838 length += 32;
839 break;
840 default:
841 printk(KERN_ERR "ide-floppy: unsupported page code "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 "in create_mode_sense_cmd\n");
843 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100844 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 pc->request_transfer = length;
846}
847
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100848static void idefloppy_create_start_stop_cmd(idefloppy_pc_t *pc, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100851 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 pc->c[4] = start;
853}
854
855static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
856{
857 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100858 pc->c[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100861static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
862 idefloppy_pc_t *pc, struct request *rq,
863 unsigned long sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 int block = sector / floppy->bs_factor;
866 int blocks = rq->nr_sectors / floppy->bs_factor;
867 int cmd = rq_data_dir(rq);
868
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100869 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 block, blocks);
871
872 idefloppy_init_pc(pc);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100873 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
874 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Borislav Petkov8f622432008-02-02 19:56:33 +0100875 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 pc->callback = &idefloppy_rw_callback;
878 pc->rq = rq;
879 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200880 if (rq->cmd_flags & REQ_RW)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100881 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 pc->buffer = NULL;
883 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100884 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100887static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
888 idefloppy_pc_t *pc, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 idefloppy_init_pc(pc);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200891 pc->callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200893 pc->rq = rq;
894 pc->b_count = rq->data_len;
895 if (rq->data_len && rq_data_dir(rq) == WRITE)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100896 pc->flags |= PC_FLAG_WRITING;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200897 pc->buffer = rq->data;
898 if (rq->bio)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100899 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200900 /*
901 * possibly problematic, doesn't look like ide-floppy correctly
902 * handled scattered requests if dma fails...
903 */
904 pc->request_transfer = pc->buffer_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100907static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
908 struct request *rq, sector_t block_s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 idefloppy_floppy_t *floppy = drive->driver_data;
911 idefloppy_pc_t *pc;
912 unsigned long block = (unsigned long)block_s;
913
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100914 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -0700915 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100916 rq->cmd_type, rq->errors);
917 debug_log("sector: %ld, nr_sectors: %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 "current_nr_sectors: %d\n", (long)rq->sector,
919 rq->nr_sectors, rq->current_nr_sectors);
920
921 if (rq->errors >= ERROR_MAX) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100922 if (floppy->failed_pc)
923 ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 else
925 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
926 drive->name);
927 idefloppy_do_end_request(drive, 0, 0);
928 return ide_stopped;
929 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200930 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (((long)rq->sector % floppy->bs_factor) ||
932 (rq->nr_sectors % floppy->bs_factor)) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100933 printk(KERN_ERR "%s: unsupported r/w request size\n",
934 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 idefloppy_do_end_request(drive, 0, 0);
936 return ide_stopped;
937 }
938 pc = idefloppy_next_pc_storage(drive);
939 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200940 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 pc = (idefloppy_pc_t *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200942 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200944 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 } else {
946 blk_dump_rq_flags(rq,
947 "ide-floppy: unsupported command in queue");
948 idefloppy_do_end_request(drive, 0, 0);
949 return ide_stopped;
950 }
951
952 pc->rq = rq;
953 return idefloppy_issue_pc(drive, pc);
954}
955
956/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100957 * Add a special packet command request to the tail of the request queue,
958 * and wait for it to be serviced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100960static int idefloppy_queue_pc_tail(ide_drive_t *drive, idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct ide_floppy_obj *floppy = drive->driver_data;
963 struct request rq;
964
Borislav Petkov0571c7a2008-02-02 19:56:38 +0100965 ide_init_drive_cmd(&rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200967 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 rq.rq_disk = floppy->disk;
969
970 return ide_do_drive_cmd(drive, &rq, ide_wait);
971}
972
973/*
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100974 * Look at the flexible disk page parameters. We ignore the CHS capacity
975 * parameters and use the LBA parameters instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100977static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 idefloppy_floppy_t *floppy = drive->driver_data;
980 idefloppy_pc_t pc;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100981 u8 *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int capacity, lba_capacity;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100983 u16 transfer_rate, sector_size, cyls, rpm;
984 u8 heads, sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100986 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
987 MODE_SENSE_CURRENT);
988
989 if (idefloppy_queue_pc_tail(drive, &pc)) {
990 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
991 " parameters\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 1;
993 }
Borislav Petkov24a5d702008-02-02 19:56:34 +0100994 floppy->wp = !!(pc.buffer[3] & 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 set_disk_ro(floppy->disk, floppy->wp);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100996 page = &pc.buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100998 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
999 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
1000 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
1001 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
1002 heads = pc.buffer[8 + 4];
1003 sectors = pc.buffer[8 + 5];
1004
1005 capacity = cyls * heads * sectors * sector_size;
1006
1007 if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1009 "%d sector size, %d rpm\n",
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001010 drive->name, capacity / 1024, cyls, heads,
1011 sectors, transfer_rate / 8, sector_size, rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001013 memcpy(&floppy->flexible_disk_page, page, 32);
1014 drive->bios_cyl = cyls;
1015 drive->bios_head = heads;
1016 drive->bios_sect = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 lba_capacity = floppy->blocks * floppy->block_size;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (capacity < lba_capacity) {
1020 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1021 "bytes, but the drive only handles %d\n",
1022 drive->name, lba_capacity, capacity);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001023 floppy->blocks = floppy->block_size ?
1024 capacity / floppy->block_size : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 return 0;
1027}
1028
Borislav Petkov948391d2008-02-02 19:56:34 +01001029static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 idefloppy_floppy_t *floppy = drive->driver_data;
1032 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 floppy->srfp = 0;
1035 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1036 MODE_SENSE_CURRENT);
1037
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001038 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
Borislav Petkov948391d2008-02-02 19:56:34 +01001039 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Borislav Petkov948391d2008-02-02 19:56:34 +01001042 floppy->srfp = pc.buffer[8 + 2] & 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return (0);
1044}
1045
1046/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001047 * Determine if a media is present in the floppy drive, and if so, its LBA
1048 * capacity.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001050static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 idefloppy_floppy_t *floppy = drive->driver_data;
1053 idefloppy_pc_t pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001054 u8 *cap_desc;
1055 u8 header_len, desc_cnt;
1056 int i, rc = 1, blocks, length;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 drive->bios_cyl = 0;
1059 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +01001060 floppy->blocks = 0;
1061 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 set_capacity(floppy->disk, 0);
1063
1064 idefloppy_create_read_capacity_cmd(&pc);
1065 if (idefloppy_queue_pc_tail(drive, &pc)) {
1066 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1067 return 1;
1068 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001069 header_len = pc.buffer[3];
1070 cap_desc = &pc.buffer[4];
1071 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001073 for (i = 0; i < desc_cnt; i++) {
1074 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001076 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1077 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1078
1079 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1080 i, blocks * length / 1024, blocks, length);
1081
1082 if (i)
1083 continue;
1084 /*
1085 * the code below is valid only for the 1st descriptor, ie i=0
1086 */
1087
1088 switch (pc.buffer[desc_start + 4] & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1090 case CAPACITY_UNFORMATTED:
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001091 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001092 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * If it is not a clik drive, break out
1094 * (maintains previous driver behaviour)
1095 */
1096 break;
1097 case CAPACITY_CURRENT:
1098 /* Normal Zip/LS-120 disks */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001099 if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1101 "sector size\n", drive->name,
1102 blocks * length / 1024, blocks, length);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001103 memcpy(&floppy->cap_desc, cap_desc, 8);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (!length || length % 512) {
1106 printk(KERN_NOTICE "%s: %d bytes block size "
1107 "not supported\n", drive->name, length);
1108 } else {
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001109 floppy->blocks = blocks;
1110 floppy->block_size = length;
1111 floppy->bs_factor = length / 512;
1112 if (floppy->bs_factor != 1)
1113 printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 "512 bytes block size not "
1115 "fully supported\n",
1116 drive->name);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001117 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 break;
1120 case CAPACITY_NO_CARTRIDGE:
1121 /*
1122 * This is a KERN_ERR so it appears on screen
1123 * for the user to see
1124 */
1125 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1126 break;
1127 case CAPACITY_INVALID:
1128 printk(KERN_ERR "%s: Invalid capacity for disk "
1129 "in drive\n", drive->name);
1130 break;
1131 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001132 debug_log("Descriptor 0 Code: %d\n",
1133 pc.buffer[desc_start + 4] & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 /* Clik! disk does not support get_flexible_disk_page */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001137 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001138 (void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1141 return rc;
1142}
1143
1144/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001145 * Obtain the list of formattable capacities.
1146 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1147 * descriptors to userland, instead of our own structures.
1148 *
1149 * Userland gives us the following structure:
1150 *
1151 * struct idefloppy_format_capacities {
1152 * int nformats;
1153 * struct {
1154 * int nblocks;
1155 * int blocksize;
1156 * } formats[];
1157 * };
1158 *
1159 * userland initializes nformats to the number of allocated formats[] records.
1160 * On exit we set nformats to the number of records we've actually initialized.
1161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001163static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001165 idefloppy_pc_t pc;
1166 u8 header_len, desc_cnt;
1167 int i, blocks, length, u_array_size, u_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int __user *argp;
1169
1170 if (get_user(u_array_size, arg))
1171 return (-EFAULT);
1172
1173 if (u_array_size <= 0)
1174 return (-EINVAL);
1175
1176 idefloppy_create_read_capacity_cmd(&pc);
1177 if (idefloppy_queue_pc_tail(drive, &pc)) {
1178 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001179 return (-EIO);
1180 }
1181 header_len = pc.buffer[3];
1182 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 u_index = 0;
1185 argp = arg + 1;
1186
1187 /*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001188 * We always skip the first capacity descriptor. That's the current
1189 * capacity. We are interested in the remaining descriptors, the
1190 * formattable capacities.
1191 */
1192 for (i = 1; i < desc_cnt; i++) {
1193 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (u_index >= u_array_size)
1196 break; /* User-supplied buffer too small */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001198 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1199 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (put_user(blocks, argp))
1202 return(-EFAULT);
1203 ++argp;
1204
1205 if (put_user(length, argp))
1206 return (-EFAULT);
1207 ++argp;
1208
1209 ++u_index;
1210 }
1211
1212 if (put_user(u_index, arg))
1213 return (-EFAULT);
1214 return (0);
1215}
1216
1217/*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001218 * Get ATAPI_FORMAT_UNIT progress indication.
1219 *
1220 * Userland gives a pointer to an int. The int is set to a progress
1221 * indicator 0-65536, with 65536=100%.
1222 *
1223 * If the drive does not support format progress indication, we just check
1224 * the dsc bit, and return either 0 or 65536.
1225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1228{
1229 idefloppy_floppy_t *floppy = drive->driver_data;
1230 idefloppy_pc_t pc;
1231 int progress_indication = 0x10000;
1232
1233 if (floppy->srfp) {
1234 idefloppy_create_request_sense_cmd(&pc);
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001235 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return (-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (floppy->sense_key == 2 &&
1239 floppy->asc == 4 &&
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001240 floppy->ascq == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 progress_indication = floppy->progress_indication;
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001242
1243 /* Else assume format_unit has finished, and we're at 0x10000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001246 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 local_irq_save(flags);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001249 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 local_irq_restore(flags);
1251
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001252 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 if (put_user(progress_indication, arg))
1255 return (-EFAULT);
1256
1257 return (0);
1258}
1259
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001260static sector_t idefloppy_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 idefloppy_floppy_t *floppy = drive->driver_data;
1263 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1264
1265 return capacity;
1266}
1267
1268/*
Borislav Petkovf373bd82008-02-02 19:56:36 +01001269 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1270 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
Borislav Petkovf373bd82008-02-02 19:56:36 +01001272static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 struct idefloppy_id_gcw gcw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 *((u16 *) &gcw) = id->config;
1277
1278#ifdef CONFIG_PPC
1279 /* kludge for Apple PowerBook internal zip */
1280 if ((gcw.device_type == 5) &&
1281 !strstr(id->model, "CD-ROM") &&
1282 strstr(id->model, "ZIP"))
Borislav Petkovf373bd82008-02-02 19:56:36 +01001283 gcw.device_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284#endif
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (gcw.protocol != 2)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001287 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
1288 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 else if (gcw.device_type != 0)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001290 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
1291 "to floppy\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 else if (!gcw.removable)
1293 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1294 else if (gcw.drq_type == 3) {
Borislav Petkovf373bd82008-02-02 19:56:36 +01001295 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
1296 "supported\n", gcw.drq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 } else if (gcw.packet_size != 0) {
Borislav Petkovf373bd82008-02-02 19:56:36 +01001298 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
1299 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 } else
1301 return 1;
1302 return 0;
1303}
1304
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001305#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306static void idefloppy_add_settings(ide_drive_t *drive)
1307{
1308 idefloppy_floppy_t *floppy = drive->driver_data;
1309
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001310 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
1311 &drive->bios_cyl, NULL);
1312 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1313 &drive->bios_head, NULL);
1314 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
1315 &drive->bios_sect, NULL);
1316 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1317 &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001319#else
1320static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001323static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct idefloppy_id_gcw gcw;
1326
1327 *((u16 *) &gcw) = drive->id->config;
1328 floppy->pc = floppy->pc_stack;
1329 if (gcw.drq_type == 1)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001330 floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001332 * We used to check revisions here. At this point however I'm giving up.
1333 * Just assume they are all broken, its easier.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 *
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001335 * The actual reason for the workarounds was likely a driver bug after
1336 * all rather than a firmware bug, and the workaround below used to hide
1337 * it. It should be fixed as of version 1.9, but to be on the safe side
1338 * we'll leave the limitation below for the 2.2.x tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001341 floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /* This value will be visible in the /proc/ide/hdx/settings */
1343 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1344 blk_queue_max_sectors(drive->queue, 64);
1345 }
1346
1347 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001348 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1349 * nasty clicking noises without it, so please don't remove this.
1350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1352 blk_queue_max_sectors(drive->queue, 64);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001353 floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001356 (void) ide_floppy_get_capacity(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 idefloppy_add_settings(drive);
1358}
1359
Russell King4031bbe2006-01-06 11:41:00 +00001360static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 idefloppy_floppy_t *floppy = drive->driver_data;
1363 struct gendisk *g = floppy->disk;
1364
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001365 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 del_gendisk(g);
1368
1369 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Borislav Petkov9a24b632008-02-02 19:56:33 +01001372static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1375 ide_drive_t *drive = floppy->drive;
1376 struct gendisk *g = floppy->disk;
1377
1378 drive->driver_data = NULL;
1379 g->private_data = NULL;
1380 put_disk(g);
1381 kfree(floppy);
1382}
1383
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001384#ifdef CONFIG_IDE_PROC_FS
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001385static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1386 int count, int *eof, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 ide_drive_t*drive = (ide_drive_t *)data;
1389 int len;
1390
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001391 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1392 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395static ide_proc_entry_t idefloppy_proc[] = {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001396 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1397 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 { NULL, 0, NULL, NULL }
1399};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001400#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Russell King4031bbe2006-01-06 11:41:00 +00001402static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001405 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001406 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001407 .name = "ide-floppy",
1408 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001409 },
Russell King4031bbe2006-01-06 11:41:00 +00001410 .probe = ide_floppy_probe,
1411 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 .version = IDEFLOPPY_VERSION,
1413 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 .do_request = idefloppy_do_request,
1416 .end_request = idefloppy_do_end_request,
1417 .error = __ide_error,
1418 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001419#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001421#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422};
1423
1424static int idefloppy_open(struct inode *inode, struct file *filp)
1425{
1426 struct gendisk *disk = inode->i_bdev->bd_disk;
1427 struct ide_floppy_obj *floppy;
1428 ide_drive_t *drive;
1429 idefloppy_pc_t pc;
1430 int ret = 0;
1431
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001432 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001434 floppy = ide_floppy_get(disk);
1435 if (!floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -ENXIO;
1437
1438 drive = floppy->drive;
1439
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001440 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001442 if (floppy->openers == 1) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001443 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Just in case */
1445
1446 idefloppy_create_test_unit_ready_cmd(&pc);
1447 if (idefloppy_queue_pc_tail(drive, &pc)) {
1448 idefloppy_create_start_stop_cmd(&pc, 1);
1449 (void) idefloppy_queue_pc_tail(drive, &pc);
1450 }
1451
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001452 if (ide_floppy_get_capacity(drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 && (filp->f_flags & O_NDELAY) == 0
1454 /*
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001455 * Allow O_NDELAY to open a drive without a disk, or with an
1456 * unreadable disk, so that we can get the format capacity
1457 * of the drive or begin the format - Sam
1458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 ret = -EIO;
1461 goto out_put_floppy;
1462 }
1463
1464 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 ret = -EROFS;
1466 goto out_put_floppy;
1467 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001468 floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001470 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 idefloppy_create_prevent_cmd(&pc, 1);
1472 (void) idefloppy_queue_pc_tail(drive, &pc);
1473 }
1474 check_disk_change(inode->i_bdev);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001475 } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 ret = -EBUSY;
1477 goto out_put_floppy;
1478 }
1479 return 0;
1480
1481out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001482 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 ide_floppy_put(floppy);
1484 return ret;
1485}
1486
1487static int idefloppy_release(struct inode *inode, struct file *filp)
1488{
1489 struct gendisk *disk = inode->i_bdev->bd_disk;
1490 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1491 ide_drive_t *drive = floppy->drive;
1492 idefloppy_pc_t pc;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001493
1494 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001496 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001498 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 idefloppy_create_prevent_cmd(&pc, 0);
1500 (void) idefloppy_queue_pc_tail(drive, &pc);
1501 }
1502
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001503 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001505
1506 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 ide_floppy_put(floppy);
1509
1510 return 0;
1511}
1512
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001513static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1514{
1515 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1516 ide_drive_t *drive = floppy->drive;
1517
1518 geo->heads = drive->bios_head;
1519 geo->sectors = drive->bios_sect;
1520 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1521 return 0;
1522}
1523
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001524static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
1525 unsigned long arg, unsigned int cmd)
1526{
1527 if (floppy->openers > 1)
1528 return -EBUSY;
1529
1530 /* The IOMEGA Clik! Drive doesn't support this command -
1531 * no room for an eject mechanism */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001532 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001533 int prevent = arg ? 1 : 0;
1534
1535 if (cmd == CDROMEJECT)
1536 prevent = 0;
1537
1538 idefloppy_create_prevent_cmd(pc, prevent);
1539 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1540 }
1541
1542 if (cmd == CDROMEJECT) {
1543 idefloppy_create_start_stop_cmd(pc, 2);
1544 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1545 }
1546
1547 return 0;
1548}
1549
1550static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1551 int __user *arg)
1552{
1553 int blocks, length, flags, err = 0;
1554 idefloppy_pc_t pc;
1555
1556 if (floppy->openers > 1) {
1557 /* Don't format if someone is using the disk */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001558 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001559 return -EBUSY;
1560 }
1561
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001562 floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001563
1564 /*
1565 * Send ATAPI_FORMAT_UNIT to the drive.
1566 *
1567 * Userland gives us the following structure:
1568 *
1569 * struct idefloppy_format_command {
1570 * int nblocks;
1571 * int blocksize;
1572 * int flags;
1573 * } ;
1574 *
1575 * flags is a bitmask, currently, the only defined flag is:
1576 *
1577 * 0x01 - verify media after format.
1578 */
1579 if (get_user(blocks, arg) ||
1580 get_user(length, arg+1) ||
1581 get_user(flags, arg+2)) {
1582 err = -EFAULT;
1583 goto out;
1584 }
1585
1586 (void) idefloppy_get_sfrp_bit(floppy->drive);
1587 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1588
1589 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1590 err = -EIO;
1591
1592out:
1593 if (err)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001594 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001595 return err;
1596}
1597
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static int idefloppy_ioctl(struct inode *inode, struct file *file,
1600 unsigned int cmd, unsigned long arg)
1601{
1602 struct block_device *bdev = inode->i_bdev;
1603 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1604 ide_drive_t *drive = floppy->drive;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001605 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001607 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 switch (cmd) {
1610 case CDROMEJECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /* fall through */
1612 case CDROM_LOCKDOOR:
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001613 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1615 return 0;
1616 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001617 return ide_floppy_get_format_capacities(drive, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 case IDEFLOPPY_IOCTL_FORMAT_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (!(file->f_mode & 2))
1620 return -EPERM;
1621
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001622 return ide_floppy_format_unit(floppy, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1624 return idefloppy_get_format_progress(drive, argp);
1625 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001626
1627 /*
1628 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1629 * and CDROM_SEND_PACKET (legacy) ioctls
1630 */
1631 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1632 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1633 bdev->bd_disk, cmd, argp);
1634 else
1635 err = -ENOTTY;
1636
1637 if (err == -ENOTTY)
1638 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1639
1640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static int idefloppy_media_changed(struct gendisk *disk)
1644{
1645 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1646 ide_drive_t *drive = floppy->drive;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001647 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* do not scan partitions twice if this is a removable device */
1650 if (drive->attach) {
1651 drive->attach = 0;
1652 return 0;
1653 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001654 ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
1655 floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
1656 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659static int idefloppy_revalidate_disk(struct gendisk *disk)
1660{
1661 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1662 set_capacity(disk, idefloppy_capacity(floppy->drive));
1663 return 0;
1664}
1665
1666static struct block_device_operations idefloppy_ops = {
1667 .owner = THIS_MODULE,
1668 .open = idefloppy_open,
1669 .release = idefloppy_release,
1670 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001671 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 .media_changed = idefloppy_media_changed,
1673 .revalidate_disk= idefloppy_revalidate_disk
1674};
1675
Russell King4031bbe2006-01-06 11:41:00 +00001676static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 idefloppy_floppy_t *floppy;
1679 struct gendisk *g;
1680
1681 if (!strstr("ide-floppy", drive->driver_req))
1682 goto failed;
1683 if (!drive->present)
1684 goto failed;
1685 if (drive->media != ide_floppy)
1686 goto failed;
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001687 if (!idefloppy_identify_device(drive, drive->id)) {
1688 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1689 " of ide-floppy\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto failed;
1691 }
1692 if (drive->scsi) {
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001693 printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi"
1694 " emulation.\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 goto failed;
1696 }
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001697 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1698 if (!floppy) {
1699 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1700 " structure\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 goto failed;
1702 }
1703
1704 g = alloc_disk(1 << PARTN_BITS);
1705 if (!g)
1706 goto out_free_floppy;
1707
1708 ide_init_disk(g, drive);
1709
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001710 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 kref_init(&floppy->kref);
1713
1714 floppy->drive = drive;
1715 floppy->driver = &idefloppy_driver;
1716 floppy->disk = g;
1717
1718 g->private_data = &floppy->driver;
1719
1720 drive->driver_data = floppy;
1721
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001722 idefloppy_setup(drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 g->minors = 1 << PARTN_BITS;
1725 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1727 g->fops = &idefloppy_ops;
1728 drive->attach = 1;
1729 add_disk(g);
1730 return 0;
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732out_free_floppy:
1733 kfree(floppy);
1734failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001735 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001738static void __exit idefloppy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001740 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001743static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001746 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Kay Sievers263756e2005-12-12 18:03:44 +01001749MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750module_init(idefloppy_init);
1751module_exit(idefloppy_exit);
1752MODULE_LICENSE("GPL");
Borislav Petkov0571c7a2008-02-02 19:56:38 +01001753MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1754