ide-floppy.c 45 KB

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