ide: add ide_atapi_{discard_data,write_zeros} inline helpers
[deliverable/linux.git] / drivers / ide / ide-floppy.c
CommitLineData
1da177e4 1/*
d3f20848
BP
2 * IDE ATAPI floppy driver.
3 *
59bca8cc
BZ
4 * 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
0571c7a4 7 *
1da177e4
LT
8 * 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 *
d3f20848
BP
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
1da177e4
LT
16 */
17
fc6c5bc7 18#define IDEFLOPPY_VERSION "1.00"
1da177e4 19
1da177e4
LT
20#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>
cf8b8975 35#include <linux/mutex.h>
1da177e4 36
89636af2
BZ
37#include <scsi/scsi_ioctl.h>
38
1da177e4 39#include <asm/byteorder.h>
7e8b163b
BP
40#include <linux/irq.h>
41#include <linux/uaccess.h>
42#include <linux/io.h>
1da177e4
LT
43#include <asm/unaligned.h>
44
f373bd82 45/* define to see debug info */
1da177e4 46#define IDEFLOPPY_DEBUG_LOG 0
1da177e4
LT
47
48/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
0571c7a4 49#define IDEFLOPPY_DEBUG(fmt, args...)
1da177e4
LT
50
51#if IDEFLOPPY_DEBUG_LOG
bcc77d9c
BP
52#define debug_log(fmt, args...) \
53 printk(KERN_INFO "ide-floppy: " fmt, ## args)
1da177e4 54#else
0571c7a4 55#define debug_log(fmt, args...) do {} while (0)
1da177e4
LT
56#endif
57
58
0571c7a4 59/* Some drives require a longer irq timeout. */
1da177e4
LT
60#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
61
62/*
0571c7a4
BP
63 * After each failed packet command we issue a request sense command and retry
64 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
1da177e4
LT
65 */
66#define IDEFLOPPY_MAX_PC_RETRIES 3
67
68/*
0571c7a4
BP
69 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
70 * bytes.
1da177e4
LT
71 */
72#define IDEFLOPPY_PC_BUFFER_SIZE 256
73
74/*
0571c7a4
BP
75 * 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.
1da177e4
LT
78 */
79#define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
80
1da177e4
LT
81typedef struct idefloppy_packet_command_s {
82 u8 c[12]; /* Actual packet bytes */
0571c7a4
BP
83 int retries; /* On each retry, we increment
84 retries */
1da177e4
LT
85 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 */
0571c7a4
BP
89 int b_count; /* Missing/Available data on
90 the current buffer */
1da177e4
LT
91 struct request *rq; /* The corresponding request */
92 u8 *buffer; /* Data buffer */
0571c7a4
BP
93 u8 *current_position; /* Pointer into above buffer */
94 void (*callback) (ide_drive_t *); /* Called when this packet
95 command is completed */
1da177e4 96 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
0571c7a4
BP
97 unsigned long flags; /* Status/Action bit flags: long
98 for set_bit */
1da177e4
LT
99} idefloppy_pc_t;
100
6e5fa7b8
BP
101/* 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};
1da177e4 114
194ec0c0 115/* format capacities descriptor codes */
1da177e4
LT
116#define CAPACITY_INVALID 0x00
117#define CAPACITY_UNFORMATTED 0x01
118#define CAPACITY_CURRENT 0x02
119#define CAPACITY_NO_CARTRIDGE 0x03
120
121/*
0571c7a4
BP
122 * 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.
1da177e4
LT
125 */
126typedef struct ide_floppy_obj {
127 ide_drive_t *drive;
128 ide_driver_t *driver;
129 struct gendisk *disk;
130 struct kref kref;
c94964a4 131 unsigned int openers; /* protected by BKL for now */
1da177e4
LT
132
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
0571c7a4 145 /* Last error information */
1da177e4
LT
146 u8 sense_key, asc, ascq;
147 /* delay this long before sending packet command */
148 u8 ticks;
149 int progress_indication;
150
0571c7a4 151 /* Device information */
1da177e4
LT
152 /* Current format */
153 int blocks, block_size, bs_factor;
194ec0c0
BP
154 /* Last format capacity descriptor */
155 u8 cap_desc[8];
1da177e4 156 /* Copy of the flexible disk page */
8e81bbba 157 u8 flexible_disk_page[32];
1da177e4
LT
158 /* 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
c40d3d38 166#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
1da177e4 167
6e5fa7b8
BP
168/* 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};
1da177e4 181
0571c7a4 182/* Defines for the MODE SENSE command */
1da177e4
LT
183#define MODE_SENSE_CURRENT 0x00
184#define MODE_SENSE_CHANGEABLE 0x01
0571c7a4 185#define MODE_SENSE_DEFAULT 0x02
1da177e4
LT
186#define MODE_SENSE_SAVED 0x03
187
0571c7a4 188/* IOCTLs used in low-level formatting. */
1da177e4
LT
189#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
0571c7a4 194/* Error code returned in rq->errors to the higher part of the driver. */
1da177e4
LT
195#define IDEFLOPPY_ERROR_GENERAL 101
196
1da177e4 197/*
948391d1
BP
198 * Pages of the SELECT SENSE / MODE SENSE packet commands.
199 * See SFF-8070i spec.
1da177e4
LT
200 */
201#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
202#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
203
cf8b8975 204static DEFINE_MUTEX(idefloppy_ref_mutex);
1da177e4
LT
205
206#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
207
208#define ide_floppy_g(disk) \
209 container_of((disk)->private_data, struct ide_floppy_obj, driver)
210
211static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
212{
213 struct ide_floppy_obj *floppy = NULL;
214
cf8b8975 215 mutex_lock(&idefloppy_ref_mutex);
1da177e4
LT
216 floppy = ide_floppy_g(disk);
217 if (floppy)
218 kref_get(&floppy->kref);
cf8b8975 219 mutex_unlock(&idefloppy_ref_mutex);
1da177e4
LT
220 return floppy;
221}
222
9a24b63d 223static void idefloppy_cleanup_obj(struct kref *);
1da177e4
LT
224
225static void ide_floppy_put(struct ide_floppy_obj *floppy)
226{
cf8b8975 227 mutex_lock(&idefloppy_ref_mutex);
9a24b63d 228 kref_put(&floppy->kref, idefloppy_cleanup_obj);
cf8b8975 229 mutex_unlock(&idefloppy_ref_mutex);
1da177e4
LT
230}
231
1da177e4 232/*
0571c7a4
BP
233 * Used to finish servicing a request. For read/write requests, we will call
234 * ide_end_request to pass to the next buffer.
1da177e4
LT
235 */
236static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
237{
238 idefloppy_floppy_t *floppy = drive->driver_data;
239 struct request *rq = HWGROUP(drive)->rq;
240 int error;
241
bcc77d9c 242 debug_log("Reached %s\n", __func__);
1da177e4
LT
243
244 switch (uptodate) {
0571c7a4
BP
245 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
246 case 1: error = 0; break;
247 default: error = uptodate;
1da177e4
LT
248 }
249 if (error)
250 floppy->failed_pc = NULL;
251 /* Why does this happen? */
252 if (!rq)
253 return 0;
4aff5e23 254 if (!blk_special_request(rq)) {
1da177e4
LT
255 /* our real local end request function */
256 ide_end_request(drive, uptodate, nsecs);
257 return 0;
258 }
259 rq->errors = error;
260 /* fixme: need to move this local also */
261 ide_end_drive_cmd(drive, 0, 0);
262 return 0;
263}
264
32925e1a
BP
265static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc,
266 unsigned int bcount, int direction)
1da177e4
LT
267{
268 struct request *rq = pc->rq;
5705f702 269 struct req_iterator iter;
1da177e4
LT
270 struct bio_vec *bvec;
271 unsigned long flags;
5705f702 272 int count, done = 0;
1da177e4
LT
273 char *data;
274
5705f702 275 rq_for_each_segment(bvec, rq, iter) {
6c92e699
JA
276 if (!bcount)
277 break;
1da177e4 278
6c92e699 279 count = min(bvec->bv_len, bcount);
1da177e4 280
6c92e699 281 data = bvec_kmap_irq(bvec, &flags);
32925e1a
BP
282 if (direction)
283 drive->hwif->atapi_output_bytes(drive, data, count);
284 else
285 drive->hwif->atapi_input_bytes(drive, data, count);
6c92e699 286 bvec_kunmap_irq(data, &flags);
1da177e4 287
6c92e699
JA
288 bcount -= count;
289 pc->b_count += count;
290 done += count;
1da177e4
LT
291 }
292
293 idefloppy_do_end_request(drive, 1, done >> 9);
294
295 if (bcount) {
32925e1a
BP
296 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
297 drive->name, __func__, bcount);
298 if (direction)
7616c0ad 299 ide_atapi_write_zeros(drive, bcount);
32925e1a 300 else
7616c0ad 301 ide_atapi_discard_data(drive, bcount);
1da177e4
LT
302 }
303}
304
0571c7a4 305static void idefloppy_update_buffers(ide_drive_t *drive, idefloppy_pc_t *pc)
1da177e4
LT
306{
307 struct request *rq = pc->rq;
308 struct bio *bio = rq->bio;
309
310 while ((bio = rq->bio) != NULL)
311 idefloppy_do_end_request(drive, 1, 0);
312}
313
314/*
0571c7a4
BP
315 * Generate a new packet command request in front of the request queue, before
316 * the current request so that it will be processed immediately, on the next
317 * pass through the driver.
1da177e4 318 */
0571c7a4
BP
319static void idefloppy_queue_pc_head(ide_drive_t *drive, idefloppy_pc_t *pc,
320 struct request *rq)
1da177e4
LT
321{
322 struct ide_floppy_obj *floppy = drive->driver_data;
323
324 ide_init_drive_cmd(rq);
325 rq->buffer = (char *) pc;
4aff5e23 326 rq->cmd_type = REQ_TYPE_SPECIAL;
1da177e4
LT
327 rq->rq_disk = floppy->disk;
328 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
329}
330
0571c7a4 331static idefloppy_pc_t *idefloppy_next_pc_storage(ide_drive_t *drive)
1da177e4
LT
332{
333 idefloppy_floppy_t *floppy = drive->driver_data;
334
335 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
0571c7a4 336 floppy->pc_stack_index = 0;
1da177e4
LT
337 return (&floppy->pc_stack[floppy->pc_stack_index++]);
338}
339
0571c7a4 340static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
1da177e4
LT
341{
342 idefloppy_floppy_t *floppy = drive->driver_data;
343
344 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
345 floppy->rq_stack_index = 0;
346 return (&floppy->rq_stack[floppy->rq_stack_index++]);
347}
348
a6ff2d3b 349static void idefloppy_request_sense_callback(ide_drive_t *drive)
1da177e4
LT
350{
351 idefloppy_floppy_t *floppy = drive->driver_data;
a6ff2d3b 352 u8 *buf = floppy->pc->buffer;
1da177e4 353
bcc77d9c
BP
354 debug_log("Reached %s\n", __func__);
355
1da177e4 356 if (!floppy->pc->error) {
a6ff2d3b
BP
357 floppy->sense_key = buf[2] & 0x0F;
358 floppy->asc = buf[12];
359 floppy->ascq = buf[13];
360 floppy->progress_indication = buf[15] & 0x80 ?
361 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
362
363 if (floppy->failed_pc)
364 debug_log("pc = %x, sense key = %x, asc = %x,"
365 " ascq = %x\n",
366 floppy->failed_pc->c[0],
367 floppy->sense_key,
368 floppy->asc,
369 floppy->ascq);
370 else
371 debug_log("sense key = %x, asc = %x, ascq = %x\n",
372 floppy->sense_key,
373 floppy->asc,
374 floppy->ascq);
375
376
1da177e4
LT
377 idefloppy_do_end_request(drive, 1, 0);
378 } else {
a6ff2d3b
BP
379 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
380 " request!\n");
1da177e4
LT
381 idefloppy_do_end_request(drive, 0, 0);
382 }
383}
384
0571c7a4
BP
385/* General packet command callback function. */
386static void idefloppy_pc_callback(ide_drive_t *drive)
1da177e4
LT
387{
388 idefloppy_floppy_t *floppy = drive->driver_data;
bcc77d9c
BP
389
390 debug_log("Reached %s\n", __func__);
1da177e4
LT
391
392 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
393}
394
0571c7a4 395static void idefloppy_init_pc(idefloppy_pc_t *pc)
1da177e4
LT
396{
397 memset(pc->c, 0, 12);
398 pc->retries = 0;
399 pc->flags = 0;
400 pc->request_transfer = 0;
401 pc->buffer = pc->pc_buffer;
402 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
403 pc->callback = &idefloppy_pc_callback;
404}
405
0571c7a4 406static void idefloppy_create_request_sense_cmd(idefloppy_pc_t *pc)
1da177e4 407{
30d67099
BP
408 idefloppy_init_pc(pc);
409 pc->c[0] = GPCMD_REQUEST_SENSE;
1da177e4
LT
410 pc->c[4] = 255;
411 pc->request_transfer = 18;
412 pc->callback = &idefloppy_request_sense_callback;
413}
414
415/*
0571c7a4
BP
416 * Called when an error was detected during the last packet command. We queue a
417 * request sense packet command in the head of the request list.
1da177e4 418 */
0571c7a4 419static void idefloppy_retry_pc(ide_drive_t *drive)
1da177e4
LT
420{
421 idefloppy_pc_t *pc;
422 struct request *rq;
1da177e4 423
64a57fe4 424 (void)ide_read_error(drive);
1da177e4
LT
425 pc = idefloppy_next_pc_storage(drive);
426 rq = idefloppy_next_rq_storage(drive);
427 idefloppy_create_request_sense_cmd(pc);
428 idefloppy_queue_pc_head(drive, pc, rq);
429}
430
0eea6458 431/* The usual interrupt handler called during a packet command. */
1da177e4
LT
432static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
433{
434 idefloppy_floppy_t *floppy = drive->driver_data;
790d1239 435 ide_hwif_t *hwif = drive->hwif;
1da177e4
LT
436 idefloppy_pc_t *pc = floppy->pc;
437 struct request *rq = pc->rq;
0eea6458 438 xfer_func_t *xferfunc;
1da177e4 439 unsigned int temp;
d30a7fba 440 int dma_error = 0;
790d1239 441 u16 bcount;
8e7657ae 442 u8 stat, ireason;
1da177e4 443
bcc77d9c 444 debug_log("Reached %s interrupt handler\n", __func__);
1da177e4 445
6e5fa7b8 446 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
d30a7fba
BP
447 dma_error = hwif->ide_dma_end(drive);
448 if (dma_error) {
449 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
450 rq_data_dir(rq) ? "write" : "read");
6e5fa7b8 451 pc->flags |= PC_FLAG_DMA_ERROR;
1da177e4
LT
452 } else {
453 pc->actually_transferred = pc->request_transfer;
454 idefloppy_update_buffers(drive, pc);
455 }
bcc77d9c 456 debug_log("DMA finished\n");
1da177e4
LT
457 }
458
459 /* Clear the interrupt */
c47137a9 460 stat = ide_read_status(drive);
1da177e4 461
0571c7a4
BP
462 /* No more interrupts */
463 if ((stat & DRQ_STAT) == 0) {
bcc77d9c
BP
464 debug_log("Packet command completed, %d bytes transferred\n",
465 pc->actually_transferred);
6e5fa7b8 466 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
1da177e4 467
366c7f55 468 local_irq_enable_in_hardirq();
1da177e4 469
6e5fa7b8 470 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
1da177e4 471 /* Error detected */
bcc77d9c 472 debug_log("%s: I/O error\n", drive->name);
1da177e4 473 rq->errors++;
30d67099 474 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
1da177e4
LT
475 printk(KERN_ERR "ide-floppy: I/O error in "
476 "request sense command\n");
477 return ide_do_reset(drive);
478 }
479 /* Retry operation */
480 idefloppy_retry_pc(drive);
481 /* queued, but not started */
482 return ide_stopped;
483 }
484 pc->error = 0;
485 if (floppy->failed_pc == pc)
486 floppy->failed_pc = NULL;
487 /* Command finished - Call the callback function */
488 pc->callback(drive);
489 return ide_stopped;
490 }
491
6e5fa7b8
BP
492 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
493 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
1da177e4
LT
494 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
495 "more interrupts in DMA mode\n");
7469aaf6 496 ide_dma_off(drive);
1da177e4
LT
497 return ide_do_reset(drive);
498 }
499
500 /* Get the number of bytes to transfer */
790d1239
BZ
501 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
502 hwif->INB(IDE_BCOUNTL_REG);
1da177e4 503 /* on this interrupt */
8e7657ae 504 ireason = hwif->INB(IDE_IREASON_REG);
1da177e4 505
8e7657ae 506 if (ireason & CD) {
0eea6458 507 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
1da177e4
LT
508 return ide_do_reset(drive);
509 }
6e5fa7b8 510 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
1da177e4
LT
511 /* Hopefully, we will never get here */
512 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
8e7657ae 513 (ireason & IO) ? "Write" : "Read");
1da177e4 514 printk(KERN_ERR "but the floppy wants us to %s !\n",
8e7657ae 515 (ireason & IO) ? "Read" : "Write");
1da177e4
LT
516 return ide_do_reset(drive);
517 }
6e5fa7b8 518 if (!(pc->flags & PC_FLAG_WRITING)) {
1da177e4 519 /* Reading - Check that we have enough space */
790d1239 520 temp = pc->actually_transferred + bcount;
1da177e4
LT
521 if (temp > pc->request_transfer) {
522 if (temp > pc->buffer_size) {
523 printk(KERN_ERR "ide-floppy: The floppy wants "
524 "to send us more data than expected "
525 "- discarding data\n");
7616c0ad 526 ide_atapi_discard_data(drive, bcount);
0078df2e 527
1da177e4
LT
528 ide_set_handler(drive,
529 &idefloppy_pc_intr,
530 IDEFLOPPY_WAIT_CMD,
531 NULL);
532 return ide_started;
533 }
bcc77d9c
BP
534 debug_log("The floppy wants to send us more data than"
535 " expected - allowing transfer\n");
1da177e4
LT
536 }
537 }
6e5fa7b8 538 if (pc->flags & PC_FLAG_WRITING)
0eea6458 539 xferfunc = hwif->atapi_output_bytes;
32925e1a 540 else
0eea6458 541 xferfunc = hwif->atapi_input_bytes;
0eea6458
BP
542
543 if (pc->buffer)
544 xferfunc(drive, pc->current_position, bcount);
545 else
32925e1a 546 ide_floppy_io_buffers(drive, pc, bcount,
6e5fa7b8 547 !!(pc->flags & PC_FLAG_WRITING));
0eea6458 548
1da177e4 549 /* Update the current position */
790d1239
BZ
550 pc->actually_transferred += bcount;
551 pc->current_position += bcount;
1da177e4 552
32925e1a
BP
553 /* And set the interrupt handler again */
554 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
1da177e4
LT
555 return ide_started;
556}
557
558/*
559 * This is the original routine that did the packet transfer.
560 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
561 * for that drive below. The algorithm is chosen based on drive type
562 */
0571c7a4 563static ide_startstop_t idefloppy_transfer_pc(ide_drive_t *drive)
1da177e4
LT
564{
565 ide_startstop_t startstop;
566 idefloppy_floppy_t *floppy = drive->driver_data;
8e7657ae 567 u8 ireason;
1da177e4
LT
568
569 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
570 printk(KERN_ERR "ide-floppy: Strange, packet command "
571 "initiated yet DRQ isn't asserted\n");
572 return startstop;
573 }
8e7657ae
BZ
574 ireason = drive->hwif->INB(IDE_IREASON_REG);
575 if ((ireason & CD) == 0 || (ireason & IO)) {
1da177e4
LT
576 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
577 "issuing a packet command\n");
578 return ide_do_reset(drive);
579 }
0078df2e 580
1da177e4
LT
581 /* Set the interrupt routine */
582 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
583 /* Send the actual packet */
584 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
585 return ide_started;
586}
587
588
589/*
0571c7a4
BP
590 * What we have here is a classic case of a top half / bottom half interrupt
591 * service routine. In interrupt mode, the device sends an interrupt to signal
592 * that it is ready to receive a packet. However, we need to delay about 2-3
593 * ticks before issuing the packet or we gets in trouble.
1da177e4 594 *
0571c7a4
BP
595 * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
596 * In either case, when the device says it's ready for a packet, we schedule
597 * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
1da177e4 598 */
0571c7a4 599static int idefloppy_transfer_pc2(ide_drive_t *drive)
1da177e4
LT
600{
601 idefloppy_floppy_t *floppy = drive->driver_data;
602
603 /* Send the actual packet */
604 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
605 /* Timeout for the packet command */
606 return IDEFLOPPY_WAIT_CMD;
607}
608
0571c7a4 609static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive)
1da177e4
LT
610{
611 idefloppy_floppy_t *floppy = drive->driver_data;
612 ide_startstop_t startstop;
8e7657ae 613 u8 ireason;
1da177e4
LT
614
615 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
616 printk(KERN_ERR "ide-floppy: Strange, packet command "
617 "initiated yet DRQ isn't asserted\n");
618 return startstop;
619 }
8e7657ae
BZ
620 ireason = drive->hwif->INB(IDE_IREASON_REG);
621 if ((ireason & CD) == 0 || (ireason & IO)) {
1da177e4
LT
622 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
623 "while issuing a packet command\n");
624 return ide_do_reset(drive);
625 }
0571c7a4 626 /*
1da177e4
LT
627 * The following delay solves a problem with ATAPI Zip 100 drives
628 * where the Busy flag was apparently being deasserted before the
629 * unit was ready to receive data. This was happening on a
630 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
631 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
632 * used until after the packet is moved in about 50 msec.
633 */
0078df2e 634
0571c7a4
BP
635 ide_set_handler(drive, &idefloppy_pc_intr, floppy->ticks,
636 &idefloppy_transfer_pc2);
1da177e4
LT
637 return ide_started;
638}
639
d652c138
BP
640static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
641 idefloppy_pc_t *pc)
1da177e4 642{
d652c138 643 /* supress error messages resulting from Medium not present */
1da177e4
LT
644 if (floppy->sense_key == 0x02 &&
645 floppy->asc == 0x3a &&
646 floppy->ascq == 0x00)
d652c138
BP
647 return;
648
649 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
650 "asc = %2x, ascq = %2x\n",
651 floppy->drive->name, pc->c[0], floppy->sense_key,
652 floppy->asc, floppy->ascq);
653
1da177e4
LT
654}
655
0571c7a4
BP
656static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
657 idefloppy_pc_t *pc)
1da177e4
LT
658{
659 idefloppy_floppy_t *floppy = drive->driver_data;
660 ide_hwif_t *hwif = drive->hwif;
1da177e4 661 ide_handler_t *pkt_xfer_routine;
790d1239 662 u16 bcount;
e5f9f5a8 663 u8 dma;
1da177e4 664
1da177e4 665 if (floppy->failed_pc == NULL &&
30d67099 666 pc->c[0] != GPCMD_REQUEST_SENSE)
1da177e4
LT
667 floppy->failed_pc = pc;
668 /* Set the current packet command */
669 floppy->pc = pc;
670
757ced89 671 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
6e5fa7b8 672 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
757ced89
BP
673 ide_floppy_report_error(floppy, pc);
674 /* Giving up */
675 pc->error = IDEFLOPPY_ERROR_GENERAL;
676
1da177e4
LT
677 floppy->failed_pc = NULL;
678 pc->callback(drive);
679 return ide_stopped;
680 }
681
bcc77d9c 682 debug_log("Retry number - %d\n", pc->retries);
1da177e4
LT
683
684 pc->retries++;
685 /* We haven't transferred any data yet */
686 pc->actually_transferred = 0;
687 pc->current_position = pc->buffer;
790d1239 688 bcount = min(pc->request_transfer, 63 * 1024);
1da177e4 689
6e5fa7b8
BP
690 if (pc->flags & PC_FLAG_DMA_ERROR) {
691 pc->flags &= ~PC_FLAG_DMA_ERROR;
7469aaf6 692 ide_dma_off(drive);
6e5fa7b8 693 }
e5f9f5a8 694 dma = 0;
1da177e4 695
6e5fa7b8 696 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
e5f9f5a8 697 dma = !hwif->dma_setup(drive);
1da177e4 698
2fc57388
BZ
699 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
700 IDE_TFLAG_OUT_DEVICE, bcount, dma);
1da177e4 701
0571c7a4
BP
702 if (dma) {
703 /* Begin DMA, if necessary */
6e5fa7b8 704 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
1da177e4
LT
705 hwif->dma_start(drive);
706 }
707
708 /* Can we transfer the packet when we get the interrupt or wait? */
6e5fa7b8 709 if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) {
1da177e4
LT
710 /* wait */
711 pkt_xfer_routine = &idefloppy_transfer_pc1;
712 } else {
713 /* immediate */
714 pkt_xfer_routine = &idefloppy_transfer_pc;
715 }
716
6e5fa7b8 717 if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) {
1da177e4
LT
718 /* Issue the packet command */
719 ide_execute_command(drive, WIN_PACKETCMD,
720 pkt_xfer_routine,
721 IDEFLOPPY_WAIT_CMD,
722 NULL);
723 return ide_started;
724 } else {
725 /* Issue the packet command */
726 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
727 return (*pkt_xfer_routine) (drive);
728 }
729}
730
0571c7a4 731static void idefloppy_rw_callback(ide_drive_t *drive)
1da177e4 732{
bcc77d9c 733 debug_log("Reached %s\n", __func__);
1da177e4
LT
734
735 idefloppy_do_end_request(drive, 1, 0);
736 return;
737}
738
0571c7a4 739static void idefloppy_create_prevent_cmd(idefloppy_pc_t *pc, int prevent)
1da177e4 740{
bcc77d9c 741 debug_log("creating prevent removal command, prevent = %d\n", prevent);
1da177e4
LT
742
743 idefloppy_init_pc(pc);
30d67099 744 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1da177e4
LT
745 pc->c[4] = prevent;
746}
747
0571c7a4 748static void idefloppy_create_read_capacity_cmd(idefloppy_pc_t *pc)
1da177e4
LT
749{
750 idefloppy_init_pc(pc);
30d67099 751 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
1da177e4
LT
752 pc->c[7] = 255;
753 pc->c[8] = 255;
754 pc->request_transfer = 255;
755}
756
0571c7a4 757static void idefloppy_create_format_unit_cmd(idefloppy_pc_t *pc, int b, int l,
1da177e4
LT
758 int flags)
759{
760 idefloppy_init_pc(pc);
30d67099 761 pc->c[0] = GPCMD_FORMAT_UNIT;
1da177e4
LT
762 pc->c[1] = 0x17;
763
764 memset(pc->buffer, 0, 12);
765 pc->buffer[1] = 0xA2;
766 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
767
768 if (flags & 1) /* Verify bit on... */
769 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
770 pc->buffer[3] = 8;
771
8f622430
BP
772 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
773 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
0571c7a4 774 pc->buffer_size = 12;
6e5fa7b8 775 pc->flags |= PC_FLAG_WRITING;
1da177e4
LT
776}
777
0571c7a4
BP
778/* A mode sense command is used to "sense" floppy parameters. */
779static void idefloppy_create_mode_sense_cmd(idefloppy_pc_t *pc, u8 page_code,
780 u8 type)
1da177e4 781{
24a5d703 782 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
0571c7a4 783
1da177e4 784 idefloppy_init_pc(pc);
30d67099 785 pc->c[0] = GPCMD_MODE_SENSE_10;
1da177e4
LT
786 pc->c[1] = 0;
787 pc->c[2] = page_code + (type << 6);
788
789 switch (page_code) {
0571c7a4
BP
790 case IDEFLOPPY_CAPABILITIES_PAGE:
791 length += 12;
792 break;
793 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
794 length += 32;
795 break;
796 default:
797 printk(KERN_ERR "ide-floppy: unsupported page code "
1da177e4
LT
798 "in create_mode_sense_cmd\n");
799 }
8f622430 800 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
1da177e4
LT
801 pc->request_transfer = length;
802}
803
0571c7a4 804static void idefloppy_create_start_stop_cmd(idefloppy_pc_t *pc, int start)
1da177e4
LT
805{
806 idefloppy_init_pc(pc);
30d67099 807 pc->c[0] = GPCMD_START_STOP_UNIT;
1da177e4
LT
808 pc->c[4] = start;
809}
810
811static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
812{
813 idefloppy_init_pc(pc);
30d67099 814 pc->c[0] = GPCMD_TEST_UNIT_READY;
1da177e4
LT
815}
816
ae7e8ddc
BP
817static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
818 idefloppy_pc_t *pc, struct request *rq,
819 unsigned long sector)
1da177e4
LT
820{
821 int block = sector / floppy->bs_factor;
822 int blocks = rq->nr_sectors / floppy->bs_factor;
823 int cmd = rq_data_dir(rq);
824
ae7e8ddc 825 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
1da177e4
LT
826 block, blocks);
827
828 idefloppy_init_pc(pc);
ae7e8ddc
BP
829 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
830 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
8f622430 831 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
ae7e8ddc 832
1da177e4
LT
833 pc->callback = &idefloppy_rw_callback;
834 pc->rq = rq;
835 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
4aff5e23 836 if (rq->cmd_flags & REQ_RW)
6e5fa7b8 837 pc->flags |= PC_FLAG_WRITING;
1da177e4
LT
838 pc->buffer = NULL;
839 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
6e5fa7b8 840 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
1da177e4
LT
841}
842
0571c7a4
BP
843static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
844 idefloppy_pc_t *pc, struct request *rq)
1da177e4 845{
1da177e4 846 idefloppy_init_pc(pc);
3d6392cf 847 pc->callback = &idefloppy_rw_callback;
1da177e4 848 memcpy(pc->c, rq->cmd, sizeof(pc->c));
3d6392cf
JA
849 pc->rq = rq;
850 pc->b_count = rq->data_len;
851 if (rq->data_len && rq_data_dir(rq) == WRITE)
6e5fa7b8 852 pc->flags |= PC_FLAG_WRITING;
3d6392cf
JA
853 pc->buffer = rq->data;
854 if (rq->bio)
6e5fa7b8 855 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
3d6392cf
JA
856 /*
857 * possibly problematic, doesn't look like ide-floppy correctly
858 * handled scattered requests if dma fails...
859 */
860 pc->request_transfer = pc->buffer_size = rq->data_len;
1da177e4
LT
861}
862
0571c7a4
BP
863static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
864 struct request *rq, sector_t block_s)
1da177e4
LT
865{
866 idefloppy_floppy_t *floppy = drive->driver_data;
867 idefloppy_pc_t *pc;
868 unsigned long block = (unsigned long)block_s;
869
bcc77d9c 870 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
18cddac3 871 rq->rq_disk ? rq->rq_disk->disk_name : "?",
bcc77d9c
BP
872 rq->cmd_type, rq->errors);
873 debug_log("sector: %ld, nr_sectors: %ld, "
1da177e4
LT
874 "current_nr_sectors: %d\n", (long)rq->sector,
875 rq->nr_sectors, rq->current_nr_sectors);
876
877 if (rq->errors >= ERROR_MAX) {
d652c138
BP
878 if (floppy->failed_pc)
879 ide_floppy_report_error(floppy, floppy->failed_pc);
1da177e4
LT
880 else
881 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
882 drive->name);
883 idefloppy_do_end_request(drive, 0, 0);
884 return ide_stopped;
885 }
4aff5e23 886 if (blk_fs_request(rq)) {
1da177e4
LT
887 if (((long)rq->sector % floppy->bs_factor) ||
888 (rq->nr_sectors % floppy->bs_factor)) {
0571c7a4
BP
889 printk(KERN_ERR "%s: unsupported r/w request size\n",
890 drive->name);
1da177e4
LT
891 idefloppy_do_end_request(drive, 0, 0);
892 return ide_stopped;
893 }
894 pc = idefloppy_next_pc_storage(drive);
895 idefloppy_create_rw_cmd(floppy, pc, rq, block);
4aff5e23 896 } else if (blk_special_request(rq)) {
1da177e4 897 pc = (idefloppy_pc_t *) rq->buffer;
4aff5e23 898 } else if (blk_pc_request(rq)) {
1da177e4 899 pc = idefloppy_next_pc_storage(drive);
3d6392cf 900 idefloppy_blockpc_cmd(floppy, pc, rq);
1da177e4
LT
901 } else {
902 blk_dump_rq_flags(rq,
903 "ide-floppy: unsupported command in queue");
904 idefloppy_do_end_request(drive, 0, 0);
905 return ide_stopped;
906 }
907
908 pc->rq = rq;
909 return idefloppy_issue_pc(drive, pc);
910}
911
912/*
0571c7a4
BP
913 * Add a special packet command request to the tail of the request queue,
914 * and wait for it to be serviced.
1da177e4 915 */
0571c7a4 916static int idefloppy_queue_pc_tail(ide_drive_t *drive, idefloppy_pc_t *pc)
1da177e4
LT
917{
918 struct ide_floppy_obj *floppy = drive->driver_data;
919 struct request rq;
920
0571c7a4 921 ide_init_drive_cmd(&rq);
1da177e4 922 rq.buffer = (char *) pc;
4aff5e23 923 rq.cmd_type = REQ_TYPE_SPECIAL;
1da177e4
LT
924 rq.rq_disk = floppy->disk;
925
926 return ide_do_drive_cmd(drive, &rq, ide_wait);
927}
928
929/*
8e81bbba
BP
930 * Look at the flexible disk page parameters. We ignore the CHS capacity
931 * parameters and use the LBA parameters instead.
1da177e4 932 */
8e81bbba 933static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
1da177e4
LT
934{
935 idefloppy_floppy_t *floppy = drive->driver_data;
936 idefloppy_pc_t pc;
8e81bbba 937 u8 *page;
1da177e4 938 int capacity, lba_capacity;
8e81bbba
BP
939 u16 transfer_rate, sector_size, cyls, rpm;
940 u8 heads, sectors;
1da177e4 941
8e81bbba
BP
942 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
943 MODE_SENSE_CURRENT);
944
945 if (idefloppy_queue_pc_tail(drive, &pc)) {
946 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
947 " parameters\n");
1da177e4
LT
948 return 1;
949 }
24a5d703 950 floppy->wp = !!(pc.buffer[3] & 0x80);
1da177e4 951 set_disk_ro(floppy->disk, floppy->wp);
8e81bbba
BP
952 page = &pc.buffer[8];
953
954 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
955 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
956 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
957 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
958 heads = pc.buffer[8 + 4];
959 sectors = pc.buffer[8 + 5];
960
961 capacity = cyls * heads * sectors * sector_size;
962
963 if (memcmp(page, &floppy->flexible_disk_page, 32))
1da177e4
LT
964 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
965 "%d sector size, %d rpm\n",
8e81bbba
BP
966 drive->name, capacity / 1024, cyls, heads,
967 sectors, transfer_rate / 8, sector_size, rpm);
968
969 memcpy(&floppy->flexible_disk_page, page, 32);
970 drive->bios_cyl = cyls;
971 drive->bios_head = heads;
972 drive->bios_sect = sectors;
1da177e4 973 lba_capacity = floppy->blocks * floppy->block_size;
8e81bbba 974
1da177e4
LT
975 if (capacity < lba_capacity) {
976 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
977 "bytes, but the drive only handles %d\n",
978 drive->name, lba_capacity, capacity);
8e81bbba
BP
979 floppy->blocks = floppy->block_size ?
980 capacity / floppy->block_size : 0;
1da177e4
LT
981 }
982 return 0;
983}
984
948391d1 985static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
1da177e4
LT
986{
987 idefloppy_floppy_t *floppy = drive->driver_data;
988 idefloppy_pc_t pc;
1da177e4
LT
989
990 floppy->srfp = 0;
991 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
992 MODE_SENSE_CURRENT);
993
6e5fa7b8 994 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
948391d1 995 if (idefloppy_queue_pc_tail(drive, &pc))
1da177e4 996 return 1;
1da177e4 997
948391d1 998 floppy->srfp = pc.buffer[8 + 2] & 0x40;
1da177e4
LT
999 return (0);
1000}
1001
1002/*
194ec0c0
BP
1003 * Determine if a media is present in the floppy drive, and if so, its LBA
1004 * capacity.
1da177e4 1005 */
194ec0c0 1006static int ide_floppy_get_capacity(ide_drive_t *drive)
1da177e4
LT
1007{
1008 idefloppy_floppy_t *floppy = drive->driver_data;
1009 idefloppy_pc_t pc;
194ec0c0
BP
1010 u8 *cap_desc;
1011 u8 header_len, desc_cnt;
1012 int i, rc = 1, blocks, length;
1013
1da177e4
LT
1014 drive->bios_cyl = 0;
1015 drive->bios_head = drive->bios_sect = 0;
fdb77da4
AC
1016 floppy->blocks = 0;
1017 floppy->bs_factor = 1;
1da177e4
LT
1018 set_capacity(floppy->disk, 0);
1019
1020 idefloppy_create_read_capacity_cmd(&pc);
1021 if (idefloppy_queue_pc_tail(drive, &pc)) {
1022 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1023 return 1;
1024 }
194ec0c0
BP
1025 header_len = pc.buffer[3];
1026 cap_desc = &pc.buffer[4];
1027 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1028
1029 for (i = 0; i < desc_cnt; i++) {
1030 unsigned int desc_start = 4 + i*8;
1031
1032 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1033 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1034
1035 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1036 i, blocks * length / 1024, blocks, length);
1da177e4 1037
194ec0c0
BP
1038 if (i)
1039 continue;
1040 /*
1041 * the code below is valid only for the 1st descriptor, ie i=0
1042 */
1da177e4 1043
194ec0c0 1044 switch (pc.buffer[desc_start + 4] & 0x03) {
1da177e4
LT
1045 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1046 case CAPACITY_UNFORMATTED:
6e5fa7b8 1047 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
0571c7a4 1048 /*
1da177e4
LT
1049 * If it is not a clik drive, break out
1050 * (maintains previous driver behaviour)
1051 */
1052 break;
1053 case CAPACITY_CURRENT:
1054 /* Normal Zip/LS-120 disks */
194ec0c0 1055 if (memcmp(cap_desc, &floppy->cap_desc, 8))
1da177e4
LT
1056 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1057 "sector size\n", drive->name,
1058 blocks * length / 1024, blocks, length);
194ec0c0
BP
1059 memcpy(&floppy->cap_desc, cap_desc, 8);
1060
1da177e4
LT
1061 if (!length || length % 512) {
1062 printk(KERN_NOTICE "%s: %d bytes block size "
1063 "not supported\n", drive->name, length);
1064 } else {
194ec0c0
BP
1065 floppy->blocks = blocks;
1066 floppy->block_size = length;
1067 floppy->bs_factor = length / 512;
1068 if (floppy->bs_factor != 1)
1069 printk(KERN_NOTICE "%s: warning: non "
1da177e4
LT
1070 "512 bytes block size not "
1071 "fully supported\n",
1072 drive->name);
194ec0c0 1073 rc = 0;
1da177e4
LT
1074 }
1075 break;
1076 case CAPACITY_NO_CARTRIDGE:
1077 /*
1078 * This is a KERN_ERR so it appears on screen
1079 * for the user to see
1080 */
1081 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1082 break;
1083 case CAPACITY_INVALID:
1084 printk(KERN_ERR "%s: Invalid capacity for disk "
1085 "in drive\n", drive->name);
1086 break;
1087 }
194ec0c0
BP
1088 debug_log("Descriptor 0 Code: %d\n",
1089 pc.buffer[desc_start + 4] & 0x03);
1da177e4
LT
1090 }
1091
1092 /* Clik! disk does not support get_flexible_disk_page */
6e5fa7b8 1093 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
8e81bbba 1094 (void) ide_floppy_get_flexible_disk_page(drive);
1da177e4
LT
1095
1096 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1097 return rc;
1098}
1099
1100/*
194ec0c0
BP
1101 * Obtain the list of formattable capacities.
1102 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1103 * descriptors to userland, instead of our own structures.
1104 *
1105 * Userland gives us the following structure:
1106 *
1107 * struct idefloppy_format_capacities {
1108 * int nformats;
1109 * struct {
1110 * int nblocks;
1111 * int blocksize;
1112 * } formats[];
1113 * };
1114 *
1115 * userland initializes nformats to the number of allocated formats[] records.
1116 * On exit we set nformats to the number of records we've actually initialized.
1117 */
1da177e4 1118
194ec0c0 1119static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1da177e4 1120{
194ec0c0
BP
1121 idefloppy_pc_t pc;
1122 u8 header_len, desc_cnt;
1123 int i, blocks, length, u_array_size, u_index;
1da177e4
LT
1124 int __user *argp;
1125
1126 if (get_user(u_array_size, arg))
1127 return (-EFAULT);
1128
1129 if (u_array_size <= 0)
1130 return (-EINVAL);
1131
1132 idefloppy_create_read_capacity_cmd(&pc);
1133 if (idefloppy_queue_pc_tail(drive, &pc)) {
1134 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
194ec0c0
BP
1135 return (-EIO);
1136 }
1137 header_len = pc.buffer[3];
1138 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1da177e4
LT
1139
1140 u_index = 0;
1141 argp = arg + 1;
1142
1143 /*
194ec0c0
BP
1144 * We always skip the first capacity descriptor. That's the current
1145 * capacity. We are interested in the remaining descriptors, the
1146 * formattable capacities.
1147 */
1148 for (i = 1; i < desc_cnt; i++) {
1149 unsigned int desc_start = 4 + i*8;
1da177e4 1150
1da177e4
LT
1151 if (u_index >= u_array_size)
1152 break; /* User-supplied buffer too small */
1da177e4 1153
194ec0c0
BP
1154 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1155 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1da177e4
LT
1156
1157 if (put_user(blocks, argp))
1158 return(-EFAULT);
1159 ++argp;
1160
1161 if (put_user(length, argp))
1162 return (-EFAULT);
1163 ++argp;
1164
1165 ++u_index;
1166 }
1167
1168 if (put_user(u_index, arg))
1169 return (-EFAULT);
1170 return (0);
1171}
1172
1da177e4 1173/*
0571c7a4
BP
1174 * Get ATAPI_FORMAT_UNIT progress indication.
1175 *
1176 * Userland gives a pointer to an int. The int is set to a progress
1177 * indicator 0-65536, with 65536=100%.
1178 *
1179 * If the drive does not support format progress indication, we just check
1180 * the dsc bit, and return either 0 or 65536.
1181 */
1da177e4
LT
1182
1183static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1184{
1185 idefloppy_floppy_t *floppy = drive->driver_data;
1186 idefloppy_pc_t pc;
1187 int progress_indication = 0x10000;
1188
1189 if (floppy->srfp) {
1190 idefloppy_create_request_sense_cmd(&pc);
0571c7a4 1191 if (idefloppy_queue_pc_tail(drive, &pc))
1da177e4 1192 return (-EIO);
1da177e4
LT
1193
1194 if (floppy->sense_key == 2 &&
1195 floppy->asc == 4 &&
0571c7a4 1196 floppy->ascq == 4)
1da177e4 1197 progress_indication = floppy->progress_indication;
0571c7a4
BP
1198
1199 /* Else assume format_unit has finished, and we're at 0x10000 */
1da177e4 1200 } else {
1da177e4 1201 unsigned long flags;
22c525b9 1202 u8 stat;
1da177e4
LT
1203
1204 local_irq_save(flags);
c47137a9 1205 stat = ide_read_status(drive);
1da177e4
LT
1206 local_irq_restore(flags);
1207
22c525b9 1208 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1da177e4
LT
1209 }
1210 if (put_user(progress_indication, arg))
1211 return (-EFAULT);
1212
1213 return (0);
1214}
1215
0571c7a4 1216static sector_t idefloppy_capacity(ide_drive_t *drive)
1da177e4
LT
1217{
1218 idefloppy_floppy_t *floppy = drive->driver_data;
1219 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1220
1221 return capacity;
1222}
1223
1224/*
f373bd82
BP
1225 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1226 * results.
1da177e4 1227 */
f373bd82 1228static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
1da177e4 1229{
3ad6776c
BP
1230 u8 gcw[2];
1231 u8 device_type, protocol, removable, drq_type, packet_size;
1da177e4
LT
1232
1233 *((u16 *) &gcw) = id->config;
1234
3ad6776c
BP
1235 device_type = gcw[1] & 0x1F;
1236 removable = (gcw[0] & 0x80) >> 7;
1237 protocol = (gcw[1] & 0xC0) >> 6;
1238 drq_type = (gcw[0] & 0x60) >> 5;
1239 packet_size = gcw[0] & 0x03;
1240
1da177e4
LT
1241#ifdef CONFIG_PPC
1242 /* kludge for Apple PowerBook internal zip */
3ad6776c
BP
1243 if (device_type == 5 &&
1244 !strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP"))
1245 device_type = 0;
1da177e4
LT
1246#endif
1247
3ad6776c 1248 if (protocol != 2)
f373bd82 1249 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
3ad6776c
BP
1250 protocol);
1251 else if (device_type != 0)
f373bd82 1252 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
3ad6776c
BP
1253 "to floppy\n", device_type);
1254 else if (!removable)
1da177e4 1255 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
3ad6776c 1256 else if (drq_type == 3)
f373bd82 1257 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
3ad6776c
BP
1258 "supported\n", drq_type);
1259 else if (packet_size != 0)
f373bd82 1260 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
3ad6776c
BP
1261 "bytes\n", packet_size);
1262 else
1da177e4
LT
1263 return 1;
1264 return 0;
1265}
1266
7662d046 1267#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
1268static void idefloppy_add_settings(ide_drive_t *drive)
1269{
1270 idefloppy_floppy_t *floppy = drive->driver_data;
1271
0571c7a4
BP
1272 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
1273 &drive->bios_cyl, NULL);
1274 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1275 &drive->bios_head, NULL);
1276 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
1277 &drive->bios_sect, NULL);
1278 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1279 &floppy->ticks, NULL);
1da177e4 1280}
7662d046
BZ
1281#else
1282static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1283#endif
1da177e4 1284
0571c7a4 1285static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1da177e4 1286{
3ad6776c 1287 u8 gcw[2];
1da177e4
LT
1288
1289 *((u16 *) &gcw) = drive->id->config;
1290 floppy->pc = floppy->pc_stack;
3ad6776c
BP
1291
1292 if (((gcw[0] & 0x60) >> 5) == 1)
6e5fa7b8 1293 floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
1da177e4 1294 /*
0571c7a4
BP
1295 * We used to check revisions here. At this point however I'm giving up.
1296 * Just assume they are all broken, its easier.
1da177e4 1297 *
0571c7a4
BP
1298 * The actual reason for the workarounds was likely a driver bug after
1299 * all rather than a firmware bug, and the workaround below used to hide
1300 * it. It should be fixed as of version 1.9, but to be on the safe side
1301 * we'll leave the limitation below for the 2.2.x tree.
1da177e4 1302 */
1da177e4 1303 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
6e5fa7b8 1304 floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
1da177e4
LT
1305 /* This value will be visible in the /proc/ide/hdx/settings */
1306 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1307 blk_queue_max_sectors(drive->queue, 64);
1308 }
1309
1310 /*
0571c7a4
BP
1311 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1312 * nasty clicking noises without it, so please don't remove this.
1313 */
1da177e4
LT
1314 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1315 blk_queue_max_sectors(drive->queue, 64);
6e5fa7b8 1316 floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
1da177e4
LT
1317 }
1318
194ec0c0 1319 (void) ide_floppy_get_capacity(drive);
1da177e4
LT
1320 idefloppy_add_settings(drive);
1321}
1322
4031bbe4 1323static void ide_floppy_remove(ide_drive_t *drive)
1da177e4
LT
1324{
1325 idefloppy_floppy_t *floppy = drive->driver_data;
1326 struct gendisk *g = floppy->disk;
1327
7662d046 1328 ide_proc_unregister_driver(drive, floppy->driver);
1da177e4
LT
1329
1330 del_gendisk(g);
1331
1332 ide_floppy_put(floppy);
1da177e4
LT
1333}
1334
9a24b63d 1335static void idefloppy_cleanup_obj(struct kref *kref)
1da177e4
LT
1336{
1337 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1338 ide_drive_t *drive = floppy->drive;
1339 struct gendisk *g = floppy->disk;
1340
1341 drive->driver_data = NULL;
1342 g->private_data = NULL;
1343 put_disk(g);
1344 kfree(floppy);
1345}
1346
ecfd80e4 1347#ifdef CONFIG_IDE_PROC_FS
0571c7a4
BP
1348static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1349 int count, int *eof, void *data)
1da177e4
LT
1350{
1351 ide_drive_t*drive = (ide_drive_t *)data;
1352 int len;
1353
0571c7a4
BP
1354 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1355 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1da177e4
LT
1356}
1357
1358static ide_proc_entry_t idefloppy_proc[] = {
0571c7a4
BP
1359 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1360 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1da177e4
LT
1361 { NULL, 0, NULL, NULL }
1362};
ecfd80e4 1363#endif /* CONFIG_IDE_PROC_FS */
1da177e4 1364
4031bbe4 1365static int ide_floppy_probe(ide_drive_t *);
1da177e4 1366
1da177e4 1367static ide_driver_t idefloppy_driver = {
8604affd 1368 .gen_driver = {
4ef3b8f4 1369 .owner = THIS_MODULE,
8604affd
BZ
1370 .name = "ide-floppy",
1371 .bus = &ide_bus_type,
8604affd 1372 },
4031bbe4
RK
1373 .probe = ide_floppy_probe,
1374 .remove = ide_floppy_remove,
1da177e4
LT
1375 .version = IDEFLOPPY_VERSION,
1376 .media = ide_floppy,
1da177e4 1377 .supports_dsc_overlap = 0,
1da177e4
LT
1378 .do_request = idefloppy_do_request,
1379 .end_request = idefloppy_do_end_request,
1380 .error = __ide_error,
1381 .abort = __ide_abort,
7662d046 1382#ifdef CONFIG_IDE_PROC_FS
1da177e4 1383 .proc = idefloppy_proc,
7662d046 1384#endif
1da177e4
LT
1385};
1386
1387static int idefloppy_open(struct inode *inode, struct file *filp)
1388{
1389 struct gendisk *disk = inode->i_bdev->bd_disk;
1390 struct ide_floppy_obj *floppy;
1391 ide_drive_t *drive;
1392 idefloppy_pc_t pc;
1393 int ret = 0;
1394
bcc77d9c 1395 debug_log("Reached %s\n", __func__);
1da177e4 1396
0571c7a4
BP
1397 floppy = ide_floppy_get(disk);
1398 if (!floppy)
1da177e4
LT
1399 return -ENXIO;
1400
1401 drive = floppy->drive;
1402
c94964a4 1403 floppy->openers++;
1da177e4 1404
c94964a4 1405 if (floppy->openers == 1) {
6e5fa7b8 1406 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1da177e4
LT
1407 /* Just in case */
1408
1409 idefloppy_create_test_unit_ready_cmd(&pc);
1410 if (idefloppy_queue_pc_tail(drive, &pc)) {
1411 idefloppy_create_start_stop_cmd(&pc, 1);
1412 (void) idefloppy_queue_pc_tail(drive, &pc);
1413 }
1414
194ec0c0 1415 if (ide_floppy_get_capacity(drive)
1da177e4
LT
1416 && (filp->f_flags & O_NDELAY) == 0
1417 /*
0571c7a4
BP
1418 * Allow O_NDELAY to open a drive without a disk, or with an
1419 * unreadable disk, so that we can get the format capacity
1420 * of the drive or begin the format - Sam
1421 */
1da177e4 1422 ) {
1da177e4
LT
1423 ret = -EIO;
1424 goto out_put_floppy;
1425 }
1426
1427 if (floppy->wp && (filp->f_mode & 2)) {
1da177e4
LT
1428 ret = -EROFS;
1429 goto out_put_floppy;
1430 }
6e5fa7b8 1431 floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
1da177e4 1432 /* IOMEGA Clik! drives do not support lock/unlock commands */
6e5fa7b8 1433 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
1da177e4
LT
1434 idefloppy_create_prevent_cmd(&pc, 1);
1435 (void) idefloppy_queue_pc_tail(drive, &pc);
1436 }
1437 check_disk_change(inode->i_bdev);
6e5fa7b8 1438 } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
1da177e4
LT
1439 ret = -EBUSY;
1440 goto out_put_floppy;
1441 }
1442 return 0;
1443
1444out_put_floppy:
c94964a4 1445 floppy->openers--;
1da177e4
LT
1446 ide_floppy_put(floppy);
1447 return ret;
1448}
1449
1450static int idefloppy_release(struct inode *inode, struct file *filp)
1451{
1452 struct gendisk *disk = inode->i_bdev->bd_disk;
1453 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1454 ide_drive_t *drive = floppy->drive;
1455 idefloppy_pc_t pc;
bcc77d9c
BP
1456
1457 debug_log("Reached %s\n", __func__);
1da177e4 1458
c94964a4 1459 if (floppy->openers == 1) {
1da177e4 1460 /* IOMEGA Clik! drives do not support lock/unlock commands */
6e5fa7b8 1461 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
1da177e4
LT
1462 idefloppy_create_prevent_cmd(&pc, 0);
1463 (void) idefloppy_queue_pc_tail(drive, &pc);
1464 }
1465
6e5fa7b8 1466 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1da177e4 1467 }
c94964a4
BZ
1468
1469 floppy->openers--;
1da177e4
LT
1470
1471 ide_floppy_put(floppy);
1472
1473 return 0;
1474}
1475
a885c8c4
CH
1476static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1477{
1478 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1479 ide_drive_t *drive = floppy->drive;
1480
1481 geo->heads = drive->bios_head;
1482 geo->sectors = drive->bios_sect;
1483 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1484 return 0;
1485}
1486
20bf7bda
BP
1487static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
1488 unsigned long arg, unsigned int cmd)
1489{
1490 if (floppy->openers > 1)
1491 return -EBUSY;
1492
1493 /* The IOMEGA Clik! Drive doesn't support this command -
1494 * no room for an eject mechanism */
6e5fa7b8 1495 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
20bf7bda
BP
1496 int prevent = arg ? 1 : 0;
1497
1498 if (cmd == CDROMEJECT)
1499 prevent = 0;
1500
1501 idefloppy_create_prevent_cmd(pc, prevent);
1502 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1503 }
1504
1505 if (cmd == CDROMEJECT) {
1506 idefloppy_create_start_stop_cmd(pc, 2);
1507 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1508 }
1509
1510 return 0;
1511}
1512
1513static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1514 int __user *arg)
1515{
1516 int blocks, length, flags, err = 0;
1517 idefloppy_pc_t pc;
1518
1519 if (floppy->openers > 1) {
1520 /* Don't format if someone is using the disk */
6e5fa7b8 1521 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
20bf7bda
BP
1522 return -EBUSY;
1523 }
1524
6e5fa7b8 1525 floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
20bf7bda
BP
1526
1527 /*
1528 * Send ATAPI_FORMAT_UNIT to the drive.
1529 *
1530 * Userland gives us the following structure:
1531 *
1532 * struct idefloppy_format_command {
1533 * int nblocks;
1534 * int blocksize;
1535 * int flags;
1536 * } ;
1537 *
1538 * flags is a bitmask, currently, the only defined flag is:
1539 *
1540 * 0x01 - verify media after format.
1541 */
1542 if (get_user(blocks, arg) ||
1543 get_user(length, arg+1) ||
1544 get_user(flags, arg+2)) {
1545 err = -EFAULT;
1546 goto out;
1547 }
1548
1549 (void) idefloppy_get_sfrp_bit(floppy->drive);
1550 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1551
1552 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1553 err = -EIO;
1554
1555out:
1556 if (err)
6e5fa7b8 1557 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
20bf7bda
BP
1558 return err;
1559}
1560
1561
1da177e4
LT
1562static int idefloppy_ioctl(struct inode *inode, struct file *file,
1563 unsigned int cmd, unsigned long arg)
1564{
1565 struct block_device *bdev = inode->i_bdev;
1566 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1567 ide_drive_t *drive = floppy->drive;
20bf7bda 1568 idefloppy_pc_t pc;
1da177e4 1569 void __user *argp = (void __user *)arg;
07203f64 1570 int err;
1da177e4
LT
1571
1572 switch (cmd) {
1573 case CDROMEJECT:
1da177e4
LT
1574 /* fall through */
1575 case CDROM_LOCKDOOR:
20bf7bda 1576 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
1da177e4
LT
1577 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1578 return 0;
1579 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
194ec0c0 1580 return ide_floppy_get_format_capacities(drive, argp);
1da177e4 1581 case IDEFLOPPY_IOCTL_FORMAT_START:
1da177e4
LT
1582 if (!(file->f_mode & 2))
1583 return -EPERM;
1584
20bf7bda 1585 return ide_floppy_format_unit(floppy, (int __user *)arg);
1da177e4
LT
1586 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1587 return idefloppy_get_format_progress(drive, argp);
1588 }
89636af2
BZ
1589
1590 /*
1591 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1592 * and CDROM_SEND_PACKET (legacy) ioctls
1593 */
1594 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1595 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1596 bdev->bd_disk, cmd, argp);
1597 else
1598 err = -ENOTTY;
1599
1600 if (err == -ENOTTY)
1601 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1602
1603 return err;
1da177e4
LT
1604}
1605
1606static int idefloppy_media_changed(struct gendisk *disk)
1607{
1608 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1609 ide_drive_t *drive = floppy->drive;
6e5fa7b8 1610 int ret;
1da177e4
LT
1611
1612 /* do not scan partitions twice if this is a removable device */
1613 if (drive->attach) {
1614 drive->attach = 0;
1615 return 0;
1616 }
6e5fa7b8
BP
1617 ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
1618 floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
1619 return ret;
1da177e4
LT
1620}
1621
1622static int idefloppy_revalidate_disk(struct gendisk *disk)
1623{
1624 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1625 set_capacity(disk, idefloppy_capacity(floppy->drive));
1626 return 0;
1627}
1628
1629static struct block_device_operations idefloppy_ops = {
1630 .owner = THIS_MODULE,
1631 .open = idefloppy_open,
1632 .release = idefloppy_release,
1633 .ioctl = idefloppy_ioctl,
a885c8c4 1634 .getgeo = idefloppy_getgeo,
1da177e4
LT
1635 .media_changed = idefloppy_media_changed,
1636 .revalidate_disk= idefloppy_revalidate_disk
1637};
1638
4031bbe4 1639static int ide_floppy_probe(ide_drive_t *drive)
1da177e4
LT
1640{
1641 idefloppy_floppy_t *floppy;
1642 struct gendisk *g;
1643
1644 if (!strstr("ide-floppy", drive->driver_req))
1645 goto failed;
1646 if (!drive->present)
1647 goto failed;
1648 if (drive->media != ide_floppy)
1649 goto failed;
0571c7a4
BP
1650 if (!idefloppy_identify_device(drive, drive->id)) {
1651 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1652 " of ide-floppy\n", drive->name);
1da177e4
LT
1653 goto failed;
1654 }
1655 if (drive->scsi) {
0571c7a4
BP
1656 printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi"
1657 " emulation.\n", drive->name);
1da177e4
LT
1658 goto failed;
1659 }
0571c7a4
BP
1660 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1661 if (!floppy) {
1662 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1663 " structure\n", drive->name);
1da177e4
LT
1664 goto failed;
1665 }
1666
1667 g = alloc_disk(1 << PARTN_BITS);
1668 if (!g)
1669 goto out_free_floppy;
1670
1671 ide_init_disk(g, drive);
1672
7662d046 1673 ide_proc_register_driver(drive, &idefloppy_driver);
1da177e4 1674
1da177e4
LT
1675 kref_init(&floppy->kref);
1676
1677 floppy->drive = drive;
1678 floppy->driver = &idefloppy_driver;
1679 floppy->disk = g;
1680
1681 g->private_data = &floppy->driver;
1682
1683 drive->driver_data = floppy;
1684
0571c7a4 1685 idefloppy_setup(drive, floppy);
8604affd 1686
1da177e4
LT
1687 g->minors = 1 << PARTN_BITS;
1688 g->driverfs_dev = &drive->gendev;
1da177e4
LT
1689 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1690 g->fops = &idefloppy_ops;
1691 drive->attach = 1;
1692 add_disk(g);
1693 return 0;
1694
1da177e4
LT
1695out_free_floppy:
1696 kfree(floppy);
1697failed:
8604affd 1698 return -ENODEV;
1da177e4
LT
1699}
1700
0571c7a4 1701static void __exit idefloppy_exit(void)
1da177e4 1702{
8604affd 1703 driver_unregister(&idefloppy_driver.gen_driver);
1da177e4
LT
1704}
1705
17514e8a 1706static int __init idefloppy_init(void)
1da177e4
LT
1707{
1708 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
8604affd 1709 return driver_register(&idefloppy_driver.gen_driver);
1da177e4
LT
1710}
1711
263756ec 1712MODULE_ALIAS("ide:*m-floppy*");
1da177e4
LT
1713module_init(idefloppy_init);
1714module_exit(idefloppy_exit);
1715MODULE_LICENSE("GPL");
0571c7a4
BP
1716MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1717
This page took 0.445064 seconds and 5 git commands to generate.