ide-floppy.c 42 KB

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