ide-floppy.c 65 KB

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