blob: ac66622a3570d0e7b76617b240d9fbbda9a51b61 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
12 *
13 * This driver supports the following IDE floppy drives:
14 *
15 * LS-120/240 SuperDisk
16 * Iomega Zip 100/250
17 * Iomega PC Card Clik!/PocketZip
18 *
Borislav Petkovd3f20842008-02-01 23:09:33 +010019 * For a historical changelog see
20 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#define IDEFLOPPY_VERSION "0.99.newide"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/kernel.h>
29#include <linux/delay.h>
30#include <linux/timer.h>
31#include <linux/mm.h>
32#include <linux/interrupt.h>
33#include <linux/major.h>
34#include <linux/errno.h>
35#include <linux/genhd.h>
36#include <linux/slab.h>
37#include <linux/cdrom.h>
38#include <linux/ide.h>
39#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +020042#include <scsi/scsi_ioctl.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/byteorder.h>
Borislav Petkov7e8b1632008-02-02 19:56:34 +010045#include <linux/irq.h>
46#include <linux/uaccess.h>
47#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/unaligned.h>
49
Borislav Petkovf373bd82008-02-02 19:56:36 +010050/* define to see debug info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define IDEFLOPPY_DEBUG_LOG 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
54#define IDEFLOPPY_DEBUG( fmt, args... )
55
56#if IDEFLOPPY_DEBUG_LOG
Borislav Petkovbcc77d92008-02-02 19:56:34 +010057#define debug_log(fmt, args...) \
58 printk(KERN_INFO "ide-floppy: " fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#else
60#define debug_log(fmt, args... ) do {} while(0)
61#endif
62
63
64/*
65 * Some drives require a longer irq timeout.
66 */
67#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
68
69/*
70 * After each failed packet command we issue a request sense command
71 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
72 */
73#define IDEFLOPPY_MAX_PC_RETRIES 3
74
75/*
76 * With each packet command, we allocate a buffer of
77 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
78 */
79#define IDEFLOPPY_PC_BUFFER_SIZE 256
80
81/*
82 * In various places in the driver, we need to allocate storage
83 * for packet commands and requests, which will remain valid while
84 * we leave the driver to wait for an interrupt or a timeout event.
85 */
86#define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
87
88/*
89 * Our view of a packet command.
90 */
91typedef struct idefloppy_packet_command_s {
92 u8 c[12]; /* Actual packet bytes */
93 int retries; /* On each retry, we increment retries */
94 int error; /* Error code */
95 int request_transfer; /* Bytes to transfer */
96 int actually_transferred; /* Bytes actually transferred */
97 int buffer_size; /* Size of our data buffer */
98 int b_count; /* Missing/Available data on the current buffer */
99 struct request *rq; /* The corresponding request */
100 u8 *buffer; /* Data buffer */
101 u8 *current_position; /* Pointer into the above buffer */
102 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
103 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
104 unsigned long flags; /* Status/Action bit flags: long for set_bit */
105} idefloppy_pc_t;
106
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100107/* Packet command flag bits. */
108enum {
109 /* 1 when we prefer to use DMA if possible */
110 PC_FLAG_DMA_RECOMMENDED = (1 << 0),
111 /* 1 while DMA in progress */
112 PC_FLAG_DMA_IN_PROGRESS = (1 << 1),
113 /* 1 when encountered problem during DMA */
114 PC_FLAG_DMA_ERROR = (1 << 2),
115 /* Data direction */
116 PC_FLAG_WRITING = (1 << 3),
117 /* Suppress error reporting */
118 PC_FLAG_SUPPRESS_ERROR = (1 << 4),
119};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100121/* format capacities descriptor codes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define CAPACITY_INVALID 0x00
123#define CAPACITY_UNFORMATTED 0x01
124#define CAPACITY_CURRENT 0x02
125#define CAPACITY_NO_CARTRIDGE 0x03
126
127/*
128 * Most of our global data which we need to save even as we leave the
129 * driver due to an interrupt or a timer event is stored in a variable
130 * of type idefloppy_floppy_t, defined below.
131 */
132typedef struct ide_floppy_obj {
133 ide_drive_t *drive;
134 ide_driver_t *driver;
135 struct gendisk *disk;
136 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100137 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Current packet command */
140 idefloppy_pc_t *pc;
141 /* Last failed packet command */
142 idefloppy_pc_t *failed_pc;
143 /* Packet command stack */
144 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
145 /* Next free packet command storage space */
146 int pc_stack_index;
147 struct request rq_stack[IDEFLOPPY_PC_STACK];
148 /* We implement a circular array */
149 int rq_stack_index;
150
151 /*
152 * Last error information
153 */
154 u8 sense_key, asc, ascq;
155 /* delay this long before sending packet command */
156 u8 ticks;
157 int progress_indication;
158
159 /*
160 * Device information
161 */
162 /* Current format */
163 int blocks, block_size, bs_factor;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100164 /* Last format capacity descriptor */
165 u8 cap_desc[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* Copy of the flexible disk page */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100167 u8 flexible_disk_page[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* Write protect */
169 int wp;
170 /* Supports format progress report */
171 int srfp;
172 /* Status/Action flags */
173 unsigned long flags;
174} idefloppy_floppy_t;
175
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200176#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100178/* Floppy flag bits values. */
179enum {
180 /* DRQ interrupt device */
181 IDEFLOPPY_FLAG_DRQ_INTERRUPT = (1 << 0),
182 /* Media may have changed */
183 IDEFLOPPY_FLAG_MEDIA_CHANGED = (1 << 1),
184 /* Format in progress */
185 IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS = (1 << 2),
186 /* Avoid commands not supported in Clik drive */
187 IDEFLOPPY_FLAG_CLIK_DRIVE = (1 << 3),
188 /* Requires BH algorithm for packets */
189 IDEFLOPPY_FLAG_ZIP_DRIVE = (1 << 4),
190};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * Defines for the mode sense command
194 */
195#define MODE_SENSE_CURRENT 0x00
196#define MODE_SENSE_CHANGEABLE 0x01
197#define MODE_SENSE_DEFAULT 0x02
198#define MODE_SENSE_SAVED 0x03
199
200/*
201 * IOCTLs used in low-level formatting.
202 */
203
204#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
205#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
206#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
207#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * Error codes which are returned in rq->errors to the higher part
211 * of the driver.
212 */
213#define IDEFLOPPY_ERROR_GENERAL 101
214
215/*
216 * The following is used to format the general configuration word of
217 * the ATAPI IDENTIFY DEVICE command.
218 */
219struct idefloppy_id_gcw {
220#if defined(__LITTLE_ENDIAN_BITFIELD)
221 unsigned packet_size :2; /* Packet Size */
222 unsigned reserved234 :3; /* Reserved */
223 unsigned drq_type :2; /* Command packet DRQ type */
224 unsigned removable :1; /* Removable media */
225 unsigned device_type :5; /* Device type */
226 unsigned reserved13 :1; /* Reserved */
227 unsigned protocol :2; /* Protocol type */
228#elif defined(__BIG_ENDIAN_BITFIELD)
229 unsigned protocol :2; /* Protocol type */
230 unsigned reserved13 :1; /* Reserved */
231 unsigned device_type :5; /* Device type */
232 unsigned removable :1; /* Removable media */
233 unsigned drq_type :2; /* Command packet DRQ type */
234 unsigned reserved234 :3; /* Reserved */
235 unsigned packet_size :2; /* Packet Size */
236#else
237#error "Bitfield endianness not defined! Check your byteorder.h"
238#endif
239};
240
241/*
Borislav Petkov948391d2008-02-02 19:56:34 +0100242 * Pages of the SELECT SENSE / MODE SENSE packet commands.
243 * See SFF-8070i spec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
245#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
246#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
247
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800248static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
251
252#define ide_floppy_g(disk) \
253 container_of((disk)->private_data, struct ide_floppy_obj, driver)
254
255static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
256{
257 struct ide_floppy_obj *floppy = NULL;
258
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800259 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 floppy = ide_floppy_g(disk);
261 if (floppy)
262 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800263 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return floppy;
265}
266
Borislav Petkov9a24b632008-02-02 19:56:33 +0100267static void idefloppy_cleanup_obj(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269static void ide_floppy_put(struct ide_floppy_obj *floppy)
270{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800271 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100272 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800273 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/*
277 * Too bad. The drive wants to send us data which we are not ready to accept.
278 * Just throw it away.
279 */
280static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
281{
282 while (bcount--)
283 (void) HWIF(drive)->INB(IDE_DATA_REG);
284}
285
Borislav Petkov0bf399e2008-02-02 19:56:36 +0100286static void idefloppy_write_zeros(ide_drive_t *drive, unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 while (bcount--)
289 HWIF(drive)->OUTB(0, IDE_DATA_REG);
290}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292
293/*
294 * idefloppy_do_end_request is used to finish servicing a request.
295 *
296 * For read/write requests, we will call ide_end_request to pass to the
297 * next buffer.
298 */
299static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
300{
301 idefloppy_floppy_t *floppy = drive->driver_data;
302 struct request *rq = HWGROUP(drive)->rq;
303 int error;
304
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100305 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 switch (uptodate) {
308 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
309 case 1: error = 0; break;
310 default: error = uptodate;
311 }
312 if (error)
313 floppy->failed_pc = NULL;
314 /* Why does this happen? */
315 if (!rq)
316 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200317 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* our real local end request function */
319 ide_end_request(drive, uptodate, nsecs);
320 return 0;
321 }
322 rq->errors = error;
323 /* fixme: need to move this local also */
324 ide_end_drive_cmd(drive, 0, 0);
325 return 0;
326}
327
Borislav Petkov32925e12008-02-02 19:56:36 +0100328static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc,
329 unsigned int bcount, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200332 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct bio_vec *bvec;
334 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200335 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 char *data;
337
NeilBrown5705f702007-09-25 12:35:59 +0200338 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200339 if (!bcount)
340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jens Axboe6c92e692007-08-16 13:43:12 +0200342 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jens Axboe6c92e692007-08-16 13:43:12 +0200344 data = bvec_kmap_irq(bvec, &flags);
Borislav Petkov32925e12008-02-02 19:56:36 +0100345 if (direction)
346 drive->hwif->atapi_output_bytes(drive, data, count);
347 else
348 drive->hwif->atapi_input_bytes(drive, data, count);
Jens Axboe6c92e692007-08-16 13:43:12 +0200349 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jens Axboe6c92e692007-08-16 13:43:12 +0200351 bcount -= count;
352 pc->b_count += count;
353 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 idefloppy_do_end_request(drive, 1, done >> 9);
357
358 if (bcount) {
Borislav Petkov32925e12008-02-02 19:56:36 +0100359 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
360 drive->name, __func__, bcount);
361 if (direction)
362 idefloppy_write_zeros(drive, bcount);
363 else
364 idefloppy_discard_data(drive, bcount);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367}
368
369static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
370{
371 struct request *rq = pc->rq;
372 struct bio *bio = rq->bio;
373
374 while ((bio = rq->bio) != NULL)
375 idefloppy_do_end_request(drive, 1, 0);
376}
377
378/*
379 * idefloppy_queue_pc_head generates a new packet command request in front
380 * of the request queue, before the current request, so that it will be
381 * processed immediately, on the next pass through the driver.
382 */
383static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
384{
385 struct ide_floppy_obj *floppy = drive->driver_data;
386
387 ide_init_drive_cmd(rq);
388 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200389 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 rq->rq_disk = floppy->disk;
391 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
392}
393
394static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
395{
396 idefloppy_floppy_t *floppy = drive->driver_data;
397
398 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
399 floppy->pc_stack_index=0;
400 return (&floppy->pc_stack[floppy->pc_stack_index++]);
401}
402
403static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
404{
405 idefloppy_floppy_t *floppy = drive->driver_data;
406
407 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
408 floppy->rq_stack_index = 0;
409 return (&floppy->rq_stack[floppy->rq_stack_index++]);
410}
411
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100412static void idefloppy_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100415 u8 *buf = floppy->pc->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100417 debug_log("Reached %s\n", __func__);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!floppy->pc->error) {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100420 floppy->sense_key = buf[2] & 0x0F;
421 floppy->asc = buf[12];
422 floppy->ascq = buf[13];
423 floppy->progress_indication = buf[15] & 0x80 ?
424 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
425
426 if (floppy->failed_pc)
427 debug_log("pc = %x, sense key = %x, asc = %x,"
428 " ascq = %x\n",
429 floppy->failed_pc->c[0],
430 floppy->sense_key,
431 floppy->asc,
432 floppy->ascq);
433 else
434 debug_log("sense key = %x, asc = %x, ascq = %x\n",
435 floppy->sense_key,
436 floppy->asc,
437 floppy->ascq);
438
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 idefloppy_do_end_request(drive, 1, 0);
441 } else {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100442 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
443 " request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 idefloppy_do_end_request(drive, 0, 0);
445 }
446}
447
448/*
449 * General packet command callback function.
450 */
451static void idefloppy_pc_callback (ide_drive_t *drive)
452{
453 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100454
455 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
458}
459
460/*
461 * idefloppy_init_pc initializes a packet command.
462 */
463static void idefloppy_init_pc (idefloppy_pc_t *pc)
464{
465 memset(pc->c, 0, 12);
466 pc->retries = 0;
467 pc->flags = 0;
468 pc->request_transfer = 0;
469 pc->buffer = pc->pc_buffer;
470 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
471 pc->callback = &idefloppy_pc_callback;
472}
473
474static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
475{
Borislav Petkov30d67092008-02-02 19:56:33 +0100476 idefloppy_init_pc(pc);
477 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 pc->c[4] = 255;
479 pc->request_transfer = 18;
480 pc->callback = &idefloppy_request_sense_callback;
481}
482
483/*
484 * idefloppy_retry_pc is called when an error was detected during the
485 * last packet command. We queue a request sense packet command in
486 * the head of the request list.
487 */
488static void idefloppy_retry_pc (ide_drive_t *drive)
489{
490 idefloppy_pc_t *pc;
491 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100493 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 pc = idefloppy_next_pc_storage(drive);
495 rq = idefloppy_next_rq_storage(drive);
496 idefloppy_create_request_sense_cmd(pc);
497 idefloppy_queue_pc_head(drive, pc, rq);
498}
499
Borislav Petkov0eea6452008-02-02 19:56:36 +0100500/* The usual interrupt handler called during a packet command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
502{
503 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100504 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 idefloppy_pc_t *pc = floppy->pc;
506 struct request *rq = pc->rq;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100507 xfer_func_t *xferfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 unsigned int temp;
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100509 int dma_error = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100510 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100511 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100513 debug_log("Reached %s interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100515 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100516 dma_error = hwif->ide_dma_end(drive);
517 if (dma_error) {
518 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
519 rq_data_dir(rq) ? "write" : "read");
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100520 pc->flags |= PC_FLAG_DMA_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 } else {
522 pc->actually_transferred = pc->request_transfer;
523 idefloppy_update_buffers(drive, pc);
524 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100525 debug_log("DMA finished\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
528 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100529 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100531 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100532 debug_log("Packet command completed, %d bytes transferred\n",
533 pc->actually_transferred);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100534 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Ingo Molnar366c7f52006-07-03 00:25:25 -0700536 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100538 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* Error detected */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100540 debug_log("%s: I/O error\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 rq->errors++;
Borislav Petkov30d67092008-02-02 19:56:33 +0100542 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 printk(KERN_ERR "ide-floppy: I/O error in "
544 "request sense command\n");
545 return ide_do_reset(drive);
546 }
547 /* Retry operation */
548 idefloppy_retry_pc(drive);
549 /* queued, but not started */
550 return ide_stopped;
551 }
552 pc->error = 0;
553 if (floppy->failed_pc == pc)
554 floppy->failed_pc = NULL;
555 /* Command finished - Call the callback function */
556 pc->callback(drive);
557 return ide_stopped;
558 }
559
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100560 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
561 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
563 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100564 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return ide_do_reset(drive);
566 }
567
568 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100569 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
570 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* on this interrupt */
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100572 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100574 if (ireason & CD) {
Borislav Petkov0eea6452008-02-02 19:56:36 +0100575 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return ide_do_reset(drive);
577 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100578 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Hopefully, we will never get here */
580 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100581 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 printk(KERN_ERR "but the floppy wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100583 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return ide_do_reset(drive);
585 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100586 if (!(pc->flags & PC_FLAG_WRITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100588 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (temp > pc->request_transfer) {
590 if (temp > pc->buffer_size) {
591 printk(KERN_ERR "ide-floppy: The floppy wants "
592 "to send us more data than expected "
593 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100594 idefloppy_discard_data(drive, bcount);
Borislav Petkov0078df22008-02-02 19:56:33 +0100595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ide_set_handler(drive,
597 &idefloppy_pc_intr,
598 IDEFLOPPY_WAIT_CMD,
599 NULL);
600 return ide_started;
601 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100602 debug_log("The floppy wants to send us more data than"
603 " expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100606 if (pc->flags & PC_FLAG_WRITING)
Borislav Petkov0eea6452008-02-02 19:56:36 +0100607 xferfunc = hwif->atapi_output_bytes;
Borislav Petkov32925e12008-02-02 19:56:36 +0100608 else
Borislav Petkov0eea6452008-02-02 19:56:36 +0100609 xferfunc = hwif->atapi_input_bytes;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100610
611 if (pc->buffer)
612 xferfunc(drive, pc->current_position, bcount);
613 else
Borislav Petkov32925e12008-02-02 19:56:36 +0100614 ide_floppy_io_buffers(drive, pc, bcount,
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100615 !!(pc->flags & PC_FLAG_WRITING));
Borislav Petkov0eea6452008-02-02 19:56:36 +0100616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100618 pc->actually_transferred += bcount;
619 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Borislav Petkov32925e12008-02-02 19:56:36 +0100621 /* And set the interrupt handler again */
622 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return ide_started;
624}
625
626/*
627 * This is the original routine that did the packet transfer.
628 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
629 * for that drive below. The algorithm is chosen based on drive type
630 */
631static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
632{
633 ide_startstop_t startstop;
634 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100635 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
638 printk(KERN_ERR "ide-floppy: Strange, packet command "
639 "initiated yet DRQ isn't asserted\n");
640 return startstop;
641 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100642 ireason = drive->hwif->INB(IDE_IREASON_REG);
643 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
645 "issuing a packet command\n");
646 return ide_do_reset(drive);
647 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Set the interrupt routine */
650 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
651 /* Send the actual packet */
652 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
653 return ide_started;
654}
655
656
657/*
658 * What we have here is a classic case of a top half / bottom half
659 * interrupt service routine. In interrupt mode, the device sends
660 * an interrupt to signal it's ready to receive a packet. However,
661 * we need to delay about 2-3 ticks before issuing the packet or we
662 * gets in trouble.
663 *
664 * So, follow carefully. transfer_pc1 is called as an interrupt (or
665 * directly). In either case, when the device says it's ready for a
666 * packet, we schedule the packet transfer to occur about 2-3 ticks
667 * later in transfer_pc2.
668 */
669static int idefloppy_transfer_pc2 (ide_drive_t *drive)
670{
671 idefloppy_floppy_t *floppy = drive->driver_data;
672
673 /* Send the actual packet */
674 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
675 /* Timeout for the packet command */
676 return IDEFLOPPY_WAIT_CMD;
677}
678
679static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
680{
681 idefloppy_floppy_t *floppy = drive->driver_data;
682 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100683 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
686 printk(KERN_ERR "ide-floppy: Strange, packet command "
687 "initiated yet DRQ isn't asserted\n");
688 return startstop;
689 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100690 ireason = drive->hwif->INB(IDE_IREASON_REG);
691 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
693 "while issuing a packet command\n");
694 return ide_do_reset(drive);
695 }
696 /*
697 * The following delay solves a problem with ATAPI Zip 100 drives
698 * where the Busy flag was apparently being deasserted before the
699 * unit was ready to receive data. This was happening on a
700 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
701 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
702 * used until after the packet is moved in about 50 msec.
703 */
Borislav Petkov0078df22008-02-02 19:56:33 +0100704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 ide_set_handler(drive,
706 &idefloppy_pc_intr, /* service routine for packet command */
707 floppy->ticks, /* wait this long before "failing" */
708 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
709 return ide_started;
710}
711
Borislav Petkovd652c132008-02-02 19:56:35 +0100712static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
713 idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Borislav Petkovd652c132008-02-02 19:56:35 +0100715 /* supress error messages resulting from Medium not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (floppy->sense_key == 0x02 &&
717 floppy->asc == 0x3a &&
718 floppy->ascq == 0x00)
Borislav Petkovd652c132008-02-02 19:56:35 +0100719 return;
720
721 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
722 "asc = %2x, ascq = %2x\n",
723 floppy->drive->name, pc->c[0], floppy->sense_key,
724 floppy->asc, floppy->ascq);
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/*
729 * Issue a packet command
730 */
731static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
732{
733 idefloppy_floppy_t *floppy = drive->driver_data;
734 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100736 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100737 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100740 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 floppy->failed_pc = pc;
742 /* Set the current packet command */
743 floppy->pc = pc;
744
Borislav Petkov757ced82008-02-02 19:56:37 +0100745 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100746 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
Borislav Petkov757ced82008-02-02 19:56:37 +0100747 ide_floppy_report_error(floppy, pc);
748 /* Giving up */
749 pc->error = IDEFLOPPY_ERROR_GENERAL;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 floppy->failed_pc = NULL;
752 pc->callback(drive);
753 return ide_stopped;
754 }
755
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100756 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 pc->retries++;
759 /* We haven't transferred any data yet */
760 pc->actually_transferred = 0;
761 pc->current_position = pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100762 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100764 if (pc->flags & PC_FLAG_DMA_ERROR) {
765 pc->flags &= ~PC_FLAG_DMA_ERROR;
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100766 ide_dma_off(drive);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100767 }
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100768 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100770 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100771 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100773 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
774 IDE_TFLAG_OUT_DEVICE, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100776 if (dma) { /* Begin DMA, if necessary */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100777 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 hwif->dma_start(drive);
779 }
780
781 /* Can we transfer the packet when we get the interrupt or wait? */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100782 if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* wait */
784 pkt_xfer_routine = &idefloppy_transfer_pc1;
785 } else {
786 /* immediate */
787 pkt_xfer_routine = &idefloppy_transfer_pc;
788 }
789
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100790 if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* Issue the packet command */
792 ide_execute_command(drive, WIN_PACKETCMD,
793 pkt_xfer_routine,
794 IDEFLOPPY_WAIT_CMD,
795 NULL);
796 return ide_started;
797 } else {
798 /* Issue the packet command */
799 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
800 return (*pkt_xfer_routine) (drive);
801 }
802}
803
804static void idefloppy_rw_callback (ide_drive_t *drive)
805{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100806 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 idefloppy_do_end_request(drive, 1, 0);
809 return;
810}
811
812static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
813{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100814 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100817 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 pc->c[4] = prevent;
819}
820
821static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
822{
823 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100824 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 pc->c[7] = 255;
826 pc->c[8] = 255;
827 pc->request_transfer = 255;
828}
829
830static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
831 int flags)
832{
833 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100834 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 pc->c[1] = 0x17;
836
837 memset(pc->buffer, 0, 12);
838 pc->buffer[1] = 0xA2;
839 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
840
841 if (flags & 1) /* Verify bit on... */
842 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
843 pc->buffer[3] = 8;
844
Borislav Petkov8f622432008-02-02 19:56:33 +0100845 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
846 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 pc->buffer_size=12;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100848 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
852 * A mode sense command is used to "sense" floppy parameters.
853 */
854static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
855{
Borislav Petkov24a5d702008-02-02 19:56:34 +0100856 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100859 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 pc->c[1] = 0;
861 pc->c[2] = page_code + (type << 6);
862
863 switch (page_code) {
864 case IDEFLOPPY_CAPABILITIES_PAGE:
865 length += 12;
866 break;
867 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
868 length += 32;
869 break;
870 default:
871 printk(KERN_ERR "ide-floppy: unsupported page code "
872 "in create_mode_sense_cmd\n");
873 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100874 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 pc->request_transfer = length;
876}
877
878static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
879{
880 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100881 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 pc->c[4] = start;
883}
884
885static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
886{
887 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100888 pc->c[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100891static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
892 idefloppy_pc_t *pc, struct request *rq,
893 unsigned long sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 int block = sector / floppy->bs_factor;
896 int blocks = rq->nr_sectors / floppy->bs_factor;
897 int cmd = rq_data_dir(rq);
898
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100899 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 block, blocks);
901
902 idefloppy_init_pc(pc);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100903 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
904 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Borislav Petkov8f622432008-02-02 19:56:33 +0100905 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Borislav Petkovae7e8dd2008-02-02 19:56:36 +0100906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 pc->callback = &idefloppy_rw_callback;
908 pc->rq = rq;
909 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200910 if (rq->cmd_flags & REQ_RW)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100911 pc->flags |= PC_FLAG_WRITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 pc->buffer = NULL;
913 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100914 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Jens Axboe3d6392c2007-07-09 12:38:05 +0200917static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
919{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 idefloppy_init_pc(pc);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200921 pc->callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200923 pc->rq = rq;
924 pc->b_count = rq->data_len;
925 if (rq->data_len && rq_data_dir(rq) == WRITE)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100926 pc->flags |= PC_FLAG_WRITING;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200927 pc->buffer = rq->data;
928 if (rq->bio)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +0100929 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200930 /*
931 * possibly problematic, doesn't look like ide-floppy correctly
932 * handled scattered requests if dma fails...
933 */
934 pc->request_transfer = pc->buffer_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
938 * idefloppy_do_request is our request handling function.
939 */
940static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
941{
942 idefloppy_floppy_t *floppy = drive->driver_data;
943 idefloppy_pc_t *pc;
944 unsigned long block = (unsigned long)block_s;
945
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100946 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -0700947 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100948 rq->cmd_type, rq->errors);
949 debug_log("sector: %ld, nr_sectors: %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 "current_nr_sectors: %d\n", (long)rq->sector,
951 rq->nr_sectors, rq->current_nr_sectors);
952
953 if (rq->errors >= ERROR_MAX) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100954 if (floppy->failed_pc)
955 ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 else
957 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
958 drive->name);
959 idefloppy_do_end_request(drive, 0, 0);
960 return ide_stopped;
961 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200962 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (((long)rq->sector % floppy->bs_factor) ||
964 (rq->nr_sectors % floppy->bs_factor)) {
965 printk("%s: unsupported r/w request size\n",
966 drive->name);
967 idefloppy_do_end_request(drive, 0, 0);
968 return ide_stopped;
969 }
970 pc = idefloppy_next_pc_storage(drive);
971 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200972 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 pc = (idefloppy_pc_t *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200974 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200976 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 } else {
978 blk_dump_rq_flags(rq,
979 "ide-floppy: unsupported command in queue");
980 idefloppy_do_end_request(drive, 0, 0);
981 return ide_stopped;
982 }
983
984 pc->rq = rq;
985 return idefloppy_issue_pc(drive, pc);
986}
987
988/*
989 * idefloppy_queue_pc_tail adds a special packet command request to the
990 * tail of the request queue, and waits for it to be serviced.
991 */
992static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
993{
994 struct ide_floppy_obj *floppy = drive->driver_data;
995 struct request rq;
996
997 ide_init_drive_cmd (&rq);
998 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200999 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 rq.rq_disk = floppy->disk;
1001
1002 return ide_do_drive_cmd(drive, &rq, ide_wait);
1003}
1004
1005/*
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001006 * Look at the flexible disk page parameters. We ignore the CHS capacity
1007 * parameters and use the LBA parameters instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001009static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 idefloppy_floppy_t *floppy = drive->driver_data;
1012 idefloppy_pc_t pc;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001013 u8 *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int capacity, lba_capacity;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001015 u16 transfer_rate, sector_size, cyls, rpm;
1016 u8 heads, sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001018 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
1019 MODE_SENSE_CURRENT);
1020
1021 if (idefloppy_queue_pc_tail(drive, &pc)) {
1022 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
1023 " parameters\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 1;
1025 }
Borislav Petkov24a5d702008-02-02 19:56:34 +01001026 floppy->wp = !!(pc.buffer[3] & 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 set_disk_ro(floppy->disk, floppy->wp);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001028 page = &pc.buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001030 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
1031 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
1032 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
1033 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
1034 heads = pc.buffer[8 + 4];
1035 sectors = pc.buffer[8 + 5];
1036
1037 capacity = cyls * heads * sectors * sector_size;
1038
1039 if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1041 "%d sector size, %d rpm\n",
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001042 drive->name, capacity / 1024, cyls, heads,
1043 sectors, transfer_rate / 8, sector_size, rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001045 memcpy(&floppy->flexible_disk_page, page, 32);
1046 drive->bios_cyl = cyls;
1047 drive->bios_head = heads;
1048 drive->bios_sect = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 lba_capacity = floppy->blocks * floppy->block_size;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (capacity < lba_capacity) {
1052 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1053 "bytes, but the drive only handles %d\n",
1054 drive->name, lba_capacity, capacity);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001055 floppy->blocks = floppy->block_size ?
1056 capacity / floppy->block_size : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 return 0;
1059}
1060
Borislav Petkov948391d2008-02-02 19:56:34 +01001061static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 idefloppy_floppy_t *floppy = drive->driver_data;
1064 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 floppy->srfp = 0;
1067 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1068 MODE_SENSE_CURRENT);
1069
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001070 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
Borislav Petkov948391d2008-02-02 19:56:34 +01001071 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Borislav Petkov948391d2008-02-02 19:56:34 +01001074 floppy->srfp = pc.buffer[8 + 2] & 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return (0);
1076}
1077
1078/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001079 * Determine if a media is present in the floppy drive, and if so, its LBA
1080 * capacity.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001082static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 idefloppy_floppy_t *floppy = drive->driver_data;
1085 idefloppy_pc_t pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001086 u8 *cap_desc;
1087 u8 header_len, desc_cnt;
1088 int i, rc = 1, blocks, length;
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 drive->bios_cyl = 0;
1091 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +01001092 floppy->blocks = 0;
1093 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 set_capacity(floppy->disk, 0);
1095
1096 idefloppy_create_read_capacity_cmd(&pc);
1097 if (idefloppy_queue_pc_tail(drive, &pc)) {
1098 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1099 return 1;
1100 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001101 header_len = pc.buffer[3];
1102 cap_desc = &pc.buffer[4];
1103 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001105 for (i = 0; i < desc_cnt; i++) {
1106 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001108 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1109 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1110
1111 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1112 i, blocks * length / 1024, blocks, length);
1113
1114 if (i)
1115 continue;
1116 /*
1117 * the code below is valid only for the 1st descriptor, ie i=0
1118 */
1119
1120 switch (pc.buffer[desc_start + 4] & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1122 case CAPACITY_UNFORMATTED:
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001123 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
1125 * If it is not a clik drive, break out
1126 * (maintains previous driver behaviour)
1127 */
1128 break;
1129 case CAPACITY_CURRENT:
1130 /* Normal Zip/LS-120 disks */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001131 if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1133 "sector size\n", drive->name,
1134 blocks * length / 1024, blocks, length);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001135 memcpy(&floppy->cap_desc, cap_desc, 8);
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!length || length % 512) {
1138 printk(KERN_NOTICE "%s: %d bytes block size "
1139 "not supported\n", drive->name, length);
1140 } else {
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001141 floppy->blocks = blocks;
1142 floppy->block_size = length;
1143 floppy->bs_factor = length / 512;
1144 if (floppy->bs_factor != 1)
1145 printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 "512 bytes block size not "
1147 "fully supported\n",
1148 drive->name);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001149 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 break;
1152 case CAPACITY_NO_CARTRIDGE:
1153 /*
1154 * This is a KERN_ERR so it appears on screen
1155 * for the user to see
1156 */
1157 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1158 break;
1159 case CAPACITY_INVALID:
1160 printk(KERN_ERR "%s: Invalid capacity for disk "
1161 "in drive\n", drive->name);
1162 break;
1163 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001164 debug_log("Descriptor 0 Code: %d\n",
1165 pc.buffer[desc_start + 4] & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
1168 /* Clik! disk does not support get_flexible_disk_page */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001169 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001170 (void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1173 return rc;
1174}
1175
1176/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001177 * Obtain the list of formattable capacities.
1178 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1179 * descriptors to userland, instead of our own structures.
1180 *
1181 * Userland gives us the following structure:
1182 *
1183 * struct idefloppy_format_capacities {
1184 * int nformats;
1185 * struct {
1186 * int nblocks;
1187 * int blocksize;
1188 * } formats[];
1189 * };
1190 *
1191 * userland initializes nformats to the number of allocated formats[] records.
1192 * On exit we set nformats to the number of records we've actually initialized.
1193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001195static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001197 idefloppy_pc_t pc;
1198 u8 header_len, desc_cnt;
1199 int i, blocks, length, u_array_size, u_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int __user *argp;
1201
1202 if (get_user(u_array_size, arg))
1203 return (-EFAULT);
1204
1205 if (u_array_size <= 0)
1206 return (-EINVAL);
1207
1208 idefloppy_create_read_capacity_cmd(&pc);
1209 if (idefloppy_queue_pc_tail(drive, &pc)) {
1210 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001211 return (-EIO);
1212 }
1213 header_len = pc.buffer[3];
1214 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 u_index = 0;
1217 argp = arg + 1;
1218
1219 /*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001220 * We always skip the first capacity descriptor. That's the current
1221 * capacity. We are interested in the remaining descriptors, the
1222 * formattable capacities.
1223 */
1224 for (i = 1; i < desc_cnt; i++) {
1225 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (u_index >= u_array_size)
1228 break; /* User-supplied buffer too small */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001230 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1231 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (put_user(blocks, argp))
1234 return(-EFAULT);
1235 ++argp;
1236
1237 if (put_user(length, argp))
1238 return (-EFAULT);
1239 ++argp;
1240
1241 ++u_index;
1242 }
1243
1244 if (put_user(u_index, arg))
1245 return (-EFAULT);
1246 return (0);
1247}
1248
1249/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250** Get ATAPI_FORMAT_UNIT progress indication.
1251**
Matt LaPlante0779bf22006-11-30 05:24:39 +01001252** Userland gives a pointer to an int. The int is set to a progress
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253** indicator 0-65536, with 65536=100%.
1254**
1255** If the drive does not support format progress indication, we just check
1256** the dsc bit, and return either 0 or 65536.
1257*/
1258
1259static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1260{
1261 idefloppy_floppy_t *floppy = drive->driver_data;
1262 idefloppy_pc_t pc;
1263 int progress_indication = 0x10000;
1264
1265 if (floppy->srfp) {
1266 idefloppy_create_request_sense_cmd(&pc);
1267 if (idefloppy_queue_pc_tail(drive, &pc)) {
1268 return (-EIO);
1269 }
1270
1271 if (floppy->sense_key == 2 &&
1272 floppy->asc == 4 &&
1273 floppy->ascq == 4) {
1274 progress_indication = floppy->progress_indication;
1275 }
1276 /* Else assume format_unit has finished, and we're
1277 ** at 0x10000 */
1278 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001280 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 local_irq_save(flags);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001283 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 local_irq_restore(flags);
1285
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001286 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 if (put_user(progress_indication, arg))
1289 return (-EFAULT);
1290
1291 return (0);
1292}
1293
1294/*
1295 * Return the current floppy capacity.
1296 */
1297static sector_t idefloppy_capacity (ide_drive_t *drive)
1298{
1299 idefloppy_floppy_t *floppy = drive->driver_data;
1300 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1301
1302 return capacity;
1303}
1304
1305/*
Borislav Petkovf373bd82008-02-02 19:56:36 +01001306 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1307 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
Borislav Petkovf373bd82008-02-02 19:56:36 +01001309static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct idefloppy_id_gcw gcw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 *((u16 *) &gcw) = id->config;
1314
1315#ifdef CONFIG_PPC
1316 /* kludge for Apple PowerBook internal zip */
1317 if ((gcw.device_type == 5) &&
1318 !strstr(id->model, "CD-ROM") &&
1319 strstr(id->model, "ZIP"))
Borislav Petkovf373bd82008-02-02 19:56:36 +01001320 gcw.device_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321#endif
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (gcw.protocol != 2)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001324 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
1325 gcw.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 else if (gcw.device_type != 0)
Borislav Petkovf373bd82008-02-02 19:56:36 +01001327 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
1328 "to floppy\n", gcw.device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 else if (!gcw.removable)
1330 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1331 else if (gcw.drq_type == 3) {
Borislav Petkovf373bd82008-02-02 19:56:36 +01001332 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
1333 "supported\n", gcw.drq_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 } else if (gcw.packet_size != 0) {
Borislav Petkovf373bd82008-02-02 19:56:36 +01001335 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
1336 "bytes long\n", gcw.packet_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 } else
1338 return 1;
1339 return 0;
1340}
1341
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001342#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343static void idefloppy_add_settings(ide_drive_t *drive)
1344{
1345 idefloppy_floppy_t *floppy = drive->driver_data;
1346
1347/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001348 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001350 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1351 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1352 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1353 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001355#else
1356static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1357#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359/*
1360 * Driver initialization.
1361 */
1362static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1363{
1364 struct idefloppy_id_gcw gcw;
1365
1366 *((u16 *) &gcw) = drive->id->config;
1367 floppy->pc = floppy->pc_stack;
1368 if (gcw.drq_type == 1)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001369 floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
1371 * We used to check revisions here. At this point however
1372 * I'm giving up. Just assume they are all broken, its easier.
1373 *
1374 * The actual reason for the workarounds was likely
1375 * a driver bug after all rather than a firmware bug,
1376 * and the workaround below used to hide it. It should
1377 * be fixed as of version 1.9, but to be on the safe side
1378 * we'll leave the limitation below for the 2.2.x tree.
1379 */
1380
1381 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001382 floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /* This value will be visible in the /proc/ide/hdx/settings */
1384 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1385 blk_queue_max_sectors(drive->queue, 64);
1386 }
1387
1388 /*
1389 * Guess what? The IOMEGA Clik! drive also needs the
1390 * above fix. It makes nasty clicking noises without
1391 * it, so please don't remove this.
1392 */
1393 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1394 blk_queue_max_sectors(drive->queue, 64);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001395 floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
1398
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001399 (void) ide_floppy_get_capacity(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 idefloppy_add_settings(drive);
1401}
1402
Russell King4031bbe2006-01-06 11:41:00 +00001403static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 idefloppy_floppy_t *floppy = drive->driver_data;
1406 struct gendisk *g = floppy->disk;
1407
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001408 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 del_gendisk(g);
1411
1412 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Borislav Petkov9a24b632008-02-02 19:56:33 +01001415static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1418 ide_drive_t *drive = floppy->drive;
1419 struct gendisk *g = floppy->disk;
1420
1421 drive->driver_data = NULL;
1422 g->private_data = NULL;
1423 put_disk(g);
1424 kfree(floppy);
1425}
1426
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001427#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428static int proc_idefloppy_read_capacity
1429 (char *page, char **start, off_t off, int count, int *eof, void *data)
1430{
1431 ide_drive_t*drive = (ide_drive_t *)data;
1432 int len;
1433
1434 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1435 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1436}
1437
1438static ide_proc_entry_t idefloppy_proc[] = {
1439 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1440 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1441 { NULL, 0, NULL, NULL }
1442};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001443#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Russell King4031bbe2006-01-06 11:41:00 +00001445static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001448 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001449 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001450 .name = "ide-floppy",
1451 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001452 },
Russell King4031bbe2006-01-06 11:41:00 +00001453 .probe = ide_floppy_probe,
1454 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 .version = IDEFLOPPY_VERSION,
1456 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 .do_request = idefloppy_do_request,
1459 .end_request = idefloppy_do_end_request,
1460 .error = __ide_error,
1461 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001462#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001464#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465};
1466
1467static int idefloppy_open(struct inode *inode, struct file *filp)
1468{
1469 struct gendisk *disk = inode->i_bdev->bd_disk;
1470 struct ide_floppy_obj *floppy;
1471 ide_drive_t *drive;
1472 idefloppy_pc_t pc;
1473 int ret = 0;
1474
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001475 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (!(floppy = ide_floppy_get(disk)))
1478 return -ENXIO;
1479
1480 drive = floppy->drive;
1481
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001482 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001484 if (floppy->openers == 1) {
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001485 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /* Just in case */
1487
1488 idefloppy_create_test_unit_ready_cmd(&pc);
1489 if (idefloppy_queue_pc_tail(drive, &pc)) {
1490 idefloppy_create_start_stop_cmd(&pc, 1);
1491 (void) idefloppy_queue_pc_tail(drive, &pc);
1492 }
1493
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001494 if (ide_floppy_get_capacity(drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 && (filp->f_flags & O_NDELAY) == 0
1496 /*
1497 ** Allow O_NDELAY to open a drive without a disk, or with
1498 ** an unreadable disk, so that we can get the format
1499 ** capacity of the drive or begin the format - Sam
1500 */
1501 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 ret = -EIO;
1503 goto out_put_floppy;
1504 }
1505
1506 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 ret = -EROFS;
1508 goto out_put_floppy;
1509 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001510 floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001512 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 idefloppy_create_prevent_cmd(&pc, 1);
1514 (void) idefloppy_queue_pc_tail(drive, &pc);
1515 }
1516 check_disk_change(inode->i_bdev);
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001517 } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 ret = -EBUSY;
1519 goto out_put_floppy;
1520 }
1521 return 0;
1522
1523out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001524 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 ide_floppy_put(floppy);
1526 return ret;
1527}
1528
1529static int idefloppy_release(struct inode *inode, struct file *filp)
1530{
1531 struct gendisk *disk = inode->i_bdev->bd_disk;
1532 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1533 ide_drive_t *drive = floppy->drive;
1534 idefloppy_pc_t pc;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001535
1536 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001538 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* IOMEGA Clik! drives do not support lock/unlock commands */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001540 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 idefloppy_create_prevent_cmd(&pc, 0);
1542 (void) idefloppy_queue_pc_tail(drive, &pc);
1543 }
1544
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001545 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001547
1548 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 ide_floppy_put(floppy);
1551
1552 return 0;
1553}
1554
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001555static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1556{
1557 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1558 ide_drive_t *drive = floppy->drive;
1559
1560 geo->heads = drive->bios_head;
1561 geo->sectors = drive->bios_sect;
1562 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1563 return 0;
1564}
1565
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001566static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
1567 unsigned long arg, unsigned int cmd)
1568{
1569 if (floppy->openers > 1)
1570 return -EBUSY;
1571
1572 /* The IOMEGA Clik! Drive doesn't support this command -
1573 * no room for an eject mechanism */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001574 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001575 int prevent = arg ? 1 : 0;
1576
1577 if (cmd == CDROMEJECT)
1578 prevent = 0;
1579
1580 idefloppy_create_prevent_cmd(pc, prevent);
1581 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1582 }
1583
1584 if (cmd == CDROMEJECT) {
1585 idefloppy_create_start_stop_cmd(pc, 2);
1586 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1587 }
1588
1589 return 0;
1590}
1591
1592static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1593 int __user *arg)
1594{
1595 int blocks, length, flags, err = 0;
1596 idefloppy_pc_t pc;
1597
1598 if (floppy->openers > 1) {
1599 /* Don't format if someone is using the disk */
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001600 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001601 return -EBUSY;
1602 }
1603
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001604 floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001605
1606 /*
1607 * Send ATAPI_FORMAT_UNIT to the drive.
1608 *
1609 * Userland gives us the following structure:
1610 *
1611 * struct idefloppy_format_command {
1612 * int nblocks;
1613 * int blocksize;
1614 * int flags;
1615 * } ;
1616 *
1617 * flags is a bitmask, currently, the only defined flag is:
1618 *
1619 * 0x01 - verify media after format.
1620 */
1621 if (get_user(blocks, arg) ||
1622 get_user(length, arg+1) ||
1623 get_user(flags, arg+2)) {
1624 err = -EFAULT;
1625 goto out;
1626 }
1627
1628 (void) idefloppy_get_sfrp_bit(floppy->drive);
1629 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1630
1631 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1632 err = -EIO;
1633
1634out:
1635 if (err)
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001636 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001637 return err;
1638}
1639
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641static int idefloppy_ioctl(struct inode *inode, struct file *file,
1642 unsigned int cmd, unsigned long arg)
1643{
1644 struct block_device *bdev = inode->i_bdev;
1645 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1646 ide_drive_t *drive = floppy->drive;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001647 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001649 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 switch (cmd) {
1652 case CDROMEJECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* fall through */
1654 case CDROM_LOCKDOOR:
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001655 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1657 return 0;
1658 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001659 return ide_floppy_get_format_capacities(drive, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 case IDEFLOPPY_IOCTL_FORMAT_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (!(file->f_mode & 2))
1662 return -EPERM;
1663
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001664 return ide_floppy_format_unit(floppy, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1666 return idefloppy_get_format_progress(drive, argp);
1667 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001668
1669 /*
1670 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1671 * and CDROM_SEND_PACKET (legacy) ioctls
1672 */
1673 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1674 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1675 bdev->bd_disk, cmd, argp);
1676 else
1677 err = -ENOTTY;
1678
1679 if (err == -ENOTTY)
1680 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1681
1682 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685static int idefloppy_media_changed(struct gendisk *disk)
1686{
1687 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1688 ide_drive_t *drive = floppy->drive;
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /* do not scan partitions twice if this is a removable device */
1692 if (drive->attach) {
1693 drive->attach = 0;
1694 return 0;
1695 }
Borislav Petkov6e5fa7b2008-02-02 19:56:37 +01001696 ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
1697 floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
1698 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
1701static int idefloppy_revalidate_disk(struct gendisk *disk)
1702{
1703 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1704 set_capacity(disk, idefloppy_capacity(floppy->drive));
1705 return 0;
1706}
1707
1708static struct block_device_operations idefloppy_ops = {
1709 .owner = THIS_MODULE,
1710 .open = idefloppy_open,
1711 .release = idefloppy_release,
1712 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001713 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .media_changed = idefloppy_media_changed,
1715 .revalidate_disk= idefloppy_revalidate_disk
1716};
1717
Russell King4031bbe2006-01-06 11:41:00 +00001718static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 idefloppy_floppy_t *floppy;
1721 struct gendisk *g;
1722
1723 if (!strstr("ide-floppy", drive->driver_req))
1724 goto failed;
1725 if (!drive->present)
1726 goto failed;
1727 if (drive->media != ide_floppy)
1728 goto failed;
1729 if (!idefloppy_identify_device (drive, drive->id)) {
1730 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1731 goto failed;
1732 }
1733 if (drive->scsi) {
1734 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1735 goto failed;
1736 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001737 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1739 goto failed;
1740 }
1741
1742 g = alloc_disk(1 << PARTN_BITS);
1743 if (!g)
1744 goto out_free_floppy;
1745
1746 ide_init_disk(g, drive);
1747
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001748 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 kref_init(&floppy->kref);
1751
1752 floppy->drive = drive;
1753 floppy->driver = &idefloppy_driver;
1754 floppy->disk = g;
1755
1756 g->private_data = &floppy->driver;
1757
1758 drive->driver_data = floppy;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 idefloppy_setup (drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 g->minors = 1 << PARTN_BITS;
1763 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1765 g->fops = &idefloppy_ops;
1766 drive->attach = 1;
1767 add_disk(g);
1768 return 0;
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770out_free_floppy:
1771 kfree(floppy);
1772failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001773 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1777
1778static void __exit idefloppy_exit (void)
1779{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001780 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001783static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001786 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
Kay Sievers263756e2005-12-12 18:03:44 +01001789MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790module_init(idefloppy_init);
1791module_exit(idefloppy_exit);
1792MODULE_LICENSE("GPL");