ide-floppy.c 42 KB

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