ide-floppy.c 42 KB

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