ide-floppy.c 39 KB

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