ide-floppy.c 59 KB

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