0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 #define REALLY_SLOW_IO
0149
0150 #define DEBUGT 2
0151
0152 #define DPRINT(format, args...) \
0153 pr_info("floppy%d: " format, current_drive, ##args)
0154
0155 #define DCL_DEBUG
0156 #ifdef DCL_DEBUG
0157 #define debug_dcl(test, fmt, args...) \
0158 do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
0159 #else
0160 #define debug_dcl(test, fmt, args...) \
0161 do { if (0) DPRINT(fmt, ##args); } while (0)
0162 #endif
0163
0164
0165 static int print_unex = 1;
0166 #include <linux/module.h>
0167 #include <linux/sched.h>
0168 #include <linux/fs.h>
0169 #include <linux/kernel.h>
0170 #include <linux/timer.h>
0171 #include <linux/workqueue.h>
0172 #include <linux/fdreg.h>
0173 #include <linux/fd.h>
0174 #include <linux/hdreg.h>
0175 #include <linux/errno.h>
0176 #include <linux/slab.h>
0177 #include <linux/mm.h>
0178 #include <linux/bio.h>
0179 #include <linux/string.h>
0180 #include <linux/jiffies.h>
0181 #include <linux/fcntl.h>
0182 #include <linux/delay.h>
0183 #include <linux/mc146818rtc.h> /* CMOS defines */
0184 #include <linux/ioport.h>
0185 #include <linux/interrupt.h>
0186 #include <linux/init.h>
0187 #include <linux/major.h>
0188 #include <linux/platform_device.h>
0189 #include <linux/mod_devicetable.h>
0190 #include <linux/mutex.h>
0191 #include <linux/io.h>
0192 #include <linux/uaccess.h>
0193 #include <linux/async.h>
0194 #include <linux/compat.h>
0195
0196
0197
0198
0199
0200
0201 static DEFINE_MUTEX(floppy_mutex);
0202 static int slow_floppy;
0203
0204 #include <asm/dma.h>
0205 #include <asm/irq.h>
0206
0207 static int FLOPPY_IRQ = 6;
0208 static int FLOPPY_DMA = 2;
0209 static int can_use_virtual_dma = 2;
0210
0211
0212
0213
0214
0215
0216
0217
0218 static int use_virtual_dma;
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230 static DEFINE_SPINLOCK(floppy_lock);
0231
0232 static unsigned short virtual_dma_port = 0x3f0;
0233 irqreturn_t floppy_interrupt(int irq, void *dev_id);
0234 static int set_dor(int fdc, char mask, char data);
0235
0236 #define K_64 0x10000
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248 static int allowed_drive_mask = 0x33;
0249
0250 #include <asm/floppy.h>
0251
0252 static int irqdma_allocated;
0253
0254 #include <linux/blk-mq.h>
0255 #include <linux/blkpg.h>
0256 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
0257 #include <linux/completion.h>
0258
0259 static LIST_HEAD(floppy_reqs);
0260 static struct request *current_req;
0261 static int set_next_request(void);
0262
0263 #ifndef fd_get_dma_residue
0264 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
0265 #endif
0266
0267
0268
0269 #ifndef fd_dma_mem_free
0270 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
0271 #endif
0272
0273 #ifndef fd_dma_mem_alloc
0274 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
0275 #endif
0276
0277 #ifndef fd_cacheflush
0278 #define fd_cacheflush(addr, size)
0279 #endif
0280
0281 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
0282 {
0283 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
0284 if (*addr)
0285 return;
0286 if (can_use_virtual_dma != 2)
0287 return;
0288 pr_info("DMA memory shortage. Temporarily falling back on virtual DMA\n");
0289 *addr = (char *)nodma_mem_alloc(l);
0290 #else
0291 return;
0292 #endif
0293 }
0294
0295
0296
0297 static unsigned long fake_change;
0298 static bool initialized;
0299
0300 #define ITYPE(x) (((x) >> 2) & 0x1f)
0301 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
0302 #define UNIT(x) ((x) & 0x03)
0303 #define FDC(x) (((x) & 0x04) >> 2)
0304
0305 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
0306
0307 #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
0308 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
0309
0310
0311 #define COMMAND 0
0312 #define DR_SELECT 1
0313 #define TRACK 2
0314 #define HEAD 3
0315 #define SECTOR 4
0316 #define SIZECODE 5
0317 #define SECT_PER_TRACK 6
0318 #define GAP 7
0319 #define SIZECODE2 8
0320 #define NR_RW 9
0321
0322
0323 #define F_SIZECODE 2
0324 #define F_SECT_PER_TRACK 3
0325 #define F_GAP 4
0326 #define F_FILL 5
0327 #define NR_F 6
0328
0329
0330
0331
0332
0333
0334 #define MAX_DISK_SIZE 4
0335
0336
0337
0338
0339 static unsigned char reply_buffer[FD_RAW_REPLY_SIZE];
0340 static int inr;
0341 #define ST0 0
0342 #define ST1 1
0343 #define ST2 2
0344 #define ST3 0
0345 #define R_TRACK 3
0346 #define R_HEAD 4
0347 #define R_SECTOR 5
0348 #define R_SIZECODE 6
0349
0350 #define SEL_DLY (2 * HZ / 100)
0351
0352
0353
0354
0355 static struct {
0356 struct floppy_drive_params params;
0357 const char *name;
0358 } default_drive_params[] = {
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
0375 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
0376
0377 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
0378 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" },
0379
0380 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
0381 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" },
0382
0383 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
0384 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" },
0385
0386 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
0387 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" },
0388
0389 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
0390 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" },
0391
0392 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
0393 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" }
0394
0395
0396
0397
0398 };
0399
0400 static struct floppy_drive_params drive_params[N_DRIVE];
0401 static struct floppy_drive_struct drive_state[N_DRIVE];
0402 static struct floppy_write_errors write_errors[N_DRIVE];
0403 static struct timer_list motor_off_timer[N_DRIVE];
0404 static struct blk_mq_tag_set tag_sets[N_DRIVE];
0405 static struct block_device *opened_bdev[N_DRIVE];
0406 static DEFINE_MUTEX(open_lock);
0407 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440 static struct floppy_struct floppy_type[32] = {
0441 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL },
0442 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" },
0443 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" },
0444 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" },
0445 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" },
0446 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" },
0447 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" },
0448 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" },
0449 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" },
0450 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" },
0451
0452 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" },
0453 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" },
0454 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" },
0455 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" },
0456 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" },
0457 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" },
0458 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" },
0459 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" },
0460 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" },
0461 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" },
0462
0463 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" },
0464 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" },
0465 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" },
0466 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" },
0467 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" },
0468 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" },
0469 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" },
0470 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" },
0471 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" },
0472 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" },
0473
0474 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" },
0475 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" },
0476 };
0477
0478 static struct gendisk *disks[N_DRIVE][ARRAY_SIZE(floppy_type)];
0479
0480 #define SECTSIZE (_FD_SECTSIZE(*floppy))
0481
0482
0483 static struct floppy_struct *current_type[N_DRIVE];
0484
0485
0486
0487
0488
0489 static struct floppy_struct user_params[N_DRIVE];
0490
0491 static sector_t floppy_sizes[256];
0492
0493 static char floppy_device_name[] = "floppy";
0494
0495
0496
0497
0498
0499
0500 static int probing;
0501
0502
0503 #define FD_COMMAND_NONE -1
0504 #define FD_COMMAND_ERROR 2
0505 #define FD_COMMAND_OKAY 3
0506
0507 static volatile int command_status = FD_COMMAND_NONE;
0508 static unsigned long fdc_busy;
0509 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
0510 static DECLARE_WAIT_QUEUE_HEAD(command_done);
0511
0512
0513 static int floppy_errors;
0514
0515
0516 static struct format_descr format_req;
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530 static char *floppy_track_buffer;
0531 static int max_buffer_sectors;
0532
0533 typedef void (*done_f)(int);
0534 static const struct cont_t {
0535 void (*interrupt)(void);
0536
0537
0538 void (*redo)(void);
0539 void (*error)(void);
0540 done_f done;
0541
0542 } *cont;
0543
0544 static void floppy_ready(void);
0545 static void floppy_start(void);
0546 static void process_fd_request(void);
0547 static void recalibrate_floppy(void);
0548 static void floppy_shutdown(struct work_struct *);
0549
0550 static int floppy_request_regions(int);
0551 static void floppy_release_regions(int);
0552 static int floppy_grab_irq_and_dma(void);
0553 static void floppy_release_irq_and_dma(void);
0554
0555
0556
0557
0558
0559
0560
0561
0562 static void reset_fdc(void);
0563 static int floppy_revalidate(struct gendisk *disk);
0564
0565
0566
0567
0568
0569
0570 #define NO_TRACK -1
0571 #define NEED_1_RECAL -2
0572 #define NEED_2_RECAL -3
0573
0574 static atomic_t usage_count = ATOMIC_INIT(0);
0575
0576
0577 static int buffer_track = -1;
0578 static int buffer_drive = -1;
0579 static int buffer_min = -1;
0580 static int buffer_max = -1;
0581
0582
0583 static struct floppy_fdc_state fdc_state[N_FDC];
0584 static int current_fdc;
0585
0586 static struct workqueue_struct *floppy_wq;
0587
0588 static struct floppy_struct *_floppy = floppy_type;
0589 static unsigned char current_drive;
0590 static long current_count_sectors;
0591 static unsigned char fsector_t;
0592 static unsigned char in_sector_offset;
0593
0594
0595 static inline unsigned char fdc_inb(int fdc, int reg)
0596 {
0597 return fd_inb(fdc_state[fdc].address, reg);
0598 }
0599
0600 static inline void fdc_outb(unsigned char value, int fdc, int reg)
0601 {
0602 fd_outb(value, fdc_state[fdc].address, reg);
0603 }
0604
0605 static inline bool drive_no_geom(int drive)
0606 {
0607 return !current_type[drive] && !ITYPE(drive_state[drive].fd_device);
0608 }
0609
0610 #ifndef fd_eject
0611 static inline int fd_eject(int drive)
0612 {
0613 return -EINVAL;
0614 }
0615 #endif
0616
0617
0618
0619
0620
0621 #ifdef DEBUGT
0622 static long unsigned debugtimer;
0623
0624 static inline void set_debugt(void)
0625 {
0626 debugtimer = jiffies;
0627 }
0628
0629 static inline void debugt(const char *func, const char *msg)
0630 {
0631 if (drive_params[current_drive].flags & DEBUGT)
0632 pr_info("%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer);
0633 }
0634 #else
0635 static inline void set_debugt(void) { }
0636 static inline void debugt(const char *func, const char *msg) { }
0637 #endif
0638
0639
0640 static DECLARE_DELAYED_WORK(fd_timeout, floppy_shutdown);
0641 static const char *timeout_message;
0642
0643 static void is_alive(const char *func, const char *message)
0644 {
0645
0646 if (test_bit(0, &fdc_busy) && command_status < 2 &&
0647 !delayed_work_pending(&fd_timeout)) {
0648 DPRINT("%s: timeout handler died. %s\n", func, message);
0649 }
0650 }
0651
0652 static void (*do_floppy)(void) = NULL;
0653
0654 #define OLOGSIZE 20
0655
0656 static void (*lasthandler)(void);
0657 static unsigned long interruptjiffies;
0658 static unsigned long resultjiffies;
0659 static int resultsize;
0660 static unsigned long lastredo;
0661
0662 static struct output_log {
0663 unsigned char data;
0664 unsigned char status;
0665 unsigned long jiffies;
0666 } output_log[OLOGSIZE];
0667
0668 static int output_log_pos;
0669
0670 #define MAXTIMEOUT -2
0671
0672 static void __reschedule_timeout(int drive, const char *message)
0673 {
0674 unsigned long delay;
0675
0676 if (drive < 0 || drive >= N_DRIVE) {
0677 delay = 20UL * HZ;
0678 drive = 0;
0679 } else
0680 delay = drive_params[drive].timeout;
0681
0682 mod_delayed_work(floppy_wq, &fd_timeout, delay);
0683 if (drive_params[drive].flags & FD_DEBUG)
0684 DPRINT("reschedule timeout %s\n", message);
0685 timeout_message = message;
0686 }
0687
0688 static void reschedule_timeout(int drive, const char *message)
0689 {
0690 unsigned long flags;
0691
0692 spin_lock_irqsave(&floppy_lock, flags);
0693 __reschedule_timeout(drive, message);
0694 spin_unlock_irqrestore(&floppy_lock, flags);
0695 }
0696
0697 #define INFBOUND(a, b) (a) = max_t(int, a, b)
0698 #define SUPBOUND(a, b) (a) = min_t(int, a, b)
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733 static int disk_change(int drive)
0734 {
0735 int fdc = FDC(drive);
0736
0737 if (time_before(jiffies, drive_state[drive].select_date + drive_params[drive].select_delay))
0738 DPRINT("WARNING disk change called early\n");
0739 if (!(fdc_state[fdc].dor & (0x10 << UNIT(drive))) ||
0740 (fdc_state[fdc].dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
0741 DPRINT("probing disk change on unselected drive\n");
0742 DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
0743 (unsigned int)fdc_state[fdc].dor);
0744 }
0745
0746 debug_dcl(drive_params[drive].flags,
0747 "checking disk change line for drive %d\n", drive);
0748 debug_dcl(drive_params[drive].flags, "jiffies=%lu\n", jiffies);
0749 debug_dcl(drive_params[drive].flags, "disk change line=%x\n",
0750 fdc_inb(fdc, FD_DIR) & 0x80);
0751 debug_dcl(drive_params[drive].flags, "flags=%lx\n",
0752 drive_state[drive].flags);
0753
0754 if (drive_params[drive].flags & FD_BROKEN_DCL)
0755 return test_bit(FD_DISK_CHANGED_BIT,
0756 &drive_state[drive].flags);
0757 if ((fdc_inb(fdc, FD_DIR) ^ drive_params[drive].flags) & 0x80) {
0758 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
0759
0760
0761 if (drive_state[drive].maxblock)
0762 set_bit(FD_DISK_CHANGED_BIT,
0763 &drive_state[drive].flags);
0764
0765
0766 if (drive_state[drive].keep_data >= 0) {
0767 if ((drive_params[drive].flags & FTD_MSG) &&
0768 current_type[drive] != NULL)
0769 DPRINT("Disk type is undefined after disk change\n");
0770 current_type[drive] = NULL;
0771 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
0772 }
0773
0774 return 1;
0775 } else {
0776 drive_state[drive].last_checked = jiffies;
0777 clear_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[drive].flags);
0778 }
0779 return 0;
0780 }
0781
0782 static inline int is_selected(int dor, int unit)
0783 {
0784 return ((dor & (0x10 << unit)) && (dor & 3) == unit);
0785 }
0786
0787 static bool is_ready_state(int status)
0788 {
0789 int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
0790 return state == STATUS_READY;
0791 }
0792
0793 static int set_dor(int fdc, char mask, char data)
0794 {
0795 unsigned char unit;
0796 unsigned char drive;
0797 unsigned char newdor;
0798 unsigned char olddor;
0799
0800 if (fdc_state[fdc].address == -1)
0801 return -1;
0802
0803 olddor = fdc_state[fdc].dor;
0804 newdor = (olddor & mask) | data;
0805 if (newdor != olddor) {
0806 unit = olddor & 0x3;
0807 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
0808 drive = REVDRIVE(fdc, unit);
0809 debug_dcl(drive_params[drive].flags,
0810 "calling disk change from set_dor\n");
0811 disk_change(drive);
0812 }
0813 fdc_state[fdc].dor = newdor;
0814 fdc_outb(newdor, fdc, FD_DOR);
0815
0816 unit = newdor & 0x3;
0817 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
0818 drive = REVDRIVE(fdc, unit);
0819 drive_state[drive].select_date = jiffies;
0820 }
0821 }
0822 return olddor;
0823 }
0824
0825 static void twaddle(int fdc, int drive)
0826 {
0827 if (drive_params[drive].select_delay)
0828 return;
0829 fdc_outb(fdc_state[fdc].dor & ~(0x10 << UNIT(drive)),
0830 fdc, FD_DOR);
0831 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR);
0832 drive_state[drive].select_date = jiffies;
0833 }
0834
0835
0836
0837
0838
0839 static void reset_fdc_info(int fdc, int mode)
0840 {
0841 int drive;
0842
0843 fdc_state[fdc].spec1 = fdc_state[fdc].spec2 = -1;
0844 fdc_state[fdc].need_configure = 1;
0845 fdc_state[fdc].perp_mode = 1;
0846 fdc_state[fdc].rawcmd = 0;
0847 for (drive = 0; drive < N_DRIVE; drive++)
0848 if (FDC(drive) == fdc &&
0849 (mode || drive_state[drive].track != NEED_1_RECAL))
0850 drive_state[drive].track = NEED_2_RECAL;
0851 }
0852
0853
0854
0855
0856
0857 static void set_fdc(int drive)
0858 {
0859 unsigned int fdc;
0860
0861 if (drive < 0 || drive >= N_DRIVE) {
0862 pr_info("bad drive value %d\n", drive);
0863 return;
0864 }
0865
0866 fdc = FDC(drive);
0867 if (fdc >= N_FDC) {
0868 pr_info("bad fdc value\n");
0869 return;
0870 }
0871
0872 set_dor(fdc, ~0, 8);
0873 #if N_FDC > 1
0874 set_dor(1 - fdc, ~8, 0);
0875 #endif
0876 if (fdc_state[fdc].rawcmd == 2)
0877 reset_fdc_info(fdc, 1);
0878 if (fdc_inb(fdc, FD_STATUS) != STATUS_READY)
0879 fdc_state[fdc].reset = 1;
0880
0881 current_drive = drive;
0882 current_fdc = fdc;
0883 }
0884
0885
0886
0887
0888
0889 static int lock_fdc(int drive)
0890 {
0891 if (WARN(atomic_read(&usage_count) == 0,
0892 "Trying to lock fdc while usage count=0\n"))
0893 return -1;
0894
0895 if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy)))
0896 return -EINTR;
0897
0898 command_status = FD_COMMAND_NONE;
0899
0900 reschedule_timeout(drive, "lock fdc");
0901 set_fdc(drive);
0902 return 0;
0903 }
0904
0905
0906 static void unlock_fdc(void)
0907 {
0908 if (!test_bit(0, &fdc_busy))
0909 DPRINT("FDC access conflict!\n");
0910
0911 raw_cmd = NULL;
0912 command_status = FD_COMMAND_NONE;
0913 cancel_delayed_work(&fd_timeout);
0914 do_floppy = NULL;
0915 cont = NULL;
0916 clear_bit(0, &fdc_busy);
0917 wake_up(&fdc_wait);
0918 }
0919
0920
0921 static void motor_off_callback(struct timer_list *t)
0922 {
0923 unsigned long nr = t - motor_off_timer;
0924 unsigned char mask = ~(0x10 << UNIT(nr));
0925
0926 if (WARN_ON_ONCE(nr >= N_DRIVE))
0927 return;
0928
0929 set_dor(FDC(nr), mask, 0);
0930 }
0931
0932
0933 static void floppy_off(unsigned int drive)
0934 {
0935 unsigned long volatile delta;
0936 int fdc = FDC(drive);
0937
0938 if (!(fdc_state[fdc].dor & (0x10 << UNIT(drive))))
0939 return;
0940
0941 del_timer(motor_off_timer + drive);
0942
0943
0944
0945 if (drive_params[drive].rps) {
0946 delta = jiffies - drive_state[drive].first_read_date + HZ -
0947 drive_params[drive].spindown_offset;
0948 delta = ((delta * drive_params[drive].rps) % HZ) / drive_params[drive].rps;
0949 motor_off_timer[drive].expires =
0950 jiffies + drive_params[drive].spindown - delta;
0951 }
0952 add_timer(motor_off_timer + drive);
0953 }
0954
0955
0956
0957
0958
0959
0960 static void scandrives(void)
0961 {
0962 int i;
0963 int drive;
0964 int saved_drive;
0965
0966 if (drive_params[current_drive].select_delay)
0967 return;
0968
0969 saved_drive = current_drive;
0970 for (i = 0; i < N_DRIVE; i++) {
0971 drive = (saved_drive + i + 1) % N_DRIVE;
0972 if (drive_state[drive].fd_ref == 0 || drive_params[drive].select_delay != 0)
0973 continue;
0974 set_fdc(drive);
0975 if (!(set_dor(current_fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
0976 (0x10 << UNIT(drive))))
0977
0978
0979 set_dor(current_fdc, ~(0x10 << UNIT(drive)), 0);
0980 }
0981 set_fdc(saved_drive);
0982 }
0983
0984 static void empty(void)
0985 {
0986 }
0987
0988 static void (*floppy_work_fn)(void);
0989
0990 static void floppy_work_workfn(struct work_struct *work)
0991 {
0992 floppy_work_fn();
0993 }
0994
0995 static DECLARE_WORK(floppy_work, floppy_work_workfn);
0996
0997 static void schedule_bh(void (*handler)(void))
0998 {
0999 WARN_ON(work_pending(&floppy_work));
1000
1001 floppy_work_fn = handler;
1002 queue_work(floppy_wq, &floppy_work);
1003 }
1004
1005 static void (*fd_timer_fn)(void) = NULL;
1006
1007 static void fd_timer_workfn(struct work_struct *work)
1008 {
1009 fd_timer_fn();
1010 }
1011
1012 static DECLARE_DELAYED_WORK(fd_timer, fd_timer_workfn);
1013
1014 static void cancel_activity(void)
1015 {
1016 do_floppy = NULL;
1017 cancel_delayed_work(&fd_timer);
1018 cancel_work_sync(&floppy_work);
1019 }
1020
1021
1022
1023 static void fd_watchdog(void)
1024 {
1025 debug_dcl(drive_params[current_drive].flags,
1026 "calling disk change from watchdog\n");
1027
1028 if (disk_change(current_drive)) {
1029 DPRINT("disk removed during i/o\n");
1030 cancel_activity();
1031 cont->done(0);
1032 reset_fdc();
1033 } else {
1034 cancel_delayed_work(&fd_timer);
1035 fd_timer_fn = fd_watchdog;
1036 queue_delayed_work(floppy_wq, &fd_timer, HZ / 10);
1037 }
1038 }
1039
1040 static void main_command_interrupt(void)
1041 {
1042 cancel_delayed_work(&fd_timer);
1043 cont->interrupt();
1044 }
1045
1046
1047 static int fd_wait_for_completion(unsigned long expires,
1048 void (*function)(void))
1049 {
1050 if (fdc_state[current_fdc].reset) {
1051 reset_fdc();
1052
1053
1054 return 1;
1055 }
1056
1057 if (time_before(jiffies, expires)) {
1058 cancel_delayed_work(&fd_timer);
1059 fd_timer_fn = function;
1060 queue_delayed_work(floppy_wq, &fd_timer, expires - jiffies);
1061 return 1;
1062 }
1063 return 0;
1064 }
1065
1066 static void setup_DMA(void)
1067 {
1068 unsigned long f;
1069
1070 if (raw_cmd->length == 0) {
1071 print_hex_dump(KERN_INFO, "zero dma transfer size: ",
1072 DUMP_PREFIX_NONE, 16, 1,
1073 raw_cmd->fullcmd, raw_cmd->cmd_count, false);
1074 cont->done(0);
1075 fdc_state[current_fdc].reset = 1;
1076 return;
1077 }
1078 if (((unsigned long)raw_cmd->kernel_data) % 512) {
1079 pr_info("non aligned address: %p\n", raw_cmd->kernel_data);
1080 cont->done(0);
1081 fdc_state[current_fdc].reset = 1;
1082 return;
1083 }
1084 f = claim_dma_lock();
1085 fd_disable_dma();
1086 #ifdef fd_dma_setup
1087 if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1088 (raw_cmd->flags & FD_RAW_READ) ?
1089 DMA_MODE_READ : DMA_MODE_WRITE,
1090 fdc_state[current_fdc].address) < 0) {
1091 release_dma_lock(f);
1092 cont->done(0);
1093 fdc_state[current_fdc].reset = 1;
1094 return;
1095 }
1096 release_dma_lock(f);
1097 #else
1098 fd_clear_dma_ff();
1099 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1100 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
1101 DMA_MODE_READ : DMA_MODE_WRITE);
1102 fd_set_dma_addr(raw_cmd->kernel_data);
1103 fd_set_dma_count(raw_cmd->length);
1104 virtual_dma_port = fdc_state[current_fdc].address;
1105 fd_enable_dma();
1106 release_dma_lock(f);
1107 #endif
1108 }
1109
1110 static void show_floppy(int fdc);
1111
1112
1113 static int wait_til_ready(int fdc)
1114 {
1115 int status;
1116 int counter;
1117
1118 if (fdc_state[fdc].reset)
1119 return -1;
1120 for (counter = 0; counter < 10000; counter++) {
1121 status = fdc_inb(fdc, FD_STATUS);
1122 if (status & STATUS_READY)
1123 return status;
1124 }
1125 if (initialized) {
1126 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
1127 show_floppy(fdc);
1128 }
1129 fdc_state[fdc].reset = 1;
1130 return -1;
1131 }
1132
1133
1134 static int output_byte(int fdc, char byte)
1135 {
1136 int status = wait_til_ready(fdc);
1137
1138 if (status < 0)
1139 return -1;
1140
1141 if (is_ready_state(status)) {
1142 fdc_outb(byte, fdc, FD_DATA);
1143 output_log[output_log_pos].data = byte;
1144 output_log[output_log_pos].status = status;
1145 output_log[output_log_pos].jiffies = jiffies;
1146 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1147 return 0;
1148 }
1149 fdc_state[fdc].reset = 1;
1150 if (initialized) {
1151 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1152 byte, fdc, status);
1153 show_floppy(fdc);
1154 }
1155 return -1;
1156 }
1157
1158
1159 static int result(int fdc)
1160 {
1161 int i;
1162 int status = 0;
1163
1164 for (i = 0; i < FD_RAW_REPLY_SIZE; i++) {
1165 status = wait_til_ready(fdc);
1166 if (status < 0)
1167 break;
1168 status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
1169 if ((status & ~STATUS_BUSY) == STATUS_READY) {
1170 resultjiffies = jiffies;
1171 resultsize = i;
1172 return i;
1173 }
1174 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
1175 reply_buffer[i] = fdc_inb(fdc, FD_DATA);
1176 else
1177 break;
1178 }
1179 if (initialized) {
1180 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1181 fdc, status, i);
1182 show_floppy(fdc);
1183 }
1184 fdc_state[fdc].reset = 1;
1185 return -1;
1186 }
1187
1188 #define MORE_OUTPUT -2
1189
1190 static int need_more_output(int fdc)
1191 {
1192 int status = wait_til_ready(fdc);
1193
1194 if (status < 0)
1195 return -1;
1196
1197 if (is_ready_state(status))
1198 return MORE_OUTPUT;
1199
1200 return result(fdc);
1201 }
1202
1203
1204
1205
1206 static void perpendicular_mode(int fdc)
1207 {
1208 unsigned char perp_mode;
1209
1210 if (raw_cmd->rate & 0x40) {
1211 switch (raw_cmd->rate & 3) {
1212 case 0:
1213 perp_mode = 2;
1214 break;
1215 case 3:
1216 perp_mode = 3;
1217 break;
1218 default:
1219 DPRINT("Invalid data rate for perpendicular mode!\n");
1220 cont->done(0);
1221 fdc_state[fdc].reset = 1;
1222
1223
1224
1225
1226
1227 return;
1228 }
1229 } else
1230 perp_mode = 0;
1231
1232 if (fdc_state[fdc].perp_mode == perp_mode)
1233 return;
1234 if (fdc_state[fdc].version >= FDC_82077_ORIG) {
1235 output_byte(fdc, FD_PERPENDICULAR);
1236 output_byte(fdc, perp_mode);
1237 fdc_state[fdc].perp_mode = perp_mode;
1238 } else if (perp_mode) {
1239 DPRINT("perpendicular mode not supported by this FDC.\n");
1240 }
1241 }
1242
1243 static int fifo_depth = 0xa;
1244 static int no_fifo;
1245
1246 static int fdc_configure(int fdc)
1247 {
1248
1249 output_byte(fdc, FD_CONFIGURE);
1250 if (need_more_output(fdc) != MORE_OUTPUT)
1251 return 0;
1252 output_byte(fdc, 0);
1253 output_byte(fdc, 0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1254 output_byte(fdc, 0);
1255 return 1;
1256 }
1257
1258 #define NOMINAL_DTR 500
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279 static void fdc_specify(int fdc, int drive)
1280 {
1281 unsigned char spec1;
1282 unsigned char spec2;
1283 unsigned long srt;
1284 unsigned long hlt;
1285 unsigned long hut;
1286 unsigned long dtr = NOMINAL_DTR;
1287 unsigned long scale_dtr = NOMINAL_DTR;
1288 int hlt_max_code = 0x7f;
1289 int hut_max_code = 0xf;
1290
1291 if (fdc_state[fdc].need_configure &&
1292 fdc_state[fdc].version >= FDC_82072A) {
1293 fdc_configure(fdc);
1294 fdc_state[fdc].need_configure = 0;
1295 }
1296
1297 switch (raw_cmd->rate & 0x03) {
1298 case 3:
1299 dtr = 1000;
1300 break;
1301 case 1:
1302 dtr = 300;
1303 if (fdc_state[fdc].version >= FDC_82078) {
1304
1305
1306 output_byte(fdc, FD_DRIVESPEC);
1307 if (need_more_output(fdc) == MORE_OUTPUT) {
1308 output_byte(fdc, UNIT(drive));
1309 output_byte(fdc, 0xc0);
1310 }
1311 }
1312 break;
1313 case 2:
1314 dtr = 250;
1315 break;
1316 }
1317
1318 if (fdc_state[fdc].version >= FDC_82072) {
1319 scale_dtr = dtr;
1320 hlt_max_code = 0x00;
1321 hut_max_code = 0x0;
1322 }
1323
1324
1325 srt = 16 - DIV_ROUND_UP(drive_params[drive].srt * scale_dtr / 1000,
1326 NOMINAL_DTR);
1327 if (slow_floppy)
1328 srt = srt / 4;
1329
1330 SUPBOUND(srt, 0xf);
1331 INFBOUND(srt, 0);
1332
1333 hlt = DIV_ROUND_UP(drive_params[drive].hlt * scale_dtr / 2,
1334 NOMINAL_DTR);
1335 if (hlt < 0x01)
1336 hlt = 0x01;
1337 else if (hlt > 0x7f)
1338 hlt = hlt_max_code;
1339
1340 hut = DIV_ROUND_UP(drive_params[drive].hut * scale_dtr / 16,
1341 NOMINAL_DTR);
1342 if (hut < 0x1)
1343 hut = 0x1;
1344 else if (hut > 0xf)
1345 hut = hut_max_code;
1346
1347 spec1 = (srt << 4) | hut;
1348 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1349
1350
1351 if (fdc_state[fdc].spec1 != spec1 ||
1352 fdc_state[fdc].spec2 != spec2) {
1353
1354 output_byte(fdc, FD_SPECIFY);
1355 output_byte(fdc, fdc_state[fdc].spec1 = spec1);
1356 output_byte(fdc, fdc_state[fdc].spec2 = spec2);
1357 }
1358 }
1359
1360
1361
1362
1363
1364 static int fdc_dtr(void)
1365 {
1366
1367 if ((raw_cmd->rate & 3) == fdc_state[current_fdc].dtr)
1368 return 0;
1369
1370
1371 fdc_outb(raw_cmd->rate & 3, current_fdc, FD_DCR);
1372
1373
1374
1375
1376
1377
1378 fdc_state[current_fdc].dtr = raw_cmd->rate & 3;
1379 return fd_wait_for_completion(jiffies + 2UL * HZ / 100, floppy_ready);
1380 }
1381
1382 static void tell_sector(void)
1383 {
1384 pr_cont(": track %d, head %d, sector %d, size %d",
1385 reply_buffer[R_TRACK], reply_buffer[R_HEAD],
1386 reply_buffer[R_SECTOR],
1387 reply_buffer[R_SIZECODE]);
1388 }
1389
1390 static void print_errors(void)
1391 {
1392 DPRINT("");
1393 if (reply_buffer[ST0] & ST0_ECE) {
1394 pr_cont("Recalibrate failed!");
1395 } else if (reply_buffer[ST2] & ST2_CRC) {
1396 pr_cont("data CRC error");
1397 tell_sector();
1398 } else if (reply_buffer[ST1] & ST1_CRC) {
1399 pr_cont("CRC error");
1400 tell_sector();
1401 } else if ((reply_buffer[ST1] & (ST1_MAM | ST1_ND)) ||
1402 (reply_buffer[ST2] & ST2_MAM)) {
1403 if (!probing) {
1404 pr_cont("sector not found");
1405 tell_sector();
1406 } else
1407 pr_cont("probe failed...");
1408 } else if (reply_buffer[ST2] & ST2_WC) {
1409 pr_cont("wrong cylinder");
1410 } else if (reply_buffer[ST2] & ST2_BC) {
1411 pr_cont("bad cylinder");
1412 } else {
1413 pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1414 reply_buffer[ST0], reply_buffer[ST1],
1415 reply_buffer[ST2]);
1416 tell_sector();
1417 }
1418 pr_cont("\n");
1419 }
1420
1421
1422
1423
1424
1425
1426
1427
1428 static int interpret_errors(void)
1429 {
1430 char bad;
1431
1432 if (inr != 7) {
1433 DPRINT("-- FDC reply error\n");
1434 fdc_state[current_fdc].reset = 1;
1435 return 1;
1436 }
1437
1438
1439 switch (reply_buffer[ST0] & ST0_INTR) {
1440 case 0x40:
1441 if (reply_buffer[ST1] & ST1_EOC)
1442 return 0;
1443 bad = 1;
1444 if (reply_buffer[ST1] & ST1_WP) {
1445 DPRINT("Drive is write protected\n");
1446 clear_bit(FD_DISK_WRITABLE_BIT,
1447 &drive_state[current_drive].flags);
1448 cont->done(0);
1449 bad = 2;
1450 } else if (reply_buffer[ST1] & ST1_ND) {
1451 set_bit(FD_NEED_TWADDLE_BIT,
1452 &drive_state[current_drive].flags);
1453 } else if (reply_buffer[ST1] & ST1_OR) {
1454 if (drive_params[current_drive].flags & FTD_MSG)
1455 DPRINT("Over/Underrun - retrying\n");
1456 bad = 0;
1457 } else if (floppy_errors >= drive_params[current_drive].max_errors.reporting) {
1458 print_errors();
1459 }
1460 if (reply_buffer[ST2] & ST2_WC || reply_buffer[ST2] & ST2_BC)
1461
1462 drive_state[current_drive].track = NEED_2_RECAL;
1463 return bad;
1464 case 0x80:
1465 DPRINT("Invalid FDC command given!\n");
1466 cont->done(0);
1467 return 2;
1468 case 0xc0:
1469 DPRINT("Abnormal termination caused by polling\n");
1470 cont->error();
1471 return 2;
1472 default:
1473 return 0;
1474 }
1475 }
1476
1477
1478
1479
1480
1481
1482 static void setup_rw_floppy(void)
1483 {
1484 int i;
1485 int r;
1486 int flags;
1487 unsigned long ready_date;
1488 void (*function)(void);
1489
1490 flags = raw_cmd->flags;
1491 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1492 flags |= FD_RAW_INTR;
1493
1494 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
1495 ready_date = drive_state[current_drive].spinup_date + drive_params[current_drive].spinup;
1496
1497
1498
1499
1500 if (time_after(ready_date, jiffies + drive_params[current_drive].select_delay)) {
1501 ready_date -= drive_params[current_drive].select_delay;
1502 function = floppy_start;
1503 } else
1504 function = setup_rw_floppy;
1505
1506
1507 if (fd_wait_for_completion(ready_date, function))
1508 return;
1509 }
1510 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1511 setup_DMA();
1512
1513 if (flags & FD_RAW_INTR)
1514 do_floppy = main_command_interrupt;
1515
1516 r = 0;
1517 for (i = 0; i < raw_cmd->cmd_count; i++)
1518 r |= output_byte(current_fdc, raw_cmd->fullcmd[i]);
1519
1520 debugt(__func__, "rw_command");
1521
1522 if (r) {
1523 cont->error();
1524 reset_fdc();
1525 return;
1526 }
1527
1528 if (!(flags & FD_RAW_INTR)) {
1529 inr = result(current_fdc);
1530 cont->interrupt();
1531 } else if (flags & FD_RAW_NEED_DISK)
1532 fd_watchdog();
1533 }
1534
1535 static int blind_seek;
1536
1537
1538
1539
1540
1541 static void seek_interrupt(void)
1542 {
1543 debugt(__func__, "");
1544 if (inr != 2 || (reply_buffer[ST0] & 0xF8) != 0x20) {
1545 DPRINT("seek failed\n");
1546 drive_state[current_drive].track = NEED_2_RECAL;
1547 cont->error();
1548 cont->redo();
1549 return;
1550 }
1551 if (drive_state[current_drive].track >= 0 &&
1552 drive_state[current_drive].track != reply_buffer[ST1] &&
1553 !blind_seek) {
1554 debug_dcl(drive_params[current_drive].flags,
1555 "clearing NEWCHANGE flag because of effective seek\n");
1556 debug_dcl(drive_params[current_drive].flags, "jiffies=%lu\n",
1557 jiffies);
1558 clear_bit(FD_DISK_NEWCHANGE_BIT,
1559 &drive_state[current_drive].flags);
1560
1561 drive_state[current_drive].select_date = jiffies;
1562 }
1563 drive_state[current_drive].track = reply_buffer[ST1];
1564 floppy_ready();
1565 }
1566
1567 static void check_wp(int fdc, int drive)
1568 {
1569 if (test_bit(FD_VERIFY_BIT, &drive_state[drive].flags)) {
1570
1571 output_byte(fdc, FD_GETSTATUS);
1572 output_byte(fdc, UNIT(drive));
1573 if (result(fdc) != 1) {
1574 fdc_state[fdc].reset = 1;
1575 return;
1576 }
1577 clear_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
1578 clear_bit(FD_NEED_TWADDLE_BIT,
1579 &drive_state[drive].flags);
1580 debug_dcl(drive_params[drive].flags,
1581 "checking whether disk is write protected\n");
1582 debug_dcl(drive_params[drive].flags, "wp=%x\n",
1583 reply_buffer[ST3] & 0x40);
1584 if (!(reply_buffer[ST3] & 0x40))
1585 set_bit(FD_DISK_WRITABLE_BIT,
1586 &drive_state[drive].flags);
1587 else
1588 clear_bit(FD_DISK_WRITABLE_BIT,
1589 &drive_state[drive].flags);
1590 }
1591 }
1592
1593 static void seek_floppy(void)
1594 {
1595 int track;
1596
1597 blind_seek = 0;
1598
1599 debug_dcl(drive_params[current_drive].flags,
1600 "calling disk change from %s\n", __func__);
1601
1602 if (!test_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags) &&
1603 disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
1604
1605
1606
1607
1608 set_bit(FD_DISK_CHANGED_BIT,
1609 &drive_state[current_drive].flags);
1610 cont->done(0);
1611 cont->redo();
1612 return;
1613 }
1614 if (drive_state[current_drive].track <= NEED_1_RECAL) {
1615 recalibrate_floppy();
1616 return;
1617 } else if (test_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags) &&
1618 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1619 (drive_state[current_drive].track <= NO_TRACK || drive_state[current_drive].track == raw_cmd->track)) {
1620
1621
1622 if (raw_cmd->track)
1623 track = raw_cmd->track - 1;
1624 else {
1625 if (drive_params[current_drive].flags & FD_SILENT_DCL_CLEAR) {
1626 set_dor(current_fdc, ~(0x10 << UNIT(current_drive)), 0);
1627 blind_seek = 1;
1628 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1629 }
1630 track = 1;
1631 }
1632 } else {
1633 check_wp(current_fdc, current_drive);
1634 if (raw_cmd->track != drive_state[current_drive].track &&
1635 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1636 track = raw_cmd->track;
1637 else {
1638 setup_rw_floppy();
1639 return;
1640 }
1641 }
1642
1643 do_floppy = seek_interrupt;
1644 output_byte(current_fdc, FD_SEEK);
1645 output_byte(current_fdc, UNIT(current_drive));
1646 if (output_byte(current_fdc, track) < 0) {
1647 reset_fdc();
1648 return;
1649 }
1650 debugt(__func__, "");
1651 }
1652
1653 static void recal_interrupt(void)
1654 {
1655 debugt(__func__, "");
1656 if (inr != 2)
1657 fdc_state[current_fdc].reset = 1;
1658 else if (reply_buffer[ST0] & ST0_ECE) {
1659 switch (drive_state[current_drive].track) {
1660 case NEED_1_RECAL:
1661 debugt(__func__, "need 1 recal");
1662
1663
1664
1665
1666 cont->error();
1667 cont->redo();
1668 return;
1669 case NEED_2_RECAL:
1670 debugt(__func__, "need 2 recal");
1671
1672
1673
1674
1675
1676
1677 debug_dcl(drive_params[current_drive].flags,
1678 "clearing NEWCHANGE flag because of second recalibrate\n");
1679
1680 clear_bit(FD_DISK_NEWCHANGE_BIT,
1681 &drive_state[current_drive].flags);
1682 drive_state[current_drive].select_date = jiffies;
1683 fallthrough;
1684 default:
1685 debugt(__func__, "default");
1686
1687
1688
1689
1690
1691
1692 drive_state[current_drive].track = NEED_1_RECAL;
1693 break;
1694 }
1695 } else
1696 drive_state[current_drive].track = reply_buffer[ST1];
1697 floppy_ready();
1698 }
1699
1700 static void print_result(char *message, int inr)
1701 {
1702 int i;
1703
1704 DPRINT("%s ", message);
1705 if (inr >= 0)
1706 for (i = 0; i < inr; i++)
1707 pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
1708 pr_cont("\n");
1709 }
1710
1711
1712 irqreturn_t floppy_interrupt(int irq, void *dev_id)
1713 {
1714 int do_print;
1715 unsigned long f;
1716 void (*handler)(void) = do_floppy;
1717
1718 lasthandler = handler;
1719 interruptjiffies = jiffies;
1720
1721 f = claim_dma_lock();
1722 fd_disable_dma();
1723 release_dma_lock(f);
1724
1725 do_floppy = NULL;
1726 if (current_fdc >= N_FDC || fdc_state[current_fdc].address == -1) {
1727
1728 pr_info("DOR0=%x\n", fdc_state[0].dor);
1729 pr_info("floppy interrupt on bizarre fdc %d\n", current_fdc);
1730 pr_info("handler=%ps\n", handler);
1731 is_alive(__func__, "bizarre fdc");
1732 return IRQ_NONE;
1733 }
1734
1735 fdc_state[current_fdc].reset = 0;
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745 do_print = !handler && print_unex && initialized;
1746
1747 inr = result(current_fdc);
1748 if (do_print)
1749 print_result("unexpected interrupt", inr);
1750 if (inr == 0) {
1751 int max_sensei = 4;
1752 do {
1753 output_byte(current_fdc, FD_SENSEI);
1754 inr = result(current_fdc);
1755 if (do_print)
1756 print_result("sensei", inr);
1757 max_sensei--;
1758 } while ((reply_buffer[ST0] & 0x83) != UNIT(current_drive) &&
1759 inr == 2 && max_sensei);
1760 }
1761 if (!handler) {
1762 fdc_state[current_fdc].reset = 1;
1763 return IRQ_NONE;
1764 }
1765 schedule_bh(handler);
1766 is_alive(__func__, "normal interrupt end");
1767
1768
1769 return IRQ_HANDLED;
1770 }
1771
1772 static void recalibrate_floppy(void)
1773 {
1774 debugt(__func__, "");
1775 do_floppy = recal_interrupt;
1776 output_byte(current_fdc, FD_RECALIBRATE);
1777 if (output_byte(current_fdc, UNIT(current_drive)) < 0)
1778 reset_fdc();
1779 }
1780
1781
1782
1783
1784 static void reset_interrupt(void)
1785 {
1786 debugt(__func__, "");
1787 result(current_fdc);
1788 if (fdc_state[current_fdc].reset) {
1789 pr_info("reset set in interrupt, calling %ps\n", cont->error);
1790 cont->error();
1791 }
1792 cont->redo();
1793 }
1794
1795
1796
1797
1798
1799
1800
1801 static void reset_fdc(void)
1802 {
1803 unsigned long flags;
1804
1805 do_floppy = reset_interrupt;
1806 fdc_state[current_fdc].reset = 0;
1807 reset_fdc_info(current_fdc, 0);
1808
1809
1810
1811
1812 flags = claim_dma_lock();
1813 fd_disable_dma();
1814 release_dma_lock(flags);
1815
1816 if (fdc_state[current_fdc].version >= FDC_82072A)
1817 fdc_outb(0x80 | (fdc_state[current_fdc].dtr & 3),
1818 current_fdc, FD_STATUS);
1819 else {
1820 fdc_outb(fdc_state[current_fdc].dor & ~0x04, current_fdc, FD_DOR);
1821 udelay(FD_RESET_DELAY);
1822 fdc_outb(fdc_state[current_fdc].dor, current_fdc, FD_DOR);
1823 }
1824 }
1825
1826 static void show_floppy(int fdc)
1827 {
1828 int i;
1829
1830 pr_info("\n");
1831 pr_info("floppy driver state\n");
1832 pr_info("-------------------\n");
1833 pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%ps\n",
1834 jiffies, interruptjiffies, jiffies - interruptjiffies,
1835 lasthandler);
1836
1837 pr_info("timeout_message=%s\n", timeout_message);
1838 pr_info("last output bytes:\n");
1839 for (i = 0; i < OLOGSIZE; i++)
1840 pr_info("%2x %2x %lu\n",
1841 output_log[(i + output_log_pos) % OLOGSIZE].data,
1842 output_log[(i + output_log_pos) % OLOGSIZE].status,
1843 output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
1844 pr_info("last result at %lu\n", resultjiffies);
1845 pr_info("last redo_fd_request at %lu\n", lastredo);
1846 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
1847 reply_buffer, resultsize, true);
1848
1849 pr_info("status=%x\n", fdc_inb(fdc, FD_STATUS));
1850 pr_info("fdc_busy=%lu\n", fdc_busy);
1851 if (do_floppy)
1852 pr_info("do_floppy=%ps\n", do_floppy);
1853 if (work_pending(&floppy_work))
1854 pr_info("floppy_work.func=%ps\n", floppy_work.func);
1855 if (delayed_work_pending(&fd_timer))
1856 pr_info("delayed work.function=%p expires=%ld\n",
1857 fd_timer.work.func,
1858 fd_timer.timer.expires - jiffies);
1859 if (delayed_work_pending(&fd_timeout))
1860 pr_info("timer_function=%p expires=%ld\n",
1861 fd_timeout.work.func,
1862 fd_timeout.timer.expires - jiffies);
1863
1864 pr_info("cont=%p\n", cont);
1865 pr_info("current_req=%p\n", current_req);
1866 pr_info("command_status=%d\n", command_status);
1867 pr_info("\n");
1868 }
1869
1870 static void floppy_shutdown(struct work_struct *arg)
1871 {
1872 unsigned long flags;
1873
1874 if (initialized)
1875 show_floppy(current_fdc);
1876 cancel_activity();
1877
1878 flags = claim_dma_lock();
1879 fd_disable_dma();
1880 release_dma_lock(flags);
1881
1882
1883
1884 if (initialized)
1885 DPRINT("floppy timeout called\n");
1886 fdc_state[current_fdc].reset = 1;
1887 if (cont) {
1888 cont->done(0);
1889 cont->redo();
1890 } else {
1891 pr_info("no cont in shutdown!\n");
1892 process_fd_request();
1893 }
1894 is_alive(__func__, "");
1895 }
1896
1897
1898 static int start_motor(void (*function)(void))
1899 {
1900 int mask;
1901 int data;
1902
1903 mask = 0xfc;
1904 data = UNIT(current_drive);
1905 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
1906 if (!(fdc_state[current_fdc].dor & (0x10 << UNIT(current_drive)))) {
1907 set_debugt();
1908
1909 drive_state[current_drive].first_read_date = 0;
1910
1911 drive_state[current_drive].spinup_date = jiffies;
1912 data |= (0x10 << UNIT(current_drive));
1913 }
1914 } else if (fdc_state[current_fdc].dor & (0x10 << UNIT(current_drive)))
1915 mask &= ~(0x10 << UNIT(current_drive));
1916
1917
1918 del_timer(motor_off_timer + current_drive);
1919 set_dor(current_fdc, mask, data);
1920
1921
1922 return fd_wait_for_completion(drive_state[current_drive].select_date + drive_params[current_drive].select_delay,
1923 function);
1924 }
1925
1926 static void floppy_ready(void)
1927 {
1928 if (fdc_state[current_fdc].reset) {
1929 reset_fdc();
1930 return;
1931 }
1932 if (start_motor(floppy_ready))
1933 return;
1934 if (fdc_dtr())
1935 return;
1936
1937 debug_dcl(drive_params[current_drive].flags,
1938 "calling disk change from floppy_ready\n");
1939 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1940 disk_change(current_drive) && !drive_params[current_drive].select_delay)
1941 twaddle(current_fdc, current_drive);
1942
1943
1944 #ifdef fd_chose_dma_mode
1945 if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
1946 unsigned long flags = claim_dma_lock();
1947 fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
1948 release_dma_lock(flags);
1949 }
1950 #endif
1951
1952 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
1953 perpendicular_mode(current_fdc);
1954 fdc_specify(current_fdc, current_drive);
1955 seek_floppy();
1956 } else {
1957 if ((raw_cmd->flags & FD_RAW_READ) ||
1958 (raw_cmd->flags & FD_RAW_WRITE))
1959 fdc_specify(current_fdc, current_drive);
1960 setup_rw_floppy();
1961 }
1962 }
1963
1964 static void floppy_start(void)
1965 {
1966 reschedule_timeout(current_drive, "floppy start");
1967
1968 scandrives();
1969 debug_dcl(drive_params[current_drive].flags,
1970 "setting NEWCHANGE in floppy_start\n");
1971 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags);
1972 floppy_ready();
1973 }
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989 static void do_wakeup(void)
1990 {
1991 reschedule_timeout(MAXTIMEOUT, "do wakeup");
1992 cont = NULL;
1993 command_status += 2;
1994 wake_up(&command_done);
1995 }
1996
1997 static const struct cont_t wakeup_cont = {
1998 .interrupt = empty,
1999 .redo = do_wakeup,
2000 .error = empty,
2001 .done = (done_f)empty
2002 };
2003
2004 static const struct cont_t intr_cont = {
2005 .interrupt = empty,
2006 .redo = process_fd_request,
2007 .error = empty,
2008 .done = (done_f)empty
2009 };
2010
2011
2012
2013
2014 static int wait_til_done(void (*handler)(void), bool interruptible)
2015 {
2016 int ret;
2017
2018 schedule_bh(handler);
2019
2020 if (interruptible)
2021 wait_event_interruptible(command_done, command_status >= 2);
2022 else
2023 wait_event(command_done, command_status >= 2);
2024
2025 if (command_status < 2) {
2026 cancel_activity();
2027 cont = &intr_cont;
2028 reset_fdc();
2029 return -EINTR;
2030 }
2031
2032 if (fdc_state[current_fdc].reset)
2033 command_status = FD_COMMAND_ERROR;
2034 if (command_status == FD_COMMAND_OKAY)
2035 ret = 0;
2036 else
2037 ret = -EIO;
2038 command_status = FD_COMMAND_NONE;
2039 return ret;
2040 }
2041
2042 static void generic_done(int result)
2043 {
2044 command_status = result;
2045 cont = &wakeup_cont;
2046 }
2047
2048 static void generic_success(void)
2049 {
2050 cont->done(1);
2051 }
2052
2053 static void generic_failure(void)
2054 {
2055 cont->done(0);
2056 }
2057
2058 static void success_and_wakeup(void)
2059 {
2060 generic_success();
2061 cont->redo();
2062 }
2063
2064
2065
2066
2067
2068
2069 static int next_valid_format(int drive)
2070 {
2071 int probed_format;
2072
2073 probed_format = drive_state[drive].probed_format;
2074 while (1) {
2075 if (probed_format >= FD_AUTODETECT_SIZE ||
2076 !drive_params[drive].autodetect[probed_format]) {
2077 drive_state[drive].probed_format = 0;
2078 return 1;
2079 }
2080 if (floppy_type[drive_params[drive].autodetect[probed_format]].sect) {
2081 drive_state[drive].probed_format = probed_format;
2082 return 0;
2083 }
2084 probed_format++;
2085 }
2086 }
2087
2088 static void bad_flp_intr(void)
2089 {
2090 int err_count;
2091
2092 if (probing) {
2093 drive_state[current_drive].probed_format++;
2094 if (!next_valid_format(current_drive))
2095 return;
2096 }
2097 err_count = ++floppy_errors;
2098 INFBOUND(write_errors[current_drive].badness, err_count);
2099 if (err_count > drive_params[current_drive].max_errors.abort)
2100 cont->done(0);
2101 if (err_count > drive_params[current_drive].max_errors.reset)
2102 fdc_state[current_fdc].reset = 1;
2103 else if (err_count > drive_params[current_drive].max_errors.recal)
2104 drive_state[current_drive].track = NEED_2_RECAL;
2105 }
2106
2107 static void set_floppy(int drive)
2108 {
2109 int type = ITYPE(drive_state[drive].fd_device);
2110
2111 if (type)
2112 _floppy = floppy_type + type;
2113 else
2114 _floppy = current_type[drive];
2115 }
2116
2117
2118
2119
2120
2121 static void format_interrupt(void)
2122 {
2123 switch (interpret_errors()) {
2124 case 1:
2125 cont->error();
2126 break;
2127 case 2:
2128 break;
2129 case 0:
2130 cont->done(1);
2131 }
2132 cont->redo();
2133 }
2134
2135 #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
2136 #define CT(x) ((x) | 0xc0)
2137
2138 static void setup_format_params(int track)
2139 {
2140 int n;
2141 int il;
2142 int count;
2143 int head_shift;
2144 int track_shift;
2145 struct fparm {
2146 unsigned char track, head, sect, size;
2147 } *here = (struct fparm *)floppy_track_buffer;
2148
2149 raw_cmd = &default_raw_cmd;
2150 raw_cmd->track = track;
2151
2152 raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2153 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK);
2154 raw_cmd->rate = _floppy->rate & 0x43;
2155 raw_cmd->cmd_count = NR_F;
2156 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_FORMAT);
2157 raw_cmd->cmd[DR_SELECT] = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
2158 raw_cmd->cmd[F_SIZECODE] = FD_SIZECODE(_floppy);
2159 raw_cmd->cmd[F_SECT_PER_TRACK] = _floppy->sect << 2 >> raw_cmd->cmd[F_SIZECODE];
2160 raw_cmd->cmd[F_GAP] = _floppy->fmt_gap;
2161 raw_cmd->cmd[F_FILL] = FD_FILL_BYTE;
2162
2163 raw_cmd->kernel_data = floppy_track_buffer;
2164 raw_cmd->length = 4 * raw_cmd->cmd[F_SECT_PER_TRACK];
2165
2166 if (!raw_cmd->cmd[F_SECT_PER_TRACK])
2167 return;
2168
2169
2170 head_shift = (raw_cmd->cmd[F_SECT_PER_TRACK] + 5) / 6;
2171
2172
2173 track_shift = 2 * head_shift + 3;
2174
2175
2176 n = (track_shift * format_req.track + head_shift * format_req.head)
2177 % raw_cmd->cmd[F_SECT_PER_TRACK];
2178
2179
2180 il = 1;
2181 if (_floppy->fmt_gap < 0x22)
2182 il++;
2183
2184
2185 for (count = 0; count < raw_cmd->cmd[F_SECT_PER_TRACK]; ++count) {
2186 here[count].track = format_req.track;
2187 here[count].head = format_req.head;
2188 here[count].sect = 0;
2189 here[count].size = raw_cmd->cmd[F_SIZECODE];
2190 }
2191
2192 for (count = 1; count <= raw_cmd->cmd[F_SECT_PER_TRACK]; ++count) {
2193 here[n].sect = count;
2194 n = (n + il) % raw_cmd->cmd[F_SECT_PER_TRACK];
2195 if (here[n].sect) {
2196 ++n;
2197 if (n >= raw_cmd->cmd[F_SECT_PER_TRACK]) {
2198 n -= raw_cmd->cmd[F_SECT_PER_TRACK];
2199 while (here[n].sect)
2200 ++n;
2201 }
2202 }
2203 }
2204 if (_floppy->stretch & FD_SECTBASEMASK) {
2205 for (count = 0; count < raw_cmd->cmd[F_SECT_PER_TRACK]; count++)
2206 here[count].sect += FD_SECTBASE(_floppy) - 1;
2207 }
2208 }
2209
2210 static void redo_format(void)
2211 {
2212 buffer_track = -1;
2213 setup_format_params(format_req.track << STRETCH(_floppy));
2214 floppy_start();
2215 debugt(__func__, "queue format request");
2216 }
2217
2218 static const struct cont_t format_cont = {
2219 .interrupt = format_interrupt,
2220 .redo = redo_format,
2221 .error = bad_flp_intr,
2222 .done = generic_done
2223 };
2224
2225 static int do_format(int drive, struct format_descr *tmp_format_req)
2226 {
2227 int ret;
2228
2229 if (lock_fdc(drive))
2230 return -EINTR;
2231
2232 set_floppy(drive);
2233 if (!_floppy ||
2234 _floppy->track > drive_params[current_drive].tracks ||
2235 tmp_format_req->track >= _floppy->track ||
2236 tmp_format_req->head >= _floppy->head ||
2237 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2238 !_floppy->fmt_gap) {
2239 process_fd_request();
2240 return -EINVAL;
2241 }
2242 format_req = *tmp_format_req;
2243 cont = &format_cont;
2244 floppy_errors = 0;
2245 ret = wait_til_done(redo_format, true);
2246 if (ret == -EINTR)
2247 return -EINTR;
2248 process_fd_request();
2249 return ret;
2250 }
2251
2252
2253
2254
2255
2256
2257 static void floppy_end_request(struct request *req, blk_status_t error)
2258 {
2259 unsigned int nr_sectors = current_count_sectors;
2260 unsigned int drive = (unsigned long)req->q->disk->private_data;
2261
2262
2263 if (error)
2264 nr_sectors = blk_rq_cur_sectors(req);
2265 if (blk_update_request(req, error, nr_sectors << 9))
2266 return;
2267 __blk_mq_end_request(req, error);
2268
2269
2270 floppy_off(drive);
2271 current_req = NULL;
2272 }
2273
2274
2275
2276 static void request_done(int uptodate)
2277 {
2278 struct request *req = current_req;
2279 int block;
2280 char msg[sizeof("request done ") + sizeof(int) * 3];
2281
2282 probing = 0;
2283 snprintf(msg, sizeof(msg), "request done %d", uptodate);
2284 reschedule_timeout(MAXTIMEOUT, msg);
2285
2286 if (!req) {
2287 pr_info("floppy.c: no request in request_done\n");
2288 return;
2289 }
2290
2291 if (uptodate) {
2292
2293
2294 block = current_count_sectors + blk_rq_pos(req);
2295 INFBOUND(drive_state[current_drive].maxblock, block);
2296 if (block > _floppy->sect)
2297 drive_state[current_drive].maxtrack = 1;
2298
2299 floppy_end_request(req, 0);
2300 } else {
2301 if (rq_data_dir(req) == WRITE) {
2302
2303 write_errors[current_drive].write_errors++;
2304 if (write_errors[current_drive].write_errors == 1) {
2305 write_errors[current_drive].first_error_sector = blk_rq_pos(req);
2306 write_errors[current_drive].first_error_generation = drive_state[current_drive].generation;
2307 }
2308 write_errors[current_drive].last_error_sector = blk_rq_pos(req);
2309 write_errors[current_drive].last_error_generation = drive_state[current_drive].generation;
2310 }
2311 floppy_end_request(req, BLK_STS_IOERR);
2312 }
2313 }
2314
2315
2316 static void rw_interrupt(void)
2317 {
2318 int eoc;
2319 int ssize;
2320 int heads;
2321 int nr_sectors;
2322
2323 if (reply_buffer[R_HEAD] >= 2) {
2324
2325
2326
2327 return;
2328 }
2329
2330 if (!drive_state[current_drive].first_read_date)
2331 drive_state[current_drive].first_read_date = jiffies;
2332
2333 ssize = DIV_ROUND_UP(1 << raw_cmd->cmd[SIZECODE], 4);
2334
2335 if (reply_buffer[ST1] & ST1_EOC)
2336 eoc = 1;
2337 else
2338 eoc = 0;
2339
2340 if (raw_cmd->cmd[COMMAND] & 0x80)
2341 heads = 2;
2342 else
2343 heads = 1;
2344
2345 nr_sectors = (((reply_buffer[R_TRACK] - raw_cmd->cmd[TRACK]) * heads +
2346 reply_buffer[R_HEAD] - raw_cmd->cmd[HEAD]) * raw_cmd->cmd[SECT_PER_TRACK] +
2347 reply_buffer[R_SECTOR] - raw_cmd->cmd[SECTOR] + eoc) << raw_cmd->cmd[SIZECODE] >> 2;
2348
2349 if (nr_sectors / ssize >
2350 DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
2351 DPRINT("long rw: %x instead of %lx\n",
2352 nr_sectors, current_count_sectors);
2353 pr_info("rs=%d s=%d\n", reply_buffer[R_SECTOR],
2354 raw_cmd->cmd[SECTOR]);
2355 pr_info("rh=%d h=%d\n", reply_buffer[R_HEAD],
2356 raw_cmd->cmd[HEAD]);
2357 pr_info("rt=%d t=%d\n", reply_buffer[R_TRACK],
2358 raw_cmd->cmd[TRACK]);
2359 pr_info("heads=%d eoc=%d\n", heads, eoc);
2360 pr_info("spt=%d st=%d ss=%d\n",
2361 raw_cmd->cmd[SECT_PER_TRACK], fsector_t, ssize);
2362 pr_info("in_sector_offset=%d\n", in_sector_offset);
2363 }
2364
2365 nr_sectors -= in_sector_offset;
2366 INFBOUND(nr_sectors, 0);
2367 SUPBOUND(current_count_sectors, nr_sectors);
2368
2369 switch (interpret_errors()) {
2370 case 2:
2371 cont->redo();
2372 return;
2373 case 1:
2374 if (!current_count_sectors) {
2375 cont->error();
2376 cont->redo();
2377 return;
2378 }
2379 break;
2380 case 0:
2381 if (!current_count_sectors) {
2382 cont->redo();
2383 return;
2384 }
2385 current_type[current_drive] = _floppy;
2386 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2387 break;
2388 }
2389
2390 if (probing) {
2391 if (drive_params[current_drive].flags & FTD_MSG)
2392 DPRINT("Auto-detected floppy type %s in fd%d\n",
2393 _floppy->name, current_drive);
2394 current_type[current_drive] = _floppy;
2395 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2396 probing = 0;
2397 }
2398
2399 if (CT(raw_cmd->cmd[COMMAND]) != FD_READ) {
2400
2401 cont->done(1);
2402 } else {
2403 buffer_track = raw_cmd->track;
2404 buffer_drive = current_drive;
2405 INFBOUND(buffer_max, nr_sectors + fsector_t);
2406 }
2407 cont->redo();
2408 }
2409
2410
2411 static int transfer_size(int ssize, int max_sector, int max_size)
2412 {
2413 SUPBOUND(max_sector, fsector_t + max_size);
2414
2415
2416 max_sector -= (max_sector % _floppy->sect) % ssize;
2417
2418
2419 current_count_sectors = max_sector - fsector_t;
2420
2421 return max_sector;
2422 }
2423
2424
2425
2426
2427 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2428 {
2429 int remaining;
2430 struct bio_vec bv;
2431 char *dma_buffer;
2432 int size;
2433 struct req_iterator iter;
2434
2435 max_sector = transfer_size(ssize,
2436 min(max_sector, max_sector_2),
2437 blk_rq_sectors(current_req));
2438
2439 if (current_count_sectors <= 0 && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE &&
2440 buffer_max > fsector_t + blk_rq_sectors(current_req))
2441 current_count_sectors = min_t(int, buffer_max - fsector_t,
2442 blk_rq_sectors(current_req));
2443
2444 remaining = current_count_sectors << 9;
2445 if (remaining > blk_rq_bytes(current_req) && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2446 DPRINT("in copy buffer\n");
2447 pr_info("current_count_sectors=%ld\n", current_count_sectors);
2448 pr_info("remaining=%d\n", remaining >> 9);
2449 pr_info("current_req->nr_sectors=%u\n",
2450 blk_rq_sectors(current_req));
2451 pr_info("current_req->current_nr_sectors=%u\n",
2452 blk_rq_cur_sectors(current_req));
2453 pr_info("max_sector=%d\n", max_sector);
2454 pr_info("ssize=%d\n", ssize);
2455 }
2456
2457 buffer_max = max(max_sector, buffer_max);
2458
2459 dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2460
2461 size = blk_rq_cur_bytes(current_req);
2462
2463 rq_for_each_segment(bv, current_req, iter) {
2464 if (!remaining)
2465 break;
2466
2467 size = bv.bv_len;
2468 SUPBOUND(size, remaining);
2469 if (dma_buffer + size >
2470 floppy_track_buffer + (max_buffer_sectors << 10) ||
2471 dma_buffer < floppy_track_buffer) {
2472 DPRINT("buffer overrun in copy buffer %d\n",
2473 (int)((floppy_track_buffer - dma_buffer) >> 9));
2474 pr_info("fsector_t=%d buffer_min=%d\n",
2475 fsector_t, buffer_min);
2476 pr_info("current_count_sectors=%ld\n",
2477 current_count_sectors);
2478 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2479 pr_info("read\n");
2480 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE)
2481 pr_info("write\n");
2482 break;
2483 }
2484
2485 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2486 memcpy_to_bvec(&bv, dma_buffer);
2487 else
2488 memcpy_from_bvec(dma_buffer, &bv);
2489
2490 remaining -= size;
2491 dma_buffer += size;
2492 }
2493 if (remaining) {
2494 if (remaining > 0)
2495 max_sector -= remaining >> 9;
2496 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2497 }
2498 }
2499
2500
2501
2502
2503
2504
2505
2506
2507 static void virtualdmabug_workaround(void)
2508 {
2509 int hard_sectors;
2510 int end_sector;
2511
2512 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2513 raw_cmd->cmd[COMMAND] &= ~0x80;
2514
2515 hard_sectors = raw_cmd->length >> (7 + raw_cmd->cmd[SIZECODE]);
2516 end_sector = raw_cmd->cmd[SECTOR] + hard_sectors - 1;
2517 if (end_sector > raw_cmd->cmd[SECT_PER_TRACK]) {
2518 pr_info("too many sectors %d > %d\n",
2519 end_sector, raw_cmd->cmd[SECT_PER_TRACK]);
2520 return;
2521 }
2522 raw_cmd->cmd[SECT_PER_TRACK] = end_sector;
2523
2524
2525 }
2526 }
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538 static int make_raw_rw_request(void)
2539 {
2540 int aligned_sector_t;
2541 int max_sector;
2542 int max_size;
2543 int tracksize;
2544 int ssize;
2545
2546 if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n"))
2547 return 0;
2548
2549 set_fdc((long)current_req->q->disk->private_data);
2550
2551 raw_cmd = &default_raw_cmd;
2552 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2553 raw_cmd->cmd_count = NR_RW;
2554 if (rq_data_dir(current_req) == READ) {
2555 raw_cmd->flags |= FD_RAW_READ;
2556 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_READ);
2557 } else if (rq_data_dir(current_req) == WRITE) {
2558 raw_cmd->flags |= FD_RAW_WRITE;
2559 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_WRITE);
2560 } else {
2561 DPRINT("%s: unknown command\n", __func__);
2562 return 0;
2563 }
2564
2565 max_sector = _floppy->sect * _floppy->head;
2566
2567 raw_cmd->cmd[TRACK] = (int)blk_rq_pos(current_req) / max_sector;
2568 fsector_t = (int)blk_rq_pos(current_req) % max_sector;
2569 if (_floppy->track && raw_cmd->cmd[TRACK] >= _floppy->track) {
2570 if (blk_rq_cur_sectors(current_req) & 1) {
2571 current_count_sectors = 1;
2572 return 1;
2573 } else
2574 return 0;
2575 }
2576 raw_cmd->cmd[HEAD] = fsector_t / _floppy->sect;
2577
2578 if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
2579 test_bit(FD_NEED_TWADDLE_BIT, &drive_state[current_drive].flags)) &&
2580 fsector_t < _floppy->sect)
2581 max_sector = _floppy->sect;
2582
2583
2584 if ((_floppy->rate & FD_2M) && (!raw_cmd->cmd[TRACK]) && (!raw_cmd->cmd[HEAD])) {
2585 max_sector = 2 * _floppy->sect / 3;
2586 if (fsector_t >= max_sector) {
2587 current_count_sectors =
2588 min_t(int, _floppy->sect - fsector_t,
2589 blk_rq_sectors(current_req));
2590 return 1;
2591 }
2592 raw_cmd->cmd[SIZECODE] = 2;
2593 } else
2594 raw_cmd->cmd[SIZECODE] = FD_SIZECODE(_floppy);
2595 raw_cmd->rate = _floppy->rate & 0x43;
2596 if ((_floppy->rate & FD_2M) &&
2597 (raw_cmd->cmd[TRACK] || raw_cmd->cmd[HEAD]) && raw_cmd->rate == 2)
2598 raw_cmd->rate = 1;
2599
2600 if (raw_cmd->cmd[SIZECODE])
2601 raw_cmd->cmd[SIZECODE2] = 0xff;
2602 else
2603 raw_cmd->cmd[SIZECODE2] = 0x80;
2604 raw_cmd->track = raw_cmd->cmd[TRACK] << STRETCH(_floppy);
2605 raw_cmd->cmd[DR_SELECT] = UNIT(current_drive) + PH_HEAD(_floppy, raw_cmd->cmd[HEAD]);
2606 raw_cmd->cmd[GAP] = _floppy->gap;
2607 ssize = DIV_ROUND_UP(1 << raw_cmd->cmd[SIZECODE], 4);
2608 raw_cmd->cmd[SECT_PER_TRACK] = _floppy->sect << 2 >> raw_cmd->cmd[SIZECODE];
2609 raw_cmd->cmd[SECTOR] = ((fsector_t % _floppy->sect) << 2 >> raw_cmd->cmd[SIZECODE]) +
2610 FD_SECTBASE(_floppy);
2611
2612
2613
2614
2615 tracksize = _floppy->sect - _floppy->sect % ssize;
2616 if (tracksize < _floppy->sect) {
2617 raw_cmd->cmd[SECT_PER_TRACK]++;
2618 if (tracksize <= fsector_t % _floppy->sect)
2619 raw_cmd->cmd[SECTOR]--;
2620
2621
2622 while (tracksize <= fsector_t % _floppy->sect) {
2623 while (tracksize + ssize > _floppy->sect) {
2624 raw_cmd->cmd[SIZECODE]--;
2625 ssize >>= 1;
2626 }
2627 raw_cmd->cmd[SECTOR]++;
2628 raw_cmd->cmd[SECT_PER_TRACK]++;
2629 tracksize += ssize;
2630 }
2631 max_sector = raw_cmd->cmd[HEAD] * _floppy->sect + tracksize;
2632 } else if (!raw_cmd->cmd[TRACK] && !raw_cmd->cmd[HEAD] && !(_floppy->rate & FD_2M) && probing) {
2633 max_sector = _floppy->sect;
2634 } else if (!raw_cmd->cmd[HEAD] && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2635
2636 max_sector = _floppy->sect;
2637 }
2638
2639 in_sector_offset = (fsector_t % _floppy->sect) % ssize;
2640 aligned_sector_t = fsector_t - in_sector_offset;
2641 max_size = blk_rq_sectors(current_req);
2642 if ((raw_cmd->track == buffer_track) &&
2643 (current_drive == buffer_drive) &&
2644 (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2645
2646 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) {
2647 copy_buffer(1, max_sector, buffer_max);
2648 return 1;
2649 }
2650 } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
2651 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2652 unsigned int sectors;
2653
2654 sectors = fsector_t + blk_rq_sectors(current_req);
2655 if (sectors > ssize && sectors < ssize + ssize)
2656 max_size = ssize + ssize;
2657 else
2658 max_size = ssize;
2659 }
2660 raw_cmd->flags &= ~FD_RAW_WRITE;
2661 raw_cmd->flags |= FD_RAW_READ;
2662 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_READ);
2663 }
2664
2665 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2666 max_size = max_sector;
2667
2668
2669 if (buffer_track != raw_cmd->track ||
2670 buffer_drive != current_drive ||
2671 fsector_t > buffer_max ||
2672 fsector_t < buffer_min ||
2673 ((CT(raw_cmd->cmd[COMMAND]) == FD_READ ||
2674 (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
2675 max_sector > 2 * max_buffer_sectors + buffer_min &&
2676 max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2677
2678 buffer_track = -1;
2679 buffer_drive = current_drive;
2680 buffer_max = buffer_min = aligned_sector_t;
2681 }
2682 raw_cmd->kernel_data = floppy_track_buffer +
2683 ((aligned_sector_t - buffer_min) << 9);
2684
2685 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2686
2687
2688
2689
2690 if (in_sector_offset && buffer_track == -1)
2691 DPRINT("internal error offset !=0 on write\n");
2692 buffer_track = raw_cmd->track;
2693 buffer_drive = current_drive;
2694 copy_buffer(ssize, max_sector,
2695 2 * max_buffer_sectors + buffer_min);
2696 } else
2697 transfer_size(ssize, max_sector,
2698 2 * max_buffer_sectors + buffer_min -
2699 aligned_sector_t);
2700
2701
2702 raw_cmd->length = in_sector_offset + current_count_sectors;
2703 raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
2704 raw_cmd->length <<= 9;
2705 if ((raw_cmd->length < current_count_sectors << 9) ||
2706 (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE &&
2707 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2708 aligned_sector_t < buffer_min)) ||
2709 raw_cmd->length % (128 << raw_cmd->cmd[SIZECODE]) ||
2710 raw_cmd->length <= 0 || current_count_sectors <= 0) {
2711 DPRINT("fractionary current count b=%lx s=%lx\n",
2712 raw_cmd->length, current_count_sectors);
2713 pr_info("addr=%d, length=%ld\n",
2714 (int)((raw_cmd->kernel_data -
2715 floppy_track_buffer) >> 9),
2716 current_count_sectors);
2717 pr_info("st=%d ast=%d mse=%d msi=%d\n",
2718 fsector_t, aligned_sector_t, max_sector, max_size);
2719 pr_info("ssize=%x SIZECODE=%d\n", ssize, raw_cmd->cmd[SIZECODE]);
2720 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2721 raw_cmd->cmd[COMMAND], raw_cmd->cmd[SECTOR],
2722 raw_cmd->cmd[HEAD], raw_cmd->cmd[TRACK]);
2723 pr_info("buffer drive=%d\n", buffer_drive);
2724 pr_info("buffer track=%d\n", buffer_track);
2725 pr_info("buffer_min=%d\n", buffer_min);
2726 pr_info("buffer_max=%d\n", buffer_max);
2727 return 0;
2728 }
2729
2730 if (raw_cmd->kernel_data < floppy_track_buffer ||
2731 current_count_sectors < 0 ||
2732 raw_cmd->length < 0 ||
2733 raw_cmd->kernel_data + raw_cmd->length >
2734 floppy_track_buffer + (max_buffer_sectors << 10)) {
2735 DPRINT("buffer overrun in schedule dma\n");
2736 pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
2737 fsector_t, buffer_min, raw_cmd->length >> 9);
2738 pr_info("current_count_sectors=%ld\n",
2739 current_count_sectors);
2740 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2741 pr_info("read\n");
2742 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE)
2743 pr_info("write\n");
2744 return 0;
2745 }
2746 if (raw_cmd->length == 0) {
2747 DPRINT("zero dma transfer attempted from make_raw_request\n");
2748 return 0;
2749 }
2750
2751 virtualdmabug_workaround();
2752 return 2;
2753 }
2754
2755 static int set_next_request(void)
2756 {
2757 current_req = list_first_entry_or_null(&floppy_reqs, struct request,
2758 queuelist);
2759 if (current_req) {
2760 floppy_errors = 0;
2761 list_del_init(¤t_req->queuelist);
2762 return 1;
2763 }
2764 return 0;
2765 }
2766
2767
2768
2769
2770 static void redo_fd_request(void)
2771 {
2772 int drive;
2773 int tmp;
2774
2775 lastredo = jiffies;
2776 if (current_drive < N_DRIVE)
2777 floppy_off(current_drive);
2778
2779 do_request:
2780 if (!current_req) {
2781 int pending;
2782
2783 spin_lock_irq(&floppy_lock);
2784 pending = set_next_request();
2785 spin_unlock_irq(&floppy_lock);
2786 if (!pending) {
2787 do_floppy = NULL;
2788 unlock_fdc();
2789 return;
2790 }
2791 }
2792 drive = (long)current_req->q->disk->private_data;
2793 set_fdc(drive);
2794 reschedule_timeout(current_drive, "redo fd request");
2795
2796 set_floppy(drive);
2797 raw_cmd = &default_raw_cmd;
2798 raw_cmd->flags = 0;
2799 if (start_motor(redo_fd_request))
2800 return;
2801
2802 disk_change(current_drive);
2803 if (test_bit(current_drive, &fake_change) ||
2804 test_bit(FD_DISK_CHANGED_BIT, &drive_state[current_drive].flags)) {
2805 DPRINT("disk absent or changed during operation\n");
2806 request_done(0);
2807 goto do_request;
2808 }
2809 if (!_floppy) {
2810 if (!probing) {
2811 drive_state[current_drive].probed_format = 0;
2812 if (next_valid_format(current_drive)) {
2813 DPRINT("no autodetectable formats\n");
2814 _floppy = NULL;
2815 request_done(0);
2816 goto do_request;
2817 }
2818 }
2819 probing = 1;
2820 _floppy = floppy_type + drive_params[current_drive].autodetect[drive_state[current_drive].probed_format];
2821 } else
2822 probing = 0;
2823 tmp = make_raw_rw_request();
2824 if (tmp < 2) {
2825 request_done(tmp);
2826 goto do_request;
2827 }
2828
2829 if (test_bit(FD_NEED_TWADDLE_BIT, &drive_state[current_drive].flags))
2830 twaddle(current_fdc, current_drive);
2831 schedule_bh(floppy_start);
2832 debugt(__func__, "queue fd request");
2833 return;
2834 }
2835
2836 static const struct cont_t rw_cont = {
2837 .interrupt = rw_interrupt,
2838 .redo = redo_fd_request,
2839 .error = bad_flp_intr,
2840 .done = request_done
2841 };
2842
2843
2844 static void process_fd_request(void)
2845 {
2846 cont = &rw_cont;
2847 schedule_bh(redo_fd_request);
2848 }
2849
2850 static blk_status_t floppy_queue_rq(struct blk_mq_hw_ctx *hctx,
2851 const struct blk_mq_queue_data *bd)
2852 {
2853 blk_mq_start_request(bd->rq);
2854
2855 if (WARN(max_buffer_sectors == 0,
2856 "VFS: %s called on non-open device\n", __func__))
2857 return BLK_STS_IOERR;
2858
2859 if (WARN(atomic_read(&usage_count) == 0,
2860 "warning: usage count=0, current_req=%p sect=%ld flags=%llx\n",
2861 current_req, (long)blk_rq_pos(current_req),
2862 (__force unsigned long long) current_req->cmd_flags))
2863 return BLK_STS_IOERR;
2864
2865 if (test_and_set_bit(0, &fdc_busy)) {
2866
2867
2868 is_alive(__func__, "old request running");
2869 return BLK_STS_RESOURCE;
2870 }
2871
2872 spin_lock_irq(&floppy_lock);
2873 list_add_tail(&bd->rq->queuelist, &floppy_reqs);
2874 spin_unlock_irq(&floppy_lock);
2875
2876 command_status = FD_COMMAND_NONE;
2877 __reschedule_timeout(MAXTIMEOUT, "fd_request");
2878 set_fdc(0);
2879 process_fd_request();
2880 is_alive(__func__, "");
2881 return BLK_STS_OK;
2882 }
2883
2884 static const struct cont_t poll_cont = {
2885 .interrupt = success_and_wakeup,
2886 .redo = floppy_ready,
2887 .error = generic_failure,
2888 .done = generic_done
2889 };
2890
2891 static int poll_drive(bool interruptible, int flag)
2892 {
2893
2894 raw_cmd = &default_raw_cmd;
2895 raw_cmd->flags = flag;
2896 raw_cmd->track = 0;
2897 raw_cmd->cmd_count = 0;
2898 cont = &poll_cont;
2899 debug_dcl(drive_params[current_drive].flags,
2900 "setting NEWCHANGE in poll_drive\n");
2901 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags);
2902
2903 return wait_til_done(floppy_ready, interruptible);
2904 }
2905
2906
2907
2908
2909
2910
2911 static void reset_intr(void)
2912 {
2913 pr_info("weird, reset interrupt called\n");
2914 }
2915
2916 static const struct cont_t reset_cont = {
2917 .interrupt = reset_intr,
2918 .redo = success_and_wakeup,
2919 .error = generic_failure,
2920 .done = generic_done
2921 };
2922
2923
2924
2925
2926
2927 static int user_reset_fdc(int drive, int arg, bool interruptible)
2928 {
2929 int ret;
2930
2931 if (lock_fdc(drive))
2932 return -EINTR;
2933
2934 if (arg == FD_RESET_ALWAYS)
2935 fdc_state[current_fdc].reset = 1;
2936 if (fdc_state[current_fdc].reset) {
2937
2938
2939
2940 cont = &reset_cont;
2941 ret = wait_til_done(reset_fdc, interruptible);
2942 if (ret == -EINTR)
2943 return -EINTR;
2944 }
2945 process_fd_request();
2946 return 0;
2947 }
2948
2949
2950
2951
2952
2953 static inline int fd_copyout(void __user *param, const void *address,
2954 unsigned long size)
2955 {
2956 return copy_to_user(param, address, size) ? -EFAULT : 0;
2957 }
2958
2959 static inline int fd_copyin(void __user *param, void *address,
2960 unsigned long size)
2961 {
2962 return copy_from_user(address, param, size) ? -EFAULT : 0;
2963 }
2964
2965 static const char *drive_name(int type, int drive)
2966 {
2967 struct floppy_struct *floppy;
2968
2969 if (type)
2970 floppy = floppy_type + type;
2971 else {
2972 if (drive_params[drive].native_format)
2973 floppy = floppy_type + drive_params[drive].native_format;
2974 else
2975 return "(null)";
2976 }
2977 if (floppy->name)
2978 return floppy->name;
2979 else
2980 return "(null)";
2981 }
2982
2983 #ifdef CONFIG_BLK_DEV_FD_RAWCMD
2984
2985
2986 static void raw_cmd_done(int flag)
2987 {
2988 if (!flag) {
2989 raw_cmd->flags |= FD_RAW_FAILURE;
2990 raw_cmd->flags |= FD_RAW_HARDFAILURE;
2991 } else {
2992 raw_cmd->reply_count = inr;
2993 if (raw_cmd->reply_count > FD_RAW_REPLY_SIZE)
2994 raw_cmd->reply_count = 0;
2995 memcpy(raw_cmd->reply, reply_buffer, raw_cmd->reply_count);
2996
2997 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
2998 unsigned long flags;
2999 flags = claim_dma_lock();
3000 raw_cmd->length = fd_get_dma_residue();
3001 release_dma_lock(flags);
3002 }
3003
3004 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3005 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3006 raw_cmd->flags |= FD_RAW_FAILURE;
3007
3008 if (disk_change(current_drive))
3009 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3010 else
3011 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3012 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3013 motor_off_callback(&motor_off_timer[current_drive]);
3014
3015 if (raw_cmd->next &&
3016 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3017 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3018 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3019 !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
3020 raw_cmd = raw_cmd->next;
3021 return;
3022 }
3023 }
3024 generic_done(flag);
3025 }
3026
3027 static const struct cont_t raw_cmd_cont = {
3028 .interrupt = success_and_wakeup,
3029 .redo = floppy_start,
3030 .error = generic_failure,
3031 .done = raw_cmd_done
3032 };
3033
3034 static int raw_cmd_copyout(int cmd, void __user *param,
3035 struct floppy_raw_cmd *ptr)
3036 {
3037 int ret;
3038
3039 while (ptr) {
3040 struct floppy_raw_cmd cmd = *ptr;
3041 cmd.next = NULL;
3042 cmd.kernel_data = NULL;
3043 ret = copy_to_user(param, &cmd, sizeof(cmd));
3044 if (ret)
3045 return -EFAULT;
3046 param += sizeof(struct floppy_raw_cmd);
3047 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
3048 if (ptr->length >= 0 &&
3049 ptr->length <= ptr->buffer_length) {
3050 long length = ptr->buffer_length - ptr->length;
3051 ret = fd_copyout(ptr->data, ptr->kernel_data,
3052 length);
3053 if (ret)
3054 return ret;
3055 }
3056 }
3057 ptr = ptr->next;
3058 }
3059
3060 return 0;
3061 }
3062
3063 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3064 {
3065 struct floppy_raw_cmd *next;
3066 struct floppy_raw_cmd *this;
3067
3068 this = *ptr;
3069 *ptr = NULL;
3070 while (this) {
3071 if (this->buffer_length) {
3072 fd_dma_mem_free((unsigned long)this->kernel_data,
3073 this->buffer_length);
3074 this->buffer_length = 0;
3075 }
3076 next = this->next;
3077 kfree(this);
3078 this = next;
3079 }
3080 }
3081
3082 #define MAX_LEN (1UL << MAX_ORDER << PAGE_SHIFT)
3083
3084 static int raw_cmd_copyin(int cmd, void __user *param,
3085 struct floppy_raw_cmd **rcmd)
3086 {
3087 struct floppy_raw_cmd *ptr;
3088 int ret;
3089
3090 *rcmd = NULL;
3091
3092 loop:
3093 ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_KERNEL);
3094 if (!ptr)
3095 return -ENOMEM;
3096 *rcmd = ptr;
3097 ret = copy_from_user(ptr, param, sizeof(*ptr));
3098 ptr->next = NULL;
3099 ptr->buffer_length = 0;
3100 ptr->kernel_data = NULL;
3101 if (ret)
3102 return -EFAULT;
3103 param += sizeof(struct floppy_raw_cmd);
3104 if (ptr->cmd_count > FD_RAW_CMD_FULLSIZE)
3105 return -EINVAL;
3106
3107 memset(ptr->reply, 0, FD_RAW_REPLY_SIZE);
3108 ptr->resultcode = 0;
3109
3110 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3111 if (ptr->length <= 0 || ptr->length >= MAX_LEN)
3112 return -EINVAL;
3113 ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
3114 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3115 if (!ptr->kernel_data)
3116 return -ENOMEM;
3117 ptr->buffer_length = ptr->length;
3118 }
3119 if (ptr->flags & FD_RAW_WRITE) {
3120 ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
3121 if (ret)
3122 return ret;
3123 }
3124
3125 if (ptr->flags & FD_RAW_MORE) {
3126 rcmd = &(ptr->next);
3127 ptr->rate &= 0x43;
3128 goto loop;
3129 }
3130
3131 return 0;
3132 }
3133
3134 static int raw_cmd_ioctl(int cmd, void __user *param)
3135 {
3136 struct floppy_raw_cmd *my_raw_cmd;
3137 int drive;
3138 int ret2;
3139 int ret;
3140
3141 if (fdc_state[current_fdc].rawcmd <= 1)
3142 fdc_state[current_fdc].rawcmd = 1;
3143 for (drive = 0; drive < N_DRIVE; drive++) {
3144 if (FDC(drive) != current_fdc)
3145 continue;
3146 if (drive == current_drive) {
3147 if (drive_state[drive].fd_ref > 1) {
3148 fdc_state[current_fdc].rawcmd = 2;
3149 break;
3150 }
3151 } else if (drive_state[drive].fd_ref) {
3152 fdc_state[current_fdc].rawcmd = 2;
3153 break;
3154 }
3155 }
3156
3157 if (fdc_state[current_fdc].reset)
3158 return -EIO;
3159
3160 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3161 if (ret) {
3162 raw_cmd_free(&my_raw_cmd);
3163 return ret;
3164 }
3165
3166 raw_cmd = my_raw_cmd;
3167 cont = &raw_cmd_cont;
3168 ret = wait_til_done(floppy_start, true);
3169 debug_dcl(drive_params[current_drive].flags,
3170 "calling disk change from raw_cmd ioctl\n");
3171
3172 if (ret != -EINTR && fdc_state[current_fdc].reset)
3173 ret = -EIO;
3174
3175 drive_state[current_drive].track = NO_TRACK;
3176
3177 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3178 if (!ret)
3179 ret = ret2;
3180 raw_cmd_free(&my_raw_cmd);
3181 return ret;
3182 }
3183
3184 static int floppy_raw_cmd_ioctl(int type, int drive, int cmd,
3185 void __user *param)
3186 {
3187 int ret;
3188
3189 pr_warn_once("Note: FDRAWCMD is deprecated and will be removed from the kernel in the near future.\n");
3190
3191 if (type)
3192 return -EINVAL;
3193 if (lock_fdc(drive))
3194 return -EINTR;
3195 set_floppy(drive);
3196 ret = raw_cmd_ioctl(cmd, param);
3197 if (ret == -EINTR)
3198 return -EINTR;
3199 process_fd_request();
3200 return ret;
3201 }
3202
3203 #else
3204
3205 static int floppy_raw_cmd_ioctl(int type, int drive, int cmd,
3206 void __user *param)
3207 {
3208 return -EOPNOTSUPP;
3209 }
3210
3211 #endif
3212
3213 static int invalidate_drive(struct block_device *bdev)
3214 {
3215
3216 set_bit((long)bdev->bd_disk->private_data, &fake_change);
3217 process_fd_request();
3218 if (bdev_check_media_change(bdev))
3219 floppy_revalidate(bdev->bd_disk);
3220 return 0;
3221 }
3222
3223 static int set_geometry(unsigned int cmd, struct floppy_struct *g,
3224 int drive, int type, struct block_device *bdev)
3225 {
3226 int cnt;
3227
3228
3229 if ((int)g->sect <= 0 ||
3230 (int)g->head <= 0 ||
3231
3232 (int)(g->sect * g->head) <= 0 ||
3233
3234 (unsigned char)((g->sect << 2) >> FD_SIZECODE(g)) == 0 ||
3235 g->track <= 0 || g->track > drive_params[drive].tracks >> STRETCH(g) ||
3236
3237 (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
3238 return -EINVAL;
3239 if (type) {
3240 if (!capable(CAP_SYS_ADMIN))
3241 return -EPERM;
3242 mutex_lock(&open_lock);
3243 if (lock_fdc(drive)) {
3244 mutex_unlock(&open_lock);
3245 return -EINTR;
3246 }
3247 floppy_type[type] = *g;
3248 floppy_type[type].name = "user format";
3249 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3250 floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
3251 floppy_type[type].size + 1;
3252 process_fd_request();
3253 for (cnt = 0; cnt < N_DRIVE; cnt++) {
3254 struct block_device *bdev = opened_bdev[cnt];
3255 if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
3256 continue;
3257 __invalidate_device(bdev, true);
3258 }
3259 mutex_unlock(&open_lock);
3260 } else {
3261 int oldStretch;
3262
3263 if (lock_fdc(drive))
3264 return -EINTR;
3265 if (cmd != FDDEFPRM) {
3266
3267
3268 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3269 return -EINTR;
3270 }
3271 oldStretch = g->stretch;
3272 user_params[drive] = *g;
3273 if (buffer_drive == drive)
3274 SUPBOUND(buffer_max, user_params[drive].sect);
3275 current_type[drive] = &user_params[drive];
3276 floppy_sizes[drive] = user_params[drive].size;
3277 if (cmd == FDDEFPRM)
3278 drive_state[current_drive].keep_data = -1;
3279 else
3280 drive_state[current_drive].keep_data = 1;
3281
3282
3283
3284
3285
3286 if (drive_state[current_drive].maxblock > user_params[drive].sect ||
3287 drive_state[current_drive].maxtrack ||
3288 ((user_params[drive].sect ^ oldStretch) &
3289 (FD_SWAPSIDES | FD_SECTBASEMASK)))
3290 invalidate_drive(bdev);
3291 else
3292 process_fd_request();
3293 }
3294 return 0;
3295 }
3296
3297
3298 static unsigned int ioctl_table[] = {
3299 FDCLRPRM,
3300 FDSETPRM,
3301 FDDEFPRM,
3302 FDGETPRM,
3303 FDMSGON,
3304 FDMSGOFF,
3305 FDFMTBEG,
3306 FDFMTTRK,
3307 FDFMTEND,
3308 FDSETEMSGTRESH,
3309 FDFLUSH,
3310 FDSETMAXERRS,
3311 FDGETMAXERRS,
3312 FDGETDRVTYP,
3313 FDSETDRVPRM,
3314 FDGETDRVPRM,
3315 FDGETDRVSTAT,
3316 FDPOLLDRVSTAT,
3317 FDRESET,
3318 FDGETFDCSTAT,
3319 FDWERRORCLR,
3320 FDWERRORGET,
3321 FDRAWCMD,
3322 FDEJECT,
3323 FDTWADDLE
3324 };
3325
3326 static int normalize_ioctl(unsigned int *cmd, int *size)
3327 {
3328 int i;
3329
3330 for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
3331 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
3332 *size = _IOC_SIZE(*cmd);
3333 *cmd = ioctl_table[i];
3334 if (*size > _IOC_SIZE(*cmd)) {
3335 pr_info("ioctl not yet supported\n");
3336 return -EFAULT;
3337 }
3338 return 0;
3339 }
3340 }
3341 return -EINVAL;
3342 }
3343
3344 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3345 {
3346 if (type)
3347 *g = &floppy_type[type];
3348 else {
3349 if (lock_fdc(drive))
3350 return -EINTR;
3351 if (poll_drive(false, 0) == -EINTR)
3352 return -EINTR;
3353 process_fd_request();
3354 *g = current_type[drive];
3355 }
3356 if (!*g)
3357 return -ENODEV;
3358 return 0;
3359 }
3360
3361 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3362 {
3363 int drive = (long)bdev->bd_disk->private_data;
3364 int type = ITYPE(drive_state[drive].fd_device);
3365 struct floppy_struct *g;
3366 int ret;
3367
3368 ret = get_floppy_geometry(drive, type, &g);
3369 if (ret)
3370 return ret;
3371
3372 geo->heads = g->head;
3373 geo->sectors = g->sect;
3374 geo->cylinders = g->track;
3375 return 0;
3376 }
3377
3378 static bool valid_floppy_drive_params(const short autodetect[FD_AUTODETECT_SIZE],
3379 int native_format)
3380 {
3381 size_t floppy_type_size = ARRAY_SIZE(floppy_type);
3382 size_t i = 0;
3383
3384 for (i = 0; i < FD_AUTODETECT_SIZE; ++i) {
3385 if (autodetect[i] < 0 ||
3386 autodetect[i] >= floppy_type_size)
3387 return false;
3388 }
3389
3390 if (native_format < 0 || native_format >= floppy_type_size)
3391 return false;
3392
3393 return true;
3394 }
3395
3396 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3397 unsigned long param)
3398 {
3399 int drive = (long)bdev->bd_disk->private_data;
3400 int type = ITYPE(drive_state[drive].fd_device);
3401 int ret;
3402 int size;
3403 union inparam {
3404 struct floppy_struct g;
3405 struct format_descr f;
3406 struct floppy_max_errors max_errors;
3407 struct floppy_drive_params dp;
3408 } inparam;
3409 const void *outparam;
3410
3411
3412
3413
3414 if (cmd == CDROMEJECT ||
3415 cmd == 0x6470) {
3416 DPRINT("obsolete eject ioctl\n");
3417 DPRINT("please use floppycontrol --eject\n");
3418 cmd = FDEJECT;
3419 }
3420
3421 if (!((cmd & 0xff00) == 0x0200))
3422 return -EINVAL;
3423
3424
3425 ret = normalize_ioctl(&cmd, &size);
3426 if (ret)
3427 return ret;
3428
3429
3430 if (((cmd & 0x40) && !(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL))) ||
3431 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3432 return -EPERM;
3433
3434 if (WARN_ON(size < 0 || size > sizeof(inparam)))
3435 return -EINVAL;
3436
3437
3438 memset(&inparam, 0, sizeof(inparam));
3439 if (_IOC_DIR(cmd) & _IOC_WRITE) {
3440 ret = fd_copyin((void __user *)param, &inparam, size);
3441 if (ret)
3442 return ret;
3443 }
3444
3445 switch (cmd) {
3446 case FDEJECT:
3447 if (drive_state[drive].fd_ref != 1)
3448
3449 return -EBUSY;
3450 if (lock_fdc(drive))
3451 return -EINTR;
3452
3453
3454
3455 ret = fd_eject(UNIT(drive));
3456
3457 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
3458 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
3459 process_fd_request();
3460 return ret;
3461 case FDCLRPRM:
3462 if (lock_fdc(drive))
3463 return -EINTR;
3464 current_type[drive] = NULL;
3465 floppy_sizes[drive] = MAX_DISK_SIZE << 1;
3466 drive_state[drive].keep_data = 0;
3467 return invalidate_drive(bdev);
3468 case FDSETPRM:
3469 case FDDEFPRM:
3470 return set_geometry(cmd, &inparam.g, drive, type, bdev);
3471 case FDGETPRM:
3472 ret = get_floppy_geometry(drive, type,
3473 (struct floppy_struct **)&outparam);
3474 if (ret)
3475 return ret;
3476 memcpy(&inparam.g, outparam,
3477 offsetof(struct floppy_struct, name));
3478 outparam = &inparam.g;
3479 break;
3480 case FDMSGON:
3481 drive_params[drive].flags |= FTD_MSG;
3482 return 0;
3483 case FDMSGOFF:
3484 drive_params[drive].flags &= ~FTD_MSG;
3485 return 0;
3486 case FDFMTBEG:
3487 if (lock_fdc(drive))
3488 return -EINTR;
3489 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3490 return -EINTR;
3491 ret = drive_state[drive].flags;
3492 process_fd_request();
3493 if (ret & FD_VERIFY)
3494 return -ENODEV;
3495 if (!(ret & FD_DISK_WRITABLE))
3496 return -EROFS;
3497 return 0;
3498 case FDFMTTRK:
3499 if (drive_state[drive].fd_ref != 1)
3500 return -EBUSY;
3501 return do_format(drive, &inparam.f);
3502 case FDFMTEND:
3503 case FDFLUSH:
3504 if (lock_fdc(drive))
3505 return -EINTR;
3506 return invalidate_drive(bdev);
3507 case FDSETEMSGTRESH:
3508 drive_params[drive].max_errors.reporting = (unsigned short)(param & 0x0f);
3509 return 0;
3510 case FDGETMAXERRS:
3511 outparam = &drive_params[drive].max_errors;
3512 break;
3513 case FDSETMAXERRS:
3514 drive_params[drive].max_errors = inparam.max_errors;
3515 break;
3516 case FDGETDRVTYP:
3517 outparam = drive_name(type, drive);
3518 SUPBOUND(size, strlen((const char *)outparam) + 1);
3519 break;
3520 case FDSETDRVPRM:
3521 if (!valid_floppy_drive_params(inparam.dp.autodetect,
3522 inparam.dp.native_format))
3523 return -EINVAL;
3524 drive_params[drive] = inparam.dp;
3525 break;
3526 case FDGETDRVPRM:
3527 outparam = &drive_params[drive];
3528 break;
3529 case FDPOLLDRVSTAT:
3530 if (lock_fdc(drive))
3531 return -EINTR;
3532 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3533 return -EINTR;
3534 process_fd_request();
3535 fallthrough;
3536 case FDGETDRVSTAT:
3537 outparam = &drive_state[drive];
3538 break;
3539 case FDRESET:
3540 return user_reset_fdc(drive, (int)param, true);
3541 case FDGETFDCSTAT:
3542 outparam = &fdc_state[FDC(drive)];
3543 break;
3544 case FDWERRORCLR:
3545 memset(&write_errors[drive], 0, sizeof(write_errors[drive]));
3546 return 0;
3547 case FDWERRORGET:
3548 outparam = &write_errors[drive];
3549 break;
3550 case FDRAWCMD:
3551 return floppy_raw_cmd_ioctl(type, drive, cmd, (void __user *)param);
3552 case FDTWADDLE:
3553 if (lock_fdc(drive))
3554 return -EINTR;
3555 twaddle(current_fdc, current_drive);
3556 process_fd_request();
3557 return 0;
3558 default:
3559 return -EINVAL;
3560 }
3561
3562 if (_IOC_DIR(cmd) & _IOC_READ)
3563 return fd_copyout((void __user *)param, outparam, size);
3564
3565 return 0;
3566 }
3567
3568 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
3569 unsigned int cmd, unsigned long param)
3570 {
3571 int ret;
3572
3573 mutex_lock(&floppy_mutex);
3574 ret = fd_locked_ioctl(bdev, mode, cmd, param);
3575 mutex_unlock(&floppy_mutex);
3576
3577 return ret;
3578 }
3579
3580 #ifdef CONFIG_COMPAT
3581
3582 struct compat_floppy_drive_params {
3583 char cmos;
3584 compat_ulong_t max_dtr;
3585 compat_ulong_t hlt;
3586 compat_ulong_t hut;
3587 compat_ulong_t srt;
3588 compat_ulong_t spinup;
3589 compat_ulong_t spindown;
3590 unsigned char spindown_offset;
3591 unsigned char select_delay;
3592 unsigned char rps;
3593 unsigned char tracks;
3594 compat_ulong_t timeout;
3595 unsigned char interleave_sect;
3596 struct floppy_max_errors max_errors;
3597 char flags;
3598 char read_track;
3599 short autodetect[FD_AUTODETECT_SIZE];
3600 compat_int_t checkfreq;
3601 compat_int_t native_format;
3602 };
3603
3604 struct compat_floppy_drive_struct {
3605 signed char flags;
3606 compat_ulong_t spinup_date;
3607 compat_ulong_t select_date;
3608 compat_ulong_t first_read_date;
3609 short probed_format;
3610 short track;
3611 short maxblock;
3612 short maxtrack;
3613 compat_int_t generation;
3614 compat_int_t keep_data;
3615 compat_int_t fd_ref;
3616 compat_int_t fd_device;
3617 compat_int_t last_checked;
3618 compat_caddr_t dmabuf;
3619 compat_int_t bufblocks;
3620 };
3621
3622 struct compat_floppy_fdc_state {
3623 compat_int_t spec1;
3624 compat_int_t spec2;
3625 compat_int_t dtr;
3626 unsigned char version;
3627 unsigned char dor;
3628 compat_ulong_t address;
3629 unsigned int rawcmd:2;
3630 unsigned int reset:1;
3631 unsigned int need_configure:1;
3632 unsigned int perp_mode:2;
3633 unsigned int has_fifo:1;
3634 unsigned int driver_version;
3635 unsigned char track[4];
3636 };
3637
3638 struct compat_floppy_write_errors {
3639 unsigned int write_errors;
3640 compat_ulong_t first_error_sector;
3641 compat_int_t first_error_generation;
3642 compat_ulong_t last_error_sector;
3643 compat_int_t last_error_generation;
3644 compat_uint_t badness;
3645 };
3646
3647 #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct)
3648 #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct)
3649 #define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params)
3650 #define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params)
3651 #define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct)
3652 #define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct)
3653 #define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state)
3654 #define FDWERRORGET32 _IOR(2, 0x17, struct compat_floppy_write_errors)
3655
3656 static int compat_set_geometry(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3657 struct compat_floppy_struct __user *arg)
3658 {
3659 struct floppy_struct v;
3660 int drive, type;
3661 int err;
3662
3663 BUILD_BUG_ON(offsetof(struct floppy_struct, name) !=
3664 offsetof(struct compat_floppy_struct, name));
3665
3666 if (!(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL)))
3667 return -EPERM;
3668
3669 memset(&v, 0, sizeof(struct floppy_struct));
3670 if (copy_from_user(&v, arg, offsetof(struct floppy_struct, name)))
3671 return -EFAULT;
3672
3673 mutex_lock(&floppy_mutex);
3674 drive = (long)bdev->bd_disk->private_data;
3675 type = ITYPE(drive_state[drive].fd_device);
3676 err = set_geometry(cmd == FDSETPRM32 ? FDSETPRM : FDDEFPRM,
3677 &v, drive, type, bdev);
3678 mutex_unlock(&floppy_mutex);
3679 return err;
3680 }
3681
3682 static int compat_get_prm(int drive,
3683 struct compat_floppy_struct __user *arg)
3684 {
3685 struct compat_floppy_struct v;
3686 struct floppy_struct *p;
3687 int err;
3688
3689 memset(&v, 0, sizeof(v));
3690 mutex_lock(&floppy_mutex);
3691 err = get_floppy_geometry(drive, ITYPE(drive_state[drive].fd_device),
3692 &p);
3693 if (err) {
3694 mutex_unlock(&floppy_mutex);
3695 return err;
3696 }
3697 memcpy(&v, p, offsetof(struct floppy_struct, name));
3698 mutex_unlock(&floppy_mutex);
3699 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_struct)))
3700 return -EFAULT;
3701 return 0;
3702 }
3703
3704 static int compat_setdrvprm(int drive,
3705 struct compat_floppy_drive_params __user *arg)
3706 {
3707 struct compat_floppy_drive_params v;
3708
3709 if (!capable(CAP_SYS_ADMIN))
3710 return -EPERM;
3711 if (copy_from_user(&v, arg, sizeof(struct compat_floppy_drive_params)))
3712 return -EFAULT;
3713 if (!valid_floppy_drive_params(v.autodetect, v.native_format))
3714 return -EINVAL;
3715 mutex_lock(&floppy_mutex);
3716 drive_params[drive].cmos = v.cmos;
3717 drive_params[drive].max_dtr = v.max_dtr;
3718 drive_params[drive].hlt = v.hlt;
3719 drive_params[drive].hut = v.hut;
3720 drive_params[drive].srt = v.srt;
3721 drive_params[drive].spinup = v.spinup;
3722 drive_params[drive].spindown = v.spindown;
3723 drive_params[drive].spindown_offset = v.spindown_offset;
3724 drive_params[drive].select_delay = v.select_delay;
3725 drive_params[drive].rps = v.rps;
3726 drive_params[drive].tracks = v.tracks;
3727 drive_params[drive].timeout = v.timeout;
3728 drive_params[drive].interleave_sect = v.interleave_sect;
3729 drive_params[drive].max_errors = v.max_errors;
3730 drive_params[drive].flags = v.flags;
3731 drive_params[drive].read_track = v.read_track;
3732 memcpy(drive_params[drive].autodetect, v.autodetect,
3733 sizeof(v.autodetect));
3734 drive_params[drive].checkfreq = v.checkfreq;
3735 drive_params[drive].native_format = v.native_format;
3736 mutex_unlock(&floppy_mutex);
3737 return 0;
3738 }
3739
3740 static int compat_getdrvprm(int drive,
3741 struct compat_floppy_drive_params __user *arg)
3742 {
3743 struct compat_floppy_drive_params v;
3744
3745 memset(&v, 0, sizeof(struct compat_floppy_drive_params));
3746 mutex_lock(&floppy_mutex);
3747 v.cmos = drive_params[drive].cmos;
3748 v.max_dtr = drive_params[drive].max_dtr;
3749 v.hlt = drive_params[drive].hlt;
3750 v.hut = drive_params[drive].hut;
3751 v.srt = drive_params[drive].srt;
3752 v.spinup = drive_params[drive].spinup;
3753 v.spindown = drive_params[drive].spindown;
3754 v.spindown_offset = drive_params[drive].spindown_offset;
3755 v.select_delay = drive_params[drive].select_delay;
3756 v.rps = drive_params[drive].rps;
3757 v.tracks = drive_params[drive].tracks;
3758 v.timeout = drive_params[drive].timeout;
3759 v.interleave_sect = drive_params[drive].interleave_sect;
3760 v.max_errors = drive_params[drive].max_errors;
3761 v.flags = drive_params[drive].flags;
3762 v.read_track = drive_params[drive].read_track;
3763 memcpy(v.autodetect, drive_params[drive].autodetect,
3764 sizeof(v.autodetect));
3765 v.checkfreq = drive_params[drive].checkfreq;
3766 v.native_format = drive_params[drive].native_format;
3767 mutex_unlock(&floppy_mutex);
3768
3769 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_params)))
3770 return -EFAULT;
3771 return 0;
3772 }
3773
3774 static int compat_getdrvstat(int drive, bool poll,
3775 struct compat_floppy_drive_struct __user *arg)
3776 {
3777 struct compat_floppy_drive_struct v;
3778
3779 memset(&v, 0, sizeof(struct compat_floppy_drive_struct));
3780 mutex_lock(&floppy_mutex);
3781
3782 if (poll) {
3783 if (lock_fdc(drive))
3784 goto Eintr;
3785 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3786 goto Eintr;
3787 process_fd_request();
3788 }
3789 v.spinup_date = drive_state[drive].spinup_date;
3790 v.select_date = drive_state[drive].select_date;
3791 v.first_read_date = drive_state[drive].first_read_date;
3792 v.probed_format = drive_state[drive].probed_format;
3793 v.track = drive_state[drive].track;
3794 v.maxblock = drive_state[drive].maxblock;
3795 v.maxtrack = drive_state[drive].maxtrack;
3796 v.generation = drive_state[drive].generation;
3797 v.keep_data = drive_state[drive].keep_data;
3798 v.fd_ref = drive_state[drive].fd_ref;
3799 v.fd_device = drive_state[drive].fd_device;
3800 v.last_checked = drive_state[drive].last_checked;
3801 v.dmabuf = (uintptr_t) drive_state[drive].dmabuf;
3802 v.bufblocks = drive_state[drive].bufblocks;
3803 mutex_unlock(&floppy_mutex);
3804
3805 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_struct)))
3806 return -EFAULT;
3807 return 0;
3808 Eintr:
3809 mutex_unlock(&floppy_mutex);
3810 return -EINTR;
3811 }
3812
3813 static int compat_getfdcstat(int drive,
3814 struct compat_floppy_fdc_state __user *arg)
3815 {
3816 struct compat_floppy_fdc_state v32;
3817 struct floppy_fdc_state v;
3818
3819 mutex_lock(&floppy_mutex);
3820 v = fdc_state[FDC(drive)];
3821 mutex_unlock(&floppy_mutex);
3822
3823 memset(&v32, 0, sizeof(struct compat_floppy_fdc_state));
3824 v32.spec1 = v.spec1;
3825 v32.spec2 = v.spec2;
3826 v32.dtr = v.dtr;
3827 v32.version = v.version;
3828 v32.dor = v.dor;
3829 v32.address = v.address;
3830 v32.rawcmd = v.rawcmd;
3831 v32.reset = v.reset;
3832 v32.need_configure = v.need_configure;
3833 v32.perp_mode = v.perp_mode;
3834 v32.has_fifo = v.has_fifo;
3835 v32.driver_version = v.driver_version;
3836 memcpy(v32.track, v.track, 4);
3837 if (copy_to_user(arg, &v32, sizeof(struct compat_floppy_fdc_state)))
3838 return -EFAULT;
3839 return 0;
3840 }
3841
3842 static int compat_werrorget(int drive,
3843 struct compat_floppy_write_errors __user *arg)
3844 {
3845 struct compat_floppy_write_errors v32;
3846 struct floppy_write_errors v;
3847
3848 memset(&v32, 0, sizeof(struct compat_floppy_write_errors));
3849 mutex_lock(&floppy_mutex);
3850 v = write_errors[drive];
3851 mutex_unlock(&floppy_mutex);
3852 v32.write_errors = v.write_errors;
3853 v32.first_error_sector = v.first_error_sector;
3854 v32.first_error_generation = v.first_error_generation;
3855 v32.last_error_sector = v.last_error_sector;
3856 v32.last_error_generation = v.last_error_generation;
3857 v32.badness = v.badness;
3858 if (copy_to_user(arg, &v32, sizeof(struct compat_floppy_write_errors)))
3859 return -EFAULT;
3860 return 0;
3861 }
3862
3863 static int fd_compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3864 unsigned long param)
3865 {
3866 int drive = (long)bdev->bd_disk->private_data;
3867 switch (cmd) {
3868 case CDROMEJECT:
3869 case 0x6470:
3870
3871 case FDMSGON:
3872 case FDMSGOFF:
3873 case FDSETEMSGTRESH:
3874 case FDFLUSH:
3875 case FDWERRORCLR:
3876 case FDEJECT:
3877 case FDCLRPRM:
3878 case FDFMTBEG:
3879 case FDRESET:
3880 case FDTWADDLE:
3881 return fd_ioctl(bdev, mode, cmd, param);
3882 case FDSETMAXERRS:
3883 case FDGETMAXERRS:
3884 case FDGETDRVTYP:
3885 case FDFMTEND:
3886 case FDFMTTRK:
3887 case FDRAWCMD:
3888 return fd_ioctl(bdev, mode, cmd,
3889 (unsigned long)compat_ptr(param));
3890 case FDSETPRM32:
3891 case FDDEFPRM32:
3892 return compat_set_geometry(bdev, mode, cmd, compat_ptr(param));
3893 case FDGETPRM32:
3894 return compat_get_prm(drive, compat_ptr(param));
3895 case FDSETDRVPRM32:
3896 return compat_setdrvprm(drive, compat_ptr(param));
3897 case FDGETDRVPRM32:
3898 return compat_getdrvprm(drive, compat_ptr(param));
3899 case FDPOLLDRVSTAT32:
3900 return compat_getdrvstat(drive, true, compat_ptr(param));
3901 case FDGETDRVSTAT32:
3902 return compat_getdrvstat(drive, false, compat_ptr(param));
3903 case FDGETFDCSTAT32:
3904 return compat_getfdcstat(drive, compat_ptr(param));
3905 case FDWERRORGET32:
3906 return compat_werrorget(drive, compat_ptr(param));
3907 }
3908 return -EINVAL;
3909 }
3910 #endif
3911
3912 static void __init config_types(void)
3913 {
3914 bool has_drive = false;
3915 int drive;
3916
3917
3918 drive = 0;
3919 if (!drive_params[drive].cmos)
3920 drive_params[drive].cmos = FLOPPY0_TYPE;
3921 drive = 1;
3922 if (!drive_params[drive].cmos)
3923 drive_params[drive].cmos = FLOPPY1_TYPE;
3924
3925
3926
3927 for (drive = 0; drive < N_DRIVE; drive++) {
3928 unsigned int type = drive_params[drive].cmos;
3929 struct floppy_drive_params *params;
3930 const char *name = NULL;
3931 char temparea[32];
3932
3933 if (type < ARRAY_SIZE(default_drive_params)) {
3934 params = &default_drive_params[type].params;
3935 if (type) {
3936 name = default_drive_params[type].name;
3937 allowed_drive_mask |= 1 << drive;
3938 } else
3939 allowed_drive_mask &= ~(1 << drive);
3940 } else {
3941 params = &default_drive_params[0].params;
3942 snprintf(temparea, sizeof(temparea),
3943 "unknown type %d (usb?)", type);
3944 name = temparea;
3945 }
3946 if (name) {
3947 const char *prepend;
3948 if (!has_drive) {
3949 prepend = "";
3950 has_drive = true;
3951 pr_info("Floppy drive(s):");
3952 } else {
3953 prepend = ",";
3954 }
3955
3956 pr_cont("%s fd%d is %s", prepend, drive, name);
3957 }
3958 drive_params[drive] = *params;
3959 }
3960
3961 if (has_drive)
3962 pr_cont("\n");
3963 }
3964
3965 static void floppy_release(struct gendisk *disk, fmode_t mode)
3966 {
3967 int drive = (long)disk->private_data;
3968
3969 mutex_lock(&floppy_mutex);
3970 mutex_lock(&open_lock);
3971 if (!drive_state[drive].fd_ref--) {
3972 DPRINT("floppy_release with fd_ref == 0");
3973 drive_state[drive].fd_ref = 0;
3974 }
3975 if (!drive_state[drive].fd_ref)
3976 opened_bdev[drive] = NULL;
3977 mutex_unlock(&open_lock);
3978 mutex_unlock(&floppy_mutex);
3979 }
3980
3981
3982
3983
3984
3985
3986 static int floppy_open(struct block_device *bdev, fmode_t mode)
3987 {
3988 int drive = (long)bdev->bd_disk->private_data;
3989 int old_dev, new_dev;
3990 int try;
3991 int res = -EBUSY;
3992 char *tmp;
3993
3994 mutex_lock(&floppy_mutex);
3995 mutex_lock(&open_lock);
3996 old_dev = drive_state[drive].fd_device;
3997 if (opened_bdev[drive] && opened_bdev[drive] != bdev)
3998 goto out2;
3999
4000 if (!drive_state[drive].fd_ref && (drive_params[drive].flags & FD_BROKEN_DCL)) {
4001 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4002 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
4003 }
4004
4005 drive_state[drive].fd_ref++;
4006
4007 opened_bdev[drive] = bdev;
4008
4009 res = -ENXIO;
4010
4011 if (!floppy_track_buffer) {
4012
4013
4014 if ((drive_params[drive].cmos == 6) || (drive_params[drive].cmos == 5))
4015 try = 64;
4016 else
4017 try = 32;
4018
4019 tmp = (char *)fd_dma_mem_alloc(1024 * try);
4020 if (!tmp && !floppy_track_buffer) {
4021 try >>= 1;
4022 INFBOUND(try, 16);
4023 tmp = (char *)fd_dma_mem_alloc(1024 * try);
4024 }
4025 if (!tmp && !floppy_track_buffer)
4026 fallback_on_nodma_alloc(&tmp, 2048 * try);
4027 if (!tmp && !floppy_track_buffer) {
4028 DPRINT("Unable to allocate DMA memory\n");
4029 goto out;
4030 }
4031 if (floppy_track_buffer) {
4032 if (tmp)
4033 fd_dma_mem_free((unsigned long)tmp, try * 1024);
4034 } else {
4035 buffer_min = buffer_max = -1;
4036 floppy_track_buffer = tmp;
4037 max_buffer_sectors = try;
4038 }
4039 }
4040
4041 new_dev = MINOR(bdev->bd_dev);
4042 drive_state[drive].fd_device = new_dev;
4043 set_capacity(disks[drive][ITYPE(new_dev)], floppy_sizes[new_dev]);
4044 if (old_dev != -1 && old_dev != new_dev) {
4045 if (buffer_drive == drive)
4046 buffer_track = -1;
4047 }
4048
4049 if (fdc_state[FDC(drive)].rawcmd == 1)
4050 fdc_state[FDC(drive)].rawcmd = 2;
4051
4052 if (!(mode & FMODE_NDELAY)) {
4053 if (mode & (FMODE_READ|FMODE_WRITE)) {
4054 drive_state[drive].last_checked = 0;
4055 clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
4056 &drive_state[drive].flags);
4057 if (bdev_check_media_change(bdev))
4058 floppy_revalidate(bdev->bd_disk);
4059 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags))
4060 goto out;
4061 if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags))
4062 goto out;
4063 }
4064 res = -EROFS;
4065 if ((mode & FMODE_WRITE) &&
4066 !test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
4067 goto out;
4068 }
4069 mutex_unlock(&open_lock);
4070 mutex_unlock(&floppy_mutex);
4071 return 0;
4072 out:
4073 drive_state[drive].fd_ref--;
4074
4075 if (!drive_state[drive].fd_ref)
4076 opened_bdev[drive] = NULL;
4077 out2:
4078 mutex_unlock(&open_lock);
4079 mutex_unlock(&floppy_mutex);
4080 return res;
4081 }
4082
4083
4084
4085
4086 static unsigned int floppy_check_events(struct gendisk *disk,
4087 unsigned int clearing)
4088 {
4089 int drive = (long)disk->private_data;
4090
4091 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4092 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags))
4093 return DISK_EVENT_MEDIA_CHANGE;
4094
4095 if (time_after(jiffies, drive_state[drive].last_checked + drive_params[drive].checkfreq)) {
4096 if (lock_fdc(drive))
4097 return 0;
4098 poll_drive(false, 0);
4099 process_fd_request();
4100 }
4101
4102 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4103 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags) ||
4104 test_bit(drive, &fake_change) ||
4105 drive_no_geom(drive))
4106 return DISK_EVENT_MEDIA_CHANGE;
4107 return 0;
4108 }
4109
4110
4111
4112
4113
4114
4115
4116 struct rb0_cbdata {
4117 int drive;
4118 struct completion complete;
4119 };
4120
4121 static void floppy_rb0_cb(struct bio *bio)
4122 {
4123 struct rb0_cbdata *cbdata = (struct rb0_cbdata *)bio->bi_private;
4124 int drive = cbdata->drive;
4125
4126 if (bio->bi_status) {
4127 pr_info("floppy: error %d while reading block 0\n",
4128 bio->bi_status);
4129 set_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags);
4130 }
4131 complete(&cbdata->complete);
4132 }
4133
4134 static int __floppy_read_block_0(struct block_device *bdev, int drive)
4135 {
4136 struct bio bio;
4137 struct bio_vec bio_vec;
4138 struct page *page;
4139 struct rb0_cbdata cbdata;
4140
4141 page = alloc_page(GFP_NOIO);
4142 if (!page) {
4143 process_fd_request();
4144 return -ENOMEM;
4145 }
4146
4147 cbdata.drive = drive;
4148
4149 bio_init(&bio, bdev, &bio_vec, 1, REQ_OP_READ);
4150 bio_add_page(&bio, page, block_size(bdev), 0);
4151
4152 bio.bi_iter.bi_sector = 0;
4153 bio.bi_flags |= (1 << BIO_QUIET);
4154 bio.bi_private = &cbdata;
4155 bio.bi_end_io = floppy_rb0_cb;
4156
4157 init_completion(&cbdata.complete);
4158
4159 submit_bio(&bio);
4160 process_fd_request();
4161
4162 wait_for_completion(&cbdata.complete);
4163
4164 __free_page(page);
4165
4166 return 0;
4167 }
4168
4169
4170
4171
4172
4173 static int floppy_revalidate(struct gendisk *disk)
4174 {
4175 int drive = (long)disk->private_data;
4176 int cf;
4177 int res = 0;
4178
4179 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4180 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags) ||
4181 test_bit(drive, &fake_change) ||
4182 drive_no_geom(drive)) {
4183 if (WARN(atomic_read(&usage_count) == 0,
4184 "VFS: revalidate called on non-open device.\n"))
4185 return -EFAULT;
4186
4187 res = lock_fdc(drive);
4188 if (res)
4189 return res;
4190 cf = (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4191 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags));
4192 if (!(cf || test_bit(drive, &fake_change) || drive_no_geom(drive))) {
4193 process_fd_request();
4194 return 0;
4195 }
4196 drive_state[drive].maxblock = 0;
4197 drive_state[drive].maxtrack = 0;
4198 if (buffer_drive == drive)
4199 buffer_track = -1;
4200 clear_bit(drive, &fake_change);
4201 clear_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4202 if (cf)
4203 drive_state[drive].generation++;
4204 if (drive_no_geom(drive)) {
4205
4206 res = __floppy_read_block_0(opened_bdev[drive], drive);
4207 } else {
4208 if (cf)
4209 poll_drive(false, FD_RAW_NEED_DISK);
4210 process_fd_request();
4211 }
4212 }
4213 set_capacity(disk, floppy_sizes[drive_state[drive].fd_device]);
4214 return res;
4215 }
4216
4217 static const struct block_device_operations floppy_fops = {
4218 .owner = THIS_MODULE,
4219 .open = floppy_open,
4220 .release = floppy_release,
4221 .ioctl = fd_ioctl,
4222 .getgeo = fd_getgeo,
4223 .check_events = floppy_check_events,
4224 #ifdef CONFIG_COMPAT
4225 .compat_ioctl = fd_compat_ioctl,
4226 #endif
4227 };
4228
4229
4230
4231
4232
4233
4234
4235
4236 static char __init get_fdc_version(int fdc)
4237 {
4238 int r;
4239
4240 output_byte(fdc, FD_DUMPREGS);
4241 if (fdc_state[fdc].reset)
4242 return FDC_NONE;
4243 r = result(fdc);
4244 if (r <= 0x00)
4245 return FDC_NONE;
4246 if ((r == 1) && (reply_buffer[ST0] == 0x80)) {
4247 pr_info("FDC %d is an 8272A\n", fdc);
4248 return FDC_8272A;
4249 }
4250 if (r != 10) {
4251 pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
4252 fdc, r);
4253 return FDC_UNKNOWN;
4254 }
4255
4256 if (!fdc_configure(fdc)) {
4257 pr_info("FDC %d is an 82072\n", fdc);
4258 return FDC_82072;
4259 }
4260
4261 output_byte(fdc, FD_PERPENDICULAR);
4262 if (need_more_output(fdc) == MORE_OUTPUT) {
4263 output_byte(fdc, 0);
4264 } else {
4265 pr_info("FDC %d is an 82072A\n", fdc);
4266 return FDC_82072A;
4267 }
4268
4269 output_byte(fdc, FD_UNLOCK);
4270 r = result(fdc);
4271 if ((r == 1) && (reply_buffer[ST0] == 0x80)) {
4272 pr_info("FDC %d is a pre-1991 82077\n", fdc);
4273 return FDC_82077_ORIG;
4274
4275 }
4276 if ((r != 1) || (reply_buffer[ST0] != 0x00)) {
4277 pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
4278 fdc, r);
4279 return FDC_UNKNOWN;
4280 }
4281 output_byte(fdc, FD_PARTID);
4282 r = result(fdc);
4283 if (r != 1) {
4284 pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n",
4285 fdc, r);
4286 return FDC_UNKNOWN;
4287 }
4288 if (reply_buffer[ST0] == 0x80) {
4289 pr_info("FDC %d is a post-1991 82077\n", fdc);
4290 return FDC_82077;
4291 }
4292 switch (reply_buffer[ST0] >> 5) {
4293 case 0x0:
4294
4295 pr_info("FDC %d is an 82078.\n", fdc);
4296 return FDC_82078;
4297 case 0x1:
4298 pr_info("FDC %d is a 44pin 82078\n", fdc);
4299 return FDC_82078;
4300 case 0x2:
4301 pr_info("FDC %d is a S82078B\n", fdc);
4302 return FDC_S82078B;
4303 case 0x3:
4304 pr_info("FDC %d is a National Semiconductor PC87306\n", fdc);
4305 return FDC_87306;
4306 default:
4307 pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n",
4308 fdc, reply_buffer[ST0] >> 5);
4309 return FDC_82078_UNKN;
4310 }
4311 }
4312
4313
4314
4315 static void __init floppy_set_flags(int *ints, int param, int param2)
4316 {
4317 int i;
4318
4319 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4320 if (param)
4321 default_drive_params[i].params.flags |= param2;
4322 else
4323 default_drive_params[i].params.flags &= ~param2;
4324 }
4325 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4326 }
4327
4328 static void __init daring(int *ints, int param, int param2)
4329 {
4330 int i;
4331
4332 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4333 if (param) {
4334 default_drive_params[i].params.select_delay = 0;
4335 default_drive_params[i].params.flags |=
4336 FD_SILENT_DCL_CLEAR;
4337 } else {
4338 default_drive_params[i].params.select_delay =
4339 2 * HZ / 100;
4340 default_drive_params[i].params.flags &=
4341 ~FD_SILENT_DCL_CLEAR;
4342 }
4343 }
4344 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4345 }
4346
4347 static void __init set_cmos(int *ints, int dummy, int dummy2)
4348 {
4349 int current_drive = 0;
4350
4351 if (ints[0] != 2) {
4352 DPRINT("wrong number of parameters for CMOS\n");
4353 return;
4354 }
4355 current_drive = ints[1];
4356 if (current_drive < 0 || current_drive >= 8) {
4357 DPRINT("bad drive for set_cmos\n");
4358 return;
4359 }
4360 #if N_FDC > 1
4361 if (current_drive >= 4 && !FDC2)
4362 FDC2 = 0x370;
4363 #endif
4364 drive_params[current_drive].cmos = ints[2];
4365 DPRINT("setting CMOS code to %d\n", ints[2]);
4366 }
4367
4368 static struct param_table {
4369 const char *name;
4370 void (*fn) (int *ints, int param, int param2);
4371 int *var;
4372 int def_param;
4373 int param2;
4374 } config_params[] __initdata = {
4375 {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0},
4376 {"all_drives", NULL, &allowed_drive_mask, 0xff, 0},
4377 {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
4378 {"irq", NULL, &FLOPPY_IRQ, 6, 0},
4379 {"dma", NULL, &FLOPPY_DMA, 2, 0},
4380 {"daring", daring, NULL, 1, 0},
4381 #if N_FDC > 1
4382 {"two_fdc", NULL, &FDC2, 0x370, 0},
4383 {"one_fdc", NULL, &FDC2, 0, 0},
4384 #endif
4385 {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
4386 {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
4387 {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
4388 {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
4389 {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
4390 {"nodma", NULL, &can_use_virtual_dma, 1, 0},
4391 {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
4392 {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
4393 {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
4394 {"nofifo", NULL, &no_fifo, 0x20, 0},
4395 {"usefifo", NULL, &no_fifo, 0, 0},
4396 {"cmos", set_cmos, NULL, 0, 0},
4397 {"slow", NULL, &slow_floppy, 1, 0},
4398 {"unexpected_interrupts", NULL, &print_unex, 1, 0},
4399 {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
4400 {"L40SX", NULL, &print_unex, 0, 0}
4401
4402 EXTRA_FLOPPY_PARAMS
4403 };
4404
4405 static int __init floppy_setup(char *str)
4406 {
4407 int i;
4408 int param;
4409 int ints[11];
4410
4411 str = get_options(str, ARRAY_SIZE(ints), ints);
4412 if (str) {
4413 for (i = 0; i < ARRAY_SIZE(config_params); i++) {
4414 if (strcmp(str, config_params[i].name) == 0) {
4415 if (ints[0])
4416 param = ints[1];
4417 else
4418 param = config_params[i].def_param;
4419 if (config_params[i].fn)
4420 config_params[i].fn(ints, param,
4421 config_params[i].
4422 param2);
4423 if (config_params[i].var) {
4424 DPRINT("%s=%d\n", str, param);
4425 *config_params[i].var = param;
4426 }
4427 return 1;
4428 }
4429 }
4430 }
4431 if (str) {
4432 DPRINT("unknown floppy option [%s]\n", str);
4433
4434 DPRINT("allowed options are:");
4435 for (i = 0; i < ARRAY_SIZE(config_params); i++)
4436 pr_cont(" %s", config_params[i].name);
4437 pr_cont("\n");
4438 } else
4439 DPRINT("botched floppy option\n");
4440 DPRINT("Read Documentation/admin-guide/blockdev/floppy.rst\n");
4441 return 0;
4442 }
4443
4444 static int have_no_fdc = -ENODEV;
4445
4446 static ssize_t floppy_cmos_show(struct device *dev,
4447 struct device_attribute *attr, char *buf)
4448 {
4449 struct platform_device *p = to_platform_device(dev);
4450 int drive;
4451
4452 drive = p->id;
4453 return sprintf(buf, "%X\n", drive_params[drive].cmos);
4454 }
4455
4456 static DEVICE_ATTR(cmos, 0444, floppy_cmos_show, NULL);
4457
4458 static struct attribute *floppy_dev_attrs[] = {
4459 &dev_attr_cmos.attr,
4460 NULL
4461 };
4462
4463 ATTRIBUTE_GROUPS(floppy_dev);
4464
4465 static void floppy_device_release(struct device *dev)
4466 {
4467 }
4468
4469 static int floppy_resume(struct device *dev)
4470 {
4471 int fdc;
4472 int saved_drive;
4473
4474 saved_drive = current_drive;
4475 for (fdc = 0; fdc < N_FDC; fdc++)
4476 if (fdc_state[fdc].address != -1)
4477 user_reset_fdc(REVDRIVE(fdc, 0), FD_RESET_ALWAYS, false);
4478 set_fdc(saved_drive);
4479 return 0;
4480 }
4481
4482 static const struct dev_pm_ops floppy_pm_ops = {
4483 .resume = floppy_resume,
4484 .restore = floppy_resume,
4485 };
4486
4487 static struct platform_driver floppy_driver = {
4488 .driver = {
4489 .name = "floppy",
4490 .pm = &floppy_pm_ops,
4491 },
4492 };
4493
4494 static const struct blk_mq_ops floppy_mq_ops = {
4495 .queue_rq = floppy_queue_rq,
4496 };
4497
4498 static struct platform_device floppy_device[N_DRIVE];
4499 static bool registered[N_DRIVE];
4500
4501 static bool floppy_available(int drive)
4502 {
4503 if (!(allowed_drive_mask & (1 << drive)))
4504 return false;
4505 if (fdc_state[FDC(drive)].version == FDC_NONE)
4506 return false;
4507 return true;
4508 }
4509
4510 static int floppy_alloc_disk(unsigned int drive, unsigned int type)
4511 {
4512 struct gendisk *disk;
4513
4514 disk = blk_mq_alloc_disk(&tag_sets[drive], NULL);
4515 if (IS_ERR(disk))
4516 return PTR_ERR(disk);
4517
4518 blk_queue_max_hw_sectors(disk->queue, 64);
4519 disk->major = FLOPPY_MAJOR;
4520 disk->first_minor = TOMINOR(drive) | (type << 2);
4521 disk->minors = 1;
4522 disk->fops = &floppy_fops;
4523 disk->flags |= GENHD_FL_NO_PART;
4524 disk->events = DISK_EVENT_MEDIA_CHANGE;
4525 if (type)
4526 sprintf(disk->disk_name, "fd%d_type%d", drive, type);
4527 else
4528 sprintf(disk->disk_name, "fd%d", drive);
4529
4530 disk->private_data = (void *)(long)drive;
4531 disk->flags |= GENHD_FL_REMOVABLE;
4532
4533 disks[drive][type] = disk;
4534 return 0;
4535 }
4536
4537 static DEFINE_MUTEX(floppy_probe_lock);
4538
4539 static void floppy_probe(dev_t dev)
4540 {
4541 unsigned int drive = (MINOR(dev) & 3) | ((MINOR(dev) & 0x80) >> 5);
4542 unsigned int type = (MINOR(dev) >> 2) & 0x1f;
4543
4544 if (drive >= N_DRIVE || !floppy_available(drive) ||
4545 type >= ARRAY_SIZE(floppy_type))
4546 return;
4547
4548 mutex_lock(&floppy_probe_lock);
4549 if (disks[drive][type])
4550 goto out;
4551 if (floppy_alloc_disk(drive, type))
4552 goto out;
4553 if (add_disk(disks[drive][type]))
4554 goto cleanup_disk;
4555 out:
4556 mutex_unlock(&floppy_probe_lock);
4557 return;
4558
4559 cleanup_disk:
4560 put_disk(disks[drive][type]);
4561 disks[drive][type] = NULL;
4562 mutex_unlock(&floppy_probe_lock);
4563 }
4564
4565 static int __init do_floppy_init(void)
4566 {
4567 int i, unit, drive, err;
4568
4569 set_debugt();
4570 interruptjiffies = resultjiffies = jiffies;
4571
4572 #if defined(CONFIG_PPC)
4573 if (check_legacy_ioport(FDC1))
4574 return -ENODEV;
4575 #endif
4576
4577 raw_cmd = NULL;
4578
4579 floppy_wq = alloc_ordered_workqueue("floppy", 0);
4580 if (!floppy_wq)
4581 return -ENOMEM;
4582
4583 for (drive = 0; drive < N_DRIVE; drive++) {
4584 memset(&tag_sets[drive], 0, sizeof(tag_sets[drive]));
4585 tag_sets[drive].ops = &floppy_mq_ops;
4586 tag_sets[drive].nr_hw_queues = 1;
4587 tag_sets[drive].nr_maps = 1;
4588 tag_sets[drive].queue_depth = 2;
4589 tag_sets[drive].numa_node = NUMA_NO_NODE;
4590 tag_sets[drive].flags = BLK_MQ_F_SHOULD_MERGE;
4591 err = blk_mq_alloc_tag_set(&tag_sets[drive]);
4592 if (err)
4593 goto out_put_disk;
4594
4595 err = floppy_alloc_disk(drive, 0);
4596 if (err)
4597 goto out_put_disk;
4598
4599 timer_setup(&motor_off_timer[drive], motor_off_callback, 0);
4600 }
4601
4602 err = __register_blkdev(FLOPPY_MAJOR, "fd", floppy_probe);
4603 if (err)
4604 goto out_put_disk;
4605
4606 err = platform_driver_register(&floppy_driver);
4607 if (err)
4608 goto out_unreg_blkdev;
4609
4610 for (i = 0; i < 256; i++)
4611 if (ITYPE(i))
4612 floppy_sizes[i] = floppy_type[ITYPE(i)].size;
4613 else
4614 floppy_sizes[i] = MAX_DISK_SIZE << 1;
4615
4616 reschedule_timeout(MAXTIMEOUT, "floppy init");
4617 config_types();
4618
4619 for (i = 0; i < N_FDC; i++) {
4620 memset(&fdc_state[i], 0, sizeof(*fdc_state));
4621 fdc_state[i].dtr = -1;
4622 fdc_state[i].dor = 0x4;
4623 #if defined(__sparc__) || defined(__mc68000__)
4624
4625 #ifdef __mc68000__
4626 if (MACH_IS_SUN3X)
4627 #endif
4628 fdc_state[i].version = FDC_82072A;
4629 #endif
4630 }
4631
4632 use_virtual_dma = can_use_virtual_dma & 1;
4633 fdc_state[0].address = FDC1;
4634 if (fdc_state[0].address == -1) {
4635 cancel_delayed_work(&fd_timeout);
4636 err = -ENODEV;
4637 goto out_unreg_driver;
4638 }
4639 #if N_FDC > 1
4640 fdc_state[1].address = FDC2;
4641 #endif
4642
4643 current_fdc = 0;
4644 err = floppy_grab_irq_and_dma();
4645 if (err) {
4646 cancel_delayed_work(&fd_timeout);
4647 err = -EBUSY;
4648 goto out_unreg_driver;
4649 }
4650
4651
4652 for (drive = 0; drive < N_DRIVE; drive++) {
4653 memset(&drive_state[drive], 0, sizeof(drive_state[drive]));
4654 memset(&write_errors[drive], 0, sizeof(write_errors[drive]));
4655 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[drive].flags);
4656 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4657 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
4658 drive_state[drive].fd_device = -1;
4659 floppy_track_buffer = NULL;
4660 max_buffer_sectors = 0;
4661 }
4662
4663
4664
4665
4666
4667 msleep(10);
4668
4669 for (i = 0; i < N_FDC; i++) {
4670 fdc_state[i].driver_version = FD_DRIVER_VERSION;
4671 for (unit = 0; unit < 4; unit++)
4672 fdc_state[i].track[unit] = 0;
4673 if (fdc_state[i].address == -1)
4674 continue;
4675 fdc_state[i].rawcmd = 2;
4676 if (user_reset_fdc(REVDRIVE(i, 0), FD_RESET_ALWAYS, false)) {
4677
4678 floppy_release_regions(i);
4679 fdc_state[i].address = -1;
4680 fdc_state[i].version = FDC_NONE;
4681 continue;
4682 }
4683
4684 fdc_state[i].version = get_fdc_version(i);
4685 if (fdc_state[i].version == FDC_NONE) {
4686
4687 floppy_release_regions(i);
4688 fdc_state[i].address = -1;
4689 continue;
4690 }
4691 if (can_use_virtual_dma == 2 &&
4692 fdc_state[i].version < FDC_82072A)
4693 can_use_virtual_dma = 0;
4694
4695 have_no_fdc = 0;
4696
4697
4698
4699
4700 user_reset_fdc(REVDRIVE(i, 0), FD_RESET_ALWAYS, false);
4701 }
4702 current_fdc = 0;
4703 cancel_delayed_work(&fd_timeout);
4704 current_drive = 0;
4705 initialized = true;
4706 if (have_no_fdc) {
4707 DPRINT("no floppy controllers found\n");
4708 err = have_no_fdc;
4709 goto out_release_dma;
4710 }
4711
4712 for (drive = 0; drive < N_DRIVE; drive++) {
4713 if (!floppy_available(drive))
4714 continue;
4715
4716 floppy_device[drive].name = floppy_device_name;
4717 floppy_device[drive].id = drive;
4718 floppy_device[drive].dev.release = floppy_device_release;
4719 floppy_device[drive].dev.groups = floppy_dev_groups;
4720
4721 err = platform_device_register(&floppy_device[drive]);
4722 if (err)
4723 goto out_remove_drives;
4724
4725 registered[drive] = true;
4726
4727 err = device_add_disk(&floppy_device[drive].dev,
4728 disks[drive][0], NULL);
4729 if (err)
4730 goto out_remove_drives;
4731 }
4732
4733 return 0;
4734
4735 out_remove_drives:
4736 while (drive--) {
4737 if (floppy_available(drive)) {
4738 del_gendisk(disks[drive][0]);
4739 if (registered[drive])
4740 platform_device_unregister(&floppy_device[drive]);
4741 }
4742 }
4743 out_release_dma:
4744 if (atomic_read(&usage_count))
4745 floppy_release_irq_and_dma();
4746 out_unreg_driver:
4747 platform_driver_unregister(&floppy_driver);
4748 out_unreg_blkdev:
4749 unregister_blkdev(FLOPPY_MAJOR, "fd");
4750 out_put_disk:
4751 destroy_workqueue(floppy_wq);
4752 for (drive = 0; drive < N_DRIVE; drive++) {
4753 if (!disks[drive][0])
4754 break;
4755 del_timer_sync(&motor_off_timer[drive]);
4756 put_disk(disks[drive][0]);
4757 blk_mq_free_tag_set(&tag_sets[drive]);
4758 }
4759 return err;
4760 }
4761
4762 #ifndef MODULE
4763 static __init void floppy_async_init(void *data, async_cookie_t cookie)
4764 {
4765 do_floppy_init();
4766 }
4767 #endif
4768
4769 static int __init floppy_init(void)
4770 {
4771 #ifdef MODULE
4772 return do_floppy_init();
4773 #else
4774
4775 async_schedule(floppy_async_init, NULL);
4776 return 0;
4777 #endif
4778 }
4779
4780 static const struct io_region {
4781 int offset;
4782 int size;
4783 } io_regions[] = {
4784 { 2, 1 },
4785
4786 { 4, 2 },
4787
4788
4789 { 7, 1 },
4790 };
4791
4792 static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
4793 {
4794 while (p != io_regions) {
4795 p--;
4796 release_region(fdc_state[fdc].address + p->offset, p->size);
4797 }
4798 }
4799
4800 #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
4801
4802 static int floppy_request_regions(int fdc)
4803 {
4804 const struct io_region *p;
4805
4806 for (p = io_regions; p < ARRAY_END(io_regions); p++) {
4807 if (!request_region(fdc_state[fdc].address + p->offset,
4808 p->size, "floppy")) {
4809 DPRINT("Floppy io-port 0x%04lx in use\n",
4810 fdc_state[fdc].address + p->offset);
4811 floppy_release_allocated_regions(fdc, p);
4812 return -EBUSY;
4813 }
4814 }
4815 return 0;
4816 }
4817
4818 static void floppy_release_regions(int fdc)
4819 {
4820 floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
4821 }
4822
4823 static int floppy_grab_irq_and_dma(void)
4824 {
4825 int fdc;
4826
4827 if (atomic_inc_return(&usage_count) > 1)
4828 return 0;
4829
4830
4831
4832
4833
4834 flush_workqueue(floppy_wq);
4835
4836 if (fd_request_irq()) {
4837 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4838 FLOPPY_IRQ);
4839 atomic_dec(&usage_count);
4840 return -1;
4841 }
4842 if (fd_request_dma()) {
4843 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4844 FLOPPY_DMA);
4845 if (can_use_virtual_dma & 2)
4846 use_virtual_dma = can_use_virtual_dma = 1;
4847 if (!(can_use_virtual_dma & 1)) {
4848 fd_free_irq();
4849 atomic_dec(&usage_count);
4850 return -1;
4851 }
4852 }
4853
4854 for (fdc = 0; fdc < N_FDC; fdc++) {
4855 if (fdc_state[fdc].address != -1) {
4856 if (floppy_request_regions(fdc))
4857 goto cleanup;
4858 }
4859 }
4860 for (fdc = 0; fdc < N_FDC; fdc++) {
4861 if (fdc_state[fdc].address != -1) {
4862 reset_fdc_info(fdc, 1);
4863 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR);
4864 }
4865 }
4866
4867 set_dor(0, ~0, 8);
4868
4869 for (fdc = 0; fdc < N_FDC; fdc++)
4870 if (fdc_state[fdc].address != -1)
4871 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR);
4872
4873
4874
4875
4876 current_fdc = 0;
4877 irqdma_allocated = 1;
4878 return 0;
4879 cleanup:
4880 fd_free_irq();
4881 fd_free_dma();
4882 while (--fdc >= 0)
4883 floppy_release_regions(fdc);
4884 current_fdc = 0;
4885 atomic_dec(&usage_count);
4886 return -1;
4887 }
4888
4889 static void floppy_release_irq_and_dma(void)
4890 {
4891 int fdc;
4892 #ifndef __sparc__
4893 int drive;
4894 #endif
4895 long tmpsize;
4896 unsigned long tmpaddr;
4897
4898 if (!atomic_dec_and_test(&usage_count))
4899 return;
4900
4901 if (irqdma_allocated) {
4902 fd_disable_dma();
4903 fd_free_dma();
4904 fd_free_irq();
4905 irqdma_allocated = 0;
4906 }
4907 set_dor(0, ~0, 8);
4908 #if N_FDC > 1
4909 set_dor(1, ~8, 0);
4910 #endif
4911
4912 if (floppy_track_buffer && max_buffer_sectors) {
4913 tmpsize = max_buffer_sectors * 1024;
4914 tmpaddr = (unsigned long)floppy_track_buffer;
4915 floppy_track_buffer = NULL;
4916 max_buffer_sectors = 0;
4917 buffer_min = buffer_max = -1;
4918 fd_dma_mem_free(tmpaddr, tmpsize);
4919 }
4920 #ifndef __sparc__
4921 for (drive = 0; drive < N_FDC * 4; drive++)
4922 if (timer_pending(motor_off_timer + drive))
4923 pr_info("motor off timer %d still active\n", drive);
4924 #endif
4925
4926 if (delayed_work_pending(&fd_timeout))
4927 pr_info("floppy timer still active:%s\n", timeout_message);
4928 if (delayed_work_pending(&fd_timer))
4929 pr_info("auxiliary floppy timer still active\n");
4930 if (work_pending(&floppy_work))
4931 pr_info("work still pending\n");
4932 for (fdc = 0; fdc < N_FDC; fdc++)
4933 if (fdc_state[fdc].address != -1)
4934 floppy_release_regions(fdc);
4935 }
4936
4937 #ifdef MODULE
4938
4939 static char *floppy;
4940
4941 static void __init parse_floppy_cfg_string(char *cfg)
4942 {
4943 char *ptr;
4944
4945 while (*cfg) {
4946 ptr = cfg;
4947 while (*cfg && *cfg != ' ' && *cfg != '\t')
4948 cfg++;
4949 if (*cfg) {
4950 *cfg = '\0';
4951 cfg++;
4952 }
4953 if (*ptr)
4954 floppy_setup(ptr);
4955 }
4956 }
4957
4958 static int __init floppy_module_init(void)
4959 {
4960 if (floppy)
4961 parse_floppy_cfg_string(floppy);
4962 return floppy_init();
4963 }
4964 module_init(floppy_module_init);
4965
4966 static void __exit floppy_module_exit(void)
4967 {
4968 int drive, i;
4969
4970 unregister_blkdev(FLOPPY_MAJOR, "fd");
4971 platform_driver_unregister(&floppy_driver);
4972
4973 destroy_workqueue(floppy_wq);
4974
4975 for (drive = 0; drive < N_DRIVE; drive++) {
4976 del_timer_sync(&motor_off_timer[drive]);
4977
4978 if (floppy_available(drive)) {
4979 for (i = 0; i < ARRAY_SIZE(floppy_type); i++) {
4980 if (disks[drive][i])
4981 del_gendisk(disks[drive][i]);
4982 }
4983 if (registered[drive])
4984 platform_device_unregister(&floppy_device[drive]);
4985 }
4986 for (i = 0; i < ARRAY_SIZE(floppy_type); i++) {
4987 if (disks[drive][i])
4988 put_disk(disks[drive][i]);
4989 }
4990 blk_mq_free_tag_set(&tag_sets[drive]);
4991 }
4992
4993 cancel_delayed_work_sync(&fd_timeout);
4994 cancel_delayed_work_sync(&fd_timer);
4995
4996 if (atomic_read(&usage_count))
4997 floppy_release_irq_and_dma();
4998
4999
5000 fd_eject(0);
5001 }
5002
5003 module_exit(floppy_module_exit);
5004
5005 module_param(floppy, charp, 0);
5006 module_param(FLOPPY_IRQ, int, 0);
5007 module_param(FLOPPY_DMA, int, 0);
5008 MODULE_AUTHOR("Alain L. Knaff");
5009 MODULE_LICENSE("GPL");
5010
5011
5012 static const struct pnp_device_id floppy_pnpids[] = {
5013 {"PNP0700", 0},
5014 {}
5015 };
5016
5017 MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
5018
5019 #else
5020
5021 __setup("floppy=", floppy_setup);
5022 module_init(floppy_init)
5023 #endif
5024
5025 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);