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