ide-floppy.c 26 KB

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