ide-floppy.c 38 KB

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