]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/ide/ide-floppy.c
ide: update/add my Copyrights
[net-next-2.6.git] / drivers / ide / ide-floppy.c
CommitLineData
1da177e4 1/*
59bca8cc
BZ
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
1da177e4
LT
5 */
6
7/*
8 * IDE ATAPI floppy driver.
9 *
10 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
12 *
13 * This driver supports the following IDE floppy drives:
14 *
15 * LS-120/240 SuperDisk
16 * Iomega Zip 100/250
17 * Iomega PC Card Clik!/PocketZip
18 *
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.
21 *
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
80 */
81
82#define IDEFLOPPY_VERSION "0.99.newide"
83
1da177e4
LT
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>
90#include <linux/mm.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>
cf8b8975 99#include <linux/mutex.h>
1da177e4 100
89636af2
BZ
101#include <scsi/scsi_ioctl.h>
102
1da177e4
LT
103#include <asm/byteorder.h>
104#include <asm/irq.h>
105#include <asm/uaccess.h>
106#include <asm/io.h>
107#include <asm/unaligned.h>
108
109/*
110 * The following are used to debug the driver.
111 */
112#define IDEFLOPPY_DEBUG_LOG 0
113#define IDEFLOPPY_DEBUG_INFO 0
114#define IDEFLOPPY_DEBUG_BUGS 1
115
116/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
117#define IDEFLOPPY_DEBUG( fmt, args... )
118
119#if IDEFLOPPY_DEBUG_LOG
120#define debug_log printk
121#else
122#define debug_log(fmt, args... ) do {} while(0)
123#endif
124
125
126/*
127 * Some drives require a longer irq timeout.
128 */
129#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
130
131/*
132 * After each failed packet command we issue a request sense command
133 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
134 */
135#define IDEFLOPPY_MAX_PC_RETRIES 3
136
137/*
138 * With each packet command, we allocate a buffer of
139 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
140 */
141#define IDEFLOPPY_PC_BUFFER_SIZE 256
142
143/*
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.
147 */
148#define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
149
150/*
151 * Our view of a packet command.
152 */
153typedef 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 */
167} idefloppy_pc_t;
168
169/*
170 * Packet command flag bits.
171 */
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 */
177
178#define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
179
180/*
181 * Removable Block Access Capabilities Page
182 */
183typedef struct {
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 */
192#else
193#error "Bitfield endianness not defined! Check your byteorder.h"
194#endif
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 */
212#else
213#error "Bitfield endianness not defined! Check your byteorder.h"
214#endif
215 u8 reserved[8];
216} idefloppy_capabilities_page_t;
217
218/*
219 * Flexible disk page.
220 */
221typedef struct {
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 */
230#else
231#error "Bitfield endianness not defined! Check your byteorder.h"
232#endif
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 */
238 u8 reserved10[10];
239 u8 motor_delay; /* Motor off delay */
240 u8 reserved21[7];
241 u16 rpm; /* Rotations per minute */
242 u8 reserved30[2];
243} idefloppy_flexible_disk_page_t;
244
245/*
246 * Format capacity
247 */
248typedef struct {
249 u8 reserved[3];
250 u8 length; /* Length of the following descriptors in bytes */
251} idefloppy_capacity_header_t;
252
253typedef struct {
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 */
261#else
262#error "Bitfield endianness not defined! Check your byteorder.h"
263#endif
264 u8 length_msb; /* Block Length (MSB)*/
265 u16 length; /* Block Length */
266} idefloppy_capacity_descriptor_t;
267
268#define CAPACITY_INVALID 0x00
269#define CAPACITY_UNFORMATTED 0x01
270#define CAPACITY_CURRENT 0x02
271#define CAPACITY_NO_CARTRIDGE 0x03
272
273/*
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.
277 */
278typedef struct ide_floppy_obj {
279 ide_drive_t *drive;
280 ide_driver_t *driver;
281 struct gendisk *disk;
282 struct kref kref;
c94964a4 283 unsigned int openers; /* protected by BKL for now */
1da177e4
LT
284
285 /* Current packet command */
286 idefloppy_pc_t *pc;
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 */
292 int pc_stack_index;
293 struct request rq_stack[IDEFLOPPY_PC_STACK];
294 /* We implement a circular array */
295 int rq_stack_index;
296
297 /*
298 * Last error information
299 */
300 u8 sense_key, asc, ascq;
301 /* delay this long before sending packet command */
302 u8 ticks;
303 int progress_indication;
304
305 /*
306 * Device information
307 */
308 /* Current format */
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;
314 /* Write protect */
315 int wp;
316 /* Supports format progress report */
317 int srfp;
318 /* Status/Action flags */
319 unsigned long flags;
320} idefloppy_floppy_t;
321
c40d3d38 322#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
1da177e4
LT
323
324/*
325 * Floppy flag bits values.
326 */
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 */
333
334/*
335 * ATAPI floppy drive packet commands
336 */
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
353
354/*
355 * Defines for the mode sense command
356 */
357#define MODE_SENSE_CURRENT 0x00
358#define MODE_SENSE_CHANGEABLE 0x01
359#define MODE_SENSE_DEFAULT 0x02
360#define MODE_SENSE_SAVED 0x03
361
362/*
363 * IOCTLs used in low-level formatting.
364 */
365
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
370
1da177e4
LT
371/*
372 * Error codes which are returned in rq->errors to the higher part
373 * of the driver.
374 */
375#define IDEFLOPPY_ERROR_GENERAL 101
376
377/*
378 * The following is used to format the general configuration word of
379 * the ATAPI IDENTIFY DEVICE command.
380 */
381struct 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 */
398#else
399#error "Bitfield endianness not defined! Check your byteorder.h"
400#endif
401};
402
403/*
404 * INQUIRY packet command - Data Format
405 */
406typedef struct {
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 */
431#else
432#error "Bitfield endianness not defined! Check your byteorder.h"
433#endif
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;
443
444/*
445 * REQUEST SENSE packet command result - Data Format.
446 */
447typedef struct {
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 */
464#else
465#error "Bitfield endianness not defined! Check your byteorder.h"
466#endif
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 */
473 u8 sksv[3];
474 u8 pad[2]; /* Padding to 20 bytes */
475} idefloppy_request_sense_result_t;
476
477/*
478 * Pages of the SELECT SENSE / MODE SENSE packet commands.
479 */
480#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
481#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
482
483/*
484 * Mode Parameter Header for the MODE SENSE packet command
485 */
486typedef struct {
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;
495#else
496#error "Bitfield endianness not defined! Check your byteorder.h"
497#endif
498 u8 reserved[4];
499} idefloppy_mode_parameter_header_t;
500
cf8b8975 501static DEFINE_MUTEX(idefloppy_ref_mutex);
1da177e4
LT
502
503#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
504
505#define ide_floppy_g(disk) \
506 container_of((disk)->private_data, struct ide_floppy_obj, driver)
507
508static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
509{
510 struct ide_floppy_obj *floppy = NULL;
511
cf8b8975 512 mutex_lock(&idefloppy_ref_mutex);
1da177e4
LT
513 floppy = ide_floppy_g(disk);
514 if (floppy)
515 kref_get(&floppy->kref);
cf8b8975 516 mutex_unlock(&idefloppy_ref_mutex);
1da177e4
LT
517 return floppy;
518}
519
520static void ide_floppy_release(struct kref *);
521
522static void ide_floppy_put(struct ide_floppy_obj *floppy)
523{
cf8b8975 524 mutex_lock(&idefloppy_ref_mutex);
1da177e4 525 kref_put(&floppy->kref, ide_floppy_release);
cf8b8975 526 mutex_unlock(&idefloppy_ref_mutex);
1da177e4
LT
527}
528
529/*
530 * Too bad. The drive wants to send us data which we are not ready to accept.
531 * Just throw it away.
532 */
533static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
534{
535 while (bcount--)
536 (void) HWIF(drive)->INB(IDE_DATA_REG);
537}
538
539#if IDEFLOPPY_DEBUG_BUGS
540static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
541{
542 while (bcount--)
543 HWIF(drive)->OUTB(0, IDE_DATA_REG);
544}
545#endif /* IDEFLOPPY_DEBUG_BUGS */
546
547
548/*
549 * idefloppy_do_end_request is used to finish servicing a request.
550 *
551 * For read/write requests, we will call ide_end_request to pass to the
552 * next buffer.
553 */
554static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
555{
556 idefloppy_floppy_t *floppy = drive->driver_data;
557 struct request *rq = HWGROUP(drive)->rq;
558 int error;
559
560 debug_log(KERN_INFO "Reached idefloppy_end_request\n");
561
562 switch (uptodate) {
563 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
564 case 1: error = 0; break;
565 default: error = uptodate;
566 }
567 if (error)
568 floppy->failed_pc = NULL;
569 /* Why does this happen? */
570 if (!rq)
571 return 0;
4aff5e23 572 if (!blk_special_request(rq)) {
1da177e4
LT
573 /* our real local end request function */
574 ide_end_request(drive, uptodate, nsecs);
575 return 0;
576 }
577 rq->errors = error;
578 /* fixme: need to move this local also */
579 ide_end_drive_cmd(drive, 0, 0);
580 return 0;
581}
582
583static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
584{
585 struct request *rq = pc->rq;
586 struct bio_vec *bvec;
5705f702 587 struct req_iterator iter;
1da177e4
LT
588 unsigned long flags;
589 char *data;
5705f702 590 int count, done = 0;
1da177e4 591
5705f702 592 rq_for_each_segment(bvec, rq, iter) {
6c92e699
JA
593 if (!bcount)
594 break;
1da177e4 595
6c92e699 596 count = min(bvec->bv_len, bcount);
1da177e4 597
6c92e699
JA
598 data = bvec_kmap_irq(bvec, &flags);
599 drive->hwif->atapi_input_bytes(drive, data, count);
600 bvec_kunmap_irq(data, &flags);
1da177e4 601
6c92e699
JA
602 bcount -= count;
603 pc->b_count += count;
604 done += count;
1da177e4
LT
605 }
606
607 idefloppy_do_end_request(drive, 1, done >> 9);
608
609 if (bcount) {
610 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
611 idefloppy_discard_data(drive, bcount);
612 }
613}
614
615static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
616{
617 struct request *rq = pc->rq;
5705f702 618 struct req_iterator iter;
1da177e4
LT
619 struct bio_vec *bvec;
620 unsigned long flags;
5705f702 621 int count, done = 0;
1da177e4
LT
622 char *data;
623
5705f702 624 rq_for_each_segment(bvec, rq, iter) {
6c92e699
JA
625 if (!bcount)
626 break;
1da177e4 627
6c92e699 628 count = min(bvec->bv_len, bcount);
1da177e4 629
6c92e699
JA
630 data = bvec_kmap_irq(bvec, &flags);
631 drive->hwif->atapi_output_bytes(drive, data, count);
632 bvec_kunmap_irq(data, &flags);
1da177e4 633
6c92e699
JA
634 bcount -= count;
635 pc->b_count += count;
636 done += count;
1da177e4
LT
637 }
638
639 idefloppy_do_end_request(drive, 1, done >> 9);
640
c7ea4b31 641#if IDEFLOPPY_DEBUG_BUGS
1da177e4
LT
642 if (bcount) {
643 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
644 idefloppy_write_zeros(drive, bcount);
645 }
c7ea4b31 646#endif
1da177e4
LT
647}
648
649static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
650{
651 struct request *rq = pc->rq;
652 struct bio *bio = rq->bio;
653
654 while ((bio = rq->bio) != NULL)
655 idefloppy_do_end_request(drive, 1, 0);
656}
657
658/*
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.
662 */
663static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
664{
665 struct ide_floppy_obj *floppy = drive->driver_data;
666
667 ide_init_drive_cmd(rq);
668 rq->buffer = (char *) pc;
4aff5e23 669 rq->cmd_type = REQ_TYPE_SPECIAL;
1da177e4
LT
670 rq->rq_disk = floppy->disk;
671 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
672}
673
674static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
675{
676 idefloppy_floppy_t *floppy = drive->driver_data;
677
678 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
679 floppy->pc_stack_index=0;
680 return (&floppy->pc_stack[floppy->pc_stack_index++]);
681}
682
683static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
684{
685 idefloppy_floppy_t *floppy = drive->driver_data;
686
687 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
688 floppy->rq_stack_index = 0;
689 return (&floppy->rq_stack[floppy->rq_stack_index++]);
690}
691
692/*
693 * idefloppy_analyze_error is called on each failed packet command retry
694 * to analyze the request sense.
695 */
696static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
697{
698 idefloppy_floppy_t *floppy = drive->driver_data;
699
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);
709 else
710 debug_log(KERN_INFO "ide-floppy: sense key = %x, asc = %x, "
711 "ascq = %x\n", result->sense_key,
712 result->asc, result->ascq);
713}
714
715static void idefloppy_request_sense_callback (ide_drive_t *drive)
716{
717 idefloppy_floppy_t *floppy = drive->driver_data;
718
719 debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
720
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);
724 } else {
725 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
726 idefloppy_do_end_request(drive, 0, 0);
727 }
728}
729
730/*
731 * General packet command callback function.
732 */
733static void idefloppy_pc_callback (ide_drive_t *drive)
734{
735 idefloppy_floppy_t *floppy = drive->driver_data;
736
737 debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
738
739 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
740}
741
742/*
743 * idefloppy_init_pc initializes a packet command.
744 */
745static void idefloppy_init_pc (idefloppy_pc_t *pc)
746{
747 memset(pc->c, 0, 12);
748 pc->retries = 0;
749 pc->flags = 0;
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;
754}
755
756static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
757{
758 idefloppy_init_pc(pc);
759 pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
760 pc->c[4] = 255;
761 pc->request_transfer = 18;
762 pc->callback = &idefloppy_request_sense_callback;
763}
764
765/*
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.
769 */
770static void idefloppy_retry_pc (ide_drive_t *drive)
771{
772 idefloppy_pc_t *pc;
773 struct request *rq;
1da177e4 774
0e38a66a 775 (void)drive->hwif->INB(IDE_ERROR_REG);
1da177e4
LT
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);
780}
781
782/*
783 * idefloppy_pc_intr is the usual interrupt handler which will be called
784 * during a packet command.
785 */
786static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
787{
788 idefloppy_floppy_t *floppy = drive->driver_data;
790d1239 789 ide_hwif_t *hwif = drive->hwif;
1da177e4
LT
790 idefloppy_pc_t *pc = floppy->pc;
791 struct request *rq = pc->rq;
792 unsigned int temp;
790d1239 793 u16 bcount;
8e7657ae 794 u8 stat, ireason;
1da177e4
LT
795
796 debug_log(KERN_INFO "ide-floppy: Reached %s interrupt handler\n",
797 __FUNCTION__);
798
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);
802 } else {
803 pc->actually_transferred = pc->request_transfer;
804 idefloppy_update_buffers(drive, pc);
805 }
806 debug_log(KERN_INFO "ide-floppy: DMA finished\n");
807 }
808
809 /* Clear the interrupt */
22c525b9 810 stat = drive->hwif->INB(IDE_STATUS_REG);
1da177e4 811
22c525b9 812 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
1da177e4
LT
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);
816
366c7f55 817 local_irq_enable_in_hardirq();
1da177e4 818
22c525b9 819 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1da177e4
LT
820 /* Error detected */
821 debug_log(KERN_INFO "ide-floppy: %s: I/O error\n",
822 drive->name);
823 rq->errors++;
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);
828 }
829 /* Retry operation */
830 idefloppy_retry_pc(drive);
831 /* queued, but not started */
832 return ide_stopped;
833 }
834 pc->error = 0;
835 if (floppy->failed_pc == pc)
836 floppy->failed_pc = NULL;
837 /* Command finished - Call the callback function */
838 pc->callback(drive);
839 return ide_stopped;
840 }
841
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");
7469aaf6 845 ide_dma_off(drive);
1da177e4
LT
846 return ide_do_reset(drive);
847 }
848
849 /* Get the number of bytes to transfer */
790d1239
BZ
850 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
851 hwif->INB(IDE_BCOUNTL_REG);
1da177e4 852 /* on this interrupt */
8e7657ae 853 ireason = hwif->INB(IDE_IREASON_REG);
1da177e4 854
8e7657ae 855 if (ireason & CD) {
1da177e4
LT
856 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
857 return ide_do_reset(drive);
858 }
8e7657ae 859 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
1da177e4
LT
860 /* Hopefully, we will never get here */
861 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
8e7657ae 862 (ireason & IO) ? "Write" : "Read");
1da177e4 863 printk(KERN_ERR "but the floppy wants us to %s !\n",
8e7657ae 864 (ireason & IO) ? "Read" : "Write");
1da177e4
LT
865 return ide_do_reset(drive);
866 }
867 if (!test_bit(PC_WRITING, &pc->flags)) {
868 /* Reading - Check that we have enough space */
790d1239 869 temp = pc->actually_transferred + bcount;
1da177e4
LT
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");
790d1239 875 idefloppy_discard_data(drive, bcount);
125e1874 876 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
877 ide_set_handler(drive,
878 &idefloppy_pc_intr,
879 IDEFLOPPY_WAIT_CMD,
880 NULL);
881 return ide_started;
882 }
883 debug_log(KERN_NOTICE "ide-floppy: The floppy wants to "
884 "send us more data than expected - "
885 "allowing transfer\n");
886 }
887 }
888 if (test_bit(PC_WRITING, &pc->flags)) {
889 if (pc->buffer != NULL)
890 /* Write the current buffer */
790d1239
BZ
891 hwif->atapi_output_bytes(drive, pc->current_position,
892 bcount);
1da177e4 893 else
790d1239 894 idefloppy_output_buffers(drive, pc, bcount);
1da177e4
LT
895 } else {
896 if (pc->buffer != NULL)
897 /* Read the current buffer */
790d1239
BZ
898 hwif->atapi_input_bytes(drive, pc->current_position,
899 bcount);
1da177e4 900 else
790d1239 901 idefloppy_input_buffers(drive, pc, bcount);
1da177e4
LT
902 }
903 /* Update the current position */
790d1239
BZ
904 pc->actually_transferred += bcount;
905 pc->current_position += bcount;
1da177e4 906
125e1874 907 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
908 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
909 return ide_started;
910}
911
912/*
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
916 */
917static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
918{
919 ide_startstop_t startstop;
920 idefloppy_floppy_t *floppy = drive->driver_data;
8e7657ae 921 u8 ireason;
1da177e4
LT
922
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");
926 return startstop;
927 }
8e7657ae
BZ
928 ireason = drive->hwif->INB(IDE_IREASON_REG);
929 if ((ireason & CD) == 0 || (ireason & IO)) {
1da177e4
LT
930 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
931 "issuing a packet command\n");
932 return ide_do_reset(drive);
933 }
125e1874 934 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
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);
939 return ide_started;
940}
941
942
943/*
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
948 * gets in trouble.
949 *
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.
954 */
955static int idefloppy_transfer_pc2 (ide_drive_t *drive)
956{
957 idefloppy_floppy_t *floppy = drive->driver_data;
958
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;
963}
964
965static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
966{
967 idefloppy_floppy_t *floppy = drive->driver_data;
968 ide_startstop_t startstop;
8e7657ae 969 u8 ireason;
1da177e4
LT
970
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");
974 return startstop;
975 }
8e7657ae
BZ
976 ireason = drive->hwif->INB(IDE_IREASON_REG);
977 if ((ireason & CD) == 0 || (ireason & IO)) {
1da177e4
LT
978 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
979 "while issuing a packet command\n");
980 return ide_do_reset(drive);
981 }
982 /*
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.
989 */
125e1874 990 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
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 */
995 return ide_started;
996}
997
998/**
999 * idefloppy_should_report_error()
1000 *
1001 * Supresses error messages resulting from Medium not present
1002 */
1003static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
1004{
1005 if (floppy->sense_key == 0x02 &&
1006 floppy->asc == 0x3a &&
1007 floppy->ascq == 0x00)
1008 return 0;
1009 return 1;
1010}
1011
1012/*
1013 * Issue a packet command
1014 */
1015static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
1016{
1017 idefloppy_floppy_t *floppy = drive->driver_data;
1018 ide_hwif_t *hwif = drive->hwif;
1da177e4 1019 ide_handler_t *pkt_xfer_routine;
790d1239 1020 u16 bcount;
e5f9f5a8 1021 u8 dma;
1da177e4 1022
1da177e4
LT
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 */
1027 floppy->pc = pc;
1028
1029 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
1030 test_bit(PC_ABORT, &pc->flags)) {
1031 /*
1032 * We will "abort" retrying a packet command in case
1033 * a legitimate error code was received.
1034 */
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],
1042 floppy->sense_key,
1043 floppy->asc, floppy->ascq);
1044 }
1045 /* Giving up */
1046 pc->error = IDEFLOPPY_ERROR_GENERAL;
1047 }
1048 floppy->failed_pc = NULL;
1049 pc->callback(drive);
1050 return ide_stopped;
1051 }
1052
1053 debug_log(KERN_INFO "Retry number - %d\n",pc->retries);
1054
1055 pc->retries++;
1056 /* We haven't transferred any data yet */
1057 pc->actually_transferred = 0;
1058 pc->current_position = pc->buffer;
790d1239 1059 bcount = min(pc->request_transfer, 63 * 1024);
1da177e4 1060
7469aaf6
BZ
1061 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
1062 ide_dma_off(drive);
1063
e5f9f5a8 1064 dma = 0;
1da177e4
LT
1065
1066 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
e5f9f5a8 1067 dma = !hwif->dma_setup(drive);
1da177e4 1068
2fc57388
BZ
1069 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1070 IDE_TFLAG_OUT_DEVICE, bcount, dma);
1da177e4 1071
e5f9f5a8 1072 if (dma) { /* Begin DMA, if necessary */
1da177e4
LT
1073 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1074 hwif->dma_start(drive);
1075 }
1076
1077 /* Can we transfer the packet when we get the interrupt or wait? */
1078 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
1079 /* wait */
1080 pkt_xfer_routine = &idefloppy_transfer_pc1;
1081 } else {
1082 /* immediate */
1083 pkt_xfer_routine = &idefloppy_transfer_pc;
1084 }
1085
1086 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1087 /* Issue the packet command */
1088 ide_execute_command(drive, WIN_PACKETCMD,
1089 pkt_xfer_routine,
1090 IDEFLOPPY_WAIT_CMD,
1091 NULL);
1092 return ide_started;
1093 } else {
1094 /* Issue the packet command */
1095 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1096 return (*pkt_xfer_routine) (drive);
1097 }
1098}
1099
1100static void idefloppy_rw_callback (ide_drive_t *drive)
1101{
1102 debug_log(KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
1103
1104 idefloppy_do_end_request(drive, 1, 0);
1105 return;
1106}
1107
1108static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1109{
1110 debug_log(KERN_INFO "ide-floppy: creating prevent removal command, "
1111 "prevent = %d\n", prevent);
1112
1113 idefloppy_init_pc(pc);
1114 pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
1115 pc->c[4] = prevent;
1116}
1117
1118static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1119{
1120 idefloppy_init_pc(pc);
1121 pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
1122 pc->c[7] = 255;
1123 pc->c[8] = 255;
1124 pc->request_transfer = 255;
1125}
1126
1127static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1128 int flags)
1129{
1130 idefloppy_init_pc(pc);
1131 pc->c[0] = IDEFLOPPY_FORMAT_UNIT_CMD;
1132 pc->c[1] = 0x17;
1133
1134 memset(pc->buffer, 0, 12);
1135 pc->buffer[1] = 0xA2;
1136 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1137
1138 if (flags & 1) /* Verify bit on... */
1139 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
1140 pc->buffer[3] = 8;
1141
1142 put_unaligned(htonl(b), (unsigned int *)(&pc->buffer[4]));
1143 put_unaligned(htonl(l), (unsigned int *)(&pc->buffer[8]));
1144 pc->buffer_size=12;
1145 set_bit(PC_WRITING, &pc->flags);
1146}
1147
1148/*
1149 * A mode sense command is used to "sense" floppy parameters.
1150 */
1151static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
1152{
1153 u16 length = sizeof(idefloppy_mode_parameter_header_t);
1154
1155 idefloppy_init_pc(pc);
1156 pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
1157 pc->c[1] = 0;
1158 pc->c[2] = page_code + (type << 6);
1159
1160 switch (page_code) {
1161 case IDEFLOPPY_CAPABILITIES_PAGE:
1162 length += 12;
1163 break;
1164 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1165 length += 32;
1166 break;
1167 default:
1168 printk(KERN_ERR "ide-floppy: unsupported page code "
1169 "in create_mode_sense_cmd\n");
1170 }
1171 put_unaligned(htons(length), (u16 *) &pc->c[7]);
1172 pc->request_transfer = length;
1173}
1174
1175static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1176{
1177 idefloppy_init_pc(pc);
1178 pc->c[0] = IDEFLOPPY_START_STOP_CMD;
1179 pc->c[4] = start;
1180}
1181
1182static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1183{
1184 idefloppy_init_pc(pc);
1185 pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
1186}
1187
1188static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1189{
1190 int block = sector / floppy->bs_factor;
1191 int blocks = rq->nr_sectors / floppy->bs_factor;
1192 int cmd = rq_data_dir(rq);
1193
1194 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1195 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1196 block, blocks);
1197
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]);
1202 } else {
1203 pc->c[0] = cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
1204 put_unaligned(htons(blocks), (unsigned short *) &pc->c[7]);
1205 }
1206 put_unaligned(htonl(block), (unsigned int *) &pc->c[2]);
1207 pc->callback = &idefloppy_rw_callback;
1208 pc->rq = rq;
1209 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
4aff5e23 1210 if (rq->cmd_flags & REQ_RW)
1da177e4
LT
1211 set_bit(PC_WRITING, &pc->flags);
1212 pc->buffer = NULL;
1213 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1214 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1215}
1216
3d6392cf 1217static void
1da177e4
LT
1218idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1219{
1da177e4 1220 idefloppy_init_pc(pc);
3d6392cf 1221 pc->callback = &idefloppy_rw_callback;
1da177e4 1222 memcpy(pc->c, rq->cmd, sizeof(pc->c));
3d6392cf
JA
1223 pc->rq = rq;
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;
1228 if (rq->bio)
1229 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1230
1231 /*
1232 * possibly problematic, doesn't look like ide-floppy correctly
1233 * handled scattered requests if dma fails...
1234 */
1235 pc->request_transfer = pc->buffer_size = rq->data_len;
1da177e4
LT
1236}
1237
1238/*
1239 * idefloppy_do_request is our request handling function.
1240 */
1241static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1242{
1243 idefloppy_floppy_t *floppy = drive->driver_data;
1244 idefloppy_pc_t *pc;
1245 unsigned long block = (unsigned long)block_s;
1246
cdd60262 1247 debug_log(KERN_INFO "dev: %s, flags: %lx, errors: %d\n",
18cddac3 1248 rq->rq_disk ? rq->rq_disk->disk_name : "?",
1da177e4
LT
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);
1253
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);
1261 }
1262 else
1263 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1264 drive->name);
1265 idefloppy_do_end_request(drive, 0, 0);
1266 return ide_stopped;
1267 }
4aff5e23 1268 if (blk_fs_request(rq)) {
1da177e4
LT
1269 if (((long)rq->sector % floppy->bs_factor) ||
1270 (rq->nr_sectors % floppy->bs_factor)) {
1271 printk("%s: unsupported r/w request size\n",
1272 drive->name);
1273 idefloppy_do_end_request(drive, 0, 0);
1274 return ide_stopped;
1275 }
1276 pc = idefloppy_next_pc_storage(drive);
1277 idefloppy_create_rw_cmd(floppy, pc, rq, block);
4aff5e23 1278 } else if (blk_special_request(rq)) {
1da177e4 1279 pc = (idefloppy_pc_t *) rq->buffer;
4aff5e23 1280 } else if (blk_pc_request(rq)) {
1da177e4 1281 pc = idefloppy_next_pc_storage(drive);
3d6392cf 1282 idefloppy_blockpc_cmd(floppy, pc, rq);
1da177e4
LT
1283 } else {
1284 blk_dump_rq_flags(rq,
1285 "ide-floppy: unsupported command in queue");
1286 idefloppy_do_end_request(drive, 0, 0);
1287 return ide_stopped;
1288 }
1289
1290 pc->rq = rq;
1291 return idefloppy_issue_pc(drive, pc);
1292}
1293
1294/*
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.
1297 */
1298static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1299{
1300 struct ide_floppy_obj *floppy = drive->driver_data;
1301 struct request rq;
1302
1303 ide_init_drive_cmd (&rq);
1304 rq.buffer = (char *) pc;
4aff5e23 1305 rq.cmd_type = REQ_TYPE_SPECIAL;
1da177e4
LT
1306 rq.rq_disk = floppy->disk;
1307
1308 return ide_do_drive_cmd(drive, &rq, ide_wait);
1309}
1310
1311/*
1312 * Look at the flexible disk page parameters. We will ignore the CHS
1313 * capacity parameters and use the LBA parameters instead.
1314 */
1315static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1316{
1317 idefloppy_floppy_t *floppy = drive->driver_data;
1318 idefloppy_pc_t pc;
1319 idefloppy_mode_parameter_header_t *header;
1320 idefloppy_flexible_disk_page_t *page;
1321 int capacity, lba_capacity;
1322
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");
1327 return 1;
1328 }
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);
1333
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);
1345
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;
1356 }
1357 return 0;
1358}
1359
1360static int idefloppy_get_capability_page(ide_drive_t *drive)
1361{
1362 idefloppy_floppy_t *floppy = drive->driver_data;
1363 idefloppy_pc_t pc;
1364 idefloppy_mode_parameter_header_t *header;
1365 idefloppy_capabilities_page_t *page;
1366
1367 floppy->srfp = 0;
1368 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1369 MODE_SENSE_CURRENT);
1370
1371 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1372 if (idefloppy_queue_pc_tail(drive,&pc)) {
1373 return 1;
1374 }
1375
1376 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1377 page= (idefloppy_capabilities_page_t *)(header+1);
1378 floppy->srfp = page->srfp;
1379 return (0);
1380}
1381
1382/*
1383 * Determine if a media is present in the floppy drive, and if so,
1384 * its LBA capacity.
1385 */
1386static int idefloppy_get_capacity (ide_drive_t *drive)
1387{
1388 idefloppy_floppy_t *floppy = drive->driver_data;
1389 idefloppy_pc_t pc;
1390 idefloppy_capacity_header_t *header;
1391 idefloppy_capacity_descriptor_t *descriptor;
1392 int i, descriptors, rc = 1, blocks, length;
1393
1394 drive->bios_cyl = 0;
1395 drive->bios_head = drive->bios_sect = 0;
fdb77da4
AC
1396 floppy->blocks = 0;
1397 floppy->bs_factor = 1;
1da177e4
LT
1398 set_capacity(floppy->disk, 0);
1399
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");
1403 return 1;
1404 }
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);
1408
1409 for (i = 0; i < descriptors; i++, descriptor++) {
1410 blocks = descriptor->blocks = ntohl(descriptor->blocks);
1411 length = descriptor->length = ntohs(descriptor->length);
1412
1413 if (!i)
1414 {
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))
1419 /*
1420 * If it is not a clik drive, break out
1421 * (maintains previous driver behaviour)
1422 */
1423 break;
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);
1434 } else {
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",
1441 drive->name);
1442 rc = 0;
1443 }
1444 break;
1445 case CAPACITY_NO_CARTRIDGE:
1446 /*
1447 * This is a KERN_ERR so it appears on screen
1448 * for the user to see
1449 */
1450 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1451 break;
1452 case CAPACITY_INVALID:
1453 printk(KERN_ERR "%s: Invalid capacity for disk "
1454 "in drive\n", drive->name);
1455 break;
1456 }
1457 }
1458 if (!i) {
1459 debug_log( "Descriptor 0 Code: %d\n",
1460 descriptor->dc);
1461 }
1462 debug_log( "Descriptor %d: %dkB, %d blocks, %d "
1463 "sector size\n", i, blocks * length / 1024, blocks,
1464 length);
1465 }
1466
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);
1470 }
1471
1472 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1473 return rc;
1474}
1475
1476/*
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.
1480**
1481** Userland gives us the following structure:
1482**
1483** struct idefloppy_format_capacities {
1484** int nformats;
1485** struct {
1486** int nblocks;
1487** int blocksize;
1488** } formats[];
1489** } ;
1490**
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.
1494**
1495*/
1496
1497static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1498{
1499 idefloppy_pc_t pc;
1500 idefloppy_capacity_header_t *header;
1501 idefloppy_capacity_descriptor_t *descriptor;
1502 int i, descriptors, blocks, length;
1503 int u_array_size;
1504 int u_index;
1505 int __user *argp;
1506
1507 if (get_user(u_array_size, arg))
1508 return (-EFAULT);
1509
1510 if (u_array_size <= 0)
1511 return (-EINVAL);
1512
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");
1516 return (-EIO);
1517 }
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);
1522
1523 u_index = 0;
1524 argp = arg + 1;
1525
1526 /*
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.
1530 */
1531
1532 for (i=0; i<descriptors; i++, descriptor++) {
1533 if (u_index >= u_array_size)
1534 break; /* User-supplied buffer too small */
1535 if (i == 0)
1536 continue; /* Skip the first descriptor */
1537
1538 blocks = ntohl(descriptor->blocks);
1539 length = ntohs(descriptor->length);
1540
1541 if (put_user(blocks, argp))
1542 return(-EFAULT);
1543 ++argp;
1544
1545 if (put_user(length, argp))
1546 return (-EFAULT);
1547 ++argp;
1548
1549 ++u_index;
1550 }
1551
1552 if (put_user(u_index, arg))
1553 return (-EFAULT);
1554 return (0);
1555}
1556
1557/*
1558** Send ATAPI_FORMAT_UNIT to the drive.
1559**
1560** Userland gives us the following structure:
1561**
1562** struct idefloppy_format_command {
1563** int nblocks;
1564** int blocksize;
1565** int flags;
1566** } ;
1567**
1568** flags is a bitmask, currently, the only defined flag is:
1569**
1570** 0x01 - verify media after format.
1571*/
1572
1573static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1574{
1575 int blocks;
1576 int length;
1577 int flags;
1578 idefloppy_pc_t pc;
1579
1580 if (get_user(blocks, arg) ||
1581 get_user(length, arg+1) ||
1582 get_user(flags, arg+2)) {
1583 return (-EFAULT);
1584 }
1585
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)) {
1590 return (-EIO);
1591 }
1592
1593 return (0);
1594}
1595
1596/*
1597** Get ATAPI_FORMAT_UNIT progress indication.
1598**
0779bf2d 1599** Userland gives a pointer to an int. The int is set to a progress
1da177e4
LT
1600** indicator 0-65536, with 65536=100%.
1601**
1602** If the drive does not support format progress indication, we just check
1603** the dsc bit, and return either 0 or 65536.
1604*/
1605
1606static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1607{
1608 idefloppy_floppy_t *floppy = drive->driver_data;
1609 idefloppy_pc_t pc;
1610 int progress_indication = 0x10000;
1611
1612 if (floppy->srfp) {
1613 idefloppy_create_request_sense_cmd(&pc);
1614 if (idefloppy_queue_pc_tail(drive, &pc)) {
1615 return (-EIO);
1616 }
1617
1618 if (floppy->sense_key == 2 &&
1619 floppy->asc == 4 &&
1620 floppy->ascq == 4) {
1621 progress_indication = floppy->progress_indication;
1622 }
1623 /* Else assume format_unit has finished, and we're
1624 ** at 0x10000 */
1625 } else {
1da177e4 1626 unsigned long flags;
22c525b9 1627 u8 stat;
1da177e4
LT
1628
1629 local_irq_save(flags);
22c525b9 1630 stat = drive->hwif->INB(IDE_STATUS_REG);
1da177e4
LT
1631 local_irq_restore(flags);
1632
22c525b9 1633 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1da177e4
LT
1634 }
1635 if (put_user(progress_indication, arg))
1636 return (-EFAULT);
1637
1638 return (0);
1639}
1640
1641/*
1642 * Return the current floppy capacity.
1643 */
1644static sector_t idefloppy_capacity (ide_drive_t *drive)
1645{
1646 idefloppy_floppy_t *floppy = drive->driver_data;
1647 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1648
1649 return capacity;
1650}
1651
1652/*
1653 * idefloppy_identify_device checks if we can support a drive,
1654 * based on the ATAPI IDENTIFY command results.
1655 */
1656static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1657{
1658 struct idefloppy_id_gcw gcw;
1659#if IDEFLOPPY_DEBUG_INFO
1da177e4
LT
1660 char buffer[80];
1661#endif /* IDEFLOPPY_DEBUG_INFO */
1662
1663 *((u16 *) &gcw) = id->config;
1664
1665#ifdef CONFIG_PPC
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;
1671#endif
1672
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;
1679 }
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");
1690 }
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;
1698 }
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;
1704 }
1705 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
1da177e4
LT
1706#endif /* IDEFLOPPY_DEBUG_INFO */
1707
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");
1718 } else
1719 return 1;
1720 return 0;
1721}
1722
7662d046 1723#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
1724static void idefloppy_add_settings(ide_drive_t *drive)
1725{
1726 idefloppy_floppy_t *floppy = drive->driver_data;
1727
1728/*
1497943e 1729 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1da177e4 1730 */
1497943e
BZ
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);
1da177e4 1735}
7662d046
BZ
1736#else
1737static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1738#endif
1da177e4
LT
1739
1740/*
1741 * Driver initialization.
1742 */
1743static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1744{
1745 struct idefloppy_id_gcw gcw;
1746
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);
1751 /*
1752 * We used to check revisions here. At this point however
1753 * I'm giving up. Just assume they are all broken, its easier.
1754 *
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.
1760 */
1761
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);
1767 }
1768
1769 /*
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.
1773 */
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);
1777 }
1778
1779
1780 (void) idefloppy_get_capacity(drive);
1781 idefloppy_add_settings(drive);
1782}
1783
4031bbe4 1784static void ide_floppy_remove(ide_drive_t *drive)
1da177e4
LT
1785{
1786 idefloppy_floppy_t *floppy = drive->driver_data;
1787 struct gendisk *g = floppy->disk;
1788
7662d046 1789 ide_proc_unregister_driver(drive, floppy->driver);
1da177e4
LT
1790
1791 del_gendisk(g);
1792
1793 ide_floppy_put(floppy);
1da177e4
LT
1794}
1795
1796static void ide_floppy_release(struct kref *kref)
1797{
1798 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1799 ide_drive_t *drive = floppy->drive;
1800 struct gendisk *g = floppy->disk;
1801
1802 drive->driver_data = NULL;
1803 g->private_data = NULL;
1804 put_disk(g);
1805 kfree(floppy);
1806}
1807
ecfd80e4 1808#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
1809static int proc_idefloppy_read_capacity
1810 (char *page, char **start, off_t off, int count, int *eof, void *data)
1811{
1812 ide_drive_t*drive = (ide_drive_t *)data;
1813 int len;
1814
1815 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1816 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1817}
1818
1819static 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 }
1823};
ecfd80e4 1824#endif /* CONFIG_IDE_PROC_FS */
1da177e4 1825
4031bbe4 1826static int ide_floppy_probe(ide_drive_t *);
1da177e4 1827
1da177e4 1828static ide_driver_t idefloppy_driver = {
8604affd 1829 .gen_driver = {
4ef3b8f4 1830 .owner = THIS_MODULE,
8604affd
BZ
1831 .name = "ide-floppy",
1832 .bus = &ide_bus_type,
8604affd 1833 },
4031bbe4
RK
1834 .probe = ide_floppy_probe,
1835 .remove = ide_floppy_remove,
1da177e4
LT
1836 .version = IDEFLOPPY_VERSION,
1837 .media = ide_floppy,
1da177e4 1838 .supports_dsc_overlap = 0,
1da177e4
LT
1839 .do_request = idefloppy_do_request,
1840 .end_request = idefloppy_do_end_request,
1841 .error = __ide_error,
1842 .abort = __ide_abort,
7662d046 1843#ifdef CONFIG_IDE_PROC_FS
1da177e4 1844 .proc = idefloppy_proc,
7662d046 1845#endif
1da177e4
LT
1846};
1847
1848static int idefloppy_open(struct inode *inode, struct file *filp)
1849{
1850 struct gendisk *disk = inode->i_bdev->bd_disk;
1851 struct ide_floppy_obj *floppy;
1852 ide_drive_t *drive;
1853 idefloppy_pc_t pc;
1854 int ret = 0;
1855
1856 debug_log(KERN_INFO "Reached idefloppy_open\n");
1857
1858 if (!(floppy = ide_floppy_get(disk)))
1859 return -ENXIO;
1860
1861 drive = floppy->drive;
1862
c94964a4 1863 floppy->openers++;
1da177e4 1864
c94964a4 1865 if (floppy->openers == 1) {
1da177e4
LT
1866 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1867 /* Just in case */
1868
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);
1873 }
1874
1875 if (idefloppy_get_capacity (drive)
1876 && (filp->f_flags & O_NDELAY) == 0
1877 /*
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
1881 */
1882 ) {
1da177e4
LT
1883 ret = -EIO;
1884 goto out_put_floppy;
1885 }
1886
1887 if (floppy->wp && (filp->f_mode & 2)) {
1da177e4
LT
1888 ret = -EROFS;
1889 goto out_put_floppy;
1890 }
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);
1896 }
1897 check_disk_change(inode->i_bdev);
1898 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
1da177e4
LT
1899 ret = -EBUSY;
1900 goto out_put_floppy;
1901 }
1902 return 0;
1903
1904out_put_floppy:
c94964a4 1905 floppy->openers--;
1da177e4
LT
1906 ide_floppy_put(floppy);
1907 return ret;
1908}
1909
1910static int idefloppy_release(struct inode *inode, struct file *filp)
1911{
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;
1915 idefloppy_pc_t pc;
1916
1917 debug_log(KERN_INFO "Reached idefloppy_release\n");
1918
c94964a4 1919 if (floppy->openers == 1) {
1da177e4
LT
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);
1924 }
1925
1926 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1927 }
c94964a4
BZ
1928
1929 floppy->openers--;
1da177e4
LT
1930
1931 ide_floppy_put(floppy);
1932
1933 return 0;
1934}
1935
a885c8c4
CH
1936static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1937{
1938 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1939 ide_drive_t *drive = floppy->drive;
1940
1941 geo->heads = drive->bios_head;
1942 geo->sectors = drive->bios_sect;
1943 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1944 return 0;
1945}
1946
1da177e4
LT
1947static int idefloppy_ioctl(struct inode *inode, struct file *file,
1948 unsigned int cmd, unsigned long arg)
1949{
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;
07203f64 1954 int err;
1da177e4
LT
1955 int prevent = (arg) ? 1 : 0;
1956 idefloppy_pc_t pc;
1da177e4
LT
1957
1958 switch (cmd) {
1959 case CDROMEJECT:
1960 prevent = 0;
1961 /* fall through */
1962 case CDROM_LOCKDOOR:
c94964a4 1963 if (floppy->openers > 1)
1da177e4
LT
1964 return -EBUSY;
1965
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);
1970 }
1971 if (cmd == CDROMEJECT) {
1972 idefloppy_create_start_stop_cmd(&pc, 2);
1973 (void) idefloppy_queue_pc_tail(drive, &pc);
1974 }
1975 return 0;
1976 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1977 return 0;
1978 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1979 return idefloppy_get_format_capacities(drive, argp);
1980 case IDEFLOPPY_IOCTL_FORMAT_START:
1981
1982 if (!(file->f_mode & 2))
1983 return -EPERM;
1984
c94964a4 1985 if (floppy->openers > 1) {
1da177e4
LT
1986 /* Don't format if someone is using the disk */
1987
1988 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1989 &floppy->flags);
1990 return -EBUSY;
1991 }
1992
1993 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1994
1995 err = idefloppy_begin_format(drive, argp);
1996 if (err)
1997 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1998 return err;
1999 /*
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.
2004 */
2005 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
2006 return idefloppy_get_format_progress(drive, argp);
2007 }
89636af2
BZ
2008
2009 /*
2010 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
2011 * and CDROM_SEND_PACKET (legacy) ioctls
2012 */
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);
2016 else
2017 err = -ENOTTY;
2018
2019 if (err == -ENOTTY)
2020 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2021
2022 return err;
1da177e4
LT
2023}
2024
2025static int idefloppy_media_changed(struct gendisk *disk)
2026{
2027 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
2028 ide_drive_t *drive = floppy->drive;
2029
2030 /* do not scan partitions twice if this is a removable device */
2031 if (drive->attach) {
2032 drive->attach = 0;
2033 return 0;
2034 }
2035 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
2036}
2037
2038static int idefloppy_revalidate_disk(struct gendisk *disk)
2039{
2040 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
2041 set_capacity(disk, idefloppy_capacity(floppy->drive));
2042 return 0;
2043}
2044
2045static struct block_device_operations idefloppy_ops = {
2046 .owner = THIS_MODULE,
2047 .open = idefloppy_open,
2048 .release = idefloppy_release,
2049 .ioctl = idefloppy_ioctl,
a885c8c4 2050 .getgeo = idefloppy_getgeo,
1da177e4
LT
2051 .media_changed = idefloppy_media_changed,
2052 .revalidate_disk= idefloppy_revalidate_disk
2053};
2054
4031bbe4 2055static int ide_floppy_probe(ide_drive_t *drive)
1da177e4
LT
2056{
2057 idefloppy_floppy_t *floppy;
2058 struct gendisk *g;
2059
2060 if (!strstr("ide-floppy", drive->driver_req))
2061 goto failed;
2062 if (!drive->present)
2063 goto failed;
2064 if (drive->media != ide_floppy)
2065 goto failed;
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);
2068 goto failed;
2069 }
2070 if (drive->scsi) {
2071 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
2072 goto failed;
2073 }
5cbded58 2074 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
1da177e4
LT
2075 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
2076 goto failed;
2077 }
2078
2079 g = alloc_disk(1 << PARTN_BITS);
2080 if (!g)
2081 goto out_free_floppy;
2082
2083 ide_init_disk(g, drive);
2084
7662d046 2085 ide_proc_register_driver(drive, &idefloppy_driver);
1da177e4 2086
1da177e4
LT
2087 kref_init(&floppy->kref);
2088
2089 floppy->drive = drive;
2090 floppy->driver = &idefloppy_driver;
2091 floppy->disk = g;
2092
2093 g->private_data = &floppy->driver;
2094
2095 drive->driver_data = floppy;
2096
1da177e4 2097 idefloppy_setup (drive, floppy);
8604affd 2098
1da177e4
LT
2099 g->minors = 1 << PARTN_BITS;
2100 g->driverfs_dev = &drive->gendev;
1da177e4
LT
2101 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
2102 g->fops = &idefloppy_ops;
2103 drive->attach = 1;
2104 add_disk(g);
2105 return 0;
2106
1da177e4
LT
2107out_free_floppy:
2108 kfree(floppy);
2109failed:
8604affd 2110 return -ENODEV;
1da177e4
LT
2111}
2112
2113MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2114
2115static void __exit idefloppy_exit (void)
2116{
8604affd 2117 driver_unregister(&idefloppy_driver.gen_driver);
1da177e4
LT
2118}
2119
17514e8a 2120static int __init idefloppy_init(void)
1da177e4
LT
2121{
2122 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
8604affd 2123 return driver_register(&idefloppy_driver.gen_driver);
1da177e4
LT
2124}
2125
263756ec 2126MODULE_ALIAS("ide:*m-floppy*");
1da177e4
LT
2127module_init(idefloppy_init);
2128module_exit(idefloppy_exit);
2129MODULE_LICENSE("GPL");