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