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. }
  301. static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
  302. {
  303. idefloppy_init_pc(pc);
  304. pc->c[0] = GPCMD_REQUEST_SENSE;
  305. pc->c[4] = 255;
  306. pc->req_xfer = 18;
  307. }
  308. /*
  309. * Called when an error was detected during the last packet command. We queue a
  310. * request sense packet command in the head of the request list.
  311. */
  312. static void idefloppy_retry_pc(ide_drive_t *drive)
  313. {
  314. struct ide_atapi_pc *pc;
  315. struct request *rq;
  316. (void)ide_read_error(drive);
  317. pc = idefloppy_next_pc_storage(drive);
  318. rq = idefloppy_next_rq_storage(drive);
  319. idefloppy_create_request_sense_cmd(pc);
  320. idefloppy_queue_pc_head(drive, pc, rq);
  321. }
  322. /* The usual interrupt handler called during a packet command. */
  323. static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
  324. {
  325. idefloppy_floppy_t *floppy = drive->driver_data;
  326. return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
  327. IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
  328. idefloppy_retry_pc, NULL, ide_floppy_io_buffers);
  329. }
  330. /*
  331. * What we have here is a classic case of a top half / bottom half interrupt
  332. * service routine. In interrupt mode, the device sends an interrupt to signal
  333. * that it is ready to receive a packet. However, we need to delay about 2-3
  334. * ticks before issuing the packet or we gets in trouble.
  335. */
  336. static int idefloppy_transfer_pc(ide_drive_t *drive)
  337. {
  338. idefloppy_floppy_t *floppy = drive->driver_data;
  339. /* Send the actual packet */
  340. drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
  341. /* Timeout for the packet command */
  342. return IDEFLOPPY_WAIT_CMD;
  343. }
  344. /*
  345. * Called as an interrupt (or directly). When the device says it's ready for a
  346. * packet, we schedule the packet transfer to occur about 2-3 ticks later in
  347. * transfer_pc.
  348. */
  349. static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
  350. {
  351. idefloppy_floppy_t *floppy = drive->driver_data;
  352. struct ide_atapi_pc *pc = floppy->pc;
  353. ide_expiry_t *expiry;
  354. unsigned int timeout;
  355. /*
  356. * The following delay solves a problem with ATAPI Zip 100 drives
  357. * where the Busy flag was apparently being deasserted before the
  358. * unit was ready to receive data. This was happening on a
  359. * 1200 MHz Athlon system. 10/26/01 25msec is too short,
  360. * 40 and 50msec work well. idefloppy_pc_intr will not be actually
  361. * used until after the packet is moved in about 50 msec.
  362. */
  363. if (pc->flags & PC_FLAG_ZIP_DRIVE) {
  364. timeout = floppy->ticks;
  365. expiry = &idefloppy_transfer_pc;
  366. } else {
  367. timeout = IDEFLOPPY_WAIT_CMD;
  368. expiry = NULL;
  369. }
  370. return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
  371. }
  372. static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
  373. struct ide_atapi_pc *pc)
  374. {
  375. /* supress error messages resulting from Medium not present */
  376. if (floppy->sense_key == 0x02 &&
  377. floppy->asc == 0x3a &&
  378. floppy->ascq == 0x00)
  379. return;
  380. printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
  381. "asc = %2x, ascq = %2x\n",
  382. floppy->drive->name, pc->c[0], floppy->sense_key,
  383. floppy->asc, floppy->ascq);
  384. }
  385. static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
  386. struct ide_atapi_pc *pc)
  387. {
  388. idefloppy_floppy_t *floppy = drive->driver_data;
  389. if (floppy->failed_pc == NULL &&
  390. pc->c[0] != GPCMD_REQUEST_SENSE)
  391. floppy->failed_pc = pc;
  392. /* Set the current packet command */
  393. floppy->pc = pc;
  394. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
  395. if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
  396. ide_floppy_report_error(floppy, pc);
  397. /* Giving up */
  398. pc->error = IDEFLOPPY_ERROR_GENERAL;
  399. floppy->failed_pc = NULL;
  400. drive->pc_callback(drive);
  401. return ide_stopped;
  402. }
  403. debug_log("Retry number - %d\n", pc->retries);
  404. pc->retries++;
  405. return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
  406. IDEFLOPPY_WAIT_CMD, NULL);
  407. }
  408. static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
  409. {
  410. debug_log("creating prevent removal command, prevent = %d\n", prevent);
  411. idefloppy_init_pc(pc);
  412. pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  413. pc->c[4] = prevent;
  414. }
  415. static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
  416. {
  417. idefloppy_init_pc(pc);
  418. pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
  419. pc->c[7] = 255;
  420. pc->c[8] = 255;
  421. pc->req_xfer = 255;
  422. }
  423. static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
  424. int l, int flags)
  425. {
  426. idefloppy_init_pc(pc);
  427. pc->c[0] = GPCMD_FORMAT_UNIT;
  428. pc->c[1] = 0x17;
  429. memset(pc->buf, 0, 12);
  430. pc->buf[1] = 0xA2;
  431. /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
  432. if (flags & 1) /* Verify bit on... */
  433. pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
  434. pc->buf[3] = 8;
  435. put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
  436. put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
  437. pc->buf_size = 12;
  438. pc->flags |= PC_FLAG_WRITING;
  439. }
  440. /* A mode sense command is used to "sense" floppy parameters. */
  441. static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
  442. u8 page_code, u8 type)
  443. {
  444. u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
  445. idefloppy_init_pc(pc);
  446. pc->c[0] = GPCMD_MODE_SENSE_10;
  447. pc->c[1] = 0;
  448. pc->c[2] = page_code + (type << 6);
  449. switch (page_code) {
  450. case IDEFLOPPY_CAPABILITIES_PAGE:
  451. length += 12;
  452. break;
  453. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  454. length += 32;
  455. break;
  456. default:
  457. printk(KERN_ERR "ide-floppy: unsupported page code "
  458. "in create_mode_sense_cmd\n");
  459. }
  460. put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
  461. pc->req_xfer = length;
  462. }
  463. static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
  464. {
  465. idefloppy_init_pc(pc);
  466. pc->c[0] = GPCMD_START_STOP_UNIT;
  467. pc->c[4] = start;
  468. }
  469. static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
  470. struct ide_atapi_pc *pc, struct request *rq,
  471. unsigned long sector)
  472. {
  473. int block = sector / floppy->bs_factor;
  474. int blocks = rq->nr_sectors / floppy->bs_factor;
  475. int cmd = rq_data_dir(rq);
  476. debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
  477. block, blocks);
  478. idefloppy_init_pc(pc);
  479. pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
  480. put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
  481. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
  482. pc->rq = rq;
  483. pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
  484. if (rq->cmd_flags & REQ_RW)
  485. pc->flags |= PC_FLAG_WRITING;
  486. pc->buf = NULL;
  487. pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
  488. pc->flags |= PC_FLAG_DMA_OK;
  489. }
  490. static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
  491. struct ide_atapi_pc *pc, struct request *rq)
  492. {
  493. idefloppy_init_pc(pc);
  494. memcpy(pc->c, rq->cmd, sizeof(pc->c));
  495. pc->rq = rq;
  496. pc->b_count = rq->data_len;
  497. if (rq->data_len && rq_data_dir(rq) == WRITE)
  498. pc->flags |= PC_FLAG_WRITING;
  499. pc->buf = rq->data;
  500. if (rq->bio)
  501. pc->flags |= PC_FLAG_DMA_OK;
  502. /*
  503. * possibly problematic, doesn't look like ide-floppy correctly
  504. * handled scattered requests if dma fails...
  505. */
  506. pc->req_xfer = pc->buf_size = rq->data_len;
  507. }
  508. static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
  509. struct request *rq, sector_t block_s)
  510. {
  511. idefloppy_floppy_t *floppy = drive->driver_data;
  512. struct ide_atapi_pc *pc;
  513. unsigned long block = (unsigned long)block_s;
  514. debug_log("dev: %s, cmd_type: %x, errors: %d\n",
  515. rq->rq_disk ? rq->rq_disk->disk_name : "?",
  516. rq->cmd_type, rq->errors);
  517. debug_log("sector: %ld, nr_sectors: %ld, "
  518. "current_nr_sectors: %d\n", (long)rq->sector,
  519. rq->nr_sectors, rq->current_nr_sectors);
  520. if (rq->errors >= ERROR_MAX) {
  521. if (floppy->failed_pc)
  522. ide_floppy_report_error(floppy, floppy->failed_pc);
  523. else
  524. printk(KERN_ERR "ide-floppy: %s: I/O error\n",
  525. drive->name);
  526. idefloppy_end_request(drive, 0, 0);
  527. return ide_stopped;
  528. }
  529. if (blk_fs_request(rq)) {
  530. if (((long)rq->sector % floppy->bs_factor) ||
  531. (rq->nr_sectors % floppy->bs_factor)) {
  532. printk(KERN_ERR "%s: unsupported r/w request size\n",
  533. drive->name);
  534. idefloppy_end_request(drive, 0, 0);
  535. return ide_stopped;
  536. }
  537. pc = idefloppy_next_pc_storage(drive);
  538. idefloppy_create_rw_cmd(floppy, pc, rq, block);
  539. } else if (blk_special_request(rq)) {
  540. pc = (struct ide_atapi_pc *) rq->buffer;
  541. } else if (blk_pc_request(rq)) {
  542. pc = idefloppy_next_pc_storage(drive);
  543. idefloppy_blockpc_cmd(floppy, pc, rq);
  544. } else {
  545. blk_dump_rq_flags(rq,
  546. "ide-floppy: unsupported command in queue");
  547. idefloppy_end_request(drive, 0, 0);
  548. return ide_stopped;
  549. }
  550. if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT)
  551. pc->flags |= PC_FLAG_DRQ_INTERRUPT;
  552. if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE)
  553. pc->flags |= PC_FLAG_ZIP_DRIVE;
  554. pc->rq = rq;
  555. return idefloppy_issue_pc(drive, pc);
  556. }
  557. /*
  558. * Add a special packet command request to the tail of the request queue,
  559. * and wait for it to be serviced.
  560. */
  561. static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
  562. {
  563. struct ide_floppy_obj *floppy = drive->driver_data;
  564. struct request *rq;
  565. int error;
  566. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  567. rq->buffer = (char *) pc;
  568. rq->cmd_type = REQ_TYPE_SPECIAL;
  569. error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
  570. blk_put_request(rq);
  571. return error;
  572. }
  573. /*
  574. * Look at the flexible disk page parameters. We ignore the CHS capacity
  575. * parameters and use the LBA parameters instead.
  576. */
  577. static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
  578. {
  579. idefloppy_floppy_t *floppy = drive->driver_data;
  580. struct ide_atapi_pc pc;
  581. u8 *page;
  582. int capacity, lba_capacity;
  583. u16 transfer_rate, sector_size, cyls, rpm;
  584. u8 heads, sectors;
  585. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
  586. MODE_SENSE_CURRENT);
  587. if (idefloppy_queue_pc_tail(drive, &pc)) {
  588. printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
  589. " parameters\n");
  590. return 1;
  591. }
  592. floppy->wp = !!(pc.buf[3] & 0x80);
  593. set_disk_ro(floppy->disk, floppy->wp);
  594. page = &pc.buf[8];
  595. transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
  596. sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
  597. cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
  598. rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
  599. heads = pc.buf[8 + 4];
  600. sectors = pc.buf[8 + 5];
  601. capacity = cyls * heads * sectors * sector_size;
  602. if (memcmp(page, &floppy->flexible_disk_page, 32))
  603. printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  604. "%d sector size, %d rpm\n",
  605. drive->name, capacity / 1024, cyls, heads,
  606. sectors, transfer_rate / 8, sector_size, rpm);
  607. memcpy(&floppy->flexible_disk_page, page, 32);
  608. drive->bios_cyl = cyls;
  609. drive->bios_head = heads;
  610. drive->bios_sect = sectors;
  611. lba_capacity = floppy->blocks * floppy->block_size;
  612. if (capacity < lba_capacity) {
  613. printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
  614. "bytes, but the drive only handles %d\n",
  615. drive->name, lba_capacity, capacity);
  616. floppy->blocks = floppy->block_size ?
  617. capacity / floppy->block_size : 0;
  618. }
  619. return 0;
  620. }
  621. static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
  622. {
  623. idefloppy_floppy_t *floppy = drive->driver_data;
  624. struct ide_atapi_pc pc;
  625. floppy->srfp = 0;
  626. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
  627. MODE_SENSE_CURRENT);
  628. pc.flags |= PC_FLAG_SUPPRESS_ERROR;
  629. if (idefloppy_queue_pc_tail(drive, &pc))
  630. return 1;
  631. floppy->srfp = pc.buf[8 + 2] & 0x40;
  632. return (0);
  633. }
  634. /*
  635. * Determine if a media is present in the floppy drive, and if so, its LBA
  636. * capacity.
  637. */
  638. static int ide_floppy_get_capacity(ide_drive_t *drive)
  639. {
  640. idefloppy_floppy_t *floppy = drive->driver_data;
  641. struct ide_atapi_pc pc;
  642. u8 *cap_desc;
  643. u8 header_len, desc_cnt;
  644. int i, rc = 1, blocks, length;
  645. drive->bios_cyl = 0;
  646. drive->bios_head = drive->bios_sect = 0;
  647. floppy->blocks = 0;
  648. floppy->bs_factor = 1;
  649. set_capacity(floppy->disk, 0);
  650. idefloppy_create_read_capacity_cmd(&pc);
  651. if (idefloppy_queue_pc_tail(drive, &pc)) {
  652. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  653. return 1;
  654. }
  655. header_len = pc.buf[3];
  656. cap_desc = &pc.buf[4];
  657. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  658. for (i = 0; i < desc_cnt; i++) {
  659. unsigned int desc_start = 4 + i*8;
  660. blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
  661. length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
  662. debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
  663. i, blocks * length / 1024, blocks, length);
  664. if (i)
  665. continue;
  666. /*
  667. * the code below is valid only for the 1st descriptor, ie i=0
  668. */
  669. switch (pc.buf[desc_start + 4] & 0x03) {
  670. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  671. case CAPACITY_UNFORMATTED:
  672. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
  673. /*
  674. * If it is not a clik drive, break out
  675. * (maintains previous driver behaviour)
  676. */
  677. break;
  678. case CAPACITY_CURRENT:
  679. /* Normal Zip/LS-120 disks */
  680. if (memcmp(cap_desc, &floppy->cap_desc, 8))
  681. printk(KERN_INFO "%s: %dkB, %d blocks, %d "
  682. "sector size\n", drive->name,
  683. blocks * length / 1024, blocks, length);
  684. memcpy(&floppy->cap_desc, cap_desc, 8);
  685. if (!length || length % 512) {
  686. printk(KERN_NOTICE "%s: %d bytes block size "
  687. "not supported\n", drive->name, length);
  688. } else {
  689. floppy->blocks = blocks;
  690. floppy->block_size = length;
  691. floppy->bs_factor = length / 512;
  692. if (floppy->bs_factor != 1)
  693. printk(KERN_NOTICE "%s: warning: non "
  694. "512 bytes block size not "
  695. "fully supported\n",
  696. drive->name);
  697. rc = 0;
  698. }
  699. break;
  700. case CAPACITY_NO_CARTRIDGE:
  701. /*
  702. * This is a KERN_ERR so it appears on screen
  703. * for the user to see
  704. */
  705. printk(KERN_ERR "%s: No disk in drive\n", drive->name);
  706. break;
  707. case CAPACITY_INVALID:
  708. printk(KERN_ERR "%s: Invalid capacity for disk "
  709. "in drive\n", drive->name);
  710. break;
  711. }
  712. debug_log("Descriptor 0 Code: %d\n",
  713. pc.buf[desc_start + 4] & 0x03);
  714. }
  715. /* Clik! disk does not support get_flexible_disk_page */
  716. if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
  717. (void) ide_floppy_get_flexible_disk_page(drive);
  718. set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
  719. return rc;
  720. }
  721. /*
  722. * Obtain the list of formattable capacities.
  723. * Very similar to ide_floppy_get_capacity, except that we push the capacity
  724. * descriptors to userland, instead of our own structures.
  725. *
  726. * Userland gives us the following structure:
  727. *
  728. * struct idefloppy_format_capacities {
  729. * int nformats;
  730. * struct {
  731. * int nblocks;
  732. * int blocksize;
  733. * } formats[];
  734. * };
  735. *
  736. * userland initializes nformats to the number of allocated formats[] records.
  737. * On exit we set nformats to the number of records we've actually initialized.
  738. */
  739. static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
  740. {
  741. struct ide_atapi_pc pc;
  742. u8 header_len, desc_cnt;
  743. int i, blocks, length, u_array_size, u_index;
  744. int __user *argp;
  745. if (get_user(u_array_size, arg))
  746. return (-EFAULT);
  747. if (u_array_size <= 0)
  748. return (-EINVAL);
  749. idefloppy_create_read_capacity_cmd(&pc);
  750. if (idefloppy_queue_pc_tail(drive, &pc)) {
  751. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  752. return (-EIO);
  753. }
  754. header_len = pc.buf[3];
  755. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  756. u_index = 0;
  757. argp = arg + 1;
  758. /*
  759. * We always skip the first capacity descriptor. That's the current
  760. * capacity. We are interested in the remaining descriptors, the
  761. * formattable capacities.
  762. */
  763. for (i = 1; i < desc_cnt; i++) {
  764. unsigned int desc_start = 4 + i*8;
  765. if (u_index >= u_array_size)
  766. break; /* User-supplied buffer too small */
  767. blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
  768. length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
  769. if (put_user(blocks, argp))
  770. return(-EFAULT);
  771. ++argp;
  772. if (put_user(length, argp))
  773. return (-EFAULT);
  774. ++argp;
  775. ++u_index;
  776. }
  777. if (put_user(u_index, arg))
  778. return (-EFAULT);
  779. return (0);
  780. }
  781. /*
  782. * Get ATAPI_FORMAT_UNIT progress indication.
  783. *
  784. * Userland gives a pointer to an int. The int is set to a progress
  785. * indicator 0-65536, with 65536=100%.
  786. *
  787. * If the drive does not support format progress indication, we just check
  788. * the dsc bit, and return either 0 or 65536.
  789. */
  790. static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
  791. {
  792. idefloppy_floppy_t *floppy = drive->driver_data;
  793. struct ide_atapi_pc pc;
  794. int progress_indication = 0x10000;
  795. if (floppy->srfp) {
  796. idefloppy_create_request_sense_cmd(&pc);
  797. if (idefloppy_queue_pc_tail(drive, &pc))
  798. return (-EIO);
  799. if (floppy->sense_key == 2 &&
  800. floppy->asc == 4 &&
  801. floppy->ascq == 4)
  802. progress_indication = floppy->progress_indication;
  803. /* Else assume format_unit has finished, and we're at 0x10000 */
  804. } else {
  805. ide_hwif_t *hwif = drive->hwif;
  806. unsigned long flags;
  807. u8 stat;
  808. local_irq_save(flags);
  809. stat = hwif->tp_ops->read_status(hwif);
  810. local_irq_restore(flags);
  811. progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
  812. }
  813. if (put_user(progress_indication, arg))
  814. return (-EFAULT);
  815. return (0);
  816. }
  817. static sector_t idefloppy_capacity(ide_drive_t *drive)
  818. {
  819. idefloppy_floppy_t *floppy = drive->driver_data;
  820. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  821. return capacity;
  822. }
  823. /*
  824. * Check whether we can support a drive, based on the ATAPI IDENTIFY command
  825. * results.
  826. */
  827. static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
  828. {
  829. u8 gcw[2];
  830. u8 device_type, protocol, removable, drq_type, packet_size;
  831. *((u16 *) &gcw) = id->config;
  832. device_type = gcw[1] & 0x1F;
  833. removable = (gcw[0] & 0x80) >> 7;
  834. protocol = (gcw[1] & 0xC0) >> 6;
  835. drq_type = (gcw[0] & 0x60) >> 5;
  836. packet_size = gcw[0] & 0x03;
  837. #ifdef CONFIG_PPC
  838. /* kludge for Apple PowerBook internal zip */
  839. if (device_type == 5 &&
  840. !strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP"))
  841. device_type = 0;
  842. #endif
  843. if (protocol != 2)
  844. printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
  845. protocol);
  846. else if (device_type != 0)
  847. printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
  848. "to floppy\n", device_type);
  849. else if (!removable)
  850. printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
  851. else if (drq_type == 3)
  852. printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
  853. "supported\n", drq_type);
  854. else if (packet_size != 0)
  855. printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
  856. "bytes\n", packet_size);
  857. else
  858. return 1;
  859. return 0;
  860. }
  861. #ifdef CONFIG_IDE_PROC_FS
  862. static void idefloppy_add_settings(ide_drive_t *drive)
  863. {
  864. idefloppy_floppy_t *floppy = drive->driver_data;
  865. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
  866. &drive->bios_cyl, NULL);
  867. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
  868. &drive->bios_head, NULL);
  869. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
  870. &drive->bios_sect, NULL);
  871. ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
  872. &floppy->ticks, NULL);
  873. }
  874. #else
  875. static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
  876. #endif
  877. static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
  878. {
  879. u8 gcw[2];
  880. *((u16 *) &gcw) = drive->id->config;
  881. floppy->pc = floppy->pc_stack;
  882. drive->pc_callback = ide_floppy_callback;
  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");