2 * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
3 * Copyright (C) 2000-2002 Paul Bristow <paul@paulbristow.net>
4 * Copyright (C) 2005 Bartlomiej Zolnierkiewicz
8 * IDE ATAPI floppy driver.
10 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
13 * This driver supports the following IDE floppy drives:
15 * LS-120/240 SuperDisk
17 * Iomega PC Card Clik!/PocketZip
19 * Many thanks to Lode Leroy <Lode.Leroy@www.ibase.be>, who tested so many
20 * ALPHA patches to this driver on an EASYSTOR LS-120 ATAPI floppy drive.
22 * Ver 0.1 Oct 17 96 Initial test version, mostly based on ide-tape.c.
23 * Ver 0.2 Oct 31 96 Minor changes.
24 * Ver 0.3 Dec 2 96 Fixed error recovery bug.
25 * Ver 0.4 Jan 26 97 Add support for the HDIO_GETGEO ioctl.
26 * Ver 0.5 Feb 21 97 Add partitions support.
27 * Use the minimum of the LBA and CHS capacities.
28 * Avoid hwgroup->rq == NULL on the last irq.
29 * Fix potential null dereferencing with DEBUG_LOG.
30 * Ver 0.8 Dec 7 97 Increase irq timeout from 10 to 50 seconds.
31 * Add media write-protect detection.
32 * Issue START command only if TEST UNIT READY fails.
33 * Add work-around for IOMEGA ZIP revision 21.D.
34 * Remove idefloppy_get_capabilities().
35 * Ver 0.9 Jul 4 99 Fix a bug which might have caused the number of
36 * bytes requested on each interrupt to be zero.
37 * Thanks to <shanos@es.co.nz> for pointing this out.
38 * Ver 0.9.sv Jan 6 01 Sam Varshavchik <mrsam@courier-mta.com>
39 * Implement low level formatting. Reimplemented
40 * IDEFLOPPY_CAPABILITIES_PAGE, since we need the srfp
41 * bit. My LS-120 drive barfs on
42 * IDEFLOPPY_CAPABILITIES_PAGE, but maybe it's just me.
43 * Compromise by not reporting a failure to get this
44 * mode page. Implemented four IOCTLs in order to
45 * implement formatting. IOCTls begin with 0x4600,
46 * 0x46 is 'F' as in Format.
47 * Jan 9 01 Userland option to select format verify.
48 * Added PC_SUPPRESS_ERROR flag - some idefloppy drives
49 * do not implement IDEFLOPPY_CAPABILITIES_PAGE, and
50 * return a sense error. Suppress error reporting in
51 * this particular case in order to avoid spurious
52 * errors in syslog. The culprit is
53 * idefloppy_get_capability_page(), so move it to
54 * idefloppy_begin_format() so that it's not used
55 * unless absolutely necessary.
56 * If drive does not support format progress indication
57 * monitor the dsc bit in the status register.
58 * Also, O_NDELAY on open will allow the device to be
59 * opened without a disk available. This can be used to
60 * open an unformatted disk, or get the device capacity.
61 * Ver 0.91 Dec 11 99 Added IOMEGA Clik! drive support by
62 * <paul@paulbristow.net>
63 * Ver 0.92 Oct 22 00 Paul Bristow became official maintainer for this
64 * driver. Included Powerbook internal zip kludge.
65 * Ver 0.93 Oct 24 00 Fixed bugs for Clik! drive
66 * no disk on insert and disk change now works
67 * Ver 0.94 Oct 27 00 Tidied up to remove strstr(Clik) everywhere
68 * Ver 0.95 Nov 7 00 Brought across to kernel 2.4
69 * Ver 0.96 Jan 7 01 Actually in line with release version of 2.4.0
70 * including set_bit patch from Rusty Russell
71 * Ver 0.97 Jul 22 01 Merge 0.91-0.96 onto 0.9.sv for ac series
72 * Ver 0.97.sv Aug 3 01 Backported from 2.4.7-ac3
73 * Ver 0.98 Oct 26 01 Split idefloppy_transfer_pc into two pieces to
74 * fix a lost interrupt problem. It appears the busy
75 * bit was being deasserted by my IOMEGA ATAPI ZIP 100
76 * drive before the drive was actually ready.
77 * Ver 0.98a Oct 29 01 Expose delay value so we can play.
78 * Ver 0.99 Feb 24 02 Remove duplicate code, modify clik! detection code
79 * to support new PocketZip drives
82 #define IDEFLOPPY_VERSION "0.99.newide"
84 #include <linux/module.h>
85 #include <linux/types.h>
86 #include <linux/string.h>
87 #include <linux/kernel.h>
88 #include <linux/delay.h>
89 #include <linux/timer.h>
91 #include <linux/interrupt.h>
92 #include <linux/major.h>
93 #include <linux/errno.h>
94 #include <linux/genhd.h>
95 #include <linux/slab.h>
96 #include <linux/cdrom.h>
97 #include <linux/ide.h>
98 #include <linux/bitops.h>
99 #include <linux/mutex.h>
101 #include <scsi/scsi_ioctl.h>
103 #include <asm/byteorder.h>
105 #include <asm/uaccess.h>
107 #include <asm/unaligned.h>
110 * The following are used to debug the driver.
112 #define IDEFLOPPY_DEBUG_LOG 0
113 #define IDEFLOPPY_DEBUG_INFO 0
114 #define IDEFLOPPY_DEBUG_BUGS 1
116 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
117 #define IDEFLOPPY_DEBUG( fmt, args... )
119 #if IDEFLOPPY_DEBUG_LOG
120 #define debug_log printk
122 #define debug_log(fmt, args... ) do {} while(0)
127 * Some drives require a longer irq timeout.
129 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
132 * After each failed packet command we issue a request sense command
133 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
135 #define IDEFLOPPY_MAX_PC_RETRIES 3
138 * With each packet command, we allocate a buffer of
139 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
141 #define IDEFLOPPY_PC_BUFFER_SIZE 256
144 * In various places in the driver, we need to allocate storage
145 * for packet commands and requests, which will remain valid while
146 * we leave the driver to wait for an interrupt or a timeout event.
148 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
151 * Our view of a packet command.
153 typedef struct idefloppy_packet_command_s
{
154 u8 c
[12]; /* Actual packet bytes */
155 int retries
; /* On each retry, we increment retries */
156 int error
; /* Error code */
157 int request_transfer
; /* Bytes to transfer */
158 int actually_transferred
; /* Bytes actually transferred */
159 int buffer_size
; /* Size of our data buffer */
160 int b_count
; /* Missing/Available data on the current buffer */
161 struct request
*rq
; /* The corresponding request */
162 u8
*buffer
; /* Data buffer */
163 u8
*current_position
; /* Pointer into the above buffer */
164 void (*callback
) (ide_drive_t
*); /* Called when this packet command is completed */
165 u8 pc_buffer
[IDEFLOPPY_PC_BUFFER_SIZE
]; /* Temporary buffer */
166 unsigned long flags
; /* Status/Action bit flags: long for set_bit */
170 * Packet command flag bits.
172 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
173 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
174 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
175 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
176 #define PC_WRITING 5 /* Data direction */
178 #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
181 * Removable Block Access Capabilities Page
184 #if defined(__LITTLE_ENDIAN_BITFIELD)
185 unsigned page_code
:6; /* Page code - Should be 0x1b */
186 unsigned reserved1_6
:1; /* Reserved */
187 unsigned ps
:1; /* Should be 0 */
188 #elif defined(__BIG_ENDIAN_BITFIELD)
189 unsigned ps
:1; /* Should be 0 */
190 unsigned reserved1_6
:1; /* Reserved */
191 unsigned page_code
:6; /* Page code - Should be 0x1b */
193 #error "Bitfield endianness not defined! Check your byteorder.h"
195 u8 page_length
; /* Page Length - Should be 0xa */
196 #if defined(__LITTLE_ENDIAN_BITFIELD)
197 unsigned reserved2
:6;
198 unsigned srfp
:1; /* Supports reporting progress of format */
199 unsigned sflp
:1; /* System floppy type device */
200 unsigned tlun
:3; /* Total logical units supported by the device */
201 unsigned reserved3
:3;
202 unsigned sml
:1; /* Single / Multiple lun supported */
203 unsigned ncd
:1; /* Non cd optical device */
204 #elif defined(__BIG_ENDIAN_BITFIELD)
205 unsigned sflp
:1; /* System floppy type device */
206 unsigned srfp
:1; /* Supports reporting progress of format */
207 unsigned reserved2
:6;
208 unsigned ncd
:1; /* Non cd optical device */
209 unsigned sml
:1; /* Single / Multiple lun supported */
210 unsigned reserved3
:3;
211 unsigned tlun
:3; /* Total logical units supported by the device */
213 #error "Bitfield endianness not defined! Check your byteorder.h"
216 } idefloppy_capabilities_page_t
;
219 * Flexible disk page.
222 #if defined(__LITTLE_ENDIAN_BITFIELD)
223 unsigned page_code
:6; /* Page code - Should be 0x5 */
224 unsigned reserved1_6
:1; /* Reserved */
225 unsigned ps
:1; /* The device is capable of saving the page */
226 #elif defined(__BIG_ENDIAN_BITFIELD)
227 unsigned ps
:1; /* The device is capable of saving the page */
228 unsigned reserved1_6
:1; /* Reserved */
229 unsigned page_code
:6; /* Page code - Should be 0x5 */
231 #error "Bitfield endianness not defined! Check your byteorder.h"
233 u8 page_length
; /* Page Length - Should be 0x1e */
234 u16 transfer_rate
; /* In kilobits per second */
235 u8 heads
, sectors
; /* Number of heads, Number of sectors per track */
236 u16 sector_size
; /* Byes per sector */
237 u16 cyls
; /* Number of cylinders */
239 u8 motor_delay
; /* Motor off delay */
241 u16 rpm
; /* Rotations per minute */
243 } idefloppy_flexible_disk_page_t
;
250 u8 length
; /* Length of the following descriptors in bytes */
251 } idefloppy_capacity_header_t
;
254 u32 blocks
; /* Number of blocks */
255 #if defined(__LITTLE_ENDIAN_BITFIELD)
256 unsigned dc
:2; /* Descriptor Code */
257 unsigned reserved
:6;
258 #elif defined(__BIG_ENDIAN_BITFIELD)
259 unsigned reserved
:6;
260 unsigned dc
:2; /* Descriptor Code */
262 #error "Bitfield endianness not defined! Check your byteorder.h"
264 u8 length_msb
; /* Block Length (MSB)*/
265 u16 length
; /* Block Length */
266 } idefloppy_capacity_descriptor_t
;
268 #define CAPACITY_INVALID 0x00
269 #define CAPACITY_UNFORMATTED 0x01
270 #define CAPACITY_CURRENT 0x02
271 #define CAPACITY_NO_CARTRIDGE 0x03
274 * Most of our global data which we need to save even as we leave the
275 * driver due to an interrupt or a timer event is stored in a variable
276 * of type idefloppy_floppy_t, defined below.
278 typedef struct ide_floppy_obj
{
280 ide_driver_t
*driver
;
281 struct gendisk
*disk
;
283 unsigned int openers
; /* protected by BKL for now */
285 /* Current packet command */
287 /* Last failed packet command */
288 idefloppy_pc_t
*failed_pc
;
289 /* Packet command stack */
290 idefloppy_pc_t pc_stack
[IDEFLOPPY_PC_STACK
];
291 /* Next free packet command storage space */
293 struct request rq_stack
[IDEFLOPPY_PC_STACK
];
294 /* We implement a circular array */
298 * Last error information
300 u8 sense_key
, asc
, ascq
;
301 /* delay this long before sending packet command */
303 int progress_indication
;
309 int blocks
, block_size
, bs_factor
;
310 /* Last format capacity */
311 idefloppy_capacity_descriptor_t capacity
;
312 /* Copy of the flexible disk page */
313 idefloppy_flexible_disk_page_t flexible_disk_page
;
316 /* Supports format progress report */
318 /* Status/Action flags */
320 } idefloppy_floppy_t
;
322 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
325 * Floppy flag bits values.
327 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
328 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
329 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
330 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
331 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
332 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
335 * ATAPI floppy drive packet commands
337 #define IDEFLOPPY_FORMAT_UNIT_CMD 0x04
338 #define IDEFLOPPY_INQUIRY_CMD 0x12
339 #define IDEFLOPPY_MODE_SELECT_CMD 0x55
340 #define IDEFLOPPY_MODE_SENSE_CMD 0x5a
341 #define IDEFLOPPY_READ10_CMD 0x28
342 #define IDEFLOPPY_READ12_CMD 0xa8
343 #define IDEFLOPPY_READ_CAPACITY_CMD 0x23
344 #define IDEFLOPPY_REQUEST_SENSE_CMD 0x03
345 #define IDEFLOPPY_PREVENT_REMOVAL_CMD 0x1e
346 #define IDEFLOPPY_SEEK_CMD 0x2b
347 #define IDEFLOPPY_START_STOP_CMD 0x1b
348 #define IDEFLOPPY_TEST_UNIT_READY_CMD 0x00
349 #define IDEFLOPPY_VERIFY_CMD 0x2f
350 #define IDEFLOPPY_WRITE10_CMD 0x2a
351 #define IDEFLOPPY_WRITE12_CMD 0xaa
352 #define IDEFLOPPY_WRITE_VERIFY_CMD 0x2e
355 * Defines for the mode sense command
357 #define MODE_SENSE_CURRENT 0x00
358 #define MODE_SENSE_CHANGEABLE 0x01
359 #define MODE_SENSE_DEFAULT 0x02
360 #define MODE_SENSE_SAVED 0x03
363 * IOCTLs used in low-level formatting.
366 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
367 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
368 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
369 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
372 * Error codes which are returned in rq->errors to the higher part
375 #define IDEFLOPPY_ERROR_GENERAL 101
378 * The following is used to format the general configuration word of
379 * the ATAPI IDENTIFY DEVICE command.
381 struct idefloppy_id_gcw
{
382 #if defined(__LITTLE_ENDIAN_BITFIELD)
383 unsigned packet_size
:2; /* Packet Size */
384 unsigned reserved234
:3; /* Reserved */
385 unsigned drq_type
:2; /* Command packet DRQ type */
386 unsigned removable
:1; /* Removable media */
387 unsigned device_type
:5; /* Device type */
388 unsigned reserved13
:1; /* Reserved */
389 unsigned protocol
:2; /* Protocol type */
390 #elif defined(__BIG_ENDIAN_BITFIELD)
391 unsigned protocol
:2; /* Protocol type */
392 unsigned reserved13
:1; /* Reserved */
393 unsigned device_type
:5; /* Device type */
394 unsigned removable
:1; /* Removable media */
395 unsigned drq_type
:2; /* Command packet DRQ type */
396 unsigned reserved234
:3; /* Reserved */
397 unsigned packet_size
:2; /* Packet Size */
399 #error "Bitfield endianness not defined! Check your byteorder.h"
404 * INQUIRY packet command - Data Format
407 #if defined(__LITTLE_ENDIAN_BITFIELD)
408 unsigned device_type
:5; /* Peripheral Device Type */
409 unsigned reserved0_765
:3; /* Peripheral Qualifier - Reserved */
410 unsigned reserved1_6t0
:7; /* Reserved */
411 unsigned rmb
:1; /* Removable Medium Bit */
412 unsigned ansi_version
:3; /* ANSI Version */
413 unsigned ecma_version
:3; /* ECMA Version */
414 unsigned iso_version
:2; /* ISO Version */
415 unsigned response_format
:4; /* Response Data Format */
416 unsigned reserved3_45
:2; /* Reserved */
417 unsigned reserved3_6
:1; /* TrmIOP - Reserved */
418 unsigned reserved3_7
:1; /* AENC - Reserved */
419 #elif defined(__BIG_ENDIAN_BITFIELD)
420 unsigned reserved0_765
:3; /* Peripheral Qualifier - Reserved */
421 unsigned device_type
:5; /* Peripheral Device Type */
422 unsigned rmb
:1; /* Removable Medium Bit */
423 unsigned reserved1_6t0
:7; /* Reserved */
424 unsigned iso_version
:2; /* ISO Version */
425 unsigned ecma_version
:3; /* ECMA Version */
426 unsigned ansi_version
:3; /* ANSI Version */
427 unsigned reserved3_7
:1; /* AENC - Reserved */
428 unsigned reserved3_6
:1; /* TrmIOP - Reserved */
429 unsigned reserved3_45
:2; /* Reserved */
430 unsigned response_format
:4; /* Response Data Format */
432 #error "Bitfield endianness not defined! Check your byteorder.h"
434 u8 additional_length
; /* Additional Length (total_length-4) */
435 u8 rsv5
, rsv6
, rsv7
; /* Reserved */
436 u8 vendor_id
[8]; /* Vendor Identification */
437 u8 product_id
[16]; /* Product Identification */
438 u8 revision_level
[4]; /* Revision Level */
439 u8 vendor_specific
[20]; /* Vendor Specific - Optional */
440 u8 reserved56t95
[40]; /* Reserved - Optional */
441 /* Additional information may be returned */
442 } idefloppy_inquiry_result_t
;
445 * REQUEST SENSE packet command result - Data Format.
448 #if defined(__LITTLE_ENDIAN_BITFIELD)
449 unsigned error_code
:7; /* Current error (0x70) */
450 unsigned valid
:1; /* The information field conforms to SFF-8070i */
451 u8 reserved1
:8; /* Reserved */
452 unsigned sense_key
:4; /* Sense Key */
453 unsigned reserved2_4
:1; /* Reserved */
454 unsigned ili
:1; /* Incorrect Length Indicator */
455 unsigned reserved2_67
:2;
456 #elif defined(__BIG_ENDIAN_BITFIELD)
457 unsigned valid
:1; /* The information field conforms to SFF-8070i */
458 unsigned error_code
:7; /* Current error (0x70) */
459 u8 reserved1
:8; /* Reserved */
460 unsigned reserved2_67
:2;
461 unsigned ili
:1; /* Incorrect Length Indicator */
462 unsigned reserved2_4
:1; /* Reserved */
463 unsigned sense_key
:4; /* Sense Key */
465 #error "Bitfield endianness not defined! Check your byteorder.h"
467 u32 information
__attribute__ ((packed
));
468 u8 asl
; /* Additional sense length (n-7) */
469 u32 command_specific
; /* Additional command specific information */
470 u8 asc
; /* Additional Sense Code */
471 u8 ascq
; /* Additional Sense Code Qualifier */
472 u8 replaceable_unit_code
; /* Field Replaceable Unit Code */
474 u8 pad
[2]; /* Padding to 20 bytes */
475 } idefloppy_request_sense_result_t
;
478 * Pages of the SELECT SENSE / MODE SENSE packet commands.
480 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
481 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
484 * Mode Parameter Header for the MODE SENSE packet command
487 u16 mode_data_length
; /* Length of the following data transfer */
488 u8 medium_type
; /* Medium Type */
489 #if defined(__LITTLE_ENDIAN_BITFIELD)
490 unsigned reserved3
:7;
491 unsigned wp
:1; /* Write protect */
492 #elif defined(__BIG_ENDIAN_BITFIELD)
493 unsigned wp
:1; /* Write protect */
494 unsigned reserved3
:7;
496 #error "Bitfield endianness not defined! Check your byteorder.h"
499 } idefloppy_mode_parameter_header_t
;
501 static DEFINE_MUTEX(idefloppy_ref_mutex
);
503 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
505 #define ide_floppy_g(disk) \
506 container_of((disk)->private_data, struct ide_floppy_obj, driver)
508 static struct ide_floppy_obj
*ide_floppy_get(struct gendisk
*disk
)
510 struct ide_floppy_obj
*floppy
= NULL
;
512 mutex_lock(&idefloppy_ref_mutex
);
513 floppy
= ide_floppy_g(disk
);
515 kref_get(&floppy
->kref
);
516 mutex_unlock(&idefloppy_ref_mutex
);
520 static void ide_floppy_release(struct kref
*);
522 static void ide_floppy_put(struct ide_floppy_obj
*floppy
)
524 mutex_lock(&idefloppy_ref_mutex
);
525 kref_put(&floppy
->kref
, ide_floppy_release
);
526 mutex_unlock(&idefloppy_ref_mutex
);
530 * Too bad. The drive wants to send us data which we are not ready to accept.
531 * Just throw it away.
533 static void idefloppy_discard_data (ide_drive_t
*drive
, unsigned int bcount
)
536 (void) HWIF(drive
)->INB(IDE_DATA_REG
);
539 #if IDEFLOPPY_DEBUG_BUGS
540 static void idefloppy_write_zeros (ide_drive_t
*drive
, unsigned int bcount
)
543 HWIF(drive
)->OUTB(0, IDE_DATA_REG
);
545 #endif /* IDEFLOPPY_DEBUG_BUGS */
549 * idefloppy_do_end_request is used to finish servicing a request.
551 * For read/write requests, we will call ide_end_request to pass to the
554 static int idefloppy_do_end_request(ide_drive_t
*drive
, int uptodate
, int nsecs
)
556 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
557 struct request
*rq
= HWGROUP(drive
)->rq
;
560 debug_log(KERN_INFO
"Reached idefloppy_end_request\n");
563 case 0: error
= IDEFLOPPY_ERROR_GENERAL
; break;
564 case 1: error
= 0; break;
565 default: error
= uptodate
;
568 floppy
->failed_pc
= NULL
;
569 /* Why does this happen? */
572 if (!blk_special_request(rq
)) {
573 /* our real local end request function */
574 ide_end_request(drive
, uptodate
, nsecs
);
578 /* fixme: need to move this local also */
579 ide_end_drive_cmd(drive
, 0, 0);
583 static void idefloppy_input_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
, unsigned int bcount
)
585 struct request
*rq
= pc
->rq
;
586 struct bio_vec
*bvec
;
587 struct req_iterator iter
;
592 rq_for_each_segment(bvec
, rq
, iter
) {
596 count
= min(bvec
->bv_len
, bcount
);
598 data
= bvec_kmap_irq(bvec
, &flags
);
599 drive
->hwif
->atapi_input_bytes(drive
, data
, count
);
600 bvec_kunmap_irq(data
, &flags
);
603 pc
->b_count
+= count
;
607 idefloppy_do_end_request(drive
, 1, done
>> 9);
610 printk(KERN_ERR
"%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive
->name
, bcount
);
611 idefloppy_discard_data(drive
, bcount
);
615 static void idefloppy_output_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
, unsigned int bcount
)
617 struct request
*rq
= pc
->rq
;
618 struct req_iterator iter
;
619 struct bio_vec
*bvec
;
624 rq_for_each_segment(bvec
, rq
, iter
) {
628 count
= min(bvec
->bv_len
, bcount
);
630 data
= bvec_kmap_irq(bvec
, &flags
);
631 drive
->hwif
->atapi_output_bytes(drive
, data
, count
);
632 bvec_kunmap_irq(data
, &flags
);
635 pc
->b_count
+= count
;
639 idefloppy_do_end_request(drive
, 1, done
>> 9);
641 #if IDEFLOPPY_DEBUG_BUGS
643 printk(KERN_ERR
"%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive
->name
, bcount
);
644 idefloppy_write_zeros(drive
, bcount
);
649 static void idefloppy_update_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
651 struct request
*rq
= pc
->rq
;
652 struct bio
*bio
= rq
->bio
;
654 while ((bio
= rq
->bio
) != NULL
)
655 idefloppy_do_end_request(drive
, 1, 0);
659 * idefloppy_queue_pc_head generates a new packet command request in front
660 * of the request queue, before the current request, so that it will be
661 * processed immediately, on the next pass through the driver.
663 static void idefloppy_queue_pc_head (ide_drive_t
*drive
,idefloppy_pc_t
*pc
,struct request
*rq
)
665 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
667 ide_init_drive_cmd(rq
);
668 rq
->buffer
= (char *) pc
;
669 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
670 rq
->rq_disk
= floppy
->disk
;
671 (void) ide_do_drive_cmd(drive
, rq
, ide_preempt
);
674 static idefloppy_pc_t
*idefloppy_next_pc_storage (ide_drive_t
*drive
)
676 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
678 if (floppy
->pc_stack_index
== IDEFLOPPY_PC_STACK
)
679 floppy
->pc_stack_index
=0;
680 return (&floppy
->pc_stack
[floppy
->pc_stack_index
++]);
683 static struct request
*idefloppy_next_rq_storage (ide_drive_t
*drive
)
685 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
687 if (floppy
->rq_stack_index
== IDEFLOPPY_PC_STACK
)
688 floppy
->rq_stack_index
= 0;
689 return (&floppy
->rq_stack
[floppy
->rq_stack_index
++]);
693 * idefloppy_analyze_error is called on each failed packet command retry
694 * to analyze the request sense.
696 static void idefloppy_analyze_error (ide_drive_t
*drive
,idefloppy_request_sense_result_t
*result
)
698 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
700 floppy
->sense_key
= result
->sense_key
;
701 floppy
->asc
= result
->asc
;
702 floppy
->ascq
= result
->ascq
;
703 floppy
->progress_indication
= result
->sksv
[0] & 0x80 ?
704 (u16
)get_unaligned((u16
*)(result
->sksv
+1)):0x10000;
705 if (floppy
->failed_pc
)
706 debug_log(KERN_INFO
"ide-floppy: pc = %x, sense key = %x, "
707 "asc = %x, ascq = %x\n", floppy
->failed_pc
->c
[0],
708 result
->sense_key
, result
->asc
, result
->ascq
);
710 debug_log(KERN_INFO
"ide-floppy: sense key = %x, asc = %x, "
711 "ascq = %x\n", result
->sense_key
,
712 result
->asc
, result
->ascq
);
715 static void idefloppy_request_sense_callback (ide_drive_t
*drive
)
717 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
719 debug_log(KERN_INFO
"ide-floppy: Reached %s\n", __FUNCTION__
);
721 if (!floppy
->pc
->error
) {
722 idefloppy_analyze_error(drive
,(idefloppy_request_sense_result_t
*) floppy
->pc
->buffer
);
723 idefloppy_do_end_request(drive
, 1, 0);
725 printk(KERN_ERR
"Error in REQUEST SENSE itself - Aborting request!\n");
726 idefloppy_do_end_request(drive
, 0, 0);
731 * General packet command callback function.
733 static void idefloppy_pc_callback (ide_drive_t
*drive
)
735 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
737 debug_log(KERN_INFO
"ide-floppy: Reached %s\n", __FUNCTION__
);
739 idefloppy_do_end_request(drive
, floppy
->pc
->error
? 0 : 1, 0);
743 * idefloppy_init_pc initializes a packet command.
745 static void idefloppy_init_pc (idefloppy_pc_t
*pc
)
747 memset(pc
->c
, 0, 12);
750 pc
->request_transfer
= 0;
751 pc
->buffer
= pc
->pc_buffer
;
752 pc
->buffer_size
= IDEFLOPPY_PC_BUFFER_SIZE
;
753 pc
->callback
= &idefloppy_pc_callback
;
756 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t
*pc
)
758 idefloppy_init_pc(pc
);
759 pc
->c
[0] = IDEFLOPPY_REQUEST_SENSE_CMD
;
761 pc
->request_transfer
= 18;
762 pc
->callback
= &idefloppy_request_sense_callback
;
766 * idefloppy_retry_pc is called when an error was detected during the
767 * last packet command. We queue a request sense packet command in
768 * the head of the request list.
770 static void idefloppy_retry_pc (ide_drive_t
*drive
)
775 (void)drive
->hwif
->INB(IDE_ERROR_REG
);
776 pc
= idefloppy_next_pc_storage(drive
);
777 rq
= idefloppy_next_rq_storage(drive
);
778 idefloppy_create_request_sense_cmd(pc
);
779 idefloppy_queue_pc_head(drive
, pc
, rq
);
783 * idefloppy_pc_intr is the usual interrupt handler which will be called
784 * during a packet command.
786 static ide_startstop_t
idefloppy_pc_intr (ide_drive_t
*drive
)
788 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
789 ide_hwif_t
*hwif
= drive
->hwif
;
790 idefloppy_pc_t
*pc
= floppy
->pc
;
791 struct request
*rq
= pc
->rq
;
796 debug_log(KERN_INFO
"ide-floppy: Reached %s interrupt handler\n",
799 if (test_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
800 if (HWIF(drive
)->ide_dma_end(drive
)) {
801 set_bit(PC_DMA_ERROR
, &pc
->flags
);
803 pc
->actually_transferred
= pc
->request_transfer
;
804 idefloppy_update_buffers(drive
, pc
);
806 debug_log(KERN_INFO
"ide-floppy: DMA finished\n");
809 /* Clear the interrupt */
810 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
812 if ((stat
& DRQ_STAT
) == 0) { /* No more interrupts */
813 debug_log(KERN_INFO
"Packet command completed, %d bytes "
814 "transferred\n", pc
->actually_transferred
);
815 clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
817 local_irq_enable_in_hardirq();
819 if ((stat
& ERR_STAT
) || test_bit(PC_DMA_ERROR
, &pc
->flags
)) {
821 debug_log(KERN_INFO
"ide-floppy: %s: I/O error\n",
824 if (pc
->c
[0] == IDEFLOPPY_REQUEST_SENSE_CMD
) {
825 printk(KERN_ERR
"ide-floppy: I/O error in "
826 "request sense command\n");
827 return ide_do_reset(drive
);
829 /* Retry operation */
830 idefloppy_retry_pc(drive
);
831 /* queued, but not started */
835 if (floppy
->failed_pc
== pc
)
836 floppy
->failed_pc
= NULL
;
837 /* Command finished - Call the callback function */
842 if (test_and_clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
843 printk(KERN_ERR
"ide-floppy: The floppy wants to issue "
844 "more interrupts in DMA mode\n");
846 return ide_do_reset(drive
);
849 /* Get the number of bytes to transfer */
850 bcount
= (hwif
->INB(IDE_BCOUNTH_REG
) << 8) |
851 hwif
->INB(IDE_BCOUNTL_REG
);
852 /* on this interrupt */
853 ireason
= hwif
->INB(IDE_IREASON_REG
);
856 printk(KERN_ERR
"ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
857 return ide_do_reset(drive
);
859 if (((ireason
& IO
) == IO
) == test_bit(PC_WRITING
, &pc
->flags
)) {
860 /* Hopefully, we will never get here */
861 printk(KERN_ERR
"ide-floppy: We wanted to %s, ",
862 (ireason
& IO
) ? "Write" : "Read");
863 printk(KERN_ERR
"but the floppy wants us to %s !\n",
864 (ireason
& IO
) ? "Read" : "Write");
865 return ide_do_reset(drive
);
867 if (!test_bit(PC_WRITING
, &pc
->flags
)) {
868 /* Reading - Check that we have enough space */
869 temp
= pc
->actually_transferred
+ bcount
;
870 if (temp
> pc
->request_transfer
) {
871 if (temp
> pc
->buffer_size
) {
872 printk(KERN_ERR
"ide-floppy: The floppy wants "
873 "to send us more data than expected "
874 "- discarding data\n");
875 idefloppy_discard_data(drive
, bcount
);
876 BUG_ON(HWGROUP(drive
)->handler
!= NULL
);
877 ide_set_handler(drive
,
883 debug_log(KERN_NOTICE
"ide-floppy: The floppy wants to "
884 "send us more data than expected - "
885 "allowing transfer\n");
888 if (test_bit(PC_WRITING
, &pc
->flags
)) {
889 if (pc
->buffer
!= NULL
)
890 /* Write the current buffer */
891 hwif
->atapi_output_bytes(drive
, pc
->current_position
,
894 idefloppy_output_buffers(drive
, pc
, bcount
);
896 if (pc
->buffer
!= NULL
)
897 /* Read the current buffer */
898 hwif
->atapi_input_bytes(drive
, pc
->current_position
,
901 idefloppy_input_buffers(drive
, pc
, bcount
);
903 /* Update the current position */
904 pc
->actually_transferred
+= bcount
;
905 pc
->current_position
+= bcount
;
907 BUG_ON(HWGROUP(drive
)->handler
!= NULL
);
908 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
); /* And set the interrupt handler again */
913 * This is the original routine that did the packet transfer.
914 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
915 * for that drive below. The algorithm is chosen based on drive type
917 static ide_startstop_t
idefloppy_transfer_pc (ide_drive_t
*drive
)
919 ide_startstop_t startstop
;
920 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
923 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
924 printk(KERN_ERR
"ide-floppy: Strange, packet command "
925 "initiated yet DRQ isn't asserted\n");
928 ireason
= drive
->hwif
->INB(IDE_IREASON_REG
);
929 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
930 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) while "
931 "issuing a packet command\n");
932 return ide_do_reset(drive
);
934 BUG_ON(HWGROUP(drive
)->handler
!= NULL
);
935 /* Set the interrupt routine */
936 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
937 /* Send the actual packet */
938 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
944 * What we have here is a classic case of a top half / bottom half
945 * interrupt service routine. In interrupt mode, the device sends
946 * an interrupt to signal it's ready to receive a packet. However,
947 * we need to delay about 2-3 ticks before issuing the packet or we
950 * So, follow carefully. transfer_pc1 is called as an interrupt (or
951 * directly). In either case, when the device says it's ready for a
952 * packet, we schedule the packet transfer to occur about 2-3 ticks
953 * later in transfer_pc2.
955 static int idefloppy_transfer_pc2 (ide_drive_t
*drive
)
957 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
959 /* Send the actual packet */
960 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
961 /* Timeout for the packet command */
962 return IDEFLOPPY_WAIT_CMD
;
965 static ide_startstop_t
idefloppy_transfer_pc1 (ide_drive_t
*drive
)
967 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
968 ide_startstop_t startstop
;
971 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
972 printk(KERN_ERR
"ide-floppy: Strange, packet command "
973 "initiated yet DRQ isn't asserted\n");
976 ireason
= drive
->hwif
->INB(IDE_IREASON_REG
);
977 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
978 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) "
979 "while issuing a packet command\n");
980 return ide_do_reset(drive
);
983 * The following delay solves a problem with ATAPI Zip 100 drives
984 * where the Busy flag was apparently being deasserted before the
985 * unit was ready to receive data. This was happening on a
986 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
987 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
988 * used until after the packet is moved in about 50 msec.
990 BUG_ON(HWGROUP(drive
)->handler
!= NULL
);
991 ide_set_handler(drive
,
992 &idefloppy_pc_intr
, /* service routine for packet command */
993 floppy
->ticks
, /* wait this long before "failing" */
994 &idefloppy_transfer_pc2
); /* fail == transfer_pc2 */
999 * idefloppy_should_report_error()
1001 * Supresses error messages resulting from Medium not present
1003 static inline int idefloppy_should_report_error(idefloppy_floppy_t
*floppy
)
1005 if (floppy
->sense_key
== 0x02 &&
1006 floppy
->asc
== 0x3a &&
1007 floppy
->ascq
== 0x00)
1013 * Issue a packet command
1015 static ide_startstop_t
idefloppy_issue_pc (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
1017 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1018 ide_hwif_t
*hwif
= drive
->hwif
;
1019 ide_handler_t
*pkt_xfer_routine
;
1023 if (floppy
->failed_pc
== NULL
&&
1024 pc
->c
[0] != IDEFLOPPY_REQUEST_SENSE_CMD
)
1025 floppy
->failed_pc
= pc
;
1026 /* Set the current packet command */
1029 if (pc
->retries
> IDEFLOPPY_MAX_PC_RETRIES
||
1030 test_bit(PC_ABORT
, &pc
->flags
)) {
1032 * We will "abort" retrying a packet command in case
1033 * a legitimate error code was received.
1035 if (!test_bit(PC_ABORT
, &pc
->flags
)) {
1036 if (!test_bit(PC_SUPPRESS_ERROR
, &pc
->flags
)) {
1037 if (idefloppy_should_report_error(floppy
))
1038 printk(KERN_ERR
"ide-floppy: %s: I/O error, "
1039 "pc = %2x, key = %2x, "
1040 "asc = %2x, ascq = %2x\n",
1041 drive
->name
, pc
->c
[0],
1043 floppy
->asc
, floppy
->ascq
);
1046 pc
->error
= IDEFLOPPY_ERROR_GENERAL
;
1048 floppy
->failed_pc
= NULL
;
1049 pc
->callback(drive
);
1053 debug_log(KERN_INFO
"Retry number - %d\n",pc
->retries
);
1056 /* We haven't transferred any data yet */
1057 pc
->actually_transferred
= 0;
1058 pc
->current_position
= pc
->buffer
;
1059 bcount
= min(pc
->request_transfer
, 63 * 1024);
1061 if (test_and_clear_bit(PC_DMA_ERROR
, &pc
->flags
))
1066 if (test_bit(PC_DMA_RECOMMENDED
, &pc
->flags
) && drive
->using_dma
)
1067 dma
= !hwif
->dma_setup(drive
);
1069 ide_pktcmd_tf_load(drive
, IDE_TFLAG_NO_SELECT_MASK
|
1070 IDE_TFLAG_OUT_DEVICE
, bcount
, dma
);
1072 if (dma
) { /* Begin DMA, if necessary */
1073 set_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
1074 hwif
->dma_start(drive
);
1077 /* Can we transfer the packet when we get the interrupt or wait? */
1078 if (test_bit(IDEFLOPPY_ZIP_DRIVE
, &floppy
->flags
)) {
1080 pkt_xfer_routine
= &idefloppy_transfer_pc1
;
1083 pkt_xfer_routine
= &idefloppy_transfer_pc
;
1086 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
)) {
1087 /* Issue the packet command */
1088 ide_execute_command(drive
, WIN_PACKETCMD
,
1094 /* Issue the packet command */
1095 HWIF(drive
)->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
1096 return (*pkt_xfer_routine
) (drive
);
1100 static void idefloppy_rw_callback (ide_drive_t
*drive
)
1102 debug_log(KERN_INFO
"ide-floppy: Reached idefloppy_rw_callback\n");
1104 idefloppy_do_end_request(drive
, 1, 0);
1108 static void idefloppy_create_prevent_cmd (idefloppy_pc_t
*pc
, int prevent
)
1110 debug_log(KERN_INFO
"ide-floppy: creating prevent removal command, "
1111 "prevent = %d\n", prevent
);
1113 idefloppy_init_pc(pc
);
1114 pc
->c
[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD
;
1118 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t
*pc
)
1120 idefloppy_init_pc(pc
);
1121 pc
->c
[0] = IDEFLOPPY_READ_CAPACITY_CMD
;
1124 pc
->request_transfer
= 255;
1127 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t
*pc
, int b
, int l
,
1130 idefloppy_init_pc(pc
);
1131 pc
->c
[0] = IDEFLOPPY_FORMAT_UNIT_CMD
;
1134 memset(pc
->buffer
, 0, 12);
1135 pc
->buffer
[1] = 0xA2;
1136 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1138 if (flags
& 1) /* Verify bit on... */
1139 pc
->buffer
[1] ^= 0x20; /* ... turn off DCRT bit */
1142 put_unaligned(htonl(b
), (unsigned int *)(&pc
->buffer
[4]));
1143 put_unaligned(htonl(l
), (unsigned int *)(&pc
->buffer
[8]));
1145 set_bit(PC_WRITING
, &pc
->flags
);
1149 * A mode sense command is used to "sense" floppy parameters.
1151 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t
*pc
, u8 page_code
, u8 type
)
1153 u16 length
= sizeof(idefloppy_mode_parameter_header_t
);
1155 idefloppy_init_pc(pc
);
1156 pc
->c
[0] = IDEFLOPPY_MODE_SENSE_CMD
;
1158 pc
->c
[2] = page_code
+ (type
<< 6);
1160 switch (page_code
) {
1161 case IDEFLOPPY_CAPABILITIES_PAGE
:
1164 case IDEFLOPPY_FLEXIBLE_DISK_PAGE
:
1168 printk(KERN_ERR
"ide-floppy: unsupported page code "
1169 "in create_mode_sense_cmd\n");
1171 put_unaligned(htons(length
), (u16
*) &pc
->c
[7]);
1172 pc
->request_transfer
= length
;
1175 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t
*pc
, int start
)
1177 idefloppy_init_pc(pc
);
1178 pc
->c
[0] = IDEFLOPPY_START_STOP_CMD
;
1182 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t
*pc
)
1184 idefloppy_init_pc(pc
);
1185 pc
->c
[0] = IDEFLOPPY_TEST_UNIT_READY_CMD
;
1188 static void idefloppy_create_rw_cmd (idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
, struct request
*rq
, unsigned long sector
)
1190 int block
= sector
/ floppy
->bs_factor
;
1191 int blocks
= rq
->nr_sectors
/ floppy
->bs_factor
;
1192 int cmd
= rq_data_dir(rq
);
1194 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1195 2 * test_bit (IDEFLOPPY_USE_READ12
, &floppy
->flags
),
1198 idefloppy_init_pc(pc
);
1199 if (test_bit(IDEFLOPPY_USE_READ12
, &floppy
->flags
)) {
1200 pc
->c
[0] = cmd
== READ
? IDEFLOPPY_READ12_CMD
: IDEFLOPPY_WRITE12_CMD
;
1201 put_unaligned(htonl(blocks
), (unsigned int *) &pc
->c
[6]);
1203 pc
->c
[0] = cmd
== READ
? IDEFLOPPY_READ10_CMD
: IDEFLOPPY_WRITE10_CMD
;
1204 put_unaligned(htons(blocks
), (unsigned short *) &pc
->c
[7]);
1206 put_unaligned(htonl(block
), (unsigned int *) &pc
->c
[2]);
1207 pc
->callback
= &idefloppy_rw_callback
;
1209 pc
->b_count
= cmd
== READ
? 0 : rq
->bio
->bi_size
;
1210 if (rq
->cmd_flags
& REQ_RW
)
1211 set_bit(PC_WRITING
, &pc
->flags
);
1213 pc
->request_transfer
= pc
->buffer_size
= blocks
* floppy
->block_size
;
1214 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
1218 idefloppy_blockpc_cmd(idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
, struct request
*rq
)
1220 idefloppy_init_pc(pc
);
1221 pc
->callback
= &idefloppy_rw_callback
;
1222 memcpy(pc
->c
, rq
->cmd
, sizeof(pc
->c
));
1224 pc
->b_count
= rq
->data_len
;
1225 if (rq
->data_len
&& rq_data_dir(rq
) == WRITE
)
1226 set_bit(PC_WRITING
, &pc
->flags
);
1227 pc
->buffer
= rq
->data
;
1229 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
1232 * possibly problematic, doesn't look like ide-floppy correctly
1233 * handled scattered requests if dma fails...
1235 pc
->request_transfer
= pc
->buffer_size
= rq
->data_len
;
1239 * idefloppy_do_request is our request handling function.
1241 static ide_startstop_t
idefloppy_do_request (ide_drive_t
*drive
, struct request
*rq
, sector_t block_s
)
1243 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1245 unsigned long block
= (unsigned long)block_s
;
1247 debug_log(KERN_INFO
"dev: %s, flags: %lx, errors: %d\n",
1248 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?",
1249 rq
->flags
, rq
->errors
);
1250 debug_log(KERN_INFO
"sector: %ld, nr_sectors: %ld, "
1251 "current_nr_sectors: %d\n", (long)rq
->sector
,
1252 rq
->nr_sectors
, rq
->current_nr_sectors
);
1254 if (rq
->errors
>= ERROR_MAX
) {
1255 if (floppy
->failed_pc
!= NULL
) {
1256 if (idefloppy_should_report_error(floppy
))
1257 printk(KERN_ERR
"ide-floppy: %s: I/O error, pc = %2x,"
1258 " key = %2x, asc = %2x, ascq = %2x\n",
1259 drive
->name
, floppy
->failed_pc
->c
[0],
1260 floppy
->sense_key
, floppy
->asc
, floppy
->ascq
);
1263 printk(KERN_ERR
"ide-floppy: %s: I/O error\n",
1265 idefloppy_do_end_request(drive
, 0, 0);
1268 if (blk_fs_request(rq
)) {
1269 if (((long)rq
->sector
% floppy
->bs_factor
) ||
1270 (rq
->nr_sectors
% floppy
->bs_factor
)) {
1271 printk("%s: unsupported r/w request size\n",
1273 idefloppy_do_end_request(drive
, 0, 0);
1276 pc
= idefloppy_next_pc_storage(drive
);
1277 idefloppy_create_rw_cmd(floppy
, pc
, rq
, block
);
1278 } else if (blk_special_request(rq
)) {
1279 pc
= (idefloppy_pc_t
*) rq
->buffer
;
1280 } else if (blk_pc_request(rq
)) {
1281 pc
= idefloppy_next_pc_storage(drive
);
1282 idefloppy_blockpc_cmd(floppy
, pc
, rq
);
1284 blk_dump_rq_flags(rq
,
1285 "ide-floppy: unsupported command in queue");
1286 idefloppy_do_end_request(drive
, 0, 0);
1291 return idefloppy_issue_pc(drive
, pc
);
1295 * idefloppy_queue_pc_tail adds a special packet command request to the
1296 * tail of the request queue, and waits for it to be serviced.
1298 static int idefloppy_queue_pc_tail (ide_drive_t
*drive
,idefloppy_pc_t
*pc
)
1300 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
1303 ide_init_drive_cmd (&rq
);
1304 rq
.buffer
= (char *) pc
;
1305 rq
.cmd_type
= REQ_TYPE_SPECIAL
;
1306 rq
.rq_disk
= floppy
->disk
;
1308 return ide_do_drive_cmd(drive
, &rq
, ide_wait
);
1312 * Look at the flexible disk page parameters. We will ignore the CHS
1313 * capacity parameters and use the LBA parameters instead.
1315 static int idefloppy_get_flexible_disk_page (ide_drive_t
*drive
)
1317 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1319 idefloppy_mode_parameter_header_t
*header
;
1320 idefloppy_flexible_disk_page_t
*page
;
1321 int capacity
, lba_capacity
;
1323 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_FLEXIBLE_DISK_PAGE
, MODE_SENSE_CURRENT
);
1324 if (idefloppy_queue_pc_tail(drive
,&pc
)) {
1325 printk(KERN_ERR
"ide-floppy: Can't get flexible disk "
1326 "page parameters\n");
1329 header
= (idefloppy_mode_parameter_header_t
*) pc
.buffer
;
1330 floppy
->wp
= header
->wp
;
1331 set_disk_ro(floppy
->disk
, floppy
->wp
);
1332 page
= (idefloppy_flexible_disk_page_t
*) (header
+ 1);
1334 page
->transfer_rate
= ntohs(page
->transfer_rate
);
1335 page
->sector_size
= ntohs(page
->sector_size
);
1336 page
->cyls
= ntohs(page
->cyls
);
1337 page
->rpm
= ntohs(page
->rpm
);
1338 capacity
= page
->cyls
* page
->heads
* page
->sectors
* page
->sector_size
;
1339 if (memcmp (page
, &floppy
->flexible_disk_page
, sizeof (idefloppy_flexible_disk_page_t
)))
1340 printk(KERN_INFO
"%s: %dkB, %d/%d/%d CHS, %d kBps, "
1341 "%d sector size, %d rpm\n",
1342 drive
->name
, capacity
/ 1024, page
->cyls
,
1343 page
->heads
, page
->sectors
,
1344 page
->transfer_rate
/ 8, page
->sector_size
, page
->rpm
);
1346 floppy
->flexible_disk_page
= *page
;
1347 drive
->bios_cyl
= page
->cyls
;
1348 drive
->bios_head
= page
->heads
;
1349 drive
->bios_sect
= page
->sectors
;
1350 lba_capacity
= floppy
->blocks
* floppy
->block_size
;
1351 if (capacity
< lba_capacity
) {
1352 printk(KERN_NOTICE
"%s: The disk reports a capacity of %d "
1353 "bytes, but the drive only handles %d\n",
1354 drive
->name
, lba_capacity
, capacity
);
1355 floppy
->blocks
= floppy
->block_size
? capacity
/ floppy
->block_size
: 0;
1360 static int idefloppy_get_capability_page(ide_drive_t
*drive
)
1362 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1364 idefloppy_mode_parameter_header_t
*header
;
1365 idefloppy_capabilities_page_t
*page
;
1368 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_CAPABILITIES_PAGE
,
1369 MODE_SENSE_CURRENT
);
1371 set_bit(PC_SUPPRESS_ERROR
, &pc
.flags
);
1372 if (idefloppy_queue_pc_tail(drive
,&pc
)) {
1376 header
= (idefloppy_mode_parameter_header_t
*) pc
.buffer
;
1377 page
= (idefloppy_capabilities_page_t
*)(header
+1);
1378 floppy
->srfp
= page
->srfp
;
1383 * Determine if a media is present in the floppy drive, and if so,
1386 static int idefloppy_get_capacity (ide_drive_t
*drive
)
1388 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1390 idefloppy_capacity_header_t
*header
;
1391 idefloppy_capacity_descriptor_t
*descriptor
;
1392 int i
, descriptors
, rc
= 1, blocks
, length
;
1394 drive
->bios_cyl
= 0;
1395 drive
->bios_head
= drive
->bios_sect
= 0;
1397 floppy
->bs_factor
= 1;
1398 set_capacity(floppy
->disk
, 0);
1400 idefloppy_create_read_capacity_cmd(&pc
);
1401 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1402 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1405 header
= (idefloppy_capacity_header_t
*) pc
.buffer
;
1406 descriptors
= header
->length
/ sizeof(idefloppy_capacity_descriptor_t
);
1407 descriptor
= (idefloppy_capacity_descriptor_t
*) (header
+ 1);
1409 for (i
= 0; i
< descriptors
; i
++, descriptor
++) {
1410 blocks
= descriptor
->blocks
= ntohl(descriptor
->blocks
);
1411 length
= descriptor
->length
= ntohs(descriptor
->length
);
1415 switch (descriptor
->dc
) {
1416 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1417 case CAPACITY_UNFORMATTED
:
1418 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
))
1420 * If it is not a clik drive, break out
1421 * (maintains previous driver behaviour)
1424 case CAPACITY_CURRENT
:
1425 /* Normal Zip/LS-120 disks */
1426 if (memcmp(descriptor
, &floppy
->capacity
, sizeof (idefloppy_capacity_descriptor_t
)))
1427 printk(KERN_INFO
"%s: %dkB, %d blocks, %d "
1428 "sector size\n", drive
->name
,
1429 blocks
* length
/ 1024, blocks
, length
);
1430 floppy
->capacity
= *descriptor
;
1431 if (!length
|| length
% 512) {
1432 printk(KERN_NOTICE
"%s: %d bytes block size "
1433 "not supported\n", drive
->name
, length
);
1435 floppy
->blocks
= blocks
;
1436 floppy
->block_size
= length
;
1437 if ((floppy
->bs_factor
= length
/ 512) != 1)
1438 printk(KERN_NOTICE
"%s: warning: non "
1439 "512 bytes block size not "
1440 "fully supported\n",
1445 case CAPACITY_NO_CARTRIDGE
:
1447 * This is a KERN_ERR so it appears on screen
1448 * for the user to see
1450 printk(KERN_ERR
"%s: No disk in drive\n", drive
->name
);
1452 case CAPACITY_INVALID
:
1453 printk(KERN_ERR
"%s: Invalid capacity for disk "
1454 "in drive\n", drive
->name
);
1459 debug_log( "Descriptor 0 Code: %d\n",
1462 debug_log( "Descriptor %d: %dkB, %d blocks, %d "
1463 "sector size\n", i
, blocks
* length
/ 1024, blocks
,
1467 /* Clik! disk does not support get_flexible_disk_page */
1468 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1469 (void) idefloppy_get_flexible_disk_page(drive
);
1472 set_capacity(floppy
->disk
, floppy
->blocks
* floppy
->bs_factor
);
1477 ** Obtain the list of formattable capacities.
1478 ** Very similar to idefloppy_get_capacity, except that we push the capacity
1479 ** descriptors to userland, instead of our own structures.
1481 ** Userland gives us the following structure:
1483 ** struct idefloppy_format_capacities {
1491 ** userland initializes nformats to the number of allocated formats[]
1492 ** records. On exit we set nformats to the number of records we've
1493 ** actually initialized.
1497 static int idefloppy_get_format_capacities(ide_drive_t
*drive
, int __user
*arg
)
1500 idefloppy_capacity_header_t
*header
;
1501 idefloppy_capacity_descriptor_t
*descriptor
;
1502 int i
, descriptors
, blocks
, length
;
1507 if (get_user(u_array_size
, arg
))
1510 if (u_array_size
<= 0)
1513 idefloppy_create_read_capacity_cmd(&pc
);
1514 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1515 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1518 header
= (idefloppy_capacity_header_t
*) pc
.buffer
;
1519 descriptors
= header
->length
/
1520 sizeof(idefloppy_capacity_descriptor_t
);
1521 descriptor
= (idefloppy_capacity_descriptor_t
*) (header
+ 1);
1527 ** We always skip the first capacity descriptor. That's the
1528 ** current capacity. We are interested in the remaining descriptors,
1529 ** the formattable capacities.
1532 for (i
=0; i
<descriptors
; i
++, descriptor
++) {
1533 if (u_index
>= u_array_size
)
1534 break; /* User-supplied buffer too small */
1536 continue; /* Skip the first descriptor */
1538 blocks
= ntohl(descriptor
->blocks
);
1539 length
= ntohs(descriptor
->length
);
1541 if (put_user(blocks
, argp
))
1545 if (put_user(length
, argp
))
1552 if (put_user(u_index
, arg
))
1558 ** Send ATAPI_FORMAT_UNIT to the drive.
1560 ** Userland gives us the following structure:
1562 ** struct idefloppy_format_command {
1568 ** flags is a bitmask, currently, the only defined flag is:
1570 ** 0x01 - verify media after format.
1573 static int idefloppy_begin_format(ide_drive_t
*drive
, int __user
*arg
)
1580 if (get_user(blocks
, arg
) ||
1581 get_user(length
, arg
+1) ||
1582 get_user(flags
, arg
+2)) {
1586 /* Get the SFRP bit */
1587 (void) idefloppy_get_capability_page(drive
);
1588 idefloppy_create_format_unit_cmd(&pc
, blocks
, length
, flags
);
1589 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1597 ** Get ATAPI_FORMAT_UNIT progress indication.
1599 ** Userland gives a pointer to an int. The int is set to a progress
1600 ** indicator 0-65536, with 65536=100%.
1602 ** If the drive does not support format progress indication, we just check
1603 ** the dsc bit, and return either 0 or 65536.
1606 static int idefloppy_get_format_progress(ide_drive_t
*drive
, int __user
*arg
)
1608 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1610 int progress_indication
= 0x10000;
1613 idefloppy_create_request_sense_cmd(&pc
);
1614 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1618 if (floppy
->sense_key
== 2 &&
1620 floppy
->ascq
== 4) {
1621 progress_indication
= floppy
->progress_indication
;
1623 /* Else assume format_unit has finished, and we're
1626 unsigned long flags
;
1629 local_irq_save(flags
);
1630 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
1631 local_irq_restore(flags
);
1633 progress_indication
= ((stat
& SEEK_STAT
) == 0) ? 0 : 0x10000;
1635 if (put_user(progress_indication
, arg
))
1642 * Return the current floppy capacity.
1644 static sector_t
idefloppy_capacity (ide_drive_t
*drive
)
1646 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1647 unsigned long capacity
= floppy
->blocks
* floppy
->bs_factor
;
1653 * idefloppy_identify_device checks if we can support a drive,
1654 * based on the ATAPI IDENTIFY command results.
1656 static int idefloppy_identify_device (ide_drive_t
*drive
,struct hd_driveid
*id
)
1658 struct idefloppy_id_gcw gcw
;
1659 #if IDEFLOPPY_DEBUG_INFO
1661 #endif /* IDEFLOPPY_DEBUG_INFO */
1663 *((u16
*) &gcw
) = id
->config
;
1666 /* kludge for Apple PowerBook internal zip */
1667 if ((gcw
.device_type
== 5) &&
1668 !strstr(id
->model
, "CD-ROM") &&
1669 strstr(id
->model
, "ZIP"))
1670 gcw
.device_type
= 0;
1673 #if IDEFLOPPY_DEBUG_INFO
1674 printk(KERN_INFO
"Dumping ATAPI Identify Device floppy parameters\n");
1675 switch (gcw
.protocol
) {
1676 case 0: case 1: sprintf(buffer
, "ATA");break;
1677 case 2: sprintf(buffer
, "ATAPI");break;
1678 case 3: sprintf(buffer
, "Reserved (Unknown to ide-floppy)");break;
1680 printk(KERN_INFO
"Protocol Type: %s\n", buffer
);
1681 switch (gcw
.device_type
) {
1682 case 0: sprintf(buffer
, "Direct-access Device");break;
1683 case 1: sprintf(buffer
, "Streaming Tape Device");break;
1684 case 2: case 3: case 4: sprintf (buffer
, "Reserved");break;
1685 case 5: sprintf(buffer
, "CD-ROM Device");break;
1686 case 6: sprintf(buffer
, "Reserved");
1687 case 7: sprintf(buffer
, "Optical memory Device");break;
1688 case 0x1f: sprintf(buffer
, "Unknown or no Device type");break;
1689 default: sprintf(buffer
, "Reserved");
1691 printk(KERN_INFO
"Device Type: %x - %s\n", gcw
.device_type
, buffer
);
1692 printk(KERN_INFO
"Removable: %s\n",gcw
.removable
? "Yes":"No");
1693 switch (gcw
.drq_type
) {
1694 case 0: sprintf(buffer
, "Microprocessor DRQ");break;
1695 case 1: sprintf(buffer
, "Interrupt DRQ");break;
1696 case 2: sprintf(buffer
, "Accelerated DRQ");break;
1697 case 3: sprintf(buffer
, "Reserved");break;
1699 printk(KERN_INFO
"Command Packet DRQ Type: %s\n", buffer
);
1700 switch (gcw
.packet_size
) {
1701 case 0: sprintf(buffer
, "12 bytes");break;
1702 case 1: sprintf(buffer
, "16 bytes");break;
1703 default: sprintf(buffer
, "Reserved");break;
1705 printk(KERN_INFO
"Command Packet Size: %s\n", buffer
);
1706 #endif /* IDEFLOPPY_DEBUG_INFO */
1708 if (gcw
.protocol
!= 2)
1709 printk(KERN_ERR
"ide-floppy: Protocol is not ATAPI\n");
1710 else if (gcw
.device_type
!= 0)
1711 printk(KERN_ERR
"ide-floppy: Device type is not set to floppy\n");
1712 else if (!gcw
.removable
)
1713 printk(KERN_ERR
"ide-floppy: The removable flag is not set\n");
1714 else if (gcw
.drq_type
== 3) {
1715 printk(KERN_ERR
"ide-floppy: Sorry, DRQ type %d not supported\n", gcw
.drq_type
);
1716 } else if (gcw
.packet_size
!= 0) {
1717 printk(KERN_ERR
"ide-floppy: Packet size is not 12 bytes long\n");
1723 #ifdef CONFIG_IDE_PROC_FS
1724 static void idefloppy_add_settings(ide_drive_t
*drive
)
1726 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1729 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1731 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, TYPE_INT
, 0, 1023, 1, 1, &drive
->bios_cyl
, NULL
);
1732 ide_add_setting(drive
, "bios_head", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
1733 ide_add_setting(drive
, "bios_sect", SETTING_RW
, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
1734 ide_add_setting(drive
, "ticks", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1, &floppy
->ticks
, NULL
);
1737 static inline void idefloppy_add_settings(ide_drive_t
*drive
) { ; }
1741 * Driver initialization.
1743 static void idefloppy_setup (ide_drive_t
*drive
, idefloppy_floppy_t
*floppy
)
1745 struct idefloppy_id_gcw gcw
;
1747 *((u16
*) &gcw
) = drive
->id
->config
;
1748 floppy
->pc
= floppy
->pc_stack
;
1749 if (gcw
.drq_type
== 1)
1750 set_bit(IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
);
1752 * We used to check revisions here. At this point however
1753 * I'm giving up. Just assume they are all broken, its easier.
1755 * The actual reason for the workarounds was likely
1756 * a driver bug after all rather than a firmware bug,
1757 * and the workaround below used to hide it. It should
1758 * be fixed as of version 1.9, but to be on the safe side
1759 * we'll leave the limitation below for the 2.2.x tree.
1762 if (!strncmp(drive
->id
->model
, "IOMEGA ZIP 100 ATAPI", 20)) {
1763 set_bit(IDEFLOPPY_ZIP_DRIVE
, &floppy
->flags
);
1764 /* This value will be visible in the /proc/ide/hdx/settings */
1765 floppy
->ticks
= IDEFLOPPY_TICKS_DELAY
;
1766 blk_queue_max_sectors(drive
->queue
, 64);
1770 * Guess what? The IOMEGA Clik! drive also needs the
1771 * above fix. It makes nasty clicking noises without
1772 * it, so please don't remove this.
1774 if (strncmp(drive
->id
->model
, "IOMEGA Clik!", 11) == 0) {
1775 blk_queue_max_sectors(drive
->queue
, 64);
1776 set_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
);
1780 (void) idefloppy_get_capacity(drive
);
1781 idefloppy_add_settings(drive
);
1784 static void ide_floppy_remove(ide_drive_t
*drive
)
1786 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1787 struct gendisk
*g
= floppy
->disk
;
1789 ide_proc_unregister_driver(drive
, floppy
->driver
);
1793 ide_floppy_put(floppy
);
1796 static void ide_floppy_release(struct kref
*kref
)
1798 struct ide_floppy_obj
*floppy
= to_ide_floppy(kref
);
1799 ide_drive_t
*drive
= floppy
->drive
;
1800 struct gendisk
*g
= floppy
->disk
;
1802 drive
->driver_data
= NULL
;
1803 g
->private_data
= NULL
;
1808 #ifdef CONFIG_IDE_PROC_FS
1809 static int proc_idefloppy_read_capacity
1810 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1812 ide_drive_t
*drive
= (ide_drive_t
*)data
;
1815 len
= sprintf(page
,"%llu\n", (long long)idefloppy_capacity(drive
));
1816 PROC_IDE_READ_RETURN(page
,start
,off
,count
,eof
,len
);
1819 static ide_proc_entry_t idefloppy_proc
[] = {
1820 { "capacity", S_IFREG
|S_IRUGO
, proc_idefloppy_read_capacity
, NULL
},
1821 { "geometry", S_IFREG
|S_IRUGO
, proc_ide_read_geometry
, NULL
},
1822 { NULL
, 0, NULL
, NULL
}
1824 #endif /* CONFIG_IDE_PROC_FS */
1826 static int ide_floppy_probe(ide_drive_t
*);
1828 static ide_driver_t idefloppy_driver
= {
1830 .owner
= THIS_MODULE
,
1831 .name
= "ide-floppy",
1832 .bus
= &ide_bus_type
,
1834 .probe
= ide_floppy_probe
,
1835 .remove
= ide_floppy_remove
,
1836 .version
= IDEFLOPPY_VERSION
,
1837 .media
= ide_floppy
,
1838 .supports_dsc_overlap
= 0,
1839 .do_request
= idefloppy_do_request
,
1840 .end_request
= idefloppy_do_end_request
,
1841 .error
= __ide_error
,
1842 .abort
= __ide_abort
,
1843 #ifdef CONFIG_IDE_PROC_FS
1844 .proc
= idefloppy_proc
,
1848 static int idefloppy_open(struct inode
*inode
, struct file
*filp
)
1850 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1851 struct ide_floppy_obj
*floppy
;
1856 debug_log(KERN_INFO
"Reached idefloppy_open\n");
1858 if (!(floppy
= ide_floppy_get(disk
)))
1861 drive
= floppy
->drive
;
1865 if (floppy
->openers
== 1) {
1866 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1869 idefloppy_create_test_unit_ready_cmd(&pc
);
1870 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1871 idefloppy_create_start_stop_cmd(&pc
, 1);
1872 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1875 if (idefloppy_get_capacity (drive
)
1876 && (filp
->f_flags
& O_NDELAY
) == 0
1878 ** Allow O_NDELAY to open a drive without a disk, or with
1879 ** an unreadable disk, so that we can get the format
1880 ** capacity of the drive or begin the format - Sam
1884 goto out_put_floppy
;
1887 if (floppy
->wp
&& (filp
->f_mode
& 2)) {
1889 goto out_put_floppy
;
1891 set_bit(IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1892 /* IOMEGA Clik! drives do not support lock/unlock commands */
1893 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1894 idefloppy_create_prevent_cmd(&pc
, 1);
1895 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1897 check_disk_change(inode
->i_bdev
);
1898 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
)) {
1900 goto out_put_floppy
;
1906 ide_floppy_put(floppy
);
1910 static int idefloppy_release(struct inode
*inode
, struct file
*filp
)
1912 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1913 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1914 ide_drive_t
*drive
= floppy
->drive
;
1917 debug_log(KERN_INFO
"Reached idefloppy_release\n");
1919 if (floppy
->openers
== 1) {
1920 /* IOMEGA Clik! drives do not support lock/unlock commands */
1921 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1922 idefloppy_create_prevent_cmd(&pc
, 0);
1923 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1926 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1931 ide_floppy_put(floppy
);
1936 static int idefloppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1938 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1939 ide_drive_t
*drive
= floppy
->drive
;
1941 geo
->heads
= drive
->bios_head
;
1942 geo
->sectors
= drive
->bios_sect
;
1943 geo
->cylinders
= (u16
)drive
->bios_cyl
; /* truncate */
1947 static int idefloppy_ioctl(struct inode
*inode
, struct file
*file
,
1948 unsigned int cmd
, unsigned long arg
)
1950 struct block_device
*bdev
= inode
->i_bdev
;
1951 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1952 ide_drive_t
*drive
= floppy
->drive
;
1953 void __user
*argp
= (void __user
*)arg
;
1955 int prevent
= (arg
) ? 1 : 0;
1962 case CDROM_LOCKDOOR
:
1963 if (floppy
->openers
> 1)
1966 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1967 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1968 idefloppy_create_prevent_cmd(&pc
, prevent
);
1969 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1971 if (cmd
== CDROMEJECT
) {
1972 idefloppy_create_start_stop_cmd(&pc
, 2);
1973 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1976 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED
:
1978 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY
:
1979 return idefloppy_get_format_capacities(drive
, argp
);
1980 case IDEFLOPPY_IOCTL_FORMAT_START
:
1982 if (!(file
->f_mode
& 2))
1985 if (floppy
->openers
> 1) {
1986 /* Don't format if someone is using the disk */
1988 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
,
1993 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1995 err
= idefloppy_begin_format(drive
, argp
);
1997 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
2000 ** Note, the bit will be cleared when the device is
2001 ** closed. This is the cleanest way to handle the
2002 ** situation where the drive does not support
2003 ** format progress reporting.
2005 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS
:
2006 return idefloppy_get_format_progress(drive
, argp
);
2010 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
2011 * and CDROM_SEND_PACKET (legacy) ioctls
2013 if (cmd
!= CDROM_SEND_PACKET
&& cmd
!= SCSI_IOCTL_SEND_COMMAND
)
2014 err
= scsi_cmd_ioctl(file
, bdev
->bd_disk
->queue
,
2015 bdev
->bd_disk
, cmd
, argp
);
2020 err
= generic_ide_ioctl(drive
, file
, bdev
, cmd
, arg
);
2025 static int idefloppy_media_changed(struct gendisk
*disk
)
2027 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
2028 ide_drive_t
*drive
= floppy
->drive
;
2030 /* do not scan partitions twice if this is a removable device */
2031 if (drive
->attach
) {
2035 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
2038 static int idefloppy_revalidate_disk(struct gendisk
*disk
)
2040 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
2041 set_capacity(disk
, idefloppy_capacity(floppy
->drive
));
2045 static struct block_device_operations idefloppy_ops
= {
2046 .owner
= THIS_MODULE
,
2047 .open
= idefloppy_open
,
2048 .release
= idefloppy_release
,
2049 .ioctl
= idefloppy_ioctl
,
2050 .getgeo
= idefloppy_getgeo
,
2051 .media_changed
= idefloppy_media_changed
,
2052 .revalidate_disk
= idefloppy_revalidate_disk
2055 static int ide_floppy_probe(ide_drive_t
*drive
)
2057 idefloppy_floppy_t
*floppy
;
2060 if (!strstr("ide-floppy", drive
->driver_req
))
2062 if (!drive
->present
)
2064 if (drive
->media
!= ide_floppy
)
2066 if (!idefloppy_identify_device (drive
, drive
->id
)) {
2067 printk (KERN_ERR
"ide-floppy: %s: not supported by this version of ide-floppy\n", drive
->name
);
2071 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive
->name
);
2074 if ((floppy
= kzalloc(sizeof (idefloppy_floppy_t
), GFP_KERNEL
)) == NULL
) {
2075 printk (KERN_ERR
"ide-floppy: %s: Can't allocate a floppy structure\n", drive
->name
);
2079 g
= alloc_disk(1 << PARTN_BITS
);
2081 goto out_free_floppy
;
2083 ide_init_disk(g
, drive
);
2085 ide_proc_register_driver(drive
, &idefloppy_driver
);
2087 kref_init(&floppy
->kref
);
2089 floppy
->drive
= drive
;
2090 floppy
->driver
= &idefloppy_driver
;
2093 g
->private_data
= &floppy
->driver
;
2095 drive
->driver_data
= floppy
;
2097 idefloppy_setup (drive
, floppy
);
2099 g
->minors
= 1 << PARTN_BITS
;
2100 g
->driverfs_dev
= &drive
->gendev
;
2101 g
->flags
= drive
->removable
? GENHD_FL_REMOVABLE
: 0;
2102 g
->fops
= &idefloppy_ops
;
2113 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2115 static void __exit
idefloppy_exit (void)
2117 driver_unregister(&idefloppy_driver
.gen_driver
);
2120 static int __init
idefloppy_init(void)
2122 printk("ide-floppy driver " IDEFLOPPY_VERSION
"\n");
2123 return driver_register(&idefloppy_driver
.gen_driver
);
2126 MODULE_ALIAS("ide:*m-floppy*");
2127 module_init(idefloppy_init
);
2128 module_exit(idefloppy_exit
);
2129 MODULE_LICENSE("GPL");