Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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