ide-floppy.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 DRV_NAME "ide-floppy"
  18. #define IDEFLOPPY_VERSION "1.00"
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/string.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/timer.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/major.h>
  28. #include <linux/errno.h>
  29. #include <linux/genhd.h>
  30. #include <linux/slab.h>
  31. #include <linux/cdrom.h>
  32. #include <linux/ide.h>
  33. #include <linux/hdreg.h>
  34. #include <linux/bitops.h>
  35. #include <linux/mutex.h>
  36. #include <linux/scatterlist.h>
  37. #include <scsi/scsi_ioctl.h>
  38. #include <asm/byteorder.h>
  39. #include <linux/irq.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/io.h>
  42. #include <asm/unaligned.h>
  43. #include "ide-floppy.h"
  44. /* define to see debug info */
  45. #define IDEFLOPPY_DEBUG_LOG 0
  46. /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
  47. #define IDEFLOPPY_DEBUG(fmt, args...)
  48. #if IDEFLOPPY_DEBUG_LOG
  49. #define debug_log(fmt, args...) \
  50. printk(KERN_INFO "ide-floppy: " fmt, ## args)
  51. #else
  52. #define debug_log(fmt, args...) do {} while (0)
  53. #endif
  54. /* Some drives require a longer irq timeout. */
  55. #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
  56. /*
  57. * After each failed packet command we issue a request sense command and retry
  58. * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
  59. */
  60. #define IDEFLOPPY_MAX_PC_RETRIES 3
  61. /* format capacities descriptor codes */
  62. #define CAPACITY_INVALID 0x00
  63. #define CAPACITY_UNFORMATTED 0x01
  64. #define CAPACITY_CURRENT 0x02
  65. #define CAPACITY_NO_CARTRIDGE 0x03
  66. #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
  67. /* Error code returned in rq->errors to the higher part of the driver. */
  68. #define IDEFLOPPY_ERROR_GENERAL 101
  69. static DEFINE_MUTEX(idefloppy_ref_mutex);
  70. #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
  71. #define ide_floppy_g(disk) \
  72. container_of((disk)->private_data, struct ide_floppy_obj, driver)
  73. static void idefloppy_cleanup_obj(struct kref *);
  74. static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
  75. {
  76. struct ide_floppy_obj *floppy = NULL;
  77. mutex_lock(&idefloppy_ref_mutex);
  78. floppy = ide_floppy_g(disk);
  79. if (floppy) {
  80. if (ide_device_get(floppy->drive))
  81. floppy = NULL;
  82. else
  83. kref_get(&floppy->kref);
  84. }
  85. mutex_unlock(&idefloppy_ref_mutex);
  86. return floppy;
  87. }
  88. static void ide_floppy_put(struct ide_floppy_obj *floppy)
  89. {
  90. ide_drive_t *drive = floppy->drive;
  91. mutex_lock(&idefloppy_ref_mutex);
  92. kref_put(&floppy->kref, idefloppy_cleanup_obj);
  93. ide_device_put(drive);
  94. mutex_unlock(&idefloppy_ref_mutex);
  95. }
  96. /*
  97. * Used to finish servicing a request. For read/write requests, we will call
  98. * ide_end_request to pass to the next buffer.
  99. */
  100. static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
  101. {
  102. idefloppy_floppy_t *floppy = drive->driver_data;
  103. struct request *rq = HWGROUP(drive)->rq;
  104. int error;
  105. debug_log("Reached %s\n", __func__);
  106. switch (uptodate) {
  107. case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
  108. case 1: error = 0; break;
  109. default: error = uptodate;
  110. }
  111. if (error)
  112. floppy->failed_pc = NULL;
  113. /* Why does this happen? */
  114. if (!rq)
  115. return 0;
  116. if (!blk_special_request(rq)) {
  117. /* our real local end request function */
  118. ide_end_request(drive, uptodate, nsecs);
  119. return 0;
  120. }
  121. rq->errors = error;
  122. /* fixme: need to move this local also */
  123. ide_end_drive_cmd(drive, 0, 0);
  124. return 0;
  125. }
  126. static void idefloppy_update_buffers(ide_drive_t *drive,
  127. struct ide_atapi_pc *pc)
  128. {
  129. struct request *rq = pc->rq;
  130. struct bio *bio = rq->bio;
  131. while ((bio = rq->bio) != NULL)
  132. idefloppy_end_request(drive, 1, 0);
  133. }
  134. static void ide_floppy_callback(ide_drive_t *drive)
  135. {
  136. idefloppy_floppy_t *floppy = drive->driver_data;
  137. struct ide_atapi_pc *pc = floppy->pc;
  138. int uptodate = pc->error ? 0 : 1;
  139. debug_log("Reached %s\n", __func__);
  140. if (floppy->failed_pc == pc)
  141. floppy->failed_pc = NULL;
  142. if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
  143. (pc->rq && blk_pc_request(pc->rq)))
  144. uptodate = 1; /* FIXME */
  145. else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
  146. u8 *buf = floppy->pc->buf;
  147. if (!pc->error) {
  148. floppy->sense_key = buf[2] & 0x0F;
  149. floppy->asc = buf[12];
  150. floppy->ascq = buf[13];
  151. floppy->progress_indication = buf[15] & 0x80 ?
  152. (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
  153. if (floppy->failed_pc)
  154. debug_log("pc = %x, ", floppy->failed_pc->c[0]);
  155. debug_log("sense key = %x, asc = %x, ascq = %x\n",
  156. floppy->sense_key, floppy->asc, floppy->ascq);
  157. } else
  158. printk(KERN_ERR "Error in REQUEST SENSE itself - "
  159. "Aborting request!\n");
  160. }
  161. idefloppy_end_request(drive, uptodate, 0);
  162. }
  163. void ide_floppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
  164. {
  165. ide_init_pc(pc);
  166. pc->c[0] = GPCMD_REQUEST_SENSE;
  167. pc->c[4] = 255;
  168. pc->req_xfer = 18;
  169. }
  170. /*
  171. * Called when an error was detected during the last packet command. We queue a
  172. * request sense packet command in the head of the request list.
  173. */
  174. static void idefloppy_retry_pc(ide_drive_t *drive)
  175. {
  176. struct ide_floppy_obj *floppy = drive->driver_data;
  177. struct request *rq = &floppy->request_sense_rq;
  178. struct ide_atapi_pc *pc = &floppy->request_sense_pc;
  179. (void)ide_read_error(drive);
  180. ide_floppy_create_request_sense_cmd(pc);
  181. ide_queue_pc_head(drive, floppy->disk, pc, rq);
  182. }
  183. /* The usual interrupt handler called during a packet command. */
  184. static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
  185. {
  186. idefloppy_floppy_t *floppy = drive->driver_data;
  187. return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
  188. IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
  189. idefloppy_retry_pc, NULL, ide_io_buffers);
  190. }
  191. /*
  192. * What we have here is a classic case of a top half / bottom half interrupt
  193. * service routine. In interrupt mode, the device sends an interrupt to signal
  194. * that it is ready to receive a packet. However, we need to delay about 2-3
  195. * ticks before issuing the packet or we gets in trouble.
  196. */
  197. static int idefloppy_transfer_pc(ide_drive_t *drive)
  198. {
  199. idefloppy_floppy_t *floppy = drive->driver_data;
  200. /* Send the actual packet */
  201. drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
  202. /* Timeout for the packet command */
  203. return IDEFLOPPY_WAIT_CMD;
  204. }
  205. /*
  206. * Called as an interrupt (or directly). When the device says it's ready for a
  207. * packet, we schedule the packet transfer to occur about 2-3 ticks later in
  208. * transfer_pc.
  209. */
  210. static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
  211. {
  212. idefloppy_floppy_t *floppy = drive->driver_data;
  213. struct ide_atapi_pc *pc = floppy->pc;
  214. ide_expiry_t *expiry;
  215. unsigned int timeout;
  216. /*
  217. * The following delay solves a problem with ATAPI Zip 100 drives
  218. * where the Busy flag was apparently being deasserted before the
  219. * unit was ready to receive data. This was happening on a
  220. * 1200 MHz Athlon system. 10/26/01 25msec is too short,
  221. * 40 and 50msec work well. idefloppy_pc_intr will not be actually
  222. * used until after the packet is moved in about 50 msec.
  223. */
  224. if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
  225. timeout = floppy->ticks;
  226. expiry = &idefloppy_transfer_pc;
  227. } else {
  228. timeout = IDEFLOPPY_WAIT_CMD;
  229. expiry = NULL;
  230. }
  231. return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
  232. }
  233. static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
  234. struct ide_atapi_pc *pc)
  235. {
  236. /* supress error messages resulting from Medium not present */
  237. if (floppy->sense_key == 0x02 &&
  238. floppy->asc == 0x3a &&
  239. floppy->ascq == 0x00)
  240. return;
  241. printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
  242. "asc = %2x, ascq = %2x\n",
  243. floppy->drive->name, pc->c[0], floppy->sense_key,
  244. floppy->asc, floppy->ascq);
  245. }
  246. static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
  247. struct ide_atapi_pc *pc)
  248. {
  249. idefloppy_floppy_t *floppy = drive->driver_data;
  250. if (floppy->failed_pc == NULL &&
  251. pc->c[0] != GPCMD_REQUEST_SENSE)
  252. floppy->failed_pc = pc;
  253. /* Set the current packet command */
  254. floppy->pc = pc;
  255. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
  256. if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
  257. ide_floppy_report_error(floppy, pc);
  258. /* Giving up */
  259. pc->error = IDEFLOPPY_ERROR_GENERAL;
  260. floppy->failed_pc = NULL;
  261. drive->pc_callback(drive);
  262. return ide_stopped;
  263. }
  264. debug_log("Retry number - %d\n", pc->retries);
  265. pc->retries++;
  266. return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
  267. IDEFLOPPY_WAIT_CMD, NULL);
  268. }
  269. void ide_floppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
  270. {
  271. ide_init_pc(pc);
  272. pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
  273. pc->c[7] = 255;
  274. pc->c[8] = 255;
  275. pc->req_xfer = 255;
  276. }
  277. /* A mode sense command is used to "sense" floppy parameters. */
  278. void ide_floppy_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
  279. {
  280. u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
  281. ide_init_pc(pc);
  282. pc->c[0] = GPCMD_MODE_SENSE_10;
  283. pc->c[1] = 0;
  284. pc->c[2] = page_code;
  285. switch (page_code) {
  286. case IDEFLOPPY_CAPABILITIES_PAGE:
  287. length += 12;
  288. break;
  289. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  290. length += 32;
  291. break;
  292. default:
  293. printk(KERN_ERR "ide-floppy: unsupported page code "
  294. "in create_mode_sense_cmd\n");
  295. }
  296. put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
  297. pc->req_xfer = length;
  298. }
  299. static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
  300. {
  301. ide_init_pc(pc);
  302. pc->c[0] = GPCMD_START_STOP_UNIT;
  303. pc->c[4] = start;
  304. }
  305. static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
  306. struct ide_atapi_pc *pc, struct request *rq,
  307. unsigned long sector)
  308. {
  309. int block = sector / floppy->bs_factor;
  310. int blocks = rq->nr_sectors / floppy->bs_factor;
  311. int cmd = rq_data_dir(rq);
  312. debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
  313. block, blocks);
  314. ide_init_pc(pc);
  315. pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
  316. put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
  317. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
  318. memcpy(rq->cmd, pc->c, 12);
  319. pc->rq = rq;
  320. pc->b_count = 0;
  321. if (rq->cmd_flags & REQ_RW)
  322. pc->flags |= PC_FLAG_WRITING;
  323. pc->buf = NULL;
  324. pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
  325. pc->flags |= PC_FLAG_DMA_OK;
  326. }
  327. static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
  328. struct ide_atapi_pc *pc, struct request *rq)
  329. {
  330. ide_init_pc(pc);
  331. memcpy(pc->c, rq->cmd, sizeof(pc->c));
  332. pc->rq = rq;
  333. pc->b_count = 0;
  334. if (rq->data_len && rq_data_dir(rq) == WRITE)
  335. pc->flags |= PC_FLAG_WRITING;
  336. pc->buf = rq->data;
  337. if (rq->bio)
  338. pc->flags |= PC_FLAG_DMA_OK;
  339. /*
  340. * possibly problematic, doesn't look like ide-floppy correctly
  341. * handled scattered requests if dma fails...
  342. */
  343. pc->req_xfer = pc->buf_size = rq->data_len;
  344. }
  345. static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
  346. struct request *rq, sector_t block_s)
  347. {
  348. idefloppy_floppy_t *floppy = drive->driver_data;
  349. ide_hwif_t *hwif = drive->hwif;
  350. struct ide_atapi_pc *pc;
  351. unsigned long block = (unsigned long)block_s;
  352. debug_log("%s: dev: %s, cmd: 0x%x, cmd_type: %x, errors: %d\n",
  353. __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
  354. rq->cmd[0], rq->cmd_type, rq->errors);
  355. debug_log("%s: sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
  356. __func__, (long)rq->sector, rq->nr_sectors,
  357. rq->current_nr_sectors);
  358. if (rq->errors >= ERROR_MAX) {
  359. if (floppy->failed_pc)
  360. ide_floppy_report_error(floppy, floppy->failed_pc);
  361. else
  362. printk(KERN_ERR "ide-floppy: %s: I/O error\n",
  363. drive->name);
  364. idefloppy_end_request(drive, 0, 0);
  365. return ide_stopped;
  366. }
  367. if (blk_fs_request(rq)) {
  368. if (((long)rq->sector % floppy->bs_factor) ||
  369. (rq->nr_sectors % floppy->bs_factor)) {
  370. printk(KERN_ERR "%s: unsupported r/w request size\n",
  371. drive->name);
  372. idefloppy_end_request(drive, 0, 0);
  373. return ide_stopped;
  374. }
  375. pc = &floppy->queued_pc;
  376. idefloppy_create_rw_cmd(floppy, pc, rq, block);
  377. } else if (blk_special_request(rq)) {
  378. pc = (struct ide_atapi_pc *) rq->buffer;
  379. } else if (blk_pc_request(rq)) {
  380. pc = &floppy->queued_pc;
  381. idefloppy_blockpc_cmd(floppy, pc, rq);
  382. } else {
  383. blk_dump_rq_flags(rq,
  384. "ide-floppy: unsupported command in queue");
  385. idefloppy_end_request(drive, 0, 0);
  386. return ide_stopped;
  387. }
  388. ide_init_sg_cmd(drive, rq);
  389. ide_map_sg(drive, rq);
  390. pc->sg = hwif->sg_table;
  391. pc->sg_cnt = hwif->sg_nents;
  392. pc->rq = rq;
  393. return idefloppy_issue_pc(drive, pc);
  394. }
  395. /*
  396. * Look at the flexible disk page parameters. We ignore the CHS capacity
  397. * parameters and use the LBA parameters instead.
  398. */
  399. static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
  400. {
  401. idefloppy_floppy_t *floppy = drive->driver_data;
  402. struct gendisk *disk = floppy->disk;
  403. struct ide_atapi_pc pc;
  404. u8 *page;
  405. int capacity, lba_capacity;
  406. u16 transfer_rate, sector_size, cyls, rpm;
  407. u8 heads, sectors;
  408. ide_floppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
  409. if (ide_queue_pc_tail(drive, disk, &pc)) {
  410. printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
  411. " parameters\n");
  412. return 1;
  413. }
  414. if (pc.buf[3] & 0x80)
  415. drive->atapi_flags |= IDE_AFLAG_WP;
  416. else
  417. drive->atapi_flags &= ~IDE_AFLAG_WP;
  418. set_disk_ro(disk, !!(drive->atapi_flags & IDE_AFLAG_WP));
  419. page = &pc.buf[8];
  420. transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
  421. sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
  422. cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
  423. rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
  424. heads = pc.buf[8 + 4];
  425. sectors = pc.buf[8 + 5];
  426. capacity = cyls * heads * sectors * sector_size;
  427. if (memcmp(page, &floppy->flexible_disk_page, 32))
  428. printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  429. "%d sector size, %d rpm\n",
  430. drive->name, capacity / 1024, cyls, heads,
  431. sectors, transfer_rate / 8, sector_size, rpm);
  432. memcpy(&floppy->flexible_disk_page, page, 32);
  433. drive->bios_cyl = cyls;
  434. drive->bios_head = heads;
  435. drive->bios_sect = sectors;
  436. lba_capacity = floppy->blocks * floppy->block_size;
  437. if (capacity < lba_capacity) {
  438. printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
  439. "bytes, but the drive only handles %d\n",
  440. drive->name, lba_capacity, capacity);
  441. floppy->blocks = floppy->block_size ?
  442. capacity / floppy->block_size : 0;
  443. }
  444. return 0;
  445. }
  446. /*
  447. * Determine if a media is present in the floppy drive, and if so, its LBA
  448. * capacity.
  449. */
  450. static int ide_floppy_get_capacity(ide_drive_t *drive)
  451. {
  452. idefloppy_floppy_t *floppy = drive->driver_data;
  453. struct gendisk *disk = floppy->disk;
  454. struct ide_atapi_pc pc;
  455. u8 *cap_desc;
  456. u8 header_len, desc_cnt;
  457. int i, rc = 1, blocks, length;
  458. drive->bios_cyl = 0;
  459. drive->bios_head = drive->bios_sect = 0;
  460. floppy->blocks = 0;
  461. floppy->bs_factor = 1;
  462. set_capacity(floppy->disk, 0);
  463. ide_floppy_create_read_capacity_cmd(&pc);
  464. if (ide_queue_pc_tail(drive, disk, &pc)) {
  465. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  466. return 1;
  467. }
  468. header_len = pc.buf[3];
  469. cap_desc = &pc.buf[4];
  470. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  471. for (i = 0; i < desc_cnt; i++) {
  472. unsigned int desc_start = 4 + i*8;
  473. blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
  474. length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
  475. debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
  476. i, blocks * length / 1024, blocks, length);
  477. if (i)
  478. continue;
  479. /*
  480. * the code below is valid only for the 1st descriptor, ie i=0
  481. */
  482. switch (pc.buf[desc_start + 4] & 0x03) {
  483. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  484. case CAPACITY_UNFORMATTED:
  485. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
  486. /*
  487. * If it is not a clik drive, break out
  488. * (maintains previous driver behaviour)
  489. */
  490. break;
  491. case CAPACITY_CURRENT:
  492. /* Normal Zip/LS-120 disks */
  493. if (memcmp(cap_desc, &floppy->cap_desc, 8))
  494. printk(KERN_INFO "%s: %dkB, %d blocks, %d "
  495. "sector size\n", drive->name,
  496. blocks * length / 1024, blocks, length);
  497. memcpy(&floppy->cap_desc, cap_desc, 8);
  498. if (!length || length % 512) {
  499. printk(KERN_NOTICE "%s: %d bytes block size "
  500. "not supported\n", drive->name, length);
  501. } else {
  502. floppy->blocks = blocks;
  503. floppy->block_size = length;
  504. floppy->bs_factor = length / 512;
  505. if (floppy->bs_factor != 1)
  506. printk(KERN_NOTICE "%s: warning: non "
  507. "512 bytes block size not "
  508. "fully supported\n",
  509. drive->name);
  510. rc = 0;
  511. }
  512. break;
  513. case CAPACITY_NO_CARTRIDGE:
  514. /*
  515. * This is a KERN_ERR so it appears on screen
  516. * for the user to see
  517. */
  518. printk(KERN_ERR "%s: No disk in drive\n", drive->name);
  519. break;
  520. case CAPACITY_INVALID:
  521. printk(KERN_ERR "%s: Invalid capacity for disk "
  522. "in drive\n", drive->name);
  523. break;
  524. }
  525. debug_log("Descriptor 0 Code: %d\n",
  526. pc.buf[desc_start + 4] & 0x03);
  527. }
  528. /* Clik! disk does not support get_flexible_disk_page */
  529. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
  530. (void) ide_floppy_get_flexible_disk_page(drive);
  531. set_capacity(disk, floppy->blocks * floppy->bs_factor);
  532. return rc;
  533. }
  534. static sector_t idefloppy_capacity(ide_drive_t *drive)
  535. {
  536. idefloppy_floppy_t *floppy = drive->driver_data;
  537. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  538. return capacity;
  539. }
  540. #ifdef CONFIG_IDE_PROC_FS
  541. ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
  542. ide_devset_rw(bios_head, 0, 255, bios_head);
  543. ide_devset_rw(bios_sect, 0, 63, bios_sect);
  544. static int get_ticks(ide_drive_t *drive)
  545. {
  546. idefloppy_floppy_t *floppy = drive->driver_data;
  547. return floppy->ticks;
  548. }
  549. static int set_ticks(ide_drive_t *drive, int arg)
  550. {
  551. idefloppy_floppy_t *floppy = drive->driver_data;
  552. floppy->ticks = arg;
  553. return 0;
  554. }
  555. IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
  556. static const struct ide_devset *idefloppy_settings[] = {
  557. &ide_devset_bios_cyl,
  558. &ide_devset_bios_head,
  559. &ide_devset_bios_sect,
  560. &ide_devset_ticks,
  561. NULL
  562. };
  563. #endif
  564. static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
  565. {
  566. u16 *id = drive->id;
  567. u8 gcw[2];
  568. *((u16 *)&gcw) = id[ATA_ID_CONFIG];
  569. drive->pc_callback = ide_floppy_callback;
  570. if (((gcw[0] & 0x60) >> 5) == 1)
  571. drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
  572. /*
  573. * We used to check revisions here. At this point however I'm giving up.
  574. * Just assume they are all broken, its easier.
  575. *
  576. * The actual reason for the workarounds was likely a driver bug after
  577. * all rather than a firmware bug, and the workaround below used to hide
  578. * it. It should be fixed as of version 1.9, but to be on the safe side
  579. * we'll leave the limitation below for the 2.2.x tree.
  580. */
  581. if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
  582. drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
  583. /* This value will be visible in the /proc/ide/hdx/settings */
  584. floppy->ticks = IDEFLOPPY_TICKS_DELAY;
  585. blk_queue_max_sectors(drive->queue, 64);
  586. }
  587. /*
  588. * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
  589. * nasty clicking noises without it, so please don't remove this.
  590. */
  591. if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
  592. blk_queue_max_sectors(drive->queue, 64);
  593. drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
  594. /* IOMEGA Clik! drives do not support lock/unlock commands */
  595. drive->atapi_flags |= IDE_AFLAG_NO_DOORLOCK;
  596. }
  597. (void) ide_floppy_get_capacity(drive);
  598. ide_proc_register_driver(drive, floppy->driver);
  599. }
  600. static void ide_floppy_remove(ide_drive_t *drive)
  601. {
  602. idefloppy_floppy_t *floppy = drive->driver_data;
  603. struct gendisk *g = floppy->disk;
  604. ide_proc_unregister_driver(drive, floppy->driver);
  605. del_gendisk(g);
  606. ide_floppy_put(floppy);
  607. }
  608. static void idefloppy_cleanup_obj(struct kref *kref)
  609. {
  610. struct ide_floppy_obj *floppy = to_ide_floppy(kref);
  611. ide_drive_t *drive = floppy->drive;
  612. struct gendisk *g = floppy->disk;
  613. drive->driver_data = NULL;
  614. g->private_data = NULL;
  615. put_disk(g);
  616. kfree(floppy);
  617. }
  618. #ifdef CONFIG_IDE_PROC_FS
  619. static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
  620. int count, int *eof, void *data)
  621. {
  622. ide_drive_t*drive = (ide_drive_t *)data;
  623. int len;
  624. len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
  625. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  626. }
  627. static ide_proc_entry_t idefloppy_proc[] = {
  628. { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
  629. { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
  630. { NULL, 0, NULL, NULL }
  631. };
  632. #endif /* CONFIG_IDE_PROC_FS */
  633. static int ide_floppy_probe(ide_drive_t *);
  634. static ide_driver_t idefloppy_driver = {
  635. .gen_driver = {
  636. .owner = THIS_MODULE,
  637. .name = "ide-floppy",
  638. .bus = &ide_bus_type,
  639. },
  640. .probe = ide_floppy_probe,
  641. .remove = ide_floppy_remove,
  642. .version = IDEFLOPPY_VERSION,
  643. .media = ide_floppy,
  644. .do_request = idefloppy_do_request,
  645. .end_request = idefloppy_end_request,
  646. .error = __ide_error,
  647. #ifdef CONFIG_IDE_PROC_FS
  648. .proc = idefloppy_proc,
  649. .settings = idefloppy_settings,
  650. #endif
  651. };
  652. static int idefloppy_open(struct inode *inode, struct file *filp)
  653. {
  654. struct gendisk *disk = inode->i_bdev->bd_disk;
  655. struct ide_floppy_obj *floppy;
  656. ide_drive_t *drive;
  657. struct ide_atapi_pc pc;
  658. int ret = 0;
  659. debug_log("Reached %s\n", __func__);
  660. floppy = ide_floppy_get(disk);
  661. if (!floppy)
  662. return -ENXIO;
  663. drive = floppy->drive;
  664. floppy->openers++;
  665. if (floppy->openers == 1) {
  666. drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
  667. /* Just in case */
  668. ide_init_pc(&pc);
  669. pc.c[0] = GPCMD_TEST_UNIT_READY;
  670. if (ide_queue_pc_tail(drive, disk, &pc)) {
  671. idefloppy_create_start_stop_cmd(&pc, 1);
  672. (void)ide_queue_pc_tail(drive, disk, &pc);
  673. }
  674. if (ide_floppy_get_capacity(drive)
  675. && (filp->f_flags & O_NDELAY) == 0
  676. /*
  677. * Allow O_NDELAY to open a drive without a disk, or with an
  678. * unreadable disk, so that we can get the format capacity
  679. * of the drive or begin the format - Sam
  680. */
  681. ) {
  682. ret = -EIO;
  683. goto out_put_floppy;
  684. }
  685. if ((drive->atapi_flags & IDE_AFLAG_WP) && (filp->f_mode & 2)) {
  686. ret = -EROFS;
  687. goto out_put_floppy;
  688. }
  689. drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
  690. ide_set_media_lock(drive, disk, 1);
  691. check_disk_change(inode->i_bdev);
  692. } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
  693. ret = -EBUSY;
  694. goto out_put_floppy;
  695. }
  696. return 0;
  697. out_put_floppy:
  698. floppy->openers--;
  699. ide_floppy_put(floppy);
  700. return ret;
  701. }
  702. static int idefloppy_release(struct inode *inode, struct file *filp)
  703. {
  704. struct gendisk *disk = inode->i_bdev->bd_disk;
  705. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  706. ide_drive_t *drive = floppy->drive;
  707. debug_log("Reached %s\n", __func__);
  708. if (floppy->openers == 1) {
  709. ide_set_media_lock(drive, disk, 0);
  710. drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
  711. }
  712. floppy->openers--;
  713. ide_floppy_put(floppy);
  714. return 0;
  715. }
  716. static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  717. {
  718. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  719. ide_drive_t *drive = floppy->drive;
  720. geo->heads = drive->bios_head;
  721. geo->sectors = drive->bios_sect;
  722. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  723. return 0;
  724. }
  725. static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
  726. unsigned long arg, unsigned int cmd)
  727. {
  728. idefloppy_floppy_t *floppy = drive->driver_data;
  729. struct gendisk *disk = floppy->disk;
  730. int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
  731. if (floppy->openers > 1)
  732. return -EBUSY;
  733. ide_set_media_lock(drive, disk, prevent);
  734. if (cmd == CDROMEJECT) {
  735. idefloppy_create_start_stop_cmd(pc, 2);
  736. (void)ide_queue_pc_tail(drive, disk, pc);
  737. }
  738. return 0;
  739. }
  740. static int idefloppy_ioctl(struct inode *inode, struct file *file,
  741. unsigned int cmd, unsigned long arg)
  742. {
  743. struct block_device *bdev = inode->i_bdev;
  744. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  745. ide_drive_t *drive = floppy->drive;
  746. struct ide_atapi_pc pc;
  747. void __user *argp = (void __user *)arg;
  748. int err;
  749. if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
  750. return ide_floppy_lockdoor(drive, &pc, arg, cmd);
  751. err = ide_floppy_format_ioctl(drive, file, cmd, argp);
  752. if (err != -ENOTTY)
  753. return err;
  754. /*
  755. * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
  756. * and CDROM_SEND_PACKET (legacy) ioctls
  757. */
  758. if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
  759. err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
  760. bdev->bd_disk, cmd, argp);
  761. if (err == -ENOTTY)
  762. err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  763. return err;
  764. }
  765. static int idefloppy_media_changed(struct gendisk *disk)
  766. {
  767. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  768. ide_drive_t *drive = floppy->drive;
  769. int ret;
  770. /* do not scan partitions twice if this is a removable device */
  771. if (drive->attach) {
  772. drive->attach = 0;
  773. return 0;
  774. }
  775. ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
  776. drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
  777. return ret;
  778. }
  779. static int idefloppy_revalidate_disk(struct gendisk *disk)
  780. {
  781. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  782. set_capacity(disk, idefloppy_capacity(floppy->drive));
  783. return 0;
  784. }
  785. static struct block_device_operations idefloppy_ops = {
  786. .owner = THIS_MODULE,
  787. .open = idefloppy_open,
  788. .release = idefloppy_release,
  789. .ioctl = idefloppy_ioctl,
  790. .getgeo = idefloppy_getgeo,
  791. .media_changed = idefloppy_media_changed,
  792. .revalidate_disk = idefloppy_revalidate_disk
  793. };
  794. static int ide_floppy_probe(ide_drive_t *drive)
  795. {
  796. idefloppy_floppy_t *floppy;
  797. struct gendisk *g;
  798. if (!strstr("ide-floppy", drive->driver_req))
  799. goto failed;
  800. if (drive->media != ide_floppy)
  801. goto failed;
  802. if (!ide_check_atapi_device(drive, DRV_NAME)) {
  803. printk(KERN_ERR "ide-floppy: %s: not supported by this version"
  804. " of ide-floppy\n", drive->name);
  805. goto failed;
  806. }
  807. floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
  808. if (!floppy) {
  809. printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
  810. " structure\n", drive->name);
  811. goto failed;
  812. }
  813. g = alloc_disk(1 << PARTN_BITS);
  814. if (!g)
  815. goto out_free_floppy;
  816. ide_init_disk(g, drive);
  817. kref_init(&floppy->kref);
  818. floppy->drive = drive;
  819. floppy->driver = &idefloppy_driver;
  820. floppy->disk = g;
  821. g->private_data = &floppy->driver;
  822. drive->driver_data = floppy;
  823. idefloppy_setup(drive, floppy);
  824. g->minors = 1 << PARTN_BITS;
  825. g->driverfs_dev = &drive->gendev;
  826. g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
  827. g->fops = &idefloppy_ops;
  828. drive->attach = 1;
  829. add_disk(g);
  830. return 0;
  831. out_free_floppy:
  832. kfree(floppy);
  833. failed:
  834. return -ENODEV;
  835. }
  836. static void __exit idefloppy_exit(void)
  837. {
  838. driver_unregister(&idefloppy_driver.gen_driver);
  839. }
  840. static int __init idefloppy_init(void)
  841. {
  842. printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
  843. return driver_register(&idefloppy_driver.gen_driver);
  844. }
  845. MODULE_ALIAS("ide:*m-floppy*");
  846. MODULE_ALIAS("ide-floppy");
  847. module_init(idefloppy_init);
  848. module_exit(idefloppy_exit);
  849. MODULE_LICENSE("GPL");
  850. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");