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