ide-floppy.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. * This driver supports the following IDE floppy drives:
  9. *
  10. * LS-120/240 SuperDisk
  11. * Iomega Zip 100/250
  12. * Iomega PC Card Clik!/PocketZip
  13. *
  14. * For a historical changelog see
  15. * Documentation/ide/ChangeLog.ide-floppy.1996-2002
  16. */
  17. #define IDEFLOPPY_VERSION "1.00"
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/timer.h>
  24. #include <linux/mm.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/major.h>
  27. #include <linux/errno.h>
  28. #include <linux/genhd.h>
  29. #include <linux/slab.h>
  30. #include <linux/cdrom.h>
  31. #include <linux/ide.h>
  32. #include <linux/bitops.h>
  33. #include <linux/mutex.h>
  34. #include <scsi/scsi_ioctl.h>
  35. #include <asm/byteorder.h>
  36. #include <linux/irq.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/io.h>
  39. #include <asm/unaligned.h>
  40. /* define to see debug info */
  41. #define IDEFLOPPY_DEBUG_LOG 0
  42. /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
  43. #define IDEFLOPPY_DEBUG(fmt, args...)
  44. #if IDEFLOPPY_DEBUG_LOG
  45. #define debug_log(fmt, args...) \
  46. printk(KERN_INFO "ide-floppy: " fmt, ## args)
  47. #else
  48. #define debug_log(fmt, args...) do {} while (0)
  49. #endif
  50. /* Some drives require a longer irq timeout. */
  51. #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
  52. /*
  53. * After each failed packet command we issue a request sense command and retry
  54. * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
  55. */
  56. #define IDEFLOPPY_MAX_PC_RETRIES 3
  57. /*
  58. * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
  59. * bytes.
  60. */
  61. #define IDEFLOPPY_PC_BUFFER_SIZE 256
  62. /*
  63. * In various places in the driver, we need to allocate storage for packet
  64. * commands and requests, which will remain valid while we leave the driver to
  65. * wait for an interrupt or a timeout event.
  66. */
  67. #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
  68. /* format capacities descriptor codes */
  69. #define CAPACITY_INVALID 0x00
  70. #define CAPACITY_UNFORMATTED 0x01
  71. #define CAPACITY_CURRENT 0x02
  72. #define CAPACITY_NO_CARTRIDGE 0x03
  73. /*
  74. * Most of our global data which we need to save even as we leave the driver
  75. * due to an interrupt or a timer event is stored in a variable of type
  76. * idefloppy_floppy_t, defined below.
  77. */
  78. typedef struct ide_floppy_obj {
  79. ide_drive_t *drive;
  80. ide_driver_t *driver;
  81. struct gendisk *disk;
  82. struct kref kref;
  83. unsigned int openers; /* protected by BKL for now */
  84. /* Current packet command */
  85. struct ide_atapi_pc *pc;
  86. /* Last failed packet command */
  87. struct ide_atapi_pc *failed_pc;
  88. /* Packet command stack */
  89. struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
  90. /* Next free packet command storage space */
  91. int pc_stack_index;
  92. struct request rq_stack[IDEFLOPPY_PC_STACK];
  93. /* We implement a circular array */
  94. int rq_stack_index;
  95. /* Last error information */
  96. u8 sense_key, asc, ascq;
  97. /* delay this long before sending packet command */
  98. u8 ticks;
  99. int progress_indication;
  100. /* Device information */
  101. /* Current format */
  102. int blocks, block_size, bs_factor;
  103. /* Last format capacity descriptor */
  104. u8 cap_desc[8];
  105. /* Copy of the flexible disk page */
  106. u8 flexible_disk_page[32];
  107. /* Write protect */
  108. int wp;
  109. /* Supports format progress report */
  110. int srfp;
  111. /* Status/Action flags */
  112. unsigned long flags;
  113. } idefloppy_floppy_t;
  114. #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
  115. /* Floppy flag bits values. */
  116. enum {
  117. /* DRQ interrupt device */
  118. IDEFLOPPY_FLAG_DRQ_INTERRUPT = (1 << 0),
  119. /* Media may have changed */
  120. IDEFLOPPY_FLAG_MEDIA_CHANGED = (1 << 1),
  121. /* Format in progress */
  122. IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS = (1 << 2),
  123. /* Avoid commands not supported in Clik drive */
  124. IDEFLOPPY_FLAG_CLIK_DRIVE = (1 << 3),
  125. /* Requires BH algorithm for packets */
  126. IDEFLOPPY_FLAG_ZIP_DRIVE = (1 << 4),
  127. };
  128. /* Defines for the MODE SENSE command */
  129. #define MODE_SENSE_CURRENT 0x00
  130. #define MODE_SENSE_CHANGEABLE 0x01
  131. #define MODE_SENSE_DEFAULT 0x02
  132. #define MODE_SENSE_SAVED 0x03
  133. /* IOCTLs used in low-level formatting. */
  134. #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
  135. #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
  136. #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
  137. #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
  138. /* Error code returned in rq->errors to the higher part of the driver. */
  139. #define IDEFLOPPY_ERROR_GENERAL 101
  140. /*
  141. * Pages of the SELECT SENSE / MODE SENSE packet commands.
  142. * See SFF-8070i spec.
  143. */
  144. #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
  145. #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
  146. static DEFINE_MUTEX(idefloppy_ref_mutex);
  147. #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
  148. #define ide_floppy_g(disk) \
  149. container_of((disk)->private_data, struct ide_floppy_obj, driver)
  150. static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
  151. {
  152. struct ide_floppy_obj *floppy = NULL;
  153. mutex_lock(&idefloppy_ref_mutex);
  154. floppy = ide_floppy_g(disk);
  155. if (floppy)
  156. kref_get(&floppy->kref);
  157. mutex_unlock(&idefloppy_ref_mutex);
  158. return floppy;
  159. }
  160. static void idefloppy_cleanup_obj(struct kref *);
  161. static void ide_floppy_put(struct ide_floppy_obj *floppy)
  162. {
  163. mutex_lock(&idefloppy_ref_mutex);
  164. kref_put(&floppy->kref, idefloppy_cleanup_obj);
  165. mutex_unlock(&idefloppy_ref_mutex);
  166. }
  167. /*
  168. * Used to finish servicing a request. For read/write requests, we will call
  169. * ide_end_request to pass to the next buffer.
  170. */
  171. static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
  172. {
  173. idefloppy_floppy_t *floppy = drive->driver_data;
  174. struct request *rq = HWGROUP(drive)->rq;
  175. int error;
  176. debug_log("Reached %s\n", __func__);
  177. switch (uptodate) {
  178. case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
  179. case 1: error = 0; break;
  180. default: error = uptodate;
  181. }
  182. if (error)
  183. floppy->failed_pc = NULL;
  184. /* Why does this happen? */
  185. if (!rq)
  186. return 0;
  187. if (!blk_special_request(rq)) {
  188. /* our real local end request function */
  189. ide_end_request(drive, uptodate, nsecs);
  190. return 0;
  191. }
  192. rq->errors = error;
  193. /* fixme: need to move this local also */
  194. ide_end_drive_cmd(drive, 0, 0);
  195. return 0;
  196. }
  197. static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  198. unsigned int bcount, int direction)
  199. {
  200. ide_hwif_t *hwif = drive->hwif;
  201. struct request *rq = pc->rq;
  202. struct req_iterator iter;
  203. struct bio_vec *bvec;
  204. unsigned long flags;
  205. int count, done = 0;
  206. char *data;
  207. rq_for_each_segment(bvec, rq, iter) {
  208. if (!bcount)
  209. break;
  210. count = min(bvec->bv_len, bcount);
  211. data = bvec_kmap_irq(bvec, &flags);
  212. if (direction)
  213. hwif->output_data(drive, NULL, data, count);
  214. else
  215. hwif->input_data(drive, NULL, data, count);
  216. bvec_kunmap_irq(data, &flags);
  217. bcount -= count;
  218. pc->b_count += count;
  219. done += count;
  220. }
  221. idefloppy_end_request(drive, 1, done >> 9);
  222. if (bcount) {
  223. printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
  224. drive->name, __func__, bcount);
  225. ide_pad_transfer(drive, direction, bcount);
  226. }
  227. }
  228. static void idefloppy_update_buffers(ide_drive_t *drive,
  229. struct ide_atapi_pc *pc)
  230. {
  231. struct request *rq = pc->rq;
  232. struct bio *bio = rq->bio;
  233. while ((bio = rq->bio) != NULL)
  234. idefloppy_end_request(drive, 1, 0);
  235. }
  236. /*
  237. * Generate a new packet command request in front of the request queue, before
  238. * the current request so that it will be processed immediately, on the next
  239. * pass through the driver.
  240. */
  241. static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
  242. struct request *rq)
  243. {
  244. struct ide_floppy_obj *floppy = drive->driver_data;
  245. blk_rq_init(NULL, rq);
  246. rq->buffer = (char *) pc;
  247. rq->cmd_type = REQ_TYPE_SPECIAL;
  248. rq->cmd_flags |= REQ_PREEMPT;
  249. rq->rq_disk = floppy->disk;
  250. ide_do_drive_cmd(drive, rq);
  251. }
  252. static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
  253. {
  254. idefloppy_floppy_t *floppy = drive->driver_data;
  255. if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
  256. floppy->pc_stack_index = 0;
  257. return (&floppy->pc_stack[floppy->pc_stack_index++]);
  258. }
  259. static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
  260. {
  261. idefloppy_floppy_t *floppy = drive->driver_data;
  262. if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
  263. floppy->rq_stack_index = 0;
  264. return (&floppy->rq_stack[floppy->rq_stack_index++]);
  265. }
  266. static void ide_floppy_callback(ide_drive_t *drive)
  267. {
  268. idefloppy_floppy_t *floppy = drive->driver_data;
  269. struct ide_atapi_pc *pc = floppy->pc;
  270. int uptodate = pc->error ? 0 : 1;
  271. debug_log("Reached %s\n", __func__);
  272. if (floppy->failed_pc == pc)
  273. floppy->failed_pc = NULL;
  274. if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
  275. (pc->rq && blk_pc_request(pc->rq)))
  276. uptodate = 1; /* FIXME */
  277. else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
  278. u8 *buf = floppy->pc->buf;
  279. if (!pc->error) {
  280. floppy->sense_key = buf[2] & 0x0F;
  281. floppy->asc = buf[12];
  282. floppy->ascq = buf[13];
  283. floppy->progress_indication = buf[15] & 0x80 ?
  284. (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
  285. if (floppy->failed_pc)
  286. debug_log("pc = %x, ", floppy->failed_pc->c[0]);
  287. debug_log("sense key = %x, asc = %x, ascq = %x\n",
  288. floppy->sense_key, floppy->asc, floppy->ascq);
  289. } else
  290. printk(KERN_ERR "Error in REQUEST SENSE itself - "
  291. "Aborting request!\n");
  292. }
  293. idefloppy_end_request(drive, uptodate, 0);
  294. }
  295. static void idefloppy_init_pc(struct ide_atapi_pc *pc)
  296. {
  297. memset(pc->c, 0, 12);
  298. pc->retries = 0;
  299. pc->flags = 0;
  300. pc->req_xfer = 0;
  301. pc->buf = pc->pc_buf;
  302. pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
  303. pc->callback = ide_floppy_callback;
  304. }
  305. static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
  306. {
  307. idefloppy_init_pc(pc);
  308. pc->c[0] = GPCMD_REQUEST_SENSE;
  309. pc->c[4] = 255;
  310. pc->req_xfer = 18;
  311. }
  312. /*
  313. * Called when an error was detected during the last packet command. We queue a
  314. * request sense packet command in the head of the request list.
  315. */
  316. static void idefloppy_retry_pc(ide_drive_t *drive)
  317. {
  318. struct ide_atapi_pc *pc;
  319. struct request *rq;
  320. (void)ide_read_error(drive);
  321. pc = idefloppy_next_pc_storage(drive);
  322. rq = idefloppy_next_rq_storage(drive);
  323. idefloppy_create_request_sense_cmd(pc);
  324. idefloppy_queue_pc_head(drive, pc, rq);
  325. }
  326. /* The usual interrupt handler called during a packet command. */
  327. static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
  328. {
  329. idefloppy_floppy_t *floppy = drive->driver_data;
  330. return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
  331. IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
  332. idefloppy_retry_pc, NULL, ide_floppy_io_buffers);
  333. }
  334. /*
  335. * What we have here is a classic case of a top half / bottom half interrupt
  336. * service routine. In interrupt mode, the device sends an interrupt to signal
  337. * that it is ready to receive a packet. However, we need to delay about 2-3
  338. * ticks before issuing the packet or we gets in trouble.
  339. *
  340. * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
  341. * In either case, when the device says it's ready for a packet, we schedule
  342. * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
  343. */
  344. static int idefloppy_transfer_pc2(ide_drive_t *drive)
  345. {
  346. idefloppy_floppy_t *floppy = drive->driver_data;
  347. /* Send the actual packet */
  348. drive->hwif->output_data(drive, NULL, floppy->pc->c, 12);
  349. /* Timeout for the packet command */
  350. return IDEFLOPPY_WAIT_CMD;
  351. }
  352. static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive)
  353. {
  354. idefloppy_floppy_t *floppy = drive->driver_data;
  355. struct ide_atapi_pc *pc = floppy->pc;
  356. ide_expiry_t *expiry;
  357. unsigned int timeout;
  358. /*
  359. * The following delay solves a problem with ATAPI Zip 100 drives
  360. * where the Busy flag was apparently being deasserted before the
  361. * unit was ready to receive data. This was happening on a
  362. * 1200 MHz Athlon system. 10/26/01 25msec is too short,
  363. * 40 and 50msec work well. idefloppy_pc_intr will not be actually
  364. * used until after the packet is moved in about 50 msec.
  365. */
  366. if (pc->flags & PC_FLAG_ZIP_DRIVE) {
  367. timeout = floppy->ticks;
  368. expiry = &idefloppy_transfer_pc2;
  369. } else {
  370. timeout = IDEFLOPPY_WAIT_CMD;
  371. expiry = NULL;
  372. }
  373. return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
  374. }
  375. static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
  376. struct ide_atapi_pc *pc)
  377. {
  378. /* supress error messages resulting from Medium not present */
  379. if (floppy->sense_key == 0x02 &&
  380. floppy->asc == 0x3a &&
  381. floppy->ascq == 0x00)
  382. return;
  383. printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
  384. "asc = %2x, ascq = %2x\n",
  385. floppy->drive->name, pc->c[0], floppy->sense_key,
  386. floppy->asc, floppy->ascq);
  387. }
  388. static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
  389. struct ide_atapi_pc *pc)
  390. {
  391. idefloppy_floppy_t *floppy = drive->driver_data;
  392. if (floppy->failed_pc == NULL &&
  393. pc->c[0] != GPCMD_REQUEST_SENSE)
  394. floppy->failed_pc = pc;
  395. /* Set the current packet command */
  396. floppy->pc = pc;
  397. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
  398. if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
  399. ide_floppy_report_error(floppy, pc);
  400. /* Giving up */
  401. pc->error = IDEFLOPPY_ERROR_GENERAL;
  402. floppy->failed_pc = NULL;
  403. pc->callback(drive);
  404. return ide_stopped;
  405. }
  406. debug_log("Retry number - %d\n", pc->retries);
  407. pc->retries++;
  408. return ide_issue_pc(drive, pc, idefloppy_transfer_pc1,
  409. IDEFLOPPY_WAIT_CMD, NULL);
  410. }
  411. static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
  412. {
  413. debug_log("creating prevent removal command, prevent = %d\n", prevent);
  414. idefloppy_init_pc(pc);
  415. pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  416. pc->c[4] = prevent;
  417. }
  418. static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
  419. {
  420. idefloppy_init_pc(pc);
  421. pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
  422. pc->c[7] = 255;
  423. pc->c[8] = 255;
  424. pc->req_xfer = 255;
  425. }
  426. static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
  427. int l, int flags)
  428. {
  429. idefloppy_init_pc(pc);
  430. pc->c[0] = GPCMD_FORMAT_UNIT;
  431. pc->c[1] = 0x17;
  432. memset(pc->buf, 0, 12);
  433. pc->buf[1] = 0xA2;
  434. /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
  435. if (flags & 1) /* Verify bit on... */
  436. pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
  437. pc->buf[3] = 8;
  438. put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
  439. put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
  440. pc->buf_size = 12;
  441. pc->flags |= PC_FLAG_WRITING;
  442. }
  443. /* A mode sense command is used to "sense" floppy parameters. */
  444. static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
  445. u8 page_code, u8 type)
  446. {
  447. u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
  448. idefloppy_init_pc(pc);
  449. pc->c[0] = GPCMD_MODE_SENSE_10;
  450. pc->c[1] = 0;
  451. pc->c[2] = page_code + (type << 6);
  452. switch (page_code) {
  453. case IDEFLOPPY_CAPABILITIES_PAGE:
  454. length += 12;
  455. break;
  456. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  457. length += 32;
  458. break;
  459. default:
  460. printk(KERN_ERR "ide-floppy: unsupported page code "
  461. "in create_mode_sense_cmd\n");
  462. }
  463. put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
  464. pc->req_xfer = length;
  465. }
  466. static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
  467. {
  468. idefloppy_init_pc(pc);
  469. pc->c[0] = GPCMD_START_STOP_UNIT;
  470. pc->c[4] = start;
  471. }
  472. static void idefloppy_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
  473. {
  474. idefloppy_init_pc(pc);
  475. pc->c[0] = GPCMD_TEST_UNIT_READY;
  476. }
  477. static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
  478. struct ide_atapi_pc *pc, struct request *rq,
  479. unsigned long sector)
  480. {
  481. int block = sector / floppy->bs_factor;
  482. int blocks = rq->nr_sectors / floppy->bs_factor;
  483. int cmd = rq_data_dir(rq);
  484. debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
  485. block, blocks);
  486. idefloppy_init_pc(pc);
  487. pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
  488. put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
  489. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
  490. pc->rq = rq;
  491. pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
  492. if (rq->cmd_flags & REQ_RW)
  493. pc->flags |= PC_FLAG_WRITING;
  494. pc->buf = NULL;
  495. pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
  496. pc->flags |= PC_FLAG_DMA_OK;
  497. }
  498. static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
  499. struct ide_atapi_pc *pc, struct request *rq)
  500. {
  501. idefloppy_init_pc(pc);
  502. memcpy(pc->c, rq->cmd, sizeof(pc->c));
  503. pc->rq = rq;
  504. pc->b_count = rq->data_len;
  505. if (rq->data_len && rq_data_dir(rq) == WRITE)
  506. pc->flags |= PC_FLAG_WRITING;
  507. pc->buf = rq->data;
  508. if (rq->bio)
  509. pc->flags |= PC_FLAG_DMA_OK;
  510. /*
  511. * possibly problematic, doesn't look like ide-floppy correctly
  512. * handled scattered requests if dma fails...
  513. */
  514. pc->req_xfer = pc->buf_size = rq->data_len;
  515. }
  516. static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
  517. struct request *rq, sector_t block_s)
  518. {
  519. idefloppy_floppy_t *floppy = drive->driver_data;
  520. struct ide_atapi_pc *pc;
  521. unsigned long block = (unsigned long)block_s;
  522. debug_log("dev: %s, cmd_type: %x, errors: %d\n",
  523. rq->rq_disk ? rq->rq_disk->disk_name : "?",
  524. rq->cmd_type, rq->errors);
  525. debug_log("sector: %ld, nr_sectors: %ld, "
  526. "current_nr_sectors: %d\n", (long)rq->sector,
  527. rq->nr_sectors, rq->current_nr_sectors);
  528. if (rq->errors >= ERROR_MAX) {
  529. if (floppy->failed_pc)
  530. ide_floppy_report_error(floppy, floppy->failed_pc);
  531. else
  532. printk(KERN_ERR "ide-floppy: %s: I/O error\n",
  533. drive->name);
  534. idefloppy_end_request(drive, 0, 0);
  535. return ide_stopped;
  536. }
  537. if (blk_fs_request(rq)) {
  538. if (((long)rq->sector % floppy->bs_factor) ||
  539. (rq->nr_sectors % floppy->bs_factor)) {
  540. printk(KERN_ERR "%s: unsupported r/w request size\n",
  541. drive->name);
  542. idefloppy_end_request(drive, 0, 0);
  543. return ide_stopped;
  544. }
  545. pc = idefloppy_next_pc_storage(drive);
  546. idefloppy_create_rw_cmd(floppy, pc, rq, block);
  547. } else if (blk_special_request(rq)) {
  548. pc = (struct ide_atapi_pc *) rq->buffer;
  549. } else if (blk_pc_request(rq)) {
  550. pc = idefloppy_next_pc_storage(drive);
  551. idefloppy_blockpc_cmd(floppy, pc, rq);
  552. } else {
  553. blk_dump_rq_flags(rq,
  554. "ide-floppy: unsupported command in queue");
  555. idefloppy_end_request(drive, 0, 0);
  556. return ide_stopped;
  557. }
  558. if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT)
  559. pc->flags |= PC_FLAG_DRQ_INTERRUPT;
  560. if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE)
  561. pc->flags |= PC_FLAG_ZIP_DRIVE;
  562. pc->rq = rq;
  563. return idefloppy_issue_pc(drive, pc);
  564. }
  565. /*
  566. * Add a special packet command request to the tail of the request queue,
  567. * and wait for it to be serviced.
  568. */
  569. static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
  570. {
  571. struct ide_floppy_obj *floppy = drive->driver_data;
  572. struct request *rq;
  573. int error;
  574. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  575. rq->buffer = (char *) pc;
  576. rq->cmd_type = REQ_TYPE_SPECIAL;
  577. error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
  578. blk_put_request(rq);
  579. return error;
  580. }
  581. /*
  582. * Look at the flexible disk page parameters. We ignore the CHS capacity
  583. * parameters and use the LBA parameters instead.
  584. */
  585. static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
  586. {
  587. idefloppy_floppy_t *floppy = drive->driver_data;
  588. struct ide_atapi_pc pc;
  589. u8 *page;
  590. int capacity, lba_capacity;
  591. u16 transfer_rate, sector_size, cyls, rpm;
  592. u8 heads, sectors;
  593. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
  594. MODE_SENSE_CURRENT);
  595. if (idefloppy_queue_pc_tail(drive, &pc)) {
  596. printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
  597. " parameters\n");
  598. return 1;
  599. }
  600. floppy->wp = !!(pc.buf[3] & 0x80);
  601. set_disk_ro(floppy->disk, floppy->wp);
  602. page = &pc.buf[8];
  603. transfer_rate = be16_to_cpu(*(u16 *)&pc.buf[8 + 2]);
  604. sector_size = be16_to_cpu(*(u16 *)&pc.buf[8 + 6]);
  605. cyls = be16_to_cpu(*(u16 *)&pc.buf[8 + 8]);
  606. rpm = be16_to_cpu(*(u16 *)&pc.buf[8 + 28]);
  607. heads = pc.buf[8 + 4];
  608. sectors = pc.buf[8 + 5];
  609. capacity = cyls * heads * sectors * sector_size;
  610. if (memcmp(page, &floppy->flexible_disk_page, 32))
  611. printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  612. "%d sector size, %d rpm\n",
  613. drive->name, capacity / 1024, cyls, heads,
  614. sectors, transfer_rate / 8, sector_size, rpm);
  615. memcpy(&floppy->flexible_disk_page, page, 32);
  616. drive->bios_cyl = cyls;
  617. drive->bios_head = heads;
  618. drive->bios_sect = sectors;
  619. lba_capacity = floppy->blocks * floppy->block_size;
  620. if (capacity < lba_capacity) {
  621. printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
  622. "bytes, but the drive only handles %d\n",
  623. drive->name, lba_capacity, capacity);
  624. floppy->blocks = floppy->block_size ?
  625. capacity / floppy->block_size : 0;
  626. }
  627. return 0;
  628. }
  629. static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
  630. {
  631. idefloppy_floppy_t *floppy = drive->driver_data;
  632. struct ide_atapi_pc pc;
  633. floppy->srfp = 0;
  634. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
  635. MODE_SENSE_CURRENT);
  636. pc.flags |= PC_FLAG_SUPPRESS_ERROR;
  637. if (idefloppy_queue_pc_tail(drive, &pc))
  638. return 1;
  639. floppy->srfp = pc.buf[8 + 2] & 0x40;
  640. return (0);
  641. }
  642. /*
  643. * Determine if a media is present in the floppy drive, and if so, its LBA
  644. * capacity.
  645. */
  646. static int ide_floppy_get_capacity(ide_drive_t *drive)
  647. {
  648. idefloppy_floppy_t *floppy = drive->driver_data;
  649. struct ide_atapi_pc pc;
  650. u8 *cap_desc;
  651. u8 header_len, desc_cnt;
  652. int i, rc = 1, blocks, length;
  653. drive->bios_cyl = 0;
  654. drive->bios_head = drive->bios_sect = 0;
  655. floppy->blocks = 0;
  656. floppy->bs_factor = 1;
  657. set_capacity(floppy->disk, 0);
  658. idefloppy_create_read_capacity_cmd(&pc);
  659. if (idefloppy_queue_pc_tail(drive, &pc)) {
  660. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  661. return 1;
  662. }
  663. header_len = pc.buf[3];
  664. cap_desc = &pc.buf[4];
  665. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  666. for (i = 0; i < desc_cnt; i++) {
  667. unsigned int desc_start = 4 + i*8;
  668. blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]);
  669. length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]);
  670. debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
  671. i, blocks * length / 1024, blocks, length);
  672. if (i)
  673. continue;
  674. /*
  675. * the code below is valid only for the 1st descriptor, ie i=0
  676. */
  677. switch (pc.buf[desc_start + 4] & 0x03) {
  678. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  679. case CAPACITY_UNFORMATTED:
  680. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
  681. /*
  682. * If it is not a clik drive, break out
  683. * (maintains previous driver behaviour)
  684. */
  685. break;
  686. case CAPACITY_CURRENT:
  687. /* Normal Zip/LS-120 disks */
  688. if (memcmp(cap_desc, &floppy->cap_desc, 8))
  689. printk(KERN_INFO "%s: %dkB, %d blocks, %d "
  690. "sector size\n", drive->name,
  691. blocks * length / 1024, blocks, length);
  692. memcpy(&floppy->cap_desc, cap_desc, 8);
  693. if (!length || length % 512) {
  694. printk(KERN_NOTICE "%s: %d bytes block size "
  695. "not supported\n", drive->name, length);
  696. } else {
  697. floppy->blocks = blocks;
  698. floppy->block_size = length;
  699. floppy->bs_factor = length / 512;
  700. if (floppy->bs_factor != 1)
  701. printk(KERN_NOTICE "%s: warning: non "
  702. "512 bytes block size not "
  703. "fully supported\n",
  704. drive->name);
  705. rc = 0;
  706. }
  707. break;
  708. case CAPACITY_NO_CARTRIDGE:
  709. /*
  710. * This is a KERN_ERR so it appears on screen
  711. * for the user to see
  712. */
  713. printk(KERN_ERR "%s: No disk in drive\n", drive->name);
  714. break;
  715. case CAPACITY_INVALID:
  716. printk(KERN_ERR "%s: Invalid capacity for disk "
  717. "in drive\n", drive->name);
  718. break;
  719. }
  720. debug_log("Descriptor 0 Code: %d\n",
  721. pc.buf[desc_start + 4] & 0x03);
  722. }
  723. /* Clik! disk does not support get_flexible_disk_page */
  724. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
  725. (void) ide_floppy_get_flexible_disk_page(drive);
  726. set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
  727. return rc;
  728. }
  729. /*
  730. * Obtain the list of formattable capacities.
  731. * Very similar to ide_floppy_get_capacity, except that we push the capacity
  732. * descriptors to userland, instead of our own structures.
  733. *
  734. * Userland gives us the following structure:
  735. *
  736. * struct idefloppy_format_capacities {
  737. * int nformats;
  738. * struct {
  739. * int nblocks;
  740. * int blocksize;
  741. * } formats[];
  742. * };
  743. *
  744. * userland initializes nformats to the number of allocated formats[] records.
  745. * On exit we set nformats to the number of records we've actually initialized.
  746. */
  747. static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
  748. {
  749. struct ide_atapi_pc pc;
  750. u8 header_len, desc_cnt;
  751. int i, blocks, length, u_array_size, u_index;
  752. int __user *argp;
  753. if (get_user(u_array_size, arg))
  754. return (-EFAULT);
  755. if (u_array_size <= 0)
  756. return (-EINVAL);
  757. idefloppy_create_read_capacity_cmd(&pc);
  758. if (idefloppy_queue_pc_tail(drive, &pc)) {
  759. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  760. return (-EIO);
  761. }
  762. header_len = pc.buf[3];
  763. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  764. u_index = 0;
  765. argp = arg + 1;
  766. /*
  767. * We always skip the first capacity descriptor. That's the current
  768. * capacity. We are interested in the remaining descriptors, the
  769. * formattable capacities.
  770. */
  771. for (i = 1; i < desc_cnt; i++) {
  772. unsigned int desc_start = 4 + i*8;
  773. if (u_index >= u_array_size)
  774. break; /* User-supplied buffer too small */
  775. blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]);
  776. length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]);
  777. if (put_user(blocks, argp))
  778. return(-EFAULT);
  779. ++argp;
  780. if (put_user(length, argp))
  781. return (-EFAULT);
  782. ++argp;
  783. ++u_index;
  784. }
  785. if (put_user(u_index, arg))
  786. return (-EFAULT);
  787. return (0);
  788. }
  789. /*
  790. * Get ATAPI_FORMAT_UNIT progress indication.
  791. *
  792. * Userland gives a pointer to an int. The int is set to a progress
  793. * indicator 0-65536, with 65536=100%.
  794. *
  795. * If the drive does not support format progress indication, we just check
  796. * the dsc bit, and return either 0 or 65536.
  797. */
  798. static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
  799. {
  800. idefloppy_floppy_t *floppy = drive->driver_data;
  801. struct ide_atapi_pc pc;
  802. int progress_indication = 0x10000;
  803. if (floppy->srfp) {
  804. idefloppy_create_request_sense_cmd(&pc);
  805. if (idefloppy_queue_pc_tail(drive, &pc))
  806. return (-EIO);
  807. if (floppy->sense_key == 2 &&
  808. floppy->asc == 4 &&
  809. floppy->ascq == 4)
  810. progress_indication = floppy->progress_indication;
  811. /* Else assume format_unit has finished, and we're at 0x10000 */
  812. } else {
  813. unsigned long flags;
  814. u8 stat;
  815. local_irq_save(flags);
  816. stat = ide_read_status(drive);
  817. local_irq_restore(flags);
  818. progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
  819. }
  820. if (put_user(progress_indication, arg))
  821. return (-EFAULT);
  822. return (0);
  823. }
  824. static sector_t idefloppy_capacity(ide_drive_t *drive)
  825. {
  826. idefloppy_floppy_t *floppy = drive->driver_data;
  827. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  828. return capacity;
  829. }
  830. /*
  831. * Check whether we can support a drive, based on the ATAPI IDENTIFY command
  832. * results.
  833. */
  834. static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
  835. {
  836. u8 gcw[2];
  837. u8 device_type, protocol, removable, drq_type, packet_size;
  838. *((u16 *) &gcw) = id->config;
  839. device_type = gcw[1] & 0x1F;
  840. removable = (gcw[0] & 0x80) >> 7;
  841. protocol = (gcw[1] & 0xC0) >> 6;
  842. drq_type = (gcw[0] & 0x60) >> 5;
  843. packet_size = gcw[0] & 0x03;
  844. #ifdef CONFIG_PPC
  845. /* kludge for Apple PowerBook internal zip */
  846. if (device_type == 5 &&
  847. !strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP"))
  848. device_type = 0;
  849. #endif
  850. if (protocol != 2)
  851. printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
  852. protocol);
  853. else if (device_type != 0)
  854. printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
  855. "to floppy\n", device_type);
  856. else if (!removable)
  857. printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
  858. else if (drq_type == 3)
  859. printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
  860. "supported\n", drq_type);
  861. else if (packet_size != 0)
  862. printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
  863. "bytes\n", packet_size);
  864. else
  865. return 1;
  866. return 0;
  867. }
  868. #ifdef CONFIG_IDE_PROC_FS
  869. static void idefloppy_add_settings(ide_drive_t *drive)
  870. {
  871. idefloppy_floppy_t *floppy = drive->driver_data;
  872. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
  873. &drive->bios_cyl, NULL);
  874. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
  875. &drive->bios_head, NULL);
  876. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
  877. &drive->bios_sect, NULL);
  878. ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
  879. &floppy->ticks, NULL);
  880. }
  881. #else
  882. static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
  883. #endif
  884. static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
  885. {
  886. u8 gcw[2];
  887. *((u16 *) &gcw) = drive->id->config;
  888. floppy->pc = floppy->pc_stack;
  889. if (((gcw[0] & 0x60) >> 5) == 1)
  890. floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
  891. /*
  892. * We used to check revisions here. At this point however I'm giving up.
  893. * Just assume they are all broken, its easier.
  894. *
  895. * The actual reason for the workarounds was likely a driver bug after
  896. * all rather than a firmware bug, and the workaround below used to hide
  897. * it. It should be fixed as of version 1.9, but to be on the safe side
  898. * we'll leave the limitation below for the 2.2.x tree.
  899. */
  900. if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
  901. floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
  902. /* This value will be visible in the /proc/ide/hdx/settings */
  903. floppy->ticks = IDEFLOPPY_TICKS_DELAY;
  904. blk_queue_max_sectors(drive->queue, 64);
  905. }
  906. /*
  907. * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
  908. * nasty clicking noises without it, so please don't remove this.
  909. */
  910. if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
  911. blk_queue_max_sectors(drive->queue, 64);
  912. floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
  913. }
  914. (void) ide_floppy_get_capacity(drive);
  915. idefloppy_add_settings(drive);
  916. }
  917. static void ide_floppy_remove(ide_drive_t *drive)
  918. {
  919. idefloppy_floppy_t *floppy = drive->driver_data;
  920. struct gendisk *g = floppy->disk;
  921. ide_proc_unregister_driver(drive, floppy->driver);
  922. del_gendisk(g);
  923. ide_floppy_put(floppy);
  924. }
  925. static void idefloppy_cleanup_obj(struct kref *kref)
  926. {
  927. struct ide_floppy_obj *floppy = to_ide_floppy(kref);
  928. ide_drive_t *drive = floppy->drive;
  929. struct gendisk *g = floppy->disk;
  930. drive->driver_data = NULL;
  931. g->private_data = NULL;
  932. put_disk(g);
  933. kfree(floppy);
  934. }
  935. #ifdef CONFIG_IDE_PROC_FS
  936. static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
  937. int count, int *eof, void *data)
  938. {
  939. ide_drive_t*drive = (ide_drive_t *)data;
  940. int len;
  941. len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
  942. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  943. }
  944. static ide_proc_entry_t idefloppy_proc[] = {
  945. { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
  946. { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
  947. { NULL, 0, NULL, NULL }
  948. };
  949. #endif /* CONFIG_IDE_PROC_FS */
  950. static int ide_floppy_probe(ide_drive_t *);
  951. static ide_driver_t idefloppy_driver = {
  952. .gen_driver = {
  953. .owner = THIS_MODULE,
  954. .name = "ide-floppy",
  955. .bus = &ide_bus_type,
  956. },
  957. .probe = ide_floppy_probe,
  958. .remove = ide_floppy_remove,
  959. .version = IDEFLOPPY_VERSION,
  960. .media = ide_floppy,
  961. .supports_dsc_overlap = 0,
  962. .do_request = idefloppy_do_request,
  963. .end_request = idefloppy_end_request,
  964. .error = __ide_error,
  965. .abort = __ide_abort,
  966. #ifdef CONFIG_IDE_PROC_FS
  967. .proc = idefloppy_proc,
  968. #endif
  969. };
  970. static int idefloppy_open(struct inode *inode, struct file *filp)
  971. {
  972. struct gendisk *disk = inode->i_bdev->bd_disk;
  973. struct ide_floppy_obj *floppy;
  974. ide_drive_t *drive;
  975. struct ide_atapi_pc pc;
  976. int ret = 0;
  977. debug_log("Reached %s\n", __func__);
  978. floppy = ide_floppy_get(disk);
  979. if (!floppy)
  980. return -ENXIO;
  981. drive = floppy->drive;
  982. floppy->openers++;
  983. if (floppy->openers == 1) {
  984. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  985. /* Just in case */
  986. idefloppy_create_test_unit_ready_cmd(&pc);
  987. if (idefloppy_queue_pc_tail(drive, &pc)) {
  988. idefloppy_create_start_stop_cmd(&pc, 1);
  989. (void) idefloppy_queue_pc_tail(drive, &pc);
  990. }
  991. if (ide_floppy_get_capacity(drive)
  992. && (filp->f_flags & O_NDELAY) == 0
  993. /*
  994. * Allow O_NDELAY to open a drive without a disk, or with an
  995. * unreadable disk, so that we can get the format capacity
  996. * of the drive or begin the format - Sam
  997. */
  998. ) {
  999. ret = -EIO;
  1000. goto out_put_floppy;
  1001. }
  1002. if (floppy->wp && (filp->f_mode & 2)) {
  1003. ret = -EROFS;
  1004. goto out_put_floppy;
  1005. }
  1006. floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
  1007. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1008. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1009. idefloppy_create_prevent_cmd(&pc, 1);
  1010. (void) idefloppy_queue_pc_tail(drive, &pc);
  1011. }
  1012. check_disk_change(inode->i_bdev);
  1013. } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
  1014. ret = -EBUSY;
  1015. goto out_put_floppy;
  1016. }
  1017. return 0;
  1018. out_put_floppy:
  1019. floppy->openers--;
  1020. ide_floppy_put(floppy);
  1021. return ret;
  1022. }
  1023. static int idefloppy_release(struct inode *inode, struct file *filp)
  1024. {
  1025. struct gendisk *disk = inode->i_bdev->bd_disk;
  1026. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1027. ide_drive_t *drive = floppy->drive;
  1028. struct ide_atapi_pc pc;
  1029. debug_log("Reached %s\n", __func__);
  1030. if (floppy->openers == 1) {
  1031. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1032. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1033. idefloppy_create_prevent_cmd(&pc, 0);
  1034. (void) idefloppy_queue_pc_tail(drive, &pc);
  1035. }
  1036. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1037. }
  1038. floppy->openers--;
  1039. ide_floppy_put(floppy);
  1040. return 0;
  1041. }
  1042. static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1043. {
  1044. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1045. ide_drive_t *drive = floppy->drive;
  1046. geo->heads = drive->bios_head;
  1047. geo->sectors = drive->bios_sect;
  1048. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  1049. return 0;
  1050. }
  1051. static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy,
  1052. struct ide_atapi_pc *pc, unsigned long arg, unsigned int cmd)
  1053. {
  1054. if (floppy->openers > 1)
  1055. return -EBUSY;
  1056. /* The IOMEGA Clik! Drive doesn't support this command -
  1057. * no room for an eject mechanism */
  1058. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1059. int prevent = arg ? 1 : 0;
  1060. if (cmd == CDROMEJECT)
  1061. prevent = 0;
  1062. idefloppy_create_prevent_cmd(pc, prevent);
  1063. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1064. }
  1065. if (cmd == CDROMEJECT) {
  1066. idefloppy_create_start_stop_cmd(pc, 2);
  1067. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1068. }
  1069. return 0;
  1070. }
  1071. static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
  1072. int __user *arg)
  1073. {
  1074. int blocks, length, flags, err = 0;
  1075. struct ide_atapi_pc pc;
  1076. if (floppy->openers > 1) {
  1077. /* Don't format if someone is using the disk */
  1078. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1079. return -EBUSY;
  1080. }
  1081. floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1082. /*
  1083. * Send ATAPI_FORMAT_UNIT to the drive.
  1084. *
  1085. * Userland gives us the following structure:
  1086. *
  1087. * struct idefloppy_format_command {
  1088. * int nblocks;
  1089. * int blocksize;
  1090. * int flags;
  1091. * } ;
  1092. *
  1093. * flags is a bitmask, currently, the only defined flag is:
  1094. *
  1095. * 0x01 - verify media after format.
  1096. */
  1097. if (get_user(blocks, arg) ||
  1098. get_user(length, arg+1) ||
  1099. get_user(flags, arg+2)) {
  1100. err = -EFAULT;
  1101. goto out;
  1102. }
  1103. (void) idefloppy_get_sfrp_bit(floppy->drive);
  1104. idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
  1105. if (idefloppy_queue_pc_tail(floppy->drive, &pc))
  1106. err = -EIO;
  1107. out:
  1108. if (err)
  1109. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1110. return err;
  1111. }
  1112. static int idefloppy_ioctl(struct inode *inode, struct file *file,
  1113. unsigned int cmd, unsigned long arg)
  1114. {
  1115. struct block_device *bdev = inode->i_bdev;
  1116. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1117. ide_drive_t *drive = floppy->drive;
  1118. struct ide_atapi_pc pc;
  1119. void __user *argp = (void __user *)arg;
  1120. int err;
  1121. switch (cmd) {
  1122. case CDROMEJECT:
  1123. /* fall through */
  1124. case CDROM_LOCKDOOR:
  1125. return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
  1126. case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
  1127. return 0;
  1128. case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
  1129. return ide_floppy_get_format_capacities(drive, argp);
  1130. case IDEFLOPPY_IOCTL_FORMAT_START:
  1131. if (!(file->f_mode & 2))
  1132. return -EPERM;
  1133. return ide_floppy_format_unit(floppy, (int __user *)arg);
  1134. case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
  1135. return idefloppy_get_format_progress(drive, argp);
  1136. }
  1137. /*
  1138. * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
  1139. * and CDROM_SEND_PACKET (legacy) ioctls
  1140. */
  1141. if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
  1142. err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
  1143. bdev->bd_disk, cmd, argp);
  1144. else
  1145. err = -ENOTTY;
  1146. if (err == -ENOTTY)
  1147. err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  1148. return err;
  1149. }
  1150. static int idefloppy_media_changed(struct gendisk *disk)
  1151. {
  1152. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1153. ide_drive_t *drive = floppy->drive;
  1154. int ret;
  1155. /* do not scan partitions twice if this is a removable device */
  1156. if (drive->attach) {
  1157. drive->attach = 0;
  1158. return 0;
  1159. }
  1160. ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
  1161. floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
  1162. return ret;
  1163. }
  1164. static int idefloppy_revalidate_disk(struct gendisk *disk)
  1165. {
  1166. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1167. set_capacity(disk, idefloppy_capacity(floppy->drive));
  1168. return 0;
  1169. }
  1170. static struct block_device_operations idefloppy_ops = {
  1171. .owner = THIS_MODULE,
  1172. .open = idefloppy_open,
  1173. .release = idefloppy_release,
  1174. .ioctl = idefloppy_ioctl,
  1175. .getgeo = idefloppy_getgeo,
  1176. .media_changed = idefloppy_media_changed,
  1177. .revalidate_disk = idefloppy_revalidate_disk
  1178. };
  1179. static int ide_floppy_probe(ide_drive_t *drive)
  1180. {
  1181. idefloppy_floppy_t *floppy;
  1182. struct gendisk *g;
  1183. if (!strstr("ide-floppy", drive->driver_req))
  1184. goto failed;
  1185. if (!drive->present)
  1186. goto failed;
  1187. if (drive->media != ide_floppy)
  1188. goto failed;
  1189. if (!idefloppy_identify_device(drive, drive->id)) {
  1190. printk(KERN_ERR "ide-floppy: %s: not supported by this version"
  1191. " of ide-floppy\n", drive->name);
  1192. goto failed;
  1193. }
  1194. floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
  1195. if (!floppy) {
  1196. printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
  1197. " structure\n", drive->name);
  1198. goto failed;
  1199. }
  1200. g = alloc_disk(1 << PARTN_BITS);
  1201. if (!g)
  1202. goto out_free_floppy;
  1203. ide_init_disk(g, drive);
  1204. ide_proc_register_driver(drive, &idefloppy_driver);
  1205. kref_init(&floppy->kref);
  1206. floppy->drive = drive;
  1207. floppy->driver = &idefloppy_driver;
  1208. floppy->disk = g;
  1209. g->private_data = &floppy->driver;
  1210. drive->driver_data = floppy;
  1211. idefloppy_setup(drive, floppy);
  1212. g->minors = 1 << PARTN_BITS;
  1213. g->driverfs_dev = &drive->gendev;
  1214. g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
  1215. g->fops = &idefloppy_ops;
  1216. drive->attach = 1;
  1217. add_disk(g);
  1218. return 0;
  1219. out_free_floppy:
  1220. kfree(floppy);
  1221. failed:
  1222. return -ENODEV;
  1223. }
  1224. static void __exit idefloppy_exit(void)
  1225. {
  1226. driver_unregister(&idefloppy_driver.gen_driver);
  1227. }
  1228. static int __init idefloppy_init(void)
  1229. {
  1230. printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
  1231. return driver_register(&idefloppy_driver.gen_driver);
  1232. }
  1233. MODULE_ALIAS("ide:*m-floppy*");
  1234. module_init(idefloppy_init);
  1235. module_exit(idefloppy_exit);
  1236. MODULE_LICENSE("GPL");
  1237. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");