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