PageRenderTime 78ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/block/floppy.c

https://bitbucket.org/evzijst/gittest
C | 4638 lines | 3447 code | 550 blank | 641 comment | 743 complexity | 5c75015c7cd5a2110055307f017a9a7d MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * linux/drivers/block/floppy.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 1993, 1994 Alain Knaff
  6. * Copyright (C) 1998 Alan Cox
  7. */
  8. /*
  9. * 02.12.91 - Changed to static variables to indicate need for reset
  10. * and recalibrate. This makes some things easier (output_byte reset
  11. * checking etc), and means less interrupt jumping in case of errors,
  12. * so the code is hopefully easier to understand.
  13. */
  14. /*
  15. * This file is certainly a mess. I've tried my best to get it working,
  16. * but I don't like programming floppies, and I have only one anyway.
  17. * Urgel. I should check for more errors, and do more graceful error
  18. * recovery. Seems there are problems with several drives. I've tried to
  19. * correct them. No promises.
  20. */
  21. /*
  22. * As with hd.c, all routines within this file can (and will) be called
  23. * by interrupts, so extreme caution is needed. A hardware interrupt
  24. * handler may not sleep, or a kernel panic will happen. Thus I cannot
  25. * call "floppy-on" directly, but have to set a special timer interrupt
  26. * etc.
  27. */
  28. /*
  29. * 28.02.92 - made track-buffering routines, based on the routines written
  30. * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
  31. */
  32. /*
  33. * Automatic floppy-detection and formatting written by Werner Almesberger
  34. * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
  35. * the floppy-change signal detection.
  36. */
  37. /*
  38. * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
  39. * FDC data overrun bug, added some preliminary stuff for vertical
  40. * recording support.
  41. *
  42. * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
  43. *
  44. * TODO: Errors are still not counted properly.
  45. */
  46. /* 1992/9/20
  47. * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
  48. * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
  49. * Christoph H. Hochst\"atter.
  50. * I have fixed the shift values to the ones I always use. Maybe a new
  51. * ioctl() should be created to be able to modify them.
  52. * There is a bug in the driver that makes it impossible to format a
  53. * floppy as the first thing after bootup.
  54. */
  55. /*
  56. * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
  57. * this helped the floppy driver as well. Much cleaner, and still seems to
  58. * work.
  59. */
  60. /* 1994/6/24 --bbroad-- added the floppy table entries and made
  61. * minor modifications to allow 2.88 floppies to be run.
  62. */
  63. /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
  64. * disk types.
  65. */
  66. /*
  67. * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
  68. * format bug fixes, but unfortunately some new bugs too...
  69. */
  70. /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
  71. * errors to allow safe writing by specialized programs.
  72. */
  73. /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
  74. * by defining bit 1 of the "stretch" parameter to mean put sectors on the
  75. * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
  76. * drives are "upside-down").
  77. */
  78. /*
  79. * 1995/8/26 -- Andreas Busse -- added Mips support.
  80. */
  81. /*
  82. * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
  83. * features to asm/floppy.h.
  84. */
  85. /*
  86. * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
  87. * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
  88. * use of '0' for NULL.
  89. */
  90. /*
  91. * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
  92. * failures.
  93. */
  94. /*
  95. * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
  96. */
  97. /*
  98. * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
  99. * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
  100. * being used to store jiffies, which are unsigned longs).
  101. */
  102. /*
  103. * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  104. * - get rid of check_region
  105. * - s/suser/capable/
  106. */
  107. /*
  108. * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no
  109. * floppy controller (lingering task on list after module is gone... boom.)
  110. */
  111. /*
  112. * 2002/02/07 -- Anton Altaparmakov - Fix io ports reservation to correct range
  113. * (0x3f2-0x3f5, 0x3f7). This fix is a bit of a hack but the proper fix
  114. * requires many non-obvious changes in arch dependent code.
  115. */
  116. /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
  117. * Better audit of register_blkdev.
  118. */
  119. #define FLOPPY_SANITY_CHECK
  120. #undef FLOPPY_SILENT_DCL_CLEAR
  121. #define REALLY_SLOW_IO
  122. #define DEBUGT 2
  123. #define DCL_DEBUG /* debug disk change line */
  124. /* do print messages for unexpected interrupts */
  125. static int print_unex = 1;
  126. #include <linux/module.h>
  127. #include <linux/sched.h>
  128. #include <linux/fs.h>
  129. #include <linux/kernel.h>
  130. #include <linux/timer.h>
  131. #include <linux/workqueue.h>
  132. #define FDPATCHES
  133. #include <linux/fdreg.h>
  134. /*
  135. * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
  136. */
  137. #include <linux/fd.h>
  138. #include <linux/hdreg.h>
  139. #include <linux/errno.h>
  140. #include <linux/slab.h>
  141. #include <linux/mm.h>
  142. #include <linux/bio.h>
  143. #include <linux/string.h>
  144. #include <linux/fcntl.h>
  145. #include <linux/delay.h>
  146. #include <linux/mc146818rtc.h> /* CMOS defines */
  147. #include <linux/ioport.h>
  148. #include <linux/interrupt.h>
  149. #include <linux/init.h>
  150. #include <linux/devfs_fs_kernel.h>
  151. #include <linux/device.h>
  152. #include <linux/buffer_head.h> /* for invalidate_buffers() */
  153. /*
  154. * PS/2 floppies have much slower step rates than regular floppies.
  155. * It's been recommended that take about 1/4 of the default speed
  156. * in some more extreme cases.
  157. */
  158. static int slow_floppy;
  159. #include <asm/dma.h>
  160. #include <asm/irq.h>
  161. #include <asm/system.h>
  162. #include <asm/io.h>
  163. #include <asm/uaccess.h>
  164. static int FLOPPY_IRQ = 6;
  165. static int FLOPPY_DMA = 2;
  166. static int can_use_virtual_dma = 2;
  167. /* =======
  168. * can use virtual DMA:
  169. * 0 = use of virtual DMA disallowed by config
  170. * 1 = use of virtual DMA prescribed by config
  171. * 2 = no virtual DMA preference configured. By default try hard DMA,
  172. * but fall back on virtual DMA when not enough memory available
  173. */
  174. static int use_virtual_dma;
  175. /* =======
  176. * use virtual DMA
  177. * 0 using hard DMA
  178. * 1 using virtual DMA
  179. * This variable is set to virtual when a DMA mem problem arises, and
  180. * reset back in floppy_grab_irq_and_dma.
  181. * It is not safe to reset it in other circumstances, because the floppy
  182. * driver may have several buffers in use at once, and we do currently not
  183. * record each buffers capabilities
  184. */
  185. static DEFINE_SPINLOCK(floppy_lock);
  186. static struct completion device_release;
  187. static unsigned short virtual_dma_port = 0x3f0;
  188. irqreturn_t floppy_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  189. static int set_dor(int fdc, char mask, char data);
  190. static void register_devfs_entries(int drive) __init;
  191. #define K_64 0x10000 /* 64KB */
  192. /* the following is the mask of allowed drives. By default units 2 and
  193. * 3 of both floppy controllers are disabled, because switching on the
  194. * motor of these drives causes system hangs on some PCI computers. drive
  195. * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
  196. * a drive is allowed.
  197. *
  198. * NOTE: This must come before we include the arch floppy header because
  199. * some ports reference this variable from there. -DaveM
  200. */
  201. static int allowed_drive_mask = 0x33;
  202. #include <asm/floppy.h>
  203. static int irqdma_allocated;
  204. #define LOCAL_END_REQUEST
  205. #define DEVICE_NAME "floppy"
  206. #include <linux/blkdev.h>
  207. #include <linux/blkpg.h>
  208. #include <linux/cdrom.h> /* for the compatibility eject ioctl */
  209. #include <linux/completion.h>
  210. static struct request *current_req;
  211. static struct request_queue *floppy_queue;
  212. static void do_fd_request(request_queue_t * q);
  213. #ifndef fd_get_dma_residue
  214. #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
  215. #endif
  216. /* Dma Memory related stuff */
  217. #ifndef fd_dma_mem_free
  218. #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
  219. #endif
  220. #ifndef fd_dma_mem_alloc
  221. #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
  222. #endif
  223. static inline void fallback_on_nodma_alloc(char **addr, size_t l)
  224. {
  225. #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
  226. if (*addr)
  227. return; /* we have the memory */
  228. if (can_use_virtual_dma != 2)
  229. return; /* no fallback allowed */
  230. printk
  231. ("DMA memory shortage. Temporarily falling back on virtual DMA\n");
  232. *addr = (char *)nodma_mem_alloc(l);
  233. #else
  234. return;
  235. #endif
  236. }
  237. /* End dma memory related stuff */
  238. static unsigned long fake_change;
  239. static int initialising = 1;
  240. #define ITYPE(x) (((x)>>2) & 0x1f)
  241. #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
  242. #define UNIT(x) ((x) & 0x03) /* drive on fdc */
  243. #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
  244. #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
  245. /* reverse mapping from unit and fdc to drive */
  246. #define DP (&drive_params[current_drive])
  247. #define DRS (&drive_state[current_drive])
  248. #define DRWE (&write_errors[current_drive])
  249. #define FDCS (&fdc_state[fdc])
  250. #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
  251. #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
  252. #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
  253. #define UDP (&drive_params[drive])
  254. #define UDRS (&drive_state[drive])
  255. #define UDRWE (&write_errors[drive])
  256. #define UFDCS (&fdc_state[FDC(drive)])
  257. #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
  258. #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
  259. #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
  260. #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
  261. #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
  262. #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
  263. #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
  264. /* read/write */
  265. #define COMMAND raw_cmd->cmd[0]
  266. #define DR_SELECT raw_cmd->cmd[1]
  267. #define TRACK raw_cmd->cmd[2]
  268. #define HEAD raw_cmd->cmd[3]
  269. #define SECTOR raw_cmd->cmd[4]
  270. #define SIZECODE raw_cmd->cmd[5]
  271. #define SECT_PER_TRACK raw_cmd->cmd[6]
  272. #define GAP raw_cmd->cmd[7]
  273. #define SIZECODE2 raw_cmd->cmd[8]
  274. #define NR_RW 9
  275. /* format */
  276. #define F_SIZECODE raw_cmd->cmd[2]
  277. #define F_SECT_PER_TRACK raw_cmd->cmd[3]
  278. #define F_GAP raw_cmd->cmd[4]
  279. #define F_FILL raw_cmd->cmd[5]
  280. #define NR_F 6
  281. /*
  282. * Maximum disk size (in kilobytes). This default is used whenever the
  283. * current disk size is unknown.
  284. * [Now it is rather a minimum]
  285. */
  286. #define MAX_DISK_SIZE 4 /* 3984 */
  287. /*
  288. * globals used by 'result()'
  289. */
  290. #define MAX_REPLIES 16
  291. static unsigned char reply_buffer[MAX_REPLIES];
  292. static int inr; /* size of reply buffer, when called from interrupt */
  293. #define ST0 (reply_buffer[0])
  294. #define ST1 (reply_buffer[1])
  295. #define ST2 (reply_buffer[2])
  296. #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
  297. #define R_TRACK (reply_buffer[3])
  298. #define R_HEAD (reply_buffer[4])
  299. #define R_SECTOR (reply_buffer[5])
  300. #define R_SIZECODE (reply_buffer[6])
  301. #define SEL_DLY (2*HZ/100)
  302. /*
  303. * this struct defines the different floppy drive types.
  304. */
  305. static struct {
  306. struct floppy_drive_params params;
  307. const char *name; /* name printed while booting */
  308. } default_drive_params[] = {
  309. /* NOTE: the time values in jiffies should be in msec!
  310. CMOS drive type
  311. | Maximum data rate supported by drive type
  312. | | Head load time, msec
  313. | | | Head unload time, msec (not used)
  314. | | | | Step rate interval, usec
  315. | | | | | Time needed for spinup time (jiffies)
  316. | | | | | | Timeout for spinning down (jiffies)
  317. | | | | | | | Spindown offset (where disk stops)
  318. | | | | | | | | Select delay
  319. | | | | | | | | | RPS
  320. | | | | | | | | | | Max number of tracks
  321. | | | | | | | | | | | Interrupt timeout
  322. | | | | | | | | | | | | Max nonintlv. sectors
  323. | | | | | | | | | | | | | -Max Errors- flags */
  324. {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
  325. 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
  326. {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
  327. 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
  328. {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
  329. 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
  330. {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
  331. 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
  332. {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
  333. 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
  334. {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
  335. 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
  336. {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
  337. 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
  338. /* | --autodetected formats--- | | |
  339. * read_track | | Name printed when booting
  340. * | Native format
  341. * Frequency of disk change checks */
  342. };
  343. static struct floppy_drive_params drive_params[N_DRIVE];
  344. static struct floppy_drive_struct drive_state[N_DRIVE];
  345. static struct floppy_write_errors write_errors[N_DRIVE];
  346. static struct timer_list motor_off_timer[N_DRIVE];
  347. static struct gendisk *disks[N_DRIVE];
  348. static struct block_device *opened_bdev[N_DRIVE];
  349. static DECLARE_MUTEX(open_lock);
  350. static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
  351. /*
  352. * This struct defines the different floppy types.
  353. *
  354. * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
  355. * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
  356. * tells if the disk is in Commodore 1581 format, which means side 0 sectors
  357. * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
  358. * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
  359. * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
  360. * side 0 is on physical side 0 (but with the misnamed sector IDs).
  361. * 'stretch' should probably be renamed to something more general, like
  362. * 'options'. Other parameters should be self-explanatory (see also
  363. * setfdprm(8)).
  364. */
  365. /*
  366. Size
  367. | Sectors per track
  368. | | Head
  369. | | | Tracks
  370. | | | | Stretch
  371. | | | | | Gap 1 size
  372. | | | | | | Data rate, | 0x40 for perp
  373. | | | | | | | Spec1 (stepping rate, head unload
  374. | | | | | | | | /fmt gap (gap2) */
  375. static struct floppy_struct floppy_type[32] = {
  376. { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
  377. { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
  378. { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
  379. { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
  380. { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
  381. { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
  382. { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
  383. { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
  384. { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
  385. { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
  386. { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
  387. { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
  388. { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
  389. { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
  390. { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
  391. { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
  392. { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
  393. { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
  394. { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
  395. { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
  396. { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
  397. { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
  398. { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
  399. { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
  400. { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
  401. { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
  402. { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
  403. { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
  404. { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
  405. { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
  406. { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
  407. { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
  408. };
  409. #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
  410. #define SECTSIZE (_FD_SECTSIZE(*floppy))
  411. /* Auto-detection: Disk type used until the next media change occurs. */
  412. static struct floppy_struct *current_type[N_DRIVE];
  413. /*
  414. * User-provided type information. current_type points to
  415. * the respective entry of this array.
  416. */
  417. static struct floppy_struct user_params[N_DRIVE];
  418. static sector_t floppy_sizes[256];
  419. /*
  420. * The driver is trying to determine the correct media format
  421. * while probing is set. rw_interrupt() clears it after a
  422. * successful access.
  423. */
  424. static int probing;
  425. /* Synchronization of FDC access. */
  426. #define FD_COMMAND_NONE -1
  427. #define FD_COMMAND_ERROR 2
  428. #define FD_COMMAND_OKAY 3
  429. static volatile int command_status = FD_COMMAND_NONE;
  430. static unsigned long fdc_busy;
  431. static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
  432. static DECLARE_WAIT_QUEUE_HEAD(command_done);
  433. #define NO_SIGNAL (!interruptible || !signal_pending(current))
  434. #define CALL(x) if ((x) == -EINTR) return -EINTR
  435. #define ECALL(x) if ((ret = (x))) return ret;
  436. #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
  437. #define WAIT(x) _WAIT((x),interruptible)
  438. #define IWAIT(x) _WAIT((x),1)
  439. /* Errors during formatting are counted here. */
  440. static int format_errors;
  441. /* Format request descriptor. */
  442. static struct format_descr format_req;
  443. /*
  444. * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
  445. * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
  446. * H is head unload time (1=16ms, 2=32ms, etc)
  447. */
  448. /*
  449. * Track buffer
  450. * Because these are written to by the DMA controller, they must
  451. * not contain a 64k byte boundary crossing, or data will be
  452. * corrupted/lost.
  453. */
  454. static char *floppy_track_buffer;
  455. static int max_buffer_sectors;
  456. static int *errors;
  457. typedef void (*done_f) (int);
  458. static struct cont_t {
  459. void (*interrupt) (void); /* this is called after the interrupt of the
  460. * main command */
  461. void (*redo) (void); /* this is called to retry the operation */
  462. void (*error) (void); /* this is called to tally an error */
  463. done_f done; /* this is called to say if the operation has
  464. * succeeded/failed */
  465. } *cont;
  466. static void floppy_ready(void);
  467. static void floppy_start(void);
  468. static void process_fd_request(void);
  469. static void recalibrate_floppy(void);
  470. static void floppy_shutdown(unsigned long);
  471. static int floppy_grab_irq_and_dma(void);
  472. static void floppy_release_irq_and_dma(void);
  473. /*
  474. * The "reset" variable should be tested whenever an interrupt is scheduled,
  475. * after the commands have been sent. This is to ensure that the driver doesn't
  476. * get wedged when the interrupt doesn't come because of a failed command.
  477. * reset doesn't need to be tested before sending commands, because
  478. * output_byte is automatically disabled when reset is set.
  479. */
  480. #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
  481. static void reset_fdc(void);
  482. /*
  483. * These are global variables, as that's the easiest way to give
  484. * information to interrupts. They are the data used for the current
  485. * request.
  486. */
  487. #define NO_TRACK -1
  488. #define NEED_1_RECAL -2
  489. #define NEED_2_RECAL -3
  490. static int usage_count;
  491. /* buffer related variables */
  492. static int buffer_track = -1;
  493. static int buffer_drive = -1;
  494. static int buffer_min = -1;
  495. static int buffer_max = -1;
  496. /* fdc related variables, should end up in a struct */
  497. static struct floppy_fdc_state fdc_state[N_FDC];
  498. static int fdc; /* current fdc */
  499. static struct floppy_struct *_floppy = floppy_type;
  500. static unsigned char current_drive;
  501. static long current_count_sectors;
  502. static unsigned char fsector_t; /* sector in track */
  503. static unsigned char in_sector_offset; /* offset within physical sector,
  504. * expressed in units of 512 bytes */
  505. #ifndef fd_eject
  506. static inline int fd_eject(int drive)
  507. {
  508. return -EINVAL;
  509. }
  510. #endif
  511. /*
  512. * Debugging
  513. * =========
  514. */
  515. #ifdef DEBUGT
  516. static long unsigned debugtimer;
  517. static inline void set_debugt(void)
  518. {
  519. debugtimer = jiffies;
  520. }
  521. static inline void debugt(const char *message)
  522. {
  523. if (DP->flags & DEBUGT)
  524. printk("%s dtime=%lu\n", message, jiffies - debugtimer);
  525. }
  526. #else
  527. static inline void set_debugt(void) { }
  528. static inline void debugt(const char *message) { }
  529. #endif /* DEBUGT */
  530. typedef void (*timeout_fn) (unsigned long);
  531. static struct timer_list fd_timeout = TIMER_INITIALIZER(floppy_shutdown, 0, 0);
  532. static const char *timeout_message;
  533. #ifdef FLOPPY_SANITY_CHECK
  534. static void is_alive(const char *message)
  535. {
  536. /* this routine checks whether the floppy driver is "alive" */
  537. if (test_bit(0, &fdc_busy) && command_status < 2
  538. && !timer_pending(&fd_timeout)) {
  539. DPRINT("timeout handler died: %s\n", message);
  540. }
  541. }
  542. #endif
  543. static void (*do_floppy) (void) = NULL;
  544. #ifdef FLOPPY_SANITY_CHECK
  545. #define OLOGSIZE 20
  546. static void (*lasthandler) (void);
  547. static unsigned long interruptjiffies;
  548. static unsigned long resultjiffies;
  549. static int resultsize;
  550. static unsigned long lastredo;
  551. static struct output_log {
  552. unsigned char data;
  553. unsigned char status;
  554. unsigned long jiffies;
  555. } output_log[OLOGSIZE];
  556. static int output_log_pos;
  557. #endif
  558. #define current_reqD -1
  559. #define MAXTIMEOUT -2
  560. static void __reschedule_timeout(int drive, const char *message, int marg)
  561. {
  562. if (drive == current_reqD)
  563. drive = current_drive;
  564. del_timer(&fd_timeout);
  565. if (drive < 0 || drive > N_DRIVE) {
  566. fd_timeout.expires = jiffies + 20UL * HZ;
  567. drive = 0;
  568. } else
  569. fd_timeout.expires = jiffies + UDP->timeout;
  570. add_timer(&fd_timeout);
  571. if (UDP->flags & FD_DEBUG) {
  572. DPRINT("reschedule timeout ");
  573. printk(message, marg);
  574. printk("\n");
  575. }
  576. timeout_message = message;
  577. }
  578. static void reschedule_timeout(int drive, const char *message, int marg)
  579. {
  580. unsigned long flags;
  581. spin_lock_irqsave(&floppy_lock, flags);
  582. __reschedule_timeout(drive, message, marg);
  583. spin_unlock_irqrestore(&floppy_lock, flags);
  584. }
  585. #define INFBOUND(a,b) (a)=max_t(int, a, b)
  586. #define SUPBOUND(a,b) (a)=min_t(int, a, b)
  587. /*
  588. * Bottom half floppy driver.
  589. * ==========================
  590. *
  591. * This part of the file contains the code talking directly to the hardware,
  592. * and also the main service loop (seek-configure-spinup-command)
  593. */
  594. /*
  595. * disk change.
  596. * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
  597. * and the last_checked date.
  598. *
  599. * last_checked is the date of the last check which showed 'no disk change'
  600. * FD_DISK_CHANGE is set under two conditions:
  601. * 1. The floppy has been changed after some i/o to that floppy already
  602. * took place.
  603. * 2. No floppy disk is in the drive. This is done in order to ensure that
  604. * requests are quickly flushed in case there is no disk in the drive. It
  605. * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
  606. * the drive.
  607. *
  608. * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
  609. * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
  610. * each seek. If a disk is present, the disk change line should also be
  611. * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
  612. * change line is set, this means either that no disk is in the drive, or
  613. * that it has been removed since the last seek.
  614. *
  615. * This means that we really have a third possibility too:
  616. * The floppy has been changed after the last seek.
  617. */
  618. static int disk_change(int drive)
  619. {
  620. int fdc = FDC(drive);
  621. #ifdef FLOPPY_SANITY_CHECK
  622. if (jiffies - UDRS->select_date < UDP->select_delay)
  623. DPRINT("WARNING disk change called early\n");
  624. if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
  625. (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
  626. DPRINT("probing disk change on unselected drive\n");
  627. DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
  628. (unsigned int)FDCS->dor);
  629. }
  630. #endif
  631. #ifdef DCL_DEBUG
  632. if (UDP->flags & FD_DEBUG) {
  633. DPRINT("checking disk change line for drive %d\n", drive);
  634. DPRINT("jiffies=%lu\n", jiffies);
  635. DPRINT("disk change line=%x\n", fd_inb(FD_DIR) & 0x80);
  636. DPRINT("flags=%lx\n", UDRS->flags);
  637. }
  638. #endif
  639. if (UDP->flags & FD_BROKEN_DCL)
  640. return UTESTF(FD_DISK_CHANGED);
  641. if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
  642. USETF(FD_VERIFY); /* verify write protection */
  643. if (UDRS->maxblock) {
  644. /* mark it changed */
  645. USETF(FD_DISK_CHANGED);
  646. }
  647. /* invalidate its geometry */
  648. if (UDRS->keep_data >= 0) {
  649. if ((UDP->flags & FTD_MSG) &&
  650. current_type[drive] != NULL)
  651. DPRINT("Disk type is undefined after "
  652. "disk change\n");
  653. current_type[drive] = NULL;
  654. floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
  655. }
  656. /*USETF(FD_DISK_NEWCHANGE); */
  657. return 1;
  658. } else {
  659. UDRS->last_checked = jiffies;
  660. UCLEARF(FD_DISK_NEWCHANGE);
  661. }
  662. return 0;
  663. }
  664. static inline int is_selected(int dor, int unit)
  665. {
  666. return ((dor & (0x10 << unit)) && (dor & 3) == unit);
  667. }
  668. static int set_dor(int fdc, char mask, char data)
  669. {
  670. register unsigned char drive, unit, newdor, olddor;
  671. if (FDCS->address == -1)
  672. return -1;
  673. olddor = FDCS->dor;
  674. newdor = (olddor & mask) | data;
  675. if (newdor != olddor) {
  676. unit = olddor & 0x3;
  677. if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
  678. drive = REVDRIVE(fdc, unit);
  679. #ifdef DCL_DEBUG
  680. if (UDP->flags & FD_DEBUG) {
  681. DPRINT("calling disk change from set_dor\n");
  682. }
  683. #endif
  684. disk_change(drive);
  685. }
  686. FDCS->dor = newdor;
  687. fd_outb(newdor, FD_DOR);
  688. unit = newdor & 0x3;
  689. if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
  690. drive = REVDRIVE(fdc, unit);
  691. UDRS->select_date = jiffies;
  692. }
  693. }
  694. /*
  695. * We should propagate failures to grab the resources back
  696. * nicely from here. Actually we ought to rewrite the fd
  697. * driver some day too.
  698. */
  699. if (newdor & FLOPPY_MOTOR_MASK)
  700. floppy_grab_irq_and_dma();
  701. if (olddor & FLOPPY_MOTOR_MASK)
  702. floppy_release_irq_and_dma();
  703. return olddor;
  704. }
  705. static void twaddle(void)
  706. {
  707. if (DP->select_delay)
  708. return;
  709. fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
  710. fd_outb(FDCS->dor, FD_DOR);
  711. DRS->select_date = jiffies;
  712. }
  713. /* reset all driver information about the current fdc. This is needed after
  714. * a reset, and after a raw command. */
  715. static void reset_fdc_info(int mode)
  716. {
  717. int drive;
  718. FDCS->spec1 = FDCS->spec2 = -1;
  719. FDCS->need_configure = 1;
  720. FDCS->perp_mode = 1;
  721. FDCS->rawcmd = 0;
  722. for (drive = 0; drive < N_DRIVE; drive++)
  723. if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
  724. UDRS->track = NEED_2_RECAL;
  725. }
  726. /* selects the fdc and drive, and enables the fdc's input/dma. */
  727. static void set_fdc(int drive)
  728. {
  729. if (drive >= 0 && drive < N_DRIVE) {
  730. fdc = FDC(drive);
  731. current_drive = drive;
  732. }
  733. if (fdc != 1 && fdc != 0) {
  734. printk("bad fdc value\n");
  735. return;
  736. }
  737. set_dor(fdc, ~0, 8);
  738. #if N_FDC > 1
  739. set_dor(1 - fdc, ~8, 0);
  740. #endif
  741. if (FDCS->rawcmd == 2)
  742. reset_fdc_info(1);
  743. if (fd_inb(FD_STATUS) != STATUS_READY)
  744. FDCS->reset = 1;
  745. }
  746. /* locks the driver */
  747. static int _lock_fdc(int drive, int interruptible, int line)
  748. {
  749. if (!usage_count) {
  750. printk(KERN_ERR
  751. "Trying to lock fdc while usage count=0 at line %d\n",
  752. line);
  753. return -1;
  754. }
  755. if (floppy_grab_irq_and_dma() == -1)
  756. return -EBUSY;
  757. if (test_and_set_bit(0, &fdc_busy)) {
  758. DECLARE_WAITQUEUE(wait, current);
  759. add_wait_queue(&fdc_wait, &wait);
  760. for (;;) {
  761. set_current_state(TASK_INTERRUPTIBLE);
  762. if (!test_and_set_bit(0, &fdc_busy))
  763. break;
  764. schedule();
  765. if (!NO_SIGNAL) {
  766. remove_wait_queue(&fdc_wait, &wait);
  767. return -EINTR;
  768. }
  769. }
  770. set_current_state(TASK_RUNNING);
  771. remove_wait_queue(&fdc_wait, &wait);
  772. }
  773. command_status = FD_COMMAND_NONE;
  774. __reschedule_timeout(drive, "lock fdc", 0);
  775. set_fdc(drive);
  776. return 0;
  777. }
  778. #define lock_fdc(drive,interruptible) _lock_fdc(drive,interruptible, __LINE__)
  779. #define LOCK_FDC(drive,interruptible) \
  780. if (lock_fdc(drive,interruptible)) return -EINTR;
  781. /* unlocks the driver */
  782. static inline void unlock_fdc(void)
  783. {
  784. unsigned long flags;
  785. raw_cmd = NULL;
  786. if (!test_bit(0, &fdc_busy))
  787. DPRINT("FDC access conflict!\n");
  788. if (do_floppy)
  789. DPRINT("device interrupt still active at FDC release: %p!\n",
  790. do_floppy);
  791. command_status = FD_COMMAND_NONE;
  792. spin_lock_irqsave(&floppy_lock, flags);
  793. del_timer(&fd_timeout);
  794. cont = NULL;
  795. clear_bit(0, &fdc_busy);
  796. if (elv_next_request(floppy_queue))
  797. do_fd_request(floppy_queue);
  798. spin_unlock_irqrestore(&floppy_lock, flags);
  799. floppy_release_irq_and_dma();
  800. wake_up(&fdc_wait);
  801. }
  802. /* switches the motor off after a given timeout */
  803. static void motor_off_callback(unsigned long nr)
  804. {
  805. unsigned char mask = ~(0x10 << UNIT(nr));
  806. set_dor(FDC(nr), mask, 0);
  807. }
  808. /* schedules motor off */
  809. static void floppy_off(unsigned int drive)
  810. {
  811. unsigned long volatile delta;
  812. register int fdc = FDC(drive);
  813. if (!(FDCS->dor & (0x10 << UNIT(drive))))
  814. return;
  815. del_timer(motor_off_timer + drive);
  816. /* make spindle stop in a position which minimizes spinup time
  817. * next time */
  818. if (UDP->rps) {
  819. delta = jiffies - UDRS->first_read_date + HZ -
  820. UDP->spindown_offset;
  821. delta = ((delta * UDP->rps) % HZ) / UDP->rps;
  822. motor_off_timer[drive].expires =
  823. jiffies + UDP->spindown - delta;
  824. }
  825. add_timer(motor_off_timer + drive);
  826. }
  827. /*
  828. * cycle through all N_DRIVE floppy drives, for disk change testing.
  829. * stopping at current drive. This is done before any long operation, to
  830. * be sure to have up to date disk change information.
  831. */
  832. static void scandrives(void)
  833. {
  834. int i, drive, saved_drive;
  835. if (DP->select_delay)
  836. return;
  837. saved_drive = current_drive;
  838. for (i = 0; i < N_DRIVE; i++) {
  839. drive = (saved_drive + i + 1) % N_DRIVE;
  840. if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
  841. continue; /* skip closed drives */
  842. set_fdc(drive);
  843. if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
  844. (0x10 << UNIT(drive))))
  845. /* switch the motor off again, if it was off to
  846. * begin with */
  847. set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
  848. }
  849. set_fdc(saved_drive);
  850. }
  851. static void empty(void)
  852. {
  853. }
  854. static DECLARE_WORK(floppy_work, NULL, NULL);
  855. static void schedule_bh(void (*handler) (void))
  856. {
  857. PREPARE_WORK(&floppy_work, (void (*)(void *))handler, NULL);
  858. schedule_work(&floppy_work);
  859. }
  860. static struct timer_list fd_timer = TIMER_INITIALIZER(NULL, 0, 0);
  861. static void cancel_activity(void)
  862. {
  863. unsigned long flags;
  864. spin_lock_irqsave(&floppy_lock, flags);
  865. do_floppy = NULL;
  866. PREPARE_WORK(&floppy_work, (void *)empty, NULL);
  867. del_timer(&fd_timer);
  868. spin_unlock_irqrestore(&floppy_lock, flags);
  869. }
  870. /* this function makes sure that the disk stays in the drive during the
  871. * transfer */
  872. static void fd_watchdog(void)
  873. {
  874. #ifdef DCL_DEBUG
  875. if (DP->flags & FD_DEBUG) {
  876. DPRINT("calling disk change from watchdog\n");
  877. }
  878. #endif
  879. if (disk_change(current_drive)) {
  880. DPRINT("disk removed during i/o\n");
  881. cancel_activity();
  882. cont->done(0);
  883. reset_fdc();
  884. } else {
  885. del_timer(&fd_timer);
  886. fd_timer.function = (timeout_fn) fd_watchdog;
  887. fd_timer.expires = jiffies + HZ / 10;
  888. add_timer(&fd_timer);
  889. }
  890. }
  891. static void main_command_interrupt(void)
  892. {
  893. del_timer(&fd_timer);
  894. cont->interrupt();
  895. }
  896. /* waits for a delay (spinup or select) to pass */
  897. static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
  898. {
  899. if (FDCS->reset) {
  900. reset_fdc(); /* do the reset during sleep to win time
  901. * if we don't need to sleep, it's a good
  902. * occasion anyways */
  903. return 1;
  904. }
  905. if ((signed)(jiffies - delay) < 0) {
  906. del_timer(&fd_timer);
  907. fd_timer.function = function;
  908. fd_timer.expires = delay;
  909. add_timer(&fd_timer);
  910. return 1;
  911. }
  912. return 0;
  913. }
  914. static DEFINE_SPINLOCK(floppy_hlt_lock);
  915. static int hlt_disabled;
  916. static void floppy_disable_hlt(void)
  917. {
  918. unsigned long flags;
  919. spin_lock_irqsave(&floppy_hlt_lock, flags);
  920. if (!hlt_disabled) {
  921. hlt_disabled = 1;
  922. #ifdef HAVE_DISABLE_HLT
  923. disable_hlt();
  924. #endif
  925. }
  926. spin_unlock_irqrestore(&floppy_hlt_lock, flags);
  927. }
  928. static void floppy_enable_hlt(void)
  929. {
  930. unsigned long flags;
  931. spin_lock_irqsave(&floppy_hlt_lock, flags);
  932. if (hlt_disabled) {
  933. hlt_disabled = 0;
  934. #ifdef HAVE_DISABLE_HLT
  935. enable_hlt();
  936. #endif
  937. }
  938. spin_unlock_irqrestore(&floppy_hlt_lock, flags);
  939. }
  940. static void setup_DMA(void)
  941. {
  942. unsigned long f;
  943. #ifdef FLOPPY_SANITY_CHECK
  944. if (raw_cmd->length == 0) {
  945. int i;
  946. printk("zero dma transfer size:");
  947. for (i = 0; i < raw_cmd->cmd_count; i++)
  948. printk("%x,", raw_cmd->cmd[i]);
  949. printk("\n");
  950. cont->done(0);
  951. FDCS->reset = 1;
  952. return;
  953. }
  954. if (((unsigned long)raw_cmd->kernel_data) % 512) {
  955. printk("non aligned address: %p\n", raw_cmd->kernel_data);
  956. cont->done(0);
  957. FDCS->reset = 1;
  958. return;
  959. }
  960. #endif
  961. f = claim_dma_lock();
  962. fd_disable_dma();
  963. #ifdef fd_dma_setup
  964. if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
  965. (raw_cmd->flags & FD_RAW_READ) ?
  966. DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
  967. release_dma_lock(f);
  968. cont->done(0);
  969. FDCS->reset = 1;
  970. return;
  971. }
  972. release_dma_lock(f);
  973. #else
  974. fd_clear_dma_ff();
  975. fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
  976. fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
  977. DMA_MODE_READ : DMA_MODE_WRITE);
  978. fd_set_dma_addr(raw_cmd->kernel_data);
  979. fd_set_dma_count(raw_cmd->length);
  980. virtual_dma_port = FDCS->address;
  981. fd_enable_dma();
  982. release_dma_lock(f);
  983. #endif
  984. floppy_disable_hlt();
  985. }
  986. static void show_floppy(void);
  987. /* waits until the fdc becomes ready */
  988. static int wait_til_ready(void)
  989. {
  990. int counter, status;
  991. if (FDCS->reset)
  992. return -1;
  993. for (counter = 0; counter < 10000; counter++) {
  994. status = fd_inb(FD_STATUS);
  995. if (status & STATUS_READY)
  996. return status;
  997. }
  998. if (!initialising) {
  999. DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
  1000. show_floppy();
  1001. }
  1002. FDCS->reset = 1;
  1003. return -1;
  1004. }
  1005. /* sends a command byte to the fdc */
  1006. static int output_byte(char byte)
  1007. {
  1008. int status;
  1009. if ((status = wait_til_ready()) < 0)
  1010. return -1;
  1011. if ((status & (STATUS_READY | STATUS_DIR | STATUS_DMA)) == STATUS_READY) {
  1012. fd_outb(byte, FD_DATA);
  1013. #ifdef FLOPPY_SANITY_CHECK
  1014. output_log[output_log_pos].data = byte;
  1015. output_log[output_log_pos].status = status;
  1016. output_log[output_log_pos].jiffies = jiffies;
  1017. output_log_pos = (output_log_pos + 1) % OLOGSIZE;
  1018. #endif
  1019. return 0;
  1020. }
  1021. FDCS->reset = 1;
  1022. if (!initialising) {
  1023. DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
  1024. byte, fdc, status);
  1025. show_floppy();
  1026. }
  1027. return -1;
  1028. }
  1029. #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
  1030. /* gets the response from the fdc */
  1031. static int result(void)
  1032. {
  1033. int i, status = 0;
  1034. for (i = 0; i < MAX_REPLIES; i++) {
  1035. if ((status = wait_til_ready()) < 0)
  1036. break;
  1037. status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
  1038. if ((status & ~STATUS_BUSY) == STATUS_READY) {
  1039. #ifdef FLOPPY_SANITY_CHECK
  1040. resultjiffies = jiffies;
  1041. resultsize = i;
  1042. #endif
  1043. return i;
  1044. }
  1045. if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
  1046. reply_buffer[i] = fd_inb(FD_DATA);
  1047. else
  1048. break;
  1049. }
  1050. if (!initialising) {
  1051. DPRINT
  1052. ("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
  1053. fdc, status, i);
  1054. show_floppy();
  1055. }
  1056. FDCS->reset = 1;
  1057. return -1;
  1058. }
  1059. #define MORE_OUTPUT -2
  1060. /* does the fdc need more output? */
  1061. static int need_more_output(void)
  1062. {
  1063. int status;
  1064. if ((status = wait_til_ready()) < 0)
  1065. return -1;
  1066. if ((status & (STATUS_READY | STATUS_DIR | STATUS_DMA)) == STATUS_READY)
  1067. return MORE_OUTPUT;
  1068. return result();
  1069. }
  1070. /* Set perpendicular mode as required, based on data rate, if supported.
  1071. * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
  1072. */
  1073. static inline void perpendicular_mode(void)
  1074. {
  1075. unsigned char perp_mode;
  1076. if (raw_cmd->rate & 0x40) {
  1077. switch (raw_cmd->rate & 3) {
  1078. case 0:
  1079. perp_mode = 2;
  1080. break;
  1081. case 3:
  1082. perp_mode = 3;
  1083. break;
  1084. default:
  1085. DPRINT("Invalid data rate for perpendicular mode!\n");
  1086. cont->done(0);
  1087. FDCS->reset = 1; /* convenient way to return to
  1088. * redo without to much hassle (deep
  1089. * stack et al. */
  1090. return;
  1091. }
  1092. } else
  1093. perp_mode = 0;
  1094. if (FDCS->perp_mode == perp_mode)
  1095. return;
  1096. if (FDCS->version >= FDC_82077_ORIG) {
  1097. output_byte(FD_PERPENDICULAR);
  1098. output_byte(perp_mode);
  1099. FDCS->perp_mode = perp_mode;
  1100. } else if (perp_mode) {
  1101. DPRINT("perpendicular mode not supported by this FDC.\n");
  1102. }
  1103. } /* perpendicular_mode */
  1104. static int fifo_depth = 0xa;
  1105. static int no_fifo;
  1106. static int fdc_configure(void)
  1107. {
  1108. /* Turn on FIFO */
  1109. output_byte(FD_CONFIGURE);
  1110. if (need_more_output() != MORE_OUTPUT)
  1111. return 0;
  1112. output_byte(0);
  1113. output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
  1114. output_byte(0); /* pre-compensation from track
  1115. 0 upwards */
  1116. return 1;
  1117. }
  1118. #define NOMINAL_DTR 500
  1119. /* Issue a "SPECIFY" command to set the step rate time, head unload time,
  1120. * head load time, and DMA disable flag to values needed by floppy.
  1121. *
  1122. * The value "dtr" is the data transfer rate in Kbps. It is needed
  1123. * to account for the data rate-based scaling done by the 82072 and 82077
  1124. * FDC types. This parameter is ignored for other types of FDCs (i.e.
  1125. * 8272a).
  1126. *
  1127. * Note that changing the data transfer rate has a (probably deleterious)
  1128. * effect on the parameters subject to scaling for 82072/82077 FDCs, so
  1129. * fdc_specify is called again after each data transfer rate
  1130. * change.
  1131. *
  1132. * srt: 1000 to 16000 in microseconds
  1133. * hut: 16 to 240 milliseconds
  1134. * hlt: 2 to 254 milliseconds
  1135. *
  1136. * These values are rounded up to the next highest available delay time.
  1137. */
  1138. static void fdc_specify(void)
  1139. {
  1140. unsigned char spec1, spec2;
  1141. unsigned long srt, hlt, hut;
  1142. unsigned long dtr = NOMINAL_DTR;
  1143. unsigned long scale_dtr = NOMINAL_DTR;
  1144. int hlt_max_code = 0x7f;
  1145. int hut_max_code = 0xf;
  1146. if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
  1147. fdc_configure();
  1148. FDCS->need_configure = 0;
  1149. /*DPRINT("FIFO enabled\n"); */
  1150. }
  1151. switch (raw_cmd->rate & 0x03) {
  1152. case 3:
  1153. dtr = 1000;
  1154. break;
  1155. case 1:
  1156. dtr = 300;
  1157. if (FDCS->version >= FDC_82078) {
  1158. /* chose the default rate table, not the one
  1159. * where 1 = 2 Mbps */
  1160. output_byte(FD_DRIVESPEC);
  1161. if (need_more_output() == MORE_OUTPUT) {
  1162. output_byte(UNIT(current_drive));
  1163. output_byte(0xc0);
  1164. }
  1165. }
  1166. break;
  1167. case 2:
  1168. dtr = 250;
  1169. break;
  1170. }
  1171. if (FDCS->version >= FDC_82072) {
  1172. scale_dtr = dtr;
  1173. hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
  1174. hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
  1175. }
  1176. /* Convert step rate from microseconds to milliseconds and 4 bits */
  1177. srt = 16 - (DP->srt * scale_dtr / 1000 + NOMINAL_DTR - 1) / NOMINAL_DTR;
  1178. if (slow_floppy) {
  1179. srt = srt / 4;
  1180. }
  1181. SUPBOUND(srt, 0xf);
  1182. INFBOUND(srt, 0);
  1183. hlt = (DP->hlt * scale_dtr / 2 + NOMINAL_DTR - 1) / NOMINAL_DTR;
  1184. if (hlt < 0x01)
  1185. hlt = 0x01;
  1186. else if (hlt > 0x7f)
  1187. hlt = hlt_max_code;
  1188. hut = (DP->hut * scale_dtr / 16 + NOMINAL_DTR - 1) / NOMINAL_DTR;
  1189. if (hut < 0x1)
  1190. hut = 0x1;
  1191. else if (hut > 0xf)
  1192. hut = hut_max_code;
  1193. spec1 = (srt << 4) | hut;
  1194. spec2 = (hlt << 1) | (use_virtual_dma & 1);
  1195. /* If these parameters did not change, just return with success */
  1196. if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
  1197. /* Go ahead and set spec1 and spec2 */
  1198. output_byte(FD_SPECIFY);
  1199. output_byte(FDCS->spec1 = spec1);
  1200. output_byte(FDCS->spec2 = spec2);
  1201. }
  1202. } /* fdc_specify */
  1203. /* Set the FDC's data transfer rate on behalf of the specified drive.
  1204. * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
  1205. * of the specify command (i.e. using the fdc_specify function).
  1206. */
  1207. static int fdc_dtr(void)
  1208. {
  1209. /* If data rate not already set to desired value, set it. */
  1210. if ((raw_cmd->rate & 3) == FDCS->dtr)
  1211. return 0;
  1212. /* Set dtr */
  1213. fd_outb(raw_cmd->rate & 3, FD_DCR);
  1214. /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
  1215. * need a stabilization period of several milliseconds to be
  1216. * enforced after data rate changes before R/W operations.
  1217. * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
  1218. */
  1219. FDCS->dtr = raw_cmd->rate & 3;
  1220. return (fd_wait_for_completion(jiffies + 2UL * HZ / 100,
  1221. (timeout_fn) floppy_ready));
  1222. } /* fdc_dtr */
  1223. static void tell_sector(void)
  1224. {
  1225. printk(": track %d, head %d, sector %d, size %d",
  1226. R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
  1227. } /* tell_sector */
  1228. /*
  1229. * OK, this error interpreting routine is called after a
  1230. * DMA read/write has succeeded
  1231. * or failed, so we check the results, and copy any buffers.
  1232. * hhb: Added better error reporting.
  1233. * ak: Made this into a separate routine.
  1234. */
  1235. static int interpret_errors(void)
  1236. {
  1237. char bad;
  1238. if (inr != 7) {
  1239. DPRINT("-- FDC reply error");
  1240. FDCS->reset = 1;
  1241. return 1;
  1242. }
  1243. /* check IC to find cause of interrupt */
  1244. switch (ST0 & ST0_INTR) {
  1245. case 0x40: /* error occurred during command execution */
  1246. if (ST1 & ST1_EOC)
  1247. return 0; /* occurs with pseudo-DMA */
  1248. bad = 1;
  1249. if (ST1 & ST1_WP) {
  1250. DPRINT("Drive is write protected\n");
  1251. CLEARF(FD_DISK_WRITABLE);
  1252. cont->done(0);
  1253. bad = 2;
  1254. } else if (ST1 & ST1_ND) {
  1255. SETF(FD_NEED_TWADDLE);
  1256. } else if (ST1 & ST1_OR) {
  1257. if (DP->flags & FTD_MSG)
  1258. DPRINT("Over/Underrun - retrying\n");
  1259. bad = 0;
  1260. } else if (*errors >= DP->max_errors.reporting) {
  1261. DPRINT("");
  1262. if (ST0 & ST0_ECE) {
  1263. printk("Recalibrate failed!");
  1264. } else if (ST2 & ST2_CRC) {
  1265. printk("data CRC error");
  1266. tell_sector();
  1267. } else if (ST1 & ST1_CRC) {
  1268. printk("CRC error");
  1269. tell_sector();
  1270. } else if ((ST1 & (ST1_MAM | ST1_ND))
  1271. || (ST2 & ST2_MAM)) {
  1272. if (!probing) {
  1273. printk("sector not found");
  1274. tell_sector();
  1275. } else
  1276. printk("probe failed...");
  1277. } else if (ST2 & ST2_WC) { /* seek error */
  1278. printk("wrong cylinder");
  1279. } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
  1280. printk("bad cylinder");
  1281. } else {
  1282. printk
  1283. ("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
  1284. ST0, ST1, ST2);
  1285. tell_sector();
  1286. }
  1287. printk("\n");
  1288. }
  1289. if (ST2 & ST2_WC || ST2 & ST2_BC)
  1290. /* wrong cylinder => recal */
  1291. DRS->track = NEED_2_RECAL;
  1292. return bad;
  1293. case 0x80: /* invalid command given */
  1294. DPRINT("Invalid FDC command given!\n");
  1295. cont->done(0);
  1296. return 2;
  1297. case 0xc0:
  1298. DPRINT("Abnormal termination caused by polling\n");
  1299. cont->error();
  1300. return 2;
  1301. default: /* (0) Normal command termination */
  1302. return 0;
  1303. }
  1304. }
  1305. /*
  1306. * This routine is called when everything should be correctly set up
  1307. * for the transfer (i.e. floppy motor is on, the correct floppy is
  1308. * selected, and the head is sitting on the right track).
  1309. */
  1310. static void setup_rw_floppy(void)
  1311. {
  1312. int i, r, flags, dflags;
  1313. unsigned long ready_date;
  1314. timeout_fn function;
  1315. flags = raw_cmd->flags;
  1316. if (flags & (FD_RAW_READ | FD_RAW_WRITE))
  1317. flags |= FD_RAW_INTR;
  1318. if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
  1319. ready_date = DRS->spinup_date + DP->spinup;
  1320. /* If spinup will take a long time, rerun scandrives
  1321. * again just before spinup completion. Beware that
  1322. * after scandrives, we must again wait for selection.
  1323. */
  1324. if ((signed)(ready_date - jiffies) > DP->select_delay) {
  1325. ready_date -= DP->select_delay;
  1326. function = (timeout_fn) floppy_start;
  1327. } else
  1328. function = (timeout_fn) setup_rw_floppy;
  1329. /* wait until the floppy is spinning fast enough */
  1330. if (fd_wait_for_completion(ready_date, function))
  1331. return;
  1332. }
  1333. dflags = DRS->flags;
  1334. if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
  1335. setup_DMA();
  1336. if (flags & FD_RAW_INTR)
  1337. do_floppy = main_command_interrupt;
  1338. r = 0;
  1339. for (i = 0; i < raw_cmd->cmd_count; i++)
  1340. r |= output_byte(raw_cmd->cmd[i]);
  1341. debugt("rw_command: ");
  1342. if (r) {
  1343. cont->error();
  1344. reset_fdc();
  1345. return;
  1346. }
  1347. if (!(flags & FD_RAW_INTR)) {
  1348. inr = result();
  1349. cont->interrupt();
  1350. } else if (flags & FD_RAW_NEED_DISK)
  1351. fd_watchdog();
  1352. }
  1353. static int blind_seek;
  1354. /*
  1355. * This is the routine called after every seek (or recalibrate) interrupt
  1356. * from the floppy controller.
  1357. */
  1358. static void seek_interrupt(void)
  1359. {
  1360. debugt("seek interrupt:");
  1361. if (inr != 2 || (ST0 & 0xF8) != 0x20) {
  1362. DPRINT("seek failed\n");
  1363. DRS->track = NEED_2_RECAL;
  1364. cont->error();
  1365. cont->redo();
  1366. return;
  1367. }
  1368. if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
  1369. #ifdef DCL_DEBUG
  1370. if (DP->flags & FD_DEBUG) {
  1371. DPRINT
  1372. ("clearing NEWCHANGE flag because of effective seek\n");
  1373. DPRINT("jiffies=%lu\n", jiffies);
  1374. }
  1375. #endif
  1376. CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
  1377. DRS->select_date = jiffies;
  1378. }
  1379. DRS->track = ST1;
  1380. floppy_ready();
  1381. }
  1382. static void check_wp(void)
  1383. {
  1384. if (TESTF(FD_VERIFY)) {
  1385. /* check write protection */
  1386. output_byte(FD_GETSTATUS);
  1387. output_byte(UNIT(current_drive));
  1388. if (result() != 1) {
  1389. FDCS->reset = 1;
  1390. return;
  1391. }
  1392. CLEARF(FD_VERIFY);
  1393. CLEARF(FD_NEED_TWADDLE);
  1394. #ifdef DCL_DEBUG
  1395. if (DP->flags & FD_DEBUG) {
  1396. DPRINT("checking whether disk is write protected\n");
  1397. DPRINT("wp=%x\n", ST3 & 0x40);
  1398. }
  1399. #endif
  1400. if (!(ST3 & 0x40))
  1401. SETF(FD_DISK_WRITABLE);
  1402. else
  1403. CLEARF(FD_DISK_WRITABLE);
  1404. }
  1405. }
  1406. static void seek_floppy(void)
  1407. {
  1408. int track;
  1409. blind_seek = 0;
  1410. #ifdef DCL_DEBUG
  1411. if (DP->flags & FD_DEBUG) {
  1412. DPRINT("calling disk change from seek\n");
  1413. }
  1414. #endif
  1415. if (!TESTF(FD_DISK_NEWCHANGE) &&
  1416. disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
  1417. /* the media changed flag should be cleared after the seek.
  1418. * If it isn't, this means that there is really no disk in
  1419. * the drive.
  1420. */
  1421. SETF(FD_DISK_CHANGED);
  1422. cont->done(0);
  1423. cont->redo();
  1424. return;
  1425. }
  1426. if (DRS->track <= NEED_1_RECAL) {
  1427. recalibrate_floppy();
  1428. return;
  1429. } else if (TESTF(FD_DISK_NEWCHANGE) &&
  1430. (raw_cmd->flags & FD_RAW_NEED_DISK) &&
  1431. (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
  1432. /* we seek to clear the media-changed condition. Does anybody
  1433. * know a more elegant way, which works on all drives? */
  1434. if (raw_cmd->track)
  1435. track = raw_cmd->track - 1;
  1436. else {
  1437. if (DP->flags & FD_SILENT_DCL_CLEAR) {
  1438. set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
  1439. blind_seek = 1;
  1440. raw_cmd->flags |= FD_RAW_NEED_SEEK;
  1441. }
  1442. track = 1;
  1443. }
  1444. } else {
  1445. check_wp();
  1446. if (raw_cmd->track != DRS->track &&
  1447. (raw_cmd->flags & FD_RAW_NEED_SEEK))
  1448. track = raw_cmd->track;
  1449. else {
  1450. setup_rw_floppy();
  1451. return;
  1452. }
  1453. }
  1454. do_floppy = seek_interrupt;
  1455. output_byte(FD_SEEK);
  1456. output_byte(UNIT(current_drive));
  1457. LAST_OUT(track);
  1458. debugt("seek command:");
  1459. }
  1460. static void recal_interrupt(void)
  1461. {
  1462. debugt("recal interrupt:");
  1463. if (inr != 2)
  1464. FDCS->reset = 1;
  1465. else if (ST0 & ST0_ECE) {
  1466. switch (DRS->track) {
  1467. case NEED_1_RECAL:
  1468. debugt("recal interrupt need 1 recal:");
  1469. /* after a second recalibrate, we still haven't
  1470. * reached track 0. Probably no drive. Raise an
  1471. * error, as failing immediately might upset
  1472. * computers possessed by the Devil :-) */
  1473. cont->error();
  1474. cont->redo();
  1475. return;
  1476. case NEED_2_RECAL:
  1477. debugt("recal interrupt need 2 recal:");
  1478. /* If we already did a recalibrate,
  1479. * and we are not at track 0, this
  1480. * means we have moved. (The only way
  1481. * not to move at recalibration is to
  1482. * be already at track 0.) Clear the
  1483. * new change flag */
  1484. #ifdef DCL_DEBUG
  1485. if (DP->flags & FD_DEBUG) {
  1486. DPRINT
  1487. ("clearing NEWCHANGE flag because of second recalibrate\n");
  1488. }
  1489. #endif
  1490. CLEARF(FD_DISK_NEWCHANGE);
  1491. DRS->select_date = jiffies;
  1492. /* fall through */
  1493. default:
  1494. debugt("recal interrupt default:");
  1495. /* Recalibrate moves the head by at
  1496. * most 80 steps. If after one
  1497. * recalibrate we don't have reached
  1498. * track 0, this might mean that we
  1499. * started beyond track 80. Try
  1500. * again. */
  1501. DRS->track = NEED_1_RECAL;
  1502. break;
  1503. }
  1504. } else
  1505. DRS->track = ST1;
  1506. floppy_ready();
  1507. }
  1508. static void print_result(char *message, int inr)
  1509. {
  1510. int i;
  1511. DPRINT("%s ", message);
  1512. if (inr >= 0)
  1513. for (i = 0; i < inr; i++)
  1514. printk("repl[%d]=%x ", i, reply_buffer[i]);
  1515. printk("\n");
  1516. }
  1517. /* interrupt handler. Note that this can be called externally on the Sparc */
  1518. irqreturn_t floppy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1519. {
  1520. void (*handler) (void) = do_floppy;
  1521. int do_print;
  1522. unsigned long f;
  1523. lasthandler = handler;
  1524. interruptjiffies = jiffies;
  1525. f = claim_dma_lock();
  1526. fd_disable_dma();
  1527. release_dma_lock(f);
  1528. floppy_enable_hlt();
  1529. do_floppy = NULL;
  1530. if (fdc >= N_FDC || FDCS->address == -1) {
  1531. /* we don't even know which FDC is the culprit */
  1532. printk("DOR0=%x\n", fdc_state[0].dor);
  1533. printk("floppy interrupt on bizarre fdc %d\n", fdc);
  1534. printk("handler=%p\n", handler);
  1535. is_alive("bizarre fdc");
  1536. return IRQ_NONE;
  1537. }
  1538. FDCS->reset = 0;
  1539. /* We have to clear the reset flag here, because apparently on boxes
  1540. * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
  1541. * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
  1542. * emission of the SENSEI's.
  1543. * It is OK to emit floppy commands because we are in an interrupt
  1544. * handler here, and thus we have to fear no interference of other
  1545. * activity.
  1546. */
  1547. do_print = !handler && print_unex && !initialising;
  1548. inr = result();
  1549. if (do_print)
  1550. print_result("unexpected interrupt", inr);
  1551. if (inr == 0) {
  1552. int max_sensei = 4;
  1553. do {
  1554. output_byte(FD_SENSEI);
  1555. inr = result();
  1556. if (do_print)
  1557. print_result("sensei", inr);
  1558. max_sensei--;
  1559. } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2
  1560. && max_sensei);
  1561. }
  1562. if (!handler) {
  1563. FDCS->reset = 1;
  1564. return IRQ_NONE;
  1565. }
  1566. schedule_bh(handler);
  1567. is_alive("normal interrupt end");
  1568. /* FIXME! Was it really for us? */
  1569. return IRQ_HANDLED;
  1570. }
  1571. static void recalibrate_floppy(void)
  1572. {
  1573. debugt("recalibrate floppy:");
  1574. do_floppy = recal_interrupt;
  1575. output_byte(FD_RECALIBRATE);
  1576. LAST_OUT(UNIT(current_drive));
  1577. }
  1578. /*
  1579. * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
  1580. */
  1581. static void reset_interrupt(void)
  1582. {
  1583. debugt("reset interrupt:");
  1584. result(); /* get the status ready for set_fdc */
  1585. if (FDCS->reset) {
  1586. printk("reset set in interrupt, calling %p\n", cont->error);
  1587. cont->error(); /* a reset just after a reset. BAD! */
  1588. }
  1589. cont->redo();
  1590. }
  1591. /*
  1592. * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
  1593. * or by setting the self clearing bit 7 of STATUS (newer FDCs)
  1594. */
  1595. static void reset_fdc(void)
  1596. {
  1597. unsigned long flags;
  1598. do_floppy = reset_interrupt;
  1599. FDCS->reset = 0;
  1600. reset_fdc_info(0);
  1601. /* Pseudo-DMA may intercept 'reset finished' interrupt. */
  1602. /* Irrelevant for systems with true DMA (i386). */
  1603. flags = claim_dma_lock();
  1604. fd_disable_dma();
  1605. release_dma_lock(flags);
  1606. if (FDCS->version >= FDC_82072A)
  1607. fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
  1608. else {
  1609. fd_outb(FDCS->dor & ~0x04, FD_DOR);
  1610. udelay(FD_RESET_DELAY);
  1611. fd_outb(FDCS->dor, FD_DOR);
  1612. }
  1613. }
  1614. static void show_floppy(void)
  1615. {
  1616. int i;
  1617. printk("\n");
  1618. printk("floppy driver state\n");
  1619. printk("-------------------\n");
  1620. printk("now=%lu last interrupt=%lu diff=%lu last called handler=%p\n",
  1621. jiffies, interruptjiffies, jiffies - interruptjiffies,
  1622. lasthandler);
  1623. #ifdef FLOPPY_SANITY_CHECK
  1624. printk("timeout_message=%s\n", timeout_message);
  1625. printk("last output bytes:\n");
  1626. for (i = 0; i < OLOGSIZE; i++)
  1627. printk("%2x %2x %lu\n",
  1628. output_log[(i + output_log_pos) % OLOGSIZE].data,
  1629. output_log[(i + output_log_pos) % OLOGSIZE].status,
  1630. output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
  1631. printk("last result at %lu\n", resultjiffies);
  1632. printk("last redo_fd_request at %lu\n", lastredo);
  1633. for (i = 0; i < resultsize; i++) {
  1634. printk("%2x ", reply_buffer[i]);
  1635. }
  1636. printk("\n");
  1637. #endif
  1638. printk("status=%x\n", fd_inb(FD_STATUS));
  1639. printk("fdc_busy=%lu\n", fdc_busy);
  1640. if (do_floppy)
  1641. printk("do_floppy=%p\n", do_floppy);
  1642. if (floppy_work.pending)
  1643. printk("floppy_work.func=%p\n", floppy_work.func);
  1644. if (timer_pending(&fd_timer))
  1645. printk("fd_timer.function=%p\n", fd_timer.function);
  1646. if (timer_pending(&fd_timeout)) {
  1647. printk("timer_function=%p\n", fd_timeout.function);
  1648. printk("expires=%lu\n", fd_timeout.expires - jiffies);
  1649. printk("now=%lu\n", jiffies);
  1650. }
  1651. printk("cont=%p\n", cont);
  1652. printk("current_req=%p\n", current_req);
  1653. printk("command_status=%d\n", command_status);
  1654. printk("\n");
  1655. }
  1656. static void floppy_shutdown(unsigned long data)
  1657. {
  1658. unsigned long flags;
  1659. if (!initialising)
  1660. show_floppy();
  1661. cancel_activity();
  1662. floppy_enable_hlt();
  1663. flags = claim_dma_lock();
  1664. fd_disable_dma();
  1665. release_dma_lock(flags);
  1666. /* avoid dma going to a random drive after shutdown */
  1667. if (!initialising)
  1668. DPRINT("floppy timeout called\n");
  1669. FDCS->reset = 1;
  1670. if (cont) {
  1671. cont->done(0);
  1672. cont->redo(); /* this will recall reset when needed */
  1673. } else {
  1674. printk("no cont in shutdown!\n");
  1675. process_fd_request();
  1676. }
  1677. is_alive("floppy shutdown");
  1678. }
  1679. /*typedef void (*timeout_fn)(unsigned long);*/
  1680. /* start motor, check media-changed condition and write protection */
  1681. static int start_motor(void (*function) (void))
  1682. {
  1683. int mask, data;
  1684. mask = 0xfc;
  1685. data = UNIT(current_drive);
  1686. if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
  1687. if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
  1688. set_debugt();
  1689. /* no read since this drive is running */
  1690. DRS->first_read_date = 0;
  1691. /* note motor start time if motor is not yet running */
  1692. DRS->spinup_date = jiffies;
  1693. data |= (0x10 << UNIT(current_drive));
  1694. }
  1695. } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
  1696. mask &= ~(0x10 << UNIT(current_drive));
  1697. /* starts motor and selects floppy */
  1698. del_timer(motor_off_timer + current_drive);
  1699. set_dor(fdc, mask, data);
  1700. /* wait_for_completion also schedules reset if needed. */
  1701. return (fd_wait_for_completion(DRS->select_date + DP->select_delay,
  1702. (timeout_fn) function));
  1703. }
  1704. static void floppy_ready(void)
  1705. {
  1706. CHECK_RESET;
  1707. if (start_motor(floppy_ready))
  1708. return;
  1709. if (fdc_dtr())
  1710. return;
  1711. #ifdef DCL_DEBUG
  1712. if (DP->flags & FD_DEBUG) {
  1713. DPRINT("calling disk change from floppy_ready\n");
  1714. }
  1715. #endif
  1716. if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
  1717. disk_change(current_drive) && !DP->select_delay)
  1718. twaddle(); /* this clears the dcl on certain drive/controller
  1719. * combinations */
  1720. #ifdef fd_chose_dma_mode
  1721. if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
  1722. unsigned long flags = claim_dma_lock();
  1723. fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
  1724. release_dma_lock(flags);
  1725. }
  1726. #endif
  1727. if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
  1728. perpendicular_mode();
  1729. fdc_specify(); /* must be done here because of hut, hlt ... */
  1730. seek_floppy();
  1731. } else {
  1732. if ((raw_cmd->flags & FD_RAW_READ) ||
  1733. (raw_cmd->flags & FD_RAW_WRITE))
  1734. fdc_specify();
  1735. setup_rw_floppy();
  1736. }
  1737. }
  1738. static void floppy_start(void)
  1739. {
  1740. reschedule_timeout(current_reqD, "floppy start", 0);
  1741. scandrives();
  1742. #ifdef DCL_DEBUG
  1743. if (DP->flags & FD_DEBUG) {
  1744. DPRINT("setting NEWCHANGE in floppy_start\n");
  1745. }
  1746. #endif
  1747. SETF(FD_DISK_NEWCHANGE);
  1748. floppy_ready();
  1749. }
  1750. /*
  1751. * ========================================================================
  1752. * here ends the bottom half. Exported routines are:
  1753. * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
  1754. * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
  1755. * Initialization also uses output_byte, result, set_dor, floppy_interrupt
  1756. * and set_dor.
  1757. * ========================================================================
  1758. */
  1759. /*
  1760. * General purpose continuations.
  1761. * ==============================
  1762. */
  1763. static void do_wakeup(void)
  1764. {
  1765. reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
  1766. cont = NULL;
  1767. command_status += 2;
  1768. wake_up(&command_done);
  1769. }
  1770. static struct cont_t wakeup_cont = {
  1771. .interrupt = empty,
  1772. .redo = do_wakeup,
  1773. .error = empty,
  1774. .done = (done_f) empty
  1775. };
  1776. static struct cont_t intr_cont = {
  1777. .interrupt = empty,
  1778. .redo = process_fd_request,
  1779. .error = empty,
  1780. .done = (done_f) empty
  1781. };
  1782. static int wait_til_done(void (*handler) (void), int interruptible)
  1783. {
  1784. int ret;
  1785. schedule_bh(handler);
  1786. if (command_status < 2 && NO_SIGNAL) {
  1787. DECLARE_WAITQUEUE(wait, current);
  1788. add_wait_queue(&command_done, &wait);
  1789. for (;;) {
  1790. set_current_state(interruptible ?
  1791. TASK_INTERRUPTIBLE :
  1792. TASK_UNINTERRUPTIBLE);
  1793. if (command_status >= 2 || !NO_SIGNAL)
  1794. break;
  1795. is_alive("wait_til_done");
  1796. schedule();
  1797. }
  1798. set_current_state(TASK_RUNNING);
  1799. remove_wait_queue(&command_done, &wait);
  1800. }
  1801. if (command_status < 2) {
  1802. cancel_activity();
  1803. cont = &intr_cont;
  1804. reset_fdc();
  1805. return -EINTR;
  1806. }
  1807. if (FDCS->reset)
  1808. command_status = FD_COMMAND_ERROR;
  1809. if (command_status == FD_COMMAND_OKAY)
  1810. ret = 0;
  1811. else
  1812. ret = -EIO;
  1813. command_status = FD_COMMAND_NONE;
  1814. return ret;
  1815. }
  1816. static void generic_done(int result)
  1817. {
  1818. command_status = result;
  1819. cont = &wakeup_cont;
  1820. }
  1821. static void generic_success(void)
  1822. {
  1823. cont->done(1);
  1824. }
  1825. static void generic_failure(void)
  1826. {
  1827. cont->done(0);
  1828. }
  1829. static void success_and_wakeup(void)
  1830. {
  1831. generic_success();
  1832. cont->redo();
  1833. }
  1834. /*
  1835. * formatting and rw support.
  1836. * ==========================
  1837. */
  1838. static int next_valid_format(void)
  1839. {
  1840. int probed_format;
  1841. probed_format = DRS->probed_format;
  1842. while (1) {
  1843. if (probed_format >= 8 || !DP->autodetect[probed_format]) {
  1844. DRS->probed_format = 0;
  1845. return 1;
  1846. }
  1847. if (floppy_type[DP->autodetect[probed_format]].sect) {
  1848. DRS->probed_format = probed_format;
  1849. return 0;
  1850. }
  1851. probed_format++;
  1852. }
  1853. }
  1854. static void bad_flp_intr(void)
  1855. {
  1856. int err_count;
  1857. if (probing) {
  1858. DRS->probed_format++;
  1859. if (!next_valid_format())
  1860. return;
  1861. }
  1862. err_count = ++(*errors);
  1863. INFBOUND(DRWE->badness, err_count);
  1864. if (err_count > DP->max_errors.abort)
  1865. cont->done(0);
  1866. if (err_count > DP->max_errors.reset)
  1867. FDCS->reset = 1;
  1868. else if (err_count > DP->max_errors.recal)
  1869. DRS->track = NEED_2_RECAL;
  1870. }
  1871. static void set_floppy(int drive)
  1872. {
  1873. int type = ITYPE(UDRS->fd_device);
  1874. if (type)
  1875. _floppy = floppy_type + type;
  1876. else
  1877. _floppy = current_type[drive];
  1878. }
  1879. /*
  1880. * formatting support.
  1881. * ===================
  1882. */
  1883. static void format_interrupt(void)
  1884. {
  1885. switch (interpret_errors()) {
  1886. case 1:
  1887. cont->error();
  1888. case 2:
  1889. break;
  1890. case 0:
  1891. cont->done(1);
  1892. }
  1893. cont->redo();
  1894. }
  1895. #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
  1896. #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
  1897. #define CT(x) ((x) | 0xc0)
  1898. static void setup_format_params(int track)
  1899. {
  1900. struct fparm {
  1901. unsigned char track, head, sect, size;
  1902. } *here = (struct fparm *)floppy_track_buffer;
  1903. int il, n;
  1904. int count, head_shift, track_shift;
  1905. raw_cmd = &default_raw_cmd;
  1906. raw_cmd->track = track;
  1907. raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
  1908. FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
  1909. raw_cmd->rate = _floppy->rate & 0x43;
  1910. raw_cmd->cmd_count = NR_F;
  1911. COMMAND = FM_MODE(_floppy, FD_FORMAT);
  1912. DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
  1913. F_SIZECODE = FD_SIZECODE(_floppy);
  1914. F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
  1915. F_GAP = _floppy->fmt_gap;
  1916. F_FILL = FD_FILL_BYTE;
  1917. raw_cmd->kernel_data = floppy_track_buffer;
  1918. raw_cmd->length = 4 * F_SECT_PER_TRACK;
  1919. /* allow for about 30ms for data transport per track */
  1920. head_shift = (F_SECT_PER_TRACK + 5) / 6;
  1921. /* a ``cylinder'' is two tracks plus a little stepping time */
  1922. track_shift = 2 * head_shift + 3;
  1923. /* position of logical sector 1 on this track */
  1924. n = (track_shift * format_req.track + head_shift * format_req.head)
  1925. % F_SECT_PER_TRACK;
  1926. /* determine interleave */
  1927. il = 1;
  1928. if (_floppy->fmt_gap < 0x22)
  1929. il++;
  1930. /* initialize field */
  1931. for (count = 0; count < F_SECT_PER_TRACK; ++count) {
  1932. here[count].track = format_req.track;
  1933. here[count].head = format_req.head;
  1934. here[count].sect = 0;
  1935. here[count].size = F_SIZECODE;
  1936. }
  1937. /* place logical sectors */
  1938. for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
  1939. here[n].sect = count;
  1940. n = (n + il) % F_SECT_PER_TRACK;
  1941. if (here[n].sect) { /* sector busy, find next free sector */
  1942. ++n;
  1943. if (n >= F_SECT_PER_TRACK) {
  1944. n -= F_SECT_PER_TRACK;
  1945. while (here[n].sect)
  1946. ++n;
  1947. }
  1948. }
  1949. }
  1950. if (_floppy->stretch & FD_ZEROBASED) {
  1951. for (count = 0; count < F_SECT_PER_TRACK; count++)
  1952. here[count].sect--;
  1953. }
  1954. }
  1955. static void redo_format(void)
  1956. {
  1957. buffer_track = -1;
  1958. setup_format_params(format_req.track << STRETCH(_floppy));
  1959. floppy_start();
  1960. debugt("queue format request");
  1961. }
  1962. static struct cont_t format_cont = {
  1963. .interrupt = format_interrupt,
  1964. .redo = redo_format,
  1965. .error = bad_flp_intr,
  1966. .done = generic_done
  1967. };
  1968. static int do_format(int drive, struct format_descr *tmp_format_req)
  1969. {
  1970. int ret;
  1971. LOCK_FDC(drive, 1);
  1972. set_floppy(drive);
  1973. if (!_floppy ||
  1974. _floppy->track > DP->tracks ||
  1975. tmp_format_req->track >= _floppy->track ||
  1976. tmp_format_req->head >= _floppy->head ||
  1977. (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
  1978. !_floppy->fmt_gap) {
  1979. process_fd_request();
  1980. return -EINVAL;
  1981. }
  1982. format_req = *tmp_format_req;
  1983. format_errors = 0;
  1984. cont = &format_cont;
  1985. errors = &format_errors;
  1986. IWAIT(redo_format);
  1987. process_fd_request();
  1988. return ret;
  1989. }
  1990. /*
  1991. * Buffer read/write and support
  1992. * =============================
  1993. */
  1994. static void floppy_end_request(struct request *req, int uptodate)
  1995. {
  1996. unsigned int nr_sectors = current_count_sectors;
  1997. /* current_count_sectors can be zero if transfer failed */
  1998. if (!uptodate)
  1999. nr_sectors = req->current_nr_sectors;
  2000. if (end_that_request_first(req, uptodate, nr_sectors))
  2001. return;
  2002. add_disk_randomness(req->rq_disk);
  2003. floppy_off((long)req->rq_disk->private_data);
  2004. blkdev_dequeue_request(req);
  2005. end_that_request_last(req);
  2006. /* We're done with the request */
  2007. current_req = NULL;
  2008. }
  2009. /* new request_done. Can handle physical sectors which are smaller than a
  2010. * logical buffer */
  2011. static void request_done(int uptodate)
  2012. {
  2013. struct request_queue *q = floppy_queue;
  2014. struct request *req = current_req;
  2015. unsigned long flags;
  2016. int block;
  2017. probing = 0;
  2018. reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
  2019. if (!req) {
  2020. printk("floppy.c: no request in request_done\n");
  2021. return;
  2022. }
  2023. if (uptodate) {
  2024. /* maintain values for invalidation on geometry
  2025. * change */
  2026. block = current_count_sectors + req->sector;
  2027. INFBOUND(DRS->maxblock, block);
  2028. if (block > _floppy->sect)
  2029. DRS->maxtrack = 1;
  2030. /* unlock chained buffers */
  2031. spin_lock_irqsave(q->queue_lock, flags);
  2032. floppy_end_request(req, 1);
  2033. spin_unlock_irqrestore(q->queue_lock, flags);
  2034. } else {
  2035. if (rq_data_dir(req) == WRITE) {
  2036. /* record write error information */
  2037. DRWE->write_errors++;
  2038. if (DRWE->write_errors == 1) {
  2039. DRWE->first_error_sector = req->sector;
  2040. DRWE->first_error_generation = DRS->generation;
  2041. }
  2042. DRWE->last_error_sector = req->sector;
  2043. DRWE->last_error_generation = DRS->generation;
  2044. }
  2045. spin_lock_irqsave(q->queue_lock, flags);
  2046. floppy_end_request(req, 0);
  2047. spin_unlock_irqrestore(q->queue_lock, flags);
  2048. }
  2049. }
  2050. /* Interrupt handler evaluating the result of the r/w operation */
  2051. static void rw_interrupt(void)
  2052. {
  2053. int nr_sectors, ssize, eoc, heads;
  2054. if (R_HEAD >= 2) {
  2055. /* some Toshiba floppy controllers occasionnally seem to
  2056. * return bogus interrupts after read/write operations, which
  2057. * can be recognized by a bad head number (>= 2) */
  2058. return;
  2059. }
  2060. if (!DRS->first_read_date)
  2061. DRS->first_read_date = jiffies;
  2062. nr_sectors = 0;
  2063. CODE2SIZE;
  2064. if (ST1 & ST1_EOC)
  2065. eoc = 1;
  2066. else
  2067. eoc = 0;
  2068. if (COMMAND & 0x80)
  2069. heads = 2;
  2070. else
  2071. heads = 1;
  2072. nr_sectors = (((R_TRACK - TRACK) * heads +
  2073. R_HEAD - HEAD) * SECT_PER_TRACK +
  2074. R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
  2075. #ifdef FLOPPY_SANITY_CHECK
  2076. if (nr_sectors / ssize >
  2077. (in_sector_offset + current_count_sectors + ssize - 1) / ssize) {
  2078. DPRINT("long rw: %x instead of %lx\n",
  2079. nr_sectors, current_count_sectors);
  2080. printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
  2081. printk("rh=%d h=%d\n", R_HEAD, HEAD);
  2082. printk("rt=%d t=%d\n", R_TRACK, TRACK);
  2083. printk("heads=%d eoc=%d\n", heads, eoc);
  2084. printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
  2085. fsector_t, ssize);
  2086. printk("in_sector_offset=%d\n", in_sector_offset);
  2087. }
  2088. #endif
  2089. nr_sectors -= in_sector_offset;
  2090. INFBOUND(nr_sectors, 0);
  2091. SUPBOUND(current_count_sectors, nr_sectors);
  2092. switch (interpret_errors()) {
  2093. case 2:
  2094. cont->redo();
  2095. return;
  2096. case 1:
  2097. if (!current_count_sectors) {
  2098. cont->error();
  2099. cont->redo();
  2100. return;
  2101. }
  2102. break;
  2103. case 0:
  2104. if (!current_count_sectors) {
  2105. cont->redo();
  2106. return;
  2107. }
  2108. current_type[current_drive] = _floppy;
  2109. floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
  2110. break;
  2111. }
  2112. if (probing) {
  2113. if (DP->flags & FTD_MSG)
  2114. DPRINT("Auto-detected floppy type %s in fd%d\n",
  2115. _floppy->name, current_drive);
  2116. current_type[current_drive] = _floppy;
  2117. floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
  2118. probing = 0;
  2119. }
  2120. if (CT(COMMAND) != FD_READ ||
  2121. raw_cmd->kernel_data == current_req->buffer) {
  2122. /* transfer directly from buffer */
  2123. cont->done(1);
  2124. } else if (CT(COMMAND) == FD_READ) {
  2125. buffer_track = raw_cmd->track;
  2126. buffer_drive = current_drive;
  2127. INFBOUND(buffer_max, nr_sectors + fsector_t);
  2128. }
  2129. cont->redo();
  2130. }
  2131. /* Compute maximal contiguous buffer size. */
  2132. static int buffer_chain_size(void)
  2133. {
  2134. struct bio *bio;
  2135. struct bio_vec *bv;
  2136. int size, i;
  2137. char *base;
  2138. base = bio_data(current_req->bio);
  2139. size = 0;
  2140. rq_for_each_bio(bio, current_req) {
  2141. bio_for_each_segment(bv, bio, i) {
  2142. if (page_address(bv->bv_page) + bv->bv_offset !=
  2143. base + size)
  2144. break;
  2145. size += bv->bv_len;
  2146. }
  2147. }
  2148. return size >> 9;
  2149. }
  2150. /* Compute the maximal transfer size */
  2151. static int transfer_size(int ssize, int max_sector, int max_size)
  2152. {
  2153. SUPBOUND(max_sector, fsector_t + max_size);
  2154. /* alignment */
  2155. max_sector -= (max_sector % _floppy->sect) % ssize;
  2156. /* transfer size, beginning not aligned */
  2157. current_count_sectors = max_sector - fsector_t;
  2158. return max_sector;
  2159. }
  2160. /*
  2161. * Move data from/to the track buffer to/from the buffer cache.
  2162. */
  2163. static void copy_buffer(int ssize, int max_sector, int max_sector_2)
  2164. {
  2165. int remaining; /* number of transferred 512-byte sectors */
  2166. struct bio_vec *bv;
  2167. struct bio *bio;
  2168. char *buffer, *dma_buffer;
  2169. int size, i;
  2170. max_sector = transfer_size(ssize,
  2171. min(max_sector, max_sector_2),
  2172. current_req->nr_sectors);
  2173. if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
  2174. buffer_max > fsector_t + current_req->nr_sectors)
  2175. current_count_sectors = min_t(int, buffer_max - fsector_t,
  2176. current_req->nr_sectors);
  2177. remaining = current_count_sectors << 9;
  2178. #ifdef FLOPPY_SANITY_CHECK
  2179. if ((remaining >> 9) > current_req->nr_sectors &&
  2180. CT(COMMAND) == FD_WRITE) {
  2181. DPRINT("in copy buffer\n");
  2182. printk("current_count_sectors=%ld\n", current_count_sectors);
  2183. printk("remaining=%d\n", remaining >> 9);
  2184. printk("current_req->nr_sectors=%ld\n",
  2185. current_req->nr_sectors);
  2186. printk("current_req->current_nr_sectors=%u\n",
  2187. current_req->current_nr_sectors);
  2188. printk("max_sector=%d\n", max_sector);
  2189. printk("ssize=%d\n", ssize);
  2190. }
  2191. #endif
  2192. buffer_max = max(max_sector, buffer_max);
  2193. dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
  2194. size = current_req->current_nr_sectors << 9;
  2195. rq_for_each_bio(bio, current_req) {
  2196. bio_for_each_segment(bv, bio, i) {
  2197. if (!remaining)
  2198. break;
  2199. size = bv->bv_len;
  2200. SUPBOUND(size, remaining);
  2201. buffer = page_address(bv->bv_page) + bv->bv_offset;
  2202. #ifdef FLOPPY_SANITY_CHECK
  2203. if (dma_buffer + size >
  2204. floppy_track_buffer + (max_buffer_sectors << 10) ||
  2205. dma_buffer < floppy_track_buffer) {
  2206. DPRINT("buffer overrun in copy buffer %d\n",
  2207. (int)((floppy_track_buffer -
  2208. dma_buffer) >> 9));
  2209. printk("fsector_t=%d buffer_min=%d\n",
  2210. fsector_t, buffer_min);
  2211. printk("current_count_sectors=%ld\n",
  2212. current_count_sectors);
  2213. if (CT(COMMAND) == FD_READ)
  2214. printk("read\n");
  2215. if (CT(COMMAND) == FD_WRITE)
  2216. printk("write\n");
  2217. break;
  2218. }
  2219. if (((unsigned long)buffer) % 512)
  2220. DPRINT("%p buffer not aligned\n", buffer);
  2221. #endif
  2222. if (CT(COMMAND) == FD_READ)
  2223. memcpy(buffer, dma_buffer, size);
  2224. else
  2225. memcpy(dma_buffer, buffer, size);
  2226. remaining -= size;
  2227. dma_buffer += size;
  2228. }
  2229. }
  2230. #ifdef FLOPPY_SANITY_CHECK
  2231. if (remaining) {
  2232. if (remaining > 0)
  2233. max_sector -= remaining >> 9;
  2234. DPRINT("weirdness: remaining %d\n", remaining >> 9);
  2235. }
  2236. #endif
  2237. }
  2238. #if 0
  2239. static inline int check_dma_crossing(char *start,
  2240. unsigned long length, char *message)
  2241. {
  2242. if (CROSS_64KB(start, length)) {
  2243. printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
  2244. message, start, start + length);
  2245. return 1;
  2246. } else
  2247. return 0;
  2248. }
  2249. #endif
  2250. /* work around a bug in pseudo DMA
  2251. * (on some FDCs) pseudo DMA does not stop when the CPU stops
  2252. * sending data. Hence we need a different way to signal the
  2253. * transfer length: We use SECT_PER_TRACK. Unfortunately, this
  2254. * does not work with MT, hence we can only transfer one head at
  2255. * a time
  2256. */
  2257. static void virtualdmabug_workaround(void)
  2258. {
  2259. int hard_sectors, end_sector;
  2260. if (CT(COMMAND) == FD_WRITE) {
  2261. COMMAND &= ~0x80; /* switch off multiple track mode */
  2262. hard_sectors = raw_cmd->length >> (7 + SIZECODE);
  2263. end_sector = SECTOR + hard_sectors - 1;
  2264. #ifdef FLOPPY_SANITY_CHECK
  2265. if (end_sector > SECT_PER_TRACK) {
  2266. printk("too many sectors %d > %d\n",
  2267. end_sector, SECT_PER_TRACK);
  2268. return;
  2269. }
  2270. #endif
  2271. SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
  2272. * to end of transfer */
  2273. }
  2274. }
  2275. /*
  2276. * Formulate a read/write request.
  2277. * this routine decides where to load the data (directly to buffer, or to
  2278. * tmp floppy area), how much data to load (the size of the buffer, the whole
  2279. * track, or a single sector)
  2280. * All floppy_track_buffer handling goes in here. If we ever add track buffer
  2281. * allocation on the fly, it should be done here. No other part should need
  2282. * modification.
  2283. */
  2284. static int make_raw_rw_request(void)
  2285. {
  2286. int aligned_sector_t;
  2287. int max_sector, max_size, tracksize, ssize;
  2288. if (max_buffer_sectors == 0) {
  2289. printk("VFS: Block I/O scheduled on unopened device\n");
  2290. return 0;
  2291. }
  2292. set_fdc((long)current_req->rq_disk->private_data);
  2293. raw_cmd = &default_raw_cmd;
  2294. raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
  2295. FD_RAW_NEED_SEEK;
  2296. raw_cmd->cmd_count = NR_RW;
  2297. if (rq_data_dir(current_req) == READ) {
  2298. raw_cmd->flags |= FD_RAW_READ;
  2299. COMMAND = FM_MODE(_floppy, FD_READ);
  2300. } else if (rq_data_dir(current_req) == WRITE) {
  2301. raw_cmd->flags |= FD_RAW_WRITE;
  2302. COMMAND = FM_MODE(_floppy, FD_WRITE);
  2303. } else {
  2304. DPRINT("make_raw_rw_request: unknown command\n");
  2305. return 0;
  2306. }
  2307. max_sector = _floppy->sect * _floppy->head;
  2308. TRACK = (int)current_req->sector / max_sector;
  2309. fsector_t = (int)current_req->sector % max_sector;
  2310. if (_floppy->track && TRACK >= _floppy->track) {
  2311. if (current_req->current_nr_sectors & 1) {
  2312. current_count_sectors = 1;
  2313. return 1;
  2314. } else
  2315. return 0;
  2316. }
  2317. HEAD = fsector_t / _floppy->sect;
  2318. if (((_floppy->stretch & (FD_SWAPSIDES | FD_ZEROBASED)) ||
  2319. TESTF(FD_NEED_TWADDLE)) && fsector_t < _floppy->sect)
  2320. max_sector = _floppy->sect;
  2321. /* 2M disks have phantom sectors on the first track */
  2322. if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
  2323. max_sector = 2 * _floppy->sect / 3;
  2324. if (fsector_t >= max_sector) {
  2325. current_count_sectors =
  2326. min_t(int, _floppy->sect - fsector_t,
  2327. current_req->nr_sectors);
  2328. return 1;
  2329. }
  2330. SIZECODE = 2;
  2331. } else
  2332. SIZECODE = FD_SIZECODE(_floppy);
  2333. raw_cmd->rate = _floppy->rate & 0x43;
  2334. if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
  2335. raw_cmd->rate = 1;
  2336. if (SIZECODE)
  2337. SIZECODE2 = 0xff;
  2338. else
  2339. SIZECODE2 = 0x80;
  2340. raw_cmd->track = TRACK << STRETCH(_floppy);
  2341. DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
  2342. GAP = _floppy->gap;
  2343. CODE2SIZE;
  2344. SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
  2345. SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
  2346. ((_floppy->stretch & FD_ZEROBASED) ? 0 : 1);
  2347. /* tracksize describes the size which can be filled up with sectors
  2348. * of size ssize.
  2349. */
  2350. tracksize = _floppy->sect - _floppy->sect % ssize;
  2351. if (tracksize < _floppy->sect) {
  2352. SECT_PER_TRACK++;
  2353. if (tracksize <= fsector_t % _floppy->sect)
  2354. SECTOR--;
  2355. /* if we are beyond tracksize, fill up using smaller sectors */
  2356. while (tracksize <= fsector_t % _floppy->sect) {
  2357. while (tracksize + ssize > _floppy->sect) {
  2358. SIZECODE--;
  2359. ssize >>= 1;
  2360. }
  2361. SECTOR++;
  2362. SECT_PER_TRACK++;
  2363. tracksize += ssize;
  2364. }
  2365. max_sector = HEAD * _floppy->sect + tracksize;
  2366. } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
  2367. max_sector = _floppy->sect;
  2368. } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
  2369. /* for virtual DMA bug workaround */
  2370. max_sector = _floppy->sect;
  2371. }
  2372. in_sector_offset = (fsector_t % _floppy->sect) % ssize;
  2373. aligned_sector_t = fsector_t - in_sector_offset;
  2374. max_size = current_req->nr_sectors;
  2375. if ((raw_cmd->track == buffer_track) &&
  2376. (current_drive == buffer_drive) &&
  2377. (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
  2378. /* data already in track buffer */
  2379. if (CT(COMMAND) == FD_READ) {
  2380. copy_buffer(1, max_sector, buffer_max);
  2381. return 1;
  2382. }
  2383. } else if (in_sector_offset || current_req->nr_sectors < ssize) {
  2384. if (CT(COMMAND) == FD_WRITE) {
  2385. if (fsector_t + current_req->nr_sectors > ssize &&
  2386. fsector_t + current_req->nr_sectors < ssize + ssize)
  2387. max_size = ssize + ssize;
  2388. else
  2389. max_size = ssize;
  2390. }
  2391. raw_cmd->flags &= ~FD_RAW_WRITE;
  2392. raw_cmd->flags |= FD_RAW_READ;
  2393. COMMAND = FM_MODE(_floppy, FD_READ);
  2394. } else if ((unsigned long)current_req->buffer < MAX_DMA_ADDRESS) {
  2395. unsigned long dma_limit;
  2396. int direct, indirect;
  2397. indirect =
  2398. transfer_size(ssize, max_sector,
  2399. max_buffer_sectors * 2) - fsector_t;
  2400. /*
  2401. * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
  2402. * on a 64 bit machine!
  2403. */
  2404. max_size = buffer_chain_size();
  2405. dma_limit =
  2406. (MAX_DMA_ADDRESS -
  2407. ((unsigned long)current_req->buffer)) >> 9;
  2408. if ((unsigned long)max_size > dma_limit) {
  2409. max_size = dma_limit;
  2410. }
  2411. /* 64 kb boundaries */
  2412. if (CROSS_64KB(current_req->buffer, max_size << 9))
  2413. max_size = (K_64 -
  2414. ((unsigned long)current_req->buffer) %
  2415. K_64) >> 9;
  2416. direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
  2417. /*
  2418. * We try to read tracks, but if we get too many errors, we
  2419. * go back to reading just one sector at a time.
  2420. *
  2421. * This means we should be able to read a sector even if there
  2422. * are other bad sectors on this track.
  2423. */
  2424. if (!direct ||
  2425. (indirect * 2 > direct * 3 &&
  2426. *errors < DP->max_errors.read_track &&
  2427. /*!TESTF(FD_NEED_TWADDLE) && */
  2428. ((!probing
  2429. || (DP->read_track & (1 << DRS->probed_format)))))) {
  2430. max_size = current_req->nr_sectors;
  2431. } else {
  2432. raw_cmd->kernel_data = current_req->buffer;
  2433. raw_cmd->length = current_count_sectors << 9;
  2434. if (raw_cmd->length == 0) {
  2435. DPRINT
  2436. ("zero dma transfer attempted from make_raw_request\n");
  2437. DPRINT("indirect=%d direct=%d fsector_t=%d",
  2438. indirect, direct, fsector_t);
  2439. return 0;
  2440. }
  2441. /* check_dma_crossing(raw_cmd->kernel_data,
  2442. raw_cmd->length,
  2443. "end of make_raw_request [1]");*/
  2444. virtualdmabug_workaround();
  2445. return 2;
  2446. }
  2447. }
  2448. if (CT(COMMAND) == FD_READ)
  2449. max_size = max_sector; /* unbounded */
  2450. /* claim buffer track if needed */
  2451. if (buffer_track != raw_cmd->track || /* bad track */
  2452. buffer_drive != current_drive || /* bad drive */
  2453. fsector_t > buffer_max ||
  2454. fsector_t < buffer_min ||
  2455. ((CT(COMMAND) == FD_READ ||
  2456. (!in_sector_offset && current_req->nr_sectors >= ssize)) &&
  2457. max_sector > 2 * max_buffer_sectors + buffer_min &&
  2458. max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)
  2459. /* not enough space */
  2460. ) {
  2461. buffer_track = -1;
  2462. buffer_drive = current_drive;
  2463. buffer_max = buffer_min = aligned_sector_t;
  2464. }
  2465. raw_cmd->kernel_data = floppy_track_buffer +
  2466. ((aligned_sector_t - buffer_min) << 9);
  2467. if (CT(COMMAND) == FD_WRITE) {
  2468. /* copy write buffer to track buffer.
  2469. * if we get here, we know that the write
  2470. * is either aligned or the data already in the buffer
  2471. * (buffer will be overwritten) */
  2472. #ifdef FLOPPY_SANITY_CHECK
  2473. if (in_sector_offset && buffer_track == -1)
  2474. DPRINT("internal error offset !=0 on write\n");
  2475. #endif
  2476. buffer_track = raw_cmd->track;
  2477. buffer_drive = current_drive;
  2478. copy_buffer(ssize, max_sector,
  2479. 2 * max_buffer_sectors + buffer_min);
  2480. } else
  2481. transfer_size(ssize, max_sector,
  2482. 2 * max_buffer_sectors + buffer_min -
  2483. aligned_sector_t);
  2484. /* round up current_count_sectors to get dma xfer size */
  2485. raw_cmd->length = in_sector_offset + current_count_sectors;
  2486. raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
  2487. raw_cmd->length <<= 9;
  2488. #ifdef FLOPPY_SANITY_CHECK
  2489. /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
  2490. "end of make_raw_request"); */
  2491. if ((raw_cmd->length < current_count_sectors << 9) ||
  2492. (raw_cmd->kernel_data != current_req->buffer &&
  2493. CT(COMMAND) == FD_WRITE &&
  2494. (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
  2495. aligned_sector_t < buffer_min)) ||
  2496. raw_cmd->length % (128 << SIZECODE) ||
  2497. raw_cmd->length <= 0 || current_count_sectors <= 0) {
  2498. DPRINT("fractionary current count b=%lx s=%lx\n",
  2499. raw_cmd->length, current_count_sectors);
  2500. if (raw_cmd->kernel_data != current_req->buffer)
  2501. printk("addr=%d, length=%ld\n",
  2502. (int)((raw_cmd->kernel_data -
  2503. floppy_track_buffer) >> 9),
  2504. current_count_sectors);
  2505. printk("st=%d ast=%d mse=%d msi=%d\n",
  2506. fsector_t, aligned_sector_t, max_sector, max_size);
  2507. printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
  2508. printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
  2509. COMMAND, SECTOR, HEAD, TRACK);
  2510. printk("buffer drive=%d\n", buffer_drive);
  2511. printk("buffer track=%d\n", buffer_track);
  2512. printk("buffer_min=%d\n", buffer_min);
  2513. printk("buffer_max=%d\n", buffer_max);
  2514. return 0;
  2515. }
  2516. if (raw_cmd->kernel_data != current_req->buffer) {
  2517. if (raw_cmd->kernel_data < floppy_track_buffer ||
  2518. current_count_sectors < 0 ||
  2519. raw_cmd->length < 0 ||
  2520. raw_cmd->kernel_data + raw_cmd->length >
  2521. floppy_track_buffer + (max_buffer_sectors << 10)) {
  2522. DPRINT("buffer overrun in schedule dma\n");
  2523. printk("fsector_t=%d buffer_min=%d current_count=%ld\n",
  2524. fsector_t, buffer_min, raw_cmd->length >> 9);
  2525. printk("current_count_sectors=%ld\n",
  2526. current_count_sectors);
  2527. if (CT(COMMAND) == FD_READ)
  2528. printk("read\n");
  2529. if (CT(COMMAND) == FD_WRITE)
  2530. printk("write\n");
  2531. return 0;
  2532. }
  2533. } else if (raw_cmd->length > current_req->nr_sectors << 9 ||
  2534. current_count_sectors > current_req->nr_sectors) {
  2535. DPRINT("buffer overrun in direct transfer\n");
  2536. return 0;
  2537. } else if (raw_cmd->length < current_count_sectors << 9) {
  2538. DPRINT("more sectors than bytes\n");
  2539. printk("bytes=%ld\n", raw_cmd->length >> 9);
  2540. printk("sectors=%ld\n", current_count_sectors);
  2541. }
  2542. if (raw_cmd->length == 0) {
  2543. DPRINT("zero dma transfer attempted from make_raw_request\n");
  2544. return 0;
  2545. }
  2546. #endif
  2547. virtualdmabug_workaround();
  2548. return 2;
  2549. }
  2550. static void redo_fd_request(void)
  2551. {
  2552. #define REPEAT {request_done(0); continue; }
  2553. int drive;
  2554. int tmp;
  2555. lastredo = jiffies;
  2556. if (current_drive < N_DRIVE)
  2557. floppy_off(current_drive);
  2558. for (;;) {
  2559. if (!current_req) {
  2560. struct request *req;
  2561. spin_lock_irq(floppy_queue->queue_lock);
  2562. req = elv_next_request(floppy_queue);
  2563. spin_unlock_irq(floppy_queue->queue_lock);
  2564. if (!req) {
  2565. do_floppy = NULL;
  2566. unlock_fdc();
  2567. return;
  2568. }
  2569. current_req = req;
  2570. }
  2571. drive = (long)current_req->rq_disk->private_data;
  2572. set_fdc(drive);
  2573. reschedule_timeout(current_reqD, "redo fd request", 0);
  2574. set_floppy(drive);
  2575. raw_cmd = &default_raw_cmd;
  2576. raw_cmd->flags = 0;
  2577. if (start_motor(redo_fd_request))
  2578. return;
  2579. disk_change(current_drive);
  2580. if (test_bit(current_drive, &fake_change) ||
  2581. TESTF(FD_DISK_CHANGED)) {
  2582. DPRINT("disk absent or changed during operation\n");
  2583. REPEAT;
  2584. }
  2585. if (!_floppy) { /* Autodetection */
  2586. if (!probing) {
  2587. DRS->probed_format = 0;
  2588. if (next_valid_format()) {
  2589. DPRINT("no autodetectable formats\n");
  2590. _floppy = NULL;
  2591. REPEAT;
  2592. }
  2593. }
  2594. probing = 1;
  2595. _floppy =
  2596. floppy_type + DP->autodetect[DRS->probed_format];
  2597. } else
  2598. probing = 0;
  2599. errors = &(current_req->errors);
  2600. tmp = make_raw_rw_request();
  2601. if (tmp < 2) {
  2602. request_done(tmp);
  2603. continue;
  2604. }
  2605. if (TESTF(FD_NEED_TWADDLE))
  2606. twaddle();
  2607. schedule_bh(floppy_start);
  2608. debugt("queue fd request");
  2609. return;
  2610. }
  2611. #undef REPEAT
  2612. }
  2613. static struct cont_t rw_cont = {
  2614. .interrupt = rw_interrupt,
  2615. .redo = redo_fd_request,
  2616. .error = bad_flp_intr,
  2617. .done = request_done
  2618. };
  2619. static void process_fd_request(void)
  2620. {
  2621. cont = &rw_cont;
  2622. schedule_bh(redo_fd_request);
  2623. }
  2624. static void do_fd_request(request_queue_t * q)
  2625. {
  2626. if (max_buffer_sectors == 0) {
  2627. printk("VFS: do_fd_request called on non-open device\n");
  2628. return;
  2629. }
  2630. if (usage_count == 0) {
  2631. printk("warning: usage count=0, current_req=%p exiting\n",
  2632. current_req);
  2633. printk("sect=%ld flags=%lx\n", (long)current_req->sector,
  2634. current_req->flags);
  2635. return;
  2636. }
  2637. if (test_bit(0, &fdc_busy)) {
  2638. /* fdc busy, this new request will be treated when the
  2639. current one is done */
  2640. is_alive("do fd request, old request running");
  2641. return;
  2642. }
  2643. lock_fdc(MAXTIMEOUT, 0);
  2644. process_fd_request();
  2645. is_alive("do fd request");
  2646. }
  2647. static struct cont_t poll_cont = {
  2648. .interrupt = success_and_wakeup,
  2649. .redo = floppy_ready,
  2650. .error = generic_failure,
  2651. .done = generic_done
  2652. };
  2653. static int poll_drive(int interruptible, int flag)
  2654. {
  2655. int ret;
  2656. /* no auto-sense, just clear dcl */
  2657. raw_cmd = &default_raw_cmd;
  2658. raw_cmd->flags = flag;
  2659. raw_cmd->track = 0;
  2660. raw_cmd->cmd_count = 0;
  2661. cont = &poll_cont;
  2662. #ifdef DCL_DEBUG
  2663. if (DP->flags & FD_DEBUG) {
  2664. DPRINT("setting NEWCHANGE in poll_drive\n");
  2665. }
  2666. #endif
  2667. SETF(FD_DISK_NEWCHANGE);
  2668. WAIT(floppy_ready);
  2669. return ret;
  2670. }
  2671. /*
  2672. * User triggered reset
  2673. * ====================
  2674. */
  2675. static void reset_intr(void)
  2676. {
  2677. printk("weird, reset interrupt called\n");
  2678. }
  2679. static struct cont_t reset_cont = {
  2680. .interrupt = reset_intr,
  2681. .redo = success_and_wakeup,
  2682. .error = generic_failure,
  2683. .done = generic_done
  2684. };
  2685. static int user_reset_fdc(int drive, int arg, int interruptible)
  2686. {
  2687. int ret;
  2688. ret = 0;
  2689. LOCK_FDC(drive, interruptible);
  2690. if (arg == FD_RESET_ALWAYS)
  2691. FDCS->reset = 1;
  2692. if (FDCS->reset) {
  2693. cont = &reset_cont;
  2694. WAIT(reset_fdc);
  2695. }
  2696. process_fd_request();
  2697. return ret;
  2698. }
  2699. /*
  2700. * Misc Ioctl's and support
  2701. * ========================
  2702. */
  2703. static inline int fd_copyout(void __user *param, const void *address,
  2704. unsigned long size)
  2705. {
  2706. return copy_to_user(param, address, size) ? -EFAULT : 0;
  2707. }
  2708. static inline int fd_copyin(void __user *param, void *address, unsigned long size)
  2709. {
  2710. return copy_from_user(address, param, size) ? -EFAULT : 0;
  2711. }
  2712. #define _COPYOUT(x) (copy_to_user((void __user *)param, &(x), sizeof(x)) ? -EFAULT : 0)
  2713. #define _COPYIN(x) (copy_from_user(&(x), (void __user *)param, sizeof(x)) ? -EFAULT : 0)
  2714. #define COPYOUT(x) ECALL(_COPYOUT(x))
  2715. #define COPYIN(x) ECALL(_COPYIN(x))
  2716. static inline const char *drive_name(int type, int drive)
  2717. {
  2718. struct floppy_struct *floppy;
  2719. if (type)
  2720. floppy = floppy_type + type;
  2721. else {
  2722. if (UDP->native_format)
  2723. floppy = floppy_type + UDP->native_format;
  2724. else
  2725. return "(null)";
  2726. }
  2727. if (floppy->name)
  2728. return floppy->name;
  2729. else
  2730. return "(null)";
  2731. }
  2732. /* raw commands */
  2733. static void raw_cmd_done(int flag)
  2734. {
  2735. int i;
  2736. if (!flag) {
  2737. raw_cmd->flags |= FD_RAW_FAILURE;
  2738. raw_cmd->flags |= FD_RAW_HARDFAILURE;
  2739. } else {
  2740. raw_cmd->reply_count = inr;
  2741. if (raw_cmd->reply_count > MAX_REPLIES)
  2742. raw_cmd->reply_count = 0;
  2743. for (i = 0; i < raw_cmd->reply_count; i++)
  2744. raw_cmd->reply[i] = reply_buffer[i];
  2745. if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
  2746. unsigned long flags;
  2747. flags = claim_dma_lock();
  2748. raw_cmd->length = fd_get_dma_residue();
  2749. release_dma_lock(flags);
  2750. }
  2751. if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
  2752. (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
  2753. raw_cmd->flags |= FD_RAW_FAILURE;
  2754. if (disk_change(current_drive))
  2755. raw_cmd->flags |= FD_RAW_DISK_CHANGE;
  2756. else
  2757. raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
  2758. if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
  2759. motor_off_callback(current_drive);
  2760. if (raw_cmd->next &&
  2761. (!(raw_cmd->flags & FD_RAW_FAILURE) ||
  2762. !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
  2763. ((raw_cmd->flags & FD_RAW_FAILURE) ||
  2764. !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
  2765. raw_cmd = raw_cmd->next;
  2766. return;
  2767. }
  2768. }
  2769. generic_done(flag);
  2770. }
  2771. static struct cont_t raw_cmd_cont = {
  2772. .interrupt = success_and_wakeup,
  2773. .redo = floppy_start,
  2774. .error = generic_failure,
  2775. .done = raw_cmd_done
  2776. };
  2777. static inline int raw_cmd_copyout(int cmd, char __user *param,
  2778. struct floppy_raw_cmd *ptr)
  2779. {
  2780. int ret;
  2781. while (ptr) {
  2782. COPYOUT(*ptr);
  2783. param += sizeof(struct floppy_raw_cmd);
  2784. if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
  2785. if (ptr->length >= 0
  2786. && ptr->length <= ptr->buffer_length)
  2787. ECALL(fd_copyout
  2788. (ptr->data, ptr->kernel_data,
  2789. ptr->buffer_length - ptr->length));
  2790. }
  2791. ptr = ptr->next;
  2792. }
  2793. return 0;
  2794. }
  2795. static void raw_cmd_free(struct floppy_raw_cmd **ptr)
  2796. {
  2797. struct floppy_raw_cmd *next, *this;
  2798. this = *ptr;
  2799. *ptr = NULL;
  2800. while (this) {
  2801. if (this->buffer_length) {
  2802. fd_dma_mem_free((unsigned long)this->kernel_data,
  2803. this->buffer_length);
  2804. this->buffer_length = 0;
  2805. }
  2806. next = this->next;
  2807. kfree(this);
  2808. this = next;
  2809. }
  2810. }
  2811. static inline int raw_cmd_copyin(int cmd, char __user *param,
  2812. struct floppy_raw_cmd **rcmd)
  2813. {
  2814. struct floppy_raw_cmd *ptr;
  2815. int ret;
  2816. int i;
  2817. *rcmd = NULL;
  2818. while (1) {
  2819. ptr = (struct floppy_raw_cmd *)
  2820. kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
  2821. if (!ptr)
  2822. return -ENOMEM;
  2823. *rcmd = ptr;
  2824. COPYIN(*ptr);
  2825. ptr->next = NULL;
  2826. ptr->buffer_length = 0;
  2827. param += sizeof(struct floppy_raw_cmd);
  2828. if (ptr->cmd_count > 33)
  2829. /* the command may now also take up the space
  2830. * initially intended for the reply & the
  2831. * reply count. Needed for long 82078 commands
  2832. * such as RESTORE, which takes ... 17 command
  2833. * bytes. Murphy's law #137: When you reserve
  2834. * 16 bytes for a structure, you'll one day
  2835. * discover that you really need 17...
  2836. */
  2837. return -EINVAL;
  2838. for (i = 0; i < 16; i++)
  2839. ptr->reply[i] = 0;
  2840. ptr->resultcode = 0;
  2841. ptr->kernel_data = NULL;
  2842. if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
  2843. if (ptr->length <= 0)
  2844. return -EINVAL;
  2845. ptr->kernel_data =
  2846. (char *)fd_dma_mem_alloc(ptr->length);
  2847. fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
  2848. if (!ptr->kernel_data)
  2849. return -ENOMEM;
  2850. ptr->buffer_length = ptr->length;
  2851. }
  2852. if (ptr->flags & FD_RAW_WRITE)
  2853. ECALL(fd_copyin(ptr->data, ptr->kernel_data,
  2854. ptr->length));
  2855. rcmd = &(ptr->next);
  2856. if (!(ptr->flags & FD_RAW_MORE))
  2857. return 0;
  2858. ptr->rate &= 0x43;
  2859. }
  2860. }
  2861. static int raw_cmd_ioctl(int cmd, void __user *param)
  2862. {
  2863. int drive, ret, ret2;
  2864. struct floppy_raw_cmd *my_raw_cmd;
  2865. if (FDCS->rawcmd <= 1)
  2866. FDCS->rawcmd = 1;
  2867. for (drive = 0; drive < N_DRIVE; drive++) {
  2868. if (FDC(drive) != fdc)
  2869. continue;
  2870. if (drive == current_drive) {
  2871. if (UDRS->fd_ref > 1) {
  2872. FDCS->rawcmd = 2;
  2873. break;
  2874. }
  2875. } else if (UDRS->fd_ref) {
  2876. FDCS->rawcmd = 2;
  2877. break;
  2878. }
  2879. }
  2880. if (FDCS->reset)
  2881. return -EIO;
  2882. ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
  2883. if (ret) {
  2884. raw_cmd_free(&my_raw_cmd);
  2885. return ret;
  2886. }
  2887. raw_cmd = my_raw_cmd;
  2888. cont = &raw_cmd_cont;
  2889. ret = wait_til_done(floppy_start, 1);
  2890. #ifdef DCL_DEBUG
  2891. if (DP->flags & FD_DEBUG) {
  2892. DPRINT("calling disk change from raw_cmd ioctl\n");
  2893. }
  2894. #endif
  2895. if (ret != -EINTR && FDCS->reset)
  2896. ret = -EIO;
  2897. DRS->track = NO_TRACK;
  2898. ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
  2899. if (!ret)
  2900. ret = ret2;
  2901. raw_cmd_free(&my_raw_cmd);
  2902. return ret;
  2903. }
  2904. static int invalidate_drive(struct block_device *bdev)
  2905. {
  2906. /* invalidate the buffer track to force a reread */
  2907. set_bit((long)bdev->bd_disk->private_data, &fake_change);
  2908. process_fd_request();
  2909. check_disk_change(bdev);
  2910. return 0;
  2911. }
  2912. static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
  2913. int drive, int type, struct block_device *bdev)
  2914. {
  2915. int cnt;
  2916. /* sanity checking for parameters. */
  2917. if (g->sect <= 0 ||
  2918. g->head <= 0 ||
  2919. g->track <= 0 || g->track > UDP->tracks >> STRETCH(g) ||
  2920. /* check if reserved bits are set */
  2921. (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_ZEROBASED)) != 0)
  2922. return -EINVAL;
  2923. if (type) {
  2924. if (!capable(CAP_SYS_ADMIN))
  2925. return -EPERM;
  2926. down(&open_lock);
  2927. LOCK_FDC(drive, 1);
  2928. floppy_type[type] = *g;
  2929. floppy_type[type].name = "user format";
  2930. for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
  2931. floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
  2932. floppy_type[type].size + 1;
  2933. process_fd_request();
  2934. for (cnt = 0; cnt < N_DRIVE; cnt++) {
  2935. struct block_device *bdev = opened_bdev[cnt];
  2936. if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
  2937. continue;
  2938. __invalidate_device(bdev, 0);
  2939. }
  2940. up(&open_lock);
  2941. } else {
  2942. int oldStretch;
  2943. LOCK_FDC(drive, 1);
  2944. if (cmd != FDDEFPRM)
  2945. /* notice a disk change immediately, else
  2946. * we lose our settings immediately*/
  2947. CALL(poll_drive(1, FD_RAW_NEED_DISK));
  2948. oldStretch = g->stretch;
  2949. user_params[drive] = *g;
  2950. if (buffer_drive == drive)
  2951. SUPBOUND(buffer_max, user_params[drive].sect);
  2952. current_type[drive] = &user_params[drive];
  2953. floppy_sizes[drive] = user_params[drive].size;
  2954. if (cmd == FDDEFPRM)
  2955. DRS->keep_data = -1;
  2956. else
  2957. DRS->keep_data = 1;
  2958. /* invalidation. Invalidate only when needed, i.e.
  2959. * when there are already sectors in the buffer cache
  2960. * whose number will change. This is useful, because
  2961. * mtools often changes the geometry of the disk after
  2962. * looking at the boot block */
  2963. if (DRS->maxblock > user_params[drive].sect ||
  2964. DRS->maxtrack ||
  2965. ((user_params[drive].sect ^ oldStretch) &
  2966. (FD_SWAPSIDES | FD_ZEROBASED)))
  2967. invalidate_drive(bdev);
  2968. else
  2969. process_fd_request();
  2970. }
  2971. return 0;
  2972. }
  2973. /* handle obsolete ioctl's */
  2974. static int ioctl_table[] = {
  2975. FDCLRPRM,
  2976. FDSETPRM,
  2977. FDDEFPRM,
  2978. FDGETPRM,
  2979. FDMSGON,
  2980. FDMSGOFF,
  2981. FDFMTBEG,
  2982. FDFMTTRK,
  2983. FDFMTEND,
  2984. FDSETEMSGTRESH,
  2985. FDFLUSH,
  2986. FDSETMAXERRS,
  2987. FDGETMAXERRS,
  2988. FDGETDRVTYP,
  2989. FDSETDRVPRM,
  2990. FDGETDRVPRM,
  2991. FDGETDRVSTAT,
  2992. FDPOLLDRVSTAT,
  2993. FDRESET,
  2994. FDGETFDCSTAT,
  2995. FDWERRORCLR,
  2996. FDWERRORGET,
  2997. FDRAWCMD,
  2998. FDEJECT,
  2999. FDTWADDLE
  3000. };
  3001. static inline int normalize_ioctl(int *cmd, int *size)
  3002. {
  3003. int i;
  3004. for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
  3005. if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
  3006. *size = _IOC_SIZE(*cmd);
  3007. *cmd = ioctl_table[i];
  3008. if (*size > _IOC_SIZE(*cmd)) {
  3009. printk("ioctl not yet supported\n");
  3010. return -EFAULT;
  3011. }
  3012. return 0;
  3013. }
  3014. }
  3015. return -EINVAL;
  3016. }
  3017. static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
  3018. {
  3019. if (type)
  3020. *g = &floppy_type[type];
  3021. else {
  3022. LOCK_FDC(drive, 0);
  3023. CALL(poll_drive(0, 0));
  3024. process_fd_request();
  3025. *g = current_type[drive];
  3026. }
  3027. if (!*g)
  3028. return -ENODEV;
  3029. return 0;
  3030. }
  3031. static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  3032. unsigned long param)
  3033. {
  3034. #define FD_IOCTL_ALLOWED ((filp) && (filp)->private_data)
  3035. #define OUT(c,x) case c: outparam = (const char *) (x); break
  3036. #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
  3037. int drive = (long)inode->i_bdev->bd_disk->private_data;
  3038. int i, type = ITYPE(UDRS->fd_device);
  3039. int ret;
  3040. int size;
  3041. union inparam {
  3042. struct floppy_struct g; /* geometry */
  3043. struct format_descr f;
  3044. struct floppy_max_errors max_errors;
  3045. struct floppy_drive_params dp;
  3046. } inparam; /* parameters coming from user space */
  3047. const char *outparam; /* parameters passed back to user space */
  3048. /* convert compatibility eject ioctls into floppy eject ioctl.
  3049. * We do this in order to provide a means to eject floppy disks before
  3050. * installing the new fdutils package */
  3051. if (cmd == CDROMEJECT || /* CD-ROM eject */
  3052. cmd == 0x6470 /* SunOS floppy eject */ ) {
  3053. DPRINT("obsolete eject ioctl\n");
  3054. DPRINT("please use floppycontrol --eject\n");
  3055. cmd = FDEJECT;
  3056. }
  3057. /* generic block device ioctls */
  3058. switch (cmd) {
  3059. /* the following have been inspired by the corresponding
  3060. * code for other block devices. */
  3061. struct floppy_struct *g;
  3062. case HDIO_GETGEO:
  3063. {
  3064. struct hd_geometry loc;
  3065. ECALL(get_floppy_geometry(drive, type, &g));
  3066. loc.heads = g->head;
  3067. loc.sectors = g->sect;
  3068. loc.cylinders = g->track;
  3069. loc.start = 0;
  3070. return _COPYOUT(loc);
  3071. }
  3072. }
  3073. /* convert the old style command into a new style command */
  3074. if ((cmd & 0xff00) == 0x0200) {
  3075. ECALL(normalize_ioctl(&cmd, &size));
  3076. } else
  3077. return -EINVAL;
  3078. /* permission checks */
  3079. if (((cmd & 0x40) && !FD_IOCTL_ALLOWED) ||
  3080. ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
  3081. return -EPERM;
  3082. /* copyin */
  3083. CLEARSTRUCT(&inparam);
  3084. if (_IOC_DIR(cmd) & _IOC_WRITE)
  3085. ECALL(fd_copyin((void __user *)param, &inparam, size))
  3086. switch (cmd) {
  3087. case FDEJECT:
  3088. if (UDRS->fd_ref != 1)
  3089. /* somebody else has this drive open */
  3090. return -EBUSY;
  3091. LOCK_FDC(drive, 1);
  3092. /* do the actual eject. Fails on
  3093. * non-Sparc architectures */
  3094. ret = fd_eject(UNIT(drive));
  3095. USETF(FD_DISK_CHANGED);
  3096. USETF(FD_VERIFY);
  3097. process_fd_request();
  3098. return ret;
  3099. case FDCLRPRM:
  3100. LOCK_FDC(drive, 1);
  3101. current_type[drive] = NULL;
  3102. floppy_sizes[drive] = MAX_DISK_SIZE << 1;
  3103. UDRS->keep_data = 0;
  3104. return invalidate_drive(inode->i_bdev);
  3105. case FDSETPRM:
  3106. case FDDEFPRM:
  3107. return set_geometry(cmd, &inparam.g,
  3108. drive, type, inode->i_bdev);
  3109. case FDGETPRM:
  3110. ECALL(get_floppy_geometry(drive, type,
  3111. (struct floppy_struct **)
  3112. &outparam));
  3113. break;
  3114. case FDMSGON:
  3115. UDP->flags |= FTD_MSG;
  3116. return 0;
  3117. case FDMSGOFF:
  3118. UDP->flags &= ~FTD_MSG;
  3119. return 0;
  3120. case FDFMTBEG:
  3121. LOCK_FDC(drive, 1);
  3122. CALL(poll_drive(1, FD_RAW_NEED_DISK));
  3123. ret = UDRS->flags;
  3124. process_fd_request();
  3125. if (ret & FD_VERIFY)
  3126. return -ENODEV;
  3127. if (!(ret & FD_DISK_WRITABLE))
  3128. return -EROFS;
  3129. return 0;
  3130. case FDFMTTRK:
  3131. if (UDRS->fd_ref != 1)
  3132. return -EBUSY;
  3133. return do_format(drive, &inparam.f);
  3134. case FDFMTEND:
  3135. case FDFLUSH:
  3136. LOCK_FDC(drive, 1);
  3137. return invalidate_drive(inode->i_bdev);
  3138. case FDSETEMSGTRESH:
  3139. UDP->max_errors.reporting =
  3140. (unsigned short)(param & 0x0f);
  3141. return 0;
  3142. OUT(FDGETMAXERRS, &UDP->max_errors);
  3143. IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
  3144. case FDGETDRVTYP:
  3145. outparam = drive_name(type, drive);
  3146. SUPBOUND(size, strlen(outparam) + 1);
  3147. break;
  3148. IN(FDSETDRVPRM, UDP, dp);
  3149. OUT(FDGETDRVPRM, UDP);
  3150. case FDPOLLDRVSTAT:
  3151. LOCK_FDC(drive, 1);
  3152. CALL(poll_drive(1, FD_RAW_NEED_DISK));
  3153. process_fd_request();
  3154. /* fall through */
  3155. OUT(FDGETDRVSTAT, UDRS);
  3156. case FDRESET:
  3157. return user_reset_fdc(drive, (int)param, 1);
  3158. OUT(FDGETFDCSTAT, UFDCS);
  3159. case FDWERRORCLR:
  3160. CLEARSTRUCT(UDRWE);
  3161. return 0;
  3162. OUT(FDWERRORGET, UDRWE);
  3163. case FDRAWCMD:
  3164. if (type)
  3165. return -EINVAL;
  3166. LOCK_FDC(drive, 1);
  3167. set_floppy(drive);
  3168. CALL(i = raw_cmd_ioctl(cmd, (void __user *)param));
  3169. process_fd_request();
  3170. return i;
  3171. case FDTWADDLE:
  3172. LOCK_FDC(drive, 1);
  3173. twaddle();
  3174. process_fd_request();
  3175. return 0;
  3176. default:
  3177. return -EINVAL;
  3178. }
  3179. if (_IOC_DIR(cmd) & _IOC_READ)
  3180. return fd_copyout((void __user *)param, outparam, size);
  3181. else
  3182. return 0;
  3183. #undef OUT
  3184. #undef IN
  3185. }
  3186. static void __init config_types(void)
  3187. {
  3188. int first = 1;
  3189. int drive;
  3190. /* read drive info out of physical CMOS */
  3191. drive = 0;
  3192. if (!UDP->cmos)
  3193. UDP->cmos = FLOPPY0_TYPE;
  3194. drive = 1;
  3195. if (!UDP->cmos && FLOPPY1_TYPE)
  3196. UDP->cmos = FLOPPY1_TYPE;
  3197. /* XXX */
  3198. /* additional physical CMOS drive detection should go here */
  3199. for (drive = 0; drive < N_DRIVE; drive++) {
  3200. unsigned int type = UDP->cmos;
  3201. struct floppy_drive_params *params;
  3202. const char *name = NULL;
  3203. static char temparea[32];
  3204. if (type < NUMBER(default_drive_params)) {
  3205. params = &default_drive_params[type].params;
  3206. if (type) {
  3207. name = default_drive_params[type].name;
  3208. allowed_drive_mask |= 1 << drive;
  3209. } else
  3210. allowed_drive_mask &= ~(1 << drive);
  3211. } else {
  3212. params = &default_drive_params[0].params;
  3213. sprintf(temparea, "unknown type %d (usb?)", type);
  3214. name = temparea;
  3215. }
  3216. if (name) {
  3217. const char *prepend = ",";
  3218. if (first) {
  3219. prepend = KERN_INFO "Floppy drive(s):";
  3220. first = 0;
  3221. }
  3222. printk("%s fd%d is %s", prepend, drive, name);
  3223. register_devfs_entries(drive);
  3224. }
  3225. *UDP = *params;
  3226. }
  3227. if (!first)
  3228. printk("\n");
  3229. }
  3230. static int floppy_release(struct inode *inode, struct file *filp)
  3231. {
  3232. int drive = (long)inode->i_bdev->bd_disk->private_data;
  3233. down(&open_lock);
  3234. if (UDRS->fd_ref < 0)
  3235. UDRS->fd_ref = 0;
  3236. else if (!UDRS->fd_ref--) {
  3237. DPRINT("floppy_release with fd_ref == 0");
  3238. UDRS->fd_ref = 0;
  3239. }
  3240. if (!UDRS->fd_ref)
  3241. opened_bdev[drive] = NULL;
  3242. floppy_release_irq_and_dma();
  3243. up(&open_lock);
  3244. return 0;
  3245. }
  3246. /*
  3247. * floppy_open check for aliasing (/dev/fd0 can be the same as
  3248. * /dev/PS0 etc), and disallows simultaneous access to the same
  3249. * drive with different device numbers.
  3250. */
  3251. static int floppy_open(struct inode *inode, struct file *filp)
  3252. {
  3253. int drive = (long)inode->i_bdev->bd_disk->private_data;
  3254. int old_dev;
  3255. int try;
  3256. int res = -EBUSY;
  3257. char *tmp;
  3258. filp->private_data = (void *)0;
  3259. down(&open_lock);
  3260. old_dev = UDRS->fd_device;
  3261. if (opened_bdev[drive] && opened_bdev[drive] != inode->i_bdev)
  3262. goto out2;
  3263. if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
  3264. USETF(FD_DISK_CHANGED);
  3265. USETF(FD_VERIFY);
  3266. }
  3267. if (UDRS->fd_ref == -1 || (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
  3268. goto out2;
  3269. if (floppy_grab_irq_and_dma())
  3270. goto out2;
  3271. if (filp->f_flags & O_EXCL)
  3272. UDRS->fd_ref = -1;
  3273. else
  3274. UDRS->fd_ref++;
  3275. opened_bdev[drive] = inode->i_bdev;
  3276. res = -ENXIO;
  3277. if (!floppy_track_buffer) {
  3278. /* if opening an ED drive, reserve a big buffer,
  3279. * else reserve a small one */
  3280. if ((UDP->cmos == 6) || (UDP->cmos == 5))
  3281. try = 64; /* Only 48 actually useful */
  3282. else
  3283. try = 32; /* Only 24 actually useful */
  3284. tmp = (char *)fd_dma_mem_alloc(1024 * try);
  3285. if (!tmp && !floppy_track_buffer) {
  3286. try >>= 1; /* buffer only one side */
  3287. INFBOUND(try, 16);
  3288. tmp = (char *)fd_dma_mem_alloc(1024 * try);
  3289. }
  3290. if (!tmp && !floppy_track_buffer) {
  3291. fallback_on_nodma_alloc(&tmp, 2048 * try);
  3292. }
  3293. if (!tmp && !floppy_track_buffer) {
  3294. DPRINT("Unable to allocate DMA memory\n");
  3295. goto out;
  3296. }
  3297. if (floppy_track_buffer) {
  3298. if (tmp)
  3299. fd_dma_mem_free((unsigned long)tmp, try * 1024);
  3300. } else {
  3301. buffer_min = buffer_max = -1;
  3302. floppy_track_buffer = tmp;
  3303. max_buffer_sectors = try;
  3304. }
  3305. }
  3306. UDRS->fd_device = iminor(inode);
  3307. set_capacity(disks[drive], floppy_sizes[iminor(inode)]);
  3308. if (old_dev != -1 && old_dev != iminor(inode)) {
  3309. if (buffer_drive == drive)
  3310. buffer_track = -1;
  3311. }
  3312. /* Allow ioctls if we have write-permissions even if read-only open.
  3313. * Needed so that programs such as fdrawcmd still can work on write
  3314. * protected disks */
  3315. if (filp->f_mode & 2
  3316. || permission(filp->f_dentry->d_inode, 2, NULL) == 0)
  3317. filp->private_data = (void *)8;
  3318. if (UFDCS->rawcmd == 1)
  3319. UFDCS->rawcmd = 2;
  3320. if (!(filp->f_flags & O_NDELAY)) {
  3321. if (filp->f_mode & 3) {
  3322. UDRS->last_checked = 0;
  3323. check_disk_change(inode->i_bdev);
  3324. if (UTESTF(FD_DISK_CHANGED))
  3325. goto out;
  3326. }
  3327. res = -EROFS;
  3328. if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
  3329. goto out;
  3330. }
  3331. up(&open_lock);
  3332. return 0;
  3333. out:
  3334. if (UDRS->fd_ref < 0)
  3335. UDRS->fd_ref = 0;
  3336. else
  3337. UDRS->fd_ref--;
  3338. if (!UDRS->fd_ref)
  3339. opened_bdev[drive] = NULL;
  3340. floppy_release_irq_and_dma();
  3341. out2:
  3342. up(&open_lock);
  3343. return res;
  3344. }
  3345. /*
  3346. * Check if the disk has been changed or if a change has been faked.
  3347. */
  3348. static int check_floppy_change(struct gendisk *disk)
  3349. {
  3350. int drive = (long)disk->private_data;
  3351. if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
  3352. return 1;
  3353. if (UDP->checkfreq < (int)(jiffies - UDRS->last_checked)) {
  3354. if (floppy_grab_irq_and_dma()) {
  3355. return 1;
  3356. }
  3357. lock_fdc(drive, 0);
  3358. poll_drive(0, 0);
  3359. process_fd_request();
  3360. floppy_release_irq_and_dma();
  3361. }
  3362. if (UTESTF(FD_DISK_CHANGED) ||
  3363. UTESTF(FD_VERIFY) ||
  3364. test_bit(drive, &fake_change) ||
  3365. (!ITYPE(UDRS->fd_device) && !current_type[drive]))
  3366. return 1;
  3367. return 0;
  3368. }
  3369. /*
  3370. * This implements "read block 0" for floppy_revalidate().
  3371. * Needed for format autodetection, checking whether there is
  3372. * a disk in the drive, and whether that disk is writable.
  3373. */
  3374. static int floppy_rb0_complete(struct bio *bio, unsigned int bytes_done,
  3375. int err)
  3376. {
  3377. if (bio->bi_size)
  3378. return 1;
  3379. complete((struct completion *)bio->bi_private);
  3380. return 0;
  3381. }
  3382. static int __floppy_read_block_0(struct block_device *bdev)
  3383. {
  3384. struct bio bio;
  3385. struct bio_vec bio_vec;
  3386. struct completion complete;
  3387. struct page *page;
  3388. size_t size;
  3389. page = alloc_page(GFP_NOIO);
  3390. if (!page) {
  3391. process_fd_request();
  3392. return -ENOMEM;
  3393. }
  3394. size = bdev->bd_block_size;
  3395. if (!size)
  3396. size = 1024;
  3397. bio_init(&bio);
  3398. bio.bi_io_vec = &bio_vec;
  3399. bio_vec.bv_page = page;
  3400. bio_vec.bv_len = size;
  3401. bio_vec.bv_offset = 0;
  3402. bio.bi_vcnt = 1;
  3403. bio.bi_idx = 0;
  3404. bio.bi_size = size;
  3405. bio.bi_bdev = bdev;
  3406. bio.bi_sector = 0;
  3407. init_completion(&complete);
  3408. bio.bi_private = &complete;
  3409. bio.bi_end_io = floppy_rb0_complete;
  3410. submit_bio(READ, &bio);
  3411. generic_unplug_device(bdev_get_queue(bdev));
  3412. process_fd_request();
  3413. wait_for_completion(&complete);
  3414. __free_page(page);
  3415. return 0;
  3416. }
  3417. /* revalidate the floppy disk, i.e. trigger format autodetection by reading
  3418. * the bootblock (block 0). "Autodetection" is also needed to check whether
  3419. * there is a disk in the drive at all... Thus we also do it for fixed
  3420. * geometry formats */
  3421. static int floppy_revalidate(struct gendisk *disk)
  3422. {
  3423. int drive = (long)disk->private_data;
  3424. #define NO_GEOM (!current_type[drive] && !ITYPE(UDRS->fd_device))
  3425. int cf;
  3426. int res = 0;
  3427. if (UTESTF(FD_DISK_CHANGED) ||
  3428. UTESTF(FD_VERIFY) || test_bit(drive, &fake_change) || NO_GEOM) {
  3429. if (usage_count == 0) {
  3430. printk("VFS: revalidate called on non-open device.\n");
  3431. return -EFAULT;
  3432. }
  3433. lock_fdc(drive, 0);
  3434. cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
  3435. if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)) {
  3436. process_fd_request(); /*already done by another thread */
  3437. return 0;
  3438. }
  3439. UDRS->maxblock = 0;
  3440. UDRS->maxtrack = 0;
  3441. if (buffer_drive == drive)
  3442. buffer_track = -1;
  3443. clear_bit(drive, &fake_change);
  3444. UCLEARF(FD_DISK_CHANGED);
  3445. if (cf)
  3446. UDRS->generation++;
  3447. if (NO_GEOM) {
  3448. /* auto-sensing */
  3449. res = __floppy_read_block_0(opened_bdev[drive]);
  3450. } else {
  3451. if (cf)
  3452. poll_drive(0, FD_RAW_NEED_DISK);
  3453. process_fd_request();
  3454. }
  3455. }
  3456. set_capacity(disk, floppy_sizes[UDRS->fd_device]);
  3457. return res;
  3458. }
  3459. static struct block_device_operations floppy_fops = {
  3460. .owner = THIS_MODULE,
  3461. .open = floppy_open,
  3462. .release = floppy_release,
  3463. .ioctl = fd_ioctl,
  3464. .media_changed = check_floppy_change,
  3465. .revalidate_disk = floppy_revalidate,
  3466. };
  3467. static char *table[] = {
  3468. "", "d360", "h1200", "u360", "u720", "h360", "h720",
  3469. "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
  3470. "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
  3471. "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
  3472. "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
  3473. NULL
  3474. };
  3475. static int t360[] = { 1, 0 },
  3476. t1200[] = { 2, 5, 6, 10, 12, 14, 16, 18, 20, 23, 0 },
  3477. t3in[] = { 8, 9, 26, 27, 28, 7, 11, 15, 19, 24, 25, 29, 31, 3, 4, 13,
  3478. 17, 21, 22, 30, 0 };
  3479. static int *table_sup[] =
  3480. { NULL, t360, t1200, t3in + 5 + 8, t3in + 5, t3in, t3in };
  3481. static void __init register_devfs_entries(int drive)
  3482. {
  3483. int base_minor = (drive < 4) ? drive : (124 + drive);
  3484. if (UDP->cmos < NUMBER(default_drive_params)) {
  3485. int i = 0;
  3486. do {
  3487. int minor = base_minor + (table_sup[UDP->cmos][i] << 2);
  3488. devfs_mk_bdev(MKDEV(FLOPPY_MAJOR, minor),
  3489. S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP |
  3490. S_IWGRP, "floppy/%d%s", drive,
  3491. table[table_sup[UDP->cmos][i]]);
  3492. } while (table_sup[UDP->cmos][i++]);
  3493. }
  3494. }
  3495. /*
  3496. * Floppy Driver initialization
  3497. * =============================
  3498. */
  3499. /* Determine the floppy disk controller type */
  3500. /* This routine was written by David C. Niemi */
  3501. static char __init get_fdc_version(void)
  3502. {
  3503. int r;
  3504. output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
  3505. if (FDCS->reset)
  3506. return FDC_NONE;
  3507. if ((r = result()) <= 0x00)
  3508. return FDC_NONE; /* No FDC present ??? */
  3509. if ((r == 1) && (reply_buffer[0] == 0x80)) {
  3510. printk(KERN_INFO "FDC %d is an 8272A\n", fdc);
  3511. return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
  3512. }
  3513. if (r != 10) {
  3514. printk
  3515. ("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
  3516. fdc, r);
  3517. return FDC_UNKNOWN;
  3518. }
  3519. if (!fdc_configure()) {
  3520. printk(KERN_INFO "FDC %d is an 82072\n", fdc);
  3521. return FDC_82072; /* 82072 doesn't know CONFIGURE */
  3522. }
  3523. output_byte(FD_PERPENDICULAR);
  3524. if (need_more_output() == MORE_OUTPUT) {
  3525. output_byte(0);
  3526. } else {
  3527. printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
  3528. return FDC_82072A; /* 82072A as found on Sparcs. */
  3529. }
  3530. output_byte(FD_UNLOCK);
  3531. r = result();
  3532. if ((r == 1) && (reply_buffer[0] == 0x80)) {
  3533. printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
  3534. return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
  3535. * LOCK/UNLOCK */
  3536. }
  3537. if ((r != 1) || (reply_buffer[0] != 0x00)) {
  3538. printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
  3539. fdc, r);
  3540. return FDC_UNKNOWN;
  3541. }
  3542. output_byte(FD_PARTID);
  3543. r = result();
  3544. if (r != 1) {
  3545. printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
  3546. fdc, r);
  3547. return FDC_UNKNOWN;
  3548. }
  3549. if (reply_buffer[0] == 0x80) {
  3550. printk(KERN_INFO "FDC %d is a post-1991 82077\n", fdc);
  3551. return FDC_82077; /* Revised 82077AA passes all the tests */
  3552. }
  3553. switch (reply_buffer[0] >> 5) {
  3554. case 0x0:
  3555. /* Either a 82078-1 or a 82078SL running at 5Volt */
  3556. printk(KERN_INFO "FDC %d is an 82078.\n", fdc);
  3557. return FDC_82078;
  3558. case 0x1:
  3559. printk(KERN_INFO "FDC %d is a 44pin 82078\n", fdc);
  3560. return FDC_82078;
  3561. case 0x2:
  3562. printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
  3563. return FDC_S82078B;
  3564. case 0x3:
  3565. printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n",
  3566. fdc);
  3567. return FDC_87306;
  3568. default:
  3569. printk(KERN_INFO
  3570. "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
  3571. fdc, reply_buffer[0] >> 5);
  3572. return FDC_82078_UNKN;
  3573. }
  3574. } /* get_fdc_version */
  3575. /* lilo configuration */
  3576. static void __init floppy_set_flags(int *ints, int param, int param2)
  3577. {
  3578. int i;
  3579. for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
  3580. if (param)
  3581. default_drive_params[i].params.flags |= param2;
  3582. else
  3583. default_drive_params[i].params.flags &= ~param2;
  3584. }
  3585. DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
  3586. }
  3587. static void __init daring(int *ints, int param, int param2)
  3588. {
  3589. int i;
  3590. for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
  3591. if (param) {
  3592. default_drive_params[i].params.select_delay = 0;
  3593. default_drive_params[i].params.flags |=
  3594. FD_SILENT_DCL_CLEAR;
  3595. } else {
  3596. default_drive_params[i].params.select_delay =
  3597. 2 * HZ / 100;
  3598. default_drive_params[i].params.flags &=
  3599. ~FD_SILENT_DCL_CLEAR;
  3600. }
  3601. }
  3602. DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
  3603. }
  3604. static void __init set_cmos(int *ints, int dummy, int dummy2)
  3605. {
  3606. int current_drive = 0;
  3607. if (ints[0] != 2) {
  3608. DPRINT("wrong number of parameters for CMOS\n");
  3609. return;
  3610. }
  3611. current_drive = ints[1];
  3612. if (current_drive < 0 || current_drive >= 8) {
  3613. DPRINT("bad drive for set_cmos\n");
  3614. return;
  3615. }
  3616. #if N_FDC > 1
  3617. if (current_drive >= 4 && !FDC2)
  3618. FDC2 = 0x370;
  3619. #endif
  3620. DP->cmos = ints[2];
  3621. DPRINT("setting CMOS code to %d\n", ints[2]);
  3622. }
  3623. static struct param_table {
  3624. const char *name;
  3625. void (*fn) (int *ints, int param, int param2);
  3626. int *var;
  3627. int def_param;
  3628. int param2;
  3629. } config_params[] __initdata = {
  3630. {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
  3631. {"all_drives", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
  3632. {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
  3633. {"irq", NULL, &FLOPPY_IRQ, 6, 0},
  3634. {"dma", NULL, &FLOPPY_DMA, 2, 0},
  3635. {"daring", daring, NULL, 1, 0},
  3636. #if N_FDC > 1
  3637. {"two_fdc", NULL, &FDC2, 0x370, 0},
  3638. {"one_fdc", NULL, &FDC2, 0, 0},
  3639. #endif
  3640. {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
  3641. {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
  3642. {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
  3643. {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
  3644. {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
  3645. {"nodma", NULL, &can_use_virtual_dma, 1, 0},
  3646. {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
  3647. {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
  3648. {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
  3649. {"nofifo", NULL, &no_fifo, 0x20, 0},
  3650. {"usefifo", NULL, &no_fifo, 0, 0},
  3651. {"cmos", set_cmos, NULL, 0, 0},
  3652. {"slow", NULL, &slow_floppy, 1, 0},
  3653. {"unexpected_interrupts", NULL, &print_unex, 1, 0},
  3654. {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
  3655. {"L40SX", NULL, &print_unex, 0, 0}
  3656. EXTRA_FLOPPY_PARAMS
  3657. };
  3658. static int __init floppy_setup(char *str)
  3659. {
  3660. int i;
  3661. int param;
  3662. int ints[11];
  3663. str = get_options(str, ARRAY_SIZE(ints), ints);
  3664. if (str) {
  3665. for (i = 0; i < ARRAY_SIZE(config_params); i++) {
  3666. if (strcmp(str, config_params[i].name) == 0) {
  3667. if (ints[0])
  3668. param = ints[1];
  3669. else
  3670. param = config_params[i].def_param;
  3671. if (config_params[i].fn)
  3672. config_params[i].
  3673. fn(ints, param,
  3674. config_params[i].param2);
  3675. if (config_params[i].var) {
  3676. DPRINT("%s=%d\n", str, param);
  3677. *config_params[i].var = param;
  3678. }
  3679. return 1;
  3680. }
  3681. }
  3682. }
  3683. if (str) {
  3684. DPRINT("unknown floppy option [%s]\n", str);
  3685. DPRINT("allowed options are:");
  3686. for (i = 0; i < ARRAY_SIZE(config_params); i++)
  3687. printk(" %s", config_params[i].name);
  3688. printk("\n");
  3689. } else
  3690. DPRINT("botched floppy option\n");
  3691. DPRINT("Read Documentation/floppy.txt\n");
  3692. return 0;
  3693. }
  3694. static int have_no_fdc = -ENODEV;
  3695. static void floppy_device_release(struct device *dev)
  3696. {
  3697. complete(&device_release);
  3698. }
  3699. static struct platform_device floppy_device = {
  3700. .name = "floppy",
  3701. .id = 0,
  3702. .dev = {
  3703. .release = floppy_device_release,
  3704. }
  3705. };
  3706. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  3707. {
  3708. int drive = (*part & 3) | ((*part & 0x80) >> 5);
  3709. if (drive >= N_DRIVE ||
  3710. !(allowed_drive_mask & (1 << drive)) ||
  3711. fdc_state[FDC(drive)].version == FDC_NONE)
  3712. return NULL;
  3713. if (((*part >> 2) & 0x1f) >= NUMBER(floppy_type))
  3714. return NULL;
  3715. *part = 0;
  3716. return get_disk(disks[drive]);
  3717. }
  3718. static int __init floppy_init(void)
  3719. {
  3720. int i, unit, drive;
  3721. int err, dr;
  3722. raw_cmd = NULL;
  3723. for (dr = 0; dr < N_DRIVE; dr++) {
  3724. disks[dr] = alloc_disk(1);
  3725. if (!disks[dr]) {
  3726. err = -ENOMEM;
  3727. goto out_put_disk;
  3728. }
  3729. disks[dr]->major = FLOPPY_MAJOR;
  3730. disks[dr]->first_minor = TOMINOR(dr);
  3731. disks[dr]->fops = &floppy_fops;
  3732. sprintf(disks[dr]->disk_name, "fd%d", dr);
  3733. init_timer(&motor_off_timer[dr]);
  3734. motor_off_timer[dr].data = dr;
  3735. motor_off_timer[dr].function = motor_off_callback;
  3736. }
  3737. devfs_mk_dir("floppy");
  3738. err = register_blkdev(FLOPPY_MAJOR, "fd");
  3739. if (err)
  3740. goto out_devfs_remove;
  3741. floppy_queue = blk_init_queue(do_fd_request, &floppy_lock);
  3742. if (!floppy_queue) {
  3743. err = -ENOMEM;
  3744. goto out_unreg_blkdev;
  3745. }
  3746. blk_queue_max_sectors(floppy_queue, 64);
  3747. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  3748. floppy_find, NULL, NULL);
  3749. for (i = 0; i < 256; i++)
  3750. if (ITYPE(i))
  3751. floppy_sizes[i] = floppy_type[ITYPE(i)].size;
  3752. else
  3753. floppy_sizes[i] = MAX_DISK_SIZE << 1;
  3754. reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
  3755. config_types();
  3756. for (i = 0; i < N_FDC; i++) {
  3757. fdc = i;
  3758. CLEARSTRUCT(FDCS);
  3759. FDCS->dtr = -1;
  3760. FDCS->dor = 0x4;
  3761. #if defined(__sparc__) || defined(__mc68000__)
  3762. /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
  3763. #ifdef __mc68000__
  3764. if (MACH_IS_SUN3X)
  3765. #endif
  3766. FDCS->version = FDC_82072A;
  3767. #endif
  3768. }
  3769. use_virtual_dma = can_use_virtual_dma & 1;
  3770. #if defined(CONFIG_PPC64)
  3771. if (check_legacy_ioport(FDC1)) {
  3772. del_timer(&fd_timeout);
  3773. err = -ENODEV;
  3774. goto out_unreg_region;
  3775. }
  3776. #endif
  3777. fdc_state[0].address = FDC1;
  3778. if (fdc_state[0].address == -1) {
  3779. del_timer(&fd_timeout);
  3780. err = -ENODEV;
  3781. goto out_unreg_region;
  3782. }
  3783. #if N_FDC > 1
  3784. fdc_state[1].address = FDC2;
  3785. #endif
  3786. fdc = 0; /* reset fdc in case of unexpected interrupt */
  3787. err = floppy_grab_irq_and_dma();
  3788. if (err) {
  3789. del_timer(&fd_timeout);
  3790. err = -EBUSY;
  3791. goto out_unreg_region;
  3792. }
  3793. /* initialise drive state */
  3794. for (drive = 0; drive < N_DRIVE; drive++) {
  3795. CLEARSTRUCT(UDRS);
  3796. CLEARSTRUCT(UDRWE);
  3797. USETF(FD_DISK_NEWCHANGE);
  3798. USETF(FD_DISK_CHANGED);
  3799. USETF(FD_VERIFY);
  3800. UDRS->fd_device = -1;
  3801. floppy_track_buffer = NULL;
  3802. max_buffer_sectors = 0;
  3803. }
  3804. /*
  3805. * Small 10 msec delay to let through any interrupt that
  3806. * initialization might have triggered, to not
  3807. * confuse detection:
  3808. */
  3809. msleep(10);
  3810. for (i = 0; i < N_FDC; i++) {
  3811. fdc = i;
  3812. FDCS->driver_version = FD_DRIVER_VERSION;
  3813. for (unit = 0; unit < 4; unit++)
  3814. FDCS->track[unit] = 0;
  3815. if (FDCS->address == -1)
  3816. continue;
  3817. FDCS->rawcmd = 2;
  3818. if (user_reset_fdc(-1, FD_RESET_ALWAYS, 0)) {
  3819. /* free ioports reserved by floppy_grab_irq_and_dma() */
  3820. release_region(FDCS->address + 2, 4);
  3821. release_region(FDCS->address + 7, 1);
  3822. FDCS->address = -1;
  3823. FDCS->version = FDC_NONE;
  3824. continue;
  3825. }
  3826. /* Try to determine the floppy controller type */
  3827. FDCS->version = get_fdc_version();
  3828. if (FDCS->version == FDC_NONE) {
  3829. /* free ioports reserved by floppy_grab_irq_and_dma() */
  3830. release_region(FDCS->address + 2, 4);
  3831. release_region(FDCS->address + 7, 1);
  3832. FDCS->address = -1;
  3833. continue;
  3834. }
  3835. if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
  3836. can_use_virtual_dma = 0;
  3837. have_no_fdc = 0;
  3838. /* Not all FDCs seem to be able to handle the version command
  3839. * properly, so force a reset for the standard FDC clones,
  3840. * to avoid interrupt garbage.
  3841. */
  3842. user_reset_fdc(-1, FD_RESET_ALWAYS, 0);
  3843. }
  3844. fdc = 0;
  3845. del_timer(&fd_timeout);
  3846. current_drive = 0;
  3847. floppy_release_irq_and_dma();
  3848. initialising = 0;
  3849. if (have_no_fdc) {
  3850. DPRINT("no floppy controllers found\n");
  3851. err = have_no_fdc;
  3852. goto out_flush_work;
  3853. }
  3854. err = platform_device_register(&floppy_device);
  3855. if (err)
  3856. goto out_flush_work;
  3857. for (drive = 0; drive < N_DRIVE; drive++) {
  3858. if (!(allowed_drive_mask & (1 << drive)))
  3859. continue;
  3860. if (fdc_state[FDC(drive)].version == FDC_NONE)
  3861. continue;
  3862. /* to be cleaned up... */
  3863. disks[drive]->private_data = (void *)(long)drive;
  3864. disks[drive]->queue = floppy_queue;
  3865. disks[drive]->flags |= GENHD_FL_REMOVABLE;
  3866. disks[drive]->driverfs_dev = &floppy_device.dev;
  3867. add_disk(disks[drive]);
  3868. }
  3869. return 0;
  3870. out_flush_work:
  3871. flush_scheduled_work();
  3872. if (usage_count)
  3873. floppy_release_irq_and_dma();
  3874. out_unreg_region:
  3875. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  3876. blk_cleanup_queue(floppy_queue);
  3877. out_unreg_blkdev:
  3878. unregister_blkdev(FLOPPY_MAJOR, "fd");
  3879. out_devfs_remove:
  3880. devfs_remove("floppy");
  3881. out_put_disk:
  3882. while (dr--) {
  3883. del_timer(&motor_off_timer[dr]);
  3884. put_disk(disks[dr]);
  3885. }
  3886. return err;
  3887. }
  3888. static DEFINE_SPINLOCK(floppy_usage_lock);
  3889. static int floppy_grab_irq_and_dma(void)
  3890. {
  3891. unsigned long flags;
  3892. spin_lock_irqsave(&floppy_usage_lock, flags);
  3893. if (usage_count++) {
  3894. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3895. return 0;
  3896. }
  3897. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3898. if (fd_request_irq()) {
  3899. DPRINT("Unable to grab IRQ%d for the floppy driver\n",
  3900. FLOPPY_IRQ);
  3901. spin_lock_irqsave(&floppy_usage_lock, flags);
  3902. usage_count--;
  3903. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3904. return -1;
  3905. }
  3906. if (fd_request_dma()) {
  3907. DPRINT("Unable to grab DMA%d for the floppy driver\n",
  3908. FLOPPY_DMA);
  3909. fd_free_irq();
  3910. spin_lock_irqsave(&floppy_usage_lock, flags);
  3911. usage_count--;
  3912. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3913. return -1;
  3914. }
  3915. for (fdc = 0; fdc < N_FDC; fdc++) {
  3916. if (FDCS->address != -1) {
  3917. if (!request_region(FDCS->address + 2, 4, "floppy")) {
  3918. DPRINT("Floppy io-port 0x%04lx in use\n",
  3919. FDCS->address + 2);
  3920. goto cleanup1;
  3921. }
  3922. if (!request_region(FDCS->address + 7, 1, "floppy DIR")) {
  3923. DPRINT("Floppy io-port 0x%04lx in use\n",
  3924. FDCS->address + 7);
  3925. goto cleanup2;
  3926. }
  3927. /* address + 6 is reserved, and may be taken by IDE.
  3928. * Unfortunately, Adaptec doesn't know this :-(, */
  3929. }
  3930. }
  3931. for (fdc = 0; fdc < N_FDC; fdc++) {
  3932. if (FDCS->address != -1) {
  3933. reset_fdc_info(1);
  3934. fd_outb(FDCS->dor, FD_DOR);
  3935. }
  3936. }
  3937. fdc = 0;
  3938. set_dor(0, ~0, 8); /* avoid immediate interrupt */
  3939. for (fdc = 0; fdc < N_FDC; fdc++)
  3940. if (FDCS->address != -1)
  3941. fd_outb(FDCS->dor, FD_DOR);
  3942. /*
  3943. * The driver will try and free resources and relies on us
  3944. * to know if they were allocated or not.
  3945. */
  3946. fdc = 0;
  3947. irqdma_allocated = 1;
  3948. return 0;
  3949. cleanup2:
  3950. release_region(FDCS->address + 2, 4);
  3951. cleanup1:
  3952. fd_free_irq();
  3953. fd_free_dma();
  3954. while (--fdc >= 0) {
  3955. release_region(FDCS->address + 2, 4);
  3956. release_region(FDCS->address + 7, 1);
  3957. }
  3958. spin_lock_irqsave(&floppy_usage_lock, flags);
  3959. usage_count--;
  3960. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3961. return -1;
  3962. }
  3963. static void floppy_release_irq_and_dma(void)
  3964. {
  3965. int old_fdc;
  3966. #ifdef FLOPPY_SANITY_CHECK
  3967. #ifndef __sparc__
  3968. int drive;
  3969. #endif
  3970. #endif
  3971. long tmpsize;
  3972. unsigned long tmpaddr;
  3973. unsigned long flags;
  3974. spin_lock_irqsave(&floppy_usage_lock, flags);
  3975. if (--usage_count) {
  3976. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3977. return;
  3978. }
  3979. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3980. if (irqdma_allocated) {
  3981. fd_disable_dma();
  3982. fd_free_dma();
  3983. fd_free_irq();
  3984. irqdma_allocated = 0;
  3985. }
  3986. set_dor(0, ~0, 8);
  3987. #if N_FDC > 1
  3988. set_dor(1, ~8, 0);
  3989. #endif
  3990. floppy_enable_hlt();
  3991. if (floppy_track_buffer && max_buffer_sectors) {
  3992. tmpsize = max_buffer_sectors * 1024;
  3993. tmpaddr = (unsigned long)floppy_track_buffer;
  3994. floppy_track_buffer = NULL;
  3995. max_buffer_sectors = 0;
  3996. buffer_min = buffer_max = -1;
  3997. fd_dma_mem_free(tmpaddr, tmpsize);
  3998. }
  3999. #ifdef FLOPPY_SANITY_CHECK
  4000. #ifndef __sparc__
  4001. for (drive = 0; drive < N_FDC * 4; drive++)
  4002. if (timer_pending(motor_off_timer + drive))
  4003. printk("motor off timer %d still active\n", drive);
  4004. #endif
  4005. if (timer_pending(&fd_timeout))
  4006. printk("floppy timer still active:%s\n", timeout_message);
  4007. if (timer_pending(&fd_timer))
  4008. printk("auxiliary floppy timer still active\n");
  4009. if (floppy_work.pending)
  4010. printk("work still pending\n");
  4011. #endif
  4012. old_fdc = fdc;
  4013. for (fdc = 0; fdc < N_FDC; fdc++)
  4014. if (FDCS->address != -1) {
  4015. release_region(FDCS->address + 2, 4);
  4016. release_region(FDCS->address + 7, 1);
  4017. }
  4018. fdc = old_fdc;
  4019. }
  4020. #ifdef MODULE
  4021. static char *floppy;
  4022. static void unregister_devfs_entries(int drive)
  4023. {
  4024. int i;
  4025. if (UDP->cmos < NUMBER(default_drive_params)) {
  4026. i = 0;
  4027. do {
  4028. devfs_remove("floppy/%d%s", drive,
  4029. table[table_sup[UDP->cmos][i]]);
  4030. } while (table_sup[UDP->cmos][i++]);
  4031. }
  4032. }
  4033. static void __init parse_floppy_cfg_string(char *cfg)
  4034. {
  4035. char *ptr;
  4036. while (*cfg) {
  4037. for (ptr = cfg; *cfg && *cfg != ' ' && *cfg != '\t'; cfg++) ;
  4038. if (*cfg) {
  4039. *cfg = '\0';
  4040. cfg++;
  4041. }
  4042. if (*ptr)
  4043. floppy_setup(ptr);
  4044. }
  4045. }
  4046. int init_module(void)
  4047. {
  4048. if (floppy)
  4049. parse_floppy_cfg_string(floppy);
  4050. return floppy_init();
  4051. }
  4052. void cleanup_module(void)
  4053. {
  4054. int drive;
  4055. init_completion(&device_release);
  4056. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  4057. unregister_blkdev(FLOPPY_MAJOR, "fd");
  4058. for (drive = 0; drive < N_DRIVE; drive++) {
  4059. del_timer_sync(&motor_off_timer[drive]);
  4060. if ((allowed_drive_mask & (1 << drive)) &&
  4061. fdc_state[FDC(drive)].version != FDC_NONE) {
  4062. del_gendisk(disks[drive]);
  4063. unregister_devfs_entries(drive);
  4064. }
  4065. put_disk(disks[drive]);
  4066. }
  4067. platform_device_unregister(&floppy_device);
  4068. devfs_remove("floppy");
  4069. del_timer_sync(&fd_timeout);
  4070. del_timer_sync(&fd_timer);
  4071. blk_cleanup_queue(floppy_queue);
  4072. if (usage_count)
  4073. floppy_release_irq_and_dma();
  4074. /* eject disk, if any */
  4075. fd_eject(0);
  4076. wait_for_completion(&device_release);
  4077. }
  4078. module_param(floppy, charp, 0);
  4079. module_param(FLOPPY_IRQ, int, 0);
  4080. module_param(FLOPPY_DMA, int, 0);
  4081. MODULE_AUTHOR("Alain L. Knaff");
  4082. MODULE_SUPPORTED_DEVICE("fd");
  4083. MODULE_LICENSE("GPL");
  4084. #else
  4085. __setup("floppy=", floppy_setup);
  4086. module_init(floppy_init)
  4087. #endif
  4088. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);