ide-floppy.c 38 KB

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