swim.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * Driver for SWIM (Sander Woz Integrated Machine) floppy controller
  3. *
  4. * Copyright (C) 2004,2008 Laurent Vivier <Laurent@lvivier.info>
  5. *
  6. * based on Alastair Bridgewater SWIM analysis, 2001
  7. * based on SWIM3 driver (c) Paul Mackerras, 1996
  8. * based on netBSD IWM driver (c) 1997, 1998 Hauke Fath.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * 2004-08-21 (lv) - Initial implementation
  16. * 2008-10-30 (lv) - Port to 2.6
  17. */
  18. #include <linux/module.h>
  19. #include <linux/fd.h>
  20. #include <linux/slab.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/mutex.h>
  23. #include <linux/hdreg.h>
  24. #include <linux/kernel.h>
  25. #include <linux/delay.h>
  26. #include <linux/platform_device.h>
  27. #include <asm/macintosh.h>
  28. #include <asm/mac_via.h>
  29. #define CARDNAME "swim"
  30. struct sector_header {
  31. unsigned char side;
  32. unsigned char track;
  33. unsigned char sector;
  34. unsigned char size;
  35. unsigned char crc0;
  36. unsigned char crc1;
  37. } __attribute__((packed));
  38. #define DRIVER_VERSION "Version 0.2 (2008-10-30)"
  39. #define REG(x) unsigned char x, x ## _pad[0x200 - 1];
  40. struct swim {
  41. REG(write_data)
  42. REG(write_mark)
  43. REG(write_CRC)
  44. REG(write_parameter)
  45. REG(write_phase)
  46. REG(write_setup)
  47. REG(write_mode0)
  48. REG(write_mode1)
  49. REG(read_data)
  50. REG(read_mark)
  51. REG(read_error)
  52. REG(read_parameter)
  53. REG(read_phase)
  54. REG(read_setup)
  55. REG(read_status)
  56. REG(read_handshake)
  57. } __attribute__((packed));
  58. #define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
  59. #define swim_read(base, reg) in_8(&(base)->read_##reg)
  60. /* IWM registers */
  61. struct iwm {
  62. REG(ph0L)
  63. REG(ph0H)
  64. REG(ph1L)
  65. REG(ph1H)
  66. REG(ph2L)
  67. REG(ph2H)
  68. REG(ph3L)
  69. REG(ph3H)
  70. REG(mtrOff)
  71. REG(mtrOn)
  72. REG(intDrive)
  73. REG(extDrive)
  74. REG(q6L)
  75. REG(q6H)
  76. REG(q7L)
  77. REG(q7H)
  78. } __attribute__((packed));
  79. #define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
  80. #define iwm_read(base, reg) in_8(&(base)->reg)
  81. /* bits in phase register */
  82. #define SEEK_POSITIVE 0x070
  83. #define SEEK_NEGATIVE 0x074
  84. #define STEP 0x071
  85. #define MOTOR_ON 0x072
  86. #define MOTOR_OFF 0x076
  87. #define INDEX 0x073
  88. #define EJECT 0x077
  89. #define SETMFM 0x171
  90. #define SETGCR 0x175
  91. #define RELAX 0x033
  92. #define LSTRB 0x008
  93. #define CA_MASK 0x077
  94. /* Select values for swim_select and swim_readbit */
  95. #define READ_DATA_0 0x074
  96. #define TWOMEG_DRIVE 0x075
  97. #define SINGLE_SIDED 0x076
  98. #define DRIVE_PRESENT 0x077
  99. #define DISK_IN 0x170
  100. #define WRITE_PROT 0x171
  101. #define TRACK_ZERO 0x172
  102. #define TACHO 0x173
  103. #define READ_DATA_1 0x174
  104. #define MFM_MODE 0x175
  105. #define SEEK_COMPLETE 0x176
  106. #define ONEMEG_MEDIA 0x177
  107. /* Bits in handshake register */
  108. #define MARK_BYTE 0x01
  109. #define CRC_ZERO 0x02
  110. #define RDDATA 0x04
  111. #define SENSE 0x08
  112. #define MOTEN 0x10
  113. #define ERROR 0x20
  114. #define DAT2BYTE 0x40
  115. #define DAT1BYTE 0x80
  116. /* bits in setup register */
  117. #define S_INV_WDATA 0x01
  118. #define S_3_5_SELECT 0x02
  119. #define S_GCR 0x04
  120. #define S_FCLK_DIV2 0x08
  121. #define S_ERROR_CORR 0x10
  122. #define S_IBM_DRIVE 0x20
  123. #define S_GCR_WRITE 0x40
  124. #define S_TIMEOUT 0x80
  125. /* bits in mode register */
  126. #define CLFIFO 0x01
  127. #define ENBL1 0x02
  128. #define ENBL2 0x04
  129. #define ACTION 0x08
  130. #define WRITE_MODE 0x10
  131. #define HEDSEL 0x20
  132. #define MOTON 0x80
  133. /*----------------------------------------------------------------------------*/
  134. enum drive_location {
  135. INTERNAL_DRIVE = 0x02,
  136. EXTERNAL_DRIVE = 0x04,
  137. };
  138. enum media_type {
  139. DD_MEDIA,
  140. HD_MEDIA,
  141. };
  142. struct floppy_state {
  143. /* physical properties */
  144. enum drive_location location; /* internal or external drive */
  145. int head_number; /* single- or double-sided drive */
  146. /* media */
  147. int disk_in;
  148. int ejected;
  149. enum media_type type;
  150. int write_protected;
  151. int total_secs;
  152. int secpercyl;
  153. int secpertrack;
  154. /* in-use information */
  155. int track;
  156. int ref_count;
  157. struct gendisk *disk;
  158. /* parent controller */
  159. struct swim_priv *swd;
  160. };
  161. enum motor_action {
  162. OFF,
  163. ON,
  164. };
  165. enum head {
  166. LOWER_HEAD = 0,
  167. UPPER_HEAD = 1,
  168. };
  169. #define FD_MAX_UNIT 2
  170. struct swim_priv {
  171. struct swim __iomem *base;
  172. spinlock_t lock;
  173. struct request_queue *queue;
  174. int floppy_count;
  175. struct floppy_state unit[FD_MAX_UNIT];
  176. };
  177. extern int swim_read_sector_header(struct swim __iomem *base,
  178. struct sector_header *header);
  179. extern int swim_read_sector_data(struct swim __iomem *base,
  180. unsigned char *data);
  181. static DEFINE_MUTEX(swim_mutex);
  182. static inline void set_swim_mode(struct swim __iomem *base, int enable)
  183. {
  184. struct iwm __iomem *iwm_base;
  185. unsigned long flags;
  186. if (!enable) {
  187. swim_write(base, mode0, 0xf8);
  188. return;
  189. }
  190. iwm_base = (struct iwm __iomem *)base;
  191. local_irq_save(flags);
  192. iwm_read(iwm_base, q7L);
  193. iwm_read(iwm_base, mtrOff);
  194. iwm_read(iwm_base, q6H);
  195. iwm_write(iwm_base, q7H, 0x57);
  196. iwm_write(iwm_base, q7H, 0x17);
  197. iwm_write(iwm_base, q7H, 0x57);
  198. iwm_write(iwm_base, q7H, 0x57);
  199. local_irq_restore(flags);
  200. }
  201. static inline int get_swim_mode(struct swim __iomem *base)
  202. {
  203. unsigned long flags;
  204. local_irq_save(flags);
  205. swim_write(base, phase, 0xf5);
  206. if (swim_read(base, phase) != 0xf5)
  207. goto is_iwm;
  208. swim_write(base, phase, 0xf6);
  209. if (swim_read(base, phase) != 0xf6)
  210. goto is_iwm;
  211. swim_write(base, phase, 0xf7);
  212. if (swim_read(base, phase) != 0xf7)
  213. goto is_iwm;
  214. local_irq_restore(flags);
  215. return 1;
  216. is_iwm:
  217. local_irq_restore(flags);
  218. return 0;
  219. }
  220. static inline void swim_select(struct swim __iomem *base, int sel)
  221. {
  222. swim_write(base, phase, RELAX);
  223. via1_set_head(sel & 0x100);
  224. swim_write(base, phase, sel & CA_MASK);
  225. }
  226. static inline void swim_action(struct swim __iomem *base, int action)
  227. {
  228. unsigned long flags;
  229. local_irq_save(flags);
  230. swim_select(base, action);
  231. udelay(1);
  232. swim_write(base, phase, (LSTRB<<4) | LSTRB);
  233. udelay(1);
  234. swim_write(base, phase, (LSTRB<<4) | ((~LSTRB) & 0x0F));
  235. udelay(1);
  236. local_irq_restore(flags);
  237. }
  238. static inline int swim_readbit(struct swim __iomem *base, int bit)
  239. {
  240. int stat;
  241. swim_select(base, bit);
  242. udelay(10);
  243. stat = swim_read(base, handshake);
  244. return (stat & SENSE) == 0;
  245. }
  246. static inline void swim_drive(struct swim __iomem *base,
  247. enum drive_location location)
  248. {
  249. if (location == INTERNAL_DRIVE) {
  250. swim_write(base, mode0, EXTERNAL_DRIVE); /* clear drive 1 bit */
  251. swim_write(base, mode1, INTERNAL_DRIVE); /* set drive 0 bit */
  252. } else if (location == EXTERNAL_DRIVE) {
  253. swim_write(base, mode0, INTERNAL_DRIVE); /* clear drive 0 bit */
  254. swim_write(base, mode1, EXTERNAL_DRIVE); /* set drive 1 bit */
  255. }
  256. }
  257. static inline void swim_motor(struct swim __iomem *base,
  258. enum motor_action action)
  259. {
  260. if (action == ON) {
  261. int i;
  262. swim_action(base, MOTOR_ON);
  263. for (i = 0; i < 2*HZ; i++) {
  264. swim_select(base, RELAX);
  265. if (swim_readbit(base, MOTOR_ON))
  266. break;
  267. current->state = TASK_INTERRUPTIBLE;
  268. schedule_timeout(1);
  269. }
  270. } else if (action == OFF) {
  271. swim_action(base, MOTOR_OFF);
  272. swim_select(base, RELAX);
  273. }
  274. }
  275. static inline void swim_eject(struct swim __iomem *base)
  276. {
  277. int i;
  278. swim_action(base, EJECT);
  279. for (i = 0; i < 2*HZ; i++) {
  280. swim_select(base, RELAX);
  281. if (!swim_readbit(base, DISK_IN))
  282. break;
  283. current->state = TASK_INTERRUPTIBLE;
  284. schedule_timeout(1);
  285. }
  286. swim_select(base, RELAX);
  287. }
  288. static inline void swim_head(struct swim __iomem *base, enum head head)
  289. {
  290. /* wait drive is ready */
  291. if (head == UPPER_HEAD)
  292. swim_select(base, READ_DATA_1);
  293. else if (head == LOWER_HEAD)
  294. swim_select(base, READ_DATA_0);
  295. }
  296. static inline int swim_step(struct swim __iomem *base)
  297. {
  298. int wait;
  299. swim_action(base, STEP);
  300. for (wait = 0; wait < HZ; wait++) {
  301. current->state = TASK_INTERRUPTIBLE;
  302. schedule_timeout(1);
  303. swim_select(base, RELAX);
  304. if (!swim_readbit(base, STEP))
  305. return 0;
  306. }
  307. return -1;
  308. }
  309. static inline int swim_track00(struct swim __iomem *base)
  310. {
  311. int try;
  312. swim_action(base, SEEK_NEGATIVE);
  313. for (try = 0; try < 100; try++) {
  314. swim_select(base, RELAX);
  315. if (swim_readbit(base, TRACK_ZERO))
  316. break;
  317. if (swim_step(base))
  318. return -1;
  319. }
  320. if (swim_readbit(base, TRACK_ZERO))
  321. return 0;
  322. return -1;
  323. }
  324. static inline int swim_seek(struct swim __iomem *base, int step)
  325. {
  326. if (step == 0)
  327. return 0;
  328. if (step < 0) {
  329. swim_action(base, SEEK_NEGATIVE);
  330. step = -step;
  331. } else
  332. swim_action(base, SEEK_POSITIVE);
  333. for ( ; step > 0; step--) {
  334. if (swim_step(base))
  335. return -1;
  336. }
  337. return 0;
  338. }
  339. static inline int swim_track(struct floppy_state *fs, int track)
  340. {
  341. struct swim __iomem *base = fs->swd->base;
  342. int ret;
  343. ret = swim_seek(base, track - fs->track);
  344. if (ret == 0)
  345. fs->track = track;
  346. else {
  347. swim_track00(base);
  348. fs->track = 0;
  349. }
  350. return ret;
  351. }
  352. static int floppy_eject(struct floppy_state *fs)
  353. {
  354. struct swim __iomem *base = fs->swd->base;
  355. swim_drive(base, fs->location);
  356. swim_motor(base, OFF);
  357. swim_eject(base);
  358. fs->disk_in = 0;
  359. fs->ejected = 1;
  360. return 0;
  361. }
  362. static inline int swim_read_sector(struct floppy_state *fs,
  363. int side, int track,
  364. int sector, unsigned char *buffer)
  365. {
  366. struct swim __iomem *base = fs->swd->base;
  367. unsigned long flags;
  368. struct sector_header header;
  369. int ret = -1;
  370. short i;
  371. swim_track(fs, track);
  372. swim_write(base, mode1, MOTON);
  373. swim_head(base, side);
  374. swim_write(base, mode0, side);
  375. local_irq_save(flags);
  376. for (i = 0; i < 36; i++) {
  377. ret = swim_read_sector_header(base, &header);
  378. if (!ret && (header.sector == sector)) {
  379. /* found */
  380. ret = swim_read_sector_data(base, buffer);
  381. break;
  382. }
  383. }
  384. local_irq_restore(flags);
  385. swim_write(base, mode0, MOTON);
  386. if ((header.side != side) || (header.track != track) ||
  387. (header.sector != sector))
  388. return 0;
  389. return ret;
  390. }
  391. static int floppy_read_sectors(struct floppy_state *fs,
  392. int req_sector, int sectors_nb,
  393. unsigned char *buffer)
  394. {
  395. struct swim __iomem *base = fs->swd->base;
  396. int ret;
  397. int side, track, sector;
  398. int i, try;
  399. swim_drive(base, fs->location);
  400. for (i = req_sector; i < req_sector + sectors_nb; i++) {
  401. int x;
  402. track = i / fs->secpercyl;
  403. x = i % fs->secpercyl;
  404. side = x / fs->secpertrack;
  405. sector = x % fs->secpertrack + 1;
  406. try = 5;
  407. do {
  408. ret = swim_read_sector(fs, side, track, sector,
  409. buffer);
  410. if (try-- == 0)
  411. return -EIO;
  412. } while (ret != 512);
  413. buffer += ret;
  414. }
  415. return 0;
  416. }
  417. static void redo_fd_request(struct request_queue *q)
  418. {
  419. struct request *req;
  420. struct floppy_state *fs;
  421. req = blk_fetch_request(q);
  422. while (req) {
  423. int err = -EIO;
  424. fs = req->rq_disk->private_data;
  425. if (blk_rq_pos(req) >= fs->total_secs)
  426. goto done;
  427. if (!fs->disk_in)
  428. goto done;
  429. if (rq_data_dir(req) == WRITE && fs->write_protected)
  430. goto done;
  431. switch (rq_data_dir(req)) {
  432. case WRITE:
  433. /* NOT IMPLEMENTED */
  434. break;
  435. case READ:
  436. err = floppy_read_sectors(fs, blk_rq_pos(req),
  437. blk_rq_cur_sectors(req),
  438. req->buffer);
  439. break;
  440. }
  441. done:
  442. if (!__blk_end_request_cur(req, err))
  443. req = blk_fetch_request(q);
  444. }
  445. }
  446. static void do_fd_request(struct request_queue *q)
  447. {
  448. redo_fd_request(q);
  449. }
  450. static struct floppy_struct floppy_type[4] = {
  451. { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */
  452. { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/
  453. { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */
  454. { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */
  455. };
  456. static int get_floppy_geometry(struct floppy_state *fs, int type,
  457. struct floppy_struct **g)
  458. {
  459. if (type >= ARRAY_SIZE(floppy_type))
  460. return -EINVAL;
  461. if (type)
  462. *g = &floppy_type[type];
  463. else if (fs->type == HD_MEDIA) /* High-Density media */
  464. *g = &floppy_type[3];
  465. else if (fs->head_number == 2) /* double-sided */
  466. *g = &floppy_type[2];
  467. else
  468. *g = &floppy_type[1];
  469. return 0;
  470. }
  471. static void setup_medium(struct floppy_state *fs)
  472. {
  473. struct swim __iomem *base = fs->swd->base;
  474. if (swim_readbit(base, DISK_IN)) {
  475. struct floppy_struct *g;
  476. fs->disk_in = 1;
  477. fs->write_protected = swim_readbit(base, WRITE_PROT);
  478. fs->type = swim_readbit(base, ONEMEG_MEDIA);
  479. if (swim_track00(base))
  480. printk(KERN_ERR
  481. "SWIM: cannot move floppy head to track 0\n");
  482. swim_track00(base);
  483. get_floppy_geometry(fs, 0, &g);
  484. fs->total_secs = g->size;
  485. fs->secpercyl = g->head * g->sect;
  486. fs->secpertrack = g->sect;
  487. fs->track = 0;
  488. } else {
  489. fs->disk_in = 0;
  490. }
  491. }
  492. static int floppy_open(struct block_device *bdev, fmode_t mode)
  493. {
  494. struct floppy_state *fs = bdev->bd_disk->private_data;
  495. struct swim __iomem *base = fs->swd->base;
  496. int err;
  497. if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL))
  498. return -EBUSY;
  499. if (mode & FMODE_EXCL)
  500. fs->ref_count = -1;
  501. else
  502. fs->ref_count++;
  503. swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
  504. udelay(10);
  505. swim_drive(base, INTERNAL_DRIVE);
  506. swim_motor(base, ON);
  507. swim_action(base, SETMFM);
  508. if (fs->ejected)
  509. setup_medium(fs);
  510. if (!fs->disk_in) {
  511. err = -ENXIO;
  512. goto out;
  513. }
  514. if (mode & FMODE_NDELAY)
  515. return 0;
  516. if (mode & (FMODE_READ|FMODE_WRITE)) {
  517. check_disk_change(bdev);
  518. if ((mode & FMODE_WRITE) && fs->write_protected) {
  519. err = -EROFS;
  520. goto out;
  521. }
  522. }
  523. return 0;
  524. out:
  525. if (fs->ref_count < 0)
  526. fs->ref_count = 0;
  527. else if (fs->ref_count > 0)
  528. --fs->ref_count;
  529. if (fs->ref_count == 0)
  530. swim_motor(base, OFF);
  531. return err;
  532. }
  533. static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
  534. {
  535. int ret;
  536. mutex_lock(&swim_mutex);
  537. ret = floppy_open(bdev, mode);
  538. mutex_unlock(&swim_mutex);
  539. return ret;
  540. }
  541. static int floppy_release(struct gendisk *disk, fmode_t mode)
  542. {
  543. struct floppy_state *fs = disk->private_data;
  544. struct swim __iomem *base = fs->swd->base;
  545. mutex_lock(&swim_mutex);
  546. if (fs->ref_count < 0)
  547. fs->ref_count = 0;
  548. else if (fs->ref_count > 0)
  549. --fs->ref_count;
  550. if (fs->ref_count == 0)
  551. swim_motor(base, OFF);
  552. mutex_unlock(&swim_mutex);
  553. return 0;
  554. }
  555. static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
  556. unsigned int cmd, unsigned long param)
  557. {
  558. struct floppy_state *fs = bdev->bd_disk->private_data;
  559. int err;
  560. if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
  561. return -EPERM;
  562. switch (cmd) {
  563. case FDEJECT:
  564. if (fs->ref_count != 1)
  565. return -EBUSY;
  566. mutex_lock(&swim_mutex);
  567. err = floppy_eject(fs);
  568. mutex_unlock(&swim_mutex);
  569. return err;
  570. case FDGETPRM:
  571. if (copy_to_user((void __user *) param, (void *) &floppy_type,
  572. sizeof(struct floppy_struct)))
  573. return -EFAULT;
  574. break;
  575. default:
  576. printk(KERN_DEBUG "SWIM floppy_ioctl: unknown cmd %d\n",
  577. cmd);
  578. return -ENOSYS;
  579. }
  580. return 0;
  581. }
  582. static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  583. {
  584. struct floppy_state *fs = bdev->bd_disk->private_data;
  585. struct floppy_struct *g;
  586. int ret;
  587. ret = get_floppy_geometry(fs, 0, &g);
  588. if (ret)
  589. return ret;
  590. geo->heads = g->head;
  591. geo->sectors = g->sect;
  592. geo->cylinders = g->track;
  593. return 0;
  594. }
  595. static unsigned int floppy_check_events(struct gendisk *disk,
  596. unsigned int clearing)
  597. {
  598. struct floppy_state *fs = disk->private_data;
  599. return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
  600. }
  601. static int floppy_revalidate(struct gendisk *disk)
  602. {
  603. struct floppy_state *fs = disk->private_data;
  604. struct swim __iomem *base = fs->swd->base;
  605. swim_drive(base, fs->location);
  606. if (fs->ejected)
  607. setup_medium(fs);
  608. if (!fs->disk_in)
  609. swim_motor(base, OFF);
  610. else
  611. fs->ejected = 0;
  612. return !fs->disk_in;
  613. }
  614. static const struct block_device_operations floppy_fops = {
  615. .owner = THIS_MODULE,
  616. .open = floppy_unlocked_open,
  617. .release = floppy_release,
  618. .ioctl = floppy_ioctl,
  619. .getgeo = floppy_getgeo,
  620. .check_events = floppy_check_events,
  621. .revalidate_disk = floppy_revalidate,
  622. };
  623. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  624. {
  625. struct swim_priv *swd = data;
  626. int drive = (*part & 3);
  627. if (drive > swd->floppy_count)
  628. return NULL;
  629. *part = 0;
  630. return get_disk(swd->unit[drive].disk);
  631. }
  632. static int __devinit swim_add_floppy(struct swim_priv *swd,
  633. enum drive_location location)
  634. {
  635. struct floppy_state *fs = &swd->unit[swd->floppy_count];
  636. struct swim __iomem *base = swd->base;
  637. fs->location = location;
  638. swim_drive(base, location);
  639. swim_motor(base, OFF);
  640. if (swim_readbit(base, SINGLE_SIDED))
  641. fs->head_number = 1;
  642. else
  643. fs->head_number = 2;
  644. fs->ref_count = 0;
  645. fs->ejected = 1;
  646. swd->floppy_count++;
  647. return 0;
  648. }
  649. static int __devinit swim_floppy_init(struct swim_priv *swd)
  650. {
  651. int err;
  652. int drive;
  653. struct swim __iomem *base = swd->base;
  654. /* scan floppy drives */
  655. swim_drive(base, INTERNAL_DRIVE);
  656. if (swim_readbit(base, DRIVE_PRESENT))
  657. swim_add_floppy(swd, INTERNAL_DRIVE);
  658. swim_drive(base, EXTERNAL_DRIVE);
  659. if (swim_readbit(base, DRIVE_PRESENT))
  660. swim_add_floppy(swd, EXTERNAL_DRIVE);
  661. /* register floppy drives */
  662. err = register_blkdev(FLOPPY_MAJOR, "fd");
  663. if (err) {
  664. printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
  665. FLOPPY_MAJOR);
  666. return -EBUSY;
  667. }
  668. for (drive = 0; drive < swd->floppy_count; drive++) {
  669. swd->unit[drive].disk = alloc_disk(1);
  670. if (swd->unit[drive].disk == NULL) {
  671. err = -ENOMEM;
  672. goto exit_put_disks;
  673. }
  674. swd->unit[drive].swd = swd;
  675. }
  676. swd->queue = blk_init_queue(do_fd_request, &swd->lock);
  677. if (!swd->queue) {
  678. err = -ENOMEM;
  679. goto exit_put_disks;
  680. }
  681. for (drive = 0; drive < swd->floppy_count; drive++) {
  682. swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
  683. swd->unit[drive].disk->major = FLOPPY_MAJOR;
  684. swd->unit[drive].disk->first_minor = drive;
  685. sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
  686. swd->unit[drive].disk->fops = &floppy_fops;
  687. swd->unit[drive].disk->private_data = &swd->unit[drive];
  688. swd->unit[drive].disk->queue = swd->queue;
  689. set_capacity(swd->unit[drive].disk, 2880);
  690. add_disk(swd->unit[drive].disk);
  691. }
  692. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  693. floppy_find, NULL, swd);
  694. return 0;
  695. exit_put_disks:
  696. unregister_blkdev(FLOPPY_MAJOR, "fd");
  697. while (drive--)
  698. put_disk(swd->unit[drive].disk);
  699. return err;
  700. }
  701. static int __devinit swim_probe(struct platform_device *dev)
  702. {
  703. struct resource *res;
  704. struct swim __iomem *swim_base;
  705. struct swim_priv *swd;
  706. int ret;
  707. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  708. if (!res) {
  709. ret = -ENODEV;
  710. goto out;
  711. }
  712. if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
  713. ret = -EBUSY;
  714. goto out;
  715. }
  716. swim_base = ioremap(res->start, resource_size(res));
  717. if (!swim_base) {
  718. return -ENOMEM;
  719. goto out_release_io;
  720. }
  721. /* probe device */
  722. set_swim_mode(swim_base, 1);
  723. if (!get_swim_mode(swim_base)) {
  724. printk(KERN_INFO "SWIM device not found !\n");
  725. ret = -ENODEV;
  726. goto out_iounmap;
  727. }
  728. /* set platform driver data */
  729. swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
  730. if (!swd) {
  731. ret = -ENOMEM;
  732. goto out_iounmap;
  733. }
  734. platform_set_drvdata(dev, swd);
  735. swd->base = swim_base;
  736. ret = swim_floppy_init(swd);
  737. if (ret)
  738. goto out_kfree;
  739. return 0;
  740. out_kfree:
  741. platform_set_drvdata(dev, NULL);
  742. kfree(swd);
  743. out_iounmap:
  744. iounmap(swim_base);
  745. out_release_io:
  746. release_mem_region(res->start, resource_size(res));
  747. out:
  748. return ret;
  749. }
  750. static int __devexit swim_remove(struct platform_device *dev)
  751. {
  752. struct swim_priv *swd = platform_get_drvdata(dev);
  753. int drive;
  754. struct resource *res;
  755. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  756. for (drive = 0; drive < swd->floppy_count; drive++) {
  757. del_gendisk(swd->unit[drive].disk);
  758. put_disk(swd->unit[drive].disk);
  759. }
  760. unregister_blkdev(FLOPPY_MAJOR, "fd");
  761. blk_cleanup_queue(swd->queue);
  762. /* eject floppies */
  763. for (drive = 0; drive < swd->floppy_count; drive++)
  764. floppy_eject(&swd->unit[drive]);
  765. iounmap(swd->base);
  766. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  767. if (res)
  768. release_mem_region(res->start, resource_size(res));
  769. platform_set_drvdata(dev, NULL);
  770. kfree(swd);
  771. return 0;
  772. }
  773. static struct platform_driver swim_driver = {
  774. .probe = swim_probe,
  775. .remove = __devexit_p(swim_remove),
  776. .driver = {
  777. .name = CARDNAME,
  778. .owner = THIS_MODULE,
  779. },
  780. };
  781. static int __init swim_init(void)
  782. {
  783. printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
  784. return platform_driver_register(&swim_driver);
  785. }
  786. module_init(swim_init);
  787. static void __exit swim_exit(void)
  788. {
  789. platform_driver_unregister(&swim_driver);
  790. }
  791. module_exit(swim_exit);
  792. MODULE_DESCRIPTION("Driver for SWIM floppy controller");
  793. MODULE_LICENSE("GPL");
  794. MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
  795. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);