ide-floppy.c 38 KB

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