ide-floppy.c 38 KB

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