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