ide-floppy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. #include <linux/types.h>
  18. #include <linux/string.h>
  19. #include <linux/kernel.h>
  20. #include <linux/delay.h>
  21. #include <linux/timer.h>
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/major.h>
  25. #include <linux/errno.h>
  26. #include <linux/genhd.h>
  27. #include <linux/cdrom.h>
  28. #include <linux/ide.h>
  29. #include <linux/hdreg.h>
  30. #include <linux/bitops.h>
  31. #include <linux/mutex.h>
  32. #include <linux/scatterlist.h>
  33. #include <scsi/scsi_ioctl.h>
  34. #include <asm/byteorder.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/io.h>
  37. #include <asm/unaligned.h>
  38. #include "ide-floppy.h"
  39. /*
  40. * After each failed packet command we issue a request sense command and retry
  41. * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
  42. */
  43. #define IDEFLOPPY_MAX_PC_RETRIES 3
  44. /* format capacities descriptor codes */
  45. #define CAPACITY_INVALID 0x00
  46. #define CAPACITY_UNFORMATTED 0x01
  47. #define CAPACITY_CURRENT 0x02
  48. #define CAPACITY_NO_CARTRIDGE 0x03
  49. /*
  50. * The following delay solves a problem with ATAPI Zip 100 drive where BSY bit
  51. * was apparently being deasserted before the unit was ready to receive data.
  52. */
  53. #define IDEFLOPPY_PC_DELAY (HZ/20) /* default delay for ZIP 100 (50ms) */
  54. static int ide_floppy_callback(ide_drive_t *drive, int dsc)
  55. {
  56. struct ide_disk_obj *floppy = drive->driver_data;
  57. struct ide_atapi_pc *pc = drive->pc;
  58. struct request *rq = pc->rq;
  59. int uptodate = pc->error ? 0 : 1;
  60. ide_debug_log(IDE_DBG_FUNC, "enter");
  61. if (drive->failed_pc == pc)
  62. drive->failed_pc = NULL;
  63. if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
  64. rq->cmd_type == REQ_TYPE_BLOCK_PC)
  65. uptodate = 1; /* FIXME */
  66. else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
  67. u8 *buf = bio_data(rq->bio);
  68. if (!pc->error) {
  69. floppy->sense_key = buf[2] & 0x0F;
  70. floppy->asc = buf[12];
  71. floppy->ascq = buf[13];
  72. floppy->progress_indication = buf[15] & 0x80 ?
  73. (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
  74. if (drive->failed_pc)
  75. ide_debug_log(IDE_DBG_PC, "pc = %x",
  76. drive->failed_pc->c[0]);
  77. ide_debug_log(IDE_DBG_SENSE, "sense key = %x, asc = %x,"
  78. "ascq = %x", floppy->sense_key,
  79. floppy->asc, floppy->ascq);
  80. } else
  81. printk(KERN_ERR PFX "Error in REQUEST SENSE itself - "
  82. "Aborting request!\n");
  83. }
  84. if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
  85. rq->errors = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
  86. return uptodate;
  87. }
  88. static void ide_floppy_report_error(struct ide_disk_obj *floppy,
  89. struct ide_atapi_pc *pc)
  90. {
  91. /* suppress error messages resulting from Medium not present */
  92. if (floppy->sense_key == 0x02 &&
  93. floppy->asc == 0x3a &&
  94. floppy->ascq == 0x00)
  95. return;
  96. printk(KERN_ERR PFX "%s: I/O error, pc = %2x, key = %2x, "
  97. "asc = %2x, ascq = %2x\n",
  98. floppy->drive->name, pc->c[0], floppy->sense_key,
  99. floppy->asc, floppy->ascq);
  100. }
  101. static ide_startstop_t ide_floppy_issue_pc(ide_drive_t *drive,
  102. struct ide_cmd *cmd,
  103. struct ide_atapi_pc *pc)
  104. {
  105. struct ide_disk_obj *floppy = drive->driver_data;
  106. if (drive->failed_pc == NULL &&
  107. pc->c[0] != GPCMD_REQUEST_SENSE)
  108. drive->failed_pc = pc;
  109. /* Set the current packet command */
  110. drive->pc = pc;
  111. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
  112. unsigned int done = blk_rq_bytes(drive->hwif->rq);
  113. if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
  114. ide_floppy_report_error(floppy, pc);
  115. /* Giving up */
  116. pc->error = IDE_DRV_ERROR_GENERAL;
  117. drive->failed_pc = NULL;
  118. drive->pc_callback(drive, 0);
  119. ide_complete_rq(drive, -EIO, done);
  120. return ide_stopped;
  121. }
  122. ide_debug_log(IDE_DBG_FUNC, "retry #%d", pc->retries);
  123. pc->retries++;
  124. return ide_issue_pc(drive, cmd);
  125. }
  126. void ide_floppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
  127. {
  128. ide_init_pc(pc);
  129. pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
  130. pc->c[7] = 255;
  131. pc->c[8] = 255;
  132. pc->req_xfer = 255;
  133. }
  134. /* A mode sense command is used to "sense" floppy parameters. */
  135. void ide_floppy_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
  136. {
  137. u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
  138. ide_init_pc(pc);
  139. pc->c[0] = GPCMD_MODE_SENSE_10;
  140. pc->c[1] = 0;
  141. pc->c[2] = page_code;
  142. switch (page_code) {
  143. case IDEFLOPPY_CAPABILITIES_PAGE:
  144. length += 12;
  145. break;
  146. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  147. length += 32;
  148. break;
  149. default:
  150. printk(KERN_ERR PFX "unsupported page code in %s\n", __func__);
  151. }
  152. put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
  153. pc->req_xfer = length;
  154. }
  155. static void idefloppy_create_rw_cmd(ide_drive_t *drive,
  156. struct ide_atapi_pc *pc, struct request *rq,
  157. unsigned long sector)
  158. {
  159. struct ide_disk_obj *floppy = drive->driver_data;
  160. int block = sector / floppy->bs_factor;
  161. int blocks = blk_rq_sectors(rq) / floppy->bs_factor;
  162. int cmd = rq_data_dir(rq);
  163. ide_debug_log(IDE_DBG_FUNC, "block: %d, blocks: %d", block, blocks);
  164. ide_init_pc(pc);
  165. pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
  166. put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
  167. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
  168. memcpy(rq->cmd, pc->c, 12);
  169. pc->rq = rq;
  170. if (rq->cmd_flags & REQ_WRITE)
  171. pc->flags |= PC_FLAG_WRITING;
  172. pc->flags |= PC_FLAG_DMA_OK;
  173. }
  174. static void idefloppy_blockpc_cmd(struct ide_disk_obj *floppy,
  175. struct ide_atapi_pc *pc, struct request *rq)
  176. {
  177. ide_init_pc(pc);
  178. memcpy(pc->c, rq->cmd, sizeof(pc->c));
  179. pc->rq = rq;
  180. if (blk_rq_bytes(rq)) {
  181. pc->flags |= PC_FLAG_DMA_OK;
  182. if (rq_data_dir(rq) == WRITE)
  183. pc->flags |= PC_FLAG_WRITING;
  184. }
  185. }
  186. static ide_startstop_t ide_floppy_do_request(ide_drive_t *drive,
  187. struct request *rq, sector_t block)
  188. {
  189. struct ide_disk_obj *floppy = drive->driver_data;
  190. struct ide_cmd cmd;
  191. struct ide_atapi_pc *pc;
  192. ide_debug_log(IDE_DBG_FUNC, "enter, cmd: 0x%x\n", rq->cmd[0]);
  193. if (drive->debug_mask & IDE_DBG_RQ)
  194. blk_dump_rq_flags(rq, (rq->rq_disk
  195. ? rq->rq_disk->disk_name
  196. : "dev?"));
  197. if (rq->errors >= ERROR_MAX) {
  198. if (drive->failed_pc) {
  199. ide_floppy_report_error(floppy, drive->failed_pc);
  200. drive->failed_pc = NULL;
  201. } else
  202. printk(KERN_ERR PFX "%s: I/O error\n", drive->name);
  203. if (rq->cmd_type == REQ_TYPE_DRV_PRIV) {
  204. rq->errors = 0;
  205. ide_complete_rq(drive, 0, blk_rq_bytes(rq));
  206. return ide_stopped;
  207. } else
  208. goto out_end;
  209. }
  210. switch (rq->cmd_type) {
  211. case REQ_TYPE_FS:
  212. if (((long)blk_rq_pos(rq) % floppy->bs_factor) ||
  213. (blk_rq_sectors(rq) % floppy->bs_factor)) {
  214. printk(KERN_ERR PFX "%s: unsupported r/w rq size\n",
  215. drive->name);
  216. goto out_end;
  217. }
  218. pc = &floppy->queued_pc;
  219. idefloppy_create_rw_cmd(drive, pc, rq, (unsigned long)block);
  220. break;
  221. case REQ_TYPE_DRV_PRIV:
  222. case REQ_TYPE_ATA_SENSE:
  223. pc = (struct ide_atapi_pc *)rq->special;
  224. break;
  225. case REQ_TYPE_BLOCK_PC:
  226. pc = &floppy->queued_pc;
  227. idefloppy_blockpc_cmd(floppy, pc, rq);
  228. break;
  229. default:
  230. BUG();
  231. }
  232. ide_prep_sense(drive, rq);
  233. memset(&cmd, 0, sizeof(cmd));
  234. if (rq_data_dir(rq))
  235. cmd.tf_flags |= IDE_TFLAG_WRITE;
  236. cmd.rq = rq;
  237. if (rq->cmd_type == REQ_TYPE_FS || blk_rq_bytes(rq)) {
  238. ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
  239. ide_map_sg(drive, &cmd);
  240. }
  241. pc->rq = rq;
  242. return ide_floppy_issue_pc(drive, &cmd, pc);
  243. out_end:
  244. drive->failed_pc = NULL;
  245. if (rq->cmd_type != REQ_TYPE_FS && rq->errors == 0)
  246. rq->errors = -EIO;
  247. ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
  248. return ide_stopped;
  249. }
  250. /*
  251. * Look at the flexible disk page parameters. We ignore the CHS capacity
  252. * parameters and use the LBA parameters instead.
  253. */
  254. static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive,
  255. struct ide_atapi_pc *pc)
  256. {
  257. struct ide_disk_obj *floppy = drive->driver_data;
  258. struct gendisk *disk = floppy->disk;
  259. u8 *page, buf[40];
  260. int capacity, lba_capacity;
  261. u16 transfer_rate, sector_size, cyls, rpm;
  262. u8 heads, sectors;
  263. ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
  264. if (ide_queue_pc_tail(drive, disk, pc, buf, pc->req_xfer)) {
  265. printk(KERN_ERR PFX "Can't get flexible disk page params\n");
  266. return 1;
  267. }
  268. if (buf[3] & 0x80)
  269. drive->dev_flags |= IDE_DFLAG_WP;
  270. else
  271. drive->dev_flags &= ~IDE_DFLAG_WP;
  272. set_disk_ro(disk, !!(drive->dev_flags & IDE_DFLAG_WP));
  273. page = &buf[8];
  274. transfer_rate = be16_to_cpup((__be16 *)&buf[8 + 2]);
  275. sector_size = be16_to_cpup((__be16 *)&buf[8 + 6]);
  276. cyls = be16_to_cpup((__be16 *)&buf[8 + 8]);
  277. rpm = be16_to_cpup((__be16 *)&buf[8 + 28]);
  278. heads = buf[8 + 4];
  279. sectors = buf[8 + 5];
  280. capacity = cyls * heads * sectors * sector_size;
  281. if (memcmp(page, &floppy->flexible_disk_page, 32))
  282. printk(KERN_INFO PFX "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  283. "%d sector size, %d rpm\n",
  284. drive->name, capacity / 1024, cyls, heads,
  285. sectors, transfer_rate / 8, sector_size, rpm);
  286. memcpy(&floppy->flexible_disk_page, page, 32);
  287. drive->bios_cyl = cyls;
  288. drive->bios_head = heads;
  289. drive->bios_sect = sectors;
  290. lba_capacity = floppy->blocks * floppy->block_size;
  291. if (capacity < lba_capacity) {
  292. printk(KERN_NOTICE PFX "%s: The disk reports a capacity of %d "
  293. "bytes, but the drive only handles %d\n",
  294. drive->name, lba_capacity, capacity);
  295. floppy->blocks = floppy->block_size ?
  296. capacity / floppy->block_size : 0;
  297. drive->capacity64 = floppy->blocks * floppy->bs_factor;
  298. }
  299. return 0;
  300. }
  301. /*
  302. * Determine if a media is present in the floppy drive, and if so, its LBA
  303. * capacity.
  304. */
  305. static int ide_floppy_get_capacity(ide_drive_t *drive)
  306. {
  307. struct ide_disk_obj *floppy = drive->driver_data;
  308. struct gendisk *disk = floppy->disk;
  309. struct ide_atapi_pc pc;
  310. u8 *cap_desc;
  311. u8 pc_buf[256], header_len, desc_cnt;
  312. int i, rc = 1, blocks, length;
  313. ide_debug_log(IDE_DBG_FUNC, "enter");
  314. drive->bios_cyl = 0;
  315. drive->bios_head = drive->bios_sect = 0;
  316. floppy->blocks = 0;
  317. floppy->bs_factor = 1;
  318. drive->capacity64 = 0;
  319. ide_floppy_create_read_capacity_cmd(&pc);
  320. if (ide_queue_pc_tail(drive, disk, &pc, pc_buf, pc.req_xfer)) {
  321. printk(KERN_ERR PFX "Can't get floppy parameters\n");
  322. return 1;
  323. }
  324. header_len = pc_buf[3];
  325. cap_desc = &pc_buf[4];
  326. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  327. for (i = 0; i < desc_cnt; i++) {
  328. unsigned int desc_start = 4 + i*8;
  329. blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]);
  330. length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]);
  331. ide_debug_log(IDE_DBG_PROBE, "Descriptor %d: %dkB, %d blocks, "
  332. "%d sector size",
  333. i, blocks * length / 1024,
  334. blocks, length);
  335. if (i)
  336. continue;
  337. /*
  338. * the code below is valid only for the 1st descriptor, ie i=0
  339. */
  340. switch (pc_buf[desc_start + 4] & 0x03) {
  341. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  342. case CAPACITY_UNFORMATTED:
  343. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
  344. /*
  345. * If it is not a clik drive, break out
  346. * (maintains previous driver behaviour)
  347. */
  348. break;
  349. case CAPACITY_CURRENT:
  350. /* Normal Zip/LS-120 disks */
  351. if (memcmp(cap_desc, &floppy->cap_desc, 8))
  352. printk(KERN_INFO PFX "%s: %dkB, %d blocks, %d "
  353. "sector size\n",
  354. drive->name, blocks * length / 1024,
  355. blocks, length);
  356. memcpy(&floppy->cap_desc, cap_desc, 8);
  357. if (!length || length % 512) {
  358. printk(KERN_NOTICE PFX "%s: %d bytes block size"
  359. " not supported\n", drive->name, length);
  360. } else {
  361. floppy->blocks = blocks;
  362. floppy->block_size = length;
  363. floppy->bs_factor = length / 512;
  364. if (floppy->bs_factor != 1)
  365. printk(KERN_NOTICE PFX "%s: Warning: "
  366. "non 512 bytes block size not "
  367. "fully supported\n",
  368. drive->name);
  369. drive->capacity64 =
  370. floppy->blocks * floppy->bs_factor;
  371. rc = 0;
  372. }
  373. break;
  374. case CAPACITY_NO_CARTRIDGE:
  375. /*
  376. * This is a KERN_ERR so it appears on screen
  377. * for the user to see
  378. */
  379. printk(KERN_ERR PFX "%s: No disk in drive\n",
  380. drive->name);
  381. break;
  382. case CAPACITY_INVALID:
  383. printk(KERN_ERR PFX "%s: Invalid capacity for disk "
  384. "in drive\n", drive->name);
  385. break;
  386. }
  387. ide_debug_log(IDE_DBG_PROBE, "Descriptor 0 Code: %d",
  388. pc_buf[desc_start + 4] & 0x03);
  389. }
  390. /* Clik! disk does not support get_flexible_disk_page */
  391. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
  392. (void) ide_floppy_get_flexible_disk_page(drive, &pc);
  393. return rc;
  394. }
  395. static void ide_floppy_setup(ide_drive_t *drive)
  396. {
  397. struct ide_disk_obj *floppy = drive->driver_data;
  398. u16 *id = drive->id;
  399. drive->pc_callback = ide_floppy_callback;
  400. /*
  401. * We used to check revisions here. At this point however I'm giving up.
  402. * Just assume they are all broken, its easier.
  403. *
  404. * The actual reason for the workarounds was likely a driver bug after
  405. * all rather than a firmware bug, and the workaround below used to hide
  406. * it. It should be fixed as of version 1.9, but to be on the safe side
  407. * we'll leave the limitation below for the 2.2.x tree.
  408. */
  409. if (strstarts((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI")) {
  410. drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
  411. /* This value will be visible in the /proc/ide/hdx/settings */
  412. drive->pc_delay = IDEFLOPPY_PC_DELAY;
  413. blk_queue_max_hw_sectors(drive->queue, 64);
  414. }
  415. /*
  416. * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
  417. * nasty clicking noises without it, so please don't remove this.
  418. */
  419. if (strstarts((char *)&id[ATA_ID_PROD], "IOMEGA Clik!")) {
  420. blk_queue_max_hw_sectors(drive->queue, 64);
  421. drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
  422. /* IOMEGA Clik! drives do not support lock/unlock commands */
  423. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  424. }
  425. (void) ide_floppy_get_capacity(drive);
  426. ide_proc_register_driver(drive, floppy->driver);
  427. drive->dev_flags |= IDE_DFLAG_ATTACH;
  428. }
  429. static void ide_floppy_flush(ide_drive_t *drive)
  430. {
  431. }
  432. static int ide_floppy_init_media(ide_drive_t *drive, struct gendisk *disk)
  433. {
  434. int ret = 0;
  435. if (ide_do_test_unit_ready(drive, disk))
  436. ide_do_start_stop(drive, disk, 1);
  437. ret = ide_floppy_get_capacity(drive);
  438. set_capacity(disk, ide_gd_capacity(drive));
  439. return ret;
  440. }
  441. const struct ide_disk_ops ide_atapi_disk_ops = {
  442. .check = ide_check_atapi_device,
  443. .get_capacity = ide_floppy_get_capacity,
  444. .setup = ide_floppy_setup,
  445. .flush = ide_floppy_flush,
  446. .init_media = ide_floppy_init_media,
  447. .set_doorlock = ide_set_media_lock,
  448. .do_request = ide_floppy_do_request,
  449. .ioctl = ide_floppy_ioctl,
  450. };