Merge branch 'fix/hda' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[pandora-kernel.git] / drivers / block / paride / pd.c
1 /* 
2         pd.c    (c) 1997-8  Grant R. Guenther <grant@torque.net>
3                             Under the terms of the GNU General Public License.
4
5         This is the high-level driver for parallel port IDE hard
6         drives based on chips supported by the paride module.
7
8         By default, the driver will autoprobe for a single parallel
9         port IDE drive, but if their individual parameters are
10         specified, the driver can handle up to 4 drives.
11
12         The behaviour of the pd driver can be altered by setting
13         some parameters from the insmod command line.  The following
14         parameters are adjustable:
15  
16             drive0      These four arguments can be arrays of       
17             drive1      1-8 integers as follows:
18             drive2
19             drive3      <prt>,<pro>,<uni>,<mod>,<geo>,<sby>,<dly>,<slv>
20
21                         Where,
22
23                 <prt>   is the base of the parallel port address for
24                         the corresponding drive.  (required)
25
26                 <pro>   is the protocol number for the adapter that
27                         supports this drive.  These numbers are
28                         logged by 'paride' when the protocol modules
29                         are initialised.  (0 if not given)
30
31                 <uni>   for those adapters that support chained
32                         devices, this is the unit selector for the
33                         chain of devices on the given port.  It should
34                         be zero for devices that don't support chaining.
35                         (0 if not given)
36
37                 <mod>   this can be -1 to choose the best mode, or one
38                         of the mode numbers supported by the adapter.
39                         (-1 if not given)
40
41                 <geo>   this defaults to 0 to indicate that the driver
42                         should use the CHS geometry provided by the drive
43                         itself.  If set to 1, the driver will provide
44                         a logical geometry with 64 heads and 32 sectors
45                         per track, to be consistent with most SCSI
46                         drivers.  (0 if not given)
47
48                 <sby>   set this to zero to disable the power saving
49                         standby mode, if needed.  (1 if not given)
50
51                 <dly>   some parallel ports require the driver to 
52                         go more slowly.  -1 sets a default value that
53                         should work with the chosen protocol.  Otherwise,
54                         set this to a small integer, the larger it is
55                         the slower the port i/o.  In some cases, setting
56                         this to zero will speed up the device. (default -1)
57
58                 <slv>   IDE disks can be jumpered to master or slave.
59                         Set this to 0 to choose the master drive, 1 to
60                         choose the slave, -1 (the default) to choose the
61                         first drive found.
62                         
63
64             major       You may use this parameter to overide the
65                         default major number (45) that this driver
66                         will use.  Be sure to change the device
67                         name as well.
68
69             name        This parameter is a character string that
70                         contains the name the kernel will use for this
71                         device (in /proc output, for instance).
72                         (default "pd")
73
74             cluster     The driver will attempt to aggregate requests
75                         for adjacent blocks into larger multi-block
76                         clusters.  The maximum cluster size (in 512
77                         byte sectors) is set with this parameter.
78                         (default 64)
79
80             verbose     This parameter controls the amount of logging
81                         that the driver will do.  Set it to 0 for 
82                         normal operation, 1 to see autoprobe progress
83                         messages, or 2 to see additional debugging
84                         output.  (default 0)
85
86             nice        This parameter controls the driver's use of
87                         idle CPU time, at the expense of some speed.
88
89         If this driver is built into the kernel, you can use kernel
90         the following command line parameters, with the same values
91         as the corresponding module parameters listed above:
92
93             pd.drive0
94             pd.drive1
95             pd.drive2
96             pd.drive3
97             pd.cluster
98             pd.nice
99
100         In addition, you can use the parameter pd.disable to disable
101         the driver entirely.
102  
103 */
104
105 /* Changes:
106
107         1.01    GRG 1997.01.24  Restored pd_reset()
108                                 Added eject ioctl
109         1.02    GRG 1998.05.06  SMP spinlock changes, 
110                                 Added slave support
111         1.03    GRG 1998.06.16  Eliminate an Ugh.
112         1.04    GRG 1998.08.15  Extra debugging, use HZ in loop timing
113         1.05    GRG 1998.09.24  Added jumbo support
114
115 */
116
117 #define PD_VERSION      "1.05"
118 #define PD_MAJOR        45
119 #define PD_NAME         "pd"
120 #define PD_UNITS        4
121
122 /* Here are things one can override from the insmod command.
123    Most are autoprobed by paride unless set here.  Verbose is off
124    by default.
125
126 */
127
128 static int verbose = 0;
129 static int major = PD_MAJOR;
130 static char *name = PD_NAME;
131 static int cluster = 64;
132 static int nice = 0;
133 static int disable = 0;
134
135 static int drive0[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
136 static int drive1[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
137 static int drive2[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
138 static int drive3[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
139
140 static int (*drives[4])[8] = {&drive0, &drive1, &drive2, &drive3};
141
142 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
143
144 /* end of parameters */
145
146 #include <linux/init.h>
147 #include <linux/module.h>
148 #include <linux/gfp.h>
149 #include <linux/fs.h>
150 #include <linux/delay.h>
151 #include <linux/hdreg.h>
152 #include <linux/cdrom.h>        /* for the eject ioctl */
153 #include <linux/blkdev.h>
154 #include <linux/blkpg.h>
155 #include <linux/kernel.h>
156 #include <asm/uaccess.h>
157 #include <linux/workqueue.h>
158
159 static DEFINE_SPINLOCK(pd_lock);
160
161 module_param(verbose, bool, 0);
162 module_param(major, int, 0);
163 module_param(name, charp, 0);
164 module_param(cluster, int, 0);
165 module_param(nice, int, 0);
166 module_param_array(drive0, int, NULL, 0);
167 module_param_array(drive1, int, NULL, 0);
168 module_param_array(drive2, int, NULL, 0);
169 module_param_array(drive3, int, NULL, 0);
170
171 #include "paride.h"
172
173 #define PD_BITS    4
174
175 /* numbers for "SCSI" geometry */
176
177 #define PD_LOG_HEADS    64
178 #define PD_LOG_SECTS    32
179
180 #define PD_ID_OFF       54
181 #define PD_ID_LEN       14
182
183 #define PD_MAX_RETRIES  5
184 #define PD_TMO          800     /* interrupt timeout in jiffies */
185 #define PD_SPIN_DEL     50      /* spin delay in micro-seconds  */
186
187 #define PD_SPIN         (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
188
189 #define STAT_ERR        0x00001
190 #define STAT_INDEX      0x00002
191 #define STAT_ECC        0x00004
192 #define STAT_DRQ        0x00008
193 #define STAT_SEEK       0x00010
194 #define STAT_WRERR      0x00020
195 #define STAT_READY      0x00040
196 #define STAT_BUSY       0x00080
197
198 #define ERR_AMNF        0x00100
199 #define ERR_TK0NF       0x00200
200 #define ERR_ABRT        0x00400
201 #define ERR_MCR         0x00800
202 #define ERR_IDNF        0x01000
203 #define ERR_MC          0x02000
204 #define ERR_UNC         0x04000
205 #define ERR_TMO         0x10000
206
207 #define IDE_READ                0x20
208 #define IDE_WRITE               0x30
209 #define IDE_READ_VRFY           0x40
210 #define IDE_INIT_DEV_PARMS      0x91
211 #define IDE_STANDBY             0x96
212 #define IDE_ACKCHANGE           0xdb
213 #define IDE_DOORLOCK            0xde
214 #define IDE_DOORUNLOCK          0xdf
215 #define IDE_IDENTIFY            0xec
216 #define IDE_EJECT               0xed
217
218 #define PD_NAMELEN      8
219
220 struct pd_unit {
221         struct pi_adapter pia;  /* interface to paride layer */
222         struct pi_adapter *pi;
223         int access;             /* count of active opens ... */
224         int capacity;           /* Size of this volume in sectors */
225         int heads;              /* physical geometry */
226         int sectors;
227         int cylinders;
228         int can_lba;
229         int drive;              /* master=0 slave=1 */
230         int changed;            /* Have we seen a disk change ? */
231         int removable;          /* removable media device  ?  */
232         int standby;
233         int alt_geom;
234         char name[PD_NAMELEN];  /* pda, pdb, etc ... */
235         struct gendisk *gd;
236 };
237
238 static struct pd_unit pd[PD_UNITS];
239
240 static char pd_scratch[512];    /* scratch block buffer */
241
242 static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
243         "READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
244         "IDNF", "MC", "UNC", "???", "TMO"
245 };
246
247 static inline int status_reg(struct pd_unit *disk)
248 {
249         return pi_read_regr(disk->pi, 1, 6);
250 }
251
252 static inline int read_reg(struct pd_unit *disk, int reg)
253 {
254         return pi_read_regr(disk->pi, 0, reg);
255 }
256
257 static inline void write_status(struct pd_unit *disk, int val)
258 {
259         pi_write_regr(disk->pi, 1, 6, val);
260 }
261
262 static inline void write_reg(struct pd_unit *disk, int reg, int val)
263 {
264         pi_write_regr(disk->pi, 0, reg, val);
265 }
266
267 static inline u8 DRIVE(struct pd_unit *disk)
268 {
269         return 0xa0+0x10*disk->drive;
270 }
271
272 /*  ide command interface */
273
274 static void pd_print_error(struct pd_unit *disk, char *msg, int status)
275 {
276         int i;
277
278         printk("%s: %s: status = 0x%x =", disk->name, msg, status);
279         for (i = 0; i < ARRAY_SIZE(pd_errs); i++)
280                 if (status & (1 << i))
281                         printk(" %s", pd_errs[i]);
282         printk("\n");
283 }
284
285 static void pd_reset(struct pd_unit *disk)
286 {                               /* called only for MASTER drive */
287         write_status(disk, 4);
288         udelay(50);
289         write_status(disk, 0);
290         udelay(250);
291 }
292
293 #define DBMSG(msg)      ((verbose>1)?(msg):NULL)
294
295 static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
296 {                               /* polled wait */
297         int k, r, e;
298
299         k = 0;
300         while (k < PD_SPIN) {
301                 r = status_reg(disk);
302                 k++;
303                 if (((r & w) == w) && !(r & STAT_BUSY))
304                         break;
305                 udelay(PD_SPIN_DEL);
306         }
307         e = (read_reg(disk, 1) << 8) + read_reg(disk, 7);
308         if (k >= PD_SPIN)
309                 e |= ERR_TMO;
310         if ((e & (STAT_ERR | ERR_TMO)) && (msg != NULL))
311                 pd_print_error(disk, msg, e);
312         return e;
313 }
314
315 static void pd_send_command(struct pd_unit *disk, int n, int s, int h, int c0, int c1, int func)
316 {
317         write_reg(disk, 6, DRIVE(disk) + h);
318         write_reg(disk, 1, 0);          /* the IDE task file */
319         write_reg(disk, 2, n);
320         write_reg(disk, 3, s);
321         write_reg(disk, 4, c0);
322         write_reg(disk, 5, c1);
323         write_reg(disk, 7, func);
324
325         udelay(1);
326 }
327
328 static void pd_ide_command(struct pd_unit *disk, int func, int block, int count)
329 {
330         int c1, c0, h, s;
331
332         if (disk->can_lba) {
333                 s = block & 255;
334                 c0 = (block >>= 8) & 255;
335                 c1 = (block >>= 8) & 255;
336                 h = ((block >>= 8) & 15) + 0x40;
337         } else {
338                 s = (block % disk->sectors) + 1;
339                 h = (block /= disk->sectors) % disk->heads;
340                 c0 = (block /= disk->heads) % 256;
341                 c1 = (block >>= 8);
342         }
343         pd_send_command(disk, count, s, h, c0, c1, func);
344 }
345
346 /* The i/o request engine */
347
348 enum action {Fail = 0, Ok = 1, Hold, Wait};
349
350 static struct request *pd_req;  /* current request */
351 static enum action (*phase)(void);
352
353 static void run_fsm(void);
354
355 static void ps_tq_int(struct work_struct *work);
356
357 static DECLARE_DELAYED_WORK(fsm_tq, ps_tq_int);
358
359 static void schedule_fsm(void)
360 {
361         if (!nice)
362                 schedule_delayed_work(&fsm_tq, 0);
363         else
364                 schedule_delayed_work(&fsm_tq, nice-1);
365 }
366
367 static void ps_tq_int(struct work_struct *work)
368 {
369         run_fsm();
370 }
371
372 static enum action do_pd_io_start(void);
373 static enum action pd_special(void);
374 static enum action do_pd_read_start(void);
375 static enum action do_pd_write_start(void);
376 static enum action do_pd_read_drq(void);
377 static enum action do_pd_write_done(void);
378
379 static struct request_queue *pd_queue;
380 static int pd_claimed;
381
382 static struct pd_unit *pd_current; /* current request's drive */
383 static PIA *pi_current; /* current request's PIA */
384
385 static void run_fsm(void)
386 {
387         while (1) {
388                 enum action res;
389                 unsigned long saved_flags;
390                 int stop = 0;
391
392                 if (!phase) {
393                         pd_current = pd_req->rq_disk->private_data;
394                         pi_current = pd_current->pi;
395                         phase = do_pd_io_start;
396                 }
397
398                 switch (pd_claimed) {
399                         case 0:
400                                 pd_claimed = 1;
401                                 if (!pi_schedule_claimed(pi_current, run_fsm))
402                                         return;
403                         case 1:
404                                 pd_claimed = 2;
405                                 pi_current->proto->connect(pi_current);
406                 }
407
408                 switch(res = phase()) {
409                         case Ok: case Fail:
410                                 pi_disconnect(pi_current);
411                                 pd_claimed = 0;
412                                 phase = NULL;
413                                 spin_lock_irqsave(&pd_lock, saved_flags);
414                                 if (!__blk_end_request_cur(pd_req,
415                                                 res == Ok ? 0 : -EIO)) {
416                                         pd_req = blk_fetch_request(pd_queue);
417                                         if (!pd_req)
418                                                 stop = 1;
419                                 }
420                                 spin_unlock_irqrestore(&pd_lock, saved_flags);
421                                 if (stop)
422                                         return;
423                         case Hold:
424                                 schedule_fsm();
425                                 return;
426                         case Wait:
427                                 pi_disconnect(pi_current);
428                                 pd_claimed = 0;
429                 }
430         }
431 }
432
433 static int pd_retries = 0;      /* i/o error retry count */
434 static int pd_block;            /* address of next requested block */
435 static int pd_count;            /* number of blocks still to do */
436 static int pd_run;              /* sectors in current cluster */
437 static int pd_cmd;              /* current command READ/WRITE */
438 static char *pd_buf;            /* buffer for request in progress */
439
440 static enum action do_pd_io_start(void)
441 {
442         if (blk_special_request(pd_req)) {
443                 phase = pd_special;
444                 return pd_special();
445         }
446
447         pd_cmd = rq_data_dir(pd_req);
448         if (pd_cmd == READ || pd_cmd == WRITE) {
449                 pd_block = blk_rq_pos(pd_req);
450                 pd_count = blk_rq_cur_sectors(pd_req);
451                 if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
452                         return Fail;
453                 pd_run = blk_rq_sectors(pd_req);
454                 pd_buf = pd_req->buffer;
455                 pd_retries = 0;
456                 if (pd_cmd == READ)
457                         return do_pd_read_start();
458                 else
459                         return do_pd_write_start();
460         }
461         return Fail;
462 }
463
464 static enum action pd_special(void)
465 {
466         enum action (*func)(struct pd_unit *) = pd_req->special;
467         return func(pd_current);
468 }
469
470 static int pd_next_buf(void)
471 {
472         unsigned long saved_flags;
473
474         pd_count--;
475         pd_run--;
476         pd_buf += 512;
477         pd_block++;
478         if (!pd_run)
479                 return 1;
480         if (pd_count)
481                 return 0;
482         spin_lock_irqsave(&pd_lock, saved_flags);
483         __blk_end_request_cur(pd_req, 0);
484         pd_count = blk_rq_cur_sectors(pd_req);
485         pd_buf = pd_req->buffer;
486         spin_unlock_irqrestore(&pd_lock, saved_flags);
487         return 0;
488 }
489
490 static unsigned long pd_timeout;
491
492 static enum action do_pd_read_start(void)
493 {
494         if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
495                 if (pd_retries < PD_MAX_RETRIES) {
496                         pd_retries++;
497                         return Wait;
498                 }
499                 return Fail;
500         }
501         pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
502         phase = do_pd_read_drq;
503         pd_timeout = jiffies + PD_TMO;
504         return Hold;
505 }
506
507 static enum action do_pd_write_start(void)
508 {
509         if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
510                 if (pd_retries < PD_MAX_RETRIES) {
511                         pd_retries++;
512                         return Wait;
513                 }
514                 return Fail;
515         }
516         pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
517         while (1) {
518                 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
519                         if (pd_retries < PD_MAX_RETRIES) {
520                                 pd_retries++;
521                                 return Wait;
522                         }
523                         return Fail;
524                 }
525                 pi_write_block(pd_current->pi, pd_buf, 512);
526                 if (pd_next_buf())
527                         break;
528         }
529         phase = do_pd_write_done;
530         pd_timeout = jiffies + PD_TMO;
531         return Hold;
532 }
533
534 static inline int pd_ready(void)
535 {
536         return !(status_reg(pd_current) & STAT_BUSY);
537 }
538
539 static enum action do_pd_read_drq(void)
540 {
541         if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
542                 return Hold;
543
544         while (1) {
545                 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
546                         if (pd_retries < PD_MAX_RETRIES) {
547                                 pd_retries++;
548                                 phase = do_pd_read_start;
549                                 return Wait;
550                         }
551                         return Fail;
552                 }
553                 pi_read_block(pd_current->pi, pd_buf, 512);
554                 if (pd_next_buf())
555                         break;
556         }
557         return Ok;
558 }
559
560 static enum action do_pd_write_done(void)
561 {
562         if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
563                 return Hold;
564
565         if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
566                 if (pd_retries < PD_MAX_RETRIES) {
567                         pd_retries++;
568                         phase = do_pd_write_start;
569                         return Wait;
570                 }
571                 return Fail;
572         }
573         return Ok;
574 }
575
576 /* special io requests */
577
578 /* According to the ATA standard, the default CHS geometry should be
579    available following a reset.  Some Western Digital drives come up
580    in a mode where only LBA addresses are accepted until the device
581    parameters are initialised.
582 */
583
584 static void pd_init_dev_parms(struct pd_unit *disk)
585 {
586         pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
587         pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
588                         IDE_INIT_DEV_PARMS);
589         udelay(300);
590         pd_wait_for(disk, 0, "Initialise device parameters");
591 }
592
593 static enum action pd_door_lock(struct pd_unit *disk)
594 {
595         if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
596                 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
597                 pd_wait_for(disk, STAT_READY, "Lock done");
598         }
599         return Ok;
600 }
601
602 static enum action pd_door_unlock(struct pd_unit *disk)
603 {
604         if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
605                 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
606                 pd_wait_for(disk, STAT_READY, "Lock done");
607         }
608         return Ok;
609 }
610
611 static enum action pd_eject(struct pd_unit *disk)
612 {
613         pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
614         pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
615         pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
616         pd_wait_for(disk, 0, DBMSG("before eject"));
617         pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
618         pd_wait_for(disk, 0, DBMSG("after eject"));
619         return Ok;
620 }
621
622 static enum action pd_media_check(struct pd_unit *disk)
623 {
624         int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
625         if (!(r & STAT_ERR)) {
626                 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
627                 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
628         } else
629                 disk->changed = 1;      /* say changed if other error */
630         if (r & ERR_MC) {
631                 disk->changed = 1;
632                 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
633                 pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
634                 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
635                 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
636         }
637         return Ok;
638 }
639
640 static void pd_standby_off(struct pd_unit *disk)
641 {
642         pd_wait_for(disk, 0, DBMSG("before STANDBY"));
643         pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
644         pd_wait_for(disk, 0, DBMSG("after STANDBY"));
645 }
646
647 static enum action pd_identify(struct pd_unit *disk)
648 {
649         int j;
650         char id[PD_ID_LEN + 1];
651
652 /* WARNING:  here there may be dragons.  reset() applies to both drives,
653    but we call it only on probing the MASTER. This should allow most
654    common configurations to work, but be warned that a reset can clear
655    settings on the SLAVE drive.
656 */
657
658         if (disk->drive == 0)
659                 pd_reset(disk);
660
661         write_reg(disk, 6, DRIVE(disk));
662         pd_wait_for(disk, 0, DBMSG("before IDENT"));
663         pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
664
665         if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
666                 return Fail;
667         pi_read_block(disk->pi, pd_scratch, 512);
668         disk->can_lba = pd_scratch[99] & 2;
669         disk->sectors = le16_to_cpu(*(__le16 *) (pd_scratch + 12));
670         disk->heads = le16_to_cpu(*(__le16 *) (pd_scratch + 6));
671         disk->cylinders = le16_to_cpu(*(__le16 *) (pd_scratch + 2));
672         if (disk->can_lba)
673                 disk->capacity = le32_to_cpu(*(__le32 *) (pd_scratch + 120));
674         else
675                 disk->capacity = disk->sectors * disk->heads * disk->cylinders;
676
677         for (j = 0; j < PD_ID_LEN; j++)
678                 id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
679         j = PD_ID_LEN - 1;
680         while ((j >= 0) && (id[j] <= 0x20))
681                 j--;
682         j++;
683         id[j] = 0;
684
685         disk->removable = pd_scratch[0] & 0x80;
686
687         printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
688                disk->name, id,
689                disk->drive ? "slave" : "master",
690                disk->capacity, disk->capacity / 2048,
691                disk->cylinders, disk->heads, disk->sectors,
692                disk->removable ? "removable" : "fixed");
693
694         if (disk->capacity)
695                 pd_init_dev_parms(disk);
696         if (!disk->standby)
697                 pd_standby_off(disk);
698
699         return Ok;
700 }
701
702 /* end of io request engine */
703
704 static void do_pd_request(struct request_queue * q)
705 {
706         if (pd_req)
707                 return;
708         pd_req = blk_fetch_request(q);
709         if (!pd_req)
710                 return;
711
712         schedule_fsm();
713 }
714
715 static int pd_special_command(struct pd_unit *disk,
716                       enum action (*func)(struct pd_unit *disk))
717 {
718         struct request *rq;
719         int err = 0;
720
721         rq = blk_get_request(disk->gd->queue, READ, __GFP_WAIT);
722
723         rq->cmd_type = REQ_TYPE_SPECIAL;
724         rq->special = func;
725
726         err = blk_execute_rq(disk->gd->queue, disk->gd, rq, 0);
727
728         blk_put_request(rq);
729         return err;
730 }
731
732 /* kernel glue structures */
733
734 static int pd_open(struct block_device *bdev, fmode_t mode)
735 {
736         struct pd_unit *disk = bdev->bd_disk->private_data;
737
738         disk->access++;
739
740         if (disk->removable) {
741                 pd_special_command(disk, pd_media_check);
742                 pd_special_command(disk, pd_door_lock);
743         }
744         return 0;
745 }
746
747 static int pd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
748 {
749         struct pd_unit *disk = bdev->bd_disk->private_data;
750
751         if (disk->alt_geom) {
752                 geo->heads = PD_LOG_HEADS;
753                 geo->sectors = PD_LOG_SECTS;
754                 geo->cylinders = disk->capacity / (geo->heads * geo->sectors);
755         } else {
756                 geo->heads = disk->heads;
757                 geo->sectors = disk->sectors;
758                 geo->cylinders = disk->cylinders;
759         }
760
761         return 0;
762 }
763
764 static int pd_ioctl(struct block_device *bdev, fmode_t mode,
765          unsigned int cmd, unsigned long arg)
766 {
767         struct pd_unit *disk = bdev->bd_disk->private_data;
768
769         switch (cmd) {
770         case CDROMEJECT:
771                 if (disk->access == 1)
772                         pd_special_command(disk, pd_eject);
773                 return 0;
774         default:
775                 return -EINVAL;
776         }
777 }
778
779 static int pd_release(struct gendisk *p, fmode_t mode)
780 {
781         struct pd_unit *disk = p->private_data;
782
783         if (!--disk->access && disk->removable)
784                 pd_special_command(disk, pd_door_unlock);
785
786         return 0;
787 }
788
789 static int pd_check_media(struct gendisk *p)
790 {
791         struct pd_unit *disk = p->private_data;
792         int r;
793         if (!disk->removable)
794                 return 0;
795         pd_special_command(disk, pd_media_check);
796         r = disk->changed;
797         disk->changed = 0;
798         return r;
799 }
800
801 static int pd_revalidate(struct gendisk *p)
802 {
803         struct pd_unit *disk = p->private_data;
804         if (pd_special_command(disk, pd_identify) == 0)
805                 set_capacity(p, disk->capacity);
806         else
807                 set_capacity(p, 0);
808         return 0;
809 }
810
811 static const struct block_device_operations pd_fops = {
812         .owner          = THIS_MODULE,
813         .open           = pd_open,
814         .release        = pd_release,
815         .locked_ioctl   = pd_ioctl,
816         .getgeo         = pd_getgeo,
817         .media_changed  = pd_check_media,
818         .revalidate_disk= pd_revalidate
819 };
820
821 /* probing */
822
823 static void pd_probe_drive(struct pd_unit *disk)
824 {
825         struct gendisk *p = alloc_disk(1 << PD_BITS);
826         if (!p)
827                 return;
828         strcpy(p->disk_name, disk->name);
829         p->fops = &pd_fops;
830         p->major = major;
831         p->first_minor = (disk - pd) << PD_BITS;
832         disk->gd = p;
833         p->private_data = disk;
834         p->queue = pd_queue;
835
836         if (disk->drive == -1) {
837                 for (disk->drive = 0; disk->drive <= 1; disk->drive++)
838                         if (pd_special_command(disk, pd_identify) == 0)
839                                 return;
840         } else if (pd_special_command(disk, pd_identify) == 0)
841                 return;
842         disk->gd = NULL;
843         put_disk(p);
844 }
845
846 static int pd_detect(void)
847 {
848         int found = 0, unit, pd_drive_count = 0;
849         struct pd_unit *disk;
850
851         for (unit = 0; unit < PD_UNITS; unit++) {
852                 int *parm = *drives[unit];
853                 struct pd_unit *disk = pd + unit;
854                 disk->pi = &disk->pia;
855                 disk->access = 0;
856                 disk->changed = 1;
857                 disk->capacity = 0;
858                 disk->drive = parm[D_SLV];
859                 snprintf(disk->name, PD_NAMELEN, "%s%c", name, 'a'+unit);
860                 disk->alt_geom = parm[D_GEO];
861                 disk->standby = parm[D_SBY];
862                 if (parm[D_PRT])
863                         pd_drive_count++;
864         }
865
866         if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
867                 disk = pd;
868                 if (pi_init(disk->pi, 1, -1, -1, -1, -1, -1, pd_scratch,
869                             PI_PD, verbose, disk->name)) {
870                         pd_probe_drive(disk);
871                         if (!disk->gd)
872                                 pi_release(disk->pi);
873                 }
874
875         } else {
876                 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
877                         int *parm = *drives[unit];
878                         if (!parm[D_PRT])
879                                 continue;
880                         if (pi_init(disk->pi, 0, parm[D_PRT], parm[D_MOD],
881                                      parm[D_UNI], parm[D_PRO], parm[D_DLY],
882                                      pd_scratch, PI_PD, verbose, disk->name)) {
883                                 pd_probe_drive(disk);
884                                 if (!disk->gd)
885                                         pi_release(disk->pi);
886                         }
887                 }
888         }
889         for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
890                 if (disk->gd) {
891                         set_capacity(disk->gd, disk->capacity);
892                         add_disk(disk->gd);
893                         found = 1;
894                 }
895         }
896         if (!found)
897                 printk("%s: no valid drive found\n", name);
898         return found;
899 }
900
901 static int __init pd_init(void)
902 {
903         if (disable)
904                 goto out1;
905
906         pd_queue = blk_init_queue(do_pd_request, &pd_lock);
907         if (!pd_queue)
908                 goto out1;
909
910         blk_queue_max_hw_sectors(pd_queue, cluster);
911
912         if (register_blkdev(major, name))
913                 goto out2;
914
915         printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
916                name, name, PD_VERSION, major, cluster, nice);
917         if (!pd_detect())
918                 goto out3;
919
920         return 0;
921
922 out3:
923         unregister_blkdev(major, name);
924 out2:
925         blk_cleanup_queue(pd_queue);
926 out1:
927         return -ENODEV;
928 }
929
930 static void __exit pd_exit(void)
931 {
932         struct pd_unit *disk;
933         int unit;
934         unregister_blkdev(major, name);
935         for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
936                 struct gendisk *p = disk->gd;
937                 if (p) {
938                         disk->gd = NULL;
939                         del_gendisk(p);
940                         put_disk(p);
941                         pi_release(disk->pi);
942                 }
943         }
944         blk_cleanup_queue(pd_queue);
945 }
946
947 MODULE_LICENSE("GPL");
948 module_init(pd_init)
949 module_exit(pd_exit)