ide-floppy.c 38 KB

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