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