ide-floppy.c 65 KB

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