ide-floppy.c 67 KB

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