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

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