ide-floppy.c 25 KB

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