ide-floppy.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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 IDEFLOPPY_VERSION "1.00"
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/timer.h>
  24. #include <linux/mm.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/major.h>
  27. #include <linux/errno.h>
  28. #include <linux/genhd.h>
  29. #include <linux/slab.h>
  30. #include <linux/cdrom.h>
  31. #include <linux/ide.h>
  32. #include <linux/bitops.h>
  33. #include <linux/mutex.h>
  34. #include <scsi/scsi_ioctl.h>
  35. #include <asm/byteorder.h>
  36. #include <linux/irq.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/io.h>
  39. #include <asm/unaligned.h>
  40. /* define to see debug info */
  41. #define IDEFLOPPY_DEBUG_LOG 0
  42. /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
  43. #define IDEFLOPPY_DEBUG(fmt, args...)
  44. #if IDEFLOPPY_DEBUG_LOG
  45. #define debug_log(fmt, args...) \
  46. printk(KERN_INFO "ide-floppy: " fmt, ## args)
  47. #else
  48. #define debug_log(fmt, args...) do {} while (0)
  49. #endif
  50. /* Some drives require a longer irq timeout. */
  51. #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
  52. /*
  53. * After each failed packet command we issue a request sense command and retry
  54. * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
  55. */
  56. #define IDEFLOPPY_MAX_PC_RETRIES 3
  57. /*
  58. * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
  59. * bytes.
  60. */
  61. #define IDEFLOPPY_PC_BUFFER_SIZE 256
  62. /*
  63. * In various places in the driver, we need to allocate storage for packet
  64. * commands and requests, which will remain valid while we leave the driver to
  65. * wait for an interrupt or a timeout event.
  66. */
  67. #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
  68. /* format capacities descriptor codes */
  69. #define CAPACITY_INVALID 0x00
  70. #define CAPACITY_UNFORMATTED 0x01
  71. #define CAPACITY_CURRENT 0x02
  72. #define CAPACITY_NO_CARTRIDGE 0x03
  73. /*
  74. * Most of our global data which we need to save even as we leave the driver
  75. * due to an interrupt or a timer event is stored in a variable of type
  76. * idefloppy_floppy_t, defined below.
  77. */
  78. typedef struct ide_floppy_obj {
  79. ide_drive_t *drive;
  80. ide_driver_t *driver;
  81. struct gendisk *disk;
  82. struct kref kref;
  83. unsigned int openers; /* protected by BKL for now */
  84. /* Current packet command */
  85. struct ide_atapi_pc *pc;
  86. /* Last failed packet command */
  87. struct ide_atapi_pc *failed_pc;
  88. /* Packet command stack */
  89. struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
  90. /* Next free packet command storage space */
  91. int pc_stack_index;
  92. struct request rq_stack[IDEFLOPPY_PC_STACK];
  93. /* We implement a circular array */
  94. int rq_stack_index;
  95. /* Last error information */
  96. u8 sense_key, asc, ascq;
  97. /* delay this long before sending packet command */
  98. u8 ticks;
  99. int progress_indication;
  100. /* Device information */
  101. /* Current format */
  102. int blocks, block_size, bs_factor;
  103. /* Last format capacity descriptor */
  104. u8 cap_desc[8];
  105. /* Copy of the flexible disk page */
  106. u8 flexible_disk_page[32];
  107. /* Write protect */
  108. int wp;
  109. /* Supports format progress report */
  110. int srfp;
  111. /* Status/Action flags */
  112. unsigned long flags;
  113. } idefloppy_floppy_t;
  114. #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
  115. /* Floppy flag bits values. */
  116. enum {
  117. /* DRQ interrupt device */
  118. IDEFLOPPY_FLAG_DRQ_INTERRUPT = (1 << 0),
  119. /* Media may have changed */
  120. IDEFLOPPY_FLAG_MEDIA_CHANGED = (1 << 1),
  121. /* Format in progress */
  122. IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS = (1 << 2),
  123. /* Avoid commands not supported in Clik drive */
  124. IDEFLOPPY_FLAG_CLIK_DRIVE = (1 << 3),
  125. /* Requires BH algorithm for packets */
  126. IDEFLOPPY_FLAG_ZIP_DRIVE = (1 << 4),
  127. };
  128. /* Defines for the MODE SENSE command */
  129. #define MODE_SENSE_CURRENT 0x00
  130. #define MODE_SENSE_CHANGEABLE 0x01
  131. #define MODE_SENSE_DEFAULT 0x02
  132. #define MODE_SENSE_SAVED 0x03
  133. /* IOCTLs used in low-level formatting. */
  134. #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
  135. #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
  136. #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
  137. #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
  138. /* Error code returned in rq->errors to the higher part of the driver. */
  139. #define IDEFLOPPY_ERROR_GENERAL 101
  140. /*
  141. * Pages of the SELECT SENSE / MODE SENSE packet commands.
  142. * See SFF-8070i spec.
  143. */
  144. #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
  145. #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
  146. static DEFINE_MUTEX(idefloppy_ref_mutex);
  147. #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
  148. #define ide_floppy_g(disk) \
  149. container_of((disk)->private_data, struct ide_floppy_obj, driver)
  150. static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
  151. {
  152. struct ide_floppy_obj *floppy = NULL;
  153. mutex_lock(&idefloppy_ref_mutex);
  154. floppy = ide_floppy_g(disk);
  155. if (floppy)
  156. kref_get(&floppy->kref);
  157. mutex_unlock(&idefloppy_ref_mutex);
  158. return floppy;
  159. }
  160. static void idefloppy_cleanup_obj(struct kref *);
  161. static void ide_floppy_put(struct ide_floppy_obj *floppy)
  162. {
  163. mutex_lock(&idefloppy_ref_mutex);
  164. kref_put(&floppy->kref, idefloppy_cleanup_obj);
  165. mutex_unlock(&idefloppy_ref_mutex);
  166. }
  167. /*
  168. * Used to finish servicing a request. For read/write requests, we will call
  169. * ide_end_request to pass to the next buffer.
  170. */
  171. static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
  172. {
  173. idefloppy_floppy_t *floppy = drive->driver_data;
  174. struct request *rq = HWGROUP(drive)->rq;
  175. int error;
  176. debug_log("Reached %s\n", __func__);
  177. switch (uptodate) {
  178. case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
  179. case 1: error = 0; break;
  180. default: error = uptodate;
  181. }
  182. if (error)
  183. floppy->failed_pc = NULL;
  184. /* Why does this happen? */
  185. if (!rq)
  186. return 0;
  187. if (!blk_special_request(rq)) {
  188. /* our real local end request function */
  189. ide_end_request(drive, uptodate, nsecs);
  190. return 0;
  191. }
  192. rq->errors = error;
  193. /* fixme: need to move this local also */
  194. ide_end_drive_cmd(drive, 0, 0);
  195. return 0;
  196. }
  197. static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  198. unsigned int bcount, int direction)
  199. {
  200. ide_hwif_t *hwif = drive->hwif;
  201. struct request *rq = pc->rq;
  202. struct req_iterator iter;
  203. struct bio_vec *bvec;
  204. unsigned long flags;
  205. int count, done = 0;
  206. char *data;
  207. rq_for_each_segment(bvec, rq, iter) {
  208. if (!bcount)
  209. break;
  210. count = min(bvec->bv_len, bcount);
  211. data = bvec_kmap_irq(bvec, &flags);
  212. if (direction)
  213. hwif->output_data(drive, NULL, data, count);
  214. else
  215. hwif->input_data(drive, NULL, data, count);
  216. bvec_kunmap_irq(data, &flags);
  217. bcount -= count;
  218. pc->b_count += count;
  219. done += count;
  220. }
  221. idefloppy_end_request(drive, 1, done >> 9);
  222. if (bcount) {
  223. printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
  224. drive->name, __func__, bcount);
  225. ide_pad_transfer(drive, direction, bcount);
  226. }
  227. }
  228. static void idefloppy_update_buffers(ide_drive_t *drive,
  229. struct ide_atapi_pc *pc)
  230. {
  231. struct request *rq = pc->rq;
  232. struct bio *bio = rq->bio;
  233. while ((bio = rq->bio) != NULL)
  234. idefloppy_end_request(drive, 1, 0);
  235. }
  236. /*
  237. * Generate a new packet command request in front of the request queue, before
  238. * the current request so that it will be processed immediately, on the next
  239. * pass through the driver.
  240. */
  241. static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
  242. struct request *rq)
  243. {
  244. struct ide_floppy_obj *floppy = drive->driver_data;
  245. blk_rq_init(NULL, rq);
  246. rq->buffer = (char *) pc;
  247. rq->cmd_type = REQ_TYPE_SPECIAL;
  248. rq->cmd_flags |= REQ_PREEMPT;
  249. rq->rq_disk = floppy->disk;
  250. ide_do_drive_cmd(drive, rq);
  251. }
  252. static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
  253. {
  254. idefloppy_floppy_t *floppy = drive->driver_data;
  255. if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
  256. floppy->pc_stack_index = 0;
  257. return (&floppy->pc_stack[floppy->pc_stack_index++]);
  258. }
  259. static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
  260. {
  261. idefloppy_floppy_t *floppy = drive->driver_data;
  262. if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
  263. floppy->rq_stack_index = 0;
  264. return (&floppy->rq_stack[floppy->rq_stack_index++]);
  265. }
  266. static void ide_floppy_callback(ide_drive_t *drive)
  267. {
  268. idefloppy_floppy_t *floppy = drive->driver_data;
  269. struct ide_atapi_pc *pc = floppy->pc;
  270. int uptodate = pc->error ? 0 : 1;
  271. debug_log("Reached %s\n", __func__);
  272. if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
  273. (pc->rq && blk_pc_request(pc->rq)))
  274. uptodate = 1; /* FIXME */
  275. else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
  276. u8 *buf = floppy->pc->buf;
  277. if (!pc->error) {
  278. floppy->sense_key = buf[2] & 0x0F;
  279. floppy->asc = buf[12];
  280. floppy->ascq = buf[13];
  281. floppy->progress_indication = buf[15] & 0x80 ?
  282. (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
  283. if (floppy->failed_pc)
  284. debug_log("pc = %x, ", floppy->failed_pc->c[0]);
  285. debug_log("sense key = %x, asc = %x, ascq = %x\n",
  286. floppy->sense_key, floppy->asc, floppy->ascq);
  287. } else
  288. printk(KERN_ERR "Error in REQUEST SENSE itself - "
  289. "Aborting request!\n");
  290. }
  291. idefloppy_end_request(drive, uptodate, 0);
  292. }
  293. static void idefloppy_init_pc(struct ide_atapi_pc *pc)
  294. {
  295. memset(pc->c, 0, 12);
  296. pc->retries = 0;
  297. pc->flags = 0;
  298. pc->req_xfer = 0;
  299. pc->buf = pc->pc_buf;
  300. pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
  301. pc->callback = ide_floppy_callback;
  302. }
  303. static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
  304. {
  305. idefloppy_init_pc(pc);
  306. pc->c[0] = GPCMD_REQUEST_SENSE;
  307. pc->c[4] = 255;
  308. pc->req_xfer = 18;
  309. }
  310. /*
  311. * Called when an error was detected during the last packet command. We queue a
  312. * request sense packet command in the head of the request list.
  313. */
  314. static void idefloppy_retry_pc(ide_drive_t *drive)
  315. {
  316. struct ide_atapi_pc *pc;
  317. struct request *rq;
  318. (void)ide_read_error(drive);
  319. pc = idefloppy_next_pc_storage(drive);
  320. rq = idefloppy_next_rq_storage(drive);
  321. idefloppy_create_request_sense_cmd(pc);
  322. idefloppy_queue_pc_head(drive, pc, rq);
  323. }
  324. /* The usual interrupt handler called during a packet command. */
  325. static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
  326. {
  327. idefloppy_floppy_t *floppy = drive->driver_data;
  328. ide_hwif_t *hwif = drive->hwif;
  329. struct ide_atapi_pc *pc = floppy->pc;
  330. struct request *rq = pc->rq;
  331. xfer_func_t *xferfunc;
  332. unsigned int temp;
  333. int dma_error = 0;
  334. u16 bcount;
  335. u8 stat, ireason;
  336. debug_log("Reached %s interrupt handler\n", __func__);
  337. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  338. dma_error = hwif->dma_ops->dma_end(drive);
  339. if (dma_error) {
  340. printk(KERN_ERR "%s: DMA %s error\n", drive->name,
  341. rq_data_dir(rq) ? "write" : "read");
  342. pc->flags |= PC_FLAG_DMA_ERROR;
  343. } else {
  344. pc->xferred = pc->req_xfer;
  345. idefloppy_update_buffers(drive, pc);
  346. }
  347. debug_log("DMA finished\n");
  348. }
  349. /* Clear the interrupt */
  350. stat = ide_read_status(drive);
  351. /* No more interrupts */
  352. if ((stat & DRQ_STAT) == 0) {
  353. debug_log("Packet command completed, %d bytes transferred\n",
  354. pc->xferred);
  355. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  356. local_irq_enable_in_hardirq();
  357. if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  358. /* Error detected */
  359. debug_log("%s: I/O error\n", drive->name);
  360. rq->errors++;
  361. if (pc->c[0] == GPCMD_REQUEST_SENSE) {
  362. printk(KERN_ERR "%s: I/O error in request sense"
  363. " command\n", drive->name);
  364. return ide_do_reset(drive);
  365. }
  366. /* Retry operation */
  367. idefloppy_retry_pc(drive);
  368. /* queued, but not started */
  369. return ide_stopped;
  370. }
  371. pc->error = 0;
  372. if (floppy->failed_pc == pc)
  373. floppy->failed_pc = NULL;
  374. /* Command finished - Call the callback function */
  375. pc->callback(drive);
  376. return ide_stopped;
  377. }
  378. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  379. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  380. printk(KERN_ERR "%s: The device wants to issue more interrupts "
  381. "in DMA mode\n", drive->name);
  382. ide_dma_off(drive);
  383. return ide_do_reset(drive);
  384. }
  385. /* Get the number of bytes to transfer */
  386. bcount = (hwif->INB(hwif->io_ports.lbah_addr) << 8) |
  387. hwif->INB(hwif->io_ports.lbam_addr);
  388. /* on this interrupt */
  389. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  390. if (ireason & CD) {
  391. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  392. return ide_do_reset(drive);
  393. }
  394. if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
  395. /* Hopefully, we will never get here */
  396. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  397. "to %s!\n", drive->name,
  398. (ireason & IO) ? "Write" : "Read",
  399. (ireason & IO) ? "Read" : "Write");
  400. return ide_do_reset(drive);
  401. }
  402. if (!(pc->flags & PC_FLAG_WRITING)) {
  403. /* Reading - Check that we have enough space */
  404. temp = pc->xferred + bcount;
  405. if (temp > pc->req_xfer) {
  406. if (temp > pc->buf_size) {
  407. printk(KERN_ERR "%s: The device wants to send "
  408. "us more data than expected - "
  409. "discarding data\n",
  410. drive->name);
  411. ide_pad_transfer(drive, 0, bcount);
  412. ide_set_handler(drive,
  413. &idefloppy_pc_intr,
  414. IDEFLOPPY_WAIT_CMD,
  415. NULL);
  416. return ide_started;
  417. }
  418. debug_log("The device wants to send us more data than "
  419. "expected - allowing transfer\n");
  420. }
  421. }
  422. if (pc->flags & PC_FLAG_WRITING)
  423. xferfunc = hwif->output_data;
  424. else
  425. xferfunc = hwif->input_data;
  426. if (pc->buf)
  427. xferfunc(drive, NULL, pc->cur_pos, bcount);
  428. else
  429. ide_floppy_io_buffers(drive, pc, bcount,
  430. !!(pc->flags & PC_FLAG_WRITING));
  431. /* Update the current position */
  432. pc->xferred += bcount;
  433. pc->cur_pos += bcount;
  434. /* And set the interrupt handler again */
  435. ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
  436. return ide_started;
  437. }
  438. /*
  439. * What we have here is a classic case of a top half / bottom half interrupt
  440. * service routine. In interrupt mode, the device sends an interrupt to signal
  441. * that it is ready to receive a packet. However, we need to delay about 2-3
  442. * ticks before issuing the packet or we gets in trouble.
  443. *
  444. * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
  445. * In either case, when the device says it's ready for a packet, we schedule
  446. * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
  447. */
  448. static int idefloppy_transfer_pc2(ide_drive_t *drive)
  449. {
  450. idefloppy_floppy_t *floppy = drive->driver_data;
  451. /* Send the actual packet */
  452. drive->hwif->output_data(drive, NULL, floppy->pc->c, 12);
  453. /* Timeout for the packet command */
  454. return IDEFLOPPY_WAIT_CMD;
  455. }
  456. static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive)
  457. {
  458. ide_hwif_t *hwif = drive->hwif;
  459. idefloppy_floppy_t *floppy = drive->driver_data;
  460. ide_expiry_t *expiry;
  461. unsigned int timeout;
  462. ide_startstop_t startstop;
  463. u8 ireason;
  464. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  465. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  466. "DRQ isn't asserted\n", drive->name);
  467. return startstop;
  468. }
  469. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  470. if ((ireason & CD) == 0 || (ireason & IO)) {
  471. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  472. "a packet command\n", drive->name);
  473. return ide_do_reset(drive);
  474. }
  475. /*
  476. * The following delay solves a problem with ATAPI Zip 100 drives
  477. * where the Busy flag was apparently being deasserted before the
  478. * unit was ready to receive data. This was happening on a
  479. * 1200 MHz Athlon system. 10/26/01 25msec is too short,
  480. * 40 and 50msec work well. idefloppy_pc_intr will not be actually
  481. * used until after the packet is moved in about 50 msec.
  482. */
  483. if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) {
  484. timeout = floppy->ticks;
  485. expiry = &idefloppy_transfer_pc2;
  486. } else {
  487. timeout = IDEFLOPPY_WAIT_CMD;
  488. expiry = NULL;
  489. }
  490. ide_set_handler(drive, &idefloppy_pc_intr, timeout, expiry);
  491. if ((floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) == 0)
  492. /* Send the actual packet */
  493. hwif->output_data(drive, NULL, floppy->pc->c, 12);
  494. return ide_started;
  495. }
  496. static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
  497. struct ide_atapi_pc *pc)
  498. {
  499. /* supress error messages resulting from Medium not present */
  500. if (floppy->sense_key == 0x02 &&
  501. floppy->asc == 0x3a &&
  502. floppy->ascq == 0x00)
  503. return;
  504. printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
  505. "asc = %2x, ascq = %2x\n",
  506. floppy->drive->name, pc->c[0], floppy->sense_key,
  507. floppy->asc, floppy->ascq);
  508. }
  509. static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
  510. struct ide_atapi_pc *pc)
  511. {
  512. idefloppy_floppy_t *floppy = drive->driver_data;
  513. ide_hwif_t *hwif = drive->hwif;
  514. u16 bcount;
  515. u8 dma;
  516. if (floppy->failed_pc == NULL &&
  517. pc->c[0] != GPCMD_REQUEST_SENSE)
  518. floppy->failed_pc = pc;
  519. /* Set the current packet command */
  520. floppy->pc = pc;
  521. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
  522. if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
  523. ide_floppy_report_error(floppy, pc);
  524. /* Giving up */
  525. pc->error = IDEFLOPPY_ERROR_GENERAL;
  526. floppy->failed_pc = NULL;
  527. pc->callback(drive);
  528. return ide_stopped;
  529. }
  530. debug_log("Retry number - %d\n", pc->retries);
  531. pc->retries++;
  532. /* We haven't transferred any data yet */
  533. pc->xferred = 0;
  534. pc->cur_pos = pc->buf;
  535. bcount = min(pc->req_xfer, 63 * 1024);
  536. if (pc->flags & PC_FLAG_DMA_ERROR) {
  537. pc->flags &= ~PC_FLAG_DMA_ERROR;
  538. ide_dma_off(drive);
  539. }
  540. dma = 0;
  541. if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
  542. dma = !hwif->dma_ops->dma_setup(drive);
  543. ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_DEVICE, bcount, dma);
  544. if (dma) {
  545. /* Begin DMA, if necessary */
  546. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  547. hwif->dma_ops->dma_start(drive);
  548. }
  549. if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) {
  550. /* Issue the packet command */
  551. ide_execute_command(drive, WIN_PACKETCMD,
  552. &idefloppy_transfer_pc1,
  553. IDEFLOPPY_WAIT_CMD,
  554. NULL);
  555. return ide_started;
  556. } else {
  557. /* Issue the packet command */
  558. ide_execute_pkt_cmd(drive);
  559. return idefloppy_transfer_pc1(drive);
  560. }
  561. }
  562. static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
  563. {
  564. debug_log("creating prevent removal command, prevent = %d\n", prevent);
  565. idefloppy_init_pc(pc);
  566. pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  567. pc->c[4] = prevent;
  568. }
  569. static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
  570. {
  571. idefloppy_init_pc(pc);
  572. pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
  573. pc->c[7] = 255;
  574. pc->c[8] = 255;
  575. pc->req_xfer = 255;
  576. }
  577. static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
  578. int l, int flags)
  579. {
  580. idefloppy_init_pc(pc);
  581. pc->c[0] = GPCMD_FORMAT_UNIT;
  582. pc->c[1] = 0x17;
  583. memset(pc->buf, 0, 12);
  584. pc->buf[1] = 0xA2;
  585. /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
  586. if (flags & 1) /* Verify bit on... */
  587. pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
  588. pc->buf[3] = 8;
  589. put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
  590. put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
  591. pc->buf_size = 12;
  592. pc->flags |= PC_FLAG_WRITING;
  593. }
  594. /* A mode sense command is used to "sense" floppy parameters. */
  595. static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
  596. u8 page_code, u8 type)
  597. {
  598. u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
  599. idefloppy_init_pc(pc);
  600. pc->c[0] = GPCMD_MODE_SENSE_10;
  601. pc->c[1] = 0;
  602. pc->c[2] = page_code + (type << 6);
  603. switch (page_code) {
  604. case IDEFLOPPY_CAPABILITIES_PAGE:
  605. length += 12;
  606. break;
  607. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  608. length += 32;
  609. break;
  610. default:
  611. printk(KERN_ERR "ide-floppy: unsupported page code "
  612. "in create_mode_sense_cmd\n");
  613. }
  614. put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
  615. pc->req_xfer = length;
  616. }
  617. static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
  618. {
  619. idefloppy_init_pc(pc);
  620. pc->c[0] = GPCMD_START_STOP_UNIT;
  621. pc->c[4] = start;
  622. }
  623. static void idefloppy_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
  624. {
  625. idefloppy_init_pc(pc);
  626. pc->c[0] = GPCMD_TEST_UNIT_READY;
  627. }
  628. static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
  629. struct ide_atapi_pc *pc, struct request *rq,
  630. unsigned long sector)
  631. {
  632. int block = sector / floppy->bs_factor;
  633. int blocks = rq->nr_sectors / floppy->bs_factor;
  634. int cmd = rq_data_dir(rq);
  635. debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
  636. block, blocks);
  637. idefloppy_init_pc(pc);
  638. pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
  639. put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
  640. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
  641. pc->rq = rq;
  642. pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
  643. if (rq->cmd_flags & REQ_RW)
  644. pc->flags |= PC_FLAG_WRITING;
  645. pc->buf = NULL;
  646. pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
  647. pc->flags |= PC_FLAG_DMA_RECOMMENDED;
  648. }
  649. static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
  650. struct ide_atapi_pc *pc, struct request *rq)
  651. {
  652. idefloppy_init_pc(pc);
  653. memcpy(pc->c, rq->cmd, sizeof(pc->c));
  654. pc->rq = rq;
  655. pc->b_count = rq->data_len;
  656. if (rq->data_len && rq_data_dir(rq) == WRITE)
  657. pc->flags |= PC_FLAG_WRITING;
  658. pc->buf = rq->data;
  659. if (rq->bio)
  660. pc->flags |= PC_FLAG_DMA_RECOMMENDED;
  661. /*
  662. * possibly problematic, doesn't look like ide-floppy correctly
  663. * handled scattered requests if dma fails...
  664. */
  665. pc->req_xfer = pc->buf_size = rq->data_len;
  666. }
  667. static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
  668. struct request *rq, sector_t block_s)
  669. {
  670. idefloppy_floppy_t *floppy = drive->driver_data;
  671. struct ide_atapi_pc *pc;
  672. unsigned long block = (unsigned long)block_s;
  673. debug_log("dev: %s, cmd_type: %x, errors: %d\n",
  674. rq->rq_disk ? rq->rq_disk->disk_name : "?",
  675. rq->cmd_type, rq->errors);
  676. debug_log("sector: %ld, nr_sectors: %ld, "
  677. "current_nr_sectors: %d\n", (long)rq->sector,
  678. rq->nr_sectors, rq->current_nr_sectors);
  679. if (rq->errors >= ERROR_MAX) {
  680. if (floppy->failed_pc)
  681. ide_floppy_report_error(floppy, floppy->failed_pc);
  682. else
  683. printk(KERN_ERR "ide-floppy: %s: I/O error\n",
  684. drive->name);
  685. idefloppy_end_request(drive, 0, 0);
  686. return ide_stopped;
  687. }
  688. if (blk_fs_request(rq)) {
  689. if (((long)rq->sector % floppy->bs_factor) ||
  690. (rq->nr_sectors % floppy->bs_factor)) {
  691. printk(KERN_ERR "%s: unsupported r/w request size\n",
  692. drive->name);
  693. idefloppy_end_request(drive, 0, 0);
  694. return ide_stopped;
  695. }
  696. pc = idefloppy_next_pc_storage(drive);
  697. idefloppy_create_rw_cmd(floppy, pc, rq, block);
  698. } else if (blk_special_request(rq)) {
  699. pc = (struct ide_atapi_pc *) rq->buffer;
  700. } else if (blk_pc_request(rq)) {
  701. pc = idefloppy_next_pc_storage(drive);
  702. idefloppy_blockpc_cmd(floppy, pc, rq);
  703. } else {
  704. blk_dump_rq_flags(rq,
  705. "ide-floppy: unsupported command in queue");
  706. idefloppy_end_request(drive, 0, 0);
  707. return ide_stopped;
  708. }
  709. pc->rq = rq;
  710. return idefloppy_issue_pc(drive, pc);
  711. }
  712. /*
  713. * Add a special packet command request to the tail of the request queue,
  714. * and wait for it to be serviced.
  715. */
  716. static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
  717. {
  718. struct ide_floppy_obj *floppy = drive->driver_data;
  719. struct request *rq;
  720. int error;
  721. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  722. rq->buffer = (char *) pc;
  723. rq->cmd_type = REQ_TYPE_SPECIAL;
  724. error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
  725. blk_put_request(rq);
  726. return error;
  727. }
  728. /*
  729. * Look at the flexible disk page parameters. We ignore the CHS capacity
  730. * parameters and use the LBA parameters instead.
  731. */
  732. static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
  733. {
  734. idefloppy_floppy_t *floppy = drive->driver_data;
  735. struct ide_atapi_pc pc;
  736. u8 *page;
  737. int capacity, lba_capacity;
  738. u16 transfer_rate, sector_size, cyls, rpm;
  739. u8 heads, sectors;
  740. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
  741. MODE_SENSE_CURRENT);
  742. if (idefloppy_queue_pc_tail(drive, &pc)) {
  743. printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
  744. " parameters\n");
  745. return 1;
  746. }
  747. floppy->wp = !!(pc.buf[3] & 0x80);
  748. set_disk_ro(floppy->disk, floppy->wp);
  749. page = &pc.buf[8];
  750. transfer_rate = be16_to_cpu(*(u16 *)&pc.buf[8 + 2]);
  751. sector_size = be16_to_cpu(*(u16 *)&pc.buf[8 + 6]);
  752. cyls = be16_to_cpu(*(u16 *)&pc.buf[8 + 8]);
  753. rpm = be16_to_cpu(*(u16 *)&pc.buf[8 + 28]);
  754. heads = pc.buf[8 + 4];
  755. sectors = pc.buf[8 + 5];
  756. capacity = cyls * heads * sectors * sector_size;
  757. if (memcmp(page, &floppy->flexible_disk_page, 32))
  758. printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  759. "%d sector size, %d rpm\n",
  760. drive->name, capacity / 1024, cyls, heads,
  761. sectors, transfer_rate / 8, sector_size, rpm);
  762. memcpy(&floppy->flexible_disk_page, page, 32);
  763. drive->bios_cyl = cyls;
  764. drive->bios_head = heads;
  765. drive->bios_sect = sectors;
  766. lba_capacity = floppy->blocks * floppy->block_size;
  767. if (capacity < lba_capacity) {
  768. printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
  769. "bytes, but the drive only handles %d\n",
  770. drive->name, lba_capacity, capacity);
  771. floppy->blocks = floppy->block_size ?
  772. capacity / floppy->block_size : 0;
  773. }
  774. return 0;
  775. }
  776. static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
  777. {
  778. idefloppy_floppy_t *floppy = drive->driver_data;
  779. struct ide_atapi_pc pc;
  780. floppy->srfp = 0;
  781. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
  782. MODE_SENSE_CURRENT);
  783. pc.flags |= PC_FLAG_SUPPRESS_ERROR;
  784. if (idefloppy_queue_pc_tail(drive, &pc))
  785. return 1;
  786. floppy->srfp = pc.buf[8 + 2] & 0x40;
  787. return (0);
  788. }
  789. /*
  790. * Determine if a media is present in the floppy drive, and if so, its LBA
  791. * capacity.
  792. */
  793. static int ide_floppy_get_capacity(ide_drive_t *drive)
  794. {
  795. idefloppy_floppy_t *floppy = drive->driver_data;
  796. struct ide_atapi_pc pc;
  797. u8 *cap_desc;
  798. u8 header_len, desc_cnt;
  799. int i, rc = 1, blocks, length;
  800. drive->bios_cyl = 0;
  801. drive->bios_head = drive->bios_sect = 0;
  802. floppy->blocks = 0;
  803. floppy->bs_factor = 1;
  804. set_capacity(floppy->disk, 0);
  805. idefloppy_create_read_capacity_cmd(&pc);
  806. if (idefloppy_queue_pc_tail(drive, &pc)) {
  807. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  808. return 1;
  809. }
  810. header_len = pc.buf[3];
  811. cap_desc = &pc.buf[4];
  812. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  813. for (i = 0; i < desc_cnt; i++) {
  814. unsigned int desc_start = 4 + i*8;
  815. blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]);
  816. length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]);
  817. debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
  818. i, blocks * length / 1024, blocks, length);
  819. if (i)
  820. continue;
  821. /*
  822. * the code below is valid only for the 1st descriptor, ie i=0
  823. */
  824. switch (pc.buf[desc_start + 4] & 0x03) {
  825. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  826. case CAPACITY_UNFORMATTED:
  827. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
  828. /*
  829. * If it is not a clik drive, break out
  830. * (maintains previous driver behaviour)
  831. */
  832. break;
  833. case CAPACITY_CURRENT:
  834. /* Normal Zip/LS-120 disks */
  835. if (memcmp(cap_desc, &floppy->cap_desc, 8))
  836. printk(KERN_INFO "%s: %dkB, %d blocks, %d "
  837. "sector size\n", drive->name,
  838. blocks * length / 1024, blocks, length);
  839. memcpy(&floppy->cap_desc, cap_desc, 8);
  840. if (!length || length % 512) {
  841. printk(KERN_NOTICE "%s: %d bytes block size "
  842. "not supported\n", drive->name, length);
  843. } else {
  844. floppy->blocks = blocks;
  845. floppy->block_size = length;
  846. floppy->bs_factor = length / 512;
  847. if (floppy->bs_factor != 1)
  848. printk(KERN_NOTICE "%s: warning: non "
  849. "512 bytes block size not "
  850. "fully supported\n",
  851. drive->name);
  852. rc = 0;
  853. }
  854. break;
  855. case CAPACITY_NO_CARTRIDGE:
  856. /*
  857. * This is a KERN_ERR so it appears on screen
  858. * for the user to see
  859. */
  860. printk(KERN_ERR "%s: No disk in drive\n", drive->name);
  861. break;
  862. case CAPACITY_INVALID:
  863. printk(KERN_ERR "%s: Invalid capacity for disk "
  864. "in drive\n", drive->name);
  865. break;
  866. }
  867. debug_log("Descriptor 0 Code: %d\n",
  868. pc.buf[desc_start + 4] & 0x03);
  869. }
  870. /* Clik! disk does not support get_flexible_disk_page */
  871. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
  872. (void) ide_floppy_get_flexible_disk_page(drive);
  873. set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
  874. return rc;
  875. }
  876. /*
  877. * Obtain the list of formattable capacities.
  878. * Very similar to ide_floppy_get_capacity, except that we push the capacity
  879. * descriptors to userland, instead of our own structures.
  880. *
  881. * Userland gives us the following structure:
  882. *
  883. * struct idefloppy_format_capacities {
  884. * int nformats;
  885. * struct {
  886. * int nblocks;
  887. * int blocksize;
  888. * } formats[];
  889. * };
  890. *
  891. * userland initializes nformats to the number of allocated formats[] records.
  892. * On exit we set nformats to the number of records we've actually initialized.
  893. */
  894. static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
  895. {
  896. struct ide_atapi_pc pc;
  897. u8 header_len, desc_cnt;
  898. int i, blocks, length, u_array_size, u_index;
  899. int __user *argp;
  900. if (get_user(u_array_size, arg))
  901. return (-EFAULT);
  902. if (u_array_size <= 0)
  903. return (-EINVAL);
  904. idefloppy_create_read_capacity_cmd(&pc);
  905. if (idefloppy_queue_pc_tail(drive, &pc)) {
  906. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  907. return (-EIO);
  908. }
  909. header_len = pc.buf[3];
  910. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  911. u_index = 0;
  912. argp = arg + 1;
  913. /*
  914. * We always skip the first capacity descriptor. That's the current
  915. * capacity. We are interested in the remaining descriptors, the
  916. * formattable capacities.
  917. */
  918. for (i = 1; i < desc_cnt; i++) {
  919. unsigned int desc_start = 4 + i*8;
  920. if (u_index >= u_array_size)
  921. break; /* User-supplied buffer too small */
  922. blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]);
  923. length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]);
  924. if (put_user(blocks, argp))
  925. return(-EFAULT);
  926. ++argp;
  927. if (put_user(length, argp))
  928. return (-EFAULT);
  929. ++argp;
  930. ++u_index;
  931. }
  932. if (put_user(u_index, arg))
  933. return (-EFAULT);
  934. return (0);
  935. }
  936. /*
  937. * Get ATAPI_FORMAT_UNIT progress indication.
  938. *
  939. * Userland gives a pointer to an int. The int is set to a progress
  940. * indicator 0-65536, with 65536=100%.
  941. *
  942. * If the drive does not support format progress indication, we just check
  943. * the dsc bit, and return either 0 or 65536.
  944. */
  945. static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
  946. {
  947. idefloppy_floppy_t *floppy = drive->driver_data;
  948. struct ide_atapi_pc pc;
  949. int progress_indication = 0x10000;
  950. if (floppy->srfp) {
  951. idefloppy_create_request_sense_cmd(&pc);
  952. if (idefloppy_queue_pc_tail(drive, &pc))
  953. return (-EIO);
  954. if (floppy->sense_key == 2 &&
  955. floppy->asc == 4 &&
  956. floppy->ascq == 4)
  957. progress_indication = floppy->progress_indication;
  958. /* Else assume format_unit has finished, and we're at 0x10000 */
  959. } else {
  960. unsigned long flags;
  961. u8 stat;
  962. local_irq_save(flags);
  963. stat = ide_read_status(drive);
  964. local_irq_restore(flags);
  965. progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
  966. }
  967. if (put_user(progress_indication, arg))
  968. return (-EFAULT);
  969. return (0);
  970. }
  971. static sector_t idefloppy_capacity(ide_drive_t *drive)
  972. {
  973. idefloppy_floppy_t *floppy = drive->driver_data;
  974. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  975. return capacity;
  976. }
  977. /*
  978. * Check whether we can support a drive, based on the ATAPI IDENTIFY command
  979. * results.
  980. */
  981. static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
  982. {
  983. u8 gcw[2];
  984. u8 device_type, protocol, removable, drq_type, packet_size;
  985. *((u16 *) &gcw) = id->config;
  986. device_type = gcw[1] & 0x1F;
  987. removable = (gcw[0] & 0x80) >> 7;
  988. protocol = (gcw[1] & 0xC0) >> 6;
  989. drq_type = (gcw[0] & 0x60) >> 5;
  990. packet_size = gcw[0] & 0x03;
  991. #ifdef CONFIG_PPC
  992. /* kludge for Apple PowerBook internal zip */
  993. if (device_type == 5 &&
  994. !strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP"))
  995. device_type = 0;
  996. #endif
  997. if (protocol != 2)
  998. printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
  999. protocol);
  1000. else if (device_type != 0)
  1001. printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
  1002. "to floppy\n", device_type);
  1003. else if (!removable)
  1004. printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
  1005. else if (drq_type == 3)
  1006. printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
  1007. "supported\n", drq_type);
  1008. else if (packet_size != 0)
  1009. printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
  1010. "bytes\n", packet_size);
  1011. else
  1012. return 1;
  1013. return 0;
  1014. }
  1015. #ifdef CONFIG_IDE_PROC_FS
  1016. static void idefloppy_add_settings(ide_drive_t *drive)
  1017. {
  1018. idefloppy_floppy_t *floppy = drive->driver_data;
  1019. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
  1020. &drive->bios_cyl, NULL);
  1021. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
  1022. &drive->bios_head, NULL);
  1023. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
  1024. &drive->bios_sect, NULL);
  1025. ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
  1026. &floppy->ticks, NULL);
  1027. }
  1028. #else
  1029. static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
  1030. #endif
  1031. static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
  1032. {
  1033. u8 gcw[2];
  1034. *((u16 *) &gcw) = drive->id->config;
  1035. floppy->pc = floppy->pc_stack;
  1036. if (((gcw[0] & 0x60) >> 5) == 1)
  1037. floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
  1038. /*
  1039. * We used to check revisions here. At this point however I'm giving up.
  1040. * Just assume they are all broken, its easier.
  1041. *
  1042. * The actual reason for the workarounds was likely a driver bug after
  1043. * all rather than a firmware bug, and the workaround below used to hide
  1044. * it. It should be fixed as of version 1.9, but to be on the safe side
  1045. * we'll leave the limitation below for the 2.2.x tree.
  1046. */
  1047. if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
  1048. floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
  1049. /* This value will be visible in the /proc/ide/hdx/settings */
  1050. floppy->ticks = IDEFLOPPY_TICKS_DELAY;
  1051. blk_queue_max_sectors(drive->queue, 64);
  1052. }
  1053. /*
  1054. * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
  1055. * nasty clicking noises without it, so please don't remove this.
  1056. */
  1057. if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
  1058. blk_queue_max_sectors(drive->queue, 64);
  1059. floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
  1060. }
  1061. (void) ide_floppy_get_capacity(drive);
  1062. idefloppy_add_settings(drive);
  1063. }
  1064. static void ide_floppy_remove(ide_drive_t *drive)
  1065. {
  1066. idefloppy_floppy_t *floppy = drive->driver_data;
  1067. struct gendisk *g = floppy->disk;
  1068. ide_proc_unregister_driver(drive, floppy->driver);
  1069. del_gendisk(g);
  1070. ide_floppy_put(floppy);
  1071. }
  1072. static void idefloppy_cleanup_obj(struct kref *kref)
  1073. {
  1074. struct ide_floppy_obj *floppy = to_ide_floppy(kref);
  1075. ide_drive_t *drive = floppy->drive;
  1076. struct gendisk *g = floppy->disk;
  1077. drive->driver_data = NULL;
  1078. g->private_data = NULL;
  1079. put_disk(g);
  1080. kfree(floppy);
  1081. }
  1082. #ifdef CONFIG_IDE_PROC_FS
  1083. static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
  1084. int count, int *eof, void *data)
  1085. {
  1086. ide_drive_t*drive = (ide_drive_t *)data;
  1087. int len;
  1088. len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
  1089. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  1090. }
  1091. static ide_proc_entry_t idefloppy_proc[] = {
  1092. { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
  1093. { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
  1094. { NULL, 0, NULL, NULL }
  1095. };
  1096. #endif /* CONFIG_IDE_PROC_FS */
  1097. static int ide_floppy_probe(ide_drive_t *);
  1098. static ide_driver_t idefloppy_driver = {
  1099. .gen_driver = {
  1100. .owner = THIS_MODULE,
  1101. .name = "ide-floppy",
  1102. .bus = &ide_bus_type,
  1103. },
  1104. .probe = ide_floppy_probe,
  1105. .remove = ide_floppy_remove,
  1106. .version = IDEFLOPPY_VERSION,
  1107. .media = ide_floppy,
  1108. .supports_dsc_overlap = 0,
  1109. .do_request = idefloppy_do_request,
  1110. .end_request = idefloppy_end_request,
  1111. .error = __ide_error,
  1112. .abort = __ide_abort,
  1113. #ifdef CONFIG_IDE_PROC_FS
  1114. .proc = idefloppy_proc,
  1115. #endif
  1116. };
  1117. static int idefloppy_open(struct inode *inode, struct file *filp)
  1118. {
  1119. struct gendisk *disk = inode->i_bdev->bd_disk;
  1120. struct ide_floppy_obj *floppy;
  1121. ide_drive_t *drive;
  1122. struct ide_atapi_pc pc;
  1123. int ret = 0;
  1124. debug_log("Reached %s\n", __func__);
  1125. floppy = ide_floppy_get(disk);
  1126. if (!floppy)
  1127. return -ENXIO;
  1128. drive = floppy->drive;
  1129. floppy->openers++;
  1130. if (floppy->openers == 1) {
  1131. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1132. /* Just in case */
  1133. idefloppy_create_test_unit_ready_cmd(&pc);
  1134. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1135. idefloppy_create_start_stop_cmd(&pc, 1);
  1136. (void) idefloppy_queue_pc_tail(drive, &pc);
  1137. }
  1138. if (ide_floppy_get_capacity(drive)
  1139. && (filp->f_flags & O_NDELAY) == 0
  1140. /*
  1141. * Allow O_NDELAY to open a drive without a disk, or with an
  1142. * unreadable disk, so that we can get the format capacity
  1143. * of the drive or begin the format - Sam
  1144. */
  1145. ) {
  1146. ret = -EIO;
  1147. goto out_put_floppy;
  1148. }
  1149. if (floppy->wp && (filp->f_mode & 2)) {
  1150. ret = -EROFS;
  1151. goto out_put_floppy;
  1152. }
  1153. floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
  1154. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1155. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1156. idefloppy_create_prevent_cmd(&pc, 1);
  1157. (void) idefloppy_queue_pc_tail(drive, &pc);
  1158. }
  1159. check_disk_change(inode->i_bdev);
  1160. } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
  1161. ret = -EBUSY;
  1162. goto out_put_floppy;
  1163. }
  1164. return 0;
  1165. out_put_floppy:
  1166. floppy->openers--;
  1167. ide_floppy_put(floppy);
  1168. return ret;
  1169. }
  1170. static int idefloppy_release(struct inode *inode, struct file *filp)
  1171. {
  1172. struct gendisk *disk = inode->i_bdev->bd_disk;
  1173. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1174. ide_drive_t *drive = floppy->drive;
  1175. struct ide_atapi_pc pc;
  1176. debug_log("Reached %s\n", __func__);
  1177. if (floppy->openers == 1) {
  1178. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1179. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1180. idefloppy_create_prevent_cmd(&pc, 0);
  1181. (void) idefloppy_queue_pc_tail(drive, &pc);
  1182. }
  1183. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1184. }
  1185. floppy->openers--;
  1186. ide_floppy_put(floppy);
  1187. return 0;
  1188. }
  1189. static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1190. {
  1191. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1192. ide_drive_t *drive = floppy->drive;
  1193. geo->heads = drive->bios_head;
  1194. geo->sectors = drive->bios_sect;
  1195. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  1196. return 0;
  1197. }
  1198. static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy,
  1199. struct ide_atapi_pc *pc, unsigned long arg, unsigned int cmd)
  1200. {
  1201. if (floppy->openers > 1)
  1202. return -EBUSY;
  1203. /* The IOMEGA Clik! Drive doesn't support this command -
  1204. * no room for an eject mechanism */
  1205. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1206. int prevent = arg ? 1 : 0;
  1207. if (cmd == CDROMEJECT)
  1208. prevent = 0;
  1209. idefloppy_create_prevent_cmd(pc, prevent);
  1210. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1211. }
  1212. if (cmd == CDROMEJECT) {
  1213. idefloppy_create_start_stop_cmd(pc, 2);
  1214. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1215. }
  1216. return 0;
  1217. }
  1218. static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
  1219. int __user *arg)
  1220. {
  1221. int blocks, length, flags, err = 0;
  1222. struct ide_atapi_pc pc;
  1223. if (floppy->openers > 1) {
  1224. /* Don't format if someone is using the disk */
  1225. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1226. return -EBUSY;
  1227. }
  1228. floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1229. /*
  1230. * Send ATAPI_FORMAT_UNIT to the drive.
  1231. *
  1232. * Userland gives us the following structure:
  1233. *
  1234. * struct idefloppy_format_command {
  1235. * int nblocks;
  1236. * int blocksize;
  1237. * int flags;
  1238. * } ;
  1239. *
  1240. * flags is a bitmask, currently, the only defined flag is:
  1241. *
  1242. * 0x01 - verify media after format.
  1243. */
  1244. if (get_user(blocks, arg) ||
  1245. get_user(length, arg+1) ||
  1246. get_user(flags, arg+2)) {
  1247. err = -EFAULT;
  1248. goto out;
  1249. }
  1250. (void) idefloppy_get_sfrp_bit(floppy->drive);
  1251. idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
  1252. if (idefloppy_queue_pc_tail(floppy->drive, &pc))
  1253. err = -EIO;
  1254. out:
  1255. if (err)
  1256. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1257. return err;
  1258. }
  1259. static int idefloppy_ioctl(struct inode *inode, struct file *file,
  1260. unsigned int cmd, unsigned long arg)
  1261. {
  1262. struct block_device *bdev = inode->i_bdev;
  1263. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1264. ide_drive_t *drive = floppy->drive;
  1265. struct ide_atapi_pc pc;
  1266. void __user *argp = (void __user *)arg;
  1267. int err;
  1268. switch (cmd) {
  1269. case CDROMEJECT:
  1270. /* fall through */
  1271. case CDROM_LOCKDOOR:
  1272. return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
  1273. case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
  1274. return 0;
  1275. case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
  1276. return ide_floppy_get_format_capacities(drive, argp);
  1277. case IDEFLOPPY_IOCTL_FORMAT_START:
  1278. if (!(file->f_mode & 2))
  1279. return -EPERM;
  1280. return ide_floppy_format_unit(floppy, (int __user *)arg);
  1281. case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
  1282. return idefloppy_get_format_progress(drive, argp);
  1283. }
  1284. /*
  1285. * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
  1286. * and CDROM_SEND_PACKET (legacy) ioctls
  1287. */
  1288. if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
  1289. err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
  1290. bdev->bd_disk, cmd, argp);
  1291. else
  1292. err = -ENOTTY;
  1293. if (err == -ENOTTY)
  1294. err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  1295. return err;
  1296. }
  1297. static int idefloppy_media_changed(struct gendisk *disk)
  1298. {
  1299. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1300. ide_drive_t *drive = floppy->drive;
  1301. int ret;
  1302. /* do not scan partitions twice if this is a removable device */
  1303. if (drive->attach) {
  1304. drive->attach = 0;
  1305. return 0;
  1306. }
  1307. ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
  1308. floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
  1309. return ret;
  1310. }
  1311. static int idefloppy_revalidate_disk(struct gendisk *disk)
  1312. {
  1313. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1314. set_capacity(disk, idefloppy_capacity(floppy->drive));
  1315. return 0;
  1316. }
  1317. static struct block_device_operations idefloppy_ops = {
  1318. .owner = THIS_MODULE,
  1319. .open = idefloppy_open,
  1320. .release = idefloppy_release,
  1321. .ioctl = idefloppy_ioctl,
  1322. .getgeo = idefloppy_getgeo,
  1323. .media_changed = idefloppy_media_changed,
  1324. .revalidate_disk = idefloppy_revalidate_disk
  1325. };
  1326. static int ide_floppy_probe(ide_drive_t *drive)
  1327. {
  1328. idefloppy_floppy_t *floppy;
  1329. struct gendisk *g;
  1330. if (!strstr("ide-floppy", drive->driver_req))
  1331. goto failed;
  1332. if (!drive->present)
  1333. goto failed;
  1334. if (drive->media != ide_floppy)
  1335. goto failed;
  1336. if (!idefloppy_identify_device(drive, drive->id)) {
  1337. printk(KERN_ERR "ide-floppy: %s: not supported by this version"
  1338. " of ide-floppy\n", drive->name);
  1339. goto failed;
  1340. }
  1341. if (drive->scsi) {
  1342. printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi"
  1343. " emulation.\n", drive->name);
  1344. goto failed;
  1345. }
  1346. floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
  1347. if (!floppy) {
  1348. printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
  1349. " structure\n", drive->name);
  1350. goto failed;
  1351. }
  1352. g = alloc_disk(1 << PARTN_BITS);
  1353. if (!g)
  1354. goto out_free_floppy;
  1355. ide_init_disk(g, drive);
  1356. ide_proc_register_driver(drive, &idefloppy_driver);
  1357. kref_init(&floppy->kref);
  1358. floppy->drive = drive;
  1359. floppy->driver = &idefloppy_driver;
  1360. floppy->disk = g;
  1361. g->private_data = &floppy->driver;
  1362. drive->driver_data = floppy;
  1363. idefloppy_setup(drive, floppy);
  1364. g->minors = 1 << PARTN_BITS;
  1365. g->driverfs_dev = &drive->gendev;
  1366. g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
  1367. g->fops = &idefloppy_ops;
  1368. drive->attach = 1;
  1369. add_disk(g);
  1370. return 0;
  1371. out_free_floppy:
  1372. kfree(floppy);
  1373. failed:
  1374. return -ENODEV;
  1375. }
  1376. static void __exit idefloppy_exit(void)
  1377. {
  1378. driver_unregister(&idefloppy_driver.gen_driver);
  1379. }
  1380. static int __init idefloppy_init(void)
  1381. {
  1382. printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
  1383. return driver_register(&idefloppy_driver.gen_driver);
  1384. }
  1385. MODULE_ALIAS("ide:*m-floppy*");
  1386. module_init(idefloppy_init);
  1387. module_exit(idefloppy_exit);
  1388. MODULE_LICENSE("GPL");
  1389. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");