]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/block/paride/pd.c
block: don't set bounce limit in blk_init_queue
[karo-tx-linux.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 override 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 #include <linux/types.h>
128
129 static int verbose = 0;
130 static int major = PD_MAJOR;
131 static char *name = PD_NAME;
132 static int cluster = 64;
133 static int nice = 0;
134 static int disable = 0;
135
136 static int drive0[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
137 static int drive1[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
138 static int drive2[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
139 static int drive3[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
140
141 static int (*drives[4])[8] = {&drive0, &drive1, &drive2, &drive3};
142
143 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
144
145 /* end of parameters */
146
147 #include <linux/init.h>
148 #include <linux/module.h>
149 #include <linux/gfp.h>
150 #include <linux/fs.h>
151 #include <linux/delay.h>
152 #include <linux/hdreg.h>
153 #include <linux/cdrom.h>        /* for the eject ioctl */
154 #include <linux/blkdev.h>
155 #include <linux/blkpg.h>
156 #include <linux/kernel.h>
157 #include <linux/mutex.h>
158 #include <linux/uaccess.h>
159 #include <linux/workqueue.h>
160
161 static DEFINE_MUTEX(pd_mutex);
162 static DEFINE_SPINLOCK(pd_lock);
163
164 module_param(verbose, int, 0);
165 module_param(major, int, 0);
166 module_param(name, charp, 0);
167 module_param(cluster, int, 0);
168 module_param(nice, int, 0);
169 module_param_array(drive0, int, NULL, 0);
170 module_param_array(drive1, int, NULL, 0);
171 module_param_array(drive2, int, NULL, 0);
172 module_param_array(drive3, int, NULL, 0);
173
174 #include "paride.h"
175
176 #define PD_BITS    4
177
178 /* numbers for "SCSI" geometry */
179
180 #define PD_LOG_HEADS    64
181 #define PD_LOG_SECTS    32
182
183 #define PD_ID_OFF       54
184 #define PD_ID_LEN       14
185
186 #define PD_MAX_RETRIES  5
187 #define PD_TMO          800     /* interrupt timeout in jiffies */
188 #define PD_SPIN_DEL     50      /* spin delay in micro-seconds  */
189
190 #define PD_SPIN         (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
191
192 #define STAT_ERR        0x00001
193 #define STAT_INDEX      0x00002
194 #define STAT_ECC        0x00004
195 #define STAT_DRQ        0x00008
196 #define STAT_SEEK       0x00010
197 #define STAT_WRERR      0x00020
198 #define STAT_READY      0x00040
199 #define STAT_BUSY       0x00080
200
201 #define ERR_AMNF        0x00100
202 #define ERR_TK0NF       0x00200
203 #define ERR_ABRT        0x00400
204 #define ERR_MCR         0x00800
205 #define ERR_IDNF        0x01000
206 #define ERR_MC          0x02000
207 #define ERR_UNC         0x04000
208 #define ERR_TMO         0x10000
209
210 #define IDE_READ                0x20
211 #define IDE_WRITE               0x30
212 #define IDE_READ_VRFY           0x40
213 #define IDE_INIT_DEV_PARMS      0x91
214 #define IDE_STANDBY             0x96
215 #define IDE_ACKCHANGE           0xdb
216 #define IDE_DOORLOCK            0xde
217 #define IDE_DOORUNLOCK          0xdf
218 #define IDE_IDENTIFY            0xec
219 #define IDE_EJECT               0xed
220
221 #define PD_NAMELEN      8
222
223 struct pd_unit {
224         struct pi_adapter pia;  /* interface to paride layer */
225         struct pi_adapter *pi;
226         int access;             /* count of active opens ... */
227         int capacity;           /* Size of this volume in sectors */
228         int heads;              /* physical geometry */
229         int sectors;
230         int cylinders;
231         int can_lba;
232         int drive;              /* master=0 slave=1 */
233         int changed;            /* Have we seen a disk change ? */
234         int removable;          /* removable media device  ?  */
235         int standby;
236         int alt_geom;
237         char name[PD_NAMELEN];  /* pda, pdb, etc ... */
238         struct gendisk *gd;
239 };
240
241 static struct pd_unit pd[PD_UNITS];
242
243 static char pd_scratch[512];    /* scratch block buffer */
244
245 static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
246         "READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
247         "IDNF", "MC", "UNC", "???", "TMO"
248 };
249
250 static void *par_drv;           /* reference of parport driver */
251
252 static inline int status_reg(struct pd_unit *disk)
253 {
254         return pi_read_regr(disk->pi, 1, 6);
255 }
256
257 static inline int read_reg(struct pd_unit *disk, int reg)
258 {
259         return pi_read_regr(disk->pi, 0, reg);
260 }
261
262 static inline void write_status(struct pd_unit *disk, int val)
263 {
264         pi_write_regr(disk->pi, 1, 6, val);
265 }
266
267 static inline void write_reg(struct pd_unit *disk, int reg, int val)
268 {
269         pi_write_regr(disk->pi, 0, reg, val);
270 }
271
272 static inline u8 DRIVE(struct pd_unit *disk)
273 {
274         return 0xa0+0x10*disk->drive;
275 }
276
277 /*  ide command interface */
278
279 static void pd_print_error(struct pd_unit *disk, char *msg, int status)
280 {
281         int i;
282
283         printk("%s: %s: status = 0x%x =", disk->name, msg, status);
284         for (i = 0; i < ARRAY_SIZE(pd_errs); i++)
285                 if (status & (1 << i))
286                         printk(" %s", pd_errs[i]);
287         printk("\n");
288 }
289
290 static void pd_reset(struct pd_unit *disk)
291 {                               /* called only for MASTER drive */
292         write_status(disk, 4);
293         udelay(50);
294         write_status(disk, 0);
295         udelay(250);
296 }
297
298 #define DBMSG(msg)      ((verbose>1)?(msg):NULL)
299
300 static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
301 {                               /* polled wait */
302         int k, r, e;
303
304         k = 0;
305         while (k < PD_SPIN) {
306                 r = status_reg(disk);
307                 k++;
308                 if (((r & w) == w) && !(r & STAT_BUSY))
309                         break;
310                 udelay(PD_SPIN_DEL);
311         }
312         e = (read_reg(disk, 1) << 8) + read_reg(disk, 7);
313         if (k >= PD_SPIN)
314                 e |= ERR_TMO;
315         if ((e & (STAT_ERR | ERR_TMO)) && (msg != NULL))
316                 pd_print_error(disk, msg, e);
317         return e;
318 }
319
320 static void pd_send_command(struct pd_unit *disk, int n, int s, int h, int c0, int c1, int func)
321 {
322         write_reg(disk, 6, DRIVE(disk) + h);
323         write_reg(disk, 1, 0);          /* the IDE task file */
324         write_reg(disk, 2, n);
325         write_reg(disk, 3, s);
326         write_reg(disk, 4, c0);
327         write_reg(disk, 5, c1);
328         write_reg(disk, 7, func);
329
330         udelay(1);
331 }
332
333 static void pd_ide_command(struct pd_unit *disk, int func, int block, int count)
334 {
335         int c1, c0, h, s;
336
337         if (disk->can_lba) {
338                 s = block & 255;
339                 c0 = (block >>= 8) & 255;
340                 c1 = (block >>= 8) & 255;
341                 h = ((block >>= 8) & 15) + 0x40;
342         } else {
343                 s = (block % disk->sectors) + 1;
344                 h = (block /= disk->sectors) % disk->heads;
345                 c0 = (block /= disk->heads) % 256;
346                 c1 = (block >>= 8);
347         }
348         pd_send_command(disk, count, s, h, c0, c1, func);
349 }
350
351 /* The i/o request engine */
352
353 enum action {Fail = 0, Ok = 1, Hold, Wait};
354
355 static struct request *pd_req;  /* current request */
356 static enum action (*phase)(void);
357
358 static void run_fsm(void);
359
360 static void ps_tq_int(struct work_struct *work);
361
362 static DECLARE_DELAYED_WORK(fsm_tq, ps_tq_int);
363
364 static void schedule_fsm(void)
365 {
366         if (!nice)
367                 schedule_delayed_work(&fsm_tq, 0);
368         else
369                 schedule_delayed_work(&fsm_tq, nice-1);
370 }
371
372 static void ps_tq_int(struct work_struct *work)
373 {
374         run_fsm();
375 }
376
377 static enum action do_pd_io_start(void);
378 static enum action pd_special(void);
379 static enum action do_pd_read_start(void);
380 static enum action do_pd_write_start(void);
381 static enum action do_pd_read_drq(void);
382 static enum action do_pd_write_done(void);
383
384 static int pd_queue;
385 static int pd_claimed;
386
387 static struct pd_unit *pd_current; /* current request's drive */
388 static PIA *pi_current; /* current request's PIA */
389
390 static int set_next_request(void)
391 {
392         struct gendisk *disk;
393         struct request_queue *q;
394         int old_pos = pd_queue;
395
396         do {
397                 disk = pd[pd_queue].gd;
398                 q = disk ? disk->queue : NULL;
399                 if (++pd_queue == PD_UNITS)
400                         pd_queue = 0;
401                 if (q) {
402                         pd_req = blk_fetch_request(q);
403                         if (pd_req)
404                                 break;
405                 }
406         } while (pd_queue != old_pos);
407
408         return pd_req != NULL;
409 }
410
411 static void run_fsm(void)
412 {
413         while (1) {
414                 enum action res;
415                 unsigned long saved_flags;
416                 int stop = 0;
417
418                 if (!phase) {
419                         pd_current = pd_req->rq_disk->private_data;
420                         pi_current = pd_current->pi;
421                         phase = do_pd_io_start;
422                 }
423
424                 switch (pd_claimed) {
425                         case 0:
426                                 pd_claimed = 1;
427                                 if (!pi_schedule_claimed(pi_current, run_fsm))
428                                         return;
429                         case 1:
430                                 pd_claimed = 2;
431                                 pi_current->proto->connect(pi_current);
432                 }
433
434                 switch(res = phase()) {
435                         case Ok: case Fail:
436                                 pi_disconnect(pi_current);
437                                 pd_claimed = 0;
438                                 phase = NULL;
439                                 spin_lock_irqsave(&pd_lock, saved_flags);
440                                 if (!__blk_end_request_cur(pd_req,
441                                                 res == Ok ? 0 : BLK_STS_IOERR)) {
442                                         if (!set_next_request())
443                                                 stop = 1;
444                                 }
445                                 spin_unlock_irqrestore(&pd_lock, saved_flags);
446                                 if (stop)
447                                         return;
448                         case Hold:
449                                 schedule_fsm();
450                                 return;
451                         case Wait:
452                                 pi_disconnect(pi_current);
453                                 pd_claimed = 0;
454                 }
455         }
456 }
457
458 static int pd_retries = 0;      /* i/o error retry count */
459 static int pd_block;            /* address of next requested block */
460 static int pd_count;            /* number of blocks still to do */
461 static int pd_run;              /* sectors in current cluster */
462 static char *pd_buf;            /* buffer for request in progress */
463
464 static enum action do_pd_io_start(void)
465 {
466         switch (req_op(pd_req)) {
467         case REQ_OP_DRV_IN:
468                 phase = pd_special;
469                 return pd_special();
470         case REQ_OP_READ:
471         case REQ_OP_WRITE:
472                 pd_block = blk_rq_pos(pd_req);
473                 pd_count = blk_rq_cur_sectors(pd_req);
474                 if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
475                         return Fail;
476                 pd_run = blk_rq_sectors(pd_req);
477                 pd_buf = bio_data(pd_req->bio);
478                 pd_retries = 0;
479                 if (req_op(pd_req) == REQ_OP_READ)
480                         return do_pd_read_start();
481                 else
482                         return do_pd_write_start();
483         }
484         return Fail;
485 }
486
487 static enum action pd_special(void)
488 {
489         enum action (*func)(struct pd_unit *) = pd_req->special;
490         return func(pd_current);
491 }
492
493 static int pd_next_buf(void)
494 {
495         unsigned long saved_flags;
496
497         pd_count--;
498         pd_run--;
499         pd_buf += 512;
500         pd_block++;
501         if (!pd_run)
502                 return 1;
503         if (pd_count)
504                 return 0;
505         spin_lock_irqsave(&pd_lock, saved_flags);
506         __blk_end_request_cur(pd_req, 0);
507         pd_count = blk_rq_cur_sectors(pd_req);
508         pd_buf = bio_data(pd_req->bio);
509         spin_unlock_irqrestore(&pd_lock, saved_flags);
510         return 0;
511 }
512
513 static unsigned long pd_timeout;
514
515 static enum action do_pd_read_start(void)
516 {
517         if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
518                 if (pd_retries < PD_MAX_RETRIES) {
519                         pd_retries++;
520                         return Wait;
521                 }
522                 return Fail;
523         }
524         pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
525         phase = do_pd_read_drq;
526         pd_timeout = jiffies + PD_TMO;
527         return Hold;
528 }
529
530 static enum action do_pd_write_start(void)
531 {
532         if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
533                 if (pd_retries < PD_MAX_RETRIES) {
534                         pd_retries++;
535                         return Wait;
536                 }
537                 return Fail;
538         }
539         pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
540         while (1) {
541                 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
542                         if (pd_retries < PD_MAX_RETRIES) {
543                                 pd_retries++;
544                                 return Wait;
545                         }
546                         return Fail;
547                 }
548                 pi_write_block(pd_current->pi, pd_buf, 512);
549                 if (pd_next_buf())
550                         break;
551         }
552         phase = do_pd_write_done;
553         pd_timeout = jiffies + PD_TMO;
554         return Hold;
555 }
556
557 static inline int pd_ready(void)
558 {
559         return !(status_reg(pd_current) & STAT_BUSY);
560 }
561
562 static enum action do_pd_read_drq(void)
563 {
564         if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
565                 return Hold;
566
567         while (1) {
568                 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
569                         if (pd_retries < PD_MAX_RETRIES) {
570                                 pd_retries++;
571                                 phase = do_pd_read_start;
572                                 return Wait;
573                         }
574                         return Fail;
575                 }
576                 pi_read_block(pd_current->pi, pd_buf, 512);
577                 if (pd_next_buf())
578                         break;
579         }
580         return Ok;
581 }
582
583 static enum action do_pd_write_done(void)
584 {
585         if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
586                 return Hold;
587
588         if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
589                 if (pd_retries < PD_MAX_RETRIES) {
590                         pd_retries++;
591                         phase = do_pd_write_start;
592                         return Wait;
593                 }
594                 return Fail;
595         }
596         return Ok;
597 }
598
599 /* special io requests */
600
601 /* According to the ATA standard, the default CHS geometry should be
602    available following a reset.  Some Western Digital drives come up
603    in a mode where only LBA addresses are accepted until the device
604    parameters are initialised.
605 */
606
607 static void pd_init_dev_parms(struct pd_unit *disk)
608 {
609         pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
610         pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
611                         IDE_INIT_DEV_PARMS);
612         udelay(300);
613         pd_wait_for(disk, 0, "Initialise device parameters");
614 }
615
616 static enum action pd_door_lock(struct pd_unit *disk)
617 {
618         if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
619                 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
620                 pd_wait_for(disk, STAT_READY, "Lock done");
621         }
622         return Ok;
623 }
624
625 static enum action pd_door_unlock(struct pd_unit *disk)
626 {
627         if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
628                 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
629                 pd_wait_for(disk, STAT_READY, "Lock done");
630         }
631         return Ok;
632 }
633
634 static enum action pd_eject(struct pd_unit *disk)
635 {
636         pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
637         pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
638         pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
639         pd_wait_for(disk, 0, DBMSG("before eject"));
640         pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
641         pd_wait_for(disk, 0, DBMSG("after eject"));
642         return Ok;
643 }
644
645 static enum action pd_media_check(struct pd_unit *disk)
646 {
647         int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
648         if (!(r & STAT_ERR)) {
649                 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
650                 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
651         } else
652                 disk->changed = 1;      /* say changed if other error */
653         if (r & ERR_MC) {
654                 disk->changed = 1;
655                 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
656                 pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
657                 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
658                 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
659         }
660         return Ok;
661 }
662
663 static void pd_standby_off(struct pd_unit *disk)
664 {
665         pd_wait_for(disk, 0, DBMSG("before STANDBY"));
666         pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
667         pd_wait_for(disk, 0, DBMSG("after STANDBY"));
668 }
669
670 static enum action pd_identify(struct pd_unit *disk)
671 {
672         int j;
673         char id[PD_ID_LEN + 1];
674
675 /* WARNING:  here there may be dragons.  reset() applies to both drives,
676    but we call it only on probing the MASTER. This should allow most
677    common configurations to work, but be warned that a reset can clear
678    settings on the SLAVE drive.
679 */
680
681         if (disk->drive == 0)
682                 pd_reset(disk);
683
684         write_reg(disk, 6, DRIVE(disk));
685         pd_wait_for(disk, 0, DBMSG("before IDENT"));
686         pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
687
688         if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
689                 return Fail;
690         pi_read_block(disk->pi, pd_scratch, 512);
691         disk->can_lba = pd_scratch[99] & 2;
692         disk->sectors = le16_to_cpu(*(__le16 *) (pd_scratch + 12));
693         disk->heads = le16_to_cpu(*(__le16 *) (pd_scratch + 6));
694         disk->cylinders = le16_to_cpu(*(__le16 *) (pd_scratch + 2));
695         if (disk->can_lba)
696                 disk->capacity = le32_to_cpu(*(__le32 *) (pd_scratch + 120));
697         else
698                 disk->capacity = disk->sectors * disk->heads * disk->cylinders;
699
700         for (j = 0; j < PD_ID_LEN; j++)
701                 id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
702         j = PD_ID_LEN - 1;
703         while ((j >= 0) && (id[j] <= 0x20))
704                 j--;
705         j++;
706         id[j] = 0;
707
708         disk->removable = pd_scratch[0] & 0x80;
709
710         printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
711                disk->name, id,
712                disk->drive ? "slave" : "master",
713                disk->capacity, disk->capacity / 2048,
714                disk->cylinders, disk->heads, disk->sectors,
715                disk->removable ? "removable" : "fixed");
716
717         if (disk->capacity)
718                 pd_init_dev_parms(disk);
719         if (!disk->standby)
720                 pd_standby_off(disk);
721
722         return Ok;
723 }
724
725 /* end of io request engine */
726
727 static void do_pd_request(struct request_queue * q)
728 {
729         if (pd_req)
730                 return;
731         pd_req = blk_fetch_request(q);
732         if (!pd_req)
733                 return;
734
735         schedule_fsm();
736 }
737
738 static int pd_special_command(struct pd_unit *disk,
739                       enum action (*func)(struct pd_unit *disk))
740 {
741         struct request *rq;
742
743         rq = blk_get_request(disk->gd->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
744         if (IS_ERR(rq))
745                 return PTR_ERR(rq);
746
747         rq->special = func;
748         blk_execute_rq(disk->gd->queue, disk->gd, rq, 0);
749         blk_put_request(rq);
750         return 0;
751 }
752
753 /* kernel glue structures */
754
755 static int pd_open(struct block_device *bdev, fmode_t mode)
756 {
757         struct pd_unit *disk = bdev->bd_disk->private_data;
758
759         mutex_lock(&pd_mutex);
760         disk->access++;
761
762         if (disk->removable) {
763                 pd_special_command(disk, pd_media_check);
764                 pd_special_command(disk, pd_door_lock);
765         }
766         mutex_unlock(&pd_mutex);
767         return 0;
768 }
769
770 static int pd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
771 {
772         struct pd_unit *disk = bdev->bd_disk->private_data;
773
774         if (disk->alt_geom) {
775                 geo->heads = PD_LOG_HEADS;
776                 geo->sectors = PD_LOG_SECTS;
777                 geo->cylinders = disk->capacity / (geo->heads * geo->sectors);
778         } else {
779                 geo->heads = disk->heads;
780                 geo->sectors = disk->sectors;
781                 geo->cylinders = disk->cylinders;
782         }
783
784         return 0;
785 }
786
787 static int pd_ioctl(struct block_device *bdev, fmode_t mode,
788          unsigned int cmd, unsigned long arg)
789 {
790         struct pd_unit *disk = bdev->bd_disk->private_data;
791
792         switch (cmd) {
793         case CDROMEJECT:
794                 mutex_lock(&pd_mutex);
795                 if (disk->access == 1)
796                         pd_special_command(disk, pd_eject);
797                 mutex_unlock(&pd_mutex);
798                 return 0;
799         default:
800                 return -EINVAL;
801         }
802 }
803
804 static void pd_release(struct gendisk *p, fmode_t mode)
805 {
806         struct pd_unit *disk = p->private_data;
807
808         mutex_lock(&pd_mutex);
809         if (!--disk->access && disk->removable)
810                 pd_special_command(disk, pd_door_unlock);
811         mutex_unlock(&pd_mutex);
812 }
813
814 static unsigned int pd_check_events(struct gendisk *p, unsigned int clearing)
815 {
816         struct pd_unit *disk = p->private_data;
817         int r;
818         if (!disk->removable)
819                 return 0;
820         pd_special_command(disk, pd_media_check);
821         r = disk->changed;
822         disk->changed = 0;
823         return r ? DISK_EVENT_MEDIA_CHANGE : 0;
824 }
825
826 static int pd_revalidate(struct gendisk *p)
827 {
828         struct pd_unit *disk = p->private_data;
829         if (pd_special_command(disk, pd_identify) == 0)
830                 set_capacity(p, disk->capacity);
831         else
832                 set_capacity(p, 0);
833         return 0;
834 }
835
836 static const struct block_device_operations pd_fops = {
837         .owner          = THIS_MODULE,
838         .open           = pd_open,
839         .release        = pd_release,
840         .ioctl          = pd_ioctl,
841         .getgeo         = pd_getgeo,
842         .check_events   = pd_check_events,
843         .revalidate_disk= pd_revalidate
844 };
845
846 /* probing */
847
848 static void pd_probe_drive(struct pd_unit *disk)
849 {
850         struct gendisk *p = alloc_disk(1 << PD_BITS);
851         if (!p)
852                 return;
853         strcpy(p->disk_name, disk->name);
854         p->fops = &pd_fops;
855         p->major = major;
856         p->first_minor = (disk - pd) << PD_BITS;
857         disk->gd = p;
858         p->private_data = disk;
859         p->queue = blk_init_queue(do_pd_request, &pd_lock);
860         if (!p->queue) {
861                 disk->gd = NULL;
862                 put_disk(p);
863                 return;
864         }
865         blk_queue_max_hw_sectors(p->queue, cluster);
866         blk_queue_bounce_limit(p->queue, BLK_BOUNCE_HIGH);
867
868         if (disk->drive == -1) {
869                 for (disk->drive = 0; disk->drive <= 1; disk->drive++)
870                         if (pd_special_command(disk, pd_identify) == 0)
871                                 return;
872         } else if (pd_special_command(disk, pd_identify) == 0)
873                 return;
874         disk->gd = NULL;
875         put_disk(p);
876 }
877
878 static int pd_detect(void)
879 {
880         int found = 0, unit, pd_drive_count = 0;
881         struct pd_unit *disk;
882
883         for (unit = 0; unit < PD_UNITS; unit++) {
884                 int *parm = *drives[unit];
885                 struct pd_unit *disk = pd + unit;
886                 disk->pi = &disk->pia;
887                 disk->access = 0;
888                 disk->changed = 1;
889                 disk->capacity = 0;
890                 disk->drive = parm[D_SLV];
891                 snprintf(disk->name, PD_NAMELEN, "%s%c", name, 'a'+unit);
892                 disk->alt_geom = parm[D_GEO];
893                 disk->standby = parm[D_SBY];
894                 if (parm[D_PRT])
895                         pd_drive_count++;
896         }
897
898         par_drv = pi_register_driver(name);
899         if (!par_drv) {
900                 pr_err("failed to register %s driver\n", name);
901                 return -1;
902         }
903
904         if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
905                 disk = pd;
906                 if (pi_init(disk->pi, 1, -1, -1, -1, -1, -1, pd_scratch,
907                             PI_PD, verbose, disk->name)) {
908                         pd_probe_drive(disk);
909                         if (!disk->gd)
910                                 pi_release(disk->pi);
911                 }
912
913         } else {
914                 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
915                         int *parm = *drives[unit];
916                         if (!parm[D_PRT])
917                                 continue;
918                         if (pi_init(disk->pi, 0, parm[D_PRT], parm[D_MOD],
919                                      parm[D_UNI], parm[D_PRO], parm[D_DLY],
920                                      pd_scratch, PI_PD, verbose, disk->name)) {
921                                 pd_probe_drive(disk);
922                                 if (!disk->gd)
923                                         pi_release(disk->pi);
924                         }
925                 }
926         }
927         for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
928                 if (disk->gd) {
929                         set_capacity(disk->gd, disk->capacity);
930                         add_disk(disk->gd);
931                         found = 1;
932                 }
933         }
934         if (!found) {
935                 printk("%s: no valid drive found\n", name);
936                 pi_unregister_driver(par_drv);
937         }
938         return found;
939 }
940
941 static int __init pd_init(void)
942 {
943         if (disable)
944                 goto out1;
945
946         if (register_blkdev(major, name))
947                 goto out1;
948
949         printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
950                name, name, PD_VERSION, major, cluster, nice);
951         if (!pd_detect())
952                 goto out2;
953
954         return 0;
955
956 out2:
957         unregister_blkdev(major, name);
958 out1:
959         return -ENODEV;
960 }
961
962 static void __exit pd_exit(void)
963 {
964         struct pd_unit *disk;
965         int unit;
966         unregister_blkdev(major, name);
967         for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
968                 struct gendisk *p = disk->gd;
969                 if (p) {
970                         disk->gd = NULL;
971                         del_gendisk(p);
972                         blk_cleanup_queue(p->queue);
973                         put_disk(p);
974                         pi_release(disk->pi);
975                 }
976         }
977 }
978
979 MODULE_LICENSE("GPL");
980 module_init(pd_init)
981 module_exit(pd_exit)