File: /usr/src/linux/drivers/ide/ide-floppy.c
1 /*
2 * linux/drivers/ide/ide-floppy.c Version 0.97.sv Jan 14 2001
3 *
4 * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2000 - 2001 Paul Bristow <paul@paulbristow.net>
6 */
7
8 /*
9 * IDE ATAPI floppy driver.
10 *
11 * The driver currently doesn't have any fancy features, just the bare
12 * minimum read/write support.
13 *
14 * This driver supports the following IDE floppy drives:
15 *
16 * LS-120 SuperDisk
17 * Iomega Zip 100/250
18 * Iomega PC Card Clik!/PocketZip
19 *
20 * Many thanks to Lode Leroy <Lode.Leroy@www.ibase.be>, who tested so many
21 * ALPHA patches to this driver on an EASYSTOR LS-120 ATAPI floppy drive.
22 *
23 * Ver 0.1 Oct 17 96 Initial test version, mostly based on ide-tape.c.
24 * Ver 0.2 Oct 31 96 Minor changes.
25 * Ver 0.3 Dec 2 96 Fixed error recovery bug.
26 * Ver 0.4 Jan 26 97 Add support for the HDIO_GETGEO ioctl.
27 * Ver 0.5 Feb 21 97 Add partitions support.
28 * Use the minimum of the LBA and CHS capacities.
29 * Avoid hwgroup->rq == NULL on the last irq.
30 * Fix potential null dereferencing with DEBUG_LOG.
31 * Ver 0.8 Dec 7 97 Increase irq timeout from 10 to 50 seconds.
32 * Add media write-protect detection.
33 * Issue START command only if TEST UNIT READY fails.
34 * Add work-around for IOMEGA ZIP revision 21.D.
35 * Remove idefloppy_get_capabilities().
36 * Ver 0.9 Jul 4 99 Fix a bug which might have caused the number of
37 * bytes requested on each interrupt to be zero.
38 * Thanks to <shanos@es.co.nz> for pointing this out.
39 * Ver 0.9.sv Jan 6 01 Sam Varshavchik <mrsam@courier-mta.com>
40 * Implement low level formatting. Reimplemented
41 * IDEFLOPPY_CAPABILITIES_PAGE, since we need the srfp
42 * bit. My LS-120 drive barfs on
43 * IDEFLOPPY_CAPABILITIES_PAGE, but maybe it's just me.
44 * Compromise by not reporting a failure to get this
45 * mode page. Implemented four IOCTLs in order to
46 * implement formatting. IOCTls begin with 0x4600,
47 * 0x46 is 'F' as in Format.
48 * Jan 9 01 Userland option to select format verify.
49 * Added PC_SUPPRESS_ERROR flag - some idefloppy drives
50 * do not implement IDEFLOPPY_CAPABILITIES_PAGE, and
51 * return a sense error. Suppress error reporting in
52 * this particular case in order to avoid spurious
53 * errors in syslog. The culprit is
54 * idefloppy_get_capability_page(), so move it to
55 * idefloppy_begin_format() so that it's not used
56 * unless absolutely necessary.
57 * If drive does not support format progress indication
58 * monitor the dsc bit in the status register.
59 * Also, O_NDELAY on open will allow the device to be
60 * opened without a disk available. This can be used to
61 * open an unformatted disk, or get the device capacity.
62 * Ver 0.91 Dec 11 99 Added IOMEGA Clik! drive support by
63 * <paul@paulbristow.net>
64 * Ver 0.92 Oct 22 00 Paul Bristow became official maintainer for this
65 * driver. Included Powerbook internal zip kludge.
66 * Ver 0.93 Oct 24 00 Fixed bugs for Clik! drive
67 * no disk on insert and disk change now works
68 * Ver 0.94 Oct 27 00 Tidied up to remove strstr(Clik) everywhere
69 * Ver 0.95 Nov 7 00 Brought across to kernel 2.4
70 * Ver 0.96 Jan 7 01 Actually in line with release version of 2.4.0
71 * including set_bit patch from Rusty Russel
72 * Ver 0.97 Jul 22 01 Merge 0.91-0.96 onto 0.9.sv for ac series
73 * Ver 0.97.sv Aug 3 01 Backported from 2.4.7-ac3
74 */
75
76 #define IDEFLOPPY_VERSION "0.97.sv"
77
78 #include <linux/config.h>
79 #include <linux/module.h>
80 #include <linux/types.h>
81 #include <linux/string.h>
82 #include <linux/kernel.h>
83 #include <linux/delay.h>
84 #include <linux/timer.h>
85 #include <linux/mm.h>
86 #include <linux/interrupt.h>
87 #include <linux/major.h>
88 #include <linux/errno.h>
89 #include <linux/genhd.h>
90 #include <linux/slab.h>
91 #include <linux/cdrom.h>
92 #include <linux/ide.h>
93
94 #include <asm/byteorder.h>
95 #include <asm/irq.h>
96 #include <asm/uaccess.h>
97 #include <asm/io.h>
98 #include <asm/unaligned.h>
99 #include <asm/bitops.h>
100
101 /*
102 * The following are used to debug the driver.
103 */
104 #define IDEFLOPPY_DEBUG_LOG 0
105 #define IDEFLOPPY_DEBUG_INFO 0
106 #define IDEFLOPPY_DEBUG_BUGS 1
107
108 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
109 #define IDEFLOPPY_DEBUG( fmt, args... )
110
111
112 /*
113 * Some drives require a longer irq timeout.
114 */
115 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
116
117 /*
118 * After each failed packet command we issue a request sense command
119 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
120 */
121 #define IDEFLOPPY_MAX_PC_RETRIES 3
122
123 /*
124 * With each packet command, we allocate a buffer of
125 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
126 */
127 #define IDEFLOPPY_PC_BUFFER_SIZE 256
128
129 /*
130 * In various places in the driver, we need to allocate storage
131 * for packet commands and requests, which will remain valid while
132 * we leave the driver to wait for an interrupt or a timeout event.
133 */
134 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
135
136 /*
137 * Our view of a packet command.
138 */
139 typedef struct idefloppy_packet_command_s {
140 u8 c[12]; /* Actual packet bytes */
141 int retries; /* On each retry, we increment retries */
142 int error; /* Error code */
143 int request_transfer; /* Bytes to transfer */
144 int actually_transferred; /* Bytes actually transferred */
145 int buffer_size; /* Size of our data buffer */
146 char *b_data; /* Pointer which runs on the buffers */
147 int b_count; /* Missing/Available data on the current buffer */
148 struct request *rq; /* The corresponding request */
149 byte *buffer; /* Data buffer */
150 byte *current_position; /* Pointer into the above buffer */
151 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
152 byte pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
153 unsigned long flags; /* Status/Action bit flags: long for set_bit */
154 } idefloppy_pc_t;
155
156 /*
157 * Packet command flag bits.
158 */
159 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
160 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
161 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
162 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
163 #define PC_WRITING 5 /* Data direction */
164
165 #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
166
167 /*
168 * Removable Block Access Capabilities Page
169 */
170 typedef struct {
171 #if defined(__LITTLE_ENDIAN_BITFIELD)
172 unsigned page_code :6; /* Page code - Should be 0x1b */
173 unsigned reserved1_6 :1; /* Reserved */
174 unsigned ps :1; /* Should be 0 */
175 #elif defined(__BIG_ENDIAN_BITFIELD)
176 unsigned ps :1; /* Should be 0 */
177 unsigned reserved1_6 :1; /* Reserved */
178 unsigned page_code :6; /* Page code - Should be 0x1b */
179 #else
180 #error "Bitfield endianness not defined! Check your byteorder.h"
181 #endif
182 u8 page_length; /* Page Length - Should be 0xa */
183 #if defined(__LITTLE_ENDIAN_BITFIELD)
184 unsigned reserved2 :6;
185 unsigned srfp :1; /* Supports reporting progress of format */
186 unsigned sflp :1; /* System floppy type device */
187 unsigned tlun :3; /* Total logical units supported by the device */
188 unsigned reserved3 :3;
189 unsigned sml :1; /* Single / Multiple lun supported */
190 unsigned ncd :1; /* Non cd optical device */
191 #elif defined(__BIG_ENDIAN_BITFIELD)
192 unsigned sflp :1; /* System floppy type device */
193 unsigned srfp :1; /* Supports reporting progress of format */
194 unsigned reserved2 :6;
195 unsigned ncd :1; /* Non cd optical device */
196 unsigned sml :1; /* Single / Multiple lun supported */
197 unsigned reserved3 :3;
198 unsigned tlun :3; /* Total logical units supported by the device */
199 #else
200 #error "Bitfield endianness not defined! Check your byteorder.h"
201 #endif
202 u8 reserved[8];
203 } idefloppy_capabilities_page_t;
204
205 /*
206 * Flexible disk page.
207 */
208 typedef struct {
209 #if defined(__LITTLE_ENDIAN_BITFIELD)
210 unsigned page_code :6; /* Page code - Should be 0x5 */
211 unsigned reserved1_6 :1; /* Reserved */
212 unsigned ps :1; /* The device is capable of saving the page */
213 #elif defined(__BIG_ENDIAN_BITFIELD)
214 unsigned ps :1; /* The device is capable of saving the page */
215 unsigned reserved1_6 :1; /* Reserved */
216 unsigned page_code :6; /* Page code - Should be 0x5 */
217 #else
218 #error "Bitfield endianness not defined! Check your byteorder.h"
219 #endif
220 u8 page_length; /* Page Length - Should be 0x1e */
221 u16 transfer_rate; /* In kilobits per second */
222 u8 heads, sectors; /* Number of heads, Number of sectors per track */
223 u16 sector_size; /* Byes per sector */
224 u16 cyls; /* Number of cylinders */
225 u8 reserved10[10];
226 u8 motor_delay; /* Motor off delay */
227 u8 reserved21[7];
228 u16 rpm; /* Rotations per minute */
229 u8 reserved30[2];
230 } idefloppy_flexible_disk_page_t;
231
232 /*
233 * Format capacity
234 */
235 typedef struct {
236 u8 reserved[3];
237 u8 length; /* Length of the following descriptors in bytes */
238 } idefloppy_capacity_header_t;
239
240 typedef struct {
241 u32 blocks; /* Number of blocks */
242 #if defined(__LITTLE_ENDIAN_BITFIELD)
243 unsigned dc :2; /* Descriptor Code */
244 unsigned reserved :6;
245 #elif defined(__BIG_ENDIAN_BITFIELD)
246 unsigned reserved :6;
247 unsigned dc :2; /* Descriptor Code */
248 #else
249 #error "Bitfield endianness not defined! Check your byteorder.h"
250 #endif
251 u8 length_msb; /* Block Length (MSB)*/
252 u16 length; /* Block Length */
253 } idefloppy_capacity_descriptor_t;
254
255 #define CAPACITY_INVALID 0x00
256 #define CAPACITY_UNFORMATTED 0x01
257 #define CAPACITY_CURRENT 0x02
258 #define CAPACITY_NO_CARTRIDGE 0x03
259
260 /*
261 * Most of our global data which we need to save even as we leave the
262 * driver due to an interrupt or a timer event is stored in a variable
263 * of type idefloppy_floppy_t, defined below.
264 */
265 typedef struct {
266 ide_drive_t *drive;
267
268 idefloppy_pc_t *pc; /* Current packet command */
269 idefloppy_pc_t *failed_pc; /* Last failed packet command */
270 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];/* Packet command stack */
271 int pc_stack_index; /* Next free packet command storage space */
272 struct request rq_stack[IDEFLOPPY_PC_STACK];
273 int rq_stack_index; /* We implement a circular array */
274
275 /*
276 * Last error information
277 */
278 byte sense_key, asc, ascq;
279 int progress_indication;
280
281 /*
282 * Device information
283 */
284 int blocks, block_size, bs_factor; /* Current format */
285 idefloppy_capacity_descriptor_t capacity; /* Last format capacity */
286 idefloppy_flexible_disk_page_t flexible_disk_page; /* Copy of the flexible disk page */
287 int wp; /* Write protect */
288 int srfp; /* Supports format progress report */
289 unsigned long flags; /* Status/Action flags */
290 } idefloppy_floppy_t;
291
292 /*
293 * Floppy flag bits values.
294 */
295 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
296 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
297 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
298 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
299 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
300
301
302 /*
303 * ATAPI floppy drive packet commands
304 */
305 #define IDEFLOPPY_FORMAT_UNIT_CMD 0x04
306 #define IDEFLOPPY_INQUIRY_CMD 0x12
307 #define IDEFLOPPY_MODE_SELECT_CMD 0x55
308 #define IDEFLOPPY_MODE_SENSE_CMD 0x5a
309 #define IDEFLOPPY_READ10_CMD 0x28
310 #define IDEFLOPPY_READ12_CMD 0xa8
311 #define IDEFLOPPY_READ_CAPACITY_CMD 0x23
312 #define IDEFLOPPY_REQUEST_SENSE_CMD 0x03
313 #define IDEFLOPPY_PREVENT_REMOVAL_CMD 0x1e
314 #define IDEFLOPPY_SEEK_CMD 0x2b
315 #define IDEFLOPPY_START_STOP_CMD 0x1b
316 #define IDEFLOPPY_TEST_UNIT_READY_CMD 0x00
317 #define IDEFLOPPY_VERIFY_CMD 0x2f
318 #define IDEFLOPPY_WRITE10_CMD 0x2a
319 #define IDEFLOPPY_WRITE12_CMD 0xaa
320 #define IDEFLOPPY_WRITE_VERIFY_CMD 0x2e
321
322 /*
323 * Defines for the mode sense command
324 */
325 #define MODE_SENSE_CURRENT 0x00
326 #define MODE_SENSE_CHANGEABLE 0x01
327 #define MODE_SENSE_DEFAULT 0x02
328 #define MODE_SENSE_SAVED 0x03
329
330 /*
331 * IOCTLs used in low-level formatting.
332 */
333
334 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
335 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
336 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
337 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
338
339 /*
340 * Special requests for our block device strategy routine.
341 */
342 #define IDEFLOPPY_FIRST_RQ 90
343
344 /*
345 * IDEFLOPPY_PC_RQ is used to queue a packet command in the request queue.
346 */
347 #define IDEFLOPPY_PC_RQ 90
348
349 #define IDEFLOPPY_LAST_RQ 90
350
351 /*
352 * A macro which can be used to check if a given request command
353 * originated in the driver or in the buffer cache layer.
354 */
355 #define IDEFLOPPY_RQ_CMD(cmd) ((cmd >= IDEFLOPPY_FIRST_RQ) && (cmd <= IDEFLOPPY_LAST_RQ))
356
357 /*
358 * Error codes which are returned in rq->errors to the higher part
359 * of the driver.
360 */
361 #define IDEFLOPPY_ERROR_GENERAL 101
362
363 /*
364 * The ATAPI Status Register.
365 */
366 typedef union {
367 unsigned all :8;
368 struct {
369 #if defined(__LITTLE_ENDIAN_BITFIELD)
370 unsigned check :1; /* Error occurred */
371 unsigned idx :1; /* Reserved */
372 unsigned corr :1; /* Correctable error occurred */
373 unsigned drq :1; /* Data is request by the device */
374 unsigned dsc :1; /* Media access command finished */
375 unsigned reserved5 :1; /* Reserved */
376 unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
377 unsigned bsy :1; /* The device has access to the command block */
378 #elif defined(__BIG_ENDIAN_BITFIELD)
379 unsigned bsy :1; /* The device has access to the command block */
380 unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
381 unsigned reserved5 :1; /* Reserved */
382 unsigned dsc :1; /* Media access command finished */
383 unsigned drq :1; /* Data is request by the device */
384 unsigned corr :1; /* Correctable error occurred */
385 unsigned idx :1; /* Reserved */
386 unsigned check :1; /* Error occurred */
387 #else
388 #error "Bitfield endianness not defined! Check your byteorder.h"
389 #endif
390 } b;
391 } idefloppy_status_reg_t;
392
393 /*
394 * The ATAPI error register.
395 */
396 typedef union {
397 unsigned all :8;
398 struct {
399 #if defined(__LITTLE_ENDIAN_BITFIELD)
400 unsigned ili :1; /* Illegal Length Indication */
401 unsigned eom :1; /* End Of Media Detected */
402 unsigned abrt :1; /* Aborted command - As defined by ATA */
403 unsigned mcr :1; /* Media Change Requested - As defined by ATA */
404 unsigned sense_key :4; /* Sense key of the last failed packet command */
405 #elif defined(__BIG_ENDIAN_BITFIELD)
406 unsigned sense_key :4; /* Sense key of the last failed packet command */
407 unsigned mcr :1; /* Media Change Requested - As defined by ATA */
408 unsigned abrt :1; /* Aborted command - As defined by ATA */
409 unsigned eom :1; /* End Of Media Detected */
410 unsigned ili :1; /* Illegal Length Indication */
411 #else
412 #error "Bitfield endianness not defined! Check your byteorder.h"
413 #endif
414 } b;
415 } idefloppy_error_reg_t;
416
417 /*
418 * ATAPI Feature Register
419 */
420 typedef union {
421 unsigned all :8;
422 struct {
423 #if defined(__LITTLE_ENDIAN_BITFIELD)
424 unsigned dma :1; /* Using DMA or PIO */
425 unsigned reserved321 :3; /* Reserved */
426 unsigned reserved654 :3; /* Reserved (Tag Type) */
427 unsigned reserved7 :1; /* Reserved */
428 #elif defined(__BIG_ENDIAN_BITFIELD)
429 unsigned reserved7 :1; /* Reserved */
430 unsigned reserved654 :3; /* Reserved (Tag Type) */
431 unsigned reserved321 :3; /* Reserved */
432 unsigned dma :1; /* Using DMA or PIO */
433 #else
434 #error "Bitfield endianness not defined! Check your byteorder.h"
435 #endif
436 } b;
437 } idefloppy_feature_reg_t;
438
439 /*
440 * ATAPI Byte Count Register.
441 */
442 typedef union {
443 unsigned all :16;
444 struct {
445 #if defined(__LITTLE_ENDIAN_BITFIELD)
446 unsigned low :8; /* LSB */
447 unsigned high :8; /* MSB */
448 #elif defined(__BIG_ENDIAN_BITFIELD)
449 unsigned high :8; /* MSB */
450 unsigned low :8; /* LSB */
451 #else
452 #error "Bitfield endianness not defined! Check your byteorder.h"
453 #endif
454 } b;
455 } idefloppy_bcount_reg_t;
456
457 /*
458 * ATAPI Interrupt Reason Register.
459 */
460 typedef union {
461 unsigned all :8;
462 struct {
463 #if defined(__LITTLE_ENDIAN_BITFIELD)
464 unsigned cod :1; /* Information transferred is command (1) or data (0) */
465 unsigned io :1; /* The device requests us to read (1) or write (0) */
466 unsigned reserved :6; /* Reserved */
467 #elif defined(__BIG_ENDIAN_BITFIELD)
468 unsigned reserved :6; /* Reserved */
469 unsigned io :1; /* The device requests us to read (1) or write (0) */
470 unsigned cod :1; /* Information transferred is command (1) or data (0) */
471 #else
472 #error "Bitfield endianness not defined! Check your byteorder.h"
473 #endif
474 } b;
475 } idefloppy_ireason_reg_t;
476
477 /*
478 * ATAPI floppy Drive Select Register
479 */
480 typedef union {
481 unsigned all :8;
482 struct {
483 #if defined(__LITTLE_ENDIAN_BITFIELD)
484 unsigned sam_lun :3; /* Logical unit number */
485 unsigned reserved3 :1; /* Reserved */
486 unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
487 unsigned one5 :1; /* Should be set to 1 */
488 unsigned reserved6 :1; /* Reserved */
489 unsigned one7 :1; /* Should be set to 1 */
490 #elif defined(__BIG_ENDIAN_BITFIELD)
491 unsigned one7 :1; /* Should be set to 1 */
492 unsigned reserved6 :1; /* Reserved */
493 unsigned one5 :1; /* Should be set to 1 */
494 unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
495 unsigned reserved3 :1; /* Reserved */
496 unsigned sam_lun :3; /* Logical unit number */
497 #else
498 #error "Bitfield endianness not defined! Check your byteorder.h"
499 #endif
500 } b;
501 } idefloppy_drivesel_reg_t;
502
503 /*
504 * ATAPI Device Control Register
505 */
506 typedef union {
507 unsigned all :8;
508 struct {
509 #if defined(__LITTLE_ENDIAN_BITFIELD)
510 unsigned zero0 :1; /* Should be set to zero */
511 unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
512 unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
513 unsigned one3 :1; /* Should be set to 1 */
514 unsigned reserved4567 :4; /* Reserved */
515 #elif defined(__BIG_ENDIAN_BITFIELD)
516 unsigned reserved4567 :4; /* Reserved */
517 unsigned one3 :1; /* Should be set to 1 */
518 unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
519 unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
520 unsigned zero0 :1; /* Should be set to zero */
521 #else
522 #error "Bitfield endianness not defined! Check your byteorder.h"
523 #endif
524 } b;
525 } idefloppy_control_reg_t;
526
527 /*
528 * The following is used to format the general configuration word of
529 * the ATAPI IDENTIFY DEVICE command.
530 */
531 struct idefloppy_id_gcw {
532 #if defined(__LITTLE_ENDIAN_BITFIELD)
533 unsigned packet_size :2; /* Packet Size */
534 unsigned reserved234 :3; /* Reserved */
535 unsigned drq_type :2; /* Command packet DRQ type */
536 unsigned removable :1; /* Removable media */
537 unsigned device_type :5; /* Device type */
538 unsigned reserved13 :1; /* Reserved */
539 unsigned protocol :2; /* Protocol type */
540 #elif defined(__BIG_ENDIAN_BITFIELD)
541 unsigned protocol :2; /* Protocol type */
542 unsigned reserved13 :1; /* Reserved */
543 unsigned device_type :5; /* Device type */
544 unsigned removable :1; /* Removable media */
545 unsigned drq_type :2; /* Command packet DRQ type */
546 unsigned reserved234 :3; /* Reserved */
547 unsigned packet_size :2; /* Packet Size */
548 #else
549 #error "Bitfield endianness not defined! Check your byteorder.h"
550 #endif
551 };
552
553 /*
554 * INQUIRY packet command - Data Format
555 */
556 typedef struct {
557 #if defined(__LITTLE_ENDIAN_BITFIELD)
558 unsigned device_type :5; /* Peripheral Device Type */
559 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
560 unsigned reserved1_6t0 :7; /* Reserved */
561 unsigned rmb :1; /* Removable Medium Bit */
562 unsigned ansi_version :3; /* ANSI Version */
563 unsigned ecma_version :3; /* ECMA Version */
564 unsigned iso_version :2; /* ISO Version */
565 unsigned response_format :4; /* Response Data Format */
566 unsigned reserved3_45 :2; /* Reserved */
567 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
568 unsigned reserved3_7 :1; /* AENC - Reserved */
569 #elif defined(__BIG_ENDIAN_BITFIELD)
570 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
571 unsigned device_type :5; /* Peripheral Device Type */
572 unsigned rmb :1; /* Removable Medium Bit */
573 unsigned reserved1_6t0 :7; /* Reserved */
574 unsigned iso_version :2; /* ISO Version */
575 unsigned ecma_version :3; /* ECMA Version */
576 unsigned ansi_version :3; /* ANSI Version */
577 unsigned reserved3_7 :1; /* AENC - Reserved */
578 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
579 unsigned reserved3_45 :2; /* Reserved */
580 unsigned response_format :4; /* Response Data Format */
581 #else
582 #error "Bitfield endianness not defined! Check your byteorder.h"
583 #endif
584 u8 additional_length; /* Additional Length (total_length-4) */
585 u8 rsv5, rsv6, rsv7; /* Reserved */
586 u8 vendor_id[8]; /* Vendor Identification */
587 u8 product_id[16]; /* Product Identification */
588 u8 revision_level[4]; /* Revision Level */
589 u8 vendor_specific[20]; /* Vendor Specific - Optional */
590 u8 reserved56t95[40]; /* Reserved - Optional */
591 /* Additional information may be returned */
592 } idefloppy_inquiry_result_t;
593
594 /*
595 * REQUEST SENSE packet command result - Data Format.
596 */
597 typedef struct {
598 #if defined(__LITTLE_ENDIAN_BITFIELD)
599 unsigned error_code :7; /* Current error (0x70) */
600 unsigned valid :1; /* The information field conforms to SFF-8070i */
601 u8 reserved1 :8; /* Reserved */
602 unsigned sense_key :4; /* Sense Key */
603 unsigned reserved2_4 :1; /* Reserved */
604 unsigned ili :1; /* Incorrect Length Indicator */
605 unsigned reserved2_67 :2;
606 #elif defined(__BIG_ENDIAN_BITFIELD)
607 unsigned valid :1; /* The information field conforms to SFF-8070i */
608 unsigned error_code :7; /* Current error (0x70) */
609 u8 reserved1 :8; /* Reserved */
610 unsigned reserved2_67 :2;
611 unsigned ili :1; /* Incorrect Length Indicator */
612 unsigned reserved2_4 :1; /* Reserved */
613 unsigned sense_key :4; /* Sense Key */
614 #else
615 #error "Bitfield endianness not defined! Check your byteorder.h"
616 #endif
617 u32 information __attribute__ ((packed));
618 u8 asl; /* Additional sense length (n-7) */
619 u32 command_specific; /* Additional command specific information */
620 u8 asc; /* Additional Sense Code */
621 u8 ascq; /* Additional Sense Code Qualifier */
622 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
623 u8 sksv[3];
624 u8 pad[2]; /* Padding to 20 bytes */
625 } idefloppy_request_sense_result_t;
626
627 /*
628 * Pages of the SELECT SENSE / MODE SENSE packet commands.
629 */
630 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
631 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
632
633 /*
634 * Mode Parameter Header for the MODE SENSE packet command
635 */
636 typedef struct {
637 u16 mode_data_length; /* Length of the following data transfer */
638 u8 medium_type; /* Medium Type */
639 #if defined(__LITTLE_ENDIAN_BITFIELD)
640 unsigned reserved3 :7;
641 unsigned wp :1; /* Write protect */
642 #elif defined(__BIG_ENDIAN_BITFIELD)
643 unsigned wp :1; /* Write protect */
644 unsigned reserved3 :7;
645 #else
646 #error "Bitfield endianness not defined! Check your byteorder.h"
647 #endif
648 u8 reserved[4];
649 } idefloppy_mode_parameter_header_t;
650
651 #define IDEFLOPPY_MIN(a,b) ((a)<(b) ? (a):(b))
652 #define IDEFLOPPY_MAX(a,b) ((a)>(b) ? (a):(b))
653
654 /*
655 * Too bad. The drive wants to send us data which we are not ready to accept.
656 * Just throw it away.
657 */
658 static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
659 {
660 while (bcount--)
661 IN_BYTE (IDE_DATA_REG);
662 }
663
664 #if IDEFLOPPY_DEBUG_BUGS
665 static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
666 {
667 while (bcount--)
668 OUT_BYTE (0, IDE_DATA_REG);
669 }
670 #endif /* IDEFLOPPY_DEBUG_BUGS */
671
672 /*
673 * idefloppy_end_request is used to finish servicing a request.
674 *
675 * For read/write requests, we will call ide_end_request to pass to the
676 * next buffer.
677 */
678 static void idefloppy_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
679 {
680 ide_drive_t *drive = hwgroup->drive;
681 idefloppy_floppy_t *floppy = drive->driver_data;
682 struct request *rq = hwgroup->rq;
683 int error;
684
685 #if IDEFLOPPY_DEBUG_LOG
686 printk (KERN_INFO "Reached idefloppy_end_request\n");
687 #endif /* IDEFLOPPY_DEBUG_LOG */
688
689 switch (uptodate) {
690 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
691 case 1: error = 0; break;
692 default: error = uptodate;
693 }
694 if (error)
695 floppy->failed_pc = NULL;
696 /* Why does this happen? */
697 if (!rq)
698 return;
699 if (!IDEFLOPPY_RQ_CMD (rq->cmd)) {
700 ide_end_request (uptodate, hwgroup);
701 return;
702 }
703 rq->errors = error;
704 ide_end_drive_cmd (drive, 0, 0);
705 }
706
707 static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
708 {
709 struct request *rq = pc->rq;
710 struct buffer_head *bh = rq->bh;
711 int count;
712
713 while (bcount) {
714 if (pc->b_count == bh->b_size) {
715 rq->sector += rq->current_nr_sectors;
716 rq->nr_sectors -= rq->current_nr_sectors;
717 idefloppy_end_request (1, HWGROUP(drive));
718 if ((bh = rq->bh) != NULL)
719 pc->b_count = 0;
720 }
721 if (bh == NULL) {
722 printk (KERN_ERR "%s: bh == NULL in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
723 idefloppy_discard_data (drive, bcount);
724 return;
725 }
726 count = IDEFLOPPY_MIN (bh->b_size - pc->b_count, bcount);
727 atapi_input_bytes (drive, bh->b_data + pc->b_count, count);
728 bcount -= count; pc->b_count += count;
729 }
730 }
731
732 static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
733 {
734 struct request *rq = pc->rq;
735 struct buffer_head *bh = rq->bh;
736 int count;
737
738 while (bcount) {
739 if (!pc->b_count) {
740 rq->sector += rq->current_nr_sectors;
741 rq->nr_sectors -= rq->current_nr_sectors;
742 idefloppy_end_request (1, HWGROUP(drive));
743 if ((bh = rq->bh) != NULL) {
744 pc->b_data = bh->b_data;
745 pc->b_count = bh->b_size;
746 }
747 }
748 if (bh == NULL) {
749 printk (KERN_ERR "%s: bh == NULL in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
750 idefloppy_write_zeros (drive, bcount);
751 return;
752 }
753 count = IDEFLOPPY_MIN (pc->b_count, bcount);
754 atapi_output_bytes (drive, pc->b_data, count);
755 bcount -= count; pc->b_data += count; pc->b_count -= count;
756 }
757 }
758
759 #ifdef CONFIG_BLK_DEV_IDEDMA
760 static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
761 {
762 struct request *rq = pc->rq;
763 struct buffer_head *bh = rq->bh;
764
765 while ((bh = rq->bh) != NULL)
766 idefloppy_end_request (1, HWGROUP(drive));
767 }
768 #endif /* CONFIG_BLK_DEV_IDEDMA */
769
770 /*
771 * idefloppy_queue_pc_head generates a new packet command request in front
772 * of the request queue, before the current request, so that it will be
773 * processed immediately, on the next pass through the driver.
774 */
775 static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
776 {
777 ide_init_drive_cmd (rq);
778 rq->buffer = (char *) pc;
779 rq->cmd = IDEFLOPPY_PC_RQ;
780 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
781 }
782
783 static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
784 {
785 idefloppy_floppy_t *floppy = drive->driver_data;
786
787 if (floppy->pc_stack_index==IDEFLOPPY_PC_STACK)
788 floppy->pc_stack_index=0;
789 return (&floppy->pc_stack[floppy->pc_stack_index++]);
790 }
791
792 static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
793 {
794 idefloppy_floppy_t *floppy = drive->driver_data;
795
796 if (floppy->rq_stack_index==IDEFLOPPY_PC_STACK)
797 floppy->rq_stack_index=0;
798 return (&floppy->rq_stack[floppy->rq_stack_index++]);
799 }
800
801 /*
802 * idefloppy_analyze_error is called on each failed packet command retry
803 * to analyze the request sense.
804 */
805 static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
806 {
807 idefloppy_floppy_t *floppy = drive->driver_data;
808
809 floppy->sense_key = result->sense_key; floppy->asc = result->asc; floppy->ascq = result->ascq;
810 floppy->progress_indication= result->sksv[0] & 0x80 ?
811 (unsigned short)get_unaligned((u16 *)(result->sksv+1)):0x10000;
812 #if IDEFLOPPY_DEBUG_LOG
813 if (floppy->failed_pc)
814 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);
815 else
816 printk (KERN_INFO "ide-floppy: sense key = %x, asc = %x, ascq = %x\n",result->sense_key,result->asc,result->ascq);
817 #endif /* IDEFLOPPY_DEBUG_LOG */
818 }
819
820 static void idefloppy_request_sense_callback (ide_drive_t *drive)
821 {
822 idefloppy_floppy_t *floppy = drive->driver_data;
823
824 #if IDEFLOPPY_DEBUG_LOG
825 printk (KERN_INFO "ide-floppy: Reached idefloppy_request_sense_callback\n");
826 #endif /* IDEFLOPPY_DEBUG_LOG */
827 if (!floppy->pc->error) {
828 idefloppy_analyze_error (drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
829 idefloppy_end_request (1,HWGROUP (drive));
830 } else {
831 printk (KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
832 idefloppy_end_request (0,HWGROUP (drive));
833 }
834 }
835
836 /*
837 * General packet command callback function.
838 */
839 static void idefloppy_pc_callback (ide_drive_t *drive)
840 {
841 idefloppy_floppy_t *floppy = drive->driver_data;
842
843 #if IDEFLOPPY_DEBUG_LOG
844 printk (KERN_INFO "ide-floppy: Reached idefloppy_pc_callback\n");
845 #endif /* IDEFLOPPY_DEBUG_LOG */
846
847 idefloppy_end_request (floppy->pc->error ? 0:1, HWGROUP(drive));
848 }
849
850 /*
851 * idefloppy_init_pc initializes a packet command.
852 */
853 static void idefloppy_init_pc (idefloppy_pc_t *pc)
854 {
855 memset (pc->c, 0, 12);
856 pc->retries = 0;
857 pc->flags = 0;
858 pc->request_transfer = 0;
859 pc->buffer = pc->pc_buffer;
860 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
861 pc->b_data = NULL;
862 pc->callback = &idefloppy_pc_callback;
863 }
864
865 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
866 {
867 idefloppy_init_pc (pc);
868 pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
869 pc->c[4] = 255;
870 pc->request_transfer = 18;
871 pc->callback = &idefloppy_request_sense_callback;
872 }
873
874 /*
875 * idefloppy_retry_pc is called when an error was detected during the
876 * last packet command. We queue a request sense packet command in
877 * the head of the request list.
878 */
879 static void idefloppy_retry_pc (ide_drive_t *drive)
880 {
881 idefloppy_pc_t *pc;
882 struct request *rq;
883 idefloppy_error_reg_t error;
884
885 error.all = IN_BYTE (IDE_ERROR_REG);
886 pc = idefloppy_next_pc_storage (drive);
887 rq = idefloppy_next_rq_storage (drive);
888 idefloppy_create_request_sense_cmd (pc);
889 idefloppy_queue_pc_head (drive, pc, rq);
890 }
891
892 /*
893 * idefloppy_pc_intr is the usual interrupt handler which will be called
894 * during a packet command.
895 */
896 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
897 {
898 idefloppy_floppy_t *floppy = drive->driver_data;
899 idefloppy_status_reg_t status;
900 idefloppy_bcount_reg_t bcount;
901 idefloppy_ireason_reg_t ireason;
902 idefloppy_pc_t *pc=floppy->pc;
903 struct request *rq = pc->rq;
904 unsigned int temp;
905
906 #if IDEFLOPPY_DEBUG_LOG
907 printk (KERN_INFO "ide-floppy: Reached idefloppy_pc_intr interrupt handler\n");
908 #endif /* IDEFLOPPY_DEBUG_LOG */
909
910 #ifdef CONFIG_BLK_DEV_IDEDMA
911 if (test_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
912 if (HWIF(drive)->dmaproc(ide_dma_end, drive)) {
913 set_bit (PC_DMA_ERROR, &pc->flags);
914 } else {
915 pc->actually_transferred=pc->request_transfer;
916 idefloppy_update_buffers (drive, pc);
917 }
918 #if IDEFLOPPY_DEBUG_LOG
919 printk (KERN_INFO "ide-floppy: DMA finished\n");
920 #endif /* IDEFLOPPY_DEBUG_LOG */
921 }
922 #endif /* CONFIG_BLK_DEV_IDEDMA */
923
924 status.all = GET_STAT(); /* Clear the interrupt */
925
926 if (!status.b.drq) { /* No more interrupts */
927 #if IDEFLOPPY_DEBUG_LOG
928 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
929 #endif /* IDEFLOPPY_DEBUG_LOG */
930 clear_bit (PC_DMA_IN_PROGRESS, &pc->flags);
931
932 ide__sti(); /* local CPU only */
933
934 if (status.b.check || test_bit (PC_DMA_ERROR, &pc->flags)) { /* Error detected */
935 #if IDEFLOPPY_DEBUG_LOG
936 printk (KERN_INFO "ide-floppy: %s: I/O error\n",drive->name);
937 #endif /* IDEFLOPPY_DEBUG_LOG */
938 rq->errors++;
939 if (pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
940 printk (KERN_ERR "ide-floppy: I/O error in request sense command\n");
941 return ide_do_reset (drive);
942 }
943 idefloppy_retry_pc (drive); /* Retry operation */
944 return ide_stopped; /* queued, but not started */
945 }
946 pc->error = 0;
947 if (floppy->failed_pc == pc)
948 floppy->failed_pc=NULL;
949 pc->callback(drive); /* Command finished - Call the callback function */
950 return ide_stopped;
951 }
952 #ifdef CONFIG_BLK_DEV_IDEDMA
953 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
954 printk (KERN_ERR "ide-floppy: The floppy wants to issue more interrupts in DMA mode\n");
955 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
956 return ide_do_reset (drive);
957 }
958 #endif /* CONFIG_BLK_DEV_IDEDMA */
959 bcount.b.high=IN_BYTE (IDE_BCOUNTH_REG); /* Get the number of bytes to transfer */
960 bcount.b.low=IN_BYTE (IDE_BCOUNTL_REG); /* on this interrupt */
961 ireason.all=IN_BYTE (IDE_IREASON_REG);
962
963 if (ireason.b.cod) {
964 printk (KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
965 return ide_do_reset (drive);
966 }
967 if (ireason.b.io == test_bit (PC_WRITING, &pc->flags)) { /* Hopefully, we will never get here */
968 printk (KERN_ERR "ide-floppy: We wanted to %s, ", ireason.b.io ? "Write":"Read");
969 printk (KERN_ERR "but the floppy wants us to %s !\n",ireason.b.io ? "Read":"Write");
970 return ide_do_reset (drive);
971 }
972 if (!test_bit (PC_WRITING, &pc->flags)) { /* Reading - Check that we have enough space */
973 temp = pc->actually_transferred + bcount.all;
974 if ( temp > pc->request_transfer) {
975 if (temp > pc->buffer_size) {
976 printk (KERN_ERR "ide-floppy: The floppy wants to send us more data than expected - discarding data\n");
977 idefloppy_discard_data (drive,bcount.all);
978 ide_set_handler (drive,&idefloppy_pc_intr,IDEFLOPPY_WAIT_CMD, NULL);
979 return ide_started;
980 }
981 #if IDEFLOPPY_DEBUG_LOG
982 printk (KERN_NOTICE "ide-floppy: The floppy wants to send us more data than expected - allowing transfer\n");
983 #endif /* IDEFLOPPY_DEBUG_LOG */
984 }
985 }
986 if (test_bit (PC_WRITING, &pc->flags)) {
987 if (pc->buffer != NULL)
988 atapi_output_bytes (drive,pc->current_position,bcount.all); /* Write the current buffer */
989 else
990 idefloppy_output_buffers (drive, pc, bcount.all);
991 } else {
992 if (pc->buffer != NULL)
993 atapi_input_bytes (drive,pc->current_position,bcount.all); /* Read the current buffer */
994 else
995 idefloppy_input_buffers (drive, pc, bcount.all);
996 }
997 pc->actually_transferred+=bcount.all; /* Update the current position */
998 pc->current_position+=bcount.all;
999
1000 ide_set_handler (drive,&idefloppy_pc_intr,IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
1001 return ide_started;
1002 }
1003
1004 static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
1005 {
1006 ide_startstop_t startstop;
1007 idefloppy_floppy_t *floppy = drive->driver_data;
1008 idefloppy_ireason_reg_t ireason;
1009
1010 if (ide_wait_stat (&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
1011 printk (KERN_ERR "ide-floppy: Strange, packet command initiated yet DRQ isn't asserted\n");
1012 return startstop;
1013 }
1014 ireason.all=IN_BYTE (IDE_IREASON_REG);
1015 if (!ireason.b.cod || ireason.b.io) {
1016 printk (KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while issuing a packet command\n");
1017 return ide_do_reset (drive);
1018 }
1019 ide_set_handler (drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* Set the interrupt routine */
1020 atapi_output_bytes (drive, floppy->pc->c, 12); /* Send the actual packet */
1021 return ide_started;
1022 }
1023
1024 /*
1025 * Issue a packet command
1026 */
1027 static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
1028 {
1029 idefloppy_floppy_t *floppy = drive->driver_data;
1030 idefloppy_bcount_reg_t bcount;
1031 int dma_ok = 0;
1032
1033 #if IDEFLOPPY_DEBUG_BUGS
1034 if (floppy->pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD && pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
1035 printk (KERN_ERR "ide-floppy: possible ide-floppy.c bug - Two request sense in serial were issued\n");
1036 }
1037 #endif /* IDEFLOPPY_DEBUG_BUGS */
1038
1039 if (floppy->failed_pc == NULL && pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
1040 floppy->failed_pc=pc;
1041 floppy->pc=pc; /* Set the current packet command */
1042
1043 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES || test_bit (PC_ABORT, &pc->flags)) {
1044 /*
1045 * We will "abort" retrying a packet command in case
1046 * a legitimate error code was received.
1047 */
1048 if (!test_bit (PC_ABORT, &pc->flags)) {
1049 if (!test_bit (PC_SUPPRESS_ERROR, &pc->flags)) {
1050 ;
1051 printk( KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
1052 drive->name, pc->c[0], floppy->sense_key, floppy->asc, floppy->ascq);
1053 }
1054 pc->error = IDEFLOPPY_ERROR_GENERAL; /* Giving up */
1055 }
1056 floppy->failed_pc=NULL;
1057 pc->callback(drive);
1058 return ide_stopped;
1059 }
1060 #if IDEFLOPPY_DEBUG_LOG
1061 printk (KERN_INFO "Retry number - %d\n",pc->retries);
1062 #endif /* IDEFLOPPY_DEBUG_LOG */
1063
1064 pc->retries++;
1065 pc->actually_transferred=0; /* We haven't transferred any data yet */
1066 pc->current_position=pc->buffer;
1067 bcount.all = IDE_MIN(pc->request_transfer, 63 * 1024);
1068
1069 #ifdef CONFIG_BLK_DEV_IDEDMA
1070 if (test_and_clear_bit (PC_DMA_ERROR, &pc->flags)) {
1071 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
1072 }
1073 if (test_bit (PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1074 dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
1075 #endif /* CONFIG_BLK_DEV_IDEDMA */
1076
1077 if (IDE_CONTROL_REG)
1078 OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
1079 OUT_BYTE (dma_ok ? 1:0,IDE_FEATURE_REG); /* Use PIO/DMA */
1080 OUT_BYTE (bcount.b.high,IDE_BCOUNTH_REG);
1081 OUT_BYTE (bcount.b.low,IDE_BCOUNTL_REG);
1082 OUT_BYTE (drive->select.all,IDE_SELECT_REG);
1083
1084 #ifdef CONFIG_BLK_DEV_IDEDMA
1085 if (dma_ok) { /* Begin DMA, if necessary */
1086 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
1087 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
1088 }
1089 #endif /* CONFIG_BLK_DEV_IDEDMA */
1090
1091 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1092 ide_set_handler (drive, &idefloppy_transfer_pc, IDEFLOPPY_WAIT_CMD, NULL);
1093 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* Issue the packet command */
1094 return ide_started;
1095 } else {
1096 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);
1097 return idefloppy_transfer_pc (drive);
1098 }
1099 }
1100
1101 static void idefloppy_rw_callback (ide_drive_t *drive)
1102 {
1103 #if IDEFLOPPY_DEBUG_LOG
1104 printk (KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
1105 #endif /* IDEFLOPPY_DEBUG_LOG */
1106
1107 idefloppy_end_request(1, HWGROUP(drive));
1108 return;
1109 }
1110
1111 static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1112 {
1113 #if IDEFLOPPY_DEBUG_LOG
1114 printk (KERN_INFO "ide-floppy: creating prevent removal command, prevent = %d\n", prevent);
1115 #endif /* IDEFLOPPY_DEBUG_LOG */
1116
1117 idefloppy_init_pc (pc);
1118 pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
1119 pc->c[4] = prevent;
1120 }
1121
1122 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1123 {
1124 idefloppy_init_pc (pc);
1125 pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
1126 pc->c[7] = 255;
1127 pc->c[8] = 255;
1128 pc->request_transfer = 255;
1129 }
1130
1131 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1132 int flags)
1133 {
1134 idefloppy_init_pc (pc);
1135 pc->c[0] = IDEFLOPPY_FORMAT_UNIT_CMD;
1136 pc->c[1] = 0x17;
1137
1138 memset(pc->buffer, 0, 12);
1139 pc->buffer[1] = 0xA2;
1140 /* Default format list header, byte 1: FOV/DCRT/IMM bits set */
1141
1142 if (flags & 1) /* Verify bit on... */
1143 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
1144 pc->buffer[3] = 8;
1145
1146 put_unaligned(htonl(b), (unsigned int *)(&pc->buffer[4]));
1147 put_unaligned(htonl(l), (unsigned int *)(&pc->buffer[8]));
1148 pc->buffer_size=12;
1149 set_bit(PC_WRITING, &pc->flags);
1150 }
1151
1152 /*
1153 * A mode sense command is used to "sense" floppy parameters.
1154 */
1155 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, byte page_code, byte type)
1156 {
1157 unsigned short length = sizeof (idefloppy_mode_parameter_header_t);
1158
1159 idefloppy_init_pc (pc);
1160 pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
1161 pc->c[1] = 0;
1162 pc->c[2] = page_code + (type << 6);
1163
1164 switch (page_code) {
1165 case IDEFLOPPY_CAPABILITIES_PAGE:
1166 length += 12;
1167 break;
1168 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1169 length += 32;
1170 break;
1171 default:
1172 printk (KERN_ERR "ide-floppy: unsupported page code in create_mode_sense_cmd\n");
1173 }
1174 put_unaligned (htons (length), (unsigned short *) &pc->c[7]);
1175 pc->request_transfer = length;
1176 }
1177
1178 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1179 {
1180 idefloppy_init_pc (pc);
1181 pc->c[0] = IDEFLOPPY_START_STOP_CMD;
1182 pc->c[4] = start;
1183 }
1184
1185 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1186 {
1187 idefloppy_init_pc(pc);
1188 pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
1189 }
1190
1191 static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1192 {
1193 int block = sector / floppy->bs_factor;
1194 int blocks = rq->nr_sectors / floppy->bs_factor;
1195
1196 #if IDEFLOPPY_DEBUG_LOG
1197 printk ("create_rw1%d_cmd: block == %d, blocks == %d\n",
1198 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags), block, blocks);
1199 #endif /* IDEFLOPPY_DEBUG_LOG */
1200
1201 idefloppy_init_pc (pc);
1202 if (test_bit (IDEFLOPPY_USE_READ12, &floppy->flags)) {
1203 pc->c[0] = rq->cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
1204 put_unaligned (htonl (blocks), (unsigned int *) &pc->c[6]);
1205 } else {
1206 pc->c[0] = rq->cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
1207 put_unaligned (htons (blocks), (unsigned short *) &pc->c[7]);
1208 }
1209 put_unaligned (htonl (block), (unsigned int *) &pc->c[2]);
1210 pc->callback = &idefloppy_rw_callback;
1211 pc->rq = rq;
1212 pc->b_data = rq->buffer;
1213 pc->b_count = rq->cmd == READ ? 0 : rq->bh->b_size;
1214 if (rq->cmd == WRITE)
1215 set_bit (PC_WRITING, &pc->flags);
1216 pc->buffer = NULL;
1217 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1218 set_bit (PC_DMA_RECOMMENDED, &pc->flags);
1219 }
1220
1221 /*
1222 * idefloppy_do_request is our request handling function.
1223 */
1224 static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
1225 {
1226 idefloppy_floppy_t *floppy = drive->driver_data;
1227 idefloppy_pc_t *pc;
1228
1229 #if IDEFLOPPY_DEBUG_LOG
1230 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);
1231 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
1232 #endif /* IDEFLOPPY_DEBUG_LOG */
1233
1234 if (rq->errors >= ERROR_MAX) {
1235 if (floppy->failed_pc != NULL)
1236 printk (KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
1237 drive->name, floppy->failed_pc->c[0], floppy->sense_key, floppy->asc, floppy->ascq);
1238 else
1239 printk (KERN_ERR "ide-floppy: %s: I/O error\n", drive->name);
1240 idefloppy_end_request (0, HWGROUP(drive));
1241 return ide_stopped;
1242 }
1243 switch (rq->cmd) {
1244 case READ:
1245 case WRITE:
1246 if (rq->sector % floppy->bs_factor || rq->nr_sectors % floppy->bs_factor) {
1247 printk ("%s: unsupported r/w request size\n", drive->name);
1248 idefloppy_end_request (0, HWGROUP(drive));
1249 return ide_stopped;
1250 }
1251 pc = idefloppy_next_pc_storage (drive);
1252 idefloppy_create_rw_cmd (floppy, pc, rq, block);
1253 break;
1254 case IDEFLOPPY_PC_RQ:
1255 pc = (idefloppy_pc_t *) rq->buffer;
1256 break;
1257 default:
1258 printk (KERN_ERR "ide-floppy: unsupported command %x in request queue\n", rq->cmd);
1259 idefloppy_end_request (0,HWGROUP (drive));
1260 return ide_stopped;
1261 }
1262 pc->rq = rq;
1263 return idefloppy_issue_pc (drive, pc);
1264 }
1265
1266 /*
1267 * idefloppy_queue_pc_tail adds a special packet command request to the
1268 * tail of the request queue, and waits for it to be serviced.
1269 */
1270 static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1271 {
1272 struct request rq;
1273
1274 ide_init_drive_cmd (&rq);
1275 rq.buffer = (char *) pc;
1276 rq.cmd = IDEFLOPPY_PC_RQ;
1277 return ide_do_drive_cmd (drive, &rq, ide_wait);
1278 }
1279
1280 /*
1281 * Look at the flexible disk page parameters. We will ignore the CHS
1282 * capacity parameters and use the LBA parameters instead.
1283 */
1284 static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1285 {
1286 idefloppy_floppy_t *floppy = drive->driver_data;
1287 idefloppy_pc_t pc;
1288 idefloppy_mode_parameter_header_t *header;
1289 idefloppy_flexible_disk_page_t *page;
1290 int capacity, lba_capacity;
1291
1292 idefloppy_create_mode_sense_cmd (&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1293 if (idefloppy_queue_pc_tail (drive,&pc)) {
1294 printk (KERN_ERR "ide-floppy: Can't get flexible disk page parameters\n");
1295 return 1;
1296 }
1297 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1298 floppy->wp = header->wp;
1299 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1300
1301 page->transfer_rate = ntohs (page->transfer_rate);
1302 page->sector_size = ntohs (page->sector_size);
1303 page->cyls = ntohs (page->cyls);
1304 page->rpm = ntohs (page->rpm);
1305 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1306 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1307 printk (KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, %d sector size, %d rpm\n",
1308 drive->name, capacity / 1024, page->cyls, page->heads, page->sectors,
1309 page->transfer_rate / 8, page->sector_size, page->rpm);
1310
1311 floppy->flexible_disk_page = *page;
1312 drive->bios_cyl = page->cyls;
1313 drive->bios_head = page->heads;
1314 drive->bios_sect = page->sectors;
1315 lba_capacity = floppy->blocks * floppy->block_size;
1316 if (capacity < lba_capacity) {
1317 printk (KERN_NOTICE "%s: The disk reports a capacity of %d bytes, "
1318 "but the drive only handles %d\n",
1319 drive->name, lba_capacity, capacity);
1320 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1321 }
1322 return 0;
1323 }
1324
1325 static int idefloppy_get_capability_page(ide_drive_t *drive)
1326 {
1327 idefloppy_floppy_t *floppy = drive->driver_data;
1328 idefloppy_pc_t pc;
1329 idefloppy_mode_parameter_header_t *header;
1330 idefloppy_capabilities_page_t *page;
1331
1332 floppy->srfp=0;
1333 idefloppy_create_mode_sense_cmd (&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1334 MODE_SENSE_CURRENT);
1335
1336 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1337 if (idefloppy_queue_pc_tail (drive,&pc)) {
1338 return 1;
1339 }
1340
1341 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1342 page= (idefloppy_capabilities_page_t *)(header+1);
1343 floppy->srfp=page->srfp;
1344 return (0);
1345 }
1346
1347 /*
1348 * Determine if a media is present in the floppy drive, and if so,
1349 * its LBA capacity.
1350 */
1351 static int idefloppy_get_capacity (ide_drive_t *drive)
1352 {
1353 idefloppy_floppy_t *floppy = drive->driver_data;
1354 idefloppy_pc_t pc;
1355 idefloppy_capacity_header_t *header;
1356 idefloppy_capacity_descriptor_t *descriptor;
1357 int i, descriptors, rc = 1, blocks, length;
1358
1359 drive->bios_cyl = 0;
1360 drive->bios_head = drive->bios_sect = 0;
1361 floppy->blocks = floppy->bs_factor = 0;
1362 drive->part[0].nr_sects = 0;
1363
1364 idefloppy_create_read_capacity_cmd (&pc);
1365 if (idefloppy_queue_pc_tail (drive, &pc)) {
1366 printk (KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1367 return 1;
1368 }
1369 header = (idefloppy_capacity_header_t *) pc.buffer;
1370 descriptors = header->length / sizeof (idefloppy_capacity_descriptor_t);
1371 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1372
1373 for (i = 0; i < descriptors; i++, descriptor++) {
1374 blocks = descriptor->blocks = ntohl (descriptor->blocks);
1375 length = descriptor->length = ntohs (descriptor->length);
1376
1377 if (!i)
1378 {
1379 switch (descriptor->dc) {
1380 case CAPACITY_UNFORMATTED: /* Clik! drive returns this instead of CAPACITY_CURRENT */
1381 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1382 break; /* If it is not a clik drive, break out (maintains previous driver behaviour) */
1383 case CAPACITY_CURRENT: /* Normal Zip/LS-120 disks */
1384 if (memcmp (descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1385 printk (KERN_INFO "%s: %dkB, %d blocks, %d sector size\n", drive->name, blocks * length / 1024, blocks, length);
1386 floppy->capacity = *descriptor;
1387 if (!length || length % 512)
1388 printk (KERN_NOTICE "%s: %d bytes block size not supported\n", drive->name, length);
1389 else {
1390 floppy->blocks = blocks;
1391 floppy->block_size = length;
1392 if ((floppy->bs_factor = length / 512) != 1)
1393 printk (KERN_NOTICE "%s: warning: non 512 bytes block size not fully supported\n", drive->name);
1394 rc = 0;
1395 }
1396 break;
1397 case CAPACITY_NO_CARTRIDGE:
1398 /* This is a KERN_ERR so it appears on screen for the user to see */
1399 printk (KERN_ERR "%s: No disk in drive\n", drive->name);
1400 break;
1401 case CAPACITY_INVALID:
1402 printk (KERN_ERR "%s: Invalid capacity for disk in drive\n", drive->name);
1403 break;
1404 }
1405 }
1406 if (!i) {
1407 IDEFLOPPY_DEBUG( "Descriptor 0 Code: %d\n", descriptor->dc);
1408 }
1409 IDEFLOPPY_DEBUG( "Descriptor %d: %dkB, %d blocks, %d sector size\n", i, blocks * length / 1024, blocks, length);
1410 }
1411
1412 /* Clik! disk does not support get_flexible_disk_page */
1413 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1414 {
1415 (void) idefloppy_get_flexible_disk_page (drive);
1416 }
1417
1418 drive->part[0].nr_sects = floppy->blocks * floppy->bs_factor;
1419 return rc;
1420 }
1421
1422 /*
1423 ** Obtain the list of formattable capacities.
1424 ** Very similar to idefloppy_get_capacity, except that we push the capacity
1425 ** descriptors to userland, instead of our own structures.
1426 **
1427 ** Userland gives us the following structure:
1428 **
1429 ** struct idefloppy_format_capacities {
1430 ** int nformats;
1431 ** struct {
1432 ** int nblocks;
1433 ** int blocksize;
1434 ** } formats[];
1435 ** } ;
1436 **
1437 ** userland initializes nformats to the number of allocated formats[]
1438 ** records. On exit we set nformats to the number of records we've
1439 ** actually initialized.
1440 **
1441 */
1442
1443 static int idefloppy_get_format_capacities (ide_drive_t *drive,
1444 struct inode *inode,
1445 struct file *file,
1446 int *arg) /* Cheater */
1447 {
1448 idefloppy_pc_t pc;
1449 idefloppy_capacity_header_t *header;
1450 idefloppy_capacity_descriptor_t *descriptor;
1451 int i, descriptors, blocks, length;
1452 int u_array_size;
1453 int u_index;
1454 int *argp;
1455
1456 if (get_user(u_array_size, arg))
1457 return (-EFAULT);
1458
1459 if (u_array_size <= 0)
1460 return (-EINVAL);
1461
1462 idefloppy_create_read_capacity_cmd (&pc);
1463 if (idefloppy_queue_pc_tail (drive, &pc)) {
1464 printk (KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1465 return (-EIO);
1466 }
1467 header = (idefloppy_capacity_header_t *) pc.buffer;
1468 descriptors = header->length /
1469 sizeof (idefloppy_capacity_descriptor_t);
1470 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1471
1472 u_index=0;
1473 argp=arg+1;
1474
1475 /*
1476 ** We always skip the first capacity descriptor. That's the
1477 ** current capacity. We are interested in the remaining descriptors,
1478 ** the formattable capacities.
1479 */
1480
1481 for (i=0; i<descriptors; i++, descriptor++)
1482 {
1483 if (u_index >= u_array_size)
1484 break; /* User-supplied buffer too small */
1485 if (i == 0)
1486 continue; /* Skip the first descriptor */
1487
1488 blocks = ntohl (descriptor->blocks);
1489 length = ntohs (descriptor->length);
1490
1491 if (put_user(blocks, argp))
1492 return (-EFAULT);
1493 ++argp;
1494
1495 if (put_user(length, argp))
1496 return (-EFAULT);
1497 ++argp;
1498
1499 ++u_index;
1500 }
1501
1502 if (put_user(u_index, arg))
1503 return (-EFAULT);
1504 return (0);
1505 }
1506
1507 /*
1508 ** Send ATAPI_FORMAT_UNIT to the drive.
1509 **
1510 ** Userland gives us the following structure:
1511 **
1512 ** struct idefloppy_format_command {
1513 ** int nblocks;
1514 ** int blocksize;
1515 ** int flags;
1516 ** } ;
1517 **
1518 ** flags is a bitmask, currently, the only defined flag is:
1519 **
1520 ** 0x01 - verify media after format.
1521 */
1522
1523 static int idefloppy_begin_format(ide_drive_t *drive,
1524 struct inode *inode,
1525 struct file *file,
1526 int *arg)
1527 {
1528 int blocks;
1529 int length;
1530 int flags;
1531 idefloppy_pc_t pc;
1532
1533 if (get_user(blocks, arg)
1534 || get_user(length, arg+1)
1535 || get_user(flags, arg+2))
1536 {
1537 return (-EFAULT);
1538 }
1539
1540 (void) idefloppy_get_capability_page (drive); /* Get the SFRP bit */
1541 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1542 if (idefloppy_queue_pc_tail (drive, &pc))
1543 {
1544 return (-EIO);
1545 }
1546 return (0);
1547 }
1548
1549 /*
1550 ** Get ATAPI_FORMAT_UNIT progress indication.
1551 **
1552 ** Userland gives a pointer to an int. The int is set to a progresss
1553 ** indicator 0-65536, with 65536=100%.
1554 **
1555 ** If the drive does not support format progress indication, we just check
1556 ** the dsc bit, and return either 0 or 65536.
1557 */
1558
1559 static int idefloppy_get_format_progress(ide_drive_t *drive,
1560 struct inode *inode,
1561 struct file *file,
1562 int *arg)
1563 {
1564 idefloppy_floppy_t *floppy = drive->driver_data;
1565 idefloppy_pc_t pc;
1566 int progress_indication=0x10000;
1567
1568 if (floppy->srfp)
1569 {
1570 idefloppy_create_request_sense_cmd(&pc);
1571 if (idefloppy_queue_pc_tail (drive, &pc))
1572 {
1573 return (-EIO);
1574 }
1575
1576 if (floppy->sense_key == 2 && floppy->asc == 4 &&
1577 floppy->ascq == 4)
1578 {
1579 progress_indication=floppy->progress_indication;
1580 }
1581 /* Else assume format_unit has finished, and we're
1582 ** at 0x10000 */
1583 }
1584 else
1585 {
1586 idefloppy_status_reg_t status;
1587 unsigned long flags;
1588
1589 __save_flags(flags);
1590 __cli();
1591 status.all=GET_STAT();
1592 __restore_flags(flags);
1593
1594 progress_indication= !status.b.dsc ? 0:0x10000;
1595 }
1596 if (put_user(progress_indication, arg))
1597 return (-EFAULT);
1598
1599 return (0);
1600 }
1601
1602 /*
1603 * Our special ide-floppy ioctl's.
1604 *
1605 * Currently there aren't any ioctl's.
1606 */
1607 static int idefloppy_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
1608 unsigned int cmd, unsigned long arg)
1609 {
1610 idefloppy_pc_t pc;
1611 idefloppy_floppy_t *floppy = drive->driver_data;
1612 int prevent = (arg) ? 1 : 0;
1613
1614 switch (cmd) {
1615 case CDROMEJECT:
1616 prevent = 0;
1617 /* fall through */
1618 case CDROM_LOCKDOOR:
1619 if (drive->usage > 1)
1620 return -EBUSY;
1621
1622 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1623 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1624 idefloppy_create_prevent_cmd (&pc, prevent);
1625 (void) idefloppy_queue_pc_tail (drive, &pc);
1626 }
1627 if (cmd == CDROMEJECT) {
1628 idefloppy_create_start_stop_cmd (&pc, 2);
1629 (void) idefloppy_queue_pc_tail (drive, &pc);
1630 }
1631 return 0;
1632 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1633 return (0);
1634 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1635 return (idefloppy_get_format_capacities(drive, inode, file,
1636 (int *)arg));
1637 case IDEFLOPPY_IOCTL_FORMAT_START:
1638
1639 if (!(file->f_mode & 2))
1640 return (-EPERM);
1641
1642 {
1643 idefloppy_floppy_t *floppy = drive->driver_data;
1644
1645 if (drive->usage > 1)
1646 {
1647 /* Don't format if someone is using the disk */
1648
1649 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1650 &floppy->flags);
1651 return -EBUSY;
1652 }
1653 else
1654 {
1655 int rc;
1656
1657 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1658 &floppy->flags);
1659
1660 rc=idefloppy_begin_format(drive, inode,
1661 file,
1662 (int *)arg);
1663
1664 if (rc)
1665 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1666 &floppy->flags);
1667 return (rc);
1668
1669 /*
1670 ** Note, the bit will be cleared when the device is
1671 ** closed. This is the cleanest way to handle the
1672 ** situation where the drive does not support
1673 ** format progress reporting.
1674 */
1675 }
1676 }
1677 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1678 return (idefloppy_get_format_progress(drive, inode, file,
1679 (int *)arg));
1680 }
1681 return -EIO;
1682 }
1683
1684 /*
1685 * Our open/release functions
1686 */
1687 static int idefloppy_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
1688 {
1689 idefloppy_floppy_t *floppy = drive->driver_data;
1690 idefloppy_pc_t pc;
1691
1692 #if IDEFLOPPY_DEBUG_LOG
1693 printk (KERN_INFO "Reached idefloppy_open\n");
1694 #endif /* IDEFLOPPY_DEBUG_LOG */
1695
1696 MOD_INC_USE_COUNT;
1697 if (drive->usage == 1) {
1698 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1699 /* Just in case */
1700
1701 idefloppy_create_test_unit_ready_cmd(&pc);
1702 if (idefloppy_queue_pc_tail(drive, &pc)) {
1703 idefloppy_create_start_stop_cmd (&pc, 1);
1704 (void) idefloppy_queue_pc_tail (drive, &pc);
1705 }
1706
1707 if (idefloppy_get_capacity (drive)
1708 && (filp->f_flags & O_NDELAY) == 0
1709 /*
1710 ** Allow O_NDELAY to open a drive without a disk, or with
1711 ** an unreadable disk, so that we can get the format
1712 ** capacity of the drive or begin the format - Sam
1713 */
1714 ) {
1715 drive->usage--;
1716 MOD_DEC_USE_COUNT;
1717 return -EIO;
1718 }
1719
1720 if (floppy->wp && (filp->f_mode & 2)) {
1721 drive->usage--;
1722 MOD_DEC_USE_COUNT;
1723 return -EROFS;
1724 }
1725 set_bit (IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1726 /* IOMEGA Clik! drives do not support lock/unlock commands */
1727 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1728 idefloppy_create_prevent_cmd (&pc, 1);
1729 (void) idefloppy_queue_pc_tail (drive, &pc);
1730 }
1731 check_disk_change(inode->i_rdev);
1732 }
1733 else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags))
1734 {
1735 drive->usage--;
1736 MOD_DEC_USE_COUNT;
1737 return -EBUSY;
1738 }
1739 return 0;
1740 }
1741
1742 static void idefloppy_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
1743 {
1744 idefloppy_pc_t pc;
1745
1746 #if IDEFLOPPY_DEBUG_LOG
1747 printk (KERN_INFO "Reached idefloppy_release\n");
1748 #endif /* IDEFLOPPY_DEBUG_LOG */
1749
1750 if (!drive->usage) {
1751 idefloppy_floppy_t *floppy = drive->driver_data;
1752
1753 invalidate_buffers (inode->i_rdev);
1754
1755 /* IOMEGA Clik! drives do not support lock/unlock commands */
1756 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1757 idefloppy_create_prevent_cmd (&pc, 0);
1758 (void) idefloppy_queue_pc_tail (drive, &pc);
1759 }
1760
1761 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1762 }
1763 MOD_DEC_USE_COUNT;
1764 }
1765
1766 /*
1767 * Check media change. Use a simple algorithm for now.
1768 */
1769 static int idefloppy_media_change (ide_drive_t *drive)
1770 {
1771 idefloppy_floppy_t *floppy = drive->driver_data;
1772
1773 return test_and_clear_bit (IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1774 }
1775
1776 /*
1777 * Revalidate the new media. Should set blk_size[]
1778 */
1779 static void idefloppy_revalidate (ide_drive_t *drive)
1780 {
1781 grok_partitions(HWIF(drive)->gd, drive->select.b.unit,
1782 1<<PARTN_BITS,
1783 current_capacity(drive));
1784 }
1785
1786 /*
1787 * Return the current floppy capacity to ide.c.
1788 */
1789 static unsigned long idefloppy_capacity (ide_drive_t *drive)
1790 {
1791 idefloppy_floppy_t *floppy = drive->driver_data;
1792 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1793
1794 return capacity;
1795 }
1796
1797 /*
1798 * idefloppy_identify_device checks if we can support a drive,
1799 * based on the ATAPI IDENTIFY command results.
1800 */
1801 static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1802 {
1803 struct idefloppy_id_gcw gcw;
1804 #if IDEFLOPPY_DEBUG_INFO
1805 unsigned short mask,i;
1806 char buffer[80];
1807 #endif /* IDEFLOPPY_DEBUG_INFO */
1808
1809 *((unsigned short *) &gcw) = id->config;
1810
1811 #ifdef CONFIG_PPC
1812 /* kludge for Apple PowerBook internal zip */
1813 if ((gcw.device_type == 5) && !strstr(id->model, "CD-ROM")
1814 && strstr(id->model, "ZIP"))
1815 gcw.device_type = 0;
1816 #endif
1817
1818 #if IDEFLOPPY_DEBUG_INFO
1819 printk (KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1820 switch (gcw.protocol) {
1821 case 0: case 1: sprintf (buffer, "ATA");break;
1822 case 2: sprintf (buffer, "ATAPI");break;
1823 case 3: sprintf (buffer, "Reserved (Unknown to ide-floppy)");break;
1824 }
1825 printk (KERN_INFO "Protocol Type: %s\n", buffer);
1826 switch (gcw.device_type) {
1827 case 0: sprintf (buffer, "Direct-access Device");break;
1828 case 1: sprintf (buffer, "Streaming Tape Device");break;
1829 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1830 case 5: sprintf (buffer, "CD-ROM Device");break;
1831 case 6: sprintf (buffer, "Reserved");
1832 case 7: sprintf (buffer, "Optical memory Device");break;
1833 case 0x1f: sprintf (buffer, "Unknown or no Device type");break;
1834 default: sprintf (buffer, "Reserved");
1835 }
1836 printk (KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1837 printk (KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1838 switch (gcw.drq_type) {
1839 case 0: sprintf (buffer, "Microprocessor DRQ");break;
1840 case 1: sprintf (buffer, "Interrupt DRQ");break;
1841 case 2: sprintf (buffer, "Accelerated DRQ");break;
1842 case 3: sprintf (buffer, "Reserved");break;
1843 }
1844 printk (KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1845 switch (gcw.packet_size) {
1846 case 0: sprintf (buffer, "12 bytes");break;
1847 case 1: sprintf (buffer, "16 bytes");break;
1848 default: sprintf (buffer, "Reserved");break;
1849 }
1850 printk (KERN_INFO "Command Packet Size: %s\n", buffer);
1851 printk (KERN_INFO "Model: %.40s\n",id->model);
1852 printk (KERN_INFO "Firmware Revision: %.8s\n",id->fw_rev);
1853 printk (KERN_INFO "Serial Number: %.20s\n",id->serial_no);
1854 printk (KERN_INFO "Write buffer size(?): %d bytes\n",id->buf_size*512);
1855 printk (KERN_INFO "DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
1856 printk (KERN_INFO "LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
1857 printk (KERN_INFO "IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
1858 printk (KERN_INFO "IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
1859 printk (KERN_INFO "ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
1860 printk (KERN_INFO "PIO Cycle Timing Category: %d\n",id->tPIO);
1861 printk (KERN_INFO "DMA Cycle Timing Category: %d\n",id->tDMA);
1862 printk (KERN_INFO "Single Word DMA supported modes:\n");
1863 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1864 if (id->dma_1word & mask)
1865 printk (KERN_INFO " Mode %d%s\n", i, (id->dma_1word & (mask << 8)) ? " (active)" : "");
1866 }
1867 printk (KERN_INFO "Multi Word DMA supported modes:\n");
1868 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1869 if (id->dma_mword & mask)
1870 printk (KERN_INFO " Mode %d%s\n", i, (id->dma_mword & (mask << 8)) ? " (active)" : "");
1871 }
1872 if (id->field_valid & 0x0002) {
1873 printk (KERN_INFO "Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
1874 if (id->eide_dma_min == 0)
1875 sprintf (buffer, "Not supported");
1876 else
1877 sprintf (buffer, "%d ns",id->eide_dma_min);
1878 printk (KERN_INFO "Minimum Multi-word DMA cycle per word: %s\n", buffer);
1879 if (id->eide_dma_time == 0)
1880 sprintf (buffer, "Not supported");
1881 else
1882 sprintf (buffer, "%d ns",id->eide_dma_time);
1883 printk (KERN_INFO "Manufacturer\'s Recommended Multi-word cycle: %s\n", buffer);
1884 if (id->eide_pio == 0)
1885 sprintf (buffer, "Not supported");
1886 else
1887 sprintf (buffer, "%d ns",id->eide_pio);
1888 printk (KERN_INFO "Minimum PIO cycle without IORDY: %s\n", buffer);
1889 if (id->eide_pio_iordy == 0)
1890 sprintf (buffer, "Not supported");
1891 else
1892 sprintf (buffer, "%d ns",id->eide_pio_iordy);
1893 printk (KERN_INFO "Minimum PIO cycle with IORDY: %s\n", buffer);
1894 } else
1895 printk (KERN_INFO "According to the device, fields 64-70 are not valid.\n");
1896 #endif /* IDEFLOPPY_DEBUG_INFO */
1897
1898 if (gcw.protocol != 2)
1899 printk (KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1900 else if (gcw.device_type != 0)
1901 printk (KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1902 else if (!gcw.removable)
1903 printk (KERN_ERR "ide-floppy: The removable flag is not set\n");
1904 else if (gcw.drq_type == 3) {
1905 printk (KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1906 } else if (gcw.packet_size != 0) {
1907 printk (KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1908 } else
1909 return 1;
1910 return 0;
1911 }
1912
1913 static void idefloppy_add_settings(ide_drive_t *drive)
1914 {
1915 int major = HWIF(drive)->major;
1916 int minor = drive->select.b.unit << PARTN_BITS;
1917
1918 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1919 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1920 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1921 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
1922 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
1923 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
1924
1925 }
1926
1927 /*
1928 * Driver initialization.
1929 */
1930 static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1931 {
1932 struct idefloppy_id_gcw gcw;
1933 int major = HWIF(drive)->major, i;
1934 int minor = drive->select.b.unit << PARTN_BITS;
1935
1936 *((unsigned short *) &gcw) = drive->id->config;
1937 drive->driver_data = floppy;
1938 drive->ready_stat = 0;
1939 memset (floppy, 0, sizeof (idefloppy_floppy_t));
1940 floppy->drive = drive;
1941 floppy->pc = floppy->pc_stack;
1942 if (gcw.drq_type == 1)
1943 set_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1944 /*
1945 * We used to check revisions here. At this point however
1946 * I'm giving up. Just assume they are all broken, its easier.
1947 *
1948 * The actual reason for the workarounds was likely
1949 * a driver bug after all rather than a firmware bug,
1950 * and the workaround below used to hide it. It should
1951 * be fixed as of version 1.9, but to be on the safe side
1952 * we'll leave the limitation below for the 2.2.x tree.
1953 */
1954
1955 if (strcmp(drive->id->model, "IOMEGA ZIP 100 ATAPI") == 0)
1956 {
1957 for (i = 0; i < 1 << PARTN_BITS; i++)
1958 max_sectors[major][minor + i] = 64;
1959 }
1960 /*
1961 * Guess what? The IOMEGA Clik! drive also needs the
1962 * above fix. It makes nasty clicking noises without
1963 * it, so please don't remove this.
1964 */
1965 if (strcmp(drive->id->model, "IOMEGA Clik! 40 CZ ATAPI") == 0)
1966 {
1967 for (i = 0; i < 1 << PARTN_BITS; i++)
1968 max_sectors[major][minor + i] = 64;
1969 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1970 }
1971
1972 /*
1973 * Guess what? The IOMEGA Clik! drive also needs the
1974 * above fix. It makes nasty clicking noises without
1975 * it, so please don't remove this.
1976 */
1977 if (strcmp(drive->id->model, "IOMEGA Clik! 40 CZ ATAPI") == 0)
1978 {
1979 for (i = 0; i < 1 << PARTN_BITS; i++)
1980 max_sectors[major][minor + i] = 64;
1981 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1982 }
1983
1984
1985 (void) idefloppy_get_capacity (drive);
1986 idefloppy_add_settings(drive);
1987 for (i = 0; i < MAX_DRIVES; ++i) {
1988 ide_hwif_t *hwif = HWIF(drive);
1989
1990 if (drive != &hwif->drives[i]) continue;
1991 hwif->gd->de_arr[i] = drive->de;
1992 if (drive->removable)
1993 hwif->gd->flags[i] |= GENHD_FL_REMOVABLE;
1994 break;
1995 }
1996 }
1997
1998 static int idefloppy_cleanup (ide_drive_t *drive)
1999 {
2000 idefloppy_floppy_t *floppy = drive->driver_data;
2001
2002 if (ide_unregister_subdriver (drive))
2003 return 1;
2004 drive->driver_data = NULL;
2005 kfree (floppy);
2006 return 0;
2007 }
2008
2009 #ifdef CONFIG_PROC_FS
2010
2011 static ide_proc_entry_t idefloppy_proc[] = {
2012 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
2013 { NULL, 0, NULL, NULL }
2014 };
2015
2016 #else
2017
2018 #define idefloppy_proc NULL
2019
2020 #endif /* CONFIG_PROC_FS */
2021
2022 static int idefloppy_reinit (ide_drive_t *drive)
2023 {
2024 return 0;
2025 }
2026
2027 /*
2028 * IDE subdriver functions, registered with ide.c
2029 */
2030 static ide_driver_t idefloppy_driver = {
2031 name: "ide-floppy",
2032 version: IDEFLOPPY_VERSION,
2033 media: ide_floppy,
2034 busy: 0,
2035 supports_dma: 1,
2036 supports_dsc_overlap: 0,
2037 cleanup: idefloppy_cleanup,
2038 do_request: idefloppy_do_request,
2039 end_request: idefloppy_end_request,
2040 ioctl: idefloppy_ioctl,
2041 open: idefloppy_open,
2042 release: idefloppy_release,
2043 media_change: idefloppy_media_change,
2044 revalidate: idefloppy_revalidate,
2045 pre_reset: NULL,
2046 capacity: idefloppy_capacity,
2047 special: NULL,
2048 proc: idefloppy_proc,
2049 driver_reinit: idefloppy_reinit,
2050 };
2051
2052 int idefloppy_init (void);
2053 static ide_module_t idefloppy_module = {
2054 IDE_DRIVER_MODULE,
2055 idefloppy_init,
2056 &idefloppy_driver,
2057 NULL
2058 };
2059
2060 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2061
2062 static void __exit idefloppy_exit (void)
2063 {
2064 ide_drive_t *drive;
2065 int failed = 0;
2066
2067 while ((drive = ide_scan_devices (ide_floppy, idefloppy_driver.name, &idefloppy_driver, failed)) != NULL) {
2068 if (idefloppy_cleanup (drive)) {
2069 printk ("%s: cleanup_module() called while still busy\n", drive->name);
2070 failed++;
2071 }
2072 /* We must remove proc entries defined in this module.
2073 Otherwise we oops while accessing these entries */
2074 if (drive->proc)
2075 ide_remove_proc_entries(drive->proc, idefloppy_proc);
2076 }
2077 ide_unregister_module(&idefloppy_module);
2078 }
2079
2080 /*
2081 * idefloppy_init will register the driver for each floppy.
2082 */
2083 int idefloppy_init (void)
2084 {
2085 ide_drive_t *drive;
2086 idefloppy_floppy_t *floppy;
2087 int failed = 0;
2088
2089 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
2090 MOD_INC_USE_COUNT;
2091 while ((drive = ide_scan_devices (ide_floppy, idefloppy_driver.name, NULL, failed++)) != NULL) {
2092 if (!idefloppy_identify_device (drive, drive->id)) {
2093 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
2094 continue;
2095 }
2096 if (drive->scsi) {
2097 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
2098 continue;
2099 }
2100 if ((floppy = (idefloppy_floppy_t *) kmalloc (sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
2101 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
2102 continue;
2103 }
2104 if (ide_register_subdriver (drive, &idefloppy_driver, IDE_SUBDRIVER_VERSION)) {
2105 printk (KERN_ERR "ide-floppy: %s: Failed to register the driver with ide.c\n", drive->name);
2106 kfree (floppy);
2107 continue;
2108 }
2109 idefloppy_setup (drive, floppy);
2110 failed--;
2111 }
2112 ide_register_module(&idefloppy_module);
2113 MOD_DEC_USE_COUNT;
2114 return 0;
2115 }
2116
2117 module_init(idefloppy_init);
2118 module_exit(idefloppy_exit);
2119