]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/ide/ide-floppy.c
ide: add ide_queue_pc_tail() helper
[mirror_ubuntu-hirsute-kernel.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 * 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
51509eec
BZ
18#define DRV_NAME "ide-floppy"
19
fc6c5bc7 20#define IDEFLOPPY_VERSION "1.00"
1da177e4 21
1da177e4
LT
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <linux/major.h>
31#include <linux/errno.h>
32#include <linux/genhd.h>
33#include <linux/slab.h>
34#include <linux/cdrom.h>
35#include <linux/ide.h>
3ceca727 36#include <linux/hdreg.h>
1da177e4 37#include <linux/bitops.h>
cf8b8975 38#include <linux/mutex.h>
b98b3409 39#include <linux/scatterlist.h>
1da177e4 40
89636af2
BZ
41#include <scsi/scsi_ioctl.h>
42
1da177e4 43#include <asm/byteorder.h>
7e8b163b
BP
44#include <linux/irq.h>
45#include <linux/uaccess.h>
46#include <linux/io.h>
1da177e4
LT
47#include <asm/unaligned.h>
48
f373bd82 49/* define to see debug info */
1da177e4 50#define IDEFLOPPY_DEBUG_LOG 0
1da177e4
LT
51
52/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
0571c7a4 53#define IDEFLOPPY_DEBUG(fmt, args...)
1da177e4
LT
54
55#if IDEFLOPPY_DEBUG_LOG
bcc77d9c
BP
56#define debug_log(fmt, args...) \
57 printk(KERN_INFO "ide-floppy: " fmt, ## args)
1da177e4 58#else
0571c7a4 59#define debug_log(fmt, args...) do {} while (0)
1da177e4
LT
60#endif
61
62
0571c7a4 63/* Some drives require a longer irq timeout. */
1da177e4
LT
64#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
65
66/*
0571c7a4
BP
67 * After each failed packet command we issue a request sense command and retry
68 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
1da177e4
LT
69 */
70#define IDEFLOPPY_MAX_PC_RETRIES 3
71
194ec0c0 72/* format capacities descriptor codes */
1da177e4
LT
73#define CAPACITY_INVALID 0x00
74#define CAPACITY_UNFORMATTED 0x01
75#define CAPACITY_CURRENT 0x02
76#define CAPACITY_NO_CARTRIDGE 0x03
77
78/*
0571c7a4
BP
79 * Most of our global data which we need to save even as we leave the driver
80 * due to an interrupt or a timer event is stored in a variable of type
81 * idefloppy_floppy_t, defined below.
1da177e4
LT
82 */
83typedef struct ide_floppy_obj {
84 ide_drive_t *drive;
85 ide_driver_t *driver;
86 struct gendisk *disk;
87 struct kref kref;
c94964a4 88 unsigned int openers; /* protected by BKL for now */
1da177e4
LT
89
90 /* Current packet command */
8e555123 91 struct ide_atapi_pc *pc;
1da177e4 92 /* Last failed packet command */
8e555123 93 struct ide_atapi_pc *failed_pc;
2e8a6f89
BZ
94 /* used for blk_{fs,pc}_request() requests */
95 struct ide_atapi_pc queued_pc;
394a4c21 96
2e8a6f89 97 struct ide_atapi_pc request_sense_pc;
394a4c21 98 struct request request_sense_rq;
1da177e4 99
0571c7a4 100 /* Last error information */
1da177e4
LT
101 u8 sense_key, asc, ascq;
102 /* delay this long before sending packet command */
103 u8 ticks;
104 int progress_indication;
105
0571c7a4 106 /* Device information */
1da177e4
LT
107 /* Current format */
108 int blocks, block_size, bs_factor;
194ec0c0
BP
109 /* Last format capacity descriptor */
110 u8 cap_desc[8];
1da177e4 111 /* Copy of the flexible disk page */
8e81bbba 112 u8 flexible_disk_page[32];
1da177e4
LT
113 /* Write protect */
114 int wp;
115 /* Supports format progress report */
116 int srfp;
1da177e4
LT
117} idefloppy_floppy_t;
118
c40d3d38 119#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
1da177e4 120
0571c7a4 121/* IOCTLs used in low-level formatting. */
1da177e4
LT
122#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
123#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
124#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
125#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
126
0571c7a4 127/* Error code returned in rq->errors to the higher part of the driver. */
1da177e4
LT
128#define IDEFLOPPY_ERROR_GENERAL 101
129
1da177e4 130/*
948391d1
BP
131 * Pages of the SELECT SENSE / MODE SENSE packet commands.
132 * See SFF-8070i spec.
1da177e4
LT
133 */
134#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
135#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
136
cf8b8975 137static DEFINE_MUTEX(idefloppy_ref_mutex);
1da177e4
LT
138
139#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
140
141#define ide_floppy_g(disk) \
142 container_of((disk)->private_data, struct ide_floppy_obj, driver)
143
08da591e
BZ
144static void idefloppy_cleanup_obj(struct kref *);
145
1da177e4
LT
146static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
147{
148 struct ide_floppy_obj *floppy = NULL;
149
cf8b8975 150 mutex_lock(&idefloppy_ref_mutex);
1da177e4 151 floppy = ide_floppy_g(disk);
08da591e 152 if (floppy) {
d3e33ff5 153 if (ide_device_get(floppy->drive))
08da591e 154 floppy = NULL;
d3e33ff5
BZ
155 else
156 kref_get(&floppy->kref);
08da591e 157 }
cf8b8975 158 mutex_unlock(&idefloppy_ref_mutex);
1da177e4
LT
159 return floppy;
160}
161
1da177e4
LT
162static void ide_floppy_put(struct ide_floppy_obj *floppy)
163{
d3e33ff5
BZ
164 ide_drive_t *drive = floppy->drive;
165
cf8b8975 166 mutex_lock(&idefloppy_ref_mutex);
9a24b63d 167 kref_put(&floppy->kref, idefloppy_cleanup_obj);
d3e33ff5 168 ide_device_put(drive);
cf8b8975 169 mutex_unlock(&idefloppy_ref_mutex);
1da177e4
LT
170}
171
1da177e4 172/*
0571c7a4
BP
173 * Used to finish servicing a request. For read/write requests, we will call
174 * ide_end_request to pass to the next buffer.
1da177e4 175 */
c2b2b293 176static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
1da177e4
LT
177{
178 idefloppy_floppy_t *floppy = drive->driver_data;
179 struct request *rq = HWGROUP(drive)->rq;
180 int error;
181
bcc77d9c 182 debug_log("Reached %s\n", __func__);
1da177e4
LT
183
184 switch (uptodate) {
0571c7a4
BP
185 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
186 case 1: error = 0; break;
187 default: error = uptodate;
1da177e4
LT
188 }
189 if (error)
190 floppy->failed_pc = NULL;
191 /* Why does this happen? */
192 if (!rq)
193 return 0;
4aff5e23 194 if (!blk_special_request(rq)) {
1da177e4
LT
195 /* our real local end request function */
196 ide_end_request(drive, uptodate, nsecs);
197 return 0;
198 }
199 rq->errors = error;
200 /* fixme: need to move this local also */
201 ide_end_drive_cmd(drive, 0, 0);
202 return 0;
203}
204
8e555123
BP
205static void idefloppy_update_buffers(ide_drive_t *drive,
206 struct ide_atapi_pc *pc)
1da177e4
LT
207{
208 struct request *rq = pc->rq;
209 struct bio *bio = rq->bio;
210
211 while ((bio = rq->bio) != NULL)
c2b2b293 212 idefloppy_end_request(drive, 1, 0);
1da177e4
LT
213}
214
81f49382 215static void ide_floppy_callback(ide_drive_t *drive)
1da177e4
LT
216{
217 idefloppy_floppy_t *floppy = drive->driver_data;
81f49382
BP
218 struct ide_atapi_pc *pc = floppy->pc;
219 int uptodate = pc->error ? 0 : 1;
1da177e4 220
bcc77d9c
BP
221 debug_log("Reached %s\n", __func__);
222
dd2e9a03
BZ
223 if (floppy->failed_pc == pc)
224 floppy->failed_pc = NULL;
225
81f49382
BP
226 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
227 (pc->rq && blk_pc_request(pc->rq)))
228 uptodate = 1; /* FIXME */
229 else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
230 u8 *buf = floppy->pc->buf;
a6ff2d3b 231
81f49382
BP
232 if (!pc->error) {
233 floppy->sense_key = buf[2] & 0x0F;
234 floppy->asc = buf[12];
235 floppy->ascq = buf[13];
236 floppy->progress_indication = buf[15] & 0x80 ?
237 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
a6ff2d3b 238
81f49382
BP
239 if (floppy->failed_pc)
240 debug_log("pc = %x, ", floppy->failed_pc->c[0]);
bcc77d9c 241
81f49382
BP
242 debug_log("sense key = %x, asc = %x, ascq = %x\n",
243 floppy->sense_key, floppy->asc, floppy->ascq);
244 } else
245 printk(KERN_ERR "Error in REQUEST SENSE itself - "
246 "Aborting request!\n");
247 }
1da177e4 248
81f49382 249 idefloppy_end_request(drive, uptodate, 0);
1da177e4
LT
250}
251
8e555123 252static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
1da177e4 253{
7bf7420a 254 ide_init_pc(pc);
30d67099 255 pc->c[0] = GPCMD_REQUEST_SENSE;
1da177e4 256 pc->c[4] = 255;
8e555123 257 pc->req_xfer = 18;
1da177e4
LT
258}
259
260/*
0571c7a4
BP
261 * Called when an error was detected during the last packet command. We queue a
262 * request sense packet command in the head of the request list.
1da177e4 263 */
0571c7a4 264static void idefloppy_retry_pc(ide_drive_t *drive)
1da177e4 265{
394a4c21
BZ
266 struct ide_floppy_obj *floppy = drive->driver_data;
267 struct request *rq = &floppy->request_sense_rq;
2e8a6f89 268 struct ide_atapi_pc *pc = &floppy->request_sense_pc;
1da177e4 269
64a57fe4 270 (void)ide_read_error(drive);
1da177e4 271 idefloppy_create_request_sense_cmd(pc);
7645c151 272 ide_queue_pc_head(drive, floppy->disk, pc, rq);
1da177e4
LT
273}
274
0eea6458 275/* The usual interrupt handler called during a packet command. */
52d3ccf7 276static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
1da177e4
LT
277{
278 idefloppy_floppy_t *floppy = drive->driver_data;
1da177e4 279
646c0cb6
BZ
280 return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
281 IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
acaa0f5f 282 idefloppy_retry_pc, NULL, ide_io_buffers);
1da177e4
LT
283}
284
1da177e4 285/*
0571c7a4
BP
286 * What we have here is a classic case of a top half / bottom half interrupt
287 * service routine. In interrupt mode, the device sends an interrupt to signal
288 * that it is ready to receive a packet. However, we need to delay about 2-3
289 * ticks before issuing the packet or we gets in trouble.
1da177e4 290 */
cbbc4e81 291static int idefloppy_transfer_pc(ide_drive_t *drive)
1da177e4
LT
292{
293 idefloppy_floppy_t *floppy = drive->driver_data;
294
295 /* Send the actual packet */
374e042c 296 drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
9567b349 297
1da177e4
LT
298 /* Timeout for the packet command */
299 return IDEFLOPPY_WAIT_CMD;
300}
301
cbbc4e81
BP
302
303/*
304 * Called as an interrupt (or directly). When the device says it's ready for a
305 * packet, we schedule the packet transfer to occur about 2-3 ticks later in
306 * transfer_pc.
307 */
308static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
1da177e4
LT
309{
310 idefloppy_floppy_t *floppy = drive->driver_data;
6ffb6641 311 struct ide_atapi_pc *pc = floppy->pc;
0b2eea4c
BZ
312 ide_expiry_t *expiry;
313 unsigned int timeout;
1da177e4 314
0571c7a4 315 /*
1da177e4
LT
316 * The following delay solves a problem with ATAPI Zip 100 drives
317 * where the Busy flag was apparently being deasserted before the
318 * unit was ready to receive data. This was happening on a
319 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
320 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
321 * used until after the packet is moved in about 50 msec.
322 */
ea68d270 323 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
0b2eea4c 324 timeout = floppy->ticks;
cbbc4e81 325 expiry = &idefloppy_transfer_pc;
0b2eea4c
BZ
326 } else {
327 timeout = IDEFLOPPY_WAIT_CMD;
328 expiry = NULL;
329 }
330
594c16d8 331 return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
1da177e4
LT
332}
333
d652c138 334static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
8e555123 335 struct ide_atapi_pc *pc)
1da177e4 336{
d652c138 337 /* supress error messages resulting from Medium not present */
1da177e4
LT
338 if (floppy->sense_key == 0x02 &&
339 floppy->asc == 0x3a &&
340 floppy->ascq == 0x00)
d652c138
BP
341 return;
342
343 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
344 "asc = %2x, ascq = %2x\n",
345 floppy->drive->name, pc->c[0], floppy->sense_key,
346 floppy->asc, floppy->ascq);
347
1da177e4
LT
348}
349
0571c7a4 350static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
8e555123 351 struct ide_atapi_pc *pc)
1da177e4
LT
352{
353 idefloppy_floppy_t *floppy = drive->driver_data;
1da177e4 354
1da177e4 355 if (floppy->failed_pc == NULL &&
30d67099 356 pc->c[0] != GPCMD_REQUEST_SENSE)
1da177e4
LT
357 floppy->failed_pc = pc;
358 /* Set the current packet command */
359 floppy->pc = pc;
360
757ced89 361 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
6e5fa7b8 362 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
757ced89
BP
363 ide_floppy_report_error(floppy, pc);
364 /* Giving up */
365 pc->error = IDEFLOPPY_ERROR_GENERAL;
366
1da177e4 367 floppy->failed_pc = NULL;
2207fa5a 368 drive->pc_callback(drive);
1da177e4
LT
369 return ide_stopped;
370 }
371
bcc77d9c 372 debug_log("Retry number - %d\n", pc->retries);
1da177e4
LT
373
374 pc->retries++;
1da177e4 375
cbbc4e81 376 return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
6bf1641c 377 IDEFLOPPY_WAIT_CMD, NULL);
1da177e4
LT
378}
379
8e555123 380static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
1da177e4 381{
bcc77d9c 382 debug_log("creating prevent removal command, prevent = %d\n", prevent);
1da177e4 383
7bf7420a 384 ide_init_pc(pc);
30d67099 385 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1da177e4
LT
386 pc->c[4] = prevent;
387}
388
8e555123 389static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
1da177e4 390{
7bf7420a 391 ide_init_pc(pc);
30d67099 392 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
1da177e4
LT
393 pc->c[7] = 255;
394 pc->c[8] = 255;
8e555123 395 pc->req_xfer = 255;
1da177e4
LT
396}
397
8e555123
BP
398static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
399 int l, int flags)
1da177e4 400{
7bf7420a 401 ide_init_pc(pc);
30d67099 402 pc->c[0] = GPCMD_FORMAT_UNIT;
1da177e4
LT
403 pc->c[1] = 0x17;
404
8e555123
BP
405 memset(pc->buf, 0, 12);
406 pc->buf[1] = 0xA2;
1da177e4
LT
407 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
408
409 if (flags & 1) /* Verify bit on... */
8e555123
BP
410 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
411 pc->buf[3] = 8;
1da177e4 412
8e555123
BP
413 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
414 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
415 pc->buf_size = 12;
6e5fa7b8 416 pc->flags |= PC_FLAG_WRITING;
1da177e4
LT
417}
418
0571c7a4 419/* A mode sense command is used to "sense" floppy parameters. */
8e555123 420static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
4de4b9e1 421 u8 page_code)
1da177e4 422{
24a5d703 423 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
0571c7a4 424
7bf7420a 425 ide_init_pc(pc);
30d67099 426 pc->c[0] = GPCMD_MODE_SENSE_10;
1da177e4 427 pc->c[1] = 0;
4de4b9e1 428 pc->c[2] = page_code;
1da177e4
LT
429
430 switch (page_code) {
0571c7a4
BP
431 case IDEFLOPPY_CAPABILITIES_PAGE:
432 length += 12;
433 break;
434 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
435 length += 32;
436 break;
437 default:
438 printk(KERN_ERR "ide-floppy: unsupported page code "
1da177e4
LT
439 "in create_mode_sense_cmd\n");
440 }
8f622430 441 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
8e555123 442 pc->req_xfer = length;
1da177e4
LT
443}
444
8e555123 445static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
1da177e4 446{
7bf7420a 447 ide_init_pc(pc);
30d67099 448 pc->c[0] = GPCMD_START_STOP_UNIT;
1da177e4
LT
449 pc->c[4] = start;
450}
451
ae7e8ddc 452static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
8e555123 453 struct ide_atapi_pc *pc, struct request *rq,
ae7e8ddc 454 unsigned long sector)
1da177e4
LT
455{
456 int block = sector / floppy->bs_factor;
457 int blocks = rq->nr_sectors / floppy->bs_factor;
458 int cmd = rq_data_dir(rq);
459
ae7e8ddc 460 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
1da177e4
LT
461 block, blocks);
462
7bf7420a 463 ide_init_pc(pc);
ae7e8ddc
BP
464 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
465 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
8f622430 466 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
ae7e8ddc 467
20cd93be
BP
468 memcpy(rq->cmd, pc->c, 12);
469
1da177e4 470 pc->rq = rq;
b98b3409 471 pc->b_count = 0;
4aff5e23 472 if (rq->cmd_flags & REQ_RW)
6e5fa7b8 473 pc->flags |= PC_FLAG_WRITING;
8e555123
BP
474 pc->buf = NULL;
475 pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
5e331095 476 pc->flags |= PC_FLAG_DMA_OK;
1da177e4
LT
477}
478
0571c7a4 479static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
8e555123 480 struct ide_atapi_pc *pc, struct request *rq)
1da177e4 481{
7bf7420a 482 ide_init_pc(pc);
1da177e4 483 memcpy(pc->c, rq->cmd, sizeof(pc->c));
3d6392cf 484 pc->rq = rq;
b98b3409 485 pc->b_count = 0;
3d6392cf 486 if (rq->data_len && rq_data_dir(rq) == WRITE)
6e5fa7b8 487 pc->flags |= PC_FLAG_WRITING;
8e555123 488 pc->buf = rq->data;
3d6392cf 489 if (rq->bio)
5e331095 490 pc->flags |= PC_FLAG_DMA_OK;
3d6392cf
JA
491 /*
492 * possibly problematic, doesn't look like ide-floppy correctly
493 * handled scattered requests if dma fails...
494 */
8e555123 495 pc->req_xfer = pc->buf_size = rq->data_len;
1da177e4
LT
496}
497
0571c7a4
BP
498static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
499 struct request *rq, sector_t block_s)
1da177e4
LT
500{
501 idefloppy_floppy_t *floppy = drive->driver_data;
b98b3409 502 ide_hwif_t *hwif = drive->hwif;
8e555123 503 struct ide_atapi_pc *pc;
1da177e4
LT
504 unsigned long block = (unsigned long)block_s;
505
b98b3409
BP
506 debug_log("%s: dev: %s, cmd: 0x%x, cmd_type: %x, errors: %d\n",
507 __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
508 rq->cmd[0], rq->cmd_type, rq->errors);
509
510 debug_log("%s: sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
511 __func__, (long)rq->sector, rq->nr_sectors,
512 rq->current_nr_sectors);
1da177e4
LT
513
514 if (rq->errors >= ERROR_MAX) {
d652c138
BP
515 if (floppy->failed_pc)
516 ide_floppy_report_error(floppy, floppy->failed_pc);
1da177e4
LT
517 else
518 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
519 drive->name);
c2b2b293 520 idefloppy_end_request(drive, 0, 0);
1da177e4
LT
521 return ide_stopped;
522 }
4aff5e23 523 if (blk_fs_request(rq)) {
1da177e4
LT
524 if (((long)rq->sector % floppy->bs_factor) ||
525 (rq->nr_sectors % floppy->bs_factor)) {
0571c7a4
BP
526 printk(KERN_ERR "%s: unsupported r/w request size\n",
527 drive->name);
c2b2b293 528 idefloppy_end_request(drive, 0, 0);
1da177e4
LT
529 return ide_stopped;
530 }
2e8a6f89 531 pc = &floppy->queued_pc;
1da177e4 532 idefloppy_create_rw_cmd(floppy, pc, rq, block);
4aff5e23 533 } else if (blk_special_request(rq)) {
8e555123 534 pc = (struct ide_atapi_pc *) rq->buffer;
4aff5e23 535 } else if (blk_pc_request(rq)) {
2e8a6f89 536 pc = &floppy->queued_pc;
3d6392cf 537 idefloppy_blockpc_cmd(floppy, pc, rq);
1da177e4
LT
538 } else {
539 blk_dump_rq_flags(rq,
540 "ide-floppy: unsupported command in queue");
c2b2b293 541 idefloppy_end_request(drive, 0, 0);
1da177e4
LT
542 return ide_stopped;
543 }
544
b98b3409
BP
545 ide_init_sg_cmd(drive, rq);
546 ide_map_sg(drive, rq);
547
548 pc->sg = hwif->sg_table;
549 pc->sg_cnt = hwif->sg_nents;
550
1da177e4 551 pc->rq = rq;
5d41893c 552
1da177e4
LT
553 return idefloppy_issue_pc(drive, pc);
554}
555
1da177e4 556/*
8e81bbba
BP
557 * Look at the flexible disk page parameters. We ignore the CHS capacity
558 * parameters and use the LBA parameters instead.
1da177e4 559 */
8e81bbba 560static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
1da177e4
LT
561{
562 idefloppy_floppy_t *floppy = drive->driver_data;
2ac07d92 563 struct gendisk *disk = floppy->disk;
8e555123 564 struct ide_atapi_pc pc;
8e81bbba 565 u8 *page;
1da177e4 566 int capacity, lba_capacity;
8e81bbba
BP
567 u16 transfer_rate, sector_size, cyls, rpm;
568 u8 heads, sectors;
1da177e4 569
4de4b9e1 570 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
8e81bbba 571
2ac07d92 572 if (ide_queue_pc_tail(drive, disk, &pc)) {
8e81bbba
BP
573 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
574 " parameters\n");
1da177e4
LT
575 return 1;
576 }
8e555123 577 floppy->wp = !!(pc.buf[3] & 0x80);
2ac07d92 578 set_disk_ro(disk, floppy->wp);
8e555123 579 page = &pc.buf[8];
8e81bbba 580
85ae98a3
HH
581 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
582 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
583 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
584 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
8e555123
BP
585 heads = pc.buf[8 + 4];
586 sectors = pc.buf[8 + 5];
8e81bbba
BP
587
588 capacity = cyls * heads * sectors * sector_size;
589
590 if (memcmp(page, &floppy->flexible_disk_page, 32))
1da177e4
LT
591 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
592 "%d sector size, %d rpm\n",
8e81bbba
BP
593 drive->name, capacity / 1024, cyls, heads,
594 sectors, transfer_rate / 8, sector_size, rpm);
595
596 memcpy(&floppy->flexible_disk_page, page, 32);
597 drive->bios_cyl = cyls;
598 drive->bios_head = heads;
599 drive->bios_sect = sectors;
1da177e4 600 lba_capacity = floppy->blocks * floppy->block_size;
8e81bbba 601
1da177e4
LT
602 if (capacity < lba_capacity) {
603 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
604 "bytes, but the drive only handles %d\n",
605 drive->name, lba_capacity, capacity);
8e81bbba
BP
606 floppy->blocks = floppy->block_size ?
607 capacity / floppy->block_size : 0;
1da177e4
LT
608 }
609 return 0;
610}
611
948391d1 612static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
1da177e4
LT
613{
614 idefloppy_floppy_t *floppy = drive->driver_data;
8e555123 615 struct ide_atapi_pc pc;
1da177e4
LT
616
617 floppy->srfp = 0;
1da177e4 618
4de4b9e1 619 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE);
6e5fa7b8 620 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
4de4b9e1 621
2ac07d92 622 if (ide_queue_pc_tail(drive, floppy->disk, &pc))
1da177e4 623 return 1;
1da177e4 624
8e555123 625 floppy->srfp = pc.buf[8 + 2] & 0x40;
e3faa248 626 return 0;
1da177e4
LT
627}
628
629/*
194ec0c0
BP
630 * Determine if a media is present in the floppy drive, and if so, its LBA
631 * capacity.
1da177e4 632 */
194ec0c0 633static int ide_floppy_get_capacity(ide_drive_t *drive)
1da177e4
LT
634{
635 idefloppy_floppy_t *floppy = drive->driver_data;
2ac07d92 636 struct gendisk *disk = floppy->disk;
8e555123 637 struct ide_atapi_pc pc;
194ec0c0
BP
638 u8 *cap_desc;
639 u8 header_len, desc_cnt;
640 int i, rc = 1, blocks, length;
641
1da177e4
LT
642 drive->bios_cyl = 0;
643 drive->bios_head = drive->bios_sect = 0;
fdb77da4
AC
644 floppy->blocks = 0;
645 floppy->bs_factor = 1;
1da177e4
LT
646 set_capacity(floppy->disk, 0);
647
648 idefloppy_create_read_capacity_cmd(&pc);
2ac07d92 649 if (ide_queue_pc_tail(drive, disk, &pc)) {
1da177e4
LT
650 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
651 return 1;
652 }
8e555123
BP
653 header_len = pc.buf[3];
654 cap_desc = &pc.buf[4];
194ec0c0
BP
655 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
656
657 for (i = 0; i < desc_cnt; i++) {
658 unsigned int desc_start = 4 + i*8;
659
85ae98a3
HH
660 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
661 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
194ec0c0
BP
662
663 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
664 i, blocks * length / 1024, blocks, length);
1da177e4 665
194ec0c0
BP
666 if (i)
667 continue;
668 /*
669 * the code below is valid only for the 1st descriptor, ie i=0
670 */
1da177e4 671
8e555123 672 switch (pc.buf[desc_start + 4] & 0x03) {
1da177e4
LT
673 /* Clik! drive returns this instead of CAPACITY_CURRENT */
674 case CAPACITY_UNFORMATTED:
ea68d270 675 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
0571c7a4 676 /*
1da177e4
LT
677 * If it is not a clik drive, break out
678 * (maintains previous driver behaviour)
679 */
680 break;
681 case CAPACITY_CURRENT:
682 /* Normal Zip/LS-120 disks */
194ec0c0 683 if (memcmp(cap_desc, &floppy->cap_desc, 8))
1da177e4
LT
684 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
685 "sector size\n", drive->name,
686 blocks * length / 1024, blocks, length);
194ec0c0
BP
687 memcpy(&floppy->cap_desc, cap_desc, 8);
688
1da177e4
LT
689 if (!length || length % 512) {
690 printk(KERN_NOTICE "%s: %d bytes block size "
691 "not supported\n", drive->name, length);
692 } else {
194ec0c0
BP
693 floppy->blocks = blocks;
694 floppy->block_size = length;
695 floppy->bs_factor = length / 512;
696 if (floppy->bs_factor != 1)
697 printk(KERN_NOTICE "%s: warning: non "
1da177e4
LT
698 "512 bytes block size not "
699 "fully supported\n",
700 drive->name);
194ec0c0 701 rc = 0;
1da177e4
LT
702 }
703 break;
704 case CAPACITY_NO_CARTRIDGE:
705 /*
706 * This is a KERN_ERR so it appears on screen
707 * for the user to see
708 */
709 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
710 break;
711 case CAPACITY_INVALID:
712 printk(KERN_ERR "%s: Invalid capacity for disk "
713 "in drive\n", drive->name);
714 break;
715 }
194ec0c0 716 debug_log("Descriptor 0 Code: %d\n",
8e555123 717 pc.buf[desc_start + 4] & 0x03);
1da177e4
LT
718 }
719
720 /* Clik! disk does not support get_flexible_disk_page */
ea68d270 721 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
8e81bbba 722 (void) ide_floppy_get_flexible_disk_page(drive);
1da177e4 723
2ac07d92
BZ
724 set_capacity(disk, floppy->blocks * floppy->bs_factor);
725
1da177e4
LT
726 return rc;
727}
728
729/*
194ec0c0
BP
730 * Obtain the list of formattable capacities.
731 * Very similar to ide_floppy_get_capacity, except that we push the capacity
732 * descriptors to userland, instead of our own structures.
733 *
734 * Userland gives us the following structure:
735 *
736 * struct idefloppy_format_capacities {
737 * int nformats;
738 * struct {
739 * int nblocks;
740 * int blocksize;
741 * } formats[];
742 * };
743 *
744 * userland initializes nformats to the number of allocated formats[] records.
745 * On exit we set nformats to the number of records we've actually initialized.
746 */
1da177e4 747
194ec0c0 748static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1da177e4 749{
2ac07d92 750 struct ide_floppy_obj *floppy = drive->driver_data;
8e555123 751 struct ide_atapi_pc pc;
194ec0c0
BP
752 u8 header_len, desc_cnt;
753 int i, blocks, length, u_array_size, u_index;
1da177e4
LT
754 int __user *argp;
755
756 if (get_user(u_array_size, arg))
e3faa248 757 return -EFAULT;
1da177e4
LT
758
759 if (u_array_size <= 0)
e3faa248 760 return -EINVAL;
1da177e4
LT
761
762 idefloppy_create_read_capacity_cmd(&pc);
2ac07d92 763 if (ide_queue_pc_tail(drive, floppy->disk, &pc)) {
1da177e4 764 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
e3faa248 765 return -EIO;
194ec0c0 766 }
e3faa248 767
8e555123 768 header_len = pc.buf[3];
194ec0c0 769 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1da177e4
LT
770
771 u_index = 0;
772 argp = arg + 1;
773
774 /*
194ec0c0
BP
775 * We always skip the first capacity descriptor. That's the current
776 * capacity. We are interested in the remaining descriptors, the
777 * formattable capacities.
778 */
779 for (i = 1; i < desc_cnt; i++) {
780 unsigned int desc_start = 4 + i*8;
1da177e4 781
1da177e4
LT
782 if (u_index >= u_array_size)
783 break; /* User-supplied buffer too small */
1da177e4 784
85ae98a3
HH
785 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
786 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
1da177e4
LT
787
788 if (put_user(blocks, argp))
e3faa248
BZ
789 return -EFAULT;
790
1da177e4
LT
791 ++argp;
792
793 if (put_user(length, argp))
e3faa248
BZ
794 return -EFAULT;
795
1da177e4
LT
796 ++argp;
797
798 ++u_index;
799 }
800
801 if (put_user(u_index, arg))
e3faa248
BZ
802 return -EFAULT;
803
804 return 0;
1da177e4
LT
805}
806
1da177e4 807/*
0571c7a4
BP
808 * Get ATAPI_FORMAT_UNIT progress indication.
809 *
810 * Userland gives a pointer to an int. The int is set to a progress
811 * indicator 0-65536, with 65536=100%.
812 *
813 * If the drive does not support format progress indication, we just check
814 * the dsc bit, and return either 0 or 65536.
815 */
1da177e4 816
d56c99e2 817static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1da177e4
LT
818{
819 idefloppy_floppy_t *floppy = drive->driver_data;
8e555123 820 struct ide_atapi_pc pc;
1da177e4
LT
821 int progress_indication = 0x10000;
822
823 if (floppy->srfp) {
824 idefloppy_create_request_sense_cmd(&pc);
2ac07d92 825 if (ide_queue_pc_tail(drive, floppy->disk, &pc))
e3faa248 826 return -EIO;
1da177e4
LT
827
828 if (floppy->sense_key == 2 &&
829 floppy->asc == 4 &&
0571c7a4 830 floppy->ascq == 4)
1da177e4 831 progress_indication = floppy->progress_indication;
0571c7a4
BP
832
833 /* Else assume format_unit has finished, and we're at 0x10000 */
1da177e4 834 } else {
b73c7ee2 835 ide_hwif_t *hwif = drive->hwif;
1da177e4 836 unsigned long flags;
22c525b9 837 u8 stat;
1da177e4
LT
838
839 local_irq_save(flags);
374e042c 840 stat = hwif->tp_ops->read_status(hwif);
1da177e4
LT
841 local_irq_restore(flags);
842
3a7d2484 843 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
1da177e4 844 }
e3faa248 845
1da177e4 846 if (put_user(progress_indication, arg))
e3faa248 847 return -EFAULT;
1da177e4 848
e3faa248 849 return 0;
1da177e4
LT
850}
851
0571c7a4 852static sector_t idefloppy_capacity(ide_drive_t *drive)
1da177e4
LT
853{
854 idefloppy_floppy_t *floppy = drive->driver_data;
855 unsigned long capacity = floppy->blocks * floppy->bs_factor;
856
857 return capacity;
858}
859
7662d046 860#ifdef CONFIG_IDE_PROC_FS
8185d5aa
BZ
861ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
862ide_devset_rw(bios_head, 0, 255, bios_head);
863ide_devset_rw(bios_sect, 0, 63, bios_sect);
864
865static int get_ticks(ide_drive_t *drive)
1da177e4
LT
866{
867 idefloppy_floppy_t *floppy = drive->driver_data;
8185d5aa
BZ
868 return floppy->ticks;
869}
1da177e4 870
8185d5aa
BZ
871static int set_ticks(ide_drive_t *drive, int arg)
872{
873 idefloppy_floppy_t *floppy = drive->driver_data;
874 floppy->ticks = arg;
875 return 0;
1da177e4 876}
8185d5aa
BZ
877
878IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
879
880static const struct ide_devset *idefloppy_settings[] = {
881 &ide_devset_bios_cyl,
882 &ide_devset_bios_head,
883 &ide_devset_bios_sect,
884 &ide_devset_ticks,
885 NULL
886};
7662d046 887#endif
1da177e4 888
0571c7a4 889static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1da177e4 890{
4dde4492 891 u16 *id = drive->id;
3ad6776c 892 u8 gcw[2];
1da177e4 893
4dde4492 894 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
2e8a6f89 895
2207fa5a 896 drive->pc_callback = ide_floppy_callback;
3ad6776c
BP
897
898 if (((gcw[0] & 0x60) >> 5) == 1)
ea68d270 899 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
1da177e4 900 /*
0571c7a4
BP
901 * We used to check revisions here. At this point however I'm giving up.
902 * Just assume they are all broken, its easier.
1da177e4 903 *
0571c7a4
BP
904 * The actual reason for the workarounds was likely a driver bug after
905 * all rather than a firmware bug, and the workaround below used to hide
906 * it. It should be fixed as of version 1.9, but to be on the safe side
907 * we'll leave the limitation below for the 2.2.x tree.
1da177e4 908 */
4dde4492 909 if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
ea68d270 910 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
1da177e4
LT
911 /* This value will be visible in the /proc/ide/hdx/settings */
912 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
913 blk_queue_max_sectors(drive->queue, 64);
914 }
915
916 /*
0571c7a4
BP
917 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
918 * nasty clicking noises without it, so please don't remove this.
919 */
4dde4492 920 if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
1da177e4 921 blk_queue_max_sectors(drive->queue, 64);
ea68d270 922 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
1da177e4
LT
923 }
924
194ec0c0 925 (void) ide_floppy_get_capacity(drive);
1e874f44
BZ
926
927 ide_proc_register_driver(drive, floppy->driver);
1da177e4
LT
928}
929
4031bbe4 930static void ide_floppy_remove(ide_drive_t *drive)
1da177e4
LT
931{
932 idefloppy_floppy_t *floppy = drive->driver_data;
933 struct gendisk *g = floppy->disk;
934
7662d046 935 ide_proc_unregister_driver(drive, floppy->driver);
1da177e4
LT
936
937 del_gendisk(g);
938
939 ide_floppy_put(floppy);
1da177e4
LT
940}
941
9a24b63d 942static void idefloppy_cleanup_obj(struct kref *kref)
1da177e4
LT
943{
944 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
945 ide_drive_t *drive = floppy->drive;
946 struct gendisk *g = floppy->disk;
947
948 drive->driver_data = NULL;
949 g->private_data = NULL;
950 put_disk(g);
951 kfree(floppy);
952}
953
ecfd80e4 954#ifdef CONFIG_IDE_PROC_FS
0571c7a4
BP
955static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
956 int count, int *eof, void *data)
1da177e4
LT
957{
958 ide_drive_t*drive = (ide_drive_t *)data;
959 int len;
960
0571c7a4
BP
961 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
962 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1da177e4
LT
963}
964
965static ide_proc_entry_t idefloppy_proc[] = {
0571c7a4
BP
966 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
967 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1da177e4
LT
968 { NULL, 0, NULL, NULL }
969};
ecfd80e4 970#endif /* CONFIG_IDE_PROC_FS */
1da177e4 971
4031bbe4 972static int ide_floppy_probe(ide_drive_t *);
1da177e4 973
1da177e4 974static ide_driver_t idefloppy_driver = {
8604affd 975 .gen_driver = {
4ef3b8f4 976 .owner = THIS_MODULE,
8604affd
BZ
977 .name = "ide-floppy",
978 .bus = &ide_bus_type,
8604affd 979 },
4031bbe4
RK
980 .probe = ide_floppy_probe,
981 .remove = ide_floppy_remove,
1da177e4
LT
982 .version = IDEFLOPPY_VERSION,
983 .media = ide_floppy,
1da177e4 984 .do_request = idefloppy_do_request,
c2b2b293 985 .end_request = idefloppy_end_request,
1da177e4 986 .error = __ide_error,
7662d046 987#ifdef CONFIG_IDE_PROC_FS
1da177e4 988 .proc = idefloppy_proc,
8185d5aa 989 .settings = idefloppy_settings,
7662d046 990#endif
1da177e4
LT
991};
992
e996fc8a
BZ
993static void ide_floppy_set_media_lock(ide_drive_t *drive, int on)
994{
2ac07d92 995 struct ide_floppy_obj *floppy = drive->driver_data;
e996fc8a
BZ
996 struct ide_atapi_pc pc;
997
998 /* IOMEGA Clik! drives do not support lock/unlock commands */
999 if ((drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE) == 0) {
1000 idefloppy_create_prevent_cmd(&pc, on);
2ac07d92 1001 (void)ide_queue_pc_tail(drive, floppy->disk, &pc);
e996fc8a
BZ
1002 }
1003}
1004
1da177e4
LT
1005static int idefloppy_open(struct inode *inode, struct file *filp)
1006{
1007 struct gendisk *disk = inode->i_bdev->bd_disk;
1008 struct ide_floppy_obj *floppy;
1009 ide_drive_t *drive;
8e555123 1010 struct ide_atapi_pc pc;
1da177e4
LT
1011 int ret = 0;
1012
bcc77d9c 1013 debug_log("Reached %s\n", __func__);
1da177e4 1014
0571c7a4
BP
1015 floppy = ide_floppy_get(disk);
1016 if (!floppy)
1da177e4
LT
1017 return -ENXIO;
1018
1019 drive = floppy->drive;
1020
c94964a4 1021 floppy->openers++;
1da177e4 1022
c94964a4 1023 if (floppy->openers == 1) {
ea68d270 1024 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1da177e4
LT
1025 /* Just in case */
1026
7bf7420a 1027 ide_init_pc(&pc);
c96a7df8
BP
1028 pc.c[0] = GPCMD_TEST_UNIT_READY;
1029
2ac07d92 1030 if (ide_queue_pc_tail(drive, disk, &pc)) {
1da177e4 1031 idefloppy_create_start_stop_cmd(&pc, 1);
2ac07d92 1032 (void)ide_queue_pc_tail(drive, disk, &pc);
1da177e4
LT
1033 }
1034
194ec0c0 1035 if (ide_floppy_get_capacity(drive)
1da177e4
LT
1036 && (filp->f_flags & O_NDELAY) == 0
1037 /*
0571c7a4
BP
1038 * Allow O_NDELAY to open a drive without a disk, or with an
1039 * unreadable disk, so that we can get the format capacity
1040 * of the drive or begin the format - Sam
1041 */
1da177e4 1042 ) {
1da177e4
LT
1043 ret = -EIO;
1044 goto out_put_floppy;
1045 }
1046
1047 if (floppy->wp && (filp->f_mode & 2)) {
1da177e4
LT
1048 ret = -EROFS;
1049 goto out_put_floppy;
1050 }
e996fc8a 1051
ea68d270 1052 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
e996fc8a 1053 ide_floppy_set_media_lock(drive, 1);
1da177e4 1054 check_disk_change(inode->i_bdev);
ea68d270 1055 } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
1da177e4
LT
1056 ret = -EBUSY;
1057 goto out_put_floppy;
1058 }
1059 return 0;
1060
1061out_put_floppy:
c94964a4 1062 floppy->openers--;
1da177e4
LT
1063 ide_floppy_put(floppy);
1064 return ret;
1065}
1066
1067static int idefloppy_release(struct inode *inode, struct file *filp)
1068{
1069 struct gendisk *disk = inode->i_bdev->bd_disk;
1070 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1071 ide_drive_t *drive = floppy->drive;
bcc77d9c
BP
1072
1073 debug_log("Reached %s\n", __func__);
1da177e4 1074
c94964a4 1075 if (floppy->openers == 1) {
e996fc8a 1076 ide_floppy_set_media_lock(drive, 0);
ea68d270 1077 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1da177e4 1078 }
c94964a4
BZ
1079
1080 floppy->openers--;
1da177e4
LT
1081
1082 ide_floppy_put(floppy);
1083
1084 return 0;
1085}
1086
a885c8c4
CH
1087static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1088{
1089 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1090 ide_drive_t *drive = floppy->drive;
1091
1092 geo->heads = drive->bios_head;
1093 geo->sectors = drive->bios_sect;
1094 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1095 return 0;
1096}
1097
ea68d270
BP
1098static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1099 unsigned long arg, unsigned int cmd)
20bf7bda 1100{
ea68d270 1101 idefloppy_floppy_t *floppy = drive->driver_data;
e996fc8a 1102 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
ea68d270 1103
20bf7bda
BP
1104 if (floppy->openers > 1)
1105 return -EBUSY;
1106
e996fc8a 1107 ide_floppy_set_media_lock(drive, prevent);
20bf7bda
BP
1108
1109 if (cmd == CDROMEJECT) {
1110 idefloppy_create_start_stop_cmd(pc, 2);
2ac07d92 1111 (void)ide_queue_pc_tail(drive, floppy->disk, pc);
20bf7bda
BP
1112 }
1113
1114 return 0;
1115}
1116
d56c99e2 1117static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
20bf7bda 1118{
d56c99e2 1119 idefloppy_floppy_t *floppy = drive->driver_data;
8e555123 1120 struct ide_atapi_pc pc;
ea68d270 1121 int blocks, length, flags, err = 0;
20bf7bda
BP
1122
1123 if (floppy->openers > 1) {
1124 /* Don't format if someone is using the disk */
ea68d270 1125 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
20bf7bda
BP
1126 return -EBUSY;
1127 }
1128
ea68d270 1129 drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
20bf7bda
BP
1130
1131 /*
1132 * Send ATAPI_FORMAT_UNIT to the drive.
1133 *
1134 * Userland gives us the following structure:
1135 *
1136 * struct idefloppy_format_command {
1137 * int nblocks;
1138 * int blocksize;
1139 * int flags;
1140 * } ;
1141 *
1142 * flags is a bitmask, currently, the only defined flag is:
1143 *
1144 * 0x01 - verify media after format.
1145 */
1146 if (get_user(blocks, arg) ||
1147 get_user(length, arg+1) ||
1148 get_user(flags, arg+2)) {
1149 err = -EFAULT;
1150 goto out;
1151 }
1152
ea68d270 1153 (void) idefloppy_get_sfrp_bit(drive);
20bf7bda
BP
1154 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1155
2ac07d92 1156 if (ide_queue_pc_tail(drive, floppy->disk, &pc))
20bf7bda
BP
1157 err = -EIO;
1158
1159out:
1160 if (err)
ea68d270 1161 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
20bf7bda
BP
1162 return err;
1163}
1164
d56c99e2
BZ
1165static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
1166 unsigned int cmd, void __user *argp)
1167{
1168 switch (cmd) {
1169 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1170 return 0;
1171 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1172 return ide_floppy_get_format_capacities(drive, argp);
1173 case IDEFLOPPY_IOCTL_FORMAT_START:
1174 if (!(file->f_mode & 2))
1175 return -EPERM;
1176 return ide_floppy_format_unit(drive, (int __user *)argp);
1177 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1178 return ide_floppy_get_format_progress(drive, argp);
1179 default:
1180 return -ENOTTY;
1181 }
1182}
20bf7bda 1183
1da177e4
LT
1184static int idefloppy_ioctl(struct inode *inode, struct file *file,
1185 unsigned int cmd, unsigned long arg)
1186{
1187 struct block_device *bdev = inode->i_bdev;
1188 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1189 ide_drive_t *drive = floppy->drive;
8e555123 1190 struct ide_atapi_pc pc;
1da177e4 1191 void __user *argp = (void __user *)arg;
07203f64 1192 int err;
1da177e4 1193
d56c99e2 1194 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
ea68d270 1195 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
1da177e4 1196
d56c99e2
BZ
1197 err = ide_floppy_format_ioctl(drive, file, cmd, argp);
1198 if (err != -ENOTTY)
1199 return err;
89636af2
BZ
1200
1201 /*
1202 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1203 * and CDROM_SEND_PACKET (legacy) ioctls
1204 */
1205 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1206 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1207 bdev->bd_disk, cmd, argp);
89636af2
BZ
1208
1209 if (err == -ENOTTY)
1210 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1211
1212 return err;
1da177e4
LT
1213}
1214
1215static int idefloppy_media_changed(struct gendisk *disk)
1216{
1217 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1218 ide_drive_t *drive = floppy->drive;
6e5fa7b8 1219 int ret;
1da177e4
LT
1220
1221 /* do not scan partitions twice if this is a removable device */
1222 if (drive->attach) {
1223 drive->attach = 0;
1224 return 0;
1225 }
ea68d270
BP
1226 ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1227 drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
6e5fa7b8 1228 return ret;
1da177e4
LT
1229}
1230
1231static int idefloppy_revalidate_disk(struct gendisk *disk)
1232{
1233 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1234 set_capacity(disk, idefloppy_capacity(floppy->drive));
1235 return 0;
1236}
1237
1238static struct block_device_operations idefloppy_ops = {
52d3ccf7
PC
1239 .owner = THIS_MODULE,
1240 .open = idefloppy_open,
1241 .release = idefloppy_release,
1242 .ioctl = idefloppy_ioctl,
1243 .getgeo = idefloppy_getgeo,
1244 .media_changed = idefloppy_media_changed,
1245 .revalidate_disk = idefloppy_revalidate_disk
1da177e4
LT
1246};
1247
4031bbe4 1248static int ide_floppy_probe(ide_drive_t *drive)
1da177e4
LT
1249{
1250 idefloppy_floppy_t *floppy;
1251 struct gendisk *g;
1252
1253 if (!strstr("ide-floppy", drive->driver_req))
1254 goto failed;
2a924662 1255
1da177e4
LT
1256 if (drive->media != ide_floppy)
1257 goto failed;
2a924662 1258
51509eec 1259 if (!ide_check_atapi_device(drive, DRV_NAME)) {
0571c7a4
BP
1260 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1261 " of ide-floppy\n", drive->name);
1da177e4
LT
1262 goto failed;
1263 }
0571c7a4
BP
1264 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1265 if (!floppy) {
1266 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1267 " structure\n", drive->name);
1da177e4
LT
1268 goto failed;
1269 }
1270
1271 g = alloc_disk(1 << PARTN_BITS);
1272 if (!g)
1273 goto out_free_floppy;
1274
1275 ide_init_disk(g, drive);
1276
1da177e4
LT
1277 kref_init(&floppy->kref);
1278
1279 floppy->drive = drive;
1280 floppy->driver = &idefloppy_driver;
1281 floppy->disk = g;
1282
1283 g->private_data = &floppy->driver;
1284
1285 drive->driver_data = floppy;
1286
0571c7a4 1287 idefloppy_setup(drive, floppy);
8604affd 1288
1da177e4
LT
1289 g->minors = 1 << PARTN_BITS;
1290 g->driverfs_dev = &drive->gendev;
1da177e4
LT
1291 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1292 g->fops = &idefloppy_ops;
1293 drive->attach = 1;
1294 add_disk(g);
1295 return 0;
1296
1da177e4
LT
1297out_free_floppy:
1298 kfree(floppy);
1299failed:
8604affd 1300 return -ENODEV;
1da177e4
LT
1301}
1302
0571c7a4 1303static void __exit idefloppy_exit(void)
1da177e4 1304{
8604affd 1305 driver_unregister(&idefloppy_driver.gen_driver);
1da177e4
LT
1306}
1307
17514e8a 1308static int __init idefloppy_init(void)
1da177e4
LT
1309{
1310 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
8604affd 1311 return driver_register(&idefloppy_driver.gen_driver);
1da177e4
LT
1312}
1313
263756ec 1314MODULE_ALIAS("ide:*m-floppy*");
1da177e4
LT
1315module_init(idefloppy_init);
1316module_exit(idefloppy_exit);
1317MODULE_LICENSE("GPL");
0571c7a4
BP
1318MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1319