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