Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / block / paride / pd.c
blob2403721f9db107509cc4ff8b628795a70290597e
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 <linux/kernel.h>
155 #include <asm/uaccess.h>
156 #include <linux/sched.h>
157 #include <linux/workqueue.h>
159 static DEFINE_SPINLOCK(pd_lock);
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);
171 #include "paride.h"
173 #define PD_BITS 4
175 /* numbers for "SCSI" geometry */
177 #define PD_LOG_HEADS 64
178 #define PD_LOG_SECTS 32
180 #define PD_ID_OFF 54
181 #define PD_ID_LEN 14
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 */
187 #define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
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
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
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
218 #define PD_NAMELEN 8
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;
238 static struct pd_unit pd[PD_UNITS];
240 static char pd_scratch[512]; /* scratch block buffer */
242 static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
243 "READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
244 "IDNF", "MC", "UNC", "???", "TMO"
247 static inline int status_reg(struct pd_unit *disk)
249 return pi_read_regr(disk->pi, 1, 6);
252 static inline int read_reg(struct pd_unit *disk, int reg)
254 return pi_read_regr(disk->pi, 0, reg);
257 static inline void write_status(struct pd_unit *disk, int val)
259 pi_write_regr(disk->pi, 1, 6, val);
262 static inline void write_reg(struct pd_unit *disk, int reg, int val)
264 pi_write_regr(disk->pi, 0, reg, val);
267 static inline u8 DRIVE(struct pd_unit *disk)
269 return 0xa0+0x10*disk->drive;
272 /* ide command interface */
274 static void pd_print_error(struct pd_unit *disk, char *msg, int status)
276 int i;
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");
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);
293 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
295 static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
296 { /* polled wait */
297 int k, r, e;
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);
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;
315 static void pd_send_command(struct pd_unit *disk, int n, int s, int h, int c0, int c1, int func)
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);
325 udelay(1);
328 static void pd_ide_command(struct pd_unit *disk, int func, int block, int count)
330 int c1, c0, h, s;
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);
343 pd_send_command(disk, count, s, h, c0, c1, func);
346 /* The i/o request engine */
348 enum action {Fail = 0, Ok = 1, Hold, Wait};
350 static struct request *pd_req; /* current request */
351 static enum action (*phase)(void);
353 static void run_fsm(void);
355 static void ps_tq_int( void *data);
357 static DECLARE_WORK(fsm_tq, ps_tq_int, NULL);
359 static void schedule_fsm(void)
361 if (!nice)
362 schedule_work(&fsm_tq);
363 else
364 schedule_delayed_work(&fsm_tq, nice-1);
367 static void ps_tq_int(void *data)
369 run_fsm();
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);
379 static struct request_queue *pd_queue;
380 static int pd_claimed;
382 static struct pd_unit *pd_current; /* current request's drive */
383 static PIA *pi_current; /* current request's PIA */
385 static void run_fsm(void)
387 while (1) {
388 enum action res;
389 unsigned long saved_flags;
390 int stop = 0;
392 if (!phase) {
393 pd_current = pd_req->rq_disk->private_data;
394 pi_current = pd_current->pi;
395 phase = do_pd_io_start;
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);
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 end_request(pd_req, res);
415 pd_req = elv_next_request(pd_queue);
416 if (!pd_req)
417 stop = 1;
418 spin_unlock_irqrestore(&pd_lock, saved_flags);
419 if (stop)
420 return;
421 case Hold:
422 schedule_fsm();
423 return;
424 case Wait:
425 pi_disconnect(pi_current);
426 pd_claimed = 0;
431 static int pd_retries = 0; /* i/o error retry count */
432 static int pd_block; /* address of next requested block */
433 static int pd_count; /* number of blocks still to do */
434 static int pd_run; /* sectors in current cluster */
435 static int pd_cmd; /* current command READ/WRITE */
436 static char *pd_buf; /* buffer for request in progress */
438 static enum action do_pd_io_start(void)
440 if (pd_req->flags & REQ_SPECIAL) {
441 phase = pd_special;
442 return pd_special();
445 pd_cmd = rq_data_dir(pd_req);
446 if (pd_cmd == READ || pd_cmd == WRITE) {
447 pd_block = pd_req->sector;
448 pd_count = pd_req->current_nr_sectors;
449 if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
450 return Fail;
451 pd_run = pd_req->nr_sectors;
452 pd_buf = pd_req->buffer;
453 pd_retries = 0;
454 if (pd_cmd == READ)
455 return do_pd_read_start();
456 else
457 return do_pd_write_start();
459 return Fail;
462 static enum action pd_special(void)
464 enum action (*func)(struct pd_unit *) = pd_req->special;
465 return func(pd_current);
468 static int pd_next_buf(void)
470 unsigned long saved_flags;
472 pd_count--;
473 pd_run--;
474 pd_buf += 512;
475 pd_block++;
476 if (!pd_run)
477 return 1;
478 if (pd_count)
479 return 0;
480 spin_lock_irqsave(&pd_lock, saved_flags);
481 end_request(pd_req, 1);
482 pd_count = pd_req->current_nr_sectors;
483 pd_buf = pd_req->buffer;
484 spin_unlock_irqrestore(&pd_lock, saved_flags);
485 return 0;
488 static unsigned long pd_timeout;
490 static enum action do_pd_read_start(void)
492 if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
493 if (pd_retries < PD_MAX_RETRIES) {
494 pd_retries++;
495 return Wait;
497 return Fail;
499 pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
500 phase = do_pd_read_drq;
501 pd_timeout = jiffies + PD_TMO;
502 return Hold;
505 static enum action do_pd_write_start(void)
507 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
508 if (pd_retries < PD_MAX_RETRIES) {
509 pd_retries++;
510 return Wait;
512 return Fail;
514 pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
515 while (1) {
516 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
517 if (pd_retries < PD_MAX_RETRIES) {
518 pd_retries++;
519 return Wait;
521 return Fail;
523 pi_write_block(pd_current->pi, pd_buf, 512);
524 if (pd_next_buf())
525 break;
527 phase = do_pd_write_done;
528 pd_timeout = jiffies + PD_TMO;
529 return Hold;
532 static inline int pd_ready(void)
534 return !(status_reg(pd_current) & STAT_BUSY);
537 static enum action do_pd_read_drq(void)
539 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
540 return Hold;
542 while (1) {
543 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
544 if (pd_retries < PD_MAX_RETRIES) {
545 pd_retries++;
546 phase = do_pd_read_start;
547 return Wait;
549 return Fail;
551 pi_read_block(pd_current->pi, pd_buf, 512);
552 if (pd_next_buf())
553 break;
555 return Ok;
558 static enum action do_pd_write_done(void)
560 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
561 return Hold;
563 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
564 if (pd_retries < PD_MAX_RETRIES) {
565 pd_retries++;
566 phase = do_pd_write_start;
567 return Wait;
569 return Fail;
571 return Ok;
574 /* special io requests */
576 /* According to the ATA standard, the default CHS geometry should be
577 available following a reset. Some Western Digital drives come up
578 in a mode where only LBA addresses are accepted until the device
579 parameters are initialised.
582 static void pd_init_dev_parms(struct pd_unit *disk)
584 pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
585 pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
586 IDE_INIT_DEV_PARMS);
587 udelay(300);
588 pd_wait_for(disk, 0, "Initialise device parameters");
591 static enum action pd_door_lock(struct pd_unit *disk)
593 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
594 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
595 pd_wait_for(disk, STAT_READY, "Lock done");
597 return Ok;
600 static enum action pd_door_unlock(struct pd_unit *disk)
602 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
603 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
604 pd_wait_for(disk, STAT_READY, "Lock done");
606 return Ok;
609 static enum action pd_eject(struct pd_unit *disk)
611 pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
612 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
613 pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
614 pd_wait_for(disk, 0, DBMSG("before eject"));
615 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
616 pd_wait_for(disk, 0, DBMSG("after eject"));
617 return Ok;
620 static enum action pd_media_check(struct pd_unit *disk)
622 int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
623 if (!(r & STAT_ERR)) {
624 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
625 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
626 } else
627 disk->changed = 1; /* say changed if other error */
628 if (r & ERR_MC) {
629 disk->changed = 1;
630 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
631 pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
632 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
633 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
635 return Ok;
638 static void pd_standby_off(struct pd_unit *disk)
640 pd_wait_for(disk, 0, DBMSG("before STANDBY"));
641 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
642 pd_wait_for(disk, 0, DBMSG("after STANDBY"));
645 static enum action pd_identify(struct pd_unit *disk)
647 int j;
648 char id[PD_ID_LEN + 1];
650 /* WARNING: here there may be dragons. reset() applies to both drives,
651 but we call it only on probing the MASTER. This should allow most
652 common configurations to work, but be warned that a reset can clear
653 settings on the SLAVE drive.
656 if (disk->drive == 0)
657 pd_reset(disk);
659 write_reg(disk, 6, DRIVE(disk));
660 pd_wait_for(disk, 0, DBMSG("before IDENT"));
661 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
663 if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
664 return Fail;
665 pi_read_block(disk->pi, pd_scratch, 512);
666 disk->can_lba = pd_scratch[99] & 2;
667 disk->sectors = le16_to_cpu(*(u16 *) (pd_scratch + 12));
668 disk->heads = le16_to_cpu(*(u16 *) (pd_scratch + 6));
669 disk->cylinders = le16_to_cpu(*(u16 *) (pd_scratch + 2));
670 if (disk->can_lba)
671 disk->capacity = le32_to_cpu(*(u32 *) (pd_scratch + 120));
672 else
673 disk->capacity = disk->sectors * disk->heads * disk->cylinders;
675 for (j = 0; j < PD_ID_LEN; j++)
676 id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
677 j = PD_ID_LEN - 1;
678 while ((j >= 0) && (id[j] <= 0x20))
679 j--;
680 j++;
681 id[j] = 0;
683 disk->removable = pd_scratch[0] & 0x80;
685 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
686 disk->name, id,
687 disk->drive ? "slave" : "master",
688 disk->capacity, disk->capacity / 2048,
689 disk->cylinders, disk->heads, disk->sectors,
690 disk->removable ? "removable" : "fixed");
692 if (disk->capacity)
693 pd_init_dev_parms(disk);
694 if (!disk->standby)
695 pd_standby_off(disk);
697 return Ok;
700 /* end of io request engine */
702 static void do_pd_request(request_queue_t * q)
704 if (pd_req)
705 return;
706 pd_req = elv_next_request(q);
707 if (!pd_req)
708 return;
710 schedule_fsm();
713 static int pd_special_command(struct pd_unit *disk,
714 enum action (*func)(struct pd_unit *disk))
716 DECLARE_COMPLETION(wait);
717 struct request rq;
718 int err = 0;
720 memset(&rq, 0, sizeof(rq));
721 rq.errors = 0;
722 rq.rq_status = RQ_ACTIVE;
723 rq.rq_disk = disk->gd;
724 rq.ref_count = 1;
725 rq.waiting = &wait;
726 rq.end_io = blk_end_sync_rq;
727 blk_insert_request(disk->gd->queue, &rq, 0, func);
728 wait_for_completion(&wait);
729 rq.waiting = NULL;
730 if (rq.errors)
731 err = -EIO;
732 blk_put_request(&rq);
733 return err;
736 /* kernel glue structures */
738 static int pd_open(struct inode *inode, struct file *file)
740 struct pd_unit *disk = inode->i_bdev->bd_disk->private_data;
742 disk->access++;
744 if (disk->removable) {
745 pd_special_command(disk, pd_media_check);
746 pd_special_command(disk, pd_door_lock);
748 return 0;
751 static int pd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
753 struct pd_unit *disk = bdev->bd_disk->private_data;
755 if (disk->alt_geom) {
756 geo->heads = PD_LOG_HEADS;
757 geo->sectors = PD_LOG_SECTS;
758 geo->cylinders = disk->capacity / (geo->heads * geo->sectors);
759 } else {
760 geo->heads = disk->heads;
761 geo->sectors = disk->sectors;
762 geo->cylinders = disk->cylinders;
765 return 0;
768 static int pd_ioctl(struct inode *inode, struct file *file,
769 unsigned int cmd, unsigned long arg)
771 struct pd_unit *disk = inode->i_bdev->bd_disk->private_data;
773 switch (cmd) {
774 case CDROMEJECT:
775 if (disk->access == 1)
776 pd_special_command(disk, pd_eject);
777 return 0;
778 default:
779 return -EINVAL;
783 static int pd_release(struct inode *inode, struct file *file)
785 struct pd_unit *disk = inode->i_bdev->bd_disk->private_data;
787 if (!--disk->access && disk->removable)
788 pd_special_command(disk, pd_door_unlock);
790 return 0;
793 static int pd_check_media(struct gendisk *p)
795 struct pd_unit *disk = p->private_data;
796 int r;
797 if (!disk->removable)
798 return 0;
799 pd_special_command(disk, pd_media_check);
800 r = disk->changed;
801 disk->changed = 0;
802 return r;
805 static int pd_revalidate(struct gendisk *p)
807 struct pd_unit *disk = p->private_data;
808 if (pd_special_command(disk, pd_identify) == 0)
809 set_capacity(p, disk->capacity);
810 else
811 set_capacity(p, 0);
812 return 0;
815 static struct block_device_operations pd_fops = {
816 .owner = THIS_MODULE,
817 .open = pd_open,
818 .release = pd_release,
819 .ioctl = pd_ioctl,
820 .getgeo = pd_getgeo,
821 .media_changed = pd_check_media,
822 .revalidate_disk= pd_revalidate
825 /* probing */
827 static void pd_probe_drive(struct pd_unit *disk)
829 struct gendisk *p = alloc_disk(1 << PD_BITS);
830 if (!p)
831 return;
832 strcpy(p->disk_name, disk->name);
833 p->fops = &pd_fops;
834 p->major = major;
835 p->first_minor = (disk - pd) << PD_BITS;
836 disk->gd = p;
837 p->private_data = disk;
838 p->queue = pd_queue;
840 if (disk->drive == -1) {
841 for (disk->drive = 0; disk->drive <= 1; disk->drive++)
842 if (pd_special_command(disk, pd_identify) == 0)
843 return;
844 } else if (pd_special_command(disk, pd_identify) == 0)
845 return;
846 disk->gd = NULL;
847 put_disk(p);
850 static int pd_detect(void)
852 int found = 0, unit, pd_drive_count = 0;
853 struct pd_unit *disk;
855 for (unit = 0; unit < PD_UNITS; unit++) {
856 int *parm = *drives[unit];
857 struct pd_unit *disk = pd + unit;
858 disk->pi = &disk->pia;
859 disk->access = 0;
860 disk->changed = 1;
861 disk->capacity = 0;
862 disk->drive = parm[D_SLV];
863 snprintf(disk->name, PD_NAMELEN, "%s%c", name, 'a'+unit);
864 disk->alt_geom = parm[D_GEO];
865 disk->standby = parm[D_SBY];
866 if (parm[D_PRT])
867 pd_drive_count++;
870 if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
871 disk = pd;
872 if (pi_init(disk->pi, 1, -1, -1, -1, -1, -1, pd_scratch,
873 PI_PD, verbose, disk->name)) {
874 pd_probe_drive(disk);
875 if (!disk->gd)
876 pi_release(disk->pi);
879 } else {
880 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
881 int *parm = *drives[unit];
882 if (!parm[D_PRT])
883 continue;
884 if (pi_init(disk->pi, 0, parm[D_PRT], parm[D_MOD],
885 parm[D_UNI], parm[D_PRO], parm[D_DLY],
886 pd_scratch, PI_PD, verbose, disk->name)) {
887 pd_probe_drive(disk);
888 if (!disk->gd)
889 pi_release(disk->pi);
893 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
894 if (disk->gd) {
895 set_capacity(disk->gd, disk->capacity);
896 add_disk(disk->gd);
897 found = 1;
900 if (!found)
901 printk("%s: no valid drive found\n", name);
902 return found;
905 static int __init pd_init(void)
907 if (disable)
908 goto out1;
910 pd_queue = blk_init_queue(do_pd_request, &pd_lock);
911 if (!pd_queue)
912 goto out1;
914 blk_queue_max_sectors(pd_queue, cluster);
916 if (register_blkdev(major, name))
917 goto out2;
919 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
920 name, name, PD_VERSION, major, cluster, nice);
921 if (!pd_detect())
922 goto out3;
924 return 0;
926 out3:
927 unregister_blkdev(major, name);
928 out2:
929 blk_cleanup_queue(pd_queue);
930 out1:
931 return -ENODEV;
934 static void __exit pd_exit(void)
936 struct pd_unit *disk;
937 int unit;
938 unregister_blkdev(major, name);
939 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
940 struct gendisk *p = disk->gd;
941 if (p) {
942 disk->gd = NULL;
943 del_gendisk(p);
944 put_disk(p);
945 pi_release(disk->pi);
948 blk_cleanup_queue(pd_queue);
951 MODULE_LICENSE("GPL");
952 module_init(pd_init)
953 module_exit(pd_exit)