This source file includes following definitions.
- select_callback
- floppy_select
- motor_on_callback
- motor_off_callback
- floppy_on
- floppy_off
- request_done
- floppy_change
- setup_DMA
- output_byte
- result
- bad_flp_intr
- perpendicular_mode
- configure_fdc_mode
- tell_sector
- rw_interrupt
- setup_rw_floppy
- seek_interrupt
- transfer
- recal_interrupt
- unexpected_floppy_interrupt
- recalibrate_floppy
- reset_interrupt
- reset_floppy
- floppy_shutdown
- shake_done
- retry_recal
- shake_zero
- shake_one
- floppy_ready
- setup_format_params
- redo_fd_request
- do_fd_request
- fd_ioctl
- find_base
- config_types
- floppy_open
- floppy_release
- ignore_interrupt
- floppy_interrupt
- floppy_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 #define REALLY_SLOW_IO
68 #define FLOPPY_IRQ 6
69 #define FLOPPY_DMA 2
70
71 #include <linux/sched.h>
72 #include <linux/fs.h>
73 #include <linux/kernel.h>
74 #include <linux/timer.h>
75 #include <linux/fdreg.h>
76 #include <linux/fd.h>
77 #include <linux/errno.h>
78
79 #include <asm/dma.h>
80 #include <asm/system.h>
81 #include <asm/io.h>
82 #include <asm/segment.h>
83
84 #define MAJOR_NR FLOPPY_MAJOR
85 #include "blk.h"
86
87 static unsigned int changed_floppies = 0, fake_change = 0;
88
89 static int initial_reset_flag = 0;
90 static int need_configure = 1;
91 static int recalibrate = 0;
92 static int reset = 0;
93 static int recover = 0;
94 static int seek = 0;
95
96 static unsigned char current_DOR = 0x0C;
97 static unsigned char running = 0;
98
99 #define TYPE(x) ((x)>>2)
100 #define DRIVE(x) ((x)&0x03)
101
102
103
104
105
106
107 #define MAX_ERRORS 12
108
109
110
111
112
113 #define MAX_DISK_SIZE 1440
114
115
116
117
118
119 #define MAX_BUFFER_SECTORS 18
120
121
122
123
124
125
126
127
128
129 #define LAST_DMA_ADDR (0x100000 - BLOCK_SIZE)
130
131
132
133
134 #define MAX_REPLIES 7
135 static unsigned char reply_buffer[MAX_REPLIES];
136 #define ST0 (reply_buffer[0])
137 #define ST1 (reply_buffer[1])
138 #define ST2 (reply_buffer[2])
139 #define ST3 (reply_buffer[3])
140
141
142
143
144
145
146
147
148 static struct floppy_struct floppy_type[] = {
149 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL },
150 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,NULL },
151 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,NULL },
152 { 720, 9,2,40,1,0x2A,0x02,0xDF,0x50,NULL },
153 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,NULL },
154 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,NULL },
155 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,NULL },
156 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL },
157 };
158
159
160
161
162
163
164 static struct floppy_struct floppy_types[] = {
165 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"360k/PC" },
166 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"360k/PC" },
167 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"1.2M" },
168 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"360k/AT" },
169 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k" },
170 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k" },
171 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"1.44M" },
172 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k/AT" },
173 };
174
175
176 struct floppy_struct *current_type[4] = { NULL, NULL, NULL, NULL };
177
178
179 struct floppy_struct *base_type[4];
180
181
182
183
184
185 struct floppy_struct user_params[4];
186
187 static int floppy_sizes[] ={
188 MAX_DISK_SIZE, MAX_DISK_SIZE, MAX_DISK_SIZE, MAX_DISK_SIZE,
189 360, 360 ,360, 360,
190 1200,1200,1200,1200,
191 360, 360, 360, 360,
192 720, 720, 720, 720,
193 360, 360, 360, 360,
194 720, 720, 720, 720,
195 1440,1440,1440,1440
196 };
197
198
199
200
201
202
203 static int probing = 0;
204
205
206
207
208
209
210 static int keep_data[4] = { 0,0,0,0 };
211
212
213
214
215
216
217 static ftd_msg[4] = { 0,0,0,0 };
218
219
220
221 static fd_ref[4] = { 0,0,0,0 };
222 static fd_device[4] = { 0,0,0,0 };
223
224
225 static volatile int format_status = FORMAT_NONE, fdc_busy = 0;
226 static struct wait_queue *fdc_wait = NULL, *format_done = NULL;
227
228
229 static int format_errors;
230
231
232 static struct format_descr format_req;
233
234
235
236
237
238 #define CURRENT_DEVICE (format_status == FORMAT_BUSY ? format_req.device : \
239 (CURRENT->dev))
240
241
242 #define CURRENT_ERRORS (format_status == FORMAT_BUSY ? format_errors : \
243 (CURRENT->errors))
244
245
246
247
248
249
250 static unsigned short min_report_error_cnt[4] = {2, 2, 2, 2};
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 extern char tmp_floppy_area[BLOCK_SIZE];
269 extern char floppy_track_buffer[512*2*MAX_BUFFER_SECTORS];
270
271 static void redo_fd_request(void);
272
273
274
275
276
277
278 #define NO_TRACK 255
279
280 static int read_track = 0;
281 static int buffer_track = -1;
282 static int buffer_drive = -1;
283 static int cur_spec1 = -1;
284 static int cur_rate = -1;
285 static struct floppy_struct * floppy = floppy_type;
286 static unsigned char current_drive = 255;
287 static unsigned char sector = 0;
288 static unsigned char head = 0;
289 static unsigned char track = 0;
290 static unsigned char seek_track = 0;
291 static unsigned char current_track = NO_TRACK;
292 static unsigned char command = 0;
293 static unsigned char fdc_version = FDC_TYPE_STD;
294
295 static void floppy_ready(void);
296
297 static void select_callback(unsigned long unused)
298 {
299 floppy_ready();
300 }
301
302 static void floppy_select(unsigned int nr)
303 {
304 static struct timer_list select = { NULL, 0, 0, select_callback };
305
306 if (current_drive == (current_DOR & 3)) {
307 floppy_ready();
308 return;
309 }
310 seek = 1;
311 current_track = NO_TRACK;
312 current_DOR &= 0xFC;
313 current_DOR |= current_drive;
314 outb(current_DOR,FD_DOR);
315 del_timer(&select);
316 select.expires = 2;
317 add_timer(&select);
318 }
319
320 static void motor_on_callback(unsigned long nr)
321 {
322 running |= 0x10 << nr;
323 floppy_select(nr);
324 }
325
326 static struct timer_list motor_on_timer[4] = {
327 { NULL, 0, 0, motor_on_callback },
328 { NULL, 0, 1, motor_on_callback },
329 { NULL, 0, 2, motor_on_callback },
330 { NULL, 0, 3, motor_on_callback }
331 };
332
333 static void motor_off_callback(unsigned long nr)
334 {
335 unsigned char mask = ~(0x10 << nr);
336 cli();
337 running &= mask;
338 current_DOR &= mask;
339 outb(current_DOR,FD_DOR);
340 sti();
341 }
342
343 static struct timer_list motor_off_timer[4] = {
344 { NULL, 0, 0, motor_off_callback },
345 { NULL, 0, 1, motor_off_callback },
346 { NULL, 0, 2, motor_off_callback },
347 { NULL, 0, 3, motor_off_callback }
348 };
349
350 static void floppy_on(unsigned int nr)
351 {
352 unsigned char mask = 0x10 << nr;
353
354 del_timer(motor_off_timer + nr);
355 if (mask & running)
356 floppy_select(nr);
357 if (!(mask & current_DOR)) {
358 del_timer(motor_on_timer + nr);
359 motor_on_timer[nr].expires = HZ;
360 add_timer(motor_on_timer + nr);
361 }
362 current_DOR &= 0xFC;
363 current_DOR |= mask;
364 current_DOR |= nr;
365 outb(current_DOR,FD_DOR);
366 }
367
368 static void floppy_off(unsigned int nr)
369 {
370 del_timer(motor_off_timer+nr);
371 motor_off_timer[nr].expires = 3*HZ;
372 add_timer(motor_off_timer+nr);
373 }
374
375 void request_done(int uptodate)
376 {
377 timer_active &= ~(1 << FLOPPY_TIMER);
378 if (format_status != FORMAT_BUSY)
379 end_request(uptodate);
380 else {
381 format_status = uptodate ? FORMAT_OKAY : FORMAT_ERROR;
382 wake_up(&format_done);
383 }
384 }
385
386
387
388
389
390
391
392 int floppy_change(struct buffer_head * bh)
393 {
394 unsigned int mask = 1 << (bh->b_dev & 0x03);
395
396 if (MAJOR(bh->b_dev) != MAJOR_NR) {
397 printk("floppy_changed: not a floppy\n");
398 return 0;
399 }
400 if (fake_change & mask) {
401 buffer_track = -1;
402 fake_change &= ~mask;
403
404 changed_floppies &= ~mask;
405 return 1;
406 }
407 if (changed_floppies & mask) {
408 buffer_track = -1;
409 changed_floppies &= ~mask;
410 recalibrate = 1;
411 return 1;
412 }
413 if (!bh)
414 return 0;
415 if (bh->b_dirt)
416 ll_rw_block(WRITE, 1, &bh);
417 else {
418 buffer_track = -1;
419 bh->b_uptodate = 0;
420 ll_rw_block(READ, 1, &bh);
421 }
422 wait_on_buffer(bh);
423 if (changed_floppies & mask) {
424 changed_floppies &= ~mask;
425 recalibrate = 1;
426 return 1;
427 }
428 return 0;
429 }
430
431 #define copy_buffer(from,to) \
432 __asm__("cld ; rep ; movsl" \
433 : \
434 :"c" (BLOCK_SIZE/4),"S" ((long)(from)),"D" ((long)(to)) \
435 :"cx","di","si")
436
437 static void setup_DMA(void)
438 {
439 unsigned long addr,count;
440 unsigned char dma_code;
441
442 dma_code = DMA_WRITE;
443 if (command == FD_READ)
444 dma_code = DMA_READ;
445 if (command == FD_FORMAT) {
446 addr = (long) tmp_floppy_area;
447 count = floppy->sect*4;
448 } else {
449 addr = (long) CURRENT->buffer;
450 count = 1024;
451 }
452 if (read_track) {
453
454 buffer_drive = buffer_track = -1;
455 count = floppy->sect*floppy->head*512;
456 addr = (long) floppy_track_buffer;
457 } else if (addr >= LAST_DMA_ADDR) {
458 addr = (long) tmp_floppy_area;
459 if (command == FD_WRITE)
460 copy_buffer(CURRENT->buffer,tmp_floppy_area);
461 }
462 cli();
463 disable_dma(FLOPPY_DMA);
464 clear_dma_ff(FLOPPY_DMA);
465 set_dma_mode(FLOPPY_DMA, (command == FD_READ)? DMA_MODE_READ : DMA_MODE_WRITE);
466 set_dma_addr(FLOPPY_DMA, addr);
467 set_dma_count(FLOPPY_DMA, count);
468 enable_dma(FLOPPY_DMA);
469 sti();
470 }
471
472 static void output_byte(char byte)
473 {
474 int counter;
475 unsigned char status;
476
477 if (reset)
478 return;
479 for(counter = 0 ; counter < 10000 ; counter++) {
480 status = inb_p(FD_STATUS) & (STATUS_READY | STATUS_DIR);
481 if (status == STATUS_READY) {
482 outb(byte,FD_DATA);
483 return;
484 }
485 }
486 current_track = NO_TRACK;
487 reset = 1;
488 printk("Unable to send byte to FDC\n");
489 }
490
491 static int result(void)
492 {
493 int i = 0, counter, status;
494
495 if (reset)
496 return -1;
497 for (counter = 0 ; counter < 10000 ; counter++) {
498 status = inb_p(FD_STATUS)&(STATUS_DIR|STATUS_READY|STATUS_BUSY);
499 if (status == STATUS_READY) {
500 return i;
501 }
502 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY)) {
503 if (i >= MAX_REPLIES) {
504 printk("floppy_stat reply overrun\n");
505 break;
506 }
507 reply_buffer[i++] = inb_p(FD_DATA);
508 }
509 }
510 reset = 1;
511 current_track = NO_TRACK;
512 printk("Getstatus times out\n");
513 return -1;
514 }
515
516 static void bad_flp_intr(void)
517 {
518 int errors;
519
520 current_track = NO_TRACK;
521 if (format_status == FORMAT_BUSY)
522 errors = ++format_errors;
523 else if (!CURRENT) {
524 printk(DEVICE_NAME ": no current request\n");
525 reset = recalibrate = 1;
526 return;
527 } else
528 errors = ++CURRENT->errors;
529 if (errors > MAX_ERRORS) {
530 request_done(0);
531 }
532 if (errors > MAX_ERRORS/2)
533 reset = 1;
534 else
535 recalibrate = 1;
536 }
537
538
539
540
541
542
543 static inline void perpendicular_mode(unsigned char rate)
544 {
545 if (fdc_version == FDC_TYPE_82077) {
546 output_byte(FD_PERPENDICULAR);
547 if (rate & 0x40) {
548 unsigned char r = rate & 0x03;
549 if (r == 0)
550 output_byte(2);
551 else if (r == 3)
552 output_byte(3);
553 else {
554 printk(DEVICE_NAME ": Invalid data rate for perpendicular mode!\n");
555 reset = 1;
556 }
557 } else
558 output_byte(0);
559 } else {
560 if (rate & 0x40) {
561 printk(DEVICE_NAME ": perpendicular mode not supported by this FDC.\n");
562 reset = 1;
563 }
564 }
565 }
566
567
568
569
570
571
572
573
574 static void configure_fdc_mode(void)
575 {
576 if (need_configure && (fdc_version == FDC_TYPE_82077)) {
577
578 output_byte(FD_CONFIGURE);
579 output_byte(0);
580 output_byte(0x1A);
581 output_byte(0);
582 need_configure = 0;
583 printk(DEVICE_NAME ": FIFO enabled\n");
584 }
585 if (cur_spec1 != floppy->spec1) {
586 cur_spec1 = floppy->spec1;
587 output_byte(FD_SPECIFY);
588 output_byte(cur_spec1);
589 output_byte(6);
590 }
591 if (cur_rate != floppy->rate) {
592
593 perpendicular_mode(floppy->rate);
594 outb_p((cur_rate = (floppy->rate)) & ~0x40, FD_DCR);
595 }
596 }
597
598
599 static void tell_sector(int nr)
600 {
601 if (nr!=7) {
602 printk(" -- FDC reply errror");
603 reset = 1;
604 } else
605 printk(": track %d, head %d, sector %d", reply_buffer[3],
606 reply_buffer[4], reply_buffer[5]);
607 }
608
609
610
611
612
613
614
615 static void rw_interrupt(void)
616 {
617 char * buffer_area;
618 int nr;
619 char bad;
620
621 nr = result();
622
623 switch ((ST0 & ST0_INTR)>>6) {
624 case 1:
625 bad = 1;
626 if (ST1 & ST1_WP) {
627 printk(DEVICE_NAME ": Drive %d is write protected\n", current_drive);
628 request_done(0);
629 bad = 0;
630 } else if (ST1 & ST1_OR) {
631 if (ftd_msg[ST0 & ST0_DS])
632 printk(DEVICE_NAME ": Over/Underrun - retrying\n");
633
634 bad = 0;
635 } else if (CURRENT_ERRORS > min_report_error_cnt[ST0 & ST0_DS]) {
636 printk(DEVICE_NAME " %d: ", ST0 & ST0_DS);
637 if (ST0 & ST0_ECE) {
638 printk("Recalibrate failed!");
639 } else if (ST2 & ST2_CRC) {
640 printk("data CRC error");
641 tell_sector(nr);
642 } else if (ST1 & ST1_CRC) {
643 printk("CRC error");
644 tell_sector(nr);
645 } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
646 if (!probing) {
647 printk("sector not found");
648 tell_sector(nr);
649 } else
650 printk("probe failed...");
651 } else if (ST2 & ST2_WC) {
652 printk("wrong cylinder");
653 } else if (ST2 & ST2_BC) {
654 printk("bad cylinder");
655 } else {
656 printk("unknown error. ST[0..3] are: 0x%x 0x%x 0x%x 0x%x\n", ST0, ST1, ST2, ST3);
657 }
658 printk("\n");
659
660 }
661 if (bad)
662 bad_flp_intr();
663 redo_fd_request();
664 return;
665 case 2:
666 printk(DEVICE_NAME ": Invalid FDC command given!\n");
667 request_done(0);
668 return;
669 case 3:
670 printk(DEVICE_NAME ": Abnormal termination caused by polling\n");
671 bad_flp_intr();
672 redo_fd_request();
673 return;
674 default:
675 break;
676 }
677
678 if (probing) {
679 int drive = MINOR(CURRENT->dev);
680
681 if (ftd_msg[drive])
682 printk("Auto-detected floppy type %s in fd%d\n",
683 floppy->name,drive);
684 current_type[drive] = floppy;
685 floppy_sizes[drive] = floppy->size >> 1;
686 probing = 0;
687 }
688 if (read_track) {
689 buffer_track = seek_track;
690 buffer_drive = current_drive;
691 buffer_area = floppy_track_buffer +
692 ((sector-1 + head*floppy->sect)<<9);
693 copy_buffer(buffer_area,CURRENT->buffer);
694 } else if (command == FD_READ &&
695 (unsigned long)(CURRENT->buffer) >= LAST_DMA_ADDR)
696 copy_buffer(tmp_floppy_area,CURRENT->buffer);
697 request_done(1);
698 redo_fd_request();
699 }
700
701
702
703
704
705
706
707
708 inline void setup_rw_floppy(void)
709 {
710 setup_DMA();
711 do_floppy = rw_interrupt;
712 output_byte(command);
713 if (command != FD_FORMAT) {
714 if (read_track) {
715 output_byte(current_drive);
716 output_byte(track);
717 output_byte(0);
718 output_byte(1);
719 } else {
720 output_byte(head<<2 | current_drive);
721 output_byte(track);
722 output_byte(head);
723 output_byte(sector);
724 }
725 output_byte(2);
726 output_byte(floppy->sect);
727 output_byte(floppy->gap);
728 output_byte(0xFF);
729 } else {
730 output_byte(head<<2 | current_drive);
731 output_byte(2);
732 output_byte(floppy->sect);
733 output_byte(floppy->fmt_gap);
734 output_byte(FD_FILL_BYTE);
735 }
736 if (reset)
737 redo_fd_request();
738 }
739
740
741
742
743
744
745 static void seek_interrupt(void)
746 {
747
748 output_byte(FD_SENSEI);
749 if (result() != 2 || (ST0 & 0xF8) != 0x20 || ST1 != seek_track) {
750 printk(DEVICE_NAME ": seek failed\n");
751 recalibrate = 1;
752 bad_flp_intr();
753 redo_fd_request();
754 return;
755 }
756 current_track = ST1;
757 setup_rw_floppy();
758 }
759
760
761
762
763
764
765
766 static void transfer(void)
767 {
768 read_track = (command == FD_READ) && (CURRENT_ERRORS < 4) &&
769 (floppy->sect <= MAX_BUFFER_SECTORS);
770
771 configure_fdc_mode();
772
773 if (reset) {
774 redo_fd_request();
775 return;
776 }
777 if (!seek) {
778 setup_rw_floppy();
779 return;
780 }
781
782 do_floppy = seek_interrupt;
783 output_byte(FD_SEEK);
784 if (read_track)
785 output_byte(current_drive);
786 else
787 output_byte((head<<2) | current_drive);
788 output_byte(seek_track);
789 if (reset)
790 redo_fd_request();
791 }
792
793
794
795
796
797 static void recalibrate_floppy(void);
798
799 static void recal_interrupt(void)
800 {
801 output_byte(FD_SENSEI);
802 current_track = NO_TRACK;
803 if (result()!=2 || (ST0 & 0xE0) == 0x60)
804 reset = 1;
805
806 if ((ST0 & 0x10) == 0x10)
807 recalibrate_floppy();
808 else
809 redo_fd_request();
810 }
811
812 static void unexpected_floppy_interrupt(void)
813 {
814 current_track = NO_TRACK;
815 output_byte(FD_SENSEI);
816 printk(DEVICE_NAME ": unexpected interrupt\n");
817 if (result()!=2 || (ST0 & 0xE0) == 0x60)
818 reset = 1;
819 else
820 recalibrate = 1;
821 }
822
823 static void recalibrate_floppy(void)
824 {
825 recalibrate = 0;
826 current_track = 0;
827 do_floppy = recal_interrupt;
828 output_byte(FD_RECALIBRATE);
829 output_byte(head<<2 | current_drive);
830 if (reset)
831 redo_fd_request();
832 }
833
834
835
836
837 static void reset_interrupt(void)
838 {
839 short i;
840
841 for (i=0; i<4; i++) {
842 output_byte(FD_SENSEI);
843 (void) result();
844 }
845 output_byte(FD_SPECIFY);
846 output_byte(cur_spec1);
847 output_byte(6);
848 configure_fdc_mode();
849 if (initial_reset_flag) {
850 initial_reset_flag = 0;
851 recalibrate = 1;
852 reset = 0;
853 return;
854 }
855 if (!recover)
856 redo_fd_request();
857 else {
858 recalibrate_floppy();
859 recover = 0;
860 }
861 }
862
863
864
865
866 static void reset_floppy(void)
867 {
868 int i;
869
870 do_floppy = reset_interrupt;
871 reset = 0;
872 current_track = NO_TRACK;
873 cur_spec1 = -1;
874 cur_rate = -1;
875 recalibrate = 1;
876 need_configure = 1;
877 if (!initial_reset_flag)
878 printk("Reset-floppy called\n");
879 cli();
880 outb_p(current_DOR & ~0x04, FD_DOR);
881 for (i=0 ; i<1000 ; i++)
882 __asm__("nop");
883 outb(current_DOR, FD_DOR);
884 sti();
885 }
886
887 static void floppy_shutdown(void)
888 {
889 cli();
890 do_floppy = NULL;
891 request_done(0);
892 recover = 1;
893 reset_floppy();
894 sti();
895 redo_fd_request();
896 }
897
898 static void shake_done(void)
899 {
900 current_track = NO_TRACK;
901 if (inb(FD_DIR) & 0x80)
902 request_done(0);
903 redo_fd_request();
904 }
905
906 static int retry_recal(void (*proc)(void))
907 {
908 output_byte(FD_SENSEI);
909 if (result() == 2 && (ST0 & 0x10) != 0x10) return 0;
910 do_floppy = proc;
911 output_byte(FD_RECALIBRATE);
912 output_byte(head<<2 | current_drive);
913 return 1;
914 }
915
916 static void shake_zero(void)
917 {
918 if (!retry_recal(shake_zero)) shake_done();
919 }
920
921 static void shake_one(void)
922 {
923 if (retry_recal(shake_one)) return;
924 do_floppy = shake_done;
925 output_byte(FD_SEEK);
926 output_byte(head << 2 | current_drive);
927 output_byte(1);
928 }
929
930 static void floppy_ready(void)
931 {
932 if (inb(FD_DIR) & 0x80) {
933 changed_floppies |= 1<<current_drive;
934 buffer_track = -1;
935 if (keep_data[current_drive]) {
936 if (keep_data[current_drive] > 0)
937 keep_data[current_drive]--;
938 } else {
939 if (ftd_msg[current_drive] && current_type[current_drive] != NULL)
940 printk("Disk type is undefined after disk "
941 "change in fd%d\n",current_drive);
942 current_type[current_drive] = NULL;
943 floppy_sizes[current_drive] = MAX_DISK_SIZE;
944 }
945
946
947
948 if (!reset && !recalibrate) {
949 if (current_track && current_track != NO_TRACK)
950 do_floppy = shake_zero;
951 else
952 do_floppy = shake_one;
953 output_byte(FD_RECALIBRATE);
954 output_byte(head<<2 | current_drive);
955 return;
956 }
957 }
958 if (reset) {
959 reset_floppy();
960 return;
961 }
962 if (recalibrate) {
963 recalibrate_floppy();
964 return;
965 }
966 transfer();
967 }
968
969 static void setup_format_params(void)
970 {
971 unsigned char *here = (unsigned char *) tmp_floppy_area;
972 int count,head_shift,track_shift,total_shift;
973
974
975 head_shift = floppy->sect / 6;
976
977 track_shift = 2 * head_shift + 1;
978
979 total_shift = floppy->sect -
980 ((track_shift * track + head_shift * head) % floppy->sect);
981
982
983 for (count = 0; count < floppy->sect; count++) {
984 *here++ = track;
985 *here++ = head;
986 *here++ = 1 + (( count + total_shift ) % floppy->sect);
987 *here++ = 2;
988 }
989 }
990
991 static void redo_fd_request(void)
992 {
993 unsigned int block;
994 char * buffer_area;
995 int device;
996
997 if (CURRENT && CURRENT->dev < 0) return;
998
999 repeat:
1000 if (format_status == FORMAT_WAIT)
1001 format_status = FORMAT_BUSY;
1002 if (format_status != FORMAT_BUSY) {
1003 if (!CURRENT) {
1004 if (!fdc_busy)
1005 printk("FDC access conflict!");
1006 fdc_busy = 0;
1007 wake_up(&fdc_wait);
1008 CLEAR_INTR;
1009 return;
1010 }
1011 if (MAJOR(CURRENT->dev) != MAJOR_NR)
1012 panic(DEVICE_NAME ": request list destroyed"); \
1013 if (CURRENT->bh) {
1014 if (!CURRENT->bh->b_lock)
1015 panic(DEVICE_NAME ": block not locked");
1016 }
1017 }
1018 seek = 0;
1019 probing = 0;
1020 device = MINOR(CURRENT_DEVICE);
1021 if (device > 3)
1022 floppy = (device >> 2) + floppy_type;
1023 else {
1024 floppy = current_type[device & 3];
1025 if (!floppy) {
1026 probing = 1;
1027 floppy = base_type[device & 3];
1028 if (!floppy) {
1029 request_done(0);
1030 goto repeat;
1031 }
1032 if (CURRENT_ERRORS & 1)
1033 floppy++;
1034 }
1035 }
1036 if (format_status != FORMAT_BUSY) {
1037 if (current_drive != CURRENT_DEV) {
1038 current_track = NO_TRACK;
1039 current_drive = CURRENT_DEV;
1040 }
1041 block = CURRENT->sector;
1042 if (block+2 > floppy->size) {
1043 request_done(0);
1044 goto repeat;
1045 }
1046 sector = block % floppy->sect;
1047 block /= floppy->sect;
1048 head = block % floppy->head;
1049 track = block / floppy->head;
1050 seek_track = track << floppy->stretch;
1051 if (CURRENT->cmd == READ)
1052 command = FD_READ;
1053 else if (CURRENT->cmd == WRITE)
1054 command = FD_WRITE;
1055 else {
1056 printk("do_fd_request: unknown command\n");
1057 request_done(0);
1058 goto repeat;
1059 }
1060 } else {
1061 if (current_drive != (format_req.device & 3))
1062 current_track = NO_TRACK;
1063 current_drive = format_req.device & 3;
1064 if (((unsigned) format_req.track) >= floppy->track ||
1065 (format_req.head & 0xfffe) || probing) {
1066 request_done(0);
1067 goto repeat;
1068 }
1069 head = format_req.head;
1070 track = format_req.track;
1071 seek_track = track << floppy->stretch;
1072 if (seek_track == buffer_track) buffer_track = -1;
1073 command = FD_FORMAT;
1074 setup_format_params();
1075 }
1076 timer_table[FLOPPY_TIMER].expires = jiffies+10*HZ;
1077 timer_active |= 1 << FLOPPY_TIMER;
1078 if ((seek_track == buffer_track) &&
1079 (current_drive == buffer_drive)) {
1080 buffer_area = floppy_track_buffer +
1081 ((sector + head*floppy->sect)<<9);
1082 if (command == FD_READ) {
1083 copy_buffer(buffer_area,CURRENT->buffer);
1084 request_done(1);
1085 goto repeat;
1086 } else if (command == FD_WRITE)
1087 copy_buffer(CURRENT->buffer,buffer_area);
1088 }
1089 if (seek_track != current_track)
1090 seek = 1;
1091 sector++;
1092 del_timer(motor_off_timer + current_drive);
1093 floppy_on(current_drive);
1094 }
1095
1096 void do_fd_request(void)
1097 {
1098 cli();
1099 while (fdc_busy) sleep_on(&fdc_wait);
1100 fdc_busy = 1;
1101 sti();
1102 redo_fd_request();
1103 }
1104
1105 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1106 unsigned long param)
1107 {
1108 int i,drive,cnt,okay;
1109 struct floppy_struct *this_floppy;
1110
1111 switch (cmd) {
1112 RO_IOCTLS(inode->i_rdev,param);
1113 }
1114 drive = MINOR(inode->i_rdev);
1115 switch (cmd) {
1116 case FDFMTBEG:
1117 if (!suser())
1118 return -EPERM;
1119 return 0;
1120 case FDFMTEND:
1121 if (!suser())
1122 return -EPERM;
1123 cli();
1124 fake_change |= 1 << (drive & 3);
1125 sti();
1126 drive &= 3;
1127 cmd = FDCLRPRM;
1128 break;
1129 case FDGETPRM:
1130 if (drive > 3) this_floppy = &floppy_type[drive >> 2];
1131 else if ((this_floppy = current_type[drive & 3]) == NULL)
1132 return -ENODEV;
1133 i = verify_area(VERIFY_WRITE,(void *) param,sizeof(struct floppy_struct));
1134 if (i)
1135 return i;
1136 for (cnt = 0; cnt < sizeof(struct floppy_struct); cnt++)
1137 put_fs_byte(((char *) this_floppy)[cnt],
1138 (char *) param+cnt);
1139 return 0;
1140 case FDFMTTRK:
1141 if (!suser())
1142 return -EPERM;
1143 if (fd_ref[drive & 3] != 1)
1144 return -EBUSY;
1145 cli();
1146 while (format_status != FORMAT_NONE)
1147 sleep_on(&format_done);
1148 for (cnt = 0; cnt < sizeof(struct format_descr); cnt++)
1149 ((char *) &format_req)[cnt] = get_fs_byte(
1150 (char *) param+cnt);
1151 format_req.device = drive;
1152 format_status = FORMAT_WAIT;
1153 format_errors = 0;
1154 while (format_status != FORMAT_OKAY && format_status !=
1155 FORMAT_ERROR) {
1156 if (fdc_busy) sleep_on(&fdc_wait);
1157 else {
1158 fdc_busy = 1;
1159 redo_fd_request();
1160 }
1161 }
1162 while (format_status != FORMAT_OKAY && format_status !=
1163 FORMAT_ERROR)
1164 sleep_on(&format_done);
1165 sti();
1166 okay = format_status == FORMAT_OKAY;
1167 format_status = FORMAT_NONE;
1168 floppy_off(drive & 3);
1169 wake_up(&format_done);
1170 return okay ? 0 : -EIO;
1171 case FDFLUSH:
1172 if (!permission(inode, 2))
1173 return -EPERM;
1174 cli();
1175 fake_change |= 1 << (drive & 3);
1176 sti();
1177 check_disk_change(inode->i_rdev);
1178 return 0;
1179 }
1180 if (!suser())
1181 return -EPERM;
1182 if (drive < 0 || drive > 3)
1183 return -EINVAL;
1184 switch (cmd) {
1185 case FDCLRPRM:
1186 current_type[drive] = NULL;
1187 floppy_sizes[drive] = MAX_DISK_SIZE;
1188 keep_data[drive] = 0;
1189 break;
1190 case FDSETPRM:
1191 case FDDEFPRM:
1192 memcpy_fromfs(user_params+drive,
1193 (void *) param,
1194 sizeof(struct floppy_struct));
1195 current_type[drive] = &user_params[drive];
1196 floppy_sizes[drive] = user_params[drive].size >> 1;
1197 if (cmd == FDDEFPRM)
1198 keep_data[drive] = -1;
1199 else {
1200 cli();
1201 while (fdc_busy) sleep_on(&fdc_wait);
1202 fdc_busy = 1;
1203 sti();
1204 outb_p((current_DOR & 0xfc) | drive |
1205 (0x10 << drive),FD_DOR);
1206 for (cnt = 0; cnt < 1000; cnt++) __asm__("nop");
1207 if (inb(FD_DIR) & 0x80)
1208 keep_data[drive] = 1;
1209 else
1210 keep_data[drive] = 0;
1211 outb_p(current_DOR,FD_DOR);
1212 fdc_busy = 0;
1213 wake_up(&fdc_wait);
1214 }
1215 break;
1216 case FDMSGON:
1217 ftd_msg[drive] = 1;
1218 break;
1219 case FDMSGOFF:
1220 ftd_msg[drive] = 0;
1221 break;
1222 case FDSETEMSGTRESH:
1223 min_report_error_cnt[drive] = (unsigned short) (param & 0x0f);
1224 break;
1225 default:
1226 return -EINVAL;
1227 }
1228 return 0;
1229 }
1230
1231 #define CMOS_READ(addr) ({ \
1232 outb_p(addr,0x70); \
1233 inb_p(0x71); \
1234 })
1235
1236 static struct floppy_struct *find_base(int drive,int code)
1237 {
1238 struct floppy_struct *base;
1239
1240 if (code > 0 && code < 5) {
1241 base = &floppy_types[(code-1)*2];
1242 printk("fd%d is %s",drive,base->name);
1243 return base;
1244 }
1245 printk("fd%d is unknown type %d",drive,code);
1246 return NULL;
1247 }
1248
1249 static void config_types(void)
1250 {
1251 printk("Floppy drive(s): ");
1252 base_type[0] = find_base(0,(CMOS_READ(0x10) >> 4) & 15);
1253 if (((CMOS_READ(0x14) >> 6) & 1) == 0)
1254 base_type[1] = NULL;
1255 else {
1256 printk(", ");
1257 base_type[1] = find_base(1,CMOS_READ(0x10) & 15);
1258 }
1259 base_type[2] = base_type[3] = NULL;
1260 printk("\n");
1261 }
1262
1263
1264
1265
1266
1267
1268 static int floppy_open(struct inode * inode, struct file * filp)
1269 {
1270 int drive;
1271 int old_dev;
1272
1273 drive = inode->i_rdev & 3;
1274 old_dev = fd_device[drive];
1275 if (fd_ref[drive])
1276 if (old_dev != inode->i_rdev)
1277 return -EBUSY;
1278 fd_ref[drive]++;
1279 fd_device[drive] = inode->i_rdev;
1280 buffer_drive = buffer_track = -1;
1281 if (old_dev && old_dev != inode->i_rdev)
1282 invalidate_buffers(old_dev);
1283 if (filp && filp->f_mode)
1284 check_disk_change(inode->i_rdev);
1285 return 0;
1286 }
1287
1288 static void floppy_release(struct inode * inode, struct file * filp)
1289 {
1290 sync_dev(inode->i_rdev);
1291 if (!fd_ref[inode->i_rdev & 3]--) {
1292 printk("floppy_release with fd_ref == 0");
1293 fd_ref[inode->i_rdev & 3] = 0;
1294 }
1295 }
1296
1297 static struct file_operations floppy_fops = {
1298 NULL,
1299 block_read,
1300 block_write,
1301 NULL,
1302 NULL,
1303 fd_ioctl,
1304 NULL,
1305 floppy_open,
1306 floppy_release,
1307 block_fsync
1308 };
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319 static void ignore_interrupt(void)
1320 {
1321 printk(DEVICE_NAME ": weird interrupt ignored (%d)\n", result());
1322 reset = 1;
1323 CLEAR_INTR;
1324 }
1325
1326
1327 static void floppy_interrupt(int unused)
1328 {
1329 void (*handler)(void) = DEVICE_INTR;
1330
1331 DEVICE_INTR = NULL;
1332 if (!handler)
1333 handler = unexpected_floppy_interrupt;
1334 handler();
1335 }
1336
1337
1338
1339
1340
1341 static struct sigaction floppy_sigaction = {
1342 floppy_interrupt,
1343 0,
1344 SA_INTERRUPT,
1345 NULL
1346 };
1347
1348 void floppy_init(void)
1349 {
1350 outb(current_DOR,FD_DOR);
1351 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
1352 printk("Unable to get major %d for floppy\n",MAJOR_NR);
1353 return;
1354 }
1355 blk_size[MAJOR_NR] = floppy_sizes;
1356 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1357 timer_table[FLOPPY_TIMER].fn = floppy_shutdown;
1358 timer_active &= ~(1 << FLOPPY_TIMER);
1359 config_types();
1360 if (irqaction(FLOPPY_IRQ,&floppy_sigaction))
1361 printk("Unable to grab IRQ%d for the floppy driver\n", FLOPPY_IRQ);
1362 if (request_dma(FLOPPY_DMA))
1363 printk("Unable to grab DMA%d for the floppy driver\n", FLOPPY_DMA);
1364
1365 DEVICE_INTR = ignore_interrupt;
1366 output_byte(FD_VERSION);
1367 if (result() != 1) {
1368 printk(DEVICE_NAME ": FDC failed to return version byte\n");
1369 fdc_version = FDC_TYPE_STD;
1370 } else
1371 fdc_version = reply_buffer[0];
1372 if (fdc_version != FDC_TYPE_STD)
1373 printk(DEVICE_NAME ": FDC version 0x%x\n", fdc_version);
1374 #ifndef FDC_FIFO_UNTESTED
1375 fdc_version = FDC_TYPE_STD;
1376 #endif
1377
1378
1379
1380
1381
1382
1383 if (fdc_version == FDC_TYPE_STD) {
1384 initial_reset_flag = 1;
1385 reset_floppy();
1386 }
1387 }