2 * linux/drivers/block/ide-floppy.c Version 0.8 Dec 7, 1997
4 * Copyright (C) 1996, 1997 Gadi Oxman <gadio@netvision.net.il>
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().
31 #define IDEFLOPPY_VERSION "0.8"
33 #include <linux/config.h>
34 #include <linux/module.h>
35 #include <linux/types.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
38 #include <linux/delay.h>
39 #include <linux/timer.h>
41 #include <linux/interrupt.h>
42 #include <linux/major.h>
43 #include <linux/errno.h>
44 #include <linux/genhd.h>
45 #include <linux/malloc.h>
46 #include <linux/cdrom.h>
47 #include <linux/ide.h>
49 #include <asm/byteorder.h>
51 #include <asm/uaccess.h>
53 #include <asm/unaligned.h>
54 #include <asm/bitops.h>
57 * The following are used to debug the driver.
59 #define IDEFLOPPY_DEBUG_LOG 0
60 #define IDEFLOPPY_DEBUG_INFO 0
61 #define IDEFLOPPY_DEBUG_BUGS 1
64 * Some drives require a longer irq timeout.
66 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
69 * After each failed packet command we issue a request sense command
70 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
72 #define IDEFLOPPY_MAX_PC_RETRIES 3
75 * With each packet command, we allocate a buffer of
76 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
78 #define IDEFLOPPY_PC_BUFFER_SIZE 256
81 * In various places in the driver, we need to allocate storage
82 * for packet commands and requests, which will remain valid while
83 * we leave the driver to wait for an interrupt or a timeout event.
85 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
88 * Our view of a packet command.
90 typedef struct idefloppy_packet_command_s
{
91 u8 c
[12]; /* Actual packet bytes */
92 int retries
; /* On each retry, we increment retries */
93 int error
; /* Error code */
94 int request_transfer
; /* Bytes to transfer */
95 int actually_transferred
; /* Bytes actually transferred */
96 int buffer_size
; /* Size of our data buffer */
97 char *b_data
; /* Pointer which runs on the buffers */
98 int b_count
; /* Missing/Available data on the current buffer */
99 struct request
*rq
; /* The corresponding request */
100 byte
*buffer
; /* Data buffer */
101 byte
*current_position
; /* Pointer into the above buffer */
102 void (*callback
) (ide_drive_t
*); /* Called when this packet command is completed */
103 byte pc_buffer
[IDEFLOPPY_PC_BUFFER_SIZE
]; /* Temporary buffer */
104 unsigned int flags
; /* Status/Action bit flags */
108 * Packet command flag bits.
110 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
111 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
112 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
113 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
114 #define PC_WRITING 5 /* Data direction */
117 * Removable Block Access Capabilities Page
120 #if defined(__LITTLE_ENDIAN_BITFIELD)
121 unsigned page_code
:6; /* Page code - Should be 0x1b */
122 unsigned reserved1_6
:1; /* Reserved */
123 unsigned ps
:1; /* Should be 0 */
124 #elif defined(__BIG_ENDIAN_BITFIELD)
125 unsigned ps
:1; /* Should be 0 */
126 unsigned reserved1_6
:1; /* Reserved */
127 unsigned page_code
:6; /* Page code - Should be 0x1b */
129 #error "Bitfield endianness not defined! Check your byteorder.h"
131 u8 page_length
; /* Page Length - Should be 0xa */
132 #if defined(__LITTLE_ENDIAN_BITFIELD)
133 unsigned reserved2
:6;
134 unsigned srfp
:1; /* Supports reporting progress of format */
135 unsigned sflp
:1; /* System floppy type device */
136 unsigned tlun
:3; /* Total logical units supported by the device */
137 unsigned reserved3
:3;
138 unsigned sml
:1; /* Single / Multiple lun supported */
139 unsigned ncd
:1; /* Non cd optical device */
140 #elif defined(__BIG_ENDIAN_BITFIELD)
141 unsigned sflp
:1; /* System floppy type device */
142 unsigned srfp
:1; /* Supports reporting progress of format */
143 unsigned reserved2
:6;
144 unsigned ncd
:1; /* Non cd optical device */
145 unsigned sml
:1; /* Single / Multiple lun supported */
146 unsigned reserved3
:3;
147 unsigned tlun
:3; /* Total logical units supported by the device */
149 #error "Bitfield endianness not defined! Check your byteorder.h"
152 } idefloppy_capabilities_page_t
;
155 * Flexible disk page.
158 #if defined(__LITTLE_ENDIAN_BITFIELD)
159 unsigned page_code
:6; /* Page code - Should be 0x5 */
160 unsigned reserved1_6
:1; /* Reserved */
161 unsigned ps
:1; /* The device is capable of saving the page */
162 #elif defined(__BIG_ENDIAN_BITFIELD)
163 unsigned ps
:1; /* The device is capable of saving the page */
164 unsigned reserved1_6
:1; /* Reserved */
165 unsigned page_code
:6; /* Page code - Should be 0x5 */
167 #error "Bitfield endianness not defined! Check your byteorder.h"
169 u8 page_length
; /* Page Length - Should be 0x1e */
170 u16 transfer_rate
; /* In kilobits per second */
171 u8 heads
, sectors
; /* Number of heads, Number of sectors per track */
172 u16 sector_size
; /* Byes per sector */
173 u16 cyls
; /* Number of cylinders */
175 u8 motor_delay
; /* Motor off delay */
177 u16 rpm
; /* Rotations per minute */
179 } idefloppy_flexible_disk_page_t
;
186 u8 length
; /* Length of the following descriptors in bytes */
187 } idefloppy_capacity_header_t
;
190 u32 blocks
; /* Number of blocks */
191 #if defined(__LITTLE_ENDIAN_BITFIELD)
192 unsigned dc
:2; /* Descriptor Code */
193 unsigned reserved
:6;
194 #elif defined(__BIG_ENDIAN_BITFIELD)
195 unsigned reserved
:6;
196 unsigned dc
:2; /* Descriptor Code */
198 #error "Bitfield endianness not defined! Check your byteorder.h"
200 u8 length_msb
; /* Block Length (MSB)*/
201 u16 length
; /* Block Length */
202 } idefloppy_capacity_descriptor_t
;
204 #define CAPACITY_INVALID 0x00
205 #define CAPACITY_UNFORMATTED 0x01
206 #define CAPACITY_CURRENT 0x02
207 #define CAPACITY_NO_CARTRIDGE 0x03
210 * Most of our global data which we need to save even as we leave the
211 * driver due to an interrupt or a timer event is stored in a variable
212 * of type idefloppy_floppy_t, defined below.
217 idefloppy_pc_t
*pc
; /* Current packet command */
218 idefloppy_pc_t
*failed_pc
; /* Last failed packet command */
219 idefloppy_pc_t pc_stack
[IDEFLOPPY_PC_STACK
];/* Packet command stack */
220 int pc_stack_index
; /* Next free packet command storage space */
221 struct request rq_stack
[IDEFLOPPY_PC_STACK
];
222 int rq_stack_index
; /* We implement a circular array */
225 * Last error information
227 byte sense_key
, asc
, ascq
;
232 int blocks
, block_size
, bs_factor
; /* Current format */
233 idefloppy_capacity_descriptor_t capacity
; /* Last format capacity */
234 idefloppy_flexible_disk_page_t flexible_disk_page
; /* Copy of the flexible disk page */
235 int wp
; /* Write protect */
237 unsigned int flags
; /* Status/Action flags */
238 } idefloppy_floppy_t
;
241 * Floppy flag bits values.
243 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
244 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
245 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
248 * ATAPI floppy drive packet commands
250 #define IDEFLOPPY_FORMAT_UNIT_CMD 0x04
251 #define IDEFLOPPY_INQUIRY_CMD 0x12
252 #define IDEFLOPPY_MODE_SELECT_CMD 0x55
253 #define IDEFLOPPY_MODE_SENSE_CMD 0x5a
254 #define IDEFLOPPY_READ10_CMD 0x28
255 #define IDEFLOPPY_READ12_CMD 0xa8
256 #define IDEFLOPPY_READ_CAPACITY_CMD 0x23
257 #define IDEFLOPPY_REQUEST_SENSE_CMD 0x03
258 #define IDEFLOPPY_PREVENT_REMOVAL_CMD 0x1e
259 #define IDEFLOPPY_SEEK_CMD 0x2b
260 #define IDEFLOPPY_START_STOP_CMD 0x1b
261 #define IDEFLOPPY_TEST_UNIT_READY_CMD 0x00
262 #define IDEFLOPPY_VERIFY_CMD 0x2f
263 #define IDEFLOPPY_WRITE10_CMD 0x2a
264 #define IDEFLOPPY_WRITE12_CMD 0xaa
265 #define IDEFLOPPY_WRITE_VERIFY_CMD 0x2e
268 * Defines for the mode sense command
270 #define MODE_SENSE_CURRENT 0x00
271 #define MODE_SENSE_CHANGEABLE 0x01
272 #define MODE_SENSE_DEFAULT 0x02
273 #define MODE_SENSE_SAVED 0x03
276 * Special requests for our block device strategy routine.
278 #define IDEFLOPPY_FIRST_RQ 90
281 * IDEFLOPPY_PC_RQ is used to queue a packet command in the request queue.
283 #define IDEFLOPPY_PC_RQ 90
285 #define IDEFLOPPY_LAST_RQ 90
288 * A macro which can be used to check if a given request command
289 * originated in the driver or in the buffer cache layer.
291 #define IDEFLOPPY_RQ_CMD(cmd) ((cmd >= IDEFLOPPY_FIRST_RQ) && (cmd <= IDEFLOPPY_LAST_RQ))
294 * Error codes which are returned in rq->errors to the higher part
297 #define IDEFLOPPY_ERROR_GENERAL 101
300 * The ATAPI Status Register.
305 #if defined(__LITTLE_ENDIAN_BITFIELD)
306 unsigned check
:1; /* Error occurred */
307 unsigned idx
:1; /* Reserved */
308 unsigned corr
:1; /* Correctable error occurred */
309 unsigned drq
:1; /* Data is request by the device */
310 unsigned dsc
:1; /* Media access command finished */
311 unsigned reserved5
:1; /* Reserved */
312 unsigned drdy
:1; /* Ignored for ATAPI commands (ready to accept ATA command) */
313 unsigned bsy
:1; /* The device has access to the command block */
314 #elif defined(__BIG_ENDIAN_BITFIELD)
315 unsigned bsy
:1; /* The device has access to the command block */
316 unsigned drdy
:1; /* Ignored for ATAPI commands (ready to accept ATA command) */
317 unsigned reserved5
:1; /* Reserved */
318 unsigned dsc
:1; /* Media access command finished */
319 unsigned drq
:1; /* Data is request by the device */
320 unsigned corr
:1; /* Correctable error occurred */
321 unsigned idx
:1; /* Reserved */
322 unsigned check
:1; /* Error occurred */
324 #error "Bitfield endianness not defined! Check your byteorder.h"
327 } idefloppy_status_reg_t
;
330 * The ATAPI error register.
335 #if defined(__LITTLE_ENDIAN_BITFIELD)
336 unsigned ili
:1; /* Illegal Length Indication */
337 unsigned eom
:1; /* End Of Media Detected */
338 unsigned abrt
:1; /* Aborted command - As defined by ATA */
339 unsigned mcr
:1; /* Media Change Requested - As defined by ATA */
340 unsigned sense_key
:4; /* Sense key of the last failed packet command */
341 #elif defined(__BIG_ENDIAN_BITFIELD)
342 unsigned sense_key
:4; /* Sense key of the last failed packet command */
343 unsigned mcr
:1; /* Media Change Requested - As defined by ATA */
344 unsigned abrt
:1; /* Aborted command - As defined by ATA */
345 unsigned eom
:1; /* End Of Media Detected */
346 unsigned ili
:1; /* Illegal Length Indication */
348 #error "Bitfield endianness not defined! Check your byteorder.h"
351 } idefloppy_error_reg_t
;
354 * ATAPI Feature Register
359 #if defined(__LITTLE_ENDIAN_BITFIELD)
360 unsigned dma
:1; /* Using DMA or PIO */
361 unsigned reserved321
:3; /* Reserved */
362 unsigned reserved654
:3; /* Reserved (Tag Type) */
363 unsigned reserved7
:1; /* Reserved */
364 #elif defined(__BIG_ENDIAN_BITFIELD)
365 unsigned reserved7
:1; /* Reserved */
366 unsigned reserved654
:3; /* Reserved (Tag Type) */
367 unsigned reserved321
:3; /* Reserved */
368 unsigned dma
:1; /* Using DMA or PIO */
370 #error "Bitfield endianness not defined! Check your byteorder.h"
373 } idefloppy_feature_reg_t
;
376 * ATAPI Byte Count Register.
381 #if defined(__LITTLE_ENDIAN_BITFIELD)
382 unsigned low
:8; /* LSB */
383 unsigned high
:8; /* MSB */
384 #elif defined(__BIG_ENDIAN_BITFIELD)
385 unsigned high
:8; /* MSB */
386 unsigned low
:8; /* LSB */
388 #error "Bitfield endianness not defined! Check your byteorder.h"
391 } idefloppy_bcount_reg_t
;
394 * ATAPI Interrupt Reason Register.
399 #if defined(__LITTLE_ENDIAN_BITFIELD)
400 unsigned cod
:1; /* Information transferred is command (1) or data (0) */
401 unsigned io
:1; /* The device requests us to read (1) or write (0) */
402 unsigned reserved
:6; /* Reserved */
403 #elif defined(__BIG_ENDIAN_BITFIELD)
404 unsigned reserved
:6; /* Reserved */
405 unsigned io
:1; /* The device requests us to read (1) or write (0) */
406 unsigned cod
:1; /* Information transferred is command (1) or data (0) */
408 #error "Bitfield endianness not defined! Check your byteorder.h"
411 } idefloppy_ireason_reg_t
;
414 * ATAPI floppy Drive Select Register
419 #if defined(__LITTLE_ENDIAN_BITFIELD)
420 unsigned sam_lun
:3; /* Logical unit number */
421 unsigned reserved3
:1; /* Reserved */
422 unsigned drv
:1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
423 unsigned one5
:1; /* Should be set to 1 */
424 unsigned reserved6
:1; /* Reserved */
425 unsigned one7
:1; /* Should be set to 1 */
426 #elif defined(__BIG_ENDIAN_BITFIELD)
427 unsigned one7
:1; /* Should be set to 1 */
428 unsigned reserved6
:1; /* Reserved */
429 unsigned one5
:1; /* Should be set to 1 */
430 unsigned drv
:1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
431 unsigned reserved3
:1; /* Reserved */
432 unsigned sam_lun
:3; /* Logical unit number */
434 #error "Bitfield endianness not defined! Check your byteorder.h"
437 } idefloppy_drivesel_reg_t
;
440 * ATAPI Device Control Register
445 #if defined(__LITTLE_ENDIAN_BITFIELD)
446 unsigned zero0
:1; /* Should be set to zero */
447 unsigned nien
:1; /* Device interrupt is disabled (1) or enabled (0) */
448 unsigned srst
:1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
449 unsigned one3
:1; /* Should be set to 1 */
450 unsigned reserved4567
:4; /* Reserved */
451 #elif defined(__BIG_ENDIAN_BITFIELD)
452 unsigned reserved4567
:4; /* Reserved */
453 unsigned one3
:1; /* Should be set to 1 */
454 unsigned srst
:1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
455 unsigned nien
:1; /* Device interrupt is disabled (1) or enabled (0) */
456 unsigned zero0
:1; /* Should be set to zero */
458 #error "Bitfield endianness not defined! Check your byteorder.h"
461 } idefloppy_control_reg_t
;
464 * The following is used to format the general configuration word of
465 * the ATAPI IDENTIFY DEVICE command.
467 struct idefloppy_id_gcw
{
468 #if defined(__LITTLE_ENDIAN_BITFIELD)
469 unsigned packet_size
:2; /* Packet Size */
470 unsigned reserved234
:3; /* Reserved */
471 unsigned drq_type
:2; /* Command packet DRQ type */
472 unsigned removable
:1; /* Removable media */
473 unsigned device_type
:5; /* Device type */
474 unsigned reserved13
:1; /* Reserved */
475 unsigned protocol
:2; /* Protocol type */
476 #elif defined(__BIG_ENDIAN_BITFIELD)
477 unsigned protocol
:2; /* Protocol type */
478 unsigned reserved13
:1; /* Reserved */
479 unsigned device_type
:5; /* Device type */
480 unsigned removable
:1; /* Removable media */
481 unsigned drq_type
:2; /* Command packet DRQ type */
482 unsigned reserved234
:3; /* Reserved */
483 unsigned packet_size
:2; /* Packet Size */
485 #error "Bitfield endianness not defined! Check your byteorder.h"
490 * INQUIRY packet command - Data Format
493 #if defined(__LITTLE_ENDIAN_BITFIELD)
494 unsigned device_type
:5; /* Peripheral Device Type */
495 unsigned reserved0_765
:3; /* Peripheral Qualifier - Reserved */
496 unsigned reserved1_6t0
:7; /* Reserved */
497 unsigned rmb
:1; /* Removable Medium Bit */
498 unsigned ansi_version
:3; /* ANSI Version */
499 unsigned ecma_version
:3; /* ECMA Version */
500 unsigned iso_version
:2; /* ISO Version */
501 unsigned response_format
:4; /* Response Data Format */
502 unsigned reserved3_45
:2; /* Reserved */
503 unsigned reserved3_6
:1; /* TrmIOP - Reserved */
504 unsigned reserved3_7
:1; /* AENC - Reserved */
505 #elif defined(__BIG_ENDIAN_BITFIELD)
506 unsigned reserved0_765
:3; /* Peripheral Qualifier - Reserved */
507 unsigned device_type
:5; /* Peripheral Device Type */
508 unsigned rmb
:1; /* Removable Medium Bit */
509 unsigned reserved1_6t0
:7; /* Reserved */
510 unsigned iso_version
:2; /* ISO Version */
511 unsigned ecma_version
:3; /* ECMA Version */
512 unsigned ansi_version
:3; /* ANSI Version */
513 unsigned reserved3_7
:1; /* AENC - Reserved */
514 unsigned reserved3_6
:1; /* TrmIOP - Reserved */
515 unsigned reserved3_45
:2; /* Reserved */
516 unsigned response_format
:4; /* Response Data Format */
518 #error "Bitfield endianness not defined! Check your byteorder.h"
520 u8 additional_length
; /* Additional Length (total_length-4) */
521 u8 rsv5
, rsv6
, rsv7
; /* Reserved */
522 u8 vendor_id
[8]; /* Vendor Identification */
523 u8 product_id
[16]; /* Product Identification */
524 u8 revision_level
[4]; /* Revision Level */
525 u8 vendor_specific
[20]; /* Vendor Specific - Optional */
526 u8 reserved56t95
[40]; /* Reserved - Optional */
527 /* Additional information may be returned */
528 } idefloppy_inquiry_result_t
;
531 * REQUEST SENSE packet command result - Data Format.
534 #if defined(__LITTLE_ENDIAN_BITFIELD)
535 unsigned error_code
:7; /* Current error (0x70) */
536 unsigned valid
:1; /* The information field conforms to SFF-8070i */
537 u8 reserved1
:8; /* Reserved */
538 unsigned sense_key
:4; /* Sense Key */
539 unsigned reserved2_4
:1; /* Reserved */
540 unsigned ili
:1; /* Incorrect Length Indicator */
541 unsigned reserved2_67
:2;
542 #elif defined(__BIG_ENDIAN_BITFIELD)
543 unsigned valid
:1; /* The information field conforms to SFF-8070i */
544 unsigned error_code
:7; /* Current error (0x70) */
545 u8 reserved1
:8; /* Reserved */
546 unsigned reserved2_67
:2;
547 unsigned ili
:1; /* Incorrect Length Indicator */
548 unsigned reserved2_4
:1; /* Reserved */
549 unsigned sense_key
:4; /* Sense Key */
551 #error "Bitfield endianness not defined! Check your byteorder.h"
553 u32 information
__attribute__ ((packed
));
554 u8 asl
; /* Additional sense length (n-7) */
555 u32 command_specific
; /* Additional command specific information */
556 u8 asc
; /* Additional Sense Code */
557 u8 ascq
; /* Additional Sense Code Qualifier */
558 u8 replaceable_unit_code
; /* Field Replaceable Unit Code */
560 u8 pad
[2]; /* Padding to 20 bytes */
561 } idefloppy_request_sense_result_t
;
564 * Pages of the SELECT SENSE / MODE SENSE packet commands.
566 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
567 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
570 * Mode Parameter Header for the MODE SENSE packet command
573 u16 mode_data_length
; /* Length of the following data transfer */
574 u8 medium_type
; /* Medium Type */
575 #if defined(__LITTLE_ENDIAN_BITFIELD)
576 unsigned reserved3
:7;
577 unsigned wp
:1; /* Write protect */
578 #elif defined(__BIG_ENDIAN_BITFIELD)
579 unsigned wp
:1; /* Write protect */
580 unsigned reserved3
:7;
582 #error "Bitfield endianness not defined! Check your byteorder.h"
585 } idefloppy_mode_parameter_header_t
;
587 #define IDEFLOPPY_MIN(a,b) ((a)<(b) ? (a):(b))
588 #define IDEFLOPPY_MAX(a,b) ((a)>(b) ? (a):(b))
591 * Too bad. The drive wants to send us data which we are not ready to accept.
592 * Just throw it away.
594 static void idefloppy_discard_data (ide_drive_t
*drive
, unsigned int bcount
)
597 IN_BYTE (IDE_DATA_REG
);
600 #if IDEFLOPPY_DEBUG_BUGS
601 static void idefloppy_write_zeros (ide_drive_t
*drive
, unsigned int bcount
)
604 OUT_BYTE (0, IDE_DATA_REG
);
606 #endif /* IDEFLOPPY_DEBUG_BUGS */
609 * idefloppy_end_request is used to finish servicing a request.
611 * For read/write requests, we will call ide_end_request to pass to the
614 static void idefloppy_end_request (byte uptodate
, ide_hwgroup_t
*hwgroup
)
616 ide_drive_t
*drive
= hwgroup
->drive
;
617 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
618 struct request
*rq
= hwgroup
->rq
;
621 #if IDEFLOPPY_DEBUG_LOG
622 printk (KERN_INFO
"Reached idefloppy_end_request\n");
623 #endif /* IDEFLOPPY_DEBUG_LOG */
626 case 0: error
= IDEFLOPPY_ERROR_GENERAL
; break;
627 case 1: error
= 0; break;
628 default: error
= uptodate
;
631 floppy
->failed_pc
= NULL
;
632 /* Why does this happen? */
635 if (!IDEFLOPPY_RQ_CMD (rq
->cmd
)) {
636 ide_end_request (uptodate
, hwgroup
);
640 ide_end_drive_cmd (drive
, 0, 0);
643 static void idefloppy_input_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
, unsigned int bcount
)
645 struct request
*rq
= pc
->rq
;
646 struct buffer_head
*bh
= rq
->bh
;
650 if (pc
->b_count
== bh
->b_size
) {
651 rq
->sector
+= rq
->current_nr_sectors
;
652 rq
->nr_sectors
-= rq
->current_nr_sectors
;
653 idefloppy_end_request (1, HWGROUP(drive
));
654 if ((bh
= rq
->bh
) != NULL
)
658 printk (KERN_ERR
"%s: bh == NULL in idefloppy_input_buffers, bcount == %d\n", drive
->name
, bcount
);
659 idefloppy_discard_data (drive
, bcount
);
662 count
= IDEFLOPPY_MIN (bh
->b_size
- pc
->b_count
, bcount
);
663 atapi_input_bytes (drive
, bh
->b_data
+ pc
->b_count
, count
);
664 bcount
-= count
; pc
->b_count
+= count
;
668 static void idefloppy_output_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
, unsigned int bcount
)
670 struct request
*rq
= pc
->rq
;
671 struct buffer_head
*bh
= rq
->bh
;
676 rq
->sector
+= rq
->current_nr_sectors
;
677 rq
->nr_sectors
-= rq
->current_nr_sectors
;
678 idefloppy_end_request (1, HWGROUP(drive
));
679 if ((bh
= rq
->bh
) != NULL
) {
680 pc
->b_data
= bh
->b_data
;
681 pc
->b_count
= bh
->b_size
;
685 printk (KERN_ERR
"%s: bh == NULL in idefloppy_output_buffers, bcount == %d\n", drive
->name
, bcount
);
686 idefloppy_write_zeros (drive
, bcount
);
689 count
= IDEFLOPPY_MIN (pc
->b_count
, bcount
);
690 atapi_output_bytes (drive
, pc
->b_data
, count
);
691 bcount
-= count
; pc
->b_data
+= count
; pc
->b_count
-= count
;
695 #ifdef CONFIG_BLK_DEV_IDEDMA
696 static void idefloppy_update_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
698 struct request
*rq
= pc
->rq
;
699 struct buffer_head
*bh
= rq
->bh
;
701 while ((bh
= rq
->bh
) != NULL
)
702 idefloppy_end_request (1, HWGROUP(drive
));
704 #endif /* CONFIG_BLK_DEV_IDEDMA */
707 * idefloppy_queue_pc_head generates a new packet command request in front
708 * of the request queue, before the current request, so that it will be
709 * processed immediately, on the next pass through the driver.
711 static void idefloppy_queue_pc_head (ide_drive_t
*drive
,idefloppy_pc_t
*pc
,struct request
*rq
)
713 ide_init_drive_cmd (rq
);
714 rq
->buffer
= (char *) pc
;
715 rq
->cmd
= IDEFLOPPY_PC_RQ
;
716 (void) ide_do_drive_cmd (drive
, rq
, ide_preempt
);
719 static idefloppy_pc_t
*idefloppy_next_pc_storage (ide_drive_t
*drive
)
721 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
723 if (floppy
->pc_stack_index
==IDEFLOPPY_PC_STACK
)
724 floppy
->pc_stack_index
=0;
725 return (&floppy
->pc_stack
[floppy
->pc_stack_index
++]);
728 static struct request
*idefloppy_next_rq_storage (ide_drive_t
*drive
)
730 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
732 if (floppy
->rq_stack_index
==IDEFLOPPY_PC_STACK
)
733 floppy
->rq_stack_index
=0;
734 return (&floppy
->rq_stack
[floppy
->rq_stack_index
++]);
738 * idefloppy_analyze_error is called on each failed packet command retry
739 * to analyze the request sense.
741 static void idefloppy_analyze_error (ide_drive_t
*drive
,idefloppy_request_sense_result_t
*result
)
743 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
745 floppy
->sense_key
= result
->sense_key
; floppy
->asc
= result
->asc
; floppy
->ascq
= result
->ascq
;
746 #if IDEFLOPPY_DEBUG_LOG
747 if (floppy
->failed_pc
)
748 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
);
750 printk (KERN_INFO
"ide-floppy: sense key = %x, asc = %x, ascq = %x\n",result
->sense_key
,result
->asc
,result
->ascq
);
751 #endif /* IDEFLOPPY_DEBUG_LOG */
754 static void idefloppy_request_sense_callback (ide_drive_t
*drive
)
756 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
758 #if IDEFLOPPY_DEBUG_LOG
759 printk (KERN_INFO
"ide-floppy: Reached idefloppy_request_sense_callback\n");
760 #endif /* IDEFLOPPY_DEBUG_LOG */
761 if (!floppy
->pc
->error
) {
762 idefloppy_analyze_error (drive
,(idefloppy_request_sense_result_t
*) floppy
->pc
->buffer
);
763 idefloppy_end_request (1,HWGROUP (drive
));
765 printk (KERN_ERR
"Error in REQUEST SENSE itself - Aborting request!\n");
766 idefloppy_end_request (0,HWGROUP (drive
));
771 * General packet command callback function.
773 static void idefloppy_pc_callback (ide_drive_t
*drive
)
775 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
777 #if IDEFLOPPY_DEBUG_LOG
778 printk (KERN_INFO
"ide-floppy: Reached idefloppy_pc_callback\n");
779 #endif /* IDEFLOPPY_DEBUG_LOG */
781 idefloppy_end_request (floppy
->pc
->error
? 0:1, HWGROUP(drive
));
785 * idefloppy_init_pc initializes a packet command.
787 static void idefloppy_init_pc (idefloppy_pc_t
*pc
)
789 memset (pc
->c
, 0, 12);
792 pc
->request_transfer
= 0;
793 pc
->buffer
= pc
->pc_buffer
;
794 pc
->buffer_size
= IDEFLOPPY_PC_BUFFER_SIZE
;
796 pc
->callback
= &idefloppy_pc_callback
;
799 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t
*pc
)
801 idefloppy_init_pc (pc
);
802 pc
->c
[0] = IDEFLOPPY_REQUEST_SENSE_CMD
;
804 pc
->request_transfer
= 18;
805 pc
->callback
= &idefloppy_request_sense_callback
;
809 * idefloppy_retry_pc is called when an error was detected during the
810 * last packet command. We queue a request sense packet command in
811 * the head of the request list.
813 static void idefloppy_retry_pc (ide_drive_t
*drive
)
817 idefloppy_error_reg_t error
;
819 error
.all
= IN_BYTE (IDE_ERROR_REG
);
820 pc
= idefloppy_next_pc_storage (drive
);
821 rq
= idefloppy_next_rq_storage (drive
);
822 idefloppy_create_request_sense_cmd (pc
);
823 idefloppy_queue_pc_head (drive
, pc
, rq
);
827 * idefloppy_pc_intr is the usual interrupt handler which will be called
828 * during a packet command.
830 static void idefloppy_pc_intr (ide_drive_t
*drive
)
832 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
833 idefloppy_status_reg_t status
;
834 idefloppy_bcount_reg_t bcount
;
835 idefloppy_ireason_reg_t ireason
;
836 idefloppy_pc_t
*pc
=floppy
->pc
;
837 struct request
*rq
= pc
->rq
;
840 #if IDEFLOPPY_DEBUG_LOG
841 printk (KERN_INFO
"ide-floppy: Reached idefloppy_pc_intr interrupt handler\n");
842 #endif /* IDEFLOPPY_DEBUG_LOG */
844 #ifdef CONFIG_BLK_DEV_IDEDMA
845 if (test_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
846 if (HWIF(drive
)->dmaproc(ide_dma_end
, drive
)) {
847 set_bit (PC_DMA_ERROR
, &pc
->flags
);
849 pc
->actually_transferred
=pc
->request_transfer
;
850 idefloppy_update_buffers (drive
, pc
);
852 #if IDEFLOPPY_DEBUG_LOG
853 printk (KERN_INFO
"ide-floppy: DMA finished\n");
854 #endif /* IDEFLOPPY_DEBUG_LOG */
856 #endif /* CONFIG_BLK_DEV_IDEDMA */
858 status
.all
= GET_STAT(); /* Clear the interrupt */
860 if (!status
.b
.drq
) { /* No more interrupts */
861 #if IDEFLOPPY_DEBUG_LOG
862 printk (KERN_INFO
"Packet command completed, %d bytes transferred\n", pc
->actually_transferred
);
863 #endif /* IDEFLOPPY_DEBUG_LOG */
864 clear_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
);
866 ide__sti(); /* local CPU only */
868 if (status
.b
.check
|| test_bit (PC_DMA_ERROR
, &pc
->flags
)) { /* Error detected */
869 #if IDEFLOPPY_DEBUG_LOG
870 printk (KERN_INFO
"ide-floppy: %s: I/O error\n",drive
->name
);
871 #endif /* IDEFLOPPY_DEBUG_LOG */
873 if (pc
->c
[0] == IDEFLOPPY_REQUEST_SENSE_CMD
) {
874 printk (KERN_ERR
"ide-floppy: I/O error in request sense command\n");
875 ide_do_reset (drive
);
878 idefloppy_retry_pc (drive
); /* Retry operation */
882 if (floppy
->failed_pc
== pc
)
883 floppy
->failed_pc
=NULL
;
884 pc
->callback(drive
); /* Command finished - Call the callback function */
887 #ifdef CONFIG_BLK_DEV_IDEDMA
888 if (test_and_clear_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
889 printk (KERN_ERR
"ide-floppy: The floppy wants to issue more interrupts in DMA mode\n");
890 (void) HWIF(drive
)->dmaproc(ide_dma_off
, drive
);
891 ide_do_reset (drive
);
894 #endif /* CONFIG_BLK_DEV_IDEDMA */
895 bcount
.b
.high
=IN_BYTE (IDE_BCOUNTH_REG
); /* Get the number of bytes to transfer */
896 bcount
.b
.low
=IN_BYTE (IDE_BCOUNTL_REG
); /* on this interrupt */
897 ireason
.all
=IN_BYTE (IDE_IREASON_REG
);
900 printk (KERN_ERR
"ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
901 ide_do_reset (drive
);
904 if (ireason
.b
.io
== test_bit (PC_WRITING
, &pc
->flags
)) { /* Hopefully, we will never get here */
905 printk (KERN_ERR
"ide-floppy: We wanted to %s, ", ireason
.b
.io
? "Write":"Read");
906 printk (KERN_ERR
"but the floppy wants us to %s !\n",ireason
.b
.io
? "Read":"Write");
907 ide_do_reset (drive
);
910 if (!test_bit (PC_WRITING
, &pc
->flags
)) { /* Reading - Check that we have enough space */
911 temp
= pc
->actually_transferred
+ bcount
.all
;
912 if ( temp
> pc
->request_transfer
) {
913 if (temp
> pc
->buffer_size
) {
914 printk (KERN_ERR
"ide-floppy: The floppy wants to send us more data than expected - discarding data\n");
915 idefloppy_discard_data (drive
,bcount
.all
);
916 ide_set_handler (drive
,&idefloppy_pc_intr
,IDEFLOPPY_WAIT_CMD
);
919 #if IDEFLOPPY_DEBUG_LOG
920 printk (KERN_NOTICE
"ide-floppy: The floppy wants to send us more data than expected - allowing transfer\n");
921 #endif /* IDEFLOPPY_DEBUG_LOG */
924 if (test_bit (PC_WRITING
, &pc
->flags
)) {
925 if (pc
->buffer
!= NULL
)
926 atapi_output_bytes (drive
,pc
->current_position
,bcount
.all
); /* Write the current buffer */
928 idefloppy_output_buffers (drive
, pc
, bcount
.all
);
930 if (pc
->buffer
!= NULL
)
931 atapi_input_bytes (drive
,pc
->current_position
,bcount
.all
); /* Read the current buffer */
933 idefloppy_input_buffers (drive
, pc
, bcount
.all
);
935 pc
->actually_transferred
+=bcount
.all
; /* Update the current position */
936 pc
->current_position
+=bcount
.all
;
938 ide_set_handler (drive
,&idefloppy_pc_intr
,IDEFLOPPY_WAIT_CMD
); /* And set the interrupt handler again */
941 static void idefloppy_transfer_pc (ide_drive_t
*drive
)
943 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
944 idefloppy_ireason_reg_t ireason
;
946 if (ide_wait_stat (drive
,DRQ_STAT
,BUSY_STAT
,WAIT_READY
)) {
947 printk (KERN_ERR
"ide-floppy: Strange, packet command initiated yet DRQ isn't asserted\n");
950 ireason
.all
=IN_BYTE (IDE_IREASON_REG
);
951 if (!ireason
.b
.cod
|| ireason
.b
.io
) {
952 printk (KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) while issuing a packet command\n");
953 ide_do_reset (drive
);
956 ide_set_handler (drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
); /* Set the interrupt routine */
957 atapi_output_bytes (drive
, floppy
->pc
->c
, 12); /* Send the actual packet */
961 * Issue a packet command
963 static void idefloppy_issue_pc (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
965 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
966 idefloppy_bcount_reg_t bcount
;
969 #if IDEFLOPPY_DEBUG_BUGS
970 if (floppy
->pc
->c
[0] == IDEFLOPPY_REQUEST_SENSE_CMD
&& pc
->c
[0] == IDEFLOPPY_REQUEST_SENSE_CMD
) {
971 printk (KERN_ERR
"ide-floppy: possible ide-floppy.c bug - Two request sense in serial were issued\n");
973 #endif /* IDEFLOPPY_DEBUG_BUGS */
975 if (floppy
->failed_pc
== NULL
&& pc
->c
[0] != IDEFLOPPY_REQUEST_SENSE_CMD
)
976 floppy
->failed_pc
=pc
;
977 floppy
->pc
=pc
; /* Set the current packet command */
979 if (pc
->retries
> IDEFLOPPY_MAX_PC_RETRIES
|| test_bit (PC_ABORT
, &pc
->flags
)) {
981 * We will "abort" retrying a packet command in case
982 * a legitimate error code was received.
984 if (!test_bit (PC_ABORT
, &pc
->flags
)) {
985 printk (KERN_ERR
"ide-floppy: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
986 drive
->name
, pc
->c
[0], floppy
->sense_key
, floppy
->asc
, floppy
->ascq
);
987 pc
->error
= IDEFLOPPY_ERROR_GENERAL
; /* Giving up */
989 floppy
->failed_pc
=NULL
;
993 #if IDEFLOPPY_DEBUG_LOG
994 printk (KERN_INFO
"Retry number - %d\n",pc
->retries
);
995 #endif /* IDEFLOPPY_DEBUG_LOG */
998 pc
->actually_transferred
=0; /* We haven't transferred any data yet */
999 pc
->current_position
=pc
->buffer
;
1000 bcount
.all
=pc
->request_transfer
; /* Request to transfer the entire buffer at once */
1002 #ifdef CONFIG_BLK_DEV_IDEDMA
1003 if (test_and_clear_bit (PC_DMA_ERROR
, &pc
->flags
)) {
1004 (void) HWIF(drive
)->dmaproc(ide_dma_off
, drive
);
1006 if (test_bit (PC_DMA_RECOMMENDED
, &pc
->flags
) && drive
->using_dma
)
1007 dma_ok
=!HWIF(drive
)->dmaproc(test_bit (PC_WRITING
, &pc
->flags
) ? ide_dma_write
: ide_dma_read
, drive
);
1008 #endif /* CONFIG_BLK_DEV_IDEDMA */
1010 if (IDE_CONTROL_REG
)
1011 OUT_BYTE (drive
->ctl
,IDE_CONTROL_REG
);
1012 OUT_BYTE (dma_ok
? 1:0,IDE_FEATURE_REG
); /* Use PIO/DMA */
1013 OUT_BYTE (bcount
.b
.high
,IDE_BCOUNTH_REG
);
1014 OUT_BYTE (bcount
.b
.low
,IDE_BCOUNTL_REG
);
1015 OUT_BYTE (drive
->select
.all
,IDE_SELECT_REG
);
1017 #ifdef CONFIG_BLK_DEV_IDEDMA
1018 if (dma_ok
) { /* Begin DMA, if necessary */
1019 set_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
);
1020 (void) (HWIF(drive
)->dmaproc(ide_dma_begin
, drive
));
1022 #endif /* CONFIG_BLK_DEV_IDEDMA */
1024 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
)) {
1025 ide_set_handler (drive
, &idefloppy_transfer_pc
, IDEFLOPPY_WAIT_CMD
);
1026 OUT_BYTE (WIN_PACKETCMD
, IDE_COMMAND_REG
); /* Issue the packet command */
1028 OUT_BYTE (WIN_PACKETCMD
, IDE_COMMAND_REG
);
1029 idefloppy_transfer_pc (drive
);
1033 static void idefloppy_rw_callback (ide_drive_t
*drive
)
1035 #if IDEFLOPPY_DEBUG_LOG
1036 printk (KERN_INFO
"ide-floppy: Reached idefloppy_rw_callback\n");
1037 #endif /* IDEFLOPPY_DEBUG_LOG */
1039 idefloppy_end_request(1, HWGROUP(drive
));
1043 static void idefloppy_create_prevent_cmd (idefloppy_pc_t
*pc
, int prevent
)
1045 #if IDEFLOPPY_DEBUG_LOG
1046 printk (KERN_INFO
"ide-floppy: creating prevent removal command, prevent = %d\n", prevent
);
1047 #endif /* IDEFLOPPY_DEBUG_LOG */
1049 idefloppy_init_pc (pc
);
1050 pc
->c
[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD
;
1054 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t
*pc
)
1056 idefloppy_init_pc (pc
);
1057 pc
->c
[0] = IDEFLOPPY_READ_CAPACITY_CMD
;
1060 pc
->request_transfer
= 255;
1064 * A mode sense command is used to "sense" floppy parameters.
1066 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t
*pc
, byte page_code
, byte type
)
1068 unsigned short length
= sizeof (idefloppy_mode_parameter_header_t
);
1070 idefloppy_init_pc (pc
);
1071 pc
->c
[0] = IDEFLOPPY_MODE_SENSE_CMD
;
1073 pc
->c
[2] = page_code
+ (type
<< 6);
1075 switch (page_code
) {
1076 case IDEFLOPPY_CAPABILITIES_PAGE
:
1079 case IDEFLOPPY_FLEXIBLE_DISK_PAGE
:
1083 printk (KERN_ERR
"ide-floppy: unsupported page code in create_mode_sense_cmd\n");
1085 put_unaligned (htons (length
), (unsigned short *) &pc
->c
[7]);
1086 pc
->request_transfer
= length
;
1089 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t
*pc
, int start
)
1091 idefloppy_init_pc (pc
);
1092 pc
->c
[0] = IDEFLOPPY_START_STOP_CMD
;
1096 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t
*pc
)
1098 idefloppy_init_pc(pc
);
1099 pc
->c
[0] = IDEFLOPPY_TEST_UNIT_READY_CMD
;
1102 static void idefloppy_create_rw_cmd (idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
, struct request
*rq
, unsigned long sector
)
1104 int block
= sector
/ floppy
->bs_factor
;
1105 int blocks
= rq
->nr_sectors
/ floppy
->bs_factor
;
1107 #if IDEFLOPPY_DEBUG_LOG
1108 printk ("create_rw1%d_cmd: block == %d, blocks == %d\n",
1109 2 * test_bit (IDEFLOPPY_USE_READ12
, &floppy
->flags
), block
, blocks
);
1110 #endif /* IDEFLOPPY_DEBUG_LOG */
1112 idefloppy_init_pc (pc
);
1113 if (test_bit (IDEFLOPPY_USE_READ12
, &floppy
->flags
)) {
1114 pc
->c
[0] = rq
->cmd
== READ
? IDEFLOPPY_READ12_CMD
: IDEFLOPPY_WRITE12_CMD
;
1115 put_unaligned (htonl (blocks
), (unsigned int *) &pc
->c
[6]);
1117 pc
->c
[0] = rq
->cmd
== READ
? IDEFLOPPY_READ10_CMD
: IDEFLOPPY_WRITE10_CMD
;
1118 put_unaligned (htons (blocks
), (unsigned short *) &pc
->c
[7]);
1120 put_unaligned (htonl (block
), (unsigned int *) &pc
->c
[2]);
1121 pc
->callback
= &idefloppy_rw_callback
;
1123 pc
->b_data
= rq
->buffer
;
1124 pc
->b_count
= rq
->cmd
== READ
? 0 : rq
->bh
->b_size
;
1125 if (rq
->cmd
== WRITE
)
1126 set_bit (PC_WRITING
, &pc
->flags
);
1128 pc
->request_transfer
= pc
->buffer_size
= blocks
* floppy
->block_size
;
1129 set_bit (PC_DMA_RECOMMENDED
, &pc
->flags
);
1133 * idefloppy_do_request is our request handling function.
1135 static void idefloppy_do_request (ide_drive_t
*drive
, struct request
*rq
, unsigned long block
)
1137 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1140 #if IDEFLOPPY_DEBUG_LOG
1141 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
);
1142 printk (KERN_INFO
"sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq
->sector
,rq
->nr_sectors
,rq
->current_nr_sectors
);
1143 #endif /* IDEFLOPPY_DEBUG_LOG */
1145 if (rq
->errors
>= ERROR_MAX
) {
1146 if (floppy
->failed_pc
!= NULL
)
1147 printk (KERN_ERR
"ide-floppy: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
1148 drive
->name
, floppy
->failed_pc
->c
[0], floppy
->sense_key
, floppy
->asc
, floppy
->ascq
);
1150 printk (KERN_ERR
"ide-floppy: %s: I/O error\n", drive
->name
);
1151 idefloppy_end_request (0, HWGROUP(drive
));
1157 if (rq
->sector
% floppy
->bs_factor
|| rq
->nr_sectors
% floppy
->bs_factor
) {
1158 printk ("%s: unsupported r/w request size\n", drive
->name
);
1159 idefloppy_end_request (0, HWGROUP(drive
));
1162 pc
= idefloppy_next_pc_storage (drive
);
1163 idefloppy_create_rw_cmd (floppy
, pc
, rq
, block
);
1165 case IDEFLOPPY_PC_RQ
:
1166 pc
= (idefloppy_pc_t
*) rq
->buffer
;
1169 printk (KERN_ERR
"ide-floppy: unsupported command %x in request queue\n", rq
->cmd
);
1170 idefloppy_end_request (0,HWGROUP (drive
));
1174 idefloppy_issue_pc (drive
, pc
);
1178 * idefloppy_queue_pc_tail adds a special packet command request to the
1179 * tail of the request queue, and waits for it to be serviced.
1181 static int idefloppy_queue_pc_tail (ide_drive_t
*drive
,idefloppy_pc_t
*pc
)
1185 ide_init_drive_cmd (&rq
);
1186 rq
.buffer
= (char *) pc
;
1187 rq
.cmd
= IDEFLOPPY_PC_RQ
;
1188 return ide_do_drive_cmd (drive
, &rq
, ide_wait
);
1192 * Look at the flexible disk page parameters. We will ignore the CHS
1193 * capacity parameters and use the LBA parameters instead.
1195 static int idefloppy_get_flexible_disk_page (ide_drive_t
*drive
)
1197 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1199 idefloppy_mode_parameter_header_t
*header
;
1200 idefloppy_flexible_disk_page_t
*page
;
1201 int capacity
, lba_capacity
;
1203 idefloppy_create_mode_sense_cmd (&pc
, IDEFLOPPY_FLEXIBLE_DISK_PAGE
, MODE_SENSE_CURRENT
);
1204 if (idefloppy_queue_pc_tail (drive
,&pc
)) {
1205 printk (KERN_ERR
"ide-floppy: Can't get flexible disk page parameters\n");
1208 header
= (idefloppy_mode_parameter_header_t
*) pc
.buffer
;
1209 floppy
->wp
= header
->wp
;
1210 page
= (idefloppy_flexible_disk_page_t
*) (header
+ 1);
1212 page
->transfer_rate
= ntohs (page
->transfer_rate
);
1213 page
->sector_size
= ntohs (page
->sector_size
);
1214 page
->cyls
= ntohs (page
->cyls
);
1215 page
->rpm
= ntohs (page
->rpm
);
1216 capacity
= page
->cyls
* page
->heads
* page
->sectors
* page
->sector_size
;
1217 if (memcmp (page
, &floppy
->flexible_disk_page
, sizeof (idefloppy_flexible_disk_page_t
)))
1218 printk (KERN_INFO
"%s: %dkB, %d/%d/%d CHS, %d kBps, %d sector size, %d rpm\n",
1219 drive
->name
, capacity
/ 1024, page
->cyls
, page
->heads
, page
->sectors
,
1220 page
->transfer_rate
/ 8, page
->sector_size
, page
->rpm
);
1222 floppy
->flexible_disk_page
= *page
;
1223 drive
->bios_cyl
= page
->cyls
;
1224 drive
->bios_head
= page
->heads
;
1225 drive
->bios_sect
= page
->sectors
;
1226 lba_capacity
= floppy
->blocks
* floppy
->block_size
;
1227 if (capacity
!= lba_capacity
) {
1228 printk (KERN_NOTICE
"%s: The drive reports both %d and %d bytes as its capacity\n",
1229 drive
->name
, capacity
, lba_capacity
);
1230 capacity
= IDEFLOPPY_MIN(capacity
, lba_capacity
);
1231 floppy
->blocks
= floppy
->block_size
? capacity
/ floppy
->block_size
: 0;
1237 * Determine if a media is present in the floppy drive, and if so,
1240 static int idefloppy_get_capacity (ide_drive_t
*drive
)
1242 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1244 idefloppy_capacity_header_t
*header
;
1245 idefloppy_capacity_descriptor_t
*descriptor
;
1246 int i
, descriptors
, rc
= 1, blocks
, length
;
1248 drive
->bios_cyl
= 0;
1249 drive
->bios_head
= drive
->bios_sect
= 0;
1250 floppy
->blocks
= floppy
->bs_factor
= 0;
1251 drive
->part
[0].nr_sects
= 0;
1253 idefloppy_create_read_capacity_cmd (&pc
);
1254 if (idefloppy_queue_pc_tail (drive
, &pc
)) {
1255 printk (KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1258 header
= (idefloppy_capacity_header_t
*) pc
.buffer
;
1259 descriptors
= header
->length
/ sizeof (idefloppy_capacity_descriptor_t
);
1260 descriptor
= (idefloppy_capacity_descriptor_t
*) (header
+ 1);
1261 for (i
= 0; i
< descriptors
; i
++, descriptor
++) {
1262 blocks
= descriptor
->blocks
= ntohl (descriptor
->blocks
);
1263 length
= descriptor
->length
= ntohs (descriptor
->length
);
1264 if (!i
&& descriptor
->dc
== CAPACITY_CURRENT
) {
1265 if (memcmp (descriptor
, &floppy
->capacity
, sizeof (idefloppy_capacity_descriptor_t
)))
1266 printk (KERN_INFO
"%s: %dkB, %d blocks, %d sector size\n", drive
->name
, blocks
* length
/ 1024, blocks
, length
);
1267 floppy
->capacity
= *descriptor
;
1268 if (!length
|| length
% 512)
1269 printk (KERN_ERR
"%s: %d bytes block size not supported\n", drive
->name
, length
);
1271 floppy
->blocks
= blocks
;
1272 floppy
->block_size
= length
;
1273 if ((floppy
->bs_factor
= length
/ 512) != 1)
1274 printk (KERN_NOTICE
"%s: warning: non 512 bytes block size not fully supported\n", drive
->name
);
1278 #if IDEFLOPPY_DEBUG_INFO
1279 if (!i
) printk (KERN_INFO
"Descriptor 0 Code: %d\n", descriptor
->dc
);
1280 printk (KERN_INFO
"Descriptor %d: %dkB, %d blocks, %d sector size\n", i
, blocks
* length
/ 1024, blocks
, length
);
1281 #endif /* IDEFLOPPY_DEBUG_INFO */
1283 (void) idefloppy_get_flexible_disk_page (drive
);
1284 drive
->part
[0].nr_sects
= floppy
->blocks
* floppy
->bs_factor
;
1289 * Our special ide-floppy ioctl's.
1291 * Currently there aren't any ioctl's.
1293 static int idefloppy_ioctl (ide_drive_t
*drive
, struct inode
*inode
, struct file
*file
,
1294 unsigned int cmd
, unsigned long arg
)
1298 if (cmd
== CDROMEJECT
) {
1299 if (drive
->usage
> 1)
1301 idefloppy_create_prevent_cmd (&pc
, 0);
1302 (void) idefloppy_queue_pc_tail (drive
, &pc
);
1303 idefloppy_create_start_stop_cmd (&pc
, 2);
1304 (void) idefloppy_queue_pc_tail (drive
, &pc
);
1311 * Our open/release functions
1313 static int idefloppy_open (struct inode
*inode
, struct file
*filp
, ide_drive_t
*drive
)
1315 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1318 #if IDEFLOPPY_DEBUG_LOG
1319 printk (KERN_INFO
"Reached idefloppy_open\n");
1320 #endif /* IDEFLOPPY_DEBUG_LOG */
1323 if (drive
->usage
== 1) {
1324 idefloppy_create_test_unit_ready_cmd(&pc
);
1325 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1326 idefloppy_create_start_stop_cmd (&pc
, 1);
1327 (void) idefloppy_queue_pc_tail (drive
, &pc
);
1329 if (idefloppy_get_capacity (drive
)) {
1334 if (floppy
->wp
&& (filp
->f_mode
& 2)) {
1339 set_bit (IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1340 idefloppy_create_prevent_cmd (&pc
, 1);
1341 (void) idefloppy_queue_pc_tail (drive
, &pc
);
1342 check_disk_change(inode
->i_rdev
);
1347 static void idefloppy_release (struct inode
*inode
, struct file
*filp
, ide_drive_t
*drive
)
1351 #if IDEFLOPPY_DEBUG_LOG
1352 printk (KERN_INFO
"Reached idefloppy_release\n");
1353 #endif /* IDEFLOPPY_DEBUG_LOG */
1355 if (!drive
->usage
) {
1356 invalidate_buffers (inode
->i_rdev
);
1357 idefloppy_create_prevent_cmd (&pc
, 0);
1358 (void) idefloppy_queue_pc_tail (drive
, &pc
);
1364 * Check media change. Use a simple algorithm for now.
1366 static int idefloppy_media_change (ide_drive_t
*drive
)
1368 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1370 return test_and_clear_bit (IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1374 * Return the current floppy capacity to ide.c.
1376 static unsigned long idefloppy_capacity (ide_drive_t
*drive
)
1378 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1379 unsigned long capacity
= floppy
->blocks
* floppy
->bs_factor
;
1385 * idefloppy_identify_device checks if we can support a drive,
1386 * based on the ATAPI IDENTIFY command results.
1388 static int idefloppy_identify_device (ide_drive_t
*drive
,struct hd_driveid
*id
)
1390 struct idefloppy_id_gcw gcw
;
1391 #if IDEFLOPPY_DEBUG_INFO
1392 unsigned short mask
,i
;
1394 #endif /* IDEFLOPPY_DEBUG_INFO */
1396 *((unsigned short *) &gcw
) = id
->config
;
1398 #if IDEFLOPPY_DEBUG_INFO
1399 printk (KERN_INFO
"Dumping ATAPI Identify Device floppy parameters\n");
1400 switch (gcw
.protocol
) {
1401 case 0: case 1: sprintf (buffer
, "ATA");break;
1402 case 2: sprintf (buffer
, "ATAPI");break;
1403 case 3: sprintf (buffer
, "Reserved (Unknown to ide-floppy)");break;
1405 printk (KERN_INFO
"Protocol Type: %s\n", buffer
);
1406 switch (gcw
.device_type
) {
1407 case 0: sprintf (buffer
, "Direct-access Device");break;
1408 case 1: sprintf (buffer
, "Streaming Tape Device");break;
1409 case 2: case 3: case 4: sprintf (buffer
, "Reserved");break;
1410 case 5: sprintf (buffer
, "CD-ROM Device");break;
1411 case 6: sprintf (buffer
, "Reserved");
1412 case 7: sprintf (buffer
, "Optical memory Device");break;
1413 case 0x1f: sprintf (buffer
, "Unknown or no Device type");break;
1414 default: sprintf (buffer
, "Reserved");
1416 printk (KERN_INFO
"Device Type: %x - %s\n", gcw
.device_type
, buffer
);
1417 printk (KERN_INFO
"Removable: %s\n",gcw
.removable
? "Yes":"No");
1418 switch (gcw
.drq_type
) {
1419 case 0: sprintf (buffer
, "Microprocessor DRQ");break;
1420 case 1: sprintf (buffer
, "Interrupt DRQ");break;
1421 case 2: sprintf (buffer
, "Accelerated DRQ");break;
1422 case 3: sprintf (buffer
, "Reserved");break;
1424 printk (KERN_INFO
"Command Packet DRQ Type: %s\n", buffer
);
1425 switch (gcw
.packet_size
) {
1426 case 0: sprintf (buffer
, "12 bytes");break;
1427 case 1: sprintf (buffer
, "16 bytes");break;
1428 default: sprintf (buffer
, "Reserved");break;
1430 printk (KERN_INFO
"Command Packet Size: %s\n", buffer
);
1431 printk (KERN_INFO
"Model: %.40s\n",id
->model
);
1432 printk (KERN_INFO
"Firmware Revision: %.8s\n",id
->fw_rev
);
1433 printk (KERN_INFO
"Serial Number: %.20s\n",id
->serial_no
);
1434 printk (KERN_INFO
"Write buffer size(?): %d bytes\n",id
->buf_size
*512);
1435 printk (KERN_INFO
"DMA: %s",id
->capability
& 0x01 ? "Yes\n":"No\n");
1436 printk (KERN_INFO
"LBA: %s",id
->capability
& 0x02 ? "Yes\n":"No\n");
1437 printk (KERN_INFO
"IORDY can be disabled: %s",id
->capability
& 0x04 ? "Yes\n":"No\n");
1438 printk (KERN_INFO
"IORDY supported: %s",id
->capability
& 0x08 ? "Yes\n":"Unknown\n");
1439 printk (KERN_INFO
"ATAPI overlap supported: %s",id
->capability
& 0x20 ? "Yes\n":"No\n");
1440 printk (KERN_INFO
"PIO Cycle Timing Category: %d\n",id
->tPIO
);
1441 printk (KERN_INFO
"DMA Cycle Timing Category: %d\n",id
->tDMA
);
1442 printk (KERN_INFO
"Single Word DMA supported modes:\n");
1443 for (i
=0,mask
=1;i
<8;i
++,mask
=mask
<< 1) {
1444 if (id
->dma_1word
& mask
)
1445 printk (KERN_INFO
" Mode %d%s\n", i
, (id
->dma_1word
& (mask
<< 8)) ? " (active)" : "");
1447 printk (KERN_INFO
"Multi Word DMA supported modes:\n");
1448 for (i
=0,mask
=1;i
<8;i
++,mask
=mask
<< 1) {
1449 if (id
->dma_mword
& mask
)
1450 printk (KERN_INFO
" Mode %d%s\n", i
, (id
->dma_mword
& (mask
<< 8)) ? " (active)" : "");
1452 if (id
->field_valid
& 0x0002) {
1453 printk (KERN_INFO
"Enhanced PIO Modes: %s\n",id
->eide_pio_modes
& 1 ? "Mode 3":"None");
1454 if (id
->eide_dma_min
== 0)
1455 sprintf (buffer
, "Not supported");
1457 sprintf (buffer
, "%d ns",id
->eide_dma_min
);
1458 printk (KERN_INFO
"Minimum Multi-word DMA cycle per word: %s\n", buffer
);
1459 if (id
->eide_dma_time
== 0)
1460 sprintf (buffer
, "Not supported");
1462 sprintf (buffer
, "%d ns",id
->eide_dma_time
);
1463 printk (KERN_INFO
"Manufacturer\'s Recommended Multi-word cycle: %s\n", buffer
);
1464 if (id
->eide_pio
== 0)
1465 sprintf (buffer
, "Not supported");
1467 sprintf (buffer
, "%d ns",id
->eide_pio
);
1468 printk (KERN_INFO
"Minimum PIO cycle without IORDY: %s\n", buffer
);
1469 if (id
->eide_pio_iordy
== 0)
1470 sprintf (buffer
, "Not supported");
1472 sprintf (buffer
, "%d ns",id
->eide_pio_iordy
);
1473 printk (KERN_INFO
"Minimum PIO cycle with IORDY: %s\n", buffer
);
1475 printk (KERN_INFO
"According to the device, fields 64-70 are not valid.\n");
1476 #endif /* IDEFLOPPY_DEBUG_INFO */
1478 if (gcw
.protocol
!= 2)
1479 printk (KERN_ERR
"ide-floppy: Protocol is not ATAPI\n");
1480 else if (gcw
.device_type
!= 0)
1481 printk (KERN_ERR
"ide-floppy: Device type is not set to floppy\n");
1482 else if (!gcw
.removable
)
1483 printk (KERN_ERR
"ide-floppy: The removable flag is not set\n");
1484 else if (gcw
.drq_type
== 3) {
1485 printk (KERN_ERR
"ide-floppy: Sorry, DRQ type %d not supported\n", gcw
.drq_type
);
1486 } else if (gcw
.packet_size
!= 0) {
1487 printk (KERN_ERR
"ide-floppy: Packet size is not 12 bytes long\n");
1493 static void idefloppy_add_settings(ide_drive_t
*drive
)
1495 int major
= HWIF(drive
)->major
;
1496 int minor
= drive
->select
.b
.unit
<< PARTN_BITS
;
1498 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, -1, -1, TYPE_SHORT
, 0, 1023, 1, 1, &drive
->bios_cyl
, NULL
);
1499 ide_add_setting(drive
, "bios_head", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
1500 ide_add_setting(drive
, "bios_sect", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
1501 ide_add_setting(drive
, "breada_readahead", SETTING_RW
, BLKRAGET
, BLKRASET
, TYPE_INT
, 0, 255, 1, 2, &read_ahead
[major
], NULL
);
1502 ide_add_setting(drive
, "file_readahead", SETTING_RW
, BLKFRAGET
, BLKFRASET
, TYPE_INTA
, 0, INT_MAX
, 1, 1024, &max_readahead
[major
][minor
], NULL
);
1503 ide_add_setting(drive
, "max_kb_per_request", SETTING_RW
, BLKSECTGET
, BLKSECTSET
, TYPE_INTA
, 1, 255, 1, 2, &max_sectors
[major
][minor
], NULL
);
1508 * Driver initialization.
1510 static void idefloppy_setup (ide_drive_t
*drive
, idefloppy_floppy_t
*floppy
)
1512 struct idefloppy_id_gcw gcw
;
1513 int major
= HWIF(drive
)->major
, i
;
1514 int minor
= drive
->select
.b
.unit
<< PARTN_BITS
;
1516 *((unsigned short *) &gcw
) = drive
->id
->config
;
1517 drive
->driver_data
= floppy
;
1518 drive
->ready_stat
= 0;
1519 memset (floppy
, 0, sizeof (idefloppy_floppy_t
));
1520 floppy
->drive
= drive
;
1521 floppy
->pc
= floppy
->pc_stack
;
1522 if (gcw
.drq_type
== 1)
1523 set_bit (IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
);
1524 if (strcmp(drive
->id
->model
, "IOMEGA ZIP 100 ATAPI") == 0 &&
1525 ((strcmp(drive
->id
->fw_rev
, "21.D") == 0) ||
1526 (strcmp(drive
->id
->fw_rev
, "23.D") == 0))) {
1527 for (i
= 0; i
< 1 << PARTN_BITS
; i
++)
1528 max_sectors
[major
][minor
+ i
] = 64;
1531 (void) idefloppy_get_capacity (drive
);
1532 idefloppy_add_settings(drive
);
1535 static int idefloppy_cleanup (ide_drive_t
*drive
)
1537 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1539 if (ide_unregister_subdriver (drive
))
1541 drive
->driver_data
= NULL
;
1546 #ifdef CONFIG_PROC_FS
1548 static ide_proc_entry_t idefloppy_proc
[] = {
1549 { "geometry", S_IFREG
|S_IRUGO
, proc_ide_read_geometry
, NULL
},
1550 { NULL
, 0, NULL
, NULL
}
1555 #define idefloppy_proc NULL
1557 #endif /* CONFIG_PROC_FS */
1560 * IDE subdriver functions, registered with ide.c
1562 static ide_driver_t idefloppy_driver
= {
1563 "ide-floppy", /* name */
1564 IDEFLOPPY_VERSION
, /* version */
1565 ide_floppy
, /* media */
1567 1, /* supports_dma */
1568 0, /* supports_dsc_overlap */
1569 idefloppy_cleanup
, /* cleanup */
1570 idefloppy_do_request
, /* do_request */
1571 idefloppy_end_request
, /* end_request */
1572 idefloppy_ioctl
, /* ioctl */
1573 idefloppy_open
, /* open */
1574 idefloppy_release
, /* release */
1575 idefloppy_media_change
, /* media_change */
1576 NULL
, /* pre_reset */
1577 idefloppy_capacity
, /* capacity */
1579 idefloppy_proc
/* proc */
1582 int idefloppy_init (void);
1583 static ide_module_t idefloppy_module
= {
1591 * idefloppy_init will register the driver for each floppy.
1593 int idefloppy_init (void)
1596 idefloppy_floppy_t
*floppy
;
1600 while ((drive
= ide_scan_devices (ide_floppy
, idefloppy_driver
.name
, NULL
, failed
++)) != NULL
) {
1601 if (!idefloppy_identify_device (drive
, drive
->id
)) {
1602 printk (KERN_ERR
"ide-floppy: %s: not supported by this version of ide-floppy\n", drive
->name
);
1605 if ((floppy
= (idefloppy_floppy_t
*) kmalloc (sizeof (idefloppy_floppy_t
), GFP_KERNEL
)) == NULL
) {
1606 printk (KERN_ERR
"ide-floppy: %s: Can't allocate a floppy structure\n", drive
->name
);
1609 if (ide_register_subdriver (drive
, &idefloppy_driver
, IDE_SUBDRIVER_VERSION
)) {
1610 printk (KERN_ERR
"ide-floppy: %s: Failed to register the driver with ide.c\n", drive
->name
);
1614 idefloppy_setup (drive
, floppy
);
1617 ide_register_module(&idefloppy_module
);
1623 int init_module (void)
1625 return idefloppy_init ();
1628 void cleanup_module (void)
1633 while ((drive
= ide_scan_devices (ide_floppy
, idefloppy_driver
.name
, &idefloppy_driver
, failed
)) != NULL
) {
1634 if (idefloppy_cleanup (drive
)) {
1635 printk ("%s: cleanup_module() called while still busy\n", drive
->name
);
1638 /* We must remove proc entries defined in this module.
1639 Otherwise we oops while accessing these entries */
1641 ide_remove_proc_entries(drive
->proc
, idefloppy_proc
);
1643 ide_unregister_module(&idefloppy_module
);