ide-floppy.c 35 KB

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