ide-floppy.c 24 KB

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