ide-floppy.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209
  1. /*
  2. * linux/drivers/ide/ide-floppy.c Version 0.99 Feb 24 2002
  3. *
  4. * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
  5. * Copyright (C) 2000 - 2002 Paul Bristow <paul@paulbristow.net>
  6. */
  7. /*
  8. * IDE ATAPI floppy driver.
  9. *
  10. * The driver currently doesn't have any fancy features, just the bare
  11. * minimum read/write support.
  12. *
  13. * This driver supports the following IDE floppy drives:
  14. *
  15. * LS-120/240 SuperDisk
  16. * Iomega Zip 100/250
  17. * Iomega PC Card Clik!/PocketZip
  18. *
  19. * Many thanks to Lode Leroy <Lode.Leroy@www.ibase.be>, who tested so many
  20. * ALPHA patches to this driver on an EASYSTOR LS-120 ATAPI floppy drive.
  21. *
  22. * Ver 0.1 Oct 17 96 Initial test version, mostly based on ide-tape.c.
  23. * Ver 0.2 Oct 31 96 Minor changes.
  24. * Ver 0.3 Dec 2 96 Fixed error recovery bug.
  25. * Ver 0.4 Jan 26 97 Add support for the HDIO_GETGEO ioctl.
  26. * Ver 0.5 Feb 21 97 Add partitions support.
  27. * Use the minimum of the LBA and CHS capacities.
  28. * Avoid hwgroup->rq == NULL on the last irq.
  29. * Fix potential null dereferencing with DEBUG_LOG.
  30. * Ver 0.8 Dec 7 97 Increase irq timeout from 10 to 50 seconds.
  31. * Add media write-protect detection.
  32. * Issue START command only if TEST UNIT READY fails.
  33. * Add work-around for IOMEGA ZIP revision 21.D.
  34. * Remove idefloppy_get_capabilities().
  35. * Ver 0.9 Jul 4 99 Fix a bug which might have caused the number of
  36. * bytes requested on each interrupt to be zero.
  37. * Thanks to <shanos@es.co.nz> for pointing this out.
  38. * Ver 0.9.sv Jan 6 01 Sam Varshavchik <mrsam@courier-mta.com>
  39. * Implement low level formatting. Reimplemented
  40. * IDEFLOPPY_CAPABILITIES_PAGE, since we need the srfp
  41. * bit. My LS-120 drive barfs on
  42. * IDEFLOPPY_CAPABILITIES_PAGE, but maybe it's just me.
  43. * Compromise by not reporting a failure to get this
  44. * mode page. Implemented four IOCTLs in order to
  45. * implement formatting. IOCTls begin with 0x4600,
  46. * 0x46 is 'F' as in Format.
  47. * Jan 9 01 Userland option to select format verify.
  48. * Added PC_SUPPRESS_ERROR flag - some idefloppy drives
  49. * do not implement IDEFLOPPY_CAPABILITIES_PAGE, and
  50. * return a sense error. Suppress error reporting in
  51. * this particular case in order to avoid spurious
  52. * errors in syslog. The culprit is
  53. * idefloppy_get_capability_page(), so move it to
  54. * idefloppy_begin_format() so that it's not used
  55. * unless absolutely necessary.
  56. * If drive does not support format progress indication
  57. * monitor the dsc bit in the status register.
  58. * Also, O_NDELAY on open will allow the device to be
  59. * opened without a disk available. This can be used to
  60. * open an unformatted disk, or get the device capacity.
  61. * Ver 0.91 Dec 11 99 Added IOMEGA Clik! drive support by
  62. * <paul@paulbristow.net>
  63. * Ver 0.92 Oct 22 00 Paul Bristow became official maintainer for this
  64. * driver. Included Powerbook internal zip kludge.
  65. * Ver 0.93 Oct 24 00 Fixed bugs for Clik! drive
  66. * no disk on insert and disk change now works
  67. * Ver 0.94 Oct 27 00 Tidied up to remove strstr(Clik) everywhere
  68. * Ver 0.95 Nov 7 00 Brought across to kernel 2.4
  69. * Ver 0.96 Jan 7 01 Actually in line with release version of 2.4.0
  70. * including set_bit patch from Rusty Russell
  71. * Ver 0.97 Jul 22 01 Merge 0.91-0.96 onto 0.9.sv for ac series
  72. * Ver 0.97.sv Aug 3 01 Backported from 2.4.7-ac3
  73. * Ver 0.98 Oct 26 01 Split idefloppy_transfer_pc into two pieces to
  74. * fix a lost interrupt problem. It appears the busy
  75. * bit was being deasserted by my IOMEGA ATAPI ZIP 100
  76. * drive before the drive was actually ready.
  77. * Ver 0.98a Oct 29 01 Expose delay value so we can play.
  78. * Ver 0.99 Feb 24 02 Remove duplicate code, modify clik! detection code
  79. * to support new PocketZip drives
  80. */
  81. #define IDEFLOPPY_VERSION "0.99.newide"
  82. #include <linux/config.h>
  83. #include <linux/module.h>
  84. #include <linux/types.h>
  85. #include <linux/string.h>
  86. #include <linux/kernel.h>
  87. #include <linux/delay.h>
  88. #include <linux/timer.h>
  89. #include <linux/mm.h>
  90. #include <linux/interrupt.h>
  91. #include <linux/major.h>
  92. #include <linux/errno.h>
  93. #include <linux/genhd.h>
  94. #include <linux/slab.h>
  95. #include <linux/cdrom.h>
  96. #include <linux/ide.h>
  97. #include <linux/bitops.h>
  98. #include <asm/byteorder.h>
  99. #include <asm/irq.h>
  100. #include <asm/uaccess.h>
  101. #include <asm/io.h>
  102. #include <asm/unaligned.h>
  103. /*
  104. * The following are used to debug the driver.
  105. */
  106. #define IDEFLOPPY_DEBUG_LOG 0
  107. #define IDEFLOPPY_DEBUG_INFO 0
  108. #define IDEFLOPPY_DEBUG_BUGS 1
  109. /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
  110. #define IDEFLOPPY_DEBUG( fmt, args... )
  111. #if IDEFLOPPY_DEBUG_LOG
  112. #define debug_log printk
  113. #else
  114. #define debug_log(fmt, args... ) do {} while(0)
  115. #endif
  116. /*
  117. * Some drives require a longer irq timeout.
  118. */
  119. #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
  120. /*
  121. * After each failed packet command we issue a request sense command
  122. * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
  123. */
  124. #define IDEFLOPPY_MAX_PC_RETRIES 3
  125. /*
  126. * With each packet command, we allocate a buffer of
  127. * IDEFLOPPY_PC_BUFFER_SIZE bytes.
  128. */
  129. #define IDEFLOPPY_PC_BUFFER_SIZE 256
  130. /*
  131. * In various places in the driver, we need to allocate storage
  132. * for packet commands and requests, which will remain valid while
  133. * we leave the driver to wait for an interrupt or a timeout event.
  134. */
  135. #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
  136. /*
  137. * Our view of a packet command.
  138. */
  139. typedef struct idefloppy_packet_command_s {
  140. u8 c[12]; /* Actual packet bytes */
  141. int retries; /* On each retry, we increment retries */
  142. int error; /* Error code */
  143. int request_transfer; /* Bytes to transfer */
  144. int actually_transferred; /* Bytes actually transferred */
  145. int buffer_size; /* Size of our data buffer */
  146. int b_count; /* Missing/Available data on the current buffer */
  147. struct request *rq; /* The corresponding request */
  148. u8 *buffer; /* Data buffer */
  149. u8 *current_position; /* Pointer into the above buffer */
  150. void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
  151. u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
  152. unsigned long flags; /* Status/Action bit flags: long for set_bit */
  153. } idefloppy_pc_t;
  154. /*
  155. * Packet command flag bits.
  156. */
  157. #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
  158. #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
  159. #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
  160. #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
  161. #define PC_WRITING 5 /* Data direction */
  162. #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
  163. /*
  164. * Removable Block Access Capabilities Page
  165. */
  166. typedef struct {
  167. #if defined(__LITTLE_ENDIAN_BITFIELD)
  168. unsigned page_code :6; /* Page code - Should be 0x1b */
  169. unsigned reserved1_6 :1; /* Reserved */
  170. unsigned ps :1; /* Should be 0 */
  171. #elif defined(__BIG_ENDIAN_BITFIELD)
  172. unsigned ps :1; /* Should be 0 */
  173. unsigned reserved1_6 :1; /* Reserved */
  174. unsigned page_code :6; /* Page code - Should be 0x1b */
  175. #else
  176. #error "Bitfield endianness not defined! Check your byteorder.h"
  177. #endif
  178. u8 page_length; /* Page Length - Should be 0xa */
  179. #if defined(__LITTLE_ENDIAN_BITFIELD)
  180. unsigned reserved2 :6;
  181. unsigned srfp :1; /* Supports reporting progress of format */
  182. unsigned sflp :1; /* System floppy type device */
  183. unsigned tlun :3; /* Total logical units supported by the device */
  184. unsigned reserved3 :3;
  185. unsigned sml :1; /* Single / Multiple lun supported */
  186. unsigned ncd :1; /* Non cd optical device */
  187. #elif defined(__BIG_ENDIAN_BITFIELD)
  188. unsigned sflp :1; /* System floppy type device */
  189. unsigned srfp :1; /* Supports reporting progress of format */
  190. unsigned reserved2 :6;
  191. unsigned ncd :1; /* Non cd optical device */
  192. unsigned sml :1; /* Single / Multiple lun supported */
  193. unsigned reserved3 :3;
  194. unsigned tlun :3; /* Total logical units supported by the device */
  195. #else
  196. #error "Bitfield endianness not defined! Check your byteorder.h"
  197. #endif
  198. u8 reserved[8];
  199. } idefloppy_capabilities_page_t;
  200. /*
  201. * Flexible disk page.
  202. */
  203. typedef struct {
  204. #if defined(__LITTLE_ENDIAN_BITFIELD)
  205. unsigned page_code :6; /* Page code - Should be 0x5 */
  206. unsigned reserved1_6 :1; /* Reserved */
  207. unsigned ps :1; /* The device is capable of saving the page */
  208. #elif defined(__BIG_ENDIAN_BITFIELD)
  209. unsigned ps :1; /* The device is capable of saving the page */
  210. unsigned reserved1_6 :1; /* Reserved */
  211. unsigned page_code :6; /* Page code - Should be 0x5 */
  212. #else
  213. #error "Bitfield endianness not defined! Check your byteorder.h"
  214. #endif
  215. u8 page_length; /* Page Length - Should be 0x1e */
  216. u16 transfer_rate; /* In kilobits per second */
  217. u8 heads, sectors; /* Number of heads, Number of sectors per track */
  218. u16 sector_size; /* Byes per sector */
  219. u16 cyls; /* Number of cylinders */
  220. u8 reserved10[10];
  221. u8 motor_delay; /* Motor off delay */
  222. u8 reserved21[7];
  223. u16 rpm; /* Rotations per minute */
  224. u8 reserved30[2];
  225. } idefloppy_flexible_disk_page_t;
  226. /*
  227. * Format capacity
  228. */
  229. typedef struct {
  230. u8 reserved[3];
  231. u8 length; /* Length of the following descriptors in bytes */
  232. } idefloppy_capacity_header_t;
  233. typedef struct {
  234. u32 blocks; /* Number of blocks */
  235. #if defined(__LITTLE_ENDIAN_BITFIELD)
  236. unsigned dc :2; /* Descriptor Code */
  237. unsigned reserved :6;
  238. #elif defined(__BIG_ENDIAN_BITFIELD)
  239. unsigned reserved :6;
  240. unsigned dc :2; /* Descriptor Code */
  241. #else
  242. #error "Bitfield endianness not defined! Check your byteorder.h"
  243. #endif
  244. u8 length_msb; /* Block Length (MSB)*/
  245. u16 length; /* Block Length */
  246. } idefloppy_capacity_descriptor_t;
  247. #define CAPACITY_INVALID 0x00
  248. #define CAPACITY_UNFORMATTED 0x01
  249. #define CAPACITY_CURRENT 0x02
  250. #define CAPACITY_NO_CARTRIDGE 0x03
  251. /*
  252. * Most of our global data which we need to save even as we leave the
  253. * driver due to an interrupt or a timer event is stored in a variable
  254. * of type idefloppy_floppy_t, defined below.
  255. */
  256. typedef struct ide_floppy_obj {
  257. ide_drive_t *drive;
  258. ide_driver_t *driver;
  259. struct gendisk *disk;
  260. struct kref kref;
  261. /* Current packet command */
  262. idefloppy_pc_t *pc;
  263. /* Last failed packet command */
  264. idefloppy_pc_t *failed_pc;
  265. /* Packet command stack */
  266. idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
  267. /* Next free packet command storage space */
  268. int pc_stack_index;
  269. struct request rq_stack[IDEFLOPPY_PC_STACK];
  270. /* We implement a circular array */
  271. int rq_stack_index;
  272. /*
  273. * Last error information
  274. */
  275. u8 sense_key, asc, ascq;
  276. /* delay this long before sending packet command */
  277. u8 ticks;
  278. int progress_indication;
  279. /*
  280. * Device information
  281. */
  282. /* Current format */
  283. int blocks, block_size, bs_factor;
  284. /* Last format capacity */
  285. idefloppy_capacity_descriptor_t capacity;
  286. /* Copy of the flexible disk page */
  287. idefloppy_flexible_disk_page_t flexible_disk_page;
  288. /* Write protect */
  289. int wp;
  290. /* Supports format progress report */
  291. int srfp;
  292. /* Status/Action flags */
  293. unsigned long flags;
  294. } idefloppy_floppy_t;
  295. #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
  296. /*
  297. * Floppy flag bits values.
  298. */
  299. #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
  300. #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
  301. #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
  302. #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
  303. #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
  304. #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
  305. /*
  306. * ATAPI floppy drive packet commands
  307. */
  308. #define IDEFLOPPY_FORMAT_UNIT_CMD 0x04
  309. #define IDEFLOPPY_INQUIRY_CMD 0x12
  310. #define IDEFLOPPY_MODE_SELECT_CMD 0x55
  311. #define IDEFLOPPY_MODE_SENSE_CMD 0x5a
  312. #define IDEFLOPPY_READ10_CMD 0x28
  313. #define IDEFLOPPY_READ12_CMD 0xa8
  314. #define IDEFLOPPY_READ_CAPACITY_CMD 0x23
  315. #define IDEFLOPPY_REQUEST_SENSE_CMD 0x03
  316. #define IDEFLOPPY_PREVENT_REMOVAL_CMD 0x1e
  317. #define IDEFLOPPY_SEEK_CMD 0x2b
  318. #define IDEFLOPPY_START_STOP_CMD 0x1b
  319. #define IDEFLOPPY_TEST_UNIT_READY_CMD 0x00
  320. #define IDEFLOPPY_VERIFY_CMD 0x2f
  321. #define IDEFLOPPY_WRITE10_CMD 0x2a
  322. #define IDEFLOPPY_WRITE12_CMD 0xaa
  323. #define IDEFLOPPY_WRITE_VERIFY_CMD 0x2e
  324. /*
  325. * Defines for the mode sense command
  326. */
  327. #define MODE_SENSE_CURRENT 0x00
  328. #define MODE_SENSE_CHANGEABLE 0x01
  329. #define MODE_SENSE_DEFAULT 0x02
  330. #define MODE_SENSE_SAVED 0x03
  331. /*
  332. * IOCTLs used in low-level formatting.
  333. */
  334. #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
  335. #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
  336. #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
  337. #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
  338. #if 0
  339. /*
  340. * Special requests for our block device strategy routine.
  341. */
  342. #define IDEFLOPPY_FIRST_RQ 90
  343. /*
  344. * IDEFLOPPY_PC_RQ is used to queue a packet command in the request queue.
  345. */
  346. #define IDEFLOPPY_PC_RQ 90
  347. #define IDEFLOPPY_LAST_RQ 90
  348. /*
  349. * A macro which can be used to check if a given request command
  350. * originated in the driver or in the buffer cache layer.
  351. */
  352. #define IDEFLOPPY_RQ_CMD(cmd) ((cmd >= IDEFLOPPY_FIRST_RQ) && (cmd <= IDEFLOPPY_LAST_RQ))
  353. #endif
  354. /*
  355. * Error codes which are returned in rq->errors to the higher part
  356. * of the driver.
  357. */
  358. #define IDEFLOPPY_ERROR_GENERAL 101
  359. /*
  360. * The following is used to format the general configuration word of
  361. * the ATAPI IDENTIFY DEVICE command.
  362. */
  363. struct idefloppy_id_gcw {
  364. #if defined(__LITTLE_ENDIAN_BITFIELD)
  365. unsigned packet_size :2; /* Packet Size */
  366. unsigned reserved234 :3; /* Reserved */
  367. unsigned drq_type :2; /* Command packet DRQ type */
  368. unsigned removable :1; /* Removable media */
  369. unsigned device_type :5; /* Device type */
  370. unsigned reserved13 :1; /* Reserved */
  371. unsigned protocol :2; /* Protocol type */
  372. #elif defined(__BIG_ENDIAN_BITFIELD)
  373. unsigned protocol :2; /* Protocol type */
  374. unsigned reserved13 :1; /* Reserved */
  375. unsigned device_type :5; /* Device type */
  376. unsigned removable :1; /* Removable media */
  377. unsigned drq_type :2; /* Command packet DRQ type */
  378. unsigned reserved234 :3; /* Reserved */
  379. unsigned packet_size :2; /* Packet Size */
  380. #else
  381. #error "Bitfield endianness not defined! Check your byteorder.h"
  382. #endif
  383. };
  384. /*
  385. * INQUIRY packet command - Data Format
  386. */
  387. typedef struct {
  388. #if defined(__LITTLE_ENDIAN_BITFIELD)
  389. unsigned device_type :5; /* Peripheral Device Type */
  390. unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
  391. unsigned reserved1_6t0 :7; /* Reserved */
  392. unsigned rmb :1; /* Removable Medium Bit */
  393. unsigned ansi_version :3; /* ANSI Version */
  394. unsigned ecma_version :3; /* ECMA Version */
  395. unsigned iso_version :2; /* ISO Version */
  396. unsigned response_format :4; /* Response Data Format */
  397. unsigned reserved3_45 :2; /* Reserved */
  398. unsigned reserved3_6 :1; /* TrmIOP - Reserved */
  399. unsigned reserved3_7 :1; /* AENC - Reserved */
  400. #elif defined(__BIG_ENDIAN_BITFIELD)
  401. unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
  402. unsigned device_type :5; /* Peripheral Device Type */
  403. unsigned rmb :1; /* Removable Medium Bit */
  404. unsigned reserved1_6t0 :7; /* Reserved */
  405. unsigned iso_version :2; /* ISO Version */
  406. unsigned ecma_version :3; /* ECMA Version */
  407. unsigned ansi_version :3; /* ANSI Version */
  408. unsigned reserved3_7 :1; /* AENC - Reserved */
  409. unsigned reserved3_6 :1; /* TrmIOP - Reserved */
  410. unsigned reserved3_45 :2; /* Reserved */
  411. unsigned response_format :4; /* Response Data Format */
  412. #else
  413. #error "Bitfield endianness not defined! Check your byteorder.h"
  414. #endif
  415. u8 additional_length; /* Additional Length (total_length-4) */
  416. u8 rsv5, rsv6, rsv7; /* Reserved */
  417. u8 vendor_id[8]; /* Vendor Identification */
  418. u8 product_id[16]; /* Product Identification */
  419. u8 revision_level[4]; /* Revision Level */
  420. u8 vendor_specific[20]; /* Vendor Specific - Optional */
  421. u8 reserved56t95[40]; /* Reserved - Optional */
  422. /* Additional information may be returned */
  423. } idefloppy_inquiry_result_t;
  424. /*
  425. * REQUEST SENSE packet command result - Data Format.
  426. */
  427. typedef struct {
  428. #if defined(__LITTLE_ENDIAN_BITFIELD)
  429. unsigned error_code :7; /* Current error (0x70) */
  430. unsigned valid :1; /* The information field conforms to SFF-8070i */
  431. u8 reserved1 :8; /* Reserved */
  432. unsigned sense_key :4; /* Sense Key */
  433. unsigned reserved2_4 :1; /* Reserved */
  434. unsigned ili :1; /* Incorrect Length Indicator */
  435. unsigned reserved2_67 :2;
  436. #elif defined(__BIG_ENDIAN_BITFIELD)
  437. unsigned valid :1; /* The information field conforms to SFF-8070i */
  438. unsigned error_code :7; /* Current error (0x70) */
  439. u8 reserved1 :8; /* Reserved */
  440. unsigned reserved2_67 :2;
  441. unsigned ili :1; /* Incorrect Length Indicator */
  442. unsigned reserved2_4 :1; /* Reserved */
  443. unsigned sense_key :4; /* Sense Key */
  444. #else
  445. #error "Bitfield endianness not defined! Check your byteorder.h"
  446. #endif
  447. u32 information __attribute__ ((packed));
  448. u8 asl; /* Additional sense length (n-7) */
  449. u32 command_specific; /* Additional command specific information */
  450. u8 asc; /* Additional Sense Code */
  451. u8 ascq; /* Additional Sense Code Qualifier */
  452. u8 replaceable_unit_code; /* Field Replaceable Unit Code */
  453. u8 sksv[3];
  454. u8 pad[2]; /* Padding to 20 bytes */
  455. } idefloppy_request_sense_result_t;
  456. /*
  457. * Pages of the SELECT SENSE / MODE SENSE packet commands.
  458. */
  459. #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
  460. #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
  461. /*
  462. * Mode Parameter Header for the MODE SENSE packet command
  463. */
  464. typedef struct {
  465. u16 mode_data_length; /* Length of the following data transfer */
  466. u8 medium_type; /* Medium Type */
  467. #if defined(__LITTLE_ENDIAN_BITFIELD)
  468. unsigned reserved3 :7;
  469. unsigned wp :1; /* Write protect */
  470. #elif defined(__BIG_ENDIAN_BITFIELD)
  471. unsigned wp :1; /* Write protect */
  472. unsigned reserved3 :7;
  473. #else
  474. #error "Bitfield endianness not defined! Check your byteorder.h"
  475. #endif
  476. u8 reserved[4];
  477. } idefloppy_mode_parameter_header_t;
  478. static DECLARE_MUTEX(idefloppy_ref_sem);
  479. #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
  480. #define ide_floppy_g(disk) \
  481. container_of((disk)->private_data, struct ide_floppy_obj, driver)
  482. static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
  483. {
  484. struct ide_floppy_obj *floppy = NULL;
  485. down(&idefloppy_ref_sem);
  486. floppy = ide_floppy_g(disk);
  487. if (floppy)
  488. kref_get(&floppy->kref);
  489. up(&idefloppy_ref_sem);
  490. return floppy;
  491. }
  492. static void ide_floppy_release(struct kref *);
  493. static void ide_floppy_put(struct ide_floppy_obj *floppy)
  494. {
  495. down(&idefloppy_ref_sem);
  496. kref_put(&floppy->kref, ide_floppy_release);
  497. up(&idefloppy_ref_sem);
  498. }
  499. /*
  500. * Too bad. The drive wants to send us data which we are not ready to accept.
  501. * Just throw it away.
  502. */
  503. static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
  504. {
  505. while (bcount--)
  506. (void) HWIF(drive)->INB(IDE_DATA_REG);
  507. }
  508. #if IDEFLOPPY_DEBUG_BUGS
  509. static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
  510. {
  511. while (bcount--)
  512. HWIF(drive)->OUTB(0, IDE_DATA_REG);
  513. }
  514. #endif /* IDEFLOPPY_DEBUG_BUGS */
  515. /*
  516. * idefloppy_do_end_request is used to finish servicing a request.
  517. *
  518. * For read/write requests, we will call ide_end_request to pass to the
  519. * next buffer.
  520. */
  521. static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
  522. {
  523. idefloppy_floppy_t *floppy = drive->driver_data;
  524. struct request *rq = HWGROUP(drive)->rq;
  525. int error;
  526. debug_log(KERN_INFO "Reached idefloppy_end_request\n");
  527. switch (uptodate) {
  528. case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
  529. case 1: error = 0; break;
  530. default: error = uptodate;
  531. }
  532. if (error)
  533. floppy->failed_pc = NULL;
  534. /* Why does this happen? */
  535. if (!rq)
  536. return 0;
  537. if (!(rq->flags & REQ_SPECIAL)) { //if (!IDEFLOPPY_RQ_CMD (rq->cmd)) {
  538. /* our real local end request function */
  539. ide_end_request(drive, uptodate, nsecs);
  540. return 0;
  541. }
  542. rq->errors = error;
  543. /* fixme: need to move this local also */
  544. ide_end_drive_cmd(drive, 0, 0);
  545. return 0;
  546. }
  547. static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
  548. {
  549. struct request *rq = pc->rq;
  550. struct bio_vec *bvec;
  551. struct bio *bio;
  552. unsigned long flags;
  553. char *data;
  554. int count, i, done = 0;
  555. rq_for_each_bio(bio, rq) {
  556. bio_for_each_segment(bvec, bio, i) {
  557. if (!bcount)
  558. break;
  559. count = min(bvec->bv_len, bcount);
  560. data = bvec_kmap_irq(bvec, &flags);
  561. drive->hwif->atapi_input_bytes(drive, data, count);
  562. bvec_kunmap_irq(data, &flags);
  563. bcount -= count;
  564. pc->b_count += count;
  565. done += count;
  566. }
  567. }
  568. idefloppy_do_end_request(drive, 1, done >> 9);
  569. if (bcount) {
  570. printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
  571. idefloppy_discard_data(drive, bcount);
  572. }
  573. }
  574. static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
  575. {
  576. struct request *rq = pc->rq;
  577. struct bio *bio;
  578. struct bio_vec *bvec;
  579. unsigned long flags;
  580. int count, i, done = 0;
  581. char *data;
  582. rq_for_each_bio(bio, rq) {
  583. bio_for_each_segment(bvec, bio, i) {
  584. if (!bcount)
  585. break;
  586. count = min(bvec->bv_len, bcount);
  587. data = bvec_kmap_irq(bvec, &flags);
  588. drive->hwif->atapi_output_bytes(drive, data, count);
  589. bvec_kunmap_irq(data, &flags);
  590. bcount -= count;
  591. pc->b_count += count;
  592. done += count;
  593. }
  594. }
  595. idefloppy_do_end_request(drive, 1, done >> 9);
  596. #if IDEFLOPPY_DEBUG_BUGS
  597. if (bcount) {
  598. printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
  599. idefloppy_write_zeros(drive, bcount);
  600. }
  601. #endif
  602. }
  603. static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
  604. {
  605. struct request *rq = pc->rq;
  606. struct bio *bio = rq->bio;
  607. while ((bio = rq->bio) != NULL)
  608. idefloppy_do_end_request(drive, 1, 0);
  609. }
  610. /*
  611. * idefloppy_queue_pc_head generates a new packet command request in front
  612. * of the request queue, before the current request, so that it will be
  613. * processed immediately, on the next pass through the driver.
  614. */
  615. static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
  616. {
  617. struct ide_floppy_obj *floppy = drive->driver_data;
  618. ide_init_drive_cmd(rq);
  619. rq->buffer = (char *) pc;
  620. rq->flags = REQ_SPECIAL; //rq->cmd = IDEFLOPPY_PC_RQ;
  621. rq->rq_disk = floppy->disk;
  622. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  623. }
  624. static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
  625. {
  626. idefloppy_floppy_t *floppy = drive->driver_data;
  627. if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
  628. floppy->pc_stack_index=0;
  629. return (&floppy->pc_stack[floppy->pc_stack_index++]);
  630. }
  631. static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
  632. {
  633. idefloppy_floppy_t *floppy = drive->driver_data;
  634. if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
  635. floppy->rq_stack_index = 0;
  636. return (&floppy->rq_stack[floppy->rq_stack_index++]);
  637. }
  638. /*
  639. * idefloppy_analyze_error is called on each failed packet command retry
  640. * to analyze the request sense.
  641. */
  642. static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
  643. {
  644. idefloppy_floppy_t *floppy = drive->driver_data;
  645. floppy->sense_key = result->sense_key;
  646. floppy->asc = result->asc;
  647. floppy->ascq = result->ascq;
  648. floppy->progress_indication = result->sksv[0] & 0x80 ?
  649. (u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
  650. if (floppy->failed_pc)
  651. debug_log(KERN_INFO "ide-floppy: pc = %x, sense key = %x, "
  652. "asc = %x, ascq = %x\n", floppy->failed_pc->c[0],
  653. result->sense_key, result->asc, result->ascq);
  654. else
  655. debug_log(KERN_INFO "ide-floppy: sense key = %x, asc = %x, "
  656. "ascq = %x\n", result->sense_key,
  657. result->asc, result->ascq);
  658. }
  659. static void idefloppy_request_sense_callback (ide_drive_t *drive)
  660. {
  661. idefloppy_floppy_t *floppy = drive->driver_data;
  662. debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
  663. if (!floppy->pc->error) {
  664. idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
  665. idefloppy_do_end_request(drive, 1, 0);
  666. } else {
  667. printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
  668. idefloppy_do_end_request(drive, 0, 0);
  669. }
  670. }
  671. /*
  672. * General packet command callback function.
  673. */
  674. static void idefloppy_pc_callback (ide_drive_t *drive)
  675. {
  676. idefloppy_floppy_t *floppy = drive->driver_data;
  677. debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
  678. idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
  679. }
  680. /*
  681. * idefloppy_init_pc initializes a packet command.
  682. */
  683. static void idefloppy_init_pc (idefloppy_pc_t *pc)
  684. {
  685. memset(pc->c, 0, 12);
  686. pc->retries = 0;
  687. pc->flags = 0;
  688. pc->request_transfer = 0;
  689. pc->buffer = pc->pc_buffer;
  690. pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
  691. pc->callback = &idefloppy_pc_callback;
  692. }
  693. static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
  694. {
  695. idefloppy_init_pc(pc);
  696. pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
  697. pc->c[4] = 255;
  698. pc->request_transfer = 18;
  699. pc->callback = &idefloppy_request_sense_callback;
  700. }
  701. /*
  702. * idefloppy_retry_pc is called when an error was detected during the
  703. * last packet command. We queue a request sense packet command in
  704. * the head of the request list.
  705. */
  706. static void idefloppy_retry_pc (ide_drive_t *drive)
  707. {
  708. idefloppy_pc_t *pc;
  709. struct request *rq;
  710. atapi_error_t error;
  711. error.all = HWIF(drive)->INB(IDE_ERROR_REG);
  712. pc = idefloppy_next_pc_storage(drive);
  713. rq = idefloppy_next_rq_storage(drive);
  714. idefloppy_create_request_sense_cmd(pc);
  715. idefloppy_queue_pc_head(drive, pc, rq);
  716. }
  717. /*
  718. * idefloppy_pc_intr is the usual interrupt handler which will be called
  719. * during a packet command.
  720. */
  721. static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
  722. {
  723. idefloppy_floppy_t *floppy = drive->driver_data;
  724. atapi_status_t status;
  725. atapi_bcount_t bcount;
  726. atapi_ireason_t ireason;
  727. idefloppy_pc_t *pc = floppy->pc;
  728. struct request *rq = pc->rq;
  729. unsigned int temp;
  730. debug_log(KERN_INFO "ide-floppy: Reached %s interrupt handler\n",
  731. __FUNCTION__);
  732. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  733. if (HWIF(drive)->ide_dma_end(drive)) {
  734. set_bit(PC_DMA_ERROR, &pc->flags);
  735. } else {
  736. pc->actually_transferred = pc->request_transfer;
  737. idefloppy_update_buffers(drive, pc);
  738. }
  739. debug_log(KERN_INFO "ide-floppy: DMA finished\n");
  740. }
  741. /* Clear the interrupt */
  742. status.all = HWIF(drive)->INB(IDE_STATUS_REG);
  743. if (!status.b.drq) { /* No more interrupts */
  744. debug_log(KERN_INFO "Packet command completed, %d bytes "
  745. "transferred\n", pc->actually_transferred);
  746. clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  747. local_irq_enable();
  748. if (status.b.check || test_bit(PC_DMA_ERROR, &pc->flags)) {
  749. /* Error detected */
  750. debug_log(KERN_INFO "ide-floppy: %s: I/O error\n",
  751. drive->name);
  752. rq->errors++;
  753. if (pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
  754. printk(KERN_ERR "ide-floppy: I/O error in "
  755. "request sense command\n");
  756. return ide_do_reset(drive);
  757. }
  758. /* Retry operation */
  759. idefloppy_retry_pc(drive);
  760. /* queued, but not started */
  761. return ide_stopped;
  762. }
  763. pc->error = 0;
  764. if (floppy->failed_pc == pc)
  765. floppy->failed_pc = NULL;
  766. /* Command finished - Call the callback function */
  767. pc->callback(drive);
  768. return ide_stopped;
  769. }
  770. if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  771. printk(KERN_ERR "ide-floppy: The floppy wants to issue "
  772. "more interrupts in DMA mode\n");
  773. (void)__ide_dma_off(drive);
  774. return ide_do_reset(drive);
  775. }
  776. /* Get the number of bytes to transfer */
  777. bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  778. bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  779. /* on this interrupt */
  780. ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
  781. if (ireason.b.cod) {
  782. printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
  783. return ide_do_reset(drive);
  784. }
  785. if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) {
  786. /* Hopefully, we will never get here */
  787. printk(KERN_ERR "ide-floppy: We wanted to %s, ",
  788. ireason.b.io ? "Write":"Read");
  789. printk(KERN_ERR "but the floppy wants us to %s !\n",
  790. ireason.b.io ? "Read":"Write");
  791. return ide_do_reset(drive);
  792. }
  793. if (!test_bit(PC_WRITING, &pc->flags)) {
  794. /* Reading - Check that we have enough space */
  795. temp = pc->actually_transferred + bcount.all;
  796. if (temp > pc->request_transfer) {
  797. if (temp > pc->buffer_size) {
  798. printk(KERN_ERR "ide-floppy: The floppy wants "
  799. "to send us more data than expected "
  800. "- discarding data\n");
  801. idefloppy_discard_data(drive,bcount.all);
  802. if (HWGROUP(drive)->handler != NULL)
  803. BUG();
  804. ide_set_handler(drive,
  805. &idefloppy_pc_intr,
  806. IDEFLOPPY_WAIT_CMD,
  807. NULL);
  808. return ide_started;
  809. }
  810. debug_log(KERN_NOTICE "ide-floppy: The floppy wants to "
  811. "send us more data than expected - "
  812. "allowing transfer\n");
  813. }
  814. }
  815. if (test_bit(PC_WRITING, &pc->flags)) {
  816. if (pc->buffer != NULL)
  817. /* Write the current buffer */
  818. HWIF(drive)->atapi_output_bytes(drive,
  819. pc->current_position,
  820. bcount.all);
  821. else
  822. idefloppy_output_buffers(drive, pc, bcount.all);
  823. } else {
  824. if (pc->buffer != NULL)
  825. /* Read the current buffer */
  826. HWIF(drive)->atapi_input_bytes(drive,
  827. pc->current_position,
  828. bcount.all);
  829. else
  830. idefloppy_input_buffers(drive, pc, bcount.all);
  831. }
  832. /* Update the current position */
  833. pc->actually_transferred += bcount.all;
  834. pc->current_position += bcount.all;
  835. if (HWGROUP(drive)->handler != NULL)
  836. BUG();
  837. ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
  838. return ide_started;
  839. }
  840. /*
  841. * This is the original routine that did the packet transfer.
  842. * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
  843. * for that drive below. The algorithm is chosen based on drive type
  844. */
  845. static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
  846. {
  847. ide_startstop_t startstop;
  848. idefloppy_floppy_t *floppy = drive->driver_data;
  849. atapi_ireason_t ireason;
  850. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  851. printk(KERN_ERR "ide-floppy: Strange, packet command "
  852. "initiated yet DRQ isn't asserted\n");
  853. return startstop;
  854. }
  855. ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
  856. if (!ireason.b.cod || ireason.b.io) {
  857. printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
  858. "issuing a packet command\n");
  859. return ide_do_reset(drive);
  860. }
  861. if (HWGROUP(drive)->handler != NULL)
  862. BUG();
  863. /* Set the interrupt routine */
  864. ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
  865. /* Send the actual packet */
  866. HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
  867. return ide_started;
  868. }
  869. /*
  870. * What we have here is a classic case of a top half / bottom half
  871. * interrupt service routine. In interrupt mode, the device sends
  872. * an interrupt to signal it's ready to receive a packet. However,
  873. * we need to delay about 2-3 ticks before issuing the packet or we
  874. * gets in trouble.
  875. *
  876. * So, follow carefully. transfer_pc1 is called as an interrupt (or
  877. * directly). In either case, when the device says it's ready for a
  878. * packet, we schedule the packet transfer to occur about 2-3 ticks
  879. * later in transfer_pc2.
  880. */
  881. static int idefloppy_transfer_pc2 (ide_drive_t *drive)
  882. {
  883. idefloppy_floppy_t *floppy = drive->driver_data;
  884. /* Send the actual packet */
  885. HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
  886. /* Timeout for the packet command */
  887. return IDEFLOPPY_WAIT_CMD;
  888. }
  889. static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
  890. {
  891. idefloppy_floppy_t *floppy = drive->driver_data;
  892. ide_startstop_t startstop;
  893. atapi_ireason_t ireason;
  894. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  895. printk(KERN_ERR "ide-floppy: Strange, packet command "
  896. "initiated yet DRQ isn't asserted\n");
  897. return startstop;
  898. }
  899. ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
  900. if (!ireason.b.cod || ireason.b.io) {
  901. printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
  902. "while issuing a packet command\n");
  903. return ide_do_reset(drive);
  904. }
  905. /*
  906. * The following delay solves a problem with ATAPI Zip 100 drives
  907. * where the Busy flag was apparently being deasserted before the
  908. * unit was ready to receive data. This was happening on a
  909. * 1200 MHz Athlon system. 10/26/01 25msec is too short,
  910. * 40 and 50msec work well. idefloppy_pc_intr will not be actually
  911. * used until after the packet is moved in about 50 msec.
  912. */
  913. if (HWGROUP(drive)->handler != NULL)
  914. BUG();
  915. ide_set_handler(drive,
  916. &idefloppy_pc_intr, /* service routine for packet command */
  917. floppy->ticks, /* wait this long before "failing" */
  918. &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
  919. return ide_started;
  920. }
  921. /**
  922. * idefloppy_should_report_error()
  923. *
  924. * Supresses error messages resulting from Medium not present
  925. */
  926. static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
  927. {
  928. if (floppy->sense_key == 0x02 &&
  929. floppy->asc == 0x3a &&
  930. floppy->ascq == 0x00)
  931. return 0;
  932. return 1;
  933. }
  934. /*
  935. * Issue a packet command
  936. */
  937. static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
  938. {
  939. idefloppy_floppy_t *floppy = drive->driver_data;
  940. ide_hwif_t *hwif = drive->hwif;
  941. atapi_feature_t feature;
  942. atapi_bcount_t bcount;
  943. ide_handler_t *pkt_xfer_routine;
  944. #if 0 /* Accessing floppy->pc is not valid here, the previous pc may be gone
  945. and have lived on another thread's stack; that stack may have become
  946. unmapped meanwhile (CONFIG_DEBUG_PAGEALLOC). */
  947. #if IDEFLOPPY_DEBUG_BUGS
  948. if (floppy->pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD &&
  949. pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
  950. printk(KERN_ERR "ide-floppy: possible ide-floppy.c bug - "
  951. "Two request sense in serial were issued\n");
  952. }
  953. #endif /* IDEFLOPPY_DEBUG_BUGS */
  954. #endif
  955. if (floppy->failed_pc == NULL &&
  956. pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
  957. floppy->failed_pc = pc;
  958. /* Set the current packet command */
  959. floppy->pc = pc;
  960. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
  961. test_bit(PC_ABORT, &pc->flags)) {
  962. /*
  963. * We will "abort" retrying a packet command in case
  964. * a legitimate error code was received.
  965. */
  966. if (!test_bit(PC_ABORT, &pc->flags)) {
  967. if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
  968. if (idefloppy_should_report_error(floppy))
  969. printk(KERN_ERR "ide-floppy: %s: I/O error, "
  970. "pc = %2x, key = %2x, "
  971. "asc = %2x, ascq = %2x\n",
  972. drive->name, pc->c[0],
  973. floppy->sense_key,
  974. floppy->asc, floppy->ascq);
  975. }
  976. /* Giving up */
  977. pc->error = IDEFLOPPY_ERROR_GENERAL;
  978. }
  979. floppy->failed_pc = NULL;
  980. pc->callback(drive);
  981. return ide_stopped;
  982. }
  983. debug_log(KERN_INFO "Retry number - %d\n",pc->retries);
  984. pc->retries++;
  985. /* We haven't transferred any data yet */
  986. pc->actually_transferred = 0;
  987. pc->current_position = pc->buffer;
  988. bcount.all = min(pc->request_transfer, 63 * 1024);
  989. if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
  990. (void)__ide_dma_off(drive);
  991. }
  992. feature.all = 0;
  993. if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
  994. feature.b.dma = !hwif->dma_setup(drive);
  995. if (IDE_CONTROL_REG)
  996. HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
  997. /* Use PIO/DMA */
  998. HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
  999. HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
  1000. HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
  1001. HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
  1002. if (feature.b.dma) { /* Begin DMA, if necessary */
  1003. set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  1004. hwif->dma_start(drive);
  1005. }
  1006. /* Can we transfer the packet when we get the interrupt or wait? */
  1007. if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
  1008. /* wait */
  1009. pkt_xfer_routine = &idefloppy_transfer_pc1;
  1010. } else {
  1011. /* immediate */
  1012. pkt_xfer_routine = &idefloppy_transfer_pc;
  1013. }
  1014. if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
  1015. /* Issue the packet command */
  1016. ide_execute_command(drive, WIN_PACKETCMD,
  1017. pkt_xfer_routine,
  1018. IDEFLOPPY_WAIT_CMD,
  1019. NULL);
  1020. return ide_started;
  1021. } else {
  1022. /* Issue the packet command */
  1023. HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  1024. return (*pkt_xfer_routine) (drive);
  1025. }
  1026. }
  1027. static void idefloppy_rw_callback (ide_drive_t *drive)
  1028. {
  1029. debug_log(KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
  1030. idefloppy_do_end_request(drive, 1, 0);
  1031. return;
  1032. }
  1033. static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
  1034. {
  1035. debug_log(KERN_INFO "ide-floppy: creating prevent removal command, "
  1036. "prevent = %d\n", prevent);
  1037. idefloppy_init_pc(pc);
  1038. pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
  1039. pc->c[4] = prevent;
  1040. }
  1041. static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
  1042. {
  1043. idefloppy_init_pc(pc);
  1044. pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
  1045. pc->c[7] = 255;
  1046. pc->c[8] = 255;
  1047. pc->request_transfer = 255;
  1048. }
  1049. static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
  1050. int flags)
  1051. {
  1052. idefloppy_init_pc(pc);
  1053. pc->c[0] = IDEFLOPPY_FORMAT_UNIT_CMD;
  1054. pc->c[1] = 0x17;
  1055. memset(pc->buffer, 0, 12);
  1056. pc->buffer[1] = 0xA2;
  1057. /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
  1058. if (flags & 1) /* Verify bit on... */
  1059. pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
  1060. pc->buffer[3] = 8;
  1061. put_unaligned(htonl(b), (unsigned int *)(&pc->buffer[4]));
  1062. put_unaligned(htonl(l), (unsigned int *)(&pc->buffer[8]));
  1063. pc->buffer_size=12;
  1064. set_bit(PC_WRITING, &pc->flags);
  1065. }
  1066. /*
  1067. * A mode sense command is used to "sense" floppy parameters.
  1068. */
  1069. static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
  1070. {
  1071. u16 length = sizeof(idefloppy_mode_parameter_header_t);
  1072. idefloppy_init_pc(pc);
  1073. pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
  1074. pc->c[1] = 0;
  1075. pc->c[2] = page_code + (type << 6);
  1076. switch (page_code) {
  1077. case IDEFLOPPY_CAPABILITIES_PAGE:
  1078. length += 12;
  1079. break;
  1080. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  1081. length += 32;
  1082. break;
  1083. default:
  1084. printk(KERN_ERR "ide-floppy: unsupported page code "
  1085. "in create_mode_sense_cmd\n");
  1086. }
  1087. put_unaligned(htons(length), (u16 *) &pc->c[7]);
  1088. pc->request_transfer = length;
  1089. }
  1090. static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
  1091. {
  1092. idefloppy_init_pc(pc);
  1093. pc->c[0] = IDEFLOPPY_START_STOP_CMD;
  1094. pc->c[4] = start;
  1095. }
  1096. static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
  1097. {
  1098. idefloppy_init_pc(pc);
  1099. pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
  1100. }
  1101. static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
  1102. {
  1103. int block = sector / floppy->bs_factor;
  1104. int blocks = rq->nr_sectors / floppy->bs_factor;
  1105. int cmd = rq_data_dir(rq);
  1106. debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
  1107. 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
  1108. block, blocks);
  1109. idefloppy_init_pc(pc);
  1110. if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
  1111. pc->c[0] = cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
  1112. put_unaligned(htonl(blocks), (unsigned int *) &pc->c[6]);
  1113. } else {
  1114. pc->c[0] = cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
  1115. put_unaligned(htons(blocks), (unsigned short *) &pc->c[7]);
  1116. }
  1117. put_unaligned(htonl(block), (unsigned int *) &pc->c[2]);
  1118. pc->callback = &idefloppy_rw_callback;
  1119. pc->rq = rq;
  1120. pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
  1121. if (rq->flags & REQ_RW)
  1122. set_bit(PC_WRITING, &pc->flags);
  1123. pc->buffer = NULL;
  1124. pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
  1125. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  1126. }
  1127. static int
  1128. idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
  1129. {
  1130. /*
  1131. * just support eject for now, it would not be hard to make the
  1132. * REQ_BLOCK_PC support fully-featured
  1133. */
  1134. if (rq->cmd[0] != IDEFLOPPY_START_STOP_CMD)
  1135. return 1;
  1136. idefloppy_init_pc(pc);
  1137. memcpy(pc->c, rq->cmd, sizeof(pc->c));
  1138. return 0;
  1139. }
  1140. /*
  1141. * idefloppy_do_request is our request handling function.
  1142. */
  1143. static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
  1144. {
  1145. idefloppy_floppy_t *floppy = drive->driver_data;
  1146. idefloppy_pc_t *pc;
  1147. unsigned long block = (unsigned long)block_s;
  1148. debug_log(KERN_INFO "rq_status: %d, dev: %s, flags: %lx, errors: %d\n",
  1149. rq->rq_status,
  1150. rq->rq_disk ? rq->rq_disk->disk_name ? "?",
  1151. rq->flags, rq->errors);
  1152. debug_log(KERN_INFO "sector: %ld, nr_sectors: %ld, "
  1153. "current_nr_sectors: %d\n", (long)rq->sector,
  1154. rq->nr_sectors, rq->current_nr_sectors);
  1155. if (rq->errors >= ERROR_MAX) {
  1156. if (floppy->failed_pc != NULL) {
  1157. if (idefloppy_should_report_error(floppy))
  1158. printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
  1159. " key = %2x, asc = %2x, ascq = %2x\n",
  1160. drive->name, floppy->failed_pc->c[0],
  1161. floppy->sense_key, floppy->asc, floppy->ascq);
  1162. }
  1163. else
  1164. printk(KERN_ERR "ide-floppy: %s: I/O error\n",
  1165. drive->name);
  1166. idefloppy_do_end_request(drive, 0, 0);
  1167. return ide_stopped;
  1168. }
  1169. if (rq->flags & REQ_CMD) {
  1170. if (((long)rq->sector % floppy->bs_factor) ||
  1171. (rq->nr_sectors % floppy->bs_factor)) {
  1172. printk("%s: unsupported r/w request size\n",
  1173. drive->name);
  1174. idefloppy_do_end_request(drive, 0, 0);
  1175. return ide_stopped;
  1176. }
  1177. pc = idefloppy_next_pc_storage(drive);
  1178. idefloppy_create_rw_cmd(floppy, pc, rq, block);
  1179. } else if (rq->flags & REQ_SPECIAL) {
  1180. pc = (idefloppy_pc_t *) rq->buffer;
  1181. } else if (rq->flags & REQ_BLOCK_PC) {
  1182. pc = idefloppy_next_pc_storage(drive);
  1183. if (idefloppy_blockpc_cmd(floppy, pc, rq)) {
  1184. idefloppy_do_end_request(drive, 0, 0);
  1185. return ide_stopped;
  1186. }
  1187. } else {
  1188. blk_dump_rq_flags(rq,
  1189. "ide-floppy: unsupported command in queue");
  1190. idefloppy_do_end_request(drive, 0, 0);
  1191. return ide_stopped;
  1192. }
  1193. pc->rq = rq;
  1194. return idefloppy_issue_pc(drive, pc);
  1195. }
  1196. /*
  1197. * idefloppy_queue_pc_tail adds a special packet command request to the
  1198. * tail of the request queue, and waits for it to be serviced.
  1199. */
  1200. static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
  1201. {
  1202. struct ide_floppy_obj *floppy = drive->driver_data;
  1203. struct request rq;
  1204. ide_init_drive_cmd (&rq);
  1205. rq.buffer = (char *) pc;
  1206. rq.flags = REQ_SPECIAL; // rq.cmd = IDEFLOPPY_PC_RQ;
  1207. rq.rq_disk = floppy->disk;
  1208. return ide_do_drive_cmd(drive, &rq, ide_wait);
  1209. }
  1210. /*
  1211. * Look at the flexible disk page parameters. We will ignore the CHS
  1212. * capacity parameters and use the LBA parameters instead.
  1213. */
  1214. static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
  1215. {
  1216. idefloppy_floppy_t *floppy = drive->driver_data;
  1217. idefloppy_pc_t pc;
  1218. idefloppy_mode_parameter_header_t *header;
  1219. idefloppy_flexible_disk_page_t *page;
  1220. int capacity, lba_capacity;
  1221. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
  1222. if (idefloppy_queue_pc_tail(drive,&pc)) {
  1223. printk(KERN_ERR "ide-floppy: Can't get flexible disk "
  1224. "page parameters\n");
  1225. return 1;
  1226. }
  1227. header = (idefloppy_mode_parameter_header_t *) pc.buffer;
  1228. floppy->wp = header->wp;
  1229. set_disk_ro(floppy->disk, floppy->wp);
  1230. page = (idefloppy_flexible_disk_page_t *) (header + 1);
  1231. page->transfer_rate = ntohs(page->transfer_rate);
  1232. page->sector_size = ntohs(page->sector_size);
  1233. page->cyls = ntohs(page->cyls);
  1234. page->rpm = ntohs(page->rpm);
  1235. capacity = page->cyls * page->heads * page->sectors * page->sector_size;
  1236. if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
  1237. printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  1238. "%d sector size, %d rpm\n",
  1239. drive->name, capacity / 1024, page->cyls,
  1240. page->heads, page->sectors,
  1241. page->transfer_rate / 8, page->sector_size, page->rpm);
  1242. floppy->flexible_disk_page = *page;
  1243. drive->bios_cyl = page->cyls;
  1244. drive->bios_head = page->heads;
  1245. drive->bios_sect = page->sectors;
  1246. lba_capacity = floppy->blocks * floppy->block_size;
  1247. if (capacity < lba_capacity) {
  1248. printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
  1249. "bytes, but the drive only handles %d\n",
  1250. drive->name, lba_capacity, capacity);
  1251. floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
  1252. }
  1253. return 0;
  1254. }
  1255. static int idefloppy_get_capability_page(ide_drive_t *drive)
  1256. {
  1257. idefloppy_floppy_t *floppy = drive->driver_data;
  1258. idefloppy_pc_t pc;
  1259. idefloppy_mode_parameter_header_t *header;
  1260. idefloppy_capabilities_page_t *page;
  1261. floppy->srfp = 0;
  1262. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
  1263. MODE_SENSE_CURRENT);
  1264. set_bit(PC_SUPPRESS_ERROR, &pc.flags);
  1265. if (idefloppy_queue_pc_tail(drive,&pc)) {
  1266. return 1;
  1267. }
  1268. header = (idefloppy_mode_parameter_header_t *) pc.buffer;
  1269. page= (idefloppy_capabilities_page_t *)(header+1);
  1270. floppy->srfp = page->srfp;
  1271. return (0);
  1272. }
  1273. /*
  1274. * Determine if a media is present in the floppy drive, and if so,
  1275. * its LBA capacity.
  1276. */
  1277. static int idefloppy_get_capacity (ide_drive_t *drive)
  1278. {
  1279. idefloppy_floppy_t *floppy = drive->driver_data;
  1280. idefloppy_pc_t pc;
  1281. idefloppy_capacity_header_t *header;
  1282. idefloppy_capacity_descriptor_t *descriptor;
  1283. int i, descriptors, rc = 1, blocks, length;
  1284. drive->bios_cyl = 0;
  1285. drive->bios_head = drive->bios_sect = 0;
  1286. floppy->blocks = floppy->bs_factor = 0;
  1287. set_capacity(floppy->disk, 0);
  1288. idefloppy_create_read_capacity_cmd(&pc);
  1289. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1290. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  1291. return 1;
  1292. }
  1293. header = (idefloppy_capacity_header_t *) pc.buffer;
  1294. descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
  1295. descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
  1296. for (i = 0; i < descriptors; i++, descriptor++) {
  1297. blocks = descriptor->blocks = ntohl(descriptor->blocks);
  1298. length = descriptor->length = ntohs(descriptor->length);
  1299. if (!i)
  1300. {
  1301. switch (descriptor->dc) {
  1302. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  1303. case CAPACITY_UNFORMATTED:
  1304. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
  1305. /*
  1306. * If it is not a clik drive, break out
  1307. * (maintains previous driver behaviour)
  1308. */
  1309. break;
  1310. case CAPACITY_CURRENT:
  1311. /* Normal Zip/LS-120 disks */
  1312. if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
  1313. printk(KERN_INFO "%s: %dkB, %d blocks, %d "
  1314. "sector size\n", drive->name,
  1315. blocks * length / 1024, blocks, length);
  1316. floppy->capacity = *descriptor;
  1317. if (!length || length % 512) {
  1318. printk(KERN_NOTICE "%s: %d bytes block size "
  1319. "not supported\n", drive->name, length);
  1320. } else {
  1321. floppy->blocks = blocks;
  1322. floppy->block_size = length;
  1323. if ((floppy->bs_factor = length / 512) != 1)
  1324. printk(KERN_NOTICE "%s: warning: non "
  1325. "512 bytes block size not "
  1326. "fully supported\n",
  1327. drive->name);
  1328. rc = 0;
  1329. }
  1330. break;
  1331. case CAPACITY_NO_CARTRIDGE:
  1332. /*
  1333. * This is a KERN_ERR so it appears on screen
  1334. * for the user to see
  1335. */
  1336. printk(KERN_ERR "%s: No disk in drive\n", drive->name);
  1337. break;
  1338. case CAPACITY_INVALID:
  1339. printk(KERN_ERR "%s: Invalid capacity for disk "
  1340. "in drive\n", drive->name);
  1341. break;
  1342. }
  1343. }
  1344. if (!i) {
  1345. debug_log( "Descriptor 0 Code: %d\n",
  1346. descriptor->dc);
  1347. }
  1348. debug_log( "Descriptor %d: %dkB, %d blocks, %d "
  1349. "sector size\n", i, blocks * length / 1024, blocks,
  1350. length);
  1351. }
  1352. /* Clik! disk does not support get_flexible_disk_page */
  1353. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1354. (void) idefloppy_get_flexible_disk_page(drive);
  1355. }
  1356. set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
  1357. return rc;
  1358. }
  1359. /*
  1360. ** Obtain the list of formattable capacities.
  1361. ** Very similar to idefloppy_get_capacity, except that we push the capacity
  1362. ** descriptors to userland, instead of our own structures.
  1363. **
  1364. ** Userland gives us the following structure:
  1365. **
  1366. ** struct idefloppy_format_capacities {
  1367. ** int nformats;
  1368. ** struct {
  1369. ** int nblocks;
  1370. ** int blocksize;
  1371. ** } formats[];
  1372. ** } ;
  1373. **
  1374. ** userland initializes nformats to the number of allocated formats[]
  1375. ** records. On exit we set nformats to the number of records we've
  1376. ** actually initialized.
  1377. **
  1378. */
  1379. static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
  1380. {
  1381. idefloppy_pc_t pc;
  1382. idefloppy_capacity_header_t *header;
  1383. idefloppy_capacity_descriptor_t *descriptor;
  1384. int i, descriptors, blocks, length;
  1385. int u_array_size;
  1386. int u_index;
  1387. int __user *argp;
  1388. if (get_user(u_array_size, arg))
  1389. return (-EFAULT);
  1390. if (u_array_size <= 0)
  1391. return (-EINVAL);
  1392. idefloppy_create_read_capacity_cmd(&pc);
  1393. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1394. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  1395. return (-EIO);
  1396. }
  1397. header = (idefloppy_capacity_header_t *) pc.buffer;
  1398. descriptors = header->length /
  1399. sizeof(idefloppy_capacity_descriptor_t);
  1400. descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
  1401. u_index = 0;
  1402. argp = arg + 1;
  1403. /*
  1404. ** We always skip the first capacity descriptor. That's the
  1405. ** current capacity. We are interested in the remaining descriptors,
  1406. ** the formattable capacities.
  1407. */
  1408. for (i=0; i<descriptors; i++, descriptor++) {
  1409. if (u_index >= u_array_size)
  1410. break; /* User-supplied buffer too small */
  1411. if (i == 0)
  1412. continue; /* Skip the first descriptor */
  1413. blocks = ntohl(descriptor->blocks);
  1414. length = ntohs(descriptor->length);
  1415. if (put_user(blocks, argp))
  1416. return(-EFAULT);
  1417. ++argp;
  1418. if (put_user(length, argp))
  1419. return (-EFAULT);
  1420. ++argp;
  1421. ++u_index;
  1422. }
  1423. if (put_user(u_index, arg))
  1424. return (-EFAULT);
  1425. return (0);
  1426. }
  1427. /*
  1428. ** Send ATAPI_FORMAT_UNIT to the drive.
  1429. **
  1430. ** Userland gives us the following structure:
  1431. **
  1432. ** struct idefloppy_format_command {
  1433. ** int nblocks;
  1434. ** int blocksize;
  1435. ** int flags;
  1436. ** } ;
  1437. **
  1438. ** flags is a bitmask, currently, the only defined flag is:
  1439. **
  1440. ** 0x01 - verify media after format.
  1441. */
  1442. static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
  1443. {
  1444. int blocks;
  1445. int length;
  1446. int flags;
  1447. idefloppy_pc_t pc;
  1448. if (get_user(blocks, arg) ||
  1449. get_user(length, arg+1) ||
  1450. get_user(flags, arg+2)) {
  1451. return (-EFAULT);
  1452. }
  1453. /* Get the SFRP bit */
  1454. (void) idefloppy_get_capability_page(drive);
  1455. idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
  1456. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1457. return (-EIO);
  1458. }
  1459. return (0);
  1460. }
  1461. /*
  1462. ** Get ATAPI_FORMAT_UNIT progress indication.
  1463. **
  1464. ** Userland gives a pointer to an int. The int is set to a progresss
  1465. ** indicator 0-65536, with 65536=100%.
  1466. **
  1467. ** If the drive does not support format progress indication, we just check
  1468. ** the dsc bit, and return either 0 or 65536.
  1469. */
  1470. static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
  1471. {
  1472. idefloppy_floppy_t *floppy = drive->driver_data;
  1473. idefloppy_pc_t pc;
  1474. int progress_indication = 0x10000;
  1475. if (floppy->srfp) {
  1476. idefloppy_create_request_sense_cmd(&pc);
  1477. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1478. return (-EIO);
  1479. }
  1480. if (floppy->sense_key == 2 &&
  1481. floppy->asc == 4 &&
  1482. floppy->ascq == 4) {
  1483. progress_indication = floppy->progress_indication;
  1484. }
  1485. /* Else assume format_unit has finished, and we're
  1486. ** at 0x10000 */
  1487. } else {
  1488. atapi_status_t status;
  1489. unsigned long flags;
  1490. local_irq_save(flags);
  1491. status.all = HWIF(drive)->INB(IDE_STATUS_REG);
  1492. local_irq_restore(flags);
  1493. progress_indication = !status.b.dsc ? 0 : 0x10000;
  1494. }
  1495. if (put_user(progress_indication, arg))
  1496. return (-EFAULT);
  1497. return (0);
  1498. }
  1499. /*
  1500. * Return the current floppy capacity.
  1501. */
  1502. static sector_t idefloppy_capacity (ide_drive_t *drive)
  1503. {
  1504. idefloppy_floppy_t *floppy = drive->driver_data;
  1505. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  1506. return capacity;
  1507. }
  1508. /*
  1509. * idefloppy_identify_device checks if we can support a drive,
  1510. * based on the ATAPI IDENTIFY command results.
  1511. */
  1512. static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
  1513. {
  1514. struct idefloppy_id_gcw gcw;
  1515. #if IDEFLOPPY_DEBUG_INFO
  1516. u16 mask,i;
  1517. char buffer[80];
  1518. #endif /* IDEFLOPPY_DEBUG_INFO */
  1519. *((u16 *) &gcw) = id->config;
  1520. #ifdef CONFIG_PPC
  1521. /* kludge for Apple PowerBook internal zip */
  1522. if ((gcw.device_type == 5) &&
  1523. !strstr(id->model, "CD-ROM") &&
  1524. strstr(id->model, "ZIP"))
  1525. gcw.device_type = 0;
  1526. #endif
  1527. #if IDEFLOPPY_DEBUG_INFO
  1528. printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
  1529. switch (gcw.protocol) {
  1530. case 0: case 1: sprintf(buffer, "ATA");break;
  1531. case 2: sprintf(buffer, "ATAPI");break;
  1532. case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
  1533. }
  1534. printk(KERN_INFO "Protocol Type: %s\n", buffer);
  1535. switch (gcw.device_type) {
  1536. case 0: sprintf(buffer, "Direct-access Device");break;
  1537. case 1: sprintf(buffer, "Streaming Tape Device");break;
  1538. case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
  1539. case 5: sprintf(buffer, "CD-ROM Device");break;
  1540. case 6: sprintf(buffer, "Reserved");
  1541. case 7: sprintf(buffer, "Optical memory Device");break;
  1542. case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
  1543. default: sprintf(buffer, "Reserved");
  1544. }
  1545. printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
  1546. printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
  1547. switch (gcw.drq_type) {
  1548. case 0: sprintf(buffer, "Microprocessor DRQ");break;
  1549. case 1: sprintf(buffer, "Interrupt DRQ");break;
  1550. case 2: sprintf(buffer, "Accelerated DRQ");break;
  1551. case 3: sprintf(buffer, "Reserved");break;
  1552. }
  1553. printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
  1554. switch (gcw.packet_size) {
  1555. case 0: sprintf(buffer, "12 bytes");break;
  1556. case 1: sprintf(buffer, "16 bytes");break;
  1557. default: sprintf(buffer, "Reserved");break;
  1558. }
  1559. printk(KERN_INFO "Command Packet Size: %s\n", buffer);
  1560. printk(KERN_INFO "Model: %.40s\n",id->model);
  1561. printk(KERN_INFO "Firmware Revision: %.8s\n",id->fw_rev);
  1562. printk(KERN_INFO "Serial Number: %.20s\n",id->serial_no);
  1563. printk(KERN_INFO "Write buffer size(?): %d bytes\n",id->buf_size*512);
  1564. printk(KERN_INFO "DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
  1565. printk(KERN_INFO "LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
  1566. printk(KERN_INFO "IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
  1567. printk(KERN_INFO "IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
  1568. printk(KERN_INFO "ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
  1569. printk(KERN_INFO "PIO Cycle Timing Category: %d\n",id->tPIO);
  1570. printk(KERN_INFO "DMA Cycle Timing Category: %d\n",id->tDMA);
  1571. printk(KERN_INFO "Single Word DMA supported modes:\n");
  1572. for (i=0,mask=1;i<8;i++,mask=mask << 1) {
  1573. if (id->dma_1word & mask)
  1574. printk(KERN_INFO " Mode %d%s\n", i,
  1575. (id->dma_1word & (mask << 8)) ? " (active)" : "");
  1576. }
  1577. printk(KERN_INFO "Multi Word DMA supported modes:\n");
  1578. for (i=0,mask=1;i<8;i++,mask=mask << 1) {
  1579. if (id->dma_mword & mask)
  1580. printk(KERN_INFO " Mode %d%s\n", i,
  1581. (id->dma_mword & (mask << 8)) ? " (active)" : "");
  1582. }
  1583. if (id->field_valid & 0x0002) {
  1584. printk(KERN_INFO "Enhanced PIO Modes: %s\n",
  1585. id->eide_pio_modes & 1 ? "Mode 3":"None");
  1586. if (id->eide_dma_min == 0)
  1587. sprintf(buffer, "Not supported");
  1588. else
  1589. sprintf(buffer, "%d ns",id->eide_dma_min);
  1590. printk(KERN_INFO "Minimum Multi-word DMA cycle per word: %s\n", buffer);
  1591. if (id->eide_dma_time == 0)
  1592. sprintf(buffer, "Not supported");
  1593. else
  1594. sprintf(buffer, "%d ns",id->eide_dma_time);
  1595. printk(KERN_INFO "Manufacturer\'s Recommended Multi-word cycle: %s\n", buffer);
  1596. if (id->eide_pio == 0)
  1597. sprintf(buffer, "Not supported");
  1598. else
  1599. sprintf(buffer, "%d ns",id->eide_pio);
  1600. printk(KERN_INFO "Minimum PIO cycle without IORDY: %s\n",
  1601. buffer);
  1602. if (id->eide_pio_iordy == 0)
  1603. sprintf(buffer, "Not supported");
  1604. else
  1605. sprintf(buffer, "%d ns",id->eide_pio_iordy);
  1606. printk(KERN_INFO "Minimum PIO cycle with IORDY: %s\n", buffer);
  1607. } else
  1608. printk(KERN_INFO "According to the device, fields 64-70 are not valid.\n");
  1609. #endif /* IDEFLOPPY_DEBUG_INFO */
  1610. if (gcw.protocol != 2)
  1611. printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
  1612. else if (gcw.device_type != 0)
  1613. printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
  1614. else if (!gcw.removable)
  1615. printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
  1616. else if (gcw.drq_type == 3) {
  1617. printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
  1618. } else if (gcw.packet_size != 0) {
  1619. printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
  1620. } else
  1621. return 1;
  1622. return 0;
  1623. }
  1624. static void idefloppy_add_settings(ide_drive_t *drive)
  1625. {
  1626. idefloppy_floppy_t *floppy = drive->driver_data;
  1627. /*
  1628. * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
  1629. */
  1630. ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  1631. ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  1632. ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  1633. ide_add_setting(drive, "ticks", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
  1634. }
  1635. /*
  1636. * Driver initialization.
  1637. */
  1638. static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
  1639. {
  1640. struct idefloppy_id_gcw gcw;
  1641. *((u16 *) &gcw) = drive->id->config;
  1642. floppy->pc = floppy->pc_stack;
  1643. if (gcw.drq_type == 1)
  1644. set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
  1645. /*
  1646. * We used to check revisions here. At this point however
  1647. * I'm giving up. Just assume they are all broken, its easier.
  1648. *
  1649. * The actual reason for the workarounds was likely
  1650. * a driver bug after all rather than a firmware bug,
  1651. * and the workaround below used to hide it. It should
  1652. * be fixed as of version 1.9, but to be on the safe side
  1653. * we'll leave the limitation below for the 2.2.x tree.
  1654. */
  1655. if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
  1656. set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
  1657. /* This value will be visible in the /proc/ide/hdx/settings */
  1658. floppy->ticks = IDEFLOPPY_TICKS_DELAY;
  1659. blk_queue_max_sectors(drive->queue, 64);
  1660. }
  1661. /*
  1662. * Guess what? The IOMEGA Clik! drive also needs the
  1663. * above fix. It makes nasty clicking noises without
  1664. * it, so please don't remove this.
  1665. */
  1666. if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
  1667. blk_queue_max_sectors(drive->queue, 64);
  1668. set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
  1669. }
  1670. (void) idefloppy_get_capacity(drive);
  1671. idefloppy_add_settings(drive);
  1672. }
  1673. static int ide_floppy_remove(struct device *dev)
  1674. {
  1675. ide_drive_t *drive = to_ide_device(dev);
  1676. idefloppy_floppy_t *floppy = drive->driver_data;
  1677. struct gendisk *g = floppy->disk;
  1678. ide_unregister_subdriver(drive, floppy->driver);
  1679. del_gendisk(g);
  1680. ide_floppy_put(floppy);
  1681. return 0;
  1682. }
  1683. static void ide_floppy_release(struct kref *kref)
  1684. {
  1685. struct ide_floppy_obj *floppy = to_ide_floppy(kref);
  1686. ide_drive_t *drive = floppy->drive;
  1687. struct gendisk *g = floppy->disk;
  1688. drive->driver_data = NULL;
  1689. g->private_data = NULL;
  1690. put_disk(g);
  1691. kfree(floppy);
  1692. }
  1693. #ifdef CONFIG_PROC_FS
  1694. static int proc_idefloppy_read_capacity
  1695. (char *page, char **start, off_t off, int count, int *eof, void *data)
  1696. {
  1697. ide_drive_t*drive = (ide_drive_t *)data;
  1698. int len;
  1699. len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
  1700. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  1701. }
  1702. static ide_proc_entry_t idefloppy_proc[] = {
  1703. { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
  1704. { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
  1705. { NULL, 0, NULL, NULL }
  1706. };
  1707. #else
  1708. #define idefloppy_proc NULL
  1709. #endif /* CONFIG_PROC_FS */
  1710. static int ide_floppy_probe(struct device *);
  1711. static ide_driver_t idefloppy_driver = {
  1712. .owner = THIS_MODULE,
  1713. .gen_driver = {
  1714. .name = "ide-floppy",
  1715. .bus = &ide_bus_type,
  1716. .probe = ide_floppy_probe,
  1717. .remove = ide_floppy_remove,
  1718. },
  1719. .version = IDEFLOPPY_VERSION,
  1720. .media = ide_floppy,
  1721. .supports_dsc_overlap = 0,
  1722. .do_request = idefloppy_do_request,
  1723. .end_request = idefloppy_do_end_request,
  1724. .error = __ide_error,
  1725. .abort = __ide_abort,
  1726. .proc = idefloppy_proc,
  1727. };
  1728. static int idefloppy_open(struct inode *inode, struct file *filp)
  1729. {
  1730. struct gendisk *disk = inode->i_bdev->bd_disk;
  1731. struct ide_floppy_obj *floppy;
  1732. ide_drive_t *drive;
  1733. idefloppy_pc_t pc;
  1734. int ret = 0;
  1735. debug_log(KERN_INFO "Reached idefloppy_open\n");
  1736. if (!(floppy = ide_floppy_get(disk)))
  1737. return -ENXIO;
  1738. drive = floppy->drive;
  1739. drive->usage++;
  1740. if (drive->usage == 1) {
  1741. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1742. /* Just in case */
  1743. idefloppy_create_test_unit_ready_cmd(&pc);
  1744. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1745. idefloppy_create_start_stop_cmd(&pc, 1);
  1746. (void) idefloppy_queue_pc_tail(drive, &pc);
  1747. }
  1748. if (idefloppy_get_capacity (drive)
  1749. && (filp->f_flags & O_NDELAY) == 0
  1750. /*
  1751. ** Allow O_NDELAY to open a drive without a disk, or with
  1752. ** an unreadable disk, so that we can get the format
  1753. ** capacity of the drive or begin the format - Sam
  1754. */
  1755. ) {
  1756. drive->usage--;
  1757. ret = -EIO;
  1758. goto out_put_floppy;
  1759. }
  1760. if (floppy->wp && (filp->f_mode & 2)) {
  1761. drive->usage--;
  1762. ret = -EROFS;
  1763. goto out_put_floppy;
  1764. }
  1765. set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
  1766. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1767. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1768. idefloppy_create_prevent_cmd(&pc, 1);
  1769. (void) idefloppy_queue_pc_tail(drive, &pc);
  1770. }
  1771. check_disk_change(inode->i_bdev);
  1772. } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
  1773. drive->usage--;
  1774. ret = -EBUSY;
  1775. goto out_put_floppy;
  1776. }
  1777. return 0;
  1778. out_put_floppy:
  1779. ide_floppy_put(floppy);
  1780. return ret;
  1781. }
  1782. static int idefloppy_release(struct inode *inode, struct file *filp)
  1783. {
  1784. struct gendisk *disk = inode->i_bdev->bd_disk;
  1785. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1786. ide_drive_t *drive = floppy->drive;
  1787. idefloppy_pc_t pc;
  1788. debug_log(KERN_INFO "Reached idefloppy_release\n");
  1789. if (drive->usage == 1) {
  1790. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1791. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1792. idefloppy_create_prevent_cmd(&pc, 0);
  1793. (void) idefloppy_queue_pc_tail(drive, &pc);
  1794. }
  1795. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1796. }
  1797. drive->usage--;
  1798. ide_floppy_put(floppy);
  1799. return 0;
  1800. }
  1801. static int idefloppy_ioctl(struct inode *inode, struct file *file,
  1802. unsigned int cmd, unsigned long arg)
  1803. {
  1804. struct block_device *bdev = inode->i_bdev;
  1805. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1806. ide_drive_t *drive = floppy->drive;
  1807. void __user *argp = (void __user *)arg;
  1808. int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  1809. int prevent = (arg) ? 1 : 0;
  1810. idefloppy_pc_t pc;
  1811. if (err != -EINVAL)
  1812. return err;
  1813. switch (cmd) {
  1814. case CDROMEJECT:
  1815. prevent = 0;
  1816. /* fall through */
  1817. case CDROM_LOCKDOOR:
  1818. if (drive->usage > 1)
  1819. return -EBUSY;
  1820. /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
  1821. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1822. idefloppy_create_prevent_cmd(&pc, prevent);
  1823. (void) idefloppy_queue_pc_tail(drive, &pc);
  1824. }
  1825. if (cmd == CDROMEJECT) {
  1826. idefloppy_create_start_stop_cmd(&pc, 2);
  1827. (void) idefloppy_queue_pc_tail(drive, &pc);
  1828. }
  1829. return 0;
  1830. case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
  1831. return 0;
  1832. case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
  1833. return idefloppy_get_format_capacities(drive, argp);
  1834. case IDEFLOPPY_IOCTL_FORMAT_START:
  1835. if (!(file->f_mode & 2))
  1836. return -EPERM;
  1837. if (drive->usage > 1) {
  1838. /* Don't format if someone is using the disk */
  1839. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
  1840. &floppy->flags);
  1841. return -EBUSY;
  1842. }
  1843. set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1844. err = idefloppy_begin_format(drive, argp);
  1845. if (err)
  1846. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1847. return err;
  1848. /*
  1849. ** Note, the bit will be cleared when the device is
  1850. ** closed. This is the cleanest way to handle the
  1851. ** situation where the drive does not support
  1852. ** format progress reporting.
  1853. */
  1854. case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
  1855. return idefloppy_get_format_progress(drive, argp);
  1856. }
  1857. return -EINVAL;
  1858. }
  1859. static int idefloppy_media_changed(struct gendisk *disk)
  1860. {
  1861. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1862. ide_drive_t *drive = floppy->drive;
  1863. /* do not scan partitions twice if this is a removable device */
  1864. if (drive->attach) {
  1865. drive->attach = 0;
  1866. return 0;
  1867. }
  1868. return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
  1869. }
  1870. static int idefloppy_revalidate_disk(struct gendisk *disk)
  1871. {
  1872. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1873. set_capacity(disk, idefloppy_capacity(floppy->drive));
  1874. return 0;
  1875. }
  1876. static struct block_device_operations idefloppy_ops = {
  1877. .owner = THIS_MODULE,
  1878. .open = idefloppy_open,
  1879. .release = idefloppy_release,
  1880. .ioctl = idefloppy_ioctl,
  1881. .media_changed = idefloppy_media_changed,
  1882. .revalidate_disk= idefloppy_revalidate_disk
  1883. };
  1884. static int ide_floppy_probe(struct device *dev)
  1885. {
  1886. ide_drive_t *drive = to_ide_device(dev);
  1887. idefloppy_floppy_t *floppy;
  1888. struct gendisk *g;
  1889. if (!strstr("ide-floppy", drive->driver_req))
  1890. goto failed;
  1891. if (!drive->present)
  1892. goto failed;
  1893. if (drive->media != ide_floppy)
  1894. goto failed;
  1895. if (!idefloppy_identify_device (drive, drive->id)) {
  1896. printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
  1897. goto failed;
  1898. }
  1899. if (drive->scsi) {
  1900. printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
  1901. goto failed;
  1902. }
  1903. if ((floppy = (idefloppy_floppy_t *) kmalloc (sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
  1904. printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
  1905. goto failed;
  1906. }
  1907. g = alloc_disk(1 << PARTN_BITS);
  1908. if (!g)
  1909. goto out_free_floppy;
  1910. ide_init_disk(g, drive);
  1911. ide_register_subdriver(drive, &idefloppy_driver);
  1912. memset(floppy, 0, sizeof(*floppy));
  1913. kref_init(&floppy->kref);
  1914. floppy->drive = drive;
  1915. floppy->driver = &idefloppy_driver;
  1916. floppy->disk = g;
  1917. g->private_data = &floppy->driver;
  1918. drive->driver_data = floppy;
  1919. idefloppy_setup (drive, floppy);
  1920. g->minors = 1 << PARTN_BITS;
  1921. g->driverfs_dev = &drive->gendev;
  1922. strcpy(g->devfs_name, drive->devfs_name);
  1923. g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
  1924. g->fops = &idefloppy_ops;
  1925. drive->attach = 1;
  1926. add_disk(g);
  1927. return 0;
  1928. out_free_floppy:
  1929. kfree(floppy);
  1930. failed:
  1931. return -ENODEV;
  1932. }
  1933. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
  1934. static void __exit idefloppy_exit (void)
  1935. {
  1936. driver_unregister(&idefloppy_driver.gen_driver);
  1937. }
  1938. /*
  1939. * idefloppy_init will register the driver for each floppy.
  1940. */
  1941. static int idefloppy_init (void)
  1942. {
  1943. printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
  1944. return driver_register(&idefloppy_driver.gen_driver);
  1945. }
  1946. module_init(idefloppy_init);
  1947. module_exit(idefloppy_exit);
  1948. MODULE_LICENSE("GPL");