blob: 239aebcfc359e44f098cb82b01e889962ca9dad5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/ide-floppy.c Version 0.99 Feb 24 2002
3 *
4 * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2000 - 2002 Paul Bristow <paul@paulbristow.net>
6 */
7
8/*
9 * IDE ATAPI floppy driver.
10 *
11 * The driver currently doesn't have any fancy features, just the bare
12 * minimum read/write support.
13 *
14 * This driver supports the following IDE floppy drives:
15 *
16 * LS-120/240 SuperDisk
17 * Iomega Zip 100/250
18 * Iomega PC Card Clik!/PocketZip
19 *
20 * Many thanks to Lode Leroy <Lode.Leroy@www.ibase.be>, who tested so many
21 * ALPHA patches to this driver on an EASYSTOR LS-120 ATAPI floppy drive.
22 *
23 * Ver 0.1 Oct 17 96 Initial test version, mostly based on ide-tape.c.
24 * Ver 0.2 Oct 31 96 Minor changes.
25 * Ver 0.3 Dec 2 96 Fixed error recovery bug.
26 * Ver 0.4 Jan 26 97 Add support for the HDIO_GETGEO ioctl.
27 * Ver 0.5 Feb 21 97 Add partitions support.
28 * Use the minimum of the LBA and CHS capacities.
29 * Avoid hwgroup->rq == NULL on the last irq.
30 * Fix potential null dereferencing with DEBUG_LOG.
31 * Ver 0.8 Dec 7 97 Increase irq timeout from 10 to 50 seconds.
32 * Add media write-protect detection.
33 * Issue START command only if TEST UNIT READY fails.
34 * Add work-around for IOMEGA ZIP revision 21.D.
35 * Remove idefloppy_get_capabilities().
36 * Ver 0.9 Jul 4 99 Fix a bug which might have caused the number of
37 * bytes requested on each interrupt to be zero.
38 * Thanks to <shanos@es.co.nz> for pointing this out.
39 * Ver 0.9.sv Jan 6 01 Sam Varshavchik <mrsam@courier-mta.com>
40 * Implement low level formatting. Reimplemented
41 * IDEFLOPPY_CAPABILITIES_PAGE, since we need the srfp
42 * bit. My LS-120 drive barfs on
43 * IDEFLOPPY_CAPABILITIES_PAGE, but maybe it's just me.
44 * Compromise by not reporting a failure to get this
45 * mode page. Implemented four IOCTLs in order to
46 * implement formatting. IOCTls begin with 0x4600,
47 * 0x46 is 'F' as in Format.
48 * Jan 9 01 Userland option to select format verify.
49 * Added PC_SUPPRESS_ERROR flag - some idefloppy drives
50 * do not implement IDEFLOPPY_CAPABILITIES_PAGE, and
51 * return a sense error. Suppress error reporting in
52 * this particular case in order to avoid spurious
53 * errors in syslog. The culprit is
54 * idefloppy_get_capability_page(), so move it to
55 * idefloppy_begin_format() so that it's not used
56 * unless absolutely necessary.
57 * If drive does not support format progress indication
58 * monitor the dsc bit in the status register.
59 * Also, O_NDELAY on open will allow the device to be
60 * opened without a disk available. This can be used to
61 * open an unformatted disk, or get the device capacity.
62 * Ver 0.91 Dec 11 99 Added IOMEGA Clik! drive support by
63 * <paul@paulbristow.net>
64 * Ver 0.92 Oct 22 00 Paul Bristow became official maintainer for this
65 * driver. Included Powerbook internal zip kludge.
66 * Ver 0.93 Oct 24 00 Fixed bugs for Clik! drive
67 * no disk on insert and disk change now works
68 * Ver 0.94 Oct 27 00 Tidied up to remove strstr(Clik) everywhere
69 * Ver 0.95 Nov 7 00 Brought across to kernel 2.4
70 * Ver 0.96 Jan 7 01 Actually in line with release version of 2.4.0
71 * including set_bit patch from Rusty Russell
72 * Ver 0.97 Jul 22 01 Merge 0.91-0.96 onto 0.9.sv for ac series
73 * Ver 0.97.sv Aug 3 01 Backported from 2.4.7-ac3
74 * Ver 0.98 Oct 26 01 Split idefloppy_transfer_pc into two pieces to
75 * fix a lost interrupt problem. It appears the busy
76 * bit was being deasserted by my IOMEGA ATAPI ZIP 100
77 * drive before the drive was actually ready.
78 * Ver 0.98a Oct 29 01 Expose delay value so we can play.
79 * Ver 0.99 Feb 24 02 Remove duplicate code, modify clik! detection code
80 * to support new PocketZip drives
81 */
82
83#define IDEFLOPPY_VERSION "0.99.newide"
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/module.h>
86#include <linux/types.h>
87#include <linux/string.h>
88#include <linux/kernel.h>
89#include <linux/delay.h>
90#include <linux/timer.h>
91#include <linux/mm.h>
92#include <linux/interrupt.h>
93#include <linux/major.h>
94#include <linux/errno.h>
95#include <linux/genhd.h>
96#include <linux/slab.h>
97#include <linux/cdrom.h>
98#include <linux/ide.h>
99#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800100#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +0200102#include <scsi/scsi_ioctl.h>
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <asm/byteorder.h>
105#include <asm/irq.h>
106#include <asm/uaccess.h>
107#include <asm/io.h>
108#include <asm/unaligned.h>
109
110/*
111 * The following are used to debug the driver.
112 */
113#define IDEFLOPPY_DEBUG_LOG 0
114#define IDEFLOPPY_DEBUG_INFO 0
115#define IDEFLOPPY_DEBUG_BUGS 1
116
117/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
118#define IDEFLOPPY_DEBUG( fmt, args... )
119
120#if IDEFLOPPY_DEBUG_LOG
121#define debug_log printk
122#else
123#define debug_log(fmt, args... ) do {} while(0)
124#endif
125
126
127/*
128 * Some drives require a longer irq timeout.
129 */
130#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
131
132/*
133 * After each failed packet command we issue a request sense command
134 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
135 */
136#define IDEFLOPPY_MAX_PC_RETRIES 3
137
138/*
139 * With each packet command, we allocate a buffer of
140 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
141 */
142#define IDEFLOPPY_PC_BUFFER_SIZE 256
143
144/*
145 * In various places in the driver, we need to allocate storage
146 * for packet commands and requests, which will remain valid while
147 * we leave the driver to wait for an interrupt or a timeout event.
148 */
149#define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
150
151/*
152 * Our view of a packet command.
153 */
154typedef struct idefloppy_packet_command_s {
155 u8 c[12]; /* Actual packet bytes */
156 int retries; /* On each retry, we increment retries */
157 int error; /* Error code */
158 int request_transfer; /* Bytes to transfer */
159 int actually_transferred; /* Bytes actually transferred */
160 int buffer_size; /* Size of our data buffer */
161 int b_count; /* Missing/Available data on the current buffer */
162 struct request *rq; /* The corresponding request */
163 u8 *buffer; /* Data buffer */
164 u8 *current_position; /* Pointer into the above buffer */
165 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
166 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
167 unsigned long flags; /* Status/Action bit flags: long for set_bit */
168} idefloppy_pc_t;
169
170/*
171 * Packet command flag bits.
172 */
173#define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
174#define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
175#define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
176#define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
177#define PC_WRITING 5 /* Data direction */
178
179#define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
180
181/*
182 * Removable Block Access Capabilities Page
183 */
184typedef struct {
185#if defined(__LITTLE_ENDIAN_BITFIELD)
186 unsigned page_code :6; /* Page code - Should be 0x1b */
187 unsigned reserved1_6 :1; /* Reserved */
188 unsigned ps :1; /* Should be 0 */
189#elif defined(__BIG_ENDIAN_BITFIELD)
190 unsigned ps :1; /* Should be 0 */
191 unsigned reserved1_6 :1; /* Reserved */
192 unsigned page_code :6; /* Page code - Should be 0x1b */
193#else
194#error "Bitfield endianness not defined! Check your byteorder.h"
195#endif
196 u8 page_length; /* Page Length - Should be 0xa */
197#if defined(__LITTLE_ENDIAN_BITFIELD)
198 unsigned reserved2 :6;
199 unsigned srfp :1; /* Supports reporting progress of format */
200 unsigned sflp :1; /* System floppy type device */
201 unsigned tlun :3; /* Total logical units supported by the device */
202 unsigned reserved3 :3;
203 unsigned sml :1; /* Single / Multiple lun supported */
204 unsigned ncd :1; /* Non cd optical device */
205#elif defined(__BIG_ENDIAN_BITFIELD)
206 unsigned sflp :1; /* System floppy type device */
207 unsigned srfp :1; /* Supports reporting progress of format */
208 unsigned reserved2 :6;
209 unsigned ncd :1; /* Non cd optical device */
210 unsigned sml :1; /* Single / Multiple lun supported */
211 unsigned reserved3 :3;
212 unsigned tlun :3; /* Total logical units supported by the device */
213#else
214#error "Bitfield endianness not defined! Check your byteorder.h"
215#endif
216 u8 reserved[8];
217} idefloppy_capabilities_page_t;
218
219/*
220 * Flexible disk page.
221 */
222typedef struct {
223#if defined(__LITTLE_ENDIAN_BITFIELD)
224 unsigned page_code :6; /* Page code - Should be 0x5 */
225 unsigned reserved1_6 :1; /* Reserved */
226 unsigned ps :1; /* The device is capable of saving the page */
227#elif defined(__BIG_ENDIAN_BITFIELD)
228 unsigned ps :1; /* The device is capable of saving the page */
229 unsigned reserved1_6 :1; /* Reserved */
230 unsigned page_code :6; /* Page code - Should be 0x5 */
231#else
232#error "Bitfield endianness not defined! Check your byteorder.h"
233#endif
234 u8 page_length; /* Page Length - Should be 0x1e */
235 u16 transfer_rate; /* In kilobits per second */
236 u8 heads, sectors; /* Number of heads, Number of sectors per track */
237 u16 sector_size; /* Byes per sector */
238 u16 cyls; /* Number of cylinders */
239 u8 reserved10[10];
240 u8 motor_delay; /* Motor off delay */
241 u8 reserved21[7];
242 u16 rpm; /* Rotations per minute */
243 u8 reserved30[2];
244} idefloppy_flexible_disk_page_t;
245
246/*
247 * Format capacity
248 */
249typedef struct {
250 u8 reserved[3];
251 u8 length; /* Length of the following descriptors in bytes */
252} idefloppy_capacity_header_t;
253
254typedef struct {
255 u32 blocks; /* Number of blocks */
256#if defined(__LITTLE_ENDIAN_BITFIELD)
257 unsigned dc :2; /* Descriptor Code */
258 unsigned reserved :6;
259#elif defined(__BIG_ENDIAN_BITFIELD)
260 unsigned reserved :6;
261 unsigned dc :2; /* Descriptor Code */
262#else
263#error "Bitfield endianness not defined! Check your byteorder.h"
264#endif
265 u8 length_msb; /* Block Length (MSB)*/
266 u16 length; /* Block Length */
267} idefloppy_capacity_descriptor_t;
268
269#define CAPACITY_INVALID 0x00
270#define CAPACITY_UNFORMATTED 0x01
271#define CAPACITY_CURRENT 0x02
272#define CAPACITY_NO_CARTRIDGE 0x03
273
274/*
275 * Most of our global data which we need to save even as we leave the
276 * driver due to an interrupt or a timer event is stored in a variable
277 * of type idefloppy_floppy_t, defined below.
278 */
279typedef struct ide_floppy_obj {
280 ide_drive_t *drive;
281 ide_driver_t *driver;
282 struct gendisk *disk;
283 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100284 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* Current packet command */
287 idefloppy_pc_t *pc;
288 /* Last failed packet command */
289 idefloppy_pc_t *failed_pc;
290 /* Packet command stack */
291 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
292 /* Next free packet command storage space */
293 int pc_stack_index;
294 struct request rq_stack[IDEFLOPPY_PC_STACK];
295 /* We implement a circular array */
296 int rq_stack_index;
297
298 /*
299 * Last error information
300 */
301 u8 sense_key, asc, ascq;
302 /* delay this long before sending packet command */
303 u8 ticks;
304 int progress_indication;
305
306 /*
307 * Device information
308 */
309 /* Current format */
310 int blocks, block_size, bs_factor;
311 /* Last format capacity */
312 idefloppy_capacity_descriptor_t capacity;
313 /* Copy of the flexible disk page */
314 idefloppy_flexible_disk_page_t flexible_disk_page;
315 /* Write protect */
316 int wp;
317 /* Supports format progress report */
318 int srfp;
319 /* Status/Action flags */
320 unsigned long flags;
321} idefloppy_floppy_t;
322
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200323#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325/*
326 * Floppy flag bits values.
327 */
328#define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
329#define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
330#define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
331#define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
332#define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
333#define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
334
335/*
336 * ATAPI floppy drive packet commands
337 */
338#define IDEFLOPPY_FORMAT_UNIT_CMD 0x04
339#define IDEFLOPPY_INQUIRY_CMD 0x12
340#define IDEFLOPPY_MODE_SELECT_CMD 0x55
341#define IDEFLOPPY_MODE_SENSE_CMD 0x5a
342#define IDEFLOPPY_READ10_CMD 0x28
343#define IDEFLOPPY_READ12_CMD 0xa8
344#define IDEFLOPPY_READ_CAPACITY_CMD 0x23
345#define IDEFLOPPY_REQUEST_SENSE_CMD 0x03
346#define IDEFLOPPY_PREVENT_REMOVAL_CMD 0x1e
347#define IDEFLOPPY_SEEK_CMD 0x2b
348#define IDEFLOPPY_START_STOP_CMD 0x1b
349#define IDEFLOPPY_TEST_UNIT_READY_CMD 0x00
350#define IDEFLOPPY_VERIFY_CMD 0x2f
351#define IDEFLOPPY_WRITE10_CMD 0x2a
352#define IDEFLOPPY_WRITE12_CMD 0xaa
353#define IDEFLOPPY_WRITE_VERIFY_CMD 0x2e
354
355/*
356 * Defines for the mode sense command
357 */
358#define MODE_SENSE_CURRENT 0x00
359#define MODE_SENSE_CHANGEABLE 0x01
360#define MODE_SENSE_DEFAULT 0x02
361#define MODE_SENSE_SAVED 0x03
362
363/*
364 * IOCTLs used in low-level formatting.
365 */
366
367#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
368#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
369#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
370#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
373 * Error codes which are returned in rq->errors to the higher part
374 * of the driver.
375 */
376#define IDEFLOPPY_ERROR_GENERAL 101
377
378/*
379 * The following is used to format the general configuration word of
380 * the ATAPI IDENTIFY DEVICE command.
381 */
382struct idefloppy_id_gcw {
383#if defined(__LITTLE_ENDIAN_BITFIELD)
384 unsigned packet_size :2; /* Packet Size */
385 unsigned reserved234 :3; /* Reserved */
386 unsigned drq_type :2; /* Command packet DRQ type */
387 unsigned removable :1; /* Removable media */
388 unsigned device_type :5; /* Device type */
389 unsigned reserved13 :1; /* Reserved */
390 unsigned protocol :2; /* Protocol type */
391#elif defined(__BIG_ENDIAN_BITFIELD)
392 unsigned protocol :2; /* Protocol type */
393 unsigned reserved13 :1; /* Reserved */
394 unsigned device_type :5; /* Device type */
395 unsigned removable :1; /* Removable media */
396 unsigned drq_type :2; /* Command packet DRQ type */
397 unsigned reserved234 :3; /* Reserved */
398 unsigned packet_size :2; /* Packet Size */
399#else
400#error "Bitfield endianness not defined! Check your byteorder.h"
401#endif
402};
403
404/*
405 * INQUIRY packet command - Data Format
406 */
407typedef struct {
408#if defined(__LITTLE_ENDIAN_BITFIELD)
409 unsigned device_type :5; /* Peripheral Device Type */
410 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
411 unsigned reserved1_6t0 :7; /* Reserved */
412 unsigned rmb :1; /* Removable Medium Bit */
413 unsigned ansi_version :3; /* ANSI Version */
414 unsigned ecma_version :3; /* ECMA Version */
415 unsigned iso_version :2; /* ISO Version */
416 unsigned response_format :4; /* Response Data Format */
417 unsigned reserved3_45 :2; /* Reserved */
418 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
419 unsigned reserved3_7 :1; /* AENC - Reserved */
420#elif defined(__BIG_ENDIAN_BITFIELD)
421 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
422 unsigned device_type :5; /* Peripheral Device Type */
423 unsigned rmb :1; /* Removable Medium Bit */
424 unsigned reserved1_6t0 :7; /* Reserved */
425 unsigned iso_version :2; /* ISO Version */
426 unsigned ecma_version :3; /* ECMA Version */
427 unsigned ansi_version :3; /* ANSI Version */
428 unsigned reserved3_7 :1; /* AENC - Reserved */
429 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
430 unsigned reserved3_45 :2; /* Reserved */
431 unsigned response_format :4; /* Response Data Format */
432#else
433#error "Bitfield endianness not defined! Check your byteorder.h"
434#endif
435 u8 additional_length; /* Additional Length (total_length-4) */
436 u8 rsv5, rsv6, rsv7; /* Reserved */
437 u8 vendor_id[8]; /* Vendor Identification */
438 u8 product_id[16]; /* Product Identification */
439 u8 revision_level[4]; /* Revision Level */
440 u8 vendor_specific[20]; /* Vendor Specific - Optional */
441 u8 reserved56t95[40]; /* Reserved - Optional */
442 /* Additional information may be returned */
443} idefloppy_inquiry_result_t;
444
445/*
446 * REQUEST SENSE packet command result - Data Format.
447 */
448typedef struct {
449#if defined(__LITTLE_ENDIAN_BITFIELD)
450 unsigned error_code :7; /* Current error (0x70) */
451 unsigned valid :1; /* The information field conforms to SFF-8070i */
452 u8 reserved1 :8; /* Reserved */
453 unsigned sense_key :4; /* Sense Key */
454 unsigned reserved2_4 :1; /* Reserved */
455 unsigned ili :1; /* Incorrect Length Indicator */
456 unsigned reserved2_67 :2;
457#elif defined(__BIG_ENDIAN_BITFIELD)
458 unsigned valid :1; /* The information field conforms to SFF-8070i */
459 unsigned error_code :7; /* Current error (0x70) */
460 u8 reserved1 :8; /* Reserved */
461 unsigned reserved2_67 :2;
462 unsigned ili :1; /* Incorrect Length Indicator */
463 unsigned reserved2_4 :1; /* Reserved */
464 unsigned sense_key :4; /* Sense Key */
465#else
466#error "Bitfield endianness not defined! Check your byteorder.h"
467#endif
468 u32 information __attribute__ ((packed));
469 u8 asl; /* Additional sense length (n-7) */
470 u32 command_specific; /* Additional command specific information */
471 u8 asc; /* Additional Sense Code */
472 u8 ascq; /* Additional Sense Code Qualifier */
473 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
474 u8 sksv[3];
475 u8 pad[2]; /* Padding to 20 bytes */
476} idefloppy_request_sense_result_t;
477
478/*
479 * Pages of the SELECT SENSE / MODE SENSE packet commands.
480 */
481#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
482#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
483
484/*
485 * Mode Parameter Header for the MODE SENSE packet command
486 */
487typedef struct {
488 u16 mode_data_length; /* Length of the following data transfer */
489 u8 medium_type; /* Medium Type */
490#if defined(__LITTLE_ENDIAN_BITFIELD)
491 unsigned reserved3 :7;
492 unsigned wp :1; /* Write protect */
493#elif defined(__BIG_ENDIAN_BITFIELD)
494 unsigned wp :1; /* Write protect */
495 unsigned reserved3 :7;
496#else
497#error "Bitfield endianness not defined! Check your byteorder.h"
498#endif
499 u8 reserved[4];
500} idefloppy_mode_parameter_header_t;
501
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800502static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
505
506#define ide_floppy_g(disk) \
507 container_of((disk)->private_data, struct ide_floppy_obj, driver)
508
509static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
510{
511 struct ide_floppy_obj *floppy = NULL;
512
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800513 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 floppy = ide_floppy_g(disk);
515 if (floppy)
516 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800517 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return floppy;
519}
520
521static void ide_floppy_release(struct kref *);
522
523static void ide_floppy_put(struct ide_floppy_obj *floppy)
524{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800525 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 kref_put(&floppy->kref, ide_floppy_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800527 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
531 * Too bad. The drive wants to send us data which we are not ready to accept.
532 * Just throw it away.
533 */
534static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
535{
536 while (bcount--)
537 (void) HWIF(drive)->INB(IDE_DATA_REG);
538}
539
540#if IDEFLOPPY_DEBUG_BUGS
541static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
542{
543 while (bcount--)
544 HWIF(drive)->OUTB(0, IDE_DATA_REG);
545}
546#endif /* IDEFLOPPY_DEBUG_BUGS */
547
548
549/*
550 * idefloppy_do_end_request is used to finish servicing a request.
551 *
552 * For read/write requests, we will call ide_end_request to pass to the
553 * next buffer.
554 */
555static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
556{
557 idefloppy_floppy_t *floppy = drive->driver_data;
558 struct request *rq = HWGROUP(drive)->rq;
559 int error;
560
561 debug_log(KERN_INFO "Reached idefloppy_end_request\n");
562
563 switch (uptodate) {
564 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
565 case 1: error = 0; break;
566 default: error = uptodate;
567 }
568 if (error)
569 floppy->failed_pc = NULL;
570 /* Why does this happen? */
571 if (!rq)
572 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200573 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* our real local end request function */
575 ide_end_request(drive, uptodate, nsecs);
576 return 0;
577 }
578 rq->errors = error;
579 /* fixme: need to move this local also */
580 ide_end_drive_cmd(drive, 0, 0);
581 return 0;
582}
583
584static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
585{
586 struct request *rq = pc->rq;
587 struct bio_vec *bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200588 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 unsigned long flags;
590 char *data;
NeilBrown5705f702007-09-25 12:35:59 +0200591 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
NeilBrown5705f702007-09-25 12:35:59 +0200593 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200594 if (!bcount)
595 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Jens Axboe6c92e692007-08-16 13:43:12 +0200597 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Jens Axboe6c92e692007-08-16 13:43:12 +0200599 data = bvec_kmap_irq(bvec, &flags);
600 drive->hwif->atapi_input_bytes(drive, data, count);
601 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Jens Axboe6c92e692007-08-16 13:43:12 +0200603 bcount -= count;
604 pc->b_count += count;
605 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 idefloppy_do_end_request(drive, 1, done >> 9);
609
610 if (bcount) {
611 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
612 idefloppy_discard_data(drive, bcount);
613 }
614}
615
616static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
617{
618 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200619 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct bio_vec *bvec;
621 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200622 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 char *data;
624
NeilBrown5705f702007-09-25 12:35:59 +0200625 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200626 if (!bcount)
627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jens Axboe6c92e692007-08-16 13:43:12 +0200629 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Jens Axboe6c92e692007-08-16 13:43:12 +0200631 data = bvec_kmap_irq(bvec, &flags);
632 drive->hwif->atapi_output_bytes(drive, data, count);
633 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jens Axboe6c92e692007-08-16 13:43:12 +0200635 bcount -= count;
636 pc->b_count += count;
637 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 idefloppy_do_end_request(drive, 1, done >> 9);
641
Jan Beulichc7ea4b32005-06-23 00:09:59 -0700642#if IDEFLOPPY_DEBUG_BUGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (bcount) {
644 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
645 idefloppy_write_zeros(drive, bcount);
646 }
Jan Beulichc7ea4b32005-06-23 00:09:59 -0700647#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
651{
652 struct request *rq = pc->rq;
653 struct bio *bio = rq->bio;
654
655 while ((bio = rq->bio) != NULL)
656 idefloppy_do_end_request(drive, 1, 0);
657}
658
659/*
660 * idefloppy_queue_pc_head generates a new packet command request in front
661 * of the request queue, before the current request, so that it will be
662 * processed immediately, on the next pass through the driver.
663 */
664static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
665{
666 struct ide_floppy_obj *floppy = drive->driver_data;
667
668 ide_init_drive_cmd(rq);
669 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200670 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 rq->rq_disk = floppy->disk;
672 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
673}
674
675static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
676{
677 idefloppy_floppy_t *floppy = drive->driver_data;
678
679 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
680 floppy->pc_stack_index=0;
681 return (&floppy->pc_stack[floppy->pc_stack_index++]);
682}
683
684static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
685{
686 idefloppy_floppy_t *floppy = drive->driver_data;
687
688 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
689 floppy->rq_stack_index = 0;
690 return (&floppy->rq_stack[floppy->rq_stack_index++]);
691}
692
693/*
694 * idefloppy_analyze_error is called on each failed packet command retry
695 * to analyze the request sense.
696 */
697static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
698{
699 idefloppy_floppy_t *floppy = drive->driver_data;
700
701 floppy->sense_key = result->sense_key;
702 floppy->asc = result->asc;
703 floppy->ascq = result->ascq;
704 floppy->progress_indication = result->sksv[0] & 0x80 ?
705 (u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
706 if (floppy->failed_pc)
707 debug_log(KERN_INFO "ide-floppy: pc = %x, sense key = %x, "
708 "asc = %x, ascq = %x\n", floppy->failed_pc->c[0],
709 result->sense_key, result->asc, result->ascq);
710 else
711 debug_log(KERN_INFO "ide-floppy: sense key = %x, asc = %x, "
712 "ascq = %x\n", result->sense_key,
713 result->asc, result->ascq);
714}
715
716static void idefloppy_request_sense_callback (ide_drive_t *drive)
717{
718 idefloppy_floppy_t *floppy = drive->driver_data;
719
720 debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
721
722 if (!floppy->pc->error) {
723 idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
724 idefloppy_do_end_request(drive, 1, 0);
725 } else {
726 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
727 idefloppy_do_end_request(drive, 0, 0);
728 }
729}
730
731/*
732 * General packet command callback function.
733 */
734static void idefloppy_pc_callback (ide_drive_t *drive)
735{
736 idefloppy_floppy_t *floppy = drive->driver_data;
737
738 debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
739
740 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
741}
742
743/*
744 * idefloppy_init_pc initializes a packet command.
745 */
746static void idefloppy_init_pc (idefloppy_pc_t *pc)
747{
748 memset(pc->c, 0, 12);
749 pc->retries = 0;
750 pc->flags = 0;
751 pc->request_transfer = 0;
752 pc->buffer = pc->pc_buffer;
753 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
754 pc->callback = &idefloppy_pc_callback;
755}
756
757static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
758{
759 idefloppy_init_pc(pc);
760 pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
761 pc->c[4] = 255;
762 pc->request_transfer = 18;
763 pc->callback = &idefloppy_request_sense_callback;
764}
765
766/*
767 * idefloppy_retry_pc is called when an error was detected during the
768 * last packet command. We queue a request sense packet command in
769 * the head of the request list.
770 */
771static void idefloppy_retry_pc (ide_drive_t *drive)
772{
773 idefloppy_pc_t *pc;
774 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100776 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 pc = idefloppy_next_pc_storage(drive);
778 rq = idefloppy_next_rq_storage(drive);
779 idefloppy_create_request_sense_cmd(pc);
780 idefloppy_queue_pc_head(drive, pc, rq);
781}
782
783/*
784 * idefloppy_pc_intr is the usual interrupt handler which will be called
785 * during a packet command.
786 */
787static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
788{
789 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100790 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 atapi_ireason_t ireason;
792 idefloppy_pc_t *pc = floppy->pc;
793 struct request *rq = pc->rq;
794 unsigned int temp;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100795 u16 bcount;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100796 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 debug_log(KERN_INFO "ide-floppy: Reached %s interrupt handler\n",
799 __FUNCTION__);
800
801 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
802 if (HWIF(drive)->ide_dma_end(drive)) {
803 set_bit(PC_DMA_ERROR, &pc->flags);
804 } else {
805 pc->actually_transferred = pc->request_transfer;
806 idefloppy_update_buffers(drive, pc);
807 }
808 debug_log(KERN_INFO "ide-floppy: DMA finished\n");
809 }
810
811 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100812 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100814 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 debug_log(KERN_INFO "Packet command completed, %d bytes "
816 "transferred\n", pc->actually_transferred);
817 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
818
Ingo Molnar366c7f52006-07-03 00:25:25 -0700819 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100821 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* Error detected */
823 debug_log(KERN_INFO "ide-floppy: %s: I/O error\n",
824 drive->name);
825 rq->errors++;
826 if (pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
827 printk(KERN_ERR "ide-floppy: I/O error in "
828 "request sense command\n");
829 return ide_do_reset(drive);
830 }
831 /* Retry operation */
832 idefloppy_retry_pc(drive);
833 /* queued, but not started */
834 return ide_stopped;
835 }
836 pc->error = 0;
837 if (floppy->failed_pc == pc)
838 floppy->failed_pc = NULL;
839 /* Command finished - Call the callback function */
840 pc->callback(drive);
841 return ide_stopped;
842 }
843
844 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
845 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
846 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100847 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return ide_do_reset(drive);
849 }
850
851 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100852 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
853 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* on this interrupt */
855 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
856
857 if (ireason.b.cod) {
858 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
859 return ide_do_reset(drive);
860 }
861 if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) {
862 /* Hopefully, we will never get here */
863 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
864 ireason.b.io ? "Write":"Read");
865 printk(KERN_ERR "but the floppy wants us to %s !\n",
866 ireason.b.io ? "Read":"Write");
867 return ide_do_reset(drive);
868 }
869 if (!test_bit(PC_WRITING, &pc->flags)) {
870 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100871 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (temp > pc->request_transfer) {
873 if (temp > pc->buffer_size) {
874 printk(KERN_ERR "ide-floppy: The floppy wants "
875 "to send us more data than expected "
876 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100877 idefloppy_discard_data(drive, bcount);
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700878 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 ide_set_handler(drive,
880 &idefloppy_pc_intr,
881 IDEFLOPPY_WAIT_CMD,
882 NULL);
883 return ide_started;
884 }
885 debug_log(KERN_NOTICE "ide-floppy: The floppy wants to "
886 "send us more data than expected - "
887 "allowing transfer\n");
888 }
889 }
890 if (test_bit(PC_WRITING, &pc->flags)) {
891 if (pc->buffer != NULL)
892 /* Write the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100893 hwif->atapi_output_bytes(drive, pc->current_position,
894 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100896 idefloppy_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 } else {
898 if (pc->buffer != NULL)
899 /* Read the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100900 hwif->atapi_input_bytes(drive, pc->current_position,
901 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100903 idefloppy_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100906 pc->actually_transferred += bcount;
907 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700909 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
911 return ide_started;
912}
913
914/*
915 * This is the original routine that did the packet transfer.
916 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
917 * for that drive below. The algorithm is chosen based on drive type
918 */
919static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
920{
921 ide_startstop_t startstop;
922 idefloppy_floppy_t *floppy = drive->driver_data;
923 atapi_ireason_t ireason;
924
925 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
926 printk(KERN_ERR "ide-floppy: Strange, packet command "
927 "initiated yet DRQ isn't asserted\n");
928 return startstop;
929 }
930 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
931 if (!ireason.b.cod || ireason.b.io) {
932 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
933 "issuing a packet command\n");
934 return ide_do_reset(drive);
935 }
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700936 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* Set the interrupt routine */
938 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
939 /* Send the actual packet */
940 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
941 return ide_started;
942}
943
944
945/*
946 * What we have here is a classic case of a top half / bottom half
947 * interrupt service routine. In interrupt mode, the device sends
948 * an interrupt to signal it's ready to receive a packet. However,
949 * we need to delay about 2-3 ticks before issuing the packet or we
950 * gets in trouble.
951 *
952 * So, follow carefully. transfer_pc1 is called as an interrupt (or
953 * directly). In either case, when the device says it's ready for a
954 * packet, we schedule the packet transfer to occur about 2-3 ticks
955 * later in transfer_pc2.
956 */
957static int idefloppy_transfer_pc2 (ide_drive_t *drive)
958{
959 idefloppy_floppy_t *floppy = drive->driver_data;
960
961 /* Send the actual packet */
962 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
963 /* Timeout for the packet command */
964 return IDEFLOPPY_WAIT_CMD;
965}
966
967static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
968{
969 idefloppy_floppy_t *floppy = drive->driver_data;
970 ide_startstop_t startstop;
971 atapi_ireason_t ireason;
972
973 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
974 printk(KERN_ERR "ide-floppy: Strange, packet command "
975 "initiated yet DRQ isn't asserted\n");
976 return startstop;
977 }
978 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
979 if (!ireason.b.cod || ireason.b.io) {
980 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
981 "while issuing a packet command\n");
982 return ide_do_reset(drive);
983 }
984 /*
985 * The following delay solves a problem with ATAPI Zip 100 drives
986 * where the Busy flag was apparently being deasserted before the
987 * unit was ready to receive data. This was happening on a
988 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
989 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
990 * used until after the packet is moved in about 50 msec.
991 */
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700992 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 ide_set_handler(drive,
994 &idefloppy_pc_intr, /* service routine for packet command */
995 floppy->ticks, /* wait this long before "failing" */
996 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
997 return ide_started;
998}
999
1000/**
1001 * idefloppy_should_report_error()
1002 *
1003 * Supresses error messages resulting from Medium not present
1004 */
1005static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
1006{
1007 if (floppy->sense_key == 0x02 &&
1008 floppy->asc == 0x3a &&
1009 floppy->ascq == 0x00)
1010 return 0;
1011 return 1;
1012}
1013
1014/*
1015 * Issue a packet command
1016 */
1017static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
1018{
1019 idefloppy_floppy_t *floppy = drive->driver_data;
1020 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001022 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001023 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (floppy->failed_pc == NULL &&
1026 pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
1027 floppy->failed_pc = pc;
1028 /* Set the current packet command */
1029 floppy->pc = pc;
1030
1031 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
1032 test_bit(PC_ABORT, &pc->flags)) {
1033 /*
1034 * We will "abort" retrying a packet command in case
1035 * a legitimate error code was received.
1036 */
1037 if (!test_bit(PC_ABORT, &pc->flags)) {
1038 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
1039 if (idefloppy_should_report_error(floppy))
1040 printk(KERN_ERR "ide-floppy: %s: I/O error, "
1041 "pc = %2x, key = %2x, "
1042 "asc = %2x, ascq = %2x\n",
1043 drive->name, pc->c[0],
1044 floppy->sense_key,
1045 floppy->asc, floppy->ascq);
1046 }
1047 /* Giving up */
1048 pc->error = IDEFLOPPY_ERROR_GENERAL;
1049 }
1050 floppy->failed_pc = NULL;
1051 pc->callback(drive);
1052 return ide_stopped;
1053 }
1054
1055 debug_log(KERN_INFO "Retry number - %d\n",pc->retries);
1056
1057 pc->retries++;
1058 /* We haven't transferred any data yet */
1059 pc->actually_transferred = 0;
1060 pc->current_position = pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001061 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001063 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
1064 ide_dma_off(drive);
1065
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001066 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001069 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 if (IDE_CONTROL_REG)
1072 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
1073 /* Use PIO/DMA */
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001074 hwif->OUTB(dma, IDE_FEATURE_REG);
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001075 hwif->OUTB((bcount >> 8) & 0xff, IDE_BCOUNTH_REG);
1076 hwif->OUTB(bcount & 0xff, IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
1078
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001079 if (dma) { /* Begin DMA, if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1081 hwif->dma_start(drive);
1082 }
1083
1084 /* Can we transfer the packet when we get the interrupt or wait? */
1085 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
1086 /* wait */
1087 pkt_xfer_routine = &idefloppy_transfer_pc1;
1088 } else {
1089 /* immediate */
1090 pkt_xfer_routine = &idefloppy_transfer_pc;
1091 }
1092
1093 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1094 /* Issue the packet command */
1095 ide_execute_command(drive, WIN_PACKETCMD,
1096 pkt_xfer_routine,
1097 IDEFLOPPY_WAIT_CMD,
1098 NULL);
1099 return ide_started;
1100 } else {
1101 /* Issue the packet command */
1102 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1103 return (*pkt_xfer_routine) (drive);
1104 }
1105}
1106
1107static void idefloppy_rw_callback (ide_drive_t *drive)
1108{
1109 debug_log(KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
1110
1111 idefloppy_do_end_request(drive, 1, 0);
1112 return;
1113}
1114
1115static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1116{
1117 debug_log(KERN_INFO "ide-floppy: creating prevent removal command, "
1118 "prevent = %d\n", prevent);
1119
1120 idefloppy_init_pc(pc);
1121 pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
1122 pc->c[4] = prevent;
1123}
1124
1125static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1126{
1127 idefloppy_init_pc(pc);
1128 pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
1129 pc->c[7] = 255;
1130 pc->c[8] = 255;
1131 pc->request_transfer = 255;
1132}
1133
1134static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1135 int flags)
1136{
1137 idefloppy_init_pc(pc);
1138 pc->c[0] = IDEFLOPPY_FORMAT_UNIT_CMD;
1139 pc->c[1] = 0x17;
1140
1141 memset(pc->buffer, 0, 12);
1142 pc->buffer[1] = 0xA2;
1143 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1144
1145 if (flags & 1) /* Verify bit on... */
1146 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
1147 pc->buffer[3] = 8;
1148
1149 put_unaligned(htonl(b), (unsigned int *)(&pc->buffer[4]));
1150 put_unaligned(htonl(l), (unsigned int *)(&pc->buffer[8]));
1151 pc->buffer_size=12;
1152 set_bit(PC_WRITING, &pc->flags);
1153}
1154
1155/*
1156 * A mode sense command is used to "sense" floppy parameters.
1157 */
1158static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
1159{
1160 u16 length = sizeof(idefloppy_mode_parameter_header_t);
1161
1162 idefloppy_init_pc(pc);
1163 pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
1164 pc->c[1] = 0;
1165 pc->c[2] = page_code + (type << 6);
1166
1167 switch (page_code) {
1168 case IDEFLOPPY_CAPABILITIES_PAGE:
1169 length += 12;
1170 break;
1171 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1172 length += 32;
1173 break;
1174 default:
1175 printk(KERN_ERR "ide-floppy: unsupported page code "
1176 "in create_mode_sense_cmd\n");
1177 }
1178 put_unaligned(htons(length), (u16 *) &pc->c[7]);
1179 pc->request_transfer = length;
1180}
1181
1182static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1183{
1184 idefloppy_init_pc(pc);
1185 pc->c[0] = IDEFLOPPY_START_STOP_CMD;
1186 pc->c[4] = start;
1187}
1188
1189static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1190{
1191 idefloppy_init_pc(pc);
1192 pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
1193}
1194
1195static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1196{
1197 int block = sector / floppy->bs_factor;
1198 int blocks = rq->nr_sectors / floppy->bs_factor;
1199 int cmd = rq_data_dir(rq);
1200
1201 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1202 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1203 block, blocks);
1204
1205 idefloppy_init_pc(pc);
1206 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
1207 pc->c[0] = cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
1208 put_unaligned(htonl(blocks), (unsigned int *) &pc->c[6]);
1209 } else {
1210 pc->c[0] = cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
1211 put_unaligned(htons(blocks), (unsigned short *) &pc->c[7]);
1212 }
1213 put_unaligned(htonl(block), (unsigned int *) &pc->c[2]);
1214 pc->callback = &idefloppy_rw_callback;
1215 pc->rq = rq;
1216 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001217 if (rq->cmd_flags & REQ_RW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 set_bit(PC_WRITING, &pc->flags);
1219 pc->buffer = NULL;
1220 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1221 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1222}
1223
Jens Axboe3d6392c2007-07-09 12:38:05 +02001224static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 idefloppy_init_pc(pc);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001228 pc->callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +02001230 pc->rq = rq;
1231 pc->b_count = rq->data_len;
1232 if (rq->data_len && rq_data_dir(rq) == WRITE)
1233 set_bit(PC_WRITING, &pc->flags);
1234 pc->buffer = rq->data;
1235 if (rq->bio)
1236 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1237
1238 /*
1239 * possibly problematic, doesn't look like ide-floppy correctly
1240 * handled scattered requests if dma fails...
1241 */
1242 pc->request_transfer = pc->buffer_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245/*
1246 * idefloppy_do_request is our request handling function.
1247 */
1248static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1249{
1250 idefloppy_floppy_t *floppy = drive->driver_data;
1251 idefloppy_pc_t *pc;
1252 unsigned long block = (unsigned long)block_s;
1253
Jens Axboecdd60262006-07-28 09:32:07 +02001254 debug_log(KERN_INFO "dev: %s, flags: %lx, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -07001255 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 rq->flags, rq->errors);
1257 debug_log(KERN_INFO "sector: %ld, nr_sectors: %ld, "
1258 "current_nr_sectors: %d\n", (long)rq->sector,
1259 rq->nr_sectors, rq->current_nr_sectors);
1260
1261 if (rq->errors >= ERROR_MAX) {
1262 if (floppy->failed_pc != NULL) {
1263 if (idefloppy_should_report_error(floppy))
1264 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1265 " key = %2x, asc = %2x, ascq = %2x\n",
1266 drive->name, floppy->failed_pc->c[0],
1267 floppy->sense_key, floppy->asc, floppy->ascq);
1268 }
1269 else
1270 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1271 drive->name);
1272 idefloppy_do_end_request(drive, 0, 0);
1273 return ide_stopped;
1274 }
Jens Axboe4aff5e22006-08-10 08:44:47 +02001275 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (((long)rq->sector % floppy->bs_factor) ||
1277 (rq->nr_sectors % floppy->bs_factor)) {
1278 printk("%s: unsupported r/w request size\n",
1279 drive->name);
1280 idefloppy_do_end_request(drive, 0, 0);
1281 return ide_stopped;
1282 }
1283 pc = idefloppy_next_pc_storage(drive);
1284 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001285 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 pc = (idefloppy_pc_t *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001287 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001289 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 } else {
1291 blk_dump_rq_flags(rq,
1292 "ide-floppy: unsupported command in queue");
1293 idefloppy_do_end_request(drive, 0, 0);
1294 return ide_stopped;
1295 }
1296
1297 pc->rq = rq;
1298 return idefloppy_issue_pc(drive, pc);
1299}
1300
1301/*
1302 * idefloppy_queue_pc_tail adds a special packet command request to the
1303 * tail of the request queue, and waits for it to be serviced.
1304 */
1305static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1306{
1307 struct ide_floppy_obj *floppy = drive->driver_data;
1308 struct request rq;
1309
1310 ide_init_drive_cmd (&rq);
1311 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001312 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 rq.rq_disk = floppy->disk;
1314
1315 return ide_do_drive_cmd(drive, &rq, ide_wait);
1316}
1317
1318/*
1319 * Look at the flexible disk page parameters. We will ignore the CHS
1320 * capacity parameters and use the LBA parameters instead.
1321 */
1322static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1323{
1324 idefloppy_floppy_t *floppy = drive->driver_data;
1325 idefloppy_pc_t pc;
1326 idefloppy_mode_parameter_header_t *header;
1327 idefloppy_flexible_disk_page_t *page;
1328 int capacity, lba_capacity;
1329
1330 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1331 if (idefloppy_queue_pc_tail(drive,&pc)) {
1332 printk(KERN_ERR "ide-floppy: Can't get flexible disk "
1333 "page parameters\n");
1334 return 1;
1335 }
1336 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1337 floppy->wp = header->wp;
1338 set_disk_ro(floppy->disk, floppy->wp);
1339 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1340
1341 page->transfer_rate = ntohs(page->transfer_rate);
1342 page->sector_size = ntohs(page->sector_size);
1343 page->cyls = ntohs(page->cyls);
1344 page->rpm = ntohs(page->rpm);
1345 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1346 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1347 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1348 "%d sector size, %d rpm\n",
1349 drive->name, capacity / 1024, page->cyls,
1350 page->heads, page->sectors,
1351 page->transfer_rate / 8, page->sector_size, page->rpm);
1352
1353 floppy->flexible_disk_page = *page;
1354 drive->bios_cyl = page->cyls;
1355 drive->bios_head = page->heads;
1356 drive->bios_sect = page->sectors;
1357 lba_capacity = floppy->blocks * floppy->block_size;
1358 if (capacity < lba_capacity) {
1359 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1360 "bytes, but the drive only handles %d\n",
1361 drive->name, lba_capacity, capacity);
1362 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1363 }
1364 return 0;
1365}
1366
1367static int idefloppy_get_capability_page(ide_drive_t *drive)
1368{
1369 idefloppy_floppy_t *floppy = drive->driver_data;
1370 idefloppy_pc_t pc;
1371 idefloppy_mode_parameter_header_t *header;
1372 idefloppy_capabilities_page_t *page;
1373
1374 floppy->srfp = 0;
1375 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1376 MODE_SENSE_CURRENT);
1377
1378 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1379 if (idefloppy_queue_pc_tail(drive,&pc)) {
1380 return 1;
1381 }
1382
1383 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1384 page= (idefloppy_capabilities_page_t *)(header+1);
1385 floppy->srfp = page->srfp;
1386 return (0);
1387}
1388
1389/*
1390 * Determine if a media is present in the floppy drive, and if so,
1391 * its LBA capacity.
1392 */
1393static int idefloppy_get_capacity (ide_drive_t *drive)
1394{
1395 idefloppy_floppy_t *floppy = drive->driver_data;
1396 idefloppy_pc_t pc;
1397 idefloppy_capacity_header_t *header;
1398 idefloppy_capacity_descriptor_t *descriptor;
1399 int i, descriptors, rc = 1, blocks, length;
1400
1401 drive->bios_cyl = 0;
1402 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +01001403 floppy->blocks = 0;
1404 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 set_capacity(floppy->disk, 0);
1406
1407 idefloppy_create_read_capacity_cmd(&pc);
1408 if (idefloppy_queue_pc_tail(drive, &pc)) {
1409 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1410 return 1;
1411 }
1412 header = (idefloppy_capacity_header_t *) pc.buffer;
1413 descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1414 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1415
1416 for (i = 0; i < descriptors; i++, descriptor++) {
1417 blocks = descriptor->blocks = ntohl(descriptor->blocks);
1418 length = descriptor->length = ntohs(descriptor->length);
1419
1420 if (!i)
1421 {
1422 switch (descriptor->dc) {
1423 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1424 case CAPACITY_UNFORMATTED:
1425 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1426 /*
1427 * If it is not a clik drive, break out
1428 * (maintains previous driver behaviour)
1429 */
1430 break;
1431 case CAPACITY_CURRENT:
1432 /* Normal Zip/LS-120 disks */
1433 if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1434 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1435 "sector size\n", drive->name,
1436 blocks * length / 1024, blocks, length);
1437 floppy->capacity = *descriptor;
1438 if (!length || length % 512) {
1439 printk(KERN_NOTICE "%s: %d bytes block size "
1440 "not supported\n", drive->name, length);
1441 } else {
1442 floppy->blocks = blocks;
1443 floppy->block_size = length;
1444 if ((floppy->bs_factor = length / 512) != 1)
1445 printk(KERN_NOTICE "%s: warning: non "
1446 "512 bytes block size not "
1447 "fully supported\n",
1448 drive->name);
1449 rc = 0;
1450 }
1451 break;
1452 case CAPACITY_NO_CARTRIDGE:
1453 /*
1454 * This is a KERN_ERR so it appears on screen
1455 * for the user to see
1456 */
1457 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1458 break;
1459 case CAPACITY_INVALID:
1460 printk(KERN_ERR "%s: Invalid capacity for disk "
1461 "in drive\n", drive->name);
1462 break;
1463 }
1464 }
1465 if (!i) {
1466 debug_log( "Descriptor 0 Code: %d\n",
1467 descriptor->dc);
1468 }
1469 debug_log( "Descriptor %d: %dkB, %d blocks, %d "
1470 "sector size\n", i, blocks * length / 1024, blocks,
1471 length);
1472 }
1473
1474 /* Clik! disk does not support get_flexible_disk_page */
1475 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1476 (void) idefloppy_get_flexible_disk_page(drive);
1477 }
1478
1479 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1480 return rc;
1481}
1482
1483/*
1484** Obtain the list of formattable capacities.
1485** Very similar to idefloppy_get_capacity, except that we push the capacity
1486** descriptors to userland, instead of our own structures.
1487**
1488** Userland gives us the following structure:
1489**
1490** struct idefloppy_format_capacities {
1491** int nformats;
1492** struct {
1493** int nblocks;
1494** int blocksize;
1495** } formats[];
1496** } ;
1497**
1498** userland initializes nformats to the number of allocated formats[]
1499** records. On exit we set nformats to the number of records we've
1500** actually initialized.
1501**
1502*/
1503
1504static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1505{
1506 idefloppy_pc_t pc;
1507 idefloppy_capacity_header_t *header;
1508 idefloppy_capacity_descriptor_t *descriptor;
1509 int i, descriptors, blocks, length;
1510 int u_array_size;
1511 int u_index;
1512 int __user *argp;
1513
1514 if (get_user(u_array_size, arg))
1515 return (-EFAULT);
1516
1517 if (u_array_size <= 0)
1518 return (-EINVAL);
1519
1520 idefloppy_create_read_capacity_cmd(&pc);
1521 if (idefloppy_queue_pc_tail(drive, &pc)) {
1522 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1523 return (-EIO);
1524 }
1525 header = (idefloppy_capacity_header_t *) pc.buffer;
1526 descriptors = header->length /
1527 sizeof(idefloppy_capacity_descriptor_t);
1528 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1529
1530 u_index = 0;
1531 argp = arg + 1;
1532
1533 /*
1534 ** We always skip the first capacity descriptor. That's the
1535 ** current capacity. We are interested in the remaining descriptors,
1536 ** the formattable capacities.
1537 */
1538
1539 for (i=0; i<descriptors; i++, descriptor++) {
1540 if (u_index >= u_array_size)
1541 break; /* User-supplied buffer too small */
1542 if (i == 0)
1543 continue; /* Skip the first descriptor */
1544
1545 blocks = ntohl(descriptor->blocks);
1546 length = ntohs(descriptor->length);
1547
1548 if (put_user(blocks, argp))
1549 return(-EFAULT);
1550 ++argp;
1551
1552 if (put_user(length, argp))
1553 return (-EFAULT);
1554 ++argp;
1555
1556 ++u_index;
1557 }
1558
1559 if (put_user(u_index, arg))
1560 return (-EFAULT);
1561 return (0);
1562}
1563
1564/*
1565** Send ATAPI_FORMAT_UNIT to the drive.
1566**
1567** Userland gives us the following structure:
1568**
1569** struct idefloppy_format_command {
1570** int nblocks;
1571** int blocksize;
1572** int flags;
1573** } ;
1574**
1575** flags is a bitmask, currently, the only defined flag is:
1576**
1577** 0x01 - verify media after format.
1578*/
1579
1580static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1581{
1582 int blocks;
1583 int length;
1584 int flags;
1585 idefloppy_pc_t pc;
1586
1587 if (get_user(blocks, arg) ||
1588 get_user(length, arg+1) ||
1589 get_user(flags, arg+2)) {
1590 return (-EFAULT);
1591 }
1592
1593 /* Get the SFRP bit */
1594 (void) idefloppy_get_capability_page(drive);
1595 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1596 if (idefloppy_queue_pc_tail(drive, &pc)) {
1597 return (-EIO);
1598 }
1599
1600 return (0);
1601}
1602
1603/*
1604** Get ATAPI_FORMAT_UNIT progress indication.
1605**
Matt LaPlante0779bf22006-11-30 05:24:39 +01001606** Userland gives a pointer to an int. The int is set to a progress
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607** indicator 0-65536, with 65536=100%.
1608**
1609** If the drive does not support format progress indication, we just check
1610** the dsc bit, and return either 0 or 65536.
1611*/
1612
1613static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1614{
1615 idefloppy_floppy_t *floppy = drive->driver_data;
1616 idefloppy_pc_t pc;
1617 int progress_indication = 0x10000;
1618
1619 if (floppy->srfp) {
1620 idefloppy_create_request_sense_cmd(&pc);
1621 if (idefloppy_queue_pc_tail(drive, &pc)) {
1622 return (-EIO);
1623 }
1624
1625 if (floppy->sense_key == 2 &&
1626 floppy->asc == 4 &&
1627 floppy->ascq == 4) {
1628 progress_indication = floppy->progress_indication;
1629 }
1630 /* Else assume format_unit has finished, and we're
1631 ** at 0x10000 */
1632 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001634 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 local_irq_save(flags);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001637 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 local_irq_restore(flags);
1639
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001640 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 if (put_user(progress_indication, arg))
1643 return (-EFAULT);
1644
1645 return (0);
1646}
1647
1648/*
1649 * Return the current floppy capacity.
1650 */
1651static sector_t idefloppy_capacity (ide_drive_t *drive)
1652{
1653 idefloppy_floppy_t *floppy = drive->driver_data;
1654 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1655
1656 return capacity;
1657}
1658
1659/*
1660 * idefloppy_identify_device checks if we can support a drive,
1661 * based on the ATAPI IDENTIFY command results.
1662 */
1663static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1664{
1665 struct idefloppy_id_gcw gcw;
1666#if IDEFLOPPY_DEBUG_INFO
1667 u16 mask,i;
1668 char buffer[80];
1669#endif /* IDEFLOPPY_DEBUG_INFO */
1670
1671 *((u16 *) &gcw) = id->config;
1672
1673#ifdef CONFIG_PPC
1674 /* kludge for Apple PowerBook internal zip */
1675 if ((gcw.device_type == 5) &&
1676 !strstr(id->model, "CD-ROM") &&
1677 strstr(id->model, "ZIP"))
1678 gcw.device_type = 0;
1679#endif
1680
1681#if IDEFLOPPY_DEBUG_INFO
1682 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1683 switch (gcw.protocol) {
1684 case 0: case 1: sprintf(buffer, "ATA");break;
1685 case 2: sprintf(buffer, "ATAPI");break;
1686 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1687 }
1688 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1689 switch (gcw.device_type) {
1690 case 0: sprintf(buffer, "Direct-access Device");break;
1691 case 1: sprintf(buffer, "Streaming Tape Device");break;
1692 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1693 case 5: sprintf(buffer, "CD-ROM Device");break;
1694 case 6: sprintf(buffer, "Reserved");
1695 case 7: sprintf(buffer, "Optical memory Device");break;
1696 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1697 default: sprintf(buffer, "Reserved");
1698 }
1699 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1700 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1701 switch (gcw.drq_type) {
1702 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1703 case 1: sprintf(buffer, "Interrupt DRQ");break;
1704 case 2: sprintf(buffer, "Accelerated DRQ");break;
1705 case 3: sprintf(buffer, "Reserved");break;
1706 }
1707 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1708 switch (gcw.packet_size) {
1709 case 0: sprintf(buffer, "12 bytes");break;
1710 case 1: sprintf(buffer, "16 bytes");break;
1711 default: sprintf(buffer, "Reserved");break;
1712 }
1713 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
1714 printk(KERN_INFO "Model: %.40s\n",id->model);
1715 printk(KERN_INFO "Firmware Revision: %.8s\n",id->fw_rev);
1716 printk(KERN_INFO "Serial Number: %.20s\n",id->serial_no);
1717 printk(KERN_INFO "Write buffer size(?): %d bytes\n",id->buf_size*512);
1718 printk(KERN_INFO "DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
1719 printk(KERN_INFO "LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
1720 printk(KERN_INFO "IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
1721 printk(KERN_INFO "IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
1722 printk(KERN_INFO "ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
1723 printk(KERN_INFO "PIO Cycle Timing Category: %d\n",id->tPIO);
1724 printk(KERN_INFO "DMA Cycle Timing Category: %d\n",id->tDMA);
1725 printk(KERN_INFO "Single Word DMA supported modes:\n");
1726 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1727 if (id->dma_1word & mask)
1728 printk(KERN_INFO " Mode %d%s\n", i,
1729 (id->dma_1word & (mask << 8)) ? " (active)" : "");
1730 }
1731 printk(KERN_INFO "Multi Word DMA supported modes:\n");
1732 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1733 if (id->dma_mword & mask)
1734 printk(KERN_INFO " Mode %d%s\n", i,
1735 (id->dma_mword & (mask << 8)) ? " (active)" : "");
1736 }
1737 if (id->field_valid & 0x0002) {
1738 printk(KERN_INFO "Enhanced PIO Modes: %s\n",
1739 id->eide_pio_modes & 1 ? "Mode 3":"None");
1740 if (id->eide_dma_min == 0)
1741 sprintf(buffer, "Not supported");
1742 else
1743 sprintf(buffer, "%d ns",id->eide_dma_min);
1744 printk(KERN_INFO "Minimum Multi-word DMA cycle per word: %s\n", buffer);
1745 if (id->eide_dma_time == 0)
1746 sprintf(buffer, "Not supported");
1747 else
1748 sprintf(buffer, "%d ns",id->eide_dma_time);
1749 printk(KERN_INFO "Manufacturer\'s Recommended Multi-word cycle: %s\n", buffer);
1750 if (id->eide_pio == 0)
1751 sprintf(buffer, "Not supported");
1752 else
1753 sprintf(buffer, "%d ns",id->eide_pio);
1754 printk(KERN_INFO "Minimum PIO cycle without IORDY: %s\n",
1755 buffer);
1756 if (id->eide_pio_iordy == 0)
1757 sprintf(buffer, "Not supported");
1758 else
1759 sprintf(buffer, "%d ns",id->eide_pio_iordy);
1760 printk(KERN_INFO "Minimum PIO cycle with IORDY: %s\n", buffer);
1761 } else
1762 printk(KERN_INFO "According to the device, fields 64-70 are not valid.\n");
1763#endif /* IDEFLOPPY_DEBUG_INFO */
1764
1765 if (gcw.protocol != 2)
1766 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1767 else if (gcw.device_type != 0)
1768 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1769 else if (!gcw.removable)
1770 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1771 else if (gcw.drq_type == 3) {
1772 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1773 } else if (gcw.packet_size != 0) {
1774 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1775 } else
1776 return 1;
1777 return 0;
1778}
1779
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001780#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781static void idefloppy_add_settings(ide_drive_t *drive)
1782{
1783 idefloppy_floppy_t *floppy = drive->driver_data;
1784
1785/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001786 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001788 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1789 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1790 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1791 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001793#else
1794static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1795#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797/*
1798 * Driver initialization.
1799 */
1800static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1801{
1802 struct idefloppy_id_gcw gcw;
1803
1804 *((u16 *) &gcw) = drive->id->config;
1805 floppy->pc = floppy->pc_stack;
1806 if (gcw.drq_type == 1)
1807 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1808 /*
1809 * We used to check revisions here. At this point however
1810 * I'm giving up. Just assume they are all broken, its easier.
1811 *
1812 * The actual reason for the workarounds was likely
1813 * a driver bug after all rather than a firmware bug,
1814 * and the workaround below used to hide it. It should
1815 * be fixed as of version 1.9, but to be on the safe side
1816 * we'll leave the limitation below for the 2.2.x tree.
1817 */
1818
1819 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1820 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1821 /* This value will be visible in the /proc/ide/hdx/settings */
1822 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1823 blk_queue_max_sectors(drive->queue, 64);
1824 }
1825
1826 /*
1827 * Guess what? The IOMEGA Clik! drive also needs the
1828 * above fix. It makes nasty clicking noises without
1829 * it, so please don't remove this.
1830 */
1831 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1832 blk_queue_max_sectors(drive->queue, 64);
1833 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1834 }
1835
1836
1837 (void) idefloppy_get_capacity(drive);
1838 idefloppy_add_settings(drive);
1839}
1840
Russell King4031bbe2006-01-06 11:41:00 +00001841static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 idefloppy_floppy_t *floppy = drive->driver_data;
1844 struct gendisk *g = floppy->disk;
1845
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001846 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 del_gendisk(g);
1849
1850 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853static void ide_floppy_release(struct kref *kref)
1854{
1855 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1856 ide_drive_t *drive = floppy->drive;
1857 struct gendisk *g = floppy->disk;
1858
1859 drive->driver_data = NULL;
1860 g->private_data = NULL;
1861 put_disk(g);
1862 kfree(floppy);
1863}
1864
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001865#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866static int proc_idefloppy_read_capacity
1867 (char *page, char **start, off_t off, int count, int *eof, void *data)
1868{
1869 ide_drive_t*drive = (ide_drive_t *)data;
1870 int len;
1871
1872 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1873 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1874}
1875
1876static ide_proc_entry_t idefloppy_proc[] = {
1877 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1878 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1879 { NULL, 0, NULL, NULL }
1880};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001881#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Russell King4031bbe2006-01-06 11:41:00 +00001883static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001886 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001887 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001888 .name = "ide-floppy",
1889 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001890 },
Russell King4031bbe2006-01-06 11:41:00 +00001891 .probe = ide_floppy_probe,
1892 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 .version = IDEFLOPPY_VERSION,
1894 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 .do_request = idefloppy_do_request,
1897 .end_request = idefloppy_do_end_request,
1898 .error = __ide_error,
1899 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001900#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903};
1904
1905static int idefloppy_open(struct inode *inode, struct file *filp)
1906{
1907 struct gendisk *disk = inode->i_bdev->bd_disk;
1908 struct ide_floppy_obj *floppy;
1909 ide_drive_t *drive;
1910 idefloppy_pc_t pc;
1911 int ret = 0;
1912
1913 debug_log(KERN_INFO "Reached idefloppy_open\n");
1914
1915 if (!(floppy = ide_floppy_get(disk)))
1916 return -ENXIO;
1917
1918 drive = floppy->drive;
1919
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001920 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001922 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1924 /* Just in case */
1925
1926 idefloppy_create_test_unit_ready_cmd(&pc);
1927 if (idefloppy_queue_pc_tail(drive, &pc)) {
1928 idefloppy_create_start_stop_cmd(&pc, 1);
1929 (void) idefloppy_queue_pc_tail(drive, &pc);
1930 }
1931
1932 if (idefloppy_get_capacity (drive)
1933 && (filp->f_flags & O_NDELAY) == 0
1934 /*
1935 ** Allow O_NDELAY to open a drive without a disk, or with
1936 ** an unreadable disk, so that we can get the format
1937 ** capacity of the drive or begin the format - Sam
1938 */
1939 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ret = -EIO;
1941 goto out_put_floppy;
1942 }
1943
1944 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 ret = -EROFS;
1946 goto out_put_floppy;
1947 }
1948 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1949 /* IOMEGA Clik! drives do not support lock/unlock commands */
1950 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1951 idefloppy_create_prevent_cmd(&pc, 1);
1952 (void) idefloppy_queue_pc_tail(drive, &pc);
1953 }
1954 check_disk_change(inode->i_bdev);
1955 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 ret = -EBUSY;
1957 goto out_put_floppy;
1958 }
1959 return 0;
1960
1961out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001962 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 ide_floppy_put(floppy);
1964 return ret;
1965}
1966
1967static int idefloppy_release(struct inode *inode, struct file *filp)
1968{
1969 struct gendisk *disk = inode->i_bdev->bd_disk;
1970 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1971 ide_drive_t *drive = floppy->drive;
1972 idefloppy_pc_t pc;
1973
1974 debug_log(KERN_INFO "Reached idefloppy_release\n");
1975
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001976 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /* IOMEGA Clik! drives do not support lock/unlock commands */
1978 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1979 idefloppy_create_prevent_cmd(&pc, 0);
1980 (void) idefloppy_queue_pc_tail(drive, &pc);
1981 }
1982
1983 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1984 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001985
1986 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 ide_floppy_put(floppy);
1989
1990 return 0;
1991}
1992
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001993static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1994{
1995 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1996 ide_drive_t *drive = floppy->drive;
1997
1998 geo->heads = drive->bios_head;
1999 geo->sectors = drive->bios_sect;
2000 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
2001 return 0;
2002}
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004static int idefloppy_ioctl(struct inode *inode, struct file *file,
2005 unsigned int cmd, unsigned long arg)
2006{
2007 struct block_device *bdev = inode->i_bdev;
2008 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
2009 ide_drive_t *drive = floppy->drive;
2010 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01002011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 int prevent = (arg) ? 1 : 0;
2013 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 switch (cmd) {
2016 case CDROMEJECT:
2017 prevent = 0;
2018 /* fall through */
2019 case CDROM_LOCKDOOR:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01002020 if (floppy->openers > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return -EBUSY;
2022
2023 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
2024 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
2025 idefloppy_create_prevent_cmd(&pc, prevent);
2026 (void) idefloppy_queue_pc_tail(drive, &pc);
2027 }
2028 if (cmd == CDROMEJECT) {
2029 idefloppy_create_start_stop_cmd(&pc, 2);
2030 (void) idefloppy_queue_pc_tail(drive, &pc);
2031 }
2032 return 0;
2033 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
2034 return 0;
2035 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
2036 return idefloppy_get_format_capacities(drive, argp);
2037 case IDEFLOPPY_IOCTL_FORMAT_START:
2038
2039 if (!(file->f_mode & 2))
2040 return -EPERM;
2041
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01002042 if (floppy->openers > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /* Don't format if someone is using the disk */
2044
2045 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
2046 &floppy->flags);
2047 return -EBUSY;
2048 }
2049
2050 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
2051
2052 err = idefloppy_begin_format(drive, argp);
2053 if (err)
2054 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
2055 return err;
2056 /*
2057 ** Note, the bit will be cleared when the device is
2058 ** closed. This is the cleanest way to handle the
2059 ** situation where the drive does not support
2060 ** format progress reporting.
2061 */
2062 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
2063 return idefloppy_get_format_progress(drive, argp);
2064 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02002065
2066 /*
2067 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
2068 * and CDROM_SEND_PACKET (legacy) ioctls
2069 */
2070 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
2071 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
2072 bdev->bd_disk, cmd, argp);
2073 else
2074 err = -ENOTTY;
2075
2076 if (err == -ENOTTY)
2077 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2078
2079 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
2082static int idefloppy_media_changed(struct gendisk *disk)
2083{
2084 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
2085 ide_drive_t *drive = floppy->drive;
2086
2087 /* do not scan partitions twice if this is a removable device */
2088 if (drive->attach) {
2089 drive->attach = 0;
2090 return 0;
2091 }
2092 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
2093}
2094
2095static int idefloppy_revalidate_disk(struct gendisk *disk)
2096{
2097 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
2098 set_capacity(disk, idefloppy_capacity(floppy->drive));
2099 return 0;
2100}
2101
2102static struct block_device_operations idefloppy_ops = {
2103 .owner = THIS_MODULE,
2104 .open = idefloppy_open,
2105 .release = idefloppy_release,
2106 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002107 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 .media_changed = idefloppy_media_changed,
2109 .revalidate_disk= idefloppy_revalidate_disk
2110};
2111
Russell King4031bbe2006-01-06 11:41:00 +00002112static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 idefloppy_floppy_t *floppy;
2115 struct gendisk *g;
2116
2117 if (!strstr("ide-floppy", drive->driver_req))
2118 goto failed;
2119 if (!drive->present)
2120 goto failed;
2121 if (drive->media != ide_floppy)
2122 goto failed;
2123 if (!idefloppy_identify_device (drive, drive->id)) {
2124 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
2125 goto failed;
2126 }
2127 if (drive->scsi) {
2128 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
2129 goto failed;
2130 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002131 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
2133 goto failed;
2134 }
2135
2136 g = alloc_disk(1 << PARTN_BITS);
2137 if (!g)
2138 goto out_free_floppy;
2139
2140 ide_init_disk(g, drive);
2141
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002142 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 kref_init(&floppy->kref);
2145
2146 floppy->drive = drive;
2147 floppy->driver = &idefloppy_driver;
2148 floppy->disk = g;
2149
2150 g->private_data = &floppy->driver;
2151
2152 drive->driver_data = floppy;
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 idefloppy_setup (drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 g->minors = 1 << PARTN_BITS;
2157 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
2159 g->fops = &idefloppy_ops;
2160 drive->attach = 1;
2161 add_disk(g);
2162 return 0;
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164out_free_floppy:
2165 kfree(floppy);
2166failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002167 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
2170MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2171
2172static void __exit idefloppy_exit (void)
2173{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002174 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002177static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
2179 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002180 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Kay Sievers263756e2005-12-12 18:03:44 +01002183MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184module_init(idefloppy_init);
2185module_exit(idefloppy_exit);
2186MODULE_LICENSE("GPL");