ide-floppy.c 53 KB

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