ide-floppy.c 67 KB

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