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