]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
d3f20848 BP |
2 | * IDE ATAPI floppy driver. |
3 | * | |
59bca8cc BZ |
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 | |
0571c7a4 | 7 | * |
1da177e4 LT |
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 | * | |
d3f20848 BP |
14 | * For a historical changelog see |
15 | * Documentation/ide/ChangeLog.ide-floppy.1996-2002 | |
1da177e4 LT |
16 | */ |
17 | ||
fc6c5bc7 | 18 | #define IDEFLOPPY_VERSION "1.00" |
1da177e4 | 19 | |
1da177e4 LT |
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> | |
cf8b8975 | 35 | #include <linux/mutex.h> |
1da177e4 | 36 | |
89636af2 BZ |
37 | #include <scsi/scsi_ioctl.h> |
38 | ||
1da177e4 | 39 | #include <asm/byteorder.h> |
7e8b163b BP |
40 | #include <linux/irq.h> |
41 | #include <linux/uaccess.h> | |
42 | #include <linux/io.h> | |
1da177e4 LT |
43 | #include <asm/unaligned.h> |
44 | ||
f373bd82 | 45 | /* define to see debug info */ |
1da177e4 | 46 | #define IDEFLOPPY_DEBUG_LOG 0 |
1da177e4 LT |
47 | |
48 | /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */ | |
0571c7a4 | 49 | #define IDEFLOPPY_DEBUG(fmt, args...) |
1da177e4 LT |
50 | |
51 | #if IDEFLOPPY_DEBUG_LOG | |
bcc77d9c BP |
52 | #define debug_log(fmt, args...) \ |
53 | printk(KERN_INFO "ide-floppy: " fmt, ## args) | |
1da177e4 | 54 | #else |
0571c7a4 | 55 | #define debug_log(fmt, args...) do {} while (0) |
1da177e4 LT |
56 | #endif |
57 | ||
58 | ||
0571c7a4 | 59 | /* Some drives require a longer irq timeout. */ |
1da177e4 LT |
60 | #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD) |
61 | ||
62 | /* | |
0571c7a4 BP |
63 | * After each failed packet command we issue a request sense command and retry |
64 | * the packet command IDEFLOPPY_MAX_PC_RETRIES times. | |
1da177e4 LT |
65 | */ |
66 | #define IDEFLOPPY_MAX_PC_RETRIES 3 | |
67 | ||
68 | /* | |
0571c7a4 BP |
69 | * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE |
70 | * bytes. | |
1da177e4 LT |
71 | */ |
72 | #define IDEFLOPPY_PC_BUFFER_SIZE 256 | |
73 | ||
74 | /* | |
0571c7a4 BP |
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. | |
1da177e4 LT |
78 | */ |
79 | #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES) | |
80 | ||
1da177e4 LT |
81 | typedef struct idefloppy_packet_command_s { |
82 | u8 c[12]; /* Actual packet bytes */ | |
0571c7a4 BP |
83 | int retries; /* On each retry, we increment |
84 | retries */ | |
1da177e4 LT |
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 */ | |
0571c7a4 BP |
89 | int b_count; /* Missing/Available data on |
90 | the current buffer */ | |
1da177e4 LT |
91 | struct request *rq; /* The corresponding request */ |
92 | u8 *buffer; /* Data buffer */ | |
0571c7a4 BP |
93 | u8 *current_position; /* Pointer into above buffer */ |
94 | void (*callback) (ide_drive_t *); /* Called when this packet | |
95 | command is completed */ | |
1da177e4 | 96 | u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */ |
0571c7a4 BP |
97 | unsigned long flags; /* Status/Action bit flags: long |
98 | for set_bit */ | |
1da177e4 LT |
99 | } idefloppy_pc_t; |
100 | ||
6e5fa7b8 BP |
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 | }; | |
1da177e4 | 114 | |
194ec0c0 | 115 | /* format capacities descriptor codes */ |
1da177e4 LT |
116 | #define CAPACITY_INVALID 0x00 |
117 | #define CAPACITY_UNFORMATTED 0x01 | |
118 | #define CAPACITY_CURRENT 0x02 | |
119 | #define CAPACITY_NO_CARTRIDGE 0x03 | |
120 | ||
121 | /* | |
0571c7a4 BP |
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. | |
1da177e4 LT |
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; | |
c94964a4 | 131 | unsigned int openers; /* protected by BKL for now */ |
1da177e4 LT |
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 | ||
0571c7a4 | 145 | /* Last error information */ |
1da177e4 LT |
146 | u8 sense_key, asc, ascq; |
147 | /* delay this long before sending packet command */ | |
148 | u8 ticks; | |
149 | int progress_indication; | |
150 | ||
0571c7a4 | 151 | /* Device information */ |
1da177e4 LT |
152 | /* Current format */ |
153 | int blocks, block_size, bs_factor; | |
194ec0c0 BP |
154 | /* Last format capacity descriptor */ |
155 | u8 cap_desc[8]; | |
1da177e4 | 156 | /* Copy of the flexible disk page */ |
8e81bbba | 157 | u8 flexible_disk_page[32]; |
1da177e4 LT |
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 | ||
c40d3d38 | 166 | #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */ |
1da177e4 | 167 | |
6e5fa7b8 BP |
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 | }; | |
1da177e4 | 181 | |
0571c7a4 | 182 | /* Defines for the MODE SENSE command */ |
1da177e4 LT |
183 | #define MODE_SENSE_CURRENT 0x00 |
184 | #define MODE_SENSE_CHANGEABLE 0x01 | |
0571c7a4 | 185 | #define MODE_SENSE_DEFAULT 0x02 |
1da177e4 LT |
186 | #define MODE_SENSE_SAVED 0x03 |
187 | ||
0571c7a4 | 188 | /* IOCTLs used in low-level formatting. */ |
1da177e4 LT |
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 | ||
0571c7a4 | 194 | /* Error code returned in rq->errors to the higher part of the driver. */ |
1da177e4 LT |
195 | #define IDEFLOPPY_ERROR_GENERAL 101 |
196 | ||
197 | /* | |
0571c7a4 BP |
198 | * The following is used to format the general configuration word of the |
199 | * ATAPI IDENTIFY DEVICE command. | |
1da177e4 | 200 | */ |
0571c7a4 | 201 | struct idefloppy_id_gcw { |
1da177e4 LT |
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 | ||
1da177e4 | 223 | /* |
948391d1 BP |
224 | * Pages of the SELECT SENSE / MODE SENSE packet commands. |
225 | * See SFF-8070i spec. | |
1da177e4 LT |
226 | */ |
227 | #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b | |
228 | #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05 | |
229 | ||
cf8b8975 | 230 | static DEFINE_MUTEX(idefloppy_ref_mutex); |
1da177e4 LT |
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 | ||
cf8b8975 | 241 | mutex_lock(&idefloppy_ref_mutex); |
1da177e4 LT |
242 | floppy = ide_floppy_g(disk); |
243 | if (floppy) | |
244 | kref_get(&floppy->kref); | |
cf8b8975 | 245 | mutex_unlock(&idefloppy_ref_mutex); |
1da177e4 LT |
246 | return floppy; |
247 | } | |
248 | ||
9a24b63d | 249 | static void idefloppy_cleanup_obj(struct kref *); |
1da177e4 LT |
250 | |
251 | static void ide_floppy_put(struct ide_floppy_obj *floppy) | |
252 | { | |
cf8b8975 | 253 | mutex_lock(&idefloppy_ref_mutex); |
9a24b63d | 254 | kref_put(&floppy->kref, idefloppy_cleanup_obj); |
cf8b8975 | 255 | mutex_unlock(&idefloppy_ref_mutex); |
1da177e4 LT |
256 | } |
257 | ||
258 | /* | |
0571c7a4 BP |
259 | * Too bad. The drive wants to send us data which we are not ready to accept. |
260 | * Just throw it away. | |
1da177e4 | 261 | */ |
0571c7a4 | 262 | static void idefloppy_discard_data(ide_drive_t *drive, unsigned int bcount) |
1da177e4 LT |
263 | { |
264 | while (bcount--) | |
265 | (void) HWIF(drive)->INB(IDE_DATA_REG); | |
266 | } | |
267 | ||
0bf399e6 | 268 | static void idefloppy_write_zeros(ide_drive_t *drive, unsigned int bcount) |
1da177e4 LT |
269 | { |
270 | while (bcount--) | |
271 | HWIF(drive)->OUTB(0, IDE_DATA_REG); | |
272 | } | |
1da177e4 LT |
273 | |
274 | ||
275 | /* | |
0571c7a4 BP |
276 | * Used to finish servicing a request. For read/write requests, we will call |
277 | * ide_end_request to pass to the next buffer. | |
1da177e4 LT |
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 | ||
bcc77d9c | 285 | debug_log("Reached %s\n", __func__); |
1da177e4 LT |
286 | |
287 | switch (uptodate) { | |
0571c7a4 BP |
288 | case 0: error = IDEFLOPPY_ERROR_GENERAL; break; |
289 | case 1: error = 0; break; | |
290 | default: error = uptodate; | |
1da177e4 LT |
291 | } |
292 | if (error) | |
293 | floppy->failed_pc = NULL; | |
294 | /* Why does this happen? */ | |
295 | if (!rq) | |
296 | return 0; | |
4aff5e23 | 297 | if (!blk_special_request(rq)) { |
1da177e4 LT |
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 | ||
32925e1a BP |
308 | static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc, |
309 | unsigned int bcount, int direction) | |
1da177e4 LT |
310 | { |
311 | struct request *rq = pc->rq; | |
5705f702 | 312 | struct req_iterator iter; |
1da177e4 LT |
313 | struct bio_vec *bvec; |
314 | unsigned long flags; | |
5705f702 | 315 | int count, done = 0; |
1da177e4 LT |
316 | char *data; |
317 | ||
5705f702 | 318 | rq_for_each_segment(bvec, rq, iter) { |
6c92e699 JA |
319 | if (!bcount) |
320 | break; | |
1da177e4 | 321 | |
6c92e699 | 322 | count = min(bvec->bv_len, bcount); |
1da177e4 | 323 | |
6c92e699 | 324 | data = bvec_kmap_irq(bvec, &flags); |
32925e1a BP |
325 | if (direction) |
326 | drive->hwif->atapi_output_bytes(drive, data, count); | |
327 | else | |
328 | drive->hwif->atapi_input_bytes(drive, data, count); | |
6c92e699 | 329 | bvec_kunmap_irq(data, &flags); |
1da177e4 | 330 | |
6c92e699 JA |
331 | bcount -= count; |
332 | pc->b_count += count; | |
333 | done += count; | |
1da177e4 LT |
334 | } |
335 | ||
336 | idefloppy_do_end_request(drive, 1, done >> 9); | |
337 | ||
338 | if (bcount) { | |
32925e1a BP |
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 | ||
1da177e4 LT |
346 | } |
347 | } | |
348 | ||
0571c7a4 | 349 | static void idefloppy_update_buffers(ide_drive_t *drive, idefloppy_pc_t *pc) |
1da177e4 LT |
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 | /* | |
0571c7a4 BP |
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. | |
1da177e4 | 362 | */ |
0571c7a4 BP |
363 | static void idefloppy_queue_pc_head(ide_drive_t *drive, idefloppy_pc_t *pc, |
364 | struct request *rq) | |
1da177e4 LT |
365 | { |
366 | struct ide_floppy_obj *floppy = drive->driver_data; | |
367 | ||
368 | ide_init_drive_cmd(rq); | |
369 | rq->buffer = (char *) pc; | |
4aff5e23 | 370 | rq->cmd_type = REQ_TYPE_SPECIAL; |
1da177e4 LT |
371 | rq->rq_disk = floppy->disk; |
372 | (void) ide_do_drive_cmd(drive, rq, ide_preempt); | |
373 | } | |
374 | ||
0571c7a4 | 375 | static idefloppy_pc_t *idefloppy_next_pc_storage(ide_drive_t *drive) |
1da177e4 LT |
376 | { |
377 | idefloppy_floppy_t *floppy = drive->driver_data; | |
378 | ||
379 | if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK) | |
0571c7a4 | 380 | floppy->pc_stack_index = 0; |
1da177e4 LT |
381 | return (&floppy->pc_stack[floppy->pc_stack_index++]); |
382 | } | |
383 | ||
0571c7a4 | 384 | static struct request *idefloppy_next_rq_storage(ide_drive_t *drive) |
1da177e4 LT |
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 | ||
a6ff2d3b | 393 | static void idefloppy_request_sense_callback(ide_drive_t *drive) |
1da177e4 LT |
394 | { |
395 | idefloppy_floppy_t *floppy = drive->driver_data; | |
a6ff2d3b | 396 | u8 *buf = floppy->pc->buffer; |
1da177e4 | 397 | |
bcc77d9c BP |
398 | debug_log("Reached %s\n", __func__); |
399 | ||
1da177e4 | 400 | if (!floppy->pc->error) { |
a6ff2d3b BP |
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 | ||
1da177e4 LT |
421 | idefloppy_do_end_request(drive, 1, 0); |
422 | } else { | |
a6ff2d3b BP |
423 | printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting" |
424 | " request!\n"); | |
1da177e4 LT |
425 | idefloppy_do_end_request(drive, 0, 0); |
426 | } | |
427 | } | |
428 | ||
0571c7a4 BP |
429 | /* General packet command callback function. */ |
430 | static void idefloppy_pc_callback(ide_drive_t *drive) | |
1da177e4 LT |
431 | { |
432 | idefloppy_floppy_t *floppy = drive->driver_data; | |
bcc77d9c BP |
433 | |
434 | debug_log("Reached %s\n", __func__); | |
1da177e4 LT |
435 | |
436 | idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0); | |
437 | } | |
438 | ||
0571c7a4 | 439 | static void idefloppy_init_pc(idefloppy_pc_t *pc) |
1da177e4 LT |
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 | ||
0571c7a4 | 450 | static void idefloppy_create_request_sense_cmd(idefloppy_pc_t *pc) |
1da177e4 | 451 | { |
30d67099 BP |
452 | idefloppy_init_pc(pc); |
453 | pc->c[0] = GPCMD_REQUEST_SENSE; | |
1da177e4 LT |
454 | pc->c[4] = 255; |
455 | pc->request_transfer = 18; | |
456 | pc->callback = &idefloppy_request_sense_callback; | |
457 | } | |
458 | ||
459 | /* | |
0571c7a4 BP |
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. | |
1da177e4 | 462 | */ |
0571c7a4 | 463 | static void idefloppy_retry_pc(ide_drive_t *drive) |
1da177e4 LT |
464 | { |
465 | idefloppy_pc_t *pc; | |
466 | struct request *rq; | |
1da177e4 | 467 | |
64a57fe4 | 468 | (void)ide_read_error(drive); |
1da177e4 LT |
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 | ||
0eea6458 | 475 | /* The usual interrupt handler called during a packet command. */ |
1da177e4 LT |
476 | static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive) |
477 | { | |
478 | idefloppy_floppy_t *floppy = drive->driver_data; | |
790d1239 | 479 | ide_hwif_t *hwif = drive->hwif; |
1da177e4 LT |
480 | idefloppy_pc_t *pc = floppy->pc; |
481 | struct request *rq = pc->rq; | |
0eea6458 | 482 | xfer_func_t *xferfunc; |
1da177e4 | 483 | unsigned int temp; |
d30a7fba | 484 | int dma_error = 0; |
790d1239 | 485 | u16 bcount; |
8e7657ae | 486 | u8 stat, ireason; |
1da177e4 | 487 | |
bcc77d9c | 488 | debug_log("Reached %s interrupt handler\n", __func__); |
1da177e4 | 489 | |
6e5fa7b8 | 490 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
d30a7fba BP |
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"); | |
6e5fa7b8 | 495 | pc->flags |= PC_FLAG_DMA_ERROR; |
1da177e4 LT |
496 | } else { |
497 | pc->actually_transferred = pc->request_transfer; | |
498 | idefloppy_update_buffers(drive, pc); | |
499 | } | |
bcc77d9c | 500 | debug_log("DMA finished\n"); |
1da177e4 LT |
501 | } |
502 | ||
503 | /* Clear the interrupt */ | |
c47137a9 | 504 | stat = ide_read_status(drive); |
1da177e4 | 505 | |
0571c7a4 BP |
506 | /* No more interrupts */ |
507 | if ((stat & DRQ_STAT) == 0) { | |
bcc77d9c BP |
508 | debug_log("Packet command completed, %d bytes transferred\n", |
509 | pc->actually_transferred); | |
6e5fa7b8 | 510 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
1da177e4 | 511 | |
366c7f55 | 512 | local_irq_enable_in_hardirq(); |
1da177e4 | 513 | |
6e5fa7b8 | 514 | if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) { |
1da177e4 | 515 | /* Error detected */ |
bcc77d9c | 516 | debug_log("%s: I/O error\n", drive->name); |
1da177e4 | 517 | rq->errors++; |
30d67099 | 518 | if (pc->c[0] == GPCMD_REQUEST_SENSE) { |
1da177e4 LT |
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 | ||
6e5fa7b8 BP |
536 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
537 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; | |
1da177e4 LT |
538 | printk(KERN_ERR "ide-floppy: The floppy wants to issue " |
539 | "more interrupts in DMA mode\n"); | |
7469aaf6 | 540 | ide_dma_off(drive); |
1da177e4 LT |
541 | return ide_do_reset(drive); |
542 | } | |
543 | ||
544 | /* Get the number of bytes to transfer */ | |
790d1239 BZ |
545 | bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) | |
546 | hwif->INB(IDE_BCOUNTL_REG); | |
1da177e4 | 547 | /* on this interrupt */ |
8e7657ae | 548 | ireason = hwif->INB(IDE_IREASON_REG); |
1da177e4 | 549 | |
8e7657ae | 550 | if (ireason & CD) { |
0eea6458 | 551 | printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__); |
1da177e4 LT |
552 | return ide_do_reset(drive); |
553 | } | |
6e5fa7b8 | 554 | if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) { |
1da177e4 LT |
555 | /* Hopefully, we will never get here */ |
556 | printk(KERN_ERR "ide-floppy: We wanted to %s, ", | |
8e7657ae | 557 | (ireason & IO) ? "Write" : "Read"); |
1da177e4 | 558 | printk(KERN_ERR "but the floppy wants us to %s !\n", |
8e7657ae | 559 | (ireason & IO) ? "Read" : "Write"); |
1da177e4 LT |
560 | return ide_do_reset(drive); |
561 | } | |
6e5fa7b8 | 562 | if (!(pc->flags & PC_FLAG_WRITING)) { |
1da177e4 | 563 | /* Reading - Check that we have enough space */ |
790d1239 | 564 | temp = pc->actually_transferred + bcount; |
1da177e4 LT |
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"); | |
790d1239 | 570 | idefloppy_discard_data(drive, bcount); |
0078df2e | 571 | |
1da177e4 LT |
572 | ide_set_handler(drive, |
573 | &idefloppy_pc_intr, | |
574 | IDEFLOPPY_WAIT_CMD, | |
575 | NULL); | |
576 | return ide_started; | |
577 | } | |
bcc77d9c BP |
578 | debug_log("The floppy wants to send us more data than" |
579 | " expected - allowing transfer\n"); | |
1da177e4 LT |
580 | } |
581 | } | |
6e5fa7b8 | 582 | if (pc->flags & PC_FLAG_WRITING) |
0eea6458 | 583 | xferfunc = hwif->atapi_output_bytes; |
32925e1a | 584 | else |
0eea6458 | 585 | xferfunc = hwif->atapi_input_bytes; |
0eea6458 BP |
586 | |
587 | if (pc->buffer) | |
588 | xferfunc(drive, pc->current_position, bcount); | |
589 | else | |
32925e1a | 590 | ide_floppy_io_buffers(drive, pc, bcount, |
6e5fa7b8 | 591 | !!(pc->flags & PC_FLAG_WRITING)); |
0eea6458 | 592 | |
1da177e4 | 593 | /* Update the current position */ |
790d1239 BZ |
594 | pc->actually_transferred += bcount; |
595 | pc->current_position += bcount; | |
1da177e4 | 596 | |
32925e1a BP |
597 | /* And set the interrupt handler again */ |
598 | ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); | |
1da177e4 LT |
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 | */ | |
0571c7a4 | 607 | static ide_startstop_t idefloppy_transfer_pc(ide_drive_t *drive) |
1da177e4 LT |
608 | { |
609 | ide_startstop_t startstop; | |
610 | idefloppy_floppy_t *floppy = drive->driver_data; | |
8e7657ae | 611 | u8 ireason; |
1da177e4 LT |
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 | } | |
8e7657ae BZ |
618 | ireason = drive->hwif->INB(IDE_IREASON_REG); |
619 | if ((ireason & CD) == 0 || (ireason & IO)) { | |
1da177e4 LT |
620 | printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while " |
621 | "issuing a packet command\n"); | |
622 | return ide_do_reset(drive); | |
623 | } | |
0078df2e | 624 | |
1da177e4 LT |
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 | /* | |
0571c7a4 BP |
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. | |
1da177e4 | 638 | * |
0571c7a4 BP |
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. | |
1da177e4 | 642 | */ |
0571c7a4 | 643 | static int idefloppy_transfer_pc2(ide_drive_t *drive) |
1da177e4 LT |
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 | ||
0571c7a4 | 653 | static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive) |
1da177e4 LT |
654 | { |
655 | idefloppy_floppy_t *floppy = drive->driver_data; | |
656 | ide_startstop_t startstop; | |
8e7657ae | 657 | u8 ireason; |
1da177e4 LT |
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 | } | |
8e7657ae BZ |
664 | ireason = drive->hwif->INB(IDE_IREASON_REG); |
665 | if ((ireason & CD) == 0 || (ireason & IO)) { | |
1da177e4 LT |
666 | printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) " |
667 | "while issuing a packet command\n"); | |
668 | return ide_do_reset(drive); | |
669 | } | |
0571c7a4 | 670 | /* |
1da177e4 LT |
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 | */ | |
0078df2e | 678 | |
0571c7a4 BP |
679 | ide_set_handler(drive, &idefloppy_pc_intr, floppy->ticks, |
680 | &idefloppy_transfer_pc2); | |
1da177e4 LT |
681 | return ide_started; |
682 | } | |
683 | ||
d652c138 BP |
684 | static void ide_floppy_report_error(idefloppy_floppy_t *floppy, |
685 | idefloppy_pc_t *pc) | |
1da177e4 | 686 | { |
d652c138 | 687 | /* supress error messages resulting from Medium not present */ |
1da177e4 LT |
688 | if (floppy->sense_key == 0x02 && |
689 | floppy->asc == 0x3a && | |
690 | floppy->ascq == 0x00) | |
d652c138 BP |
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 | ||
1da177e4 LT |
698 | } |
699 | ||
0571c7a4 BP |
700 | static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive, |
701 | idefloppy_pc_t *pc) | |
1da177e4 LT |
702 | { |
703 | idefloppy_floppy_t *floppy = drive->driver_data; | |
704 | ide_hwif_t *hwif = drive->hwif; | |
1da177e4 | 705 | ide_handler_t *pkt_xfer_routine; |
790d1239 | 706 | u16 bcount; |
e5f9f5a8 | 707 | u8 dma; |
1da177e4 | 708 | |
1da177e4 | 709 | if (floppy->failed_pc == NULL && |
30d67099 | 710 | pc->c[0] != GPCMD_REQUEST_SENSE) |
1da177e4 LT |
711 | floppy->failed_pc = pc; |
712 | /* Set the current packet command */ | |
713 | floppy->pc = pc; | |
714 | ||
757ced89 | 715 | if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) { |
6e5fa7b8 | 716 | if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR)) |
757ced89 BP |
717 | ide_floppy_report_error(floppy, pc); |
718 | /* Giving up */ | |
719 | pc->error = IDEFLOPPY_ERROR_GENERAL; | |
720 | ||
1da177e4 LT |
721 | floppy->failed_pc = NULL; |
722 | pc->callback(drive); | |
723 | return ide_stopped; | |
724 | } | |
725 | ||
bcc77d9c | 726 | debug_log("Retry number - %d\n", pc->retries); |
1da177e4 LT |
727 | |
728 | pc->retries++; | |
729 | /* We haven't transferred any data yet */ | |
730 | pc->actually_transferred = 0; | |
731 | pc->current_position = pc->buffer; | |
790d1239 | 732 | bcount = min(pc->request_transfer, 63 * 1024); |
1da177e4 | 733 | |
6e5fa7b8 BP |
734 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
735 | pc->flags &= ~PC_FLAG_DMA_ERROR; | |
7469aaf6 | 736 | ide_dma_off(drive); |
6e5fa7b8 | 737 | } |
e5f9f5a8 | 738 | dma = 0; |
1da177e4 | 739 | |
6e5fa7b8 | 740 | if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma) |
e5f9f5a8 | 741 | dma = !hwif->dma_setup(drive); |
1da177e4 | 742 | |
2fc57388 BZ |
743 | ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK | |
744 | IDE_TFLAG_OUT_DEVICE, bcount, dma); | |
1da177e4 | 745 | |
0571c7a4 BP |
746 | if (dma) { |
747 | /* Begin DMA, if necessary */ | |
6e5fa7b8 | 748 | pc->flags |= PC_FLAG_DMA_IN_PROGRESS; |
1da177e4 LT |
749 | hwif->dma_start(drive); |
750 | } | |
751 | ||
752 | /* Can we transfer the packet when we get the interrupt or wait? */ | |
6e5fa7b8 | 753 | if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) { |
1da177e4 LT |
754 | /* wait */ |
755 | pkt_xfer_routine = &idefloppy_transfer_pc1; | |
756 | } else { | |
757 | /* immediate */ | |
758 | pkt_xfer_routine = &idefloppy_transfer_pc; | |
759 | } | |
760 | ||
6e5fa7b8 | 761 | if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) { |
1da177e4 LT |
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 | ||
0571c7a4 | 775 | static void idefloppy_rw_callback(ide_drive_t *drive) |
1da177e4 | 776 | { |
bcc77d9c | 777 | debug_log("Reached %s\n", __func__); |
1da177e4 LT |
778 | |
779 | idefloppy_do_end_request(drive, 1, 0); | |
780 | return; | |
781 | } | |
782 | ||
0571c7a4 | 783 | static void idefloppy_create_prevent_cmd(idefloppy_pc_t *pc, int prevent) |
1da177e4 | 784 | { |
bcc77d9c | 785 | debug_log("creating prevent removal command, prevent = %d\n", prevent); |
1da177e4 LT |
786 | |
787 | idefloppy_init_pc(pc); | |
30d67099 | 788 | pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL; |
1da177e4 LT |
789 | pc->c[4] = prevent; |
790 | } | |
791 | ||
0571c7a4 | 792 | static void idefloppy_create_read_capacity_cmd(idefloppy_pc_t *pc) |
1da177e4 LT |
793 | { |
794 | idefloppy_init_pc(pc); | |
30d67099 | 795 | pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES; |
1da177e4 LT |
796 | pc->c[7] = 255; |
797 | pc->c[8] = 255; | |
798 | pc->request_transfer = 255; | |
799 | } | |
800 | ||
0571c7a4 | 801 | static void idefloppy_create_format_unit_cmd(idefloppy_pc_t *pc, int b, int l, |
1da177e4 LT |
802 | int flags) |
803 | { | |
804 | idefloppy_init_pc(pc); | |
30d67099 | 805 | pc->c[0] = GPCMD_FORMAT_UNIT; |
1da177e4 LT |
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 | ||
8f622430 BP |
816 | put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4])); |
817 | put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8])); | |
0571c7a4 | 818 | pc->buffer_size = 12; |
6e5fa7b8 | 819 | pc->flags |= PC_FLAG_WRITING; |
1da177e4 LT |
820 | } |
821 | ||
0571c7a4 BP |
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) | |
1da177e4 | 825 | { |
24a5d703 | 826 | u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */ |
0571c7a4 | 827 | |
1da177e4 | 828 | idefloppy_init_pc(pc); |
30d67099 | 829 | pc->c[0] = GPCMD_MODE_SENSE_10; |
1da177e4 LT |
830 | pc->c[1] = 0; |
831 | pc->c[2] = page_code + (type << 6); | |
832 | ||
833 | switch (page_code) { | |
0571c7a4 BP |
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 " | |
1da177e4 LT |
842 | "in create_mode_sense_cmd\n"); |
843 | } | |
8f622430 | 844 | put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]); |
1da177e4 LT |
845 | pc->request_transfer = length; |
846 | } | |
847 | ||
0571c7a4 | 848 | static void idefloppy_create_start_stop_cmd(idefloppy_pc_t *pc, int start) |
1da177e4 LT |
849 | { |
850 | idefloppy_init_pc(pc); | |
30d67099 | 851 | pc->c[0] = GPCMD_START_STOP_UNIT; |
1da177e4 LT |
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); | |
30d67099 | 858 | pc->c[0] = GPCMD_TEST_UNIT_READY; |
1da177e4 LT |
859 | } |
860 | ||
ae7e8ddc BP |
861 | static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy, |
862 | idefloppy_pc_t *pc, struct request *rq, | |
863 | unsigned long sector) | |
1da177e4 LT |
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 | ||
ae7e8ddc | 869 | debug_log("create_rw10_cmd: block == %d, blocks == %d\n", |
1da177e4 LT |
870 | block, blocks); |
871 | ||
872 | idefloppy_init_pc(pc); | |
ae7e8ddc BP |
873 | pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10; |
874 | put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]); | |
8f622430 | 875 | put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]); |
ae7e8ddc | 876 | |
1da177e4 LT |
877 | pc->callback = &idefloppy_rw_callback; |
878 | pc->rq = rq; | |
879 | pc->b_count = cmd == READ ? 0 : rq->bio->bi_size; | |
4aff5e23 | 880 | if (rq->cmd_flags & REQ_RW) |
6e5fa7b8 | 881 | pc->flags |= PC_FLAG_WRITING; |
1da177e4 LT |
882 | pc->buffer = NULL; |
883 | pc->request_transfer = pc->buffer_size = blocks * floppy->block_size; | |
6e5fa7b8 | 884 | pc->flags |= PC_FLAG_DMA_RECOMMENDED; |
1da177e4 LT |
885 | } |
886 | ||
0571c7a4 BP |
887 | static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, |
888 | idefloppy_pc_t *pc, struct request *rq) | |
1da177e4 | 889 | { |
1da177e4 | 890 | idefloppy_init_pc(pc); |
3d6392cf | 891 | pc->callback = &idefloppy_rw_callback; |
1da177e4 | 892 | memcpy(pc->c, rq->cmd, sizeof(pc->c)); |
3d6392cf JA |
893 | pc->rq = rq; |
894 | pc->b_count = rq->data_len; | |
895 | if (rq->data_len && rq_data_dir(rq) == WRITE) | |
6e5fa7b8 | 896 | pc->flags |= PC_FLAG_WRITING; |
3d6392cf JA |
897 | pc->buffer = rq->data; |
898 | if (rq->bio) | |
6e5fa7b8 | 899 | pc->flags |= PC_FLAG_DMA_RECOMMENDED; |
3d6392cf JA |
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; | |
1da177e4 LT |
905 | } |
906 | ||
0571c7a4 BP |
907 | static ide_startstop_t idefloppy_do_request(ide_drive_t *drive, |
908 | struct request *rq, sector_t block_s) | |
1da177e4 LT |
909 | { |
910 | idefloppy_floppy_t *floppy = drive->driver_data; | |
911 | idefloppy_pc_t *pc; | |
912 | unsigned long block = (unsigned long)block_s; | |
913 | ||
bcc77d9c | 914 | debug_log("dev: %s, cmd_type: %x, errors: %d\n", |
18cddac3 | 915 | rq->rq_disk ? rq->rq_disk->disk_name : "?", |
bcc77d9c BP |
916 | rq->cmd_type, rq->errors); |
917 | debug_log("sector: %ld, nr_sectors: %ld, " | |
1da177e4 LT |
918 | "current_nr_sectors: %d\n", (long)rq->sector, |
919 | rq->nr_sectors, rq->current_nr_sectors); | |
920 | ||
921 | if (rq->errors >= ERROR_MAX) { | |
d652c138 BP |
922 | if (floppy->failed_pc) |
923 | ide_floppy_report_error(floppy, floppy->failed_pc); | |
1da177e4 LT |
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 | } | |
4aff5e23 | 930 | if (blk_fs_request(rq)) { |
1da177e4 LT |
931 | if (((long)rq->sector % floppy->bs_factor) || |
932 | (rq->nr_sectors % floppy->bs_factor)) { | |
0571c7a4 BP |
933 | printk(KERN_ERR "%s: unsupported r/w request size\n", |
934 | drive->name); | |
1da177e4 LT |
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); | |
4aff5e23 | 940 | } else if (blk_special_request(rq)) { |
1da177e4 | 941 | pc = (idefloppy_pc_t *) rq->buffer; |
4aff5e23 | 942 | } else if (blk_pc_request(rq)) { |
1da177e4 | 943 | pc = idefloppy_next_pc_storage(drive); |
3d6392cf | 944 | idefloppy_blockpc_cmd(floppy, pc, rq); |
1da177e4 LT |
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 | /* | |
0571c7a4 BP |
957 | * Add a special packet command request to the tail of the request queue, |
958 | * and wait for it to be serviced. | |
1da177e4 | 959 | */ |
0571c7a4 | 960 | static int idefloppy_queue_pc_tail(ide_drive_t *drive, idefloppy_pc_t *pc) |
1da177e4 LT |
961 | { |
962 | struct ide_floppy_obj *floppy = drive->driver_data; | |
963 | struct request rq; | |
964 | ||
0571c7a4 | 965 | ide_init_drive_cmd(&rq); |
1da177e4 | 966 | rq.buffer = (char *) pc; |
4aff5e23 | 967 | rq.cmd_type = REQ_TYPE_SPECIAL; |
1da177e4 LT |
968 | rq.rq_disk = floppy->disk; |
969 | ||
970 | return ide_do_drive_cmd(drive, &rq, ide_wait); | |
971 | } | |
972 | ||
973 | /* | |
8e81bbba BP |
974 | * Look at the flexible disk page parameters. We ignore the CHS capacity |
975 | * parameters and use the LBA parameters instead. | |
1da177e4 | 976 | */ |
8e81bbba | 977 | static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive) |
1da177e4 LT |
978 | { |
979 | idefloppy_floppy_t *floppy = drive->driver_data; | |
980 | idefloppy_pc_t pc; | |
8e81bbba | 981 | u8 *page; |
1da177e4 | 982 | int capacity, lba_capacity; |
8e81bbba BP |
983 | u16 transfer_rate, sector_size, cyls, rpm; |
984 | u8 heads, sectors; | |
1da177e4 | 985 | |
8e81bbba BP |
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"); | |
1da177e4 LT |
992 | return 1; |
993 | } | |
24a5d703 | 994 | floppy->wp = !!(pc.buffer[3] & 0x80); |
1da177e4 | 995 | set_disk_ro(floppy->disk, floppy->wp); |
8e81bbba BP |
996 | page = &pc.buffer[8]; |
997 | ||
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)) | |
1da177e4 LT |
1008 | printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, " |
1009 | "%d sector size, %d rpm\n", | |
8e81bbba BP |
1010 | drive->name, capacity / 1024, cyls, heads, |
1011 | sectors, transfer_rate / 8, sector_size, rpm); | |
1012 | ||
1013 | memcpy(&floppy->flexible_disk_page, page, 32); | |
1014 | drive->bios_cyl = cyls; | |
1015 | drive->bios_head = heads; | |
1016 | drive->bios_sect = sectors; | |
1da177e4 | 1017 | lba_capacity = floppy->blocks * floppy->block_size; |
8e81bbba | 1018 | |
1da177e4 LT |
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); | |
8e81bbba BP |
1023 | floppy->blocks = floppy->block_size ? |
1024 | capacity / floppy->block_size : 0; | |
1da177e4 LT |
1025 | } |
1026 | return 0; | |
1027 | } | |
1028 | ||
948391d1 | 1029 | static int idefloppy_get_sfrp_bit(ide_drive_t *drive) |
1da177e4 LT |
1030 | { |
1031 | idefloppy_floppy_t *floppy = drive->driver_data; | |
1032 | idefloppy_pc_t pc; | |
1da177e4 LT |
1033 | |
1034 | floppy->srfp = 0; | |
1035 | idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE, | |
1036 | MODE_SENSE_CURRENT); | |
1037 | ||
6e5fa7b8 | 1038 | pc.flags |= PC_FLAG_SUPPRESS_ERROR; |
948391d1 | 1039 | if (idefloppy_queue_pc_tail(drive, &pc)) |
1da177e4 | 1040 | return 1; |
1da177e4 | 1041 | |
948391d1 | 1042 | floppy->srfp = pc.buffer[8 + 2] & 0x40; |
1da177e4 LT |
1043 | return (0); |
1044 | } | |
1045 | ||
1046 | /* | |
194ec0c0 BP |
1047 | * Determine if a media is present in the floppy drive, and if so, its LBA |
1048 | * capacity. | |
1da177e4 | 1049 | */ |
194ec0c0 | 1050 | static int ide_floppy_get_capacity(ide_drive_t *drive) |
1da177e4 LT |
1051 | { |
1052 | idefloppy_floppy_t *floppy = drive->driver_data; | |
1053 | idefloppy_pc_t pc; | |
194ec0c0 BP |
1054 | u8 *cap_desc; |
1055 | u8 header_len, desc_cnt; | |
1056 | int i, rc = 1, blocks, length; | |
1057 | ||
1da177e4 LT |
1058 | drive->bios_cyl = 0; |
1059 | drive->bios_head = drive->bios_sect = 0; | |
fdb77da4 AC |
1060 | floppy->blocks = 0; |
1061 | floppy->bs_factor = 1; | |
1da177e4 LT |
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 | } | |
194ec0c0 BP |
1069 | header_len = pc.buffer[3]; |
1070 | cap_desc = &pc.buffer[4]; | |
1071 | desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */ | |
1072 | ||
1073 | for (i = 0; i < desc_cnt; i++) { | |
1074 | unsigned int desc_start = 4 + i*8; | |
1075 | ||
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); | |
1da177e4 | 1081 | |
194ec0c0 BP |
1082 | if (i) |
1083 | continue; | |
1084 | /* | |
1085 | * the code below is valid only for the 1st descriptor, ie i=0 | |
1086 | */ | |
1da177e4 | 1087 | |
194ec0c0 | 1088 | switch (pc.buffer[desc_start + 4] & 0x03) { |
1da177e4 LT |
1089 | /* Clik! drive returns this instead of CAPACITY_CURRENT */ |
1090 | case CAPACITY_UNFORMATTED: | |
6e5fa7b8 | 1091 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) |
0571c7a4 | 1092 | /* |
1da177e4 LT |
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 */ | |
194ec0c0 | 1099 | if (memcmp(cap_desc, &floppy->cap_desc, 8)) |
1da177e4 LT |
1100 | printk(KERN_INFO "%s: %dkB, %d blocks, %d " |
1101 | "sector size\n", drive->name, | |
1102 | blocks * length / 1024, blocks, length); | |
194ec0c0 BP |
1103 | memcpy(&floppy->cap_desc, cap_desc, 8); |
1104 | ||
1da177e4 LT |
1105 | if (!length || length % 512) { |
1106 | printk(KERN_NOTICE "%s: %d bytes block size " | |
1107 | "not supported\n", drive->name, length); | |
1108 | } else { | |
194ec0c0 BP |
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 " | |
1da177e4 LT |
1114 | "512 bytes block size not " |
1115 | "fully supported\n", | |
1116 | drive->name); | |
194ec0c0 | 1117 | rc = 0; |
1da177e4 LT |
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 | } | |
194ec0c0 BP |
1132 | debug_log("Descriptor 0 Code: %d\n", |
1133 | pc.buffer[desc_start + 4] & 0x03); | |
1da177e4 LT |
1134 | } |
1135 | ||
1136 | /* Clik! disk does not support get_flexible_disk_page */ | |
6e5fa7b8 | 1137 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) |
8e81bbba | 1138 | (void) ide_floppy_get_flexible_disk_page(drive); |
1da177e4 LT |
1139 | |
1140 | set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor); | |
1141 | return rc; | |
1142 | } | |
1143 | ||
1144 | /* | |
194ec0c0 BP |
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 | */ | |
1da177e4 | 1162 | |
194ec0c0 | 1163 | static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg) |
1da177e4 | 1164 | { |
194ec0c0 BP |
1165 | idefloppy_pc_t pc; |
1166 | u8 header_len, desc_cnt; | |
1167 | int i, blocks, length, u_array_size, u_index; | |
1da177e4 LT |
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"); | |
194ec0c0 BP |
1179 | return (-EIO); |
1180 | } | |
1181 | header_len = pc.buffer[3]; | |
1182 | desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */ | |
1da177e4 LT |
1183 | |
1184 | u_index = 0; | |
1185 | argp = arg + 1; | |
1186 | ||
1187 | /* | |
194ec0c0 BP |
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; | |
1da177e4 | 1194 | |
1da177e4 LT |
1195 | if (u_index >= u_array_size) |
1196 | break; /* User-supplied buffer too small */ | |
1da177e4 | 1197 | |
194ec0c0 BP |
1198 | blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]); |
1199 | length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]); | |
1da177e4 LT |
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 | ||
1da177e4 | 1217 | /* |
0571c7a4 BP |
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 | */ | |
1da177e4 LT |
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); | |
0571c7a4 | 1235 | if (idefloppy_queue_pc_tail(drive, &pc)) |
1da177e4 | 1236 | return (-EIO); |
1da177e4 LT |
1237 | |
1238 | if (floppy->sense_key == 2 && | |
1239 | floppy->asc == 4 && | |
0571c7a4 | 1240 | floppy->ascq == 4) |
1da177e4 | 1241 | progress_indication = floppy->progress_indication; |
0571c7a4 BP |
1242 | |
1243 | /* Else assume format_unit has finished, and we're at 0x10000 */ | |
1da177e4 | 1244 | } else { |
1da177e4 | 1245 | unsigned long flags; |
22c525b9 | 1246 | u8 stat; |
1da177e4 LT |
1247 | |
1248 | local_irq_save(flags); | |
c47137a9 | 1249 | stat = ide_read_status(drive); |
1da177e4 LT |
1250 | local_irq_restore(flags); |
1251 | ||
22c525b9 | 1252 | progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000; |
1da177e4 LT |
1253 | } |
1254 | if (put_user(progress_indication, arg)) | |
1255 | return (-EFAULT); | |
1256 | ||
1257 | return (0); | |
1258 | } | |
1259 | ||
0571c7a4 | 1260 | static sector_t idefloppy_capacity(ide_drive_t *drive) |
1da177e4 LT |
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 | /* | |
f373bd82 BP |
1269 | * Check whether we can support a drive, based on the ATAPI IDENTIFY command |
1270 | * results. | |
1da177e4 | 1271 | */ |
f373bd82 | 1272 | static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id) |
1da177e4 LT |
1273 | { |
1274 | struct idefloppy_id_gcw gcw; | |
1da177e4 LT |
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")) | |
f373bd82 | 1283 | gcw.device_type = 0; |
1da177e4 LT |
1284 | #endif |
1285 | ||
1da177e4 | 1286 | if (gcw.protocol != 2) |
f373bd82 BP |
1287 | printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n", |
1288 | gcw.protocol); | |
1da177e4 | 1289 | else if (gcw.device_type != 0) |
f373bd82 BP |
1290 | printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set " |
1291 | "to floppy\n", gcw.device_type); | |
1da177e4 LT |
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) { | |
f373bd82 BP |
1295 | printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not " |
1296 | "supported\n", gcw.drq_type); | |
1da177e4 | 1297 | } else if (gcw.packet_size != 0) { |
f373bd82 BP |
1298 | printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 " |
1299 | "bytes long\n", gcw.packet_size); | |
1da177e4 LT |
1300 | } else |
1301 | return 1; | |
1302 | return 0; | |
1303 | } | |
1304 | ||
7662d046 | 1305 | #ifdef CONFIG_IDE_PROC_FS |
1da177e4 LT |
1306 | static void idefloppy_add_settings(ide_drive_t *drive) |
1307 | { | |
1308 | idefloppy_floppy_t *floppy = drive->driver_data; | |
1309 | ||
0571c7a4 BP |
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); | |
1da177e4 | 1318 | } |
7662d046 BZ |
1319 | #else |
1320 | static inline void idefloppy_add_settings(ide_drive_t *drive) { ; } | |
1321 | #endif | |
1da177e4 | 1322 | |
0571c7a4 | 1323 | static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy) |
1da177e4 LT |
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) | |
6e5fa7b8 | 1330 | floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT; |
1da177e4 | 1331 | /* |
0571c7a4 BP |
1332 | * We used to check revisions here. At this point however I'm giving up. |
1333 | * Just assume they are all broken, its easier. | |
1da177e4 | 1334 | * |
0571c7a4 BP |
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. | |
1da177e4 | 1339 | */ |
1da177e4 | 1340 | if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) { |
6e5fa7b8 | 1341 | floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE; |
1da177e4 LT |
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 | /* | |
0571c7a4 BP |
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 | */ | |
1da177e4 LT |
1351 | if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) { |
1352 | blk_queue_max_sectors(drive->queue, 64); | |
6e5fa7b8 | 1353 | floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE; |
1da177e4 LT |
1354 | } |
1355 | ||
194ec0c0 | 1356 | (void) ide_floppy_get_capacity(drive); |
1da177e4 LT |
1357 | idefloppy_add_settings(drive); |
1358 | } | |
1359 | ||
4031bbe4 | 1360 | static void ide_floppy_remove(ide_drive_t *drive) |
1da177e4 LT |
1361 | { |
1362 | idefloppy_floppy_t *floppy = drive->driver_data; | |
1363 | struct gendisk *g = floppy->disk; | |
1364 | ||
7662d046 | 1365 | ide_proc_unregister_driver(drive, floppy->driver); |
1da177e4 LT |
1366 | |
1367 | del_gendisk(g); | |
1368 | ||
1369 | ide_floppy_put(floppy); | |
1da177e4 LT |
1370 | } |
1371 | ||
9a24b63d | 1372 | static void idefloppy_cleanup_obj(struct kref *kref) |
1da177e4 LT |
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 | ||
ecfd80e4 | 1384 | #ifdef CONFIG_IDE_PROC_FS |
0571c7a4 BP |
1385 | static int proc_idefloppy_read_capacity(char *page, char **start, off_t off, |
1386 | int count, int *eof, void *data) | |
1da177e4 LT |
1387 | { |
1388 | ide_drive_t*drive = (ide_drive_t *)data; | |
1389 | int len; | |
1390 | ||
0571c7a4 BP |
1391 | len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive)); |
1392 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); | |
1da177e4 LT |
1393 | } |
1394 | ||
1395 | static ide_proc_entry_t idefloppy_proc[] = { | |
0571c7a4 BP |
1396 | { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL }, |
1397 | { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL }, | |
1da177e4 LT |
1398 | { NULL, 0, NULL, NULL } |
1399 | }; | |
ecfd80e4 | 1400 | #endif /* CONFIG_IDE_PROC_FS */ |
1da177e4 | 1401 | |
4031bbe4 | 1402 | static int ide_floppy_probe(ide_drive_t *); |
1da177e4 | 1403 | |
1da177e4 | 1404 | static ide_driver_t idefloppy_driver = { |
8604affd | 1405 | .gen_driver = { |
4ef3b8f4 | 1406 | .owner = THIS_MODULE, |
8604affd BZ |
1407 | .name = "ide-floppy", |
1408 | .bus = &ide_bus_type, | |
8604affd | 1409 | }, |
4031bbe4 RK |
1410 | .probe = ide_floppy_probe, |
1411 | .remove = ide_floppy_remove, | |
1da177e4 LT |
1412 | .version = IDEFLOPPY_VERSION, |
1413 | .media = ide_floppy, | |
1da177e4 | 1414 | .supports_dsc_overlap = 0, |
1da177e4 LT |
1415 | .do_request = idefloppy_do_request, |
1416 | .end_request = idefloppy_do_end_request, | |
1417 | .error = __ide_error, | |
1418 | .abort = __ide_abort, | |
7662d046 | 1419 | #ifdef CONFIG_IDE_PROC_FS |
1da177e4 | 1420 | .proc = idefloppy_proc, |
7662d046 | 1421 | #endif |
1da177e4 LT |
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 | ||
bcc77d9c | 1432 | debug_log("Reached %s\n", __func__); |
1da177e4 | 1433 | |
0571c7a4 BP |
1434 | floppy = ide_floppy_get(disk); |
1435 | if (!floppy) | |
1da177e4 LT |
1436 | return -ENXIO; |
1437 | ||
1438 | drive = floppy->drive; | |
1439 | ||
c94964a4 | 1440 | floppy->openers++; |
1da177e4 | 1441 | |
c94964a4 | 1442 | if (floppy->openers == 1) { |
6e5fa7b8 | 1443 | floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
1da177e4 LT |
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 | ||
194ec0c0 | 1452 | if (ide_floppy_get_capacity(drive) |
1da177e4 LT |
1453 | && (filp->f_flags & O_NDELAY) == 0 |
1454 | /* | |
0571c7a4 BP |
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 | */ | |
1da177e4 | 1459 | ) { |
1da177e4 LT |
1460 | ret = -EIO; |
1461 | goto out_put_floppy; | |
1462 | } | |
1463 | ||
1464 | if (floppy->wp && (filp->f_mode & 2)) { | |
1da177e4 LT |
1465 | ret = -EROFS; |
1466 | goto out_put_floppy; | |
1467 | } | |
6e5fa7b8 | 1468 | floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED; |
1da177e4 | 1469 | /* IOMEGA Clik! drives do not support lock/unlock commands */ |
6e5fa7b8 | 1470 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) { |
1da177e4 LT |
1471 | idefloppy_create_prevent_cmd(&pc, 1); |
1472 | (void) idefloppy_queue_pc_tail(drive, &pc); | |
1473 | } | |
1474 | check_disk_change(inode->i_bdev); | |
6e5fa7b8 | 1475 | } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) { |
1da177e4 LT |
1476 | ret = -EBUSY; |
1477 | goto out_put_floppy; | |
1478 | } | |
1479 | return 0; | |
1480 | ||
1481 | out_put_floppy: | |
c94964a4 | 1482 | floppy->openers--; |
1da177e4 LT |
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; | |
bcc77d9c BP |
1493 | |
1494 | debug_log("Reached %s\n", __func__); | |
1da177e4 | 1495 | |
c94964a4 | 1496 | if (floppy->openers == 1) { |
1da177e4 | 1497 | /* IOMEGA Clik! drives do not support lock/unlock commands */ |
6e5fa7b8 | 1498 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) { |
1da177e4 LT |
1499 | idefloppy_create_prevent_cmd(&pc, 0); |
1500 | (void) idefloppy_queue_pc_tail(drive, &pc); | |
1501 | } | |
1502 | ||
6e5fa7b8 | 1503 | floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
1da177e4 | 1504 | } |
c94964a4 BZ |
1505 | |
1506 | floppy->openers--; | |
1da177e4 LT |
1507 | |
1508 | ide_floppy_put(floppy); | |
1509 | ||
1510 | return 0; | |
1511 | } | |
1512 | ||
a885c8c4 CH |
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 | ||
20bf7bda BP |
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 */ | |
6e5fa7b8 | 1532 | if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) { |
20bf7bda BP |
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 */ | |
6e5fa7b8 | 1558 | floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
20bf7bda BP |
1559 | return -EBUSY; |
1560 | } | |
1561 | ||
6e5fa7b8 | 1562 | floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
20bf7bda BP |
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) | |
6e5fa7b8 | 1594 | floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; |
20bf7bda BP |
1595 | return err; |
1596 | } | |
1597 | ||
1598 | ||
1da177e4 LT |
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; | |
20bf7bda | 1605 | idefloppy_pc_t pc; |
1da177e4 | 1606 | void __user *argp = (void __user *)arg; |
07203f64 | 1607 | int err; |
1da177e4 LT |
1608 | |
1609 | switch (cmd) { | |
1610 | case CDROMEJECT: | |
1da177e4 LT |
1611 | /* fall through */ |
1612 | case CDROM_LOCKDOOR: | |
20bf7bda | 1613 | return ide_floppy_lockdoor(floppy, &pc, arg, cmd); |
1da177e4 LT |
1614 | case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED: |
1615 | return 0; | |
1616 | case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY: | |
194ec0c0 | 1617 | return ide_floppy_get_format_capacities(drive, argp); |
1da177e4 | 1618 | case IDEFLOPPY_IOCTL_FORMAT_START: |
1da177e4 LT |
1619 | if (!(file->f_mode & 2)) |
1620 | return -EPERM; | |
1621 | ||
20bf7bda | 1622 | return ide_floppy_format_unit(floppy, (int __user *)arg); |
1da177e4 LT |
1623 | case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS: |
1624 | return idefloppy_get_format_progress(drive, argp); | |
1625 | } | |
89636af2 BZ |
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; | |
1da177e4 LT |
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; | |
6e5fa7b8 | 1647 | int ret; |
1da177e4 LT |
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 | } | |
6e5fa7b8 BP |
1654 | ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED); |
1655 | floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED; | |
1656 | return ret; | |
1da177e4 LT |
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, | |
a885c8c4 | 1671 | .getgeo = idefloppy_getgeo, |
1da177e4 LT |
1672 | .media_changed = idefloppy_media_changed, |
1673 | .revalidate_disk= idefloppy_revalidate_disk | |
1674 | }; | |
1675 | ||
4031bbe4 | 1676 | static int ide_floppy_probe(ide_drive_t *drive) |
1da177e4 LT |
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; | |
0571c7a4 BP |
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); | |
1da177e4 LT |
1690 | goto failed; |
1691 | } | |
1692 | if (drive->scsi) { | |
0571c7a4 BP |
1693 | printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi" |
1694 | " emulation.\n", drive->name); | |
1da177e4 LT |
1695 | goto failed; |
1696 | } | |
0571c7a4 BP |
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); | |
1da177e4 LT |
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 | ||
7662d046 | 1710 | ide_proc_register_driver(drive, &idefloppy_driver); |
1da177e4 | 1711 | |
1da177e4 LT |
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 | ||
0571c7a4 | 1722 | idefloppy_setup(drive, floppy); |
8604affd | 1723 | |
1da177e4 LT |
1724 | g->minors = 1 << PARTN_BITS; |
1725 | g->driverfs_dev = &drive->gendev; | |
1da177e4 LT |
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 | ||
1da177e4 LT |
1732 | out_free_floppy: |
1733 | kfree(floppy); | |
1734 | failed: | |
8604affd | 1735 | return -ENODEV; |
1da177e4 LT |
1736 | } |
1737 | ||
0571c7a4 | 1738 | static void __exit idefloppy_exit(void) |
1da177e4 | 1739 | { |
8604affd | 1740 | driver_unregister(&idefloppy_driver.gen_driver); |
1da177e4 LT |
1741 | } |
1742 | ||
17514e8a | 1743 | static int __init idefloppy_init(void) |
1da177e4 LT |
1744 | { |
1745 | printk("ide-floppy driver " IDEFLOPPY_VERSION "\n"); | |
8604affd | 1746 | return driver_register(&idefloppy_driver.gen_driver); |
1da177e4 LT |
1747 | } |
1748 | ||
263756ec | 1749 | MODULE_ALIAS("ide:*m-floppy*"); |
1da177e4 LT |
1750 | module_init(idefloppy_init); |
1751 | module_exit(idefloppy_exit); | |
1752 | MODULE_LICENSE("GPL"); | |
0571c7a4 BP |
1753 | MODULE_DESCRIPTION("ATAPI FLOPPY Driver"); |
1754 |