ide-floppy.c 45 KB

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