swim.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 TWOMEG_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 MFM_MODE 0x175
  104. #define SEEK_COMPLETE 0x176
  105. #define ONEMEG_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. struct request_queue *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 int 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 -EIO;
  411. } while (ret != 512);
  412. buffer += ret;
  413. }
  414. return 0;
  415. }
  416. static void redo_fd_request(struct request_queue *q)
  417. {
  418. struct request *req;
  419. struct floppy_state *fs;
  420. req = blk_fetch_request(q);
  421. while (req) {
  422. int err = -EIO;
  423. fs = req->rq_disk->private_data;
  424. if (blk_rq_pos(req) >= fs->total_secs)
  425. goto done;
  426. if (!fs->disk_in)
  427. goto done;
  428. if (rq_data_dir(req) == WRITE && fs->write_protected)
  429. goto done;
  430. switch (rq_data_dir(req)) {
  431. case WRITE:
  432. /* NOT IMPLEMENTED */
  433. break;
  434. case READ:
  435. err = floppy_read_sectors(fs, blk_rq_pos(req),
  436. blk_rq_cur_sectors(req),
  437. bio_data(req->bio));
  438. break;
  439. }
  440. done:
  441. if (!__blk_end_request_cur(req, err))
  442. req = blk_fetch_request(q);
  443. }
  444. }
  445. static void do_fd_request(struct request_queue *q)
  446. {
  447. redo_fd_request(q);
  448. }
  449. static struct floppy_struct floppy_type[4] = {
  450. { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */
  451. { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/
  452. { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */
  453. { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */
  454. };
  455. static int get_floppy_geometry(struct floppy_state *fs, int type,
  456. struct floppy_struct **g)
  457. {
  458. if (type >= ARRAY_SIZE(floppy_type))
  459. return -EINVAL;
  460. if (type)
  461. *g = &floppy_type[type];
  462. else if (fs->type == HD_MEDIA) /* High-Density media */
  463. *g = &floppy_type[3];
  464. else if (fs->head_number == 2) /* double-sided */
  465. *g = &floppy_type[2];
  466. else
  467. *g = &floppy_type[1];
  468. return 0;
  469. }
  470. static void setup_medium(struct floppy_state *fs)
  471. {
  472. struct swim __iomem *base = fs->swd->base;
  473. if (swim_readbit(base, DISK_IN)) {
  474. struct floppy_struct *g;
  475. fs->disk_in = 1;
  476. fs->write_protected = swim_readbit(base, WRITE_PROT);
  477. fs->type = swim_readbit(base, ONEMEG_MEDIA);
  478. if (swim_track00(base))
  479. printk(KERN_ERR
  480. "SWIM: cannot move floppy head to track 0\n");
  481. swim_track00(base);
  482. get_floppy_geometry(fs, 0, &g);
  483. fs->total_secs = g->size;
  484. fs->secpercyl = g->head * g->sect;
  485. fs->secpertrack = g->sect;
  486. fs->track = 0;
  487. } else {
  488. fs->disk_in = 0;
  489. }
  490. }
  491. static int floppy_open(struct block_device *bdev, fmode_t mode)
  492. {
  493. struct floppy_state *fs = bdev->bd_disk->private_data;
  494. struct swim __iomem *base = fs->swd->base;
  495. int err;
  496. if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL))
  497. return -EBUSY;
  498. if (mode & FMODE_EXCL)
  499. fs->ref_count = -1;
  500. else
  501. fs->ref_count++;
  502. swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
  503. udelay(10);
  504. swim_drive(base, INTERNAL_DRIVE);
  505. swim_motor(base, ON);
  506. swim_action(base, SETMFM);
  507. if (fs->ejected)
  508. setup_medium(fs);
  509. if (!fs->disk_in) {
  510. err = -ENXIO;
  511. goto out;
  512. }
  513. if (mode & FMODE_NDELAY)
  514. return 0;
  515. if (mode & (FMODE_READ|FMODE_WRITE)) {
  516. check_disk_change(bdev);
  517. if ((mode & FMODE_WRITE) && fs->write_protected) {
  518. err = -EROFS;
  519. goto out;
  520. }
  521. }
  522. return 0;
  523. out:
  524. if (fs->ref_count < 0)
  525. fs->ref_count = 0;
  526. else if (fs->ref_count > 0)
  527. --fs->ref_count;
  528. if (fs->ref_count == 0)
  529. swim_motor(base, OFF);
  530. return err;
  531. }
  532. static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
  533. {
  534. int ret;
  535. mutex_lock(&swim_mutex);
  536. ret = floppy_open(bdev, mode);
  537. mutex_unlock(&swim_mutex);
  538. return ret;
  539. }
  540. static void floppy_release(struct gendisk *disk, fmode_t mode)
  541. {
  542. struct floppy_state *fs = disk->private_data;
  543. struct swim __iomem *base = fs->swd->base;
  544. mutex_lock(&swim_mutex);
  545. if (fs->ref_count < 0)
  546. fs->ref_count = 0;
  547. else if (fs->ref_count > 0)
  548. --fs->ref_count;
  549. if (fs->ref_count == 0)
  550. swim_motor(base, OFF);
  551. mutex_unlock(&swim_mutex);
  552. }
  553. static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
  554. unsigned int cmd, unsigned long param)
  555. {
  556. struct floppy_state *fs = bdev->bd_disk->private_data;
  557. int err;
  558. if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
  559. return -EPERM;
  560. switch (cmd) {
  561. case FDEJECT:
  562. if (fs->ref_count != 1)
  563. return -EBUSY;
  564. mutex_lock(&swim_mutex);
  565. err = floppy_eject(fs);
  566. mutex_unlock(&swim_mutex);
  567. return err;
  568. case FDGETPRM:
  569. if (copy_to_user((void __user *) param, (void *) &floppy_type,
  570. sizeof(struct floppy_struct)))
  571. return -EFAULT;
  572. break;
  573. default:
  574. printk(KERN_DEBUG "SWIM floppy_ioctl: unknown cmd %d\n",
  575. cmd);
  576. return -ENOSYS;
  577. }
  578. return 0;
  579. }
  580. static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  581. {
  582. struct floppy_state *fs = bdev->bd_disk->private_data;
  583. struct floppy_struct *g;
  584. int ret;
  585. ret = get_floppy_geometry(fs, 0, &g);
  586. if (ret)
  587. return ret;
  588. geo->heads = g->head;
  589. geo->sectors = g->sect;
  590. geo->cylinders = g->track;
  591. return 0;
  592. }
  593. static unsigned int floppy_check_events(struct gendisk *disk,
  594. unsigned int clearing)
  595. {
  596. struct floppy_state *fs = disk->private_data;
  597. return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
  598. }
  599. static int floppy_revalidate(struct gendisk *disk)
  600. {
  601. struct floppy_state *fs = disk->private_data;
  602. struct swim __iomem *base = fs->swd->base;
  603. swim_drive(base, fs->location);
  604. if (fs->ejected)
  605. setup_medium(fs);
  606. if (!fs->disk_in)
  607. swim_motor(base, OFF);
  608. else
  609. fs->ejected = 0;
  610. return !fs->disk_in;
  611. }
  612. static const struct block_device_operations floppy_fops = {
  613. .owner = THIS_MODULE,
  614. .open = floppy_unlocked_open,
  615. .release = floppy_release,
  616. .ioctl = floppy_ioctl,
  617. .getgeo = floppy_getgeo,
  618. .check_events = floppy_check_events,
  619. .revalidate_disk = floppy_revalidate,
  620. };
  621. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  622. {
  623. struct swim_priv *swd = data;
  624. int drive = (*part & 3);
  625. if (drive > swd->floppy_count)
  626. return NULL;
  627. *part = 0;
  628. return get_disk(swd->unit[drive].disk);
  629. }
  630. static int swim_add_floppy(struct swim_priv *swd, enum drive_location location)
  631. {
  632. struct floppy_state *fs = &swd->unit[swd->floppy_count];
  633. struct swim __iomem *base = swd->base;
  634. fs->location = location;
  635. swim_drive(base, location);
  636. swim_motor(base, OFF);
  637. if (swim_readbit(base, SINGLE_SIDED))
  638. fs->head_number = 1;
  639. else
  640. fs->head_number = 2;
  641. fs->ref_count = 0;
  642. fs->ejected = 1;
  643. swd->floppy_count++;
  644. return 0;
  645. }
  646. static int swim_floppy_init(struct swim_priv *swd)
  647. {
  648. int err;
  649. int drive;
  650. struct swim __iomem *base = swd->base;
  651. /* scan floppy drives */
  652. swim_drive(base, INTERNAL_DRIVE);
  653. if (swim_readbit(base, DRIVE_PRESENT))
  654. swim_add_floppy(swd, INTERNAL_DRIVE);
  655. swim_drive(base, EXTERNAL_DRIVE);
  656. if (swim_readbit(base, DRIVE_PRESENT))
  657. swim_add_floppy(swd, EXTERNAL_DRIVE);
  658. /* register floppy drives */
  659. err = register_blkdev(FLOPPY_MAJOR, "fd");
  660. if (err) {
  661. printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
  662. FLOPPY_MAJOR);
  663. return -EBUSY;
  664. }
  665. for (drive = 0; drive < swd->floppy_count; drive++) {
  666. swd->unit[drive].disk = alloc_disk(1);
  667. if (swd->unit[drive].disk == NULL) {
  668. err = -ENOMEM;
  669. goto exit_put_disks;
  670. }
  671. swd->unit[drive].swd = swd;
  672. }
  673. spin_lock_init(&swd->lock);
  674. swd->queue = blk_init_queue(do_fd_request, &swd->lock);
  675. if (!swd->queue) {
  676. err = -ENOMEM;
  677. goto exit_put_disks;
  678. }
  679. for (drive = 0; drive < swd->floppy_count; drive++) {
  680. swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
  681. swd->unit[drive].disk->major = FLOPPY_MAJOR;
  682. swd->unit[drive].disk->first_minor = drive;
  683. sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
  684. swd->unit[drive].disk->fops = &floppy_fops;
  685. swd->unit[drive].disk->private_data = &swd->unit[drive];
  686. swd->unit[drive].disk->queue = swd->queue;
  687. set_capacity(swd->unit[drive].disk, 2880);
  688. add_disk(swd->unit[drive].disk);
  689. }
  690. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  691. floppy_find, NULL, swd);
  692. return 0;
  693. exit_put_disks:
  694. unregister_blkdev(FLOPPY_MAJOR, "fd");
  695. while (drive--)
  696. put_disk(swd->unit[drive].disk);
  697. return err;
  698. }
  699. static int swim_probe(struct platform_device *dev)
  700. {
  701. struct resource *res;
  702. struct swim __iomem *swim_base;
  703. struct swim_priv *swd;
  704. int ret;
  705. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  706. if (!res) {
  707. ret = -ENODEV;
  708. goto out;
  709. }
  710. if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
  711. ret = -EBUSY;
  712. goto out;
  713. }
  714. swim_base = ioremap(res->start, resource_size(res));
  715. if (!swim_base) {
  716. ret = -ENOMEM;
  717. goto out_release_io;
  718. }
  719. /* probe device */
  720. set_swim_mode(swim_base, 1);
  721. if (!get_swim_mode(swim_base)) {
  722. printk(KERN_INFO "SWIM device not found !\n");
  723. ret = -ENODEV;
  724. goto out_iounmap;
  725. }
  726. /* set platform driver data */
  727. swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
  728. if (!swd) {
  729. ret = -ENOMEM;
  730. goto out_iounmap;
  731. }
  732. platform_set_drvdata(dev, swd);
  733. swd->base = swim_base;
  734. ret = swim_floppy_init(swd);
  735. if (ret)
  736. goto out_kfree;
  737. return 0;
  738. out_kfree:
  739. kfree(swd);
  740. out_iounmap:
  741. iounmap(swim_base);
  742. out_release_io:
  743. release_mem_region(res->start, resource_size(res));
  744. out:
  745. return ret;
  746. }
  747. static int swim_remove(struct platform_device *dev)
  748. {
  749. struct swim_priv *swd = platform_get_drvdata(dev);
  750. int drive;
  751. struct resource *res;
  752. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  753. for (drive = 0; drive < swd->floppy_count; drive++) {
  754. del_gendisk(swd->unit[drive].disk);
  755. put_disk(swd->unit[drive].disk);
  756. }
  757. unregister_blkdev(FLOPPY_MAJOR, "fd");
  758. blk_cleanup_queue(swd->queue);
  759. /* eject floppies */
  760. for (drive = 0; drive < swd->floppy_count; drive++)
  761. floppy_eject(&swd->unit[drive]);
  762. iounmap(swd->base);
  763. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  764. if (res)
  765. release_mem_region(res->start, resource_size(res));
  766. kfree(swd);
  767. return 0;
  768. }
  769. static struct platform_driver swim_driver = {
  770. .probe = swim_probe,
  771. .remove = swim_remove,
  772. .driver = {
  773. .name = CARDNAME,
  774. },
  775. };
  776. static int __init swim_init(void)
  777. {
  778. printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
  779. return platform_driver_register(&swim_driver);
  780. }
  781. module_init(swim_init);
  782. static void __exit swim_exit(void)
  783. {
  784. platform_driver_unregister(&swim_driver);
  785. }
  786. module_exit(swim_exit);
  787. MODULE_DESCRIPTION("Driver for SWIM floppy controller");
  788. MODULE_LICENSE("GPL");
  789. MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
  790. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);