OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [block/] [ide-floppy.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * linux/drivers/block/ide-floppy.c     Version 0.71 - ALPHA    May  21, 1998
3
 *
4
 * Copyright (C) 1996, 1997 Gadi Oxman <gadio@netvision.net.il>
5
 */
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
 * Many thanks to Lode Leroy <Lode.Leroy@www.ibase.be>, who tested so many
14
 * ALPHA patches to this driver on an EASYSTOR LS-120 ATAPI floppy drive.
15
 *
16
 * Ver 0.1   Oct 17 96   Initial test version, mostly based on ide-tape.c.
17
 * Ver 0.2   Oct 31 96   Minor changes.
18
 * Ver 0.3   Dec  2 96   Fixed error recovery bug.
19
 * Ver 0.4   Jan 26 97   Add support for the HDIO_GETGEO ioctl.
20
 * Ver 0.5   Feb 21 97   Add partitions support.
21
 *                       Use the minimum of the LBA and CHS capacities.
22
 *                       Avoid hwgroup->rq == NULL on the last irq.
23
 *                       Fix potential null dereferencing with DEBUG_LOG.
24
 * Ver 0.6   Jul  3 97   Limit max sectors per read/write command to 64.
25
 * Ver 0.7   Aug  4 97   Increase irq timeout from 10 to 100 seconds.
26
 * Ver 0.71  May 21 98   Disallow opening a write protected media for writing
27
 *                       Limit max sectors only on IOMEGA ZIP 21.D
28
 *                       Issue START cmd only if TEST_UNIT_READY fails
29
 *                       Add CDROMEJECT ioctl
30
 *                       Clean up error messages a bit
31
 * Ver 0.72  Jul  8 98   Limit max sectors only on IOMEGA ZIP 23.D
32
 */
33
 
34
#include <linux/config.h>
35
#include <linux/module.h>
36
#include <linux/types.h>
37
#include <linux/string.h>
38
#include <linux/kernel.h>
39
#include <linux/delay.h>
40
#include <linux/timer.h>
41
#include <linux/mm.h>
42
#include <linux/ioport.h>
43
#include <linux/interrupt.h>
44
#include <linux/major.h>
45
#include <linux/blkdev.h>
46
#include <linux/errno.h>
47
#include <linux/hdreg.h>
48
#include <linux/genhd.h>
49
#include <linux/malloc.h>
50
#include <linux/cdrom.h>
51
 
52
#include <asm/byteorder.h>
53
#include <asm/irq.h>
54
#include <asm/segment.h>
55
#include <asm/io.h>
56
#include <asm/unaligned.h>
57
#include <asm/bitops.h>
58
 
59
/*
60
 *      Main Linux ide driver include file
61
 */
62
#include "ide.h"
63
 
64
/*
65
 *      The following are used to debug the driver.
66
 */
67
#define IDEFLOPPY_DEBUG_LOG             0
68
#define IDEFLOPPY_DEBUG_INFO            0
69
#define IDEFLOPPY_DEBUG_BUGS            1
70
 
71
/*
72
 *      After each failed packet command we issue a request sense command
73
 *      and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
74
 */
75
#define IDEFLOPPY_MAX_PC_RETRIES        3
76
 
77
/*
78
 *      With each packet command, we allocate a buffer of
79
 *      IDEFLOPPY_PC_BUFFER_SIZE bytes.
80
 */
81
#define IDEFLOPPY_PC_BUFFER_SIZE        256
82
 
83
/*
84
 *      In various places in the driver, we need to allocate storage
85
 *      for packet commands and requests, which will remain valid while
86
 *      we leave the driver to wait for an interrupt or a timeout event.
87
 */
88
#define IDEFLOPPY_PC_STACK              (10 + IDEFLOPPY_MAX_PC_RETRIES)
89
 
90
#define IDEFLOPPY_MAX_SECTORS           256
91
 
92
/*
93
 *      Some drives require a longer irq timeout.
94
 */
95
#define IDEFLOPPY_WAIT_CMD              (10 * WAIT_CMD)
96
 
97
/*
98
 *      Our view of a packet command.
99
 */
100
typedef struct idefloppy_packet_command_s {
101
        u8 c[12];                               /* Actual packet bytes */
102
        int retries;                            /* On each retry, we increment retries */
103
        int error;                              /* Error code */
104
        int request_transfer;                   /* Bytes to transfer */
105
        int actually_transferred;               /* Bytes actually transferred */
106
        int buffer_size;                        /* Size of our data buffer */
107
        char *b_data;                           /* Pointer which runs on the buffers */
108
        int b_count;                            /* Missing/Available data on the current buffer */
109
        struct request *rq;                     /* The corresponding request */
110
        byte *buffer;                           /* Data buffer */
111
        byte *current_position;                 /* Pointer into the above buffer */
112
        void (*callback) (ide_drive_t *);       /* Called when this packet command is completed */
113
        byte pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE];       /* Temporary buffer */
114
        unsigned int flags;                     /* Status/Action bit flags */
115
} idefloppy_pc_t;
116
 
117
/*
118
 *      Packet command flag bits.
119
 */
120
#define PC_ABORT                        0        /* Set when an error is considered normal - We won't retry */
121
#define PC_DMA_RECOMMENDED              2       /* 1 when we prefer to use DMA if possible */
122
#define PC_DMA_IN_PROGRESS              3       /* 1 while DMA in progress */
123
#define PC_DMA_ERROR                    4       /* 1 when encountered problem during DMA */
124
#define PC_WRITING                      5       /* Data direction */
125
 
126
/*
127
 *      Removable Block Access Capabilities Page
128
 */
129
typedef struct {
130
        unsigned        page_code       :6;     /* Page code - Should be 0x1b */
131
        unsigned        reserved1_6     :1;     /* Reserved */
132
        unsigned        ps              :1;     /* Should be 0 */
133
        u8              page_length;            /* Page Length - Should be 0xa */
134
        unsigned        reserved2       :6;
135
        unsigned        srfp            :1;     /* Supports reporting progress of format */
136
        unsigned        sflp            :1;     /* System floppy type device */
137
        unsigned        tlun            :3;     /* Total logical units supported by the device */
138
        unsigned        reserved3       :3;
139
        unsigned        sml             :1;     /* Single / Multiple lun supported */
140
        unsigned        ncd             :1;     /* Non cd optical device */
141
        u8              reserved[8];
142
} idefloppy_capabilities_page_t;
143
 
144
/*
145
 *      Flexible disk page.
146
 */
147
typedef struct {
148
        unsigned        page_code       :6;     /* Page code - Should be 0x5 */
149
        unsigned        reserved1_6     :1;     /* Reserved */
150
        unsigned        ps              :1;     /* The device is capable of saving the page */
151
        u8              page_length;            /* Page Length - Should be 0x1e */
152
        u16             transfer_rate;          /* In kilobits per second */
153
        u8              heads, sectors;         /* Number of heads, Number of sectors per track */
154
        u16             sector_size;            /* Byes per sector */
155
        u16             cyls;                   /* Number of cylinders */
156
        u8              reserved10[10];
157
        u8              motor_delay;            /* Motor off delay */
158
        u8              reserved21[7];
159
        u16             rpm;                    /* Rotations per minute */
160
        u8              reserved30[2];
161
} idefloppy_flexible_disk_page_t;
162
 
163
/*
164
 *      Format capacity
165
 */
166
typedef struct {
167
        u8              reserved[3];
168
        u8              length;                 /* Length of the following descriptors in bytes */
169
} idefloppy_capacity_header_t;
170
 
171
typedef struct {
172
        u32             blocks;                 /* Number of blocks */
173
        unsigned        dc              :2;     /* Descriptor Code */
174
        unsigned        reserved        :6;
175
        u8              length_msb;             /* Block Length (MSB)*/
176
        u16             length;                 /* Block Length */
177
} idefloppy_capacity_descriptor_t;
178
 
179
#define CAPACITY_INVALID        0x00
180
#define CAPACITY_UNFORMATTED    0x01
181
#define CAPACITY_CURRENT        0x02
182
#define CAPACITY_NO_CARTRIDGE   0x03
183
 
184
/*
185
 *      Most of our global data which we need to save even as we leave the
186
 *      driver due to an interrupt or a timer event is stored in a variable
187
 *      of type idefloppy_floppy_t, defined below.
188
 */
189
typedef struct {
190
        ide_drive_t *drive;
191
 
192
        idefloppy_pc_t *pc;                     /* Current packet command */
193
        idefloppy_pc_t *failed_pc;              /* Last failed packet command */
194
        idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];/* Packet command stack */
195
        int pc_stack_index;                     /* Next free packet command storage space */
196
        struct request rq_stack[IDEFLOPPY_PC_STACK];
197
        int rq_stack_index;                     /* We implement a circular array */
198
 
199
        /*
200
         *      Last error information
201
         */
202
        byte sense_key, asc, ascq;
203
 
204
        /*
205
         *      Device information
206
         */
207
        int blocks, block_size, bs_factor;                      /* Current format */
208
        idefloppy_capacity_descriptor_t capacity;               /* Last format capacity */
209
        idefloppy_flexible_disk_page_t flexible_disk_page;      /* Copy of the flexible disk page */
210
        int wp;
211
        int max_sectors;
212
 
213
        unsigned int flags;                     /* Status/Action flags */
214
} idefloppy_floppy_t;
215
 
216
/*
217
 *      Floppy flag bits values.
218
 */
219
#define IDEFLOPPY_DRQ_INTERRUPT         0        /* DRQ interrupt device */
220
#define IDEFLOPPY_MEDIA_CHANGED         1       /* Media may have changed */
221
#define IDEFLOPPY_USE_READ12            2       /* Use READ12/WRITE12 or READ10/WRITE10 */
222
 
223
/*
224
 *      ATAPI floppy drive packet commands
225
 */
226
#define IDEFLOPPY_FORMAT_UNIT_CMD       0x04
227
#define IDEFLOPPY_INQUIRY_CMD           0x12
228
#define IDEFLOPPY_MODE_SELECT_CMD       0x55
229
#define IDEFLOPPY_MODE_SENSE_CMD        0x5a
230
#define IDEFLOPPY_READ10_CMD            0x28
231
#define IDEFLOPPY_READ12_CMD            0xa8
232
#define IDEFLOPPY_READ_CAPACITY_CMD     0x23
233
#define IDEFLOPPY_REQUEST_SENSE_CMD     0x03
234
#define IDEFLOPPY_PREVENT_REMOVAL_CMD   0x1e
235
#define IDEFLOPPY_SEEK_CMD              0x2b
236
#define IDEFLOPPY_START_STOP_CMD        0x1b
237
#define IDEFLOPPY_TEST_UNIT_READY_CMD   0x00
238
#define IDEFLOPPY_VERIFY_CMD            0x2f
239
#define IDEFLOPPY_WRITE10_CMD           0x2a
240
#define IDEFLOPPY_WRITE12_CMD           0xaa
241
#define IDEFLOPPY_WRITE_VERIFY_CMD      0x2e
242
 
243
/*
244
 *      Defines for the mode sense command
245
 */
246
#define MODE_SENSE_CURRENT              0x00
247
#define MODE_SENSE_CHANGEABLE           0x01
248
#define MODE_SENSE_DEFAULT              0x02 
249
#define MODE_SENSE_SAVED                0x03
250
 
251
/*
252
 *      Special requests for our block device strategy routine.
253
 */
254
#define IDEFLOPPY_FIRST_RQ              90
255
 
256
/*
257
 *      IDEFLOPPY_PC_RQ is used to queue a packet command in the request queue.
258
 */
259
#define IDEFLOPPY_PC_RQ                 90
260
 
261
#define IDEFLOPPY_LAST_RQ               90
262
 
263
/*
264
 *      A macro which can be used to check if a given request command
265
 *      originated in the driver or in the buffer cache layer.
266
 */
267
#define IDEFLOPPY_RQ_CMD(cmd)           ((cmd >= IDEFLOPPY_FIRST_RQ) && (cmd <= IDEFLOPPY_LAST_RQ))
268
 
269
/*
270
 *      Error codes which are returned in rq->errors to the higher part
271
 *      of the driver.
272
 */
273
#define IDEFLOPPY_ERROR_GENERAL         101
274
 
275
/*
276
 *      The ATAPI Status Register.
277
 */
278
typedef union {
279
        unsigned all                    :8;
280
        struct {
281
                unsigned check          :1;     /* Error occurred */
282
                unsigned idx            :1;     /* Reserved */
283
                unsigned corr           :1;     /* Correctable error occurred */
284
                unsigned drq            :1;     /* Data is request by the device */
285
                unsigned dsc            :1;     /* Media access command finished */
286
                unsigned reserved5      :1;     /* Reserved */
287
                unsigned drdy           :1;     /* Ignored for ATAPI commands (ready to accept ATA command) */
288
                unsigned bsy            :1;     /* The device has access to the command block */
289
        } b;
290
} idefloppy_status_reg_t;
291
 
292
/*
293
 *      The ATAPI error register.
294
 */
295
typedef union {
296
        unsigned all                    :8;
297
        struct {
298
                unsigned ili            :1;     /* Illegal Length Indication */
299
                unsigned eom            :1;     /* End Of Media Detected */
300
                unsigned abrt           :1;     /* Aborted command - As defined by ATA */
301
                unsigned mcr            :1;     /* Media Change Requested - As defined by ATA */
302
                unsigned sense_key      :4;     /* Sense key of the last failed packet command */
303
        } b;
304
} idefloppy_error_reg_t;
305
 
306
/*
307
 *      ATAPI Feature Register
308
 */
309
typedef union {
310
        unsigned all                    :8;
311
        struct {
312
                unsigned dma            :1;     /* Using DMA or PIO */
313
                unsigned reserved321    :3;     /* Reserved */
314
                unsigned reserved654    :3;     /* Reserved (Tag Type) */
315
                unsigned reserved7      :1;     /* Reserved */
316
        } b;
317
} idefloppy_feature_reg_t;
318
 
319
/*
320
 *      ATAPI Byte Count Register.
321
 */
322
typedef union {
323
        unsigned all                    :16;
324
        struct {
325
                unsigned low            :8;     /* LSB */
326
                unsigned high           :8;     /* MSB */
327
        } b;
328
} idefloppy_bcount_reg_t;
329
 
330
/*
331
 *      ATAPI Interrupt Reason Register.
332
 */
333
typedef union {
334
        unsigned all                    :8;
335
        struct {
336
                unsigned cod            :1;     /* Information transferred is command (1) or data (0) */
337
                unsigned io             :1;     /* The device requests us to read (1) or write (0) */
338
                unsigned reserved       :6;     /* Reserved */
339
        } b;
340
} idefloppy_ireason_reg_t;
341
 
342
/*
343
 *      ATAPI floppy Drive Select Register
344
 */
345
typedef union {
346
        unsigned all                    :8;
347
        struct {
348
                unsigned sam_lun        :3;     /* Logical unit number */
349
                unsigned reserved3      :1;     /* Reserved */
350
                unsigned drv            :1;     /* The responding drive will be drive 0 (0) or drive 1 (1) */
351
                unsigned one5           :1;     /* Should be set to 1 */
352
                unsigned reserved6      :1;     /* Reserved */
353
                unsigned one7           :1;     /* Should be set to 1 */
354
        } b;
355
} idefloppy_drivesel_reg_t;
356
 
357
/*
358
 *      ATAPI Device Control Register
359
 */
360
typedef union {
361
        unsigned all                    :8;
362
        struct {
363
                unsigned zero0          :1;     /* Should be set to zero */
364
                unsigned nien           :1;     /* Device interrupt is disabled (1) or enabled (0) */
365
                unsigned srst           :1;     /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
366
                unsigned one3           :1;     /* Should be set to 1 */
367
                unsigned reserved4567   :4;     /* Reserved */
368
        } b;
369
} idefloppy_control_reg_t;
370
 
371
/*
372
 *      The following is used to format the general configuration word of
373
 *      the ATAPI IDENTIFY DEVICE command.
374
 */
375
struct idefloppy_id_gcw {
376
        unsigned packet_size            :2;     /* Packet Size */
377
        unsigned reserved234            :3;     /* Reserved */
378
        unsigned drq_type               :2;     /* Command packet DRQ type */
379
        unsigned removable              :1;     /* Removable media */
380
        unsigned device_type            :5;     /* Device type */
381
        unsigned reserved13             :1;     /* Reserved */
382
        unsigned protocol               :2;     /* Protocol type */
383
};
384
 
385
/*
386
 *      INQUIRY packet command - Data Format
387
 */
388
typedef struct {
389
        unsigned        device_type     :5;     /* Peripheral Device Type */
390
        unsigned        reserved0_765   :3;     /* Peripheral Qualifier - Reserved */
391
        unsigned        reserved1_6t0   :7;     /* Reserved */
392
        unsigned        rmb             :1;     /* Removable Medium Bit */
393
        unsigned        ansi_version    :3;     /* ANSI Version */
394
        unsigned        ecma_version    :3;     /* ECMA Version */
395
        unsigned        iso_version     :2;     /* ISO Version */
396
        unsigned        response_format :4;     /* Response Data Format */
397
        unsigned        reserved3_45    :2;     /* Reserved */
398
        unsigned        reserved3_6     :1;     /* TrmIOP - Reserved */
399
        unsigned        reserved3_7     :1;     /* AENC - Reserved */
400
        u8              additional_length;      /* Additional Length (total_length-4) */
401
        u8              rsv5, rsv6, rsv7;       /* Reserved */
402
        u8              vendor_id[8];           /* Vendor Identification */
403
        u8              product_id[16];         /* Product Identification */
404
        u8              revision_level[4];      /* Revision Level */
405
        u8              vendor_specific[20];    /* Vendor Specific - Optional */
406
        u8              reserved56t95[40];      /* Reserved - Optional */
407
                                                /* Additional information may be returned */
408
} idefloppy_inquiry_result_t;
409
 
410
/*
411
 *      REQUEST SENSE packet command result - Data Format.
412
 */
413
typedef struct {
414
        unsigned        error_code      :7;     /* Current error (0x70) */
415
        unsigned        valid           :1;     /* The information field conforms to SFF-8070i */
416
        u8              reserved1       :8;     /* Reserved */
417
        unsigned        sense_key       :4;     /* Sense Key */
418
        unsigned        reserved2_4     :1;     /* Reserved */
419
        unsigned        ili             :1;     /* Incorrect Length Indicator */
420
        unsigned        reserved2_67    :2;
421
        u32             information __attribute__ ((packed));
422
        u8              asl;                    /* Additional sense length (n-7) */
423
        u32             command_specific;       /* Additional command specific information */
424
        u8              asc;                    /* Additional Sense Code */
425
        u8              ascq;                   /* Additional Sense Code Qualifier */
426
        u8              replaceable_unit_code;  /* Field Replaceable Unit Code */
427
        u8              reserved[3];
428
        u8              pad[2];                 /* Padding to 20 bytes */
429
} idefloppy_request_sense_result_t;
430
 
431
/*
432
 *      Pages of the SELECT SENSE / MODE SENSE packet commands.
433
 */
434
#define IDEFLOPPY_CAPABILITIES_PAGE     0x1b
435
#define IDEFLOPPY_FLEXIBLE_DISK_PAGE    0x05
436
 
437
/*
438
 *      Mode Parameter Header for the MODE SENSE packet command
439
 */
440
typedef struct {
441
        u16             mode_data_length;       /* Length of the following data transfer */
442
        u8              medium_type;            /* Medium Type */
443
        unsigned        reserved3       :7;
444
        unsigned        wp              :1;     /* Write protect */
445
        u8              reserved[4];
446
} idefloppy_mode_parameter_header_t;
447
 
448
#define IDEFLOPPY_MIN(a,b)      ((a)<(b) ? (a):(b))
449
#define IDEFLOPPY_MAX(a,b)      ((a)>(b) ? (a):(b))
450
 
451
/*
452
 *      Too bad. The drive wants to send us data which we are not ready to accept.
453
 *      Just throw it away.
454
 */
455
static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
456
{
457
        while (bcount--)
458
                IN_BYTE (IDE_DATA_REG);
459
}
460
 
461
#if IDEFLOPPY_DEBUG_BUGS
462
static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
463
{
464
        while (bcount--)
465
                OUT_BYTE (0, IDE_DATA_REG);
466
}
467
#endif /* IDEFLOPPY_DEBUG_BUGS */
468
 
469
/*
470
 *      idefloppy_end_request is used to finish servicing a request.
471
 *
472
 *      For read/write requests, we will call ide_end_request to pass to the
473
 *      next buffer.
474
 */
475
void idefloppy_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
476
{
477
        ide_drive_t *drive = hwgroup->drive;
478
        idefloppy_floppy_t *floppy = drive->floppy;
479
        struct request *rq = hwgroup->rq;
480
        int error;
481
 
482
#if IDEFLOPPY_DEBUG_LOG
483
        printk (KERN_INFO "Reached idefloppy_end_request\n");
484
#endif /* IDEFLOPPY_DEBUG_LOG */
485
 
486
        switch (uptodate) {
487
                case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
488
                case 1: error = 0; break;
489
                default: error = uptodate;
490
        }
491
        if (error)
492
                floppy->failed_pc = NULL;
493
        if (!IDEFLOPPY_RQ_CMD (rq->cmd)) {
494
                ide_end_request (uptodate, hwgroup);
495
                return;
496
        }
497
        rq->errors = error;
498
        ide_end_drive_cmd (drive, 0, 0);
499
}
500
 
501
static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
502
{
503
        struct request *rq = pc->rq;
504
        struct buffer_head *bh = rq->bh;
505
        int count;
506
 
507
        while (bcount) {
508
                if (pc->b_count == bh->b_size) {
509
                        idefloppy_end_request (1, HWGROUP(drive));
510
                        if ((bh = rq->bh) != NULL)
511
                                pc->b_count = 0;
512
                }
513
                if (bh == NULL) {
514
                        printk (KERN_ERR "%s: bh == NULL in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
515
                        idefloppy_discard_data (drive, bcount);
516
                        return;
517
                }
518
                count = IDEFLOPPY_MIN (bh->b_size - pc->b_count, bcount);
519
                atapi_input_bytes (drive, bh->b_data + pc->b_count, count);
520
                bcount -= count; pc->b_count += count;
521
                if (pc->b_count == bh->b_size) {
522
                        rq->sector += rq->current_nr_sectors;
523
                        rq->nr_sectors -= rq->current_nr_sectors;
524
                }
525
        }
526
}
527
 
528
static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
529
{
530
        struct request *rq = pc->rq;
531
        struct buffer_head *bh = rq->bh;
532
        int count;
533
 
534
        while (bcount) {
535
                if (!pc->b_count) {
536
                        idefloppy_end_request (1, HWGROUP(drive));
537
                        if ((bh = rq->bh) != NULL) {
538
                                pc->b_data = bh->b_data;
539
                                pc->b_count = bh->b_size;
540
                        }
541
                }
542
                if (bh == NULL) {
543
                        printk (KERN_ERR "%s: bh == NULL in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
544
                        idefloppy_write_zeros (drive, bcount);
545
                        return;
546
                }
547
                count = IDEFLOPPY_MIN (pc->b_count, bcount);
548
                atapi_output_bytes (drive, pc->b_data, count);
549
                bcount -= count; pc->b_data += count; pc->b_count -= count;
550
                if (!pc->b_count) {
551
                        rq->sector += rq->current_nr_sectors;
552
                        rq->nr_sectors -= rq->current_nr_sectors;
553
                }
554
        }
555
}
556
 
557
#ifdef CONFIG_BLK_DEV_TRITON
558
static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
559
{
560
        struct request *rq = pc->rq;
561
        struct buffer_head *bh = rq->bh;
562
 
563
        while ((bh = rq->bh) != NULL)
564
                idefloppy_end_request (1, HWGROUP(drive));
565
}
566
#endif /* CONFIG_BLK_DEV_TRITON */
567
 
568
/*
569
 *      idefloppy_queue_pc_head generates a new packet command request in front
570
 *      of the request queue, before the current request, so that it will be
571
 *      processed immediately, on the next pass through the driver.
572
 */
573
static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
574
{
575
        ide_init_drive_cmd (rq);
576
        rq->buffer = (char *) pc;
577
        rq->cmd = IDEFLOPPY_PC_RQ;
578
        (void) ide_do_drive_cmd (drive, rq, ide_preempt);
579
}
580
 
581
static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
582
{
583
        idefloppy_floppy_t *floppy = drive->floppy;
584
 
585
        if (floppy->pc_stack_index==IDEFLOPPY_PC_STACK)
586
                floppy->pc_stack_index=0;
587
        return (&floppy->pc_stack[floppy->pc_stack_index++]);
588
}
589
 
590
static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
591
{
592
        idefloppy_floppy_t *floppy = drive->floppy;
593
 
594
        if (floppy->rq_stack_index==IDEFLOPPY_PC_STACK)
595
                floppy->rq_stack_index=0;
596
        return (&floppy->rq_stack[floppy->rq_stack_index++]);
597
}
598
 
599
/*
600
 *      idefloppy_analyze_error is called on each failed packet command retry
601
 *      to analyze the request sense.
602
 */
603
static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
604
{
605
        idefloppy_floppy_t *floppy = drive->floppy;
606
 
607
        floppy->sense_key = result->sense_key; floppy->asc = result->asc; floppy->ascq = result->ascq;
608
#if IDEFLOPPY_DEBUG_LOG
609
        if (floppy->failed_pc)
610
                printk (KERN_INFO "ide-floppy: pc = %x, sense key = %x, asc = %x, ascq = %x\n",floppy->failed_pc->c[0],result->sense_key,result->asc,result->ascq);
611
        else
612
                printk (KERN_INFO "ide-floppy: sense key = %x, asc = %x, ascq = %x\n",result->sense_key,result->asc,result->ascq);
613
#endif /* IDEFLOPPY_DEBUG_LOG */
614
}
615
 
616
static void idefloppy_request_sense_callback (ide_drive_t *drive)
617
{
618
        idefloppy_floppy_t *floppy = drive->floppy;
619
 
620
#if IDEFLOPPY_DEBUG_LOG
621
        printk (KERN_INFO "ide-floppy: Reached idefloppy_request_sense_callback\n");
622
#endif /* IDEFLOPPY_DEBUG_LOG */
623
        if (!floppy->pc->error) {
624
                idefloppy_analyze_error (drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
625
                idefloppy_end_request (1,HWGROUP (drive));
626
        } else {
627
                printk (KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
628
                idefloppy_end_request (0,HWGROUP (drive));
629
        }
630
}
631
 
632
/*
633
 *      General packet command callback function.
634
 */
635
static void idefloppy_pc_callback (ide_drive_t *drive)
636
{
637
        idefloppy_floppy_t *floppy = drive->floppy;
638
 
639
#if IDEFLOPPY_DEBUG_LOG
640
        printk (KERN_INFO "ide-floppy: Reached idefloppy_pc_callback\n");
641
#endif /* IDEFLOPPY_DEBUG_LOG */
642
 
643
        idefloppy_end_request (floppy->pc->error ? 0:1, HWGROUP(drive));
644
}
645
 
646
/*
647
 *      idefloppy_init_pc initializes a packet command.
648
 */
649
static void idefloppy_init_pc (idefloppy_pc_t *pc)
650
{
651
        memset (pc->c, 0, 12);
652
        pc->retries = 0;
653
        pc->flags = 0;
654
        pc->request_transfer = 0;
655
        pc->buffer = pc->pc_buffer;
656
        pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
657
        pc->b_data = NULL;
658
        pc->callback = &idefloppy_pc_callback;
659
}
660
 
661
static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
662
{
663
        idefloppy_init_pc (pc);
664
        pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
665
        pc->c[4] = 255;
666
        pc->request_transfer = 18;
667
        pc->callback = &idefloppy_request_sense_callback;
668
}
669
 
670
/*
671
 *      idefloppy_retry_pc is called when an error was detected during the
672
 *      last packet command. We queue a request sense packet command in
673
 *      the head of the request list.
674
 */
675
static void idefloppy_retry_pc (ide_drive_t *drive)
676
{
677
        idefloppy_pc_t *pc;
678
        struct request *rq;
679
        idefloppy_error_reg_t error;
680
 
681
        error.all = IN_BYTE (IDE_ERROR_REG);
682
        pc = idefloppy_next_pc_storage (drive);
683
        rq = idefloppy_next_rq_storage (drive);
684
        idefloppy_create_request_sense_cmd (pc);
685
        idefloppy_queue_pc_head (drive, pc, rq);
686
}
687
 
688
/*
689
 *      idefloppy_pc_intr is the usual interrupt handler which will be called
690
 *      during a packet command.
691
 */
692
static void idefloppy_pc_intr (ide_drive_t *drive)
693
{
694
        idefloppy_floppy_t *floppy = drive->floppy;
695
        idefloppy_status_reg_t status;
696
        idefloppy_bcount_reg_t bcount;
697
        idefloppy_ireason_reg_t ireason;
698
        idefloppy_pc_t *pc=floppy->pc;
699
        struct request *rq = pc->rq;
700
        unsigned int temp;
701
 
702
#if IDEFLOPPY_DEBUG_LOG
703
        printk (KERN_INFO "ide-floppy: Reached idefloppy_pc_intr interrupt handler\n");
704
#endif /* IDEFLOPPY_DEBUG_LOG */        
705
 
706
#ifdef CONFIG_BLK_DEV_TRITON
707
        if (test_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
708
                if (HWIF(drive)->dmaproc(ide_dma_status_bad, drive)) {
709
                        set_bit (PC_DMA_ERROR, &pc->flags);
710
                } else {
711
                        pc->actually_transferred=pc->request_transfer;
712
                        idefloppy_update_buffers (drive, pc);
713
                }
714
                (void) (HWIF(drive)->dmaproc(ide_dma_abort, drive));    /* End DMA */
715
#if IDEFLOPPY_DEBUG_LOG
716
                printk (KERN_INFO "ide-floppy: DMA finished\n");
717
#endif /* IDEFLOPPY_DEBUG_LOG */
718
        }
719
#endif /* CONFIG_BLK_DEV_TRITON */
720
 
721
        status.all = GET_STAT();                                        /* Clear the interrupt */
722
 
723
        if (!status.b.drq) {                                            /* No more interrupts */
724
#if IDEFLOPPY_DEBUG_LOG
725
                printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
726
#endif /* IDEFLOPPY_DEBUG_LOG */
727
                clear_bit (PC_DMA_IN_PROGRESS, &pc->flags);
728
 
729
                sti();
730
 
731
                if (status.b.check || test_bit (PC_DMA_ERROR, &pc->flags)) {    /* Error detected */
732
#if IDEFLOPPY_DEBUG_LOG
733
                        printk (KERN_INFO "ide-floppy: %s: I/O error, ",drive->name);
734
#endif /* IDEFLOPPY_DEBUG_LOG */
735
                        rq->errors++;
736
                        if (pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
737
                                printk (KERN_ERR "ide-floppy: I/O error in request sense command\n");
738
                                ide_do_reset (drive);
739
                                return;
740
                        }
741
                        idefloppy_retry_pc (drive);                             /* Retry operation */
742
                        return;
743
                }
744
                pc->error = 0;
745
                if (floppy->failed_pc == pc)
746
                        floppy->failed_pc=NULL;
747
                pc->callback(drive);                    /* Command finished - Call the callback function */
748
                return;
749
        }
750
#ifdef CONFIG_BLK_DEV_TRITON
751
        if (clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
752
                printk (KERN_ERR "ide-floppy: The floppy wants to issue more interrupts in DMA mode\n");
753
                printk (KERN_ERR "ide-floppy: DMA disabled, reverting to PIO\n");
754
                drive->using_dma=0;
755
                ide_do_reset (drive);
756
                return;
757
        }
758
#endif /* CONFIG_BLK_DEV_TRITON */
759
        bcount.b.high=IN_BYTE (IDE_BCOUNTH_REG);                        /* Get the number of bytes to transfer */
760
        bcount.b.low=IN_BYTE (IDE_BCOUNTL_REG);                 /* on this interrupt */
761
        ireason.all=IN_BYTE (IDE_IREASON_REG);
762
 
763
        if (ireason.b.cod) {
764
                printk (KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
765
                ide_do_reset (drive);
766
                return;
767
        }
768
        if (ireason.b.io == test_bit (PC_WRITING, &pc->flags)) {        /* Hopefully, we will never get here */
769
                printk (KERN_ERR "ide-floppy: We wanted to %s, ", ireason.b.io ? "Write":"Read");
770
                printk (KERN_ERR "but the floppy wants us to %s !\n",ireason.b.io ? "Read":"Write");
771
                ide_do_reset (drive);
772
                return;
773
        }
774
        if (!test_bit (PC_WRITING, &pc->flags)) {                       /* Reading - Check that we have enough space */
775
                temp = pc->actually_transferred + bcount.all;
776
                if ( temp > pc->request_transfer) {
777
                        if (temp > pc->buffer_size) {
778
                                printk (KERN_ERR "ide-floppy: The floppy wants to send us more data than expected - discarding data\n");
779
                                idefloppy_discard_data (drive,bcount.all);
780
                                ide_set_handler (drive,&idefloppy_pc_intr,IDEFLOPPY_WAIT_CMD);
781
                                return;
782
                        }
783
#if IDEFLOPPY_DEBUG_LOG
784
                        printk (KERN_NOTICE "ide-floppy: The floppy wants to send us more data than expected - allowing transfer\n");
785
#endif /* IDEFLOPPY_DEBUG_LOG */
786
                }
787
        }
788
        if (test_bit (PC_WRITING, &pc->flags)) {
789
                if (pc->buffer != NULL)
790
                        atapi_output_bytes (drive,pc->current_position,bcount.all);     /* Write the current buffer */
791
                else
792
                        idefloppy_output_buffers (drive, pc, bcount.all);
793
        } else {
794
                if (pc->buffer != NULL)
795
                        atapi_input_bytes (drive,pc->current_position,bcount.all);      /* Read the current buffer */
796
                else
797
                        idefloppy_input_buffers (drive, pc, bcount.all);
798
        }
799
        pc->actually_transferred+=bcount.all;                           /* Update the current position */
800
        pc->current_position+=bcount.all;
801
 
802
        ide_set_handler (drive,&idefloppy_pc_intr,IDEFLOPPY_WAIT_CMD);          /* And set the interrupt handler again */
803
}
804
 
805
static void idefloppy_transfer_pc (ide_drive_t *drive)
806
{
807
        idefloppy_floppy_t *floppy = drive->floppy;
808
        idefloppy_ireason_reg_t ireason;
809
 
810
        if (ide_wait_stat (drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
811
                printk (KERN_ERR "ide-floppy: Strange, packet command initiated yet DRQ isn't asserted\n");
812
                return;
813
        }
814
        ireason.all=IN_BYTE (IDE_IREASON_REG);
815
        if (!ireason.b.cod || ireason.b.io) {
816
                printk (KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while issuing a packet command\n");
817
                ide_do_reset (drive);
818
                return;
819
        }
820
        ide_set_handler (drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD);        /* Set the interrupt routine */
821
        atapi_output_bytes (drive, floppy->pc->c, 12);          /* Send the actual packet */
822
}
823
 
824
/*
825
 *      Issue a packet command
826
 */
827
static void idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
828
{
829
        idefloppy_floppy_t *floppy = drive->floppy;
830
        idefloppy_bcount_reg_t bcount;
831
        int dma_ok = 0;
832
 
833
#if IDEFLOPPY_DEBUG_BUGS
834
        if (floppy->pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD && pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
835
                printk (KERN_ERR "ide-floppy: possible ide-floppy.c bug - Two request sense in serial were issued\n");
836
        }
837
#endif /* IDEFLOPPY_DEBUG_BUGS */
838
 
839
        if (floppy->failed_pc == NULL && pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
840
                floppy->failed_pc=pc;
841
        floppy->pc=pc;                                                  /* Set the current packet command */
842
 
843
        if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES || test_bit (PC_ABORT, &pc->flags)) {
844
                /*
845
                 *      We will "abort" retrying a packet command in case
846
                 *      a legitimate error code was received.
847
                 */
848
                if (!test_bit (PC_ABORT, &pc->flags)) {
849
                        printk (KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
850
                                drive->name, pc->c[0], floppy->sense_key, floppy->asc, floppy->ascq);
851
                        pc->error = IDEFLOPPY_ERROR_GENERAL;            /* Giving up */
852
                }
853
                floppy->failed_pc=NULL;
854
                pc->callback(drive);
855
                return;
856
        }
857
#if IDEFLOPPY_DEBUG_LOG
858
        printk (KERN_INFO "Retry number - %d\n",pc->retries);
859
#endif /* IDEFLOPPY_DEBUG_LOG */
860
 
861
        pc->retries++;
862
        pc->actually_transferred=0;                                      /* We haven't transferred any data yet */
863
        pc->current_position=pc->buffer;
864
        bcount.all=pc->request_transfer;                                /* Request to transfer the entire buffer at once */
865
 
866
#ifdef CONFIG_BLK_DEV_TRITON
867
        if (clear_bit (PC_DMA_ERROR, &pc->flags)) {
868
                printk (KERN_WARNING "ide-floppy: DMA disabled, reverting to PIO\n");
869
                drive->using_dma=0;
870
        }
871
        if (test_bit (PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
872
                dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
873
#endif /* CONFIG_BLK_DEV_TRITON */
874
 
875
        OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
876
        OUT_BYTE (dma_ok ? 1:0,IDE_FEATURE_REG);                 /* Use PIO/DMA */
877
        OUT_BYTE (bcount.b.high,IDE_BCOUNTH_REG);
878
        OUT_BYTE (bcount.b.low,IDE_BCOUNTL_REG);
879
        OUT_BYTE (drive->select.all,IDE_SELECT_REG);
880
 
881
#ifdef CONFIG_BLK_DEV_TRITON
882
        if (dma_ok) {                                                   /* Begin DMA, if necessary */
883
                set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
884
                (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
885
        }
886
#endif /* CONFIG_BLK_DEV_TRITON */
887
 
888
        if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
889
                ide_set_handler (drive, &idefloppy_transfer_pc, IDEFLOPPY_WAIT_CMD);
890
                OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);              /* Issue the packet command */
891
        } else {
892
                OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);
893
                idefloppy_transfer_pc (drive);
894
        }
895
}
896
 
897
static void idefloppy_rw_callback (ide_drive_t *drive)
898
{
899
#if IDEFLOPPY_DEBUG_LOG 
900
        printk (KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
901
#endif /* IDEFLOPPY_DEBUG_LOG */
902
 
903
        idefloppy_end_request(1, HWGROUP(drive));
904
        return;
905
}
906
 
907
static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
908
{
909
#if IDEFLOPPY_DEBUG_LOG
910
        printk (KERN_INFO "ide-floppy: creating prevent removal command, prevent = %d\n", prevent);
911
#endif /* IDEFLOPPY_DEBUG_LOG */
912
 
913
        idefloppy_init_pc (pc);
914
        pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
915
        pc->c[4] = prevent;
916
}
917
 
918
static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
919
{
920
        idefloppy_init_pc (pc);
921
        pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
922
        pc->c[7] = 255;
923
        pc->c[8] = 255;
924
}
925
 
926
/*
927
 *      A mode sense command is used to "sense" floppy parameters.
928
 */
929
static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, byte page_code, byte type)
930
{
931
        unsigned short length = sizeof (idefloppy_mode_parameter_header_t);
932
 
933
        idefloppy_init_pc (pc);
934
        pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
935
        pc->c[1] = 0;
936
        pc->c[2] = page_code + (type << 6);
937
 
938
        switch (page_code) {
939
                case IDEFLOPPY_CAPABILITIES_PAGE:
940
                        length += 12;
941
                        break;
942
                case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
943
                        length += 32;
944
                        break;
945
                default:
946
                        printk (KERN_ERR "ide-floppy: unsupported page code in create_mode_sense_cmd\n");
947
        }
948
        put_unaligned (htons (length), (unsigned short *) &pc->c[7]);
949
        pc->request_transfer = length;
950
}
951
 
952
static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
953
{
954
        idefloppy_init_pc (pc);
955
        pc->c[0] = IDEFLOPPY_START_STOP_CMD;
956
        pc->c[4] = start;
957
}
958
 
959
static void idefloppy_create_test_unit_ready_cmd (idefloppy_pc_t *pc)
960
{
961
        idefloppy_init_pc (pc);
962
        pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
963
}
964
 
965
static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
966
{
967
        int block = sector / floppy->bs_factor;
968
        int blocks = IDEFLOPPY_MIN(rq->nr_sectors, floppy->max_sectors) / floppy->bs_factor;
969
 
970
#if IDEFLOPPY_DEBUG_LOG
971
        printk ("create_rw1%d_cmd: block == %d, blocks == %d\n",
972
                2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags), block, blocks);
973
#endif /* IDEFLOPPY_DEBUG_LOG */
974
 
975
        idefloppy_init_pc (pc);
976
        if (test_bit (IDEFLOPPY_USE_READ12, &floppy->flags)) {
977
                pc->c[0] = rq->cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
978
                put_unaligned (htonl (blocks), (unsigned int *) &pc->c[6]);
979
        } else {
980
                pc->c[0] = rq->cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
981
                put_unaligned (htons (blocks), (unsigned short *) &pc->c[7]);
982
        }
983
        put_unaligned (htonl (block), (unsigned int *) &pc->c[2]);
984
        pc->callback = &idefloppy_rw_callback;
985
        pc->rq = rq;
986
        pc->b_data = rq->buffer;
987
        pc->b_count = rq->cmd == READ ? 0 : rq->bh->b_size;
988
        if (rq->cmd == WRITE)
989
                set_bit (PC_WRITING, &pc->flags);
990
        pc->buffer = NULL;
991
        pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
992
        set_bit (PC_DMA_RECOMMENDED, &pc->flags);
993
}
994
 
995
/*
996
 *      idefloppy_do_request is our request handling function.
997
 */
998
void idefloppy_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
999
{
1000
        idefloppy_floppy_t *floppy = drive->floppy;
1001
        idefloppy_pc_t *pc;
1002
 
1003
#if IDEFLOPPY_DEBUG_LOG
1004
        printk (KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
1005
        printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
1006
#endif /* IDEFLOPPY_DEBUG_LOG */
1007
 
1008
        if (rq->errors >= ERROR_MAX) {
1009
                if (floppy->failed_pc != NULL)
1010
                        printk (KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2x\n",
1011
                                drive->name, floppy->failed_pc->c[0], floppy->sense_key, floppy->asc, floppy->ascq);
1012
                else
1013
                        printk (KERN_ERR "ide-floppy: %s: I/O error\n", drive->name);
1014
                idefloppy_end_request (0, HWGROUP(drive));
1015
                return;
1016
        }
1017
        switch (rq->cmd) {
1018
                case READ:
1019
                case WRITE:
1020
                        if (rq->sector % floppy->bs_factor || rq->nr_sectors % floppy->bs_factor) {
1021
                                printk ("%s: unsupported r/w request size\n", drive->name);
1022
                                idefloppy_end_request (0, HWGROUP(drive));
1023
                                return;
1024
                        }
1025
                        pc = idefloppy_next_pc_storage (drive);
1026
                        idefloppy_create_rw_cmd (floppy, pc, rq, block);
1027
                        break;
1028
                case IDEFLOPPY_PC_RQ:
1029
                        pc = (idefloppy_pc_t *) rq->buffer;
1030
                        break;
1031
                default:
1032
                        printk (KERN_ERR "ide-floppy: unsupported command %x in request queue\n", rq->cmd);
1033
                        idefloppy_end_request (0,HWGROUP (drive));
1034
                        return;
1035
        }
1036
        pc->rq = rq;
1037
        idefloppy_issue_pc (drive, pc);
1038
}
1039
 
1040
/*
1041
 *      idefloppy_queue_pc_tail adds a special packet command request to the
1042
 *      tail of the request queue, and waits for it to be serviced.
1043
 */
1044
static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1045
{
1046
        struct request rq;
1047
 
1048
        ide_init_drive_cmd (&rq);
1049
        rq.buffer = (char *) pc;
1050
        rq.cmd = IDEFLOPPY_PC_RQ;
1051
        return ide_do_drive_cmd (drive, &rq, ide_wait);
1052
}
1053
 
1054
/*
1055
 *      Look at the flexible disk page parameters. We will ignore the CHS
1056
 *      capacity parameters and use the LBA parameters instead.
1057
 */
1058
static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1059
{
1060
        idefloppy_floppy_t *floppy = drive->floppy;
1061
        idefloppy_pc_t pc;
1062
        idefloppy_mode_parameter_header_t *header;
1063
        idefloppy_flexible_disk_page_t *page;
1064
        int capacity, lba_capacity;
1065
 
1066
        idefloppy_create_mode_sense_cmd (&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1067
        if (idefloppy_queue_pc_tail (drive,&pc)) {
1068
                printk (KERN_ERR "ide-floppy: Can't get flexible disk page parameters\n");
1069
                return 1;
1070
        }
1071
        header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1072
        floppy->wp = header->wp;
1073
        page = (idefloppy_flexible_disk_page_t *) (header + 1);
1074
 
1075
        page->transfer_rate = ntohs (page->transfer_rate);
1076
        page->sector_size = ntohs (page->sector_size);
1077
        page->cyls = ntohs (page->cyls);
1078
        page->rpm = ntohs (page->rpm);
1079
        capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1080
        if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1081
                printk (KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, %d sector size, %d rpm\n",
1082
                        drive->name, capacity / 1024, page->cyls, page->heads, page->sectors,
1083
                        page->transfer_rate / 8, page->sector_size, page->rpm);
1084
 
1085
        floppy->flexible_disk_page = *page;
1086
        drive->bios_cyl = page->cyls;
1087
        drive->bios_head = page->heads;
1088
        drive->bios_sect = page->sectors;
1089
        lba_capacity = floppy->blocks * floppy->block_size;
1090
        if (capacity != lba_capacity) {
1091
                if (!lba_capacity)
1092
                        printk(KERN_NOTICE "%s: no media in the drive\n", drive->name);
1093
                else printk (KERN_NOTICE "%s: The drive reports both %d and %d bytes as its capacity\n",
1094
                             drive->name, capacity, lba_capacity);
1095
                capacity = IDEFLOPPY_MIN(capacity, lba_capacity);
1096
                floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1097
        }
1098
        return 0;
1099
}
1100
 
1101
/*
1102
 *      Determine if a media is present in the floppy drive, and if so,
1103
 *      its LBA capacity.
1104
 */
1105
static int idefloppy_get_capacity (ide_drive_t *drive)
1106
{
1107
        idefloppy_floppy_t *floppy = drive->floppy;
1108
        idefloppy_pc_t pc;
1109
        idefloppy_capacity_header_t *header;
1110
        idefloppy_capacity_descriptor_t *descriptor;
1111
        int i, descriptors, rc = 1, blocks, length;
1112
 
1113
        drive->bios_cyl = 0;
1114
        drive->bios_head = drive->bios_sect = 0;
1115
        floppy->blocks = floppy->bs_factor = 0;
1116
        drive->part[0].nr_sects = 0;
1117
 
1118
        idefloppy_create_read_capacity_cmd (&pc);
1119
        if (idefloppy_queue_pc_tail (drive, &pc)) {
1120
                printk (KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1121
                return 1;
1122
        }
1123
        header = (idefloppy_capacity_header_t *) pc.buffer;
1124
        descriptors = header->length / sizeof (idefloppy_capacity_descriptor_t);
1125
        descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1126
        for (i = 0; i < descriptors; i++, descriptor++) {
1127
                blocks = descriptor->blocks = ntohl (descriptor->blocks);
1128
                length = descriptor->length = ntohs (descriptor->length);
1129
                if (!i && descriptor->dc == CAPACITY_CURRENT) {
1130
                        if (memcmp (descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1131
                                printk (KERN_INFO "%s: %dkB, %d blocks, %d sector size\n", drive->name, blocks * length / 1024, blocks, length);
1132
                        floppy->capacity = *descriptor;
1133
                        if (!length || length % 512)
1134
                                printk (KERN_ERR "%s: %d bytes block size not supported\n", drive->name, length);
1135
                        else {
1136
                                floppy->blocks = blocks;
1137
                                floppy->block_size = length;
1138
                                if ((floppy->bs_factor = length / 512) != 1)
1139
                                        printk (KERN_NOTICE "%s: warning: non 512 bytes block size not fully supported\n", drive->name);
1140
                                rc = 0;
1141
                        }
1142
                }
1143
#if IDEFLOPPY_DEBUG_INFO
1144
                if (!i) printk (KERN_INFO "Descriptor 0 Code: %d\n", descriptor->dc);
1145
                printk (KERN_INFO "Descriptor %d: %dkB, %d blocks, %d sector size\n", i, blocks * length / 1024, blocks, length);
1146
#endif /* IDEFLOPPY_DEBUG_INFO */
1147
        }
1148
        (void) idefloppy_get_flexible_disk_page (drive);
1149
        drive->part[0].nr_sects = floppy->blocks * floppy->bs_factor;
1150
        return rc;
1151
}
1152
 
1153
/*
1154
 *      Our special ide-floppy ioctl's.
1155
 *
1156
 *      Currently there aren't any ioctl's.
1157
 */
1158
int idefloppy_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
1159
                        unsigned int cmd, unsigned long arg)
1160
{
1161
        idefloppy_pc_t pc;
1162
 
1163
        if (cmd == CDROMEJECT) {
1164
                if (drive->usage > 1)
1165
                        return -EBUSY;
1166
                idefloppy_create_prevent_cmd (&pc, 0);
1167
                (void) idefloppy_queue_pc_tail (drive, &pc);
1168
                idefloppy_create_start_stop_cmd (&pc, 2);
1169
                (void) idefloppy_queue_pc_tail (drive, &pc);
1170
                return 0;
1171
        }
1172
        return -EIO;
1173
}
1174
 
1175
/*
1176
 *      Our open/release functions
1177
 */
1178
int idefloppy_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
1179
{
1180
        idefloppy_floppy_t *floppy = drive->floppy;
1181
        idefloppy_pc_t pc;
1182
 
1183
#if IDEFLOPPY_DEBUG_LOG
1184
        printk (KERN_INFO "Reached idefloppy_open\n");
1185
#endif /* IDEFLOPPY_DEBUG_LOG */
1186
 
1187
        MOD_INC_USE_COUNT;
1188
        if (drive->usage == 1) {
1189
                idefloppy_create_test_unit_ready_cmd(&pc);
1190
                if (idefloppy_queue_pc_tail(drive, &pc)) {
1191
                        idefloppy_create_start_stop_cmd (&pc, 1);
1192
                        (void) idefloppy_queue_pc_tail (drive, &pc);
1193
                }
1194
                if (idefloppy_get_capacity (drive)) {
1195
                        drive->usage--;
1196
                        MOD_DEC_USE_COUNT;
1197
                        return -EIO;
1198
                }
1199
                if (floppy->wp && (filp->f_mode & 2)) {
1200
                        drive->usage--;
1201
                        MOD_DEC_USE_COUNT;
1202
                        return -EROFS;
1203
                }
1204
                set_bit (IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1205
                idefloppy_create_prevent_cmd (&pc, 1);
1206
                (void) idefloppy_queue_pc_tail (drive, &pc);
1207
                check_disk_change(inode->i_rdev);
1208
        }
1209
        return 0;
1210
}
1211
 
1212
void idefloppy_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
1213
{
1214
        idefloppy_pc_t pc;
1215
 
1216
#if IDEFLOPPY_DEBUG_LOG
1217
        printk (KERN_INFO "Reached idefloppy_release\n");
1218
#endif /* IDEFLOPPY_DEBUG_LOG */
1219
 
1220
        if (!drive->usage) {
1221
                invalidate_buffers (inode->i_rdev);
1222
                idefloppy_create_prevent_cmd (&pc, 0);
1223
                (void) idefloppy_queue_pc_tail (drive, &pc);
1224
        }
1225
        MOD_DEC_USE_COUNT;
1226
}
1227
 
1228
/*
1229
 *      Check media change. Use a simple algorithm for now.
1230
 */
1231
int idefloppy_media_change (ide_drive_t *drive)
1232
{
1233
        idefloppy_floppy_t *floppy = drive->floppy;
1234
 
1235
        return clear_bit (IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1236
}
1237
 
1238
/*
1239
 *      Return the current floppy capacity to ide.c.
1240
 */
1241
unsigned long idefloppy_capacity (ide_drive_t *drive)
1242
{
1243
        idefloppy_floppy_t *floppy = drive->floppy;
1244
        unsigned long capacity = floppy->blocks * floppy->bs_factor;
1245
 
1246
        return capacity;
1247
}
1248
 
1249
/*
1250
 *      idefloppy_identify_device checks if we can support a drive,
1251
 *      based on the ATAPI IDENTIFY command results.
1252
 */
1253
int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1254
{
1255
        struct idefloppy_id_gcw gcw;
1256
        idefloppy_floppy_t *floppy;
1257
#if IDEFLOPPY_DEBUG_INFO
1258
        unsigned short mask,i;
1259
        char buffer[80];
1260
#endif /* IDEFLOPPY_DEBUG_INFO */
1261
 
1262
        *((unsigned short *) &gcw) = id->config;
1263
 
1264
#if IDEFLOPPY_DEBUG_INFO
1265
        printk (KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1266
        switch (gcw.protocol) {
1267
                case 0: case 1: sprintf (buffer, "ATA");break;
1268
                case 2: sprintf (buffer, "ATAPI");break;
1269
                case 3: sprintf (buffer, "Reserved (Unknown to ide-floppy)");break;
1270
        }
1271
        printk (KERN_INFO "Protocol Type: %s\n", buffer);
1272
        switch (gcw.device_type) {
1273
                case 0: sprintf (buffer, "Direct-access Device");break;
1274
                case 1: sprintf (buffer, "Streaming Tape Device");break;
1275
                case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1276
                case 5: sprintf (buffer, "CD-ROM Device");break;
1277
                case 6: sprintf (buffer, "Reserved");
1278
                case 7: sprintf (buffer, "Optical memory Device");break;
1279
                case 0x1f: sprintf (buffer, "Unknown or no Device type");break;
1280
                default: sprintf (buffer, "Reserved");
1281
        }
1282
        printk (KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1283
        printk (KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1284
        switch (gcw.drq_type) {
1285
                case 0: sprintf (buffer, "Microprocessor DRQ");break;
1286
                case 1: sprintf (buffer, "Interrupt DRQ");break;
1287
                case 2: sprintf (buffer, "Accelerated DRQ");break;
1288
                case 3: sprintf (buffer, "Reserved");break;
1289
        }
1290
        printk (KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1291
        switch (gcw.packet_size) {
1292
                case 0: sprintf (buffer, "12 bytes");break;
1293
                case 1: sprintf (buffer, "16 bytes");break;
1294
                default: sprintf (buffer, "Reserved");break;
1295
        }
1296
        printk (KERN_INFO "Command Packet Size: %s\n", buffer);
1297
        printk (KERN_INFO "Model: %s\n",id->model);
1298
        printk (KERN_INFO "Firmware Revision: %s\n",id->fw_rev);
1299
        printk (KERN_INFO "Serial Number: %s\n",id->serial_no);
1300
        printk (KERN_INFO "Write buffer size(?): %d bytes\n",id->buf_size*512);
1301
        printk (KERN_INFO "DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
1302
        printk (KERN_INFO "LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
1303
        printk (KERN_INFO "IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
1304
        printk (KERN_INFO "IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
1305
        printk (KERN_INFO "ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
1306
        printk (KERN_INFO "PIO Cycle Timing Category: %d\n",id->tPIO);
1307
        printk (KERN_INFO "DMA Cycle Timing Category: %d\n",id->tDMA);
1308
        printk (KERN_INFO "Single Word DMA supported modes:\n");
1309
        for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1310
                if (id->dma_1word & mask)
1311
                        printk (KERN_INFO "   Mode %d%s\n", i, (id->dma_1word & (mask << 8)) ? " (active)" : "");
1312
        }
1313
        printk (KERN_INFO "Multi Word DMA supported modes:\n");
1314
        for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1315
                if (id->dma_mword & mask)
1316
                        printk (KERN_INFO "   Mode %d%s\n", i, (id->dma_mword & (mask << 8)) ? " (active)" : "");
1317
        }
1318
        if (id->field_valid & 0x0002) {
1319
                printk (KERN_INFO "Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
1320
                if (id->eide_dma_min == 0)
1321
                        sprintf (buffer, "Not supported");
1322
                else
1323
                        sprintf (buffer, "%d ns",id->eide_dma_min);
1324
                printk (KERN_INFO "Minimum Multi-word DMA cycle per word: %s\n", buffer);
1325
                if (id->eide_dma_time == 0)
1326
                        sprintf (buffer, "Not supported");
1327
                else
1328
                        sprintf (buffer, "%d ns",id->eide_dma_time);
1329
                printk (KERN_INFO "Manufacturer\'s Recommended Multi-word cycle: %s\n", buffer);
1330
                if (id->eide_pio == 0)
1331
                        sprintf (buffer, "Not supported");
1332
                else
1333
                        sprintf (buffer, "%d ns",id->eide_pio);
1334
                printk (KERN_INFO "Minimum PIO cycle without IORDY: %s\n", buffer);
1335
                if (id->eide_pio_iordy == 0)
1336
                        sprintf (buffer, "Not supported");
1337
                else
1338
                        sprintf (buffer, "%d ns",id->eide_pio_iordy);
1339
                printk (KERN_INFO "Minimum PIO cycle with IORDY: %s\n", buffer);
1340
        } else
1341
                printk (KERN_INFO "According to the device, fields 64-70 are not valid.\n");
1342
#endif /* IDEFLOPPY_DEBUG_INFO */
1343
 
1344
        if (gcw.protocol != 2)
1345
                printk (KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1346
        else if (gcw.device_type != 0)
1347
                printk (KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1348
        else if (!gcw.removable)
1349
                printk (KERN_ERR "ide-floppy: The removable flag is not set\n");
1350
        else if (gcw.drq_type == 3)
1351
                printk (KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1352
        else if (gcw.packet_size != 0)
1353
                printk (KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1354
        else if ((floppy = (idefloppy_floppy_t *) kmalloc (sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL)
1355
                printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1356
        else {
1357
                drive->floppy = floppy;
1358
                return 1;
1359
        }
1360
        printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1361
        return 0;
1362
}
1363
 
1364
/*
1365
 *      Driver initialization.
1366
 */
1367
void idefloppy_setup (ide_drive_t *drive)
1368
{
1369
        idefloppy_floppy_t *floppy = drive->floppy;
1370
        struct idefloppy_id_gcw gcw;
1371
 
1372
        *((unsigned short *) &gcw) = drive->id->config;
1373
        drive->ready_stat = 0;
1374
        memset (floppy, 0, sizeof (idefloppy_floppy_t));
1375
        floppy->drive = drive;
1376
        floppy->pc = floppy->pc_stack;
1377
        if (gcw.drq_type == 1)
1378
                set_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1379
        if (strcmp(drive->id->model, "IOMEGA ZIP 100 ATAPI") == 0 &&
1380
            ((strcmp(drive->id->fw_rev, "21.D") == 0) ||
1381
             (strcmp(drive->id->fw_rev, "23.D") == 0)))
1382
                floppy->max_sectors = 64;
1383
        else
1384
                floppy->max_sectors = IDEFLOPPY_MAX_SECTORS;
1385
        (void) idefloppy_get_capacity (drive);
1386
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.