blob: 416719c1718bc0b2939215868087f973ef632bfa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkovd3f20842008-02-01 23:09:33 +01002 * IDE ATAPI floppy driver.
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
12 *
13 * This driver supports the following IDE floppy drives:
14 *
15 * LS-120/240 SuperDisk
16 * Iomega Zip 100/250
17 * Iomega PC Card Clik!/PocketZip
18 *
Borislav Petkovd3f20842008-02-01 23:09:33 +010019 * For a historical changelog see
20 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#define IDEFLOPPY_VERSION "0.99.newide"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/kernel.h>
29#include <linux/delay.h>
30#include <linux/timer.h>
31#include <linux/mm.h>
32#include <linux/interrupt.h>
33#include <linux/major.h>
34#include <linux/errno.h>
35#include <linux/genhd.h>
36#include <linux/slab.h>
37#include <linux/cdrom.h>
38#include <linux/ide.h>
39#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +020042#include <scsi/scsi_ioctl.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/byteorder.h>
Borislav Petkov7e8b1632008-02-02 19:56:34 +010045#include <linux/irq.h>
46#include <linux/uaccess.h>
47#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/unaligned.h>
49
50/*
51 * The following are used to debug the driver.
52 */
53#define IDEFLOPPY_DEBUG_LOG 0
54#define IDEFLOPPY_DEBUG_INFO 0
55#define IDEFLOPPY_DEBUG_BUGS 1
56
57/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
58#define IDEFLOPPY_DEBUG( fmt, args... )
59
60#if IDEFLOPPY_DEBUG_LOG
Borislav Petkovbcc77d92008-02-02 19:56:34 +010061#define debug_log(fmt, args...) \
62 printk(KERN_INFO "ide-floppy: " fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#else
64#define debug_log(fmt, args... ) do {} while(0)
65#endif
66
67
68/*
69 * Some drives require a longer irq timeout.
70 */
71#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
72
73/*
74 * After each failed packet command we issue a request sense command
75 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
76 */
77#define IDEFLOPPY_MAX_PC_RETRIES 3
78
79/*
80 * With each packet command, we allocate a buffer of
81 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
82 */
83#define IDEFLOPPY_PC_BUFFER_SIZE 256
84
85/*
86 * In various places in the driver, we need to allocate storage
87 * for packet commands and requests, which will remain valid while
88 * we leave the driver to wait for an interrupt or a timeout event.
89 */
90#define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
91
92/*
93 * Our view of a packet command.
94 */
95typedef struct idefloppy_packet_command_s {
96 u8 c[12]; /* Actual packet bytes */
97 int retries; /* On each retry, we increment retries */
98 int error; /* Error code */
99 int request_transfer; /* Bytes to transfer */
100 int actually_transferred; /* Bytes actually transferred */
101 int buffer_size; /* Size of our data buffer */
102 int b_count; /* Missing/Available data on the current buffer */
103 struct request *rq; /* The corresponding request */
104 u8 *buffer; /* Data buffer */
105 u8 *current_position; /* Pointer into the above buffer */
106 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
107 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
108 unsigned long flags; /* Status/Action bit flags: long for set_bit */
109} idefloppy_pc_t;
110
111/*
112 * Packet command flag bits.
113 */
114#define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
115#define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
116#define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
117#define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
118#define PC_WRITING 5 /* Data direction */
119
120#define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
121
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100122/* format capacities descriptor codes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define CAPACITY_INVALID 0x00
124#define CAPACITY_UNFORMATTED 0x01
125#define CAPACITY_CURRENT 0x02
126#define CAPACITY_NO_CARTRIDGE 0x03
127
128/*
129 * Most of our global data which we need to save even as we leave the
130 * driver due to an interrupt or a timer event is stored in a variable
131 * of type idefloppy_floppy_t, defined below.
132 */
133typedef struct ide_floppy_obj {
134 ide_drive_t *drive;
135 ide_driver_t *driver;
136 struct gendisk *disk;
137 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100138 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* Current packet command */
141 idefloppy_pc_t *pc;
142 /* Last failed packet command */
143 idefloppy_pc_t *failed_pc;
144 /* Packet command stack */
145 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
146 /* Next free packet command storage space */
147 int pc_stack_index;
148 struct request rq_stack[IDEFLOPPY_PC_STACK];
149 /* We implement a circular array */
150 int rq_stack_index;
151
152 /*
153 * Last error information
154 */
155 u8 sense_key, asc, ascq;
156 /* delay this long before sending packet command */
157 u8 ticks;
158 int progress_indication;
159
160 /*
161 * Device information
162 */
163 /* Current format */
164 int blocks, block_size, bs_factor;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100165 /* Last format capacity descriptor */
166 u8 cap_desc[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* Copy of the flexible disk page */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100168 u8 flexible_disk_page[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Write protect */
170 int wp;
171 /* Supports format progress report */
172 int srfp;
173 /* Status/Action flags */
174 unsigned long flags;
175} idefloppy_floppy_t;
176
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200177#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/*
180 * Floppy flag bits values.
181 */
182#define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
183#define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
184#define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
185#define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
186#define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
187#define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
188
189/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * Defines for the mode sense command
191 */
192#define MODE_SENSE_CURRENT 0x00
193#define MODE_SENSE_CHANGEABLE 0x01
194#define MODE_SENSE_DEFAULT 0x02
195#define MODE_SENSE_SAVED 0x03
196
197/*
198 * IOCTLs used in low-level formatting.
199 */
200
201#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
202#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
203#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
204#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
207 * Error codes which are returned in rq->errors to the higher part
208 * of the driver.
209 */
210#define IDEFLOPPY_ERROR_GENERAL 101
211
212/*
213 * The following is used to format the general configuration word of
214 * the ATAPI IDENTIFY DEVICE command.
215 */
216struct idefloppy_id_gcw {
217#if defined(__LITTLE_ENDIAN_BITFIELD)
218 unsigned packet_size :2; /* Packet Size */
219 unsigned reserved234 :3; /* Reserved */
220 unsigned drq_type :2; /* Command packet DRQ type */
221 unsigned removable :1; /* Removable media */
222 unsigned device_type :5; /* Device type */
223 unsigned reserved13 :1; /* Reserved */
224 unsigned protocol :2; /* Protocol type */
225#elif defined(__BIG_ENDIAN_BITFIELD)
226 unsigned protocol :2; /* Protocol type */
227 unsigned reserved13 :1; /* Reserved */
228 unsigned device_type :5; /* Device type */
229 unsigned removable :1; /* Removable media */
230 unsigned drq_type :2; /* Command packet DRQ type */
231 unsigned reserved234 :3; /* Reserved */
232 unsigned packet_size :2; /* Packet Size */
233#else
234#error "Bitfield endianness not defined! Check your byteorder.h"
235#endif
236};
237
238/*
Borislav Petkov948391d2008-02-02 19:56:34 +0100239 * Pages of the SELECT SENSE / MODE SENSE packet commands.
240 * See SFF-8070i spec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
242#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
243#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
244
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800245static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
248
249#define ide_floppy_g(disk) \
250 container_of((disk)->private_data, struct ide_floppy_obj, driver)
251
252static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
253{
254 struct ide_floppy_obj *floppy = NULL;
255
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800256 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 floppy = ide_floppy_g(disk);
258 if (floppy)
259 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800260 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return floppy;
262}
263
Borislav Petkov9a24b632008-02-02 19:56:33 +0100264static void idefloppy_cleanup_obj(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266static void ide_floppy_put(struct ide_floppy_obj *floppy)
267{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800268 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100269 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800270 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
274 * Too bad. The drive wants to send us data which we are not ready to accept.
275 * Just throw it away.
276 */
277static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
278{
279 while (bcount--)
280 (void) HWIF(drive)->INB(IDE_DATA_REG);
281}
282
283#if IDEFLOPPY_DEBUG_BUGS
284static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
285{
286 while (bcount--)
287 HWIF(drive)->OUTB(0, IDE_DATA_REG);
288}
289#endif /* IDEFLOPPY_DEBUG_BUGS */
290
291
292/*
293 * idefloppy_do_end_request is used to finish servicing a request.
294 *
295 * For read/write requests, we will call ide_end_request to pass to the
296 * next buffer.
297 */
298static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
299{
300 idefloppy_floppy_t *floppy = drive->driver_data;
301 struct request *rq = HWGROUP(drive)->rq;
302 int error;
303
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100304 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 switch (uptodate) {
307 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
308 case 1: error = 0; break;
309 default: error = uptodate;
310 }
311 if (error)
312 floppy->failed_pc = NULL;
313 /* Why does this happen? */
314 if (!rq)
315 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200316 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* our real local end request function */
318 ide_end_request(drive, uptodate, nsecs);
319 return 0;
320 }
321 rq->errors = error;
322 /* fixme: need to move this local also */
323 ide_end_drive_cmd(drive, 0, 0);
324 return 0;
325}
326
327static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
328{
329 struct request *rq = pc->rq;
330 struct bio_vec *bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200331 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 unsigned long flags;
333 char *data;
NeilBrown5705f702007-09-25 12:35:59 +0200334 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
NeilBrown5705f702007-09-25 12:35:59 +0200336 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200337 if (!bcount)
338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jens Axboe6c92e692007-08-16 13:43:12 +0200340 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jens Axboe6c92e692007-08-16 13:43:12 +0200342 data = bvec_kmap_irq(bvec, &flags);
343 drive->hwif->atapi_input_bytes(drive, data, count);
344 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jens Axboe6c92e692007-08-16 13:43:12 +0200346 bcount -= count;
347 pc->b_count += count;
348 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 idefloppy_do_end_request(drive, 1, done >> 9);
352
353 if (bcount) {
354 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
355 idefloppy_discard_data(drive, bcount);
356 }
357}
358
359static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
360{
361 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200362 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct bio_vec *bvec;
364 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200365 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 char *data;
367
NeilBrown5705f702007-09-25 12:35:59 +0200368 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200369 if (!bcount)
370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Jens Axboe6c92e692007-08-16 13:43:12 +0200372 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Jens Axboe6c92e692007-08-16 13:43:12 +0200374 data = bvec_kmap_irq(bvec, &flags);
375 drive->hwif->atapi_output_bytes(drive, data, count);
376 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Jens Axboe6c92e692007-08-16 13:43:12 +0200378 bcount -= count;
379 pc->b_count += count;
380 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 idefloppy_do_end_request(drive, 1, done >> 9);
384
Jan Beulichc7ea4b32005-06-23 00:09:59 -0700385#if IDEFLOPPY_DEBUG_BUGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (bcount) {
387 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
388 idefloppy_write_zeros(drive, bcount);
389 }
Jan Beulichc7ea4b32005-06-23 00:09:59 -0700390#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
394{
395 struct request *rq = pc->rq;
396 struct bio *bio = rq->bio;
397
398 while ((bio = rq->bio) != NULL)
399 idefloppy_do_end_request(drive, 1, 0);
400}
401
402/*
403 * idefloppy_queue_pc_head generates a new packet command request in front
404 * of the request queue, before the current request, so that it will be
405 * processed immediately, on the next pass through the driver.
406 */
407static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
408{
409 struct ide_floppy_obj *floppy = drive->driver_data;
410
411 ide_init_drive_cmd(rq);
412 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200413 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 rq->rq_disk = floppy->disk;
415 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
416}
417
418static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
419{
420 idefloppy_floppy_t *floppy = drive->driver_data;
421
422 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
423 floppy->pc_stack_index=0;
424 return (&floppy->pc_stack[floppy->pc_stack_index++]);
425}
426
427static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
428{
429 idefloppy_floppy_t *floppy = drive->driver_data;
430
431 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
432 floppy->rq_stack_index = 0;
433 return (&floppy->rq_stack[floppy->rq_stack_index++]);
434}
435
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100436static void idefloppy_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100439 u8 *buf = floppy->pc->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100441 debug_log("Reached %s\n", __func__);
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (!floppy->pc->error) {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100444 floppy->sense_key = buf[2] & 0x0F;
445 floppy->asc = buf[12];
446 floppy->ascq = buf[13];
447 floppy->progress_indication = buf[15] & 0x80 ?
448 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
449
450 if (floppy->failed_pc)
451 debug_log("pc = %x, sense key = %x, asc = %x,"
452 " ascq = %x\n",
453 floppy->failed_pc->c[0],
454 floppy->sense_key,
455 floppy->asc,
456 floppy->ascq);
457 else
458 debug_log("sense key = %x, asc = %x, ascq = %x\n",
459 floppy->sense_key,
460 floppy->asc,
461 floppy->ascq);
462
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 idefloppy_do_end_request(drive, 1, 0);
465 } else {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100466 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
467 " request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 idefloppy_do_end_request(drive, 0, 0);
469 }
470}
471
472/*
473 * General packet command callback function.
474 */
475static void idefloppy_pc_callback (ide_drive_t *drive)
476{
477 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100478
479 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
482}
483
484/*
485 * idefloppy_init_pc initializes a packet command.
486 */
487static void idefloppy_init_pc (idefloppy_pc_t *pc)
488{
489 memset(pc->c, 0, 12);
490 pc->retries = 0;
491 pc->flags = 0;
492 pc->request_transfer = 0;
493 pc->buffer = pc->pc_buffer;
494 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
495 pc->callback = &idefloppy_pc_callback;
496}
497
498static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
499{
Borislav Petkov30d67092008-02-02 19:56:33 +0100500 idefloppy_init_pc(pc);
501 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 pc->c[4] = 255;
503 pc->request_transfer = 18;
504 pc->callback = &idefloppy_request_sense_callback;
505}
506
507/*
508 * idefloppy_retry_pc is called when an error was detected during the
509 * last packet command. We queue a request sense packet command in
510 * the head of the request list.
511 */
512static void idefloppy_retry_pc (ide_drive_t *drive)
513{
514 idefloppy_pc_t *pc;
515 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100517 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 pc = idefloppy_next_pc_storage(drive);
519 rq = idefloppy_next_rq_storage(drive);
520 idefloppy_create_request_sense_cmd(pc);
521 idefloppy_queue_pc_head(drive, pc, rq);
522}
523
524/*
525 * idefloppy_pc_intr is the usual interrupt handler which will be called
526 * during a packet command.
527 */
528static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
529{
530 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100531 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 idefloppy_pc_t *pc = floppy->pc;
533 struct request *rq = pc->rq;
534 unsigned int temp;
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100535 int dma_error = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100536 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100537 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100539 debug_log("Reached %s interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100542 dma_error = hwif->ide_dma_end(drive);
543 if (dma_error) {
544 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
545 rq_data_dir(rq) ? "write" : "read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 set_bit(PC_DMA_ERROR, &pc->flags);
547 } else {
548 pc->actually_transferred = pc->request_transfer;
549 idefloppy_update_buffers(drive, pc);
550 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100551 debug_log("DMA finished\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
554 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100555 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100557 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100558 debug_log("Packet command completed, %d bytes transferred\n",
559 pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
561
Ingo Molnar366c7f52006-07-03 00:25:25 -0700562 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100564 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* Error detected */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100566 debug_log("%s: I/O error\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 rq->errors++;
Borislav Petkov30d67092008-02-02 19:56:33 +0100568 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 printk(KERN_ERR "ide-floppy: I/O error in "
570 "request sense command\n");
571 return ide_do_reset(drive);
572 }
573 /* Retry operation */
574 idefloppy_retry_pc(drive);
575 /* queued, but not started */
576 return ide_stopped;
577 }
578 pc->error = 0;
579 if (floppy->failed_pc == pc)
580 floppy->failed_pc = NULL;
581 /* Command finished - Call the callback function */
582 pc->callback(drive);
583 return ide_stopped;
584 }
585
586 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
587 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
588 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100589 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return ide_do_reset(drive);
591 }
592
593 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100594 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
595 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* on this interrupt */
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100597 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100599 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
601 return ide_do_reset(drive);
602 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100603 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Hopefully, we will never get here */
605 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100606 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 printk(KERN_ERR "but the floppy wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100608 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return ide_do_reset(drive);
610 }
611 if (!test_bit(PC_WRITING, &pc->flags)) {
612 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100613 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (temp > pc->request_transfer) {
615 if (temp > pc->buffer_size) {
616 printk(KERN_ERR "ide-floppy: The floppy wants "
617 "to send us more data than expected "
618 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100619 idefloppy_discard_data(drive, bcount);
Borislav Petkov0078df22008-02-02 19:56:33 +0100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 ide_set_handler(drive,
622 &idefloppy_pc_intr,
623 IDEFLOPPY_WAIT_CMD,
624 NULL);
625 return ide_started;
626 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100627 debug_log("The floppy wants to send us more data than"
628 " expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 }
631 if (test_bit(PC_WRITING, &pc->flags)) {
632 if (pc->buffer != NULL)
633 /* Write the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100634 hwif->atapi_output_bytes(drive, pc->current_position,
635 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100637 idefloppy_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 } else {
639 if (pc->buffer != NULL)
640 /* Read the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100641 hwif->atapi_input_bytes(drive, pc->current_position,
642 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100644 idefloppy_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100647 pc->actually_transferred += bcount;
648 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
651 return ide_started;
652}
653
654/*
655 * This is the original routine that did the packet transfer.
656 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
657 * for that drive below. The algorithm is chosen based on drive type
658 */
659static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
660{
661 ide_startstop_t startstop;
662 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100663 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
666 printk(KERN_ERR "ide-floppy: Strange, packet command "
667 "initiated yet DRQ isn't asserted\n");
668 return startstop;
669 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100670 ireason = drive->hwif->INB(IDE_IREASON_REG);
671 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
673 "issuing a packet command\n");
674 return ide_do_reset(drive);
675 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* Set the interrupt routine */
678 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
679 /* Send the actual packet */
680 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
681 return ide_started;
682}
683
684
685/*
686 * What we have here is a classic case of a top half / bottom half
687 * interrupt service routine. In interrupt mode, the device sends
688 * an interrupt to signal it's ready to receive a packet. However,
689 * we need to delay about 2-3 ticks before issuing the packet or we
690 * gets in trouble.
691 *
692 * So, follow carefully. transfer_pc1 is called as an interrupt (or
693 * directly). In either case, when the device says it's ready for a
694 * packet, we schedule the packet transfer to occur about 2-3 ticks
695 * later in transfer_pc2.
696 */
697static int idefloppy_transfer_pc2 (ide_drive_t *drive)
698{
699 idefloppy_floppy_t *floppy = drive->driver_data;
700
701 /* Send the actual packet */
702 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
703 /* Timeout for the packet command */
704 return IDEFLOPPY_WAIT_CMD;
705}
706
707static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
708{
709 idefloppy_floppy_t *floppy = drive->driver_data;
710 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100711 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
714 printk(KERN_ERR "ide-floppy: Strange, packet command "
715 "initiated yet DRQ isn't asserted\n");
716 return startstop;
717 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100718 ireason = drive->hwif->INB(IDE_IREASON_REG);
719 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
721 "while issuing a packet command\n");
722 return ide_do_reset(drive);
723 }
724 /*
725 * The following delay solves a problem with ATAPI Zip 100 drives
726 * where the Busy flag was apparently being deasserted before the
727 * unit was ready to receive data. This was happening on a
728 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
729 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
730 * used until after the packet is moved in about 50 msec.
731 */
Borislav Petkov0078df22008-02-02 19:56:33 +0100732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 ide_set_handler(drive,
734 &idefloppy_pc_intr, /* service routine for packet command */
735 floppy->ticks, /* wait this long before "failing" */
736 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
737 return ide_started;
738}
739
Borislav Petkovd652c132008-02-02 19:56:35 +0100740static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
741 idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Borislav Petkovd652c132008-02-02 19:56:35 +0100743 /* supress error messages resulting from Medium not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (floppy->sense_key == 0x02 &&
745 floppy->asc == 0x3a &&
746 floppy->ascq == 0x00)
Borislav Petkovd652c132008-02-02 19:56:35 +0100747 return;
748
749 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
750 "asc = %2x, ascq = %2x\n",
751 floppy->drive->name, pc->c[0], floppy->sense_key,
752 floppy->asc, floppy->ascq);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
757 * Issue a packet command
758 */
759static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
760{
761 idefloppy_floppy_t *floppy = drive->driver_data;
762 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100764 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100765 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100768 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 floppy->failed_pc = pc;
770 /* Set the current packet command */
771 floppy->pc = pc;
772
773 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
774 test_bit(PC_ABORT, &pc->flags)) {
775 /*
776 * We will "abort" retrying a packet command in case
777 * a legitimate error code was received.
778 */
779 if (!test_bit(PC_ABORT, &pc->flags)) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100780 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags))
781 ide_floppy_report_error(floppy, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* Giving up */
783 pc->error = IDEFLOPPY_ERROR_GENERAL;
784 }
785 floppy->failed_pc = NULL;
786 pc->callback(drive);
787 return ide_stopped;
788 }
789
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100790 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 pc->retries++;
793 /* We haven't transferred any data yet */
794 pc->actually_transferred = 0;
795 pc->current_position = pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100796 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100798 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
799 ide_dma_off(drive);
800
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100801 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100804 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100806 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
807 IDE_TFLAG_OUT_DEVICE, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100809 if (dma) { /* Begin DMA, if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
811 hwif->dma_start(drive);
812 }
813
814 /* Can we transfer the packet when we get the interrupt or wait? */
815 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
816 /* wait */
817 pkt_xfer_routine = &idefloppy_transfer_pc1;
818 } else {
819 /* immediate */
820 pkt_xfer_routine = &idefloppy_transfer_pc;
821 }
822
823 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
824 /* Issue the packet command */
825 ide_execute_command(drive, WIN_PACKETCMD,
826 pkt_xfer_routine,
827 IDEFLOPPY_WAIT_CMD,
828 NULL);
829 return ide_started;
830 } else {
831 /* Issue the packet command */
832 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
833 return (*pkt_xfer_routine) (drive);
834 }
835}
836
837static void idefloppy_rw_callback (ide_drive_t *drive)
838{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100839 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 idefloppy_do_end_request(drive, 1, 0);
842 return;
843}
844
845static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
846{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100847 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100850 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 pc->c[4] = prevent;
852}
853
854static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
855{
856 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100857 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 pc->c[7] = 255;
859 pc->c[8] = 255;
860 pc->request_transfer = 255;
861}
862
863static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
864 int flags)
865{
866 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100867 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 pc->c[1] = 0x17;
869
870 memset(pc->buffer, 0, 12);
871 pc->buffer[1] = 0xA2;
872 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
873
874 if (flags & 1) /* Verify bit on... */
875 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
876 pc->buffer[3] = 8;
877
Borislav Petkov8f622432008-02-02 19:56:33 +0100878 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
879 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 pc->buffer_size=12;
881 set_bit(PC_WRITING, &pc->flags);
882}
883
884/*
885 * A mode sense command is used to "sense" floppy parameters.
886 */
887static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
888{
Borislav Petkov24a5d702008-02-02 19:56:34 +0100889 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100892 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 pc->c[1] = 0;
894 pc->c[2] = page_code + (type << 6);
895
896 switch (page_code) {
897 case IDEFLOPPY_CAPABILITIES_PAGE:
898 length += 12;
899 break;
900 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
901 length += 32;
902 break;
903 default:
904 printk(KERN_ERR "ide-floppy: unsupported page code "
905 "in create_mode_sense_cmd\n");
906 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100907 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 pc->request_transfer = length;
909}
910
911static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
912{
913 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100914 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 pc->c[4] = start;
916}
917
918static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
919{
920 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100921 pc->c[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
925{
926 int block = sector / floppy->bs_factor;
927 int blocks = rq->nr_sectors / floppy->bs_factor;
928 int cmd = rq_data_dir(rq);
929
930 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
931 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
932 block, blocks);
933
934 idefloppy_init_pc(pc);
935 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
Borislav Petkov30d67092008-02-02 19:56:33 +0100936 pc->c[0] = cmd == READ ? GPCMD_READ_12 : GPCMD_WRITE_12;
Borislav Petkov8f622432008-02-02 19:56:33 +0100937 put_unaligned(cpu_to_be32(blocks), (unsigned int *) &pc->c[6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 } else {
Borislav Petkov30d67092008-02-02 19:56:33 +0100939 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
Borislav Petkov8f622432008-02-02 19:56:33 +0100940 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100942 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 pc->callback = &idefloppy_rw_callback;
944 pc->rq = rq;
945 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200946 if (rq->cmd_flags & REQ_RW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 set_bit(PC_WRITING, &pc->flags);
948 pc->buffer = NULL;
949 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
950 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
951}
952
Jens Axboe3d6392c2007-07-09 12:38:05 +0200953static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
955{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 idefloppy_init_pc(pc);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200957 pc->callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200959 pc->rq = rq;
960 pc->b_count = rq->data_len;
961 if (rq->data_len && rq_data_dir(rq) == WRITE)
962 set_bit(PC_WRITING, &pc->flags);
963 pc->buffer = rq->data;
964 if (rq->bio)
965 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
966
967 /*
968 * possibly problematic, doesn't look like ide-floppy correctly
969 * handled scattered requests if dma fails...
970 */
971 pc->request_transfer = pc->buffer_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
975 * idefloppy_do_request is our request handling function.
976 */
977static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
978{
979 idefloppy_floppy_t *floppy = drive->driver_data;
980 idefloppy_pc_t *pc;
981 unsigned long block = (unsigned long)block_s;
982
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100983 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -0700984 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100985 rq->cmd_type, rq->errors);
986 debug_log("sector: %ld, nr_sectors: %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 "current_nr_sectors: %d\n", (long)rq->sector,
988 rq->nr_sectors, rq->current_nr_sectors);
989
990 if (rq->errors >= ERROR_MAX) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100991 if (floppy->failed_pc)
992 ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 else
994 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
995 drive->name);
996 idefloppy_do_end_request(drive, 0, 0);
997 return ide_stopped;
998 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200999 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (((long)rq->sector % floppy->bs_factor) ||
1001 (rq->nr_sectors % floppy->bs_factor)) {
1002 printk("%s: unsupported r/w request size\n",
1003 drive->name);
1004 idefloppy_do_end_request(drive, 0, 0);
1005 return ide_stopped;
1006 }
1007 pc = idefloppy_next_pc_storage(drive);
1008 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001009 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 pc = (idefloppy_pc_t *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001011 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001013 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 } else {
1015 blk_dump_rq_flags(rq,
1016 "ide-floppy: unsupported command in queue");
1017 idefloppy_do_end_request(drive, 0, 0);
1018 return ide_stopped;
1019 }
1020
1021 pc->rq = rq;
1022 return idefloppy_issue_pc(drive, pc);
1023}
1024
1025/*
1026 * idefloppy_queue_pc_tail adds a special packet command request to the
1027 * tail of the request queue, and waits for it to be serviced.
1028 */
1029static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1030{
1031 struct ide_floppy_obj *floppy = drive->driver_data;
1032 struct request rq;
1033
1034 ide_init_drive_cmd (&rq);
1035 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001036 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 rq.rq_disk = floppy->disk;
1038
1039 return ide_do_drive_cmd(drive, &rq, ide_wait);
1040}
1041
1042/*
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001043 * Look at the flexible disk page parameters. We ignore the CHS capacity
1044 * parameters and use the LBA parameters instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001046static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 idefloppy_floppy_t *floppy = drive->driver_data;
1049 idefloppy_pc_t pc;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001050 u8 *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int capacity, lba_capacity;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001052 u16 transfer_rate, sector_size, cyls, rpm;
1053 u8 heads, sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001055 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
1056 MODE_SENSE_CURRENT);
1057
1058 if (idefloppy_queue_pc_tail(drive, &pc)) {
1059 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
1060 " parameters\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return 1;
1062 }
Borislav Petkov24a5d702008-02-02 19:56:34 +01001063 floppy->wp = !!(pc.buffer[3] & 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 set_disk_ro(floppy->disk, floppy->wp);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001065 page = &pc.buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001067 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
1068 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
1069 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
1070 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
1071 heads = pc.buffer[8 + 4];
1072 sectors = pc.buffer[8 + 5];
1073
1074 capacity = cyls * heads * sectors * sector_size;
1075
1076 if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1078 "%d sector size, %d rpm\n",
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001079 drive->name, capacity / 1024, cyls, heads,
1080 sectors, transfer_rate / 8, sector_size, rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001082 memcpy(&floppy->flexible_disk_page, page, 32);
1083 drive->bios_cyl = cyls;
1084 drive->bios_head = heads;
1085 drive->bios_sect = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 lba_capacity = floppy->blocks * floppy->block_size;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (capacity < lba_capacity) {
1089 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1090 "bytes, but the drive only handles %d\n",
1091 drive->name, lba_capacity, capacity);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001092 floppy->blocks = floppy->block_size ?
1093 capacity / floppy->block_size : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095 return 0;
1096}
1097
Borislav Petkov948391d2008-02-02 19:56:34 +01001098static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 idefloppy_floppy_t *floppy = drive->driver_data;
1101 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 floppy->srfp = 0;
1104 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1105 MODE_SENSE_CURRENT);
1106
1107 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
Borislav Petkov948391d2008-02-02 19:56:34 +01001108 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Borislav Petkov948391d2008-02-02 19:56:34 +01001111 floppy->srfp = pc.buffer[8 + 2] & 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return (0);
1113}
1114
1115/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001116 * Determine if a media is present in the floppy drive, and if so, its LBA
1117 * capacity.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001119static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 idefloppy_floppy_t *floppy = drive->driver_data;
1122 idefloppy_pc_t pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001123 u8 *cap_desc;
1124 u8 header_len, desc_cnt;
1125 int i, rc = 1, blocks, length;
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 drive->bios_cyl = 0;
1128 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +01001129 floppy->blocks = 0;
1130 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 set_capacity(floppy->disk, 0);
1132
1133 idefloppy_create_read_capacity_cmd(&pc);
1134 if (idefloppy_queue_pc_tail(drive, &pc)) {
1135 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1136 return 1;
1137 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001138 header_len = pc.buffer[3];
1139 cap_desc = &pc.buffer[4];
1140 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001142 for (i = 0; i < desc_cnt; i++) {
1143 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001145 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1146 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1147
1148 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1149 i, blocks * length / 1024, blocks, length);
1150
1151 if (i)
1152 continue;
1153 /*
1154 * the code below is valid only for the 1st descriptor, ie i=0
1155 */
1156
1157 switch (pc.buffer[desc_start + 4] & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1159 case CAPACITY_UNFORMATTED:
1160 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1161 /*
1162 * If it is not a clik drive, break out
1163 * (maintains previous driver behaviour)
1164 */
1165 break;
1166 case CAPACITY_CURRENT:
1167 /* Normal Zip/LS-120 disks */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001168 if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1170 "sector size\n", drive->name,
1171 blocks * length / 1024, blocks, length);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001172 memcpy(&floppy->cap_desc, cap_desc, 8);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (!length || length % 512) {
1175 printk(KERN_NOTICE "%s: %d bytes block size "
1176 "not supported\n", drive->name, length);
1177 } else {
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001178 floppy->blocks = blocks;
1179 floppy->block_size = length;
1180 floppy->bs_factor = length / 512;
1181 if (floppy->bs_factor != 1)
1182 printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 "512 bytes block size not "
1184 "fully supported\n",
1185 drive->name);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001186 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 break;
1189 case CAPACITY_NO_CARTRIDGE:
1190 /*
1191 * This is a KERN_ERR so it appears on screen
1192 * for the user to see
1193 */
1194 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1195 break;
1196 case CAPACITY_INVALID:
1197 printk(KERN_ERR "%s: Invalid capacity for disk "
1198 "in drive\n", drive->name);
1199 break;
1200 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001201 debug_log("Descriptor 0 Code: %d\n",
1202 pc.buffer[desc_start + 4] & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
1205 /* Clik! disk does not support get_flexible_disk_page */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001206 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001207 (void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1210 return rc;
1211}
1212
1213/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001214 * Obtain the list of formattable capacities.
1215 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1216 * descriptors to userland, instead of our own structures.
1217 *
1218 * Userland gives us the following structure:
1219 *
1220 * struct idefloppy_format_capacities {
1221 * int nformats;
1222 * struct {
1223 * int nblocks;
1224 * int blocksize;
1225 * } formats[];
1226 * };
1227 *
1228 * userland initializes nformats to the number of allocated formats[] records.
1229 * On exit we set nformats to the number of records we've actually initialized.
1230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001232static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001234 idefloppy_pc_t pc;
1235 u8 header_len, desc_cnt;
1236 int i, blocks, length, u_array_size, u_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int __user *argp;
1238
1239 if (get_user(u_array_size, arg))
1240 return (-EFAULT);
1241
1242 if (u_array_size <= 0)
1243 return (-EINVAL);
1244
1245 idefloppy_create_read_capacity_cmd(&pc);
1246 if (idefloppy_queue_pc_tail(drive, &pc)) {
1247 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001248 return (-EIO);
1249 }
1250 header_len = pc.buffer[3];
1251 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 u_index = 0;
1254 argp = arg + 1;
1255
1256 /*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001257 * We always skip the first capacity descriptor. That's the current
1258 * capacity. We are interested in the remaining descriptors, the
1259 * formattable capacities.
1260 */
1261 for (i = 1; i < desc_cnt; i++) {
1262 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (u_index >= u_array_size)
1265 break; /* User-supplied buffer too small */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001267 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1268 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (put_user(blocks, argp))
1271 return(-EFAULT);
1272 ++argp;
1273
1274 if (put_user(length, argp))
1275 return (-EFAULT);
1276 ++argp;
1277
1278 ++u_index;
1279 }
1280
1281 if (put_user(u_index, arg))
1282 return (-EFAULT);
1283 return (0);
1284}
1285
1286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287** Get ATAPI_FORMAT_UNIT progress indication.
1288**
Matt LaPlante0779bf22006-11-30 05:24:39 +01001289** Userland gives a pointer to an int. The int is set to a progress
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290** indicator 0-65536, with 65536=100%.
1291**
1292** If the drive does not support format progress indication, we just check
1293** the dsc bit, and return either 0 or 65536.
1294*/
1295
1296static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1297{
1298 idefloppy_floppy_t *floppy = drive->driver_data;
1299 idefloppy_pc_t pc;
1300 int progress_indication = 0x10000;
1301
1302 if (floppy->srfp) {
1303 idefloppy_create_request_sense_cmd(&pc);
1304 if (idefloppy_queue_pc_tail(drive, &pc)) {
1305 return (-EIO);
1306 }
1307
1308 if (floppy->sense_key == 2 &&
1309 floppy->asc == 4 &&
1310 floppy->ascq == 4) {
1311 progress_indication = floppy->progress_indication;
1312 }
1313 /* Else assume format_unit has finished, and we're
1314 ** at 0x10000 */
1315 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001317 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 local_irq_save(flags);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001320 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 local_irq_restore(flags);
1322
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001323 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 if (put_user(progress_indication, arg))
1326 return (-EFAULT);
1327
1328 return (0);
1329}
1330
1331/*
1332 * Return the current floppy capacity.
1333 */
1334static sector_t idefloppy_capacity (ide_drive_t *drive)
1335{
1336 idefloppy_floppy_t *floppy = drive->driver_data;
1337 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1338
1339 return capacity;
1340}
1341
1342/*
1343 * idefloppy_identify_device checks if we can support a drive,
1344 * based on the ATAPI IDENTIFY command results.
1345 */
1346static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1347{
1348 struct idefloppy_id_gcw gcw;
1349#if IDEFLOPPY_DEBUG_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 char buffer[80];
1351#endif /* IDEFLOPPY_DEBUG_INFO */
1352
1353 *((u16 *) &gcw) = id->config;
1354
1355#ifdef CONFIG_PPC
1356 /* kludge for Apple PowerBook internal zip */
1357 if ((gcw.device_type == 5) &&
1358 !strstr(id->model, "CD-ROM") &&
1359 strstr(id->model, "ZIP"))
1360 gcw.device_type = 0;
1361#endif
1362
1363#if IDEFLOPPY_DEBUG_INFO
1364 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1365 switch (gcw.protocol) {
1366 case 0: case 1: sprintf(buffer, "ATA");break;
1367 case 2: sprintf(buffer, "ATAPI");break;
1368 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1369 }
1370 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1371 switch (gcw.device_type) {
1372 case 0: sprintf(buffer, "Direct-access Device");break;
1373 case 1: sprintf(buffer, "Streaming Tape Device");break;
1374 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1375 case 5: sprintf(buffer, "CD-ROM Device");break;
1376 case 6: sprintf(buffer, "Reserved");
1377 case 7: sprintf(buffer, "Optical memory Device");break;
1378 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1379 default: sprintf(buffer, "Reserved");
1380 }
1381 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1382 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1383 switch (gcw.drq_type) {
1384 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1385 case 1: sprintf(buffer, "Interrupt DRQ");break;
1386 case 2: sprintf(buffer, "Accelerated DRQ");break;
1387 case 3: sprintf(buffer, "Reserved");break;
1388 }
1389 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1390 switch (gcw.packet_size) {
1391 case 0: sprintf(buffer, "12 bytes");break;
1392 case 1: sprintf(buffer, "16 bytes");break;
1393 default: sprintf(buffer, "Reserved");break;
1394 }
1395 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396#endif /* IDEFLOPPY_DEBUG_INFO */
1397
1398 if (gcw.protocol != 2)
1399 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1400 else if (gcw.device_type != 0)
1401 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1402 else if (!gcw.removable)
1403 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1404 else if (gcw.drq_type == 3) {
1405 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1406 } else if (gcw.packet_size != 0) {
1407 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1408 } else
1409 return 1;
1410 return 0;
1411}
1412
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001413#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414static void idefloppy_add_settings(ide_drive_t *drive)
1415{
1416 idefloppy_floppy_t *floppy = drive->driver_data;
1417
1418/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001419 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001421 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1422 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1423 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1424 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001426#else
1427static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1428#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430/*
1431 * Driver initialization.
1432 */
1433static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1434{
1435 struct idefloppy_id_gcw gcw;
1436
1437 *((u16 *) &gcw) = drive->id->config;
1438 floppy->pc = floppy->pc_stack;
1439 if (gcw.drq_type == 1)
1440 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1441 /*
1442 * We used to check revisions here. At this point however
1443 * I'm giving up. Just assume they are all broken, its easier.
1444 *
1445 * The actual reason for the workarounds was likely
1446 * a driver bug after all rather than a firmware bug,
1447 * and the workaround below used to hide it. It should
1448 * be fixed as of version 1.9, but to be on the safe side
1449 * we'll leave the limitation below for the 2.2.x tree.
1450 */
1451
1452 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1453 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1454 /* This value will be visible in the /proc/ide/hdx/settings */
1455 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1456 blk_queue_max_sectors(drive->queue, 64);
1457 }
1458
1459 /*
1460 * Guess what? The IOMEGA Clik! drive also needs the
1461 * above fix. It makes nasty clicking noises without
1462 * it, so please don't remove this.
1463 */
1464 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1465 blk_queue_max_sectors(drive->queue, 64);
1466 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1467 }
1468
1469
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001470 (void) ide_floppy_get_capacity(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 idefloppy_add_settings(drive);
1472}
1473
Russell King4031bbe2006-01-06 11:41:00 +00001474static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 idefloppy_floppy_t *floppy = drive->driver_data;
1477 struct gendisk *g = floppy->disk;
1478
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001479 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 del_gendisk(g);
1482
1483 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Borislav Petkov9a24b632008-02-02 19:56:33 +01001486static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1489 ide_drive_t *drive = floppy->drive;
1490 struct gendisk *g = floppy->disk;
1491
1492 drive->driver_data = NULL;
1493 g->private_data = NULL;
1494 put_disk(g);
1495 kfree(floppy);
1496}
1497
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001498#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499static int proc_idefloppy_read_capacity
1500 (char *page, char **start, off_t off, int count, int *eof, void *data)
1501{
1502 ide_drive_t*drive = (ide_drive_t *)data;
1503 int len;
1504
1505 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1506 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1507}
1508
1509static ide_proc_entry_t idefloppy_proc[] = {
1510 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1511 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1512 { NULL, 0, NULL, NULL }
1513};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001514#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Russell King4031bbe2006-01-06 11:41:00 +00001516static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001519 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001520 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001521 .name = "ide-floppy",
1522 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001523 },
Russell King4031bbe2006-01-06 11:41:00 +00001524 .probe = ide_floppy_probe,
1525 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 .version = IDEFLOPPY_VERSION,
1527 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 .do_request = idefloppy_do_request,
1530 .end_request = idefloppy_do_end_request,
1531 .error = __ide_error,
1532 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001533#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001535#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536};
1537
1538static int idefloppy_open(struct inode *inode, struct file *filp)
1539{
1540 struct gendisk *disk = inode->i_bdev->bd_disk;
1541 struct ide_floppy_obj *floppy;
1542 ide_drive_t *drive;
1543 idefloppy_pc_t pc;
1544 int ret = 0;
1545
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001546 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 if (!(floppy = ide_floppy_get(disk)))
1549 return -ENXIO;
1550
1551 drive = floppy->drive;
1552
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001553 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001555 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1557 /* Just in case */
1558
1559 idefloppy_create_test_unit_ready_cmd(&pc);
1560 if (idefloppy_queue_pc_tail(drive, &pc)) {
1561 idefloppy_create_start_stop_cmd(&pc, 1);
1562 (void) idefloppy_queue_pc_tail(drive, &pc);
1563 }
1564
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001565 if (ide_floppy_get_capacity(drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 && (filp->f_flags & O_NDELAY) == 0
1567 /*
1568 ** Allow O_NDELAY to open a drive without a disk, or with
1569 ** an unreadable disk, so that we can get the format
1570 ** capacity of the drive or begin the format - Sam
1571 */
1572 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 ret = -EIO;
1574 goto out_put_floppy;
1575 }
1576
1577 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ret = -EROFS;
1579 goto out_put_floppy;
1580 }
1581 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1582 /* IOMEGA Clik! drives do not support lock/unlock commands */
1583 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1584 idefloppy_create_prevent_cmd(&pc, 1);
1585 (void) idefloppy_queue_pc_tail(drive, &pc);
1586 }
1587 check_disk_change(inode->i_bdev);
1588 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 ret = -EBUSY;
1590 goto out_put_floppy;
1591 }
1592 return 0;
1593
1594out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001595 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 ide_floppy_put(floppy);
1597 return ret;
1598}
1599
1600static int idefloppy_release(struct inode *inode, struct file *filp)
1601{
1602 struct gendisk *disk = inode->i_bdev->bd_disk;
1603 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1604 ide_drive_t *drive = floppy->drive;
1605 idefloppy_pc_t pc;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001606
1607 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001609 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* IOMEGA Clik! drives do not support lock/unlock commands */
1611 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1612 idefloppy_create_prevent_cmd(&pc, 0);
1613 (void) idefloppy_queue_pc_tail(drive, &pc);
1614 }
1615
1616 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1617 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001618
1619 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 ide_floppy_put(floppy);
1622
1623 return 0;
1624}
1625
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001626static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1627{
1628 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1629 ide_drive_t *drive = floppy->drive;
1630
1631 geo->heads = drive->bios_head;
1632 geo->sectors = drive->bios_sect;
1633 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1634 return 0;
1635}
1636
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001637static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
1638 unsigned long arg, unsigned int cmd)
1639{
1640 if (floppy->openers > 1)
1641 return -EBUSY;
1642
1643 /* The IOMEGA Clik! Drive doesn't support this command -
1644 * no room for an eject mechanism */
1645 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1646 int prevent = arg ? 1 : 0;
1647
1648 if (cmd == CDROMEJECT)
1649 prevent = 0;
1650
1651 idefloppy_create_prevent_cmd(pc, prevent);
1652 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1653 }
1654
1655 if (cmd == CDROMEJECT) {
1656 idefloppy_create_start_stop_cmd(pc, 2);
1657 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1658 }
1659
1660 return 0;
1661}
1662
1663static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1664 int __user *arg)
1665{
1666 int blocks, length, flags, err = 0;
1667 idefloppy_pc_t pc;
1668
1669 if (floppy->openers > 1) {
1670 /* Don't format if someone is using the disk */
1671 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1672 return -EBUSY;
1673 }
1674
1675 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1676
1677 /*
1678 * Send ATAPI_FORMAT_UNIT to the drive.
1679 *
1680 * Userland gives us the following structure:
1681 *
1682 * struct idefloppy_format_command {
1683 * int nblocks;
1684 * int blocksize;
1685 * int flags;
1686 * } ;
1687 *
1688 * flags is a bitmask, currently, the only defined flag is:
1689 *
1690 * 0x01 - verify media after format.
1691 */
1692 if (get_user(blocks, arg) ||
1693 get_user(length, arg+1) ||
1694 get_user(flags, arg+2)) {
1695 err = -EFAULT;
1696 goto out;
1697 }
1698
1699 (void) idefloppy_get_sfrp_bit(floppy->drive);
1700 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1701
1702 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1703 err = -EIO;
1704
1705out:
1706 if (err)
1707 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1708 return err;
1709}
1710
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static int idefloppy_ioctl(struct inode *inode, struct file *file,
1713 unsigned int cmd, unsigned long arg)
1714{
1715 struct block_device *bdev = inode->i_bdev;
1716 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1717 ide_drive_t *drive = floppy->drive;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001718 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001720 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 switch (cmd) {
1723 case CDROMEJECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /* fall through */
1725 case CDROM_LOCKDOOR:
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001726 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1728 return 0;
1729 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001730 return ide_floppy_get_format_capacities(drive, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 case IDEFLOPPY_IOCTL_FORMAT_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (!(file->f_mode & 2))
1733 return -EPERM;
1734
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001735 return ide_floppy_format_unit(floppy, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1737 return idefloppy_get_format_progress(drive, argp);
1738 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001739
1740 /*
1741 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1742 * and CDROM_SEND_PACKET (legacy) ioctls
1743 */
1744 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1745 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1746 bdev->bd_disk, cmd, argp);
1747 else
1748 err = -ENOTTY;
1749
1750 if (err == -ENOTTY)
1751 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1752
1753 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756static int idefloppy_media_changed(struct gendisk *disk)
1757{
1758 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1759 ide_drive_t *drive = floppy->drive;
1760
1761 /* do not scan partitions twice if this is a removable device */
1762 if (drive->attach) {
1763 drive->attach = 0;
1764 return 0;
1765 }
1766 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1767}
1768
1769static int idefloppy_revalidate_disk(struct gendisk *disk)
1770{
1771 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1772 set_capacity(disk, idefloppy_capacity(floppy->drive));
1773 return 0;
1774}
1775
1776static struct block_device_operations idefloppy_ops = {
1777 .owner = THIS_MODULE,
1778 .open = idefloppy_open,
1779 .release = idefloppy_release,
1780 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001781 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 .media_changed = idefloppy_media_changed,
1783 .revalidate_disk= idefloppy_revalidate_disk
1784};
1785
Russell King4031bbe2006-01-06 11:41:00 +00001786static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 idefloppy_floppy_t *floppy;
1789 struct gendisk *g;
1790
1791 if (!strstr("ide-floppy", drive->driver_req))
1792 goto failed;
1793 if (!drive->present)
1794 goto failed;
1795 if (drive->media != ide_floppy)
1796 goto failed;
1797 if (!idefloppy_identify_device (drive, drive->id)) {
1798 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1799 goto failed;
1800 }
1801 if (drive->scsi) {
1802 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1803 goto failed;
1804 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001805 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1807 goto failed;
1808 }
1809
1810 g = alloc_disk(1 << PARTN_BITS);
1811 if (!g)
1812 goto out_free_floppy;
1813
1814 ide_init_disk(g, drive);
1815
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001816 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 kref_init(&floppy->kref);
1819
1820 floppy->drive = drive;
1821 floppy->driver = &idefloppy_driver;
1822 floppy->disk = g;
1823
1824 g->private_data = &floppy->driver;
1825
1826 drive->driver_data = floppy;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 idefloppy_setup (drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 g->minors = 1 << PARTN_BITS;
1831 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1833 g->fops = &idefloppy_ops;
1834 drive->attach = 1;
1835 add_disk(g);
1836 return 0;
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838out_free_floppy:
1839 kfree(floppy);
1840failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001841 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1845
1846static void __exit idefloppy_exit (void)
1847{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001848 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001851static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001854 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Kay Sievers263756e2005-12-12 18:03:44 +01001857MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858module_init(idefloppy_init);
1859module_exit(idefloppy_exit);
1860MODULE_LICENSE("GPL");