Import 2.3.25pre1
[davej-history.git] / drivers / block / ide-floppy.c
blob3db1c1515644511d6d3c06e23ce15986a6f81e23
1 /*
2 * linux/drivers/block/ide-floppy.c Version 0.9 Jul 4, 1999
4 * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
5 */
7 /*
8 * IDE ATAPI floppy driver.
10 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
13 * Many thanks to Lode Leroy <Lode.Leroy@www.ibase.be>, who tested so many
14 * ALPHA patches to this driver on an EASYSTOR LS-120 ATAPI floppy drive.
16 * Ver 0.1 Oct 17 96 Initial test version, mostly based on ide-tape.c.
17 * Ver 0.2 Oct 31 96 Minor changes.
18 * Ver 0.3 Dec 2 96 Fixed error recovery bug.
19 * Ver 0.4 Jan 26 97 Add support for the HDIO_GETGEO ioctl.
20 * Ver 0.5 Feb 21 97 Add partitions support.
21 * Use the minimum of the LBA and CHS capacities.
22 * Avoid hwgroup->rq == NULL on the last irq.
23 * Fix potential null dereferencing with DEBUG_LOG.
24 * Ver 0.8 Dec 7 97 Increase irq timeout from 10 to 50 seconds.
25 * Add media write-protect detection.
26 * Issue START command only if TEST UNIT READY fails.
27 * Add work-around for IOMEGA ZIP revision 21.D.
28 * Remove idefloppy_get_capabilities().
29 * Ver 0.9 Jul 4 99 Fix a bug which might have caused the number of
30 * bytes requested on each interrupt to be zero.
31 * Thanks to <shanos@es.co.nz> for pointing this out.
34 #define IDEFLOPPY_VERSION "0.9"
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/kernel.h>
41 #include <linux/delay.h>
42 #include <linux/timer.h>
43 #include <linux/mm.h>
44 #include <linux/interrupt.h>
45 #include <linux/major.h>
46 #include <linux/errno.h>
47 #include <linux/genhd.h>
48 #include <linux/malloc.h>
49 #include <linux/cdrom.h>
50 #include <linux/ide.h>
52 #include <asm/byteorder.h>
53 #include <asm/irq.h>
54 #include <asm/uaccess.h>
55 #include <asm/io.h>
56 #include <asm/unaligned.h>
57 #include <asm/bitops.h>
60 * The following are used to debug the driver.
62 #define IDEFLOPPY_DEBUG_LOG 0
63 #define IDEFLOPPY_DEBUG_INFO 0
64 #define IDEFLOPPY_DEBUG_BUGS 1
67 * Some drives require a longer irq timeout.
69 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
72 * After each failed packet command we issue a request sense command
73 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
75 #define IDEFLOPPY_MAX_PC_RETRIES 3
78 * With each packet command, we allocate a buffer of
79 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
81 #define IDEFLOPPY_PC_BUFFER_SIZE 256
84 * In various places in the driver, we need to allocate storage
85 * for packet commands and requests, which will remain valid while
86 * we leave the driver to wait for an interrupt or a timeout event.
88 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
91 * Our view of a packet command.
93 typedef struct idefloppy_packet_command_s {
94 u8 c[12]; /* Actual packet bytes */
95 int retries; /* On each retry, we increment retries */
96 int error; /* Error code */
97 int request_transfer; /* Bytes to transfer */
98 int actually_transferred; /* Bytes actually transferred */
99 int buffer_size; /* Size of our data buffer */
100 char *b_data; /* Pointer which runs on the buffers */
101 int b_count; /* Missing/Available data on the current buffer */
102 struct request *rq; /* The corresponding request */
103 byte *buffer; /* Data buffer */
104 byte *current_position; /* Pointer into the above buffer */
105 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
106 byte pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
107 unsigned int flags; /* Status/Action bit flags */
108 } idefloppy_pc_t;
111 * Packet command flag bits.
113 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
114 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
115 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
116 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
117 #define PC_WRITING 5 /* Data direction */
120 * Removable Block Access Capabilities Page
122 typedef struct {
123 #if defined(__LITTLE_ENDIAN_BITFIELD)
124 unsigned page_code :6; /* Page code - Should be 0x1b */
125 unsigned reserved1_6 :1; /* Reserved */
126 unsigned ps :1; /* Should be 0 */
127 #elif defined(__BIG_ENDIAN_BITFIELD)
128 unsigned ps :1; /* Should be 0 */
129 unsigned reserved1_6 :1; /* Reserved */
130 unsigned page_code :6; /* Page code - Should be 0x1b */
131 #else
132 #error "Bitfield endianness not defined! Check your byteorder.h"
133 #endif
134 u8 page_length; /* Page Length - Should be 0xa */
135 #if defined(__LITTLE_ENDIAN_BITFIELD)
136 unsigned reserved2 :6;
137 unsigned srfp :1; /* Supports reporting progress of format */
138 unsigned sflp :1; /* System floppy type device */
139 unsigned tlun :3; /* Total logical units supported by the device */
140 unsigned reserved3 :3;
141 unsigned sml :1; /* Single / Multiple lun supported */
142 unsigned ncd :1; /* Non cd optical device */
143 #elif defined(__BIG_ENDIAN_BITFIELD)
144 unsigned sflp :1; /* System floppy type device */
145 unsigned srfp :1; /* Supports reporting progress of format */
146 unsigned reserved2 :6;
147 unsigned ncd :1; /* Non cd optical device */
148 unsigned sml :1; /* Single / Multiple lun supported */
149 unsigned reserved3 :3;
150 unsigned tlun :3; /* Total logical units supported by the device */
151 #else
152 #error "Bitfield endianness not defined! Check your byteorder.h"
153 #endif
154 u8 reserved[8];
155 } idefloppy_capabilities_page_t;
158 * Flexible disk page.
160 typedef struct {
161 #if defined(__LITTLE_ENDIAN_BITFIELD)
162 unsigned page_code :6; /* Page code - Should be 0x5 */
163 unsigned reserved1_6 :1; /* Reserved */
164 unsigned ps :1; /* The device is capable of saving the page */
165 #elif defined(__BIG_ENDIAN_BITFIELD)
166 unsigned ps :1; /* The device is capable of saving the page */
167 unsigned reserved1_6 :1; /* Reserved */
168 unsigned page_code :6; /* Page code - Should be 0x5 */
169 #else
170 #error "Bitfield endianness not defined! Check your byteorder.h"
171 #endif
172 u8 page_length; /* Page Length - Should be 0x1e */
173 u16 transfer_rate; /* In kilobits per second */
174 u8 heads, sectors; /* Number of heads, Number of sectors per track */
175 u16 sector_size; /* Byes per sector */
176 u16 cyls; /* Number of cylinders */
177 u8 reserved10[10];
178 u8 motor_delay; /* Motor off delay */
179 u8 reserved21[7];
180 u16 rpm; /* Rotations per minute */
181 u8 reserved30[2];
182 } idefloppy_flexible_disk_page_t;
185 * Format capacity
187 typedef struct {
188 u8 reserved[3];
189 u8 length; /* Length of the following descriptors in bytes */
190 } idefloppy_capacity_header_t;
192 typedef struct {
193 u32 blocks; /* Number of blocks */
194 #if defined(__LITTLE_ENDIAN_BITFIELD)
195 unsigned dc :2; /* Descriptor Code */
196 unsigned reserved :6;
197 #elif defined(__BIG_ENDIAN_BITFIELD)
198 unsigned reserved :6;
199 unsigned dc :2; /* Descriptor Code */
200 #else
201 #error "Bitfield endianness not defined! Check your byteorder.h"
202 #endif
203 u8 length_msb; /* Block Length (MSB)*/
204 u16 length; /* Block Length */
205 } idefloppy_capacity_descriptor_t;
207 #define CAPACITY_INVALID 0x00
208 #define CAPACITY_UNFORMATTED 0x01
209 #define CAPACITY_CURRENT 0x02
210 #define CAPACITY_NO_CARTRIDGE 0x03
213 * Most of our global data which we need to save even as we leave the
214 * driver due to an interrupt or a timer event is stored in a variable
215 * of type idefloppy_floppy_t, defined below.
217 typedef struct {
218 ide_drive_t *drive;
220 idefloppy_pc_t *pc; /* Current packet command */
221 idefloppy_pc_t *failed_pc; /* Last failed packet command */
222 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];/* Packet command stack */
223 int pc_stack_index; /* Next free packet command storage space */
224 struct request rq_stack[IDEFLOPPY_PC_STACK];
225 int rq_stack_index; /* We implement a circular array */
228 * Last error information
230 byte sense_key, asc, ascq;
233 * Device information
235 int blocks, block_size, bs_factor; /* Current format */
236 idefloppy_capacity_descriptor_t capacity; /* Last format capacity */
237 idefloppy_flexible_disk_page_t flexible_disk_page; /* Copy of the flexible disk page */
238 int wp; /* Write protect */
240 unsigned int flags; /* Status/Action flags */
241 } idefloppy_floppy_t;
244 * Floppy flag bits values.
246 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
247 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
248 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
251 * ATAPI floppy drive packet commands
253 #define IDEFLOPPY_FORMAT_UNIT_CMD 0x04
254 #define IDEFLOPPY_INQUIRY_CMD 0x12
255 #define IDEFLOPPY_MODE_SELECT_CMD 0x55
256 #define IDEFLOPPY_MODE_SENSE_CMD 0x5a
257 #define IDEFLOPPY_READ10_CMD 0x28
258 #define IDEFLOPPY_READ12_CMD 0xa8
259 #define IDEFLOPPY_READ_CAPACITY_CMD 0x23
260 #define IDEFLOPPY_REQUEST_SENSE_CMD 0x03
261 #define IDEFLOPPY_PREVENT_REMOVAL_CMD 0x1e
262 #define IDEFLOPPY_SEEK_CMD 0x2b
263 #define IDEFLOPPY_START_STOP_CMD 0x1b
264 #define IDEFLOPPY_TEST_UNIT_READY_CMD 0x00
265 #define IDEFLOPPY_VERIFY_CMD 0x2f
266 #define IDEFLOPPY_WRITE10_CMD 0x2a
267 #define IDEFLOPPY_WRITE12_CMD 0xaa
268 #define IDEFLOPPY_WRITE_VERIFY_CMD 0x2e
271 * Defines for the mode sense command
273 #define MODE_SENSE_CURRENT 0x00
274 #define MODE_SENSE_CHANGEABLE 0x01
275 #define MODE_SENSE_DEFAULT 0x02
276 #define MODE_SENSE_SAVED 0x03
279 * Special requests for our block device strategy routine.
281 #define IDEFLOPPY_FIRST_RQ 90
284 * IDEFLOPPY_PC_RQ is used to queue a packet command in the request queue.
286 #define IDEFLOPPY_PC_RQ 90
288 #define IDEFLOPPY_LAST_RQ 90
291 * A macro which can be used to check if a given request command
292 * originated in the driver or in the buffer cache layer.
294 #define IDEFLOPPY_RQ_CMD(cmd) ((cmd >= IDEFLOPPY_FIRST_RQ) && (cmd <= IDEFLOPPY_LAST_RQ))
297 * Error codes which are returned in rq->errors to the higher part
298 * of the driver.
300 #define IDEFLOPPY_ERROR_GENERAL 101
303 * The ATAPI Status Register.
305 typedef union {
306 unsigned all :8;
307 struct {
308 #if defined(__LITTLE_ENDIAN_BITFIELD)
309 unsigned check :1; /* Error occurred */
310 unsigned idx :1; /* Reserved */
311 unsigned corr :1; /* Correctable error occurred */
312 unsigned drq :1; /* Data is request by the device */
313 unsigned dsc :1; /* Media access command finished */
314 unsigned reserved5 :1; /* Reserved */
315 unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
316 unsigned bsy :1; /* The device has access to the command block */
317 #elif defined(__BIG_ENDIAN_BITFIELD)
318 unsigned bsy :1; /* The device has access to the command block */
319 unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
320 unsigned reserved5 :1; /* Reserved */
321 unsigned dsc :1; /* Media access command finished */
322 unsigned drq :1; /* Data is request by the device */
323 unsigned corr :1; /* Correctable error occurred */
324 unsigned idx :1; /* Reserved */
325 unsigned check :1; /* Error occurred */
326 #else
327 #error "Bitfield endianness not defined! Check your byteorder.h"
328 #endif
329 } b;
330 } idefloppy_status_reg_t;
333 * The ATAPI error register.
335 typedef union {
336 unsigned all :8;
337 struct {
338 #if defined(__LITTLE_ENDIAN_BITFIELD)
339 unsigned ili :1; /* Illegal Length Indication */
340 unsigned eom :1; /* End Of Media Detected */
341 unsigned abrt :1; /* Aborted command - As defined by ATA */
342 unsigned mcr :1; /* Media Change Requested - As defined by ATA */
343 unsigned sense_key :4; /* Sense key of the last failed packet command */
344 #elif defined(__BIG_ENDIAN_BITFIELD)
345 unsigned sense_key :4; /* Sense key of the last failed packet command */
346 unsigned mcr :1; /* Media Change Requested - As defined by ATA */
347 unsigned abrt :1; /* Aborted command - As defined by ATA */
348 unsigned eom :1; /* End Of Media Detected */
349 unsigned ili :1; /* Illegal Length Indication */
350 #else
351 #error "Bitfield endianness not defined! Check your byteorder.h"
352 #endif
353 } b;
354 } idefloppy_error_reg_t;
357 * ATAPI Feature Register
359 typedef union {
360 unsigned all :8;
361 struct {
362 #if defined(__LITTLE_ENDIAN_BITFIELD)
363 unsigned dma :1; /* Using DMA or PIO */
364 unsigned reserved321 :3; /* Reserved */
365 unsigned reserved654 :3; /* Reserved (Tag Type) */
366 unsigned reserved7 :1; /* Reserved */
367 #elif defined(__BIG_ENDIAN_BITFIELD)
368 unsigned reserved7 :1; /* Reserved */
369 unsigned reserved654 :3; /* Reserved (Tag Type) */
370 unsigned reserved321 :3; /* Reserved */
371 unsigned dma :1; /* Using DMA or PIO */
372 #else
373 #error "Bitfield endianness not defined! Check your byteorder.h"
374 #endif
375 } b;
376 } idefloppy_feature_reg_t;
379 * ATAPI Byte Count Register.
381 typedef union {
382 unsigned all :16;
383 struct {
384 #if defined(__LITTLE_ENDIAN_BITFIELD)
385 unsigned low :8; /* LSB */
386 unsigned high :8; /* MSB */
387 #elif defined(__BIG_ENDIAN_BITFIELD)
388 unsigned high :8; /* MSB */
389 unsigned low :8; /* LSB */
390 #else
391 #error "Bitfield endianness not defined! Check your byteorder.h"
392 #endif
393 } b;
394 } idefloppy_bcount_reg_t;
397 * ATAPI Interrupt Reason Register.
399 typedef union {
400 unsigned all :8;
401 struct {
402 #if defined(__LITTLE_ENDIAN_BITFIELD)
403 unsigned cod :1; /* Information transferred is command (1) or data (0) */
404 unsigned io :1; /* The device requests us to read (1) or write (0) */
405 unsigned reserved :6; /* Reserved */
406 #elif defined(__BIG_ENDIAN_BITFIELD)
407 unsigned reserved :6; /* Reserved */
408 unsigned io :1; /* The device requests us to read (1) or write (0) */
409 unsigned cod :1; /* Information transferred is command (1) or data (0) */
410 #else
411 #error "Bitfield endianness not defined! Check your byteorder.h"
412 #endif
413 } b;
414 } idefloppy_ireason_reg_t;
417 * ATAPI floppy Drive Select Register
419 typedef union {
420 unsigned all :8;
421 struct {
422 #if defined(__LITTLE_ENDIAN_BITFIELD)
423 unsigned sam_lun :3; /* Logical unit number */
424 unsigned reserved3 :1; /* Reserved */
425 unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
426 unsigned one5 :1; /* Should be set to 1 */
427 unsigned reserved6 :1; /* Reserved */
428 unsigned one7 :1; /* Should be set to 1 */
429 #elif defined(__BIG_ENDIAN_BITFIELD)
430 unsigned one7 :1; /* Should be set to 1 */
431 unsigned reserved6 :1; /* Reserved */
432 unsigned one5 :1; /* Should be set to 1 */
433 unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
434 unsigned reserved3 :1; /* Reserved */
435 unsigned sam_lun :3; /* Logical unit number */
436 #else
437 #error "Bitfield endianness not defined! Check your byteorder.h"
438 #endif
439 } b;
440 } idefloppy_drivesel_reg_t;
443 * ATAPI Device Control Register
445 typedef union {
446 unsigned all :8;
447 struct {
448 #if defined(__LITTLE_ENDIAN_BITFIELD)
449 unsigned zero0 :1; /* Should be set to zero */
450 unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
451 unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
452 unsigned one3 :1; /* Should be set to 1 */
453 unsigned reserved4567 :4; /* Reserved */
454 #elif defined(__BIG_ENDIAN_BITFIELD)
455 unsigned reserved4567 :4; /* Reserved */
456 unsigned one3 :1; /* Should be set to 1 */
457 unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
458 unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
459 unsigned zero0 :1; /* Should be set to zero */
460 #else
461 #error "Bitfield endianness not defined! Check your byteorder.h"
462 #endif
463 } b;
464 } idefloppy_control_reg_t;
467 * The following is used to format the general configuration word of
468 * the ATAPI IDENTIFY DEVICE command.
470 struct idefloppy_id_gcw {
471 #if defined(__LITTLE_ENDIAN_BITFIELD)
472 unsigned packet_size :2; /* Packet Size */
473 unsigned reserved234 :3; /* Reserved */
474 unsigned drq_type :2; /* Command packet DRQ type */
475 unsigned removable :1; /* Removable media */
476 unsigned device_type :5; /* Device type */
477 unsigned reserved13 :1; /* Reserved */
478 unsigned protocol :2; /* Protocol type */
479 #elif defined(__BIG_ENDIAN_BITFIELD)
480 unsigned protocol :2; /* Protocol type */
481 unsigned reserved13 :1; /* Reserved */
482 unsigned device_type :5; /* Device type */
483 unsigned removable :1; /* Removable media */
484 unsigned drq_type :2; /* Command packet DRQ type */
485 unsigned reserved234 :3; /* Reserved */
486 unsigned packet_size :2; /* Packet Size */
487 #else
488 #error "Bitfield endianness not defined! Check your byteorder.h"
489 #endif
493 * INQUIRY packet command - Data Format
495 typedef struct {
496 #if defined(__LITTLE_ENDIAN_BITFIELD)
497 unsigned device_type :5; /* Peripheral Device Type */
498 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
499 unsigned reserved1_6t0 :7; /* Reserved */
500 unsigned rmb :1; /* Removable Medium Bit */
501 unsigned ansi_version :3; /* ANSI Version */
502 unsigned ecma_version :3; /* ECMA Version */
503 unsigned iso_version :2; /* ISO Version */
504 unsigned response_format :4; /* Response Data Format */
505 unsigned reserved3_45 :2; /* Reserved */
506 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
507 unsigned reserved3_7 :1; /* AENC - Reserved */
508 #elif defined(__BIG_ENDIAN_BITFIELD)
509 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
510 unsigned device_type :5; /* Peripheral Device Type */
511 unsigned rmb :1; /* Removable Medium Bit */
512 unsigned reserved1_6t0 :7; /* Reserved */
513 unsigned iso_version :2; /* ISO Version */
514 unsigned ecma_version :3; /* ECMA Version */
515 unsigned ansi_version :3; /* ANSI Version */
516 unsigned reserved3_7 :1; /* AENC - Reserved */
517 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
518 unsigned reserved3_45 :2; /* Reserved */
519 unsigned response_format :4; /* Response Data Format */
520 #else
521 #error "Bitfield endianness not defined! Check your byteorder.h"
522 #endif
523 u8 additional_length; /* Additional Length (total_length-4) */
524 u8 rsv5, rsv6, rsv7; /* Reserved */
525 u8 vendor_id[8]; /* Vendor Identification */
526 u8 product_id[16]; /* Product Identification */
527 u8 revision_level[4]; /* Revision Level */
528 u8 vendor_specific[20]; /* Vendor Specific - Optional */
529 u8 reserved56t95[40]; /* Reserved - Optional */
530 /* Additional information may be returned */
531 } idefloppy_inquiry_result_t;
534 * REQUEST SENSE packet command result - Data Format.
536 typedef struct {
537 #if defined(__LITTLE_ENDIAN_BITFIELD)
538 unsigned error_code :7; /* Current error (0x70) */
539 unsigned valid :1; /* The information field conforms to SFF-8070i */
540 u8 reserved1 :8; /* Reserved */
541 unsigned sense_key :4; /* Sense Key */
542 unsigned reserved2_4 :1; /* Reserved */
543 unsigned ili :1; /* Incorrect Length Indicator */
544 unsigned reserved2_67 :2;
545 #elif defined(__BIG_ENDIAN_BITFIELD)
546 unsigned valid :1; /* The information field conforms to SFF-8070i */
547 unsigned error_code :7; /* Current error (0x70) */
548 u8 reserved1 :8; /* Reserved */
549 unsigned reserved2_67 :2;
550 unsigned ili :1; /* Incorrect Length Indicator */
551 unsigned reserved2_4 :1; /* Reserved */
552 unsigned sense_key :4; /* Sense Key */
553 #else
554 #error "Bitfield endianness not defined! Check your byteorder.h"
555 #endif
556 u32 information __attribute__ ((packed));
557 u8 asl; /* Additional sense length (n-7) */
558 u32 command_specific; /* Additional command specific information */
559 u8 asc; /* Additional Sense Code */
560 u8 ascq; /* Additional Sense Code Qualifier */
561 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
562 u8 reserved[3];
563 u8 pad[2]; /* Padding to 20 bytes */
564 } idefloppy_request_sense_result_t;
567 * Pages of the SELECT SENSE / MODE SENSE packet commands.
569 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
570 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
573 * Mode Parameter Header for the MODE SENSE packet command
575 typedef struct {
576 u16 mode_data_length; /* Length of the following data transfer */
577 u8 medium_type; /* Medium Type */
578 #if defined(__LITTLE_ENDIAN_BITFIELD)
579 unsigned reserved3 :7;
580 unsigned wp :1; /* Write protect */
581 #elif defined(__BIG_ENDIAN_BITFIELD)
582 unsigned wp :1; /* Write protect */
583 unsigned reserved3 :7;
584 #else
585 #error "Bitfield endianness not defined! Check your byteorder.h"
586 #endif
587 u8 reserved[4];
588 } idefloppy_mode_parameter_header_t;
590 #define IDEFLOPPY_MIN(a,b) ((a)<(b) ? (a):(b))
591 #define IDEFLOPPY_MAX(a,b) ((a)>(b) ? (a):(b))
594 * Too bad. The drive wants to send us data which we are not ready to accept.
595 * Just throw it away.
597 static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
599 while (bcount--)
600 IN_BYTE (IDE_DATA_REG);
603 #if IDEFLOPPY_DEBUG_BUGS
604 static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
606 while (bcount--)
607 OUT_BYTE (0, IDE_DATA_REG);
609 #endif /* IDEFLOPPY_DEBUG_BUGS */
612 * idefloppy_end_request is used to finish servicing a request.
614 * For read/write requests, we will call ide_end_request to pass to the
615 * next buffer.
617 static void idefloppy_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
619 ide_drive_t *drive = hwgroup->drive;
620 idefloppy_floppy_t *floppy = drive->driver_data;
621 struct request *rq = hwgroup->rq;
622 int error;
624 #if IDEFLOPPY_DEBUG_LOG
625 printk (KERN_INFO "Reached idefloppy_end_request\n");
626 #endif /* IDEFLOPPY_DEBUG_LOG */
628 switch (uptodate) {
629 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
630 case 1: error = 0; break;
631 default: error = uptodate;
633 if (error)
634 floppy->failed_pc = NULL;
635 /* Why does this happen? */
636 if (!rq)
637 return;
638 if (!IDEFLOPPY_RQ_CMD (rq->cmd)) {
639 ide_end_request (uptodate, hwgroup);
640 return;
642 rq->errors = error;
643 ide_end_drive_cmd (drive, 0, 0);
646 static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
648 struct request *rq = pc->rq;
649 struct buffer_head *bh = rq->bh;
650 int count;
652 while (bcount) {
653 if (pc->b_count == bh->b_size) {
654 rq->sector += rq->current_nr_sectors;
655 rq->nr_sectors -= rq->current_nr_sectors;
656 idefloppy_end_request (1, HWGROUP(drive));
657 if ((bh = rq->bh) != NULL)
658 pc->b_count = 0;
660 if (bh == NULL) {
661 printk (KERN_ERR "%s: bh == NULL in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
662 idefloppy_discard_data (drive, bcount);
663 return;
665 count = IDEFLOPPY_MIN (bh->b_size - pc->b_count, bcount);
666 atapi_input_bytes (drive, bh->b_data + pc->b_count, count);
667 bcount -= count; pc->b_count += count;
671 static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
673 struct request *rq = pc->rq;
674 struct buffer_head *bh = rq->bh;
675 int count;
677 while (bcount) {
678 if (!pc->b_count) {
679 rq->sector += rq->current_nr_sectors;
680 rq->nr_sectors -= rq->current_nr_sectors;
681 idefloppy_end_request (1, HWGROUP(drive));
682 if ((bh = rq->bh) != NULL) {
683 pc->b_data = bh->b_data;
684 pc->b_count = bh->b_size;
687 if (bh == NULL) {
688 printk (KERN_ERR "%s: bh == NULL in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
689 idefloppy_write_zeros (drive, bcount);
690 return;
692 count = IDEFLOPPY_MIN (pc->b_count, bcount);
693 atapi_output_bytes (drive, pc->b_data, count);
694 bcount -= count; pc->b_data += count; pc->b_count -= count;
698 #ifdef CONFIG_BLK_DEV_IDEDMA
699 static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
701 struct request *rq = pc->rq;
702 struct buffer_head *bh = rq->bh;
704 while ((bh = rq->bh) != NULL)
705 idefloppy_end_request (1, HWGROUP(drive));
707 #endif /* CONFIG_BLK_DEV_IDEDMA */
710 * idefloppy_queue_pc_head generates a new packet command request in front
711 * of the request queue, before the current request, so that it will be
712 * processed immediately, on the next pass through the driver.
714 static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
716 ide_init_drive_cmd (rq);
717 rq->buffer = (char *) pc;
718 rq->cmd = IDEFLOPPY_PC_RQ;
719 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
722 static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
724 idefloppy_floppy_t *floppy = drive->driver_data;
726 if (floppy->pc_stack_index==IDEFLOPPY_PC_STACK)
727 floppy->pc_stack_index=0;
728 return (&floppy->pc_stack[floppy->pc_stack_index++]);
731 static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
733 idefloppy_floppy_t *floppy = drive->driver_data;
735 if (floppy->rq_stack_index==IDEFLOPPY_PC_STACK)
736 floppy->rq_stack_index=0;
737 return (&floppy->rq_stack[floppy->rq_stack_index++]);
741 * idefloppy_analyze_error is called on each failed packet command retry
742 * to analyze the request sense.
744 static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
746 idefloppy_floppy_t *floppy = drive->driver_data;
748 floppy->sense_key = result->sense_key; floppy->asc = result->asc; floppy->ascq = result->ascq;
749 #if IDEFLOPPY_DEBUG_LOG
750 if (floppy->failed_pc)
751 printk (KERN_INFO "ide-floppy: pc = %x, sense key = %x, asc = %x, ascq = %x\n",floppy->failed_pc->c[0],result->sense_key,result->asc,result->ascq);
752 else
753 printk (KERN_INFO "ide-floppy: sense key = %x, asc = %x, ascq = %x\n",result->sense_key,result->asc,result->ascq);
754 #endif /* IDEFLOPPY_DEBUG_LOG */
757 static void idefloppy_request_sense_callback (ide_drive_t *drive)
759 idefloppy_floppy_t *floppy = drive->driver_data;
761 #if IDEFLOPPY_DEBUG_LOG
762 printk (KERN_INFO "ide-floppy: Reached idefloppy_request_sense_callback\n");
763 #endif /* IDEFLOPPY_DEBUG_LOG */
764 if (!floppy->pc->error) {
765 idefloppy_analyze_error (drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
766 idefloppy_end_request (1,HWGROUP (drive));
767 } else {
768 printk (KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
769 idefloppy_end_request (0,HWGROUP (drive));
774 * General packet command callback function.
776 static void idefloppy_pc_callback (ide_drive_t *drive)
778 idefloppy_floppy_t *floppy = drive->driver_data;
780 #if IDEFLOPPY_DEBUG_LOG
781 printk (KERN_INFO "ide-floppy: Reached idefloppy_pc_callback\n");
782 #endif /* IDEFLOPPY_DEBUG_LOG */
784 idefloppy_end_request (floppy->pc->error ? 0:1, HWGROUP(drive));
788 * idefloppy_init_pc initializes a packet command.
790 static void idefloppy_init_pc (idefloppy_pc_t *pc)
792 memset (pc->c, 0, 12);
793 pc->retries = 0;
794 pc->flags = 0;
795 pc->request_transfer = 0;
796 pc->buffer = pc->pc_buffer;
797 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
798 pc->b_data = NULL;
799 pc->callback = &idefloppy_pc_callback;
802 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
804 idefloppy_init_pc (pc);
805 pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
806 pc->c[4] = 255;
807 pc->request_transfer = 18;
808 pc->callback = &idefloppy_request_sense_callback;
812 * idefloppy_retry_pc is called when an error was detected during the
813 * last packet command. We queue a request sense packet command in
814 * the head of the request list.
816 static void idefloppy_retry_pc (ide_drive_t *drive)
818 idefloppy_pc_t *pc;
819 struct request *rq;
820 idefloppy_error_reg_t error;
822 error.all = IN_BYTE (IDE_ERROR_REG);
823 pc = idefloppy_next_pc_storage (drive);
824 rq = idefloppy_next_rq_storage (drive);
825 idefloppy_create_request_sense_cmd (pc);
826 idefloppy_queue_pc_head (drive, pc, rq);
830 * idefloppy_pc_intr is the usual interrupt handler which will be called
831 * during a packet command.
833 static void idefloppy_pc_intr (ide_drive_t *drive)
835 idefloppy_floppy_t *floppy = drive->driver_data;
836 idefloppy_status_reg_t status;
837 idefloppy_bcount_reg_t bcount;
838 idefloppy_ireason_reg_t ireason;
839 idefloppy_pc_t *pc=floppy->pc;
840 struct request *rq = pc->rq;
841 unsigned int temp;
843 #if IDEFLOPPY_DEBUG_LOG
844 printk (KERN_INFO "ide-floppy: Reached idefloppy_pc_intr interrupt handler\n");
845 #endif /* IDEFLOPPY_DEBUG_LOG */
847 #ifdef CONFIG_BLK_DEV_IDEDMA
848 if (test_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
849 if (HWIF(drive)->dmaproc(ide_dma_end, drive)) {
850 set_bit (PC_DMA_ERROR, &pc->flags);
851 } else {
852 pc->actually_transferred=pc->request_transfer;
853 idefloppy_update_buffers (drive, pc);
855 #if IDEFLOPPY_DEBUG_LOG
856 printk (KERN_INFO "ide-floppy: DMA finished\n");
857 #endif /* IDEFLOPPY_DEBUG_LOG */
859 #endif /* CONFIG_BLK_DEV_IDEDMA */
861 status.all = GET_STAT(); /* Clear the interrupt */
863 if (!status.b.drq) { /* No more interrupts */
864 #if IDEFLOPPY_DEBUG_LOG
865 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
866 #endif /* IDEFLOPPY_DEBUG_LOG */
867 clear_bit (PC_DMA_IN_PROGRESS, &pc->flags);
869 ide__sti(); /* local CPU only */
871 if (status.b.check || test_bit (PC_DMA_ERROR, &pc->flags)) { /* Error detected */
872 #if IDEFLOPPY_DEBUG_LOG
873 printk (KERN_INFO "ide-floppy: %s: I/O error\n",drive->name);
874 #endif /* IDEFLOPPY_DEBUG_LOG */
875 rq->errors++;
876 if (pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
877 printk (KERN_ERR "ide-floppy: I/O error in request sense command\n");
878 ide_do_reset (drive);
879 return;
881 idefloppy_retry_pc (drive); /* Retry operation */
882 return;
884 pc->error = 0;
885 if (floppy->failed_pc == pc)
886 floppy->failed_pc=NULL;
887 pc->callback(drive); /* Command finished - Call the callback function */
888 return;
890 #ifdef CONFIG_BLK_DEV_IDEDMA
891 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
892 printk (KERN_ERR "ide-floppy: The floppy wants to issue more interrupts in DMA mode\n");
893 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
894 ide_do_reset (drive);
895 return;
897 #endif /* CONFIG_BLK_DEV_IDEDMA */
898 bcount.b.high=IN_BYTE (IDE_BCOUNTH_REG); /* Get the number of bytes to transfer */
899 bcount.b.low=IN_BYTE (IDE_BCOUNTL_REG); /* on this interrupt */
900 ireason.all=IN_BYTE (IDE_IREASON_REG);
902 if (ireason.b.cod) {
903 printk (KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
904 ide_do_reset (drive);
905 return;
907 if (ireason.b.io == test_bit (PC_WRITING, &pc->flags)) { /* Hopefully, we will never get here */
908 printk (KERN_ERR "ide-floppy: We wanted to %s, ", ireason.b.io ? "Write":"Read");
909 printk (KERN_ERR "but the floppy wants us to %s !\n",ireason.b.io ? "Read":"Write");
910 ide_do_reset (drive);
911 return;
913 if (!test_bit (PC_WRITING, &pc->flags)) { /* Reading - Check that we have enough space */
914 temp = pc->actually_transferred + bcount.all;
915 if ( temp > pc->request_transfer) {
916 if (temp > pc->buffer_size) {
917 printk (KERN_ERR "ide-floppy: The floppy wants to send us more data than expected - discarding data\n");
918 idefloppy_discard_data (drive,bcount.all);
919 ide_set_handler (drive,&idefloppy_pc_intr);
920 return;
922 #if IDEFLOPPY_DEBUG_LOG
923 printk (KERN_NOTICE "ide-floppy: The floppy wants to send us more data than expected - allowing transfer\n");
924 #endif /* IDEFLOPPY_DEBUG_LOG */
927 if (test_bit (PC_WRITING, &pc->flags)) {
928 if (pc->buffer != NULL)
929 atapi_output_bytes (drive,pc->current_position,bcount.all); /* Write the current buffer */
930 else
931 idefloppy_output_buffers (drive, pc, bcount.all);
932 } else {
933 if (pc->buffer != NULL)
934 atapi_input_bytes (drive,pc->current_position,bcount.all); /* Read the current buffer */
935 else
936 idefloppy_input_buffers (drive, pc, bcount.all);
938 pc->actually_transferred+=bcount.all; /* Update the current position */
939 pc->current_position+=bcount.all;
941 ide_set_handler (drive,&idefloppy_pc_intr); /* And set the interrupt handler again */
944 static void idefloppy_transfer_pc (ide_drive_t *drive)
946 idefloppy_floppy_t *floppy = drive->driver_data;
947 idefloppy_ireason_reg_t ireason;
949 if (ide_wait_stat (drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
950 printk (KERN_ERR "ide-floppy: Strange, packet command initiated yet DRQ isn't asserted\n");
951 return;
953 ireason.all=IN_BYTE (IDE_IREASON_REG);
954 if (!ireason.b.cod || ireason.b.io) {
955 printk (KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while issuing a packet command\n");
956 ide_do_reset (drive);
957 return;
959 ide_set_handler (drive, &idefloppy_pc_intr); /* Set the interrupt routine */
960 atapi_output_bytes (drive, floppy->pc->c, 12); /* Send the actual packet */
964 * Issue a packet command
966 static void idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
968 idefloppy_floppy_t *floppy = drive->driver_data;
969 idefloppy_bcount_reg_t bcount;
970 int dma_ok = 0;
972 #if IDEFLOPPY_DEBUG_BUGS
973 if (floppy->pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD && pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
974 printk (KERN_ERR "ide-floppy: possible ide-floppy.c bug - Two request sense in serial were issued\n");
976 #endif /* IDEFLOPPY_DEBUG_BUGS */
978 if (floppy->failed_pc == NULL && pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
979 floppy->failed_pc=pc;
980 floppy->pc=pc; /* Set the current packet command */
982 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES || test_bit (PC_ABORT, &pc->flags)) {
984 * We will "abort" retrying a packet command in case
985 * a legitimate error code was received.
987 if (!test_bit (PC_ABORT, &pc->flags)) {
988 printk (KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
989 drive->name, pc->c[0], floppy->sense_key, floppy->asc, floppy->ascq);
990 pc->error = IDEFLOPPY_ERROR_GENERAL; /* Giving up */
992 floppy->failed_pc=NULL;
993 pc->callback(drive);
994 return;
996 #if IDEFLOPPY_DEBUG_LOG
997 printk (KERN_INFO "Retry number - %d\n",pc->retries);
998 #endif /* IDEFLOPPY_DEBUG_LOG */
1000 pc->retries++;
1001 pc->actually_transferred=0; /* We haven't transferred any data yet */
1002 pc->current_position=pc->buffer;
1003 bcount.all = IDE_MIN(pc->request_transfer, 63 * 1024);
1005 #ifdef CONFIG_BLK_DEV_IDEDMA
1006 if (test_and_clear_bit (PC_DMA_ERROR, &pc->flags)) {
1007 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
1009 if (test_bit (PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1010 dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
1011 #endif /* CONFIG_BLK_DEV_IDEDMA */
1013 if (IDE_CONTROL_REG)
1014 OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
1015 OUT_BYTE (dma_ok ? 1:0,IDE_FEATURE_REG); /* Use PIO/DMA */
1016 OUT_BYTE (bcount.b.high,IDE_BCOUNTH_REG);
1017 OUT_BYTE (bcount.b.low,IDE_BCOUNTL_REG);
1018 OUT_BYTE (drive->select.all,IDE_SELECT_REG);
1020 #ifdef CONFIG_BLK_DEV_IDEDMA
1021 if (dma_ok) { /* Begin DMA, if necessary */
1022 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
1023 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
1025 #endif /* CONFIG_BLK_DEV_IDEDMA */
1027 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1028 ide_set_handler (drive, &idefloppy_transfer_pc);
1029 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* Issue the packet command */
1030 } else {
1031 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);
1032 idefloppy_transfer_pc (drive);
1036 static void idefloppy_rw_callback (ide_drive_t *drive)
1038 #if IDEFLOPPY_DEBUG_LOG
1039 printk (KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
1040 #endif /* IDEFLOPPY_DEBUG_LOG */
1042 idefloppy_end_request(1, HWGROUP(drive));
1043 return;
1046 static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1048 #if IDEFLOPPY_DEBUG_LOG
1049 printk (KERN_INFO "ide-floppy: creating prevent removal command, prevent = %d\n", prevent);
1050 #endif /* IDEFLOPPY_DEBUG_LOG */
1052 idefloppy_init_pc (pc);
1053 pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
1054 pc->c[4] = prevent;
1057 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1059 idefloppy_init_pc (pc);
1060 pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
1061 pc->c[7] = 255;
1062 pc->c[8] = 255;
1063 pc->request_transfer = 255;
1067 * A mode sense command is used to "sense" floppy parameters.
1069 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, byte page_code, byte type)
1071 unsigned short length = sizeof (idefloppy_mode_parameter_header_t);
1073 idefloppy_init_pc (pc);
1074 pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
1075 pc->c[1] = 0;
1076 pc->c[2] = page_code + (type << 6);
1078 switch (page_code) {
1079 case IDEFLOPPY_CAPABILITIES_PAGE:
1080 length += 12;
1081 break;
1082 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1083 length += 32;
1084 break;
1085 default:
1086 printk (KERN_ERR "ide-floppy: unsupported page code in create_mode_sense_cmd\n");
1088 put_unaligned (htons (length), (unsigned short *) &pc->c[7]);
1089 pc->request_transfer = length;
1092 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1094 idefloppy_init_pc (pc);
1095 pc->c[0] = IDEFLOPPY_START_STOP_CMD;
1096 pc->c[4] = start;
1099 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1101 idefloppy_init_pc(pc);
1102 pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
1105 static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1107 int block = sector / floppy->bs_factor;
1108 int blocks = rq->nr_sectors / floppy->bs_factor;
1110 #if IDEFLOPPY_DEBUG_LOG
1111 printk ("create_rw1%d_cmd: block == %d, blocks == %d\n",
1112 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags), block, blocks);
1113 #endif /* IDEFLOPPY_DEBUG_LOG */
1115 idefloppy_init_pc (pc);
1116 if (test_bit (IDEFLOPPY_USE_READ12, &floppy->flags)) {
1117 pc->c[0] = rq->cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
1118 put_unaligned (htonl (blocks), (unsigned int *) &pc->c[6]);
1119 } else {
1120 pc->c[0] = rq->cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
1121 put_unaligned (htons (blocks), (unsigned short *) &pc->c[7]);
1123 put_unaligned (htonl (block), (unsigned int *) &pc->c[2]);
1124 pc->callback = &idefloppy_rw_callback;
1125 pc->rq = rq;
1126 pc->b_data = rq->buffer;
1127 pc->b_count = rq->cmd == READ ? 0 : rq->bh->b_size;
1128 if (rq->cmd == WRITE)
1129 set_bit (PC_WRITING, &pc->flags);
1130 pc->buffer = NULL;
1131 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1132 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
1136 * idefloppy_do_request is our request handling function.
1138 static void idefloppy_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
1140 idefloppy_floppy_t *floppy = drive->driver_data;
1141 idefloppy_pc_t *pc;
1143 #if IDEFLOPPY_DEBUG_LOG
1144 printk (KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
1145 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
1146 #endif /* IDEFLOPPY_DEBUG_LOG */
1148 if (rq->errors >= ERROR_MAX) {
1149 if (floppy->failed_pc != NULL)
1150 printk (KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
1151 drive->name, floppy->failed_pc->c[0], floppy->sense_key, floppy->asc, floppy->ascq);
1152 else
1153 printk (KERN_ERR "ide-floppy: %s: I/O error\n", drive->name);
1154 idefloppy_end_request (0, HWGROUP(drive));
1155 return;
1157 switch (rq->cmd) {
1158 case READ:
1159 case WRITE:
1160 if (rq->sector % floppy->bs_factor || rq->nr_sectors % floppy->bs_factor) {
1161 printk ("%s: unsupported r/w request size\n", drive->name);
1162 idefloppy_end_request (0, HWGROUP(drive));
1163 return;
1165 pc = idefloppy_next_pc_storage (drive);
1166 idefloppy_create_rw_cmd (floppy, pc, rq, block);
1167 break;
1168 case IDEFLOPPY_PC_RQ:
1169 pc = (idefloppy_pc_t *) rq->buffer;
1170 break;
1171 default:
1172 printk (KERN_ERR "ide-floppy: unsupported command %x in request queue\n", rq->cmd);
1173 idefloppy_end_request (0,HWGROUP (drive));
1174 return;
1176 pc->rq = rq;
1177 idefloppy_issue_pc (drive, pc);
1181 * idefloppy_queue_pc_tail adds a special packet command request to the
1182 * tail of the request queue, and waits for it to be serviced.
1184 static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1186 struct request rq;
1188 ide_init_drive_cmd (&rq);
1189 rq.buffer = (char *) pc;
1190 rq.cmd = IDEFLOPPY_PC_RQ;
1191 return ide_do_drive_cmd (drive, &rq, ide_wait);
1195 * Look at the flexible disk page parameters. We will ignore the CHS
1196 * capacity parameters and use the LBA parameters instead.
1198 static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1200 idefloppy_floppy_t *floppy = drive->driver_data;
1201 idefloppy_pc_t pc;
1202 idefloppy_mode_parameter_header_t *header;
1203 idefloppy_flexible_disk_page_t *page;
1204 int capacity, lba_capacity;
1206 idefloppy_create_mode_sense_cmd (&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1207 if (idefloppy_queue_pc_tail (drive,&pc)) {
1208 printk (KERN_ERR "ide-floppy: Can't get flexible disk page parameters\n");
1209 return 1;
1211 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1212 floppy->wp = header->wp;
1213 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1215 page->transfer_rate = ntohs (page->transfer_rate);
1216 page->sector_size = ntohs (page->sector_size);
1217 page->cyls = ntohs (page->cyls);
1218 page->rpm = ntohs (page->rpm);
1219 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1220 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1221 printk (KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, %d sector size, %d rpm\n",
1222 drive->name, capacity / 1024, page->cyls, page->heads, page->sectors,
1223 page->transfer_rate / 8, page->sector_size, page->rpm);
1225 floppy->flexible_disk_page = *page;
1226 drive->bios_cyl = page->cyls;
1227 drive->bios_head = page->heads;
1228 drive->bios_sect = page->sectors;
1229 lba_capacity = floppy->blocks * floppy->block_size;
1230 if (capacity != lba_capacity) {
1231 printk (KERN_NOTICE "%s: The drive reports both %d and %d bytes as its capacity\n",
1232 drive->name, capacity, lba_capacity);
1233 capacity = IDEFLOPPY_MIN(capacity, lba_capacity);
1234 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1236 return 0;
1240 * Determine if a media is present in the floppy drive, and if so,
1241 * its LBA capacity.
1243 static int idefloppy_get_capacity (ide_drive_t *drive)
1245 idefloppy_floppy_t *floppy = drive->driver_data;
1246 idefloppy_pc_t pc;
1247 idefloppy_capacity_header_t *header;
1248 idefloppy_capacity_descriptor_t *descriptor;
1249 int i, descriptors, rc = 1, blocks, length;
1251 drive->bios_cyl = 0;
1252 drive->bios_head = drive->bios_sect = 0;
1253 floppy->blocks = floppy->bs_factor = 0;
1254 drive->part[0].nr_sects = 0;
1256 idefloppy_create_read_capacity_cmd (&pc);
1257 if (idefloppy_queue_pc_tail (drive, &pc)) {
1258 printk (KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1259 return 1;
1261 header = (idefloppy_capacity_header_t *) pc.buffer;
1262 descriptors = header->length / sizeof (idefloppy_capacity_descriptor_t);
1263 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1264 for (i = 0; i < descriptors; i++, descriptor++) {
1265 blocks = descriptor->blocks = ntohl (descriptor->blocks);
1266 length = descriptor->length = ntohs (descriptor->length);
1267 if (!i && descriptor->dc == CAPACITY_CURRENT) {
1268 if (memcmp (descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1269 printk (KERN_INFO "%s: %dkB, %d blocks, %d sector size\n", drive->name, blocks * length / 1024, blocks, length);
1270 floppy->capacity = *descriptor;
1271 if (!length || length % 512)
1272 printk (KERN_ERR "%s: %d bytes block size not supported\n", drive->name, length);
1273 else {
1274 floppy->blocks = blocks;
1275 floppy->block_size = length;
1276 if ((floppy->bs_factor = length / 512) != 1)
1277 printk (KERN_NOTICE "%s: warning: non 512 bytes block size not fully supported\n", drive->name);
1278 rc = 0;
1281 #if IDEFLOPPY_DEBUG_INFO
1282 if (!i) printk (KERN_INFO "Descriptor 0 Code: %d\n", descriptor->dc);
1283 printk (KERN_INFO "Descriptor %d: %dkB, %d blocks, %d sector size\n", i, blocks * length / 1024, blocks, length);
1284 #endif /* IDEFLOPPY_DEBUG_INFO */
1286 (void) idefloppy_get_flexible_disk_page (drive);
1287 drive->part[0].nr_sects = floppy->blocks * floppy->bs_factor;
1288 return rc;
1292 * Our special ide-floppy ioctl's.
1294 * Currently there aren't any ioctl's.
1296 static int idefloppy_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
1297 unsigned int cmd, unsigned long arg)
1299 idefloppy_pc_t pc;
1301 if (cmd == CDROMEJECT) {
1302 if (drive->usage > 1)
1303 return -EBUSY;
1304 idefloppy_create_prevent_cmd (&pc, 0);
1305 (void) idefloppy_queue_pc_tail (drive, &pc);
1306 idefloppy_create_start_stop_cmd (&pc, 2);
1307 (void) idefloppy_queue_pc_tail (drive, &pc);
1308 return 0;
1310 return -EIO;
1314 * Our open/release functions
1316 static int idefloppy_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
1318 idefloppy_floppy_t *floppy = drive->driver_data;
1319 idefloppy_pc_t pc;
1321 #if IDEFLOPPY_DEBUG_LOG
1322 printk (KERN_INFO "Reached idefloppy_open\n");
1323 #endif /* IDEFLOPPY_DEBUG_LOG */
1325 MOD_INC_USE_COUNT;
1326 if (drive->usage == 1) {
1327 idefloppy_create_test_unit_ready_cmd(&pc);
1328 if (idefloppy_queue_pc_tail(drive, &pc)) {
1329 idefloppy_create_start_stop_cmd (&pc, 1);
1330 (void) idefloppy_queue_pc_tail (drive, &pc);
1332 if (idefloppy_get_capacity (drive)) {
1333 drive->usage--;
1334 MOD_DEC_USE_COUNT;
1335 return -EIO;
1337 if (floppy->wp && (filp->f_mode & 2)) {
1338 drive->usage--;
1339 MOD_DEC_USE_COUNT;
1340 return -EROFS;
1342 set_bit (IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1343 idefloppy_create_prevent_cmd (&pc, 1);
1344 (void) idefloppy_queue_pc_tail (drive, &pc);
1345 check_disk_change(inode->i_rdev);
1347 return 0;
1350 static void idefloppy_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
1352 idefloppy_pc_t pc;
1354 #if IDEFLOPPY_DEBUG_LOG
1355 printk (KERN_INFO "Reached idefloppy_release\n");
1356 #endif /* IDEFLOPPY_DEBUG_LOG */
1358 if (!drive->usage) {
1359 invalidate_buffers (inode->i_rdev);
1360 idefloppy_create_prevent_cmd (&pc, 0);
1361 (void) idefloppy_queue_pc_tail (drive, &pc);
1363 MOD_DEC_USE_COUNT;
1367 * Check media change. Use a simple algorithm for now.
1369 static int idefloppy_media_change (ide_drive_t *drive)
1371 idefloppy_floppy_t *floppy = drive->driver_data;
1373 return test_and_clear_bit (IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1377 * Return the current floppy capacity to ide.c.
1379 static unsigned long idefloppy_capacity (ide_drive_t *drive)
1381 idefloppy_floppy_t *floppy = drive->driver_data;
1382 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1384 return capacity;
1388 * idefloppy_identify_device checks if we can support a drive,
1389 * based on the ATAPI IDENTIFY command results.
1391 static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1393 struct idefloppy_id_gcw gcw;
1394 #if IDEFLOPPY_DEBUG_INFO
1395 unsigned short mask,i;
1396 char buffer[80];
1397 #endif /* IDEFLOPPY_DEBUG_INFO */
1399 *((unsigned short *) &gcw) = id->config;
1401 #if IDEFLOPPY_DEBUG_INFO
1402 printk (KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1403 switch (gcw.protocol) {
1404 case 0: case 1: sprintf (buffer, "ATA");break;
1405 case 2: sprintf (buffer, "ATAPI");break;
1406 case 3: sprintf (buffer, "Reserved (Unknown to ide-floppy)");break;
1408 printk (KERN_INFO "Protocol Type: %s\n", buffer);
1409 switch (gcw.device_type) {
1410 case 0: sprintf (buffer, "Direct-access Device");break;
1411 case 1: sprintf (buffer, "Streaming Tape Device");break;
1412 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1413 case 5: sprintf (buffer, "CD-ROM Device");break;
1414 case 6: sprintf (buffer, "Reserved");
1415 case 7: sprintf (buffer, "Optical memory Device");break;
1416 case 0x1f: sprintf (buffer, "Unknown or no Device type");break;
1417 default: sprintf (buffer, "Reserved");
1419 printk (KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1420 printk (KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1421 switch (gcw.drq_type) {
1422 case 0: sprintf (buffer, "Microprocessor DRQ");break;
1423 case 1: sprintf (buffer, "Interrupt DRQ");break;
1424 case 2: sprintf (buffer, "Accelerated DRQ");break;
1425 case 3: sprintf (buffer, "Reserved");break;
1427 printk (KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1428 switch (gcw.packet_size) {
1429 case 0: sprintf (buffer, "12 bytes");break;
1430 case 1: sprintf (buffer, "16 bytes");break;
1431 default: sprintf (buffer, "Reserved");break;
1433 printk (KERN_INFO "Command Packet Size: %s\n", buffer);
1434 printk (KERN_INFO "Model: %.40s\n",id->model);
1435 printk (KERN_INFO "Firmware Revision: %.8s\n",id->fw_rev);
1436 printk (KERN_INFO "Serial Number: %.20s\n",id->serial_no);
1437 printk (KERN_INFO "Write buffer size(?): %d bytes\n",id->buf_size*512);
1438 printk (KERN_INFO "DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
1439 printk (KERN_INFO "LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
1440 printk (KERN_INFO "IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
1441 printk (KERN_INFO "IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
1442 printk (KERN_INFO "ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
1443 printk (KERN_INFO "PIO Cycle Timing Category: %d\n",id->tPIO);
1444 printk (KERN_INFO "DMA Cycle Timing Category: %d\n",id->tDMA);
1445 printk (KERN_INFO "Single Word DMA supported modes:\n");
1446 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1447 if (id->dma_1word & mask)
1448 printk (KERN_INFO " Mode %d%s\n", i, (id->dma_1word & (mask << 8)) ? " (active)" : "");
1450 printk (KERN_INFO "Multi Word DMA supported modes:\n");
1451 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1452 if (id->dma_mword & mask)
1453 printk (KERN_INFO " Mode %d%s\n", i, (id->dma_mword & (mask << 8)) ? " (active)" : "");
1455 if (id->field_valid & 0x0002) {
1456 printk (KERN_INFO "Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
1457 if (id->eide_dma_min == 0)
1458 sprintf (buffer, "Not supported");
1459 else
1460 sprintf (buffer, "%d ns",id->eide_dma_min);
1461 printk (KERN_INFO "Minimum Multi-word DMA cycle per word: %s\n", buffer);
1462 if (id->eide_dma_time == 0)
1463 sprintf (buffer, "Not supported");
1464 else
1465 sprintf (buffer, "%d ns",id->eide_dma_time);
1466 printk (KERN_INFO "Manufacturer\'s Recommended Multi-word cycle: %s\n", buffer);
1467 if (id->eide_pio == 0)
1468 sprintf (buffer, "Not supported");
1469 else
1470 sprintf (buffer, "%d ns",id->eide_pio);
1471 printk (KERN_INFO "Minimum PIO cycle without IORDY: %s\n", buffer);
1472 if (id->eide_pio_iordy == 0)
1473 sprintf (buffer, "Not supported");
1474 else
1475 sprintf (buffer, "%d ns",id->eide_pio_iordy);
1476 printk (KERN_INFO "Minimum PIO cycle with IORDY: %s\n", buffer);
1477 } else
1478 printk (KERN_INFO "According to the device, fields 64-70 are not valid.\n");
1479 #endif /* IDEFLOPPY_DEBUG_INFO */
1481 if (gcw.protocol != 2)
1482 printk (KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1483 else if (gcw.device_type != 0)
1484 printk (KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1485 else if (!gcw.removable)
1486 printk (KERN_ERR "ide-floppy: The removable flag is not set\n");
1487 else if (gcw.drq_type == 3) {
1488 printk (KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1489 } else if (gcw.packet_size != 0) {
1490 printk (KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1491 } else
1492 return 1;
1493 return 0;
1496 static void idefloppy_add_settings(ide_drive_t *drive)
1498 int major = HWIF(drive)->major;
1499 int minor = drive->select.b.unit << PARTN_BITS;
1501 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1502 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1503 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1504 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
1505 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
1506 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
1511 * Driver initialization.
1513 static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1515 struct idefloppy_id_gcw gcw;
1516 int major = HWIF(drive)->major, i;
1517 int minor = drive->select.b.unit << PARTN_BITS;
1519 *((unsigned short *) &gcw) = drive->id->config;
1520 drive->driver_data = floppy;
1521 drive->ready_stat = 0;
1522 drive->timeout = IDEFLOPPY_WAIT_CMD;
1523 memset (floppy, 0, sizeof (idefloppy_floppy_t));
1524 floppy->drive = drive;
1525 floppy->pc = floppy->pc_stack;
1526 if (gcw.drq_type == 1)
1527 set_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1529 * We used to check revisions here. At this point however
1530 * I'm giving up. Just assume they are all broken, its easier.
1532 * The actual reason for the workarounds was likely
1533 * a driver bug after all rather than a firmware bug,
1534 * and the workaround below used to hide it. It should
1535 * be fixed as of version 1.9, but to be on the safe side
1536 * we'll leave the limitation below for the 2.2.x tree.
1539 if (strcmp(drive->id->model, "IOMEGA ZIP 100 ATAPI") == 0)
1541 for (i = 0; i < 1 << PARTN_BITS; i++)
1542 max_sectors[major][minor + i] = 64;
1545 (void) idefloppy_get_capacity (drive);
1546 idefloppy_add_settings(drive);
1549 static int idefloppy_cleanup (ide_drive_t *drive)
1551 idefloppy_floppy_t *floppy = drive->driver_data;
1553 if (ide_unregister_subdriver (drive))
1554 return 1;
1555 drive->driver_data = NULL;
1556 kfree (floppy);
1557 return 0;
1560 #ifdef CONFIG_PROC_FS
1562 static ide_proc_entry_t idefloppy_proc[] = {
1563 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1564 { NULL, 0, NULL, NULL }
1567 #else
1569 #define idefloppy_proc NULL
1571 #endif /* CONFIG_PROC_FS */
1574 * IDE subdriver functions, registered with ide.c
1576 static ide_driver_t idefloppy_driver = {
1577 "ide-floppy", /* name */
1578 IDEFLOPPY_VERSION, /* version */
1579 ide_floppy, /* media */
1580 0, /* busy */
1581 1, /* supports_dma */
1582 0, /* supports_dsc_overlap */
1583 idefloppy_cleanup, /* cleanup */
1584 idefloppy_do_request, /* do_request */
1585 idefloppy_end_request, /* end_request */
1586 idefloppy_ioctl, /* ioctl */
1587 idefloppy_open, /* open */
1588 idefloppy_release, /* release */
1589 idefloppy_media_change, /* media_change */
1590 NULL, /* pre_reset */
1591 idefloppy_capacity, /* capacity */
1592 NULL, /* special */
1593 idefloppy_proc /* proc */
1596 int idefloppy_init (void);
1597 static ide_module_t idefloppy_module = {
1598 IDE_DRIVER_MODULE,
1599 idefloppy_init,
1600 &idefloppy_driver,
1601 NULL
1605 * idefloppy_init will register the driver for each floppy.
1607 int idefloppy_init (void)
1609 ide_drive_t *drive;
1610 idefloppy_floppy_t *floppy;
1611 int failed = 0;
1613 MOD_INC_USE_COUNT;
1614 while ((drive = ide_scan_devices (ide_floppy, idefloppy_driver.name, NULL, failed++)) != NULL) {
1615 if (!idefloppy_identify_device (drive, drive->id)) {
1616 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1617 continue;
1619 if ((floppy = (idefloppy_floppy_t *) kmalloc (sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
1620 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1621 continue;
1623 if (ide_register_subdriver (drive, &idefloppy_driver, IDE_SUBDRIVER_VERSION)) {
1624 printk (KERN_ERR "ide-floppy: %s: Failed to register the driver with ide.c\n", drive->name);
1625 kfree (floppy);
1626 continue;
1628 idefloppy_setup (drive, floppy);
1629 failed--;
1631 ide_register_module(&idefloppy_module);
1632 MOD_DEC_USE_COUNT;
1633 return 0;
1636 #ifdef MODULE
1637 int init_module (void)
1639 return idefloppy_init ();
1642 void cleanup_module (void)
1644 ide_drive_t *drive;
1645 int failed = 0;
1647 while ((drive = ide_scan_devices (ide_floppy, idefloppy_driver.name, &idefloppy_driver, failed)) != NULL) {
1648 if (idefloppy_cleanup (drive)) {
1649 printk ("%s: cleanup_module() called while still busy\n", drive->name);
1650 failed++;
1652 /* We must remove proc entries defined in this module.
1653 Otherwise we oops while accessing these entries */
1654 if (drive->proc)
1655 ide_remove_proc_entries(drive->proc, idefloppy_proc);
1657 ide_unregister_module(&idefloppy_module);
1659 #endif /* MODULE */