ide-floppy.c 15 KB

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