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