ide-floppy.c 26 KB

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