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