blob: c63dc4039ab943a0de1c7fd2682e50505385252d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01002 * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
3 * Copyright (C) 2000-2002 Paul Bristow <paul@paulbristow.net>
4 * Copyright (C) 2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
7/*
8 * IDE ATAPI floppy driver.
9 *
10 * 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 *
19 * Many thanks to Lode Leroy <Lode.Leroy@www.ibase.be>, who tested so many
20 * ALPHA patches to this driver on an EASYSTOR LS-120 ATAPI floppy drive.
21 *
22 * Ver 0.1 Oct 17 96 Initial test version, mostly based on ide-tape.c.
23 * Ver 0.2 Oct 31 96 Minor changes.
24 * Ver 0.3 Dec 2 96 Fixed error recovery bug.
25 * Ver 0.4 Jan 26 97 Add support for the HDIO_GETGEO ioctl.
26 * Ver 0.5 Feb 21 97 Add partitions support.
27 * Use the minimum of the LBA and CHS capacities.
28 * Avoid hwgroup->rq == NULL on the last irq.
29 * Fix potential null dereferencing with DEBUG_LOG.
30 * Ver 0.8 Dec 7 97 Increase irq timeout from 10 to 50 seconds.
31 * Add media write-protect detection.
32 * Issue START command only if TEST UNIT READY fails.
33 * Add work-around for IOMEGA ZIP revision 21.D.
34 * Remove idefloppy_get_capabilities().
35 * Ver 0.9 Jul 4 99 Fix a bug which might have caused the number of
36 * bytes requested on each interrupt to be zero.
37 * Thanks to <shanos@es.co.nz> for pointing this out.
38 * Ver 0.9.sv Jan 6 01 Sam Varshavchik <mrsam@courier-mta.com>
39 * Implement low level formatting. Reimplemented
40 * IDEFLOPPY_CAPABILITIES_PAGE, since we need the srfp
41 * bit. My LS-120 drive barfs on
42 * IDEFLOPPY_CAPABILITIES_PAGE, but maybe it's just me.
43 * Compromise by not reporting a failure to get this
44 * mode page. Implemented four IOCTLs in order to
45 * implement formatting. IOCTls begin with 0x4600,
46 * 0x46 is 'F' as in Format.
47 * Jan 9 01 Userland option to select format verify.
48 * Added PC_SUPPRESS_ERROR flag - some idefloppy drives
49 * do not implement IDEFLOPPY_CAPABILITIES_PAGE, and
50 * return a sense error. Suppress error reporting in
51 * this particular case in order to avoid spurious
52 * errors in syslog. The culprit is
53 * idefloppy_get_capability_page(), so move it to
54 * idefloppy_begin_format() so that it's not used
55 * unless absolutely necessary.
56 * If drive does not support format progress indication
57 * monitor the dsc bit in the status register.
58 * Also, O_NDELAY on open will allow the device to be
59 * opened without a disk available. This can be used to
60 * open an unformatted disk, or get the device capacity.
61 * Ver 0.91 Dec 11 99 Added IOMEGA Clik! drive support by
62 * <paul@paulbristow.net>
63 * Ver 0.92 Oct 22 00 Paul Bristow became official maintainer for this
64 * driver. Included Powerbook internal zip kludge.
65 * Ver 0.93 Oct 24 00 Fixed bugs for Clik! drive
66 * no disk on insert and disk change now works
67 * Ver 0.94 Oct 27 00 Tidied up to remove strstr(Clik) everywhere
68 * Ver 0.95 Nov 7 00 Brought across to kernel 2.4
69 * Ver 0.96 Jan 7 01 Actually in line with release version of 2.4.0
70 * including set_bit patch from Rusty Russell
71 * Ver 0.97 Jul 22 01 Merge 0.91-0.96 onto 0.9.sv for ac series
72 * Ver 0.97.sv Aug 3 01 Backported from 2.4.7-ac3
73 * Ver 0.98 Oct 26 01 Split idefloppy_transfer_pc into two pieces to
74 * fix a lost interrupt problem. It appears the busy
75 * bit was being deasserted by my IOMEGA ATAPI ZIP 100
76 * drive before the drive was actually ready.
77 * Ver 0.98a Oct 29 01 Expose delay value so we can play.
78 * Ver 0.99 Feb 24 02 Remove duplicate code, modify clik! detection code
79 * to support new PocketZip drives
80 */
81
82#define IDEFLOPPY_VERSION "0.99.newide"
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/module.h>
85#include <linux/types.h>
86#include <linux/string.h>
87#include <linux/kernel.h>
88#include <linux/delay.h>
89#include <linux/timer.h>
90#include <linux/mm.h>
91#include <linux/interrupt.h>
92#include <linux/major.h>
93#include <linux/errno.h>
94#include <linux/genhd.h>
95#include <linux/slab.h>
96#include <linux/cdrom.h>
97#include <linux/ide.h>
98#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080099#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +0200101#include <scsi/scsi_ioctl.h>
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <asm/byteorder.h>
104#include <asm/irq.h>
105#include <asm/uaccess.h>
106#include <asm/io.h>
107#include <asm/unaligned.h>
108
109/*
110 * The following are used to debug the driver.
111 */
112#define IDEFLOPPY_DEBUG_LOG 0
113#define IDEFLOPPY_DEBUG_INFO 0
114#define IDEFLOPPY_DEBUG_BUGS 1
115
116/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
117#define IDEFLOPPY_DEBUG( fmt, args... )
118
119#if IDEFLOPPY_DEBUG_LOG
120#define debug_log printk
121#else
122#define debug_log(fmt, args... ) do {} while(0)
123#endif
124
125
126/*
127 * Some drives require a longer irq timeout.
128 */
129#define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
130
131/*
132 * After each failed packet command we issue a request sense command
133 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
134 */
135#define IDEFLOPPY_MAX_PC_RETRIES 3
136
137/*
138 * With each packet command, we allocate a buffer of
139 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
140 */
141#define IDEFLOPPY_PC_BUFFER_SIZE 256
142
143/*
144 * In various places in the driver, we need to allocate storage
145 * for packet commands and requests, which will remain valid while
146 * we leave the driver to wait for an interrupt or a timeout event.
147 */
148#define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
149
150/*
151 * Our view of a packet command.
152 */
153typedef struct idefloppy_packet_command_s {
154 u8 c[12]; /* Actual packet bytes */
155 int retries; /* On each retry, we increment retries */
156 int error; /* Error code */
157 int request_transfer; /* Bytes to transfer */
158 int actually_transferred; /* Bytes actually transferred */
159 int buffer_size; /* Size of our data buffer */
160 int b_count; /* Missing/Available data on the current buffer */
161 struct request *rq; /* The corresponding request */
162 u8 *buffer; /* Data buffer */
163 u8 *current_position; /* Pointer into the above buffer */
164 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
165 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
166 unsigned long flags; /* Status/Action bit flags: long for set_bit */
167} idefloppy_pc_t;
168
169/*
170 * Packet command flag bits.
171 */
172#define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
173#define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
174#define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
175#define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
176#define PC_WRITING 5 /* Data direction */
177
178#define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
179
180/*
181 * Removable Block Access Capabilities Page
182 */
183typedef struct {
184#if defined(__LITTLE_ENDIAN_BITFIELD)
185 unsigned page_code :6; /* Page code - Should be 0x1b */
186 unsigned reserved1_6 :1; /* Reserved */
187 unsigned ps :1; /* Should be 0 */
188#elif defined(__BIG_ENDIAN_BITFIELD)
189 unsigned ps :1; /* Should be 0 */
190 unsigned reserved1_6 :1; /* Reserved */
191 unsigned page_code :6; /* Page code - Should be 0x1b */
192#else
193#error "Bitfield endianness not defined! Check your byteorder.h"
194#endif
195 u8 page_length; /* Page Length - Should be 0xa */
196#if defined(__LITTLE_ENDIAN_BITFIELD)
197 unsigned reserved2 :6;
198 unsigned srfp :1; /* Supports reporting progress of format */
199 unsigned sflp :1; /* System floppy type device */
200 unsigned tlun :3; /* Total logical units supported by the device */
201 unsigned reserved3 :3;
202 unsigned sml :1; /* Single / Multiple lun supported */
203 unsigned ncd :1; /* Non cd optical device */
204#elif defined(__BIG_ENDIAN_BITFIELD)
205 unsigned sflp :1; /* System floppy type device */
206 unsigned srfp :1; /* Supports reporting progress of format */
207 unsigned reserved2 :6;
208 unsigned ncd :1; /* Non cd optical device */
209 unsigned sml :1; /* Single / Multiple lun supported */
210 unsigned reserved3 :3;
211 unsigned tlun :3; /* Total logical units supported by the device */
212#else
213#error "Bitfield endianness not defined! Check your byteorder.h"
214#endif
215 u8 reserved[8];
216} idefloppy_capabilities_page_t;
217
218/*
219 * Flexible disk page.
220 */
221typedef struct {
222#if defined(__LITTLE_ENDIAN_BITFIELD)
223 unsigned page_code :6; /* Page code - Should be 0x5 */
224 unsigned reserved1_6 :1; /* Reserved */
225 unsigned ps :1; /* The device is capable of saving the page */
226#elif defined(__BIG_ENDIAN_BITFIELD)
227 unsigned ps :1; /* The device is capable of saving the page */
228 unsigned reserved1_6 :1; /* Reserved */
229 unsigned page_code :6; /* Page code - Should be 0x5 */
230#else
231#error "Bitfield endianness not defined! Check your byteorder.h"
232#endif
233 u8 page_length; /* Page Length - Should be 0x1e */
234 u16 transfer_rate; /* In kilobits per second */
235 u8 heads, sectors; /* Number of heads, Number of sectors per track */
236 u16 sector_size; /* Byes per sector */
237 u16 cyls; /* Number of cylinders */
238 u8 reserved10[10];
239 u8 motor_delay; /* Motor off delay */
240 u8 reserved21[7];
241 u16 rpm; /* Rotations per minute */
242 u8 reserved30[2];
243} idefloppy_flexible_disk_page_t;
244
245/*
246 * Format capacity
247 */
248typedef struct {
249 u8 reserved[3];
250 u8 length; /* Length of the following descriptors in bytes */
251} idefloppy_capacity_header_t;
252
253typedef struct {
254 u32 blocks; /* Number of blocks */
255#if defined(__LITTLE_ENDIAN_BITFIELD)
256 unsigned dc :2; /* Descriptor Code */
257 unsigned reserved :6;
258#elif defined(__BIG_ENDIAN_BITFIELD)
259 unsigned reserved :6;
260 unsigned dc :2; /* Descriptor Code */
261#else
262#error "Bitfield endianness not defined! Check your byteorder.h"
263#endif
264 u8 length_msb; /* Block Length (MSB)*/
265 u16 length; /* Block Length */
266} idefloppy_capacity_descriptor_t;
267
268#define CAPACITY_INVALID 0x00
269#define CAPACITY_UNFORMATTED 0x01
270#define CAPACITY_CURRENT 0x02
271#define CAPACITY_NO_CARTRIDGE 0x03
272
273/*
274 * Most of our global data which we need to save even as we leave the
275 * driver due to an interrupt or a timer event is stored in a variable
276 * of type idefloppy_floppy_t, defined below.
277 */
278typedef struct ide_floppy_obj {
279 ide_drive_t *drive;
280 ide_driver_t *driver;
281 struct gendisk *disk;
282 struct kref kref;
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +0100283 unsigned int openers; /* protected by BKL for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* Current packet command */
286 idefloppy_pc_t *pc;
287 /* Last failed packet command */
288 idefloppy_pc_t *failed_pc;
289 /* Packet command stack */
290 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
291 /* Next free packet command storage space */
292 int pc_stack_index;
293 struct request rq_stack[IDEFLOPPY_PC_STACK];
294 /* We implement a circular array */
295 int rq_stack_index;
296
297 /*
298 * Last error information
299 */
300 u8 sense_key, asc, ascq;
301 /* delay this long before sending packet command */
302 u8 ticks;
303 int progress_indication;
304
305 /*
306 * Device information
307 */
308 /* Current format */
309 int blocks, block_size, bs_factor;
310 /* Last format capacity */
311 idefloppy_capacity_descriptor_t capacity;
312 /* Copy of the flexible disk page */
313 idefloppy_flexible_disk_page_t flexible_disk_page;
314 /* Write protect */
315 int wp;
316 /* Supports format progress report */
317 int srfp;
318 /* Status/Action flags */
319 unsigned long flags;
320} idefloppy_floppy_t;
321
Bartlomiej Zolnierkiewiczc40d3d32005-08-18 22:09:21 +0200322#define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324/*
325 * Floppy flag bits values.
326 */
327#define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
328#define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
329#define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
330#define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
331#define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
332#define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
333
334/*
335 * ATAPI floppy drive packet commands
336 */
337#define IDEFLOPPY_FORMAT_UNIT_CMD 0x04
338#define IDEFLOPPY_INQUIRY_CMD 0x12
339#define IDEFLOPPY_MODE_SELECT_CMD 0x55
340#define IDEFLOPPY_MODE_SENSE_CMD 0x5a
341#define IDEFLOPPY_READ10_CMD 0x28
342#define IDEFLOPPY_READ12_CMD 0xa8
343#define IDEFLOPPY_READ_CAPACITY_CMD 0x23
344#define IDEFLOPPY_REQUEST_SENSE_CMD 0x03
345#define IDEFLOPPY_PREVENT_REMOVAL_CMD 0x1e
346#define IDEFLOPPY_SEEK_CMD 0x2b
347#define IDEFLOPPY_START_STOP_CMD 0x1b
348#define IDEFLOPPY_TEST_UNIT_READY_CMD 0x00
349#define IDEFLOPPY_VERIFY_CMD 0x2f
350#define IDEFLOPPY_WRITE10_CMD 0x2a
351#define IDEFLOPPY_WRITE12_CMD 0xaa
352#define IDEFLOPPY_WRITE_VERIFY_CMD 0x2e
353
354/*
355 * Defines for the mode sense command
356 */
357#define MODE_SENSE_CURRENT 0x00
358#define MODE_SENSE_CHANGEABLE 0x01
359#define MODE_SENSE_DEFAULT 0x02
360#define MODE_SENSE_SAVED 0x03
361
362/*
363 * IOCTLs used in low-level formatting.
364 */
365
366#define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
367#define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
368#define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
369#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/*
372 * Error codes which are returned in rq->errors to the higher part
373 * of the driver.
374 */
375#define IDEFLOPPY_ERROR_GENERAL 101
376
377/*
378 * The following is used to format the general configuration word of
379 * the ATAPI IDENTIFY DEVICE command.
380 */
381struct idefloppy_id_gcw {
382#if defined(__LITTLE_ENDIAN_BITFIELD)
383 unsigned packet_size :2; /* Packet Size */
384 unsigned reserved234 :3; /* Reserved */
385 unsigned drq_type :2; /* Command packet DRQ type */
386 unsigned removable :1; /* Removable media */
387 unsigned device_type :5; /* Device type */
388 unsigned reserved13 :1; /* Reserved */
389 unsigned protocol :2; /* Protocol type */
390#elif defined(__BIG_ENDIAN_BITFIELD)
391 unsigned protocol :2; /* Protocol type */
392 unsigned reserved13 :1; /* Reserved */
393 unsigned device_type :5; /* Device type */
394 unsigned removable :1; /* Removable media */
395 unsigned drq_type :2; /* Command packet DRQ type */
396 unsigned reserved234 :3; /* Reserved */
397 unsigned packet_size :2; /* Packet Size */
398#else
399#error "Bitfield endianness not defined! Check your byteorder.h"
400#endif
401};
402
403/*
404 * INQUIRY packet command - Data Format
405 */
406typedef struct {
407#if defined(__LITTLE_ENDIAN_BITFIELD)
408 unsigned device_type :5; /* Peripheral Device Type */
409 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
410 unsigned reserved1_6t0 :7; /* Reserved */
411 unsigned rmb :1; /* Removable Medium Bit */
412 unsigned ansi_version :3; /* ANSI Version */
413 unsigned ecma_version :3; /* ECMA Version */
414 unsigned iso_version :2; /* ISO Version */
415 unsigned response_format :4; /* Response Data Format */
416 unsigned reserved3_45 :2; /* Reserved */
417 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
418 unsigned reserved3_7 :1; /* AENC - Reserved */
419#elif defined(__BIG_ENDIAN_BITFIELD)
420 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
421 unsigned device_type :5; /* Peripheral Device Type */
422 unsigned rmb :1; /* Removable Medium Bit */
423 unsigned reserved1_6t0 :7; /* Reserved */
424 unsigned iso_version :2; /* ISO Version */
425 unsigned ecma_version :3; /* ECMA Version */
426 unsigned ansi_version :3; /* ANSI Version */
427 unsigned reserved3_7 :1; /* AENC - Reserved */
428 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
429 unsigned reserved3_45 :2; /* Reserved */
430 unsigned response_format :4; /* Response Data Format */
431#else
432#error "Bitfield endianness not defined! Check your byteorder.h"
433#endif
434 u8 additional_length; /* Additional Length (total_length-4) */
435 u8 rsv5, rsv6, rsv7; /* Reserved */
436 u8 vendor_id[8]; /* Vendor Identification */
437 u8 product_id[16]; /* Product Identification */
438 u8 revision_level[4]; /* Revision Level */
439 u8 vendor_specific[20]; /* Vendor Specific - Optional */
440 u8 reserved56t95[40]; /* Reserved - Optional */
441 /* Additional information may be returned */
442} idefloppy_inquiry_result_t;
443
444/*
445 * REQUEST SENSE packet command result - Data Format.
446 */
447typedef struct {
448#if defined(__LITTLE_ENDIAN_BITFIELD)
449 unsigned error_code :7; /* Current error (0x70) */
450 unsigned valid :1; /* The information field conforms to SFF-8070i */
451 u8 reserved1 :8; /* Reserved */
452 unsigned sense_key :4; /* Sense Key */
453 unsigned reserved2_4 :1; /* Reserved */
454 unsigned ili :1; /* Incorrect Length Indicator */
455 unsigned reserved2_67 :2;
456#elif defined(__BIG_ENDIAN_BITFIELD)
457 unsigned valid :1; /* The information field conforms to SFF-8070i */
458 unsigned error_code :7; /* Current error (0x70) */
459 u8 reserved1 :8; /* Reserved */
460 unsigned reserved2_67 :2;
461 unsigned ili :1; /* Incorrect Length Indicator */
462 unsigned reserved2_4 :1; /* Reserved */
463 unsigned sense_key :4; /* Sense Key */
464#else
465#error "Bitfield endianness not defined! Check your byteorder.h"
466#endif
467 u32 information __attribute__ ((packed));
468 u8 asl; /* Additional sense length (n-7) */
469 u32 command_specific; /* Additional command specific information */
470 u8 asc; /* Additional Sense Code */
471 u8 ascq; /* Additional Sense Code Qualifier */
472 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
473 u8 sksv[3];
474 u8 pad[2]; /* Padding to 20 bytes */
475} idefloppy_request_sense_result_t;
476
477/*
478 * Pages of the SELECT SENSE / MODE SENSE packet commands.
479 */
480#define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
481#define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
482
483/*
484 * Mode Parameter Header for the MODE SENSE packet command
485 */
486typedef struct {
487 u16 mode_data_length; /* Length of the following data transfer */
488 u8 medium_type; /* Medium Type */
489#if defined(__LITTLE_ENDIAN_BITFIELD)
490 unsigned reserved3 :7;
491 unsigned wp :1; /* Write protect */
492#elif defined(__BIG_ENDIAN_BITFIELD)
493 unsigned wp :1; /* Write protect */
494 unsigned reserved3 :7;
495#else
496#error "Bitfield endianness not defined! Check your byteorder.h"
497#endif
498 u8 reserved[4];
499} idefloppy_mode_parameter_header_t;
500
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800501static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
504
505#define ide_floppy_g(disk) \
506 container_of((disk)->private_data, struct ide_floppy_obj, driver)
507
508static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
509{
510 struct ide_floppy_obj *floppy = NULL;
511
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800512 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 floppy = ide_floppy_g(disk);
514 if (floppy)
515 kref_get(&floppy->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800516 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return floppy;
518}
519
520static void ide_floppy_release(struct kref *);
521
522static void ide_floppy_put(struct ide_floppy_obj *floppy)
523{
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800524 mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 kref_put(&floppy->kref, ide_floppy_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800526 mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/*
530 * Too bad. The drive wants to send us data which we are not ready to accept.
531 * Just throw it away.
532 */
533static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
534{
535 while (bcount--)
536 (void) HWIF(drive)->INB(IDE_DATA_REG);
537}
538
539#if IDEFLOPPY_DEBUG_BUGS
540static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
541{
542 while (bcount--)
543 HWIF(drive)->OUTB(0, IDE_DATA_REG);
544}
545#endif /* IDEFLOPPY_DEBUG_BUGS */
546
547
548/*
549 * idefloppy_do_end_request is used to finish servicing a request.
550 *
551 * For read/write requests, we will call ide_end_request to pass to the
552 * next buffer.
553 */
554static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
555{
556 idefloppy_floppy_t *floppy = drive->driver_data;
557 struct request *rq = HWGROUP(drive)->rq;
558 int error;
559
560 debug_log(KERN_INFO "Reached idefloppy_end_request\n");
561
562 switch (uptodate) {
563 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
564 case 1: error = 0; break;
565 default: error = uptodate;
566 }
567 if (error)
568 floppy->failed_pc = NULL;
569 /* Why does this happen? */
570 if (!rq)
571 return 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200572 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* our real local end request function */
574 ide_end_request(drive, uptodate, nsecs);
575 return 0;
576 }
577 rq->errors = error;
578 /* fixme: need to move this local also */
579 ide_end_drive_cmd(drive, 0, 0);
580 return 0;
581}
582
583static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
584{
585 struct request *rq = pc->rq;
586 struct bio_vec *bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200587 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 unsigned long flags;
589 char *data;
NeilBrown5705f702007-09-25 12:35:59 +0200590 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
NeilBrown5705f702007-09-25 12:35:59 +0200592 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200593 if (!bcount)
594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Jens Axboe6c92e692007-08-16 13:43:12 +0200596 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Jens Axboe6c92e692007-08-16 13:43:12 +0200598 data = bvec_kmap_irq(bvec, &flags);
599 drive->hwif->atapi_input_bytes(drive, data, count);
600 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jens Axboe6c92e692007-08-16 13:43:12 +0200602 bcount -= count;
603 pc->b_count += count;
604 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 idefloppy_do_end_request(drive, 1, done >> 9);
608
609 if (bcount) {
610 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
611 idefloppy_discard_data(drive, bcount);
612 }
613}
614
615static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
616{
617 struct request *rq = pc->rq;
NeilBrown5705f702007-09-25 12:35:59 +0200618 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct bio_vec *bvec;
620 unsigned long flags;
NeilBrown5705f702007-09-25 12:35:59 +0200621 int count, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 char *data;
623
NeilBrown5705f702007-09-25 12:35:59 +0200624 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200625 if (!bcount)
626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Jens Axboe6c92e692007-08-16 13:43:12 +0200628 count = min(bvec->bv_len, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jens Axboe6c92e692007-08-16 13:43:12 +0200630 data = bvec_kmap_irq(bvec, &flags);
631 drive->hwif->atapi_output_bytes(drive, data, count);
632 bvec_kunmap_irq(data, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Jens Axboe6c92e692007-08-16 13:43:12 +0200634 bcount -= count;
635 pc->b_count += count;
636 done += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
639 idefloppy_do_end_request(drive, 1, done >> 9);
640
Jan Beulichc7ea4b32005-06-23 00:09:59 -0700641#if IDEFLOPPY_DEBUG_BUGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (bcount) {
643 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
644 idefloppy_write_zeros(drive, bcount);
645 }
Jan Beulichc7ea4b32005-06-23 00:09:59 -0700646#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
650{
651 struct request *rq = pc->rq;
652 struct bio *bio = rq->bio;
653
654 while ((bio = rq->bio) != NULL)
655 idefloppy_do_end_request(drive, 1, 0);
656}
657
658/*
659 * idefloppy_queue_pc_head generates a new packet command request in front
660 * of the request queue, before the current request, so that it will be
661 * processed immediately, on the next pass through the driver.
662 */
663static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
664{
665 struct ide_floppy_obj *floppy = drive->driver_data;
666
667 ide_init_drive_cmd(rq);
668 rq->buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200669 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 rq->rq_disk = floppy->disk;
671 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
672}
673
674static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
675{
676 idefloppy_floppy_t *floppy = drive->driver_data;
677
678 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
679 floppy->pc_stack_index=0;
680 return (&floppy->pc_stack[floppy->pc_stack_index++]);
681}
682
683static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
684{
685 idefloppy_floppy_t *floppy = drive->driver_data;
686
687 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
688 floppy->rq_stack_index = 0;
689 return (&floppy->rq_stack[floppy->rq_stack_index++]);
690}
691
692/*
693 * idefloppy_analyze_error is called on each failed packet command retry
694 * to analyze the request sense.
695 */
696static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
697{
698 idefloppy_floppy_t *floppy = drive->driver_data;
699
700 floppy->sense_key = result->sense_key;
701 floppy->asc = result->asc;
702 floppy->ascq = result->ascq;
703 floppy->progress_indication = result->sksv[0] & 0x80 ?
704 (u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
705 if (floppy->failed_pc)
706 debug_log(KERN_INFO "ide-floppy: pc = %x, sense key = %x, "
707 "asc = %x, ascq = %x\n", floppy->failed_pc->c[0],
708 result->sense_key, result->asc, result->ascq);
709 else
710 debug_log(KERN_INFO "ide-floppy: sense key = %x, asc = %x, "
711 "ascq = %x\n", result->sense_key,
712 result->asc, result->ascq);
713}
714
715static void idefloppy_request_sense_callback (ide_drive_t *drive)
716{
717 idefloppy_floppy_t *floppy = drive->driver_data;
718
719 debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
720
721 if (!floppy->pc->error) {
722 idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
723 idefloppy_do_end_request(drive, 1, 0);
724 } else {
725 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
726 idefloppy_do_end_request(drive, 0, 0);
727 }
728}
729
730/*
731 * General packet command callback function.
732 */
733static void idefloppy_pc_callback (ide_drive_t *drive)
734{
735 idefloppy_floppy_t *floppy = drive->driver_data;
736
737 debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
738
739 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
740}
741
742/*
743 * idefloppy_init_pc initializes a packet command.
744 */
745static void idefloppy_init_pc (idefloppy_pc_t *pc)
746{
747 memset(pc->c, 0, 12);
748 pc->retries = 0;
749 pc->flags = 0;
750 pc->request_transfer = 0;
751 pc->buffer = pc->pc_buffer;
752 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
753 pc->callback = &idefloppy_pc_callback;
754}
755
756static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
757{
758 idefloppy_init_pc(pc);
759 pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
760 pc->c[4] = 255;
761 pc->request_transfer = 18;
762 pc->callback = &idefloppy_request_sense_callback;
763}
764
765/*
766 * idefloppy_retry_pc is called when an error was detected during the
767 * last packet command. We queue a request sense packet command in
768 * the head of the request list.
769 */
770static void idefloppy_retry_pc (ide_drive_t *drive)
771{
772 idefloppy_pc_t *pc;
773 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +0100775 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 pc = idefloppy_next_pc_storage(drive);
777 rq = idefloppy_next_rq_storage(drive);
778 idefloppy_create_request_sense_cmd(pc);
779 idefloppy_queue_pc_head(drive, pc, rq);
780}
781
782/*
783 * idefloppy_pc_intr is the usual interrupt handler which will be called
784 * during a packet command.
785 */
786static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
787{
788 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100789 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 idefloppy_pc_t *pc = floppy->pc;
791 struct request *rq = pc->rq;
792 unsigned int temp;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100793 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100794 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 debug_log(KERN_INFO "ide-floppy: Reached %s interrupt handler\n",
797 __FUNCTION__);
798
799 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
800 if (HWIF(drive)->ide_dma_end(drive)) {
801 set_bit(PC_DMA_ERROR, &pc->flags);
802 } else {
803 pc->actually_transferred = pc->request_transfer;
804 idefloppy_update_buffers(drive, pc);
805 }
806 debug_log(KERN_INFO "ide-floppy: DMA finished\n");
807 }
808
809 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100810 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100812 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 debug_log(KERN_INFO "Packet command completed, %d bytes "
814 "transferred\n", pc->actually_transferred);
815 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
816
Ingo Molnar366c7f52006-07-03 00:25:25 -0700817 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100819 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* Error detected */
821 debug_log(KERN_INFO "ide-floppy: %s: I/O error\n",
822 drive->name);
823 rq->errors++;
824 if (pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
825 printk(KERN_ERR "ide-floppy: I/O error in "
826 "request sense command\n");
827 return ide_do_reset(drive);
828 }
829 /* Retry operation */
830 idefloppy_retry_pc(drive);
831 /* queued, but not started */
832 return ide_stopped;
833 }
834 pc->error = 0;
835 if (floppy->failed_pc == pc)
836 floppy->failed_pc = NULL;
837 /* Command finished - Call the callback function */
838 pc->callback(drive);
839 return ide_stopped;
840 }
841
842 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
843 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
844 "more interrupts in DMA mode\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100845 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return ide_do_reset(drive);
847 }
848
849 /* Get the number of bytes to transfer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100850 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
851 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* on this interrupt */
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100853 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100855 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
857 return ide_do_reset(drive);
858 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100859 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* Hopefully, we will never get here */
861 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100862 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 printk(KERN_ERR "but the floppy wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100864 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return ide_do_reset(drive);
866 }
867 if (!test_bit(PC_WRITING, &pc->flags)) {
868 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100869 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (temp > pc->request_transfer) {
871 if (temp > pc->buffer_size) {
872 printk(KERN_ERR "ide-floppy: The floppy wants "
873 "to send us more data than expected "
874 "- discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100875 idefloppy_discard_data(drive, bcount);
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700876 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ide_set_handler(drive,
878 &idefloppy_pc_intr,
879 IDEFLOPPY_WAIT_CMD,
880 NULL);
881 return ide_started;
882 }
883 debug_log(KERN_NOTICE "ide-floppy: The floppy wants to "
884 "send us more data than expected - "
885 "allowing transfer\n");
886 }
887 }
888 if (test_bit(PC_WRITING, &pc->flags)) {
889 if (pc->buffer != NULL)
890 /* Write the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100891 hwif->atapi_output_bytes(drive, pc->current_position,
892 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100894 idefloppy_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 } else {
896 if (pc->buffer != NULL)
897 /* Read the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100898 hwif->atapi_input_bytes(drive, pc->current_position,
899 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100901 idefloppy_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100904 pc->actually_transferred += bcount;
905 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700907 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
909 return ide_started;
910}
911
912/*
913 * This is the original routine that did the packet transfer.
914 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
915 * for that drive below. The algorithm is chosen based on drive type
916 */
917static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
918{
919 ide_startstop_t startstop;
920 idefloppy_floppy_t *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100921 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
924 printk(KERN_ERR "ide-floppy: Strange, packet command "
925 "initiated yet DRQ isn't asserted\n");
926 return startstop;
927 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100928 ireason = drive->hwif->INB(IDE_IREASON_REG);
929 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
931 "issuing a packet command\n");
932 return ide_do_reset(drive);
933 }
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700934 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* Set the interrupt routine */
936 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
937 /* Send the actual packet */
938 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
939 return ide_started;
940}
941
942
943/*
944 * What we have here is a classic case of a top half / bottom half
945 * interrupt service routine. In interrupt mode, the device sends
946 * an interrupt to signal it's ready to receive a packet. However,
947 * we need to delay about 2-3 ticks before issuing the packet or we
948 * gets in trouble.
949 *
950 * So, follow carefully. transfer_pc1 is called as an interrupt (or
951 * directly). In either case, when the device says it's ready for a
952 * packet, we schedule the packet transfer to occur about 2-3 ticks
953 * later in transfer_pc2.
954 */
955static int idefloppy_transfer_pc2 (ide_drive_t *drive)
956{
957 idefloppy_floppy_t *floppy = drive->driver_data;
958
959 /* Send the actual packet */
960 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
961 /* Timeout for the packet command */
962 return IDEFLOPPY_WAIT_CMD;
963}
964
965static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
966{
967 idefloppy_floppy_t *floppy = drive->driver_data;
968 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100969 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
972 printk(KERN_ERR "ide-floppy: Strange, packet command "
973 "initiated yet DRQ isn't asserted\n");
974 return startstop;
975 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100976 ireason = drive->hwif->INB(IDE_IREASON_REG);
977 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
979 "while issuing a packet command\n");
980 return ide_do_reset(drive);
981 }
982 /*
983 * The following delay solves a problem with ATAPI Zip 100 drives
984 * where the Busy flag was apparently being deasserted before the
985 * unit was ready to receive data. This was happening on a
986 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
987 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
988 * used until after the packet is moved in about 50 msec.
989 */
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700990 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ide_set_handler(drive,
992 &idefloppy_pc_intr, /* service routine for packet command */
993 floppy->ticks, /* wait this long before "failing" */
994 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
995 return ide_started;
996}
997
998/**
999 * idefloppy_should_report_error()
1000 *
1001 * Supresses error messages resulting from Medium not present
1002 */
1003static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
1004{
1005 if (floppy->sense_key == 0x02 &&
1006 floppy->asc == 0x3a &&
1007 floppy->ascq == 0x00)
1008 return 0;
1009 return 1;
1010}
1011
1012/*
1013 * Issue a packet command
1014 */
1015static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
1016{
1017 idefloppy_floppy_t *floppy = drive->driver_data;
1018 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ide_handler_t *pkt_xfer_routine;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001020 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001021 u8 dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (floppy->failed_pc == NULL &&
1024 pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
1025 floppy->failed_pc = pc;
1026 /* Set the current packet command */
1027 floppy->pc = pc;
1028
1029 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
1030 test_bit(PC_ABORT, &pc->flags)) {
1031 /*
1032 * We will "abort" retrying a packet command in case
1033 * a legitimate error code was received.
1034 */
1035 if (!test_bit(PC_ABORT, &pc->flags)) {
1036 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
1037 if (idefloppy_should_report_error(floppy))
1038 printk(KERN_ERR "ide-floppy: %s: I/O error, "
1039 "pc = %2x, key = %2x, "
1040 "asc = %2x, ascq = %2x\n",
1041 drive->name, pc->c[0],
1042 floppy->sense_key,
1043 floppy->asc, floppy->ascq);
1044 }
1045 /* Giving up */
1046 pc->error = IDEFLOPPY_ERROR_GENERAL;
1047 }
1048 floppy->failed_pc = NULL;
1049 pc->callback(drive);
1050 return ide_stopped;
1051 }
1052
1053 debug_log(KERN_INFO "Retry number - %d\n",pc->retries);
1054
1055 pc->retries++;
1056 /* We haven't transferred any data yet */
1057 pc->actually_transferred = 0;
1058 pc->current_position = pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001059 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001061 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
1062 ide_dma_off(drive);
1063
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001064 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001067 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001069 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1070 IDE_TFLAG_OUT_DEVICE, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +01001072 if (dma) { /* Begin DMA, if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1074 hwif->dma_start(drive);
1075 }
1076
1077 /* Can we transfer the packet when we get the interrupt or wait? */
1078 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
1079 /* wait */
1080 pkt_xfer_routine = &idefloppy_transfer_pc1;
1081 } else {
1082 /* immediate */
1083 pkt_xfer_routine = &idefloppy_transfer_pc;
1084 }
1085
1086 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1087 /* Issue the packet command */
1088 ide_execute_command(drive, WIN_PACKETCMD,
1089 pkt_xfer_routine,
1090 IDEFLOPPY_WAIT_CMD,
1091 NULL);
1092 return ide_started;
1093 } else {
1094 /* Issue the packet command */
1095 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1096 return (*pkt_xfer_routine) (drive);
1097 }
1098}
1099
1100static void idefloppy_rw_callback (ide_drive_t *drive)
1101{
1102 debug_log(KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
1103
1104 idefloppy_do_end_request(drive, 1, 0);
1105 return;
1106}
1107
1108static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1109{
1110 debug_log(KERN_INFO "ide-floppy: creating prevent removal command, "
1111 "prevent = %d\n", prevent);
1112
1113 idefloppy_init_pc(pc);
1114 pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
1115 pc->c[4] = prevent;
1116}
1117
1118static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1119{
1120 idefloppy_init_pc(pc);
1121 pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
1122 pc->c[7] = 255;
1123 pc->c[8] = 255;
1124 pc->request_transfer = 255;
1125}
1126
1127static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1128 int flags)
1129{
1130 idefloppy_init_pc(pc);
1131 pc->c[0] = IDEFLOPPY_FORMAT_UNIT_CMD;
1132 pc->c[1] = 0x17;
1133
1134 memset(pc->buffer, 0, 12);
1135 pc->buffer[1] = 0xA2;
1136 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1137
1138 if (flags & 1) /* Verify bit on... */
1139 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
1140 pc->buffer[3] = 8;
1141
1142 put_unaligned(htonl(b), (unsigned int *)(&pc->buffer[4]));
1143 put_unaligned(htonl(l), (unsigned int *)(&pc->buffer[8]));
1144 pc->buffer_size=12;
1145 set_bit(PC_WRITING, &pc->flags);
1146}
1147
1148/*
1149 * A mode sense command is used to "sense" floppy parameters.
1150 */
1151static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
1152{
1153 u16 length = sizeof(idefloppy_mode_parameter_header_t);
1154
1155 idefloppy_init_pc(pc);
1156 pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
1157 pc->c[1] = 0;
1158 pc->c[2] = page_code + (type << 6);
1159
1160 switch (page_code) {
1161 case IDEFLOPPY_CAPABILITIES_PAGE:
1162 length += 12;
1163 break;
1164 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1165 length += 32;
1166 break;
1167 default:
1168 printk(KERN_ERR "ide-floppy: unsupported page code "
1169 "in create_mode_sense_cmd\n");
1170 }
1171 put_unaligned(htons(length), (u16 *) &pc->c[7]);
1172 pc->request_transfer = length;
1173}
1174
1175static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1176{
1177 idefloppy_init_pc(pc);
1178 pc->c[0] = IDEFLOPPY_START_STOP_CMD;
1179 pc->c[4] = start;
1180}
1181
1182static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1183{
1184 idefloppy_init_pc(pc);
1185 pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
1186}
1187
1188static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1189{
1190 int block = sector / floppy->bs_factor;
1191 int blocks = rq->nr_sectors / floppy->bs_factor;
1192 int cmd = rq_data_dir(rq);
1193
1194 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1195 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1196 block, blocks);
1197
1198 idefloppy_init_pc(pc);
1199 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
1200 pc->c[0] = cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
1201 put_unaligned(htonl(blocks), (unsigned int *) &pc->c[6]);
1202 } else {
1203 pc->c[0] = cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
1204 put_unaligned(htons(blocks), (unsigned short *) &pc->c[7]);
1205 }
1206 put_unaligned(htonl(block), (unsigned int *) &pc->c[2]);
1207 pc->callback = &idefloppy_rw_callback;
1208 pc->rq = rq;
1209 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001210 if (rq->cmd_flags & REQ_RW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 set_bit(PC_WRITING, &pc->flags);
1212 pc->buffer = NULL;
1213 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1214 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1215}
1216
Jens Axboe3d6392c2007-07-09 12:38:05 +02001217static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 idefloppy_init_pc(pc);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001221 pc->callback = &idefloppy_rw_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 memcpy(pc->c, rq->cmd, sizeof(pc->c));
Jens Axboe3d6392c2007-07-09 12:38:05 +02001223 pc->rq = rq;
1224 pc->b_count = rq->data_len;
1225 if (rq->data_len && rq_data_dir(rq) == WRITE)
1226 set_bit(PC_WRITING, &pc->flags);
1227 pc->buffer = rq->data;
1228 if (rq->bio)
1229 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1230
1231 /*
1232 * possibly problematic, doesn't look like ide-floppy correctly
1233 * handled scattered requests if dma fails...
1234 */
1235 pc->request_transfer = pc->buffer_size = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238/*
1239 * idefloppy_do_request is our request handling function.
1240 */
1241static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1242{
1243 idefloppy_floppy_t *floppy = drive->driver_data;
1244 idefloppy_pc_t *pc;
1245 unsigned long block = (unsigned long)block_s;
1246
Jens Axboecdd60262006-07-28 09:32:07 +02001247 debug_log(KERN_INFO "dev: %s, flags: %lx, errors: %d\n",
Randy Dunlap18cddac2006-06-25 05:48:56 -07001248 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 rq->flags, rq->errors);
1250 debug_log(KERN_INFO "sector: %ld, nr_sectors: %ld, "
1251 "current_nr_sectors: %d\n", (long)rq->sector,
1252 rq->nr_sectors, rq->current_nr_sectors);
1253
1254 if (rq->errors >= ERROR_MAX) {
1255 if (floppy->failed_pc != NULL) {
1256 if (idefloppy_should_report_error(floppy))
1257 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1258 " key = %2x, asc = %2x, ascq = %2x\n",
1259 drive->name, floppy->failed_pc->c[0],
1260 floppy->sense_key, floppy->asc, floppy->ascq);
1261 }
1262 else
1263 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1264 drive->name);
1265 idefloppy_do_end_request(drive, 0, 0);
1266 return ide_stopped;
1267 }
Jens Axboe4aff5e22006-08-10 08:44:47 +02001268 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (((long)rq->sector % floppy->bs_factor) ||
1270 (rq->nr_sectors % floppy->bs_factor)) {
1271 printk("%s: unsupported r/w request size\n",
1272 drive->name);
1273 idefloppy_do_end_request(drive, 0, 0);
1274 return ide_stopped;
1275 }
1276 pc = idefloppy_next_pc_storage(drive);
1277 idefloppy_create_rw_cmd(floppy, pc, rq, block);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001278 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 pc = (idefloppy_pc_t *) rq->buffer;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001280 } else if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 pc = idefloppy_next_pc_storage(drive);
Jens Axboe3d6392c2007-07-09 12:38:05 +02001282 idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 } else {
1284 blk_dump_rq_flags(rq,
1285 "ide-floppy: unsupported command in queue");
1286 idefloppy_do_end_request(drive, 0, 0);
1287 return ide_stopped;
1288 }
1289
1290 pc->rq = rq;
1291 return idefloppy_issue_pc(drive, pc);
1292}
1293
1294/*
1295 * idefloppy_queue_pc_tail adds a special packet command request to the
1296 * tail of the request queue, and waits for it to be serviced.
1297 */
1298static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1299{
1300 struct ide_floppy_obj *floppy = drive->driver_data;
1301 struct request rq;
1302
1303 ide_init_drive_cmd (&rq);
1304 rq.buffer = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001305 rq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 rq.rq_disk = floppy->disk;
1307
1308 return ide_do_drive_cmd(drive, &rq, ide_wait);
1309}
1310
1311/*
1312 * Look at the flexible disk page parameters. We will ignore the CHS
1313 * capacity parameters and use the LBA parameters instead.
1314 */
1315static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1316{
1317 idefloppy_floppy_t *floppy = drive->driver_data;
1318 idefloppy_pc_t pc;
1319 idefloppy_mode_parameter_header_t *header;
1320 idefloppy_flexible_disk_page_t *page;
1321 int capacity, lba_capacity;
1322
1323 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1324 if (idefloppy_queue_pc_tail(drive,&pc)) {
1325 printk(KERN_ERR "ide-floppy: Can't get flexible disk "
1326 "page parameters\n");
1327 return 1;
1328 }
1329 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1330 floppy->wp = header->wp;
1331 set_disk_ro(floppy->disk, floppy->wp);
1332 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1333
1334 page->transfer_rate = ntohs(page->transfer_rate);
1335 page->sector_size = ntohs(page->sector_size);
1336 page->cyls = ntohs(page->cyls);
1337 page->rpm = ntohs(page->rpm);
1338 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1339 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1340 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1341 "%d sector size, %d rpm\n",
1342 drive->name, capacity / 1024, page->cyls,
1343 page->heads, page->sectors,
1344 page->transfer_rate / 8, page->sector_size, page->rpm);
1345
1346 floppy->flexible_disk_page = *page;
1347 drive->bios_cyl = page->cyls;
1348 drive->bios_head = page->heads;
1349 drive->bios_sect = page->sectors;
1350 lba_capacity = floppy->blocks * floppy->block_size;
1351 if (capacity < lba_capacity) {
1352 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1353 "bytes, but the drive only handles %d\n",
1354 drive->name, lba_capacity, capacity);
1355 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1356 }
1357 return 0;
1358}
1359
1360static int idefloppy_get_capability_page(ide_drive_t *drive)
1361{
1362 idefloppy_floppy_t *floppy = drive->driver_data;
1363 idefloppy_pc_t pc;
1364 idefloppy_mode_parameter_header_t *header;
1365 idefloppy_capabilities_page_t *page;
1366
1367 floppy->srfp = 0;
1368 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1369 MODE_SENSE_CURRENT);
1370
1371 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1372 if (idefloppy_queue_pc_tail(drive,&pc)) {
1373 return 1;
1374 }
1375
1376 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1377 page= (idefloppy_capabilities_page_t *)(header+1);
1378 floppy->srfp = page->srfp;
1379 return (0);
1380}
1381
1382/*
1383 * Determine if a media is present in the floppy drive, and if so,
1384 * its LBA capacity.
1385 */
1386static int idefloppy_get_capacity (ide_drive_t *drive)
1387{
1388 idefloppy_floppy_t *floppy = drive->driver_data;
1389 idefloppy_pc_t pc;
1390 idefloppy_capacity_header_t *header;
1391 idefloppy_capacity_descriptor_t *descriptor;
1392 int i, descriptors, rc = 1, blocks, length;
1393
1394 drive->bios_cyl = 0;
1395 drive->bios_head = drive->bios_sect = 0;
Alan Coxfdb77da2007-02-17 02:40:20 +01001396 floppy->blocks = 0;
1397 floppy->bs_factor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 set_capacity(floppy->disk, 0);
1399
1400 idefloppy_create_read_capacity_cmd(&pc);
1401 if (idefloppy_queue_pc_tail(drive, &pc)) {
1402 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1403 return 1;
1404 }
1405 header = (idefloppy_capacity_header_t *) pc.buffer;
1406 descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1407 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1408
1409 for (i = 0; i < descriptors; i++, descriptor++) {
1410 blocks = descriptor->blocks = ntohl(descriptor->blocks);
1411 length = descriptor->length = ntohs(descriptor->length);
1412
1413 if (!i)
1414 {
1415 switch (descriptor->dc) {
1416 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1417 case CAPACITY_UNFORMATTED:
1418 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1419 /*
1420 * If it is not a clik drive, break out
1421 * (maintains previous driver behaviour)
1422 */
1423 break;
1424 case CAPACITY_CURRENT:
1425 /* Normal Zip/LS-120 disks */
1426 if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1427 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1428 "sector size\n", drive->name,
1429 blocks * length / 1024, blocks, length);
1430 floppy->capacity = *descriptor;
1431 if (!length || length % 512) {
1432 printk(KERN_NOTICE "%s: %d bytes block size "
1433 "not supported\n", drive->name, length);
1434 } else {
1435 floppy->blocks = blocks;
1436 floppy->block_size = length;
1437 if ((floppy->bs_factor = length / 512) != 1)
1438 printk(KERN_NOTICE "%s: warning: non "
1439 "512 bytes block size not "
1440 "fully supported\n",
1441 drive->name);
1442 rc = 0;
1443 }
1444 break;
1445 case CAPACITY_NO_CARTRIDGE:
1446 /*
1447 * This is a KERN_ERR so it appears on screen
1448 * for the user to see
1449 */
1450 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1451 break;
1452 case CAPACITY_INVALID:
1453 printk(KERN_ERR "%s: Invalid capacity for disk "
1454 "in drive\n", drive->name);
1455 break;
1456 }
1457 }
1458 if (!i) {
1459 debug_log( "Descriptor 0 Code: %d\n",
1460 descriptor->dc);
1461 }
1462 debug_log( "Descriptor %d: %dkB, %d blocks, %d "
1463 "sector size\n", i, blocks * length / 1024, blocks,
1464 length);
1465 }
1466
1467 /* Clik! disk does not support get_flexible_disk_page */
1468 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1469 (void) idefloppy_get_flexible_disk_page(drive);
1470 }
1471
1472 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1473 return rc;
1474}
1475
1476/*
1477** Obtain the list of formattable capacities.
1478** Very similar to idefloppy_get_capacity, except that we push the capacity
1479** descriptors to userland, instead of our own structures.
1480**
1481** Userland gives us the following structure:
1482**
1483** struct idefloppy_format_capacities {
1484** int nformats;
1485** struct {
1486** int nblocks;
1487** int blocksize;
1488** } formats[];
1489** } ;
1490**
1491** userland initializes nformats to the number of allocated formats[]
1492** records. On exit we set nformats to the number of records we've
1493** actually initialized.
1494**
1495*/
1496
1497static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1498{
1499 idefloppy_pc_t pc;
1500 idefloppy_capacity_header_t *header;
1501 idefloppy_capacity_descriptor_t *descriptor;
1502 int i, descriptors, blocks, length;
1503 int u_array_size;
1504 int u_index;
1505 int __user *argp;
1506
1507 if (get_user(u_array_size, arg))
1508 return (-EFAULT);
1509
1510 if (u_array_size <= 0)
1511 return (-EINVAL);
1512
1513 idefloppy_create_read_capacity_cmd(&pc);
1514 if (idefloppy_queue_pc_tail(drive, &pc)) {
1515 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1516 return (-EIO);
1517 }
1518 header = (idefloppy_capacity_header_t *) pc.buffer;
1519 descriptors = header->length /
1520 sizeof(idefloppy_capacity_descriptor_t);
1521 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1522
1523 u_index = 0;
1524 argp = arg + 1;
1525
1526 /*
1527 ** We always skip the first capacity descriptor. That's the
1528 ** current capacity. We are interested in the remaining descriptors,
1529 ** the formattable capacities.
1530 */
1531
1532 for (i=0; i<descriptors; i++, descriptor++) {
1533 if (u_index >= u_array_size)
1534 break; /* User-supplied buffer too small */
1535 if (i == 0)
1536 continue; /* Skip the first descriptor */
1537
1538 blocks = ntohl(descriptor->blocks);
1539 length = ntohs(descriptor->length);
1540
1541 if (put_user(blocks, argp))
1542 return(-EFAULT);
1543 ++argp;
1544
1545 if (put_user(length, argp))
1546 return (-EFAULT);
1547 ++argp;
1548
1549 ++u_index;
1550 }
1551
1552 if (put_user(u_index, arg))
1553 return (-EFAULT);
1554 return (0);
1555}
1556
1557/*
1558** Send ATAPI_FORMAT_UNIT to the drive.
1559**
1560** Userland gives us the following structure:
1561**
1562** struct idefloppy_format_command {
1563** int nblocks;
1564** int blocksize;
1565** int flags;
1566** } ;
1567**
1568** flags is a bitmask, currently, the only defined flag is:
1569**
1570** 0x01 - verify media after format.
1571*/
1572
1573static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1574{
1575 int blocks;
1576 int length;
1577 int flags;
1578 idefloppy_pc_t pc;
1579
1580 if (get_user(blocks, arg) ||
1581 get_user(length, arg+1) ||
1582 get_user(flags, arg+2)) {
1583 return (-EFAULT);
1584 }
1585
1586 /* Get the SFRP bit */
1587 (void) idefloppy_get_capability_page(drive);
1588 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1589 if (idefloppy_queue_pc_tail(drive, &pc)) {
1590 return (-EIO);
1591 }
1592
1593 return (0);
1594}
1595
1596/*
1597** Get ATAPI_FORMAT_UNIT progress indication.
1598**
Matt LaPlante0779bf22006-11-30 05:24:39 +01001599** Userland gives a pointer to an int. The int is set to a progress
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600** indicator 0-65536, with 65536=100%.
1601**
1602** If the drive does not support format progress indication, we just check
1603** the dsc bit, and return either 0 or 65536.
1604*/
1605
1606static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1607{
1608 idefloppy_floppy_t *floppy = drive->driver_data;
1609 idefloppy_pc_t pc;
1610 int progress_indication = 0x10000;
1611
1612 if (floppy->srfp) {
1613 idefloppy_create_request_sense_cmd(&pc);
1614 if (idefloppy_queue_pc_tail(drive, &pc)) {
1615 return (-EIO);
1616 }
1617
1618 if (floppy->sense_key == 2 &&
1619 floppy->asc == 4 &&
1620 floppy->ascq == 4) {
1621 progress_indication = floppy->progress_indication;
1622 }
1623 /* Else assume format_unit has finished, and we're
1624 ** at 0x10000 */
1625 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 unsigned long flags;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001627 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 local_irq_save(flags);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001630 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 local_irq_restore(flags);
1632
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001633 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635 if (put_user(progress_indication, arg))
1636 return (-EFAULT);
1637
1638 return (0);
1639}
1640
1641/*
1642 * Return the current floppy capacity.
1643 */
1644static sector_t idefloppy_capacity (ide_drive_t *drive)
1645{
1646 idefloppy_floppy_t *floppy = drive->driver_data;
1647 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1648
1649 return capacity;
1650}
1651
1652/*
1653 * idefloppy_identify_device checks if we can support a drive,
1654 * based on the ATAPI IDENTIFY command results.
1655 */
1656static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1657{
1658 struct idefloppy_id_gcw gcw;
1659#if IDEFLOPPY_DEBUG_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 char buffer[80];
1661#endif /* IDEFLOPPY_DEBUG_INFO */
1662
1663 *((u16 *) &gcw) = id->config;
1664
1665#ifdef CONFIG_PPC
1666 /* kludge for Apple PowerBook internal zip */
1667 if ((gcw.device_type == 5) &&
1668 !strstr(id->model, "CD-ROM") &&
1669 strstr(id->model, "ZIP"))
1670 gcw.device_type = 0;
1671#endif
1672
1673#if IDEFLOPPY_DEBUG_INFO
1674 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1675 switch (gcw.protocol) {
1676 case 0: case 1: sprintf(buffer, "ATA");break;
1677 case 2: sprintf(buffer, "ATAPI");break;
1678 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1679 }
1680 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1681 switch (gcw.device_type) {
1682 case 0: sprintf(buffer, "Direct-access Device");break;
1683 case 1: sprintf(buffer, "Streaming Tape Device");break;
1684 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1685 case 5: sprintf(buffer, "CD-ROM Device");break;
1686 case 6: sprintf(buffer, "Reserved");
1687 case 7: sprintf(buffer, "Optical memory Device");break;
1688 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1689 default: sprintf(buffer, "Reserved");
1690 }
1691 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1692 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1693 switch (gcw.drq_type) {
1694 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1695 case 1: sprintf(buffer, "Interrupt DRQ");break;
1696 case 2: sprintf(buffer, "Accelerated DRQ");break;
1697 case 3: sprintf(buffer, "Reserved");break;
1698 }
1699 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1700 switch (gcw.packet_size) {
1701 case 0: sprintf(buffer, "12 bytes");break;
1702 case 1: sprintf(buffer, "16 bytes");break;
1703 default: sprintf(buffer, "Reserved");break;
1704 }
1705 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706#endif /* IDEFLOPPY_DEBUG_INFO */
1707
1708 if (gcw.protocol != 2)
1709 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1710 else if (gcw.device_type != 0)
1711 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1712 else if (!gcw.removable)
1713 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1714 else if (gcw.drq_type == 3) {
1715 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1716 } else if (gcw.packet_size != 0) {
1717 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1718 } else
1719 return 1;
1720 return 0;
1721}
1722
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001723#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724static void idefloppy_add_settings(ide_drive_t *drive)
1725{
1726 idefloppy_floppy_t *floppy = drive->driver_data;
1727
1728/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001729 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001731 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1732 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1733 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1734 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001736#else
1737static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1738#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740/*
1741 * Driver initialization.
1742 */
1743static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1744{
1745 struct idefloppy_id_gcw gcw;
1746
1747 *((u16 *) &gcw) = drive->id->config;
1748 floppy->pc = floppy->pc_stack;
1749 if (gcw.drq_type == 1)
1750 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1751 /*
1752 * We used to check revisions here. At this point however
1753 * I'm giving up. Just assume they are all broken, its easier.
1754 *
1755 * The actual reason for the workarounds was likely
1756 * a driver bug after all rather than a firmware bug,
1757 * and the workaround below used to hide it. It should
1758 * be fixed as of version 1.9, but to be on the safe side
1759 * we'll leave the limitation below for the 2.2.x tree.
1760 */
1761
1762 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1763 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1764 /* This value will be visible in the /proc/ide/hdx/settings */
1765 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1766 blk_queue_max_sectors(drive->queue, 64);
1767 }
1768
1769 /*
1770 * Guess what? The IOMEGA Clik! drive also needs the
1771 * above fix. It makes nasty clicking noises without
1772 * it, so please don't remove this.
1773 */
1774 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1775 blk_queue_max_sectors(drive->queue, 64);
1776 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1777 }
1778
1779
1780 (void) idefloppy_get_capacity(drive);
1781 idefloppy_add_settings(drive);
1782}
1783
Russell King4031bbe2006-01-06 11:41:00 +00001784static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 idefloppy_floppy_t *floppy = drive->driver_data;
1787 struct gendisk *g = floppy->disk;
1788
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001789 ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 del_gendisk(g);
1792
1793 ide_floppy_put(floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
1796static void ide_floppy_release(struct kref *kref)
1797{
1798 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1799 ide_drive_t *drive = floppy->drive;
1800 struct gendisk *g = floppy->disk;
1801
1802 drive->driver_data = NULL;
1803 g->private_data = NULL;
1804 put_disk(g);
1805 kfree(floppy);
1806}
1807
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001808#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809static int proc_idefloppy_read_capacity
1810 (char *page, char **start, off_t off, int count, int *eof, void *data)
1811{
1812 ide_drive_t*drive = (ide_drive_t *)data;
1813 int len;
1814
1815 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1816 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1817}
1818
1819static ide_proc_entry_t idefloppy_proc[] = {
1820 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1821 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1822 { NULL, 0, NULL, NULL }
1823};
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02001824#endif /* CONFIG_IDE_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Russell King4031bbe2006-01-06 11:41:00 +00001826static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828static ide_driver_t idefloppy_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001829 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001830 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001831 .name = "ide-floppy",
1832 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001833 },
Russell King4031bbe2006-01-06 11:41:00 +00001834 .probe = ide_floppy_probe,
1835 .remove = ide_floppy_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 .version = IDEFLOPPY_VERSION,
1837 .media = ide_floppy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 .do_request = idefloppy_do_request,
1840 .end_request = idefloppy_do_end_request,
1841 .error = __ide_error,
1842 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001843#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 .proc = idefloppy_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846};
1847
1848static int idefloppy_open(struct inode *inode, struct file *filp)
1849{
1850 struct gendisk *disk = inode->i_bdev->bd_disk;
1851 struct ide_floppy_obj *floppy;
1852 ide_drive_t *drive;
1853 idefloppy_pc_t pc;
1854 int ret = 0;
1855
1856 debug_log(KERN_INFO "Reached idefloppy_open\n");
1857
1858 if (!(floppy = ide_floppy_get(disk)))
1859 return -ENXIO;
1860
1861 drive = floppy->drive;
1862
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001863 floppy->openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001865 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1867 /* Just in case */
1868
1869 idefloppy_create_test_unit_ready_cmd(&pc);
1870 if (idefloppy_queue_pc_tail(drive, &pc)) {
1871 idefloppy_create_start_stop_cmd(&pc, 1);
1872 (void) idefloppy_queue_pc_tail(drive, &pc);
1873 }
1874
1875 if (idefloppy_get_capacity (drive)
1876 && (filp->f_flags & O_NDELAY) == 0
1877 /*
1878 ** Allow O_NDELAY to open a drive without a disk, or with
1879 ** an unreadable disk, so that we can get the format
1880 ** capacity of the drive or begin the format - Sam
1881 */
1882 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 ret = -EIO;
1884 goto out_put_floppy;
1885 }
1886
1887 if (floppy->wp && (filp->f_mode & 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 ret = -EROFS;
1889 goto out_put_floppy;
1890 }
1891 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1892 /* IOMEGA Clik! drives do not support lock/unlock commands */
1893 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1894 idefloppy_create_prevent_cmd(&pc, 1);
1895 (void) idefloppy_queue_pc_tail(drive, &pc);
1896 }
1897 check_disk_change(inode->i_bdev);
1898 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 ret = -EBUSY;
1900 goto out_put_floppy;
1901 }
1902 return 0;
1903
1904out_put_floppy:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001905 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 ide_floppy_put(floppy);
1907 return ret;
1908}
1909
1910static int idefloppy_release(struct inode *inode, struct file *filp)
1911{
1912 struct gendisk *disk = inode->i_bdev->bd_disk;
1913 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1914 ide_drive_t *drive = floppy->drive;
1915 idefloppy_pc_t pc;
1916
1917 debug_log(KERN_INFO "Reached idefloppy_release\n");
1918
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001919 if (floppy->openers == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /* IOMEGA Clik! drives do not support lock/unlock commands */
1921 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1922 idefloppy_create_prevent_cmd(&pc, 0);
1923 (void) idefloppy_queue_pc_tail(drive, &pc);
1924 }
1925
1926 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1927 }
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001928
1929 floppy->openers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 ide_floppy_put(floppy);
1932
1933 return 0;
1934}
1935
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001936static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1937{
1938 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1939 ide_drive_t *drive = floppy->drive;
1940
1941 geo->heads = drive->bios_head;
1942 geo->sectors = drive->bios_sect;
1943 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1944 return 0;
1945}
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947static int idefloppy_ioctl(struct inode *inode, struct file *file,
1948 unsigned int cmd, unsigned long arg)
1949{
1950 struct block_device *bdev = inode->i_bdev;
1951 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1952 ide_drive_t *drive = floppy->drive;
1953 void __user *argp = (void __user *)arg;
Ondrej Zary07203f62005-11-10 00:25:15 +01001954 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 int prevent = (arg) ? 1 : 0;
1956 idefloppy_pc_t pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 switch (cmd) {
1959 case CDROMEJECT:
1960 prevent = 0;
1961 /* fall through */
1962 case CDROM_LOCKDOOR:
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001963 if (floppy->openers > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return -EBUSY;
1965
1966 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1967 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1968 idefloppy_create_prevent_cmd(&pc, prevent);
1969 (void) idefloppy_queue_pc_tail(drive, &pc);
1970 }
1971 if (cmd == CDROMEJECT) {
1972 idefloppy_create_start_stop_cmd(&pc, 2);
1973 (void) idefloppy_queue_pc_tail(drive, &pc);
1974 }
1975 return 0;
1976 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1977 return 0;
1978 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1979 return idefloppy_get_format_capacities(drive, argp);
1980 case IDEFLOPPY_IOCTL_FORMAT_START:
1981
1982 if (!(file->f_mode & 2))
1983 return -EPERM;
1984
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01001985 if (floppy->openers > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /* Don't format if someone is using the disk */
1987
1988 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1989 &floppy->flags);
1990 return -EBUSY;
1991 }
1992
1993 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1994
1995 err = idefloppy_begin_format(drive, argp);
1996 if (err)
1997 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1998 return err;
1999 /*
2000 ** Note, the bit will be cleared when the device is
2001 ** closed. This is the cleanest way to handle the
2002 ** situation where the drive does not support
2003 ** format progress reporting.
2004 */
2005 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
2006 return idefloppy_get_format_progress(drive, argp);
2007 }
Bartlomiej Zolnierkiewicz89636af2007-07-20 01:11:59 +02002008
2009 /*
2010 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
2011 * and CDROM_SEND_PACKET (legacy) ioctls
2012 */
2013 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
2014 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
2015 bdev->bd_disk, cmd, argp);
2016 else
2017 err = -ENOTTY;
2018
2019 if (err == -ENOTTY)
2020 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
2021
2022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
2025static int idefloppy_media_changed(struct gendisk *disk)
2026{
2027 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
2028 ide_drive_t *drive = floppy->drive;
2029
2030 /* do not scan partitions twice if this is a removable device */
2031 if (drive->attach) {
2032 drive->attach = 0;
2033 return 0;
2034 }
2035 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
2036}
2037
2038static int idefloppy_revalidate_disk(struct gendisk *disk)
2039{
2040 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
2041 set_capacity(disk, idefloppy_capacity(floppy->drive));
2042 return 0;
2043}
2044
2045static struct block_device_operations idefloppy_ops = {
2046 .owner = THIS_MODULE,
2047 .open = idefloppy_open,
2048 .release = idefloppy_release,
2049 .ioctl = idefloppy_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002050 .getgeo = idefloppy_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 .media_changed = idefloppy_media_changed,
2052 .revalidate_disk= idefloppy_revalidate_disk
2053};
2054
Russell King4031bbe2006-01-06 11:41:00 +00002055static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 idefloppy_floppy_t *floppy;
2058 struct gendisk *g;
2059
2060 if (!strstr("ide-floppy", drive->driver_req))
2061 goto failed;
2062 if (!drive->present)
2063 goto failed;
2064 if (drive->media != ide_floppy)
2065 goto failed;
2066 if (!idefloppy_identify_device (drive, drive->id)) {
2067 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
2068 goto failed;
2069 }
2070 if (drive->scsi) {
2071 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
2072 goto failed;
2073 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002074 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
2076 goto failed;
2077 }
2078
2079 g = alloc_disk(1 << PARTN_BITS);
2080 if (!g)
2081 goto out_free_floppy;
2082
2083 ide_init_disk(g, drive);
2084
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002085 ide_proc_register_driver(drive, &idefloppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 kref_init(&floppy->kref);
2088
2089 floppy->drive = drive;
2090 floppy->driver = &idefloppy_driver;
2091 floppy->disk = g;
2092
2093 g->private_data = &floppy->driver;
2094
2095 drive->driver_data = floppy;
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 idefloppy_setup (drive, floppy);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 g->minors = 1 << PARTN_BITS;
2100 g->driverfs_dev = &drive->gendev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
2102 g->fops = &idefloppy_ops;
2103 drive->attach = 1;
2104 add_disk(g);
2105 return 0;
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107out_free_floppy:
2108 kfree(floppy);
2109failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002110 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2114
2115static void __exit idefloppy_exit (void)
2116{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002117 driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002120static int __init idefloppy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002123 return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Kay Sievers263756e2005-12-12 18:03:44 +01002126MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127module_init(idefloppy_init);
2128module_exit(idefloppy_exit);
2129MODULE_LICENSE("GPL");