ide-floppy.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. /*
  9. * The driver currently doesn't have any fancy features, just the bare
  10. * minimum read/write support.
  11. *
  12. * This driver supports the following IDE floppy drives:
  13. *
  14. * LS-120/240 SuperDisk
  15. * Iomega Zip 100/250
  16. * Iomega PC Card Clik!/PocketZip
  17. *
  18. * For a historical changelog see
  19. * Documentation/ide/ChangeLog.ide-floppy.1996-2002
  20. */
  21. #define IDEFLOPPY_VERSION "0.99.newide"
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/string.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/timer.h>
  28. #include <linux/mm.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/major.h>
  31. #include <linux/errno.h>
  32. #include <linux/genhd.h>
  33. #include <linux/slab.h>
  34. #include <linux/cdrom.h>
  35. #include <linux/ide.h>
  36. #include <linux/bitops.h>
  37. #include <linux/mutex.h>
  38. #include <scsi/scsi_ioctl.h>
  39. #include <asm/byteorder.h>
  40. #include <linux/irq.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/io.h>
  43. #include <asm/unaligned.h>
  44. /* define to see debug info */
  45. #define IDEFLOPPY_DEBUG_LOG 0
  46. /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
  47. #define IDEFLOPPY_DEBUG( fmt, args... )
  48. #if IDEFLOPPY_DEBUG_LOG
  49. #define debug_log(fmt, args...) \
  50. printk(KERN_INFO "ide-floppy: " fmt, ## args)
  51. #else
  52. #define debug_log(fmt, args... ) do {} while(0)
  53. #endif
  54. /*
  55. * Some drives require a longer irq timeout.
  56. */
  57. #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
  58. /*
  59. * After each failed packet command we issue a request sense command
  60. * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
  61. */
  62. #define IDEFLOPPY_MAX_PC_RETRIES 3
  63. /*
  64. * With each packet command, we allocate a buffer of
  65. * IDEFLOPPY_PC_BUFFER_SIZE bytes.
  66. */
  67. #define IDEFLOPPY_PC_BUFFER_SIZE 256
  68. /*
  69. * In various places in the driver, we need to allocate storage
  70. * for packet commands and requests, which will remain valid while
  71. * we leave the driver to wait for an interrupt or a timeout event.
  72. */
  73. #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
  74. /*
  75. * Our view of a packet command.
  76. */
  77. typedef struct idefloppy_packet_command_s {
  78. u8 c[12]; /* Actual packet bytes */
  79. int retries; /* On each retry, we increment retries */
  80. int error; /* Error code */
  81. int request_transfer; /* Bytes to transfer */
  82. int actually_transferred; /* Bytes actually transferred */
  83. int buffer_size; /* Size of our data buffer */
  84. int b_count; /* Missing/Available data on the current buffer */
  85. struct request *rq; /* The corresponding request */
  86. u8 *buffer; /* Data buffer */
  87. u8 *current_position; /* Pointer into the above buffer */
  88. void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
  89. u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
  90. unsigned long flags; /* Status/Action bit flags: long for set_bit */
  91. } idefloppy_pc_t;
  92. /*
  93. * Packet command flag bits.
  94. */
  95. #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
  96. #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
  97. #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
  98. #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
  99. #define PC_WRITING 5 /* Data direction */
  100. #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
  101. /* format capacities descriptor codes */
  102. #define CAPACITY_INVALID 0x00
  103. #define CAPACITY_UNFORMATTED 0x01
  104. #define CAPACITY_CURRENT 0x02
  105. #define CAPACITY_NO_CARTRIDGE 0x03
  106. /*
  107. * Most of our global data which we need to save even as we leave the
  108. * driver due to an interrupt or a timer event is stored in a variable
  109. * of type idefloppy_floppy_t, defined below.
  110. */
  111. typedef struct ide_floppy_obj {
  112. ide_drive_t *drive;
  113. ide_driver_t *driver;
  114. struct gendisk *disk;
  115. struct kref kref;
  116. unsigned int openers; /* protected by BKL for now */
  117. /* Current packet command */
  118. idefloppy_pc_t *pc;
  119. /* Last failed packet command */
  120. idefloppy_pc_t *failed_pc;
  121. /* Packet command stack */
  122. idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
  123. /* Next free packet command storage space */
  124. int pc_stack_index;
  125. struct request rq_stack[IDEFLOPPY_PC_STACK];
  126. /* We implement a circular array */
  127. int rq_stack_index;
  128. /*
  129. * Last error information
  130. */
  131. u8 sense_key, asc, ascq;
  132. /* delay this long before sending packet command */
  133. u8 ticks;
  134. int progress_indication;
  135. /*
  136. * Device information
  137. */
  138. /* Current format */
  139. int blocks, block_size, bs_factor;
  140. /* Last format capacity descriptor */
  141. u8 cap_desc[8];
  142. /* Copy of the flexible disk page */
  143. u8 flexible_disk_page[32];
  144. /* Write protect */
  145. int wp;
  146. /* Supports format progress report */
  147. int srfp;
  148. /* Status/Action flags */
  149. unsigned long flags;
  150. } idefloppy_floppy_t;
  151. #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
  152. /*
  153. * Floppy flag bits values.
  154. */
  155. #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
  156. #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
  157. #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
  158. #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
  159. #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
  160. /*
  161. * Defines for the mode sense command
  162. */
  163. #define MODE_SENSE_CURRENT 0x00
  164. #define MODE_SENSE_CHANGEABLE 0x01
  165. #define MODE_SENSE_DEFAULT 0x02
  166. #define MODE_SENSE_SAVED 0x03
  167. /*
  168. * IOCTLs used in low-level formatting.
  169. */
  170. #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
  171. #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
  172. #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
  173. #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
  174. /*
  175. * Error codes which are returned in rq->errors to the higher part
  176. * of the driver.
  177. */
  178. #define IDEFLOPPY_ERROR_GENERAL 101
  179. /*
  180. * The following is used to format the general configuration word of
  181. * the ATAPI IDENTIFY DEVICE command.
  182. */
  183. struct idefloppy_id_gcw {
  184. #if defined(__LITTLE_ENDIAN_BITFIELD)
  185. unsigned packet_size :2; /* Packet Size */
  186. unsigned reserved234 :3; /* Reserved */
  187. unsigned drq_type :2; /* Command packet DRQ type */
  188. unsigned removable :1; /* Removable media */
  189. unsigned device_type :5; /* Device type */
  190. unsigned reserved13 :1; /* Reserved */
  191. unsigned protocol :2; /* Protocol type */
  192. #elif defined(__BIG_ENDIAN_BITFIELD)
  193. unsigned protocol :2; /* Protocol type */
  194. unsigned reserved13 :1; /* Reserved */
  195. unsigned device_type :5; /* Device type */
  196. unsigned removable :1; /* Removable media */
  197. unsigned drq_type :2; /* Command packet DRQ type */
  198. unsigned reserved234 :3; /* Reserved */
  199. unsigned packet_size :2; /* Packet Size */
  200. #else
  201. #error "Bitfield endianness not defined! Check your byteorder.h"
  202. #endif
  203. };
  204. /*
  205. * Pages of the SELECT SENSE / MODE SENSE packet commands.
  206. * See SFF-8070i spec.
  207. */
  208. #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
  209. #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
  210. static DEFINE_MUTEX(idefloppy_ref_mutex);
  211. #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
  212. #define ide_floppy_g(disk) \
  213. container_of((disk)->private_data, struct ide_floppy_obj, driver)
  214. static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
  215. {
  216. struct ide_floppy_obj *floppy = NULL;
  217. mutex_lock(&idefloppy_ref_mutex);
  218. floppy = ide_floppy_g(disk);
  219. if (floppy)
  220. kref_get(&floppy->kref);
  221. mutex_unlock(&idefloppy_ref_mutex);
  222. return floppy;
  223. }
  224. static void idefloppy_cleanup_obj(struct kref *);
  225. static void ide_floppy_put(struct ide_floppy_obj *floppy)
  226. {
  227. mutex_lock(&idefloppy_ref_mutex);
  228. kref_put(&floppy->kref, idefloppy_cleanup_obj);
  229. mutex_unlock(&idefloppy_ref_mutex);
  230. }
  231. /*
  232. * Too bad. The drive wants to send us data which we are not ready to accept.
  233. * Just throw it away.
  234. */
  235. static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
  236. {
  237. while (bcount--)
  238. (void) HWIF(drive)->INB(IDE_DATA_REG);
  239. }
  240. static void idefloppy_write_zeros(ide_drive_t *drive, unsigned int bcount)
  241. {
  242. while (bcount--)
  243. HWIF(drive)->OUTB(0, IDE_DATA_REG);
  244. }
  245. /*
  246. * idefloppy_do_end_request is used to finish servicing a request.
  247. *
  248. * For read/write requests, we will call ide_end_request to pass to the
  249. * next buffer.
  250. */
  251. static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
  252. {
  253. idefloppy_floppy_t *floppy = drive->driver_data;
  254. struct request *rq = HWGROUP(drive)->rq;
  255. int error;
  256. debug_log("Reached %s\n", __func__);
  257. switch (uptodate) {
  258. case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
  259. case 1: error = 0; break;
  260. default: error = uptodate;
  261. }
  262. if (error)
  263. floppy->failed_pc = NULL;
  264. /* Why does this happen? */
  265. if (!rq)
  266. return 0;
  267. if (!blk_special_request(rq)) {
  268. /* our real local end request function */
  269. ide_end_request(drive, uptodate, nsecs);
  270. return 0;
  271. }
  272. rq->errors = error;
  273. /* fixme: need to move this local also */
  274. ide_end_drive_cmd(drive, 0, 0);
  275. return 0;
  276. }
  277. static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc,
  278. unsigned int bcount, int direction)
  279. {
  280. struct request *rq = pc->rq;
  281. struct req_iterator iter;
  282. struct bio_vec *bvec;
  283. unsigned long flags;
  284. int count, done = 0;
  285. char *data;
  286. rq_for_each_segment(bvec, rq, iter) {
  287. if (!bcount)
  288. break;
  289. count = min(bvec->bv_len, bcount);
  290. data = bvec_kmap_irq(bvec, &flags);
  291. if (direction)
  292. drive->hwif->atapi_output_bytes(drive, data, count);
  293. else
  294. drive->hwif->atapi_input_bytes(drive, data, count);
  295. bvec_kunmap_irq(data, &flags);
  296. bcount -= count;
  297. pc->b_count += count;
  298. done += count;
  299. }
  300. idefloppy_do_end_request(drive, 1, done >> 9);
  301. if (bcount) {
  302. printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
  303. drive->name, __func__, bcount);
  304. if (direction)
  305. idefloppy_write_zeros(drive, bcount);
  306. else
  307. idefloppy_discard_data(drive, bcount);
  308. }
  309. }
  310. static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
  311. {
  312. struct request *rq = pc->rq;
  313. struct bio *bio = rq->bio;
  314. while ((bio = rq->bio) != NULL)
  315. idefloppy_do_end_request(drive, 1, 0);
  316. }
  317. /*
  318. * idefloppy_queue_pc_head generates a new packet command request in front
  319. * of the request queue, before the current request, so that it will be
  320. * processed immediately, on the next pass through the driver.
  321. */
  322. static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
  323. {
  324. struct ide_floppy_obj *floppy = drive->driver_data;
  325. ide_init_drive_cmd(rq);
  326. rq->buffer = (char *) pc;
  327. rq->cmd_type = REQ_TYPE_SPECIAL;
  328. rq->rq_disk = floppy->disk;
  329. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  330. }
  331. static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
  332. {
  333. idefloppy_floppy_t *floppy = drive->driver_data;
  334. if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
  335. floppy->pc_stack_index=0;
  336. return (&floppy->pc_stack[floppy->pc_stack_index++]);
  337. }
  338. static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
  339. {
  340. idefloppy_floppy_t *floppy = drive->driver_data;
  341. if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
  342. floppy->rq_stack_index = 0;
  343. return (&floppy->rq_stack[floppy->rq_stack_index++]);
  344. }
  345. static void idefloppy_request_sense_callback(ide_drive_t *drive)
  346. {
  347. idefloppy_floppy_t *floppy = drive->driver_data;
  348. u8 *buf = floppy->pc->buffer;
  349. debug_log("Reached %s\n", __func__);
  350. if (!floppy->pc->error) {
  351. floppy->sense_key = buf[2] & 0x0F;
  352. floppy->asc = buf[12];
  353. floppy->ascq = buf[13];
  354. floppy->progress_indication = buf[15] & 0x80 ?
  355. (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
  356. if (floppy->failed_pc)
  357. debug_log("pc = %x, sense key = %x, asc = %x,"
  358. " ascq = %x\n",
  359. floppy->failed_pc->c[0],
  360. floppy->sense_key,
  361. floppy->asc,
  362. floppy->ascq);
  363. else
  364. debug_log("sense key = %x, asc = %x, ascq = %x\n",
  365. floppy->sense_key,
  366. floppy->asc,
  367. floppy->ascq);
  368. idefloppy_do_end_request(drive, 1, 0);
  369. } else {
  370. printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
  371. " request!\n");
  372. idefloppy_do_end_request(drive, 0, 0);
  373. }
  374. }
  375. /*
  376. * General packet command callback function.
  377. */
  378. static void idefloppy_pc_callback (ide_drive_t *drive)
  379. {
  380. idefloppy_floppy_t *floppy = drive->driver_data;
  381. debug_log("Reached %s\n", __func__);
  382. idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
  383. }
  384. /*
  385. * idefloppy_init_pc initializes a packet command.
  386. */
  387. static void idefloppy_init_pc (idefloppy_pc_t *pc)
  388. {
  389. memset(pc->c, 0, 12);
  390. pc->retries = 0;
  391. pc->flags = 0;
  392. pc->request_transfer = 0;
  393. pc->buffer = pc->pc_buffer;
  394. pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
  395. pc->callback = &idefloppy_pc_callback;
  396. }
  397. static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
  398. {
  399. idefloppy_init_pc(pc);
  400. pc->c[0] = GPCMD_REQUEST_SENSE;
  401. pc->c[4] = 255;
  402. pc->request_transfer = 18;
  403. pc->callback = &idefloppy_request_sense_callback;
  404. }
  405. /*
  406. * idefloppy_retry_pc is called when an error was detected during the
  407. * last packet command. We queue a request sense packet command in
  408. * the head of the request list.
  409. */
  410. static void idefloppy_retry_pc (ide_drive_t *drive)
  411. {
  412. idefloppy_pc_t *pc;
  413. struct request *rq;
  414. (void)drive->hwif->INB(IDE_ERROR_REG);
  415. pc = idefloppy_next_pc_storage(drive);
  416. rq = idefloppy_next_rq_storage(drive);
  417. idefloppy_create_request_sense_cmd(pc);
  418. idefloppy_queue_pc_head(drive, pc, rq);
  419. }
  420. /* The usual interrupt handler called during a packet command. */
  421. static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
  422. {
  423. idefloppy_floppy_t *floppy = drive->driver_data;
  424. ide_hwif_t *hwif = drive->hwif;
  425. idefloppy_pc_t *pc = floppy->pc;
  426. struct request *rq = pc->rq;
  427. xfer_func_t *xferfunc;
  428. unsigned int temp;
  429. int dma_error = 0;
  430. u16 bcount;
  431. u8 stat, ireason;
  432. debug_log("Reached %s interrupt handler\n", __func__);
  433. if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  434. dma_error = hwif->ide_dma_end(drive);
  435. if (dma_error) {
  436. printk(KERN_ERR "%s: DMA %s error\n", drive->name,
  437. rq_data_dir(rq) ? "write" : "read");
  438. set_bit(PC_DMA_ERROR, &pc->flags);
  439. } else {
  440. pc->actually_transferred = pc->request_transfer;
  441. idefloppy_update_buffers(drive, pc);
  442. }
  443. debug_log("DMA finished\n");
  444. }
  445. /* Clear the interrupt */
  446. stat = drive->hwif->INB(IDE_STATUS_REG);
  447. if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
  448. debug_log("Packet command completed, %d bytes transferred\n",
  449. pc->actually_transferred);
  450. clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  451. local_irq_enable_in_hardirq();
  452. if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
  453. /* Error detected */
  454. debug_log("%s: I/O error\n", drive->name);
  455. rq->errors++;
  456. if (pc->c[0] == GPCMD_REQUEST_SENSE) {
  457. printk(KERN_ERR "ide-floppy: I/O error in "
  458. "request sense command\n");
  459. return ide_do_reset(drive);
  460. }
  461. /* Retry operation */
  462. idefloppy_retry_pc(drive);
  463. /* queued, but not started */
  464. return ide_stopped;
  465. }
  466. pc->error = 0;
  467. if (floppy->failed_pc == pc)
  468. floppy->failed_pc = NULL;
  469. /* Command finished - Call the callback function */
  470. pc->callback(drive);
  471. return ide_stopped;
  472. }
  473. if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
  474. printk(KERN_ERR "ide-floppy: The floppy wants to issue "
  475. "more interrupts in DMA mode\n");
  476. ide_dma_off(drive);
  477. return ide_do_reset(drive);
  478. }
  479. /* Get the number of bytes to transfer */
  480. bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
  481. hwif->INB(IDE_BCOUNTL_REG);
  482. /* on this interrupt */
  483. ireason = hwif->INB(IDE_IREASON_REG);
  484. if (ireason & CD) {
  485. printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
  486. return ide_do_reset(drive);
  487. }
  488. if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
  489. /* Hopefully, we will never get here */
  490. printk(KERN_ERR "ide-floppy: We wanted to %s, ",
  491. (ireason & IO) ? "Write" : "Read");
  492. printk(KERN_ERR "but the floppy wants us to %s !\n",
  493. (ireason & IO) ? "Read" : "Write");
  494. return ide_do_reset(drive);
  495. }
  496. if (!test_bit(PC_WRITING, &pc->flags)) {
  497. /* Reading - Check that we have enough space */
  498. temp = pc->actually_transferred + bcount;
  499. if (temp > pc->request_transfer) {
  500. if (temp > pc->buffer_size) {
  501. printk(KERN_ERR "ide-floppy: The floppy wants "
  502. "to send us more data than expected "
  503. "- discarding data\n");
  504. idefloppy_discard_data(drive, bcount);
  505. ide_set_handler(drive,
  506. &idefloppy_pc_intr,
  507. IDEFLOPPY_WAIT_CMD,
  508. NULL);
  509. return ide_started;
  510. }
  511. debug_log("The floppy wants to send us more data than"
  512. " expected - allowing transfer\n");
  513. }
  514. }
  515. if (test_bit(PC_WRITING, &pc->flags))
  516. xferfunc = hwif->atapi_output_bytes;
  517. else
  518. xferfunc = hwif->atapi_input_bytes;
  519. if (pc->buffer)
  520. xferfunc(drive, pc->current_position, bcount);
  521. else
  522. ide_floppy_io_buffers(drive, pc, bcount,
  523. test_bit(PC_WRITING, &pc->flags));
  524. /* Update the current position */
  525. pc->actually_transferred += bcount;
  526. pc->current_position += bcount;
  527. /* And set the interrupt handler again */
  528. ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
  529. return ide_started;
  530. }
  531. /*
  532. * This is the original routine that did the packet transfer.
  533. * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
  534. * for that drive below. The algorithm is chosen based on drive type
  535. */
  536. static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
  537. {
  538. ide_startstop_t startstop;
  539. idefloppy_floppy_t *floppy = drive->driver_data;
  540. u8 ireason;
  541. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  542. printk(KERN_ERR "ide-floppy: Strange, packet command "
  543. "initiated yet DRQ isn't asserted\n");
  544. return startstop;
  545. }
  546. ireason = drive->hwif->INB(IDE_IREASON_REG);
  547. if ((ireason & CD) == 0 || (ireason & IO)) {
  548. printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
  549. "issuing a packet command\n");
  550. return ide_do_reset(drive);
  551. }
  552. /* Set the interrupt routine */
  553. ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
  554. /* Send the actual packet */
  555. HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
  556. return ide_started;
  557. }
  558. /*
  559. * What we have here is a classic case of a top half / bottom half
  560. * interrupt service routine. In interrupt mode, the device sends
  561. * an interrupt to signal it's ready to receive a packet. However,
  562. * we need to delay about 2-3 ticks before issuing the packet or we
  563. * gets in trouble.
  564. *
  565. * So, follow carefully. transfer_pc1 is called as an interrupt (or
  566. * directly). In either case, when the device says it's ready for a
  567. * packet, we schedule the packet transfer to occur about 2-3 ticks
  568. * later in transfer_pc2.
  569. */
  570. static int idefloppy_transfer_pc2 (ide_drive_t *drive)
  571. {
  572. idefloppy_floppy_t *floppy = drive->driver_data;
  573. /* Send the actual packet */
  574. HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
  575. /* Timeout for the packet command */
  576. return IDEFLOPPY_WAIT_CMD;
  577. }
  578. static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
  579. {
  580. idefloppy_floppy_t *floppy = drive->driver_data;
  581. ide_startstop_t startstop;
  582. u8 ireason;
  583. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  584. printk(KERN_ERR "ide-floppy: Strange, packet command "
  585. "initiated yet DRQ isn't asserted\n");
  586. return startstop;
  587. }
  588. ireason = drive->hwif->INB(IDE_IREASON_REG);
  589. if ((ireason & CD) == 0 || (ireason & IO)) {
  590. printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
  591. "while issuing a packet command\n");
  592. return ide_do_reset(drive);
  593. }
  594. /*
  595. * The following delay solves a problem with ATAPI Zip 100 drives
  596. * where the Busy flag was apparently being deasserted before the
  597. * unit was ready to receive data. This was happening on a
  598. * 1200 MHz Athlon system. 10/26/01 25msec is too short,
  599. * 40 and 50msec work well. idefloppy_pc_intr will not be actually
  600. * used until after the packet is moved in about 50 msec.
  601. */
  602. ide_set_handler(drive,
  603. &idefloppy_pc_intr, /* service routine for packet command */
  604. floppy->ticks, /* wait this long before "failing" */
  605. &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
  606. return ide_started;
  607. }
  608. static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
  609. idefloppy_pc_t *pc)
  610. {
  611. /* supress error messages resulting from Medium not present */
  612. if (floppy->sense_key == 0x02 &&
  613. floppy->asc == 0x3a &&
  614. floppy->ascq == 0x00)
  615. return;
  616. printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
  617. "asc = %2x, ascq = %2x\n",
  618. floppy->drive->name, pc->c[0], floppy->sense_key,
  619. floppy->asc, floppy->ascq);
  620. }
  621. /*
  622. * Issue a packet command
  623. */
  624. static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
  625. {
  626. idefloppy_floppy_t *floppy = drive->driver_data;
  627. ide_hwif_t *hwif = drive->hwif;
  628. ide_handler_t *pkt_xfer_routine;
  629. u16 bcount;
  630. u8 dma;
  631. if (floppy->failed_pc == NULL &&
  632. pc->c[0] != GPCMD_REQUEST_SENSE)
  633. floppy->failed_pc = pc;
  634. /* Set the current packet command */
  635. floppy->pc = pc;
  636. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
  637. test_bit(PC_ABORT, &pc->flags)) {
  638. /*
  639. * We will "abort" retrying a packet command in case
  640. * a legitimate error code was received.
  641. */
  642. if (!test_bit(PC_ABORT, &pc->flags)) {
  643. if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags))
  644. ide_floppy_report_error(floppy, pc);
  645. /* Giving up */
  646. pc->error = IDEFLOPPY_ERROR_GENERAL;
  647. }
  648. floppy->failed_pc = NULL;
  649. pc->callback(drive);
  650. return ide_stopped;
  651. }
  652. debug_log("Retry number - %d\n", pc->retries);
  653. pc->retries++;
  654. /* We haven't transferred any data yet */
  655. pc->actually_transferred = 0;
  656. pc->current_position = pc->buffer;
  657. bcount = min(pc->request_transfer, 63 * 1024);
  658. if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
  659. ide_dma_off(drive);
  660. dma = 0;
  661. if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
  662. dma = !hwif->dma_setup(drive);
  663. ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
  664. IDE_TFLAG_OUT_DEVICE, bcount, dma);
  665. if (dma) { /* Begin DMA, if necessary */
  666. set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
  667. hwif->dma_start(drive);
  668. }
  669. /* Can we transfer the packet when we get the interrupt or wait? */
  670. if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
  671. /* wait */
  672. pkt_xfer_routine = &idefloppy_transfer_pc1;
  673. } else {
  674. /* immediate */
  675. pkt_xfer_routine = &idefloppy_transfer_pc;
  676. }
  677. if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
  678. /* Issue the packet command */
  679. ide_execute_command(drive, WIN_PACKETCMD,
  680. pkt_xfer_routine,
  681. IDEFLOPPY_WAIT_CMD,
  682. NULL);
  683. return ide_started;
  684. } else {
  685. /* Issue the packet command */
  686. HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  687. return (*pkt_xfer_routine) (drive);
  688. }
  689. }
  690. static void idefloppy_rw_callback (ide_drive_t *drive)
  691. {
  692. debug_log("Reached %s\n", __func__);
  693. idefloppy_do_end_request(drive, 1, 0);
  694. return;
  695. }
  696. static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
  697. {
  698. debug_log("creating prevent removal command, prevent = %d\n", prevent);
  699. idefloppy_init_pc(pc);
  700. pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  701. pc->c[4] = prevent;
  702. }
  703. static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
  704. {
  705. idefloppy_init_pc(pc);
  706. pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
  707. pc->c[7] = 255;
  708. pc->c[8] = 255;
  709. pc->request_transfer = 255;
  710. }
  711. static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
  712. int flags)
  713. {
  714. idefloppy_init_pc(pc);
  715. pc->c[0] = GPCMD_FORMAT_UNIT;
  716. pc->c[1] = 0x17;
  717. memset(pc->buffer, 0, 12);
  718. pc->buffer[1] = 0xA2;
  719. /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
  720. if (flags & 1) /* Verify bit on... */
  721. pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
  722. pc->buffer[3] = 8;
  723. put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
  724. put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
  725. pc->buffer_size=12;
  726. set_bit(PC_WRITING, &pc->flags);
  727. }
  728. /*
  729. * A mode sense command is used to "sense" floppy parameters.
  730. */
  731. static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
  732. {
  733. u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
  734. idefloppy_init_pc(pc);
  735. pc->c[0] = GPCMD_MODE_SENSE_10;
  736. pc->c[1] = 0;
  737. pc->c[2] = page_code + (type << 6);
  738. switch (page_code) {
  739. case IDEFLOPPY_CAPABILITIES_PAGE:
  740. length += 12;
  741. break;
  742. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  743. length += 32;
  744. break;
  745. default:
  746. printk(KERN_ERR "ide-floppy: unsupported page code "
  747. "in create_mode_sense_cmd\n");
  748. }
  749. put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
  750. pc->request_transfer = length;
  751. }
  752. static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
  753. {
  754. idefloppy_init_pc(pc);
  755. pc->c[0] = GPCMD_START_STOP_UNIT;
  756. pc->c[4] = start;
  757. }
  758. static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
  759. {
  760. idefloppy_init_pc(pc);
  761. pc->c[0] = GPCMD_TEST_UNIT_READY;
  762. }
  763. static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
  764. idefloppy_pc_t *pc, struct request *rq,
  765. unsigned long sector)
  766. {
  767. int block = sector / floppy->bs_factor;
  768. int blocks = rq->nr_sectors / floppy->bs_factor;
  769. int cmd = rq_data_dir(rq);
  770. debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
  771. block, blocks);
  772. idefloppy_init_pc(pc);
  773. pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
  774. put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
  775. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
  776. pc->callback = &idefloppy_rw_callback;
  777. pc->rq = rq;
  778. pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
  779. if (rq->cmd_flags & REQ_RW)
  780. set_bit(PC_WRITING, &pc->flags);
  781. pc->buffer = NULL;
  782. pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
  783. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  784. }
  785. static void
  786. idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
  787. {
  788. idefloppy_init_pc(pc);
  789. pc->callback = &idefloppy_rw_callback;
  790. memcpy(pc->c, rq->cmd, sizeof(pc->c));
  791. pc->rq = rq;
  792. pc->b_count = rq->data_len;
  793. if (rq->data_len && rq_data_dir(rq) == WRITE)
  794. set_bit(PC_WRITING, &pc->flags);
  795. pc->buffer = rq->data;
  796. if (rq->bio)
  797. set_bit(PC_DMA_RECOMMENDED, &pc->flags);
  798. /*
  799. * possibly problematic, doesn't look like ide-floppy correctly
  800. * handled scattered requests if dma fails...
  801. */
  802. pc->request_transfer = pc->buffer_size = rq->data_len;
  803. }
  804. /*
  805. * idefloppy_do_request is our request handling function.
  806. */
  807. static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
  808. {
  809. idefloppy_floppy_t *floppy = drive->driver_data;
  810. idefloppy_pc_t *pc;
  811. unsigned long block = (unsigned long)block_s;
  812. debug_log("dev: %s, cmd_type: %x, errors: %d\n",
  813. rq->rq_disk ? rq->rq_disk->disk_name : "?",
  814. rq->cmd_type, rq->errors);
  815. debug_log("sector: %ld, nr_sectors: %ld, "
  816. "current_nr_sectors: %d\n", (long)rq->sector,
  817. rq->nr_sectors, rq->current_nr_sectors);
  818. if (rq->errors >= ERROR_MAX) {
  819. if (floppy->failed_pc)
  820. ide_floppy_report_error(floppy, floppy->failed_pc);
  821. else
  822. printk(KERN_ERR "ide-floppy: %s: I/O error\n",
  823. drive->name);
  824. idefloppy_do_end_request(drive, 0, 0);
  825. return ide_stopped;
  826. }
  827. if (blk_fs_request(rq)) {
  828. if (((long)rq->sector % floppy->bs_factor) ||
  829. (rq->nr_sectors % floppy->bs_factor)) {
  830. printk("%s: unsupported r/w request size\n",
  831. drive->name);
  832. idefloppy_do_end_request(drive, 0, 0);
  833. return ide_stopped;
  834. }
  835. pc = idefloppy_next_pc_storage(drive);
  836. idefloppy_create_rw_cmd(floppy, pc, rq, block);
  837. } else if (blk_special_request(rq)) {
  838. pc = (idefloppy_pc_t *) rq->buffer;
  839. } else if (blk_pc_request(rq)) {
  840. pc = idefloppy_next_pc_storage(drive);
  841. idefloppy_blockpc_cmd(floppy, pc, rq);
  842. } else {
  843. blk_dump_rq_flags(rq,
  844. "ide-floppy: unsupported command in queue");
  845. idefloppy_do_end_request(drive, 0, 0);
  846. return ide_stopped;
  847. }
  848. pc->rq = rq;
  849. return idefloppy_issue_pc(drive, pc);
  850. }
  851. /*
  852. * idefloppy_queue_pc_tail adds a special packet command request to the
  853. * tail of the request queue, and waits for it to be serviced.
  854. */
  855. static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
  856. {
  857. struct ide_floppy_obj *floppy = drive->driver_data;
  858. struct request rq;
  859. ide_init_drive_cmd (&rq);
  860. rq.buffer = (char *) pc;
  861. rq.cmd_type = REQ_TYPE_SPECIAL;
  862. rq.rq_disk = floppy->disk;
  863. return ide_do_drive_cmd(drive, &rq, ide_wait);
  864. }
  865. /*
  866. * Look at the flexible disk page parameters. We ignore the CHS capacity
  867. * parameters and use the LBA parameters instead.
  868. */
  869. static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
  870. {
  871. idefloppy_floppy_t *floppy = drive->driver_data;
  872. idefloppy_pc_t pc;
  873. u8 *page;
  874. int capacity, lba_capacity;
  875. u16 transfer_rate, sector_size, cyls, rpm;
  876. u8 heads, sectors;
  877. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
  878. MODE_SENSE_CURRENT);
  879. if (idefloppy_queue_pc_tail(drive, &pc)) {
  880. printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
  881. " parameters\n");
  882. return 1;
  883. }
  884. floppy->wp = !!(pc.buffer[3] & 0x80);
  885. set_disk_ro(floppy->disk, floppy->wp);
  886. page = &pc.buffer[8];
  887. transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
  888. sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
  889. cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
  890. rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
  891. heads = pc.buffer[8 + 4];
  892. sectors = pc.buffer[8 + 5];
  893. capacity = cyls * heads * sectors * sector_size;
  894. if (memcmp(page, &floppy->flexible_disk_page, 32))
  895. printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  896. "%d sector size, %d rpm\n",
  897. drive->name, capacity / 1024, cyls, heads,
  898. sectors, transfer_rate / 8, sector_size, rpm);
  899. memcpy(&floppy->flexible_disk_page, page, 32);
  900. drive->bios_cyl = cyls;
  901. drive->bios_head = heads;
  902. drive->bios_sect = sectors;
  903. lba_capacity = floppy->blocks * floppy->block_size;
  904. if (capacity < lba_capacity) {
  905. printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
  906. "bytes, but the drive only handles %d\n",
  907. drive->name, lba_capacity, capacity);
  908. floppy->blocks = floppy->block_size ?
  909. capacity / floppy->block_size : 0;
  910. }
  911. return 0;
  912. }
  913. static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
  914. {
  915. idefloppy_floppy_t *floppy = drive->driver_data;
  916. idefloppy_pc_t pc;
  917. floppy->srfp = 0;
  918. idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
  919. MODE_SENSE_CURRENT);
  920. set_bit(PC_SUPPRESS_ERROR, &pc.flags);
  921. if (idefloppy_queue_pc_tail(drive, &pc))
  922. return 1;
  923. floppy->srfp = pc.buffer[8 + 2] & 0x40;
  924. return (0);
  925. }
  926. /*
  927. * Determine if a media is present in the floppy drive, and if so, its LBA
  928. * capacity.
  929. */
  930. static int ide_floppy_get_capacity(ide_drive_t *drive)
  931. {
  932. idefloppy_floppy_t *floppy = drive->driver_data;
  933. idefloppy_pc_t pc;
  934. u8 *cap_desc;
  935. u8 header_len, desc_cnt;
  936. int i, rc = 1, blocks, length;
  937. drive->bios_cyl = 0;
  938. drive->bios_head = drive->bios_sect = 0;
  939. floppy->blocks = 0;
  940. floppy->bs_factor = 1;
  941. set_capacity(floppy->disk, 0);
  942. idefloppy_create_read_capacity_cmd(&pc);
  943. if (idefloppy_queue_pc_tail(drive, &pc)) {
  944. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  945. return 1;
  946. }
  947. header_len = pc.buffer[3];
  948. cap_desc = &pc.buffer[4];
  949. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  950. for (i = 0; i < desc_cnt; i++) {
  951. unsigned int desc_start = 4 + i*8;
  952. blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
  953. length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
  954. debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
  955. i, blocks * length / 1024, blocks, length);
  956. if (i)
  957. continue;
  958. /*
  959. * the code below is valid only for the 1st descriptor, ie i=0
  960. */
  961. switch (pc.buffer[desc_start + 4] & 0x03) {
  962. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  963. case CAPACITY_UNFORMATTED:
  964. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
  965. /*
  966. * If it is not a clik drive, break out
  967. * (maintains previous driver behaviour)
  968. */
  969. break;
  970. case CAPACITY_CURRENT:
  971. /* Normal Zip/LS-120 disks */
  972. if (memcmp(cap_desc, &floppy->cap_desc, 8))
  973. printk(KERN_INFO "%s: %dkB, %d blocks, %d "
  974. "sector size\n", drive->name,
  975. blocks * length / 1024, blocks, length);
  976. memcpy(&floppy->cap_desc, cap_desc, 8);
  977. if (!length || length % 512) {
  978. printk(KERN_NOTICE "%s: %d bytes block size "
  979. "not supported\n", drive->name, length);
  980. } else {
  981. floppy->blocks = blocks;
  982. floppy->block_size = length;
  983. floppy->bs_factor = length / 512;
  984. if (floppy->bs_factor != 1)
  985. printk(KERN_NOTICE "%s: warning: non "
  986. "512 bytes block size not "
  987. "fully supported\n",
  988. drive->name);
  989. rc = 0;
  990. }
  991. break;
  992. case CAPACITY_NO_CARTRIDGE:
  993. /*
  994. * This is a KERN_ERR so it appears on screen
  995. * for the user to see
  996. */
  997. printk(KERN_ERR "%s: No disk in drive\n", drive->name);
  998. break;
  999. case CAPACITY_INVALID:
  1000. printk(KERN_ERR "%s: Invalid capacity for disk "
  1001. "in drive\n", drive->name);
  1002. break;
  1003. }
  1004. debug_log("Descriptor 0 Code: %d\n",
  1005. pc.buffer[desc_start + 4] & 0x03);
  1006. }
  1007. /* Clik! disk does not support get_flexible_disk_page */
  1008. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
  1009. (void) ide_floppy_get_flexible_disk_page(drive);
  1010. set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
  1011. return rc;
  1012. }
  1013. /*
  1014. * Obtain the list of formattable capacities.
  1015. * Very similar to ide_floppy_get_capacity, except that we push the capacity
  1016. * descriptors to userland, instead of our own structures.
  1017. *
  1018. * Userland gives us the following structure:
  1019. *
  1020. * struct idefloppy_format_capacities {
  1021. * int nformats;
  1022. * struct {
  1023. * int nblocks;
  1024. * int blocksize;
  1025. * } formats[];
  1026. * };
  1027. *
  1028. * userland initializes nformats to the number of allocated formats[] records.
  1029. * On exit we set nformats to the number of records we've actually initialized.
  1030. */
  1031. static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
  1032. {
  1033. idefloppy_pc_t pc;
  1034. u8 header_len, desc_cnt;
  1035. int i, blocks, length, u_array_size, u_index;
  1036. int __user *argp;
  1037. if (get_user(u_array_size, arg))
  1038. return (-EFAULT);
  1039. if (u_array_size <= 0)
  1040. return (-EINVAL);
  1041. idefloppy_create_read_capacity_cmd(&pc);
  1042. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1043. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  1044. return (-EIO);
  1045. }
  1046. header_len = pc.buffer[3];
  1047. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  1048. u_index = 0;
  1049. argp = arg + 1;
  1050. /*
  1051. * We always skip the first capacity descriptor. That's the current
  1052. * capacity. We are interested in the remaining descriptors, the
  1053. * formattable capacities.
  1054. */
  1055. for (i = 1; i < desc_cnt; i++) {
  1056. unsigned int desc_start = 4 + i*8;
  1057. if (u_index >= u_array_size)
  1058. break; /* User-supplied buffer too small */
  1059. blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
  1060. length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
  1061. if (put_user(blocks, argp))
  1062. return(-EFAULT);
  1063. ++argp;
  1064. if (put_user(length, argp))
  1065. return (-EFAULT);
  1066. ++argp;
  1067. ++u_index;
  1068. }
  1069. if (put_user(u_index, arg))
  1070. return (-EFAULT);
  1071. return (0);
  1072. }
  1073. /*
  1074. ** Get ATAPI_FORMAT_UNIT progress indication.
  1075. **
  1076. ** Userland gives a pointer to an int. The int is set to a progress
  1077. ** indicator 0-65536, with 65536=100%.
  1078. **
  1079. ** If the drive does not support format progress indication, we just check
  1080. ** the dsc bit, and return either 0 or 65536.
  1081. */
  1082. static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
  1083. {
  1084. idefloppy_floppy_t *floppy = drive->driver_data;
  1085. idefloppy_pc_t pc;
  1086. int progress_indication = 0x10000;
  1087. if (floppy->srfp) {
  1088. idefloppy_create_request_sense_cmd(&pc);
  1089. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1090. return (-EIO);
  1091. }
  1092. if (floppy->sense_key == 2 &&
  1093. floppy->asc == 4 &&
  1094. floppy->ascq == 4) {
  1095. progress_indication = floppy->progress_indication;
  1096. }
  1097. /* Else assume format_unit has finished, and we're
  1098. ** at 0x10000 */
  1099. } else {
  1100. unsigned long flags;
  1101. u8 stat;
  1102. local_irq_save(flags);
  1103. stat = drive->hwif->INB(IDE_STATUS_REG);
  1104. local_irq_restore(flags);
  1105. progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
  1106. }
  1107. if (put_user(progress_indication, arg))
  1108. return (-EFAULT);
  1109. return (0);
  1110. }
  1111. /*
  1112. * Return the current floppy capacity.
  1113. */
  1114. static sector_t idefloppy_capacity (ide_drive_t *drive)
  1115. {
  1116. idefloppy_floppy_t *floppy = drive->driver_data;
  1117. unsigned long capacity = floppy->blocks * floppy->bs_factor;
  1118. return capacity;
  1119. }
  1120. /*
  1121. * Check whether we can support a drive, based on the ATAPI IDENTIFY command
  1122. * results.
  1123. */
  1124. static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
  1125. {
  1126. struct idefloppy_id_gcw gcw;
  1127. *((u16 *) &gcw) = id->config;
  1128. #ifdef CONFIG_PPC
  1129. /* kludge for Apple PowerBook internal zip */
  1130. if ((gcw.device_type == 5) &&
  1131. !strstr(id->model, "CD-ROM") &&
  1132. strstr(id->model, "ZIP"))
  1133. gcw.device_type = 0;
  1134. #endif
  1135. if (gcw.protocol != 2)
  1136. printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
  1137. gcw.protocol);
  1138. else if (gcw.device_type != 0)
  1139. printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
  1140. "to floppy\n", gcw.device_type);
  1141. else if (!gcw.removable)
  1142. printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
  1143. else if (gcw.drq_type == 3) {
  1144. printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
  1145. "supported\n", gcw.drq_type);
  1146. } else if (gcw.packet_size != 0) {
  1147. printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
  1148. "bytes long\n", gcw.packet_size);
  1149. } else
  1150. return 1;
  1151. return 0;
  1152. }
  1153. #ifdef CONFIG_IDE_PROC_FS
  1154. static void idefloppy_add_settings(ide_drive_t *drive)
  1155. {
  1156. idefloppy_floppy_t *floppy = drive->driver_data;
  1157. /*
  1158. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  1159. */
  1160. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  1161. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  1162. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  1163. ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
  1164. }
  1165. #else
  1166. static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
  1167. #endif
  1168. /*
  1169. * Driver initialization.
  1170. */
  1171. static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
  1172. {
  1173. struct idefloppy_id_gcw gcw;
  1174. *((u16 *) &gcw) = drive->id->config;
  1175. floppy->pc = floppy->pc_stack;
  1176. if (gcw.drq_type == 1)
  1177. set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
  1178. /*
  1179. * We used to check revisions here. At this point however
  1180. * I'm giving up. Just assume they are all broken, its easier.
  1181. *
  1182. * The actual reason for the workarounds was likely
  1183. * a driver bug after all rather than a firmware bug,
  1184. * and the workaround below used to hide it. It should
  1185. * be fixed as of version 1.9, but to be on the safe side
  1186. * we'll leave the limitation below for the 2.2.x tree.
  1187. */
  1188. if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
  1189. set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
  1190. /* This value will be visible in the /proc/ide/hdx/settings */
  1191. floppy->ticks = IDEFLOPPY_TICKS_DELAY;
  1192. blk_queue_max_sectors(drive->queue, 64);
  1193. }
  1194. /*
  1195. * Guess what? The IOMEGA Clik! drive also needs the
  1196. * above fix. It makes nasty clicking noises without
  1197. * it, so please don't remove this.
  1198. */
  1199. if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
  1200. blk_queue_max_sectors(drive->queue, 64);
  1201. set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
  1202. }
  1203. (void) ide_floppy_get_capacity(drive);
  1204. idefloppy_add_settings(drive);
  1205. }
  1206. static void ide_floppy_remove(ide_drive_t *drive)
  1207. {
  1208. idefloppy_floppy_t *floppy = drive->driver_data;
  1209. struct gendisk *g = floppy->disk;
  1210. ide_proc_unregister_driver(drive, floppy->driver);
  1211. del_gendisk(g);
  1212. ide_floppy_put(floppy);
  1213. }
  1214. static void idefloppy_cleanup_obj(struct kref *kref)
  1215. {
  1216. struct ide_floppy_obj *floppy = to_ide_floppy(kref);
  1217. ide_drive_t *drive = floppy->drive;
  1218. struct gendisk *g = floppy->disk;
  1219. drive->driver_data = NULL;
  1220. g->private_data = NULL;
  1221. put_disk(g);
  1222. kfree(floppy);
  1223. }
  1224. #ifdef CONFIG_IDE_PROC_FS
  1225. static int proc_idefloppy_read_capacity
  1226. (char *page, char **start, off_t off, int count, int *eof, void *data)
  1227. {
  1228. ide_drive_t*drive = (ide_drive_t *)data;
  1229. int len;
  1230. len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
  1231. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  1232. }
  1233. static ide_proc_entry_t idefloppy_proc[] = {
  1234. { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
  1235. { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
  1236. { NULL, 0, NULL, NULL }
  1237. };
  1238. #endif /* CONFIG_IDE_PROC_FS */
  1239. static int ide_floppy_probe(ide_drive_t *);
  1240. static ide_driver_t idefloppy_driver = {
  1241. .gen_driver = {
  1242. .owner = THIS_MODULE,
  1243. .name = "ide-floppy",
  1244. .bus = &ide_bus_type,
  1245. },
  1246. .probe = ide_floppy_probe,
  1247. .remove = ide_floppy_remove,
  1248. .version = IDEFLOPPY_VERSION,
  1249. .media = ide_floppy,
  1250. .supports_dsc_overlap = 0,
  1251. .do_request = idefloppy_do_request,
  1252. .end_request = idefloppy_do_end_request,
  1253. .error = __ide_error,
  1254. .abort = __ide_abort,
  1255. #ifdef CONFIG_IDE_PROC_FS
  1256. .proc = idefloppy_proc,
  1257. #endif
  1258. };
  1259. static int idefloppy_open(struct inode *inode, struct file *filp)
  1260. {
  1261. struct gendisk *disk = inode->i_bdev->bd_disk;
  1262. struct ide_floppy_obj *floppy;
  1263. ide_drive_t *drive;
  1264. idefloppy_pc_t pc;
  1265. int ret = 0;
  1266. debug_log("Reached %s\n", __func__);
  1267. if (!(floppy = ide_floppy_get(disk)))
  1268. return -ENXIO;
  1269. drive = floppy->drive;
  1270. floppy->openers++;
  1271. if (floppy->openers == 1) {
  1272. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1273. /* Just in case */
  1274. idefloppy_create_test_unit_ready_cmd(&pc);
  1275. if (idefloppy_queue_pc_tail(drive, &pc)) {
  1276. idefloppy_create_start_stop_cmd(&pc, 1);
  1277. (void) idefloppy_queue_pc_tail(drive, &pc);
  1278. }
  1279. if (ide_floppy_get_capacity(drive)
  1280. && (filp->f_flags & O_NDELAY) == 0
  1281. /*
  1282. ** Allow O_NDELAY to open a drive without a disk, or with
  1283. ** an unreadable disk, so that we can get the format
  1284. ** capacity of the drive or begin the format - Sam
  1285. */
  1286. ) {
  1287. ret = -EIO;
  1288. goto out_put_floppy;
  1289. }
  1290. if (floppy->wp && (filp->f_mode & 2)) {
  1291. ret = -EROFS;
  1292. goto out_put_floppy;
  1293. }
  1294. set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
  1295. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1296. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1297. idefloppy_create_prevent_cmd(&pc, 1);
  1298. (void) idefloppy_queue_pc_tail(drive, &pc);
  1299. }
  1300. check_disk_change(inode->i_bdev);
  1301. } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
  1302. ret = -EBUSY;
  1303. goto out_put_floppy;
  1304. }
  1305. return 0;
  1306. out_put_floppy:
  1307. floppy->openers--;
  1308. ide_floppy_put(floppy);
  1309. return ret;
  1310. }
  1311. static int idefloppy_release(struct inode *inode, struct file *filp)
  1312. {
  1313. struct gendisk *disk = inode->i_bdev->bd_disk;
  1314. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1315. ide_drive_t *drive = floppy->drive;
  1316. idefloppy_pc_t pc;
  1317. debug_log("Reached %s\n", __func__);
  1318. if (floppy->openers == 1) {
  1319. /* IOMEGA Clik! drives do not support lock/unlock commands */
  1320. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1321. idefloppy_create_prevent_cmd(&pc, 0);
  1322. (void) idefloppy_queue_pc_tail(drive, &pc);
  1323. }
  1324. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1325. }
  1326. floppy->openers--;
  1327. ide_floppy_put(floppy);
  1328. return 0;
  1329. }
  1330. static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1331. {
  1332. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1333. ide_drive_t *drive = floppy->drive;
  1334. geo->heads = drive->bios_head;
  1335. geo->sectors = drive->bios_sect;
  1336. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  1337. return 0;
  1338. }
  1339. static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
  1340. unsigned long arg, unsigned int cmd)
  1341. {
  1342. if (floppy->openers > 1)
  1343. return -EBUSY;
  1344. /* The IOMEGA Clik! Drive doesn't support this command -
  1345. * no room for an eject mechanism */
  1346. if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
  1347. int prevent = arg ? 1 : 0;
  1348. if (cmd == CDROMEJECT)
  1349. prevent = 0;
  1350. idefloppy_create_prevent_cmd(pc, prevent);
  1351. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1352. }
  1353. if (cmd == CDROMEJECT) {
  1354. idefloppy_create_start_stop_cmd(pc, 2);
  1355. (void) idefloppy_queue_pc_tail(floppy->drive, pc);
  1356. }
  1357. return 0;
  1358. }
  1359. static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
  1360. int __user *arg)
  1361. {
  1362. int blocks, length, flags, err = 0;
  1363. idefloppy_pc_t pc;
  1364. if (floppy->openers > 1) {
  1365. /* Don't format if someone is using the disk */
  1366. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1367. return -EBUSY;
  1368. }
  1369. set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1370. /*
  1371. * Send ATAPI_FORMAT_UNIT to the drive.
  1372. *
  1373. * Userland gives us the following structure:
  1374. *
  1375. * struct idefloppy_format_command {
  1376. * int nblocks;
  1377. * int blocksize;
  1378. * int flags;
  1379. * } ;
  1380. *
  1381. * flags is a bitmask, currently, the only defined flag is:
  1382. *
  1383. * 0x01 - verify media after format.
  1384. */
  1385. if (get_user(blocks, arg) ||
  1386. get_user(length, arg+1) ||
  1387. get_user(flags, arg+2)) {
  1388. err = -EFAULT;
  1389. goto out;
  1390. }
  1391. (void) idefloppy_get_sfrp_bit(floppy->drive);
  1392. idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
  1393. if (idefloppy_queue_pc_tail(floppy->drive, &pc))
  1394. err = -EIO;
  1395. out:
  1396. if (err)
  1397. clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
  1398. return err;
  1399. }
  1400. static int idefloppy_ioctl(struct inode *inode, struct file *file,
  1401. unsigned int cmd, unsigned long arg)
  1402. {
  1403. struct block_device *bdev = inode->i_bdev;
  1404. struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
  1405. ide_drive_t *drive = floppy->drive;
  1406. idefloppy_pc_t pc;
  1407. void __user *argp = (void __user *)arg;
  1408. int err;
  1409. switch (cmd) {
  1410. case CDROMEJECT:
  1411. /* fall through */
  1412. case CDROM_LOCKDOOR:
  1413. return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
  1414. case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
  1415. return 0;
  1416. case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
  1417. return ide_floppy_get_format_capacities(drive, argp);
  1418. case IDEFLOPPY_IOCTL_FORMAT_START:
  1419. if (!(file->f_mode & 2))
  1420. return -EPERM;
  1421. return ide_floppy_format_unit(floppy, (int __user *)arg);
  1422. case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
  1423. return idefloppy_get_format_progress(drive, argp);
  1424. }
  1425. /*
  1426. * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
  1427. * and CDROM_SEND_PACKET (legacy) ioctls
  1428. */
  1429. if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
  1430. err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
  1431. bdev->bd_disk, cmd, argp);
  1432. else
  1433. err = -ENOTTY;
  1434. if (err == -ENOTTY)
  1435. err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
  1436. return err;
  1437. }
  1438. static int idefloppy_media_changed(struct gendisk *disk)
  1439. {
  1440. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1441. ide_drive_t *drive = floppy->drive;
  1442. /* do not scan partitions twice if this is a removable device */
  1443. if (drive->attach) {
  1444. drive->attach = 0;
  1445. return 0;
  1446. }
  1447. return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
  1448. }
  1449. static int idefloppy_revalidate_disk(struct gendisk *disk)
  1450. {
  1451. struct ide_floppy_obj *floppy = ide_floppy_g(disk);
  1452. set_capacity(disk, idefloppy_capacity(floppy->drive));
  1453. return 0;
  1454. }
  1455. static struct block_device_operations idefloppy_ops = {
  1456. .owner = THIS_MODULE,
  1457. .open = idefloppy_open,
  1458. .release = idefloppy_release,
  1459. .ioctl = idefloppy_ioctl,
  1460. .getgeo = idefloppy_getgeo,
  1461. .media_changed = idefloppy_media_changed,
  1462. .revalidate_disk= idefloppy_revalidate_disk
  1463. };
  1464. static int ide_floppy_probe(ide_drive_t *drive)
  1465. {
  1466. idefloppy_floppy_t *floppy;
  1467. struct gendisk *g;
  1468. if (!strstr("ide-floppy", drive->driver_req))
  1469. goto failed;
  1470. if (!drive->present)
  1471. goto failed;
  1472. if (drive->media != ide_floppy)
  1473. goto failed;
  1474. if (!idefloppy_identify_device (drive, drive->id)) {
  1475. printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
  1476. goto failed;
  1477. }
  1478. if (drive->scsi) {
  1479. printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
  1480. goto failed;
  1481. }
  1482. if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
  1483. printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
  1484. goto failed;
  1485. }
  1486. g = alloc_disk(1 << PARTN_BITS);
  1487. if (!g)
  1488. goto out_free_floppy;
  1489. ide_init_disk(g, drive);
  1490. ide_proc_register_driver(drive, &idefloppy_driver);
  1491. kref_init(&floppy->kref);
  1492. floppy->drive = drive;
  1493. floppy->driver = &idefloppy_driver;
  1494. floppy->disk = g;
  1495. g->private_data = &floppy->driver;
  1496. drive->driver_data = floppy;
  1497. idefloppy_setup (drive, floppy);
  1498. g->minors = 1 << PARTN_BITS;
  1499. g->driverfs_dev = &drive->gendev;
  1500. g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
  1501. g->fops = &idefloppy_ops;
  1502. drive->attach = 1;
  1503. add_disk(g);
  1504. return 0;
  1505. out_free_floppy:
  1506. kfree(floppy);
  1507. failed:
  1508. return -ENODEV;
  1509. }
  1510. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
  1511. static void __exit idefloppy_exit (void)
  1512. {
  1513. driver_unregister(&idefloppy_driver.gen_driver);
  1514. }
  1515. static int __init idefloppy_init(void)
  1516. {
  1517. printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
  1518. return driver_register(&idefloppy_driver.gen_driver);
  1519. }
  1520. MODULE_ALIAS("ide:*m-floppy*");
  1521. module_init(idefloppy_init);
  1522. module_exit(idefloppy_exit);
  1523. MODULE_LICENSE("GPL");