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