ide-floppy.c 51 KB

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