ide-floppy.c 26 KB

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