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
  15. init_module
  16. cleanup_module

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

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