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  *              People reported that it is necessary to eject and reinsert
 297  *              the CD after the set-density call to get this working for
 298  *              old drives.
 299  *              And some very new drives don't need this call any more...
 300  *              Multisession CD's are supported too.
 301  *
 302  * Dec 1994: completely rewritten, uses kernel_scsi_ioctl() now
 303  *
 304  *   kraxel@cs.tu-berlin.de (Gerd Knorr)
 305  */
 306 
 307 #define DEBUG
 308 
 309 static void sr_photocd(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 310 {
 311   unsigned long   sector,min,sec,frame;
 312   unsigned char   buf[40];    /* the buffer for the ioctl */
 313   unsigned char   *cmd;       /* the scsi-command */
 314   unsigned char   *send;      /* the data we send to the drive ... */
 315   unsigned char   *rec;       /* ... and get back */
 316   int             rc,is_xa;
 317 
 318   if (!suser()) {
 319     /* I'm not the superuser, so SCSI_IOCTL_SEND_COMMAND isn't allowed for me.
 320      * That's why mpcd_sector will be initialized with zero, because I'm not
 321      * able to get the right value. Necessary only if access_count is 1, else
 322      * no disk change happened since the last call of this function and we can
 323      * keep the old value.
 324      */
 325     if (1 == scsi_CDs[MINOR(inode->i_rdev)].device->access_count)
 326       scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = 0;
 327     return;
 328   }
 329 
 330   cmd = rec = &buf[8];
 331   switch(scsi_CDs[MINOR(inode->i_rdev)].device->manufacturer) {
 332 
 333   case SCSI_MAN_NEC:
 334 #ifdef DEBUG
 335     printk("sr_photocd: use NEC code\n");
 336 #endif
 337     memset(buf,0,40);
 338     *((unsigned long*)buf)   = 0x0;   /* we send nothing...     */
 339     *((unsigned long*)buf+1) = 0x16;  /* and receive 0x16 bytes */
 340     cmd[0] = 0xde;
 341     cmd[1] = 0x03;
 342     cmd[2] = 0xb0;
 343     rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 344                            SCSI_IOCTL_SEND_COMMAND, buf);
 345     if (rc != 0) {
 346       printk("sr_photocd: ioctl error (NEC): 0x%x\n",rc);
 347       sector = 0;
 348     } else {
 349       min   = (unsigned long) rec[15]/16*10 + (unsigned long) rec[15]%16;
 350       sec   = (unsigned long) rec[16]/16*10 + (unsigned long) rec[16]%16;
 351       frame = (unsigned long) rec[17]/16*10 + (unsigned long) rec[17]%16;
 352       /* if rec[14] isn't 0xb0, the drive does not support multisession CD's, use zero */
 353       sector = (0xb0 == rec[14]) ? min*CD_SECS*CD_FRAMES + sec*CD_FRAMES + frame : 0;
 354 #ifdef DEBUG
 355       printk("NEC: (%2x) %2li:%02li:%02li = %li\n",buf[8+14],min,sec,frame,sector);
 356       if (sector) {
 357         printk("sr_photocd: multisession CD detected. start: %lu\n",sector);
 358       }
 359 #endif
 360     }
 361     break;
 362 
 363   case SCSI_MAN_TOSHIBA:
 364 #ifdef DEBUG
 365     printk("sr_photocd: use TOSHIBA code\n");
 366 #endif
 367     
 368     /* we request some disc information (is it a XA-CD ?,
 369        where starts the last session ?) */
 370     memset(buf,0,40);
 371     *((unsigned long*)buf)   = 0;
 372     *((unsigned long*)buf+1) = 4;  /* we receive 4 bytes from the drive */
 373     cmd[0] = 0xc7;
 374     cmd[1] = 3;
 375     rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 376                            SCSI_IOCTL_SEND_COMMAND, buf);
 377     if (rc != 0) {
 378       printk("sr_photocd: ioctl error (TOSHIBA #1): 0x%x\n",rc);
 379       sector = 0;
 380       break; /* if the first ioctl fails, we don't call the second one */
 381     }
 382     is_xa  = (rec[0] == 0x20);
 383 #ifdef DEBUG
 384     printk("sr_photocd: TOSHIBA %x\n",rec[0]);
 385 #endif
 386     min    = (unsigned long) rec[1]/16*10 + (unsigned long) rec[1]%16;
 387     sec    = (unsigned long) rec[2]/16*10 + (unsigned long) rec[2]%16;
 388     frame  = (unsigned long) rec[3]/16*10 + (unsigned long) rec[3]%16;
 389     sector = min*CD_SECS*CD_FRAMES + sec*CD_FRAMES + frame;
 390     if (sector) {
 391       sector -= CD_BLOCK_OFFSET;
 392 #ifdef DEBUG
 393       printk("sr_photocd: multisession CD detected: start: %lu\n",sector);
 394 #endif
 395     }
 396 
 397     /* now we do a get_density... */
 398     memset(buf,0,40);
 399     *((unsigned long*)buf)   = 0;
 400     *((unsigned long*)buf+1) = 12;
 401     cmd[0] = 0x1a;
 402     cmd[2] = 1;
 403     cmd[4] = 12;
 404     rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 405                            SCSI_IOCTL_SEND_COMMAND, buf);
 406     if (rc != 0) {
 407       printk("sr_photocd: ioctl error (TOSHIBA #2): 0x%x\n",rc);
 408       break;
 409     }
 410 #ifdef DEBUG
 411     printk("sr_photocd: get_density: 0x%x\n",rec[4]);
 412 #endif
 413     
 414     /* ...and only if necessary a set_density */
 415     if ((rec[4] != 0x81 && is_xa) || (rec[4] != 0 && !is_xa)) {
 416 #ifdef DEBUG
 417       printk("sr_photocd: doing set_density\n");
 418 #endif
 419       memset(buf,0,40);
 420       *((unsigned long*)buf)   = 12;  /* sending 12 bytes... */
 421       *((unsigned long*)buf+1) = 0;
 422       cmd[0] = 0x15;
 423       cmd[1] = (1 << 4);
 424       cmd[4] = 12;
 425       send = &cmd[6];                 /* this is a 6-Byte command          */
 426       send[ 3] = 0x08;                /* the data for the command          */
 427       send[ 4] = (is_xa) ? 0x81 : 0;  /* density 0x81 for XA-CD's, 0 else  */
 428       send[10] = 0x08;
 429       rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 430                              SCSI_IOCTL_SEND_COMMAND, buf);
 431       if (rc != 0) {
 432         printk("sr_photocd: ioctl error (TOSHIBA #3): 0x%x\n",rc);
 433       }
 434     }
 435     break;
 436 
 437   case SCSI_MAN_UNKNOWN:
 438   default:
 439 #ifdef DEBUG
 440     printk("sr_photocd: unknown drive, no special multisession code\n");
 441 #endif
 442     sector = 0;
 443     break; }
 444 
 445   scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = sector;
 446   /* The code above may have changed the sector size or capacity. */
 447   scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size = 1;
 448   return;
 449 }
 450 
 451 #undef DEBUG
 452 
 453 static int sr_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 454 {
 455         if(MINOR(inode->i_rdev) >= sr_template.nr_dev || 
 456            !scsi_CDs[MINOR(inode->i_rdev)].device) return -ENXIO;   /* No such device */
 457 
 458         if (filp->f_mode & 2)  
 459             return -EROFS;
 460 
 461         check_disk_change(inode->i_rdev);
 462 
 463         if(!scsi_CDs[MINOR(inode->i_rdev)].device->access_count++)
 464           sr_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
 465         if (scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)
 466           (*scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)++;
 467 
 468         sr_photocd(inode);
 469 
 470         /* If this device did not have media in the drive at boot time, then
 471            we would have been unable to get the sector size.  Check to see if
 472            this is the case, and try again.
 473            */
 474 
 475         if(scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size)
 476           get_sectorsize(MINOR(inode->i_rdev));
 477 
 478         return 0;
 479 }
 480 
 481 
 482 /*
 483  * do_sr_request() is the request handler function for the sr driver.  Its function in life 
 484  * is to take block device requests, and translate them to SCSI commands.
 485  */
 486         
 487 static void do_sr_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489   Scsi_Cmnd * SCpnt = NULL;
 490   struct request * req = NULL;
 491   unsigned long flags;
 492   int flag = 0;
 493 
 494   while (1==1){
 495     save_flags(flags);
 496     cli();
 497     if (CURRENT != NULL && CURRENT->dev == -1) {
 498       restore_flags(flags);
 499       return;
 500     };
 501     
 502     INIT_SCSI_REQUEST;
 503 
 504     if (flag++ == 0)
 505       SCpnt = allocate_device(&CURRENT,
 506                               scsi_CDs[DEVICE_NR(MINOR(CURRENT->dev))].device, 0); 
 507     else SCpnt = NULL;
 508     restore_flags(flags);
 509 
 510 /* This is a performance enhancement.  We dig down into the request list and
 511    try and find a queueable request (i.e. device not busy, and host able to
 512    accept another command.  If we find one, then we queue it. This can
 513    make a big difference on systems with more than one disk drive.  We want
 514    to have the interrupts off when monkeying with the request list, because
 515    otherwise the kernel might try and slip in a request in between somewhere. */
 516 
 517     if (!SCpnt && sr_template.nr_dev > 1){
 518       struct request *req1;
 519       req1 = NULL;
 520       save_flags(flags);
 521       cli();
 522       req = CURRENT;
 523       while(req){
 524         SCpnt = request_queueable(req,
 525                                   scsi_CDs[DEVICE_NR(MINOR(req->dev))].device);
 526         if(SCpnt) break;
 527         req1 = req;
 528         req = req->next;
 529       };
 530       if (SCpnt && req->dev == -1) {
 531         if (req == CURRENT) 
 532           CURRENT = CURRENT->next;
 533         else
 534           req1->next = req->next;
 535       };
 536       restore_flags(flags);
 537     };
 538     
 539     if (!SCpnt)
 540       return; /* Could not find anything to do */
 541     
 542   wake_up(&wait_for_request);
 543 
 544 /* Queue command */
 545   requeue_sr_request(SCpnt);
 546   };  /* While */
 547 }    
 548 
 549 void requeue_sr_request (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 550 {
 551         unsigned int dev, block, realcount;
 552         unsigned char cmd[10], *buffer, tries;
 553         int this_count, start, end_rec;
 554 
 555         tries = 2;
 556 
 557       repeat:
 558         if(!SCpnt || SCpnt->request.dev <= 0) {
 559           do_sr_request();
 560           return;
 561         }
 562 
 563         dev =  MINOR(SCpnt->request.dev);
 564         block = SCpnt->request.sector;  
 565         buffer = NULL;
 566         this_count = 0;
 567 
 568         if (dev >= sr_template.nr_dev)
 569                 {
 570                 /* printk("CD-ROM request error: invalid device.\n");                   */
 571                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 572                 tries = 2;
 573                 goto repeat;
 574                 }
 575 
 576         if (!scsi_CDs[dev].use)
 577                 {
 578                 /* printk("CD-ROM request error: device marked not in use.\n");         */
 579                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 580                 tries = 2;
 581                 goto repeat;
 582                 }
 583 
 584         if (scsi_CDs[dev].device->changed)
 585                 {
 586 /* 
 587  * quietly refuse to do anything to a changed disc until the changed bit has been reset
 588  */
 589                 /* printk("CD-ROM has been changed.  Prohibiting further I/O.\n");      */
 590                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 591                 tries = 2;
 592                 goto repeat;
 593                 }
 594         
 595         switch (SCpnt->request.cmd)
 596                 {
 597                 case WRITE:             
 598                         SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 599                         goto repeat;
 600                         break;
 601                 case READ : 
 602                         cmd[0] = READ_6;
 603                         break;
 604                 default : 
 605                         panic ("Unknown sr command %d\n", SCpnt->request.cmd);
 606                 }
 607         
 608         cmd[1] = (SCpnt->lun << 5) & 0xe0;
 609 
 610 /*
 611            Now do the grungy work of figuring out which sectors we need, and
 612            where in memory we are going to put them.
 613 
 614            The variables we need are:
 615 
 616            this_count= number of 512 byte sectors being read 
 617            block     = starting cdrom sector to read.
 618            realcount = # of cdrom sectors to read
 619 
 620            The major difference between a scsi disk and a scsi cdrom
 621 is that we will always use scatter-gather if we can, because we can
 622 work around the fact that the buffer cache has a block size of 1024,
 623 and we have 2048 byte sectors.  This code should work for buffers that
 624 are any multiple of 512 bytes long.  */
 625 
 626         SCpnt->use_sg = 0;
 627 
 628         if (SCpnt->host->sg_tablesize > 0 &&
 629             (!need_isa_buffer ||
 630             dma_free_sectors >= 10)) {
 631           struct buffer_head * bh;
 632           struct scatterlist * sgpnt;
 633           int count, this_count_max;
 634           bh = SCpnt->request.bh;
 635           this_count = 0;
 636           count = 0;
 637           this_count_max = (scsi_CDs[dev].ten ? 0xffff : 0xff) << 4;
 638           /* Calculate how many links we can use.  First see if we need
 639            a padding record at the start */
 640           this_count = SCpnt->request.sector % 4;
 641           if(this_count) count++;
 642           while(bh && count < SCpnt->host->sg_tablesize) {
 643             if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
 644             this_count += (bh->b_size >> 9);
 645             count++;
 646             bh = bh->b_reqnext;
 647           };
 648           /* Fix up in case of an odd record at the end */
 649           end_rec = 0;
 650           if(this_count % 4) {
 651             if (count < SCpnt->host->sg_tablesize) {
 652               count++;
 653               end_rec = (4 - (this_count % 4)) << 9;
 654               this_count += 4 - (this_count % 4);
 655             } else {
 656               count--;
 657               this_count -= (this_count % 4);
 658             };
 659           };
 660           SCpnt->use_sg = count;  /* Number of chains */
 661           count = 512;/* scsi_malloc can only allocate in chunks of 512 bytes*/
 662           while( count < (SCpnt->use_sg * sizeof(struct scatterlist))) 
 663             count = count << 1;
 664           SCpnt->sglist_len = count;
 665           sgpnt = (struct scatterlist * ) scsi_malloc(count);
 666           if (!sgpnt) {
 667             printk("Warning - running *really* short on DMA buffers\n");
 668             SCpnt->use_sg = 0;  /* No memory left - bail out */
 669           } else {
 670             buffer = (unsigned char *) sgpnt;
 671             count = 0;
 672             bh = SCpnt->request.bh;
 673             if(SCpnt->request.sector % 4) {
 674               sgpnt[count].length = (SCpnt->request.sector % 4) << 9;
 675               sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
 676               if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
 677               sgpnt[count].alt_address = sgpnt[count].address; /* Flag to delete
 678                                                                   if needed */
 679               count++;
 680             };
 681             for(bh = SCpnt->request.bh; count < SCpnt->use_sg; 
 682                 count++, bh = bh->b_reqnext) {
 683               if (bh) { /* Need a placeholder at the end of the record? */
 684                 sgpnt[count].address = bh->b_data;
 685                 sgpnt[count].length = bh->b_size;
 686                 sgpnt[count].alt_address = NULL;
 687               } else {
 688                 sgpnt[count].address = (char *) scsi_malloc(end_rec);
 689                 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
 690                 sgpnt[count].length = end_rec;
 691                 sgpnt[count].alt_address = sgpnt[count].address;
 692                 if (count+1 != SCpnt->use_sg) panic("Bad sr request list");
 693                 break;
 694               };
 695               if (((int) sgpnt[count].address) + sgpnt[count].length > 
 696                   ISA_DMA_THRESHOLD & (SCpnt->host->unchecked_isa_dma)) {
 697                 sgpnt[count].alt_address = sgpnt[count].address;
 698                 /* We try and avoid exhausting the DMA pool, since it is easier
 699                    to control usage here.  In other places we might have a more
 700                    pressing need, and we would be screwed if we ran out */
 701                 if(dma_free_sectors < (sgpnt[count].length >> 9) + 5) {
 702                   sgpnt[count].address = NULL;
 703                 } else {
 704                   sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
 705                 };
 706 /* If we start running low on DMA buffers, we abort the scatter-gather
 707    operation, and free all of the memory we have allocated.  We want to
 708    ensure that all scsi operations are able to do at least a non-scatter/gather
 709    operation */
 710                 if(sgpnt[count].address == NULL){ /* Out of dma memory */
 711                   printk("Warning: Running low on SCSI DMA buffers");
 712                   /* Try switching back to a non scatter-gather operation. */
 713                   while(--count >= 0){
 714                     if(sgpnt[count].alt_address) 
 715                       scsi_free(sgpnt[count].address, sgpnt[count].length);
 716                   };
 717                   SCpnt->use_sg = 0;
 718                   scsi_free(buffer, SCpnt->sglist_len);
 719                   break;
 720                 }; /* if address == NULL */
 721               };  /* if need DMA fixup */
 722             };  /* for loop to fill list */
 723 #ifdef DEBUG
 724             printk("SR: %d %d %d %d %d *** ",SCpnt->use_sg, SCpnt->request.sector,
 725                    this_count, 
 726                    SCpnt->request.current_nr_sectors,
 727                    SCpnt->request.nr_sectors);
 728             for(count=0; count<SCpnt->use_sg; count++)
 729               printk("SGlist: %d %x %x %x\n", count,
 730                      sgpnt[count].address, 
 731                      sgpnt[count].alt_address, 
 732                      sgpnt[count].length);
 733 #endif
 734           };  /* Able to allocate scatter-gather list */
 735         };
 736         
 737         if (SCpnt->use_sg == 0){
 738           /* We cannot use scatter-gather.  Do this the old fashion way */
 739           if (!SCpnt->request.bh)       
 740             this_count = SCpnt->request.nr_sectors;
 741           else
 742             this_count = (SCpnt->request.bh->b_size >> 9);
 743           
 744           start = block % 4;
 745           if (start)
 746             {                             
 747               this_count = ((this_count > 4 - start) ? 
 748                             (4 - start) : (this_count));
 749               buffer = (unsigned char *) scsi_malloc(2048);
 750             } 
 751           else if (this_count < 4)
 752             {
 753               buffer = (unsigned char *) scsi_malloc(2048);
 754             }
 755           else
 756             {
 757               this_count -= this_count % 4;
 758               buffer = (unsigned char *) SCpnt->request.buffer;
 759               if (((int) buffer) + (this_count << 9) > ISA_DMA_THRESHOLD & 
 760                   (SCpnt->host->unchecked_isa_dma))
 761                 buffer = (unsigned char *) scsi_malloc(this_count << 9);
 762             }
 763         };
 764 
 765         if (scsi_CDs[dev].sector_size == 2048)
 766           block = block >> 2; /* These are the sectors that the cdrom uses */
 767         else
 768           block = block & 0xfffffffc;
 769 
 770         realcount = (this_count + 3) / 4;
 771 
 772         if (scsi_CDs[dev].sector_size == 512) realcount = realcount << 2;
 773 
 774         if (((realcount > 0xff) || (block > 0x1fffff)) && scsi_CDs[dev].ten) 
 775                 {
 776                 if (realcount > 0xffff)
 777                         {
 778                         realcount = 0xffff;
 779                         this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
 780                         }
 781 
 782                 cmd[0] += READ_10 - READ_6 ;
 783                 cmd[2] = (unsigned char) (block >> 24) & 0xff;
 784                 cmd[3] = (unsigned char) (block >> 16) & 0xff;
 785                 cmd[4] = (unsigned char) (block >> 8) & 0xff;
 786                 cmd[5] = (unsigned char) block & 0xff;
 787                 cmd[6] = cmd[9] = 0;
 788                 cmd[7] = (unsigned char) (realcount >> 8) & 0xff;
 789                 cmd[8] = (unsigned char) realcount & 0xff;
 790                 }
 791         else
 792                 {
 793                   if (realcount > 0xff)
 794                     {
 795                       realcount = 0xff;
 796                       this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
 797                     }
 798                   
 799                   cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
 800                   cmd[2] = (unsigned char) ((block >> 8) & 0xff);
 801                   cmd[3] = (unsigned char) block & 0xff;
 802                   cmd[4] = (unsigned char) realcount;
 803                   cmd[5] = 0;
 804                 }   
 805 
 806 #ifdef DEBUG
 807         { 
 808           int i;
 809           printk("ReadCD: %d %d %d %d\n",block, realcount, buffer, this_count);
 810           printk("Use sg: %d\n", SCpnt->use_sg);
 811           printk("Dumping command: ");
 812           for(i=0; i<12; i++) printk("%2.2x ", cmd[i]);
 813           printk("\n");
 814         };
 815 #endif
 816 
 817 /* Some dumb host adapters can speed transfers by knowing the
 818  * minimum transfersize in advance.
 819  *
 820  * We shouldn't disconnect in the middle of a sector, but the cdrom
 821  * sector size can be larger than the size of a buffer and the
 822  * transfer may be split to the size of a buffer.  So it's safe to
 823  * assume that we can at least transfer the minimum of the buffer
 824  * size (1024) and the sector size between each connect / disconnect.
 825  */
 826 
 827         SCpnt->transfersize = (scsi_CDs[dev].sector_size > 1024) ?
 828                         1024 : scsi_CDs[dev].sector_size;
 829 
 830         SCpnt->this_count = this_count;
 831         scsi_do_cmd (SCpnt, (void *) cmd, buffer, 
 832                      realcount * scsi_CDs[dev].sector_size, 
 833                      rw_intr, SR_TIMEOUT, MAX_RETRIES);
 834 }
 835 
 836 static int sr_detect(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 837   
 838   if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 0;
 839 
 840   printk("Detected scsi CD-ROM sr%d at scsi%d, id %d, lun %d\n", 
 841          sr_template.dev_noticed++,
 842          SDp->host->host_no , SDp->id, SDp->lun); 
 843 
 844          return 1;
 845 }
 846 
 847 static int sr_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 848   Scsi_CD * cpnt;
 849   int i;
 850   
 851   if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 1;
 852   
 853   if (sr_template.nr_dev >= sr_template.dev_max)
 854     {
 855         SDp->attached--;
 856         return 1;
 857     }
 858   
 859   for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++) 
 860     if(!cpnt->device) break;
 861   
 862   if(i >= sr_template.dev_max) panic ("scsi_devices corrupt (sr)");
 863   
 864   SDp->scsi_request_fn = do_sr_request;
 865   scsi_CDs[i].device = SDp;
 866   sr_template.nr_dev++;
 867   if(sr_template.nr_dev > sr_template.dev_max)
 868     panic ("scsi_devices corrupt (sr)");
 869   return 0;
 870 }
 871      
 872 
 873 static void sr_init_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 874 {
 875   struct request * req;
 876   
 877   req = &SCpnt->request;
 878   req->dev = 0xfffe; /* Busy, but indicate request done */
 879   
 880   if (req->sem != NULL) {
 881     up(req->sem);
 882   }
 883 }
 884 
 885 static void get_sectorsize(int i){
     /* [previous][next][first][last][top][bottom][index][help] */
 886   unsigned char cmd[10];
 887   unsigned char *buffer;
 888   int the_result, retries;
 889   Scsi_Cmnd * SCpnt;
 890   
 891   buffer = (unsigned char *) scsi_malloc(512);
 892   SCpnt = allocate_device(NULL, scsi_CDs[i].device, 1);
 893 
 894   retries = 3;
 895   do {
 896     cmd[0] = READ_CAPACITY;
 897     cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
 898     memset ((void *) &cmd[2], 0, 8);
 899     SCpnt->request.dev = 0xffff;  /* Mark as really busy */
 900     SCpnt->cmd_len = 0;
 901     
 902     memset(buffer, 0, 8);
 903 
 904     scsi_do_cmd (SCpnt,
 905                  (void *) cmd, (void *) buffer,
 906                  512, sr_init_done,  SR_TIMEOUT,
 907                  MAX_RETRIES);
 908     
 909     if (current == task[0])
 910       while(SCpnt->request.dev != 0xfffe);
 911     else
 912       if (SCpnt->request.dev != 0xfffe){
 913         struct semaphore sem = MUTEX_LOCKED;
 914         SCpnt->request.sem = &sem;
 915         down(&sem);
 916         /* Hmm.. Have to ask about this */
 917         while (SCpnt->request.dev != 0xfffe) schedule();
 918       };
 919     
 920     the_result = SCpnt->result;
 921     retries--;
 922     
 923   } while(the_result && retries);
 924   
 925   SCpnt->request.dev = -1;  /* Mark as not busy */
 926   
 927   wake_up(&SCpnt->device->device_wait); 
 928 
 929   if (the_result) {
 930     scsi_CDs[i].capacity = 0x1fffff;
 931     scsi_CDs[i].sector_size = 2048;  /* A guess, just in case */
 932     scsi_CDs[i].needs_sector_size = 1;
 933   } else {
 934     scsi_CDs[i].capacity = (buffer[0] << 24) |
 935       (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
 936     scsi_CDs[i].sector_size = (buffer[4] << 24) |
 937       (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
 938     if(scsi_CDs[i].sector_size == 0) scsi_CDs[i].sector_size = 2048;
 939     if(scsi_CDs[i].sector_size != 2048 && 
 940        scsi_CDs[i].sector_size != 512) {
 941       printk ("scd%d : unsupported sector size %d.\n",
 942               i, scsi_CDs[i].sector_size);
 943       scsi_CDs[i].capacity = 0;
 944       scsi_CDs[i].needs_sector_size = 1;
 945     };
 946     if(scsi_CDs[i].sector_size == 2048)
 947       scsi_CDs[i].capacity *= 4;
 948     scsi_CDs[i].needs_sector_size = 0;
 949     sr_sizes[i] = scsi_CDs[i].capacity;
 950   };
 951   scsi_free(buffer, 512);
 952 }
 953 
 954 static void sr_init()
     /* [previous][next][first][last][top][bottom][index][help] */
 955 {
 956         int i;
 957         static int sr_registered = 0;
 958 
 959         if(sr_template.dev_noticed == 0) return;
 960 
 961         if(!sr_registered) {
 962           if (register_blkdev(MAJOR_NR,"sr",&sr_fops)) {
 963             printk("Unable to get major %d for SCSI-CD\n",MAJOR_NR);
 964             return;
 965           }
 966           sr_registered++;
 967         }
 968 
 969         
 970         if (scsi_CDs) return;
 971         sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
 972         scsi_CDs = (Scsi_CD *) scsi_init_malloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
 973         memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
 974 
 975         sr_sizes = (int *) scsi_init_malloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
 976         memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
 977 
 978         sr_blocksizes = (int *) scsi_init_malloc(sr_template.dev_max * 
 979                                                  sizeof(int), GFP_ATOMIC);
 980         for(i=0;i<sr_template.dev_max;i++) sr_blocksizes[i] = 2048;
 981         blksize_size[MAJOR_NR] = sr_blocksizes;
 982 
 983 }
 984 
 985 void sr_finish()
     /* [previous][next][first][last][top][bottom][index][help] */
 986 {
 987   int i;
 988 
 989         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
 990         blk_size[MAJOR_NR] = sr_sizes;  
 991 
 992         for (i = 0; i < sr_template.nr_dev; ++i)
 993                 {
 994                   /* If we have already seen this, then skip it.  Comes up
 995                      with loadable modules. */
 996                   if (scsi_CDs[i].capacity) continue;
 997                   scsi_CDs[i].capacity = 0x1fffff;
 998                   scsi_CDs[i].sector_size = 2048;  /* A guess, just in case */
 999                   scsi_CDs[i].needs_sector_size = 1;
1000 #if 0
1001                   /* seems better to leave this for later */
1002                   get_sectorsize(i);
1003                   printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
1004 #endif
1005                   scsi_CDs[i].use = 1;
1006                   scsi_CDs[i].ten = 1;
1007                   scsi_CDs[i].remap = 1;
1008                   sr_sizes[i] = scsi_CDs[i].capacity;
1009                 }
1010 
1011 
1012         /* If our host adapter is capable of scatter-gather, then we increase
1013            the read-ahead to 16 blocks (32 sectors).  If not, we use
1014            a two block (4 sector) read ahead. */
1015         if(scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
1016           read_ahead[MAJOR_NR] = 32;  /* 32 sector read-ahead.  Always removable. */
1017         else
1018           read_ahead[MAJOR_NR] = 4;  /* 4 sector read-ahead */
1019 
1020         return;
1021 }       
1022 
1023 static void sr_detach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
1024 {
1025   Scsi_CD * cpnt;
1026   int i, major;
1027   
1028   major = MAJOR_NR << 8;
1029 
1030   for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++) 
1031     if(cpnt->device == SDp) {
1032       /*
1033        * Since the cdrom is read-only, no need to sync the device.
1034        * We should be kind to our buffer cache, however.
1035        */
1036       invalidate_inodes(major | i);
1037       invalidate_buffers(major | i);
1038 
1039       /*
1040        * Reset things back to a sane state so that one can re-load a new
1041        * driver (perhaps the same one).
1042        */
1043       cpnt->device = NULL;
1044       cpnt->capacity = 0;
1045       SDp->attached--;
1046       sr_template.nr_dev--;
1047       sr_template.dev_noticed--;
1048       sr_sizes[i] = 0;
1049       return;
1050     }
1051   return;
1052 }

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