Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Borislav Petkov | d3f2084 | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 2 | * IDE ATAPI floppy driver. |
| 3 | * |
Bartlomiej Zolnierkiewicz | 59bca8c | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 4 | * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il> |
| 5 | * Copyright (C) 2000-2002 Paul Bristow <paul@paulbristow.net> |
| 6 | * Copyright (C) 2005 Bartlomiej Zolnierkiewicz |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * The driver currently doesn't have any fancy features, just the bare |
| 9 | * minimum read/write support. |
| 10 | * |
| 11 | * This driver supports the following IDE floppy drives: |
| 12 | * |
| 13 | * LS-120/240 SuperDisk |
| 14 | * Iomega Zip 100/250 |
| 15 | * Iomega PC Card Clik!/PocketZip |
| 16 | * |
Borislav Petkov | d3f2084 | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 17 | * For a historical changelog see |
| 18 | * Documentation/ide/ChangeLog.ide-floppy.1996-2002 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #define IDEFLOPPY_VERSION "0.99.newide" |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/timer.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/major.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/genhd.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/cdrom.h> |
| 36 | #include <linux/ide.h> |
| 37 | #include <linux/bitops.h> |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Bartlomiej Zolnierkiewicz | 89636af | 2007-07-20 01:11:59 +0200 | [diff] [blame] | 40 | #include <scsi/scsi_ioctl.h> |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/byteorder.h> |
Borislav Petkov | 7e8b163 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 43 | #include <linux/irq.h> |
| 44 | #include <linux/uaccess.h> |
| 45 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/unaligned.h> |
| 47 | |
Borislav Petkov | f373bd8 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 48 | /* define to see debug info */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #define IDEFLOPPY_DEBUG_LOG 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 52 | #define IDEFLOPPY_DEBUG(fmt, args...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | #if IDEFLOPPY_DEBUG_LOG |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 55 | #define debug_log(fmt, args...) \ |
| 56 | printk(KERN_INFO "ide-floppy: " fmt, ## args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #else |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 58 | #define debug_log(fmt, args...) do {} while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
| 61 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 62 | /* Some drives require a longer irq timeout. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD) |
| 64 | |
| 65 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 66 | * After each failed packet command we issue a request sense command and retry |
| 67 | * the packet command IDEFLOPPY_MAX_PC_RETRIES times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | */ |
| 69 | #define IDEFLOPPY_MAX_PC_RETRIES 3 |
| 70 | |
| 71 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 72 | * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE |
| 73 | * bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
| 75 | #define IDEFLOPPY_PC_BUFFER_SIZE 256 |
| 76 | |
| 77 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 78 | * In various places in the driver, we need to allocate storage for packet |
| 79 | * commands and requests, which will remain valid while we leave the driver to |
| 80 | * wait for an interrupt or a timeout event. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | */ |
| 82 | #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES) |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | typedef struct idefloppy_packet_command_s { |
| 85 | u8 c[12]; /* Actual packet bytes */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 86 | int retries; /* On each retry, we increment |
| 87 | retries */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | int error; /* Error code */ |
| 89 | int request_transfer; /* Bytes to transfer */ |
| 90 | int actually_transferred; /* Bytes actually transferred */ |
| 91 | int buffer_size; /* Size of our data buffer */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 92 | int b_count; /* Missing/Available data on |
| 93 | the current buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | struct request *rq; /* The corresponding request */ |
| 95 | u8 *buffer; /* Data buffer */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 96 | u8 *current_position; /* Pointer into above buffer */ |
| 97 | void (*callback) (ide_drive_t *); /* Called when this packet |
| 98 | command is completed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 100 | unsigned long flags; /* Status/Action bit flags: long |
| 101 | for set_bit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } idefloppy_pc_t; |
| 103 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 104 | /* Packet command flag bits. */ |
| 105 | enum { |
| 106 | /* 1 when we prefer to use DMA if possible */ |
| 107 | PC_FLAG_DMA_RECOMMENDED = (1 << 0), |
| 108 | /* 1 while DMA in progress */ |
| 109 | PC_FLAG_DMA_IN_PROGRESS = (1 << 1), |
| 110 | /* 1 when encountered problem during DMA */ |
| 111 | PC_FLAG_DMA_ERROR = (1 << 2), |
| 112 | /* Data direction */ |
| 113 | PC_FLAG_WRITING = (1 << 3), |
| 114 | /* Suppress error reporting */ |
| 115 | PC_FLAG_SUPPRESS_ERROR = (1 << 4), |
| 116 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 118 | /* format capacities descriptor codes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | #define CAPACITY_INVALID 0x00 |
| 120 | #define CAPACITY_UNFORMATTED 0x01 |
| 121 | #define CAPACITY_CURRENT 0x02 |
| 122 | #define CAPACITY_NO_CARTRIDGE 0x03 |
| 123 | |
| 124 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 125 | * Most of our global data which we need to save even as we leave the driver |
| 126 | * due to an interrupt or a timer event is stored in a variable of type |
| 127 | * idefloppy_floppy_t, defined below. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | */ |
| 129 | typedef struct ide_floppy_obj { |
| 130 | ide_drive_t *drive; |
| 131 | ide_driver_t *driver; |
| 132 | struct gendisk *disk; |
| 133 | struct kref kref; |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 134 | unsigned int openers; /* protected by BKL for now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | /* Current packet command */ |
| 137 | idefloppy_pc_t *pc; |
| 138 | /* Last failed packet command */ |
| 139 | idefloppy_pc_t *failed_pc; |
| 140 | /* Packet command stack */ |
| 141 | idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK]; |
| 142 | /* Next free packet command storage space */ |
| 143 | int pc_stack_index; |
| 144 | struct request rq_stack[IDEFLOPPY_PC_STACK]; |
| 145 | /* We implement a circular array */ |
| 146 | int rq_stack_index; |
| 147 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 148 | /* Last error information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | u8 sense_key, asc, ascq; |
| 150 | /* delay this long before sending packet command */ |
| 151 | u8 ticks; |
| 152 | int progress_indication; |
| 153 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 154 | /* Device information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* Current format */ |
| 156 | int blocks, block_size, bs_factor; |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 157 | /* Last format capacity descriptor */ |
| 158 | u8 cap_desc[8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* Copy of the flexible disk page */ |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 160 | u8 flexible_disk_page[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* Write protect */ |
| 162 | int wp; |
| 163 | /* Supports format progress report */ |
| 164 | int srfp; |
| 165 | /* Status/Action flags */ |
| 166 | unsigned long flags; |
| 167 | } idefloppy_floppy_t; |
| 168 | |
Bartlomiej Zolnierkiewicz | c40d3d3 | 2005-08-18 22:09:21 +0200 | [diff] [blame] | 169 | #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 171 | /* Floppy flag bits values. */ |
| 172 | enum { |
| 173 | /* DRQ interrupt device */ |
| 174 | IDEFLOPPY_FLAG_DRQ_INTERRUPT = (1 << 0), |
| 175 | /* Media may have changed */ |
| 176 | IDEFLOPPY_FLAG_MEDIA_CHANGED = (1 << 1), |
| 177 | /* Format in progress */ |
| 178 | IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS = (1 << 2), |
| 179 | /* Avoid commands not supported in Clik drive */ |
| 180 | IDEFLOPPY_FLAG_CLIK_DRIVE = (1 << 3), |
| 181 | /* Requires BH algorithm for packets */ |
| 182 | IDEFLOPPY_FLAG_ZIP_DRIVE = (1 << 4), |
| 183 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 185 | /* Defines for the MODE SENSE command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | #define MODE_SENSE_CURRENT 0x00 |
| 187 | #define MODE_SENSE_CHANGEABLE 0x01 |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 188 | #define MODE_SENSE_DEFAULT 0x02 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | #define MODE_SENSE_SAVED 0x03 |
| 190 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 191 | /* IOCTLs used in low-level formatting. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600 |
| 193 | #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601 |
| 194 | #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602 |
| 195 | #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603 |
| 196 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 197 | /* Error code returned in rq->errors to the higher part of the driver. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | #define IDEFLOPPY_ERROR_GENERAL 101 |
| 199 | |
| 200 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 201 | * The following is used to format the general configuration word of the |
| 202 | * ATAPI IDENTIFY DEVICE command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 204 | struct idefloppy_id_gcw { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 206 | unsigned packet_size :2; /* Packet Size */ |
| 207 | unsigned reserved234 :3; /* Reserved */ |
| 208 | unsigned drq_type :2; /* Command packet DRQ type */ |
| 209 | unsigned removable :1; /* Removable media */ |
| 210 | unsigned device_type :5; /* Device type */ |
| 211 | unsigned reserved13 :1; /* Reserved */ |
| 212 | unsigned protocol :2; /* Protocol type */ |
| 213 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 214 | unsigned protocol :2; /* Protocol type */ |
| 215 | unsigned reserved13 :1; /* Reserved */ |
| 216 | unsigned device_type :5; /* Device type */ |
| 217 | unsigned removable :1; /* Removable media */ |
| 218 | unsigned drq_type :2; /* Command packet DRQ type */ |
| 219 | unsigned reserved234 :3; /* Reserved */ |
| 220 | unsigned packet_size :2; /* Packet Size */ |
| 221 | #else |
| 222 | #error "Bitfield endianness not defined! Check your byteorder.h" |
| 223 | #endif |
| 224 | }; |
| 225 | |
| 226 | /* |
Borislav Petkov | 948391d | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 227 | * Pages of the SELECT SENSE / MODE SENSE packet commands. |
| 228 | * See SFF-8070i spec. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | */ |
| 230 | #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b |
| 231 | #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05 |
| 232 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 233 | static DEFINE_MUTEX(idefloppy_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref) |
| 236 | |
| 237 | #define ide_floppy_g(disk) \ |
| 238 | container_of((disk)->private_data, struct ide_floppy_obj, driver) |
| 239 | |
| 240 | static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk) |
| 241 | { |
| 242 | struct ide_floppy_obj *floppy = NULL; |
| 243 | |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 244 | mutex_lock(&idefloppy_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | floppy = ide_floppy_g(disk); |
| 246 | if (floppy) |
| 247 | kref_get(&floppy->kref); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 248 | mutex_unlock(&idefloppy_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return floppy; |
| 250 | } |
| 251 | |
Borislav Petkov | 9a24b63 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 252 | static void idefloppy_cleanup_obj(struct kref *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | static void ide_floppy_put(struct ide_floppy_obj *floppy) |
| 255 | { |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 256 | mutex_lock(&idefloppy_ref_mutex); |
Borislav Petkov | 9a24b63 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 257 | kref_put(&floppy->kref, idefloppy_cleanup_obj); |
Arjan van de Ven | cf8b897 | 2006-03-23 03:00:45 -0800 | [diff] [blame] | 258 | mutex_unlock(&idefloppy_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 262 | * Too bad. The drive wants to send us data which we are not ready to accept. |
| 263 | * Just throw it away. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 265 | static void idefloppy_discard_data(ide_drive_t *drive, unsigned int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
| 267 | while (bcount--) |
| 268 | (void) HWIF(drive)->INB(IDE_DATA_REG); |
| 269 | } |
| 270 | |
Borislav Petkov | 0bf399e | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 271 | static void idefloppy_write_zeros(ide_drive_t *drive, unsigned int bcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
| 273 | while (bcount--) |
| 274 | HWIF(drive)->OUTB(0, IDE_DATA_REG); |
| 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | |
| 278 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 279 | * Used to finish servicing a request. For read/write requests, we will call |
| 280 | * ide_end_request to pass to the next buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | */ |
| 282 | static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs) |
| 283 | { |
| 284 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 285 | struct request *rq = HWGROUP(drive)->rq; |
| 286 | int error; |
| 287 | |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 288 | debug_log("Reached %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | switch (uptodate) { |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 291 | case 0: error = IDEFLOPPY_ERROR_GENERAL; break; |
| 292 | case 1: error = 0; break; |
| 293 | default: error = uptodate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | if (error) |
| 296 | floppy->failed_pc = NULL; |
| 297 | /* Why does this happen? */ |
| 298 | if (!rq) |
| 299 | return 0; |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 300 | if (!blk_special_request(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | /* our real local end request function */ |
| 302 | ide_end_request(drive, uptodate, nsecs); |
| 303 | return 0; |
| 304 | } |
| 305 | rq->errors = error; |
| 306 | /* fixme: need to move this local also */ |
| 307 | ide_end_drive_cmd(drive, 0, 0); |
| 308 | return 0; |
| 309 | } |
| 310 | |
Borislav Petkov | 32925e1 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 311 | static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc, |
| 312 | unsigned int bcount, int direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | struct request *rq = pc->rq; |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 315 | struct req_iterator iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | struct bio_vec *bvec; |
| 317 | unsigned long flags; |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 318 | int count, done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | char *data; |
| 320 | |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 321 | rq_for_each_segment(bvec, rq, iter) { |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 322 | if (!bcount) |
| 323 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 325 | count = min(bvec->bv_len, bcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 327 | data = bvec_kmap_irq(bvec, &flags); |
Borislav Petkov | 32925e1 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 328 | if (direction) |
| 329 | drive->hwif->atapi_output_bytes(drive, data, count); |
| 330 | else |
| 331 | drive->hwif->atapi_input_bytes(drive, data, count); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 332 | bvec_kunmap_irq(data, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 334 | bcount -= count; |
| 335 | pc->b_count += count; |
| 336 | done += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | idefloppy_do_end_request(drive, 1, done >> 9); |
| 340 | |
| 341 | if (bcount) { |
Borislav Petkov | 32925e1 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 342 | printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n", |
| 343 | drive->name, __func__, bcount); |
| 344 | if (direction) |
| 345 | idefloppy_write_zeros(drive, bcount); |
| 346 | else |
| 347 | idefloppy_discard_data(drive, bcount); |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 352 | static void idefloppy_update_buffers(ide_drive_t *drive, idefloppy_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | struct request *rq = pc->rq; |
| 355 | struct bio *bio = rq->bio; |
| 356 | |
| 357 | while ((bio = rq->bio) != NULL) |
| 358 | idefloppy_do_end_request(drive, 1, 0); |
| 359 | } |
| 360 | |
| 361 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 362 | * Generate a new packet command request in front of the request queue, before |
| 363 | * the current request so that it will be processed immediately, on the next |
| 364 | * pass through the driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 366 | static void idefloppy_queue_pc_head(ide_drive_t *drive, idefloppy_pc_t *pc, |
| 367 | struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
| 369 | struct ide_floppy_obj *floppy = drive->driver_data; |
| 370 | |
| 371 | ide_init_drive_cmd(rq); |
| 372 | rq->buffer = (char *) pc; |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 373 | rq->cmd_type = REQ_TYPE_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | rq->rq_disk = floppy->disk; |
| 375 | (void) ide_do_drive_cmd(drive, rq, ide_preempt); |
| 376 | } |
| 377 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 378 | static idefloppy_pc_t *idefloppy_next_pc_storage(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
| 380 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 381 | |
| 382 | if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK) |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 383 | floppy->pc_stack_index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return (&floppy->pc_stack[floppy->pc_stack_index++]); |
| 385 | } |
| 386 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 387 | static struct request *idefloppy_next_rq_storage(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
| 389 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 390 | |
| 391 | if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK) |
| 392 | floppy->rq_stack_index = 0; |
| 393 | return (&floppy->rq_stack[floppy->rq_stack_index++]); |
| 394 | } |
| 395 | |
Borislav Petkov | a6ff2d3 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 396 | static void idefloppy_request_sense_callback(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
| 398 | idefloppy_floppy_t *floppy = drive->driver_data; |
Borislav Petkov | a6ff2d3 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 399 | u8 *buf = floppy->pc->buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 401 | debug_log("Reached %s\n", __func__); |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | if (!floppy->pc->error) { |
Borislav Petkov | a6ff2d3 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 404 | floppy->sense_key = buf[2] & 0x0F; |
| 405 | floppy->asc = buf[12]; |
| 406 | floppy->ascq = buf[13]; |
| 407 | floppy->progress_indication = buf[15] & 0x80 ? |
| 408 | (u16)get_unaligned((u16 *)&buf[16]) : 0x10000; |
| 409 | |
| 410 | if (floppy->failed_pc) |
| 411 | debug_log("pc = %x, sense key = %x, asc = %x," |
| 412 | " ascq = %x\n", |
| 413 | floppy->failed_pc->c[0], |
| 414 | floppy->sense_key, |
| 415 | floppy->asc, |
| 416 | floppy->ascq); |
| 417 | else |
| 418 | debug_log("sense key = %x, asc = %x, ascq = %x\n", |
| 419 | floppy->sense_key, |
| 420 | floppy->asc, |
| 421 | floppy->ascq); |
| 422 | |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | idefloppy_do_end_request(drive, 1, 0); |
| 425 | } else { |
Borislav Petkov | a6ff2d3 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 426 | printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting" |
| 427 | " request!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | idefloppy_do_end_request(drive, 0, 0); |
| 429 | } |
| 430 | } |
| 431 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 432 | /* General packet command callback function. */ |
| 433 | static void idefloppy_pc_callback(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | idefloppy_floppy_t *floppy = drive->driver_data; |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 436 | |
| 437 | debug_log("Reached %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
| 439 | idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0); |
| 440 | } |
| 441 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 442 | static void idefloppy_init_pc(idefloppy_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
| 444 | memset(pc->c, 0, 12); |
| 445 | pc->retries = 0; |
| 446 | pc->flags = 0; |
| 447 | pc->request_transfer = 0; |
| 448 | pc->buffer = pc->pc_buffer; |
| 449 | pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE; |
| 450 | pc->callback = &idefloppy_pc_callback; |
| 451 | } |
| 452 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 453 | static void idefloppy_create_request_sense_cmd(idefloppy_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 455 | idefloppy_init_pc(pc); |
| 456 | pc->c[0] = GPCMD_REQUEST_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | pc->c[4] = 255; |
| 458 | pc->request_transfer = 18; |
| 459 | pc->callback = &idefloppy_request_sense_callback; |
| 460 | } |
| 461 | |
| 462 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 463 | * Called when an error was detected during the last packet command. We queue a |
| 464 | * request sense packet command in the head of the request list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 466 | static void idefloppy_retry_pc(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
| 468 | idefloppy_pc_t *pc; |
| 469 | struct request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Bartlomiej Zolnierkiewicz | 0e38a66 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 471 | (void)drive->hwif->INB(IDE_ERROR_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | pc = idefloppy_next_pc_storage(drive); |
| 473 | rq = idefloppy_next_rq_storage(drive); |
| 474 | idefloppy_create_request_sense_cmd(pc); |
| 475 | idefloppy_queue_pc_head(drive, pc, rq); |
| 476 | } |
| 477 | |
Borislav Petkov | 0eea645 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 478 | /* The usual interrupt handler called during a packet command. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive) |
| 480 | { |
| 481 | idefloppy_floppy_t *floppy = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 482 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | idefloppy_pc_t *pc = floppy->pc; |
| 484 | struct request *rq = pc->rq; |
Borislav Petkov | 0eea645 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 485 | xfer_func_t *xferfunc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | unsigned int temp; |
Borislav Petkov | d30a7fb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 487 | int dma_error = 0; |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 488 | u16 bcount; |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 489 | u8 stat, ireason; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 491 | debug_log("Reached %s interrupt handler\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 493 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
Borislav Petkov | d30a7fb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 494 | dma_error = hwif->ide_dma_end(drive); |
| 495 | if (dma_error) { |
| 496 | printk(KERN_ERR "%s: DMA %s error\n", drive->name, |
| 497 | rq_data_dir(rq) ? "write" : "read"); |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 498 | pc->flags |= PC_FLAG_DMA_ERROR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } else { |
| 500 | pc->actually_transferred = pc->request_transfer; |
| 501 | idefloppy_update_buffers(drive, pc); |
| 502 | } |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 503 | debug_log("DMA finished\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* Clear the interrupt */ |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 507 | stat = drive->hwif->INB(IDE_STATUS_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 509 | /* No more interrupts */ |
| 510 | if ((stat & DRQ_STAT) == 0) { |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 511 | debug_log("Packet command completed, %d bytes transferred\n", |
| 512 | pc->actually_transferred); |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 513 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Ingo Molnar | 366c7f5 | 2006-07-03 00:25:25 -0700 | [diff] [blame] | 515 | local_irq_enable_in_hardirq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 517 | if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* Error detected */ |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 519 | debug_log("%s: I/O error\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | rq->errors++; |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 521 | if (pc->c[0] == GPCMD_REQUEST_SENSE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | printk(KERN_ERR "ide-floppy: I/O error in " |
| 523 | "request sense command\n"); |
| 524 | return ide_do_reset(drive); |
| 525 | } |
| 526 | /* Retry operation */ |
| 527 | idefloppy_retry_pc(drive); |
| 528 | /* queued, but not started */ |
| 529 | return ide_stopped; |
| 530 | } |
| 531 | pc->error = 0; |
| 532 | if (floppy->failed_pc == pc) |
| 533 | floppy->failed_pc = NULL; |
| 534 | /* Command finished - Call the callback function */ |
| 535 | pc->callback(drive); |
| 536 | return ide_stopped; |
| 537 | } |
| 538 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 539 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
| 540 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | printk(KERN_ERR "ide-floppy: The floppy wants to issue " |
| 542 | "more interrupts in DMA mode\n"); |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 543 | ide_dma_off(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | return ide_do_reset(drive); |
| 545 | } |
| 546 | |
| 547 | /* Get the number of bytes to transfer */ |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 548 | bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) | |
| 549 | hwif->INB(IDE_BCOUNTL_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | /* on this interrupt */ |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 551 | ireason = hwif->INB(IDE_IREASON_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 553 | if (ireason & CD) { |
Borislav Petkov | 0eea645 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 554 | printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | return ide_do_reset(drive); |
| 556 | } |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 557 | if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* Hopefully, we will never get here */ |
| 559 | printk(KERN_ERR "ide-floppy: We wanted to %s, ", |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 560 | (ireason & IO) ? "Write" : "Read"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | printk(KERN_ERR "but the floppy wants us to %s !\n", |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 562 | (ireason & IO) ? "Read" : "Write"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return ide_do_reset(drive); |
| 564 | } |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 565 | if (!(pc->flags & PC_FLAG_WRITING)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | /* Reading - Check that we have enough space */ |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 567 | temp = pc->actually_transferred + bcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (temp > pc->request_transfer) { |
| 569 | if (temp > pc->buffer_size) { |
| 570 | printk(KERN_ERR "ide-floppy: The floppy wants " |
| 571 | "to send us more data than expected " |
| 572 | "- discarding data\n"); |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 573 | idefloppy_discard_data(drive, bcount); |
Borislav Petkov | 0078df2 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | ide_set_handler(drive, |
| 576 | &idefloppy_pc_intr, |
| 577 | IDEFLOPPY_WAIT_CMD, |
| 578 | NULL); |
| 579 | return ide_started; |
| 580 | } |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 581 | debug_log("The floppy wants to send us more data than" |
| 582 | " expected - allowing transfer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 585 | if (pc->flags & PC_FLAG_WRITING) |
Borislav Petkov | 0eea645 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 586 | xferfunc = hwif->atapi_output_bytes; |
Borislav Petkov | 32925e1 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 587 | else |
Borislav Petkov | 0eea645 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 588 | xferfunc = hwif->atapi_input_bytes; |
Borislav Petkov | 0eea645 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 589 | |
| 590 | if (pc->buffer) |
| 591 | xferfunc(drive, pc->current_position, bcount); |
| 592 | else |
Borislav Petkov | 32925e1 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 593 | ide_floppy_io_buffers(drive, pc, bcount, |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 594 | !!(pc->flags & PC_FLAG_WRITING)); |
Borislav Petkov | 0eea645 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | /* Update the current position */ |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 597 | pc->actually_transferred += bcount; |
| 598 | pc->current_position += bcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Borislav Petkov | 32925e1 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 600 | /* And set the interrupt handler again */ |
| 601 | ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | return ide_started; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * This is the original routine that did the packet transfer. |
| 607 | * It fails at high speeds on the Iomega ZIP drive, so there's a slower version |
| 608 | * for that drive below. The algorithm is chosen based on drive type |
| 609 | */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 610 | static ide_startstop_t idefloppy_transfer_pc(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
| 612 | ide_startstop_t startstop; |
| 613 | idefloppy_floppy_t *floppy = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 614 | u8 ireason; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) { |
| 617 | printk(KERN_ERR "ide-floppy: Strange, packet command " |
| 618 | "initiated yet DRQ isn't asserted\n"); |
| 619 | return startstop; |
| 620 | } |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 621 | ireason = drive->hwif->INB(IDE_IREASON_REG); |
| 622 | if ((ireason & CD) == 0 || (ireason & IO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while " |
| 624 | "issuing a packet command\n"); |
| 625 | return ide_do_reset(drive); |
| 626 | } |
Borislav Petkov | 0078df2 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | /* Set the interrupt routine */ |
| 629 | ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); |
| 630 | /* Send the actual packet */ |
| 631 | HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12); |
| 632 | return ide_started; |
| 633 | } |
| 634 | |
| 635 | |
| 636 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 637 | * What we have here is a classic case of a top half / bottom half interrupt |
| 638 | * service routine. In interrupt mode, the device sends an interrupt to signal |
| 639 | * that it is ready to receive a packet. However, we need to delay about 2-3 |
| 640 | * ticks before issuing the packet or we gets in trouble. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | * |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 642 | * So, follow carefully. transfer_pc1 is called as an interrupt (or directly). |
| 643 | * In either case, when the device says it's ready for a packet, we schedule |
| 644 | * the packet transfer to occur about 2-3 ticks later in transfer_pc2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 646 | static int idefloppy_transfer_pc2(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
| 648 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 649 | |
| 650 | /* Send the actual packet */ |
| 651 | HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12); |
| 652 | /* Timeout for the packet command */ |
| 653 | return IDEFLOPPY_WAIT_CMD; |
| 654 | } |
| 655 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 656 | static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
| 658 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 659 | ide_startstop_t startstop; |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 660 | u8 ireason; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
| 662 | if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) { |
| 663 | printk(KERN_ERR "ide-floppy: Strange, packet command " |
| 664 | "initiated yet DRQ isn't asserted\n"); |
| 665 | return startstop; |
| 666 | } |
Bartlomiej Zolnierkiewicz | 8e7657a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 667 | ireason = drive->hwif->INB(IDE_IREASON_REG); |
| 668 | if ((ireason & CD) == 0 || (ireason & IO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) " |
| 670 | "while issuing a packet command\n"); |
| 671 | return ide_do_reset(drive); |
| 672 | } |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 673 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | * The following delay solves a problem with ATAPI Zip 100 drives |
| 675 | * where the Busy flag was apparently being deasserted before the |
| 676 | * unit was ready to receive data. This was happening on a |
| 677 | * 1200 MHz Athlon system. 10/26/01 25msec is too short, |
| 678 | * 40 and 50msec work well. idefloppy_pc_intr will not be actually |
| 679 | * used until after the packet is moved in about 50 msec. |
| 680 | */ |
Borislav Petkov | 0078df2 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 681 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 682 | ide_set_handler(drive, &idefloppy_pc_intr, floppy->ticks, |
| 683 | &idefloppy_transfer_pc2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return ide_started; |
| 685 | } |
| 686 | |
Borislav Petkov | d652c13 | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 687 | static void ide_floppy_report_error(idefloppy_floppy_t *floppy, |
| 688 | idefloppy_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
Borislav Petkov | d652c13 | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 690 | /* supress error messages resulting from Medium not present */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | if (floppy->sense_key == 0x02 && |
| 692 | floppy->asc == 0x3a && |
| 693 | floppy->ascq == 0x00) |
Borislav Petkov | d652c13 | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 694 | return; |
| 695 | |
| 696 | printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, " |
| 697 | "asc = %2x, ascq = %2x\n", |
| 698 | floppy->drive->name, pc->c[0], floppy->sense_key, |
| 699 | floppy->asc, floppy->ascq); |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 703 | static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive, |
| 704 | idefloppy_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
| 706 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 707 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | ide_handler_t *pkt_xfer_routine; |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 709 | u16 bcount; |
Bartlomiej Zolnierkiewicz | e5f9f5a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 710 | u8 dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | if (floppy->failed_pc == NULL && |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 713 | pc->c[0] != GPCMD_REQUEST_SENSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | floppy->failed_pc = pc; |
| 715 | /* Set the current packet command */ |
| 716 | floppy->pc = pc; |
| 717 | |
Borislav Petkov | 757ced8 | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 718 | if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) { |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 719 | if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR)) |
Borislav Petkov | 757ced8 | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 720 | ide_floppy_report_error(floppy, pc); |
| 721 | /* Giving up */ |
| 722 | pc->error = IDEFLOPPY_ERROR_GENERAL; |
| 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | floppy->failed_pc = NULL; |
| 725 | pc->callback(drive); |
| 726 | return ide_stopped; |
| 727 | } |
| 728 | |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 729 | debug_log("Retry number - %d\n", pc->retries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
| 731 | pc->retries++; |
| 732 | /* We haven't transferred any data yet */ |
| 733 | pc->actually_transferred = 0; |
| 734 | pc->current_position = pc->buffer; |
Bartlomiej Zolnierkiewicz | 790d123 | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 735 | bcount = min(pc->request_transfer, 63 * 1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 737 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
| 738 | pc->flags &= ~PC_FLAG_DMA_ERROR; |
Bartlomiej Zolnierkiewicz | 7469aaf | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 739 | ide_dma_off(drive); |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 740 | } |
Bartlomiej Zolnierkiewicz | e5f9f5a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 741 | dma = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 743 | if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma) |
Bartlomiej Zolnierkiewicz | e5f9f5a | 2008-01-25 22:17:12 +0100 | [diff] [blame] | 744 | dma = !hwif->dma_setup(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
Bartlomiej Zolnierkiewicz | 2fc5738 | 2008-01-25 22:17:13 +0100 | [diff] [blame] | 746 | ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK | |
| 747 | IDE_TFLAG_OUT_DEVICE, bcount, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 749 | if (dma) { |
| 750 | /* Begin DMA, if necessary */ |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 751 | pc->flags |= PC_FLAG_DMA_IN_PROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | hwif->dma_start(drive); |
| 753 | } |
| 754 | |
| 755 | /* Can we transfer the packet when we get the interrupt or wait? */ |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 756 | if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | /* wait */ |
| 758 | pkt_xfer_routine = &idefloppy_transfer_pc1; |
| 759 | } else { |
| 760 | /* immediate */ |
| 761 | pkt_xfer_routine = &idefloppy_transfer_pc; |
| 762 | } |
| 763 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 764 | if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | /* Issue the packet command */ |
| 766 | ide_execute_command(drive, WIN_PACKETCMD, |
| 767 | pkt_xfer_routine, |
| 768 | IDEFLOPPY_WAIT_CMD, |
| 769 | NULL); |
| 770 | return ide_started; |
| 771 | } else { |
| 772 | /* Issue the packet command */ |
| 773 | HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG); |
| 774 | return (*pkt_xfer_routine) (drive); |
| 775 | } |
| 776 | } |
| 777 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 778 | static void idefloppy_rw_callback(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 780 | debug_log("Reached %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | idefloppy_do_end_request(drive, 1, 0); |
| 783 | return; |
| 784 | } |
| 785 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 786 | static void idefloppy_create_prevent_cmd(idefloppy_pc_t *pc, int prevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | { |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 788 | debug_log("creating prevent removal command, prevent = %d\n", prevent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
| 790 | idefloppy_init_pc(pc); |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 791 | pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | pc->c[4] = prevent; |
| 793 | } |
| 794 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 795 | static void idefloppy_create_read_capacity_cmd(idefloppy_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
| 797 | idefloppy_init_pc(pc); |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 798 | pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | pc->c[7] = 255; |
| 800 | pc->c[8] = 255; |
| 801 | pc->request_transfer = 255; |
| 802 | } |
| 803 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 804 | static void idefloppy_create_format_unit_cmd(idefloppy_pc_t *pc, int b, int l, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | int flags) |
| 806 | { |
| 807 | idefloppy_init_pc(pc); |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 808 | pc->c[0] = GPCMD_FORMAT_UNIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | pc->c[1] = 0x17; |
| 810 | |
| 811 | memset(pc->buffer, 0, 12); |
| 812 | pc->buffer[1] = 0xA2; |
| 813 | /* Default format list header, u8 1: FOV/DCRT/IMM bits set */ |
| 814 | |
| 815 | if (flags & 1) /* Verify bit on... */ |
| 816 | pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */ |
| 817 | pc->buffer[3] = 8; |
| 818 | |
Borislav Petkov | 8f62243 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 819 | put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4])); |
| 820 | put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8])); |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 821 | pc->buffer_size = 12; |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 822 | pc->flags |= PC_FLAG_WRITING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 825 | /* A mode sense command is used to "sense" floppy parameters. */ |
| 826 | static void idefloppy_create_mode_sense_cmd(idefloppy_pc_t *pc, u8 page_code, |
| 827 | u8 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
Borislav Petkov | 24a5d70 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 829 | u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | idefloppy_init_pc(pc); |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 832 | pc->c[0] = GPCMD_MODE_SENSE_10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | pc->c[1] = 0; |
| 834 | pc->c[2] = page_code + (type << 6); |
| 835 | |
| 836 | switch (page_code) { |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 837 | case IDEFLOPPY_CAPABILITIES_PAGE: |
| 838 | length += 12; |
| 839 | break; |
| 840 | case IDEFLOPPY_FLEXIBLE_DISK_PAGE: |
| 841 | length += 32; |
| 842 | break; |
| 843 | default: |
| 844 | printk(KERN_ERR "ide-floppy: unsupported page code " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | "in create_mode_sense_cmd\n"); |
| 846 | } |
Borislav Petkov | 8f62243 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 847 | put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | pc->request_transfer = length; |
| 849 | } |
| 850 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 851 | static void idefloppy_create_start_stop_cmd(idefloppy_pc_t *pc, int start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | { |
| 853 | idefloppy_init_pc(pc); |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 854 | pc->c[0] = GPCMD_START_STOP_UNIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | pc->c[4] = start; |
| 856 | } |
| 857 | |
| 858 | static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc) |
| 859 | { |
| 860 | idefloppy_init_pc(pc); |
Borislav Petkov | 30d6709 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 861 | pc->c[0] = GPCMD_TEST_UNIT_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Borislav Petkov | ae7e8dd | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 864 | static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy, |
| 865 | idefloppy_pc_t *pc, struct request *rq, |
| 866 | unsigned long sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
| 868 | int block = sector / floppy->bs_factor; |
| 869 | int blocks = rq->nr_sectors / floppy->bs_factor; |
| 870 | int cmd = rq_data_dir(rq); |
| 871 | |
Borislav Petkov | ae7e8dd | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 872 | debug_log("create_rw10_cmd: block == %d, blocks == %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | block, blocks); |
| 874 | |
| 875 | idefloppy_init_pc(pc); |
Borislav Petkov | ae7e8dd | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 876 | pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10; |
| 877 | put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]); |
Borislav Petkov | 8f62243 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 878 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]); |
Borislav Petkov | ae7e8dd | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | pc->callback = &idefloppy_rw_callback; |
| 881 | pc->rq = rq; |
| 882 | pc->b_count = cmd == READ ? 0 : rq->bio->bi_size; |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 883 | if (rq->cmd_flags & REQ_RW) |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 884 | pc->flags |= PC_FLAG_WRITING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | pc->buffer = NULL; |
| 886 | pc->request_transfer = pc->buffer_size = blocks * floppy->block_size; |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 887 | pc->flags |= PC_FLAG_DMA_RECOMMENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 890 | static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, |
| 891 | idefloppy_pc_t *pc, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | idefloppy_init_pc(pc); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 894 | pc->callback = &idefloppy_rw_callback; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | memcpy(pc->c, rq->cmd, sizeof(pc->c)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 896 | pc->rq = rq; |
| 897 | pc->b_count = rq->data_len; |
| 898 | if (rq->data_len && rq_data_dir(rq) == WRITE) |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 899 | pc->flags |= PC_FLAG_WRITING; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 900 | pc->buffer = rq->data; |
| 901 | if (rq->bio) |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 902 | pc->flags |= PC_FLAG_DMA_RECOMMENDED; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 903 | /* |
| 904 | * possibly problematic, doesn't look like ide-floppy correctly |
| 905 | * handled scattered requests if dma fails... |
| 906 | */ |
| 907 | pc->request_transfer = pc->buffer_size = rq->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 910 | static ide_startstop_t idefloppy_do_request(ide_drive_t *drive, |
| 911 | struct request *rq, sector_t block_s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | { |
| 913 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 914 | idefloppy_pc_t *pc; |
| 915 | unsigned long block = (unsigned long)block_s; |
| 916 | |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 917 | debug_log("dev: %s, cmd_type: %x, errors: %d\n", |
Randy Dunlap | 18cddac | 2006-06-25 05:48:56 -0700 | [diff] [blame] | 918 | rq->rq_disk ? rq->rq_disk->disk_name : "?", |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 919 | rq->cmd_type, rq->errors); |
| 920 | debug_log("sector: %ld, nr_sectors: %ld, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | "current_nr_sectors: %d\n", (long)rq->sector, |
| 922 | rq->nr_sectors, rq->current_nr_sectors); |
| 923 | |
| 924 | if (rq->errors >= ERROR_MAX) { |
Borislav Petkov | d652c13 | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 925 | if (floppy->failed_pc) |
| 926 | ide_floppy_report_error(floppy, floppy->failed_pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | else |
| 928 | printk(KERN_ERR "ide-floppy: %s: I/O error\n", |
| 929 | drive->name); |
| 930 | idefloppy_do_end_request(drive, 0, 0); |
| 931 | return ide_stopped; |
| 932 | } |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 933 | if (blk_fs_request(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | if (((long)rq->sector % floppy->bs_factor) || |
| 935 | (rq->nr_sectors % floppy->bs_factor)) { |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 936 | printk(KERN_ERR "%s: unsupported r/w request size\n", |
| 937 | drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | idefloppy_do_end_request(drive, 0, 0); |
| 939 | return ide_stopped; |
| 940 | } |
| 941 | pc = idefloppy_next_pc_storage(drive); |
| 942 | idefloppy_create_rw_cmd(floppy, pc, rq, block); |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 943 | } else if (blk_special_request(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | pc = (idefloppy_pc_t *) rq->buffer; |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 945 | } else if (blk_pc_request(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | pc = idefloppy_next_pc_storage(drive); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 947 | idefloppy_blockpc_cmd(floppy, pc, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } else { |
| 949 | blk_dump_rq_flags(rq, |
| 950 | "ide-floppy: unsupported command in queue"); |
| 951 | idefloppy_do_end_request(drive, 0, 0); |
| 952 | return ide_stopped; |
| 953 | } |
| 954 | |
| 955 | pc->rq = rq; |
| 956 | return idefloppy_issue_pc(drive, pc); |
| 957 | } |
| 958 | |
| 959 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 960 | * Add a special packet command request to the tail of the request queue, |
| 961 | * and wait for it to be serviced. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | */ |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 963 | static int idefloppy_queue_pc_tail(ide_drive_t *drive, idefloppy_pc_t *pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | { |
| 965 | struct ide_floppy_obj *floppy = drive->driver_data; |
| 966 | struct request rq; |
| 967 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 968 | ide_init_drive_cmd(&rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | rq.buffer = (char *) pc; |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 970 | rq.cmd_type = REQ_TYPE_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | rq.rq_disk = floppy->disk; |
| 972 | |
| 973 | return ide_do_drive_cmd(drive, &rq, ide_wait); |
| 974 | } |
| 975 | |
| 976 | /* |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 977 | * Look at the flexible disk page parameters. We ignore the CHS capacity |
| 978 | * parameters and use the LBA parameters instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | */ |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 980 | static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | { |
| 982 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 983 | idefloppy_pc_t pc; |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 984 | u8 *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | int capacity, lba_capacity; |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 986 | u16 transfer_rate, sector_size, cyls, rpm; |
| 987 | u8 heads, sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 989 | idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, |
| 990 | MODE_SENSE_CURRENT); |
| 991 | |
| 992 | if (idefloppy_queue_pc_tail(drive, &pc)) { |
| 993 | printk(KERN_ERR "ide-floppy: Can't get flexible disk page" |
| 994 | " parameters\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | return 1; |
| 996 | } |
Borislav Petkov | 24a5d70 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 997 | floppy->wp = !!(pc.buffer[3] & 0x80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | set_disk_ro(floppy->disk, floppy->wp); |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 999 | page = &pc.buffer[8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1001 | transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]); |
| 1002 | sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]); |
| 1003 | cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]); |
| 1004 | rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]); |
| 1005 | heads = pc.buffer[8 + 4]; |
| 1006 | sectors = pc.buffer[8 + 5]; |
| 1007 | |
| 1008 | capacity = cyls * heads * sectors * sector_size; |
| 1009 | |
| 1010 | if (memcmp(page, &floppy->flexible_disk_page, 32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, " |
| 1012 | "%d sector size, %d rpm\n", |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1013 | drive->name, capacity / 1024, cyls, heads, |
| 1014 | sectors, transfer_rate / 8, sector_size, rpm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1016 | memcpy(&floppy->flexible_disk_page, page, 32); |
| 1017 | drive->bios_cyl = cyls; |
| 1018 | drive->bios_head = heads; |
| 1019 | drive->bios_sect = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | lba_capacity = floppy->blocks * floppy->block_size; |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | if (capacity < lba_capacity) { |
| 1023 | printk(KERN_NOTICE "%s: The disk reports a capacity of %d " |
| 1024 | "bytes, but the drive only handles %d\n", |
| 1025 | drive->name, lba_capacity, capacity); |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1026 | floppy->blocks = floppy->block_size ? |
| 1027 | capacity / floppy->block_size : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
Borislav Petkov | 948391d | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 1032 | static int idefloppy_get_sfrp_bit(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
| 1034 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 1035 | idefloppy_pc_t pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
| 1037 | floppy->srfp = 0; |
| 1038 | idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE, |
| 1039 | MODE_SENSE_CURRENT); |
| 1040 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1041 | pc.flags |= PC_FLAG_SUPPRESS_ERROR; |
Borislav Petkov | 948391d | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 1042 | if (idefloppy_queue_pc_tail(drive, &pc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
Borislav Petkov | 948391d | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 1045 | floppy->srfp = pc.buffer[8 + 2] & 0x40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | return (0); |
| 1047 | } |
| 1048 | |
| 1049 | /* |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1050 | * Determine if a media is present in the floppy drive, and if so, its LBA |
| 1051 | * capacity. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | */ |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1053 | static int ide_floppy_get_capacity(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { |
| 1055 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 1056 | idefloppy_pc_t pc; |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1057 | u8 *cap_desc; |
| 1058 | u8 header_len, desc_cnt; |
| 1059 | int i, rc = 1, blocks, length; |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | drive->bios_cyl = 0; |
| 1062 | drive->bios_head = drive->bios_sect = 0; |
Alan Cox | fdb77da | 2007-02-17 02:40:20 +0100 | [diff] [blame] | 1063 | floppy->blocks = 0; |
| 1064 | floppy->bs_factor = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | set_capacity(floppy->disk, 0); |
| 1066 | |
| 1067 | idefloppy_create_read_capacity_cmd(&pc); |
| 1068 | if (idefloppy_queue_pc_tail(drive, &pc)) { |
| 1069 | printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n"); |
| 1070 | return 1; |
| 1071 | } |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1072 | header_len = pc.buffer[3]; |
| 1073 | cap_desc = &pc.buffer[4]; |
| 1074 | desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1076 | for (i = 0; i < desc_cnt; i++) { |
| 1077 | unsigned int desc_start = 4 + i*8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1079 | blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]); |
| 1080 | length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]); |
| 1081 | |
| 1082 | debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n", |
| 1083 | i, blocks * length / 1024, blocks, length); |
| 1084 | |
| 1085 | if (i) |
| 1086 | continue; |
| 1087 | /* |
| 1088 | * the code below is valid only for the 1st descriptor, ie i=0 |
| 1089 | */ |
| 1090 | |
| 1091 | switch (pc.buffer[desc_start + 4] & 0x03) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | /* Clik! drive returns this instead of CAPACITY_CURRENT */ |
| 1093 | case CAPACITY_UNFORMATTED: |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1094 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1095 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | * If it is not a clik drive, break out |
| 1097 | * (maintains previous driver behaviour) |
| 1098 | */ |
| 1099 | break; |
| 1100 | case CAPACITY_CURRENT: |
| 1101 | /* Normal Zip/LS-120 disks */ |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1102 | if (memcmp(cap_desc, &floppy->cap_desc, 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | printk(KERN_INFO "%s: %dkB, %d blocks, %d " |
| 1104 | "sector size\n", drive->name, |
| 1105 | blocks * length / 1024, blocks, length); |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1106 | memcpy(&floppy->cap_desc, cap_desc, 8); |
| 1107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | if (!length || length % 512) { |
| 1109 | printk(KERN_NOTICE "%s: %d bytes block size " |
| 1110 | "not supported\n", drive->name, length); |
| 1111 | } else { |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1112 | floppy->blocks = blocks; |
| 1113 | floppy->block_size = length; |
| 1114 | floppy->bs_factor = length / 512; |
| 1115 | if (floppy->bs_factor != 1) |
| 1116 | printk(KERN_NOTICE "%s: warning: non " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | "512 bytes block size not " |
| 1118 | "fully supported\n", |
| 1119 | drive->name); |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1120 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | break; |
| 1123 | case CAPACITY_NO_CARTRIDGE: |
| 1124 | /* |
| 1125 | * This is a KERN_ERR so it appears on screen |
| 1126 | * for the user to see |
| 1127 | */ |
| 1128 | printk(KERN_ERR "%s: No disk in drive\n", drive->name); |
| 1129 | break; |
| 1130 | case CAPACITY_INVALID: |
| 1131 | printk(KERN_ERR "%s: Invalid capacity for disk " |
| 1132 | "in drive\n", drive->name); |
| 1133 | break; |
| 1134 | } |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1135 | debug_log("Descriptor 0 Code: %d\n", |
| 1136 | pc.buffer[desc_start + 4] & 0x03); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /* Clik! disk does not support get_flexible_disk_page */ |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1140 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) |
Borislav Petkov | 8e81bbb | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1141 | (void) ide_floppy_get_flexible_disk_page(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
| 1143 | set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor); |
| 1144 | return rc; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1148 | * Obtain the list of formattable capacities. |
| 1149 | * Very similar to ide_floppy_get_capacity, except that we push the capacity |
| 1150 | * descriptors to userland, instead of our own structures. |
| 1151 | * |
| 1152 | * Userland gives us the following structure: |
| 1153 | * |
| 1154 | * struct idefloppy_format_capacities { |
| 1155 | * int nformats; |
| 1156 | * struct { |
| 1157 | * int nblocks; |
| 1158 | * int blocksize; |
| 1159 | * } formats[]; |
| 1160 | * }; |
| 1161 | * |
| 1162 | * userland initializes nformats to the number of allocated formats[] records. |
| 1163 | * On exit we set nformats to the number of records we've actually initialized. |
| 1164 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1166 | static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | { |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1168 | idefloppy_pc_t pc; |
| 1169 | u8 header_len, desc_cnt; |
| 1170 | int i, blocks, length, u_array_size, u_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | int __user *argp; |
| 1172 | |
| 1173 | if (get_user(u_array_size, arg)) |
| 1174 | return (-EFAULT); |
| 1175 | |
| 1176 | if (u_array_size <= 0) |
| 1177 | return (-EINVAL); |
| 1178 | |
| 1179 | idefloppy_create_read_capacity_cmd(&pc); |
| 1180 | if (idefloppy_queue_pc_tail(drive, &pc)) { |
| 1181 | printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n"); |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1182 | return (-EIO); |
| 1183 | } |
| 1184 | header_len = pc.buffer[3]; |
| 1185 | desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
| 1187 | u_index = 0; |
| 1188 | argp = arg + 1; |
| 1189 | |
| 1190 | /* |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1191 | * We always skip the first capacity descriptor. That's the current |
| 1192 | * capacity. We are interested in the remaining descriptors, the |
| 1193 | * formattable capacities. |
| 1194 | */ |
| 1195 | for (i = 1; i < desc_cnt; i++) { |
| 1196 | unsigned int desc_start = 4 + i*8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | if (u_index >= u_array_size) |
| 1199 | break; /* User-supplied buffer too small */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1201 | blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]); |
| 1202 | length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
| 1204 | if (put_user(blocks, argp)) |
| 1205 | return(-EFAULT); |
| 1206 | ++argp; |
| 1207 | |
| 1208 | if (put_user(length, argp)) |
| 1209 | return (-EFAULT); |
| 1210 | ++argp; |
| 1211 | |
| 1212 | ++u_index; |
| 1213 | } |
| 1214 | |
| 1215 | if (put_user(u_index, arg)) |
| 1216 | return (-EFAULT); |
| 1217 | return (0); |
| 1218 | } |
| 1219 | |
| 1220 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1221 | * Get ATAPI_FORMAT_UNIT progress indication. |
| 1222 | * |
| 1223 | * Userland gives a pointer to an int. The int is set to a progress |
| 1224 | * indicator 0-65536, with 65536=100%. |
| 1225 | * |
| 1226 | * If the drive does not support format progress indication, we just check |
| 1227 | * the dsc bit, and return either 0 or 65536. |
| 1228 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
| 1230 | static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg) |
| 1231 | { |
| 1232 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 1233 | idefloppy_pc_t pc; |
| 1234 | int progress_indication = 0x10000; |
| 1235 | |
| 1236 | if (floppy->srfp) { |
| 1237 | idefloppy_create_request_sense_cmd(&pc); |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1238 | if (idefloppy_queue_pc_tail(drive, &pc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | return (-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
| 1241 | if (floppy->sense_key == 2 && |
| 1242 | floppy->asc == 4 && |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1243 | floppy->ascq == 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | progress_indication = floppy->progress_indication; |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1245 | |
| 1246 | /* Else assume format_unit has finished, and we're at 0x10000 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | unsigned long flags; |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1249 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
| 1251 | local_irq_save(flags); |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1252 | stat = drive->hwif->INB(IDE_STATUS_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | local_irq_restore(flags); |
| 1254 | |
Bartlomiej Zolnierkiewicz | 22c525b | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 1255 | progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | } |
| 1257 | if (put_user(progress_indication, arg)) |
| 1258 | return (-EFAULT); |
| 1259 | |
| 1260 | return (0); |
| 1261 | } |
| 1262 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1263 | static sector_t idefloppy_capacity(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | { |
| 1265 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 1266 | unsigned long capacity = floppy->blocks * floppy->bs_factor; |
| 1267 | |
| 1268 | return capacity; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
Borislav Petkov | f373bd8 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 1272 | * Check whether we can support a drive, based on the ATAPI IDENTIFY command |
| 1273 | * results. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | */ |
Borislav Petkov | f373bd8 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 1275 | static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | { |
| 1277 | struct idefloppy_id_gcw gcw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
| 1279 | *((u16 *) &gcw) = id->config; |
| 1280 | |
| 1281 | #ifdef CONFIG_PPC |
| 1282 | /* kludge for Apple PowerBook internal zip */ |
| 1283 | if ((gcw.device_type == 5) && |
| 1284 | !strstr(id->model, "CD-ROM") && |
| 1285 | strstr(id->model, "ZIP")) |
Borislav Petkov | f373bd8 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 1286 | gcw.device_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | #endif |
| 1288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if (gcw.protocol != 2) |
Borislav Petkov | f373bd8 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 1290 | printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n", |
| 1291 | gcw.protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | else if (gcw.device_type != 0) |
Borislav Petkov | f373bd8 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 1293 | printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set " |
| 1294 | "to floppy\n", gcw.device_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | else if (!gcw.removable) |
| 1296 | printk(KERN_ERR "ide-floppy: The removable flag is not set\n"); |
| 1297 | else if (gcw.drq_type == 3) { |
Borislav Petkov | f373bd8 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 1298 | printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not " |
| 1299 | "supported\n", gcw.drq_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } else if (gcw.packet_size != 0) { |
Borislav Petkov | f373bd8 | 2008-02-02 19:56:36 +0100 | [diff] [blame] | 1301 | printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 " |
| 1302 | "bytes long\n", gcw.packet_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | } else |
| 1304 | return 1; |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1308 | #ifdef CONFIG_IDE_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | static void idefloppy_add_settings(ide_drive_t *drive) |
| 1310 | { |
| 1311 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 1312 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1313 | ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, |
| 1314 | &drive->bios_cyl, NULL); |
| 1315 | ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, |
| 1316 | &drive->bios_head, NULL); |
| 1317 | ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, |
| 1318 | &drive->bios_sect, NULL); |
| 1319 | ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, |
| 1320 | &floppy->ticks, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | } |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1322 | #else |
| 1323 | static inline void idefloppy_add_settings(ide_drive_t *drive) { ; } |
| 1324 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1326 | static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | { |
| 1328 | struct idefloppy_id_gcw gcw; |
| 1329 | |
| 1330 | *((u16 *) &gcw) = drive->id->config; |
| 1331 | floppy->pc = floppy->pc_stack; |
| 1332 | if (gcw.drq_type == 1) |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1333 | floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1335 | * We used to check revisions here. At this point however I'm giving up. |
| 1336 | * Just assume they are all broken, its easier. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | * |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1338 | * The actual reason for the workarounds was likely a driver bug after |
| 1339 | * all rather than a firmware bug, and the workaround below used to hide |
| 1340 | * it. It should be fixed as of version 1.9, but to be on the safe side |
| 1341 | * we'll leave the limitation below for the 2.2.x tree. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) { |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1344 | floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | /* This value will be visible in the /proc/ide/hdx/settings */ |
| 1346 | floppy->ticks = IDEFLOPPY_TICKS_DELAY; |
| 1347 | blk_queue_max_sectors(drive->queue, 64); |
| 1348 | } |
| 1349 | |
| 1350 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1351 | * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes |
| 1352 | * nasty clicking noises without it, so please don't remove this. |
| 1353 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) { |
| 1355 | blk_queue_max_sectors(drive->queue, 64); |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1356 | floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1359 | (void) ide_floppy_get_capacity(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | idefloppy_add_settings(drive); |
| 1361 | } |
| 1362 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1363 | static void ide_floppy_remove(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | { |
| 1365 | idefloppy_floppy_t *floppy = drive->driver_data; |
| 1366 | struct gendisk *g = floppy->disk; |
| 1367 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1368 | ide_proc_unregister_driver(drive, floppy->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
| 1370 | del_gendisk(g); |
| 1371 | |
| 1372 | ide_floppy_put(floppy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Borislav Petkov | 9a24b63 | 2008-02-02 19:56:33 +0100 | [diff] [blame] | 1375 | static void idefloppy_cleanup_obj(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | { |
| 1377 | struct ide_floppy_obj *floppy = to_ide_floppy(kref); |
| 1378 | ide_drive_t *drive = floppy->drive; |
| 1379 | struct gendisk *g = floppy->disk; |
| 1380 | |
| 1381 | drive->driver_data = NULL; |
| 1382 | g->private_data = NULL; |
| 1383 | put_disk(g); |
| 1384 | kfree(floppy); |
| 1385 | } |
| 1386 | |
Bartlomiej Zolnierkiewicz | ecfd80e | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 1387 | #ifdef CONFIG_IDE_PROC_FS |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1388 | static int proc_idefloppy_read_capacity(char *page, char **start, off_t off, |
| 1389 | int count, int *eof, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | { |
| 1391 | ide_drive_t*drive = (ide_drive_t *)data; |
| 1392 | int len; |
| 1393 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1394 | len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive)); |
| 1395 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | static ide_proc_entry_t idefloppy_proc[] = { |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1399 | { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL }, |
| 1400 | { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | { NULL, 0, NULL, NULL } |
| 1402 | }; |
Bartlomiej Zolnierkiewicz | ecfd80e | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 1403 | #endif /* CONFIG_IDE_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1405 | static int ide_floppy_probe(ide_drive_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | static ide_driver_t idefloppy_driver = { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1408 | .gen_driver = { |
Laurent Riffard | 4ef3b8f | 2005-11-18 22:15:40 +0100 | [diff] [blame] | 1409 | .owner = THIS_MODULE, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1410 | .name = "ide-floppy", |
| 1411 | .bus = &ide_bus_type, |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1412 | }, |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1413 | .probe = ide_floppy_probe, |
| 1414 | .remove = ide_floppy_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | .version = IDEFLOPPY_VERSION, |
| 1416 | .media = ide_floppy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | .supports_dsc_overlap = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | .do_request = idefloppy_do_request, |
| 1419 | .end_request = idefloppy_do_end_request, |
| 1420 | .error = __ide_error, |
| 1421 | .abort = __ide_abort, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1422 | #ifdef CONFIG_IDE_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | .proc = idefloppy_proc, |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1424 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | }; |
| 1426 | |
| 1427 | static int idefloppy_open(struct inode *inode, struct file *filp) |
| 1428 | { |
| 1429 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 1430 | struct ide_floppy_obj *floppy; |
| 1431 | ide_drive_t *drive; |
| 1432 | idefloppy_pc_t pc; |
| 1433 | int ret = 0; |
| 1434 | |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 1435 | debug_log("Reached %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1437 | floppy = ide_floppy_get(disk); |
| 1438 | if (!floppy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | return -ENXIO; |
| 1440 | |
| 1441 | drive = floppy->drive; |
| 1442 | |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 1443 | floppy->openers++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 1445 | if (floppy->openers == 1) { |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1446 | floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | /* Just in case */ |
| 1448 | |
| 1449 | idefloppy_create_test_unit_ready_cmd(&pc); |
| 1450 | if (idefloppy_queue_pc_tail(drive, &pc)) { |
| 1451 | idefloppy_create_start_stop_cmd(&pc, 1); |
| 1452 | (void) idefloppy_queue_pc_tail(drive, &pc); |
| 1453 | } |
| 1454 | |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1455 | if (ide_floppy_get_capacity(drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | && (filp->f_flags & O_NDELAY) == 0 |
| 1457 | /* |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1458 | * Allow O_NDELAY to open a drive without a disk, or with an |
| 1459 | * unreadable disk, so that we can get the format capacity |
| 1460 | * of the drive or begin the format - Sam |
| 1461 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | ret = -EIO; |
| 1464 | goto out_put_floppy; |
| 1465 | } |
| 1466 | |
| 1467 | if (floppy->wp && (filp->f_mode & 2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | ret = -EROFS; |
| 1469 | goto out_put_floppy; |
| 1470 | } |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1471 | floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | /* IOMEGA Clik! drives do not support lock/unlock commands */ |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1473 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | idefloppy_create_prevent_cmd(&pc, 1); |
| 1475 | (void) idefloppy_queue_pc_tail(drive, &pc); |
| 1476 | } |
| 1477 | check_disk_change(inode->i_bdev); |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1478 | } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | ret = -EBUSY; |
| 1480 | goto out_put_floppy; |
| 1481 | } |
| 1482 | return 0; |
| 1483 | |
| 1484 | out_put_floppy: |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 1485 | floppy->openers--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | ide_floppy_put(floppy); |
| 1487 | return ret; |
| 1488 | } |
| 1489 | |
| 1490 | static int idefloppy_release(struct inode *inode, struct file *filp) |
| 1491 | { |
| 1492 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 1493 | struct ide_floppy_obj *floppy = ide_floppy_g(disk); |
| 1494 | ide_drive_t *drive = floppy->drive; |
| 1495 | idefloppy_pc_t pc; |
Borislav Petkov | bcc77d9 | 2008-02-02 19:56:34 +0100 | [diff] [blame] | 1496 | |
| 1497 | debug_log("Reached %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 1499 | if (floppy->openers == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | /* IOMEGA Clik! drives do not support lock/unlock commands */ |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1501 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | idefloppy_create_prevent_cmd(&pc, 0); |
| 1503 | (void) idefloppy_queue_pc_tail(drive, &pc); |
| 1504 | } |
| 1505 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1506 | floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | } |
Bartlomiej Zolnierkiewicz | c94964a | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 1508 | |
| 1509 | floppy->openers--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
| 1511 | ide_floppy_put(floppy); |
| 1512 | |
| 1513 | return 0; |
| 1514 | } |
| 1515 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1516 | static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 1517 | { |
| 1518 | struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk); |
| 1519 | ide_drive_t *drive = floppy->drive; |
| 1520 | |
| 1521 | geo->heads = drive->bios_head; |
| 1522 | geo->sectors = drive->bios_sect; |
| 1523 | geo->cylinders = (u16)drive->bios_cyl; /* truncate */ |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
Borislav Petkov | 20bf7bd | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1527 | static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, |
| 1528 | unsigned long arg, unsigned int cmd) |
| 1529 | { |
| 1530 | if (floppy->openers > 1) |
| 1531 | return -EBUSY; |
| 1532 | |
| 1533 | /* The IOMEGA Clik! Drive doesn't support this command - |
| 1534 | * no room for an eject mechanism */ |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1535 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) { |
Borislav Petkov | 20bf7bd | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1536 | int prevent = arg ? 1 : 0; |
| 1537 | |
| 1538 | if (cmd == CDROMEJECT) |
| 1539 | prevent = 0; |
| 1540 | |
| 1541 | idefloppy_create_prevent_cmd(pc, prevent); |
| 1542 | (void) idefloppy_queue_pc_tail(floppy->drive, pc); |
| 1543 | } |
| 1544 | |
| 1545 | if (cmd == CDROMEJECT) { |
| 1546 | idefloppy_create_start_stop_cmd(pc, 2); |
| 1547 | (void) idefloppy_queue_pc_tail(floppy->drive, pc); |
| 1548 | } |
| 1549 | |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | static int ide_floppy_format_unit(idefloppy_floppy_t *floppy, |
| 1554 | int __user *arg) |
| 1555 | { |
| 1556 | int blocks, length, flags, err = 0; |
| 1557 | idefloppy_pc_t pc; |
| 1558 | |
| 1559 | if (floppy->openers > 1) { |
| 1560 | /* Don't format if someone is using the disk */ |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1561 | floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
Borislav Petkov | 20bf7bd | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1562 | return -EBUSY; |
| 1563 | } |
| 1564 | |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1565 | floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
Borislav Petkov | 20bf7bd | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1566 | |
| 1567 | /* |
| 1568 | * Send ATAPI_FORMAT_UNIT to the drive. |
| 1569 | * |
| 1570 | * Userland gives us the following structure: |
| 1571 | * |
| 1572 | * struct idefloppy_format_command { |
| 1573 | * int nblocks; |
| 1574 | * int blocksize; |
| 1575 | * int flags; |
| 1576 | * } ; |
| 1577 | * |
| 1578 | * flags is a bitmask, currently, the only defined flag is: |
| 1579 | * |
| 1580 | * 0x01 - verify media after format. |
| 1581 | */ |
| 1582 | if (get_user(blocks, arg) || |
| 1583 | get_user(length, arg+1) || |
| 1584 | get_user(flags, arg+2)) { |
| 1585 | err = -EFAULT; |
| 1586 | goto out; |
| 1587 | } |
| 1588 | |
| 1589 | (void) idefloppy_get_sfrp_bit(floppy->drive); |
| 1590 | idefloppy_create_format_unit_cmd(&pc, blocks, length, flags); |
| 1591 | |
| 1592 | if (idefloppy_queue_pc_tail(floppy->drive, &pc)) |
| 1593 | err = -EIO; |
| 1594 | |
| 1595 | out: |
| 1596 | if (err) |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1597 | floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
Borislav Petkov | 20bf7bd | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1598 | return err; |
| 1599 | } |
| 1600 | |
| 1601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | static int idefloppy_ioctl(struct inode *inode, struct file *file, |
| 1603 | unsigned int cmd, unsigned long arg) |
| 1604 | { |
| 1605 | struct block_device *bdev = inode->i_bdev; |
| 1606 | struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk); |
| 1607 | ide_drive_t *drive = floppy->drive; |
Borislav Petkov | 20bf7bd | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1608 | idefloppy_pc_t pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | void __user *argp = (void __user *)arg; |
Ondrej Zary | 07203f6 | 2005-11-10 00:25:15 +0100 | [diff] [blame] | 1610 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | |
| 1612 | switch (cmd) { |
| 1613 | case CDROMEJECT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | /* fall through */ |
| 1615 | case CDROM_LOCKDOOR: |
Borislav Petkov | 20bf7bd | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1616 | return ide_floppy_lockdoor(floppy, &pc, arg, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED: |
| 1618 | return 0; |
| 1619 | case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY: |
Borislav Petkov | 194ec0c | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1620 | return ide_floppy_get_format_capacities(drive, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | case IDEFLOPPY_IOCTL_FORMAT_START: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | if (!(file->f_mode & 2)) |
| 1623 | return -EPERM; |
| 1624 | |
Borislav Petkov | 20bf7bd | 2008-02-02 19:56:35 +0100 | [diff] [blame] | 1625 | return ide_floppy_format_unit(floppy, (int __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS: |
| 1627 | return idefloppy_get_format_progress(drive, argp); |
| 1628 | } |
Bartlomiej Zolnierkiewicz | 89636af | 2007-07-20 01:11:59 +0200 | [diff] [blame] | 1629 | |
| 1630 | /* |
| 1631 | * skip SCSI_IOCTL_SEND_COMMAND (deprecated) |
| 1632 | * and CDROM_SEND_PACKET (legacy) ioctls |
| 1633 | */ |
| 1634 | if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND) |
| 1635 | err = scsi_cmd_ioctl(file, bdev->bd_disk->queue, |
| 1636 | bdev->bd_disk, cmd, argp); |
| 1637 | else |
| 1638 | err = -ENOTTY; |
| 1639 | |
| 1640 | if (err == -ENOTTY) |
| 1641 | err = generic_ide_ioctl(drive, file, bdev, cmd, arg); |
| 1642 | |
| 1643 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | static int idefloppy_media_changed(struct gendisk *disk) |
| 1647 | { |
| 1648 | struct ide_floppy_obj *floppy = ide_floppy_g(disk); |
| 1649 | ide_drive_t *drive = floppy->drive; |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1650 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
| 1652 | /* do not scan partitions twice if this is a removable device */ |
| 1653 | if (drive->attach) { |
| 1654 | drive->attach = 0; |
| 1655 | return 0; |
| 1656 | } |
Borislav Petkov | 6e5fa7b | 2008-02-02 19:56:37 +0100 | [diff] [blame] | 1657 | ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED); |
| 1658 | floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED; |
| 1659 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | static int idefloppy_revalidate_disk(struct gendisk *disk) |
| 1663 | { |
| 1664 | struct ide_floppy_obj *floppy = ide_floppy_g(disk); |
| 1665 | set_capacity(disk, idefloppy_capacity(floppy->drive)); |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | static struct block_device_operations idefloppy_ops = { |
| 1670 | .owner = THIS_MODULE, |
| 1671 | .open = idefloppy_open, |
| 1672 | .release = idefloppy_release, |
| 1673 | .ioctl = idefloppy_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1674 | .getgeo = idefloppy_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | .media_changed = idefloppy_media_changed, |
| 1676 | .revalidate_disk= idefloppy_revalidate_disk |
| 1677 | }; |
| 1678 | |
Russell King | 4031bbe | 2006-01-06 11:41:00 +0000 | [diff] [blame] | 1679 | static int ide_floppy_probe(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | { |
| 1681 | idefloppy_floppy_t *floppy; |
| 1682 | struct gendisk *g; |
| 1683 | |
| 1684 | if (!strstr("ide-floppy", drive->driver_req)) |
| 1685 | goto failed; |
| 1686 | if (!drive->present) |
| 1687 | goto failed; |
| 1688 | if (drive->media != ide_floppy) |
| 1689 | goto failed; |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1690 | if (!idefloppy_identify_device(drive, drive->id)) { |
| 1691 | printk(KERN_ERR "ide-floppy: %s: not supported by this version" |
| 1692 | " of ide-floppy\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | goto failed; |
| 1694 | } |
| 1695 | if (drive->scsi) { |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1696 | printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi" |
| 1697 | " emulation.\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | goto failed; |
| 1699 | } |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1700 | floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL); |
| 1701 | if (!floppy) { |
| 1702 | printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy" |
| 1703 | " structure\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | goto failed; |
| 1705 | } |
| 1706 | |
| 1707 | g = alloc_disk(1 << PARTN_BITS); |
| 1708 | if (!g) |
| 1709 | goto out_free_floppy; |
| 1710 | |
| 1711 | ide_init_disk(g, drive); |
| 1712 | |
Bartlomiej Zolnierkiewicz | 7662d04 | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 1713 | ide_proc_register_driver(drive, &idefloppy_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | kref_init(&floppy->kref); |
| 1716 | |
| 1717 | floppy->drive = drive; |
| 1718 | floppy->driver = &idefloppy_driver; |
| 1719 | floppy->disk = g; |
| 1720 | |
| 1721 | g->private_data = &floppy->driver; |
| 1722 | |
| 1723 | drive->driver_data = floppy; |
| 1724 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1725 | idefloppy_setup(drive, floppy); |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | g->minors = 1 << PARTN_BITS; |
| 1728 | g->driverfs_dev = &drive->gendev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0; |
| 1730 | g->fops = &idefloppy_ops; |
| 1731 | drive->attach = 1; |
| 1732 | add_disk(g); |
| 1733 | return 0; |
| 1734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | out_free_floppy: |
| 1736 | kfree(floppy); |
| 1737 | failed: |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1738 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1741 | static void __exit idefloppy_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | { |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1743 | driver_unregister(&idefloppy_driver.gen_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Bartlomiej Zolnierkiewicz | 17514e8 | 2005-11-19 22:24:35 +0100 | [diff] [blame] | 1746 | static int __init idefloppy_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | { |
| 1748 | printk("ide-floppy driver " IDEFLOPPY_VERSION "\n"); |
Bartlomiej Zolnierkiewicz | 8604aff | 2005-05-26 14:55:34 +0200 | [diff] [blame] | 1749 | return driver_register(&idefloppy_driver.gen_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
Kay Sievers | 263756e | 2005-12-12 18:03:44 +0100 | [diff] [blame] | 1752 | MODULE_ALIAS("ide:*m-floppy*"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | module_init(idefloppy_init); |
| 1754 | module_exit(idefloppy_exit); |
| 1755 | MODULE_LICENSE("GPL"); |
Borislav Petkov | 0571c7a | 2008-02-02 19:56:38 +0100 | [diff] [blame^] | 1756 | MODULE_DESCRIPTION("ATAPI FLOPPY Driver"); |
| 1757 | |