1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
123
124
125
126
127#include <linux/types.h>
128
129static int verbose = 0;
130static int major = PD_MAJOR;
131static char *name = PD_NAME;
132static int cluster = 64;
133static int nice = 0;
134static int disable = 0;
135
136static int drive0[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
137static int drive1[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
138static int drive2[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
139static int drive3[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
140
141static int (*drives[4])[8] = {&drive0, &drive1, &drive2, &drive3};
142
143enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
144
145
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>
154#include <linux/blkdev.h>
155#include <linux/blkpg.h>
156#include <linux/kernel.h>
157#include <linux/mutex.h>
158#include <asm/uaccess.h>
159#include <linux/workqueue.h>
160
161static DEFINE_MUTEX(pd_mutex);
162static DEFINE_SPINLOCK(pd_lock);
163
164module_param(verbose, int, 0);
165module_param(major, int, 0);
166module_param(name, charp, 0);
167module_param(cluster, int, 0);
168module_param(nice, int, 0);
169module_param_array(drive0, int, NULL, 0);
170module_param_array(drive1, int, NULL, 0);
171module_param_array(drive2, int, NULL, 0);
172module_param_array(drive3, int, NULL, 0);
173
174#include "paride.h"
175
176#define PD_BITS 4
177
178
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
188#define PD_SPIN_DEL 50
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
223struct pd_unit {
224 struct pi_adapter pia;
225 struct pi_adapter *pi;
226 int access;
227 int capacity;
228 int heads;
229 int sectors;
230 int cylinders;
231 int can_lba;
232 int drive;
233 int changed;
234 int removable;
235 int standby;
236 int alt_geom;
237 char name[PD_NAMELEN];
238 struct gendisk *gd;
239};
240
241static struct pd_unit pd[PD_UNITS];
242
243static char pd_scratch[512];
244
245static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
246 "READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
247 "IDNF", "MC", "UNC", "???", "TMO"
248};
249
250static void *par_drv;
251
252static inline int status_reg(struct pd_unit *disk)
253{
254 return pi_read_regr(disk->pi, 1, 6);
255}
256
257static inline int read_reg(struct pd_unit *disk, int reg)
258{
259 return pi_read_regr(disk->pi, 0, reg);
260}
261
262static inline void write_status(struct pd_unit *disk, int val)
263{
264 pi_write_regr(disk->pi, 1, 6, val);
265}
266
267static inline void write_reg(struct pd_unit *disk, int reg, int val)
268{
269 pi_write_regr(disk->pi, 0, reg, val);
270}
271
272static inline u8 DRIVE(struct pd_unit *disk)
273{
274 return 0xa0+0x10*disk->drive;
275}
276
277
278
279static 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
290static void pd_reset(struct pd_unit *disk)
291{
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
300static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
301{
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
320static 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);
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
333static 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
352
353enum action {Fail = 0, Ok = 1, Hold, Wait};
354
355static struct request *pd_req;
356static enum action (*phase)(void);
357
358static void run_fsm(void);
359
360static void ps_tq_int(struct work_struct *work);
361
362static DECLARE_DELAYED_WORK(fsm_tq, ps_tq_int);
363
364static 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
372static void ps_tq_int(struct work_struct *work)
373{
374 run_fsm();
375}
376
377static enum action do_pd_io_start(void);
378static enum action pd_special(void);
379static enum action do_pd_read_start(void);
380static enum action do_pd_write_start(void);
381static enum action do_pd_read_drq(void);
382static enum action do_pd_write_done(void);
383
384static struct request_queue *pd_queue;
385static int pd_claimed;
386
387static struct pd_unit *pd_current;
388static PIA *pi_current;
389
390static void run_fsm(void)
391{
392 while (1) {
393 enum action res;
394 unsigned long saved_flags;
395 int stop = 0;
396
397 if (!phase) {
398 pd_current = pd_req->rq_disk->private_data;
399 pi_current = pd_current->pi;
400 phase = do_pd_io_start;
401 }
402
403 switch (pd_claimed) {
404 case 0:
405 pd_claimed = 1;
406 if (!pi_schedule_claimed(pi_current, run_fsm))
407 return;
408 case 1:
409 pd_claimed = 2;
410 pi_current->proto->connect(pi_current);
411 }
412
413 switch(res = phase()) {
414 case Ok: case Fail:
415 pi_disconnect(pi_current);
416 pd_claimed = 0;
417 phase = NULL;
418 spin_lock_irqsave(&pd_lock, saved_flags);
419 if (!__blk_end_request_cur(pd_req,
420 res == Ok ? 0 : -EIO)) {
421 pd_req = blk_fetch_request(pd_queue);
422 if (!pd_req)
423 stop = 1;
424 }
425 spin_unlock_irqrestore(&pd_lock, saved_flags);
426 if (stop)
427 return;
428 case Hold:
429 schedule_fsm();
430 return;
431 case Wait:
432 pi_disconnect(pi_current);
433 pd_claimed = 0;
434 }
435 }
436}
437
438static int pd_retries = 0;
439static int pd_block;
440static int pd_count;
441static int pd_run;
442static int pd_cmd;
443static char *pd_buf;
444
445static enum action do_pd_io_start(void)
446{
447 if (pd_req->cmd_type == REQ_TYPE_DRV_PRIV) {
448 phase = pd_special;
449 return pd_special();
450 }
451
452 pd_cmd = rq_data_dir(pd_req);
453 if (pd_cmd == READ || pd_cmd == WRITE) {
454 pd_block = blk_rq_pos(pd_req);
455 pd_count = blk_rq_cur_sectors(pd_req);
456 if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
457 return Fail;
458 pd_run = blk_rq_sectors(pd_req);
459 pd_buf = bio_data(pd_req->bio);
460 pd_retries = 0;
461 if (pd_cmd == READ)
462 return do_pd_read_start();
463 else
464 return do_pd_write_start();
465 }
466 return Fail;
467}
468
469static enum action pd_special(void)
470{
471 enum action (*func)(struct pd_unit *) = pd_req->special;
472 return func(pd_current);
473}
474
475static int pd_next_buf(void)
476{
477 unsigned long saved_flags;
478
479 pd_count--;
480 pd_run--;
481 pd_buf += 512;
482 pd_block++;
483 if (!pd_run)
484 return 1;
485 if (pd_count)
486 return 0;
487 spin_lock_irqsave(&pd_lock, saved_flags);
488 __blk_end_request_cur(pd_req, 0);
489 pd_count = blk_rq_cur_sectors(pd_req);
490 pd_buf = bio_data(pd_req->bio);
491 spin_unlock_irqrestore(&pd_lock, saved_flags);
492 return 0;
493}
494
495static unsigned long pd_timeout;
496
497static enum action do_pd_read_start(void)
498{
499 if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
500 if (pd_retries < PD_MAX_RETRIES) {
501 pd_retries++;
502 return Wait;
503 }
504 return Fail;
505 }
506 pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
507 phase = do_pd_read_drq;
508 pd_timeout = jiffies + PD_TMO;
509 return Hold;
510}
511
512static enum action do_pd_write_start(void)
513{
514 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
515 if (pd_retries < PD_MAX_RETRIES) {
516 pd_retries++;
517 return Wait;
518 }
519 return Fail;
520 }
521 pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
522 while (1) {
523 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
524 if (pd_retries < PD_MAX_RETRIES) {
525 pd_retries++;
526 return Wait;
527 }
528 return Fail;
529 }
530 pi_write_block(pd_current->pi, pd_buf, 512);
531 if (pd_next_buf())
532 break;
533 }
534 phase = do_pd_write_done;
535 pd_timeout = jiffies + PD_TMO;
536 return Hold;
537}
538
539static inline int pd_ready(void)
540{
541 return !(status_reg(pd_current) & STAT_BUSY);
542}
543
544static enum action do_pd_read_drq(void)
545{
546 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
547 return Hold;
548
549 while (1) {
550 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
551 if (pd_retries < PD_MAX_RETRIES) {
552 pd_retries++;
553 phase = do_pd_read_start;
554 return Wait;
555 }
556 return Fail;
557 }
558 pi_read_block(pd_current->pi, pd_buf, 512);
559 if (pd_next_buf())
560 break;
561 }
562 return Ok;
563}
564
565static enum action do_pd_write_done(void)
566{
567 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
568 return Hold;
569
570 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
571 if (pd_retries < PD_MAX_RETRIES) {
572 pd_retries++;
573 phase = do_pd_write_start;
574 return Wait;
575 }
576 return Fail;
577 }
578 return Ok;
579}
580
581
582
583
584
585
586
587
588
589static void pd_init_dev_parms(struct pd_unit *disk)
590{
591 pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
592 pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
593 IDE_INIT_DEV_PARMS);
594 udelay(300);
595 pd_wait_for(disk, 0, "Initialise device parameters");
596}
597
598static enum action pd_door_lock(struct pd_unit *disk)
599{
600 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
601 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
602 pd_wait_for(disk, STAT_READY, "Lock done");
603 }
604 return Ok;
605}
606
607static enum action pd_door_unlock(struct pd_unit *disk)
608{
609 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
610 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
611 pd_wait_for(disk, STAT_READY, "Lock done");
612 }
613 return Ok;
614}
615
616static enum action pd_eject(struct pd_unit *disk)
617{
618 pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
619 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
620 pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
621 pd_wait_for(disk, 0, DBMSG("before eject"));
622 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
623 pd_wait_for(disk, 0, DBMSG("after eject"));
624 return Ok;
625}
626
627static enum action pd_media_check(struct pd_unit *disk)
628{
629 int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
630 if (!(r & STAT_ERR)) {
631 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
632 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
633 } else
634 disk->changed = 1;
635 if (r & ERR_MC) {
636 disk->changed = 1;
637 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
638 pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
639 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
640 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
641 }
642 return Ok;
643}
644
645static void pd_standby_off(struct pd_unit *disk)
646{
647 pd_wait_for(disk, 0, DBMSG("before STANDBY"));
648 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
649 pd_wait_for(disk, 0, DBMSG("after STANDBY"));
650}
651
652static enum action pd_identify(struct pd_unit *disk)
653{
654 int j;
655 char id[PD_ID_LEN + 1];
656
657
658
659
660
661
662
663 if (disk->drive == 0)
664 pd_reset(disk);
665
666 write_reg(disk, 6, DRIVE(disk));
667 pd_wait_for(disk, 0, DBMSG("before IDENT"));
668 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
669
670 if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
671 return Fail;
672 pi_read_block(disk->pi, pd_scratch, 512);
673 disk->can_lba = pd_scratch[99] & 2;
674 disk->sectors = le16_to_cpu(*(__le16 *) (pd_scratch + 12));
675 disk->heads = le16_to_cpu(*(__le16 *) (pd_scratch + 6));
676 disk->cylinders = le16_to_cpu(*(__le16 *) (pd_scratch + 2));
677 if (disk->can_lba)
678 disk->capacity = le32_to_cpu(*(__le32 *) (pd_scratch + 120));
679 else
680 disk->capacity = disk->sectors * disk->heads * disk->cylinders;
681
682 for (j = 0; j < PD_ID_LEN; j++)
683 id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
684 j = PD_ID_LEN - 1;
685 while ((j >= 0) && (id[j] <= 0x20))
686 j--;
687 j++;
688 id[j] = 0;
689
690 disk->removable = pd_scratch[0] & 0x80;
691
692 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
693 disk->name, id,
694 disk->drive ? "slave" : "master",
695 disk->capacity, disk->capacity / 2048,
696 disk->cylinders, disk->heads, disk->sectors,
697 disk->removable ? "removable" : "fixed");
698
699 if (disk->capacity)
700 pd_init_dev_parms(disk);
701 if (!disk->standby)
702 pd_standby_off(disk);
703
704 return Ok;
705}
706
707
708
709static void do_pd_request(struct request_queue * q)
710{
711 if (pd_req)
712 return;
713 pd_req = blk_fetch_request(q);
714 if (!pd_req)
715 return;
716
717 schedule_fsm();
718}
719
720static int pd_special_command(struct pd_unit *disk,
721 enum action (*func)(struct pd_unit *disk))
722{
723 struct request *rq;
724 int err = 0;
725
726 rq = blk_get_request(disk->gd->queue, READ, __GFP_RECLAIM);
727 if (IS_ERR(rq))
728 return PTR_ERR(rq);
729
730 rq->cmd_type = REQ_TYPE_DRV_PRIV;
731 rq->special = func;
732
733 err = blk_execute_rq(disk->gd->queue, disk->gd, rq, 0);
734
735 blk_put_request(rq);
736 return err;
737}
738
739
740
741static int pd_open(struct block_device *bdev, fmode_t mode)
742{
743 struct pd_unit *disk = bdev->bd_disk->private_data;
744
745 mutex_lock(&pd_mutex);
746 disk->access++;
747
748 if (disk->removable) {
749 pd_special_command(disk, pd_media_check);
750 pd_special_command(disk, pd_door_lock);
751 }
752 mutex_unlock(&pd_mutex);
753 return 0;
754}
755
756static int pd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
757{
758 struct pd_unit *disk = bdev->bd_disk->private_data;
759
760 if (disk->alt_geom) {
761 geo->heads = PD_LOG_HEADS;
762 geo->sectors = PD_LOG_SECTS;
763 geo->cylinders = disk->capacity / (geo->heads * geo->sectors);
764 } else {
765 geo->heads = disk->heads;
766 geo->sectors = disk->sectors;
767 geo->cylinders = disk->cylinders;
768 }
769
770 return 0;
771}
772
773static int pd_ioctl(struct block_device *bdev, fmode_t mode,
774 unsigned int cmd, unsigned long arg)
775{
776 struct pd_unit *disk = bdev->bd_disk->private_data;
777
778 switch (cmd) {
779 case CDROMEJECT:
780 mutex_lock(&pd_mutex);
781 if (disk->access == 1)
782 pd_special_command(disk, pd_eject);
783 mutex_unlock(&pd_mutex);
784 return 0;
785 default:
786 return -EINVAL;
787 }
788}
789
790static void pd_release(struct gendisk *p, fmode_t mode)
791{
792 struct pd_unit *disk = p->private_data;
793
794 mutex_lock(&pd_mutex);
795 if (!--disk->access && disk->removable)
796 pd_special_command(disk, pd_door_unlock);
797 mutex_unlock(&pd_mutex);
798}
799
800static unsigned int pd_check_events(struct gendisk *p, unsigned int clearing)
801{
802 struct pd_unit *disk = p->private_data;
803 int r;
804 if (!disk->removable)
805 return 0;
806 pd_special_command(disk, pd_media_check);
807 r = disk->changed;
808 disk->changed = 0;
809 return r ? DISK_EVENT_MEDIA_CHANGE : 0;
810}
811
812static int pd_revalidate(struct gendisk *p)
813{
814 struct pd_unit *disk = p->private_data;
815 if (pd_special_command(disk, pd_identify) == 0)
816 set_capacity(p, disk->capacity);
817 else
818 set_capacity(p, 0);
819 return 0;
820}
821
822static const struct block_device_operations pd_fops = {
823 .owner = THIS_MODULE,
824 .open = pd_open,
825 .release = pd_release,
826 .ioctl = pd_ioctl,
827 .getgeo = pd_getgeo,
828 .check_events = pd_check_events,
829 .revalidate_disk= pd_revalidate
830};
831
832
833
834static void pd_probe_drive(struct pd_unit *disk)
835{
836 struct gendisk *p = alloc_disk(1 << PD_BITS);
837 if (!p)
838 return;
839 strcpy(p->disk_name, disk->name);
840 p->fops = &pd_fops;
841 p->major = major;
842 p->first_minor = (disk - pd) << PD_BITS;
843 disk->gd = p;
844 p->private_data = disk;
845 p->queue = pd_queue;
846
847 if (disk->drive == -1) {
848 for (disk->drive = 0; disk->drive <= 1; disk->drive++)
849 if (pd_special_command(disk, pd_identify) == 0)
850 return;
851 } else if (pd_special_command(disk, pd_identify) == 0)
852 return;
853 disk->gd = NULL;
854 put_disk(p);
855}
856
857static int pd_detect(void)
858{
859 int found = 0, unit, pd_drive_count = 0;
860 struct pd_unit *disk;
861
862 for (unit = 0; unit < PD_UNITS; unit++) {
863 int *parm = *drives[unit];
864 struct pd_unit *disk = pd + unit;
865 disk->pi = &disk->pia;
866 disk->access = 0;
867 disk->changed = 1;
868 disk->capacity = 0;
869 disk->drive = parm[D_SLV];
870 snprintf(disk->name, PD_NAMELEN, "%s%c", name, 'a'+unit);
871 disk->alt_geom = parm[D_GEO];
872 disk->standby = parm[D_SBY];
873 if (parm[D_PRT])
874 pd_drive_count++;
875 }
876
877 par_drv = pi_register_driver(name);
878 if (!par_drv) {
879 pr_err("failed to register %s driver\n", name);
880 return -1;
881 }
882
883 if (pd_drive_count == 0) {
884 disk = pd;
885 if (pi_init(disk->pi, 1, -1, -1, -1, -1, -1, pd_scratch,
886 PI_PD, verbose, disk->name)) {
887 pd_probe_drive(disk);
888 if (!disk->gd)
889 pi_release(disk->pi);
890 }
891
892 } else {
893 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
894 int *parm = *drives[unit];
895 if (!parm[D_PRT])
896 continue;
897 if (pi_init(disk->pi, 0, parm[D_PRT], parm[D_MOD],
898 parm[D_UNI], parm[D_PRO], parm[D_DLY],
899 pd_scratch, PI_PD, verbose, disk->name)) {
900 pd_probe_drive(disk);
901 if (!disk->gd)
902 pi_release(disk->pi);
903 }
904 }
905 }
906 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
907 if (disk->gd) {
908 set_capacity(disk->gd, disk->capacity);
909 add_disk(disk->gd);
910 found = 1;
911 }
912 }
913 if (!found) {
914 printk("%s: no valid drive found\n", name);
915 pi_unregister_driver(par_drv);
916 }
917 return found;
918}
919
920static int __init pd_init(void)
921{
922 if (disable)
923 goto out1;
924
925 pd_queue = blk_init_queue(do_pd_request, &pd_lock);
926 if (!pd_queue)
927 goto out1;
928
929 blk_queue_max_hw_sectors(pd_queue, cluster);
930
931 if (register_blkdev(major, name))
932 goto out2;
933
934 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
935 name, name, PD_VERSION, major, cluster, nice);
936 if (!pd_detect())
937 goto out3;
938
939 return 0;
940
941out3:
942 unregister_blkdev(major, name);
943out2:
944 blk_cleanup_queue(pd_queue);
945out1:
946 return -ENODEV;
947}
948
949static void __exit pd_exit(void)
950{
951 struct pd_unit *disk;
952 int unit;
953 unregister_blkdev(major, name);
954 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
955 struct gendisk *p = disk->gd;
956 if (p) {
957 disk->gd = NULL;
958 del_gendisk(p);
959 put_disk(p);
960 pi_release(disk->pi);
961 }
962 }
963 blk_cleanup_queue(pd_queue);
964}
965
966MODULE_LICENSE("GPL");
967module_init(pd_init)
968module_exit(pd_exit)
969