ide-floppy.c 24 KB

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