swim.c 21 KB

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