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