ide-floppy.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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. atapi_bcount_t bcount;
  706. atapi_ireason_t ireason;
  707. idefloppy_pc_t *pc = floppy->pc;
  708. struct request *rq = pc->rq;
  709. unsigned int temp;
  710. u8 stat;
  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.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  759. bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  760. /* on this interrupt */
  761. ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
  762. if (ireason.b.cod) {
  763. printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
  764. return ide_do_reset(drive);
  765. }
  766. if (ireason.b.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.b.io ? "Write":"Read");
  770. printk(KERN_ERR "but the floppy wants us to %s !\n",
  771. ireason.b.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.all;
  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.all);
  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(drive)->atapi_output_bytes(drive,
  799. pc->current_position,
  800. bcount.all);
  801. else
  802. idefloppy_output_buffers(drive, pc, bcount.all);
  803. } else {
  804. if (pc->buffer != NULL)
  805. /* Read the current buffer */
  806. HWIF(drive)->atapi_input_bytes(drive,
  807. pc->current_position,
  808. bcount.all);
  809. else
  810. idefloppy_input_buffers(drive, pc, bcount.all);
  811. }
  812. /* Update the current position */
  813. pc->actually_transferred += bcount.all;
  814. pc->current_position += bcount.all;
  815. BUG_ON(HWGROUP(drive)->handler != NULL);
  816. ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
  817. return ide_started;
  818. }
  819. /*
  820. * This is the original routine that did the packet transfer.
  821. * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
  822. * for that drive below. The algorithm is chosen based on drive type
  823. */
  824. static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
  825. {
  826. ide_startstop_t startstop;
  827. idefloppy_floppy_t *floppy = drive->driver_data;
  828. atapi_ireason_t ireason;
  829. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  830. printk(KERN_ERR "ide-floppy: Strange, packet command "
  831. "initiated yet DRQ isn't asserted\n");
  832. return startstop;
  833. }
  834. ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
  835. if (!ireason.b.cod || ireason.b.io) {
  836. printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
  837. "issuing a packet command\n");
  838. return ide_do_reset(drive);
  839. }
  840. BUG_ON(HWGROUP(drive)->handler != NULL);
  841. /* Set the interrupt routine */
  842. ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
  843. /* Send the actual packet */
  844. HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
  845. return ide_started;
  846. }
  847. /*
  848. * What we have here is a classic case of a top half / bottom half
  849. * interrupt service routine. In interrupt mode, the device sends
  850. * an interrupt to signal it's ready to receive a packet. However,
  851. * we need to delay about 2-3 ticks before issuing the packet or we
  852. * gets in trouble.
  853. *
  854. * So, follow carefully. transfer_pc1 is called as an interrupt (or
  855. * directly). In either case, when the device says it's ready for a
  856. * packet, we schedule the packet transfer to occur about 2-3 ticks
  857. * later in transfer_pc2.
  858. */
  859. static int idefloppy_transfer_pc2 (ide_drive_t *drive)
  860. {
  861. idefloppy_floppy_t *floppy = drive->driver_data;
  862. /* Send the actual packet */
  863. HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
  864. /* Timeout for the packet command */
  865. return IDEFLOPPY_WAIT_CMD;
  866. }
  867. static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
  868. {
  869. idefloppy_floppy_t *floppy = drive->driver_data;
  870. ide_startstop_t startstop;
  871. atapi_ireason_t ireason;
  872. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  873. printk(KERN_ERR "ide-floppy: Strange, packet command "
  874. "initiated yet DRQ isn't asserted\n");
  875. return startstop;
  876. }
  877. ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
  878. if (!ireason.b.cod || ireason.b.io) {
  879. printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
  880. "while issuing a packet command\n");
  881. return ide_do_reset(drive);
  882. }
  883. /*
  884. * The following delay solves a problem with ATAPI Zip 100 drives
  885. * where the Busy flag was apparently being deasserted before the
  886. * unit was ready to receive data. This was happening on a
  887. * 1200 MHz Athlon system. 10/26/01 25msec is too short,
  888. * 40 and 50msec work well. idefloppy_pc_intr will not be actually
  889. * used until after the packet is moved in about 50 msec.
  890. */
  891. BUG_ON(HWGROUP(drive)->handler != NULL);
  892. ide_set_handler(drive,
  893. &idefloppy_pc_intr, /* service routine for packet command */
  894. floppy->ticks, /* wait this long before "failing" */
  895. &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
  896. return ide_started;
  897. }
  898. /**
  899. * idefloppy_should_report_error()
  900. *
  901. * Supresses error messages resulting from Medium not present
  902. */
  903. static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
  904. {
  905. if (floppy->sense_key == 0x02 &&
  906. floppy->asc == 0x3a &&
  907. floppy->ascq == 0x00)
  908. return 0;
  909. return 1;
  910. }
  911. /*
  912. * Issue a packet command
  913. */
  914. static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
  915. {
  916. idefloppy_floppy_t *floppy = drive->driver_data;
  917. ide_hwif_t *hwif = drive->hwif;
  918. atapi_bcount_t bcount;
  919. ide_handler_t *pkt_xfer_routine;
  920. u8 dma;
  921. if (floppy->failed_pc == NULL &&
  922. pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
  923. floppy->failed_pc = pc;
  924. /* Set the current packet command */
  925. floppy->pc = pc;
  926. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
  927. test_bit(PC_ABORT, &pc->flags)) {
  928. /*
  929. * We will "abort" retrying a packet command in case
  930. * a legitimate error code was received.
  931. */
  932. if (!test_bit(PC_ABORT, &pc->flags)) {
  933. if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
  934. if (idefloppy_should_report_error(floppy))
  935. printk(KERN_ERR "ide-floppy: %s: I/O error, "
  936. "pc = %2x, key = %2x, "
  937. "asc = %2x, ascq = %2x\n",
  938. drive->name, pc->c[0],
  939. floppy->sense_key,
  940. floppy->asc, floppy->ascq);
  941. }
  942. /* Giving up */
  943. pc->error = IDEFLOPPY_ERROR_GENERAL;
  944. }
  945. floppy->failed_pc = NULL;
  946. pc->callback(drive);
  947. return ide_stopped;
  948. }
  949. debug_log(KERN_INFO "Retry number - %d\n",pc->retries);
  950. pc->retries++;
  951. /* We haven't transferred any data yet */
  952. pc->actually_transferred = 0;
  953. pc->current_position = pc->buffer;
  954. bcount.all = min(pc->request_transfer, 63 * 1024);
  955. if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
  956. ide_dma_off(drive);
  957. dma = 0;
  958. if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
  959. dma = !hwif->dma_setup(drive);
  960. if (IDE_CONTROL_REG)
  961. HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
  962. /* Use PIO/DMA */
  963. hwif->OUTB(dma, IDE_FEATURE_REG);
  964. HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
  965. HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
  966. HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
  967. if (dma) { /* Begin DMA, if necessary */
  968. set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  969. hwif->dma_start(drive);
  970. }
  971. /* Can we transfer the packet when we get the interrupt or wait? */
  972. if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
  973. /* wait */
  974. pkt_xfer_routine = &idefloppy_transfer_pc1;
  975. } else {
  976. /* immediate */
  977. pkt_xfer_routine = &idefloppy_transfer_pc;
  978. }
  979. if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
  980. /* Issue the packet command */
  981. ide_execute_command(drive, WIN_PACKETCMD,
  982. pkt_xfer_routine,
  983. IDEFLOPPY_WAIT_CMD,
  984. NULL);
  985. return ide_started;
  986. } else {
  987. /* Issue the packet command */
  988. HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  989. return (*pkt_xfer_routine) (drive);
  990. }
  991. }
  992. static void idefloppy_rw_callback (ide_drive_t *drive)
  993. {
  994. debug_log(KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
  995. idefloppy_do_end_request(drive, 1, 0);
  996. return;
  997. }
  998. static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
  999. {
  1000. debug_log(KERN_INFO "ide-floppy: creating prevent removal command, "
  1001. "prevent = %d\n", prevent);
  1002. idefloppy_init_pc(pc);
  1003. pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
  1004. pc->c[4] = prevent;
  1005. }
  1006. static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
  1007. {
  1008. idefloppy_init_pc(pc);
  1009. pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
  1010. pc->c[7] = 255;
  1011. pc->c[8] = 255;
  1012. pc->request_transfer = 255;
  1013. }
  1014. static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
  1015. int flags)
  1016. {
  1017. idefloppy_init_pc(pc);
  1018. pc->c[0] = IDEFLOPPY_FORMAT_UNIT_CMD;
  1019. pc->c[1] = 0x17;
  1020. memset(pc->buffer, 0, 12);
  1021. pc->buffer[1] = 0xA2;
  1022. /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
  1023. if (flags & 1) /* Verify bit on... */
  1024. pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
  1025. pc->buffer[3] = 8;
  1026. put_unaligned(htonl(b), (unsigned int *)(&pc->buffer[4]));
  1027. put_unaligned(htonl(l), (unsigned int *)(&pc->buffer[8]));
  1028. pc->buffer_size=12;
  1029. set_bit(PC_WRITING, &pc->flags);
  1030. }
  1031. /*
  1032. * A mode sense command is used to "sense" floppy parameters.
  1033. */
  1034. static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
  1035. {
  1036. u16 length = sizeof(idefloppy_mode_parameter_header_t);
  1037. idefloppy_init_pc(pc);
  1038. pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
  1039. pc->c[1] = 0;
  1040. pc->c[2] = page_code + (type << 6);
  1041. switch (page_code) {
  1042. case IDEFLOPPY_CAPABILITIES_PAGE:
  1043. length += 12;
  1044. break;
  1045. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  1046. length += 32;
  1047. break;
  1048. default:
  1049. printk(KERN_ERR "ide-floppy: unsupported page code "
  1050. "in create_mode_sense_cmd\n");
  1051. }
  1052. put_unaligned(htons(length), (u16 *) &pc->c[7]);
  1053. pc->request_transfer = length;
  1054. }
  1055. static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
  1056. {
  1057. idefloppy_init_pc(pc);
  1058. pc->c[0] = IDEFLOPPY_START_STOP_CMD;
  1059. pc->c[4] = start;
  1060. }
  1061. static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
  1062. {
  1063. idefloppy_init_pc(pc);
  1064. pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
  1065. }
  1066. static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
  1067. {
  1068. int block = sector / floppy->bs_factor;
  1069. int blocks = rq->nr_sectors / floppy->bs_factor;
  1070. int cmd = rq_data_dir(rq);
  1071. debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
  1072. 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
  1073. block, blocks);
  1074. idefloppy_init_pc(pc);
  1075. if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
  1076. pc->c[0] = cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
  1077. put_unaligned(htonl(blocks), (unsigned int *) &pc->c[6]);
  1078. } else {
  1079. pc->c[0] = cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
  1080. put_unaligned(htons(blocks), (unsigned short *) &pc->c[7]);
  1081. }
  1082. put_unaligned(htonl(block), (unsigned int *) &pc->c[2]);
  1083. pc->callback = &idefloppy_rw_callback;
  1084. pc->rq = rq;
  1085. pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
  1086. if (rq->cmd_flags & REQ_RW)
  1087. set_bit(PC_WRITING, &pc->flags);
  1088. pc->buffer = NULL;
  1089. pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
  1090. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  1091. }
  1092. static void
  1093. idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
  1094. {
  1095. idefloppy_init_pc(pc);
  1096. pc->callback = &idefloppy_rw_callback;
  1097. memcpy(pc->c, rq->cmd, sizeof(pc->c));
  1098. pc->rq = rq;
  1099. pc->b_count = rq->data_len;
  1100. if (rq->data_len && rq_data_dir(rq) == WRITE)
  1101. set_bit(PC_WRITING, &pc->flags);
  1102. pc->buffer = rq->data;
  1103. if (rq->bio)
  1104. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  1105. /*
  1106. * possibly problematic, doesn't look like ide-floppy correctly
  1107. * handled scattered requests if dma fails...
  1108. */
  1109. pc->request_transfer = pc->buffer_size = rq->data_len;
  1110. }
  1111. /*
  1112. * idefloppy_do_request is our request handling function.
  1113. */
  1114. static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
  1115. {
  1116. idefloppy_floppy_t *floppy = drive->driver_data;
  1117. idefloppy_pc_t *pc;
  1118. unsigned long block = (unsigned long)block_s;
  1119. debug_log(KERN_INFO "dev: %s, flags: %lx, errors: %d\n",
  1120. rq->rq_disk ? rq->rq_disk->disk_name : "?",
  1121. rq->flags, rq->errors);
  1122. debug_log(KERN_INFO "sector: %ld, nr_sectors: %ld, "
  1123. "current_nr_sectors: %d\n", (long)rq->sector,
  1124. rq->nr_sectors, rq->current_nr_sectors);
  1125. if (rq->errors >= ERROR_MAX) {
  1126. if (floppy->failed_pc != NULL) {
  1127. if (idefloppy_should_report_error(floppy))
  1128. printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
  1129. " key = %2x, asc = %2x, ascq = %2x\n",
  1130. drive->name, floppy->failed_pc->c[0],
  1131. floppy->sense_key, floppy->asc, floppy->ascq);
  1132. }
  1133. else
  1134. printk(KERN_ERR "ide-floppy: %s: I/O error\n",
  1135. drive->name);
  1136. idefloppy_do_end_request(drive, 0, 0);
  1137. return ide_stopped;
  1138. }
  1139. if (blk_fs_request(rq)) {
  1140. if (((long)rq->sector % floppy->bs_factor) ||
  1141. (rq->nr_sectors % floppy->bs_factor)) {
  1142. printk("%s: unsupported r/w request size\n",
  1143. drive->name);
  1144. idefloppy_do_end_request(drive, 0, 0);
  1145. return ide_stopped;
  1146. }
  1147. pc = idefloppy_next_pc_storage(drive);
  1148. idefloppy_create_rw_cmd(floppy, pc, rq, block);
  1149. } else if (blk_special_request(rq)) {
  1150. pc = (idefloppy_pc_t *) rq->buffer;
  1151. } else if (blk_pc_request(rq)) {
  1152. pc = idefloppy_next_pc_storage(drive);
  1153. idefloppy_blockpc_cmd(floppy, pc, rq);
  1154. } else {
  1155. blk_dump_rq_flags(rq,
  1156. "ide-floppy: unsupported command in queue");
  1157. idefloppy_do_end_request(drive, 0, 0);
  1158. return ide_stopped;
  1159. }
  1160. pc->rq = rq;
  1161. return idefloppy_issue_pc(drive, pc);
  1162. }
  1163. /*
  1164. * idefloppy_queue_pc_tail adds a special packet command request to the
  1165. * tail of the request queue, and waits for it to be serviced.
  1166. */
  1167. static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
  1168. {
  1169. struct ide_floppy_obj *floppy = drive->driver_data;
  1170. struct request rq;
  1171. ide_init_drive_cmd (&rq);
  1172. rq.buffer = (char *) pc;
  1173. rq.cmd_type = REQ_TYPE_SPECIAL;
  1174. rq.rq_disk = floppy->disk;
  1175. return ide_do_drive_cmd(drive, &rq, ide_wait);
  1176. }
  1177. /*
  1178. * Look at the flexible disk page parameters. We will ignore the CHS
  1179. * capacity parameters and use the LBA parameters instead.
  1180. */
  1181. static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
  1182. {
  1183. idefloppy_floppy_t *floppy = drive->driver_data;
  1184. idefloppy_pc_t pc;
  1185. idefloppy_mode_parameter_header_t *header;
  1186. idefloppy_flexible_disk_page_t *page;
  1187. int capacity, lba_capacity;
  1188. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
  1189. if (idefloppy_queue_pc_tail(drive,&pc)) {
  1190. printk(KERN_ERR "ide-floppy: Can't get flexible disk "
  1191. "page parameters\n");
  1192. return 1;
  1193. }
  1194. header = (idefloppy_mode_parameter_header_t *) pc.buffer;
  1195. floppy->wp = header->wp;
  1196. set_disk_ro(floppy->disk, floppy->wp);
  1197. page = (idefloppy_flexible_disk_page_t *) (header + 1);
  1198. page->transfer_rate = ntohs(page->transfer_rate);
  1199. page->sector_size = ntohs(page->sector_size);
  1200. page->cyls = ntohs(page->cyls);
  1201. page->rpm = ntohs(page->rpm);
  1202. capacity = page->cyls * page->heads * page->sectors * page->sector_size;
  1203. if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
  1204. printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  1205. "%d sector size, %d rpm\n",
  1206. drive->name, capacity / 1024, page->cyls,
  1207. page->heads, page->sectors,
  1208. page->transfer_rate / 8, page->sector_size, page->rpm);
  1209. floppy->flexible_disk_page = *page;
  1210. drive->bios_cyl = page->cyls;
  1211. drive->bios_head = page->heads;
  1212. drive->bios_sect = page->sectors;
  1213. lba_capacity = floppy->blocks * floppy->block_size;
  1214. if (capacity < lba_capacity) {
  1215. printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
  1216. "bytes, but the drive only handles %d\n",
  1217. drive->name, lba_capacity, capacity);
  1218. floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
  1219. }
  1220. return 0;
  1221. }
  1222. static int idefloppy_get_capability_page(ide_drive_t *drive)
  1223. {
  1224. idefloppy_floppy_t *floppy = drive->driver_data;
  1225. idefloppy_pc_t pc;
  1226. idefloppy_mode_parameter_header_t *header;
  1227. idefloppy_capabilities_page_t *page;
  1228. floppy->srfp = 0;
  1229. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
  1230. MODE_SENSE_CURRENT);
  1231. set_bit(PC_SUPPRESS_ERROR, &pc.flags);
  1232. if (idefloppy_queue_pc_tail(drive,&pc)) {
  1233. return 1;
  1234. }
  1235. header = (idefloppy_mode_parameter_header_t *) pc.buffer;
  1236. page= (idefloppy_capabilities_page_t *)(header+1);
  1237. floppy->srfp = page->srfp;
  1238. return (0);
  1239. }
  1240. /*
  1241. * Determine if a media is present in the floppy drive, and if so,
  1242. * its LBA capacity.
  1243. */
  1244. static int idefloppy_get_capacity (ide_drive_t *drive)
  1245. {
  1246. idefloppy_floppy_t *floppy = drive->driver_data;
  1247. idefloppy_pc_t pc;
  1248. idefloppy_capacity_header_t *header;
  1249. idefloppy_capacity_descriptor_t *descriptor;
  1250. int i, descriptors, rc = 1, blocks, length;
  1251. drive->bios_cyl = 0;
  1252. drive->bios_head = drive->bios_sect = 0;
  1253. floppy->blocks = 0;
  1254. floppy->bs_factor = 1;
  1255. set_capacity(floppy->disk, 0);
  1256. idefloppy_create_read_capacity_cmd(&pc);
  1257. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1258. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  1259. return 1;
  1260. }
  1261. header = (idefloppy_capacity_header_t *) pc.buffer;
  1262. descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
  1263. descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
  1264. for (i = 0; i < descriptors; i++, descriptor++) {
  1265. blocks = descriptor->blocks = ntohl(descriptor->blocks);
  1266. length = descriptor->length = ntohs(descriptor->length);
  1267. if (!i)
  1268. {
  1269. switch (descriptor->dc) {
  1270. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  1271. case CAPACITY_UNFORMATTED:
  1272. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
  1273. /*
  1274. * If it is not a clik drive, break out
  1275. * (maintains previous driver behaviour)
  1276. */
  1277. break;
  1278. case CAPACITY_CURRENT:
  1279. /* Normal Zip/LS-120 disks */
  1280. if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
  1281. printk(KERN_INFO "%s: %dkB, %d blocks, %d "
  1282. "sector size\n", drive->name,
  1283. blocks * length / 1024, blocks, length);
  1284. floppy->capacity = *descriptor;
  1285. if (!length || length % 512) {
  1286. printk(KERN_NOTICE "%s: %d bytes block size "
  1287. "not supported\n", drive->name, length);
  1288. } else {
  1289. floppy->blocks = blocks;
  1290. floppy->block_size = length;
  1291. if ((floppy->bs_factor = length / 512) != 1)
  1292. printk(KERN_NOTICE "%s: warning: non "
  1293. "512 bytes block size not "
  1294. "fully supported\n",
  1295. drive->name);
  1296. rc = 0;
  1297. }
  1298. break;
  1299. case CAPACITY_NO_CARTRIDGE:
  1300. /*
  1301. * This is a KERN_ERR so it appears on screen
  1302. * for the user to see
  1303. */
  1304. printk(KERN_ERR "%s: No disk in drive\n", drive->name);
  1305. break;
  1306. case CAPACITY_INVALID:
  1307. printk(KERN_ERR "%s: Invalid capacity for disk "
  1308. "in drive\n", drive->name);
  1309. break;
  1310. }
  1311. }
  1312. if (!i) {
  1313. debug_log( "Descriptor 0 Code: %d\n",
  1314. descriptor->dc);
  1315. }
  1316. debug_log( "Descriptor %d: %dkB, %d blocks, %d "
  1317. "sector size\n", i, blocks * length / 1024, blocks,
  1318. length);
  1319. }
  1320. /* Clik! disk does not support get_flexible_disk_page */
  1321. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1322. (void) idefloppy_get_flexible_disk_page(drive);
  1323. }
  1324. set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
  1325. return rc;
  1326. }
  1327. /*
  1328. ** Obtain the list of formattable capacities.
  1329. ** Very similar to idefloppy_get_capacity, except that we push the capacity
  1330. ** descriptors to userland, instead of our own structures.
  1331. **
  1332. ** Userland gives us the following structure:
  1333. **
  1334. ** struct idefloppy_format_capacities {
  1335. ** int nformats;
  1336. ** struct {
  1337. ** int nblocks;
  1338. ** int blocksize;
  1339. ** } formats[];
  1340. ** } ;
  1341. **
  1342. ** userland initializes nformats to the number of allocated formats[]
  1343. ** records. On exit we set nformats to the number of records we've
  1344. ** actually initialized.
  1345. **
  1346. */
  1347. static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
  1348. {
  1349. idefloppy_pc_t pc;
  1350. idefloppy_capacity_header_t *header;
  1351. idefloppy_capacity_descriptor_t *descriptor;
  1352. int i, descriptors, blocks, length;
  1353. int u_array_size;
  1354. int u_index;
  1355. int __user *argp;
  1356. if (get_user(u_array_size, arg))
  1357. return (-EFAULT);
  1358. if (u_array_size <= 0)
  1359. return (-EINVAL);
  1360. idefloppy_create_read_capacity_cmd(&pc);
  1361. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1362. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  1363. return (-EIO);
  1364. }
  1365. header = (idefloppy_capacity_header_t *) pc.buffer;
  1366. descriptors = header->length /
  1367. sizeof(idefloppy_capacity_descriptor_t);
  1368. descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
  1369. u_index = 0;
  1370. argp = arg + 1;
  1371. /*
  1372. ** We always skip the first capacity descriptor. That's the
  1373. ** current capacity. We are interested in the remaining descriptors,
  1374. ** the formattable capacities.
  1375. */
  1376. for (i=0; i<descriptors; i++, descriptor++) {
  1377. if (u_index >= u_array_size)
  1378. break; /* User-supplied buffer too small */
  1379. if (i == 0)
  1380. continue; /* Skip the first descriptor */
  1381. blocks = ntohl(descriptor->blocks);
  1382. length = ntohs(descriptor->length);
  1383. if (put_user(blocks, argp))
  1384. return(-EFAULT);
  1385. ++argp;
  1386. if (put_user(length, argp))
  1387. return (-EFAULT);
  1388. ++argp;
  1389. ++u_index;
  1390. }
  1391. if (put_user(u_index, arg))
  1392. return (-EFAULT);
  1393. return (0);
  1394. }
  1395. /*
  1396. ** Send ATAPI_FORMAT_UNIT to the drive.
  1397. **
  1398. ** Userland gives us the following structure:
  1399. **
  1400. ** struct idefloppy_format_command {
  1401. ** int nblocks;
  1402. ** int blocksize;
  1403. ** int flags;
  1404. ** } ;
  1405. **
  1406. ** flags is a bitmask, currently, the only defined flag is:
  1407. **
  1408. ** 0x01 - verify media after format.
  1409. */
  1410. static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
  1411. {
  1412. int blocks;
  1413. int length;
  1414. int flags;
  1415. idefloppy_pc_t pc;
  1416. if (get_user(blocks, arg) ||
  1417. get_user(length, arg+1) ||
  1418. get_user(flags, arg+2)) {
  1419. return (-EFAULT);
  1420. }
  1421. /* Get the SFRP bit */
  1422. (void) idefloppy_get_capability_page(drive);
  1423. idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
  1424. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1425. return (-EIO);
  1426. }
  1427. return (0);
  1428. }
  1429. /*
  1430. ** Get ATAPI_FORMAT_UNIT progress indication.
  1431. **
  1432. ** Userland gives a pointer to an int. The int is set to a progress
  1433. ** indicator 0-65536, with 65536=100%.
  1434. **
  1435. ** If the drive does not support format progress indication, we just check
  1436. ** the dsc bit, and return either 0 or 65536.
  1437. */
  1438. static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
  1439. {
  1440. idefloppy_floppy_t *floppy = drive->driver_data;
  1441. idefloppy_pc_t pc;
  1442. int progress_indication = 0x10000;
  1443. if (floppy->srfp) {
  1444. idefloppy_create_request_sense_cmd(&pc);
  1445. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1446. return (-EIO);
  1447. }
  1448. if (floppy->sense_key == 2 &&
  1449. floppy->asc == 4 &&
  1450. floppy->ascq == 4) {
  1451. progress_indication = floppy->progress_indication;
  1452. }
  1453. /* Else assume format_unit has finished, and we're
  1454. ** at 0x10000 */
  1455. } else {
  1456. unsigned long flags;
  1457. u8 stat;
  1458. local_irq_save(flags);
  1459. stat = drive->hwif->INB(IDE_STATUS_REG);
  1460. local_irq_restore(flags);
  1461. progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
  1462. }
  1463. if (put_user(progress_indication, arg))
  1464. return (-EFAULT);
  1465. return (0);
  1466. }
  1467. /*
  1468. * Return the current floppy capacity.
  1469. */
  1470. static sector_t idefloppy_capacity (ide_drive_t *drive)
  1471. {
  1472. idefloppy_floppy_t *floppy = drive->driver_data;
  1473. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  1474. return capacity;
  1475. }
  1476. /*
  1477. * idefloppy_identify_device checks if we can support a drive,
  1478. * based on the ATAPI IDENTIFY command results.
  1479. */
  1480. static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
  1481. {
  1482. struct idefloppy_id_gcw gcw;
  1483. #if IDEFLOPPY_DEBUG_INFO
  1484. u16 mask,i;
  1485. char buffer[80];
  1486. #endif /* IDEFLOPPY_DEBUG_INFO */
  1487. *((u16 *) &gcw) = id->config;
  1488. #ifdef CONFIG_PPC
  1489. /* kludge for Apple PowerBook internal zip */
  1490. if ((gcw.device_type == 5) &&
  1491. !strstr(id->model, "CD-ROM") &&
  1492. strstr(id->model, "ZIP"))
  1493. gcw.device_type = 0;
  1494. #endif
  1495. #if IDEFLOPPY_DEBUG_INFO
  1496. printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
  1497. switch (gcw.protocol) {
  1498. case 0: case 1: sprintf(buffer, "ATA");break;
  1499. case 2: sprintf(buffer, "ATAPI");break;
  1500. case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
  1501. }
  1502. printk(KERN_INFO "Protocol Type: %s\n", buffer);
  1503. switch (gcw.device_type) {
  1504. case 0: sprintf(buffer, "Direct-access Device");break;
  1505. case 1: sprintf(buffer, "Streaming Tape Device");break;
  1506. case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
  1507. case 5: sprintf(buffer, "CD-ROM Device");break;
  1508. case 6: sprintf(buffer, "Reserved");
  1509. case 7: sprintf(buffer, "Optical memory Device");break;
  1510. case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
  1511. default: sprintf(buffer, "Reserved");
  1512. }
  1513. printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
  1514. printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
  1515. switch (gcw.drq_type) {
  1516. case 0: sprintf(buffer, "Microprocessor DRQ");break;
  1517. case 1: sprintf(buffer, "Interrupt DRQ");break;
  1518. case 2: sprintf(buffer, "Accelerated DRQ");break;
  1519. case 3: sprintf(buffer, "Reserved");break;
  1520. }
  1521. printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
  1522. switch (gcw.packet_size) {
  1523. case 0: sprintf(buffer, "12 bytes");break;
  1524. case 1: sprintf(buffer, "16 bytes");break;
  1525. default: sprintf(buffer, "Reserved");break;
  1526. }
  1527. printk(KERN_INFO "Command Packet Size: %s\n", buffer);
  1528. printk(KERN_INFO "Model: %.40s\n",id->model);
  1529. printk(KERN_INFO "Firmware Revision: %.8s\n",id->fw_rev);
  1530. printk(KERN_INFO "Serial Number: %.20s\n",id->serial_no);
  1531. printk(KERN_INFO "Write buffer size(?): %d bytes\n",id->buf_size*512);
  1532. printk(KERN_INFO "DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
  1533. printk(KERN_INFO "LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
  1534. printk(KERN_INFO "IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
  1535. printk(KERN_INFO "IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
  1536. printk(KERN_INFO "ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
  1537. printk(KERN_INFO "PIO Cycle Timing Category: %d\n",id->tPIO);
  1538. printk(KERN_INFO "DMA Cycle Timing Category: %d\n",id->tDMA);
  1539. printk(KERN_INFO "Single Word DMA supported modes:\n");
  1540. for (i=0,mask=1;i<8;i++,mask=mask << 1) {
  1541. if (id->dma_1word & mask)
  1542. printk(KERN_INFO " Mode %d%s\n", i,
  1543. (id->dma_1word & (mask << 8)) ? " (active)" : "");
  1544. }
  1545. printk(KERN_INFO "Multi Word DMA supported modes:\n");
  1546. for (i=0,mask=1;i<8;i++,mask=mask << 1) {
  1547. if (id->dma_mword & mask)
  1548. printk(KERN_INFO " Mode %d%s\n", i,
  1549. (id->dma_mword & (mask << 8)) ? " (active)" : "");
  1550. }
  1551. if (id->field_valid & 0x0002) {
  1552. printk(KERN_INFO "Enhanced PIO Modes: %s\n",
  1553. id->eide_pio_modes & 1 ? "Mode 3":"None");
  1554. if (id->eide_dma_min == 0)
  1555. sprintf(buffer, "Not supported");
  1556. else
  1557. sprintf(buffer, "%d ns",id->eide_dma_min);
  1558. printk(KERN_INFO "Minimum Multi-word DMA cycle per word: %s\n", buffer);
  1559. if (id->eide_dma_time == 0)
  1560. sprintf(buffer, "Not supported");
  1561. else
  1562. sprintf(buffer, "%d ns",id->eide_dma_time);
  1563. printk(KERN_INFO "Manufacturer\'s Recommended Multi-word cycle: %s\n", buffer);
  1564. if (id->eide_pio == 0)
  1565. sprintf(buffer, "Not supported");
  1566. else
  1567. sprintf(buffer, "%d ns",id->eide_pio);
  1568. printk(KERN_INFO "Minimum PIO cycle without IORDY: %s\n",
  1569. buffer);
  1570. if (id->eide_pio_iordy == 0)
  1571. sprintf(buffer, "Not supported");
  1572. else
  1573. sprintf(buffer, "%d ns",id->eide_pio_iordy);
  1574. printk(KERN_INFO "Minimum PIO cycle with IORDY: %s\n", buffer);
  1575. } else
  1576. printk(KERN_INFO "According to the device, fields 64-70 are not valid.\n");
  1577. #endif /* IDEFLOPPY_DEBUG_INFO */
  1578. if (gcw.protocol != 2)
  1579. printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
  1580. else if (gcw.device_type != 0)
  1581. printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
  1582. else if (!gcw.removable)
  1583. printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
  1584. else if (gcw.drq_type == 3) {
  1585. printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
  1586. } else if (gcw.packet_size != 0) {
  1587. printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
  1588. } else
  1589. return 1;
  1590. return 0;
  1591. }
  1592. #ifdef CONFIG_IDE_PROC_FS
  1593. static void idefloppy_add_settings(ide_drive_t *drive)
  1594. {
  1595. idefloppy_floppy_t *floppy = drive->driver_data;
  1596. /*
  1597. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  1598. */
  1599. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  1600. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  1601. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  1602. ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
  1603. }
  1604. #else
  1605. static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
  1606. #endif
  1607. /*
  1608. * Driver initialization.
  1609. */
  1610. static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
  1611. {
  1612. struct idefloppy_id_gcw gcw;
  1613. *((u16 *) &gcw) = drive->id->config;
  1614. floppy->pc = floppy->pc_stack;
  1615. if (gcw.drq_type == 1)
  1616. set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
  1617. /*
  1618. * We used to check revisions here. At this point however
  1619. * I'm giving up. Just assume they are all broken, its easier.
  1620. *
  1621. * The actual reason for the workarounds was likely
  1622. * a driver bug after all rather than a firmware bug,
  1623. * and the workaround below used to hide it. It should
  1624. * be fixed as of version 1.9, but to be on the safe side
  1625. * we'll leave the limitation below for the 2.2.x tree.
  1626. */
  1627. if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
  1628. set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
  1629. /* This value will be visible in the /proc/ide/hdx/settings */
  1630. floppy->ticks = IDEFLOPPY_TICKS_DELAY;
  1631. blk_queue_max_sectors(drive->queue, 64);
  1632. }
  1633. /*
  1634. * Guess what? The IOMEGA Clik! drive also needs the
  1635. * above fix. It makes nasty clicking noises without
  1636. * it, so please don't remove this.
  1637. */
  1638. if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
  1639. blk_queue_max_sectors(drive->queue, 64);
  1640. set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
  1641. }
  1642. (void) idefloppy_get_capacity(drive);
  1643. idefloppy_add_settings(drive);
  1644. }
  1645. static void ide_floppy_remove(ide_drive_t *drive)
  1646. {
  1647. idefloppy_floppy_t *floppy = drive->driver_data;
  1648. struct gendisk *g = floppy->disk;
  1649. ide_proc_unregister_driver(drive, floppy->driver);
  1650. del_gendisk(g);
  1651. ide_floppy_put(floppy);
  1652. }
  1653. static void ide_floppy_release(struct kref *kref)
  1654. {
  1655. struct ide_floppy_obj *floppy = to_ide_floppy(kref);
  1656. ide_drive_t *drive = floppy->drive;
  1657. struct gendisk *g = floppy->disk;
  1658. drive->driver_data = NULL;
  1659. g->private_data = NULL;
  1660. put_disk(g);
  1661. kfree(floppy);
  1662. }
  1663. #ifdef CONFIG_IDE_PROC_FS
  1664. static int proc_idefloppy_read_capacity
  1665. (char *page, char **start, off_t off, int count, int *eof, void *data)
  1666. {
  1667. ide_drive_t*drive = (ide_drive_t *)data;
  1668. int len;
  1669. len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
  1670. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  1671. }
  1672. static ide_proc_entry_t idefloppy_proc[] = {
  1673. { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
  1674. { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
  1675. { NULL, 0, NULL, NULL }
  1676. };
  1677. #endif /* CONFIG_IDE_PROC_FS */
  1678. static int ide_floppy_probe(ide_drive_t *);
  1679. static ide_driver_t idefloppy_driver = {
  1680. .gen_driver = {
  1681. .owner = THIS_MODULE,
  1682. .name = "ide-floppy",
  1683. .bus = &ide_bus_type,
  1684. },
  1685. .probe = ide_floppy_probe,
  1686. .remove = ide_floppy_remove,
  1687. .version = IDEFLOPPY_VERSION,
  1688. .media = ide_floppy,
  1689. .supports_dsc_overlap = 0,
  1690. .do_request = idefloppy_do_request,
  1691. .end_request = idefloppy_do_end_request,
  1692. .error = __ide_error,
  1693. .abort = __ide_abort,
  1694. #ifdef CONFIG_IDE_PROC_FS
  1695. .proc = idefloppy_proc,
  1696. #endif
  1697. };
  1698. static int idefloppy_open(struct inode *inode, struct file *filp)
  1699. {
  1700. struct gendisk *disk = inode->i_bdev->bd_disk;
  1701. struct ide_floppy_obj *floppy;
  1702. ide_drive_t *drive;
  1703. idefloppy_pc_t pc;
  1704. int ret = 0;
  1705. debug_log(KERN_INFO "Reached idefloppy_open\n");
  1706. if (!(floppy = ide_floppy_get(disk)))
  1707. return -ENXIO;
  1708. drive = floppy->drive;
  1709. floppy->openers++;
  1710. if (floppy->openers == 1) {
  1711. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1712. /* Just in case */
  1713. idefloppy_create_test_unit_ready_cmd(&pc);
  1714. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1715. idefloppy_create_start_stop_cmd(&pc, 1);
  1716. (void) idefloppy_queue_pc_tail(drive, &pc);
  1717. }
  1718. if (idefloppy_get_capacity (drive)
  1719. && (filp->f_flags & O_NDELAY) == 0
  1720. /*
  1721. ** Allow O_NDELAY to open a drive without a disk, or with
  1722. ** an unreadable disk, so that we can get the format
  1723. ** capacity of the drive or begin the format - Sam
  1724. */
  1725. ) {
  1726. ret = -EIO;
  1727. goto out_put_floppy;
  1728. }
  1729. if (floppy->wp && (filp->f_mode & 2)) {
  1730. ret = -EROFS;
  1731. goto out_put_floppy;
  1732. }
  1733. set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
  1734. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1735. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1736. idefloppy_create_prevent_cmd(&pc, 1);
  1737. (void) idefloppy_queue_pc_tail(drive, &pc);
  1738. }
  1739. check_disk_change(inode->i_bdev);
  1740. } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
  1741. ret = -EBUSY;
  1742. goto out_put_floppy;
  1743. }
  1744. return 0;
  1745. out_put_floppy:
  1746. floppy->openers--;
  1747. ide_floppy_put(floppy);
  1748. return ret;
  1749. }
  1750. static int idefloppy_release(struct inode *inode, struct file *filp)
  1751. {
  1752. struct gendisk *disk = inode->i_bdev->bd_disk;
  1753. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1754. ide_drive_t *drive = floppy->drive;
  1755. idefloppy_pc_t pc;
  1756. debug_log(KERN_INFO "Reached idefloppy_release\n");
  1757. if (floppy->openers == 1) {
  1758. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1759. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1760. idefloppy_create_prevent_cmd(&pc, 0);
  1761. (void) idefloppy_queue_pc_tail(drive, &pc);
  1762. }
  1763. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1764. }
  1765. floppy->openers--;
  1766. ide_floppy_put(floppy);
  1767. return 0;
  1768. }
  1769. static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1770. {
  1771. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1772. ide_drive_t *drive = floppy->drive;
  1773. geo->heads = drive->bios_head;
  1774. geo->sectors = drive->bios_sect;
  1775. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  1776. return 0;
  1777. }
  1778. static int idefloppy_ioctl(struct inode *inode, struct file *file,
  1779. unsigned int cmd, unsigned long arg)
  1780. {
  1781. struct block_device *bdev = inode->i_bdev;
  1782. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1783. ide_drive_t *drive = floppy->drive;
  1784. void __user *argp = (void __user *)arg;
  1785. int err;
  1786. int prevent = (arg) ? 1 : 0;
  1787. idefloppy_pc_t pc;
  1788. switch (cmd) {
  1789. case CDROMEJECT:
  1790. prevent = 0;
  1791. /* fall through */
  1792. case CDROM_LOCKDOOR:
  1793. if (floppy->openers > 1)
  1794. return -EBUSY;
  1795. /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
  1796. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1797. idefloppy_create_prevent_cmd(&pc, prevent);
  1798. (void) idefloppy_queue_pc_tail(drive, &pc);
  1799. }
  1800. if (cmd == CDROMEJECT) {
  1801. idefloppy_create_start_stop_cmd(&pc, 2);
  1802. (void) idefloppy_queue_pc_tail(drive, &pc);
  1803. }
  1804. return 0;
  1805. case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
  1806. return 0;
  1807. case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
  1808. return idefloppy_get_format_capacities(drive, argp);
  1809. case IDEFLOPPY_IOCTL_FORMAT_START:
  1810. if (!(file->f_mode & 2))
  1811. return -EPERM;
  1812. if (floppy->openers > 1) {
  1813. /* Don't format if someone is using the disk */
  1814. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
  1815. &floppy->flags);
  1816. return -EBUSY;
  1817. }
  1818. set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1819. err = idefloppy_begin_format(drive, argp);
  1820. if (err)
  1821. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1822. return err;
  1823. /*
  1824. ** Note, the bit will be cleared when the device is
  1825. ** closed. This is the cleanest way to handle the
  1826. ** situation where the drive does not support
  1827. ** format progress reporting.
  1828. */
  1829. case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
  1830. return idefloppy_get_format_progress(drive, argp);
  1831. }
  1832. /*
  1833. * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
  1834. * and CDROM_SEND_PACKET (legacy) ioctls
  1835. */
  1836. if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
  1837. err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
  1838. bdev->bd_disk, cmd, argp);
  1839. else
  1840. err = -ENOTTY;
  1841. if (err == -ENOTTY)
  1842. err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  1843. return err;
  1844. }
  1845. static int idefloppy_media_changed(struct gendisk *disk)
  1846. {
  1847. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1848. ide_drive_t *drive = floppy->drive;
  1849. /* do not scan partitions twice if this is a removable device */
  1850. if (drive->attach) {
  1851. drive->attach = 0;
  1852. return 0;
  1853. }
  1854. return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
  1855. }
  1856. static int idefloppy_revalidate_disk(struct gendisk *disk)
  1857. {
  1858. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1859. set_capacity(disk, idefloppy_capacity(floppy->drive));
  1860. return 0;
  1861. }
  1862. static struct block_device_operations idefloppy_ops = {
  1863. .owner = THIS_MODULE,
  1864. .open = idefloppy_open,
  1865. .release = idefloppy_release,
  1866. .ioctl = idefloppy_ioctl,
  1867. .getgeo = idefloppy_getgeo,
  1868. .media_changed = idefloppy_media_changed,
  1869. .revalidate_disk= idefloppy_revalidate_disk
  1870. };
  1871. static int ide_floppy_probe(ide_drive_t *drive)
  1872. {
  1873. idefloppy_floppy_t *floppy;
  1874. struct gendisk *g;
  1875. if (!strstr("ide-floppy", drive->driver_req))
  1876. goto failed;
  1877. if (!drive->present)
  1878. goto failed;
  1879. if (drive->media != ide_floppy)
  1880. goto failed;
  1881. if (!idefloppy_identify_device (drive, drive->id)) {
  1882. printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
  1883. goto failed;
  1884. }
  1885. if (drive->scsi) {
  1886. printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
  1887. goto failed;
  1888. }
  1889. if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
  1890. printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
  1891. goto failed;
  1892. }
  1893. g = alloc_disk(1 << PARTN_BITS);
  1894. if (!g)
  1895. goto out_free_floppy;
  1896. ide_init_disk(g, drive);
  1897. ide_proc_register_driver(drive, &idefloppy_driver);
  1898. kref_init(&floppy->kref);
  1899. floppy->drive = drive;
  1900. floppy->driver = &idefloppy_driver;
  1901. floppy->disk = g;
  1902. g->private_data = &floppy->driver;
  1903. drive->driver_data = floppy;
  1904. idefloppy_setup (drive, floppy);
  1905. g->minors = 1 << PARTN_BITS;
  1906. g->driverfs_dev = &drive->gendev;
  1907. g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
  1908. g->fops = &idefloppy_ops;
  1909. drive->attach = 1;
  1910. add_disk(g);
  1911. return 0;
  1912. out_free_floppy:
  1913. kfree(floppy);
  1914. failed:
  1915. return -ENODEV;
  1916. }
  1917. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
  1918. static void __exit idefloppy_exit (void)
  1919. {
  1920. driver_unregister(&idefloppy_driver.gen_driver);
  1921. }
  1922. static int __init idefloppy_init(void)
  1923. {
  1924. printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
  1925. return driver_register(&idefloppy_driver.gen_driver);
  1926. }
  1927. MODULE_ALIAS("ide:*m-floppy*");
  1928. module_init(idefloppy_init);
  1929. module_exit(idefloppy_exit);
  1930. MODULE_LICENSE("GPL");