ide-floppy.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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->tp_ops->output_data(drive, NULL, data, count);
  214. else
  215. hwif->tp_ops->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->tp_ops->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. ide_hwif_t *hwif = drive->hwif;
  807. unsigned long flags;
  808. u8 stat;
  809. local_irq_save(flags);
  810. stat = hwif->tp_ops->read_status(hwif);
  811. local_irq_restore(flags);
  812. progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
  813. }
  814. if (put_user(progress_indication, arg))
  815. return (-EFAULT);
  816. return (0);
  817. }
  818. static sector_t idefloppy_capacity(ide_drive_t *drive)
  819. {
  820. idefloppy_floppy_t *floppy = drive->driver_data;
  821. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  822. return capacity;
  823. }
  824. /*
  825. * Check whether we can support a drive, based on the ATAPI IDENTIFY command
  826. * results.
  827. */
  828. static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
  829. {
  830. u8 gcw[2];
  831. u8 device_type, protocol, removable, drq_type, packet_size;
  832. *((u16 *) &gcw) = id->config;
  833. device_type = gcw[1] & 0x1F;
  834. removable = (gcw[0] & 0x80) >> 7;
  835. protocol = (gcw[1] & 0xC0) >> 6;
  836. drq_type = (gcw[0] & 0x60) >> 5;
  837. packet_size = gcw[0] & 0x03;
  838. #ifdef CONFIG_PPC
  839. /* kludge for Apple PowerBook internal zip */
  840. if (device_type == 5 &&
  841. !strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP"))
  842. device_type = 0;
  843. #endif
  844. if (protocol != 2)
  845. printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
  846. protocol);
  847. else if (device_type != 0)
  848. printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
  849. "to floppy\n", device_type);
  850. else if (!removable)
  851. printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
  852. else if (drq_type == 3)
  853. printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
  854. "supported\n", drq_type);
  855. else if (packet_size != 0)
  856. printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
  857. "bytes\n", packet_size);
  858. else
  859. return 1;
  860. return 0;
  861. }
  862. #ifdef CONFIG_IDE_PROC_FS
  863. static void idefloppy_add_settings(ide_drive_t *drive)
  864. {
  865. idefloppy_floppy_t *floppy = drive->driver_data;
  866. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
  867. &drive->bios_cyl, NULL);
  868. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
  869. &drive->bios_head, NULL);
  870. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
  871. &drive->bios_sect, NULL);
  872. ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
  873. &floppy->ticks, NULL);
  874. }
  875. #else
  876. static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
  877. #endif
  878. static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
  879. {
  880. u8 gcw[2];
  881. *((u16 *) &gcw) = drive->id->config;
  882. floppy->pc = floppy->pc_stack;
  883. if (((gcw[0] & 0x60) >> 5) == 1)
  884. floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
  885. /*
  886. * We used to check revisions here. At this point however I'm giving up.
  887. * Just assume they are all broken, its easier.
  888. *
  889. * The actual reason for the workarounds was likely a driver bug after
  890. * all rather than a firmware bug, and the workaround below used to hide
  891. * it. It should be fixed as of version 1.9, but to be on the safe side
  892. * we'll leave the limitation below for the 2.2.x tree.
  893. */
  894. if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
  895. floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
  896. /* This value will be visible in the /proc/ide/hdx/settings */
  897. floppy->ticks = IDEFLOPPY_TICKS_DELAY;
  898. blk_queue_max_sectors(drive->queue, 64);
  899. }
  900. /*
  901. * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
  902. * nasty clicking noises without it, so please don't remove this.
  903. */
  904. if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
  905. blk_queue_max_sectors(drive->queue, 64);
  906. floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
  907. }
  908. (void) ide_floppy_get_capacity(drive);
  909. idefloppy_add_settings(drive);
  910. }
  911. static void ide_floppy_remove(ide_drive_t *drive)
  912. {
  913. idefloppy_floppy_t *floppy = drive->driver_data;
  914. struct gendisk *g = floppy->disk;
  915. ide_proc_unregister_driver(drive, floppy->driver);
  916. del_gendisk(g);
  917. ide_floppy_put(floppy);
  918. }
  919. static void idefloppy_cleanup_obj(struct kref *kref)
  920. {
  921. struct ide_floppy_obj *floppy = to_ide_floppy(kref);
  922. ide_drive_t *drive = floppy->drive;
  923. struct gendisk *g = floppy->disk;
  924. drive->driver_data = NULL;
  925. g->private_data = NULL;
  926. put_disk(g);
  927. kfree(floppy);
  928. }
  929. #ifdef CONFIG_IDE_PROC_FS
  930. static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
  931. int count, int *eof, void *data)
  932. {
  933. ide_drive_t*drive = (ide_drive_t *)data;
  934. int len;
  935. len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
  936. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  937. }
  938. static ide_proc_entry_t idefloppy_proc[] = {
  939. { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
  940. { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
  941. { NULL, 0, NULL, NULL }
  942. };
  943. #endif /* CONFIG_IDE_PROC_FS */
  944. static int ide_floppy_probe(ide_drive_t *);
  945. static ide_driver_t idefloppy_driver = {
  946. .gen_driver = {
  947. .owner = THIS_MODULE,
  948. .name = "ide-floppy",
  949. .bus = &ide_bus_type,
  950. },
  951. .probe = ide_floppy_probe,
  952. .remove = ide_floppy_remove,
  953. .version = IDEFLOPPY_VERSION,
  954. .media = ide_floppy,
  955. .supports_dsc_overlap = 0,
  956. .do_request = idefloppy_do_request,
  957. .end_request = idefloppy_end_request,
  958. .error = __ide_error,
  959. #ifdef CONFIG_IDE_PROC_FS
  960. .proc = idefloppy_proc,
  961. #endif
  962. };
  963. static int idefloppy_open(struct inode *inode, struct file *filp)
  964. {
  965. struct gendisk *disk = inode->i_bdev->bd_disk;
  966. struct ide_floppy_obj *floppy;
  967. ide_drive_t *drive;
  968. struct ide_atapi_pc pc;
  969. int ret = 0;
  970. debug_log("Reached %s\n", __func__);
  971. floppy = ide_floppy_get(disk);
  972. if (!floppy)
  973. return -ENXIO;
  974. drive = floppy->drive;
  975. floppy->openers++;
  976. if (floppy->openers == 1) {
  977. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  978. /* Just in case */
  979. idefloppy_init_pc(&pc);
  980. pc.c[0] = GPCMD_TEST_UNIT_READY;
  981. if (idefloppy_queue_pc_tail(drive, &pc)) {
  982. idefloppy_create_start_stop_cmd(&pc, 1);
  983. (void) idefloppy_queue_pc_tail(drive, &pc);
  984. }
  985. if (ide_floppy_get_capacity(drive)
  986. && (filp->f_flags & O_NDELAY) == 0
  987. /*
  988. * Allow O_NDELAY to open a drive without a disk, or with an
  989. * unreadable disk, so that we can get the format capacity
  990. * of the drive or begin the format - Sam
  991. */
  992. ) {
  993. ret = -EIO;
  994. goto out_put_floppy;
  995. }
  996. if (floppy->wp && (filp->f_mode & 2)) {
  997. ret = -EROFS;
  998. goto out_put_floppy;
  999. }
  1000. floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
  1001. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1002. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1003. idefloppy_create_prevent_cmd(&pc, 1);
  1004. (void) idefloppy_queue_pc_tail(drive, &pc);
  1005. }
  1006. check_disk_change(inode->i_bdev);
  1007. } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
  1008. ret = -EBUSY;
  1009. goto out_put_floppy;
  1010. }
  1011. return 0;
  1012. out_put_floppy:
  1013. floppy->openers--;
  1014. ide_floppy_put(floppy);
  1015. return ret;
  1016. }
  1017. static int idefloppy_release(struct inode *inode, struct file *filp)
  1018. {
  1019. struct gendisk *disk = inode->i_bdev->bd_disk;
  1020. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1021. ide_drive_t *drive = floppy->drive;
  1022. struct ide_atapi_pc pc;
  1023. debug_log("Reached %s\n", __func__);
  1024. if (floppy->openers == 1) {
  1025. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1026. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1027. idefloppy_create_prevent_cmd(&pc, 0);
  1028. (void) idefloppy_queue_pc_tail(drive, &pc);
  1029. }
  1030. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1031. }
  1032. floppy->openers--;
  1033. ide_floppy_put(floppy);
  1034. return 0;
  1035. }
  1036. static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1037. {
  1038. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1039. ide_drive_t *drive = floppy->drive;
  1040. geo->heads = drive->bios_head;
  1041. geo->sectors = drive->bios_sect;
  1042. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  1043. return 0;
  1044. }
  1045. static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy,
  1046. struct ide_atapi_pc *pc, unsigned long arg, unsigned int cmd)
  1047. {
  1048. if (floppy->openers > 1)
  1049. return -EBUSY;
  1050. /* The IOMEGA Clik! Drive doesn't support this command -
  1051. * no room for an eject mechanism */
  1052. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
  1053. int prevent = arg ? 1 : 0;
  1054. if (cmd == CDROMEJECT)
  1055. prevent = 0;
  1056. idefloppy_create_prevent_cmd(pc, prevent);
  1057. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1058. }
  1059. if (cmd == CDROMEJECT) {
  1060. idefloppy_create_start_stop_cmd(pc, 2);
  1061. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1062. }
  1063. return 0;
  1064. }
  1065. static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
  1066. int __user *arg)
  1067. {
  1068. int blocks, length, flags, err = 0;
  1069. struct ide_atapi_pc pc;
  1070. if (floppy->openers > 1) {
  1071. /* Don't format if someone is using the disk */
  1072. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1073. return -EBUSY;
  1074. }
  1075. floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1076. /*
  1077. * Send ATAPI_FORMAT_UNIT to the drive.
  1078. *
  1079. * Userland gives us the following structure:
  1080. *
  1081. * struct idefloppy_format_command {
  1082. * int nblocks;
  1083. * int blocksize;
  1084. * int flags;
  1085. * } ;
  1086. *
  1087. * flags is a bitmask, currently, the only defined flag is:
  1088. *
  1089. * 0x01 - verify media after format.
  1090. */
  1091. if (get_user(blocks, arg) ||
  1092. get_user(length, arg+1) ||
  1093. get_user(flags, arg+2)) {
  1094. err = -EFAULT;
  1095. goto out;
  1096. }
  1097. (void) idefloppy_get_sfrp_bit(floppy->drive);
  1098. idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
  1099. if (idefloppy_queue_pc_tail(floppy->drive, &pc))
  1100. err = -EIO;
  1101. out:
  1102. if (err)
  1103. floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
  1104. return err;
  1105. }
  1106. static int idefloppy_ioctl(struct inode *inode, struct file *file,
  1107. unsigned int cmd, unsigned long arg)
  1108. {
  1109. struct block_device *bdev = inode->i_bdev;
  1110. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1111. ide_drive_t *drive = floppy->drive;
  1112. struct ide_atapi_pc pc;
  1113. void __user *argp = (void __user *)arg;
  1114. int err;
  1115. switch (cmd) {
  1116. case CDROMEJECT:
  1117. /* fall through */
  1118. case CDROM_LOCKDOOR:
  1119. return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
  1120. case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
  1121. return 0;
  1122. case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
  1123. return ide_floppy_get_format_capacities(drive, argp);
  1124. case IDEFLOPPY_IOCTL_FORMAT_START:
  1125. if (!(file->f_mode & 2))
  1126. return -EPERM;
  1127. return ide_floppy_format_unit(floppy, (int __user *)arg);
  1128. case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
  1129. return idefloppy_get_format_progress(drive, argp);
  1130. }
  1131. /*
  1132. * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
  1133. * and CDROM_SEND_PACKET (legacy) ioctls
  1134. */
  1135. if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
  1136. err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
  1137. bdev->bd_disk, cmd, argp);
  1138. else
  1139. err = -ENOTTY;
  1140. if (err == -ENOTTY)
  1141. err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  1142. return err;
  1143. }
  1144. static int idefloppy_media_changed(struct gendisk *disk)
  1145. {
  1146. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1147. ide_drive_t *drive = floppy->drive;
  1148. int ret;
  1149. /* do not scan partitions twice if this is a removable device */
  1150. if (drive->attach) {
  1151. drive->attach = 0;
  1152. return 0;
  1153. }
  1154. ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
  1155. floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
  1156. return ret;
  1157. }
  1158. static int idefloppy_revalidate_disk(struct gendisk *disk)
  1159. {
  1160. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1161. set_capacity(disk, idefloppy_capacity(floppy->drive));
  1162. return 0;
  1163. }
  1164. static struct block_device_operations idefloppy_ops = {
  1165. .owner = THIS_MODULE,
  1166. .open = idefloppy_open,
  1167. .release = idefloppy_release,
  1168. .ioctl = idefloppy_ioctl,
  1169. .getgeo = idefloppy_getgeo,
  1170. .media_changed = idefloppy_media_changed,
  1171. .revalidate_disk = idefloppy_revalidate_disk
  1172. };
  1173. static int ide_floppy_probe(ide_drive_t *drive)
  1174. {
  1175. idefloppy_floppy_t *floppy;
  1176. struct gendisk *g;
  1177. if (!strstr("ide-floppy", drive->driver_req))
  1178. goto failed;
  1179. if (!drive->present)
  1180. goto failed;
  1181. if (drive->media != ide_floppy)
  1182. goto failed;
  1183. if (!idefloppy_identify_device(drive, drive->id)) {
  1184. printk(KERN_ERR "ide-floppy: %s: not supported by this version"
  1185. " of ide-floppy\n", drive->name);
  1186. goto failed;
  1187. }
  1188. floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
  1189. if (!floppy) {
  1190. printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
  1191. " structure\n", drive->name);
  1192. goto failed;
  1193. }
  1194. g = alloc_disk(1 << PARTN_BITS);
  1195. if (!g)
  1196. goto out_free_floppy;
  1197. ide_init_disk(g, drive);
  1198. ide_proc_register_driver(drive, &idefloppy_driver);
  1199. kref_init(&floppy->kref);
  1200. floppy->drive = drive;
  1201. floppy->driver = &idefloppy_driver;
  1202. floppy->disk = g;
  1203. g->private_data = &floppy->driver;
  1204. drive->driver_data = floppy;
  1205. idefloppy_setup(drive, floppy);
  1206. g->minors = 1 << PARTN_BITS;
  1207. g->driverfs_dev = &drive->gendev;
  1208. g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
  1209. g->fops = &idefloppy_ops;
  1210. drive->attach = 1;
  1211. add_disk(g);
  1212. return 0;
  1213. out_free_floppy:
  1214. kfree(floppy);
  1215. failed:
  1216. return -ENODEV;
  1217. }
  1218. static void __exit idefloppy_exit(void)
  1219. {
  1220. driver_unregister(&idefloppy_driver.gen_driver);
  1221. }
  1222. static int __init idefloppy_init(void)
  1223. {
  1224. printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
  1225. return driver_register(&idefloppy_driver.gen_driver);
  1226. }
  1227. MODULE_ALIAS("ide:*m-floppy*");
  1228. module_init(idefloppy_init);
  1229. module_exit(idefloppy_exit);
  1230. MODULE_LICENSE("GPL");
  1231. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");