Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / paride / pd.c
blob202a5a74ad37ba6e3aee1edcac7ffe1c0482e4cf
1 /*
2 pd.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU General Public License.
5 This is the high-level driver for parallel port IDE hard
6 drives based on chips supported by the paride module.
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.
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:
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>
21 Where,
23 <prt> is the base of the parallel port address for
24 the corresponding drive. (required)
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)
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)
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)
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)
48 <sby> set this to zero to disable the power saving
49 standby mode, if needed. (1 if not given)
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)
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.
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.
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")
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)
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)
86 nice This parameter controls the driver's use of
87 idle CPU time, at the expense of some speed.
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:
93 pd.drive0
94 pd.drive1
95 pd.drive2
96 pd.drive3
97 pd.cluster
98 pd.nice
100 In addition, you can use the parameter pd.disable to disable
101 the driver entirely.
105 /* Changes:
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
117 #define PD_VERSION "1.05"
118 #define PD_MAJOR 45
119 #define PD_NAME "pd"
120 #define PD_UNITS 4
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.
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;
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 };
140 static int (*drives[4])[8] = {&drive0, &drive1, &drive2, &drive3};
142 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
144 /* end of parameters */
146 #include <linux/init.h>
147 #include <linux/module.h>
148 #include <linux/fs.h>
149 #include <linux/delay.h>
150 #include <linux/hdreg.h>
151 #include <linux/cdrom.h> /* for the eject ioctl */
152 #include <linux/blkdev.h>
153 #include <linux/blkpg.h>
154 #include <asm/uaccess.h>
155 #include <linux/sched.h>
156 #include <linux/workqueue.h>
158 static DEFINE_SPINLOCK(pd_lock);
160 module_param(verbose, bool, 0);
161 module_param(major, int, 0);
162 module_param(name, charp, 0);
163 module_param(cluster, int, 0);
164 module_param(nice, int, 0);
165 module_param_array(drive0, int, NULL, 0);
166 module_param_array(drive1, int, NULL, 0);
167 module_param_array(drive2, int, NULL, 0);
168 module_param_array(drive3, int, NULL, 0);
170 #include "paride.h"
172 #define PD_BITS 4
174 /* numbers for "SCSI" geometry */
176 #define PD_LOG_HEADS 64
177 #define PD_LOG_SECTS 32
179 #define PD_ID_OFF 54
180 #define PD_ID_LEN 14
182 #define PD_MAX_RETRIES 5
183 #define PD_TMO 800 /* interrupt timeout in jiffies */
184 #define PD_SPIN_DEL 50 /* spin delay in micro-seconds */
186 #define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
188 #define STAT_ERR 0x00001
189 #define STAT_INDEX 0x00002
190 #define STAT_ECC 0x00004
191 #define STAT_DRQ 0x00008
192 #define STAT_SEEK 0x00010
193 #define STAT_WRERR 0x00020
194 #define STAT_READY 0x00040
195 #define STAT_BUSY 0x00080
197 #define ERR_AMNF 0x00100
198 #define ERR_TK0NF 0x00200
199 #define ERR_ABRT 0x00400
200 #define ERR_MCR 0x00800
201 #define ERR_IDNF 0x01000
202 #define ERR_MC 0x02000
203 #define ERR_UNC 0x04000
204 #define ERR_TMO 0x10000
206 #define IDE_READ 0x20
207 #define IDE_WRITE 0x30
208 #define IDE_READ_VRFY 0x40
209 #define IDE_INIT_DEV_PARMS 0x91
210 #define IDE_STANDBY 0x96
211 #define IDE_ACKCHANGE 0xdb
212 #define IDE_DOORLOCK 0xde
213 #define IDE_DOORUNLOCK 0xdf
214 #define IDE_IDENTIFY 0xec
215 #define IDE_EJECT 0xed
217 #define PD_NAMELEN 8
219 struct pd_unit {
220 struct pi_adapter pia; /* interface to paride layer */
221 struct pi_adapter *pi;
222 int access; /* count of active opens ... */
223 int capacity; /* Size of this volume in sectors */
224 int heads; /* physical geometry */
225 int sectors;
226 int cylinders;
227 int can_lba;
228 int drive; /* master=0 slave=1 */
229 int changed; /* Have we seen a disk change ? */
230 int removable; /* removable media device ? */
231 int standby;
232 int alt_geom;
233 char name[PD_NAMELEN]; /* pda, pdb, etc ... */
234 struct gendisk *gd;
237 static struct pd_unit pd[PD_UNITS];
239 static char pd_scratch[512]; /* scratch block buffer */
241 static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
242 "READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
243 "IDNF", "MC", "UNC", "???", "TMO"
246 static inline int status_reg(struct pd_unit *disk)
248 return pi_read_regr(disk->pi, 1, 6);
251 static inline int read_reg(struct pd_unit *disk, int reg)
253 return pi_read_regr(disk->pi, 0, reg);
256 static inline void write_status(struct pd_unit *disk, int val)
258 pi_write_regr(disk->pi, 1, 6, val);
261 static inline void write_reg(struct pd_unit *disk, int reg, int val)
263 pi_write_regr(disk->pi, 0, reg, val);
266 static inline u8 DRIVE(struct pd_unit *disk)
268 return 0xa0+0x10*disk->drive;
271 /* ide command interface */
273 static void pd_print_error(struct pd_unit *disk, char *msg, int status)
275 int i;
277 printk("%s: %s: status = 0x%x =", disk->name, msg, status);
278 for (i = 0; i < 18; i++)
279 if (status & (1 << i))
280 printk(" %s", pd_errs[i]);
281 printk("\n");
284 static void pd_reset(struct pd_unit *disk)
285 { /* called only for MASTER drive */
286 write_status(disk, 4);
287 udelay(50);
288 write_status(disk, 0);
289 udelay(250);
292 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
294 static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
295 { /* polled wait */
296 int k, r, e;
298 k = 0;
299 while (k < PD_SPIN) {
300 r = status_reg(disk);
301 k++;
302 if (((r & w) == w) && !(r & STAT_BUSY))
303 break;
304 udelay(PD_SPIN_DEL);
306 e = (read_reg(disk, 1) << 8) + read_reg(disk, 7);
307 if (k >= PD_SPIN)
308 e |= ERR_TMO;
309 if ((e & (STAT_ERR | ERR_TMO)) && (msg != NULL))
310 pd_print_error(disk, msg, e);
311 return e;
314 static void pd_send_command(struct pd_unit *disk, int n, int s, int h, int c0, int c1, int func)
316 write_reg(disk, 6, DRIVE(disk) + h);
317 write_reg(disk, 1, 0); /* the IDE task file */
318 write_reg(disk, 2, n);
319 write_reg(disk, 3, s);
320 write_reg(disk, 4, c0);
321 write_reg(disk, 5, c1);
322 write_reg(disk, 7, func);
324 udelay(1);
327 static void pd_ide_command(struct pd_unit *disk, int func, int block, int count)
329 int c1, c0, h, s;
331 if (disk->can_lba) {
332 s = block & 255;
333 c0 = (block >>= 8) & 255;
334 c1 = (block >>= 8) & 255;
335 h = ((block >>= 8) & 15) + 0x40;
336 } else {
337 s = (block % disk->sectors) + 1;
338 h = (block /= disk->sectors) % disk->heads;
339 c0 = (block /= disk->heads) % 256;
340 c1 = (block >>= 8);
342 pd_send_command(disk, count, s, h, c0, c1, func);
345 /* The i/o request engine */
347 enum action {Fail = 0, Ok = 1, Hold, Wait};
349 static struct request *pd_req; /* current request */
350 static enum action (*phase)(void);
352 static void run_fsm(void);
354 static void ps_tq_int( void *data);
356 static DECLARE_WORK(fsm_tq, ps_tq_int, NULL);
358 static void schedule_fsm(void)
360 if (!nice)
361 schedule_work(&fsm_tq);
362 else
363 schedule_delayed_work(&fsm_tq, nice-1);
366 static void ps_tq_int(void *data)
368 run_fsm();
371 static enum action do_pd_io_start(void);
372 static enum action pd_special(void);
373 static enum action do_pd_read_start(void);
374 static enum action do_pd_write_start(void);
375 static enum action do_pd_read_drq(void);
376 static enum action do_pd_write_done(void);
378 static struct request_queue *pd_queue;
379 static int pd_claimed;
381 static struct pd_unit *pd_current; /* current request's drive */
382 static PIA *pi_current; /* current request's PIA */
384 static void run_fsm(void)
386 while (1) {
387 enum action res;
388 unsigned long saved_flags;
389 int stop = 0;
391 if (!phase) {
392 pd_current = pd_req->rq_disk->private_data;
393 pi_current = pd_current->pi;
394 phase = do_pd_io_start;
397 switch (pd_claimed) {
398 case 0:
399 pd_claimed = 1;
400 if (!pi_schedule_claimed(pi_current, run_fsm))
401 return;
402 case 1:
403 pd_claimed = 2;
404 pi_current->proto->connect(pi_current);
407 switch(res = phase()) {
408 case Ok: case Fail:
409 pi_disconnect(pi_current);
410 pd_claimed = 0;
411 phase = NULL;
412 spin_lock_irqsave(&pd_lock, saved_flags);
413 end_request(pd_req, res);
414 pd_req = elv_next_request(pd_queue);
415 if (!pd_req)
416 stop = 1;
417 spin_unlock_irqrestore(&pd_lock, saved_flags);
418 if (stop)
419 return;
420 case Hold:
421 schedule_fsm();
422 return;
423 case Wait:
424 pi_disconnect(pi_current);
425 pd_claimed = 0;
430 static int pd_retries = 0; /* i/o error retry count */
431 static int pd_block; /* address of next requested block */
432 static int pd_count; /* number of blocks still to do */
433 static int pd_run; /* sectors in current cluster */
434 static int pd_cmd; /* current command READ/WRITE */
435 static char *pd_buf; /* buffer for request in progress */
437 static enum action do_pd_io_start(void)
439 if (pd_req->flags & REQ_SPECIAL) {
440 phase = pd_special;
441 return pd_special();
444 pd_cmd = rq_data_dir(pd_req);
445 if (pd_cmd == READ || pd_cmd == WRITE) {
446 pd_block = pd_req->sector;
447 pd_count = pd_req->current_nr_sectors;
448 if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
449 return Fail;
450 pd_run = pd_req->nr_sectors;
451 pd_buf = pd_req->buffer;
452 pd_retries = 0;
453 if (pd_cmd == READ)
454 return do_pd_read_start();
455 else
456 return do_pd_write_start();
458 return Fail;
461 static enum action pd_special(void)
463 enum action (*func)(struct pd_unit *) = pd_req->special;
464 return func(pd_current);
467 static int pd_next_buf(void)
469 unsigned long saved_flags;
471 pd_count--;
472 pd_run--;
473 pd_buf += 512;
474 pd_block++;
475 if (!pd_run)
476 return 1;
477 if (pd_count)
478 return 0;
479 spin_lock_irqsave(&pd_lock, saved_flags);
480 end_request(pd_req, 1);
481 pd_count = pd_req->current_nr_sectors;
482 pd_buf = pd_req->buffer;
483 spin_unlock_irqrestore(&pd_lock, saved_flags);
484 return 0;
487 static unsigned long pd_timeout;
489 static enum action do_pd_read_start(void)
491 if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
492 if (pd_retries < PD_MAX_RETRIES) {
493 pd_retries++;
494 return Wait;
496 return Fail;
498 pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
499 phase = do_pd_read_drq;
500 pd_timeout = jiffies + PD_TMO;
501 return Hold;
504 static enum action do_pd_write_start(void)
506 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
507 if (pd_retries < PD_MAX_RETRIES) {
508 pd_retries++;
509 return Wait;
511 return Fail;
513 pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
514 while (1) {
515 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
516 if (pd_retries < PD_MAX_RETRIES) {
517 pd_retries++;
518 return Wait;
520 return Fail;
522 pi_write_block(pd_current->pi, pd_buf, 512);
523 if (pd_next_buf())
524 break;
526 phase = do_pd_write_done;
527 pd_timeout = jiffies + PD_TMO;
528 return Hold;
531 static inline int pd_ready(void)
533 return !(status_reg(pd_current) & STAT_BUSY);
536 static enum action do_pd_read_drq(void)
538 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
539 return Hold;
541 while (1) {
542 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
543 if (pd_retries < PD_MAX_RETRIES) {
544 pd_retries++;
545 phase = do_pd_read_start;
546 return Wait;
548 return Fail;
550 pi_read_block(pd_current->pi, pd_buf, 512);
551 if (pd_next_buf())
552 break;
554 return Ok;
557 static enum action do_pd_write_done(void)
559 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
560 return Hold;
562 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
563 if (pd_retries < PD_MAX_RETRIES) {
564 pd_retries++;
565 phase = do_pd_write_start;
566 return Wait;
568 return Fail;
570 return Ok;
573 /* special io requests */
575 /* According to the ATA standard, the default CHS geometry should be
576 available following a reset. Some Western Digital drives come up
577 in a mode where only LBA addresses are accepted until the device
578 parameters are initialised.
581 static void pd_init_dev_parms(struct pd_unit *disk)
583 pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
584 pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
585 IDE_INIT_DEV_PARMS);
586 udelay(300);
587 pd_wait_for(disk, 0, "Initialise device parameters");
590 static enum action pd_door_lock(struct pd_unit *disk)
592 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
593 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
594 pd_wait_for(disk, STAT_READY, "Lock done");
596 return Ok;
599 static enum action pd_door_unlock(struct pd_unit *disk)
601 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
602 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
603 pd_wait_for(disk, STAT_READY, "Lock done");
605 return Ok;
608 static enum action pd_eject(struct pd_unit *disk)
610 pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
611 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
612 pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
613 pd_wait_for(disk, 0, DBMSG("before eject"));
614 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
615 pd_wait_for(disk, 0, DBMSG("after eject"));
616 return Ok;
619 static enum action pd_media_check(struct pd_unit *disk)
621 int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
622 if (!(r & STAT_ERR)) {
623 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
624 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
625 } else
626 disk->changed = 1; /* say changed if other error */
627 if (r & ERR_MC) {
628 disk->changed = 1;
629 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
630 pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
631 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
632 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
634 return Ok;
637 static void pd_standby_off(struct pd_unit *disk)
639 pd_wait_for(disk, 0, DBMSG("before STANDBY"));
640 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
641 pd_wait_for(disk, 0, DBMSG("after STANDBY"));
644 static enum action pd_identify(struct pd_unit *disk)
646 int j;
647 char id[PD_ID_LEN + 1];
649 /* WARNING: here there may be dragons. reset() applies to both drives,
650 but we call it only on probing the MASTER. This should allow most
651 common configurations to work, but be warned that a reset can clear
652 settings on the SLAVE drive.
655 if (disk->drive == 0)
656 pd_reset(disk);
658 write_reg(disk, 6, DRIVE(disk));
659 pd_wait_for(disk, 0, DBMSG("before IDENT"));
660 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
662 if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
663 return Fail;
664 pi_read_block(disk->pi, pd_scratch, 512);
665 disk->can_lba = pd_scratch[99] & 2;
666 disk->sectors = le16_to_cpu(*(u16 *) (pd_scratch + 12));
667 disk->heads = le16_to_cpu(*(u16 *) (pd_scratch + 6));
668 disk->cylinders = le16_to_cpu(*(u16 *) (pd_scratch + 2));
669 if (disk->can_lba)
670 disk->capacity = le32_to_cpu(*(u32 *) (pd_scratch + 120));
671 else
672 disk->capacity = disk->sectors * disk->heads * disk->cylinders;
674 for (j = 0; j < PD_ID_LEN; j++)
675 id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
676 j = PD_ID_LEN - 1;
677 while ((j >= 0) && (id[j] <= 0x20))
678 j--;
679 j++;
680 id[j] = 0;
682 disk->removable = pd_scratch[0] & 0x80;
684 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
685 disk->name, id,
686 disk->drive ? "slave" : "master",
687 disk->capacity, disk->capacity / 2048,
688 disk->cylinders, disk->heads, disk->sectors,
689 disk->removable ? "removable" : "fixed");
691 if (disk->capacity)
692 pd_init_dev_parms(disk);
693 if (!disk->standby)
694 pd_standby_off(disk);
696 return Ok;
699 /* end of io request engine */
701 static void do_pd_request(request_queue_t * q)
703 if (pd_req)
704 return;
705 pd_req = elv_next_request(q);
706 if (!pd_req)
707 return;
709 schedule_fsm();
712 static int pd_special_command(struct pd_unit *disk,
713 enum action (*func)(struct pd_unit *disk))
715 DECLARE_COMPLETION(wait);
716 struct request rq;
717 int err = 0;
719 memset(&rq, 0, sizeof(rq));
720 rq.errors = 0;
721 rq.rq_status = RQ_ACTIVE;
722 rq.rq_disk = disk->gd;
723 rq.ref_count = 1;
724 rq.waiting = &wait;
725 rq.end_io = blk_end_sync_rq;
726 blk_insert_request(disk->gd->queue, &rq, 0, func, 0);
727 wait_for_completion(&wait);
728 rq.waiting = NULL;
729 if (rq.errors)
730 err = -EIO;
731 blk_put_request(&rq);
732 return err;
735 /* kernel glue structures */
737 static int pd_open(struct inode *inode, struct file *file)
739 struct pd_unit *disk = inode->i_bdev->bd_disk->private_data;
741 disk->access++;
743 if (disk->removable) {
744 pd_special_command(disk, pd_media_check);
745 pd_special_command(disk, pd_door_lock);
747 return 0;
750 static int pd_ioctl(struct inode *inode, struct file *file,
751 unsigned int cmd, unsigned long arg)
753 struct pd_unit *disk = inode->i_bdev->bd_disk->private_data;
754 struct hd_geometry __user *geo = (struct hd_geometry __user *) arg;
755 struct hd_geometry g;
757 switch (cmd) {
758 case CDROMEJECT:
759 if (disk->access == 1)
760 pd_special_command(disk, pd_eject);
761 return 0;
762 case HDIO_GETGEO:
763 if (disk->alt_geom) {
764 g.heads = PD_LOG_HEADS;
765 g.sectors = PD_LOG_SECTS;
766 g.cylinders = disk->capacity / (g.heads * g.sectors);
767 } else {
768 g.heads = disk->heads;
769 g.sectors = disk->sectors;
770 g.cylinders = disk->cylinders;
772 g.start = get_start_sect(inode->i_bdev);
773 if (copy_to_user(geo, &g, sizeof(struct hd_geometry)))
774 return -EFAULT;
775 return 0;
776 default:
777 return -EINVAL;
781 static int pd_release(struct inode *inode, struct file *file)
783 struct pd_unit *disk = inode->i_bdev->bd_disk->private_data;
785 if (!--disk->access && disk->removable)
786 pd_special_command(disk, pd_door_unlock);
788 return 0;
791 static int pd_check_media(struct gendisk *p)
793 struct pd_unit *disk = p->private_data;
794 int r;
795 if (!disk->removable)
796 return 0;
797 pd_special_command(disk, pd_media_check);
798 r = disk->changed;
799 disk->changed = 0;
800 return r;
803 static int pd_revalidate(struct gendisk *p)
805 struct pd_unit *disk = p->private_data;
806 if (pd_special_command(disk, pd_identify) == 0)
807 set_capacity(p, disk->capacity);
808 else
809 set_capacity(p, 0);
810 return 0;
813 static struct block_device_operations pd_fops = {
814 .owner = THIS_MODULE,
815 .open = pd_open,
816 .release = pd_release,
817 .ioctl = pd_ioctl,
818 .media_changed = pd_check_media,
819 .revalidate_disk= pd_revalidate
822 /* probing */
824 static void pd_probe_drive(struct pd_unit *disk)
826 struct gendisk *p = alloc_disk(1 << PD_BITS);
827 if (!p)
828 return;
829 strcpy(p->disk_name, disk->name);
830 p->fops = &pd_fops;
831 p->major = major;
832 p->first_minor = (disk - pd) << PD_BITS;
833 disk->gd = p;
834 p->private_data = disk;
835 p->queue = pd_queue;
837 if (disk->drive == -1) {
838 for (disk->drive = 0; disk->drive <= 1; disk->drive++)
839 if (pd_special_command(disk, pd_identify) == 0)
840 return;
841 } else if (pd_special_command(disk, pd_identify) == 0)
842 return;
843 disk->gd = NULL;
844 put_disk(p);
847 static int pd_detect(void)
849 int found = 0, unit, pd_drive_count = 0;
850 struct pd_unit *disk;
852 for (unit = 0; unit < PD_UNITS; unit++) {
853 int *parm = *drives[unit];
854 struct pd_unit *disk = pd + unit;
855 disk->pi = &disk->pia;
856 disk->access = 0;
857 disk->changed = 1;
858 disk->capacity = 0;
859 disk->drive = parm[D_SLV];
860 snprintf(disk->name, PD_NAMELEN, "%s%c", name, 'a'+unit);
861 disk->alt_geom = parm[D_GEO];
862 disk->standby = parm[D_SBY];
863 if (parm[D_PRT])
864 pd_drive_count++;
867 if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
868 disk = pd;
869 if (pi_init(disk->pi, 1, -1, -1, -1, -1, -1, pd_scratch,
870 PI_PD, verbose, disk->name)) {
871 pd_probe_drive(disk);
872 if (!disk->gd)
873 pi_release(disk->pi);
876 } else {
877 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
878 int *parm = *drives[unit];
879 if (!parm[D_PRT])
880 continue;
881 if (pi_init(disk->pi, 0, parm[D_PRT], parm[D_MOD],
882 parm[D_UNI], parm[D_PRO], parm[D_DLY],
883 pd_scratch, PI_PD, verbose, disk->name)) {
884 pd_probe_drive(disk);
885 if (!disk->gd)
886 pi_release(disk->pi);
890 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
891 if (disk->gd) {
892 set_capacity(disk->gd, disk->capacity);
893 add_disk(disk->gd);
894 found = 1;
897 if (!found)
898 printk("%s: no valid drive found\n", name);
899 return found;
902 static int __init pd_init(void)
904 if (disable)
905 goto out1;
907 pd_queue = blk_init_queue(do_pd_request, &pd_lock);
908 if (!pd_queue)
909 goto out1;
911 blk_queue_max_sectors(pd_queue, cluster);
913 if (register_blkdev(major, name))
914 goto out2;
916 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
917 name, name, PD_VERSION, major, cluster, nice);
918 if (!pd_detect())
919 goto out3;
921 return 0;
923 out3:
924 unregister_blkdev(major, name);
925 out2:
926 blk_cleanup_queue(pd_queue);
927 out1:
928 return -ENODEV;
931 static void __exit pd_exit(void)
933 struct pd_unit *disk;
934 int unit;
935 unregister_blkdev(major, name);
936 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
937 struct gendisk *p = disk->gd;
938 if (p) {
939 disk->gd = NULL;
940 del_gendisk(p);
941 put_disk(p);
942 pi_release(disk->pi);
945 blk_cleanup_queue(pd_queue);
948 MODULE_LICENSE("GPL");
949 module_init(pd_init)
950 module_exit(pd_exit)