ide-floppy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. sector_t ide_floppy_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. static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
  473. {
  474. u16 *id = drive->id;
  475. drive->pc_callback = ide_floppy_callback;
  476. drive->pc_update_buffers = idefloppy_update_buffers;
  477. drive->pc_io_buffers = ide_io_buffers;
  478. /*
  479. * We used to check revisions here. At this point however I'm giving up.
  480. * Just assume they are all broken, its easier.
  481. *
  482. * The actual reason for the workarounds was likely a driver bug after
  483. * all rather than a firmware bug, and the workaround below used to hide
  484. * it. It should be fixed as of version 1.9, but to be on the safe side
  485. * we'll leave the limitation below for the 2.2.x tree.
  486. */
  487. if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
  488. drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
  489. /* This value will be visible in the /proc/ide/hdx/settings */
  490. drive->pc_delay = IDEFLOPPY_PC_DELAY;
  491. blk_queue_max_sectors(drive->queue, 64);
  492. }
  493. /*
  494. * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
  495. * nasty clicking noises without it, so please don't remove this.
  496. */
  497. if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
  498. blk_queue_max_sectors(drive->queue, 64);
  499. drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
  500. /* IOMEGA Clik! drives do not support lock/unlock commands */
  501. drive->atapi_flags |= IDE_AFLAG_NO_DOORLOCK;
  502. }
  503. (void) ide_floppy_get_capacity(drive);
  504. ide_proc_register_driver(drive, floppy->driver);
  505. }
  506. static void ide_floppy_remove(ide_drive_t *drive)
  507. {
  508. idefloppy_floppy_t *floppy = drive->driver_data;
  509. struct gendisk *g = floppy->disk;
  510. ide_proc_unregister_driver(drive, floppy->driver);
  511. del_gendisk(g);
  512. ide_floppy_put(floppy);
  513. }
  514. static void idefloppy_cleanup_obj(struct kref *kref)
  515. {
  516. struct ide_floppy_obj *floppy = to_ide_drv(kref, ide_floppy_obj);
  517. ide_drive_t *drive = floppy->drive;
  518. struct gendisk *g = floppy->disk;
  519. drive->driver_data = NULL;
  520. g->private_data = NULL;
  521. put_disk(g);
  522. kfree(floppy);
  523. }
  524. static int ide_floppy_probe(ide_drive_t *);
  525. static ide_driver_t idefloppy_driver = {
  526. .gen_driver = {
  527. .owner = THIS_MODULE,
  528. .name = "ide-floppy",
  529. .bus = &ide_bus_type,
  530. },
  531. .probe = ide_floppy_probe,
  532. .remove = ide_floppy_remove,
  533. .version = IDEFLOPPY_VERSION,
  534. .do_request = idefloppy_do_request,
  535. .end_request = idefloppy_end_request,
  536. .error = __ide_error,
  537. #ifdef CONFIG_IDE_PROC_FS
  538. .proc = ide_floppy_proc,
  539. .settings = ide_floppy_settings,
  540. #endif
  541. };
  542. static int idefloppy_open(struct inode *inode, struct file *filp)
  543. {
  544. struct gendisk *disk = inode->i_bdev->bd_disk;
  545. struct ide_floppy_obj *floppy;
  546. ide_drive_t *drive;
  547. int ret = 0;
  548. floppy = ide_floppy_get(disk);
  549. if (!floppy)
  550. return -ENXIO;
  551. drive = floppy->drive;
  552. ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
  553. floppy->openers++;
  554. if (floppy->openers == 1) {
  555. drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
  556. /* Just in case */
  557. if (ide_do_test_unit_ready(drive, disk))
  558. ide_do_start_stop(drive, disk, 1);
  559. if (ide_floppy_get_capacity(drive)
  560. && (filp->f_flags & O_NDELAY) == 0
  561. /*
  562. * Allow O_NDELAY to open a drive without a disk, or with an
  563. * unreadable disk, so that we can get the format capacity
  564. * of the drive or begin the format - Sam
  565. */
  566. ) {
  567. ret = -EIO;
  568. goto out_put_floppy;
  569. }
  570. if ((drive->atapi_flags & IDE_AFLAG_WP) && (filp->f_mode & 2)) {
  571. ret = -EROFS;
  572. goto out_put_floppy;
  573. }
  574. drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
  575. ide_set_media_lock(drive, disk, 1);
  576. check_disk_change(inode->i_bdev);
  577. } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
  578. ret = -EBUSY;
  579. goto out_put_floppy;
  580. }
  581. return 0;
  582. out_put_floppy:
  583. floppy->openers--;
  584. ide_floppy_put(floppy);
  585. return ret;
  586. }
  587. static int idefloppy_release(struct inode *inode, struct file *filp)
  588. {
  589. struct gendisk *disk = inode->i_bdev->bd_disk;
  590. struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
  591. ide_drive_t *drive = floppy->drive;
  592. ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
  593. if (floppy->openers == 1) {
  594. ide_set_media_lock(drive, disk, 0);
  595. drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
  596. }
  597. floppy->openers--;
  598. ide_floppy_put(floppy);
  599. return 0;
  600. }
  601. static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  602. {
  603. struct ide_floppy_obj *floppy = ide_drv_g(bdev->bd_disk,
  604. ide_floppy_obj);
  605. ide_drive_t *drive = floppy->drive;
  606. geo->heads = drive->bios_head;
  607. geo->sectors = drive->bios_sect;
  608. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  609. return 0;
  610. }
  611. static int idefloppy_media_changed(struct gendisk *disk)
  612. {
  613. struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
  614. ide_drive_t *drive = floppy->drive;
  615. int ret;
  616. /* do not scan partitions twice if this is a removable device */
  617. if (drive->dev_flags & IDE_DFLAG_ATTACH) {
  618. drive->dev_flags &= ~IDE_DFLAG_ATTACH;
  619. return 0;
  620. }
  621. ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
  622. drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
  623. return ret;
  624. }
  625. static int idefloppy_revalidate_disk(struct gendisk *disk)
  626. {
  627. struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
  628. set_capacity(disk, ide_floppy_capacity(floppy->drive));
  629. return 0;
  630. }
  631. static struct block_device_operations idefloppy_ops = {
  632. .owner = THIS_MODULE,
  633. .open = idefloppy_open,
  634. .release = idefloppy_release,
  635. .ioctl = ide_floppy_ioctl,
  636. .getgeo = idefloppy_getgeo,
  637. .media_changed = idefloppy_media_changed,
  638. .revalidate_disk = idefloppy_revalidate_disk
  639. };
  640. static int ide_floppy_probe(ide_drive_t *drive)
  641. {
  642. idefloppy_floppy_t *floppy;
  643. struct gendisk *g;
  644. if (!strstr("ide-floppy", drive->driver_req))
  645. goto failed;
  646. if (drive->media != ide_floppy)
  647. goto failed;
  648. if (!ide_check_atapi_device(drive, DRV_NAME)) {
  649. printk(KERN_ERR PFX "%s: not supported by this version of "
  650. DRV_NAME "\n", drive->name);
  651. goto failed;
  652. }
  653. floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
  654. if (!floppy) {
  655. printk(KERN_ERR PFX "%s: Can't allocate a floppy structure\n",
  656. drive->name);
  657. goto failed;
  658. }
  659. g = alloc_disk(1 << PARTN_BITS);
  660. if (!g)
  661. goto out_free_floppy;
  662. ide_init_disk(g, drive);
  663. kref_init(&floppy->kref);
  664. floppy->drive = drive;
  665. floppy->driver = &idefloppy_driver;
  666. floppy->disk = g;
  667. g->private_data = &floppy->driver;
  668. drive->driver_data = floppy;
  669. drive->debug_mask = debug_mask;
  670. idefloppy_setup(drive, floppy);
  671. drive->dev_flags |= IDE_DFLAG_ATTACH;
  672. g->minors = 1 << PARTN_BITS;
  673. g->driverfs_dev = &drive->gendev;
  674. if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
  675. g->flags = GENHD_FL_REMOVABLE;
  676. g->fops = &idefloppy_ops;
  677. add_disk(g);
  678. return 0;
  679. out_free_floppy:
  680. kfree(floppy);
  681. failed:
  682. return -ENODEV;
  683. }
  684. static void __exit idefloppy_exit(void)
  685. {
  686. driver_unregister(&idefloppy_driver.gen_driver);
  687. }
  688. static int __init idefloppy_init(void)
  689. {
  690. printk(KERN_INFO DRV_NAME " driver " IDEFLOPPY_VERSION "\n");
  691. return driver_register(&idefloppy_driver.gen_driver);
  692. }
  693. MODULE_ALIAS("ide:*m-floppy*");
  694. MODULE_ALIAS("ide-floppy");
  695. module_init(idefloppy_init);
  696. module_exit(idefloppy_exit);
  697. MODULE_LICENSE("GPL");
  698. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");