ide-floppy.c 66 KB

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