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

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