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