home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / drivers / scsi / sr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  21.7 KB  |  747 lines

  1. /*
  2.  *      sr.c by David Giller
  3.  *
  4.  *      adapted from:
  5.  *    sd.c Copyright (C) 1992 Drew Eckhardt 
  6.  *    Linux scsi disk driver by
  7.  *        Drew Eckhardt 
  8.  *
  9.  *    <drew@colorado.edu>
  10.  *
  11.  *       Modified by Eric Youngdale eric@tantalus.nrl.navy.mil to
  12.  *       add scatter-gather, multiple outstanding request, and other
  13.  *       enhancements.
  14.  */
  15.  
  16. #include <linux/fs.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <asm/system.h>
  22.  
  23. #define MAJOR_NR SCSI_CDROM_MAJOR
  24. #include "../block/blk.h"
  25. #include "scsi.h"
  26. #include "hosts.h"
  27. #include "sr.h"
  28. #include "scsi_ioctl.h"   /* For the door lock/unlock commands */
  29. #include "constants.h"
  30.  
  31. #define MAX_RETRIES 1
  32. #define SR_TIMEOUT 500
  33.  
  34. int NR_SR=0;
  35. int MAX_SR=0;
  36. Scsi_CD * scsi_CDs;
  37. static int * sr_sizes;
  38.  
  39. static int * sr_blocksizes;
  40.  
  41. static int sr_open(struct inode *, struct file *);
  42. static void get_sectorsize(int);
  43.  
  44. extern int sr_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  45.  
  46. void requeue_sr_request (Scsi_Cmnd * SCpnt);
  47.  
  48. static void sr_release(struct inode * inode, struct file * file)
  49. {
  50.     sync_dev(inode->i_rdev);
  51.     if(! --scsi_CDs[MINOR(inode->i_rdev)].device->access_count)
  52.       sr_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
  53. }
  54.  
  55. static struct file_operations sr_fops = 
  56. {
  57.     NULL,            /* lseek - default */
  58.     block_read,        /* read - general block-dev read */
  59.     block_write,        /* write - general block-dev write */
  60.     NULL,            /* readdir - bad */
  61.     NULL,            /* select */
  62.     sr_ioctl,        /* ioctl */
  63.     NULL,            /* mmap */
  64.     sr_open,               /* no special open code */
  65.     sr_release,        /* release */
  66.     NULL            /* fsync */
  67. };
  68.  
  69. /*
  70.  * This function checks to see if the media has been changed in the
  71.  * CDROM drive.  It is possible that we have already sensed a change,
  72.  * or the drive may have sensed one and not yet reported it.  We must
  73.  * be ready for either case. This function always reports the current
  74.  * value of the changed bit.  If flag is 0, then the changed bit is reset.
  75.  * This function could be done as an ioctl, but we would need to have
  76.  * an inode for that to work, and we do not always have one.
  77.  */
  78.  
  79. int check_cdrom_media_change(int full_dev, int flag){
  80.     int retval, target;
  81.     struct inode inode;
  82.  
  83.     target =  MINOR(full_dev);
  84.  
  85.     if (target >= NR_SR) {
  86.         printk("CD-ROM request error: invalid device.\n");
  87.         return 0;
  88.     };
  89.  
  90.     inode.i_rdev = full_dev;  /* This is all we really need here */
  91.     retval = sr_ioctl(&inode, NULL, SCSI_IOCTL_TEST_UNIT_READY, 0);
  92.  
  93.     if(retval){ /* Unable to test, unit probably not ready.  This usually
  94.              means there is no disc in the drive.  Mark as changed,
  95.              and we will figure it out later once the drive is
  96.              available again.  */
  97.  
  98.       scsi_CDs[target].device->changed = 1;
  99.       return 1; /* This will force a flush, if called from
  100.                check_disk_change */
  101.     };
  102.  
  103.     retval = scsi_CDs[target].device->changed;
  104.     if(!flag) scsi_CDs[target].device->changed = 0;
  105.     return retval;
  106. }
  107.  
  108. /*
  109.  * rw_intr is the interrupt routine for the device driver.  It will be notified on the 
  110.  * end of a SCSI read / write, and will take on of several actions based on success or failure.
  111.  */
  112.  
  113. static void rw_intr (Scsi_Cmnd * SCpnt)
  114. {
  115.     int result = SCpnt->result;
  116.     int this_count = SCpnt->this_count;
  117.  
  118. #ifdef DEBUG
  119.     printk("sr.c done: %x %x\n",result, SCpnt->request.bh->b_data);
  120. #endif
  121.     if (!result)
  122.         { /* No error */
  123.           if (SCpnt->use_sg == 0) {
  124.             if (SCpnt->buffer != SCpnt->request.buffer)
  125.               {
  126.             int offset;
  127.             offset = (SCpnt->request.sector % 4) << 9;
  128.             memcpy((char *)SCpnt->request.buffer, 
  129.                    (char *)SCpnt->buffer + offset, 
  130.                    this_count << 9);
  131.             /* Even though we are not using scatter-gather, we look
  132.                ahead and see if there is a linked request for the
  133.                other half of this buffer.  If there is, then satisfy
  134.                it. */
  135.             if((offset == 0) && this_count == 2 &&
  136.                SCpnt->request.nr_sectors > this_count && 
  137.                SCpnt->request.bh &&
  138.                SCpnt->request.bh->b_reqnext &&
  139.                SCpnt->request.bh->b_reqnext->b_size == 1024) {
  140.               memcpy((char *)SCpnt->request.bh->b_reqnext->b_data, 
  141.                  (char *)SCpnt->buffer + 1024, 
  142.                  1024);
  143.               this_count += 2;
  144.             };
  145.             
  146.             scsi_free(SCpnt->buffer, 2048);
  147.               }
  148.           } else {
  149.             struct scatterlist * sgpnt;
  150.             int i;
  151.             sgpnt = (struct scatterlist *) SCpnt->buffer;
  152.             for(i=0; i<SCpnt->use_sg; i++) {
  153.               if (sgpnt[i].alt_address) {
  154.             if (sgpnt[i].alt_address != sgpnt[i].address) {
  155.               memcpy(sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
  156.             };
  157.             scsi_free(sgpnt[i].address, sgpnt[i].length);
  158.               };
  159.             };
  160.             scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
  161.             if(SCpnt->request.sector % 4) this_count -= 2;
  162. /* See   if there is a padding record at the end that needs to be removed */
  163.             if(this_count > SCpnt->request.nr_sectors)
  164.               this_count -= 2;
  165.           };
  166.  
  167. #ifdef DEBUG
  168.         printk("(%x %x %x) ",SCpnt->request.bh, SCpnt->request.nr_sectors, 
  169.                this_count);
  170. #endif
  171.         if (SCpnt->request.nr_sectors > this_count)
  172.             {     
  173.             SCpnt->request.errors = 0;
  174.             if (!SCpnt->request.bh)
  175.                 panic("sr.c: linked page request (%lx %x)",
  176.                   SCpnt->request.sector, this_count);
  177.             }
  178.  
  179.           end_scsi_request(SCpnt, 1, this_count);  /* All done */
  180.           requeue_sr_request(SCpnt);
  181.           return;
  182.         } /* Normal completion */
  183.  
  184.     /* We only come through here if we have an error of some kind */
  185.  
  186. /* Free up any indirection buffers we allocated for DMA purposes. */
  187.     if (SCpnt->use_sg) {
  188.       struct scatterlist * sgpnt;
  189.       int i;
  190.       sgpnt = (struct scatterlist *) SCpnt->buffer;
  191.       for(i=0; i<SCpnt->use_sg; i++) {
  192.         if (sgpnt[i].alt_address) {
  193.           scsi_free(sgpnt[i].address, sgpnt[i].length);
  194.         };
  195.       };
  196.       scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
  197.     } else {
  198.       if (SCpnt->buffer != SCpnt->request.buffer)
  199.         scsi_free(SCpnt->buffer, SCpnt->bufflen);
  200.     };
  201.  
  202.     if (driver_byte(result) != 0) {
  203.         if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
  204.             if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
  205.                 /* detected disc change.  set a bit and quietly refuse    */
  206.                 /* further access.                    */
  207.             
  208.                 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->changed = 1;
  209.                 end_scsi_request(SCpnt, 0, this_count);
  210.                     requeue_sr_request(SCpnt);
  211.                 return;
  212.             }
  213.         }
  214.         
  215.         if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
  216.             printk("CD-ROM error: Drive reports ILLEGAL REQUEST.\n");
  217.             if (scsi_CDs[DEVICE_NR(SCpnt->request.dev)].ten) {
  218.                 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].ten = 0;
  219.                 requeue_sr_request(SCpnt);
  220.                 result = 0;
  221.                 return;
  222.             } else {
  223.               printk("CD-ROM error: Drive reports %d.\n", SCpnt->sense_buffer[2]);                
  224.               end_scsi_request(SCpnt, 0, this_count);
  225.               requeue_sr_request(SCpnt); /* Do next request */
  226.               return;
  227.             }
  228.  
  229.         }
  230.  
  231.         if (SCpnt->sense_buffer[2] == NOT_READY) {
  232.             printk("CDROM not ready.  Make sure you have a disc in the drive.\n");
  233.             end_scsi_request(SCpnt, 0, this_count);
  234.             requeue_sr_request(SCpnt); /* Do next request */
  235.             return;
  236.         };
  237.           }
  238.     
  239.     /* We only get this far if we have an error we have not recognized */
  240.     if(result) {
  241.       printk("SCSI CD error : host %d id %d lun %d return code = %03x\n", 
  242.          scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->host->host_no, 
  243.          scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->id,
  244.          scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->lun,
  245.          result);
  246.         
  247.       if (status_byte(result) == CHECK_CONDITION)
  248.           print_sense("sr", SCpnt);
  249.       
  250.       end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
  251.       requeue_sr_request(SCpnt);
  252.   }
  253. }
  254.  
  255. static int sr_open(struct inode * inode, struct file * filp)
  256. {
  257.     if(MINOR(inode->i_rdev) >= NR_SR || 
  258.        !scsi_CDs[MINOR(inode->i_rdev)].device) return -ENODEV;   /* No such device */
  259.  
  260.         check_disk_change(inode->i_rdev);
  261.  
  262.     if(!scsi_CDs[MINOR(inode->i_rdev)].device->access_count++)
  263.       sr_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
  264.  
  265.     /* If this device did not have media in the drive at boot time, then
  266.        we would have been unable to get the sector size.  Check to see if
  267.        this is the case, and try again.
  268.        */
  269.  
  270.     if(scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size)
  271.       get_sectorsize(MINOR(inode->i_rdev));
  272.  
  273.     return 0;
  274. }
  275.  
  276.  
  277. /*
  278.  * do_sr_request() is the request handler function for the sr driver.  Its function in life 
  279.  * is to take block device requests, and translate them to SCSI commands.
  280.  */
  281.     
  282. static void do_sr_request (void)
  283. {
  284.   Scsi_Cmnd * SCpnt = NULL;
  285.   struct request * req = NULL;
  286.   int flag = 0;
  287.   unsigned long flags;
  288.  
  289.   while (1==1){
  290.     save_flags (flags);
  291.     cli();
  292.     if (CURRENT != NULL && CURRENT->dev == -1) {
  293.       restore_flags (flags);
  294.       return;
  295.     };
  296.  
  297.     INIT_SCSI_REQUEST;
  298.  
  299.     if (flag++ == 0)
  300.       SCpnt = allocate_device(&CURRENT,
  301.                   scsi_CDs[DEVICE_NR(MINOR(CURRENT->dev))].device->index, 0); 
  302.     else SCpnt = NULL;
  303.     restore_flags (flags);
  304.  
  305. /* This is a performance enhancement.  We dig down into the request list and
  306.    try and find a queueable request (i.e. device not busy, and host able to
  307.    accept another command.  If we find one, then we queue it. This can
  308.    make a big difference on systems with more than one disk drive.  We want
  309.    to have the interrupts off when monkeying with the request list, because
  310.    otherwise the kernel might try and slip in a request inbetween somewhere. */
  311.  
  312.     if (!SCpnt && NR_SR > 1){
  313.       struct request *req1;
  314.       req1 = NULL;
  315.       save_flags (flags);
  316.       cli();
  317.       req = CURRENT;
  318.       while(req){
  319.     SCpnt = request_queueable(req,
  320.                   scsi_CDs[DEVICE_NR(MINOR(req->dev))].device->index);
  321.     if(SCpnt) break;
  322.     req1 = req;
  323.     req = req->next;
  324.       };
  325.       if (SCpnt && req->dev == -1) {
  326.     if (req == CURRENT) 
  327.       CURRENT = CURRENT->next;
  328.     else
  329.       req1->next = req->next;
  330.       };
  331.       restore_flags(flags);
  332.     };
  333.     
  334.     if (!SCpnt)
  335.       return; /* Could not find anything to do */
  336.     
  337.   wake_up(&wait_for_request);
  338.  
  339. /* Queue command */
  340.   requeue_sr_request(SCpnt);
  341.   };  /* While */
  342. }    
  343.  
  344. void requeue_sr_request (Scsi_Cmnd * SCpnt)
  345. {
  346.     unsigned int dev, block, realcount;
  347.     unsigned char cmd[10], *buffer, tries;
  348.     int this_count, start, end_rec;
  349.  
  350.     tries = 2;
  351.  
  352.       repeat:
  353.     if(SCpnt->request.dev <= 0) {
  354.       do_sr_request();
  355.       return;
  356.     }
  357.  
  358.     dev =  MINOR(SCpnt->request.dev);
  359.     block = SCpnt->request.sector;    
  360.     buffer = NULL;
  361.     this_count = 0;
  362.  
  363.     if (dev >= NR_SR)
  364.         {
  365.         /* printk("CD-ROM request error: invalid device.\n");            */
  366.         end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
  367.         tries = 2;
  368.         goto repeat;
  369.         }
  370.  
  371.     if (!scsi_CDs[dev].use)
  372.         {
  373.         /* printk("CD-ROM request error: device marked not in use.\n");        */
  374.         end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
  375.         tries = 2;
  376.         goto repeat;
  377.         }
  378.  
  379.     if (scsi_CDs[dev].device->changed)
  380.             {
  381. /* 
  382.  * quietly refuse to do anything to a changed disc until the changed bit has been reset
  383.  */
  384.         /* printk("CD-ROM has been changed.  Prohibiting further I/O.\n");    */
  385.         end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
  386.         tries = 2;
  387.         goto repeat;
  388.         }
  389.     
  390.     switch (SCpnt->request.cmd)
  391.         {
  392.         case WRITE:         
  393.             end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
  394.             goto repeat;
  395.             break;
  396.         case READ : 
  397.                 cmd[0] = READ_6;
  398.             break;
  399.         default : 
  400.             panic ("Unknown sr command %d\n", SCpnt->request.cmd);
  401.         }
  402.     
  403.     cmd[1] = (SCpnt->lun << 5) & 0xe0;
  404.  
  405. /*
  406.            Now do the grungy work of figuring out which sectors we need, and
  407.        where in memory we are going to put them.
  408.  
  409.        The variables we need are:
  410.  
  411.        this_count= number of 512 byte sectors being read 
  412.        block     = starting cdrom sector to read.
  413.        realcount = # of cdrom sectors to read
  414.  
  415.        The major difference between a scsi disk and a scsi cdrom
  416. is that we will always use scatter-gather if we can, because we can
  417. work around the fact that the buffer cache has a block size of 1024,
  418. and we have 2048 byte sectors.  This code should work for buffers that
  419. are any multiple of 512 bytes long.  */
  420.  
  421.     SCpnt->use_sg = 0;
  422.  
  423.     if (SCpnt->host->sg_tablesize > 0 &&
  424.         (!need_isa_buffer ||
  425.         dma_free_sectors >= 10)) {
  426.       struct buffer_head * bh;
  427.       struct scatterlist * sgpnt;
  428.       int count, this_count_max;
  429.       bh = SCpnt->request.bh;
  430.       this_count = 0;
  431.       count = 0;
  432.       this_count_max = (scsi_CDs[dev].ten ? 0xffff : 0xff) << 4;
  433.       /* Calculate how many links we can use.  First see if we need
  434.        a padding record at the start */
  435.       this_count = SCpnt->request.sector % 4;
  436.       if(this_count) count++;
  437.       while(bh && count < SCpnt->host->sg_tablesize) {
  438.         if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
  439.         this_count += (bh->b_size >> 9);
  440.         count++;
  441.         bh = bh->b_reqnext;
  442.       };
  443.       /* Fix up in case of an odd record at the end */
  444.       end_rec = 0;
  445.       if(this_count % 4) {
  446.         if (count < SCpnt->host->sg_tablesize) {
  447.           count++;
  448.           end_rec = (4 - (this_count % 4)) << 9;
  449.           this_count += 4 - (this_count % 4);
  450.         } else {
  451.           count--;
  452.           this_count -= (this_count % 4);
  453.         };
  454.       };
  455.       SCpnt->use_sg = count;  /* Number of chains */
  456.       count = 512;/* scsi_malloc can only allocate in chunks of 512 bytes*/
  457.       while( count < (SCpnt->use_sg * sizeof(struct scatterlist))) 
  458.         count = count << 1;
  459.       SCpnt->sglist_len = count;
  460.       sgpnt = (struct scatterlist * ) scsi_malloc(count);
  461.       if (!sgpnt) {
  462.         printk("Warning - running *really* short on DMA buffers\n");
  463.         SCpnt->use_sg = 0;  /* No memory left - bail out */
  464.       } else {
  465.         buffer = (unsigned char *) sgpnt;
  466.         count = 0;
  467.         bh = SCpnt->request.bh;
  468.         if(SCpnt->request.sector % 4) {
  469.           sgpnt[count].length = (SCpnt->request.sector % 4) << 9;
  470.           sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
  471.           if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
  472.           sgpnt[count].alt_address = sgpnt[count].address; /* Flag to delete
  473.                                   if needed */
  474.           count++;
  475.         };
  476.         for(bh = SCpnt->request.bh; count < SCpnt->use_sg; 
  477.         count++, bh = bh->b_reqnext) {
  478.           if (bh) { /* Need a placeholder at the end of the record? */
  479.         sgpnt[count].address = bh->b_data;
  480.         sgpnt[count].length = bh->b_size;
  481.         sgpnt[count].alt_address = NULL;
  482.           } else {
  483.         sgpnt[count].address = (char *) scsi_malloc(end_rec);
  484.         if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
  485.         sgpnt[count].length = end_rec;
  486.         sgpnt[count].alt_address = sgpnt[count].address;
  487.         if (count+1 != SCpnt->use_sg) panic("Bad sr request list");
  488.         break;
  489.           };
  490.           if (((int) sgpnt[count].address) + sgpnt[count].length > 
  491.           ISA_DMA_THRESHOLD & (SCpnt->host->unchecked_isa_dma)) {
  492.         sgpnt[count].alt_address = sgpnt[count].address;
  493.         /* We try and avoid exhausting the DMA pool, since it is easier
  494.            to control usage here.  In other places we might have a more
  495.            pressing need, and we would be screwed if we ran out */
  496.         if(dma_free_sectors < (sgpnt[count].length >> 9) + 5) {
  497.           sgpnt[count].address = NULL;
  498.         } else {
  499.           sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
  500.         };
  501. /* If we start running low on DMA buffers, we abort the scatter-gather
  502.    operation, and free all of the memory we have allocated.  We want to
  503.    ensure that all scsi operations are able to do at least a non-scatter/gather
  504.    operation */
  505.         if(sgpnt[count].address == NULL){ /* Out of dma memory */
  506.           printk("Warning: Running low on SCSI DMA buffers");
  507.           /* Try switching back to a non scatter-gather operation. */
  508.           while(--count >= 0){
  509.             if(sgpnt[count].alt_address) 
  510.               scsi_free(sgpnt[count].address, sgpnt[count].length);
  511.           };
  512.           SCpnt->use_sg = 0;
  513.           scsi_free(buffer, SCpnt->sglist_len);
  514.           break;
  515.         }; /* if address == NULL */
  516.           };  /* if need DMA fixup */
  517.         };  /* for loop to fill list */
  518. #ifdef DEBUG
  519.         printk("SG: %d %d %d %d %d *** ",SCpnt->use_sg, SCpnt->request.sector,
  520.            this_count, 
  521.            SCpnt->request.current_nr_sectors,
  522.            SCpnt->request.nr_sectors);
  523.         for(count=0; count<SCpnt->use_sg; count++)
  524.           printk("SGlist: %d %x %x %x\n", count,
  525.              sgpnt[count].address, 
  526.              sgpnt[count].alt_address, 
  527.              sgpnt[count].length);
  528. #endif
  529.       };  /* Able to allocate scatter-gather list */
  530.     };
  531.     
  532.     if (SCpnt->use_sg == 0){
  533.       /* We cannot use scatter-gather.  Do this the old fashion way */
  534.       if (!SCpnt->request.bh)      
  535.         this_count = SCpnt->request.nr_sectors;
  536.       else
  537.         this_count = (SCpnt->request.bh->b_size >> 9);
  538.       
  539.       start = block % 4;
  540.       if (start)
  541.         {                  
  542.           this_count = ((this_count > 4 - start) ? 
  543.                 (4 - start) : (this_count));
  544.           buffer = (unsigned char *) scsi_malloc(2048);
  545.         } 
  546.       else if (this_count < 4)
  547.         {
  548.           buffer = (unsigned char *) scsi_malloc(2048);
  549.         }
  550.       else
  551.         {
  552.           this_count -= this_count % 4;
  553.           buffer = (unsigned char *) SCpnt->request.buffer;
  554.           if (((int) buffer) + (this_count << 9) > ISA_DMA_THRESHOLD & 
  555.           (SCpnt->host->unchecked_isa_dma))
  556.         buffer = (unsigned char *) scsi_malloc(this_count << 9);
  557.         }
  558.     };
  559.  
  560.     if (scsi_CDs[dev].sector_size == 2048)
  561.       block = block >> 2; /* These are the sectors that the cdrom uses */
  562.     else
  563.       block = block & 0xfffffffc;
  564.  
  565.     realcount = (this_count + 3) / 4;
  566.  
  567.     if (scsi_CDs[dev].sector_size == 512) realcount = realcount << 2;
  568.  
  569.     if (((realcount > 0xff) || (block > 0x1fffff)) && scsi_CDs[dev].ten) 
  570.         {
  571.         if (realcount > 0xffff)
  572.                 {
  573.             realcount = 0xffff;
  574.             this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
  575.             }
  576.  
  577.         cmd[0] += READ_10 - READ_6 ;
  578.         cmd[2] = (unsigned char) (block >> 24) & 0xff;
  579.         cmd[3] = (unsigned char) (block >> 16) & 0xff;
  580.         cmd[4] = (unsigned char) (block >> 8) & 0xff;
  581.         cmd[5] = (unsigned char) block & 0xff;
  582.         cmd[6] = cmd[9] = 0;
  583.         cmd[7] = (unsigned char) (realcount >> 8) & 0xff;
  584.         cmd[8] = (unsigned char) realcount & 0xff;
  585.         }
  586.     else
  587.         {
  588.         if (realcount > 0xff)
  589.                 {
  590.             realcount = 0xff;
  591.             this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
  592.                 }
  593.     
  594.         cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  595.         cmd[2] = (unsigned char) ((block >> 8) & 0xff);
  596.         cmd[3] = (unsigned char) block & 0xff;
  597.         cmd[4] = (unsigned char) realcount;
  598.         cmd[5] = 0;
  599.         }   
  600.  
  601. #ifdef DEBUG
  602.     int i;
  603.     printk("ReadCD: %d %d %d %d\n",block, realcount, buffer, this_count);
  604.     printk("Use sg: %d\n", SCpnt->use_sg);
  605.     printk("Dumping command: ");
  606.     for(i=0; i<12; i++) printk("%2.2x ", cmd[i]);
  607.     printk("\n");
  608. };
  609. #endif
  610.  
  611.     SCpnt->this_count = this_count;
  612.     scsi_do_cmd (SCpnt, (void *) cmd, buffer, 
  613.              realcount * scsi_CDs[dev].sector_size, 
  614.              rw_intr, SR_TIMEOUT, MAX_RETRIES);
  615. }
  616.  
  617. unsigned long sr_init1(unsigned long mem_start, unsigned long mem_end){
  618.   scsi_CDs = (Scsi_CD *) mem_start;
  619.   mem_start += MAX_SR * sizeof(Scsi_CD);
  620.   return mem_start;
  621. };
  622.  
  623. void sr_attach(Scsi_Device * SDp){
  624.   scsi_CDs[NR_SR++].device = SDp;
  625.   if(NR_SR > MAX_SR) panic ("scsi_devices corrupt (sr)");
  626. };
  627.  
  628. static void sr_init_done (Scsi_Cmnd * SCpnt)
  629. {
  630.   struct request * req;
  631.   struct task_struct * p;
  632.   
  633.   req = &SCpnt->request;
  634.   req->dev = 0xfffe; /* Busy, but indicate request done */
  635.   
  636.   if ((p = req->waiting) != NULL) {
  637.     req->waiting = NULL;
  638.     p->state = TASK_RUNNING;
  639.     if (p->counter > current->counter)
  640.       need_resched = 1;
  641.   }
  642. }
  643.  
  644. static void get_sectorsize(int i){
  645.   unsigned char cmd[10];
  646.   unsigned char buffer[513];
  647.   int the_result, retries;
  648.   Scsi_Cmnd * SCpnt;
  649.   
  650.   SCpnt = allocate_device(NULL, scsi_CDs[i].device->index, 1);
  651.  
  652.   retries = 3;
  653.   do {
  654.     cmd[0] = READ_CAPACITY;
  655.     cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
  656.     memset ((void *) &cmd[2], 0, 8);
  657.     SCpnt->request.dev = 0xffff;  /* Mark as really busy */
  658.     
  659.     scsi_do_cmd (SCpnt,
  660.          (void *) cmd, (void *) buffer,
  661.          512, sr_init_done,  SR_TIMEOUT,
  662.          MAX_RETRIES);
  663.     
  664.     if (current == task[0])
  665.       while(SCpnt->request.dev != 0xfffe);
  666.     else
  667.       if (SCpnt->request.dev != 0xfffe){
  668.     SCpnt->request.waiting = current;
  669.     current->state = TASK_UNINTERRUPTIBLE;
  670.     while (SCpnt->request.dev != 0xfffe) schedule();
  671.       };
  672.     
  673.     the_result = SCpnt->result;
  674.     retries--;
  675.     
  676.   } while(the_result && retries);
  677.   
  678.   SCpnt->request.dev = -1;  /* Mark as not busy */
  679.   
  680.   wake_up(&scsi_devices[SCpnt->index].device_wait); 
  681.  
  682.   if (the_result) {
  683.     scsi_CDs[i].capacity = 0x1fffff;
  684.     scsi_CDs[i].sector_size = 2048;  /* A guess, just in case */
  685.     scsi_CDs[i].needs_sector_size = 1;
  686.   } else {
  687.     scsi_CDs[i].capacity = (buffer[0] << 24) |
  688.       (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
  689.     scsi_CDs[i].sector_size = (buffer[4] << 24) |
  690.       (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  691.     if(scsi_CDs[i].sector_size == 0) scsi_CDs[i].sector_size = 2048;
  692.     if(scsi_CDs[i].sector_size != 2048 && 
  693.        scsi_CDs[i].sector_size != 512) {
  694.       printk ("scd%d : unsupported sector size %d.\n",
  695.           i, scsi_CDs[i].sector_size);
  696.       scsi_CDs[i].capacity = 0;
  697.     };
  698.     if(scsi_CDs[i].sector_size == 2048)
  699.       scsi_CDs[i].capacity *= 4;
  700.     scsi_CDs[i].needs_sector_size = 0;
  701.   };
  702. }
  703.  
  704. unsigned long sr_init(unsigned long memory_start, unsigned long memory_end)
  705. {
  706.     int i;
  707.  
  708.     if (register_blkdev(MAJOR_NR,"sr",&sr_fops)) {
  709.         printk("Unable to get major %d for SCSI-CD\n",MAJOR_NR);
  710.         return memory_start;
  711.     }
  712.     if(MAX_SR == 0) return memory_start;
  713.  
  714.     sr_sizes = (int *) memory_start;
  715.     memory_start += MAX_SR * sizeof(int);
  716.     memset(sr_sizes, 0, MAX_SR * sizeof(int));
  717.  
  718.     sr_blocksizes = (int *) memory_start;
  719.     memory_start += MAX_SR * sizeof(int);
  720.     for(i=0;i<MAX_SR;i++) sr_blocksizes[i] = 2048;
  721.     blksize_size[MAJOR_NR] = sr_blocksizes;
  722.  
  723.     for (i = 0; i < NR_SR; ++i)
  724.         {
  725.           get_sectorsize(i);
  726.           printk("Scd sectorsize = %d bytes\n", scsi_CDs[i].sector_size);
  727.           scsi_CDs[i].use = 1;
  728.           scsi_CDs[i].ten = 1;
  729.           scsi_CDs[i].remap = 1;
  730.           sr_sizes[i] = scsi_CDs[i].capacity;
  731.         }
  732.  
  733.     blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
  734.     blk_size[MAJOR_NR] = sr_sizes;    
  735.  
  736.     /* If our host adapter is capable of scatter-gather, then we increase
  737.        the read-ahead to 16 blocks (32 sectors).  If not, we use
  738.        a two block (4 sector) read ahead. */
  739.     if(scsi_CDs[0].device->host->sg_tablesize)
  740.       read_ahead[MAJOR_NR] = 32;  /* 32 sector read-ahead.  Always removable. */
  741.     else
  742.       read_ahead[MAJOR_NR] = 4;  /* 4 sector read-ahead */
  743.  
  744.     return memory_start;
  745. }    
  746.