ide-floppy.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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 <scsi/scsi_ioctl.h>
  37. #include <asm/byteorder.h>
  38. #include <linux/irq.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/io.h>
  41. #include <asm/unaligned.h>
  42. /* define to see debug info */
  43. #define IDEFLOPPY_DEBUG_LOG 0
  44. /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
  45. #define IDEFLOPPY_DEBUG(fmt, args...)
  46. #if IDEFLOPPY_DEBUG_LOG
  47. #define debug_log(fmt, args...) \
  48. printk(KERN_INFO "ide-floppy: " fmt, ## args)
  49. #else
  50. #define debug_log(fmt, args...) do {} while (0)
  51. #endif
  52. /* Some drives require a longer irq timeout. */
  53. #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
  54. /*
  55. * After each failed packet command we issue a request sense command and retry
  56. * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
  57. */
  58. #define IDEFLOPPY_MAX_PC_RETRIES 3
  59. /*
  60. * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
  61. * bytes.
  62. */
  63. #define IDEFLOPPY_PC_BUFFER_SIZE 256
  64. /*
  65. * In various places in the driver, we need to allocate storage for packet
  66. * commands and requests, which will remain valid while we leave the driver to
  67. * wait for an interrupt or a timeout event.
  68. */
  69. #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
  70. /* format capacities descriptor codes */
  71. #define CAPACITY_INVALID 0x00
  72. #define CAPACITY_UNFORMATTED 0x01
  73. #define CAPACITY_CURRENT 0x02
  74. #define CAPACITY_NO_CARTRIDGE 0x03
  75. /*
  76. * Most of our global data which we need to save even as we leave the driver
  77. * due to an interrupt or a timer event is stored in a variable of type
  78. * idefloppy_floppy_t, defined below.
  79. */
  80. typedef struct ide_floppy_obj {
  81. ide_drive_t *drive;
  82. ide_driver_t *driver;
  83. struct gendisk *disk;
  84. struct kref kref;
  85. unsigned int openers; /* protected by BKL for now */
  86. /* Current packet command */
  87. struct ide_atapi_pc *pc;
  88. /* Last failed packet command */
  89. struct ide_atapi_pc *failed_pc;
  90. /* Packet command stack */
  91. struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
  92. /* Next free packet command storage space */
  93. int pc_stack_index;
  94. struct request rq_stack[IDEFLOPPY_PC_STACK];
  95. /* We implement a circular array */
  96. int rq_stack_index;
  97. /* Last error information */
  98. u8 sense_key, asc, ascq;
  99. /* delay this long before sending packet command */
  100. u8 ticks;
  101. int progress_indication;
  102. /* Device information */
  103. /* Current format */
  104. int blocks, block_size, bs_factor;
  105. /* Last format capacity descriptor */
  106. u8 cap_desc[8];
  107. /* Copy of the flexible disk page */
  108. u8 flexible_disk_page[32];
  109. /* Write protect */
  110. int wp;
  111. /* Supports format progress report */
  112. int srfp;
  113. } idefloppy_floppy_t;
  114. #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
  115. /* Defines for the MODE SENSE command */
  116. #define MODE_SENSE_CURRENT 0x00
  117. #define MODE_SENSE_CHANGEABLE 0x01
  118. #define MODE_SENSE_DEFAULT 0x02
  119. #define MODE_SENSE_SAVED 0x03
  120. /* IOCTLs used in low-level formatting. */
  121. #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
  122. #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
  123. #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
  124. #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
  125. /* Error code returned in rq->errors to the higher part of the driver. */
  126. #define IDEFLOPPY_ERROR_GENERAL 101
  127. /*
  128. * Pages of the SELECT SENSE / MODE SENSE packet commands.
  129. * See SFF-8070i spec.
  130. */
  131. #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
  132. #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
  133. static DEFINE_MUTEX(idefloppy_ref_mutex);
  134. #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
  135. #define ide_floppy_g(disk) \
  136. container_of((disk)->private_data, struct ide_floppy_obj, driver)
  137. static void idefloppy_cleanup_obj(struct kref *);
  138. static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
  139. {
  140. struct ide_floppy_obj *floppy = NULL;
  141. mutex_lock(&idefloppy_ref_mutex);
  142. floppy = ide_floppy_g(disk);
  143. if (floppy) {
  144. if (ide_device_get(floppy->drive))
  145. floppy = NULL;
  146. else
  147. kref_get(&floppy->kref);
  148. }
  149. mutex_unlock(&idefloppy_ref_mutex);
  150. return floppy;
  151. }
  152. static void ide_floppy_put(struct ide_floppy_obj *floppy)
  153. {
  154. ide_drive_t *drive = floppy->drive;
  155. mutex_lock(&idefloppy_ref_mutex);
  156. kref_put(&floppy->kref, idefloppy_cleanup_obj);
  157. ide_device_put(drive);
  158. mutex_unlock(&idefloppy_ref_mutex);
  159. }
  160. /*
  161. * Used to finish servicing a request. For read/write requests, we will call
  162. * ide_end_request to pass to the next buffer.
  163. */
  164. static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
  165. {
  166. idefloppy_floppy_t *floppy = drive->driver_data;
  167. struct request *rq = HWGROUP(drive)->rq;
  168. int error;
  169. debug_log("Reached %s\n", __func__);
  170. switch (uptodate) {
  171. case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
  172. case 1: error = 0; break;
  173. default: error = uptodate;
  174. }
  175. if (error)
  176. floppy->failed_pc = NULL;
  177. /* Why does this happen? */
  178. if (!rq)
  179. return 0;
  180. if (!blk_special_request(rq)) {
  181. /* our real local end request function */
  182. ide_end_request(drive, uptodate, nsecs);
  183. return 0;
  184. }
  185. rq->errors = error;
  186. /* fixme: need to move this local also */
  187. ide_end_drive_cmd(drive, 0, 0);
  188. return 0;
  189. }
  190. static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  191. unsigned int bcount, int direction)
  192. {
  193. ide_hwif_t *hwif = drive->hwif;
  194. struct request *rq = pc->rq;
  195. struct req_iterator iter;
  196. struct bio_vec *bvec;
  197. unsigned long flags;
  198. int count, done = 0;
  199. char *data;
  200. rq_for_each_segment(bvec, rq, iter) {
  201. if (!bcount)
  202. break;
  203. count = min(bvec->bv_len, bcount);
  204. data = bvec_kmap_irq(bvec, &flags);
  205. if (direction)
  206. hwif->tp_ops->output_data(drive, NULL, data, count);
  207. else
  208. hwif->tp_ops->input_data(drive, NULL, data, count);
  209. bvec_kunmap_irq(data, &flags);
  210. bcount -= count;
  211. pc->b_count += count;
  212. done += count;
  213. }
  214. idefloppy_end_request(drive, 1, done >> 9);
  215. if (bcount) {
  216. printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
  217. drive->name, __func__, bcount);
  218. ide_pad_transfer(drive, direction, bcount);
  219. }
  220. }
  221. static void idefloppy_update_buffers(ide_drive_t *drive,
  222. struct ide_atapi_pc *pc)
  223. {
  224. struct request *rq = pc->rq;
  225. struct bio *bio = rq->bio;
  226. while ((bio = rq->bio) != NULL)
  227. idefloppy_end_request(drive, 1, 0);
  228. }
  229. /*
  230. * Generate a new packet command request in front of the request queue, before
  231. * the current request so that it will be processed immediately, on the next
  232. * pass through the driver.
  233. */
  234. static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
  235. struct request *rq)
  236. {
  237. struct ide_floppy_obj *floppy = drive->driver_data;
  238. blk_rq_init(NULL, rq);
  239. rq->buffer = (char *) pc;
  240. rq->cmd_type = REQ_TYPE_SPECIAL;
  241. rq->cmd_flags |= REQ_PREEMPT;
  242. rq->rq_disk = floppy->disk;
  243. memcpy(rq->cmd, pc->c, 12);
  244. ide_do_drive_cmd(drive, rq);
  245. }
  246. static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
  247. {
  248. idefloppy_floppy_t *floppy = drive->driver_data;
  249. if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
  250. floppy->pc_stack_index = 0;
  251. return (&floppy->pc_stack[floppy->pc_stack_index++]);
  252. }
  253. static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
  254. {
  255. idefloppy_floppy_t *floppy = drive->driver_data;
  256. if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
  257. floppy->rq_stack_index = 0;
  258. return (&floppy->rq_stack[floppy->rq_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_atapi_pc *pc;
  309. struct request *rq;
  310. (void)ide_read_error(drive);
  311. pc = idefloppy_next_pc_storage(drive);
  312. rq = idefloppy_next_rq_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 = cmd == READ ? 0 : rq->bio->bi_size;
  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 = rq->data_len;
  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. struct ide_atapi_pc *pc;
  508. unsigned long block = (unsigned long)block_s;
  509. debug_log("dev: %s, cmd_type: %x, errors: %d\n",
  510. rq->rq_disk ? rq->rq_disk->disk_name : "?",
  511. rq->cmd_type, rq->errors);
  512. debug_log("sector: %ld, nr_sectors: %ld, "
  513. "current_nr_sectors: %d\n", (long)rq->sector,
  514. rq->nr_sectors, rq->current_nr_sectors);
  515. if (rq->errors >= ERROR_MAX) {
  516. if (floppy->failed_pc)
  517. ide_floppy_report_error(floppy, floppy->failed_pc);
  518. else
  519. printk(KERN_ERR "ide-floppy: %s: I/O error\n",
  520. drive->name);
  521. idefloppy_end_request(drive, 0, 0);
  522. return ide_stopped;
  523. }
  524. if (blk_fs_request(rq)) {
  525. if (((long)rq->sector % floppy->bs_factor) ||
  526. (rq->nr_sectors % floppy->bs_factor)) {
  527. printk(KERN_ERR "%s: unsupported r/w request size\n",
  528. drive->name);
  529. idefloppy_end_request(drive, 0, 0);
  530. return ide_stopped;
  531. }
  532. pc = idefloppy_next_pc_storage(drive);
  533. idefloppy_create_rw_cmd(floppy, pc, rq, block);
  534. } else if (blk_special_request(rq)) {
  535. pc = (struct ide_atapi_pc *) rq->buffer;
  536. } else if (blk_pc_request(rq)) {
  537. pc = idefloppy_next_pc_storage(drive);
  538. idefloppy_blockpc_cmd(floppy, pc, rq);
  539. } else {
  540. blk_dump_rq_flags(rq,
  541. "ide-floppy: unsupported command in queue");
  542. idefloppy_end_request(drive, 0, 0);
  543. return ide_stopped;
  544. }
  545. pc->rq = rq;
  546. return idefloppy_issue_pc(drive, pc);
  547. }
  548. /*
  549. * Add a special packet command request to the tail of the request queue,
  550. * and wait for it to be serviced.
  551. */
  552. static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
  553. {
  554. struct ide_floppy_obj *floppy = drive->driver_data;
  555. struct request *rq;
  556. int error;
  557. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  558. rq->buffer = (char *) pc;
  559. rq->cmd_type = REQ_TYPE_SPECIAL;
  560. memcpy(rq->cmd, pc->c, 12);
  561. error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
  562. blk_put_request(rq);
  563. return error;
  564. }
  565. /*
  566. * Look at the flexible disk page parameters. We ignore the CHS capacity
  567. * parameters and use the LBA parameters instead.
  568. */
  569. static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
  570. {
  571. idefloppy_floppy_t *floppy = drive->driver_data;
  572. struct ide_atapi_pc pc;
  573. u8 *page;
  574. int capacity, lba_capacity;
  575. u16 transfer_rate, sector_size, cyls, rpm;
  576. u8 heads, sectors;
  577. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
  578. MODE_SENSE_CURRENT);
  579. if (idefloppy_queue_pc_tail(drive, &pc)) {
  580. printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
  581. " parameters\n");
  582. return 1;
  583. }
  584. floppy->wp = !!(pc.buf[3] & 0x80);
  585. set_disk_ro(floppy->disk, floppy->wp);
  586. page = &pc.buf[8];
  587. transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
  588. sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
  589. cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
  590. rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
  591. heads = pc.buf[8 + 4];
  592. sectors = pc.buf[8 + 5];
  593. capacity = cyls * heads * sectors * sector_size;
  594. if (memcmp(page, &floppy->flexible_disk_page, 32))
  595. printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  596. "%d sector size, %d rpm\n",
  597. drive->name, capacity / 1024, cyls, heads,
  598. sectors, transfer_rate / 8, sector_size, rpm);
  599. memcpy(&floppy->flexible_disk_page, page, 32);
  600. drive->bios_cyl = cyls;
  601. drive->bios_head = heads;
  602. drive->bios_sect = sectors;
  603. lba_capacity = floppy->blocks * floppy->block_size;
  604. if (capacity < lba_capacity) {
  605. printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
  606. "bytes, but the drive only handles %d\n",
  607. drive->name, lba_capacity, capacity);
  608. floppy->blocks = floppy->block_size ?
  609. capacity / floppy->block_size : 0;
  610. }
  611. return 0;
  612. }
  613. static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
  614. {
  615. idefloppy_floppy_t *floppy = drive->driver_data;
  616. struct ide_atapi_pc pc;
  617. floppy->srfp = 0;
  618. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
  619. MODE_SENSE_CURRENT);
  620. pc.flags |= PC_FLAG_SUPPRESS_ERROR;
  621. if (idefloppy_queue_pc_tail(drive, &pc))
  622. return 1;
  623. floppy->srfp = pc.buf[8 + 2] & 0x40;
  624. return 0;
  625. }
  626. /*
  627. * Determine if a media is present in the floppy drive, and if so, its LBA
  628. * capacity.
  629. */
  630. static int ide_floppy_get_capacity(ide_drive_t *drive)
  631. {
  632. idefloppy_floppy_t *floppy = drive->driver_data;
  633. struct ide_atapi_pc pc;
  634. u8 *cap_desc;
  635. u8 header_len, desc_cnt;
  636. int i, rc = 1, blocks, length;
  637. drive->bios_cyl = 0;
  638. drive->bios_head = drive->bios_sect = 0;
  639. floppy->blocks = 0;
  640. floppy->bs_factor = 1;
  641. set_capacity(floppy->disk, 0);
  642. idefloppy_create_read_capacity_cmd(&pc);
  643. if (idefloppy_queue_pc_tail(drive, &pc)) {
  644. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  645. return 1;
  646. }
  647. header_len = pc.buf[3];
  648. cap_desc = &pc.buf[4];
  649. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  650. for (i = 0; i < desc_cnt; i++) {
  651. unsigned int desc_start = 4 + i*8;
  652. blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
  653. length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
  654. debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
  655. i, blocks * length / 1024, blocks, length);
  656. if (i)
  657. continue;
  658. /*
  659. * the code below is valid only for the 1st descriptor, ie i=0
  660. */
  661. switch (pc.buf[desc_start + 4] & 0x03) {
  662. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  663. case CAPACITY_UNFORMATTED:
  664. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
  665. /*
  666. * If it is not a clik drive, break out
  667. * (maintains previous driver behaviour)
  668. */
  669. break;
  670. case CAPACITY_CURRENT:
  671. /* Normal Zip/LS-120 disks */
  672. if (memcmp(cap_desc, &floppy->cap_desc, 8))
  673. printk(KERN_INFO "%s: %dkB, %d blocks, %d "
  674. "sector size\n", drive->name,
  675. blocks * length / 1024, blocks, length);
  676. memcpy(&floppy->cap_desc, cap_desc, 8);
  677. if (!length || length % 512) {
  678. printk(KERN_NOTICE "%s: %d bytes block size "
  679. "not supported\n", drive->name, length);
  680. } else {
  681. floppy->blocks = blocks;
  682. floppy->block_size = length;
  683. floppy->bs_factor = length / 512;
  684. if (floppy->bs_factor != 1)
  685. printk(KERN_NOTICE "%s: warning: non "
  686. "512 bytes block size not "
  687. "fully supported\n",
  688. drive->name);
  689. rc = 0;
  690. }
  691. break;
  692. case CAPACITY_NO_CARTRIDGE:
  693. /*
  694. * This is a KERN_ERR so it appears on screen
  695. * for the user to see
  696. */
  697. printk(KERN_ERR "%s: No disk in drive\n", drive->name);
  698. break;
  699. case CAPACITY_INVALID:
  700. printk(KERN_ERR "%s: Invalid capacity for disk "
  701. "in drive\n", drive->name);
  702. break;
  703. }
  704. debug_log("Descriptor 0 Code: %d\n",
  705. pc.buf[desc_start + 4] & 0x03);
  706. }
  707. /* Clik! disk does not support get_flexible_disk_page */
  708. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
  709. (void) ide_floppy_get_flexible_disk_page(drive);
  710. set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
  711. return rc;
  712. }
  713. /*
  714. * Obtain the list of formattable capacities.
  715. * Very similar to ide_floppy_get_capacity, except that we push the capacity
  716. * descriptors to userland, instead of our own structures.
  717. *
  718. * Userland gives us the following structure:
  719. *
  720. * struct idefloppy_format_capacities {
  721. * int nformats;
  722. * struct {
  723. * int nblocks;
  724. * int blocksize;
  725. * } formats[];
  726. * };
  727. *
  728. * userland initializes nformats to the number of allocated formats[] records.
  729. * On exit we set nformats to the number of records we've actually initialized.
  730. */
  731. static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
  732. {
  733. struct ide_atapi_pc pc;
  734. u8 header_len, desc_cnt;
  735. int i, blocks, length, u_array_size, u_index;
  736. int __user *argp;
  737. if (get_user(u_array_size, arg))
  738. return -EFAULT;
  739. if (u_array_size <= 0)
  740. return -EINVAL;
  741. idefloppy_create_read_capacity_cmd(&pc);
  742. if (idefloppy_queue_pc_tail(drive, &pc)) {
  743. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  744. return -EIO;
  745. }
  746. header_len = pc.buf[3];
  747. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  748. u_index = 0;
  749. argp = arg + 1;
  750. /*
  751. * We always skip the first capacity descriptor. That's the current
  752. * capacity. We are interested in the remaining descriptors, the
  753. * formattable capacities.
  754. */
  755. for (i = 1; i < desc_cnt; i++) {
  756. unsigned int desc_start = 4 + i*8;
  757. if (u_index >= u_array_size)
  758. break; /* User-supplied buffer too small */
  759. blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
  760. length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
  761. if (put_user(blocks, argp))
  762. return -EFAULT;
  763. ++argp;
  764. if (put_user(length, argp))
  765. return -EFAULT;
  766. ++argp;
  767. ++u_index;
  768. }
  769. if (put_user(u_index, arg))
  770. return -EFAULT;
  771. return 0;
  772. }
  773. /*
  774. * Get ATAPI_FORMAT_UNIT progress indication.
  775. *
  776. * Userland gives a pointer to an int. The int is set to a progress
  777. * indicator 0-65536, with 65536=100%.
  778. *
  779. * If the drive does not support format progress indication, we just check
  780. * the dsc bit, and return either 0 or 65536.
  781. */
  782. static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
  783. {
  784. idefloppy_floppy_t *floppy = drive->driver_data;
  785. struct ide_atapi_pc pc;
  786. int progress_indication = 0x10000;
  787. if (floppy->srfp) {
  788. idefloppy_create_request_sense_cmd(&pc);
  789. if (idefloppy_queue_pc_tail(drive, &pc))
  790. return -EIO;
  791. if (floppy->sense_key == 2 &&
  792. floppy->asc == 4 &&
  793. floppy->ascq == 4)
  794. progress_indication = floppy->progress_indication;
  795. /* Else assume format_unit has finished, and we're at 0x10000 */
  796. } else {
  797. ide_hwif_t *hwif = drive->hwif;
  798. unsigned long flags;
  799. u8 stat;
  800. local_irq_save(flags);
  801. stat = hwif->tp_ops->read_status(hwif);
  802. local_irq_restore(flags);
  803. progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
  804. }
  805. if (put_user(progress_indication, arg))
  806. return -EFAULT;
  807. return 0;
  808. }
  809. static sector_t idefloppy_capacity(ide_drive_t *drive)
  810. {
  811. idefloppy_floppy_t *floppy = drive->driver_data;
  812. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  813. return capacity;
  814. }
  815. #ifdef CONFIG_IDE_PROC_FS
  816. ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
  817. ide_devset_rw(bios_head, 0, 255, bios_head);
  818. ide_devset_rw(bios_sect, 0, 63, bios_sect);
  819. static int get_ticks(ide_drive_t *drive)
  820. {
  821. idefloppy_floppy_t *floppy = drive->driver_data;
  822. return floppy->ticks;
  823. }
  824. static int set_ticks(ide_drive_t *drive, int arg)
  825. {
  826. idefloppy_floppy_t *floppy = drive->driver_data;
  827. floppy->ticks = arg;
  828. return 0;
  829. }
  830. IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
  831. static const struct ide_devset *idefloppy_settings[] = {
  832. &ide_devset_bios_cyl,
  833. &ide_devset_bios_head,
  834. &ide_devset_bios_sect,
  835. &ide_devset_ticks,
  836. NULL
  837. };
  838. #endif
  839. static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
  840. {
  841. u16 *id = drive->id;
  842. u8 gcw[2];
  843. *((u16 *)&gcw) = id[ATA_ID_CONFIG];
  844. floppy->pc = floppy->pc_stack;
  845. drive->pc_callback = ide_floppy_callback;
  846. if (((gcw[0] & 0x60) >> 5) == 1)
  847. drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
  848. /*
  849. * We used to check revisions here. At this point however I'm giving up.
  850. * Just assume they are all broken, its easier.
  851. *
  852. * The actual reason for the workarounds was likely a driver bug after
  853. * all rather than a firmware bug, and the workaround below used to hide
  854. * it. It should be fixed as of version 1.9, but to be on the safe side
  855. * we'll leave the limitation below for the 2.2.x tree.
  856. */
  857. if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
  858. drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
  859. /* This value will be visible in the /proc/ide/hdx/settings */
  860. floppy->ticks = IDEFLOPPY_TICKS_DELAY;
  861. blk_queue_max_sectors(drive->queue, 64);
  862. }
  863. /*
  864. * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
  865. * nasty clicking noises without it, so please don't remove this.
  866. */
  867. if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
  868. blk_queue_max_sectors(drive->queue, 64);
  869. drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
  870. }
  871. (void) ide_floppy_get_capacity(drive);
  872. ide_proc_register_driver(drive, floppy->driver);
  873. }
  874. static void ide_floppy_remove(ide_drive_t *drive)
  875. {
  876. idefloppy_floppy_t *floppy = drive->driver_data;
  877. struct gendisk *g = floppy->disk;
  878. ide_proc_unregister_driver(drive, floppy->driver);
  879. del_gendisk(g);
  880. ide_floppy_put(floppy);
  881. }
  882. static void idefloppy_cleanup_obj(struct kref *kref)
  883. {
  884. struct ide_floppy_obj *floppy = to_ide_floppy(kref);
  885. ide_drive_t *drive = floppy->drive;
  886. struct gendisk *g = floppy->disk;
  887. drive->driver_data = NULL;
  888. g->private_data = NULL;
  889. put_disk(g);
  890. kfree(floppy);
  891. }
  892. #ifdef CONFIG_IDE_PROC_FS
  893. static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
  894. int count, int *eof, void *data)
  895. {
  896. ide_drive_t*drive = (ide_drive_t *)data;
  897. int len;
  898. len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
  899. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  900. }
  901. static ide_proc_entry_t idefloppy_proc[] = {
  902. { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
  903. { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
  904. { NULL, 0, NULL, NULL }
  905. };
  906. #endif /* CONFIG_IDE_PROC_FS */
  907. static int ide_floppy_probe(ide_drive_t *);
  908. static ide_driver_t idefloppy_driver = {
  909. .gen_driver = {
  910. .owner = THIS_MODULE,
  911. .name = "ide-floppy",
  912. .bus = &ide_bus_type,
  913. },
  914. .probe = ide_floppy_probe,
  915. .remove = ide_floppy_remove,
  916. .version = IDEFLOPPY_VERSION,
  917. .media = ide_floppy,
  918. .do_request = idefloppy_do_request,
  919. .end_request = idefloppy_end_request,
  920. .error = __ide_error,
  921. #ifdef CONFIG_IDE_PROC_FS
  922. .proc = idefloppy_proc,
  923. .settings = idefloppy_settings,
  924. #endif
  925. };
  926. static int idefloppy_open(struct inode *inode, struct file *filp)
  927. {
  928. struct gendisk *disk = inode->i_bdev->bd_disk;
  929. struct ide_floppy_obj *floppy;
  930. ide_drive_t *drive;
  931. struct ide_atapi_pc pc;
  932. int ret = 0;
  933. debug_log("Reached %s\n", __func__);
  934. floppy = ide_floppy_get(disk);
  935. if (!floppy)
  936. return -ENXIO;
  937. drive = floppy->drive;
  938. floppy->openers++;
  939. if (floppy->openers == 1) {
  940. drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
  941. /* Just in case */
  942. idefloppy_init_pc(&pc);
  943. pc.c[0] = GPCMD_TEST_UNIT_READY;
  944. if (idefloppy_queue_pc_tail(drive, &pc)) {
  945. idefloppy_create_start_stop_cmd(&pc, 1);
  946. (void) idefloppy_queue_pc_tail(drive, &pc);
  947. }
  948. if (ide_floppy_get_capacity(drive)
  949. && (filp->f_flags & O_NDELAY) == 0
  950. /*
  951. * Allow O_NDELAY to open a drive without a disk, or with an
  952. * unreadable disk, so that we can get the format capacity
  953. * of the drive or begin the format - Sam
  954. */
  955. ) {
  956. ret = -EIO;
  957. goto out_put_floppy;
  958. }
  959. if (floppy->wp && (filp->f_mode & 2)) {
  960. ret = -EROFS;
  961. goto out_put_floppy;
  962. }
  963. drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
  964. /* IOMEGA Clik! drives do not support lock/unlock commands */
  965. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
  966. idefloppy_create_prevent_cmd(&pc, 1);
  967. (void) idefloppy_queue_pc_tail(drive, &pc);
  968. }
  969. check_disk_change(inode->i_bdev);
  970. } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
  971. ret = -EBUSY;
  972. goto out_put_floppy;
  973. }
  974. return 0;
  975. out_put_floppy:
  976. floppy->openers--;
  977. ide_floppy_put(floppy);
  978. return ret;
  979. }
  980. static int idefloppy_release(struct inode *inode, struct file *filp)
  981. {
  982. struct gendisk *disk = inode->i_bdev->bd_disk;
  983. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  984. ide_drive_t *drive = floppy->drive;
  985. struct ide_atapi_pc pc;
  986. debug_log("Reached %s\n", __func__);
  987. if (floppy->openers == 1) {
  988. /* IOMEGA Clik! drives do not support lock/unlock commands */
  989. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
  990. idefloppy_create_prevent_cmd(&pc, 0);
  991. (void) idefloppy_queue_pc_tail(drive, &pc);
  992. }
  993. drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
  994. }
  995. floppy->openers--;
  996. ide_floppy_put(floppy);
  997. return 0;
  998. }
  999. static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1000. {
  1001. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1002. ide_drive_t *drive = floppy->drive;
  1003. geo->heads = drive->bios_head;
  1004. geo->sectors = drive->bios_sect;
  1005. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  1006. return 0;
  1007. }
  1008. static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
  1009. unsigned long arg, unsigned int cmd)
  1010. {
  1011. idefloppy_floppy_t *floppy = drive->driver_data;
  1012. if (floppy->openers > 1)
  1013. return -EBUSY;
  1014. /* The IOMEGA Clik! Drive doesn't support this command -
  1015. * no room for an eject mechanism */
  1016. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
  1017. int prevent = arg ? 1 : 0;
  1018. if (cmd == CDROMEJECT)
  1019. prevent = 0;
  1020. idefloppy_create_prevent_cmd(pc, prevent);
  1021. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1022. }
  1023. if (cmd == CDROMEJECT) {
  1024. idefloppy_create_start_stop_cmd(pc, 2);
  1025. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1026. }
  1027. return 0;
  1028. }
  1029. static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
  1030. {
  1031. idefloppy_floppy_t *floppy = drive->driver_data;
  1032. struct ide_atapi_pc pc;
  1033. int blocks, length, flags, err = 0;
  1034. if (floppy->openers > 1) {
  1035. /* Don't format if someone is using the disk */
  1036. drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
  1037. return -EBUSY;
  1038. }
  1039. drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
  1040. /*
  1041. * Send ATAPI_FORMAT_UNIT to the drive.
  1042. *
  1043. * Userland gives us the following structure:
  1044. *
  1045. * struct idefloppy_format_command {
  1046. * int nblocks;
  1047. * int blocksize;
  1048. * int flags;
  1049. * } ;
  1050. *
  1051. * flags is a bitmask, currently, the only defined flag is:
  1052. *
  1053. * 0x01 - verify media after format.
  1054. */
  1055. if (get_user(blocks, arg) ||
  1056. get_user(length, arg+1) ||
  1057. get_user(flags, arg+2)) {
  1058. err = -EFAULT;
  1059. goto out;
  1060. }
  1061. (void) idefloppy_get_sfrp_bit(drive);
  1062. idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
  1063. if (idefloppy_queue_pc_tail(drive, &pc))
  1064. err = -EIO;
  1065. out:
  1066. if (err)
  1067. drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
  1068. return err;
  1069. }
  1070. static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
  1071. unsigned int cmd, void __user *argp)
  1072. {
  1073. switch (cmd) {
  1074. case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
  1075. return 0;
  1076. case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
  1077. return ide_floppy_get_format_capacities(drive, argp);
  1078. case IDEFLOPPY_IOCTL_FORMAT_START:
  1079. if (!(file->f_mode & 2))
  1080. return -EPERM;
  1081. return ide_floppy_format_unit(drive, (int __user *)argp);
  1082. case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
  1083. return ide_floppy_get_format_progress(drive, argp);
  1084. default:
  1085. return -ENOTTY;
  1086. }
  1087. }
  1088. static int idefloppy_ioctl(struct inode *inode, struct file *file,
  1089. unsigned int cmd, unsigned long arg)
  1090. {
  1091. struct block_device *bdev = inode->i_bdev;
  1092. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1093. ide_drive_t *drive = floppy->drive;
  1094. struct ide_atapi_pc pc;
  1095. void __user *argp = (void __user *)arg;
  1096. int err;
  1097. if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
  1098. return ide_floppy_lockdoor(drive, &pc, arg, cmd);
  1099. err = ide_floppy_format_ioctl(drive, file, cmd, argp);
  1100. if (err != -ENOTTY)
  1101. return err;
  1102. /*
  1103. * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
  1104. * and CDROM_SEND_PACKET (legacy) ioctls
  1105. */
  1106. if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
  1107. err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
  1108. bdev->bd_disk, cmd, argp);
  1109. if (err == -ENOTTY)
  1110. err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  1111. return err;
  1112. }
  1113. static int idefloppy_media_changed(struct gendisk *disk)
  1114. {
  1115. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1116. ide_drive_t *drive = floppy->drive;
  1117. int ret;
  1118. /* do not scan partitions twice if this is a removable device */
  1119. if (drive->attach) {
  1120. drive->attach = 0;
  1121. return 0;
  1122. }
  1123. ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
  1124. drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
  1125. return ret;
  1126. }
  1127. static int idefloppy_revalidate_disk(struct gendisk *disk)
  1128. {
  1129. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1130. set_capacity(disk, idefloppy_capacity(floppy->drive));
  1131. return 0;
  1132. }
  1133. static struct block_device_operations idefloppy_ops = {
  1134. .owner = THIS_MODULE,
  1135. .open = idefloppy_open,
  1136. .release = idefloppy_release,
  1137. .ioctl = idefloppy_ioctl,
  1138. .getgeo = idefloppy_getgeo,
  1139. .media_changed = idefloppy_media_changed,
  1140. .revalidate_disk = idefloppy_revalidate_disk
  1141. };
  1142. static int ide_floppy_probe(ide_drive_t *drive)
  1143. {
  1144. idefloppy_floppy_t *floppy;
  1145. struct gendisk *g;
  1146. if (!strstr("ide-floppy", drive->driver_req))
  1147. goto failed;
  1148. if (drive->media != ide_floppy)
  1149. goto failed;
  1150. if (!ide_check_atapi_device(drive, DRV_NAME)) {
  1151. printk(KERN_ERR "ide-floppy: %s: not supported by this version"
  1152. " of ide-floppy\n", drive->name);
  1153. goto failed;
  1154. }
  1155. floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
  1156. if (!floppy) {
  1157. printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
  1158. " structure\n", drive->name);
  1159. goto failed;
  1160. }
  1161. g = alloc_disk(1 << PARTN_BITS);
  1162. if (!g)
  1163. goto out_free_floppy;
  1164. ide_init_disk(g, drive);
  1165. kref_init(&floppy->kref);
  1166. floppy->drive = drive;
  1167. floppy->driver = &idefloppy_driver;
  1168. floppy->disk = g;
  1169. g->private_data = &floppy->driver;
  1170. drive->driver_data = floppy;
  1171. idefloppy_setup(drive, floppy);
  1172. g->minors = 1 << PARTN_BITS;
  1173. g->driverfs_dev = &drive->gendev;
  1174. g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
  1175. g->fops = &idefloppy_ops;
  1176. drive->attach = 1;
  1177. add_disk(g);
  1178. return 0;
  1179. out_free_floppy:
  1180. kfree(floppy);
  1181. failed:
  1182. return -ENODEV;
  1183. }
  1184. static void __exit idefloppy_exit(void)
  1185. {
  1186. driver_unregister(&idefloppy_driver.gen_driver);
  1187. }
  1188. static int __init idefloppy_init(void)
  1189. {
  1190. printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
  1191. return driver_register(&idefloppy_driver.gen_driver);
  1192. }
  1193. MODULE_ALIAS("ide:*m-floppy*");
  1194. module_init(idefloppy_init);
  1195. module_exit(idefloppy_exit);
  1196. MODULE_LICENSE("GPL");
  1197. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");