ide-floppy.c 39 KB

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