ide-floppy.c 34 KB

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