root/drivers/scsi/sr.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sr_release
  2. check_cdrom_media_change
  3. rw_intr
  4. sr_photocd
  5. sr_open
  6. do_sr_request
  7. requeue_sr_request
  8. sr_detect
  9. sr_attach
  10. sr_init_done
  11. get_sectorsize
  12. sr_init
  13. sr_finish
  14. sr_detach

   1 /*
   2  *      sr.c Copyright (C) 1992 David Giller
   3  *           Copyright (C) 1993, 1994 Eric Youngdale
   4  *
   5  *      adapted from:
   6  *      sd.c Copyright (C) 1992 Drew Eckhardt 
   7  *      Linux scsi disk driver by
   8  *              Drew Eckhardt 
   9  *
  10  *      <drew@colorado.edu>
  11  *
  12  *       Modified by Eric Youngdale ericy@cais.com to
  13  *       add scatter-gather, multiple outstanding request, and other
  14  *       enhancements.
  15  */
  16 
  17 #include <linux/fs.h>
  18 #include <linux/kernel.h>
  19 #include <linux/sched.h>
  20 #include <linux/string.h>
  21 #include <linux/errno.h>
  22 #include <linux/cdrom.h>
  23 #include <asm/system.h>
  24 
  25 #define MAJOR_NR SCSI_CDROM_MAJOR
  26 #include "../block/blk.h"
  27 #include "scsi.h"
  28 #include "hosts.h"
  29 #include "sr.h"
  30 #include "scsi_ioctl.h"   /* For the door lock/unlock commands */
  31 #include "constants.h"
  32 
  33 #define MAX_RETRIES 3
  34 #define SR_TIMEOUT 5000
  35 
  36 static void sr_init(void);
  37 static void sr_finish(void);
  38 static int sr_attach(Scsi_Device *);
  39 static int sr_detect(Scsi_Device *);
  40 static void sr_detach(Scsi_Device *);
  41 
  42 struct Scsi_Device_Template sr_template = {NULL, "cdrom", "sr", TYPE_ROM, 
  43                                              SCSI_CDROM_MAJOR, 0, 0, 0, 1,
  44                                              sr_detect, sr_init,
  45                                              sr_finish, sr_attach, sr_detach};
  46 
  47 Scsi_CD * scsi_CDs;
  48 static int * sr_sizes;
  49 
  50 static int * sr_blocksizes;
  51 
  52 static int sr_open(struct inode *, struct file *);
  53 static void get_sectorsize(int);
  54 
  55 extern int sr_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  56 
  57 void requeue_sr_request (Scsi_Cmnd * SCpnt);
  58 static int check_cdrom_media_change(dev_t);
  59 
  60 static void sr_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
  61 {
  62         sync_dev(inode->i_rdev);
  63         if(! --scsi_CDs[MINOR(inode->i_rdev)].device->access_count)
  64           sr_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
  65         if (scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)
  66           (*scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)--;
  67 }
  68 
  69 static struct file_operations sr_fops = 
  70 {
  71         NULL,                   /* lseek - default */
  72         block_read,             /* read - general block-dev read */
  73         block_write,            /* write - general block-dev write */
  74         NULL,                   /* readdir - bad */
  75         NULL,                   /* select */
  76         sr_ioctl,               /* ioctl */
  77         NULL,                   /* mmap */
  78         sr_open,                /* special open code */
  79         sr_release,             /* release */
  80         NULL,                   /* fsync */
  81         NULL,                   /* fasync */
  82         check_cdrom_media_change,  /* Disk change */
  83         NULL                    /* revalidate */
  84 };
  85 
  86 /*
  87  * This function checks to see if the media has been changed in the
  88  * CDROM drive.  It is possible that we have already sensed a change,
  89  * or the drive may have sensed one and not yet reported it.  We must
  90  * be ready for either case. This function always reports the current
  91  * value of the changed bit.  If flag is 0, then the changed bit is reset.
  92  * This function could be done as an ioctl, but we would need to have
  93  * an inode for that to work, and we do not always have one.
  94  */
  95 
  96 int check_cdrom_media_change(dev_t full_dev){
     /* [previous][next][first][last][top][bottom][index][help] */
  97         int retval, target;
  98         struct inode inode;
  99         int flag = 0;
 100 
 101         target =  MINOR(full_dev);
 102 
 103         if (target >= sr_template.nr_dev) {
 104                 printk("CD-ROM request error: invalid device.\n");
 105                 return 0;
 106         };
 107 
 108         inode.i_rdev = full_dev;  /* This is all we really need here */
 109         retval = sr_ioctl(&inode, NULL, SCSI_IOCTL_TEST_UNIT_READY, 0);
 110 
 111         if(retval){ /* Unable to test, unit probably not ready.  This usually
 112                      means there is no disc in the drive.  Mark as changed,
 113                      and we will figure it out later once the drive is
 114                      available again.  */
 115 
 116           scsi_CDs[target].device->changed = 1;
 117           return 1; /* This will force a flush, if called from
 118                        check_disk_change */
 119         };
 120 
 121         retval = scsi_CDs[target].device->changed;
 122         if(!flag) {
 123           scsi_CDs[target].device->changed = 0;
 124           /* If the disk changed, the capacity will now be different,
 125              so we force a re-read of this information */
 126           if (retval) scsi_CDs[target].needs_sector_size = 1;
 127         };
 128         return retval;
 129 }
 130 
 131 /*
 132  * rw_intr is the interrupt routine for the device driver.  It will be notified on the 
 133  * end of a SCSI read / write, and will take on of several actions based on success or failure.
 134  */
 135 
 136 static void rw_intr (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 137 {
 138         int result = SCpnt->result;
 139         int this_count = SCpnt->this_count;
 140 
 141 #ifdef DEBUG
 142         printk("sr.c done: %x %x\n",result, SCpnt->request.bh->b_data);
 143 #endif
 144         if (!result)
 145                 { /* No error */
 146                   if (SCpnt->use_sg == 0) {
 147                     if (SCpnt->buffer != SCpnt->request.buffer)
 148                       {
 149                         int offset;
 150                         offset = (SCpnt->request.sector % 4) << 9;
 151                         memcpy((char *)SCpnt->request.buffer, 
 152                                (char *)SCpnt->buffer + offset, 
 153                                this_count << 9);
 154                         /* Even though we are not using scatter-gather, we look
 155                            ahead and see if there is a linked request for the
 156                            other half of this buffer.  If there is, then satisfy
 157                            it. */
 158                         if((offset == 0) && this_count == 2 &&
 159                            SCpnt->request.nr_sectors > this_count && 
 160                            SCpnt->request.bh &&
 161                            SCpnt->request.bh->b_reqnext &&
 162                            SCpnt->request.bh->b_reqnext->b_size == 1024) {
 163                           memcpy((char *)SCpnt->request.bh->b_reqnext->b_data, 
 164                                  (char *)SCpnt->buffer + 1024, 
 165                                  1024);
 166                           this_count += 2;
 167                         };
 168                         
 169                         scsi_free(SCpnt->buffer, 2048);
 170                       }
 171                   } else {
 172                     struct scatterlist * sgpnt;
 173                     int i;
 174                     sgpnt = (struct scatterlist *) SCpnt->buffer;
 175                     for(i=0; i<SCpnt->use_sg; i++) {
 176                       if (sgpnt[i].alt_address) {
 177                         if (sgpnt[i].alt_address != sgpnt[i].address) {
 178                           memcpy(sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
 179                         };
 180                         scsi_free(sgpnt[i].address, sgpnt[i].length);
 181                       };
 182                     };
 183                     scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
 184                     if(SCpnt->request.sector % 4) this_count -= 2;
 185 /* See   if there is a padding record at the end that needs to be removed */
 186                     if(this_count > SCpnt->request.nr_sectors)
 187                       this_count -= 2;
 188                   };
 189 
 190 #ifdef DEBUG
 191                 printk("(%x %x %x) ",SCpnt->request.bh, SCpnt->request.nr_sectors, 
 192                        this_count);
 193 #endif
 194                 if (SCpnt->request.nr_sectors > this_count)
 195                         {        
 196                         SCpnt->request.errors = 0;
 197                         if (!SCpnt->request.bh)
 198                             panic("sr.c: linked page request (%lx %x)",
 199                                   SCpnt->request.sector, this_count);
 200                         }
 201 
 202                   SCpnt = end_scsi_request(SCpnt, 1, this_count);  /* All done */
 203                   requeue_sr_request(SCpnt);
 204                   return;
 205                 } /* Normal completion */
 206 
 207         /* We only come through here if we have an error of some kind */
 208 
 209 /* Free up any indirection buffers we allocated for DMA purposes. */
 210         if (SCpnt->use_sg) {
 211           struct scatterlist * sgpnt;
 212           int i;
 213           sgpnt = (struct scatterlist *) SCpnt->buffer;
 214           for(i=0; i<SCpnt->use_sg; i++) {
 215             if (sgpnt[i].alt_address) {
 216               scsi_free(sgpnt[i].address, sgpnt[i].length);
 217             };
 218           };
 219           scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
 220         } else {
 221           if (SCpnt->buffer != SCpnt->request.buffer)
 222             scsi_free(SCpnt->buffer, SCpnt->bufflen);
 223         };
 224 
 225         if (driver_byte(result) != 0) {
 226                 if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
 227                         if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
 228                                 /* detected disc change.  set a bit and quietly refuse  */
 229                                 /* further access.                                      */
 230                     
 231                                 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->changed = 1;
 232                                 SCpnt = end_scsi_request(SCpnt, 0, this_count);
 233                                 requeue_sr_request(SCpnt);
 234                                 return;
 235                         }
 236                 }
 237             
 238                 if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
 239                         printk("CD-ROM error: Drive reports ILLEGAL REQUEST.\n");
 240                         if (scsi_CDs[DEVICE_NR(SCpnt->request.dev)].ten) {
 241                                 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].ten = 0;
 242                                 requeue_sr_request(SCpnt);
 243                                 result = 0;
 244                                 return;
 245                         } else {
 246                           printk("CD-ROM error: Drive reports %d.\n", SCpnt->sense_buffer[2]);                          
 247                           SCpnt = end_scsi_request(SCpnt, 0, this_count);
 248                           requeue_sr_request(SCpnt); /* Do next request */
 249                           return;
 250                         }
 251 
 252                 }
 253 
 254                 if (SCpnt->sense_buffer[2] == NOT_READY) {
 255                         printk("CDROM not ready.  Make sure you have a disc in the drive.\n");
 256                         SCpnt = end_scsi_request(SCpnt, 0, this_count);
 257                         requeue_sr_request(SCpnt); /* Do next request */
 258                         return;
 259                 };
 260               }
 261         
 262         /* We only get this far if we have an error we have not recognized */
 263         if(result) {
 264           printk("SCSI CD error : host %d id %d lun %d return code = %03x\n", 
 265                  scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->host->host_no, 
 266                  scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->id,
 267                  scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->lun,
 268                  result);
 269             
 270           if (status_byte(result) == CHECK_CONDITION)
 271                   print_sense("sr", SCpnt);
 272           
 273           SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
 274           requeue_sr_request(SCpnt);
 275   }
 276 }
 277 
 278 /*
 279  * Here I tried to implement better support for PhotoCD's.
 280  * 
 281  * Much of this has do be done with vendor-specific SCSI-commands.
 282  * So I have to complete it step by step. Useful information is welcome.
 283  *
 284  * Actually works:
 285  *   - NEC:     Detection and support of multisession CD's. Special handling
 286  *              for XA-disks is not necessary.
 287  *     
 288  *   - TOSHIBA: setting density is done here now, mounting PhotoCD's should
 289  *              work now without running the program "set_density"
 290  *              People reported that it is necessary to eject and reinsert
 291  *              the CD after the set-density call to get this working for
 292  *              old drives.
 293  *              And some very new drives don't need this call any more...
 294  *              Multisession CD's are supported too.
 295  *
 296  * Dec 1994: completely rewritten, uses kernel_scsi_ioctl() now
 297  *
 298  *   kraxel@cs.tu-berlin.de (Gerd Knorr)
 299  */
 300 
 301 static void sr_photocd(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 302 {
 303   unsigned long   sector,min,sec,frame;
 304   unsigned char   buf[40];
 305   int             rc;
 306 
 307   if (!suser()) {
 308     /* I'm not the superuser, so SCSI_IOCTL_SEND_COMMAND isn't allowed for me.
 309      * That's why mpcd_sector will be initialized with zero, because I'm not
 310      * able to get the right value. Necessary only if access_count is 1, else
 311      * no disk change happened since the last call of this function and we can
 312      * keep the old value.
 313      */
 314     if (1 == scsi_CDs[MINOR(inode->i_rdev)].device->access_count)
 315       scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = 0;
 316     return;
 317   }
 318   
 319   switch(scsi_CDs[MINOR(inode->i_rdev)].device->manufacturer) {
 320 
 321   case SCSI_MAN_NEC:
 322 #ifdef DEBUG
 323     printk("sr_photocd: use NEC code\n");
 324 #endif
 325     memset(buf,0,40);
 326     *((unsigned long*)buf)   = 0;
 327     *((unsigned long*)buf+1) = 0x16;
 328     buf[8+0] = 0xde;
 329     buf[8+1] = 0x03;
 330     buf[8+2] = 0xb0;
 331     rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 332                            SCSI_IOCTL_SEND_COMMAND, buf);
 333     if (rc != 0) {
 334       printk("sr_photocd: ioctl error (NEC): 0x%x\n",rc);
 335       sector = 0;
 336     } else {
 337       min   = (unsigned long)buf[8+15]/16*10 + (unsigned long)buf[8+15]%16;
 338       sec   = (unsigned long)buf[8+16]/16*10 + (unsigned long)buf[8+16]%16;
 339       frame = (unsigned long)buf[8+17]/16*10 + (unsigned long)buf[8+17]%16;
 340       sector = min*60*75 + sec*75 + frame;
 341 #ifdef DEBUG
 342       if (sector) {
 343         printk("sr_photocd: multisession CD detected. start: %lu\n",sector);
 344       }
 345 #endif
 346     }
 347     break;
 348 
 349   case SCSI_MAN_TOSHIBA:
 350 #ifdef DEBUG
 351     printk("sr_photocd: use TOSHIBA code\n");
 352 #endif
 353     
 354     /* first I do a set_density-call (for reading XA-sectors) ... */
 355     memset(buf,0,40);
 356     *((unsigned long*)buf)   = 12;
 357     *((unsigned long*)buf+1) = 12;
 358     buf[8+0] = 0x15;
 359     buf[8+1] = (1 << 4);
 360     buf[8+4] = 12;
 361     buf[14+ 3] = 0x08;
 362     buf[14+ 4] = 0x83;
 363     buf[14+10] = 0x08;
 364     rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 365                            SCSI_IOCTL_SEND_COMMAND, buf);
 366     if (rc != 0) {
 367       printk("sr_photocd: ioctl error (TOSHIBA #1): 0x%x\n",rc);
 368     }
 369 
 370     /* ... and then I ask, if there is a multisession-Disk */
 371     memset(buf,0,40);
 372     *((unsigned long*)buf)   = 0;
 373     *((unsigned long*)buf+1) = 4;
 374     buf[8+0] = 0xc7;
 375     buf[8+1] = 3;
 376     rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 377                            SCSI_IOCTL_SEND_COMMAND, buf);
 378     if (rc != 0) {
 379       printk("sr_photocd: ioctl error (TOSHIBA #2): 0x%x\n",rc);
 380       sector = 0;
 381     } else {
 382       min   = (unsigned long)buf[8+1]/16*10 + (unsigned long)buf[8+1]%16;
 383       sec   = (unsigned long)buf[8+2]/16*10 + (unsigned long)buf[8+2]%16;
 384       frame = (unsigned long)buf[8+3]/16*10 + (unsigned long)buf[8+3]%16;
 385       sector = min*60*75 + sec*75 + frame;
 386       if (sector) {
 387         sector -= CD_BLOCK_OFFSET;
 388 #ifdef DEBUG
 389         printk("sr_photocd: multisession CD detected: start: %lu\n",sector);
 390 #endif
 391       }
 392     }
 393     break;
 394 
 395   case SCSI_MAN_UNKNOWN:
 396   default:
 397 #ifdef DEBUG
 398     printk("sr_photocd: unknown drive, no special multisession code\n");
 399 #endif
 400     sector = 0;
 401     break; }
 402 
 403   scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = sector;
 404   return;
 405 }
 406 
 407 static int sr_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409         if(MINOR(inode->i_rdev) >= sr_template.nr_dev || 
 410            !scsi_CDs[MINOR(inode->i_rdev)].device) return -ENXIO;   /* No such device */
 411 
 412         if (filp->f_mode & 2)  
 413             return -EROFS;
 414 
 415         check_disk_change(inode->i_rdev);
 416 
 417         if(!scsi_CDs[MINOR(inode->i_rdev)].device->access_count++)
 418           sr_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
 419         if (scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)
 420           (*scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)++;
 421 
 422         /* If this device did not have media in the drive at boot time, then
 423            we would have been unable to get the sector size.  Check to see if
 424            this is the case, and try again.
 425            */
 426 
 427         if(scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size)
 428           get_sectorsize(MINOR(inode->i_rdev));
 429 
 430 #if 1   /* don't use for now - it doesn't seem to work for everybody */
 431         sr_photocd(inode);
 432 #endif
 433 
 434         return 0;
 435 }
 436 
 437 
 438 /*
 439  * do_sr_request() is the request handler function for the sr driver.  Its function in life 
 440  * is to take block device requests, and translate them to SCSI commands.
 441  */
 442         
 443 static void do_sr_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 444 {
 445   Scsi_Cmnd * SCpnt = NULL;
 446   struct request * req = NULL;
 447   unsigned long flags;
 448   int flag = 0;
 449 
 450   while (1==1){
 451     save_flags(flags);
 452     cli();
 453     if (CURRENT != NULL && CURRENT->dev == -1) {
 454       restore_flags(flags);
 455       return;
 456     };
 457     
 458     INIT_SCSI_REQUEST;
 459 
 460     if (flag++ == 0)
 461       SCpnt = allocate_device(&CURRENT,
 462                               scsi_CDs[DEVICE_NR(MINOR(CURRENT->dev))].device, 0); 
 463     else SCpnt = NULL;
 464     restore_flags(flags);
 465 
 466 /* This is a performance enhancement.  We dig down into the request list and
 467    try and find a queueable request (i.e. device not busy, and host able to
 468    accept another command.  If we find one, then we queue it. This can
 469    make a big difference on systems with more than one disk drive.  We want
 470    to have the interrupts off when monkeying with the request list, because
 471    otherwise the kernel might try and slip in a request in between somewhere. */
 472 
 473     if (!SCpnt && sr_template.nr_dev > 1){
 474       struct request *req1;
 475       req1 = NULL;
 476       save_flags(flags);
 477       cli();
 478       req = CURRENT;
 479       while(req){
 480         SCpnt = request_queueable(req,
 481                                   scsi_CDs[DEVICE_NR(MINOR(req->dev))].device);
 482         if(SCpnt) break;
 483         req1 = req;
 484         req = req->next;
 485       };
 486       if (SCpnt && req->dev == -1) {
 487         if (req == CURRENT) 
 488           CURRENT = CURRENT->next;
 489         else
 490           req1->next = req->next;
 491       };
 492       restore_flags(flags);
 493     };
 494     
 495     if (!SCpnt)
 496       return; /* Could not find anything to do */
 497     
 498   wake_up(&wait_for_request);
 499 
 500 /* Queue command */
 501   requeue_sr_request(SCpnt);
 502   };  /* While */
 503 }    
 504 
 505 void requeue_sr_request (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 506 {
 507         unsigned int dev, block, realcount;
 508         unsigned char cmd[10], *buffer, tries;
 509         int this_count, start, end_rec;
 510 
 511         tries = 2;
 512 
 513       repeat:
 514         if(!SCpnt || SCpnt->request.dev <= 0) {
 515           do_sr_request();
 516           return;
 517         }
 518 
 519         dev =  MINOR(SCpnt->request.dev);
 520         block = SCpnt->request.sector;  
 521         buffer = NULL;
 522         this_count = 0;
 523 
 524         if (dev >= sr_template.nr_dev)
 525                 {
 526                 /* printk("CD-ROM request error: invalid device.\n");                   */
 527                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 528                 tries = 2;
 529                 goto repeat;
 530                 }
 531 
 532         if (!scsi_CDs[dev].use)
 533                 {
 534                 /* printk("CD-ROM request error: device marked not in use.\n");         */
 535                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 536                 tries = 2;
 537                 goto repeat;
 538                 }
 539 
 540         if (scsi_CDs[dev].device->changed)
 541                 {
 542 /* 
 543  * quietly refuse to do anything to a changed disc until the changed bit has been reset
 544  */
 545                 /* printk("CD-ROM has been changed.  Prohibiting further I/O.\n");      */
 546                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 547                 tries = 2;
 548                 goto repeat;
 549                 }
 550         
 551         switch (SCpnt->request.cmd)
 552                 {
 553                 case WRITE:             
 554                         SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 555                         goto repeat;
 556                         break;
 557                 case READ : 
 558                         cmd[0] = READ_6;
 559                         break;
 560                 default : 
 561                         panic ("Unknown sr command %d\n", SCpnt->request.cmd);
 562                 }
 563         
 564         cmd[1] = (SCpnt->lun << 5) & 0xe0;
 565 
 566 /*
 567            Now do the grungy work of figuring out which sectors we need, and
 568            where in memory we are going to put them.
 569 
 570            The variables we need are:
 571 
 572            this_count= number of 512 byte sectors being read 
 573            block     = starting cdrom sector to read.
 574            realcount = # of cdrom sectors to read
 575 
 576            The major difference between a scsi disk and a scsi cdrom
 577 is that we will always use scatter-gather if we can, because we can
 578 work around the fact that the buffer cache has a block size of 1024,
 579 and we have 2048 byte sectors.  This code should work for buffers that
 580 are any multiple of 512 bytes long.  */
 581 
 582 #if 1
 583         /* Here we redirect the volume descriptor block of the CD-ROM.
 584          * Necessary for multisession CD's, until the isofs-routines
 585          * handle this via the CDROMMULTISESSION_SYS call
 586          */
 587         if (block >= 64 && block < 68) {
 588           block += scsi_CDs[dev].mpcd_sector*4; }
 589 #endif
 590         
 591         SCpnt->use_sg = 0;
 592 
 593         if (SCpnt->host->sg_tablesize > 0 &&
 594             (!need_isa_buffer ||
 595             dma_free_sectors >= 10)) {
 596           struct buffer_head * bh;
 597           struct scatterlist * sgpnt;
 598           int count, this_count_max;
 599           bh = SCpnt->request.bh;
 600           this_count = 0;
 601           count = 0;
 602           this_count_max = (scsi_CDs[dev].ten ? 0xffff : 0xff) << 4;
 603           /* Calculate how many links we can use.  First see if we need
 604            a padding record at the start */
 605           this_count = SCpnt->request.sector % 4;
 606           if(this_count) count++;
 607           while(bh && count < SCpnt->host->sg_tablesize) {
 608             if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
 609             this_count += (bh->b_size >> 9);
 610             count++;
 611             bh = bh->b_reqnext;
 612           };
 613           /* Fix up in case of an odd record at the end */
 614           end_rec = 0;
 615           if(this_count % 4) {
 616             if (count < SCpnt->host->sg_tablesize) {
 617               count++;
 618               end_rec = (4 - (this_count % 4)) << 9;
 619               this_count += 4 - (this_count % 4);
 620             } else {
 621               count--;
 622               this_count -= (this_count % 4);
 623             };
 624           };
 625           SCpnt->use_sg = count;  /* Number of chains */
 626           count = 512;/* scsi_malloc can only allocate in chunks of 512 bytes*/
 627           while( count < (SCpnt->use_sg * sizeof(struct scatterlist))) 
 628             count = count << 1;
 629           SCpnt->sglist_len = count;
 630           sgpnt = (struct scatterlist * ) scsi_malloc(count);
 631           if (!sgpnt) {
 632             printk("Warning - running *really* short on DMA buffers\n");
 633             SCpnt->use_sg = 0;  /* No memory left - bail out */
 634           } else {
 635             buffer = (unsigned char *) sgpnt;
 636             count = 0;
 637             bh = SCpnt->request.bh;
 638             if(SCpnt->request.sector % 4) {
 639               sgpnt[count].length = (SCpnt->request.sector % 4) << 9;
 640               sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
 641               if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
 642               sgpnt[count].alt_address = sgpnt[count].address; /* Flag to delete
 643                                                                   if needed */
 644               count++;
 645             };
 646             for(bh = SCpnt->request.bh; count < SCpnt->use_sg; 
 647                 count++, bh = bh->b_reqnext) {
 648               if (bh) { /* Need a placeholder at the end of the record? */
 649                 sgpnt[count].address = bh->b_data;
 650                 sgpnt[count].length = bh->b_size;
 651                 sgpnt[count].alt_address = NULL;
 652               } else {
 653                 sgpnt[count].address = (char *) scsi_malloc(end_rec);
 654                 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
 655                 sgpnt[count].length = end_rec;
 656                 sgpnt[count].alt_address = sgpnt[count].address;
 657                 if (count+1 != SCpnt->use_sg) panic("Bad sr request list");
 658                 break;
 659               };
 660               if (((int) sgpnt[count].address) + sgpnt[count].length > 
 661                   ISA_DMA_THRESHOLD & (SCpnt->host->unchecked_isa_dma)) {
 662                 sgpnt[count].alt_address = sgpnt[count].address;
 663                 /* We try and avoid exhausting the DMA pool, since it is easier
 664                    to control usage here.  In other places we might have a more
 665                    pressing need, and we would be screwed if we ran out */
 666                 if(dma_free_sectors < (sgpnt[count].length >> 9) + 5) {
 667                   sgpnt[count].address = NULL;
 668                 } else {
 669                   sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
 670                 };
 671 /* If we start running low on DMA buffers, we abort the scatter-gather
 672    operation, and free all of the memory we have allocated.  We want to
 673    ensure that all scsi operations are able to do at least a non-scatter/gather
 674    operation */
 675                 if(sgpnt[count].address == NULL){ /* Out of dma memory */
 676                   printk("Warning: Running low on SCSI DMA buffers");
 677                   /* Try switching back to a non scatter-gather operation. */
 678                   while(--count >= 0){
 679                     if(sgpnt[count].alt_address) 
 680                       scsi_free(sgpnt[count].address, sgpnt[count].length);
 681                   };
 682                   SCpnt->use_sg = 0;
 683                   scsi_free(buffer, SCpnt->sglist_len);
 684                   break;
 685                 }; /* if address == NULL */
 686               };  /* if need DMA fixup */
 687             };  /* for loop to fill list */
 688 #ifdef DEBUG
 689             printk("SG: %d %d %d %d %d *** ",SCpnt->use_sg, SCpnt->request.sector,
 690                    this_count, 
 691                    SCpnt->request.current_nr_sectors,
 692                    SCpnt->request.nr_sectors);
 693             for(count=0; count<SCpnt->use_sg; count++)
 694               printk("SGlist: %d %x %x %x\n", count,
 695                      sgpnt[count].address, 
 696                      sgpnt[count].alt_address, 
 697                      sgpnt[count].length);
 698 #endif
 699           };  /* Able to allocate scatter-gather list */
 700         };
 701         
 702         if (SCpnt->use_sg == 0){
 703           /* We cannot use scatter-gather.  Do this the old fashion way */
 704           if (!SCpnt->request.bh)       
 705             this_count = SCpnt->request.nr_sectors;
 706           else
 707             this_count = (SCpnt->request.bh->b_size >> 9);
 708           
 709           start = block % 4;
 710           if (start)
 711             {                             
 712               this_count = ((this_count > 4 - start) ? 
 713                             (4 - start) : (this_count));
 714               buffer = (unsigned char *) scsi_malloc(2048);
 715             } 
 716           else if (this_count < 4)
 717             {
 718               buffer = (unsigned char *) scsi_malloc(2048);
 719             }
 720           else
 721             {
 722               this_count -= this_count % 4;
 723               buffer = (unsigned char *) SCpnt->request.buffer;
 724               if (((int) buffer) + (this_count << 9) > ISA_DMA_THRESHOLD & 
 725                   (SCpnt->host->unchecked_isa_dma))
 726                 buffer = (unsigned char *) scsi_malloc(this_count << 9);
 727             }
 728         };
 729 
 730         if (scsi_CDs[dev].sector_size == 2048)
 731           block = block >> 2; /* These are the sectors that the cdrom uses */
 732         else
 733           block = block & 0xfffffffc;
 734 
 735         realcount = (this_count + 3) / 4;
 736 
 737         if (scsi_CDs[dev].sector_size == 512) realcount = realcount << 2;
 738 
 739         if (((realcount > 0xff) || (block > 0x1fffff)) && scsi_CDs[dev].ten) 
 740                 {
 741                 if (realcount > 0xffff)
 742                         {
 743                         realcount = 0xffff;
 744                         this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
 745                         }
 746 
 747                 cmd[0] += READ_10 - READ_6 ;
 748                 cmd[2] = (unsigned char) (block >> 24) & 0xff;
 749                 cmd[3] = (unsigned char) (block >> 16) & 0xff;
 750                 cmd[4] = (unsigned char) (block >> 8) & 0xff;
 751                 cmd[5] = (unsigned char) block & 0xff;
 752                 cmd[6] = cmd[9] = 0;
 753                 cmd[7] = (unsigned char) (realcount >> 8) & 0xff;
 754                 cmd[8] = (unsigned char) realcount & 0xff;
 755                 }
 756         else
 757                 {
 758                   if (realcount > 0xff)
 759                     {
 760                       realcount = 0xff;
 761                       this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
 762                     }
 763                   
 764                   cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
 765                   cmd[2] = (unsigned char) ((block >> 8) & 0xff);
 766                   cmd[3] = (unsigned char) block & 0xff;
 767                   cmd[4] = (unsigned char) realcount;
 768                   cmd[5] = 0;
 769                 }   
 770 
 771 #ifdef DEBUG
 772         { 
 773           int i;
 774           printk("ReadCD: %d %d %d %d\n",block, realcount, buffer, this_count);
 775           printk("Use sg: %d\n", SCpnt->use_sg);
 776           printk("Dumping command: ");
 777           for(i=0; i<12; i++) printk("%2.2x ", cmd[i]);
 778           printk("\n");
 779         };
 780 #endif
 781 
 782 /* Some dumb host adapters can speed transfers by knowing the
 783  * minimum transfersize in advance.
 784  *
 785  * We shouldn't disconnect in the middle of a sector, but the cdrom
 786  * sector size can be larger than the size of a buffer and the
 787  * transfer may be split to the size of a buffer.  So it's safe to
 788  * assume that we can at least transfer the minimum of the buffer
 789  * size (1024) and the sector size between each connect / disconnect.
 790  */
 791 
 792         SCpnt->transfersize = (scsi_CDs[dev].sector_size > 1024) ?
 793                         1024 : scsi_CDs[dev].sector_size;
 794 
 795         SCpnt->this_count = this_count;
 796         scsi_do_cmd (SCpnt, (void *) cmd, buffer, 
 797                      realcount * scsi_CDs[dev].sector_size, 
 798                      rw_intr, SR_TIMEOUT, MAX_RETRIES);
 799 }
 800 
 801 static int sr_detect(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 802   
 803   if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 0;
 804 
 805   printk("Detected scsi CD-ROM sr%d at scsi%d, id %d, lun %d\n", 
 806          sr_template.dev_noticed++,
 807          SDp->host->host_no , SDp->id, SDp->lun); 
 808 
 809          return 1;
 810 }
 811 
 812 static int sr_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 813   Scsi_CD * cpnt;
 814   int i;
 815   
 816   if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 1;
 817   
 818   if (sr_template.nr_dev >= sr_template.dev_max)
 819     {
 820         SDp->attached--;
 821         return 1;
 822     }
 823   
 824   for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++) 
 825     if(!cpnt->device) break;
 826   
 827   if(i >= sr_template.dev_max) panic ("scsi_devices corrupt (sr)");
 828   
 829   SDp->scsi_request_fn = do_sr_request;
 830   scsi_CDs[i].device = SDp;
 831   sr_template.nr_dev++;
 832   if(sr_template.nr_dev > sr_template.dev_max)
 833     panic ("scsi_devices corrupt (sr)");
 834   return 0;
 835 }
 836      
 837 
 838 static void sr_init_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 839 {
 840   struct request * req;
 841   
 842   req = &SCpnt->request;
 843   req->dev = 0xfffe; /* Busy, but indicate request done */
 844   
 845   if (req->sem != NULL) {
 846     up(req->sem);
 847   }
 848 }
 849 
 850 static void get_sectorsize(int i){
     /* [previous][next][first][last][top][bottom][index][help] */
 851   unsigned char cmd[10];
 852   unsigned char *buffer;
 853   int the_result, retries;
 854   Scsi_Cmnd * SCpnt;
 855   
 856   buffer = (unsigned char *) scsi_malloc(512);
 857   SCpnt = allocate_device(NULL, scsi_CDs[i].device, 1);
 858 
 859   retries = 3;
 860   do {
 861     cmd[0] = READ_CAPACITY;
 862     cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
 863     memset ((void *) &cmd[2], 0, 8);
 864     SCpnt->request.dev = 0xffff;  /* Mark as really busy */
 865     SCpnt->cmd_len = 0;
 866     
 867     memset(buffer, 0, 8);
 868 
 869     scsi_do_cmd (SCpnt,
 870                  (void *) cmd, (void *) buffer,
 871                  512, sr_init_done,  SR_TIMEOUT,
 872                  MAX_RETRIES);
 873     
 874     if (current == task[0])
 875       while(SCpnt->request.dev != 0xfffe);
 876     else
 877       if (SCpnt->request.dev != 0xfffe){
 878         struct semaphore sem = MUTEX_LOCKED;
 879         SCpnt->request.sem = &sem;
 880         down(&sem);
 881         /* Hmm.. Have to ask about this */
 882         while (SCpnt->request.dev != 0xfffe) schedule();
 883       };
 884     
 885     the_result = SCpnt->result;
 886     retries--;
 887     
 888   } while(the_result && retries);
 889   
 890   SCpnt->request.dev = -1;  /* Mark as not busy */
 891   
 892   wake_up(&SCpnt->device->device_wait); 
 893 
 894   if (the_result) {
 895     scsi_CDs[i].capacity = 0x1fffff;
 896     scsi_CDs[i].sector_size = 2048;  /* A guess, just in case */
 897     scsi_CDs[i].needs_sector_size = 1;
 898   } else {
 899     scsi_CDs[i].capacity = (buffer[0] << 24) |
 900       (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
 901     scsi_CDs[i].sector_size = (buffer[4] << 24) |
 902       (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
 903     if(scsi_CDs[i].sector_size == 0) scsi_CDs[i].sector_size = 2048;
 904     if(scsi_CDs[i].sector_size != 2048 && 
 905        scsi_CDs[i].sector_size != 512) {
 906       printk ("scd%d : unsupported sector size %d.\n",
 907               i, scsi_CDs[i].sector_size);
 908       scsi_CDs[i].capacity = 0;
 909       scsi_CDs[i].needs_sector_size = 1;
 910     };
 911     if(scsi_CDs[i].sector_size == 2048)
 912       scsi_CDs[i].capacity *= 4;
 913     scsi_CDs[i].needs_sector_size = 0;
 914   };
 915   scsi_free(buffer, 512);
 916 }
 917 
 918 static void sr_init()
     /* [previous][next][first][last][top][bottom][index][help] */
 919 {
 920         int i;
 921         static int sr_registered = 0;
 922 
 923         if(sr_template.dev_noticed == 0) return;
 924 
 925         if(!sr_registered) {
 926           if (register_blkdev(MAJOR_NR,"sr",&sr_fops)) {
 927             printk("Unable to get major %d for SCSI-CD\n",MAJOR_NR);
 928             return;
 929           }
 930           sr_registered++;
 931         }
 932 
 933         
 934         if (scsi_CDs) return;
 935         sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
 936         scsi_CDs = (Scsi_CD *) scsi_init_malloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
 937         memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
 938 
 939         sr_sizes = (int *) scsi_init_malloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
 940         memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
 941 
 942         sr_blocksizes = (int *) scsi_init_malloc(sr_template.dev_max * 
 943                                                  sizeof(int), GFP_ATOMIC);
 944         for(i=0;i<sr_template.dev_max;i++) sr_blocksizes[i] = 2048;
 945         blksize_size[MAJOR_NR] = sr_blocksizes;
 946 
 947 }
 948 
 949 void sr_finish()
     /* [previous][next][first][last][top][bottom][index][help] */
 950 {
 951   int i;
 952 
 953         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
 954         blk_size[MAJOR_NR] = sr_sizes;  
 955 
 956         for (i = 0; i < sr_template.nr_dev; ++i)
 957                 {
 958                   /* If we have already seen this, then skip it.  Comes up
 959                      with loadable modules. */
 960                   if (scsi_CDs[i].capacity) continue;
 961                   get_sectorsize(i);
 962                   printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
 963                   scsi_CDs[i].use = 1;
 964                   scsi_CDs[i].ten = 1;
 965                   scsi_CDs[i].remap = 1;
 966                   sr_sizes[i] = scsi_CDs[i].capacity;
 967                 }
 968 
 969 
 970         /* If our host adapter is capable of scatter-gather, then we increase
 971            the read-ahead to 16 blocks (32 sectors).  If not, we use
 972            a two block (4 sector) read ahead. */
 973         if(scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
 974           read_ahead[MAJOR_NR] = 32;  /* 32 sector read-ahead.  Always removable. */
 975         else
 976           read_ahead[MAJOR_NR] = 4;  /* 4 sector read-ahead */
 977 
 978         return;
 979 }       
 980 
 981 static void sr_detach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
 982 {
 983   Scsi_CD * cpnt;
 984   int i, major;
 985   
 986   major = MAJOR_NR << 8;
 987 
 988   for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++) 
 989     if(cpnt->device == SDp) {
 990       /*
 991        * Since the cdrom is read-only, no need to sync the device.
 992        * We should be kind to our buffer cache, however.
 993        */
 994       invalidate_inodes(major | i);
 995       invalidate_buffers(major | i);
 996 
 997       /*
 998        * Reset things back to a sane state so that one can re-load a new
 999        * driver (perhaps the same one).
1000        */
1001       cpnt->device = NULL;
1002       cpnt->capacity = 0;
1003       SDp->attached--;
1004       sr_template.nr_dev--;
1005       sr_template.dev_noticed--;
1006       sr_sizes[i] = 0;
1007       return;
1008     }
1009   return;
1010 }

/* [previous][next][first][last][top][bottom][index][help] */