ide-floppy.c 47 KB

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