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