ide-floppy.c 37 KB

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