ide-floppy.c 55 KB

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