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