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