147 #undef FLOPPY_SILENT_DCL_CLEAR
149 #define REALLY_SLOW_IO
153 #define DPRINT(format, args...) \
154 pr_info("floppy%d: " format, current_drive, ##args)
158 #define debug_dcl(test, fmt, args...) \
159 do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
161 #define debug_dcl(test, fmt, args...) \
162 do { if (0) DPRINT(fmt, ##args); } while (0)
166 static int print_unex = 1;
167 #include <linux/module.h>
168 #include <linux/sched.h>
169 #include <linux/fs.h>
170 #include <linux/kernel.h>
175 #include <linux/fd.h>
177 #include <linux/errno.h>
178 #include <linux/slab.h>
181 #include <linux/string.h>
183 #include <linux/fcntl.h>
202 static int slow_floppy;
207 static int FLOPPY_IRQ = 6;
208 static int FLOPPY_DMA = 2;
209 static int can_use_virtual_dma = 2;
218 static int use_virtual_dma;
232 static unsigned short virtual_dma_port = 0x3f0;
234 static int set_dor(
int fdc,
char mask,
char data);
248 static int allowed_drive_mask = 0x33;
250 #include <asm/floppy.h>
252 static int irqdma_allocated;
256 #include <linux/cdrom.h>
261 static int set_next_request(
void);
263 #ifndef fd_get_dma_residue
264 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
269 #ifndef fd_dma_mem_free
270 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
273 #ifndef fd_dma_mem_alloc
274 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
277 static inline void fallback_on_nodma_alloc(
char **
addr,
size_t l)
279 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
282 if (can_use_virtual_dma != 2)
284 pr_info(
"DMA memory shortage. Temporarily falling back on virtual DMA\n");
293 static unsigned long fake_change;
296 #define ITYPE(x) (((x) >> 2) & 0x1f)
297 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
298 #define UNIT(x) ((x) & 0x03)
299 #define FDC(x) (((x) & 0x04) >> 2)
301 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
303 #define DP (&drive_params[current_drive])
304 #define DRS (&drive_state[current_drive])
305 #define DRWE (&write_errors[current_drive])
306 #define FDCS (&fdc_state[fdc])
308 #define UDP (&drive_params[drive])
309 #define UDRS (&drive_state[drive])
310 #define UDRWE (&write_errors[drive])
311 #define UFDCS (&fdc_state[FDC(drive)])
313 #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
314 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
317 #define COMMAND (raw_cmd->cmd[0])
318 #define DR_SELECT (raw_cmd->cmd[1])
319 #define TRACK (raw_cmd->cmd[2])
320 #define HEAD (raw_cmd->cmd[3])
321 #define SECTOR (raw_cmd->cmd[4])
322 #define SIZECODE (raw_cmd->cmd[5])
323 #define SECT_PER_TRACK (raw_cmd->cmd[6])
324 #define GAP (raw_cmd->cmd[7])
325 #define SIZECODE2 (raw_cmd->cmd[8])
329 #define F_SIZECODE (raw_cmd->cmd[2])
330 #define F_SECT_PER_TRACK (raw_cmd->cmd[3])
331 #define F_GAP (raw_cmd->cmd[4])
332 #define F_FILL (raw_cmd->cmd[5])
340 #define MAX_DISK_SIZE 4
345 #define MAX_REPLIES 16
348 #define ST0 (reply_buffer[0])
349 #define ST1 (reply_buffer[1])
350 #define ST2 (reply_buffer[2])
351 #define ST3 (reply_buffer[0])
352 #define R_TRACK (reply_buffer[3])
353 #define R_HEAD (reply_buffer[4])
354 #define R_SECTOR (reply_buffer[5])
355 #define R_SIZECODE (reply_buffer[6])
357 #define SEL_DLY (2 * HZ / 100)
365 } default_drive_params[] = {
381 {{0, 500, 16, 16, 8000, 1*
HZ, 3*
HZ, 0,
SEL_DLY, 5, 80, 3*
HZ, 20, {3,1,2,0,2}, 0,
382 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 },
"unknown" },
384 {{1, 300, 16, 16, 8000, 1*
HZ, 3*
HZ, 0,
SEL_DLY, 5, 40, 3*
HZ, 17, {3,1,2,0,2}, 0,
385 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 },
"360K PC" },
387 {{2, 500, 16, 16, 6000, 4*
HZ/10, 3*
HZ, 14,
SEL_DLY, 6, 83, 3*
HZ, 17, {3,1,2,0,2}, 0,
388 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 },
"1.2M" },
390 {{3, 250, 16, 16, 3000, 1*
HZ, 3*
HZ, 0,
SEL_DLY, 5, 83, 3*
HZ, 20, {3,1,2,0,2}, 0,
391 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 },
"720k" },
393 {{4, 500, 16, 16, 4000, 4*
HZ/10, 3*
HZ, 10,
SEL_DLY, 5, 83, 3*
HZ, 20, {3,1,2,0,2}, 0,
394 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 },
"1.44M" },
396 {{5, 1000, 15, 8, 3000, 4*
HZ/10, 3*
HZ, 10,
SEL_DLY, 5, 83, 3*
HZ, 40, {3,1,2,0,2}, 0,
397 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 },
"2.88M AMI BIOS" },
399 {{6, 1000, 15, 8, 3000, 4*
HZ/10, 3*
HZ, 10,
SEL_DLY, 5, 83, 3*
HZ, 40, {3,1,2,0,2}, 0,
400 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 },
"2.88M" }
411 static struct gendisk *disks[
N_DRIVE];
415 static int fdc_queue;
449 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,
NULL },
450 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,
"d360" },
451 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,
"h1200" },
452 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,
"D360" },
453 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,
"D720" },
454 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,
"h360" },
455 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,
"h720" },
456 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,
"H1440" },
457 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,
"E2880" },
458 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,
"E3120" },
460 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,
"h1440" },
461 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,
"H1680" },
462 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,
"h410" },
463 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,
"H820" },
464 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,
"h1476" },
465 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,
"H1722" },
466 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,
"h420" },
467 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,
"H830" },
468 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,
"h1494" },
469 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,
"H1743" },
471 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,
"h880" },
472 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,
"D1040" },
473 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,
"D1120" },
474 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,
"h1600" },
475 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,
"H1760" },
476 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,
"H1920" },
477 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,
"E3200" },
478 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,
"E3520" },
479 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,
"E3840" },
480 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,
"H1840" },
482 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,
"D800" },
483 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,
"H1600" },
486 #define SECTSIZE (_FD_SECTSIZE(*floppy))
499 static char floppy_device_name[] =
"floppy";
509 #define FD_COMMAND_NONE -1
510 #define FD_COMMAND_ERROR 2
511 #define FD_COMMAND_OKAY 3
514 static unsigned long fdc_busy;
519 static int format_errors;
536 static char *floppy_track_buffer;
537 static int max_buffer_sectors;
541 static const struct cont_t {
551 static void floppy_ready(
void);
552 static void floppy_start(
void);
553 static void process_fd_request(
void);
554 static void recalibrate_floppy(
void);
557 static int floppy_request_regions(
int);
558 static void floppy_release_regions(
int);
559 static int floppy_grab_irq_and_dma(
void);
560 static void floppy_release_irq_and_dma(
void);
569 static void reset_fdc(
void);
577 #define NEED_1_RECAL -2
578 #define NEED_2_RECAL -3
583 static int buffer_track = -1;
584 static int buffer_drive = -1;
585 static int buffer_min = -1;
586 static int buffer_max = -1;
595 static unsigned char current_drive;
596 static long current_count_sectors;
597 static unsigned char fsector_t;
598 static unsigned char in_sector_offset;
601 static inline bool drive_no_geom(
int drive)
603 return !current_type[drive] && !
ITYPE(
UDRS->fd_device);
607 static inline int fd_eject(
int drive)
618 static long unsigned debugtimer;
620 static inline void set_debugt(
void)
625 static inline void debugt(
const char *
func,
const char *
msg)
628 pr_info(
"%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer);
631 static inline void set_debugt(
void) { }
632 static inline void debugt(
const char *func,
const char *msg) { }
637 static const char *timeout_message;
639 static void is_alive(
const char *func,
const char *
message)
644 DPRINT(
"%s: timeout handler died. %s\n", func, message);
653 static unsigned long interruptjiffies;
654 static unsigned long resultjiffies;
655 static int resultsize;
656 static unsigned long lastredo;
658 static struct output_log {
664 static int output_log_pos;
666 #define current_reqD -1
667 #define MAXTIMEOUT -2
669 static void __reschedule_timeout(
int drive,
const char *message)
674 drive = current_drive;
676 if (drive < 0 || drive >=
N_DRIVE) {
680 delay =
UDP->timeout;
684 DPRINT(
"reschedule timeout %s\n", message);
688 static void reschedule_timeout(
int drive,
const char *message)
693 __reschedule_timeout(drive, message);
694 spin_unlock_irqrestore(&floppy_lock, flags);
697 #define INFBOUND(a, b) (a) = max_t(int, a, b)
698 #define SUPBOUND(a, b) (a) = min_t(int, a, b)
733 static int disk_change(
int drive)
735 int fdc =
FDC(drive);
738 DPRINT(
"WARNING disk change called early\n");
739 if (!(
FDCS->dor & (0x10 <<
UNIT(drive))) ||
740 (
FDCS->dor & 3) !=
UNIT(drive) || fdc !=
FDC(drive)) {
741 DPRINT(
"probing disk change on unselected drive\n");
742 DPRINT(
"drive=%d fdc=%d dor=%x\n", drive,
FDC(drive),
743 (
unsigned int)
FDCS->dor);
747 "checking disk change line for drive %d\n", drive);
762 if (
UDRS->keep_data >= 0) {
764 current_type[drive] !=
NULL)
765 DPRINT(
"Disk type is undefined after disk change\n");
766 current_type[drive] =
NULL;
778 static inline int is_selected(
int dor,
int unit)
780 return ((dor & (0x10 << unit)) && (dor & 3) == unit);
783 static bool is_ready_state(
int status)
789 static int set_dor(
int fdc,
char mask,
char data)
793 unsigned char newdor;
794 unsigned char olddor;
796 if (
FDCS->address == -1)
800 newdor = (olddor &
mask) | data;
801 if (newdor != olddor) {
803 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
806 "calling disk change from set_dor\n");
813 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
821 static void twaddle(
void)
823 if (
DP->select_delay)
834 static void reset_fdc_info(
int mode)
839 FDCS->need_configure = 1;
842 for (drive = 0; drive <
N_DRIVE; drive++)
848 static void set_fdc(
int drive)
850 if (drive >= 0 && drive < N_DRIVE) {
852 current_drive = drive;
854 if (fdc != 1 && fdc != 0) {
860 set_dor(1 - fdc, ~8, 0);
862 if (
FDCS->rawcmd == 2)
869 static int lock_fdc(
int drive,
bool interruptible)
872 "Trying to lock fdc while usage count=0\n"))
880 reschedule_timeout(drive,
"lock fdc");
886 static void unlock_fdc(
void)
889 DPRINT(
"FDC access conflict!\n");
901 static void motor_off_callback(
unsigned long nr)
903 unsigned char mask = ~(0x10 <<
UNIT(nr));
905 set_dor(
FDC(nr), mask, 0);
909 static void floppy_off(
unsigned int drive)
911 unsigned long volatile delta;
912 int fdc =
FDC(drive);
914 if (!(
FDCS->dor & (0x10 <<
UNIT(drive))))
923 UDP->spindown_offset;
924 delta = ((delta *
UDP->rps) %
HZ) /
UDP->rps;
925 motor_off_timer[drive].expires =
936 static void scandrives(
void)
942 if (
DP->select_delay)
945 saved_drive = current_drive;
946 for (i = 0; i <
N_DRIVE; i++) {
947 drive = (saved_drive + i + 1) % N_DRIVE;
948 if (
UDRS->fd_ref == 0 ||
UDP->select_delay != 0)
951 if (!(set_dor(fdc, ~3,
UNIT(drive) | (0x10 <<
UNIT(drive))) &
952 (0x10 <<
UNIT(drive))))
955 set_dor(fdc, ~(0x10 <<
UNIT(drive)), 0);
957 set_fdc(saved_drive);
960 static void empty(
void)
966 static void schedule_bh(
void (*handler)(
void))
976 static void cancel_activity(
void)
987 debug_dcl(
DP->flags,
"calling disk change from watchdog\n");
989 if (disk_change(current_drive)) {
990 DPRINT(
"disk removed during i/o\n");
1001 static void main_command_interrupt(
void)
1008 static int fd_wait_for_completion(
unsigned long expires,
work_func_t function)
1026 static void setup_DMA(
void)
1030 if (raw_cmd->
length == 0) {
1033 pr_info(
"zero dma transfer size:");
1034 for (i = 0; i < raw_cmd->
cmd_count; i++)
1041 if (((
unsigned long)raw_cmd->
kernel_data) % 512) {
1066 virtual_dma_port =
FDCS->address;
1072 static void show_floppy(
void);
1075 static int wait_til_ready(
void)
1082 for (counter = 0; counter < 10000; counter++) {
1088 DPRINT(
"Getstatus times out (%x) on fdc %d\n", status, fdc);
1096 static int output_byte(
char byte)
1098 int status = wait_til_ready();
1103 if (is_ready_state(status)) {
1105 output_log[output_log_pos].data =
byte;
1106 output_log[output_log_pos].status =
status;
1107 output_log[output_log_pos].jiffies =
jiffies;
1108 output_log_pos = (output_log_pos + 1) %
OLOGSIZE;
1113 DPRINT(
"Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1127 status = wait_til_ready();
1142 DPRINT(
"get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1150 #define MORE_OUTPUT -2
1152 static int need_more_output(
void)
1154 int status = wait_til_ready();
1159 if (is_ready_state(status))
1168 static void perpendicular_mode(
void)
1170 unsigned char perp_mode;
1172 if (raw_cmd->
rate & 0x40) {
1173 switch (raw_cmd->
rate & 3) {
1181 DPRINT(
"Invalid data rate for perpendicular mode!\n");
1194 if (
FDCS->perp_mode == perp_mode)
1198 output_byte(perp_mode);
1199 FDCS->perp_mode = perp_mode;
1200 }
else if (perp_mode) {
1201 DPRINT(
"perpendicular mode not supported by this FDC.\n");
1205 static int fifo_depth = 0xa;
1208 static int fdc_configure(
void)
1215 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1221 #define NOMINAL_DTR 500
1242 static void fdc_specify(
void)
1244 unsigned char spec1;
1245 unsigned char spec2;
1251 int hlt_max_code = 0x7f;
1252 int hut_max_code = 0xf;
1256 FDCS->need_configure = 0;
1259 switch (raw_cmd->
rate & 0x03) {
1270 output_byte(
UNIT(current_drive));
1282 hlt_max_code = 0x00;
1297 else if (hlt > 0x7f)
1306 spec1 = (srt << 4) | hut;
1307 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1310 if (
FDCS->spec1 != spec1 ||
FDCS->spec2 != spec2) {
1313 output_byte(
FDCS->spec1 = spec1);
1314 output_byte(
FDCS->spec2 = spec2);
1322 static int fdc_dtr(
void)
1325 if ((raw_cmd->
rate & 3) ==
FDCS->dtr)
1337 return fd_wait_for_completion(jiffies + 2
UL *
HZ / 100,
1341 static void tell_sector(
void)
1343 pr_cont(
": track %d, head %d, sector %d, size %d",
1347 static void print_errors(
void)
1351 pr_cont(
"Recalibrate failed!");
1370 pr_cont(
"unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1384 static int interpret_errors(
void)
1389 DPRINT(
"-- FDC reply error\n");
1401 DPRINT(
"Drive is write protected\n");
1409 DPRINT(
"Over/Underrun - retrying\n");
1411 }
else if (*
errors >=
DP->max_errors.reporting) {
1419 DPRINT(
"Invalid FDC command given!\n");
1423 DPRINT(
"Abnormal termination caused by polling\n");
1436 static void setup_rw_floppy(
void)
1442 unsigned long ready_date;
1445 flags = raw_cmd->
flags;
1450 ready_date =
DRS->spinup_date +
DP->spinup;
1455 if (
time_after(ready_date, jiffies +
DP->select_delay)) {
1456 ready_date -=
DP->select_delay;
1462 if (fd_wait_for_completion(ready_date,
function))
1465 dflags =
DRS->flags;
1471 do_floppy = main_command_interrupt;
1474 for (i = 0; i < raw_cmd->
cmd_count; i++)
1475 r |= output_byte(raw_cmd->
cmd[i]);
1477 debugt(__func__,
"rw_command");
1485 if (!(flags & FD_RAW_INTR)) {
1492 static int blind_seek;
1498 static void seek_interrupt(
void)
1500 debugt(__func__,
"");
1501 if (inr != 2 || (
ST0 & 0xF8) != 0x20) {
1508 if (
DRS->track >= 0 &&
DRS->track !=
ST1 && !blind_seek) {
1510 "clearing NEWCHANGE flag because of effective seek\n");
1520 static void check_wp(
void)
1525 output_byte(
UNIT(current_drive));
1533 "checking whether disk is write protected\n");
1542 static void seek_floppy(
void)
1548 debug_dcl(
DP->flags,
"calling disk change from %s\n", __func__);
1562 recalibrate_floppy();
1570 track = raw_cmd->
track - 1;
1573 set_dor(fdc, ~(0x10 <<
UNIT(current_drive)), 0);
1581 if (raw_cmd->
track !=
DRS->track &&
1583 track = raw_cmd->
track;
1590 do_floppy = seek_interrupt;
1592 output_byte(
UNIT(current_drive));
1593 if (output_byte(track) < 0) {
1597 debugt(__func__,
"");
1600 static void recal_interrupt(
void)
1602 debugt(__func__,
"");
1605 else if (
ST0 & ST0_ECE) {
1606 switch (
DRS->track) {
1608 debugt(__func__,
"need 1 recal");
1617 debugt(__func__,
"need 2 recal");
1625 "clearing NEWCHANGE flag because of second recalibrate\n");
1631 debugt(__func__,
"default");
1646 static void print_result(
char *message,
int inr)
1652 for (i = 0; i < inr; i++)
1653 pr_cont(
"repl[%d]=%x ", i, reply_buffer[i]);
1672 if (fdc >=
N_FDC ||
FDCS->address == -1) {
1674 pr_info(
"DOR0=%x\n", fdc_state[0].dor);
1675 pr_info(
"floppy interrupt on bizarre fdc %d\n", fdc);
1677 is_alive(__func__,
"bizarre fdc");
1695 print_result(
"unexpected interrupt", inr);
1702 print_result(
"sensei", inr);
1704 }
while ((
ST0 & 0x83) !=
UNIT(current_drive) &&
1705 inr == 2 && max_sensei);
1712 is_alive(__func__,
"normal interrupt end");
1718 static void recalibrate_floppy(
void)
1720 debugt(__func__,
"");
1721 do_floppy = recal_interrupt;
1723 if (output_byte(
UNIT(current_drive)) < 0)
1730 static void reset_interrupt(
void)
1732 debugt(__func__,
"");
1735 pr_info(
"reset set in interrupt, calling %pf\n",
cont->error);
1745 static void reset_fdc(
void)
1747 unsigned long flags;
1749 do_floppy = reset_interrupt;
1769 static void show_floppy(
void)
1774 pr_info(
"floppy driver state\n");
1775 pr_info(
"-------------------\n");
1776 pr_info(
"now=%lu last interrupt=%lu diff=%lu last called handler=%pf\n",
1777 jiffies, interruptjiffies, jiffies - interruptjiffies,
1780 pr_info(
"timeout_message=%s\n", timeout_message);
1781 pr_info(
"last output bytes:\n");
1784 output_log[(i + output_log_pos) % OLOGSIZE].data,
1785 output_log[(i + output_log_pos) % OLOGSIZE].status,
1786 output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
1787 pr_info(
"last result at %lu\n", resultjiffies);
1788 pr_info(
"last redo_fd_request at %lu\n", lastredo);
1790 reply_buffer, resultsize,
true);
1793 pr_info(
"fdc_busy=%lu\n", fdc_busy);
1795 pr_info(
"do_floppy=%pf\n", do_floppy);
1797 pr_info(
"floppy_work.func=%pf\n", floppy_work.func);
1799 pr_info(
"delayed work.function=%p expires=%ld\n",
1801 fd_timer.timer.expires - jiffies);
1803 pr_info(
"timer_function=%p expires=%ld\n",
1804 fd_timeout.work.func,
1805 fd_timeout.timer.expires - jiffies);
1813 static void floppy_shutdown(
struct work_struct *arg)
1815 unsigned long flags;
1828 DPRINT(
"floppy timeout called\n");
1834 pr_info(
"no cont in shutdown!\n");
1835 process_fd_request();
1837 is_alive(__func__,
"");
1841 static int start_motor(
void (*
function)(
void))
1847 data =
UNIT(current_drive);
1848 if (!(raw_cmd->
flags & FD_RAW_NO_MOTOR)) {
1849 if (!(
FDCS->dor & (0x10 <<
UNIT(current_drive)))) {
1852 DRS->first_read_date = 0;
1854 DRS->spinup_date = jiffies;
1855 data |= (0x10 <<
UNIT(current_drive));
1857 }
else if (
FDCS->dor & (0x10 <<
UNIT(current_drive)))
1858 mask &= ~(0x10 <<
UNIT(current_drive));
1861 del_timer(motor_off_timer + current_drive);
1862 set_dor(fdc, mask, data);
1865 return fd_wait_for_completion(
DRS->select_date +
DP->select_delay,
1869 static void floppy_ready(
void)
1875 if (start_motor(floppy_ready))
1880 debug_dcl(
DP->flags,
"calling disk change from floppy_ready\n");
1881 if (!(raw_cmd->
flags & FD_RAW_NO_MOTOR) &&
1882 disk_change(current_drive) && !
DP->select_delay)
1886 #ifdef fd_chose_dma_mode
1895 perpendicular_mode();
1899 if ((raw_cmd->
flags & FD_RAW_READ) ||
1906 static void floppy_start(
void)
1911 debug_dcl(
DP->flags,
"setting NEWCHANGE in floppy_start\n");
1930 static void do_wakeup(
void)
1938 static const struct cont_t wakeup_cont = {
1945 static const struct cont_t intr_cont = {
1947 .redo = process_fd_request,
1952 static int wait_til_done(
void (*handler)(
void),
bool interruptible)
1956 schedule_bh(handler);
1980 static void generic_done(
int result)
1983 cont = &wakeup_cont;
1986 static void generic_success(
void)
1991 static void generic_failure(
void)
1996 static void success_and_wakeup(
void)
2007 static int next_valid_format(
void)
2011 probed_format =
DRS->probed_format;
2013 if (probed_format >= 8 || !
DP->autodetect[probed_format]) {
2014 DRS->probed_format = 0;
2017 if (floppy_type[
DP->autodetect[probed_format]].
sect) {
2018 DRS->probed_format = probed_format;
2025 static void bad_flp_intr(
void)
2030 DRS->probed_format++;
2031 if (!next_valid_format())
2034 err_count = ++(*errors);
2036 if (err_count >
DP->max_errors.abort)
2038 if (err_count >
DP->max_errors.reset)
2040 else if (err_count >
DP->max_errors.recal)
2044 static void set_floppy(
int drive)
2049 _floppy = floppy_type +
type;
2051 _floppy = current_type[drive];
2058 static void format_interrupt(
void)
2060 switch (interpret_errors()) {
2071 #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
2072 #define CT(x) ((x) | 0xc0)
2074 static void setup_format_params(
int track)
2083 } *
here = (
struct fparm *)floppy_track_buffer;
2085 raw_cmd = &default_raw_cmd;
2090 raw_cmd->
rate = _floppy->
rate & 0x43;
2106 track_shift = 2 * head_shift + 3;
2109 n = (track_shift * format_req.track + head_shift * format_req.head)
2127 n = (n + il) % F_SECT_PER_TRACK;
2130 if (n >= F_SECT_PER_TRACK) {
2132 while (
here[n].sect)
2143 static void redo_format(
void)
2146 setup_format_params(format_req.track <<
STRETCH(_floppy));
2148 debugt(__func__,
"queue format request");
2151 static const struct cont_t format_cont = {
2152 .interrupt = format_interrupt,
2153 .redo = redo_format,
2154 .error = bad_flp_intr,
2155 .done = generic_done
2158 static int do_format(
int drive,
struct format_descr *tmp_format_req)
2162 if (lock_fdc(drive,
true))
2167 _floppy->
track >
DP->tracks ||
2169 tmp_format_req->
head >= _floppy->
head ||
2172 process_fd_request();
2175 format_req = *tmp_format_req;
2177 cont = &format_cont;
2179 ret = wait_til_done(redo_format,
true);
2182 process_fd_request();
2193 unsigned int nr_sectors = current_count_sectors;
2194 unsigned int drive = (
unsigned long)req->rq_disk->private_data;
2198 nr_sectors = blk_rq_cur_sectors(req);
2209 static void request_done(
int uptodate)
2213 unsigned long flags;
2215 char msg[
sizeof(
"request done ") +
sizeof(
int) * 3];
2218 snprintf(msg,
sizeof(msg),
"request done %d", uptodate);
2222 pr_info(
"floppy.c: no request in request_done\n");
2231 block = current_count_sectors + blk_rq_pos(req);
2233 if (block > _floppy->
sect)
2238 floppy_end_request(req, 0);
2239 spin_unlock_irqrestore(q->queue_lock, flags);
2241 if (rq_data_dir(req) ==
WRITE) {
2243 DRWE->write_errors++;
2244 if (
DRWE->write_errors == 1) {
2245 DRWE->first_error_sector = blk_rq_pos(req);
2246 DRWE->first_error_generation =
DRS->generation;
2248 DRWE->last_error_sector = blk_rq_pos(req);
2249 DRWE->last_error_generation =
DRS->generation;
2252 floppy_end_request(req, -
EIO);
2253 spin_unlock_irqrestore(q->queue_lock, flags);
2258 static void rw_interrupt(
void)
2272 if (!
DRS->first_read_date)
2273 DRS->first_read_date = jiffies;
2292 if (nr_sectors / ssize >
2293 DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
2294 DPRINT(
"long rw: %x instead of %lx\n",
2295 nr_sectors, current_count_sectors);
2299 pr_info(
"heads=%d eoc=%d\n", heads, eoc);
2300 pr_info(
"spt=%d st=%d ss=%d\n",
2302 pr_info(
"in_sector_offset=%d\n", in_sector_offset);
2305 nr_sectors -= in_sector_offset;
2307 SUPBOUND(current_count_sectors, nr_sectors);
2309 switch (interpret_errors()) {
2314 if (!current_count_sectors) {
2321 if (!current_count_sectors) {
2325 current_type[current_drive] = _floppy;
2326 floppy_sizes[
TOMINOR(current_drive)] = _floppy->
size;
2332 DPRINT(
"Auto-detected floppy type %s in fd%d\n",
2333 _floppy->
name, current_drive);
2334 current_type[current_drive] = _floppy;
2335 floppy_sizes[
TOMINOR(current_drive)] = _floppy->
size;
2344 buffer_track = raw_cmd->
track;
2345 buffer_drive = current_drive;
2346 INFBOUND(buffer_max, nr_sectors + fsector_t);
2352 static int buffer_chain_size(
void)
2356 struct req_iterator iter;
2363 if (
page_address(bv->bv_page) + bv->bv_offset != base + size)
2375 SUPBOUND(max_sector, fsector_t + max_size);
2378 max_sector -= (max_sector % _floppy->
sect) % ssize;
2381 current_count_sectors = max_sector - fsector_t;
2389 static void copy_buffer(
int ssize,
int max_sector,
int max_sector_2)
2396 struct req_iterator iter;
2399 min(max_sector, max_sector_2),
2403 buffer_max > fsector_t + blk_rq_sectors(
current_req))
2404 current_count_sectors =
min_t(
int, buffer_max - fsector_t,
2407 remaining = current_count_sectors << 9;
2409 DPRINT(
"in copy buffer\n");
2410 pr_info(
"current_count_sectors=%ld\n", current_count_sectors);
2411 pr_info(
"remaining=%d\n", remaining >> 9);
2412 pr_info(
"current_req->nr_sectors=%u\n",
2414 pr_info(
"current_req->current_nr_sectors=%u\n",
2416 pr_info(
"max_sector=%d\n", max_sector);
2420 buffer_max =
max(max_sector, buffer_max);
2422 dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2434 if (dma_buffer + size >
2435 floppy_track_buffer + (max_buffer_sectors << 10) ||
2436 dma_buffer < floppy_track_buffer) {
2437 DPRINT(
"buffer overrun in copy buffer %d\n",
2438 (
int)((floppy_track_buffer - dma_buffer) >> 9));
2439 pr_info(
"fsector_t=%d buffer_min=%d\n",
2440 fsector_t, buffer_min);
2441 pr_info(
"current_count_sectors=%ld\n",
2442 current_count_sectors);
2449 if (((
unsigned long)buffer) % 512)
2450 DPRINT(
"%p buffer not aligned\n", buffer);
2453 memcpy(buffer, dma_buffer, size);
2455 memcpy(dma_buffer, buffer, size);
2462 max_sector -= remaining >> 9;
2463 DPRINT(
"weirdness: remaining %d\n", remaining >> 9);
2474 static void virtualdmabug_workaround(
void)
2483 end_sector =
SECTOR + hard_sectors - 1;
2485 pr_info(
"too many sectors %d > %d\n",
2505 static int make_raw_rw_request(
void)
2507 int aligned_sector_t;
2513 if (
WARN(max_buffer_sectors == 0,
"VFS: Block I/O scheduled on unopened device\n"))
2516 set_fdc((
long)
current_req->rq_disk->private_data);
2518 raw_cmd = &default_raw_cmd;
2528 DPRINT(
"%s: unknown command\n", __func__);
2532 max_sector = _floppy->
sect * _floppy->
head;
2538 current_count_sectors = 1;
2547 fsector_t < _floppy->
sect)
2548 max_sector = _floppy->
sect;
2552 max_sector = 2 * _floppy->
sect / 3;
2553 if (fsector_t >= max_sector) {
2554 current_count_sectors =
2562 raw_cmd->
rate = _floppy->
rate & 0x43;
2581 tracksize = _floppy->
sect - _floppy->
sect % ssize;
2582 if (tracksize < _floppy->sect) {
2584 if (tracksize <= fsector_t % _floppy->sect)
2588 while (tracksize <= fsector_t % _floppy->sect) {
2589 while (tracksize + ssize > _floppy->
sect) {
2597 max_sector =
HEAD * _floppy->
sect + tracksize;
2599 max_sector = _floppy->
sect;
2602 max_sector = _floppy->
sect;
2605 in_sector_offset = (fsector_t % _floppy->
sect) % ssize;
2606 aligned_sector_t = fsector_t - in_sector_offset;
2608 if ((raw_cmd->
track == buffer_track) &&
2609 (current_drive == buffer_drive) &&
2610 (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2613 copy_buffer(1, max_sector, buffer_max);
2616 }
else if (in_sector_offset || blk_rq_sectors(
current_req) < ssize) {
2620 sectors = fsector_t + blk_rq_sectors(
current_req);
2621 if (sectors > ssize && sectors < ssize + ssize)
2622 max_size = ssize + ssize;
2630 unsigned long dma_limit;
2635 max_buffer_sectors * 2) - fsector_t;
2641 max_size = buffer_chain_size();
2644 if ((
unsigned long)max_size > dma_limit)
2645 max_size = dma_limit;
2660 (indirect * 2 >
direct * 3 &&
2661 *errors < DP->max_errors.read_track &&
2663 (
DP->read_track & (1 <<
DRS->probed_format)))))) {
2667 raw_cmd->
length = current_count_sectors << 9;
2668 if (raw_cmd->
length == 0) {
2669 DPRINT(
"%s: zero dma transfer attempted\n", __func__);
2670 DPRINT(
"indirect=%d direct=%d fsector_t=%d\n",
2671 indirect,
direct, fsector_t);
2674 virtualdmabug_workaround();
2680 max_size = max_sector;
2683 if (buffer_track != raw_cmd->
track ||
2684 buffer_drive != current_drive ||
2685 fsector_t > buffer_max ||
2686 fsector_t < buffer_min ||
2688 (!in_sector_offset && blk_rq_sectors(
current_req) >= ssize)) &&
2689 max_sector > 2 * max_buffer_sectors + buffer_min &&
2690 max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2693 buffer_drive = current_drive;
2694 buffer_max = buffer_min = aligned_sector_t;
2697 ((aligned_sector_t - buffer_min) << 9);
2704 if (in_sector_offset && buffer_track == -1)
2705 DPRINT(
"internal error offset !=0 on write\n");
2706 buffer_track = raw_cmd->
track;
2707 buffer_drive = current_drive;
2708 copy_buffer(ssize, max_sector,
2709 2 * max_buffer_sectors + buffer_min);
2712 2 * max_buffer_sectors + buffer_min -
2716 raw_cmd->
length = in_sector_offset + current_count_sectors;
2717 raw_cmd->
length = ((raw_cmd->
length - 1) | (ssize - 1)) + 1;
2719 if ((raw_cmd->
length < current_count_sectors << 9) ||
2722 (aligned_sector_t + (raw_cmd->
length >> 9) > buffer_max ||
2723 aligned_sector_t < buffer_min)) ||
2725 raw_cmd->
length <= 0 || current_count_sectors <= 0) {
2726 DPRINT(
"fractionary current count b=%lx s=%lx\n",
2727 raw_cmd->
length, current_count_sectors);
2729 pr_info(
"addr=%d, length=%ld\n",
2731 floppy_track_buffer) >> 9),
2732 current_count_sectors);
2733 pr_info(
"st=%d ast=%d mse=%d msi=%d\n",
2734 fsector_t, aligned_sector_t, max_sector, max_size);
2736 pr_info(
"command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2738 pr_info(
"buffer drive=%d\n", buffer_drive);
2739 pr_info(
"buffer track=%d\n", buffer_track);
2740 pr_info(
"buffer_min=%d\n", buffer_min);
2741 pr_info(
"buffer_max=%d\n", buffer_max);
2747 current_count_sectors < 0 ||
2750 floppy_track_buffer + (max_buffer_sectors << 10)) {
2751 DPRINT(
"buffer overrun in schedule dma\n");
2752 pr_info(
"fsector_t=%d buffer_min=%d current_count=%ld\n",
2753 fsector_t, buffer_min, raw_cmd->
length >> 9);
2754 pr_info(
"current_count_sectors=%ld\n",
2755 current_count_sectors);
2763 current_count_sectors > blk_rq_sectors(
current_req)) {
2764 DPRINT(
"buffer overrun in direct transfer\n");
2766 }
else if (raw_cmd->
length < current_count_sectors << 9) {
2767 DPRINT(
"more sectors than bytes\n");
2769 pr_info(
"sectors=%ld\n", current_count_sectors);
2771 if (raw_cmd->
length == 0) {
2772 DPRINT(
"zero dma transfer attempted from make_raw_request\n");
2776 virtualdmabug_workaround();
2783 static int set_next_request(
void)
2786 int old_pos = fdc_queue;
2789 q = disks[fdc_queue]->queue;
2790 if (++fdc_queue == N_DRIVE)
2797 }
while (fdc_queue != old_pos);
2802 static void redo_fd_request(
void)
2808 if (current_drive < N_DRIVE)
2809 floppy_off(current_drive);
2815 spin_lock_irq(&floppy_lock);
2816 pending = set_next_request();
2817 spin_unlock_irq(&floppy_lock);
2829 raw_cmd = &default_raw_cmd;
2831 if (start_motor(redo_fd_request))
2834 disk_change(current_drive);
2835 if (
test_bit(current_drive, &fake_change) ||
2837 DPRINT(
"disk absent or changed during operation\n");
2843 DRS->probed_format = 0;
2844 if (next_valid_format()) {
2845 DPRINT(
"no autodetectable formats\n");
2852 _floppy = floppy_type +
DP->autodetect[
DRS->probed_format];
2856 tmp = make_raw_rw_request();
2864 schedule_bh(floppy_start);
2865 debugt(__func__,
"queue fd request");
2869 static const struct cont_t rw_cont = {
2870 .interrupt = rw_interrupt,
2871 .redo = redo_fd_request,
2872 .error = bad_flp_intr,
2873 .done = request_done
2876 static void process_fd_request(
void)
2879 schedule_bh(redo_fd_request);
2884 if (
WARN(max_buffer_sectors == 0,
2885 "VFS: %s called on non-open device\n", __func__))
2889 "warning: usage count=0, current_req=%p sect=%ld type=%x flags=%x\n",
2897 is_alive(__func__,
"old request running");
2901 __reschedule_timeout(
MAXTIMEOUT,
"fd_request");
2903 process_fd_request();
2904 is_alive(__func__,
"");
2907 static const struct cont_t poll_cont = {
2908 .interrupt = success_and_wakeup,
2909 .redo = floppy_ready,
2910 .error = generic_failure,
2911 .done = generic_done
2914 static int poll_drive(
bool interruptible,
int flag)
2917 raw_cmd = &default_raw_cmd;
2922 debug_dcl(
DP->flags,
"setting NEWCHANGE in poll_drive\n");
2925 return wait_til_done(floppy_ready, interruptible);
2933 static void reset_intr(
void)
2935 pr_info(
"weird, reset interrupt called\n");
2939 .interrupt = reset_intr,
2940 .redo = success_and_wakeup,
2941 .error = generic_failure,
2942 .done = generic_done
2945 static int user_reset_fdc(
int drive,
int arg,
bool interruptible)
2949 if (lock_fdc(drive, interruptible))
2956 ret = wait_til_done(reset_fdc, interruptible);
2960 process_fd_request();
2968 static inline int fd_copyout(
void __user *
param,
const void *
address,
2974 static inline int fd_copyin(
void __user *
param,
void *
address,
2980 static const char *drive_name(
int type,
int drive)
2985 floppy = floppy_type +
type;
2987 if (
UDP->native_format)
2988 floppy = floppy_type +
UDP->native_format;
2993 return floppy->
name;
2999 static void raw_cmd_done(
int flag)
3011 raw_cmd->
reply[i] = reply_buffer[i];
3014 unsigned long flags;
3024 if (disk_change(current_drive))
3029 motor_off_callback(current_drive);
3031 if (raw_cmd->
next &&
3036 raw_cmd = raw_cmd->
next;
3043 static const struct cont_t raw_cmd_cont = {
3044 .interrupt = success_and_wakeup,
3045 .redo = floppy_start,
3046 .error = generic_failure,
3047 .done = raw_cmd_done
3050 static int raw_cmd_copyout(
int cmd,
void __user *
param,
3095 static int raw_cmd_copyin(
int cmd,
void __user *param,
3126 for (i = 0; i < 16; i++)
3147 rcmd = &(ptr->
next);
3155 static int raw_cmd_ioctl(
int cmd,
void __user *param)
3162 if (
FDCS->rawcmd <= 1)
3164 for (drive = 0; drive <
N_DRIVE; drive++) {
3165 if (
FDC(drive) != fdc)
3167 if (drive == current_drive) {
3168 if (
UDRS->fd_ref > 1) {
3172 }
else if (
UDRS->fd_ref) {
3181 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3183 raw_cmd_free(&my_raw_cmd);
3187 raw_cmd = my_raw_cmd;
3188 cont = &raw_cmd_cont;
3189 ret = wait_til_done(floppy_start,
true);
3190 debug_dcl(
DP->flags,
"calling disk change from raw_cmd ioctl\n");
3197 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3200 raw_cmd_free(&my_raw_cmd);
3208 process_fd_request();
3213 static int set_geometry(
unsigned int cmd,
struct floppy_struct *g,
3229 if (lock_fdc(drive,
true)) {
3233 floppy_type[
type] = *g;
3234 floppy_type[
type].
name =
"user format";
3235 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3236 floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
3237 floppy_type[type].size + 1;
3238 process_fd_request();
3239 for (cnt = 0; cnt <
N_DRIVE; cnt++) {
3241 if (!bdev ||
ITYPE(drive_state[cnt].fd_device) != type)
3249 if (lock_fdc(drive,
true))
3258 user_params[drive] = *g;
3259 if (buffer_drive == drive)
3260 SUPBOUND(buffer_max, user_params[drive].sect);
3261 current_type[drive] = &user_params[drive];
3262 floppy_sizes[drive] = user_params[drive].size;
3264 DRS->keep_data = -1;
3272 if (
DRS->maxblock > user_params[drive].sect ||
3274 ((user_params[drive].sect ^ oldStretch) &
3276 invalidate_drive(bdev);
3278 process_fd_request();
3284 static unsigned int ioctl_table[] = {
3312 static int normalize_ioctl(
unsigned int *cmd,
int *size)
3316 for (i = 0; i <
ARRAY_SIZE(ioctl_table); i++) {
3317 if ((*cmd & 0xffff) == (ioctl_table[
i] & 0xffff)) {
3319 *cmd = ioctl_table[
i];
3321 pr_info(
"ioctl not yet supported\n");
3330 static int get_floppy_geometry(
int drive,
int type,
struct floppy_struct **g)
3333 *g = &floppy_type[
type];
3335 if (lock_fdc(drive,
false))
3337 if (poll_drive(
false, 0) == -
EINTR)
3339 process_fd_request();
3340 *g = current_type[drive];
3350 int type =
ITYPE(drive_state[drive].fd_device);
3354 ret = get_floppy_geometry(drive, type, &g);
3365 unsigned long param)
3378 const void *outparam;
3385 DPRINT(
"obsolete eject ioctl\n");
3386 DPRINT(
"please use floppycontrol --eject\n");
3390 if (!((cmd & 0xff00) == 0x0200))
3394 ret = normalize_ioctl(&cmd, &size);
3403 if (
WARN_ON(size < 0 || size >
sizeof(inparam)))
3407 memset(&inparam, 0,
sizeof(inparam));
3409 ret = fd_copyin((
void __user *)param, &inparam, size);
3416 if (
UDRS->fd_ref != 1)
3419 if (lock_fdc(drive,
true))
3428 process_fd_request();
3431 if (lock_fdc(drive,
true))
3433 current_type[drive] =
NULL;
3435 UDRS->keep_data = 0;
3436 return invalidate_drive(bdev);
3439 return set_geometry(cmd, &inparam.g, drive, type, bdev);
3441 ret = get_floppy_geometry(drive, type,
3453 if (lock_fdc(drive,
true))
3458 process_fd_request();
3465 if (
UDRS->fd_ref != 1)
3467 return do_format(drive, &inparam.f);
3470 if (lock_fdc(drive,
true))
3472 return invalidate_drive(bdev);
3474 UDP->max_errors.reporting = (
unsigned short)(param & 0x0f);
3477 outparam = &
UDP->max_errors;
3480 UDP->max_errors = inparam.max_errors;
3483 outparam = drive_name(type, drive);
3493 if (lock_fdc(drive,
true))
3497 process_fd_request();
3503 return user_reset_fdc(drive, (
int)param,
true);
3516 if (lock_fdc(drive,
true))
3519 i = raw_cmd_ioctl(cmd, (
void __user *)param);
3522 process_fd_request();
3525 if (lock_fdc(drive,
true))
3528 process_fd_request();
3535 return fd_copyout((
void __user *)param, outparam, size);
3541 unsigned int cmd,
unsigned long param)
3546 ret = fd_locked_ioctl(bdev, mode, cmd, param);
3552 static void __init config_types(
void)
3554 bool has_drive =
false;
3567 for (drive = 0; drive <
N_DRIVE; drive++) {
3568 unsigned int type =
UDP->cmos;
3571 static char temparea[32];
3573 if (type <
ARRAY_SIZE(default_drive_params)) {
3574 params = &default_drive_params[
type].params;
3576 name = default_drive_params[
type].name;
3577 allowed_drive_mask |= 1 << drive;
3579 allowed_drive_mask &= ~(1 << drive);
3581 params = &default_drive_params[0].params;
3582 sprintf(temparea,
"unknown type %d (usb?)", type);
3586 const char *prepend;
3595 pr_cont(
"%s fd%d is %s", prepend, drive, name);
3604 static int floppy_release(
struct gendisk *disk,
fmode_t mode)
3606 int drive = (
long)disk->private_data;
3610 if (!
UDRS->fd_ref--) {
3611 DPRINT(
"floppy_release with fd_ref == 0");
3615 opened_bdev[drive] =
NULL;
3630 int old_dev, new_dev;
3637 old_dev =
UDRS->fd_device;
3638 if (opened_bdev[drive] && opened_bdev[drive] != bdev)
3648 opened_bdev[drive] = bdev;
3652 if (!floppy_track_buffer) {
3655 if ((
UDP->cmos == 6) || (
UDP->cmos == 5))
3661 if (!tmp && !floppy_track_buffer) {
3666 if (!tmp && !floppy_track_buffer)
3667 fallback_on_nodma_alloc(&tmp, 2048 *
try);
3668 if (!tmp && !floppy_track_buffer) {
3669 DPRINT(
"Unable to allocate DMA memory\n");
3672 if (floppy_track_buffer) {
3676 buffer_min = buffer_max = -1;
3677 floppy_track_buffer =
tmp;
3678 max_buffer_sectors =
try;
3683 UDRS->fd_device = new_dev;
3684 set_capacity(disks[drive], floppy_sizes[new_dev]);
3685 if (old_dev != -1 && old_dev != new_dev) {
3686 if (buffer_drive == drive)
3690 if (
UFDCS->rawcmd == 1)
3695 UDRS->last_checked = 0;
3712 opened_bdev[drive] =
NULL;
3722 static unsigned int floppy_check_events(
struct gendisk *disk,
3723 unsigned int clearing)
3725 int drive = (
long)disk->private_data;
3729 return DISK_EVENT_MEDIA_CHANGE;
3732 lock_fdc(drive,
false);
3733 poll_drive(
false, 0);
3734 process_fd_request();
3740 drive_no_geom(drive))
3741 return DISK_EVENT_MEDIA_CHANGE;
3751 static void floppy_rb0_complete(
struct bio *bio,
int err)
3756 static int __floppy_read_block_0(
struct block_device *bdev)
3759 struct bio_vec bio_vec;
3766 process_fd_request();
3775 bio.bi_io_vec = &bio_vec;
3776 bio_vec.bv_page =
page;
3777 bio_vec.bv_len =
size;
3778 bio_vec.bv_offset = 0;
3784 bio.bi_flags = (1 << BIO_QUIET);
3787 bio.bi_end_io = floppy_rb0_complete;
3790 process_fd_request();
3802 static int floppy_revalidate(
struct gendisk *disk)
3804 int drive = (
long)disk->private_data;
3811 drive_no_geom(drive)) {
3813 "VFS: revalidate called on non-open device.\n"))
3816 lock_fdc(drive,
false);
3819 if (!(
cf ||
test_bit(drive, &fake_change) || drive_no_geom(drive))) {
3820 process_fd_request();
3825 if (buffer_drive == drive)
3831 if (drive_no_geom(drive)) {
3833 res = __floppy_read_block_0(opened_bdev[drive]);
3837 process_fd_request();
3840 set_capacity(disk, floppy_sizes[
UDRS->fd_device]);
3844 static const struct block_device_operations floppy_fops = {
3846 .open = floppy_open,
3847 .release = floppy_release,
3849 .getgeo = fd_getgeo,
3850 .check_events = floppy_check_events,
3851 .revalidate_disk = floppy_revalidate,
3861 static char __init get_fdc_version(
void)
3871 if ((r == 1) && (reply_buffer[0] == 0x80)) {
3872 pr_info(
"FDC %d is an 8272A\n", fdc);
3876 pr_info(
"FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3881 if (!fdc_configure()) {
3882 pr_info(
"FDC %d is an 82072\n", fdc);
3890 pr_info(
"FDC %d is an 82072A\n", fdc);
3896 if ((r == 1) && (reply_buffer[0] == 0x80)) {
3897 pr_info(
"FDC %d is a pre-1991 82077\n", fdc);
3901 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3902 pr_info(
"FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3909 pr_info(
"FDC %d init: PARTID: unexpected return of %d bytes.\n",
3913 if (reply_buffer[0] == 0x80) {
3914 pr_info(
"FDC %d is a post-1991 82077\n", fdc);
3917 switch (reply_buffer[0] >> 5) {
3920 pr_info(
"FDC %d is an 82078.\n", fdc);
3923 pr_info(
"FDC %d is a 44pin 82078\n", fdc);
3926 pr_info(
"FDC %d is a S82078B\n", fdc);
3929 pr_info(
"FDC %d is a National Semiconductor PC87306\n", fdc);
3932 pr_info(
"FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3933 fdc, reply_buffer[0] >> 5);
3940 static void __init floppy_set_flags(
int *ints,
int param,
int param2)
3944 for (i = 0; i <
ARRAY_SIZE(default_drive_params); i++) {
3946 default_drive_params[
i].params.flags |= param2;
3948 default_drive_params[
i].params.flags &= ~param2;
3950 DPRINT(
"%s flag 0x%x\n", param2 ?
"Setting" :
"Clearing", param);
3953 static void __init daring(
int *ints,
int param,
int param2)
3957 for (i = 0; i <
ARRAY_SIZE(default_drive_params); i++) {
3959 default_drive_params[
i].params.select_delay = 0;
3960 default_drive_params[
i].params.flags |=
3963 default_drive_params[
i].params.select_delay =
3965 default_drive_params[
i].params.flags &=
3969 DPRINT(
"Assuming %s floppy hardware\n", param ?
"standard" :
"broken");
3972 static void __init set_cmos(
int *ints,
int dummy,
int dummy2)
3974 int current_drive = 0;
3977 DPRINT(
"wrong number of parameters for CMOS\n");
3980 current_drive = ints[1];
3981 if (current_drive < 0 || current_drive >= 8) {
3982 DPRINT(
"bad drive for set_cmos\n");
3986 if (current_drive >= 4 && !FDC2)
3990 DPRINT(
"setting CMOS code to %d\n", ints[2]);
3993 static struct param_table {
4000 {
"allowed_drive_mask",
NULL, &allowed_drive_mask, 0xff, 0},
4001 {
"all_drives",
NULL, &allowed_drive_mask, 0xff, 0},
4002 {
"asus_pci",
NULL, &allowed_drive_mask, 0x33, 0},
4003 {
"irq",
NULL, &FLOPPY_IRQ, 6, 0},
4004 {
"dma",
NULL, &FLOPPY_DMA, 2, 0},
4005 {
"daring", daring,
NULL, 1, 0},
4007 {
"two_fdc",
NULL, &FDC2, 0x370, 0},
4008 {
"one_fdc",
NULL, &FDC2, 0, 0},
4015 {
"nodma",
NULL, &can_use_virtual_dma, 1, 0},
4016 {
"omnibook",
NULL, &can_use_virtual_dma, 1, 0},
4017 {
"yesdma",
NULL, &can_use_virtual_dma, 0, 0},
4018 {
"fifo_depth",
NULL, &fifo_depth, 0xa, 0},
4019 {
"nofifo",
NULL, &no_fifo, 0x20, 0},
4020 {
"usefifo",
NULL, &no_fifo, 0, 0},
4021 {
"cmos", set_cmos,
NULL, 0, 0},
4022 {
"slow",
NULL, &slow_floppy, 1, 0},
4023 {
"unexpected_interrupts",
NULL, &print_unex, 1, 0},
4024 {
"no_unexpected_interrupts",
NULL, &print_unex, 0, 0},
4025 {
"L40SX",
NULL, &print_unex, 0, 0}
4030 static int __init floppy_setup(
char *
str)
4038 for (i = 0; i <
ARRAY_SIZE(config_params); i++) {
4039 if (
strcmp(str, config_params[i].name) == 0) {
4043 param = config_params[
i].def_param;
4044 if (config_params[i].
fn)
4045 config_params[
i].fn(ints, param,
4048 if (config_params[i].var) {
4049 DPRINT(
"%s=%d\n", str, param);
4050 *config_params[
i].var =
param;
4057 DPRINT(
"unknown floppy option [%s]\n", str);
4059 DPRINT(
"allowed options are:");
4060 for (i = 0; i <
ARRAY_SIZE(config_params); i++)
4061 pr_cont(
" %s", config_params[i].name);
4064 DPRINT(
"botched floppy option\n");
4065 DPRINT(
"Read Documentation/blockdev/floppy.txt\n");
4069 static int have_no_fdc = -
ENODEV;
4083 static void floppy_device_release(
struct device *
dev)
4087 static int floppy_resume(
struct device *
dev)
4091 for (fdc = 0; fdc <
N_FDC; fdc++)
4092 if (
FDCS->address != -1)
4098 static const struct dev_pm_ops floppy_pm_ops = {
4099 .resume = floppy_resume,
4100 .restore = floppy_resume,
4106 .pm = &floppy_pm_ops,
4112 static bool floppy_available(
int drive)
4114 if (!(allowed_drive_mask & (1 << drive)))
4123 int drive = (*part & 3) | ((*part & 0x80) >> 5);
4124 if (drive >= N_DRIVE || !floppy_available(drive))
4126 if (((*part >> 2) & 0x1f) >=
ARRAY_SIZE(floppy_type))
4132 static int __init do_floppy_init(
void)
4137 interruptjiffies = resultjiffies = jiffies;
4139 #if defined(CONFIG_PPC)
4150 for (drive = 0; drive <
N_DRIVE; drive++) {
4152 if (!disks[drive]) {
4158 if (!disks[drive]->
queue) {
4165 disks[drive]->first_minor =
TOMINOR(drive);
4166 disks[drive]->fops = &floppy_fops;
4170 motor_off_timer[drive].data = drive;
4171 motor_off_timer[drive].function = motor_off_callback;
4180 goto out_unreg_blkdev;
4185 for (i = 0; i < 256; i++)
4187 floppy_sizes[
i] = floppy_type[
ITYPE(i)].
size;
4191 reschedule_timeout(
MAXTIMEOUT,
"floppy init");
4194 for (i = 0; i <
N_FDC; i++) {
4199 #if defined(__sparc__) || defined(__mc68000__)
4208 use_virtual_dma = can_use_virtual_dma & 1;
4209 fdc_state[0].address =
FDC1;
4210 if (fdc_state[0].
address == -1) {
4213 goto out_unreg_region;
4216 fdc_state[1].address = FDC2;
4220 err = floppy_grab_irq_and_dma();
4224 goto out_unreg_region;
4228 for (drive = 0; drive <
N_DRIVE; drive++) {
4234 UDRS->fd_device = -1;
4235 floppy_track_buffer =
NULL;
4236 max_buffer_sectors = 0;
4245 for (i = 0; i <
N_FDC; i++) {
4248 for (unit = 0; unit < 4; unit++)
4249 FDCS->track[unit] = 0;
4255 floppy_release_regions(fdc);
4261 FDCS->version = get_fdc_version();
4264 floppy_release_regions(fdc);
4269 can_use_virtual_dma = 0;
4283 DPRINT(
"no floppy controllers found\n");
4285 goto out_release_dma;
4288 for (drive = 0; drive <
N_DRIVE; drive++) {
4289 if (!floppy_available(drive))
4292 floppy_device[drive].name = floppy_device_name;
4293 floppy_device[drive].id = drive;
4294 floppy_device[drive].dev.release = floppy_device_release;
4298 goto out_remove_drives;
4303 goto out_unreg_platform_dev;
4306 disks[drive]->private_data = (
void *)(
long)drive;
4307 disks[drive]->flags |= GENHD_FL_REMOVABLE;
4308 disks[drive]->driverfs_dev = &floppy_device[drive].dev;
4314 out_unreg_platform_dev:
4318 if (floppy_available(drive)) {
4326 floppy_release_irq_and_dma();
4334 for (drive = 0; drive <
N_DRIVE; drive++) {
4337 if (disks[drive]->
queue) {
4340 disks[drive]->queue =
NULL;
4354 static int __init floppy_init(
void)
4357 return do_floppy_init();
4365 static const struct io_region {
4377 static void floppy_release_allocated_regions(
int fdc,
const struct io_region *
p)
4379 while (p != io_regions) {
4385 #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
4387 static int floppy_request_regions(
int fdc)
4389 const struct io_region *
p;
4391 for (p = io_regions; p <
ARRAY_END(io_regions); p++) {
4393 p->size,
"floppy")) {
4394 DPRINT(
"Floppy io-port 0x%04lx in use\n",
4395 FDCS->address + p->offset);
4396 floppy_release_allocated_regions(fdc, p);
4403 static void floppy_release_regions(
int fdc)
4405 floppy_release_allocated_regions(fdc,
ARRAY_END(io_regions));
4408 static int floppy_grab_irq_and_dma(
void)
4420 DPRINT(
"Unable to grab IRQ%d for the floppy driver\n",
4426 DPRINT(
"Unable to grab DMA%d for the floppy driver\n",
4428 if (can_use_virtual_dma & 2)
4429 use_virtual_dma = can_use_virtual_dma = 1;
4430 if (!(can_use_virtual_dma & 1)) {
4437 for (fdc = 0; fdc <
N_FDC; fdc++) {
4438 if (
FDCS->address != -1) {
4439 if (floppy_request_regions(fdc))
4443 for (fdc = 0; fdc <
N_FDC; fdc++) {
4444 if (
FDCS->address != -1) {
4452 for (fdc = 0; fdc <
N_FDC; fdc++)
4453 if (
FDCS->address != -1)
4460 irqdma_allocated = 1;
4466 floppy_release_regions(fdc);
4471 static void floppy_release_irq_and_dma(
void)
4478 unsigned long tmpaddr;
4483 if (irqdma_allocated) {
4487 irqdma_allocated = 0;
4494 if (floppy_track_buffer && max_buffer_sectors) {
4495 tmpsize = max_buffer_sectors * 1024;
4496 tmpaddr = (
unsigned long)floppy_track_buffer;
4497 floppy_track_buffer =
NULL;
4498 max_buffer_sectors = 0;
4499 buffer_min = buffer_max = -1;
4503 for (drive = 0; drive < N_FDC * 4; drive++)
4504 if (timer_pending(motor_off_timer + drive))
4505 pr_info(
"motor off timer %d still active\n", drive);
4509 pr_info(
"floppy timer still active:%s\n", timeout_message);
4511 pr_info(
"auxiliary floppy timer still active\n");
4513 pr_info(
"work still pending\n");
4515 for (fdc = 0; fdc <
N_FDC; fdc++)
4516 if (
FDCS->address != -1)
4517 floppy_release_regions(fdc);
4523 static char *floppy;
4525 static void __init parse_floppy_cfg_string(
char *
cfg)
4531 while (*cfg && *cfg !=
' ' && *cfg !=
'\t')
4542 static int __init floppy_module_init(
void)
4545 parse_floppy_cfg_string(floppy);
4546 return floppy_init();
4550 static void __exit floppy_module_exit(
void)
4560 for (drive = 0; drive <
N_DRIVE; drive++) {
4563 if (floppy_available(drive)) {
4574 if (!(allowed_drive_mask & (1 << drive)) ||
4576 disks[drive]->queue =
NULL;
4585 floppy_release_irq_and_dma();
4610 __setup(
"floppy=", floppy_setup);