ide-floppy.c 45 KB

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