blob: 78afc493e1fdc3309756335614365349c0ea30cd [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
57#define IDEFLOPPY_DEBUG( fmt, args... )
58
59#if IDEFLOPPY_DEBUG_LOG
Borislav Petkovbcc77d92008-02-02 19:56:34 +010060#define debug_log(fmt, args...) \
61 printk(KERN_INFO "ide-floppy: " fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
63#define debug_log(fmt, args... ) do {} while(0)
64#endif
65
66
67/*
68 * Some drives require a longer irq timeout.
69 */
70#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
71
72/*
73 * After each failed packet command we issue a request sense command
74 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
75 */
76#define IDEFLOPPY_MAX_PC_RETRIES 3
77
78/*
79 * With each packet command, we allocate a buffer of
80 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
81 */
82#define IDEFLOPPY_PC_BUFFER_SIZE 256
83
84/*
85 * In various places in the driver, we need to allocate storage
86 * for packet commands and requests, which will remain valid while
87 * we leave the driver to wait for an interrupt or a timeout event.
88 */
89#define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
90
91/*
92 * Our view of a packet command.
93 */
94typedef struct idefloppy_packet_command_s {
95 u8 c[12]; /* Actual packet bytes */
96 int retries; /* On each retry, we increment retries */
97 int error; /* Error code */
98 int request_transfer; /* Bytes to transfer */
99 int actually_transferred; /* Bytes actually transferred */
100 int buffer_size; /* Size of our data buffer */
101 int b_count; /* Missing/Available data on the current buffer */
102 struct request *rq; /* The corresponding request */
103 u8 *buffer; /* Data buffer */
104 u8 *current_position; /* Pointer into the above buffer */
105 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
106 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
107 unsigned long flags; /* Status/Action bit flags: long for set_bit */
108} idefloppy_pc_t;
109
110/*
111 * Packet command flag bits.
112 */
113#define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
114#define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
115#define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
116#define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
117#define PC_WRITING 5 /* Data direction */
118
119#define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
120
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100121/* format capacities descriptor codes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define CAPACITY_INVALID 0x00
123#define CAPACITY_UNFORMATTED 0x01
124#define CAPACITY_CURRENT 0x02
125#define CAPACITY_NO_CARTRIDGE 0x03
126
127/*
128 * Most of our global data which we need to save even as we leave the
129 * driver due to an interrupt or a timer event is stored in a variable
130 * of type idefloppy_floppy_t, defined below.
131 */
132typedef struct ide_floppy_obj {
133 ide_drive_t *drive;
134 ide_driver_t *driver;
135 struct gendisk *disk;
136 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100137 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Current packet command */
140 idefloppy_pc_t *pc;
141 /* Last failed packet command */
142 idefloppy_pc_t *failed_pc;
143 /* Packet command stack */
144 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
145 /* Next free packet command storage space */
146 int pc_stack_index;
147 struct request rq_stack[IDEFLOPPY_PC_STACK];
148 /* We implement a circular array */
149 int rq_stack_index;
150
151 /*
152 * Last error information
153 */
154 u8 sense_key, asc, ascq;
155 /* delay this long before sending packet command */
156 u8 ticks;
157 int progress_indication;
158
159 /*
160 * Device information
161 */
162 /* Current format */
163 int blocks, block_size, bs_factor;
Borislav Petkov194ec0c2008-02-02 19:56:35 +0100164 /* Last format capacity descriptor */
165 u8 cap_desc[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* Copy of the flexible disk page */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +0100167 u8 flexible_disk_page[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* Write protect */
169 int wp;
170 /* Supports format progress report */
171 int srfp;
172 /* Status/Action flags */
173 unsigned long flags;
174} idefloppy_floppy_t;
175
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200176#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/*
179 * Floppy flag bits values.
180 */
181#define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
182#define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
183#define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
184#define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
185#define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
186#define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
187
188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * Defines for the mode sense command
190 */
191#define MODE_SENSE_CURRENT 0x00
192#define MODE_SENSE_CHANGEABLE 0x01
193#define MODE_SENSE_DEFAULT 0x02
194#define MODE_SENSE_SAVED 0x03
195
196/*
197 * IOCTLs used in low-level formatting.
198 */
199
200#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
201#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
202#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
203#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * Error codes which are returned in rq->errors to the higher part
207 * of the driver.
208 */
209#define IDEFLOPPY_ERROR_GENERAL 101
210
211/*
212 * The following is used to format the general configuration word of
213 * the ATAPI IDENTIFY DEVICE command.
214 */
215struct idefloppy_id_gcw {
216#if defined(__LITTLE_ENDIAN_BITFIELD)
217 unsigned packet_size :2; /* Packet Size */
218 unsigned reserved234 :3; /* Reserved */
219 unsigned drq_type :2; /* Command packet DRQ type */
220 unsigned removable :1; /* Removable media */
221 unsigned device_type :5; /* Device type */
222 unsigned reserved13 :1; /* Reserved */
223 unsigned protocol :2; /* Protocol type */
224#elif defined(__BIG_ENDIAN_BITFIELD)
225 unsigned protocol :2; /* Protocol type */
226 unsigned reserved13 :1; /* Reserved */
227 unsigned device_type :5; /* Device type */
228 unsigned removable :1; /* Removable media */
229 unsigned drq_type :2; /* Command packet DRQ type */
230 unsigned reserved234 :3; /* Reserved */
231 unsigned packet_size :2; /* Packet Size */
232#else
233#error "Bitfield endianness not defined! Check your byteorder.h"
234#endif
235};
236
237/*
Borislav Petkov948391d2008-02-02 19:56:34 +0100238 * Pages of the SELECT SENSE / MODE SENSE packet commands.
239 * See SFF-8070i spec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
242#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
243
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800244static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
247
248#define ide_floppy_g(disk) \
249 container_of((disk)->private_data, struct ide_floppy_obj, driver)
250
251static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
252{
253 struct ide_floppy_obj *floppy = NULL;
254
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800255 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 floppy = ide_floppy_g(disk);
257 if (floppy)
258 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800259 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return floppy;
261}
262
Borislav Petkov9a24b632008-02-02 19:56:33 +0100263static void idefloppy_cleanup_obj(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265static void ide_floppy_put(struct ide_floppy_obj *floppy)
266{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800267 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100268 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800269 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/*
273 * Too bad. The drive wants to send us data which we are not ready to accept.
274 * Just throw it away.
275 */
276static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
277{
278 while (bcount--)
279 (void) HWIF(drive)->INB(IDE_DATA_REG);
280}
281
Borislav Petkov0bf399e2008-02-02 19:56:36 +0100282static void idefloppy_write_zeros(ide_drive_t *drive, unsigned int bcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 while (bcount--)
285 HWIF(drive)->OUTB(0, IDE_DATA_REG);
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288
289/*
290 * idefloppy_do_end_request is used to finish servicing a request.
291 *
292 * For read/write requests, we will call ide_end_request to pass to the
293 * next buffer.
294 */
295static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
296{
297 idefloppy_floppy_t *floppy = drive->driver_data;
298 struct request *rq = HWGROUP(drive)->rq;
299 int error;
300
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100301 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 switch (uptodate) {
304 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
305 case 1: error = 0; break;
306 default: error = uptodate;
307 }
308 if (error)
309 floppy->failed_pc = NULL;
310 /* Why does this happen? */
311 if (!rq)
312 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200313 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* our real local end request function */
315 ide_end_request(drive, uptodate, nsecs);
316 return 0;
317 }
318 rq->errors = error;
319 /* fixme: need to move this local also */
320 ide_end_drive_cmd(drive, 0, 0);
321 return 0;
322}
323
324static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
325{
326 struct request *rq = pc->rq;
327 struct bio_vec *bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200328 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned long flags;
330 char *data;
NeilBrown5705f702007-09-25 12:35:59 +0200331 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
NeilBrown5705f702007-09-25 12:35:59 +0200333 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200334 if (!bcount)
335 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Jens Axboe6c92e692007-08-16 13:43:12 +0200337 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jens Axboe6c92e692007-08-16 13:43:12 +0200339 data = bvec_kmap_irq(bvec, &flags);
340 drive->hwif->atapi_input_bytes(drive, data, count);
341 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jens Axboe6c92e692007-08-16 13:43:12 +0200343 bcount -= count;
344 pc->b_count += count;
345 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 idefloppy_do_end_request(drive, 1, done >> 9);
349
350 if (bcount) {
351 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
352 idefloppy_discard_data(drive, bcount);
353 }
354}
355
356static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
357{
358 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200359 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct bio_vec *bvec;
361 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200362 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 char *data;
364
NeilBrown5705f702007-09-25 12:35:59 +0200365 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200366 if (!bcount)
367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Jens Axboe6c92e692007-08-16 13:43:12 +0200369 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jens Axboe6c92e692007-08-16 13:43:12 +0200371 data = bvec_kmap_irq(bvec, &flags);
372 drive->hwif->atapi_output_bytes(drive, data, count);
373 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jens Axboe6c92e692007-08-16 13:43:12 +0200375 bcount -= count;
376 pc->b_count += count;
377 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
380 idefloppy_do_end_request(drive, 1, done >> 9);
381
382 if (bcount) {
383 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
384 idefloppy_write_zeros(drive, bcount);
385 }
386}
387
388static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
389{
390 struct request *rq = pc->rq;
391 struct bio *bio = rq->bio;
392
393 while ((bio = rq->bio) != NULL)
394 idefloppy_do_end_request(drive, 1, 0);
395}
396
397/*
398 * idefloppy_queue_pc_head generates a new packet command request in front
399 * of the request queue, before the current request, so that it will be
400 * processed immediately, on the next pass through the driver.
401 */
402static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
403{
404 struct ide_floppy_obj *floppy = drive->driver_data;
405
406 ide_init_drive_cmd(rq);
407 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200408 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rq->rq_disk = floppy->disk;
410 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
411}
412
413static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
414{
415 idefloppy_floppy_t *floppy = drive->driver_data;
416
417 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
418 floppy->pc_stack_index=0;
419 return (&floppy->pc_stack[floppy->pc_stack_index++]);
420}
421
422static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
423{
424 idefloppy_floppy_t *floppy = drive->driver_data;
425
426 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
427 floppy->rq_stack_index = 0;
428 return (&floppy->rq_stack[floppy->rq_stack_index++]);
429}
430
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100431static void idefloppy_request_sense_callback(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100434 u8 *buf = floppy->pc->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100436 debug_log("Reached %s\n", __func__);
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!floppy->pc->error) {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100439 floppy->sense_key = buf[2] & 0x0F;
440 floppy->asc = buf[12];
441 floppy->ascq = buf[13];
442 floppy->progress_indication = buf[15] & 0x80 ?
443 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
444
445 if (floppy->failed_pc)
446 debug_log("pc = %x, sense key = %x, asc = %x,"
447 " ascq = %x\n",
448 floppy->failed_pc->c[0],
449 floppy->sense_key,
450 floppy->asc,
451 floppy->ascq);
452 else
453 debug_log("sense key = %x, asc = %x, ascq = %x\n",
454 floppy->sense_key,
455 floppy->asc,
456 floppy->ascq);
457
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 idefloppy_do_end_request(drive, 1, 0);
460 } else {
Borislav Petkova6ff2d32008-02-02 19:56:34 +0100461 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
462 " request!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 idefloppy_do_end_request(drive, 0, 0);
464 }
465}
466
467/*
468 * General packet command callback function.
469 */
470static void idefloppy_pc_callback (ide_drive_t *drive)
471{
472 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100473
474 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
477}
478
479/*
480 * idefloppy_init_pc initializes a packet command.
481 */
482static void idefloppy_init_pc (idefloppy_pc_t *pc)
483{
484 memset(pc->c, 0, 12);
485 pc->retries = 0;
486 pc->flags = 0;
487 pc->request_transfer = 0;
488 pc->buffer = pc->pc_buffer;
489 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
490 pc->callback = &idefloppy_pc_callback;
491}
492
493static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
494{
Borislav Petkov30d67092008-02-02 19:56:33 +0100495 idefloppy_init_pc(pc);
496 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 pc->c[4] = 255;
498 pc->request_transfer = 18;
499 pc->callback = &idefloppy_request_sense_callback;
500}
501
502/*
503 * idefloppy_retry_pc is called when an error was detected during the
504 * last packet command. We queue a request sense packet command in
505 * the head of the request list.
506 */
507static void idefloppy_retry_pc (ide_drive_t *drive)
508{
509 idefloppy_pc_t *pc;
510 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100512 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 pc = idefloppy_next_pc_storage(drive);
514 rq = idefloppy_next_rq_storage(drive);
515 idefloppy_create_request_sense_cmd(pc);
516 idefloppy_queue_pc_head(drive, pc, rq);
517}
518
Borislav Petkov0eea6452008-02-02 19:56:36 +0100519typedef void (io_buf_t)(ide_drive_t *, idefloppy_pc_t *, unsigned int);
520
521/* The usual interrupt handler called during a packet command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
523{
524 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100525 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 idefloppy_pc_t *pc = floppy->pc;
527 struct request *rq = pc->rq;
Borislav Petkov0eea6452008-02-02 19:56:36 +0100528 xfer_func_t *xferfunc;
529 io_buf_t *iobuf_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 unsigned int temp;
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100531 int dma_error = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100532 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100533 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100535 debug_log("Reached %s interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Borislav Petkovd30a7fb2008-02-02 19:56:35 +0100538 dma_error = hwif->ide_dma_end(drive);
539 if (dma_error) {
540 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
541 rq_data_dir(rq) ? "write" : "read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 set_bit(PC_DMA_ERROR, &pc->flags);
543 } else {
544 pc->actually_transferred = pc->request_transfer;
545 idefloppy_update_buffers(drive, pc);
546 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100547 debug_log("DMA finished\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100551 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100553 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100554 debug_log("Packet command completed, %d bytes transferred\n",
555 pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
557
Ingo Molnar366c7f52006-07-03 00:25:25 -0700558 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100560 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Error detected */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100562 debug_log("%s: I/O error\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 rq->errors++;
Borislav Petkov30d67092008-02-02 19:56:33 +0100564 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 printk(KERN_ERR "ide-floppy: I/O error in "
566 "request sense command\n");
567 return ide_do_reset(drive);
568 }
569 /* Retry operation */
570 idefloppy_retry_pc(drive);
571 /* queued, but not started */
572 return ide_stopped;
573 }
574 pc->error = 0;
575 if (floppy->failed_pc == pc)
576 floppy->failed_pc = NULL;
577 /* Command finished - Call the callback function */
578 pc->callback(drive);
579 return ide_stopped;
580 }
581
582 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
583 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
584 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100585 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return ide_do_reset(drive);
587 }
588
589 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100590 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
591 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* on this interrupt */
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100593 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100595 if (ireason & CD) {
Borislav Petkov0eea6452008-02-02 19:56:36 +0100596 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return ide_do_reset(drive);
598 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100599 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* Hopefully, we will never get here */
601 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100602 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 printk(KERN_ERR "but the floppy wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100604 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return ide_do_reset(drive);
606 }
607 if (!test_bit(PC_WRITING, &pc->flags)) {
608 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100609 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (temp > pc->request_transfer) {
611 if (temp > pc->buffer_size) {
612 printk(KERN_ERR "ide-floppy: The floppy wants "
613 "to send us more data than expected "
614 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100615 idefloppy_discard_data(drive, bcount);
Borislav Petkov0078df22008-02-02 19:56:33 +0100616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ide_set_handler(drive,
618 &idefloppy_pc_intr,
619 IDEFLOPPY_WAIT_CMD,
620 NULL);
621 return ide_started;
622 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100623 debug_log("The floppy wants to send us more data than"
624 " expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 }
627 if (test_bit(PC_WRITING, &pc->flags)) {
Borislav Petkov0eea6452008-02-02 19:56:36 +0100628 xferfunc = hwif->atapi_output_bytes;
629 iobuf_func = &idefloppy_output_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 } else {
Borislav Petkov0eea6452008-02-02 19:56:36 +0100631 xferfunc = hwif->atapi_input_bytes;
632 iobuf_func = &idefloppy_input_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Borislav Petkov0eea6452008-02-02 19:56:36 +0100634
635 if (pc->buffer)
636 xferfunc(drive, pc->current_position, bcount);
637 else
638 iobuf_func(drive, pc, bcount);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100641 pc->actually_transferred += bcount;
642 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
645 return ide_started;
646}
647
648/*
649 * This is the original routine that did the packet transfer.
650 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
651 * for that drive below. The algorithm is chosen based on drive type
652 */
653static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
654{
655 ide_startstop_t startstop;
656 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100657 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
660 printk(KERN_ERR "ide-floppy: Strange, packet command "
661 "initiated yet DRQ isn't asserted\n");
662 return startstop;
663 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100664 ireason = drive->hwif->INB(IDE_IREASON_REG);
665 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
667 "issuing a packet command\n");
668 return ide_do_reset(drive);
669 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Set the interrupt routine */
672 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
673 /* Send the actual packet */
674 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
675 return ide_started;
676}
677
678
679/*
680 * What we have here is a classic case of a top half / bottom half
681 * interrupt service routine. In interrupt mode, the device sends
682 * an interrupt to signal it's ready to receive a packet. However,
683 * we need to delay about 2-3 ticks before issuing the packet or we
684 * gets in trouble.
685 *
686 * So, follow carefully. transfer_pc1 is called as an interrupt (or
687 * directly). In either case, when the device says it's ready for a
688 * packet, we schedule the packet transfer to occur about 2-3 ticks
689 * later in transfer_pc2.
690 */
691static int idefloppy_transfer_pc2 (ide_drive_t *drive)
692{
693 idefloppy_floppy_t *floppy = drive->driver_data;
694
695 /* Send the actual packet */
696 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
697 /* Timeout for the packet command */
698 return IDEFLOPPY_WAIT_CMD;
699}
700
701static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
702{
703 idefloppy_floppy_t *floppy = drive->driver_data;
704 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100705 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
708 printk(KERN_ERR "ide-floppy: Strange, packet command "
709 "initiated yet DRQ isn't asserted\n");
710 return startstop;
711 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100712 ireason = drive->hwif->INB(IDE_IREASON_REG);
713 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
715 "while issuing a packet command\n");
716 return ide_do_reset(drive);
717 }
718 /*
719 * The following delay solves a problem with ATAPI Zip 100 drives
720 * where the Busy flag was apparently being deasserted before the
721 * unit was ready to receive data. This was happening on a
722 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
723 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
724 * used until after the packet is moved in about 50 msec.
725 */
Borislav Petkov0078df22008-02-02 19:56:33 +0100726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 ide_set_handler(drive,
728 &idefloppy_pc_intr, /* service routine for packet command */
729 floppy->ticks, /* wait this long before "failing" */
730 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
731 return ide_started;
732}
733
Borislav Petkovd652c132008-02-02 19:56:35 +0100734static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
735 idefloppy_pc_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Borislav Petkovd652c132008-02-02 19:56:35 +0100737 /* supress error messages resulting from Medium not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (floppy->sense_key == 0x02 &&
739 floppy->asc == 0x3a &&
740 floppy->ascq == 0x00)
Borislav Petkovd652c132008-02-02 19:56:35 +0100741 return;
742
743 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
744 "asc = %2x, ascq = %2x\n",
745 floppy->drive->name, pc->c[0], floppy->sense_key,
746 floppy->asc, floppy->ascq);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/*
751 * Issue a packet command
752 */
753static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
754{
755 idefloppy_floppy_t *floppy = drive->driver_data;
756 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100758 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100759 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100762 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 floppy->failed_pc = pc;
764 /* Set the current packet command */
765 floppy->pc = pc;
766
767 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
768 test_bit(PC_ABORT, &pc->flags)) {
769 /*
770 * We will "abort" retrying a packet command in case
771 * a legitimate error code was received.
772 */
773 if (!test_bit(PC_ABORT, &pc->flags)) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100774 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags))
775 ide_floppy_report_error(floppy, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Giving up */
777 pc->error = IDEFLOPPY_ERROR_GENERAL;
778 }
779 floppy->failed_pc = NULL;
780 pc->callback(drive);
781 return ide_stopped;
782 }
783
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100784 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 pc->retries++;
787 /* We haven't transferred any data yet */
788 pc->actually_transferred = 0;
789 pc->current_position = pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100790 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100792 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
793 ide_dma_off(drive);
794
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100795 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100798 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100800 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
801 IDE_TFLAG_OUT_DEVICE, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100803 if (dma) { /* Begin DMA, if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
805 hwif->dma_start(drive);
806 }
807
808 /* Can we transfer the packet when we get the interrupt or wait? */
809 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
810 /* wait */
811 pkt_xfer_routine = &idefloppy_transfer_pc1;
812 } else {
813 /* immediate */
814 pkt_xfer_routine = &idefloppy_transfer_pc;
815 }
816
817 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
818 /* Issue the packet command */
819 ide_execute_command(drive, WIN_PACKETCMD,
820 pkt_xfer_routine,
821 IDEFLOPPY_WAIT_CMD,
822 NULL);
823 return ide_started;
824 } else {
825 /* Issue the packet command */
826 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
827 return (*pkt_xfer_routine) (drive);
828 }
829}
830
831static void idefloppy_rw_callback (ide_drive_t *drive)
832{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100833 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 idefloppy_do_end_request(drive, 1, 0);
836 return;
837}
838
839static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
840{
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100841 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100844 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 pc->c[4] = prevent;
846}
847
848static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
849{
850 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100851 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 pc->c[7] = 255;
853 pc->c[8] = 255;
854 pc->request_transfer = 255;
855}
856
857static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
858 int flags)
859{
860 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100861 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 pc->c[1] = 0x17;
863
864 memset(pc->buffer, 0, 12);
865 pc->buffer[1] = 0xA2;
866 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
867
868 if (flags & 1) /* Verify bit on... */
869 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
870 pc->buffer[3] = 8;
871
Borislav Petkov8f622432008-02-02 19:56:33 +0100872 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
873 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 pc->buffer_size=12;
875 set_bit(PC_WRITING, &pc->flags);
876}
877
878/*
879 * A mode sense command is used to "sense" floppy parameters.
880 */
881static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
882{
Borislav Petkov24a5d702008-02-02 19:56:34 +0100883 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100886 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 pc->c[1] = 0;
888 pc->c[2] = page_code + (type << 6);
889
890 switch (page_code) {
891 case IDEFLOPPY_CAPABILITIES_PAGE:
892 length += 12;
893 break;
894 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
895 length += 32;
896 break;
897 default:
898 printk(KERN_ERR "ide-floppy: unsupported page code "
899 "in create_mode_sense_cmd\n");
900 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100901 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 pc->request_transfer = length;
903}
904
905static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
906{
907 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100908 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 pc->c[4] = start;
910}
911
912static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
913{
914 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +0100915 pc->c[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
919{
920 int block = sector / floppy->bs_factor;
921 int blocks = rq->nr_sectors / floppy->bs_factor;
922 int cmd = rq_data_dir(rq);
923
924 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
925 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
926 block, blocks);
927
928 idefloppy_init_pc(pc);
929 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
Borislav Petkov30d67092008-02-02 19:56:33 +0100930 pc->c[0] = cmd == READ ? GPCMD_READ_12 : GPCMD_WRITE_12;
Borislav Petkov8f622432008-02-02 19:56:33 +0100931 put_unaligned(cpu_to_be32(blocks), (unsigned int *) &pc->c[6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 } else {
Borislav Petkov30d67092008-02-02 19:56:33 +0100933 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
Borislav Petkov8f622432008-02-02 19:56:33 +0100934 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Borislav Petkov8f622432008-02-02 19:56:33 +0100936 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 pc->callback = &idefloppy_rw_callback;
938 pc->rq = rq;
939 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200940 if (rq->cmd_flags & REQ_RW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 set_bit(PC_WRITING, &pc->flags);
942 pc->buffer = NULL;
943 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
944 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
945}
946
Jens Axboe3d6392c2007-07-09 12:38:05 +0200947static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
949{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 idefloppy_init_pc(pc);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200951 pc->callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200953 pc->rq = rq;
954 pc->b_count = rq->data_len;
955 if (rq->data_len && rq_data_dir(rq) == WRITE)
956 set_bit(PC_WRITING, &pc->flags);
957 pc->buffer = rq->data;
958 if (rq->bio)
959 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
960
961 /*
962 * possibly problematic, doesn't look like ide-floppy correctly
963 * handled scattered requests if dma fails...
964 */
965 pc->request_transfer = pc->buffer_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * idefloppy_do_request is our request handling function.
970 */
971static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
972{
973 idefloppy_floppy_t *floppy = drive->driver_data;
974 idefloppy_pc_t *pc;
975 unsigned long block = (unsigned long)block_s;
976
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100977 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -0700978 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100979 rq->cmd_type, rq->errors);
980 debug_log("sector: %ld, nr_sectors: %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 "current_nr_sectors: %d\n", (long)rq->sector,
982 rq->nr_sectors, rq->current_nr_sectors);
983
984 if (rq->errors >= ERROR_MAX) {
Borislav Petkovd652c132008-02-02 19:56:35 +0100985 if (floppy->failed_pc)
986 ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 else
988 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
989 drive->name);
990 idefloppy_do_end_request(drive, 0, 0);
991 return ide_stopped;
992 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200993 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (((long)rq->sector % floppy->bs_factor) ||
995 (rq->nr_sectors % floppy->bs_factor)) {
996 printk("%s: unsupported r/w request size\n",
997 drive->name);
998 idefloppy_do_end_request(drive, 0, 0);
999 return ide_stopped;
1000 }
1001 pc = idefloppy_next_pc_storage(drive);
1002 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001003 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 pc = (idefloppy_pc_t *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001005 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001007 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 } else {
1009 blk_dump_rq_flags(rq,
1010 "ide-floppy: unsupported command in queue");
1011 idefloppy_do_end_request(drive, 0, 0);
1012 return ide_stopped;
1013 }
1014
1015 pc->rq = rq;
1016 return idefloppy_issue_pc(drive, pc);
1017}
1018
1019/*
1020 * idefloppy_queue_pc_tail adds a special packet command request to the
1021 * tail of the request queue, and waits for it to be serviced.
1022 */
1023static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1024{
1025 struct ide_floppy_obj *floppy = drive->driver_data;
1026 struct request rq;
1027
1028 ide_init_drive_cmd (&rq);
1029 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001030 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 rq.rq_disk = floppy->disk;
1032
1033 return ide_do_drive_cmd(drive, &rq, ide_wait);
1034}
1035
1036/*
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001037 * Look at the flexible disk page parameters. We ignore the CHS capacity
1038 * parameters and use the LBA parameters instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001040static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 idefloppy_floppy_t *floppy = drive->driver_data;
1043 idefloppy_pc_t pc;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001044 u8 *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int capacity, lba_capacity;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001046 u16 transfer_rate, sector_size, cyls, rpm;
1047 u8 heads, sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001049 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
1050 MODE_SENSE_CURRENT);
1051
1052 if (idefloppy_queue_pc_tail(drive, &pc)) {
1053 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
1054 " parameters\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 1;
1056 }
Borislav Petkov24a5d702008-02-02 19:56:34 +01001057 floppy->wp = !!(pc.buffer[3] & 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 set_disk_ro(floppy->disk, floppy->wp);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001059 page = &pc.buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001061 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
1062 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
1063 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
1064 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
1065 heads = pc.buffer[8 + 4];
1066 sectors = pc.buffer[8 + 5];
1067
1068 capacity = cyls * heads * sectors * sector_size;
1069
1070 if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1072 "%d sector size, %d rpm\n",
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001073 drive->name, capacity / 1024, cyls, heads,
1074 sectors, transfer_rate / 8, sector_size, rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001076 memcpy(&floppy->flexible_disk_page, page, 32);
1077 drive->bios_cyl = cyls;
1078 drive->bios_head = heads;
1079 drive->bios_sect = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 lba_capacity = floppy->blocks * floppy->block_size;
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (capacity < lba_capacity) {
1083 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1084 "bytes, but the drive only handles %d\n",
1085 drive->name, lba_capacity, capacity);
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001086 floppy->blocks = floppy->block_size ?
1087 capacity / floppy->block_size : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 return 0;
1090}
1091
Borislav Petkov948391d2008-02-02 19:56:34 +01001092static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 idefloppy_floppy_t *floppy = drive->driver_data;
1095 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 floppy->srfp = 0;
1098 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1099 MODE_SENSE_CURRENT);
1100
1101 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
Borislav Petkov948391d2008-02-02 19:56:34 +01001102 if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Borislav Petkov948391d2008-02-02 19:56:34 +01001105 floppy->srfp = pc.buffer[8 + 2] & 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return (0);
1107}
1108
1109/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001110 * Determine if a media is present in the floppy drive, and if so, its LBA
1111 * capacity.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001113static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 idefloppy_floppy_t *floppy = drive->driver_data;
1116 idefloppy_pc_t pc;
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001117 u8 *cap_desc;
1118 u8 header_len, desc_cnt;
1119 int i, rc = 1, blocks, length;
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 drive->bios_cyl = 0;
1122 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +01001123 floppy->blocks = 0;
1124 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 set_capacity(floppy->disk, 0);
1126
1127 idefloppy_create_read_capacity_cmd(&pc);
1128 if (idefloppy_queue_pc_tail(drive, &pc)) {
1129 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1130 return 1;
1131 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001132 header_len = pc.buffer[3];
1133 cap_desc = &pc.buffer[4];
1134 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001136 for (i = 0; i < desc_cnt; i++) {
1137 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001139 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1140 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1141
1142 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1143 i, blocks * length / 1024, blocks, length);
1144
1145 if (i)
1146 continue;
1147 /*
1148 * the code below is valid only for the 1st descriptor, ie i=0
1149 */
1150
1151 switch (pc.buffer[desc_start + 4] & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1153 case CAPACITY_UNFORMATTED:
1154 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1155 /*
1156 * If it is not a clik drive, break out
1157 * (maintains previous driver behaviour)
1158 */
1159 break;
1160 case CAPACITY_CURRENT:
1161 /* Normal Zip/LS-120 disks */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001162 if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1164 "sector size\n", drive->name,
1165 blocks * length / 1024, blocks, length);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001166 memcpy(&floppy->cap_desc, cap_desc, 8);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!length || length % 512) {
1169 printk(KERN_NOTICE "%s: %d bytes block size "
1170 "not supported\n", drive->name, length);
1171 } else {
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001172 floppy->blocks = blocks;
1173 floppy->block_size = length;
1174 floppy->bs_factor = length / 512;
1175 if (floppy->bs_factor != 1)
1176 printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 "512 bytes block size not "
1178 "fully supported\n",
1179 drive->name);
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001180 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 break;
1183 case CAPACITY_NO_CARTRIDGE:
1184 /*
1185 * This is a KERN_ERR so it appears on screen
1186 * for the user to see
1187 */
1188 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1189 break;
1190 case CAPACITY_INVALID:
1191 printk(KERN_ERR "%s: Invalid capacity for disk "
1192 "in drive\n", drive->name);
1193 break;
1194 }
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001195 debug_log("Descriptor 0 Code: %d\n",
1196 pc.buffer[desc_start + 4] & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
1199 /* Clik! disk does not support get_flexible_disk_page */
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001200 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
Borislav Petkov8e81bbb2008-02-02 19:56:35 +01001201 (void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1204 return rc;
1205}
1206
1207/*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001208 * Obtain the list of formattable capacities.
1209 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1210 * descriptors to userland, instead of our own structures.
1211 *
1212 * Userland gives us the following structure:
1213 *
1214 * struct idefloppy_format_capacities {
1215 * int nformats;
1216 * struct {
1217 * int nblocks;
1218 * int blocksize;
1219 * } formats[];
1220 * };
1221 *
1222 * userland initializes nformats to the number of allocated formats[] records.
1223 * On exit we set nformats to the number of records we've actually initialized.
1224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001226static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001228 idefloppy_pc_t pc;
1229 u8 header_len, desc_cnt;
1230 int i, blocks, length, u_array_size, u_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int __user *argp;
1232
1233 if (get_user(u_array_size, arg))
1234 return (-EFAULT);
1235
1236 if (u_array_size <= 0)
1237 return (-EINVAL);
1238
1239 idefloppy_create_read_capacity_cmd(&pc);
1240 if (idefloppy_queue_pc_tail(drive, &pc)) {
1241 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001242 return (-EIO);
1243 }
1244 header_len = pc.buffer[3];
1245 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 u_index = 0;
1248 argp = arg + 1;
1249
1250 /*
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001251 * We always skip the first capacity descriptor. That's the current
1252 * capacity. We are interested in the remaining descriptors, the
1253 * formattable capacities.
1254 */
1255 for (i = 1; i < desc_cnt; i++) {
1256 unsigned int desc_start = 4 + i*8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (u_index >= u_array_size)
1259 break; /* User-supplied buffer too small */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001261 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1262 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (put_user(blocks, argp))
1265 return(-EFAULT);
1266 ++argp;
1267
1268 if (put_user(length, argp))
1269 return (-EFAULT);
1270 ++argp;
1271
1272 ++u_index;
1273 }
1274
1275 if (put_user(u_index, arg))
1276 return (-EFAULT);
1277 return (0);
1278}
1279
1280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281** Get ATAPI_FORMAT_UNIT progress indication.
1282**
Matt LaPlante0779bf22006-11-30 05:24:39 +01001283** Userland gives a pointer to an int. The int is set to a progress
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284** indicator 0-65536, with 65536=100%.
1285**
1286** If the drive does not support format progress indication, we just check
1287** the dsc bit, and return either 0 or 65536.
1288*/
1289
1290static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1291{
1292 idefloppy_floppy_t *floppy = drive->driver_data;
1293 idefloppy_pc_t pc;
1294 int progress_indication = 0x10000;
1295
1296 if (floppy->srfp) {
1297 idefloppy_create_request_sense_cmd(&pc);
1298 if (idefloppy_queue_pc_tail(drive, &pc)) {
1299 return (-EIO);
1300 }
1301
1302 if (floppy->sense_key == 2 &&
1303 floppy->asc == 4 &&
1304 floppy->ascq == 4) {
1305 progress_indication = floppy->progress_indication;
1306 }
1307 /* Else assume format_unit has finished, and we're
1308 ** at 0x10000 */
1309 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001311 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 local_irq_save(flags);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001314 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 local_irq_restore(flags);
1316
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001317 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 if (put_user(progress_indication, arg))
1320 return (-EFAULT);
1321
1322 return (0);
1323}
1324
1325/*
1326 * Return the current floppy capacity.
1327 */
1328static sector_t idefloppy_capacity (ide_drive_t *drive)
1329{
1330 idefloppy_floppy_t *floppy = drive->driver_data;
1331 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1332
1333 return capacity;
1334}
1335
1336/*
1337 * idefloppy_identify_device checks if we can support a drive,
1338 * based on the ATAPI IDENTIFY command results.
1339 */
1340static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1341{
1342 struct idefloppy_id_gcw gcw;
1343#if IDEFLOPPY_DEBUG_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 char buffer[80];
1345#endif /* IDEFLOPPY_DEBUG_INFO */
1346
1347 *((u16 *) &gcw) = id->config;
1348
1349#ifdef CONFIG_PPC
1350 /* kludge for Apple PowerBook internal zip */
1351 if ((gcw.device_type == 5) &&
1352 !strstr(id->model, "CD-ROM") &&
1353 strstr(id->model, "ZIP"))
1354 gcw.device_type = 0;
1355#endif
1356
1357#if IDEFLOPPY_DEBUG_INFO
1358 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1359 switch (gcw.protocol) {
1360 case 0: case 1: sprintf(buffer, "ATA");break;
1361 case 2: sprintf(buffer, "ATAPI");break;
1362 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1363 }
1364 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1365 switch (gcw.device_type) {
1366 case 0: sprintf(buffer, "Direct-access Device");break;
1367 case 1: sprintf(buffer, "Streaming Tape Device");break;
1368 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1369 case 5: sprintf(buffer, "CD-ROM Device");break;
1370 case 6: sprintf(buffer, "Reserved");
1371 case 7: sprintf(buffer, "Optical memory Device");break;
1372 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1373 default: sprintf(buffer, "Reserved");
1374 }
1375 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1376 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1377 switch (gcw.drq_type) {
1378 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1379 case 1: sprintf(buffer, "Interrupt DRQ");break;
1380 case 2: sprintf(buffer, "Accelerated DRQ");break;
1381 case 3: sprintf(buffer, "Reserved");break;
1382 }
1383 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1384 switch (gcw.packet_size) {
1385 case 0: sprintf(buffer, "12 bytes");break;
1386 case 1: sprintf(buffer, "16 bytes");break;
1387 default: sprintf(buffer, "Reserved");break;
1388 }
1389 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390#endif /* IDEFLOPPY_DEBUG_INFO */
1391
1392 if (gcw.protocol != 2)
1393 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1394 else if (gcw.device_type != 0)
1395 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1396 else if (!gcw.removable)
1397 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1398 else if (gcw.drq_type == 3) {
1399 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1400 } else if (gcw.packet_size != 0) {
1401 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1402 } else
1403 return 1;
1404 return 0;
1405}
1406
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001407#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408static void idefloppy_add_settings(ide_drive_t *drive)
1409{
1410 idefloppy_floppy_t *floppy = drive->driver_data;
1411
1412/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001413 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001415 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1416 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1417 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1418 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001420#else
1421static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1422#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424/*
1425 * Driver initialization.
1426 */
1427static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1428{
1429 struct idefloppy_id_gcw gcw;
1430
1431 *((u16 *) &gcw) = drive->id->config;
1432 floppy->pc = floppy->pc_stack;
1433 if (gcw.drq_type == 1)
1434 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1435 /*
1436 * We used to check revisions here. At this point however
1437 * I'm giving up. Just assume they are all broken, its easier.
1438 *
1439 * The actual reason for the workarounds was likely
1440 * a driver bug after all rather than a firmware bug,
1441 * and the workaround below used to hide it. It should
1442 * be fixed as of version 1.9, but to be on the safe side
1443 * we'll leave the limitation below for the 2.2.x tree.
1444 */
1445
1446 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1447 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1448 /* This value will be visible in the /proc/ide/hdx/settings */
1449 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1450 blk_queue_max_sectors(drive->queue, 64);
1451 }
1452
1453 /*
1454 * Guess what? The IOMEGA Clik! drive also needs the
1455 * above fix. It makes nasty clicking noises without
1456 * it, so please don't remove this.
1457 */
1458 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1459 blk_queue_max_sectors(drive->queue, 64);
1460 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1461 }
1462
1463
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001464 (void) ide_floppy_get_capacity(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 idefloppy_add_settings(drive);
1466}
1467
Russell King4031bbe2006-01-06 11:41:00 +00001468static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 idefloppy_floppy_t *floppy = drive->driver_data;
1471 struct gendisk *g = floppy->disk;
1472
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001473 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 del_gendisk(g);
1476
1477 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Borislav Petkov9a24b632008-02-02 19:56:33 +01001480static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1483 ide_drive_t *drive = floppy->drive;
1484 struct gendisk *g = floppy->disk;
1485
1486 drive->driver_data = NULL;
1487 g->private_data = NULL;
1488 put_disk(g);
1489 kfree(floppy);
1490}
1491
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001492#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493static int proc_idefloppy_read_capacity
1494 (char *page, char **start, off_t off, int count, int *eof, void *data)
1495{
1496 ide_drive_t*drive = (ide_drive_t *)data;
1497 int len;
1498
1499 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1500 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1501}
1502
1503static ide_proc_entry_t idefloppy_proc[] = {
1504 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1505 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1506 { NULL, 0, NULL, NULL }
1507};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001508#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Russell King4031bbe2006-01-06 11:41:00 +00001510static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001513 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001514 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001515 .name = "ide-floppy",
1516 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001517 },
Russell King4031bbe2006-01-06 11:41:00 +00001518 .probe = ide_floppy_probe,
1519 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 .version = IDEFLOPPY_VERSION,
1521 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 .do_request = idefloppy_do_request,
1524 .end_request = idefloppy_do_end_request,
1525 .error = __ide_error,
1526 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001527#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001529#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530};
1531
1532static int idefloppy_open(struct inode *inode, struct file *filp)
1533{
1534 struct gendisk *disk = inode->i_bdev->bd_disk;
1535 struct ide_floppy_obj *floppy;
1536 ide_drive_t *drive;
1537 idefloppy_pc_t pc;
1538 int ret = 0;
1539
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001540 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 if (!(floppy = ide_floppy_get(disk)))
1543 return -ENXIO;
1544
1545 drive = floppy->drive;
1546
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001547 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001549 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1551 /* Just in case */
1552
1553 idefloppy_create_test_unit_ready_cmd(&pc);
1554 if (idefloppy_queue_pc_tail(drive, &pc)) {
1555 idefloppy_create_start_stop_cmd(&pc, 1);
1556 (void) idefloppy_queue_pc_tail(drive, &pc);
1557 }
1558
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001559 if (ide_floppy_get_capacity(drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 && (filp->f_flags & O_NDELAY) == 0
1561 /*
1562 ** Allow O_NDELAY to open a drive without a disk, or with
1563 ** an unreadable disk, so that we can get the format
1564 ** capacity of the drive or begin the format - Sam
1565 */
1566 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 ret = -EIO;
1568 goto out_put_floppy;
1569 }
1570
1571 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 ret = -EROFS;
1573 goto out_put_floppy;
1574 }
1575 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1576 /* IOMEGA Clik! drives do not support lock/unlock commands */
1577 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1578 idefloppy_create_prevent_cmd(&pc, 1);
1579 (void) idefloppy_queue_pc_tail(drive, &pc);
1580 }
1581 check_disk_change(inode->i_bdev);
1582 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ret = -EBUSY;
1584 goto out_put_floppy;
1585 }
1586 return 0;
1587
1588out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001589 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ide_floppy_put(floppy);
1591 return ret;
1592}
1593
1594static int idefloppy_release(struct inode *inode, struct file *filp)
1595{
1596 struct gendisk *disk = inode->i_bdev->bd_disk;
1597 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1598 ide_drive_t *drive = floppy->drive;
1599 idefloppy_pc_t pc;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001600
1601 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001603 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /* IOMEGA Clik! drives do not support lock/unlock commands */
1605 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1606 idefloppy_create_prevent_cmd(&pc, 0);
1607 (void) idefloppy_queue_pc_tail(drive, &pc);
1608 }
1609
1610 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1611 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001612
1613 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 ide_floppy_put(floppy);
1616
1617 return 0;
1618}
1619
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001620static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1621{
1622 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1623 ide_drive_t *drive = floppy->drive;
1624
1625 geo->heads = drive->bios_head;
1626 geo->sectors = drive->bios_sect;
1627 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1628 return 0;
1629}
1630
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001631static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
1632 unsigned long arg, unsigned int cmd)
1633{
1634 if (floppy->openers > 1)
1635 return -EBUSY;
1636
1637 /* The IOMEGA Clik! Drive doesn't support this command -
1638 * no room for an eject mechanism */
1639 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1640 int prevent = arg ? 1 : 0;
1641
1642 if (cmd == CDROMEJECT)
1643 prevent = 0;
1644
1645 idefloppy_create_prevent_cmd(pc, prevent);
1646 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1647 }
1648
1649 if (cmd == CDROMEJECT) {
1650 idefloppy_create_start_stop_cmd(pc, 2);
1651 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1652 }
1653
1654 return 0;
1655}
1656
1657static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1658 int __user *arg)
1659{
1660 int blocks, length, flags, err = 0;
1661 idefloppy_pc_t pc;
1662
1663 if (floppy->openers > 1) {
1664 /* Don't format if someone is using the disk */
1665 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1666 return -EBUSY;
1667 }
1668
1669 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1670
1671 /*
1672 * Send ATAPI_FORMAT_UNIT to the drive.
1673 *
1674 * Userland gives us the following structure:
1675 *
1676 * struct idefloppy_format_command {
1677 * int nblocks;
1678 * int blocksize;
1679 * int flags;
1680 * } ;
1681 *
1682 * flags is a bitmask, currently, the only defined flag is:
1683 *
1684 * 0x01 - verify media after format.
1685 */
1686 if (get_user(blocks, arg) ||
1687 get_user(length, arg+1) ||
1688 get_user(flags, arg+2)) {
1689 err = -EFAULT;
1690 goto out;
1691 }
1692
1693 (void) idefloppy_get_sfrp_bit(floppy->drive);
1694 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1695
1696 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1697 err = -EIO;
1698
1699out:
1700 if (err)
1701 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1702 return err;
1703}
1704
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706static int idefloppy_ioctl(struct inode *inode, struct file *file,
1707 unsigned int cmd, unsigned long arg)
1708{
1709 struct block_device *bdev = inode->i_bdev;
1710 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1711 ide_drive_t *drive = floppy->drive;
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001712 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001714 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 switch (cmd) {
1717 case CDROMEJECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /* fall through */
1719 case CDROM_LOCKDOOR:
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001720 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1722 return 0;
1723 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Borislav Petkov194ec0c2008-02-02 19:56:35 +01001724 return ide_floppy_get_format_capacities(drive, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 case IDEFLOPPY_IOCTL_FORMAT_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (!(file->f_mode & 2))
1727 return -EPERM;
1728
Borislav Petkov20bf7bd2008-02-02 19:56:35 +01001729 return ide_floppy_format_unit(floppy, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1731 return idefloppy_get_format_progress(drive, argp);
1732 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001733
1734 /*
1735 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1736 * and CDROM_SEND_PACKET (legacy) ioctls
1737 */
1738 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1739 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1740 bdev->bd_disk, cmd, argp);
1741 else
1742 err = -ENOTTY;
1743
1744 if (err == -ENOTTY)
1745 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1746
1747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
1750static int idefloppy_media_changed(struct gendisk *disk)
1751{
1752 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1753 ide_drive_t *drive = floppy->drive;
1754
1755 /* do not scan partitions twice if this is a removable device */
1756 if (drive->attach) {
1757 drive->attach = 0;
1758 return 0;
1759 }
1760 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1761}
1762
1763static int idefloppy_revalidate_disk(struct gendisk *disk)
1764{
1765 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1766 set_capacity(disk, idefloppy_capacity(floppy->drive));
1767 return 0;
1768}
1769
1770static struct block_device_operations idefloppy_ops = {
1771 .owner = THIS_MODULE,
1772 .open = idefloppy_open,
1773 .release = idefloppy_release,
1774 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001775 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 .media_changed = idefloppy_media_changed,
1777 .revalidate_disk= idefloppy_revalidate_disk
1778};
1779
Russell King4031bbe2006-01-06 11:41:00 +00001780static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 idefloppy_floppy_t *floppy;
1783 struct gendisk *g;
1784
1785 if (!strstr("ide-floppy", drive->driver_req))
1786 goto failed;
1787 if (!drive->present)
1788 goto failed;
1789 if (drive->media != ide_floppy)
1790 goto failed;
1791 if (!idefloppy_identify_device (drive, drive->id)) {
1792 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1793 goto failed;
1794 }
1795 if (drive->scsi) {
1796 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1797 goto failed;
1798 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001799 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1801 goto failed;
1802 }
1803
1804 g = alloc_disk(1 << PARTN_BITS);
1805 if (!g)
1806 goto out_free_floppy;
1807
1808 ide_init_disk(g, drive);
1809
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001810 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 kref_init(&floppy->kref);
1813
1814 floppy->drive = drive;
1815 floppy->driver = &idefloppy_driver;
1816 floppy->disk = g;
1817
1818 g->private_data = &floppy->driver;
1819
1820 drive->driver_data = floppy;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 idefloppy_setup (drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 g->minors = 1 << PARTN_BITS;
1825 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1827 g->fops = &idefloppy_ops;
1828 drive->attach = 1;
1829 add_disk(g);
1830 return 0;
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832out_free_floppy:
1833 kfree(floppy);
1834failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001835 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
1838MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1839
1840static void __exit idefloppy_exit (void)
1841{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001842 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001845static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001848 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Kay Sievers263756e2005-12-12 18:03:44 +01001851MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852module_init(idefloppy_init);
1853module_exit(idefloppy_exit);
1854MODULE_LICENSE("GPL");