commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / block / swim_iop.c
1 /*
2  * Driver for the SWIM (Super Woz Integrated Machine) IOP
3  * floppy controller on the Macintosh IIfx and Quadra 900/950
4  *
5  * Written by Joshua M. Thompson (funaho@jurai.org)
6  * based on the SWIM3 driver (c) 1996 by Paul Mackerras.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  *
13  * 1999-06-12 (jmt) - Initial implementation.
14  */
15
16 /*
17  * -------------------
18  * Theory of Operation
19  * -------------------
20  *
21  * Since the SWIM IOP is message-driven we implement a simple request queue
22  * system.  One outstanding request may be queued at any given time (this is
23  * an IOP limitation); only when that request has completed can a new request
24  * be sent.
25  */
26
27 #include <linux/stddef.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/delay.h>
32 #include <linux/fd.h>
33 #include <linux/ioctl.h>
34 #include <asm/io.h>
35 #include <asm/uaccess.h>
36 #include <asm/mac_iop.h>
37 #include <asm/swim_iop.h>
38
39 #define DRIVER_VERSION "Version 0.1 (1999-06-12)"
40
41 #define MAX_FLOPPIES    4
42
43 enum swim_state {
44         idle,
45         available,
46         revalidating,
47         transferring,
48         ejecting
49 };
50
51 struct floppy_state {
52         enum swim_state state;
53         int     drive_num;      /* device number */
54         int     secpercyl;      /* disk geometry information */
55         int     secpertrack;
56         int     total_secs;
57         int     write_prot;     /* 1 if write-protected, 0 if not, -1 dunno */
58         int     ref_count;
59         struct timer_list timeout;
60         int     ejected;
61         struct wait_queue *wait;
62         int     wanted;
63         int     timeout_pending;
64 };
65
66 struct swim_iop_req {
67         int     sent;
68         int     complete;
69         __u8    command[32];
70         struct floppy_state *fs;
71         void    (*done)(struct swim_iop_req *);
72 };
73
74 static struct swim_iop_req *current_req;
75 static int floppy_count;
76
77 static struct floppy_state floppy_states[MAX_FLOPPIES];
78 static spinlock_t swim_iop_lock = SPIN_LOCK_UNLOCKED;
79
80 #define CURRENT elv_next_request(swim_queue)
81
82 static char *drive_names[7] = {
83         "not installed",        /* DRV_NONE    */
84         "unknown (1)",          /* DRV_UNKNOWN */
85         "a 400K drive",         /* DRV_400K    */
86         "an 800K drive"         /* DRV_800K    */
87         "unknown (4)",          /* ????        */
88         "an FDHD",              /* DRV_FDHD    */
89         "unknown (6)",          /* ????        */
90         "an Apple HD20"         /* DRV_HD20    */
91 };
92
93 int swimiop_init(void);
94 static void swimiop_init_request(struct swim_iop_req *);
95 static int swimiop_send_request(struct swim_iop_req *);
96 static void swimiop_receive(struct iop_msg *, struct pt_regs *);
97 static void swimiop_status_update(int, struct swim_drvstatus *);
98 static int swimiop_eject(struct floppy_state *fs);
99
100 static int floppy_ioctl(struct inode *inode, struct file *filp,
101                         unsigned int cmd, unsigned long param);
102 static int floppy_open(struct inode *inode, struct file *filp);
103 static int floppy_release(struct inode *inode, struct file *filp);
104 static int floppy_check_change(struct gendisk *disk);
105 static int floppy_revalidate(struct gendisk *disk);
106 static int grab_drive(struct floppy_state *fs, enum swim_state state,
107                       int interruptible);
108 static void release_drive(struct floppy_state *fs);
109 static void set_timeout(struct floppy_state *fs, int nticks,
110                         void (*proc)(unsigned long));
111 static void fd_request_timeout(unsigned long);
112 static void do_fd_request(request_queue_t * q);
113 static void start_request(struct floppy_state *fs);
114
115 static struct block_device_operations floppy_fops = {
116         .open           = floppy_open,
117         .release        = floppy_release,
118         .ioctl          = floppy_ioctl,
119         .media_changed  = floppy_check_change,
120         .revalidate_disk= floppy_revalidate,
121 };
122
123 static struct request_queue *swim_queue;
124 /*
125  * SWIM IOP initialization
126  */
127
128 int swimiop_init(void)
129 {
130         volatile struct swim_iop_req req;
131         struct swimcmd_status *cmd = (struct swimcmd_status *) &req.command[0];
132         struct swim_drvstatus *ds = &cmd->status;
133         struct floppy_state *fs;
134         int i;
135
136         current_req = NULL;
137         floppy_count = 0;
138
139         if (!iop_ism_present)
140                 return -ENODEV;
141
142         if (register_blkdev(FLOPPY_MAJOR, "fd"))
143                 return -EBUSY;
144
145         swim_queue = blk_init_queue(do_fd_request, &swim_iop_lock);
146         if (!swim_queue) {
147                 unregister_blkdev(FLOPPY_MAJOR, "fd");
148                 return -ENOMEM;
149         }
150
151         printk("SWIM-IOP: %s by Joshua M. Thompson (funaho@jurai.org)\n",
152                 DRIVER_VERSION);
153
154         if (iop_listen(SWIM_IOP, SWIM_CHAN, swimiop_receive, "SWIM") != 0) {
155                 printk(KERN_ERR "SWIM-IOP: IOP channel already in use; can't initialize.\n");
156                 unregister_blkdev(FLOPPY_MAJOR, "fd");
157                 blk_cleanup_queue(swim_queue);
158                 return -EBUSY;
159         }
160
161         printk(KERN_ERR "SWIM_IOP: probing for installed drives.\n");
162
163         for (i = 0 ; i < MAX_FLOPPIES ; i++) {
164                 memset(&floppy_states[i], 0, sizeof(struct floppy_state));
165                 fs = &floppy_states[floppy_count];
166
167                 swimiop_init_request(&req);
168                 cmd->code = CMD_STATUS;
169                 cmd->drive_num = i + 1;
170                 if (swimiop_send_request(&req) != 0) continue;
171                 while (!req.complete);
172                 if (cmd->error != 0) {
173                         printk(KERN_ERR "SWIM-IOP: probe on drive %d returned error %d\n", i, (uint) cmd->error);
174                         continue;
175                 }
176                 if (ds->installed != 0x01) continue;
177                 printk("SWIM-IOP: drive %d is %s (%s, %s, %s, %s)\n", i,
178                         drive_names[ds->info.type],
179                         ds->info.external? "ext" : "int",
180                         ds->info.scsi? "scsi" : "floppy",
181                         ds->info.fixed? "fixed" : "removable",
182                         ds->info.secondary? "secondary" : "primary");
183                 swimiop_status_update(floppy_count, ds);
184                 fs->state = idle;
185
186                 init_timer(&fs->timeout);
187                 floppy_count++;
188         }
189         printk("SWIM-IOP: detected %d installed drives.\n", floppy_count);
190
191         for (i = 0; i < floppy_count; i++) {
192                 struct gendisk *disk = alloc_disk(1);
193                 if (!disk)
194                         continue;
195                 disk->major = FLOPPY_MAJOR;
196                 disk->first_minor = i;
197                 disk->fops = &floppy_fops;
198                 sprintf(disk->disk_name, "fd%d", i);
199                 disk->private_data = &floppy_states[i];
200                 disk->queue = swim_queue;
201                 set_capacity(disk, 2880 * 2);
202                 add_disk(disk);
203         }
204
205         return 0;
206 }
207
208 static void swimiop_init_request(struct swim_iop_req *req)
209 {
210         req->sent = 0;
211         req->complete = 0;
212         req->done = NULL;
213 }
214
215 static int swimiop_send_request(struct swim_iop_req *req)
216 {
217         unsigned long flags;
218         int err;
219
220         /* It's doubtful an interrupt routine would try to send */
221         /* a SWIM request, but I'd rather play it safe here.    */
222
223         local_irq_save(flags);
224
225         if (current_req != NULL) {
226                 local_irq_restore(flags);
227                 return -ENOMEM;
228         }
229
230         current_req = req;
231
232         /* Interrupts should be back on for iop_send_message() */
233
234         local_irq_restore(flags);
235
236         err = iop_send_message(SWIM_IOP, SWIM_CHAN, (void *) req,
237                                 sizeof(req->command), (__u8 *) &req->command[0],
238                                 swimiop_receive);
239
240         /* No race condition here; we own current_req at this point */
241
242         if (err) {
243                 current_req = NULL;
244         } else {
245                 req->sent = 1;
246         }
247         return err;
248 }
249
250 /*
251  * Receive a SWIM message from the IOP.
252  *
253  * This will be called in two cases:
254  *
255  * 1. A message has been successfully sent to the IOP.
256  * 2. An unsolicited message was received from the IOP.
257  */
258
259 void swimiop_receive(struct iop_msg *msg, struct pt_regs *regs)
260 {
261         struct swim_iop_req *req;
262         struct swimmsg_status *sm;
263         struct swim_drvstatus *ds;
264
265         req = current_req;
266
267         switch(msg->status) {
268                 case IOP_MSGSTATUS_COMPLETE:
269                         memcpy(&req->command[0], &msg->reply[0], sizeof(req->command));
270                         req->complete = 1;
271                         if (req->done) (*req->done)(req);
272                         current_req = NULL;
273                         break;
274                 case IOP_MSGSTATUS_UNSOL:
275                         sm = (struct swimmsg_status *) &msg->message[0];
276                         ds = &sm->status;
277                         swimiop_status_update(sm->drive_num, ds);
278                         iop_complete_message(msg);
279                         break;
280         }
281 }
282
283 static void swimiop_status_update(int drive_num, struct swim_drvstatus *ds)
284 {
285         struct floppy_state *fs = &floppy_states[drive_num];
286
287         fs->write_prot = (ds->write_prot == 0x80);
288         if ((ds->disk_in_drive != 0x01) && (ds->disk_in_drive != 0x02)) {
289                 fs->ejected = 1;
290         } else {
291                 fs->ejected = 0;
292         }
293         switch(ds->info.type) {
294                 case DRV_400K:
295                         fs->secpercyl = 10;
296                         fs->secpertrack = 10;
297                         fs->total_secs = 800;
298                         break;
299                 case DRV_800K:
300                         fs->secpercyl = 20;
301                         fs->secpertrack = 10;
302                         fs->total_secs = 1600;
303                         break;
304                 case DRV_FDHD:
305                         fs->secpercyl = 36;
306                         fs->secpertrack = 18;
307                         fs->total_secs = 2880;
308                         break;
309                 default:
310                         fs->secpercyl = 0;
311                         fs->secpertrack = 0;
312                         fs->total_secs = 0;
313                         break;
314         }
315 }
316
317 static int swimiop_eject(struct floppy_state *fs)
318 {
319         int err, n;
320         struct swim_iop_req req;
321         struct swimcmd_eject *cmd = (struct swimcmd_eject *) &req.command[0];
322
323         err = grab_drive(fs, ejecting, 1);
324         if (err) return err;
325
326         swimiop_init_request(&req);
327         cmd->code = CMD_EJECT;
328         cmd->drive_num = fs->drive_num;
329         err = swimiop_send_request(&req);
330         if (err) {
331                 release_drive(fs);
332                 return err;
333         }
334         for (n = 2*HZ; n > 0; --n) {
335                 if (req.complete) break;
336                 if (signal_pending(current)) {
337                         err = -EINTR;
338                         break;
339                 }
340                 current->state = TASK_INTERRUPTIBLE;
341                 schedule_timeout(1);
342         }
343         release_drive(fs);
344         return cmd->error;
345 }
346
347 static struct floppy_struct floppy_type =
348         { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL };    /*  7 1.44MB 3.5"   */
349
350 static int floppy_ioctl(struct inode *inode, struct file *filp,
351                         unsigned int cmd, unsigned long param)
352 {
353         struct floppy_state *fs = inode->i_bdev->bd_disk->private_data;
354         int err;
355
356         if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
357                 return -EPERM;
358
359         switch (cmd) {
360         case FDEJECT:
361                 if (fs->ref_count != 1)
362                         return -EBUSY;
363                 err = swimiop_eject(fs);
364                 return err;
365         case FDGETPRM:
366                 if (copy_to_user((void *) param, (void *) &floppy_type,
367                                  sizeof(struct floppy_struct)))
368                         return -EFAULT;
369                 return 0;
370         }
371         return -ENOTTY;
372 }
373
374 static int floppy_open(struct inode *inode, struct file *filp)
375 {
376         struct floppy_state *fs = inode->i_bdev->bd_disk->private_data;
377
378         if (fs->ref_count == -1 || filp->f_flags & O_EXCL)
379                 return -EBUSY;
380
381         if ((filp->f_flags & O_NDELAY) == 0 && (filp->f_mode & 3)) {
382                 check_disk_change(inode->i_bdev);
383                 if (fs->ejected)
384                         return -ENXIO;
385         }
386
387         if ((filp->f_mode & 2) && fs->write_prot)
388                 return -EROFS;
389
390         if (filp->f_flags & O_EXCL)
391                 fs->ref_count = -1;
392         else
393                 ++fs->ref_count;
394
395         return 0;
396 }
397
398 static int floppy_release(struct inode *inode, struct file *filp)
399 {
400         struct floppy_state *fs = inode->i_bdev->bd_disk->private_data;
401         if (fs->ref_count > 0)
402                 fs->ref_count--;
403         return 0;
404 }
405
406 static int floppy_check_change(struct gendisk *disk)
407 {
408         struct floppy_state *fs = disk->private_data;
409         return fs->ejected;
410 }
411
412 static int floppy_revalidate(struct gendisk *disk)
413 {
414         struct floppy_state *fs = disk->private_data;
415         grab_drive(fs, revalidating, 0);
416         /* yadda, yadda */
417         release_drive(fs);
418         return 0;
419 }
420
421 static void floppy_off(unsigned int nr)
422 {
423 }
424
425 static int grab_drive(struct floppy_state *fs, enum swim_state state,
426                       int interruptible)
427 {
428         unsigned long flags;
429
430         local_irq_save(flags);
431         if (fs->state != idle) {
432                 ++fs->wanted;
433                 while (fs->state != available) {
434                         if (interruptible && signal_pending(current)) {
435                                 --fs->wanted;
436                                 local_irq_restore(flags);
437                                 return -EINTR;
438                         }
439                         interruptible_sleep_on(&fs->wait);
440                 }
441                 --fs->wanted;
442         }
443         fs->state = state;
444         local_irq_restore(flags);
445         return 0;
446 }
447
448 static void release_drive(struct floppy_state *fs)
449 {
450         unsigned long flags;
451
452         local_irq_save(flags);
453         fs->state = idle;
454         start_request(fs);
455         local_irq_restore(flags);
456 }
457
458 static void set_timeout(struct floppy_state *fs, int nticks,
459                         void (*proc)(unsigned long))
460 {
461         unsigned long flags;
462
463         local_irq_save(flags);
464         if (fs->timeout_pending)
465                 del_timer(&fs->timeout);
466         init_timer(&fs->timeout);
467         fs->timeout.expires = jiffies + nticks;
468         fs->timeout.function = proc;
469         fs->timeout.data = (unsigned long) fs;
470         add_timer(&fs->timeout);
471         fs->timeout_pending = 1;
472         local_irq_restore(flags);
473 }
474
475 static void do_fd_request(request_queue_t * q)
476 {
477         int i;
478
479         for (i = 0 ; i < floppy_count ; i++) {
480                 start_request(&floppy_states[i]);
481         }
482 }
483
484 static void fd_request_complete(struct swim_iop_req *req)
485 {
486         struct floppy_state *fs = req->fs;
487         struct swimcmd_rw *cmd = (struct swimcmd_rw *) &req->command[0];
488
489         del_timer(&fs->timeout);
490         fs->timeout_pending = 0;
491         fs->state = idle;
492         if (cmd->error) {
493                 printk(KERN_ERR "SWIM-IOP: error %d on read/write request.\n", cmd->error);
494                 end_request(CURRENT, 0);
495         } else {
496                 CURRENT->sector += cmd->num_blocks;
497                 CURRENT->current_nr_sectors -= cmd->num_blocks;
498                 if (CURRENT->current_nr_sectors <= 0) {
499                         end_request(CURRENT, 1);
500                         return;
501                 }
502         }
503         start_request(fs);
504 }
505
506 static void fd_request_timeout(unsigned long data)
507 {
508         struct floppy_state *fs = (struct floppy_state *) data;
509
510         fs->timeout_pending = 0;
511         end_request(CURRENT, 0);
512         fs->state = idle;
513 }
514
515 static void start_request(struct floppy_state *fs)
516 {
517         volatile struct swim_iop_req req;
518         struct swimcmd_rw *cmd = (struct swimcmd_rw *) &req.command[0];
519
520         if (fs->state == idle && fs->wanted) {
521                 fs->state = available;
522                 wake_up(&fs->wait);
523                 return;
524         }
525         while (CURRENT && fs->state == idle) {
526                 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
527                         panic("floppy: block not locked");
528 #if 0
529                 printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%ld buf=%p\n",
530                        CURRENT->rq_disk->disk_name, CURRENT->cmd,
531                        CURRENT->sector, CURRENT->nr_sectors, CURRENT->buffer);
532                 printk("           rq_status=%d errors=%d current_nr_sectors=%ld\n",
533                        CURRENT->rq_status, CURRENT->errors, CURRENT->current_nr_sectors);
534 #endif
535
536                 if (CURRENT->sector < 0 || CURRENT->sector >= fs->total_secs) {
537                         end_request(CURRENT, 0);
538                         continue;
539                 }
540                 if (CURRENT->current_nr_sectors == 0) {
541                         end_request(CURRENT, 1);
542                         continue;
543                 }
544                 if (fs->ejected) {
545                         end_request(CURRENT, 0);
546                         continue;
547                 }
548
549                 swimiop_init_request(&req);
550                 req.fs = fs;
551                 req.done = fd_request_complete;
552
553                 if (CURRENT->cmd == WRITE) {
554                         if (fs->write_prot) {
555                                 end_request(CURRENT, 0);
556                                 continue;
557                         }
558                         cmd->code = CMD_WRITE;
559                 } else {
560                         cmd->code = CMD_READ;
561
562                 }
563                 cmd->drive_num = fs->drive_num;
564                 cmd->buffer = CURRENT->buffer;
565                 cmd->first_block = CURRENT->sector;
566                 cmd->num_blocks = CURRENT->current_nr_sectors;
567
568                 if (swimiop_send_request(&req)) {
569                         end_request(CURRENT, 0);
570                         continue;
571                 }
572
573                 set_timeout(fs, HZ*CURRENT->current_nr_sectors,
574                                 fd_request_timeout);
575
576                 fs->state = transferring;
577         }
578 }