blob: 2860398b193c9440a1436c90c59f1c9c873f4046 [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
122/*
123 * Removable Block Access Capabilities Page
124 */
125typedef struct {
126#if defined(__LITTLE_ENDIAN_BITFIELD)
127 unsigned page_code :6; /* Page code - Should be 0x1b */
128 unsigned reserved1_6 :1; /* Reserved */
129 unsigned ps :1; /* Should be 0 */
130#elif defined(__BIG_ENDIAN_BITFIELD)
131 unsigned ps :1; /* Should be 0 */
132 unsigned reserved1_6 :1; /* Reserved */
133 unsigned page_code :6; /* Page code - Should be 0x1b */
134#else
135#error "Bitfield endianness not defined! Check your byteorder.h"
136#endif
137 u8 page_length; /* Page Length - Should be 0xa */
138#if defined(__LITTLE_ENDIAN_BITFIELD)
139 unsigned reserved2 :6;
140 unsigned srfp :1; /* Supports reporting progress of format */
141 unsigned sflp :1; /* System floppy type device */
142 unsigned tlun :3; /* Total logical units supported by the device */
143 unsigned reserved3 :3;
144 unsigned sml :1; /* Single / Multiple lun supported */
145 unsigned ncd :1; /* Non cd optical device */
146#elif defined(__BIG_ENDIAN_BITFIELD)
147 unsigned sflp :1; /* System floppy type device */
148 unsigned srfp :1; /* Supports reporting progress of format */
149 unsigned reserved2 :6;
150 unsigned ncd :1; /* Non cd optical device */
151 unsigned sml :1; /* Single / Multiple lun supported */
152 unsigned reserved3 :3;
153 unsigned tlun :3; /* Total logical units supported by the device */
154#else
155#error "Bitfield endianness not defined! Check your byteorder.h"
156#endif
157 u8 reserved[8];
158} idefloppy_capabilities_page_t;
159
160/*
161 * Flexible disk page.
162 */
163typedef struct {
164#if defined(__LITTLE_ENDIAN_BITFIELD)
165 unsigned page_code :6; /* Page code - Should be 0x5 */
166 unsigned reserved1_6 :1; /* Reserved */
167 unsigned ps :1; /* The device is capable of saving the page */
168#elif defined(__BIG_ENDIAN_BITFIELD)
169 unsigned ps :1; /* The device is capable of saving the page */
170 unsigned reserved1_6 :1; /* Reserved */
171 unsigned page_code :6; /* Page code - Should be 0x5 */
172#else
173#error "Bitfield endianness not defined! Check your byteorder.h"
174#endif
175 u8 page_length; /* Page Length - Should be 0x1e */
176 u16 transfer_rate; /* In kilobits per second */
177 u8 heads, sectors; /* Number of heads, Number of sectors per track */
178 u16 sector_size; /* Byes per sector */
179 u16 cyls; /* Number of cylinders */
180 u8 reserved10[10];
181 u8 motor_delay; /* Motor off delay */
182 u8 reserved21[7];
183 u16 rpm; /* Rotations per minute */
184 u8 reserved30[2];
185} idefloppy_flexible_disk_page_t;
186
187/*
188 * Format capacity
189 */
190typedef struct {
191 u8 reserved[3];
192 u8 length; /* Length of the following descriptors in bytes */
193} idefloppy_capacity_header_t;
194
195typedef struct {
196 u32 blocks; /* Number of blocks */
197#if defined(__LITTLE_ENDIAN_BITFIELD)
198 unsigned dc :2; /* Descriptor Code */
199 unsigned reserved :6;
200#elif defined(__BIG_ENDIAN_BITFIELD)
201 unsigned reserved :6;
202 unsigned dc :2; /* Descriptor Code */
203#else
204#error "Bitfield endianness not defined! Check your byteorder.h"
205#endif
206 u8 length_msb; /* Block Length (MSB)*/
207 u16 length; /* Block Length */
208} idefloppy_capacity_descriptor_t;
209
210#define CAPACITY_INVALID 0x00
211#define CAPACITY_UNFORMATTED 0x01
212#define CAPACITY_CURRENT 0x02
213#define CAPACITY_NO_CARTRIDGE 0x03
214
215/*
216 * Most of our global data which we need to save even as we leave the
217 * driver due to an interrupt or a timer event is stored in a variable
218 * of type idefloppy_floppy_t, defined below.
219 */
220typedef struct ide_floppy_obj {
221 ide_drive_t *drive;
222 ide_driver_t *driver;
223 struct gendisk *disk;
224 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100225 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* Current packet command */
228 idefloppy_pc_t *pc;
229 /* Last failed packet command */
230 idefloppy_pc_t *failed_pc;
231 /* Packet command stack */
232 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
233 /* Next free packet command storage space */
234 int pc_stack_index;
235 struct request rq_stack[IDEFLOPPY_PC_STACK];
236 /* We implement a circular array */
237 int rq_stack_index;
238
239 /*
240 * Last error information
241 */
242 u8 sense_key, asc, ascq;
243 /* delay this long before sending packet command */
244 u8 ticks;
245 int progress_indication;
246
247 /*
248 * Device information
249 */
250 /* Current format */
251 int blocks, block_size, bs_factor;
252 /* Last format capacity */
253 idefloppy_capacity_descriptor_t capacity;
254 /* Copy of the flexible disk page */
255 idefloppy_flexible_disk_page_t flexible_disk_page;
256 /* Write protect */
257 int wp;
258 /* Supports format progress report */
259 int srfp;
260 /* Status/Action flags */
261 unsigned long flags;
262} idefloppy_floppy_t;
263
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200264#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266/*
267 * Floppy flag bits values.
268 */
269#define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
270#define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
271#define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
272#define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
273#define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
274#define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
275
276/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * Defines for the mode sense command
278 */
279#define MODE_SENSE_CURRENT 0x00
280#define MODE_SENSE_CHANGEABLE 0x01
281#define MODE_SENSE_DEFAULT 0x02
282#define MODE_SENSE_SAVED 0x03
283
284/*
285 * IOCTLs used in low-level formatting.
286 */
287
288#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
289#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
290#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
291#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * Error codes which are returned in rq->errors to the higher part
295 * of the driver.
296 */
297#define IDEFLOPPY_ERROR_GENERAL 101
298
299/*
300 * The following is used to format the general configuration word of
301 * the ATAPI IDENTIFY DEVICE command.
302 */
303struct idefloppy_id_gcw {
304#if defined(__LITTLE_ENDIAN_BITFIELD)
305 unsigned packet_size :2; /* Packet Size */
306 unsigned reserved234 :3; /* Reserved */
307 unsigned drq_type :2; /* Command packet DRQ type */
308 unsigned removable :1; /* Removable media */
309 unsigned device_type :5; /* Device type */
310 unsigned reserved13 :1; /* Reserved */
311 unsigned protocol :2; /* Protocol type */
312#elif defined(__BIG_ENDIAN_BITFIELD)
313 unsigned protocol :2; /* Protocol type */
314 unsigned reserved13 :1; /* Reserved */
315 unsigned device_type :5; /* Device type */
316 unsigned removable :1; /* Removable media */
317 unsigned drq_type :2; /* Command packet DRQ type */
318 unsigned reserved234 :3; /* Reserved */
319 unsigned packet_size :2; /* Packet Size */
320#else
321#error "Bitfield endianness not defined! Check your byteorder.h"
322#endif
323};
324
325/*
326 * INQUIRY packet command - Data Format
327 */
328typedef struct {
329#if defined(__LITTLE_ENDIAN_BITFIELD)
330 unsigned device_type :5; /* Peripheral Device Type */
331 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
332 unsigned reserved1_6t0 :7; /* Reserved */
333 unsigned rmb :1; /* Removable Medium Bit */
334 unsigned ansi_version :3; /* ANSI Version */
335 unsigned ecma_version :3; /* ECMA Version */
336 unsigned iso_version :2; /* ISO Version */
337 unsigned response_format :4; /* Response Data Format */
338 unsigned reserved3_45 :2; /* Reserved */
339 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
340 unsigned reserved3_7 :1; /* AENC - Reserved */
341#elif defined(__BIG_ENDIAN_BITFIELD)
342 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
343 unsigned device_type :5; /* Peripheral Device Type */
344 unsigned rmb :1; /* Removable Medium Bit */
345 unsigned reserved1_6t0 :7; /* Reserved */
346 unsigned iso_version :2; /* ISO Version */
347 unsigned ecma_version :3; /* ECMA Version */
348 unsigned ansi_version :3; /* ANSI Version */
349 unsigned reserved3_7 :1; /* AENC - Reserved */
350 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
351 unsigned reserved3_45 :2; /* Reserved */
352 unsigned response_format :4; /* Response Data Format */
353#else
354#error "Bitfield endianness not defined! Check your byteorder.h"
355#endif
356 u8 additional_length; /* Additional Length (total_length-4) */
357 u8 rsv5, rsv6, rsv7; /* Reserved */
358 u8 vendor_id[8]; /* Vendor Identification */
359 u8 product_id[16]; /* Product Identification */
360 u8 revision_level[4]; /* Revision Level */
361 u8 vendor_specific[20]; /* Vendor Specific - Optional */
362 u8 reserved56t95[40]; /* Reserved - Optional */
363 /* Additional information may be returned */
364} idefloppy_inquiry_result_t;
365
366/*
367 * REQUEST SENSE packet command result - Data Format.
368 */
369typedef struct {
370#if defined(__LITTLE_ENDIAN_BITFIELD)
371 unsigned error_code :7; /* Current error (0x70) */
372 unsigned valid :1; /* The information field conforms to SFF-8070i */
373 u8 reserved1 :8; /* Reserved */
374 unsigned sense_key :4; /* Sense Key */
375 unsigned reserved2_4 :1; /* Reserved */
376 unsigned ili :1; /* Incorrect Length Indicator */
377 unsigned reserved2_67 :2;
378#elif defined(__BIG_ENDIAN_BITFIELD)
379 unsigned valid :1; /* The information field conforms to SFF-8070i */
380 unsigned error_code :7; /* Current error (0x70) */
381 u8 reserved1 :8; /* Reserved */
382 unsigned reserved2_67 :2;
383 unsigned ili :1; /* Incorrect Length Indicator */
384 unsigned reserved2_4 :1; /* Reserved */
385 unsigned sense_key :4; /* Sense Key */
386#else
387#error "Bitfield endianness not defined! Check your byteorder.h"
388#endif
389 u32 information __attribute__ ((packed));
390 u8 asl; /* Additional sense length (n-7) */
391 u32 command_specific; /* Additional command specific information */
392 u8 asc; /* Additional Sense Code */
393 u8 ascq; /* Additional Sense Code Qualifier */
394 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
395 u8 sksv[3];
396 u8 pad[2]; /* Padding to 20 bytes */
397} idefloppy_request_sense_result_t;
398
399/*
400 * Pages of the SELECT SENSE / MODE SENSE packet commands.
401 */
402#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
403#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
404
405/*
406 * Mode Parameter Header for the MODE SENSE packet command
407 */
408typedef struct {
409 u16 mode_data_length; /* Length of the following data transfer */
410 u8 medium_type; /* Medium Type */
411#if defined(__LITTLE_ENDIAN_BITFIELD)
412 unsigned reserved3 :7;
413 unsigned wp :1; /* Write protect */
414#elif defined(__BIG_ENDIAN_BITFIELD)
415 unsigned wp :1; /* Write protect */
416 unsigned reserved3 :7;
417#else
418#error "Bitfield endianness not defined! Check your byteorder.h"
419#endif
420 u8 reserved[4];
421} idefloppy_mode_parameter_header_t;
422
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800423static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
426
427#define ide_floppy_g(disk) \
428 container_of((disk)->private_data, struct ide_floppy_obj, driver)
429
430static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
431{
432 struct ide_floppy_obj *floppy = NULL;
433
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800434 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 floppy = ide_floppy_g(disk);
436 if (floppy)
437 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800438 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return floppy;
440}
441
Borislav Petkov9a24b632008-02-02 19:56:33 +0100442static void idefloppy_cleanup_obj(struct kref *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444static void ide_floppy_put(struct ide_floppy_obj *floppy)
445{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800446 mutex_lock(&idefloppy_ref_mutex);
Borislav Petkov9a24b632008-02-02 19:56:33 +0100447 kref_put(&floppy->kref, idefloppy_cleanup_obj);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800448 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451/*
452 * Too bad. The drive wants to send us data which we are not ready to accept.
453 * Just throw it away.
454 */
455static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
456{
457 while (bcount--)
458 (void) HWIF(drive)->INB(IDE_DATA_REG);
459}
460
461#if IDEFLOPPY_DEBUG_BUGS
462static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
463{
464 while (bcount--)
465 HWIF(drive)->OUTB(0, IDE_DATA_REG);
466}
467#endif /* IDEFLOPPY_DEBUG_BUGS */
468
469
470/*
471 * idefloppy_do_end_request is used to finish servicing a request.
472 *
473 * For read/write requests, we will call ide_end_request to pass to the
474 * next buffer.
475 */
476static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
477{
478 idefloppy_floppy_t *floppy = drive->driver_data;
479 struct request *rq = HWGROUP(drive)->rq;
480 int error;
481
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100482 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 switch (uptodate) {
485 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
486 case 1: error = 0; break;
487 default: error = uptodate;
488 }
489 if (error)
490 floppy->failed_pc = NULL;
491 /* Why does this happen? */
492 if (!rq)
493 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200494 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* our real local end request function */
496 ide_end_request(drive, uptodate, nsecs);
497 return 0;
498 }
499 rq->errors = error;
500 /* fixme: need to move this local also */
501 ide_end_drive_cmd(drive, 0, 0);
502 return 0;
503}
504
505static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
506{
507 struct request *rq = pc->rq;
508 struct bio_vec *bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200509 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 unsigned long flags;
511 char *data;
NeilBrown5705f702007-09-25 12:35:59 +0200512 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
NeilBrown5705f702007-09-25 12:35:59 +0200514 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200515 if (!bcount)
516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jens Axboe6c92e692007-08-16 13:43:12 +0200518 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Jens Axboe6c92e692007-08-16 13:43:12 +0200520 data = bvec_kmap_irq(bvec, &flags);
521 drive->hwif->atapi_input_bytes(drive, data, count);
522 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Jens Axboe6c92e692007-08-16 13:43:12 +0200524 bcount -= count;
525 pc->b_count += count;
526 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
529 idefloppy_do_end_request(drive, 1, done >> 9);
530
531 if (bcount) {
532 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
533 idefloppy_discard_data(drive, bcount);
534 }
535}
536
537static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
538{
539 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200540 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct bio_vec *bvec;
542 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200543 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 char *data;
545
NeilBrown5705f702007-09-25 12:35:59 +0200546 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200547 if (!bcount)
548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Jens Axboe6c92e692007-08-16 13:43:12 +0200550 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Jens Axboe6c92e692007-08-16 13:43:12 +0200552 data = bvec_kmap_irq(bvec, &flags);
553 drive->hwif->atapi_output_bytes(drive, data, count);
554 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Jens Axboe6c92e692007-08-16 13:43:12 +0200556 bcount -= count;
557 pc->b_count += count;
558 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
561 idefloppy_do_end_request(drive, 1, done >> 9);
562
Jan Beulichc7ea4b32005-06-23 00:09:59 -0700563#if IDEFLOPPY_DEBUG_BUGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (bcount) {
565 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
566 idefloppy_write_zeros(drive, bcount);
567 }
Jan Beulichc7ea4b32005-06-23 00:09:59 -0700568#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
572{
573 struct request *rq = pc->rq;
574 struct bio *bio = rq->bio;
575
576 while ((bio = rq->bio) != NULL)
577 idefloppy_do_end_request(drive, 1, 0);
578}
579
580/*
581 * idefloppy_queue_pc_head generates a new packet command request in front
582 * of the request queue, before the current request, so that it will be
583 * processed immediately, on the next pass through the driver.
584 */
585static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
586{
587 struct ide_floppy_obj *floppy = drive->driver_data;
588
589 ide_init_drive_cmd(rq);
590 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200591 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 rq->rq_disk = floppy->disk;
593 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
594}
595
596static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
597{
598 idefloppy_floppy_t *floppy = drive->driver_data;
599
600 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
601 floppy->pc_stack_index=0;
602 return (&floppy->pc_stack[floppy->pc_stack_index++]);
603}
604
605static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
606{
607 idefloppy_floppy_t *floppy = drive->driver_data;
608
609 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
610 floppy->rq_stack_index = 0;
611 return (&floppy->rq_stack[floppy->rq_stack_index++]);
612}
613
614/*
615 * idefloppy_analyze_error is called on each failed packet command retry
616 * to analyze the request sense.
617 */
618static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
619{
620 idefloppy_floppy_t *floppy = drive->driver_data;
621
622 floppy->sense_key = result->sense_key;
623 floppy->asc = result->asc;
624 floppy->ascq = result->ascq;
625 floppy->progress_indication = result->sksv[0] & 0x80 ?
626 (u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
627 if (floppy->failed_pc)
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100628 debug_log("pc = %x, sense key = %x, asc = %x, ascq = %x\n",
629 floppy->failed_pc->c[0], result->sense_key,
630 result->asc, result->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 else
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100632 debug_log("sense key = %x, asc = %x, ascq = %x\n",
633 result->sense_key, result->asc, result->ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636static void idefloppy_request_sense_callback (ide_drive_t *drive)
637{
638 idefloppy_floppy_t *floppy = drive->driver_data;
639
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100640 debug_log("Reached %s\n", __func__);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!floppy->pc->error) {
643 idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
644 idefloppy_do_end_request(drive, 1, 0);
645 } else {
646 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
647 idefloppy_do_end_request(drive, 0, 0);
648 }
649}
650
651/*
652 * General packet command callback function.
653 */
654static void idefloppy_pc_callback (ide_drive_t *drive)
655{
656 idefloppy_floppy_t *floppy = drive->driver_data;
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100657
658 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
661}
662
663/*
664 * idefloppy_init_pc initializes a packet command.
665 */
666static void idefloppy_init_pc (idefloppy_pc_t *pc)
667{
668 memset(pc->c, 0, 12);
669 pc->retries = 0;
670 pc->flags = 0;
671 pc->request_transfer = 0;
672 pc->buffer = pc->pc_buffer;
673 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
674 pc->callback = &idefloppy_pc_callback;
675}
676
677static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
678{
Borislav Petkov30d67092008-02-02 19:56:33 +0100679 idefloppy_init_pc(pc);
680 pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 pc->c[4] = 255;
682 pc->request_transfer = 18;
683 pc->callback = &idefloppy_request_sense_callback;
684}
685
686/*
687 * idefloppy_retry_pc is called when an error was detected during the
688 * last packet command. We queue a request sense packet command in
689 * the head of the request list.
690 */
691static void idefloppy_retry_pc (ide_drive_t *drive)
692{
693 idefloppy_pc_t *pc;
694 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100696 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 pc = idefloppy_next_pc_storage(drive);
698 rq = idefloppy_next_rq_storage(drive);
699 idefloppy_create_request_sense_cmd(pc);
700 idefloppy_queue_pc_head(drive, pc, rq);
701}
702
703/*
704 * idefloppy_pc_intr is the usual interrupt handler which will be called
705 * during a packet command.
706 */
707static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
708{
709 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100710 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 idefloppy_pc_t *pc = floppy->pc;
712 struct request *rq = pc->rq;
713 unsigned int temp;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100714 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100715 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100717 debug_log("Reached %s interrupt handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
720 if (HWIF(drive)->ide_dma_end(drive)) {
721 set_bit(PC_DMA_ERROR, &pc->flags);
722 } else {
723 pc->actually_transferred = pc->request_transfer;
724 idefloppy_update_buffers(drive, pc);
725 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100726 debug_log("DMA finished\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
729 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100730 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100732 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100733 debug_log("Packet command completed, %d bytes transferred\n",
734 pc->actually_transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
736
Ingo Molnar366c7f52006-07-03 00:25:25 -0700737 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100739 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /* Error detected */
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100741 debug_log("%s: I/O error\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 rq->errors++;
Borislav Petkov30d67092008-02-02 19:56:33 +0100743 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 printk(KERN_ERR "ide-floppy: I/O error in "
745 "request sense command\n");
746 return ide_do_reset(drive);
747 }
748 /* Retry operation */
749 idefloppy_retry_pc(drive);
750 /* queued, but not started */
751 return ide_stopped;
752 }
753 pc->error = 0;
754 if (floppy->failed_pc == pc)
755 floppy->failed_pc = NULL;
756 /* Command finished - Call the callback function */
757 pc->callback(drive);
758 return ide_stopped;
759 }
760
761 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
762 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
763 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100764 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return ide_do_reset(drive);
766 }
767
768 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100769 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
770 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* on this interrupt */
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100772 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100774 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
776 return ide_do_reset(drive);
777 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100778 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* Hopefully, we will never get here */
780 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100781 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 printk(KERN_ERR "but the floppy wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100783 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return ide_do_reset(drive);
785 }
786 if (!test_bit(PC_WRITING, &pc->flags)) {
787 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100788 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (temp > pc->request_transfer) {
790 if (temp > pc->buffer_size) {
791 printk(KERN_ERR "ide-floppy: The floppy wants "
792 "to send us more data than expected "
793 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100794 idefloppy_discard_data(drive, bcount);
Borislav Petkov0078df22008-02-02 19:56:33 +0100795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ide_set_handler(drive,
797 &idefloppy_pc_intr,
798 IDEFLOPPY_WAIT_CMD,
799 NULL);
800 return ide_started;
801 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100802 debug_log("The floppy wants to send us more data than"
803 " expected - allowing transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 }
806 if (test_bit(PC_WRITING, &pc->flags)) {
807 if (pc->buffer != NULL)
808 /* Write the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100809 hwif->atapi_output_bytes(drive, pc->current_position,
810 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100812 idefloppy_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 } else {
814 if (pc->buffer != NULL)
815 /* Read the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100816 hwif->atapi_input_bytes(drive, pc->current_position,
817 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100819 idefloppy_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100822 pc->actually_transferred += bcount;
823 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
826 return ide_started;
827}
828
829/*
830 * This is the original routine that did the packet transfer.
831 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
832 * for that drive below. The algorithm is chosen based on drive type
833 */
834static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
835{
836 ide_startstop_t startstop;
837 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100838 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
841 printk(KERN_ERR "ide-floppy: Strange, packet command "
842 "initiated yet DRQ isn't asserted\n");
843 return startstop;
844 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100845 ireason = drive->hwif->INB(IDE_IREASON_REG);
846 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
848 "issuing a packet command\n");
849 return ide_do_reset(drive);
850 }
Borislav Petkov0078df22008-02-02 19:56:33 +0100851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Set the interrupt routine */
853 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
854 /* Send the actual packet */
855 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
856 return ide_started;
857}
858
859
860/*
861 * What we have here is a classic case of a top half / bottom half
862 * interrupt service routine. In interrupt mode, the device sends
863 * an interrupt to signal it's ready to receive a packet. However,
864 * we need to delay about 2-3 ticks before issuing the packet or we
865 * gets in trouble.
866 *
867 * So, follow carefully. transfer_pc1 is called as an interrupt (or
868 * directly). In either case, when the device says it's ready for a
869 * packet, we schedule the packet transfer to occur about 2-3 ticks
870 * later in transfer_pc2.
871 */
872static int idefloppy_transfer_pc2 (ide_drive_t *drive)
873{
874 idefloppy_floppy_t *floppy = drive->driver_data;
875
876 /* Send the actual packet */
877 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
878 /* Timeout for the packet command */
879 return IDEFLOPPY_WAIT_CMD;
880}
881
882static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
883{
884 idefloppy_floppy_t *floppy = drive->driver_data;
885 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100886 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
889 printk(KERN_ERR "ide-floppy: Strange, packet command "
890 "initiated yet DRQ isn't asserted\n");
891 return startstop;
892 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100893 ireason = drive->hwif->INB(IDE_IREASON_REG);
894 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
896 "while issuing a packet command\n");
897 return ide_do_reset(drive);
898 }
899 /*
900 * The following delay solves a problem with ATAPI Zip 100 drives
901 * where the Busy flag was apparently being deasserted before the
902 * unit was ready to receive data. This was happening on a
903 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
904 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
905 * used until after the packet is moved in about 50 msec.
906 */
Borislav Petkov0078df22008-02-02 19:56:33 +0100907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 ide_set_handler(drive,
909 &idefloppy_pc_intr, /* service routine for packet command */
910 floppy->ticks, /* wait this long before "failing" */
911 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
912 return ide_started;
913}
914
915/**
916 * idefloppy_should_report_error()
917 *
918 * Supresses error messages resulting from Medium not present
919 */
920static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
921{
922 if (floppy->sense_key == 0x02 &&
923 floppy->asc == 0x3a &&
924 floppy->ascq == 0x00)
925 return 0;
926 return 1;
927}
928
929/*
930 * Issue a packet command
931 */
932static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
933{
934 idefloppy_floppy_t *floppy = drive->driver_data;
935 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100937 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100938 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (floppy->failed_pc == NULL &&
Borislav Petkov30d67092008-02-02 19:56:33 +0100941 pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 floppy->failed_pc = pc;
943 /* Set the current packet command */
944 floppy->pc = pc;
945
946 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
947 test_bit(PC_ABORT, &pc->flags)) {
948 /*
949 * We will "abort" retrying a packet command in case
950 * a legitimate error code was received.
951 */
952 if (!test_bit(PC_ABORT, &pc->flags)) {
953 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
954 if (idefloppy_should_report_error(floppy))
955 printk(KERN_ERR "ide-floppy: %s: I/O error, "
956 "pc = %2x, key = %2x, "
957 "asc = %2x, ascq = %2x\n",
958 drive->name, pc->c[0],
959 floppy->sense_key,
960 floppy->asc, floppy->ascq);
961 }
962 /* Giving up */
963 pc->error = IDEFLOPPY_ERROR_GENERAL;
964 }
965 floppy->failed_pc = NULL;
966 pc->callback(drive);
967 return ide_stopped;
968 }
969
Borislav Petkovbcc77d92008-02-02 19:56:34 +0100970 debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 pc->retries++;
973 /* We haven't transferred any data yet */
974 pc->actually_transferred = 0;
975 pc->current_position = pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100976 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100978 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
979 ide_dma_off(drive);
980
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100981 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100984 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100986 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
987 IDE_TFLAG_OUT_DEVICE, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100989 if (dma) { /* Begin DMA, if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
991 hwif->dma_start(drive);
992 }
993
994 /* Can we transfer the packet when we get the interrupt or wait? */
995 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
996 /* wait */
997 pkt_xfer_routine = &idefloppy_transfer_pc1;
998 } else {
999 /* immediate */
1000 pkt_xfer_routine = &idefloppy_transfer_pc;
1001 }
1002
1003 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1004 /* Issue the packet command */
1005 ide_execute_command(drive, WIN_PACKETCMD,
1006 pkt_xfer_routine,
1007 IDEFLOPPY_WAIT_CMD,
1008 NULL);
1009 return ide_started;
1010 } else {
1011 /* Issue the packet command */
1012 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1013 return (*pkt_xfer_routine) (drive);
1014 }
1015}
1016
1017static void idefloppy_rw_callback (ide_drive_t *drive)
1018{
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001019 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 idefloppy_do_end_request(drive, 1, 0);
1022 return;
1023}
1024
1025static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1026{
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001027 debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +01001030 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 pc->c[4] = prevent;
1032}
1033
1034static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1035{
1036 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +01001037 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 pc->c[7] = 255;
1039 pc->c[8] = 255;
1040 pc->request_transfer = 255;
1041}
1042
1043static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1044 int flags)
1045{
1046 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +01001047 pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 pc->c[1] = 0x17;
1049
1050 memset(pc->buffer, 0, 12);
1051 pc->buffer[1] = 0xA2;
1052 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1053
1054 if (flags & 1) /* Verify bit on... */
1055 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
1056 pc->buffer[3] = 8;
1057
Borislav Petkov8f622432008-02-02 19:56:33 +01001058 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
1059 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 pc->buffer_size=12;
1061 set_bit(PC_WRITING, &pc->flags);
1062}
1063
1064/*
1065 * A mode sense command is used to "sense" floppy parameters.
1066 */
1067static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
1068{
1069 u16 length = sizeof(idefloppy_mode_parameter_header_t);
1070
1071 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +01001072 pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 pc->c[1] = 0;
1074 pc->c[2] = page_code + (type << 6);
1075
1076 switch (page_code) {
1077 case IDEFLOPPY_CAPABILITIES_PAGE:
1078 length += 12;
1079 break;
1080 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1081 length += 32;
1082 break;
1083 default:
1084 printk(KERN_ERR "ide-floppy: unsupported page code "
1085 "in create_mode_sense_cmd\n");
1086 }
Borislav Petkov8f622432008-02-02 19:56:33 +01001087 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 pc->request_transfer = length;
1089}
1090
1091static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1092{
1093 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +01001094 pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 pc->c[4] = start;
1096}
1097
1098static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1099{
1100 idefloppy_init_pc(pc);
Borislav Petkov30d67092008-02-02 19:56:33 +01001101 pc->c[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1105{
1106 int block = sector / floppy->bs_factor;
1107 int blocks = rq->nr_sectors / floppy->bs_factor;
1108 int cmd = rq_data_dir(rq);
1109
1110 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1111 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1112 block, blocks);
1113
1114 idefloppy_init_pc(pc);
1115 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
Borislav Petkov30d67092008-02-02 19:56:33 +01001116 pc->c[0] = cmd == READ ? GPCMD_READ_12 : GPCMD_WRITE_12;
Borislav Petkov8f622432008-02-02 19:56:33 +01001117 put_unaligned(cpu_to_be32(blocks), (unsigned int *) &pc->c[6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 } else {
Borislav Petkov30d67092008-02-02 19:56:33 +01001119 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
Borislav Petkov8f622432008-02-02 19:56:33 +01001120 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
Borislav Petkov8f622432008-02-02 19:56:33 +01001122 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 pc->callback = &idefloppy_rw_callback;
1124 pc->rq = rq;
1125 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001126 if (rq->cmd_flags & REQ_RW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 set_bit(PC_WRITING, &pc->flags);
1128 pc->buffer = NULL;
1129 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1130 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1131}
1132
Jens Axboe3d6392c2007-07-09 12:38:05 +02001133static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 idefloppy_init_pc(pc);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001137 pc->callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +02001139 pc->rq = rq;
1140 pc->b_count = rq->data_len;
1141 if (rq->data_len && rq_data_dir(rq) == WRITE)
1142 set_bit(PC_WRITING, &pc->flags);
1143 pc->buffer = rq->data;
1144 if (rq->bio)
1145 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1146
1147 /*
1148 * possibly problematic, doesn't look like ide-floppy correctly
1149 * handled scattered requests if dma fails...
1150 */
1151 pc->request_transfer = pc->buffer_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154/*
1155 * idefloppy_do_request is our request handling function.
1156 */
1157static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1158{
1159 idefloppy_floppy_t *floppy = drive->driver_data;
1160 idefloppy_pc_t *pc;
1161 unsigned long block = (unsigned long)block_s;
1162
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001163 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -07001164 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001165 rq->cmd_type, rq->errors);
1166 debug_log("sector: %ld, nr_sectors: %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 "current_nr_sectors: %d\n", (long)rq->sector,
1168 rq->nr_sectors, rq->current_nr_sectors);
1169
1170 if (rq->errors >= ERROR_MAX) {
1171 if (floppy->failed_pc != NULL) {
1172 if (idefloppy_should_report_error(floppy))
1173 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1174 " key = %2x, asc = %2x, ascq = %2x\n",
1175 drive->name, floppy->failed_pc->c[0],
1176 floppy->sense_key, floppy->asc, floppy->ascq);
1177 }
1178 else
1179 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1180 drive->name);
1181 idefloppy_do_end_request(drive, 0, 0);
1182 return ide_stopped;
1183 }
Jens Axboe4aff5e22006-08-10 08:44:47 +02001184 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (((long)rq->sector % floppy->bs_factor) ||
1186 (rq->nr_sectors % floppy->bs_factor)) {
1187 printk("%s: unsupported r/w request size\n",
1188 drive->name);
1189 idefloppy_do_end_request(drive, 0, 0);
1190 return ide_stopped;
1191 }
1192 pc = idefloppy_next_pc_storage(drive);
1193 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001194 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 pc = (idefloppy_pc_t *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001196 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001198 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
1200 blk_dump_rq_flags(rq,
1201 "ide-floppy: unsupported command in queue");
1202 idefloppy_do_end_request(drive, 0, 0);
1203 return ide_stopped;
1204 }
1205
1206 pc->rq = rq;
1207 return idefloppy_issue_pc(drive, pc);
1208}
1209
1210/*
1211 * idefloppy_queue_pc_tail adds a special packet command request to the
1212 * tail of the request queue, and waits for it to be serviced.
1213 */
1214static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1215{
1216 struct ide_floppy_obj *floppy = drive->driver_data;
1217 struct request rq;
1218
1219 ide_init_drive_cmd (&rq);
1220 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001221 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 rq.rq_disk = floppy->disk;
1223
1224 return ide_do_drive_cmd(drive, &rq, ide_wait);
1225}
1226
1227/*
1228 * Look at the flexible disk page parameters. We will ignore the CHS
1229 * capacity parameters and use the LBA parameters instead.
1230 */
1231static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1232{
1233 idefloppy_floppy_t *floppy = drive->driver_data;
1234 idefloppy_pc_t pc;
1235 idefloppy_mode_parameter_header_t *header;
1236 idefloppy_flexible_disk_page_t *page;
1237 int capacity, lba_capacity;
1238
1239 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1240 if (idefloppy_queue_pc_tail(drive,&pc)) {
1241 printk(KERN_ERR "ide-floppy: Can't get flexible disk "
1242 "page parameters\n");
1243 return 1;
1244 }
1245 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1246 floppy->wp = header->wp;
1247 set_disk_ro(floppy->disk, floppy->wp);
1248 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1249
Borislav Petkov8f622432008-02-02 19:56:33 +01001250 page->transfer_rate = be16_to_cpu(page->transfer_rate);
1251 page->sector_size = be16_to_cpu(page->sector_size);
1252 page->cyls = be16_to_cpu(page->cyls);
1253 page->rpm = be16_to_cpu(page->rpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1255 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1256 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1257 "%d sector size, %d rpm\n",
1258 drive->name, capacity / 1024, page->cyls,
1259 page->heads, page->sectors,
1260 page->transfer_rate / 8, page->sector_size, page->rpm);
1261
1262 floppy->flexible_disk_page = *page;
1263 drive->bios_cyl = page->cyls;
1264 drive->bios_head = page->heads;
1265 drive->bios_sect = page->sectors;
1266 lba_capacity = floppy->blocks * floppy->block_size;
1267 if (capacity < lba_capacity) {
1268 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1269 "bytes, but the drive only handles %d\n",
1270 drive->name, lba_capacity, capacity);
1271 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1272 }
1273 return 0;
1274}
1275
1276static int idefloppy_get_capability_page(ide_drive_t *drive)
1277{
1278 idefloppy_floppy_t *floppy = drive->driver_data;
1279 idefloppy_pc_t pc;
1280 idefloppy_mode_parameter_header_t *header;
1281 idefloppy_capabilities_page_t *page;
1282
1283 floppy->srfp = 0;
1284 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1285 MODE_SENSE_CURRENT);
1286
1287 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1288 if (idefloppy_queue_pc_tail(drive,&pc)) {
1289 return 1;
1290 }
1291
1292 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1293 page= (idefloppy_capabilities_page_t *)(header+1);
1294 floppy->srfp = page->srfp;
1295 return (0);
1296}
1297
1298/*
1299 * Determine if a media is present in the floppy drive, and if so,
1300 * its LBA capacity.
1301 */
1302static int idefloppy_get_capacity (ide_drive_t *drive)
1303{
1304 idefloppy_floppy_t *floppy = drive->driver_data;
1305 idefloppy_pc_t pc;
1306 idefloppy_capacity_header_t *header;
1307 idefloppy_capacity_descriptor_t *descriptor;
1308 int i, descriptors, rc = 1, blocks, length;
1309
1310 drive->bios_cyl = 0;
1311 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +01001312 floppy->blocks = 0;
1313 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 set_capacity(floppy->disk, 0);
1315
1316 idefloppy_create_read_capacity_cmd(&pc);
1317 if (idefloppy_queue_pc_tail(drive, &pc)) {
1318 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1319 return 1;
1320 }
1321 header = (idefloppy_capacity_header_t *) pc.buffer;
1322 descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1323 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1324
1325 for (i = 0; i < descriptors; i++, descriptor++) {
Borislav Petkov8f622432008-02-02 19:56:33 +01001326 blocks = descriptor->blocks = be32_to_cpu(descriptor->blocks);
1327 length = descriptor->length = be16_to_cpu(descriptor->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 if (!i)
1330 {
1331 switch (descriptor->dc) {
1332 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1333 case CAPACITY_UNFORMATTED:
1334 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1335 /*
1336 * If it is not a clik drive, break out
1337 * (maintains previous driver behaviour)
1338 */
1339 break;
1340 case CAPACITY_CURRENT:
1341 /* Normal Zip/LS-120 disks */
1342 if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1343 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1344 "sector size\n", drive->name,
1345 blocks * length / 1024, blocks, length);
1346 floppy->capacity = *descriptor;
1347 if (!length || length % 512) {
1348 printk(KERN_NOTICE "%s: %d bytes block size "
1349 "not supported\n", drive->name, length);
1350 } else {
1351 floppy->blocks = blocks;
1352 floppy->block_size = length;
1353 if ((floppy->bs_factor = length / 512) != 1)
1354 printk(KERN_NOTICE "%s: warning: non "
1355 "512 bytes block size not "
1356 "fully supported\n",
1357 drive->name);
1358 rc = 0;
1359 }
1360 break;
1361 case CAPACITY_NO_CARTRIDGE:
1362 /*
1363 * This is a KERN_ERR so it appears on screen
1364 * for the user to see
1365 */
1366 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1367 break;
1368 case CAPACITY_INVALID:
1369 printk(KERN_ERR "%s: Invalid capacity for disk "
1370 "in drive\n", drive->name);
1371 break;
1372 }
1373 }
1374 if (!i) {
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001375 debug_log("Descriptor 0 Code: %d\n", descriptor->dc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001377 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1378 i, blocks * length / 1024, blocks, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
1381 /* Clik! disk does not support get_flexible_disk_page */
1382 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1383 (void) idefloppy_get_flexible_disk_page(drive);
1384 }
1385
1386 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1387 return rc;
1388}
1389
1390/*
1391** Obtain the list of formattable capacities.
1392** Very similar to idefloppy_get_capacity, except that we push the capacity
1393** descriptors to userland, instead of our own structures.
1394**
1395** Userland gives us the following structure:
1396**
1397** struct idefloppy_format_capacities {
1398** int nformats;
1399** struct {
1400** int nblocks;
1401** int blocksize;
1402** } formats[];
1403** } ;
1404**
1405** userland initializes nformats to the number of allocated formats[]
1406** records. On exit we set nformats to the number of records we've
1407** actually initialized.
1408**
1409*/
1410
1411static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1412{
1413 idefloppy_pc_t pc;
1414 idefloppy_capacity_header_t *header;
1415 idefloppy_capacity_descriptor_t *descriptor;
1416 int i, descriptors, blocks, length;
1417 int u_array_size;
1418 int u_index;
1419 int __user *argp;
1420
1421 if (get_user(u_array_size, arg))
1422 return (-EFAULT);
1423
1424 if (u_array_size <= 0)
1425 return (-EINVAL);
1426
1427 idefloppy_create_read_capacity_cmd(&pc);
1428 if (idefloppy_queue_pc_tail(drive, &pc)) {
1429 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1430 return (-EIO);
1431 }
1432 header = (idefloppy_capacity_header_t *) pc.buffer;
1433 descriptors = header->length /
1434 sizeof(idefloppy_capacity_descriptor_t);
1435 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1436
1437 u_index = 0;
1438 argp = arg + 1;
1439
1440 /*
1441 ** We always skip the first capacity descriptor. That's the
1442 ** current capacity. We are interested in the remaining descriptors,
1443 ** the formattable capacities.
1444 */
1445
1446 for (i=0; i<descriptors; i++, descriptor++) {
1447 if (u_index >= u_array_size)
1448 break; /* User-supplied buffer too small */
1449 if (i == 0)
1450 continue; /* Skip the first descriptor */
1451
Borislav Petkov8f622432008-02-02 19:56:33 +01001452 blocks = be32_to_cpu(descriptor->blocks);
1453 length = be16_to_cpu(descriptor->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (put_user(blocks, argp))
1456 return(-EFAULT);
1457 ++argp;
1458
1459 if (put_user(length, argp))
1460 return (-EFAULT);
1461 ++argp;
1462
1463 ++u_index;
1464 }
1465
1466 if (put_user(u_index, arg))
1467 return (-EFAULT);
1468 return (0);
1469}
1470
1471/*
1472** Send ATAPI_FORMAT_UNIT to the drive.
1473**
1474** Userland gives us the following structure:
1475**
1476** struct idefloppy_format_command {
1477** int nblocks;
1478** int blocksize;
1479** int flags;
1480** } ;
1481**
1482** flags is a bitmask, currently, the only defined flag is:
1483**
1484** 0x01 - verify media after format.
1485*/
1486
1487static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1488{
1489 int blocks;
1490 int length;
1491 int flags;
1492 idefloppy_pc_t pc;
1493
1494 if (get_user(blocks, arg) ||
1495 get_user(length, arg+1) ||
1496 get_user(flags, arg+2)) {
1497 return (-EFAULT);
1498 }
1499
1500 /* Get the SFRP bit */
1501 (void) idefloppy_get_capability_page(drive);
1502 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1503 if (idefloppy_queue_pc_tail(drive, &pc)) {
1504 return (-EIO);
1505 }
1506
1507 return (0);
1508}
1509
1510/*
1511** Get ATAPI_FORMAT_UNIT progress indication.
1512**
Matt LaPlante0779bf22006-11-30 05:24:39 +01001513** Userland gives a pointer to an int. The int is set to a progress
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514** indicator 0-65536, with 65536=100%.
1515**
1516** If the drive does not support format progress indication, we just check
1517** the dsc bit, and return either 0 or 65536.
1518*/
1519
1520static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1521{
1522 idefloppy_floppy_t *floppy = drive->driver_data;
1523 idefloppy_pc_t pc;
1524 int progress_indication = 0x10000;
1525
1526 if (floppy->srfp) {
1527 idefloppy_create_request_sense_cmd(&pc);
1528 if (idefloppy_queue_pc_tail(drive, &pc)) {
1529 return (-EIO);
1530 }
1531
1532 if (floppy->sense_key == 2 &&
1533 floppy->asc == 4 &&
1534 floppy->ascq == 4) {
1535 progress_indication = floppy->progress_indication;
1536 }
1537 /* Else assume format_unit has finished, and we're
1538 ** at 0x10000 */
1539 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001541 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 local_irq_save(flags);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001544 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 local_irq_restore(flags);
1546
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001547 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549 if (put_user(progress_indication, arg))
1550 return (-EFAULT);
1551
1552 return (0);
1553}
1554
1555/*
1556 * Return the current floppy capacity.
1557 */
1558static sector_t idefloppy_capacity (ide_drive_t *drive)
1559{
1560 idefloppy_floppy_t *floppy = drive->driver_data;
1561 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1562
1563 return capacity;
1564}
1565
1566/*
1567 * idefloppy_identify_device checks if we can support a drive,
1568 * based on the ATAPI IDENTIFY command results.
1569 */
1570static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1571{
1572 struct idefloppy_id_gcw gcw;
1573#if IDEFLOPPY_DEBUG_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 char buffer[80];
1575#endif /* IDEFLOPPY_DEBUG_INFO */
1576
1577 *((u16 *) &gcw) = id->config;
1578
1579#ifdef CONFIG_PPC
1580 /* kludge for Apple PowerBook internal zip */
1581 if ((gcw.device_type == 5) &&
1582 !strstr(id->model, "CD-ROM") &&
1583 strstr(id->model, "ZIP"))
1584 gcw.device_type = 0;
1585#endif
1586
1587#if IDEFLOPPY_DEBUG_INFO
1588 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1589 switch (gcw.protocol) {
1590 case 0: case 1: sprintf(buffer, "ATA");break;
1591 case 2: sprintf(buffer, "ATAPI");break;
1592 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1593 }
1594 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1595 switch (gcw.device_type) {
1596 case 0: sprintf(buffer, "Direct-access Device");break;
1597 case 1: sprintf(buffer, "Streaming Tape Device");break;
1598 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1599 case 5: sprintf(buffer, "CD-ROM Device");break;
1600 case 6: sprintf(buffer, "Reserved");
1601 case 7: sprintf(buffer, "Optical memory Device");break;
1602 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1603 default: sprintf(buffer, "Reserved");
1604 }
1605 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1606 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1607 switch (gcw.drq_type) {
1608 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1609 case 1: sprintf(buffer, "Interrupt DRQ");break;
1610 case 2: sprintf(buffer, "Accelerated DRQ");break;
1611 case 3: sprintf(buffer, "Reserved");break;
1612 }
1613 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1614 switch (gcw.packet_size) {
1615 case 0: sprintf(buffer, "12 bytes");break;
1616 case 1: sprintf(buffer, "16 bytes");break;
1617 default: sprintf(buffer, "Reserved");break;
1618 }
1619 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620#endif /* IDEFLOPPY_DEBUG_INFO */
1621
1622 if (gcw.protocol != 2)
1623 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1624 else if (gcw.device_type != 0)
1625 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1626 else if (!gcw.removable)
1627 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1628 else if (gcw.drq_type == 3) {
1629 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1630 } else if (gcw.packet_size != 0) {
1631 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1632 } else
1633 return 1;
1634 return 0;
1635}
1636
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001637#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638static void idefloppy_add_settings(ide_drive_t *drive)
1639{
1640 idefloppy_floppy_t *floppy = drive->driver_data;
1641
1642/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001643 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001645 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1646 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1647 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1648 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001650#else
1651static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1652#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654/*
1655 * Driver initialization.
1656 */
1657static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1658{
1659 struct idefloppy_id_gcw gcw;
1660
1661 *((u16 *) &gcw) = drive->id->config;
1662 floppy->pc = floppy->pc_stack;
1663 if (gcw.drq_type == 1)
1664 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1665 /*
1666 * We used to check revisions here. At this point however
1667 * I'm giving up. Just assume they are all broken, its easier.
1668 *
1669 * The actual reason for the workarounds was likely
1670 * a driver bug after all rather than a firmware bug,
1671 * and the workaround below used to hide it. It should
1672 * be fixed as of version 1.9, but to be on the safe side
1673 * we'll leave the limitation below for the 2.2.x tree.
1674 */
1675
1676 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1677 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1678 /* This value will be visible in the /proc/ide/hdx/settings */
1679 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1680 blk_queue_max_sectors(drive->queue, 64);
1681 }
1682
1683 /*
1684 * Guess what? The IOMEGA Clik! drive also needs the
1685 * above fix. It makes nasty clicking noises without
1686 * it, so please don't remove this.
1687 */
1688 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1689 blk_queue_max_sectors(drive->queue, 64);
1690 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1691 }
1692
1693
1694 (void) idefloppy_get_capacity(drive);
1695 idefloppy_add_settings(drive);
1696}
1697
Russell King4031bbe2006-01-06 11:41:00 +00001698static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 idefloppy_floppy_t *floppy = drive->driver_data;
1701 struct gendisk *g = floppy->disk;
1702
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001703 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 del_gendisk(g);
1706
1707 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Borislav Petkov9a24b632008-02-02 19:56:33 +01001710static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1713 ide_drive_t *drive = floppy->drive;
1714 struct gendisk *g = floppy->disk;
1715
1716 drive->driver_data = NULL;
1717 g->private_data = NULL;
1718 put_disk(g);
1719 kfree(floppy);
1720}
1721
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001722#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723static int proc_idefloppy_read_capacity
1724 (char *page, char **start, off_t off, int count, int *eof, void *data)
1725{
1726 ide_drive_t*drive = (ide_drive_t *)data;
1727 int len;
1728
1729 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1730 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1731}
1732
1733static ide_proc_entry_t idefloppy_proc[] = {
1734 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1735 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1736 { NULL, 0, NULL, NULL }
1737};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001738#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Russell King4031bbe2006-01-06 11:41:00 +00001740static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001743 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001744 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001745 .name = "ide-floppy",
1746 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001747 },
Russell King4031bbe2006-01-06 11:41:00 +00001748 .probe = ide_floppy_probe,
1749 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 .version = IDEFLOPPY_VERSION,
1751 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 .do_request = idefloppy_do_request,
1754 .end_request = idefloppy_do_end_request,
1755 .error = __ide_error,
1756 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001757#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760};
1761
1762static int idefloppy_open(struct inode *inode, struct file *filp)
1763{
1764 struct gendisk *disk = inode->i_bdev->bd_disk;
1765 struct ide_floppy_obj *floppy;
1766 ide_drive_t *drive;
1767 idefloppy_pc_t pc;
1768 int ret = 0;
1769
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001770 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 if (!(floppy = ide_floppy_get(disk)))
1773 return -ENXIO;
1774
1775 drive = floppy->drive;
1776
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001777 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001779 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1781 /* Just in case */
1782
1783 idefloppy_create_test_unit_ready_cmd(&pc);
1784 if (idefloppy_queue_pc_tail(drive, &pc)) {
1785 idefloppy_create_start_stop_cmd(&pc, 1);
1786 (void) idefloppy_queue_pc_tail(drive, &pc);
1787 }
1788
1789 if (idefloppy_get_capacity (drive)
1790 && (filp->f_flags & O_NDELAY) == 0
1791 /*
1792 ** Allow O_NDELAY to open a drive without a disk, or with
1793 ** an unreadable disk, so that we can get the format
1794 ** capacity of the drive or begin the format - Sam
1795 */
1796 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 ret = -EIO;
1798 goto out_put_floppy;
1799 }
1800
1801 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 ret = -EROFS;
1803 goto out_put_floppy;
1804 }
1805 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1806 /* IOMEGA Clik! drives do not support lock/unlock commands */
1807 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1808 idefloppy_create_prevent_cmd(&pc, 1);
1809 (void) idefloppy_queue_pc_tail(drive, &pc);
1810 }
1811 check_disk_change(inode->i_bdev);
1812 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 ret = -EBUSY;
1814 goto out_put_floppy;
1815 }
1816 return 0;
1817
1818out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001819 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 ide_floppy_put(floppy);
1821 return ret;
1822}
1823
1824static int idefloppy_release(struct inode *inode, struct file *filp)
1825{
1826 struct gendisk *disk = inode->i_bdev->bd_disk;
1827 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1828 ide_drive_t *drive = floppy->drive;
1829 idefloppy_pc_t pc;
Borislav Petkovbcc77d92008-02-02 19:56:34 +01001830
1831 debug_log("Reached %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001833 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* IOMEGA Clik! drives do not support lock/unlock commands */
1835 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1836 idefloppy_create_prevent_cmd(&pc, 0);
1837 (void) idefloppy_queue_pc_tail(drive, &pc);
1838 }
1839
1840 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1841 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001842
1843 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 ide_floppy_put(floppy);
1846
1847 return 0;
1848}
1849
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001850static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1851{
1852 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1853 ide_drive_t *drive = floppy->drive;
1854
1855 geo->heads = drive->bios_head;
1856 geo->sectors = drive->bios_sect;
1857 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1858 return 0;
1859}
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861static int idefloppy_ioctl(struct inode *inode, struct file *file,
1862 unsigned int cmd, unsigned long arg)
1863{
1864 struct block_device *bdev = inode->i_bdev;
1865 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1866 ide_drive_t *drive = floppy->drive;
1867 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001868 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 int prevent = (arg) ? 1 : 0;
1870 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 switch (cmd) {
1873 case CDROMEJECT:
1874 prevent = 0;
1875 /* fall through */
1876 case CDROM_LOCKDOOR:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001877 if (floppy->openers > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return -EBUSY;
1879
1880 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1881 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1882 idefloppy_create_prevent_cmd(&pc, prevent);
1883 (void) idefloppy_queue_pc_tail(drive, &pc);
1884 }
1885 if (cmd == CDROMEJECT) {
1886 idefloppy_create_start_stop_cmd(&pc, 2);
1887 (void) idefloppy_queue_pc_tail(drive, &pc);
1888 }
1889 return 0;
1890 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1891 return 0;
1892 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1893 return idefloppy_get_format_capacities(drive, argp);
1894 case IDEFLOPPY_IOCTL_FORMAT_START:
1895
1896 if (!(file->f_mode & 2))
1897 return -EPERM;
1898
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001899 if (floppy->openers > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /* Don't format if someone is using the disk */
1901
1902 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1903 &floppy->flags);
1904 return -EBUSY;
1905 }
1906
1907 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1908
1909 err = idefloppy_begin_format(drive, argp);
1910 if (err)
1911 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1912 return err;
1913 /*
1914 ** Note, the bit will be cleared when the device is
1915 ** closed. This is the cleanest way to handle the
1916 ** situation where the drive does not support
1917 ** format progress reporting.
1918 */
1919 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1920 return idefloppy_get_format_progress(drive, argp);
1921 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02001922
1923 /*
1924 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1925 * and CDROM_SEND_PACKET (legacy) ioctls
1926 */
1927 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1928 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1929 bdev->bd_disk, cmd, argp);
1930 else
1931 err = -ENOTTY;
1932
1933 if (err == -ENOTTY)
1934 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1935
1936 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
1939static int idefloppy_media_changed(struct gendisk *disk)
1940{
1941 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1942 ide_drive_t *drive = floppy->drive;
1943
1944 /* do not scan partitions twice if this is a removable device */
1945 if (drive->attach) {
1946 drive->attach = 0;
1947 return 0;
1948 }
1949 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1950}
1951
1952static int idefloppy_revalidate_disk(struct gendisk *disk)
1953{
1954 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1955 set_capacity(disk, idefloppy_capacity(floppy->drive));
1956 return 0;
1957}
1958
1959static struct block_device_operations idefloppy_ops = {
1960 .owner = THIS_MODULE,
1961 .open = idefloppy_open,
1962 .release = idefloppy_release,
1963 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001964 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 .media_changed = idefloppy_media_changed,
1966 .revalidate_disk= idefloppy_revalidate_disk
1967};
1968
Russell King4031bbe2006-01-06 11:41:00 +00001969static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 idefloppy_floppy_t *floppy;
1972 struct gendisk *g;
1973
1974 if (!strstr("ide-floppy", drive->driver_req))
1975 goto failed;
1976 if (!drive->present)
1977 goto failed;
1978 if (drive->media != ide_floppy)
1979 goto failed;
1980 if (!idefloppy_identify_device (drive, drive->id)) {
1981 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1982 goto failed;
1983 }
1984 if (drive->scsi) {
1985 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1986 goto failed;
1987 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001988 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1990 goto failed;
1991 }
1992
1993 g = alloc_disk(1 << PARTN_BITS);
1994 if (!g)
1995 goto out_free_floppy;
1996
1997 ide_init_disk(g, drive);
1998
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001999 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 kref_init(&floppy->kref);
2002
2003 floppy->drive = drive;
2004 floppy->driver = &idefloppy_driver;
2005 floppy->disk = g;
2006
2007 g->private_data = &floppy->driver;
2008
2009 drive->driver_data = floppy;
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 idefloppy_setup (drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 g->minors = 1 << PARTN_BITS;
2014 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
2016 g->fops = &idefloppy_ops;
2017 drive->attach = 1;
2018 add_disk(g);
2019 return 0;
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021out_free_floppy:
2022 kfree(floppy);
2023failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002024 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2028
2029static void __exit idefloppy_exit (void)
2030{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002031 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002034static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002037 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Kay Sievers263756e2005-12-12 18:03:44 +01002040MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041module_init(idefloppy_init);
2042module_exit(idefloppy_exit);
2043MODULE_LICENSE("GPL");