ide-floppy.c 16 KB

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